1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 */
6
7 #include <command.h>
8 #include <spl.h>
9 #include <stdio.h>
10 #include <linux/bitops.h>
11 #include <linux/bug.h>
12 #include <linux/errno.h>
13 #include <linux/io.h>
14 #include <linux/log2.h>
15
16 #include "../init.h"
17 #include "../sg-regs.h"
18 #include "../soc-info.h"
19 #include "boot-device.h"
20
21 #define SBBASE0 0x58c00100
22 #define SBBASE_BANK_ENABLE BIT(0)
23
uniphier_sbc_boot_is_swapped(void)24 static int uniphier_sbc_boot_is_swapped(void)
25 {
26 return !(readl(SBBASE0) & SBBASE_BANK_ENABLE);
27 }
28
29 struct uniphier_boot_device_info {
30 unsigned int soc_id;
31 unsigned int boot_device_sel_shift;
32 const struct uniphier_boot_device *boot_device_table;
33 const unsigned int *boot_device_count;
34 int (*boot_device_is_sd)(u32 pinmon);
35 int (*boot_device_is_usb)(u32 pinmon);
36 unsigned int (*boot_device_fixup)(unsigned int mode);
37 int (*boot_is_swapped)(void);
38 bool have_internal_stm;
39 };
40
41 static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
42 #if defined(CONFIG_ARCH_UNIPHIER_LD4)
43 {
44 .soc_id = UNIPHIER_LD4_ID,
45 .boot_device_sel_shift = 1,
46 .boot_device_table = uniphier_ld4_boot_device_table,
47 .boot_device_count = &uniphier_ld4_boot_device_count,
48 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
49 .have_internal_stm = true,
50 },
51 #endif
52 #if defined(CONFIG_ARCH_UNIPHIER_PRO4)
53 {
54 .soc_id = UNIPHIER_PRO4_ID,
55 .boot_device_sel_shift = 1,
56 .boot_device_table = uniphier_ld4_boot_device_table,
57 .boot_device_count = &uniphier_ld4_boot_device_count,
58 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
59 .have_internal_stm = false,
60 },
61 #endif
62 #if defined(CONFIG_ARCH_UNIPHIER_SLD8)
63 {
64 .soc_id = UNIPHIER_SLD8_ID,
65 .boot_device_sel_shift = 1,
66 .boot_device_table = uniphier_ld4_boot_device_table,
67 .boot_device_count = &uniphier_ld4_boot_device_count,
68 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
69 .have_internal_stm = true,
70 },
71 #endif
72 #if defined(CONFIG_ARCH_UNIPHIER_PRO5)
73 {
74 .soc_id = UNIPHIER_PRO5_ID,
75 .boot_device_sel_shift = 1,
76 .boot_device_table = uniphier_pro5_boot_device_table,
77 .boot_device_count = &uniphier_pro5_boot_device_count,
78 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
79 .have_internal_stm = false,
80 },
81 #endif
82 #if defined(CONFIG_ARCH_UNIPHIER_PXS2)
83 {
84 .soc_id = UNIPHIER_PXS2_ID,
85 .boot_device_sel_shift = 1,
86 .boot_device_table = uniphier_pxs2_boot_device_table,
87 .boot_device_count = &uniphier_pxs2_boot_device_count,
88 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
89 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
90 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
91 .have_internal_stm = false,
92 },
93 #endif
94 #if defined(CONFIG_ARCH_UNIPHIER_LD6B)
95 {
96 .soc_id = UNIPHIER_LD6B_ID,
97 .boot_device_sel_shift = 1,
98 .boot_device_table = uniphier_pxs2_boot_device_table,
99 .boot_device_count = &uniphier_pxs2_boot_device_count,
100 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
101 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
102 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
103 .have_internal_stm = true, /* STM on A-chip */
104 },
105 #endif
106 #if defined(CONFIG_ARCH_UNIPHIER_LD11)
107 {
108 .soc_id = UNIPHIER_LD11_ID,
109 .boot_device_sel_shift = 1,
110 .boot_device_table = uniphier_ld11_boot_device_table,
111 .boot_device_count = &uniphier_ld11_boot_device_count,
112 .boot_device_is_usb = uniphier_ld11_boot_device_is_usb,
113 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
114 .have_internal_stm = true,
115 },
116 #endif
117 #if defined(CONFIG_ARCH_UNIPHIER_LD20)
118 {
119 .soc_id = UNIPHIER_LD20_ID,
120 .boot_device_sel_shift = 1,
121 .boot_device_table = uniphier_ld11_boot_device_table,
122 .boot_device_count = &uniphier_ld11_boot_device_count,
123 .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
124 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
125 .have_internal_stm = true,
126 },
127 #endif
128 #if defined(CONFIG_ARCH_UNIPHIER_PXS3)
129 {
130 .soc_id = UNIPHIER_PXS3_ID,
131 .boot_device_sel_shift = 1,
132 .boot_device_table = uniphier_pxs3_boot_device_table,
133 .boot_device_count = &uniphier_pxs3_boot_device_count,
134 .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
135 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
136 .have_internal_stm = false,
137 },
138 #endif
139 };
UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,uniphier_boot_device_info)140 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
141 uniphier_boot_device_info)
142
143 static unsigned int __uniphier_boot_device_raw(
144 const struct uniphier_boot_device_info *info)
145 {
146 u32 pinmon;
147 unsigned int boot_sel;
148
149 if (info->boot_is_swapped && info->boot_is_swapped())
150 return BOOT_DEVICE_NOR;
151
152 pinmon = readl(sg_base + SG_PINMON0);
153
154 if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon))
155 return BOOT_DEVICE_MMC2;
156
157 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
158 return BOOT_DEVICE_USB;
159
160 boot_sel = pinmon >> info->boot_device_sel_shift;
161
162 BUG_ON(!is_power_of_2(*info->boot_device_count));
163 boot_sel &= *info->boot_device_count - 1;
164
165 return info->boot_device_table[boot_sel].boot_device;
166 }
167
uniphier_boot_device_raw(void)168 unsigned int uniphier_boot_device_raw(void)
169 {
170 const struct uniphier_boot_device_info *info;
171
172 info = uniphier_get_boot_device_info();
173 if (!info) {
174 pr_err("unsupported SoC\n");
175 return BOOT_DEVICE_NONE;
176 }
177
178 return __uniphier_boot_device_raw(info);
179 }
180
spl_boot_device(void)181 u32 spl_boot_device(void)
182 {
183 const struct uniphier_boot_device_info *info;
184 u32 raw_mode;
185
186 info = uniphier_get_boot_device_info();
187 if (!info) {
188 pr_err("unsupported SoC\n");
189 return BOOT_DEVICE_NONE;
190 }
191
192 raw_mode = __uniphier_boot_device_raw(info);
193
194 return info->boot_device_fixup ?
195 info->boot_device_fixup(raw_mode) : raw_mode;
196 }
197
uniphier_have_internal_stm(void)198 int uniphier_have_internal_stm(void)
199 {
200 const struct uniphier_boot_device_info *info;
201
202 info = uniphier_get_boot_device_info();
203 if (!info) {
204 pr_err("unsupported SoC\n");
205 return -ENOTSUPP;
206 }
207
208 return info->have_internal_stm;
209 }
210
uniphier_boot_from_backend(void)211 int uniphier_boot_from_backend(void)
212 {
213 return !!(readl(sg_base + SG_PINMON0) & BIT(27));
214 }
215
216 #ifndef CONFIG_SPL_BUILD
217
do_pinmon(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])218 static int do_pinmon(struct cmd_tbl *cmdtp, int flag, int argc,
219 char *const argv[])
220 {
221 const struct uniphier_boot_device_info *info;
222 u32 pinmon;
223 unsigned int boot_device_count, boot_sel;
224 int i;
225
226 info = uniphier_get_boot_device_info();
227 if (!info) {
228 pr_err("unsupported SoC\n");
229 return CMD_RET_FAILURE;
230 }
231
232 if (uniphier_have_internal_stm())
233 printf("STB Micon: %s\n",
234 uniphier_boot_from_backend() ? "OFF" : "ON");
235
236 if (info->boot_is_swapped)
237 printf("Boot Swap: %s\n",
238 info->boot_is_swapped() ? "ON" : "OFF");
239
240 pinmon = readl(sg_base + SG_PINMON0);
241
242 if (info->boot_device_is_sd)
243 printf("SD Boot: %s\n",
244 info->boot_device_is_sd(pinmon) ? "ON" : "OFF");
245
246 if (info->boot_device_is_usb)
247 printf("USB Boot: %s\n",
248 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
249
250 boot_device_count = *info->boot_device_count;
251
252 boot_sel = pinmon >> info->boot_device_sel_shift;
253 boot_sel &= boot_device_count - 1;
254
255 printf("\nBoot Mode Sel:\n");
256 for (i = 0; i < boot_device_count; i++)
257 printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
258 info->boot_device_table[i].desc);
259
260 return CMD_RET_SUCCESS;
261 }
262
263 U_BOOT_CMD(
264 pinmon, 1, 1, do_pinmon,
265 "pin monitor",
266 ""
267 );
268
269 #endif /* !CONFIG_SPL_BUILD */
270