1/*
2 * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
3 * reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the NetLogic
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in
19 *    the documentation and/or other materials provided with the
20 *    distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY NETLOGIC ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <linux/kernel.h>
36#include <linux/serial_8250.h>
37#include <linux/memblock.h>
38#include <linux/pm.h>
39
40#include <asm/idle.h>
41#include <asm/reboot.h>
42#include <asm/time.h>
43#include <asm/bootinfo.h>
44
45#include <asm/netlogic/interrupt.h>
46#include <asm/netlogic/psb-bootinfo.h>
47#include <asm/netlogic/haldefs.h>
48#include <asm/netlogic/common.h>
49
50#include <asm/netlogic/xlr/xlr.h>
51#include <asm/netlogic/xlr/iomap.h>
52#include <asm/netlogic/xlr/pic.h>
53#include <asm/netlogic/xlr/gpio.h>
54#include <asm/netlogic/xlr/fmn.h>
55
56uint64_t nlm_io_base = DEFAULT_NETLOGIC_IO_BASE;
57struct psb_info nlm_prom_info;
58
59/* default to uniprocessor */
60unsigned int  nlm_threads_per_core = 1;
61struct nlm_soc_info nlm_nodes[NLM_NR_NODES];
62cpumask_t nlm_cpumask = CPU_MASK_CPU0;
63
64static void nlm_linux_exit(void)
65{
66	uint64_t gpiobase;
67
68	gpiobase = nlm_mmio_base(NETLOGIC_IO_GPIO_OFFSET);
69	/* trigger a chip reset by writing 1 to GPIO_SWRESET_REG */
70	nlm_write_reg(gpiobase, GPIO_SWRESET_REG, 1);
71	for ( ; ; )
72		cpu_wait();
73}
74
75void __init plat_mem_setup(void)
76{
77	_machine_restart = (void (*)(char *))nlm_linux_exit;
78	_machine_halt	= nlm_linux_exit;
79	pm_power_off	= nlm_linux_exit;
80}
81
82const char *get_system_type(void)
83{
84	return "Netlogic XLR/XLS Series";
85}
86
87unsigned int nlm_get_cpu_frequency(void)
88{
89	return (unsigned int)nlm_prom_info.cpu_frequency;
90}
91
92void __init prom_free_prom_memory(void)
93{
94	/* Nothing yet */
95}
96
97void nlm_percpu_init(int hwcpuid)
98{
99	if (hwcpuid % 4 == 0)
100		xlr_percpu_fmn_init();
101}
102
103static void __init build_arcs_cmdline(int *argv)
104{
105	int i, remain, len;
106	char *arg;
107
108	remain = sizeof(arcs_cmdline) - 1;
109	arcs_cmdline[0] = '\0';
110	for (i = 0; argv[i] != 0; i++) {
111		arg = (char *)(long)argv[i];
112		len = strlen(arg);
113		if (len + 1 > remain)
114			break;
115		strcat(arcs_cmdline, arg);
116		strcat(arcs_cmdline, " ");
117		remain -=  len + 1;
118	}
119
120	/* Add the default options here */
121	if ((strstr(arcs_cmdline, "console=")) == NULL) {
122		arg = "console=ttyS0,38400 ";
123		len = strlen(arg);
124		if (len > remain)
125			goto fail;
126		strcat(arcs_cmdline, arg);
127		remain -= len;
128	}
129#ifdef CONFIG_BLK_DEV_INITRD
130	if ((strstr(arcs_cmdline, "rdinit=")) == NULL) {
131		arg = "rdinit=/sbin/init ";
132		len = strlen(arg);
133		if (len > remain)
134			goto fail;
135		strcat(arcs_cmdline, arg);
136		remain -= len;
137	}
138#endif
139	return;
140fail:
141	panic("Cannot add %s, command line too big!", arg);
142}
143
144static void prom_add_memory(void)
145{
146	struct nlm_boot_mem_map *bootm;
147	u64 start, size;
148	u64 pref_backup = 512;	/* avoid pref walking beyond end */
149	int i;
150
151	bootm = (void *)(long)nlm_prom_info.psb_mem_map;
152	for (i = 0; i < bootm->nr_map; i++) {
153		if (bootm->map[i].type != NLM_BOOT_MEM_RAM)
154			continue;
155		start = bootm->map[i].addr;
156		size   = bootm->map[i].size;
157
158		/* Work around for using bootloader mem */
159		if (i == 0 && start == 0 && size == 0x0c000000)
160			size = 0x0ff00000;
161
162		memblock_add(start, size - pref_backup);
163	}
164}
165
166static void nlm_init_node(void)
167{
168	struct nlm_soc_info *nodep;
169
170	nodep = nlm_current_node();
171	nodep->picbase = nlm_mmio_base(NETLOGIC_IO_PIC_OFFSET);
172	nodep->ebase = read_c0_ebase() & MIPS_EBASE_BASE;
173	spin_lock_init(&nodep->piclock);
174}
175
176void __init prom_init(void)
177{
178	int *argv, *envp;		/* passed as 32 bit ptrs */
179	struct psb_info *prom_infop;
180	void *reset_vec;
181#ifdef CONFIG_SMP
182	int i;
183#endif
184
185	/* truncate to 32 bit and sign extend all args */
186	argv = (int *)(long)(int)fw_arg1;
187	envp = (int *)(long)(int)fw_arg2;
188	prom_infop = (struct psb_info *)(long)(int)fw_arg3;
189
190	nlm_prom_info = *prom_infop;
191	nlm_init_node();
192
193	/* Update reset entry point with CPU init code */
194	reset_vec = (void *)CKSEG1ADDR(RESET_VEC_PHYS);
195	memset(reset_vec, 0, RESET_VEC_SIZE);
196	memcpy(reset_vec, (void *)nlm_reset_entry,
197			(nlm_reset_entry_end - nlm_reset_entry));
198
199	build_arcs_cmdline(argv);
200	prom_add_memory();
201
202#ifdef CONFIG_SMP
203	for (i = 0; i < 32; i++)
204		if (nlm_prom_info.online_cpu_map & (1 << i))
205			cpumask_set_cpu(i, &nlm_cpumask);
206	nlm_wakeup_secondary_cpus();
207	register_smp_ops(&nlm_smp_ops);
208#endif
209	xlr_board_info_setup();
210	xlr_percpu_fmn_init();
211}
212