Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch> |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <dm/device_compat.h> |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 9 | #include <dm/pinctrl.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 13 | /** |
| 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 21 | struct single_pdata { |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 22 | fdt_addr_t base; |
| 23 | int offset; |
| 24 | u32 mask; |
| 25 | int width; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 26 | bool bits_per_mux; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 29 | /** |
| 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 38 | struct single_fdt_pin_cfg { |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 39 | fdt32_t reg; |
| 40 | fdt32_t val; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 43 | /** |
| 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 53 | struct single_fdt_bits_cfg { |
Dario Binacchi | 3b48802 | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 54 | fdt32_t reg; |
| 55 | fdt32_t val; |
| 56 | fdt32_t mask; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 59 | /** |
| 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 | */ |
| 73 | static int single_configure_pins(struct udevice *dev, |
| 74 | const struct single_fdt_pin_cfg *pins, |
| 75 | int size) |
| 76 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 77 | struct single_pdata *pdata = dev_get_plat(dev); |
Dario Binacchi | e02eea3 | 2021-04-11 09:39:40 +0200 | [diff] [blame] | 78 | int n, count = size / sizeof(struct single_fdt_pin_cfg); |
| 79 | phys_addr_t reg; |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 80 | u32 offset, val; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 81 | |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 82 | for (n = 0; n < count; n++, pins++) { |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 83 | 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 87 | continue; |
| 88 | } |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 89 | |
| 90 | reg = pdata->base + offset; |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 91 | val = fdt32_to_cpu(pins->val) & pdata->mask; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 92 | switch (pdata->width) { |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 93 | case 16: |
| 94 | writew((readw(reg) & ~pdata->mask) | val, reg); |
| 95 | break; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 96 | case 32: |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 97 | writel((readl(reg) & ~pdata->mask) | val, reg); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 98 | break; |
| 99 | default: |
| 100 | dev_warn(dev, "unsupported register width %i\n", |
| 101 | pdata->width); |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 102 | continue; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 103 | } |
Dario Binacchi | 35f4ac5 | 2021-04-11 09:39:42 +0200 | [diff] [blame] | 104 | dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 109 | static int single_configure_bits(struct udevice *dev, |
| 110 | const struct single_fdt_bits_cfg *pins, |
| 111 | int size) |
| 112 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 113 | struct single_pdata *pdata = dev_get_plat(dev); |
Dario Binacchi | e02eea3 | 2021-04-11 09:39:40 +0200 | [diff] [blame] | 114 | int n, count = size / sizeof(struct single_fdt_bits_cfg); |
| 115 | phys_addr_t reg; |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 116 | u32 offset, val, mask; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 117 | |
| 118 | for (n = 0; n < count; n++, pins++) { |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 119 | 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 123 | continue; |
| 124 | } |
Dario Binacchi | c3ed31f | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 125 | |
| 126 | reg = pdata->base + offset; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 127 | |
| 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 Binacchi | 35f4ac5 | 2021-04-11 09:39:42 +0200 | [diff] [blame] | 143 | dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val); |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 144 | } |
| 145 | return 0; |
| 146 | } |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 147 | static int single_set_state(struct udevice *dev, |
| 148 | struct udevice *config) |
| 149 | { |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 150 | const struct single_fdt_pin_cfg *prop; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 151 | const struct single_fdt_bits_cfg *prop_bits; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 152 | int len; |
| 153 | |
Lokesh Vutla | 0bef026 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 154 | prop = dev_read_prop(config, "pinctrl-single,pins", &len); |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 155 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 156 | 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 163 | return 0; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 166 | /* pinctrl-single,pins not found so check for pinctrl-single,bits */ |
Lokesh Vutla | 0bef026 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 167 | prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len); |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 168 | 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 179 | return len; |
| 180 | } |
| 181 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 182 | static int single_of_to_plat(struct udevice *dev) |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 183 | { |
| 184 | fdt_addr_t addr; |
Dario Binacchi | 22fb01d | 2021-04-11 09:39:43 +0200 | [diff] [blame^] | 185 | fdt_size_t size; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 186 | struct single_pdata *pdata = dev_get_plat(dev); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 187 | |
Patrick Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 188 | pdata->width = |
| 189 | dev_read_u32_default(dev, "pinctrl-single,register-width", 0); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 190 | |
Dario Binacchi | 22fb01d | 2021-04-11 09:39:43 +0200 | [diff] [blame^] | 191 | 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 198 | |
Patrick Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 199 | addr = dev_read_addr(dev); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 200 | 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 Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 206 | 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 209 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | const struct pinctrl_ops single_pinctrl_ops = { |
| 214 | .set_state = single_set_state, |
| 215 | }; |
| 216 | |
| 217 | static const struct udevice_id single_pinctrl_match[] = { |
| 218 | { .compatible = "pinctrl-single" }, |
| 219 | { /* sentinel */ } |
| 220 | }; |
| 221 | |
| 222 | U_BOOT_DRIVER(single_pinctrl) = { |
| 223 | .name = "single-pinctrl", |
| 224 | .id = UCLASS_PINCTRL, |
| 225 | .of_match = single_pinctrl_match, |
| 226 | .ops = &single_pinctrl_ops, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 227 | .plat_auto = sizeof(struct single_pdata), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 228 | .of_to_plat = single_of_to_plat, |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 229 | }; |