blob: d5656de8e8a794a8506079f9a4e0d47b4b313786 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Felix Brack7bc23542017-03-22 11:26:44 +01002/*
3 * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
Felix Brack7bc23542017-03-22 11:26:44 +01004 */
5
6#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <dm/device_compat.h>
Felix Brack7bc23542017-03-22 11:26:44 +01009#include <dm/pinctrl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Felix Brack7bc23542017-03-22 11:26:44 +010011#include <asm/io.h>
12
Dario Binacchi3b488022021-04-11 09:39:39 +020013/**
14 * struct single_pdata - platform data
15 * @base: first configuration register
16 * @offset: index of last configuration register
17 * @mask: configuration-value mask bits
18 * @width: configuration register bit width
19 * @bits_per_mux: true if one register controls more than one pin
20 */
Felix Brack7bc23542017-03-22 11:26:44 +010021struct single_pdata {
Dario Binacchi3b488022021-04-11 09:39:39 +020022 fdt_addr_t base;
23 int offset;
24 u32 mask;
25 int width;
Adam Ford6305a5c2019-06-10 13:15:55 -050026 bool bits_per_mux;
Felix Brack7bc23542017-03-22 11:26:44 +010027};
28
Dario Binacchi3b488022021-04-11 09:39:39 +020029/**
30 * struct single_fdt_pin_cfg - pin configuration
31 *
32 * This structure is used for the pin configuration parameters in case
33 * the register controls only one pin.
34 *
35 * @reg: configuration register offset
36 * @val: configuration register value
37 */
Felix Brack7bc23542017-03-22 11:26:44 +010038struct single_fdt_pin_cfg {
Dario Binacchi3b488022021-04-11 09:39:39 +020039 fdt32_t reg;
40 fdt32_t val;
Felix Brack7bc23542017-03-22 11:26:44 +010041};
42
Dario Binacchi3b488022021-04-11 09:39:39 +020043/**
44 * struct single_fdt_bits_cfg - pin configuration
45 *
46 * This structure is used for the pin configuration parameters in case
47 * the register controls more than one pin.
48 *
49 * @reg: configuration register offset
50 * @val: configuration register value
51 * @mask: configuration register mask
52 */
Adam Ford6305a5c2019-06-10 13:15:55 -050053struct single_fdt_bits_cfg {
Dario Binacchi3b488022021-04-11 09:39:39 +020054 fdt32_t reg;
55 fdt32_t val;
56 fdt32_t mask;
Adam Ford6305a5c2019-06-10 13:15:55 -050057};
58
Felix Brack7bc23542017-03-22 11:26:44 +010059/**
60 * single_configure_pins() - Configure pins based on FDT data
61 *
62 * @dev: Pointer to single pin configuration device which is the parent of
63 * the pins node holding the pin configuration data.
64 * @pins: Pointer to the first element of an array of register/value pairs
65 * of type 'struct single_fdt_pin_cfg'. Each such pair describes the
66 * the pin to be configured and the value to be used for configuration.
67 * This pointer points to a 'pinctrl-single,pins' property in the
68 * device-tree.
69 * @size: Size of the 'pins' array in bytes.
70 * The number of register/value pairs in the 'pins' array therefore
71 * equals to 'size / sizeof(struct single_fdt_pin_cfg)'.
72 */
73static int single_configure_pins(struct udevice *dev,
74 const struct single_fdt_pin_cfg *pins,
75 int size)
76{
Simon Glass95588622020-12-22 19:30:28 -070077 struct single_pdata *pdata = dev_get_plat(dev);
Dario Binacchie02eea32021-04-11 09:39:40 +020078 int n, count = size / sizeof(struct single_fdt_pin_cfg);
79 phys_addr_t reg;
Dario Binacchic3ed31f2021-04-11 09:39:41 +020080 u32 offset, val;
Felix Brack7bc23542017-03-22 11:26:44 +010081
James Baleand2f96ad2017-04-18 21:06:35 -050082 for (n = 0; n < count; n++, pins++) {
Dario Binacchic3ed31f2021-04-11 09:39:41 +020083 offset = fdt32_to_cpu(pins->reg);
84 if (offset < 0 || offset > pdata->offset) {
85 dev_dbg(dev, " invalid register offset 0x%x\n",
86 offset);
Felix Brack7bc23542017-03-22 11:26:44 +010087 continue;
88 }
Dario Binacchic3ed31f2021-04-11 09:39:41 +020089
90 reg = pdata->base + offset;
James Baleand2f96ad2017-04-18 21:06:35 -050091 val = fdt32_to_cpu(pins->val) & pdata->mask;
Felix Brack7bc23542017-03-22 11:26:44 +010092 switch (pdata->width) {
James Baleand2f96ad2017-04-18 21:06:35 -050093 case 16:
94 writew((readw(reg) & ~pdata->mask) | val, reg);
95 break;
Felix Brack7bc23542017-03-22 11:26:44 +010096 case 32:
James Baleand2f96ad2017-04-18 21:06:35 -050097 writel((readl(reg) & ~pdata->mask) | val, reg);
Felix Brack7bc23542017-03-22 11:26:44 +010098 break;
99 default:
100 dev_warn(dev, "unsupported register width %i\n",
101 pdata->width);
James Baleand2f96ad2017-04-18 21:06:35 -0500102 continue;
Felix Brack7bc23542017-03-22 11:26:44 +0100103 }
Dario Binacchi35f4ac52021-04-11 09:39:42 +0200104 dev_dbg(dev, " reg/val %pa/0x%08x\n", &reg, val);
Felix Brack7bc23542017-03-22 11:26:44 +0100105 }
106 return 0;
107}
108
Adam Ford6305a5c2019-06-10 13:15:55 -0500109static int single_configure_bits(struct udevice *dev,
110 const struct single_fdt_bits_cfg *pins,
111 int size)
112{
Simon Glass95588622020-12-22 19:30:28 -0700113 struct single_pdata *pdata = dev_get_plat(dev);
Dario Binacchie02eea32021-04-11 09:39:40 +0200114 int n, count = size / sizeof(struct single_fdt_bits_cfg);
115 phys_addr_t reg;
Dario Binacchic3ed31f2021-04-11 09:39:41 +0200116 u32 offset, val, mask;
Adam Ford6305a5c2019-06-10 13:15:55 -0500117
118 for (n = 0; n < count; n++, pins++) {
Dario Binacchic3ed31f2021-04-11 09:39:41 +0200119 offset = fdt32_to_cpu(pins->reg);
120 if (offset < 0 || offset > pdata->offset) {
121 dev_dbg(dev, " invalid register offset 0x%x\n",
122 offset);
Adam Ford6305a5c2019-06-10 13:15:55 -0500123 continue;
124 }
Dario Binacchic3ed31f2021-04-11 09:39:41 +0200125
126 reg = pdata->base + offset;
Adam Ford6305a5c2019-06-10 13:15:55 -0500127
128 mask = fdt32_to_cpu(pins->mask);
129 val = fdt32_to_cpu(pins->val) & mask;
130
131 switch (pdata->width) {
132 case 16:
133 writew((readw(reg) & ~mask) | val, reg);
134 break;
135 case 32:
136 writel((readl(reg) & ~mask) | val, reg);
137 break;
138 default:
139 dev_warn(dev, "unsupported register width %i\n",
140 pdata->width);
141 continue;
142 }
Dario Binacchi35f4ac52021-04-11 09:39:42 +0200143 dev_dbg(dev, " reg/val %pa/0x%08x\n", &reg, val);
Adam Ford6305a5c2019-06-10 13:15:55 -0500144 }
145 return 0;
146}
Felix Brack7bc23542017-03-22 11:26:44 +0100147static int single_set_state(struct udevice *dev,
148 struct udevice *config)
149{
Felix Brack7bc23542017-03-22 11:26:44 +0100150 const struct single_fdt_pin_cfg *prop;
Adam Ford6305a5c2019-06-10 13:15:55 -0500151 const struct single_fdt_bits_cfg *prop_bits;
Felix Brack7bc23542017-03-22 11:26:44 +0100152 int len;
153
Lokesh Vutla0bef0262020-04-22 22:55:31 +0530154 prop = dev_read_prop(config, "pinctrl-single,pins", &len);
Adam Ford6305a5c2019-06-10 13:15:55 -0500155
Felix Brack7bc23542017-03-22 11:26:44 +0100156 if (prop) {
157 dev_dbg(dev, "configuring pins for %s\n", config->name);
158 if (len % sizeof(struct single_fdt_pin_cfg)) {
159 dev_dbg(dev, " invalid pin configuration in fdt\n");
160 return -FDT_ERR_BADSTRUCTURE;
161 }
162 single_configure_pins(dev, prop, len);
Adam Ford6305a5c2019-06-10 13:15:55 -0500163 return 0;
Felix Brack7bc23542017-03-22 11:26:44 +0100164 }
165
Adam Ford6305a5c2019-06-10 13:15:55 -0500166 /* pinctrl-single,pins not found so check for pinctrl-single,bits */
Lokesh Vutla0bef0262020-04-22 22:55:31 +0530167 prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len);
Adam Ford6305a5c2019-06-10 13:15:55 -0500168 if (prop_bits) {
169 dev_dbg(dev, "configuring pins for %s\n", config->name);
170 if (len % sizeof(struct single_fdt_bits_cfg)) {
171 dev_dbg(dev, " invalid bits configuration in fdt\n");
172 return -FDT_ERR_BADSTRUCTURE;
173 }
174 single_configure_bits(dev, prop_bits, len);
175 return 0;
176 }
177
178 /* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
Felix Brack7bc23542017-03-22 11:26:44 +0100179 return len;
180}
181
Simon Glassaad29ae2020-12-03 16:55:21 -0700182static int single_of_to_plat(struct udevice *dev)
Felix Brack7bc23542017-03-22 11:26:44 +0100183{
184 fdt_addr_t addr;
Dario Binacchi22fb01d2021-04-11 09:39:43 +0200185 fdt_size_t size;
Simon Glass95588622020-12-22 19:30:28 -0700186 struct single_pdata *pdata = dev_get_plat(dev);
Felix Brack7bc23542017-03-22 11:26:44 +0100187
Patrick Delaunayaf2fb742020-01-13 11:34:55 +0100188 pdata->width =
189 dev_read_u32_default(dev, "pinctrl-single,register-width", 0);
Felix Brack7bc23542017-03-22 11:26:44 +0100190
Dario Binacchi22fb01d2021-04-11 09:39:43 +0200191 addr = dev_read_addr_size(dev, "reg", &size);
192 if (addr == FDT_ADDR_T_NONE) {
193 dev_err(dev, "failed to get base register size\n");
194 return -EINVAL;
195 }
196
197 pdata->offset = size - pdata->width / BITS_PER_BYTE;
Felix Brack7bc23542017-03-22 11:26:44 +0100198
Patrick Delaunayaf2fb742020-01-13 11:34:55 +0100199 addr = dev_read_addr(dev);
Felix Brack7bc23542017-03-22 11:26:44 +0100200 if (addr == FDT_ADDR_T_NONE) {
201 dev_dbg(dev, "no valid base register address\n");
202 return -EINVAL;
203 }
204 pdata->base = addr;
205
Patrick Delaunayaf2fb742020-01-13 11:34:55 +0100206 pdata->mask = dev_read_u32_default(dev, "pinctrl-single,function-mask",
207 0xffffffff);
208 pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux");
Adam Ford6305a5c2019-06-10 13:15:55 -0500209
Felix Brack7bc23542017-03-22 11:26:44 +0100210 return 0;
211}
212
213const struct pinctrl_ops single_pinctrl_ops = {
214 .set_state = single_set_state,
215};
216
217static const struct udevice_id single_pinctrl_match[] = {
218 { .compatible = "pinctrl-single" },
219 { /* sentinel */ }
220};
221
222U_BOOT_DRIVER(single_pinctrl) = {
223 .name = "single-pinctrl",
224 .id = UCLASS_PINCTRL,
225 .of_match = single_pinctrl_match,
226 .ops = &single_pinctrl_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700227 .plat_auto = sizeof(struct single_pdata),
Simon Glassaad29ae2020-12-03 16:55:21 -0700228 .of_to_plat = single_of_to_plat,
Felix Brack7bc23542017-03-22 11:26:44 +0100229};