micro-support-card.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012-2015 Panasonic Corporation
  4. * Copyright (C) 2015-2020 Socionext Inc.
  5. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  6. */
  7. #include <common.h>
  8. #include <fdt_support.h>
  9. #include <net.h>
  10. #include <dm/of.h>
  11. #include <linux/ctype.h>
  12. #include <linux/io.h>
  13. #include "micro-support-card.h"
  14. #define SMC911X_OFFSET 0x00000
  15. #define LED_OFFSET 0x90000
  16. #define NS16550A_OFFSET 0xb0000
  17. #define MICRO_SUPPORT_CARD_RESET 0xd0034
  18. #define MICRO_SUPPORT_CARD_REVISION 0xd00e0
  19. static bool support_card_found;
  20. static void __iomem *support_card_base;
  21. static void support_card_detect(void)
  22. {
  23. DECLARE_GLOBAL_DATA_PTR;
  24. const void *fdt = gd->fdt_blob;
  25. int offset;
  26. u64 addr, addr2;
  27. offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
  28. if (offset < 0)
  29. return;
  30. addr = fdt_get_base_address(fdt, offset);
  31. if (addr == OF_BAD_ADDR)
  32. return;
  33. addr -= SMC911X_OFFSET;
  34. offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
  35. if (offset < 0)
  36. return;
  37. addr2 = fdt_get_base_address(fdt, offset);
  38. if (addr2 == OF_BAD_ADDR)
  39. return;
  40. addr2 -= NS16550A_OFFSET;
  41. /* sanity check */
  42. if (addr != addr2)
  43. return;
  44. support_card_base = ioremap(addr, 0x100000);
  45. support_card_found = true;
  46. }
  47. /*
  48. * 0: reset deassert, 1: reset
  49. *
  50. * bit[0]: LAN, I2C, LED
  51. * bit[1]: UART
  52. */
  53. static void support_card_reset_deassert(void)
  54. {
  55. writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
  56. }
  57. static void support_card_reset(void)
  58. {
  59. writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
  60. }
  61. static int support_card_show_revision(void)
  62. {
  63. u32 revision;
  64. revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
  65. revision &= 0xff;
  66. /* revision 3.6.x card changed the revision format */
  67. printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
  68. revision >> 4 == 6 ? "3." : "",
  69. revision >> 4, revision & 0xf);
  70. return 0;
  71. }
  72. void support_card_init(void)
  73. {
  74. support_card_detect();
  75. if (!support_card_found)
  76. return;
  77. support_card_reset();
  78. /*
  79. * After power on, we need to keep the LAN controller in reset state
  80. * for a while. (200 usec)
  81. */
  82. udelay(200);
  83. support_card_reset_deassert();
  84. support_card_show_revision();
  85. }
  86. #if defined(CONFIG_SMC911X)
  87. #include <netdev.h>
  88. int board_eth_init(bd_t *bis)
  89. {
  90. if (!support_card_found)
  91. return 0;
  92. return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
  93. }
  94. #endif
  95. #if defined(CONFIG_MTD_NOR_FLASH)
  96. #include <mtd/cfi_flash.h>
  97. struct memory_bank {
  98. phys_addr_t base;
  99. unsigned long size;
  100. };
  101. static int mem_is_flash(const struct memory_bank *mem)
  102. {
  103. const int loop = 128;
  104. u32 *scratch_addr;
  105. u32 saved_value;
  106. int ret = 1;
  107. int i;
  108. /* just in case, use the tail of the memory bank */
  109. scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
  110. sizeof(u32) * loop, MAP_NOCACHE);
  111. for (i = 0; i < loop; i++, scratch_addr++) {
  112. saved_value = readl(scratch_addr);
  113. writel(~saved_value, scratch_addr);
  114. if (readl(scratch_addr) != saved_value) {
  115. /* We assume no memory or SRAM here. */
  116. writel(saved_value, scratch_addr);
  117. ret = 0;
  118. break;
  119. }
  120. }
  121. unmap_physmem(scratch_addr, MAP_NOCACHE);
  122. return ret;
  123. }
  124. /* {address, size} */
  125. static const struct memory_bank memory_banks[] = {
  126. {0x42000000, 0x01f00000},
  127. };
  128. static const struct memory_bank
  129. *flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
  130. phys_addr_t cfi_flash_bank_addr(int i)
  131. {
  132. return flash_banks_list[i]->base;
  133. }
  134. unsigned long cfi_flash_bank_size(int i)
  135. {
  136. return flash_banks_list[i]->size;
  137. }
  138. static void detect_num_flash_banks(void)
  139. {
  140. const struct memory_bank *memory_bank, *end;
  141. cfi_flash_num_flash_banks = 0;
  142. memory_bank = memory_banks;
  143. end = memory_bank + ARRAY_SIZE(memory_banks);
  144. for (; memory_bank < end; memory_bank++) {
  145. if (cfi_flash_num_flash_banks >=
  146. CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
  147. break;
  148. if (mem_is_flash(memory_bank)) {
  149. flash_banks_list[cfi_flash_num_flash_banks] =
  150. memory_bank;
  151. debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
  152. (unsigned long)memory_bank->base,
  153. (unsigned long)memory_bank->size);
  154. cfi_flash_num_flash_banks++;
  155. }
  156. }
  157. debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
  158. }
  159. #else /* CONFIG_MTD_NOR_FLASH */
  160. static void detect_num_flash_banks(void)
  161. {
  162. };
  163. #endif /* CONFIG_MTD_NOR_FLASH */
  164. void support_card_late_init(void)
  165. {
  166. if (!support_card_found)
  167. return;
  168. detect_num_flash_banks();
  169. }
  170. static const u8 ledval_num[] = {
  171. 0x7e, /* 0 */
  172. 0x0c, /* 1 */
  173. 0xb6, /* 2 */
  174. 0x9e, /* 3 */
  175. 0xcc, /* 4 */
  176. 0xda, /* 5 */
  177. 0xfa, /* 6 */
  178. 0x4e, /* 7 */
  179. 0xfe, /* 8 */
  180. 0xde, /* 9 */
  181. };
  182. static const u8 ledval_alpha[] = {
  183. 0xee, /* A */
  184. 0xf8, /* B */
  185. 0x72, /* C */
  186. 0xbc, /* D */
  187. 0xf2, /* E */
  188. 0xe2, /* F */
  189. 0x7a, /* G */
  190. 0xe8, /* H */
  191. 0x08, /* I */
  192. 0x3c, /* J */
  193. 0xea, /* K */
  194. 0x70, /* L */
  195. 0x6e, /* M */
  196. 0xa8, /* N */
  197. 0xb8, /* O */
  198. 0xe6, /* P */
  199. 0xce, /* Q */
  200. 0xa0, /* R */
  201. 0xc8, /* S */
  202. 0x8c, /* T */
  203. 0x7c, /* U */
  204. 0x54, /* V */
  205. 0xfc, /* W */
  206. 0xec, /* X */
  207. 0xdc, /* Y */
  208. 0xa4, /* Z */
  209. };
  210. static u8 char2ledval(char c)
  211. {
  212. if (isdigit(c))
  213. return ledval_num[c - '0'];
  214. else if (isalpha(c))
  215. return ledval_alpha[toupper(c) - 'A'];
  216. return 0;
  217. }
  218. void led_puts(const char *s)
  219. {
  220. int i;
  221. u32 val = 0;
  222. if (!support_card_found)
  223. return;
  224. if (!s)
  225. return;
  226. for (i = 0; i < 4; i++) {
  227. val <<= 8;
  228. val |= char2ledval(*s);
  229. if (*s != '\0')
  230. s++;
  231. }
  232. writel(~val, support_card_base + LED_OFFSET);
  233. }