2012-12-10 13:34:42 +01:00
|
|
|
/*
|
|
|
|
* \brief Gpio driver for the i.MX53
|
|
|
|
* \author Ivan Loskutov <ivan.loskutov@ksyslabs.org>
|
|
|
|
* \author Nikolay Golikov <nik@ksyslabs.org>
|
2013-05-06 14:33:30 +02:00
|
|
|
* \author Stefan Kalkowski <stefan.kalkowski@genode-labs.com>
|
2012-12-10 13:34:42 +01:00
|
|
|
* \date 2012-12-04
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Ksys Labs LLC
|
2013-05-06 14:33:30 +02:00
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
2012-12-10 13:34:42 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _DRIVER_H_
|
|
|
|
#define _DRIVER_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
2013-05-06 14:33:30 +02:00
|
|
|
#include <drivers/board_base.h>
|
|
|
|
#include <gpio/driver.h>
|
|
|
|
#include <irq_session/connection.h>
|
2012-12-10 13:34:42 +01:00
|
|
|
#include <timer_session/connection.h>
|
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include "gpio.h"
|
|
|
|
|
|
|
|
static int verbose = 0;
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
|
|
|
|
class Imx53_driver : public Gpio::Driver
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
2013-05-06 14:33:30 +02:00
|
|
|
private:
|
|
|
|
|
2012-12-10 13:34:42 +01:00
|
|
|
enum {
|
2013-05-06 14:33:30 +02:00
|
|
|
MAX_BANKS = 7,
|
|
|
|
MAX_PINS = 32
|
2012-12-10 13:34:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
struct Timer_delayer : Timer::Connection, Genode::Mmio::Delayer
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Implementation of 'Delayer' interface
|
|
|
|
*/
|
2013-05-06 14:33:30 +02:00
|
|
|
void usleep(unsigned us) { Timer::Connection::usleep(us); }
|
2012-12-10 13:34:42 +01:00
|
|
|
} _delayer;
|
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
class Gpio_bank
|
|
|
|
{
|
|
|
|
public:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void handle_irq();
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
private:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
class Irq_handler : public Genode::Thread<4096>
|
|
|
|
{
|
|
|
|
private:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Genode::Irq_connection _irq;
|
|
|
|
Gpio_bank *_bank;
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
public:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Irq_handler(unsigned irq, Gpio_bank *bank)
|
|
|
|
: Genode::Thread<4096>("irq handler"),
|
|
|
|
_irq(irq), _bank(bank) { start(); }
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void entry()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
_irq.wait_for_irq();
|
|
|
|
_bank->handle_irq();
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
};
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg _reg;
|
|
|
|
Irq_handler _irqh_low;
|
|
|
|
Irq_handler _irqh_high;
|
|
|
|
Genode::Signal_context_capability _sig_cap[MAX_PINS];
|
|
|
|
bool _irq_enabled[MAX_PINS];
|
|
|
|
Genode::Lock _lock;
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
public:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_bank(Genode::addr_t base, Genode::size_t size,
|
|
|
|
unsigned irq_low, unsigned irq_high)
|
|
|
|
: _reg(base, size),
|
|
|
|
_irqh_low(irq_low, this),
|
|
|
|
_irqh_high(irq_high, this) { }
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg* regs() { return &_reg; }
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void irq(int pin, bool enable)
|
|
|
|
{
|
|
|
|
_reg.write<Gpio_reg::Int_mask>(enable ? 1 : 0, pin);
|
|
|
|
_irq_enabled[pin] = enable;
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void sigh(int pin, Genode::Signal_context_capability cap) {
|
|
|
|
_sig_cap[pin] = cap; }
|
|
|
|
};
|
2012-12-10 13:34:42 +01:00
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
static Gpio_bank _gpio_bank[MAX_BANKS];
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
int _gpio_bank_index(int gpio) { return gpio >> 5; }
|
|
|
|
int _gpio_index(int gpio) { return gpio & 0x1f; }
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Imx53_driver()
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
2013-05-06 14:33:30 +02:00
|
|
|
for (unsigned i = 0; i < MAX_BANKS; ++i) {
|
|
|
|
Gpio_reg *regs = _gpio_bank[i].regs();
|
|
|
|
for (unsigned j = 0; j < MAX_PINS; j++) {
|
|
|
|
regs->write<Gpio_reg::Int_conf>(Gpio_reg::Int_conf::LOW_LEVEL, j);
|
|
|
|
regs->write<Gpio_reg::Int_mask>(0, j);
|
|
|
|
}
|
|
|
|
regs->write<Gpio_reg::Int_stat>(0xffffffff);
|
2012-12-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
public:
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
static Imx53_driver& factory();
|
2012-12-10 13:34:42 +01:00
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
/******************************
|
|
|
|
** Gpio::Driver interface **
|
|
|
|
******************************/
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void direction(unsigned gpio, bool input)
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
2013-05-06 14:33:30 +02:00
|
|
|
if (verbose) PDBG("gpio=%d input=%d", gpio, input);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
gpio_reg->write<Gpio_reg::Dir>(input ? 0 : 1,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void write(unsigned gpio, bool level)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d level=%d", gpio, level);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
gpio_reg->write<Gpio_reg::Data>(level ? 1 : 0,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
bool read(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
return gpio_reg->read<Gpio_reg::Pad_stat>(_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void debounce_enable(unsigned gpio, bool enable)
|
|
|
|
{
|
|
|
|
PWRN("Not supported!");
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void debounce_time(unsigned gpio, unsigned long us)
|
|
|
|
{
|
|
|
|
PWRN("Not supported!");
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void falling_detect(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
gpio_reg->write<Gpio_reg::Int_conf>(Gpio_reg::Int_conf::FAL_EDGE,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void rising_detect(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
gpio_reg->write<Gpio_reg::Int_conf>(Gpio_reg::Int_conf::RIS_EDGE,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void high_detect(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
gpio_reg->write<Gpio_reg::Int_conf>(Gpio_reg::Int_conf::HIGH_LEVEL,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void low_detect(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Gpio_reg *gpio_reg = _gpio_bank[_gpio_bank_index(gpio)].regs();
|
|
|
|
gpio_reg->write<Gpio_reg::Int_conf>(Gpio_reg::Int_conf::LOW_LEVEL,
|
|
|
|
_gpio_index(gpio));
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void irq_enable(unsigned gpio, bool enable)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d enable=%d", gpio, enable);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
_gpio_bank[_gpio_bank_index(gpio)].irq(_gpio_index(gpio), enable);
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void register_signal(unsigned gpio,
|
|
|
|
Genode::Signal_context_capability cap)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
_gpio_bank[_gpio_bank_index(gpio)].sigh(_gpio_index(gpio), cap); }
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void unregister_signal(unsigned gpio)
|
|
|
|
{
|
|
|
|
if (verbose) PDBG("gpio=%d", gpio);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Genode::Signal_context_capability cap;
|
|
|
|
_gpio_bank[_gpio_bank_index(gpio)].sigh(_gpio_index(gpio), cap);
|
|
|
|
}
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
bool gpio_valid(unsigned gpio) { return gpio < (MAX_PINS*MAX_BANKS); }
|
|
|
|
};
|
2012-12-10 13:34:42 +01:00
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Imx53_driver::Gpio_bank Imx53_driver::_gpio_bank[Imx53_driver::MAX_BANKS] = {
|
2014-06-04 15:02:37 +02:00
|
|
|
{ Genode::Board_base::GPIO1_MMIO_BASE, Genode::Board_base::GPIO1_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO1_IRQL, Genode::Board_base::GPIO1_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO2_MMIO_BASE, Genode::Board_base::GPIO2_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO2_IRQL, Genode::Board_base::GPIO2_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO3_MMIO_BASE, Genode::Board_base::GPIO3_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO3_IRQL, Genode::Board_base::GPIO3_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO4_MMIO_BASE, Genode::Board_base::GPIO4_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO4_IRQL, Genode::Board_base::GPIO4_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO5_MMIO_BASE, Genode::Board_base::GPIO5_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO5_IRQL, Genode::Board_base::GPIO5_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO6_MMIO_BASE, Genode::Board_base::GPIO6_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO6_IRQL, Genode::Board_base::GPIO6_IRQH },
|
|
|
|
{ Genode::Board_base::GPIO7_MMIO_BASE, Genode::Board_base::GPIO7_MMIO_SIZE,
|
|
|
|
Genode::Board_base::GPIO7_IRQL, Genode::Board_base::GPIO7_IRQH }
|
2013-05-06 14:33:30 +02:00
|
|
|
};
|
2012-12-10 13:34:42 +01:00
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
Imx53_driver& Imx53_driver::factory()
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
2013-05-06 14:33:30 +02:00
|
|
|
static Imx53_driver driver;
|
|
|
|
return driver;
|
2012-12-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
void Imx53_driver::Gpio_bank::handle_irq()
|
2012-12-10 13:34:42 +01:00
|
|
|
{
|
2013-05-06 14:33:30 +02:00
|
|
|
Genode::Lock::Guard lock_guard(_lock);
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
unsigned long status = _reg.read<Gpio_reg::Int_stat>();
|
2012-12-10 13:34:42 +01:00
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
for(unsigned i = 0; i < MAX_PINS; i++) {
|
|
|
|
if ((status & (1 << i)) && _irq_enabled[i] &&
|
|
|
|
_sig_cap[i].valid())
|
|
|
|
Genode::Signal_transmitter(_sig_cap[i]).submit();
|
2012-12-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
_reg.write<Gpio_reg::Int_stat>(0xffffffff);
|
2012-12-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
2013-05-06 14:33:30 +02:00
|
|
|
|
2012-12-10 13:34:42 +01:00
|
|
|
#endif /* _DRIVER_H_ */
|