162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (C) 2011 Google, Inc.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Author:
762306a36Sopenharmony_ci *	Jay Cheng <jacheng@nvidia.com>
862306a36Sopenharmony_ci *	James Wylder <james.wylder@motorola.com>
962306a36Sopenharmony_ci *	Benoit Goby <benoit@android.com>
1062306a36Sopenharmony_ci *	Colin Cross <ccross@android.com>
1162306a36Sopenharmony_ci *	Hiroshi DOYU <hdoyu@nvidia.com>
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/err.h>
1562306a36Sopenharmony_ci#include <linux/kernel.h>
1662306a36Sopenharmony_ci#include <linux/module.h>
1762306a36Sopenharmony_ci#include <linux/platform_device.h>
1862306a36Sopenharmony_ci#include <linux/io.h>
1962306a36Sopenharmony_ci#include <linux/of.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#include <soc/tegra/ahb.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define DRV_NAME "tegra-ahb"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define AHB_ARBITRATION_DISABLE		0x04
2662306a36Sopenharmony_ci#define AHB_ARBITRATION_PRIORITY_CTRL	0x08
2762306a36Sopenharmony_ci#define   AHB_PRIORITY_WEIGHT(x)	(((x) & 0x7) << 29)
2862306a36Sopenharmony_ci#define   PRIORITY_SELECT_USB BIT(6)
2962306a36Sopenharmony_ci#define   PRIORITY_SELECT_USB2 BIT(18)
3062306a36Sopenharmony_ci#define   PRIORITY_SELECT_USB3 BIT(17)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define AHB_GIZMO_AHB_MEM		0x10
3362306a36Sopenharmony_ci#define   ENB_FAST_REARBITRATE BIT(2)
3462306a36Sopenharmony_ci#define   DONT_SPLIT_AHB_WR     BIT(7)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define AHB_GIZMO_APB_DMA		0x14
3762306a36Sopenharmony_ci#define AHB_GIZMO_IDE			0x1c
3862306a36Sopenharmony_ci#define AHB_GIZMO_USB			0x20
3962306a36Sopenharmony_ci#define AHB_GIZMO_AHB_XBAR_BRIDGE	0x24
4062306a36Sopenharmony_ci#define AHB_GIZMO_CPU_AHB_BRIDGE	0x28
4162306a36Sopenharmony_ci#define AHB_GIZMO_COP_AHB_BRIDGE	0x2c
4262306a36Sopenharmony_ci#define AHB_GIZMO_XBAR_APB_CTLR		0x30
4362306a36Sopenharmony_ci#define AHB_GIZMO_VCP_AHB_BRIDGE	0x34
4462306a36Sopenharmony_ci#define AHB_GIZMO_NAND			0x40
4562306a36Sopenharmony_ci#define AHB_GIZMO_SDMMC4		0x48
4662306a36Sopenharmony_ci#define AHB_GIZMO_XIO			0x4c
4762306a36Sopenharmony_ci#define AHB_GIZMO_BSEV			0x64
4862306a36Sopenharmony_ci#define AHB_GIZMO_BSEA			0x74
4962306a36Sopenharmony_ci#define AHB_GIZMO_NOR			0x78
5062306a36Sopenharmony_ci#define AHB_GIZMO_USB2			0x7c
5162306a36Sopenharmony_ci#define AHB_GIZMO_USB3			0x80
5262306a36Sopenharmony_ci#define   IMMEDIATE	BIT(18)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define AHB_GIZMO_SDMMC1		0x84
5562306a36Sopenharmony_ci#define AHB_GIZMO_SDMMC2		0x88
5662306a36Sopenharmony_ci#define AHB_GIZMO_SDMMC3		0x8c
5762306a36Sopenharmony_ci#define AHB_MEM_PREFETCH_CFG_X		0xdc
5862306a36Sopenharmony_ci#define AHB_ARBITRATION_XBAR_CTRL	0xe0
5962306a36Sopenharmony_ci#define AHB_MEM_PREFETCH_CFG3		0xe4
6062306a36Sopenharmony_ci#define AHB_MEM_PREFETCH_CFG4		0xe8
6162306a36Sopenharmony_ci#define AHB_MEM_PREFETCH_CFG1		0xf0
6262306a36Sopenharmony_ci#define AHB_MEM_PREFETCH_CFG2		0xf4
6362306a36Sopenharmony_ci#define   PREFETCH_ENB	BIT(31)
6462306a36Sopenharmony_ci#define   MST_ID(x)	(((x) & 0x1f) << 26)
6562306a36Sopenharmony_ci#define   AHBDMA_MST_ID	MST_ID(5)
6662306a36Sopenharmony_ci#define   USB_MST_ID	MST_ID(6)
6762306a36Sopenharmony_ci#define   USB2_MST_ID	MST_ID(18)
6862306a36Sopenharmony_ci#define   USB3_MST_ID	MST_ID(17)
6962306a36Sopenharmony_ci#define   ADDR_BNDRY(x)	(((x) & 0xf) << 21)
7062306a36Sopenharmony_ci#define   INACTIVITY_TIMEOUT(x)	(((x) & 0xffff) << 0)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define AHB_ARBITRATION_AHB_MEM_WRQUE_MST_ID	0xfc
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define AHB_ARBITRATION_XBAR_CTRL_SMMU_INIT_DONE BIT(17)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/*
7762306a36Sopenharmony_ci * INCORRECT_BASE_ADDR_LOW_BYTE: Legacy kernel DT files for Tegra SoCs
7862306a36Sopenharmony_ci * prior to Tegra124 generally use a physical base address ending in
7962306a36Sopenharmony_ci * 0x4 for the AHB IP block.  According to the TRM, the low byte
8062306a36Sopenharmony_ci * should be 0x0.  During device probing, this macro is used to detect
8162306a36Sopenharmony_ci * whether the passed-in physical address is incorrect, and if so, to
8262306a36Sopenharmony_ci * correct it.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci#define INCORRECT_BASE_ADDR_LOW_BYTE		0x4
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistatic struct platform_driver tegra_ahb_driver;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic const u32 tegra_ahb_gizmo[] = {
8962306a36Sopenharmony_ci	AHB_ARBITRATION_DISABLE,
9062306a36Sopenharmony_ci	AHB_ARBITRATION_PRIORITY_CTRL,
9162306a36Sopenharmony_ci	AHB_GIZMO_AHB_MEM,
9262306a36Sopenharmony_ci	AHB_GIZMO_APB_DMA,
9362306a36Sopenharmony_ci	AHB_GIZMO_IDE,
9462306a36Sopenharmony_ci	AHB_GIZMO_USB,
9562306a36Sopenharmony_ci	AHB_GIZMO_AHB_XBAR_BRIDGE,
9662306a36Sopenharmony_ci	AHB_GIZMO_CPU_AHB_BRIDGE,
9762306a36Sopenharmony_ci	AHB_GIZMO_COP_AHB_BRIDGE,
9862306a36Sopenharmony_ci	AHB_GIZMO_XBAR_APB_CTLR,
9962306a36Sopenharmony_ci	AHB_GIZMO_VCP_AHB_BRIDGE,
10062306a36Sopenharmony_ci	AHB_GIZMO_NAND,
10162306a36Sopenharmony_ci	AHB_GIZMO_SDMMC4,
10262306a36Sopenharmony_ci	AHB_GIZMO_XIO,
10362306a36Sopenharmony_ci	AHB_GIZMO_BSEV,
10462306a36Sopenharmony_ci	AHB_GIZMO_BSEA,
10562306a36Sopenharmony_ci	AHB_GIZMO_NOR,
10662306a36Sopenharmony_ci	AHB_GIZMO_USB2,
10762306a36Sopenharmony_ci	AHB_GIZMO_USB3,
10862306a36Sopenharmony_ci	AHB_GIZMO_SDMMC1,
10962306a36Sopenharmony_ci	AHB_GIZMO_SDMMC2,
11062306a36Sopenharmony_ci	AHB_GIZMO_SDMMC3,
11162306a36Sopenharmony_ci	AHB_MEM_PREFETCH_CFG_X,
11262306a36Sopenharmony_ci	AHB_ARBITRATION_XBAR_CTRL,
11362306a36Sopenharmony_ci	AHB_MEM_PREFETCH_CFG3,
11462306a36Sopenharmony_ci	AHB_MEM_PREFETCH_CFG4,
11562306a36Sopenharmony_ci	AHB_MEM_PREFETCH_CFG1,
11662306a36Sopenharmony_ci	AHB_MEM_PREFETCH_CFG2,
11762306a36Sopenharmony_ci	AHB_ARBITRATION_AHB_MEM_WRQUE_MST_ID,
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistruct tegra_ahb {
12162306a36Sopenharmony_ci	void __iomem	*regs;
12262306a36Sopenharmony_ci	struct device	*dev;
12362306a36Sopenharmony_ci	u32		ctx[];
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic inline u32 gizmo_readl(struct tegra_ahb *ahb, u32 offset)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	return readl(ahb->regs + offset);
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic inline void gizmo_writel(struct tegra_ahb *ahb, u32 value, u32 offset)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	writel(value, ahb->regs + offset);
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#ifdef CONFIG_TEGRA_IOMMU_SMMU
13762306a36Sopenharmony_ciint tegra_ahb_enable_smmu(struct device_node *dn)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	struct device *dev;
14062306a36Sopenharmony_ci	u32 val;
14162306a36Sopenharmony_ci	struct tegra_ahb *ahb;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	dev = driver_find_device_by_of_node(&tegra_ahb_driver.driver, dn);
14462306a36Sopenharmony_ci	if (!dev)
14562306a36Sopenharmony_ci		return -EPROBE_DEFER;
14662306a36Sopenharmony_ci	ahb = dev_get_drvdata(dev);
14762306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_ARBITRATION_XBAR_CTRL);
14862306a36Sopenharmony_ci	val |= AHB_ARBITRATION_XBAR_CTRL_SMMU_INIT_DONE;
14962306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_ARBITRATION_XBAR_CTRL);
15062306a36Sopenharmony_ci	return 0;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ciEXPORT_SYMBOL(tegra_ahb_enable_smmu);
15362306a36Sopenharmony_ci#endif
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic int __maybe_unused tegra_ahb_suspend(struct device *dev)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	int i;
15862306a36Sopenharmony_ci	struct tegra_ahb *ahb = dev_get_drvdata(dev);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tegra_ahb_gizmo); i++)
16162306a36Sopenharmony_ci		ahb->ctx[i] = gizmo_readl(ahb, tegra_ahb_gizmo[i]);
16262306a36Sopenharmony_ci	return 0;
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic int __maybe_unused tegra_ahb_resume(struct device *dev)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	int i;
16862306a36Sopenharmony_ci	struct tegra_ahb *ahb = dev_get_drvdata(dev);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tegra_ahb_gizmo); i++)
17162306a36Sopenharmony_ci		gizmo_writel(ahb, ahb->ctx[i], tegra_ahb_gizmo[i]);
17262306a36Sopenharmony_ci	return 0;
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic UNIVERSAL_DEV_PM_OPS(tegra_ahb_pm,
17662306a36Sopenharmony_ci			    tegra_ahb_suspend,
17762306a36Sopenharmony_ci			    tegra_ahb_resume, NULL);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic void tegra_ahb_gizmo_init(struct tegra_ahb *ahb)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	u32 val;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_GIZMO_AHB_MEM);
18462306a36Sopenharmony_ci	val |= ENB_FAST_REARBITRATE | IMMEDIATE | DONT_SPLIT_AHB_WR;
18562306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_GIZMO_AHB_MEM);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_GIZMO_USB);
18862306a36Sopenharmony_ci	val |= IMMEDIATE;
18962306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_GIZMO_USB);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_GIZMO_USB2);
19262306a36Sopenharmony_ci	val |= IMMEDIATE;
19362306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_GIZMO_USB2);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_GIZMO_USB3);
19662306a36Sopenharmony_ci	val |= IMMEDIATE;
19762306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_GIZMO_USB3);
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_ARBITRATION_PRIORITY_CTRL);
20062306a36Sopenharmony_ci	val |= PRIORITY_SELECT_USB |
20162306a36Sopenharmony_ci		PRIORITY_SELECT_USB2 |
20262306a36Sopenharmony_ci		PRIORITY_SELECT_USB3 |
20362306a36Sopenharmony_ci		AHB_PRIORITY_WEIGHT(7);
20462306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_ARBITRATION_PRIORITY_CTRL);
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG1);
20762306a36Sopenharmony_ci	val &= ~MST_ID(~0);
20862306a36Sopenharmony_ci	val |= PREFETCH_ENB |
20962306a36Sopenharmony_ci		AHBDMA_MST_ID |
21062306a36Sopenharmony_ci		ADDR_BNDRY(0xc) |
21162306a36Sopenharmony_ci		INACTIVITY_TIMEOUT(0x1000);
21262306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG1);
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG2);
21562306a36Sopenharmony_ci	val &= ~MST_ID(~0);
21662306a36Sopenharmony_ci	val |= PREFETCH_ENB |
21762306a36Sopenharmony_ci		USB_MST_ID |
21862306a36Sopenharmony_ci		ADDR_BNDRY(0xc) |
21962306a36Sopenharmony_ci		INACTIVITY_TIMEOUT(0x1000);
22062306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG2);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG3);
22362306a36Sopenharmony_ci	val &= ~MST_ID(~0);
22462306a36Sopenharmony_ci	val |= PREFETCH_ENB |
22562306a36Sopenharmony_ci		USB3_MST_ID |
22662306a36Sopenharmony_ci		ADDR_BNDRY(0xc) |
22762306a36Sopenharmony_ci		INACTIVITY_TIMEOUT(0x1000);
22862306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG3);
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG4);
23162306a36Sopenharmony_ci	val &= ~MST_ID(~0);
23262306a36Sopenharmony_ci	val |= PREFETCH_ENB |
23362306a36Sopenharmony_ci		USB2_MST_ID |
23462306a36Sopenharmony_ci		ADDR_BNDRY(0xc) |
23562306a36Sopenharmony_ci		INACTIVITY_TIMEOUT(0x1000);
23662306a36Sopenharmony_ci	gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG4);
23762306a36Sopenharmony_ci}
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cistatic int tegra_ahb_probe(struct platform_device *pdev)
24062306a36Sopenharmony_ci{
24162306a36Sopenharmony_ci	struct resource *res;
24262306a36Sopenharmony_ci	struct tegra_ahb *ahb;
24362306a36Sopenharmony_ci	size_t bytes;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	bytes = sizeof(*ahb) + sizeof(u32) * ARRAY_SIZE(tegra_ahb_gizmo);
24662306a36Sopenharmony_ci	ahb = devm_kzalloc(&pdev->dev, bytes, GFP_KERNEL);
24762306a36Sopenharmony_ci	if (!ahb)
24862306a36Sopenharmony_ci		return -ENOMEM;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	/* Correct the IP block base address if necessary */
25362306a36Sopenharmony_ci	if (res &&
25462306a36Sopenharmony_ci	    (res->start & INCORRECT_BASE_ADDR_LOW_BYTE) ==
25562306a36Sopenharmony_ci	    INCORRECT_BASE_ADDR_LOW_BYTE) {
25662306a36Sopenharmony_ci		dev_warn(&pdev->dev, "incorrect AHB base address in DT data - enabling workaround\n");
25762306a36Sopenharmony_ci		res->start -= INCORRECT_BASE_ADDR_LOW_BYTE;
25862306a36Sopenharmony_ci	}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	ahb->regs = devm_ioremap_resource(&pdev->dev, res);
26162306a36Sopenharmony_ci	if (IS_ERR(ahb->regs))
26262306a36Sopenharmony_ci		return PTR_ERR(ahb->regs);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	ahb->dev = &pdev->dev;
26562306a36Sopenharmony_ci	platform_set_drvdata(pdev, ahb);
26662306a36Sopenharmony_ci	tegra_ahb_gizmo_init(ahb);
26762306a36Sopenharmony_ci	return 0;
26862306a36Sopenharmony_ci}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_cistatic const struct of_device_id tegra_ahb_of_match[] = {
27162306a36Sopenharmony_ci	{ .compatible = "nvidia,tegra30-ahb", },
27262306a36Sopenharmony_ci	{ .compatible = "nvidia,tegra20-ahb", },
27362306a36Sopenharmony_ci	{},
27462306a36Sopenharmony_ci};
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_cistatic struct platform_driver tegra_ahb_driver = {
27762306a36Sopenharmony_ci	.probe = tegra_ahb_probe,
27862306a36Sopenharmony_ci	.driver = {
27962306a36Sopenharmony_ci		.name = DRV_NAME,
28062306a36Sopenharmony_ci		.of_match_table = tegra_ahb_of_match,
28162306a36Sopenharmony_ci		.pm = &tegra_ahb_pm,
28262306a36Sopenharmony_ci	},
28362306a36Sopenharmony_ci};
28462306a36Sopenharmony_cimodule_platform_driver(tegra_ahb_driver);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ciMODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
28762306a36Sopenharmony_ciMODULE_DESCRIPTION("Tegra AHB driver");
28862306a36Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME);
289