blob: 51689721ea7a1c20d17ea6a7e104ee97025e52ce [file] [log] [blame]
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +02001/*
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>
Suman Annac869c752013-03-12 17:55:29 -050030#include <linux/omap-mailbox.h>
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020031
Arnd Bergmann22037472012-08-24 15:21:06 +020032#include <linux/platform_data/remoteproc-omap.h>
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020033
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 */
43struct 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 */
64static 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);
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030069 struct device *dev = oproc->rproc->dev.parent;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020070 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:
Ohad Ben-Cohen55f34082012-02-13 11:24:50 +010083 /* msg contains the index of the triggered vring */
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020084 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 */
92static void omap_rproc_kick(struct rproc *rproc, int vqid)
93{
94 struct omap_rproc *oproc = rproc->priv;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030095 struct device *dev = rproc->dev.parent;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +020096 int ret;
97
98 /* send the index of the triggered virtqueue in the mailbox payload */
99 ret = omap_mbox_msg_send(oproc->mbox, vqid);
100 if (ret)
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300101 dev_err(dev, "omap_mbox_msg_send failed: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200102}
103
104/*
105 * Power up the remote processor.
106 *
107 * This function will be invoked only after the firmware for this rproc
108 * was loaded, parsed successfully, and all of its resource requirements
109 * were met.
110 */
111static int omap_rproc_start(struct rproc *rproc)
112{
113 struct omap_rproc *oproc = rproc->priv;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300114 struct device *dev = rproc->dev.parent;
115 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200116 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
117 int ret;
118
Juan Gutierrez4980f462012-08-15 10:25:48 -0500119 if (pdata->set_bootaddr)
120 pdata->set_bootaddr(rproc->bootaddr);
121
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200122 oproc->nb.notifier_call = omap_rproc_mbox_callback;
123
124 /* every omap rproc is assigned a mailbox instance for messaging */
125 oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb);
126 if (IS_ERR(oproc->mbox)) {
127 ret = PTR_ERR(oproc->mbox);
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300128 dev_err(dev, "omap_mbox_get failed: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200129 return ret;
130 }
131
132 /*
133 * Ping the remote processor. this is only for sanity-sake;
134 * there is no functional effect whatsoever.
135 *
136 * Note that the reply will _not_ arrive immediately: this message
137 * will wait in the mailbox fifo until the remote processor is booted.
138 */
139 ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST);
140 if (ret) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300141 dev_err(dev, "omap_mbox_get failed: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200142 goto put_mbox;
143 }
144
145 ret = pdata->device_enable(pdev);
146 if (ret) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300147 dev_err(dev, "omap_device_enable failed: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200148 goto put_mbox;
149 }
150
151 return 0;
152
153put_mbox:
154 omap_mbox_put(oproc->mbox, &oproc->nb);
155 return ret;
156}
157
158/* power off the remote processor */
159static int omap_rproc_stop(struct rproc *rproc)
160{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300161 struct device *dev = rproc->dev.parent;
162 struct platform_device *pdev = to_platform_device(dev);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200163 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
164 struct omap_rproc *oproc = rproc->priv;
165 int ret;
166
167 ret = pdata->device_shutdown(pdev);
168 if (ret)
169 return ret;
170
171 omap_mbox_put(oproc->mbox, &oproc->nb);
172
173 return 0;
174}
175
176static struct rproc_ops omap_rproc_ops = {
177 .start = omap_rproc_start,
178 .stop = omap_rproc_stop,
179 .kick = omap_rproc_kick,
180};
181
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800182static int omap_rproc_probe(struct platform_device *pdev)
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200183{
184 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
185 struct omap_rproc *oproc;
186 struct rproc *rproc;
187 int ret;
188
189 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
190 if (ret) {
Ohad Ben-Cohen6b039762012-05-21 16:31:12 +0300191 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200192 return ret;
193 }
194
195 rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
196 pdata->firmware, sizeof(*oproc));
197 if (!rproc)
198 return -ENOMEM;
199
200 oproc = rproc->priv;
201 oproc->rproc = rproc;
202
203 platform_set_drvdata(pdev, rproc);
204
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300205 ret = rproc_add(rproc);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200206 if (ret)
207 goto free_rproc;
208
209 return 0;
210
211free_rproc:
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300212 rproc_put(rproc);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200213 return ret;
214}
215
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800216static int omap_rproc_remove(struct platform_device *pdev)
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200217{
218 struct rproc *rproc = platform_get_drvdata(pdev);
219
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300220 rproc_del(rproc);
221 rproc_put(rproc);
Ohad Ben-Cohenc6b5a272012-07-02 11:41:16 +0300222
223 return 0;
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200224}
225
226static struct platform_driver omap_rproc_driver = {
227 .probe = omap_rproc_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800228 .remove = omap_rproc_remove,
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200229 .driver = {
230 .name = "omap-rproc",
231 .owner = THIS_MODULE,
232 },
233};
234
Ohad Ben-Cohen63d667b2011-12-13 14:41:47 +0200235module_platform_driver(omap_rproc_driver);
Ohad Ben-Cohen34ed5a32011-10-20 18:53:35 +0200236
237MODULE_LICENSE("GPL v2");
238MODULE_DESCRIPTION("OMAP Remote Processor control driver");