blob: d62618cd0f618230fd01a85766e949e6a61a1554 [file] [log] [blame]
Stephen Warren9026dfd2014-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 Warrenf4df6052014-03-21 12:28:56 -060013#define pmux_pingrp_isvalid(pin) (((pin) >= 0) && ((pin) < PMUX_PINGRP_COUNT))
Stephen Warren9026dfd2014-03-21 12:28:54 -060014
15/* return 1 if a pmux_func is in range */
16#define pmux_func_isvalid(func) \
Stephen Warren70b080f2014-03-21 15:58:03 -060017 (((func) >= 0) && ((func) < PMUX_FUNC_COUNT))
Stephen Warren9026dfd2014-03-21 12:28:54 -060018
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))
22
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))
26
27#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
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))
31
32/* return 1 if a pin_lock is in range */
33#define pmux_pin_lock_isvalid(lock) \
34 (((lock) >= PMUX_PIN_LOCK_DISABLE) && ((lock) <= PMUX_PIN_LOCK_ENABLE))
35
36/* return 1 if a pin_od is in range */
37#define pmux_pin_od_isvalid(od) \
38 (((od) >= PMUX_PIN_OD_DISABLE) && ((od) <= PMUX_PIN_OD_ENABLE))
39
40/* return 1 if a pin_ioreset_is in range */
41#define pmux_pin_ioreset_isvalid(ioreset) \
42 (((ioreset) >= PMUX_PIN_IO_RESET_DISABLE) && \
43 ((ioreset) <= PMUX_PIN_IO_RESET_ENABLE))
44
45#ifdef TEGRA_PMX_HAS_RCV_SEL
46/* return 1 if a pin_rcv_sel_is in range */
47#define pmux_pin_rcv_sel_isvalid(rcv_sel) \
48 (((rcv_sel) >= PMUX_PIN_RCV_SEL_NORMAL) && \
49 ((rcv_sel) <= PMUX_PIN_RCV_SEL_HIGH))
50#endif /* TEGRA_PMX_HAS_RCV_SEL */
51#endif /* TEGRA_PMX_HAS_PIN_IO_BIT_ETC */
52
53#define _R(offset) (u32 *)(NV_PA_APB_MISC_BASE + (offset))
54
55#if defined(CONFIG_TEGRA20)
56
57#define MUX_REG(grp) _R(0x80 + ((tegra_soc_pingroups[grp].ctl_id / 16) * 4))
58#define MUX_SHIFT(grp) ((tegra_soc_pingroups[grp].ctl_id % 16) * 2)
59
60#define PULL_REG(grp) _R(0xa0 + ((tegra_soc_pingroups[grp].pull_id / 16) * 4))
61#define PULL_SHIFT(grp) ((tegra_soc_pingroups[grp].pull_id % 16) * 2)
62
63#define TRI_REG(grp) _R(0x14 + (((grp) / 32) * 4))
64#define TRI_SHIFT(grp) ((grp) % 32)
65
66#else
67
68#define REG(pin) _R(0x3000 + ((pin) * 4))
69
70#define MUX_REG(pin) REG(pin)
71#define MUX_SHIFT(pin) 0
72
73#define PULL_REG(pin) REG(pin)
74#define PULL_SHIFT(pin) 2
75
76#define TRI_REG(pin) REG(pin)
77#define TRI_SHIFT(pin) 4
78
79#endif /* CONFIG_TEGRA20 */
80
81#define DRV_REG(group) _R(0x868 + ((group) * 4))
82
83#define IO_SHIFT 5
84#define OD_SHIFT 6
85#define LOCK_SHIFT 7
86#define IO_RESET_SHIFT 8
87#define RCV_SEL_SHIFT 9
88
89void pinmux_set_func(enum pmux_pingrp pin, enum pmux_func func)
90{
91 u32 *reg = MUX_REG(pin);
92 int i, mux = -1;
93 u32 val;
94
95 /* Error check on pin and func */
96 assert(pmux_pingrp_isvalid(pin));
97 assert(pmux_func_isvalid(func));
98
Stephen Warren70b080f2014-03-21 15:58:03 -060099 if (func >= PMUX_FUNC_RSVD1) {
100 mux = (func - PMUX_FUNC_RSVD1) & 3;
Stephen Warren9026dfd2014-03-21 12:28:54 -0600101 } else {
102 /* Search for the appropriate function */
103 for (i = 0; i < 4; i++) {
104 if (tegra_soc_pingroups[pin].funcs[i] == func) {
105 mux = i;
106 break;
107 }
108 }
109 }
110 assert(mux != -1);
111
112 val = readl(reg);
113 val &= ~(3 << MUX_SHIFT(pin));
114 val |= (mux << MUX_SHIFT(pin));
115 writel(val, reg);
116}
117
118void pinmux_set_pullupdown(enum pmux_pingrp pin, enum pmux_pull pupd)
119{
120 u32 *reg = PULL_REG(pin);
121 u32 val;
122
123 /* Error check on pin and pupd */
124 assert(pmux_pingrp_isvalid(pin));
125 assert(pmux_pin_pupd_isvalid(pupd));
126
127 val = readl(reg);
128 val &= ~(3 << PULL_SHIFT(pin));
129 val |= (pupd << PULL_SHIFT(pin));
130 writel(val, reg);
131}
132
Stephen Warren51ed4ad2014-03-21 12:28:55 -0600133static void pinmux_set_tristate(enum pmux_pingrp pin, int tri)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600134{
135 u32 *reg = TRI_REG(pin);
136 u32 val;
137
138 /* Error check on pin */
139 assert(pmux_pingrp_isvalid(pin));
140 assert(pmux_pin_tristate_isvalid(tri));
141
142 val = readl(reg);
143 if (tri == PMUX_TRI_TRISTATE)
144 val |= (1 << TRI_SHIFT(pin));
145 else
146 val &= ~(1 << TRI_SHIFT(pin));
147 writel(val, reg);
148}
149
150void pinmux_tristate_enable(enum pmux_pingrp pin)
151{
152 pinmux_set_tristate(pin, PMUX_TRI_TRISTATE);
153}
154
155void pinmux_tristate_disable(enum pmux_pingrp pin)
156{
157 pinmux_set_tristate(pin, PMUX_TRI_NORMAL);
158}
159
160#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
161void pinmux_set_io(enum pmux_pingrp pin, enum pmux_pin_io io)
162{
163 u32 *reg = REG(pin);
164 u32 val;
165
166 if (io == PMUX_PIN_NONE)
167 return;
168
169 /* Error check on pin and io */
170 assert(pmux_pingrp_isvalid(pin));
171 assert(pmux_pin_io_isvalid(io));
172
173 val = readl(reg);
174 if (io == PMUX_PIN_INPUT)
175 val |= (io & 1) << IO_SHIFT;
176 else
177 val &= ~(1 << IO_SHIFT);
178 writel(val, reg);
179}
180
181static void pinmux_set_lock(enum pmux_pingrp pin, enum pmux_pin_lock lock)
182{
183 u32 *reg = REG(pin);
184 u32 val;
185
186 if (lock == PMUX_PIN_LOCK_DEFAULT)
187 return;
188
189 /* Error check on pin and lock */
190 assert(pmux_pingrp_isvalid(pin));
191 assert(pmux_pin_lock_isvalid(lock));
192
193 val = readl(reg);
194 if (lock == PMUX_PIN_LOCK_ENABLE) {
195 val |= (1 << LOCK_SHIFT);
196 } else {
197 if (val & (1 << LOCK_SHIFT))
198 printf("%s: Cannot clear LOCK bit!\n", __func__);
199 val &= ~(1 << LOCK_SHIFT);
200 }
201 writel(val, reg);
202
203 return;
204}
205
206static void pinmux_set_od(enum pmux_pingrp pin, enum pmux_pin_od od)
207{
208 u32 *reg = REG(pin);
209 u32 val;
210
211 if (od == PMUX_PIN_OD_DEFAULT)
212 return;
213
214 /* Error check on pin and od */
215 assert(pmux_pingrp_isvalid(pin));
216 assert(pmux_pin_od_isvalid(od));
217
218 val = readl(reg);
219 if (od == PMUX_PIN_OD_ENABLE)
220 val |= (1 << OD_SHIFT);
221 else
222 val &= ~(1 << OD_SHIFT);
223 writel(val, reg);
224
225 return;
226}
227
228static void pinmux_set_ioreset(enum pmux_pingrp pin,
229 enum pmux_pin_ioreset ioreset)
230{
231 u32 *reg = REG(pin);
232 u32 val;
233
234 if (ioreset == PMUX_PIN_IO_RESET_DEFAULT)
235 return;
236
237 /* Error check on pin and ioreset */
238 assert(pmux_pingrp_isvalid(pin));
239 assert(pmux_pin_ioreset_isvalid(ioreset));
240
241 val = readl(reg);
242 if (ioreset == PMUX_PIN_IO_RESET_ENABLE)
243 val |= (1 << IO_RESET_SHIFT);
244 else
245 val &= ~(1 << IO_RESET_SHIFT);
246 writel(val, reg);
247
248 return;
249}
250
251#ifdef TEGRA_PMX_HAS_RCV_SEL
252static void pinmux_set_rcv_sel(enum pmux_pingrp pin,
253 enum pmux_pin_rcv_sel rcv_sel)
254{
255 u32 *reg = REG(pin);
256 u32 val;
257
258 if (rcv_sel == PMUX_PIN_RCV_SEL_DEFAULT)
259 return;
260
261 /* Error check on pin and rcv_sel */
262 assert(pmux_pingrp_isvalid(pin));
263 assert(pmux_pin_rcv_sel_isvalid(rcv_sel));
264
265 val = readl(reg);
266 if (rcv_sel == PMUX_PIN_RCV_SEL_HIGH)
267 val |= (1 << RCV_SEL_SHIFT);
268 else
269 val &= ~(1 << RCV_SEL_SHIFT);
270 writel(val, reg);
271
272 return;
273}
274#endif /* TEGRA_PMX_HAS_RCV_SEL */
275#endif /* TEGRA_PMX_HAS_PIN_IO_BIT_ETC */
276
Stephen Warrenf4df6052014-03-21 12:28:56 -0600277static void pinmux_config_pingrp(const struct pmux_pingrp_config *config)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600278{
Stephen Warrenf4df6052014-03-21 12:28:56 -0600279 enum pmux_pingrp pin = config->pingrp;
Stephen Warren9026dfd2014-03-21 12:28:54 -0600280
281 pinmux_set_func(pin, config->func);
282 pinmux_set_pullupdown(pin, config->pull);
283 pinmux_set_tristate(pin, config->tristate);
284#ifdef TEGRA_PMX_HAS_PIN_IO_BIT_ETC
285 pinmux_set_io(pin, config->io);
286 pinmux_set_lock(pin, config->lock);
287 pinmux_set_od(pin, config->od);
288 pinmux_set_ioreset(pin, config->ioreset);
289#ifdef TEGRA_PMX_HAS_RCV_SEL
290 pinmux_set_rcv_sel(pin, config->rcv_sel);
291#endif
292#endif
293}
294
Stephen Warrenf4df6052014-03-21 12:28:56 -0600295void pinmux_config_pingrp_table(const struct pmux_pingrp_config *config,
296 int len)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600297{
298 int i;
299
300 for (i = 0; i < len; i++)
Stephen Warrenf4df6052014-03-21 12:28:56 -0600301 pinmux_config_pingrp(&config[i]);
Stephen Warren9026dfd2014-03-21 12:28:54 -0600302}
303
Stephen Warrenf4df6052014-03-21 12:28:56 -0600304#ifdef TEGRA_PMX_HAS_DRVGRPS
Stephen Warren9026dfd2014-03-21 12:28:54 -0600305
Stephen Warrenf4df6052014-03-21 12:28:56 -0600306#define pmux_drvgrp_isvalid(pd) (((pd) >= 0) && ((pd) < PMUX_DRVGRP_COUNT))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600307
Stephen Warrenf4df6052014-03-21 12:28:56 -0600308#define pmux_slw_isvalid(slw) \
309 (((slw) >= PMUX_SLWF_MIN) && ((slw) <= PMUX_SLWF_MAX))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600310
Stephen Warrenf4df6052014-03-21 12:28:56 -0600311#define pmux_drv_isvalid(drv) \
312 (((drv) >= PMUX_DRVUP_MIN) && ((drv) <= PMUX_DRVUP_MAX))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600313
Stephen Warrenf4df6052014-03-21 12:28:56 -0600314#define pmux_lpmd_isvalid(lpm) \
315 (((lpm) >= PMUX_LPMD_X8) && ((lpm) <= PMUX_LPMD_X))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600316
Stephen Warrenf4df6052014-03-21 12:28:56 -0600317#define pmux_schmt_isvalid(schmt) \
318 (((schmt) >= PMUX_SCHMT_DISABLE) && ((schmt) <= PMUX_SCHMT_ENABLE))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600319
Stephen Warrenf4df6052014-03-21 12:28:56 -0600320#define pmux_hsm_isvalid(hsm) \
321 (((hsm) >= PMUX_HSM_DISABLE) && ((hsm) <= PMUX_HSM_ENABLE))
Stephen Warren9026dfd2014-03-21 12:28:54 -0600322
323#define HSM_SHIFT 2
324#define SCHMT_SHIFT 3
325#define LPMD_SHIFT 4
326#define LPMD_MASK (3 << LPMD_SHIFT)
327#define DRVDN_SHIFT 12
328#define DRVDN_MASK (0x7F << DRVDN_SHIFT)
329#define DRVUP_SHIFT 20
330#define DRVUP_MASK (0x7F << DRVUP_SHIFT)
331#define SLWR_SHIFT 28
332#define SLWR_MASK (3 << SLWR_SHIFT)
333#define SLWF_SHIFT 30
334#define SLWF_MASK (3 << SLWF_SHIFT)
335
Stephen Warrenf4df6052014-03-21 12:28:56 -0600336static void pinmux_set_drvup_slwf(enum pmux_drvgrp grp, int slwf)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600337{
338 u32 *reg = DRV_REG(grp);
339 u32 val;
340
341 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600342 if (slwf == PMUX_SLWF_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600343 return;
344
345 /* Error check on pad and slwf */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600346 assert(pmux_drvgrp_isvalid(grp));
347 assert(pmux_slw_isvalid(slwf));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600348
349 val = readl(reg);
350 val &= ~SLWF_MASK;
351 val |= (slwf << SLWF_SHIFT);
352 writel(val, reg);
353
354 return;
355}
356
Stephen Warrenf4df6052014-03-21 12:28:56 -0600357static void pinmux_set_drvdn_slwr(enum pmux_drvgrp grp, int slwr)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600358{
359 u32 *reg = DRV_REG(grp);
360 u32 val;
361
362 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600363 if (slwr == PMUX_SLWR_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600364 return;
365
366 /* Error check on pad and slwr */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600367 assert(pmux_drvgrp_isvalid(grp));
368 assert(pmux_slw_isvalid(slwr));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600369
370 val = readl(reg);
371 val &= ~SLWR_MASK;
372 val |= (slwr << SLWR_SHIFT);
373 writel(val, reg);
374
375 return;
376}
377
Stephen Warrenf4df6052014-03-21 12:28:56 -0600378static void pinmux_set_drvup(enum pmux_drvgrp grp, int drvup)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600379{
380 u32 *reg = DRV_REG(grp);
381 u32 val;
382
383 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600384 if (drvup == PMUX_DRVUP_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600385 return;
386
387 /* Error check on pad and drvup */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600388 assert(pmux_drvgrp_isvalid(grp));
389 assert(pmux_drv_isvalid(drvup));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600390
391 val = readl(reg);
392 val &= ~DRVUP_MASK;
393 val |= (drvup << DRVUP_SHIFT);
394 writel(val, reg);
395
396 return;
397}
398
Stephen Warrenf4df6052014-03-21 12:28:56 -0600399static void pinmux_set_drvdn(enum pmux_drvgrp grp, int drvdn)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600400{
401 u32 *reg = DRV_REG(grp);
402 u32 val;
403
404 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600405 if (drvdn == PMUX_DRVDN_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600406 return;
407
408 /* Error check on pad and drvdn */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600409 assert(pmux_drvgrp_isvalid(grp));
410 assert(pmux_drv_isvalid(drvdn));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600411
412 val = readl(reg);
413 val &= ~DRVDN_MASK;
414 val |= (drvdn << DRVDN_SHIFT);
415 writel(val, reg);
416
417 return;
418}
419
Stephen Warrenf4df6052014-03-21 12:28:56 -0600420static void pinmux_set_lpmd(enum pmux_drvgrp grp, enum pmux_lpmd lpmd)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600421{
422 u32 *reg = DRV_REG(grp);
423 u32 val;
424
425 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600426 if (lpmd == PMUX_LPMD_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600427 return;
428
429 /* Error check pad and lpmd value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600430 assert(pmux_drvgrp_isvalid(grp));
431 assert(pmux_lpmd_isvalid(lpmd));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600432
433 val = readl(reg);
434 val &= ~LPMD_MASK;
435 val |= (lpmd << LPMD_SHIFT);
436 writel(val, reg);
437
438 return;
439}
440
Stephen Warrenf4df6052014-03-21 12:28:56 -0600441static void pinmux_set_schmt(enum pmux_drvgrp grp, enum pmux_schmt schmt)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600442{
443 u32 *reg = DRV_REG(grp);
444 u32 val;
445
446 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600447 if (schmt == PMUX_SCHMT_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600448 return;
449
450 /* Error check pad */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600451 assert(pmux_drvgrp_isvalid(grp));
452 assert(pmux_schmt_isvalid(schmt));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600453
454 val = readl(reg);
Stephen Warrenf4df6052014-03-21 12:28:56 -0600455 if (schmt == PMUX_SCHMT_ENABLE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600456 val |= (1 << SCHMT_SHIFT);
457 else
458 val &= ~(1 << SCHMT_SHIFT);
459 writel(val, reg);
460
461 return;
462}
463
Stephen Warrenf4df6052014-03-21 12:28:56 -0600464static void pinmux_set_hsm(enum pmux_drvgrp grp, enum pmux_hsm hsm)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600465{
466 u32 *reg = DRV_REG(grp);
467 u32 val;
468
469 /* NONE means unspecified/do not change/use POR value */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600470 if (hsm == PMUX_HSM_NONE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600471 return;
472
473 /* Error check pad */
Stephen Warrenf4df6052014-03-21 12:28:56 -0600474 assert(pmux_drvgrp_isvalid(grp));
475 assert(pmux_hsm_isvalid(hsm));
Stephen Warren9026dfd2014-03-21 12:28:54 -0600476
477 val = readl(reg);
Stephen Warrenf4df6052014-03-21 12:28:56 -0600478 if (hsm == PMUX_HSM_ENABLE)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600479 val |= (1 << HSM_SHIFT);
480 else
481 val &= ~(1 << HSM_SHIFT);
482 writel(val, reg);
483
484 return;
485}
486
Stephen Warrenf4df6052014-03-21 12:28:56 -0600487static void pinmux_config_drvgrp(const struct pmux_drvgrp_config *config)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600488{
Stephen Warrenf4df6052014-03-21 12:28:56 -0600489 enum pmux_drvgrp grp = config->drvgrp;
Stephen Warren9026dfd2014-03-21 12:28:54 -0600490
Stephen Warrenf4df6052014-03-21 12:28:56 -0600491 pinmux_set_drvup_slwf(grp, config->slwf);
492 pinmux_set_drvdn_slwr(grp, config->slwr);
493 pinmux_set_drvup(grp, config->drvup);
494 pinmux_set_drvdn(grp, config->drvdn);
495 pinmux_set_lpmd(grp, config->lpmd);
496 pinmux_set_schmt(grp, config->schmt);
497 pinmux_set_hsm(grp, config->hsm);
Stephen Warren9026dfd2014-03-21 12:28:54 -0600498}
499
Stephen Warrenf4df6052014-03-21 12:28:56 -0600500void pinmux_config_drvgrp_table(const struct pmux_drvgrp_config *config,
501 int len)
Stephen Warren9026dfd2014-03-21 12:28:54 -0600502{
503 int i;
504
505 for (i = 0; i < len; i++)
Stephen Warrenf4df6052014-03-21 12:28:56 -0600506 pinmux_config_drvgrp(&config[i]);
Stephen Warren9026dfd2014-03-21 12:28:54 -0600507}
Stephen Warrenf4df6052014-03-21 12:28:56 -0600508#endif /* TEGRA_PMX_HAS_DRVGRPS */