blob: 10f68ba98b5212e7ca9882e86d9dfeeb20953f42 [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 */
13#define pmux_pingrp_isvalid(pin) (((pin) >= 0) && ((pin) < PINGRP_COUNT))
14
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
134void pinmux_set_tristate(enum pmux_pingrp pin, int tri)
135{
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
278void pinmux_config_pingroup(const struct pingroup_config *config)
279{
280 enum pmux_pingrp pin = config->pingroup;
281
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
296void pinmux_config_table(const struct pingroup_config *config, int len)
297{
298 int i;
299
300 for (i = 0; i < len; i++)
301 pinmux_config_pingroup(&config[i]);
302}
303
304#ifdef TEGRA_PMX_HAS_PADGRPS
305
306#define pmux_padgrp_isvalid(pd) (((pd) >= 0) && ((pd) < PDRIVE_PINGROUP_COUNT))
307
308#define pmux_pad_slw_isvalid(slw) \
309 (((slw) >= PGRP_SLWF_MIN) && ((slw) <= PGRP_SLWF_MAX))
310
311#define pmux_pad_drv_isvalid(drv) \
312 (((drv) >= PGRP_DRVUP_MIN) && ((drv) <= PGRP_DRVUP_MAX))
313
314#define pmux_pad_lpmd_isvalid(lpm) \
315 (((lpm) >= PGRP_LPMD_X8) && ((lpm) <= PGRP_LPMD_X))
316
317#define pmux_pad_schmt_isvalid(schmt) \
318 (((schmt) >= PGRP_SCHMT_DISABLE) && ((schmt) <= PGRP_SCHMT_ENABLE))
319
320#define pmux_pad_hsm_isvalid(hsm) \
321 (((hsm) >= PGRP_HSM_DISABLE) && ((hsm) <= PGRP_HSM_ENABLE))
322
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
336static void padgrp_set_drvup_slwf(enum pdrive_pingrp grp, int slwf)
337{
338 u32 *reg = DRV_REG(grp);
339 u32 val;
340
341 /* NONE means unspecified/do not change/use POR value */
342 if (slwf == PGRP_SLWF_NONE)
343 return;
344
345 /* Error check on pad and slwf */
346 assert(pmux_padgrp_isvalid(grp));
347 assert(pmux_pad_slw_isvalid(slwf));
348
349 val = readl(reg);
350 val &= ~SLWF_MASK;
351 val |= (slwf << SLWF_SHIFT);
352 writel(val, reg);
353
354 return;
355}
356
357static void padgrp_set_drvdn_slwr(enum pdrive_pingrp grp, int slwr)
358{
359 u32 *reg = DRV_REG(grp);
360 u32 val;
361
362 /* NONE means unspecified/do not change/use POR value */
363 if (slwr == PGRP_SLWR_NONE)
364 return;
365
366 /* Error check on pad and slwr */
367 assert(pmux_padgrp_isvalid(grp));
368 assert(pmux_pad_slw_isvalid(slwr));
369
370 val = readl(reg);
371 val &= ~SLWR_MASK;
372 val |= (slwr << SLWR_SHIFT);
373 writel(val, reg);
374
375 return;
376}
377
378static void padgrp_set_drvup(enum pdrive_pingrp grp, int drvup)
379{
380 u32 *reg = DRV_REG(grp);
381 u32 val;
382
383 /* NONE means unspecified/do not change/use POR value */
384 if (drvup == PGRP_DRVUP_NONE)
385 return;
386
387 /* Error check on pad and drvup */
388 assert(pmux_padgrp_isvalid(grp));
389 assert(pmux_pad_drv_isvalid(drvup));
390
391 val = readl(reg);
392 val &= ~DRVUP_MASK;
393 val |= (drvup << DRVUP_SHIFT);
394 writel(val, reg);
395
396 return;
397}
398
399static void padgrp_set_drvdn(enum pdrive_pingrp grp, int drvdn)
400{
401 u32 *reg = DRV_REG(grp);
402 u32 val;
403
404 /* NONE means unspecified/do not change/use POR value */
405 if (drvdn == PGRP_DRVDN_NONE)
406 return;
407
408 /* Error check on pad and drvdn */
409 assert(pmux_padgrp_isvalid(grp));
410 assert(pmux_pad_drv_isvalid(drvdn));
411
412 val = readl(reg);
413 val &= ~DRVDN_MASK;
414 val |= (drvdn << DRVDN_SHIFT);
415 writel(val, reg);
416
417 return;
418}
419
420static void padgrp_set_lpmd(enum pdrive_pingrp grp, enum pgrp_lpmd lpmd)
421{
422 u32 *reg = DRV_REG(grp);
423 u32 val;
424
425 /* NONE means unspecified/do not change/use POR value */
426 if (lpmd == PGRP_LPMD_NONE)
427 return;
428
429 /* Error check pad and lpmd value */
430 assert(pmux_padgrp_isvalid(grp));
431 assert(pmux_pad_lpmd_isvalid(lpmd));
432
433 val = readl(reg);
434 val &= ~LPMD_MASK;
435 val |= (lpmd << LPMD_SHIFT);
436 writel(val, reg);
437
438 return;
439}
440
441static void padgrp_set_schmt(enum pdrive_pingrp grp, enum pgrp_schmt schmt)
442{
443 u32 *reg = DRV_REG(grp);
444 u32 val;
445
446 /* NONE means unspecified/do not change/use POR value */
447 if (schmt == PGRP_SCHMT_NONE)
448 return;
449
450 /* Error check pad */
451 assert(pmux_padgrp_isvalid(grp));
452 assert(pmux_pad_schmt_isvalid(schmt));
453
454 val = readl(reg);
455 if (schmt == PGRP_SCHMT_ENABLE)
456 val |= (1 << SCHMT_SHIFT);
457 else
458 val &= ~(1 << SCHMT_SHIFT);
459 writel(val, reg);
460
461 return;
462}
463
464static void padgrp_set_hsm(enum pdrive_pingrp grp, enum pgrp_hsm hsm)
465{
466 u32 *reg = DRV_REG(grp);
467 u32 val;
468
469 /* NONE means unspecified/do not change/use POR value */
470 if (hsm == PGRP_HSM_NONE)
471 return;
472
473 /* Error check pad */
474 assert(pmux_padgrp_isvalid(grp));
475 assert(pmux_pad_hsm_isvalid(hsm));
476
477 val = readl(reg);
478 if (hsm == PGRP_HSM_ENABLE)
479 val |= (1 << HSM_SHIFT);
480 else
481 val &= ~(1 << HSM_SHIFT);
482 writel(val, reg);
483
484 return;
485}
486
487static void padctrl_config_pingroup(const struct padctrl_config *config)
488{
489 enum pdrive_pingrp grp = config->padgrp;
490
491 padgrp_set_drvup_slwf(grp, config->slwf);
492 padgrp_set_drvdn_slwr(grp, config->slwr);
493 padgrp_set_drvup(grp, config->drvup);
494 padgrp_set_drvdn(grp, config->drvdn);
495 padgrp_set_lpmd(grp, config->lpmd);
496 padgrp_set_schmt(grp, config->schmt);
497 padgrp_set_hsm(grp, config->hsm);
498}
499
500void padgrp_config_table(const struct padctrl_config *config, int len)
501{
502 int i;
503
504 for (i = 0; i < len; i++)
505 padctrl_config_pingroup(&config[i]);
506}
507#endif /* TEGRA_PMX_HAS_PADGRPS */