Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <arch/io.h> |
Kyösti Mälkki | b5d998b | 2017-08-20 21:36:08 +0300 | [diff] [blame] | 16 | #include <console/console.h> |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 17 | #include <device/smbus_def.h> |
Kyösti Mälkki | 1cae454 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 18 | #include <device/smbus_host.h> |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 19 | #include <types.h> |
| 20 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 21 | #if CONFIG(DEBUG_SMBUS) |
Kyösti Mälkki | b5d998b | 2017-08-20 21:36:08 +0300 | [diff] [blame] | 22 | #define dprintk(args...) printk(BIOS_DEBUG, ##args) |
| 23 | #else |
| 24 | #define dprintk(args...) do {} while (0) |
| 25 | #endif |
| 26 | |
Kyösti Mälkki | 7f40bd6 | 2020-01-06 19:00:31 +0200 | [diff] [blame] | 27 | /* SMBus register offsets. */ |
| 28 | #define SMBHSTSTAT 0x0 |
| 29 | #define SMBHSTCTL 0x2 |
| 30 | #define SMBHSTCMD 0x3 |
| 31 | #define SMBXMITADD 0x4 |
| 32 | #define SMBHSTDAT0 0x5 |
| 33 | #define SMBHSTDAT1 0x6 |
| 34 | #define SMBBLKDAT 0x7 |
| 35 | #define SMBTRNSADD 0x9 |
| 36 | #define SMBSLVDATA 0xa |
| 37 | #define SMLINK_PIN_CTL 0xe |
| 38 | #define SMBUS_PIN_CTL 0xf |
| 39 | #define SMBSLVCMD 0x11 |
| 40 | |
| 41 | #define SMB_RCV_SLVA SMBTRNSADD |
| 42 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 43 | /* I801 command constants */ |
| 44 | #define I801_QUICK (0 << 2) |
| 45 | #define I801_BYTE (1 << 2) |
| 46 | #define I801_BYTE_DATA (2 << 2) |
| 47 | #define I801_WORD_DATA (3 << 2) |
| 48 | #define I801_BLOCK_DATA (5 << 2) |
| 49 | #define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */ |
| 50 | |
| 51 | /* I801 Host Control register bits */ |
| 52 | #define SMBHSTCNT_INTREN (1 << 0) |
| 53 | #define SMBHSTCNT_KILL (1 << 1) |
| 54 | #define SMBHSTCNT_LAST_BYTE (1 << 5) |
| 55 | #define SMBHSTCNT_START (1 << 6) |
| 56 | #define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */ |
| 57 | |
| 58 | /* I801 Hosts Status register bits */ |
| 59 | #define SMBHSTSTS_BYTE_DONE (1 << 7) |
| 60 | #define SMBHSTSTS_INUSE_STS (1 << 6) |
| 61 | #define SMBHSTSTS_SMBALERT_STS (1 << 5) |
| 62 | #define SMBHSTSTS_FAILED (1 << 4) |
| 63 | #define SMBHSTSTS_BUS_ERR (1 << 3) |
| 64 | #define SMBHSTSTS_DEV_ERR (1 << 2) |
| 65 | #define SMBHSTSTS_INTR (1 << 1) |
| 66 | #define SMBHSTSTS_HOST_BUSY (1 << 0) |
| 67 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 68 | /* For SMBXMITADD register. */ |
| 69 | #define XMIT_WRITE(dev) (((dev) << 1) | 0) |
| 70 | #define XMIT_READ(dev) (((dev) << 1) | 1) |
| 71 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 72 | #define SMBUS_TIMEOUT (10 * 1000 * 100) |
Elyes HAOUAS | b0f1988 | 2018-06-09 11:59:00 +0200 | [diff] [blame] | 73 | #define SMBUS_BLOCK_MAXLEN 32 |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 74 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 75 | /* block_cmd_loop flags */ |
| 76 | #define BLOCK_READ 0 |
| 77 | #define BLOCK_WRITE (1 << 0) |
| 78 | #define BLOCK_I2C (1 << 1) |
| 79 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 80 | static void smbus_delay(void) |
| 81 | { |
| 82 | inb(0x80); |
| 83 | } |
| 84 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 85 | static void host_outb(uintptr_t base, u8 reg, u8 value) |
Kyösti Mälkki | b49638d | 2020-01-02 16:36:56 +0200 | [diff] [blame] | 86 | { |
| 87 | outb(value, base + reg); |
| 88 | } |
| 89 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 90 | static u8 host_inb(uintptr_t base, u8 reg) |
Kyösti Mälkki | b49638d | 2020-01-02 16:36:56 +0200 | [diff] [blame] | 91 | { |
| 92 | return inb(base + reg); |
| 93 | } |
| 94 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 95 | static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or) |
Kyösti Mälkki | 65f5de2 | 2020-01-02 16:36:56 +0200 | [diff] [blame] | 96 | { |
| 97 | u8 value; |
| 98 | value = host_inb(base, reg); |
| 99 | value &= mask; |
| 100 | value |= or; |
| 101 | host_outb(base, reg, value); |
| 102 | } |
| 103 | |
Kyösti Mälkki | 7cdcc38 | 2020-01-06 19:00:31 +0200 | [diff] [blame] | 104 | void smbus_host_reset(uintptr_t base) |
| 105 | { |
| 106 | /* Disable interrupt generation. */ |
| 107 | host_outb(base, SMBHSTCTL, 0); |
| 108 | |
| 109 | /* Clear any lingering errors, so transactions can run. */ |
| 110 | host_and_or(base, SMBHSTSTAT, 0xff, 0); |
| 111 | } |
| 112 | |
Kyösti Mälkki | 73451fd | 2020-01-06 19:00:31 +0200 | [diff] [blame] | 113 | void smbus_set_slave_addr(uintptr_t base, u8 slave_address) |
| 114 | { |
| 115 | host_outb(base, SMB_RCV_SLVA, slave_address); |
| 116 | } |
| 117 | |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 118 | static int host_completed(u8 status) |
| 119 | { |
| 120 | if (status & SMBHSTSTS_HOST_BUSY) |
| 121 | return 0; |
Kyösti Mälkki | 44206e3 | 2019-02-26 17:17:24 +0200 | [diff] [blame] | 122 | |
| 123 | /* These status bits do not imply completion of transaction. */ |
| 124 | status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS | |
| 125 | SMBHSTSTS_SMBALERT_STS); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 126 | return status != 0; |
| 127 | } |
| 128 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 129 | static int recover_master(uintptr_t base, int ret) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 130 | { |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 131 | /* TODO: Depending of the failure, drive KILL transaction |
| 132 | * or force soft reset on SMBus master controller. |
| 133 | */ |
| 134 | printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret); |
| 135 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 138 | static int cb_err_from_stat(u8 status) |
| 139 | { |
Kyösti Mälkki | 44206e3 | 2019-02-26 17:17:24 +0200 | [diff] [blame] | 140 | /* These status bits do not imply errors. */ |
| 141 | status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS | |
| 142 | SMBHSTSTS_SMBALERT_STS); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 143 | |
| 144 | if (status == SMBHSTSTS_INTR) |
| 145 | return 0; |
| 146 | |
| 147 | return SMBUS_ERROR; |
| 148 | } |
| 149 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 150 | static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 151 | { |
| 152 | unsigned int loops = SMBUS_TIMEOUT; |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 153 | u8 host_busy; |
| 154 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 155 | do { |
| 156 | smbus_delay(); |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 157 | host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY; |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 158 | } while (--loops && host_busy); |
| 159 | |
| 160 | if (loops == 0) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 161 | return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 162 | |
| 163 | /* Clear any lingering errors, so the transaction will run. */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 164 | host_and_or(base, SMBHSTSTAT, 0xff, 0); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 165 | |
| 166 | /* Set up transaction */ |
| 167 | /* Disable interrupts */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 168 | host_outb(base, SMBHSTCTL, ctrl); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 169 | |
| 170 | /* Set the device I'm talking to. */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 171 | host_outb(base, SMBXMITADD, xmitadd); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 172 | |
| 173 | return 0; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 176 | static int execute_command(uintptr_t base) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 177 | { |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 178 | unsigned int loops = SMBUS_TIMEOUT; |
| 179 | u8 status; |
| 180 | |
| 181 | /* Start the command. */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 182 | host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START); |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 183 | |
| 184 | /* Poll for it to start. */ |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 185 | do { |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 186 | smbus_delay(); |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 187 | |
| 188 | /* If we poll too slow, we could miss HOST_BUSY flag |
| 189 | * set and detect INTR or x_ERR flags instead here. |
| 190 | */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 191 | status = host_inb(base, SMBHSTSTAT); |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 192 | status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS); |
| 193 | } while (--loops && status == 0); |
| 194 | |
| 195 | if (loops == 0) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 196 | return recover_master(base, |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 197 | SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT); |
| 198 | |
| 199 | return 0; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 202 | static int complete_command(uintptr_t base) |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 203 | { |
| 204 | unsigned int loops = SMBUS_TIMEOUT; |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 205 | u8 status; |
| 206 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 207 | do { |
| 208 | smbus_delay(); |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 209 | status = host_inb(base, SMBHSTSTAT); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 210 | } while (--loops && !host_completed(status)); |
| 211 | |
| 212 | if (loops == 0) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 213 | return recover_master(base, |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 214 | SMBUS_WAIT_UNTIL_DONE_TIMEOUT); |
| 215 | |
| 216 | return cb_err_from_stat(status); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 217 | } |
| 218 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 219 | static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 220 | { |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 221 | int ret; |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 222 | u16 word; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 223 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 224 | /* Set up for a byte data read. */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 225 | ret = setup_command(base, ctrl, XMIT_READ(device)); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 226 | if (ret < 0) |
| 227 | return ret; |
| 228 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 229 | /* Set the command/address... */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 230 | host_outb(base, SMBHSTCMD, address); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 231 | |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 232 | /* Clear the data bytes... */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 233 | host_outb(base, SMBHSTDAT0, 0); |
| 234 | host_outb(base, SMBHSTDAT1, 0); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 235 | |
| 236 | /* Start the command */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 237 | ret = execute_command(base); |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 238 | if (ret < 0) |
| 239 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 240 | |
| 241 | /* Poll for transaction completion */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 242 | ret = complete_command(base); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 243 | if (ret < 0) |
| 244 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 245 | |
| 246 | /* Read results of transaction */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 247 | word = host_inb(base, SMBHSTDAT0); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 248 | if (ctrl == I801_WORD_DATA) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 249 | word |= host_inb(base, SMBHSTDAT1) << 8; |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 250 | |
| 251 | return word; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 254 | static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 255 | { |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 256 | int ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 257 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 258 | /* Set up for a byte data write. */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 259 | ret = setup_command(base, ctrl, XMIT_WRITE(device)); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 260 | if (ret < 0) |
| 261 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 262 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 263 | /* Set the command/address... */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 264 | host_outb(base, SMBHSTCMD, address); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 265 | |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 266 | /* Set the data bytes... */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 267 | host_outb(base, SMBHSTDAT0, data & 0xff); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 268 | if (ctrl == I801_WORD_DATA) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 269 | host_outb(base, SMBHSTDAT1, data >> 8); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 270 | |
| 271 | /* Start the command */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 272 | ret = execute_command(base); |
Kyösti Mälkki | a2dcf73 | 2017-08-20 21:36:15 +0300 | [diff] [blame] | 273 | if (ret < 0) |
| 274 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 275 | |
| 276 | /* Poll for transaction completion */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 277 | return complete_command(base); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 280 | static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags) |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 281 | { |
| 282 | u8 status; |
| 283 | unsigned int loops = SMBUS_TIMEOUT; |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 284 | int ret; |
| 285 | size_t bytes = 0; |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 286 | int is_write_cmd = flags & BLOCK_WRITE; |
| 287 | int sw_drives_nak = flags & BLOCK_I2C; |
| 288 | |
| 289 | /* Hardware limitations. */ |
| 290 | if (flags == (BLOCK_WRITE | BLOCK_I2C)) |
| 291 | return SMBUS_ERROR; |
| 292 | |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 293 | /* Set number of bytes to transfer. */ |
| 294 | /* Reset number of bytes to transfer so we notice later it |
| 295 | * was really updated with the transaction. */ |
| 296 | if (!sw_drives_nak) { |
| 297 | if (is_write_cmd) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 298 | host_outb(base, SMBHSTDAT0, max_bytes); |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 299 | else |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 300 | host_outb(base, SMBHSTDAT0, 0); |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Send first byte from buffer, bytes_sent increments after |
| 304 | * hardware acknowledges it. |
| 305 | */ |
| 306 | if (is_write_cmd) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 307 | host_outb(base, SMBBLKDAT, *buf++); |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 308 | |
| 309 | /* Start the command */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 310 | ret = execute_command(base); |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 311 | if (ret < 0) |
| 312 | return ret; |
| 313 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 314 | /* Poll for transaction completion */ |
| 315 | do { |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 316 | status = host_inb(base, SMBHSTSTAT); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 317 | |
| 318 | if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */ |
| 319 | |
| 320 | if (is_write_cmd) { |
| 321 | bytes++; |
| 322 | if (bytes < max_bytes) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 323 | host_outb(base, SMBBLKDAT, *buf++); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 324 | } else { |
| 325 | if (bytes < max_bytes) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 326 | *buf++ = host_inb(base, SMBBLKDAT); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 327 | bytes++; |
| 328 | |
| 329 | /* Indicate that next byte is the last one. */ |
| 330 | if (sw_drives_nak && (bytes + 1 >= max_bytes)) { |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 331 | host_and_or(base, SMBHSTCTL, 0xff, |
| 332 | SMBHSTCNT_LAST_BYTE); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | } |
| 336 | |
| 337 | /* Engine internally completes the transaction |
| 338 | * and clears HOST_BUSY flag once the byte count |
| 339 | * has been reached or LAST_BYTE was set. |
| 340 | */ |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 341 | host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | } while (--loops && !host_completed(status)); |
| 345 | |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 346 | dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n", |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 347 | __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops); |
| 348 | |
| 349 | if (loops == 0) |
Kyösti Mälkki | 5e9ae0c | 2020-01-06 13:35:59 +0200 | [diff] [blame] | 350 | return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 351 | |
| 352 | ret = cb_err_from_stat(status); |
| 353 | if (ret < 0) |
| 354 | return ret; |
| 355 | |
| 356 | return bytes; |
| 357 | } |
| 358 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 359 | int do_smbus_read_byte(uintptr_t base, u8 device, u8 address) |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 360 | { |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 361 | return smbus_read_cmd(base, I801_BYTE_DATA, device, address); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 364 | int do_smbus_read_word(uintptr_t base, u8 device, u8 address) |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 365 | { |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 366 | return smbus_read_cmd(base, I801_WORD_DATA, device, address); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 369 | int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data) |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 370 | { |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 371 | return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 374 | int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data) |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 375 | { |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 376 | return smbus_write_cmd(base, I801_WORD_DATA, device, address, data); |
Kyösti Mälkki | 7ca19b2 | 2020-01-02 17:02:54 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 379 | int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 380 | { |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 381 | int ret, slave_bytes; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 382 | |
Kyösti Mälkki | c17e855 | 2017-08-20 23:48:23 +0300 | [diff] [blame] | 383 | max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes); |
Arthur Heymans | 1b04aa2 | 2017-08-04 14:28:50 +0200 | [diff] [blame] | 384 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 385 | /* Set up for a block data read. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 386 | ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device)); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 387 | if (ret < 0) |
| 388 | return ret; |
| 389 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 390 | /* Set the command/address... */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 391 | host_outb(base, SMBHSTCMD, cmd); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 392 | |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 393 | /* Execute block transaction. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 394 | ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 395 | if (ret < 0) |
| 396 | return ret; |
| 397 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 398 | /* Post-check we received complete message. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 399 | slave_bytes = host_inb(base, SMBHSTDAT0); |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 400 | if (ret < slave_bytes) |
Arthur Heymans | 1b04aa2 | 2017-08-04 14:28:50 +0200 | [diff] [blame] | 401 | return SMBUS_ERROR; |
| 402 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 403 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 406 | int do_smbus_block_write(uintptr_t base, u8 device, u8 cmd, const size_t bytes, const u8 *buf) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 407 | { |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 408 | int ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 409 | |
Kyösti Mälkki | c17e855 | 2017-08-20 23:48:23 +0300 | [diff] [blame] | 410 | if (bytes > SMBUS_BLOCK_MAXLEN) |
Arthur Heymans | 1b04aa2 | 2017-08-04 14:28:50 +0200 | [diff] [blame] | 411 | return SMBUS_ERROR; |
| 412 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 413 | /* Set up for a block data write. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 414 | ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device)); |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 415 | if (ret < 0) |
| 416 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 417 | |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 418 | /* Set the command/address... */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 419 | host_outb(base, SMBHSTCMD, cmd); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 420 | |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 421 | /* Execute block transaction. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 422 | ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 423 | if (ret < 0) |
| 424 | return ret; |
| 425 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 426 | if (ret < bytes) |
Kyösti Mälkki | c17e855 | 2017-08-20 23:48:23 +0300 | [diff] [blame] | 427 | return SMBUS_ERROR; |
| 428 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 429 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* Only since ICH5 */ |
Kyösti Mälkki | c01a505 | 2019-01-30 09:39:23 +0200 | [diff] [blame] | 433 | static int has_i2c_read_command(void) |
| 434 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 435 | if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) || |
| 436 | CONFIG(SOUTHBRIDGE_INTEL_I82801DX)) |
Kyösti Mälkki | c01a505 | 2019-01-30 09:39:23 +0200 | [diff] [blame] | 437 | return 0; |
| 438 | return 1; |
| 439 | } |
| 440 | |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 441 | int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf) |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 442 | { |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 443 | int ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 444 | |
Kyösti Mälkki | c01a505 | 2019-01-30 09:39:23 +0200 | [diff] [blame] | 445 | if (!has_i2c_read_command()) |
| 446 | return SMBUS_ERROR; |
| 447 | |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 448 | /* Set up for a i2c block data read. |
| 449 | * |
| 450 | * FIXME: Address parameter changes to XMIT_READ(device) with |
| 451 | * some revision of PCH. Presumably hardware revisions that |
| 452 | * do not have i2c block write support internally set LSB. |
| 453 | */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 454 | ret = setup_command(base, I801_I2C_BLOCK_DATA, |
Kyösti Mälkki | 957511c | 2017-08-20 21:36:11 +0300 | [diff] [blame] | 455 | XMIT_WRITE(device)); |
| 456 | if (ret < 0) |
| 457 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 458 | |
| 459 | /* device offset */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 460 | host_outb(base, SMBHSTDAT1, offset); |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 461 | |
Kyösti Mälkki | d6c15d0 | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 462 | /* Execute block transaction. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 463 | ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C); |
Kyösti Mälkki | c38d543 | 2017-08-20 21:36:18 +0300 | [diff] [blame] | 464 | if (ret < 0) |
| 465 | return ret; |
| 466 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 467 | /* Post-check we received complete message. */ |
| 468 | if (ret < bytes) |
Kyösti Mälkki | 1e39236 | 2017-08-20 21:36:03 +0300 | [diff] [blame] | 469 | return SMBUS_ERROR; |
| 470 | |
Kyösti Mälkki | 893edee | 2017-08-20 21:36:24 +0300 | [diff] [blame] | 471 | return ret; |
Arthur Heymans | 16fe790 | 2017-04-12 17:01:31 +0200 | [diff] [blame] | 472 | } |
Frans Hendriks | e48be35 | 2019-06-19 11:01:27 +0200 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * The caller is responsible of settings HOSTC I2C_EN bit prior to making this |
| 476 | * call! |
| 477 | */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 478 | int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf) |
Frans Hendriks | e48be35 | 2019-06-19 11:01:27 +0200 | [diff] [blame] | 479 | { |
| 480 | u8 cmd; |
| 481 | int ret; |
| 482 | |
| 483 | if (!CONFIG(SOC_INTEL_BRASWELL)) |
| 484 | return SMBUS_ERROR; |
| 485 | |
| 486 | if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN)) |
| 487 | return SMBUS_ERROR; |
| 488 | |
| 489 | /* Set up for a block data write. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 490 | ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device)); |
Frans Hendriks | e48be35 | 2019-06-19 11:01:27 +0200 | [diff] [blame] | 491 | if (ret < 0) |
| 492 | return ret; |
| 493 | |
| 494 | /* |
| 495 | * In i2c mode SMBus controller sequence on bus will be: |
| 496 | * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT> |
| 497 | * The SMBHSTCMD must be written also to ensure the SMBUs controller |
| 498 | * will generate the i2c sequence. |
| 499 | */ |
| 500 | cmd = *buf++; |
| 501 | bytes--; |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 502 | host_outb(base, SMBHSTCMD, cmd); |
| 503 | host_outb(base, SMBHSTDAT1, cmd); |
Frans Hendriks | e48be35 | 2019-06-19 11:01:27 +0200 | [diff] [blame] | 504 | |
| 505 | /* Execute block transaction. */ |
Kyösti Mälkki | c528426 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 506 | ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE); |
Frans Hendriks | e48be35 | 2019-06-19 11:01:27 +0200 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | return ret; |
| 509 | |
| 510 | if (ret < bytes) |
| 511 | return SMBUS_ERROR; |
| 512 | |
| 513 | ret++; /* 1st byte has been written using SMBHSTDAT1 */ |
| 514 | return ret; |
| 515 | } |