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