blob: 59302150b89c45905d3aa4bfdf3c18e161eb572b [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
7#include <common.h>
8#include <spl.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +09009#include <stdio.h>
Masahiro Yamada92727322019-07-10 20:07:34 +090010#include <linux/io.h>
Masahiro Yamadafb092032017-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;
24 int (*boot_device_is_usb)(u32 pinmon);
25 unsigned int (*boot_device_fixup)(unsigned int mode);
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090026 bool have_internal_stm;
Masahiro Yamadafb092032017-02-14 01:24:26 +090027};
28
29static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
Masahiro Yamadafb092032017-02-14 01:24:26 +090030#if defined(CONFIG_ARCH_UNIPHIER_LD4)
31 {
32 .soc_id = UNIPHIER_LD4_ID,
33 .boot_device_sel_shift = 1,
34 .boot_device_table = uniphier_ld4_boot_device_table,
35 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090036 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090037 },
38#endif
39#if defined(CONFIG_ARCH_UNIPHIER_PRO4)
40 {
41 .soc_id = UNIPHIER_PRO4_ID,
42 .boot_device_sel_shift = 1,
43 .boot_device_table = uniphier_ld4_boot_device_table,
44 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090045 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090046 },
47#endif
48#if defined(CONFIG_ARCH_UNIPHIER_SLD8)
49 {
50 .soc_id = UNIPHIER_SLD8_ID,
51 .boot_device_sel_shift = 1,
52 .boot_device_table = uniphier_ld4_boot_device_table,
53 .boot_device_count = &uniphier_ld4_boot_device_count,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090054 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090055 },
56#endif
57#if defined(CONFIG_ARCH_UNIPHIER_PRO5)
58 {
59 .soc_id = UNIPHIER_PRO5_ID,
60 .boot_device_sel_shift = 1,
61 .boot_device_table = uniphier_pro5_boot_device_table,
62 .boot_device_count = &uniphier_pro5_boot_device_count,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090063 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090064 },
65#endif
66#if defined(CONFIG_ARCH_UNIPHIER_PXS2)
67 {
68 .soc_id = UNIPHIER_PXS2_ID,
69 .boot_device_sel_shift = 1,
70 .boot_device_table = uniphier_pxs2_boot_device_table,
71 .boot_device_count = &uniphier_pxs2_boot_device_count,
72 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
73 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090074 .have_internal_stm = false,
Masahiro Yamadafb092032017-02-14 01:24:26 +090075 },
76#endif
77#if defined(CONFIG_ARCH_UNIPHIER_LD6B)
78 {
79 .soc_id = UNIPHIER_LD6B_ID,
80 .boot_device_sel_shift = 1,
81 .boot_device_table = uniphier_pxs2_boot_device_table,
82 .boot_device_count = &uniphier_pxs2_boot_device_count,
83 .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
84 .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090085 .have_internal_stm = true, /* STM on A-chip */
Masahiro Yamadafb092032017-02-14 01:24:26 +090086 },
87#endif
88#if defined(CONFIG_ARCH_UNIPHIER_LD11)
89 {
90 .soc_id = UNIPHIER_LD11_ID,
91 .boot_device_sel_shift = 1,
92 .boot_device_table = uniphier_ld11_boot_device_table,
93 .boot_device_count = &uniphier_ld11_boot_device_count,
94 .boot_device_is_usb = uniphier_ld11_boot_device_is_usb,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +090095 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +090096 },
97#endif
98#if defined(CONFIG_ARCH_UNIPHIER_LD20)
99 {
100 .soc_id = UNIPHIER_LD20_ID,
101 .boot_device_sel_shift = 1,
102 .boot_device_table = uniphier_ld11_boot_device_table,
103 .boot_device_count = &uniphier_ld11_boot_device_count,
104 .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900105 .have_internal_stm = true,
Masahiro Yamadafb092032017-02-14 01:24:26 +0900106 },
107#endif
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900108#if defined(CONFIG_ARCH_UNIPHIER_PXS3)
109 {
110 .soc_id = UNIPHIER_PXS3_ID,
111 .boot_device_sel_shift = 1,
112 .boot_device_table = uniphier_pxs3_boot_device_table,
113 .boot_device_count = &uniphier_pxs3_boot_device_count,
114 .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
Masahiro Yamadadb0b8122019-07-10 20:07:33 +0900115 .have_internal_stm = false,
Masahiro Yamada8e0a4502017-05-15 14:26:33 +0900116 },
117#endif
Masahiro Yamadafb092032017-02-14 01:24:26 +0900118};
119UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
120 uniphier_boot_device_info)
121
122static unsigned int __uniphier_boot_device_raw(
123 const struct uniphier_boot_device_info *info)
124{
125 u32 pinmon;
126 unsigned int boot_sel;
127
128 if (boot_is_swapped())
129 return BOOT_DEVICE_NOR;
130
131 pinmon = readl(SG_PINMON0);
132
133 if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
134 return BOOT_DEVICE_USB;
135
136 boot_sel = pinmon >> info->boot_device_sel_shift;
137
138 BUG_ON(!is_power_of_2(*info->boot_device_count));
139 boot_sel &= *info->boot_device_count - 1;
140
141 return info->boot_device_table[boot_sel].boot_device;
142}
143
144unsigned int uniphier_boot_device_raw(void)
145{
146 const struct uniphier_boot_device_info *info;
147
148 info = uniphier_get_boot_device_info();
149 if (!info) {
150 pr_err("unsupported SoC\n");
151 return BOOT_DEVICE_NONE;
152 }
153
154 return __uniphier_boot_device_raw(info);
155}
156
157u32 spl_boot_device(void)
158{
159 const struct uniphier_boot_device_info *info;
160 u32 raw_mode;
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 raw_mode = __uniphier_boot_device_raw(info);
169
170 return info->boot_device_fixup ?
171 info->boot_device_fixup(raw_mode) : raw_mode;
172}
173
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900174int uniphier_have_internal_stm(void)
175{
176 const struct uniphier_boot_device_info *info;
177
178 info = uniphier_get_boot_device_info();
179 if (!info) {
180 pr_err("unsupported SoC\n");
181 return -ENOTSUPP;
182 }
183
184 return info->have_internal_stm;
185}
186
187int uniphier_boot_from_backend(void)
188{
189 return !!(readl(SG_PINMON0) & BIT(27));
190}
191
Masahiro Yamadafb092032017-02-14 01:24:26 +0900192#ifndef CONFIG_SPL_BUILD
193
194static int do_pinmon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
195{
196 const struct uniphier_boot_device_info *info;
197 u32 pinmon;
198 unsigned int boot_device_count, boot_sel;
199 int i;
200
201 info = uniphier_get_boot_device_info();
202 if (!info) {
203 pr_err("unsupported SoC\n");
204 return CMD_RET_FAILURE;
205 }
206
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900207 if (uniphier_have_internal_stm())
208 printf("STB Micon: %s\n",
209 uniphier_boot_from_backend() ? "OFF" : "ON");
210
211 printf("Boot Swap: %s\n", boot_is_swapped() ? "ON" : "OFF");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900212
213 pinmon = readl(SG_PINMON0);
214
215 if (info->boot_device_is_usb)
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900216 printf("USB Boot: %s\n",
Masahiro Yamadafb092032017-02-14 01:24:26 +0900217 info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
218
219 boot_device_count = *info->boot_device_count;
220
221 boot_sel = pinmon >> info->boot_device_sel_shift;
222 boot_sel &= boot_device_count - 1;
223
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900224 printf("\nBoot Mode Sel:\n");
Masahiro Yamadafb092032017-02-14 01:24:26 +0900225 for (i = 0; i < boot_device_count; i++)
226 printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
227 info->boot_device_table[i].desc);
228
229 return CMD_RET_SUCCESS;
230}
231
232U_BOOT_CMD(
233 pinmon, 1, 1, do_pinmon,
234 "pin monitor",
235 ""
236);
237
238#endif /* !CONFIG_SPL_BUILD */