1 /* 2 * Copyright (C) 2015 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 11 #include <linux/bug.h> 12 #include <linux/kernel.h> 13 #include <linux/libfdt.h> 14 #include <linux/of_fdt.h> 15 #include <linux/sizes.h> 16 #include <asm/bootinfo.h> 17 #include <asm/fw/fw.h> 18 #include <asm/page.h> 19 20 static unsigned char fdt_buf[16 << 10] __initdata; 21 22 /* determined physical memory size, not overridden by command line args */ 23 extern unsigned long physical_memsize; 24 25 #define MAX_MEM_ARRAY_ENTRIES 1 26 27 static unsigned __init gen_fdt_mem_array(__be32 *mem_array, unsigned long size) 28 { 29 unsigned long size_preio; 30 unsigned entries; 31 32 entries = 1; 33 mem_array[0] = cpu_to_be32(PHYS_OFFSET); 34 if (config_enabled(CONFIG_EVA)) { 35 /* 36 * The current Malta EVA configuration is "special" in that it 37 * always makes use of addresses in the upper half of the 32 bit 38 * physical address map, which gives it a contiguous region of 39 * DDR but limits it to 2GB. 40 */ 41 mem_array[1] = cpu_to_be32(size); 42 } else { 43 size_preio = min_t(unsigned long, size, SZ_256M); 44 mem_array[1] = cpu_to_be32(size_preio); 45 } 46 47 BUG_ON(entries > MAX_MEM_ARRAY_ENTRIES); 48 return entries; 49 } 50 51 static void __init append_memory(void *fdt, int root_off) 52 { 53 __be32 mem_array[2 * MAX_MEM_ARRAY_ENTRIES]; 54 unsigned long memsize; 55 unsigned mem_entries; 56 int i, err, mem_off; 57 char *var, param_name[10], *var_names[] = { 58 "ememsize", "memsize", 59 }; 60 61 /* if a memory node already exists, leave it alone */ 62 mem_off = fdt_path_offset(fdt, "/memory"); 63 if (mem_off >= 0) 64 return; 65 66 /* find memory size from the bootloader environment */ 67 for (i = 0; i < ARRAY_SIZE(var_names); i++) { 68 var = fw_getenv(var_names[i]); 69 if (!var) 70 continue; 71 72 err = kstrtoul(var, 0, &physical_memsize); 73 if (!err) 74 break; 75 76 pr_warn("Failed to read the '%s' env variable '%s'\n", 77 var_names[i], var); 78 } 79 80 if (!physical_memsize) { 81 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n"); 82 physical_memsize = 32 << 20; 83 } 84 85 if (config_enabled(CONFIG_CPU_BIG_ENDIAN)) { 86 /* 87 * SOC-it swaps, or perhaps doesn't swap, when DMA'ing 88 * the last word of physical memory. 89 */ 90 physical_memsize -= PAGE_SIZE; 91 } 92 93 /* default to using all available RAM */ 94 memsize = physical_memsize; 95 96 /* allow the user to override the usable memory */ 97 for (i = 0; i < ARRAY_SIZE(var_names); i++) { 98 snprintf(param_name, sizeof(param_name), "%s=", var_names[i]); 99 var = strstr(arcs_cmdline, param_name); 100 if (!var) 101 continue; 102 103 memsize = memparse(var + strlen(param_name), NULL); 104 } 105 106 /* if the user says there's more RAM than we thought, believe them */ 107 physical_memsize = max_t(unsigned long, physical_memsize, memsize); 108 109 /* append memory to the DT */ 110 mem_off = fdt_add_subnode(fdt, root_off, "memory"); 111 if (mem_off < 0) 112 panic("Unable to add memory node to DT: %d", mem_off); 113 114 err = fdt_setprop_string(fdt, mem_off, "device_type", "memory"); 115 if (err) 116 panic("Unable to set memory node device_type: %d", err); 117 118 mem_entries = gen_fdt_mem_array(mem_array, physical_memsize); 119 err = fdt_setprop(fdt, mem_off, "reg", mem_array, 120 mem_entries * 2 * sizeof(mem_array[0])); 121 if (err) 122 panic("Unable to set memory regs property: %d", err); 123 124 mem_entries = gen_fdt_mem_array(mem_array, memsize); 125 err = fdt_setprop(fdt, mem_off, "linux,usable-memory", mem_array, 126 mem_entries * 2 * sizeof(mem_array[0])); 127 if (err) 128 panic("Unable to set linux,usable-memory property: %d", err); 129 } 130 131 void __init *malta_dt_shim(void *fdt) 132 { 133 int root_off, len, err; 134 const char *compat; 135 136 if (fdt_check_header(fdt)) 137 panic("Corrupt DT"); 138 139 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf)); 140 if (err) 141 panic("Unable to open FDT: %d", err); 142 143 root_off = fdt_path_offset(fdt_buf, "/"); 144 if (root_off < 0) 145 panic("No / node in DT"); 146 147 compat = fdt_getprop(fdt_buf, root_off, "compatible", &len); 148 if (!compat) 149 panic("No root compatible property in DT: %d", len); 150 151 /* if this isn't Malta, leave the DT alone */ 152 if (strncmp(compat, "mti,malta", len)) 153 return fdt; 154 155 append_memory(fdt_buf, root_off); 156 157 err = fdt_pack(fdt_buf); 158 if (err) 159 panic("Unable to pack FDT: %d\n", err); 160 161 return fdt_buf; 162 } 163