1 From bcd97dbdcb7bc0300397db481872252e8849307b Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 25 Mar 2013 10:50:53 +0100
4 Subject: [PATCH 207/208] owrt: MIPS: ralink: add support for runtime memory
7 This allows us to add a device_node called "memorydetect" to the DT with
8 information about the memory windoe of the SoC. Based on this the memory is
11 Signed-off-by: John Crispin <blogic@openwrt.org>
13 arch/mips/include/asm/prom.h | 3 ++
14 arch/mips/kernel/prom.c | 3 ++
15 arch/mips/ralink/Makefile | 2 +-
16 arch/mips/ralink/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++
17 4 files changed, 126 insertions(+), 1 deletion(-)
18 create mode 100644 arch/mips/ralink/memory.c
20 --- a/arch/mips/include/asm/prom.h
21 +++ b/arch/mips/include/asm/prom.h
23 extern int early_init_dt_scan_memory_arch(unsigned long node,
24 const char *uname, int depth, void *data);
26 +extern int early_init_dt_detect_memory(unsigned long node,
27 + const char *uname, int depth, void *data);
29 extern void device_tree_init(void);
31 static inline unsigned long pci_address_to_pio(phys_addr_t address)
32 --- a/arch/mips/kernel/prom.c
33 +++ b/arch/mips/kernel/prom.c
34 @@ -88,6 +88,9 @@ void __init early_init_devtree(void *par
35 of_scan_flat_dt(early_init_dt_scan_memory_arch, NULL);
37 /* try to load the mips machine name */
38 + of_scan_flat_dt(early_init_dt_detect_memory, NULL);
40 + /* try to load the mips machine name */
41 of_scan_flat_dt(early_init_dt_scan_model, NULL);
44 --- a/arch/mips/ralink/Makefile
45 +++ b/arch/mips/ralink/Makefile
47 # Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
48 # Copyright (C) 2013 John Crispin <blogic@openwrt.org>
50 -obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o timer.o
51 +obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o timer.o memory.o
53 obj-$(CONFIG_SOC_RT288X) += rt288x.o
54 obj-$(CONFIG_SOC_RT305X) += rt305x.o rt305x-usb.o
56 +++ b/arch/mips/ralink/memory.c
59 + * This program is free software; you can redistribute it and/or modify it
60 + * under the terms of the GNU General Public License version 2 as published
61 + * by the Free Software Foundation.
63 + * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
64 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
67 +#include <linux/string.h>
68 +#include <linux/of_fdt.h>
69 +#include <linux/of_platform.h>
71 +#include <asm/bootinfo.h>
72 +#include <asm/addrspace.h>
76 +#define MB (1024 * 1024)
78 +unsigned long ramips_mem_base;
79 +unsigned long ramips_mem_size_min;
80 +unsigned long ramips_mem_size_max;
82 +#ifdef CONFIG_SOC_RT305X
84 +#include <asm/mach-ralink/rt305x.h>
86 +static unsigned long rt5350_get_mem_size(void)
88 + void __iomem *sysc = (void __iomem *) KSEG1ADDR(RT305X_SYSC_BASE);
92 + t = __raw_readl(sysc + SYSC_REG_SYSTEM_CONFIG);
93 + t = (t >> RT5350_SYSCFG0_DRAM_SIZE_SHIFT) &
94 + RT5350_SYSCFG0_DRAM_SIZE_MASK;
97 + case RT5350_SYSCFG0_DRAM_SIZE_2M:
98 + ret = 2 * 1024 * 1024;
100 + case RT5350_SYSCFG0_DRAM_SIZE_8M:
101 + ret = 8 * 1024 * 1024;
103 + case RT5350_SYSCFG0_DRAM_SIZE_16M:
104 + ret = 16 * 1024 * 1024;
106 + case RT5350_SYSCFG0_DRAM_SIZE_32M:
107 + ret = 32 * 1024 * 1024;
109 + case RT5350_SYSCFG0_DRAM_SIZE_64M:
110 + ret = 64 * 1024 * 1024;
113 + panic("rt5350: invalid DRAM size: %u", t);
122 +static void __init detect_mem_size(void)
124 + unsigned long size;
126 +#ifdef CONFIG_SOC_RT305X
127 + if (soc_is_rt5350()) {
128 + size = rt5350_get_mem_size();
134 + base = (void *) KSEG1ADDR(detect_mem_size);
135 + for (size = ramips_mem_size_min; size < ramips_mem_size_max;
137 + if (!memcmp(base, base + size, 1024))
142 + pr_info("memory detected: %uMB\n", (unsigned int) size / MB);
144 + add_memory_region(ramips_mem_base, size, BOOT_MEM_RAM);
147 +int __init early_init_dt_detect_memory(unsigned long node, const char *uname,
148 + int depth, void *data)
153 + /* We are scanning "memorydetect" nodes only */
154 + if (depth != 1 || strcmp(uname, "memorydetect") != 0)
157 + mem = of_get_flat_dt_prop(node, "ralink,memory", &l);
161 + if ((l / sizeof(__be32)) != 3)
162 + panic("invalid memorydetect node\n");
164 + ramips_mem_base = dt_mem_next_cell(dt_root_addr_cells, &mem);
165 + ramips_mem_size_min = dt_mem_next_cell(dt_root_size_cells, &mem);
166 + ramips_mem_size_max = dt_mem_next_cell(dt_root_size_cells, &mem);
168 + pr_info("memory window: 0x%llx, min: %uMB, max: %uMB\n",
169 + (unsigned long long) ramips_mem_base,
170 + (unsigned int) ramips_mem_size_min / MB,
171 + (unsigned int) ramips_mem_size_max / MB);