blob: 48c7bde375269a066ebc0145c5e64db99210b685 [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Peng Fane2fd36cc2016-02-03 10:06:07 +080012#include <linux/io.h>
13#include <linux/err.h>
Simon Glass11c89f32017-05-17 17:18:03 -060014#include <dm.h>
Peng Fane2fd36cc2016-02-03 10:06:07 +080015#include <dm/pinctrl.h>
16
17#include "pinctrl-imx.h"
18
19DECLARE_GLOBAL_DATA_PTR;
20
21static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)
22{
23 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
24 struct imx_pinctrl_soc_info *info = priv->info;
Simon Glassdd79d6e2017-01-17 16:52:55 -070025 int node = dev_of_offset(config);
Peng Fane2fd36cc2016-02-03 10:06:07 +080026 const struct fdt_property *prop;
27 u32 *pin_data;
28 int npins, size, pin_size;
Ye Lieeebae32019-01-04 09:08:26 +000029 int mux_reg, conf_reg, input_reg;
30 u32 input_val, mux_mode, config_val;
Peng Fanf70bf2b2017-02-22 16:21:49 +080031 u32 mux_shift = info->mux_mask ? ffs(info->mux_mask) - 1 : 0;
Peng Fane2fd36cc2016-02-03 10:06:07 +080032 int i, j = 0;
33
34 dev_dbg(dev, "%s: %s\n", __func__, config->name);
35
Peng Fane84d11f2018-10-18 14:28:28 +020036 if (info->flags & IMX8_USE_SCU)
37 pin_size = SHARE_IMX8_PIN_SIZE;
38 else if (info->flags & SHARE_MUX_CONF_REG)
Peng Fane2fd36cc2016-02-03 10:06:07 +080039 pin_size = SHARE_FSL_PIN_SIZE;
40 else
41 pin_size = FSL_PIN_SIZE;
42
43 prop = fdt_getprop(gd->fdt_blob, node, "fsl,pins", &size);
44 if (!prop) {
45 dev_err(dev, "No fsl,pins property in node %s\n", config->name);
46 return -EINVAL;
47 }
48
49 if (!size || size % pin_size) {
50 dev_err(dev, "Invalid fsl,pins property in node %s\n",
51 config->name);
52 return -EINVAL;
53 }
54
55 pin_data = devm_kzalloc(dev, size, 0);
56 if (!pin_data)
57 return -ENOMEM;
58
59 if (fdtdec_get_int_array(gd->fdt_blob, node, "fsl,pins",
60 pin_data, size >> 2)) {
61 dev_err(dev, "Error reading pin data.\n");
Peng Fande91c162017-05-11 17:34:14 +080062 devm_kfree(dev, pin_data);
Peng Fane2fd36cc2016-02-03 10:06:07 +080063 return -EINVAL;
64 }
65
66 npins = size / pin_size;
67
Peng Fane84d11f2018-10-18 14:28:28 +020068 if (info->flags & IMX8_USE_SCU) {
69 imx_pinctrl_scu_conf_pins(info, pin_data, npins);
70 } else {
71 /*
72 * Refer to linux documentation for details:
73 * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt
74 */
75 for (i = 0; i < npins; i++) {
76 mux_reg = pin_data[j++];
Peng Fane2fd36cc2016-02-03 10:06:07 +080077
Peng Fane84d11f2018-10-18 14:28:28 +020078 if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
79 mux_reg = -1;
Peng Fane2fd36cc2016-02-03 10:06:07 +080080
Peng Fane84d11f2018-10-18 14:28:28 +020081 if (info->flags & SHARE_MUX_CONF_REG) {
82 conf_reg = mux_reg;
83 } else {
84 conf_reg = pin_data[j++];
85 if (!(info->flags & ZERO_OFFSET_VALID) &&
86 !conf_reg)
87 conf_reg = -1;
88 }
Peng Fane2fd36cc2016-02-03 10:06:07 +080089
Peng Fane84d11f2018-10-18 14:28:28 +020090 if ((mux_reg == -1) || (conf_reg == -1)) {
91 dev_err(dev, "Error mux_reg or conf_reg\n");
92 devm_kfree(dev, pin_data);
93 return -EINVAL;
94 }
Peng Fane2fd36cc2016-02-03 10:06:07 +080095
Peng Fane84d11f2018-10-18 14:28:28 +020096 input_reg = pin_data[j++];
97 mux_mode = pin_data[j++];
98 input_val = pin_data[j++];
99 config_val = pin_data[j++];
Peng Fane2fd36cc2016-02-03 10:06:07 +0800100
Peng Fane84d11f2018-10-18 14:28:28 +0200101 dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, "
102 "input_reg 0x%x, mux_mode 0x%x, "
103 "input_val 0x%x, config_val 0x%x\n",
104 mux_reg, conf_reg, input_reg, mux_mode,
105 input_val, config_val);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800106
Peng Fane84d11f2018-10-18 14:28:28 +0200107 if (config_val & IMX_PAD_SION)
108 mux_mode |= IOMUXC_CONFIG_SION;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800109
Peng Fane84d11f2018-10-18 14:28:28 +0200110 config_val &= ~IMX_PAD_SION;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800111
Peng Fane84d11f2018-10-18 14:28:28 +0200112 /* Set Mux */
113 if (info->flags & SHARE_MUX_CONF_REG) {
114 clrsetbits_le32(info->base + mux_reg,
115 info->mux_mask,
116 mux_mode << mux_shift);
117 } else {
118 writel(mux_mode, info->base + mux_reg);
119 }
Peng Fane2fd36cc2016-02-03 10:06:07 +0800120
Peng Fane84d11f2018-10-18 14:28:28 +0200121 dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n",
122 mux_reg, mux_mode);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800123
Peng Fane2fd36cc2016-02-03 10:06:07 +0800124 /*
Peng Fane84d11f2018-10-18 14:28:28 +0200125 * Set select input
126 *
127 * If the select input value begins with 0xff,
128 * it's a quirky select input and the value should
129 * be interpreted as below.
130 * 31 23 15 7 0
131 * | 0xff | shift | width | select |
132 * It's used to work around the problem that the
133 * select input for some pin is not implemented in
134 * the select input register but in some general
135 * purpose register. We encode the select input
136 * value, width and shift of the bit field into
137 * input_val cell of pin function ID in device tree,
138 * and then decode them here for setting up the select
139 * input bits in general purpose register.
Peng Fane2fd36cc2016-02-03 10:06:07 +0800140 */
Peng Fane2fd36cc2016-02-03 10:06:07 +0800141
Peng Fane84d11f2018-10-18 14:28:28 +0200142 if (input_val >> 24 == 0xff) {
143 u32 val = input_val;
144 u8 select = val & 0xff;
145 u8 width = (val >> 8) & 0xff;
146 u8 shift = (val >> 16) & 0xff;
147 u32 mask = ((1 << width) - 1) << shift;
148 /*
149 * The input_reg[i] here is actually some
150 * IOMUXC general purpose register, not
151 * regular select input register.
152 */
153 val = readl(info->base + input_reg);
154 val &= ~mask;
155 val |= select << shift;
156 writel(val, info->base + input_reg);
157 } else if (input_reg) {
158 /*
159 * Regular select input register can never be
160 * at offset 0, and we only print register
161 * value for regular case.
162 */
163 if (info->input_sel_base)
164 writel(input_val,
165 info->input_sel_base +
166 input_reg);
167 else
168 writel(input_val,
169 info->base + input_reg);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800170
Peng Fane84d11f2018-10-18 14:28:28 +0200171 dev_dbg(dev, "select_input: offset 0x%x val "
172 "0x%x\n", input_reg, input_val);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800173 }
174
Peng Fane84d11f2018-10-18 14:28:28 +0200175 /* Set config */
176 if (!(config_val & IMX_NO_PAD_CTL)) {
177 if (info->flags & SHARE_MUX_CONF_REG) {
178 clrsetbits_le32(info->base + conf_reg,
179 ~info->mux_mask,
180 config_val);
181 } else {
182 writel(config_val,
183 info->base + conf_reg);
184 }
185
186 dev_dbg(dev, "write config: offset 0x%x val "
187 "0x%x\n", conf_reg, config_val);
188 }
Peng Fane2fd36cc2016-02-03 10:06:07 +0800189 }
190 }
191
Peng Fande91c162017-05-11 17:34:14 +0800192 devm_kfree(dev, pin_data);
193
Peng Fane2fd36cc2016-02-03 10:06:07 +0800194 return 0;
195}
196
197const struct pinctrl_ops imx_pinctrl_ops = {
198 .set_state = imx_pinctrl_set_state,
199};
200
201int imx_pinctrl_probe(struct udevice *dev,
202 struct imx_pinctrl_soc_info *info)
203{
204 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700205 int node = dev_of_offset(dev), ret;
Peng Fane2fd36cc2016-02-03 10:06:07 +0800206 struct fdtdec_phandle_args arg;
207 fdt_addr_t addr;
208 fdt_size_t size;
209
210 if (!info) {
211 dev_err(dev, "wrong pinctrl info\n");
212 return -EINVAL;
213 }
214
215 priv->dev = dev;
216 priv->info = info;
217
Peng Fane84d11f2018-10-18 14:28:28 +0200218 if (info->flags & IMX8_USE_SCU)
219 return 0;
220
Peng Fan3dd874c2019-08-06 10:05:48 +0000221 addr = devfdt_get_addr_size_index(dev, 0, &size);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800222 if (addr == FDT_ADDR_T_NONE)
223 return -EINVAL;
224
225 info->base = map_sysmem(addr, size);
226 if (!info->base)
227 return -ENOMEM;
228 priv->info = info;
229
Peng Fanf70bf2b2017-02-22 16:21:49 +0800230 info->mux_mask = fdtdec_get_int(gd->fdt_blob, node, "fsl,mux_mask", 0);
Peng Fane2fd36cc2016-02-03 10:06:07 +0800231 /*
232 * Refer to linux documentation for details:
233 * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt
234 */
235 if (fdtdec_get_bool(gd->fdt_blob, node, "fsl,input-sel")) {
236 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
237 node, "fsl,input-sel",
238 NULL, 0, 0, &arg);
239 if (ret) {
240 dev_err(dev, "iomuxc fsl,input-sel property not found\n");
241 return -EINVAL;
242 }
243
244 addr = fdtdec_get_addr_size(gd->fdt_blob, arg.node, "reg",
245 &size);
246 if (addr == FDT_ADDR_T_NONE)
247 return -EINVAL;
248
249 info->input_sel_base = map_sysmem(addr, size);
250 if (!info->input_sel_base)
251 return -ENOMEM;
252 }
253
Stefan Agnerd5675e02016-10-05 15:27:04 -0700254 dev_dbg(dev, "initialized IMX pinctrl driver\n");
Peng Fane2fd36cc2016-02-03 10:06:07 +0800255
256 return 0;
257}
258
259int imx_pinctrl_remove(struct udevice *dev)
260{
261 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
262 struct imx_pinctrl_soc_info *info = priv->info;
263
Peng Fane84d11f2018-10-18 14:28:28 +0200264 if (info->flags & IMX8_USE_SCU)
265 return 0;
266
Peng Fane2fd36cc2016-02-03 10:06:07 +0800267 if (info->input_sel_base)
268 unmap_sysmem(info->input_sel_base);
269 if (info->base)
270 unmap_sysmem(info->base);
271
272 return 0;
273}