blob: 23419324ba592b6091fef2081d9ec9aec09605b6 [file] [log] [blame]
wdenk25521902003-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
Wolfgang Denk6405a152006-03-31 18:32:53 +020026DECLARE_GLOBAL_DATA_PTR;
27
wdenk25521902003-09-13 19:01:12 +000028#ifdef CONFIG_HARD_I2C
29
30#include <mpc5xxx.h>
31#include <i2c.h>
32
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020033#if (CONFIG_SYS_I2C_MODULE == 2)
wdenk25521902003-09-13 19:01:12 +000034#define I2C_BASE MPC5XXX_I2C2
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035#elif (CONFIG_SYS_I2C_MODULE == 1)
wdenk25521902003-09-13 19:01:12 +000036#define I2C_BASE MPC5XXX_I2C1
dzu62177922003-09-30 14:08:43 +000037#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038#error CONFIG_SYS_I2C_MODULE is not properly configured
wdenk25521902003-09-13 19:01:12 +000039#endif
40
Jon Smirlc831da92009-04-04 17:44:51 -040041#define I2C_TIMEOUT 6667
wdenk25521902003-09-13 19:01:12 +000042#define I2C_RETRIES 3
43
dzu62177922003-09-30 14:08:43 +000044struct mpc5xxx_i2c_tap {
45 int scl2tap;
46 int tap2tap;
47};
48
wdenk25521902003-09-13 19:01:12 +000049static int mpc_reg_in (volatile u32 *reg);
50static void mpc_reg_out (volatile u32 *reg, int val, int mask);
51static int wait_for_bb (void);
52static int wait_for_pin (int *status);
53static int do_address (uchar chip, char rdwr_flag);
54static int send_bytes (uchar chip, char *buf, int len);
55static int receive_bytes (uchar chip, char *buf, int len);
dzu62177922003-09-30 14:08:43 +000056static int mpc_get_fdr (int);
wdenk25521902003-09-13 19:01:12 +000057
58static int mpc_reg_in(volatile u32 *reg)
59{
Wolfgang Denk7fb52662005-10-13 16:45:02 +020060 int ret = *reg >> 24;
wdenk25521902003-09-13 19:01:12 +000061 __asm__ __volatile__ ("eieio");
Wolfgang Denk7fb52662005-10-13 16:45:02 +020062 return ret;
wdenk25521902003-09-13 19:01:12 +000063}
64
65static void mpc_reg_out(volatile u32 *reg, int val, int mask)
66{
67 int tmp;
68
69 if (!mask) {
70 *reg = val << 24;
71 } else {
72 tmp = mpc_reg_in(reg);
73 *reg = ((tmp & ~mask) | (val & mask)) << 24;
74 }
75 __asm__ __volatile__ ("eieio");
76
77 return;
78}
79
80static int wait_for_bb(void)
81{
82 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
83 int timeout = I2C_TIMEOUT;
84 int status;
85
86 status = mpc_reg_in(&regs->msr);
87
88 while (timeout-- && (status & I2C_BB)) {
89#if 1
90 volatile int temp;
91 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
92 temp = mpc_reg_in(&regs->mdr);
93 mpc_reg_out(&regs->mcr, 0, I2C_STA);
94 mpc_reg_out(&regs->mcr, 0, 0);
95 mpc_reg_out(&regs->mcr, I2C_EN, 0);
96#endif
Jon Smirlc831da92009-04-04 17:44:51 -040097 udelay(15);
wdenk25521902003-09-13 19:01:12 +000098 status = mpc_reg_in(&regs->msr);
99 }
100
101 return (status & I2C_BB);
102}
103
104static int wait_for_pin(int *status)
105{
106 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
107 int timeout = I2C_TIMEOUT;
108
109 *status = mpc_reg_in(&regs->msr);
110
111 while (timeout-- && !(*status & I2C_IF)) {
Jon Smirlc831da92009-04-04 17:44:51 -0400112 udelay(15);
wdenk25521902003-09-13 19:01:12 +0000113 *status = mpc_reg_in(&regs->msr);
114 }
115
116 if (!(*status & I2C_IF)) {
117 return -1;
118 }
119
120 mpc_reg_out(&regs->msr, 0, I2C_IF);
121
122 return 0;
123}
124
125static int do_address(uchar chip, char rdwr_flag)
126{
127 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
128 int status;
129
130 chip <<= 1;
131
132 if (rdwr_flag) {
133 chip |= 1;
134 }
135
136 mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
137 mpc_reg_out(&regs->mdr, chip, 0);
138
wdenk9c53f402003-10-15 23:53:47 +0000139 if (wait_for_pin(&status)) {
140 return -2;
141 }
wdenk25521902003-09-13 19:01:12 +0000142
wdenk9c53f402003-10-15 23:53:47 +0000143 if (status & I2C_RXAK) {
144 return -3;
145 }
wdenk25521902003-09-13 19:01:12 +0000146
147 return 0;
148}
149
150static int send_bytes(uchar chip, char *buf, int len)
151{
152 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
153 int wrcount;
154 int status;
155
156 for (wrcount = 0; wrcount < len; ++wrcount) {
157
158 mpc_reg_out(&regs->mdr, buf[wrcount], 0);
159
160 if (wait_for_pin(&status)) {
161 break;
162 }
163
164 if (status & I2C_RXAK) {
165 break;
166 }
167
168 }
169
170 return !(wrcount == len);
171}
172
173static int receive_bytes(uchar chip, char *buf, int len)
174{
175 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
176 int dummy = 1;
177 int rdcount = 0;
178 int status;
179 int i;
180
181 mpc_reg_out(&regs->mcr, 0, I2C_TX);
182
183 for (i = 0; i < len; ++i) {
184 buf[rdcount] = mpc_reg_in(&regs->mdr);
185
186 if (dummy) {
187 dummy = 0;
188 } else {
189 rdcount++;
190 }
191
192
193 if (wait_for_pin(&status)) {
194 return -4;
195 }
196 }
197
198 mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
199 buf[rdcount++] = mpc_reg_in(&regs->mdr);
200
201 if (wait_for_pin(&status)) {
202 return -5;
203 }
204
205 mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
206
207 return 0;
208}
209
210/**************** I2C API ****************/
211
212void i2c_init(int speed, int saddr)
213{
214 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
215
216 mpc_reg_out(&regs->mcr, 0, 0);
217 mpc_reg_out(&regs->madr, saddr << 1, 0);
218
219 /* Set clock
220 */
dzu62177922003-09-30 14:08:43 +0000221 mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
wdenk25521902003-09-13 19:01:12 +0000222
223 /* Enable module
224 */
225 mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
226 mpc_reg_out(&regs->msr, 0, I2C_IF);
227
228 return;
229}
230
dzu62177922003-09-30 14:08:43 +0000231static int mpc_get_fdr(int speed)
232{
dzu62177922003-09-30 14:08:43 +0000233 static int fdr = -1;
dzu62177922003-09-30 14:08:43 +0000234
235 if (fdr == -1) {
wdenk4b16c2e2003-11-07 13:42:26 +0000236 ulong best_speed = 0;
237 ulong divider;
dzu62177922003-09-30 14:08:43 +0000238 ulong ipb, scl;
239 ulong bestmatch = 0xffffffffUL;
240 int best_i = 0, best_j = 0, i, j;
241 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
242 struct mpc5xxx_i2c_tap scltap[] = {
243 {4, 1},
244 {4, 2},
245 {6, 4},
246 {6, 8},
247 {14, 16},
248 {30, 32},
249 {62, 64},
250 {126, 128}
251 };
252
253 ipb = gd->ipb_clk;
254 for (i = 7; i >= 0; i--) {
255 for (j = 7; j >= 0; j--) {
wdenk9c53f402003-10-15 23:53:47 +0000256 scl = 2 * (scltap[j].scl2tap +
dzu62177922003-09-30 14:08:43 +0000257 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
258 if (ipb <= speed*scl) {
259 if ((speed*scl - ipb) < bestmatch) {
260 bestmatch = speed*scl - ipb;
261 best_i = i;
262 best_j = j;
263 best_speed = ipb/scl;
264 }
265 }
266 }
267 }
wdenk4b16c2e2003-11-07 13:42:26 +0000268 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
269 if (gd->flags & GD_FLG_RELOC) {
270 fdr = divider;
271 } else {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400272 if (gd->have_console)
273 printf("%ld kHz, ", best_speed / 1000);
wdenk4b16c2e2003-11-07 13:42:26 +0000274 return divider;
275 }
dzu62177922003-09-30 14:08:43 +0000276 }
277
278 return fdr;
279}
280
wdenk25521902003-09-13 19:01:12 +0000281int i2c_probe(uchar chip)
282{
283 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
284 int i;
285
286 for (i = 0; i < I2C_RETRIES; i++) {
287 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
288
289 if (! do_address(chip, 0)) {
290 mpc_reg_out(&regs->mcr, 0, I2C_STA);
wdenk604b7a12004-06-09 15:29:49 +0000291 udelay(500);
wdenk25521902003-09-13 19:01:12 +0000292 break;
293 }
294
295 mpc_reg_out(&regs->mcr, 0, I2C_STA);
296 udelay(500);
297 }
298
299 return (i == I2C_RETRIES);
300}
301
302int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
303{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200304 char xaddr[4];
wdenk25521902003-09-13 19:01:12 +0000305 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
306 int ret = -1;
307
308 xaddr[0] = (addr >> 24) & 0xFF;
309 xaddr[1] = (addr >> 16) & 0xFF;
310 xaddr[2] = (addr >> 8) & 0xFF;
311 xaddr[3] = addr & 0xFF;
312
313 if (wait_for_bb()) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400314 if (gd->have_console)
315 printf("i2c_read: bus is busy\n");
wdenk25521902003-09-13 19:01:12 +0000316 goto Done;
317 }
318
319 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
320 if (do_address(chip, 0)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400321 if (gd->have_console)
322 printf("i2c_read: failed to address chip\n");
wdenk25521902003-09-13 19:01:12 +0000323 goto Done;
324 }
325
326 if (send_bytes(chip, &xaddr[4-alen], alen)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400327 if (gd->have_console)
328 printf("i2c_read: send_bytes failed\n");
wdenk25521902003-09-13 19:01:12 +0000329 goto Done;
330 }
331
332 mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
333 if (do_address(chip, 1)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400334 if (gd->have_console)
335 printf("i2c_read: failed to address chip\n");
wdenk25521902003-09-13 19:01:12 +0000336 goto Done;
337 }
338
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200339 if (receive_bytes(chip, (char *)buf, len)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400340 if (gd->have_console)
341 printf("i2c_read: receive_bytes failed\n");
wdenk25521902003-09-13 19:01:12 +0000342 goto Done;
343 }
344
345 ret = 0;
346Done:
347 mpc_reg_out(&regs->mcr, 0, I2C_STA);
348 return ret;
349}
350
351int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
352{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200353 char xaddr[4];
wdenk25521902003-09-13 19:01:12 +0000354 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
355 int ret = -1;
356
357 xaddr[0] = (addr >> 24) & 0xFF;
358 xaddr[1] = (addr >> 16) & 0xFF;
359 xaddr[2] = (addr >> 8) & 0xFF;
360 xaddr[3] = addr & 0xFF;
361
wdenk9c53f402003-10-15 23:53:47 +0000362 if (wait_for_bb()) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400363 if (gd->have_console)
364 printf("i2c_write: bus is busy\n");
wdenk25521902003-09-13 19:01:12 +0000365 goto Done;
366 }
367
wdenk9c53f402003-10-15 23:53:47 +0000368 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
369 if (do_address(chip, 0)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400370 if (gd->have_console)
371 printf("i2c_write: failed to address chip\n");
wdenk25521902003-09-13 19:01:12 +0000372 goto Done;
373 }
374
375 if (send_bytes(chip, &xaddr[4-alen], alen)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400376 if (gd->have_console)
377 printf("i2c_write: send_bytes failed\n");
wdenk25521902003-09-13 19:01:12 +0000378 goto Done;
379 }
380
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200381 if (send_bytes(chip, (char *)buf, len)) {
Jon Smirlcb2a2502009-03-22 22:55:45 -0400382 if (gd->have_console)
383 printf("i2c_write: send_bytes failed\n");
wdenk25521902003-09-13 19:01:12 +0000384 goto Done;
385 }
386
387 ret = 0;
388Done:
389 mpc_reg_out(&regs->mcr, 0, I2C_STA);
390 return ret;
391}
392
wdenk25521902003-09-13 19:01:12 +0000393#endif /* CONFIG_HARD_I2C */