18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Versatile board support using the device tree
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2010 Secret Lab Technologies Ltd.
68c2ecf20Sopenharmony_ci *  Copyright (C) 2009 Jeremy Kerr <jeremy.kerr@canonical.com>
78c2ecf20Sopenharmony_ci *  Copyright (C) 2004 ARM Limited
88c2ecf20Sopenharmony_ci *  Copyright (C) 2000 Deep Blue Solutions Ltd
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/of.h>
148c2ecf20Sopenharmony_ci#include <linux/of_address.h>
158c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
168c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/amba/bus.h>
198c2ecf20Sopenharmony_ci#include <linux/amba/mmci.h>
208c2ecf20Sopenharmony_ci#include <asm/mach-types.h>
218c2ecf20Sopenharmony_ci#include <asm/mach/arch.h>
228c2ecf20Sopenharmony_ci#include <asm/mach/map.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* macro to get at MMIO space when running virtually */
258c2ecf20Sopenharmony_ci#define IO_ADDRESS(x)		(((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
268c2ecf20Sopenharmony_ci#define __io_address(n)		((void __iomem __force *)IO_ADDRESS(n))
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * ------------------------------------------------------------------------
308c2ecf20Sopenharmony_ci *  Versatile Registers
318c2ecf20Sopenharmony_ci * ------------------------------------------------------------------------
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define VERSATILE_SYS_PCICTL_OFFSET           0x44
348c2ecf20Sopenharmony_ci#define VERSATILE_SYS_MCI_OFFSET              0x48
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * VERSATILE peripheral addresses
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci#define VERSATILE_MMCI0_BASE           0x10005000	/* MMC interface */
408c2ecf20Sopenharmony_ci#define VERSATILE_MMCI1_BASE           0x1000B000	/* MMC Interface */
418c2ecf20Sopenharmony_ci#define VERSATILE_SCTL_BASE            0x101E0000	/* System controller */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * System controller bit assignment
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ci#define VERSATILE_REFCLK	0
478c2ecf20Sopenharmony_ci#define VERSATILE_TIMCLK	1
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define VERSATILE_TIMER1_EnSel	15
508c2ecf20Sopenharmony_ci#define VERSATILE_TIMER2_EnSel	17
518c2ecf20Sopenharmony_ci#define VERSATILE_TIMER3_EnSel	19
528c2ecf20Sopenharmony_ci#define VERSATILE_TIMER4_EnSel	21
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void __iomem *versatile_sys_base;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciunsigned int mmc_status(struct device *dev)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct amba_device *adev = container_of(dev, struct amba_device, dev);
598c2ecf20Sopenharmony_ci	u32 mask;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (adev->res.start == VERSATILE_MMCI0_BASE)
628c2ecf20Sopenharmony_ci		mask = 1;
638c2ecf20Sopenharmony_ci	else
648c2ecf20Sopenharmony_ci		mask = 2;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	return readl(versatile_sys_base + VERSATILE_SYS_MCI_OFFSET) & mask;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct mmci_platform_data mmc0_plat_data = {
708c2ecf20Sopenharmony_ci	.ocr_mask	= MMC_VDD_32_33|MMC_VDD_33_34,
718c2ecf20Sopenharmony_ci	.status		= mmc_status,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic struct mmci_platform_data mmc1_plat_data = {
758c2ecf20Sopenharmony_ci	.ocr_mask	= MMC_VDD_32_33|MMC_VDD_33_34,
768c2ecf20Sopenharmony_ci	.status		= mmc_status,
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Lookup table for attaching a specific name and platform_data pointer to
818c2ecf20Sopenharmony_ci * devices as they get created by of_platform_populate().  Ideally this table
828c2ecf20Sopenharmony_ci * would not exist, but the current clock implementation depends on some devices
838c2ecf20Sopenharmony_ci * having a specific name.
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_cistruct of_dev_auxdata versatile_auxdata_lookup[] __initdata = {
868c2ecf20Sopenharmony_ci	OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI0_BASE, "fpga:05", &mmc0_plat_data),
878c2ecf20Sopenharmony_ci	OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI1_BASE, "fpga:0b", &mmc1_plat_data),
888c2ecf20Sopenharmony_ci	{}
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct map_desc versatile_io_desc[] __initdata __maybe_unused = {
928c2ecf20Sopenharmony_ci	{
938c2ecf20Sopenharmony_ci		.virtual	=  IO_ADDRESS(VERSATILE_SCTL_BASE),
948c2ecf20Sopenharmony_ci		.pfn		= __phys_to_pfn(VERSATILE_SCTL_BASE),
958c2ecf20Sopenharmony_ci		.length		= SZ_4K * 9,
968c2ecf20Sopenharmony_ci		.type		= MT_DEVICE
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void __init versatile_map_io(void)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	debug_ll_io_init();
1038c2ecf20Sopenharmony_ci	iotable_init(versatile_io_desc, ARRAY_SIZE(versatile_io_desc));
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic void __init versatile_init_early(void)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	u32 val;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/*
1118c2ecf20Sopenharmony_ci	 * set clock frequency:
1128c2ecf20Sopenharmony_ci	 *	VERSATILE_REFCLK is 32KHz
1138c2ecf20Sopenharmony_ci	 *	VERSATILE_TIMCLK is 1MHz
1148c2ecf20Sopenharmony_ci	 */
1158c2ecf20Sopenharmony_ci	val = readl(__io_address(VERSATILE_SCTL_BASE));
1168c2ecf20Sopenharmony_ci	writel((VERSATILE_TIMCLK << VERSATILE_TIMER1_EnSel) |
1178c2ecf20Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER2_EnSel) |
1188c2ecf20Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER3_EnSel) |
1198c2ecf20Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER4_EnSel) | val,
1208c2ecf20Sopenharmony_ci	       __io_address(VERSATILE_SCTL_BASE));
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic void __init versatile_dt_pci_init(void)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	u32 val;
1268c2ecf20Sopenharmony_ci	struct device_node *np;
1278c2ecf20Sopenharmony_ci	struct property *newprop;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "arm,versatile-pci");
1308c2ecf20Sopenharmony_ci	if (!np)
1318c2ecf20Sopenharmony_ci		return;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Check if PCI backplane is detected */
1348c2ecf20Sopenharmony_ci	val = readl(versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
1358c2ecf20Sopenharmony_ci	if (val & 1) {
1368c2ecf20Sopenharmony_ci		/*
1378c2ecf20Sopenharmony_ci		 * Enable PCI accesses. Note that the documentaton is
1388c2ecf20Sopenharmony_ci		 * inconsistent whether or not this is needed, but the old
1398c2ecf20Sopenharmony_ci		 * driver had it so we will keep it.
1408c2ecf20Sopenharmony_ci		 */
1418c2ecf20Sopenharmony_ci		writel(1, versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
1428c2ecf20Sopenharmony_ci		goto out_put_node;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	newprop = kzalloc(sizeof(*newprop), GFP_KERNEL);
1468c2ecf20Sopenharmony_ci	if (!newprop)
1478c2ecf20Sopenharmony_ci		goto out_put_node;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	newprop->name = kstrdup("status", GFP_KERNEL);
1508c2ecf20Sopenharmony_ci	newprop->value = kstrdup("disabled", GFP_KERNEL);
1518c2ecf20Sopenharmony_ci	newprop->length = sizeof("disabled");
1528c2ecf20Sopenharmony_ci	of_update_property(np, newprop);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	pr_info("Not plugged into PCI backplane!\n");
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciout_put_node:
1578c2ecf20Sopenharmony_ci	of_node_put(np);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic void __init versatile_dt_init(void)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct device_node *np;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "arm,core-module-versatile");
1658c2ecf20Sopenharmony_ci	if (np)
1668c2ecf20Sopenharmony_ci		versatile_sys_base = of_iomap(np, 0);
1678c2ecf20Sopenharmony_ci	WARN_ON(!versatile_sys_base);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	versatile_dt_pci_init();
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	of_platform_default_populate(NULL, versatile_auxdata_lookup, NULL);
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const char *const versatile_dt_match[] __initconst = {
1758c2ecf20Sopenharmony_ci	"arm,versatile-ab",
1768c2ecf20Sopenharmony_ci	"arm,versatile-pb",
1778c2ecf20Sopenharmony_ci	NULL,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciDT_MACHINE_START(VERSATILE_PB, "ARM-Versatile (Device Tree Support)")
1818c2ecf20Sopenharmony_ci	.map_io		= versatile_map_io,
1828c2ecf20Sopenharmony_ci	.init_early	= versatile_init_early,
1838c2ecf20Sopenharmony_ci	.init_machine	= versatile_dt_init,
1848c2ecf20Sopenharmony_ci	.dt_compat	= versatile_dt_match,
1858c2ecf20Sopenharmony_ciMACHINE_END
186