Factor out some drawing code.
[kugel-rb.git] / firmware / target / coldfire / i2c-coldfire.c
blobebfe0a006e92f35747bf39811d34a2cb07fa6f1a
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 #endif
76 void i2c_close(void)
78 MBCR = 0;
79 MBCR2 = 0;
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)
96 int i, rc;
98 if (count <= 0)
99 return 0;
101 rc = i2c_start(iface);
102 if (rc < 0)
103 return rc;
105 rc = i2c_outb(iface, addr & 0xfe);
106 if (rc < 0)
107 return rc;
109 for (i = 0; i < count; i++)
111 rc = i2c_outb(iface, *buf++);
112 if (rc < 0)
113 return rc;
115 i2c_stop(iface);
117 return count;
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)
128 int i, rc;
130 if (count <= 0)
131 return 0;
133 rc = i2c_start(iface);
134 if (rc < 0)
135 return rc;
137 rc = i2c_outb(iface, addr | 1);
138 if (rc < 0)
139 return rc;
141 /* Switch to Rx mode */
142 iface[O_MBCR] &= ~MTX;
144 /* Turn on ACK generation if reading multiple bytes */
145 if (count > 1)
146 iface[O_MBCR] &= ~TXAK;
148 /* Dummy read */
149 rc = (int) iface[O_MBDR];
151 for (i = count; i > 0; i--)
153 rc = i2c_wait_for_slave(iface);
154 if (rc < 0)
155 return rc;
157 if (i == 2)
158 /* Don't ACK the last byte to be read from the slave */
159 iface[O_MBCR] |= TXAK;
160 else if (i == 1)
161 /* Generate STOP before reading last byte received */
162 i2c_stop(iface);
164 *buf++ = iface[O_MBDR];
166 return count;
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 */
178 int j = MAX_LOOP;
179 while (--j && (iface[O_MBSR] & IBB))
181 if (!j)
183 logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface);
184 return -1;
187 /* Generate START and prepare for write */
188 iface[O_MBCR] |= (MSTA | TXAK | MTX);
190 return 0;
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)
199 int j = MAX_LOOP;
200 while (--j && ! (iface[O_MBSR] & IIF))
202 if (!j)
204 logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface);
205 i2c_stop(iface);
206 return -2;
209 /* Clear interrupt flag */
210 iface[O_MBSR] &= ~IIF;
212 return 0;
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)
221 int rc;
223 iface[O_MBDR] = byte;
225 rc = i2c_wait_for_slave(iface);
226 if (rc < 0)
227 return rc;
229 /* Check that transfer is complete */
230 if ( !(iface[O_MBSR] & ICF))
232 logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface);
233 i2c_stop(iface);
234 return -3;
237 /* Check that the byte has been ACKed */
238 if (iface[O_MBSR] & RXAK)
240 logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface);
241 i2c_stop(iface);
242 return -4;
245 return 0;