2 * Copyright (c) 2010-2013, NVIDIA CORPORATION. All rights reserved.
3 * Copyright (c) 2011 The Chromium OS Authors.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/pinmux.h>
12 /* return 1 if a pingrp is in range */
13 #define pmux_pingrp_isvalid(pin) (((pin) >= 0) && ((pin) < PMUX_PINGRP_COUNT))
15 /* return 1 if a pmux_func is in range */
16 #define pmux_func_isvalid(func) \
17 (((func) >= 0) && ((func) < PMUX_FUNC_COUNT))
19 /* return 1 if a pin_pupd_is in range */
20 #define pmux_pin_pupd_isvalid(pupd) \
21 (((pupd) >= PMUX_PULL_NORMAL) && ((pupd) <= PMUX_PULL_UP))
23 /* return 1 if a pin_tristate_is in range */
24 #define pmux_pin_tristate_isvalid(tristate) \
25 (((tristate) >= PMUX_TRI_NORMAL) && ((tristate) <= PMUX_TRI_TRISTATE))
27 #ifdef TEGRA_PMX_PINS_HAVE_E_INPUT
28 /* return 1 if a pin_io_is in range */
29 #define pmux_pin_io_isvalid(io) \
30 (((io) >= PMUX_PIN_OUTPUT) && ((io) <= PMUX_PIN_INPUT))
33 #ifdef TEGRA_PMX_PINS_HAVE_LOCK
34 /* return 1 if a pin_lock is in range */
35 #define pmux_pin_lock_isvalid(lock) \
36 (((lock) >= PMUX_PIN_LOCK_DISABLE) && ((lock) <= PMUX_PIN_LOCK_ENABLE))
39 #ifdef TEGRA_PMX_PINS_HAVE_OD
40 /* return 1 if a pin_od is in range */
41 #define pmux_pin_od_isvalid(od) \
42 (((od) >= PMUX_PIN_OD_DISABLE) && ((od) <= PMUX_PIN_OD_ENABLE))
45 #ifdef TEGRA_PMX_PINS_HAVE_IO_RESET
46 /* return 1 if a pin_ioreset_is in range */
47 #define pmux_pin_ioreset_isvalid(ioreset) \
48 (((ioreset) >= PMUX_PIN_IO_RESET_DISABLE) && \
49 ((ioreset) <= PMUX_PIN_IO_RESET_ENABLE))
52 #ifdef TEGRA_PMX_PINS_HAVE_RCV_SEL
53 /* return 1 if a pin_rcv_sel_is in range */
54 #define pmux_pin_rcv_sel_isvalid(rcv_sel) \
55 (((rcv_sel) >= PMUX_PIN_RCV_SEL_NORMAL) && \
56 ((rcv_sel) <= PMUX_PIN_RCV_SEL_HIGH))
59 #ifdef TEGRA_PMX_PINS_HAVE_E_IO_HV
60 /* return 1 if a pin_e_io_hv is in range */
61 #define pmux_pin_e_io_hv_isvalid(e_io_hv) \
62 (((e_io_hv) >= PMUX_PIN_E_IO_HV_NORMAL) && \
63 ((e_io_hv) <= PMUX_PIN_E_IO_HV_HIGH))
66 #ifdef TEGRA_PMX_GRPS_HAVE_LPMD
67 #define pmux_lpmd_isvalid(lpm) \
68 (((lpm) >= PMUX_LPMD_X8) && ((lpm) <= PMUX_LPMD_X))
71 #if defined(TEGRA_PMX_PINS_HAVE_SCHMT) || defined(TEGRA_PMX_GRPS_HAVE_SCHMT)
72 #define pmux_schmt_isvalid(schmt) \
73 (((schmt) >= PMUX_SCHMT_DISABLE) && ((schmt) <= PMUX_SCHMT_ENABLE))
76 #if defined(TEGRA_PMX_PINS_HAVE_HSM) || defined(TEGRA_PMX_GRPS_HAVE_HSM)
77 #define pmux_hsm_isvalid(hsm) \
78 (((hsm) >= PMUX_HSM_DISABLE) && ((hsm) <= PMUX_HSM_ENABLE))
81 #define _R(offset) (u32 *)((unsigned long)NV_PA_APB_MISC_BASE + (offset))
83 #if defined(CONFIG_TEGRA20)
85 #define MUX_REG(grp) _R(0x80 + ((tegra_soc_pingroups[grp].ctl_id / 16) * 4))
86 #define MUX_SHIFT(grp) ((tegra_soc_pingroups[grp].ctl_id % 16) * 2)
88 #define PULL_REG(grp) _R(0xa0 + ((tegra_soc_pingroups[grp].pull_id / 16) * 4))
89 #define PULL_SHIFT(grp) ((tegra_soc_pingroups[grp].pull_id % 16) * 2)
91 #define TRI_REG(grp) _R(0x14 + (((grp) / 32) * 4))
92 #define TRI_SHIFT(grp) ((grp) % 32)
96 #define REG(pin) _R(0x3000 + ((pin) * 4))
98 #define MUX_REG(pin) REG(pin)
99 #define MUX_SHIFT(pin) 0
101 #define PULL_REG(pin) REG(pin)
102 #define PULL_SHIFT(pin) 2
104 #define TRI_REG(pin) REG(pin)
105 #define TRI_SHIFT(pin) 4
107 #endif /* CONFIG_TEGRA20 */
109 #define DRV_REG(group) _R(TEGRA_PMX_SOC_DRV_GROUP_BASE_REG + ((group) * 4))
111 #define MIPIPADCTRL_REG(group) _R(TEGRA_PMX_SOC_MIPIPADCTRL_BASE_REG + ((group) * 4))
114 * We could force arch-tegraNN/pinmux.h to define all of these. However,
115 * that's a lot of defines, and for now it's manageable to just put a
116 * special case here. It's possible this decision will change with future
119 #ifdef CONFIG_TEGRA210
122 #ifdef TEGRA_PMX_PINS_HAVE_HSM
125 #define E_IO_HV_SHIFT 10
127 #ifdef TEGRA_PMX_PINS_HAVE_SCHMT
128 #define SCHMT_SHIFT 12
134 #define IO_RESET_SHIFT 8
135 #define RCV_SEL_SHIFT 9
138 #ifdef TEGRA_PMX_SOC_HAS_IO_CLAMPING
139 /* This register/field only exists on Tegra114 and later */
140 #define APB_MISC_PP_PINMUX_GLOBAL_0 0x40
141 #define CLAMP_INPUTS_WHEN_TRISTATED 1
143 void pinmux_set_tristate_input_clamping(void)
145 u32 *reg = _R(APB_MISC_PP_PINMUX_GLOBAL_0);
147 setbits_le32(reg, CLAMP_INPUTS_WHEN_TRISTATED);
150 void pinmux_clear_tristate_input_clamping(void)
152 u32 *reg = _R(APB_MISC_PP_PINMUX_GLOBAL_0);
154 clrbits_le32(reg, CLAMP_INPUTS_WHEN_TRISTATED);
158 void pinmux_set_func(enum pmux_pingrp pin, enum pmux_func func)
160 u32 *reg = MUX_REG(pin);
164 if (func == PMUX_FUNC_DEFAULT)
167 /* Error check on pin and func */
168 assert(pmux_pingrp_isvalid(pin));
169 assert(pmux_func_isvalid(func));
171 if (func >= PMUX_FUNC_RSVD1) {
172 mux = (func - PMUX_FUNC_RSVD1) & 3;
174 /* Search for the appropriate function */
175 for (i = 0; i < 4; i++) {
176 if (tegra_soc_pingroups[pin].funcs[i] == func) {
185 val &= ~(3 << MUX_SHIFT(pin));
186 val |= (mux << MUX_SHIFT(pin));
190 void pinmux_set_pullupdown(enum pmux_pingrp pin, enum pmux_pull pupd)
192 u32 *reg = PULL_REG(pin);
195 /* Error check on pin and pupd */
196 assert(pmux_pingrp_isvalid(pin));
197 assert(pmux_pin_pupd_isvalid(pupd));
200 val &= ~(3 << PULL_SHIFT(pin));
201 val |= (pupd << PULL_SHIFT(pin));
205 static void pinmux_set_tristate(enum pmux_pingrp pin, int tri)
207 u32 *reg = TRI_REG(pin);
210 /* Error check on pin */
211 assert(pmux_pingrp_isvalid(pin));
212 assert(pmux_pin_tristate_isvalid(tri));
215 if (tri == PMUX_TRI_TRISTATE)
216 val |= (1 << TRI_SHIFT(pin));
218 val &= ~(1 << TRI_SHIFT(pin));
222 void pinmux_tristate_enable(enum pmux_pingrp pin)
224 pinmux_set_tristate(pin, PMUX_TRI_TRISTATE);
227 void pinmux_tristate_disable(enum pmux_pingrp pin)
229 pinmux_set_tristate(pin, PMUX_TRI_NORMAL);
232 #ifdef TEGRA_PMX_PINS_HAVE_E_INPUT
233 void pinmux_set_io(enum pmux_pingrp pin, enum pmux_pin_io io)
238 if (io == PMUX_PIN_NONE)
241 /* Error check on pin and io */
242 assert(pmux_pingrp_isvalid(pin));
243 assert(pmux_pin_io_isvalid(io));
246 if (io == PMUX_PIN_INPUT)
247 val |= (io & 1) << IO_SHIFT;
249 val &= ~(1 << IO_SHIFT);
254 #ifdef TEGRA_PMX_PINS_HAVE_LOCK
255 static void pinmux_set_lock(enum pmux_pingrp pin, enum pmux_pin_lock lock)
260 if (lock == PMUX_PIN_LOCK_DEFAULT)
263 /* Error check on pin and lock */
264 assert(pmux_pingrp_isvalid(pin));
265 assert(pmux_pin_lock_isvalid(lock));
268 if (lock == PMUX_PIN_LOCK_ENABLE) {
269 val |= (1 << LOCK_SHIFT);
271 if (val & (1 << LOCK_SHIFT))
272 printf("%s: Cannot clear LOCK bit!\n", __func__);
273 val &= ~(1 << LOCK_SHIFT);
281 #ifdef TEGRA_PMX_PINS_HAVE_OD
282 static void pinmux_set_od(enum pmux_pingrp pin, enum pmux_pin_od od)
287 if (od == PMUX_PIN_OD_DEFAULT)
290 /* Error check on pin and od */
291 assert(pmux_pingrp_isvalid(pin));
292 assert(pmux_pin_od_isvalid(od));
295 if (od == PMUX_PIN_OD_ENABLE)
296 val |= (1 << OD_SHIFT);
298 val &= ~(1 << OD_SHIFT);
305 #ifdef TEGRA_PMX_PINS_HAVE_IO_RESET
306 static void pinmux_set_ioreset(enum pmux_pingrp pin,
307 enum pmux_pin_ioreset ioreset)
312 if (ioreset == PMUX_PIN_IO_RESET_DEFAULT)
315 /* Error check on pin and ioreset */
316 assert(pmux_pingrp_isvalid(pin));
317 assert(pmux_pin_ioreset_isvalid(ioreset));
320 if (ioreset == PMUX_PIN_IO_RESET_ENABLE)
321 val |= (1 << IO_RESET_SHIFT);
323 val &= ~(1 << IO_RESET_SHIFT);
330 #ifdef TEGRA_PMX_PINS_HAVE_RCV_SEL
331 static void pinmux_set_rcv_sel(enum pmux_pingrp pin,
332 enum pmux_pin_rcv_sel rcv_sel)
337 if (rcv_sel == PMUX_PIN_RCV_SEL_DEFAULT)
340 /* Error check on pin and rcv_sel */
341 assert(pmux_pingrp_isvalid(pin));
342 assert(pmux_pin_rcv_sel_isvalid(rcv_sel));
345 if (rcv_sel == PMUX_PIN_RCV_SEL_HIGH)
346 val |= (1 << RCV_SEL_SHIFT);
348 val &= ~(1 << RCV_SEL_SHIFT);
355 #ifdef TEGRA_PMX_PINS_HAVE_E_IO_HV
356 static void pinmux_set_e_io_hv(enum pmux_pingrp pin,
357 enum pmux_pin_e_io_hv e_io_hv)
362 if (e_io_hv == PMUX_PIN_E_IO_HV_DEFAULT)
365 /* Error check on pin and e_io_hv */
366 assert(pmux_pingrp_isvalid(pin));
367 assert(pmux_pin_e_io_hv_isvalid(e_io_hv));
370 if (e_io_hv == PMUX_PIN_E_IO_HV_HIGH)
371 val |= (1 << E_IO_HV_SHIFT);
373 val &= ~(1 << E_IO_HV_SHIFT);
380 #ifdef TEGRA_PMX_PINS_HAVE_SCHMT
381 static void pinmux_set_schmt(enum pmux_pingrp pin, enum pmux_schmt schmt)
386 /* NONE means unspecified/do not change/use POR value */
387 if (schmt == PMUX_SCHMT_NONE)
390 /* Error check pad */
391 assert(pmux_pingrp_isvalid(pin));
392 assert(pmux_schmt_isvalid(schmt));
395 if (schmt == PMUX_SCHMT_ENABLE)
396 val |= (1 << SCHMT_SHIFT);
398 val &= ~(1 << SCHMT_SHIFT);
405 #ifdef TEGRA_PMX_PINS_HAVE_HSM
406 static void pinmux_set_hsm(enum pmux_pingrp pin, enum pmux_hsm hsm)
411 /* NONE means unspecified/do not change/use POR value */
412 if (hsm == PMUX_HSM_NONE)
415 /* Error check pad */
416 assert(pmux_pingrp_isvalid(pin));
417 assert(pmux_hsm_isvalid(hsm));
420 if (hsm == PMUX_HSM_ENABLE)
421 val |= (1 << HSM_SHIFT);
423 val &= ~(1 << HSM_SHIFT);
430 static void pinmux_config_pingrp(const struct pmux_pingrp_config *config)
432 enum pmux_pingrp pin = config->pingrp;
434 pinmux_set_func(pin, config->func);
435 pinmux_set_pullupdown(pin, config->pull);
436 pinmux_set_tristate(pin, config->tristate);
437 #ifdef TEGRA_PMX_PINS_HAVE_E_INPUT
438 pinmux_set_io(pin, config->io);
440 #ifdef TEGRA_PMX_PINS_HAVE_LOCK
441 pinmux_set_lock(pin, config->lock);
443 #ifdef TEGRA_PMX_PINS_HAVE_OD
444 pinmux_set_od(pin, config->od);
446 #ifdef TEGRA_PMX_PINS_HAVE_IO_RESET
447 pinmux_set_ioreset(pin, config->ioreset);
449 #ifdef TEGRA_PMX_PINS_HAVE_RCV_SEL
450 pinmux_set_rcv_sel(pin, config->rcv_sel);
452 #ifdef TEGRA_PMX_PINS_HAVE_E_IO_HV
453 pinmux_set_e_io_hv(pin, config->e_io_hv);
455 #ifdef TEGRA_PMX_PINS_HAVE_SCHMT
456 pinmux_set_schmt(pin, config->schmt);
458 #ifdef TEGRA_PMX_PINS_HAVE_HSM
459 pinmux_set_hsm(pin, config->hsm);
463 void pinmux_config_pingrp_table(const struct pmux_pingrp_config *config,
468 for (i = 0; i < len; i++)
469 pinmux_config_pingrp(&config[i]);
472 #ifdef TEGRA_PMX_SOC_HAS_DRVGRPS
474 #define pmux_drvgrp_isvalid(pd) (((pd) >= 0) && ((pd) < PMUX_DRVGRP_COUNT))
476 #define pmux_slw_isvalid(slw) \
477 (((slw) >= PMUX_SLWF_MIN) && ((slw) <= PMUX_SLWF_MAX))
479 #define pmux_drv_isvalid(drv) \
480 (((drv) >= PMUX_DRVUP_MIN) && ((drv) <= PMUX_DRVUP_MAX))
482 #ifdef TEGRA_PMX_GRPS_HAVE_HSM
485 #ifdef TEGRA_PMX_GRPS_HAVE_SCHMT
486 #define SCHMT_SHIFT 3
488 #ifdef TEGRA_PMX_GRPS_HAVE_LPMD
490 #define LPMD_MASK (3 << LPMD_SHIFT)
493 * Note that the following DRV* and SLW* defines are accurate for many drive
494 * groups on many SoCs. We really need a per-group data structure to solve
495 * this, since the fields are in different positions/sizes in different
496 * registers (for different groups).
498 * On Tegra30/114/124, the DRV*_SHIFT values vary.
499 * On Tegra30, the SLW*_SHIFT values vary.
500 * On Tegra30/114/124/210, the DRV*_MASK values vary, although the values
501 * below are wide enough to cover the widest fields, and hopefully don't
502 * interfere with any other fields.
503 * On Tegra30, the SLW*_MASK values vary, but we can't use a value that's
504 * wide enough to cover all cases, since that would cause the field to
505 * overlap with other fields in the narrower cases.
507 #define DRVDN_SHIFT 12
508 #define DRVDN_MASK (0x7F << DRVDN_SHIFT)
509 #define DRVUP_SHIFT 20
510 #define DRVUP_MASK (0x7F << DRVUP_SHIFT)
511 #define SLWR_SHIFT 28
512 #define SLWR_MASK (3 << SLWR_SHIFT)
513 #define SLWF_SHIFT 30
514 #define SLWF_MASK (3 << SLWF_SHIFT)
516 static void pinmux_set_drvup_slwf(enum pmux_drvgrp grp, int slwf)
518 u32 *reg = DRV_REG(grp);
521 /* NONE means unspecified/do not change/use POR value */
522 if (slwf == PMUX_SLWF_NONE)
525 /* Error check on pad and slwf */
526 assert(pmux_drvgrp_isvalid(grp));
527 assert(pmux_slw_isvalid(slwf));
531 val |= (slwf << SLWF_SHIFT);
537 static void pinmux_set_drvdn_slwr(enum pmux_drvgrp grp, int slwr)
539 u32 *reg = DRV_REG(grp);
542 /* NONE means unspecified/do not change/use POR value */
543 if (slwr == PMUX_SLWR_NONE)
546 /* Error check on pad and slwr */
547 assert(pmux_drvgrp_isvalid(grp));
548 assert(pmux_slw_isvalid(slwr));
552 val |= (slwr << SLWR_SHIFT);
558 static void pinmux_set_drvup(enum pmux_drvgrp grp, int drvup)
560 u32 *reg = DRV_REG(grp);
563 /* NONE means unspecified/do not change/use POR value */
564 if (drvup == PMUX_DRVUP_NONE)
567 /* Error check on pad and drvup */
568 assert(pmux_drvgrp_isvalid(grp));
569 assert(pmux_drv_isvalid(drvup));
573 val |= (drvup << DRVUP_SHIFT);
579 static void pinmux_set_drvdn(enum pmux_drvgrp grp, int drvdn)
581 u32 *reg = DRV_REG(grp);
584 /* NONE means unspecified/do not change/use POR value */
585 if (drvdn == PMUX_DRVDN_NONE)
588 /* Error check on pad and drvdn */
589 assert(pmux_drvgrp_isvalid(grp));
590 assert(pmux_drv_isvalid(drvdn));
594 val |= (drvdn << DRVDN_SHIFT);
600 #ifdef TEGRA_PMX_GRPS_HAVE_LPMD
601 static void pinmux_set_lpmd(enum pmux_drvgrp grp, enum pmux_lpmd lpmd)
603 u32 *reg = DRV_REG(grp);
606 /* NONE means unspecified/do not change/use POR value */
607 if (lpmd == PMUX_LPMD_NONE)
610 /* Error check pad and lpmd value */
611 assert(pmux_drvgrp_isvalid(grp));
612 assert(pmux_lpmd_isvalid(lpmd));
616 val |= (lpmd << LPMD_SHIFT);
623 #ifdef TEGRA_PMX_GRPS_HAVE_SCHMT
624 static void pinmux_set_schmt(enum pmux_drvgrp grp, enum pmux_schmt schmt)
626 u32 *reg = DRV_REG(grp);
629 /* NONE means unspecified/do not change/use POR value */
630 if (schmt == PMUX_SCHMT_NONE)
633 /* Error check pad */
634 assert(pmux_drvgrp_isvalid(grp));
635 assert(pmux_schmt_isvalid(schmt));
638 if (schmt == PMUX_SCHMT_ENABLE)
639 val |= (1 << SCHMT_SHIFT);
641 val &= ~(1 << SCHMT_SHIFT);
648 #ifdef TEGRA_PMX_GRPS_HAVE_HSM
649 static void pinmux_set_hsm(enum pmux_drvgrp grp, enum pmux_hsm hsm)
651 u32 *reg = DRV_REG(grp);
654 /* NONE means unspecified/do not change/use POR value */
655 if (hsm == PMUX_HSM_NONE)
658 /* Error check pad */
659 assert(pmux_drvgrp_isvalid(grp));
660 assert(pmux_hsm_isvalid(hsm));
663 if (hsm == PMUX_HSM_ENABLE)
664 val |= (1 << HSM_SHIFT);
666 val &= ~(1 << HSM_SHIFT);
673 static void pinmux_config_drvgrp(const struct pmux_drvgrp_config *config)
675 enum pmux_drvgrp grp = config->drvgrp;
677 pinmux_set_drvup_slwf(grp, config->slwf);
678 pinmux_set_drvdn_slwr(grp, config->slwr);
679 pinmux_set_drvup(grp, config->drvup);
680 pinmux_set_drvdn(grp, config->drvdn);
681 #ifdef TEGRA_PMX_GRPS_HAVE_LPMD
682 pinmux_set_lpmd(grp, config->lpmd);
684 #ifdef TEGRA_PMX_GRPS_HAVE_SCHMT
685 pinmux_set_schmt(grp, config->schmt);
687 #ifdef TEGRA_PMX_GRPS_HAVE_HSM
688 pinmux_set_hsm(grp, config->hsm);
692 void pinmux_config_drvgrp_table(const struct pmux_drvgrp_config *config,
697 for (i = 0; i < len; i++)
698 pinmux_config_drvgrp(&config[i]);
700 #endif /* TEGRA_PMX_SOC_HAS_DRVGRPS */
702 #ifdef TEGRA_PMX_SOC_HAS_MIPI_PAD_CTRL_GRPS
704 #define pmux_mipipadctrlgrp_isvalid(pd) (((pd) >= 0) && ((pd) < PMUX_MIPIPADCTRLGRP_COUNT))
706 static void pinmux_mipipadctrl_set_func(enum pmux_mipipadctrlgrp grp,
709 u32 *reg = MIPIPADCTRL_REG(grp);
713 if (func == PMUX_FUNC_DEFAULT)
716 /* Error check grp and func */
717 assert(pmux_mipipadctrlgrp_isvalid(grp));
718 assert(pmux_func_isvalid(func));
720 if (func >= PMUX_FUNC_RSVD1) {
721 mux = (func - PMUX_FUNC_RSVD1) & 1;
723 /* Search for the appropriate function */
724 for (i = 0; i < 2; i++) {
725 if (tegra_soc_mipipadctrl_groups[grp].funcs[i]
740 static void pinmux_config_mipipadctrlgrp(const struct pmux_mipipadctrlgrp_config *config)
742 enum pmux_mipipadctrlgrp grp = config->grp;
744 pinmux_mipipadctrl_set_func(grp, config->func);
747 void pinmux_config_mipipadctrlgrp_table(
748 const struct pmux_mipipadctrlgrp_config *config, int len)
752 for (i = 0; i < len; i++)
753 pinmux_config_mipipadctrlgrp(&config[i]);
755 #endif /* TEGRA_PMX_SOC_HAS_MIPI_PAD_CTRL_GRPS */