[ipc/ipcdev.git] / linux / patches / 3.8.0 / omapl138 / 0005-Add-a-remoteproc-driver-implementation-for-OMAP-L138.patch
1 From 8425d8e1cb44c188816ef6648d4c2f4fa115dcc8 Mon Sep 17 00:00:00 2001
2 From: Robert Tivy <rtivy@ti.com>
3 Date: Fri, 8 Mar 2013 10:36:19 -0800
4 Subject: [PATCH v8 5/7] ARM: davinci: Add a remoteproc driver implementation for OMAP-L138 DSP
6 Signed-off-by: Robert Tivy <rtivy@ti.com>
7 ---
8 drivers/remoteproc/Makefile | 1 +
9 drivers/remoteproc/da8xx_remoteproc.c | 323 +++++++++++++++++++++++++++++++++
10 2 files changed, 324 insertions(+)
11 create mode 100644 drivers/remoteproc/da8xx_remoteproc.c
13 diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
14 index 391b651..ac2ff75 100644
15 --- a/drivers/remoteproc/Makefile
16 +++ b/drivers/remoteproc/Makefile
17 @@ -9,3 +9,4 @@ remoteproc-y += remoteproc_virtio.o
18 remoteproc-y += remoteproc_elf_loader.o
19 obj-$(CONFIG_OMAP_REMOTEPROC) += omap_remoteproc.o
20 obj-$(CONFIG_STE_MODEM_RPROC) += ste_modem_rproc.o
21 +obj-$(CONFIG_DA8XX_REMOTEPROC) += da8xx_remoteproc.o
22 diff --git a/drivers/remoteproc/da8xx_remoteproc.c b/drivers/remoteproc/da8xx_remoteproc.c
23 new file mode 100644
24 index 0000000..a40e72a
25 --- /dev/null
26 +++ b/drivers/remoteproc/da8xx_remoteproc.c
27 @@ -0,0 +1,323 @@
28 +/*
29 + * Remote processor machine-specific module for DA8XX
30 + *
31 + * Copyright (C) 2013 Texas Instruments, Inc.
32 + *
33 + * This program is free software; you can redistribute it and/or
34 + * modify it under the terms of the GNU General Public License
35 + * version 2 as published by the Free Software Foundation.
36 + */
37 +
38 +#include <linux/bitops.h>
39 +#include <linux/clk.h>
40 +#include <linux/err.h>
41 +#include <linux/interrupt.h>
42 +#include <linux/io.h>
43 +#include <linux/irq.h>
44 +#include <linux/kernel.h>
45 +#include <linux/module.h>
46 +#include <linux/platform_device.h>
47 +#include <linux/remoteproc.h>
48 +
49 +#include <mach/clock.h> /* for davinci_clk_reset_assert/deassert() */
50 +
51 +#include "remoteproc_internal.h"
52 +
53 +static char *da8xx_fw_name;
54 +module_param(da8xx_fw_name, charp, S_IRUGO);
55 +MODULE_PARM_DESC(da8xx_fw_name,
56 + "\n\t\tName of DSP firmware file in /lib/firmware");
57 +
58 +/*
59 + * OMAP-L138 Technical References:
60 + * http://www.ti.com/product/omap-l138
61 + */
62 +#define SYSCFG_CHIPSIG0 BIT(0)
63 +#define SYSCFG_CHIPSIG1 BIT(1)
64 +#define SYSCFG_CHIPSIG2 BIT(2)
65 +#define SYSCFG_CHIPSIG3 BIT(3)
66 +#define SYSCFG_CHIPSIG4 BIT(4)
67 +
68 +/**
69 + * struct da8xx_rproc - da8xx remote processor instance state
70 + * @rproc: rproc handle
71 + * @dsp_clk: placeholder for platform's DSP clk
72 + * @ack_fxn: chip-specific ack function for ack'ing irq
73 + * @irq_data: ack_fxn function parameter
74 + * @chipsig: virt ptr to DSP interrupt registers (CHIPSIG & CHIPSIG_CLR)
75 + * @bootreg: virt ptr to DSP boot address register (HOST1CFG)
76 + * @irq: irq # used by this instance
77 + */
78 +struct da8xx_rproc {
79 + struct rproc *rproc;
80 + struct clk *dsp_clk;
81 + void (*ack_fxn)(struct irq_data *data);
82 + struct irq_data *irq_data;
83 + void __iomem *chipsig;
84 + void __iomem *bootreg;
85 + int irq;
86 +};
87 +
88 +/**
89 + * handle_event() - inbound virtqueue message workqueue function
90 + *
91 + * This function is registered as a kernel thread and is scheduled by the
92 + * kernel handler.
93 + */
94 +static irqreturn_t handle_event(int irq, void *p)
95 +{
96 + struct rproc *rproc = (struct rproc *)p;
97 +
98 + /* Process incoming buffers on all our vrings */
99 + rproc_vq_interrupt(rproc, -1);
100 +
101 + return IRQ_HANDLED;
102 +}
103 +
104 +/**
105 + * da8xx_rproc_callback() - inbound virtqueue message handler
106 + *
107 + * This handler is invoked directly by the kernel whenever the remote
108 + * core (DSP) has modified the state of a virtqueue. There is no
109 + * "payload" message indicating the virtqueue index as is the case with
110 + * mailbox-based implementations on OMAP4. As such, this handler "polls"
111 + * each known virtqueue index for every invocation.
112 + */
113 +static irqreturn_t da8xx_rproc_callback(int irq, void *p)
114 +{
115 + struct rproc *rproc = (struct rproc *)p;
116 + struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
117 + u32 chipsig;
118 +
119 + chipsig = readl(drproc->chipsig);
120 + if (chipsig & SYSCFG_CHIPSIG0) {
121 + /* Clear interrupt level source */
122 + writel(SYSCFG_CHIPSIG0, drproc->chipsig + 4);
123 +
124 + /*
125 + * ACK intr to AINTC.
126 + *
127 + * It has already been ack'ed by the kernel before calling
128 + * this function, but since the ARM<->DSP interrupts in the
129 + * CHIPSIG register are "level" instead of "pulse" variety,
130 + * we need to ack it after taking down the level else we'll
131 + * be called again immediately after returning.
132 + */
133 + drproc->ack_fxn(drproc->irq_data);
134 +
135 + return IRQ_WAKE_THREAD;
136 + }
137 +
138 + return IRQ_HANDLED;
139 +}
140 +
141 +static int da8xx_rproc_start(struct rproc *rproc)
142 +{
143 + struct device *dev = rproc->dev.parent;
144 + struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
145 + struct clk *dsp_clk = drproc->dsp_clk;
146 +
147 + /* hw requires the start (boot) address be on 1KB boundary */
148 + if (rproc->bootaddr & 0x3ff) {
149 + dev_err(dev, "invalid boot address: must be aligned to 1KB\n");
150 +
151 + return -EINVAL;
152 + }
153 + writel(rproc->bootaddr, drproc->bootreg);
154 +
155 + clk_enable(dsp_clk);
156 + davinci_clk_reset_deassert(dsp_clk);
157 +
158 + return 0;
159 +}
160 +
161 +static int da8xx_rproc_stop(struct rproc *rproc)
162 +{
163 + struct da8xx_rproc *drproc = rproc->priv;
164 +
165 + clk_disable(drproc->dsp_clk);
166 +
167 + return 0;
168 +}
169 +
170 +/* kick a virtqueue */
171 +static void da8xx_rproc_kick(struct rproc *rproc, int vqid)
172 +{
173 + struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
174 +
175 + /* Interupt remote proc */
176 + writel(SYSCFG_CHIPSIG2, drproc->chipsig);
177 +}
178 +
179 +static struct rproc_ops da8xx_rproc_ops = {
180 + .start = da8xx_rproc_start,
181 + .stop = da8xx_rproc_stop,
182 + .kick = da8xx_rproc_kick,
183 +};
184 +
185 +static int reset_assert(struct device *dev)
186 +{
187 + struct clk *dsp_clk;
188 +
189 + dsp_clk = clk_get(dev, NULL);
190 + if (IS_ERR(dsp_clk)) {
191 + dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
192 + return PTR_RET(dsp_clk);
193 + }
194 + davinci_clk_reset_assert(dsp_clk);
195 + clk_put(dsp_clk);
196 +
197 + return 0;
198 +}
199 +
200 +static int da8xx_rproc_probe(struct platform_device *pdev)
201 +{
202 + struct device *dev = &pdev->dev;
203 + struct da8xx_rproc *drproc;
204 + struct rproc *rproc;
205 + struct irq_data *irq_data;
206 + struct resource *bootreg_res;
207 + struct resource *chipsig_res;
208 + struct clk *dsp_clk;
209 + void __iomem *chipsig;
210 + void __iomem *bootreg;
211 + int irq;
212 + int ret;
213 +
214 + irq = platform_get_irq(pdev, 0);
215 + if (irq < 0) {
216 + dev_err(dev, "platform_get_irq(pdev, 0) error: %d\n", irq);
217 + return irq;
218 + }
219 +
220 + irq_data = irq_get_irq_data(irq);
221 + if (!irq_data) {
222 + dev_err(dev, "irq_get_irq_data(%d): NULL\n", irq);
223 + return -EINVAL;
224 + }
225 +
226 + bootreg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
227 + if (!bootreg_res) {
228 + dev_err(dev,
229 + "platform_get_resource(IORESOURCE_MEM, 0): NULL\n");
230 + return -EADDRNOTAVAIL;
231 + }
232 +
233 + chipsig_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
234 + if (!chipsig_res) {
235 + dev_err(dev,
236 + "platform_get_resource(IORESOURCE_MEM, 1): NULL\n");
237 + return -EADDRNOTAVAIL;
238 + }
239 +
240 + bootreg = devm_request_and_ioremap(dev, bootreg_res);
241 + if (!bootreg) {
242 + dev_err(dev, "unable to map boot register\n");
243 + return -EADDRNOTAVAIL;
244 + }
245 +
246 + chipsig = devm_request_and_ioremap(dev, chipsig_res);
247 + if (!chipsig) {
248 + dev_err(dev, "unable to map CHIPSIG register\n");
249 + return -EADDRNOTAVAIL;
250 + }
251 +
252 + dsp_clk = devm_clk_get(dev, NULL);
253 + if (IS_ERR(dsp_clk)) {
254 + dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
255 +
256 + return PTR_RET(dsp_clk);
257 + }
258 +
259 + rproc = rproc_alloc(dev, "dsp", &da8xx_rproc_ops, da8xx_fw_name,
260 + sizeof(*drproc));
261 + if (!rproc)
262 + return -ENOMEM;
263 +
264 + drproc = rproc->priv;
265 + drproc->rproc = rproc;
266 +
267 + platform_set_drvdata(pdev, rproc);
268 +
269 + /* everything the ISR needs is now setup, so hook it up */
270 + ret = devm_request_threaded_irq(dev, irq, da8xx_rproc_callback,
271 + handle_event, 0, "da8xx-remoteproc", rproc);
272 + if (ret) {
273 + dev_err(dev, "devm_request_threaded_irq error: %d\n", ret);
274 + goto free_rproc;
275 + }
276 +
277 + /*
278 + * rproc_add() can end up enabling the DSP's clk with the DSP
279 + * *not* in reset, but da8xx_rproc_start() needs the DSP to be
280 + * held in reset at the time it is called.
281 + */
282 + ret = reset_assert(dev);
283 + if (ret)
284 + goto free_rproc;
285 +
286 + ret = rproc_add(rproc);
287 + if (ret) {
288 + dev_err(dev, "rproc_add failed: %d\n", ret);
289 + goto free_rproc;
290 + }
291 +
292 + drproc->chipsig = chipsig;
293 + drproc->bootreg = bootreg;
294 + drproc->ack_fxn = irq_data->chip->irq_ack;
295 + drproc->irq_data = irq_data;
296 + drproc->irq = irq;
297 + drproc->dsp_clk = dsp_clk;
298 +
299 + return 0;
300 +
301 +free_rproc:
302 + rproc_put(rproc);
303 +
304 + return ret;
305 +}
306 +
307 +static int da8xx_rproc_remove(struct platform_device *pdev)
308 +{
309 + struct device *dev = &pdev->dev;
310 + struct rproc *rproc = platform_get_drvdata(pdev);
311 + struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
312 +
313 + /*
314 + * It's important to place the DSP in reset before going away,
315 + * since a subsequent insmod of this module may enable the DSP's
316 + * clock before its program/boot-address has been loaded and
317 + * before this module's probe has had a chance to reset the DSP.
318 + * Without the reset, the DSP can lockup permanently when it
319 + * begins executing garbage.
320 + */
321 + reset_assert(dev);
322 +
323 + /*
324 + * The devm subsystem might end up releasing things before
325 + * freeing the irq, thus allowing an interrupt to sneak in while
326 + * the device is being removed. This should prevent that.
327 + */
328 + disable_irq(drproc->irq);
329 +
330 + devm_clk_put(dev, drproc->dsp_clk);
331 +
332 + rproc_del(rproc);
333 + rproc_put(rproc);
334 +
335 + return 0;
336 +}
337 +
338 +static struct platform_driver da8xx_rproc_driver = {
339 + .probe = da8xx_rproc_probe,
340 + .remove = da8xx_rproc_remove,
341 + .driver = {
342 + .name = "davinci-rproc",
343 + .owner = THIS_MODULE,
344 + },
345 +};
346 +
347 +module_platform_driver(da8xx_rproc_driver);
348 +
349 +MODULE_LICENSE("GPL v2");
350 +MODULE_DESCRIPTION("DA8XX Remote Processor control driver");
351 --
352 1.7.9.4