]> err.no Git - linux-2.6/blobdiff - arch/blackfin/kernel/bfin_gpio.c
Blackfin arch: extract gpio number from PIN function
[linux-2.6] / arch / blackfin / kernel / bfin_gpio.c
index bafcfa52142ba0731f5caf3ecc71e672d7678285..0d1e87b9c93d2169f8f87ccc07beae7369703f16 100644 (file)
 *  GPIO_47      PH15        PF47
 */
 
+#include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/err.h>
 #include <asm/blackfin.h>
 #include <asm/gpio.h>
+#include <asm/portmux.h>
 #include <linux/irq.h>
 
 #ifdef BF533_FAMILY
@@ -115,7 +117,16 @@ static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
 };
 #endif
 
-static unsigned short reserved_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
+static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
+static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
+
+#define MAX_RESOURCES          256
+#define RESOURCE_LABEL_SIZE    16
+
+struct str_ident {
+       char name[RESOURCE_LABEL_SIZE];
+} *str_ident;
+
 
 #ifdef CONFIG_PM
 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
@@ -143,22 +154,124 @@ inline int check_gpio(unsigned short gpio)
        return 0;
 }
 
+static void set_label(unsigned short ident, const char *label)
+{
+
+       if (label && str_ident) {
+               strncpy(str_ident[ident].name, label,
+                        RESOURCE_LABEL_SIZE);
+               str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
+       }
+}
+
+static char *get_label(unsigned short ident)
+{
+       if (!str_ident)
+               return "UNKNOWN";
+
+       return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
+}
+
+static int cmp_label(unsigned short ident, const char *label)
+{
+       if (label && str_ident)
+               return strncmp(str_ident[ident].name,
+                                label, strlen(label));
+       else
+               return -EINVAL;
+}
+
 #ifdef BF537_FAMILY
 static void port_setup(unsigned short gpio, unsigned short usage)
 {
-       if (usage == GPIO_USAGE) {
-               if (*port_fer[gpio_bank(gpio)] & gpio_bit(gpio))
-                       printk(KERN_WARNING "bfin-gpio: Possible Conflict with Peripheral "
-                              "usage and GPIO %d detected!\n", gpio);
-               *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
-       } else
-               *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
-       SSYNC();
+       if (!check_gpio(gpio)) {
+               if (usage == GPIO_USAGE) {
+                       *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
+               } else
+                       *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
+               SSYNC();
+       }
 }
 #else
 # define port_setup(...)  do { } while (0)
 #endif
 
