blob: 1e6bc8433f7d3bafd52e5925f8ca8747a33fd810 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Masahiro Yamadafb092032017-02-14 01:24:26 +090016
17#include "../init.h"
Masahiro Yamadafb092032017-02-14 01:24:26 +090018#include "../sg-regs.h"
19#include "../soc-info.h"
20#include "boot-device.h"
21
Masahiro Yamada98b5f342020-07-09 15:08:20 +090022#define SBBASE0 0x58c00100
23#define SBBASE_BANK_ENABLE BIT(0)
24
25static int uniphier_sbc_boot_is_swapped(void)
26{
27 return !(readl(SBBASE0) & SBBASE_BANK_ENABLE);
28}
29
Masahiro Yamadafb092032017-02-14 01:24:26 +090030struct uniphier_boot_device_info {
31 unsigned int soc_id;
32 unsigned int boot_device_sel_shift;
33 const struct uniphier_boot_device *boot_device_table;
34 const unsigned int *boot_device_count;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +090035 int (*boot_device_is_sd)(u32 pinmon);
Masahiro Yamadafb092032017-02-14 01:24:26 +090036 int (*boot_device_is_usb)(u32 pinmon);
37 unsigned int (*boot_device_fixup)(unsigned int mode);
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090038 int (*boot_is_swapped)(void);
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090039 bool have_internal_stm;
Masahiro Yamadafb092032017-02-14 01:24:26 +090040};
41
42static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
Masahiro Yamadafb092032017-02-14 01:24:26 +090043#if defined(CONFIG_ARCH_UNIPHIER_LD4)
44 {
45 .soc_id = UNIPHIER_LD4_ID,
46 .boot_device_sel_shift = 1,
47 .boot_device_table = uniphier_ld4_boot_device_table,
48 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090049 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090050 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090051 },
52#endif
53#if defined(CONFIG_ARCH_UNIPHIER_PRO4)
54 {
55 .soc_id = UNIPHIER_PRO4_ID,
56 .boot_device_sel_shift = 1,
57 .boot_device_table = uniphier_ld4_boot_device_table,
58 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090059 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090060 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090061 },
62#endif
63#if defined(CONFIG_ARCH_UNIPHIER_SLD8)
64 {
65 .soc_id = UNIPHIER_SLD8_ID,
66 .boot_device_sel_shift = 1,
67 .boot_device_table = uniphier_ld4_boot_device_table,
68 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090069 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090070 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090071 },
72#endif
73#if defined(CONFIG_ARCH_UNIPHIER_PRO5)
74 {
75 .soc_id = UNIPHIER_PRO5_ID,
76 .boot_device_sel_shift = 1,
77 .boot_device_table = uniphier_pro5_boot_device_table,
78 .boot_device_count = &uniphier_pro5_boot_device_count,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090079 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090080 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090081 },
82#endif
83#if defined(CONFIG_ARCH_UNIPHIER_PXS2)
84 {
85 .soc_id = UNIPHIER_PXS2_ID,
86 .boot_device_sel_shift = 1,
87 .boot_device_table = uniphier_pxs2_boot_device_table,
88 .boot_device_count = &uniphier_pxs2_boot_device_count,
89 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
90 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +090091 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090092 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090093 },
94#endif
95#if defined(CONFIG_ARCH_UNIPHIER_LD6B)
96 {
97 .soc_id = UNIPHIER_LD6B_ID,
98 .boot_device_sel_shift = 1,
99 .boot_device_table = uniphier_pxs2_boot_device_table,
100 .boot_device_count = &uniphier_pxs2_boot_device_count,
101 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
102 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900103 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900104 .have_internal_stm = true, /* STM on A-chip */
Masahiro Yamadafb092032017-02-14 01:24:26 +0900105 },
106#endif
107#if defined(CONFIG_ARCH_UNIPHIER_LD11)
108 {
109 .soc_id = UNIPHIER_LD11_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_ld11_boot_device_is_usb,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900114 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900115 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +0900116 },
117#endif
118#if defined(CONFIG_ARCH_UNIPHIER_LD20)
119 {
120 .soc_id = UNIPHIER_LD20_ID,
121 .boot_device_sel_shift = 1,
122 .boot_device_table = uniphier_ld11_boot_device_table,
123 .boot_device_count = &uniphier_ld11_boot_device_count,
124 .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900125 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900126 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +0900127 },
128#endif
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900129#if defined(CONFIG_ARCH_UNIPHIER_PXS3)
130 {
131 .soc_id = UNIPHIER_PXS3_ID,
132 .boot_device_sel_shift = 1,
133 .boot_device_table = uniphier_pxs3_boot_device_table,
134 .boot_device_count = &uniphier_pxs3_boot_device_count,
135 .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900136 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900137 .have_internal_stm = false,
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900138 },
139#endif
Masahiro Yamadafb092032017-02-14 01:24:26 +0900140};
141UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
142 uniphier_boot_device_info)
143
144static unsigned int __uniphier_boot_device_raw(
145 const struct uniphier_boot_device_info *info)
146{
147 u32 pinmon;
148 unsigned int boot_sel;
149
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900150 if (info->boot_is_swapped && info->boot_is_swapped())
Masahiro Yamadafb092032017-02-14 01:24:26 +0900151 return BOOT_DEVICE_NOR;
152
Masahiro Yamada76b31242019-07-10 20:07:40 +0900153 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamadafb092032017-02-14 01:24:26 +0900154
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900155 if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon))
156 return BOOT_DEVICE_MMC2;
157
Masahiro Yamadafb092032017-02-14 01:24:26 +0900158 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
159 return BOOT_DEVICE_USB;
160
161 boot_sel = pinmon >> info->boot_device_sel_shift;
162
163 BUG_ON(!is_power_of_2(*info->boot_device_count));
164 boot_sel &= *info->boot_device_count - 1;
165
166 return info->boot_device_table[boot_sel].boot_device;
167}
168
169unsigned int uniphier_boot_device_raw(void)
170{
171 const struct uniphier_boot_device_info *info;
172
173 info = uniphier_get_boot_device_info();
174 if (!info) {
175 pr_err("unsupported SoC\n");
176 return BOOT_DEVICE_NONE;
177 }
178
179 return __uniphier_boot_device_raw(info);
180}
181
182u32 spl_boot_device(void)
183{
184 const struct uniphier_boot_device_info *info;
185 u32 raw_mode;
186
187 info = uniphier_get_boot_device_info();
188 if (!info) {
189 pr_err("unsupported SoC\n");
190 return BOOT_DEVICE_NONE;
191 }
192
193 raw_mode = __uniphier_boot_device_raw(info);
194
195 return info->boot_device_fixup ?
196 info->boot_device_fixup(raw_mode) : raw_mode;
197}
198
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900199int uniphier_have_internal_stm(void)
200{
201 const struct uniphier_boot_device_info *info;
202
203 info = uniphier_get_boot_device_info();
204 if (!info) {
205 pr_err("unsupported SoC\n");
206 return -ENOTSUPP;
207 }
208
209 return info->have_internal_stm;
210}
211
212int uniphier_boot_from_backend(void)
213{
Masahiro Yamada76b31242019-07-10 20:07:40 +0900214 return !!(readl(sg_base + SG_PINMON0) & BIT(27));
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900215}
216
Masahiro Yamadafb092032017-02-14 01:24:26 +0900217#ifndef CONFIG_SPL_BUILD
218
Simon Glassed38aef2020-05-10 11:40:03 -0600219static int do_pinmon(struct cmd_tbl *cmdtp, int flag, int argc,
220 char *const argv[])
Masahiro Yamadafb092032017-02-14 01:24:26 +0900221{
222 const struct uniphier_boot_device_info *info;
223 u32 pinmon;
224 unsigned int boot_device_count, boot_sel;
225 int i;
226
227 info = uniphier_get_boot_device_info();
228 if (!info) {
229 pr_err("unsupported SoC\n");
230 return CMD_RET_FAILURE;
231 }
232
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900233 if (uniphier_have_internal_stm())
234 printf("STB Micon: %s\n",
235 uniphier_boot_from_backend() ? "OFF" : "ON");
236
Masahiro Yamada6f7b68c2019-07-10 20:07:38 +0900237 if (info->boot_is_swapped)
238 printf("Boot Swap: %s\n",
239 info->boot_is_swapped() ? "ON" : "OFF");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900240
Masahiro Yamada76b31242019-07-10 20:07:40 +0900241 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamadafb092032017-02-14 01:24:26 +0900242
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900243 if (info->boot_device_is_sd)
244 printf("SD Boot: %s\n",
245 info->boot_device_is_sd(pinmon) ? "ON" : "OFF");
246
Masahiro Yamadafb092032017-02-14 01:24:26 +0900247 if (info->boot_device_is_usb)
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900248 printf("USB Boot: %s\n",
Masahiro Yamadafb092032017-02-14 01:24:26 +0900249 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
250
251 boot_device_count = *info->boot_device_count;
252
253 boot_sel = pinmon >> info->boot_device_sel_shift;
254 boot_sel &= boot_device_count - 1;
255
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900256 printf("\nBoot Mode Sel:\n");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900257 for (i = 0; i < boot_device_count; i++)
258 printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
259 info->boot_device_table[i].desc);
260
261 return CMD_RET_SUCCESS;
262}
263
264U_BOOT_CMD(
265 pinmon, 1, 1, do_pinmon,
266 "pin monitor",
267 ""
268);
269
270#endif /* !CONFIG_SPL_BUILD */