setup.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <asm/sections.h>
  41. #include <asm/segment.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/types.h>
  44. #include <asm/setup.h>
  45. #include <asm/io.h>
  46. #include <asm/cpuinfo.h>
  47. #include <asm/delay.h>
  48. #include "vmlinux.h"
  49. static void __init setup_memory(void)
  50. {
  51. unsigned long ram_start_pfn;
  52. unsigned long ram_end_pfn;
  53. phys_addr_t memory_start, memory_end;
  54. struct memblock_region *region;
  55. memory_end = memory_start = 0;
  56. /* Find main memory where is the kernel, we assume its the only one */
  57. for_each_memblock(memory, region) {
  58. memory_start = region->base;
  59. memory_end = region->base + region->size;
  60. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  61. memory_start, memory_end);
  62. }
  63. if (!memory_end) {
  64. panic("No memory!");
  65. }
  66. ram_start_pfn = PFN_UP(memory_start);
  67. ram_end_pfn = PFN_DOWN(memblock_end_of_DRAM());
  68. /* setup bootmem globals (we use no_bootmem, but mm still depends on this) */
  69. min_low_pfn = ram_start_pfn;
  70. max_low_pfn = ram_end_pfn;
  71. max_pfn = ram_end_pfn;
  72. /*
  73. * initialize the boot-time allocator (with low memory only).
  74. *
  75. * This makes the memory from the end of the kernel to the end of
  76. * RAM usable.
  77. */
  78. memblock_reserve(__pa(_stext), _end - _stext);
  79. early_init_fdt_reserve_self();
  80. early_init_fdt_scan_reserved_mem();
  81. memblock_dump_all();
  82. }
  83. struct cpuinfo cpuinfo;
  84. static void print_cpuinfo(void)
  85. {
  86. unsigned long upr = mfspr(SPR_UPR);
  87. unsigned long vr = mfspr(SPR_VR);
  88. unsigned int version;
  89. unsigned int revision;
  90. version = (vr & SPR_VR_VER) >> 24;
  91. revision = (vr & SPR_VR_REV);
  92. printk(KERN_INFO "CPU: OpenRISC-%x (revision %d) @%d MHz\n",
  93. version, revision, cpuinfo.clock_frequency / 1000000);
  94. if (!(upr & SPR_UPR_UP)) {
  95. printk(KERN_INFO
  96. "-- no UPR register... unable to detect configuration\n");
  97. return;
  98. }
  99. if (upr & SPR_UPR_DCP)
  100. printk(KERN_INFO
  101. "-- dcache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  102. cpuinfo.dcache_size, cpuinfo.dcache_block_size,
  103. cpuinfo.dcache_ways);
  104. else
  105. printk(KERN_INFO "-- dcache disabled\n");
  106. if (upr & SPR_UPR_ICP)
  107. printk(KERN_INFO
  108. "-- icache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  109. cpuinfo.icache_size, cpuinfo.icache_block_size,
  110. cpuinfo.icache_ways);
  111. else
  112. printk(KERN_INFO "-- icache disabled\n");
  113. if (upr & SPR_UPR_DMP)
  114. printk(KERN_INFO "-- dmmu: %4d entries, %lu way(s)\n",
  115. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  116. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW));
  117. if (upr & SPR_UPR_IMP)
  118. printk(KERN_INFO "-- immu: %4d entries, %lu way(s)\n",
  119. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  120. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW));
  121. printk(KERN_INFO "-- additional features:\n");
  122. if (upr & SPR_UPR_DUP)
  123. printk(KERN_INFO "-- debug unit\n");
  124. if (upr & SPR_UPR_PCUP)
  125. printk(KERN_INFO "-- performance counters\n");
  126. if (upr & SPR_UPR_PMP)
  127. printk(KERN_INFO "-- power management\n");
  128. if (upr & SPR_UPR_PICP)
  129. printk(KERN_INFO "-- PIC\n");
  130. if (upr & SPR_UPR_TTP)
  131. printk(KERN_INFO "-- timer\n");
  132. if (upr & SPR_UPR_CUP)
  133. printk(KERN_INFO "-- custom unit(s)\n");
  134. }
  135. void __init setup_cpuinfo(void)
  136. {
  137. struct device_node *cpu;
  138. unsigned long iccfgr, dccfgr;
  139. unsigned long cache_set_size;
  140. cpu = of_find_compatible_node(NULL, NULL, "opencores,or1200-rtlsvn481");
  141. if (!cpu)
  142. panic("No compatible CPU found in device tree...\n");
  143. iccfgr = mfspr(SPR_ICCFGR);
  144. cpuinfo.icache_ways = 1 << (iccfgr & SPR_ICCFGR_NCW);
  145. cache_set_size = 1 << ((iccfgr & SPR_ICCFGR_NCS) >> 3);
  146. cpuinfo.icache_block_size = 16 << ((iccfgr & SPR_ICCFGR_CBS) >> 7);
  147. cpuinfo.icache_size =
  148. cache_set_size * cpuinfo.icache_ways * cpuinfo.icache_block_size;
  149. dccfgr = mfspr(SPR_DCCFGR);
  150. cpuinfo.dcache_ways = 1 << (dccfgr & SPR_DCCFGR_NCW);
  151. cache_set_size = 1 << ((dccfgr & SPR_DCCFGR_NCS) >> 3);
  152. cpuinfo.dcache_block_size = 16 << ((dccfgr & SPR_DCCFGR_CBS) >> 7);
  153. cpuinfo.dcache_size =
  154. cache_set_size * cpuinfo.dcache_ways * cpuinfo.dcache_block_size;
  155. if (of_property_read_u32(cpu, "clock-frequency",
  156. &cpuinfo.clock_frequency)) {
  157. printk(KERN_WARNING
  158. "Device tree missing CPU 'clock-frequency' parameter."
  159. "Assuming frequency 25MHZ"
  160. "This is probably not what you want.");
  161. }
  162. of_node_put(cpu);
  163. print_cpuinfo();
  164. }
  165. /**
  166. * or32_early_setup
  167. *
  168. * Handles the pointer to the device tree that this kernel is to use
  169. * for establishing the available platform devices.
  170. *
  171. * Falls back on built-in device tree in case null pointer is passed.
  172. */
  173. void __init or32_early_setup(void *fdt)
  174. {
  175. if (fdt)
  176. pr_info("FDT at %p\n", fdt);
  177. else {
  178. fdt = __dtb_start;
  179. pr_info("Compiled-in FDT at %p\n", fdt);
  180. }
  181. early_init_devtree(fdt);
  182. }
  183. static inline unsigned long extract_value_bits(unsigned long reg,
  184. short bit_nr, short width)
  185. {
  186. return (reg >> bit_nr) & (0 << width);
  187. }
  188. static inline unsigned long extract_value(unsigned long reg, unsigned long mask)
  189. {
  190. while (!(mask & 0x1)) {
  191. reg = reg >> 1;
  192. mask = mask >> 1;
  193. }
  194. return mask & reg;
  195. }
  196. void __init detect_unit_config(unsigned long upr, unsigned long mask,
  197. char *text, void (*func) (void))
  198. {
  199. if (text != NULL)
  200. printk("%s", text);
  201. if (upr & mask) {
  202. if (func != NULL)
  203. func();
  204. else
  205. printk("present\n");
  206. } else
  207. printk("not present\n");
  208. }
  209. /*
  210. * calibrate_delay
  211. *
  212. * Lightweight calibrate_delay implementation that calculates loops_per_jiffy
  213. * from the clock frequency passed in via the device tree
  214. *
  215. */
  216. void calibrate_delay(void)
  217. {
  218. const int *val;
  219. struct device_node *cpu = NULL;
  220. cpu = of_find_compatible_node(NULL, NULL, "opencores,or1200-rtlsvn481");
  221. val = of_get_property(cpu, "clock-frequency", NULL);
  222. if (!val)
  223. panic("no cpu 'clock-frequency' parameter in device tree");
  224. loops_per_jiffy = *val / HZ;
  225. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  226. loops_per_jiffy / (500000 / HZ),
  227. (loops_per_jiffy / (5000 / HZ)) % 100, loops_per_jiffy);
  228. }
  229. void __init setup_arch(char **cmdline_p)
  230. {
  231. unflatten_and_copy_device_tree();
  232. setup_cpuinfo();
  233. /* process 1's initial memory region is the kernel code/data */
  234. init_mm.start_code = (unsigned long)_stext;
  235. init_mm.end_code = (unsigned long)_etext;
  236. init_mm.end_data = (unsigned long)_edata;
  237. init_mm.brk = (unsigned long)_end;
  238. #ifdef CONFIG_BLK_DEV_INITRD
  239. initrd_start = (unsigned long)&__initrd_start;
  240. initrd_end = (unsigned long)&__initrd_end;
  241. if (initrd_start == initrd_end) {
  242. initrd_start = 0;
  243. initrd_end = 0;
  244. }
  245. initrd_below_start_ok = 1;
  246. #endif
  247. /* setup memblock allocator */
  248. setup_memory();
  249. /* paging_init() sets up the MMU and marks all pages as reserved */
  250. paging_init();
  251. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  252. if (!conswitchp)
  253. conswitchp = &dummy_con;
  254. #endif
  255. *cmdline_p = boot_command_line;
  256. printk(KERN_INFO "OpenRISC Linux -- http://openrisc.io\n");
  257. }
  258. static int show_cpuinfo(struct seq_file *m, void *v)
  259. {
  260. unsigned long vr;
  261. int version, revision;
  262. vr = mfspr(SPR_VR);
  263. version = (vr & SPR_VR_VER) >> 24;
  264. revision = vr & SPR_VR_REV;
  265. seq_printf(m,
  266. "cpu\t\t: OpenRISC-%x\n"
  267. "revision\t: %d\n"
  268. "frequency\t: %ld\n"
  269. "dcache size\t: %d bytes\n"
  270. "dcache block size\t: %d bytes\n"
  271. "dcache ways\t: %d\n"
  272. "icache size\t: %d bytes\n"
  273. "icache block size\t: %d bytes\n"
  274. "icache ways\t: %d\n"
  275. "immu\t\t: %d entries, %lu ways\n"
  276. "dmmu\t\t: %d entries, %lu ways\n"
  277. "bogomips\t: %lu.%02lu\n",
  278. version,
  279. revision,
  280. loops_per_jiffy * HZ,
  281. cpuinfo.dcache_size,
  282. cpuinfo.dcache_block_size,
  283. cpuinfo.dcache_ways,
  284. cpuinfo.icache_size,
  285. cpuinfo.icache_block_size,
  286. cpuinfo.icache_ways,
  287. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  288. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW),
  289. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  290. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW),
  291. (loops_per_jiffy * HZ) / 500000,
  292. ((loops_per_jiffy * HZ) / 5000) % 100);
  293. return 0;
  294. }
  295. static void *c_start(struct seq_file *m, loff_t * pos)
  296. {
  297. /* We only have one CPU... */
  298. return *pos < 1 ? (void *)1 : NULL;
  299. }
  300. static void *c_next(struct seq_file *m, void *v, loff_t * pos)
  301. {
  302. ++*pos;
  303. return NULL;
  304. }
  305. static void c_stop(struct seq_file *m, void *v)
  306. {
  307. }
  308. const struct seq_operations cpuinfo_op = {
  309. .start = c_start,
  310. .next = c_next,
  311. .stop = c_stop,
  312. .show = show_cpuinfo,
  313. };