blob: dfba3a2374cb3a91de9fe8aafe4dc9fddf5ea802 [file] [log] [blame]
Stefan Bosch95e9a8e2020-07-10 19:07:26 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2016 Nexell
4 * Youngbok, Park <ybpark@nexell.co.kr>
5 */
6
7/*
8 * FIXME : will be remove after support pinctrl
9 */
10#include <linux/types.h>
11#include <asm/io.h>
12#include <asm/arch/nexell.h>
13#include "asm/arch/nx_gpio.h"
14#define NUMBER_OF_GPIO_MODULE 5
15u32 __g_nx_gpio_valid_bit[NUMBER_OF_GPIO_MODULE] = {
16 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
17
18static struct {
19 struct nx_gpio_register_set *pregister;
20} __g_module_variables[NUMBER_OF_GPIO_MODULE] = {
21 { (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOA },
22 { (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOB },
23 { (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOC },
24 { (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOD },
25 { (struct nx_gpio_register_set *)PHY_BASEADDR_GPIOE },
26};
27
28enum { nx_gpio_max_bit = 32 };
29
30void nx_gpio_set_bit(u32 *value, u32 bit, int enable)
31{
32 register u32 newvalue;
33
34 newvalue = *value;
35 newvalue &= ~(1ul << bit);
36 newvalue |= (u32)enable << bit;
37 writel(newvalue, value);
38}
39
40int nx_gpio_get_bit(u32 value, u32 bit)
41{
42 return (int)((value >> bit) & (1ul));
43}
44
45void nx_gpio_set_bit2(u32 *value, u32 bit, u32 bit_value)
46{
47 register u32 newvalue = *value;
48
49 newvalue = (u32)(newvalue & ~(3ul << (bit * 2)));
50 newvalue = (u32)(newvalue | (bit_value << (bit * 2)));
51
52 writel(newvalue, value);
53}
54
55u32 nx_gpio_get_bit2(u32 value, u32 bit)
56{
57 return (u32)((u32)(value >> (bit * 2)) & 3ul);
58}
59
60int nx_gpio_initialize(void)
61{
62 static int binit;
63 u32 i;
64
65 binit = 0;
66
67 if (binit == 0) {
68 for (i = 0; i < NUMBER_OF_GPIO_MODULE; i++)
69 __g_module_variables[i].pregister = NULL;
70 binit = true;
71 }
72 for (i = 0; i < NUMBER_OF_GPIO_MODULE; i++) {
73 __g_nx_gpio_valid_bit[i] = 0xFFFFFFFF;
74 };
75 return true;
76}
77
78u32 nx_gpio_get_number_of_module(void)
79{
80 return NUMBER_OF_GPIO_MODULE;
81}
82
83u32 nx_gpio_get_size_of_register_set(void)
84{
85 return sizeof(struct nx_gpio_register_set);
86}
87
88void nx_gpio_set_base_address(u32 module_index, void *base_address)
89{
90 __g_module_variables[module_index].pregister =
91 (struct nx_gpio_register_set *)base_address;
92}
93
94void *nx_gpio_get_base_address(u32 module_index)
95{
96 return (void *)__g_module_variables[module_index].pregister;
97}
98
99int nx_gpio_open_module(u32 module_index)
100{
101 register struct nx_gpio_register_set *pregister;
102
103 pregister = __g_module_variables[module_index].pregister;
104 writel(0xFFFFFFFF, &pregister->gpiox_slew_disable_default);
105 writel(0xFFFFFFFF, &pregister->gpiox_drv1_disable_default);
106 writel(0xFFFFFFFF, &pregister->gpiox_drv0_disable_default);
107 writel(0xFFFFFFFF, &pregister->gpiox_pullsel_disable_default);
108 writel(0xFFFFFFFF, &pregister->gpiox_pullenb_disable_default);
109 return true;
110}
111
112int nx_gpio_close_module(u32 module_index) { return true; }
113
114int nx_gpio_check_busy(u32 module_index) { return false; }
115
116void nx_gpio_set_pad_function(u32 module_index, u32 bit_number,
117 u32 padfunc)
118{
119 register struct nx_gpio_register_set *pregister;
120
121 pregister = __g_module_variables[module_index].pregister;
122 nx_gpio_set_bit2(&pregister->gpioxaltfn[bit_number / 16],
123 bit_number % 16, padfunc);
124}
125
126void nx_gpio_set_pad_function32(u32 module_index, u32 msbvalue, u32 lsbvalue)
127{
128 register struct nx_gpio_register_set *pregister;
129
130 pregister = __g_module_variables[module_index].pregister;
131 writel(lsbvalue, &pregister->gpioxaltfn[0]);
132 writel(msbvalue, &pregister->gpioxaltfn[1]);
133}
134
135int nx_gpio_get_pad_function(u32 module_index, u32 bit_number)
136{
137 register struct nx_gpio_register_set *pregister;
138
139 pregister = __g_module_variables[module_index].pregister;
140 return (int)nx_gpio_get_bit2
141 (readl(&pregister->gpioxaltfn[bit_number / 16]),
142 bit_number % 16);
143}
144
145void nx_gpio_set_output_enable(u32 module_index, u32 bit_number,
146 int output_enb)
147{
148 register struct nx_gpio_register_set *pregister;
149
150 pregister = __g_module_variables[module_index].pregister;
151 nx_gpio_set_bit(&pregister->gpioxoutenb, bit_number, output_enb);
152}
153
154int nx_gpio_get_detect_enable(u32 module_index, u32 bit_number)
155{
156 register struct nx_gpio_register_set *pregister;
157
158 pregister = __g_module_variables[module_index].pregister;
159 return nx_gpio_get_bit(readl(&pregister->gpioxdetenb), bit_number);
160}
161
162u32 nx_gpio_get_detect_enable32(u32 module_index)
163{
164 register struct nx_gpio_register_set *pregister;
165
166 pregister = __g_module_variables[module_index].pregister;
167 return readl(&pregister->gpioxdetenb);
168}
169
170void nx_gpio_set_detect_enable(u32 module_index, u32 bit_number,
171 int detect_enb)
172{
173 register struct nx_gpio_register_set *pregister;
174
175 pregister = __g_module_variables[module_index].pregister;
176 nx_gpio_set_bit(&pregister->gpioxdetenb, bit_number, detect_enb);
177}
178
179void nx_gpio_set_detect_enable32(u32 module_index, u32 enable_flag)
180{
181 register struct nx_gpio_register_set *pregister;
182
183 pregister = __g_module_variables[module_index].pregister;
184 writel(enable_flag, &pregister->gpioxdetenb);
185}
186
187int nx_gpio_get_output_enable(u32 module_index, u32 bit_number)
188{
189 register struct nx_gpio_register_set *pregister;
190
191 pregister = __g_module_variables[module_index].pregister;
192 return nx_gpio_get_bit(readl(&pregister->gpioxoutenb), bit_number);
193}
194
195void nx_gpio_set_output_enable32(u32 module_index, int output_enb)
196{
197 register struct nx_gpio_register_set *pregister;
198
199 pregister = __g_module_variables[module_index].pregister;
200 if (output_enb)
201 writel(0xFFFFFFFF, &pregister->gpioxoutenb);
202 else
203 writel(0x0, &pregister->gpioxoutenb);
204}
205
206u32 nx_gpio_get_output_enable32(u32 module_index)
207{
208 register struct nx_gpio_register_set *pregister;
209
210 pregister = __g_module_variables[module_index].pregister;
211 return readl(&pregister->gpioxoutenb);
212}
213
214void nx_gpio_set_output_value(u32 module_index, u32 bit_number, int value)
215{
216 register struct nx_gpio_register_set *pregister;
217
218 pregister = __g_module_variables[module_index].pregister;
219 nx_gpio_set_bit(&pregister->gpioxout, bit_number, value);
220}
221
222int nx_gpio_get_output_value(u32 module_index, u32 bit_number)
223{
224 register struct nx_gpio_register_set *pregister;
225
226 pregister = __g_module_variables[module_index].pregister;
227 return nx_gpio_get_bit(readl(&pregister->gpioxout), bit_number);
228}
229
230void nx_gpio_set_output_value32(u32 module_index, u32 value)
231{
232 register struct nx_gpio_register_set *pregister;
233
234 pregister = __g_module_variables[module_index].pregister;
235 writel(value, &pregister->gpioxout);
236}
237
238u32 nx_gpio_get_output_value32(u32 module_index)
239{
240 register struct nx_gpio_register_set *pregister;
241
242 pregister = __g_module_variables[module_index].pregister;
243 return readl(&pregister->gpioxout);
244}
245
246int nx_gpio_get_input_value(u32 module_index, u32 bit_number)
247{
248 register struct nx_gpio_register_set *pregister;
249
250 pregister = __g_module_variables[module_index].pregister;
251 return nx_gpio_get_bit(readl(&pregister->gpioxpad), bit_number);
252}
253
254void nx_gpio_set_pull_select(u32 module_index, u32 bit_number, int enable)
255{
256 nx_gpio_set_bit(&__g_module_variables[module_index]
257 .pregister->gpiox_pullsel_disable_default,
258 bit_number, true);
259 nx_gpio_set_bit
260 (&__g_module_variables[module_index].pregister->gpiox_pullsel,
261 bit_number, enable);
262}
263
264void nx_gpio_set_pull_select32(u32 module_index, u32 value)
265{
266 writel(value,
267 &__g_module_variables[module_index].pregister->gpiox_pullsel);
268}
269
270int nx_gpio_get_pull_select(u32 module_index, u32 bit_number)
271{
272 return nx_gpio_get_bit
273 (__g_module_variables[module_index].pregister->gpiox_pullsel,
274 bit_number);
275}
276
277u32 nx_gpio_get_pull_select32(u32 module_index)
278{
279 return __g_module_variables[module_index].pregister->gpiox_pullsel;
280}
281
282void nx_gpio_set_pull_mode(u32 module_index, u32 bit_number, u32 mode)
283{
284 nx_gpio_set_bit(&__g_module_variables[module_index]
285 .pregister->gpiox_pullsel_disable_default,
286 bit_number, true);
287 nx_gpio_set_bit(&__g_module_variables[module_index]
288 .pregister->gpiox_pullenb_disable_default,
289 bit_number, true);
290 if (mode == nx_gpio_pull_off) {
291 nx_gpio_set_bit
292 (&__g_module_variables[module_index].pregister->gpiox_pullenb,
293 bit_number, false);
294 nx_gpio_set_bit
295 (&__g_module_variables[module_index].pregister->gpiox_pullsel,
296 bit_number, false);
297 } else {
298 nx_gpio_set_bit
299 (&__g_module_variables[module_index].pregister->gpiox_pullsel,
300 bit_number, (mode & 1 ? true : false));
301 nx_gpio_set_bit
302 (&__g_module_variables[module_index].pregister->gpiox_pullenb,
303 bit_number, true);
304 }
305}
306
307void nx_gpio_set_fast_slew(u32 module_index, u32 bit_number,
308 int enable)
309{
310 register struct nx_gpio_register_set *pregister;
311
312 pregister = __g_module_variables[module_index].pregister;
313 nx_gpio_set_bit(&pregister->gpiox_slew, bit_number,
314 (int)(!enable));
315}
316
317void nx_gpio_set_drive_strength(u32 module_index, u32 bit_number,
318 u32 drvstrength)
319{
320 register struct nx_gpio_register_set *pregister;
321
322 pregister = __g_module_variables[module_index].pregister;
323 nx_gpio_set_bit(&pregister->gpiox_drv1, bit_number,
324 (int)(((u32)drvstrength >> 0) & 0x1));
325 nx_gpio_set_bit(&pregister->gpiox_drv0, bit_number,
326 (int)(((u32)drvstrength >> 1) & 0x1));
327}
328
329void nx_gpio_set_drive_strength_disable_default(u32 module_index,
330 u32 bit_number, int enable)
331{
332 register struct nx_gpio_register_set *pregister;
333
334 pregister = __g_module_variables[module_index].pregister;
335 nx_gpio_set_bit(&pregister->gpiox_drv1_disable_default, bit_number,
336 (int)(enable));
337 nx_gpio_set_bit(&pregister->gpiox_drv0_disable_default, bit_number,
338 (int)(enable));
339}
340
341u32 nx_gpio_get_drive_strength(u32 module_index, u32 bit_number)
342{
343 register struct nx_gpio_register_set *pregister;
344 register u32 retvalue;
345
346 pregister = __g_module_variables[module_index].pregister;
347 retvalue =
348 nx_gpio_get_bit(readl(&pregister->gpiox_drv0), bit_number) << 1;
349 retvalue |=
350 nx_gpio_get_bit(readl(&pregister->gpiox_drv1), bit_number) << 0;
351 return retvalue;
352}