18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * TX4939 setup routines
38c2ecf20Sopenharmony_ci * Based on linux/arch/mips/txx9/generic/setup_tx4938.c,
48c2ecf20Sopenharmony_ci *	    and RBTX49xx patch from CELF patch archive.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * 2003-2005 (c) MontaVista Software, Inc.
78c2ecf20Sopenharmony_ci * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
108c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
118c2ecf20Sopenharmony_ci * for more details.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/ioport.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
178c2ecf20Sopenharmony_ci#include <linux/notifier.h>
188c2ecf20Sopenharmony_ci#include <linux/device.h>
198c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
208c2ecf20Sopenharmony_ci#include <linux/param.h>
218c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
228c2ecf20Sopenharmony_ci#include <linux/mtd/physmap.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_data/txx9/ndfmc.h>
258c2ecf20Sopenharmony_ci#include <asm/reboot.h>
268c2ecf20Sopenharmony_ci#include <asm/traps.h>
278c2ecf20Sopenharmony_ci#include <asm/txx9irq.h>
288c2ecf20Sopenharmony_ci#include <asm/txx9tmr.h>
298c2ecf20Sopenharmony_ci#include <asm/txx9/generic.h>
308c2ecf20Sopenharmony_ci#include <asm/txx9/dmac.h>
318c2ecf20Sopenharmony_ci#include <asm/txx9/tx4939.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void __init tx4939_wdr_init(void)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	/* report watchdog reset status */
368c2ecf20Sopenharmony_ci	if (____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_WDRST)
378c2ecf20Sopenharmony_ci		pr_warn("Watchdog reset detected at 0x%lx\n",
388c2ecf20Sopenharmony_ci			read_c0_errorepc());
398c2ecf20Sopenharmony_ci	/* clear WatchDogReset (W1C) */
408c2ecf20Sopenharmony_ci	tx4939_ccfg_set(TX4939_CCFG_WDRST);
418c2ecf20Sopenharmony_ci	/* do reset on watchdog */
428c2ecf20Sopenharmony_ci	tx4939_ccfg_set(TX4939_CCFG_WR);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid __init tx4939_wdt_init(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	txx9_wdt_init(TX4939_TMR_REG(2) & 0xfffffffffULL);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void tx4939_machine_restart(char *command)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	local_irq_disable();
538c2ecf20Sopenharmony_ci	pr_emerg("Rebooting (with %s watchdog reset)...\n",
548c2ecf20Sopenharmony_ci		 (____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_WDREXEN) ?
558c2ecf20Sopenharmony_ci		 "external" : "internal");
568c2ecf20Sopenharmony_ci	/* clear watchdog status */
578c2ecf20Sopenharmony_ci	tx4939_ccfg_set(TX4939_CCFG_WDRST);	/* W1C */
588c2ecf20Sopenharmony_ci	txx9_wdt_now(TX4939_TMR_REG(2) & 0xfffffffffULL);
598c2ecf20Sopenharmony_ci	while (!(____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_WDRST))
608c2ecf20Sopenharmony_ci		;
618c2ecf20Sopenharmony_ci	mdelay(10);
628c2ecf20Sopenharmony_ci	if (____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_WDREXEN) {
638c2ecf20Sopenharmony_ci		pr_emerg("Rebooting (with internal watchdog reset)...\n");
648c2ecf20Sopenharmony_ci		/* External WDRST failed.  Do internal watchdog reset */
658c2ecf20Sopenharmony_ci		tx4939_ccfg_clear(TX4939_CCFG_WDREXEN);
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci	/* fallback */
688c2ecf20Sopenharmony_ci	(*_machine_halt)();
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_civoid show_registers(struct pt_regs *regs);
728c2ecf20Sopenharmony_cistatic int tx4939_be_handler(struct pt_regs *regs, int is_fixup)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int data = regs->cp0_cause & 4;
758c2ecf20Sopenharmony_ci	console_verbose();
768c2ecf20Sopenharmony_ci	pr_err("%cBE exception at %#lx\n",
778c2ecf20Sopenharmony_ci	       data ? 'D' : 'I', regs->cp0_epc);
788c2ecf20Sopenharmony_ci	pr_err("ccfg:%llx, toea:%llx\n",
798c2ecf20Sopenharmony_ci	       (unsigned long long)____raw_readq(&tx4939_ccfgptr->ccfg),
808c2ecf20Sopenharmony_ci	       (unsigned long long)____raw_readq(&tx4939_ccfgptr->toea));
818c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI
828c2ecf20Sopenharmony_ci	tx4927_report_pcic_status();
838c2ecf20Sopenharmony_ci#endif
848c2ecf20Sopenharmony_ci	show_registers(regs);
858c2ecf20Sopenharmony_ci	panic("BusError!");
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_cistatic void __init tx4939_be_init(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	board_be_handler = tx4939_be_handler;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic struct resource tx4939_sdram_resource[4];
938c2ecf20Sopenharmony_cistatic struct resource tx4939_sram_resource;
948c2ecf20Sopenharmony_ci#define TX4939_SRAM_SIZE 0x800
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_civoid __init tx4939_setup(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	int i;
998c2ecf20Sopenharmony_ci	__u32 divmode;
1008c2ecf20Sopenharmony_ci	__u64 pcfg;
1018c2ecf20Sopenharmony_ci	unsigned int cpuclk = 0;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	txx9_reg_res_init(TX4939_REV_PCODE(), TX4939_REG_BASE,
1048c2ecf20Sopenharmony_ci			  TX4939_REG_SIZE);
1058c2ecf20Sopenharmony_ci	set_c0_config(TX49_CONF_CWFON);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* SDRAMC,EBUSC are configured by PROM */
1088c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1098c2ecf20Sopenharmony_ci		if (!(TX4939_EBUSC_CR(i) & 0x8))
1108c2ecf20Sopenharmony_ci			continue;	/* disabled */
1118c2ecf20Sopenharmony_ci		txx9_ce_res[i].start = (unsigned long)TX4939_EBUSC_BA(i);
1128c2ecf20Sopenharmony_ci		txx9_ce_res[i].end =
1138c2ecf20Sopenharmony_ci			txx9_ce_res[i].start + TX4939_EBUSC_SIZE(i) - 1;
1148c2ecf20Sopenharmony_ci		request_resource(&iomem_resource, &txx9_ce_res[i]);
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* clocks */
1188c2ecf20Sopenharmony_ci	if (txx9_master_clock) {
1198c2ecf20Sopenharmony_ci		/* calculate cpu_clock from master_clock */
1208c2ecf20Sopenharmony_ci		divmode = (__u32)____raw_readq(&tx4939_ccfgptr->ccfg) &
1218c2ecf20Sopenharmony_ci			TX4939_CCFG_MULCLK_MASK;
1228c2ecf20Sopenharmony_ci		cpuclk = txx9_master_clock * 20 / 2;
1238c2ecf20Sopenharmony_ci		switch (divmode) {
1248c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_8:
1258c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 3 * 4 /* / 6 *  8 */; break;
1268c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_9:
1278c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 2 * 3 /* / 6 *  9 */; break;
1288c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_10:
1298c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 3 * 5 /* / 6 * 10 */; break;
1308c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_11:
1318c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 6 * 11; break;
1328c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_12:
1338c2ecf20Sopenharmony_ci			cpuclk = cpuclk * 2 /* / 6 * 12 */; break;
1348c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_13:
1358c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 6 * 13; break;
1368c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_14:
1378c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 3 * 7 /* / 6 * 14 */; break;
1388c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_15:
1398c2ecf20Sopenharmony_ci			cpuclk = cpuclk / 2 * 5 /* / 6 * 15 */; break;
1408c2ecf20Sopenharmony_ci		}
1418c2ecf20Sopenharmony_ci		txx9_cpu_clock = cpuclk;
1428c2ecf20Sopenharmony_ci	} else {
1438c2ecf20Sopenharmony_ci		if (txx9_cpu_clock == 0)
1448c2ecf20Sopenharmony_ci			txx9_cpu_clock = 400000000;	/* 400MHz */
1458c2ecf20Sopenharmony_ci		/* calculate master_clock from cpu_clock */
1468c2ecf20Sopenharmony_ci		cpuclk = txx9_cpu_clock;
1478c2ecf20Sopenharmony_ci		divmode = (__u32)____raw_readq(&tx4939_ccfgptr->ccfg) &
1488c2ecf20Sopenharmony_ci			TX4939_CCFG_MULCLK_MASK;
1498c2ecf20Sopenharmony_ci		switch (divmode) {
1508c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_8:
1518c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 8; break;
1528c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_9:
1538c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 9; break;
1548c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_10:
1558c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 10; break;
1568c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_11:
1578c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 11; break;
1588c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_12:
1598c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 12; break;
1608c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_13:
1618c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 13; break;
1628c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_14:
1638c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 14; break;
1648c2ecf20Sopenharmony_ci		case TX4939_CCFG_MULCLK_15:
1658c2ecf20Sopenharmony_ci			txx9_master_clock = cpuclk * 6 / 15; break;
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci		txx9_master_clock /= 10; /* * 2 / 20 */
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	/* calculate gbus_clock from cpu_clock */
1708c2ecf20Sopenharmony_ci	divmode = (__u32)____raw_readq(&tx4939_ccfgptr->ccfg) &
1718c2ecf20Sopenharmony_ci		TX4939_CCFG_YDIVMODE_MASK;
1728c2ecf20Sopenharmony_ci	txx9_gbus_clock = txx9_cpu_clock;
1738c2ecf20Sopenharmony_ci	switch (divmode) {
1748c2ecf20Sopenharmony_ci	case TX4939_CCFG_YDIVMODE_2:
1758c2ecf20Sopenharmony_ci		txx9_gbus_clock /= 2; break;
1768c2ecf20Sopenharmony_ci	case TX4939_CCFG_YDIVMODE_3:
1778c2ecf20Sopenharmony_ci		txx9_gbus_clock /= 3; break;
1788c2ecf20Sopenharmony_ci	case TX4939_CCFG_YDIVMODE_5:
1798c2ecf20Sopenharmony_ci		txx9_gbus_clock /= 5; break;
1808c2ecf20Sopenharmony_ci	case TX4939_CCFG_YDIVMODE_6:
1818c2ecf20Sopenharmony_ci		txx9_gbus_clock /= 6; break;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	/* change default value to udelay/mdelay take reasonable time */
1848c2ecf20Sopenharmony_ci	loops_per_jiffy = txx9_cpu_clock / HZ / 2;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* CCFG */
1878c2ecf20Sopenharmony_ci	tx4939_wdr_init();
1888c2ecf20Sopenharmony_ci	/* clear BusErrorOnWrite flag (W1C) */
1898c2ecf20Sopenharmony_ci	tx4939_ccfg_set(TX4939_CCFG_WDRST | TX4939_CCFG_BEOW);
1908c2ecf20Sopenharmony_ci	/* enable Timeout BusError */
1918c2ecf20Sopenharmony_ci	if (txx9_ccfg_toeon)
1928c2ecf20Sopenharmony_ci		tx4939_ccfg_set(TX4939_CCFG_TOE);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* DMA selection */
1958c2ecf20Sopenharmony_ci	txx9_clear64(&tx4939_ccfgptr->pcfg, TX4939_PCFG_DMASEL_ALL);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Use external clock for external arbiter */
1988c2ecf20Sopenharmony_ci	if (!(____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_PCIARB))
1998c2ecf20Sopenharmony_ci		txx9_clear64(&tx4939_ccfgptr->pcfg, TX4939_PCFG_PCICLKEN_ALL);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	pr_info("%s -- %dMHz(M%dMHz,G%dMHz) CRIR:%08x CCFG:%llx PCFG:%llx\n",
2028c2ecf20Sopenharmony_ci		txx9_pcode_str,
2038c2ecf20Sopenharmony_ci		(cpuclk + 500000) / 1000000,
2048c2ecf20Sopenharmony_ci		(txx9_master_clock + 500000) / 1000000,
2058c2ecf20Sopenharmony_ci		(txx9_gbus_clock + 500000) / 1000000,
2068c2ecf20Sopenharmony_ci		(__u32)____raw_readq(&tx4939_ccfgptr->crir),
2078c2ecf20Sopenharmony_ci		____raw_readq(&tx4939_ccfgptr->ccfg),
2088c2ecf20Sopenharmony_ci		____raw_readq(&tx4939_ccfgptr->pcfg));
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	pr_info("%s DDRC -- EN:%08x", txx9_pcode_str,
2118c2ecf20Sopenharmony_ci		(__u32)____raw_readq(&tx4939_ddrcptr->winen));
2128c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2138c2ecf20Sopenharmony_ci		__u64 win = ____raw_readq(&tx4939_ddrcptr->win[i]);
2148c2ecf20Sopenharmony_ci		if (!((__u32)____raw_readq(&tx4939_ddrcptr->winen) & (1 << i)))
2158c2ecf20Sopenharmony_ci			continue;	/* disabled */
2168c2ecf20Sopenharmony_ci		pr_cont(" #%d:%016llx", i, win);
2178c2ecf20Sopenharmony_ci		tx4939_sdram_resource[i].name = "DDR SDRAM";
2188c2ecf20Sopenharmony_ci		tx4939_sdram_resource[i].start =
2198c2ecf20Sopenharmony_ci			(unsigned long)(win >> 48) << 20;
2208c2ecf20Sopenharmony_ci		tx4939_sdram_resource[i].end =
2218c2ecf20Sopenharmony_ci			((((unsigned long)(win >> 32) & 0xffff) + 1) <<
2228c2ecf20Sopenharmony_ci			 20) - 1;
2238c2ecf20Sopenharmony_ci		tx4939_sdram_resource[i].flags = IORESOURCE_MEM;
2248c2ecf20Sopenharmony_ci		request_resource(&iomem_resource, &tx4939_sdram_resource[i]);
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci	pr_cont("\n");
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* SRAM */
2298c2ecf20Sopenharmony_ci	if (____raw_readq(&tx4939_sramcptr->cr) & 1) {
2308c2ecf20Sopenharmony_ci		unsigned int size = TX4939_SRAM_SIZE;
2318c2ecf20Sopenharmony_ci		tx4939_sram_resource.name = "SRAM";
2328c2ecf20Sopenharmony_ci		tx4939_sram_resource.start =
2338c2ecf20Sopenharmony_ci			(____raw_readq(&tx4939_sramcptr->cr) >> (39-11))
2348c2ecf20Sopenharmony_ci			& ~(size - 1);
2358c2ecf20Sopenharmony_ci		tx4939_sram_resource.end =
2368c2ecf20Sopenharmony_ci			tx4939_sram_resource.start + TX4939_SRAM_SIZE - 1;
2378c2ecf20Sopenharmony_ci		tx4939_sram_resource.flags = IORESOURCE_MEM;
2388c2ecf20Sopenharmony_ci		request_resource(&iomem_resource, &tx4939_sram_resource);
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* TMR */
2428c2ecf20Sopenharmony_ci	/* disable all timers */
2438c2ecf20Sopenharmony_ci	for (i = 0; i < TX4939_NR_TMR; i++)
2448c2ecf20Sopenharmony_ci		txx9_tmr_init(TX4939_TMR_REG(i) & 0xfffffffffULL);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* set PCIC1 reset (required to prevent hangup on BIST) */
2478c2ecf20Sopenharmony_ci	txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_PCI1RST);
2488c2ecf20Sopenharmony_ci	pcfg = ____raw_readq(&tx4939_ccfgptr->pcfg);
2498c2ecf20Sopenharmony_ci	if (pcfg & (TX4939_PCFG_ET0MODE | TX4939_PCFG_ET1MODE)) {
2508c2ecf20Sopenharmony_ci		mdelay(1);	/* at least 128 cpu clock */
2518c2ecf20Sopenharmony_ci		/* clear PCIC1 reset */
2528c2ecf20Sopenharmony_ci		txx9_clear64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_PCI1RST);
2538c2ecf20Sopenharmony_ci	} else {
2548c2ecf20Sopenharmony_ci		pr_info("%s: stop PCIC1\n", txx9_pcode_str);
2558c2ecf20Sopenharmony_ci		/* stop PCIC1 */
2568c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_PCI1CKD);
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	if (!(pcfg & TX4939_PCFG_ET0MODE)) {
2598c2ecf20Sopenharmony_ci		pr_info("%s: stop ETH0\n", txx9_pcode_str);
2608c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_ETH0RST);
2618c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_ETH0CKD);
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci	if (!(pcfg & TX4939_PCFG_ET1MODE)) {
2648c2ecf20Sopenharmony_ci		pr_info("%s: stop ETH1\n", txx9_pcode_str);
2658c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_ETH1RST);
2668c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, TX4939_CLKCTR_ETH1CKD);
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	_machine_restart = tx4939_machine_restart;
2708c2ecf20Sopenharmony_ci	board_be_init = tx4939_be_init;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_civoid __init tx4939_time_init(unsigned int tmrnr)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	if (____raw_readq(&tx4939_ccfgptr->ccfg) & TX4939_CCFG_TINTDIS)
2768c2ecf20Sopenharmony_ci		txx9_clockevent_init(TX4939_TMR_REG(tmrnr) & 0xfffffffffULL,
2778c2ecf20Sopenharmony_ci				     TXX9_IRQ_BASE + TX4939_IR_TMR(tmrnr),
2788c2ecf20Sopenharmony_ci				     TXX9_IMCLK);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_civoid __init tx4939_sio_init(unsigned int sclk, unsigned int cts_mask)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	int i;
2848c2ecf20Sopenharmony_ci	unsigned int ch_mask = 0;
2858c2ecf20Sopenharmony_ci	__u64 pcfg = __raw_readq(&tx4939_ccfgptr->pcfg);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	cts_mask |= ~1; /* only SIO0 have RTS/CTS */
2888c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_SIO2MODE_MASK) != TX4939_PCFG_SIO2MODE_SIO0)
2898c2ecf20Sopenharmony_ci		cts_mask |= 1 << 0; /* disable SIO0 RTS/CTS by PCFG setting */
2908c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_SIO2MODE_MASK) != TX4939_PCFG_SIO2MODE_SIO2)
2918c2ecf20Sopenharmony_ci		ch_mask |= 1 << 2; /* disable SIO2 by PCFG setting */
2928c2ecf20Sopenharmony_ci	if (pcfg & TX4939_PCFG_SIO3MODE)
2938c2ecf20Sopenharmony_ci		ch_mask |= 1 << 3; /* disable SIO3 by PCFG setting */
2948c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2958c2ecf20Sopenharmony_ci		if ((1 << i) & ch_mask)
2968c2ecf20Sopenharmony_ci			continue;
2978c2ecf20Sopenharmony_ci		txx9_sio_init(TX4939_SIO_REG(i) & 0xfffffffffULL,
2988c2ecf20Sopenharmony_ci			      TXX9_IRQ_BASE + TX4939_IR_SIO(i),
2998c2ecf20Sopenharmony_ci			      i, sclk, (1 << i) & cts_mask);
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_TC35815)
3048c2ecf20Sopenharmony_cistatic u32 tx4939_get_eth_speed(struct net_device *dev)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct ethtool_link_ksettings cmd;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (__ethtool_get_link_ksettings(dev, &cmd))
3098c2ecf20Sopenharmony_ci		return 100;	/* default 100Mbps */
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	return cmd.base.speed;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int tx4939_netdev_event(struct notifier_block *this,
3158c2ecf20Sopenharmony_ci			       unsigned long event,
3168c2ecf20Sopenharmony_ci			       void *ptr)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (event == NETDEV_CHANGE && netif_carrier_ok(dev)) {
3218c2ecf20Sopenharmony_ci		__u64 bit = 0;
3228c2ecf20Sopenharmony_ci		if (dev->irq == TXX9_IRQ_BASE + TX4939_IR_ETH(0))
3238c2ecf20Sopenharmony_ci			bit = TX4939_PCFG_SPEED0;
3248c2ecf20Sopenharmony_ci		else if (dev->irq == TXX9_IRQ_BASE + TX4939_IR_ETH(1))
3258c2ecf20Sopenharmony_ci			bit = TX4939_PCFG_SPEED1;
3268c2ecf20Sopenharmony_ci		if (bit) {
3278c2ecf20Sopenharmony_ci			if (tx4939_get_eth_speed(dev) == 100)
3288c2ecf20Sopenharmony_ci				txx9_set64(&tx4939_ccfgptr->pcfg, bit);
3298c2ecf20Sopenharmony_ci			else
3308c2ecf20Sopenharmony_ci				txx9_clear64(&tx4939_ccfgptr->pcfg, bit);
3318c2ecf20Sopenharmony_ci		}
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic struct notifier_block tx4939_netdev_notifier = {
3378c2ecf20Sopenharmony_ci	.notifier_call = tx4939_netdev_event,
3388c2ecf20Sopenharmony_ci	.priority = 1,
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_civoid __init tx4939_ethaddr_init(unsigned char *addr0, unsigned char *addr1)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	u64 pcfg = __raw_readq(&tx4939_ccfgptr->pcfg);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (addr0 && (pcfg & TX4939_PCFG_ET0MODE))
3468c2ecf20Sopenharmony_ci		txx9_ethaddr_init(TXX9_IRQ_BASE + TX4939_IR_ETH(0), addr0);
3478c2ecf20Sopenharmony_ci	if (addr1 && (pcfg & TX4939_PCFG_ET1MODE))
3488c2ecf20Sopenharmony_ci		txx9_ethaddr_init(TXX9_IRQ_BASE + TX4939_IR_ETH(1), addr1);
3498c2ecf20Sopenharmony_ci	register_netdevice_notifier(&tx4939_netdev_notifier);
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci#else
3528c2ecf20Sopenharmony_civoid __init tx4939_ethaddr_init(unsigned char *addr0, unsigned char *addr1)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci#endif
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_civoid __init tx4939_mtd_init(int ch)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	struct physmap_flash_data pdata = {
3608c2ecf20Sopenharmony_ci		.width = TX4939_EBUSC_WIDTH(ch) / 8,
3618c2ecf20Sopenharmony_ci	};
3628c2ecf20Sopenharmony_ci	unsigned long start = txx9_ce_res[ch].start;
3638c2ecf20Sopenharmony_ci	unsigned long size = txx9_ce_res[ch].end - start + 1;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	if (!(TX4939_EBUSC_CR(ch) & 0x8))
3668c2ecf20Sopenharmony_ci		return; /* disabled */
3678c2ecf20Sopenharmony_ci	txx9_physmap_flash_init(ch, start, size, &pdata);
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci#define TX4939_ATA_REG_PHYS(ch) (TX4939_ATA_REG(ch) & 0xfffffffffULL)
3718c2ecf20Sopenharmony_civoid __init tx4939_ata_init(void)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	static struct resource ata0_res[] = {
3748c2ecf20Sopenharmony_ci		{
3758c2ecf20Sopenharmony_ci			.start = TX4939_ATA_REG_PHYS(0),
3768c2ecf20Sopenharmony_ci			.end = TX4939_ATA_REG_PHYS(0) + 0x1000 - 1,
3778c2ecf20Sopenharmony_ci			.flags = IORESOURCE_MEM,
3788c2ecf20Sopenharmony_ci		}, {
3798c2ecf20Sopenharmony_ci			.start = TXX9_IRQ_BASE + TX4939_IR_ATA(0),
3808c2ecf20Sopenharmony_ci			.flags = IORESOURCE_IRQ,
3818c2ecf20Sopenharmony_ci		},
3828c2ecf20Sopenharmony_ci	};
3838c2ecf20Sopenharmony_ci	static struct resource ata1_res[] = {
3848c2ecf20Sopenharmony_ci		{
3858c2ecf20Sopenharmony_ci			.start = TX4939_ATA_REG_PHYS(1),
3868c2ecf20Sopenharmony_ci			.end = TX4939_ATA_REG_PHYS(1) + 0x1000 - 1,
3878c2ecf20Sopenharmony_ci			.flags = IORESOURCE_MEM,
3888c2ecf20Sopenharmony_ci		}, {
3898c2ecf20Sopenharmony_ci			.start = TXX9_IRQ_BASE + TX4939_IR_ATA(1),
3908c2ecf20Sopenharmony_ci			.flags = IORESOURCE_IRQ,
3918c2ecf20Sopenharmony_ci		},
3928c2ecf20Sopenharmony_ci	};
3938c2ecf20Sopenharmony_ci	static struct platform_device ata0_dev = {
3948c2ecf20Sopenharmony_ci		.name = "tx4939ide",
3958c2ecf20Sopenharmony_ci		.id = 0,
3968c2ecf20Sopenharmony_ci		.num_resources = ARRAY_SIZE(ata0_res),
3978c2ecf20Sopenharmony_ci		.resource = ata0_res,
3988c2ecf20Sopenharmony_ci	};
3998c2ecf20Sopenharmony_ci	static struct platform_device ata1_dev = {
4008c2ecf20Sopenharmony_ci		.name = "tx4939ide",
4018c2ecf20Sopenharmony_ci		.id = 1,
4028c2ecf20Sopenharmony_ci		.num_resources = ARRAY_SIZE(ata1_res),
4038c2ecf20Sopenharmony_ci		.resource = ata1_res,
4048c2ecf20Sopenharmony_ci	};
4058c2ecf20Sopenharmony_ci	__u64 pcfg = __raw_readq(&tx4939_ccfgptr->pcfg);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (pcfg & TX4939_PCFG_ATA0MODE)
4088c2ecf20Sopenharmony_ci		platform_device_register(&ata0_dev);
4098c2ecf20Sopenharmony_ci	if ((pcfg & (TX4939_PCFG_ATA1MODE |
4108c2ecf20Sopenharmony_ci		     TX4939_PCFG_ET1MODE |
4118c2ecf20Sopenharmony_ci		     TX4939_PCFG_ET0MODE)) == TX4939_PCFG_ATA1MODE)
4128c2ecf20Sopenharmony_ci		platform_device_register(&ata1_dev);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_civoid __init tx4939_rtc_init(void)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	static struct resource res[] = {
4188c2ecf20Sopenharmony_ci		{
4198c2ecf20Sopenharmony_ci			.start = TX4939_RTC_REG & 0xfffffffffULL,
4208c2ecf20Sopenharmony_ci			.end = (TX4939_RTC_REG & 0xfffffffffULL) + 0x100 - 1,
4218c2ecf20Sopenharmony_ci			.flags = IORESOURCE_MEM,
4228c2ecf20Sopenharmony_ci		}, {
4238c2ecf20Sopenharmony_ci			.start = TXX9_IRQ_BASE + TX4939_IR_RTC,
4248c2ecf20Sopenharmony_ci			.flags = IORESOURCE_IRQ,
4258c2ecf20Sopenharmony_ci		},
4268c2ecf20Sopenharmony_ci	};
4278c2ecf20Sopenharmony_ci	static struct platform_device rtc_dev = {
4288c2ecf20Sopenharmony_ci		.name = "tx4939rtc",
4298c2ecf20Sopenharmony_ci		.id = -1,
4308c2ecf20Sopenharmony_ci		.num_resources = ARRAY_SIZE(res),
4318c2ecf20Sopenharmony_ci		.resource = res,
4328c2ecf20Sopenharmony_ci	};
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	platform_device_register(&rtc_dev);
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_civoid __init tx4939_ndfmc_init(unsigned int hold, unsigned int spw,
4388c2ecf20Sopenharmony_ci			      unsigned char ch_mask, unsigned char wide_mask)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	struct txx9ndfmc_platform_data plat_data = {
4418c2ecf20Sopenharmony_ci		.shift = 1,
4428c2ecf20Sopenharmony_ci		.gbus_clock = txx9_gbus_clock,
4438c2ecf20Sopenharmony_ci		.hold = hold,
4448c2ecf20Sopenharmony_ci		.spw = spw,
4458c2ecf20Sopenharmony_ci		.flags = NDFMC_PLAT_FLAG_NO_RSTR | NDFMC_PLAT_FLAG_HOLDADD |
4468c2ecf20Sopenharmony_ci			 NDFMC_PLAT_FLAG_DUMMYWRITE,
4478c2ecf20Sopenharmony_ci		.ch_mask = ch_mask,
4488c2ecf20Sopenharmony_ci		.wide_mask = wide_mask,
4498c2ecf20Sopenharmony_ci	};
4508c2ecf20Sopenharmony_ci	txx9_ndfmc_init(TX4939_NDFMC_REG & 0xfffffffffULL, &plat_data);
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_civoid __init tx4939_dmac_init(int memcpy_chan0, int memcpy_chan1)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct txx9dmac_platform_data plat_data = {
4568c2ecf20Sopenharmony_ci		.have_64bit_regs = true,
4578c2ecf20Sopenharmony_ci	};
4588c2ecf20Sopenharmony_ci	int i;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
4618c2ecf20Sopenharmony_ci		plat_data.memcpy_chan = i ? memcpy_chan1 : memcpy_chan0;
4628c2ecf20Sopenharmony_ci		txx9_dmac_init(i, TX4939_DMA_REG(i) & 0xfffffffffULL,
4638c2ecf20Sopenharmony_ci			       TXX9_IRQ_BASE + TX4939_IR_DMA(i, 0),
4648c2ecf20Sopenharmony_ci			       &plat_data);
4658c2ecf20Sopenharmony_ci	}
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_civoid __init tx4939_aclc_init(void)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	u64 pcfg = __raw_readq(&tx4939_ccfgptr->pcfg);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_I2SMODE_MASK) == TX4939_PCFG_I2SMODE_ACLC)
4738c2ecf20Sopenharmony_ci		txx9_aclc_init(TX4939_ACLC_REG & 0xfffffffffULL,
4748c2ecf20Sopenharmony_ci			       TXX9_IRQ_BASE + TX4939_IR_ACLC, 1, 0, 1);
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_civoid __init tx4939_sramc_init(void)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	if (tx4939_sram_resource.start)
4808c2ecf20Sopenharmony_ci		txx9_sramc_init(&tx4939_sram_resource);
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_civoid __init tx4939_rng_init(void)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	static struct resource res = {
4868c2ecf20Sopenharmony_ci		.start = TX4939_RNG_REG & 0xfffffffffULL,
4878c2ecf20Sopenharmony_ci		.end = (TX4939_RNG_REG & 0xfffffffffULL) + 0x30 - 1,
4888c2ecf20Sopenharmony_ci		.flags = IORESOURCE_MEM,
4898c2ecf20Sopenharmony_ci	};
4908c2ecf20Sopenharmony_ci	static struct platform_device pdev = {
4918c2ecf20Sopenharmony_ci		.name = "tx4939-rng",
4928c2ecf20Sopenharmony_ci		.id = -1,
4938c2ecf20Sopenharmony_ci		.num_resources = 1,
4948c2ecf20Sopenharmony_ci		.resource = &res,
4958c2ecf20Sopenharmony_ci	};
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	platform_device_register(&pdev);
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic void __init tx4939_stop_unused_modules(void)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	__u64 pcfg, rst = 0, ckd = 0;
5038c2ecf20Sopenharmony_ci	char buf[128];
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	buf[0] = '\0';
5068c2ecf20Sopenharmony_ci	local_irq_disable();
5078c2ecf20Sopenharmony_ci	pcfg = ____raw_readq(&tx4939_ccfgptr->pcfg);
5088c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_I2SMODE_MASK) !=
5098c2ecf20Sopenharmony_ci	    TX4939_PCFG_I2SMODE_ACLC) {
5108c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_ACLRST;
5118c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_ACLCKD;
5128c2ecf20Sopenharmony_ci		strcat(buf, " ACLC");
5138c2ecf20Sopenharmony_ci	}
5148c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_I2SMODE_MASK) !=
5158c2ecf20Sopenharmony_ci	    TX4939_PCFG_I2SMODE_I2S &&
5168c2ecf20Sopenharmony_ci	    (pcfg & TX4939_PCFG_I2SMODE_MASK) !=
5178c2ecf20Sopenharmony_ci	    TX4939_PCFG_I2SMODE_I2S_ALT) {
5188c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_I2SRST;
5198c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_I2SCKD;
5208c2ecf20Sopenharmony_ci		strcat(buf, " I2S");
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci	if (!(pcfg & TX4939_PCFG_ATA0MODE)) {
5238c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_ATA0RST;
5248c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_ATA0CKD;
5258c2ecf20Sopenharmony_ci		strcat(buf, " ATA0");
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci	if (!(pcfg & TX4939_PCFG_ATA1MODE)) {
5288c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_ATA1RST;
5298c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_ATA1CKD;
5308c2ecf20Sopenharmony_ci		strcat(buf, " ATA1");
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci	if (pcfg & TX4939_PCFG_SPIMODE) {
5338c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_SPIRST;
5348c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_SPICKD;
5358c2ecf20Sopenharmony_ci		strcat(buf, " SPI");
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	if (!(pcfg & (TX4939_PCFG_VSSMODE | TX4939_PCFG_VPSMODE))) {
5388c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_VPCRST;
5398c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_VPCCKD;
5408c2ecf20Sopenharmony_ci		strcat(buf, " VPC");
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci	if ((pcfg & TX4939_PCFG_SIO2MODE_MASK) != TX4939_PCFG_SIO2MODE_SIO2) {
5438c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_SIO2RST;
5448c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_SIO2CKD;
5458c2ecf20Sopenharmony_ci		strcat(buf, " SIO2");
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci	if (pcfg & TX4939_PCFG_SIO3MODE) {
5488c2ecf20Sopenharmony_ci		rst |= TX4939_CLKCTR_SIO3RST;
5498c2ecf20Sopenharmony_ci		ckd |= TX4939_CLKCTR_SIO3CKD;
5508c2ecf20Sopenharmony_ci		strcat(buf, " SIO3");
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci	if (rst | ckd) {
5538c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, rst);
5548c2ecf20Sopenharmony_ci		txx9_set64(&tx4939_ccfgptr->clkctr, ckd);
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci	local_irq_enable();
5578c2ecf20Sopenharmony_ci	if (buf[0])
5588c2ecf20Sopenharmony_ci		pr_info("%s: stop%s\n", txx9_pcode_str, buf);
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic int __init tx4939_late_init(void)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	if (txx9_pcode != 0x4939)
5648c2ecf20Sopenharmony_ci		return -ENODEV;
5658c2ecf20Sopenharmony_ci	tx4939_stop_unused_modules();
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_cilate_initcall(tx4939_late_init);
569