Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com> |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <mapmem.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/err.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 11 | #include <dm/pinctrl.h> |
| 12 | |
| 13 | #include "pinctrl-imx.h" |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 18 | { |
| 19 | struct imx_pinctrl_priv *priv = dev_get_priv(dev); |
| 20 | struct imx_pinctrl_soc_info *info = priv->info; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 21 | int node = dev_of_offset(config); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 22 | const struct fdt_property *prop; |
| 23 | u32 *pin_data; |
| 24 | int npins, size, pin_size; |
Ye Li | eeebae3 | 2019-01-04 09:08:26 +0000 | [diff] [blame] | 25 | int mux_reg, conf_reg, input_reg; |
| 26 | u32 input_val, mux_mode, config_val; |
Peng Fan | f70bf2b | 2017-02-22 16:21:49 +0800 | [diff] [blame] | 27 | u32 mux_shift = info->mux_mask ? ffs(info->mux_mask) - 1 : 0; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 28 | int i, j = 0; |
| 29 | |
| 30 | dev_dbg(dev, "%s: %s\n", __func__, config->name); |
| 31 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 32 | if (info->flags & IMX8_USE_SCU) |
| 33 | pin_size = SHARE_IMX8_PIN_SIZE; |
| 34 | else if (info->flags & SHARE_MUX_CONF_REG) |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 35 | pin_size = SHARE_FSL_PIN_SIZE; |
| 36 | else |
| 37 | pin_size = FSL_PIN_SIZE; |
| 38 | |
| 39 | prop = fdt_getprop(gd->fdt_blob, node, "fsl,pins", &size); |
| 40 | if (!prop) { |
| 41 | dev_err(dev, "No fsl,pins property in node %s\n", config->name); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
| 45 | if (!size || size % pin_size) { |
| 46 | dev_err(dev, "Invalid fsl,pins property in node %s\n", |
| 47 | config->name); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | |
| 51 | pin_data = devm_kzalloc(dev, size, 0); |
| 52 | if (!pin_data) |
| 53 | return -ENOMEM; |
| 54 | |
| 55 | if (fdtdec_get_int_array(gd->fdt_blob, node, "fsl,pins", |
| 56 | pin_data, size >> 2)) { |
| 57 | dev_err(dev, "Error reading pin data.\n"); |
Peng Fan | de91c16 | 2017-05-11 17:34:14 +0800 | [diff] [blame] | 58 | devm_kfree(dev, pin_data); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | npins = size / pin_size; |
| 63 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 64 | if (info->flags & IMX8_USE_SCU) { |
| 65 | imx_pinctrl_scu_conf_pins(info, pin_data, npins); |
| 66 | } else { |
| 67 | /* |
| 68 | * Refer to linux documentation for details: |
| 69 | * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt |
| 70 | */ |
| 71 | for (i = 0; i < npins; i++) { |
| 72 | mux_reg = pin_data[j++]; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 73 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 74 | if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg) |
| 75 | mux_reg = -1; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 76 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 77 | if (info->flags & SHARE_MUX_CONF_REG) { |
| 78 | conf_reg = mux_reg; |
| 79 | } else { |
| 80 | conf_reg = pin_data[j++]; |
| 81 | if (!(info->flags & ZERO_OFFSET_VALID) && |
| 82 | !conf_reg) |
| 83 | conf_reg = -1; |
| 84 | } |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 85 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 86 | if ((mux_reg == -1) || (conf_reg == -1)) { |
| 87 | dev_err(dev, "Error mux_reg or conf_reg\n"); |
| 88 | devm_kfree(dev, pin_data); |
| 89 | return -EINVAL; |
| 90 | } |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 91 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 92 | input_reg = pin_data[j++]; |
| 93 | mux_mode = pin_data[j++]; |
| 94 | input_val = pin_data[j++]; |
| 95 | config_val = pin_data[j++]; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 96 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 97 | dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, " |
| 98 | "input_reg 0x%x, mux_mode 0x%x, " |
| 99 | "input_val 0x%x, config_val 0x%x\n", |
| 100 | mux_reg, conf_reg, input_reg, mux_mode, |
| 101 | input_val, config_val); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 102 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 103 | if (config_val & IMX_PAD_SION) |
| 104 | mux_mode |= IOMUXC_CONFIG_SION; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 105 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 106 | config_val &= ~IMX_PAD_SION; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 107 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 108 | /* Set Mux */ |
| 109 | if (info->flags & SHARE_MUX_CONF_REG) { |
| 110 | clrsetbits_le32(info->base + mux_reg, |
| 111 | info->mux_mask, |
| 112 | mux_mode << mux_shift); |
| 113 | } else { |
| 114 | writel(mux_mode, info->base + mux_reg); |
| 115 | } |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 116 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 117 | dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n", |
| 118 | mux_reg, mux_mode); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 119 | |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 120 | /* |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 121 | * Set select input |
| 122 | * |
| 123 | * If the select input value begins with 0xff, |
| 124 | * it's a quirky select input and the value should |
| 125 | * be interpreted as below. |
| 126 | * 31 23 15 7 0 |
| 127 | * | 0xff | shift | width | select | |
| 128 | * It's used to work around the problem that the |
| 129 | * select input for some pin is not implemented in |
| 130 | * the select input register but in some general |
| 131 | * purpose register. We encode the select input |
| 132 | * value, width and shift of the bit field into |
| 133 | * input_val cell of pin function ID in device tree, |
| 134 | * and then decode them here for setting up the select |
| 135 | * input bits in general purpose register. |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 136 | */ |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 137 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 138 | if (input_val >> 24 == 0xff) { |
| 139 | u32 val = input_val; |
| 140 | u8 select = val & 0xff; |
| 141 | u8 width = (val >> 8) & 0xff; |
| 142 | u8 shift = (val >> 16) & 0xff; |
| 143 | u32 mask = ((1 << width) - 1) << shift; |
| 144 | /* |
| 145 | * The input_reg[i] here is actually some |
| 146 | * IOMUXC general purpose register, not |
| 147 | * regular select input register. |
| 148 | */ |
| 149 | val = readl(info->base + input_reg); |
| 150 | val &= ~mask; |
| 151 | val |= select << shift; |
| 152 | writel(val, info->base + input_reg); |
| 153 | } else if (input_reg) { |
| 154 | /* |
| 155 | * Regular select input register can never be |
| 156 | * at offset 0, and we only print register |
| 157 | * value for regular case. |
| 158 | */ |
| 159 | if (info->input_sel_base) |
| 160 | writel(input_val, |
| 161 | info->input_sel_base + |
| 162 | input_reg); |
| 163 | else |
| 164 | writel(input_val, |
| 165 | info->base + input_reg); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 166 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 167 | dev_dbg(dev, "select_input: offset 0x%x val " |
| 168 | "0x%x\n", input_reg, input_val); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 171 | /* Set config */ |
| 172 | if (!(config_val & IMX_NO_PAD_CTL)) { |
| 173 | if (info->flags & SHARE_MUX_CONF_REG) { |
| 174 | clrsetbits_le32(info->base + conf_reg, |
| 175 | ~info->mux_mask, |
| 176 | config_val); |
| 177 | } else { |
| 178 | writel(config_val, |
| 179 | info->base + conf_reg); |
| 180 | } |
| 181 | |
| 182 | dev_dbg(dev, "write config: offset 0x%x val " |
| 183 | "0x%x\n", conf_reg, config_val); |
| 184 | } |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Peng Fan | de91c16 | 2017-05-11 17:34:14 +0800 | [diff] [blame] | 188 | devm_kfree(dev, pin_data); |
| 189 | |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | const struct pinctrl_ops imx_pinctrl_ops = { |
| 194 | .set_state = imx_pinctrl_set_state, |
| 195 | }; |
| 196 | |
| 197 | int imx_pinctrl_probe(struct udevice *dev, |
| 198 | struct imx_pinctrl_soc_info *info) |
| 199 | { |
| 200 | struct imx_pinctrl_priv *priv = dev_get_priv(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 201 | int node = dev_of_offset(dev), ret; |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 202 | struct fdtdec_phandle_args arg; |
| 203 | fdt_addr_t addr; |
| 204 | fdt_size_t size; |
| 205 | |
| 206 | if (!info) { |
| 207 | dev_err(dev, "wrong pinctrl info\n"); |
| 208 | return -EINVAL; |
| 209 | } |
| 210 | |
| 211 | priv->dev = dev; |
| 212 | priv->info = info; |
| 213 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 214 | if (info->flags & IMX8_USE_SCU) |
| 215 | return 0; |
| 216 | |
Peng Fan | 3dd874c | 2019-08-06 10:05:48 +0000 | [diff] [blame^] | 217 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 218 | if (addr == FDT_ADDR_T_NONE) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | info->base = map_sysmem(addr, size); |
| 222 | if (!info->base) |
| 223 | return -ENOMEM; |
| 224 | priv->info = info; |
| 225 | |
Peng Fan | f70bf2b | 2017-02-22 16:21:49 +0800 | [diff] [blame] | 226 | info->mux_mask = fdtdec_get_int(gd->fdt_blob, node, "fsl,mux_mask", 0); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 227 | /* |
| 228 | * Refer to linux documentation for details: |
| 229 | * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt |
| 230 | */ |
| 231 | if (fdtdec_get_bool(gd->fdt_blob, node, "fsl,input-sel")) { |
| 232 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 233 | node, "fsl,input-sel", |
| 234 | NULL, 0, 0, &arg); |
| 235 | if (ret) { |
| 236 | dev_err(dev, "iomuxc fsl,input-sel property not found\n"); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | addr = fdtdec_get_addr_size(gd->fdt_blob, arg.node, "reg", |
| 241 | &size); |
| 242 | if (addr == FDT_ADDR_T_NONE) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | info->input_sel_base = map_sysmem(addr, size); |
| 246 | if (!info->input_sel_base) |
| 247 | return -ENOMEM; |
| 248 | } |
| 249 | |
Stefan Agner | d5675e0 | 2016-10-05 15:27:04 -0700 | [diff] [blame] | 250 | dev_dbg(dev, "initialized IMX pinctrl driver\n"); |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int imx_pinctrl_remove(struct udevice *dev) |
| 256 | { |
| 257 | struct imx_pinctrl_priv *priv = dev_get_priv(dev); |
| 258 | struct imx_pinctrl_soc_info *info = priv->info; |
| 259 | |
Peng Fan | e84d11f | 2018-10-18 14:28:28 +0200 | [diff] [blame] | 260 | if (info->flags & IMX8_USE_SCU) |
| 261 | return 0; |
| 262 | |
Peng Fan | e2fd36cc | 2016-02-03 10:06:07 +0800 | [diff] [blame] | 263 | if (info->input_sel_base) |
| 264 | unmap_sysmem(info->input_sel_base); |
| 265 | if (info->base) |
| 266 | unmap_sysmem(info->base); |
| 267 | |
| 268 | return 0; |
| 269 | } |