2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/initrd.h>
12 #include <linux/sched.h>
13 #include <linux/console.h>
14 #include <linux/ioport.h>
15 #include <linux/bootmem.h>
17 #include <linux/module.h>
18 #include <linux/pfn.h>
19 #include <linux/root_dev.h>
20 #include <linux/cpu.h>
21 #include <linux/kernel.h>
23 #include <asm/sections.h>
24 #include <asm/processor.h>
25 #include <asm/pgtable.h>
26 #include <asm/setup.h>
27 #include <asm/sysreg.h>
29 #include <mach/board.h>
30 #include <mach/init.h>
32 extern int root_mountflags
;
35 * Initialize loops_per_jiffy as 5000000 (500MIPS).
36 * Better make it too large than too small...
38 struct avr32_cpuinfo boot_cpu_data
= {
39 .loops_per_jiffy
= 5000000
41 EXPORT_SYMBOL(boot_cpu_data
);
43 static char __initdata command_line
[COMMAND_LINE_SIZE
];
46 * Standard memory resources
48 static struct resource __initdata kernel_data
= {
49 .name
= "Kernel data",
52 .flags
= IORESOURCE_MEM
,
54 static struct resource __initdata kernel_code
= {
55 .name
= "Kernel code",
58 .flags
= IORESOURCE_MEM
,
59 .sibling
= &kernel_data
,
63 * Available system RAM and reserved regions as singly linked
64 * lists. These lists are traversed using the sibling pointer in
65 * struct resource and are kept sorted at all times.
67 static struct resource
*__initdata system_ram
;
68 static struct resource
*__initdata reserved
= &kernel_code
;
71 * We need to allocate these before the bootmem allocator is up and
72 * running, so we need this "cache". 32 entries are probably enough
73 * for all but the most insanely complex systems.
75 static struct resource __initdata res_cache
[32];
76 static unsigned int __initdata res_cache_next_free
;
78 static void __init
resource_init(void)
80 struct resource
*mem
, *res
;
83 kernel_code
.start
= __pa(init_mm
.start_code
);
85 for (mem
= system_ram
; mem
; mem
= mem
->sibling
) {
86 new = alloc_bootmem_low(sizeof(struct resource
));
87 memcpy(new, mem
, sizeof(struct resource
));
90 if (request_resource(&iomem_resource
, new))
91 printk(KERN_WARNING
"Bad RAM resource %08x-%08x\n",
92 mem
->start
, mem
->end
);
95 for (res
= reserved
; res
; res
= res
->sibling
) {
96 new = alloc_bootmem_low(sizeof(struct resource
));
97 memcpy(new, res
, sizeof(struct resource
));
100 if (insert_resource(&iomem_resource
, new))
102 "Bad reserved resource %s (%08x-%08x)\n",
103 res
->name
, res
->start
, res
->end
);
108 add_physical_memory(resource_size_t start
, resource_size_t end
)
110 struct resource
*new, *next
, **pprev
;
112 for (pprev
= &system_ram
, next
= system_ram
; next
;
113 pprev
= &next
->sibling
, next
= next
->sibling
) {
114 if (end
< next
->start
)
116 if (start
<= next
->end
) {
118 "Warning: Physical memory map is broken\n");
120 "Warning: %08x-%08x overlaps %08x-%08x\n",
121 start
, end
, next
->start
, next
->end
);
126 if (res_cache_next_free
>= ARRAY_SIZE(res_cache
)) {
128 "Warning: Failed to add physical memory %08x-%08x\n",
133 new = &res_cache
[res_cache_next_free
++];
136 new->name
= "System RAM";
137 new->flags
= IORESOURCE_MEM
;
143 add_reserved_region(resource_size_t start
, resource_size_t end
,
146 struct resource
*new, *next
, **pprev
;
151 if (res_cache_next_free
>= ARRAY_SIZE(res_cache
))
154 for (pprev
= &reserved
, next
= reserved
; next
;
155 pprev
= &next
->sibling
, next
= next
->sibling
) {
156 if (end
< next
->start
)
158 if (start
<= next
->end
)
162 new = &res_cache
[res_cache_next_free
++];
167 new->flags
= IORESOURCE_MEM
;
174 static unsigned long __init
175 find_free_region(const struct resource
*mem
, resource_size_t size
,
176 resource_size_t align
)
178 struct resource
*res
;
179 unsigned long target
;
181 target
= ALIGN(mem
->start
, align
);
182 for (res
= reserved
; res
; res
= res
->sibling
) {
183 if ((target
+ size
) <= res
->start
)
185 if (target
<= res
->end
)
186 target
= ALIGN(res
->end
+ 1, align
);
189 if ((target
+ size
) > (mem
->end
+ 1))
196 alloc_reserved_region(resource_size_t
*start
, resource_size_t size
,
197 resource_size_t align
, const char *name
)
199 struct resource
*mem
;
200 resource_size_t target
;
203 for (mem
= system_ram
; mem
; mem
= mem
->sibling
) {
204 target
= find_free_region(mem
, size
, align
);
205 if (target
<= mem
->end
) {
206 ret
= add_reserved_region(target
, target
+ size
- 1,
218 * Early framebuffer allocation. Works as follows:
219 * - If fbmem_size is zero, nothing will be allocated or reserved.
220 * - If fbmem_start is zero when setup_bootmem() is called,
221 * a block of fbmem_size bytes will be reserved before bootmem
222 * initialization. It will be aligned to the largest page size
223 * that fbmem_size is a multiple of.
224 * - If fbmem_start is nonzero, an area of size fbmem_size will be
225 * reserved at the physical address fbmem_start if possible. If
226 * it collides with other reserved memory, a different block of
227 * same size will be allocated, just as if fbmem_start was zero.
229 * Board-specific code may use these variables to set up platform data
230 * for the framebuffer driver if fbmem_size is nonzero.
232 resource_size_t __initdata fbmem_start
;
233 resource_size_t __initdata fbmem_size
;
236 * "fbmem=xxx[kKmM]" allocates the specified amount of boot memory for
237 * use as framebuffer.
239 * "fbmem=xxx[kKmM]@yyy[kKmM]" defines a memory region of size xxx and
240 * starting at yyy to be reserved for use as framebuffer.
242 * The kernel won't verify that the memory region starting at yyy
243 * actually contains usable RAM.
245 static int __init
early_parse_fbmem(char *p
)
250 fbmem_size
= memparse(p
, &p
);
252 fbmem_start
= memparse(p
+ 1, &p
);
253 ret
= add_reserved_region(fbmem_start
,
254 fbmem_start
+ fbmem_size
- 1,
258 "Failed to reserve framebuffer memory\n");
264 if ((fbmem_size
& 0x000fffffUL
) == 0)
265 align
= 0x100000; /* 1 MiB */
266 else if ((fbmem_size
& 0x0000ffffUL
) == 0)
267 align
= 0x10000; /* 64 KiB */
269 align
= 0x1000; /* 4 KiB */
271 ret
= alloc_reserved_region(&fbmem_start
, fbmem_size
,
272 align
, "Framebuffer");
275 "Failed to allocate framebuffer memory\n");
278 memset(__va(fbmem_start
), 0, fbmem_size
);
284 early_param("fbmem", early_parse_fbmem
);
287 * Pick out the memory size. We look for mem=size@start,
288 * where start and size are "size[KkMmGg]"
290 static int __init
early_mem(char *p
)
292 resource_size_t size
, start
;
294 start
= system_ram
->start
;
295 size
= memparse(p
, &p
);
297 start
= memparse(p
+ 1, &p
);
299 system_ram
->start
= start
;
300 system_ram
->end
= system_ram
->start
+ size
- 1;
303 early_param("mem", early_mem
);
305 static int __init
parse_tag_core(struct tag
*tag
)
307 if (tag
->hdr
.size
> 2) {
308 if ((tag
->u
.core
.flags
& 1) == 0)
309 root_mountflags
&= ~MS_RDONLY
;
310 ROOT_DEV
= new_decode_dev(tag
->u
.core
.rootdev
);
314 __tagtable(ATAG_CORE
, parse_tag_core
);
316 static int __init
parse_tag_mem(struct tag
*tag
)
318 unsigned long start
, end
;
321 * Ignore zero-sized entries. If we're running standalone, the
322 * SDRAM code may emit such entries if something goes
325 if (tag
->u
.mem_range
.size
== 0)
328 start
= tag
->u
.mem_range
.addr
;
329 end
= tag
->u
.mem_range
.addr
+ tag
->u
.mem_range
.size
- 1;
331 add_physical_memory(start
, end
);
334 __tagtable(ATAG_MEM
, parse_tag_mem
);
336 static int __init
parse_tag_rdimg(struct tag
*tag
)
338 #ifdef CONFIG_BLK_DEV_INITRD
339 struct tag_mem_range
*mem
= &tag
->u
.mem_range
;
344 "Warning: Only the first initrd image will be used\n");
348 ret
= add_reserved_region(mem
->addr
, mem
->addr
+ mem
->size
- 1,
352 "Warning: Failed to reserve initrd memory\n");
356 initrd_start
= (unsigned long)__va(mem
->addr
);
357 initrd_end
= initrd_start
+ mem
->size
;
359 printk(KERN_WARNING
"RAM disk image present, but "
360 "no initrd support in kernel, ignoring\n");
365 __tagtable(ATAG_RDIMG
, parse_tag_rdimg
);
367 static int __init
parse_tag_rsvd_mem(struct tag
*tag
)
369 struct tag_mem_range
*mem
= &tag
->u
.mem_range
;
371 return add_reserved_region(mem
->addr
, mem
->addr
+ mem
->size
- 1,
374 __tagtable(ATAG_RSVD_MEM
, parse_tag_rsvd_mem
);
376 static int __init
parse_tag_cmdline(struct tag
*tag
)
378 strlcpy(boot_command_line
, tag
->u
.cmdline
.cmdline
, COMMAND_LINE_SIZE
);
381 __tagtable(ATAG_CMDLINE
, parse_tag_cmdline
);
383 static int __init
parse_tag_clock(struct tag
*tag
)
386 * We'll figure out the clocks by peeking at the system
387 * manager regs directly.
391 __tagtable(ATAG_CLOCK
, parse_tag_clock
);
394 * Scan the tag table for this tag, and call its parse function. The
395 * tag table is built by the linker from all the __tagtable
398 static int __init
parse_tag(struct tag
*tag
)
400 extern struct tagtable __tagtable_begin
, __tagtable_end
;
403 for (t
= &__tagtable_begin
; t
< &__tagtable_end
; t
++)
404 if (tag
->hdr
.tag
== t
->tag
) {
409 return t
< &__tagtable_end
;
413 * Parse all tags in the list we got from the boot loader
415 static void __init
parse_tags(struct tag
*t
)
417 for (; t
->hdr
.tag
!= ATAG_NONE
; t
= tag_next(t
))
420 "Ignoring unrecognised tag 0x%08x\n",
425 * Find a free memory region large enough for storing the
428 static unsigned long __init
429 find_bootmap_pfn(const struct resource
*mem
)
431 unsigned long bootmap_pages
, bootmap_len
;
432 unsigned long node_pages
= PFN_UP(mem
->end
- mem
->start
+ 1);
433 unsigned long bootmap_start
;
435 bootmap_pages
= bootmem_bootmap_pages(node_pages
);
436 bootmap_len
= bootmap_pages
<< PAGE_SHIFT
;
439 * Find a large enough region without reserved pages for
440 * storing the bootmem bitmap. We can take advantage of the
441 * fact that all lists have been sorted.
443 * We have to check that we don't collide with any reserved
444 * regions, which includes the kernel image and any RAMDISK
447 bootmap_start
= find_free_region(mem
, bootmap_len
, PAGE_SIZE
);
449 return bootmap_start
>> PAGE_SHIFT
;
452 #define MAX_LOWMEM HIGHMEM_START
453 #define MAX_LOWMEM_PFN PFN_DOWN(MAX_LOWMEM)
455 static void __init
setup_bootmem(void)
457 unsigned bootmap_size
;
458 unsigned long first_pfn
, bootmap_pfn
, pages
;
459 unsigned long max_pfn
, max_low_pfn
;
461 struct resource
*res
;
463 printk(KERN_INFO
"Physical memory:\n");
464 for (res
= system_ram
; res
; res
= res
->sibling
)
465 printk(" %08x-%08x\n", res
->start
, res
->end
);
466 printk(KERN_INFO
"Reserved memory:\n");
467 for (res
= reserved
; res
; res
= res
->sibling
)
468 printk(" %08x-%08x: %s\n",
469 res
->start
, res
->end
, res
->name
);
471 nodes_clear(node_online_map
);
473 if (system_ram
->sibling
)
474 printk(KERN_WARNING
"Only using first memory bank\n");
476 for (res
= system_ram
; res
; res
= NULL
) {
477 first_pfn
= PFN_UP(res
->start
);
478 max_low_pfn
= max_pfn
= PFN_DOWN(res
->end
+ 1);
479 bootmap_pfn
= find_bootmap_pfn(res
);
480 if (bootmap_pfn
> max_pfn
)
481 panic("No space for bootmem bitmap!\n");
483 if (max_low_pfn
> MAX_LOWMEM_PFN
) {
484 max_low_pfn
= MAX_LOWMEM_PFN
;
485 #ifndef CONFIG_HIGHMEM
487 * Lowmem is memory that can be addressed
488 * directly through P1/P2
491 "Node %u: Only %ld MiB of memory will be used.\n",
492 node
, MAX_LOWMEM
>> 20);
493 printk(KERN_WARNING
"Use a HIGHMEM enabled kernel.\n");
495 #error HIGHMEM is not supported by AVR32 yet
499 /* Initialize the boot-time allocator with low memory only. */
500 bootmap_size
= init_bootmem_node(NODE_DATA(node
), bootmap_pfn
,
501 first_pfn
, max_low_pfn
);
504 * Register fully available RAM pages with the bootmem
507 pages
= max_low_pfn
- first_pfn
;
508 free_bootmem_node (NODE_DATA(node
), PFN_PHYS(first_pfn
),
511 /* Reserve space for the bootmem bitmap... */
512 reserve_bootmem_node(NODE_DATA(node
),
513 PFN_PHYS(bootmap_pfn
),
517 /* ...and any other reserved regions. */
518 for (res
= reserved
; res
; res
= res
->sibling
) {
519 if (res
->start
> PFN_PHYS(max_pfn
))
523 * resource_init will complain about partial
524 * overlaps, so we'll just ignore such
527 if (res
->start
>= PFN_PHYS(first_pfn
)
528 && res
->end
< PFN_PHYS(max_pfn
))
529 reserve_bootmem_node(
530 NODE_DATA(node
), res
->start
,
531 res
->end
- res
->start
+ 1,
535 node_set_online(node
);
539 void __init
setup_arch (char **cmdline_p
)
543 init_mm
.start_code
= (unsigned long)_text
;
544 init_mm
.end_code
= (unsigned long)_etext
;
545 init_mm
.end_data
= (unsigned long)_edata
;
546 init_mm
.brk
= (unsigned long)_end
;
549 * Include .init section to make allocations easier. It will
550 * be removed before the resource is actually requested.
552 kernel_code
.start
= __pa(__init_begin
);
553 kernel_code
.end
= __pa(init_mm
.end_code
- 1);
554 kernel_data
.start
= __pa(init_mm
.end_code
);
555 kernel_data
.end
= __pa(init_mm
.brk
- 1);
557 parse_tags(bootloader_tags
);
563 cpu_clk
= clk_get(NULL
, "cpu");
564 if (IS_ERR(cpu_clk
)) {
565 printk(KERN_WARNING
"Warning: Unable to get CPU clock\n");
567 unsigned long cpu_hz
= clk_get_rate(cpu_clk
);
570 * Well, duh, but it's probably a good idea to
571 * increment the use count.
575 boot_cpu_data
.clk
= cpu_clk
;
576 boot_cpu_data
.loops_per_jiffy
= cpu_hz
* 4;
577 printk("CPU: Running at %lu.%03lu MHz\n",
578 ((cpu_hz
+ 500) / 1000) / 1000,
579 ((cpu_hz
+ 500) / 1000) % 1000);
582 strlcpy(command_line
, boot_command_line
, COMMAND_LINE_SIZE
);
583 *cmdline_p
= command_line
;
589 conswitchp
= &dummy_con
;