setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/memblock.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/device.h>
  39. #include <asm/sections.h>
  40. #include <asm/segment.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/types.h>
  43. #include <asm/setup.h>
  44. #include <asm/io.h>
  45. #include <asm/cpuinfo.h>
  46. #include <asm/delay.h>
  47. #include "vmlinux.h"
  48. static void __init setup_memory(void)
  49. {
  50. unsigned long ram_start_pfn;
  51. unsigned long ram_end_pfn;
  52. phys_addr_t memory_start, memory_end;
  53. struct memblock_region *region;
  54. memory_end = memory_start = 0;
  55. /* Find main memory where is the kernel, we assume its the only one */
  56. for_each_memblock(memory, region) {
  57. memory_start = region->base;
  58. memory_end = region->base + region->size;
  59. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  60. memory_start, memory_end);
  61. }
  62. if (!memory_end) {
  63. panic("No memory!");
  64. }
  65. ram_start_pfn = PFN_UP(memory_start);
  66. ram_end_pfn = PFN_DOWN(memblock_end_of_DRAM());
  67. /* setup bootmem globals (we use no_bootmem, but mm still depends on this) */
  68. min_low_pfn = ram_start_pfn;
  69. max_low_pfn = ram_end_pfn;
  70. max_pfn = ram_end_pfn;
  71. /*
  72. * initialize the boot-time allocator (with low memory only).
  73. *
  74. * This makes the memory from the end of the kernel to the end of
  75. * RAM usable.
  76. */
  77. memblock_reserve(__pa(_stext), _end - _stext);
  78. early_init_fdt_reserve_self();
  79. early_init_fdt_scan_reserved_mem();
  80. memblock_dump_all();
  81. }
  82. struct cpuinfo_or1k cpuinfo_or1k[NR_CPUS];
  83. static void print_cpuinfo(void)
  84. {
  85. unsigned long upr = mfspr(SPR_UPR);
  86. unsigned long vr = mfspr(SPR_VR);
  87. unsigned int version;
  88. unsigned int revision;
  89. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  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. static struct device_node *setup_find_cpu_node(int cpu)
  136. {
  137. u32 hwid;
  138. struct device_node *cpun;
  139. for_each_of_cpu_node(cpun) {
  140. if (of_property_read_u32(cpun, "reg", &hwid))
  141. continue;
  142. if (hwid == cpu)
  143. return cpun;
  144. }
  145. return NULL;
  146. }
  147. void __init setup_cpuinfo(void)
  148. {
  149. struct device_node *cpu;
  150. unsigned long iccfgr, dccfgr;
  151. unsigned long cache_set_size;
  152. int cpu_id = smp_processor_id();
  153. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[cpu_id];
  154. cpu = setup_find_cpu_node(cpu_id);
  155. if (!cpu)
  156. panic("Couldn't find CPU%d in device tree...\n", cpu_id);
  157. iccfgr = mfspr(SPR_ICCFGR);
  158. cpuinfo->icache_ways = 1 << (iccfgr & SPR_ICCFGR_NCW);
  159. cache_set_size = 1 << ((iccfgr & SPR_ICCFGR_NCS) >> 3);
  160. cpuinfo->icache_block_size = 16 << ((iccfgr & SPR_ICCFGR_CBS) >> 7);
  161. cpuinfo->icache_size =
  162. cache_set_size * cpuinfo->icache_ways * cpuinfo->icache_block_size;
  163. dccfgr = mfspr(SPR_DCCFGR);
  164. cpuinfo->dcache_ways = 1 << (dccfgr & SPR_DCCFGR_NCW);
  165. cache_set_size = 1 << ((dccfgr & SPR_DCCFGR_NCS) >> 3);
  166. cpuinfo->dcache_block_size = 16 << ((dccfgr & SPR_DCCFGR_CBS) >> 7);
  167. cpuinfo->dcache_size =
  168. cache_set_size * cpuinfo->dcache_ways * cpuinfo->dcache_block_size;
  169. if (of_property_read_u32(cpu, "clock-frequency",
  170. &cpuinfo->clock_frequency)) {
  171. printk(KERN_WARNING
  172. "Device tree missing CPU 'clock-frequency' parameter."
  173. "Assuming frequency 25MHZ"
  174. "This is probably not what you want.");
  175. }
  176. cpuinfo->coreid = mfspr(SPR_COREID);
  177. of_node_put(cpu);
  178. print_cpuinfo();
  179. }
  180. /**
  181. * or32_early_setup
  182. *
  183. * Handles the pointer to the device tree that this kernel is to use
  184. * for establishing the available platform devices.
  185. *
  186. * Falls back on built-in device tree in case null pointer is passed.
  187. */
  188. void __init or32_early_setup(void *fdt)
  189. {
  190. if (fdt)
  191. pr_info("FDT at %p\n", fdt);
  192. else {
  193. fdt = __dtb_start;
  194. pr_info("Compiled-in FDT at %p\n", fdt);
  195. }
  196. early_init_devtree(fdt);
  197. }
  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 = setup_find_cpu_node(smp_processor_id());
  235. val = of_get_property(cpu, "clock-frequency", NULL);
  236. if (!val)
  237. panic("no cpu 'clock-frequency' parameter in device tree");
  238. loops_per_jiffy = *val / HZ;
  239. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  240. loops_per_jiffy / (500000 / HZ),
  241. (loops_per_jiffy / (5000 / HZ)) % 100, loops_per_jiffy);
  242. }
  243. void __init setup_arch(char **cmdline_p)
  244. {
  245. unflatten_and_copy_device_tree();
  246. setup_cpuinfo();
  247. #ifdef CONFIG_SMP
  248. smp_init_cpus();
  249. #endif
  250. /* process 1's initial memory region is the kernel code/data */
  251. init_mm.start_code = (unsigned long)_stext;
  252. init_mm.end_code = (unsigned long)_etext;
  253. init_mm.end_data = (unsigned long)_edata;
  254. init_mm.brk = (unsigned long)_end;
  255. #ifdef CONFIG_BLK_DEV_INITRD
  256. initrd_start = (unsigned long)&__initrd_start;
  257. initrd_end = (unsigned long)&__initrd_end;
  258. if (initrd_start == initrd_end) {
  259. initrd_start = 0;
  260. initrd_end = 0;
  261. }
  262. initrd_below_start_ok = 1;
  263. #endif
  264. /* setup memblock allocator */
  265. setup_memory();
  266. /* paging_init() sets up the MMU and marks all pages as reserved */
  267. paging_init();
  268. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  269. if (!conswitchp)
  270. conswitchp = &dummy_con;
  271. #endif
  272. *cmdline_p = boot_command_line;
  273. printk(KERN_INFO "OpenRISC Linux -- http://openrisc.io\n");
  274. }
  275. static int show_cpuinfo(struct seq_file *m, void *v)
  276. {
  277. unsigned int vr, cpucfgr;
  278. unsigned int avr;
  279. unsigned int version;
  280. struct cpuinfo_or1k *cpuinfo = v;
  281. vr = mfspr(SPR_VR);
  282. cpucfgr = mfspr(SPR_CPUCFGR);
  283. #ifdef CONFIG_SMP
  284. seq_printf(m, "processor\t\t: %d\n", cpuinfo->coreid);
  285. #endif
  286. if (vr & SPR_VR_UVRP) {
  287. vr = mfspr(SPR_VR2);
  288. version = vr & SPR_VR2_VER;
  289. avr = mfspr(SPR_AVR);
  290. seq_printf(m, "cpu architecture\t: "
  291. "OpenRISC 1000 (%d.%d-rev%d)\n",
  292. (avr >> 24) & 0xff,
  293. (avr >> 16) & 0xff,
  294. (avr >> 8) & 0xff);
  295. seq_printf(m, "cpu implementation id\t: 0x%x\n",
  296. (vr & SPR_VR2_CPUID) >> 24);
  297. seq_printf(m, "cpu version\t\t: 0x%x\n", version);
  298. } else {
  299. version = (vr & SPR_VR_VER) >> 24;
  300. seq_printf(m, "cpu\t\t\t: OpenRISC-%x\n", version);
  301. seq_printf(m, "revision\t\t: %d\n", vr & SPR_VR_REV);
  302. }
  303. seq_printf(m, "frequency\t\t: %ld\n", loops_per_jiffy * HZ);
  304. seq_printf(m, "dcache size\t\t: %d bytes\n", cpuinfo->dcache_size);
  305. seq_printf(m, "dcache block size\t: %d bytes\n",
  306. cpuinfo->dcache_block_size);
  307. seq_printf(m, "dcache ways\t\t: %d\n", cpuinfo->dcache_ways);
  308. seq_printf(m, "icache size\t\t: %d bytes\n", cpuinfo->icache_size);
  309. seq_printf(m, "icache block size\t: %d bytes\n",
  310. cpuinfo->icache_block_size);
  311. seq_printf(m, "icache ways\t\t: %d\n", cpuinfo->icache_ways);
  312. seq_printf(m, "immu\t\t\t: %d entries, %lu ways\n",
  313. 1 << ((mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTS) >> 2),
  314. 1 + (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_NTW));
  315. seq_printf(m, "dmmu\t\t\t: %d entries, %lu ways\n",
  316. 1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> 2),
  317. 1 + (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTW));
  318. seq_printf(m, "bogomips\t\t: %lu.%02lu\n",
  319. (loops_per_jiffy * HZ) / 500000,
  320. ((loops_per_jiffy * HZ) / 5000) % 100);
  321. seq_puts(m, "features\t\t: ");
  322. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OB32S ? "orbis32" : "");
  323. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OB64S ? "orbis64" : "");
  324. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OF32S ? "orfpx32" : "");
  325. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OF64S ? "orfpx64" : "");
  326. seq_printf(m, "%s ", cpucfgr & SPR_CPUCFGR_OV64S ? "orvdx64" : "");
  327. seq_puts(m, "\n");
  328. seq_puts(m, "\n");
  329. return 0;
  330. }
  331. static void *c_start(struct seq_file *m, loff_t *pos)
  332. {
  333. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  334. if ((*pos) < nr_cpu_ids)
  335. return &cpuinfo_or1k[*pos];
  336. return NULL;
  337. }
  338. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  339. {
  340. (*pos)++;
  341. return c_start(m, pos);
  342. }
  343. static void c_stop(struct seq_file *m, void *v)
  344. {
  345. }
  346. const struct seq_operations cpuinfo_op = {
  347. .start = c_start,
  348. .next = c_next,
  349. .stop = c_stop,
  350. .show = show_cpuinfo,
  351. };