]> git.ipfire.org Git - people/ms/u-boot.git/blame - cpu/mpc5xxx/i2c.c
* Patches by Xianghua Xiao, 15 Oct 2003:
[people/ms/u-boot.git] / cpu / mpc5xxx / i2c.c
CommitLineData
531716e1
WD
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
ab209d51 31#if (CFG_I2C_MODULE == 2)
531716e1 32#define I2C_BASE MPC5XXX_I2C2
ab209d51 33#elif (CFG_I2C_MODULE == 1)
531716e1 34#define I2C_BASE MPC5XXX_I2C1
ab209d51 35#else
36#error CFG_I2C_MODULE is not properly configured
531716e1
WD
37#endif
38
39#define I2C_TIMEOUT 100
40#define I2C_RETRIES 3
41
ab209d51 42struct mpc5xxx_i2c_tap {
43 int scl2tap;
44 int tap2tap;
45};
46
531716e1
WD
47static 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);
ab209d51 54static int mpc_get_fdr (int);
531716e1
WD
55
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
42d1f039
WD
136 if (wait_for_pin(&status)) {
137 return -2;
138 }
531716e1 139
42d1f039
WD
140 if (status & I2C_RXAK) {
141 return -3;
142 }
531716e1
WD
143
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 */
ab209d51 218 mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
531716e1
WD
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
ab209d51 228static int mpc_get_fdr(int speed)
229{
230 DECLARE_GLOBAL_DATA_PTR;
231 static int fdr = -1;
232 static int best_speed = 0;
233
234 if (fdr == -1) {
235 ulong ipb, scl;
236 ulong bestmatch = 0xffffffffUL;
237 int best_i = 0, best_j = 0, i, j;
238 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
239 struct mpc5xxx_i2c_tap scltap[] = {
240 {4, 1},
241 {4, 2},
242 {6, 4},
243 {6, 8},
244 {14, 16},
245 {30, 32},
246 {62, 64},
247 {126, 128}
248 };
249
250 ipb = gd->ipb_clk;
251 for (i = 7; i >= 0; i--) {
252 for (j = 7; j >= 0; j--) {
42d1f039 253 scl = 2 * (scltap[j].scl2tap +
ab209d51 254 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
255 if (ipb <= speed*scl) {
256 if ((speed*scl - ipb) < bestmatch) {
257 bestmatch = speed*scl - ipb;
258 best_i = i;
259 best_j = j;
260 best_speed = ipb/scl;
261 }
262 }
263 }
264 }
265 fdr = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
266 printf("%d kHz, ", best_speed / 1000);
267 }
268
269 return fdr;
270}
271
531716e1
WD
272int i2c_probe(uchar chip)
273{
274 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
275 int i;
276
277 for (i = 0; i < I2C_RETRIES; i++) {
278 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
279
280 if (! do_address(chip, 0)) {
281 mpc_reg_out(&regs->mcr, 0, I2C_STA);
282 break;
283 }
284
285 mpc_reg_out(&regs->mcr, 0, I2C_STA);
286 udelay(500);
287 }
288
289 return (i == I2C_RETRIES);
290}
291
292int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
293{
294 uchar xaddr[4];
295 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
296 int ret = -1;
297
298 xaddr[0] = (addr >> 24) & 0xFF;
299 xaddr[1] = (addr >> 16) & 0xFF;
300 xaddr[2] = (addr >> 8) & 0xFF;
301 xaddr[3] = addr & 0xFF;
302
303 if (wait_for_bb()) {
304 printf("i2c_read: bus is busy\n");
305 goto Done;
306 }
307
308 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
309 if (do_address(chip, 0)) {
310 printf("i2c_read: failed to address chip\n");
311 goto Done;
312 }
313
314 if (send_bytes(chip, &xaddr[4-alen], alen)) {
315 printf("i2c_read: send_bytes failed\n");
316 goto Done;
317 }
318
319 mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
320 if (do_address(chip, 1)) {
321 printf("i2c_read: failed to address chip\n");
322 goto Done;
323 }
324
325 if (receive_bytes(chip, buf, len)) {
326 printf("i2c_read: receive_bytes failed\n");
327 goto Done;
328 }
329
330 ret = 0;
331Done:
332 mpc_reg_out(&regs->mcr, 0, I2C_STA);
333 return ret;
334}
335
336int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
337{
338 uchar xaddr[4];
339 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
340 int ret = -1;
341
342 xaddr[0] = (addr >> 24) & 0xFF;
343 xaddr[1] = (addr >> 16) & 0xFF;
344 xaddr[2] = (addr >> 8) & 0xFF;
345 xaddr[3] = addr & 0xFF;
346
42d1f039 347 if (wait_for_bb()) {
531716e1
WD
348 printf("i2c_write: bus is busy\n");
349 goto Done;
350 }
351
42d1f039
WD
352 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
353 if (do_address(chip, 0)) {
531716e1
WD
354 printf("i2c_write: failed to address chip\n");
355 goto Done;
356 }
357
358 if (send_bytes(chip, &xaddr[4-alen], alen)) {
359 printf("i2c_write: send_bytes failed\n");
360 goto Done;
361 }
362
363 if (send_bytes(chip, buf, len)) {
364 printf("i2c_write: send_bytes failed\n");
365 goto Done;
366 }
367
368 ret = 0;
369Done:
370 mpc_reg_out(&regs->mcr, 0, I2C_STA);
371 return ret;
372}
373
374uchar i2c_reg_read(uchar chip, uchar reg)
375{
376 char buf;
377
378 i2c_read(chip, reg, 1, &buf, 1);
379
380 return buf;
381}
382
383void i2c_reg_write(uchar chip, uchar reg, uchar val)
384{
385 i2c_write(chip, reg, 1, &val, 1);
386
387 return;
388}
389
390#endif /* CONFIG_HARD_I2C */