2 * (C) Copyright 2003 - 2007
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,
23 * Based on the MPC5xxx code.
28 DECLARE_GLOBAL_DATA_PTR;
30 #ifdef CONFIG_HARD_I2C
35 #define immr ((immap_t *)CFG_IMMR)
37 /* by default set I2C bus 0 active */
38 static unsigned int bus_num = 0;
40 #define I2C_TIMEOUT 100
43 struct mpc512x_i2c_tap {
48 static int mpc_reg_in(volatile u32 *reg);
49 static void mpc_reg_out(volatile u32 *reg, int val, int mask);
50 static int wait_for_bb(void);
51 static int wait_for_pin(int *status);
52 static int do_address(uchar chip, char rdwr_flag);
53 static int send_bytes(uchar chip, char *buf, int len);
54 static int receive_bytes(uchar chip, char *buf, int len);
55 static int mpc_get_fdr(int);
57 static int mpc_reg_in (volatile u32 *reg)
60 __asm__ __volatile__ ("eieio");
64 static void mpc_reg_out (volatile u32 *reg, int val, int mask)
71 tmp = mpc_reg_in (reg);
72 *reg = ((tmp & ~mask) | (val & mask)) << 24;
74 __asm__ __volatile__ ("eieio");
79 static int wait_for_bb (void)
81 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
82 int timeout = I2C_TIMEOUT;
85 status = mpc_reg_in (®s->msr);
87 while (timeout-- && (status & I2C_BB)) {
89 mpc_reg_out (®s->mcr, I2C_STA, I2C_STA);
90 temp = mpc_reg_in (®s->mdr);
91 mpc_reg_out (®s->mcr, 0, I2C_STA);
92 mpc_reg_out (®s->mcr, 0, 0);
93 mpc_reg_out (®s->mcr, I2C_EN, 0);
96 status = mpc_reg_in (®s->msr);
99 return (status & I2C_BB);
102 static int wait_for_pin (int *status)
104 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
105 int timeout = I2C_TIMEOUT;
107 *status = mpc_reg_in (®s->msr);
109 while (timeout-- && !(*status & I2C_IF)) {
111 *status = mpc_reg_in (®s->msr);
114 if (!(*status & I2C_IF)) {
118 mpc_reg_out (®s->msr, 0, I2C_IF);
123 static int do_address (uchar chip, char rdwr_flag)
125 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
134 mpc_reg_out (®s->mcr, I2C_TX, I2C_TX);
135 mpc_reg_out (®s->mdr, chip, 0);
137 if (wait_for_pin (&status)) {
141 if (status & I2C_RXAK) {
148 static int send_bytes (uchar chip, char *buf, int len)
150 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
154 for (wrcount = 0; wrcount < len; ++wrcount) {
156 mpc_reg_out (®s->mdr, buf[wrcount], 0);
158 if (wait_for_pin (&status)) {
162 if (status & I2C_RXAK) {
168 return !(wrcount == len);
171 static int receive_bytes (uchar chip, char *buf, int len)
173 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
179 mpc_reg_out (®s->mcr, 0, I2C_TX);
181 for (i = 0; i < len; ++i) {
182 buf[rdcount] = mpc_reg_in (®s->mdr);
190 if (wait_for_pin (&status)) {
195 mpc_reg_out (®s->mcr, I2C_TXAK, I2C_TXAK);
196 buf[rdcount++] = mpc_reg_in (®s->mdr);
198 if (wait_for_pin (&status)) {
202 mpc_reg_out (®s->mcr, 0, I2C_TXAK);
207 /**************** I2C API ****************/
209 void i2c_init (int speed, int saddr)
212 for(i = 0; i < I2C_BUS_CNT; i++){
213 i2c512x_dev_t *regs = &immr->i2c.dev[i];
214 mpc_reg_out (®s->mcr, 0, 0);
217 mpc_reg_out (®s->mfdr, mpc_get_fdr (speed), 0);
218 mpc_reg_out (®s->madr, saddr << 1, 0);
221 mpc_reg_out (®s->mcr, I2C_EN, I2C_INIT_MASK);
222 mpc_reg_out (®s->msr, 0, I2C_IF);
225 /* Disable interrupts */
227 /* Turn off filters */
232 static int mpc_get_fdr (int speed)
237 ulong best_speed = 0;
240 ulong bestmatch = 0xffffffffUL;
241 int best_i = 0, best_j = 0, i, j;
242 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
243 struct mpc512x_i2c_tap scltap[] = {
255 for (i = 7; i >= 0; i--) {
256 for (j = 7; j >= 0; j--) {
257 scl = 2 * (scltap[j].scl2tap +
258 (SCL_Tap[i] - 1) * scltap[j].tap2tap
260 if (ipb <= speed*scl) {
261 if ((speed*scl - ipb) < bestmatch) {
262 bestmatch = speed*scl - ipb;
265 best_speed = ipb/scl;
270 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
271 if (gd->flags & GD_FLG_RELOC) {
274 debug("%ld kHz, \n", best_speed / 1000);
282 int i2c_probe (uchar chip)
284 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
287 for (i = 0; i < I2C_RETRIES; i++) {
288 mpc_reg_out (®s->mcr, I2C_STA, I2C_STA);
290 if (! do_address (chip, 0)) {
291 mpc_reg_out (®s->mcr, 0, I2C_STA);
296 mpc_reg_out (®s->mcr, 0, I2C_STA);
300 return (i == I2C_RETRIES);
303 int i2c_read (uchar chip, uint addr, int alen, uchar *buf, int len)
306 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
309 xaddr[0] = (addr >> 24) & 0xFF;
310 xaddr[1] = (addr >> 16) & 0xFF;
311 xaddr[2] = (addr >> 8) & 0xFF;
312 xaddr[3] = addr & 0xFF;
314 if (wait_for_bb ()) {
315 printf ("i2c_read: bus is busy\n");
319 mpc_reg_out (®s->mcr, I2C_STA, I2C_STA);
320 if (do_address (chip, 0)) {
321 printf ("i2c_read: failed to address chip\n");
325 if (send_bytes (chip, &xaddr[4-alen], alen)) {
326 printf ("i2c_read: send_bytes failed\n");
330 mpc_reg_out (®s->mcr, I2C_RSTA, I2C_RSTA);
331 if (do_address (chip, 1)) {
332 printf ("i2c_read: failed to address chip\n");
336 if (receive_bytes (chip, (char *)buf, len)) {
337 printf ("i2c_read: receive_bytes failed\n");
343 mpc_reg_out (®s->mcr, 0, I2C_STA);
347 int i2c_write (uchar chip, uint addr, int alen, uchar *buf, int len)
350 i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
353 xaddr[0] = (addr >> 24) & 0xFF;
354 xaddr[1] = (addr >> 16) & 0xFF;
355 xaddr[2] = (addr >> 8) & 0xFF;
356 xaddr[3] = addr & 0xFF;
358 if (wait_for_bb ()) {
359 printf ("i2c_write: bus is busy\n");
363 mpc_reg_out (®s->mcr, I2C_STA, I2C_STA);
364 if (do_address (chip, 0)) {
365 printf ("i2c_write: failed to address chip\n");
369 if (send_bytes (chip, &xaddr[4-alen], alen)) {
370 printf ("i2c_write: send_bytes failed\n");
374 if (send_bytes (chip, (char *)buf, len)) {
375 printf ("i2c_write: send_bytes failed\n");
381 mpc_reg_out (®s->mcr, 0, I2C_STA);
385 uchar i2c_reg_read (uchar chip, uchar reg)
389 i2c_read (chip, reg, 1, &buf, 1);
394 void i2c_reg_write (uchar chip, uchar reg, uchar val)
396 i2c_write (chip, reg, 1, &val, 1);
402 int i2c_set_bus_num (unsigned int bus)
404 if (bus >= I2C_BUS_CNT) {
412 unsigned int i2c_get_bus_num (void)
418 unsigned int i2c_get_bus_speed (void)
423 int i2c_set_bus_speed (unsigned int speed)
425 if (speed != CFG_I2C_SPEED)
431 #endif /* CONFIG_HARD_I2C */