Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 1 | /* |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 2 | * (C) Copyright 2007-2009 |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 3 | * 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 11 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | |
| 13 | #include <common.h> |
Stefan Roese | 247e9d7 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 14 | #include <asm/ppc4xx.h> |
| 15 | #include <asm/ppc4xx-i2c.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 16 | #include <i2c.h> |
Peter Tyser | 133c0fe | 2010-04-12 22:28:07 -0500 | [diff] [blame] | 17 | #include <asm/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 18 | |
| 19 | #ifdef CONFIG_HARD_I2C |
| 20 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 23 | #if defined(CONFIG_I2C_MULTI_BUS) |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 24 | /* |
| 25 | * Initialize the bus pointer to whatever one the SPD EEPROM is on. |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 26 | * 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 Piepho | 3e9dabd | 2008-11-12 17:29:48 -0800 | [diff] [blame] | 30 | #ifndef CONFIG_SYS_SPD_BUS_NUM |
| 31 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 32 | #endif |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 33 | static unsigned int i2c_bus_num __attribute__ ((section (".data"))) = |
| 34 | CONFIG_SYS_SPD_BUS_NUM; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 35 | #endif /* CONFIG_I2C_MULTI_BUS */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 37 | static void _i2c_bus_reset(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 39 | struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 40 | int i; |
| 41 | u8 dc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | |
| 43 | /* Reset status register */ |
| 44 | /* write 1 in SCMP and IRQA to clear these fields */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 45 | out_8(&i2c->sts, 0x0A); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | |
| 47 | /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 48 | out_8(&i2c->extsts, 0x8F); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 50 | /* Place chip in the reset state */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 51 | out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 53 | /* Check if bus is free */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 54 | dc = in_8(&i2c->directcntl); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 55 | if (!DIRCTNL_FREE(dc)){ |
| 56 | /* Try to set bus free state */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 57 | out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 58 | |
| 59 | /* Wait until we regain bus control */ |
| 60 | for (i = 0; i < 100; ++i) { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 61 | dc = in_8(&i2c->directcntl); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 62 | if (DIRCTNL_FREE(dc)) |
| 63 | break; |
| 64 | |
| 65 | /* Toggle SCL line */ |
| 66 | dc ^= IIC_DIRCNTL_SCC; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 67 | out_8(&i2c->directcntl, dc); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 68 | udelay(10); |
| 69 | dc ^= IIC_DIRCNTL_SCC; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 70 | out_8(&i2c->directcntl, dc); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 73 | |
| 74 | /* Remove reset */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 75 | out_8(&i2c->xtcntlss, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 78 | void i2c_init(int speed, int slaveaddr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 79 | { |
Stefan Roese | b1bcb0e | 2010-03-29 15:30:46 +0200 | [diff] [blame] | 80 | struct ppc4xx_i2c *i2c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | int val, divisor; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 82 | int bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 84 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 85 | /* |
| 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 | */ |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 90 | i2c_init_board(); |
| 91 | #endif |
| 92 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 93 | for (bus = 0; bus < CONFIG_SYS_MAX_I2C_BUS; bus++) { |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 94 | I2C_SET_BUS(bus); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | |
Stefan Roese | b1bcb0e | 2010-03-29 15:30:46 +0200 | [diff] [blame] | 96 | /* Set i2c pointer after calling I2C_SET_BUS() */ |
| 97 | i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR; |
| 98 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 99 | /* Handle possible failed I2C state */ |
| 100 | /* FIXME: put this into i2c_init_board()? */ |
| 101 | _i2c_bus_reset(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 103 | /* clear lo master address */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 104 | out_8(&i2c->lmadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 106 | /* clear hi master address */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 107 | out_8(&i2c->hmadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 109 | /* clear lo slave address */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 110 | out_8(&i2c->lsadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 112 | /* clear hi slave address */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 113 | out_8(&i2c->hsadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 115 | /* Clock divide Register */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 116 | /* set divisor according to freq_opb */ |
| 117 | divisor = (get_OPB_freq() - 1) / 10000000; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 118 | if (divisor == 0) |
| 119 | divisor = 1; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 120 | out_8(&i2c->clkdiv, divisor); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 122 | /* no interrupts */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 123 | out_8(&i2c->intrmsk, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 124 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 125 | /* clear transfer count */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 126 | out_8(&i2c->xfrcnt, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 128 | /* clear extended control & stat */ |
| 129 | /* write 1 in SRC SRS SWC SWS to clear these fields */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 130 | out_8(&i2c->xtcntlss, 0xF0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 132 | /* Mode Control Register |
| 133 | Flush Slave/Master data buffer */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 134 | out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 136 | val = in_8(&i2c->mdcntl); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 138 | /* 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 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 142 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 143 | val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 144 | if (speed >= 400000) |
| 145 | val |= IIC_MDCNTL_FSM; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 146 | out_8(&i2c->mdcntl, val); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 148 | /* clear control reg */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 149 | out_8(&i2c->cntl, 0x00); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 150 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 152 | /* set to SPD bus as default bus upon powerup */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 153 | I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 157 | * 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 | */ |
| 181 | static 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) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 187 | { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 188 | struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR; |
| 189 | u8 *ptr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 190 | int reading; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 191 | int tran, cnt; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 192 | int result; |
| 193 | int status; |
| 194 | int i; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 195 | u8 creg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 197 | if (data == 0 || data_len == 0) { |
| 198 | /* Don't support data transfer of no length or to address 0 */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 199 | printf( "i2c_transfer: bad call\n" ); |
| 200 | return IIC_NOK; |
| 201 | } |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 202 | if (addr && addr_len) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 203 | ptr = addr; |
| 204 | cnt = addr_len; |
| 205 | reading = 0; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 206 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 207 | ptr = data; |
| 208 | cnt = data_len; |
| 209 | reading = cmd_type; |
| 210 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 212 | /* Clear Stop Complete Bit */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 213 | out_8(&i2c->sts, IIC_STS_SCMP); |
| 214 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | /* Check init */ |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 216 | i = 10; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 217 | do { |
| 218 | /* Get status */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 219 | status = in_8(&i2c->sts); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 220 | i--; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 221 | } while ((status & IIC_STS_PT) && (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 222 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | if (status & IIC_STS_PT) { |
| 224 | result = IIC_NOK_TOUT; |
| 225 | return(result); |
| 226 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 227 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 228 | /* flush the Master/Slave Databuffers */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 229 | out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) | |
| 230 | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB); |
| 231 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 232 | /* need to wait 4 OPB clocks? code below should take that long */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 234 | /* 7-bit adressing */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 235 | out_8(&i2c->hmadr, 0); |
| 236 | out_8(&i2c->lmadr, chip); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | tran = 0; |
| 239 | result = IIC_OK; |
| 240 | creg = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 242 | while (tran != cnt && (result == IIC_OK)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 243 | int bc,j; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 244 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Control register = |
| 247 | * Normal transfer, 7-bits adressing, Transfer up to |
| 248 | * bc bytes, Normal start, Transfer is a sequence of transfers |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 249 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 250 | creg |= IIC_CNTL_PT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 251 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 252 | 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)) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 256 | creg |= IIC_CNTL_CHT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 258 | if (reading) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 259 | creg |= IIC_CNTL_READ; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 260 | } else { |
| 261 | for(j = 0; j < bc; j++) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 262 | /* Set buffer */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 263 | out_8(&i2c->mdbuf, ptr[tran + j]); |
| 264 | } |
| 265 | } |
| 266 | out_8(&i2c->cntl, creg); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 268 | /* |
| 269 | * Transfer is in progress |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 270 | * 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 Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 276 | i = 2 * 5 * 8; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 277 | do { |
| 278 | /* Get status */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 279 | status = in_8(&i2c->sts); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 280 | udelay(10); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 281 | i--; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 282 | } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) && |
| 283 | (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 285 | if (status & IIC_STS_ERR) { |
| 286 | result = IIC_NOK; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 287 | status = in_8(&i2c->extsts); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 288 | /* 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 Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 300 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 301 | /* 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 Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 306 | * 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 Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 311 | */ |
| 312 | udelay(1); |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 313 | for (j = 0; j < bc; j++) |
| 314 | ptr[tran + j] = in_8(&i2c->mdbuf); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 315 | } else |
| 316 | result = IIC_NOK_DATA; |
| 317 | } |
| 318 | creg = 0; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 319 | tran += bc; |
| 320 | if (ptr == addr && tran == cnt) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 321 | ptr = data; |
| 322 | cnt = data_len; |
| 323 | tran = 0; |
| 324 | reading = cmd_type; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 325 | if (reading) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 326 | creg = IIC_CNTL_RPST; |
| 327 | } |
| 328 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 329 | return result; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 332 | int i2c_probe(uchar chip) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 333 | { |
| 334 | uchar buf[1]; |
| 335 | |
| 336 | buf[0] = 0; |
| 337 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 338 | /* |
| 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 Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 343 | return (i2c_transfer(1, chip << 1, 0, 0, buf, 1) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 346 | static int ppc4xx_i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, |
| 347 | int len, int read) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 349 | uchar xaddr[4]; |
| 350 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 351 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 352 | if (alen > 4) { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 353 | printf("I2C: addr len %d not supported\n", alen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 354 | return 1; |
| 355 | } |
| 356 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 357 | if (alen > 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 358 | xaddr[0] = (addr >> 24) & 0xFF; |
| 359 | xaddr[1] = (addr >> 16) & 0xFF; |
| 360 | xaddr[2] = (addr >> 8) & 0xFF; |
| 361 | xaddr[3] = addr & 0xFF; |
| 362 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 363 | |
| 364 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 365 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 366 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 367 | * 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. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 372 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 373 | * 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. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 376 | */ |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 377 | if (alen > 0) |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 378 | chip |= ((addr >> (alen * 8)) & |
| 379 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 380 | #endif |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 381 | if ((ret = i2c_transfer(read, chip << 1, &xaddr[4 - alen], alen, |
| 382 | buffer, len)) != 0) { |
Graeme Russ | 70600b0 | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 383 | printf("I2C %s: failed %d\n", read ? "read" : "write", ret); |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 384 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 385 | return 1; |
| 386 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 387 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 388 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 391 | int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 392 | { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 393 | return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 1); |
| 394 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 396 | int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len) |
| 397 | { |
| 398 | return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 401 | #if defined(CONFIG_I2C_MULTI_BUS) |
| 402 | /* |
| 403 | * Functions for multiple I2C bus handling |
| 404 | */ |
| 405 | unsigned int i2c_get_bus_num(void) |
| 406 | { |
| 407 | return i2c_bus_num; |
| 408 | } |
| 409 | |
| 410 | int i2c_set_bus_num(unsigned int bus) |
| 411 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 412 | if (bus >= CONFIG_SYS_MAX_I2C_BUS) |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 413 | return -1; |
| 414 | |
| 415 | i2c_bus_num = bus; |
| 416 | |
| 417 | return 0; |
| 418 | } |
Matthias Fuchs | 62f0704 | 2007-03-08 16:23:11 +0100 | [diff] [blame] | 419 | #endif /* CONFIG_I2C_MULTI_BUS */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 420 | #endif /* CONFIG_HARD_I2C */ |