Various cosmetic and coding style fixes in src/devices.
[coreboot.git] / src / devices / smbus_ops.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Tyan
5  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
6  * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <stdint.h>
24 #include <device/device.h>
25 #include <device/path.h>
26 #include <device/smbus.h>
27
28 struct bus *get_pbus_smbus(device_t dev)
29 {
30         struct bus *pbus = dev->bus;
31
32         while (pbus && pbus->dev && !ops_smbus_bus(pbus))
33                 pbus = pbus->dev->bus;
34
35         if (!pbus || !pbus->dev || !pbus->dev->ops
36             || !pbus->dev->ops->ops_smbus_bus) {
37                 printk(BIOS_ALERT, "%s Cannot find SMBus bus operations",
38                        dev_path(dev));
39                 die("");
40         }
41
42         return pbus;
43 }
44
45 /*
46  * Multi-level I2C MUX? May need to find the first I2C device and then set link
47  * down to current dev.
48  *
49  * 1 store get_pbus_smbus list link
50  * 2 reverse the link and call set link.
51  *
52  * @param dev TODO.
53  */
54 int smbus_set_link(device_t dev)
55 {
56         struct bus *pbus_a[4]; // 4 level mux only. Enough?
57         struct bus *pbus = dev->bus;
58         int pbus_num = 0;
59         int i;
60
61         while (pbus && pbus->dev && (pbus->dev->path.type == DEVICE_PATH_I2C)) {
62                 pbus_a[pbus_num++] = pbus;
63                 pbus = pbus->dev->bus;
64         }
65
66         // printk(BIOS_INFO, "smbus_set_link: ");
67         for (i = pbus_num - 1; i >= 0; i--) {
68                 // printk(BIOS_INFO, " %s[%d] -> ", dev_path(pbus_a[i]->dev),
69                 // pbus_a[i]->link);
70                 if (ops_smbus_bus(get_pbus_smbus(pbus_a[i]->dev))) {
71                         if (pbus_a[i]->dev->ops
72                             && pbus_a[i]->dev->ops->set_link)
73                                 pbus_a[i]->dev->ops->set_link(pbus_a[i]->dev,
74                                                         pbus_a[i]->link_num);
75                 }
76         }
77         // printk(BIOS_INFO, " %s\n", dev_path(dev));
78
79         return pbus_num;
80 }
81
82 int smbus_quick_read(device_t dev)
83 {
84         return ops_smbus_bus(get_pbus_smbus(dev))->quick_read(dev);
85 }
86
87 int smbus_quick_write(device_t dev)
88 {
89         return ops_smbus_bus(get_pbus_smbus(dev))->quick_write(dev);
90 }
91
92 int smbus_recv_byte(device_t dev)
93 {
94         return ops_smbus_bus(get_pbus_smbus(dev))->recv_byte(dev);
95 }
96
97 int smbus_send_byte(device_t dev, u8 byte)
98 {
99         return ops_smbus_bus(get_pbus_smbus(dev))->send_byte(dev, byte);
100 }
101
102 int smbus_read_byte(device_t dev, u8 addr)
103 {
104         return ops_smbus_bus(get_pbus_smbus(dev))->read_byte(dev, addr);
105 }
106
107 int smbus_write_byte(device_t dev, u8 addr, u8 val)
108 {
109         return ops_smbus_bus(get_pbus_smbus(dev))->write_byte(dev, addr, val);
110 }
111
112 int smbus_read_word(device_t dev, u8 addr)
113 {
114         return ops_smbus_bus(get_pbus_smbus(dev))->read_word(dev, addr);
115 }
116
117 int smbus_write_word(device_t dev, u8 addr, u16 val)
118 {
119         return ops_smbus_bus(get_pbus_smbus(dev))->write_word(dev, addr, val);
120 }
121
122 int smbus_process_call(device_t dev, u8 cmd, u16 data)
123 {
124         return ops_smbus_bus(get_pbus_smbus(dev))->process_call(dev, cmd, data);
125 }
126
127 int smbus_block_read(device_t dev, u8 cmd, u8 bytes, u8 *buffer)
128 {
129         return ops_smbus_bus(get_pbus_smbus(dev))->block_read(dev, cmd,
130                                                               bytes, buffer);
131 }
132
133 int smbus_block_write(device_t dev, u8 cmd, u8 bytes, const u8 *buffer)
134 {
135         return ops_smbus_bus(get_pbus_smbus(dev))->block_write(dev, cmd,
136                                                                bytes, buffer);
137 }