blob: 0e392134872cc2ebe8e9821439e6d212aed692ed [file] [log] [blame]
Jon Loeligere4773be2006-10-19 11:02:16 -05001/*
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 Loeligere4773be2006-10-19 11:02:16 -050019#include <common.h>
Jon Loeligere4773be2006-10-19 11:02:16 -050020
Jon Loeliger43d818f2006-10-20 15:50:15 -050021#ifdef CONFIG_FSL_I2C
Jon Loeligere4773be2006-10-19 11:02:16 -050022#ifdef CONFIG_HARD_I2C
23
Jon Loeliger24df9772006-10-19 12:02:24 -050024#include <command.h>
Jon Loeliger43d818f2006-10-20 15:50:15 -050025#include <i2c.h> /* Functional interface */
26
Jon Loeligere4773be2006-10-19 11:02:16 -050027#include <asm/io.h>
Jon Loeliger43d818f2006-10-20 15:50:15 -050028#include <asm/fsl_i2c.h> /* HW definitions */
Jon Loeligere4773be2006-10-19 11:02:16 -050029
30#define I2C_TIMEOUT (CFG_HZ / 4)
31
Timur Tabiab347542006-11-03 19:15:00 -060032/* Initialize the bus pointer to whatever one the SPD EEPROM is on.
33 * Default is bus 0. This is necessary because the DDR initialization
34 * runs from ROM, and we can't switch buses because we can't modify
35 * the global variables.
36 */
37#ifdef CFG_SPD_BUS_NUM
38static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = CFG_SPD_BUS_NUM;
39#else
40static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = 0;
41#endif
42
43static volatile struct fsl_i2c *i2c_dev[2] = {
44 (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET),
45#ifdef CFG_I2C2_OFFSET
46 (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET)
47#endif
48};
Jon Loeligere4773be2006-10-19 11:02:16 -050049
50void
51i2c_init(int speed, int slaveadd)
52{
Timur Tabiab347542006-11-03 19:15:00 -060053 volatile struct fsl_i2c *dev;
Jon Loeligere4773be2006-10-19 11:02:16 -050054
Timur Tabiab347542006-11-03 19:15:00 -060055 dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET);
Jon Loeligere4773be2006-10-19 11:02:16 -050056
Timur Tabiab347542006-11-03 19:15:00 -060057 writeb(0, &dev->cr); /* stop I2C controller */
58 writeb(0x3F, &dev->fdr); /* set bus speed */
59 writeb(0x3F, &dev->dfsrr); /* set default filter */
60 writeb(slaveadd, &dev->adr); /* write slave address */
61 writeb(0x0, &dev->sr); /* clear status register */
62 writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
Jon Loeligere4773be2006-10-19 11:02:16 -050063
Timur Tabiab347542006-11-03 19:15:00 -060064#ifdef CFG_I2C2_OFFSET
65 dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET);
Jon Loeligere4773be2006-10-19 11:02:16 -050066
Timur Tabiab347542006-11-03 19:15:00 -060067 writeb(0, &dev->cr); /* stop I2C controller */
68 writeb(0x3F, &dev->fdr); /* set bus speed */
69 writeb(0x3F, &dev->dfsrr); /* set default filter */
70 writeb(slaveadd, &dev->adr); /* write slave address */
71 writeb(0x0, &dev->sr); /* clear status register */
72 writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
73#endif /* CFG_I2C2_OFFSET */
Jon Loeligere4773be2006-10-19 11:02:16 -050074}
75
76static __inline__ int
77i2c_wait4bus(void)
78{
Jon Loeliger43d818f2006-10-20 15:50:15 -050079 ulong timeval = get_timer(0);
Jon Loeligere4773be2006-10-19 11:02:16 -050080
Timur Tabiab347542006-11-03 19:15:00 -060081 while (readb(&i2c_dev[i2c_bus_num]->sr) & I2C_SR_MBB) {
Jon Loeligere4773be2006-10-19 11:02:16 -050082 if (get_timer(timeval) > I2C_TIMEOUT) {
83 return -1;
84 }
85 }
86
87 return 0;
88}
89
90static __inline__ int
91i2c_wait(int write)
92{
93 u32 csr;
94 ulong timeval = get_timer(0);
95
96 do {
Timur Tabiab347542006-11-03 19:15:00 -060097 csr = readb(&i2c_dev[i2c_bus_num]->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -050098 if (!(csr & I2C_SR_MIF))
99 continue;
100
Timur Tabiab347542006-11-03 19:15:00 -0600101 writeb(0x0, &i2c_dev[i2c_bus_num]->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500102
103 if (csr & I2C_SR_MAL) {
104 debug("i2c_wait: MAL\n");
105 return -1;
106 }
107
108 if (!(csr & I2C_SR_MCF)) {
109 debug("i2c_wait: unfinished\n");
110 return -1;
111 }
112
113 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
114 debug("i2c_wait: No RXACK\n");
115 return -1;
116 }
117
118 return 0;
119 } while (get_timer (timeval) < I2C_TIMEOUT);
120
121 debug("i2c_wait: timed out\n");
122 return -1;
123}
124
125static __inline__ int
126i2c_write_addr (u8 dev, u8 dir, int rsta)
127{
128 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
129 | (rsta ? I2C_CR_RSTA : 0),
Timur Tabiab347542006-11-03 19:15:00 -0600130 &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500131
Timur Tabiab347542006-11-03 19:15:00 -0600132 writeb((dev << 1) | dir, &i2c_dev[i2c_bus_num]->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500133
134 if (i2c_wait(I2C_WRITE) < 0)
135 return 0;
136
137 return 1;
138}
139
140static __inline__ int
141__i2c_write(u8 *data, int length)
142{
143 int i;
144
145 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
Timur Tabiab347542006-11-03 19:15:00 -0600146 &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500147
148 for (i = 0; i < length; i++) {
Timur Tabiab347542006-11-03 19:15:00 -0600149 writeb(data[i], &i2c_dev[i2c_bus_num]->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500150
151 if (i2c_wait(I2C_WRITE) < 0)
152 break;
153 }
154
155 return i;
156}
157
158static __inline__ int
159__i2c_read(u8 *data, int length)
160{
161 int i;
162
163 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
Timur Tabiab347542006-11-03 19:15:00 -0600164 &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500165
166 /* dummy read */
Timur Tabiab347542006-11-03 19:15:00 -0600167 readb(&i2c_dev[i2c_bus_num]->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500168
169 for (i = 0; i < length; i++) {
170 if (i2c_wait(I2C_READ) < 0)
171 break;
172
173 /* Generate ack on last next to last byte */
174 if (i == length - 2)
175 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
Timur Tabiab347542006-11-03 19:15:00 -0600176 &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500177
178 /* Generate stop on last byte */
179 if (i == length - 1)
Timur Tabiab347542006-11-03 19:15:00 -0600180 writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500181
Timur Tabiab347542006-11-03 19:15:00 -0600182 data[i] = readb(&i2c_dev[i2c_bus_num]->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500183 }
184
185 return i;
186}
187
188int
189i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
190{
191 int i = 0;
192 u8 *a = (u8*)&addr;
193
Jon Loeliger24df9772006-10-19 12:02:24 -0500194 if (i2c_wait4bus() >= 0
195 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
196 && __i2c_write(&a[4 - alen], alen) == alen
197 && i2c_write_addr(dev, I2C_READ, 1) != 0) {
198 i = __i2c_read(data, length);
199 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500200
Timur Tabiab347542006-11-03 19:15:00 -0600201 writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500202
Jon Loeliger24df9772006-10-19 12:02:24 -0500203 if (i == length)
204 return 0;
205
206 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500207}
208
209int
210i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
211{
212 int i = 0;
213 u8 *a = (u8*)&addr;
214
Jon Loeliger24df9772006-10-19 12:02:24 -0500215 if (i2c_wait4bus() >= 0
216 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
217 && __i2c_write(&a[4 - alen], alen) == alen) {
218 i = __i2c_write(data, length);
219 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500220
Timur Tabiab347542006-11-03 19:15:00 -0600221 writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500222
Jon Loeliger24df9772006-10-19 12:02:24 -0500223 if (i == length)
224 return 0;
225
226 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500227}
228
229int
230i2c_probe(uchar chip)
231{
232 int tmp;
233
234 /*
235 * Try to read the first location of the chip. The underlying
236 * driver doesn't appear to support sending just the chip address
237 * and looking for an <ACK> back.
238 */
239 udelay(10000);
240
241 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
242}
243
244uchar
245i2c_reg_read(uchar i2c_addr, uchar reg)
246{
247 uchar buf[1];
248
249 i2c_read(i2c_addr, reg, 1, buf, 1);
250
251 return buf[0];
252}
253
254void
255i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
256{
257 i2c_write(i2c_addr, reg, 1, &val, 1);
258}
259
Timur Tabiab347542006-11-03 19:15:00 -0600260int i2c_set_bus_num(unsigned int bus)
261{
262#ifdef CFG_I2C2_OFFSET
263 if (bus > 1) {
264#else
265 if (bus > 0) {
266#endif
267 return -1;
268 }
269
270 i2c_bus_num = bus;
271
272 return 0;
273}
274
275int i2c_set_bus_speed(unsigned int speed)
276{
277 return -1;
278}
279
280unsigned int i2c_get_bus_num(void)
281{
282 return i2c_bus_num;
283}
284
285unsigned int i2c_get_bus_speed(void)
286{
287 return 0;
288}
Jon Loeligere4773be2006-10-19 11:02:16 -0500289#endif /* CONFIG_HARD_I2C */
Jon Loeliger43d818f2006-10-20 15:50:15 -0500290#endif /* CONFIG_FSL_I2C */