blob: f615fce32e1f62fcfafe038bc59297f8d9503cb2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang5a09d132015-11-04 14:25:13 +08002/*
3 * Atmel PIO4 device driver
4 *
5 * Copyright (C) 2015 Atmel Corporation
6 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yang5a09d132015-11-04 14:25:13 +08007 */
8#include <common.h>
Wenyou Yang0b971522016-07-20 17:16:26 +08009#include <clk.h>
Wenyou Yang5a09d132015-11-04 14:25:13 +080010#include <dm.h>
Wenyou Yang0b971522016-07-20 17:16:26 +080011#include <fdtdec.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Wenyou Yang5a09d132015-11-04 14:25:13 +080013#include <asm/arch/hardware.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Wenyou Yang0b971522016-07-20 17:16:26 +080015#include <asm/gpio.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Wenyou Yang5a09d132015-11-04 14:25:13 +080017#include <mach/gpio.h>
18#include <mach/atmel_pio4.h>
19
Wenyou Yang0b971522016-07-20 17:16:26 +080020DECLARE_GLOBAL_DATA_PTR;
21
Wenyou Yang5a09d132015-11-04 14:25:13 +080022static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
23{
24 struct atmel_pio4_port *base = NULL;
25
26 switch (port) {
27 case AT91_PIO_PORTA:
28 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
29 break;
30 case AT91_PIO_PORTB:
31 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
32 break;
33 case AT91_PIO_PORTC:
34 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
35 break;
36 case AT91_PIO_PORTD:
37 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
38 break;
39 default:
40 printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
41 port);
42 break;
43 }
44
45 return base;
46}
47
48static int atmel_pio4_config_io_func(u32 port, u32 pin,
Ludovic Desroches86504912018-04-24 10:16:01 +030049 u32 func, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +080050{
51 struct atmel_pio4_port *port_base;
52 u32 reg, mask;
53
Wenyou Yang312bf892016-07-20 17:16:25 +080054 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glassf44b4bf2017-09-17 16:54:53 -060055 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +080056
57 port_base = atmel_pio4_port_base(port);
58 if (!port_base)
Simon Glassf44b4bf2017-09-17 16:54:53 -060059 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +080060
61 mask = 1 << pin;
62 reg = func;
Ludovic Desroches86504912018-04-24 10:16:01 +030063 reg |= config;
Wenyou Yang5a09d132015-11-04 14:25:13 +080064
65 writel(mask, &port_base->mskr);
66 writel(reg, &port_base->cfgr);
67
68 return 0;
69}
70
Ludovic Desroches86504912018-04-24 10:16:01 +030071int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +080072{
73 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +080074 ATMEL_PIO_CFGR_FUNC_GPIO,
Ludovic Desroches86504912018-04-24 10:16:01 +030075 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +080076}
77
Ludovic Desroches86504912018-04-24 10:16:01 +030078int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +080079{
80 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +080081 ATMEL_PIO_CFGR_FUNC_PERIPH_A,
Ludovic Desroches86504912018-04-24 10:16:01 +030082 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +080083}
84
Ludovic Desroches86504912018-04-24 10:16:01 +030085int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +080086{
87 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +080088 ATMEL_PIO_CFGR_FUNC_PERIPH_B,
Ludovic Desroches86504912018-04-24 10:16:01 +030089 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +080090}
91
Ludovic Desroches86504912018-04-24 10:16:01 +030092int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +080093{
94 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +080095 ATMEL_PIO_CFGR_FUNC_PERIPH_C,
Ludovic Desroches86504912018-04-24 10:16:01 +030096 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +080097}
98
Ludovic Desroches86504912018-04-24 10:16:01 +030099int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +0800100{
101 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +0800102 ATMEL_PIO_CFGR_FUNC_PERIPH_D,
Ludovic Desroches86504912018-04-24 10:16:01 +0300103 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800104}
105
Ludovic Desroches86504912018-04-24 10:16:01 +0300106int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +0800107{
108 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +0800109 ATMEL_PIO_CFGR_FUNC_PERIPH_E,
Ludovic Desroches86504912018-04-24 10:16:01 +0300110 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800111}
112
Ludovic Desroches86504912018-04-24 10:16:01 +0300113int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +0800114{
115 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +0800116 ATMEL_PIO_CFGR_FUNC_PERIPH_F,
Ludovic Desroches86504912018-04-24 10:16:01 +0300117 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800118}
119
Ludovic Desroches86504912018-04-24 10:16:01 +0300120int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config)
Wenyou Yang5a09d132015-11-04 14:25:13 +0800121{
122 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang312bf892016-07-20 17:16:25 +0800123 ATMEL_PIO_CFGR_FUNC_PERIPH_G,
Ludovic Desroches86504912018-04-24 10:16:01 +0300124 config);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800125}
126
127int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
128{
129 struct atmel_pio4_port *port_base;
130 u32 reg, mask;
131
Wenyou Yang312bf892016-07-20 17:16:25 +0800132 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600133 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800134
135 port_base = atmel_pio4_port_base(port);
136 if (!port_base)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600137 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800138
139 mask = 0x01 << pin;
Wenyou Yang312bf892016-07-20 17:16:25 +0800140 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800141
142 writel(mask, &port_base->mskr);
143 writel(reg, &port_base->cfgr);
144
145 if (value)
146 writel(mask, &port_base->sodr);
147 else
148 writel(mask, &port_base->codr);
149
150 return 0;
151}
152
153int atmel_pio4_get_pio_input(u32 port, u32 pin)
154{
155 struct atmel_pio4_port *port_base;
156 u32 reg, mask;
157
Wenyou Yang312bf892016-07-20 17:16:25 +0800158 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600159 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800160
161 port_base = atmel_pio4_port_base(port);
162 if (!port_base)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600163 return -EINVAL;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800164
165 mask = 0x01 << pin;
Wenyou Yang312bf892016-07-20 17:16:25 +0800166 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800167
168 writel(mask, &port_base->mskr);
169 writel(reg, &port_base->cfgr);
170
171 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
172}
173
Simon Glassfa4689a2019-12-06 21:41:35 -0700174#if CONFIG_IS_ENABLED(DM_GPIO)
Wenyou Yang0b971522016-07-20 17:16:26 +0800175
176struct atmel_pioctrl_data {
177 u32 nbanks;
178};
179
Simon Glassb75b15b2020-12-03 16:55:23 -0700180struct atmel_pio4_plat {
Wenyou Yang0b971522016-07-20 17:16:26 +0800181 struct atmel_pio4_port *reg_base;
182};
183
184static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
185 u32 bank)
186{
Simon Glassb75b15b2020-12-03 16:55:23 -0700187 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Wenyou Yang0b971522016-07-20 17:16:26 +0800188 struct atmel_pio4_port *port_base =
189 (struct atmel_pio4_port *)((u32)plat->reg_base +
190 ATMEL_PIO_BANK_OFFSET * bank);
191
192 return port_base;
193}
194
Wenyou Yang5a09d132015-11-04 14:25:13 +0800195static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
196{
Wenyou Yang0b971522016-07-20 17:16:26 +0800197 u32 bank = ATMEL_PIO_BANK(offset);
198 u32 line = ATMEL_PIO_LINE(offset);
199 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
200 u32 mask = BIT(line);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800201
202 writel(mask, &port_base->mskr);
Wenyou Yang0b971522016-07-20 17:16:26 +0800203
204 clrbits_le32(&port_base->cfgr,
205 ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800206
207 return 0;
208}
209
210static int atmel_pio4_direction_output(struct udevice *dev,
211 unsigned offset, int value)
212{
Wenyou Yang0b971522016-07-20 17:16:26 +0800213 u32 bank = ATMEL_PIO_BANK(offset);
214 u32 line = ATMEL_PIO_LINE(offset);
215 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
216 u32 mask = BIT(line);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800217
218 writel(mask, &port_base->mskr);
Wenyou Yang0b971522016-07-20 17:16:26 +0800219
220 clrsetbits_le32(&port_base->cfgr,
221 ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800222
223 if (value)
224 writel(mask, &port_base->sodr);
225 else
226 writel(mask, &port_base->codr);
227
228 return 0;
229}
230
231static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
232{
Wenyou Yang0b971522016-07-20 17:16:26 +0800233 u32 bank = ATMEL_PIO_BANK(offset);
234 u32 line = ATMEL_PIO_LINE(offset);
235 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
236 u32 mask = BIT(line);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800237
238 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
239}
240
241static int atmel_pio4_set_value(struct udevice *dev,
242 unsigned offset, int value)
243{
Wenyou Yang0b971522016-07-20 17:16:26 +0800244 u32 bank = ATMEL_PIO_BANK(offset);
245 u32 line = ATMEL_PIO_LINE(offset);
246 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
247 u32 mask = BIT(line);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800248
249 if (value)
250 writel(mask, &port_base->sodr);
251 else
252 writel(mask, &port_base->codr);
253
254 return 0;
255}
256
257static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
258{
Wenyou Yang0b971522016-07-20 17:16:26 +0800259 u32 bank = ATMEL_PIO_BANK(offset);
260 u32 line = ATMEL_PIO_LINE(offset);
261 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
262 u32 mask = BIT(line);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800263
264 writel(mask, &port_base->mskr);
265
266 return (readl(&port_base->cfgr) &
Wenyou Yang312bf892016-07-20 17:16:25 +0800267 ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800268}
269
270static const struct dm_gpio_ops atmel_pio4_ops = {
271 .direction_input = atmel_pio4_direction_input,
272 .direction_output = atmel_pio4_direction_output,
273 .get_value = atmel_pio4_get_value,
274 .set_value = atmel_pio4_set_value,
275 .get_function = atmel_pio4_get_function,
276};
277
Wenyou Yang0b971522016-07-20 17:16:26 +0800278static int atmel_pio4_bind(struct udevice *dev)
279{
Simon Glass292796f2017-05-17 17:18:06 -0600280 return dm_scan_fdt_dev(dev);
Wenyou Yang0b971522016-07-20 17:16:26 +0800281}
282
Wenyou Yang5a09d132015-11-04 14:25:13 +0800283static int atmel_pio4_probe(struct udevice *dev)
284{
Simon Glassb75b15b2020-12-03 16:55:23 -0700285 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Wenyou Yang5a09d132015-11-04 14:25:13 +0800286 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Wenyou Yang0b971522016-07-20 17:16:26 +0800287 struct atmel_pioctrl_data *pioctrl_data;
Wenyou Yang0b971522016-07-20 17:16:26 +0800288 struct clk clk;
289 fdt_addr_t addr_base;
290 u32 nbanks;
Wenyou Yang0b971522016-07-20 17:16:26 +0800291 int ret;
292
293 ret = clk_get_by_index(dev, 0, &clk);
294 if (ret)
295 return ret;
296
Wenyou Yang0b971522016-07-20 17:16:26 +0800297 ret = clk_enable(&clk);
298 if (ret)
299 return ret;
300
301 clk_free(&clk);
302
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900303 addr_base = dev_read_addr(dev);
Wenyou Yang0b971522016-07-20 17:16:26 +0800304 if (addr_base == FDT_ADDR_T_NONE)
305 return -EINVAL;
306
307 plat->reg_base = (struct atmel_pio4_port *)addr_base;
308
309 pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
310 nbanks = pioctrl_data->nbanks;
311
Simon Glassdd79d6e2017-01-17 16:52:55 -0700312 uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
313 NULL);
Wenyou Yang0b971522016-07-20 17:16:26 +0800314 uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
Wenyou Yang5a09d132015-11-04 14:25:13 +0800315
316 return 0;
317}
318
Wenyou Yang0b971522016-07-20 17:16:26 +0800319/*
320 * The number of banks can be different from a SoC to another one.
321 * We can have up to 16 banks.
322 */
323static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
324 .nbanks = 4,
325};
326
327static const struct udevice_id atmel_pio4_ids[] = {
328 {
329 .compatible = "atmel,sama5d2-gpio",
330 .data = (ulong)&atmel_sama5d2_pioctrl_data,
331 },
332 {}
333};
334
Wenyou Yang5a09d132015-11-04 14:25:13 +0800335U_BOOT_DRIVER(gpio_atmel_pio4) = {
336 .name = "gpio_atmel_pio4",
337 .id = UCLASS_GPIO,
338 .ops = &atmel_pio4_ops,
339 .probe = atmel_pio4_probe,
Wenyou Yang0b971522016-07-20 17:16:26 +0800340 .bind = atmel_pio4_bind,
341 .of_match = atmel_pio4_ids,
Simon Glassb75b15b2020-12-03 16:55:23 -0700342 .plat_auto = sizeof(struct atmel_pio4_plat),
Wenyou Yang5a09d132015-11-04 14:25:13 +0800343};
Wenyou Yang0b971522016-07-20 17:16:26 +0800344
Wenyou Yang5a09d132015-11-04 14:25:13 +0800345#endif