123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- #include <common.h>
- #include <asm/ppc4xx.h>
- #include <asm/ppc4xx-i2c.h>
- #include <i2c.h>
- #include <asm/io.h>
- DECLARE_GLOBAL_DATA_PTR;
- 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)
- base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
- #else
- base = 0xEF600500 + (hwadapnr * 0x100);
- #endif
- return (struct ppc4xx_i2c *)base;
- }
- static void _i2c_bus_reset(struct i2c_adapter *adap)
- {
- struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
- int i;
- u8 dc;
-
-
- out_8(&i2c->sts, 0x0A);
-
- out_8(&i2c->extsts, 0x8F);
-
- out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
-
- dc = in_8(&i2c->directcntl);
- if (!DIRCTNL_FREE(dc)){
-
- out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
-
- for (i = 0; i < 100; ++i) {
- dc = in_8(&i2c->directcntl);
- if (DIRCTNL_FREE(dc))
- break;
-
- dc ^= IIC_DIRCNTL_SCC;
- out_8(&i2c->directcntl, dc);
- udelay(10);
- dc ^= IIC_DIRCNTL_SCC;
- out_8(&i2c->directcntl, dc);
- }
- }
-
- out_8(&i2c->xtcntlss, 0);
- }
- static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
- {
- struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
- int val, divisor;
- #ifdef CONFIG_SYS_I2C_INIT_BOARD
-
- i2c_init_board();
- #endif
-
-
- _i2c_bus_reset(adap);
-
- out_8(&i2c->lmadr, 0);
-
- out_8(&i2c->hmadr, 0);
-
- out_8(&i2c->lsadr, 0);
-
- out_8(&i2c->hsadr, 0);
-
-
- divisor = (get_OPB_freq() - 1) / 10000000;
- if (divisor == 0)
- divisor = 1;
- out_8(&i2c->clkdiv, divisor);
-
- out_8(&i2c->intrmsk, 0);
-
- out_8(&i2c->xfrcnt, 0);
-
-
- out_8(&i2c->xtcntlss, 0xF0);
-
- out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
- val = in_8(&i2c->mdcntl);
-
- val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
- if (speed >= 400000)
- val |= IIC_MDCNTL_FSM;
- out_8(&i2c->mdcntl, val);
-
- out_8(&i2c->cntl, 0x00);
- }
- static int _i2c_transfer(struct i2c_adapter *adap,
- unsigned char cmd_type,
- unsigned char chip,
- unsigned char addr[],
- unsigned char addr_len,
- unsigned char data[],
- unsigned short data_len)
- {
- struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
- u8 *ptr;
- int reading;
- int tran, cnt;
- int result;
- int status;
- int i;
- u8 creg;
- if (data == 0 || data_len == 0) {
-
- printf( "i2c_transfer: bad call\n" );
- return IIC_NOK;
- }
- if (addr && addr_len) {
- ptr = addr;
- cnt = addr_len;
- reading = 0;
- } else {
- ptr = data;
- cnt = data_len;
- reading = cmd_type;
- }
-
- out_8(&i2c->sts, IIC_STS_SCMP);
-
- i = 10;
- do {
-
- status = in_8(&i2c->sts);
- i--;
- } while ((status & IIC_STS_PT) && (i > 0));
- if (status & IIC_STS_PT) {
- result = IIC_NOK_TOUT;
- return(result);
- }
-
- out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
- IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
-
-
- out_8(&i2c->hmadr, 0);
- out_8(&i2c->lmadr, chip);
- tran = 0;
- result = IIC_OK;
- creg = 0;
- while (tran != cnt && (result == IIC_OK)) {
- int bc,j;
-
- creg |= IIC_CNTL_PT;
- bc = (cnt - tran) > 4 ? 4 : cnt - tran;
- creg |= (bc - 1) << 4;
-
- if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
- creg |= IIC_CNTL_CHT;
-
- if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
- creg |= IIC_CNTL_RPST;
- if (reading) {
- creg |= IIC_CNTL_READ;
- } else {
- for(j = 0; j < bc; j++) {
-
- out_8(&i2c->mdbuf, ptr[tran + j]);
- }
- }
- out_8(&i2c->cntl, creg);
-
- i = 2 * 5 * 8;
- do {
-
- status = in_8(&i2c->sts);
- udelay(10);
- i--;
- } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
- (i > 0));
- if (status & IIC_STS_ERR) {
- result = IIC_NOK;
- status = in_8(&i2c->extsts);
-
- if (status & IIC_EXTSTS_LA)
- result = IIC_NOK_LA;
-
- if (status & IIC_EXTSTS_ICT)
- result = IIC_NOK_ICT;
-
- if (status & IIC_EXTSTS_XFRA)
- result = IIC_NOK_XFRA;
-
- if ((status & IIC_EXTSTS_BCS_MASK)
- != IIC_EXTSTS_BCS_FREE){
- u8 mdcntl = in_8(&i2c->mdcntl);
-
- 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);
- }
- } else if ( status & IIC_STS_PT) {
- result = IIC_NOK_TOUT;
- }
-
- if ((reading) && (result == IIC_OK)) {
-
- if (status & IIC_STS_MDBS) {
-
- udelay(1);
- for (j = 0; j < bc; j++)
- ptr[tran + j] = in_8(&i2c->mdbuf);
- } else
- result = IIC_NOK_DATA;
- }
- creg = 0;
- tran += bc;
- if (ptr == addr && tran == cnt) {
- ptr = data;
- cnt = data_len;
- tran = 0;
- reading = cmd_type;
- }
- }
- return result;
- }
- static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
- {
- uchar buf[1];
- buf[0] = 0;
-
- return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
- }
- static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
- int alen, uchar *buffer, int len, int read)
- {
- uchar xaddr[4];
- int ret;
- if (alen > 4) {
- printf("I2C: addr len %d not supported\n", alen);
- return 1;
- }
- if (alen > 0) {
- xaddr[0] = (addr >> 24) & 0xFF;
- xaddr[1] = (addr >> 16) & 0xFF;
- xaddr[2] = (addr >> 8) & 0xFF;
- xaddr[3] = addr & 0xFF;
- }
- #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
-
- if (alen > 0)
- chip |= ((addr >> (alen * 8)) &
- CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
- #endif
- ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
- buffer, len);
- if (ret) {
- printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
- return 1;
- }
- return 0;
- }
- static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
- int alen, uchar *buffer, int len)
- {
- return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
- }
- static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
- int alen, uchar *buffer, int len)
- {
- return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
- }
- static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
- unsigned int speed)
- {
- if (speed != adap->speed)
- return -1;
- return speed;
- }
- #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
|