1 /*
2 * Copyright (c) 2022 SEAL AG
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nuvoton_numicro_pinctrl
8
9 #include <stdint.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/pinctrl.h>
12 #include <zephyr/dt-bindings/pinctrl/numicro-pinctrl.h>
13 #include <NuMicro.h>
14
15 #define MODE_PIN_SHIFT(pin) ((pin) * 2)
16 #define MODE_MASK(pin) (3 << MODE_PIN_SHIFT(pin))
17 #define DINOFF_PIN_SHIFT(pin) ((pin) + 16)
18 #define DINOFF_MASK(pin) (1 << DINOFF_PIN_SHIFT(pin))
19 #define PUSEL_PIN_SHIFT(pin) ((pin) * 2)
20 #define PUSEL_MASK(pin) (3 << PUSEL_PIN_SHIFT(pin))
21 #define SLEWCTL_PIN_SHIFT(pin) ((pin) * 2)
22 #define SLEWCTL_MASK(pin) (3 << SLEWCTL_PIN_SHIFT(pin))
23
24 #define PORT_PIN_MASK 0xFFFF
25
26 #define REG_MFP(port, pin) (*(volatile uint32_t *)((uint32_t)DT_INST_REG_ADDR_BY_NAME(0, mfp) + \
27 ((port) * 8) + \
28 ((pin) > 7 ? 4 : 0)))
29
30 #define REG_MFOS(port) (*(volatile uint32_t *)((uint32_t)DT_INST_REG_ADDR_BY_NAME(0, mfos) + \
31 ((port) * 4)))
32
33 #define MFP_CTL(pin, mfp) ((mfp) << (((pin) % 8) * 4))
34
35 /** Utility macro that expands to the GPIO port address if it exists */
36 #define NUMICRO_PORT_ADDR_OR_NONE(nodelabel) \
37 IF_ENABLED(DT_NODE_EXISTS(DT_NODELABEL(nodelabel)), \
38 (DT_REG_ADDR(DT_NODELABEL(nodelabel)),))
39
40 /** Port addresses */
41 static const uint32_t gpio_port_addrs[] = {
42 NUMICRO_PORT_ADDR_OR_NONE(gpioa)
43 NUMICRO_PORT_ADDR_OR_NONE(gpiob)
44 NUMICRO_PORT_ADDR_OR_NONE(gpioc)
45 NUMICRO_PORT_ADDR_OR_NONE(gpiod)
46 NUMICRO_PORT_ADDR_OR_NONE(gpioe)
47 NUMICRO_PORT_ADDR_OR_NONE(gpiof)
48 NUMICRO_PORT_ADDR_OR_NONE(gpiog)
49 NUMICRO_PORT_ADDR_OR_NONE(gpioh)
50 };
51
gpio_configure(const pinctrl_soc_pin_t * pin)52 static int gpio_configure(const pinctrl_soc_pin_t *pin)
53 {
54 uint8_t port_idx, pin_idx;
55 GPIO_T *port;
56 uint32_t bias = GPIO_PUSEL_DISABLE;
57
58 port_idx = NUMICRO_PORT(pin->pinmux);
59 if (port_idx >= ARRAY_SIZE(gpio_port_addrs)) {
60 return -EINVAL;
61 }
62
63 pin_idx = NUMICRO_PIN(pin->pinmux);
64
65 port = (GPIO_T *)gpio_port_addrs[port_idx];
66
67 if (pin->pull_up != 0) {
68 bias = GPIO_PUSEL_PULL_UP;
69 } else if (pin->pull_down != 0) {
70 bias = GPIO_PUSEL_PULL_DOWN;
71 }
72
73 port->MODE = (port->MODE & ~MODE_MASK(pin_idx)) |
74 ((pin->open_drain ? 2 : 0) << MODE_PIN_SHIFT(pin_idx));
75 port->DBEN = (port->DBEN & ~BIT(pin_idx)) |
76 ((pin->input_debounce ? 1 : 0) << pin_idx);
77 port->SMTEN = (port->SMTEN & ~BIT(pin_idx)) |
78 ((pin->schmitt_trigger ? 1 : 0) << pin_idx);
79 port->DINOFF = (port->SMTEN & ~DINOFF_MASK(pin_idx)) |
80 ((pin->input_disable ? 1 : 0) << DINOFF_PIN_SHIFT(pin_idx));
81 port->PUSEL = (port->PUSEL & ~PUSEL_MASK(pin_idx)) |
82 (bias << PUSEL_PIN_SHIFT(pin_idx));
83 port->SLEWCTL = (port->SLEWCTL & ~SLEWCTL_MASK(pin_idx)) |
84 (pin->slew_rate << SLEWCTL_PIN_SHIFT(pin_idx));
85
86 return 0;
87 }
88
pinctrl_configure_pins(const pinctrl_soc_pin_t * pins,uint8_t pin_cnt,uintptr_t reg)89 int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt,
90 uintptr_t reg)
91 {
92 int ret = 0;
93
94 for (uint8_t i = 0U; i < pin_cnt; i++) {
95 uint32_t port = NUMICRO_PORT(pins[i].pinmux);
96 uint32_t pin = NUMICRO_PIN(pins[i].pinmux);
97 uint32_t mfp = NUMICRO_MFP(pins[i].pinmux);
98
99 REG_MFP(port, pin) = (REG_MFP(port, pin) & ~MFP_CTL(pin, 0xf)) |
100 MFP_CTL(pin, mfp);
101
102 if (pins[i].open_drain != 0) {
103 REG_MFOS(port) |= BIT(pin);
104 } else {
105 REG_MFOS(port) &= ~BIT(pin);
106 }
107
108 ret = gpio_configure(&pins[i]);
109 if (ret != 0) {
110 return ret;
111 }
112 }
113
114 return 0;
115 }
116