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