Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 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 |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 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; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 31 | |
| 32 | while (pbus && pbus->dev && !ops_smbus_bus(pbus)) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 33 | pbus = pbus->dev->bus; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 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)); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 39 | die(""); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 40 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 41 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 42 | return pbus; |
| 43 | } |
| 44 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 45 | /* |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 46 | * Multi-level I2C MUX? May need to find the first I2C device and then set link |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 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 | */ |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 54 | int smbus_set_link(device_t dev) |
| 55 | { |
| 56 | struct bus *pbus_a[4]; // 4 level mux only. Enough? |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 57 | struct bus *pbus = dev->bus; |
| 58 | int pbus_num = 0; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 59 | int i; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 60 | |
| 61 | while (pbus && pbus->dev && (pbus->dev->path.type == DEVICE_PATH_I2C)) { |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 62 | pbus_a[pbus_num++] = pbus; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 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); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 77 | // printk(BIOS_INFO, " %s\n", dev_path(dev)); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 78 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 79 | return pbus_num; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int smbus_quick_read(device_t dev) |
| 83 | { |
| 84 | return ops_smbus_bus(get_pbus_smbus(dev))->quick_read(dev); |
| 85 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 86 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 87 | int smbus_quick_write(device_t dev) |
| 88 | { |
| 89 | return ops_smbus_bus(get_pbus_smbus(dev))->quick_write(dev); |
| 90 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 91 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 92 | int smbus_recv_byte(device_t dev) |
| 93 | { |
| 94 | return ops_smbus_bus(get_pbus_smbus(dev))->recv_byte(dev); |
| 95 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 96 | |
| 97 | int smbus_send_byte(device_t dev, u8 byte) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 98 | { |
| 99 | return ops_smbus_bus(get_pbus_smbus(dev))->send_byte(dev, byte); |
| 100 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 101 | |
| 102 | int smbus_read_byte(device_t dev, u8 addr) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 103 | { |
| 104 | return ops_smbus_bus(get_pbus_smbus(dev))->read_byte(dev, addr); |
| 105 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 106 | |
| 107 | int smbus_write_byte(device_t dev, u8 addr, u8 val) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 108 | { |
| 109 | return ops_smbus_bus(get_pbus_smbus(dev))->write_byte(dev, addr, val); |
| 110 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 111 | |
| 112 | int smbus_read_word(device_t dev, u8 addr) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 113 | { |
| 114 | return ops_smbus_bus(get_pbus_smbus(dev))->read_word(dev, addr); |
| 115 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 116 | |
| 117 | int smbus_write_word(device_t dev, u8 addr, u16 val) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 118 | { |
| 119 | return ops_smbus_bus(get_pbus_smbus(dev))->write_word(dev, addr, val); |
| 120 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 121 | |
| 122 | int smbus_process_call(device_t dev, u8 cmd, u16 data) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 123 | { |
| 124 | return ops_smbus_bus(get_pbus_smbus(dev))->process_call(dev, cmd, data); |
| 125 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 126 | |
| 127 | int smbus_block_read(device_t dev, u8 cmd, u8 bytes, u8 *buffer) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 128 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 129 | return ops_smbus_bus(get_pbus_smbus(dev))->block_read(dev, cmd, |
| 130 | bytes, buffer); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 131 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 132 | |
| 133 | int smbus_block_write(device_t dev, u8 cmd, u8 bytes, const u8 *buffer) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 134 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 135 | return ops_smbus_bus(get_pbus_smbus(dev))->block_write(dev, cmd, |
| 136 | bytes, buffer); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 137 | } |