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