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