2 * Copyright (C) ST-Ericsson AB 2012
3 * Author: Sjur Brændeland <sjur.brandeland@stericsson.com>
4 * License terms: GNU General Public License (GPL), version 2
7 #include <linux/module.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/remoteproc.h>
10 #include <linux/ste_modem_shm.h>
11 #include "remoteproc_internal.h"
13 #define SPROC_FW_SIZE (50 * 4096)
14 #define SPROC_MAX_TOC_ENTRIES 32
15 #define SPROC_MAX_NOTIFY_ID 14
16 #define SPROC_RESOURCE_NAME "rsc-table"
17 #define SPROC_MODEM_NAME "ste-modem"
18 #define SPROC_MODEM_FIRMWARE SPROC_MODEM_NAME "-fw.bin"
20 #define sproc_dbg(sproc, fmt, ...) \
21 dev_dbg(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
22 #define sproc_err(sproc, fmt, ...) \
23 dev_err(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
25 /* STE-modem control structure */
28 struct ste_modem_device *mdev;
32 dma_addr_t fw_dma_addr;
35 /* STE-Modem firmware entry */
36 struct ste_toc_entry {
46 * The Table Of Content is located at the start of the firmware image and
47 * at offset zero in the shared memory region. The resource table typically
48 * contains the initial boot image (boot strap) and other information elements
49 * such as remoteproc resource table. Each entry is identified by a unique
53 struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
56 /* Loads the firmware to shared memory. */
57 static int sproc_load_segments(struct rproc *rproc, const struct firmware *fw)
59 struct sproc *sproc = rproc->priv;
61 memcpy(sproc->fw_addr, fw->data, fw->size);
66 /* Find the entry for resource table in the Table of Content */
67 static struct ste_toc_entry *sproc_find_rsc_entry(const struct firmware *fw)
75 toc = (void *)fw->data;
77 /* Search the table for the resource table */
78 for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
79 toc->table[i].start != 0xffffffff; i++) {
81 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
82 sizeof(toc->table[i].name))) {
83 if (toc->table[i].start > fw->size)
85 return &toc->table[i];
92 /* Find the resource table inside the remote processor's firmware. */
93 static struct resource_table *
94 sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
97 struct sproc *sproc = rproc->priv;
98 struct resource_table *table;
99 struct ste_toc_entry *entry;
101 entry = sproc_find_rsc_entry(fw);
103 sproc_err(sproc, "resource table not found in fw\n");
107 table = (void *)(fw->data + entry->start);
109 /* sanity check size and offset of resource table */
110 if (entry->start > SPROC_FW_SIZE ||
111 entry->size > SPROC_FW_SIZE ||
112 fw->size > SPROC_FW_SIZE ||
113 entry->start + entry->size > fw->size ||
114 sizeof(struct resource_table) > entry->size) {
115 sproc_err(sproc, "bad size of fw or resource table\n");
119 /* we don't support any version beyond the first */
120 if (table->ver != 1) {
121 sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
125 /* make sure reserved bytes are zeroes */
126 if (table->reserved[0] || table->reserved[1]) {
127 sproc_err(sproc, "non zero reserved bytes\n");
131 /* make sure the offsets array isn't truncated */
132 if (table->num > SPROC_MAX_TOC_ENTRIES ||
133 table->num * sizeof(table->offset[0]) +
134 sizeof(struct resource_table) > entry->size) {
135 sproc_err(sproc, "resource table incomplete\n");
139 /* If the fw size has grown, release the previous fw allocation */
140 if (SPROC_FW_SIZE < fw->size) {
141 sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
142 SPROC_FW_SIZE, fw->size);
146 sproc->fw_size = fw->size;
147 *tablesz = entry->size;
152 /* STE modem firmware handler operations */
153 const struct rproc_fw_ops sproc_fw_ops = {
154 .load = sproc_load_segments,
155 .find_rsc_table = sproc_find_rsc_table,
158 /* Kick the modem with specified notification id */
159 static void sproc_kick(struct rproc *rproc, int vqid)
161 struct sproc *sproc = rproc->priv;
163 sproc_dbg(sproc, "kick vqid:%d\n", vqid);
166 * We need different notification IDs for RX and TX so add
167 * an offset on TX notification IDs.
169 sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
172 /* Received a kick from a modem, kick the virtqueue */
173 static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
175 struct sproc *sproc = mdev->drv_data;
177 if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
178 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
181 struct ste_modem_dev_cb sproc_dev_cb = {
182 .kick = sproc_kick_callback,
185 /* Start the STE modem */
186 static int sproc_start(struct rproc *rproc)
188 struct sproc *sproc = rproc->priv;
191 sproc_dbg(sproc, "start ste-modem\n");
193 /* Sanity test the max_notifyid */
194 if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
195 sproc_err(sproc, "Notification IDs too high:%d\n",
196 rproc->max_notifyid);
200 /* Subscribe to notifications */
201 for (i = 0; i < rproc->max_notifyid; i++) {
202 err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
205 "subscription of kicks failed:%d\n", err);
210 /* Request modem start-up*/
211 return sproc->mdev->ops.power(sproc->mdev, true);
214 /* Stop the STE modem */
215 static int sproc_stop(struct rproc *rproc)
217 struct sproc *sproc = rproc->priv;
218 sproc_dbg(sproc, "stop ste-modem\n");
220 return sproc->mdev->ops.power(sproc->mdev, false);
223 static struct rproc_ops sproc_ops = {
224 .start = sproc_start,
229 /* STE modem device is unregistered */
230 static int sproc_drv_remove(struct platform_device *pdev)
232 struct ste_modem_device *mdev =
233 container_of(pdev, struct ste_modem_device, pdev);
234 struct sproc *sproc = mdev->drv_data;
236 sproc_dbg(sproc, "remove ste-modem\n");
238 /* Reset device callback functions */
239 sproc->mdev->ops.setup(sproc->mdev, NULL);
241 /* Unregister as remoteproc device */
242 rproc_del(sproc->rproc);
243 rproc_put(sproc->rproc);
245 mdev->drv_data = NULL;
250 /* Handle probe of a modem device */
251 static int sproc_probe(struct platform_device *pdev)
253 struct ste_modem_device *mdev =
254 container_of(pdev, struct ste_modem_device, pdev);
259 dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
261 if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
263 dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
267 rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
268 SPROC_MODEM_FIRMWARE, sizeof(*sproc));
274 sproc->rproc = rproc;
275 mdev->drv_data = sproc;
277 /* Provide callback functions to modem device */
278 sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
280 /* Set the STE-modem specific firmware handler */
281 rproc->fw_ops = &sproc_fw_ops;
284 * STE-modem requires the firmware to be located
285 * at the start of the shared memory region. So we need to
286 * reserve space for firmware at the start.
288 sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
291 if (!sproc->fw_addr) {
292 sproc_err(sproc, "Cannot allocate memory for fw\n");
297 /* Register as a remoteproc device */
298 err = rproc_add(rproc);
305 /* Reset device data upon error */
306 mdev->drv_data = NULL;
311 static struct platform_driver sproc_driver = {
313 .name = SPROC_MODEM_NAME,
314 .owner = THIS_MODULE,
316 .probe = sproc_probe,
317 .remove = sproc_drv_remove,
320 module_platform_driver(sproc_driver);
321 MODULE_LICENSE("GPL v2");
322 MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");