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)) {
103 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
104 (void)mpc_reg_in(®s->mdr);
105 mpc_reg_out(®s->mcr, 0, I2C_STA);
106 mpc_reg_out(®s->mcr, 0, 0);
107 mpc_reg_out(®s->mcr, I2C_EN, 0);
109 status = mpc_reg_in(®s->msr);
112 return (status & I2C_BB);
115 static int wait_for_pin(int *status)
117 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
118 int timeout = I2C_TIMEOUT;
120 *status = mpc_reg_in(®s->msr);
122 while (timeout-- && !(*status & I2C_IF)) {
124 *status = mpc_reg_in(®s->msr);
127 if (!(*status & I2C_IF)) {
131 mpc_reg_out(®s->msr, 0, I2C_IF);
136 static int do_address(uchar chip, char rdwr_flag)
138 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
147 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
148 mpc_reg_out(®s->mdr, chip, 0);
150 if (wait_for_pin(&status)) {
154 if (status & I2C_RXAK) {
161 static int send_bytes(uchar chip, char *buf, int len)
163 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
167 for (wrcount = 0; wrcount < len; ++wrcount) {
169 mpc_reg_out(®s->mdr, buf[wrcount], 0);
171 if (wait_for_pin(&status)) {
175 if (status & I2C_RXAK) {
181 return !(wrcount == len);
184 static int receive_bytes(uchar chip, char *buf, int len)
186 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
192 mpc_reg_out(®s->mcr, 0, I2C_TX);
194 for (i = 0; i < len; ++i) {
195 buf[rdcount] = mpc_reg_in(®s->mdr);
204 if (wait_for_pin(&status)) {
209 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
210 buf[rdcount++] = mpc_reg_in(®s->mdr);
212 if (wait_for_pin(&status)) {
216 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
221 #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
223 #define FDR510(x) (u8) (((x & 0x20) >> 3) | (x & 0x3))
224 #define FDR432(x) (u8) ((x & 0x1C) >> 2)
226 * Reset any i2c devices that may have been interrupted during a system reset.
227 * Normally this would be accomplished by clocking the line until SCL and SDA
228 * are released and then sending a start condtiion (From an Atmel datasheet).
229 * There is no direct access to the i2c pins so instead create start commands
230 * through the i2c interface. Send a start command then delay for the SDA Hold
231 * time, repeat this by disabling/enabling the bus a total of 9 times.
233 static void send_reset(void)
235 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
239 int SDA_Tap[] = { 3, 3, 4, 4, 1, 1, 2, 2};
240 struct mpc5xxx_i2c_tap scltap[] = {
251 fdr = (u8)mpc_reg_in(®s->mfdr);
253 delay = scltap[FDR432(fdr)].scl2tap + ((SDA_Tap[FDR510(fdr)] - 1) * \
254 scltap[FDR432(fdr)].tap2tap) + 3;
256 for (i = 0; i < 9; i++) {
257 mpc_reg_out(®s->mcr, I2C_EN|I2C_STA|I2C_TX, I2C_INIT_MASK);
259 mpc_reg_out(®s->mcr, 0, I2C_INIT_MASK);
263 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
265 #endif /* CONFIG_SYS_I2c_INIT_MPC5XXX */
267 /**************** I2C API ****************/
269 void i2c_init(int speed, int saddr)
271 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
273 mpc_reg_out(®s->mcr, 0, 0);
274 mpc_reg_out(®s->madr, saddr << 1, 0);
278 mpc_reg_out(®s->mfdr, mpc_get_fdr(speed), 0);
282 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
283 mpc_reg_out(®s->msr, 0, I2C_IF);
285 #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
291 static int mpc_get_fdr(int speed)
296 ulong best_speed = 0;
299 ulong bestmatch = 0xffffffffUL;
300 int best_i = 0, best_j = 0, i, j;
301 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
302 struct mpc5xxx_i2c_tap scltap[] = {
314 for (i = 7; i >= 0; i--) {
315 for (j = 7; j >= 0; j--) {
316 scl = 2 * (scltap[j].scl2tap +
317 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
318 if (ipb <= speed*scl) {
319 if ((speed*scl - ipb) < bestmatch) {
320 bestmatch = speed*scl - ipb;
323 best_speed = ipb/scl;
328 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
329 if (gd->flags & GD_FLG_RELOC) {
332 printf("%ld kHz, ", best_speed / 1000);
340 int i2c_probe(uchar chip)
342 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
345 for (i = 0; i < I2C_RETRIES; i++) {
346 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
348 if (! do_address(chip, 0)) {
349 mpc_reg_out(®s->mcr, 0, I2C_STA);
354 mpc_reg_out(®s->mcr, 0, I2C_STA);
358 return (i == I2C_RETRIES);
361 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
364 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
367 xaddr[0] = (addr >> 24) & 0xFF;
368 xaddr[1] = (addr >> 16) & 0xFF;
369 xaddr[2] = (addr >> 8) & 0xFF;
370 xaddr[3] = addr & 0xFF;
373 printf("i2c_read: bus is busy\n");
377 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
378 if (do_address(chip, 0)) {
379 printf("i2c_read: failed to address chip\n");
383 if (send_bytes(chip, &xaddr[4-alen], alen)) {
384 printf("i2c_read: send_bytes failed\n");
388 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
389 if (do_address(chip, 1)) {
390 printf("i2c_read: failed to address chip\n");
394 if (receive_bytes(chip, (char *)buf, len)) {
395 printf("i2c_read: receive_bytes failed\n");
401 mpc_reg_out(®s->mcr, 0, I2C_STA);
405 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
408 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
411 xaddr[0] = (addr >> 24) & 0xFF;
412 xaddr[1] = (addr >> 16) & 0xFF;
413 xaddr[2] = (addr >> 8) & 0xFF;
414 xaddr[3] = addr & 0xFF;
417 printf("i2c_write: bus is busy\n");
421 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
422 if (do_address(chip, 0)) {
423 printf("i2c_write: failed to address chip\n");
427 if (send_bytes(chip, &xaddr[4-alen], alen)) {
428 printf("i2c_write: send_bytes failed\n");
432 if (send_bytes(chip, (char *)buf, len)) {
433 printf("i2c_write: send_bytes failed\n");
439 mpc_reg_out(®s->mcr, 0, I2C_STA);
443 #if defined(CONFIG_I2C_MULTI_BUS)
444 int i2c_set_bus_num(unsigned int bus)
450 i2c_init(i2c_bus_speed[bus], CONFIG_SYS_I2C_SLAVE);
454 int i2c_set_bus_speed(unsigned int speed)
456 i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
460 unsigned int i2c_get_bus_num(void)
465 unsigned int i2c_get_bus_speed(void)
467 return i2c_bus_speed[i2c_bus_num];
472 #endif /* CONFIG_HARD_I2C */