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