treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 174
[sfrench/cifs-2.6.git] / drivers / remoteproc / omap_remoteproc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OMAP Remote Processor driver
4  *
5  * Copyright (C) 2011 Texas Instruments, Inc.
6  * Copyright (C) 2011 Google, Inc.
7  *
8  * Ohad Ben-Cohen <ohad@wizery.com>
9  * Brian Swetland <swetland@google.com>
10  * Fernando Guzman Lugo <fernando.lugo@ti.com>
11  * Mark Grosen <mgrosen@ti.com>
12  * Suman Anna <s-anna@ti.com>
13  * Hari Kanigeri <h-kanigeri2@ti.com>
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/remoteproc.h>
22 #include <linux/mailbox_client.h>
23 #include <linux/omap-mailbox.h>
24
25 #include <linux/platform_data/remoteproc-omap.h>
26
27 #include "omap_remoteproc.h"
28 #include "remoteproc_internal.h"
29
30 /**
31  * struct omap_rproc - omap remote processor state
32  * @mbox: mailbox channel handle
33  * @client: mailbox client to request the mailbox channel
34  * @rproc: rproc handle
35  */
36 struct omap_rproc {
37         struct mbox_chan *mbox;
38         struct mbox_client client;
39         struct rproc *rproc;
40 };
41
42 /**
43  * omap_rproc_mbox_callback() - inbound mailbox message handler
44  * @client: mailbox client pointer used for requesting the mailbox channel
45  * @data: mailbox payload
46  *
47  * This handler is invoked by omap's mailbox driver whenever a mailbox
48  * message is received. Usually, the mailbox payload simply contains
49  * the index of the virtqueue that is kicked by the remote processor,
50  * and we let remoteproc core handle it.
51  *
52  * In addition to virtqueue indices, we also have some out-of-band values
53  * that indicates different events. Those values are deliberately very
54  * big so they don't coincide with virtqueue indices.
55  */
56 static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
57 {
58         struct omap_rproc *oproc = container_of(client, struct omap_rproc,
59                                                 client);
60         struct device *dev = oproc->rproc->dev.parent;
61         const char *name = oproc->rproc->name;
62         u32 msg = (u32)data;
63
64         dev_dbg(dev, "mbox msg: 0x%x\n", msg);
65
66         switch (msg) {
67         case RP_MBOX_CRASH:
68                 /* just log this for now. later, we'll also do recovery */
69                 dev_err(dev, "omap rproc %s crashed\n", name);
70                 break;
71         case RP_MBOX_ECHO_REPLY:
72                 dev_info(dev, "received echo reply from %s\n", name);
73                 break;
74         default:
75                 /* msg contains the index of the triggered vring */
76                 if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
77                         dev_dbg(dev, "no message was found in vqid %d\n", msg);
78         }
79 }
80
81 /* kick a virtqueue */
82 static void omap_rproc_kick(struct rproc *rproc, int vqid)
83 {
84         struct omap_rproc *oproc = rproc->priv;
85         struct device *dev = rproc->dev.parent;
86         int ret;
87
88         /* send the index of the triggered virtqueue in the mailbox payload */
89         ret = mbox_send_message(oproc->mbox, (void *)vqid);
90         if (ret < 0)
91                 dev_err(dev, "failed to send mailbox message, status = %d\n",
92                         ret);
93 }
94
95 /*
96  * Power up the remote processor.
97  *
98  * This function will be invoked only after the firmware for this rproc
99  * was loaded, parsed successfully, and all of its resource requirements
100  * were met.
101  */
102 static int omap_rproc_start(struct rproc *rproc)
103 {
104         struct omap_rproc *oproc = rproc->priv;
105         struct device *dev = rproc->dev.parent;
106         struct platform_device *pdev = to_platform_device(dev);
107         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
108         int ret;
109         struct mbox_client *client = &oproc->client;
110
111         if (pdata->set_bootaddr)
112                 pdata->set_bootaddr(rproc->bootaddr);
113
114         client->dev = dev;
115         client->tx_done = NULL;
116         client->rx_callback = omap_rproc_mbox_callback;
117         client->tx_block = false;
118         client->knows_txdone = false;
119
120         oproc->mbox = omap_mbox_request_channel(client, pdata->mbox_name);
121         if (IS_ERR(oproc->mbox)) {
122                 ret = -EBUSY;
123                 dev_err(dev, "mbox_request_channel failed: %ld\n",
124                         PTR_ERR(oproc->mbox));
125                 return ret;
126         }
127
128         /*
129          * Ping the remote processor. this is only for sanity-sake;
130          * there is no functional effect whatsoever.
131          *
132          * Note that the reply will _not_ arrive immediately: this message
133          * will wait in the mailbox fifo until the remote processor is booted.
134          */
135         ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
136         if (ret < 0) {
137                 dev_err(dev, "mbox_send_message failed: %d\n", ret);
138                 goto put_mbox;
139         }
140
141         ret = pdata->device_enable(pdev);
142         if (ret) {
143                 dev_err(dev, "omap_device_enable failed: %d\n", ret);
144                 goto put_mbox;
145         }
146
147         return 0;
148
149 put_mbox:
150         mbox_free_channel(oproc->mbox);
151         return ret;
152 }
153
154 /* power off the remote processor */
155 static int omap_rproc_stop(struct rproc *rproc)
156 {
157         struct device *dev = rproc->dev.parent;
158         struct platform_device *pdev = to_platform_device(dev);
159         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
160         struct omap_rproc *oproc = rproc->priv;
161         int ret;
162
163         ret = pdata->device_shutdown(pdev);
164         if (ret)
165                 return ret;
166
167         mbox_free_channel(oproc->mbox);
168
169         return 0;
170 }
171
172 static const struct rproc_ops omap_rproc_ops = {
173         .start          = omap_rproc_start,
174         .stop           = omap_rproc_stop,
175         .kick           = omap_rproc_kick,
176 };
177
178 static int omap_rproc_probe(struct platform_device *pdev)
179 {
180         struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
181         struct omap_rproc *oproc;
182         struct rproc *rproc;
183         int ret;
184
185         ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
186         if (ret) {
187                 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
188                 return ret;
189         }
190
191         rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
192                             pdata->firmware, sizeof(*oproc));
193         if (!rproc)
194                 return -ENOMEM;
195
196         oproc = rproc->priv;
197         oproc->rproc = rproc;
198         /* All existing OMAP IPU and DSP processors have an MMU */
199         rproc->has_iommu = true;
200
201         platform_set_drvdata(pdev, rproc);
202
203         ret = rproc_add(rproc);
204         if (ret)
205                 goto free_rproc;
206
207         return 0;
208
209 free_rproc:
210         rproc_free(rproc);
211         return ret;
212 }
213
214 static int omap_rproc_remove(struct platform_device *pdev)
215 {
216         struct rproc *rproc = platform_get_drvdata(pdev);
217
218         rproc_del(rproc);
219         rproc_free(rproc);
220
221         return 0;
222 }
223
224 static struct platform_driver omap_rproc_driver = {
225         .probe = omap_rproc_probe,
226         .remove = omap_rproc_remove,
227         .driver = {
228                 .name = "omap-rproc",
229         },
230 };
231
232 module_platform_driver(omap_rproc_driver);
233
234 MODULE_LICENSE("GPL v2");
235 MODULE_DESCRIPTION("OMAP Remote Processor control driver");