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