setup.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * OpenRISC setup.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * This file handles the architecture-dependent parts of initialization
  18. */
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/stddef.h>
  24. #include <linux/unistd.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/ioport.h>
  29. #include <linux/delay.h>
  30. #include <linux/console.h>
  31. #include <linux/init.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/serial.h>
  35. #include <linux/initrd.h>
  36. #include <linux/of_fdt.h>
  37. #include <linux/of.h>
  38. #include <linux/memblock.h>
  39. #include <linux/device.h>
  40. #include <linux/of_platform.h>
  41. #include <asm/sections.h>
  42. #include <asm/segment.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/types.h>
  45. #include <asm/setup.h>
  46. #include <asm/io.h>
  47. #include <asm/cpuinfo.h>
  48. #include <asm/delay.h>
  49. #include "vmlinux.h"
  50. static unsigned long __init setup_memory(void)
  51. {
  52. unsigned long bootmap_size;
  53. unsigned long ram_start_pfn;
  54. unsigned long free_ram_start_pfn;
  55. unsigned long ram_end_pfn;
  56. phys_addr_t memory_start, memory_end;
  57. struct memblock_region *region;
  58. memory_end = memory_start = 0;
  59. /* Find main memory where is the kernel */
  60. for_each_memblock(memory, region) {
  61. memory_start = region->base;
  62. memory_end = region->base + region->size;
  63. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  64. memory_start, memory_end);
  65. }
  66. if (!memory_end) {
  67. panic("No memory!");
  68. }
  69. ram_start_pfn = PFN_UP(memory_start);
  70. /* free_ram_start_pfn is first page after kernel */
  71. free_ram_start_pfn = PFN_UP(__pa(_end));
  72. ram_end_pfn = PFN_DOWN(memblock_end_of_DRAM());
  73. max_pfn = ram_end_pfn;
  74. /*
  75. * initialize the boot-time allocator (with low memory only).
  76. *
  77. * This makes the memory from the end of the kernel to the end of
  78. * RAM usable.
  79. * init_bootmem sets the global values min_low_pfn, max_low_pfn.
  80. */
  81. bootmap_size = init_bootmem(free_ram_start_pfn,
  82. ram_end_pfn - ram_start_pfn);
  83. free_bootmem(PFN_PHYS(free_ram_start_pfn),
  84. (ram_end_pfn - free_ram_start_pfn) << PAGE_SHIFT);
  85. reserve_bootmem(PFN_PHYS(free_ram_start_pfn), bootmap_size,
  86. BOOTMEM_DEFAULT);
  87. for_each_memblock(reserved, region) {
  88. printk(KERN_INFO "Reserved - 0x%08x-0x%08x\n",
  89. (u32) region->base, (u32) region->size);
  90. reserve_bootmem(region->base, region->size, BOOTMEM_DEFAULT);
  91. }
  92. return ram_end_pfn;
  93. }
  94. struct cpuinfo cpuinfo;
  95. static void print_cpuinfo(void)
  96. {
  97. unsigned long upr = mfspr(SPR_UPR);
  98. unsigned long vr = mfspr(SPR_VR);
  99. unsigned int version;
  100. unsigned int revision;
  101. version = (vr & SPR_VR_VER) >> 24;
  102. revision = (vr & SPR_VR_REV);
  103. printk(KERN_INFO "CPU: OpenRISC-%x (revision %d) @%d MHz\n",
  104. version, revision, cpuinfo.clock_frequency / 1000000);
  105. if (!(upr & SPR_UPR_UP)) {
  106. printk(KERN_INFO
  107. "-- no UPR register... unable to detect configuration\n");
  108. return;
  109. }
  110. if (upr & SPR_UPR_DCP)
  111. printk(KERN_INFO
  112. "-- dcache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  113. cpuinfo.dcache_size, cpuinfo.dcache_block_size, 1);
  114. else
  115. printk(KERN_INFO "-- dcache disabled\n");
  116. if (upr & SPR_UPR_ICP)
  117. printk(KERN_INFO
  118. "-- icache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  119. cpuinfo.icache_size, cpuinfo.icache_block_size, 1);
  120. else
  121. printk(KERN_INFO "-- icache disabled\n");
  122. if (upr & SPR_UPR_DMP)
  123. printk(KERN_INFO "-- dmmu: %4d entries, %lu way(s)\n",
  124. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  125. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW));
  126. if (upr & SPR_UPR_IMP)
  127. printk(KERN_INFO "-- immu: %4d entries, %lu way(s)\n",
  128. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  129. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW));
  130. printk(KERN_INFO "-- additional features:\n");
  131. if (upr & SPR_UPR_DUP)
  132. printk(KERN_INFO "-- debug unit\n");
  133. if (upr & SPR_UPR_PCUP)
  134. printk(KERN_INFO "-- performance counters\n");
  135. if (upr & SPR_UPR_PMP)
  136. printk(KERN_INFO "-- power management\n");
  137. if (upr & SPR_UPR_PICP)
  138. printk(KERN_INFO "-- PIC\n");
  139. if (upr & SPR_UPR_TTP)
  140. printk(KERN_INFO "-- timer\n");
  141. if (upr & SPR_UPR_CUP)
  142. printk(KERN_INFO "-- custom unit(s)\n");
  143. }
  144. void __init setup_cpuinfo(void)
  145. {
  146. struct device_node *cpu;
  147. unsigned long iccfgr, dccfgr;
  148. unsigned long cache_set_size, cache_ways;
  149. cpu = of_find_compatible_node(NULL, NULL, "opencores,or1200-rtlsvn481");
  150. if (!cpu)
  151. panic("No compatible CPU found in device tree...\n");
  152. iccfgr = mfspr(SPR_ICCFGR);
  153. cache_ways = 1 << (iccfgr & SPR_ICCFGR_NCW);
  154. cache_set_size = 1 << ((iccfgr & SPR_ICCFGR_NCS) >> 3);
  155. cpuinfo.icache_block_size = 16 << ((iccfgr & SPR_ICCFGR_CBS) >> 7);
  156. cpuinfo.icache_size =
  157. cache_set_size * cache_ways * cpuinfo.icache_block_size;
  158. dccfgr = mfspr(SPR_DCCFGR);
  159. cache_ways = 1 << (dccfgr & SPR_DCCFGR_NCW);
  160. cache_set_size = 1 << ((dccfgr & SPR_DCCFGR_NCS) >> 3);
  161. cpuinfo.dcache_block_size = 16 << ((dccfgr & SPR_DCCFGR_CBS) >> 7);
  162. cpuinfo.dcache_size =
  163. cache_set_size * cache_ways * cpuinfo.dcache_block_size;
  164. if (of_property_read_u32(cpu, "clock-frequency",
  165. &cpuinfo.clock_frequency)) {
  166. printk(KERN_WARNING
  167. "Device tree missing CPU 'clock-frequency' parameter."
  168. "Assuming frequency 25MHZ"
  169. "This is probably not what you want.");
  170. }
  171. of_node_put(cpu);
  172. print_cpuinfo();
  173. }
  174. /**
  175. * or32_early_setup
  176. *
  177. * Handles the pointer to the device tree that this kernel is to use
  178. * for establishing the available platform devices.
  179. *
  180. * Falls back on built-in device tree in case null pointer is passed.
  181. */
  182. void __init or32_early_setup(void *fdt)
  183. {
  184. if (fdt)
  185. pr_info("FDT at %p\n", fdt);
  186. else {
  187. fdt = __dtb_start;
  188. pr_info("Compiled-in FDT at %p\n", fdt);
  189. }
  190. early_init_devtree(fdt);
  191. }
  192. static int __init openrisc_device_probe(void)
  193. {
  194. of_platform_populate(NULL, NULL, NULL, NULL);
  195. return 0;
  196. }
  197. device_initcall(openrisc_device_probe);
  198. static inline unsigned long extract_value_bits(unsigned long reg,
  199. short bit_nr, short width)
  200. {
  201. return (reg >> bit_nr) & (0 << width);
  202. }
  203. static inline unsigned long extract_value(unsigned long reg, unsigned long mask)
  204. {
  205. while (!(mask & 0x1)) {
  206. reg = reg >> 1;
  207. mask = mask >> 1;
  208. }
  209. return mask & reg;
  210. }
  211. void __init detect_unit_config(unsigned long upr, unsigned long mask,
  212. char *text, void (*func) (void))
  213. {
  214. if (text != NULL)
  215. printk("%s", text);
  216. if (upr & mask) {
  217. if (func != NULL)
  218. func();
  219. else
  220. printk("present\n");
  221. } else
  222. printk("not present\n");
  223. }
  224. /*
  225. * calibrate_delay
  226. *
  227. * Lightweight calibrate_delay implementation that calculates loops_per_jiffy
  228. * from the clock frequency passed in via the device tree
  229. *
  230. */
  231. void calibrate_delay(void)
  232. {
  233. const int *val;
  234. struct device_node *cpu = NULL;
  235. cpu = of_find_compatible_node(NULL, NULL, "opencores,or1200-rtlsvn481");
  236. val = of_get_property(cpu, "clock-frequency", NULL);
  237. if (!val)
  238. panic("no cpu 'clock-frequency' parameter in device tree");
  239. loops_per_jiffy = *val / HZ;
  240. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  241. loops_per_jiffy / (500000 / HZ),
  242. (loops_per_jiffy / (5000 / HZ)) % 100, loops_per_jiffy);
  243. }
  244. void __init setup_arch(char **cmdline_p)
  245. {
  246. unsigned long max_low_pfn;
  247. unflatten_and_copy_device_tree();
  248. setup_cpuinfo();
  249. /* process 1's initial memory region is the kernel code/data */
  250. init_mm.start_code = (unsigned long)_stext;
  251. init_mm.end_code = (unsigned long)_etext;
  252. init_mm.end_data = (unsigned long)_edata;
  253. init_mm.brk = (unsigned long)_end;
  254. #ifdef CONFIG_BLK_DEV_INITRD
  255. initrd_start = (unsigned long)&__initrd_start;
  256. initrd_end = (unsigned long)&__initrd_end;
  257. if (initrd_start == initrd_end) {
  258. initrd_start = 0;
  259. initrd_end = 0;
  260. }
  261. initrd_below_start_ok = 1;
  262. #endif
  263. /* setup bootmem allocator */
  264. max_low_pfn = setup_memory();
  265. /* paging_init() sets up the MMU and marks all pages as reserved */
  266. paging_init();
  267. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  268. if (!conswitchp)
  269. conswitchp = &dummy_con;
  270. #endif
  271. *cmdline_p = boot_command_line;
  272. printk(KERN_INFO "OpenRISC Linux -- http://openrisc.net\n");
  273. }
  274. static int show_cpuinfo(struct seq_file *m, void *v)
  275. {
  276. unsigned long vr;
  277. int version, revision;
  278. vr = mfspr(SPR_VR);
  279. version = (vr & SPR_VR_VER) >> 24;
  280. revision = vr & SPR_VR_REV;
  281. seq_printf(m,
  282. "cpu\t\t: OpenRISC-%x\n"
  283. "revision\t: %d\n"
  284. "frequency\t: %ld\n"
  285. "dcache size\t: %d bytes\n"
  286. "dcache block size\t: %d bytes\n"
  287. "icache size\t: %d bytes\n"
  288. "icache block size\t: %d bytes\n"
  289. "immu\t\t: %d entries, %lu ways\n"
  290. "dmmu\t\t: %d entries, %lu ways\n"
  291. "bogomips\t: %lu.%02lu\n",
  292. version,
  293. revision,
  294. loops_per_jiffy * HZ,
  295. cpuinfo.dcache_size,
  296. cpuinfo.dcache_block_size,
  297. cpuinfo.icache_size,
  298. cpuinfo.icache_block_size,
  299. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  300. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW),
  301. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  302. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW),
  303. (loops_per_jiffy * HZ) / 500000,
  304. ((loops_per_jiffy * HZ) / 5000) % 100);
  305. return 0;
  306. }
  307. static void *c_start(struct seq_file *m, loff_t * pos)
  308. {
  309. /* We only have one CPU... */
  310. return *pos < 1 ? (void *)1 : NULL;
  311. }
  312. static void *c_next(struct seq_file *m, void *v, loff_t * pos)
  313. {
  314. ++*pos;
  315. return NULL;
  316. }
  317. static void c_stop(struct seq_file *m, void *v)
  318. {
  319. }
  320. const struct seq_operations cpuinfo_op = {
  321. .start = c_start,
  322. .next = c_next,
  323. .stop = c_stop,
  324. .show = show_cpuinfo,
  325. };