setup.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004-2007 Cavium Networks
  7. * Copyright (C) 2008, 2009 Wind River Systems
  8. * written by Ralf Baechle <ralf@linux-mips.org>
  9. */
  10. #include <linux/compiler.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/console.h>
  15. #include <linux/delay.h>
  16. #include <linux/export.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/serial.h>
  20. #include <linux/smp.h>
  21. #include <linux/types.h>
  22. #include <linux/string.h> /* for memset */
  23. #include <linux/tty.h>
  24. #include <linux/time.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/serial_8250.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/libfdt.h>
  30. #include <linux/kexec.h>
  31. #include <asm/processor.h>
  32. #include <asm/reboot.h>
  33. #include <asm/smp-ops.h>
  34. #include <asm/irq_cpu.h>
  35. #include <asm/mipsregs.h>
  36. #include <asm/bootinfo.h>
  37. #include <asm/sections.h>
  38. #include <asm/time.h>
  39. #include <asm/octeon/octeon.h>
  40. #include <asm/octeon/pci-octeon.h>
  41. #include <asm/octeon/cvmx-mio-defs.h>
  42. extern struct plat_smp_ops octeon_smp_ops;
  43. #ifdef CONFIG_PCI
  44. extern void pci_console_init(const char *arg);
  45. #endif
  46. static unsigned long long MAX_MEMORY = 512ull << 20;
  47. struct octeon_boot_descriptor *octeon_boot_desc_ptr;
  48. struct cvmx_bootinfo *octeon_bootinfo;
  49. EXPORT_SYMBOL(octeon_bootinfo);
  50. static unsigned long long RESERVE_LOW_MEM = 0ull;
  51. #ifdef CONFIG_KEXEC
  52. #ifdef CONFIG_SMP
  53. /*
  54. * Wait for relocation code is prepared and send
  55. * secondary CPUs to spin until kernel is relocated.
  56. */
  57. static void octeon_kexec_smp_down(void *ignored)
  58. {
  59. int cpu = smp_processor_id();
  60. local_irq_disable();
  61. set_cpu_online(cpu, false);
  62. while (!atomic_read(&kexec_ready_to_reboot))
  63. cpu_relax();
  64. asm volatile (
  65. " sync \n"
  66. " synci ($0) \n");
  67. relocated_kexec_smp_wait(NULL);
  68. }
  69. #endif
  70. #define OCTEON_DDR0_BASE (0x0ULL)
  71. #define OCTEON_DDR0_SIZE (0x010000000ULL)
  72. #define OCTEON_DDR1_BASE (0x410000000ULL)
  73. #define OCTEON_DDR1_SIZE (0x010000000ULL)
  74. #define OCTEON_DDR2_BASE (0x020000000ULL)
  75. #define OCTEON_DDR2_SIZE (0x3e0000000ULL)
  76. #define OCTEON_MAX_PHY_MEM_SIZE (16*1024*1024*1024ULL)
  77. static struct kimage *kimage_ptr;
  78. static void kexec_bootmem_init(uint64_t mem_size, uint32_t low_reserved_bytes)
  79. {
  80. int64_t addr;
  81. struct cvmx_bootmem_desc *bootmem_desc;
  82. bootmem_desc = cvmx_bootmem_get_desc();
  83. if (mem_size > OCTEON_MAX_PHY_MEM_SIZE) {
  84. mem_size = OCTEON_MAX_PHY_MEM_SIZE;
  85. pr_err("Error: requested memory too large,"
  86. "truncating to maximum size\n");
  87. }
  88. bootmem_desc->major_version = CVMX_BOOTMEM_DESC_MAJ_VER;
  89. bootmem_desc->minor_version = CVMX_BOOTMEM_DESC_MIN_VER;
  90. addr = (OCTEON_DDR0_BASE + RESERVE_LOW_MEM + low_reserved_bytes);
  91. bootmem_desc->head_addr = 0;
  92. if (mem_size <= OCTEON_DDR0_SIZE) {
  93. __cvmx_bootmem_phy_free(addr,
  94. mem_size - RESERVE_LOW_MEM -
  95. low_reserved_bytes, 0);
  96. return;
  97. }
  98. __cvmx_bootmem_phy_free(addr,
  99. OCTEON_DDR0_SIZE - RESERVE_LOW_MEM -
  100. low_reserved_bytes, 0);
  101. mem_size -= OCTEON_DDR0_SIZE;
  102. if (mem_size > OCTEON_DDR1_SIZE) {
  103. __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, OCTEON_DDR1_SIZE, 0);
  104. __cvmx_bootmem_phy_free(OCTEON_DDR2_BASE,
  105. mem_size - OCTEON_DDR1_SIZE, 0);
  106. } else
  107. __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, mem_size, 0);
  108. }
  109. static int octeon_kexec_prepare(struct kimage *image)
  110. {
  111. int i;
  112. char *bootloader = "kexec";
  113. octeon_boot_desc_ptr->argc = 0;
  114. for (i = 0; i < image->nr_segments; i++) {
  115. if (!strncmp(bootloader, (char *)image->segment[i].buf,
  116. strlen(bootloader))) {
  117. /*
  118. * convert command line string to array
  119. * of parameters (as bootloader does).
  120. */
  121. int argc = 0, offt;
  122. char *str = (char *)image->segment[i].buf;
  123. char *ptr = strchr(str, ' ');
  124. while (ptr && (OCTEON_ARGV_MAX_ARGS > argc)) {
  125. *ptr = '\0';
  126. if (ptr[1] != ' ') {
  127. offt = (int)(ptr - str + 1);
  128. octeon_boot_desc_ptr->argv[argc] =
  129. image->segment[i].mem + offt;
  130. argc++;
  131. }
  132. ptr = strchr(ptr + 1, ' ');
  133. }
  134. octeon_boot_desc_ptr->argc = argc;
  135. break;
  136. }
  137. }
  138. /*
  139. * Information about segments will be needed during pre-boot memory
  140. * initialization.
  141. */
  142. kimage_ptr = image;
  143. return 0;
  144. }
  145. static void octeon_generic_shutdown(void)
  146. {
  147. int i;
  148. #ifdef CONFIG_SMP
  149. int cpu;
  150. #endif
  151. struct cvmx_bootmem_desc *bootmem_desc;
  152. void *named_block_array_ptr;
  153. bootmem_desc = cvmx_bootmem_get_desc();
  154. named_block_array_ptr =
  155. cvmx_phys_to_ptr(bootmem_desc->named_block_array_addr);
  156. #ifdef CONFIG_SMP
  157. /* disable watchdogs */
  158. for_each_online_cpu(cpu)
  159. cvmx_write_csr(CVMX_CIU_WDOGX(cpu_logical_map(cpu)), 0);
  160. #else
  161. cvmx_write_csr(CVMX_CIU_WDOGX(cvmx_get_core_num()), 0);
  162. #endif
  163. if (kimage_ptr != kexec_crash_image) {
  164. memset(named_block_array_ptr,
  165. 0x0,
  166. CVMX_BOOTMEM_NUM_NAMED_BLOCKS *
  167. sizeof(struct cvmx_bootmem_named_block_desc));
  168. /*
  169. * Mark all memory (except low 0x100000 bytes) as free.
  170. * It is the same thing that bootloader does.
  171. */
  172. kexec_bootmem_init(octeon_bootinfo->dram_size*1024ULL*1024ULL,
  173. 0x100000);
  174. /*
  175. * Allocate all segments to avoid their corruption during boot.
  176. */
  177. for (i = 0; i < kimage_ptr->nr_segments; i++)
  178. cvmx_bootmem_alloc_address(
  179. kimage_ptr->segment[i].memsz + 2*PAGE_SIZE,
  180. kimage_ptr->segment[i].mem - PAGE_SIZE,
  181. PAGE_SIZE);
  182. } else {
  183. /*
  184. * Do not mark all memory as free. Free only named sections
  185. * leaving the rest of memory unchanged.
  186. */
  187. struct cvmx_bootmem_named_block_desc *ptr =
  188. (struct cvmx_bootmem_named_block_desc *)
  189. named_block_array_ptr;
  190. for (i = 0; i < bootmem_desc->named_block_num_blocks; i++)
  191. if (ptr[i].size)
  192. cvmx_bootmem_free_named(ptr[i].name);
  193. }
  194. kexec_args[2] = 1UL; /* running on octeon_main_processor */
  195. kexec_args[3] = (unsigned long)octeon_boot_desc_ptr;
  196. #ifdef CONFIG_SMP
  197. secondary_kexec_args[2] = 0UL; /* running on secondary cpu */
  198. secondary_kexec_args[3] = (unsigned long)octeon_boot_desc_ptr;
  199. #endif
  200. }
  201. static void octeon_shutdown(void)
  202. {
  203. octeon_generic_shutdown();
  204. #ifdef CONFIG_SMP
  205. smp_call_function(octeon_kexec_smp_down, NULL, 0);
  206. smp_wmb();
  207. while (num_online_cpus() > 1) {
  208. cpu_relax();
  209. mdelay(1);
  210. }
  211. #endif
  212. }
  213. static void octeon_crash_shutdown(struct pt_regs *regs)
  214. {
  215. octeon_generic_shutdown();
  216. default_machine_crash_shutdown(regs);
  217. }
  218. #endif /* CONFIG_KEXEC */
  219. #ifdef CONFIG_CAVIUM_RESERVE32
  220. uint64_t octeon_reserve32_memory;
  221. EXPORT_SYMBOL(octeon_reserve32_memory);
  222. #endif
  223. #ifdef CONFIG_KEXEC
  224. /* crashkernel cmdline parameter is parsed _after_ memory setup
  225. * we also parse it here (workaround for EHB5200) */
  226. static uint64_t crashk_size, crashk_base;
  227. #endif
  228. static int octeon_uart;
  229. extern asmlinkage void handle_int(void);
  230. /**
  231. * Return non zero if we are currently running in the Octeon simulator
  232. *
  233. * Returns
  234. */
  235. int octeon_is_simulation(void)
  236. {
  237. return octeon_bootinfo->board_type == CVMX_BOARD_TYPE_SIM;
  238. }
  239. EXPORT_SYMBOL(octeon_is_simulation);
  240. /**
  241. * Return true if Octeon is in PCI Host mode. This means
  242. * Linux can control the PCI bus.
  243. *
  244. * Returns Non zero if Octeon in host mode.
  245. */
  246. int octeon_is_pci_host(void)
  247. {
  248. #ifdef CONFIG_PCI
  249. return octeon_bootinfo->config_flags & CVMX_BOOTINFO_CFG_FLAG_PCI_HOST;
  250. #else
  251. return 0;
  252. #endif
  253. }
  254. /**
  255. * Get the clock rate of Octeon
  256. *
  257. * Returns Clock rate in HZ
  258. */
  259. uint64_t octeon_get_clock_rate(void)
  260. {
  261. struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
  262. return sysinfo->cpu_clock_hz;
  263. }
  264. EXPORT_SYMBOL(octeon_get_clock_rate);
  265. static u64 octeon_io_clock_rate;
  266. u64 octeon_get_io_clock_rate(void)
  267. {
  268. return octeon_io_clock_rate;
  269. }
  270. EXPORT_SYMBOL(octeon_get_io_clock_rate);
  271. /**
  272. * Write to the LCD display connected to the bootbus. This display
  273. * exists on most Cavium evaluation boards. If it doesn't exist, then
  274. * this function doesn't do anything.
  275. *
  276. * @s: String to write
  277. */
  278. void octeon_write_lcd(const char *s)
  279. {
  280. if (octeon_bootinfo->led_display_base_addr) {
  281. void __iomem *lcd_address =
  282. ioremap_nocache(octeon_bootinfo->led_display_base_addr,
  283. 8);
  284. int i;
  285. for (i = 0; i < 8; i++, s++) {
  286. if (*s)
  287. iowrite8(*s, lcd_address + i);
  288. else
  289. iowrite8(' ', lcd_address + i);
  290. }
  291. iounmap(lcd_address);
  292. }
  293. }
  294. /**
  295. * Return the console uart passed by the bootloader
  296. *
  297. * Returns uart (0 or 1)
  298. */
  299. int octeon_get_boot_uart(void)
  300. {
  301. int uart;
  302. #ifdef CONFIG_CAVIUM_OCTEON_2ND_KERNEL
  303. uart = 1;
  304. #else
  305. uart = (octeon_boot_desc_ptr->flags & OCTEON_BL_FLAG_CONSOLE_UART1) ?
  306. 1 : 0;
  307. #endif
  308. return uart;
  309. }
  310. /**
  311. * Get the coremask Linux was booted on.
  312. *
  313. * Returns Core mask
  314. */
  315. int octeon_get_boot_coremask(void)
  316. {
  317. return octeon_boot_desc_ptr->core_mask;
  318. }
  319. /**
  320. * Check the hardware BIST results for a CPU
  321. */
  322. void octeon_check_cpu_bist(void)
  323. {
  324. const int coreid = cvmx_get_core_num();
  325. unsigned long long mask;
  326. unsigned long long bist_val;
  327. /* Check BIST results for COP0 registers */
  328. mask = 0x1f00000000ull;
  329. bist_val = read_octeon_c0_icacheerr();
  330. if (bist_val & mask)
  331. pr_err("Core%d BIST Failure: CacheErr(icache) = 0x%llx\n",
  332. coreid, bist_val);
  333. bist_val = read_octeon_c0_dcacheerr();
  334. if (bist_val & 1)
  335. pr_err("Core%d L1 Dcache parity error: "
  336. "CacheErr(dcache) = 0x%llx\n",
  337. coreid, bist_val);
  338. mask = 0xfc00000000000000ull;
  339. bist_val = read_c0_cvmmemctl();
  340. if (bist_val & mask)
  341. pr_err("Core%d BIST Failure: COP0_CVM_MEM_CTL = 0x%llx\n",
  342. coreid, bist_val);
  343. write_octeon_c0_dcacheerr(0);
  344. }
  345. /**
  346. * Reboot Octeon
  347. *
  348. * @command: Command to pass to the bootloader. Currently ignored.
  349. */
  350. static void octeon_restart(char *command)
  351. {
  352. /* Disable all watchdogs before soft reset. They don't get cleared */
  353. #ifdef CONFIG_SMP
  354. int cpu;
  355. for_each_online_cpu(cpu)
  356. cvmx_write_csr(CVMX_CIU_WDOGX(cpu_logical_map(cpu)), 0);
  357. #else
  358. cvmx_write_csr(CVMX_CIU_WDOGX(cvmx_get_core_num()), 0);
  359. #endif
  360. mb();
  361. while (1)
  362. cvmx_write_csr(CVMX_CIU_SOFT_RST, 1);
  363. }
  364. /**
  365. * Permanently stop a core.
  366. *
  367. * @arg: Ignored.
  368. */
  369. static void octeon_kill_core(void *arg)
  370. {
  371. if (octeon_is_simulation())
  372. /* A break instruction causes the simulator stop a core */
  373. asm volatile ("break" ::: "memory");
  374. local_irq_disable();
  375. /* Disable watchdog on this core. */
  376. cvmx_write_csr(CVMX_CIU_WDOGX(cvmx_get_core_num()), 0);
  377. /* Spin in a low power mode. */
  378. while (true)
  379. asm volatile ("wait" ::: "memory");
  380. }
  381. /**
  382. * Halt the system
  383. */
  384. static void octeon_halt(void)
  385. {
  386. smp_call_function(octeon_kill_core, NULL, 0);
  387. switch (octeon_bootinfo->board_type) {
  388. case CVMX_BOARD_TYPE_NAO38:
  389. /* Driving a 1 to GPIO 12 shuts off this board */
  390. cvmx_write_csr(CVMX_GPIO_BIT_CFGX(12), 1);
  391. cvmx_write_csr(CVMX_GPIO_TX_SET, 0x1000);
  392. break;
  393. default:
  394. octeon_write_lcd("PowerOff");
  395. break;
  396. }
  397. octeon_kill_core(NULL);
  398. }
  399. static char __read_mostly octeon_system_type[80];
  400. static int __init init_octeon_system_type(void)
  401. {
  402. snprintf(octeon_system_type, sizeof(octeon_system_type), "%s (%s)",
  403. cvmx_board_type_to_string(octeon_bootinfo->board_type),
  404. octeon_model_get_string(read_c0_prid()));
  405. return 0;
  406. }
  407. early_initcall(init_octeon_system_type);
  408. /**
  409. * Return a string representing the system type
  410. *
  411. * Returns
  412. */
  413. const char *octeon_board_type_string(void)
  414. {
  415. return octeon_system_type;
  416. }
  417. const char *get_system_type(void)
  418. __attribute__ ((alias("octeon_board_type_string")));
  419. void octeon_user_io_init(void)
  420. {
  421. union octeon_cvmemctl cvmmemctl;
  422. union cvmx_iob_fau_timeout fau_timeout;
  423. union cvmx_pow_nw_tim nm_tim;
  424. /* Get the current settings for CP0_CVMMEMCTL_REG */
  425. cvmmemctl.u64 = read_c0_cvmmemctl();
  426. /* R/W If set, marked write-buffer entries time out the same
  427. * as as other entries; if clear, marked write-buffer entries
  428. * use the maximum timeout. */
  429. cvmmemctl.s.dismarkwblongto = 1;
  430. /* R/W If set, a merged store does not clear the write-buffer
  431. * entry timeout state. */
  432. cvmmemctl.s.dismrgclrwbto = 0;
  433. /* R/W Two bits that are the MSBs of the resultant CVMSEG LM
  434. * word location for an IOBDMA. The other 8 bits come from the
  435. * SCRADDR field of the IOBDMA. */
  436. cvmmemctl.s.iobdmascrmsb = 0;
  437. /* R/W If set, SYNCWS and SYNCS only order marked stores; if
  438. * clear, SYNCWS and SYNCS only order unmarked
  439. * stores. SYNCWSMARKED has no effect when DISSYNCWS is
  440. * set. */
  441. cvmmemctl.s.syncwsmarked = 0;
  442. /* R/W If set, SYNCWS acts as SYNCW and SYNCS acts as SYNC. */
  443. cvmmemctl.s.dissyncws = 0;
  444. /* R/W If set, no stall happens on write buffer full. */
  445. if (OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2))
  446. cvmmemctl.s.diswbfst = 1;
  447. else
  448. cvmmemctl.s.diswbfst = 0;
  449. /* R/W If set (and SX set), supervisor-level loads/stores can
  450. * use XKPHYS addresses with <48>==0 */
  451. cvmmemctl.s.xkmemenas = 0;
  452. /* R/W If set (and UX set), user-level loads/stores can use
  453. * XKPHYS addresses with VA<48>==0 */
  454. cvmmemctl.s.xkmemenau = 0;
  455. /* R/W If set (and SX set), supervisor-level loads/stores can
  456. * use XKPHYS addresses with VA<48>==1 */
  457. cvmmemctl.s.xkioenas = 0;
  458. /* R/W If set (and UX set), user-level loads/stores can use
  459. * XKPHYS addresses with VA<48>==1 */
  460. cvmmemctl.s.xkioenau = 0;
  461. /* R/W If set, all stores act as SYNCW (NOMERGE must be set
  462. * when this is set) RW, reset to 0. */
  463. cvmmemctl.s.allsyncw = 0;
  464. /* R/W If set, no stores merge, and all stores reach the
  465. * coherent bus in order. */
  466. cvmmemctl.s.nomerge = 0;
  467. /* R/W Selects the bit in the counter used for DID time-outs 0
  468. * = 231, 1 = 230, 2 = 229, 3 = 214. Actual time-out is
  469. * between 1x and 2x this interval. For example, with
  470. * DIDTTO=3, expiration interval is between 16K and 32K. */
  471. cvmmemctl.s.didtto = 0;
  472. /* R/W If set, the (mem) CSR clock never turns off. */
  473. cvmmemctl.s.csrckalwys = 0;
  474. /* R/W If set, mclk never turns off. */
  475. cvmmemctl.s.mclkalwys = 0;
  476. /* R/W Selects the bit in the counter used for write buffer
  477. * flush time-outs (WBFLT+11) is the bit position in an
  478. * internal counter used to determine expiration. The write
  479. * buffer expires between 1x and 2x this interval. For
  480. * example, with WBFLT = 0, a write buffer expires between 2K
  481. * and 4K cycles after the write buffer entry is allocated. */
  482. cvmmemctl.s.wbfltime = 0;
  483. /* R/W If set, do not put Istream in the L2 cache. */
  484. cvmmemctl.s.istrnol2 = 0;
  485. /*
  486. * R/W The write buffer threshold. As per erratum Core-14752
  487. * for CN63XX, a sc/scd might fail if the write buffer is
  488. * full. Lowering WBTHRESH greatly lowers the chances of the
  489. * write buffer ever being full and triggering the erratum.
  490. */
  491. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X))
  492. cvmmemctl.s.wbthresh = 4;
  493. else
  494. cvmmemctl.s.wbthresh = 10;
  495. /* R/W If set, CVMSEG is available for loads/stores in
  496. * kernel/debug mode. */
  497. #if CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
  498. cvmmemctl.s.cvmsegenak = 1;
  499. #else
  500. cvmmemctl.s.cvmsegenak = 0;
  501. #endif
  502. /* R/W If set, CVMSEG is available for loads/stores in
  503. * supervisor mode. */
  504. cvmmemctl.s.cvmsegenas = 0;
  505. /* R/W If set, CVMSEG is available for loads/stores in user
  506. * mode. */
  507. cvmmemctl.s.cvmsegenau = 0;
  508. /* R/W Size of local memory in cache blocks, 54 (6912 bytes)
  509. * is max legal value. */
  510. cvmmemctl.s.lmemsz = CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE;
  511. write_c0_cvmmemctl(cvmmemctl.u64);
  512. if (smp_processor_id() == 0)
  513. pr_notice("CVMSEG size: %d cache lines (%d bytes)\n",
  514. CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE,
  515. CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128);
  516. /* Set a default for the hardware timeouts */
  517. fau_timeout.u64 = 0;
  518. fau_timeout.s.tout_val = 0xfff;
  519. /* Disable tagwait FAU timeout */
  520. fau_timeout.s.tout_enb = 0;
  521. cvmx_write_csr(CVMX_IOB_FAU_TIMEOUT, fau_timeout.u64);
  522. nm_tim.u64 = 0;
  523. /* 4096 cycles */
  524. nm_tim.s.nw_tim = 3;
  525. cvmx_write_csr(CVMX_POW_NW_TIM, nm_tim.u64);
  526. write_octeon_c0_icacheerr(0);
  527. write_c0_derraddr1(0);
  528. }
  529. /**
  530. * Early entry point for arch setup
  531. */
  532. void __init prom_init(void)
  533. {
  534. struct cvmx_sysinfo *sysinfo;
  535. const char *arg;
  536. char *p;
  537. int i;
  538. int argc;
  539. #ifdef CONFIG_CAVIUM_RESERVE32
  540. int64_t addr = -1;
  541. #endif
  542. /*
  543. * The bootloader passes a pointer to the boot descriptor in
  544. * $a3, this is available as fw_arg3.
  545. */
  546. octeon_boot_desc_ptr = (struct octeon_boot_descriptor *)fw_arg3;
  547. octeon_bootinfo =
  548. cvmx_phys_to_ptr(octeon_boot_desc_ptr->cvmx_desc_vaddr);
  549. cvmx_bootmem_init(cvmx_phys_to_ptr(octeon_bootinfo->phy_mem_desc_addr));
  550. sysinfo = cvmx_sysinfo_get();
  551. memset(sysinfo, 0, sizeof(*sysinfo));
  552. sysinfo->system_dram_size = octeon_bootinfo->dram_size << 20;
  553. sysinfo->phy_mem_desc_ptr =
  554. cvmx_phys_to_ptr(octeon_bootinfo->phy_mem_desc_addr);
  555. sysinfo->core_mask = octeon_bootinfo->core_mask;
  556. sysinfo->exception_base_addr = octeon_bootinfo->exception_base_addr;
  557. sysinfo->cpu_clock_hz = octeon_bootinfo->eclock_hz;
  558. sysinfo->dram_data_rate_hz = octeon_bootinfo->dclock_hz * 2;
  559. sysinfo->board_type = octeon_bootinfo->board_type;
  560. sysinfo->board_rev_major = octeon_bootinfo->board_rev_major;
  561. sysinfo->board_rev_minor = octeon_bootinfo->board_rev_minor;
  562. memcpy(sysinfo->mac_addr_base, octeon_bootinfo->mac_addr_base,
  563. sizeof(sysinfo->mac_addr_base));
  564. sysinfo->mac_addr_count = octeon_bootinfo->mac_addr_count;
  565. memcpy(sysinfo->board_serial_number,
  566. octeon_bootinfo->board_serial_number,
  567. sizeof(sysinfo->board_serial_number));
  568. sysinfo->compact_flash_common_base_addr =
  569. octeon_bootinfo->compact_flash_common_base_addr;
  570. sysinfo->compact_flash_attribute_base_addr =
  571. octeon_bootinfo->compact_flash_attribute_base_addr;
  572. sysinfo->led_display_base_addr = octeon_bootinfo->led_display_base_addr;
  573. sysinfo->dfa_ref_clock_hz = octeon_bootinfo->dfa_ref_clock_hz;
  574. sysinfo->bootloader_config_flags = octeon_bootinfo->config_flags;
  575. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  576. /* I/O clock runs at a different rate than the CPU. */
  577. union cvmx_mio_rst_boot rst_boot;
  578. rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
  579. octeon_io_clock_rate = 50000000 * rst_boot.s.pnr_mul;
  580. } else {
  581. octeon_io_clock_rate = sysinfo->cpu_clock_hz;
  582. }
  583. /*
  584. * Only enable the LED controller if we're running on a CN38XX, CN58XX,
  585. * or CN56XX. The CN30XX and CN31XX don't have an LED controller.
  586. */
  587. if (!octeon_is_simulation() &&
  588. octeon_has_feature(OCTEON_FEATURE_LED_CONTROLLER)) {
  589. cvmx_write_csr(CVMX_LED_EN, 0);
  590. cvmx_write_csr(CVMX_LED_PRT, 0);
  591. cvmx_write_csr(CVMX_LED_DBG, 0);
  592. cvmx_write_csr(CVMX_LED_PRT_FMT, 0);
  593. cvmx_write_csr(CVMX_LED_UDD_CNTX(0), 32);
  594. cvmx_write_csr(CVMX_LED_UDD_CNTX(1), 32);
  595. cvmx_write_csr(CVMX_LED_UDD_DATX(0), 0);
  596. cvmx_write_csr(CVMX_LED_UDD_DATX(1), 0);
  597. cvmx_write_csr(CVMX_LED_EN, 1);
  598. }
  599. #ifdef CONFIG_CAVIUM_RESERVE32
  600. /*
  601. * We need to temporarily allocate all memory in the reserve32
  602. * region. This makes sure the kernel doesn't allocate this
  603. * memory when it is getting memory from the
  604. * bootloader. Later, after the memory allocations are
  605. * complete, the reserve32 will be freed.
  606. *
  607. * Allocate memory for RESERVED32 aligned on 2MB boundary. This
  608. * is in case we later use hugetlb entries with it.
  609. */
  610. addr = cvmx_bootmem_phy_named_block_alloc(CONFIG_CAVIUM_RESERVE32 << 20,
  611. 0, 0, 2 << 20,
  612. "CAVIUM_RESERVE32", 0);
  613. if (addr < 0)
  614. pr_err("Failed to allocate CAVIUM_RESERVE32 memory area\n");
  615. else
  616. octeon_reserve32_memory = addr;
  617. #endif
  618. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2
  619. if (cvmx_read_csr(CVMX_L2D_FUS3) & (3ull << 34)) {
  620. pr_info("Skipping L2 locking due to reduced L2 cache size\n");
  621. } else {
  622. uint32_t __maybe_unused ebase = read_c0_ebase() & 0x3ffff000;
  623. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2_TLB
  624. /* TLB refill */
  625. cvmx_l2c_lock_mem_region(ebase, 0x100);
  626. #endif
  627. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2_EXCEPTION
  628. /* General exception */
  629. cvmx_l2c_lock_mem_region(ebase + 0x180, 0x80);
  630. #endif
  631. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2_LOW_LEVEL_INTERRUPT
  632. /* Interrupt handler */
  633. cvmx_l2c_lock_mem_region(ebase + 0x200, 0x80);
  634. #endif
  635. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2_INTERRUPT
  636. cvmx_l2c_lock_mem_region(__pa_symbol(handle_int), 0x100);
  637. cvmx_l2c_lock_mem_region(__pa_symbol(plat_irq_dispatch), 0x80);
  638. #endif
  639. #ifdef CONFIG_CAVIUM_OCTEON_LOCK_L2_MEMCPY
  640. cvmx_l2c_lock_mem_region(__pa_symbol(memcpy), 0x480);
  641. #endif
  642. }
  643. #endif
  644. octeon_check_cpu_bist();
  645. octeon_uart = octeon_get_boot_uart();
  646. #ifdef CONFIG_SMP
  647. octeon_write_lcd("LinuxSMP");
  648. #else
  649. octeon_write_lcd("Linux");
  650. #endif
  651. octeon_setup_delays();
  652. /*
  653. * BIST should always be enabled when doing a soft reset. L2
  654. * Cache locking for instance is not cleared unless BIST is
  655. * enabled. Unfortunately due to a chip errata G-200 for
  656. * Cn38XX and CN31XX, BIST msut be disabled on these parts.
  657. */
  658. if (OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2) ||
  659. OCTEON_IS_MODEL(OCTEON_CN31XX))
  660. cvmx_write_csr(CVMX_CIU_SOFT_BIST, 0);
  661. else
  662. cvmx_write_csr(CVMX_CIU_SOFT_BIST, 1);
  663. /* Default to 64MB in the simulator to speed things up */
  664. if (octeon_is_simulation())
  665. MAX_MEMORY = 64ull << 20;
  666. arg = strstr(arcs_cmdline, "mem=");
  667. if (arg) {
  668. MAX_MEMORY = memparse(arg + 4, &p);
  669. if (MAX_MEMORY == 0)
  670. MAX_MEMORY = 32ull << 30;
  671. if (*p == '@')
  672. RESERVE_LOW_MEM = memparse(p + 1, &p);
  673. }
  674. arcs_cmdline[0] = 0;
  675. argc = octeon_boot_desc_ptr->argc;
  676. for (i = 0; i < argc; i++) {
  677. const char *arg =
  678. cvmx_phys_to_ptr(octeon_boot_desc_ptr->argv[i]);
  679. if ((strncmp(arg, "MEM=", 4) == 0) ||
  680. (strncmp(arg, "mem=", 4) == 0)) {
  681. MAX_MEMORY = memparse(arg + 4, &p);
  682. if (MAX_MEMORY == 0)
  683. MAX_MEMORY = 32ull << 30;
  684. if (*p == '@')
  685. RESERVE_LOW_MEM = memparse(p + 1, &p);
  686. #ifdef CONFIG_KEXEC
  687. } else if (strncmp(arg, "crashkernel=", 12) == 0) {
  688. crashk_size = memparse(arg+12, &p);
  689. if (*p == '@')
  690. crashk_base = memparse(p+1, &p);
  691. strcat(arcs_cmdline, " ");
  692. strcat(arcs_cmdline, arg);
  693. /*
  694. * To do: switch parsing to new style, something like:
  695. * parse_crashkernel(arg, sysinfo->system_dram_size,
  696. * &crashk_size, &crashk_base);
  697. */
  698. #endif
  699. } else if (strlen(arcs_cmdline) + strlen(arg) + 1 <
  700. sizeof(arcs_cmdline) - 1) {
  701. strcat(arcs_cmdline, " ");
  702. strcat(arcs_cmdline, arg);
  703. }
  704. }
  705. if (strstr(arcs_cmdline, "console=") == NULL) {
  706. #ifdef CONFIG_CAVIUM_OCTEON_2ND_KERNEL
  707. strcat(arcs_cmdline, " console=ttyS0,115200");
  708. #else
  709. if (octeon_uart == 1)
  710. strcat(arcs_cmdline, " console=ttyS1,115200");
  711. else
  712. strcat(arcs_cmdline, " console=ttyS0,115200");
  713. #endif
  714. }
  715. mips_hpt_frequency = octeon_get_clock_rate();
  716. octeon_init_cvmcount();
  717. _machine_restart = octeon_restart;
  718. _machine_halt = octeon_halt;
  719. #ifdef CONFIG_KEXEC
  720. _machine_kexec_shutdown = octeon_shutdown;
  721. _machine_crash_shutdown = octeon_crash_shutdown;
  722. _machine_kexec_prepare = octeon_kexec_prepare;
  723. #endif
  724. octeon_user_io_init();
  725. register_smp_ops(&octeon_smp_ops);
  726. }
  727. /* Exclude a single page from the regions obtained in plat_mem_setup. */
  728. #ifndef CONFIG_CRASH_DUMP
  729. static __init void memory_exclude_page(u64 addr, u64 *mem, u64 *size)
  730. {
  731. if (addr > *mem && addr < *mem + *size) {
  732. u64 inc = addr - *mem;
  733. add_memory_region(*mem, inc, BOOT_MEM_RAM);
  734. *mem += inc;
  735. *size -= inc;
  736. }
  737. if (addr == *mem && *size > PAGE_SIZE) {
  738. *mem += PAGE_SIZE;
  739. *size -= PAGE_SIZE;
  740. }
  741. }
  742. #endif /* CONFIG_CRASH_DUMP */
  743. void __init plat_mem_setup(void)
  744. {
  745. uint64_t mem_alloc_size;
  746. uint64_t total;
  747. uint64_t crashk_end;
  748. #ifndef CONFIG_CRASH_DUMP
  749. int64_t memory;
  750. uint64_t kernel_start;
  751. uint64_t kernel_size;
  752. #endif
  753. total = 0;
  754. crashk_end = 0;
  755. /*
  756. * The Mips memory init uses the first memory location for
  757. * some memory vectors. When SPARSEMEM is in use, it doesn't
  758. * verify that the size is big enough for the final
  759. * vectors. Making the smallest chuck 4MB seems to be enough
  760. * to consistently work.
  761. */
  762. mem_alloc_size = 4 << 20;
  763. if (mem_alloc_size > MAX_MEMORY)
  764. mem_alloc_size = MAX_MEMORY;
  765. /* Crashkernel ignores bootmem list. It relies on mem=X@Y option */
  766. #ifdef CONFIG_CRASH_DUMP
  767. add_memory_region(RESERVE_LOW_MEM, MAX_MEMORY, BOOT_MEM_RAM);
  768. total += MAX_MEMORY;
  769. #else
  770. #ifdef CONFIG_KEXEC
  771. if (crashk_size > 0) {
  772. add_memory_region(crashk_base, crashk_size, BOOT_MEM_RAM);
  773. crashk_end = crashk_base + crashk_size;
  774. }
  775. #endif
  776. /*
  777. * When allocating memory, we want incrementing addresses from
  778. * bootmem_alloc so the code in add_memory_region can merge
  779. * regions next to each other.
  780. */
  781. cvmx_bootmem_lock();
  782. while ((boot_mem_map.nr_map < BOOT_MEM_MAP_MAX)
  783. && (total < MAX_MEMORY)) {
  784. memory = cvmx_bootmem_phy_alloc(mem_alloc_size,
  785. __pa_symbol(&__init_end), -1,
  786. 0x100000,
  787. CVMX_BOOTMEM_FLAG_NO_LOCKING);
  788. if (memory >= 0) {
  789. u64 size = mem_alloc_size;
  790. #ifdef CONFIG_KEXEC
  791. uint64_t end;
  792. #endif
  793. /*
  794. * exclude a page at the beginning and end of
  795. * the 256MB PCIe 'hole' so the kernel will not
  796. * try to allocate multi-page buffers that
  797. * span the discontinuity.
  798. */
  799. memory_exclude_page(CVMX_PCIE_BAR1_PHYS_BASE,
  800. &memory, &size);
  801. memory_exclude_page(CVMX_PCIE_BAR1_PHYS_BASE +
  802. CVMX_PCIE_BAR1_PHYS_SIZE,
  803. &memory, &size);
  804. #ifdef CONFIG_KEXEC
  805. end = memory + mem_alloc_size;
  806. /*
  807. * This function automatically merges address regions
  808. * next to each other if they are received in
  809. * incrementing order
  810. */
  811. if (memory < crashk_base && end > crashk_end) {
  812. /* region is fully in */
  813. add_memory_region(memory,
  814. crashk_base - memory,
  815. BOOT_MEM_RAM);
  816. total += crashk_base - memory;
  817. add_memory_region(crashk_end,
  818. end - crashk_end,
  819. BOOT_MEM_RAM);
  820. total += end - crashk_end;
  821. continue;
  822. }
  823. if (memory >= crashk_base && end <= crashk_end)
  824. /*
  825. * Entire memory region is within the new
  826. * kernel's memory, ignore it.
  827. */
  828. continue;
  829. if (memory > crashk_base && memory < crashk_end &&
  830. end > crashk_end) {
  831. /*
  832. * Overlap with the beginning of the region,
  833. * reserve the beginning.
  834. */
  835. mem_alloc_size -= crashk_end - memory;
  836. memory = crashk_end;
  837. } else if (memory < crashk_base && end > crashk_base &&
  838. end < crashk_end)
  839. /*
  840. * Overlap with the beginning of the region,
  841. * chop of end.
  842. */
  843. mem_alloc_size -= end - crashk_base;
  844. #endif
  845. add_memory_region(memory, mem_alloc_size, BOOT_MEM_RAM);
  846. total += mem_alloc_size;
  847. /* Recovering mem_alloc_size */
  848. mem_alloc_size = 4 << 20;
  849. } else {
  850. break;
  851. }
  852. }
  853. cvmx_bootmem_unlock();
  854. /* Add the memory region for the kernel. */
  855. kernel_start = (unsigned long) _text;
  856. kernel_size = _end - _text;
  857. /* Adjust for physical offset. */
  858. kernel_start &= ~0xffffffff80000000ULL;
  859. add_memory_region(kernel_start, kernel_size, BOOT_MEM_RAM);
  860. #endif /* CONFIG_CRASH_DUMP */
  861. #ifdef CONFIG_CAVIUM_RESERVE32
  862. /*
  863. * Now that we've allocated the kernel memory it is safe to
  864. * free the reserved region. We free it here so that builtin
  865. * drivers can use the memory.
  866. */
  867. if (octeon_reserve32_memory)
  868. cvmx_bootmem_free_named("CAVIUM_RESERVE32");
  869. #endif /* CONFIG_CAVIUM_RESERVE32 */
  870. if (total == 0)
  871. panic("Unable to allocate memory from "
  872. "cvmx_bootmem_phy_alloc");
  873. }
  874. /*
  875. * Emit one character to the boot UART. Exported for use by the
  876. * watchdog timer.
  877. */
  878. int prom_putchar(char c)
  879. {
  880. uint64_t lsrval;
  881. /* Spin until there is room */
  882. do {
  883. lsrval = cvmx_read_csr(CVMX_MIO_UARTX_LSR(octeon_uart));
  884. } while ((lsrval & 0x20) == 0);
  885. /* Write the byte */
  886. cvmx_write_csr(CVMX_MIO_UARTX_THR(octeon_uart), c & 0xffull);
  887. return 1;
  888. }
  889. EXPORT_SYMBOL(prom_putchar);
  890. void prom_free_prom_memory(void)
  891. {
  892. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X)) {
  893. /* Check for presence of Core-14449 fix. */
  894. u32 insn;
  895. u32 *foo;
  896. foo = &insn;
  897. asm volatile("# before" : : : "memory");
  898. prefetch(foo);
  899. asm volatile(
  900. ".set push\n\t"
  901. ".set noreorder\n\t"
  902. "bal 1f\n\t"
  903. "nop\n"
  904. "1:\tlw %0,-12($31)\n\t"
  905. ".set pop\n\t"
  906. : "=r" (insn) : : "$31", "memory");
  907. if ((insn >> 26) != 0x33)
  908. panic("No PREF instruction at Core-14449 probe point.");
  909. if (((insn >> 16) & 0x1f) != 28)
  910. panic("Core-14449 WAR not in place (%04x).\n"
  911. "Please build kernel with proper options (CONFIG_CAVIUM_CN63XXP1).", insn);
  912. }
  913. }
  914. int octeon_prune_device_tree(void);
  915. extern const char __dtb_octeon_3xxx_begin;
  916. extern const char __dtb_octeon_68xx_begin;
  917. void __init device_tree_init(void)
  918. {
  919. const void *fdt;
  920. bool do_prune;
  921. if (octeon_bootinfo->minor_version >= 3 && octeon_bootinfo->fdt_addr) {
  922. fdt = phys_to_virt(octeon_bootinfo->fdt_addr);
  923. if (fdt_check_header(fdt))
  924. panic("Corrupt Device Tree passed to kernel.");
  925. do_prune = false;
  926. } else if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
  927. fdt = &__dtb_octeon_68xx_begin;
  928. do_prune = true;
  929. } else {
  930. fdt = &__dtb_octeon_3xxx_begin;
  931. do_prune = true;
  932. }
  933. initial_boot_params = (void *)fdt;
  934. if (do_prune) {
  935. octeon_prune_device_tree();
  936. pr_info("Using internal Device Tree.\n");
  937. } else {
  938. pr_info("Using passed Device Tree.\n");
  939. }
  940. unflatten_and_copy_device_tree();
  941. }
  942. static int __initdata disable_octeon_edac_p;
  943. static int __init disable_octeon_edac(char *str)
  944. {
  945. disable_octeon_edac_p = 1;
  946. return 0;
  947. }
  948. early_param("disable_octeon_edac", disable_octeon_edac);
  949. static char *edac_device_names[] = {
  950. "octeon_l2c_edac",
  951. "octeon_pc_edac",
  952. };
  953. static int __init edac_devinit(void)
  954. {
  955. struct platform_device *dev;
  956. int i, err = 0;
  957. int num_lmc;
  958. char *name;
  959. if (disable_octeon_edac_p)
  960. return 0;
  961. for (i = 0; i < ARRAY_SIZE(edac_device_names); i++) {
  962. name = edac_device_names[i];
  963. dev = platform_device_register_simple(name, -1, NULL, 0);
  964. if (IS_ERR(dev)) {
  965. pr_err("Registation of %s failed!\n", name);
  966. err = PTR_ERR(dev);
  967. }
  968. }
  969. num_lmc = OCTEON_IS_MODEL(OCTEON_CN68XX) ? 4 :
  970. (OCTEON_IS_MODEL(OCTEON_CN56XX) ? 2 : 1);
  971. for (i = 0; i < num_lmc; i++) {
  972. dev = platform_device_register_simple("octeon_lmc_edac",
  973. i, NULL, 0);
  974. if (IS_ERR(dev)) {
  975. pr_err("Registation of octeon_lmc_edac %d failed!\n", i);
  976. err = PTR_ERR(dev);
  977. }
  978. }
  979. return err;
  980. }
  981. device_initcall(edac_devinit);
  982. static void __initdata *octeon_dummy_iospace;
  983. static int __init octeon_no_pci_init(void)
  984. {
  985. /*
  986. * Initially assume there is no PCI. The PCI/PCIe platform code will
  987. * later re-initialize these to correct values if they are present.
  988. */
  989. octeon_dummy_iospace = vzalloc(IO_SPACE_LIMIT);
  990. set_io_port_base((unsigned long)octeon_dummy_iospace);
  991. ioport_resource.start = MAX_RESOURCE;
  992. ioport_resource.end = 0;
  993. return 0;
  994. }
  995. core_initcall(octeon_no_pci_init);
  996. static int __init octeon_no_pci_release(void)
  997. {
  998. /*
  999. * Release the allocated memory if a real IO space is there.
  1000. */
  1001. if ((unsigned long)octeon_dummy_iospace != mips_io_port_base)
  1002. vfree(octeon_dummy_iospace);
  1003. return 0;
  1004. }
  1005. late_initcall(octeon_no_pci_release);