blob: 27ee2a4bffdbc1d5db63d3e5a2d6b0531c99d574 [file] [log] [blame]
Vikas Manocha07e9e412017-02-12 10:25:49 -08001#include <common.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08002#include <dm.h>
3#include <dm/pinctrl.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -07004#include <asm/arch/gpio.h>
5#include <asm/gpio.h>
6#include <asm/io.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08007
8DECLARE_GLOBAL_DATA_PTR;
9
Vikas Manocha40ddb3a2017-04-10 15:03:04 -070010#define MAX_PINS_ONE_IP 70
Vikas Manochaec8630a2017-04-10 15:02:57 -070011#define MODE_BITS_MASK 3
12#define OSPEED_MASK 3
13#define PUPD_MASK 3
14#define OTYPE_MSK 1
15#define AFR_MASK 0xF
16
Patrice Chotardaaf68e82018-10-24 14:10:18 +020017#ifndef CONFIG_SPL_BUILD
18struct stm32_pinctrl_priv {
19 int pinctrl_ngpios;
20 struct list_head gpio_dev;
21};
22
23struct stm32_gpio_bank {
24 struct udevice *gpio_dev;
25 struct list_head list;
26};
27
28static int stm32_pinctrl_get_pins_count(struct udevice *dev)
29{
30 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
31 struct gpio_dev_priv *uc_priv;
32 struct stm32_gpio_bank *gpio_bank;
33
34 /*
35 * if get_pins_count has already been executed once on this
36 * pin-controller, no need to run it again
37 */
38 if (priv->pinctrl_ngpios)
39 return priv->pinctrl_ngpios;
40
41 /*
42 * walk through all banks to retrieve the pin-controller
43 * pins number
44 */
45 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
46 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
47
48 priv->pinctrl_ngpios += uc_priv->gpio_count;
49 }
50
51 return priv->pinctrl_ngpios;
52}
53
54int stm32_pinctrl_probe(struct udevice *dev)
55{
56 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
57 struct udevice *gpio_dev;
58 struct udevice *child;
59 struct stm32_gpio_bank *gpio_bank;
60 int ret;
61
62 INIT_LIST_HEAD(&priv->gpio_dev);
63
64 /*
65 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
66 * a list with all gpio device reference which belongs to the
67 * current pin-controller. This list is used to find pin_name and
68 * pin muxing
69 */
70 list_for_each_entry(child, &dev->child_head, sibling_node) {
71 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
72 &gpio_dev);
73 if (ret < 0)
74 continue;
75
76 gpio_bank = malloc(sizeof(*gpio_bank));
77 if (!gpio_bank) {
78 dev_err(dev, "Not enough memory\n");
79 return -ENOMEM;
80 }
81
82 gpio_bank->gpio_dev = gpio_dev;
83 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
84 }
85
86 return 0;
87}
88#endif
89
Vikas Manochaec8630a2017-04-10 15:02:57 -070090static int stm32_gpio_config(struct gpio_desc *desc,
91 const struct stm32_gpio_ctl *ctl)
92{
93 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
94 struct stm32_gpio_regs *regs = priv->regs;
95 u32 index;
96
97 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
98 ctl->pupd > 2 || ctl->speed > 3)
99 return -EINVAL;
100
101 index = (desc->offset & 0x07) * 4;
102 clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
103 ctl->af << index);
104
105 index = desc->offset * 2;
106 clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
107 ctl->mode << index);
108 clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
109 ctl->speed << index);
110 clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
111
112 index = desc->offset;
113 clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
114
115 return 0;
116}
Patrick Delaunayd252d752018-03-12 10:46:13 +0100117
Vikas Manocha07e9e412017-02-12 10:25:49 -0800118static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
119{
Patrick Delaunayd252d752018-03-12 10:46:13 +0100120 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800121 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
122 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
123 gpio_dsc->pin);
124
125 return 0;
126}
127
128static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
129{
130 gpio_fn &= 0x00FF;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700131 gpio_ctl->af = 0;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800132
133 switch (gpio_fn) {
134 case 0:
135 gpio_ctl->mode = STM32_GPIO_MODE_IN;
136 break;
137 case 1 ... 16:
138 gpio_ctl->mode = STM32_GPIO_MODE_AF;
139 gpio_ctl->af = gpio_fn - 1;
140 break;
141 case 17:
142 gpio_ctl->mode = STM32_GPIO_MODE_AN;
143 break;
144 default:
145 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
146 break;
147 }
148
149 gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
150
151 if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
152 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
153 else
154 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
155
156 if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
157 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
158 else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
159 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
160 else
161 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
162
163 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
164 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
165 gpio_ctl->pupd);
166
167 return 0;
168}
169
Christophe Kerelloa466d212017-06-20 17:04:18 +0200170static int stm32_pinctrl_config(int offset)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800171{
Vikas Manocha40ddb3a2017-04-10 15:03:04 -0700172 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha07e9e412017-02-12 10:25:49 -0800173 int rv, len;
174
Vikas Manocha07e9e412017-02-12 10:25:49 -0800175 /*
176 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
177 * usart1) of pin controller phandle "pinctrl-0"
178 * */
Christophe Kerelloa466d212017-06-20 17:04:18 +0200179 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
Vikas Manocha07e9e412017-02-12 10:25:49 -0800180 struct stm32_gpio_dsc gpio_dsc;
181 struct stm32_gpio_ctl gpio_ctl;
182 int i;
183
Christophe Kerelloa466d212017-06-20 17:04:18 +0200184 len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
Vikas Manocha07e9e412017-02-12 10:25:49 -0800185 "pinmux", pin_mux,
186 ARRAY_SIZE(pin_mux));
Christophe Kerelloa466d212017-06-20 17:04:18 +0200187 debug("%s: no of pinmux entries= %d\n", __func__, len);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800188 if (len < 0)
189 return -EINVAL;
190 for (i = 0; i < len; i++) {
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700191 struct gpio_desc desc;
Patrick Delaunayd252d752018-03-12 10:46:13 +0100192
Vikas Manocha07e9e412017-02-12 10:25:49 -0800193 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
194 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Christophe Kerelloa466d212017-06-20 17:04:18 +0200195 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700196 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunayd252d752018-03-12 10:46:13 +0100197 gpio_dsc.port,
198 &desc.dev);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700199 if (rv)
200 return rv;
201 desc.offset = gpio_dsc.pin;
202 rv = stm32_gpio_config(&desc, &gpio_ctl);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800203 debug("%s: rv = %d\n\n", __func__, rv);
204 if (rv)
205 return rv;
206 }
Christophe Kerelloa466d212017-06-20 17:04:18 +0200207 }
208
209 return 0;
210}
211
Christophe Kerellod6661552017-06-20 17:04:19 +0200212#if CONFIG_IS_ENABLED(PINCTRL_FULL)
213static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
214{
215 return stm32_pinctrl_config(dev_of_offset(config));
216}
217#else /* PINCTRL_FULL */
Christophe Kerelloa466d212017-06-20 17:04:18 +0200218static int stm32_pinctrl_set_state_simple(struct udevice *dev,
219 struct udevice *periph)
220{
221 const void *fdt = gd->fdt_blob;
222 const fdt32_t *list;
223 uint32_t phandle;
224 int config_node;
225 int size, i, ret;
226
227 list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
228 if (!list)
229 return -EINVAL;
230
231 debug("%s: periph->name = %s\n", __func__, periph->name);
232
233 size /= sizeof(*list);
234 for (i = 0; i < size; i++) {
235 phandle = fdt32_to_cpu(*list++);
236
237 config_node = fdt_node_offset_by_phandle(fdt, phandle);
238 if (config_node < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900239 pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200240 return -EINVAL;
241 }
242
243 ret = stm32_pinctrl_config(config_node);
244 if (ret)
245 return ret;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800246 }
247
248 return 0;
249}
Christophe Kerellod6661552017-06-20 17:04:19 +0200250#endif /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800251
252static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellod6661552017-06-20 17:04:19 +0200253#if CONFIG_IS_ENABLED(PINCTRL_FULL)
254 .set_state = stm32_pinctrl_set_state,
255#else /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800256 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellod6661552017-06-20 17:04:19 +0200257#endif /* PINCTRL_FULL */
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200258#ifndef CONFIG_SPL_BUILD
259 .get_pins_count = stm32_pinctrl_get_pins_count,
260#endif
Vikas Manocha07e9e412017-02-12 10:25:49 -0800261};
262
263static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotardb5652b72017-12-12 09:49:35 +0100264 { .compatible = "st,stm32f429-pinctrl" },
265 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800266 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotard6502c472017-09-13 18:00:04 +0200267 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunayd252d752018-03-12 10:46:13 +0100268 { .compatible = "st,stm32mp157-pinctrl" },
269 { .compatible = "st,stm32mp157-z-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800270 { }
271};
272
273U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200274 .name = "pinctrl_stm32",
275 .id = UCLASS_PINCTRL,
276 .of_match = stm32_pinctrl_ids,
277 .ops = &stm32_pinctrl_ops,
278 .bind = dm_scan_fdt_dev,
279#ifndef CONFIG_SPL_BUILD
280 .probe = stm32_pinctrl_probe,
281 .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv),
282#endif
Vikas Manocha07e9e412017-02-12 10:25:49 -0800283};