1 // SPDX-License-Identifier: GPL-2.0+
3 * Pinctrl driver for STMicroelectronics STi SoCs
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
16 #include <dm/pinctrl.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #define MAX_STI_PINCONF_ENTRIES 7
28 /* User-frendly defines for Pin Direction */
29 /* oe = 0, pu = 0, od = 0 */
31 /* oe = 0, pu = 1, od = 0 */
33 /* oe = 1, pu = 0, od = 0 */
35 /* oe = 1, pu = 1, od = 0 */
36 #define OUT_PU (OE | PU)
37 /* oe = 1, pu = 0, od = 1 */
38 #define BIDIR (OE | OD)
39 /* oe = 1, pu = 1, od = 1 */
40 #define BIDIR_PU (OE | PU | OD)
42 struct sti_pinctrl_platdata {
43 struct regmap *regmap;
54 * PIO alternative Function selector
56 void sti_alternate_select(struct udevice *dev, struct sti_pin_desc *pin_desc)
58 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
59 unsigned long sysconf, *sysconfreg;
60 int alt = pin_desc->alt;
61 int bank = pin_desc->bank;
62 int pin = pin_desc->pin;
64 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start;
67 case 0 ... 5: /* in "SBC Bank" */
70 case 10 ... 20: /* in "FRONT Bank" */
71 sysconfreg += bank - 10;
73 case 30 ... 35: /* in "REAR Bank" */
74 sysconfreg += bank - 30;
76 case 40 ... 42: /* in "FLASH Bank" */
77 sysconfreg += bank - 40;
84 sysconf = readl(sysconfreg);
85 sysconf = bitfield_replace(sysconf, pin * 4, 3, alt);
86 writel(sysconf, sysconfreg);
89 /* pin configuration */
90 void sti_pin_configure(struct udevice *dev, struct sti_pin_desc *pin_desc)
92 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
94 int oe = 0, pu = 0, od = 0;
95 unsigned long *sysconfreg;
96 int bank = pin_desc->bank;
98 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start + 40;
101 * NOTE: The PIO configuration for the PIO pins in the
102 * "FLASH Bank" are different from all the other banks!
103 * Specifically, the output-enable pin control register
104 * (SYS_CFG_3040) and the pull-up pin control register
105 * (SYS_CFG_3050), are both classed as being "reserved".
106 * Hence, we do not write to these registers to configure
107 * the OE and PU features for PIOs in this bank. However,
108 * the open-drain pin control register (SYS_CFG_3060)
109 * follows the style of the other banks, and so we can
110 * treat that register normally.
112 * Being pedantic, we should configure the PU and PD features
113 * in the "FLASH Bank" explicitly instead using the four
114 * SYS_CFG registers: 3080, 3081, 3085, and 3086. However, this
115 * would necessitate passing in the alternate function number
116 * to this function, and adding some horrible complexity here.
117 * Alternatively, we could just perform 4 32-bit "pokes" to
118 * these four SYS_CFG registers early in the initialization.
119 * In practice, these four SYS_CFG registers are correct
120 * after a reset, and U-Boot does not need to change them, so
121 * we (cheat and) rely on these registers being correct.
122 * WARNING: Please be aware of this (pragmatic) behaviour!
124 int flashss = 0; /* bool: PIO in the Flash Sub-System ? */
126 switch (pin_desc->dir) {
128 oe = 0; pu = 0; od = 0;
131 oe = 0; pu = 1; od = 0;
134 oe = 1; pu = 0; od = 0;
137 oe = 1; pu = 0; od = 1;
140 oe = 1; pu = 1; od = 1;
144 pr_err("%s invalid direction value: 0x%x\n",
145 __func__, pin_desc->dir);
151 case 0 ... 5: /* in "SBC Bank" */
152 sysconfreg += bank / 4;
154 case 10 ... 20: /* in "FRONT Bank" */
156 sysconfreg += bank / 4;
158 case 30 ... 35: /* in "REAR Bank" */
160 sysconfreg += bank / 4;
162 case 40 ... 42: /* in "FLASH Bank" */
164 sysconfreg += bank / 4;
165 flashss = 1; /* pin is in the Flash Sub-System */
172 bit = ((bank * 8) + pin_desc->pin) % 32;
175 * set the "Output Enable" pin control
176 * but, do nothing if in the flashSS
180 generic_set_bit(bit, sysconfreg);
182 generic_clear_bit(bit, sysconfreg);
185 sysconfreg += 10; /* skip to next set of syscfg registers */
188 * set the "Pull Up" pin control
189 * but, do nothing if in the FlashSS
194 generic_set_bit(bit, sysconfreg);
196 generic_clear_bit(bit, sysconfreg);
199 sysconfreg += 10; /* skip to next set of syscfg registers */
201 /* set the "Open Drain Enable" pin control */
203 generic_set_bit(bit, sysconfreg);
205 generic_clear_bit(bit, sysconfreg);
209 static int sti_pinctrl_set_state(struct udevice *dev, struct udevice *config)
211 struct fdtdec_phandle_args args;
212 const void *blob = gd->fdt_blob;
213 const char *prop_name;
214 int node = dev_of_offset(config);
215 int property_offset, prop_len;
216 int pinconf_node, ret, count;
217 const char *bank_name;
218 u32 cells[MAX_STI_PINCONF_ENTRIES];
220 struct sti_pin_desc pin_desc;
222 /* go to next node "st,pins" which contains the pins configuration */
223 pinconf_node = fdt_subnode_offset(blob, node, "st,pins");
226 * parse each pins configuration which looks like :
227 * pin_name = <bank_phandle pin_nb alt dir rt_type rt_delay rt_clk>
230 fdt_for_each_property_offset(property_offset, blob, pinconf_node) {
231 fdt_getprop_by_offset(blob, property_offset, &prop_name,
234 /* extract the bank of the pin description */
235 ret = fdtdec_parse_phandle_with_args(blob, pinconf_node,
236 prop_name, "#gpio-cells",
239 pr_err("Can't get the gpio bank phandle: %d\n", ret);
243 bank_name = fdt_getprop(blob, args.node, "st,bank-name",
246 pr_err("Can't find bank-name property %d\n", count);
250 pin_desc.bank = trailing_strtoln(bank_name, NULL);
252 count = fdtdec_get_int_array_count(blob, pinconf_node,
256 pr_err("Bad pin configuration array %d\n", count);
260 if (count > MAX_STI_PINCONF_ENTRIES) {
261 pr_err("Unsupported pinconf array count %d\n", count);
265 pin_desc.pin = cells[1];
266 pin_desc.alt = cells[2];
267 pin_desc.dir = cells[3];
269 sti_alternate_select(dev, &pin_desc);
270 sti_pin_configure(dev, &pin_desc);
276 static int sti_pinctrl_probe(struct udevice *dev)
278 struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
279 struct udevice *syscon;
282 /* get corresponding syscon phandle */
283 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
284 "st,syscfg", &syscon);
286 pr_err("unable to find syscon device\n");
290 plat->regmap = syscon_get_regmap(syscon);
292 pr_err("unable to find regmap\n");
299 static const struct udevice_id sti_pinctrl_ids[] = {
300 { .compatible = "st,stih407-sbc-pinctrl" },
301 { .compatible = "st,stih407-front-pinctrl" },
302 { .compatible = "st,stih407-rear-pinctrl" },
303 { .compatible = "st,stih407-flash-pinctrl" },
307 const struct pinctrl_ops sti_pinctrl_ops = {
308 .set_state = sti_pinctrl_set_state,
311 U_BOOT_DRIVER(pinctrl_sti) = {
312 .name = "pinctrl_sti",
313 .id = UCLASS_PINCTRL,
314 .of_match = sti_pinctrl_ids,
315 .ops = &sti_pinctrl_ops,
316 .probe = sti_pinctrl_probe,
317 .platdata_auto_alloc_size = sizeof(struct sti_pinctrl_platdata),
318 .ops = &sti_pinctrl_ops,