194a4c329SAtsushi Nemoto /*
294a4c329SAtsushi Nemoto  * TX4927 setup routines
394a4c329SAtsushi Nemoto  * Based on linux/arch/mips/txx9/rbtx4938/setup.c,
494a4c329SAtsushi Nemoto  *	    and RBTX49xx patch from CELF patch archive.
594a4c329SAtsushi Nemoto  *
694a4c329SAtsushi Nemoto  * 2003-2005 (c) MontaVista Software, Inc.
794a4c329SAtsushi Nemoto  * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007
894a4c329SAtsushi Nemoto  *
994a4c329SAtsushi Nemoto  * This file is subject to the terms and conditions of the GNU General Public
1094a4c329SAtsushi Nemoto  * License.  See the file "COPYING" in the main directory of this archive
1194a4c329SAtsushi Nemoto  * for more details.
1294a4c329SAtsushi Nemoto  */
1394a4c329SAtsushi Nemoto #include <linux/init.h>
1494a4c329SAtsushi Nemoto #include <linux/ioport.h>
1594a4c329SAtsushi Nemoto #include <linux/delay.h>
1694a4c329SAtsushi Nemoto #include <linux/param.h>
1751f607c7SAtsushi Nemoto #include <linux/mtd/physmap.h>
18496a3b5cSAtsushi Nemoto #include <asm/reboot.h>
1994a4c329SAtsushi Nemoto #include <asm/txx9irq.h>
2094a4c329SAtsushi Nemoto #include <asm/txx9tmr.h>
2194a4c329SAtsushi Nemoto #include <asm/txx9pio.h>
2294a4c329SAtsushi Nemoto #include <asm/txx9/generic.h>
2394a4c329SAtsushi Nemoto #include <asm/txx9/tx4927.h>
2494a4c329SAtsushi Nemoto 
2568314725SAtsushi Nemoto static void __init tx4927_wdr_init(void)
2694a4c329SAtsushi Nemoto {
27496a3b5cSAtsushi Nemoto 	/* report watchdog reset status */
28496a3b5cSAtsushi Nemoto 	if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDRST)
29496a3b5cSAtsushi Nemoto 		pr_warning("Watchdog reset detected at 0x%lx\n",
30496a3b5cSAtsushi Nemoto 			   read_c0_errorepc());
3194a4c329SAtsushi Nemoto 	/* clear WatchDogReset (W1C) */
3294a4c329SAtsushi Nemoto 	tx4927_ccfg_set(TX4927_CCFG_WDRST);
3394a4c329SAtsushi Nemoto 	/* do reset on watchdog */
3494a4c329SAtsushi Nemoto 	tx4927_ccfg_set(TX4927_CCFG_WR);
3594a4c329SAtsushi Nemoto }
3694a4c329SAtsushi Nemoto 
3768314725SAtsushi Nemoto void __init tx4927_wdt_init(void)
3868314725SAtsushi Nemoto {
3968314725SAtsushi Nemoto 	txx9_wdt_init(TX4927_TMR_REG(2) & 0xfffffffffULL);
4068314725SAtsushi Nemoto }
4168314725SAtsushi Nemoto 
42496a3b5cSAtsushi Nemoto static void tx4927_machine_restart(char *command)
43496a3b5cSAtsushi Nemoto {
44496a3b5cSAtsushi Nemoto 	local_irq_disable();
45496a3b5cSAtsushi Nemoto 	pr_emerg("Rebooting (with %s watchdog reset)...\n",
46496a3b5cSAtsushi Nemoto 		 (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDREXEN) ?
47496a3b5cSAtsushi Nemoto 		 "external" : "internal");
48496a3b5cSAtsushi Nemoto 	/* clear watchdog status */
49496a3b5cSAtsushi Nemoto 	tx4927_ccfg_set(TX4927_CCFG_WDRST);	/* W1C */
50496a3b5cSAtsushi Nemoto 	txx9_wdt_now(TX4927_TMR_REG(2) & 0xfffffffffULL);
51496a3b5cSAtsushi Nemoto 	while (!(____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDRST))
52496a3b5cSAtsushi Nemoto 		;
53496a3b5cSAtsushi Nemoto 	mdelay(10);
54496a3b5cSAtsushi Nemoto 	if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDREXEN) {
55496a3b5cSAtsushi Nemoto 		pr_emerg("Rebooting (with internal watchdog reset)...\n");
56496a3b5cSAtsushi Nemoto 		/* External WDRST failed.  Do internal watchdog reset */
57496a3b5cSAtsushi Nemoto 		tx4927_ccfg_clear(TX4927_CCFG_WDREXEN);
58496a3b5cSAtsushi Nemoto 	}
59496a3b5cSAtsushi Nemoto 	/* fallback */
60496a3b5cSAtsushi Nemoto 	(*_machine_halt)();
61496a3b5cSAtsushi Nemoto }
62496a3b5cSAtsushi Nemoto 
6394a4c329SAtsushi Nemoto static struct resource tx4927_sdram_resource[4];
6494a4c329SAtsushi Nemoto 
6594a4c329SAtsushi Nemoto void __init tx4927_setup(void)
6694a4c329SAtsushi Nemoto {
6794a4c329SAtsushi Nemoto 	int i;
6894a4c329SAtsushi Nemoto 	__u32 divmode;
6994a4c329SAtsushi Nemoto 	int cpuclk = 0;
7094a4c329SAtsushi Nemoto 	u64 ccfg;
7194a4c329SAtsushi Nemoto 
7294a4c329SAtsushi Nemoto 	txx9_reg_res_init(TX4927_REV_PCODE(), TX4927_REG_BASE,
7394a4c329SAtsushi Nemoto 			  TX4927_REG_SIZE);
74d10e025fSAtsushi Nemoto 	set_c0_config(TX49_CONF_CWFON);
7594a4c329SAtsushi Nemoto 
7694a4c329SAtsushi Nemoto 	/* SDRAMC,EBUSC are configured by PROM */
7794a4c329SAtsushi Nemoto 	for (i = 0; i < 8; i++) {
7894a4c329SAtsushi Nemoto 		if (!(TX4927_EBUSC_CR(i) & 0x8))
7994a4c329SAtsushi Nemoto 			continue;	/* disabled */
8094a4c329SAtsushi Nemoto 		txx9_ce_res[i].start = (unsigned long)TX4927_EBUSC_BA(i);
8194a4c329SAtsushi Nemoto 		txx9_ce_res[i].end =
8294a4c329SAtsushi Nemoto 			txx9_ce_res[i].start + TX4927_EBUSC_SIZE(i) - 1;
8394a4c329SAtsushi Nemoto 		request_resource(&iomem_resource, &txx9_ce_res[i]);
8494a4c329SAtsushi Nemoto 	}
8594a4c329SAtsushi Nemoto 
8694a4c329SAtsushi Nemoto 	/* clocks */
8794a4c329SAtsushi Nemoto 	ccfg = ____raw_readq(&tx4927_ccfgptr->ccfg);
8894a4c329SAtsushi Nemoto 	if (txx9_master_clock) {
8994a4c329SAtsushi Nemoto 		/* calculate gbus_clock and cpu_clock from master_clock */
9094a4c329SAtsushi Nemoto 		divmode = (__u32)ccfg & TX4927_CCFG_DIVMODE_MASK;
9194a4c329SAtsushi Nemoto 		switch (divmode) {
9294a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_8:
9394a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_10:
9494a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_12:
9594a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_16:
9694a4c329SAtsushi Nemoto 			txx9_gbus_clock = txx9_master_clock * 4; break;
9794a4c329SAtsushi Nemoto 		default:
9894a4c329SAtsushi Nemoto 			txx9_gbus_clock = txx9_master_clock;
9994a4c329SAtsushi Nemoto 		}
10094a4c329SAtsushi Nemoto 		switch (divmode) {
10194a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_2:
10294a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_8:
10394a4c329SAtsushi Nemoto 			cpuclk = txx9_gbus_clock * 2; break;
10494a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_2_5:
10594a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_10:
10694a4c329SAtsushi Nemoto 			cpuclk = txx9_gbus_clock * 5 / 2; break;
10794a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_3:
10894a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_12:
10994a4c329SAtsushi Nemoto 			cpuclk = txx9_gbus_clock * 3; break;
11094a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_4:
11194a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_16:
11294a4c329SAtsushi Nemoto 			cpuclk = txx9_gbus_clock * 4; break;
11394a4c329SAtsushi Nemoto 		}
11494a4c329SAtsushi Nemoto 		txx9_cpu_clock = cpuclk;
11594a4c329SAtsushi Nemoto 	} else {
11694a4c329SAtsushi Nemoto 		if (txx9_cpu_clock == 0)
11794a4c329SAtsushi Nemoto 			txx9_cpu_clock = 200000000;	/* 200MHz */
11894a4c329SAtsushi Nemoto 		/* calculate gbus_clock and master_clock from cpu_clock */
11994a4c329SAtsushi Nemoto 		cpuclk = txx9_cpu_clock;
12094a4c329SAtsushi Nemoto 		divmode = (__u32)ccfg & TX4927_CCFG_DIVMODE_MASK;
12194a4c329SAtsushi Nemoto 		switch (divmode) {
12294a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_2:
12394a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_8:
12494a4c329SAtsushi Nemoto 			txx9_gbus_clock = cpuclk / 2; break;
12594a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_2_5:
12694a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_10:
12794a4c329SAtsushi Nemoto 			txx9_gbus_clock = cpuclk * 2 / 5; break;
12894a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_3:
12994a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_12:
13094a4c329SAtsushi Nemoto 			txx9_gbus_clock = cpuclk / 3; break;
13194a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_4:
13294a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_16:
13394a4c329SAtsushi Nemoto 			txx9_gbus_clock = cpuclk / 4; break;
13494a4c329SAtsushi Nemoto 		}
13594a4c329SAtsushi Nemoto 		switch (divmode) {
13694a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_8:
13794a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_10:
13894a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_12:
13994a4c329SAtsushi Nemoto 		case TX4927_CCFG_DIVMODE_16:
14094a4c329SAtsushi Nemoto 			txx9_master_clock = txx9_gbus_clock / 4; break;
14194a4c329SAtsushi Nemoto 		default:
14294a4c329SAtsushi Nemoto 			txx9_master_clock = txx9_gbus_clock;
14394a4c329SAtsushi Nemoto 		}
14494a4c329SAtsushi Nemoto 	}
14594a4c329SAtsushi Nemoto 	/* change default value to udelay/mdelay take reasonable time */
14694a4c329SAtsushi Nemoto 	loops_per_jiffy = txx9_cpu_clock / HZ / 2;
14794a4c329SAtsushi Nemoto 
14894a4c329SAtsushi Nemoto 	/* CCFG */
14994a4c329SAtsushi Nemoto 	tx4927_wdr_init();
15094a4c329SAtsushi Nemoto 	/* clear BusErrorOnWrite flag (W1C) */
15194a4c329SAtsushi Nemoto 	tx4927_ccfg_set(TX4927_CCFG_BEOW);
15294a4c329SAtsushi Nemoto 	/* enable Timeout BusError */
15394a4c329SAtsushi Nemoto 	if (txx9_ccfg_toeon)
15494a4c329SAtsushi Nemoto 		tx4927_ccfg_set(TX4927_CCFG_TOE);
15594a4c329SAtsushi Nemoto 
15694a4c329SAtsushi Nemoto 	/* DMA selection */
15794a4c329SAtsushi Nemoto 	txx9_clear64(&tx4927_ccfgptr->pcfg, TX4927_PCFG_DMASEL_ALL);
15894a4c329SAtsushi Nemoto 
15994a4c329SAtsushi Nemoto 	/* Use external clock for external arbiter */
16094a4c329SAtsushi Nemoto 	if (!(____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_PCIARB))
16194a4c329SAtsushi Nemoto 		txx9_clear64(&tx4927_ccfgptr->pcfg, TX4927_PCFG_PCICLKEN_ALL);
16294a4c329SAtsushi Nemoto 
16394a4c329SAtsushi Nemoto 	printk(KERN_INFO "%s -- %dMHz(M%dMHz) CRIR:%08x CCFG:%llx PCFG:%llx\n",
16494a4c329SAtsushi Nemoto 	       txx9_pcode_str,
16594a4c329SAtsushi Nemoto 	       (cpuclk + 500000) / 1000000,
16694a4c329SAtsushi Nemoto 	       (txx9_master_clock + 500000) / 1000000,
16794a4c329SAtsushi Nemoto 	       (__u32)____raw_readq(&tx4927_ccfgptr->crir),
16894a4c329SAtsushi Nemoto 	       (unsigned long long)____raw_readq(&tx4927_ccfgptr->ccfg),
16994a4c329SAtsushi Nemoto 	       (unsigned long long)____raw_readq(&tx4927_ccfgptr->pcfg));
17094a4c329SAtsushi Nemoto 
17194a4c329SAtsushi Nemoto 	printk(KERN_INFO "%s SDRAMC --", txx9_pcode_str);
17294a4c329SAtsushi Nemoto 	for (i = 0; i < 4; i++) {
17394a4c329SAtsushi Nemoto 		__u64 cr = TX4927_SDRAMC_CR(i);
17494a4c329SAtsushi Nemoto 		unsigned long base, size;
17594a4c329SAtsushi Nemoto 		if (!((__u32)cr & 0x00000400))
17694a4c329SAtsushi Nemoto 			continue;	/* disabled */
17794a4c329SAtsushi Nemoto 		base = (unsigned long)(cr >> 49) << 21;
17894a4c329SAtsushi Nemoto 		size = (((unsigned long)(cr >> 33) & 0x7fff) + 1) << 21;
17994a4c329SAtsushi Nemoto 		printk(" CR%d:%016llx", i, (unsigned long long)cr);
18094a4c329SAtsushi Nemoto 		tx4927_sdram_resource[i].name = "SDRAM";
18194a4c329SAtsushi Nemoto 		tx4927_sdram_resource[i].start = base;
18294a4c329SAtsushi Nemoto 		tx4927_sdram_resource[i].end = base + size - 1;
18394a4c329SAtsushi Nemoto 		tx4927_sdram_resource[i].flags = IORESOURCE_MEM;
18494a4c329SAtsushi Nemoto 		request_resource(&iomem_resource, &tx4927_sdram_resource[i]);
18594a4c329SAtsushi Nemoto 	}
18694a4c329SAtsushi Nemoto 	printk(" TR:%09llx\n",
18794a4c329SAtsushi Nemoto 	       (unsigned long long)____raw_readq(&tx4927_sdramcptr->tr));
18894a4c329SAtsushi Nemoto 
18994a4c329SAtsushi Nemoto 	/* TMR */
19094a4c329SAtsushi Nemoto 	/* disable all timers */
19194a4c329SAtsushi Nemoto 	for (i = 0; i < TX4927_NR_TMR; i++)
19294a4c329SAtsushi Nemoto 		txx9_tmr_init(TX4927_TMR_REG(i) & 0xfffffffffULL);
19394a4c329SAtsushi Nemoto 
19494a4c329SAtsushi Nemoto 	/* PIO */
19594a4c329SAtsushi Nemoto 	txx9_gpio_init(TX4927_PIO_REG & 0xfffffffffULL, 0, TX4927_NUM_PIO);
19694a4c329SAtsushi Nemoto 	__raw_writel(0, &tx4927_pioptr->maskcpu);
19794a4c329SAtsushi Nemoto 	__raw_writel(0, &tx4927_pioptr->maskext);
198496a3b5cSAtsushi Nemoto 
199496a3b5cSAtsushi Nemoto 	_machine_restart = tx4927_machine_restart;
20094a4c329SAtsushi Nemoto }
20194a4c329SAtsushi Nemoto 
20294a4c329SAtsushi Nemoto void __init tx4927_time_init(unsigned int tmrnr)
20394a4c329SAtsushi Nemoto {
20494a4c329SAtsushi Nemoto 	if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_TINTDIS)
20594a4c329SAtsushi Nemoto 		txx9_clockevent_init(TX4927_TMR_REG(tmrnr) & 0xfffffffffULL,
20694a4c329SAtsushi Nemoto 				     TXX9_IRQ_BASE + TX4927_IR_TMR(tmrnr),
20794a4c329SAtsushi Nemoto 				     TXX9_IMCLK);
20894a4c329SAtsushi Nemoto }
20994a4c329SAtsushi Nemoto 
2107779a5e0SAtsushi Nemoto void __init tx4927_sio_init(unsigned int sclk, unsigned int cts_mask)
21194a4c329SAtsushi Nemoto {
21294a4c329SAtsushi Nemoto 	int i;
21394a4c329SAtsushi Nemoto 
2147779a5e0SAtsushi Nemoto 	for (i = 0; i < 2; i++)
2157779a5e0SAtsushi Nemoto 		txx9_sio_init(TX4927_SIO_REG(i) & 0xfffffffffULL,
2167779a5e0SAtsushi Nemoto 			      TXX9_IRQ_BASE + TX4927_IR_SIO(i),
2177779a5e0SAtsushi Nemoto 			      i, sclk, (1 << i) & cts_mask);
21894a4c329SAtsushi Nemoto }
21951f607c7SAtsushi Nemoto 
22051f607c7SAtsushi Nemoto void __init tx4927_mtd_init(int ch)
22151f607c7SAtsushi Nemoto {
22251f607c7SAtsushi Nemoto 	struct physmap_flash_data pdata = {
22351f607c7SAtsushi Nemoto 		.width = TX4927_EBUSC_WIDTH(ch) / 8,
22451f607c7SAtsushi Nemoto 	};
22551f607c7SAtsushi Nemoto 	unsigned long start = txx9_ce_res[ch].start;
22651f607c7SAtsushi Nemoto 	unsigned long size = txx9_ce_res[ch].end - start + 1;
22751f607c7SAtsushi Nemoto 
22851f607c7SAtsushi Nemoto 	if (!(TX4927_EBUSC_CR(ch) & 0x8))
22951f607c7SAtsushi Nemoto 		return;	/* disabled */
23051f607c7SAtsushi Nemoto 	txx9_physmap_flash_init(ch, start, size, &pdata);
23151f607c7SAtsushi Nemoto }
232