blob: 20c3c82aa98a83557119e5e8ecbeaf350c789fd6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Felix Brack44d5c372017-03-22 11:26:44 +01002/*
3 * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
Felix Brack44d5c372017-03-22 11:26:44 +01004 */
5
6#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <dm/device_compat.h>
Felix Brack44d5c372017-03-22 11:26:44 +01009#include <dm/pinctrl.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Felix Brack44d5c372017-03-22 11:26:44 +010011#include <asm/io.h>
12
Felix Brack44d5c372017-03-22 11:26:44 +010013struct single_pdata {
14 fdt_addr_t base; /* first configuration register */
15 int offset; /* index of last configuration register */
16 u32 mask; /* configuration-value mask bits */
17 int width; /* configuration register bit width */
Adam Ford159a8872019-06-10 13:15:55 -050018 bool bits_per_mux;
Felix Brack44d5c372017-03-22 11:26:44 +010019};
20
21struct single_fdt_pin_cfg {
22 fdt32_t reg; /* configuration register offset */
23 fdt32_t val; /* configuration register value */
24};
25
Adam Ford159a8872019-06-10 13:15:55 -050026struct single_fdt_bits_cfg {
27 fdt32_t reg; /* configuration register offset */
28 fdt32_t val; /* configuration register value */
29 fdt32_t mask; /* configuration register mask */
30};
31
Felix Brack44d5c372017-03-22 11:26:44 +010032/**
33 * single_configure_pins() - Configure pins based on FDT data
34 *
35 * @dev: Pointer to single pin configuration device which is the parent of
36 * the pins node holding the pin configuration data.
37 * @pins: Pointer to the first element of an array of register/value pairs
38 * of type 'struct single_fdt_pin_cfg'. Each such pair describes the
39 * the pin to be configured and the value to be used for configuration.
40 * This pointer points to a 'pinctrl-single,pins' property in the
41 * device-tree.
42 * @size: Size of the 'pins' array in bytes.
43 * The number of register/value pairs in the 'pins' array therefore
44 * equals to 'size / sizeof(struct single_fdt_pin_cfg)'.
45 */
46static int single_configure_pins(struct udevice *dev,
47 const struct single_fdt_pin_cfg *pins,
48 int size)
49{
Simon Glass0fd3d912020-12-22 19:30:28 -070050 struct single_pdata *pdata = dev_get_plat(dev);
Felix Brack44d5c372017-03-22 11:26:44 +010051 int count = size / sizeof(struct single_fdt_pin_cfg);
Lokesh Vutla5a07cf52018-08-16 18:41:49 +053052 phys_addr_t n, reg;
Felix Brack44d5c372017-03-22 11:26:44 +010053 u32 val;
54
James Balean46f51dc2017-04-18 21:06:35 -050055 for (n = 0; n < count; n++, pins++) {
Felix Brack44d5c372017-03-22 11:26:44 +010056 reg = fdt32_to_cpu(pins->reg);
57 if ((reg < 0) || (reg > pdata->offset)) {
Lokesh Vutla5a07cf52018-08-16 18:41:49 +053058 dev_dbg(dev, " invalid register offset 0x%pa\n", &reg);
Felix Brack44d5c372017-03-22 11:26:44 +010059 continue;
60 }
61 reg += pdata->base;
James Balean46f51dc2017-04-18 21:06:35 -050062 val = fdt32_to_cpu(pins->val) & pdata->mask;
Felix Brack44d5c372017-03-22 11:26:44 +010063 switch (pdata->width) {
James Balean46f51dc2017-04-18 21:06:35 -050064 case 16:
65 writew((readw(reg) & ~pdata->mask) | val, reg);
66 break;
Felix Brack44d5c372017-03-22 11:26:44 +010067 case 32:
James Balean46f51dc2017-04-18 21:06:35 -050068 writel((readl(reg) & ~pdata->mask) | val, reg);
Felix Brack44d5c372017-03-22 11:26:44 +010069 break;
70 default:
71 dev_warn(dev, "unsupported register width %i\n",
72 pdata->width);
James Balean46f51dc2017-04-18 21:06:35 -050073 continue;
Felix Brack44d5c372017-03-22 11:26:44 +010074 }
Lokesh Vutla5a07cf52018-08-16 18:41:49 +053075 dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", &reg, val);
Felix Brack44d5c372017-03-22 11:26:44 +010076 }
77 return 0;
78}
79
Adam Ford159a8872019-06-10 13:15:55 -050080static int single_configure_bits(struct udevice *dev,
81 const struct single_fdt_bits_cfg *pins,
82 int size)
83{
Simon Glass0fd3d912020-12-22 19:30:28 -070084 struct single_pdata *pdata = dev_get_plat(dev);
Adam Ford159a8872019-06-10 13:15:55 -050085 int count = size / sizeof(struct single_fdt_bits_cfg);
86 phys_addr_t n, reg;
87 u32 val, mask;
88
89 for (n = 0; n < count; n++, pins++) {
90 reg = fdt32_to_cpu(pins->reg);
91 if ((reg < 0) || (reg > pdata->offset)) {
92 dev_dbg(dev, " invalid register offset 0x%pa\n", &reg);
93 continue;
94 }
95 reg += pdata->base;
96
97 mask = fdt32_to_cpu(pins->mask);
98 val = fdt32_to_cpu(pins->val) & mask;
99
100 switch (pdata->width) {
101 case 16:
102 writew((readw(reg) & ~mask) | val, reg);
103 break;
104 case 32:
105 writel((readl(reg) & ~mask) | val, reg);
106 break;
107 default:
108 dev_warn(dev, "unsupported register width %i\n",
109 pdata->width);
110 continue;
111 }
112 dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", &reg, val);
113 }
114 return 0;
115}
Felix Brack44d5c372017-03-22 11:26:44 +0100116static int single_set_state(struct udevice *dev,
117 struct udevice *config)
118{
Felix Brack44d5c372017-03-22 11:26:44 +0100119 const struct single_fdt_pin_cfg *prop;
Adam Ford159a8872019-06-10 13:15:55 -0500120 const struct single_fdt_bits_cfg *prop_bits;
Felix Brack44d5c372017-03-22 11:26:44 +0100121 int len;
122
Lokesh Vutladbfd9e02020-04-22 22:55:31 +0530123 prop = dev_read_prop(config, "pinctrl-single,pins", &len);
Adam Ford159a8872019-06-10 13:15:55 -0500124
Felix Brack44d5c372017-03-22 11:26:44 +0100125 if (prop) {
126 dev_dbg(dev, "configuring pins for %s\n", config->name);
127 if (len % sizeof(struct single_fdt_pin_cfg)) {
128 dev_dbg(dev, " invalid pin configuration in fdt\n");
129 return -FDT_ERR_BADSTRUCTURE;
130 }
131 single_configure_pins(dev, prop, len);
Adam Ford159a8872019-06-10 13:15:55 -0500132 return 0;
Felix Brack44d5c372017-03-22 11:26:44 +0100133 }
134
Adam Ford159a8872019-06-10 13:15:55 -0500135 /* pinctrl-single,pins not found so check for pinctrl-single,bits */
Lokesh Vutladbfd9e02020-04-22 22:55:31 +0530136 prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len);
Adam Ford159a8872019-06-10 13:15:55 -0500137 if (prop_bits) {
138 dev_dbg(dev, "configuring pins for %s\n", config->name);
139 if (len % sizeof(struct single_fdt_bits_cfg)) {
140 dev_dbg(dev, " invalid bits configuration in fdt\n");
141 return -FDT_ERR_BADSTRUCTURE;
142 }
143 single_configure_bits(dev, prop_bits, len);
144 return 0;
145 }
146
147 /* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
Felix Brack44d5c372017-03-22 11:26:44 +0100148 return len;
149}
150
Simon Glassd1998a92020-12-03 16:55:21 -0700151static int single_of_to_plat(struct udevice *dev)
Felix Brack44d5c372017-03-22 11:26:44 +0100152{
153 fdt_addr_t addr;
154 u32 of_reg[2];
155 int res;
Simon Glass0fd3d912020-12-22 19:30:28 -0700156 struct single_pdata *pdata = dev_get_plat(dev);
Felix Brack44d5c372017-03-22 11:26:44 +0100157
Patrick Delaunay719cab62020-01-13 11:34:55 +0100158 pdata->width =
159 dev_read_u32_default(dev, "pinctrl-single,register-width", 0);
Felix Brack44d5c372017-03-22 11:26:44 +0100160
Patrick Delaunay719cab62020-01-13 11:34:55 +0100161 res = dev_read_u32_array(dev, "reg", of_reg, 2);
Felix Brack44d5c372017-03-22 11:26:44 +0100162 if (res)
163 return res;
164 pdata->offset = of_reg[1] - pdata->width / 8;
165
Patrick Delaunay719cab62020-01-13 11:34:55 +0100166 addr = dev_read_addr(dev);
Felix Brack44d5c372017-03-22 11:26:44 +0100167 if (addr == FDT_ADDR_T_NONE) {
168 dev_dbg(dev, "no valid base register address\n");
169 return -EINVAL;
170 }
171 pdata->base = addr;
172
Patrick Delaunay719cab62020-01-13 11:34:55 +0100173 pdata->mask = dev_read_u32_default(dev, "pinctrl-single,function-mask",
174 0xffffffff);
175 pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux");
Adam Ford159a8872019-06-10 13:15:55 -0500176
Felix Brack44d5c372017-03-22 11:26:44 +0100177 return 0;
178}
179
180const struct pinctrl_ops single_pinctrl_ops = {
181 .set_state = single_set_state,
182};
183
184static const struct udevice_id single_pinctrl_match[] = {
185 { .compatible = "pinctrl-single" },
186 { /* sentinel */ }
187};
188
189U_BOOT_DRIVER(single_pinctrl) = {
190 .name = "single-pinctrl",
191 .id = UCLASS_PINCTRL,
192 .of_match = single_pinctrl_match,
193 .ops = &single_pinctrl_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700194 .plat_auto = sizeof(struct single_pdata),
Simon Glassd1998a92020-12-03 16:55:21 -0700195 .of_to_plat = single_of_to_plat,
Felix Brack44d5c372017-03-22 11:26:44 +0100196};