1 // SPDX-License-Identifier: GPL-2.0+
3 * Atmel PIO4 device driver
5 * Copyright (C) 2015 Atmel Corporation
13 #include <asm/arch/hardware.h>
14 #include <asm/global_data.h>
16 #include <linux/bitops.h>
17 #include <mach/gpio.h>
18 #include <mach/atmel_pio4.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
24 struct atmel_pio4_port *base = NULL;
28 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
31 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
34 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
37 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
39 #if (ATMEL_PIO_PORTS > 4)
41 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOE;
45 printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
53 static int atmel_pio4_config_io_func(u32 port, u32 pin,
56 struct atmel_pio4_port *port_base;
59 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
62 port_base = atmel_pio4_port_base(port);
70 writel(mask, &port_base->mskr);
71 writel(reg, &port_base->cfgr);
76 int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config)
78 return atmel_pio4_config_io_func(port, pin,
79 ATMEL_PIO_CFGR_FUNC_GPIO,
83 int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config)
85 return atmel_pio4_config_io_func(port, pin,
86 ATMEL_PIO_CFGR_FUNC_PERIPH_A,
90 int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config)
92 return atmel_pio4_config_io_func(port, pin,
93 ATMEL_PIO_CFGR_FUNC_PERIPH_B,
97 int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config)
99 return atmel_pio4_config_io_func(port, pin,
100 ATMEL_PIO_CFGR_FUNC_PERIPH_C,
104 int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config)
106 return atmel_pio4_config_io_func(port, pin,
107 ATMEL_PIO_CFGR_FUNC_PERIPH_D,
111 int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config)
113 return atmel_pio4_config_io_func(port, pin,
114 ATMEL_PIO_CFGR_FUNC_PERIPH_E,
118 int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config)
120 return atmel_pio4_config_io_func(port, pin,
121 ATMEL_PIO_CFGR_FUNC_PERIPH_F,
125 int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config)
127 return atmel_pio4_config_io_func(port, pin,
128 ATMEL_PIO_CFGR_FUNC_PERIPH_G,
132 int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
134 struct atmel_pio4_port *port_base;
137 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
140 port_base = atmel_pio4_port_base(port);
145 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
147 writel(mask, &port_base->mskr);
148 writel(reg, &port_base->cfgr);
151 writel(mask, &port_base->sodr);
153 writel(mask, &port_base->codr);
158 int atmel_pio4_get_pio_input(u32 port, u32 pin)
160 struct atmel_pio4_port *port_base;
163 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
166 port_base = atmel_pio4_port_base(port);
171 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
173 writel(mask, &port_base->mskr);
174 writel(reg, &port_base->cfgr);
176 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
179 #if CONFIG_IS_ENABLED(DM_GPIO)
182 * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct
183 * @nbanks: number of PIO banks
184 * @last_bank_count: number of lines in the last bank (can be less than
185 * the rest of the banks).
187 struct atmel_pioctrl_data {
192 struct atmel_pio4_plat {
193 struct atmel_pio4_port *reg_base;
196 static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
199 struct atmel_pio4_plat *plat = dev_get_plat(dev);
200 struct atmel_pio4_port *port_base =
201 (struct atmel_pio4_port *)((u32)plat->reg_base +
202 ATMEL_PIO_BANK_OFFSET * bank);
207 static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
209 u32 bank = ATMEL_PIO_BANK(offset);
210 u32 line = ATMEL_PIO_LINE(offset);
211 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
212 u32 mask = BIT(line);
214 writel(mask, &port_base->mskr);
216 clrbits_le32(&port_base->cfgr,
217 ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
222 static int atmel_pio4_direction_output(struct udevice *dev,
223 unsigned offset, int value)
225 u32 bank = ATMEL_PIO_BANK(offset);
226 u32 line = ATMEL_PIO_LINE(offset);
227 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
228 u32 mask = BIT(line);
230 writel(mask, &port_base->mskr);
232 clrsetbits_le32(&port_base->cfgr,
233 ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
236 writel(mask, &port_base->sodr);
238 writel(mask, &port_base->codr);
243 static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
245 u32 bank = ATMEL_PIO_BANK(offset);
246 u32 line = ATMEL_PIO_LINE(offset);
247 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
248 u32 mask = BIT(line);
250 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
253 static int atmel_pio4_set_value(struct udevice *dev,
254 unsigned offset, int value)
256 u32 bank = ATMEL_PIO_BANK(offset);
257 u32 line = ATMEL_PIO_LINE(offset);
258 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
259 u32 mask = BIT(line);
262 writel(mask, &port_base->sodr);
264 writel(mask, &port_base->codr);
269 static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
271 u32 bank = ATMEL_PIO_BANK(offset);
272 u32 line = ATMEL_PIO_LINE(offset);
273 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
274 u32 mask = BIT(line);
276 writel(mask, &port_base->mskr);
278 return (readl(&port_base->cfgr) &
279 ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
282 static const struct dm_gpio_ops atmel_pio4_ops = {
283 .direction_input = atmel_pio4_direction_input,
284 .direction_output = atmel_pio4_direction_output,
285 .get_value = atmel_pio4_get_value,
286 .set_value = atmel_pio4_set_value,
287 .get_function = atmel_pio4_get_function,
290 static int atmel_pio4_bind(struct udevice *dev)
292 return dm_scan_fdt_dev(dev);
295 static int atmel_pio4_probe(struct udevice *dev)
297 struct atmel_pio4_plat *plat = dev_get_plat(dev);
298 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
299 struct atmel_pioctrl_data *pioctrl_data;
301 fdt_addr_t addr_base;
305 ret = clk_get_by_index(dev, 0, &clk);
309 ret = clk_enable(&clk);
315 addr_base = dev_read_addr(dev);
316 if (addr_base == FDT_ADDR_T_NONE)
319 plat->reg_base = (struct atmel_pio4_port *)addr_base;
321 pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
322 nbanks = pioctrl_data->nbanks;
324 uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
326 uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
328 /* if last bank has limited number of pins, adjust accordingly */
329 if (pioctrl_data->last_bank_count != ATMEL_PIO_NPINS_PER_BANK) {
330 uc_priv->gpio_count -= ATMEL_PIO_NPINS_PER_BANK;
331 uc_priv->gpio_count += pioctrl_data->last_bank_count;
338 * The number of banks can be different from a SoC to another one.
339 * We can have up to 16 banks.
341 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
343 .last_bank_count = ATMEL_PIO_NPINS_PER_BANK,
346 static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data = {
348 .last_bank_count = 8, /* 5th bank has only 8 lines on sama7g5 */
351 static const struct udevice_id atmel_pio4_ids[] = {
353 .compatible = "atmel,sama5d2-gpio",
354 .data = (ulong)&atmel_sama5d2_pioctrl_data,
356 .compatible = "microchip,sama7g5-gpio",
357 .data = (ulong)µchip_sama7g5_pioctrl_data,
362 U_BOOT_DRIVER(gpio_atmel_pio4) = {
363 .name = "gpio_atmel_pio4",
365 .ops = &atmel_pio4_ops,
366 .probe = atmel_pio4_probe,
367 .bind = atmel_pio4_bind,
368 .of_match = atmel_pio4_ids,
369 .plat_auto = sizeof(struct atmel_pio4_plat),