1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2004 Tundra Semiconductor Corp.
6 * NOTE: This driver should be converted to driver model before June 2017.
7 * Please see doc/driver-model/i2c-howto.txt for instructions.
15 #if defined(CONFIG_CMD_I2C)
17 #define I2C_DELAY 100000
21 #define DPRINT(x) printf (x)
26 /* All functions assume that Tsi108 I2C block is the only master on the bus */
27 /* I2C read helper function */
29 void i2c_init(int speed, int slaveaddr)
32 * The TSI108 has a fixed I2C clock rate and doesn't support slave
33 * operation. This function only exists as a stub to fit into the
38 static int i2c_read_byte (
39 uint i2c_chan, /* I2C channel number: 0 - main, 1 - SDC SPD */
40 uchar chip_addr,/* I2C device address on the bus */
41 uint byte_addr, /* Byte address within I2C device */
42 uchar * buffer /* pointer to data buffer */
46 u32 to_count = I2C_DELAY;
47 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
48 u32 chan_offset = TSI108_I2C_OFFSET;
50 DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
51 i2c_chan, chip_addr, byte_addr));
54 chan_offset = TSI108_I2C_SDRAM_OFFSET;
56 /* Check if I2C operation is in progress */
57 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
59 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
61 /* Set device address and operation (read = 0) */
62 temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
63 ((chip_addr >> 3) & 0x0F);
64 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
67 /* Issue the read command
68 * (at this moment all other parameters are 0
69 * (size = 1 byte, lane = 0)
72 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
75 /* Wait until operation completed */
77 /* Read I2C operation status */
78 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
80 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
82 (I2C_CNTRL2_I2C_CFGERR |
83 I2C_CNTRL2_I2C_TO_ERR))
85 op_status = TSI108_I2C_SUCCESS;
87 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
91 *buffer = (u8) (temp & 0xFF);
94 op_status = TSI108_I2C_IF_ERROR;
96 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
101 } while (to_count--);
103 op_status = TSI108_I2C_IF_BUSY;
105 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
108 DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
113 * I2C Read interface as defined in "include/i2c.h" :
114 * chip_addr: I2C chip address, range 0..127
115 * (to read from SPD channel EEPROM use (0xD0 ... 0xD7)
116 * NOTE: The bit 7 in the chip_addr serves as a channel select.
117 * This hack is for enabling "i2c sdram" command on Tsi108 boards
118 * without changes to common code. Used for I2C reads only.
119 * byte_addr: Memory or register address within the chip
120 * alen: Number of bytes to use for addr (typically 1, 2 for larger
121 * memories, 0 for register type devices with only one
123 * buffer: Pointer to destination buffer for data to be read
124 * len: How many bytes to read
126 * Returns: 0 on success, not 0 on failure
129 int i2c_read (uchar chip_addr, uint byte_addr, int alen,
130 uchar * buffer, int len)
132 u32 op_status = TSI108_I2C_PARAM_ERR;
135 /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
136 if (0xD0 == (chip_addr & ~0x07)) {
140 /* Check for valid I2C address */
141 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
143 op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
145 if (TSI108_I2C_SUCCESS != op_status) {
146 DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
153 DPRINT (("I2C read() status: 0x%02x\n", op_status));
157 /* I2C write helper function */
159 static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */
160 uint byte_addr, /* Byte address within I2C device */
161 uchar * buffer /* pointer to data buffer */
165 u32 to_count = I2C_DELAY;
166 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
168 /* Check if I2C operation is in progress */
169 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
171 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
172 /* Place data into the I2C Tx Register */
173 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
174 I2C_TX_DATA) = (u32) * buffer;
176 /* Set device address and operation */
178 I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
179 ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
180 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
183 /* Issue the write command (at this moment all other parameters
184 * are 0 (size = 1 byte, lane = 0)
187 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
188 I2C_CNTRL2) = (I2C_CNTRL2_START);
190 op_status = TSI108_I2C_TIMEOUT_ERR;
192 /* Wait until operation completed */
194 /* Read I2C operation status */
195 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
197 if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
199 (I2C_CNTRL2_I2C_CFGERR |
200 I2C_CNTRL2_I2C_TO_ERR))) {
201 op_status = TSI108_I2C_SUCCESS;
203 /* report detected HW error */
204 op_status = TSI108_I2C_IF_ERROR;
206 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
212 } while (to_count--);
214 op_status = TSI108_I2C_IF_BUSY;
216 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
223 * I2C Write interface as defined in "include/i2c.h" :
224 * chip_addr: I2C chip address, range 0..127
225 * byte_addr: Memory or register address within the chip
226 * alen: Number of bytes to use for addr (typically 1, 2 for larger
227 * memories, 0 for register type devices with only one
229 * buffer: Pointer to data to be written
230 * len: How many bytes to write
232 * Returns: 0 on success, not 0 on failure
235 int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
238 u32 op_status = TSI108_I2C_PARAM_ERR;
240 /* Check for valid I2C address */
241 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
244 i2c_write_byte (chip_addr, byte_addr++, buffer++);
246 if (TSI108_I2C_SUCCESS != op_status) {
247 DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
258 * I2C interface function as defined in "include/i2c.h".
259 * Probe the given I2C chip address by reading single byte from offset 0.
260 * Returns 0 if a chip responded, not 0 on failure.
263 int i2c_probe (uchar chip)
268 * Try to read the first location of the chip.
269 * The Tsi108 HW doesn't support sending just the chip address
270 * and checkong for an <ACK> back.
272 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);