board-sead3.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (C) 2016 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #define pr_fmt(fmt) "sead3: " fmt
  11. #include <linux/errno.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/printk.h>
  14. #include <asm/fw/fw.h>
  15. #include <asm/io.h>
  16. #include <asm/machine.h>
  17. #define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
  18. #define SEAD_CONFIG_GIC_PRESENT BIT(1)
  19. #define MIPS_REVISION CKSEG1ADDR(0x1fc00010)
  20. #define MIPS_REVISION_MACHINE (0xf << 4)
  21. #define MIPS_REVISION_MACHINE_SEAD3 (0x4 << 4)
  22. static __init bool sead3_detect(void)
  23. {
  24. uint32_t rev;
  25. rev = __raw_readl((void *)MIPS_REVISION);
  26. return (rev & MIPS_REVISION_MACHINE) == MIPS_REVISION_MACHINE_SEAD3;
  27. }
  28. static __init int append_cmdline(void *fdt)
  29. {
  30. int err, chosen_off;
  31. /* find or add chosen node */
  32. chosen_off = fdt_path_offset(fdt, "/chosen");
  33. if (chosen_off == -FDT_ERR_NOTFOUND)
  34. chosen_off = fdt_path_offset(fdt, "/chosen@0");
  35. if (chosen_off == -FDT_ERR_NOTFOUND)
  36. chosen_off = fdt_add_subnode(fdt, 0, "chosen");
  37. if (chosen_off < 0) {
  38. pr_err("Unable to find or add DT chosen node: %d\n",
  39. chosen_off);
  40. return chosen_off;
  41. }
  42. err = fdt_setprop_string(fdt, chosen_off, "bootargs", fw_getcmdline());
  43. if (err) {
  44. pr_err("Unable to set bootargs property: %d\n", err);
  45. return err;
  46. }
  47. return 0;
  48. }
  49. static __init int append_memory(void *fdt)
  50. {
  51. unsigned long phys_memsize, memsize;
  52. __be32 mem_array[2];
  53. int err, mem_off;
  54. char *var;
  55. /* find memory size from the bootloader environment */
  56. var = fw_getenv("memsize");
  57. if (var) {
  58. err = kstrtoul(var, 0, &phys_memsize);
  59. if (err) {
  60. pr_err("Failed to read memsize env variable '%s'\n",
  61. var);
  62. return -EINVAL;
  63. }
  64. } else {
  65. pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
  66. phys_memsize = 32 << 20;
  67. }
  68. /* default to using all available RAM */
  69. memsize = phys_memsize;
  70. /* allow the user to override the usable memory */
  71. var = strstr(arcs_cmdline, "memsize=");
  72. if (var)
  73. memsize = memparse(var + strlen("memsize="), NULL);
  74. /* if the user says there's more RAM than we thought, believe them */
  75. phys_memsize = max_t(unsigned long, phys_memsize, memsize);
  76. /* find or add a memory node */
  77. mem_off = fdt_path_offset(fdt, "/memory");
  78. if (mem_off == -FDT_ERR_NOTFOUND)
  79. mem_off = fdt_add_subnode(fdt, 0, "memory");
  80. if (mem_off < 0) {
  81. pr_err("Unable to find or add memory DT node: %d\n", mem_off);
  82. return mem_off;
  83. }
  84. err = fdt_setprop_string(fdt, mem_off, "device_type", "memory");
  85. if (err) {
  86. pr_err("Unable to set memory node device_type: %d\n", err);
  87. return err;
  88. }
  89. mem_array[0] = 0;
  90. mem_array[1] = cpu_to_be32(phys_memsize);
  91. err = fdt_setprop(fdt, mem_off, "reg", mem_array, sizeof(mem_array));
  92. if (err) {
  93. pr_err("Unable to set memory regs property: %d\n", err);
  94. return err;
  95. }
  96. mem_array[0] = 0;
  97. mem_array[1] = cpu_to_be32(memsize);
  98. err = fdt_setprop(fdt, mem_off, "linux,usable-memory",
  99. mem_array, sizeof(mem_array));
  100. if (err) {
  101. pr_err("Unable to set linux,usable-memory property: %d\n", err);
  102. return err;
  103. }
  104. return 0;
  105. }
  106. static __init int remove_gic(void *fdt)
  107. {
  108. const unsigned int cpu_ehci_int = 2;
  109. const unsigned int cpu_uart_int = 4;
  110. const unsigned int cpu_eth_int = 6;
  111. int gic_off, cpu_off, uart_off, eth_off, ehci_off, err;
  112. uint32_t cfg, cpu_phandle;
  113. /* leave the GIC node intact if a GIC is present */
  114. cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
  115. if (cfg & SEAD_CONFIG_GIC_PRESENT)
  116. return 0;
  117. gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
  118. if (gic_off < 0) {
  119. pr_err("unable to find DT GIC node: %d\n", gic_off);
  120. return gic_off;
  121. }
  122. err = fdt_nop_node(fdt, gic_off);
  123. if (err) {
  124. pr_err("unable to nop GIC node\n");
  125. return err;
  126. }
  127. cpu_off = fdt_node_offset_by_compatible(fdt, -1,
  128. "mti,cpu-interrupt-controller");
  129. if (cpu_off < 0) {
  130. pr_err("unable to find CPU intc node: %d\n", cpu_off);
  131. return cpu_off;
  132. }
  133. cpu_phandle = fdt_get_phandle(fdt, cpu_off);
  134. if (!cpu_phandle) {
  135. pr_err("unable to get CPU intc phandle\n");
  136. return -EINVAL;
  137. }
  138. err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
  139. if (err) {
  140. pr_err("unable to set root interrupt-parent: %d\n", err);
  141. return err;
  142. }
  143. uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
  144. while (uart_off >= 0) {
  145. err = fdt_setprop_u32(fdt, uart_off, "interrupts",
  146. cpu_uart_int);
  147. if (err) {
  148. pr_err("unable to set UART interrupts property: %d\n",
  149. err);
  150. return err;
  151. }
  152. uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
  153. "ns16550a");
  154. }
  155. if (uart_off != -FDT_ERR_NOTFOUND) {
  156. pr_err("error searching for UART DT node: %d\n", uart_off);
  157. return uart_off;
  158. }
  159. eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
  160. if (eth_off < 0) {
  161. pr_err("unable to find ethernet DT node: %d\n", eth_off);
  162. return eth_off;
  163. }
  164. err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
  165. if (err) {
  166. pr_err("unable to set ethernet interrupts property: %d\n", err);
  167. return err;
  168. }
  169. ehci_off = fdt_node_offset_by_compatible(fdt, -1, "generic-ehci");
  170. if (ehci_off < 0) {
  171. pr_err("unable to find EHCI DT node: %d\n", ehci_off);
  172. return ehci_off;
  173. }
  174. err = fdt_setprop_u32(fdt, ehci_off, "interrupts", cpu_ehci_int);
  175. if (err) {
  176. pr_err("unable to set EHCI interrupts property: %d\n", err);
  177. return err;
  178. }
  179. return 0;
  180. }
  181. static __init int serial_config(void *fdt)
  182. {
  183. const char *yamontty, *mode_var;
  184. char mode_var_name[9], path[18], parity;
  185. unsigned int uart, baud, stop_bits;
  186. bool hw_flow;
  187. int chosen_off, err;
  188. yamontty = fw_getenv("yamontty");
  189. if (!yamontty || !strcmp(yamontty, "tty0")) {
  190. uart = 0;
  191. } else if (!strcmp(yamontty, "tty1")) {
  192. uart = 1;
  193. } else {
  194. pr_warn("yamontty environment variable '%s' invalid\n",
  195. yamontty);
  196. uart = 0;
  197. }
  198. baud = stop_bits = 0;
  199. parity = 0;
  200. hw_flow = false;
  201. snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
  202. mode_var = fw_getenv(mode_var_name);
  203. if (mode_var) {
  204. while (mode_var[0] >= '0' && mode_var[0] <= '9') {
  205. baud *= 10;
  206. baud += mode_var[0] - '0';
  207. mode_var++;
  208. }
  209. if (mode_var[0] == ',')
  210. mode_var++;
  211. if (mode_var[0])
  212. parity = mode_var[0];
  213. if (mode_var[0] == ',')
  214. mode_var++;
  215. if (mode_var[0])
  216. stop_bits = mode_var[0] - '0';
  217. if (mode_var[0] == ',')
  218. mode_var++;
  219. if (!strcmp(mode_var, "hw"))
  220. hw_flow = true;
  221. }
  222. if (!baud)
  223. baud = 38400;
  224. if (parity != 'e' && parity != 'n' && parity != 'o')
  225. parity = 'n';
  226. if (stop_bits != 7 && stop_bits != 8)
  227. stop_bits = 8;
  228. WARN_ON(snprintf(path, sizeof(path), "uart%u:%u%c%u%s",
  229. uart, baud, parity, stop_bits,
  230. hw_flow ? "r" : "") >= sizeof(path));
  231. /* find or add chosen node */
  232. chosen_off = fdt_path_offset(fdt, "/chosen");
  233. if (chosen_off == -FDT_ERR_NOTFOUND)
  234. chosen_off = fdt_path_offset(fdt, "/chosen@0");
  235. if (chosen_off == -FDT_ERR_NOTFOUND)
  236. chosen_off = fdt_add_subnode(fdt, 0, "chosen");
  237. if (chosen_off < 0) {
  238. pr_err("Unable to find or add DT chosen node: %d\n",
  239. chosen_off);
  240. return chosen_off;
  241. }
  242. err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
  243. if (err) {
  244. pr_err("Unable to set stdout-path property: %d\n", err);
  245. return err;
  246. }
  247. return 0;
  248. }
  249. static __init const void *sead3_fixup_fdt(const void *fdt,
  250. const void *match_data)
  251. {
  252. static unsigned char fdt_buf[16 << 10] __initdata;
  253. int err;
  254. if (fdt_check_header(fdt))
  255. panic("Corrupt DT");
  256. /* if this isn't SEAD3, something went wrong */
  257. BUG_ON(fdt_node_check_compatible(fdt, 0, "mti,sead-3"));
  258. fw_init_cmdline();
  259. err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
  260. if (err)
  261. panic("Unable to open FDT: %d", err);
  262. err = append_cmdline(fdt_buf);
  263. if (err)
  264. panic("Unable to patch FDT: %d", err);
  265. err = append_memory(fdt_buf);
  266. if (err)
  267. panic("Unable to patch FDT: %d", err);
  268. err = remove_gic(fdt_buf);
  269. if (err)
  270. panic("Unable to patch FDT: %d", err);
  271. err = serial_config(fdt_buf);
  272. if (err)
  273. panic("Unable to patch FDT: %d", err);
  274. err = fdt_pack(fdt_buf);
  275. if (err)
  276. panic("Unable to pack FDT: %d\n", err);
  277. return fdt_buf;
  278. }
  279. static __init unsigned int sead3_measure_hpt_freq(void)
  280. {
  281. void __iomem *status_reg = (void __iomem *)0xbf000410;
  282. unsigned int freq, orig, tick = 0;
  283. unsigned long flags;
  284. local_irq_save(flags);
  285. orig = readl(status_reg) & 0x2; /* get original sample */
  286. /* wait for transition */
  287. while ((readl(status_reg) & 0x2) == orig)
  288. ;
  289. orig = orig ^ 0x2; /* flip the bit */
  290. write_c0_count(0);
  291. /* wait 1 second (the sampling clock transitions every 10ms) */
  292. while (tick < 100) {
  293. /* wait for transition */
  294. while ((readl(status_reg) & 0x2) == orig)
  295. ;
  296. orig = orig ^ 0x2; /* flip the bit */
  297. tick++;
  298. }
  299. freq = read_c0_count();
  300. local_irq_restore(flags);
  301. return freq;
  302. }
  303. extern char __dtb_sead3_begin[];
  304. MIPS_MACHINE(sead3) = {
  305. .fdt = __dtb_sead3_begin,
  306. .detect = sead3_detect,
  307. .fixup_fdt = sead3_fixup_fdt,
  308. .measure_hpt_freq = sead3_measure_hpt_freq,
  309. };