blob: 7e65b39a7581df0928fb9168e6bf09c13cb93b93 [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
Vikram Narayanan8d28c212012-04-10 04:26:08 +000037#define GPIO_TO_PORT(n) (n / 32)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020038
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010039/* GPIO port description */
40static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020041 [0] = GPIO1_BASE_ADDR,
42 [1] = GPIO2_BASE_ADDR,
43 [2] = GPIO3_BASE_ADDR,
Jason Liuc8bfe242011-11-25 00:18:03 +000044#if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
Stefano Babicc4ea1422010-07-06 17:05:06 +020045 [3] = GPIO4_BASE_ADDR,
46#endif
Jason Liuc8bfe242011-11-25 00:18:03 +000047#if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000048 [4] = GPIO5_BASE_ADDR,
49 [5] = GPIO6_BASE_ADDR,
50 [6] = GPIO7_BASE_ADDR,
51#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010052};
53
Stefano Babicd8e0ca82011-08-21 10:45:44 +020054static int mxc_gpio_direction(unsigned int gpio,
55 enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010056{
57 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020058 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010059 u32 l;
60
61 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060062 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010063
64 gpio &= 0x1f;
65
Stefano Babicc4ea1422010-07-06 17:05:06 +020066 regs = (struct gpio_regs *)gpio_ports[port];
67
68 l = readl(&regs->gpio_dir);
69
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010070 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020071 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010072 l |= 1 << gpio;
73 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020074 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010075 l &= ~(1 << gpio);
76 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020077 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010078
79 return 0;
80}
81
Joe Hershberger365d6072011-11-11 15:55:36 -060082int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010083{
84 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020085 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010086 u32 l;
87
88 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060089 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010090
91 gpio &= 0x1f;
92
Stefano Babicc4ea1422010-07-06 17:05:06 +020093 regs = (struct gpio_regs *)gpio_ports[port];
94
95 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010096 if (value)
97 l |= 1 << gpio;
98 else
99 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200100 writel(l, &regs->gpio_dr);
Joe Hershberger365d6072011-11-11 15:55:36 -0600101
102 return 0;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100103}
Stefano Babic7d27cd02010-04-13 12:07:00 +0200104
Joe Hershberger365d6072011-11-11 15:55:36 -0600105int gpio_get_value(unsigned gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +0200106{
107 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +0200108 struct gpio_regs *regs;
Joe Hershberger365d6072011-11-11 15:55:36 -0600109 u32 val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200110
111 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600112 return -1;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200113
114 gpio &= 0x1f;
115
Stefano Babicc4ea1422010-07-06 17:05:06 +0200116 regs = (struct gpio_regs *)gpio_ports[port];
117
Joe Hershberger365d6072011-11-11 15:55:36 -0600118 val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200119
Joe Hershberger365d6072011-11-11 15:55:36 -0600120 return val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200121}
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200122
Joe Hershberger365d6072011-11-11 15:55:36 -0600123int gpio_request(unsigned gpio, const char *label)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200124{
Joe Hershberger365d6072011-11-11 15:55:36 -0600125 unsigned int port = gpio >> 5;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200126 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600127 return -1;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200128 return 0;
129}
130
Joe Hershberger365d6072011-11-11 15:55:36 -0600131int gpio_free(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200132{
Joe Hershberger365d6072011-11-11 15:55:36 -0600133 return 0;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200134}
135
Joe Hershberger365d6072011-11-11 15:55:36 -0600136int gpio_direction_input(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200137{
Joe Hershberger365d6072011-11-11 15:55:36 -0600138 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200139}
140
Joe Hershberger365d6072011-11-11 15:55:36 -0600141int gpio_direction_output(unsigned gpio, int value)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200142{
Joe Hershberger365d6072011-11-11 15:55:36 -0600143 int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200144
145 if (ret < 0)
146 return ret;
147
Joe Hershberger365d6072011-11-11 15:55:36 -0600148 gpio_set_value(gpio, value);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200149 return 0;
150}