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 | |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 82 | #define CHECK_PRESENCE(x) \ |
| 83 | if (!ops_smbus_bus(get_pbus_smbus(dev))->x) { \ |
| 84 | printk(BIOS_ERR, "%s missing " #x "\n", \ |
| 85 | dev_path(dev)); \ |
| 86 | return -1; \ |
| 87 | } |
| 88 | |
| 89 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 90 | int smbus_quick_read(device_t dev) |
| 91 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 92 | CHECK_PRESENCE(quick_read); |
| 93 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 94 | return ops_smbus_bus(get_pbus_smbus(dev))->quick_read(dev); |
| 95 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 96 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 97 | int smbus_quick_write(device_t dev) |
| 98 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 99 | CHECK_PRESENCE(quick_write); |
| 100 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 101 | return ops_smbus_bus(get_pbus_smbus(dev))->quick_write(dev); |
| 102 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 103 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 104 | int smbus_recv_byte(device_t dev) |
| 105 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 106 | CHECK_PRESENCE(recv_byte); |
| 107 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 108 | return ops_smbus_bus(get_pbus_smbus(dev))->recv_byte(dev); |
| 109 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 110 | |
| 111 | int smbus_send_byte(device_t dev, u8 byte) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 112 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 113 | CHECK_PRESENCE(send_byte); |
| 114 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 115 | return ops_smbus_bus(get_pbus_smbus(dev))->send_byte(dev, byte); |
| 116 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 117 | |
| 118 | int smbus_read_byte(device_t dev, u8 addr) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 119 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 120 | CHECK_PRESENCE(read_byte); |
| 121 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 122 | return ops_smbus_bus(get_pbus_smbus(dev))->read_byte(dev, addr); |
| 123 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 124 | |
| 125 | int smbus_write_byte(device_t dev, u8 addr, u8 val) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 126 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 127 | CHECK_PRESENCE(write_byte); |
| 128 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 129 | return ops_smbus_bus(get_pbus_smbus(dev))->write_byte(dev, addr, val); |
| 130 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 131 | |
| 132 | int smbus_read_word(device_t dev, u8 addr) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 133 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 134 | CHECK_PRESENCE(read_word); |
| 135 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 136 | return ops_smbus_bus(get_pbus_smbus(dev))->read_word(dev, addr); |
| 137 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 138 | |
| 139 | int smbus_write_word(device_t dev, u8 addr, u16 val) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 140 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 141 | CHECK_PRESENCE(write_word); |
| 142 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 143 | return ops_smbus_bus(get_pbus_smbus(dev))->write_word(dev, addr, val); |
| 144 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 145 | |
| 146 | int smbus_process_call(device_t dev, u8 cmd, u16 data) |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 147 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 148 | CHECK_PRESENCE(process_call); |
| 149 | |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 150 | return ops_smbus_bus(get_pbus_smbus(dev))->process_call(dev, cmd, data); |
| 151 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 152 | |
| 153 | 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] | 154 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 155 | CHECK_PRESENCE(block_read); |
| 156 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 157 | return ops_smbus_bus(get_pbus_smbus(dev))->block_read(dev, cmd, |
| 158 | bytes, buffer); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 159 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 160 | |
| 161 | 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] | 162 | { |
Vladimir Serbinenko | dec9198 | 2014-01-27 23:46:46 +0100 | [diff] [blame] | 163 | CHECK_PRESENCE(block_write); |
| 164 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 165 | return ops_smbus_bus(get_pbus_smbus(dev))->block_write(dev, cmd, |
| 166 | bytes, buffer); |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 167 | } |