1/*
2 *  Copyright (C) 2004 Florian Schirmer <jolt@tuxbox.org>
3 *  Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
4 *  Copyright (C) 2006 Michael Buesch <m@bues.ch>
5 *  Copyright (C) 2010 Waldemar Brodkorb <wbx@openadk.org>
6 *  Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 *
8 *  This program is free software; you can redistribute  it and/or modify it
9 *  under  the terms of  the GNU General  Public License as published by the
10 *  Free Software Foundation;  either version 2 of the  License, or (at your
11 *  option) any later version.
12 *
13 *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
14 *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
15 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
16 *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
17 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
19 *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
21 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 *  You should have received a copy of the  GNU General Public License along
25 *  with this program; if not, write  to the Free Software Foundation, Inc.,
26 *  675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include "bcm47xx_private.h"
30
31#include <linux/export.h>
32#include <linux/types.h>
33#include <linux/ethtool.h>
34#include <linux/phy.h>
35#include <linux/phy_fixed.h>
36#include <linux/ssb/ssb.h>
37#include <linux/ssb/ssb_embedded.h>
38#include <linux/bcma/bcma_soc.h>
39#include <asm/bootinfo.h>
40#include <asm/idle.h>
41#include <asm/prom.h>
42#include <asm/reboot.h>
43#include <asm/time.h>
44#include <bcm47xx.h>
45#include <bcm47xx_board.h>
46
47union bcm47xx_bus bcm47xx_bus;
48EXPORT_SYMBOL(bcm47xx_bus);
49
50enum bcm47xx_bus_type bcm47xx_bus_type;
51EXPORT_SYMBOL(bcm47xx_bus_type);
52
53static void bcm47xx_machine_restart(char *command)
54{
55	pr_alert("Please stand by while rebooting the system...\n");
56	local_irq_disable();
57	/* Set the watchdog timer to reset immediately */
58	switch (bcm47xx_bus_type) {
59#ifdef CONFIG_BCM47XX_SSB
60	case BCM47XX_BUS_TYPE_SSB:
61		if (bcm47xx_bus.ssb.chip_id == 0x4785)
62			write_c0_diag4(1 << 22);
63		ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 1);
64		if (bcm47xx_bus.ssb.chip_id == 0x4785) {
65			__asm__ __volatile__(
66				".set\tmips3\n\t"
67				"sync\n\t"
68				"wait\n\t"
69				".set\tmips0");
70		}
71		break;
72#endif
73#ifdef CONFIG_BCM47XX_BCMA
74	case BCM47XX_BUS_TYPE_BCMA:
75		bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 1);
76		break;
77#endif
78	}
79	while (1)
80		cpu_relax();
81}
82
83static void bcm47xx_machine_halt(void)
84{
85	/* Disable interrupts and watchdog and spin forever */
86	local_irq_disable();
87	switch (bcm47xx_bus_type) {
88#ifdef CONFIG_BCM47XX_SSB
89	case BCM47XX_BUS_TYPE_SSB:
90		ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0);
91		break;
92#endif
93#ifdef CONFIG_BCM47XX_BCMA
94	case BCM47XX_BUS_TYPE_BCMA:
95		bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 0);
96		break;
97#endif
98	}
99	while (1)
100		cpu_relax();
101}
102
103#ifdef CONFIG_BCM47XX_SSB
104static int bcm47xx_get_invariants(struct ssb_bus *bus,
105				  struct ssb_init_invariants *iv)
106{
107	char buf[20];
108	int len, err;
109
110	/* Fill boardinfo structure */
111	memset(&iv->boardinfo, 0 , sizeof(struct ssb_boardinfo));
112
113	len = bcm47xx_nvram_getenv("boardvendor", buf, sizeof(buf));
114	if (len > 0) {
115		err = kstrtou16(strim(buf), 0, &iv->boardinfo.vendor);
116		if (err)
117			pr_warn("Couldn't parse nvram board vendor entry with value \"%s\"\n",
118				buf);
119	}
120	if (!iv->boardinfo.vendor)
121		iv->boardinfo.vendor = SSB_BOARDVENDOR_BCM;
122
123	len = bcm47xx_nvram_getenv("boardtype", buf, sizeof(buf));
124	if (len > 0) {
125		err = kstrtou16(strim(buf), 0, &iv->boardinfo.type);
126		if (err)
127			pr_warn("Couldn't parse nvram board type entry with value \"%s\"\n",
128				buf);
129	}
130
131	memset(&iv->sprom, 0, sizeof(struct ssb_sprom));
132	bcm47xx_fill_sprom(&iv->sprom, NULL, false);
133
134	if (bcm47xx_nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
135		iv->has_cardbus_slot = !!simple_strtoul(buf, NULL, 10);
136
137	return 0;
138}
139
140static void __init bcm47xx_register_ssb(void)
141{
142	int err;
143	char buf[100];
144	struct ssb_mipscore *mcore;
145
146	err = ssb_bus_ssbbus_register(&bcm47xx_bus.ssb, SSB_ENUM_BASE,
147				      bcm47xx_get_invariants);
148	if (err)
149		panic("Failed to initialize SSB bus (err %d)", err);
150
151	mcore = &bcm47xx_bus.ssb.mipscore;
152	if (bcm47xx_nvram_getenv("kernel_args", buf, sizeof(buf)) >= 0) {
153		if (strstr(buf, "console=ttyS1")) {
154			struct ssb_serial_port port;
155
156			pr_debug("Swapping serial ports!\n");
157			/* swap serial ports */
158			memcpy(&port, &mcore->serial_ports[0], sizeof(port));
159			memcpy(&mcore->serial_ports[0], &mcore->serial_ports[1],
160			       sizeof(port));
161			memcpy(&mcore->serial_ports[1], &port, sizeof(port));
162		}
163	}
164}
165#endif
166
167#ifdef CONFIG_BCM47XX_BCMA
168static void __init bcm47xx_register_bcma(void)
169{
170	int err;
171
172	err = bcma_host_soc_register(&bcm47xx_bus.bcma);
173	if (err)
174		panic("Failed to register BCMA bus (err %d)", err);
175}
176#endif
177
178/*
179 * Memory setup is done in the early part of MIPS's arch_mem_init. It's supposed
180 * to detect memory and record it with add_memory_region.
181 * Any extra initializaion performed here must not use kmalloc or bootmem.
182 */
183void __init plat_mem_setup(void)
184{
185	struct cpuinfo_mips *c = &current_cpu_data;
186
187	if ((c->cputype == CPU_74K) || (c->cputype == CPU_1074K)) {
188		pr_info("Using bcma bus\n");
189#ifdef CONFIG_BCM47XX_BCMA
190		bcm47xx_bus_type = BCM47XX_BUS_TYPE_BCMA;
191		bcm47xx_sprom_register_fallbacks();
192		bcm47xx_register_bcma();
193		bcm47xx_set_system_type(bcm47xx_bus.bcma.bus.chipinfo.id);
194#ifdef CONFIG_HIGHMEM
195		bcm47xx_prom_highmem_init();
196#endif
197#endif
198	} else {
199		pr_info("Using ssb bus\n");
200#ifdef CONFIG_BCM47XX_SSB
201		bcm47xx_bus_type = BCM47XX_BUS_TYPE_SSB;
202		bcm47xx_sprom_register_fallbacks();
203		bcm47xx_register_ssb();
204		bcm47xx_set_system_type(bcm47xx_bus.ssb.chip_id);
205#endif
206	}
207
208	_machine_restart = bcm47xx_machine_restart;
209	_machine_halt = bcm47xx_machine_halt;
210	pm_power_off = bcm47xx_machine_halt;
211}
212
213/*
214 * This finishes bus initialization doing things that were not possible without
215 * kmalloc. Make sure to call it late enough (after mm_init).
216 */
217void __init bcm47xx_bus_setup(void)
218{
219#ifdef CONFIG_BCM47XX_BCMA
220	if (bcm47xx_bus_type == BCM47XX_BUS_TYPE_BCMA) {
221		int err;
222
223		err = bcma_host_soc_init(&bcm47xx_bus.bcma);
224		if (err)
225			panic("Failed to initialize BCMA bus (err %d)", err);
226	}
227#endif
228
229	/* With bus initialized we can access NVRAM and detect the board */
230	bcm47xx_board_detect();
231	mips_set_machine_name(bcm47xx_board_get_name());
232}
233
234static int __init bcm47xx_cpu_fixes(void)
235{
236	switch (bcm47xx_bus_type) {
237#ifdef CONFIG_BCM47XX_SSB
238	case BCM47XX_BUS_TYPE_SSB:
239		/* Nothing to do */
240		break;
241#endif
242#ifdef CONFIG_BCM47XX_BCMA
243	case BCM47XX_BUS_TYPE_BCMA:
244		/* The BCM4706 has a problem with the CPU wait instruction.
245		 * When r4k_wait or r4k_wait_irqoff is used will just hang and
246		 * not return from a msleep(). Removing the cpu_wait
247		 * functionality is a workaround for this problem. The BCM4716
248		 * does not have this problem.
249		 */
250		if (bcm47xx_bus.bcma.bus.chipinfo.id == BCMA_CHIP_ID_BCM4706)
251			cpu_wait = NULL;
252		break;
253#endif
254	}
255	return 0;
256}
257arch_initcall(bcm47xx_cpu_fixes);
258
259static struct fixed_phy_status bcm47xx_fixed_phy_status __initdata = {
260	.link	= 1,
261	.speed	= SPEED_100,
262	.duplex	= DUPLEX_FULL,
263};
264
265static int __init bcm47xx_register_bus_complete(void)
266{
267	switch (bcm47xx_bus_type) {
268#ifdef CONFIG_BCM47XX_SSB
269	case BCM47XX_BUS_TYPE_SSB:
270		/* Nothing to do */
271		break;
272#endif
273#ifdef CONFIG_BCM47XX_BCMA
274	case BCM47XX_BUS_TYPE_BCMA:
275		bcma_bus_register(&bcm47xx_bus.bcma.bus);
276		break;
277#endif
278	}
279	bcm47xx_buttons_register();
280	bcm47xx_leds_register();
281	bcm47xx_workarounds();
282
283	fixed_phy_add(PHY_POLL, 0, &bcm47xx_fixed_phy_status, -1);
284	return 0;
285}
286device_initcall(bcm47xx_register_bus_complete);
287