xref: /openbmc/u-boot/drivers/gpio/kw_gpio.c (revision a2ee7f07)
1 /*
2  * arch/arm/plat-orion/gpio.c
3  *
4  * Marvell Orion SoC GPIO handling.
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22  * MA 02110-1301 USA
23  */
24 
25 /*
26  * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver.
27  * Removed orion_gpiochip struct and kernel level irq handling.
28  *
29  * Dieter Kiermaier dk-arm-linux@gmx.de
30  */
31 
32 #include <common.h>
33 #include <asm/bitops.h>
34 #include <asm/io.h>
35 #include <asm/arch/kirkwood.h>
36 #include <asm/arch/gpio.h>
37 
38 static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
39 static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
40 
41 void __set_direction(unsigned pin, int input)
42 {
43 	u32 u;
44 
45 	u = readl(GPIO_IO_CONF(pin));
46 	if (input)
47 		u |= 1 << (pin & 31);
48 	else
49 		u &= ~(1 << (pin & 31));
50 	writel(u, GPIO_IO_CONF(pin));
51 
52 	u = readl(GPIO_IO_CONF(pin));
53 }
54 
55 void __set_level(unsigned pin, int high)
56 {
57 	u32 u;
58 
59 	u = readl(GPIO_OUT(pin));
60 	if (high)
61 		u |= 1 << (pin & 31);
62 	else
63 		u &= ~(1 << (pin & 31));
64 	writel(u, GPIO_OUT(pin));
65 }
66 
67 void __set_blinking(unsigned pin, int blink)
68 {
69 	u32 u;
70 
71 	u = readl(GPIO_BLINK_EN(pin));
72 	if (blink)
73 		u |= 1 << (pin & 31);
74 	else
75 		u &= ~(1 << (pin & 31));
76 	writel(u, GPIO_BLINK_EN(pin));
77 }
78 
79 int kw_gpio_is_valid(unsigned pin, int mode)
80 {
81 	if (pin < GPIO_MAX) {
82 		if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
83 			goto err_out;
84 
85 		if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
86 			goto err_out;
87 		return 0;
88 	}
89 
90 err_out:
91 		printf("%s: invalid GPIO %d\n", __func__, pin);
92 	return 1;
93 }
94 
95 void kw_gpio_set_valid(unsigned pin, int mode)
96 {
97 	if (mode == 1)
98 		mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
99 	if (mode & GPIO_INPUT_OK)
100 		__set_bit(pin, gpio_valid_input);
101 	else
102 		__clear_bit(pin, gpio_valid_input);
103 	if (mode & GPIO_OUTPUT_OK)
104 		__set_bit(pin, gpio_valid_output);
105 	else
106 		__clear_bit(pin, gpio_valid_output);
107 }
108 /*
109  * GENERIC_GPIO primitives.
110  */
111 int kw_gpio_direction_input(unsigned pin)
112 {
113 	if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
114 		return 1;
115 
116 	/* Configure GPIO direction. */
117 	__set_direction(pin, 1);
118 
119 	return 0;
120 }
121 
122 int kw_gpio_direction_output(unsigned pin, int value)
123 {
124 	if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
125 	{
126 		printf("%s: invalid GPIO %d\n", __func__, pin);
127 		return 1;
128 	}
129 
130 	__set_blinking(pin, 0);
131 
132 	/* Configure GPIO output value. */
133 	__set_level(pin, value);
134 
135 	/* Configure GPIO direction. */
136 	__set_direction(pin, 0);
137 
138 	return 0;
139 }
140 
141 int kw_gpio_get_value(unsigned pin)
142 {
143 	int val;
144 
145 	if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
146 		val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
147 	else
148 		val = readl(GPIO_OUT(pin));
149 
150 	return (val >> (pin & 31)) & 1;
151 }
152 
153 void kw_gpio_set_value(unsigned pin, int value)
154 {
155 	/* Configure GPIO output value. */
156 	__set_level(pin, value);
157 }
158 
159 void kw_gpio_set_blink(unsigned pin, int blink)
160 {
161 	/* Set output value to zero. */
162 	__set_level(pin, 0);
163 
164 	/* Set blinking. */
165 	__set_blinking(pin, blink);
166 }
167