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>
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;
40 printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
48 static int atmel_pio4_config_io_func(u32 port, u32 pin,
51 struct atmel_pio4_port *port_base;
54 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
57 port_base = atmel_pio4_port_base(port);
65 writel(mask, &port_base->mskr);
66 writel(reg, &port_base->cfgr);
71 int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config)
73 return atmel_pio4_config_io_func(port, pin,
74 ATMEL_PIO_CFGR_FUNC_GPIO,
78 int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config)
80 return atmel_pio4_config_io_func(port, pin,
81 ATMEL_PIO_CFGR_FUNC_PERIPH_A,
85 int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config)
87 return atmel_pio4_config_io_func(port, pin,
88 ATMEL_PIO_CFGR_FUNC_PERIPH_B,
92 int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config)
94 return atmel_pio4_config_io_func(port, pin,
95 ATMEL_PIO_CFGR_FUNC_PERIPH_C,
99 int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config)
101 return atmel_pio4_config_io_func(port, pin,
102 ATMEL_PIO_CFGR_FUNC_PERIPH_D,
106 int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config)
108 return atmel_pio4_config_io_func(port, pin,
109 ATMEL_PIO_CFGR_FUNC_PERIPH_E,
113 int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config)
115 return atmel_pio4_config_io_func(port, pin,
116 ATMEL_PIO_CFGR_FUNC_PERIPH_F,
120 int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config)
122 return atmel_pio4_config_io_func(port, pin,
123 ATMEL_PIO_CFGR_FUNC_PERIPH_G,
127 int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
129 struct atmel_pio4_port *port_base;
132 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
135 port_base = atmel_pio4_port_base(port);
140 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
142 writel(mask, &port_base->mskr);
143 writel(reg, &port_base->cfgr);
146 writel(mask, &port_base->sodr);
148 writel(mask, &port_base->codr);
153 int atmel_pio4_get_pio_input(u32 port, u32 pin)
155 struct atmel_pio4_port *port_base;
158 if (pin >= ATMEL_PIO_NPINS_PER_BANK)
161 port_base = atmel_pio4_port_base(port);
166 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
168 writel(mask, &port_base->mskr);
169 writel(reg, &port_base->cfgr);
171 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
174 #if CONFIG_IS_ENABLED(DM_GPIO)
177 * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct
178 * @nbanks: number of PIO banks
179 * @last_bank_count: number of lines in the last bank (can be less than
180 * the rest of the banks).
182 struct atmel_pioctrl_data {
187 struct atmel_pio4_plat {
188 struct atmel_pio4_port *reg_base;
191 static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
194 struct atmel_pio4_plat *plat = dev_get_plat(dev);
195 struct atmel_pio4_port *port_base =
196 (struct atmel_pio4_port *)((u32)plat->reg_base +
197 ATMEL_PIO_BANK_OFFSET * bank);
202 static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
204 u32 bank = ATMEL_PIO_BANK(offset);
205 u32 line = ATMEL_PIO_LINE(offset);
206 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
207 u32 mask = BIT(line);
209 writel(mask, &port_base->mskr);
211 clrbits_le32(&port_base->cfgr,
212 ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
217 static int atmel_pio4_direction_output(struct udevice *dev,
218 unsigned offset, int value)
220 u32 bank = ATMEL_PIO_BANK(offset);
221 u32 line = ATMEL_PIO_LINE(offset);
222 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
223 u32 mask = BIT(line);
225 writel(mask, &port_base->mskr);
227 clrsetbits_le32(&port_base->cfgr,
228 ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
231 writel(mask, &port_base->sodr);
233 writel(mask, &port_base->codr);
238 static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
240 u32 bank = ATMEL_PIO_BANK(offset);
241 u32 line = ATMEL_PIO_LINE(offset);
242 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
243 u32 mask = BIT(line);
245 return (readl(&port_base->pdsr) & mask) ? 1 : 0;
248 static int atmel_pio4_set_value(struct udevice *dev,
249 unsigned offset, int value)
251 u32 bank = ATMEL_PIO_BANK(offset);
252 u32 line = ATMEL_PIO_LINE(offset);
253 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
254 u32 mask = BIT(line);
257 writel(mask, &port_base->sodr);
259 writel(mask, &port_base->codr);
264 static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
266 u32 bank = ATMEL_PIO_BANK(offset);
267 u32 line = ATMEL_PIO_LINE(offset);
268 struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
269 u32 mask = BIT(line);
271 writel(mask, &port_base->mskr);
273 return (readl(&port_base->cfgr) &
274 ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
277 static const struct dm_gpio_ops atmel_pio4_ops = {
278 .direction_input = atmel_pio4_direction_input,
279 .direction_output = atmel_pio4_direction_output,
280 .get_value = atmel_pio4_get_value,
281 .set_value = atmel_pio4_set_value,
282 .get_function = atmel_pio4_get_function,
285 static int atmel_pio4_bind(struct udevice *dev)
287 return dm_scan_fdt_dev(dev);
290 static int atmel_pio4_probe(struct udevice *dev)
292 struct atmel_pio4_plat *plat = dev_get_plat(dev);
293 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
294 struct atmel_pioctrl_data *pioctrl_data;
296 fdt_addr_t addr_base;
300 ret = clk_get_by_index(dev, 0, &clk);
304 ret = clk_enable(&clk);
310 addr_base = dev_read_addr(dev);
311 if (addr_base == FDT_ADDR_T_NONE)
314 plat->reg_base = (struct atmel_pio4_port *)addr_base;
316 pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
317 nbanks = pioctrl_data->nbanks;
319 uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
321 uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
323 /* if last bank has limited number of pins, adjust accordingly */
324 if (pioctrl_data->last_bank_count != ATMEL_PIO_NPINS_PER_BANK) {
325 uc_priv->gpio_count -= ATMEL_PIO_NPINS_PER_BANK;
326 uc_priv->gpio_count += pioctrl_data->last_bank_count;
333 * The number of banks can be different from a SoC to another one.
334 * We can have up to 16 banks.
336 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
338 .last_bank_count = ATMEL_PIO_NPINS_PER_BANK,
341 static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data = {
343 .last_bank_count = 8, /* 5th bank has only 8 lines on sama7g5 */
346 static const struct udevice_id atmel_pio4_ids[] = {
348 .compatible = "atmel,sama5d2-gpio",
349 .data = (ulong)&atmel_sama5d2_pioctrl_data,
351 .compatible = "microchip,sama7g5-gpio",
352 .data = (ulong)µchip_sama7g5_pioctrl_data,
357 U_BOOT_DRIVER(gpio_atmel_pio4) = {
358 .name = "gpio_atmel_pio4",
360 .ops = &atmel_pio4_ops,
361 .probe = atmel_pio4_probe,
362 .bind = atmel_pio4_bind,
363 .of_match = atmel_pio4_ids,
364 .plat_auto = sizeof(struct atmel_pio4_plat),