Angel Pons | 230e4f9d | 2020-04-05 15:47:14 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 2 | |
Lee Leahy | 14d0926 | 2016-07-21 09:17:10 -0700 | [diff] [blame] | 3 | #include <assert.h> |
Elyes HAOUAS | f97c1c9 | 2019-12-03 18:22:06 +0100 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <delay.h> |
| 7 | #include <device/device.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 8 | #include <device/i2c_simple.h> |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 9 | #include <device/pci.h> |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 10 | #include <soc/i2c.h> |
| 11 | #include <soc/ramstage.h> |
| 12 | #include <soc/reg_access.h> |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 13 | #include <timer.h> |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 14 | |
| 15 | static void i2c_disable(I2C_REGS *regs) |
| 16 | { |
| 17 | uint32_t status; |
| 18 | uint32_t timeout; |
| 19 | |
| 20 | /* Disable I2C controller */ |
| 21 | regs->ic_enable = 0; |
| 22 | |
| 23 | /* Wait for the enable bit to clear */ |
| 24 | timeout = 1 * 1000 * 1000; |
| 25 | status = regs->ic_enable_status; |
| 26 | while (status & IC_ENABLE_CONTROLLER) { |
| 27 | udelay(1); |
| 28 | if (--timeout == 0) |
Keith Short | 15588b0 | 2019-05-09 11:40:34 -0600 | [diff] [blame] | 29 | die_with_post_code(POST_HW_INIT_FAILURE, |
| 30 | "ERROR - I2C failed to disable!\n"); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 31 | status = regs->ic_enable_status; |
| 32 | } |
| 33 | |
| 34 | /* Clear any pending interrupts */ |
| 35 | status = regs->ic_clr_intr; |
| 36 | } |
| 37 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 38 | static int platform_i2c_write(uint32_t restart, uint8_t *tx_buffer, int length, |
| 39 | uint32_t stop, uint8_t *rx_buffer, struct stopwatch *timeout) |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 40 | { |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 41 | int bytes_transferred; |
| 42 | uint32_t cmd; |
| 43 | I2C_REGS *regs; |
| 44 | uint32_t status; |
| 45 | |
| 46 | ASSERT(tx_buffer != NULL); |
| 47 | ASSERT(timeout != NULL); |
| 48 | regs = get_i2c_address(); |
| 49 | |
| 50 | /* Fill the FIFO with the write operation */ |
| 51 | bytes_transferred = 0; |
| 52 | do { |
| 53 | status = regs->ic_raw_intr_stat; |
| 54 | |
| 55 | /* Check for errors */ |
| 56 | if (status & (IC_INTR_RX_OVER | IC_INTR_RX_UNDER |
| 57 | | IC_INTR_TX_ABRT | IC_INTR_TX_OVER)) { |
| 58 | i2c_disable(regs); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 59 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 60 | printk(BIOS_ERR, |
| 61 | "0x%08x: ic_raw_intr_stat, I2C write error!\n", |
| 62 | status); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | /* Check for timeout */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 67 | if (stopwatch_expired(timeout)) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 68 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 69 | printk(BIOS_ERR, |
| 70 | "0x%08x: ic_raw_intr_stat, I2C write timeout!\n", |
| 71 | status); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 72 | return -1; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 73 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 74 | |
| 75 | /* Receive any available data */ |
| 76 | status = regs->ic_status; |
| 77 | if (rx_buffer != NULL) { |
| 78 | while (status & IC_STATUS_RFNE) { |
| 79 | *rx_buffer++ = (uint8_t)regs->ic_data_cmd; |
| 80 | bytes_transferred++; |
| 81 | status = regs->ic_status; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Determine if space is available in the FIFO */ |
| 86 | if (status & IC_STATUS_TFNF) { |
| 87 | /* End of the transaction? */ |
| 88 | cmd = IC_DATA_CMD_WRITE | *tx_buffer++ | restart; |
| 89 | if (length == 1) |
| 90 | cmd |= stop; |
| 91 | restart = 0; |
| 92 | |
| 93 | /* Place a data byte into the FIFO */ |
| 94 | regs->ic_data_cmd = cmd; |
| 95 | length--; |
| 96 | bytes_transferred++; |
| 97 | } else |
| 98 | udelay(1); |
| 99 | } while (length > 0); |
| 100 | return bytes_transferred; |
| 101 | } |
| 102 | |
| 103 | static int platform_i2c_read(uint32_t restart, uint8_t *rx_buffer, int length, |
| 104 | uint32_t stop, struct stopwatch *timeout) |
| 105 | { |
| 106 | int bytes_transferred; |
| 107 | uint32_t cmd; |
| 108 | int fifo_bytes; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 109 | I2C_REGS *regs; |
| 110 | uint32_t status; |
| 111 | |
| 112 | ASSERT(rx_buffer != NULL); |
| 113 | ASSERT(timeout != NULL); |
| 114 | regs = get_i2c_address(); |
| 115 | |
| 116 | /* Empty the FIFO */ |
| 117 | status = regs->ic_status; |
| 118 | while (status & IC_STATUS_RFNE) { |
Elyes HAOUAS | 3647920 | 2019-05-29 13:47:43 +0200 | [diff] [blame] | 119 | (void)regs->ic_data_cmd; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 120 | status = regs->ic_status; |
| 121 | } |
| 122 | |
| 123 | /* Fill the FIFO with read commands */ |
Elyes HAOUAS | f97c1c9 | 2019-12-03 18:22:06 +0100 | [diff] [blame] | 124 | fifo_bytes = MIN(length, 16); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 125 | bytes_transferred = 0; |
| 126 | while (length > 0) { |
| 127 | status = regs->ic_raw_intr_stat; |
| 128 | |
| 129 | /* Check for errors */ |
| 130 | if (status & (IC_INTR_RX_OVER | IC_INTR_RX_UNDER |
| 131 | | IC_INTR_TX_ABRT | IC_INTR_TX_OVER)) { |
| 132 | i2c_disable(regs); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 133 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 134 | printk(BIOS_ERR, |
| 135 | "0x%08x: ic_raw_intr_stat, I2C read error!\n", |
| 136 | status); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | /* Check for timeout */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 141 | if (stopwatch_expired(timeout)) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 142 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 143 | printk(BIOS_ERR, |
| 144 | "0x%08x: ic_raw_intr_stat, I2C read timeout!\n", |
| 145 | status); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 146 | return -1; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 147 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 148 | |
| 149 | /* Receive any available data */ |
| 150 | status = regs->ic_status; |
| 151 | if (status & IC_STATUS_RFNE) { |
| 152 | /* Save the next data byte, removed from the RX FIFO */ |
| 153 | *rx_buffer++ = (uint8_t)regs->ic_data_cmd; |
| 154 | bytes_transferred++; |
| 155 | } |
| 156 | |
| 157 | if ((status & IC_STATUS_TFNF) |
| 158 | || ((status & IC_STATUS_RFNE) && (fifo_bytes > 0))) { |
| 159 | /* End of the transaction? */ |
| 160 | cmd = IC_DATA_CMD_READ | restart; |
| 161 | if (length == 1) |
| 162 | cmd |= stop; |
| 163 | restart = 0; |
| 164 | |
| 165 | /* Place a read command into the TX FIFO */ |
| 166 | regs->ic_data_cmd = cmd; |
| 167 | if (fifo_bytes > 0) |
| 168 | fifo_bytes--; |
| 169 | length--; |
| 170 | } else |
| 171 | udelay(1); |
| 172 | } |
| 173 | return bytes_transferred; |
| 174 | } |
| 175 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 176 | int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segment, |
| 177 | int seg_count) |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 178 | { |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 179 | int bytes_transferred; |
| 180 | uint8_t chip; |
| 181 | uint32_t cmd; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 182 | int data_bytes; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 183 | int index; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 184 | int length; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 185 | I2C_REGS *regs; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 186 | uint32_t restart; |
| 187 | uint8_t *rx_buffer; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 188 | uint32_t status; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 189 | uint32_t stop; |
| 190 | struct stopwatch timeout; |
| 191 | int total_bytes; |
| 192 | uint8_t *tx_buffer; |
| 193 | int tx_bytes; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 194 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 195 | if (CONFIG(I2C_DEBUG)) { |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 196 | for (index = 0; index < seg_count;) { |
| 197 | if (index == 0) |
| 198 | printk(BIOS_ERR, "I2C Start\n"); |
| 199 | printk(BIOS_ERR, |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 200 | "I2C segment[%d]: %s 0x%02x %s %p, 0x%08x bytes\n", |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 201 | index, |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 202 | (segment[index].flags & I2C_M_RD) ? "Read from" : "Write to", |
| 203 | segment[index].slave, |
| 204 | (segment[index].flags & I2C_M_RD) ? "to " : "from", |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 205 | segment[index].buf, |
| 206 | segment[index].len); |
| 207 | printk(BIOS_ERR, "I2C %s\n", |
| 208 | (++index >= seg_count) ? "Stop" : "Restart"); |
| 209 | } |
| 210 | } |
| 211 | |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 212 | regs = get_i2c_address(); |
| 213 | |
| 214 | /* Disable the I2C controller to get access to the registers */ |
| 215 | i2c_disable(regs); |
| 216 | |
| 217 | /* Set the slave address */ |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 218 | ASSERT(seg_count > 0); |
| 219 | ASSERT(segment != NULL); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 220 | |
| 221 | /* Clear the start and stop detection */ |
| 222 | status = regs->ic_clr_start_det; |
| 223 | status = regs->ic_clr_stop_det; |
| 224 | |
| 225 | /* Set addressing mode to 7-bit and fast mode */ |
| 226 | cmd = regs->ic_con; |
| 227 | cmd &= ~(IC_CON_10B | IC_CON_SPEED); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 228 | cmd |= IC_CON_RESTART_EN | IC_CON_7B | IC_CON_SPEED_400_KHz |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 229 | | IC_CON_MASTER_MODE; |
| 230 | regs->ic_con = cmd; |
| 231 | |
| 232 | /* Set the target chip address */ |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 233 | chip = segment->slave; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 234 | regs->ic_tar = chip; |
| 235 | |
| 236 | /* Enable the I2C controller */ |
| 237 | regs->ic_enable = IC_ENABLE_CONTROLLER; |
| 238 | |
| 239 | /* Clear the interrupts */ |
| 240 | status = regs->ic_clr_rx_under; |
| 241 | status = regs->ic_clr_rx_over; |
| 242 | status = regs->ic_clr_tx_over; |
| 243 | status = regs->ic_clr_tx_abrt; |
| 244 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 245 | /* Start the timeout */ |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 246 | stopwatch_init_usecs_expire(&timeout, CONFIG_I2C_TRANSFER_TIMEOUT_US); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 247 | |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 248 | /* Process each of the segments */ |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 249 | total_bytes = 0; |
| 250 | tx_bytes = 0; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 251 | bytes_transferred = 0; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 252 | rx_buffer = NULL; |
| 253 | restart = 0; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 254 | index = 0; |
| 255 | while (index++ < seg_count) { |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 256 | length = segment->len; |
| 257 | total_bytes += length; |
| 258 | ASSERT(segment->buf != NULL); |
| 259 | ASSERT(length >= 1); |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 260 | ASSERT(segment->slave == chip); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 261 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 262 | /* Determine if this is the last segment of the transaction */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 263 | stop = (index == seg_count) ? IC_DATA_CMD_STOP : 0; |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 264 | |
| 265 | /* Fill the FIFO with the necessary command bytes */ |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 266 | if (segment->flags & I2C_M_RD) { |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 267 | /* Place read commands into the FIFO */ |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 268 | rx_buffer = segment->buf; |
| 269 | data_bytes = platform_i2c_read(restart, rx_buffer, |
| 270 | length, stop, &timeout); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 271 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 272 | /* Return any detected error */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 273 | if (data_bytes < 0) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 274 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 275 | printk(BIOS_ERR, |
| 276 | "I2C segment[%d] failed\n", |
| 277 | index); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 278 | return data_bytes; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 279 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 280 | bytes_transferred += data_bytes; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 281 | } else { |
| 282 | /* Write the data into the FIFO */ |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 283 | tx_buffer = segment->buf; |
| 284 | tx_bytes += length; |
| 285 | data_bytes = platform_i2c_write(restart, tx_buffer, |
| 286 | length, stop, rx_buffer, &timeout); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 287 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 288 | /* Return any detected error */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 289 | if (data_bytes < 0) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 290 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 291 | printk(BIOS_ERR, |
| 292 | "I2C segment[%d] failed\n", |
| 293 | index); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 294 | return data_bytes; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 295 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 296 | bytes_transferred += data_bytes; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 297 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 298 | segment++; |
| 299 | restart = IC_DATA_CMD_RESTART; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* Wait for the end of the transaction */ |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 303 | if (rx_buffer != NULL) |
| 304 | rx_buffer += bytes_transferred - tx_bytes; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 305 | do { |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 306 | /* Receive any available data */ |
| 307 | status = regs->ic_status; |
| 308 | if ((rx_buffer != NULL) && (status & IC_STATUS_RFNE)) { |
| 309 | *rx_buffer++ = (uint8_t)regs->ic_data_cmd; |
| 310 | bytes_transferred++; |
| 311 | } else { |
| 312 | status = regs->ic_raw_intr_stat; |
| 313 | if ((total_bytes == bytes_transferred) |
| 314 | && (status & IC_INTR_STOP_DET)) |
| 315 | break; |
| 316 | |
| 317 | /* Check for errors */ |
| 318 | if (status & (IC_INTR_RX_OVER | IC_INTR_RX_UNDER |
| 319 | | IC_INTR_TX_ABRT | IC_INTR_TX_OVER)) { |
| 320 | i2c_disable(regs); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 321 | if (CONFIG(I2C_DEBUG)) { |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 322 | printk(BIOS_ERR, |
| 323 | "0x%08x: ic_raw_intr_stat, I2C read error!\n", |
| 324 | status); |
| 325 | printk(BIOS_ERR, |
| 326 | "I2C segment[%d] failed\n", |
| 327 | seg_count - 1); |
| 328 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | /* Check for timeout */ |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 333 | if (stopwatch_expired(&timeout)) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 334 | if (CONFIG(I2C_DEBUG)) { |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 335 | printk(BIOS_ERR, |
| 336 | "0x%08x: ic_raw_intr_stat, I2C read timeout!\n", |
| 337 | status); |
| 338 | printk(BIOS_ERR, |
| 339 | "I2C segment[%d] failed\n", |
| 340 | seg_count - 1); |
| 341 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 342 | return -1; |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 343 | } |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 344 | |
| 345 | /* Delay for a while */ |
| 346 | udelay(1); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 347 | } |
Elyes HAOUAS | 4a83f1c | 2016-08-25 21:07:59 +0200 | [diff] [blame] | 348 | } while (1); |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 349 | i2c_disable(regs); |
| 350 | regs->ic_tar = 0; |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 351 | |
Lee Leahy | 16568c7 | 2017-01-03 16:00:15 -0800 | [diff] [blame] | 352 | /* Return the number of bytes transferred */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 353 | if (CONFIG(I2C_DEBUG)) |
Lee Leahy | 5764cbb | 2017-06-29 17:29:07 -0700 | [diff] [blame] | 354 | printk(BIOS_ERR, "0x%08x: bytes transferred\n", |
| 355 | bytes_transferred); |
Lee Leahy | a5258cb | 2016-05-30 14:06:25 -0700 | [diff] [blame] | 356 | return bytes_transferred; |
| 357 | } |