1/* 2 * Copyright (C) 2004-2006 Atmel Corporation 3 * 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. 7 */ 8 9#include <linux/clk.h> 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> 16#include <linux/fs.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> 22 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> 28 29#include <mach/board.h> 30#include <mach/init.h> 31 32extern int root_mountflags; 33 34/* 35 * Initialize loops_per_jiffy as 5000000 (500MIPS). 36 * Better make it too large than too small... 37 */ 38struct avr32_cpuinfo boot_cpu_data = { 39 .loops_per_jiffy = 5000000 40}; 41EXPORT_SYMBOL(boot_cpu_data); 42 43static char __initdata command_line[COMMAND_LINE_SIZE]; 44 45/* 46 * Standard memory resources 47 */ 48static struct resource __initdata kernel_data = { 49 .name = "Kernel data", 50 .start = 0, 51 .end = 0, 52 .flags = IORESOURCE_MEM, 53}; 54static struct resource __initdata kernel_code = { 55 .name = "Kernel code", 56 .start = 0, 57 .end = 0, 58 .flags = IORESOURCE_MEM, 59 .sibling = &kernel_data, 60}; 61 62/* 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. 66 */ 67static struct resource *__initdata system_ram; 68static struct resource *__initdata reserved = &kernel_code; 69 70/* 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. 74 */ 75static struct resource __initdata res_cache[32]; 76static unsigned int __initdata res_cache_next_free; 77 78static void __init resource_init(void) 79{ 80 struct resource *mem, *res; 81 struct resource *new; 82 83 kernel_code.start = __pa(init_mm.start_code); 84 85 for (mem = system_ram; mem; mem = mem->sibling) { 86 new = alloc_bootmem_low(sizeof(struct resource)); 87 memcpy(new, mem, sizeof(struct resource)); 88 89 new->sibling = NULL; 90 if (request_resource(&iomem_resource, new)) 91 printk(KERN_WARNING "Bad RAM resource %08x-%08x\n", 92 mem->start, mem->end); 93 } 94 95 for (res = reserved; res; res = res->sibling) { 96 new = alloc_bootmem_low(sizeof(struct resource)); 97 memcpy(new, res, sizeof(struct resource)); 98 99 new->sibling = NULL; 100 if (insert_resource(&iomem_resource, new)) 101 printk(KERN_WARNING 102 "Bad reserved resource %s (%08x-%08x)\n", 103 res->name, res->start, res->end); 104 } 105} 106 107static void __init 108add_physical_memory(resource_size_t start, resource_size_t end) 109{ 110 struct resource *new, *next, **pprev; 111 112 for (pprev = &system_ram, next = system_ram; next; 113 pprev = &next->sibling, next = next->sibling) { 114 if (end < next->start) 115 break; 116 if (start <= next->end) { 117 printk(KERN_WARNING 118 "Warning: Physical memory map is broken\n"); 119 printk(KERN_WARNING 120 "Warning: %08x-%08x overlaps %08x-%08x\n", 121 start, end, next->start, next->end); 122 return; 123 } 124 } 125 126 if (res_cache_next_free >= ARRAY_SIZE(res_cache)) { 127 printk(KERN_WARNING 128 "Warning: Failed to add physical memory %08x-%08x\n", 129 start, end); 130 return; 131 } 132 133 new = &res_cache[res_cache_next_free++]; 134 new->start = start; 135 new->end = end; 136 new->name = "System RAM"; 137 new->flags = IORESOURCE_MEM; 138 139 *pprev = new; 140} 141 142static int __init 143add_reserved_region(resource_size_t start, resource_size_t end, 144 const char *name) 145{ 146 struct resource *new, *next, **pprev; 147 148 if (end < start) 149 return -EINVAL; 150 151 if (res_cache_next_free >= ARRAY_SIZE(res_cache)) 152 return -ENOMEM; 153 154 for (pprev = &reserved, next = reserved; next; 155 pprev = &next->sibling, next = next->sibling) { 156 if (end < next->start) 157 break; 158 if (start <= next->end) 159 return -EBUSY; 160 } 161 162 new = &res_cache[res_cache_next_free++]; 163 new->start = start; 164 new->end = end; 165 new->name = name; 166 new->sibling = next; 167 new->flags = IORESOURCE_MEM; 168 169 *pprev = new; 170 171 return 0; 172} 173 174static unsigned long __init 175find_free_region(const struct resource *mem, resource_size_t size, 176 resource_size_t align) 177{ 178 struct resource *res; 179 unsigned long target; 180 181 target = ALIGN(mem->start, align); 182 for (res = reserved; res; res = res->sibling) { 183 if ((target + size) <= res->start) 184 break; 185 if (target <= res->end) 186 target = ALIGN(res->end + 1, align); 187 } 188 189 if ((target + size) > (mem->end + 1)) 190 return mem->end + 1; 191 192 return target; 193} 194 195static int __init 196alloc_reserved_region(resource_size_t *start, resource_size_t size, 197 resource_size_t align, const char *name) 198{ 199 struct resource *mem; 200 resource_size_t target; 201 int ret; 202 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, 207 name); 208 if (!ret) 209 *start = target; 210 return ret; 211 } 212 } 213 214 return -ENOMEM; 215} 216 217/* 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. 228 * 229 * Board-specific code may use these variables to set up platform data 230 * for the framebuffer driver if fbmem_size is nonzero. 231 */ 232resource_size_t __initdata fbmem_start; 233resource_size_t __initdata fbmem_size; 234 235static int __init early_parse_fbmem(char *p) 236{ 237 int ret; 238 unsigned long align; 239 240 fbmem_size = memparse(p, &p); 241 if (*p == '@') { 242 fbmem_start = memparse(p + 1, &p); 243 ret = add_reserved_region(fbmem_start, 244 fbmem_start + fbmem_size - 1, 245 "Framebuffer"); 246 if (ret) { 247 printk(KERN_WARNING 248 "Failed to reserve framebuffer memory\n"); 249 fbmem_start = 0; 250 } 251 } 252 253 if (!fbmem_start) { 254 if ((fbmem_size & 0x000fffffUL) == 0) 255 align = 0x100000; /* 1 MiB */ 256 else if ((fbmem_size & 0x0000ffffUL) == 0) 257 align = 0x10000; /* 64 KiB */ 258 else 259 align = 0x1000; /* 4 KiB */ 260 261 ret = alloc_reserved_region(&fbmem_start, fbmem_size, 262 align, "Framebuffer"); 263 if (ret) { 264 printk(KERN_WARNING 265 "Failed to allocate framebuffer memory\n"); 266 fbmem_size = 0; 267 } else { 268 memset(__va(fbmem_start), 0, fbmem_size); 269 } 270 } 271 272 return 0; 273} 274early_param("fbmem", early_parse_fbmem); 275 276/* 277 * Pick out the memory size. We look for mem=size@start, 278 * where start and size are "size[KkMmGg]" 279 */ 280static int __init early_mem(char *p) 281{ 282 resource_size_t size, start; 283 284 start = system_ram->start; 285 size = memparse(p, &p); 286 if (*p == '@') 287 start = memparse(p + 1, &p); 288 289 system_ram->start = start; 290 system_ram->end = system_ram->start + size - 1; 291 return 0; 292} 293early_param("mem", early_mem); 294 295static int __init parse_tag_core(struct tag *tag) 296{ 297 if (tag->hdr.size > 2) { 298 if ((tag->u.core.flags & 1) == 0) 299 root_mountflags &= ~MS_RDONLY; 300 ROOT_DEV = new_decode_dev(tag->u.core.rootdev); 301 } 302 return 0; 303} 304__tagtable(ATAG_CORE, parse_tag_core); 305 306static int __init parse_tag_mem(struct tag *tag) 307{ 308 unsigned long start, end; 309 310 /* 311 * Ignore zero-sized entries. If we're running standalone, the 312 * SDRAM code may emit such entries if something goes 313 * wrong... 314 */ 315 if (tag->u.mem_range.size == 0) 316 return 0; 317 318 start = tag->u.mem_range.addr; 319 end = tag->u.mem_range.addr + tag->u.mem_range.size - 1; 320 321 add_physical_memory(start, end); 322 return 0; 323} 324__tagtable(ATAG_MEM, parse_tag_mem); 325 326static int __init parse_tag_rdimg(struct tag *tag) 327{ 328#ifdef CONFIG_BLK_DEV_INITRD 329 struct tag_mem_range *mem = &tag->u.mem_range; 330 int ret; 331 332 if (initrd_start) { 333 printk(KERN_WARNING 334 "Warning: Only the first initrd image will be used\n"); 335 return 0; 336 } 337 338 ret = add_reserved_region(mem->addr, mem->addr + mem->size - 1, 339 "initrd"); 340 if (ret) { 341 printk(KERN_WARNING 342 "Warning: Failed to reserve initrd memory\n"); 343 return ret; 344 } 345 346 initrd_start = (unsigned long)__va(mem->addr); 347 initrd_end = initrd_start + mem->size; 348#else 349 printk(KERN_WARNING "RAM disk image present, but " 350 "no initrd support in kernel, ignoring\n"); 351#endif 352 353 return 0; 354} 355__tagtable(ATAG_RDIMG, parse_tag_rdimg); 356 357static int __init parse_tag_rsvd_mem(struct tag *tag) 358{ 359 struct tag_mem_range *mem = &tag->u.mem_range; 360 361 return add_reserved_region(mem->addr, mem->addr + mem->size - 1, 362 "Reserved"); 363} 364__tagtable(ATAG_RSVD_MEM, parse_tag_rsvd_mem); 365 366static int __init parse_tag_cmdline(struct tag *tag) 367{ 368 strlcpy(boot_command_line, tag->u.cmdline.cmdline, COMMAND_LINE_SIZE); 369 return 0; 370} 371__tagtable(ATAG_CMDLINE, parse_tag_cmdline); 372 373static int __init parse_tag_clock(struct tag *tag) 374{ 375 /* 376 * We'll figure out the clocks by peeking at the system 377 * manager regs directly. 378 */ 379 return 0; 380} 381__tagtable(ATAG_CLOCK, parse_tag_clock); 382 383/* 384 * Scan the tag table for this tag, and call its parse function. The 385 * tag table is built by the linker from all the __tagtable 386 * declarations. 387 */ 388static int __init parse_tag(struct tag *tag) 389{ 390 extern struct tagtable __tagtable_begin, __tagtable_end; 391 struct tagtable *t; 392 393 for (t = &__tagtable_begin; t < &__tagtable_end; t++) 394 if (tag->hdr.tag == t->tag) { 395 t->parse(tag); 396 break; 397 } 398 399 return t < &__tagtable_end; 400} 401 402/* 403 * Parse all tags in the list we got from the boot loader 404 */ 405static void __init parse_tags(struct tag *t) 406{ 407 for (; t->hdr.tag != ATAG_NONE; t = tag_next(t)) 408 if (!parse_tag(t)) 409 printk(KERN_WARNING 410 "Ignoring unrecognised tag 0x%08x\n", 411 t->hdr.tag); 412} 413 414/* 415 * Find a free memory region large enough for storing the 416 * bootmem bitmap. 417 */ 418static unsigned long __init 419find_bootmap_pfn(const struct resource *mem) 420{ 421 unsigned long bootmap_pages, bootmap_len; 422 unsigned long node_pages = PFN_UP(mem->end - mem->start + 1); 423 unsigned long bootmap_start; 424 425 bootmap_pages = bootmem_bootmap_pages(node_pages); 426 bootmap_len = bootmap_pages << PAGE_SHIFT; 427 428 /* 429 * Find a large enough region without reserved pages for 430 * storing the bootmem bitmap. We can take advantage of the 431 * fact that all lists have been sorted. 432 * 433 * We have to check that we don't collide with any reserved 434 * regions, which includes the kernel image and any RAMDISK 435 * images. 436 */ 437 bootmap_start = find_free_region(mem, bootmap_len, PAGE_SIZE); 438 439 return bootmap_start >> PAGE_SHIFT; 440} 441 442#define MAX_LOWMEM HIGHMEM_START 443#define MAX_LOWMEM_PFN PFN_DOWN(MAX_LOWMEM) 444 445static void __init setup_bootmem(void) 446{ 447 unsigned bootmap_size; 448 unsigned long first_pfn, bootmap_pfn, pages; 449 unsigned long max_pfn, max_low_pfn; 450 unsigned node = 0; 451 struct resource *res; 452 453 printk(KERN_INFO "Physical memory:\n"); 454 for (res = system_ram; res; res = res->sibling) 455 printk(" %08x-%08x\n", res->start, res->end); 456 printk(KERN_INFO "Reserved memory:\n"); 457 for (res = reserved; res; res = res->sibling) 458 printk(" %08x-%08x: %s\n", 459 res->start, res->end, res->name); 460 461 nodes_clear(node_online_map); 462 463 if (system_ram->sibling) 464 printk(KERN_WARNING "Only using first memory bank\n"); 465 466 for (res = system_ram; res; res = NULL) { 467 first_pfn = PFN_UP(res->start); 468 max_low_pfn = max_pfn = PFN_DOWN(res->end + 1); 469 bootmap_pfn = find_bootmap_pfn(res); 470 if (bootmap_pfn > max_pfn) 471 panic("No space for bootmem bitmap!\n"); 472 473 if (max_low_pfn > MAX_LOWMEM_PFN) { 474 max_low_pfn = MAX_LOWMEM_PFN; 475#ifndef CONFIG_HIGHMEM 476 /* 477 * Lowmem is memory that can be addressed 478 * directly through P1/P2 479 */ 480 printk(KERN_WARNING 481 "Node %u: Only %ld MiB of memory will be used.\n", 482 node, MAX_LOWMEM >> 20); 483 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n"); 484#else 485#error HIGHMEM is not supported by AVR32 yet 486#endif 487 } 488 489 /* Initialize the boot-time allocator with low memory only. */ 490 bootmap_size = init_bootmem_node(NODE_DATA(node), bootmap_pfn, 491 first_pfn, max_low_pfn); 492 493 /* 494 * Register fully available RAM pages with the bootmem 495 * allocator. 496 */ 497 pages = max_low_pfn - first_pfn; 498 free_bootmem_node (NODE_DATA(node), PFN_PHYS(first_pfn), 499 PFN_PHYS(pages)); 500 501 /* Reserve space for the bootmem bitmap... */ 502 reserve_bootmem_node(NODE_DATA(node), 503 PFN_PHYS(bootmap_pfn), 504 bootmap_size, 505 BOOTMEM_DEFAULT); 506 507 /* ...and any other reserved regions. */ 508 for (res = reserved; res; res = res->sibling) { 509 if (res->start > PFN_PHYS(max_pfn)) 510 break; 511 512 /* 513 * resource_init will complain about partial 514 * overlaps, so we'll just ignore such 515 * resources for now. 516 */ 517 if (res->start >= PFN_PHYS(first_pfn) 518 && res->end < PFN_PHYS(max_pfn)) 519 reserve_bootmem_node( 520 NODE_DATA(node), res->start, 521 res->end - res->start + 1, 522 BOOTMEM_DEFAULT); 523 } 524 525 node_set_online(node); 526 } 527} 528 529void __init setup_arch (char **cmdline_p) 530{ 531 struct clk *cpu_clk; 532 533 init_mm.start_code = (unsigned long)_text; 534 init_mm.end_code = (unsigned long)_etext; 535 init_mm.end_data = (unsigned long)_edata; 536 init_mm.brk = (unsigned long)_end; 537 538 /* 539 * Include .init section to make allocations easier. It will 540 * be removed before the resource is actually requested. 541 */ 542 kernel_code.start = __pa(__init_begin); 543 kernel_code.end = __pa(init_mm.end_code - 1); 544 kernel_data.start = __pa(init_mm.end_code); 545 kernel_data.end = __pa(init_mm.brk - 1); 546 547 parse_tags(bootloader_tags); 548 549 setup_processor(); 550 setup_platform(); 551 setup_board(); 552 553 cpu_clk = clk_get(NULL, "cpu"); 554 if (IS_ERR(cpu_clk)) { 555 printk(KERN_WARNING "Warning: Unable to get CPU clock\n"); 556 } else { 557 unsigned long cpu_hz = clk_get_rate(cpu_clk); 558 559 /* 560 * Well, duh, but it's probably a good idea to 561 * increment the use count. 562 */ 563 clk_enable(cpu_clk); 564 565 boot_cpu_data.clk = cpu_clk; 566 boot_cpu_data.loops_per_jiffy = cpu_hz * 4; 567 printk("CPU: Running at %lu.%03lu MHz\n", 568 ((cpu_hz + 500) / 1000) / 1000, 569 ((cpu_hz + 500) / 1000) % 1000); 570 } 571 572 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE); 573 *cmdline_p = command_line; 574 parse_early_param(); 575 576 setup_bootmem(); 577 578#ifdef CONFIG_VT 579 conswitchp = &dummy_con; 580#endif 581 582 paging_init(); 583 resource_init(); 584} 585