summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/s5l8702/gpio-s5l8702.c
blob: a4561c3ab50f584f436cd2329f40ee52806ea778 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 Cástor Muñoz
 * Code based on openiBoot project
 *
 * 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 <stdint.h>

#include "config.h"
#include "system.h"
#include "panic.h"

#include "gpio-s5l8702.h"


int rec_hw_ver;

static uint32_t gpio_data[] =
{
#if defined(IPOD_6G)
    0x5322222F, 0xEEEEEE00, 0x2332EEEE, 0x3333E222,
    0x33333333, 0x33333333, 0x3F000E33, 0xEEEEEEEE,
    0xEEEEEEEE, 0xEEEEEEEE, 0xE0EEEEEE, 0xEE00EE0E,
    0xEEEE0EEE, 0xEEEEEEEE, 0xEE2222EE, 0xEEEE0EEE,
#elif defined(IPOD_NANO3G)
    0xE322222F, 0xEEEEEE00, 0x2332EEEE, 0x3333E222,
    0xEEE33333, 0x3EE0EEEE, 0x0F00EE33, 0xEEEEEEE0,
    0x22222222, 0x22222222, 0x33322222, 0xEEEEEEEE,
    0xEEEEEEEE, 0xEEEEEEEE, 0xEE2222EE, 0xEEEE0EEE,
#elif defined(IPOD_NANO4G)
    0x3202EEEE, 0xE0EE2253, 0x2223EEEE, 0x33333332,
    0xFF333E33, 0xE0FEE200, 0x2222222E, 0x22222222,
    0xEEEEEEE2, 0xEEE0EEEE, 0x2EEEEEEE, 0xEEEE0222,
    0xEEEEE00E, 0xEEEEEEEE, 0xEEEEEEEE,
#endif
};

void INIT_ATTR gpio_preinit(void)
{
    for (int i = 0; i < GPIO_N_GROUPS; i++) {
        PCON(i) = gpio_data[i];
        PUNB(i) = 0;
        PUNC(i) = 0;
    }
#if defined(IPOD_NANO4G)
    PUNB(0) = 0x20;
    PUNC(1) = 0x40;
    GPIOUNK388 = (GPIOUNK388 & ~3) | 1;
#endif
}

void INIT_ATTR gpio_init(void)
{
#ifdef IPOD_6G    
    /* Capture hardware versions:
     *
     * HW version 1 includes an amplifier for the jack plug
     * microphone, it is activated configuring GPIO E7 as output
     * high. It is posible to detect capture HW version (even
     * when HP are not plugged) reading GPIO E7:
     *
     *   Ver  GPIO E7  models       capture support
     *   ---  -------  ------       ---------------
     *   0    1        80/160fat    dock line-in
     *   1    0        120/160slim  dock line-in + jack mic
     */
    GPIOCMD = 0xe0700;
    udelay(10000);
    rec_hw_ver = (PDAT(14) & (1 << 7)) ? 0 : 1;
    GPIOCMD = 0xe070e;  /* restore default configuration */

    if (rec_hw_ver == 0) {
        GPIOCMD = 0xe060e;
    }
    else {
        /* GPIO E6 is connected to mikey IRQ line (active low),
           configure it as pull-up input */
        GPIOCMD = 0xe0600;
        PUNB(14) |= (1 << 6);
    }
#elif defined(IPOD_NANO3G)
    rec_hw_ver = 0;
#endif
}

#if 0
uint32_t gpio_group_get(int group)
{
    uint32_t pcon = PCON(group);
    uint32_t pdat = PDAT(group);
    uint32_t group_cfg = 0;
    int i;

    for (i = 0; i < 8; i++) {
        int pin_cfg = pcon & 0xF;
        if (pin_cfg == 1) /* output */
            pin_cfg = 0xE | ((pdat >> i) & 1);
        group_cfg = (group_cfg >> 4) | (pin_cfg << 28);
        pcon >>= 4;
    }

    return group_cfg;
}

