blob: 8a38d11fb9f42489e4d5566c271adf1a8f06795b [file] [log] [blame]
Stefan Roesed07117e2007-02-20 10:27:08 +01001/*
Stefan Roese77c1f1d2009-11-19 14:03:17 +01002 * (C) Copyright 2007-2009
Stefan Roesed07117e2007-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 Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Simon Glasscb052ff2016-11-23 06:34:44 -070011 *
12 * NOTE: This driver should be converted to driver model before June 2017.
13 * Please see doc/driver-model/i2c-howto.txt for instructions.
Stefan Roesed07117e2007-02-20 10:27:08 +010014 */
wdenkc6097192002-11-03 00:24:07 +000015
16#include <common.h>
Stefan Roese247e9d72010-09-09 19:18:00 +020017#include <asm/ppc4xx.h>
18#include <asm/ppc4xx-i2c.h>
wdenkc6097192002-11-03 00:24:07 +000019#include <i2c.h>
Peter Tyser133c0fe2010-04-12 22:28:07 -050020#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000021
Wolfgang Denk6405a152006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Dirk Eibach42b204f2013-04-25 02:40:01 +000024static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr)
25{
26 unsigned long base;
27
28#if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
29 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
30 defined(CONFIG_460EX) || defined(CONFIG_460GT)
31 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100);
32#elif defined(CONFIG_440) || defined(CONFIG_405EX)
33/* all remaining 440 variants */
34 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
35#else
36/* all 405 variants */
37 base = 0xEF600500 + (hwadapnr * 0x100);
Stefan Roesed07117e2007-02-20 10:27:08 +010038#endif
Dirk Eibach42b204f2013-04-25 02:40:01 +000039 return (struct ppc4xx_i2c *)base;
40}
wdenkc6097192002-11-03 00:24:07 +000041
Dirk Eibach42b204f2013-04-25 02:40:01 +000042static void _i2c_bus_reset(struct i2c_adapter *adap)
wdenkc6097192002-11-03 00:24:07 +000043{
Dirk Eibach42b204f2013-04-25 02:40:01 +000044 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
Stefan Roesed07117e2007-02-20 10:27:08 +010045 int i;
46 u8 dc;
wdenkc6097192002-11-03 00:24:07 +000047
48 /* Reset status register */
49 /* write 1 in SCMP and IRQA to clear these fields */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010050 out_8(&i2c->sts, 0x0A);
wdenkc6097192002-11-03 00:24:07 +000051
52 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010053 out_8(&i2c->extsts, 0x8F);
wdenkc6097192002-11-03 00:24:07 +000054
Wolfgang Denka1be4762008-05-20 16:00:29 +020055 /* Place chip in the reset state */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010056 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
wdenkc6097192002-11-03 00:24:07 +000057
Stefan Roesed07117e2007-02-20 10:27:08 +010058 /* Check if bus is free */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010059 dc = in_8(&i2c->directcntl);
Stefan Roesed07117e2007-02-20 10:27:08 +010060 if (!DIRCTNL_FREE(dc)){
61 /* Try to set bus free state */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010062 out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
Stefan Roesed07117e2007-02-20 10:27:08 +010063
64 /* Wait until we regain bus control */
65 for (i = 0; i < 100; ++i) {
Stefan Roese77c1f1d2009-11-19 14:03:17 +010066 dc = in_8(&i2c->directcntl);
Stefan Roesed07117e2007-02-20 10:27:08 +010067 if (DIRCTNL_FREE(dc))
68 break;
69
70 /* Toggle SCL line */
71 dc ^= IIC_DIRCNTL_SCC;
Stefan Roese77c1f1d2009-11-19 14:03:17 +010072 out_8(&i2c->directcntl, dc);
Stefan Roesed07117e2007-02-20 10:27:08 +010073 udelay(10);
74 dc ^= IIC_DIRCNTL_SCC;
Stefan Roese77c1f1d2009-11-19 14:03:17 +010075 out_8(&i2c->directcntl, dc);
wdenkc6097192002-11-03 00:24:07 +000076 }
77 }
Stefan Roesed07117e2007-02-20 10:27:08 +010078
79 /* Remove reset */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010080 out_8(&i2c->xtcntlss, 0);
wdenkc6097192002-11-03 00:24:07 +000081}
82
Dirk Eibach42b204f2013-04-25 02:40:01 +000083static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
wdenkc6097192002-11-03 00:24:07 +000084{
Dirk Eibach42b204f2013-04-25 02:40:01 +000085 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
wdenkc6097192002-11-03 00:24:07 +000086 int val, divisor;
87
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020088#ifdef CONFIG_SYS_I2C_INIT_BOARD
Stefan Roese77c1f1d2009-11-19 14:03:17 +010089 /*
90 * Call board specific i2c bus reset routine before accessing the
91 * environment, which might be in a chip on that bus. For details
92 * about this problem see doc/I2C_Edge_Conditions.
93 */
wdenkcc1e2562003-03-06 13:39:27 +000094 i2c_init_board();
95#endif
96
Dirk Eibach42b204f2013-04-25 02:40:01 +000097 /* Handle possible failed I2C state */
98 /* FIXME: put this into i2c_init_board()? */
99 _i2c_bus_reset(adap);
Stefan Roeseb1bcb0e2010-03-29 15:30:46 +0200100
Dirk Eibach42b204f2013-04-25 02:40:01 +0000101 /* clear lo master address */
102 out_8(&i2c->lmadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000103
Dirk Eibach42b204f2013-04-25 02:40:01 +0000104 /* clear hi master address */
105 out_8(&i2c->hmadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000106
Dirk Eibach42b204f2013-04-25 02:40:01 +0000107 /* clear lo slave address */
108 out_8(&i2c->lsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000109
Dirk Eibach42b204f2013-04-25 02:40:01 +0000110 /* clear hi slave address */
111 out_8(&i2c->hsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000112
Dirk Eibach42b204f2013-04-25 02:40:01 +0000113 /* Clock divide Register */
114 /* set divisor according to freq_opb */
115 divisor = (get_OPB_freq() - 1) / 10000000;
116 if (divisor == 0)
117 divisor = 1;
118 out_8(&i2c->clkdiv, divisor);
wdenkc6097192002-11-03 00:24:07 +0000119
Dirk Eibach42b204f2013-04-25 02:40:01 +0000120 /* no interrupts */
121 out_8(&i2c->intrmsk, 0);
wdenkc6097192002-11-03 00:24:07 +0000122
Dirk Eibach42b204f2013-04-25 02:40:01 +0000123 /* clear transfer count */
124 out_8(&i2c->xfrcnt, 0);
wdenkc6097192002-11-03 00:24:07 +0000125
Dirk Eibach42b204f2013-04-25 02:40:01 +0000126 /* clear extended control & stat */
127 /* write 1 in SRC SRS SWC SWS to clear these fields */
128 out_8(&i2c->xtcntlss, 0xF0);
wdenkc6097192002-11-03 00:24:07 +0000129
Dirk Eibach42b204f2013-04-25 02:40:01 +0000130 /* Mode Control Register
131 Flush Slave/Master data buffer */
132 out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
wdenkc6097192002-11-03 00:24:07 +0000133
Dirk Eibach42b204f2013-04-25 02:40:01 +0000134 val = in_8(&i2c->mdcntl);
wdenkc6097192002-11-03 00:24:07 +0000135
Dirk Eibach42b204f2013-04-25 02:40:01 +0000136 /* Ignore General Call, slave transfers are ignored,
137 * disable interrupts, exit unknown bus state, enable hold
138 * SCL 100kHz normaly or FastMode for 400kHz and above
139 */
wdenkc6097192002-11-03 00:24:07 +0000140
Dirk Eibach42b204f2013-04-25 02:40:01 +0000141 val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
142 if (speed >= 400000)
143 val |= IIC_MDCNTL_FSM;
144 out_8(&i2c->mdcntl, val);
wdenkc6097192002-11-03 00:24:07 +0000145
Dirk Eibach42b204f2013-04-25 02:40:01 +0000146 /* clear control reg */
147 out_8(&i2c->cntl, 0x00);
wdenkc6097192002-11-03 00:24:07 +0000148}
149
150/*
Stefan Roesed07117e2007-02-20 10:27:08 +0100151 * This code tries to use the features of the 405GP i2c
152 * controller. It will transfer up to 4 bytes in one pass
153 * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
154 * is possible to do out16(lhz) transfers.
155 *
156 * cmd_type is 0 for write 1 for read.
157 *
158 * addr_len can take any value from 0-255, it is only limited
159 * by the char, we could make it larger if needed. If it is
160 * 0 we skip the address write cycle.
161 *
162 * Typical case is a Write of an addr followd by a Read. The
163 * IBM FAQ does not cover this. On the last byte of the write
Dirk Eibach63011732014-10-29 15:56:43 +0100164 * we don't set the creg CHT bit but the RPST bit.
Stefan Roesed07117e2007-02-20 10:27:08 +0100165 *
166 * It does not support address only transfers, there must be
167 * a data part. If you want to write the address yourself, put
168 * it in the data pointer.
169 *
170 * It does not support transfer to/from address 0.
171 *
172 * It does not check XFRCNT.
173 */
Dirk Eibach42b204f2013-04-25 02:40:01 +0000174static int _i2c_transfer(struct i2c_adapter *adap,
175 unsigned char cmd_type,
Stefan Roesed07117e2007-02-20 10:27:08 +0100176 unsigned char chip,
177 unsigned char addr[],
178 unsigned char addr_len,
179 unsigned char data[],
180 unsigned short data_len)
wdenkc6097192002-11-03 00:24:07 +0000181{
Dirk Eibach42b204f2013-04-25 02:40:01 +0000182 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100183 u8 *ptr;
wdenk57b2d802003-06-27 21:31:46 +0000184 int reading;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100185 int tran, cnt;
wdenk57b2d802003-06-27 21:31:46 +0000186 int result;
187 int status;
188 int i;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100189 u8 creg;
wdenkc6097192002-11-03 00:24:07 +0000190
Stefan Roesed07117e2007-02-20 10:27:08 +0100191 if (data == 0 || data_len == 0) {
192 /* Don't support data transfer of no length or to address 0 */
wdenk57b2d802003-06-27 21:31:46 +0000193 printf( "i2c_transfer: bad call\n" );
194 return IIC_NOK;
195 }
Stefan Roesed07117e2007-02-20 10:27:08 +0100196 if (addr && addr_len) {
wdenk57b2d802003-06-27 21:31:46 +0000197 ptr = addr;
198 cnt = addr_len;
199 reading = 0;
Stefan Roesed07117e2007-02-20 10:27:08 +0100200 } else {
wdenk57b2d802003-06-27 21:31:46 +0000201 ptr = data;
202 cnt = data_len;
203 reading = cmd_type;
204 }
wdenkc6097192002-11-03 00:24:07 +0000205
Stefan Roesed07117e2007-02-20 10:27:08 +0100206 /* Clear Stop Complete Bit */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100207 out_8(&i2c->sts, IIC_STS_SCMP);
208
wdenk57b2d802003-06-27 21:31:46 +0000209 /* Check init */
Stefan Roesed07117e2007-02-20 10:27:08 +0100210 i = 10;
wdenk57b2d802003-06-27 21:31:46 +0000211 do {
212 /* Get status */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100213 status = in_8(&i2c->sts);
wdenk57b2d802003-06-27 21:31:46 +0000214 i--;
Stefan Roesed07117e2007-02-20 10:27:08 +0100215 } while ((status & IIC_STS_PT) && (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000216
wdenk57b2d802003-06-27 21:31:46 +0000217 if (status & IIC_STS_PT) {
218 result = IIC_NOK_TOUT;
219 return(result);
220 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100221
Stefan Roesed07117e2007-02-20 10:27:08 +0100222 /* flush the Master/Slave Databuffers */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100223 out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
224 IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
225
Stefan Roesed07117e2007-02-20 10:27:08 +0100226 /* need to wait 4 OPB clocks? code below should take that long */
wdenkc6097192002-11-03 00:24:07 +0000227
wdenk57b2d802003-06-27 21:31:46 +0000228 /* 7-bit adressing */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100229 out_8(&i2c->hmadr, 0);
230 out_8(&i2c->lmadr, chip);
wdenkc6097192002-11-03 00:24:07 +0000231
wdenk57b2d802003-06-27 21:31:46 +0000232 tran = 0;
233 result = IIC_OK;
234 creg = 0;
wdenkc6097192002-11-03 00:24:07 +0000235
Stefan Roesed07117e2007-02-20 10:27:08 +0100236 while (tran != cnt && (result == IIC_OK)) {
wdenk57b2d802003-06-27 21:31:46 +0000237 int bc,j;
wdenkc6097192002-11-03 00:24:07 +0000238
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100239 /*
240 * Control register =
241 * Normal transfer, 7-bits adressing, Transfer up to
242 * bc bytes, Normal start, Transfer is a sequence of transfers
Stefan Roesed07117e2007-02-20 10:27:08 +0100243 */
wdenk57b2d802003-06-27 21:31:46 +0000244 creg |= IIC_CNTL_PT;
wdenkc6097192002-11-03 00:24:07 +0000245
Stefan Roesed07117e2007-02-20 10:27:08 +0100246 bc = (cnt - tran) > 4 ? 4 : cnt - tran;
247 creg |= (bc - 1) << 4;
248 /* if the real cmd type is write continue trans */
249 if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
wdenk57b2d802003-06-27 21:31:46 +0000250 creg |= IIC_CNTL_CHT;
wdenkc6097192002-11-03 00:24:07 +0000251
Dirk Eibach63011732014-10-29 15:56:43 +0100252 /* last part of address, prepare for repeated start on read */
253 if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
254 creg |= IIC_CNTL_RPST;
255
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100256 if (reading) {
wdenk57b2d802003-06-27 21:31:46 +0000257 creg |= IIC_CNTL_READ;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100258 } else {
259 for(j = 0; j < bc; j++) {
wdenk57b2d802003-06-27 21:31:46 +0000260 /* Set buffer */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100261 out_8(&i2c->mdbuf, ptr[tran + j]);
262 }
263 }
264 out_8(&i2c->cntl, creg);
wdenkc6097192002-11-03 00:24:07 +0000265
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100266 /*
267 * Transfer is in progress
Stefan Roesed07117e2007-02-20 10:27:08 +0100268 * we have to wait for upto 5 bytes of data
269 * 1 byte chip address+r/w bit then bc bytes
270 * of data.
271 * udelay(10) is 1 bit time at 100khz
272 * Doubled for slop. 20 is too small.
273 */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100274 i = 2 * 5 * 8;
wdenk57b2d802003-06-27 21:31:46 +0000275 do {
276 /* Get status */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100277 status = in_8(&i2c->sts);
Stefan Roesed07117e2007-02-20 10:27:08 +0100278 udelay(10);
wdenk57b2d802003-06-27 21:31:46 +0000279 i--;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100280 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
281 (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000282
wdenk57b2d802003-06-27 21:31:46 +0000283 if (status & IIC_STS_ERR) {
284 result = IIC_NOK;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100285 status = in_8(&i2c->extsts);
wdenk57b2d802003-06-27 21:31:46 +0000286 /* Lost arbitration? */
287 if (status & IIC_EXTSTS_LA)
288 result = IIC_NOK_LA;
289 /* Incomplete transfer? */
290 if (status & IIC_EXTSTS_ICT)
291 result = IIC_NOK_ICT;
292 /* Transfer aborted? */
293 if (status & IIC_EXTSTS_XFRA)
294 result = IIC_NOK_XFRA;
Dirk Eibach455b7462014-10-29 15:56:44 +0100295 /* Is bus free?
296 * If error happened during combined xfer
297 * IIC interface is usually stuck in some strange
298 * state without a valid stop condition.
299 * Brute, but working: generate stop, then soft reset.
300 */
301 if ((status & IIC_EXTSTS_BCS_MASK)
302 != IIC_EXTSTS_BCS_FREE){
303 u8 mdcntl = in_8(&i2c->mdcntl);
304
305 /* Generate valid stop condition */
306 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
307 out_8(&i2c->directcntl, IIC_DIRCNTL_SCC);
308 udelay(10);
309 out_8(&i2c->directcntl,
310 IIC_DIRCNTL_SCC | IIC_DIRCNTL_SDAC);
311 out_8(&i2c->xtcntlss, 0);
312
313 ppc4xx_i2c_init(adap, (mdcntl & IIC_MDCNTL_FSM)
314 ? 400000 : 100000, 0);
315 }
wdenk57b2d802003-06-27 21:31:46 +0000316 } else if ( status & IIC_STS_PT) {
317 result = IIC_NOK_TOUT;
318 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100319
wdenk57b2d802003-06-27 21:31:46 +0000320 /* Command is reading => get buffer */
321 if ((reading) && (result == IIC_OK)) {
322 /* Are there data in buffer */
323 if (status & IIC_STS_MDBS) {
324 /*
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100325 * even if we have data we have to wait 4OPB
326 * clocks for it to hit the front of the FIFO,
327 * after that we can just read. We should check
328 * XFCNT here and if the FIFO is full there is
329 * no need to wait.
Stefan Roesed07117e2007-02-20 10:27:08 +0100330 */
331 udelay(1);
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100332 for (j = 0; j < bc; j++)
333 ptr[tran + j] = in_8(&i2c->mdbuf);
wdenk57b2d802003-06-27 21:31:46 +0000334 } else
335 result = IIC_NOK_DATA;
336 }
337 creg = 0;
Stefan Roesed07117e2007-02-20 10:27:08 +0100338 tran += bc;
339 if (ptr == addr && tran == cnt) {
wdenk57b2d802003-06-27 21:31:46 +0000340 ptr = data;
341 cnt = data_len;
342 tran = 0;
343 reading = cmd_type;
wdenk57b2d802003-06-27 21:31:46 +0000344 }
345 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100346 return result;
wdenkc6097192002-11-03 00:24:07 +0000347}
348
Dirk Eibach42b204f2013-04-25 02:40:01 +0000349static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
wdenkc6097192002-11-03 00:24:07 +0000350{
351 uchar buf[1];
352
353 buf[0] = 0;
354
wdenk57b2d802003-06-27 21:31:46 +0000355 /*
356 * What is needed is to send the chip address and verify that the
357 * address was <ACK>ed (i.e. there was a chip at that address which
358 * drove the data line low).
359 */
Dirk Eibach42b204f2013-04-25 02:40:01 +0000360 return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
wdenkc6097192002-11-03 00:24:07 +0000361}
362
Dirk Eibach42b204f2013-04-25 02:40:01 +0000363static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
364 int alen, uchar *buffer, int len, int read)
wdenkc6097192002-11-03 00:24:07 +0000365{
wdenk57b2d802003-06-27 21:31:46 +0000366 uchar xaddr[4];
367 int ret;
wdenkc6097192002-11-03 00:24:07 +0000368
Stefan Roesed07117e2007-02-20 10:27:08 +0100369 if (alen > 4) {
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100370 printf("I2C: addr len %d not supported\n", alen);
wdenkc6097192002-11-03 00:24:07 +0000371 return 1;
372 }
373
Stefan Roesed07117e2007-02-20 10:27:08 +0100374 if (alen > 0) {
wdenk57b2d802003-06-27 21:31:46 +0000375 xaddr[0] = (addr >> 24) & 0xFF;
376 xaddr[1] = (addr >> 16) & 0xFF;
377 xaddr[2] = (addr >> 8) & 0xFF;
378 xaddr[3] = addr & 0xFF;
379 }
wdenkc6097192002-11-03 00:24:07 +0000380
381
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200382#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkc6097192002-11-03 00:24:07 +0000383 /*
wdenk57b2d802003-06-27 21:31:46 +0000384 * EEPROM chips that implement "address overflow" are ones
385 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
386 * address and the extra bits end up in the "chip address"
387 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
388 * four 256 byte chips.
wdenkc6097192002-11-03 00:24:07 +0000389 *
wdenk57b2d802003-06-27 21:31:46 +0000390 * Note that we consider the length of the address field to
391 * still be one byte because the extra address bits are
392 * hidden in the chip address.
wdenkc6097192002-11-03 00:24:07 +0000393 */
Stefan Roesed07117e2007-02-20 10:27:08 +0100394 if (alen > 0)
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100395 chip |= ((addr >> (alen * 8)) &
396 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenkc6097192002-11-03 00:24:07 +0000397#endif
Dirk Eibach42b204f2013-04-25 02:40:01 +0000398 ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
399 buffer, len);
400 if (ret) {
Graeme Russ70600b02011-08-29 02:14:05 +0000401 printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
wdenk57b2d802003-06-27 21:31:46 +0000402 return 1;
403 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100404
wdenk57b2d802003-06-27 21:31:46 +0000405 return 0;
wdenkc6097192002-11-03 00:24:07 +0000406}
407
Dirk Eibach42b204f2013-04-25 02:40:01 +0000408static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
409 int alen, uchar *buffer, int len)
wdenkc6097192002-11-03 00:24:07 +0000410{
Dirk Eibach42b204f2013-04-25 02:40:01 +0000411 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100412}
wdenkc6097192002-11-03 00:24:07 +0000413
Dirk Eibach42b204f2013-04-25 02:40:01 +0000414static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
415 int alen, uchar *buffer, int len)
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100416{
Dirk Eibach42b204f2013-04-25 02:40:01 +0000417 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
wdenkc6097192002-11-03 00:24:07 +0000418}
419
Dirk Eibach42b204f2013-04-25 02:40:01 +0000420static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
421 unsigned int speed)
Stefan Roesed07117e2007-02-20 10:27:08 +0100422{
Dirk Eibach42b204f2013-04-25 02:40:01 +0000423 if (speed != adap->speed)
Stefan Roesed07117e2007-02-20 10:27:08 +0100424 return -1;
Dirk Eibach42b204f2013-04-25 02:40:01 +0000425 return speed;
Stefan Roesed07117e2007-02-20 10:27:08 +0100426}
Dirk Eibach42b204f2013-04-25 02:40:01 +0000427
428/*
429 * Register ppc4xx i2c adapters
430 */
431#ifdef CONFIG_SYS_I2C_PPC4XX_CH0
432U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
433 ppc4xx_i2c_read, ppc4xx_i2c_write,
434 ppc4xx_i2c_set_bus_speed,
435 CONFIG_SYS_I2C_PPC4XX_SPEED_0,
436 CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
437#endif
438#ifdef CONFIG_SYS_I2C_PPC4XX_CH1
439U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
440 ppc4xx_i2c_read, ppc4xx_i2c_write,
441 ppc4xx_i2c_set_bus_speed,
442 CONFIG_SYS_I2C_PPC4XX_SPEED_1,
443 CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
444#endif