blob: 908808d50e1a706f0e8d94628181fe30ebf76843 [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 *
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +01008 * 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>
Stefano Babicc4ea1422010-07-06 17:05:06 +020027#include <asm/arch/imx-regs.h>
Stefano Babicd8e0ca82011-08-21 10:45:44 +020028#include <asm/gpio.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020029#include <asm/io.h>
Fabio Estevame53bcd92011-04-09 10:43:24 +000030#include <errno.h>
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010031
Stefano Babicd8e0ca82011-08-21 10:45:44 +020032enum mxc_gpio_direction {
33 MXC_GPIO_DIRECTION_IN,
34 MXC_GPIO_DIRECTION_OUT,
35};
36
37
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010038/* GPIO port description */
39static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020040 [0] = GPIO1_BASE_ADDR,
41 [1] = GPIO2_BASE_ADDR,
42 [2] = GPIO3_BASE_ADDR,
Jason Liuc8bfe242011-11-25 00:18:03 +000043#if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
Stefano Babicc4ea1422010-07-06 17:05:06 +020044 [3] = GPIO4_BASE_ADDR,
45#endif
Jason Liuc8bfe242011-11-25 00:18:03 +000046#if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000047 [4] = GPIO5_BASE_ADDR,
48 [5] = GPIO6_BASE_ADDR,
49 [6] = GPIO7_BASE_ADDR,
50#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010051};
52
Stefano Babicd8e0ca82011-08-21 10:45:44 +020053static int mxc_gpio_direction(unsigned int gpio,
54 enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010055{
56 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020057 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010058 u32 l;
59
60 if (port >= ARRAY_SIZE(gpio_ports))
Fabio Estevame53bcd92011-04-09 10:43:24 +000061 return -EINVAL;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010062
63 gpio &= 0x1f;
64
Stefano Babicc4ea1422010-07-06 17:05:06 +020065 regs = (struct gpio_regs *)gpio_ports[port];
66
67 l = readl(&regs->gpio_dir);
68
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010069 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020070 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010071 l |= 1 << gpio;
72 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020073 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010074 l &= ~(1 << gpio);
75 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020076 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010077
78 return 0;
79}
80
Stefano Babicd8e0ca82011-08-21 10:45:44 +020081void gpio_set_value(int gpio, int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010082{
83 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020084 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010085 u32 l;
86
87 if (port >= ARRAY_SIZE(gpio_ports))
88 return;
89
90 gpio &= 0x1f;
91
Stefano Babicc4ea1422010-07-06 17:05:06 +020092 regs = (struct gpio_regs *)gpio_ports[port];
93
94 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010095 if (value)
96 l |= 1 << gpio;
97 else
98 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020099 writel(l, &regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100100}
Stefano Babic7d27cd02010-04-13 12:07:00 +0200101
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200102int gpio_get_value(int gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +0200103{
104 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +0200105 struct gpio_regs *regs;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200106 u32 l;
107
108 if (port >= ARRAY_SIZE(gpio_ports))
Fabio Estevame53bcd92011-04-09 10:43:24 +0000109 return -EINVAL;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200110
111 gpio &= 0x1f;
112
Stefano Babicc4ea1422010-07-06 17:05:06 +0200113 regs = (struct gpio_regs *)gpio_ports[port];
114
115 l = (readl(&regs->gpio_dr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200116
117 return l;
118}
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200119
120int gpio_request(int gp, const char *label)
121{
122 unsigned int port = gp >> 5;
123 if (port >= ARRAY_SIZE(gpio_ports))
124 return -EINVAL;
125 return 0;
126}
127
128void gpio_free(int gp)
129{
130}
131
132void gpio_toggle_value(int gp)
133{
134 gpio_set_value(gp, !gpio_get_value(gp));
135}
136
137int gpio_direction_input(int gp)
138{
139 return mxc_gpio_direction(gp, MXC_GPIO_DIRECTION_IN);
140}
141
142int gpio_direction_output(int gp, int value)
143{
144 int ret = mxc_gpio_direction(gp, MXC_GPIO_DIRECTION_OUT);
145
146 if (ret < 0)
147 return ret;
148
149 gpio_set_value(gp, value);
150 return 0;
151}