blob: 0b2c20fddb7c5851e373b901a1330cedf43a93bd [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +02002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 * Copyright (C) 2011 Google, Inc.
5 *
6 * Author:
7 * Jay Cheng <jacheng@nvidia.com>
8 * James Wylder <james.wylder@motorola.com>
9 * Benoit Goby <benoit@android.com>
10 * Colin Cross <ccross@android.com>
11 * Hiroshi DOYU <hdoyu@nvidia.com>
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020012 */
13
Thierry Reding903b33e2013-01-21 11:08:57 +010014#include <linux/err.h>
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
Paul Walmsley049e4b32015-03-26 08:53:57 +010019#include <linux/of.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020020
21#include <soc/tegra/ahb.h>
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020022
23#define DRV_NAME "tegra-ahb"
24
Paul Walmsley049e4b32015-03-26 08:53:57 +010025#define AHB_ARBITRATION_DISABLE 0x04
26#define AHB_ARBITRATION_PRIORITY_CTRL 0x08
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020027#define AHB_PRIORITY_WEIGHT(x) (((x) & 0x7) << 29)
28#define PRIORITY_SELECT_USB BIT(6)
29#define PRIORITY_SELECT_USB2 BIT(18)
30#define PRIORITY_SELECT_USB3 BIT(17)
31
Paul Walmsley049e4b32015-03-26 08:53:57 +010032#define AHB_GIZMO_AHB_MEM 0x10
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020033#define ENB_FAST_REARBITRATE BIT(2)
34#define DONT_SPLIT_AHB_WR BIT(7)
35
Paul Walmsley049e4b32015-03-26 08:53:57 +010036#define AHB_GIZMO_APB_DMA 0x14
37#define AHB_GIZMO_IDE 0x1c
38#define AHB_GIZMO_USB 0x20
39#define AHB_GIZMO_AHB_XBAR_BRIDGE 0x24
40#define AHB_GIZMO_CPU_AHB_BRIDGE 0x28
41#define AHB_GIZMO_COP_AHB_BRIDGE 0x2c
42#define AHB_GIZMO_XBAR_APB_CTLR 0x30
43#define AHB_GIZMO_VCP_AHB_BRIDGE 0x34
44#define AHB_GIZMO_NAND 0x40
45#define AHB_GIZMO_SDMMC4 0x48
46#define AHB_GIZMO_XIO 0x4c
47#define AHB_GIZMO_BSEV 0x64
48#define AHB_GIZMO_BSEA 0x74
49#define AHB_GIZMO_NOR 0x78
50#define AHB_GIZMO_USB2 0x7c
51#define AHB_GIZMO_USB3 0x80
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020052#define IMMEDIATE BIT(18)
53
Paul Walmsley049e4b32015-03-26 08:53:57 +010054#define AHB_GIZMO_SDMMC1 0x84
55#define AHB_GIZMO_SDMMC2 0x88
56#define AHB_GIZMO_SDMMC3 0x8c
57#define AHB_MEM_PREFETCH_CFG_X 0xdc
58#define AHB_ARBITRATION_XBAR_CTRL 0xe0
59#define AHB_MEM_PREFETCH_CFG3 0xe4
60#define AHB_MEM_PREFETCH_CFG4 0xe8
61#define AHB_MEM_PREFETCH_CFG1 0xf0
62#define AHB_MEM_PREFETCH_CFG2 0xf4
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020063#define PREFETCH_ENB BIT(31)
64#define MST_ID(x) (((x) & 0x1f) << 26)
65#define AHBDMA_MST_ID MST_ID(5)
66#define USB_MST_ID MST_ID(6)
67#define USB2_MST_ID MST_ID(18)
68#define USB3_MST_ID MST_ID(17)
69#define ADDR_BNDRY(x) (((x) & 0xf) << 21)
70#define INACTIVITY_TIMEOUT(x) (((x) & 0xffff) << 0)
71
Paul Walmsley049e4b32015-03-26 08:53:57 +010072#define AHB_ARBITRATION_AHB_MEM_WRQUE_MST_ID 0xfc
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020073
Hiroshi DOYU89c788b2012-05-07 09:43:46 +030074#define AHB_ARBITRATION_XBAR_CTRL_SMMU_INIT_DONE BIT(17)
75
Paul Walmsleyce7a10b2015-03-26 08:56:35 +010076/*
77 * INCORRECT_BASE_ADDR_LOW_BYTE: Legacy kernel DT files for Tegra SoCs
78 * prior to Tegra124 generally use a physical base address ending in
79 * 0x4 for the AHB IP block. According to the TRM, the low byte
80 * should be 0x0. During device probing, this macro is used to detect
81 * whether the passed-in physical address is incorrect, and if so, to
82 * correct it.
83 */
84#define INCORRECT_BASE_ADDR_LOW_BYTE 0x4
85
Hiroshi DOYU89c788b2012-05-07 09:43:46 +030086static struct platform_driver tegra_ahb_driver;
87
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +020088static const u32 tegra_ahb_gizmo[] = {
89 AHB_ARBITRATION_DISABLE,
90 AHB_ARBITRATION_PRIORITY_CTRL,
91 AHB_GIZMO_AHB_MEM,
92 AHB_GIZMO_APB_DMA,
93 AHB_GIZMO_IDE,
94 AHB_GIZMO_USB,
95 AHB_GIZMO_AHB_XBAR_BRIDGE,
96 AHB_GIZMO_CPU_AHB_BRIDGE,
97 AHB_GIZMO_COP_AHB_BRIDGE,
98 AHB_GIZMO_XBAR_APB_CTLR,
99 AHB_GIZMO_VCP_AHB_BRIDGE,
100 AHB_GIZMO_NAND,
101 AHB_GIZMO_SDMMC4,
102 AHB_GIZMO_XIO,
103 AHB_GIZMO_BSEV,
104 AHB_GIZMO_BSEA,
105 AHB_GIZMO_NOR,
106 AHB_GIZMO_USB2,
107 AHB_GIZMO_USB3,
108 AHB_GIZMO_SDMMC1,
109 AHB_GIZMO_SDMMC2,
110 AHB_GIZMO_SDMMC3,
111 AHB_MEM_PREFETCH_CFG_X,
112 AHB_ARBITRATION_XBAR_CTRL,
113 AHB_MEM_PREFETCH_CFG3,
114 AHB_MEM_PREFETCH_CFG4,
115 AHB_MEM_PREFETCH_CFG1,
116 AHB_MEM_PREFETCH_CFG2,
117 AHB_ARBITRATION_AHB_MEM_WRQUE_MST_ID,
118};
119
120struct tegra_ahb {
121 void __iomem *regs;
122 struct device *dev;
Gustavo A. R. Silva06f3a5a2020-05-28 09:35:11 -0500123 u32 ctx[];
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200124};
125
126static inline u32 gizmo_readl(struct tegra_ahb *ahb, u32 offset)
127{
Paul Walmsleyce7a10b2015-03-26 08:56:35 +0100128 return readl(ahb->regs + offset);
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200129}
130
131static inline void gizmo_writel(struct tegra_ahb *ahb, u32 value, u32 offset)
132{
Paul Walmsleyce7a10b2015-03-26 08:56:35 +0100133 writel(value, ahb->regs + offset);
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200134}
135
Hiroshi Doyub44bf432013-02-06 20:12:35 +0100136#ifdef CONFIG_TEGRA_IOMMU_SMMU
Hiroshi DOYU89c788b2012-05-07 09:43:46 +0300137int tegra_ahb_enable_smmu(struct device_node *dn)
138{
139 struct device *dev;
140 u32 val;
141 struct tegra_ahb *ahb;
142
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +0100143 dev = driver_find_device_by_of_node(&tegra_ahb_driver.driver, dn);
Hiroshi DOYU89c788b2012-05-07 09:43:46 +0300144 if (!dev)
145 return -EPROBE_DEFER;
146 ahb = dev_get_drvdata(dev);
147 val = gizmo_readl(ahb, AHB_ARBITRATION_XBAR_CTRL);
148 val |= AHB_ARBITRATION_XBAR_CTRL_SMMU_INIT_DONE;
149 gizmo_writel(ahb, val, AHB_ARBITRATION_XBAR_CTRL);
150 return 0;
151}
152EXPORT_SYMBOL(tegra_ahb_enable_smmu);
153#endif
154
Arnd Bergmann6d616562019-04-26 16:56:03 +0200155static int __maybe_unused tegra_ahb_suspend(struct device *dev)
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200156{
157 int i;
158 struct tegra_ahb *ahb = dev_get_drvdata(dev);
159
160 for (i = 0; i < ARRAY_SIZE(tegra_ahb_gizmo); i++)
161 ahb->ctx[i] = gizmo_readl(ahb, tegra_ahb_gizmo[i]);
162 return 0;
163}
164
Arnd Bergmann6d616562019-04-26 16:56:03 +0200165static int __maybe_unused tegra_ahb_resume(struct device *dev)
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200166{
167 int i;
168 struct tegra_ahb *ahb = dev_get_drvdata(dev);
169
170 for (i = 0; i < ARRAY_SIZE(tegra_ahb_gizmo); i++)
171 gizmo_writel(ahb, ahb->ctx[i], tegra_ahb_gizmo[i]);
172 return 0;
173}
174
175static UNIVERSAL_DEV_PM_OPS(tegra_ahb_pm,
176 tegra_ahb_suspend,
177 tegra_ahb_resume, NULL);
178
179static void tegra_ahb_gizmo_init(struct tegra_ahb *ahb)
180{
181 u32 val;
182
183 val = gizmo_readl(ahb, AHB_GIZMO_AHB_MEM);
184 val |= ENB_FAST_REARBITRATE | IMMEDIATE | DONT_SPLIT_AHB_WR;
185 gizmo_writel(ahb, val, AHB_GIZMO_AHB_MEM);
186
187 val = gizmo_readl(ahb, AHB_GIZMO_USB);
188 val |= IMMEDIATE;
189 gizmo_writel(ahb, val, AHB_GIZMO_USB);
190
191 val = gizmo_readl(ahb, AHB_GIZMO_USB2);
192 val |= IMMEDIATE;
193 gizmo_writel(ahb, val, AHB_GIZMO_USB2);
194
195 val = gizmo_readl(ahb, AHB_GIZMO_USB3);
196 val |= IMMEDIATE;
197 gizmo_writel(ahb, val, AHB_GIZMO_USB3);
198
199 val = gizmo_readl(ahb, AHB_ARBITRATION_PRIORITY_CTRL);
200 val |= PRIORITY_SELECT_USB |
201 PRIORITY_SELECT_USB2 |
202 PRIORITY_SELECT_USB3 |
203 AHB_PRIORITY_WEIGHT(7);
204 gizmo_writel(ahb, val, AHB_ARBITRATION_PRIORITY_CTRL);
205
206 val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG1);
207 val &= ~MST_ID(~0);
208 val |= PREFETCH_ENB |
209 AHBDMA_MST_ID |
210 ADDR_BNDRY(0xc) |
211 INACTIVITY_TIMEOUT(0x1000);
212 gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG1);
213
214 val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG2);
215 val &= ~MST_ID(~0);
216 val |= PREFETCH_ENB |
217 USB_MST_ID |
218 ADDR_BNDRY(0xc) |
219 INACTIVITY_TIMEOUT(0x1000);
220 gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG2);
221
222 val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG3);
223 val &= ~MST_ID(~0);
224 val |= PREFETCH_ENB |
225 USB3_MST_ID |
226 ADDR_BNDRY(0xc) |
227 INACTIVITY_TIMEOUT(0x1000);
228 gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG3);
229
230 val = gizmo_readl(ahb, AHB_MEM_PREFETCH_CFG4);
231 val &= ~MST_ID(~0);
232 val |= PREFETCH_ENB |
233 USB2_MST_ID |
234 ADDR_BNDRY(0xc) |
235 INACTIVITY_TIMEOUT(0x1000);
236 gizmo_writel(ahb, val, AHB_MEM_PREFETCH_CFG4);
237}
238
Greg Kroah-Hartmanc571b212012-12-21 13:16:42 -0800239static int tegra_ahb_probe(struct platform_device *pdev)
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200240{
241 struct resource *res;
242 struct tegra_ahb *ahb;
243 size_t bytes;
244
245 bytes = sizeof(*ahb) + sizeof(u32) * ARRAY_SIZE(tegra_ahb_gizmo);
246 ahb = devm_kzalloc(&pdev->dev, bytes, GFP_KERNEL);
247 if (!ahb)
248 return -ENOMEM;
249
250 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Paul Walmsleyce7a10b2015-03-26 08:56:35 +0100251
252 /* Correct the IP block base address if necessary */
253 if (res &&
254 (res->start & INCORRECT_BASE_ADDR_LOW_BYTE) ==
255 INCORRECT_BASE_ADDR_LOW_BYTE) {
256 dev_warn(&pdev->dev, "incorrect AHB base address in DT data - enabling workaround\n");
257 res->start -= INCORRECT_BASE_ADDR_LOW_BYTE;
258 }
259
Thierry Reding903b33e2013-01-21 11:08:57 +0100260 ahb->regs = devm_ioremap_resource(&pdev->dev, res);
261 if (IS_ERR(ahb->regs))
262 return PTR_ERR(ahb->regs);
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200263
264 ahb->dev = &pdev->dev;
265 platform_set_drvdata(pdev, ahb);
266 tegra_ahb_gizmo_init(ahb);
267 return 0;
268}
269
Greg Kroah-Hartmanc571b212012-12-21 13:16:42 -0800270static const struct of_device_id tegra_ahb_of_match[] = {
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200271 { .compatible = "nvidia,tegra30-ahb", },
272 { .compatible = "nvidia,tegra20-ahb", },
273 {},
274};
275
276static struct platform_driver tegra_ahb_driver = {
277 .probe = tegra_ahb_probe,
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200278 .driver = {
279 .name = DRV_NAME,
Hiroshi DOYU87d0bab22012-05-07 12:24:48 +0200280 .of_match_table = tegra_ahb_of_match,
281 .pm = &tegra_ahb_pm,
282 },
283};
284module_platform_driver(tegra_ahb_driver);
285
286MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
287MODULE_DESCRIPTION("Tegra AHB driver");
288MODULE_LICENSE("GPL v2");
289MODULE_ALIAS("platform:" DRV_NAME);