blob: 98ff34cfa7130b588cfa73283b1cd58108a12868 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadafb092032017-02-14 01:24:26 +09002/*
3 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadafb092032017-02-14 01:24:26 +09005 */
6
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Masahiro Yamadafb092032017-02-14 01:24:26 +09008#include <spl.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +09009#include <stdio.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060011#include <linux/bug.h>
Masahiro Yamadae6314412020-05-20 12:31:27 +090012#include <linux/errno.h>
Masahiro Yamada92727322019-07-10 20:07:34 +090013#include <linux/io.h>
Masahiro Yamadafb092032017-02-14 01:24:26 +090014#include <linux/log2.h>
15
16#include "../init.h"
Masahiro Yamadafb092032017-02-14 01:24:26 +090017#include "../sg-regs.h"
18#include "../soc-info.h"
19#include "boot-device.h"
20
Masahiro Yamada98b5f342020-07-09 15:08:20 +090021#define SBBASE0 0x58c00100
22#define SBBASE_BANK_ENABLE BIT(0)
23
24static int uniphier_sbc_boot_is_swapped(void)
25{
26 return !(readl(SBBASE0) & SBBASE_BANK_ENABLE);
27}
28
Masahiro Yamadafb092032017-02-14 01:24:26 +090029struct 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;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +090034 int (*boot_device_is_sd)(u32 pinmon);
Masahiro Yamadafb092032017-02-14 01:24:26 +090035 int (*boot_device_is_usb)(u32 pinmon);
36 unsigned int (*boot_device_fixup)(unsigned int mode);
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090037 int (*boot_is_swapped)(void);
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090038 bool have_internal_stm;
Masahiro Yamadafb092032017-02-14 01:24:26 +090039};
40
41static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
Masahiro Yamadafb092032017-02-14 01:24:26 +090042#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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090048 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090049 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090050 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090058 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090059 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090060 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090068 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090069 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090070 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090078 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090079 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090080 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090090 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090091 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090092 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900102 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900103 .have_internal_stm = true, /* STM on A-chip */
Masahiro Yamadafb092032017-02-14 01:24:26 +0900104 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900113 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900114 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +0900115 },
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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900124 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900125 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +0900126 },
127#endif
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900128#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,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900135 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900136 .have_internal_stm = false,
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900137 },
138#endif
Masahiro Yamadafb092032017-02-14 01:24:26 +0900139};
140UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
141 uniphier_boot_device_info)
142
143static unsigned int __uniphier_boot_device_raw(
144 const struct uniphier_boot_device_info *info)
145{
146 u32 pinmon;
147 unsigned int boot_sel;
148
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900149 if (info->boot_is_swapped && info->boot_is_swapped())
Masahiro Yamadafb092032017-02-14 01:24:26 +0900150 return BOOT_DEVICE_NOR;
151
Masahiro Yamada76b31242019-07-10 20:07:40 +0900152 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamadafb092032017-02-14 01:24:26 +0900153
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900154 if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon))
155 return BOOT_DEVICE_MMC2;
156
Masahiro Yamadafb092032017-02-14 01:24:26 +0900157 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
168unsigned 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
181u32 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
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900198int 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
211int uniphier_boot_from_backend(void)
212{
Masahiro Yamada76b31242019-07-10 20:07:40 +0900213 return !!(readl(sg_base + SG_PINMON0) & BIT(27));
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900214}
215
Masahiro Yamadafb092032017-02-14 01:24:26 +0900216#ifndef CONFIG_SPL_BUILD
217
Simon Glassed38aef2020-05-10 11:40:03 -0600218static int do_pinmon(struct cmd_tbl *cmdtp, int flag, int argc,
219 char *const argv[])
Masahiro Yamadafb092032017-02-14 01:24:26 +0900220{
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
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900232 if (uniphier_have_internal_stm())
233 printf("STB Micon: %s\n",
234 uniphier_boot_from_backend() ? "OFF" : "ON");
235
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900236 if (info->boot_is_swapped)
237 printf("Boot Swap: %s\n",
238 info->boot_is_swapped() ? "ON" : "OFF");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900239
Masahiro Yamada76b31242019-07-10 20:07:40 +0900240 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamadafb092032017-02-14 01:24:26 +0900241
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900242 if (info->boot_device_is_sd)
243 printf("SD Boot: %s\n",
244 info->boot_device_is_sd(pinmon) ? "ON" : "OFF");
245
Masahiro Yamadafb092032017-02-14 01:24:26 +0900246 if (info->boot_device_is_usb)
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900247 printf("USB Boot: %s\n",
Masahiro Yamadafb092032017-02-14 01:24:26 +0900248 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
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900255 printf("\nBoot Mode Sel:\n");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900256 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
263U_BOOT_CMD(
264 pinmon, 1, 1, do_pinmon,
265 "pin monitor",
266 ""
267);
268
269#endif /* !CONFIG_SPL_BUILD */