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 | |
John Rigby | 0d21ed0 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 32 | #define I2C_TIMEOUT 1000 |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 33 | |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 34 | static int wait_for_bb(void); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 35 | static u16 wait_for_pin(void); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 36 | static void flush_fifo(void); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 37 | |
Andreas Müller | 1e96e9e | 2012-01-04 15:26:22 +0000 | [diff] [blame] | 38 | /* |
| 39 | * For SPL boot some boards need i2c before SDRAM is initialised so force |
| 40 | * variables to live in SRAM |
| 41 | */ |
| 42 | static struct i2c __attribute__((section (".data"))) *i2c_base = |
| 43 | (struct i2c *)I2C_DEFAULT_BASE; |
| 44 | static unsigned int __attribute__((section (".data"))) bus_initialized[I2C_BUS_MAX] = |
| 45 | { [0 ... (I2C_BUS_MAX-1)] = 0 }; |
| 46 | static unsigned int __attribute__((section (".data"))) current_bus = 0; |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 47 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 48 | void i2c_init(int speed, int slaveadd) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 49 | { |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 50 | int psc, fsscll, fssclh; |
| 51 | int hsscll = 0, hssclh = 0; |
| 52 | u32 scll, sclh; |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 53 | int timeout = I2C_TIMEOUT; |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 54 | |
| 55 | /* Only handle standard, fast and high speeds */ |
| 56 | if ((speed != OMAP_I2C_STANDARD) && |
| 57 | (speed != OMAP_I2C_FAST_MODE) && |
| 58 | (speed != OMAP_I2C_HIGH_SPEED)) { |
| 59 | printf("Error : I2C unsupported speed %d\n", speed); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK; |
| 64 | psc -= 1; |
| 65 | if (psc < I2C_PSC_MIN) { |
| 66 | printf("Error : I2C unsupported prescalar %d\n", psc); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (speed == OMAP_I2C_HIGH_SPEED) { |
| 71 | /* High speed */ |
| 72 | |
| 73 | /* For first phase of HS mode */ |
| 74 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / |
| 75 | (2 * OMAP_I2C_FAST_MODE); |
| 76 | |
| 77 | fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM; |
| 78 | fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM; |
| 79 | if (((fsscll < 0) || (fssclh < 0)) || |
| 80 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 81 | puts("Error : I2C initializing first phase clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | /* For second phase of HS mode */ |
| 86 | hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 87 | |
| 88 | hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM; |
| 89 | hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM; |
| 90 | if (((fsscll < 0) || (fssclh < 0)) || |
| 91 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 92 | puts("Error : I2C initializing second phase clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll; |
| 97 | sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh; |
| 98 | |
| 99 | } else { |
| 100 | /* Standard and fast speed */ |
| 101 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 102 | |
| 103 | fsscll -= I2C_FASTSPEED_SCLL_TRIM; |
| 104 | fssclh -= I2C_FASTSPEED_SCLH_TRIM; |
| 105 | if (((fsscll < 0) || (fssclh < 0)) || |
| 106 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 107 | puts("Error : I2C initializing clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | scll = (unsigned int)fsscll; |
| 112 | sclh = (unsigned int)fssclh; |
| 113 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 114 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 115 | if (readw(&i2c_base->con) & I2C_CON_EN) { |
| 116 | writew(0, &i2c_base->con); |
| 117 | udelay(50000); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 120 | writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */ |
| 121 | udelay(1000); |
| 122 | |
| 123 | writew(I2C_CON_EN, &i2c_base->con); |
| 124 | while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) { |
| 125 | if (timeout <= 0) { |
| 126 | puts("ERROR: Timeout in soft-reset\n"); |
| 127 | return; |
| 128 | } |
| 129 | udelay(1000); |
| 130 | } |
| 131 | |
| 132 | writew(0, &i2c_base->con); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 133 | writew(psc, &i2c_base->psc); |
| 134 | writew(scll, &i2c_base->scll); |
| 135 | writew(sclh, &i2c_base->sclh); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 136 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 137 | /* own address */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 138 | writew(slaveadd, &i2c_base->oa); |
| 139 | writew(I2C_CON_EN, &i2c_base->con); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 140 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 141 | /* have to enable intrrupts or OMAP i2c module doesn't work */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 142 | 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] | 143 | I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 144 | udelay(1000); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 145 | flush_fifo(); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 146 | writew(0xFFFF, &i2c_base->stat); |
| 147 | writew(0, &i2c_base->cnt); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 148 | |
| 149 | if (gd->flags & GD_FLG_RELOC) |
| 150 | bus_initialized[current_bus] = 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 153 | static int i2c_read_byte(u8 devaddr, u16 regoffset, u8 alen, u8 *value) |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 154 | { |
| 155 | int i2c_error = 0; |
| 156 | u16 status; |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 157 | int i = 2 - alen; |
| 158 | u8 tmpbuf[2] = {(regoffset) >> 8, regoffset & 0xff}; |
| 159 | u16 w; |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 160 | |
| 161 | /* wait until bus not busy */ |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 162 | if (wait_for_bb()) |
| 163 | return 1; |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 164 | |
| 165 | /* one byte only */ |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 166 | writew(alen, &i2c_base->cnt); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 167 | /* set slave address */ |
| 168 | writew(devaddr, &i2c_base->sa); |
| 169 | /* no stop bit needed here */ |
| 170 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | |
| 171 | I2C_CON_TRX, &i2c_base->con); |
| 172 | |
| 173 | /* send register offset */ |
| 174 | while (1) { |
| 175 | status = wait_for_pin(); |
| 176 | if (status == 0 || status & I2C_STAT_NACK) { |
| 177 | i2c_error = 1; |
| 178 | goto read_exit; |
| 179 | } |
| 180 | if (status & I2C_STAT_XRDY) { |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 181 | w = tmpbuf[i++]; |
| 182 | #if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Vincent Stehlé | 17b4852 | 2012-12-03 06:07:21 +0000 | [diff] [blame] | 183 | defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX) || \ |
| 184 | defined(CONFIG_OMAP54XX)) |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 185 | w |= tmpbuf[i++] << 8; |
| 186 | #endif |
| 187 | writew(w, &i2c_base->data); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 188 | writew(I2C_STAT_XRDY, &i2c_base->stat); |
| 189 | } |
| 190 | if (status & I2C_STAT_ARDY) { |
| 191 | writew(I2C_STAT_ARDY, &i2c_base->stat); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* set slave address */ |
| 197 | writew(devaddr, &i2c_base->sa); |
| 198 | /* read one byte from slave */ |
| 199 | writew(1, &i2c_base->cnt); |
| 200 | /* need stop bit here */ |
| 201 | writew(I2C_CON_EN | I2C_CON_MST | |
| 202 | I2C_CON_STT | I2C_CON_STP, |
| 203 | &i2c_base->con); |
| 204 | |
| 205 | /* receive data */ |
| 206 | while (1) { |
| 207 | status = wait_for_pin(); |
| 208 | if (status == 0 || status & I2C_STAT_NACK) { |
| 209 | i2c_error = 1; |
| 210 | goto read_exit; |
| 211 | } |
| 212 | if (status & I2C_STAT_RRDY) { |
| 213 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Vincent Stehlé | 17b4852 | 2012-12-03 06:07:21 +0000 | [diff] [blame] | 214 | defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX) || \ |
| 215 | defined(CONFIG_OMAP54XX) |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 216 | *value = readb(&i2c_base->data); |
| 217 | #else |
| 218 | *value = readw(&i2c_base->data); |
| 219 | #endif |
| 220 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
| 221 | } |
| 222 | if (status & I2C_STAT_ARDY) { |
| 223 | writew(I2C_STAT_ARDY, &i2c_base->stat); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | read_exit: |
| 229 | flush_fifo(); |
| 230 | writew(0xFFFF, &i2c_base->stat); |
| 231 | writew(0, &i2c_base->cnt); |
| 232 | return i2c_error; |
| 233 | } |
| 234 | |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 235 | static void flush_fifo(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 236 | { u16 stat; |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 237 | |
| 238 | /* note: if you try and read data when its not there or ready |
| 239 | * you get a bus error |
| 240 | */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 241 | while (1) { |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 242 | stat = readw(&i2c_base->stat); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 243 | if (stat == I2C_STAT_RRDY) { |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 244 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Vincent Stehlé | 17b4852 | 2012-12-03 06:07:21 +0000 | [diff] [blame] | 245 | defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX) || \ |
| 246 | defined(CONFIG_OMAP54XX) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 247 | readb(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 248 | #else |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 249 | readw(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 250 | #endif |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 251 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 252 | udelay(1000); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 253 | } else |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 258 | int i2c_probe(uchar chip) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 259 | { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 260 | u16 status; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 261 | int res = 1; /* default = fail */ |
| 262 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 263 | if (chip == readw(&i2c_base->oa)) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 264 | return res; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 265 | |
| 266 | /* wait until bus not busy */ |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 267 | if (wait_for_bb()) |
| 268 | return res; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 269 | |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 270 | /* try to read one byte */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 271 | writew(1, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 272 | /* set slave address */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 273 | writew(chip, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 274 | /* stop bit needed here */ |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 275 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con); |
Nick Thompson | 48f7ae4 | 2011-04-11 22:37:41 +0000 | [diff] [blame] | 276 | |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 277 | while (1) { |
| 278 | status = wait_for_pin(); |
| 279 | if (status == 0 || status & I2C_STAT_AL) { |
| 280 | res = 1; |
| 281 | goto probe_exit; |
| 282 | } |
| 283 | if (status & I2C_STAT_NACK) { |
| 284 | res = 1; |
| 285 | writew(0xff, &i2c_base->stat); |
| 286 | writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con); |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 287 | |
| 288 | if (wait_for_bb()) |
| 289 | res = 1; |
| 290 | |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | if (status & I2C_STAT_ARDY) { |
| 294 | writew(I2C_STAT_ARDY, &i2c_base->stat); |
| 295 | break; |
| 296 | } |
| 297 | if (status & I2C_STAT_RRDY) { |
| 298 | res = 0; |
| 299 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Vincent Stehlé | 17b4852 | 2012-12-03 06:07:21 +0000 | [diff] [blame] | 300 | defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX) || \ |
| 301 | defined(CONFIG_OMAP54XX) |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 302 | readb(&i2c_base->data); |
| 303 | #else |
| 304 | readw(&i2c_base->data); |
| 305 | #endif |
| 306 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
| 307 | } |
| 308 | } |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 309 | |
Tom Rini | 27eed8b | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 310 | probe_exit: |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 311 | flush_fifo(); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 312 | /* don't allow any more data in... we don't want it. */ |
| 313 | writew(0, &i2c_base->cnt); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 314 | writew(0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 315 | return res; |
| 316 | } |
| 317 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 318 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 319 | { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 320 | int i; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 321 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 322 | if (alen > 2) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 323 | printf("I2C read: addr len %d not supported\n", alen); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 324 | return 1; |
| 325 | } |
| 326 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 327 | if (addr + len > (1 << 16)) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 328 | puts("I2C read: address out of range\n"); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 329 | return 1; |
| 330 | } |
| 331 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 332 | for (i = 0; i < len; i++) { |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 333 | if (i2c_read_byte(chip, addr + i, alen, &buffer[i])) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 334 | puts("I2C read: I/O error\n"); |
| 335 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 336 | return 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 343 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 344 | { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 345 | int i; |
| 346 | u16 status; |
| 347 | int i2c_error = 0; |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 348 | u16 w; |
| 349 | u8 tmpbuf[2] = {addr >> 8, addr & 0xff}; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 350 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 351 | if (alen > 2) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 352 | printf("I2C write: addr len %d not supported\n", alen); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 353 | return 1; |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 354 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 355 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 356 | if (addr + len > (1 << 16)) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 357 | printf("I2C write: address 0x%x + 0x%x out of range\n", |
| 358 | addr, len); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 362 | /* wait until bus not busy */ |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 363 | if (wait_for_bb()) |
| 364 | return 1; |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 365 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 366 | /* start address phase - will write regoffset + len bytes data */ |
| 367 | /* TODO consider case when !CONFIG_OMAP243X/34XX/44XX */ |
| 368 | writew(alen + len, &i2c_base->cnt); |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 369 | /* set slave address */ |
| 370 | writew(chip, &i2c_base->sa); |
| 371 | /* stop bit needed here */ |
| 372 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
| 373 | I2C_CON_STP, &i2c_base->con); |
| 374 | |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 375 | /* Send address and data */ |
| 376 | for (i = -alen; i < len; i++) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 377 | status = wait_for_pin(); |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame] | 378 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 379 | if (status == 0 || status & I2C_STAT_NACK) { |
| 380 | i2c_error = 1; |
| 381 | printf("i2c error waiting for data ACK (status=0x%x)\n", |
| 382 | status); |
| 383 | goto write_exit; |
| 384 | } |
| 385 | |
| 386 | if (status & I2C_STAT_XRDY) { |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 387 | w = (i < 0) ? tmpbuf[2+i] : buffer[i]; |
| 388 | #if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Vincent Stehlé | 17b4852 | 2012-12-03 06:07:21 +0000 | [diff] [blame] | 389 | defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX) || \ |
| 390 | defined(CONFIG_OMAP54XX)) |
Ilya Yanok | be6c2e4 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 391 | w |= ((++i < 0) ? tmpbuf[2+i] : buffer[i]) << 8; |
| 392 | #endif |
| 393 | writew(w, &i2c_base->data); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 394 | writew(I2C_STAT_XRDY, &i2c_base->stat); |
| 395 | } else { |
| 396 | i2c_error = 1; |
| 397 | printf("i2c bus not ready for Tx (i=%d)\n", i); |
| 398 | goto write_exit; |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 402 | write_exit: |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 403 | flush_fifo(); |
| 404 | writew(0xFFFF, &i2c_base->stat); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 405 | return i2c_error; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 408 | static int wait_for_bb(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 409 | { |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 410 | int timeout = I2C_TIMEOUT; |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 411 | u16 stat; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 412 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 413 | writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 414 | while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) { |
| 415 | writew(stat, &i2c_base->stat); |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 416 | udelay(1000); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (timeout <= 0) { |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 420 | printf("timed out in wait_for_bb: I2C_STAT=%x\n", |
| 421 | readw(&i2c_base->stat)); |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 422 | return 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 423 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 424 | writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/ |
Vincent Stehlé | 33205e3 | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 425 | return 0; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 428 | static u16 wait_for_pin(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 429 | { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 430 | u16 status; |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 431 | int timeout = I2C_TIMEOUT; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 432 | |
| 433 | do { |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 434 | udelay(1000); |
| 435 | status = readw(&i2c_base->stat); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 436 | } while (!(status & |
| 437 | (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY | |
| 438 | I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK | |
| 439 | I2C_STAT_AL)) && timeout--); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 440 | |
| 441 | if (timeout <= 0) { |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 442 | printf("timed out in wait_for_pin: I2C_STAT=%x\n", |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 443 | readw(&i2c_base->stat)); |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 444 | writew(0xFFFF, &i2c_base->stat); |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 445 | status = 0; |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 446 | } |
Tom Rini | 49fbf67 | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 447 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 448 | return status; |
| 449 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 450 | |
| 451 | int i2c_set_bus_num(unsigned int bus) |
| 452 | { |
| 453 | if ((bus < 0) || (bus >= I2C_BUS_MAX)) { |
| 454 | printf("Bad bus: %d\n", bus); |
| 455 | return -1; |
| 456 | } |
| 457 | |
Koen Kooi | 584ff5f | 2012-08-08 00:57:35 +0000 | [diff] [blame] | 458 | #if I2C_BUS_MAX == 4 |
| 459 | if (bus == 3) |
| 460 | i2c_base = (struct i2c *)I2C_BASE4; |
| 461 | else |
| 462 | if (bus == 2) |
| 463 | i2c_base = (struct i2c *)I2C_BASE3; |
| 464 | else |
| 465 | #endif |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 466 | #if I2C_BUS_MAX == 3 |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 467 | if (bus == 2) |
| 468 | i2c_base = (struct i2c *)I2C_BASE3; |
| 469 | else |
| 470 | #endif |
| 471 | if (bus == 1) |
| 472 | i2c_base = (struct i2c *)I2C_BASE2; |
| 473 | else |
| 474 | i2c_base = (struct i2c *)I2C_BASE1; |
| 475 | |
| 476 | current_bus = bus; |
| 477 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 478 | if (!bus_initialized[current_bus]) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 479 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 480 | |
| 481 | return 0; |
| 482 | } |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 483 | |
| 484 | int i2c_get_bus_num(void) |
| 485 | { |
| 486 | return (int) current_bus; |
| 487 | } |