blob: 52929ebc4e660f5deef9c07b929e67d340e4eedf [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+
Stefan Roesed07117e2007-02-20 10:27:08 +010011 */
wdenkc6097192002-11-03 00:24:07 +000012
13#include <common.h>
Stefan Roese247e9d72010-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 Tyser133c0fe2010-04-12 22:28:07 -050017#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000018
19#ifdef CONFIG_HARD_I2C
20
Wolfgang Denk6405a152006-03-31 18:32:53 +020021DECLARE_GLOBAL_DATA_PTR;
22
Stefan Roesed07117e2007-02-20 10:27:08 +010023#if defined(CONFIG_I2C_MULTI_BUS)
Stefan Roese77c1f1d2009-11-19 14:03:17 +010024/*
25 * Initialize the bus pointer to whatever one the SPD EEPROM is on.
Stefan Roesed07117e2007-02-20 10:27:08 +010026 * Default is bus 0. This is necessary because the DDR initialization
27 * runs from ROM, and we can't switch buses because we can't modify
28 * the global variables.
29 */
Trent Piepho3e9dabd2008-11-12 17:29:48 -080030#ifndef CONFIG_SYS_SPD_BUS_NUM
31#define CONFIG_SYS_SPD_BUS_NUM 0
Stefan Roesed07117e2007-02-20 10:27:08 +010032#endif
Stefan Roese77c1f1d2009-11-19 14:03:17 +010033static unsigned int i2c_bus_num __attribute__ ((section (".data"))) =
34 CONFIG_SYS_SPD_BUS_NUM;
Stefan Roesed07117e2007-02-20 10:27:08 +010035#endif /* CONFIG_I2C_MULTI_BUS */
wdenkc6097192002-11-03 00:24:07 +000036
Stefan Roesed07117e2007-02-20 10:27:08 +010037static void _i2c_bus_reset(void)
wdenkc6097192002-11-03 00:24:07 +000038{
Stefan Roese77c1f1d2009-11-19 14:03:17 +010039 struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
Stefan Roesed07117e2007-02-20 10:27:08 +010040 int i;
41 u8 dc;
wdenkc6097192002-11-03 00:24:07 +000042
43 /* Reset status register */
44 /* write 1 in SCMP and IRQA to clear these fields */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010045 out_8(&i2c->sts, 0x0A);
wdenkc6097192002-11-03 00:24:07 +000046
47 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010048 out_8(&i2c->extsts, 0x8F);
wdenkc6097192002-11-03 00:24:07 +000049
Wolfgang Denka1be4762008-05-20 16:00:29 +020050 /* Place chip in the reset state */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010051 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
wdenkc6097192002-11-03 00:24:07 +000052
Stefan Roesed07117e2007-02-20 10:27:08 +010053 /* Check if bus is free */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010054 dc = in_8(&i2c->directcntl);
Stefan Roesed07117e2007-02-20 10:27:08 +010055 if (!DIRCTNL_FREE(dc)){
56 /* Try to set bus free state */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010057 out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
Stefan Roesed07117e2007-02-20 10:27:08 +010058
59 /* Wait until we regain bus control */
60 for (i = 0; i < 100; ++i) {
Stefan Roese77c1f1d2009-11-19 14:03:17 +010061 dc = in_8(&i2c->directcntl);
Stefan Roesed07117e2007-02-20 10:27:08 +010062 if (DIRCTNL_FREE(dc))
63 break;
64
65 /* Toggle SCL line */
66 dc ^= IIC_DIRCNTL_SCC;
Stefan Roese77c1f1d2009-11-19 14:03:17 +010067 out_8(&i2c->directcntl, dc);
Stefan Roesed07117e2007-02-20 10:27:08 +010068 udelay(10);
69 dc ^= IIC_DIRCNTL_SCC;
Stefan Roese77c1f1d2009-11-19 14:03:17 +010070 out_8(&i2c->directcntl, dc);
wdenkc6097192002-11-03 00:24:07 +000071 }
72 }
Stefan Roesed07117e2007-02-20 10:27:08 +010073
74 /* Remove reset */
Stefan Roese77c1f1d2009-11-19 14:03:17 +010075 out_8(&i2c->xtcntlss, 0);
wdenkc6097192002-11-03 00:24:07 +000076}
77
Stefan Roese77c1f1d2009-11-19 14:03:17 +010078void i2c_init(int speed, int slaveaddr)
wdenkc6097192002-11-03 00:24:07 +000079{
Stefan Roeseb1bcb0e2010-03-29 15:30:46 +020080 struct ppc4xx_i2c *i2c;
wdenkc6097192002-11-03 00:24:07 +000081 int val, divisor;
Stefan Roesed07117e2007-02-20 10:27:08 +010082 int bus;
wdenkc6097192002-11-03 00:24:07 +000083
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084#ifdef CONFIG_SYS_I2C_INIT_BOARD
Stefan Roese77c1f1d2009-11-19 14:03:17 +010085 /*
86 * Call board specific i2c bus reset routine before accessing the
87 * environment, which might be in a chip on that bus. For details
88 * about this problem see doc/I2C_Edge_Conditions.
89 */
wdenkcc1e2562003-03-06 13:39:27 +000090 i2c_init_board();
91#endif
92
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093 for (bus = 0; bus < CONFIG_SYS_MAX_I2C_BUS; bus++) {
Stefan Roesed07117e2007-02-20 10:27:08 +010094 I2C_SET_BUS(bus);
wdenkc6097192002-11-03 00:24:07 +000095
Stefan Roeseb1bcb0e2010-03-29 15:30:46 +020096 /* Set i2c pointer after calling I2C_SET_BUS() */
97 i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
98
Stefan Roesed07117e2007-02-20 10:27:08 +010099 /* Handle possible failed I2C state */
100 /* FIXME: put this into i2c_init_board()? */
101 _i2c_bus_reset();
wdenkc6097192002-11-03 00:24:07 +0000102
Stefan Roesed07117e2007-02-20 10:27:08 +0100103 /* clear lo master address */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100104 out_8(&i2c->lmadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000105
Stefan Roesed07117e2007-02-20 10:27:08 +0100106 /* clear hi master address */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100107 out_8(&i2c->hmadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000108
Stefan Roesed07117e2007-02-20 10:27:08 +0100109 /* clear lo slave address */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100110 out_8(&i2c->lsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000111
Stefan Roesed07117e2007-02-20 10:27:08 +0100112 /* clear hi slave address */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100113 out_8(&i2c->hsadr, 0);
wdenkc6097192002-11-03 00:24:07 +0000114
Stefan Roesed07117e2007-02-20 10:27:08 +0100115 /* Clock divide Register */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100116 /* set divisor according to freq_opb */
117 divisor = (get_OPB_freq() - 1) / 10000000;
Stefan Roesed07117e2007-02-20 10:27:08 +0100118 if (divisor == 0)
119 divisor = 1;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100120 out_8(&i2c->clkdiv, divisor);
wdenkc6097192002-11-03 00:24:07 +0000121
Stefan Roesed07117e2007-02-20 10:27:08 +0100122 /* no interrupts */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100123 out_8(&i2c->intrmsk, 0);
wdenkc6097192002-11-03 00:24:07 +0000124
Stefan Roesed07117e2007-02-20 10:27:08 +0100125 /* clear transfer count */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100126 out_8(&i2c->xfrcnt, 0);
wdenkc6097192002-11-03 00:24:07 +0000127
Stefan Roesed07117e2007-02-20 10:27:08 +0100128 /* clear extended control & stat */
129 /* write 1 in SRC SRS SWC SWS to clear these fields */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100130 out_8(&i2c->xtcntlss, 0xF0);
wdenkc6097192002-11-03 00:24:07 +0000131
Stefan Roesed07117e2007-02-20 10:27:08 +0100132 /* Mode Control Register
133 Flush Slave/Master data buffer */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100134 out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
wdenkc6097192002-11-03 00:24:07 +0000135
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100136 val = in_8(&i2c->mdcntl);
wdenkc6097192002-11-03 00:24:07 +0000137
Stefan Roesed07117e2007-02-20 10:27:08 +0100138 /* Ignore General Call, slave transfers are ignored,
139 * disable interrupts, exit unknown bus state, enable hold
140 * SCL 100kHz normaly or FastMode for 400kHz and above
141 */
wdenkc6097192002-11-03 00:24:07 +0000142
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100143 val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
Stefan Roesed07117e2007-02-20 10:27:08 +0100144 if (speed >= 400000)
145 val |= IIC_MDCNTL_FSM;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100146 out_8(&i2c->mdcntl, val);
wdenkc6097192002-11-03 00:24:07 +0000147
Stefan Roesed07117e2007-02-20 10:27:08 +0100148 /* clear control reg */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100149 out_8(&i2c->cntl, 0x00);
Stefan Roesed07117e2007-02-20 10:27:08 +0100150 }
wdenkc6097192002-11-03 00:24:07 +0000151
Stefan Roesed07117e2007-02-20 10:27:08 +0100152 /* set to SPD bus as default bus upon powerup */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200153 I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM);
wdenkc6097192002-11-03 00:24:07 +0000154}
155
156/*
Stefan Roesed07117e2007-02-20 10:27:08 +0100157 * This code tries to use the features of the 405GP i2c
158 * controller. It will transfer up to 4 bytes in one pass
159 * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
160 * is possible to do out16(lhz) transfers.
161 *
162 * cmd_type is 0 for write 1 for read.
163 *
164 * addr_len can take any value from 0-255, it is only limited
165 * by the char, we could make it larger if needed. If it is
166 * 0 we skip the address write cycle.
167 *
168 * Typical case is a Write of an addr followd by a Read. The
169 * IBM FAQ does not cover this. On the last byte of the write
170 * we don't set the creg CHT bit, and on the first bytes of the
171 * read we set the RPST bit.
172 *
173 * It does not support address only transfers, there must be
174 * a data part. If you want to write the address yourself, put
175 * it in the data pointer.
176 *
177 * It does not support transfer to/from address 0.
178 *
179 * It does not check XFRCNT.
180 */
181static int i2c_transfer(unsigned char cmd_type,
182 unsigned char chip,
183 unsigned char addr[],
184 unsigned char addr_len,
185 unsigned char data[],
186 unsigned short data_len)
wdenkc6097192002-11-03 00:24:07 +0000187{
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100188 struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
189 u8 *ptr;
wdenk57b2d802003-06-27 21:31:46 +0000190 int reading;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100191 int tran, cnt;
wdenk57b2d802003-06-27 21:31:46 +0000192 int result;
193 int status;
194 int i;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100195 u8 creg;
wdenkc6097192002-11-03 00:24:07 +0000196
Stefan Roesed07117e2007-02-20 10:27:08 +0100197 if (data == 0 || data_len == 0) {
198 /* Don't support data transfer of no length or to address 0 */
wdenk57b2d802003-06-27 21:31:46 +0000199 printf( "i2c_transfer: bad call\n" );
200 return IIC_NOK;
201 }
Stefan Roesed07117e2007-02-20 10:27:08 +0100202 if (addr && addr_len) {
wdenk57b2d802003-06-27 21:31:46 +0000203 ptr = addr;
204 cnt = addr_len;
205 reading = 0;
Stefan Roesed07117e2007-02-20 10:27:08 +0100206 } else {
wdenk57b2d802003-06-27 21:31:46 +0000207 ptr = data;
208 cnt = data_len;
209 reading = cmd_type;
210 }
wdenkc6097192002-11-03 00:24:07 +0000211
Stefan Roesed07117e2007-02-20 10:27:08 +0100212 /* Clear Stop Complete Bit */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100213 out_8(&i2c->sts, IIC_STS_SCMP);
214
wdenk57b2d802003-06-27 21:31:46 +0000215 /* Check init */
Stefan Roesed07117e2007-02-20 10:27:08 +0100216 i = 10;
wdenk57b2d802003-06-27 21:31:46 +0000217 do {
218 /* Get status */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100219 status = in_8(&i2c->sts);
wdenk57b2d802003-06-27 21:31:46 +0000220 i--;
Stefan Roesed07117e2007-02-20 10:27:08 +0100221 } while ((status & IIC_STS_PT) && (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000222
wdenk57b2d802003-06-27 21:31:46 +0000223 if (status & IIC_STS_PT) {
224 result = IIC_NOK_TOUT;
225 return(result);
226 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100227
Stefan Roesed07117e2007-02-20 10:27:08 +0100228 /* flush the Master/Slave Databuffers */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100229 out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
230 IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
231
Stefan Roesed07117e2007-02-20 10:27:08 +0100232 /* need to wait 4 OPB clocks? code below should take that long */
wdenkc6097192002-11-03 00:24:07 +0000233
wdenk57b2d802003-06-27 21:31:46 +0000234 /* 7-bit adressing */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100235 out_8(&i2c->hmadr, 0);
236 out_8(&i2c->lmadr, chip);
wdenkc6097192002-11-03 00:24:07 +0000237
wdenk57b2d802003-06-27 21:31:46 +0000238 tran = 0;
239 result = IIC_OK;
240 creg = 0;
wdenkc6097192002-11-03 00:24:07 +0000241
Stefan Roesed07117e2007-02-20 10:27:08 +0100242 while (tran != cnt && (result == IIC_OK)) {
wdenk57b2d802003-06-27 21:31:46 +0000243 int bc,j;
wdenkc6097192002-11-03 00:24:07 +0000244
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100245 /*
246 * Control register =
247 * Normal transfer, 7-bits adressing, Transfer up to
248 * bc bytes, Normal start, Transfer is a sequence of transfers
Stefan Roesed07117e2007-02-20 10:27:08 +0100249 */
wdenk57b2d802003-06-27 21:31:46 +0000250 creg |= IIC_CNTL_PT;
wdenkc6097192002-11-03 00:24:07 +0000251
Stefan Roesed07117e2007-02-20 10:27:08 +0100252 bc = (cnt - tran) > 4 ? 4 : cnt - tran;
253 creg |= (bc - 1) << 4;
254 /* if the real cmd type is write continue trans */
255 if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
wdenk57b2d802003-06-27 21:31:46 +0000256 creg |= IIC_CNTL_CHT;
wdenkc6097192002-11-03 00:24:07 +0000257
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100258 if (reading) {
wdenk57b2d802003-06-27 21:31:46 +0000259 creg |= IIC_CNTL_READ;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100260 } else {
261 for(j = 0; j < bc; j++) {
wdenk57b2d802003-06-27 21:31:46 +0000262 /* Set buffer */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100263 out_8(&i2c->mdbuf, ptr[tran + j]);
264 }
265 }
266 out_8(&i2c->cntl, creg);
wdenkc6097192002-11-03 00:24:07 +0000267
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100268 /*
269 * Transfer is in progress
Stefan Roesed07117e2007-02-20 10:27:08 +0100270 * we have to wait for upto 5 bytes of data
271 * 1 byte chip address+r/w bit then bc bytes
272 * of data.
273 * udelay(10) is 1 bit time at 100khz
274 * Doubled for slop. 20 is too small.
275 */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100276 i = 2 * 5 * 8;
wdenk57b2d802003-06-27 21:31:46 +0000277 do {
278 /* Get status */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100279 status = in_8(&i2c->sts);
Stefan Roesed07117e2007-02-20 10:27:08 +0100280 udelay(10);
wdenk57b2d802003-06-27 21:31:46 +0000281 i--;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100282 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
283 (i > 0));
wdenkc6097192002-11-03 00:24:07 +0000284
wdenk57b2d802003-06-27 21:31:46 +0000285 if (status & IIC_STS_ERR) {
286 result = IIC_NOK;
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100287 status = in_8(&i2c->extsts);
wdenk57b2d802003-06-27 21:31:46 +0000288 /* Lost arbitration? */
289 if (status & IIC_EXTSTS_LA)
290 result = IIC_NOK_LA;
291 /* Incomplete transfer? */
292 if (status & IIC_EXTSTS_ICT)
293 result = IIC_NOK_ICT;
294 /* Transfer aborted? */
295 if (status & IIC_EXTSTS_XFRA)
296 result = IIC_NOK_XFRA;
297 } else if ( status & IIC_STS_PT) {
298 result = IIC_NOK_TOUT;
299 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100300
wdenk57b2d802003-06-27 21:31:46 +0000301 /* Command is reading => get buffer */
302 if ((reading) && (result == IIC_OK)) {
303 /* Are there data in buffer */
304 if (status & IIC_STS_MDBS) {
305 /*
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100306 * even if we have data we have to wait 4OPB
307 * clocks for it to hit the front of the FIFO,
308 * after that we can just read. We should check
309 * XFCNT here and if the FIFO is full there is
310 * no need to wait.
Stefan Roesed07117e2007-02-20 10:27:08 +0100311 */
312 udelay(1);
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100313 for (j = 0; j < bc; j++)
314 ptr[tran + j] = in_8(&i2c->mdbuf);
wdenk57b2d802003-06-27 21:31:46 +0000315 } else
316 result = IIC_NOK_DATA;
317 }
318 creg = 0;
Stefan Roesed07117e2007-02-20 10:27:08 +0100319 tran += bc;
320 if (ptr == addr && tran == cnt) {
wdenk57b2d802003-06-27 21:31:46 +0000321 ptr = data;
322 cnt = data_len;
323 tran = 0;
324 reading = cmd_type;
Stefan Roesed07117e2007-02-20 10:27:08 +0100325 if (reading)
wdenk57b2d802003-06-27 21:31:46 +0000326 creg = IIC_CNTL_RPST;
327 }
328 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100329 return result;
wdenkc6097192002-11-03 00:24:07 +0000330}
331
Stefan Roesed07117e2007-02-20 10:27:08 +0100332int i2c_probe(uchar chip)
wdenkc6097192002-11-03 00:24:07 +0000333{
334 uchar buf[1];
335
336 buf[0] = 0;
337
wdenk57b2d802003-06-27 21:31:46 +0000338 /*
339 * What is needed is to send the chip address and verify that the
340 * address was <ACK>ed (i.e. there was a chip at that address which
341 * drove the data line low).
342 */
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100343 return (i2c_transfer(1, chip << 1, 0, 0, buf, 1) != 0);
wdenkc6097192002-11-03 00:24:07 +0000344}
345
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100346static int ppc4xx_i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer,
347 int len, int read)
wdenkc6097192002-11-03 00:24:07 +0000348{
wdenk57b2d802003-06-27 21:31:46 +0000349 uchar xaddr[4];
350 int ret;
wdenkc6097192002-11-03 00:24:07 +0000351
Stefan Roesed07117e2007-02-20 10:27:08 +0100352 if (alen > 4) {
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100353 printf("I2C: addr len %d not supported\n", alen);
wdenkc6097192002-11-03 00:24:07 +0000354 return 1;
355 }
356
Stefan Roesed07117e2007-02-20 10:27:08 +0100357 if (alen > 0) {
wdenk57b2d802003-06-27 21:31:46 +0000358 xaddr[0] = (addr >> 24) & 0xFF;
359 xaddr[1] = (addr >> 16) & 0xFF;
360 xaddr[2] = (addr >> 8) & 0xFF;
361 xaddr[3] = addr & 0xFF;
362 }
wdenkc6097192002-11-03 00:24:07 +0000363
364
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200365#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkc6097192002-11-03 00:24:07 +0000366 /*
wdenk57b2d802003-06-27 21:31:46 +0000367 * EEPROM chips that implement "address overflow" are ones
368 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
369 * address and the extra bits end up in the "chip address"
370 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
371 * four 256 byte chips.
wdenkc6097192002-11-03 00:24:07 +0000372 *
wdenk57b2d802003-06-27 21:31:46 +0000373 * Note that we consider the length of the address field to
374 * still be one byte because the extra address bits are
375 * hidden in the chip address.
wdenkc6097192002-11-03 00:24:07 +0000376 */
Stefan Roesed07117e2007-02-20 10:27:08 +0100377 if (alen > 0)
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100378 chip |= ((addr >> (alen * 8)) &
379 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenkc6097192002-11-03 00:24:07 +0000380#endif
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100381 if ((ret = i2c_transfer(read, chip << 1, &xaddr[4 - alen], alen,
382 buffer, len)) != 0) {
Graeme Russ70600b02011-08-29 02:14:05 +0000383 printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100384
wdenk57b2d802003-06-27 21:31:46 +0000385 return 1;
386 }
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100387
wdenk57b2d802003-06-27 21:31:46 +0000388 return 0;
wdenkc6097192002-11-03 00:24:07 +0000389}
390
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100391int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
wdenkc6097192002-11-03 00:24:07 +0000392{
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100393 return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 1);
394}
wdenkc6097192002-11-03 00:24:07 +0000395
Stefan Roese77c1f1d2009-11-19 14:03:17 +0100396int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
397{
398 return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 0);
wdenkc6097192002-11-03 00:24:07 +0000399}
400
Stefan Roesed07117e2007-02-20 10:27:08 +0100401#if defined(CONFIG_I2C_MULTI_BUS)
402/*
403 * Functions for multiple I2C bus handling
404 */
405unsigned int i2c_get_bus_num(void)
406{
407 return i2c_bus_num;
408}
409
410int i2c_set_bus_num(unsigned int bus)
411{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200412 if (bus >= CONFIG_SYS_MAX_I2C_BUS)
Stefan Roesed07117e2007-02-20 10:27:08 +0100413 return -1;
414
415 i2c_bus_num = bus;
416
417 return 0;
418}
Matthias Fuchs62f07042007-03-08 16:23:11 +0100419#endif /* CONFIG_I2C_MULTI_BUS */
wdenkc6097192002-11-03 00:24:07 +0000420#endif /* CONFIG_HARD_I2C */