micro-support-card.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <dm.h>
  8. #include <fdt_support.h>
  9. #include <linux/ctype.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <asm/global_data.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. struct udevice *dev;
  75. int ret;
  76. /* The system bus must be initialized for access to the support card. */
  77. ret = uclass_get_device_by_driver(UCLASS_SIMPLE_BUS,
  78. DM_GET_DRIVER(uniphier_system_bus_driver),
  79. &dev);
  80. if (ret)
  81. return;
  82. /* Check DT to see if this board has the support card. */
  83. support_card_detect();
  84. if (!support_card_found)
  85. return;
  86. support_card_reset();
  87. /*
  88. * After power on, we need to keep the LAN controller in reset state
  89. * for a while. (200 usec)
  90. */
  91. udelay(200);
  92. support_card_reset_deassert();
  93. support_card_show_revision();
  94. }
  95. static const u8 ledval_num[] = {
  96. 0x7e, /* 0 */
  97. 0x0c, /* 1 */
  98. 0xb6, /* 2 */
  99. 0x9e, /* 3 */
  100. 0xcc, /* 4 */
  101. 0xda, /* 5 */
  102. 0xfa, /* 6 */
  103. 0x4e, /* 7 */
  104. 0xfe, /* 8 */
  105. 0xde, /* 9 */
  106. };
  107. static const u8 ledval_alpha[] = {
  108. 0xee, /* A */
  109. 0xf8, /* B */
  110. 0x72, /* C */
  111. 0xbc, /* D */
  112. 0xf2, /* E */
  113. 0xe2, /* F */
  114. 0x7a, /* G */
  115. 0xe8, /* H */
  116. 0x08, /* I */
  117. 0x3c, /* J */
  118. 0xea, /* K */
  119. 0x70, /* L */
  120. 0x6e, /* M */
  121. 0xa8, /* N */
  122. 0xb8, /* O */
  123. 0xe6, /* P */
  124. 0xce, /* Q */
  125. 0xa0, /* R */
  126. 0xc8, /* S */
  127. 0x8c, /* T */
  128. 0x7c, /* U */
  129. 0x54, /* V */
  130. 0xfc, /* W */
  131. 0xec, /* X */
  132. 0xdc, /* Y */
  133. 0xa4, /* Z */
  134. };
  135. static u8 char2ledval(char c)
  136. {
  137. if (isdigit(c))
  138. return ledval_num[c - '0'];
  139. else if (isalpha(c))
  140. return ledval_alpha[toupper(c) - 'A'];
  141. return 0;
  142. }
  143. void led_puts(const char *s)
  144. {
  145. int i;
  146. u32 val = 0;
  147. if (!support_card_found)
  148. return;
  149. if (!s)
  150. return;
  151. for (i = 0; i < 4; i++) {
  152. val <<= 8;
  153. val |= char2ledval(*s);
  154. if (*s != '\0')
  155. s++;
  156. }
  157. writel(~val, support_card_base + LED_OFFSET);
  158. }