blob: 8bb9e39b7231e522f87eaf9612ad5abf383931e7 [file] [log] [blame]
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +01001/*
2 * Copyright (C) 2009
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
Stefano Babic7faee912011-08-21 10:45:44 +02005 * Copyright (C) 2011
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +01009 */
10#include <common.h>
Simon Glass2772b4d2014-10-01 19:57:26 -060011#include <errno.h>
12#include <dm.h>
13#include <malloc.h>
Stefano Babicd77fe992010-07-06 17:05:06 +020014#include <asm/arch/imx-regs.h>
Stefano Babic7faee912011-08-21 10:45:44 +020015#include <asm/gpio.h>
Stefano Babicd77fe992010-07-06 17:05:06 +020016#include <asm/io.h>
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010017
Stefano Babic7faee912011-08-21 10:45:44 +020018enum mxc_gpio_direction {
19 MXC_GPIO_DIRECTION_IN,
20 MXC_GPIO_DIRECTION_OUT,
21};
22
Simon Glass2772b4d2014-10-01 19:57:26 -060023#define GPIO_PER_BANK 32
24
25struct mxc_gpio_plat {
26 struct gpio_regs *regs;
27};
28
29struct mxc_bank_info {
Simon Glass2772b4d2014-10-01 19:57:26 -060030 struct gpio_regs *regs;
31};
32
33#ifndef CONFIG_DM_GPIO
Vikram Narayanan10999cf2012-04-10 04:26:08 +000034#define GPIO_TO_PORT(n) (n / 32)
Stefano Babic7faee912011-08-21 10:45:44 +020035
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010036/* GPIO port description */
37static unsigned long gpio_ports[] = {
Stefano Babicd77fe992010-07-06 17:05:06 +020038 [0] = GPIO1_BASE_ADDR,
39 [1] = GPIO2_BASE_ADDR,
40 [2] = GPIO3_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000041#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000042 defined(CONFIG_MX53) || defined(CONFIG_MX6)
Stefano Babicd77fe992010-07-06 17:05:06 +020043 [3] = GPIO4_BASE_ADDR,
44#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000045#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000046 [4] = GPIO5_BASE_ADDR,
47 [5] = GPIO6_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000048#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000049#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000050 [6] = GPIO7_BASE_ADDR,
51#endif
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010052};
53
Stefano Babic7faee912011-08-21 10:45:44 +020054static int mxc_gpio_direction(unsigned int gpio,
55 enum mxc_gpio_direction direction)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010056{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000057 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020058 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010059 u32 l;
60
61 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060062 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010063
64 gpio &= 0x1f;
65
Stefano Babicd77fe992010-07-06 17:05:06 +020066 regs = (struct gpio_regs *)gpio_ports[port];
67
68 l = readl(&regs->gpio_dir);
69
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010070 switch (direction) {
Stefano Babicd77fe992010-07-06 17:05:06 +020071 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010072 l |= 1 << gpio;
73 break;
Stefano Babicd77fe992010-07-06 17:05:06 +020074 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010075 l &= ~(1 << gpio);
76 }
Stefano Babicd77fe992010-07-06 17:05:06 +020077 writel(l, &regs->gpio_dir);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010078
79 return 0;
80}
81
Joe Hershbergerf8928f12011-11-11 15:55:36 -060082int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010083{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000084 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020085 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010086 u32 l;
87
88 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060089 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010090
91 gpio &= 0x1f;
92
Stefano Babicd77fe992010-07-06 17:05:06 +020093 regs = (struct gpio_regs *)gpio_ports[port];
94
95 l = readl(&regs->gpio_dr);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010096 if (value)
97 l |= 1 << gpio;
98 else
99 l &= ~(1 << gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +0200100 writel(l, &regs->gpio_dr);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600101
102 return 0;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +0100103}
Stefano Babica44d2a52010-04-13 12:07:00 +0200104
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600105int gpio_get_value(unsigned gpio)
Stefano Babica44d2a52010-04-13 12:07:00 +0200106{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +0000107 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +0200108 struct gpio_regs *regs;
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600109 u32 val;
Stefano Babica44d2a52010-04-13 12:07:00 +0200110
111 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600112 return -1;
Stefano Babica44d2a52010-04-13 12:07:00 +0200113
114 gpio &= 0x1f;
115
Stefano Babicd77fe992010-07-06 17:05:06 +0200116 regs = (struct gpio_regs *)gpio_ports[port];
117
Benoît Thébaudeaudaaf7a92012-08-20 10:55:41 +0000118 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babica44d2a52010-04-13 12:07:00 +0200119
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600120 return val;
Stefano Babica44d2a52010-04-13 12:07:00 +0200121}
Stefano Babic7faee912011-08-21 10:45:44 +0200122
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600123int gpio_request(unsigned gpio, const char *label)
Stefano Babic7faee912011-08-21 10:45:44 +0200124{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +0000125 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babic7faee912011-08-21 10:45:44 +0200126 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600127 return -1;
Stefano Babic7faee912011-08-21 10:45:44 +0200128 return 0;
129}
130
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600131int gpio_free(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200132{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600133 return 0;
Stefano Babic7faee912011-08-21 10:45:44 +0200134}
135
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600136int gpio_direction_input(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200137{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600138 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babic7faee912011-08-21 10:45:44 +0200139}
140
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600141int gpio_direction_output(unsigned gpio, int value)
Stefano Babic7faee912011-08-21 10:45:44 +0200142{
Dirk Behme1e0803f2013-07-15 15:58:27 +0200143 int ret = gpio_set_value(gpio, value);
Stefano Babic7faee912011-08-21 10:45:44 +0200144
145 if (ret < 0)
146 return ret;
147
Dirk Behme1e0803f2013-07-15 15:58:27 +0200148 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babic7faee912011-08-21 10:45:44 +0200149}
Simon Glass2772b4d2014-10-01 19:57:26 -0600150#endif
151
152#ifdef CONFIG_DM_GPIO
Simon Glass2772b4d2014-10-01 19:57:26 -0600153static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
154{
155 u32 val;
156
157 val = readl(&regs->gpio_dir);
158
159 return val & (1 << offset) ? 1 : 0;
160}
161
162static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
163 enum mxc_gpio_direction direction)
164{
165 u32 l;
166
167 l = readl(&regs->gpio_dir);
168
169 switch (direction) {
170 case MXC_GPIO_DIRECTION_OUT:
171 l |= 1 << offset;
172 break;
173 case MXC_GPIO_DIRECTION_IN:
174 l &= ~(1 << offset);
175 }
176 writel(l, &regs->gpio_dir);
177}
178
179static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
180 int value)
181{
182 u32 l;
183
184 l = readl(&regs->gpio_dr);
185 if (value)
186 l |= 1 << offset;
187 else
188 l &= ~(1 << offset);
189 writel(l, &regs->gpio_dr);
190}
191
192static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
193{
194 return (readl(&regs->gpio_psr) >> offset) & 0x01;
195}
196
Simon Glass2772b4d2014-10-01 19:57:26 -0600197/* set GPIO pin 'gpio' as an input */
198static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
199{
200 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass2772b4d2014-10-01 19:57:26 -0600201
202 /* Configure GPIO direction as input. */
203 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
204
205 return 0;
206}
207
208/* set GPIO pin 'gpio' as an output, with polarity 'value' */
209static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
210 int value)
211{
212 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass2772b4d2014-10-01 19:57:26 -0600213
214 /* Configure GPIO output value. */
215 mxc_gpio_bank_set_value(bank->regs, offset, value);
216
217 /* Configure GPIO direction as output. */
218 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
219
220 return 0;
221}
222
223/* read GPIO IN value of pin 'gpio' */
224static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
225{
226 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass2772b4d2014-10-01 19:57:26 -0600227
228 return mxc_gpio_bank_get_value(bank->regs, offset);
229}
230
231/* write GPIO OUT value to pin 'gpio' */
232static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
233 int value)
234{
235 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass2772b4d2014-10-01 19:57:26 -0600236
237 mxc_gpio_bank_set_value(bank->regs, offset, value);
238
239 return 0;
240}
241
Simon Glass2772b4d2014-10-01 19:57:26 -0600242static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
243{
244 struct mxc_bank_info *bank = dev_get_priv(dev);
245
Simon Glass2772b4d2014-10-01 19:57:26 -0600246 /* GPIOF_FUNC is not implemented yet */
247 if (mxc_gpio_is_output(bank->regs, offset))
248 return GPIOF_OUTPUT;
249 else
250 return GPIOF_INPUT;
251}
252
253static const struct dm_gpio_ops gpio_mxc_ops = {
Simon Glass2772b4d2014-10-01 19:57:26 -0600254 .direction_input = mxc_gpio_direction_input,
255 .direction_output = mxc_gpio_direction_output,
256 .get_value = mxc_gpio_get_value,
257 .set_value = mxc_gpio_set_value,
258 .get_function = mxc_gpio_get_function,
Simon Glass2772b4d2014-10-01 19:57:26 -0600259};
260
261static const struct mxc_gpio_plat mxc_plat[] = {
262 { (struct gpio_regs *)GPIO1_BASE_ADDR },
263 { (struct gpio_regs *)GPIO2_BASE_ADDR },
264 { (struct gpio_regs *)GPIO3_BASE_ADDR },
265#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
266 defined(CONFIG_MX53) || defined(CONFIG_MX6)
267 { (struct gpio_regs *)GPIO4_BASE_ADDR },
268#endif
269#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
270 { (struct gpio_regs *)GPIO5_BASE_ADDR },
271 { (struct gpio_regs *)GPIO6_BASE_ADDR },
272#endif
273#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
274 { (struct gpio_regs *)GPIO7_BASE_ADDR },
275#endif
276};
277
278static int mxc_gpio_probe(struct udevice *dev)
279{
280 struct mxc_bank_info *bank = dev_get_priv(dev);
281 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
282 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
283 int banknum;
284 char name[18], *str;
285
286 banknum = plat - mxc_plat;
287 sprintf(name, "GPIO%d_", banknum + 1);
288 str = strdup(name);
289 if (!str)
290 return -ENOMEM;
291 uc_priv->bank_name = str;
292 uc_priv->gpio_count = GPIO_PER_BANK;
293 bank->regs = plat->regs;
294
295 return 0;
296}
297
298U_BOOT_DRIVER(gpio_mxc) = {
299 .name = "gpio_mxc",
300 .id = UCLASS_GPIO,
301 .ops = &gpio_mxc_ops,
302 .probe = mxc_gpio_probe,
303 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
304};
305
306U_BOOT_DEVICES(mxc_gpios) = {
307 { "gpio_mxc", &mxc_plat[0] },
308 { "gpio_mxc", &mxc_plat[1] },
309 { "gpio_mxc", &mxc_plat[2] },
310#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
311 defined(CONFIG_MX53) || defined(CONFIG_MX6)
312 { "gpio_mxc", &mxc_plat[3] },
313#endif
314#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
315 { "gpio_mxc", &mxc_plat[4] },
316 { "gpio_mxc", &mxc_plat[5] },
317#endif
318#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
319 { "gpio_mxc", &mxc_plat[6] },
320#endif
321};
322#endif