wdenk | 8ed9604 | 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 | * |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 21 | * Copyright (c) 2013 Lubomir Popov <lpopov@mm-sol.com>, MM Solutions |
| 22 | * New i2c_read, i2c_write and i2c_probe functions, tested on OMAP4 |
| 23 | * (4430/60/70), OMAP5 (5430) and AM335X (3359); should work on older |
| 24 | * OMAPs and derivatives as well. The only anticipated exception would |
| 25 | * be the OMAP2420, which shall require driver modification. |
| 26 | * - Rewritten i2c_read to operate correctly with all types of chips |
| 27 | * (old function could not read consistent data from some I2C slaves). |
| 28 | * - Optimized i2c_write. |
| 29 | * - New i2c_probe, performs write access vs read. The old probe could |
| 30 | * hang the system under certain conditions (e.g. unconfigured pads). |
| 31 | * - The read/write/probe functions try to identify unconfigured bus. |
| 32 | * - Status functions now read irqstatus_raw as per TRM guidelines |
| 33 | * (except for OMAP243X and OMAP34XX). |
| 34 | * - Driver now supports up to I2C5 (OMAP5). |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 35 | * |
Hannes Schmelzer | 4c302b9 | 2015-05-28 15:41:12 +0200 | [diff] [blame] | 36 | * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 37 | * - Added support for set_speed |
| 38 | * |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <common.h> |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 42 | #include <i2c.h> |
wdenk | 289f932 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 43 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 44 | #include <asm/arch/i2c.h> |
| 45 | #include <asm/io.h> |
| 46 | |
Steve Sakoman | 938717c | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 47 | #include "omap24xx_i2c.h" |
| 48 | |
John Rigby | 2956532 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
| 50 | |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 51 | #define I2C_TIMEOUT 1000 |
Steve Sakoman | d708395 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 52 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 53 | /* Absolutely safe for status update at 100 kHz I2C: */ |
| 54 | #define I2C_WAIT 200 |
| 55 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 56 | static int wait_for_bb(struct i2c_adapter *adap); |
| 57 | static struct i2c *omap24_get_base(struct i2c_adapter *adap); |
| 58 | static u16 wait_for_event(struct i2c_adapter *adap); |
| 59 | static void flush_fifo(struct i2c_adapter *adap); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 60 | static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed) |
| 61 | { |
| 62 | unsigned int sampleclk, prescaler; |
| 63 | int fsscll, fssclh; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 64 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 65 | speed <<= 1; |
| 66 | prescaler = 0; |
| 67 | /* |
| 68 | * some divisors may cause a precission loss, but shouldn't |
| 69 | * be a big thing, because i2c_clk is then allready very slow. |
| 70 | */ |
| 71 | while (prescaler <= 0xFF) { |
| 72 | sampleclk = I2C_IP_CLK / (prescaler+1); |
| 73 | |
| 74 | fsscll = sampleclk / speed; |
| 75 | fssclh = fsscll; |
| 76 | fsscll -= I2C_FASTSPEED_SCLL_TRIM; |
| 77 | fssclh -= I2C_FASTSPEED_SCLH_TRIM; |
| 78 | |
| 79 | if (((fsscll > 0) && (fssclh > 0)) && |
| 80 | ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) && |
| 81 | (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) { |
| 82 | if (pscl) |
| 83 | *pscl = fsscll; |
| 84 | if (psch) |
| 85 | *psch = fssclh; |
| 86 | |
| 87 | return prescaler; |
| 88 | } |
| 89 | prescaler++; |
| 90 | } |
| 91 | return -1; |
| 92 | } |
| 93 | static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 94 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 95 | struct i2c *i2c_base = omap24_get_base(adap); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 96 | int psc, fsscll = 0, fssclh = 0; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 97 | int hsscll = 0, hssclh = 0; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 98 | u32 scll = 0, sclh = 0; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 99 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 100 | if (speed >= OMAP_I2C_HIGH_SPEED) { |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 101 | /* High speed */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 102 | psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK; |
| 103 | psc -= 1; |
| 104 | if (psc < I2C_PSC_MIN) { |
| 105 | printf("Error : I2C unsupported prescaler %d\n", psc); |
| 106 | return -1; |
| 107 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 108 | |
| 109 | /* For first phase of HS mode */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 110 | fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 111 | |
| 112 | fssclh = fsscll; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 113 | |
| 114 | fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM; |
| 115 | fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM; |
| 116 | if (((fsscll < 0) || (fssclh < 0)) || |
| 117 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 118 | puts("Error : I2C initializing first phase clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 119 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* For second phase of HS mode */ |
| 123 | hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 124 | |
| 125 | hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM; |
| 126 | hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM; |
| 127 | if (((fsscll < 0) || (fssclh < 0)) || |
| 128 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 129 | puts("Error : I2C initializing second phase clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 130 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll; |
| 134 | sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh; |
| 135 | |
| 136 | } else { |
| 137 | /* Standard and fast speed */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 138 | psc = omap24_i2c_findpsc(&scll, &sclh, speed); |
| 139 | if (0 > psc) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 140 | puts("Error : I2C initializing clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 141 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 142 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 143 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 144 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 145 | adap->speed = speed; |
| 146 | adap->waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */ |
| 147 | writew(0, &i2c_base->con); |
| 148 | writew(psc, &i2c_base->psc); |
| 149 | writew(scll, &i2c_base->scll); |
| 150 | writew(sclh, &i2c_base->sclh); |
| 151 | writew(I2C_CON_EN, &i2c_base->con); |
| 152 | writew(0xFFFF, &i2c_base->stat); /* clear all pending status */ |
| 153 | |
| 154 | return 0; |
| 155 | } |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 156 | |
| 157 | static void omap24_i2c_deblock(struct i2c_adapter *adap) |
| 158 | { |
| 159 | struct i2c *i2c_base = omap24_get_base(adap); |
| 160 | int i; |
| 161 | u16 systest; |
| 162 | u16 orgsystest; |
| 163 | |
| 164 | /* set test mode ST_EN = 1 */ |
| 165 | orgsystest = readw(&i2c_base->systest); |
| 166 | systest = orgsystest; |
| 167 | /* enable testmode */ |
| 168 | systest |= I2C_SYSTEST_ST_EN; |
| 169 | writew(systest, &i2c_base->systest); |
| 170 | systest &= ~I2C_SYSTEST_TMODE_MASK; |
| 171 | systest |= 3 << I2C_SYSTEST_TMODE_SHIFT; |
| 172 | writew(systest, &i2c_base->systest); |
| 173 | |
| 174 | /* set SCL, SDA = 1 */ |
| 175 | systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O; |
| 176 | writew(systest, &i2c_base->systest); |
| 177 | udelay(10); |
| 178 | |
| 179 | /* toggle scl 9 clocks */ |
| 180 | for (i = 0; i < 9; i++) { |
| 181 | /* SCL = 0 */ |
| 182 | systest &= ~I2C_SYSTEST_SCL_O; |
| 183 | writew(systest, &i2c_base->systest); |
| 184 | udelay(10); |
| 185 | /* SCL = 1 */ |
| 186 | systest |= I2C_SYSTEST_SCL_O; |
| 187 | writew(systest, &i2c_base->systest); |
| 188 | udelay(10); |
| 189 | } |
| 190 | |
| 191 | /* send stop */ |
| 192 | systest &= ~I2C_SYSTEST_SDA_O; |
| 193 | writew(systest, &i2c_base->systest); |
| 194 | udelay(10); |
| 195 | systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O; |
| 196 | writew(systest, &i2c_base->systest); |
| 197 | udelay(10); |
| 198 | |
| 199 | /* restore original mode */ |
| 200 | writew(orgsystest, &i2c_base->systest); |
| 201 | } |
| 202 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 203 | static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 204 | { |
| 205 | struct i2c *i2c_base = omap24_get_base(adap); |
| 206 | int timeout = I2C_TIMEOUT; |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 207 | int deblock = 1; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 208 | |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 209 | retry: |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 210 | if (readw(&i2c_base->con) & I2C_CON_EN) { |
| 211 | writew(0, &i2c_base->con); |
| 212 | udelay(50000); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 215 | writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */ |
| 216 | udelay(1000); |
| 217 | |
| 218 | writew(I2C_CON_EN, &i2c_base->con); |
| 219 | while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) { |
| 220 | if (timeout <= 0) { |
| 221 | puts("ERROR: Timeout in soft-reset\n"); |
| 222 | return; |
| 223 | } |
| 224 | udelay(1000); |
| 225 | } |
| 226 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 227 | if (0 != omap24_i2c_setspeed(adap, speed)) { |
| 228 | printf("ERROR: failed to setup I2C bus-speed!\n"); |
| 229 | return; |
| 230 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 231 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 232 | /* own address */ |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 233 | writew(slaveadd, &i2c_base->oa); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 234 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 235 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 236 | /* |
| 237 | * Have to enable interrupts for OMAP2/3, these IPs don't have |
| 238 | * an 'irqstatus_raw' register and we shall have to poll 'stat' |
| 239 | */ |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 240 | writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 241 | I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie); |
| 242 | #endif |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 243 | udelay(1000); |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 244 | flush_fifo(adap); |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 245 | writew(0xFFFF, &i2c_base->stat); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 246 | |
| 247 | /* Handle possible failed I2C state */ |
| 248 | if (wait_for_bb(adap)) |
| 249 | if (deblock == 1) { |
| 250 | omap24_i2c_deblock(adap); |
| 251 | deblock = 0; |
| 252 | goto retry; |
| 253 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 256 | static void flush_fifo(struct i2c_adapter *adap) |
| 257 | { |
| 258 | struct i2c *i2c_base = omap24_get_base(adap); |
| 259 | u16 stat; |
wdenk | 082acfd | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 260 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 261 | /* |
| 262 | * note: if you try and read data when its not there or ready |
wdenk | 082acfd | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 263 | * you get a bus error |
| 264 | */ |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 265 | while (1) { |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 266 | stat = readw(&i2c_base->stat); |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 267 | if (stat == I2C_STAT_RRDY) { |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 268 | readb(&i2c_base->data); |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 269 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 270 | udelay(1000); |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 271 | } else |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 276 | /* |
| 277 | * i2c_probe: Use write access. Allows to identify addresses that are |
| 278 | * write-only (like the config register of dual-port EEPROMs) |
| 279 | */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 280 | static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 281 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 282 | struct i2c *i2c_base = omap24_get_base(adap); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 283 | u16 status; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 284 | int res = 1; /* default = fail */ |
| 285 | |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 286 | if (chip == readw(&i2c_base->oa)) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 287 | return res; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 288 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 289 | /* Wait until bus is free */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 290 | if (wait_for_bb(adap)) |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 291 | return res; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 292 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 293 | /* No data transfer, slave addr only */ |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 294 | writew(chip, &i2c_base->sa); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 295 | /* Stop bit needed here */ |
| 296 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
| 297 | I2C_CON_STP, &i2c_base->con); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 298 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 299 | status = wait_for_event(adap); |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 300 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 301 | if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) { |
| 302 | /* |
| 303 | * With current high-level command implementation, notifying |
| 304 | * the user shall flood the console with 127 messages. If |
| 305 | * silent exit is desired upon unconfigured bus, remove the |
| 306 | * following 'if' section: |
| 307 | */ |
| 308 | if (status == I2C_STAT_XRDY) |
| 309 | printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n", |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 310 | adap->hwadapnr, status); |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 311 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 312 | goto pr_exit; |
Tom Rini | 168a5ac | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 313 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 314 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 315 | /* Check for ACK (!NAK) */ |
| 316 | if (!(status & I2C_STAT_NACK)) { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 317 | res = 0; /* Device found */ |
| 318 | udelay(adap->waitdelay);/* Required by AM335X in SPL */ |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 319 | /* Abort transfer (force idle state) */ |
| 320 | writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */ |
| 321 | udelay(1000); |
| 322 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX | |
| 323 | I2C_CON_STP, &i2c_base->con); /* STP */ |
| 324 | } |
| 325 | pr_exit: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 326 | flush_fifo(adap); |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 327 | writew(0xFFFF, &i2c_base->stat); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 328 | return res; |
| 329 | } |
| 330 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 331 | /* |
| 332 | * i2c_read: Function now uses a single I2C read transaction with bulk transfer |
| 333 | * of the requested number of bytes (note that the 'i2c md' command |
| 334 | * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is |
| 335 | * defined in the board config header, this transaction shall be with |
| 336 | * Repeated Start (Sr) between the address and data phases; otherwise |
| 337 | * Stop-Start (P-S) shall be used (some I2C chips do require a P-S). |
| 338 | * The address (reg offset) may be 0, 1 or 2 bytes long. |
| 339 | * Function now reads correctly from chips that return more than one |
| 340 | * byte of data per addressed register (like TI temperature sensors), |
| 341 | * or that do not need a register address at all (such as some clock |
| 342 | * distributors). |
| 343 | */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 344 | static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 345 | int alen, uchar *buffer, int len) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 346 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 347 | struct i2c *i2c_base = omap24_get_base(adap); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 348 | int i2c_error = 0; |
| 349 | u16 status; |
| 350 | |
| 351 | if (alen < 0) { |
| 352 | puts("I2C read: addr len < 0\n"); |
| 353 | return 1; |
| 354 | } |
| 355 | if (len < 0) { |
| 356 | puts("I2C read: data len < 0\n"); |
| 357 | return 1; |
| 358 | } |
| 359 | if (buffer == NULL) { |
| 360 | puts("I2C read: NULL pointer passed\n"); |
| 361 | return 1; |
| 362 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 363 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 364 | if (alen > 2) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 365 | printf("I2C read: addr len %d not supported\n", alen); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 366 | return 1; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 367 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 368 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 369 | if (addr + len > (1 << 16)) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 370 | puts("I2C read: address out of range\n"); |
| 371 | return 1; |
| 372 | } |
| 373 | |
Guy Thouret | 32b9b55 | 2016-03-11 16:23:41 +0000 | [diff] [blame] | 374 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 375 | /* |
| 376 | * EEPROM chips that implement "address overflow" are ones |
| 377 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 378 | * address and the extra bits end up in the "chip address" |
| 379 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 380 | * four 256 byte chips. |
| 381 | * |
| 382 | * Note that we consider the length of the address field to |
| 383 | * still be one byte because the extra address bits are |
| 384 | * hidden in the chip address. |
| 385 | */ |
| 386 | if (alen > 0) |
| 387 | chip |= ((addr >> (alen * 8)) & |
| 388 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 389 | #endif |
| 390 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 391 | /* Wait until bus not busy */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 392 | if (wait_for_bb(adap)) |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 393 | return 1; |
| 394 | |
| 395 | /* Zero, one or two bytes reg address (offset) */ |
| 396 | writew(alen, &i2c_base->cnt); |
| 397 | /* Set slave address */ |
| 398 | writew(chip, &i2c_base->sa); |
| 399 | |
| 400 | if (alen) { |
| 401 | /* Must write reg offset first */ |
| 402 | #ifdef CONFIG_I2C_REPEATED_START |
| 403 | /* No stop bit, use Repeated Start (Sr) */ |
| 404 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | |
| 405 | I2C_CON_TRX, &i2c_base->con); |
| 406 | #else |
| 407 | /* Stop - Start (P-S) */ |
| 408 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP | |
| 409 | I2C_CON_TRX, &i2c_base->con); |
| 410 | #endif |
| 411 | /* Send register offset */ |
| 412 | while (1) { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 413 | status = wait_for_event(adap); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 414 | /* Try to identify bus that is not padconf'd for I2C */ |
| 415 | if (status == I2C_STAT_XRDY) { |
| 416 | i2c_error = 2; |
| 417 | printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n", |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 418 | adap->hwadapnr, status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 419 | goto rd_exit; |
| 420 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 421 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 422 | i2c_error = 1; |
| 423 | printf("i2c_read: error waiting for addr ACK (status=0x%x)\n", |
| 424 | status); |
| 425 | goto rd_exit; |
| 426 | } |
| 427 | if (alen) { |
| 428 | if (status & I2C_STAT_XRDY) { |
| 429 | alen--; |
| 430 | /* Do we have to use byte access? */ |
| 431 | writeb((addr >> (8 * alen)) & 0xff, |
| 432 | &i2c_base->data); |
| 433 | writew(I2C_STAT_XRDY, &i2c_base->stat); |
| 434 | } |
| 435 | } |
| 436 | if (status & I2C_STAT_ARDY) { |
| 437 | writew(I2C_STAT_ARDY, &i2c_base->stat); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | /* Set slave address */ |
| 443 | writew(chip, &i2c_base->sa); |
| 444 | /* Read len bytes from slave */ |
| 445 | writew(len, &i2c_base->cnt); |
| 446 | /* Need stop bit here */ |
| 447 | writew(I2C_CON_EN | I2C_CON_MST | |
| 448 | I2C_CON_STT | I2C_CON_STP, |
| 449 | &i2c_base->con); |
| 450 | |
| 451 | /* Receive data */ |
| 452 | while (1) { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 453 | status = wait_for_event(adap); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Try to identify bus that is not padconf'd for I2C. This |
| 456 | * state could be left over from previous transactions if |
| 457 | * the address phase is skipped due to alen=0. |
| 458 | */ |
| 459 | if (status == I2C_STAT_XRDY) { |
| 460 | i2c_error = 2; |
| 461 | printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n", |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 462 | adap->hwadapnr, status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 463 | goto rd_exit; |
| 464 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 465 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 466 | i2c_error = 1; |
| 467 | goto rd_exit; |
| 468 | } |
| 469 | if (status & I2C_STAT_RRDY) { |
| 470 | *buffer++ = readb(&i2c_base->data); |
| 471 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
| 472 | } |
| 473 | if (status & I2C_STAT_ARDY) { |
| 474 | writew(I2C_STAT_ARDY, &i2c_base->stat); |
| 475 | break; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 479 | rd_exit: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 480 | flush_fifo(adap); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 481 | writew(0xFFFF, &i2c_base->stat); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 482 | return i2c_error; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 485 | /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 486 | static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 487 | int alen, uchar *buffer, int len) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 488 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 489 | struct i2c *i2c_base = omap24_get_base(adap); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 490 | int i; |
| 491 | u16 status; |
| 492 | int i2c_error = 0; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 493 | int timeout = I2C_TIMEOUT; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 494 | |
| 495 | if (alen < 0) { |
| 496 | puts("I2C write: addr len < 0\n"); |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | if (len < 0) { |
| 501 | puts("I2C write: data len < 0\n"); |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | if (buffer == NULL) { |
| 506 | puts("I2C write: NULL pointer passed\n"); |
| 507 | return 1; |
| 508 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 509 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 510 | if (alen > 2) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 511 | printf("I2C write: addr len %d not supported\n", alen); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 512 | return 1; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 513 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 514 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 515 | if (addr + len > (1 << 16)) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 516 | printf("I2C write: address 0x%x + 0x%x out of range\n", |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 517 | addr, len); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 518 | return 1; |
| 519 | } |
| 520 | |
Guy Thouret | 32b9b55 | 2016-03-11 16:23:41 +0000 | [diff] [blame] | 521 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 522 | /* |
| 523 | * EEPROM chips that implement "address overflow" are ones |
| 524 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 525 | * address and the extra bits end up in the "chip address" |
| 526 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 527 | * four 256 byte chips. |
| 528 | * |
| 529 | * Note that we consider the length of the address field to |
| 530 | * still be one byte because the extra address bits are |
| 531 | * hidden in the chip address. |
| 532 | */ |
| 533 | if (alen > 0) |
| 534 | chip |= ((addr >> (alen * 8)) & |
| 535 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 536 | #endif |
| 537 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 538 | /* Wait until bus not busy */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 539 | if (wait_for_bb(adap)) |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 540 | return 1; |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 541 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 542 | /* Start address phase - will write regoffset + len bytes data */ |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 543 | writew(alen + len, &i2c_base->cnt); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 544 | /* Set slave address */ |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 545 | writew(chip, &i2c_base->sa); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 546 | /* Stop bit needed here */ |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 547 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 548 | I2C_CON_STP, &i2c_base->con); |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 549 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 550 | while (alen) { |
| 551 | /* Must write reg offset (one or two bytes) */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 552 | status = wait_for_event(adap); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 553 | /* Try to identify bus that is not padconf'd for I2C */ |
| 554 | if (status == I2C_STAT_XRDY) { |
| 555 | i2c_error = 2; |
| 556 | printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n", |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 557 | adap->hwadapnr, status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 558 | goto wr_exit; |
| 559 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 560 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 561 | i2c_error = 1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 562 | printf("i2c_write: error waiting for addr ACK (status=0x%x)\n", |
| 563 | status); |
| 564 | goto wr_exit; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 565 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 566 | if (status & I2C_STAT_XRDY) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 567 | alen--; |
| 568 | writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 569 | writew(I2C_STAT_XRDY, &i2c_base->stat); |
| 570 | } else { |
| 571 | i2c_error = 1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 572 | printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n", |
| 573 | status); |
| 574 | goto wr_exit; |
| 575 | } |
| 576 | } |
| 577 | /* Address phase is over, now write data */ |
| 578 | for (i = 0; i < len; i++) { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 579 | status = wait_for_event(adap); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 580 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 581 | i2c_error = 1; |
| 582 | printf("i2c_write: error waiting for data ACK (status=0x%x)\n", |
| 583 | status); |
| 584 | goto wr_exit; |
| 585 | } |
| 586 | if (status & I2C_STAT_XRDY) { |
| 587 | writeb(buffer[i], &i2c_base->data); |
| 588 | writew(I2C_STAT_XRDY, &i2c_base->stat); |
| 589 | } else { |
| 590 | i2c_error = 1; |
| 591 | printf("i2c_write: bus not ready for data Tx (i=%d)\n", |
| 592 | i); |
| 593 | goto wr_exit; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 596 | /* |
| 597 | * poll ARDY bit for making sure that last byte really has been |
| 598 | * transferred on the bus. |
| 599 | */ |
| 600 | do { |
| 601 | status = wait_for_event(adap); |
| 602 | } while (!(status & I2C_STAT_ARDY) && timeout--); |
| 603 | if (timeout <= 0) |
| 604 | printf("i2c_write: timed out writig last byte!\n"); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 605 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 606 | wr_exit: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 607 | flush_fifo(adap); |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 608 | writew(0xFFFF, &i2c_base->stat); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 609 | return i2c_error; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 612 | /* |
| 613 | * Wait for the bus to be free by checking the Bus Busy (BB) |
| 614 | * bit to become clear |
| 615 | */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 616 | static int wait_for_bb(struct i2c_adapter *adap) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 617 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 618 | struct i2c *i2c_base = omap24_get_base(adap); |
Steve Sakoman | 73e8747 | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 619 | int timeout = I2C_TIMEOUT; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 620 | u16 stat; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 621 | |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 622 | writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/ |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 623 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 624 | while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 625 | #else |
| 626 | /* Read RAW status */ |
| 627 | while ((stat = readw(&i2c_base->irqstatus_raw) & |
| 628 | I2C_STAT_BB) && timeout--) { |
| 629 | #endif |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 630 | writew(stat, &i2c_base->stat); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 631 | udelay(adap->waitdelay); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (timeout <= 0) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 635 | printf("Timed out in wait_for_bb: status=%04x\n", |
| 636 | stat); |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 637 | return 1; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 638 | } |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 639 | writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/ |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 640 | return 0; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 643 | /* |
| 644 | * Wait for the I2C controller to complete current action |
| 645 | * and update status |
| 646 | */ |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 647 | static u16 wait_for_event(struct i2c_adapter *adap) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 648 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 649 | struct i2c *i2c_base = omap24_get_base(adap); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 650 | u16 status; |
Steve Sakoman | 73e8747 | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 651 | int timeout = I2C_TIMEOUT; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 652 | |
| 653 | do { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 654 | udelay(adap->waitdelay); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 655 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 656 | status = readw(&i2c_base->stat); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 657 | #else |
| 658 | /* Read RAW status */ |
| 659 | status = readw(&i2c_base->irqstatus_raw); |
| 660 | #endif |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 661 | } while (!(status & |
| 662 | (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY | |
| 663 | I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK | |
| 664 | I2C_STAT_AL)) && timeout--); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 665 | |
| 666 | if (timeout <= 0) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 667 | printf("Timed out in wait_for_event: status=%04x\n", |
| 668 | status); |
| 669 | /* |
| 670 | * If status is still 0 here, probably the bus pads have |
| 671 | * not been configured for I2C, and/or pull-ups are missing. |
| 672 | */ |
| 673 | printf("Check if pads/pull-ups of bus %d are properly configured\n", |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 674 | adap->hwadapnr); |
Steve Sakoman | 73e8747 | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 675 | writew(0xFFFF, &i2c_base->stat); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 676 | status = 0; |
Steve Sakoman | 73e8747 | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 677 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 678 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 679 | return status; |
| 680 | } |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 681 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 682 | static struct i2c *omap24_get_base(struct i2c_adapter *adap) |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 683 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 684 | switch (adap->hwadapnr) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 685 | case 0: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 686 | return (struct i2c *)I2C_BASE1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 687 | break; |
| 688 | case 1: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 689 | return (struct i2c *)I2C_BASE2; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 690 | break; |
| 691 | #if (I2C_BUS_MAX > 2) |
| 692 | case 2: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 693 | return (struct i2c *)I2C_BASE3; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 694 | break; |
| 695 | #if (I2C_BUS_MAX > 3) |
| 696 | case 3: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 697 | return (struct i2c *)I2C_BASE4; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 698 | break; |
| 699 | #if (I2C_BUS_MAX > 4) |
| 700 | case 4: |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 701 | return (struct i2c *)I2C_BASE5; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 702 | break; |
| 703 | #endif |
| 704 | #endif |
| 705 | #endif |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 706 | default: |
| 707 | printf("wrong hwadapnr: %d\n", adap->hwadapnr); |
| 708 | break; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 709 | } |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 710 | return NULL; |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 711 | } |
Steve Sakoman | 938717c | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 712 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 713 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1) |
| 714 | #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED |
| 715 | #endif |
| 716 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1) |
| 717 | #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 718 | #endif |
| 719 | |
| 720 | U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe, |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 721 | omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed, |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 722 | CONFIG_SYS_OMAP24_I2C_SPEED, |
| 723 | CONFIG_SYS_OMAP24_I2C_SLAVE, |
| 724 | 0) |
| 725 | U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe, |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 726 | omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed, |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 727 | CONFIG_SYS_OMAP24_I2C_SPEED1, |
| 728 | CONFIG_SYS_OMAP24_I2C_SLAVE1, |
| 729 | 1) |
| 730 | #if (I2C_BUS_MAX > 2) |
| 731 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2) |
| 732 | #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED |
| 733 | #endif |
| 734 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2) |
| 735 | #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 736 | #endif |
| 737 | |
| 738 | U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe, |
| 739 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 740 | CONFIG_SYS_OMAP24_I2C_SPEED2, |
| 741 | CONFIG_SYS_OMAP24_I2C_SLAVE2, |
| 742 | 2) |
| 743 | #if (I2C_BUS_MAX > 3) |
| 744 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3) |
| 745 | #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED |
| 746 | #endif |
| 747 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3) |
| 748 | #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 749 | #endif |
| 750 | |
| 751 | U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe, |
| 752 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 753 | CONFIG_SYS_OMAP24_I2C_SPEED3, |
| 754 | CONFIG_SYS_OMAP24_I2C_SLAVE3, |
| 755 | 3) |
| 756 | #if (I2C_BUS_MAX > 4) |
| 757 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4) |
| 758 | #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED |
| 759 | #endif |
| 760 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4) |
| 761 | #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 762 | #endif |
| 763 | |
| 764 | U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe, |
| 765 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 766 | CONFIG_SYS_OMAP24_I2C_SPEED4, |
| 767 | CONFIG_SYS_OMAP24_I2C_SLAVE4, |
| 768 | 4) |
| 769 | #endif |
| 770 | #endif |
| 771 | #endif |