]> git.ipfire.org Git - people/ms/u-boot.git/blob - cpu/mpc5xxx/i2c.c
* Patch by Bernhard Kuhn, 28 Oct 2003:
[people/ms/u-boot.git] / cpu / mpc5xxx / i2c.c
1 /*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 #include <common.h>
25
26 #ifdef CONFIG_HARD_I2C
27
28 #include <mpc5xxx.h>
29 #include <i2c.h>
30
31 #if (CFG_I2C_MODULE == 2)
32 #define I2C_BASE MPC5XXX_I2C2
33 #elif (CFG_I2C_MODULE == 1)
34 #define I2C_BASE MPC5XXX_I2C1
35 #else
36 #error CFG_I2C_MODULE is not properly configured
37 #endif
38
39 #define I2C_TIMEOUT 100
40 #define I2C_RETRIES 3
41
42 struct mpc5xxx_i2c_tap {
43 int scl2tap;
44 int tap2tap;
45 };
46
47 static int mpc_reg_in (volatile u32 *reg);
48 static void mpc_reg_out (volatile u32 *reg, int val, int mask);
49 static int wait_for_bb (void);
50 static int wait_for_pin (int *status);
51 static int do_address (uchar chip, char rdwr_flag);
52 static int send_bytes (uchar chip, char *buf, int len);
53 static int receive_bytes (uchar chip, char *buf, int len);
54 static int mpc_get_fdr (int);
55
56 static int mpc_reg_in(volatile u32 *reg)
57 {
58 return *reg >> 24;
59 __asm__ __volatile__ ("eieio");
60 }
61
62 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
63 {
64 int tmp;
65
66 if (!mask) {
67 *reg = val << 24;
68 } else {
69 tmp = mpc_reg_in(reg);
70 *reg = ((tmp & ~mask) | (val & mask)) << 24;
71 }
72 __asm__ __volatile__ ("eieio");
73
74 return;
75 }
76
77 static int wait_for_bb(void)
78 {
79 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
80 int timeout = I2C_TIMEOUT;
81 int status;
82
83 status = mpc_reg_in(&regs->msr);
84
85 while (timeout-- && (status & I2C_BB)) {
86 #if 1
87 volatile int temp;
88 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
89 temp = mpc_reg_in(&regs->mdr);
90 mpc_reg_out(&regs->mcr, 0, I2C_STA);
91 mpc_reg_out(&regs->mcr, 0, 0);
92 mpc_reg_out(&regs->mcr, I2C_EN, 0);
93 #endif
94 udelay(1000);
95 status = mpc_reg_in(&regs->msr);
96 }
97
98 return (status & I2C_BB);
99 }
100
101 static int wait_for_pin(int *status)
102 {
103 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
104 int timeout = I2C_TIMEOUT;
105
106 *status = mpc_reg_in(&regs->msr);
107
108 while (timeout-- && !(*status & I2C_IF)) {
109 udelay(1000);
110 *status = mpc_reg_in(&regs->msr);
111 }
112
113 if (!(*status & I2C_IF)) {
114 return -1;
115 }
116
117 mpc_reg_out(&regs->msr, 0, I2C_IF);
118
119 return 0;
120 }
121
122 static int do_address(uchar chip, char rdwr_flag)
123 {
124 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
125 int status;
126
127 chip <<= 1;
128
129 if (rdwr_flag) {
130 chip |= 1;
131 }
132
133 mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
134 mpc_reg_out(&regs->mdr, chip, 0);
135
136 if (wait_for_pin(&status)) {
137 return -2;
138 }
139
140 if (status & I2C_RXAK) {
141 return -3;
142 }
143
144 return 0;
145 }
146
147 static int send_bytes(uchar chip, char *buf, int len)
148 {
149 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
150 int wrcount;
151 int status;
152
153 for (wrcount = 0; wrcount < len; ++wrcount) {
154
155 mpc_reg_out(&regs->mdr, buf[wrcount], 0);
156
157 if (wait_for_pin(&status)) {
158 break;
159 }
160
161 if (status & I2C_RXAK) {
162 break;
163 }
164
165 }
166
167 return !(wrcount == len);
168 }
169
170 static int receive_bytes(uchar chip, char *buf, int len)
171 {
172 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
173 int dummy = 1;
174 int rdcount = 0;
175 int status;
176 int i;
177
178 mpc_reg_out(&regs->mcr, 0, I2C_TX);
179
180 for (i = 0; i < len; ++i) {
181 buf[rdcount] = mpc_reg_in(&regs->mdr);
182
183 if (dummy) {
184 dummy = 0;
185 } else {
186 rdcount++;
187 }
188
189
190 if (wait_for_pin(&status)) {
191 return -4;
192 }
193 }
194
195 mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
196 buf[rdcount++] = mpc_reg_in(&regs->mdr);
197
198 if (wait_for_pin(&status)) {
199 return -5;
200 }
201
202 mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
203
204 return 0;
205 }
206
207 /**************** I2C API ****************/
208
209 void i2c_init(int speed, int saddr)
210 {
211 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
212
213 mpc_reg_out(&regs->mcr, 0, 0);
214 mpc_reg_out(&regs->madr, saddr << 1, 0);
215
216 /* Set clock
217 */
218 mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
219
220 /* Enable module
221 */
222 mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
223 mpc_reg_out(&regs->msr, 0, I2C_IF);
224
225 return;
226 }
227
228 static int mpc_get_fdr(int speed)
229 {
230 DECLARE_GLOBAL_DATA_PTR;
231 static int fdr = -1;
232
233 if (fdr == -1) {
234 ulong best_speed = 0;
235 ulong divider;
236 ulong ipb, scl;
237 ulong bestmatch = 0xffffffffUL;
238 int best_i = 0, best_j = 0, i, j;
239 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
240 struct mpc5xxx_i2c_tap scltap[] = {
241 {4, 1},
242 {4, 2},
243 {6, 4},
244 {6, 8},
245 {14, 16},
246 {30, 32},
247 {62, 64},
248 {126, 128}
249 };
250
251 ipb = gd->ipb_clk;
252 for (i = 7; i >= 0; i--) {
253 for (j = 7; j >= 0; j--) {
254 scl = 2 * (scltap[j].scl2tap +
255 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
256 if (ipb <= speed*scl) {
257 if ((speed*scl - ipb) < bestmatch) {
258 bestmatch = speed*scl - ipb;
259 best_i = i;
260 best_j = j;
261 best_speed = ipb/scl;
262 }
263 }
264 }
265 }
266 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
267 if (gd->flags & GD_FLG_RELOC) {
268 fdr = divider;
269 } else {
270 printf("%ld kHz, ", best_speed / 1000);
271 return divider;
272 }
273 }
274
275 return fdr;
276 }
277
278 int i2c_probe(uchar chip)
279 {
280 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
281 int i;
282
283 for (i = 0; i < I2C_RETRIES; i++) {
284 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
285
286 if (! do_address(chip, 0)) {
287 mpc_reg_out(&regs->mcr, 0, I2C_STA);
288 break;
289 }
290
291 mpc_reg_out(&regs->mcr, 0, I2C_STA);
292 udelay(500);
293 }
294
295 return (i == I2C_RETRIES);
296 }
297
298 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
299 {
300 uchar xaddr[4];
301 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
302 int ret = -1;
303
304 xaddr[0] = (addr >> 24) & 0xFF;
305 xaddr[1] = (addr >> 16) & 0xFF;
306 xaddr[2] = (addr >> 8) & 0xFF;
307 xaddr[3] = addr & 0xFF;
308
309 if (wait_for_bb()) {
310 printf("i2c_read: bus is busy\n");
311 goto Done;
312 }
313
314 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
315 if (do_address(chip, 0)) {
316 printf("i2c_read: failed to address chip\n");
317 goto Done;
318 }
319
320 if (send_bytes(chip, &xaddr[4-alen], alen)) {
321 printf("i2c_read: send_bytes failed\n");
322 goto Done;
323 }
324
325 mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
326 if (do_address(chip, 1)) {
327 printf("i2c_read: failed to address chip\n");
328 goto Done;
329 }
330
331 if (receive_bytes(chip, buf, len)) {
332 printf("i2c_read: receive_bytes failed\n");
333 goto Done;
334 }
335
336 ret = 0;
337 Done:
338 mpc_reg_out(&regs->mcr, 0, I2C_STA);
339 return ret;
340 }
341
342 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
343 {
344 uchar xaddr[4];
345 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
346 int ret = -1;
347
348 xaddr[0] = (addr >> 24) & 0xFF;
349 xaddr[1] = (addr >> 16) & 0xFF;
350 xaddr[2] = (addr >> 8) & 0xFF;
351 xaddr[3] = addr & 0xFF;
352
353 if (wait_for_bb()) {
354 printf("i2c_write: bus is busy\n");
355 goto Done;
356 }
357
358 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
359 if (do_address(chip, 0)) {
360 printf("i2c_write: failed to address chip\n");
361 goto Done;
362 }
363
364 if (send_bytes(chip, &xaddr[4-alen], alen)) {
365 printf("i2c_write: send_bytes failed\n");
366 goto Done;
367 }
368
369 if (send_bytes(chip, buf, len)) {
370 printf("i2c_write: send_bytes failed\n");
371 goto Done;
372 }
373
374 ret = 0;
375 Done:
376 mpc_reg_out(&regs->mcr, 0, I2C_STA);
377 return ret;
378 }
379
380 uchar i2c_reg_read(uchar chip, uchar reg)
381 {
382 char buf;
383
384 i2c_read(chip, reg, 1, &buf, 1);
385
386 return buf;
387 }
388
389 void i2c_reg_write(uchar chip, uchar reg, uchar val)
390 {
391 i2c_write(chip, reg, 1, &val, 1);
392
393 return;
394 }
395
396 #endif /* CONFIG_HARD_I2C */