setup.c 30 KB

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