Merge branch 'lpc32xx/core2' of git://git.antcom.de/linux-2.6 into next/soc
[cascardo/linux.git] / drivers / remoteproc / omap_remoteproc.c
1 /*
2  * OMAP Remote Processor driver
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  * Copyright (C) 2011 Google, Inc.
6  *
7  * Ohad Ben-Cohen <ohad@wizery.com>
8  * Brian Swetland <swetland@google.com>
9  * Fernando Guzman Lugo <fernando.lugo@ti.com>
10  * Mark Grosen <mgrosen@ti.com>
11  * Suman Anna <s-anna@ti.com>
12  * Hari Kanigeri <h-kanigeri2@ti.com>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * version 2 as published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/err.h>
27 #include <linux/platform_device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/remoteproc.h>
30
31 #include <plat/mailbox.h>
32 #include <plat/remoteproc.h>
33
34 #include "omap_remoteproc.h"
35 #include "remoteproc_internal.h"
36
37 /**
38  * struct omap_rproc - omap remote processor state
39  * @mbox: omap mailbox handle
40  * @nb: notifier block that will be invoked on inbound mailbox messages
41  * @rproc: rproc handle
42  */
43 struct omap_rproc {
44         struct omap_mbox *mbox;
45         struct notifier_block nb;
46         struct rproc *rproc;
47 };
48
49 /**
50  * omap_rproc_mbox_callback() - inbound mailbox message handler
51  * @this: notifier block
52  * @index: unused
53  * @data: mailbox payload
54  *
55  * This handler is invoked by omap's mailbox driver whenever a mailbox
56  * message is received. Usually, the mailbox payload simply contains
57  * the index of the virtqueue that is kicked by the remote processor,
58  * and we let remoteproc core handle it.
59  *
60  * In addition to virtqueue indices, we also have some out-of-band values
61  * that indicates different events. Those values are deliberately very
62  * big so they don't coincide with virtqueue indices.
63  */
64 static int omap_rproc_mbox_callback(struct notifier_block *this,
65                                         unsigned long index, void *data)
66 {
67         mbox_msg_t msg = (mbox_msg_t) data;
68         struct omap_rproc *oproc = container_of(this, struct omap_rproc, nb);
69         struct device *dev = oproc->rproc->dev;
70         const char *name = oproc->rproc->name;
71
72         dev_dbg(dev, "mbox msg: 0x%x\n", msg);
73
74         switch (msg) {
75         case RP_MBOX_CRASH:
76                 /* just log this for now. later, we'll also do recovery */
77                 dev_err(dev, "omap rproc %s crashed\n", name);
78                 break;
79         case RP_MBOX_ECHO_REPLY:
80                 dev_info(dev, "received echo reply from %s\n", name);
81                 break;
82         default:
83                 /* msg contains the index of the triggered vring */
84                 if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
85                         dev_dbg(dev, "no message was found in vqid %d\n", msg);
86         }
87
88         return NOTIFY_DONE;
89 }
90
91 /* kick a virtqueue */
92 static void omap_rproc_kick(struct rproc *rproc, int vqid)
93 {
94         struct omap_rproc *oproc = rproc->priv;
95         int ret;
96
97         /* send the index of the triggered virtqueue in the mailbox payload */
98         ret = omap_mbox_msg_send(oproc->mbox, vqid);
99         if (ret)
100                 dev_err(rproc->dev, "omap_mbox_msg_send failed: %d\n", ret);
101 }
102
103 /*
104  * Power up the remote processor.
105  *
106  * This function will be invoked only after the firmware for this rproc
107  * was loaded, parsed successfully, and all of its resource requirements
108  * were met.
109  */
110 static int omap_rproc_start(struct rproc *rproc)
111 {
112         struct omap_rproc *oproc = rproc->priv;
113         struct platform_device *pdev = to_platform_device(rproc->dev);
114         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
115         int ret;
116
117         oproc->nb.notifier_call = omap_rproc_mbox_callback;
118
119         /* every omap rproc is assigned a mailbox instance for messaging */
120         oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb);
121         if (IS_ERR(oproc->mbox)) {
122                 ret = PTR_ERR(oproc->mbox);
123                 dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret);
124                 return ret;
125         }
126
127         /*
128          * Ping the remote processor. this is only for sanity-sake;
129          * there is no functional effect whatsoever.
130          *
131          * Note that the reply will _not_ arrive immediately: this message
132          * will wait in the mailbox fifo until the remote processor is booted.
133          */
134         ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST);
135         if (ret) {
136                 dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret);
137                 goto put_mbox;
138         }
139
140         ret = pdata->device_enable(pdev);
141         if (ret) {
142                 dev_err(rproc->dev, "omap_device_enable failed: %d\n", ret);
143                 goto put_mbox;
144         }
145
146         return 0;
147
148 put_mbox:
149         omap_mbox_put(oproc->mbox, &oproc->nb);
150         return ret;
151 }
152
153 /* power off the remote processor */
154 static int omap_rproc_stop(struct rproc *rproc)
155 {
156         struct platform_device *pdev = to_platform_device(rproc->dev);
157         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
158         struct omap_rproc *oproc = rproc->priv;
159         int ret;
160
161         ret = pdata->device_shutdown(pdev);
162         if (ret)
163                 return ret;
164
165         omap_mbox_put(oproc->mbox, &oproc->nb);
166
167         return 0;
168 }
169
170 static struct rproc_ops omap_rproc_ops = {
171         .start          = omap_rproc_start,
172         .stop           = omap_rproc_stop,
173         .kick           = omap_rproc_kick,
174 };
175
176 static int __devinit omap_rproc_probe(struct platform_device *pdev)
177 {
178         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
179         struct omap_rproc *oproc;
180         struct rproc *rproc;
181         int ret;
182
183         ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
184         if (ret) {
185                 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
186                 return ret;
187         }
188
189         rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
190                                 pdata->firmware, sizeof(*oproc));
191         if (!rproc)
192                 return -ENOMEM;
193
194         oproc = rproc->priv;
195         oproc->rproc = rproc;
196
197         platform_set_drvdata(pdev, rproc);
198
199         ret = rproc_register(rproc);
200         if (ret)
201                 goto free_rproc;
202
203         return 0;
204
205 free_rproc:
206         rproc_free(rproc);
207         return ret;
208 }
209
210 static int __devexit omap_rproc_remove(struct platform_device *pdev)
211 {
212         struct rproc *rproc = platform_get_drvdata(pdev);
213
214         return rproc_unregister(rproc);
215 }
216
217 static struct platform_driver omap_rproc_driver = {
218         .probe = omap_rproc_probe,
219         .remove = __devexit_p(omap_rproc_remove),
220         .driver = {
221                 .name = "omap-rproc",
222                 .owner = THIS_MODULE,
223         },
224 };
225
226 module_platform_driver(omap_rproc_driver);
227
228 MODULE_LICENSE("GPL v2");
229 MODULE_DESCRIPTION("OMAP Remote Processor control driver");