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