void gpio_group_set(int group, uint32_t mask, uint32_t cfg)
{
    PCON(group) = (PCON(group) & ~mask) | (cfg & mask);
}
#endif


/*
 * eINT API
 */
#ifndef EINT_MAX_HANDLERS
#define EINT_MAX_HANDLERS 1
#endif
static struct eic_handler* l_handlers[EINT_MAX_HANDLERS] IDATA_ATTR;

void INIT_ATTR eint_init(void)
{
    /* disable and clear external interrupts */
    for (int i = 0; i < EIC_N_GROUPS; i++) {
        EIC_INTEN(i) = 0;
        EIC_INTLEVEL(i) = 0;
        EIC_INTTYPE(i) = 0;
        EIC_INTSTAT(i) = ~0;
    }        
}

void eint_register(struct eic_handler *h)
{
    int i;
    int flags = disable_irq_save();

    for (i = 0; i < EINT_MAX_HANDLERS; i++) {
        if (!l_handlers[i]) {
            int group = EIC_GROUP(h->gpio_n);
            int index = EIC_INDEX(h->gpio_n);

            EIC_INTTYPE(group) |= (h->type << index);
            EIC_INTLEVEL(group) |= (h->level << index);
            EIC_INTSTAT(group) = (1 << index); /* clear */
            EIC_INTEN(group) |= (1 << index); /* enable */

            /* XXX: valid only for gpio_n = 0..127 (IRQ_EXT0..IRQ_EXT3) */
            VIC0INTENABLE = 1 << (IRQ_EXT0 + (h->gpio_n >> 5));

            l_handlers[i] = h;
            break;
        }
    }

    restore_irq(flags);

    if (i == EINT_MAX_HANDLERS)
        panicf("%s(): too many handlers!", __func__);
}

void eint_unregister(struct eic_handler *h)
{
    int flags = disable_irq_save();

    for (int i = 0; i < EINT_MAX_HANDLERS; i++) {
        if (l_handlers[i] == h) {
            int group = EIC_GROUP(h->gpio_n);
            int index = EIC_INDEX(h->gpio_n);

            EIC_INTEN(group) &= ~(1 << index); /* disable */

            /* XXX: valid only for gpio_n = 0..127 (IRQ_EXT0..IRQ_EXT3) */
            if (EIC_INTEN(group) == 0)
                VIC0INTENCLEAR = 1 << (IRQ_EXT0 + (h->gpio_n >> 5));

            l_handlers[i] = NULL;
            break;
        }
    }

    restore_irq(flags);
}

/* ISR */
static void ICODE_ATTR eint_handler(int group)
{
    int i;
    uint32_t ints;

    ints = EIC_INTSTAT(group) & EIC_INTEN(group);

    for (i = 0; i < EINT_MAX_HANDLERS; i++) {
        struct eic_handler *h = l_handlers[i];

        if (!h || (EIC_GROUP(h->gpio_n) != group))
            continue;

        uint32_t bit = 1 << EIC_INDEX(h->gpio_n);

        if (ints & bit) {
            /* Clear INTTYPE_EDGE interrupt, to clear INTTYPE_LEVEL
               interrupts the source (line level) must be "cleared" */
            if (h->type == EIC_INTTYPE_EDGE)
                EIC_INTSTAT(group) = bit; /* clear */

            if (h->autoflip)
                EIC_INTLEVEL(group) ^= bit; /* swap level */

            if (h->isr)
                h->isr(h); /* exec app handler */
        }
    }
}

void ICODE_ATTR INT_EXT0(void)
{
    eint_handler(6);
}

void ICODE_ATTR INT_EXT1(void)
{
    eint_handler(5);
}

void ICODE_ATTR INT_EXT2(void)
{
    eint_handler(4);
}

void ICODE_ATTR INT_EXT3(void)
{
    eint_handler(3);
}

void ICODE_ATTR INT_EXT6(void)
{
    eint_handler(0);
}