blob: 90292d287137ecddaf1d19a82c64b424f31e74c8 [file] [log] [blame]
roy zangb825f152006-11-02 19:12:31 +08001/*
2 * (C) Copyright 2004 Tundra Semiconductor Corp.
3 * Author: Alex Bounine
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Simon Glass28527092016-11-23 06:34:44 -07006 *
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 zangb825f152006-11-02 19:12:31 +08009 */
10
11#include <config.h>
roy zangee311212006-12-01 11:47:36 +080012#include <common.h>
roy zangb825f152006-11-02 19:12:31 +080013
roy zangb825f152006-11-02 19:12:31 +080014#include <tsi108.h>
15
Jon Loeligercb51c0b2007-07-09 17:39:42 -050016#if defined(CONFIG_CMD_I2C)
roy zangb825f152006-11-02 19:12:31 +080017
roy zangee311212006-12-01 11:47:36 +080018#define I2C_DELAY 100000
roy zangb825f152006-11-02 19:12:31 +080019#undef DEBUG_I2C
20
21#ifdef DEBUG_I2C
roy zangee311212006-12-01 11:47:36 +080022#define DPRINT(x) printf (x)
roy zangb825f152006-11-02 19:12:31 +080023#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 Tyserf0722ee2009-04-24 15:34:09 -050030void 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 zangee311212006-12-01 11:47:36 +080039static int i2c_read_byte (
roy zangb825f152006-11-02 19:12:31 +080040 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 zangee311212006-12-01 11:47:36 +080051 DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
roy zangb825f152006-11-02 19:12:31 +080052 i2c_chan, chip_addr, byte_addr));
53
roy zangee311212006-12-01 11:47:36 +080054 if (0 != i2c_chan)
roy zangb825f152006-11-02 19:12:31 +080055 chan_offset = TSI108_I2C_SDRAM_OFFSET;
roy zangb825f152006-11-02 19:12:31 +080056
57 /* Check if I2C operation is in progress */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
roy zangb825f152006-11-02 19:12:31 +080059
60 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
roy zangee311212006-12-01 11:47:36 +080061 I2C_CNTRL2_START))) {
roy zangb825f152006-11-02 19:12:31 +080062 /* Set device address and operation (read = 0) */
63 temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
64 ((chip_addr >> 3) & 0x0F);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
roy zangb825f152006-11-02 19:12:31 +080066 temp;
67
68 /* Issue the read command
roy zangee311212006-12-01 11:47:36 +080069 * (at this moment all other parameters are 0
roy zangb825f152006-11-02 19:12:31 +080070 * (size = 1 byte, lane = 0)
71 */
72
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
roy zangb825f152006-11-02 19:12:31 +080074 (I2C_CNTRL2_START);
75
76 /* Wait until operation completed */
77 do {
78 /* Read I2C operation status */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
roy zangb825f152006-11-02 19:12:31 +080080
Wolfgang Denk647d3c32007-03-04 01:36:05 +010081 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
82 if (0 == (temp &
roy zangb825f152006-11-02 19:12:31 +080083 (I2C_CNTRL2_I2C_CFGERR |
84 I2C_CNTRL2_I2C_TO_ERR))
85 ) {
86 op_status = TSI108_I2C_SUCCESS;
87
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
roy zangb825f152006-11-02 19:12:31 +080089 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 zangee311212006-12-01 11:47:36 +080097 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
roy zangb825f152006-11-02 19:12:31 +080098 }
99
100 break;
101 }
102 } while (to_count--);
103 } else {
104 op_status = TSI108_I2C_IF_BUSY;
105
roy zangee311212006-12-01 11:47:36 +0800106 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
roy zangb825f152006-11-02 19:12:31 +0800107 }
108
roy zangee311212006-12-01 11:47:36 +0800109 DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
roy zangb825f152006-11-02 19:12:31 +0800110 return op_status;
111}
112
roy zangee311212006-12-01 11:47:36 +0800113/*
roy zangb825f152006-11-02 19:12:31 +0800114 * 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 Tyser0f89c542009-04-18 22:34:03 -0500118 * This hack is for enabling "i2c sdram" command on Tsi108 boards
roy zangee311212006-12-01 11:47:36 +0800119 * without changes to common code. Used for I2C reads only.
roy zangb825f152006-11-02 19:12:31 +0800120 * 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 zangee311212006-12-01 11:47:36 +0800130int i2c_read (uchar chip_addr, uint byte_addr, int alen,
131 uchar * buffer, int len)
roy zangb825f152006-11-02 19:12:31 +0800132{
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 Denk647d3c32007-03-04 01:36:05 +0100144 op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
roy zangb825f152006-11-02 19:12:31 +0800145
146 if (TSI108_I2C_SUCCESS != op_status) {
roy zangee311212006-12-01 11:47:36 +0800147 DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
roy zangb825f152006-11-02 19:12:31 +0800148
149 break;
150 }
151 }
152 }
153
roy zangee311212006-12-01 11:47:36 +0800154 DPRINT (("I2C read() status: 0x%02x\n", op_status));
roy zangb825f152006-11-02 19:12:31 +0800155 return op_status;
156}
157
158/* I2C write helper function */
159
roy zangee311212006-12-01 11:47:36 +0800160static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */
roy zangb825f152006-11-02 19:12:31 +0800161 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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200170 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
roy zangb825f152006-11-02 19:12:31 +0800171
Wolfgang Denk647d3c32007-03-04 01:36:05 +0100172 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
roy zangb825f152006-11-02 19:12:31 +0800173 /* Place data into the I2C Tx Register */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200174 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
roy zangb825f152006-11-02 19:12:31 +0800175 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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200181 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
roy zangb825f152006-11-02 19:12:31 +0800182 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 Denk647d3c32007-03-04 01:36:05 +0100187
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200188 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
roy zangb825f152006-11-02 19:12:31 +0800189 I2C_CNTRL2) = (I2C_CNTRL2_START);
190
191 op_status = TSI108_I2C_TIMEOUT_ERR;
192
193 /* Wait until operation completed */
194 do {
roy zangee311212006-12-01 11:47:36 +0800195 /* Read I2C operation status */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200196 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
roy zangb825f152006-11-02 19:12:31 +0800197
Wolfgang Denk647d3c32007-03-04 01:36:05 +0100198 if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
199 if (0 == (temp &
roy zangb825f152006-11-02 19:12:31 +0800200 (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 zangee311212006-12-01 11:47:36 +0800207 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
roy zangb825f152006-11-02 19:12:31 +0800208 }
209
210 break;
211 }
212
213 } while (to_count--);
214 } else {
215 op_status = TSI108_I2C_IF_BUSY;
216
roy zangee311212006-12-01 11:47:36 +0800217 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
roy zangb825f152006-11-02 19:12:31 +0800218 }
219
220 return op_status;
221}
222
roy zangee311212006-12-01 11:47:36 +0800223/*
roy zangb825f152006-11-02 19:12:31 +0800224 * 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 zangee311212006-12-01 11:47:36 +0800236int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
roy zangb825f152006-11-02 19:12:31 +0800237 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 zangee311212006-12-01 11:47:36 +0800245 i2c_write_byte (chip_addr, byte_addr++, buffer++);
roy zangb825f152006-11-02 19:12:31 +0800246
247 if (TSI108_I2C_SUCCESS != op_status) {
roy zangee311212006-12-01 11:47:36 +0800248 DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
roy zangb825f152006-11-02 19:12:31 +0800249
250 break;
251 }
252 }
253 }
254
255 return op_status;
256}
257
roy zangee311212006-12-01 11:47:36 +0800258/*
roy zangb825f152006-11-02 19:12:31 +0800259 * 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 zangee311212006-12-01 11:47:36 +0800264int i2c_probe (uchar chip)
roy zangb825f152006-11-02 19:12:31 +0800265{
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 Denk409ecdc2007-11-18 16:36:27 +0100273 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
roy zangb825f152006-11-02 19:12:31 +0800274}
275
Jon Loeligercb51c0b2007-07-09 17:39:42 -0500276#endif