1 // SPDX-License-Identifier: GPL-2.0+
3 * Pinctrl driver for STMicroelectronics STi SoCs
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
16 #include <dm/pinctrl.h>
17 #include <linux/bug.h>
18 #include <linux/libfdt.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define MAX_STI_PINCONF_ENTRIES 7
30 /* User-frendly defines for Pin Direction */
31 /* oe = 0, pu = 0, od = 0 */
33 /* oe = 0, pu = 1, od = 0 */
35 /* oe = 1, pu = 0, od = 0 */
37 /* oe = 1, pu = 1, od = 0 */
38 #define OUT_PU (OE | PU)
39 /* oe = 1, pu = 0, od = 1 */
40 #define BIDIR (OE | OD)
41 /* oe = 1, pu = 1, od = 1 */
42 #define BIDIR_PU (OE | PU | OD)
44 struct sti_pinctrl_platdata {
45 struct regmap *regmap;
56 * PIO alternative Function selector
58 void sti_alternate_select(struct udevice *dev, struct sti_pin_desc *pin_desc)
60 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
61 unsigned long sysconf, *sysconfreg;
62 int alt = pin_desc->alt;
63 int bank = pin_desc->bank;
64 int pin = pin_desc->pin;
66 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start;
69 case 0 ... 5: /* in "SBC Bank" */
72 case 10 ... 20: /* in "FRONT Bank" */
73 sysconfreg += bank - 10;
75 case 30 ... 35: /* in "REAR Bank" */
76 sysconfreg += bank - 30;
78 case 40 ... 42: /* in "FLASH Bank" */
79 sysconfreg += bank - 40;
86 sysconf = readl(sysconfreg);
87 sysconf = bitfield_replace(sysconf, pin * 4, 3, alt);
88 writel(sysconf, sysconfreg);
91 /* pin configuration */
92 void sti_pin_configure(struct udevice *dev, struct sti_pin_desc *pin_desc)
94 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
96 int oe = 0, pu = 0, od = 0;
97 unsigned long *sysconfreg;
98 int bank = pin_desc->bank;
100 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start + 40;
103 * NOTE: The PIO configuration for the PIO pins in the
104 * "FLASH Bank" are different from all the other banks!
105 * Specifically, the output-enable pin control register
106 * (SYS_CFG_3040) and the pull-up pin control register
107 * (SYS_CFG_3050), are both classed as being "reserved".
108 * Hence, we do not write to these registers to configure
109 * the OE and PU features for PIOs in this bank. However,
110 * the open-drain pin control register (SYS_CFG_3060)
111 * follows the style of the other banks, and so we can
112 * treat that register normally.
114 * Being pedantic, we should configure the PU and PD features
115 * in the "FLASH Bank" explicitly instead using the four
116 * SYS_CFG registers: 3080, 3081, 3085, and 3086. However, this
117 * would necessitate passing in the alternate function number
118 * to this function, and adding some horrible complexity here.
119 * Alternatively, we could just perform 4 32-bit "pokes" to
120 * these four SYS_CFG registers early in the initialization.
121 * In practice, these four SYS_CFG registers are correct
122 * after a reset, and U-Boot does not need to change them, so
123 * we (cheat and) rely on these registers being correct.
124 * WARNING: Please be aware of this (pragmatic) behaviour!
126 int flashss = 0; /* bool: PIO in the Flash Sub-System ? */
128 switch (pin_desc->dir) {
130 oe = 0; pu = 0; od = 0;
133 oe = 0; pu = 1; od = 0;
136 oe = 1; pu = 0; od = 0;
139 oe = 1; pu = 0; od = 1;
142 oe = 1; pu = 1; od = 1;
146 pr_err("%s invalid direction value: 0x%x\n",
147 __func__, pin_desc->dir);
153 case 0 ... 5: /* in "SBC Bank" */
154 sysconfreg += bank / 4;
156 case 10 ... 20: /* in "FRONT Bank" */
158 sysconfreg += bank / 4;
160 case 30 ... 35: /* in "REAR Bank" */
162 sysconfreg += bank / 4;
164 case 40 ... 42: /* in "FLASH Bank" */
166 sysconfreg += bank / 4;
167 flashss = 1; /* pin is in the Flash Sub-System */
174 bit = ((bank * 8) + pin_desc->pin) % 32;
177 * set the "Output Enable" pin control
178 * but, do nothing if in the flashSS
182 generic_set_bit(bit, sysconfreg);
184 generic_clear_bit(bit, sysconfreg);
187 sysconfreg += 10; /* skip to next set of syscfg registers */
190 * set the "Pull Up" pin control
191 * but, do nothing if in the FlashSS
196 generic_set_bit(bit, sysconfreg);
198 generic_clear_bit(bit, sysconfreg);
201 sysconfreg += 10; /* skip to next set of syscfg registers */
203 /* set the "Open Drain Enable" pin control */
205 generic_set_bit(bit, sysconfreg);
207 generic_clear_bit(bit, sysconfreg);
211 static int sti_pinctrl_set_state(struct udevice *dev, struct udevice *config)
213 struct fdtdec_phandle_args args;
214 const void *blob = gd->fdt_blob;
215 const char *prop_name;
216 int node = dev_of_offset(config);
217 int property_offset, prop_len;
218 int pinconf_node, ret, count;
219 const char *bank_name;
220 u32 cells[MAX_STI_PINCONF_ENTRIES];
222 struct sti_pin_desc pin_desc;
224 /* go to next node "st,pins" which contains the pins configuration */
225 pinconf_node = fdt_subnode_offset(blob, node, "st,pins");
228 * parse each pins configuration which looks like :
229 * pin_name = <bank_phandle pin_nb alt dir rt_type rt_delay rt_clk>
232 fdt_for_each_property_offset(property_offset, blob, pinconf_node) {
233 fdt_getprop_by_offset(blob, property_offset, &prop_name,
236 /* extract the bank of the pin description */
237 ret = fdtdec_parse_phandle_with_args(blob, pinconf_node,
238 prop_name, "#gpio-cells",
241 pr_err("Can't get the gpio bank phandle: %d\n", ret);
245 bank_name = fdt_getprop(blob, args.node, "st,bank-name",
248 pr_err("Can't find bank-name property %d\n", count);
252 pin_desc.bank = trailing_strtoln(bank_name, NULL);
254 count = fdtdec_get_int_array_count(blob, pinconf_node,
258 pr_err("Bad pin configuration array %d\n", count);
262 if (count > MAX_STI_PINCONF_ENTRIES) {
263 pr_err("Unsupported pinconf array count %d\n", count);
267 pin_desc.pin = cells[1];
268 pin_desc.alt = cells[2];
269 pin_desc.dir = cells[3];
271 sti_alternate_select(dev, &pin_desc);
272 sti_pin_configure(dev, &pin_desc);
278 static int sti_pinctrl_probe(struct udevice *dev)
280 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
281 struct udevice *syscon;
284 /* get corresponding syscon phandle */
285 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
286 "st,syscfg", &syscon);
288 pr_err("unable to find syscon device\n");
292 plat->regmap = syscon_get_regmap(syscon);
294 pr_err("unable to find regmap\n");
301 static const struct udevice_id sti_pinctrl_ids[] = {
302 { .compatible = "st,stih407-sbc-pinctrl" },
303 { .compatible = "st,stih407-front-pinctrl" },
304 { .compatible = "st,stih407-rear-pinctrl" },
305 { .compatible = "st,stih407-flash-pinctrl" },
309 const struct pinctrl_ops sti_pinctrl_ops = {
310 .set_state = sti_pinctrl_set_state,
313 U_BOOT_DRIVER(pinctrl_sti) = {
314 .name = "pinctrl_sti",
315 .id = UCLASS_PINCTRL,
316 .of_match = sti_pinctrl_ids,
317 .ops = &sti_pinctrl_ops,
318 .probe = sti_pinctrl_probe,
319 .platdata_auto_alloc_size = sizeof(struct sti_pinctrl_platdata),
320 .ops = &sti_pinctrl_ops,