MIPS: FW: malta: Use new common FW library variable processing.
[firefly-linux-kernel-4.4.55.git] / arch / mips / mti-malta / malta-memory.c
1 /*
2  * Carsten Langgaard, carstenl@mips.com
3  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17  *
18  * PROM library functions for acquiring/using memory descriptors given to
19  * us from the YAMON.
20  */
21 #include <linux/init.h>
22 #include <linux/mm.h>
23 #include <linux/bootmem.h>
24 #include <linux/pfn.h>
25 #include <linux/string.h>
26
27 #include <asm/bootinfo.h>
28 #include <asm/page.h>
29 #include <asm/sections.h>
30 #include <asm/fw/fw.h>
31
32 #include <asm/mips-boards/prom.h>
33
34 static fw_memblock_t mdesc[FW_MAX_MEMBLOCKS];
35
36 /* determined physical memory size, not overridden by command line args  */
37 unsigned long physical_memsize = 0L;
38
39 fw_memblock_t * __init fw_getmdesc(void)
40 {
41         char *memsize_str;
42         unsigned int memsize;
43         char *ptr;
44         static char cmdline[COMMAND_LINE_SIZE] __initdata;
45
46         /* otherwise look in the environment */
47         memsize_str = fw_getenv("memsize");
48         if (!memsize_str) {
49                 printk(KERN_WARNING
50                        "memsize not set in boot prom, set to default (32Mb)\n");
51                 physical_memsize = 0x02000000;
52         } else {
53                 physical_memsize = simple_strtol(memsize_str, NULL, 0);
54         }
55
56 #ifdef CONFIG_CPU_BIG_ENDIAN
57         /* SOC-it swaps, or perhaps doesn't swap, when DMA'ing the last
58            word of physical memory */
59         physical_memsize -= PAGE_SIZE;
60 #endif
61
62         /* Check the command line for a memsize directive that overrides
63            the physical/default amount */
64         strcpy(cmdline, arcs_cmdline);
65         ptr = strstr(cmdline, "memsize=");
66         if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
67                 ptr = strstr(ptr, " memsize=");
68
69         if (ptr)
70                 memsize = memparse(ptr + 8, &ptr);
71         else
72                 memsize = physical_memsize;
73
74         memset(mdesc, 0, sizeof(mdesc));
75
76         mdesc[0].type = fw_dontuse;
77         mdesc[0].base = 0x00000000;
78         mdesc[0].size = 0x00001000;
79
80         mdesc[1].type = fw_code;
81         mdesc[1].base = 0x00001000;
82         mdesc[1].size = 0x000ef000;
83
84         /*
85          * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
86          * south bridge and PCI access always forwarded to the ISA Bus and
87          * BIOSCS# is always generated.
88          * This mean that this area can't be used as DMA memory for PCI
89          * devices.
90          */
91         mdesc[2].type = fw_dontuse;
92         mdesc[2].base = 0x000f0000;
93         mdesc[2].size = 0x00010000;
94
95         mdesc[3].type = fw_dontuse;
96         mdesc[3].base = 0x00100000;
97         mdesc[3].size = CPHYSADDR(PFN_ALIGN((unsigned long)&_end)) - mdesc[3].base;
98
99         mdesc[4].type = fw_free;
100         mdesc[4].base = CPHYSADDR(PFN_ALIGN(&_end));
101         mdesc[4].size = memsize - mdesc[4].base;
102
103         return &mdesc[0];
104 }
105
106 static int __init fw_memtype_classify(unsigned int type)
107 {
108         switch (type) {
109         case fw_free:
110                 return BOOT_MEM_RAM;
111         case fw_code:
112                 return BOOT_MEM_ROM_DATA;
113         default:
114                 return BOOT_MEM_RESERVED;
115         }
116 }
117
118 void __init fw_meminit(void)
119 {
120         fw_memblock_t *p;
121
122         p = fw_getmdesc();
123
124         while (p->size) {
125                 long type;
126                 unsigned long base, size;
127
128                 type = fw_memtype_classify(p->type);
129                 base = p->base;
130                 size = p->size;
131
132                 add_memory_region(base, size, type);
133                 p++;
134         }
135 }
136
137 void __init prom_free_prom_memory(void)
138 {
139         unsigned long addr;
140         int i;
141
142         for (i = 0; i < boot_mem_map.nr_map; i++) {
143                 if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
144                         continue;
145
146                 addr = boot_mem_map.map[i].addr;
147                 free_init_pages("prom memory",
148                                 addr, addr + boot_mem_map.map[i].size);
149         }
150 }