thermal: fix Mediatek thermal controller build
[linux/fpc-iii.git] / arch / mips / mti-malta / malta-dtshim.c
blobf7133efc58431113656f552c89b5438256bbc7e7
1 /*
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.
9 */
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>
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;
30 unsigned entries;
32 entries = 1;
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);
42 } else {
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);
48 return 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;
55 unsigned mem_entries;
56 int i, err, mem_off;
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");
63 if (mem_off >= 0)
64 return;
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;
72 err = kstrtoul(var, 0, &physical_memsize);
73 if (!err)
74 break;
76 pr_warn("Failed to read the '%s' env variable '%s'\n",
77 var_names[i], var);
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);
100 if (!var)
101 continue;
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");
111 if (mem_off < 0)
112 panic("Unable to add memory node to DT: %d", mem_off);
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);
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);
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);
131 void __init *malta_dt_shim(void *fdt)
133 int root_off, len, err;
134 const char *compat;
136 if (fdt_check_header(fdt))
137 panic("Corrupt DT");
139 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
140 if (err)
141 panic("Unable to open FDT: %d", err);
143 root_off = fdt_path_offset(fdt_buf, "/");
144 if (root_off < 0)
145 panic("No / node in DT");
147 compat = fdt_getprop(fdt_buf, root_off, "compatible", &len);
148 if (!compat)
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))
153 return fdt;
155 append_memory(fdt_buf, root_off);
157 err = fdt_pack(fdt_buf);
158 if (err)
159 panic("Unable to pack FDT: %d\n", err);
161 return fdt_buf;