xref: /linux/drivers/remoteproc/wkup_m3_rproc.c (revision 4fd18fc38757217c746aa063ba9e4729814dc737)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI AMx3 Wakeup M3 Remote Processor driver
4  *
5  * Copyright (C) 2014-2015 Texas Instruments, Inc.
6  *
7  * Dave Gerlach <d-gerlach@ti.com>
8  * Suman Anna <s-anna@ti.com>
9  */
10 
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/remoteproc.h>
20 #include <linux/reset.h>
21 
22 #include <linux/platform_data/wkup_m3.h>
23 
24 #include "remoteproc_internal.h"
25 
26 #define WKUPM3_MEM_MAX	2
27 
28 /**
29  * struct wkup_m3_mem - WkupM3 internal memory structure
30  * @cpu_addr: MPU virtual address of the memory region
31  * @bus_addr: Bus address used to access the memory region
32  * @dev_addr: Device address from Wakeup M3 view
33  * @size: Size of the memory region
34  */
35 struct wkup_m3_mem {
36 	void __iomem *cpu_addr;
37 	phys_addr_t bus_addr;
38 	u32 dev_addr;
39 	size_t size;
40 };
41 
42 /**
43  * struct wkup_m3_rproc - WkupM3 remote processor state
44  * @rproc: rproc handle
45  * @pdev: pointer to platform device
46  * @mem: WkupM3 memory information
47  * @rsts: reset control
48  */
49 struct wkup_m3_rproc {
50 	struct rproc *rproc;
51 	struct platform_device *pdev;
52 	struct wkup_m3_mem mem[WKUPM3_MEM_MAX];
53 	struct reset_control *rsts;
54 };
55 
56 static int wkup_m3_rproc_start(struct rproc *rproc)
57 {
58 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
59 	struct platform_device *pdev = wkupm3->pdev;
60 	struct device *dev = &pdev->dev;
61 	struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
62 	int error = 0;
63 
64 	error = reset_control_deassert(wkupm3->rsts);
65 
66 	if (!wkupm3->rsts && pdata->deassert_reset(pdev, pdata->reset_name)) {
67 		dev_err(dev, "Unable to reset wkup_m3!\n");
68 		error = -ENODEV;
69 	}
70 
71 	return error;
72 }
73 
74 static int wkup_m3_rproc_stop(struct rproc *rproc)
75 {
76 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
77 	struct platform_device *pdev = wkupm3->pdev;
78 	struct device *dev = &pdev->dev;
79 	struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
80 	int error = 0;
81 
82 	error = reset_control_assert(wkupm3->rsts);
83 
84 	if (!wkupm3->rsts && pdata->assert_reset(pdev, pdata->reset_name)) {
85 		dev_err(dev, "Unable to assert reset of wkup_m3!\n");
86 		error = -ENODEV;
87 	}
88 
89 	return error;
90 }
91 
92 static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
93 {
94 	struct wkup_m3_rproc *wkupm3 = rproc->priv;
95 	void *va = NULL;
96 	int i;
97 	u32 offset;
98 
99 	if (len == 0)
100 		return NULL;
101 
102 	for (i = 0; i < WKUPM3_MEM_MAX; i++) {
103 		if (da >= wkupm3->mem[i].dev_addr && da + len <=
104 		    wkupm3->mem[i].dev_addr +  wkupm3->mem[i].size) {
105 			offset = da -  wkupm3->mem[i].dev_addr;
106 			/* __force to make sparse happy with type conversion */
107 			va = (__force void *)(wkupm3->mem[i].cpu_addr + offset);
108 			break;
109 		}
110 	}
111 
112 	return va;
113 }
114 
115 static const struct rproc_ops wkup_m3_rproc_ops = {
116 	.start		= wkup_m3_rproc_start,
117 	.stop		= wkup_m3_rproc_stop,
118 	.da_to_va	= wkup_m3_rproc_da_to_va,
119 };
120 
121 static const struct of_device_id wkup_m3_rproc_of_match[] = {
122 	{ .compatible = "ti,am3352-wkup-m3", },
123 	{ .compatible = "ti,am4372-wkup-m3", },
124 	{},
125 };
126 MODULE_DEVICE_TABLE(of, wkup_m3_rproc_of_match);
127 
128 static int wkup_m3_rproc_probe(struct platform_device *pdev)
129 {
130 	struct device *dev = &pdev->dev;
131 	struct wkup_m3_platform_data *pdata = dev->platform_data;
132 	/* umem always needs to be processed first */
133 	const char *mem_names[WKUPM3_MEM_MAX] = { "umem", "dmem" };
134 	struct wkup_m3_rproc *wkupm3;
135 	const char *fw_name;
136 	struct rproc *rproc;
137 	struct resource *res;
138 	const __be32 *addrp;
139 	u32 l4_offset = 0;
140 	u64 size;
141 	int ret;
142 	int i;
143 
144 	ret = of_property_read_string(dev->of_node, "ti,pm-firmware",
145 				      &fw_name);
146 	if (ret) {
147 		dev_err(dev, "No firmware filename given\n");
148 		return -ENODEV;
149 	}
150 
151 	pm_runtime_enable(&pdev->dev);
152 	ret = pm_runtime_get_sync(&pdev->dev);
153 	if (ret < 0) {
154 		dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
155 		goto err;
156 	}
157 
158 	rproc = rproc_alloc(dev, "wkup_m3", &wkup_m3_rproc_ops,
159 			    fw_name, sizeof(*wkupm3));
160 	if (!rproc) {
161 		ret = -ENOMEM;
162 		goto err;
163 	}
164 
165 	rproc->auto_boot = false;
166 
167 	wkupm3 = rproc->priv;
168 	wkupm3->rproc = rproc;
169 	wkupm3->pdev = pdev;
170 
171 	wkupm3->rsts = devm_reset_control_get_optional_shared(dev, "rstctrl");
172 	if (IS_ERR(wkupm3->rsts))
173 		return PTR_ERR(wkupm3->rsts);
174 	if (!wkupm3->rsts) {
175 		if (!(pdata && pdata->deassert_reset && pdata->assert_reset &&
176 		      pdata->reset_name)) {
177 			dev_err(dev, "Platform data missing!\n");
178 			ret = -ENODEV;
179 			goto err_put_rproc;
180 		}
181 	}
182 
183 	for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
184 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
185 						   mem_names[i]);
186 		wkupm3->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
187 		if (IS_ERR(wkupm3->mem[i].cpu_addr)) {
188 			dev_err(&pdev->dev, "devm_ioremap_resource failed for resource %d\n",
189 				i);
190 			ret = PTR_ERR(wkupm3->mem[i].cpu_addr);
191 			goto err_put_rproc;
192 		}
193 		wkupm3->mem[i].bus_addr = res->start;
194 		wkupm3->mem[i].size = resource_size(res);
195 		addrp = of_get_address(dev->of_node, i, &size, NULL);
196 		/*
197 		 * The wkupm3 has umem at address 0 in its view, so the device
198 		 * addresses for each memory region is computed as a relative
199 		 * offset of the bus address for umem, and therefore needs to be
200 		 * processed first.
201 		 */
202 		if (!strcmp(mem_names[i], "umem"))
203 			l4_offset = be32_to_cpu(*addrp);
204 		wkupm3->mem[i].dev_addr = be32_to_cpu(*addrp) - l4_offset;
205 	}
206 
207 	dev_set_drvdata(dev, rproc);
208 
209 	ret = rproc_add(rproc);
210 	if (ret) {
211 		dev_err(dev, "rproc_add failed\n");
212 		goto err_put_rproc;
213 	}
214 
215 	return 0;
216 
217 err_put_rproc:
218 	rproc_free(rproc);
219 err:
220 	pm_runtime_put_noidle(dev);
221 	pm_runtime_disable(dev);
222 	return ret;
223 }
224 
225 static int wkup_m3_rproc_remove(struct platform_device *pdev)
226 {
227 	struct rproc *rproc = platform_get_drvdata(pdev);
228 
229 	rproc_del(rproc);
230 	rproc_free(rproc);
231 	pm_runtime_put_sync(&pdev->dev);
232 	pm_runtime_disable(&pdev->dev);
233 
234 	return 0;
235 }
236 
237 #ifdef CONFIG_PM
238 static int wkup_m3_rpm_suspend(struct device *dev)
239 {
240 	return -EBUSY;
241 }
242 
243 static int wkup_m3_rpm_resume(struct device *dev)
244 {
245 	return 0;
246 }
247 #endif
248 
249 static const struct dev_pm_ops wkup_m3_rproc_pm_ops = {
250 	SET_RUNTIME_PM_OPS(wkup_m3_rpm_suspend, wkup_m3_rpm_resume, NULL)
251 };
252 
253 static struct platform_driver wkup_m3_rproc_driver = {
254 	.probe = wkup_m3_rproc_probe,
255 	.remove = wkup_m3_rproc_remove,
256 	.driver = {
257 		.name = "wkup_m3_rproc",
258 		.of_match_table = wkup_m3_rproc_of_match,
259 		.pm = &wkup_m3_rproc_pm_ops,
260 	},
261 };
262 
263 module_platform_driver(wkup_m3_rproc_driver);
264 
265 MODULE_LICENSE("GPL v2");
266 MODULE_DESCRIPTION("TI Wakeup M3 remote processor control driver");
267 MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
268