Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2017 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 8 | #include <spl.h> |
Masahiro Yamada | 609cd53 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 9 | #include <stdio.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 11 | #include <linux/bug.h> |
Masahiro Yamada | e631441 | 2020-05-20 12:31:27 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | 9272732 | 2019-07-10 20:07:34 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 14 | #include <linux/log2.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 15 | #include <linux/printk.h> |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 16 | |
| 17 | #include "../init.h" |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 18 | #include "../sg-regs.h" |
| 19 | #include "../soc-info.h" |
| 20 | #include "boot-device.h" |
| 21 | |
Masahiro Yamada | 98b5f34 | 2020-07-09 15:08:20 +0900 | [diff] [blame] | 22 | #define SBBASE0 0x58c00100 |
| 23 | #define SBBASE_BANK_ENABLE BIT(0) |
| 24 | |
| 25 | static int uniphier_sbc_boot_is_swapped(void) |
| 26 | { |
| 27 | return !(readl(SBBASE0) & SBBASE_BANK_ENABLE); |
| 28 | } |
| 29 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 30 | struct 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 Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 35 | int (*boot_device_is_sd)(u32 pinmon); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 36 | int (*boot_device_is_usb)(u32 pinmon); |
| 37 | unsigned int (*boot_device_fixup)(unsigned int mode); |
Masahiro Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 38 | int (*boot_is_swapped)(void); |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 39 | bool have_internal_stm; |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static const struct uniphier_boot_device_info uniphier_boot_device_info[] = { |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 43 | #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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 49 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 50 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 51 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 59 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 60 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 61 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 69 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 70 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 71 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 79 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 80 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 81 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 91 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 92 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 93 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 103 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 104 | .have_internal_stm = true, /* STM on A-chip */ |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 105 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 114 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 115 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 116 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 125 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 126 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 127 | }, |
| 128 | #endif |
Masahiro Yamada | 8e0a450 | 2017-05-15 14:26:33 +0900 | [diff] [blame] | 129 | #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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 136 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 137 | .have_internal_stm = false, |
Masahiro Yamada | 8e0a450 | 2017-05-15 14:26:33 +0900 | [diff] [blame] | 138 | }, |
| 139 | #endif |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 140 | }; |
| 141 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info, |
| 142 | uniphier_boot_device_info) |
| 143 | |
| 144 | static 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 150 | if (info->boot_is_swapped && info->boot_is_swapped()) |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 151 | return BOOT_DEVICE_NOR; |
| 152 | |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 153 | pinmon = readl(sg_base + SG_PINMON0); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 154 | |
Masahiro Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 155 | if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon)) |
| 156 | return BOOT_DEVICE_MMC2; |
| 157 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 158 | 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 | |
| 169 | unsigned 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 | |
| 182 | u32 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 199 | int 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 | |
| 212 | int uniphier_boot_from_backend(void) |
| 213 | { |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 214 | return !!(readl(sg_base + SG_PINMON0) & BIT(27)); |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 217 | #ifndef CONFIG_SPL_BUILD |
| 218 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 219 | static int do_pinmon(struct cmd_tbl *cmdtp, int flag, int argc, |
| 220 | char *const argv[]) |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 221 | { |
| 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 233 | if (uniphier_have_internal_stm()) |
| 234 | printf("STB Micon: %s\n", |
| 235 | uniphier_boot_from_backend() ? "OFF" : "ON"); |
| 236 | |
Masahiro Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 237 | if (info->boot_is_swapped) |
| 238 | printf("Boot Swap: %s\n", |
| 239 | info->boot_is_swapped() ? "ON" : "OFF"); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 240 | |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 241 | pinmon = readl(sg_base + SG_PINMON0); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 242 | |
Masahiro Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 243 | if (info->boot_device_is_sd) |
| 244 | printf("SD Boot: %s\n", |
| 245 | info->boot_device_is_sd(pinmon) ? "ON" : "OFF"); |
| 246 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 247 | if (info->boot_device_is_usb) |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 248 | printf("USB Boot: %s\n", |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 249 | 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 256 | printf("\nBoot Mode Sel:\n"); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 257 | 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 | |
| 264 | U_BOOT_CMD( |
| 265 | pinmon, 1, 1, do_pinmon, |
| 266 | "pin monitor", |
| 267 | "" |
| 268 | ); |
| 269 | |
| 270 | #endif /* !CONFIG_SPL_BUILD */ |