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