9d134619b89d771f4e23fbbf083d73940dc09044
[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         while (pbus && pbus->dev && !ops_smbus_bus(pbus)) {
32                 pbus = pbus->dev->bus;
33         }
34         if (!pbus || !pbus->dev || !pbus->dev->ops || !pbus->dev->ops->ops_smbus_bus) {
35                 printk(BIOS_ALERT, "%s Cannot find smbus bus operations", dev_path(dev));
36                 die("");
37         }
38         return pbus;
39 }
40
41 /*
42  * Multi-level I2C MUX? may need to find the first i2c device and then set link
43  * down to current dev.
44  *
45  * 1 store get_pbus_smbus list link
46  * 2 reverse the link and call set link.
47  *
48  * @param dev TODO.
49  */
50 int smbus_set_link(device_t dev)
51 {
52         struct bus *pbus_a[4]; // 4 level mux only. Enough?
53         struct bus *pbus = dev->bus;
54         int pbus_num=0;
55         int i;
56         while(pbus && pbus->dev && (pbus->dev->path.type==DEVICE_PATH_I2C)) {
57                 pbus_a[pbus_num++] = pbus;
58                 pbus = pbus->dev->bus;
59         }
60 //      printk(BIOS_INFO, "smbus_set_link: ");
61         for (i=pbus_num-1; i>=0; i--) {
62 //              printk(BIOS_INFO, " %s[%d] -> ", dev_path(pbus_a[i]->dev), pbus_a[i]->link);
63                 if (ops_smbus_bus(get_pbus_smbus(pbus_a[i]->dev))) {
64                         if (pbus_a[i]->dev->ops && pbus_a[i]->dev->ops->set_link)
65                                 pbus_a[i]->dev->ops->set_link(pbus_a[i]->dev, pbus_a[i]->link_num);
66                 }
67         }
68 //      printk(BIOS_INFO, " %s\n", dev_path(dev));
69
70         return pbus_num;
71 }
72
73 int smbus_quick_read(device_t dev)
74 {
75         return ops_smbus_bus(get_pbus_smbus(dev))->quick_read(dev);
76 }
77 int smbus_quick_write(device_t dev)
78 {
79         return ops_smbus_bus(get_pbus_smbus(dev))->quick_write(dev);
80 }
81 int smbus_recv_byte(device_t dev)
82 {
83         return ops_smbus_bus(get_pbus_smbus(dev))->recv_byte(dev);
84 }
85 int smbus_send_byte(device_t dev, uint8_t byte)
86 {
87         return ops_smbus_bus(get_pbus_smbus(dev))->send_byte(dev, byte);
88 }
89 int smbus_read_byte(device_t dev, uint8_t addr)
90 {
91         return ops_smbus_bus(get_pbus_smbus(dev))->read_byte(dev, addr);
92 }
93 int smbus_write_byte(device_t dev, uint8_t addr, uint8_t val)
94 {
95         return ops_smbus_bus(get_pbus_smbus(dev))->write_byte(dev, addr, val);
96 }
97 int smbus_read_word(device_t dev, uint8_t addr)
98 {
99         return ops_smbus_bus(get_pbus_smbus(dev))->read_word(dev, addr);
100 }
101 int smbus_write_word(device_t dev, uint8_t addr, uint16_t val)
102 {
103         return ops_smbus_bus(get_pbus_smbus(dev))->write_word(dev, addr, val);
104 }
105 int smbus_process_call(device_t dev, uint8_t cmd, uint16_t data)
106 {
107         return ops_smbus_bus(get_pbus_smbus(dev))->process_call(dev, cmd, data);
108 }
109 int smbus_block_read(device_t dev, uint8_t cmd, uint8_t bytes, uint8_t *buffer)
110 {
111         return ops_smbus_bus(get_pbus_smbus(dev))->block_read(dev, cmd, bytes, buffer);
112 }
113 int smbus_block_write(device_t dev, uint8_t cmd, uint8_t bytes, const uint8_t *buffer)
114 {
115         return ops_smbus_bus(get_pbus_smbus(dev))->block_write(dev, cmd, bytes, buffer);
116 }