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