blob: 1d820d4c3ef63201be241c2c4c0e634b3d8c332c [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 *
Guennadi Liakhovetski7c6a8562009-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 Babicd77fe992010-07-06 17:05:06 +020027#include <asm/arch/imx-regs.h>
Stefano Babic7faee912011-08-21 10:45:44 +020028#include <asm/gpio.h>
Stefano Babicd77fe992010-07-06 17:05:06 +020029#include <asm/io.h>
Fabio Estevam893eafb2011-04-09 10:43:24 +000030#include <errno.h>
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010031
Stefano Babic7faee912011-08-21 10:45:44 +020032enum mxc_gpio_direction {
33 MXC_GPIO_DIRECTION_IN,
34 MXC_GPIO_DIRECTION_OUT,
35};
36
Vikram Narayanan10999cf2012-04-10 04:26:08 +000037#define GPIO_TO_PORT(n) (n / 32)
Stefano Babic7faee912011-08-21 10:45:44 +020038
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010039/* GPIO port description */
40static unsigned long gpio_ports[] = {
Stefano Babicd77fe992010-07-06 17:05:06 +020041 [0] = GPIO1_BASE_ADDR,
42 [1] = GPIO2_BASE_ADDR,
43 [2] = GPIO3_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000044#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000045 defined(CONFIG_MX53) || defined(CONFIG_MX6)
Stefano Babicd77fe992010-07-06 17:05:06 +020046 [3] = GPIO4_BASE_ADDR,
47#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000048#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000049 [4] = GPIO5_BASE_ADDR,
50 [5] = GPIO6_BASE_ADDR,
tremcf233ed2012-08-25 05:30:33 +000051#endif
Troy Kiskyd8c2fd02012-10-23 10:57:47 +000052#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R64343a71164c2011-01-03 22:27:38 +000053 [6] = GPIO7_BASE_ADDR,
54#endif
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010055};
56
Stefano Babic7faee912011-08-21 10:45:44 +020057static int mxc_gpio_direction(unsigned int gpio,
58 enum mxc_gpio_direction direction)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010059{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000060 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020061 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010062 u32 l;
63
64 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060065 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010066
67 gpio &= 0x1f;
68
Stefano Babicd77fe992010-07-06 17:05:06 +020069 regs = (struct gpio_regs *)gpio_ports[port];
70
71 l = readl(&regs->gpio_dir);
72
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010073 switch (direction) {
Stefano Babicd77fe992010-07-06 17:05:06 +020074 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010075 l |= 1 << gpio;
76 break;
Stefano Babicd77fe992010-07-06 17:05:06 +020077 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010078 l &= ~(1 << gpio);
79 }
Stefano Babicd77fe992010-07-06 17:05:06 +020080 writel(l, &regs->gpio_dir);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010081
82 return 0;
83}
84
Joe Hershbergerf8928f12011-11-11 15:55:36 -060085int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010086{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +000087 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +020088 struct gpio_regs *regs;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010089 u32 l;
90
91 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -060092 return -1;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010093
94 gpio &= 0x1f;
95
Stefano Babicd77fe992010-07-06 17:05:06 +020096 regs = (struct gpio_regs *)gpio_ports[port];
97
98 l = readl(&regs->gpio_dr);
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +010099 if (value)
100 l |= 1 << gpio;
101 else
102 l &= ~(1 << gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +0200103 writel(l, &regs->gpio_dr);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600104
105 return 0;
Guennadi Liakhovetski7c6a8562009-02-07 01:18:07 +0100106}
Stefano Babica44d2a52010-04-13 12:07:00 +0200107
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600108int gpio_get_value(unsigned gpio)
Stefano Babica44d2a52010-04-13 12:07:00 +0200109{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +0000110 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd77fe992010-07-06 17:05:06 +0200111 struct gpio_regs *regs;
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600112 u32 val;
Stefano Babica44d2a52010-04-13 12:07:00 +0200113
114 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600115 return -1;
Stefano Babica44d2a52010-04-13 12:07:00 +0200116
117 gpio &= 0x1f;
118
Stefano Babicd77fe992010-07-06 17:05:06 +0200119 regs = (struct gpio_regs *)gpio_ports[port];
120
Benoît Thébaudeaudaaf7a92012-08-20 10:55:41 +0000121 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babica44d2a52010-04-13 12:07:00 +0200122
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600123 return val;
Stefano Babica44d2a52010-04-13 12:07:00 +0200124}
Stefano Babic7faee912011-08-21 10:45:44 +0200125
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600126int gpio_request(unsigned gpio, const char *label)
Stefano Babic7faee912011-08-21 10:45:44 +0200127{
Vikram Narayananfbdf6bc2012-04-10 04:26:20 +0000128 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babic7faee912011-08-21 10:45:44 +0200129 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600130 return -1;
Stefano Babic7faee912011-08-21 10:45:44 +0200131 return 0;
132}
133
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600134int gpio_free(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200135{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600136 return 0;
Stefano Babic7faee912011-08-21 10:45:44 +0200137}
138
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600139int gpio_direction_input(unsigned gpio)
Stefano Babic7faee912011-08-21 10:45:44 +0200140{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600141 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babic7faee912011-08-21 10:45:44 +0200142}
143
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600144int gpio_direction_output(unsigned gpio, int value)
Stefano Babic7faee912011-08-21 10:45:44 +0200145{
Dirk Behme1e0803f2013-07-15 15:58:27 +0200146 int ret = gpio_set_value(gpio, value);
Stefano Babic7faee912011-08-21 10:45:44 +0200147
148 if (ret < 0)
149 return ret;
150
Dirk Behme1e0803f2013-07-15 15:58:27 +0200151 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babic7faee912011-08-21 10:45:44 +0200152}