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 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 21 | static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr) |
| 22 | { |
| 23 | unsigned long base; |
| 24 | |
| 25 | #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \ |
| 26 | defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \ |
| 27 | defined(CONFIG_460EX) || defined(CONFIG_460GT) |
| 28 | base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100); |
| 29 | #elif defined(CONFIG_440) || defined(CONFIG_405EX) |
| 30 | /* all remaining 440 variants */ |
| 31 | base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100); |
| 32 | #else |
| 33 | /* all 405 variants */ |
| 34 | base = 0xEF600500 + (hwadapnr * 0x100); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 35 | #endif |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 36 | return (struct ppc4xx_i2c *)base; |
| 37 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 39 | static void _i2c_bus_reset(struct i2c_adapter *adap) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 41 | struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 42 | int i; |
| 43 | u8 dc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | |
| 45 | /* Reset status register */ |
| 46 | /* write 1 in SCMP and IRQA to clear these fields */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 47 | out_8(&i2c->sts, 0x0A); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | |
| 49 | /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 50 | out_8(&i2c->extsts, 0x8F); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 52 | /* Place chip in the reset state */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 53 | out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 55 | /* Check if bus is free */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 56 | dc = in_8(&i2c->directcntl); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 57 | if (!DIRCTNL_FREE(dc)){ |
| 58 | /* Try to set bus free state */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 59 | out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 60 | |
| 61 | /* Wait until we regain bus control */ |
| 62 | for (i = 0; i < 100; ++i) { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 63 | dc = in_8(&i2c->directcntl); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 64 | if (DIRCTNL_FREE(dc)) |
| 65 | break; |
| 66 | |
| 67 | /* Toggle SCL line */ |
| 68 | dc ^= IIC_DIRCNTL_SCC; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 69 | out_8(&i2c->directcntl, dc); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 70 | udelay(10); |
| 71 | dc ^= IIC_DIRCNTL_SCC; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 72 | out_8(&i2c->directcntl, dc); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 75 | |
| 76 | /* Remove reset */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 77 | out_8(&i2c->xtcntlss, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 80 | static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 82 | struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | int val, divisor; |
| 84 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 85 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 86 | /* |
| 87 | * Call board specific i2c bus reset routine before accessing the |
| 88 | * environment, which might be in a chip on that bus. For details |
| 89 | * about this problem see doc/I2C_Edge_Conditions. |
| 90 | */ |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 91 | i2c_init_board(); |
| 92 | #endif |
| 93 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 94 | /* Handle possible failed I2C state */ |
| 95 | /* FIXME: put this into i2c_init_board()? */ |
| 96 | _i2c_bus_reset(adap); |
Stefan Roese | b1bcb0e | 2010-03-29 15:30:46 +0200 | [diff] [blame] | 97 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 98 | /* clear lo master address */ |
| 99 | out_8(&i2c->lmadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 101 | /* clear hi master address */ |
| 102 | out_8(&i2c->hmadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 104 | /* clear lo slave address */ |
| 105 | out_8(&i2c->lsadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 107 | /* clear hi slave address */ |
| 108 | out_8(&i2c->hsadr, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 110 | /* Clock divide Register */ |
| 111 | /* set divisor according to freq_opb */ |
| 112 | divisor = (get_OPB_freq() - 1) / 10000000; |
| 113 | if (divisor == 0) |
| 114 | divisor = 1; |
| 115 | out_8(&i2c->clkdiv, divisor); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 117 | /* no interrupts */ |
| 118 | out_8(&i2c->intrmsk, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 120 | /* clear transfer count */ |
| 121 | out_8(&i2c->xfrcnt, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 123 | /* clear extended control & stat */ |
| 124 | /* write 1 in SRC SRS SWC SWS to clear these fields */ |
| 125 | out_8(&i2c->xtcntlss, 0xF0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 127 | /* Mode Control Register |
| 128 | Flush Slave/Master data buffer */ |
| 129 | out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 131 | val = in_8(&i2c->mdcntl); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 133 | /* Ignore General Call, slave transfers are ignored, |
| 134 | * disable interrupts, exit unknown bus state, enable hold |
| 135 | * SCL 100kHz normaly or FastMode for 400kHz and above |
| 136 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 138 | val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL; |
| 139 | if (speed >= 400000) |
| 140 | val |= IIC_MDCNTL_FSM; |
| 141 | out_8(&i2c->mdcntl, val); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 142 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 143 | /* clear control reg */ |
| 144 | out_8(&i2c->cntl, 0x00); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 148 | * This code tries to use the features of the 405GP i2c |
| 149 | * controller. It will transfer up to 4 bytes in one pass |
| 150 | * on the loop. It only does out_8((u8 *)lbz) to the buffer when it |
| 151 | * is possible to do out16(lhz) transfers. |
| 152 | * |
| 153 | * cmd_type is 0 for write 1 for read. |
| 154 | * |
| 155 | * addr_len can take any value from 0-255, it is only limited |
| 156 | * by the char, we could make it larger if needed. If it is |
| 157 | * 0 we skip the address write cycle. |
| 158 | * |
| 159 | * Typical case is a Write of an addr followd by a Read. The |
| 160 | * IBM FAQ does not cover this. On the last byte of the write |
| 161 | * we don't set the creg CHT bit, and on the first bytes of the |
| 162 | * read we set the RPST bit. |
| 163 | * |
| 164 | * It does not support address only transfers, there must be |
| 165 | * a data part. If you want to write the address yourself, put |
| 166 | * it in the data pointer. |
| 167 | * |
| 168 | * It does not support transfer to/from address 0. |
| 169 | * |
| 170 | * It does not check XFRCNT. |
| 171 | */ |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 172 | static int _i2c_transfer(struct i2c_adapter *adap, |
| 173 | unsigned char cmd_type, |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 174 | unsigned char chip, |
| 175 | unsigned char addr[], |
| 176 | unsigned char addr_len, |
| 177 | unsigned char data[], |
| 178 | unsigned short data_len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 179 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 180 | struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr); |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 181 | u8 *ptr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 182 | int reading; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 183 | int tran, cnt; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 184 | int result; |
| 185 | int status; |
| 186 | int i; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 187 | u8 creg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 188 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 189 | if (data == 0 || data_len == 0) { |
| 190 | /* Don't support data transfer of no length or to address 0 */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 191 | printf( "i2c_transfer: bad call\n" ); |
| 192 | return IIC_NOK; |
| 193 | } |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 194 | if (addr && addr_len) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 195 | ptr = addr; |
| 196 | cnt = addr_len; |
| 197 | reading = 0; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 198 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 199 | ptr = data; |
| 200 | cnt = data_len; |
| 201 | reading = cmd_type; |
| 202 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 204 | /* Clear Stop Complete Bit */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 205 | out_8(&i2c->sts, IIC_STS_SCMP); |
| 206 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 207 | /* Check init */ |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 208 | i = 10; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 209 | do { |
| 210 | /* Get status */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 211 | status = in_8(&i2c->sts); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 212 | i--; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 213 | } while ((status & IIC_STS_PT) && (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | if (status & IIC_STS_PT) { |
| 216 | result = IIC_NOK_TOUT; |
| 217 | return(result); |
| 218 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 219 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 220 | /* flush the Master/Slave Databuffers */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 221 | out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) | |
| 222 | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB); |
| 223 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 224 | /* need to wait 4 OPB clocks? code below should take that long */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 225 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 226 | /* 7-bit adressing */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 227 | out_8(&i2c->hmadr, 0); |
| 228 | out_8(&i2c->lmadr, chip); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 230 | tran = 0; |
| 231 | result = IIC_OK; |
| 232 | creg = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 234 | while (tran != cnt && (result == IIC_OK)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 235 | int bc,j; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Control register = |
| 239 | * Normal transfer, 7-bits adressing, Transfer up to |
| 240 | * bc bytes, Normal start, Transfer is a sequence of transfers |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 241 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 242 | creg |= IIC_CNTL_PT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 243 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 244 | bc = (cnt - tran) > 4 ? 4 : cnt - tran; |
| 245 | creg |= (bc - 1) << 4; |
| 246 | /* if the real cmd type is write continue trans */ |
| 247 | if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt)) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | creg |= IIC_CNTL_CHT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 250 | if (reading) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 251 | creg |= IIC_CNTL_READ; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 252 | } else { |
| 253 | for(j = 0; j < bc; j++) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 254 | /* Set buffer */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 255 | out_8(&i2c->mdbuf, ptr[tran + j]); |
| 256 | } |
| 257 | } |
| 258 | out_8(&i2c->cntl, creg); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 260 | /* |
| 261 | * Transfer is in progress |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 262 | * we have to wait for upto 5 bytes of data |
| 263 | * 1 byte chip address+r/w bit then bc bytes |
| 264 | * of data. |
| 265 | * udelay(10) is 1 bit time at 100khz |
| 266 | * Doubled for slop. 20 is too small. |
| 267 | */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 268 | i = 2 * 5 * 8; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | do { |
| 270 | /* Get status */ |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 271 | status = in_8(&i2c->sts); |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 272 | udelay(10); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 273 | i--; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 274 | } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) && |
| 275 | (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 277 | if (status & IIC_STS_ERR) { |
| 278 | result = IIC_NOK; |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 279 | status = in_8(&i2c->extsts); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 280 | /* Lost arbitration? */ |
| 281 | if (status & IIC_EXTSTS_LA) |
| 282 | result = IIC_NOK_LA; |
| 283 | /* Incomplete transfer? */ |
| 284 | if (status & IIC_EXTSTS_ICT) |
| 285 | result = IIC_NOK_ICT; |
| 286 | /* Transfer aborted? */ |
| 287 | if (status & IIC_EXTSTS_XFRA) |
| 288 | result = IIC_NOK_XFRA; |
| 289 | } else if ( status & IIC_STS_PT) { |
| 290 | result = IIC_NOK_TOUT; |
| 291 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 292 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 293 | /* Command is reading => get buffer */ |
| 294 | if ((reading) && (result == IIC_OK)) { |
| 295 | /* Are there data in buffer */ |
| 296 | if (status & IIC_STS_MDBS) { |
| 297 | /* |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 298 | * even if we have data we have to wait 4OPB |
| 299 | * clocks for it to hit the front of the FIFO, |
| 300 | * after that we can just read. We should check |
| 301 | * XFCNT here and if the FIFO is full there is |
| 302 | * no need to wait. |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 303 | */ |
| 304 | udelay(1); |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 305 | for (j = 0; j < bc; j++) |
| 306 | ptr[tran + j] = in_8(&i2c->mdbuf); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 307 | } else |
| 308 | result = IIC_NOK_DATA; |
| 309 | } |
| 310 | creg = 0; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 311 | tran += bc; |
| 312 | if (ptr == addr && tran == cnt) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 313 | ptr = data; |
| 314 | cnt = data_len; |
| 315 | tran = 0; |
| 316 | reading = cmd_type; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 317 | if (reading) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 318 | creg = IIC_CNTL_RPST; |
| 319 | } |
| 320 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 321 | return result; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 324 | static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 325 | { |
| 326 | uchar buf[1]; |
| 327 | |
| 328 | buf[0] = 0; |
| 329 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 330 | /* |
| 331 | * What is needed is to send the chip address and verify that the |
| 332 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 333 | * drove the data line low). |
| 334 | */ |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 335 | return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 338 | static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr, |
| 339 | int alen, uchar *buffer, int len, int read) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 341 | uchar xaddr[4]; |
| 342 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 344 | if (alen > 4) { |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 345 | printf("I2C: addr len %d not supported\n", alen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 346 | return 1; |
| 347 | } |
| 348 | |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 349 | if (alen > 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 350 | xaddr[0] = (addr >> 24) & 0xFF; |
| 351 | xaddr[1] = (addr >> 16) & 0xFF; |
| 352 | xaddr[2] = (addr >> 8) & 0xFF; |
| 353 | xaddr[3] = addr & 0xFF; |
| 354 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 355 | |
| 356 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 357 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 358 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 359 | * EEPROM chips that implement "address overflow" are ones |
| 360 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 361 | * address and the extra bits end up in the "chip address" |
| 362 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 363 | * four 256 byte chips. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 364 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 365 | * Note that we consider the length of the address field to |
| 366 | * still be one byte because the extra address bits are |
| 367 | * hidden in the chip address. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 368 | */ |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 369 | if (alen > 0) |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 370 | chip |= ((addr >> (alen * 8)) & |
| 371 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 372 | #endif |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 373 | ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen, |
| 374 | buffer, len); |
| 375 | if (ret) { |
Graeme Russ | 70600b0 | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 376 | printf("I2C %s: failed %d\n", read ? "read" : "write", ret); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 377 | return 1; |
| 378 | } |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 379 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 380 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 383 | static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 384 | int alen, uchar *buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 385 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 386 | return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1); |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 387 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 388 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 389 | static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 390 | int alen, uchar *buffer, int len) |
Stefan Roese | 77c1f1d | 2009-11-19 14:03:17 +0100 | [diff] [blame] | 391 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 392 | return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 395 | static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 396 | unsigned int speed) |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 397 | { |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 398 | if (speed != adap->speed) |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 399 | return -1; |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 400 | return speed; |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 401 | } |
Dirk Eibach | 42b204f | 2013-04-25 02:40:01 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Register ppc4xx i2c adapters |
| 405 | */ |
| 406 | #ifdef CONFIG_SYS_I2C_PPC4XX_CH0 |
| 407 | U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe, |
| 408 | ppc4xx_i2c_read, ppc4xx_i2c_write, |
| 409 | ppc4xx_i2c_set_bus_speed, |
| 410 | CONFIG_SYS_I2C_PPC4XX_SPEED_0, |
| 411 | CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0) |
| 412 | #endif |
| 413 | #ifdef CONFIG_SYS_I2C_PPC4XX_CH1 |
| 414 | U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe, |
| 415 | ppc4xx_i2c_read, ppc4xx_i2c_write, |
| 416 | ppc4xx_i2c_set_bus_speed, |
| 417 | CONFIG_SYS_I2C_PPC4XX_SPEED_1, |
| 418 | CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1) |
| 419 | #endif |