board-sead3.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Imagination Technologies
  4. * Author: Paul Burton <[email protected]>
  5. */
  6. #define pr_fmt(fmt) "sead3: " fmt
  7. #include <linux/errno.h>
  8. #include <linux/libfdt.h>
  9. #include <linux/printk.h>
  10. #include <linux/sizes.h>
  11. #include <asm/fw/fw.h>
  12. #include <asm/io.h>
  13. #include <asm/machine.h>
  14. #include <asm/yamon-dt.h>
  15. #define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
  16. #define SEAD_CONFIG_GIC_PRESENT BIT(1)
  17. #define MIPS_REVISION CKSEG1ADDR(0x1fc00010)
  18. #define MIPS_REVISION_MACHINE (0xf << 4)
  19. #define MIPS_REVISION_MACHINE_SEAD3 (0x4 << 4)
  20. /*
  21. * Maximum 384MB RAM at physical address 0, preceding any I/O.
  22. */
  23. static struct yamon_mem_region mem_regions[] __initdata = {
  24. /* start size */
  25. { 0, SZ_256M + SZ_128M },
  26. {}
  27. };
  28. static __init bool sead3_detect(void)
  29. {
  30. uint32_t rev;
  31. rev = __raw_readl((void *)MIPS_REVISION);
  32. return (rev & MIPS_REVISION_MACHINE) == MIPS_REVISION_MACHINE_SEAD3;
  33. }
  34. static __init int append_memory(void *fdt)
  35. {
  36. return yamon_dt_append_memory(fdt, mem_regions);
  37. }
  38. static __init int remove_gic(void *fdt)
  39. {
  40. const unsigned int cpu_ehci_int = 2;
  41. const unsigned int cpu_uart_int = 4;
  42. const unsigned int cpu_eth_int = 6;
  43. int gic_off, cpu_off, uart_off, eth_off, ehci_off, err;
  44. uint32_t cfg, cpu_phandle;
  45. /* leave the GIC node intact if a GIC is present */
  46. cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
  47. if (cfg & SEAD_CONFIG_GIC_PRESENT)
  48. return 0;
  49. gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
  50. if (gic_off < 0) {
  51. pr_err("unable to find DT GIC node: %d\n", gic_off);
  52. return gic_off;
  53. }
  54. err = fdt_nop_node(fdt, gic_off);
  55. if (err) {
  56. pr_err("unable to nop GIC node\n");
  57. return err;
  58. }
  59. cpu_off = fdt_node_offset_by_compatible(fdt, -1,
  60. "mti,cpu-interrupt-controller");
  61. if (cpu_off < 0) {
  62. pr_err("unable to find CPU intc node: %d\n", cpu_off);
  63. return cpu_off;
  64. }
  65. cpu_phandle = fdt_get_phandle(fdt, cpu_off);
  66. if (!cpu_phandle) {
  67. pr_err("unable to get CPU intc phandle\n");
  68. return -EINVAL;
  69. }
  70. uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
  71. while (uart_off >= 0) {
  72. err = fdt_setprop_u32(fdt, uart_off, "interrupt-parent",
  73. cpu_phandle);
  74. if (err) {
  75. pr_warn("unable to set UART interrupt-parent: %d\n",
  76. err);
  77. return err;
  78. }
  79. err = fdt_setprop_u32(fdt, uart_off, "interrupts",
  80. cpu_uart_int);
  81. if (err) {
  82. pr_err("unable to set UART interrupts property: %d\n",
  83. err);
  84. return err;
  85. }
  86. uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
  87. "ns16550a");
  88. }
  89. if (uart_off != -FDT_ERR_NOTFOUND) {
  90. pr_err("error searching for UART DT node: %d\n", uart_off);
  91. return uart_off;
  92. }
  93. eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
  94. if (eth_off < 0) {
  95. pr_err("unable to find ethernet DT node: %d\n", eth_off);
  96. return eth_off;
  97. }
  98. err = fdt_setprop_u32(fdt, eth_off, "interrupt-parent", cpu_phandle);
  99. if (err) {
  100. pr_err("unable to set ethernet interrupt-parent: %d\n", err);
  101. return err;
  102. }
  103. err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
  104. if (err) {
  105. pr_err("unable to set ethernet interrupts property: %d\n", err);
  106. return err;
  107. }
  108. ehci_off = fdt_node_offset_by_compatible(fdt, -1, "generic-ehci");
  109. if (ehci_off < 0) {
  110. pr_err("unable to find EHCI DT node: %d\n", ehci_off);
  111. return ehci_off;
  112. }
  113. err = fdt_setprop_u32(fdt, ehci_off, "interrupt-parent", cpu_phandle);
  114. if (err) {
  115. pr_err("unable to set EHCI interrupt-parent: %d\n", err);
  116. return err;
  117. }
  118. err = fdt_setprop_u32(fdt, ehci_off, "interrupts", cpu_ehci_int);
  119. if (err) {
  120. pr_err("unable to set EHCI interrupts property: %d\n", err);
  121. return err;
  122. }
  123. return 0;
  124. }
  125. static const struct mips_fdt_fixup sead3_fdt_fixups[] __initconst = {
  126. { yamon_dt_append_cmdline, "append command line" },
  127. { append_memory, "append memory" },
  128. { remove_gic, "remove GIC when not present" },
  129. { yamon_dt_serial_config, "append serial configuration" },
  130. { },
  131. };
  132. static __init const void *sead3_fixup_fdt(const void *fdt,
  133. const void *match_data)
  134. {
  135. static unsigned char fdt_buf[16 << 10] __initdata;
  136. int err;
  137. if (fdt_check_header(fdt))
  138. panic("Corrupt DT");
  139. /* if this isn't SEAD3, something went wrong */
  140. BUG_ON(fdt_node_check_compatible(fdt, 0, "mti,sead-3"));
  141. fw_init_cmdline();
  142. err = apply_mips_fdt_fixups(fdt_buf, sizeof(fdt_buf),
  143. fdt, sead3_fdt_fixups);
  144. if (err)
  145. panic("Unable to fixup FDT: %d", err);
  146. return fdt_buf;
  147. }
  148. static __init unsigned int sead3_measure_hpt_freq(void)
  149. {
  150. void __iomem *status_reg = (void __iomem *)0xbf000410;
  151. unsigned int freq, orig, tick = 0;
  152. unsigned long flags;
  153. local_irq_save(flags);
  154. orig = readl(status_reg) & 0x2; /* get original sample */
  155. /* wait for transition */
  156. while ((readl(status_reg) & 0x2) == orig)
  157. ;
  158. orig = orig ^ 0x2; /* flip the bit */
  159. write_c0_count(0);
  160. /* wait 1 second (the sampling clock transitions every 10ms) */
  161. while (tick < 100) {
  162. /* wait for transition */
  163. while ((readl(status_reg) & 0x2) == orig)
  164. ;
  165. orig = orig ^ 0x2; /* flip the bit */
  166. tick++;
  167. }
  168. freq = read_c0_count();
  169. local_irq_restore(flags);
  170. return freq;
  171. }
  172. extern char __dtb_sead3_begin[];
  173. MIPS_MACHINE(sead3) = {
  174. .fdt = __dtb_sead3_begin,
  175. .detect = sead3_detect,
  176. .fixup_fdt = sead3_fixup_fdt,
  177. .measure_hpt_freq = sead3_measure_hpt_freq,
  178. };