setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. * 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_MEM,
  46. };
  47. static struct resource __initdata kernel_code = {
  48. .name = "Kernel code",
  49. .start = 0,
  50. .end = 0,
  51. .flags = IORESOURCE_MEM,
  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_MEM;
  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->flags = IORESOURCE_MEM;
  142. *pprev = new;
  143. return 0;
  144. }
  145. static unsigned long __init
  146. find_free_region(const struct resource *mem, resource_size_t size,
  147. resource_size_t align)
  148. {
  149. struct resource *res;
  150. unsigned long target;
  151. target = ALIGN(mem->start, align);
  152. for (res = reserved; res; res = res->sibling) {
  153. if ((target + size) <= res->start)
  154. break;
  155. if (target <= res->end)
  156. target = ALIGN(res->end + 1, align);
  157. }
  158. if ((target + size) > (mem->end + 1))
  159. return mem->end + 1;
  160. return target;
  161. }
  162. static int __init
  163. alloc_reserved_region(resource_size_t *start, resource_size_t size,
  164. resource_size_t align, const char *name)
  165. {
  166. struct resource *mem;
  167. resource_size_t target;
  168. int ret;
  169. for (mem = system_ram; mem; mem = mem->sibling) {
  170. target = find_free_region(mem, size, align);
  171. if (target <= mem->end) {
  172. ret = add_reserved_region(target, target + size - 1,
  173. name);
  174. if (!ret)
  175. *start = target;
  176. return ret;
  177. }
  178. }
  179. return -ENOMEM;
  180. }
  181. /*
  182. * Early framebuffer allocation. Works as follows:
  183. * - If fbmem_size is zero, nothing will be allocated or reserved.
  184. * - If fbmem_start is zero when setup_bootmem() is called,
  185. * a block of fbmem_size bytes will be reserved before bootmem
  186. * initialization. It will be aligned to the largest page size
  187. * that fbmem_size is a multiple of.
  188. * - If fbmem_start is nonzero, an area of size fbmem_size will be
  189. * reserved at the physical address fbmem_start if possible. If
  190. * it collides with other reserved memory, a different block of
  191. * same size will be allocated, just as if fbmem_start was zero.
  192. *
  193. * Board-specific code may use these variables to set up platform data
  194. * for the framebuffer driver if fbmem_size is nonzero.
  195. */
  196. resource_size_t __initdata fbmem_start;
  197. resource_size_t __initdata fbmem_size;
  198. /*
  199. * "fbmem=xxx[kKmM]" allocates the specified amount of boot memory for
  200. * use as framebuffer.
  201. *
  202. * "fbmem=xxx[kKmM]@yyy[kKmM]" defines a memory region of size xxx and
  203. * starting at yyy to be reserved for use as framebuffer.
  204. *
  205. * The kernel won't verify that the memory region starting at yyy
  206. * actually contains usable RAM.
  207. */
  208. static int __init early_parse_fbmem(char *p)
  209. {
  210. int ret;
  211. unsigned long align;
  212. fbmem_size = memparse(p, &p);
  213. if (*p == '@') {
  214. fbmem_start = memparse(p + 1, &p);
  215. ret = add_reserved_region(fbmem_start,
  216. fbmem_start + fbmem_size - 1,
  217. "Framebuffer");
  218. if (ret) {
  219. printk(KERN_WARNING
  220. "Failed to reserve framebuffer memory\n");
  221. fbmem_start = 0;
  222. }
  223. }
  224. if (!fbmem_start) {
  225. if ((fbmem_size & 0x000fffffUL) == 0)
  226. align = 0x100000; /* 1 MiB */
  227. else if ((fbmem_size & 0x0000ffffUL) == 0)
  228. align = 0x10000; /* 64 KiB */
  229. else
  230. align = 0x1000; /* 4 KiB */
  231. ret = alloc_reserved_region(&fbmem_start, fbmem_size,
  232. align, "Framebuffer");
  233. if (ret) {
  234. printk(KERN_WARNING
  235. "Failed to allocate framebuffer memory\n");
  236. fbmem_size = 0;
  237. }
  238. }
  239. return 0;
  240. }
  241. early_param("fbmem", early_parse_fbmem);
  242. static int __init parse_tag_core(struct tag *tag)
  243. {
  244. if (tag->hdr.size > 2) {
  245. if ((tag->u.core.flags & 1) == 0)
  246. root_mountflags &= ~MS_RDONLY;
  247. ROOT_DEV = new_decode_dev(tag->u.core.rootdev);
  248. }
  249. return 0;
  250. }
  251. __tagtable(ATAG_CORE, parse_tag_core);
  252. static int __init parse_tag_mem(struct tag *tag)
  253. {
  254. unsigned long start, end;
  255. /*
  256. * Ignore zero-sized entries. If we're running standalone, the
  257. * SDRAM code may emit such entries if something goes
  258. * wrong...
  259. */
  260. if (tag->u.mem_range.size == 0)
  261. return 0;
  262. start = tag->u.mem_range.addr;
  263. end = tag->u.mem_range.addr + tag->u.mem_range.size - 1;
  264. add_physical_memory(start, end);
  265. return 0;
  266. }
  267. __tagtable(ATAG_MEM, parse_tag_mem);
  268. static int __init parse_tag_rdimg(struct tag *tag)
  269. {
  270. #ifdef CONFIG_BLK_DEV_INITRD
  271. struct tag_mem_range *mem = &tag->u.mem_range;
  272. int ret;
  273. if (initrd_start) {
  274. printk(KERN_WARNING
  275. "Warning: Only the first initrd image will be used\n");
  276. return 0;
  277. }
  278. ret = add_reserved_region(mem->addr, mem->addr + mem->size - 1,
  279. "initrd");
  280. if (ret) {
  281. printk(KERN_WARNING
  282. "Warning: Failed to reserve initrd memory\n");
  283. return ret;
  284. }
  285. initrd_start = (unsigned long)__va(mem->addr);
  286. initrd_end = initrd_start + mem->size;
  287. #else
  288. printk(KERN_WARNING "RAM disk image present, but "
  289. "no initrd support in kernel, ignoring\n");
  290. #endif
  291. return 0;
  292. }
  293. __tagtable(ATAG_RDIMG, parse_tag_rdimg);
  294. static int __init parse_tag_rsvd_mem(struct tag *tag)
  295. {
  296. struct tag_mem_range *mem = &tag->u.mem_range;
  297. return add_reserved_region(mem->addr, mem->addr + mem->size - 1,
  298. "Reserved");
  299. }
  300. __tagtable(ATAG_RSVD_MEM, parse_tag_rsvd_mem);
  301. static int __init parse_tag_cmdline(struct tag *tag)
  302. {
  303. strlcpy(boot_command_line, tag->u.cmdline.cmdline, COMMAND_LINE_SIZE);
  304. return 0;
  305. }
  306. __tagtable(ATAG_CMDLINE, parse_tag_cmdline);
  307. static int __init parse_tag_clock(struct tag *tag)
  308. {
  309. /*
  310. * We'll figure out the clocks by peeking at the system
  311. * manager regs directly.
  312. */
  313. return 0;
  314. }
  315. __tagtable(ATAG_CLOCK, parse_tag_clock);
  316. /*
  317. * Scan the tag table for this tag, and call its parse function. The
  318. * tag table is built by the linker from all the __tagtable
  319. * declarations.
  320. */
  321. static int __init parse_tag(struct tag *tag)
  322. {
  323. extern struct tagtable __tagtable_begin, __tagtable_end;
  324. struct tagtable *t;
  325. for (t = &__tagtable_begin; t < &__tagtable_end; t++)
  326. if (tag->hdr.tag == t->tag) {
  327. t->parse(tag);
  328. break;
  329. }
  330. return t < &__tagtable_end;
  331. }
  332. /*
  333. * Parse all tags in the list we got from the boot loader
  334. */
  335. static void __init parse_tags(struct tag *t)
  336. {
  337. for (; t->hdr.tag != ATAG_NONE; t = tag_next(t))
  338. if (!parse_tag(t))
  339. printk(KERN_WARNING
  340. "Ignoring unrecognised tag 0x%08x\n",
  341. t->hdr.tag);
  342. }
  343. /*
  344. * Find a free memory region large enough for storing the
  345. * bootmem bitmap.
  346. */
  347. static unsigned long __init
  348. find_bootmap_pfn(const struct resource *mem)
  349. {
  350. unsigned long bootmap_pages, bootmap_len;
  351. unsigned long node_pages = PFN_UP(mem->end - mem->start + 1);
  352. unsigned long bootmap_start;
  353. bootmap_pages = bootmem_bootmap_pages(node_pages);
  354. bootmap_len = bootmap_pages << PAGE_SHIFT;
  355. /*
  356. * Find a large enough region without reserved pages for
  357. * storing the bootmem bitmap. We can take advantage of the
  358. * fact that all lists have been sorted.
  359. *
  360. * We have to check that we don't collide with any reserved
  361. * regions, which includes the kernel image and any RAMDISK
  362. * images.
  363. */
  364. bootmap_start = find_free_region(mem, bootmap_len, PAGE_SIZE);
  365. return bootmap_start >> PAGE_SHIFT;
  366. }
  367. #define MAX_LOWMEM HIGHMEM_START
  368. #define MAX_LOWMEM_PFN PFN_DOWN(MAX_LOWMEM)
  369. static void __init setup_bootmem(void)
  370. {
  371. unsigned bootmap_size;
  372. unsigned long first_pfn, bootmap_pfn, pages;
  373. unsigned long max_pfn, max_low_pfn;
  374. unsigned node = 0;
  375. struct resource *res;
  376. printk(KERN_INFO "Physical memory:\n");
  377. for (res = system_ram; res; res = res->sibling)
  378. printk(" %08x-%08x\n", res->start, res->end);
  379. printk(KERN_INFO "Reserved memory:\n");
  380. for (res = reserved; res; res = res->sibling)
  381. printk(" %08x-%08x: %s\n",
  382. res->start, res->end, res->name);
  383. nodes_clear(node_online_map);
  384. if (system_ram->sibling)
  385. printk(KERN_WARNING "Only using first memory bank\n");
  386. for (res = system_ram; res; res = NULL) {
  387. first_pfn = PFN_UP(res->start);
  388. max_low_pfn = max_pfn = PFN_DOWN(res->end + 1);
  389. bootmap_pfn = find_bootmap_pfn(res);
  390. if (bootmap_pfn > max_pfn)
  391. panic("No space for bootmem bitmap!\n");
  392. if (max_low_pfn > MAX_LOWMEM_PFN) {
  393. max_low_pfn = MAX_LOWMEM_PFN;
  394. #ifndef CONFIG_HIGHMEM
  395. /*
  396. * Lowmem is memory that can be addressed
  397. * directly through P1/P2
  398. */
  399. printk(KERN_WARNING
  400. "Node %u: Only %ld MiB of memory will be used.\n",
  401. node, MAX_LOWMEM >> 20);
  402. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  403. #else
  404. #error HIGHMEM is not supported by AVR32 yet
  405. #endif
  406. }
  407. /* Initialize the boot-time allocator with low memory only. */
  408. bootmap_size = init_bootmem_node(NODE_DATA(node), bootmap_pfn,
  409. first_pfn, max_low_pfn);
  410. /*
  411. * Register fully available RAM pages with the bootmem
  412. * allocator.
  413. */
  414. pages = max_low_pfn - first_pfn;
  415. free_bootmem_node (NODE_DATA(node), PFN_PHYS(first_pfn),
  416. PFN_PHYS(pages));
  417. /* Reserve space for the bootmem bitmap... */
  418. reserve_bootmem_node(NODE_DATA(node),
  419. PFN_PHYS(bootmap_pfn),
  420. bootmap_size);
  421. /* ...and any other reserved regions. */
  422. for (res = reserved; res; res = res->sibling) {
  423. if (res->start > PFN_PHYS(max_pfn))
  424. break;
  425. /*
  426. * resource_init will complain about partial
  427. * overlaps, so we'll just ignore such
  428. * resources for now.
  429. */
  430. if (res->start >= PFN_PHYS(first_pfn)
  431. && res->end < PFN_PHYS(max_pfn))
  432. reserve_bootmem_node(
  433. NODE_DATA(node), res->start,
  434. res->end - res->start + 1);
  435. }
  436. node_set_online(node);
  437. }
  438. }
  439. void __init setup_arch (char **cmdline_p)
  440. {
  441. struct clk *cpu_clk;
  442. init_mm.start_code = (unsigned long)_text;
  443. init_mm.end_code = (unsigned long)_etext;
  444. init_mm.end_data = (unsigned long)_edata;
  445. init_mm.brk = (unsigned long)_end;
  446. /*
  447. * Include .init section to make allocations easier. It will
  448. * be removed before the resource is actually requested.
  449. */
  450. kernel_code.start = __pa(__init_begin);
  451. kernel_code.end = __pa(init_mm.end_code - 1);
  452. kernel_data.start = __pa(init_mm.end_code);
  453. kernel_data.end = __pa(init_mm.brk - 1);
  454. parse_tags(bootloader_tags);
  455. setup_processor();
  456. setup_platform();
  457. setup_board();
  458. cpu_clk = clk_get(NULL, "cpu");
  459. if (IS_ERR(cpu_clk)) {
  460. printk(KERN_WARNING "Warning: Unable to get CPU clock\n");
  461. } else {
  462. unsigned long cpu_hz = clk_get_rate(cpu_clk);
  463. /*
  464. * Well, duh, but it's probably a good idea to
  465. * increment the use count.
  466. */
  467. clk_enable(cpu_clk);
  468. boot_cpu_data.clk = cpu_clk;
  469. boot_cpu_data.loops_per_jiffy = cpu_hz * 4;
  470. printk("CPU: Running at %lu.%03lu MHz\n",
  471. ((cpu_hz + 500) / 1000) / 1000,
  472. ((cpu_hz + 500) / 1000) % 1000);
  473. }
  474. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  475. *cmdline_p = command_line;
  476. parse_early_param();
  477. setup_bootmem();
  478. #ifdef CONFIG_VT
  479. conswitchp = &dummy_con;
  480. #endif
  481. paging_init();
  482. resource_init();
  483. }