1 // SPDX-License-Identifier: GPL-2.0+
3 * Atmel PIO pinctrl driver
5 * Copyright (C) 2016 Atmel Corporation
6 * Wenyou.Yang <wenyou.yang@atmel.com>
11 #include <dm/pinctrl.h>
12 #include <asm/hardware.h>
14 #include <linux/err.h>
15 #include <mach/at91_pio.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define MAX_GPIO_BANKS 5
20 #define MAX_NB_GPIO_PER_BANK 32
22 #define MAX_PINMUX_ENTRIES 200
24 struct at91_pinctrl_priv {
25 struct at91_port *reg_base[MAX_GPIO_BANKS];
29 #define PULL_UP BIT(0)
30 #define MULTI_DRIVE BIT(1)
31 #define DEGLITCH BIT(2)
32 #define PULL_DOWN BIT(3)
33 #define DIS_SCHMIT BIT(4)
34 #define DRIVE_STRENGTH_SHIFT 5
35 #define DRIVE_STRENGTH_MASK 0x3
36 #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
38 #define OUTPUT_VAL_SHIFT 8
39 #define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT)
40 #define DEBOUNCE BIT(16)
41 #define DEBOUNCE_VAL_SHIFT 17
42 #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
45 * These defines will translated the dt binding settings to our internal
46 * settings. They are not necessarily the same value as the register setting.
47 * The actual drive strength current of low, medium and high must be looked up
48 * from the corresponding device datasheet. This value is different for pins
49 * that are even in the same banks. It is also dependent on VCC.
50 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
51 * strength when there is no dt config for it.
53 enum drive_strength_bit {
54 DRIVE_STRENGTH_BIT_DEF,
55 DRIVE_STRENGTH_BIT_LOW,
56 DRIVE_STRENGTH_BIT_MED,
57 DRIVE_STRENGTH_BIT_HI,
60 #define DRIVE_STRENGTH_BIT_MSK(name) (DRIVE_STRENGTH_BIT_##name << \
65 AT91_MUX_PERIPH_A = 1,
66 AT91_MUX_PERIPH_B = 2,
67 AT91_MUX_PERIPH_C = 3,
68 AT91_MUX_PERIPH_D = 4,
72 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
73 * on new IP with support for periph C and D the way to mux in
74 * periph A and B has changed
75 * So provide the right callbacks
76 * if not present means the IP does not support it
77 * @mux_A_periph: assign the corresponding pin to the peripheral A function.
78 * @mux_B_periph: assign the corresponding pin to the peripheral B function.
79 * @mux_C_periph: assign the corresponding pin to the peripheral C function.
80 * @mux_D_periph: assign the corresponding pin to the peripheral D function.
81 * @set_deglitch: enable/disable the deglitch feature.
82 * @set_debounce: enable/disable the debounce feature.
83 * @set_pulldown: enable/disable the pulldown feature.
84 * @disable_schmitt_trig: disable schmitt trigger
86 struct at91_pinctrl_mux_ops {
87 void (*mux_A_periph)(struct at91_port *pio, u32 mask);
88 void (*mux_B_periph)(struct at91_port *pio, u32 mask);
89 void (*mux_C_periph)(struct at91_port *pio, u32 mask);
90 void (*mux_D_periph)(struct at91_port *pio, u32 mask);
91 void (*set_deglitch)(struct at91_port *pio, u32 mask, bool is_on);
92 void (*set_debounce)(struct at91_port *pio, u32 mask, bool is_on,
94 void (*set_pulldown)(struct at91_port *pio, u32 mask, bool is_on);
95 void (*disable_schmitt_trig)(struct at91_port *pio, u32 mask);
96 void (*set_drivestrength)(struct at91_port *pio, u32 pin,
100 static u32 two_bit_pin_value_shift_amount(u32 pin)
102 /* return the shift value for a pin for "two bit" per pin registers,
103 * i.e. drive strength */
104 return 2 * ((pin >= MAX_NB_GPIO_PER_BANK/2)
105 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
108 static void at91_mux_disable_interrupt(struct at91_port *pio, u32 mask)
110 writel(mask, &pio->idr);
113 static void at91_mux_set_pullup(struct at91_port *pio, u32 mask, bool on)
116 writel(mask, &pio->mux.pio3.ppddr);
118 writel(mask, (on ? &pio->puer : &pio->pudr));
121 static void at91_mux_set_output(struct at91_port *pio, unsigned mask,
122 bool is_on, bool val)
124 writel(mask, (val ? &pio->sodr : &pio->codr));
125 writel(mask, (is_on ? &pio->oer : &pio->odr));
128 static void at91_mux_set_multidrive(struct at91_port *pio, u32 mask, bool on)
130 writel(mask, (on ? &pio->mder : &pio->mddr));
133 static void at91_mux_set_A_periph(struct at91_port *pio, u32 mask)
135 writel(mask, &pio->mux.pio2.asr);
138 static void at91_mux_set_B_periph(struct at91_port *pio, u32 mask)
140 writel(mask, &pio->mux.pio2.bsr);
143 static void at91_mux_pio3_set_A_periph(struct at91_port *pio, u32 mask)
145 writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1);
146 writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2);
149 static void at91_mux_pio3_set_B_periph(struct at91_port *pio, u32 mask)
151 writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1);
152 writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2);
155 static void at91_mux_pio3_set_C_periph(struct at91_port *pio, u32 mask)
157 writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1);
158 writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2);
161 static void at91_mux_pio3_set_D_periph(struct at91_port *pio, u32 mask)
163 writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1);
164 writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2);
167 static void at91_mux_set_deglitch(struct at91_port *pio, u32 mask, bool is_on)
169 writel(mask, (is_on ? &pio->ifer : &pio->ifdr));
172 static void at91_mux_pio3_set_deglitch(struct at91_port *pio,
173 u32 mask, bool is_on)
176 writel(mask, &pio->mux.pio3.ifscdr);
177 at91_mux_set_deglitch(pio, mask, is_on);
180 static void at91_mux_pio3_set_debounce(struct at91_port *pio, u32 mask,
184 writel(mask, &pio->mux.pio3.ifscer);
185 writel(div & PIO_SCDR_DIV, &pio->mux.pio3.scdr);
186 writel(mask, &pio->ifer);
188 writel(mask, &pio->mux.pio3.ifscdr);
192 static void at91_mux_pio3_set_pulldown(struct at91_port *pio,
193 u32 mask, bool is_on)
196 writel(mask, &pio->pudr);
198 writel(mask, (is_on ? &pio->mux.pio3.ppder : &pio->mux.pio3.ppddr));
201 static void at91_mux_pio3_disable_schmitt_trig(struct at91_port *pio,
204 writel(readl(&pio->schmitt) | mask, &pio->schmitt);
207 static void set_drive_strength(void *reg, u32 pin, u32 strength)
209 u32 shift = two_bit_pin_value_shift_amount(pin);
211 clrsetbits_le32(reg, DRIVE_STRENGTH_MASK << shift, strength << shift);
214 static void at91_mux_sama5d3_set_drivestrength(struct at91_port *pio,
215 u32 pin, u32 setting)
219 reg = &pio->driver12;
220 if (pin >= MAX_NB_GPIO_PER_BANK / 2)
223 /* do nothing if setting is zero */
227 /* strength is 1 to 1 with setting for SAMA5 */
228 set_drive_strength(reg, pin, setting);
231 static void at91_mux_sam9x5_set_drivestrength(struct at91_port *pio,
232 u32 pin, u32 setting)
237 if (pin >= MAX_NB_GPIO_PER_BANK / 2)
238 reg = &pio->driver12;
240 /* do nothing if setting is zero */
244 /* strength is inverse on SAM9x5s with our defines
245 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
246 setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
248 set_drive_strength(reg, pin, setting);
251 static void at91_mux_sam9x60_set_drivestrength(struct at91_port *pio, u32 pin,
254 void *reg = &pio->driver12;
257 if (setting <= DRIVE_STRENGTH_BIT_DEF ||
258 setting == DRIVE_STRENGTH_BIT_MED ||
259 setting > DRIVE_STRENGTH_BIT_HI)
264 /* Strength is 0: low, 1: hi */
265 if (setting == DRIVE_STRENGTH_BIT_LOW)
273 static struct at91_pinctrl_mux_ops at91rm9200_ops = {
274 .mux_A_periph = at91_mux_set_A_periph,
275 .mux_B_periph = at91_mux_set_B_periph,
276 .set_deglitch = at91_mux_set_deglitch,
279 static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
280 .mux_A_periph = at91_mux_pio3_set_A_periph,
281 .mux_B_periph = at91_mux_pio3_set_B_periph,
282 .mux_C_periph = at91_mux_pio3_set_C_periph,
283 .mux_D_periph = at91_mux_pio3_set_D_periph,
284 .set_deglitch = at91_mux_pio3_set_deglitch,
285 .set_debounce = at91_mux_pio3_set_debounce,
286 .set_pulldown = at91_mux_pio3_set_pulldown,
287 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
288 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
291 static struct at91_pinctrl_mux_ops sama5d3_ops = {
292 .mux_A_periph = at91_mux_pio3_set_A_periph,
293 .mux_B_periph = at91_mux_pio3_set_B_periph,
294 .mux_C_periph = at91_mux_pio3_set_C_periph,
295 .mux_D_periph = at91_mux_pio3_set_D_periph,
296 .set_deglitch = at91_mux_pio3_set_deglitch,
297 .set_debounce = at91_mux_pio3_set_debounce,
298 .set_pulldown = at91_mux_pio3_set_pulldown,
299 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
300 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
303 static struct at91_pinctrl_mux_ops sam9x60_ops = {
304 .mux_A_periph = at91_mux_pio3_set_A_periph,
305 .mux_B_periph = at91_mux_pio3_set_B_periph,
306 .mux_C_periph = at91_mux_pio3_set_C_periph,
307 .mux_D_periph = at91_mux_pio3_set_D_periph,
308 .set_deglitch = at91_mux_pio3_set_deglitch,
309 .set_debounce = at91_mux_pio3_set_debounce,
310 .set_pulldown = at91_mux_pio3_set_pulldown,
311 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
312 .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
315 static void at91_mux_gpio_disable(struct at91_port *pio, u32 mask)
317 writel(mask, &pio->pdr);
320 static void at91_mux_gpio_enable(struct at91_port *pio, u32 mask, bool input)
322 writel(mask, &pio->per);
323 writel(mask, (input ? &pio->odr : &pio->oer));
326 static int at91_pmx_set(struct at91_pinctrl_mux_ops *ops,
327 struct at91_port *pio, u32 mask, enum at91_mux mux)
329 at91_mux_disable_interrupt(pio, mask);
332 at91_mux_gpio_enable(pio, mask, 1);
334 case AT91_MUX_PERIPH_A:
335 ops->mux_A_periph(pio, mask);
337 case AT91_MUX_PERIPH_B:
338 ops->mux_B_periph(pio, mask);
340 case AT91_MUX_PERIPH_C:
341 if (!ops->mux_C_periph)
343 ops->mux_C_periph(pio, mask);
345 case AT91_MUX_PERIPH_D:
346 if (!ops->mux_D_periph)
348 ops->mux_D_periph(pio, mask);
352 at91_mux_gpio_disable(pio, mask);
357 static int at91_pinconf_set(struct at91_pinctrl_mux_ops *ops,
358 struct at91_port *pio, u32 pin, u32 config)
362 if ((config & PULL_UP) && (config & PULL_DOWN))
365 at91_mux_set_output(pio, mask, config & OUTPUT,
366 (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
367 at91_mux_set_pullup(pio, mask, config & PULL_UP);
368 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
369 if (ops->set_deglitch)
370 ops->set_deglitch(pio, mask, config & DEGLITCH);
371 if (ops->set_debounce)
372 ops->set_debounce(pio, mask, config & DEBOUNCE,
373 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
374 if (ops->set_pulldown)
375 ops->set_pulldown(pio, mask, config & PULL_DOWN);
376 if (ops->disable_schmitt_trig && config & DIS_SCHMIT)
377 ops->disable_schmitt_trig(pio, mask);
378 if (ops->set_drivestrength)
379 ops->set_drivestrength(pio, pin,
380 (config & DRIVE_STRENGTH) >> DRIVE_STRENGTH_SHIFT);
385 static int at91_pin_check_config(struct udevice *dev, u32 bank, u32 pin)
387 struct at91_pinctrl_priv *priv = dev_get_priv(dev);
389 if (bank >= priv->nbanks) {
390 debug("pin conf bank %d >= nbanks %d\n", bank, priv->nbanks);
394 if (pin >= MAX_NB_GPIO_PER_BANK) {
395 debug("pin conf pin %d >= %d\n", pin, MAX_NB_GPIO_PER_BANK);
402 static int at91_pinctrl_set_state(struct udevice *dev, struct udevice *config)
404 struct at91_pinctrl_priv *priv = dev_get_priv(dev);
405 const void *blob = gd->fdt_blob;
406 int node = dev_of_offset(config);
407 u32 cells[MAX_PINMUX_ENTRIES];
408 const u32 *list = cells;
410 u32 conf, mask, count, i;
414 struct at91_port *pio;
415 struct at91_pinctrl_mux_ops *ops =
416 (struct at91_pinctrl_mux_ops *)dev_get_driver_data(dev);
419 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
420 * do sanity check and calculate pins number
422 size = fdtdec_get_int_array_count(blob, node, "atmel,pins",
423 cells, ARRAY_SIZE(cells));
425 /* we do not check return since it's safe node passed down */
430 for (i = 0; i < count; i++) {
436 ret = at91_pin_check_config(dev, bank, pin);
440 pio = priv->reg_base[bank];
443 ret = at91_pmx_set(ops, pio, mask, mux);
447 ret = at91_pinconf_set(ops, pio, pin, conf);
455 const struct pinctrl_ops at91_pinctrl_ops = {
456 .set_state = at91_pinctrl_set_state,
459 static int at91_pinctrl_probe(struct udevice *dev)
461 struct at91_pinctrl_priv *priv = dev_get_priv(dev);
462 fdt_addr_t addr_base;
465 for (index = 0; index < MAX_GPIO_BANKS; index++) {
466 addr_base = devfdt_get_addr_index(dev, index);
467 if (addr_base == FDT_ADDR_T_NONE)
470 priv->reg_base[index] = (struct at91_port *)addr_base;
473 priv->nbanks = index;
478 static const struct udevice_id at91_pinctrl_match[] = {
479 { .compatible = "atmel,sama5d3-pinctrl", .data = (ulong)&sama5d3_ops },
480 { .compatible = "atmel,at91sam9x5-pinctrl", .data = (ulong)&at91sam9x5_ops },
481 { .compatible = "atmel,at91rm9200-pinctrl", .data = (ulong)&at91rm9200_ops },
482 { .compatible = "microchip,sam9x60-pinctrl", .data = (ulong)&sam9x60_ops },
486 U_BOOT_DRIVER(at91_pinctrl) = {
487 .name = "pinctrl_at91",
488 .id = UCLASS_PINCTRL,
489 .of_match = at91_pinctrl_match,
490 .probe = at91_pinctrl_probe,
491 .priv_auto_alloc_size = sizeof(struct at91_pinctrl_priv),
492 .ops = &at91_pinctrl_ops,