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