blob: 090182bed44b6f2f2dfebb10c295849b103d6a36 [file] [log] [blame]
Michael Sevakisa7af9e42008-04-12 16:56:45 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (c) 2008 by Michael Sevakis
11 *
12 * IMX31 GPIO event manager
13 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000014 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
Michael Sevakisa7af9e42008-04-12 16:56:45 +000018 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23#include "config.h"
24#include "system.h"
25#include "avic-imx31.h"
26#include "gpio-imx31.h"
27
28/* UIE vector found in avic-imx31.c */
29extern void UIE_VECTOR(void);
30
31/* Event lists are allocated for the specific target */
32#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
33static __attribute__((interrupt("IRQ"))) void GPIO1_HANDLER(void);
34extern const struct gpio_event_list gpio1_event_list;
35#endif
36
37#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
38static __attribute__((interrupt("IRQ"))) void GPIO2_HANDLER(void);
39extern const struct gpio_event_list gpio2_event_list;
40#endif
41
42#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
43static __attribute__((interrupt("IRQ"))) void GPIO3_HANDLER(void);
44extern const struct gpio_event_list gpio3_event_list;
45#endif
46
47static struct gpio_module_descriptor
48{
Michael Sevakis88451d52008-04-27 21:32:10 +000049 struct gpio_map * const base; /* Module base address */
Michael Sevakisa7af9e42008-04-12 16:56:45 +000050 enum IMX31_INT_LIST ints; /* AVIC int number */
51 void (*handler)(void); /* Interrupt function */
52 const struct gpio_event_list *list; /* Event handler list */
53} gpio_descs[GPIO_NUM_GPIO] =
54{
55#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
56 {
Michael Sevakis88451d52008-04-27 21:32:10 +000057 .base = (struct gpio_map *)GPIO1_BASE_ADDR,
Michael Sevakisa7af9e42008-04-12 16:56:45 +000058 .ints = GPIO1,
59 .handler = GPIO1_HANDLER,
60 },
61#endif
62#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
63 {
Michael Sevakis88451d52008-04-27 21:32:10 +000064 .base = (struct gpio_map *)GPIO2_BASE_ADDR,
Michael Sevakisa7af9e42008-04-12 16:56:45 +000065 .ints = GPIO2,
66 .handler = GPIO2_HANDLER,
67 },
68#endif
69#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
70 {
Michael Sevakis88451d52008-04-27 21:32:10 +000071 .base = (struct gpio_map *)GPIO3_BASE_ADDR,
Michael Sevakisa7af9e42008-04-12 16:56:45 +000072 .ints = GPIO3,
73 .handler = GPIO3_HANDLER,
74 },
75#endif
76};
77
Michael Sevakisa9c20f52008-05-21 08:42:11 +000078static void gpio_call_events(const struct gpio_module_descriptor * const desc)
Michael Sevakisa7af9e42008-04-12 16:56:45 +000079{
Michael Sevakisa7af9e42008-04-12 16:56:45 +000080 const struct gpio_event_list * const list = desc->list;
Michael Sevakis88451d52008-04-27 21:32:10 +000081 struct gpio_map * const base = desc->base;
Michael Sevakisa9c20f52008-05-21 08:42:11 +000082 const struct gpio_event * event, *event_last;
Michael Sevakisa7af9e42008-04-12 16:56:45 +000083
84 /* Intersect pending and unmasked bits */
Michael Sevakisa9c20f52008-05-21 08:42:11 +000085 uint32_t pnd = base->isr & base->imr;
86
87 event = list->events;
88 event_last = event + list->count;
Michael Sevakisa7af9e42008-04-12 16:56:45 +000089
90 /* Call each event handler in order */
Michael Sevakisa9c20f52008-05-21 08:42:11 +000091 /* .count is surely expected to be > 0 */
92 do
Michael Sevakisa7af9e42008-04-12 16:56:45 +000093 {
Michael Sevakisa9c20f52008-05-21 08:42:11 +000094 uint32_t mask = event->mask;
Michael Sevakisa7af9e42008-04-12 16:56:45 +000095
Michael Sevakisa9c20f52008-05-21 08:42:11 +000096 if (pnd & mask)
97 {
98 event->callback();
99 pnd &= ~mask;
100 }
101
102 if (pnd == 0)
103 break; /* Teminate early if nothing more to service */
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000104 }
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000105 while (++event < event_last);
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000106
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000107 if (pnd != 0)
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000108 {
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000109 /* One or more weren't handled */
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000110 UIE_VECTOR();
111 }
112}
113
114#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
115static __attribute__((interrupt("IRQ"))) void GPIO1_HANDLER(void)
116{
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000117 gpio_call_events(&gpio_descs[GPIO1_NUM]);
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000118}
119#endif
120
121#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
122static __attribute__((interrupt("IRQ"))) void GPIO2_HANDLER(void)
123{
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000124 gpio_call_events(&gpio_descs[GPIO2_NUM]);
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000125}
126#endif
127
128#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
129static __attribute__((interrupt("IRQ"))) void GPIO3_HANDLER(void)
130{
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000131 gpio_call_events(&gpio_descs[GPIO3_NUM]);
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000132}
133#endif
134
135void gpio_init(void)
136{
137 /* Mask-out GPIO interrupts - enable what's wanted later */
138 GPIO1_IMR = 0;
139 GPIO2_IMR = 0;
140 GPIO3_IMR = 0;
141
142 /* Init the externally-defined event lists for each port */
143#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
144 gpio_descs[GPIO1_NUM].list = &gpio1_event_list;
145#endif
146#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
147 gpio_descs[GPIO2_NUM].list = &gpio2_event_list;
148#endif
149#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
150 gpio_descs[GPIO3_NUM].list = &gpio3_event_list;
151#endif
152}
153
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000154bool gpio_enable_event(enum gpio_event_ids id)
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000155{
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000156 const struct gpio_module_descriptor * const desc = &gpio_descs[id >> 5];
157 const struct gpio_event * const event = &desc->list->events[id & 31];
Michael Sevakis88451d52008-04-27 21:32:10 +0000158 struct gpio_map * const base = desc->base;
159 volatile uint32_t *icr;
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000160 uint32_t mask, line;
Michael Sevakis88451d52008-04-27 21:32:10 +0000161 uint32_t imr;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000162 int shift;
163
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000164 int oldlevel = disable_irq_save();
165
Michael Sevakis88451d52008-04-27 21:32:10 +0000166 imr = base->imr;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000167
168 if (imr == 0)
169 {
170 /* First enabled interrupt for this GPIO */
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000171 avic_enable_int(desc->ints, IRQ, desc->list->ints_priority,
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000172 desc->handler);
173 }
174
175 /* Set the line sense */
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000176 line = find_first_set_bit(event->mask);
177 icr = &base->icr[line >> 4];
178 shift = (line & 15) << 1;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000179 mask = GPIO_SENSE_CONFIG_MASK << shift;
180
181 *icr = (*icr & ~mask) | ((event->sense << shift) & mask);
182
183 /* Unmask the line */
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000184 base->imr = imr | event->mask;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000185
186 restore_irq(oldlevel);
187
188 return true;
189}
190
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000191void gpio_disable_event(enum gpio_event_ids id)
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000192{
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000193 const struct gpio_module_descriptor * const desc = &gpio_descs[id >> 5];
194 const struct gpio_event * const event = &desc->list->events[id & 31];
Michael Sevakis88451d52008-04-27 21:32:10 +0000195 struct gpio_map * const base = desc->base;
196 uint32_t imr;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000197
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000198 int oldlevel = disable_irq_save();
199
200 /* Remove bit from mask */
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000201 imr = base->imr & ~event->mask;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000202
203 /* Mask the line */
Michael Sevakis88451d52008-04-27 21:32:10 +0000204 base->imr = imr;
Michael Sevakisa7af9e42008-04-12 16:56:45 +0000205
206 if (imr == 0)
207 {
208 /* No events remain enabled */
209 avic_disable_int(desc->ints);
210 }
211
212 restore_irq(oldlevel);
213}