blob: 6d4117d941f6ec4c4d8035334e9d2dbe83496232 [file] [log] [blame]
Vikas Manocha94d53082017-02-12 10:25:49 -08001#include <common.h>
Vikas Manocha94d53082017-02-12 10:25:49 -08002#include <dm.h>
3#include <dm/pinctrl.h>
Vikas Manocha77417102017-04-10 15:02:57 -07004#include <asm/arch/gpio.h>
5#include <asm/gpio.h>
6#include <asm/io.h>
Vikas Manocha94d53082017-02-12 10:25:49 -08007
8DECLARE_GLOBAL_DATA_PTR;
9
Vikas Manocha58fb3c82017-04-10 15:03:04 -070010#define MAX_PINS_ONE_IP 70
Vikas Manocha77417102017-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 Chotard8f651ca2018-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
Patrice Chotard4ff1c202018-10-24 14:10:19 +020028#define MAX_PIN_PER_BANK 16
29
30static char pin_name[PINNAME_SIZE];
Patrice Chotardb42d9382018-10-24 14:10:20 +020031#define PINMUX_MODE_COUNT 5
32static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
33 "gpio input",
34 "gpio output",
35 "analog",
36 "unknown",
37 "alt function",
38};
39
40static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
41{
42 struct stm32_gpio_priv *priv = dev_get_priv(dev);
43 struct stm32_gpio_regs *regs = priv->regs;
44 u32 af;
45 u32 alt_shift = (offset % 8) * 4;
46 u32 alt_index = offset / 8;
47
48 af = (readl(&regs->afr[alt_index]) &
49 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
50
51 return af;
52}
53
Patrice Chotard8f651ca2018-10-24 14:10:18 +020054static int stm32_pinctrl_get_pins_count(struct udevice *dev)
55{
56 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
57 struct gpio_dev_priv *uc_priv;
58 struct stm32_gpio_bank *gpio_bank;
59
60 /*
61 * if get_pins_count has already been executed once on this
62 * pin-controller, no need to run it again
63 */
64 if (priv->pinctrl_ngpios)
65 return priv->pinctrl_ngpios;
66
67 /*
68 * walk through all banks to retrieve the pin-controller
69 * pins number
70 */
71 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
72 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
73
74 priv->pinctrl_ngpios += uc_priv->gpio_count;
75 }
76
77 return priv->pinctrl_ngpios;
78}
79
Patrice Chotard4ff1c202018-10-24 14:10:19 +020080static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
81 unsigned int selector)
82{
83 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
84 struct stm32_gpio_bank *gpio_bank;
85 struct gpio_dev_priv *uc_priv;
86 int first_pin = 0;
87
88 /* look up for the bank which owns the requested pin */
89 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
90 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
91
92 if (selector < (first_pin + uc_priv->gpio_count))
93 /* we found the bank */
94 return gpio_bank->gpio_dev;
95
96 first_pin += uc_priv->gpio_count;
97 }
98
99 return NULL;
100}
101
102static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
103 unsigned int selector)
104{
105 struct gpio_dev_priv *uc_priv;
106 struct udevice *gpio_dev;
107
108 /* look up for the bank which owns the requested pin */
109 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector);
110 if (!gpio_dev) {
111 snprintf(pin_name, PINNAME_SIZE, "Error");
112 } else {
113 uc_priv = dev_get_uclass_priv(gpio_dev);
114
115 snprintf(pin_name, PINNAME_SIZE, "%s%d",
116 uc_priv->bank_name,
117 selector % MAX_PIN_PER_BANK);
118 }
119
120 return pin_name;
121}
Patrice Chotardb42d9382018-10-24 14:10:20 +0200122
123static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
124 unsigned int selector,
125 char *buf,
126 int size)
127{
128 struct udevice *gpio_dev;
129 const char *label;
130 int gpio_pin;
131 int mode;
132 int af_num;
133
134 /* look up for the bank which owns the requested pin */
135 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector);
136
137 if (!gpio_dev)
138 return -ENODEV;
139
140 /* translate pin-controller pin number to gpio pin number */
141 gpio_pin = selector % MAX_PIN_PER_BANK;
142
143 mode = gpio_get_raw_function(gpio_dev, gpio_pin, &label);
144
145 dev_dbg(dev, "selector = %d gpio_pin = %d mode = %d\n",
146 selector, gpio_pin, mode);
147
148 switch (mode) {
149 case GPIOF_UNKNOWN:
150 /* should never happen */
151 return -EINVAL;
152 case GPIOF_UNUSED:
153 snprintf(buf, size, "%s", pinmux_mode[mode]);
154 break;
155 case GPIOF_FUNC:
156 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_pin);
157 snprintf(buf, size, "%s %d", pinmux_mode[mode], af_num);
158 break;
159 case GPIOF_OUTPUT:
160 case GPIOF_INPUT:
161 snprintf(buf, size, "%s %s",
162 pinmux_mode[mode], label ? label : "");
163 break;
164 }
165
166 return 0;
167}
168
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200169int stm32_pinctrl_probe(struct udevice *dev)
170{
171 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
172 struct udevice *gpio_dev;
173 struct udevice *child;
174 struct stm32_gpio_bank *gpio_bank;
175 int ret;
176
177 INIT_LIST_HEAD(&priv->gpio_dev);
178
179 /*
180 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
181 * a list with all gpio device reference which belongs to the
182 * current pin-controller. This list is used to find pin_name and
183 * pin muxing
184 */
185 list_for_each_entry(child, &dev->child_head, sibling_node) {
186 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
187 &gpio_dev);
188 if (ret < 0)
189 continue;
190
191 gpio_bank = malloc(sizeof(*gpio_bank));
192 if (!gpio_bank) {
193 dev_err(dev, "Not enough memory\n");
194 return -ENOMEM;
195 }
196
197 gpio_bank->gpio_dev = gpio_dev;
198 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
199 }
200
201 return 0;
202}
203#endif
204
Vikas Manocha77417102017-04-10 15:02:57 -0700205static int stm32_gpio_config(struct gpio_desc *desc,
206 const struct stm32_gpio_ctl *ctl)
207{
208 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
209 struct stm32_gpio_regs *regs = priv->regs;
210 u32 index;
211
212 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
213 ctl->pupd > 2 || ctl->speed > 3)
214 return -EINVAL;
215
216 index = (desc->offset & 0x07) * 4;
217 clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
218 ctl->af << index);
219
220 index = desc->offset * 2;
221 clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
222 ctl->mode << index);
223 clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
224 ctl->speed << index);
225 clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
226
227 index = desc->offset;
228 clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
229
230 return 0;
231}
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100232
Vikas Manocha94d53082017-02-12 10:25:49 -0800233static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
234{
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100235 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha94d53082017-02-12 10:25:49 -0800236 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
237 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
238 gpio_dsc->pin);
239
240 return 0;
241}
242
243static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
244{
245 gpio_fn &= 0x00FF;
Vikas Manocha77417102017-04-10 15:02:57 -0700246 gpio_ctl->af = 0;
Vikas Manocha94d53082017-02-12 10:25:49 -0800247
248 switch (gpio_fn) {
249 case 0:
250 gpio_ctl->mode = STM32_GPIO_MODE_IN;
251 break;
252 case 1 ... 16:
253 gpio_ctl->mode = STM32_GPIO_MODE_AF;
254 gpio_ctl->af = gpio_fn - 1;
255 break;
256 case 17:
257 gpio_ctl->mode = STM32_GPIO_MODE_AN;
258 break;
259 default:
260 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
261 break;
262 }
263
264 gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
265
266 if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
267 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
268 else
269 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
270
271 if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
272 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
273 else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
274 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
275 else
276 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
277
278 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
279 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
280 gpio_ctl->pupd);
281
282 return 0;
283}
284
Christophe Kerelload0376e2017-06-20 17:04:18 +0200285static int stm32_pinctrl_config(int offset)
Vikas Manocha94d53082017-02-12 10:25:49 -0800286{
Vikas Manocha58fb3c82017-04-10 15:03:04 -0700287 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha94d53082017-02-12 10:25:49 -0800288 int rv, len;
289
Vikas Manocha94d53082017-02-12 10:25:49 -0800290 /*
291 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
292 * usart1) of pin controller phandle "pinctrl-0"
293 * */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200294 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
Vikas Manocha94d53082017-02-12 10:25:49 -0800295 struct stm32_gpio_dsc gpio_dsc;
296 struct stm32_gpio_ctl gpio_ctl;
297 int i;
298
Christophe Kerelload0376e2017-06-20 17:04:18 +0200299 len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
Vikas Manocha94d53082017-02-12 10:25:49 -0800300 "pinmux", pin_mux,
301 ARRAY_SIZE(pin_mux));
Christophe Kerelload0376e2017-06-20 17:04:18 +0200302 debug("%s: no of pinmux entries= %d\n", __func__, len);
Vikas Manocha94d53082017-02-12 10:25:49 -0800303 if (len < 0)
304 return -EINVAL;
305 for (i = 0; i < len; i++) {
Vikas Manocha280057b2017-04-10 15:02:59 -0700306 struct gpio_desc desc;
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100307
Vikas Manocha94d53082017-02-12 10:25:49 -0800308 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
309 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Christophe Kerelload0376e2017-06-20 17:04:18 +0200310 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
Vikas Manocha280057b2017-04-10 15:02:59 -0700311 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100312 gpio_dsc.port,
313 &desc.dev);
Vikas Manocha280057b2017-04-10 15:02:59 -0700314 if (rv)
315 return rv;
316 desc.offset = gpio_dsc.pin;
317 rv = stm32_gpio_config(&desc, &gpio_ctl);
Vikas Manocha94d53082017-02-12 10:25:49 -0800318 debug("%s: rv = %d\n\n", __func__, rv);
319 if (rv)
320 return rv;
321 }
322 }
323
324 return 0;
325}
326
Christophe Kerellobb44b962017-06-20 17:04:19 +0200327#if CONFIG_IS_ENABLED(PINCTRL_FULL)
328static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
329{
330 return stm32_pinctrl_config(dev_of_offset(config));
331}
332#else /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200333static int stm32_pinctrl_set_state_simple(struct udevice *dev,
334 struct udevice *periph)
335{
336 const void *fdt = gd->fdt_blob;
337 const fdt32_t *list;
338 uint32_t phandle;
339 int config_node;
340 int size, i, ret;
341
342 list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
343 if (!list)
344 return -EINVAL;
345
346 debug("%s: periph->name = %s\n", __func__, periph->name);
347
348 size /= sizeof(*list);
349 for (i = 0; i < size; i++) {
350 phandle = fdt32_to_cpu(*list++);
351
352 config_node = fdt_node_offset_by_phandle(fdt, phandle);
353 if (config_node < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900354 pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200355 return -EINVAL;
356 }
357
358 ret = stm32_pinctrl_config(config_node);
359 if (ret)
360 return ret;
361 }
362
363 return 0;
364}
Christophe Kerellobb44b962017-06-20 17:04:19 +0200365#endif /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200366
Vikas Manocha94d53082017-02-12 10:25:49 -0800367static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellobb44b962017-06-20 17:04:19 +0200368#if CONFIG_IS_ENABLED(PINCTRL_FULL)
369 .set_state = stm32_pinctrl_set_state,
370#else /* PINCTRL_FULL */
Vikas Manocha94d53082017-02-12 10:25:49 -0800371 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellobb44b962017-06-20 17:04:19 +0200372#endif /* PINCTRL_FULL */
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200373#ifndef CONFIG_SPL_BUILD
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200374 .get_pin_name = stm32_pinctrl_get_pin_name,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200375 .get_pins_count = stm32_pinctrl_get_pins_count,
Patrice Chotardb42d9382018-10-24 14:10:20 +0200376 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200377#endif
Vikas Manocha94d53082017-02-12 10:25:49 -0800378};
379
380static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotard98693c22017-12-12 09:49:35 +0100381 { .compatible = "st,stm32f429-pinctrl" },
382 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800383 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotard092e72c2017-09-13 18:00:04 +0200384 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100385 { .compatible = "st,stm32mp157-pinctrl" },
386 { .compatible = "st,stm32mp157-z-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800387 { }
388};
389
390U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200391 .name = "pinctrl_stm32",
392 .id = UCLASS_PINCTRL,
393 .of_match = stm32_pinctrl_ids,
394 .ops = &stm32_pinctrl_ops,
395 .bind = dm_scan_fdt_dev,
396#ifndef CONFIG_SPL_BUILD
397 .probe = stm32_pinctrl_probe,
398 .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv),
399#endif
Vikas Manocha94d53082017-02-12 10:25:49 -0800400};