setup.c 8.5 KB

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