blob: df8888550bb0e2010db25362ca9695b4ac7a293e [file] [log] [blame]
Stefan Roese79b2d0b2007-02-20 10:27:08 +01001/*
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +01002 * (C) Copyright 2007-2009
Stefan Roese79b2d0b2007-02-20 10:27:08 +01003 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
5 * based on work by Anne Sophie Harnois <anne-sophie.harnois@nextream.fr>
6 *
7 * (C) Copyright 2001
8 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese79b2d0b2007-02-20 10:27:08 +010011 */
wdenkc6097192002-11-03 00:24:07 +000012
13#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020014#include <asm/ppc4xx.h>
15#include <asm/ppc4xx-i2c.h>
wdenkc6097192002-11-03 00:24:07 +000016#include <i2c.h>
Peter Tyser61f2b382010-04-12 22:28:07 -050017#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000018
Wolfgang Denkd87080b2006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
20
Dirk Eibach880540d2013-04-25 02:40:01 +000021static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr)
wdenkc6097192002-11-03 00:24:07 +000022{
Dirk Eibach880540d2013-04-25 02:40:01 +000023 unsigned long base;
24
25#if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
26 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
27 defined(CONFIG_460EX) || defined(CONFIG_460GT)
28 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100);
29#elif defined(CONFIG_440) || defined(CONFIG_405EX)
30/* all remaining 440 variants */
31 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
32#else
33/* all 405 variants */
34 base = 0xEF600500 + (hwadapnr * 0x100);
35#endif
36 return (struct ppc4xx_i2c *)base;
37}
38
39static void _i2c_bus_reset(struct i2c_adapter *adap)
40{
41 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
Stefan Roese79b2d0b2007-02-20 10:27:08 +010042 int i;
43 u8 dc;
wdenkc6097192002-11-03 00:24:07 +000044
45 /* Reset status register */
46 /* write 1 in SCMP and IRQA to clear these fields */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010047 out_8(&i2c->sts, 0x0A);
wdenkc6097192002-11-03 00:24:07 +000048
49 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010050 out_8(&i2c->extsts, 0x8F);
wdenkc6097192002-11-03 00:24:07 +000051
Wolfgang Denk53677ef2008-05-20 16:00:29 +020052 /* Place chip in the reset state */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010053 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
wdenkc6097192002-11-03 00:24:07 +000054
Stefan Roese79b2d0b2007-02-20 10:27:08 +010055 /* Check if bus is free */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010056 dc = in_8(&i2c->directcntl);
Stefan Roese79b2d0b2007-02-20 10:27:08 +010057 if (!DIRCTNL_FREE(dc)){
58 /* Try to set bus free state */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010059 out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
Stefan Roese79b2d0b2007-02-20 10:27:08 +010060
61 /* Wait until we regain bus control */
62 for (i = 0; i < 100; ++i) {
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010063 dc = in_8(&i2c->directcntl);
Stefan Roese79b2d0b2007-02-20 10:27:08 +010064 if (DIRCTNL_FREE(dc))
65 break;
66
67 /* Toggle SCL line */
68 dc ^= IIC_DIRCNTL_SCC;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010069 out_8(&i2c->directcntl, dc);
Stefan Roese79b2d0b2007-02-20 10:27:08 +010070 udelay(10);
71 dc ^= IIC_DIRCNTL_SCC;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010072 out_8(&i2c->directcntl, dc);
wdenkc6097192002-11-03 00:24:07 +000073 }
74 }
Stefan Roese79b2d0b2007-02-20 10:27:08 +010075
76 /* Remove reset */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010077 out_8(&i2c->xtcntlss, 0);
wdenkc6097192002-11-03 00:24:07 +000078}
79
Dirk Eibach880540d2013-04-25 02:40:01 +000080static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
wdenkc6097192002-11-03 00:24:07 +000081{
Dirk Eibach880540d2013-04-25 02:40:01 +000082 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
wdenkc6097192002-11-03 00:24:07 +000083 int val, divisor;
84
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085#ifdef CONFIG_SYS_I2C_INIT_BOARD
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +010086 /*
87 * Call board specific i2c bus reset routine before accessing the
88 * environment, which might be in a chip on that bus. For details
89 * about this problem see doc/I2C_Edge_Conditions.
90 */
wdenk47cd00f2003-03-06 13:39:27 +000091 i2c_init_board();
92#endif
93
Dirk Eibach880540d2013-04-25 02:40:01 +000094 /* Handle possible failed I2C state */
95 /* FIXME: put this into i2c_init_board()? */
96 _i2c_bus_reset(adap);
wdenkc6097192002-11-03 00:24:07 +000097
Dirk Eibach880540d2013-04-25 02:40:01 +000098 /* clear lo master address */
99 out_8(&i2c->lmadr, 0);
Stefan Roese1a332da2010-03-29 15:30:46 +0200100
Dirk Eibach880540d2013-04-25 02:40:01 +0000101 /* clear hi master address */
102 out_8(&i2c->hmadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000103
Dirk Eibach880540d2013-04-25 02:40:01 +0000104 /* clear lo slave address */
105 out_8(&i2c->lsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000106
Dirk Eibach880540d2013-04-25 02:40:01 +0000107 /* clear hi slave address */
108 out_8(&i2c->hsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000109
Dirk Eibach880540d2013-04-25 02:40:01 +0000110 /* Clock divide Register */
111 /* set divisor according to freq_opb */
112 divisor = (get_OPB_freq() - 1) / 10000000;
113 if (divisor == 0)
114 divisor = 1;
115 out_8(&i2c->clkdiv, divisor);
wdenkc6097192002-11-03 00:24:07 +0000116
Dirk Eibach880540d2013-04-25 02:40:01 +0000117 /* no interrupts */
118 out_8(&i2c->intrmsk, 0);
wdenkc6097192002-11-03 00:24:07 +0000119
Dirk Eibach880540d2013-04-25 02:40:01 +0000120 /* clear transfer count */
121 out_8(&i2c->xfrcnt, 0);
wdenkc6097192002-11-03 00:24:07 +0000122
Dirk Eibach880540d2013-04-25 02:40:01 +0000123 /* clear extended control & stat */
124 /* write 1 in SRC SRS SWC SWS to clear these fields */
125 out_8(&i2c->xtcntlss, 0xF0);
wdenkc6097192002-11-03 00:24:07 +0000126
Dirk Eibach880540d2013-04-25 02:40:01 +0000127 /* Mode Control Register
128 Flush Slave/Master data buffer */
129 out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
wdenkc6097192002-11-03 00:24:07 +0000130
Dirk Eibach880540d2013-04-25 02:40:01 +0000131 val = in_8(&i2c->mdcntl);
wdenkc6097192002-11-03 00:24:07 +0000132
Dirk Eibach880540d2013-04-25 02:40:01 +0000133 /* Ignore General Call, slave transfers are ignored,
134 * disable interrupts, exit unknown bus state, enable hold
135 * SCL 100kHz normaly or FastMode for 400kHz and above
136 */
wdenkc6097192002-11-03 00:24:07 +0000137
Dirk Eibach880540d2013-04-25 02:40:01 +0000138 val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
139 if (speed >= 400000)
140 val |= IIC_MDCNTL_FSM;
141 out_8(&i2c->mdcntl, val);
wdenkc6097192002-11-03 00:24:07 +0000142
Dirk Eibach880540d2013-04-25 02:40:01 +0000143 /* clear control reg */
144 out_8(&i2c->cntl, 0x00);
wdenkc6097192002-11-03 00:24:07 +0000145}
146
147/*
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100148 * This code tries to use the features of the 405GP i2c
149 * controller. It will transfer up to 4 bytes in one pass
150 * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
151 * is possible to do out16(lhz) transfers.
152 *
153 * cmd_type is 0 for write 1 for read.
154 *
155 * addr_len can take any value from 0-255, it is only limited
156 * by the char, we could make it larger if needed. If it is
157 * 0 we skip the address write cycle.
158 *
159 * Typical case is a Write of an addr followd by a Read. The
160 * IBM FAQ does not cover this. On the last byte of the write
Dirk Eibach7e78f7a2014-10-29 15:56:43 +0100161 * we don't set the creg CHT bit but the RPST bit.
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100162 *
163 * It does not support address only transfers, there must be
164 * a data part. If you want to write the address yourself, put
165 * it in the data pointer.
166 *
167 * It does not support transfer to/from address 0.
168 *
169 * It does not check XFRCNT.
170 */
Dirk Eibach880540d2013-04-25 02:40:01 +0000171static int _i2c_transfer(struct i2c_adapter *adap,
172 unsigned char cmd_type,
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100173 unsigned char chip,
174 unsigned char addr[],
175 unsigned char addr_len,
176 unsigned char data[],
177 unsigned short data_len)
wdenkc6097192002-11-03 00:24:07 +0000178{
Dirk Eibach880540d2013-04-25 02:40:01 +0000179 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100180 u8 *ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000181 int reading;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100182 int tran, cnt;
wdenk8bde7f72003-06-27 21:31:46 +0000183 int result;
184 int status;
185 int i;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100186 u8 creg;
wdenkc6097192002-11-03 00:24:07 +0000187
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100188 if (data == 0 || data_len == 0) {
189 /* Don't support data transfer of no length or to address 0 */
wdenk8bde7f72003-06-27 21:31:46 +0000190 printf( "i2c_transfer: bad call\n" );
191 return IIC_NOK;
192 }
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100193 if (addr && addr_len) {
wdenk8bde7f72003-06-27 21:31:46 +0000194 ptr = addr;
195 cnt = addr_len;
196 reading = 0;
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100197 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000198 ptr = data;
199 cnt = data_len;
200 reading = cmd_type;
201 }
wdenkc6097192002-11-03 00:24:07 +0000202
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100203 /* Clear Stop Complete Bit */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100204 out_8(&i2c->sts, IIC_STS_SCMP);
205
wdenk8bde7f72003-06-27 21:31:46 +0000206 /* Check init */
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100207 i = 10;
wdenk8bde7f72003-06-27 21:31:46 +0000208 do {
209 /* Get status */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100210 status = in_8(&i2c->sts);
wdenk8bde7f72003-06-27 21:31:46 +0000211 i--;
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100212 } while ((status & IIC_STS_PT) && (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000213
wdenk8bde7f72003-06-27 21:31:46 +0000214 if (status & IIC_STS_PT) {
215 result = IIC_NOK_TOUT;
216 return(result);
217 }
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100218
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100219 /* flush the Master/Slave Databuffers */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100220 out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
221 IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
222
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100223 /* need to wait 4 OPB clocks? code below should take that long */
wdenkc6097192002-11-03 00:24:07 +0000224
wdenk8bde7f72003-06-27 21:31:46 +0000225 /* 7-bit adressing */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100226 out_8(&i2c->hmadr, 0);
227 out_8(&i2c->lmadr, chip);
wdenkc6097192002-11-03 00:24:07 +0000228
wdenk8bde7f72003-06-27 21:31:46 +0000229 tran = 0;
230 result = IIC_OK;
231 creg = 0;
wdenkc6097192002-11-03 00:24:07 +0000232
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100233 while (tran != cnt && (result == IIC_OK)) {
wdenk8bde7f72003-06-27 21:31:46 +0000234 int bc,j;
wdenkc6097192002-11-03 00:24:07 +0000235
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100236 /*
237 * Control register =
238 * Normal transfer, 7-bits adressing, Transfer up to
239 * bc bytes, Normal start, Transfer is a sequence of transfers
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100240 */
wdenk8bde7f72003-06-27 21:31:46 +0000241 creg |= IIC_CNTL_PT;
242
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100243 bc = (cnt - tran) > 4 ? 4 : cnt - tran;
244 creg |= (bc - 1) << 4;
245 /* if the real cmd type is write continue trans */
246 if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
wdenk8bde7f72003-06-27 21:31:46 +0000247 creg |= IIC_CNTL_CHT;
248
Dirk Eibach7e78f7a2014-10-29 15:56:43 +0100249 /* last part of address, prepare for repeated start on read */
250 if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
251 creg |= IIC_CNTL_RPST;
252
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100253 if (reading) {
wdenk8bde7f72003-06-27 21:31:46 +0000254 creg |= IIC_CNTL_READ;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100255 } else {
256 for(j = 0; j < bc; j++) {
wdenk8bde7f72003-06-27 21:31:46 +0000257 /* Set buffer */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100258 out_8(&i2c->mdbuf, ptr[tran + j]);
259 }
260 }
261 out_8(&i2c->cntl, creg);
wdenk8bde7f72003-06-27 21:31:46 +0000262
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100263 /*
264 * Transfer is in progress
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100265 * we have to wait for upto 5 bytes of data
266 * 1 byte chip address+r/w bit then bc bytes
267 * of data.
268 * udelay(10) is 1 bit time at 100khz
269 * Doubled for slop. 20 is too small.
270 */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100271 i = 2 * 5 * 8;
wdenk8bde7f72003-06-27 21:31:46 +0000272 do {
273 /* Get status */
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100274 status = in_8(&i2c->sts);
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100275 udelay(10);
wdenk8bde7f72003-06-27 21:31:46 +0000276 i--;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100277 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
278 (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000279
wdenk8bde7f72003-06-27 21:31:46 +0000280 if (status & IIC_STS_ERR) {
281 result = IIC_NOK;
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100282 status = in_8(&i2c->extsts);
wdenk8bde7f72003-06-27 21:31:46 +0000283 /* Lost arbitration? */
284 if (status & IIC_EXTSTS_LA)
285 result = IIC_NOK_LA;
286 /* Incomplete transfer? */
287 if (status & IIC_EXTSTS_ICT)
288 result = IIC_NOK_ICT;
289 /* Transfer aborted? */
290 if (status & IIC_EXTSTS_XFRA)
291 result = IIC_NOK_XFRA;
Dirk Eibachb97cd682014-10-29 15:56:44 +0100292 /* Is bus free?
293 * If error happened during combined xfer
294 * IIC interface is usually stuck in some strange
295 * state without a valid stop condition.
296 * Brute, but working: generate stop, then soft reset.
297 */
298 if ((status & IIC_EXTSTS_BCS_MASK)
299 != IIC_EXTSTS_BCS_FREE){
300 u8 mdcntl = in_8(&i2c->mdcntl);
301
302 /* Generate valid stop condition */
303 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
304 out_8(&i2c->directcntl, IIC_DIRCNTL_SCC);
305 udelay(10);
306 out_8(&i2c->directcntl,
307 IIC_DIRCNTL_SCC | IIC_DIRCNTL_SDAC);
308 out_8(&i2c->xtcntlss, 0);
309
310 ppc4xx_i2c_init(adap, (mdcntl & IIC_MDCNTL_FSM)
311 ? 400000 : 100000, 0);
312 }
wdenk8bde7f72003-06-27 21:31:46 +0000313 } else if ( status & IIC_STS_PT) {
314 result = IIC_NOK_TOUT;
315 }
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100316
wdenk8bde7f72003-06-27 21:31:46 +0000317 /* Command is reading => get buffer */
318 if ((reading) && (result == IIC_OK)) {
319 /* Are there data in buffer */
320 if (status & IIC_STS_MDBS) {
321 /*
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100322 * even if we have data we have to wait 4OPB
323 * clocks for it to hit the front of the FIFO,
324 * after that we can just read. We should check
325 * XFCNT here and if the FIFO is full there is
326 * no need to wait.
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100327 */
328 udelay(1);
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100329 for (j = 0; j < bc; j++)
330 ptr[tran + j] = in_8(&i2c->mdbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000331 } else
332 result = IIC_NOK_DATA;
333 }
334 creg = 0;
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100335 tran += bc;
336 if (ptr == addr && tran == cnt) {
wdenk8bde7f72003-06-27 21:31:46 +0000337 ptr = data;
338 cnt = data_len;
339 tran = 0;
340 reading = cmd_type;
wdenk8bde7f72003-06-27 21:31:46 +0000341 }
342 }
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100343 return result;
wdenkc6097192002-11-03 00:24:07 +0000344}
345
Dirk Eibach880540d2013-04-25 02:40:01 +0000346static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
wdenkc6097192002-11-03 00:24:07 +0000347{
348 uchar buf[1];
349
350 buf[0] = 0;
351
wdenk8bde7f72003-06-27 21:31:46 +0000352 /*
353 * What is needed is to send the chip address and verify that the
354 * address was <ACK>ed (i.e. there was a chip at that address which
355 * drove the data line low).
356 */
Dirk Eibach880540d2013-04-25 02:40:01 +0000357 return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
wdenkc6097192002-11-03 00:24:07 +0000358}
359
Dirk Eibach880540d2013-04-25 02:40:01 +0000360static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
361 int alen, uchar *buffer, int len, int read)
wdenkc6097192002-11-03 00:24:07 +0000362{
wdenk8bde7f72003-06-27 21:31:46 +0000363 uchar xaddr[4];
364 int ret;
wdenkc6097192002-11-03 00:24:07 +0000365
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100366 if (alen > 4) {
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100367 printf("I2C: addr len %d not supported\n", alen);
wdenkc6097192002-11-03 00:24:07 +0000368 return 1;
369 }
370
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100371 if (alen > 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000372 xaddr[0] = (addr >> 24) & 0xFF;
373 xaddr[1] = (addr >> 16) & 0xFF;
374 xaddr[2] = (addr >> 8) & 0xFF;
375 xaddr[3] = addr & 0xFF;
376 }
wdenkc6097192002-11-03 00:24:07 +0000377
378
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200379#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkc6097192002-11-03 00:24:07 +0000380 /*
wdenk8bde7f72003-06-27 21:31:46 +0000381 * EEPROM chips that implement "address overflow" are ones
382 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
383 * address and the extra bits end up in the "chip address"
384 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
385 * four 256 byte chips.
wdenkc6097192002-11-03 00:24:07 +0000386 *
wdenk8bde7f72003-06-27 21:31:46 +0000387 * Note that we consider the length of the address field to
388 * still be one byte because the extra address bits are
389 * hidden in the chip address.
wdenkc6097192002-11-03 00:24:07 +0000390 */
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100391 if (alen > 0)
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100392 chip |= ((addr >> (alen * 8)) &
393 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenkc6097192002-11-03 00:24:07 +0000394#endif
Dirk Eibach880540d2013-04-25 02:40:01 +0000395 ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
396 buffer, len);
397 if (ret) {
Graeme Russe3e454c2011-08-29 02:14:05 +0000398 printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
wdenk8bde7f72003-06-27 21:31:46 +0000399 return 1;
400 }
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100401
wdenk8bde7f72003-06-27 21:31:46 +0000402 return 0;
wdenkc6097192002-11-03 00:24:07 +0000403}
404
Dirk Eibach880540d2013-04-25 02:40:01 +0000405static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
406 int alen, uchar *buffer, int len)
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100407{
Dirk Eibach880540d2013-04-25 02:40:01 +0000408 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
Stefan Roeseeb5eb2b2009-11-19 14:03:17 +0100409}
410
Dirk Eibach880540d2013-04-25 02:40:01 +0000411static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
412 int alen, uchar *buffer, int len)
wdenkc6097192002-11-03 00:24:07 +0000413{
Dirk Eibach880540d2013-04-25 02:40:01 +0000414 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
wdenkc6097192002-11-03 00:24:07 +0000415}
416
Dirk Eibach880540d2013-04-25 02:40:01 +0000417static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
418 unsigned int speed)
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100419{
Dirk Eibach880540d2013-04-25 02:40:01 +0000420 if (speed != adap->speed)
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100421 return -1;
Dirk Eibach880540d2013-04-25 02:40:01 +0000422 return speed;
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100423}
Dirk Eibach880540d2013-04-25 02:40:01 +0000424
425/*
426 * Register ppc4xx i2c adapters
427 */
428#ifdef CONFIG_SYS_I2C_PPC4XX_CH0
429U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
430 ppc4xx_i2c_read, ppc4xx_i2c_write,
431 ppc4xx_i2c_set_bus_speed,
432 CONFIG_SYS_I2C_PPC4XX_SPEED_0,
433 CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
434#endif
435#ifdef CONFIG_SYS_I2C_PPC4XX_CH1
436U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
437 ppc4xx_i2c_read, ppc4xx_i2c_write,
438 ppc4xx_i2c_set_bus_speed,
439 CONFIG_SYS_I2C_PPC4XX_SPEED_1,
440 CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
441#endif