blob: 32a46d53f068e3b35875a8cb037a47d0059c3177 [file] [log] [blame]
Stephen Warrene2969952014-03-21 12:28:54 -06001/*
2 * Copyright (c) 2010-2013, NVIDIA CORPORATION. All rights reserved.
3 * Copyright (c) 2011 The Chromium OS Authors.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <asm/arch/pinmux.h>
11
12/* return 1 if a pingrp is in range */
Stephen Warrendfb42fc2014-03-21 12:28:56 -060013#define pmux_pingrp_isvalid(pin) (((pin) >= 0) && ((pin) < PMUX_PINGRP_COUNT))
Stephen Warrene2969952014-03-21 12:28:54 -060014
15/* return 1 if a pmux_func is in range */
16#define pmux_func_isvalid(func) \
17 ((((func) >= 0) && ((func) < PMUX_FUNC_COUNT)) || \
18 (((func) >= PMUX_FUNC_RSVD1) && ((func) <= PMUX_FUNC_RSVD4)))
19
20/* return 1 if a pin_pupd_is in range */
21#define pmux_pin_pupd_isvalid(pupd) \
22 (((pupd) >= PMUX_PULL_NORMAL) && ((pupd) <= PMUX_PULL_UP))
23
24/* return 1 if a pin_tristate_is in range */
25#define pmux_pin_tristate_isvalid(tristate) \
26 (((tristate) >= PMUX_TRI_NORMAL) && ((tristate) <= PMUX_TRI_TRISTATE))
27
28#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
29/* return 1 if a pin_io_is in range */
30#define pmux_pin_io_isvalid(io) \
31 (((io) >= PMUX_PIN_OUTPUT) && ((io) <= PMUX_PIN_INPUT))
32
33/* return 1 if a pin_lock is in range */
34#define pmux_pin_lock_isvalid(lock) \
35 (((lock) >= PMUX_PIN_LOCK_DISABLE) && ((lock) <= PMUX_PIN_LOCK_ENABLE))
36
37/* return 1 if a pin_od is in range */
38#define pmux_pin_od_isvalid(od) \
39 (((od) >= PMUX_PIN_OD_DISABLE) && ((od) <= PMUX_PIN_OD_ENABLE))
40
41/* return 1 if a pin_ioreset_is in range */
42#define pmux_pin_ioreset_isvalid(ioreset) \
43 (((ioreset) >= PMUX_PIN_IO_RESET_DISABLE) && \
44 ((ioreset) <= PMUX_PIN_IO_RESET_ENABLE))
45
46#ifdef TEGRA_PMX_HAS_RCV_SEL
47/* return 1 if a pin_rcv_sel_is in range */
48#define pmux_pin_rcv_sel_isvalid(rcv_sel) \
49 (((rcv_sel) >= PMUX_PIN_RCV_SEL_NORMAL) && \
50 ((rcv_sel) <= PMUX_PIN_RCV_SEL_HIGH))
51#endif /* TEGRA_PMX_HAS_RCV_SEL */
52#endif /* TEGRA_PMX_HAS_PIN_IO_BIT_ETC */
53
54#define _R(offset) (u32 *)(NV_PA_APB_MISC_BASE + (offset))
55
56#if defined(CONFIG_TEGRA20)
57
58#define MUX_REG(grp) _R(0x80 + ((tegra_soc_pingroups[grp].ctl_id / 16) * 4))
59#define MUX_SHIFT(grp) ((tegra_soc_pingroups[grp].ctl_id % 16) * 2)
60
61#define PULL_REG(grp) _R(0xa0 + ((tegra_soc_pingroups[grp].pull_id / 16) * 4))
62#define PULL_SHIFT(grp) ((tegra_soc_pingroups[grp].pull_id % 16) * 2)
63
64#define TRI_REG(grp) _R(0x14 + (((grp) / 32) * 4))
65#define TRI_SHIFT(grp) ((grp) % 32)
66
67#else
68
69#define REG(pin) _R(0x3000 + ((pin) * 4))
70
71#define MUX_REG(pin) REG(pin)
72#define MUX_SHIFT(pin) 0
73
74#define PULL_REG(pin) REG(pin)
75#define PULL_SHIFT(pin) 2
76
77#define TRI_REG(pin) REG(pin)
78#define TRI_SHIFT(pin) 4
79
80#endif /* CONFIG_TEGRA20 */
81
82#define DRV_REG(group) _R(0x868 + ((group) * 4))
83
84#define IO_SHIFT 5
85#define OD_SHIFT 6
86#define LOCK_SHIFT 7
87#define IO_RESET_SHIFT 8
88#define RCV_SEL_SHIFT 9
89
90void pinmux_set_func(enum pmux_pingrp pin, enum pmux_func func)
91{
92 u32 *reg = MUX_REG(pin);
93 int i, mux = -1;
94 u32 val;
95
96 /* Error check on pin and func */
97 assert(pmux_pingrp_isvalid(pin));
98 assert(pmux_func_isvalid(func));
99
100 if (func & PMUX_FUNC_RSVD1) {
101 mux = func & 3;
102 } else {
103 /* Search for the appropriate function */
104 for (i = 0; i < 4; i++) {
105 if (tegra_soc_pingroups[pin].funcs[i] == func) {
106 mux = i;
107 break;
108 }
109 }
110 }
111 assert(mux != -1);
112
113 val = readl(reg);
114 val &= ~(3 << MUX_SHIFT(pin));
115 val |= (mux << MUX_SHIFT(pin));
116 writel(val, reg);
117}
118
119void pinmux_set_pullupdown(enum pmux_pingrp pin, enum pmux_pull pupd)
120{
121 u32 *reg = PULL_REG(pin);
122 u32 val;
123
124 /* Error check on pin and pupd */
125 assert(pmux_pingrp_isvalid(pin));
126 assert(pmux_pin_pupd_isvalid(pupd));
127
128 val = readl(reg);
129 val &= ~(3 << PULL_SHIFT(pin));
130 val |= (pupd << PULL_SHIFT(pin));
131 writel(val, reg);
132}
133
Stephen Warrena45fa432014-03-21 12:28:55 -0600134static void pinmux_set_tristate(enum pmux_pingrp pin, int tri)
Stephen Warrene2969952014-03-21 12:28:54 -0600135{
136 u32 *reg = TRI_REG(pin);
137 u32 val;
138
139 /* Error check on pin */
140 assert(pmux_pingrp_isvalid(pin));
141 assert(pmux_pin_tristate_isvalid(tri));
142
143 val = readl(reg);
144 if (tri == PMUX_TRI_TRISTATE)
145 val |= (1 << TRI_SHIFT(pin));
146 else
147 val &= ~(1 << TRI_SHIFT(pin));
148 writel(val, reg);
149}
150
151void pinmux_tristate_enable(enum pmux_pingrp pin)
152{
153 pinmux_set_tristate(pin, PMUX_TRI_TRISTATE);
154}
155
156void pinmux_tristate_disable(enum pmux_pingrp pin)
157{
158 pinmux_set_tristate(pin, PMUX_TRI_NORMAL);
159}
160
161#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
162void pinmux_set_io(enum pmux_pingrp pin, enum pmux_pin_io io)
163{
164 u32 *reg = REG(pin);
165 u32 val;
166
167 if (io == PMUX_PIN_NONE)
168 return;
169
170 /* Error check on pin and io */
171 assert(pmux_pingrp_isvalid(pin));
172 assert(pmux_pin_io_isvalid(io));
173
174 val = readl(reg);
175 if (io == PMUX_PIN_INPUT)
176 val |= (io & 1) << IO_SHIFT;
177 else
178 val &= ~(1 << IO_SHIFT);
179 writel(val, reg);
180}
181
182static void pinmux_set_lock(enum pmux_pingrp pin, enum pmux_pin_lock lock)
183{
184 u32 *reg = REG(pin);
185 u32 val;
186
187 if (lock == PMUX_PIN_LOCK_DEFAULT)
188 return;
189
190 /* Error check on pin and lock */
191 assert(pmux_pingrp_isvalid(pin));
192 assert(pmux_pin_lock_isvalid(lock));
193
194 val = readl(reg);
195 if (lock == PMUX_PIN_LOCK_ENABLE) {
196 val |= (1 << LOCK_SHIFT);
197 } else {
198 if (val & (1 << LOCK_SHIFT))
199 printf("%s: Cannot clear LOCK bit!\n", __func__);
200 val &= ~(1 << LOCK_SHIFT);
201 }
202 writel(val, reg);
203
204 return;
205}
206
207static void pinmux_set_od(enum pmux_pingrp pin, enum pmux_pin_od od)
208{
209 u32 *reg = REG(pin);
210 u32 val;
211
212 if (od == PMUX_PIN_OD_DEFAULT)
213 return;
214
215 /* Error check on pin and od */
216 assert(pmux_pingrp_isvalid(pin));
217 assert(pmux_pin_od_isvalid(od));
218
219 val = readl(reg);
220 if (od == PMUX_PIN_OD_ENABLE)
221 val |= (1 << OD_SHIFT);
222 else
223 val &= ~(1 << OD_SHIFT);
224 writel(val, reg);
225
226 return;
227}
228
229static void pinmux_set_ioreset(enum pmux_pingrp pin,
230 enum pmux_pin_ioreset ioreset)
231{
232 u32 *reg = REG(pin);
233 u32 val;
234
235 if (ioreset == PMUX_PIN_IO_RESET_DEFAULT)
236 return;
237
238 /* Error check on pin and ioreset */
239 assert(pmux_pingrp_isvalid(pin));
240 assert(pmux_pin_ioreset_isvalid(ioreset));
241
242 val = readl(reg);
243 if (ioreset == PMUX_PIN_IO_RESET_ENABLE)
244 val |= (1 << IO_RESET_SHIFT);
245 else
246 val &= ~(1 << IO_RESET_SHIFT);
247 writel(val, reg);
248
249 return;
250}
251
252#ifdef TEGRA_PMX_HAS_RCV_SEL
253static void pinmux_set_rcv_sel(enum pmux_pingrp pin,
254 enum pmux_pin_rcv_sel rcv_sel)
255{
256 u32 *reg = REG(pin);
257 u32 val;
258
259 if (rcv_sel == PMUX_PIN_RCV_SEL_DEFAULT)
260 return;
261
262 /* Error check on pin and rcv_sel */
263 assert(pmux_pingrp_isvalid(pin));
264 assert(pmux_pin_rcv_sel_isvalid(rcv_sel));
265
266 val = readl(reg);
267 if (rcv_sel == PMUX_PIN_RCV_SEL_HIGH)
268 val |= (1 << RCV_SEL_SHIFT);
269 else
270 val &= ~(1 << RCV_SEL_SHIFT);
271 writel(val, reg);
272
273 return;
274}
275#endif /* TEGRA_PMX_HAS_RCV_SEL */
276#endif /* TEGRA_PMX_HAS_PIN_IO_BIT_ETC */
277
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600278static void pinmux_config_pingrp(const struct pmux_pingrp_config *config)
Stephen Warrene2969952014-03-21 12:28:54 -0600279{
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600280 enum pmux_pingrp pin = config->pingrp;
Stephen Warrene2969952014-03-21 12:28:54 -0600281
282 pinmux_set_func(pin, config->func);
283 pinmux_set_pullupdown(pin, config->pull);
284 pinmux_set_tristate(pin, config->tristate);
285#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
286 pinmux_set_io(pin, config->io);
287 pinmux_set_lock(pin, config->lock);
288 pinmux_set_od(pin, config->od);
289 pinmux_set_ioreset(pin, config->ioreset);
290#ifdef TEGRA_PMX_HAS_RCV_SEL
291 pinmux_set_rcv_sel(pin, config->rcv_sel);
292#endif
293#endif
294}
295
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600296void pinmux_config_pingrp_table(const struct pmux_pingrp_config *config,
297 int len)
Stephen Warrene2969952014-03-21 12:28:54 -0600298{
299 int i;
300
301 for (i = 0; i < len; i++)
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600302 pinmux_config_pingrp(&config[i]);
Stephen Warrene2969952014-03-21 12:28:54 -0600303}
304
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600305#ifdef TEGRA_PMX_HAS_DRVGRPS
Stephen Warrene2969952014-03-21 12:28:54 -0600306
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600307#define pmux_drvgrp_isvalid(pd) (((pd) >= 0) && ((pd) < PMUX_DRVGRP_COUNT))
Stephen Warrene2969952014-03-21 12:28:54 -0600308
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600309#define pmux_slw_isvalid(slw) \
310 (((slw) >= PMUX_SLWF_MIN) && ((slw) <= PMUX_SLWF_MAX))
Stephen Warrene2969952014-03-21 12:28:54 -0600311
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600312#define pmux_drv_isvalid(drv) \
313 (((drv) >= PMUX_DRVUP_MIN) && ((drv) <= PMUX_DRVUP_MAX))
Stephen Warrene2969952014-03-21 12:28:54 -0600314
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600315#define pmux_lpmd_isvalid(lpm) \
316 (((lpm) >= PMUX_LPMD_X8) && ((lpm) <= PMUX_LPMD_X))
Stephen Warrene2969952014-03-21 12:28:54 -0600317
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600318#define pmux_schmt_isvalid(schmt) \
319 (((schmt) >= PMUX_SCHMT_DISABLE) && ((schmt) <= PMUX_SCHMT_ENABLE))
Stephen Warrene2969952014-03-21 12:28:54 -0600320
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600321#define pmux_hsm_isvalid(hsm) \
322 (((hsm) >= PMUX_HSM_DISABLE) && ((hsm) <= PMUX_HSM_ENABLE))
Stephen Warrene2969952014-03-21 12:28:54 -0600323
324#define HSM_SHIFT 2
325#define SCHMT_SHIFT 3
326#define LPMD_SHIFT 4
327#define LPMD_MASK (3 << LPMD_SHIFT)
328#define DRVDN_SHIFT 12
329#define DRVDN_MASK (0x7F << DRVDN_SHIFT)
330#define DRVUP_SHIFT 20
331#define DRVUP_MASK (0x7F << DRVUP_SHIFT)
332#define SLWR_SHIFT 28
333#define SLWR_MASK (3 << SLWR_SHIFT)
334#define SLWF_SHIFT 30
335#define SLWF_MASK (3 << SLWF_SHIFT)
336
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600337static void pinmux_set_drvup_slwf(enum pmux_drvgrp grp, int slwf)
Stephen Warrene2969952014-03-21 12:28:54 -0600338{
339 u32 *reg = DRV_REG(grp);
340 u32 val;
341
342 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600343 if (slwf == PMUX_SLWF_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600344 return;
345
346 /* Error check on pad and slwf */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600347 assert(pmux_drvgrp_isvalid(grp));
348 assert(pmux_slw_isvalid(slwf));
Stephen Warrene2969952014-03-21 12:28:54 -0600349
350 val = readl(reg);
351 val &= ~SLWF_MASK;
352 val |= (slwf << SLWF_SHIFT);
353 writel(val, reg);
354
355 return;
356}
357
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600358static void pinmux_set_drvdn_slwr(enum pmux_drvgrp grp, int slwr)
Stephen Warrene2969952014-03-21 12:28:54 -0600359{
360 u32 *reg = DRV_REG(grp);
361 u32 val;
362
363 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600364 if (slwr == PMUX_SLWR_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600365 return;
366
367 /* Error check on pad and slwr */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600368 assert(pmux_drvgrp_isvalid(grp));
369 assert(pmux_slw_isvalid(slwr));
Stephen Warrene2969952014-03-21 12:28:54 -0600370
371 val = readl(reg);
372 val &= ~SLWR_MASK;
373 val |= (slwr << SLWR_SHIFT);
374 writel(val, reg);
375
376 return;
377}
378
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600379static void pinmux_set_drvup(enum pmux_drvgrp grp, int drvup)
Stephen Warrene2969952014-03-21 12:28:54 -0600380{
381 u32 *reg = DRV_REG(grp);
382 u32 val;
383
384 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600385 if (drvup == PMUX_DRVUP_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600386 return;
387
388 /* Error check on pad and drvup */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600389 assert(pmux_drvgrp_isvalid(grp));
390 assert(pmux_drv_isvalid(drvup));
Stephen Warrene2969952014-03-21 12:28:54 -0600391
392 val = readl(reg);
393 val &= ~DRVUP_MASK;
394 val |= (drvup << DRVUP_SHIFT);
395 writel(val, reg);
396
397 return;
398}
399
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600400static void pinmux_set_drvdn(enum pmux_drvgrp grp, int drvdn)
Stephen Warrene2969952014-03-21 12:28:54 -0600401{
402 u32 *reg = DRV_REG(grp);
403 u32 val;
404
405 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600406 if (drvdn == PMUX_DRVDN_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600407 return;
408
409 /* Error check on pad and drvdn */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600410 assert(pmux_drvgrp_isvalid(grp));
411 assert(pmux_drv_isvalid(drvdn));
Stephen Warrene2969952014-03-21 12:28:54 -0600412
413 val = readl(reg);
414 val &= ~DRVDN_MASK;
415 val |= (drvdn << DRVDN_SHIFT);
416 writel(val, reg);
417
418 return;
419}
420
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600421static void pinmux_set_lpmd(enum pmux_drvgrp grp, enum pmux_lpmd lpmd)
Stephen Warrene2969952014-03-21 12:28:54 -0600422{
423 u32 *reg = DRV_REG(grp);
424 u32 val;
425
426 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600427 if (lpmd == PMUX_LPMD_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600428 return;
429
430 /* Error check pad and lpmd value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600431 assert(pmux_drvgrp_isvalid(grp));
432 assert(pmux_lpmd_isvalid(lpmd));
Stephen Warrene2969952014-03-21 12:28:54 -0600433
434 val = readl(reg);
435 val &= ~LPMD_MASK;
436 val |= (lpmd << LPMD_SHIFT);
437 writel(val, reg);
438
439 return;
440}
441
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600442static void pinmux_set_schmt(enum pmux_drvgrp grp, enum pmux_schmt schmt)
Stephen Warrene2969952014-03-21 12:28:54 -0600443{
444 u32 *reg = DRV_REG(grp);
445 u32 val;
446
447 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600448 if (schmt == PMUX_SCHMT_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600449 return;
450
451 /* Error check pad */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600452 assert(pmux_drvgrp_isvalid(grp));
453 assert(pmux_schmt_isvalid(schmt));
Stephen Warrene2969952014-03-21 12:28:54 -0600454
455 val = readl(reg);
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600456 if (schmt == PMUX_SCHMT_ENABLE)
Stephen Warrene2969952014-03-21 12:28:54 -0600457 val |= (1 << SCHMT_SHIFT);
458 else
459 val &= ~(1 << SCHMT_SHIFT);
460 writel(val, reg);
461
462 return;
463}
464
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600465static void pinmux_set_hsm(enum pmux_drvgrp grp, enum pmux_hsm hsm)
Stephen Warrene2969952014-03-21 12:28:54 -0600466{
467 u32 *reg = DRV_REG(grp);
468 u32 val;
469
470 /* NONE means unspecified/do not change/use POR value */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600471 if (hsm == PMUX_HSM_NONE)
Stephen Warrene2969952014-03-21 12:28:54 -0600472 return;
473
474 /* Error check pad */
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600475 assert(pmux_drvgrp_isvalid(grp));
476 assert(pmux_hsm_isvalid(hsm));
Stephen Warrene2969952014-03-21 12:28:54 -0600477
478 val = readl(reg);
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600479 if (hsm == PMUX_HSM_ENABLE)
Stephen Warrene2969952014-03-21 12:28:54 -0600480 val |= (1 << HSM_SHIFT);
481 else
482 val &= ~(1 << HSM_SHIFT);
483 writel(val, reg);
484
485 return;
486}
487
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600488static void pinmux_config_drvgrp(const struct pmux_drvgrp_config *config)
Stephen Warrene2969952014-03-21 12:28:54 -0600489{
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600490 enum pmux_drvgrp grp = config->drvgrp;
Stephen Warrene2969952014-03-21 12:28:54 -0600491
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600492 pinmux_set_drvup_slwf(grp, config->slwf);
493 pinmux_set_drvdn_slwr(grp, config->slwr);
494 pinmux_set_drvup(grp, config->drvup);
495 pinmux_set_drvdn(grp, config->drvdn);
496 pinmux_set_lpmd(grp, config->lpmd);
497 pinmux_set_schmt(grp, config->schmt);
498 pinmux_set_hsm(grp, config->hsm);
Stephen Warrene2969952014-03-21 12:28:54 -0600499}
500
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600501void pinmux_config_drvgrp_table(const struct pmux_drvgrp_config *config,
502 int len)
Stephen Warrene2969952014-03-21 12:28:54 -0600503{
504 int i;
505
506 for (i = 0; i < len; i++)
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600507 pinmux_config_drvgrp(&config[i]);
Stephen Warrene2969952014-03-21 12:28:54 -0600508}
Stephen Warrendfb42fc2014-03-21 12:28:56 -0600509#endif /* TEGRA_PMX_HAS_DRVGRPS */