blob: 83f8c6a428cf9d0d62b051f2546ee17c480bdf7c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada784548e2017-02-14 01:24:26 +09002/*
3 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada784548e2017-02-14 01:24:26 +09005 */
6
7#include <common.h>
8#include <spl.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +09009#include <stdio.h>
Masahiro Yamadab521ea32019-07-10 20:07:34 +090010#include <linux/io.h>
Masahiro Yamada784548e2017-02-14 01:24:26 +090011#include <linux/log2.h>
12
13#include "../init.h"
14#include "../sbc/sbc-regs.h"
15#include "../sg-regs.h"
16#include "../soc-info.h"
17#include "boot-device.h"
18
19struct uniphier_boot_device_info {
20 unsigned int soc_id;
21 unsigned int boot_device_sel_shift;
22 const struct uniphier_boot_device *boot_device_table;
23 const unsigned int *boot_device_count;
Masahiro Yamadac3d8f1e2019-07-10 20:07:39 +090024 int (*boot_device_is_sd)(u32 pinmon);
Masahiro Yamada784548e2017-02-14 01:24:26 +090025 int (*boot_device_is_usb)(u32 pinmon);
26 unsigned int (*boot_device_fixup)(unsigned int mode);
Masahiro Yamada539d6e02019-07-10 20:07:38 +090027 int (*boot_is_swapped)(void);
Masahiro Yamadaef866f62019-07-10 20:07:33 +090028 bool have_internal_stm;
Masahiro Yamada784548e2017-02-14 01:24:26 +090029};
30
31static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
Masahiro Yamada784548e2017-02-14 01:24:26 +090032#if defined(CONFIG_ARCH_UNIPHIER_LD4)
33 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090038 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090039 .have_internal_stm = true,
Masahiro Yamada784548e2017-02-14 01:24:26 +090040 },
41#endif
42#if defined(CONFIG_ARCH_UNIPHIER_PRO4)
43 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090048 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090049 .have_internal_stm = false,
Masahiro Yamada784548e2017-02-14 01:24:26 +090050 },
51#endif
52#if defined(CONFIG_ARCH_UNIPHIER_SLD8)
53 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090058 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090059 .have_internal_stm = true,
Masahiro Yamada784548e2017-02-14 01:24:26 +090060 },
61#endif
62#if defined(CONFIG_ARCH_UNIPHIER_PRO5)
63 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090068 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090069 .have_internal_stm = false,
Masahiro Yamada784548e2017-02-14 01:24:26 +090070 },
71#endif
72#if defined(CONFIG_ARCH_UNIPHIER_PXS2)
73 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090080 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090081 .have_internal_stm = false,
Masahiro Yamada784548e2017-02-14 01:24:26 +090082 },
83#endif
84#if defined(CONFIG_ARCH_UNIPHIER_LD6B)
85 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +090092 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +090093 .have_internal_stm = true, /* STM on A-chip */
Masahiro Yamada784548e2017-02-14 01:24:26 +090094 },
95#endif
96#if defined(CONFIG_ARCH_UNIPHIER_LD11)
97 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +0900103 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +0900104 .have_internal_stm = true,
Masahiro Yamada784548e2017-02-14 01:24:26 +0900105 },
106#endif
107#if defined(CONFIG_ARCH_UNIPHIER_LD20)
108 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +0900114 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +0900115 .have_internal_stm = true,
Masahiro Yamada784548e2017-02-14 01:24:26 +0900116 },
117#endif
Masahiro Yamada81afa9c2017-05-15 14:26:33 +0900118#if defined(CONFIG_ARCH_UNIPHIER_PXS3)
119 {
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,
Masahiro Yamada539d6e02019-07-10 20:07:38 +0900125 .boot_is_swapped = uniphier_sbc_boot_is_swapped,
Masahiro Yamadaef866f62019-07-10 20:07:33 +0900126 .have_internal_stm = false,
Masahiro Yamada81afa9c2017-05-15 14:26:33 +0900127 },
128#endif
Masahiro Yamada784548e2017-02-14 01:24:26 +0900129};
130UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
131 uniphier_boot_device_info)
132
133static unsigned int __uniphier_boot_device_raw(
134 const struct uniphier_boot_device_info *info)
135{
136 u32 pinmon;
137 unsigned int boot_sel;
138
Masahiro Yamada539d6e02019-07-10 20:07:38 +0900139 if (info->boot_is_swapped && info->boot_is_swapped())
Masahiro Yamada784548e2017-02-14 01:24:26 +0900140 return BOOT_DEVICE_NOR;
141
Masahiro Yamadad41b3582019-07-10 20:07:40 +0900142 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamada784548e2017-02-14 01:24:26 +0900143
Masahiro Yamadac3d8f1e2019-07-10 20:07:39 +0900144 if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon))
145 return BOOT_DEVICE_MMC2;
146
Masahiro Yamada784548e2017-02-14 01:24:26 +0900147 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
148 return BOOT_DEVICE_USB;
149
150 boot_sel = pinmon >> info->boot_device_sel_shift;
151
152 BUG_ON(!is_power_of_2(*info->boot_device_count));
153 boot_sel &= *info->boot_device_count - 1;
154
155 return info->boot_device_table[boot_sel].boot_device;
156}
157
158unsigned int uniphier_boot_device_raw(void)
159{
160 const struct uniphier_boot_device_info *info;
161
162 info = uniphier_get_boot_device_info();
163 if (!info) {
164 pr_err("unsupported SoC\n");
165 return BOOT_DEVICE_NONE;
166 }
167
168 return __uniphier_boot_device_raw(info);
169}
170
171u32 spl_boot_device(void)
172{
173 const struct uniphier_boot_device_info *info;
174 u32 raw_mode;
175
176 info = uniphier_get_boot_device_info();
177 if (!info) {
178 pr_err("unsupported SoC\n");
179 return BOOT_DEVICE_NONE;
180 }
181
182 raw_mode = __uniphier_boot_device_raw(info);
183
184 return info->boot_device_fixup ?
185 info->boot_device_fixup(raw_mode) : raw_mode;
186}
187
Masahiro Yamada63754842017-04-20 16:54:43 +0900188int uniphier_have_internal_stm(void)
189{
190 const struct uniphier_boot_device_info *info;
191
192 info = uniphier_get_boot_device_info();
193 if (!info) {
194 pr_err("unsupported SoC\n");
195 return -ENOTSUPP;
196 }
197
198 return info->have_internal_stm;
199}
200
201int uniphier_boot_from_backend(void)
202{
Masahiro Yamadad41b3582019-07-10 20:07:40 +0900203 return !!(readl(sg_base + SG_PINMON0) & BIT(27));
Masahiro Yamada63754842017-04-20 16:54:43 +0900204}
205
Masahiro Yamada784548e2017-02-14 01:24:26 +0900206#ifndef CONFIG_SPL_BUILD
207
208static int do_pinmon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
209{
210 const struct uniphier_boot_device_info *info;
211 u32 pinmon;
212 unsigned int boot_device_count, boot_sel;
213 int i;
214
215 info = uniphier_get_boot_device_info();
216 if (!info) {
217 pr_err("unsupported SoC\n");
218 return CMD_RET_FAILURE;
219 }
220
Masahiro Yamada63754842017-04-20 16:54:43 +0900221 if (uniphier_have_internal_stm())
222 printf("STB Micon: %s\n",
223 uniphier_boot_from_backend() ? "OFF" : "ON");
224
Masahiro Yamada539d6e02019-07-10 20:07:38 +0900225 if (info->boot_is_swapped)
226 printf("Boot Swap: %s\n",
227 info->boot_is_swapped() ? "ON" : "OFF");
Masahiro Yamada784548e2017-02-14 01:24:26 +0900228
Masahiro Yamadad41b3582019-07-10 20:07:40 +0900229 pinmon = readl(sg_base + SG_PINMON0);
Masahiro Yamada784548e2017-02-14 01:24:26 +0900230
Masahiro Yamadac3d8f1e2019-07-10 20:07:39 +0900231 if (info->boot_device_is_sd)
232 printf("SD Boot: %s\n",
233 info->boot_device_is_sd(pinmon) ? "ON" : "OFF");
234
Masahiro Yamada784548e2017-02-14 01:24:26 +0900235 if (info->boot_device_is_usb)
Masahiro Yamada63754842017-04-20 16:54:43 +0900236 printf("USB Boot: %s\n",
Masahiro Yamada784548e2017-02-14 01:24:26 +0900237 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
238
239 boot_device_count = *info->boot_device_count;
240
241 boot_sel = pinmon >> info->boot_device_sel_shift;
242 boot_sel &= boot_device_count - 1;
243
Masahiro Yamada63754842017-04-20 16:54:43 +0900244 printf("\nBoot Mode Sel:\n");
Masahiro Yamada784548e2017-02-14 01:24:26 +0900245 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);
248
249 return CMD_RET_SUCCESS;
250}
251
252U_BOOT_CMD(
253 pinmon, 1, 1, do_pinmon,
254 "pin monitor",
255 ""
256);
257
258#endif /* !CONFIG_SPL_BUILD */