blob: 474c38a0497601c0aed35f178fbf951f10fa2321 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fane2fd36cc2016-02-03 10:06:07 +08002/*
3 * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
Peng Fane2fd36cc2016-02-03 10:06:07 +08004 */
5
6#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -07007#include <malloc.h>
Peng Fane2fd36cc2016-02-03 10:06:07 +08008#include <mapmem.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070010#include <dm/devres.h>
Peng Fane2fd36cc2016-02-03 10:06:07 +080011#include <linux/io.h>
12#include <linux/err.h>
Simon Glass11c89f32017-05-17 17:18:03 -060013#include <dm.h>
Peng Fane2fd36cc2016-02-03 10:06:07 +080014#include <dm/pinctrl.h>
15
16#include "pinctrl-imx.h"
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)
21{
22 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
23 struct imx_pinctrl_soc_info *info = priv->info;
Simon Glassdd79d6e2017-01-17 16:52:55 -070024 int node = dev_of_offset(config);
Peng Fane2fd36cc2016-02-03 10:06:07 +080025 const struct fdt_property *prop;
26 u32 *pin_data;
27 int npins, size, pin_size;
Ye Lieeebae32019-01-04 09:08:26 +000028 int mux_reg, conf_reg, input_reg;
29 u32 input_val, mux_mode, config_val;
Peng Fanf70bf2b2017-02-22 16:21:49 +080030 u32 mux_shift = info->mux_mask ? ffs(info->mux_mask) - 1 : 0;
Peng Fane2fd36cc2016-02-03 10:06:07 +080031 int i, j = 0;
32
33 dev_dbg(dev, "%s: %s\n", __func__, config->name);
34
Peng Fane84d11f2018-10-18 14:28:28 +020035 if (info->flags & IMX8_USE_SCU)
36 pin_size = SHARE_IMX8_PIN_SIZE;
37 else if (info->flags & SHARE_MUX_CONF_REG)
Peng Fane2fd36cc2016-02-03 10:06:07 +080038 pin_size = SHARE_FSL_PIN_SIZE;
39 else
40 pin_size = FSL_PIN_SIZE;
41
42 prop = fdt_getprop(gd->fdt_blob, node, "fsl,pins", &size);
43 if (!prop) {
44 dev_err(dev, "No fsl,pins property in node %s\n", config->name);
45 return -EINVAL;
46 }
47
48 if (!size || size % pin_size) {
49 dev_err(dev, "Invalid fsl,pins property in node %s\n",
50 config->name);
51 return -EINVAL;
52 }
53
54 pin_data = devm_kzalloc(dev, size, 0);
55 if (!pin_data)
56 return -ENOMEM;
57
58 if (fdtdec_get_int_array(gd->fdt_blob, node, "fsl,pins",
59 pin_data, size >> 2)) {
60 dev_err(dev, "Error reading pin data.\n");
Peng Fande91c162017-05-11 17:34:14 +080061 devm_kfree(dev, pin_data);
Peng Fane2fd36cc2016-02-03 10:06:07 +080062 return -EINVAL;
63 }
64
65 npins = size / pin_size;
66
Peng Fane84d11f2018-10-18 14:28:28 +020067 if (info->flags & IMX8_USE_SCU) {
68 imx_pinctrl_scu_conf_pins(info, pin_data, npins);
69 } else {
70 /*
71 * Refer to linux documentation for details:
72 * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt
73 */
74 for (i = 0; i < npins; i++) {
75 mux_reg = pin_data[j++];
Peng Fane2fd36cc2016-02-03 10:06:07 +080076
Peng Fane84d11f2018-10-18 14:28:28 +020077 if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
78 mux_reg = -1;
Peng Fane2fd36cc2016-02-03 10:06:07 +080079
Peng Fane84d11f2018-10-18 14:28:28 +020080 if (info->flags & SHARE_MUX_CONF_REG) {
81 conf_reg = mux_reg;
82 } else {
83 conf_reg = pin_data[j++];
84 if (!(info->flags & ZERO_OFFSET_VALID) &&
85 !conf_reg)
86 conf_reg = -1;
87 }
Peng Fane2fd36cc2016-02-03 10:06:07 +080088
Peng Fane84d11f2018-10-18 14:28:28 +020089 if ((mux_reg == -1) || (conf_reg == -1)) {
90 dev_err(dev, "Error mux_reg or conf_reg\n");
91 devm_kfree(dev, pin_data);
92 return -EINVAL;
93 }
Peng Fane2fd36cc2016-02-03 10:06:07 +080094
Peng Fane84d11f2018-10-18 14:28:28 +020095 input_reg = pin_data[j++];
96 mux_mode = pin_data[j++];
97 input_val = pin_data[j++];
98 config_val = pin_data[j++];
Peng Fane2fd36cc2016-02-03 10:06:07 +080099
Peng Fane84d11f2018-10-18 14:28:28 +0200100 dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, "
101 "input_reg 0x%x, mux_mode 0x%x, "
102 "input_val 0x%x, config_val 0x%x\n",
103 mux_reg, conf_reg, input_reg, mux_mode,
104 input_val, config_val);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800105
Peng Fane84d11f2018-10-18 14:28:28 +0200106 if (config_val & IMX_PAD_SION)
107 mux_mode |= IOMUXC_CONFIG_SION;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800108
Peng Fane84d11f2018-10-18 14:28:28 +0200109 config_val &= ~IMX_PAD_SION;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800110
Peng Fane84d11f2018-10-18 14:28:28 +0200111 /* Set Mux */
112 if (info->flags & SHARE_MUX_CONF_REG) {
113 clrsetbits_le32(info->base + mux_reg,
114 info->mux_mask,
115 mux_mode << mux_shift);
116 } else {
117 writel(mux_mode, info->base + mux_reg);
118 }
Peng Fane2fd36cc2016-02-03 10:06:07 +0800119
Peng Fane84d11f2018-10-18 14:28:28 +0200120 dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n",
121 mux_reg, mux_mode);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800122
Peng Fane2fd36cc2016-02-03 10:06:07 +0800123 /*
Peng Fane84d11f2018-10-18 14:28:28 +0200124 * Set select input
125 *
126 * If the select input value begins with 0xff,
127 * it's a quirky select input and the value should
128 * be interpreted as below.
129 * 31 23 15 7 0
130 * | 0xff | shift | width | select |
131 * It's used to work around the problem that the
132 * select input for some pin is not implemented in
133 * the select input register but in some general
134 * purpose register. We encode the select input
135 * value, width and shift of the bit field into
136 * input_val cell of pin function ID in device tree,
137 * and then decode them here for setting up the select
138 * input bits in general purpose register.
Peng Fane2fd36cc2016-02-03 10:06:07 +0800139 */
Peng Fane2fd36cc2016-02-03 10:06:07 +0800140
Peng Fane84d11f2018-10-18 14:28:28 +0200141 if (input_val >> 24 == 0xff) {
142 u32 val = input_val;
143 u8 select = val & 0xff;
144 u8 width = (val >> 8) & 0xff;
145 u8 shift = (val >> 16) & 0xff;
146 u32 mask = ((1 << width) - 1) << shift;
147 /*
148 * The input_reg[i] here is actually some
149 * IOMUXC general purpose register, not
150 * regular select input register.
151 */
152 val = readl(info->base + input_reg);
153 val &= ~mask;
154 val |= select << shift;
155 writel(val, info->base + input_reg);
156 } else if (input_reg) {
157 /*
158 * Regular select input register can never be
159 * at offset 0, and we only print register
160 * value for regular case.
161 */
162 if (info->input_sel_base)
163 writel(input_val,
164 info->input_sel_base +
165 input_reg);
166 else
167 writel(input_val,
168 info->base + input_reg);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800169
Peng Fane84d11f2018-10-18 14:28:28 +0200170 dev_dbg(dev, "select_input: offset 0x%x val "
171 "0x%x\n", input_reg, input_val);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800172 }
173
Peng Fane84d11f2018-10-18 14:28:28 +0200174 /* Set config */
175 if (!(config_val & IMX_NO_PAD_CTL)) {
176 if (info->flags & SHARE_MUX_CONF_REG) {
177 clrsetbits_le32(info->base + conf_reg,
178 ~info->mux_mask,
179 config_val);
180 } else {
181 writel(config_val,
182 info->base + conf_reg);
183 }
184
185 dev_dbg(dev, "write config: offset 0x%x val "
186 "0x%x\n", conf_reg, config_val);
187 }
Peng Fane2fd36cc2016-02-03 10:06:07 +0800188 }
189 }
190
Peng Fande91c162017-05-11 17:34:14 +0800191 devm_kfree(dev, pin_data);
192
Peng Fane2fd36cc2016-02-03 10:06:07 +0800193 return 0;
194}
195
196const struct pinctrl_ops imx_pinctrl_ops = {
197 .set_state = imx_pinctrl_set_state,
198};
199
200int imx_pinctrl_probe(struct udevice *dev,
201 struct imx_pinctrl_soc_info *info)
202{
203 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700204 int node = dev_of_offset(dev), ret;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800205 struct fdtdec_phandle_args arg;
206 fdt_addr_t addr;
207 fdt_size_t size;
208
209 if (!info) {
210 dev_err(dev, "wrong pinctrl info\n");
211 return -EINVAL;
212 }
213
214 priv->dev = dev;
215 priv->info = info;
216
Peng Fane84d11f2018-10-18 14:28:28 +0200217 if (info->flags & IMX8_USE_SCU)
218 return 0;
219
Peng Fan3dd874c2019-08-06 10:05:48 +0000220 addr = devfdt_get_addr_size_index(dev, 0, &size);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800221 if (addr == FDT_ADDR_T_NONE)
222 return -EINVAL;
223
224 info->base = map_sysmem(addr, size);
225 if (!info->base)
226 return -ENOMEM;
227 priv->info = info;
228
Peng Fanf70bf2b2017-02-22 16:21:49 +0800229 info->mux_mask = fdtdec_get_int(gd->fdt_blob, node, "fsl,mux_mask", 0);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800230 /*
231 * Refer to linux documentation for details:
232 * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt
233 */
234 if (fdtdec_get_bool(gd->fdt_blob, node, "fsl,input-sel")) {
235 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
236 node, "fsl,input-sel",
237 NULL, 0, 0, &arg);
238 if (ret) {
239 dev_err(dev, "iomuxc fsl,input-sel property not found\n");
240 return -EINVAL;
241 }
242
243 addr = fdtdec_get_addr_size(gd->fdt_blob, arg.node, "reg",
244 &size);
245 if (addr == FDT_ADDR_T_NONE)
246 return -EINVAL;
247
248 info->input_sel_base = map_sysmem(addr, size);
249 if (!info->input_sel_base)
250 return -ENOMEM;
251 }
252
Stefan Agnerd5675e02016-10-05 15:27:04 -0700253 dev_dbg(dev, "initialized IMX pinctrl driver\n");
Peng Fane2fd36cc2016-02-03 10:06:07 +0800254
255 return 0;
256}
257
258int imx_pinctrl_remove(struct udevice *dev)
259{
260 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
261 struct imx_pinctrl_soc_info *info = priv->info;
262
Peng Fane84d11f2018-10-18 14:28:28 +0200263 if (info->flags & IMX8_USE_SCU)
264 return 0;
265
Peng Fane2fd36cc2016-02-03 10:06:07 +0800266 if (info->input_sel_base)
267 unmap_sysmem(info->input_sel_base);
268 if (info->base)
269 unmap_sysmem(info->base);
270
271 return 0;
272}