setup.c 15 KB

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