1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License.  See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * PROM library functions for acquiring/using memory descriptors given to
7 * us from the YAMON.
8 *
9 * Copyright (C) 1999,2000,2012  MIPS Technologies, Inc.
10 * All rights reserved.
11 * Authors: Carsten Langgaard <carstenl@mips.com>
12 *          Steven J. Hill <sjhill@mips.com>
13 */
14#include <linux/init.h>
15#include <linux/bootmem.h>
16#include <linux/string.h>
17
18#include <asm/bootinfo.h>
19#include <asm/cdmm.h>
20#include <asm/maar.h>
21#include <asm/sections.h>
22#include <asm/fw/fw.h>
23
24static fw_memblock_t mdesc[FW_MAX_MEMBLOCKS];
25
26/* determined physical memory size, not overridden by command line args	 */
27unsigned long physical_memsize = 0L;
28
29fw_memblock_t * __init fw_getmdesc(int eva)
30{
31	char *memsize_str, *ememsize_str = NULL, *ptr;
32	unsigned long memsize = 0, ememsize = 0;
33	static char cmdline[COMMAND_LINE_SIZE] __initdata;
34	int tmp;
35
36	/* otherwise look in the environment */
37
38	memsize_str = fw_getenv("memsize");
39	if (memsize_str) {
40		tmp = kstrtoul(memsize_str, 0, &memsize);
41		if (tmp)
42			pr_warn("Failed to read the 'memsize' env variable.\n");
43	}
44	if (eva) {
45	/* Look for ememsize for EVA */
46		ememsize_str = fw_getenv("ememsize");
47		if (ememsize_str) {
48			tmp = kstrtoul(ememsize_str, 0, &ememsize);
49			if (tmp)
50				pr_warn("Failed to read the 'ememsize' env variable.\n");
51		}
52	}
53	if (!memsize && !ememsize) {
54		pr_warn("memsize not set in YAMON, set to default (32Mb)\n");
55		physical_memsize = 0x02000000;
56	} else {
57		if (memsize > (256 << 20)) { /* memsize should be capped to 256M */
58			pr_warn("Unsupported memsize value (0x%lx) detected! "
59				"Using 0x10000000 (256M) instead\n",
60				memsize);
61			memsize = 256 << 20;
62		}
63		/* If ememsize is set, then set physical_memsize to that */
64		physical_memsize = ememsize ? : memsize;
65	}
66
67#ifdef CONFIG_CPU_BIG_ENDIAN
68	/* SOC-it swaps, or perhaps doesn't swap, when DMA'ing the last
69	   word of physical memory */
70	physical_memsize -= PAGE_SIZE;
71#endif
72
73	/* Check the command line for a memsize directive that overrides
74	   the physical/default amount */
75	strcpy(cmdline, arcs_cmdline);
76	ptr = strstr(cmdline, "memsize=");
77	if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
78		ptr = strstr(ptr, " memsize=");
79	/* And now look for ememsize */
80	if (eva) {
81		ptr = strstr(cmdline, "ememsize=");
82		if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
83			ptr = strstr(ptr, " ememsize=");
84	}
85
86	if (ptr)
87		memsize = memparse(ptr + 8 + (eva ? 1 : 0), &ptr);
88	else
89		memsize = physical_memsize;
90
91	/* Last 64K for HIGHMEM arithmetics */
92	if (memsize > 0x7fff0000)
93		memsize = 0x7fff0000;
94
95	memset(mdesc, 0, sizeof(mdesc));
96
97	mdesc[0].type = fw_dontuse;
98	mdesc[0].base = PHYS_OFFSET;
99	mdesc[0].size = 0x00001000;
100
101	mdesc[1].type = fw_code;
102	mdesc[1].base = mdesc[0].base + 0x00001000UL;
103	mdesc[1].size = 0x000ef000;
104
105	/*
106	 * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
107	 * south bridge and PCI access always forwarded to the ISA Bus and
108	 * BIOSCS# is always generated.
109	 * This mean that this area can't be used as DMA memory for PCI
110	 * devices.
111	 */
112	mdesc[2].type = fw_dontuse;
113	mdesc[2].base = mdesc[0].base + 0x000f0000UL;
114	mdesc[2].size = 0x00010000;
115
116	mdesc[3].type = fw_dontuse;
117	mdesc[3].base = mdesc[0].base + 0x00100000UL;
118	mdesc[3].size = CPHYSADDR(PFN_ALIGN((unsigned long)&_end)) -
119		0x00100000UL;
120
121	mdesc[4].type = fw_free;
122	mdesc[4].base = mdesc[0].base + CPHYSADDR(PFN_ALIGN(&_end));
123	mdesc[4].size = memsize - CPHYSADDR(mdesc[4].base);
124
125	return &mdesc[0];
126}
127
128static void free_init_pages_eva_malta(void *begin, void *end)
129{
130	free_init_pages("unused kernel", __pa_symbol((unsigned long *)begin),
131			__pa_symbol((unsigned long *)end));
132}
133
134static int __init fw_memtype_classify(unsigned int type)
135{
136	switch (type) {
137	case fw_free:
138		return BOOT_MEM_RAM;
139	case fw_code:
140		return BOOT_MEM_ROM_DATA;
141	default:
142		return BOOT_MEM_RESERVED;
143	}
144}
145
146void __init fw_meminit(void)
147{
148	fw_memblock_t *p;
149
150	p = fw_getmdesc(config_enabled(CONFIG_EVA));
151	free_init_pages_eva = (config_enabled(CONFIG_EVA) ?
152			       free_init_pages_eva_malta : NULL);
153
154	while (p->size) {
155		long type;
156		unsigned long base, size;
157
158		type = fw_memtype_classify(p->type);
159		base = p->base;
160		size = p->size;
161
162		add_memory_region(base, size, type);
163		p++;
164	}
165}
166
167void __init prom_free_prom_memory(void)
168{
169	unsigned long addr;
170	int i;
171
172	for (i = 0; i < boot_mem_map.nr_map; i++) {
173		if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
174			continue;
175
176		addr = boot_mem_map.map[i].addr;
177		free_init_pages("YAMON memory",
178				addr, addr + boot_mem_map.map[i].size);
179	}
180}
181
182unsigned platform_maar_init(unsigned num_pairs)
183{
184	phys_addr_t mem_end = (physical_memsize & ~0xffffull) - 1;
185	struct maar_config cfg[] = {
186		/* DRAM preceding I/O */
187		{ 0x00000000, 0x0fffffff, MIPS_MAAR_S },
188
189		/* DRAM following I/O */
190		{ 0x20000000, mem_end, MIPS_MAAR_S },
191
192		/* DRAM alias in upper half of physical */
193		{ 0x80000000, 0x80000000 + mem_end, MIPS_MAAR_S },
194	};
195	unsigned i, num_cfg = ARRAY_SIZE(cfg);
196
197	/* If DRAM fits before I/O, drop the region following it */
198	if (physical_memsize <= 0x10000000) {
199		num_cfg--;
200		for (i = 1; i < num_cfg; i++)
201			cfg[i] = cfg[i + 1];
202	}
203
204	return maar_config(cfg, num_cfg, num_pairs);
205}
206
207phys_addr_t mips_cdmm_phys_base(void)
208{
209	/* This address is "typically unused" */
210	return 0x1fc10000;
211}
212