1 // SPDX-License-Identifier: GPL-2.0+
3 * Atmel PIO4 device driver
5 * Copyright (C) 2015 Atmel Corporation
6 * Wenyou.Yang <wenyou.yang@atmel.com>
13 #include <asm/arch/hardware.h>
15 #include <mach/gpio.h>
16 #include <mach/atmel_pio4.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
22 struct atmel_pio4_port *base = NULL;
26 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
29 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
32 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
35 base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
38 printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
46 static int atmel_pio4_config_io_func(u32 port, u32 pin,
49 struct atmel_pio4_port *port_base;
52 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
55 port_base = atmel_pio4_port_base(port);
63 writel(mask, &port_base->mskr);
64 writel(reg, &port_base->cfgr);
69 int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config)
71 return atmel_pio4_config_io_func(port, pin,
72 ATMEL_PIO_CFGR_FUNC_GPIO,
76 int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config)
78 return atmel_pio4_config_io_func(port, pin,
79 ATMEL_PIO_CFGR_FUNC_PERIPH_A,
83 int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config)
85 return atmel_pio4_config_io_func(port, pin,
86 ATMEL_PIO_CFGR_FUNC_PERIPH_B,
90 int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config)
92 return atmel_pio4_config_io_func(port, pin,
93 ATMEL_PIO_CFGR_FUNC_PERIPH_C,
97 int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config)
99 return atmel_pio4_config_io_func(port, pin,
100 ATMEL_PIO_CFGR_FUNC_PERIPH_D,
104 int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config)
106 return atmel_pio4_config_io_func(port, pin,
107 ATMEL_PIO_CFGR_FUNC_PERIPH_E,
111 int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config)
113 return atmel_pio4_config_io_func(port, pin,
114 ATMEL_PIO_CFGR_FUNC_PERIPH_F,
118 int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config)
120 return atmel_pio4_config_io_func(port, pin,
121 ATMEL_PIO_CFGR_FUNC_PERIPH_G,
125 int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
127 struct atmel_pio4_port *port_base;
130 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
133 port_base = atmel_pio4_port_base(port);
138 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
140 writel(mask, &port_base->mskr);
141 writel(reg, &port_base->cfgr);
144 writel(mask, &port_base->sodr);
146 writel(mask, &port_base->codr);
151 int atmel_pio4_get_pio_input(u32 port, u32 pin)
153 struct atmel_pio4_port *port_base;
156 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
159 port_base = atmel_pio4_port_base(port);
164 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
166 writel(mask, &port_base->mskr);
167 writel(reg, &port_base->cfgr);
169 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
172 #if CONFIG_IS_ENABLED(DM_GPIO)
174 struct atmel_pioctrl_data {
178 struct atmel_pio4_platdata {
179 struct atmel_pio4_port *reg_base;
182 static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
185 struct atmel_pio4_platdata *plat = dev_get_platdata(dev);
186 struct atmel_pio4_port *port_base =
187 (struct atmel_pio4_port *)((u32)plat->reg_base +
188 ATMEL_PIO_BANK_OFFSET * bank);
193 static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
195 u32 bank = ATMEL_PIO_BANK(offset);
196 u32 line = ATMEL_PIO_LINE(offset);
197 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
198 u32 mask = BIT(line);
200 writel(mask, &port_base->mskr);
202 clrbits_le32(&port_base->cfgr,
203 ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
208 static int atmel_pio4_direction_output(struct udevice *dev,
209 unsigned offset, int value)
211 u32 bank = ATMEL_PIO_BANK(offset);
212 u32 line = ATMEL_PIO_LINE(offset);
213 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
214 u32 mask = BIT(line);
216 writel(mask, &port_base->mskr);
218 clrsetbits_le32(&port_base->cfgr,
219 ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
222 writel(mask, &port_base->sodr);
224 writel(mask, &port_base->codr);
229 static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
231 u32 bank = ATMEL_PIO_BANK(offset);
232 u32 line = ATMEL_PIO_LINE(offset);
233 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
234 u32 mask = BIT(line);
236 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
239 static int atmel_pio4_set_value(struct udevice *dev,
240 unsigned offset, int value)
242 u32 bank = ATMEL_PIO_BANK(offset);
243 u32 line = ATMEL_PIO_LINE(offset);
244 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
245 u32 mask = BIT(line);
248 writel(mask, &port_base->sodr);
250 writel(mask, &port_base->codr);
255 static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
257 u32 bank = ATMEL_PIO_BANK(offset);
258 u32 line = ATMEL_PIO_LINE(offset);
259 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
260 u32 mask = BIT(line);
262 writel(mask, &port_base->mskr);
264 return (readl(&port_base->cfgr) &
265 ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
268 static const struct dm_gpio_ops atmel_pio4_ops = {
269 .direction_input = atmel_pio4_direction_input,
270 .direction_output = atmel_pio4_direction_output,
271 .get_value = atmel_pio4_get_value,
272 .set_value = atmel_pio4_set_value,
273 .get_function = atmel_pio4_get_function,
276 static int atmel_pio4_bind(struct udevice *dev)
278 return dm_scan_fdt_dev(dev);
281 static int atmel_pio4_probe(struct udevice *dev)
283 struct atmel_pio4_platdata *plat = dev_get_platdata(dev);
284 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
285 struct atmel_pioctrl_data *pioctrl_data;
287 fdt_addr_t addr_base;
291 ret = clk_get_by_index(dev, 0, &clk);
295 ret = clk_enable(&clk);
301 addr_base = devfdt_get_addr(dev);
302 if (addr_base == FDT_ADDR_T_NONE)
305 plat->reg_base = (struct atmel_pio4_port *)addr_base;
307 pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
308 nbanks = pioctrl_data->nbanks;
310 uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
312 uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
318 * The number of banks can be different from a SoC to another one.
319 * We can have up to 16 banks.
321 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
325 static const struct udevice_id atmel_pio4_ids[] = {
327 .compatible = "atmel,sama5d2-gpio",
328 .data = (ulong)&atmel_sama5d2_pioctrl_data,
333 U_BOOT_DRIVER(gpio_atmel_pio4) = {
334 .name = "gpio_atmel_pio4",
336 .ops = &atmel_pio4_ops,
337 .probe = atmel_pio4_probe,
338 .bind = atmel_pio4_bind,
339 .of_match = atmel_pio4_ids,
340 .platdata_auto_alloc_size = sizeof(struct atmel_pio4_platdata),