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 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 13 | struct 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 18 | bool bits_per_mux; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | struct single_fdt_pin_cfg { |
| 22 | fdt32_t reg; /* configuration register offset */ |
| 23 | fdt32_t val; /* configuration register value */ |
| 24 | }; |
| 25 | |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 26 | struct 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 32 | /** |
| 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 | */ |
| 46 | static int single_configure_pins(struct udevice *dev, |
| 47 | const struct single_fdt_pin_cfg *pins, |
| 48 | int size) |
| 49 | { |
| 50 | struct single_pdata *pdata = dev->platdata; |
| 51 | int count = size / sizeof(struct single_fdt_pin_cfg); |
Lokesh Vutla | 95be3b5 | 2018-08-16 18:41:49 +0530 | [diff] [blame] | 52 | phys_addr_t n, reg; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 53 | u32 val; |
| 54 | |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 55 | for (n = 0; n < count; n++, pins++) { |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 56 | reg = fdt32_to_cpu(pins->reg); |
| 57 | if ((reg < 0) || (reg > pdata->offset)) { |
Lokesh Vutla | 95be3b5 | 2018-08-16 18:41:49 +0530 | [diff] [blame] | 58 | dev_dbg(dev, " invalid register offset 0x%pa\n", ®); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 59 | continue; |
| 60 | } |
| 61 | reg += pdata->base; |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 62 | val = fdt32_to_cpu(pins->val) & pdata->mask; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 63 | switch (pdata->width) { |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 64 | case 16: |
| 65 | writew((readw(reg) & ~pdata->mask) | val, reg); |
| 66 | break; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 67 | case 32: |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 68 | writel((readl(reg) & ~pdata->mask) | val, reg); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 69 | break; |
| 70 | default: |
| 71 | dev_warn(dev, "unsupported register width %i\n", |
| 72 | pdata->width); |
James Balean | d2f96ad | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 73 | continue; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 74 | } |
Lokesh Vutla | 95be3b5 | 2018-08-16 18:41:49 +0530 | [diff] [blame] | 75 | dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", ®, val); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 80 | static int single_configure_bits(struct udevice *dev, |
| 81 | const struct single_fdt_bits_cfg *pins, |
| 82 | int size) |
| 83 | { |
| 84 | struct single_pdata *pdata = dev->platdata; |
| 85 | 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", ®); |
| 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", ®, val); |
| 113 | } |
| 114 | return 0; |
| 115 | } |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 116 | static int single_set_state(struct udevice *dev, |
| 117 | struct udevice *config) |
| 118 | { |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 119 | const struct single_fdt_pin_cfg *prop; |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 120 | const struct single_fdt_bits_cfg *prop_bits; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 121 | int len; |
| 122 | |
Lokesh Vutla | 0bef026 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 123 | prop = dev_read_prop(config, "pinctrl-single,pins", &len); |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 124 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 125 | 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 132 | return 0; |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 135 | /* pinctrl-single,pins not found so check for pinctrl-single,bits */ |
Lokesh Vutla | 0bef026 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 136 | prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len); |
Adam Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 137 | 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 Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 148 | return len; |
| 149 | } |
| 150 | |
| 151 | static int single_ofdata_to_platdata(struct udevice *dev) |
| 152 | { |
| 153 | fdt_addr_t addr; |
| 154 | u32 of_reg[2]; |
| 155 | int res; |
| 156 | struct single_pdata *pdata = dev->platdata; |
| 157 | |
Patrick Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 158 | pdata->width = |
| 159 | dev_read_u32_default(dev, "pinctrl-single,register-width", 0); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 160 | |
Patrick Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 161 | res = dev_read_u32_array(dev, "reg", of_reg, 2); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 162 | if (res) |
| 163 | return res; |
| 164 | pdata->offset = of_reg[1] - pdata->width / 8; |
| 165 | |
Patrick Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 166 | addr = dev_read_addr(dev); |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 167 | 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 Delaunay | af2fb74 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 173 | 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 Ford | 6305a5c | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 176 | |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | const struct pinctrl_ops single_pinctrl_ops = { |
| 181 | .set_state = single_set_state, |
| 182 | }; |
| 183 | |
| 184 | static const struct udevice_id single_pinctrl_match[] = { |
| 185 | { .compatible = "pinctrl-single" }, |
| 186 | { /* sentinel */ } |
| 187 | }; |
| 188 | |
| 189 | U_BOOT_DRIVER(single_pinctrl) = { |
| 190 | .name = "single-pinctrl", |
| 191 | .id = UCLASS_PINCTRL, |
| 192 | .of_match = single_pinctrl_match, |
| 193 | .ops = &single_pinctrl_ops, |
Felix Brack | 7bc2354 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 194 | .platdata_auto_alloc_size = sizeof(struct single_pdata), |
| 195 | .ofdata_to_platdata = single_ofdata_to_platdata, |
| 196 | }; |