+#ifdef BF537_FAMILY
+
+static struct {
+       unsigned short res;
+       unsigned short offset;
+} port_mux_lut[] = {
+       {.res = P_PPI0_D13, .offset = 11},
+       {.res = P_PPI0_D14, .offset = 11},
+       {.res = P_PPI0_D15, .offset = 11},
+       {.res = P_SPORT1_TFS, .offset = 11},
+       {.res = P_SPORT1_TSCLK, .offset = 11},
+       {.res = P_SPORT1_DTPRI, .offset = 11},
+       {.res = P_PPI0_D10, .offset = 10},
+       {.res = P_PPI0_D11, .offset = 10},
+       {.res = P_PPI0_D12, .offset = 10},
+       {.res = P_SPORT1_RSCLK, .offset = 10},
+       {.res = P_SPORT1_RFS, .offset = 10},
+       {.res = P_SPORT1_DRPRI, .offset = 10},
+       {.res = P_PPI0_D8, .offset = 9},
+       {.res = P_PPI0_D9, .offset = 9},
+       {.res = P_SPORT1_DRSEC, .offset = 9},
+       {.res = P_SPORT1_DTSEC, .offset = 9},
+       {.res = P_TMR2, .offset = 8},
+       {.res = P_PPI0_FS3, .offset = 8},
+       {.res = P_TMR3, .offset = 7},
+       {.res = P_SPI0_SSEL4, .offset = 7},
+       {.res = P_TMR4, .offset = 6},
+       {.res = P_SPI0_SSEL5, .offset = 6},
+       {.res = P_TMR5, .offset = 5},
+       {.res = P_SPI0_SSEL6, .offset = 5},
+       {.res = P_UART1_RX, .offset = 4},
+       {.res = P_UART1_TX, .offset = 4},
+       {.res = P_TMR6, .offset = 4},
+       {.res = P_TMR7, .offset = 4},
+       {.res = P_UART0_RX, .offset = 3},
+       {.res = P_UART0_TX, .offset = 3},
+       {.res = P_DMAR0, .offset = 3},
+       {.res = P_DMAR1, .offset = 3},
+       {.res = P_SPORT0_DTSEC, .offset = 1},
+       {.res = P_SPORT0_DRSEC, .offset = 1},
+       {.res = P_CAN0_RX, .offset = 1},
+       {.res = P_CAN0_TX, .offset = 1},
+       {.res = P_SPI0_SSEL7, .offset = 1},
+       {.res = P_SPORT0_TFS, .offset = 0},
+       {.res = P_SPORT0_DTPRI, .offset = 0},
+       {.res = P_SPI0_SSEL2, .offset = 0},
+       {.res = P_SPI0_SSEL3, .offset = 0},
+};
+
+static void portmux_setup(unsigned short per, unsigned short function)
+{
+       u16 y, offset, muxreg;
+
+       for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
+               if (port_mux_lut[y].res == per) {
+
+                       /* SET PORTMUX REG */
+
+                       offset = port_mux_lut[y].offset;
+                       muxreg = bfin_read_PORT_MUX();
+
+                       if (offset != 1) {
+                               muxreg &= ~(1 << offset);
+                       } else {
+                               muxreg &= ~(3 << 1);
+                       }
+
+                       muxreg |= (function << offset);
+                       bfin_write_PORT_MUX(muxreg);
+               }
+       }
+}
+
+#else
+# define portmux_setup(...)  do { } while (0)
+#endif
 
 static void default_gpio(unsigned short gpio)
 {
@@ -179,24 +292,18 @@ static void default_gpio(unsigned short gpio)
 
 static int __init bfin_gpio_init(void)
 {
-       int i;
-
-       printk(KERN_INFO "Blackfin GPIO Controller\n");
+       str_ident = kcalloc(MAX_RESOURCES,
+                                sizeof(struct str_ident), GFP_KERNEL);
+       if (str_ident == NULL)
+               return -ENOMEM;
 
-       for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE)
-               reserved_map[gpio_bank(i)] = 0;
+       memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
 
-#if defined(BF537_FAMILY) && (defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE))
-# if defined(CONFIG_BFIN_MAC_RMII)
-       reserved_map[gpio_bank(PORT_H)] = 0xC373;
-# else
-       reserved_map[gpio_bank(PORT_H)] = 0xFFFF;
-# endif
-#endif
+       printk(KERN_INFO "Blackfin GPIO Controller\n");
 
        return 0;
-}
 
+}
 arch_initcall(bfin_gpio_init);
 
 
