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