blob: 0817f7136c4fa3cf640e4c53bbba2523f8fb4b32 [file] [log] [blame]
David Wufd2fdf72019-12-03 19:26:50 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
David Wufd2fdf72019-12-03 19:26:50 +08009#include <dm/pinctrl.h>
10#include <regmap.h>
11#include <syscon.h>
12
13#include "pinctrl-rockchip.h"
14
15static struct rockchip_mux_recalced_data rk3308_mux_recalced_data[] = {
16 {
17 .num = 1,
18 .pin = 14,
19 .reg = 0x28,
20 .bit = 12,
21 .mask = 0xf
22 }, {
23 .num = 1,
24 .pin = 15,
25 .reg = 0x2c,
26 .bit = 0,
27 .mask = 0x3
28 }, {
29 .num = 1,
30 .pin = 18,
31 .reg = 0x30,
32 .bit = 4,
33 .mask = 0xf
34 }, {
35 .num = 1,
36 .pin = 19,
37 .reg = 0x30,
38 .bit = 8,
39 .mask = 0xf
40 }, {
41 .num = 1,
42 .pin = 20,
43 .reg = 0x30,
44 .bit = 12,
45 .mask = 0xf
46 }, {
47 .num = 1,
48 .pin = 21,
49 .reg = 0x34,
50 .bit = 0,
51 .mask = 0xf
52 }, {
53 .num = 1,
54 .pin = 22,
55 .reg = 0x34,
56 .bit = 4,
57 .mask = 0xf
58 }, {
59 .num = 1,
60 .pin = 23,
61 .reg = 0x34,
62 .bit = 8,
63 .mask = 0xf
64 }, {
65 .num = 3,
66 .pin = 12,
67 .reg = 0x68,
68 .bit = 8,
69 .mask = 0xf
70 }, {
71 .num = 3,
72 .pin = 13,
73 .reg = 0x68,
74 .bit = 12,
75 .mask = 0xf
76 }, {
77 .num = 2,
78 .pin = 2,
79 .reg = 0x608,
80 .bit = 0,
81 .mask = 0x7
82 }, {
83 .num = 2,
84 .pin = 3,
85 .reg = 0x608,
86 .bit = 4,
87 .mask = 0x7
88 }, {
89 .num = 2,
90 .pin = 16,
91 .reg = 0x610,
92 .bit = 8,
93 .mask = 0x7
94 }, {
95 .num = 3,
96 .pin = 10,
97 .reg = 0x610,
98 .bit = 0,
99 .mask = 0x7
100 }, {
101 .num = 3,
102 .pin = 11,
103 .reg = 0x610,
104 .bit = 4,
105 .mask = 0x7
106 },
107};
108
109static struct rockchip_mux_route_data rk3308_mux_route_data[] = {
110 {
111 /* rtc_clk */
112 .bank_num = 0,
113 .pin = 19,
114 .func = 1,
115 .route_offset = 0x314,
116 .route_val = BIT(16 + 0) | BIT(0),
117 }, {
118 /* uart2_rxm0 */
119 .bank_num = 1,
120 .pin = 22,
121 .func = 2,
122 .route_offset = 0x314,
123 .route_val = BIT(16 + 2) | BIT(16 + 3),
124 }, {
125 /* uart2_rxm1 */
126 .bank_num = 4,
127 .pin = 26,
128 .func = 2,
129 .route_offset = 0x314,
130 .route_val = BIT(16 + 2) | BIT(16 + 3) | BIT(2),
131 }, {
132 /* i2c3_sdam0 */
133 .bank_num = 0,
134 .pin = 15,
135 .func = 2,
136 .route_offset = 0x608,
137 .route_val = BIT(16 + 8) | BIT(16 + 9),
138 }, {
139 /* i2c3_sdam1 */
140 .bank_num = 3,
141 .pin = 12,
142 .func = 2,
143 .route_offset = 0x608,
144 .route_val = BIT(16 + 8) | BIT(16 + 9) | BIT(8),
145 }, {
146 /* i2c3_sdam2 */
147 .bank_num = 2,
148 .pin = 0,
149 .func = 3,
150 .route_offset = 0x608,
151 .route_val = BIT(16 + 8) | BIT(16 + 9) | BIT(9),
152 }, {
153 /* i2s-8ch-1-sclktxm0 */
154 .bank_num = 1,
155 .pin = 3,
156 .func = 2,
157 .route_offset = 0x308,
158 .route_val = BIT(16 + 3),
159 }, {
160 /* i2s-8ch-1-sclkrxm0 */
161 .bank_num = 1,
162 .pin = 4,
163 .func = 2,
164 .route_offset = 0x308,
165 .route_val = BIT(16 + 3),
166 }, {
167 /* i2s-8ch-1-sclktxm1 */
168 .bank_num = 1,
169 .pin = 13,
170 .func = 2,
171 .route_offset = 0x308,
172 .route_val = BIT(16 + 3) | BIT(3),
173 }, {
174 /* i2s-8ch-1-sclkrxm1 */
175 .bank_num = 1,
176 .pin = 14,
177 .func = 2,
178 .route_offset = 0x308,
179 .route_val = BIT(16 + 3) | BIT(3),
180 }, {
181 /* pdm-clkm0 */
182 .bank_num = 1,
183 .pin = 4,
184 .func = 3,
185 .route_offset = 0x308,
186 .route_val = BIT(16 + 12) | BIT(16 + 13),
187 }, {
188 /* pdm-clkm1 */
189 .bank_num = 1,
190 .pin = 14,
191 .func = 4,
192 .route_offset = 0x308,
193 .route_val = BIT(16 + 12) | BIT(16 + 13) | BIT(12),
194 }, {
195 /* pdm-clkm2 */
196 .bank_num = 2,
197 .pin = 6,
198 .func = 2,
199 .route_offset = 0x308,
200 .route_val = BIT(16 + 12) | BIT(16 + 13) | BIT(13),
201 }, {
202 /* pdm-clkm-m2 */
203 .bank_num = 2,
204 .pin = 4,
205 .func = 3,
206 .route_offset = 0x600,
207 .route_val = BIT(16 + 2) | BIT(2),
208 }, {
209 /* spi1_miso */
210 .bank_num = 3,
211 .pin = 10,
212 .func = 3,
213 .route_offset = 0x314,
214 .route_val = BIT(16 + 9),
215 }, {
216 /* spi1_miso_m1 */
217 .bank_num = 2,
218 .pin = 4,
219 .func = 2,
220 .route_offset = 0x314,
221 .route_val = BIT(16 + 9) | BIT(9),
222 }, {
223 /* mac_rxd0_m0 */
224 .bank_num = 1,
225 .pin = 20,
226 .func = 3,
227 .route_offset = 0x314,
228 .route_val = BIT(16 + 14),
229 }, {
230 /* mac_rxd0_m1 */
231 .bank_num = 4,
232 .pin = 2,
233 .func = 2,
234 .route_offset = 0x314,
235 .route_val = BIT(16 + 14) | BIT(14),
236 }, {
237 /* uart3_rx */
238 .bank_num = 3,
239 .pin = 12,
240 .func = 4,
241 .route_offset = 0x314,
242 .route_val = BIT(16 + 15),
243 }, {
244 /* uart3_rx_m1 */
245 .bank_num = 0,
246 .pin = 17,
247 .func = 3,
248 .route_offset = 0x314,
249 .route_val = BIT(16 + 15) | BIT(15),
250 },
251};
252
253static int rk3308_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
254{
255 struct rockchip_pinctrl_priv *priv = bank->priv;
256 int iomux_num = (pin / 8);
257 struct regmap *regmap;
258 int reg, ret, mask, mux_type;
259 u8 bit;
260 u32 data, route_reg, route_val;
261
262 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
263 ? priv->regmap_pmu : priv->regmap_base;
264
265 /* get basic quadrupel of mux registers and the correct reg inside */
266 mux_type = bank->iomux[iomux_num].type;
267 reg = bank->iomux[iomux_num].offset;
268 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
269
270 if (bank->recalced_mask & BIT(pin))
271 rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
272
273 if (bank->route_mask & BIT(pin)) {
274 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
275 &route_val)) {
276 ret = regmap_write(regmap, route_reg, route_val);
277 if (ret)
278 return ret;
279 }
280 }
281
282 data = (mask << (bit + 16));
283 data |= (mux & mask) << bit;
284 ret = regmap_write(regmap, reg, data);
285
286 return ret;
287}
288
289#define RK3308_PULL_OFFSET 0xa0
290
291static void rk3308_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
292 int pin_num, struct regmap **regmap,
293 int *reg, u8 *bit)
294{
295 struct rockchip_pinctrl_priv *priv = bank->priv;
296
297 *regmap = priv->regmap_base;
298 *reg = RK3308_PULL_OFFSET;
299 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
300 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
301
302 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
303 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
304}
305
306static int rk3308_set_pull(struct rockchip_pin_bank *bank,
307 int pin_num, int pull)
308{
309 struct regmap *regmap;
310 int reg, ret;
311 u8 bit, type;
312 u32 data;
313
314 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
315 return -ENOTSUPP;
316
317 rk3308_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
318 type = bank->pull_type[pin_num / 8];
319 ret = rockchip_translate_pull_value(type, pull);
320 if (ret < 0) {
321 debug("unsupported pull setting %d\n", pull);
322 return ret;
323 }
324
325 /* enable the write to the equivalent lower bits */
326 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
327 data |= (ret << bit);
328
329 ret = regmap_write(regmap, reg, data);
330
331 return ret;
332}
333
334#define RK3308_DRV_GRF_OFFSET 0x100
335
336static void rk3308_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
337 int pin_num, struct regmap **regmap,
338 int *reg, u8 *bit)
339{
340 struct rockchip_pinctrl_priv *priv = bank->priv;
341
342 *regmap = priv->regmap_base;
343 *reg = RK3308_DRV_GRF_OFFSET;
344 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
345 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
346
347 *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG);
348 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
349}
350
351static int rk3308_set_drive(struct rockchip_pin_bank *bank,
352 int pin_num, int strength)
353{
354 struct regmap *regmap;
355 int reg, ret;
356 u32 data;
357 u8 bit;
358 int type = bank->drv[pin_num / 8].drv_type;
359
360 rk3308_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
361 ret = rockchip_translate_drive_value(type, strength);
362 if (ret < 0) {
363 debug("unsupported driver strength %d\n", strength);
364 return ret;
365 }
366
367 /* enable the write to the equivalent lower bits */
368 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
369 data |= (ret << bit);
370 ret = regmap_write(regmap, reg, data);
371 return ret;
372}
373
374#define RK3308_SCHMITT_PINS_PER_REG 8
375#define RK3308_SCHMITT_BANK_STRIDE 16
376#define RK3308_SCHMITT_GRF_OFFSET 0x1a0
377
378static int rk3308_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
379 int pin_num,
380 struct regmap **regmap,
381 int *reg, u8 *bit)
382{
383 struct rockchip_pinctrl_priv *priv = bank->priv;
384
385 *regmap = priv->regmap_base;
386 *reg = RK3308_SCHMITT_GRF_OFFSET;
387
388 *reg += bank->bank_num * RK3308_SCHMITT_BANK_STRIDE;
389 *reg += ((pin_num / RK3308_SCHMITT_PINS_PER_REG) * 4);
390 *bit = pin_num % RK3308_SCHMITT_PINS_PER_REG;
391
392 return 0;
393}
394
395static int rk3308_set_schmitt(struct rockchip_pin_bank *bank,
396 int pin_num, int enable)
397{
398 struct regmap *regmap;
399 int reg;
400 u8 bit;
401 u32 data;
402
403 rk3308_calc_schmitt_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
404 /* enable the write to the equivalent lower bits */
405 data = BIT(bit + 16) | (enable << bit);
406
407 return regmap_write(regmap, reg, data);
408}
409
410static struct rockchip_pin_bank rk3308_pin_banks[] = {
411 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_8WIDTH_2BIT,
412 IOMUX_8WIDTH_2BIT,
413 IOMUX_8WIDTH_2BIT,
414 IOMUX_8WIDTH_2BIT),
415 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_8WIDTH_2BIT,
416 IOMUX_8WIDTH_2BIT,
417 IOMUX_8WIDTH_2BIT,
418 IOMUX_8WIDTH_2BIT),
419 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_8WIDTH_2BIT,
420 IOMUX_8WIDTH_2BIT,
421 IOMUX_8WIDTH_2BIT,
422 IOMUX_8WIDTH_2BIT),
423 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_8WIDTH_2BIT,
424 IOMUX_8WIDTH_2BIT,
425 IOMUX_8WIDTH_2BIT,
426 IOMUX_8WIDTH_2BIT),
427 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_8WIDTH_2BIT,
428 IOMUX_8WIDTH_2BIT,
429 IOMUX_8WIDTH_2BIT,
430 IOMUX_8WIDTH_2BIT),
431};
432
433static struct rockchip_pin_ctrl rk3308_pin_ctrl = {
434 .pin_banks = rk3308_pin_banks,
435 .nr_banks = ARRAY_SIZE(rk3308_pin_banks),
436 .grf_mux_offset = 0x0,
437 .iomux_recalced = rk3308_mux_recalced_data,
438 .niomux_recalced = ARRAY_SIZE(rk3308_mux_recalced_data),
439 .iomux_routes = rk3308_mux_route_data,
440 .niomux_routes = ARRAY_SIZE(rk3308_mux_route_data),
441 .set_mux = rk3308_set_mux,
442 .set_drive = rk3308_set_drive,
443 .set_pull = rk3308_set_pull,
444 .set_schmitt = rk3308_set_schmitt,
445};
446
447static const struct udevice_id rk3308_pinctrl_ids[] = {
448 {
449 .compatible = "rockchip,rk3308-pinctrl",
450 .data = (ulong)&rk3308_pin_ctrl
451 },
452 { }
453};
454
455U_BOOT_DRIVER(pinctrl_rk3308) = {
456 .name = "rockchip_rk3308_pinctrl",
457 .id = UCLASS_PINCTRL,
458 .of_match = rk3308_pinctrl_ids,
459 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
460 .ops = &rockchip_pinctrl_ops,
461#if !CONFIG_IS_ENABLED(OF_PLATDATA)
462 .bind = dm_scan_fdt_dev,
463#endif
464 .probe = rockchip_pinctrl_probe,
465};