3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CONFIG_HARD_I2C
33 #if !defined(CONFIG_I2C_MULTI_BUS)
34 #if (CONFIG_SYS_I2C_MODULE == 2)
35 #define I2C_BASE MPC5XXX_I2C2
36 #elif (CONFIG_SYS_I2C_MODULE == 1)
37 #define I2C_BASE MPC5XXX_I2C1
39 #error CONFIG_SYS_I2C_MODULE is not properly configured
42 static unsigned int i2c_bus_num __attribute__ ((section (".data"))) =
43 CONFIG_SYS_SPD_BUS_NUM;
44 static unsigned int i2c_bus_speed[2] = {CONFIG_SYS_I2C_SPEED,
45 CONFIG_SYS_I2C_SPEED};
47 static const unsigned long i2c_dev[2] = {
52 #define I2C_BASE ((struct mpc5xxx_i2c *)i2c_dev[i2c_bus_num])
55 #define I2C_TIMEOUT 6667
58 struct mpc5xxx_i2c_tap {
63 static int mpc_reg_in (volatile u32 *reg);
64 static void mpc_reg_out (volatile u32 *reg, int val, int mask);
65 static int wait_for_bb (void);
66 static int wait_for_pin (int *status);
67 static int do_address (uchar chip, char rdwr_flag);
68 static int send_bytes (uchar chip, char *buf, int len);
69 static int receive_bytes (uchar chip, char *buf, int len);
70 static int mpc_get_fdr (int);
72 static int mpc_reg_in(volatile u32 *reg)
75 __asm__ __volatile__ ("eieio");
79 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
86 tmp = mpc_reg_in(reg);
87 *reg = ((tmp & ~mask) | (val & mask)) << 24;
89 __asm__ __volatile__ ("eieio");
94 static int wait_for_bb(void)
96 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
97 int timeout = I2C_TIMEOUT;
100 status = mpc_reg_in(®s->msr);
102 while (timeout-- && (status & I2C_BB)) {
105 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
106 temp = mpc_reg_in(®s->mdr);
107 mpc_reg_out(®s->mcr, 0, I2C_STA);
108 mpc_reg_out(®s->mcr, 0, 0);
109 mpc_reg_out(®s->mcr, I2C_EN, 0);
112 status = mpc_reg_in(®s->msr);
115 return (status & I2C_BB);
118 static int wait_for_pin(int *status)
120 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
121 int timeout = I2C_TIMEOUT;
123 *status = mpc_reg_in(®s->msr);
125 while (timeout-- && !(*status & I2C_IF)) {
127 *status = mpc_reg_in(®s->msr);
130 if (!(*status & I2C_IF)) {
134 mpc_reg_out(®s->msr, 0, I2C_IF);
139 static int do_address(uchar chip, char rdwr_flag)
141 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
150 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
151 mpc_reg_out(®s->mdr, chip, 0);
153 if (wait_for_pin(&status)) {
157 if (status & I2C_RXAK) {
164 static int send_bytes(uchar chip, char *buf, int len)
166 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
170 for (wrcount = 0; wrcount < len; ++wrcount) {
172 mpc_reg_out(®s->mdr, buf[wrcount], 0);
174 if (wait_for_pin(&status)) {
178 if (status & I2C_RXAK) {
184 return !(wrcount == len);
187 static int receive_bytes(uchar chip, char *buf, int len)
189 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
195 mpc_reg_out(®s->mcr, 0, I2C_TX);
197 for (i = 0; i < len; ++i) {
198 buf[rdcount] = mpc_reg_in(®s->mdr);
207 if (wait_for_pin(&status)) {
212 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
213 buf[rdcount++] = mpc_reg_in(®s->mdr);
215 if (wait_for_pin(&status)) {
219 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
224 #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
226 #define FDR510(x) (u8) (((x & 0x20) >> 3) | (x & 0x3))
227 #define FDR432(x) (u8) ((x & 0x1C) >> 2)
229 * Reset any i2c devices that may have been interrupted during a system reset.
230 * Normally this would be accomplished by clocking the line until SCL and SDA
231 * are released and then sending a start condtiion (From an Atmel datasheet).
232 * There is no direct access to the i2c pins so instead create start commands
233 * through the i2c interface. Send a start command then delay for the SDA Hold
234 * time, repeat this by disabling/enabling the bus a total of 9 times.
236 static void send_reset(void)
238 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
242 int SDA_Tap[] = { 3, 3, 4, 4, 1, 1, 2, 2};
243 struct mpc5xxx_i2c_tap scltap[] = {
254 fdr = (u8)mpc_reg_in(®s->mfdr);
256 delay = scltap[FDR432(fdr)].scl2tap + ((SDA_Tap[FDR510(fdr)] - 1) * \
257 scltap[FDR432(fdr)].tap2tap) + 3;
259 for (i = 0; i < 9; i++) {
260 mpc_reg_out(®s->mcr, I2C_EN|I2C_STA|I2C_TX, I2C_INIT_MASK);
262 mpc_reg_out(®s->mcr, 0, I2C_INIT_MASK);
266 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
268 #endif /* CONFIG_SYS_I2c_INIT_MPC5XXX */
270 /**************** I2C API ****************/
272 void i2c_init(int speed, int saddr)
274 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
276 mpc_reg_out(®s->mcr, 0, 0);
277 mpc_reg_out(®s->madr, saddr << 1, 0);
281 mpc_reg_out(®s->mfdr, mpc_get_fdr(speed), 0);
285 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
286 mpc_reg_out(®s->msr, 0, I2C_IF);
288 #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
294 static int mpc_get_fdr(int speed)
299 ulong best_speed = 0;
302 ulong bestmatch = 0xffffffffUL;
303 int best_i = 0, best_j = 0, i, j;
304 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
305 struct mpc5xxx_i2c_tap scltap[] = {
317 for (i = 7; i >= 0; i--) {
318 for (j = 7; j >= 0; j--) {
319 scl = 2 * (scltap[j].scl2tap +
320 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
321 if (ipb <= speed*scl) {
322 if ((speed*scl - ipb) < bestmatch) {
323 bestmatch = speed*scl - ipb;
326 best_speed = ipb/scl;
331 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
332 if (gd->flags & GD_FLG_RELOC) {
335 if (gd->have_console)
336 printf("%ld kHz, ", best_speed / 1000);
344 int i2c_probe(uchar chip)
346 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
349 for (i = 0; i < I2C_RETRIES; i++) {
350 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
352 if (! do_address(chip, 0)) {
353 mpc_reg_out(®s->mcr, 0, I2C_STA);
358 mpc_reg_out(®s->mcr, 0, I2C_STA);
362 return (i == I2C_RETRIES);
365 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
368 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
371 xaddr[0] = (addr >> 24) & 0xFF;
372 xaddr[1] = (addr >> 16) & 0xFF;
373 xaddr[2] = (addr >> 8) & 0xFF;
374 xaddr[3] = addr & 0xFF;
377 if (gd->have_console)
378 printf("i2c_read: bus is busy\n");
382 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
383 if (do_address(chip, 0)) {
384 if (gd->have_console)
385 printf("i2c_read: failed to address chip\n");
389 if (send_bytes(chip, &xaddr[4-alen], alen)) {
390 if (gd->have_console)
391 printf("i2c_read: send_bytes failed\n");
395 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
396 if (do_address(chip, 1)) {
397 if (gd->have_console)
398 printf("i2c_read: failed to address chip\n");
402 if (receive_bytes(chip, (char *)buf, len)) {
403 if (gd->have_console)
404 printf("i2c_read: receive_bytes failed\n");
410 mpc_reg_out(®s->mcr, 0, I2C_STA);
414 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
417 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
420 xaddr[0] = (addr >> 24) & 0xFF;
421 xaddr[1] = (addr >> 16) & 0xFF;
422 xaddr[2] = (addr >> 8) & 0xFF;
423 xaddr[3] = addr & 0xFF;
426 if (gd->have_console)
427 printf("i2c_write: bus is busy\n");
431 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
432 if (do_address(chip, 0)) {
433 if (gd->have_console)
434 printf("i2c_write: failed to address chip\n");
438 if (send_bytes(chip, &xaddr[4-alen], alen)) {
439 if (gd->have_console)
440 printf("i2c_write: send_bytes failed\n");
444 if (send_bytes(chip, (char *)buf, len)) {
445 if (gd->have_console)
446 printf("i2c_write: send_bytes failed\n");
452 mpc_reg_out(®s->mcr, 0, I2C_STA);
456 #if defined(CONFIG_I2C_MULTI_BUS)
457 int i2c_set_bus_num(unsigned int bus)
463 i2c_init(i2c_bus_speed[bus], CONFIG_SYS_I2C_SLAVE);
467 int i2c_set_bus_speed(unsigned int speed)
469 i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
473 unsigned int i2c_get_bus_num(void)
478 unsigned int i2c_get_bus_speed(void)
480 return i2c_bus_speed[i2c_bus_num];
485 #endif /* CONFIG_HARD_I2C */