blob: bda2f3ac3a69c8c06e9a159639f748c1b19f40d4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mingkai Hueee86ff2015-10-26 19:47:52 +08002/*
3 * Copyright 2015 Freescale Semiconductor
4 *
Mingkai Hueee86ff2015-10-26 19:47:52 +08005 * Freescale LS1043ARDB board-specific CPLD controlling supports.
6 */
7
Tom Rinidec7ea02024-05-20 13:35:03 -06008#include <config.h>
Mingkai Hueee86ff2015-10-26 19:47:52 +08009#include <command.h>
10#include <asm/io.h>
11#include "cpld.h"
12
13u8 cpld_read(unsigned int reg)
14{
Tom Rini6a5dccc2022-11-16 13:10:41 -050015 void *p = (void *)CFG_SYS_CPLD_BASE;
Mingkai Hueee86ff2015-10-26 19:47:52 +080016
17 return in_8(p + reg);
18}
19
20void cpld_write(unsigned int reg, u8 value)
21{
Tom Rini6a5dccc2022-11-16 13:10:41 -050022 void *p = (void *)CFG_SYS_CPLD_BASE;
Mingkai Hueee86ff2015-10-26 19:47:52 +080023
24 out_8(p + reg, value);
25}
26
27/* Set the boot bank to the alternate bank */
28void cpld_set_altbank(void)
29{
Qianyu Gong24be17c2016-04-25 16:38:35 +080030 u16 reg = CPLD_CFG_RCW_SRC_NOR;
Mingkai Hueee86ff2015-10-26 19:47:52 +080031 u8 reg4 = CPLD_READ(soft_mux_on);
Qianyu Gong24be17c2016-04-25 16:38:35 +080032 u8 reg5 = (u8)(reg >> 1);
33 u8 reg6 = (u8)(reg & 1);
Mingkai Hueee86ff2015-10-26 19:47:52 +080034 u8 reg7 = CPLD_READ(vbank);
35
Qianyu Gong24be17c2016-04-25 16:38:35 +080036 cpld_rev_bit(&reg5);
37
38 CPLD_WRITE(soft_mux_on, reg4 | CPLD_SW_MUX_BANK_SEL | 1);
39
40 CPLD_WRITE(cfg_rcw_src1, reg5);
41 CPLD_WRITE(cfg_rcw_src2, reg6);
Mingkai Hueee86ff2015-10-26 19:47:52 +080042
43 reg7 = (reg7 & ~CPLD_BANK_SEL_MASK) | CPLD_BANK_SEL_ALTBANK;
44 CPLD_WRITE(vbank, reg7);
45
46 CPLD_WRITE(system_rst, 1);
47}
48
49/* Set the boot bank to the default bank */
50void cpld_set_defbank(void)
51{
Qianyu Gong24be17c2016-04-25 16:38:35 +080052 u16 reg = CPLD_CFG_RCW_SRC_NOR;
53 u8 reg4 = CPLD_READ(soft_mux_on);
54 u8 reg5 = (u8)(reg >> 1);
55 u8 reg6 = (u8)(reg & 1);
56
57 cpld_rev_bit(&reg5);
58
59 CPLD_WRITE(soft_mux_on, reg4 | CPLD_SW_MUX_BANK_SEL | 1);
60
61 CPLD_WRITE(cfg_rcw_src1, reg5);
62 CPLD_WRITE(cfg_rcw_src2, reg6);
63
64 CPLD_WRITE(vbank, 0);
65
66 CPLD_WRITE(system_rst, 1);
Mingkai Hueee86ff2015-10-26 19:47:52 +080067}
68
Gong Qianyu8168a0f2015-10-26 19:47:53 +080069void cpld_set_nand(void)
70{
71 u16 reg = CPLD_CFG_RCW_SRC_NAND;
Wei Lu795e8062022-09-26 16:18:49 +080072
73 if (CPLD_READ(cpld_ver) > 0x2)
74 reg = CPLD_CFG_RCW_SRC_NAND_4K;
75
Gong Qianyu8168a0f2015-10-26 19:47:53 +080076 u8 reg5 = (u8)(reg >> 1);
77 u8 reg6 = (u8)(reg & 1);
78
79 cpld_rev_bit(&reg5);
80
81 CPLD_WRITE(soft_mux_on, 1);
82
83 CPLD_WRITE(cfg_rcw_src1, reg5);
84 CPLD_WRITE(cfg_rcw_src2, reg6);
85
86 CPLD_WRITE(system_rst, 1);
87}
88
Gong Qianyuf671f6c2015-10-26 19:47:56 +080089void cpld_set_sd(void)
90{
91 u16 reg = CPLD_CFG_RCW_SRC_SD;
92 u8 reg5 = (u8)(reg >> 1);
93 u8 reg6 = (u8)(reg & 1);
94
95 cpld_rev_bit(&reg5);
96
97 CPLD_WRITE(soft_mux_on, 1);
98
99 CPLD_WRITE(cfg_rcw_src1, reg5);
100 CPLD_WRITE(cfg_rcw_src2, reg6);
101
102 CPLD_WRITE(system_rst, 1);
103}
Mingkai Hueee86ff2015-10-26 19:47:52 +0800104#ifdef DEBUG
105static void cpld_dump_regs(void)
106{
107 printf("cpld_ver = %x\n", CPLD_READ(cpld_ver));
108 printf("cpld_ver_sub = %x\n", CPLD_READ(cpld_ver_sub));
109 printf("pcba_ver = %x\n", CPLD_READ(pcba_ver));
110 printf("soft_mux_on = %x\n", CPLD_READ(soft_mux_on));
111 printf("cfg_rcw_src1 = %x\n", CPLD_READ(cfg_rcw_src1));
112 printf("cfg_rcw_src2 = %x\n", CPLD_READ(cfg_rcw_src2));
113 printf("vbank = %x\n", CPLD_READ(vbank));
114 printf("sysclk_sel = %x\n", CPLD_READ(sysclk_sel));
115 printf("uart_sel = %x\n", CPLD_READ(uart_sel));
116 printf("sd1refclk_sel = %x\n", CPLD_READ(sd1refclk_sel));
117 printf("tdmclk_mux_sel = %x\n", CPLD_READ(tdmclk_mux_sel));
118 printf("sdhc_spics_sel = %x\n", CPLD_READ(sdhc_spics_sel));
119 printf("status_led = %x\n", CPLD_READ(status_led));
120 putc('\n');
121}
122#endif
123
124void cpld_rev_bit(unsigned char *value)
125{
126 u8 rev_val, val;
127 int i;
128
129 val = *value;
130 rev_val = val & 1;
131 for (i = 1; i <= 7; i++) {
132 val >>= 1;
133 rev_val <<= 1;
134 rev_val |= val & 1;
135 }
136
137 *value = rev_val;
138}
139
Simon Glassed38aef2020-05-10 11:40:03 -0600140int do_cpld(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Mingkai Hueee86ff2015-10-26 19:47:52 +0800141{
142 int rc = 0;
143
144 if (argc <= 1)
145 return cmd_usage(cmdtp);
146
147 if (strcmp(argv[1], "reset") == 0) {
148 if (strcmp(argv[2], "altbank") == 0)
149 cpld_set_altbank();
Gong Qianyu8168a0f2015-10-26 19:47:53 +0800150 else if (strcmp(argv[2], "nand") == 0)
151 cpld_set_nand();
Gong Qianyuf671f6c2015-10-26 19:47:56 +0800152 else if (strcmp(argv[2], "sd") == 0)
153 cpld_set_sd();
Mingkai Hueee86ff2015-10-26 19:47:52 +0800154 else
155 cpld_set_defbank();
156#ifdef DEBUG
157 } else if (strcmp(argv[1], "dump") == 0) {
158 cpld_dump_regs();
159#endif
160 } else {
161 rc = cmd_usage(cmdtp);
162 }
163
164 return rc;
165}
166
167U_BOOT_CMD(
168 cpld, CONFIG_SYS_MAXARGS, 1, do_cpld,
169 "Reset the board or alternate bank",
170 "reset: reset to default bank\n"
171 "cpld reset altbank: reset to alternate bank\n"
Gong Qianyu8168a0f2015-10-26 19:47:53 +0800172 "cpld reset nand: reset to boot from NAND flash\n"
Gong Qianyuf671f6c2015-10-26 19:47:56 +0800173 "cpld reset sd: reset to boot from SD card\n"
Mingkai Hueee86ff2015-10-26 19:47:52 +0800174#ifdef DEBUG
175 "cpld dump - display the CPLD registers\n"
176#endif
177);