summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx31/i2c-imx31.c
blob: 972c7d465b31eea264d259232a838c5867c03389 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdlib.h>
#include "config.h"
#include "system.h"
#include "kernel.h"
#include "avic-imx31.h"
#include "ccm-imx31.h"
#include "i2c-imx31.h"

/* Forward interrupt handler declarations */
#if (I2C_MODULE_MASK & USE_I2C1_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C1_HANDLER(void);
#endif
#if (I2C_MODULE_MASK & USE_I2C2_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C2_HANDLER(void);
#endif
#if (I2C_MODULE_MASK & USE_I2C3_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C3_HANDLER(void);
#endif

#define IADR (0x00 / sizeof (unsigned short)) /* 00h */
#define IFDR (0x04 / sizeof (unsigned short)) /* 04h */
#define I2CR (0x08 / sizeof (unsigned short)) /* 08h */
#define I2SR (0x0c / sizeof (unsigned short)) /* 0ch */
#define I2DR (0x10 / sizeof (unsigned short)) /* 10h */

static struct i2c_module_descriptor
{
    volatile unsigned short * const base; /* Module base address */
    struct i2c_transfer_desc *head;       /* Running job */
    struct i2c_transfer_desc *tail;       /* Most recent job added */
    void (* const handler)(void);         /* Module interrupt handler */
    unsigned char addr;                   /* Composite address value */
    unsigned char busy;                   /* Have buffers been modified? */
    uint8_t enable;                       /* Enable count */
    const uint8_t cg;                     /* Clock gating index */
    const uint8_t ints;                   /* Module interrupt number */
} i2c_descs[I2C_NUM_I2C] =
{
#if (I2C_MODULE_MASK & USE_I2C1_MODULE)
    {
        .base    = (unsigned short *)I2C1_BASE_ADDR,
        .cg      = CG_I2C1,
        .ints    = INT_I2C1,
        .handler = I2C1_HANDLER,
    },
#endif
#if (I2C_MODULE_MASK & USE_I2C2_MODULE)
    {
        .base    = (unsigned short *)I2C2_BASE_ADDR,
        .cg      = CG_I2C2,
        .ints    = INT_I2C2,
        .handler = I2C2_HANDLER,
    },
#endif
#if (I2C_MODULE_MASK & USE_I2C3_MODULE)
    {
        .base    = (unsigned short *)I2C3_BASE_ADDR,
        .cg      = CG_I2C3,
        .ints    = INT_I2C3,
        .handler = I2C3_HANDLER,
    },
#endif
};


/* Actually begin the session at the queue head */
static bool start_transfer(struct i2c_module_descriptor * const desc,
                           struct i2c_transfer_desc * const xfer)
{
    volatile unsigned short * const base = desc->base;

    /* If interface isn't enabled or buffers have been used, the transfer
       cannot be started/restarted. */
    if (desc->enable == 0 || desc->busy != 0)
        return false;

    /* Set speed */
    base[IFDR] = xfer->node->ifdr;

    /* Clear status */
    base[I2SR] = 0;

    /* Enable module, enable Interrupt, master transmitter */
    unsigned short i2cr = I2C_I2CR_IEN | I2C_I2CR_IIEN | I2C_I2CR_MTX;

    unsigned char addr = xfer->node->addr & 0xfe;

    if (xfer->rxcount > 0)
    {
        addr |= 0x01; /* Slave address/rd */

        if (xfer->rxcount < 2)
        {
            /* Receiving less than two bytes - disable ACK generation */
            i2cr |= I2C_I2CR_TXAK;
        }
    }

    /* Remember composite address - contains info concerning RX or TX */
    desc->addr = addr;

    /* Set config, generate START. */
    base[I2CR] = i2cr | I2C_I2CR_MSTA;

    /* Address slave (first byte sent) and begin session. */
    base[I2DR] = addr;

    return true;
}

