[ramips] add patches for v3.8
[openwrt.git] / target / linux / ramips / patches-3.8 / 0207-owrt-MIPS-ralink-add-support-for-runtime-memory-dete.patch
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
5  detection
6
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
9 detected ar runtime.
10
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 ---
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
19
20 diff --git a/arch/mips/include/asm/prom.h b/arch/mips/include/asm/prom.h
21 index a4ad354..91e83e3 100644
22 --- a/arch/mips/include/asm/prom.h
23 +++ b/arch/mips/include/asm/prom.h
24 @@ -20,6 +20,9 @@
25  extern int early_init_dt_scan_memory_arch(unsigned long node,
26         const char *uname, int depth, void *data);
27  
28 +extern int early_init_dt_detect_memory(unsigned long node,
29 +       const char *uname, int depth, void *data);
30 +
31  extern void device_tree_init(void);
32  
33  static inline unsigned long pci_address_to_pio(phys_addr_t address)
34 diff --git a/arch/mips/kernel/prom.c b/arch/mips/kernel/prom.c
35 index 1aa68a2..feac917 100644
36 --- a/arch/mips/kernel/prom.c
37 +++ b/arch/mips/kernel/prom.c
38 @@ -88,6 +88,9 @@ void __init early_init_devtree(void *params)
39         of_scan_flat_dt(early_init_dt_scan_memory_arch, NULL);
40  
41         /* try to load the mips machine name */
42 +       of_scan_flat_dt(early_init_dt_detect_memory, NULL);
43 +
44 +       /* try to load the mips machine name */
45         of_scan_flat_dt(early_init_dt_scan_model, NULL);
46  }
47  
48 diff --git a/arch/mips/ralink/Makefile b/arch/mips/ralink/Makefile
49 index 9e58aa1..800f98b 100644
50 --- a/arch/mips/ralink/Makefile
51 +++ b/arch/mips/ralink/Makefile
52 @@ -6,7 +6,7 @@
53  # Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
54  # Copyright (C) 2013 John Crispin <blogic@openwrt.org>
55  
56 -obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o timer.o
57 +obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o timer.o memory.o
58  
59  obj-$(CONFIG_SOC_RT288X) += rt288x.o
60  obj-$(CONFIG_SOC_RT305X) += rt305x.o rt305x-usb.o
61 diff --git a/arch/mips/ralink/memory.c b/arch/mips/ralink/memory.c
62 new file mode 100644
63 index 0000000..54da31d
64 --- /dev/null
65 +++ b/arch/mips/ralink/memory.c
66 @@ -0,0 +1,119 @@
67 +/*
68 + *  This program is free software; you can redistribute it and/or modify it
69 + *  under the terms of the GNU General Public License version 2 as published
70 + *  by the Free Software Foundation.
71 + *
72 + *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
73 + *  Copyright (C) 2013 John Crispin <blogic@openwrt.org>
74 + */
75 +
76 +#include <linux/string.h>
77 +#include <linux/of_fdt.h>
78 +#include <linux/of_platform.h>
79 +
80 +#include <asm/bootinfo.h>
81 +#include <asm/addrspace.h>
82 +
83 +#include "common.h"
84 +
85 +#define MB     (1024 * 1024)
86 +
87 +unsigned long ramips_mem_base;
88 +unsigned long ramips_mem_size_min;
89 +unsigned long ramips_mem_size_max;
90 +
91 +#ifdef CONFIG_SOC_RT305X
92 +
93 +#include <asm/mach-ralink/rt305x.h>
94 +
95 +static unsigned long rt5350_get_mem_size(void)
96 +{
97 +       void __iomem *sysc = (void __iomem *) KSEG1ADDR(RT305X_SYSC_BASE);
98 +       unsigned long ret;
99 +       u32 t;
100 +
101 +       t = __raw_readl(sysc + SYSC_REG_SYSTEM_CONFIG);
102 +       t = (t >> RT5350_SYSCFG0_DRAM_SIZE_SHIFT) &
103 +       RT5350_SYSCFG0_DRAM_SIZE_MASK;
104 +
105 +       switch (t) {
106 +       case RT5350_SYSCFG0_DRAM_SIZE_2M:
107 +               ret = 2 * 1024 * 1024;
108 +               break;
109 +       case RT5350_SYSCFG0_DRAM_SIZE_8M:
110 +               ret = 8 * 1024 * 1024;
111 +               break;
112 +       case RT5350_SYSCFG0_DRAM_SIZE_16M:
113 +               ret = 16 * 1024 * 1024;
114 +               break;
115 +       case RT5350_SYSCFG0_DRAM_SIZE_32M:
116 +               ret = 32 * 1024 * 1024;
117 +               break;
118 +       case RT5350_SYSCFG0_DRAM_SIZE_64M:
119 +               ret = 64 * 1024 * 1024;
120 +               break;
121 +       default:
122 +               panic("rt5350: invalid DRAM size: %u", t);
123 +               break;
124 +       }
125 +
126 +       return ret;
127 +}
128 +
129 +#endif
130 +
131 +static void __init detect_mem_size(void)
132 +{
133 +       unsigned long size;
134 +
135 +#ifdef CONFIG_SOC_RT305X
136 +       if (soc_is_rt5350()) {
137 +               size = rt5350_get_mem_size();
138 +       } else
139 +#endif
140 +       {
141 +               void *base;
142 +
143 +               base = (void *) KSEG1ADDR(detect_mem_size);
144 +               for (size = ramips_mem_size_min; size < ramips_mem_size_max;
145 +                    size <<= 1 ) {
146 +                       if (!memcmp(base, base + size, 1024))
147 +                               break;
148 +               }
149 +       }
150 +
151 +       pr_info("memory detected: %uMB\n", (unsigned int) size / MB);
152 +
153 +       add_memory_region(ramips_mem_base, size, BOOT_MEM_RAM);
154 +}
155 +
156 +int __init early_init_dt_detect_memory(unsigned long node, const char *uname,
157 +                                    int depth, void *data)
158 +{
159 +       unsigned long l;
160 +       __be32 *mem;
161 +
162 +       /* We are scanning "memorydetect" nodes only */
163 +       if (depth != 1 || strcmp(uname, "memorydetect") != 0)
164 +               return 0;
165 +
166 +       mem = of_get_flat_dt_prop(node, "ralink,memory", &l);
167 +       if (mem == NULL)
168 +               return 0;
169 +
170 +       if ((l / sizeof(__be32)) != 3)
171 +               panic("invalid memorydetect node\n");
172 +
173 +       ramips_mem_base = dt_mem_next_cell(dt_root_addr_cells, &mem);
174 +       ramips_mem_size_min = dt_mem_next_cell(dt_root_size_cells, &mem);
175 +       ramips_mem_size_max = dt_mem_next_cell(dt_root_size_cells, &mem);
176 +
177 +       pr_info("memory window: 0x%llx, min: %uMB, max: %uMB\n",
178 +               (unsigned long long) ramips_mem_base,
179 +               (unsigned int) ramips_mem_size_min / MB,
180 +               (unsigned int) ramips_mem_size_max / MB);
181 +
182 +       detect_mem_size();
183 +
184 +       return 0;
185 +}
186 -- 
187 1.7.10.4
188