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