static void i2c_interrupt(struct i2c_module_descriptor * const desc)
{
    volatile unsigned short * const base = desc->base;
    unsigned short const i2sr = base[I2SR];
    struct i2c_transfer_desc *xfer = desc->head;

    base[I2SR] = 0; /* Clear IIF */

    if (i2sr & I2C_I2SR_IAL)
    {
        /* Bus arbitration was lost - retry current transfer until it succeeds */
        /* Best guess as to why: at high CPU speeds, voltage hasn't had time to
         * stabilize to register STOP if a new transfer is started very soon
         * after a previous one has completed. AFAICT, this might happen once at
         * most on a transfer. Switching to repeated START could be a better way
         * to handle the cases where multiple transfers are already queued. */
        if (start_transfer(desc, xfer))
            return;

        /* Restart failed - STOP */
        base[I2CR] &= ~(I2C_I2CR_MSTA | I2C_I2CR_IIEN);
    }
    else if (xfer->txcount >= 0 && (base[I2CR] & I2C_I2CR_MTX))
    {
        /* ADDR cycle or TX */
        if ((i2sr & I2C_I2SR_RXAK) != 0)
        {
            /* NACK */
        }
        else if (xfer->txcount > 0)
        {
            desc->busy = 1;
            base[I2DR] = *xfer->txdata++; /* Send next TX byte */
            xfer->txcount--;
            return;
        }
        else if (desc->addr & 0x01)
        {
            /* ADDR cycle is complete */
            base[I2CR] &= ~I2C_I2CR_MTX; /* Switch to RX mode */
            base[I2DR];                  /* Dummy read */
            return;
        }

        /* Transfer complete or NACK - STOP */
        base[I2CR] &= ~(I2C_I2CR_MSTA | I2C_I2CR_IIEN);
    }
    else if (xfer->rxcount > 0)
    {
        /* RX */
        desc->busy = 1;

        if (--xfer->rxcount > 0)
        {
            if (xfer->rxcount == 1)
            {
                /* 2nd to Last byte - NACK */
                base[I2CR] |= I2C_I2CR_TXAK;
            }

            *xfer->rxdata++ = base[I2DR]; /* Read data from I2DR and store */
            return;
        }

        /* Generate STOP signal before reading final byte */
        base[I2CR] &= ~(I2C_I2CR_MSTA | I2C_I2CR_IIEN);
        *xfer->rxdata++ = base[I2DR]; /* Read data from I2DR and store */
    }

    /* Start next transfer, if any */
    for (;;)
    {
        struct i2c_transfer_desc *next = xfer->next;
        i2c_transfer_cb_fn_type callback = xfer->callback;
        xfer->next = NULL;

        if (next == xfer)
        {
            /* Last job on queue */
            desc->head = NULL;

            if (callback != NULL)
                callback(xfer);

            /* Callback may have restarted transfers. */
        }
        else
        {
            /* Queue next job */
            desc->head = next;

            if (callback != NULL)
                callback(xfer);

            desc->busy = 0;
            if (!start_transfer(desc, next))
            {
                xfer = next;
                continue;
            }
        }

        break;
    }
}

#if (I2C_MODULE_MASK & USE_I2C1_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C1_HANDLER(void)
{
    i2c_interrupt(&i2c_descs[I2C1_NUM]);
}
#endif
#if (I2C_MODULE_MASK & USE_I2C2_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C2_HANDLER(void)
{
    i2c_interrupt(&i2c_descs[I2C2_NUM]);
}
#endif
#if (I2C_MODULE_MASK & USE_I2C3_MODULE)
static __attribute__((interrupt("IRQ"))) void I2C3_HANDLER(void)
{
    i2c_interrupt(&i2c_descs[I2C3_NUM]);
}
#endif

/* Send and/or receive data on the specified node asynchronously */
bool i2c_transfer(struct i2c_transfer_desc *xfer)
{
    if (xfer == NULL || xfer->next != NULL || xfer->node == NULL ||
        xfer->rxcount < 0 || xfer->txcount < 0 ||
        (xfer->rxcount <= 0 && xfer->txcount <= 0))
    {
        /* Can't pass a busy descriptor, requires a node, < 0 sizes
           or all-0 sizes not permitted. */
        return false;
    }

    bool retval = true;
    struct i2c_module_descriptor * const desc = &i2c_descs[xfer->node->num];
    unsigned long cpsr = disable_irq_save();

    if (desc->head == NULL)
    {
        /* No transfers in progress; start interface. */
        desc->busy = 0;
        retval = start_transfer(desc, xfer);

        if (retval)
        {
            /* Start ok: actually put it in the queue. */
            desc->head = xfer;
            desc->tail = xfer;
            xfer->next = xfer; /* First, self-reference terminate */
        }
    }
    else
    {
        /* Already running: simply add to end and the final INT on the
         * running transfer will pick it up. */
        desc->tail->next = xfer; /* Add to tail */
        desc->tail       = xfer; /* New tail */
        xfer->next       = xfer; /* Self-reference terminate */
    }

    restore_irq(cpsr);

    return retval;
}

