blob: 46c0255adc35e43fc6367f2c54ca03cf95203410 [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>
Stefano Babicd77fe992010-07-06 17:05:06 +020011#include <asm/arch/imx-regs.h>
Stefano Babic7faee912011-08-21 10:45:44 +020012#include <asm/gpio.h>
Stefano Babicd77fe992010-07-06 17:05:06 +020013#include <asm/io.h>
Fabio Estevam893eafb2011-04-09 10:43:24 +000014#include <errno.h>
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010015
Stefano Babic7faee912011-08-21 10:45:44 +020016enum mxc_gpio_direction {
17 MXC_GPIO_DIRECTION_IN,
18 MXC_GPIO_DIRECTION_OUT,
19};
20
Vikram Narayanan10999cf2012-04-10 04:26:08 +000021#define GPIO_TO_PORT(n) (n / 32)
Stefano Babic7faee912011-08-21 10:45:44 +020022
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010023/* GPIO port description */
24static unsigned long gpio_ports[] = {
Stefano Babicd77fe992010-07-06 17:05:06 +020025 [0] = GPIO1_BASE_ADDR,
26 [1] = GPIO2_BASE_ADDR,
27 [2] = GPIO3_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000028#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000029 defined(CONFIG_MX53) || defined(CONFIG_MX6)
Stefano Babicd77fe992010-07-06 17:05:06 +020030 [3] = GPIO4_BASE_ADDR,
31#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000032#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000033 [4] = GPIO5_BASE_ADDR,
34 [5] = GPIO6_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000035#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000036#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000037 [6] = GPIO7_BASE_ADDR,
38#endif
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010039};
40
Stefano Babic7faee912011-08-21 10:45:44 +020041static int mxc_gpio_direction(unsigned int gpio,
42 enum mxc_gpio_direction direction)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010043{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000044 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020045 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010046 u32 l;
47
48 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060049 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010050
51 gpio &= 0x1f;
52
Stefano Babicd77fe992010-07-06 17:05:06 +020053 regs = (struct gpio_regs *)gpio_ports[port];
54
55 l = readl(&regs->gpio_dir);
56
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010057 switch (direction) {
Stefano Babicd77fe992010-07-06 17:05:06 +020058 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010059 l |= 1 << gpio;
60 break;
Stefano Babicd77fe992010-07-06 17:05:06 +020061 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010062 l &= ~(1 << gpio);
63 }
Stefano Babicd77fe992010-07-06 17:05:06 +020064 writel(l, &regs->gpio_dir);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010065
66 return 0;
67}
68
Joe Hershbergerf8928f12011-11-11 15:55:36 -060069int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010070{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000071 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020072 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010073 u32 l;
74
75 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060076 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010077
78 gpio &= 0x1f;
79
Stefano Babicd77fe992010-07-06 17:05:06 +020080 regs = (struct gpio_regs *)gpio_ports[port];
81
82 l = readl(&regs->gpio_dr);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010083 if (value)
84 l |= 1 << gpio;
85 else
86 l &= ~(1 << gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020087 writel(l, &regs->gpio_dr);
Joe Hershbergerf8928f12011-11-11 15:55:36 -060088
89 return 0;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010090}
Stefano Babica44d2a52010-04-13 12:07:00 +020091
Joe Hershbergerf8928f12011-11-11 15:55:36 -060092int gpio_get_value(unsigned gpio)
Stefano Babica44d2a52010-04-13 12:07:00 +020093{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000094 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020095 struct gpio_regs *regs;
Joe Hershbergerf8928f12011-11-11 15:55:36 -060096 u32 val;
Stefano Babica44d2a52010-04-13 12:07:00 +020097
98 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060099 return -1;
Stefano Babica44d2a52010-04-13 12:07:00 +0200100
101 gpio &= 0x1f;
102
Stefano Babicd77fe992010-07-06 17:05:06 +0200103 regs = (struct gpio_regs *)gpio_ports[port];
104
Benoît Thébaudeaudaaf7a92012-08-20 10:55:41 +0000105 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babica44d2a52010-04-13 12:07:00 +0200106
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600107 return val;
Stefano Babica44d2a52010-04-13 12:07:00 +0200108}
Stefano Babic7faee912011-08-21 10:45:44 +0200109
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600110int gpio_request(unsigned gpio, const char *label)
Stefano Babic7faee912011-08-21 10:45:44 +0200111{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +0000112 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babic7faee912011-08-21 10:45:44 +0200113 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600114 return -1;
Stefano Babic7faee912011-08-21 10:45:44 +0200115 return 0;
116}
117
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600118int gpio_free(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200119{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600120 return 0;
Stefano Babic7faee912011-08-21 10:45:44 +0200121}
122
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600123int gpio_direction_input(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200124{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600125 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babic7faee912011-08-21 10:45:44 +0200126}
127
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600128int gpio_direction_output(unsigned gpio, int value)
Stefano Babic7faee912011-08-21 10:45:44 +0200129{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600130 int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babic7faee912011-08-21 10:45:44 +0200131
132 if (ret < 0)
133 return ret;
134
Vikram Narayananc0a17fe2012-04-10 04:26:30 +0000135 return gpio_set_value(gpio, value);
Stefano Babic7faee912011-08-21 10:45:44 +0200136}