1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2005 by Andy Young
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
27 #include "i2c-coldfire.h"
30 /* --- Local functions - declarations --- */
32 static int i2c_start(volatile unsigned char *iface
);
33 static int i2c_wait_for_slave(volatile unsigned char *iface
);
34 static int i2c_outb(volatile unsigned char *iface
, unsigned char byte
);
35 static inline void i2c_stop(volatile unsigned char *iface
);
38 /* --- Public functions - implementation --- */
42 #ifdef IRIVER_H100_SERIES
43 /* The FM chip has no pullup for SCL, so we have to bit-bang the
45 or_l(0x00800000, &GPIO1_OUT
);
46 or_l(0x00000008, &GPIO_OUT
);
47 or_l(0x00800000, &GPIO1_ENABLE
);
48 or_l(0x00000008, &GPIO_ENABLE
);
49 or_l(0x00800000, &GPIO1_FUNCTION
);
50 or_l(0x00000008, &GPIO_FUNCTION
);
51 #elif defined(IRIVER_H300_SERIES)
52 /* The FM chip has no pullup for SCL, so we have to bit-bang the
54 or_l(0x03000000, &GPIO1_OUT
);
55 or_l(0x03000000, &GPIO1_ENABLE
);
56 or_l(0x03000000, &GPIO1_FUNCTION
);
59 /* I2C Clock divisor = 160 => 124.1556 MHz / 2 / 160 = 388.08 kHz */
62 #if defined(IAUDIO_M3)
63 MBCR
= IEN
; /* Enable interface 1 */
64 /* secondary channel is handled in the interrupt driven ADC driver */
65 #elif defined(IAUDIO_X5) || defined(IAUDIO_M5)
66 MBCR
= IEN
; /* Enable interface 1 */
69 MBCR2
= IEN
; /* Enable interface 2 */
70 #elif defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
71 MBDR
= 0; /* iRiver firmware does this */
72 MBCR
= IEN
; /* Enable interface */
73 #elif defined(MPIO_HD200)
86 /* End I2C session on the given interface. */
87 static inline void i2c_stop(volatile unsigned char *iface
)
89 iface
[O_MBCR
] &= ~MSTA
;
93 * Writes bytes to a I2C device.
95 * Returns number of bytes successfully sent or a negative value on error.
97 int i2c_write(volatile unsigned char *iface
, unsigned char addr
,
98 const unsigned char *buf
, int count
)
105 rc
= i2c_start(iface
);
109 rc
= i2c_outb(iface
, addr
& 0xfe);
113 for (i
= 0; i
< count
; i
++)
115 rc
= i2c_outb(iface
, *buf
++);
125 * Reads bytes from a I2C device.
127 * Returns number of bytes successfully received or a negative value on error.
129 int i2c_read(volatile unsigned char *iface
, unsigned char addr
,
130 unsigned char *buf
, int count
)
137 rc
= i2c_start(iface
);
141 rc
= i2c_outb(iface
, addr
| 1);
145 /* Switch to Rx mode */
146 iface
[O_MBCR
] &= ~MTX
;
148 /* Turn on ACK generation if reading multiple bytes */
150 iface
[O_MBCR
] &= ~TXAK
;
153 rc
= (int) iface
[O_MBDR
];
155 for (i
= count
; i
> 0; i
--)
157 rc
= i2c_wait_for_slave(iface
);
162 /* Don't ACK the last byte to be read from the slave */
163 iface
[O_MBCR
] |= TXAK
;
165 /* Generate STOP before reading last byte received */
168 *buf
++ = iface
[O_MBDR
];
173 /* --- Local functions - implementation --- */
175 /* Begin I2C session on the given interface.
177 * Returns 0 on success, negative value on error.
179 int i2c_start(volatile unsigned char *iface
)
181 /* Wait for bus to become free */
183 while (--j
&& (iface
[O_MBSR
] & IBB
))
187 logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface
);
191 /* Generate START and prepare for write */
192 iface
[O_MBCR
] |= (MSTA
| TXAK
| MTX
);
197 /* Wait for slave to act on given I2C interface.
199 * Returns 0 on success, negative value on error.
201 int i2c_wait_for_slave(volatile unsigned char *iface
)
204 while (--j
&& ! (iface
[O_MBSR
] & IIF
))
208 logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface
);
213 /* Clear interrupt flag */
214 iface
[O_MBSR
] &= ~IIF
;
219 /* Write the given byte to the given I2C interface.
221 * Returns 0 on success, negative value on error.
223 int i2c_outb(volatile unsigned char *iface
, unsigned char byte
)
227 iface
[O_MBDR
] = byte
;
229 rc
= i2c_wait_for_slave(iface
);
233 /* Check that transfer is complete */
234 if ( !(iface
[O_MBSR
] & ICF
))
236 logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface
);
241 /* Check that the byte has been ACKed */
242 if (iface
[O_MBSR
] & RXAK
)
244 logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface
);