uboot/arch/arm/cpu/arm926ejs/nomadik/gpio.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2009 Alessandro Rubini
   3 *
   4 * See file CREDITS for list of people who contributed to this
   5 * project.
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License as
   9 * published by the Free Software Foundation; either version 2 of
  10 * the License, or (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20 * MA 02111-1307 USA
  21 */
  22
  23#include <common.h>
  24#include <asm/io.h>
  25#include <asm/arch/gpio.h>
  26
  27static unsigned long gpio_base[4] = {
  28        NOMADIK_GPIO0_BASE,
  29        NOMADIK_GPIO1_BASE,
  30        NOMADIK_GPIO2_BASE,
  31        NOMADIK_GPIO3_BASE
  32};
  33
  34enum gpio_registers {
  35        GPIO_DAT =      0x00,           /* data register */
  36        GPIO_DATS =     0x04,           /* data set */
  37        GPIO_DATC =     0x08,           /* data clear */
  38        GPIO_PDIS =     0x0c,           /* pull disable */
  39        GPIO_DIR =      0x10,           /* direction */
  40        GPIO_DIRS =     0x14,           /* direction set */
  41        GPIO_DIRC =     0x18,           /* direction clear */
  42        GPIO_AFSLA =    0x20,           /* alternate function select A */
  43        GPIO_AFSLB =    0x24,           /* alternate function select B */
  44};
  45
  46static inline unsigned long gpio_to_base(int gpio)
  47{
  48        return gpio_base[gpio / 32];
  49}
  50
  51static inline u32 gpio_to_bit(int gpio)
  52{
  53        return 1 << (gpio & 0x1f);
  54}
  55
  56void nmk_gpio_af(int gpio, int alternate_function)
  57{
  58        unsigned long base = gpio_to_base(gpio);
  59        u32 bit = gpio_to_bit(gpio);
  60        u32 afunc, bfunc;
  61
  62        /* alternate function is 0..3, with one bit per register */
  63        afunc = readl(base + GPIO_AFSLA) & ~bit;
  64        bfunc = readl(base + GPIO_AFSLB) & ~bit;
  65        if (alternate_function & 1) afunc |= bit;
  66        if (alternate_function & 2) bfunc |= bit;
  67        writel(afunc, base + GPIO_AFSLA);
  68        writel(bfunc, base + GPIO_AFSLB);
  69}
  70
  71void nmk_gpio_dir(int gpio, int dir)
  72{
  73        unsigned long base = gpio_to_base(gpio);
  74        u32 bit = gpio_to_bit(gpio);
  75
  76        if (dir)
  77                writel(bit, base + GPIO_DIRS);
  78        else
  79                writel(bit, base + GPIO_DIRC);
  80}
  81
  82void nmk_gpio_set(int gpio, int val)
  83{
  84        unsigned long base = gpio_to_base(gpio);
  85        u32 bit = gpio_to_bit(gpio);
  86
  87        if (val)
  88                writel(bit, base + GPIO_DATS);
  89        else
  90                writel(bit, base + GPIO_DATC);
  91}
  92
  93int nmk_gpio_get(int gpio)
  94{
  95        unsigned long base = gpio_to_base(gpio);
  96        u32 bit = gpio_to_bit(gpio);
  97
  98        return readl(base + GPIO_DAT) & bit;
  99}
 100