wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Basic I2C functions |
| 3 | * |
| 4 | * Copyright (c) 2004 Texas Instruments |
| 5 | * |
| 6 | * This package is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the license found in the file |
| 8 | * named COPYING that should have accompanied this file. |
| 9 | * |
| 10 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 11 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 12 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Author: Jian Zhang jzhang@ti.com, Texas Instruments |
| 15 | * |
| 16 | * Copyright (c) 2003 Wolfgang Denk, wd@denx.de |
| 17 | * Rewritten to fit into the current U-Boot framework |
| 18 | * |
| 19 | * Adapted for OMAP2420 I2C, r-woodruff2@ti.com |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
wdenk | cb99da5 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 24 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 25 | #include <asm/arch/i2c.h> |
| 26 | #include <asm/io.h> |
| 27 | |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 28 | #include "omap24xx_i2c.h" |
| 29 | |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 30 | #define I2C_TIMEOUT 10 |
| 31 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 32 | static void wait_for_bb (void); |
| 33 | static u16 wait_for_pin (void); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 34 | static void flush_fifo(void); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 35 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 36 | static struct i2c *i2c_base = (struct i2c *)I2C_DEFAULT_BASE; |
| 37 | |
| 38 | static unsigned int bus_initialized[I2C_BUS_MAX]; |
| 39 | static unsigned int current_bus; |
| 40 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 41 | void i2c_init (int speed, int slaveadd) |
| 42 | { |
Heiko Schocher | 2afe314 | 2010-09-17 13:10:37 +0200 | [diff] [blame] | 43 | DECLARE_GLOBAL_DATA_PTR; |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 44 | int psc, fsscll, fssclh; |
| 45 | int hsscll = 0, hssclh = 0; |
| 46 | u32 scll, sclh; |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 47 | int timeout = I2C_TIMEOUT; |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 48 | |
| 49 | /* Only handle standard, fast and high speeds */ |
| 50 | if ((speed != OMAP_I2C_STANDARD) && |
| 51 | (speed != OMAP_I2C_FAST_MODE) && |
| 52 | (speed != OMAP_I2C_HIGH_SPEED)) { |
| 53 | printf("Error : I2C unsupported speed %d\n", speed); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK; |
| 58 | psc -= 1; |
| 59 | if (psc < I2C_PSC_MIN) { |
| 60 | printf("Error : I2C unsupported prescalar %d\n", psc); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (speed == OMAP_I2C_HIGH_SPEED) { |
| 65 | /* High speed */ |
| 66 | |
| 67 | /* For first phase of HS mode */ |
| 68 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / |
| 69 | (2 * OMAP_I2C_FAST_MODE); |
| 70 | |
| 71 | fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM; |
| 72 | fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM; |
| 73 | if (((fsscll < 0) || (fssclh < 0)) || |
| 74 | ((fsscll > 255) || (fssclh > 255))) { |
| 75 | printf("Error : I2C initializing first phase clock\n"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | /* For second phase of HS mode */ |
| 80 | hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 81 | |
| 82 | hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM; |
| 83 | hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM; |
| 84 | if (((fsscll < 0) || (fssclh < 0)) || |
| 85 | ((fsscll > 255) || (fssclh > 255))) { |
| 86 | printf("Error : I2C initializing second phase clock\n"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll; |
| 91 | sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh; |
| 92 | |
| 93 | } else { |
| 94 | /* Standard and fast speed */ |
| 95 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 96 | |
| 97 | fsscll -= I2C_FASTSPEED_SCLL_TRIM; |
| 98 | fssclh -= I2C_FASTSPEED_SCLH_TRIM; |
| 99 | if (((fsscll < 0) || (fssclh < 0)) || |
| 100 | ((fsscll > 255) || (fssclh > 255))) { |
| 101 | printf("Error : I2C initializing clock\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | scll = (unsigned int)fsscll; |
| 106 | sclh = (unsigned int)fssclh; |
| 107 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 108 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 109 | if (readw (&i2c_base->con) & I2C_CON_EN) { |
| 110 | writew (0, &i2c_base->con); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 111 | udelay (50000); |
| 112 | } |
| 113 | |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 114 | writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */ |
| 115 | udelay(1000); |
| 116 | |
| 117 | writew(I2C_CON_EN, &i2c_base->con); |
| 118 | while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) { |
| 119 | if (timeout <= 0) { |
| 120 | printf("ERROR: Timeout in soft-reset\n"); |
| 121 | return; |
| 122 | } |
| 123 | udelay(1000); |
| 124 | } |
| 125 | |
| 126 | writew(0, &i2c_base->con); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 127 | writew(psc, &i2c_base->psc); |
| 128 | writew(scll, &i2c_base->scll); |
| 129 | writew(sclh, &i2c_base->sclh); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 130 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 131 | /* own address */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 132 | writew (slaveadd, &i2c_base->oa); |
| 133 | writew (I2C_CON_EN, &i2c_base->con); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 134 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 135 | /* have to enable intrrupts or OMAP i2c module doesn't work */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 136 | writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 137 | I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 138 | udelay (1000); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 139 | flush_fifo(); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 140 | writew (0xFFFF, &i2c_base->stat); |
| 141 | writew (0, &i2c_base->cnt); |
| 142 | |
Heiko Schocher | 2afe314 | 2010-09-17 13:10:37 +0200 | [diff] [blame] | 143 | if (gd->flags & GD_FLG_RELOC) |
| 144 | bus_initialized[current_bus] = 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value) |
| 148 | { |
| 149 | int i2c_error = 0; |
| 150 | u16 status; |
| 151 | |
| 152 | /* wait until bus not busy */ |
| 153 | wait_for_bb (); |
| 154 | |
| 155 | /* one byte only */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 156 | writew (1, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 157 | /* set slave address */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 158 | writew (devaddr, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 159 | /* no stop bit needed here */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 160 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, &i2c_base->con); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 161 | |
| 162 | status = wait_for_pin (); |
| 163 | |
| 164 | if (status & I2C_STAT_XRDY) { |
| 165 | /* Important: have to use byte access */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 166 | writeb (regoffset, &i2c_base->data); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 167 | udelay (20000); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 168 | if (readw (&i2c_base->stat) & I2C_STAT_NACK) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 169 | i2c_error = 1; |
| 170 | } |
| 171 | } else { |
| 172 | i2c_error = 1; |
| 173 | } |
| 174 | |
| 175 | if (!i2c_error) { |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 176 | writew (I2C_CON_EN, &i2c_base->con); |
| 177 | while (readw(&i2c_base->stat) & |
| 178 | (I2C_STAT_XRDY | I2C_STAT_ARDY)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 179 | udelay (10000); |
| 180 | /* Have to clear pending interrupt to clear I2C_STAT */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 181 | writew (0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 184 | /* set slave address */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 185 | writew (devaddr, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 186 | /* read one byte from slave */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 187 | writew (1, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 188 | /* need stop bit here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 189 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 190 | &i2c_base->con); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 191 | |
| 192 | status = wait_for_pin (); |
| 193 | if (status & I2C_STAT_RRDY) { |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 194 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
| 195 | defined(CONFIG_OMAP44XX) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 196 | *value = readb (&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 197 | #else |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 198 | *value = readw (&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 199 | #endif |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 200 | udelay (20000); |
| 201 | } else { |
| 202 | i2c_error = 1; |
| 203 | } |
| 204 | |
| 205 | if (!i2c_error) { |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 206 | writew (I2C_CON_EN, &i2c_base->con); |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 207 | while (readw (&i2c_base->stat) & |
| 208 | (I2C_STAT_RRDY | I2C_STAT_ARDY)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 209 | udelay (10000); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 210 | writew (0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | flush_fifo(); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 215 | writew (0xFFFF, &i2c_base->stat); |
| 216 | writew (0, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 217 | return i2c_error; |
| 218 | } |
| 219 | |
| 220 | static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value) |
| 221 | { |
| 222 | int i2c_error = 0; |
| 223 | u16 status, stat; |
| 224 | |
| 225 | /* wait until bus not busy */ |
| 226 | wait_for_bb (); |
| 227 | |
| 228 | /* two bytes */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 229 | writew (2, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 230 | /* set slave address */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 231 | writew (devaddr, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 232 | /* stop bit needed here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 233 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 234 | I2C_CON_STP, &i2c_base->con); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 235 | |
| 236 | /* wait until state change */ |
| 237 | status = wait_for_pin (); |
| 238 | |
| 239 | if (status & I2C_STAT_XRDY) { |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 240 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
| 241 | defined(CONFIG_OMAP44XX) |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 242 | /* send out 1 byte */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 243 | writeb (regoffset, &i2c_base->data); |
| 244 | writew (I2C_STAT_XRDY, &i2c_base->stat); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 245 | |
| 246 | status = wait_for_pin (); |
| 247 | if ((status & I2C_STAT_XRDY)) { |
| 248 | /* send out next 1 byte */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 249 | writeb (value, &i2c_base->data); |
| 250 | writew (I2C_STAT_XRDY, &i2c_base->stat); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 251 | } else { |
| 252 | i2c_error = 1; |
| 253 | } |
| 254 | #else |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 255 | /* send out two bytes */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 256 | writew ((value << 8) + regoffset, &i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 257 | #endif |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 258 | /* must have enough delay to allow BB bit to go low */ |
| 259 | udelay (50000); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 260 | if (readw (&i2c_base->stat) & I2C_STAT_NACK) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 261 | i2c_error = 1; |
| 262 | } |
| 263 | } else { |
| 264 | i2c_error = 1; |
| 265 | } |
| 266 | |
| 267 | if (!i2c_error) { |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 268 | int eout = 200; |
| 269 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 270 | writew (I2C_CON_EN, &i2c_base->con); |
| 271 | while ((stat = readw (&i2c_base->stat)) || (readw (&i2c_base->con) & I2C_CON_MST)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 272 | udelay (1000); |
| 273 | /* have to read to clear intrrupt */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 274 | writew (0xFFFF, &i2c_base->stat); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 275 | if(--eout == 0) /* better leave with error than hang */ |
| 276 | break; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | flush_fifo(); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 280 | writew (0xFFFF, &i2c_base->stat); |
| 281 | writew (0, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 282 | return i2c_error; |
| 283 | } |
| 284 | |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 285 | static void flush_fifo(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 286 | { u16 stat; |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 287 | |
| 288 | /* note: if you try and read data when its not there or ready |
| 289 | * you get a bus error |
| 290 | */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 291 | while(1){ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 292 | stat = readw(&i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 293 | if(stat == I2C_STAT_RRDY){ |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 294 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
| 295 | defined(CONFIG_OMAP44XX) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 296 | readb(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 297 | #else |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 298 | readw(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 299 | #endif |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 300 | writew(I2C_STAT_RRDY,&i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 301 | udelay(1000); |
| 302 | }else |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | int i2c_probe (uchar chip) |
| 308 | { |
| 309 | int res = 1; /* default = fail */ |
| 310 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 311 | if (chip == readw (&i2c_base->oa)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 312 | return res; |
| 313 | } |
| 314 | |
| 315 | /* wait until bus not busy */ |
| 316 | wait_for_bb (); |
| 317 | |
| 318 | /* try to read one byte */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 319 | writew (1, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 320 | /* set slave address */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 321 | writew (chip, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 322 | /* stop bit needed here */ |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 323 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 324 | /* enough delay for the NACK bit set */ |
| 325 | udelay (50000); |
| 326 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 327 | if (!(readw (&i2c_base->stat) & I2C_STAT_NACK)) { |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 328 | res = 0; /* success case */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 329 | flush_fifo(); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 330 | writew(0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 331 | } else { |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 332 | writew(0xFFFF, &i2c_base->stat); /* failue, clear sources*/ |
| 333 | writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con); /* finish up xfer */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 334 | udelay(20000); |
| 335 | wait_for_bb (); |
| 336 | } |
| 337 | flush_fifo(); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 338 | writew (0, &i2c_base->cnt); /* don't allow any more data in...we don't want it.*/ |
| 339 | writew(0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 340 | return res; |
| 341 | } |
| 342 | |
| 343 | int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | if (alen > 1) { |
| 348 | printf ("I2C read: addr len %d not supported\n", alen); |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | if (addr + len > 256) { |
| 353 | printf ("I2C read: address out of range\n"); |
| 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | for (i = 0; i < len; i++) { |
| 358 | if (i2c_read_byte (chip, addr + i, &buffer[i])) { |
| 359 | printf ("I2C read: I/O error\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 360 | i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 361 | return 1; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len) |
| 369 | { |
| 370 | int i; |
| 371 | |
| 372 | if (alen > 1) { |
| 373 | printf ("I2C read: addr len %d not supported\n", alen); |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | if (addr + len > 256) { |
| 378 | printf ("I2C read: address out of range\n"); |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | for (i = 0; i < len; i++) { |
| 383 | if (i2c_write_byte (chip, addr + i, buffer[i])) { |
| 384 | printf ("I2C read: I/O error\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 385 | i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static void wait_for_bb (void) |
| 394 | { |
| 395 | int timeout = 10; |
| 396 | u16 stat; |
| 397 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 398 | writew(0xFFFF, &i2c_base->stat); /* clear current interruts...*/ |
| 399 | while ((stat = readw (&i2c_base->stat) & I2C_STAT_BB) && timeout--) { |
| 400 | writew (stat, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 401 | udelay (50000); |
| 402 | } |
| 403 | |
| 404 | if (timeout <= 0) { |
| 405 | printf ("timed out in wait_for_bb: I2C_STAT=%x\n", |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 406 | readw (&i2c_base->stat)); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 407 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 408 | writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static u16 wait_for_pin (void) |
| 412 | { |
| 413 | u16 status; |
| 414 | int timeout = 10; |
| 415 | |
| 416 | do { |
| 417 | udelay (1000); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 418 | status = readw (&i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 419 | } while ( !(status & |
| 420 | (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY | |
| 421 | I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK | |
| 422 | I2C_STAT_AL)) && timeout--); |
| 423 | |
| 424 | if (timeout <= 0) { |
| 425 | printf ("timed out in wait_for_pin: I2C_STAT=%x\n", |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 426 | readw (&i2c_base->stat)); |
| 427 | writew(0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 428 | } |
| 429 | return status; |
| 430 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 431 | |
| 432 | int i2c_set_bus_num(unsigned int bus) |
| 433 | { |
| 434 | if ((bus < 0) || (bus >= I2C_BUS_MAX)) { |
| 435 | printf("Bad bus: %d\n", bus); |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | #if I2C_BUS_MAX==3 |
| 440 | if (bus == 2) |
| 441 | i2c_base = (struct i2c *)I2C_BASE3; |
| 442 | else |
| 443 | #endif |
| 444 | if (bus == 1) |
| 445 | i2c_base = (struct i2c *)I2C_BASE2; |
| 446 | else |
| 447 | i2c_base = (struct i2c *)I2C_BASE1; |
| 448 | |
| 449 | current_bus = bus; |
| 450 | |
| 451 | if(!bus_initialized[current_bus]) |
| 452 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 453 | |
| 454 | return 0; |
| 455 | } |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 456 | |
| 457 | int i2c_get_bus_num(void) |
| 458 | { |
| 459 | return (int) current_bus; |
| 460 | } |