wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Basic I2C functions |
| 3 | * |
| 4 | * Copyright (c) 2004 Texas Instruments |
| 5 | * |
| 6 | * This package is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the license found in the file |
| 8 | * named COPYING that should have accompanied this file. |
| 9 | * |
| 10 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 11 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 12 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Author: Jian Zhang jzhang@ti.com, Texas Instruments |
| 15 | * |
| 16 | * Copyright (c) 2003 Wolfgang Denk, wd@denx.de |
| 17 | * Rewritten to fit into the current U-Boot framework |
| 18 | * |
| 19 | * Adapted for OMAP2420 I2C, r-woodruff2@ti.com |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
wdenk | cb99da5 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 24 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 25 | #include <asm/arch/i2c.h> |
| 26 | #include <asm/io.h> |
| 27 | |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 28 | #include "omap24xx_i2c.h" |
| 29 | |
John Rigby | 0d21ed0 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 32 | #define I2C_STAT_TIMEO (1 << 31) |
| 33 | #define I2C_TIMEOUT 10 |
Steve Sakoman | e2bdc13 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 34 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 35 | static u32 wait_for_bb(void); |
| 36 | static u32 wait_for_status_mask(u16 mask); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 37 | static void flush_fifo(void); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 38 | |
Andreas Müller | 1e96e9e | 2012-01-04 15:26:22 +0000 | [diff] [blame] | 39 | /* |
| 40 | * For SPL boot some boards need i2c before SDRAM is initialised so force |
| 41 | * variables to live in SRAM |
| 42 | */ |
| 43 | static struct i2c __attribute__((section (".data"))) *i2c_base = |
| 44 | (struct i2c *)I2C_DEFAULT_BASE; |
| 45 | static unsigned int __attribute__((section (".data"))) bus_initialized[I2C_BUS_MAX] = |
| 46 | { [0 ... (I2C_BUS_MAX-1)] = 0 }; |
| 47 | static unsigned int __attribute__((section (".data"))) current_bus = 0; |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 48 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 49 | void i2c_init(int speed, int slaveadd) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 50 | { |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 51 | int psc, fsscll, fssclh; |
| 52 | int hsscll = 0, hssclh = 0; |
| 53 | u32 scll, sclh; |
| 54 | |
| 55 | /* Only handle standard, fast and high speeds */ |
| 56 | if ((speed != OMAP_I2C_STANDARD) && |
| 57 | (speed != OMAP_I2C_FAST_MODE) && |
| 58 | (speed != OMAP_I2C_HIGH_SPEED)) { |
| 59 | printf("Error : I2C unsupported speed %d\n", speed); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK; |
| 64 | psc -= 1; |
| 65 | if (psc < I2C_PSC_MIN) { |
| 66 | printf("Error : I2C unsupported prescalar %d\n", psc); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (speed == OMAP_I2C_HIGH_SPEED) { |
| 71 | /* High speed */ |
| 72 | |
| 73 | /* For first phase of HS mode */ |
| 74 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / |
| 75 | (2 * OMAP_I2C_FAST_MODE); |
| 76 | |
| 77 | fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM; |
| 78 | fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM; |
| 79 | if (((fsscll < 0) || (fssclh < 0)) || |
| 80 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 81 | puts("Error : I2C initializing first phase clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | /* For second phase of HS mode */ |
| 86 | hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 87 | |
| 88 | hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM; |
| 89 | hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM; |
| 90 | if (((fsscll < 0) || (fssclh < 0)) || |
| 91 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 92 | puts("Error : I2C initializing second phase clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll; |
| 97 | sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh; |
| 98 | |
| 99 | } else { |
| 100 | /* Standard and fast speed */ |
| 101 | fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 102 | |
| 103 | fsscll -= I2C_FASTSPEED_SCLL_TRIM; |
| 104 | fssclh -= I2C_FASTSPEED_SCLH_TRIM; |
| 105 | if (((fsscll < 0) || (fssclh < 0)) || |
| 106 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | a30293f | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 107 | puts("Error : I2C initializing clock\n"); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | scll = (unsigned int)fsscll; |
| 112 | sclh = (unsigned int)fssclh; |
| 113 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 114 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 115 | if (gd->flags & GD_FLG_RELOC) |
| 116 | bus_initialized[current_bus] = 1; |
| 117 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 118 | if (readw(&i2c_base->con) & I2C_CON_EN) { |
| 119 | writew(0, &i2c_base->con); |
| 120 | udelay(50000); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 123 | writew(psc, &i2c_base->psc); |
| 124 | writew(scll, &i2c_base->scll); |
| 125 | writew(sclh, &i2c_base->sclh); |
Tom Rix | 03b2a74 | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 126 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 127 | /* own address */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 128 | writew(slaveadd, &i2c_base->oa); |
| 129 | writew(I2C_CON_EN, &i2c_base->con); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 130 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 131 | /* have to enable intrrupts or OMAP i2c module doesn't work */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 132 | writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 133 | I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 134 | udelay(1000); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 135 | flush_fifo(); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 136 | writew(0xFFFF, &i2c_base->stat); |
| 137 | writew(0, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 140 | static void flush_fifo(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 141 | { u16 stat; |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 142 | |
| 143 | /* note: if you try and read data when its not there or ready |
| 144 | * you get a bus error |
| 145 | */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 146 | while (1) { |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 147 | stat = readw(&i2c_base->stat); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 148 | if (stat == I2C_STAT_RRDY) { |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 149 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 150 | defined(CONFIG_OMAP44XX) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 151 | readb(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 152 | #else |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 153 | readw(&i2c_base->data); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 154 | #endif |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 155 | writew(I2C_STAT_RRDY, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 156 | udelay(1000); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 157 | } else |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 162 | int i2c_probe(uchar chip) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 163 | { |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 164 | u32 status; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 165 | int res = 1; /* default = fail */ |
| 166 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 167 | if (chip == readw(&i2c_base->oa)) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 168 | return res; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 169 | |
| 170 | /* wait until bus not busy */ |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 171 | status = wait_for_bb(); |
| 172 | /* exit on BUS busy */ |
| 173 | if (status & I2C_STAT_TIMEO) |
| 174 | return res; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 175 | |
Nick Thompson | 48f7ae4 | 2011-04-11 22:37:41 +0000 | [diff] [blame] | 176 | /* try to write one byte */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 177 | writew(1, &i2c_base->cnt); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 178 | /* set slave address */ |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 179 | writew(chip, &i2c_base->sa); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 180 | /* stop bit needed here */ |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 181 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
| 182 | | I2C_CON_STP, &i2c_base->con); |
| 183 | /* enough delay for the NACK bit set */ |
| 184 | udelay(9000); |
Nick Thompson | 48f7ae4 | 2011-04-11 22:37:41 +0000 | [diff] [blame] | 185 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 186 | if (!(readw(&i2c_base->stat) & I2C_STAT_NACK)) { |
| 187 | res = 0; /* success case */ |
| 188 | flush_fifo(); |
| 189 | writew(0xFFFF, &i2c_base->stat); |
| 190 | } else { |
| 191 | /* failure, clear sources*/ |
| 192 | writew(0xFFFF, &i2c_base->stat); |
| 193 | /* finish up xfer */ |
| 194 | writew(readw(&i2c_base->con) | I2C_CON_STP, &i2c_base->con); |
| 195 | status = wait_for_bb(); |
| 196 | /* exit on BUS busy */ |
| 197 | if (status & I2C_STAT_TIMEO) |
| 198 | return res; |
| 199 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 200 | flush_fifo(); |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 201 | /* don't allow any more data in... we don't want it. */ |
| 202 | writew(0, &i2c_base->cnt); |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 203 | writew(0xFFFF, &i2c_base->stat); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 204 | return res; |
| 205 | } |
| 206 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 207 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 208 | { |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 209 | int i2c_error = 0, i; |
| 210 | u32 status; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 211 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 212 | if ((alen > 2) || (alen < 0)) |
| 213 | return 1; |
| 214 | |
| 215 | if (alen < 2) { |
| 216 | if (addr + len > 256) |
| 217 | return 1; |
| 218 | } else if (addr + len > 0xFFFF) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 219 | return 1; |
| 220 | } |
| 221 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 222 | /* wait until bus not busy */ |
| 223 | status = wait_for_bb(); |
| 224 | |
| 225 | /* exit on BUS busy */ |
| 226 | if (status & I2C_STAT_TIMEO) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 227 | return 1; |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 228 | |
| 229 | writew((alen & 0xFF), &i2c_base->cnt); |
| 230 | /* set slave address */ |
| 231 | writew(chip, &i2c_base->sa); |
| 232 | /* Clear the Tx & Rx FIFOs */ |
| 233 | writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR | |
| 234 | I2C_TXFIFO_CLEAR), &i2c_base->buf); |
| 235 | /* no stop bit needed here */ |
| 236 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX | |
| 237 | I2C_CON_STT, &i2c_base->con); |
| 238 | |
| 239 | /* wait for Transmit ready condition */ |
| 240 | status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK); |
| 241 | |
| 242 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) |
| 243 | i2c_error = 1; |
| 244 | |
| 245 | if (!i2c_error) { |
| 246 | if (status & I2C_STAT_XRDY) { |
| 247 | switch (alen) { |
| 248 | case 2: |
| 249 | /* Send address MSByte */ |
| 250 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 251 | writew(((addr >> 8) & 0xFF), &i2c_base->data); |
| 252 | |
| 253 | /* Clearing XRDY event */ |
| 254 | writew((status & I2C_STAT_XRDY), |
| 255 | &i2c_base->stat); |
| 256 | /* wait for Transmit ready condition */ |
| 257 | status = wait_for_status_mask(I2C_STAT_XRDY | |
| 258 | I2C_STAT_NACK); |
| 259 | |
| 260 | if (status & (I2C_STAT_NACK | |
| 261 | I2C_STAT_TIMEO)) { |
| 262 | i2c_error = 1; |
| 263 | break; |
| 264 | } |
| 265 | #endif |
| 266 | case 1: |
| 267 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 268 | /* Send address LSByte */ |
| 269 | writew((addr & 0xFF), &i2c_base->data); |
| 270 | #else |
| 271 | /* Send address Short word */ |
| 272 | writew((addr & 0xFFFF), &i2c_base->data); |
| 273 | #endif |
| 274 | /* Clearing XRDY event */ |
| 275 | writew((status & I2C_STAT_XRDY), |
| 276 | &i2c_base->stat); |
| 277 | /*wait for Transmit ready condition */ |
| 278 | status = wait_for_status_mask(I2C_STAT_ARDY | |
| 279 | I2C_STAT_NACK); |
| 280 | |
| 281 | if (status & (I2C_STAT_NACK | |
| 282 | I2C_STAT_TIMEO)) { |
| 283 | i2c_error = 1; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } else |
| 288 | i2c_error = 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 291 | /* Wait for ARDY to set */ |
| 292 | status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK |
| 293 | | I2C_STAT_AL); |
| 294 | |
| 295 | if (!i2c_error) { |
| 296 | /* set slave address */ |
| 297 | writew(chip, &i2c_base->sa); |
| 298 | writew((len & 0xFF), &i2c_base->cnt); |
| 299 | /* Clear the Tx & Rx FIFOs */ |
| 300 | writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR | |
| 301 | I2C_TXFIFO_CLEAR), &i2c_base->buf); |
| 302 | /* need stop bit here */ |
| 303 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, |
| 304 | &i2c_base->con); |
| 305 | |
| 306 | for (i = 0; i < len; i++) { |
| 307 | /* wait for Receive condition */ |
| 308 | status = wait_for_status_mask(I2C_STAT_RRDY | |
| 309 | I2C_STAT_NACK); |
| 310 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) { |
| 311 | i2c_error = 1; |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if (status & I2C_STAT_RRDY) { |
| 316 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 317 | buffer[i] = readb(&i2c_base->data); |
| 318 | #else |
| 319 | *((u16 *)&buffer[i]) = |
| 320 | readw(&i2c_base->data) & 0xFFFF; |
| 321 | i++; |
| 322 | #endif |
| 323 | writew((status & I2C_STAT_RRDY), |
| 324 | &i2c_base->stat); |
| 325 | udelay(1000); |
| 326 | } else { |
| 327 | i2c_error = 1; |
| 328 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 332 | /* Wait for ARDY to set */ |
| 333 | status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK |
| 334 | | I2C_STAT_AL); |
| 335 | |
| 336 | if (i2c_error) { |
| 337 | writew(0, &i2c_base->con); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | writew(I2C_CON_EN, &i2c_base->con); |
| 342 | |
| 343 | while (readw(&i2c_base->stat) |
| 344 | || (readw(&i2c_base->con) & I2C_CON_MST)) { |
| 345 | udelay(10000); |
| 346 | writew(0xFFFF, &i2c_base->stat); |
| 347 | } |
| 348 | |
| 349 | writew(I2C_CON_EN, &i2c_base->con); |
| 350 | flush_fifo(); |
| 351 | writew(0xFFFF, &i2c_base->stat); |
| 352 | writew(0, &i2c_base->cnt); |
| 353 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 357 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 358 | { |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 359 | |
| 360 | int i, i2c_error = 0; |
| 361 | u32 status; |
| 362 | u16 writelen; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 363 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 364 | if (alen > 2) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 365 | return 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 366 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 367 | if (alen < 2) { |
| 368 | if (addr + len > 256) |
| 369 | return 1; |
| 370 | } else if (addr + len > 0xFFFF) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 374 | /* wait until bus not busy */ |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 375 | status = wait_for_bb(); |
| 376 | |
| 377 | /* exiting on BUS busy */ |
| 378 | if (status & I2C_STAT_TIMEO) |
| 379 | return 1; |
| 380 | |
| 381 | writelen = (len & 0xFFFF) + alen; |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 382 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 383 | /* two bytes */ |
| 384 | writew((writelen & 0xFFFF), &i2c_base->cnt); |
| 385 | /* Clear the Tx & Rx FIFOs */ |
| 386 | writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR | |
| 387 | I2C_TXFIFO_CLEAR), &i2c_base->buf); |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 388 | /* set slave address */ |
| 389 | writew(chip, &i2c_base->sa); |
| 390 | /* stop bit needed here */ |
| 391 | writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
| 392 | I2C_CON_STP, &i2c_base->con); |
| 393 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 394 | /* wait for Transmit ready condition */ |
| 395 | status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK); |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 396 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 397 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 398 | i2c_error = 1; |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 399 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 400 | if (!i2c_error) { |
| 401 | if (status & I2C_STAT_XRDY) { |
| 402 | switch (alen) { |
| 403 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 404 | case 2: |
| 405 | /* send out MSB byte */ |
| 406 | writeb(((addr >> 8) & 0xFF), &i2c_base->data); |
| 407 | #else |
| 408 | writeb((addr & 0xFFFF), &i2c_base->data); |
| 409 | break; |
| 410 | #endif |
| 411 | /* Clearing XRDY event */ |
| 412 | writew((status & I2C_STAT_XRDY), |
| 413 | &i2c_base->stat); |
| 414 | /*waiting for Transmit ready * condition */ |
| 415 | status = wait_for_status_mask(I2C_STAT_XRDY | |
| 416 | I2C_STAT_NACK); |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 417 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 418 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) { |
| 419 | i2c_error = 1; |
| 420 | break; |
| 421 | } |
| 422 | case 1: |
| 423 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 424 | /* send out MSB byte */ |
| 425 | writeb((addr & 0xFF), &i2c_base->data); |
| 426 | #else |
| 427 | writew(((buffer[0] << 8) | (addr & 0xFF)), |
| 428 | &i2c_base->data); |
| 429 | #endif |
| 430 | } |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 431 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 432 | /* Clearing XRDY event */ |
| 433 | writew((status & I2C_STAT_XRDY), &i2c_base->stat); |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 436 | /* waiting for Transmit ready condition */ |
| 437 | status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK); |
| 438 | |
| 439 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 440 | i2c_error = 1; |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 441 | |
| 442 | if (!i2c_error) { |
| 443 | for (i = ((alen > 1) ? 0 : 1); i < len; i++) { |
| 444 | if (status & I2C_STAT_XRDY) { |
| 445 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 446 | writeb((buffer[i] & 0xFF), |
| 447 | &i2c_base->data); |
| 448 | #else |
| 449 | writew((((buffer[i] << 8) | |
| 450 | buffer[i + 1]) & 0xFFFF), |
| 451 | &i2c_base->data); |
| 452 | i++; |
| 453 | #endif |
| 454 | } else |
| 455 | i2c_error = 1; |
| 456 | /* Clearing XRDY event */ |
| 457 | writew((status & I2C_STAT_XRDY), |
| 458 | &i2c_base->stat); |
| 459 | /* waiting for XRDY condition */ |
| 460 | status = wait_for_status_mask( |
| 461 | I2C_STAT_XRDY | |
| 462 | I2C_STAT_ARDY | |
| 463 | I2C_STAT_NACK); |
| 464 | if (status & (I2C_STAT_NACK | |
| 465 | I2C_STAT_TIMEO)) { |
| 466 | i2c_error = 1; |
| 467 | break; |
| 468 | } |
| 469 | if (status & I2C_STAT_ARDY) |
| 470 | break; |
| 471 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 475 | status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK | |
| 476 | I2C_STAT_AL); |
| 477 | |
| 478 | if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) |
| 479 | i2c_error = 1; |
| 480 | |
| 481 | if (i2c_error) { |
| 482 | writew(0, &i2c_base->con); |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | if (!i2c_error) { |
| 487 | int eout = 200; |
| 488 | |
| 489 | writew(I2C_CON_EN, &i2c_base->con); |
| 490 | while ((status = readw(&i2c_base->stat)) || |
| 491 | (readw(&i2c_base->con) & I2C_CON_MST)) { |
| 492 | udelay(1000); |
| 493 | /* have to read to clear intrrupt */ |
| 494 | writew(0xFFFF, &i2c_base->stat); |
| 495 | if (--eout == 0) |
| 496 | /* better leave with error than hang */ |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
Michael Jones | bb54d57 | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 501 | flush_fifo(); |
| 502 | writew(0xFFFF, &i2c_base->stat); |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 503 | writew(0, &i2c_base->cnt); |
| 504 | return 0; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 507 | static u32 wait_for_bb(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 508 | { |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 509 | int timeout = I2C_TIMEOUT; |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 510 | u32 stat; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 511 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 512 | while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) { |
| 513 | writew(stat, &i2c_base->stat); |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 514 | udelay(1000); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (timeout <= 0) { |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 518 | printf("timed out in wait_for_bb: I2C_STAT=%x\n", |
| 519 | readw(&i2c_base->stat)); |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 520 | stat |= I2C_STAT_TIMEO; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 521 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 522 | writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/ |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 523 | return stat; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 526 | static u32 wait_for_status_mask(u16 mask) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 527 | { |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 528 | u32 status; |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 529 | int timeout = I2C_TIMEOUT; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 530 | |
| 531 | do { |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 532 | udelay(1000); |
| 533 | status = readw(&i2c_base->stat); |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 534 | } while (!(status & mask) && timeout--); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 535 | |
| 536 | if (timeout <= 0) { |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 537 | printf("timed out in wait_for_status_mask: I2C_STAT=%x\n", |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 538 | readw(&i2c_base->stat)); |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 539 | writew(0xFFFF, &i2c_base->stat); |
Patil, Rachna | a9e18c2 | 2012-01-22 23:44:12 +0000 | [diff] [blame^] | 540 | status |= I2C_STAT_TIMEO; |
Steve Sakoman | fb5c39a | 2010-10-20 06:07:44 -0700 | [diff] [blame] | 541 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 542 | return status; |
| 543 | } |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 544 | |
| 545 | int i2c_set_bus_num(unsigned int bus) |
| 546 | { |
| 547 | if ((bus < 0) || (bus >= I2C_BUS_MAX)) { |
| 548 | printf("Bad bus: %d\n", bus); |
| 549 | return -1; |
| 550 | } |
| 551 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 552 | #if I2C_BUS_MAX == 3 |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 553 | if (bus == 2) |
| 554 | i2c_base = (struct i2c *)I2C_BASE3; |
| 555 | else |
| 556 | #endif |
| 557 | if (bus == 1) |
| 558 | i2c_base = (struct i2c *)I2C_BASE2; |
| 559 | else |
| 560 | i2c_base = (struct i2c *)I2C_BASE1; |
| 561 | |
| 562 | current_bus = bus; |
| 563 | |
Michael Jones | 4db6786 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 564 | if (!bus_initialized[current_bus]) |
Dirk Behme | 7a8f657 | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 565 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 566 | |
| 567 | return 0; |
| 568 | } |
Steve Sakoman | 10acc71 | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 569 | |
| 570 | int i2c_get_bus_num(void) |
| 571 | { |
| 572 | return (int) current_bus; |
| 573 | } |