1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2017 Socionext Inc.
10 #include <linux/log2.h>
13 #include "../sbc/sbc-regs.h"
14 #include "../sg-regs.h"
15 #include "../soc-info.h"
16 #include "boot-device.h"
18 struct uniphier_boot_device_info {
20 unsigned int boot_device_sel_shift;
21 const struct uniphier_boot_device *boot_device_table;
22 const unsigned int *boot_device_count;
23 int (*boot_device_is_usb)(u32 pinmon);
24 unsigned int (*boot_device_fixup)(unsigned int mode);
25 bool have_internal_stm;
28 static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
29 #if defined(CONFIG_ARCH_UNIPHIER_LD4)
31 .soc_id = UNIPHIER_LD4_ID,
32 .boot_device_sel_shift = 1,
33 .boot_device_table = uniphier_ld4_boot_device_table,
34 .boot_device_count = &uniphier_ld4_boot_device_count,
35 .have_internal_stm = true,
38 #if defined(CONFIG_ARCH_UNIPHIER_PRO4)
40 .soc_id = UNIPHIER_PRO4_ID,
41 .boot_device_sel_shift = 1,
42 .boot_device_table = uniphier_ld4_boot_device_table,
43 .boot_device_count = &uniphier_ld4_boot_device_count,
44 .have_internal_stm = false,
47 #if defined(CONFIG_ARCH_UNIPHIER_SLD8)
49 .soc_id = UNIPHIER_SLD8_ID,
50 .boot_device_sel_shift = 1,
51 .boot_device_table = uniphier_ld4_boot_device_table,
52 .boot_device_count = &uniphier_ld4_boot_device_count,
53 .have_internal_stm = true,
56 #if defined(CONFIG_ARCH_UNIPHIER_PRO5)
58 .soc_id = UNIPHIER_PRO5_ID,
59 .boot_device_sel_shift = 1,
60 .boot_device_table = uniphier_pro5_boot_device_table,
61 .boot_device_count = &uniphier_pro5_boot_device_count,
62 .have_internal_stm = false,
65 #if defined(CONFIG_ARCH_UNIPHIER_PXS2)
67 .soc_id = UNIPHIER_PXS2_ID,
68 .boot_device_sel_shift = 1,
69 .boot_device_table = uniphier_pxs2_boot_device_table,
70 .boot_device_count = &uniphier_pxs2_boot_device_count,
71 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
72 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
73 .have_internal_stm = false,
76 #if defined(CONFIG_ARCH_UNIPHIER_LD6B)
78 .soc_id = UNIPHIER_LD6B_ID,
79 .boot_device_sel_shift = 1,
80 .boot_device_table = uniphier_pxs2_boot_device_table,
81 .boot_device_count = &uniphier_pxs2_boot_device_count,
82 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
83 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
84 .have_internal_stm = true, /* STM on A-chip */
87 #if defined(CONFIG_ARCH_UNIPHIER_LD11)
89 .soc_id = UNIPHIER_LD11_ID,
90 .boot_device_sel_shift = 1,
91 .boot_device_table = uniphier_ld11_boot_device_table,
92 .boot_device_count = &uniphier_ld11_boot_device_count,
93 .boot_device_is_usb = uniphier_ld11_boot_device_is_usb,
94 .have_internal_stm = true,
97 #if defined(CONFIG_ARCH_UNIPHIER_LD20)
99 .soc_id = UNIPHIER_LD20_ID,
100 .boot_device_sel_shift = 1,
101 .boot_device_table = uniphier_ld11_boot_device_table,
102 .boot_device_count = &uniphier_ld11_boot_device_count,
103 .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
104 .have_internal_stm = true,
107 #if defined(CONFIG_ARCH_UNIPHIER_PXS3)
109 .soc_id = UNIPHIER_PXS3_ID,
110 .boot_device_sel_shift = 1,
111 .boot_device_table = uniphier_pxs3_boot_device_table,
112 .boot_device_count = &uniphier_pxs3_boot_device_count,
113 .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
114 .have_internal_stm = false,
118 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
119 uniphier_boot_device_info)
121 static unsigned int __uniphier_boot_device_raw(
122 const struct uniphier_boot_device_info *info)
125 unsigned int boot_sel;
127 if (boot_is_swapped())
128 return BOOT_DEVICE_NOR;
130 pinmon = readl(SG_PINMON0);
132 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
133 return BOOT_DEVICE_USB;
135 boot_sel = pinmon >> info->boot_device_sel_shift;
137 BUG_ON(!is_power_of_2(*info->boot_device_count));
138 boot_sel &= *info->boot_device_count - 1;
140 return info->boot_device_table[boot_sel].boot_device;
143 unsigned int uniphier_boot_device_raw(void)
145 const struct uniphier_boot_device_info *info;
147 info = uniphier_get_boot_device_info();
149 pr_err("unsupported SoC\n");
150 return BOOT_DEVICE_NONE;
153 return __uniphier_boot_device_raw(info);
156 u32 spl_boot_device(void)
158 const struct uniphier_boot_device_info *info;
161 info = uniphier_get_boot_device_info();
163 pr_err("unsupported SoC\n");
164 return BOOT_DEVICE_NONE;
167 raw_mode = __uniphier_boot_device_raw(info);
169 return info->boot_device_fixup ?
170 info->boot_device_fixup(raw_mode) : raw_mode;
173 int uniphier_have_internal_stm(void)
175 const struct uniphier_boot_device_info *info;
177 info = uniphier_get_boot_device_info();
179 pr_err("unsupported SoC\n");
183 return info->have_internal_stm;
186 int uniphier_boot_from_backend(void)
188 return !!(readl(SG_PINMON0) & BIT(27));
191 #ifndef CONFIG_SPL_BUILD
193 static int do_pinmon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
195 const struct uniphier_boot_device_info *info;
197 unsigned int boot_device_count, boot_sel;
200 info = uniphier_get_boot_device_info();
202 pr_err("unsupported SoC\n");
203 return CMD_RET_FAILURE;
206 if (uniphier_have_internal_stm())
207 printf("STB Micon: %s\n",
208 uniphier_boot_from_backend() ? "OFF" : "ON");
210 printf("Boot Swap: %s\n", boot_is_swapped() ? "ON" : "OFF");
212 pinmon = readl(SG_PINMON0);
214 if (info->boot_device_is_usb)
215 printf("USB Boot: %s\n",
216 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
218 boot_device_count = *info->boot_device_count;
220 boot_sel = pinmon >> info->boot_device_sel_shift;
221 boot_sel &= boot_device_count - 1;
223 printf("\nBoot Mode Sel:\n");
224 for (i = 0; i < boot_device_count; i++)
225 printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
226 info->boot_device_table[i].desc);
228 return CMD_RET_SUCCESS;
232 pinmon, 1, 1, do_pinmon,
237 #endif /* !CONFIG_SPL_BUILD */