blob: af191915e37f780085bae62edc15813c30ba8784 [file] [log] [blame]
Jon Loeliger7237c032006-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 Loeliger7237c032006-10-19 11:02:16 -050019#include <common.h>
Jon Loeliger7237c032006-10-19 11:02:16 -050020
21#ifdef CONFIG_HARD_I2C
22
Jon Loeliger4d45f692006-10-19 12:02:24 -050023#include <command.h>
Jon Loeliger7237c032006-10-19 11:02:16 -050024#include <asm/io.h>
25#include <asm/fsl_i2c.h>
26
27#define I2C_TIMEOUT (CFG_HZ / 4)
Jon Loeliger4d45f692006-10-19 12:02:24 -050028#define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
Jon Loeliger7237c032006-10-19 11:02:16 -050029
30
31void
32i2c_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
53static __inline__ int
54i2c_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
67static __inline__ int
68i2c_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
102static __inline__ int
103i2c_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
117static __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
135static __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
165int
166i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
167{
168 int i = 0;
169 u8 *a = (u8*)&addr;
170
Jon Loeliger4d45f692006-10-19 12:02:24 -0500171 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 Loeliger7237c032006-10-19 11:02:16 -0500177
Jon Loeliger7237c032006-10-19 11:02:16 -0500178 writeb(I2C_CR_MEN, &I2C->cr);
179
Jon Loeliger4d45f692006-10-19 12:02:24 -0500180 if (i == length)
181 return 0;
182
183 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500184}
185
186int
187i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
188{
189 int i = 0;
190 u8 *a = (u8*)&addr;
191
Jon Loeliger4d45f692006-10-19 12:02:24 -0500192 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 Loeliger7237c032006-10-19 11:02:16 -0500197
Jon Loeliger7237c032006-10-19 11:02:16 -0500198 writeb(I2C_CR_MEN, &I2C->cr);
199
Jon Loeliger4d45f692006-10-19 12:02:24 -0500200 if (i == length)
201 return 0;
202
203 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500204}
205
206int
207i2c_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
221uchar
222i2c_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
231void
232i2c_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 */