blob: 82c8feeb7b74be0a1e20813b49ea081c6ff90990 [file] [log] [blame]
David Wue7ae4cf2019-01-02 21:00:55 +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>
8#include <dm/pinctrl.h>
9#include <regmap.h>
10#include <syscon.h>
11
12#include "pinctrl-rockchip.h"
13
14static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
15 {
16 /* uart2dbga_rx */
17 .bank_num = 4,
18 .pin = 8,
19 .func = 2,
20 .route_offset = 0xe21c,
21 .route_val = BIT(16 + 10) | BIT(16 + 11),
22 }, {
23 /* uart2dbgb_rx */
24 .bank_num = 4,
25 .pin = 16,
26 .func = 2,
27 .route_offset = 0xe21c,
28 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
29 }, {
30 /* uart2dbgc_rx */
31 .bank_num = 4,
32 .pin = 19,
33 .func = 1,
34 .route_offset = 0xe21c,
35 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
36 }, {
37 /* pcie_clkreqn */
38 .bank_num = 2,
39 .pin = 26,
40 .func = 2,
41 .route_offset = 0xe21c,
42 .route_val = BIT(16 + 14),
43 }, {
44 /* pcie_clkreqnb */
45 .bank_num = 4,
46 .pin = 24,
47 .func = 1,
48 .route_offset = 0xe21c,
49 .route_val = BIT(16 + 14) | BIT(14),
50 },
51};
52
David Wu54e75702019-04-16 21:50:55 +080053static int rk3399_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
54{
55 struct rockchip_pinctrl_priv *priv = bank->priv;
56 int iomux_num = (pin / 8);
57 struct regmap *regmap;
58 int reg, ret, mask, mux_type;
59 u8 bit;
60 u32 data, route_reg, route_val;
61
62 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
63 ? priv->regmap_pmu : priv->regmap_base;
64
65 /* get basic quadrupel of mux registers and the correct reg inside */
66 mux_type = bank->iomux[iomux_num].type;
67 reg = bank->iomux[iomux_num].offset;
68 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
69
70 if (bank->route_mask & BIT(pin)) {
71 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
72 &route_val)) {
73 ret = regmap_write(regmap, route_reg, route_val);
74 if (ret)
75 return ret;
76 }
77 }
78
79 data = (mask << (bit + 16));
80 data |= (mux & mask) << bit;
81 ret = regmap_write(regmap, reg, data);
82
83 return ret;
84}
85
David Wue7ae4cf2019-01-02 21:00:55 +080086#define RK3399_PULL_GRF_OFFSET 0xe040
87#define RK3399_PULL_PMU_OFFSET 0x40
88
89static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
90 int pin_num, struct regmap **regmap,
91 int *reg, u8 *bit)
92{
93 struct rockchip_pinctrl_priv *priv = bank->priv;
94
95 /* The bank0:16 and bank1:32 pins are located in PMU */
96 if (bank->bank_num == 0 || bank->bank_num == 1) {
97 *regmap = priv->regmap_pmu;
98 *reg = RK3399_PULL_PMU_OFFSET;
99
100 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
101
102 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
103 *bit = pin_num % ROCKCHIP_PULL_PINS_PER_REG;
104 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
105 } else {
106 *regmap = priv->regmap_base;
107 *reg = RK3399_PULL_GRF_OFFSET;
108
109 /* correct the offset, as we're starting with the 3rd bank */
110 *reg -= 0x20;
111 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
112 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
113
114 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
115 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
116 }
117}
118
119static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
120 int pin_num, struct regmap **regmap,
121 int *reg, u8 *bit)
122{
123 struct rockchip_pinctrl_priv *priv = bank->priv;
124 int drv_num = (pin_num / 8);
125
126 /* The bank0:16 and bank1:32 pins are located in PMU */
127 if (bank->bank_num == 0 || bank->bank_num == 1)
128 *regmap = priv->regmap_pmu;
129 else
130 *regmap = priv->regmap_base;
131
132 *reg = bank->drv[drv_num].offset;
133 if (bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO ||
134 bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY)
135 *bit = (pin_num % 8) * 3;
136 else
137 *bit = (pin_num % 8) * 2;
138}
139
David Wu625ab112019-04-16 21:55:26 +0800140static int rk3399_set_drive(struct rockchip_pin_bank *bank,
141 int pin_num, int strength)
142{
143 struct regmap *regmap;
144 int reg, ret;
145 u32 data, rmask_bits, temp;
146 u8 bit;
147 int drv_type = bank->drv[pin_num / 8].drv_type;
148
149 rk3399_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
150 ret = rockchip_translate_drive_value(drv_type, strength);
151 if (ret < 0) {
152 debug("unsupported driver strength %d\n", strength);
153 return ret;
154 }
155
156 switch (drv_type) {
157 case DRV_TYPE_IO_1V8_3V0_AUTO:
158 case DRV_TYPE_IO_3V3_ONLY:
159 rmask_bits = ROCKCHIP_DRV_3BITS_PER_PIN;
160 switch (bit) {
161 case 0 ... 12:
162 /* regular case, nothing to do */
163 break;
164 case 15:
165 /*
166 * drive-strength offset is special, as it is spread
167 * over 2 registers, the bit data[15] contains bit 0
168 * of the value while temp[1:0] contains bits 2 and 1
169 */
170 data = (ret & 0x1) << 15;
171 temp = (ret >> 0x1) & 0x3;
172
173 data |= BIT(31);
174 ret = regmap_write(regmap, reg, data);
175 if (ret)
176 return ret;
177
178 temp |= (0x3 << 16);
179 reg += 0x4;
180 ret = regmap_write(regmap, reg, temp);
181
182 return ret;
183 case 18 ... 21:
184 /* setting fully enclosed in the second register */
185 reg += 4;
186 bit -= 16;
187 break;
188 default:
189 debug("unsupported bit: %d for pinctrl drive type: %d\n",
190 bit, drv_type);
191 return -EINVAL;
192 }
193 break;
194 case DRV_TYPE_IO_DEFAULT:
195 case DRV_TYPE_IO_1V8_OR_3V0:
196 case DRV_TYPE_IO_1V8_ONLY:
197 rmask_bits = ROCKCHIP_DRV_BITS_PER_PIN;
198 break;
199 default:
200 debug("unsupported pinctrl drive type: %d\n",
201 drv_type);
202 return -EINVAL;
203 }
204
205 /* enable the write to the equivalent lower bits */
206 data = ((1 << rmask_bits) - 1) << (bit + 16);
207 data |= (ret << bit);
208 ret = regmap_write(regmap, reg, data);
209
210 return ret;
211}
212
David Wue7ae4cf2019-01-02 21:00:55 +0800213static struct rockchip_pin_bank rk3399_pin_banks[] = {
214 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
215 IOMUX_SOURCE_PMU,
216 IOMUX_SOURCE_PMU,
217 IOMUX_SOURCE_PMU,
218 IOMUX_SOURCE_PMU,
219 DRV_TYPE_IO_1V8_ONLY,
220 DRV_TYPE_IO_1V8_ONLY,
221 DRV_TYPE_IO_DEFAULT,
222 DRV_TYPE_IO_DEFAULT,
223 0x80,
224 0x88,
225 -1,
226 -1,
227 PULL_TYPE_IO_1V8_ONLY,
228 PULL_TYPE_IO_1V8_ONLY,
229 PULL_TYPE_IO_DEFAULT,
230 PULL_TYPE_IO_DEFAULT
231 ),
232 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
233 IOMUX_SOURCE_PMU,
234 IOMUX_SOURCE_PMU,
235 IOMUX_SOURCE_PMU,
236 DRV_TYPE_IO_1V8_OR_3V0,
237 DRV_TYPE_IO_1V8_OR_3V0,
238 DRV_TYPE_IO_1V8_OR_3V0,
239 DRV_TYPE_IO_1V8_OR_3V0,
240 0xa0,
241 0xa8,
242 0xb0,
243 0xb8
244 ),
245 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
246 DRV_TYPE_IO_1V8_OR_3V0,
247 DRV_TYPE_IO_1V8_ONLY,
248 DRV_TYPE_IO_1V8_ONLY,
249 PULL_TYPE_IO_DEFAULT,
250 PULL_TYPE_IO_DEFAULT,
251 PULL_TYPE_IO_1V8_ONLY,
252 PULL_TYPE_IO_1V8_ONLY
253 ),
254 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
255 DRV_TYPE_IO_3V3_ONLY,
256 DRV_TYPE_IO_3V3_ONLY,
257 DRV_TYPE_IO_1V8_OR_3V0
258 ),
259 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
260 DRV_TYPE_IO_1V8_3V0_AUTO,
261 DRV_TYPE_IO_1V8_OR_3V0,
262 DRV_TYPE_IO_1V8_OR_3V0
263 ),
264};
265
266static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
David Wu8541beb2019-04-16 21:50:54 +0800267 .pin_banks = rk3399_pin_banks,
268 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
269 .label = "RK3399-GPIO",
270 .type = RK3399,
271 .grf_mux_offset = 0xe000,
272 .pmu_mux_offset = 0x0,
273 .grf_drv_offset = 0xe100,
274 .pmu_drv_offset = 0x80,
275 .iomux_routes = rk3399_mux_route_data,
276 .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
David Wu54e75702019-04-16 21:50:55 +0800277 .set_mux = rk3399_set_mux,
David Wu8541beb2019-04-16 21:50:54 +0800278 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
David Wu625ab112019-04-16 21:55:26 +0800279 .set_drive = rk3399_set_drive,
David Wue7ae4cf2019-01-02 21:00:55 +0800280};
281
282static const struct udevice_id rk3399_pinctrl_ids[] = {
283 {
284 .compatible = "rockchip,rk3399-pinctrl",
285 .data = (ulong)&rk3399_pin_ctrl
286 },
287 { }
288};
289
290U_BOOT_DRIVER(pinctrl_rk3399) = {
291 .name = "rockchip_rk3399_pinctrl",
292 .id = UCLASS_PINCTRL,
293 .of_match = rk3399_pinctrl_ids,
294 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
295 .ops = &rockchip_pinctrl_ops,
296#if !CONFIG_IS_ENABLED(OF_PLATDATA)
297 .bind = dm_scan_fdt_dev,
298#endif
299 .probe = rockchip_pinctrl_probe,
300};