blob: c3d0dd6b28c154709fc6dd51434ed3fa828248a0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jens Scharsig8d065462010-02-03 22:46:16 +01002/*
Bo Shenf0adeaa2013-08-13 14:38:32 +08003 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
Jens Scharsig8d065462010-02-03 22:46:16 +01004 *
5 * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
6 *
7 * Copyright (C) 2005 HP Labs
Jens Scharsig8d065462010-02-03 22:46:16 +01008 */
9
10#include <config.h>
Wenyou Yange29b82b2017-03-23 12:46:21 +080011#include <clk.h>
Simon Glass4d717882014-10-29 13:08:57 -060012#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Reinhard Meyerb06208c2010-11-07 13:26:14 +010014#include <asm/io.h>
Alexey Brodkin267d8e22014-02-26 17:47:58 +040015#include <linux/sizes.h>
Simon Glass4d717882014-10-29 13:08:57 -060016#include <asm/gpio.h>
Jens Scharsig8d065462010-02-03 22:46:16 +010017#include <asm/arch/hardware.h>
Jens Scharsig8d065462010-02-03 22:46:16 +010018#include <asm/arch/at91_pio.h>
Simon Glass4d717882014-10-29 13:08:57 -060019
20#define GPIO_PER_BANK 32
Jens Scharsig8d065462010-02-03 22:46:16 +010021
Bo Shen02d88142013-08-22 15:24:40 +080022static struct at91_port *at91_pio_get_port(unsigned port)
23{
24 switch (port) {
25 case AT91_PIO_PORTA:
26 return (struct at91_port *)ATMEL_BASE_PIOA;
27 case AT91_PIO_PORTB:
28 return (struct at91_port *)ATMEL_BASE_PIOB;
29 case AT91_PIO_PORTC:
30 return (struct at91_port *)ATMEL_BASE_PIOC;
31#if (ATMEL_PIO_PORTS > 3)
32 case AT91_PIO_PORTD:
33 return (struct at91_port *)ATMEL_BASE_PIOD;
34#if (ATMEL_PIO_PORTS > 4)
35 case AT91_PIO_PORTE:
36 return (struct at91_port *)ATMEL_BASE_PIOE;
37#endif
38#endif
39 default:
Wu, Josh383543a2014-05-07 16:50:45 +080040 printf("Error: at91_gpio: Fail to get PIO base!\n");
Bo Shen02d88142013-08-22 15:24:40 +080041 return NULL;
42 }
43}
44
Simon Glass4d717882014-10-29 13:08:57 -060045static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset,
46 int use_pullup)
47{
48 u32 mask;
49
50 mask = 1 << offset;
51 if (use_pullup)
52 writel(mask, &at91_port->puer);
53 else
54 writel(mask, &at91_port->pudr);
55 writel(mask, &at91_port->per);
56}
57
Jens Scharsig8d065462010-02-03 22:46:16 +010058int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
59{
Bo Shen02d88142013-08-22 15:24:40 +080060 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +010061
Simon Glass4d717882014-10-29 13:08:57 -060062 if (at91_port && (pin < GPIO_PER_BANK))
63 at91_set_port_pullup(at91_port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +080064
Jens Scharsig8d065462010-02-03 22:46:16 +010065 return 0;
66}
67
68/*
69 * mux the pin to the "GPIO" peripheral role.
70 */
71int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
72{
Bo Shen02d88142013-08-22 15:24:40 +080073 struct at91_port *at91_port = at91_pio_get_port(port);
74 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +010075
Simon Glass4d717882014-10-29 13:08:57 -060076 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +010077 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +080078 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +010079 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +080080 writel(mask, &at91_port->per);
Jens Scharsig8d065462010-02-03 22:46:16 +010081 }
Bo Shen02d88142013-08-22 15:24:40 +080082
Jens Scharsig8d065462010-02-03 22:46:16 +010083 return 0;
84}
85
86/*
87 * mux the pin to the "A" internal peripheral role.
88 */
89int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
90{
Bo Shen02d88142013-08-22 15:24:40 +080091 struct at91_port *at91_port = at91_pio_get_port(port);
92 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +010093
Simon Glass4d717882014-10-29 13:08:57 -060094 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +010095 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +080096 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +010097 at91_set_pio_pullup(port, pin, use_pullup);
Wenyou Yang4a92a3e2017-03-23 12:44:36 +080098 writel(mask, &at91_port->mux.pio2.asr);
Bo Shen02d88142013-08-22 15:24:40 +080099 writel(mask, &at91_port->pdr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100100 }
Bo Shen02d88142013-08-22 15:24:40 +0800101
Jens Scharsig8d065462010-02-03 22:46:16 +0100102 return 0;
103}
104
105/*
106 * mux the pin to the "B" internal peripheral role.
107 */
108int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
109{
Bo Shen02d88142013-08-22 15:24:40 +0800110 struct at91_port *at91_port = at91_pio_get_port(port);
111 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100112
Simon Glass4d717882014-10-29 13:08:57 -0600113 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100114 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800115 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100116 at91_set_pio_pullup(port, pin, use_pullup);
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800117 writel(mask, &at91_port->mux.pio2.bsr);
Bo Shen02d88142013-08-22 15:24:40 +0800118 writel(mask, &at91_port->pdr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100119 }
Bo Shen02d88142013-08-22 15:24:40 +0800120
Jens Scharsig8d065462010-02-03 22:46:16 +0100121 return 0;
122}
123
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800124/*
125 * mux the pin to the "A" internal peripheral role.
126 */
127int at91_pio3_set_a_periph(unsigned port, unsigned pin, int use_pullup)
128{
129 struct at91_port *at91_port = at91_pio_get_port(port);
130 u32 mask;
131
132 if (at91_port && (pin < GPIO_PER_BANK)) {
133 mask = 1 << pin;
134 writel(mask, &at91_port->idr);
135 at91_set_pio_pullup(port, pin, use_pullup);
136 writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask,
137 &at91_port->mux.pio3.abcdsr1);
138 writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask,
139 &at91_port->mux.pio3.abcdsr2);
140
141 writel(mask, &at91_port->pdr);
142 }
143
144 return 0;
145}
146
147/*
148 * mux the pin to the "B" internal peripheral role.
149 */
150int at91_pio3_set_b_periph(unsigned port, unsigned pin, int use_pullup)
151{
152 struct at91_port *at91_port = at91_pio_get_port(port);
153 u32 mask;
154
155 if (at91_port && (pin < GPIO_PER_BANK)) {
156 mask = 1 << pin;
157 writel(mask, &at91_port->idr);
158 at91_set_pio_pullup(port, pin, use_pullup);
159 writel(readl(&at91_port->mux.pio3.abcdsr1) | mask,
160 &at91_port->mux.pio3.abcdsr1);
161 writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask,
162 &at91_port->mux.pio3.abcdsr2);
163
164 writel(mask, &at91_port->pdr);
165 }
166
167 return 0;
168}
Bo Shen0ac13452012-05-20 15:50:00 +0000169/*
170 * mux the pin to the "C" internal peripheral role.
171 */
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800172int at91_pio3_set_c_periph(unsigned port, unsigned pin, int use_pullup)
Bo Shen0ac13452012-05-20 15:50:00 +0000173{
Bo Shen02d88142013-08-22 15:24:40 +0800174 struct at91_port *at91_port = at91_pio_get_port(port);
175 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000176
Simon Glass4d717882014-10-29 13:08:57 -0600177 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000178 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800179 writel(mask, &at91_port->idr);
Bo Shen0ac13452012-05-20 15:50:00 +0000180 at91_set_pio_pullup(port, pin, use_pullup);
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800181 writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask,
182 &at91_port->mux.pio3.abcdsr1);
183 writel(readl(&at91_port->mux.pio3.abcdsr2) | mask,
184 &at91_port->mux.pio3.abcdsr2);
Bo Shen02d88142013-08-22 15:24:40 +0800185 writel(mask, &at91_port->pdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000186 }
Bo Shen02d88142013-08-22 15:24:40 +0800187
Bo Shen0ac13452012-05-20 15:50:00 +0000188 return 0;
189}
190
191/*
192 * mux the pin to the "D" internal peripheral role.
193 */
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800194int at91_pio3_set_d_periph(unsigned port, unsigned pin, int use_pullup)
Bo Shen0ac13452012-05-20 15:50:00 +0000195{
Bo Shen02d88142013-08-22 15:24:40 +0800196 struct at91_port *at91_port = at91_pio_get_port(port);
197 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000198
Simon Glass4d717882014-10-29 13:08:57 -0600199 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000200 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800201 writel(mask, &at91_port->idr);
Bo Shen0ac13452012-05-20 15:50:00 +0000202 at91_set_pio_pullup(port, pin, use_pullup);
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800203 writel(readl(&at91_port->mux.pio3.abcdsr1) | mask,
204 &at91_port->mux.pio3.abcdsr1);
205 writel(readl(&at91_port->mux.pio3.abcdsr2) | mask,
206 &at91_port->mux.pio3.abcdsr2);
Bo Shen02d88142013-08-22 15:24:40 +0800207 writel(mask, &at91_port->pdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000208 }
Bo Shen02d88142013-08-22 15:24:40 +0800209
Bo Shen0ac13452012-05-20 15:50:00 +0000210 return 0;
211}
Bo Shen0ac13452012-05-20 15:50:00 +0000212
Simon Glassfa4689a2019-12-06 21:41:35 -0700213#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass4d717882014-10-29 13:08:57 -0600214static bool at91_get_port_output(struct at91_port *at91_port, int offset)
215{
216 u32 mask, val;
217
218 mask = 1 << offset;
219 val = readl(&at91_port->osr);
220 return val & mask;
221}
Zixun LI6e399502024-11-13 11:10:22 +0100222
223static bool at91_is_port_gpio(struct at91_port *at91_port, int offset)
224{
225 u32 mask, val;
226
227 mask = 1 << offset;
228 val = readl(&at91_port->psr);
229 return !!(val & mask);
230}
Zixun LI760e4dd2024-11-13 11:10:54 +0100231
232static void at91_set_port_multi_drive(struct at91_port *at91_port, int offset, int is_on)
233{
234 u32 mask;
235
236 mask = 1 << offset;
237 if (is_on)
238 writel(mask, &at91_port->mder);
239 else
240 writel(mask, &at91_port->mddr);
241}
Simon Glass4d717882014-10-29 13:08:57 -0600242#endif
243
244static void at91_set_port_input(struct at91_port *at91_port, int offset,
245 int use_pullup)
246{
247 u32 mask;
248
249 mask = 1 << offset;
250 writel(mask, &at91_port->idr);
251 at91_set_port_pullup(at91_port, offset, use_pullup);
252 writel(mask, &at91_port->odr);
253 writel(mask, &at91_port->per);
254}
255
Jens Scharsig8d065462010-02-03 22:46:16 +0100256/*
257 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
258 * configure it for an input.
259 */
260int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
261{
Bo Shen02d88142013-08-22 15:24:40 +0800262 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100263
Simon Glass4d717882014-10-29 13:08:57 -0600264 if (at91_port && (pin < GPIO_PER_BANK))
265 at91_set_port_input(at91_port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +0800266
Jens Scharsig8d065462010-02-03 22:46:16 +0100267 return 0;
268}
269
Simon Glass4d717882014-10-29 13:08:57 -0600270static void at91_set_port_output(struct at91_port *at91_port, int offset,
271 int value)
272{
273 u32 mask;
274
275 mask = 1 << offset;
276 writel(mask, &at91_port->idr);
277 writel(mask, &at91_port->pudr);
278 if (value)
279 writel(mask, &at91_port->sodr);
280 else
281 writel(mask, &at91_port->codr);
282 writel(mask, &at91_port->oer);
283 writel(mask, &at91_port->per);
284}
285
Jens Scharsig8d065462010-02-03 22:46:16 +0100286/*
287 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
288 * and configure it for an output.
289 */
290int at91_set_pio_output(unsigned port, u32 pin, int value)
291{
Bo Shen02d88142013-08-22 15:24:40 +0800292 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100293
Simon Glass4d717882014-10-29 13:08:57 -0600294 if (at91_port && (pin < GPIO_PER_BANK))
295 at91_set_port_output(at91_port, pin, value);
Bo Shen02d88142013-08-22 15:24:40 +0800296
Jens Scharsig8d065462010-02-03 22:46:16 +0100297 return 0;
298}
299
300/*
301 * enable/disable the glitch filter. mostly used with IRQ handling.
302 */
303int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
304{
Bo Shen02d88142013-08-22 15:24:40 +0800305 struct at91_port *at91_port = at91_pio_get_port(port);
306 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100307
Simon Glass4d717882014-10-29 13:08:57 -0600308 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100309 mask = 1 << pin;
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800310 if (is_on)
311 writel(mask, &at91_port->ifer);
312 else
313 writel(mask, &at91_port->ifdr);
314 }
315
316 return 0;
317}
318
319/*
320 * enable/disable the glitch filter. mostly used with IRQ handling.
321 */
322int at91_pio3_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
323{
324 struct at91_port *at91_port = at91_pio_get_port(port);
325 u32 mask;
326
327 if (at91_port && (pin < GPIO_PER_BANK)) {
328 mask = 1 << pin;
Bo Shen0ac13452012-05-20 15:50:00 +0000329 if (is_on) {
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800330 writel(mask, &at91_port->mux.pio3.ifscdr);
Bo Shen02d88142013-08-22 15:24:40 +0800331 writel(mask, &at91_port->ifer);
Bo Shen0ac13452012-05-20 15:50:00 +0000332 } else {
Bo Shen02d88142013-08-22 15:24:40 +0800333 writel(mask, &at91_port->ifdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000334 }
335 }
Bo Shen02d88142013-08-22 15:24:40 +0800336
Bo Shen0ac13452012-05-20 15:50:00 +0000337 return 0;
338}
339
Bo Shen0ac13452012-05-20 15:50:00 +0000340/*
341 * enable/disable the debounce filter.
342 */
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800343int at91_pio3_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
Bo Shen0ac13452012-05-20 15:50:00 +0000344{
Bo Shen02d88142013-08-22 15:24:40 +0800345 struct at91_port *at91_port = at91_pio_get_port(port);
346 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000347
Simon Glass4d717882014-10-29 13:08:57 -0600348 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000349 mask = 1 << pin;
350 if (is_on) {
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800351 writel(mask, &at91_port->mux.pio3.ifscer);
352 writel(div & PIO_SCDR_DIV, &at91_port->mux.pio3.scdr);
Bo Shen02d88142013-08-22 15:24:40 +0800353 writel(mask, &at91_port->ifer);
Bo Shen0ac13452012-05-20 15:50:00 +0000354 } else {
Bo Shen02d88142013-08-22 15:24:40 +0800355 writel(mask, &at91_port->ifdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000356 }
357 }
Bo Shen02d88142013-08-22 15:24:40 +0800358
Bo Shen0ac13452012-05-20 15:50:00 +0000359 return 0;
360}
361
362/*
363 * enable/disable the pull-down.
364 * If pull-up already enabled while calling the function, we disable it.
365 */
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800366int at91_pio3_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
Bo Shen0ac13452012-05-20 15:50:00 +0000367{
Bo Shen02d88142013-08-22 15:24:40 +0800368 struct at91_port *at91_port = at91_pio_get_port(port);
369 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000370
Simon Glass4d717882014-10-29 13:08:57 -0600371 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000372 mask = 1 << pin;
Marek Vasutba91bd52016-05-04 23:05:23 +0200373 if (is_on) {
374 at91_set_pio_pullup(port, pin, 0);
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800375 writel(mask, &at91_port->mux.pio3.ppder);
Marek Vasutba91bd52016-05-04 23:05:23 +0200376 } else
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800377 writel(mask, &at91_port->mux.pio3.ppddr);
Bo Shen0ac13452012-05-20 15:50:00 +0000378 }
Bo Shen02d88142013-08-22 15:24:40 +0800379
Bo Shen0ac13452012-05-20 15:50:00 +0000380 return 0;
381}
382
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800383int at91_pio3_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
384{
385 struct at91_port *at91_port = at91_pio_get_port(port);
386
387 if (use_pullup)
388 at91_pio3_set_pio_pulldown(port, pin, 0);
389
390 if (at91_port && (pin < GPIO_PER_BANK))
391 at91_set_port_pullup(at91_port, pin, use_pullup);
392
393 return 0;
394}
395
Bo Shen0ac13452012-05-20 15:50:00 +0000396/*
397 * disable Schmitt trigger
398 */
Wenyou Yang4a92a3e2017-03-23 12:44:36 +0800399int at91_pio3_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
Bo Shen0ac13452012-05-20 15:50:00 +0000400{
Bo Shen02d88142013-08-22 15:24:40 +0800401 struct at91_port *at91_port = at91_pio_get_port(port);
402 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000403
Simon Glass4d717882014-10-29 13:08:57 -0600404 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000405 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800406 writel(readl(&at91_port->schmitt) | mask,
407 &at91_port->schmitt);
Jens Scharsig8d065462010-02-03 22:46:16 +0100408 }
Bo Shen02d88142013-08-22 15:24:40 +0800409
Jens Scharsig8d065462010-02-03 22:46:16 +0100410 return 0;
411}
412
413/*
414 * enable/disable the multi-driver. This is only valid for output and
415 * allows the output pin to run as an open collector output.
416 */
417int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
418{
Bo Shen02d88142013-08-22 15:24:40 +0800419 struct at91_port *at91_port = at91_pio_get_port(port);
420 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100421
Simon Glass4d717882014-10-29 13:08:57 -0600422 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100423 mask = 1 << pin;
424 if (is_on)
Bo Shen02d88142013-08-22 15:24:40 +0800425 writel(mask, &at91_port->mder);
Jens Scharsig8d065462010-02-03 22:46:16 +0100426 else
Bo Shen02d88142013-08-22 15:24:40 +0800427 writel(mask, &at91_port->mddr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100428 }
Bo Shen02d88142013-08-22 15:24:40 +0800429
Jens Scharsig8d065462010-02-03 22:46:16 +0100430 return 0;
431}
432
Simon Glass4d717882014-10-29 13:08:57 -0600433static void at91_set_port_value(struct at91_port *at91_port, int offset,
434 int value)
435{
436 u32 mask;
437
438 mask = 1 << offset;
439 if (value)
440 writel(mask, &at91_port->sodr);
441 else
442 writel(mask, &at91_port->codr);
443}
444
Jens Scharsig8d065462010-02-03 22:46:16 +0100445/*
446 * assuming the pin is muxed as a gpio output, set its value.
447 */
448int at91_set_pio_value(unsigned port, unsigned pin, int value)
449{
Bo Shen02d88142013-08-22 15:24:40 +0800450 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100451
Simon Glass4d717882014-10-29 13:08:57 -0600452 if (at91_port && (pin < GPIO_PER_BANK))
453 at91_set_port_value(at91_port, pin, value);
Bo Shen02d88142013-08-22 15:24:40 +0800454
Jens Scharsig8d065462010-02-03 22:46:16 +0100455 return 0;
456}
457
Simon Glass4d717882014-10-29 13:08:57 -0600458static int at91_get_port_value(struct at91_port *at91_port, int offset)
459{
460 u32 pdsr = 0, mask;
461
462 mask = 1 << offset;
463 pdsr = readl(&at91_port->pdsr) & mask;
464
465 return pdsr != 0;
466}
Jens Scharsig8d065462010-02-03 22:46:16 +0100467/*
468 * read the pin's value (works even if it's not muxed as a gpio).
469 */
470int at91_get_pio_value(unsigned port, unsigned pin)
471{
Bo Shen02d88142013-08-22 15:24:40 +0800472 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100473
Simon Glass4d717882014-10-29 13:08:57 -0600474 if (at91_port && (pin < GPIO_PER_BANK))
475 return at91_get_port_value(at91_port, pin);
Bo Shen02d88142013-08-22 15:24:40 +0800476
Simon Glass4d717882014-10-29 13:08:57 -0600477 return 0;
Jens Scharsig8d065462010-02-03 22:46:16 +0100478}
Bo Shenad1d2ac2013-08-13 14:38:31 +0800479
Simon Glassfa4689a2019-12-06 21:41:35 -0700480#if !CONFIG_IS_ENABLED(DM_GPIO)
Bo Shenad1d2ac2013-08-13 14:38:31 +0800481/* Common GPIO API */
482
Bo Shenad1d2ac2013-08-13 14:38:31 +0800483int gpio_request(unsigned gpio, const char *label)
484{
485 return 0;
486}
487
488int gpio_free(unsigned gpio)
489{
490 return 0;
491}
492
493int gpio_direction_input(unsigned gpio)
494{
495 at91_set_pio_input(at91_gpio_to_port(gpio),
496 at91_gpio_to_pin(gpio), 0);
497 return 0;
498}
499
500int gpio_direction_output(unsigned gpio, int value)
501{
502 at91_set_pio_output(at91_gpio_to_port(gpio),
503 at91_gpio_to_pin(gpio), value);
504 return 0;
505}
506
507int gpio_get_value(unsigned gpio)
508{
509 return at91_get_pio_value(at91_gpio_to_port(gpio),
510 at91_gpio_to_pin(gpio));
511}
512
513int gpio_set_value(unsigned gpio, int value)
514{
515 at91_set_pio_value(at91_gpio_to_port(gpio),
516 at91_gpio_to_pin(gpio), value);
517
518 return 0;
519}
Simon Glass4d717882014-10-29 13:08:57 -0600520#endif
521
Simon Glassfa4689a2019-12-06 21:41:35 -0700522#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass4d717882014-10-29 13:08:57 -0600523
524struct at91_port_priv {
525 struct at91_port *regs;
526};
527
528/* set GPIO pin 'gpio' as an input */
529static int at91_gpio_direction_input(struct udevice *dev, unsigned offset)
530{
Axel Lin98b9b372015-01-31 14:47:34 +0800531 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-10-29 13:08:57 -0600532
533 at91_set_port_input(port->regs, offset, 0);
534
535 return 0;
536}
537
538/* set GPIO pin 'gpio' as an output, with polarity 'value' */
539static int at91_gpio_direction_output(struct udevice *dev, unsigned offset,
540 int value)
541{
Axel Lin98b9b372015-01-31 14:47:34 +0800542 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-10-29 13:08:57 -0600543
544 at91_set_port_output(port->regs, offset, value);
545
546 return 0;
547}
548
549/* read GPIO IN value of pin 'gpio' */
550static int at91_gpio_get_value(struct udevice *dev, unsigned offset)
551{
Axel Lin98b9b372015-01-31 14:47:34 +0800552 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-10-29 13:08:57 -0600553
554 return at91_get_port_value(port->regs, offset);
555}
556
557/* write GPIO OUT value to pin 'gpio' */
558static int at91_gpio_set_value(struct udevice *dev, unsigned offset,
559 int value)
560{
Axel Lin98b9b372015-01-31 14:47:34 +0800561 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-10-29 13:08:57 -0600562
563 at91_set_port_value(port->regs, offset, value);
564
565 return 0;
566}
567
568static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
569{
Axel Lin98b9b372015-01-31 14:47:34 +0800570 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-10-29 13:08:57 -0600571
Zixun LI6e399502024-11-13 11:10:22 +0100572 if (!at91_is_port_gpio(port->regs, offset))
573 return GPIOF_FUNC;
574
Simon Glass4d717882014-10-29 13:08:57 -0600575 if (at91_get_port_output(port->regs, offset))
576 return GPIOF_OUTPUT;
577 else
578 return GPIOF_INPUT;
579}
580
Zixun LI760e4dd2024-11-13 11:10:54 +0100581static int at91_gpio_set_flags(struct udevice *dev, unsigned int offset,
582 ulong flags)
583{
584 struct at91_port_priv *port = dev_get_priv(dev);
585 ulong supported_mask;
586
587 supported_mask = GPIOD_OPEN_DRAIN | GPIOD_MASK_DIR | GPIOD_PULL_UP;
588 if (flags & ~supported_mask)
589 return -ENOTSUPP;
590
591 if (flags & GPIOD_IS_OUT) {
592 if (flags & GPIOD_OPEN_DRAIN)
593 at91_set_port_multi_drive(port->regs, offset, true);
594 else
595 at91_set_port_multi_drive(port->regs, offset, false);
596
597 at91_set_port_output(port->regs, offset, flags & GPIOD_IS_OUT_ACTIVE);
598
599 } else if (flags & GPIOD_IS_IN) {
600 at91_set_port_input(port->regs, offset, false);
601 }
602 if (flags & GPIOD_PULL_UP)
603 at91_set_port_pullup(port->regs, offset, true);
604
605 return 0;
606}
607
James Byrneb7e33d32019-11-26 11:52:04 +0000608static const char *at91_get_bank_name(uint32_t base_addr)
609{
610 switch (base_addr) {
611 case ATMEL_BASE_PIOA:
612 return "PIOA";
613 case ATMEL_BASE_PIOB:
614 return "PIOB";
615 case ATMEL_BASE_PIOC:
616 return "PIOC";
617#if (ATMEL_PIO_PORTS > 3)
618 case ATMEL_BASE_PIOD:
619 return "PIOD";
620#if (ATMEL_PIO_PORTS > 4)
621 case ATMEL_BASE_PIOE:
622 return "PIOE";
623#endif
624#endif
625 }
626
627 return "undefined";
628}
629
Simon Glass4d717882014-10-29 13:08:57 -0600630static const struct dm_gpio_ops gpio_at91_ops = {
631 .direction_input = at91_gpio_direction_input,
632 .direction_output = at91_gpio_direction_output,
633 .get_value = at91_gpio_get_value,
634 .set_value = at91_gpio_set_value,
635 .get_function = at91_gpio_get_function,
Zixun LI760e4dd2024-11-13 11:10:54 +0100636 .set_flags = at91_gpio_set_flags,
Simon Glass4d717882014-10-29 13:08:57 -0600637};
638
639static int at91_gpio_probe(struct udevice *dev)
640{
641 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700642 struct at91_port_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700643 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Wenyou Yange29b82b2017-03-23 12:46:21 +0800644 struct clk clk;
645 int ret;
646
647 ret = clk_get_by_index(dev, 0, &clk);
648 if (ret)
649 return ret;
650
651 ret = clk_enable(&clk);
652 if (ret)
653 return ret;
654
Wenyou Yangeff6d8a2017-03-23 12:46:20 +0800655#if CONFIG_IS_ENABLED(OF_CONTROL)
Masahiro Yamada5150bc72020-08-04 14:14:41 +0900656 plat->base_addr = dev_read_addr(dev);
Wenyou Yangeff6d8a2017-03-23 12:46:20 +0800657#endif
James Byrneb7e33d32019-11-26 11:52:04 +0000658 plat->bank_name = at91_get_bank_name(plat->base_addr);
Simon Glass4d717882014-10-29 13:08:57 -0600659 port->regs = (struct at91_port *)plat->base_addr;
660
James Byrneb7e33d32019-11-26 11:52:04 +0000661 uc_priv->bank_name = plat->bank_name;
662 uc_priv->gpio_count = GPIO_PER_BANK;
663
Simon Glass4d717882014-10-29 13:08:57 -0600664 return 0;
665}
666
Wenyou Yangeff6d8a2017-03-23 12:46:20 +0800667#if CONFIG_IS_ENABLED(OF_CONTROL)
668static const struct udevice_id at91_gpio_ids[] = {
669 { .compatible = "atmel,at91rm9200-gpio" },
670 { }
671};
672#endif
673
Walter Lozano2901ac62020-06-25 01:10:04 -0300674U_BOOT_DRIVER(atmel_at91rm9200_gpio) = {
675 .name = "atmel_at91rm9200_gpio",
Simon Glass4d717882014-10-29 13:08:57 -0600676 .id = UCLASS_GPIO,
Wenyou Yangeff6d8a2017-03-23 12:46:20 +0800677#if CONFIG_IS_ENABLED(OF_CONTROL)
678 .of_match = at91_gpio_ids,
Simon Glassb75b15b2020-12-03 16:55:23 -0700679 .plat_auto = sizeof(struct at91_port_plat),
Wenyou Yangeff6d8a2017-03-23 12:46:20 +0800680#endif
Simon Glass4d717882014-10-29 13:08:57 -0600681 .ops = &gpio_at91_ops,
682 .probe = at91_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700683 .priv_auto = sizeof(struct at91_port_priv),
Simon Glass4d717882014-10-29 13:08:57 -0600684};
685#endif