]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - drivers/remoteproc/wkup_m3_rproc.c
remoteproc: Extend rproc_da_to_va() API with a flags parameter
[rpmsg/rpmsg.git] / drivers / remoteproc / wkup_m3_rproc.c
1 /*
2  * TI AMx3 Wakeup M3 Remote Processor driver
3  *
4  * Copyright (C) 2014-2015 Texas Instruments, Inc.
5  *
6  * Dave Gerlach <d-gerlach@ti.com>
7  * Suman Anna <s-anna@ti.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/of_address.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/remoteproc.h>
29 #include <linux/platform_data/wkup_m3.h>
31 #include "remoteproc_internal.h"
33 #define WKUPM3_MEM_MAX  2
35 /**
36  * struct wkup_m3_mem - WkupM3 internal memory structure
37  * @cpu_addr: MPU virtual address of the memory region
38  * @bus_addr: Bus address used to access the memory region
39  * @dev_addr: Device address from Wakeup M3 view
40  * @size: Size of the memory region
41  */
42 struct wkup_m3_mem {
43         void __iomem *cpu_addr;
44         phys_addr_t bus_addr;
45         u32 dev_addr;
46         size_t size;
47 };
49 /**
50  * struct wkup_m3_rproc - WkupM3 remote processor state
51  * @rproc: rproc handle
52  * @pdev: pointer to platform device
53  * @mem: WkupM3 memory information
54  */
55 struct wkup_m3_rproc {
56         struct rproc *rproc;
57         struct platform_device *pdev;
58         struct wkup_m3_mem mem[WKUPM3_MEM_MAX];
59 };
61 static int wkup_m3_rproc_start(struct rproc *rproc)
62 {
63         struct wkup_m3_rproc *wkupm3 = rproc->priv;
64         struct platform_device *pdev = wkupm3->pdev;
65         struct device *dev = &pdev->dev;
66         struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
68         if (pdata->deassert_reset(pdev, pdata->reset_name)) {
69                 dev_err(dev, "Unable to reset wkup_m3!\n");
70                 return -ENODEV;
71         }
73         return 0;
74 }
76 static int wkup_m3_rproc_stop(struct rproc *rproc)
77 {
78         struct wkup_m3_rproc *wkupm3 = rproc->priv;
79         struct platform_device *pdev = wkupm3->pdev;
80         struct device *dev = &pdev->dev;
81         struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
83         if (pdata->assert_reset(pdev, pdata->reset_name)) {
84                 dev_err(dev, "Unable to assert reset of wkup_m3!\n");
85                 return -ENODEV;
86         }
88         return 0;
89 }
91 static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
92                                     u32 flags)
93 {
94         struct wkup_m3_rproc *wkupm3 = rproc->priv;
95         void *va = NULL;
96         int i;
97         u32 offset;
99         if (len <= 0)
100                 return NULL;
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         }
112         return va;
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 };
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);
128 static int wkup_m3_rproc_probe(struct platform_device *pdev)
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;
144         if (!(pdata && pdata->deassert_reset && pdata->assert_reset &&
145               pdata->reset_name)) {
146                 dev_err(dev, "Platform data missing!\n");
147                 return -ENODEV;
148         }
150         ret = of_property_read_string(dev->of_node, "ti,pm-firmware",
151                                       &fw_name);
152         if (ret) {
153                 dev_err(dev, "No firmware filename given\n");
154                 return -ENODEV;
155         }
157         pm_runtime_enable(&pdev->dev);
158         ret = pm_runtime_get_sync(&pdev->dev);
159         if (ret < 0) {
160                 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
161                 goto err;
162         }
164         rproc = rproc_alloc(dev, "wkup_m3", &wkup_m3_rproc_ops,
165                             fw_name, sizeof(*wkupm3));
166         if (!rproc) {
167                 ret = -ENOMEM;
168                 goto err;
169         }
171         rproc->auto_boot = false;
172         rproc->deny_sysfs_ops = 1;
174         wkupm3 = rproc->priv;
175         wkupm3->rproc = rproc;
176         wkupm3->pdev = pdev;
178         for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
179                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
180                                                    mem_names[i]);
181                 wkupm3->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
182                 if (IS_ERR(wkupm3->mem[i].cpu_addr)) {
183                         dev_err(&pdev->dev, "devm_ioremap_resource failed for resource %d\n",
184                                 i);
185                         ret = PTR_ERR(wkupm3->mem[i].cpu_addr);
186                         goto err;
187                 }
188                 wkupm3->mem[i].bus_addr = res->start;
189                 wkupm3->mem[i].size = resource_size(res);
190                 addrp = of_get_address(dev->of_node, i, &size, NULL);
191                 /*
192                  * The wkupm3 has umem at address 0 in its view, so the device
193                  * addresses for each memory region is computed as a relative
194                  * offset of the bus address for umem, and therefore needs to be
195                  * processed first.
196                  */
197                 if (!strcmp(mem_names[i], "umem"))
198                         l4_offset = be32_to_cpu(*addrp);
199                 wkupm3->mem[i].dev_addr = be32_to_cpu(*addrp) - l4_offset;
200         }
202         dev_set_drvdata(dev, rproc);
204         ret = rproc_add(rproc);
205         if (ret) {
206                 dev_err(dev, "rproc_add failed\n");
207                 goto err_put_rproc;
208         }
210         return 0;
212 err_put_rproc:
213         rproc_free(rproc);
214 err:
215         pm_runtime_put_noidle(dev);
216         pm_runtime_disable(dev);
217         return ret;
220 static int wkup_m3_rproc_remove(struct platform_device *pdev)
222         struct rproc *rproc = platform_get_drvdata(pdev);
224         rproc_del(rproc);
225         rproc_free(rproc);
226         pm_runtime_put_sync(&pdev->dev);
227         pm_runtime_disable(&pdev->dev);
229         return 0;
232 #ifdef CONFIG_PM
233 static int wkup_m3_rpm_suspend(struct device *dev)
235         return -EBUSY;
238 static int wkup_m3_rpm_resume(struct device *dev)
240         return 0;
242 #endif
244 static const struct dev_pm_ops wkup_m3_rproc_pm_ops = {
245         SET_RUNTIME_PM_OPS(wkup_m3_rpm_suspend, wkup_m3_rpm_resume, NULL)
246 };
248 static struct platform_driver wkup_m3_rproc_driver = {
249         .probe = wkup_m3_rproc_probe,
250         .remove = wkup_m3_rproc_remove,
251         .driver = {
252                 .name = "wkup_m3_rproc",
253                 .of_match_table = wkup_m3_rproc_of_match,
254                 .pm = &wkup_m3_rproc_pm_ops,
255         },
256 };
258 module_platform_driver(wkup_m3_rproc_driver);
260 MODULE_LICENSE("GPL v2");
261 MODULE_DESCRIPTION("TI Wakeup M3 remote processor control driver");
262 MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");