malta-dtshim.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2015 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.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. #include <linux/bug.h>
  11. #include <linux/kernel.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/sizes.h>
  15. #include <asm/addrspace.h>
  16. #include <asm/bootinfo.h>
  17. #include <asm/fw/fw.h>
  18. #include <asm/mips-boards/generic.h>
  19. #include <asm/mips-boards/malta.h>
  20. #include <asm/mips-cps.h>
  21. #include <asm/page.h>
  22. #define ROCIT_REG_BASE 0x1f403000
  23. #define ROCIT_CONFIG_GEN1 (ROCIT_REG_BASE + 0x04)
  24. #define ROCIT_CONFIG_GEN1_MEMMAP_SHIFT 8
  25. #define ROCIT_CONFIG_GEN1_MEMMAP_MASK (0xf << 8)
  26. static unsigned char fdt_buf[16 << 10] __initdata;
  27. /* determined physical memory size, not overridden by command line args */
  28. extern unsigned long physical_memsize;
  29. enum mem_map {
  30. MEM_MAP_V1 = 0,
  31. MEM_MAP_V2,
  32. };
  33. #define MAX_MEM_ARRAY_ENTRIES 2
  34. static __init int malta_scon(void)
  35. {
  36. int scon = MIPS_REVISION_SCONID;
  37. if (scon != MIPS_REVISION_SCON_OTHER)
  38. return scon;
  39. switch (MIPS_REVISION_CORID) {
  40. case MIPS_REVISION_CORID_QED_RM5261:
  41. case MIPS_REVISION_CORID_CORE_LV:
  42. case MIPS_REVISION_CORID_CORE_FPGA:
  43. case MIPS_REVISION_CORID_CORE_FPGAR2:
  44. return MIPS_REVISION_SCON_GT64120;
  45. case MIPS_REVISION_CORID_CORE_EMUL_BON:
  46. case MIPS_REVISION_CORID_BONITO64:
  47. case MIPS_REVISION_CORID_CORE_20K:
  48. return MIPS_REVISION_SCON_BONITO;
  49. case MIPS_REVISION_CORID_CORE_MSC:
  50. case MIPS_REVISION_CORID_CORE_FPGA2:
  51. case MIPS_REVISION_CORID_CORE_24K:
  52. return MIPS_REVISION_SCON_SOCIT;
  53. case MIPS_REVISION_CORID_CORE_FPGA3:
  54. case MIPS_REVISION_CORID_CORE_FPGA4:
  55. case MIPS_REVISION_CORID_CORE_FPGA5:
  56. case MIPS_REVISION_CORID_CORE_EMUL_MSC:
  57. default:
  58. return MIPS_REVISION_SCON_ROCIT;
  59. }
  60. }
  61. static unsigned __init gen_fdt_mem_array(__be32 *mem_array, unsigned long size,
  62. enum mem_map map)
  63. {
  64. unsigned long size_preio;
  65. unsigned entries;
  66. entries = 1;
  67. mem_array[0] = cpu_to_be32(PHYS_OFFSET);
  68. if (IS_ENABLED(CONFIG_EVA)) {
  69. /*
  70. * The current Malta EVA configuration is "special" in that it
  71. * always makes use of addresses in the upper half of the 32 bit
  72. * physical address map, which gives it a contiguous region of
  73. * DDR but limits it to 2GB.
  74. */
  75. mem_array[1] = cpu_to_be32(size);
  76. goto done;
  77. }
  78. size_preio = min_t(unsigned long, size, SZ_256M);
  79. mem_array[1] = cpu_to_be32(size_preio);
  80. size -= size_preio;
  81. if (!size)
  82. goto done;
  83. if (map == MEM_MAP_V2) {
  84. /*
  85. * We have a flat 32 bit physical memory map with DDR filling
  86. * all 4GB of the memory map, apart from the I/O region which
  87. * obscures 256MB from 0x10000000-0x1fffffff.
  88. *
  89. * Therefore we discard the 256MB behind the I/O region.
  90. */
  91. if (size <= SZ_256M)
  92. goto done;
  93. size -= SZ_256M;
  94. /* Make use of the memory following the I/O region */
  95. entries++;
  96. mem_array[2] = cpu_to_be32(PHYS_OFFSET + SZ_512M);
  97. mem_array[3] = cpu_to_be32(size);
  98. } else {
  99. /*
  100. * We have a 32 bit physical memory map with a 2GB DDR region
  101. * aliased in the upper & lower halves of it. The I/O region
  102. * obscures 256MB from 0x10000000-0x1fffffff in the low alias
  103. * but the DDR it obscures is accessible via the high alias.
  104. *
  105. * Simply access everything beyond the lowest 256MB of DDR using
  106. * the high alias.
  107. */
  108. entries++;
  109. mem_array[2] = cpu_to_be32(PHYS_OFFSET + SZ_2G + SZ_256M);
  110. mem_array[3] = cpu_to_be32(size);
  111. }
  112. done:
  113. BUG_ON(entries > MAX_MEM_ARRAY_ENTRIES);
  114. return entries;
  115. }
  116. static void __init append_memory(void *fdt, int root_off)
  117. {
  118. __be32 mem_array[2 * MAX_MEM_ARRAY_ENTRIES];
  119. unsigned long memsize;
  120. unsigned mem_entries;
  121. int i, err, mem_off;
  122. enum mem_map mem_map;
  123. u32 config;
  124. char *var, param_name[10], *var_names[] = {
  125. "ememsize", "memsize",
  126. };
  127. /* if a memory node already exists, leave it alone */
  128. mem_off = fdt_path_offset(fdt, "/memory");
  129. if (mem_off >= 0)
  130. return;
  131. /* find memory size from the bootloader environment */
  132. for (i = 0; i < ARRAY_SIZE(var_names); i++) {
  133. var = fw_getenv(var_names[i]);
  134. if (!var)
  135. continue;
  136. err = kstrtoul(var, 0, &physical_memsize);
  137. if (!err)
  138. break;
  139. pr_warn("Failed to read the '%s' env variable '%s'\n",
  140. var_names[i], var);
  141. }
  142. if (!physical_memsize) {
  143. pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
  144. physical_memsize = 32 << 20;
  145. }
  146. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
  147. /*
  148. * SOC-it swaps, or perhaps doesn't swap, when DMA'ing
  149. * the last word of physical memory.
  150. */
  151. physical_memsize -= PAGE_SIZE;
  152. }
  153. /* default to using all available RAM */
  154. memsize = physical_memsize;
  155. /* allow the user to override the usable memory */
  156. for (i = 0; i < ARRAY_SIZE(var_names); i++) {
  157. snprintf(param_name, sizeof(param_name), "%s=", var_names[i]);
  158. var = strstr(arcs_cmdline, param_name);
  159. if (!var)
  160. continue;
  161. memsize = memparse(var + strlen(param_name), NULL);
  162. }
  163. /* if the user says there's more RAM than we thought, believe them */
  164. physical_memsize = max_t(unsigned long, physical_memsize, memsize);
  165. /* detect the memory map in use */
  166. if (malta_scon() == MIPS_REVISION_SCON_ROCIT) {
  167. /* ROCit has a register indicating the memory map in use */
  168. config = readl((void __iomem *)CKSEG1ADDR(ROCIT_CONFIG_GEN1));
  169. mem_map = config & ROCIT_CONFIG_GEN1_MEMMAP_MASK;
  170. mem_map >>= ROCIT_CONFIG_GEN1_MEMMAP_SHIFT;
  171. } else {
  172. /* if not using ROCit, presume the v1 memory map */
  173. mem_map = MEM_MAP_V1;
  174. }
  175. if (mem_map > MEM_MAP_V2)
  176. panic("Unsupported physical memory map v%u detected",
  177. (unsigned int)mem_map);
  178. /* append memory to the DT */
  179. mem_off = fdt_add_subnode(fdt, root_off, "memory");
  180. if (mem_off < 0)
  181. panic("Unable to add memory node to DT: %d", mem_off);
  182. err = fdt_setprop_string(fdt, mem_off, "device_type", "memory");
  183. if (err)
  184. panic("Unable to set memory node device_type: %d", err);
  185. mem_entries = gen_fdt_mem_array(mem_array, physical_memsize, mem_map);
  186. err = fdt_setprop(fdt, mem_off, "reg", mem_array,
  187. mem_entries * 2 * sizeof(mem_array[0]));
  188. if (err)
  189. panic("Unable to set memory regs property: %d", err);
  190. mem_entries = gen_fdt_mem_array(mem_array, memsize, mem_map);
  191. err = fdt_setprop(fdt, mem_off, "linux,usable-memory", mem_array,
  192. mem_entries * 2 * sizeof(mem_array[0]));
  193. if (err)
  194. panic("Unable to set linux,usable-memory property: %d", err);
  195. }
  196. static void __init remove_gic(void *fdt)
  197. {
  198. int err, gic_off, i8259_off, cpu_off;
  199. void __iomem *biu_base;
  200. uint32_t cpu_phandle, sc_cfg;
  201. /* if we have a CM which reports a GIC is present, leave the DT alone */
  202. err = mips_cm_probe();
  203. if (!err && (read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX))
  204. return;
  205. if (malta_scon() == MIPS_REVISION_SCON_ROCIT) {
  206. /*
  207. * On systems using the RocIT system controller a GIC may be
  208. * present without a CM. Detect whether that is the case.
  209. */
  210. biu_base = ioremap_nocache(MSC01_BIU_REG_BASE,
  211. MSC01_BIU_ADDRSPACE_SZ);
  212. sc_cfg = __raw_readl(biu_base + MSC01_SC_CFG_OFS);
  213. if (sc_cfg & MSC01_SC_CFG_GICPRES_MSK) {
  214. /* enable the GIC at the system controller level */
  215. sc_cfg |= BIT(MSC01_SC_CFG_GICENA_SHF);
  216. __raw_writel(sc_cfg, biu_base + MSC01_SC_CFG_OFS);
  217. return;
  218. }
  219. }
  220. gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
  221. if (gic_off < 0) {
  222. pr_warn("malta-dtshim: unable to find DT GIC node: %d\n",
  223. gic_off);
  224. return;
  225. }
  226. err = fdt_nop_node(fdt, gic_off);
  227. if (err)
  228. pr_warn("malta-dtshim: unable to nop GIC node\n");
  229. i8259_off = fdt_node_offset_by_compatible(fdt, -1, "intel,i8259");
  230. if (i8259_off < 0) {
  231. pr_warn("malta-dtshim: unable to find DT i8259 node: %d\n",
  232. i8259_off);
  233. return;
  234. }
  235. cpu_off = fdt_node_offset_by_compatible(fdt, -1,
  236. "mti,cpu-interrupt-controller");
  237. if (cpu_off < 0) {
  238. pr_warn("malta-dtshim: unable to find CPU intc node: %d\n",
  239. cpu_off);
  240. return;
  241. }
  242. cpu_phandle = fdt_get_phandle(fdt, cpu_off);
  243. if (!cpu_phandle) {
  244. pr_warn("malta-dtshim: unable to get CPU intc phandle\n");
  245. return;
  246. }
  247. err = fdt_setprop_u32(fdt, i8259_off, "interrupt-parent", cpu_phandle);
  248. if (err) {
  249. pr_warn("malta-dtshim: unable to set i8259 interrupt-parent: %d\n",
  250. err);
  251. return;
  252. }
  253. err = fdt_setprop_u32(fdt, i8259_off, "interrupts", 2);
  254. if (err) {
  255. pr_warn("malta-dtshim: unable to set i8259 interrupts: %d\n",
  256. err);
  257. return;
  258. }
  259. }
  260. void __init *malta_dt_shim(void *fdt)
  261. {
  262. int root_off, len, err;
  263. const char *compat;
  264. if (fdt_check_header(fdt))
  265. panic("Corrupt DT");
  266. err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
  267. if (err)
  268. panic("Unable to open FDT: %d", err);
  269. root_off = fdt_path_offset(fdt_buf, "/");
  270. if (root_off < 0)
  271. panic("No / node in DT");
  272. compat = fdt_getprop(fdt_buf, root_off, "compatible", &len);
  273. if (!compat)
  274. panic("No root compatible property in DT: %d", len);
  275. /* if this isn't Malta, leave the DT alone */
  276. if (strncmp(compat, "mti,malta", len))
  277. return fdt;
  278. append_memory(fdt_buf, root_off);
  279. remove_gic(fdt_buf);
  280. err = fdt_pack(fdt_buf);
  281. if (err)
  282. panic("Unable to pack FDT: %d\n", err);
  283. return fdt_buf;
  284. }