malta-dtshim.c 8.8 KB

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