2 * Copyright (C) 2015 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
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.
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>
20 static unsigned char fdt_buf
[16 << 10] __initdata
;
22 /* determined physical memory size, not overridden by command line args */
23 extern unsigned long physical_memsize
;
25 #define MAX_MEM_ARRAY_ENTRIES 1
27 static unsigned __init
gen_fdt_mem_array(__be32
*mem_array
, unsigned long size
)
29 unsigned long size_preio
;
33 mem_array
[0] = cpu_to_be32(PHYS_OFFSET
);
34 if (config_enabled(CONFIG_EVA
)) {
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.
41 mem_array
[1] = cpu_to_be32(size
);
43 size_preio
= min_t(unsigned long, size
, SZ_256M
);
44 mem_array
[1] = cpu_to_be32(size_preio
);
47 BUG_ON(entries
> MAX_MEM_ARRAY_ENTRIES
);
51 static void __init
append_memory(void *fdt
, int root_off
)
53 __be32 mem_array
[2 * MAX_MEM_ARRAY_ENTRIES
];
54 unsigned long memsize
;
57 char *var
, param_name
[10], *var_names
[] = {
58 "ememsize", "memsize",
61 /* if a memory node already exists, leave it alone */
62 mem_off
= fdt_path_offset(fdt
, "/memory");
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
]);
72 err
= kstrtoul(var
, 0, &physical_memsize
);
76 pr_warn("Failed to read the '%s' env variable '%s'\n",
80 if (!physical_memsize
) {
81 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
82 physical_memsize
= 32 << 20;
85 if (config_enabled(CONFIG_CPU_BIG_ENDIAN
)) {
87 * SOC-it swaps, or perhaps doesn't swap, when DMA'ing
88 * the last word of physical memory.
90 physical_memsize
-= PAGE_SIZE
;
93 /* default to using all available RAM */
94 memsize
= physical_memsize
;
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
);
103 memsize
= memparse(var
+ strlen(param_name
), NULL
);
106 /* if the user says there's more RAM than we thought, believe them */
107 physical_memsize
= max_t(unsigned long, physical_memsize
, memsize
);
109 /* append memory to the DT */
110 mem_off
= fdt_add_subnode(fdt
, root_off
, "memory");
112 panic("Unable to add memory node to DT: %d", mem_off
);
114 err
= fdt_setprop_string(fdt
, mem_off
, "device_type", "memory");
116 panic("Unable to set memory node device_type: %d", err
);
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]));
122 panic("Unable to set memory regs property: %d", err
);
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]));
128 panic("Unable to set linux,usable-memory property: %d", err
);
131 void __init
*malta_dt_shim(void *fdt
)
133 int root_off
, len
, err
;
136 if (fdt_check_header(fdt
))
139 err
= fdt_open_into(fdt
, fdt_buf
, sizeof(fdt_buf
));
141 panic("Unable to open FDT: %d", err
);
143 root_off
= fdt_path_offset(fdt_buf
, "/");
145 panic("No / node in DT");
147 compat
= fdt_getprop(fdt_buf
, root_off
, "compatible", &len
);
149 panic("No root compatible property in DT: %d", len
);
151 /* if this isn't Malta, leave the DT alone */
152 if (strncmp(compat
, "mti,malta", len
))
155 append_memory(fdt_buf
, root_off
);
157 err
= fdt_pack(fdt_buf
);
159 panic("Unable to pack FDT: %d\n", err
);