summaryrefslogtreecommitdiffstats
path: root/firmware/target/coldfire/i2c-coldfire.c
blob: d0c369d1cc92aeb500c10546b60909dc6d401e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * $Id$
 *
 * Copyright (C) 2005 by Andy Young
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "cpu.h"
#include "kernel.h"
#include "logf.h"
#include "system.h"
#include "i2c-coldfire.h"


/* --- Local functions - declarations --- */

static int i2c_start(volatile unsigned char *iface);
static int i2c_wait_for_slave(volatile unsigned char *iface);
static int i2c_outb(volatile unsigned char *iface, unsigned char byte);
static inline void i2c_stop(volatile unsigned char *iface);


/* --- Public functions - implementation --- */

void i2c_init(void)
{
#ifdef IRIVER_H100_SERIES
    /* The FM chip has no pullup for SCL, so we have to bit-bang the
       I2C for that one. */
    or_l(0x00800000, &GPIO1_OUT);
    or_l(0x00000008, &GPIO_OUT);
    or_l(0x00800000, &GPIO1_ENABLE);
    or_l(0x00000008, &GPIO_ENABLE);
    or_l(0x00800000, &GPIO1_FUNCTION);
    or_l(0x00000008, &GPIO_FUNCTION);
#elif defined(IRIVER_H300_SERIES)
    /* The FM chip has no pullup for SCL, so we have to bit-bang the
       I2C for that one. */
    or_l(0x03000000, &GPIO1_OUT);
    or_l(0x03000000, &GPIO1_ENABLE);
    or_l(0x03000000, &GPIO1_FUNCTION);
#endif
    
    /* I2C Clock divisor = 160 => 124.1556 MHz / 2 / 160 = 388.08 kHz */
    MFDR  = 0x0d;

#if defined(IAUDIO_M3)
    MBCR  = IEN;  /* Enable interface 1 */
    /* secondary channel is handled in the interrupt driven ADC driver */
#elif defined(IAUDIO_X5) || defined(IAUDIO_M5)
    MBCR  = IEN;  /* Enable interface 1 */

    MFDR2 = 0x0d;
    MBCR2 = IEN;  /* Enable interface 2 */
#elif defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
    MBDR = 0;    /* iRiver firmware does this */
    MBCR = IEN;  /* Enable interface */
#endif
}

void i2c_close(void)
{
    MBCR  = 0;
    MBCR2 = 0;
}

/* End I2C session on the given interface. */
static inline void i2c_stop(volatile unsigned char *iface)
{
    iface[O_MBCR] &= ~MSTA;
}

/*
 * Writes bytes to a I2C device.
 *
 * Returns number of bytes successfully sent or a negative value on error.
 */
int i2c_write(volatile unsigned char *iface, unsigned char addr, 
              const unsigned char *buf, int count)
{
    int i, rc;

    if (count <= 0)
        return 0;
   
    rc = i2c_start(iface);
    if (rc < 0)
        return rc;
    
    rc = i2c_outb(iface, addr & 0xfe);
    if (rc < 0)
        return rc;
    
    for (i = 0; i < count; i++)
    {
        rc = i2c_outb(iface, *buf++);
        if (rc < 0)
            return rc;
    }
    i2c_stop(iface);
    
    return count;
}

/*
 * Reads bytes from a I2C device.
 *
 * Returns number of bytes successfully received or a negative value on error.
 */
int i2c_read(volatile unsigned char *iface, unsigned char addr, 
             unsigned char *buf, int count)
{
    int i, rc;

    if (count <= 0)
        return 0;
   
    rc = i2c_start(iface);
    if (rc < 0)
        return rc;

    rc = i2c_outb(iface, addr | 1);
    if (rc < 0)
        return rc;

    /* Switch to Rx mode */
    iface[O_MBCR] &= ~MTX;

    /* Turn on ACK generation if reading multiple bytes */
    if (count > 1)
        iface[O_MBCR] &= ~TXAK;
 
    /* Dummy read */
    rc = (int) iface[O_MBDR];
  
    for (i = count; i > 0; i--)
    {
        rc = i2c_wait_for_slave(iface);
        if (rc < 0)
            return rc;

        if (i == 2)
            /* Don't ACK the last byte to be read from the slave */
            iface[O_MBCR] |= TXAK;
        else if (i == 1)
            /* Generate STOP before reading last byte received */
            i2c_stop(iface);

        *buf++ = iface[O_MBDR];
    }
    return count;
}

/* --- Local functions - implementation --- */

/* Begin I2C session on the given interface.
 *
 * Returns 0 on success, negative value on error.
 */
int i2c_start(volatile unsigned char *iface)
{
    /* Wait for bus to become free */
    int j = MAX_LOOP;
    while (--j && (iface[O_MBSR] & IBB))
        ;
    if (!j)
    {
        logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface);
        return -1;
    }
 
    /* Generate START and prepare for write */
    iface[O_MBCR] |= (MSTA | TXAK | MTX);
  
    return 0; 
}

/* Wait for slave to act on given I2C interface.
 *
 * Returns 0 on success, negative value on error.
 */
int i2c_wait_for_slave(volatile unsigned char *iface)
{
    int j = MAX_LOOP;
    while (--j && ! (iface[O_MBSR] & IIF))
        ;
    if (!j)
    {
        logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -2;
    }
  
    /* Clear interrupt flag */
    iface[O_MBSR] &= ~IIF;
    
    return 0;
}

/* Write the given byte to the given I2C interface.
 *
 * Returns 0 on success, negative value on error.
 */
int i2c_outb(volatile unsigned char *iface, unsigned char byte)
{
    int rc;

    iface[O_MBDR] = byte;

    rc = i2c_wait_for_slave(iface);
    if (rc < 0)
        return rc;
    
    /* Check that transfer is complete */
    if ( !(iface[O_MBSR] & ICF))
    {
        logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -3;
    }
    
    /* Check that the byte has been ACKed */
    if (iface[O_MBSR] & RXAK)
    {
        logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -4;
    }
    
    return 0;
}