2 * (C) Copyright 2004 Tundra Semiconductor Corp.
5 * SPDX-License-Identifier: GPL-2.0+
7 * NOTE: This driver should be converted to driver model before June 2017.
8 * Please see doc/driver-model/i2c-howto.txt for instructions.
16 #if defined(CONFIG_CMD_I2C)
18 #define I2C_DELAY 100000
22 #define DPRINT(x) printf (x)
27 /* All functions assume that Tsi108 I2C block is the only master on the bus */
28 /* I2C read helper function */
30 void i2c_init(int speed, int slaveaddr)
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
39 static int i2c_read_byte (
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 */
47 u32 to_count = I2C_DELAY;
48 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
49 u32 chan_offset = TSI108_I2C_OFFSET;
51 DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
52 i2c_chan, chip_addr, byte_addr));
55 chan_offset = TSI108_I2C_SDRAM_OFFSET;
57 /* Check if I2C operation is in progress */
58 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
60 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
62 /* Set device address and operation (read = 0) */
63 temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
64 ((chip_addr >> 3) & 0x0F);
65 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
68 /* Issue the read command
69 * (at this moment all other parameters are 0
70 * (size = 1 byte, lane = 0)
73 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
76 /* Wait until operation completed */
78 /* Read I2C operation status */
79 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
81 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
83 (I2C_CNTRL2_I2C_CFGERR |
84 I2C_CNTRL2_I2C_TO_ERR))
86 op_status = TSI108_I2C_SUCCESS;
88 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
92 *buffer = (u8) (temp & 0xFF);
95 op_status = TSI108_I2C_IF_ERROR;
97 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
102 } while (to_count--);
104 op_status = TSI108_I2C_IF_BUSY;
106 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
109 DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
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.
118 * This hack is for enabling "i2c sdram" command on Tsi108 boards
119 * without changes to common code. Used for I2C reads only.
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
124 * buffer: Pointer to destination buffer for data to be read
125 * len: How many bytes to read
127 * Returns: 0 on success, not 0 on failure
130 int i2c_read (uchar chip_addr, uint byte_addr, int alen,
131 uchar * buffer, int len)
133 u32 op_status = TSI108_I2C_PARAM_ERR;
136 /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
137 if (0xD0 == (chip_addr & ~0x07)) {
141 /* Check for valid I2C address */
142 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
144 op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
146 if (TSI108_I2C_SUCCESS != op_status) {
147 DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
154 DPRINT (("I2C read() status: 0x%02x\n", op_status));
158 /* I2C write helper function */
160 static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */
161 uint byte_addr, /* Byte address within I2C device */
162 uchar * buffer /* pointer to data buffer */
166 u32 to_count = I2C_DELAY;
167 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
169 /* Check if I2C operation is in progress */
170 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
172 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
173 /* Place data into the I2C Tx Register */
174 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
175 I2C_TX_DATA) = (u32) * buffer;
177 /* Set device address and operation */
179 I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
180 ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
181 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
184 /* Issue the write command (at this moment all other parameters
185 * are 0 (size = 1 byte, lane = 0)
188 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
189 I2C_CNTRL2) = (I2C_CNTRL2_START);
191 op_status = TSI108_I2C_TIMEOUT_ERR;
193 /* Wait until operation completed */
195 /* Read I2C operation status */
196 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
198 if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
200 (I2C_CNTRL2_I2C_CFGERR |
201 I2C_CNTRL2_I2C_TO_ERR))) {
202 op_status = TSI108_I2C_SUCCESS;
204 /* report detected HW error */
205 op_status = TSI108_I2C_IF_ERROR;
207 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
213 } while (to_count--);
215 op_status = TSI108_I2C_IF_BUSY;
217 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
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
230 * buffer: Pointer to data to be written
231 * len: How many bytes to write
233 * Returns: 0 on success, not 0 on failure
236 int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
239 u32 op_status = TSI108_I2C_PARAM_ERR;
241 /* Check for valid I2C address */
242 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
245 i2c_write_byte (chip_addr, byte_addr++, buffer++);
247 if (TSI108_I2C_SUCCESS != op_status) {
248 DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
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.
264 int i2c_probe (uchar chip)
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.
273 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);