2008-04-12 16:56:45 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 by Michael Sevakis
|
|
|
|
*
|
|
|
|
* IMX31 GPIO event manager
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2008-04-12 16:56:45 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "avic-imx31.h"
|
2017-01-28 19:43:35 +00:00
|
|
|
#define DEFINE_GPIO_VECTOR_TABLE
|
|
|
|
#include "gpio-target.h"
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
/* UIE vector found in avic-imx31.c */
|
|
|
|
extern void UIE_VECTOR(void);
|
|
|
|
|
|
|
|
/* Event lists are allocated for the specific target */
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO1_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO2_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO3_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
static struct gpio_module_desc
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2010-05-07 10:53:19 +00:00
|
|
|
volatile unsigned long * const base; /* Module base address */
|
|
|
|
void (* const handler)(void); /* Interrupt function */
|
2017-01-28 19:43:35 +00:00
|
|
|
const struct gpio_event *events; /* Event handler list */
|
|
|
|
unsigned long enabled; /* Enabled event mask */
|
2010-05-07 10:53:19 +00:00
|
|
|
const uint8_t ints; /* AVIC int number */
|
|
|
|
const uint8_t int_priority; /* AVIC int priority */
|
2017-01-28 19:43:35 +00:00
|
|
|
uint8_t count; /* Number of events */
|
|
|
|
} * const gpio_descs[] =
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
|
2017-01-28 19:43:35 +00:00
|
|
|
[GPIO1_NUM] = &(struct gpio_module_desc) {
|
2010-05-07 10:53:19 +00:00
|
|
|
.base = (unsigned long *)GPIO1_BASE_ADDR,
|
|
|
|
.ints = INT_GPIO1,
|
|
|
|
.handler = GPIO1_HANDLER,
|
|
|
|
.int_priority = GPIO1_INT_PRIO
|
2008-04-12 16:56:45 +00:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
|
2017-01-28 19:43:35 +00:00
|
|
|
[GPIO2_NUM] = &(struct gpio_module_desc) {
|
2010-05-07 10:53:19 +00:00
|
|
|
.base = (unsigned long *)GPIO2_BASE_ADDR,
|
|
|
|
.ints = INT_GPIO2,
|
|
|
|
.handler = GPIO2_HANDLER,
|
|
|
|
.int_priority = GPIO2_INT_PRIO
|
2008-04-12 16:56:45 +00:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
|
2017-01-28 19:43:35 +00:00
|
|
|
[GPIO3_NUM] = &(struct gpio_module_desc) {
|
2010-05-07 10:53:19 +00:00
|
|
|
.base = (unsigned long *)GPIO3_BASE_ADDR,
|
|
|
|
.ints = INT_GPIO3,
|
|
|
|
.handler = GPIO3_HANDLER,
|
|
|
|
.int_priority = GPIO3_INT_PRIO,
|
2008-04-12 16:56:45 +00:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
#define GPIO_MODULE_CNT ARRAYLEN(gpio_descs)
|
|
|
|
|
|
|
|
static const struct gpio_event * event_from_id(
|
|
|
|
const struct gpio_module_desc *desc, enum gpio_id id)
|
|
|
|
{
|
|
|
|
const struct gpio_event *events = desc->events;
|
|
|
|
for (unsigned int i = 0; i < desc->count; i++)
|
|
|
|
{
|
|
|
|
if (events[i].id == id)
|
|
|
|
return &events[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-07 10:53:19 +00:00
|
|
|
static void gpio_call_events(enum gpio_module_number gpio)
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2017-01-28 19:43:35 +00:00
|
|
|
const struct gpio_module_desc * const desc = gpio_descs[gpio];
|
2010-05-07 10:53:19 +00:00
|
|
|
volatile unsigned long * const base = desc->base;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
/* Send only events that are not masked */
|
|
|
|
unsigned long pnd = base[GPIO_ISR] & base[GPIO_IMR];
|
2008-05-21 08:42:11 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
if (pnd & ~desc->enabled)
|
|
|
|
UIE_VECTOR(); /* One or more aren't handled properly */
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
const struct gpio_event *event = desc->events;
|
2008-05-21 08:42:11 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
while (pnd)
|
|
|
|
{
|
|
|
|
unsigned long mask = 1ul << (event->id % 32);
|
2008-05-21 08:42:11 +00:00
|
|
|
if (pnd & mask)
|
|
|
|
{
|
|
|
|
event->callback();
|
|
|
|
pnd &= ~mask;
|
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
event++;
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO1_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO1_HANDLER(void)
|
|
|
|
{
|
2010-05-07 10:53:19 +00:00
|
|
|
gpio_call_events(GPIO1_NUM);
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO2_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO2_HANDLER(void)
|
|
|
|
{
|
2010-05-07 10:53:19 +00:00
|
|
|
gpio_call_events(GPIO2_NUM);
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (GPIO_EVENT_MASK & USE_GPIO3_EVENTS)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void GPIO3_HANDLER(void)
|
|
|
|
{
|
2010-05-07 10:53:19 +00:00
|
|
|
gpio_call_events(GPIO3_NUM);
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-11 14:39:35 +00:00
|
|
|
void INIT_ATTR gpio_init(void)
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2017-01-28 19:43:35 +00:00
|
|
|
for (unsigned int mod = 0; mod < GPIO_MODULE_CNT; mod++)
|
|
|
|
{
|
|
|
|
struct gpio_module_desc * const desc = gpio_descs[mod];
|
|
|
|
if (!desc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Parse the event table per module. First contiguous run seen for a
|
|
|
|
* module is used. */
|
|
|
|
const struct gpio_event *event = gpio_event_vector_tbl;
|
|
|
|
for (unsigned int i = 0; i < gpio_event_vector_tbl_len; i++, event++)
|
|
|
|
{
|
|
|
|
if (event->id / 32 == mod)
|
|
|
|
{
|
|
|
|
desc->events = event;
|
|
|
|
while (++desc->count < 32 && (++event)->id / 32 == mod);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask-out GPIO interrupts - enable what's wanted later */
|
|
|
|
desc->base[GPIO_IMR] = 0;
|
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
bool gpio_enable_event(enum gpio_id id, bool enable)
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2017-01-28 19:43:35 +00:00
|
|
|
unsigned int mod = id / 32;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
struct gpio_module_desc * const desc = gpio_descs[mod];
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
if (mod >= GPIO_MODULE_CNT || desc == NULL)
|
|
|
|
return false;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
const struct gpio_event * const event = event_from_id(desc, id);
|
|
|
|
if (!event)
|
|
|
|
return false;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
volatile unsigned long * const base = desc->base;
|
|
|
|
unsigned long num = id % 32;
|
|
|
|
unsigned long mask = 1ul << num;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
unsigned long cpsr = disable_irq_save();
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
unsigned long imr = base[GPIO_IMR];
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
if (desc->enabled == 0)
|
|
|
|
{
|
|
|
|
/* First enabled interrupt for this GPIO */
|
|
|
|
avic_enable_int(desc->ints, INT_TYPE_IRQ, desc->int_priority,
|
|
|
|
desc->handler);
|
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
/* Set the line sense */
|
|
|
|
if (event->sense == GPIO_SENSE_EDGE_SEL)
|
|
|
|
{
|
|
|
|
/* Interrupt configuration register is ignored */
|
|
|
|
base[GPIO_EDGE_SEL] |= mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
volatile unsigned long *icrp = &base[GPIO_ICR + num / 16];
|
|
|
|
unsigned int shift = 2*(num % 16);
|
|
|
|
bitmod32(icrp, event->sense << shift, 0x3 << shift);
|
|
|
|
base[GPIO_EDGE_SEL] &= ~mask;
|
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
/* Unmask the line */
|
|
|
|
desc->enabled |= mask;
|
|
|
|
imr |= mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imr &= ~mask;
|
|
|
|
desc->enabled &= ~mask;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
if (desc->enabled == 0)
|
|
|
|
avic_disable_int(desc->ints); /* No events remain enabled */
|
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
base[GPIO_IMR] = imr;
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
restore_irq(cpsr);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2017-01-28 19:43:35 +00:00
|
|
|
return true;
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|