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 */
82 /* End I2C session on the given interface. */
83 static inline void i2c_stop(volatile unsigned char *iface
)
85 iface
[O_MBCR
] &= ~MSTA
;
89 * Writes bytes to a I2C device.
91 * Returns number of bytes successfully sent or a negative value on error.
93 int i2c_write(volatile unsigned char *iface
, unsigned char addr
,
94 const unsigned char *buf
, int count
)
101 rc
= i2c_start(iface
);
105 rc
= i2c_outb(iface
, addr
& 0xfe);
109 for (i
= 0; i
< count
; i
++)
111 rc
= i2c_outb(iface
, *buf
++);
121 * Reads bytes from a I2C device.
123 * Returns number of bytes successfully received or a negative value on error.
125 int i2c_read(volatile unsigned char *iface
, unsigned char addr
,
126 unsigned char *buf
, int count
)
133 rc
= i2c_start(iface
);
137 rc
= i2c_outb(iface
, addr
| 1);
141 /* Switch to Rx mode */
142 iface
[O_MBCR
] &= ~MTX
;
144 /* Turn on ACK generation if reading multiple bytes */
146 iface
[O_MBCR
] &= ~TXAK
;
149 rc
= (int) iface
[O_MBDR
];
151 for (i
= count
; i
> 0; i
--)
153 rc
= i2c_wait_for_slave(iface
);
158 /* Don't ACK the last byte to be read from the slave */
159 iface
[O_MBCR
] |= TXAK
;
161 /* Generate STOP before reading last byte received */
164 *buf
++ = iface
[O_MBDR
];
169 /* --- Local functions - implementation --- */
171 /* Begin I2C session on the given interface.
173 * Returns 0 on success, negative value on error.
175 int i2c_start(volatile unsigned char *iface
)
177 /* Wait for bus to become free */
179 while (--j
&& (iface
[O_MBSR
] & IBB
))
183 logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface
);
187 /* Generate START and prepare for write */
188 iface
[O_MBCR
] |= (MSTA
| TXAK
| MTX
);
193 /* Wait for slave to act on given I2C interface.
195 * Returns 0 on success, negative value on error.
197 int i2c_wait_for_slave(volatile unsigned char *iface
)
200 while (--j
&& ! (iface
[O_MBSR
] & IIF
))
204 logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface
);
209 /* Clear interrupt flag */
210 iface
[O_MBSR
] &= ~IIF
;
215 /* Write the given byte to the given I2C interface.
217 * Returns 0 on success, negative value on error.
219 int i2c_outb(volatile unsigned char *iface
, unsigned char byte
)
223 iface
[O_MBDR
] = byte
;
225 rc
= i2c_wait_for_slave(iface
);
229 /* Check that transfer is complete */
230 if ( !(iface
[O_MBSR
] & ICF
))
232 logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface
);
237 /* Check that the byte has been ACKed */
238 if (iface
[O_MBSR
] & RXAK
)
240 logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface
);