Jon Loeliger | 7237c03 | 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 | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 19 | #include <common.h> |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 20 | |
| 21 | #ifdef CONFIG_HARD_I2C |
| 22 | |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 23 | #include <command.h> |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 24 | #include <asm/io.h> |
| 25 | #include <asm/fsl_i2c.h> |
| 26 | |
| 27 | #define I2C_TIMEOUT (CFG_HZ / 4) |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 28 | #define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET)) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 29 | |
| 30 | |
| 31 | void |
| 32 | i2c_init(int speed, int slaveadd) |
| 33 | { |
| 34 | /* stop I2C controller */ |
| 35 | writeb(0x0 , &I2C->cr); |
| 36 | |
| 37 | /* set clock */ |
| 38 | writeb(0x3f, &I2C->fdr); |
| 39 | |
| 40 | /* set default filter */ |
| 41 | writeb(0x10, &I2C->dfsrr); |
| 42 | |
| 43 | /* write slave address */ |
| 44 | writeb(slaveadd, &I2C->adr); |
| 45 | |
| 46 | /* clear status register */ |
| 47 | writeb(0x0, &I2C->sr); |
| 48 | |
| 49 | /* start I2C controller */ |
| 50 | writeb(I2C_CR_MEN, &I2C->cr); |
| 51 | } |
| 52 | |
| 53 | static __inline__ int |
| 54 | i2c_wait4bus(void) |
| 55 | { |
| 56 | ulong timeval = get_timer (0); |
| 57 | |
| 58 | while (readb(&I2C->sr) & I2C_SR_MBB) { |
| 59 | if (get_timer(timeval) > I2C_TIMEOUT) { |
| 60 | return -1; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static __inline__ int |
| 68 | i2c_wait(int write) |
| 69 | { |
| 70 | u32 csr; |
| 71 | ulong timeval = get_timer(0); |
| 72 | |
| 73 | do { |
| 74 | csr = readb(&I2C->sr); |
| 75 | if (!(csr & I2C_SR_MIF)) |
| 76 | continue; |
| 77 | |
| 78 | writeb(0x0, &I2C->sr); |
| 79 | |
| 80 | if (csr & I2C_SR_MAL) { |
| 81 | debug("i2c_wait: MAL\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | if (!(csr & I2C_SR_MCF)) { |
| 86 | debug("i2c_wait: unfinished\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) { |
| 91 | debug("i2c_wait: No RXACK\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } while (get_timer (timeval) < I2C_TIMEOUT); |
| 97 | |
| 98 | debug("i2c_wait: timed out\n"); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | static __inline__ int |
| 103 | i2c_write_addr (u8 dev, u8 dir, int rsta) |
| 104 | { |
| 105 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
| 106 | | (rsta ? I2C_CR_RSTA : 0), |
| 107 | &I2C->cr); |
| 108 | |
| 109 | writeb((dev << 1) | dir, &I2C->dr); |
| 110 | |
| 111 | if (i2c_wait(I2C_WRITE) < 0) |
| 112 | return 0; |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | static __inline__ int |
| 118 | __i2c_write(u8 *data, int length) |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
| 123 | &I2C->cr); |
| 124 | |
| 125 | for (i = 0; i < length; i++) { |
| 126 | writeb(data[i], &I2C->dr); |
| 127 | |
| 128 | if (i2c_wait(I2C_WRITE) < 0) |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | return i; |
| 133 | } |
| 134 | |
| 135 | static __inline__ int |
| 136 | __i2c_read(u8 *data, int length) |
| 137 | { |
| 138 | int i; |
| 139 | |
| 140 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
| 141 | &I2C->cr); |
| 142 | |
| 143 | /* dummy read */ |
| 144 | readb(&I2C->dr); |
| 145 | |
| 146 | for (i = 0; i < length; i++) { |
| 147 | if (i2c_wait(I2C_READ) < 0) |
| 148 | break; |
| 149 | |
| 150 | /* Generate ack on last next to last byte */ |
| 151 | if (i == length - 2) |
| 152 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
| 153 | &I2C->cr); |
| 154 | |
| 155 | /* Generate stop on last byte */ |
| 156 | if (i == length - 1) |
| 157 | writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr); |
| 158 | |
| 159 | data[i] = readb(&I2C->dr); |
| 160 | } |
| 161 | |
| 162 | return i; |
| 163 | } |
| 164 | |
| 165 | int |
| 166 | i2c_read(u8 dev, uint addr, int alen, u8 *data, int length) |
| 167 | { |
| 168 | int i = 0; |
| 169 | u8 *a = (u8*)&addr; |
| 170 | |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 171 | if (i2c_wait4bus() >= 0 |
| 172 | && i2c_write_addr(dev, I2C_WRITE, 0) != 0 |
| 173 | && __i2c_write(&a[4 - alen], alen) == alen |
| 174 | && i2c_write_addr(dev, I2C_READ, 1) != 0) { |
| 175 | i = __i2c_read(data, length); |
| 176 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 177 | |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 178 | writeb(I2C_CR_MEN, &I2C->cr); |
| 179 | |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 180 | if (i == length) |
| 181 | return 0; |
| 182 | |
| 183 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | int |
| 187 | i2c_write(u8 dev, uint addr, int alen, u8 *data, int length) |
| 188 | { |
| 189 | int i = 0; |
| 190 | u8 *a = (u8*)&addr; |
| 191 | |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 192 | if (i2c_wait4bus() >= 0 |
| 193 | && i2c_write_addr(dev, I2C_WRITE, 0) != 0 |
| 194 | && __i2c_write(&a[4 - alen], alen) == alen) { |
| 195 | i = __i2c_write(data, length); |
| 196 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 197 | |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 198 | writeb(I2C_CR_MEN, &I2C->cr); |
| 199 | |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame^] | 200 | if (i == length) |
| 201 | return 0; |
| 202 | |
| 203 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int |
| 207 | i2c_probe(uchar chip) |
| 208 | { |
| 209 | int tmp; |
| 210 | |
| 211 | /* |
| 212 | * Try to read the first location of the chip. The underlying |
| 213 | * driver doesn't appear to support sending just the chip address |
| 214 | * and looking for an <ACK> back. |
| 215 | */ |
| 216 | udelay(10000); |
| 217 | |
| 218 | return i2c_read(chip, 0, 1, (uchar *)&tmp, 1); |
| 219 | } |
| 220 | |
| 221 | uchar |
| 222 | i2c_reg_read(uchar i2c_addr, uchar reg) |
| 223 | { |
| 224 | uchar buf[1]; |
| 225 | |
| 226 | i2c_read(i2c_addr, reg, 1, buf, 1); |
| 227 | |
| 228 | return buf[0]; |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | i2c_reg_write(uchar i2c_addr, uchar reg, uchar val) |
| 233 | { |
| 234 | i2c_write(i2c_addr, reg, 1, &val, 1); |
| 235 | } |
| 236 | |
| 237 | #endif /* CONFIG_HARD_I2C */ |