blob: cc26cc165856766ff24370b359b48b490c799ba1 [file] [log] [blame]
Dieter Kiermaierec164412009-06-29 14:45:08 +02001/*
2 * arch/arm/plat-orion/gpio.c
3 *
4 * Marvell Orion SoC GPIO handling.
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Dieter Kiermaierec164412009-06-29 14:45:08 +02007 */
8
9/*
10 * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver.
11 * Removed orion_gpiochip struct and kernel level irq handling.
12 *
13 * Dieter Kiermaier dk-arm-linux@gmx.de
14 */
15
16#include <common.h>
Masahiro Yamadaed3986c2017-11-22 02:38:11 +090017#include <linux/bitops.h>
Lei Wena7efd712011-10-18 20:11:42 +053018#include <asm/io.h>
Stefan Roese3dc23f72014-10-22 12:13:06 +020019#include <asm/arch/soc.h>
Dieter Kiermaierec164412009-06-29 14:45:08 +020020#include <asm/arch/gpio.h>
21
22static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
23static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
24
25void __set_direction(unsigned pin, int input)
26{
27 u32 u;
28
29 u = readl(GPIO_IO_CONF(pin));
30 if (input)
31 u |= 1 << (pin & 31);
32 else
33 u &= ~(1 << (pin & 31));
34 writel(u, GPIO_IO_CONF(pin));
35
36 u = readl(GPIO_IO_CONF(pin));
37}
38
Jeroen Hofstee2115d892014-10-08 22:57:37 +020039static void __set_level(unsigned pin, int high)
Dieter Kiermaierec164412009-06-29 14:45:08 +020040{
41 u32 u;
42
43 u = readl(GPIO_OUT(pin));
44 if (high)
45 u |= 1 << (pin & 31);
46 else
47 u &= ~(1 << (pin & 31));
48 writel(u, GPIO_OUT(pin));
49}
50
Jeroen Hofstee2115d892014-10-08 22:57:37 +020051static void __set_blinking(unsigned pin, int blink)
Dieter Kiermaierec164412009-06-29 14:45:08 +020052{
53 u32 u;
54
55 u = readl(GPIO_BLINK_EN(pin));
56 if (blink)
57 u |= 1 << (pin & 31);
58 else
59 u &= ~(1 << (pin & 31));
60 writel(u, GPIO_BLINK_EN(pin));
61}
62
63int kw_gpio_is_valid(unsigned pin, int mode)
64{
65 if (pin < GPIO_MAX) {
66 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
67 goto err_out;
68
69 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
70 goto err_out;
71 return 0;
72 }
73
74err_out:
75 printf("%s: invalid GPIO %d\n", __func__, pin);
76 return 1;
77}
78
79void kw_gpio_set_valid(unsigned pin, int mode)
80{
81 if (mode == 1)
82 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
83 if (mode & GPIO_INPUT_OK)
84 __set_bit(pin, gpio_valid_input);
85 else
86 __clear_bit(pin, gpio_valid_input);
87 if (mode & GPIO_OUTPUT_OK)
88 __set_bit(pin, gpio_valid_output);
89 else
90 __clear_bit(pin, gpio_valid_output);
91}
92/*
93 * GENERIC_GPIO primitives.
94 */
95int kw_gpio_direction_input(unsigned pin)
96{
Holger Bruncke5afdf52011-08-21 23:21:06 +000097 if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
Dieter Kiermaierec164412009-06-29 14:45:08 +020098 return 1;
99
100 /* Configure GPIO direction. */
101 __set_direction(pin, 1);
102
103 return 0;
104}
105
106int kw_gpio_direction_output(unsigned pin, int value)
107{
108 if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
109 {
110 printf("%s: invalid GPIO %d\n", __func__, pin);
111 return 1;
112 }
113
114 __set_blinking(pin, 0);
115
116 /* Configure GPIO output value. */
117 __set_level(pin, value);
118
119 /* Configure GPIO direction. */
120 __set_direction(pin, 0);
121
122 return 0;
123}
124
125int kw_gpio_get_value(unsigned pin)
126{
127 int val;
128
129 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
130 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
131 else
132 val = readl(GPIO_OUT(pin));
133
134 return (val >> (pin & 31)) & 1;
135}
136
137void kw_gpio_set_value(unsigned pin, int value)
138{
139 /* Configure GPIO output value. */
140 __set_level(pin, value);
141}
142
143void kw_gpio_set_blink(unsigned pin, int blink)
144{
145 /* Set output value to zero. */
146 __set_level(pin, 0);
147
148 /* Set blinking. */
149 __set_blinking(pin, blink);
150}