]> git.karo-electronics.de Git - karo-tx-uboot.git/blobdiff - drivers/gpio/mxc_gpio.c
arm: mx6: add support for i.MX6ULL
[karo-tx-uboot.git] / drivers / gpio / mxc_gpio.c
index 8bb9e39b7231e522f87eaf9612ad5abf383931e7..5c8ee0f99b118a95f28b43bb01d2a61cb797e570 100644 (file)
@@ -23,6 +23,7 @@ enum mxc_gpio_direction {
 #define GPIO_PER_BANK                  32
 
 struct mxc_gpio_plat {
+       int bank_index;
        struct gpio_regs *regs;
 };
 
@@ -31,24 +32,28 @@ struct mxc_bank_info {
 };
 
 #ifndef CONFIG_DM_GPIO
-#define GPIO_TO_PORT(n)                (n / 32)
+#define GPIO_TO_PORT(n)                ((n) / 32)
 
 /* GPIO port description */
 static unsigned long gpio_ports[] = {
        [0] = GPIO1_BASE_ADDR,
        [1] = GPIO2_BASE_ADDR,
        [2] = GPIO3_BASE_ADDR,
-#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
-               defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
+               defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
        [3] = GPIO4_BASE_ADDR,
 #endif
-#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
        [4] = GPIO5_BASE_ADDR,
+#ifndef CONFIG_SOX_MX6UL
        [5] = GPIO6_BASE_ADDR,
 #endif
-#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#endif
+#if defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
+#if !(defined(CONFIG_SOC_MX6UL) || defined(CONFIG_SOC_MX6ULL))
        [6] = GPIO7_BASE_ADDR,
 #endif
+#endif
 };
 
 static int mxc_gpio_direction(unsigned int gpio,
@@ -58,8 +63,10 @@ static int mxc_gpio_direction(unsigned int gpio,
        struct gpio_regs *regs;
        u32 l;
 
-       if (port >= ARRAY_SIZE(gpio_ports))
+       if (port >= ARRAY_SIZE(gpio_ports)) {
+               printf("%s: Invalid GPIO %d\n", __func__, gpio);
                return -1;
+       }
 
        gpio &= 0x1f;
 
@@ -85,8 +92,10 @@ int gpio_set_value(unsigned gpio, int value)
        struct gpio_regs *regs;
        u32 l;
 
-       if (port >= ARRAY_SIZE(gpio_ports))
+       if (port >= ARRAY_SIZE(gpio_ports)) {
+               printf("%s: Invalid GPIO %d\n", __func__, gpio);
                return -1;
+       }
 
        gpio &= 0x1f;
 
@@ -108,28 +117,42 @@ int gpio_get_value(unsigned gpio)
        struct gpio_regs *regs;
        u32 val;
 
-       if (port >= ARRAY_SIZE(gpio_ports))
+       if (port >= ARRAY_SIZE(gpio_ports)) {
+               printf("%s: Invalid GPIO %d\n", __func__, gpio);
                return -1;
+       }
 
        gpio &= 0x1f;
 
        regs = (struct gpio_regs *)gpio_ports[port];
 
-       val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
-
+       if (readl(&regs->gpio_dir) & (1 << gpio)) {
+               printf("WARNING: Reading status of output GPIO_%d_%d\n",
+                       port - GPIO_TO_PORT(0), gpio);
+               val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
+       } else {
+               val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
+       }
        return val;
 }
 
 int gpio_request(unsigned gpio, const char *label)
 {
        unsigned int port = GPIO_TO_PORT(gpio);
-       if (port >= ARRAY_SIZE(gpio_ports))
+       if (port >= ARRAY_SIZE(gpio_ports)) {
+               printf("%s: Invalid GPIO %d\n", __func__, gpio);
                return -1;
+       }
        return 0;
 }
 
 int gpio_free(unsigned gpio)
 {
+       unsigned int port = GPIO_TO_PORT(gpio);
+       if (port >= ARRAY_SIZE(gpio_ports)) {
+               printf("%s: Invalid GPIO %d\n", __func__, gpio);
+               return -1;
+       }
        return 0;
 }
 
@@ -150,6 +173,9 @@ int gpio_direction_output(unsigned gpio, int value)
 #endif
 
 #ifdef CONFIG_DM_GPIO
+#include <fdtdec.h>
+DECLARE_GLOBAL_DATA_PTR;
+
 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
 {
        u32 val;
@@ -258,32 +284,15 @@ static const struct dm_gpio_ops gpio_mxc_ops = {
        .get_function           = mxc_gpio_get_function,
 };
 
-static const struct mxc_gpio_plat mxc_plat[] = {
-       { (struct gpio_regs *)GPIO1_BASE_ADDR },
-       { (struct gpio_regs *)GPIO2_BASE_ADDR },
-       { (struct gpio_regs *)GPIO3_BASE_ADDR },
-#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
-               defined(CONFIG_MX53) || defined(CONFIG_MX6)
-       { (struct gpio_regs *)GPIO4_BASE_ADDR },
-#endif
-#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
-       { (struct gpio_regs *)GPIO5_BASE_ADDR },
-       { (struct gpio_regs *)GPIO6_BASE_ADDR },
-#endif
-#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
-       { (struct gpio_regs *)GPIO7_BASE_ADDR },
-#endif
-};
-
 static int mxc_gpio_probe(struct udevice *dev)
 {
        struct mxc_bank_info *bank = dev_get_priv(dev);
        struct mxc_gpio_plat *plat = dev_get_platdata(dev);
-       struct gpio_dev_priv *uc_priv = dev->uclass_priv;
+       struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
        int banknum;
        char name[18], *str;
 
-       banknum = plat - mxc_plat;
+       banknum = plat->bank_index;
        sprintf(name, "GPIO%d_", banknum + 1);
        str = strdup(name);
        if (!str)
@@ -295,28 +304,89 @@ static int mxc_gpio_probe(struct udevice *dev)
        return 0;
 }
 
+static int mxc_gpio_bind(struct udevice *dev)
+{
+       struct mxc_gpio_plat *plat = dev->platdata;
+       fdt_addr_t addr;
+
+       /*
+        * If platdata already exsits, directly return.
+        * Actually only when DT is not supported, platdata
+        * is statically initialized in U_BOOT_DEVICES.Here
+        * will return.
+        */
+       if (plat)
+               return 0;
+
+       addr = dev_get_addr(dev);
+       if (addr == FDT_ADDR_T_NONE)
+               return -ENODEV;
+
+       /*
+        * TODO:
+        * When every board is converted to driver model and DT is supported,
+        * this can be done by auto-alloc feature, but not using calloc
+        * to alloc memory for platdata.
+        */
+       plat = calloc(1, sizeof(*plat));
+       if (!plat)
+               return -ENOMEM;
+
+       plat->regs = (struct gpio_regs *)addr;
+       plat->bank_index = dev->req_seq;
+       dev->platdata = plat;
+
+       return 0;
+}
+
+static const struct udevice_id mxc_gpio_ids[] = {
+       { .compatible = "fsl,imx35-gpio" },
+       { }
+};
+
 U_BOOT_DRIVER(gpio_mxc) = {
        .name   = "gpio_mxc",
        .id     = UCLASS_GPIO,
        .ops    = &gpio_mxc_ops,
        .probe  = mxc_gpio_probe,
        .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
+       .of_match = mxc_gpio_ids,
+       .bind   = mxc_gpio_bind,
+};
+
+#if !CONFIG_IS_ENABLED(OF_CONTROL)
+static const struct mxc_gpio_plat mxc_plat[] = {
+       { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
+       { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
+       { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
+#if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
+               defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
+       { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
+#endif
+#if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
+       { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
+       { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
+#endif
+#if defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
+       { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
+#endif
 };
 
 U_BOOT_DEVICES(mxc_gpios) = {
        { "gpio_mxc", &mxc_plat[0] },
        { "gpio_mxc", &mxc_plat[1] },
        { "gpio_mxc", &mxc_plat[2] },
-#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
-               defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
+               defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
        { "gpio_mxc", &mxc_plat[3] },
 #endif
-#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
        { "gpio_mxc", &mxc_plat[4] },
        { "gpio_mxc", &mxc_plat[5] },
 #endif
-#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
+#if defined(CONFIG_SOC_MX53) || defined(CONFIG_ARCH_MX6)
        { "gpio_mxc", &mxc_plat[6] },
 #endif
 };
 #endif
+#endif