Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 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 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | |
| 29 | #include <common.h> |
| 30 | #include <ppc4xx.h> |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 31 | #include <4xx_i2c.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | #include <i2c.h> |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 33 | #include <asm-ppc/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef CONFIG_HARD_I2C |
| 36 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 39 | #if defined(CONFIG_I2C_MULTI_BUS) |
| 40 | /* Initialize the bus pointer to whatever one the SPD EEPROM is on. |
| 41 | * Default is bus 0. This is necessary because the DDR initialization |
| 42 | * runs from ROM, and we can't switch buses because we can't modify |
| 43 | * the global variables. |
| 44 | */ |
Trent Piepho | 5e3ab68 | 2008-11-12 17:29:48 -0800 | [diff] [blame] | 45 | #ifndef CONFIG_SYS_SPD_BUS_NUM |
| 46 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 47 | #endif |
Trent Piepho | 5e3ab68 | 2008-11-12 17:29:48 -0800 | [diff] [blame] | 48 | static unsigned int i2c_bus_num __attribute__ ((section (".data"))) = CONFIG_SYS_SPD_BUS_NUM; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 49 | #endif /* CONFIG_I2C_MULTI_BUS */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 51 | static void _i2c_bus_reset(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | { |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 53 | int i; |
| 54 | u8 dc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | |
| 56 | /* Reset status register */ |
| 57 | /* write 1 in SCMP and IRQA to clear these fields */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 58 | out_8((u8 *)IIC_STS, 0x0A); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | |
| 60 | /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 61 | out_8((u8 *)IIC_EXTSTS, 0x8F); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 63 | /* Place chip in the reset state */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 64 | out_8((u8 *)IIC_XTCNTLSS, IIC_XTCNTLSS_SRST); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 66 | /* Check if bus is free */ |
| 67 | dc = in_8((u8 *)IIC_DIRECTCNTL); |
| 68 | if (!DIRCTNL_FREE(dc)){ |
| 69 | /* Try to set bus free state */ |
| 70 | out_8((u8 *)IIC_DIRECTCNTL, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC); |
| 71 | |
| 72 | /* Wait until we regain bus control */ |
| 73 | for (i = 0; i < 100; ++i) { |
| 74 | dc = in_8((u8 *)IIC_DIRECTCNTL); |
| 75 | if (DIRCTNL_FREE(dc)) |
| 76 | break; |
| 77 | |
| 78 | /* Toggle SCL line */ |
| 79 | dc ^= IIC_DIRCNTL_SCC; |
| 80 | out_8((u8 *)IIC_DIRECTCNTL, dc); |
| 81 | udelay(10); |
| 82 | dc ^= IIC_DIRCNTL_SCC; |
| 83 | out_8((u8 *)IIC_DIRECTCNTL, dc); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 86 | |
| 87 | /* Remove reset */ |
| 88 | out_8((u8 *)IIC_XTCNTLSS, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 91 | void i2c_init(int speed, int slaveadd) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | unsigned long freqOPB; |
| 94 | int val, divisor; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 95 | int bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 98 | /* call board specific i2c bus reset routine before accessing the */ |
| 99 | /* environment, which might be in a chip on that bus. For details */ |
| 100 | /* about this problem see doc/I2C_Edge_Conditions. */ |
| 101 | i2c_init_board(); |
| 102 | #endif |
| 103 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 104 | for (bus = 0; bus < CONFIG_SYS_MAX_I2C_BUS; bus++) { |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 105 | I2C_SET_BUS(bus); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 107 | /* Handle possible failed I2C state */ |
| 108 | /* FIXME: put this into i2c_init_board()? */ |
| 109 | _i2c_bus_reset(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 111 | /* clear lo master address */ |
| 112 | out_8((u8 *)IIC_LMADR, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 114 | /* clear hi master address */ |
| 115 | out_8((u8 *)IIC_HMADR, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 117 | /* clear lo slave address */ |
| 118 | out_8((u8 *)IIC_LSADR, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 120 | /* clear hi slave address */ |
| 121 | out_8((u8 *)IIC_HSADR, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 123 | /* Clock divide Register */ |
| 124 | /* get OPB frequency */ |
Jeffrey Mann | 2ad3aba | 2007-04-12 14:15:59 +0200 | [diff] [blame] | 125 | freqOPB = get_OPB_freq(); |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 126 | /* set divisor according to freqOPB */ |
| 127 | divisor = (freqOPB - 1) / 10000000; |
| 128 | if (divisor == 0) |
| 129 | divisor = 1; |
| 130 | out_8((u8 *)IIC_CLKDIV, divisor); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 132 | /* no interrupts */ |
| 133 | out_8((u8 *)IIC_INTRMSK, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 134 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 135 | /* clear transfer count */ |
| 136 | out_8((u8 *)IIC_XFRCNT, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 138 | /* clear extended control & stat */ |
| 139 | /* write 1 in SRC SRS SWC SWS to clear these fields */ |
| 140 | out_8((u8 *)IIC_XTCNTLSS, 0xF0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 142 | /* Mode Control Register |
| 143 | Flush Slave/Master data buffer */ |
| 144 | out_8((u8 *)IIC_MDCNTL, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 146 | val = in_8((u8 *)IIC_MDCNTL); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 148 | /* Ignore General Call, slave transfers are ignored, |
| 149 | * disable interrupts, exit unknown bus state, enable hold |
| 150 | * SCL 100kHz normaly or FastMode for 400kHz and above |
| 151 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 153 | val |= IIC_MDCNTL_EUBS|IIC_MDCNTL_HSCL; |
| 154 | if (speed >= 400000) |
| 155 | val |= IIC_MDCNTL_FSM; |
| 156 | out_8((u8 *)IIC_MDCNTL, val); |
| 157 | |
| 158 | /* clear control reg */ |
| 159 | out_8((u8 *)IIC_CNTL, 0x00); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 160 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 162 | /* set to SPD bus as default bus upon powerup */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 163 | I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 167 | * This code tries to use the features of the 405GP i2c |
| 168 | * controller. It will transfer up to 4 bytes in one pass |
| 169 | * on the loop. It only does out_8((u8 *)lbz) to the buffer when it |
| 170 | * is possible to do out16(lhz) transfers. |
| 171 | * |
| 172 | * cmd_type is 0 for write 1 for read. |
| 173 | * |
| 174 | * addr_len can take any value from 0-255, it is only limited |
| 175 | * by the char, we could make it larger if needed. If it is |
| 176 | * 0 we skip the address write cycle. |
| 177 | * |
| 178 | * Typical case is a Write of an addr followd by a Read. The |
| 179 | * IBM FAQ does not cover this. On the last byte of the write |
| 180 | * we don't set the creg CHT bit, and on the first bytes of the |
| 181 | * read we set the RPST bit. |
| 182 | * |
| 183 | * It does not support address only transfers, there must be |
| 184 | * a data part. If you want to write the address yourself, put |
| 185 | * it in the data pointer. |
| 186 | * |
| 187 | * It does not support transfer to/from address 0. |
| 188 | * |
| 189 | * It does not check XFRCNT. |
| 190 | */ |
| 191 | static int i2c_transfer(unsigned char cmd_type, |
| 192 | unsigned char chip, |
| 193 | unsigned char addr[], |
| 194 | unsigned char addr_len, |
| 195 | unsigned char data[], |
| 196 | unsigned short data_len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | unsigned char* ptr; |
| 199 | int reading; |
| 200 | int tran,cnt; |
| 201 | int result; |
| 202 | int status; |
| 203 | int i; |
| 204 | uchar creg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 205 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 206 | if (data == 0 || data_len == 0) { |
| 207 | /* Don't support data transfer of no length or to address 0 */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | printf( "i2c_transfer: bad call\n" ); |
| 209 | return IIC_NOK; |
| 210 | } |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 211 | if (addr && addr_len) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 212 | ptr = addr; |
| 213 | cnt = addr_len; |
| 214 | reading = 0; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 215 | } else { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 216 | ptr = data; |
| 217 | cnt = data_len; |
| 218 | reading = cmd_type; |
| 219 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 221 | /* Clear Stop Complete Bit */ |
| 222 | out_8((u8 *)IIC_STS, IIC_STS_SCMP); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | /* Check init */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 224 | i = 10; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | do { |
| 226 | /* Get status */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 227 | status = in_8((u8 *)IIC_STS); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 228 | i--; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 229 | } while ((status & IIC_STS_PT) && (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 230 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 231 | if (status & IIC_STS_PT) { |
| 232 | result = IIC_NOK_TOUT; |
| 233 | return(result); |
| 234 | } |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 235 | /* flush the Master/Slave Databuffers */ |
| 236 | out_8((u8 *)IIC_MDCNTL, ((in_8((u8 *)IIC_MDCNTL))|IIC_MDCNTL_FMDB|IIC_MDCNTL_FSDB)); |
| 237 | /* need to wait 4 OPB clocks? code below should take that long */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 239 | /* 7-bit adressing */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 240 | out_8((u8 *)IIC_HMADR, 0); |
| 241 | out_8((u8 *)IIC_LMADR, chip); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 243 | tran = 0; |
| 244 | result = IIC_OK; |
| 245 | creg = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 246 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 247 | while (tran != cnt && (result == IIC_OK)) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | int bc,j; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 250 | /* Control register = |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 251 | * Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start, |
| 252 | * Transfer is a sequence of transfers |
| 253 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 254 | creg |= IIC_CNTL_PT; |
| 255 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 256 | bc = (cnt - tran) > 4 ? 4 : cnt - tran; |
| 257 | creg |= (bc - 1) << 4; |
| 258 | /* if the real cmd type is write continue trans */ |
| 259 | if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt)) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 260 | creg |= IIC_CNTL_CHT; |
| 261 | |
| 262 | if (reading) |
| 263 | creg |= IIC_CNTL_READ; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 264 | else |
| 265 | for(j=0; j < bc; j++) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 266 | /* Set buffer */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 267 | out_8((u8 *)IIC_MDBUF, ptr[tran+j]); |
| 268 | out_8((u8 *)IIC_CNTL, creg); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | |
| 270 | /* Transfer is in progress |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 271 | * we have to wait for upto 5 bytes of data |
| 272 | * 1 byte chip address+r/w bit then bc bytes |
| 273 | * of data. |
| 274 | * udelay(10) is 1 bit time at 100khz |
| 275 | * Doubled for slop. 20 is too small. |
| 276 | */ |
| 277 | i = 2*5*8; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 278 | do { |
| 279 | /* Get status */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 280 | status = in_8((u8 *)IIC_STS); |
| 281 | udelay(10); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 282 | i--; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 283 | } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) && (i > 0)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 285 | if (status & IIC_STS_ERR) { |
| 286 | result = IIC_NOK; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 287 | status = in_8((u8 *)IIC_EXTSTS); |
wdenk | 8bde7f7 | 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 | } |
| 300 | /* Command is reading => get buffer */ |
| 301 | if ((reading) && (result == IIC_OK)) { |
| 302 | /* Are there data in buffer */ |
| 303 | if (status & IIC_STS_MDBS) { |
| 304 | /* |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 305 | * even if we have data we have to wait 4OPB clocks |
| 306 | * for it to hit the front of the FIFO, after that |
| 307 | * we can just read. We should check XFCNT here and |
| 308 | * if the FIFO is full there is no need to wait. |
| 309 | */ |
| 310 | udelay(1); |
| 311 | for (j=0; j<bc; j++) |
| 312 | ptr[tran+j] = in_8((u8 *)IIC_MDBUF); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 313 | } else |
| 314 | result = IIC_NOK_DATA; |
| 315 | } |
| 316 | creg = 0; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 317 | tran += bc; |
| 318 | if (ptr == addr && tran == cnt) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 319 | ptr = data; |
| 320 | cnt = data_len; |
| 321 | tran = 0; |
| 322 | reading = cmd_type; |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 323 | if (reading) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 324 | creg = IIC_CNTL_RPST; |
| 325 | } |
| 326 | } |
| 327 | return (result); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 330 | int i2c_probe(uchar chip) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 331 | { |
| 332 | uchar buf[1]; |
| 333 | |
| 334 | buf[0] = 0; |
| 335 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 336 | /* |
| 337 | * What is needed is to send the chip address and verify that the |
| 338 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 339 | * drove the data line low). |
| 340 | */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 341 | return (i2c_transfer(1, chip << 1, 0,0, buf, 1) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 345 | int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 346 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 347 | uchar xaddr[4]; |
| 348 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 349 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 350 | if (alen > 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 351 | printf ("I2C read: addr len %d not supported\n", alen); |
| 352 | return 1; |
| 353 | } |
| 354 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 355 | if (alen > 0) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 356 | xaddr[0] = (addr >> 24) & 0xFF; |
| 357 | xaddr[1] = (addr >> 16) & 0xFF; |
| 358 | xaddr[2] = (addr >> 8) & 0xFF; |
| 359 | xaddr[3] = addr & 0xFF; |
| 360 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 361 | |
| 362 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 363 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 364 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 365 | * EEPROM chips that implement "address overflow" are ones |
| 366 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 367 | * address and the extra bits end up in the "chip address" |
| 368 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 369 | * four 256 byte chips. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 370 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 371 | * Note that we consider the length of the address field to |
| 372 | * still be one byte because the extra address bits are |
| 373 | * hidden in the chip address. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 374 | */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 375 | if (alen > 0) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 376 | chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 377 | #endif |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 378 | if ((ret = i2c_transfer(1, chip<<1, &xaddr[4-alen], alen, buffer, len)) != 0) { |
stroese | 2c96baa | 2004-07-02 14:37:04 +0000 | [diff] [blame] | 379 | if (gd->have_console) |
| 380 | printf( "I2c read: failed %d\n", ret); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 381 | return 1; |
| 382 | } |
| 383 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 386 | int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 388 | uchar xaddr[4]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 389 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 390 | if (alen > 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 391 | printf ("I2C write: addr len %d not supported\n", alen); |
| 392 | return 1; |
| 393 | |
| 394 | } |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 395 | |
| 396 | if (alen > 0) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 397 | xaddr[0] = (addr >> 24) & 0xFF; |
| 398 | xaddr[1] = (addr >> 16) & 0xFF; |
| 399 | xaddr[2] = (addr >> 8) & 0xFF; |
| 400 | xaddr[3] = addr & 0xFF; |
| 401 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 402 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 403 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 404 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 405 | * EEPROM chips that implement "address overflow" are ones |
| 406 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 407 | * address and the extra bits end up in the "chip address" |
| 408 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 409 | * four 256 byte chips. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 410 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 411 | * Note that we consider the length of the address field to |
| 412 | * still be one byte because the extra address bits are |
| 413 | * hidden in the chip address. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 414 | */ |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 415 | if (alen > 0) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 416 | chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 417 | #endif |
| 418 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 419 | return (i2c_transfer(0, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 422 | #if defined(CONFIG_I2C_MULTI_BUS) |
| 423 | /* |
| 424 | * Functions for multiple I2C bus handling |
| 425 | */ |
| 426 | unsigned int i2c_get_bus_num(void) |
| 427 | { |
| 428 | return i2c_bus_num; |
| 429 | } |
| 430 | |
| 431 | int i2c_set_bus_num(unsigned int bus) |
| 432 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 433 | if (bus >= CONFIG_SYS_MAX_I2C_BUS) |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 434 | return -1; |
| 435 | |
| 436 | i2c_bus_num = bus; |
| 437 | |
| 438 | return 0; |
| 439 | } |
Matthias Fuchs | ced5b90 | 2007-03-08 16:23:11 +0100 | [diff] [blame] | 440 | #endif /* CONFIG_I2C_MULTI_BUS */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 441 | #endif /* CONFIG_HARD_I2C */ |