]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/gpio/mxc_gpio.c
TX6 Release 2013-04-22
[karo-tx-uboot.git] / drivers / gpio / mxc_gpio.c
1 /*
2  * Copyright (C) 2009
3  * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4  *
5  * Copyright (C) 2011
6  * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26 #include <common.h>
27 #include <asm/arch/imx-regs.h>
28 #include <asm/gpio.h>
29 #include <asm/io.h>
30 #include <errno.h>
31
32 enum mxc_gpio_direction {
33         MXC_GPIO_DIRECTION_IN,
34         MXC_GPIO_DIRECTION_OUT,
35 };
36
37 #define GPIO_TO_PORT(n)         ((n) / 32)
38
39 /* GPIO port description */
40 static unsigned long gpio_ports[] = {
41         [0] = GPIO1_BASE_ADDR,
42         [1] = GPIO2_BASE_ADDR,
43         [2] = GPIO3_BASE_ADDR,
44 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
45                 defined(CONFIG_MX53) || defined(CONFIG_MX6)
46         [3] = GPIO4_BASE_ADDR,
47 #endif
48 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
49         [4] = GPIO5_BASE_ADDR,
50         [5] = GPIO6_BASE_ADDR,
51 #endif
52 #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
53         [6] = GPIO7_BASE_ADDR,
54 #endif
55 };
56
57 static int mxc_gpio_direction(unsigned int gpio,
58         enum mxc_gpio_direction direction)
59 {
60         unsigned int port = GPIO_TO_PORT(gpio);
61         struct gpio_regs *regs;
62         u32 l;
63
64         if (port >= ARRAY_SIZE(gpio_ports)) {
65                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
66                 return -1;
67         }
68
69         gpio &= 0x1f;
70
71         regs = (struct gpio_regs *)gpio_ports[port];
72
73         l = readl(&regs->gpio_dir);
74
75         switch (direction) {
76         case MXC_GPIO_DIRECTION_OUT:
77                 l |= 1 << gpio;
78                 break;
79         case MXC_GPIO_DIRECTION_IN:
80                 l &= ~(1 << gpio);
81         }
82         writel(l, &regs->gpio_dir);
83
84         return 0;
85 }
86
87 int gpio_set_value(unsigned gpio, int value)
88 {
89         unsigned int port = GPIO_TO_PORT(gpio);
90         struct gpio_regs *regs;
91         u32 l;
92
93         if (port >= ARRAY_SIZE(gpio_ports)) {
94                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
95                 return -1;
96         }
97
98         gpio &= 0x1f;
99
100         regs = (struct gpio_regs *)gpio_ports[port];
101
102         l = readl(&regs->gpio_dr);
103         if (value)
104                 l |= 1 << gpio;
105         else
106                 l &= ~(1 << gpio);
107         writel(l, &regs->gpio_dr);
108
109         return 0;
110 }
111
112 int gpio_get_value(unsigned gpio)
113 {
114         unsigned int port = GPIO_TO_PORT(gpio);
115         struct gpio_regs *regs;
116         u32 val;
117
118         if (port >= ARRAY_SIZE(gpio_ports)) {
119                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
120                 return -1;
121         }
122
123         gpio &= 0x1f;
124
125         regs = (struct gpio_regs *)gpio_ports[port];
126
127         if (readl(&regs->gpio_dir) & (1 << gpio)) {
128                 printf("WARNING: Reading status of output GPIO_%d_%d\n",
129                         port - GPIO_TO_PORT(0), gpio);
130                 val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
131         } else {
132                 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
133         }
134         return val;
135 }
136
137 int gpio_request(unsigned gpio, const char *label)
138 {
139         unsigned int port = GPIO_TO_PORT(gpio);
140         if (port >= ARRAY_SIZE(gpio_ports)) {
141                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
142                 return -1;
143         }
144         return 0;
145 }
146
147 int gpio_free(unsigned gpio)
148 {
149         unsigned int port = GPIO_TO_PORT(gpio);
150         if (port >= ARRAY_SIZE(gpio_ports)) {
151                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
152                 return -1;
153         }
154         return 0;
155 }
156
157 int gpio_direction_input(unsigned gpio)
158 {
159         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
160 }
161
162 int gpio_direction_output(unsigned gpio, int value)
163 {
164         int ret = gpio_set_value(gpio, value);
165
166         if (ret < 0)
167                 return ret;
168
169         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
170 }