setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_or1k cpuinfo_or1k[NR_CPUS];
  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. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  91. version = (vr & SPR_VR_VER) >> 24;
  92. revision = (vr & SPR_VR_REV);
  93. printk(KERN_INFO "CPU: OpenRISC-%x (revision %d) @%d MHz\n",
  94. version, revision, cpuinfo->clock_frequency / 1000000);
  95. if (!(upr & SPR_UPR_UP)) {
  96. printk(KERN_INFO
  97. "-- no UPR register... unable to detect configuration\n");
  98. return;
  99. }
  100. if (upr & SPR_UPR_DCP)
  101. printk(KERN_INFO
  102. "-- dcache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  103. cpuinfo->dcache_size, cpuinfo->dcache_block_size,
  104. cpuinfo->dcache_ways);
  105. else
  106. printk(KERN_INFO "-- dcache disabled\n");
  107. if (upr & SPR_UPR_ICP)
  108. printk(KERN_INFO
  109. "-- icache: %4d bytes total, %2d bytes/line, %d way(s)\n",
  110. cpuinfo->icache_size, cpuinfo->icache_block_size,
  111. cpuinfo->icache_ways);
  112. else
  113. printk(KERN_INFO "-- icache disabled\n");
  114. if (upr & SPR_UPR_DMP)
  115. printk(KERN_INFO "-- dmmu: %4d entries, %lu way(s)\n",
  116. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  117. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW));
  118. if (upr & SPR_UPR_IMP)
  119. printk(KERN_INFO "-- immu: %4d entries, %lu way(s)\n",
  120. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  121. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW));
  122. printk(KERN_INFO "-- additional features:\n");
  123. if (upr & SPR_UPR_DUP)
  124. printk(KERN_INFO "-- debug unit\n");
  125. if (upr & SPR_UPR_PCUP)
  126. printk(KERN_INFO "-- performance counters\n");
  127. if (upr & SPR_UPR_PMP)
  128. printk(KERN_INFO "-- power management\n");
  129. if (upr & SPR_UPR_PICP)
  130. printk(KERN_INFO "-- PIC\n");
  131. if (upr & SPR_UPR_TTP)
  132. printk(KERN_INFO "-- timer\n");
  133. if (upr & SPR_UPR_CUP)
  134. printk(KERN_INFO "-- custom unit(s)\n");
  135. }
  136. static struct device_node *setup_find_cpu_node(int cpu)
  137. {
  138. u32 hwid;
  139. struct device_node *cpun;
  140. struct device_node *cpus = of_find_node_by_path("/cpus");
  141. for_each_available_child_of_node(cpus, cpun) {
  142. if (of_property_read_u32(cpun, "reg", &hwid))
  143. continue;
  144. if (hwid == cpu)
  145. return cpun;
  146. }
  147. return NULL;
  148. }
  149. void __init setup_cpuinfo(void)
  150. {
  151. struct device_node *cpu;
  152. unsigned long iccfgr, dccfgr;
  153. unsigned long cache_set_size;
  154. int cpu_id = smp_processor_id();
  155. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[cpu_id];
  156. cpu = setup_find_cpu_node(cpu_id);
  157. if (!cpu)
  158. panic("Couldn't find CPU%d in device tree...\n", cpu_id);
  159. iccfgr = mfspr(SPR_ICCFGR);
  160. cpuinfo->icache_ways = 1 << (iccfgr & SPR_ICCFGR_NCW);
  161. cache_set_size = 1 << ((iccfgr & SPR_ICCFGR_NCS) >> 3);
  162. cpuinfo->icache_block_size = 16 << ((iccfgr & SPR_ICCFGR_CBS) >> 7);
  163. cpuinfo->icache_size =
  164. cache_set_size * cpuinfo->icache_ways * cpuinfo->icache_block_size;
  165. dccfgr = mfspr(SPR_DCCFGR);
  166. cpuinfo->dcache_ways = 1 << (dccfgr & SPR_DCCFGR_NCW);
  167. cache_set_size = 1 << ((dccfgr & SPR_DCCFGR_NCS) >> 3);
  168. cpuinfo->dcache_block_size = 16 << ((dccfgr & SPR_DCCFGR_CBS) >> 7);
  169. cpuinfo->dcache_size =
  170. cache_set_size * cpuinfo->dcache_ways * cpuinfo->dcache_block_size;
  171. if (of_property_read_u32(cpu, "clock-frequency",
  172. &cpuinfo->clock_frequency)) {
  173. printk(KERN_WARNING
  174. "Device tree missing CPU 'clock-frequency' parameter."
  175. "Assuming frequency 25MHZ"
  176. "This is probably not what you want.");
  177. }
  178. cpuinfo->coreid = mfspr(SPR_COREID);
  179. of_node_put(cpu);
  180. print_cpuinfo();
  181. }
  182. /**
  183. * or32_early_setup
  184. *
  185. * Handles the pointer to the device tree that this kernel is to use
  186. * for establishing the available platform devices.
  187. *
  188. * Falls back on built-in device tree in case null pointer is passed.
  189. */
  190. void __init or32_early_setup(void *fdt)
  191. {
  192. if (fdt)
  193. pr_info("FDT at %p\n", fdt);
  194. else {
  195. fdt = __dtb_start;
  196. pr_info("Compiled-in FDT at %p\n", fdt);
  197. }
  198. early_init_devtree(fdt);
  199. }
  200. static inline unsigned long extract_value_bits(unsigned long reg,
  201. short bit_nr, short width)
  202. {
  203. return (reg >> bit_nr) & (0 << width);
  204. }
  205. static inline unsigned long extract_value(unsigned long reg, unsigned long mask)
  206. {
  207. while (!(mask & 0x1)) {
  208. reg = reg >> 1;
  209. mask = mask >> 1;
  210. }
  211. return mask & reg;
  212. }
  213. void __init detect_unit_config(unsigned long upr, unsigned long mask,
  214. char *text, void (*func) (void))
  215. {
  216. if (text != NULL)
  217. printk("%s", text);
  218. if (upr & mask) {
  219. if (func != NULL)
  220. func();
  221. else
  222. printk("present\n");
  223. } else
  224. printk("not present\n");
  225. }
  226. /*
  227. * calibrate_delay
  228. *
  229. * Lightweight calibrate_delay implementation that calculates loops_per_jiffy
  230. * from the clock frequency passed in via the device tree
  231. *
  232. */
  233. void calibrate_delay(void)
  234. {
  235. const int *val;
  236. struct device_node *cpu = setup_find_cpu_node(smp_processor_id());
  237. val = of_get_property(cpu, "clock-frequency", NULL);
  238. if (!val)
  239. panic("no cpu 'clock-frequency' parameter in device tree");
  240. loops_per_jiffy = *val / HZ;
  241. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  242. loops_per_jiffy / (500000 / HZ),
  243. (loops_per_jiffy / (5000 / HZ)) % 100, loops_per_jiffy);
  244. }
  245. void __init setup_arch(char **cmdline_p)
  246. {
  247. unflatten_and_copy_device_tree();
  248. setup_cpuinfo();
  249. #ifdef CONFIG_SMP
  250. smp_init_cpus();
  251. #endif
  252. /* process 1's initial memory region is the kernel code/data */
  253. init_mm.start_code = (unsigned long)_stext;
  254. init_mm.end_code = (unsigned long)_etext;
  255. init_mm.end_data = (unsigned long)_edata;
  256. init_mm.brk = (unsigned long)_end;
  257. #ifdef CONFIG_BLK_DEV_INITRD
  258. initrd_start = (unsigned long)&__initrd_start;
  259. initrd_end = (unsigned long)&__initrd_end;
  260. if (initrd_start == initrd_end) {
  261. initrd_start = 0;
  262. initrd_end = 0;
  263. }
  264. initrd_below_start_ok = 1;
  265. #endif
  266. /* setup memblock allocator */
  267. setup_memory();
  268. /* paging_init() sets up the MMU and marks all pages as reserved */
  269. paging_init();
  270. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  271. if (!conswitchp)
  272. conswitchp = &dummy_con;
  273. #endif
  274. *cmdline_p = boot_command_line;
  275. printk(KERN_INFO "OpenRISC Linux -- http://openrisc.io\n");
  276. }
  277. static int show_cpuinfo(struct seq_file *m, void *v)
  278. {
  279. unsigned int vr, cpucfgr;
  280. unsigned int avr;
  281. unsigned int version;
  282. struct cpuinfo_or1k *cpuinfo = v;
  283. vr = mfspr(SPR_VR);
  284. cpucfgr = mfspr(SPR_CPUCFGR);
  285. #ifdef CONFIG_SMP
  286. seq_printf(m, "processor\t\t: %d\n", cpuinfo->coreid);
  287. #endif
  288. if (vr & SPR_VR_UVRP) {
  289. vr = mfspr(SPR_VR2);
  290. version = vr & SPR_VR2_VER;
  291. avr = mfspr(SPR_AVR);
  292. seq_printf(m, "cpu architecture\t: "
  293. "OpenRISC 1000 (%d.%d-rev%d)\n",
  294. (avr >> 24) & 0xff,
  295. (avr >> 16) & 0xff,
  296. (avr >> 8) & 0xff);
  297. seq_printf(m, "cpu implementation id\t: 0x%x\n",
  298. (vr & SPR_VR2_CPUID) >> 24);
  299. seq_printf(m, "cpu version\t\t: 0x%x\n", version);
  300. } else {
  301. version = (vr & SPR_VR_VER) >> 24;
  302. seq_printf(m, "cpu\t\t\t: OpenRISC-%x\n", version);
  303. seq_printf(m, "revision\t\t: %d\n", vr & SPR_VR_REV);
  304. }
  305. seq_printf(m, "frequency\t\t: %ld\n", loops_per_jiffy * HZ);
  306. seq_printf(m, "dcache size\t\t: %d bytes\n", cpuinfo->dcache_size);
  307. seq_printf(m, "dcache block size\t: %d bytes\n",
  308. cpuinfo->dcache_block_size);
  309. seq_printf(m, "dcache ways\t\t: %d\n", cpuinfo->dcache_ways);
  310. seq_printf(m, "icache size\t\t: %d bytes\n", cpuinfo->icache_size);
  311. seq_printf(m, "icache block size\t: %d bytes\n",
  312. cpuinfo->icache_block_size);
  313. seq_printf(m, "icache ways\t\t: %d\n", cpuinfo->icache_ways);
  314. seq_printf(m, "immu\t\t\t: %d entries, %lu ways\n",
  315. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  316. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW));
  317. seq_printf(m, "dmmu\t\t\t: %d entries, %lu ways\n",
  318. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  319. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW));
  320. seq_printf(m, "bogomips\t\t: %lu.%02lu\n",
  321. (loops_per_jiffy * HZ) / 500000,
  322. ((loops_per_jiffy * HZ) / 5000) % 100);
  323. seq_puts(m, "features\t\t: ");
  324. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OB32S ? "orbis32" : "");
  325. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OB64S ? "orbis64" : "");
  326. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OF32S ? "orfpx32" : "");
  327. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OF64S ? "orfpx64" : "");
  328. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OV64S ? "orvdx64" : "");
  329. seq_puts(m, "\n");
  330. seq_puts(m, "\n");
  331. return 0;
  332. }
  333. static void *c_start(struct seq_file *m, loff_t *pos)
  334. {
  335. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  336. if ((*pos) < nr_cpu_ids)
  337. return &cpuinfo_or1k[*pos];
  338. return NULL;
  339. }
  340. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  341. {
  342. (*pos)++;
  343. return c_start(m, pos);
  344. }
  345. static void c_stop(struct seq_file *m, void *v)
  346. {
  347. }
  348. const struct seq_operations cpuinfo_op = {
  349. .start = c_start,
  350. .next = c_next,
  351. .stop = c_stop,
  352. .show = show_cpuinfo,
  353. };