boot-device.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2015-2017 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <spl.h>
  9. #include <linux/log2.h>
  10. #include "../init.h"
  11. #include "../sbc/sbc-regs.h"
  12. #include "../sg-regs.h"
  13. #include "../soc-info.h"
  14. #include "boot-device.h"
  15. struct uniphier_boot_device_info {
  16. unsigned int soc_id;
  17. unsigned int boot_device_sel_shift;
  18. const struct uniphier_boot_device *boot_device_table;
  19. const unsigned int *boot_device_count;
  20. int (*boot_device_is_usb)(u32 pinmon);
  21. unsigned int (*boot_device_fixup)(unsigned int mode);
  22. int have_internal_stm;
  23. };
  24. static const struct uniphier_boot_device_info uniphier_boot_device_info[] = {
  25. #if defined(CONFIG_ARCH_UNIPHIER_SLD3)
  26. {
  27. .soc_id = UNIPHIER_SLD3_ID,
  28. .boot_device_sel_shift = 0,
  29. .boot_device_table = uniphier_sld3_boot_device_table,
  30. .boot_device_count = &uniphier_sld3_boot_device_count,
  31. .have_internal_stm = 0,
  32. },
  33. #endif
  34. #if defined(CONFIG_ARCH_UNIPHIER_LD4)
  35. {
  36. .soc_id = UNIPHIER_LD4_ID,
  37. .boot_device_sel_shift = 1,
  38. .boot_device_table = uniphier_ld4_boot_device_table,
  39. .boot_device_count = &uniphier_ld4_boot_device_count,
  40. .have_internal_stm = 1,
  41. },
  42. #endif
  43. #if defined(CONFIG_ARCH_UNIPHIER_PRO4)
  44. {
  45. .soc_id = UNIPHIER_PRO4_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,
  49. .have_internal_stm = 0,
  50. },
  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,
  58. .have_internal_stm = 1,
  59. },
  60. #endif
  61. #if defined(CONFIG_ARCH_UNIPHIER_PRO5)
  62. {
  63. .soc_id = UNIPHIER_PRO5_ID,
  64. .boot_device_sel_shift = 1,
  65. .boot_device_table = uniphier_pro5_boot_device_table,
  66. .boot_device_count = &uniphier_pro5_boot_device_count,
  67. .have_internal_stm = 0,
  68. },
  69. #endif
  70. #if defined(CONFIG_ARCH_UNIPHIER_PXS2)
  71. {
  72. .soc_id = UNIPHIER_PXS2_ID,
  73. .boot_device_sel_shift = 1,
  74. .boot_device_table = uniphier_pxs2_boot_device_table,
  75. .boot_device_count = &uniphier_pxs2_boot_device_count,
  76. .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
  77. .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
  78. .have_internal_stm = 0,
  79. },
  80. #endif
  81. #if defined(CONFIG_ARCH_UNIPHIER_LD6B)
  82. {
  83. .soc_id = UNIPHIER_LD6B_ID,
  84. .boot_device_sel_shift = 1,
  85. .boot_device_table = uniphier_pxs2_boot_device_table,
  86. .boot_device_count = &uniphier_pxs2_boot_device_count,
  87. .boot_device_is_usb = uniphier_pxs2_boot_device_is_usb,
  88. .boot_device_fixup = uniphier_pxs2_boot_device_fixup,
  89. .have_internal_stm = 1, /* STM on A-chip */
  90. },
  91. #endif
  92. #if defined(CONFIG_ARCH_UNIPHIER_LD11)
  93. {
  94. .soc_id = UNIPHIER_LD11_ID,
  95. .boot_device_sel_shift = 1,
  96. .boot_device_table = uniphier_ld11_boot_device_table,
  97. .boot_device_count = &uniphier_ld11_boot_device_count,
  98. .boot_device_is_usb = uniphier_ld11_boot_device_is_usb,
  99. .boot_device_fixup = uniphier_ld11_boot_device_fixup,
  100. .have_internal_stm = 1,
  101. },
  102. #endif
  103. #if defined(CONFIG_ARCH_UNIPHIER_LD20)
  104. {
  105. .soc_id = UNIPHIER_LD20_ID,
  106. .boot_device_sel_shift = 1,
  107. .boot_device_table = uniphier_ld11_boot_device_table,
  108. .boot_device_count = &uniphier_ld11_boot_device_count,
  109. .boot_device_is_usb = uniphier_ld20_boot_device_is_usb,
  110. .boot_device_fixup = uniphier_ld11_boot_device_fixup,
  111. .have_internal_stm = 1,
  112. },
  113. #endif
  114. #if defined(CONFIG_ARCH_UNIPHIER_PXS3)
  115. {
  116. .soc_id = UNIPHIER_PXS3_ID,
  117. .boot_device_sel_shift = 1,
  118. .boot_device_table = uniphier_pxs3_boot_device_table,
  119. .boot_device_count = &uniphier_pxs3_boot_device_count,
  120. .boot_device_is_usb = uniphier_pxs3_boot_device_is_usb,
  121. .have_internal_stm = 0,
  122. },
  123. #endif
  124. };
  125. UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_boot_device_info,
  126. uniphier_boot_device_info)
  127. static unsigned int __uniphier_boot_device_raw(
  128. const struct uniphier_boot_device_info *info)
  129. {
  130. u32 pinmon;
  131. unsigned int boot_sel;
  132. if (boot_is_swapped())
  133. return BOOT_DEVICE_NOR;
  134. pinmon = readl(SG_PINMON0);
  135. if (info->boot_device_is_usb && info->boot_device_is_usb(pinmon))
  136. return BOOT_DEVICE_USB;
  137. boot_sel = pinmon >> info->boot_device_sel_shift;
  138. BUG_ON(!is_power_of_2(*info->boot_device_count));
  139. boot_sel &= *info->boot_device_count - 1;
  140. return info->boot_device_table[boot_sel].boot_device;
  141. }
  142. unsigned int uniphier_boot_device_raw(void)
  143. {
  144. const struct uniphier_boot_device_info *info;
  145. info = uniphier_get_boot_device_info();
  146. if (!info) {
  147. pr_err("unsupported SoC\n");
  148. return BOOT_DEVICE_NONE;
  149. }
  150. return __uniphier_boot_device_raw(info);
  151. }
  152. u32 spl_boot_device(void)
  153. {
  154. const struct uniphier_boot_device_info *info;
  155. u32 raw_mode;
  156. info = uniphier_get_boot_device_info();
  157. if (!info) {
  158. pr_err("unsupported SoC\n");
  159. return BOOT_DEVICE_NONE;
  160. }
  161. raw_mode = __uniphier_boot_device_raw(info);
  162. return info->boot_device_fixup ?
  163. info->boot_device_fixup(raw_mode) : raw_mode;
  164. }
  165. int uniphier_have_internal_stm(void)
  166. {
  167. const struct uniphier_boot_device_info *info;
  168. info = uniphier_get_boot_device_info();
  169. if (!info) {
  170. pr_err("unsupported SoC\n");
  171. return -ENOTSUPP;
  172. }
  173. return info->have_internal_stm;
  174. }
  175. int uniphier_boot_from_backend(void)
  176. {
  177. return !!(readl(SG_PINMON0) & BIT(27));
  178. }
  179. #ifndef CONFIG_SPL_BUILD
  180. static int do_pinmon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  181. {
  182. const struct uniphier_boot_device_info *info;
  183. u32 pinmon;
  184. unsigned int boot_device_count, boot_sel;
  185. int i;
  186. info = uniphier_get_boot_device_info();
  187. if (!info) {
  188. pr_err("unsupported SoC\n");
  189. return CMD_RET_FAILURE;
  190. }
  191. if (uniphier_have_internal_stm())
  192. printf("STB Micon: %s\n",
  193. uniphier_boot_from_backend() ? "OFF" : "ON");
  194. printf("Boot Swap: %s\n", boot_is_swapped() ? "ON" : "OFF");
  195. pinmon = readl(SG_PINMON0);
  196. if (info->boot_device_is_usb)
  197. printf("USB Boot: %s\n",
  198. info->boot_device_is_usb(pinmon) ? "ON" : "OFF");
  199. boot_device_count = *info->boot_device_count;
  200. boot_sel = pinmon >> info->boot_device_sel_shift;
  201. boot_sel &= boot_device_count - 1;
  202. printf("\nBoot Mode Sel:\n");
  203. for (i = 0; i < boot_device_count; i++)
  204. printf(" %c %02x %s\n", i == boot_sel ? '*' : ' ', i,
  205. info->boot_device_table[i].desc);
  206. return CMD_RET_SUCCESS;
  207. }
  208. U_BOOT_CMD(
  209. pinmon, 1, 1, do_pinmon,
  210. "pin monitor",
  211. ""
  212. );
  213. #endif /* !CONFIG_SPL_BUILD */