/** Synchronous transfers support **/
static void i2c_sync_xfer_complete_cb(struct i2c_transfer_desc *xfer)
{
    semaphore_release(&((struct i2c_sync_transfer_desc *)xfer)->sema);
}

static inline bool i2c_sync_wait_for_xfer_complete(struct i2c_sync_transfer_desc *xfer)
{
    return semaphore_wait(&xfer->sema, TIMEOUT_BLOCK) == OBJ_WAIT_SUCCEEDED;
}

int i2c_read(struct i2c_node *node, int addr, unsigned char *data,
             int data_count)
{
    struct i2c_sync_transfer_desc xfer;

    xfer.xfer.node = node;

    unsigned char ad;

    if (addr >= 0)
    {
        /* Sub-address */
        ad = addr;
        xfer.xfer.txdata = &ad;
        xfer.xfer.txcount = 1;
    }
    else
    {
        /* Raw read from slave */
        xfer.xfer.txcount = 0;
    }

    xfer.xfer.rxdata = data;
    xfer.xfer.rxcount = data_count;
    xfer.xfer.callback = i2c_sync_xfer_complete_cb;
    xfer.xfer.next = NULL;

    semaphore_init(&xfer.sema, 1, 0);

    if (i2c_transfer(&xfer.xfer) && i2c_sync_wait_for_xfer_complete(&xfer))
    {
        return data_count - xfer.xfer.rxcount;
    }

    return -1;
}

int i2c_write(struct i2c_node *node, const unsigned char *data,
              int data_count)
{
    struct i2c_sync_transfer_desc xfer;

    xfer.xfer.node = node;
    xfer.xfer.txdata = data;
    xfer.xfer.txcount = data_count;
    xfer.xfer.rxcount = 0;
    xfer.xfer.callback = i2c_sync_xfer_complete_cb;
    xfer.xfer.next = NULL;

    semaphore_init(&xfer.sema, 1, 0);

    if (i2c_transfer(&xfer.xfer) && i2c_sync_wait_for_xfer_complete(&xfer))
    {
        return data_count - xfer.xfer.txcount;
    }

    return -1;
}

void INIT_ATTR i2c_init(void)
{
    /* Do one-time inits for each module that will be used - leave
     * module disabled and unclocked until something wants it. */
    for (int i = 0; i < I2C_NUM_I2C; i++)
    {
        struct i2c_module_descriptor * const desc = &i2c_descs[i];
        ccm_module_clock_gating(desc->cg, CGM_ON_RUN_WAIT);
        desc->base[I2CR] = 0;
        ccm_module_clock_gating(desc->cg, CGM_OFF);
    }
}

/* Enable or disable the node - modules will be switched on/off accordingly. */
void i2c_enable_node(struct i2c_node *node, bool enable)
{
    struct i2c_module_descriptor * const desc = &i2c_descs[node->num];

    if (enable)
    {
        if (++desc->enable == 1)
        {
            /* First enable */
            ccm_module_clock_gating(desc->cg, CGM_ON_RUN_WAIT);
            desc->base[I2CR] = I2C_I2CR_IEN;
            desc->base[I2SR] = 0;
            avic_enable_int(desc->ints, INT_TYPE_IRQ, INT_PRIO_DEFAULT,
                            desc->handler);
        }
    }
    else
    {
        if (desc->enable > 0 && --desc->enable == 0)
        {
            /* Last enable */

            /* Wait for last tranfer */
            while (*(void ** volatile)&desc->head != NULL);

            /* Wait for STOP */
            while (desc->base[I2SR] & I2C_I2SR_IBB);

            desc->base[I2CR] &= ~I2C_I2CR_IEN;
            avic_disable_int(desc->ints);
            ccm_module_clock_gating(desc->cg, CGM_OFF);
        }
    }
}