setup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/kernel/setup.c
  4. *
  5. * This file handles the architecture-dependent parts of initialization
  6. *
  7. * Copyright (C) 1999 Niibe Yutaka
  8. * Copyright (C) 2002 - 2010 Paul Mundt
  9. */
  10. #include <linux/ioport.h>
  11. #include <linux/init.h>
  12. #include <linux/initrd.h>
  13. #include <linux/console.h>
  14. #include <linux/root_dev.h>
  15. #include <linux/utsname.h>
  16. #include <linux/nodemask.h>
  17. #include <linux/cpu.h>
  18. #include <linux/pfn.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/kexec.h>
  22. #include <linux/module.h>
  23. #include <linux/smp.h>
  24. #include <linux/err.h>
  25. #include <linux/crash_dump.h>
  26. #include <linux/mmzone.h>
  27. #include <linux/clk.h>
  28. #include <linux/delay.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/memblock.h>
  31. #include <linux/of.h>
  32. #include <linux/of_fdt.h>
  33. #include <linux/uaccess.h>
  34. #include <uapi/linux/mount.h>
  35. #include <asm/io.h>
  36. #include <asm/page.h>
  37. #include <asm/elf.h>
  38. #include <asm/sections.h>
  39. #include <asm/irq.h>
  40. #include <asm/setup.h>
  41. #include <asm/clock.h>
  42. #include <asm/smp.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/mmzone.h>
  45. #include <asm/processor.h>
  46. #include <asm/sparsemem.h>
  47. #include <asm/platform_early.h>
  48. /*
  49. * Initialize loops_per_jiffy as 10000000 (1000MIPS).
  50. * This value will be used at the very early stage of serial setup.
  51. * The bigger value means no problem.
  52. */
  53. struct sh_cpuinfo cpu_data[NR_CPUS] __read_mostly = {
  54. [0] = {
  55. .type = CPU_SH_NONE,
  56. .family = CPU_FAMILY_UNKNOWN,
  57. .loops_per_jiffy = 10000000,
  58. .phys_bits = MAX_PHYSMEM_BITS,
  59. },
  60. };
  61. EXPORT_SYMBOL(cpu_data);
  62. /*
  63. * The machine vector. First entry in .machvec.init, or clobbered by
  64. * sh_mv= on the command line, prior to .machvec.init teardown.
  65. */
  66. struct sh_machine_vector sh_mv = { .mv_name = "generic", };
  67. EXPORT_SYMBOL(sh_mv);
  68. extern int root_mountflags;
  69. #define RAMDISK_IMAGE_START_MASK 0x07FF
  70. #define RAMDISK_PROMPT_FLAG 0x8000
  71. #define RAMDISK_LOAD_FLAG 0x4000
  72. static char __initdata command_line[COMMAND_LINE_SIZE] = { 0, };
  73. static struct resource code_resource = {
  74. .name = "Kernel code",
  75. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  76. };
  77. static struct resource data_resource = {
  78. .name = "Kernel data",
  79. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  80. };
  81. static struct resource bss_resource = {
  82. .name = "Kernel bss",
  83. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  84. };
  85. unsigned long memory_start;
  86. EXPORT_SYMBOL(memory_start);
  87. unsigned long memory_end = 0;
  88. EXPORT_SYMBOL(memory_end);
  89. unsigned long memory_limit = 0;
  90. static struct resource mem_resources[MAX_NUMNODES];
  91. int l1i_cache_shape, l1d_cache_shape, l2_cache_shape;
  92. static int __init early_parse_mem(char *p)
  93. {
  94. if (!p)
  95. return 1;
  96. memory_limit = PAGE_ALIGN(memparse(p, &p));
  97. pr_notice("Memory limited to %ldMB\n", memory_limit >> 20);
  98. return 0;
  99. }
  100. early_param("mem", early_parse_mem);
  101. void __init check_for_initrd(void)
  102. {
  103. #ifdef CONFIG_BLK_DEV_INITRD
  104. unsigned long start, end;
  105. /*
  106. * Check for the rare cases where boot loaders adhere to the boot
  107. * ABI.
  108. */
  109. if (!LOADER_TYPE || !INITRD_START || !INITRD_SIZE)
  110. goto disable;
  111. start = INITRD_START + __MEMORY_START;
  112. end = start + INITRD_SIZE;
  113. if (unlikely(end <= start))
  114. goto disable;
  115. if (unlikely(start & ~PAGE_MASK)) {
  116. pr_err("initrd must be page aligned\n");
  117. goto disable;
  118. }
  119. if (unlikely(start < __MEMORY_START)) {
  120. pr_err("initrd start (%08lx) < __MEMORY_START(%x)\n",
  121. start, __MEMORY_START);
  122. goto disable;
  123. }
  124. if (unlikely(end > memblock_end_of_DRAM())) {
  125. pr_err("initrd extends beyond end of memory "
  126. "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
  127. end, (unsigned long)memblock_end_of_DRAM());
  128. goto disable;
  129. }
  130. /*
  131. * If we got this far in spite of the boot loader's best efforts
  132. * to the contrary, assume we actually have a valid initrd and
  133. * fix up the root dev.
  134. */
  135. ROOT_DEV = Root_RAM0;
  136. /*
  137. * Address sanitization
  138. */
  139. initrd_start = (unsigned long)__va(start);
  140. initrd_end = initrd_start + INITRD_SIZE;
  141. memblock_reserve(__pa(initrd_start), INITRD_SIZE);
  142. return;
  143. disable:
  144. pr_info("initrd disabled\n");
  145. initrd_start = initrd_end = 0;
  146. #endif
  147. }
  148. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  149. void calibrate_delay(void)
  150. {
  151. struct clk *clk = clk_get(NULL, "cpu_clk");
  152. if (IS_ERR(clk))
  153. panic("Need a sane CPU clock definition!");
  154. loops_per_jiffy = (clk_get_rate(clk) >> 1) / HZ;
  155. printk(KERN_INFO "Calibrating delay loop (skipped)... "
  156. "%lu.%02lu BogoMIPS PRESET (lpj=%lu)\n",
  157. loops_per_jiffy/(500000/HZ),
  158. (loops_per_jiffy/(5000/HZ)) % 100,
  159. loops_per_jiffy);
  160. }
  161. #endif
  162. void __init __add_active_range(unsigned int nid, unsigned long start_pfn,
  163. unsigned long end_pfn)
  164. {
  165. struct resource *res = &mem_resources[nid];
  166. unsigned long start, end;
  167. WARN_ON(res->name); /* max one active range per node for now */
  168. start = start_pfn << PAGE_SHIFT;
  169. end = end_pfn << PAGE_SHIFT;
  170. res->name = "System RAM";
  171. res->start = start;
  172. res->end = end - 1;
  173. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  174. if (request_resource(&iomem_resource, res)) {
  175. pr_err("unable to request memory_resource 0x%lx 0x%lx\n",
  176. start_pfn, end_pfn);
  177. return;
  178. }
  179. /*
  180. * We don't know which RAM region contains kernel data or
  181. * the reserved crashkernel region, so try it repeatedly
  182. * and let the resource manager test it.
  183. */
  184. request_resource(res, &code_resource);
  185. request_resource(res, &data_resource);
  186. request_resource(res, &bss_resource);
  187. #ifdef CONFIG_CRASH_RESERVE
  188. request_resource(res, &crashk_res);
  189. #endif
  190. /*
  191. * Also make sure that there is a PMB mapping that covers this
  192. * range before we attempt to activate it, to avoid reset by MMU.
  193. * We can hit this path with NUMA or memory hot-add.
  194. */
  195. pmb_bolt_mapping((unsigned long)__va(start), start, end - start,
  196. PAGE_KERNEL);
  197. memblock_set_node(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn - start_pfn),
  198. &memblock.memory, nid);
  199. }
  200. void __init __weak plat_early_device_setup(void)
  201. {
  202. }
  203. #ifdef CONFIG_OF_EARLY_FLATTREE
  204. void __ref sh_fdt_init(phys_addr_t dt_phys)
  205. {
  206. static int done = 0;
  207. void *dt_virt;
  208. /* Avoid calling an __init function on secondary cpus. */
  209. if (done) return;
  210. #ifdef CONFIG_USE_BUILTIN_DTB
  211. dt_virt = __dtb_start;
  212. #else
  213. dt_virt = phys_to_virt(dt_phys);
  214. #endif
  215. if (!dt_virt || !early_init_dt_scan(dt_virt, __pa(dt_virt))) {
  216. pr_crit("Error: invalid device tree blob"
  217. " at physical address %p\n", (void *)dt_phys);
  218. while (true)
  219. cpu_relax();
  220. }
  221. done = 1;
  222. }
  223. #endif
  224. void __init setup_arch(char **cmdline_p)
  225. {
  226. enable_mmu();
  227. ROOT_DEV = old_decode_dev(ORIG_ROOT_DEV);
  228. printk(KERN_NOTICE "Boot params:\n"
  229. "... MOUNT_ROOT_RDONLY - %08lx\n"
  230. "... RAMDISK_FLAGS - %08lx\n"
  231. "... ORIG_ROOT_DEV - %08lx\n"
  232. "... LOADER_TYPE - %08lx\n"
  233. "... INITRD_START - %08lx\n"
  234. "... INITRD_SIZE - %08lx\n",
  235. MOUNT_ROOT_RDONLY, RAMDISK_FLAGS,
  236. ORIG_ROOT_DEV, LOADER_TYPE,
  237. INITRD_START, INITRD_SIZE);
  238. #ifdef CONFIG_BLK_DEV_RAM
  239. rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
  240. #endif
  241. if (!MOUNT_ROOT_RDONLY)
  242. root_mountflags &= ~MS_RDONLY;
  243. setup_initial_init_mm(_text, _etext, _edata, _end);
  244. code_resource.start = virt_to_phys(_text);
  245. code_resource.end = virt_to_phys(_etext)-1;
  246. data_resource.start = virt_to_phys(_etext);
  247. data_resource.end = virt_to_phys(_edata)-1;
  248. bss_resource.start = virt_to_phys(__bss_start);
  249. bss_resource.end = virt_to_phys(__bss_stop)-1;
  250. #ifdef CONFIG_CMDLINE_OVERWRITE
  251. strscpy(command_line, CONFIG_CMDLINE, sizeof(command_line));
  252. #else
  253. strscpy(command_line, COMMAND_LINE, sizeof(command_line));
  254. #ifdef CONFIG_CMDLINE_EXTEND
  255. strlcat(command_line, " ", sizeof(command_line));
  256. strlcat(command_line, CONFIG_CMDLINE, sizeof(command_line));
  257. #endif
  258. #endif
  259. /* Save unparsed command line copy for /proc/cmdline */
  260. memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  261. *cmdline_p = command_line;
  262. parse_early_param();
  263. plat_early_device_setup();
  264. sh_mv_setup();
  265. /* Let earlyprintk output early console messages */
  266. sh_early_platform_driver_probe("earlyprintk", 1, 1);
  267. #ifdef CONFIG_OF_EARLY_FLATTREE
  268. #ifdef CONFIG_USE_BUILTIN_DTB
  269. unflatten_and_copy_device_tree();
  270. #else
  271. unflatten_device_tree();
  272. #endif
  273. #endif
  274. paging_init();
  275. /* Perform the machine specific initialisation */
  276. if (likely(sh_mv.mv_setup))
  277. sh_mv.mv_setup(cmdline_p);
  278. plat_smp_setup();
  279. }
  280. /* processor boot mode configuration */
  281. int generic_mode_pins(void)
  282. {
  283. pr_warn("generic_mode_pins(): missing mode pin configuration\n");
  284. return 0;
  285. }
  286. int test_mode_pin(int pin)
  287. {
  288. return sh_mv.mv_mode_pins() & pin;
  289. }
  290. void __init arch_cpu_finalize_init(void)
  291. {
  292. char *p = &init_utsname()->machine[2]; /* "sh" */
  293. select_idle_routine();
  294. current_cpu_data.loops_per_jiffy = loops_per_jiffy;
  295. switch (current_cpu_data.family) {
  296. case CPU_FAMILY_SH2:
  297. *p++ = '2';
  298. break;
  299. case CPU_FAMILY_SH2A:
  300. *p++ = '2';
  301. *p++ = 'a';
  302. break;
  303. case CPU_FAMILY_SH3:
  304. *p++ = '3';
  305. break;
  306. case CPU_FAMILY_SH4:
  307. *p++ = '4';
  308. break;
  309. case CPU_FAMILY_SH4A:
  310. *p++ = '4';
  311. *p++ = 'a';
  312. break;
  313. case CPU_FAMILY_SH4AL_DSP:
  314. *p++ = '4';
  315. *p++ = 'a';
  316. *p++ = 'l';
  317. *p++ = '-';
  318. *p++ = 'd';
  319. *p++ = 's';
  320. *p++ = 'p';
  321. break;
  322. case CPU_FAMILY_UNKNOWN:
  323. /*
  324. * Specifically use CPU_FAMILY_UNKNOWN rather than
  325. * default:, so we're able to have the compiler whine
  326. * about unhandled enumerations.
  327. */
  328. break;
  329. }
  330. pr_info("CPU: %s\n", get_cpu_subtype(&current_cpu_data));
  331. #ifndef __LITTLE_ENDIAN__
  332. /* 'eb' means 'Endian Big' */
  333. *p++ = 'e';
  334. *p++ = 'b';
  335. #endif
  336. *p = '\0';
  337. }