Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * Version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 16 | * MA 02111-1307 USA |
| 17 | */ |
| 18 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 19 | #include <common.h> |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 20 | |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 21 | #ifdef CONFIG_FSL_I2C |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 22 | #ifdef CONFIG_HARD_I2C |
| 23 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 24 | #include <command.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 25 | #include <i2c.h> /* Functional interface */ |
| 26 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 27 | #include <asm/io.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 28 | #include <asm/fsl_i2c.h> /* HW definitions */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 29 | |
| 30 | #define I2C_TIMEOUT (CFG_HZ / 4) |
| 31 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 32 | #define I2C_READ_BIT 1 |
| 33 | #define I2C_WRITE_BIT 0 |
| 34 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 35 | /* Initialize the bus pointer to whatever one the SPD EEPROM is on. |
| 36 | * Default is bus 0. This is necessary because the DDR initialization |
| 37 | * runs from ROM, and we can't switch buses because we can't modify |
| 38 | * the global variables. |
| 39 | */ |
| 40 | #ifdef CFG_SPD_BUS_NUM |
| 41 | static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = CFG_SPD_BUS_NUM; |
| 42 | #else |
| 43 | static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = 0; |
| 44 | #endif |
| 45 | |
| 46 | static volatile struct fsl_i2c *i2c_dev[2] = { |
| 47 | (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET), |
| 48 | #ifdef CFG_I2C2_OFFSET |
| 49 | (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET) |
| 50 | #endif |
| 51 | }; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 52 | |
| 53 | void |
| 54 | i2c_init(int speed, int slaveadd) |
| 55 | { |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 56 | volatile struct fsl_i2c *dev; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 57 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 58 | dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 59 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 60 | writeb(0, &dev->cr); /* stop I2C controller */ |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 61 | udelay(5); /* let it shutdown in peace */ |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 62 | writeb(0x3F, &dev->fdr); /* set bus speed */ |
| 63 | writeb(0x3F, &dev->dfsrr); /* set default filter */ |
Joakim Tjernlund | a292af2 | 2006-11-28 16:17:18 -0600 | [diff] [blame] | 64 | writeb(slaveadd << 1, &dev->adr); /* write slave address */ |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 65 | writeb(0x0, &dev->sr); /* clear status register */ |
| 66 | writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 67 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 68 | #ifdef CFG_I2C2_OFFSET |
| 69 | dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 70 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 71 | writeb(0, &dev->cr); /* stop I2C controller */ |
| 72 | writeb(0x3F, &dev->fdr); /* set bus speed */ |
| 73 | writeb(0x3F, &dev->dfsrr); /* set default filter */ |
| 74 | writeb(slaveadd, &dev->adr); /* write slave address */ |
| 75 | writeb(0x0, &dev->sr); /* clear status register */ |
| 76 | writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */ |
| 77 | #endif /* CFG_I2C2_OFFSET */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static __inline__ int |
| 81 | i2c_wait4bus(void) |
| 82 | { |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 83 | ulong timeval = get_timer(0); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 84 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 85 | while (readb(&i2c_dev[i2c_bus_num]->sr) & I2C_SR_MBB) { |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 86 | if (get_timer(timeval) > I2C_TIMEOUT) { |
| 87 | return -1; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static __inline__ int |
| 95 | i2c_wait(int write) |
| 96 | { |
| 97 | u32 csr; |
| 98 | ulong timeval = get_timer(0); |
| 99 | |
| 100 | do { |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 101 | csr = readb(&i2c_dev[i2c_bus_num]->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 102 | if (!(csr & I2C_SR_MIF)) |
| 103 | continue; |
| 104 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 105 | writeb(0x0, &i2c_dev[i2c_bus_num]->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 106 | |
| 107 | if (csr & I2C_SR_MAL) { |
| 108 | debug("i2c_wait: MAL\n"); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | if (!(csr & I2C_SR_MCF)) { |
| 113 | debug("i2c_wait: unfinished\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 117 | if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 118 | debug("i2c_wait: No RXACK\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } while (get_timer (timeval) < I2C_TIMEOUT); |
| 124 | |
| 125 | debug("i2c_wait: timed out\n"); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | static __inline__ int |
| 130 | i2c_write_addr (u8 dev, u8 dir, int rsta) |
| 131 | { |
| 132 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
| 133 | | (rsta ? I2C_CR_RSTA : 0), |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 134 | &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 135 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 136 | writeb((dev << 1) | dir, &i2c_dev[i2c_bus_num]->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 137 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 138 | if (i2c_wait(I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 139 | return 0; |
| 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | static __inline__ int |
| 145 | __i2c_write(u8 *data, int length) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 150 | &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 151 | |
| 152 | for (i = 0; i < length; i++) { |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 153 | writeb(data[i], &i2c_dev[i2c_bus_num]->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 154 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 155 | if (i2c_wait(I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | |
| 159 | return i; |
| 160 | } |
| 161 | |
| 162 | static __inline__ int |
| 163 | __i2c_read(u8 *data, int length) |
| 164 | { |
| 165 | int i; |
| 166 | |
| 167 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 168 | &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 169 | |
| 170 | /* dummy read */ |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 171 | readb(&i2c_dev[i2c_bus_num]->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 172 | |
| 173 | for (i = 0; i < length; i++) { |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 174 | if (i2c_wait(I2C_READ_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | /* Generate ack on last next to last byte */ |
| 178 | if (i == length - 2) |
| 179 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 180 | &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 181 | |
| 182 | /* Generate stop on last byte */ |
| 183 | if (i == length - 1) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 184 | writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 185 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 186 | data[i] = readb(&i2c_dev[i2c_bus_num]->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return i; |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | i2c_read(u8 dev, uint addr, int alen, u8 *data, int length) |
| 194 | { |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 195 | int i = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 196 | u8 *a = (u8*)&addr; |
| 197 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 198 | if (i2c_wait4bus() >= 0 |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 199 | && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0 |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 200 | && __i2c_write(&a[4 - alen], alen) == alen) |
| 201 | i = 0; /* No error so far */ |
| 202 | |
| 203 | if (length |
| 204 | && i2c_write_addr(dev, I2C_READ_BIT, 1) != 0) |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 205 | i = __i2c_read(data, length); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 206 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 207 | writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 208 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 209 | if (i == length) |
| 210 | return 0; |
| 211 | |
| 212 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int |
| 216 | i2c_write(u8 dev, uint addr, int alen, u8 *data, int length) |
| 217 | { |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 218 | int i = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 219 | u8 *a = (u8*)&addr; |
| 220 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 221 | if (i2c_wait4bus() >= 0 |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 222 | && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0 |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 223 | && __i2c_write(&a[4 - alen], alen) == alen) { |
| 224 | i = __i2c_write(data, length); |
| 225 | } |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 226 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 227 | writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 228 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 229 | if (i == length) |
| 230 | return 0; |
| 231 | |
| 232 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | int |
| 236 | i2c_probe(uchar chip) |
| 237 | { |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 238 | /* For unknow reason the controller will ACK when |
| 239 | * probing for a slave with the same address, so skip |
| 240 | * it. |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 241 | */ |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 242 | if (chip == (readb(&i2c_dev[i2c_bus_num]->adr) >> 1)) |
| 243 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 244 | |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 245 | return i2c_read(chip, 0, 0, NULL, 0); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | uchar |
| 249 | i2c_reg_read(uchar i2c_addr, uchar reg) |
| 250 | { |
| 251 | uchar buf[1]; |
| 252 | |
| 253 | i2c_read(i2c_addr, reg, 1, buf, 1); |
| 254 | |
| 255 | return buf[0]; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | i2c_reg_write(uchar i2c_addr, uchar reg, uchar val) |
| 260 | { |
| 261 | i2c_write(i2c_addr, reg, 1, &val, 1); |
| 262 | } |
| 263 | |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 264 | int i2c_set_bus_num(unsigned int bus) |
| 265 | { |
| 266 | #ifdef CFG_I2C2_OFFSET |
| 267 | if (bus > 1) { |
| 268 | #else |
| 269 | if (bus > 0) { |
| 270 | #endif |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | i2c_bus_num = bus; |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int i2c_set_bus_speed(unsigned int speed) |
| 280 | { |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | unsigned int i2c_get_bus_num(void) |
| 285 | { |
| 286 | return i2c_bus_num; |
| 287 | } |
| 288 | |
| 289 | unsigned int i2c_get_bus_speed(void) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 293 | #endif /* CONFIG_HARD_I2C */ |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 294 | #endif /* CONFIG_FSL_I2C */ |