blob: 65d064b46dfb617fbfc25e1340a5d78fde049957 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang2c62c562015-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 Yang2c62c562015-11-04 14:25:13 +08007 */
Wenyou Yangee3311d2016-07-20 17:16:26 +08008#include <clk.h>
Wenyou Yang2c62c562015-11-04 14:25:13 +08009#include <dm.h>
Wenyou Yangee3311d2016-07-20 17:16:26 +080010#include <fdtdec.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Wenyou Yang2c62c562015-11-04 14:25:13 +080012#include <asm/arch/hardware.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wenyou Yangee3311d2016-07-20 17:16:26 +080014#include <asm/gpio.h>
Simon Glasscd93d622020-05-10 11:40:13 -060015#include <linux/bitops.h>
Wenyou Yang2c62c562015-11-04 14:25:13 +080016#include <mach/gpio.h>
17#include <mach/atmel_pio4.h>
18
Wenyou Yangee3311d2016-07-20 17:16:26 +080019DECLARE_GLOBAL_DATA_PTR;
20
Wenyou Yang2c62c562015-11-04 14:25:13 +080021static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
22{
23 struct atmel_pio4_port *base = NULL;
24
25 switch (port) {
26 case AT91_PIO_PORTA:
27 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
28 break;
29 case AT91_PIO_PORTB:
30 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
31 break;
32 case AT91_PIO_PORTC:
33 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
34 break;
35 case AT91_PIO_PORTD:
36 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
37 break;
Mihai Sainc1cadac2022-05-25 13:32:08 +030038#if (ATMEL_PIO_PORTS > 4)
39 case AT91_PIO_PORTE:
40 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOE;
41 break;
42#endif
Wenyou Yang2c62c562015-11-04 14:25:13 +080043 default:
44 printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
45 port);
46 break;
47 }
48
49 return base;
50}
51
52static int atmel_pio4_config_io_func(u32 port, u32 pin,
Ludovic Desroches8ee54672018-04-24 10:16:01 +030053 u32 func, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +080054{
55 struct atmel_pio4_port *port_base;
56 u32 reg, mask;
57
Wenyou Yang46ed9382016-07-20 17:16:25 +080058 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glass7c843192017-09-17 16:54:53 -060059 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +080060
61 port_base = atmel_pio4_port_base(port);
62 if (!port_base)
Simon Glass7c843192017-09-17 16:54:53 -060063 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +080064
65 mask = 1 << pin;
66 reg = func;
Ludovic Desroches8ee54672018-04-24 10:16:01 +030067 reg |= config;
Wenyou Yang2c62c562015-11-04 14:25:13 +080068
69 writel(mask, &port_base->mskr);
70 writel(reg, &port_base->cfgr);
71
72 return 0;
73}
74
Ludovic Desroches8ee54672018-04-24 10:16:01 +030075int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +080076{
77 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +080078 ATMEL_PIO_CFGR_FUNC_GPIO,
Ludovic Desroches8ee54672018-04-24 10:16:01 +030079 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +080080}
81
Ludovic Desroches8ee54672018-04-24 10:16:01 +030082int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +080083{
84 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +080085 ATMEL_PIO_CFGR_FUNC_PERIPH_A,
Ludovic Desroches8ee54672018-04-24 10:16:01 +030086 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +080087}
88
Ludovic Desroches8ee54672018-04-24 10:16:01 +030089int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +080090{
91 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +080092 ATMEL_PIO_CFGR_FUNC_PERIPH_B,
Ludovic Desroches8ee54672018-04-24 10:16:01 +030093 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +080094}
95
Ludovic Desroches8ee54672018-04-24 10:16:01 +030096int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +080097{
98 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +080099 ATMEL_PIO_CFGR_FUNC_PERIPH_C,
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300100 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800101}
102
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300103int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +0800104{
105 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +0800106 ATMEL_PIO_CFGR_FUNC_PERIPH_D,
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300107 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800108}
109
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300110int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +0800111{
112 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +0800113 ATMEL_PIO_CFGR_FUNC_PERIPH_E,
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300114 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800115}
116
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300117int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +0800118{
119 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +0800120 ATMEL_PIO_CFGR_FUNC_PERIPH_F,
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300121 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800122}
123
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300124int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config)
Wenyou Yang2c62c562015-11-04 14:25:13 +0800125{
126 return atmel_pio4_config_io_func(port, pin,
Wenyou Yang46ed9382016-07-20 17:16:25 +0800127 ATMEL_PIO_CFGR_FUNC_PERIPH_G,
Ludovic Desroches8ee54672018-04-24 10:16:01 +0300128 config);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800129}
130
131int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
132{
133 struct atmel_pio4_port *port_base;
134 u32 reg, mask;
135
Wenyou Yang46ed9382016-07-20 17:16:25 +0800136 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glass7c843192017-09-17 16:54:53 -0600137 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800138
139 port_base = atmel_pio4_port_base(port);
140 if (!port_base)
Simon Glass7c843192017-09-17 16:54:53 -0600141 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800142
143 mask = 0x01 << pin;
Wenyou Yang46ed9382016-07-20 17:16:25 +0800144 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800145
146 writel(mask, &port_base->mskr);
147 writel(reg, &port_base->cfgr);
148
149 if (value)
150 writel(mask, &port_base->sodr);
151 else
152 writel(mask, &port_base->codr);
153
154 return 0;
155}
156
157int atmel_pio4_get_pio_input(u32 port, u32 pin)
158{
159 struct atmel_pio4_port *port_base;
160 u32 reg, mask;
161
Wenyou Yang46ed9382016-07-20 17:16:25 +0800162 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
Simon Glass7c843192017-09-17 16:54:53 -0600163 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800164
165 port_base = atmel_pio4_port_base(port);
166 if (!port_base)
Simon Glass7c843192017-09-17 16:54:53 -0600167 return -EINVAL;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800168
169 mask = 0x01 << pin;
Wenyou Yang46ed9382016-07-20 17:16:25 +0800170 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800171
172 writel(mask, &port_base->mskr);
173 writel(reg, &port_base->cfgr);
174
175 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
176}
177
Simon Glassbcee8d62019-12-06 21:41:35 -0700178#if CONFIG_IS_ENABLED(DM_GPIO)
Wenyou Yangee3311d2016-07-20 17:16:26 +0800179
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300180/**
181 * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct
182 * @nbanks: number of PIO banks
183 * @last_bank_count: number of lines in the last bank (can be less than
184 * the rest of the banks).
185 */
Wenyou Yangee3311d2016-07-20 17:16:26 +0800186struct atmel_pioctrl_data {
187 u32 nbanks;
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300188 u32 last_bank_count;
Wenyou Yangee3311d2016-07-20 17:16:26 +0800189};
190
Simon Glass8a8d24b2020-12-03 16:55:23 -0700191struct atmel_pio4_plat {
Wenyou Yangee3311d2016-07-20 17:16:26 +0800192 struct atmel_pio4_port *reg_base;
193};
194
195static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
196 u32 bank)
197{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700198 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800199 struct atmel_pio4_port *port_base =
200 (struct atmel_pio4_port *)((u32)plat->reg_base +
201 ATMEL_PIO_BANK_OFFSET * bank);
202
203 return port_base;
204}
205
Wenyou Yang2c62c562015-11-04 14:25:13 +0800206static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
207{
Wenyou Yangee3311d2016-07-20 17:16:26 +0800208 u32 bank = ATMEL_PIO_BANK(offset);
209 u32 line = ATMEL_PIO_LINE(offset);
210 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
211 u32 mask = BIT(line);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800212
213 writel(mask, &port_base->mskr);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800214
215 clrbits_le32(&port_base->cfgr,
216 ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800217
218 return 0;
219}
220
221static int atmel_pio4_direction_output(struct udevice *dev,
222 unsigned offset, int value)
223{
Wenyou Yangee3311d2016-07-20 17:16:26 +0800224 u32 bank = ATMEL_PIO_BANK(offset);
225 u32 line = ATMEL_PIO_LINE(offset);
226 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
227 u32 mask = BIT(line);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800228
229 writel(mask, &port_base->mskr);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800230
231 clrsetbits_le32(&port_base->cfgr,
232 ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800233
234 if (value)
235 writel(mask, &port_base->sodr);
236 else
237 writel(mask, &port_base->codr);
238
239 return 0;
240}
241
242static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
243{
Wenyou Yangee3311d2016-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 Yang2c62c562015-11-04 14:25:13 +0800248
249 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
250}
251
252static int atmel_pio4_set_value(struct udevice *dev,
253 unsigned offset, int value)
254{
Wenyou Yangee3311d2016-07-20 17:16:26 +0800255 u32 bank = ATMEL_PIO_BANK(offset);
256 u32 line = ATMEL_PIO_LINE(offset);
257 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
258 u32 mask = BIT(line);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800259
260 if (value)
261 writel(mask, &port_base->sodr);
262 else
263 writel(mask, &port_base->codr);
264
265 return 0;
266}
267
268static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
269{
Wenyou Yangee3311d2016-07-20 17:16:26 +0800270 u32 bank = ATMEL_PIO_BANK(offset);
271 u32 line = ATMEL_PIO_LINE(offset);
272 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
273 u32 mask = BIT(line);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800274
275 writel(mask, &port_base->mskr);
276
277 return (readl(&port_base->cfgr) &
Wenyou Yang46ed9382016-07-20 17:16:25 +0800278 ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800279}
280
281static const struct dm_gpio_ops atmel_pio4_ops = {
282 .direction_input = atmel_pio4_direction_input,
283 .direction_output = atmel_pio4_direction_output,
284 .get_value = atmel_pio4_get_value,
285 .set_value = atmel_pio4_set_value,
286 .get_function = atmel_pio4_get_function,
287};
288
Wenyou Yangee3311d2016-07-20 17:16:26 +0800289static int atmel_pio4_bind(struct udevice *dev)
290{
Simon Glass79fc0c72017-05-17 17:18:06 -0600291 return dm_scan_fdt_dev(dev);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800292}
293
Wenyou Yang2c62c562015-11-04 14:25:13 +0800294static int atmel_pio4_probe(struct udevice *dev)
295{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700296 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Wenyou Yang2c62c562015-11-04 14:25:13 +0800297 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800298 struct atmel_pioctrl_data *pioctrl_data;
Wenyou Yangee3311d2016-07-20 17:16:26 +0800299 struct clk clk;
300 fdt_addr_t addr_base;
301 u32 nbanks;
Wenyou Yangee3311d2016-07-20 17:16:26 +0800302 int ret;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800303
Wenyou Yangee3311d2016-07-20 17:16:26 +0800304 ret = clk_get_by_index(dev, 0, &clk);
305 if (ret)
306 return ret;
307
Wenyou Yangee3311d2016-07-20 17:16:26 +0800308 ret = clk_enable(&clk);
309 if (ret)
310 return ret;
311
Masahiro Yamada25484932020-07-17 14:36:48 +0900312 addr_base = dev_read_addr(dev);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800313 if (addr_base == FDT_ADDR_T_NONE)
314 return -EINVAL;
315
316 plat->reg_base = (struct atmel_pio4_port *)addr_base;
317
318 pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
319 nbanks = pioctrl_data->nbanks;
320
Simon Glasse160f7d2017-01-17 16:52:55 -0700321 uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
322 NULL);
Wenyou Yangee3311d2016-07-20 17:16:26 +0800323 uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
Wenyou Yang2c62c562015-11-04 14:25:13 +0800324
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300325 /* if last bank has limited number of pins, adjust accordingly */
326 if (pioctrl_data->last_bank_count != ATMEL_PIO_NPINS_PER_BANK) {
327 uc_priv->gpio_count -= ATMEL_PIO_NPINS_PER_BANK;
328 uc_priv->gpio_count += pioctrl_data->last_bank_count;
329 }
330
Wenyou Yang2c62c562015-11-04 14:25:13 +0800331 return 0;
332}
333
Wenyou Yangee3311d2016-07-20 17:16:26 +0800334/*
335 * The number of banks can be different from a SoC to another one.
336 * We can have up to 16 banks.
337 */
338static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
339 .nbanks = 4,
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300340 .last_bank_count = ATMEL_PIO_NPINS_PER_BANK,
341};
342
343static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data = {
344 .nbanks = 5,
345 .last_bank_count = 8, /* 5th bank has only 8 lines on sama7g5 */
Wenyou Yangee3311d2016-07-20 17:16:26 +0800346};
347
348static const struct udevice_id atmel_pio4_ids[] = {
349 {
Wenyou Yangee3311d2016-07-20 17:16:26 +0800350 .data = (ulong)&atmel_sama5d2_pioctrl_data,
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300351 }, {
Eugen Hristeve1038ac2021-04-07 11:39:28 +0300352 .data = (ulong)&microchip_sama7g5_pioctrl_data,
Wenyou Yangee3311d2016-07-20 17:16:26 +0800353 },
354 {}
355};
356
Wenyou Yang2c62c562015-11-04 14:25:13 +0800357U_BOOT_DRIVER(gpio_atmel_pio4) = {
358 .name = "gpio_atmel_pio4",
359 .id = UCLASS_GPIO,
360 .ops = &atmel_pio4_ops,
361 .probe = atmel_pio4_probe,
Wenyou Yangee3311d2016-07-20 17:16:26 +0800362 .bind = atmel_pio4_bind,
363 .of_match = atmel_pio4_ids,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700364 .plat_auto = sizeof(struct atmel_pio4_plat),
Wenyou Yang2c62c562015-11-04 14:25:13 +0800365};
Wenyou Yangee3311d2016-07-20 17:16:26 +0800366
Wenyou Yang2c62c562015-11-04 14:25:13 +0800367#endif