micro-support-card.c 5.4 KB

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