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