Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 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> |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 30 | #include <linux/mailbox_client.h> |
Suman Anna | c869c75 | 2013-03-12 17:55:29 -0500 | [diff] [blame] | 31 | #include <linux/omap-mailbox.h> |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 32 | |
Arnd Bergmann | 2203747 | 2012-08-24 15:21:06 +0200 | [diff] [blame] | 33 | #include <linux/platform_data/remoteproc-omap.h> |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 34 | |
| 35 | #include "omap_remoteproc.h" |
| 36 | #include "remoteproc_internal.h" |
| 37 | |
| 38 | /** |
| 39 | * struct omap_rproc - omap remote processor state |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 40 | * @mbox: mailbox channel handle |
| 41 | * @client: mailbox client to request the mailbox channel |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 42 | * @rproc: rproc handle |
| 43 | */ |
| 44 | struct omap_rproc { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 45 | struct mbox_chan *mbox; |
| 46 | struct mbox_client client; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 47 | struct rproc *rproc; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * omap_rproc_mbox_callback() - inbound mailbox message handler |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 52 | * @client: mailbox client pointer used for requesting the mailbox channel |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 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 | */ |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 64 | static void omap_rproc_mbox_callback(struct mbox_client *client, void *data) |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 65 | { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 66 | struct omap_rproc *oproc = container_of(client, struct omap_rproc, |
| 67 | client); |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 68 | struct device *dev = oproc->rproc->dev.parent; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 69 | const char *name = oproc->rproc->name; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 70 | u32 msg = (u32)data; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 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-Cohen | 55f3408 | 2012-02-13 11:24:50 +0100 | [diff] [blame] | 83 | /* msg contains the index of the triggered vring */ |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 84 | if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE) |
| 85 | dev_dbg(dev, "no message was found in vqid %d\n", msg); |
| 86 | } |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* kick a virtqueue */ |
| 90 | static void omap_rproc_kick(struct rproc *rproc, int vqid) |
| 91 | { |
| 92 | struct omap_rproc *oproc = rproc->priv; |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 93 | struct device *dev = rproc->dev.parent; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 94 | int ret; |
| 95 | |
| 96 | /* send the index of the triggered virtqueue in the mailbox payload */ |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 97 | ret = mbox_send_message(oproc->mbox, (void *)vqid); |
| 98 | if (ret < 0) |
Anna, Suman | 14096c1 | 2016-08-12 18:42:23 -0500 | [diff] [blame] | 99 | dev_err(dev, "failed to send mailbox message, status = %d\n", |
| 100 | ret); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 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; |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 113 | struct device *dev = rproc->dev.parent; |
| 114 | struct platform_device *pdev = to_platform_device(dev); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 115 | struct omap_rproc_pdata *pdata = pdev->dev.platform_data; |
| 116 | int ret; |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 117 | struct mbox_client *client = &oproc->client; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 118 | |
Juan Gutierrez | 4980f46 | 2012-08-15 10:25:48 -0500 | [diff] [blame] | 119 | if (pdata->set_bootaddr) |
| 120 | pdata->set_bootaddr(rproc->bootaddr); |
| 121 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 122 | client->dev = dev; |
| 123 | client->tx_done = NULL; |
| 124 | client->rx_callback = omap_rproc_mbox_callback; |
| 125 | client->tx_block = false; |
| 126 | client->knows_txdone = false; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 127 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 128 | oproc->mbox = omap_mbox_request_channel(client, pdata->mbox_name); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 129 | if (IS_ERR(oproc->mbox)) { |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 130 | ret = -EBUSY; |
| 131 | dev_err(dev, "mbox_request_channel failed: %ld\n", |
| 132 | PTR_ERR(oproc->mbox)); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Ping the remote processor. this is only for sanity-sake; |
| 138 | * there is no functional effect whatsoever. |
| 139 | * |
| 140 | * Note that the reply will _not_ arrive immediately: this message |
| 141 | * will wait in the mailbox fifo until the remote processor is booted. |
| 142 | */ |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 143 | ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST); |
| 144 | if (ret < 0) { |
| 145 | dev_err(dev, "mbox_send_message failed: %d\n", ret); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 146 | goto put_mbox; |
| 147 | } |
| 148 | |
| 149 | ret = pdata->device_enable(pdev); |
| 150 | if (ret) { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 151 | dev_err(dev, "omap_device_enable failed: %d\n", ret); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 152 | goto put_mbox; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | put_mbox: |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 158 | mbox_free_channel(oproc->mbox); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | /* power off the remote processor */ |
| 163 | static int omap_rproc_stop(struct rproc *rproc) |
| 164 | { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 165 | struct device *dev = rproc->dev.parent; |
| 166 | struct platform_device *pdev = to_platform_device(dev); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 167 | struct omap_rproc_pdata *pdata = pdev->dev.platform_data; |
| 168 | struct omap_rproc *oproc = rproc->priv; |
| 169 | int ret; |
| 170 | |
| 171 | ret = pdata->device_shutdown(pdev); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
Suman Anna | 8841a66 | 2014-11-03 17:05:50 -0600 | [diff] [blame] | 175 | mbox_free_channel(oproc->mbox); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Bhumika Goyal | c008fad | 2017-01-01 16:13:37 +0530 | [diff] [blame] | 180 | static const struct rproc_ops omap_rproc_ops = { |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 181 | .start = omap_rproc_start, |
| 182 | .stop = omap_rproc_stop, |
| 183 | .kick = omap_rproc_kick, |
| 184 | }; |
| 185 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 186 | static int omap_rproc_probe(struct platform_device *pdev) |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 187 | { |
| 188 | struct omap_rproc_pdata *pdata = pdev->dev.platform_data; |
| 189 | struct omap_rproc *oproc; |
| 190 | struct rproc *rproc; |
| 191 | int ret; |
| 192 | |
| 193 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 194 | if (ret) { |
Ohad Ben-Cohen | 6b03976 | 2012-05-21 16:31:12 +0300 | [diff] [blame] | 195 | dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops, |
Anna, Suman | 334765f | 2016-08-12 18:42:22 -0500 | [diff] [blame] | 200 | pdata->firmware, sizeof(*oproc)); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 201 | if (!rproc) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | oproc = rproc->priv; |
| 205 | oproc->rproc = rproc; |
Suman Anna | 315491e | 2015-01-09 15:21:58 -0600 | [diff] [blame] | 206 | /* All existing OMAP IPU and DSP processors have an MMU */ |
| 207 | rproc->has_iommu = true; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 208 | |
| 209 | platform_set_drvdata(pdev, rproc); |
| 210 | |
Ohad Ben-Cohen | 160e7c8 | 2012-07-04 16:25:06 +0300 | [diff] [blame] | 211 | ret = rproc_add(rproc); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 212 | if (ret) |
| 213 | goto free_rproc; |
| 214 | |
| 215 | return 0; |
| 216 | |
| 217 | free_rproc: |
Bjorn Andersson | 433c0e0 | 2016-10-02 17:46:38 -0700 | [diff] [blame] | 218 | rproc_free(rproc); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 222 | static int omap_rproc_remove(struct platform_device *pdev) |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 223 | { |
| 224 | struct rproc *rproc = platform_get_drvdata(pdev); |
| 225 | |
Ohad Ben-Cohen | 160e7c8 | 2012-07-04 16:25:06 +0300 | [diff] [blame] | 226 | rproc_del(rproc); |
Bjorn Andersson | 433c0e0 | 2016-10-02 17:46:38 -0700 | [diff] [blame] | 227 | rproc_free(rproc); |
Ohad Ben-Cohen | c6b5a27 | 2012-07-02 11:41:16 +0300 | [diff] [blame] | 228 | |
| 229 | return 0; |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static struct platform_driver omap_rproc_driver = { |
| 233 | .probe = omap_rproc_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 234 | .remove = omap_rproc_remove, |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 235 | .driver = { |
| 236 | .name = "omap-rproc", |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 237 | }, |
| 238 | }; |
| 239 | |
Ohad Ben-Cohen | 63d667b | 2011-12-13 14:41:47 +0200 | [diff] [blame] | 240 | module_platform_driver(omap_rproc_driver); |
Ohad Ben-Cohen | 34ed5a3 | 2011-10-20 18:53:35 +0200 | [diff] [blame] | 241 | |
| 242 | MODULE_LICENSE("GPL v2"); |
| 243 | MODULE_DESCRIPTION("OMAP Remote Processor control driver"); |