xref: /OK3568_Linux_fs/kernel/drivers/soc/tegra/flowctrl.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * drivers/soc/tegra/flowctrl.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Functions and macros to control the flowcontroller
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2010-2012, NVIDIA Corporation. All rights reserved.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/cpumask.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/io.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/of.h>
15*4882a593Smuzhiyun #include <linux/of_address.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <soc/tegra/common.h>
19*4882a593Smuzhiyun #include <soc/tegra/flowctrl.h>
20*4882a593Smuzhiyun #include <soc/tegra/fuse.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static u8 flowctrl_offset_halt_cpu[] = {
23*4882a593Smuzhiyun 	FLOW_CTRL_HALT_CPU0_EVENTS,
24*4882a593Smuzhiyun 	FLOW_CTRL_HALT_CPU1_EVENTS,
25*4882a593Smuzhiyun 	FLOW_CTRL_HALT_CPU1_EVENTS + 8,
26*4882a593Smuzhiyun 	FLOW_CTRL_HALT_CPU1_EVENTS + 16,
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static u8 flowctrl_offset_cpu_csr[] = {
30*4882a593Smuzhiyun 	FLOW_CTRL_CPU0_CSR,
31*4882a593Smuzhiyun 	FLOW_CTRL_CPU1_CSR,
32*4882a593Smuzhiyun 	FLOW_CTRL_CPU1_CSR + 8,
33*4882a593Smuzhiyun 	FLOW_CTRL_CPU1_CSR + 16,
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static void __iomem *tegra_flowctrl_base;
37*4882a593Smuzhiyun 
flowctrl_update(u8 offset,u32 value)38*4882a593Smuzhiyun static void flowctrl_update(u8 offset, u32 value)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	if (WARN_ONCE(IS_ERR_OR_NULL(tegra_flowctrl_base),
41*4882a593Smuzhiyun 		      "Tegra flowctrl not initialised!\n"))
42*4882a593Smuzhiyun 		return;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	writel(value, tegra_flowctrl_base + offset);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	/* ensure the update has reached the flow controller */
47*4882a593Smuzhiyun 	wmb();
48*4882a593Smuzhiyun 	readl_relaxed(tegra_flowctrl_base + offset);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
flowctrl_read_cpu_csr(unsigned int cpuid)51*4882a593Smuzhiyun u32 flowctrl_read_cpu_csr(unsigned int cpuid)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	u8 offset = flowctrl_offset_cpu_csr[cpuid];
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (WARN_ONCE(IS_ERR_OR_NULL(tegra_flowctrl_base),
56*4882a593Smuzhiyun 		      "Tegra flowctrl not initialised!\n"))
57*4882a593Smuzhiyun 		return 0;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	return readl(tegra_flowctrl_base + offset);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
flowctrl_write_cpu_csr(unsigned int cpuid,u32 value)62*4882a593Smuzhiyun void flowctrl_write_cpu_csr(unsigned int cpuid, u32 value)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	return flowctrl_update(flowctrl_offset_cpu_csr[cpuid], value);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
flowctrl_write_cpu_halt(unsigned int cpuid,u32 value)67*4882a593Smuzhiyun void flowctrl_write_cpu_halt(unsigned int cpuid, u32 value)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	return flowctrl_update(flowctrl_offset_halt_cpu[cpuid], value);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
flowctrl_cpu_suspend_enter(unsigned int cpuid)72*4882a593Smuzhiyun void flowctrl_cpu_suspend_enter(unsigned int cpuid)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	unsigned int reg;
75*4882a593Smuzhiyun 	int i;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	reg = flowctrl_read_cpu_csr(cpuid);
78*4882a593Smuzhiyun 	switch (tegra_get_chip_id()) {
79*4882a593Smuzhiyun 	case TEGRA20:
80*4882a593Smuzhiyun 		/* clear wfe bitmap */
81*4882a593Smuzhiyun 		reg &= ~TEGRA20_FLOW_CTRL_CSR_WFE_BITMAP;
82*4882a593Smuzhiyun 		/* clear wfi bitmap */
83*4882a593Smuzhiyun 		reg &= ~TEGRA20_FLOW_CTRL_CSR_WFI_BITMAP;
84*4882a593Smuzhiyun 		/* pwr gating on wfe */
85*4882a593Smuzhiyun 		reg |= TEGRA20_FLOW_CTRL_CSR_WFE_CPU0 << cpuid;
86*4882a593Smuzhiyun 		break;
87*4882a593Smuzhiyun 	case TEGRA30:
88*4882a593Smuzhiyun 	case TEGRA114:
89*4882a593Smuzhiyun 	case TEGRA124:
90*4882a593Smuzhiyun 		/* clear wfe bitmap */
91*4882a593Smuzhiyun 		reg &= ~TEGRA30_FLOW_CTRL_CSR_WFE_BITMAP;
92*4882a593Smuzhiyun 		/* clear wfi bitmap */
93*4882a593Smuzhiyun 		reg &= ~TEGRA30_FLOW_CTRL_CSR_WFI_BITMAP;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 		if (tegra_get_chip_id() == TEGRA30) {
96*4882a593Smuzhiyun 			/*
97*4882a593Smuzhiyun 			 * The wfi doesn't work well on Tegra30 because
98*4882a593Smuzhiyun 			 * CPU hangs under some odd circumstances after
99*4882a593Smuzhiyun 			 * power-gating (like memory running off PLLP),
100*4882a593Smuzhiyun 			 * hence use wfe that is working perfectly fine.
101*4882a593Smuzhiyun 			 * Note that Tegra30 TRM doc clearly stands that
102*4882a593Smuzhiyun 			 * wfi should be used for the "Cluster Switching",
103*4882a593Smuzhiyun 			 * while wfe for the power-gating, just like it
104*4882a593Smuzhiyun 			 * is done on Tegra20.
105*4882a593Smuzhiyun 			 */
106*4882a593Smuzhiyun 			reg |= TEGRA20_FLOW_CTRL_CSR_WFE_CPU0 << cpuid;
107*4882a593Smuzhiyun 		} else {
108*4882a593Smuzhiyun 			/* pwr gating on wfi */
109*4882a593Smuzhiyun 			reg |= TEGRA30_FLOW_CTRL_CSR_WFI_CPU0 << cpuid;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 		break;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 	reg |= FLOW_CTRL_CSR_INTR_FLAG;			/* clear intr flag */
114*4882a593Smuzhiyun 	reg |= FLOW_CTRL_CSR_EVENT_FLAG;		/* clear event flag */
115*4882a593Smuzhiyun 	reg |= FLOW_CTRL_CSR_ENABLE;			/* pwr gating */
116*4882a593Smuzhiyun 	flowctrl_write_cpu_csr(cpuid, reg);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	for (i = 0; i < num_possible_cpus(); i++) {
119*4882a593Smuzhiyun 		if (i == cpuid)
120*4882a593Smuzhiyun 			continue;
121*4882a593Smuzhiyun 		reg = flowctrl_read_cpu_csr(i);
122*4882a593Smuzhiyun 		reg |= FLOW_CTRL_CSR_EVENT_FLAG;
123*4882a593Smuzhiyun 		reg |= FLOW_CTRL_CSR_INTR_FLAG;
124*4882a593Smuzhiyun 		flowctrl_write_cpu_csr(i, reg);
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
flowctrl_cpu_suspend_exit(unsigned int cpuid)128*4882a593Smuzhiyun void flowctrl_cpu_suspend_exit(unsigned int cpuid)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	unsigned int reg;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* Disable powergating via flow controller for CPU0 */
133*4882a593Smuzhiyun 	reg = flowctrl_read_cpu_csr(cpuid);
134*4882a593Smuzhiyun 	switch (tegra_get_chip_id()) {
135*4882a593Smuzhiyun 	case TEGRA20:
136*4882a593Smuzhiyun 		/* clear wfe bitmap */
137*4882a593Smuzhiyun 		reg &= ~TEGRA20_FLOW_CTRL_CSR_WFE_BITMAP;
138*4882a593Smuzhiyun 		/* clear wfi bitmap */
139*4882a593Smuzhiyun 		reg &= ~TEGRA20_FLOW_CTRL_CSR_WFI_BITMAP;
140*4882a593Smuzhiyun 		break;
141*4882a593Smuzhiyun 	case TEGRA30:
142*4882a593Smuzhiyun 	case TEGRA114:
143*4882a593Smuzhiyun 	case TEGRA124:
144*4882a593Smuzhiyun 		/* clear wfe bitmap */
145*4882a593Smuzhiyun 		reg &= ~TEGRA30_FLOW_CTRL_CSR_WFE_BITMAP;
146*4882a593Smuzhiyun 		/* clear wfi bitmap */
147*4882a593Smuzhiyun 		reg &= ~TEGRA30_FLOW_CTRL_CSR_WFI_BITMAP;
148*4882a593Smuzhiyun 		break;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 	reg &= ~FLOW_CTRL_CSR_ENABLE;			/* clear enable */
151*4882a593Smuzhiyun 	reg |= FLOW_CTRL_CSR_INTR_FLAG;			/* clear intr */
152*4882a593Smuzhiyun 	reg |= FLOW_CTRL_CSR_EVENT_FLAG;		/* clear event */
153*4882a593Smuzhiyun 	flowctrl_write_cpu_csr(cpuid, reg);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
tegra_flowctrl_probe(struct platform_device * pdev)156*4882a593Smuzhiyun static int tegra_flowctrl_probe(struct platform_device *pdev)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	void __iomem *base = tegra_flowctrl_base;
159*4882a593Smuzhiyun 	struct resource *res;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
162*4882a593Smuzhiyun 	tegra_flowctrl_base = devm_ioremap_resource(&pdev->dev, res);
163*4882a593Smuzhiyun 	if (IS_ERR(tegra_flowctrl_base))
164*4882a593Smuzhiyun 		return PTR_ERR(tegra_flowctrl_base);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	iounmap(base);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	return 0;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun static const struct of_device_id tegra_flowctrl_match[] = {
172*4882a593Smuzhiyun 	{ .compatible = "nvidia,tegra210-flowctrl" },
173*4882a593Smuzhiyun 	{ .compatible = "nvidia,tegra124-flowctrl" },
174*4882a593Smuzhiyun 	{ .compatible = "nvidia,tegra114-flowctrl" },
175*4882a593Smuzhiyun 	{ .compatible = "nvidia,tegra30-flowctrl" },
176*4882a593Smuzhiyun 	{ .compatible = "nvidia,tegra20-flowctrl" },
177*4882a593Smuzhiyun 	{ }
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static struct platform_driver tegra_flowctrl_driver = {
181*4882a593Smuzhiyun 	.driver = {
182*4882a593Smuzhiyun 		.name = "tegra-flowctrl",
183*4882a593Smuzhiyun 		.suppress_bind_attrs = true,
184*4882a593Smuzhiyun 		.of_match_table = tegra_flowctrl_match,
185*4882a593Smuzhiyun 	},
186*4882a593Smuzhiyun 	.probe = tegra_flowctrl_probe,
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun builtin_platform_driver(tegra_flowctrl_driver);
189*4882a593Smuzhiyun 
tegra_flowctrl_init(void)190*4882a593Smuzhiyun static int __init tegra_flowctrl_init(void)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct resource res;
193*4882a593Smuzhiyun 	struct device_node *np;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (!soc_is_tegra())
196*4882a593Smuzhiyun 		return 0;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	np = of_find_matching_node(NULL, tegra_flowctrl_match);
199*4882a593Smuzhiyun 	if (np) {
200*4882a593Smuzhiyun 		if (of_address_to_resource(np, 0, &res) < 0) {
201*4882a593Smuzhiyun 			pr_err("failed to get flowctrl register\n");
202*4882a593Smuzhiyun 			return -ENXIO;
203*4882a593Smuzhiyun 		}
204*4882a593Smuzhiyun 		of_node_put(np);
205*4882a593Smuzhiyun 	} else if (IS_ENABLED(CONFIG_ARM)) {
206*4882a593Smuzhiyun 		/*
207*4882a593Smuzhiyun 		 * Hardcoded fallback for 32-bit Tegra
208*4882a593Smuzhiyun 		 * devices if device tree node is missing.
209*4882a593Smuzhiyun 		 */
210*4882a593Smuzhiyun 		res.start = 0x60007000;
211*4882a593Smuzhiyun 		res.end = 0x60007fff;
212*4882a593Smuzhiyun 		res.flags = IORESOURCE_MEM;
213*4882a593Smuzhiyun 	} else {
214*4882a593Smuzhiyun 		/*
215*4882a593Smuzhiyun 		 * At this point we're running on a Tegra,
216*4882a593Smuzhiyun 		 * that doesn't support the flow controller
217*4882a593Smuzhiyun 		 * (eg. Tegra186), so just return.
218*4882a593Smuzhiyun 		 */
219*4882a593Smuzhiyun 		return 0;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	tegra_flowctrl_base = ioremap(res.start, resource_size(&res));
223*4882a593Smuzhiyun 	if (!tegra_flowctrl_base)
224*4882a593Smuzhiyun 		return -ENXIO;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun early_initcall(tegra_flowctrl_init);
229