xref: /linux/drivers/remoteproc/wkup_m3_rproc.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
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.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 
wkup_m3_rproc_start(struct rproc * rproc)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 
wkup_m3_rproc_stop(struct rproc * rproc)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 
wkup_m3_rproc_da_to_va(struct rproc * rproc,u64 da,size_t len,bool * is_iomem)92 static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
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 
wkup_m3_rproc_probe(struct platform_device * pdev)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 	rproc->sysfs_read_only = true;
167 
168 	wkupm3 = rproc->priv;
169 	wkupm3->rproc = rproc;
170 	wkupm3->pdev = pdev;
171 
172 	wkupm3->rsts = devm_reset_control_get_optional_shared(dev, "rstctrl");
173 	if (IS_ERR(wkupm3->rsts))
174 		return PTR_ERR(wkupm3->rsts);
175 	if (!wkupm3->rsts) {
176 		if (!(pdata && pdata->deassert_reset && pdata->assert_reset &&
177 		      pdata->reset_name)) {
178 			dev_err(dev, "Platform data missing!\n");
179 			ret = -ENODEV;
180 			goto err_put_rproc;
181 		}
182 	}
183 
184 	for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
185 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
186 						   mem_names[i]);
187 		wkupm3->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
188 		if (IS_ERR(wkupm3->mem[i].cpu_addr)) {
189 			dev_err(&pdev->dev, "devm_ioremap_resource failed for resource %d\n",
190 				i);
191 			ret = PTR_ERR(wkupm3->mem[i].cpu_addr);
192 			goto err_put_rproc;
193 		}
194 		wkupm3->mem[i].bus_addr = res->start;
195 		wkupm3->mem[i].size = resource_size(res);
196 		addrp = of_get_address(dev->of_node, i, &size, NULL);
197 		/*
198 		 * The wkupm3 has umem at address 0 in its view, so the device
199 		 * addresses for each memory region is computed as a relative
200 		 * offset of the bus address for umem, and therefore needs to be
201 		 * processed first.
202 		 */
203 		if (!strcmp(mem_names[i], "umem"))
204 			l4_offset = be32_to_cpu(*addrp);
205 		wkupm3->mem[i].dev_addr = be32_to_cpu(*addrp) - l4_offset;
206 	}
207 
208 	dev_set_drvdata(dev, rproc);
209 
210 	ret = rproc_add(rproc);
211 	if (ret) {
212 		dev_err(dev, "rproc_add failed\n");
213 		goto err_put_rproc;
214 	}
215 
216 	return 0;
217 
218 err_put_rproc:
219 	rproc_free(rproc);
220 err:
221 	pm_runtime_put_noidle(dev);
222 	pm_runtime_disable(dev);
223 	return ret;
224 }
225 
wkup_m3_rproc_remove(struct platform_device * pdev)226 static void wkup_m3_rproc_remove(struct platform_device *pdev)
227 {
228 	struct rproc *rproc = platform_get_drvdata(pdev);
229 
230 	rproc_del(rproc);
231 	rproc_free(rproc);
232 	pm_runtime_put_sync(&pdev->dev);
233 	pm_runtime_disable(&pdev->dev);
234 }
235 
236 #ifdef CONFIG_PM
wkup_m3_rpm_suspend(struct device * dev)237 static int wkup_m3_rpm_suspend(struct device *dev)
238 {
239 	return -EBUSY;
240 }
241 
wkup_m3_rpm_resume(struct device * dev)242 static int wkup_m3_rpm_resume(struct device *dev)
243 {
244 	return 0;
245 }
246 #endif
247 
248 static const struct dev_pm_ops wkup_m3_rproc_pm_ops = {
249 	SET_RUNTIME_PM_OPS(wkup_m3_rpm_suspend, wkup_m3_rpm_resume, NULL)
250 };
251 
252 static struct platform_driver wkup_m3_rproc_driver = {
253 	.probe = wkup_m3_rproc_probe,
254 	.remove_new = wkup_m3_rproc_remove,
255 	.driver = {
256 		.name = "wkup_m3_rproc",
257 		.of_match_table = wkup_m3_rproc_of_match,
258 		.pm = &wkup_m3_rproc_pm_ops,
259 	},
260 };
261 
262 module_platform_driver(wkup_m3_rproc_driver);
263 
264 MODULE_LICENSE("GPL v2");
265 MODULE_DESCRIPTION("TI Wakeup M3 remote processor control driver");
266 MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
267