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