roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 Tundra Semiconductor Corp. |
| 3 | * Author: Alex Bounine |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2852709 | 2016-11-23 06:34:44 -0700 | [diff] [blame] | 6 | * |
| 7 | * NOTE: This driver should be converted to driver model before June 2017. |
| 8 | * Please see doc/driver-model/i2c-howto.txt for instructions. |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 12 | #include <common.h> |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 13 | |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 14 | #include <tsi108.h> |
| 15 | |
Jon Loeliger | cb51c0b | 2007-07-09 17:39:42 -0500 | [diff] [blame] | 16 | #if defined(CONFIG_CMD_I2C) |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 17 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 18 | #define I2C_DELAY 100000 |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 19 | #undef DEBUG_I2C |
| 20 | |
| 21 | #ifdef DEBUG_I2C |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 22 | #define DPRINT(x) printf (x) |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 23 | #else |
| 24 | #define DPRINT(x) |
| 25 | #endif |
| 26 | |
| 27 | /* All functions assume that Tsi108 I2C block is the only master on the bus */ |
| 28 | /* I2C read helper function */ |
| 29 | |
Peter Tyser | f0722ee | 2009-04-24 15:34:09 -0500 | [diff] [blame] | 30 | void i2c_init(int speed, int slaveaddr) |
| 31 | { |
| 32 | /* |
| 33 | * The TSI108 has a fixed I2C clock rate and doesn't support slave |
| 34 | * operation. This function only exists as a stub to fit into the |
| 35 | * U-Boot I2C API. |
| 36 | */ |
| 37 | } |
| 38 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 39 | static int i2c_read_byte ( |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 40 | uint i2c_chan, /* I2C channel number: 0 - main, 1 - SDC SPD */ |
| 41 | uchar chip_addr,/* I2C device address on the bus */ |
| 42 | uint byte_addr, /* Byte address within I2C device */ |
| 43 | uchar * buffer /* pointer to data buffer */ |
| 44 | ) |
| 45 | { |
| 46 | u32 temp; |
| 47 | u32 to_count = I2C_DELAY; |
| 48 | u32 op_status = TSI108_I2C_TIMEOUT_ERR; |
| 49 | u32 chan_offset = TSI108_I2C_OFFSET; |
| 50 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 51 | DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n", |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 52 | i2c_chan, chip_addr, byte_addr)); |
| 53 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 54 | if (0 != i2c_chan) |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 55 | chan_offset = TSI108_I2C_SDRAM_OFFSET; |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 56 | |
| 57 | /* Check if I2C operation is in progress */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 58 | temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 59 | |
| 60 | if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 61 | I2C_CNTRL2_START))) { |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 62 | /* Set device address and operation (read = 0) */ |
| 63 | temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) | |
| 64 | ((chip_addr >> 3) & 0x0F); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) = |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 66 | temp; |
| 67 | |
| 68 | /* Issue the read command |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 69 | * (at this moment all other parameters are 0 |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 70 | * (size = 1 byte, lane = 0) |
| 71 | */ |
| 72 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) = |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 74 | (I2C_CNTRL2_START); |
| 75 | |
| 76 | /* Wait until operation completed */ |
| 77 | do { |
| 78 | /* Read I2C operation status */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 79 | temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 80 | |
Wolfgang Denk | 647d3c3 | 2007-03-04 01:36:05 +0100 | [diff] [blame] | 81 | if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) { |
| 82 | if (0 == (temp & |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 83 | (I2C_CNTRL2_I2C_CFGERR | |
| 84 | I2C_CNTRL2_I2C_TO_ERR)) |
| 85 | ) { |
| 86 | op_status = TSI108_I2C_SUCCESS; |
| 87 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 89 | chan_offset + |
| 90 | I2C_RD_DATA); |
| 91 | |
| 92 | *buffer = (u8) (temp & 0xFF); |
| 93 | } else { |
| 94 | /* report HW error */ |
| 95 | op_status = TSI108_I2C_IF_ERROR; |
| 96 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 97 | DPRINT (("I2C HW error reported: 0x%02x\n", temp)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | break; |
| 101 | } |
| 102 | } while (to_count--); |
| 103 | } else { |
| 104 | op_status = TSI108_I2C_IF_BUSY; |
| 105 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 106 | DPRINT (("I2C Transaction start failed: 0x%02x\n", temp)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 107 | } |
| 108 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 109 | DPRINT (("I2C read_byte() status: 0x%02x\n", op_status)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 110 | return op_status; |
| 111 | } |
| 112 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 113 | /* |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 114 | * I2C Read interface as defined in "include/i2c.h" : |
| 115 | * chip_addr: I2C chip address, range 0..127 |
| 116 | * (to read from SPD channel EEPROM use (0xD0 ... 0xD7) |
| 117 | * NOTE: The bit 7 in the chip_addr serves as a channel select. |
Peter Tyser | 0f89c54 | 2009-04-18 22:34:03 -0500 | [diff] [blame] | 118 | * This hack is for enabling "i2c sdram" command on Tsi108 boards |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 119 | * without changes to common code. Used for I2C reads only. |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 120 | * byte_addr: Memory or register address within the chip |
| 121 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 122 | * memories, 0 for register type devices with only one |
| 123 | * register) |
| 124 | * buffer: Pointer to destination buffer for data to be read |
| 125 | * len: How many bytes to read |
| 126 | * |
| 127 | * Returns: 0 on success, not 0 on failure |
| 128 | */ |
| 129 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 130 | int i2c_read (uchar chip_addr, uint byte_addr, int alen, |
| 131 | uchar * buffer, int len) |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 132 | { |
| 133 | u32 op_status = TSI108_I2C_PARAM_ERR; |
| 134 | u32 i2c_if = 0; |
| 135 | |
| 136 | /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/ |
| 137 | if (0xD0 == (chip_addr & ~0x07)) { |
| 138 | i2c_if = 1; |
| 139 | chip_addr &= 0x7F; |
| 140 | } |
| 141 | /* Check for valid I2C address */ |
| 142 | if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) { |
| 143 | while (len--) { |
Wolfgang Denk | 647d3c3 | 2007-03-04 01:36:05 +0100 | [diff] [blame] | 144 | op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 145 | |
| 146 | if (TSI108_I2C_SUCCESS != op_status) { |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 147 | DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 148 | |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 154 | DPRINT (("I2C read() status: 0x%02x\n", op_status)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 155 | return op_status; |
| 156 | } |
| 157 | |
| 158 | /* I2C write helper function */ |
| 159 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 160 | static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */ |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 161 | uint byte_addr, /* Byte address within I2C device */ |
| 162 | uchar * buffer /* pointer to data buffer */ |
| 163 | ) |
| 164 | { |
| 165 | u32 temp; |
| 166 | u32 to_count = I2C_DELAY; |
| 167 | u32 op_status = TSI108_I2C_TIMEOUT_ERR; |
| 168 | |
| 169 | /* Check if I2C operation is in progress */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 170 | temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 171 | |
Wolfgang Denk | 647d3c3 | 2007-03-04 01:36:05 +0100 | [diff] [blame] | 172 | if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) { |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 173 | /* Place data into the I2C Tx Register */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 174 | *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 175 | I2C_TX_DATA) = (u32) * buffer; |
| 176 | |
| 177 | /* Set device address and operation */ |
| 178 | temp = |
| 179 | I2C_CNTRL1_I2CWRITE | (byte_addr << 16) | |
| 180 | ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 181 | *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 182 | I2C_CNTRL1) = temp; |
| 183 | |
| 184 | /* Issue the write command (at this moment all other parameters |
| 185 | * are 0 (size = 1 byte, lane = 0) |
| 186 | */ |
Wolfgang Denk | 647d3c3 | 2007-03-04 01:36:05 +0100 | [diff] [blame] | 187 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 188 | *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 189 | I2C_CNTRL2) = (I2C_CNTRL2_START); |
| 190 | |
| 191 | op_status = TSI108_I2C_TIMEOUT_ERR; |
| 192 | |
| 193 | /* Wait until operation completed */ |
| 194 | do { |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 195 | /* Read I2C operation status */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 196 | temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 197 | |
Wolfgang Denk | 647d3c3 | 2007-03-04 01:36:05 +0100 | [diff] [blame] | 198 | if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) { |
| 199 | if (0 == (temp & |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 200 | (I2C_CNTRL2_I2C_CFGERR | |
| 201 | I2C_CNTRL2_I2C_TO_ERR))) { |
| 202 | op_status = TSI108_I2C_SUCCESS; |
| 203 | } else { |
| 204 | /* report detected HW error */ |
| 205 | op_status = TSI108_I2C_IF_ERROR; |
| 206 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 207 | DPRINT (("I2C HW error reported: 0x%02x\n", temp)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | } while (to_count--); |
| 214 | } else { |
| 215 | op_status = TSI108_I2C_IF_BUSY; |
| 216 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 217 | DPRINT (("I2C Transaction start failed: 0x%02x\n", temp)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return op_status; |
| 221 | } |
| 222 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 223 | /* |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 224 | * I2C Write interface as defined in "include/i2c.h" : |
| 225 | * chip_addr: I2C chip address, range 0..127 |
| 226 | * byte_addr: Memory or register address within the chip |
| 227 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 228 | * memories, 0 for register type devices with only one |
| 229 | * register) |
| 230 | * buffer: Pointer to data to be written |
| 231 | * len: How many bytes to write |
| 232 | * |
| 233 | * Returns: 0 on success, not 0 on failure |
| 234 | */ |
| 235 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 236 | int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer, |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 237 | int len) |
| 238 | { |
| 239 | u32 op_status = TSI108_I2C_PARAM_ERR; |
| 240 | |
| 241 | /* Check for valid I2C address */ |
| 242 | if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) { |
| 243 | while (len--) { |
| 244 | op_status = |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 245 | i2c_write_byte (chip_addr, byte_addr++, buffer++); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 246 | |
| 247 | if (TSI108_I2C_SUCCESS != op_status) { |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 248 | DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len)); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 249 | |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return op_status; |
| 256 | } |
| 257 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 258 | /* |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 259 | * I2C interface function as defined in "include/i2c.h". |
| 260 | * Probe the given I2C chip address by reading single byte from offset 0. |
| 261 | * Returns 0 if a chip responded, not 0 on failure. |
| 262 | */ |
| 263 | |
roy zang | ee31121 | 2006-12-01 11:47:36 +0800 | [diff] [blame] | 264 | int i2c_probe (uchar chip) |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 265 | { |
| 266 | u32 tmp; |
| 267 | |
| 268 | /* |
| 269 | * Try to read the first location of the chip. |
| 270 | * The Tsi108 HW doesn't support sending just the chip address |
| 271 | * and checkong for an <ACK> back. |
| 272 | */ |
Wolfgang Denk | 409ecdc | 2007-11-18 16:36:27 +0100 | [diff] [blame] | 273 | return i2c_read (chip, 0, 1, (uchar *)&tmp, 1); |
roy zang | b825f15 | 2006-11-02 19:12:31 +0800 | [diff] [blame] | 274 | } |
| 275 | |
Jon Loeliger | cb51c0b | 2007-07-09 17:39:42 -0500 | [diff] [blame] | 276 | #endif |