123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include <config.h>
- #include <common.h>
- #include <tsi108.h>
- #if defined(CONFIG_CMD_I2C)
- #define I2C_DELAY 100000
- #undef DEBUG_I2C
- #ifdef DEBUG_I2C
- #define DPRINT(x) printf (x)
- #else
- #define DPRINT(x)
- #endif
- void i2c_init(int speed, int slaveaddr)
- {
-
- }
- static int i2c_read_byte (
- uint i2c_chan,
- uchar chip_addr,
- uint byte_addr,
- uchar * buffer
- )
- {
- u32 temp;
- u32 to_count = I2C_DELAY;
- u32 op_status = TSI108_I2C_TIMEOUT_ERR;
- u32 chan_offset = TSI108_I2C_OFFSET;
- DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
- i2c_chan, chip_addr, byte_addr));
- if (0 != i2c_chan)
- chan_offset = TSI108_I2C_SDRAM_OFFSET;
-
- temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
- if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
- I2C_CNTRL2_START))) {
-
- temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
- ((chip_addr >> 3) & 0x0F);
- *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
- temp;
-
- *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
- (I2C_CNTRL2_START);
-
- do {
-
- temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
- if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
- if (0 == (temp &
- (I2C_CNTRL2_I2C_CFGERR |
- I2C_CNTRL2_I2C_TO_ERR))
- ) {
- op_status = TSI108_I2C_SUCCESS;
- temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
- chan_offset +
- I2C_RD_DATA);
- *buffer = (u8) (temp & 0xFF);
- } else {
-
- op_status = TSI108_I2C_IF_ERROR;
- DPRINT (("I2C HW error reported: 0x%02x\n", temp));
- }
- break;
- }
- } while (to_count--);
- } else {
- op_status = TSI108_I2C_IF_BUSY;
- DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
- }
- DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
- return op_status;
- }
- int i2c_read (uchar chip_addr, uint byte_addr, int alen,
- uchar * buffer, int len)
- {
- u32 op_status = TSI108_I2C_PARAM_ERR;
- u32 i2c_if = 0;
-
- if (0xD0 == (chip_addr & ~0x07)) {
- i2c_if = 1;
- chip_addr &= 0x7F;
- }
-
- if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
- while (len--) {
- op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
- if (TSI108_I2C_SUCCESS != op_status) {
- DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
- break;
- }
- }
- }
- DPRINT (("I2C read() status: 0x%02x\n", op_status));
- return op_status;
- }
- static int i2c_write_byte (uchar chip_addr,
- uint byte_addr,
- uchar * buffer
- )
- {
- u32 temp;
- u32 to_count = I2C_DELAY;
- u32 op_status = TSI108_I2C_TIMEOUT_ERR;
-
- temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
- if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
-
- *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
- I2C_TX_DATA) = (u32) * buffer;
-
- temp =
- I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
- ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
- *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
- I2C_CNTRL1) = temp;
-
- *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
- I2C_CNTRL2) = (I2C_CNTRL2_START);
- op_status = TSI108_I2C_TIMEOUT_ERR;
-
- do {
-
- temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
- if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
- if (0 == (temp &
- (I2C_CNTRL2_I2C_CFGERR |
- I2C_CNTRL2_I2C_TO_ERR))) {
- op_status = TSI108_I2C_SUCCESS;
- } else {
-
- op_status = TSI108_I2C_IF_ERROR;
- DPRINT (("I2C HW error reported: 0x%02x\n", temp));
- }
- break;
- }
- } while (to_count--);
- } else {
- op_status = TSI108_I2C_IF_BUSY;
- DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
- }
- return op_status;
- }
- int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
- int len)
- {
- u32 op_status = TSI108_I2C_PARAM_ERR;
-
- if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
- while (len--) {
- op_status =
- i2c_write_byte (chip_addr, byte_addr++, buffer++);
- if (TSI108_I2C_SUCCESS != op_status) {
- DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
- break;
- }
- }
- }
- return op_status;
- }
- int i2c_probe (uchar chip)
- {
- u32 tmp;
-
- return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
- }
- #endif
|