2007-02-20 09:27:08 +00:00
|
|
|
/*
|
2009-11-19 13:03:17 +00:00
|
|
|
* (C) Copyright 2007-2009
|
2007-02-20 09:27:08 +00:00
|
|
|
* Stefan Roese, DENX Software Engineering, sr@denx.de.
|
|
|
|
*
|
|
|
|
* based on work by Anne Sophie Harnois <anne-sophie.harnois@nextream.fr>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2001
|
|
|
|
* Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2007-02-20 09:27:08 +00:00
|
|
|
*/
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
#include <common.h>
|
2010-09-09 17:18:00 +00:00
|
|
|
#include <asm/ppc4xx.h>
|
|
|
|
#include <asm/ppc4xx-i2c.h>
|
2002-11-03 00:24:07 +00:00
|
|
|
#include <i2c.h>
|
2010-04-13 03:28:07 +00:00
|
|
|
#include <asm/io.h>
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2006-03-31 16:32:53 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr)
|
|
|
|
{
|
|
|
|
unsigned long base;
|
|
|
|
|
|
|
|
#if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
|
|
|
|
defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
|
|
|
|
defined(CONFIG_460EX) || defined(CONFIG_460GT)
|
|
|
|
base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100);
|
|
|
|
#elif defined(CONFIG_440) || defined(CONFIG_405EX)
|
|
|
|
/* all remaining 440 variants */
|
|
|
|
base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
|
|
|
|
#else
|
|
|
|
/* all 405 variants */
|
|
|
|
base = 0xEF600500 + (hwadapnr * 0x100);
|
2007-02-20 09:27:08 +00:00
|
|
|
#endif
|
2013-04-25 02:40:01 +00:00
|
|
|
return (struct ppc4xx_i2c *)base;
|
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static void _i2c_bus_reset(struct i2c_adapter *adap)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
|
2007-02-20 09:27:08 +00:00
|
|
|
int i;
|
|
|
|
u8 dc;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Reset status register */
|
|
|
|
/* write 1 in SCMP and IRQA to clear these fields */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->sts, 0x0A);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->extsts, 0x8F);
|
2007-02-20 09:27:08 +00:00
|
|
|
|
2008-05-20 14:00:29 +00:00
|
|
|
/* Place chip in the reset state */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
|
2007-02-20 09:27:08 +00:00
|
|
|
|
|
|
|
/* Check if bus is free */
|
2009-11-19 13:03:17 +00:00
|
|
|
dc = in_8(&i2c->directcntl);
|
2007-02-20 09:27:08 +00:00
|
|
|
if (!DIRCTNL_FREE(dc)){
|
|
|
|
/* Try to set bus free state */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
|
2007-02-20 09:27:08 +00:00
|
|
|
|
|
|
|
/* Wait until we regain bus control */
|
|
|
|
for (i = 0; i < 100; ++i) {
|
2009-11-19 13:03:17 +00:00
|
|
|
dc = in_8(&i2c->directcntl);
|
2007-02-20 09:27:08 +00:00
|
|
|
if (DIRCTNL_FREE(dc))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Toggle SCL line */
|
|
|
|
dc ^= IIC_DIRCNTL_SCC;
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->directcntl, dc);
|
2007-02-20 09:27:08 +00:00
|
|
|
udelay(10);
|
|
|
|
dc ^= IIC_DIRCNTL_SCC;
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->directcntl, dc);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-20 09:27:08 +00:00
|
|
|
|
|
|
|
/* Remove reset */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->xtcntlss, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
|
2002-11-03 00:24:07 +00:00
|
|
|
int val, divisor;
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_I2C_INIT_BOARD
|
2009-11-19 13:03:17 +00:00
|
|
|
/*
|
|
|
|
* Call board specific i2c bus reset routine before accessing the
|
|
|
|
* environment, which might be in a chip on that bus. For details
|
|
|
|
* about this problem see doc/I2C_Edge_Conditions.
|
|
|
|
*/
|
2003-03-06 13:39:27 +00:00
|
|
|
i2c_init_board();
|
|
|
|
#endif
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* Handle possible failed I2C state */
|
|
|
|
/* FIXME: put this into i2c_init_board()? */
|
|
|
|
_i2c_bus_reset(adap);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear lo master address */
|
|
|
|
out_8(&i2c->lmadr, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear hi master address */
|
|
|
|
out_8(&i2c->hmadr, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear lo slave address */
|
|
|
|
out_8(&i2c->lsadr, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear hi slave address */
|
|
|
|
out_8(&i2c->hsadr, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* Clock divide Register */
|
|
|
|
/* set divisor according to freq_opb */
|
|
|
|
divisor = (get_OPB_freq() - 1) / 10000000;
|
|
|
|
if (divisor == 0)
|
|
|
|
divisor = 1;
|
|
|
|
out_8(&i2c->clkdiv, divisor);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* no interrupts */
|
|
|
|
out_8(&i2c->intrmsk, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear transfer count */
|
|
|
|
out_8(&i2c->xfrcnt, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear extended control & stat */
|
|
|
|
/* write 1 in SRC SRS SWC SWS to clear these fields */
|
|
|
|
out_8(&i2c->xtcntlss, 0xF0);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* Mode Control Register
|
|
|
|
Flush Slave/Master data buffer */
|
|
|
|
out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
val = in_8(&i2c->mdcntl);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* Ignore General Call, slave transfers are ignored,
|
|
|
|
* disable interrupts, exit unknown bus state, enable hold
|
|
|
|
* SCL 100kHz normaly or FastMode for 400kHz and above
|
|
|
|
*/
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
|
|
|
|
if (speed >= 400000)
|
|
|
|
val |= IIC_MDCNTL_FSM;
|
|
|
|
out_8(&i2c->mdcntl, val);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
/* clear control reg */
|
|
|
|
out_8(&i2c->cntl, 0x00);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-20 09:27:08 +00:00
|
|
|
* This code tries to use the features of the 405GP i2c
|
|
|
|
* controller. It will transfer up to 4 bytes in one pass
|
|
|
|
* on the loop. It only does out_8((u8 *)lbz) to the buffer when it
|
|
|
|
* is possible to do out16(lhz) transfers.
|
|
|
|
*
|
|
|
|
* cmd_type is 0 for write 1 for read.
|
|
|
|
*
|
|
|
|
* addr_len can take any value from 0-255, it is only limited
|
|
|
|
* by the char, we could make it larger if needed. If it is
|
|
|
|
* 0 we skip the address write cycle.
|
|
|
|
*
|
|
|
|
* Typical case is a Write of an addr followd by a Read. The
|
|
|
|
* IBM FAQ does not cover this. On the last byte of the write
|
2014-10-29 14:56:43 +00:00
|
|
|
* we don't set the creg CHT bit but the RPST bit.
|
2007-02-20 09:27:08 +00:00
|
|
|
*
|
|
|
|
* It does not support address only transfers, there must be
|
|
|
|
* a data part. If you want to write the address yourself, put
|
|
|
|
* it in the data pointer.
|
|
|
|
*
|
|
|
|
* It does not support transfer to/from address 0.
|
|
|
|
*
|
|
|
|
* It does not check XFRCNT.
|
|
|
|
*/
|
2013-04-25 02:40:01 +00:00
|
|
|
static int _i2c_transfer(struct i2c_adapter *adap,
|
|
|
|
unsigned char cmd_type,
|
2007-02-20 09:27:08 +00:00
|
|
|
unsigned char chip,
|
|
|
|
unsigned char addr[],
|
|
|
|
unsigned char addr_len,
|
|
|
|
unsigned char data[],
|
|
|
|
unsigned short data_len)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
|
2009-11-19 13:03:17 +00:00
|
|
|
u8 *ptr;
|
2003-06-27 21:31:46 +00:00
|
|
|
int reading;
|
2009-11-19 13:03:17 +00:00
|
|
|
int tran, cnt;
|
2003-06-27 21:31:46 +00:00
|
|
|
int result;
|
|
|
|
int status;
|
|
|
|
int i;
|
2009-11-19 13:03:17 +00:00
|
|
|
u8 creg;
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
if (data == 0 || data_len == 0) {
|
|
|
|
/* Don't support data transfer of no length or to address 0 */
|
2003-06-27 21:31:46 +00:00
|
|
|
printf( "i2c_transfer: bad call\n" );
|
|
|
|
return IIC_NOK;
|
|
|
|
}
|
2007-02-20 09:27:08 +00:00
|
|
|
if (addr && addr_len) {
|
2003-06-27 21:31:46 +00:00
|
|
|
ptr = addr;
|
|
|
|
cnt = addr_len;
|
|
|
|
reading = 0;
|
2007-02-20 09:27:08 +00:00
|
|
|
} else {
|
2003-06-27 21:31:46 +00:00
|
|
|
ptr = data;
|
|
|
|
cnt = data_len;
|
|
|
|
reading = cmd_type;
|
|
|
|
}
|
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
/* Clear Stop Complete Bit */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->sts, IIC_STS_SCMP);
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
/* Check init */
|
2007-02-20 09:27:08 +00:00
|
|
|
i = 10;
|
2003-06-27 21:31:46 +00:00
|
|
|
do {
|
|
|
|
/* Get status */
|
2009-11-19 13:03:17 +00:00
|
|
|
status = in_8(&i2c->sts);
|
2003-06-27 21:31:46 +00:00
|
|
|
i--;
|
2007-02-20 09:27:08 +00:00
|
|
|
} while ((status & IIC_STS_PT) && (i > 0));
|
2003-06-27 21:31:46 +00:00
|
|
|
|
|
|
|
if (status & IIC_STS_PT) {
|
|
|
|
result = IIC_NOK_TOUT;
|
|
|
|
return(result);
|
|
|
|
}
|
2009-11-19 13:03:17 +00:00
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
/* flush the Master/Slave Databuffers */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
|
|
|
|
IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
|
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
/* need to wait 4 OPB clocks? code below should take that long */
|
2003-06-27 21:31:46 +00:00
|
|
|
|
|
|
|
/* 7-bit adressing */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->hmadr, 0);
|
|
|
|
out_8(&i2c->lmadr, chip);
|
2003-06-27 21:31:46 +00:00
|
|
|
|
|
|
|
tran = 0;
|
|
|
|
result = IIC_OK;
|
|
|
|
creg = 0;
|
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
while (tran != cnt && (result == IIC_OK)) {
|
2003-06-27 21:31:46 +00:00
|
|
|
int bc,j;
|
|
|
|
|
2009-11-19 13:03:17 +00:00
|
|
|
/*
|
|
|
|
* Control register =
|
|
|
|
* Normal transfer, 7-bits adressing, Transfer up to
|
|
|
|
* bc bytes, Normal start, Transfer is a sequence of transfers
|
2007-02-20 09:27:08 +00:00
|
|
|
*/
|
2003-06-27 21:31:46 +00:00
|
|
|
creg |= IIC_CNTL_PT;
|
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
bc = (cnt - tran) > 4 ? 4 : cnt - tran;
|
|
|
|
creg |= (bc - 1) << 4;
|
|
|
|
/* if the real cmd type is write continue trans */
|
|
|
|
if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
|
2003-06-27 21:31:46 +00:00
|
|
|
creg |= IIC_CNTL_CHT;
|
|
|
|
|
2014-10-29 14:56:43 +00:00
|
|
|
/* last part of address, prepare for repeated start on read */
|
|
|
|
if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
|
|
|
|
creg |= IIC_CNTL_RPST;
|
|
|
|
|
2009-11-19 13:03:17 +00:00
|
|
|
if (reading) {
|
2003-06-27 21:31:46 +00:00
|
|
|
creg |= IIC_CNTL_READ;
|
2009-11-19 13:03:17 +00:00
|
|
|
} else {
|
|
|
|
for(j = 0; j < bc; j++) {
|
2003-06-27 21:31:46 +00:00
|
|
|
/* Set buffer */
|
2009-11-19 13:03:17 +00:00
|
|
|
out_8(&i2c->mdbuf, ptr[tran + j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_8(&i2c->cntl, creg);
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2009-11-19 13:03:17 +00:00
|
|
|
/*
|
|
|
|
* Transfer is in progress
|
2007-02-20 09:27:08 +00:00
|
|
|
* we have to wait for upto 5 bytes of data
|
|
|
|
* 1 byte chip address+r/w bit then bc bytes
|
|
|
|
* of data.
|
|
|
|
* udelay(10) is 1 bit time at 100khz
|
|
|
|
* Doubled for slop. 20 is too small.
|
|
|
|
*/
|
2009-11-19 13:03:17 +00:00
|
|
|
i = 2 * 5 * 8;
|
2003-06-27 21:31:46 +00:00
|
|
|
do {
|
|
|
|
/* Get status */
|
2009-11-19 13:03:17 +00:00
|
|
|
status = in_8(&i2c->sts);
|
2007-02-20 09:27:08 +00:00
|
|
|
udelay(10);
|
2003-06-27 21:31:46 +00:00
|
|
|
i--;
|
2009-11-19 13:03:17 +00:00
|
|
|
} while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
|
|
|
|
(i > 0));
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
if (status & IIC_STS_ERR) {
|
|
|
|
result = IIC_NOK;
|
2009-11-19 13:03:17 +00:00
|
|
|
status = in_8(&i2c->extsts);
|
2003-06-27 21:31:46 +00:00
|
|
|
/* Lost arbitration? */
|
|
|
|
if (status & IIC_EXTSTS_LA)
|
|
|
|
result = IIC_NOK_LA;
|
|
|
|
/* Incomplete transfer? */
|
|
|
|
if (status & IIC_EXTSTS_ICT)
|
|
|
|
result = IIC_NOK_ICT;
|
|
|
|
/* Transfer aborted? */
|
|
|
|
if (status & IIC_EXTSTS_XFRA)
|
|
|
|
result = IIC_NOK_XFRA;
|
2014-10-29 14:56:44 +00:00
|
|
|
/* Is bus free?
|
|
|
|
* If error happened during combined xfer
|
|
|
|
* IIC interface is usually stuck in some strange
|
|
|
|
* state without a valid stop condition.
|
|
|
|
* Brute, but working: generate stop, then soft reset.
|
|
|
|
*/
|
|
|
|
if ((status & IIC_EXTSTS_BCS_MASK)
|
|
|
|
!= IIC_EXTSTS_BCS_FREE){
|
|
|
|
u8 mdcntl = in_8(&i2c->mdcntl);
|
|
|
|
|
|
|
|
/* Generate valid stop condition */
|
|
|
|
out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
|
|
|
|
out_8(&i2c->directcntl, IIC_DIRCNTL_SCC);
|
|
|
|
udelay(10);
|
|
|
|
out_8(&i2c->directcntl,
|
|
|
|
IIC_DIRCNTL_SCC | IIC_DIRCNTL_SDAC);
|
|
|
|
out_8(&i2c->xtcntlss, 0);
|
|
|
|
|
|
|
|
ppc4xx_i2c_init(adap, (mdcntl & IIC_MDCNTL_FSM)
|
|
|
|
? 400000 : 100000, 0);
|
|
|
|
}
|
2003-06-27 21:31:46 +00:00
|
|
|
} else if ( status & IIC_STS_PT) {
|
|
|
|
result = IIC_NOK_TOUT;
|
|
|
|
}
|
2009-11-19 13:03:17 +00:00
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
/* Command is reading => get buffer */
|
|
|
|
if ((reading) && (result == IIC_OK)) {
|
|
|
|
/* Are there data in buffer */
|
|
|
|
if (status & IIC_STS_MDBS) {
|
|
|
|
/*
|
2009-11-19 13:03:17 +00:00
|
|
|
* even if we have data we have to wait 4OPB
|
|
|
|
* clocks for it to hit the front of the FIFO,
|
|
|
|
* after that we can just read. We should check
|
|
|
|
* XFCNT here and if the FIFO is full there is
|
|
|
|
* no need to wait.
|
2007-02-20 09:27:08 +00:00
|
|
|
*/
|
|
|
|
udelay(1);
|
2009-11-19 13:03:17 +00:00
|
|
|
for (j = 0; j < bc; j++)
|
|
|
|
ptr[tran + j] = in_8(&i2c->mdbuf);
|
2003-06-27 21:31:46 +00:00
|
|
|
} else
|
|
|
|
result = IIC_NOK_DATA;
|
|
|
|
}
|
|
|
|
creg = 0;
|
2007-02-20 09:27:08 +00:00
|
|
|
tran += bc;
|
|
|
|
if (ptr == addr && tran == cnt) {
|
2003-06-27 21:31:46 +00:00
|
|
|
ptr = data;
|
|
|
|
cnt = data_len;
|
|
|
|
tran = 0;
|
|
|
|
reading = cmd_type;
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 13:03:17 +00:00
|
|
|
return result;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
uchar buf[1];
|
|
|
|
|
|
|
|
buf[0] = 0;
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
/*
|
|
|
|
* What is needed is to send the chip address and verify that the
|
|
|
|
* address was <ACK>ed (i.e. there was a chip at that address which
|
|
|
|
* drove the data line low).
|
|
|
|
*/
|
2013-04-25 02:40:01 +00:00
|
|
|
return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
|
|
|
|
int alen, uchar *buffer, int len, int read)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2003-06-27 21:31:46 +00:00
|
|
|
uchar xaddr[4];
|
|
|
|
int ret;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
if (alen > 4) {
|
2009-11-19 13:03:17 +00:00
|
|
|
printf("I2C: addr len %d not supported\n", alen);
|
2002-11-03 00:24:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-20 09:27:08 +00:00
|
|
|
if (alen > 0) {
|
2003-06-27 21:31:46 +00:00
|
|
|
xaddr[0] = (addr >> 24) & 0xFF;
|
|
|
|
xaddr[1] = (addr >> 16) & 0xFF;
|
|
|
|
xaddr[2] = (addr >> 8) & 0xFF;
|
|
|
|
xaddr[3] = addr & 0xFF;
|
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
|
2002-11-03 00:24:07 +00:00
|
|
|
/*
|
2003-06-27 21:31:46 +00:00
|
|
|
* EEPROM chips that implement "address overflow" are ones
|
|
|
|
* like Catalyst 24WC04/08/16 which has 9/10/11 bits of
|
|
|
|
* address and the extra bits end up in the "chip address"
|
|
|
|
* bit slots. This makes a 24WC08 (1Kbyte) chip look like
|
|
|
|
* four 256 byte chips.
|
2002-11-03 00:24:07 +00:00
|
|
|
*
|
2003-06-27 21:31:46 +00:00
|
|
|
* Note that we consider the length of the address field to
|
|
|
|
* still be one byte because the extra address bits are
|
|
|
|
* hidden in the chip address.
|
2002-11-03 00:24:07 +00:00
|
|
|
*/
|
2007-02-20 09:27:08 +00:00
|
|
|
if (alen > 0)
|
2009-11-19 13:03:17 +00:00
|
|
|
chip |= ((addr >> (alen * 8)) &
|
|
|
|
CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
|
2002-11-03 00:24:07 +00:00
|
|
|
#endif
|
2013-04-25 02:40:01 +00:00
|
|
|
ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
|
|
|
|
buffer, len);
|
|
|
|
if (ret) {
|
2011-08-29 02:14:05 +00:00
|
|
|
printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
|
2003-06-27 21:31:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-11-19 13:03:17 +00:00
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
return 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
|
|
|
|
int alen, uchar *buffer, int len)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
|
2009-11-19 13:03:17 +00:00
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
|
|
|
|
int alen, uchar *buffer, int len)
|
2009-11-19 13:03:17 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 02:40:01 +00:00
|
|
|
static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
|
|
|
|
unsigned int speed)
|
2007-02-20 09:27:08 +00:00
|
|
|
{
|
2013-04-25 02:40:01 +00:00
|
|
|
if (speed != adap->speed)
|
2007-02-20 09:27:08 +00:00
|
|
|
return -1;
|
2013-04-25 02:40:01 +00:00
|
|
|
return speed;
|
2007-02-20 09:27:08 +00:00
|
|
|
}
|
2013-04-25 02:40:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register ppc4xx i2c adapters
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_SYS_I2C_PPC4XX_CH0
|
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
|
|
|
|
ppc4xx_i2c_read, ppc4xx_i2c_write,
|
|
|
|
ppc4xx_i2c_set_bus_speed,
|
|
|
|
CONFIG_SYS_I2C_PPC4XX_SPEED_0,
|
|
|
|
CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SYS_I2C_PPC4XX_CH1
|
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
|
|
|
|
ppc4xx_i2c_read, ppc4xx_i2c_write,
|
|
|
|
ppc4xx_i2c_set_bus_speed,
|
|
|
|
CONFIG_SYS_I2C_PPC4XX_SPEED_1,
|
|
|
|
CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
|
|
|
|
#endif
|