]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/gpio/mxs_gpio.c
Merge branch 'tx6-bugfix'
[karo-tx-uboot.git] / drivers / gpio / mxs_gpio.c
1 /*
2  * Freescale i.MX28 GPIO control code
3  *
4  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5  * on behalf of DENX Software Engineering GmbH
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <netdev.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/gpio.h>
15 #include <asm/arch/iomux.h>
16 #include <asm/arch/imx-regs.h>
17
18 #if     defined(CONFIG_SOC_MX23)
19 #define PINCTRL_BANKS           3
20 #define PINCTRL_DOUT(n)         (0x0500 + ((n) * 0x10))
21 #define PINCTRL_DIN(n)          (0x0600 + ((n) * 0x10))
22 #define PINCTRL_DOE(n)          (0x0700 + ((n) * 0x10))
23 #define PINCTRL_PIN2IRQ(n)      (0x0800 + ((n) * 0x10))
24 #define PINCTRL_IRQEN(n)        (0x0900 + ((n) * 0x10))
25 #define PINCTRL_IRQSTAT(n)      (0x0c00 + ((n) * 0x10))
26 #elif   defined(CONFIG_SOC_MX28)
27 #define PINCTRL_BANKS           5
28 #define PINCTRL_DOUT(n)         (0x0700 + ((n) * 0x10))
29 #define PINCTRL_DIN(n)          (0x0900 + ((n) * 0x10))
30 #define PINCTRL_DOE(n)          (0x0b00 + ((n) * 0x10))
31 #define PINCTRL_PIN2IRQ(n)      (0x1000 + ((n) * 0x10))
32 #define PINCTRL_IRQEN(n)        (0x1100 + ((n) * 0x10))
33 #define PINCTRL_IRQSTAT(n)      (0x1400 + ((n) * 0x10))
34 #else
35 #error "Please select CONFIG_SOC_MX23 or CONFIG_SOC_MX28"
36 #endif
37
38 void mxs_gpio_init(void)
39 {
40         int i;
41
42         for (i = 0; i < PINCTRL_BANKS; i++) {
43                 writel(0, MXS_PINCTRL_BASE + PINCTRL_PIN2IRQ(i));
44                 writel(0, MXS_PINCTRL_BASE + PINCTRL_IRQEN(i));
45                 /* Use SCT address here to clear the IRQSTAT bits */
46                 writel(0xffffffff, MXS_PINCTRL_BASE + PINCTRL_IRQSTAT(i) + 8);
47         }
48 }
49
50 int gpio_get_value(unsigned gpio)
51 {
52         uint32_t bank = MXS_GPIO_TO_BANK(gpio);
53         uint32_t offset = PINCTRL_DIN(bank);
54         struct mxs_register_32 *reg = (void *)(MXS_PINCTRL_BASE + offset);
55
56         if (bank >= PINCTRL_BANKS)
57                 return -EINVAL;
58
59         return (readl(&reg->reg) >> MXS_GPIO_TO_PIN(gpio)) & 1;
60 }
61
62 int gpio_set_value(unsigned gpio, int value)
63 {
64         uint32_t bank = MXS_GPIO_TO_BANK(gpio);
65         uint32_t offset = PINCTRL_DOUT(bank);
66         struct mxs_register_32 *reg = (void *)(MXS_PINCTRL_BASE + offset);
67
68         if (bank >= PINCTRL_BANKS)
69                 return -EINVAL;
70
71         if (value)
72                 writel(1 << MXS_GPIO_TO_PIN(gpio), &reg->reg_set);
73         else
74                 writel(1 << MXS_GPIO_TO_PIN(gpio), &reg->reg_clr);
75
76         return 0;
77 }
78
79 int gpio_direction_input(unsigned gpio)
80 {
81         uint32_t bank = MXS_GPIO_TO_BANK(gpio);
82         uint32_t offset = PINCTRL_DOE(bank);
83         struct mxs_register_32 *reg = (void *)(MXS_PINCTRL_BASE + offset);
84
85         if (bank >= PINCTRL_BANKS)
86                 return -EINVAL;
87
88         writel(1 << MXS_GPIO_TO_PIN(gpio), &reg->reg_clr);
89
90         return 0;
91 }
92
93 int gpio_direction_output(unsigned gpio, int value)
94 {
95         uint32_t bank = MXS_GPIO_TO_BANK(gpio);
96         uint32_t offset = PINCTRL_DOE(bank);
97         struct mxs_register_32 *reg = (void *)(MXS_PINCTRL_BASE + offset);
98
99         if (bank >= PINCTRL_BANKS)
100                 return -EINVAL;
101
102         gpio_set_value(gpio, value);
103
104         writel(1 << MXS_GPIO_TO_PIN(gpio), &reg->reg_set);
105
106         return 0;
107 }
108
109 int gpio_request(unsigned gpio, const char *label)
110 {
111         if (MXS_GPIO_TO_BANK(gpio) >= PINCTRL_BANKS) {
112                 printf("%s(): Invalid GPIO%d (GPIO_%u_%u) requested; possibly intended: GPIO_%u_%u\n",
113                         __func__, gpio, gpio / 32, gpio % 32,
114                         PAD_BANK(gpio), PAD_PIN(gpio));
115                 printf("Linear GPIO number required rather than iomux_cfg_t cookie!\n");
116                 printf("Possibly missing MXS_PAD_TO_GPIO() in the GPIO specification.\n");
117                 return -EINVAL;
118         }
119
120         return 0;
121 }
122
123 int gpio_free(unsigned gpio)
124 {
125         return 0;
126 }