1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2017 Socionext Inc.
11 #include <linux/log2.h>
14 #include "../sbc/sbc-regs.h"
15 #include "../sg-regs.h"
16 #include "../soc-info.h"
17 #include "boot-device.h"
19 struct uniphier_boot_device_info {
21 unsigned int boot_device_sel_shift;
22 const struct uniphier_boot_device *boot_device_table;
23 const unsigned int *boot_device_count;
24 int (*boot_device_is_sd)(u32 pinmon);
25 int (*boot_device_is_usb)(u32 pinmon);
26 unsigned int (*boot_device_fixup)(unsigned int mode);
27 int (*boot_is_swapped)(void);
28 bool have_internal_stm;
31 static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
32 #if defined(CONFIG_ARCH_UNIPHIER_LD4)
34 .soc_id = UNIPHIER_LD4_ID,
35 .boot_device_sel_shift = 1,
36 .boot_device_table = uniphier_ld4_boot_device_table,
37 .boot_device_count = &uniphier_ld4_boot_device_count,
38 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
39 .have_internal_stm = true,
42 #if defined(CONFIG_ARCH_UNIPHIER_PRO4)
44 .soc_id = UNIPHIER_PRO4_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 = false,
52 #if defined(CONFIG_ARCH_UNIPHIER_SLD8)
54 .soc_id = UNIPHIER_SLD8_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 = true,
62 #if defined(CONFIG_ARCH_UNIPHIER_PRO5)
64 .soc_id = UNIPHIER_PRO5_ID,
65 .boot_device_sel_shift = 1,
66 .boot_device_table = uniphier_pro5_boot_device_table,
67 .boot_device_count = &uniphier_pro5_boot_device_count,
68 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
69 .have_internal_stm = false,
72 #if defined(CONFIG_ARCH_UNIPHIER_PXS2)
74 .soc_id = UNIPHIER_PXS2_ID,
75 .boot_device_sel_shift = 1,
76 .boot_device_table = uniphier_pxs2_boot_device_table,
77 .boot_device_count = &uniphier_pxs2_boot_device_count,
78 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
79 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
80 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
81 .have_internal_stm = false,
84 #if defined(CONFIG_ARCH_UNIPHIER_LD6B)
86 .soc_id = UNIPHIER_LD6B_ID,
87 .boot_device_sel_shift = 1,
88 .boot_device_table = uniphier_pxs2_boot_device_table,
89 .boot_device_count = &uniphier_pxs2_boot_device_count,
90 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
91 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
92 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
93 .have_internal_stm = true, /* STM on A-chip */
96 #if defined(CONFIG_ARCH_UNIPHIER_LD11)
98 .soc_id = UNIPHIER_LD11_ID,
99 .boot_device_sel_shift = 1,
100 .boot_device_table = uniphier_ld11_boot_device_table,
101 .boot_device_count = &uniphier_ld11_boot_device_count,
102 .boot_device_is_usb = uniphier_ld11_boot_device_is_usb,
103 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
104 .have_internal_stm = true,
107 #if defined(CONFIG_ARCH_UNIPHIER_LD20)
109 .soc_id = UNIPHIER_LD20_ID,
110 .boot_device_sel_shift = 1,
111 .boot_device_table = uniphier_ld11_boot_device_table,
112 .boot_device_count = &uniphier_ld11_boot_device_count,
113 .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
114 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
115 .have_internal_stm = true,
118 #if defined(CONFIG_ARCH_UNIPHIER_PXS3)
120 .soc_id = UNIPHIER_PXS3_ID,
121 .boot_device_sel_shift = 1,
122 .boot_device_table = uniphier_pxs3_boot_device_table,
123 .boot_device_count = &uniphier_pxs3_boot_device_count,
124 .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
125 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
126 .have_internal_stm = false,
130 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
131 uniphier_boot_device_info)
133 static unsigned int __uniphier_boot_device_raw(
134 const struct uniphier_boot_device_info *info)
137 unsigned int boot_sel;
139 if (info->boot_is_swapped && info->boot_is_swapped())
140 return BOOT_DEVICE_NOR;
142 pinmon = readl(sg_base + SG_PINMON0);
144 if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon))
145 return BOOT_DEVICE_MMC2;
147 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
148 return BOOT_DEVICE_USB;
150 boot_sel = pinmon >> info->boot_device_sel_shift;
152 BUG_ON(!is_power_of_2(*info->boot_device_count));
153 boot_sel &= *info->boot_device_count - 1;
155 return info->boot_device_table[boot_sel].boot_device;
158 unsigned int uniphier_boot_device_raw(void)
160 const struct uniphier_boot_device_info *info;
162 info = uniphier_get_boot_device_info();
164 pr_err("unsupported SoC\n");
165 return BOOT_DEVICE_NONE;
168 return __uniphier_boot_device_raw(info);
171 u32 spl_boot_device(void)
173 const struct uniphier_boot_device_info *info;
176 info = uniphier_get_boot_device_info();
178 pr_err("unsupported SoC\n");
179 return BOOT_DEVICE_NONE;
182 raw_mode = __uniphier_boot_device_raw(info);
184 return info->boot_device_fixup ?
185 info->boot_device_fixup(raw_mode) : raw_mode;
188 int uniphier_have_internal_stm(void)
190 const struct uniphier_boot_device_info *info;
192 info = uniphier_get_boot_device_info();
194 pr_err("unsupported SoC\n");
198 return info->have_internal_stm;
201 int uniphier_boot_from_backend(void)
203 return !!(readl(sg_base + SG_PINMON0) & BIT(27));
206 #ifndef CONFIG_SPL_BUILD
208 static int do_pinmon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
210 const struct uniphier_boot_device_info *info;
212 unsigned int boot_device_count, boot_sel;
215 info = uniphier_get_boot_device_info();
217 pr_err("unsupported SoC\n");
218 return CMD_RET_FAILURE;
221 if (uniphier_have_internal_stm())
222 printf("STB Micon: %s\n",
223 uniphier_boot_from_backend() ? "OFF" : "ON");
225 if (info->boot_is_swapped)
226 printf("Boot Swap: %s\n",
227 info->boot_is_swapped() ? "ON" : "OFF");
229 pinmon = readl(sg_base + SG_PINMON0);
231 if (info->boot_device_is_sd)
232 printf("SD Boot: %s\n",
233 info->boot_device_is_sd(pinmon) ? "ON" : "OFF");
235 if (info->boot_device_is_usb)
236 printf("USB Boot: %s\n",
237 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
239 boot_device_count = *info->boot_device_count;
241 boot_sel = pinmon >> info->boot_device_sel_shift;
242 boot_sel &= boot_device_count - 1;
244 printf("\nBoot Mode Sel:\n");
245 for (i = 0; i < boot_device_count; i++)
246 printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
247 info->boot_device_table[i].desc);
249 return CMD_RET_SUCCESS;
253 pinmon, 1, 1, do_pinmon,
258 #endif /* !CONFIG_SPL_BUILD */