xref: /openbmc/u-boot/drivers/gpio/mxc_gpio.c (revision bbbc1ae9)
1 /*
2  * Copyright (C) 2009
3  * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4  *
5  * Copyright (C) 2011
6  * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
7  *
8  * 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>
27 #include <asm/arch/imx-regs.h>
28 #include <asm/gpio.h>
29 #include <asm/io.h>
30 #include <errno.h>
31 
32 enum mxc_gpio_direction {
33 	MXC_GPIO_DIRECTION_IN,
34 	MXC_GPIO_DIRECTION_OUT,
35 };
36 
37 #define GPIO_TO_PORT(n)		(n / 32)
38 
39 /* GPIO port description */
40 static unsigned long gpio_ports[] = {
41 	[0] = GPIO1_BASE_ADDR,
42 	[1] = GPIO2_BASE_ADDR,
43 	[2] = GPIO3_BASE_ADDR,
44 #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
45 	[3] = GPIO4_BASE_ADDR,
46 #endif
47 #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
48 	[4] = GPIO5_BASE_ADDR,
49 	[5] = GPIO6_BASE_ADDR,
50 	[6] = GPIO7_BASE_ADDR,
51 #endif
52 };
53 
54 static int mxc_gpio_direction(unsigned int gpio,
55 	enum mxc_gpio_direction direction)
56 {
57 	unsigned int port = GPIO_TO_PORT(gpio);
58 	struct gpio_regs *regs;
59 	u32 l;
60 
61 	if (port >= ARRAY_SIZE(gpio_ports))
62 		return -1;
63 
64 	gpio &= 0x1f;
65 
66 	regs = (struct gpio_regs *)gpio_ports[port];
67 
68 	l = readl(&regs->gpio_dir);
69 
70 	switch (direction) {
71 	case MXC_GPIO_DIRECTION_OUT:
72 		l |= 1 << gpio;
73 		break;
74 	case MXC_GPIO_DIRECTION_IN:
75 		l &= ~(1 << gpio);
76 	}
77 	writel(l, &regs->gpio_dir);
78 
79 	return 0;
80 }
81 
82 int gpio_set_value(unsigned gpio, int value)
83 {
84 	unsigned int port = GPIO_TO_PORT(gpio);
85 	struct gpio_regs *regs;
86 	u32 l;
87 
88 	if (port >= ARRAY_SIZE(gpio_ports))
89 		return -1;
90 
91 	gpio &= 0x1f;
92 
93 	regs = (struct gpio_regs *)gpio_ports[port];
94 
95 	l = readl(&regs->gpio_dr);
96 	if (value)
97 		l |= 1 << gpio;
98 	else
99 		l &= ~(1 << gpio);
100 	writel(l, &regs->gpio_dr);
101 
102 	return 0;
103 }
104 
105 int gpio_get_value(unsigned gpio)
106 {
107 	unsigned int port = GPIO_TO_PORT(gpio);
108 	struct gpio_regs *regs;
109 	u32 val;
110 
111 	if (port >= ARRAY_SIZE(gpio_ports))
112 		return -1;
113 
114 	gpio &= 0x1f;
115 
116 	regs = (struct gpio_regs *)gpio_ports[port];
117 
118 	val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
119 
120 	return val;
121 }
122 
123 int gpio_request(unsigned gpio, const char *label)
124 {
125 	unsigned int port = GPIO_TO_PORT(gpio);
126 	if (port >= ARRAY_SIZE(gpio_ports))
127 		return -1;
128 	return 0;
129 }
130 
131 int gpio_free(unsigned gpio)
132 {
133 	return 0;
134 }
135 
136 int gpio_direction_input(unsigned gpio)
137 {
138 	return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
139 }
140 
141 int gpio_direction_output(unsigned gpio, int value)
142 {
143 	int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
144 
145 	if (ret < 0)
146 		return ret;
147 
148 	return gpio_set_value(gpio, value);
149 }
150