18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2015 Nikolay Martynov <mar.kolya@gmail.com>
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 John Crispin <john@phrozen.org>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/sys_soc.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
148c2ecf20Sopenharmony_ci#include <asm/smp-ops.h>
158c2ecf20Sopenharmony_ci#include <asm/mips-cps.h>
168c2ecf20Sopenharmony_ci#include <asm/mach-ralink/ralink_regs.h>
178c2ecf20Sopenharmony_ci#include <asm/mach-ralink/mt7621.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <pinmux.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "common.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART1		1
248c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_I2C		2
258c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART3_MASK	0x3
268c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART3_SHIFT	3
278c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART3_GPIO	1
288c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART2_MASK	0x3
298c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART2_SHIFT	5
308c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_UART2_GPIO	1
318c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_JTAG		7
328c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_WDT_MASK	0x3
338c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_WDT_SHIFT	8
348c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_WDT_GPIO	1
358c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_PCIE_RST	0
368c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_PCIE_REF	2
378c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_PCIE_MASK	0x3
388c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_PCIE_SHIFT	10
398c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_PCIE_GPIO	1
408c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_MDIO_MASK	0x3
418c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_MDIO_SHIFT	12
428c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_MDIO_GPIO	1
438c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_RGMII1		14
448c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_RGMII2		15
458c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SPI_MASK	0x3
468c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SPI_SHIFT	16
478c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SPI_GPIO	1
488c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SDHCI_MASK	0x3
498c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SDHCI_SHIFT	18
508c2ecf20Sopenharmony_ci#define MT7621_GPIO_MODE_SDHCI_GPIO	1
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func uart1_grp[] =  { FUNC("uart1", 0, 1, 2) };
538c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func i2c_grp[] =  { FUNC("i2c", 0, 3, 2) };
548c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func uart3_grp[] = {
558c2ecf20Sopenharmony_ci	FUNC("uart3", 0, 5, 4),
568c2ecf20Sopenharmony_ci	FUNC("i2s", 2, 5, 4),
578c2ecf20Sopenharmony_ci	FUNC("spdif3", 3, 5, 4),
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func uart2_grp[] = {
608c2ecf20Sopenharmony_ci	FUNC("uart2", 0, 9, 4),
618c2ecf20Sopenharmony_ci	FUNC("pcm", 2, 9, 4),
628c2ecf20Sopenharmony_ci	FUNC("spdif2", 3, 9, 4),
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func jtag_grp[] = { FUNC("jtag", 0, 13, 5) };
658c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func wdt_grp[] = {
668c2ecf20Sopenharmony_ci	FUNC("wdt rst", 0, 18, 1),
678c2ecf20Sopenharmony_ci	FUNC("wdt refclk", 2, 18, 1),
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func pcie_rst_grp[] = {
708c2ecf20Sopenharmony_ci	FUNC("pcie rst", MT7621_GPIO_MODE_PCIE_RST, 19, 1),
718c2ecf20Sopenharmony_ci	FUNC("pcie refclk", MT7621_GPIO_MODE_PCIE_REF, 19, 1)
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func mdio_grp[] = { FUNC("mdio", 0, 20, 2) };
748c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func rgmii2_grp[] = { FUNC("rgmii2", 0, 22, 12) };
758c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func spi_grp[] = {
768c2ecf20Sopenharmony_ci	FUNC("spi", 0, 34, 7),
778c2ecf20Sopenharmony_ci	FUNC("nand1", 2, 34, 7),
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func sdhci_grp[] = {
808c2ecf20Sopenharmony_ci	FUNC("sdhci", 0, 41, 8),
818c2ecf20Sopenharmony_ci	FUNC("nand2", 2, 41, 8),
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_cistatic struct rt2880_pmx_func rgmii1_grp[] = { FUNC("rgmii1", 0, 49, 12) };
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic struct rt2880_pmx_group mt7621_pinmux_data[] = {
868c2ecf20Sopenharmony_ci	GRP("uart1", uart1_grp, 1, MT7621_GPIO_MODE_UART1),
878c2ecf20Sopenharmony_ci	GRP("i2c", i2c_grp, 1, MT7621_GPIO_MODE_I2C),
888c2ecf20Sopenharmony_ci	GRP_G("uart3", uart3_grp, MT7621_GPIO_MODE_UART3_MASK,
898c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_UART3_GPIO, MT7621_GPIO_MODE_UART3_SHIFT),
908c2ecf20Sopenharmony_ci	GRP_G("uart2", uart2_grp, MT7621_GPIO_MODE_UART2_MASK,
918c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_UART2_GPIO, MT7621_GPIO_MODE_UART2_SHIFT),
928c2ecf20Sopenharmony_ci	GRP("jtag", jtag_grp, 1, MT7621_GPIO_MODE_JTAG),
938c2ecf20Sopenharmony_ci	GRP_G("wdt", wdt_grp, MT7621_GPIO_MODE_WDT_MASK,
948c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_WDT_GPIO, MT7621_GPIO_MODE_WDT_SHIFT),
958c2ecf20Sopenharmony_ci	GRP_G("pcie", pcie_rst_grp, MT7621_GPIO_MODE_PCIE_MASK,
968c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_PCIE_GPIO, MT7621_GPIO_MODE_PCIE_SHIFT),
978c2ecf20Sopenharmony_ci	GRP_G("mdio", mdio_grp, MT7621_GPIO_MODE_MDIO_MASK,
988c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_MDIO_GPIO, MT7621_GPIO_MODE_MDIO_SHIFT),
998c2ecf20Sopenharmony_ci	GRP("rgmii2", rgmii2_grp, 1, MT7621_GPIO_MODE_RGMII2),
1008c2ecf20Sopenharmony_ci	GRP_G("spi", spi_grp, MT7621_GPIO_MODE_SPI_MASK,
1018c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_SPI_GPIO, MT7621_GPIO_MODE_SPI_SHIFT),
1028c2ecf20Sopenharmony_ci	GRP_G("sdhci", sdhci_grp, MT7621_GPIO_MODE_SDHCI_MASK,
1038c2ecf20Sopenharmony_ci		MT7621_GPIO_MODE_SDHCI_GPIO, MT7621_GPIO_MODE_SDHCI_SHIFT),
1048c2ecf20Sopenharmony_ci	GRP("rgmii1", rgmii1_grp, 1, MT7621_GPIO_MODE_RGMII1),
1058c2ecf20Sopenharmony_ci	{ 0 }
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciphys_addr_t mips_cpc_default_phys_base(void)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	panic("Cannot detect cpc address");
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_civoid __init ralink_of_remap(void)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	rt_sysc_membase = plat_of_remap_node("mtk,mt7621-sysc");
1168c2ecf20Sopenharmony_ci	rt_memc_membase = plat_of_remap_node("mtk,mt7621-memc");
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!rt_sysc_membase || !rt_memc_membase)
1198c2ecf20Sopenharmony_ci		panic("Failed to remap core resources");
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic void soc_dev_init(struct ralink_soc_info *soc_info, u32 rev)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct soc_device *soc_dev;
1258c2ecf20Sopenharmony_ci	struct soc_device_attribute *soc_dev_attr;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
1288c2ecf20Sopenharmony_ci	if (!soc_dev_attr)
1298c2ecf20Sopenharmony_ci		return;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	soc_dev_attr->soc_id = "mt7621";
1328c2ecf20Sopenharmony_ci	soc_dev_attr->family = "Ralink";
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (((rev >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK) == 1 &&
1358c2ecf20Sopenharmony_ci	    (rev & CHIP_REV_ECO_MASK) == 1)
1368c2ecf20Sopenharmony_ci		soc_dev_attr->revision = "E2";
1378c2ecf20Sopenharmony_ci	else
1388c2ecf20Sopenharmony_ci		soc_dev_attr->revision = "E1";
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	soc_dev_attr->data = soc_info;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	soc_dev = soc_device_register(soc_dev_attr);
1438c2ecf20Sopenharmony_ci	if (IS_ERR(soc_dev)) {
1448c2ecf20Sopenharmony_ci		kfree(soc_dev_attr);
1458c2ecf20Sopenharmony_ci		return;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_civoid prom_soc_init(struct ralink_soc_info *soc_info)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	void __iomem *sysc = (void __iomem *) KSEG1ADDR(MT7621_SYSC_BASE);
1528c2ecf20Sopenharmony_ci	unsigned char *name = NULL;
1538c2ecf20Sopenharmony_ci	u32 n0;
1548c2ecf20Sopenharmony_ci	u32 n1;
1558c2ecf20Sopenharmony_ci	u32 rev;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Early detection of CMP support */
1588c2ecf20Sopenharmony_ci	mips_cm_probe();
1598c2ecf20Sopenharmony_ci	mips_cpc_probe();
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if (mips_cps_numiocu(0)) {
1628c2ecf20Sopenharmony_ci		/*
1638c2ecf20Sopenharmony_ci		 * mips_cm_probe() wipes out bootloader
1648c2ecf20Sopenharmony_ci		 * config for CM regions and we have to configure them
1658c2ecf20Sopenharmony_ci		 * again. This SoC cannot talk to pamlbus devices
1668c2ecf20Sopenharmony_ci		 * witout proper iocu region set up.
1678c2ecf20Sopenharmony_ci		 *
1688c2ecf20Sopenharmony_ci		 * FIXME: it would be better to do this with values
1698c2ecf20Sopenharmony_ci		 * from DT, but we need this very early because
1708c2ecf20Sopenharmony_ci		 * without this we cannot talk to pretty much anything
1718c2ecf20Sopenharmony_ci		 * including serial.
1728c2ecf20Sopenharmony_ci		 */
1738c2ecf20Sopenharmony_ci		write_gcr_reg0_base(MT7621_PALMBUS_BASE);
1748c2ecf20Sopenharmony_ci		write_gcr_reg0_mask(~MT7621_PALMBUS_SIZE |
1758c2ecf20Sopenharmony_ci				    CM_GCR_REGn_MASK_CMTGT_IOCU0);
1768c2ecf20Sopenharmony_ci		__sync();
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	n0 = __raw_readl(sysc + SYSC_REG_CHIP_NAME0);
1808c2ecf20Sopenharmony_ci	n1 = __raw_readl(sysc + SYSC_REG_CHIP_NAME1);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (n0 == MT7621_CHIP_NAME0 && n1 == MT7621_CHIP_NAME1) {
1838c2ecf20Sopenharmony_ci		name = "MT7621";
1848c2ecf20Sopenharmony_ci		soc_info->compatible = "mtk,mt7621-soc";
1858c2ecf20Sopenharmony_ci	} else {
1868c2ecf20Sopenharmony_ci		panic("mt7621: unknown SoC, n0:%08x n1:%08x\n", n0, n1);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	ralink_soc = MT762X_SOC_MT7621AT;
1898c2ecf20Sopenharmony_ci	rev = __raw_readl(sysc + SYSC_REG_CHIP_REV);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
1928c2ecf20Sopenharmony_ci		"MediaTek %s ver:%u eco:%u",
1938c2ecf20Sopenharmony_ci		name,
1948c2ecf20Sopenharmony_ci		(rev >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK,
1958c2ecf20Sopenharmony_ci		(rev & CHIP_REV_ECO_MASK));
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	soc_info->mem_size_min = MT7621_DDR2_SIZE_MIN;
1988c2ecf20Sopenharmony_ci	soc_info->mem_size_max = MT7621_DDR2_SIZE_MAX;
1998c2ecf20Sopenharmony_ci	soc_info->mem_base = MT7621_DRAM_BASE;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	rt2880_pinmux_data = mt7621_pinmux_data;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	soc_dev_init(soc_info, rev);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (!register_cps_smp_ops())
2068c2ecf20Sopenharmony_ci		return;
2078c2ecf20Sopenharmony_ci	if (!register_cmp_smp_ops())
2088c2ecf20Sopenharmony_ci		return;
2098c2ecf20Sopenharmony_ci	if (!register_vsmp_smp_ops())
2108c2ecf20Sopenharmony_ci		return;
2118c2ecf20Sopenharmony_ci}
212