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 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 28 | static void wait_for_bb (void); |
| 29 | static u16 wait_for_pin (void); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 30 | static void flush_fifo(void); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 31 | |
| 32 | void i2c_init (int speed, int slaveadd) |
| 33 | { |
| 34 | u16 scl; |
| 35 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 36 | writew(0x2, I2C_SYSC); /* for ES2 after soft reset */ |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 37 | udelay(1000); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 38 | writew(0x0, I2C_SYSC); /* will probably self clear but */ |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 39 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 40 | if (readw (I2C_CON) & I2C_CON_EN) { |
| 41 | writew (0, I2C_CON); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 42 | udelay (50000); |
| 43 | } |
| 44 | |
Wolfgang Denk | af0501a | 2008-10-19 02:35:50 +0200 | [diff] [blame] | 45 | /* 12MHz I2C module clock */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 46 | writew (0, I2C_PSC); |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 47 | speed = speed/1000; /* 100 or 400 */ |
| 48 | scl = ((12000/(speed*2)) - 7); /* use 7 when PSC = 0 */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 49 | writew (scl, I2C_SCLL); |
| 50 | writew (scl, I2C_SCLH); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 51 | /* own address */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 52 | writew (slaveadd, I2C_OA); |
| 53 | writew (I2C_CON_EN, I2C_CON); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 54 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 55 | /* have to enable intrrupts or OMAP i2c module doesn't work */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 56 | writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | |
| 57 | I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 58 | udelay (1000); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 59 | flush_fifo(); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 60 | writew (0xFFFF, I2C_STAT); |
| 61 | writew (0, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value) |
| 65 | { |
| 66 | int i2c_error = 0; |
| 67 | u16 status; |
| 68 | |
| 69 | /* wait until bus not busy */ |
| 70 | wait_for_bb (); |
| 71 | |
| 72 | /* one byte only */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 73 | writew (1, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 74 | /* set slave address */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 75 | writew (devaddr, I2C_SA); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 76 | /* no stop bit needed here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 77 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 78 | |
| 79 | status = wait_for_pin (); |
| 80 | |
| 81 | if (status & I2C_STAT_XRDY) { |
| 82 | /* Important: have to use byte access */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 83 | writeb (regoffset, I2C_DATA); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 84 | udelay (20000); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 85 | if (readw (I2C_STAT) & I2C_STAT_NACK) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 86 | i2c_error = 1; |
| 87 | } |
| 88 | } else { |
| 89 | i2c_error = 1; |
| 90 | } |
| 91 | |
| 92 | if (!i2c_error) { |
| 93 | /* free bus, otherwise we can't use a combined transction */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 94 | writew (0, I2C_CON); |
| 95 | while (readw (I2C_STAT) || (readw (I2C_CON) & I2C_CON_MST)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 96 | udelay (10000); |
| 97 | /* Have to clear pending interrupt to clear I2C_STAT */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 98 | writew (0xFFFF, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | wait_for_bb (); |
| 102 | /* set slave address */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 103 | writew (devaddr, I2C_SA); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 104 | /* read one byte from slave */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 105 | writew (1, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 106 | /* need stop bit here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 107 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, |
| 108 | I2C_CON); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 109 | |
| 110 | status = wait_for_pin (); |
| 111 | if (status & I2C_STAT_RRDY) { |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 112 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 113 | *value = readb (I2C_DATA); |
| 114 | #else |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 115 | *value = readw (I2C_DATA); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 116 | #endif |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 117 | udelay (20000); |
| 118 | } else { |
| 119 | i2c_error = 1; |
| 120 | } |
| 121 | |
| 122 | if (!i2c_error) { |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 123 | writew (I2C_CON_EN, I2C_CON); |
| 124 | while (readw (I2C_STAT) |
| 125 | || (readw (I2C_CON) & I2C_CON_MST)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 126 | udelay (10000); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 127 | writew (0xFFFF, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | flush_fifo(); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 132 | writew (0xFFFF, I2C_STAT); |
| 133 | writew (0, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 134 | return i2c_error; |
| 135 | } |
| 136 | |
| 137 | static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value) |
| 138 | { |
| 139 | int i2c_error = 0; |
| 140 | u16 status, stat; |
| 141 | |
| 142 | /* wait until bus not busy */ |
| 143 | wait_for_bb (); |
| 144 | |
| 145 | /* two bytes */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 146 | writew (2, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 147 | /* set slave address */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 148 | writew (devaddr, I2C_SA); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 149 | /* stop bit needed here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 150 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | |
| 151 | I2C_CON_STP, I2C_CON); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 152 | |
| 153 | /* wait until state change */ |
| 154 | status = wait_for_pin (); |
| 155 | |
| 156 | if (status & I2C_STAT_XRDY) { |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 157 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 158 | /* send out 1 byte */ |
| 159 | writeb (regoffset, I2C_DATA); |
| 160 | writew (I2C_STAT_XRDY, I2C_STAT); |
| 161 | |
| 162 | status = wait_for_pin (); |
| 163 | if ((status & I2C_STAT_XRDY)) { |
| 164 | /* send out next 1 byte */ |
| 165 | writeb (value, I2C_DATA); |
| 166 | writew (I2C_STAT_XRDY, I2C_STAT); |
| 167 | } else { |
| 168 | i2c_error = 1; |
| 169 | } |
| 170 | #else |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 171 | /* send out two bytes */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 172 | writew ((value << 8) + regoffset, I2C_DATA); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 173 | #endif |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 174 | /* must have enough delay to allow BB bit to go low */ |
| 175 | udelay (50000); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 176 | if (readw (I2C_STAT) & I2C_STAT_NACK) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 177 | i2c_error = 1; |
| 178 | } |
| 179 | } else { |
| 180 | i2c_error = 1; |
| 181 | } |
| 182 | |
| 183 | if (!i2c_error) { |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 184 | int eout = 200; |
| 185 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 186 | writew (I2C_CON_EN, I2C_CON); |
| 187 | while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 188 | udelay (1000); |
| 189 | /* have to read to clear intrrupt */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 190 | writew (0xFFFF, I2C_STAT); |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 191 | if(--eout == 0) /* better leave with error than hang */ |
| 192 | break; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | flush_fifo(); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 196 | writew (0xFFFF, I2C_STAT); |
| 197 | writew (0, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 198 | return i2c_error; |
| 199 | } |
| 200 | |
Wolfgang Denk | e1e4679 | 2005-09-25 18:41:04 +0200 | [diff] [blame] | 201 | static void flush_fifo(void) |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 202 | { u16 stat; |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 203 | |
| 204 | /* note: if you try and read data when its not there or ready |
| 205 | * you get a bus error |
| 206 | */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 207 | while(1){ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 208 | stat = readw(I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 209 | if(stat == I2C_STAT_RRDY){ |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 210 | #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) |
| 211 | readb(I2C_DATA); |
| 212 | #else |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 213 | readw(I2C_DATA); |
Dirk Behme | 5648e51 | 2008-12-14 09:47:18 +0100 | [diff] [blame] | 214 | #endif |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 215 | writew(I2C_STAT_RRDY,I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 216 | udelay(1000); |
| 217 | }else |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | int i2c_probe (uchar chip) |
| 223 | { |
| 224 | int res = 1; /* default = fail */ |
| 225 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 226 | if (chip == readw (I2C_OA)) { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 227 | return res; |
| 228 | } |
| 229 | |
| 230 | /* wait until bus not busy */ |
| 231 | wait_for_bb (); |
| 232 | |
| 233 | /* try to read one byte */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 234 | writew (1, I2C_CNT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 235 | /* set slave address */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 236 | writew (chip, I2C_SA); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 237 | /* stop bit needed here */ |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 238 | writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 239 | /* enough delay for the NACK bit set */ |
| 240 | udelay (50000); |
| 241 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 242 | if (!(readw (I2C_STAT) & I2C_STAT_NACK)) { |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 243 | res = 0; /* success case */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 244 | flush_fifo(); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 245 | writew(0xFFFF, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 246 | } else { |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 247 | writew(0xFFFF, I2C_STAT); /* failue, clear sources*/ |
| 248 | writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 249 | udelay(20000); |
| 250 | wait_for_bb (); |
| 251 | } |
| 252 | flush_fifo(); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 253 | writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/ |
| 254 | writew(0xFFFF, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 255 | return res; |
| 256 | } |
| 257 | |
| 258 | int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | if (alen > 1) { |
| 263 | printf ("I2C read: addr len %d not supported\n", alen); |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | if (addr + len > 256) { |
| 268 | printf ("I2C read: address out of range\n"); |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | for (i = 0; i < len; i++) { |
| 273 | if (i2c_read_byte (chip, addr + i, &buffer[i])) { |
| 274 | printf ("I2C read: I/O error\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 275 | i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 276 | return 1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len) |
| 284 | { |
| 285 | int i; |
| 286 | |
| 287 | if (alen > 1) { |
| 288 | printf ("I2C read: addr len %d not supported\n", alen); |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | if (addr + len > 256) { |
| 293 | printf ("I2C read: address out of range\n"); |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < len; i++) { |
| 298 | if (i2c_write_byte (chip, addr + i, buffer[i])) { |
| 299 | printf ("I2C read: I/O error\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 300 | i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void wait_for_bb (void) |
| 309 | { |
| 310 | int timeout = 10; |
| 311 | u16 stat; |
| 312 | |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 313 | writew(0xFFFF, I2C_STAT); /* clear current interruts...*/ |
| 314 | while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) { |
| 315 | writew (stat, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 316 | udelay (50000); |
| 317 | } |
| 318 | |
| 319 | if (timeout <= 0) { |
| 320 | printf ("timed out in wait_for_bb: I2C_STAT=%x\n", |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 321 | readw (I2C_STAT)); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 322 | } |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 323 | writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/ |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static u16 wait_for_pin (void) |
| 327 | { |
| 328 | u16 status; |
| 329 | int timeout = 10; |
| 330 | |
| 331 | do { |
| 332 | udelay (1000); |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 333 | status = readw (I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 334 | } while ( !(status & |
| 335 | (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY | |
| 336 | I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK | |
| 337 | I2C_STAT_AL)) && timeout--); |
| 338 | |
| 339 | if (timeout <= 0) { |
| 340 | printf ("timed out in wait_for_pin: I2C_STAT=%x\n", |
Dirk Behme | 5a6dc87 | 2008-11-10 20:15:25 +0100 | [diff] [blame] | 341 | readw (I2C_STAT)); |
| 342 | writew(0xFFFF, I2C_STAT); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 343 | } |
| 344 | return status; |
| 345 | } |