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> |
| 15 | |
| 16 | #include "../init.h" |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 17 | #include "../sg-regs.h" |
| 18 | #include "../soc-info.h" |
| 19 | #include "boot-device.h" |
| 20 | |
Masahiro Yamada | 98b5f34 | 2020-07-09 15:08:20 +0900 | [diff] [blame] | 21 | #define SBBASE0 0x58c00100 |
| 22 | #define SBBASE_BANK_ENABLE BIT(0) |
| 23 | |
| 24 | static int uniphier_sbc_boot_is_swapped(void) |
| 25 | { |
| 26 | return !(readl(SBBASE0) & SBBASE_BANK_ENABLE); |
| 27 | } |
| 28 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 29 | struct 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 Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 34 | int (*boot_device_is_sd)(u32 pinmon); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 35 | int (*boot_device_is_usb)(u32 pinmon); |
| 36 | unsigned int (*boot_device_fixup)(unsigned int mode); |
Masahiro Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 37 | int (*boot_is_swapped)(void); |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 38 | bool have_internal_stm; |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static const struct uniphier_boot_device_info uniphier_boot_device_info[] = { |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 42 | #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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 48 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 49 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 50 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 58 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 59 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 60 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 68 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 69 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 70 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 78 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 79 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 80 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 90 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 91 | .have_internal_stm = false, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 92 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 102 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 103 | .have_internal_stm = true, /* STM on A-chip */ |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 104 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 113 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 114 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 115 | }, |
| 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 124 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 125 | .have_internal_stm = true, |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 126 | }, |
| 127 | #endif |
Masahiro Yamada | 8e0a450 | 2017-05-15 14:26:33 +0900 | [diff] [blame] | 128 | #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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 135 | .boot_is_swapped = uniphier_sbc_boot_is_swapped, |
Masahiro Yamada | db0b812 | 2019-07-10 20:07:33 +0900 | [diff] [blame] | 136 | .have_internal_stm = false, |
Masahiro Yamada | 8e0a450 | 2017-05-15 14:26:33 +0900 | [diff] [blame] | 137 | }, |
| 138 | #endif |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 139 | }; |
| 140 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info, |
| 141 | uniphier_boot_device_info) |
| 142 | |
| 143 | static 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 Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 149 | if (info->boot_is_swapped && info->boot_is_swapped()) |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 150 | return BOOT_DEVICE_NOR; |
| 151 | |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 152 | pinmon = readl(sg_base + SG_PINMON0); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 153 | |
Masahiro Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 154 | if (info->boot_device_is_sd && info->boot_device_is_sd(pinmon)) |
| 155 | return BOOT_DEVICE_MMC2; |
| 156 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 157 | 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 | |
| 168 | unsigned 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 | |
| 181 | u32 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 198 | int 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 | |
| 211 | int uniphier_boot_from_backend(void) |
| 212 | { |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 213 | return !!(readl(sg_base + SG_PINMON0) & BIT(27)); |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 216 | #ifndef CONFIG_SPL_BUILD |
| 217 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 218 | static int do_pinmon(struct cmd_tbl *cmdtp, int flag, int argc, |
| 219 | char *const argv[]) |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 220 | { |
| 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 232 | if (uniphier_have_internal_stm()) |
| 233 | printf("STB Micon: %s\n", |
| 234 | uniphier_boot_from_backend() ? "OFF" : "ON"); |
| 235 | |
Masahiro Yamada | 6f7b68c | 2019-07-10 20:07:38 +0900 | [diff] [blame] | 236 | if (info->boot_is_swapped) |
| 237 | printf("Boot Swap: %s\n", |
| 238 | info->boot_is_swapped() ? "ON" : "OFF"); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 239 | |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 240 | pinmon = readl(sg_base + SG_PINMON0); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 241 | |
Masahiro Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 242 | if (info->boot_device_is_sd) |
| 243 | printf("SD Boot: %s\n", |
| 244 | info->boot_device_is_sd(pinmon) ? "ON" : "OFF"); |
| 245 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 246 | if (info->boot_device_is_usb) |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 247 | printf("USB Boot: %s\n", |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 248 | 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 Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 255 | printf("\nBoot Mode Sel:\n"); |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 256 | 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 | |
| 263 | U_BOOT_CMD( |
| 264 | pinmon, 1, 1, do_pinmon, |
| 265 | "pin monitor", |
| 266 | "" |
| 267 | ); |
| 268 | |
| 269 | #endif /* !CONFIG_SPL_BUILD */ |