@@ -223,7 +330,7 @@ arch_initcall(bfin_gpio_init);
 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
 { \
        unsigned long flags; \
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
        local_irq_save(flags); \
        if (arg) \
                gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
@@ -243,7 +350,7 @@ SET_GPIO(both)
 #define SET_GPIO_SC(name) \
 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
 { \
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
        if (arg) \
                gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
        else \
@@ -254,11 +361,11 @@ EXPORT_SYMBOL(set_gpio_ ## name);
 SET_GPIO_SC(maska)
 SET_GPIO_SC(maskb)
 
-#if defined(ANOMALY_05000311)
+#if ANOMALY_05000311
 void set_gpio_data(unsigned short gpio, unsigned short arg)
 {
        unsigned long flags;
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
        local_irq_save(flags);
        if (arg)
                gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
@@ -273,11 +380,11 @@ SET_GPIO_SC(data)
 #endif
 
 
-#if defined(ANOMALY_05000311)
+#if ANOMALY_05000311
 void set_gpio_toggle(unsigned short gpio)
 {
        unsigned long flags;
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
        local_irq_save(flags);
        gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
        bfin_read_CHIPID();
@@ -286,7 +393,7 @@ void set_gpio_toggle(unsigned short gpio)
 #else
 void set_gpio_toggle(unsigned short gpio)
 {
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
        gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
 }
 #endif
@@ -311,7 +418,7 @@ SET_GPIO_P(maska)
 SET_GPIO_P(maskb)
 
 
-#if defined(ANOMALY_05000311)
+#if ANOMALY_05000311
 void set_gpiop_data(unsigned short gpio, unsigned short arg)
 {
        unsigned long flags;
@@ -345,12 +452,12 @@ GET_GPIO(maska)
 GET_GPIO(maskb)
 
 
-#if defined(ANOMALY_05000311)
+#if ANOMALY_05000311
 unsigned short get_gpio_data(unsigned short gpio)
 {
        unsigned long flags;
        unsigned short ret;
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
        local_irq_save(flags);
        ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
        bfin_read_CHIPID();
@@ -379,7 +486,7 @@ GET_GPIO_P(both)
 GET_GPIO_P(maska)
 GET_GPIO_P(maskb)
 
-#if defined(ANOMALY_05000311)
+#if ANOMALY_05000311
 unsigned short get_gpiop_data(unsigned short gpio)
 {
        unsigned long flags;
@@ -494,13 +601,14 @@ u32 gpio_pm_setup(void)
                        gpio_bank_saved[bank].dir   = gpio_bankb[bank]->dir;
                        gpio_bank_saved[bank].edge  = gpio_bankb[bank]->edge;
                        gpio_bank_saved[bank].both  = gpio_bankb[bank]->both;
-                       gpio_bank_saved[bank].reserved = reserved_map[bank];
+                       gpio_bank_saved[bank].reserved =
+                                               reserved_gpio_map[bank];
 
                        gpio = i;
 
                        while (mask) {
                                if (mask & 1) {
-                                       reserved_map[gpio_bank(gpio)] |=
+                                       reserved_gpio_map[gpio_bank(gpio)] |=
                                                        gpio_bit(gpio);
                                        bfin_gpio_wakeup_type(gpio,
                                                wakeup_flags_map[gpio]);
@@ -540,7 +648,8 @@ void gpio_pm_restore(void)
                        gpio_bankb[bank]->edge  = gpio_bank_saved[bank].edge;
                        gpio_bankb[bank]->both  = gpio_bank_saved[bank].both;
 
-                       reserved_map[bank] = gpio_bank_saved[bank].reserved;
+                       reserved_gpio_map[bank] =
+                                       gpio_bank_saved[bank].reserved;
 
                }
 
@@ -550,6 +659,147 @@ void gpio_pm_restore(void)
 
 #endif
 
+
+
+
+int peripheral_request(unsigned short per, const char *label)
+{
+       unsigned long flags;
+       unsigned short ident = P_IDENT(per);
+
+       /*
+        * Don't cares are pins with only one dedicated function
+        */
+
+       if (per & P_DONTCARE)
+               return 0;
+
+       if (!(per & P_DEFINED))
+               return -ENODEV;
+
+       local_irq_save(flags);
+
+       if (!check_gpio(ident)) {
+
+       if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
+               printk(KERN_ERR
+                      "%s: Peripheral %d is already reserved as GPIO by %s !\n",
+                      __FUNCTION__, ident, get_label(ident));
+               dump_stack();
+               local_irq_restore(flags);
+               return -EBUSY;
+       }
+
+       }
+
+       if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
+
+       /*
+        * Pin functions like AMC address strobes my
+        * be requested and used by several drivers
+        */
+
+       if (!(per & P_MAYSHARE)) {
+
+       /*
+        * Allow that the identical pin function can
+        * be requested from the same driver twice
+        */
+
+               if (cmp_label(ident, label) == 0)
+                       goto anyway;
+
+                       printk(KERN_ERR
+                              "%s: Peripheral %d function %d is already"
+                              " reserved by %s !\n",
+                              __FUNCTION__, ident, P_FUNCT2MUX(per),
+                               get_label(ident));
+                       dump_stack();
+                       local_irq_restore(flags);
+                       return -EBUSY;
+               }
+
+       }
+
+anyway:
+
+
+       portmux_setup(per, P_FUNCT2MUX(per));
+
+       port_setup(ident, PERIPHERAL_USAGE);
+
+       reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
+       local_irq_restore(flags);
+       set_label(ident, label);
+
+       return 0;
+}
+EXPORT_SYMBOL(peripheral_request);
+
+int peripheral_request_list(unsigned short per[], const char *label)
+{
+       u16 cnt;
+       int ret;
+
+       for (cnt = 0; per[cnt] != 0; cnt++) {
+
+               ret = peripheral_request(per[cnt], label);
+
+               if (ret < 0) {
+                       for ( ; cnt > 0; cnt--) {
+                               peripheral_free(per[cnt - 1]);
+                       }
+               return ret;
+               }
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(peripheral_request_list);
+
+void peripheral_free(unsigned short per)
+{
+       unsigned long flags;
+       unsigned short ident = P_IDENT(per);
+
+       if (per & P_DONTCARE)
+               return;
+
+       if (!(per & P_DEFINED))
+               return;
+
+       if (check_gpio(ident) < 0)
+               return;
+
+       local_irq_save(flags);
+
+       if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
+                        & gpio_bit(ident)))) {
+               local_irq_restore(flags);
+               return;
+       }
+
+       if (!(per & P_MAYSHARE)) {
+               port_setup(ident, GPIO_USAGE);
+       }
+
+       reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
+
+       local_irq_restore(flags);
+}
+EXPORT_SYMBOL(peripheral_free);
+
+void peripheral_free_list(unsigned short per[])
+{
+       u16 cnt;
+
+       for (cnt = 0; per[cnt] != 0; cnt++) {
+               peripheral_free(per[cnt]);
+       }
+
+}
+EXPORT_SYMBOL(peripheral_free_list);
+
 /***********************************************************
 *
 * FUNCTIONS: Blackfin GPIO Driver
@@ -574,13 +824,13 @@ int gpio_request(unsigned short gpio, const char *label)
 
        local_irq_save(flags);
 
-       if (unlikely(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
+       if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
                printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
                dump_stack();
                local_irq_restore(flags);
                return -EBUSY;
        }
-       reserved_map[gpio_bank(gpio)] |= gpio_bit(gpio);
+       reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
 
        local_irq_restore(flags);
 
@@ -599,7 +849,7 @@ void gpio_free(unsigned short gpio)
 
        local_irq_save(flags);
 
-       if (unlikely(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
+       if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
                printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
                dump_stack();
                local_irq_restore(flags);
@@ -608,7 +858,7 @@ void gpio_free(unsigned short gpio)
 
        default_gpio(gpio);
 
-       reserved_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
+       reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
 
        local_irq_restore(flags);
 }
@@ -618,7 +868,7 @@ void gpio_direction_input(unsigned short gpio)
 {
        unsigned long flags;
 
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
 
        local_irq_save(flags);
        gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
@@ -631,7 +881,7 @@ void gpio_direction_output(unsigned short gpio)
 {
        unsigned long flags;
 
-       BUG_ON(!(reserved_map[gpio_bank(gpio)] & gpio_bit(gpio)));
+       BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
 
        local_irq_save(flags);
        gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
@@ -639,3 +889,22 @@ void gpio_direction_output(unsigned short gpio)
        local_irq_restore(flags);
 }
 EXPORT_SYMBOL(gpio_direction_output);
+
+/* If we are booting from SPI and our board lacks a strong enough pull up,
+ * the core can reset and execute the bootrom faster than the resistor can
+ * pull the signal logically high.  To work around this (common) error in
+ * board design, we explicitly set the pin back to GPIO mode, force /CS
+ * high, and wait for the electrons to do their thing.
+ *
+ * This function only makes sense to be called from reset code, but it
+ * lives here as we need to force all the GPIO states w/out going through
+ * BUG() checks and such.
+ */
+void bfin_gpio_reset_spi0_ssel1(void)
+{
+       u16 gpio = P_IDENT(P_SPI0_SSEL1);
+
+       port_setup(gpio, GPIO_USAGE);
+       gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
+       udelay(1);
+}