blob: 6ee108a0120fce8d903cedb08906b32ee47a3482 [file] [log] [blame]
Marek Vasut89a00592025-01-24 15:50:59 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
4 */
5
6#include <malloc.h>
7#include <mapmem.h>
8#include <asm/global_data.h>
9#include <dm/device_compat.h>
10#include <dm/devres.h>
11#include <linux/bitops.h>
12#include <linux/io.h>
13#include <linux/err.h>
14#include <dm.h>
15#include <dm/pinctrl.h>
16
17#include "pinctrl-imx.h"
18
19DECLARE_GLOBAL_DATA_PTR;
20
21int imx_pinctrl_set_state_mmio(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;
25 u32 mux_shift = info->mux_mask ? ffs(info->mux_mask) - 1 : 0;
26 u32 input_val, mux_mode, config_val;
27 int mux_reg, conf_reg, input_reg;
28 int npins, pin_size;
29 int i, j = 0, ret;
30 u32 *pin_data;
31
32 if (info->flags & SHARE_MUX_CONF_REG)
33 pin_size = SHARE_FSL_PIN_SIZE;
34 else
35 pin_size = FSL_PIN_SIZE;
36
37 ret = imx_pinctrl_set_state_common(dev, config, pin_size,
38 &pin_data, &npins);
39 if (ret)
40 return ret;
41
42 /*
43 * Refer to linux documentation for details:
44 * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt
45 */
46 for (i = 0; i < npins; i++) {
47 mux_reg = pin_data[j++];
48
49 if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
50 mux_reg = -1;
51
52 if (info->flags & SHARE_MUX_CONF_REG) {
53 conf_reg = mux_reg;
54 } else {
55 conf_reg = pin_data[j++];
56 if (!(info->flags & ZERO_OFFSET_VALID) &&
57 !conf_reg)
58 conf_reg = -1;
59 }
60
61 if ((mux_reg == -1) || (conf_reg == -1)) {
62 dev_err(dev, "Error mux_reg or conf_reg\n");
63 devm_kfree(dev, pin_data);
64 return -EINVAL;
65 }
66
67 input_reg = pin_data[j++];
68 mux_mode = pin_data[j++];
69 input_val = pin_data[j++];
70 config_val = pin_data[j++];
71
72 dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, input_reg 0x%x, mux_mode 0x%x, input_val 0x%x, config_val 0x%x\n",
73 mux_reg, conf_reg, input_reg, mux_mode,
74 input_val, config_val);
75
76 if (config_val & IMX_PAD_SION)
77 mux_mode |= IOMUXC_CONFIG_SION;
78
79 config_val &= ~IMX_PAD_SION;
80
81 /* Set Mux */
82 if (info->flags & SHARE_MUX_CONF_REG) {
83 clrsetbits_le32(info->base + mux_reg,
84 info->mux_mask,
85 mux_mode << mux_shift);
86 } else {
87 writel(mux_mode, info->base + mux_reg);
88 }
89
90 dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n",
91 mux_reg, mux_mode);
92
93 /*
94 * Set select input
95 *
96 * If the select input value begins with 0xff,
97 * it's a quirky select input and the value should
98 * be interpreted as below.
99 * 31 23 15 7 0
100 * | 0xff | shift | width | select |
101 * It's used to work around the problem that the
102 * select input for some pin is not implemented in
103 * the select input register but in some general
104 * purpose register. We encode the select input
105 * value, width and shift of the bit field into
106 * input_val cell of pin function ID in device tree,
107 * and then decode them here for setting up the select
108 * input bits in general purpose register.
109 */
110
111 if (input_val >> 24 == 0xff) {
112 u32 val = input_val;
113 u8 select = val & 0xff;
114 u8 width = (val >> 8) & 0xff;
115 u8 shift = (val >> 16) & 0xff;
116 u32 mask = ((1 << width) - 1) << shift;
117 /*
118 * The input_reg[i] here is actually some
119 * IOMUXC general purpose register, not
120 * regular select input register.
121 */
122 val = readl(info->base + input_reg);
123 val &= ~mask;
124 val |= select << shift;
125 writel(val, info->base + input_reg);
126 } else if (input_reg) {
127 /*
128 * Regular select input register can never be
129 * at offset 0, and we only print register
130 * value for regular case.
131 */
132 if (info->input_sel_base)
133 writel(input_val,
134 info->input_sel_base +
135 input_reg);
136 else
137 writel(input_val,
138 info->base + input_reg);
139
140 dev_dbg(dev, "select_input: offset 0x%x val 0x%x\n",
141 input_reg, input_val);
142 }
143
144 /* Set config */
145 if (!(config_val & IMX_NO_PAD_CTL)) {
146 if (info->flags & SHARE_MUX_CONF_REG) {
147 clrsetbits_le32(info->base + conf_reg,
148 ~info->mux_mask,
149 config_val);
150 } else {
151 writel(config_val,
152 info->base + conf_reg);
153 }
154
155 dev_dbg(dev, "write config: offset 0x%x val 0x%x\n",
156 conf_reg, config_val);
157 }
158 }
159
160 devm_kfree(dev, pin_data);
161
162 return 0;
163}
164
165
166int imx_pinctrl_probe_mmio(struct udevice *dev)
167{
168 struct imx_pinctrl_soc_info *info =
169 (struct imx_pinctrl_soc_info *)dev_get_driver_data(dev);
170 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
171 struct ofnode_phandle_args arg;
172 ofnode node = dev_ofnode(dev);
173 fdt_addr_t addr;
174 fdt_size_t size;
175 int ret;
176
177 ret = imx_pinctrl_probe_common(dev);
178 if (ret)
179 return ret;
180
181 addr = ofnode_get_addr_size_index(node, 0, &size);
182 if (addr == FDT_ADDR_T_NONE)
183 return -EINVAL;
184
185 info->base = map_sysmem(addr, size);
186 if (!info->base)
187 return -ENOMEM;
188 priv->info = info;
189
190 info->mux_mask = ofnode_read_u32_default(node, "fsl,mux_mask", 0);
191 /*
192 * Refer to linux documentation for details:
193 * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt
194 */
195 if (ofnode_read_bool(node, "fsl,input-sel")) {
196 ret = ofnode_parse_phandle_with_args(node, "fsl,input-sel",
197 NULL, 0, 0, &arg);
198 if (ret) {
199 dev_err(dev, "iomuxc fsl,input-sel property not found\n");
200 return -EINVAL;
201 }
202
203 addr = ofnode_get_addr_size(arg.node, "reg", &size);
204 if (addr == FDT_ADDR_T_NONE)
205 return -EINVAL;
206
207 info->input_sel_base = map_sysmem(addr, size);
208 if (!info->input_sel_base)
209 return -ENOMEM;
210 }
211
212 dev_dbg(dev, "initialized IMX pinctrl driver\n");
213
214 return 0;
215}
216
217int imx_pinctrl_remove_mmio(struct udevice *dev)
218{
219 struct imx_pinctrl_priv *priv = dev_get_priv(dev);
220 struct imx_pinctrl_soc_info *info = priv->info;
221
222 if (info->input_sel_base)
223 unmap_sysmem(info->input_sel_base);
224 if (info->base)
225 unmap_sysmem(info->base);
226
227 return 0;
228}