162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Versatile board support using the device tree
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 2010 Secret Lab Technologies Ltd.
662306a36Sopenharmony_ci *  Copyright (C) 2009 Jeremy Kerr <jeremy.kerr@canonical.com>
762306a36Sopenharmony_ci *  Copyright (C) 2004 ARM Limited
862306a36Sopenharmony_ci *  Copyright (C) 2000 Deep Blue Solutions Ltd
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/io.h>
1362306a36Sopenharmony_ci#include <linux/of.h>
1462306a36Sopenharmony_ci#include <linux/of_address.h>
1562306a36Sopenharmony_ci#include <linux/of_irq.h>
1662306a36Sopenharmony_ci#include <linux/of_platform.h>
1762306a36Sopenharmony_ci#include <linux/slab.h>
1862306a36Sopenharmony_ci#include <linux/amba/bus.h>
1962306a36Sopenharmony_ci#include <linux/amba/mmci.h>
2062306a36Sopenharmony_ci#include <asm/mach-types.h>
2162306a36Sopenharmony_ci#include <asm/mach/arch.h>
2262306a36Sopenharmony_ci#include <asm/mach/map.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/* macro to get at MMIO space when running virtually */
2562306a36Sopenharmony_ci#define IO_ADDRESS(x)		(((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
2662306a36Sopenharmony_ci#define __io_address(n)		((void __iomem __force *)IO_ADDRESS(n))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * ------------------------------------------------------------------------
3062306a36Sopenharmony_ci *  Versatile Registers
3162306a36Sopenharmony_ci * ------------------------------------------------------------------------
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci#define VERSATILE_SYS_PCICTL_OFFSET           0x44
3462306a36Sopenharmony_ci#define VERSATILE_SYS_MCI_OFFSET              0x48
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/*
3762306a36Sopenharmony_ci * VERSATILE peripheral addresses
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_ci#define VERSATILE_MMCI0_BASE           0x10005000	/* MMC interface */
4062306a36Sopenharmony_ci#define VERSATILE_MMCI1_BASE           0x1000B000	/* MMC Interface */
4162306a36Sopenharmony_ci#define VERSATILE_SCTL_BASE            0x101E0000	/* System controller */
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * System controller bit assignment
4562306a36Sopenharmony_ci */
4662306a36Sopenharmony_ci#define VERSATILE_REFCLK	0
4762306a36Sopenharmony_ci#define VERSATILE_TIMCLK	1
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define VERSATILE_TIMER1_EnSel	15
5062306a36Sopenharmony_ci#define VERSATILE_TIMER2_EnSel	17
5162306a36Sopenharmony_ci#define VERSATILE_TIMER3_EnSel	19
5262306a36Sopenharmony_ci#define VERSATILE_TIMER4_EnSel	21
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic void __iomem *versatile_sys_base;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic unsigned int mmc_status(struct device *dev)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	struct amba_device *adev = container_of(dev, struct amba_device, dev);
5962306a36Sopenharmony_ci	u32 mask;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	if (adev->res.start == VERSATILE_MMCI0_BASE)
6262306a36Sopenharmony_ci		mask = 1;
6362306a36Sopenharmony_ci	else
6462306a36Sopenharmony_ci		mask = 2;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	return readl(versatile_sys_base + VERSATILE_SYS_MCI_OFFSET) & mask;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic struct mmci_platform_data mmc0_plat_data = {
7062306a36Sopenharmony_ci	.ocr_mask	= MMC_VDD_32_33|MMC_VDD_33_34,
7162306a36Sopenharmony_ci	.status		= mmc_status,
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic struct mmci_platform_data mmc1_plat_data = {
7562306a36Sopenharmony_ci	.ocr_mask	= MMC_VDD_32_33|MMC_VDD_33_34,
7662306a36Sopenharmony_ci	.status		= mmc_status,
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/*
8062306a36Sopenharmony_ci * Lookup table for attaching a specific name and platform_data pointer to
8162306a36Sopenharmony_ci * devices as they get created by of_platform_populate().  Ideally this table
8262306a36Sopenharmony_ci * would not exist, but the current clock implementation depends on some devices
8362306a36Sopenharmony_ci * having a specific name.
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_cistruct of_dev_auxdata versatile_auxdata_lookup[] __initdata = {
8662306a36Sopenharmony_ci	OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI0_BASE, "fpga:05", &mmc0_plat_data),
8762306a36Sopenharmony_ci	OF_DEV_AUXDATA("arm,primecell", VERSATILE_MMCI1_BASE, "fpga:0b", &mmc1_plat_data),
8862306a36Sopenharmony_ci	{}
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic struct map_desc versatile_io_desc[] __initdata __maybe_unused = {
9262306a36Sopenharmony_ci	{
9362306a36Sopenharmony_ci		.virtual	=  IO_ADDRESS(VERSATILE_SCTL_BASE),
9462306a36Sopenharmony_ci		.pfn		= __phys_to_pfn(VERSATILE_SCTL_BASE),
9562306a36Sopenharmony_ci		.length		= SZ_4K * 9,
9662306a36Sopenharmony_ci		.type		= MT_DEVICE
9762306a36Sopenharmony_ci	}
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic void __init versatile_map_io(void)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	debug_ll_io_init();
10362306a36Sopenharmony_ci	iotable_init(versatile_io_desc, ARRAY_SIZE(versatile_io_desc));
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic void __init versatile_init_early(void)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	u32 val;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	/*
11162306a36Sopenharmony_ci	 * set clock frequency:
11262306a36Sopenharmony_ci	 *	VERSATILE_REFCLK is 32KHz
11362306a36Sopenharmony_ci	 *	VERSATILE_TIMCLK is 1MHz
11462306a36Sopenharmony_ci	 */
11562306a36Sopenharmony_ci	val = readl(__io_address(VERSATILE_SCTL_BASE));
11662306a36Sopenharmony_ci	writel((VERSATILE_TIMCLK << VERSATILE_TIMER1_EnSel) |
11762306a36Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER2_EnSel) |
11862306a36Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER3_EnSel) |
11962306a36Sopenharmony_ci	       (VERSATILE_TIMCLK << VERSATILE_TIMER4_EnSel) | val,
12062306a36Sopenharmony_ci	       __io_address(VERSATILE_SCTL_BASE));
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic void __init versatile_dt_pci_init(void)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	u32 val;
12662306a36Sopenharmony_ci	struct device_node *np;
12762306a36Sopenharmony_ci	struct property *newprop;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "arm,versatile-pci");
13062306a36Sopenharmony_ci	if (!np)
13162306a36Sopenharmony_ci		return;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* Check if PCI backplane is detected */
13462306a36Sopenharmony_ci	val = readl(versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
13562306a36Sopenharmony_ci	if (val & 1) {
13662306a36Sopenharmony_ci		/*
13762306a36Sopenharmony_ci		 * Enable PCI accesses. Note that the documentaton is
13862306a36Sopenharmony_ci		 * inconsistent whether or not this is needed, but the old
13962306a36Sopenharmony_ci		 * driver had it so we will keep it.
14062306a36Sopenharmony_ci		 */
14162306a36Sopenharmony_ci		writel(1, versatile_sys_base + VERSATILE_SYS_PCICTL_OFFSET);
14262306a36Sopenharmony_ci		goto out_put_node;
14362306a36Sopenharmony_ci	}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	newprop = kzalloc(sizeof(*newprop), GFP_KERNEL);
14662306a36Sopenharmony_ci	if (!newprop)
14762306a36Sopenharmony_ci		goto out_put_node;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	newprop->name = kstrdup("status", GFP_KERNEL);
15062306a36Sopenharmony_ci	newprop->value = kstrdup("disabled", GFP_KERNEL);
15162306a36Sopenharmony_ci	newprop->length = sizeof("disabled");
15262306a36Sopenharmony_ci	of_update_property(np, newprop);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	pr_info("Not plugged into PCI backplane!\n");
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciout_put_node:
15762306a36Sopenharmony_ci	of_node_put(np);
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic void __init versatile_dt_init(void)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	struct device_node *np;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "arm,core-module-versatile");
16562306a36Sopenharmony_ci	if (np)
16662306a36Sopenharmony_ci		versatile_sys_base = of_iomap(np, 0);
16762306a36Sopenharmony_ci	WARN_ON(!versatile_sys_base);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	versatile_dt_pci_init();
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	of_platform_default_populate(NULL, versatile_auxdata_lookup, NULL);
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic const char *const versatile_dt_match[] __initconst = {
17562306a36Sopenharmony_ci	"arm,versatile-ab",
17662306a36Sopenharmony_ci	"arm,versatile-pb",
17762306a36Sopenharmony_ci	NULL,
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciDT_MACHINE_START(VERSATILE_PB, "ARM-Versatile (Device Tree Support)")
18162306a36Sopenharmony_ci	.map_io		= versatile_map_io,
18262306a36Sopenharmony_ci	.init_early	= versatile_init_early,
18362306a36Sopenharmony_ci	.init_machine	= versatile_dt_init,
18462306a36Sopenharmony_ci	.dt_compat	= versatile_dt_match,
18562306a36Sopenharmony_ciMACHINE_END
186