HD300 - add missing i2c initializtion.
[maemo-rb.git] / firmware / target / coldfire / i2c-coldfire.c
blob1c4bd338fc6e3fbc81a50e467530aa069e94a87b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * $Id$
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 ****************************************************************************/
23 #include "cpu.h"
24 #include "kernel.h"
25 #include "logf.h"
26 #include "system.h"
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 --- */
40 void i2c_init(void)
42 #ifdef IRIVER_H100_SERIES
43 /* The FM chip has no pullup for SCL, so we have to bit-bang the
44 I2C for that one. */
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
53 I2C for that one. */
54 or_l(0x03000000, &GPIO1_OUT);
55 or_l(0x03000000, &GPIO1_ENABLE);
56 or_l(0x03000000, &GPIO1_FUNCTION);
57 #endif
59 /* I2C Clock divisor = 160 => 124.1556 MHz / 2 / 160 = 388.08 kHz */
60 MFDR = 0x0d;
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 */
68 MFDR2 = 0x0d;
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) || defined(MPIO_HD300)
74 /* second channel */
75 MFDR2 = 0x0d;
76 MBCR2 = IEN;
77 #endif
80 void i2c_close(void)
82 MBCR = 0;
83 MBCR2 = 0;
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)
100 int i, rc;
102 if (count <= 0)
103 return 0;
105 rc = i2c_start(iface);
106 if (rc < 0)
107 return rc;
109 rc = i2c_outb(iface, addr & 0xfe);
110 if (rc < 0)
111 return rc;
113 for (i = 0; i < count; i++)
115 rc = i2c_outb(iface, *buf++);
116 if (rc < 0)
117 return rc;
119 i2c_stop(iface);
121 return count;
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)
132 int i, rc;
134 if (count <= 0)
135 return 0;
137 rc = i2c_start(iface);
138 if (rc < 0)
139 return rc;
141 rc = i2c_outb(iface, addr | 1);
142 if (rc < 0)
143 return rc;
145 /* Switch to Rx mode */
146 iface[O_MBCR] &= ~MTX;
148 /* Turn on ACK generation if reading multiple bytes */
149 if (count > 1)
150 iface[O_MBCR] &= ~TXAK;
152 /* Dummy read */
153 rc = (int) iface[O_MBDR];
155 for (i = count; i > 0; i--)
157 rc = i2c_wait_for_slave(iface);
158 if (rc < 0)
159 return rc;
161 if (i == 2)
162 /* Don't ACK the last byte to be read from the slave */
163 iface[O_MBCR] |= TXAK;
164 else if (i == 1)
165 /* Generate STOP before reading last byte received */
166 i2c_stop(iface);
168 *buf++ = iface[O_MBDR];
170 return count;
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 */
182 int j = MAX_LOOP;
183 while (--j && (iface[O_MBSR] & IBB))
185 if (!j)
187 logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface);
188 return -1;
191 /* Generate START and prepare for write */
192 iface[O_MBCR] |= (MSTA | TXAK | MTX);
194 return 0;
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)
203 int j = MAX_LOOP;
204 while (--j && ! (iface[O_MBSR] & IIF))
206 if (!j)
208 logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface);
209 i2c_stop(iface);
210 return -2;
213 /* Clear interrupt flag */
214 iface[O_MBSR] &= ~IIF;
216 return 0;
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)
225 int rc;
227 iface[O_MBDR] = byte;
229 rc = i2c_wait_for_slave(iface);
230 if (rc < 0)
231 return rc;
233 /* Check that transfer is complete */
234 if ( !(iface[O_MBSR] & ICF))
236 logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface);
237 i2c_stop(iface);
238 return -3;
241 /* Check that the byte has been ACKed */
242 if (iface[O_MBSR] & RXAK)
244 logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface);
245 i2c_stop(iface);
246 return -4;
249 return 0;