setup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 <asm/arch/board.h>
  27. #include <asm/arch/init.h>
  28. extern int root_mountflags;
  29. /*
  30. * Bootloader-provided information about physical memory
  31. */
  32. struct tag_mem_range *mem_phys;
  33. struct tag_mem_range *mem_reserved;
  34. struct tag_mem_range *mem_ramdisk;
  35. /*
  36. * Initialize loops_per_jiffy as 5000000 (500MIPS).
  37. * Better make it too large than too small...
  38. */
  39. struct avr32_cpuinfo boot_cpu_data = {
  40. .loops_per_jiffy = 5000000
  41. };
  42. EXPORT_SYMBOL(boot_cpu_data);
  43. static char __initdata command_line[COMMAND_LINE_SIZE];
  44. /*
  45. * Should be more than enough, but if you have a _really_ complex
  46. * setup, you might need to increase the size of this...
  47. */
  48. static struct tag_mem_range __initdata mem_range_cache[32];
  49. static unsigned mem_range_next_free;
  50. /*
  51. * Standard memory resources
  52. */
  53. static struct resource mem_res[] = {
  54. {
  55. .name = "Kernel code",
  56. .start = 0,
  57. .end = 0,
  58. .flags = IORESOURCE_MEM
  59. },
  60. {
  61. .name = "Kernel data",
  62. .start = 0,
  63. .end = 0,
  64. .flags = IORESOURCE_MEM,
  65. },
  66. };
  67. #define kernel_code mem_res[0]
  68. #define kernel_data mem_res[1]
  69. /*
  70. * Early framebuffer allocation. Works as follows:
  71. * - If fbmem_size is zero, nothing will be allocated or reserved.
  72. * - If fbmem_start is zero when setup_bootmem() is called,
  73. * fbmem_size bytes will be allocated from the bootmem allocator.
  74. * - If fbmem_start is nonzero, an area of size fbmem_size will be
  75. * reserved at the physical address fbmem_start if necessary. If
  76. * the area isn't in a memory region known to the kernel, it will
  77. * be left alone.
  78. *
  79. * Board-specific code may use these variables to set up platform data
  80. * for the framebuffer driver if fbmem_size is nonzero.
  81. */
  82. static unsigned long __initdata fbmem_start;
  83. static unsigned long __initdata fbmem_size;
  84. /*
  85. * "fbmem=xxx[kKmM]" allocates the specified amount of boot memory for
  86. * use as framebuffer.
  87. *
  88. * "fbmem=xxx[kKmM]@yyy[kKmM]" defines a memory region of size xxx and
  89. * starting at yyy to be reserved for use as framebuffer.
  90. *
  91. * The kernel won't verify that the memory region starting at yyy
  92. * actually contains usable RAM.
  93. */
  94. static int __init early_parse_fbmem(char *p)
  95. {
  96. fbmem_size = memparse(p, &p);
  97. if (*p == '@')
  98. fbmem_start = memparse(p, &p);
  99. return 0;
  100. }
  101. early_param("fbmem", early_parse_fbmem);
  102. static inline void __init resource_init(void)
  103. {
  104. struct tag_mem_range *region;
  105. kernel_code.start = __pa(init_mm.start_code);
  106. kernel_code.end = __pa(init_mm.end_code - 1);
  107. kernel_data.start = __pa(init_mm.end_code);
  108. kernel_data.end = __pa(init_mm.brk - 1);
  109. for (region = mem_phys; region; region = region->next) {
  110. struct resource *res;
  111. unsigned long phys_start, phys_end;
  112. if (region->size == 0)
  113. continue;
  114. phys_start = region->addr;
  115. phys_end = phys_start + region->size - 1;
  116. res = alloc_bootmem_low(sizeof(*res));
  117. res->name = "System RAM";
  118. res->start = phys_start;
  119. res->end = phys_end;
  120. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  121. request_resource (&iomem_resource, res);
  122. if (kernel_code.start >= res->start &&
  123. kernel_code.end <= res->end)
  124. request_resource (res, &kernel_code);
  125. if (kernel_data.start >= res->start &&
  126. kernel_data.end <= res->end)
  127. request_resource (res, &kernel_data);
  128. }
  129. }
  130. static int __init parse_tag_core(struct tag *tag)
  131. {
  132. if (tag->hdr.size > 2) {
  133. if ((tag->u.core.flags & 1) == 0)
  134. root_mountflags &= ~MS_RDONLY;
  135. ROOT_DEV = new_decode_dev(tag->u.core.rootdev);
  136. }
  137. return 0;
  138. }
  139. __tagtable(ATAG_CORE, parse_tag_core);
  140. static int __init parse_tag_mem_range(struct tag *tag,
  141. struct tag_mem_range **root)
  142. {
  143. struct tag_mem_range *cur, **pprev;
  144. struct tag_mem_range *new;
  145. /*
  146. * Ignore zero-sized entries. If we're running standalone, the
  147. * SDRAM code may emit such entries if something goes
  148. * wrong...
  149. */
  150. if (tag->u.mem_range.size == 0)
  151. return 0;
  152. /*
  153. * Copy the data so the bootmem init code doesn't need to care
  154. * about it.
  155. */
  156. if (mem_range_next_free >= ARRAY_SIZE(mem_range_cache))
  157. panic("Physical memory map too complex!\n");
  158. new = &mem_range_cache[mem_range_next_free++];
  159. *new = tag->u.mem_range;
  160. pprev = root;
  161. cur = *root;
  162. while (cur) {
  163. pprev = &cur->next;
  164. cur = cur->next;
  165. }
  166. *pprev = new;
  167. new->next = NULL;
  168. return 0;
  169. }
  170. static int __init parse_tag_mem(struct tag *tag)
  171. {
  172. return parse_tag_mem_range(tag, &mem_phys);
  173. }
  174. __tagtable(ATAG_MEM, parse_tag_mem);
  175. static int __init parse_tag_cmdline(struct tag *tag)
  176. {
  177. strlcpy(boot_command_line, tag->u.cmdline.cmdline, COMMAND_LINE_SIZE);
  178. return 0;
  179. }
  180. __tagtable(ATAG_CMDLINE, parse_tag_cmdline);
  181. static int __init parse_tag_rdimg(struct tag *tag)
  182. {
  183. return parse_tag_mem_range(tag, &mem_ramdisk);
  184. }
  185. __tagtable(ATAG_RDIMG, parse_tag_rdimg);
  186. static int __init parse_tag_clock(struct tag *tag)
  187. {
  188. /*
  189. * We'll figure out the clocks by peeking at the system
  190. * manager regs directly.
  191. */
  192. return 0;
  193. }
  194. __tagtable(ATAG_CLOCK, parse_tag_clock);
  195. static int __init parse_tag_rsvd_mem(struct tag *tag)
  196. {
  197. return parse_tag_mem_range(tag, &mem_reserved);
  198. }
  199. __tagtable(ATAG_RSVD_MEM, parse_tag_rsvd_mem);
  200. /*
  201. * Scan the tag table for this tag, and call its parse function. The
  202. * tag table is built by the linker from all the __tagtable
  203. * declarations.
  204. */
  205. static int __init parse_tag(struct tag *tag)
  206. {
  207. extern struct tagtable __tagtable_begin, __tagtable_end;
  208. struct tagtable *t;
  209. for (t = &__tagtable_begin; t < &__tagtable_end; t++)
  210. if (tag->hdr.tag == t->tag) {
  211. t->parse(tag);
  212. break;
  213. }
  214. return t < &__tagtable_end;
  215. }
  216. /*
  217. * Parse all tags in the list we got from the boot loader
  218. */
  219. static void __init parse_tags(struct tag *t)
  220. {
  221. for (; t->hdr.tag != ATAG_NONE; t = tag_next(t))
  222. if (!parse_tag(t))
  223. printk(KERN_WARNING
  224. "Ignoring unrecognised tag 0x%08x\n",
  225. t->hdr.tag);
  226. }
  227. static void __init print_memory_map(const char *what,
  228. struct tag_mem_range *mem)
  229. {
  230. printk ("%s:\n", what);
  231. for (; mem; mem = mem->next) {
  232. printk (" %08lx - %08lx\n",
  233. (unsigned long)mem->addr,
  234. (unsigned long)(mem->addr + mem->size));
  235. }
  236. }
  237. #define MAX_LOWMEM HIGHMEM_START
  238. #define MAX_LOWMEM_PFN PFN_DOWN(MAX_LOWMEM)
  239. /*
  240. * Sort a list of memory regions in-place by ascending address.
  241. *
  242. * We're using bubble sort because we only have singly linked lists
  243. * with few elements.
  244. */
  245. static void __init sort_mem_list(struct tag_mem_range **pmem)
  246. {
  247. int done;
  248. struct tag_mem_range **a, **b;
  249. if (!*pmem)
  250. return;
  251. do {
  252. done = 1;
  253. a = pmem, b = &(*pmem)->next;
  254. while (*b) {
  255. if ((*a)->addr > (*b)->addr) {
  256. struct tag_mem_range *tmp;
  257. tmp = (*b)->next;
  258. (*b)->next = *a;
  259. *a = *b;
  260. *b = tmp;
  261. done = 0;
  262. }
  263. a = &(*a)->next;
  264. b = &(*a)->next;
  265. }
  266. } while (!done);
  267. }
  268. /*
  269. * Find a free memory region large enough for storing the
  270. * bootmem bitmap.
  271. */
  272. static unsigned long __init
  273. find_bootmap_pfn(const struct tag_mem_range *mem)
  274. {
  275. unsigned long bootmap_pages, bootmap_len;
  276. unsigned long node_pages = PFN_UP(mem->size);
  277. unsigned long bootmap_addr = mem->addr;
  278. struct tag_mem_range *reserved = mem_reserved;
  279. struct tag_mem_range *ramdisk = mem_ramdisk;
  280. unsigned long kern_start = __pa(_stext);
  281. unsigned long kern_end = __pa(_end);
  282. bootmap_pages = bootmem_bootmap_pages(node_pages);
  283. bootmap_len = bootmap_pages << PAGE_SHIFT;
  284. /*
  285. * Find a large enough region without reserved pages for
  286. * storing the bootmem bitmap. We can take advantage of the
  287. * fact that all lists have been sorted.
  288. *
  289. * We have to check explicitly reserved regions as well as the
  290. * kernel image and any RAMDISK images...
  291. *
  292. * Oh, and we have to make sure we don't overwrite the taglist
  293. * since we're going to use it until the bootmem allocator is
  294. * fully up and running.
  295. */
  296. while (1) {
  297. if ((bootmap_addr < kern_end) &&
  298. ((bootmap_addr + bootmap_len) > kern_start))
  299. bootmap_addr = kern_end;
  300. while (reserved &&
  301. (bootmap_addr >= (reserved->addr + reserved->size)))
  302. reserved = reserved->next;
  303. if (reserved &&
  304. ((bootmap_addr + bootmap_len) >= reserved->addr)) {
  305. bootmap_addr = reserved->addr + reserved->size;
  306. continue;
  307. }
  308. while (ramdisk &&
  309. (bootmap_addr >= (ramdisk->addr + ramdisk->size)))
  310. ramdisk = ramdisk->next;
  311. if (!ramdisk ||
  312. ((bootmap_addr + bootmap_len) < ramdisk->addr))
  313. break;
  314. bootmap_addr = ramdisk->addr + ramdisk->size;
  315. }
  316. if ((PFN_UP(bootmap_addr) + bootmap_len) >= (mem->addr + mem->size))
  317. return ~0UL;
  318. return PFN_UP(bootmap_addr);
  319. }
  320. static void __init setup_bootmem(void)
  321. {
  322. unsigned bootmap_size;
  323. unsigned long first_pfn, bootmap_pfn, pages;
  324. unsigned long max_pfn, max_low_pfn;
  325. unsigned long kern_start = __pa(_stext);
  326. unsigned long kern_end = __pa(_end);
  327. unsigned node = 0;
  328. struct tag_mem_range *bank, *res;
  329. sort_mem_list(&mem_phys);
  330. sort_mem_list(&mem_reserved);
  331. print_memory_map("Physical memory", mem_phys);
  332. print_memory_map("Reserved memory", mem_reserved);
  333. nodes_clear(node_online_map);
  334. if (mem_ramdisk) {
  335. #ifdef CONFIG_BLK_DEV_INITRD
  336. initrd_start = (unsigned long)__va(mem_ramdisk->addr);
  337. initrd_end = initrd_start + mem_ramdisk->size;
  338. print_memory_map("RAMDISK images", mem_ramdisk);
  339. if (mem_ramdisk->next)
  340. printk(KERN_WARNING
  341. "Warning: Only the first RAMDISK image "
  342. "will be used\n");
  343. sort_mem_list(&mem_ramdisk);
  344. #else
  345. printk(KERN_WARNING "RAM disk image present, but "
  346. "no initrd support in kernel!\n");
  347. #endif
  348. }
  349. if (mem_phys->next)
  350. printk(KERN_WARNING "Only using first memory bank\n");
  351. for (bank = mem_phys; bank; bank = NULL) {
  352. first_pfn = PFN_UP(bank->addr);
  353. max_low_pfn = max_pfn = PFN_DOWN(bank->addr + bank->size);
  354. bootmap_pfn = find_bootmap_pfn(bank);
  355. if (bootmap_pfn > max_pfn)
  356. panic("No space for bootmem bitmap!\n");
  357. if (max_low_pfn > MAX_LOWMEM_PFN) {
  358. max_low_pfn = MAX_LOWMEM_PFN;
  359. #ifndef CONFIG_HIGHMEM
  360. /*
  361. * Lowmem is memory that can be addressed
  362. * directly through P1/P2
  363. */
  364. printk(KERN_WARNING
  365. "Node %u: Only %ld MiB of memory will be used.\n",
  366. node, MAX_LOWMEM >> 20);
  367. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  368. #else
  369. #error HIGHMEM is not supported by AVR32 yet
  370. #endif
  371. }
  372. /* Initialize the boot-time allocator with low memory only. */
  373. bootmap_size = init_bootmem_node(NODE_DATA(node), bootmap_pfn,
  374. first_pfn, max_low_pfn);
  375. printk("Node %u: bdata = %p, bdata->node_bootmem_map = %p\n",
  376. node, NODE_DATA(node)->bdata,
  377. NODE_DATA(node)->bdata->node_bootmem_map);
  378. /*
  379. * Register fully available RAM pages with the bootmem
  380. * allocator.
  381. */
  382. pages = max_low_pfn - first_pfn;
  383. free_bootmem_node (NODE_DATA(node), PFN_PHYS(first_pfn),
  384. PFN_PHYS(pages));
  385. /*
  386. * Reserve space for the kernel image (if present in
  387. * this node)...
  388. */
  389. if ((kern_start >= PFN_PHYS(first_pfn)) &&
  390. (kern_start < PFN_PHYS(max_pfn))) {
  391. printk("Node %u: Kernel image %08lx - %08lx\n",
  392. node, kern_start, kern_end);
  393. reserve_bootmem_node(NODE_DATA(node), kern_start,
  394. kern_end - kern_start);
  395. }
  396. /* ...the bootmem bitmap... */
  397. reserve_bootmem_node(NODE_DATA(node),
  398. PFN_PHYS(bootmap_pfn),
  399. bootmap_size);
  400. /* ...any RAMDISK images... */
  401. for (res = mem_ramdisk; res; res = res->next) {
  402. if (res->addr > PFN_PHYS(max_pfn))
  403. break;
  404. if (res->addr >= PFN_PHYS(first_pfn)) {
  405. printk("Node %u: RAMDISK %08lx - %08lx\n",
  406. node,
  407. (unsigned long)res->addr,
  408. (unsigned long)(res->addr + res->size));
  409. reserve_bootmem_node(NODE_DATA(node),
  410. res->addr, res->size);
  411. }
  412. }
  413. /* ...and any other reserved regions. */
  414. for (res = mem_reserved; res; res = res->next) {
  415. if (res->addr > PFN_PHYS(max_pfn))
  416. break;
  417. if (res->addr >= PFN_PHYS(first_pfn)) {
  418. printk("Node %u: Reserved %08lx - %08lx\n",
  419. node,
  420. (unsigned long)res->addr,
  421. (unsigned long)(res->addr + res->size));
  422. reserve_bootmem_node(NODE_DATA(node),
  423. res->addr, res->size);
  424. }
  425. }
  426. node_set_online(node);
  427. }
  428. }
  429. void __init setup_arch (char **cmdline_p)
  430. {
  431. struct clk *cpu_clk;
  432. parse_tags(bootloader_tags);
  433. setup_processor();
  434. setup_platform();
  435. setup_board();
  436. cpu_clk = clk_get(NULL, "cpu");
  437. if (IS_ERR(cpu_clk)) {
  438. printk(KERN_WARNING "Warning: Unable to get CPU clock\n");
  439. } else {
  440. unsigned long cpu_hz = clk_get_rate(cpu_clk);
  441. /*
  442. * Well, duh, but it's probably a good idea to
  443. * increment the use count.
  444. */
  445. clk_enable(cpu_clk);
  446. boot_cpu_data.clk = cpu_clk;
  447. boot_cpu_data.loops_per_jiffy = cpu_hz * 4;
  448. printk("CPU: Running at %lu.%03lu MHz\n",
  449. ((cpu_hz + 500) / 1000) / 1000,
  450. ((cpu_hz + 500) / 1000) % 1000);
  451. }
  452. init_mm.start_code = (unsigned long) &_text;
  453. init_mm.end_code = (unsigned long) &_etext;
  454. init_mm.end_data = (unsigned long) &_edata;
  455. init_mm.brk = (unsigned long) &_end;
  456. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  457. *cmdline_p = command_line;
  458. parse_early_param();
  459. setup_bootmem();
  460. board_setup_fbmem(fbmem_start, fbmem_size);
  461. #ifdef CONFIG_VT
  462. conswitchp = &dummy_con;
  463. #endif
  464. paging_init();
  465. resource_init();
  466. }