blob: 663141f1b4690f79286f9b1904ae619139a91936 [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23#include <common.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020024#ifdef CONFIG_MX31
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010025#include <asm/arch/mx31-regs.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020026#endif
27#ifdef CONFIG_MX51
28#include <asm/arch/imx-regs.h>
29#endif
30#include <asm/io.h>
31#include <mxc_gpio.h>
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010032
33/* GPIO port description */
34static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020035 [0] = GPIO1_BASE_ADDR,
36 [1] = GPIO2_BASE_ADDR,
37 [2] = GPIO3_BASE_ADDR,
38#ifdef CONFIG_MX51
39 [3] = GPIO4_BASE_ADDR,
40#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010041};
42
Stefano Babicc4ea1422010-07-06 17:05:06 +020043int mxc_gpio_direction(unsigned int gpio, enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010044{
45 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020046 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010047 u32 l;
48
49 if (port >= ARRAY_SIZE(gpio_ports))
50 return 1;
51
52 gpio &= 0x1f;
53
Stefano Babicc4ea1422010-07-06 17:05:06 +020054 regs = (struct gpio_regs *)gpio_ports[port];
55
56 l = readl(&regs->gpio_dir);
57
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010058 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020059 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010060 l |= 1 << gpio;
61 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020062 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010063 l &= ~(1 << gpio);
64 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020065 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010066
67 return 0;
68}
69
Stefano Babicc4ea1422010-07-06 17:05:06 +020070void mxc_gpio_set(unsigned int gpio, unsigned int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010071{
72 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020073 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010074 u32 l;
75
76 if (port >= ARRAY_SIZE(gpio_ports))
77 return;
78
79 gpio &= 0x1f;
80
Stefano Babicc4ea1422010-07-06 17:05:06 +020081 regs = (struct gpio_regs *)gpio_ports[port];
82
83 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010084 if (value)
85 l |= 1 << gpio;
86 else
87 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020088 writel(l, &regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010089}
Stefano Babic7d27cd02010-04-13 12:07:00 +020090
Stefano Babicc4ea1422010-07-06 17:05:06 +020091int mxc_gpio_get(unsigned int gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +020092{
93 unsigned int port = gpio >> 5;
Stefano Babicc4ea1422010-07-06 17:05:06 +020094 struct gpio_regs *regs;
Stefano Babic7d27cd02010-04-13 12:07:00 +020095 u32 l;
96
97 if (port >= ARRAY_SIZE(gpio_ports))
98 return -1;
99
100 gpio &= 0x1f;
101
Stefano Babicc4ea1422010-07-06 17:05:06 +0200102 regs = (struct gpio_regs *)gpio_ports[port];
103
104 l = (readl(&regs->gpio_dr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200105
106 return l;
107}