blob: 845f7c05eff51356eed4777e3fe68b6a6bc26f63 [file] [log] [blame]
wdenk531716e2003-09-13 19:01:12 +00001/*
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
dzuab209d52003-09-30 14:08:43 +000031#if (CFG_I2C_MODULE == 2)
wdenk531716e2003-09-13 19:01:12 +000032#define I2C_BASE MPC5XXX_I2C2
dzuab209d52003-09-30 14:08:43 +000033#elif (CFG_I2C_MODULE == 1)
wdenk531716e2003-09-13 19:01:12 +000034#define I2C_BASE MPC5XXX_I2C1
dzuab209d52003-09-30 14:08:43 +000035#else
36#error CFG_I2C_MODULE is not properly configured
wdenk531716e2003-09-13 19:01:12 +000037#endif
38
39#define I2C_TIMEOUT 100
40#define I2C_RETRIES 3
41
dzuab209d52003-09-30 14:08:43 +000042struct mpc5xxx_i2c_tap {
43 int scl2tap;
44 int tap2tap;
45};
46
wdenk531716e2003-09-13 19:01:12 +000047static int mpc_reg_in (volatile u32 *reg);
48static void mpc_reg_out (volatile u32 *reg, int val, int mask);
49static int wait_for_bb (void);
50static int wait_for_pin (int *status);
51static int do_address (uchar chip, char rdwr_flag);
52static int send_bytes (uchar chip, char *buf, int len);
53static int receive_bytes (uchar chip, char *buf, int len);
dzuab209d52003-09-30 14:08:43 +000054static int mpc_get_fdr (int);
wdenk531716e2003-09-13 19:01:12 +000055
56static int mpc_reg_in(volatile u32 *reg)
57{
58 return *reg >> 24;
59 __asm__ __volatile__ ("eieio");
60}
61
62static 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
77static 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
101static 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
122static 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
wdenk42d1f032003-10-15 23:53:47 +0000136 if (wait_for_pin(&status)) {
137 return -2;
138 }
wdenk531716e2003-09-13 19:01:12 +0000139
wdenk42d1f032003-10-15 23:53:47 +0000140 if (status & I2C_RXAK) {
141 return -3;
142 }
wdenk531716e2003-09-13 19:01:12 +0000143
144 return 0;
145}
146
147static 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
170static 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
209void 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 */
dzuab209d52003-09-30 14:08:43 +0000218 mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
wdenk531716e2003-09-13 19:01:12 +0000219
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
dzuab209d52003-09-30 14:08:43 +0000228static int mpc_get_fdr(int speed)
229{
230 DECLARE_GLOBAL_DATA_PTR;
231 static int fdr = -1;
dzuab209d52003-09-30 14:08:43 +0000232
233 if (fdr == -1) {
wdenk5cf9da42003-11-07 13:42:26 +0000234 ulong best_speed = 0;
235 ulong divider;
dzuab209d52003-09-30 14:08:43 +0000236 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--) {
wdenk42d1f032003-10-15 23:53:47 +0000254 scl = 2 * (scltap[j].scl2tap +
dzuab209d52003-09-30 14:08:43 +0000255 (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 }
wdenk5cf9da42003-11-07 13:42:26 +0000266 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 }
dzuab209d52003-09-30 14:08:43 +0000273 }
274
275 return fdr;
276}
277
wdenk531716e2003-09-13 19:01:12 +0000278int 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);
wdenk697037f2004-06-09 15:29:49 +0000288 udelay(500);
wdenk531716e2003-09-13 19:01:12 +0000289 break;
290 }
291
292 mpc_reg_out(&regs->mcr, 0, I2C_STA);
293 udelay(500);
294 }
295
296 return (i == I2C_RETRIES);
297}
298
299int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
300{
301 uchar xaddr[4];
302 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
303 int ret = -1;
304
305 xaddr[0] = (addr >> 24) & 0xFF;
306 xaddr[1] = (addr >> 16) & 0xFF;
307 xaddr[2] = (addr >> 8) & 0xFF;
308 xaddr[3] = addr & 0xFF;
309
310 if (wait_for_bb()) {
311 printf("i2c_read: bus is busy\n");
312 goto Done;
313 }
314
315 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
316 if (do_address(chip, 0)) {
317 printf("i2c_read: failed to address chip\n");
318 goto Done;
319 }
320
321 if (send_bytes(chip, &xaddr[4-alen], alen)) {
322 printf("i2c_read: send_bytes failed\n");
323 goto Done;
324 }
325
326 mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
327 if (do_address(chip, 1)) {
328 printf("i2c_read: failed to address chip\n");
329 goto Done;
330 }
331
332 if (receive_bytes(chip, buf, len)) {
333 printf("i2c_read: receive_bytes failed\n");
334 goto Done;
335 }
336
337 ret = 0;
338Done:
339 mpc_reg_out(&regs->mcr, 0, I2C_STA);
340 return ret;
341}
342
343int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
344{
345 uchar xaddr[4];
346 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
347 int ret = -1;
348
349 xaddr[0] = (addr >> 24) & 0xFF;
350 xaddr[1] = (addr >> 16) & 0xFF;
351 xaddr[2] = (addr >> 8) & 0xFF;
352 xaddr[3] = addr & 0xFF;
353
wdenk42d1f032003-10-15 23:53:47 +0000354 if (wait_for_bb()) {
wdenk531716e2003-09-13 19:01:12 +0000355 printf("i2c_write: bus is busy\n");
356 goto Done;
357 }
358
wdenk42d1f032003-10-15 23:53:47 +0000359 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
360 if (do_address(chip, 0)) {
wdenk531716e2003-09-13 19:01:12 +0000361 printf("i2c_write: failed to address chip\n");
362 goto Done;
363 }
364
365 if (send_bytes(chip, &xaddr[4-alen], alen)) {
366 printf("i2c_write: send_bytes failed\n");
367 goto Done;
368 }
369
370 if (send_bytes(chip, buf, len)) {
371 printf("i2c_write: send_bytes failed\n");
372 goto Done;
373 }
374
375 ret = 0;
376Done:
377 mpc_reg_out(&regs->mcr, 0, I2C_STA);
378 return ret;
379}
380
381uchar i2c_reg_read(uchar chip, uchar reg)
382{
383 char buf;
384
385 i2c_read(chip, reg, 1, &buf, 1);
386
387 return buf;
388}
389
390void i2c_reg_write(uchar chip, uchar reg, uchar val)
391{
392 i2c_write(chip, reg, 1, &val, 1);
393
394 return;
395}
396
397#endif /* CONFIG_HARD_I2C */