blob: 6398194075aae7bd7df7985ec9621a97aaf53bcd [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +02002/*
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>
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020014 */
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>
Suman Anna8841a662014-11-03 17:05:50 -060022#include <linux/mailbox_client.h>
Suman Annac869c752013-03-12 17:55:29 -050023#include <linux/omap-mailbox.h>
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020024
Arnd Bergmann22037472012-08-24 15:21:06 +020025#include <linux/platform_data/remoteproc-omap.h>
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020026
27#include "omap_remoteproc.h"
28#include "remoteproc_internal.h"
29
30/**
31 * struct omap_rproc - omap remote processor state
Suman Anna8841a662014-11-03 17:05:50 -060032 * @mbox: mailbox channel handle
33 * @client: mailbox client to request the mailbox channel
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020034 * @rproc: rproc handle
35 */
36struct omap_rproc {
Suman Anna8841a662014-11-03 17:05:50 -060037 struct mbox_chan *mbox;
38 struct mbox_client client;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020039 struct rproc *rproc;
40};
41
42/**
43 * omap_rproc_mbox_callback() - inbound mailbox message handler
Suman Anna8841a662014-11-03 17:05:50 -060044 * @client: mailbox client pointer used for requesting the mailbox channel
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020045 * @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 */
Suman Anna8841a662014-11-03 17:05:50 -060056static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020057{
Suman Anna8841a662014-11-03 17:05:50 -060058 struct omap_rproc *oproc = container_of(client, struct omap_rproc,
59 client);
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030060 struct device *dev = oproc->rproc->dev.parent;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020061 const char *name = oproc->rproc->name;
Suman Anna8841a662014-11-03 17:05:50 -060062 u32 msg = (u32)data;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020063
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:
Ohad Ben-Cohen55f34082012-02-13 11:24:50 +010075 /* msg contains the index of the triggered vring */
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020076 if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
77 dev_dbg(dev, "no message was found in vqid %d\n", msg);
78 }
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020079}
80
81/* kick a virtqueue */
82static void omap_rproc_kick(struct rproc *rproc, int vqid)
83{
84 struct omap_rproc *oproc = rproc->priv;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030085 struct device *dev = rproc->dev.parent;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020086 int ret;
87
88 /* send the index of the triggered virtqueue in the mailbox payload */
Suman Anna8841a662014-11-03 17:05:50 -060089 ret = mbox_send_message(oproc->mbox, (void *)vqid);
90 if (ret < 0)
Anna, Suman14096c12016-08-12 18:42:23 -050091 dev_err(dev, "failed to send mailbox message, status = %d\n",
92 ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020093}
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 */
102static int omap_rproc_start(struct rproc *rproc)
103{
104 struct omap_rproc *oproc = rproc->priv;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300105 struct device *dev = rproc->dev.parent;
106 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200107 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
108 int ret;
Suman Anna8841a662014-11-03 17:05:50 -0600109 struct mbox_client *client = &oproc->client;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200110
Juan Gutierrez4980f462012-08-15 10:25:48 -0500111 if (pdata->set_bootaddr)
112 pdata->set_bootaddr(rproc->bootaddr);
113
Suman Anna8841a662014-11-03 17:05:50 -0600114 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;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200119
Suman Anna8841a662014-11-03 17:05:50 -0600120 oproc->mbox = omap_mbox_request_channel(client, pdata->mbox_name);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200121 if (IS_ERR(oproc->mbox)) {
Suman Anna8841a662014-11-03 17:05:50 -0600122 ret = -EBUSY;
123 dev_err(dev, "mbox_request_channel failed: %ld\n",
124 PTR_ERR(oproc->mbox));
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200125 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 */
Suman Anna8841a662014-11-03 17:05:50 -0600135 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);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200138 goto put_mbox;
139 }
140
141 ret = pdata->device_enable(pdev);
142 if (ret) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300143 dev_err(dev, "omap_device_enable failed: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200144 goto put_mbox;
145 }
146
147 return 0;
148
149put_mbox:
Suman Anna8841a662014-11-03 17:05:50 -0600150 mbox_free_channel(oproc->mbox);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200151 return ret;
152}
153
154/* power off the remote processor */
155static int omap_rproc_stop(struct rproc *rproc)
156{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300157 struct device *dev = rproc->dev.parent;
158 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200159 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
Suman Anna8841a662014-11-03 17:05:50 -0600167 mbox_free_channel(oproc->mbox);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200168
169 return 0;
170}
171
Bhumika Goyalc008fad2017-01-01 16:13:37 +0530172static const struct rproc_ops omap_rproc_ops = {
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200173 .start = omap_rproc_start,
174 .stop = omap_rproc_stop,
175 .kick = omap_rproc_kick,
176};
177
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800178static int omap_rproc_probe(struct platform_device *pdev)
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200179{
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) {
Ohad Ben-Cohen6b039762012-05-21 16:31:12 +0300187 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200188 return ret;
189 }
190
191 rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
Anna, Suman334765f2016-08-12 18:42:22 -0500192 pdata->firmware, sizeof(*oproc));
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200193 if (!rproc)
194 return -ENOMEM;
195
196 oproc = rproc->priv;
197 oproc->rproc = rproc;
Suman Anna315491e2015-01-09 15:21:58 -0600198 /* All existing OMAP IPU and DSP processors have an MMU */
199 rproc->has_iommu = true;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200200
201 platform_set_drvdata(pdev, rproc);
202
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300203 ret = rproc_add(rproc);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200204 if (ret)
205 goto free_rproc;
206
207 return 0;
208
209free_rproc:
Bjorn Andersson433c0e02016-10-02 17:46:38 -0700210 rproc_free(rproc);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200211 return ret;
212}
213
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800214static int omap_rproc_remove(struct platform_device *pdev)
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200215{
216 struct rproc *rproc = platform_get_drvdata(pdev);
217
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300218 rproc_del(rproc);
Bjorn Andersson433c0e02016-10-02 17:46:38 -0700219 rproc_free(rproc);
Ohad Ben-Cohenc6b5a272012-07-02 11:41:16 +0300220
221 return 0;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200222}
223
224static struct platform_driver omap_rproc_driver = {
225 .probe = omap_rproc_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800226 .remove = omap_rproc_remove,
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200227 .driver = {
228 .name = "omap-rproc",
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200229 },
230};
231
Ohad Ben-Cohen63d667b2011-12-13 14:41:47 +0200232module_platform_driver(omap_rproc_driver);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200233
234MODULE_LICENSE("GPL v2");
235MODULE_DESCRIPTION("OMAP Remote Processor control driver");