blob: 2f35d20c87e1929017adf1229fdcd9706f70e597 [file] [log] [blame]
wdenk983fda82004-10-28 00:09:35 +00001/*
2 * (C) Copyright 2004, Freescale, Inc
3 * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
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 Denkd87080b2006-03-31 18:32:53 +020026DECLARE_GLOBAL_DATA_PTR;
27
wdenk983fda82004-10-28 00:09:35 +000028#ifdef CONFIG_HARD_I2C
29
30#include <mpc8220.h>
31#include <i2c.h>
32
33typedef struct mpc8220_i2c {
34 volatile u32 adr; /* I2Cn + 0x00 */
35 volatile u32 fdr; /* I2Cn + 0x04 */
36 volatile u32 cr; /* I2Cn + 0x08 */
37 volatile u32 sr; /* I2Cn + 0x0C */
38 volatile u32 dr; /* I2Cn + 0x10 */
39} i2c_t;
40
41/* I2Cn control register bits */
42#define I2C_EN 0x80
43#define I2C_IEN 0x40
44#define I2C_STA 0x20
45#define I2C_TX 0x10
46#define I2C_TXAK 0x08
47#define I2C_RSTA 0x04
48#define I2C_INIT_MASK (I2C_EN | I2C_STA | I2C_TX | I2C_RSTA)
49
50/* I2Cn status register bits */
51#define I2C_CF 0x80
52#define I2C_AAS 0x40
53#define I2C_BB 0x20
54#define I2C_AL 0x10
55#define I2C_SRW 0x04
56#define I2C_IF 0x02
57#define I2C_RXAK 0x01
58
59#define I2C_TIMEOUT 100
60#define I2C_RETRIES 1
61
62struct mpc8220_i2c_tap {
63 int scl2tap;
64 int tap2tap;
65};
66
67static int mpc_reg_in (volatile u32 * reg);
68static void mpc_reg_out (volatile u32 * reg, int val, int mask);
69static int wait_for_bb (void);
70static int wait_for_pin (int *status);
71static int do_address (uchar chip, char rdwr_flag);
72static int send_bytes (uchar chip, char *buf, int len);
73static int receive_bytes (uchar chip, char *buf, int len);
74static int mpc_get_fdr (int);
75
76static int mpc_reg_in (volatile u32 * reg)
77{
Wolfgang Denk77ddac92005-10-13 16:45:02 +020078 int ret;
79 ret = *reg >> 24;
wdenk983fda82004-10-28 00:09:35 +000080 __asm__ __volatile__ ("eieio");
Wolfgang Denk77ddac92005-10-13 16:45:02 +020081 return ret;
wdenk983fda82004-10-28 00:09:35 +000082}
83
84static void mpc_reg_out (volatile u32 * reg, int val, int mask)
85{
86 int tmp;
87
88 if (!mask) {
89 *reg = val << 24;
90 } else {
91 tmp = mpc_reg_in (reg);
92 *reg = ((tmp & ~mask) | (val & mask)) << 24;
93 }
94 __asm__ __volatile__ ("eieio");
95
96 return;
97}
98
99static int wait_for_bb (void)
100{
101 i2c_t *regs = (i2c_t *) MMAP_I2C;
102 int timeout = I2C_TIMEOUT;
103 int status;
104
105 status = mpc_reg_in (&regs->sr);
106
107 while (timeout-- && (status & I2C_BB)) {
wdenk983fda82004-10-28 00:09:35 +0000108
109 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
Wolfgang Denk8b337be2011-11-04 15:55:58 +0000110 (void)mpc_reg_in (&regs->dr);
wdenk983fda82004-10-28 00:09:35 +0000111 mpc_reg_out (&regs->cr, 0, I2C_STA);
112 mpc_reg_out (&regs->cr, 0, 0);
113 mpc_reg_out (&regs->cr, I2C_EN, 0);
Wolfgang Denk8b337be2011-11-04 15:55:58 +0000114
wdenk983fda82004-10-28 00:09:35 +0000115 udelay (1000);
116 status = mpc_reg_in (&regs->sr);
117 }
118
119 return (status & I2C_BB);
120}
121
122static int wait_for_pin (int *status)
123{
124 i2c_t *regs = (i2c_t *) MMAP_I2C;
125 int timeout = I2C_TIMEOUT;
126
127 *status = mpc_reg_in (&regs->sr);
128
129 while (timeout-- && !(*status & I2C_IF)) {
130 udelay (1000);
131 *status = mpc_reg_in (&regs->sr);
132 }
133
134 if (!(*status & I2C_IF)) {
135 return -1;
136 }
137
138 mpc_reg_out (&regs->sr, 0, I2C_IF);
139 return 0;
140}
141
142static int do_address (uchar chip, char rdwr_flag)
143{
144 i2c_t *regs = (i2c_t *) MMAP_I2C;
145 int status;
146
147 chip <<= 1;
148
149 if (rdwr_flag)
150 chip |= 1;
151
152 mpc_reg_out (&regs->cr, I2C_TX, I2C_TX);
153 mpc_reg_out (&regs->dr, chip, 0);
154
155 if (wait_for_pin (&status))
156 return -2;
157 if (status & I2C_RXAK)
158 return -3;
159 return 0;
160}
161
162static int send_bytes (uchar chip, char *buf, int len)
163{
164 i2c_t *regs = (i2c_t *) MMAP_I2C;
165 int wrcount;
166 int status;
167
168 for (wrcount = 0; wrcount < len; ++wrcount) {
169
170 mpc_reg_out (&regs->dr, buf[wrcount], 0);
171
172 if (wait_for_pin (&status))
173 break;
174
175 if (status & I2C_RXAK)
176 break;
177
178 }
179
180 return !(wrcount == len);
181 return 0;
182}
183
184static int receive_bytes (uchar chip, char *buf, int len)
185{
186 i2c_t *regs = (i2c_t *) MMAP_I2C;
187 int dummy = 1;
188 int rdcount = 0;
189 int status;
190 int i;
191
192 mpc_reg_out (&regs->cr, 0, I2C_TX);
193
194 for (i = 0; i < len; ++i) {
195 buf[rdcount] = mpc_reg_in (&regs->dr);
196
197 if (dummy)
198 dummy = 0;
199 else
200 rdcount++;
201
202 if (wait_for_pin (&status))
203 return -4;
204 }
205
206 mpc_reg_out (&regs->cr, I2C_TXAK, I2C_TXAK);
207 buf[rdcount++] = mpc_reg_in (&regs->dr);
208
209 if (wait_for_pin (&status))
210 return -5;
211
212 mpc_reg_out (&regs->cr, 0, I2C_TXAK);
213 return 0;
214}
215
216/**************** I2C API ****************/
217
218void i2c_init (int speed, int saddr)
219{
220 i2c_t *regs = (i2c_t *) MMAP_I2C;
221
222 mpc_reg_out (&regs->cr, 0, 0);
223 mpc_reg_out (&regs->adr, saddr << 1, 0);
224
225 /* Set clock
226 */
227 mpc_reg_out (&regs->fdr, mpc_get_fdr (speed), 0);
228
229 /* Enable module
230 */
231 mpc_reg_out (&regs->cr, I2C_EN, I2C_INIT_MASK);
232 mpc_reg_out (&regs->sr, 0, I2C_IF);
233 return;
234}
235
236static int mpc_get_fdr (int speed)
237{
wdenk983fda82004-10-28 00:09:35 +0000238 static int fdr = -1;
239
240 if (fdr == -1) {
241 ulong best_speed = 0;
242 ulong divider;
243 ulong ipb, scl;
244 ulong bestmatch = 0xffffffffUL;
245 int best_i = 0, best_j = 0, i, j;
246 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8 };
247 struct mpc8220_i2c_tap scltap[] = {
248 {4, 1},
249 {4, 2},
250 {6, 4},
251 {6, 8},
252 {14, 16},
253 {30, 32},
254 {62, 64},
255 {126, 128}
256 };
257
258 ipb = gd->bus_clk;
259 for (i = 7; i >= 0; i--) {
260 for (j = 7; j >= 0; j--) {
261 scl = 2 * (scltap[j].scl2tap +
262 (SCL_Tap[i] -
263 1) * scltap[j].tap2tap + 2);
264 if (ipb <= speed * scl) {
265 if ((speed * scl - ipb) < bestmatch) {
266 bestmatch = speed * scl - ipb;
267 best_i = i;
268 best_j = j;
269 best_speed = ipb / scl;
270 }
271 }
272 }
273 }
274 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
275 if (gd->flags & GD_FLG_RELOC) {
276 fdr = divider;
277 } else {
278 printf ("%ld kHz, ", best_speed / 1000);
279 return divider;
280 }
281 }
282
283 return fdr;
284}
285
286int i2c_probe (uchar chip)
287{
288 i2c_t *regs = (i2c_t *) MMAP_I2C;
289 int i;
290
291 for (i = 0; i < I2C_RETRIES; i++) {
292 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
293
294 if (!do_address (chip, 0)) {
295 mpc_reg_out (&regs->cr, 0, I2C_STA);
296 break;
297 }
298
299 mpc_reg_out (&regs->cr, 0, I2C_STA);
300 udelay (50);
301 }
302
303 return (i == I2C_RETRIES);
304}
305
306int i2c_read (uchar chip, uint addr, int alen, uchar * buf, int len)
307{
308 uchar xaddr[4];
309 i2c_t *regs = (i2c_t *) MMAP_I2C;
310 int ret = -1;
311
312 xaddr[0] = (addr >> 24) & 0xFF;
313 xaddr[1] = (addr >> 16) & 0xFF;
314 xaddr[2] = (addr >> 8) & 0xFF;
315 xaddr[3] = addr & 0xFF;
316
317 if (wait_for_bb ()) {
318 printf ("i2c_read: bus is busy\n");
319 goto Done;
320 }
321
322 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
323 if (do_address (chip, 0)) {
324 printf ("i2c_read: failed to address chip\n");
325 goto Done;
326 }
327
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200328 if (send_bytes (chip, (char *)&xaddr[4 - alen], alen)) {
wdenk983fda82004-10-28 00:09:35 +0000329 printf ("i2c_read: send_bytes failed\n");
330 goto Done;
331 }
332
333 mpc_reg_out (&regs->cr, I2C_RSTA, I2C_RSTA);
334 if (do_address (chip, 1)) {
335 printf ("i2c_read: failed to address chip\n");
336 goto Done;
337 }
338
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200339 if (receive_bytes (chip, (char *)buf, len)) {
wdenk983fda82004-10-28 00:09:35 +0000340 printf ("i2c_read: receive_bytes failed\n");
341 goto Done;
342 }
343
344 ret = 0;
345 Done:
346 mpc_reg_out (&regs->cr, 0, I2C_STA);
347 return ret;
348}
349
350int i2c_write (uchar chip, uint addr, int alen, uchar * buf, int len)
351{
352 uchar xaddr[4];
353 i2c_t *regs = (i2c_t *) MMAP_I2C;
354 int ret = -1;
355
356 xaddr[0] = (addr >> 24) & 0xFF;
357 xaddr[1] = (addr >> 16) & 0xFF;
358 xaddr[2] = (addr >> 8) & 0xFF;
359 xaddr[3] = addr & 0xFF;
360
361 if (wait_for_bb ()) {
362 printf ("i2c_write: bus is busy\n");
363 goto Done;
364 }
365
366 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
367 if (do_address (chip, 0)) {
368 printf ("i2c_write: failed to address chip\n");
369 goto Done;
370 }
371
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200372 if (send_bytes (chip, (char *)&xaddr[4 - alen], alen)) {
wdenk983fda82004-10-28 00:09:35 +0000373 printf ("i2c_write: send_bytes failed\n");
374 goto Done;
375 }
376
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200377 if (send_bytes (chip, (char *)buf, len)) {
wdenk983fda82004-10-28 00:09:35 +0000378 printf ("i2c_write: send_bytes failed\n");
379 goto Done;
380 }
381
382 ret = 0;
383 Done:
384 mpc_reg_out (&regs->cr, 0, I2C_STA);
385 return ret;
386}
387
wdenk983fda82004-10-28 00:09:35 +0000388#endif /* CONFIG_HARD_I2C */