Linux 3.11-rc3
[cris-mirror.git] / drivers / remoteproc / ste_modem_rproc.c
blob1ec39a4c0b3edbd9a6c64ef95e22649ab6ca7de7
1 /*
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
5 */
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 */
26 struct sproc {
27 struct rproc *rproc;
28 struct ste_modem_device *mdev;
29 int error;
30 void *fw_addr;
31 size_t fw_size;
32 dma_addr_t fw_dma_addr;
35 /* STE-Modem firmware entry */
36 struct ste_toc_entry {
37 __le32 start;
38 __le32 size;
39 __le32 flags;
40 __le32 entry_point;
41 __le32 load_addr;
42 char name[12];
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
50 * name.
52 struct ste_toc {
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);
63 return 0;
66 /* Find the entry for resource table in the Table of Content */
67 static const struct ste_toc_entry *sproc_find_rsc_entry(const void *data)
69 int i;
70 const struct ste_toc *toc;
71 toc = data;
73 /* Search the table for the resource table */
74 for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
75 toc->table[i].start != 0xffffffff; i++) {
76 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
77 sizeof(toc->table[i].name)))
78 return &toc->table[i];
81 return NULL;
84 /* Find the resource table inside the remote processor's firmware. */
85 static struct resource_table *
86 sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
87 int *tablesz)
89 struct sproc *sproc = rproc->priv;
90 struct resource_table *table;
91 const struct ste_toc_entry *entry;
93 if (!fw)
94 return NULL;
96 entry = sproc_find_rsc_entry(fw->data);
97 if (!entry) {
98 sproc_err(sproc, "resource table not found in fw\n");
99 return NULL;
102 table = (void *)(fw->data + entry->start);
104 /* sanity check size and offset of resource table */
105 if (entry->start > SPROC_FW_SIZE ||
106 entry->size > SPROC_FW_SIZE ||
107 fw->size > SPROC_FW_SIZE ||
108 entry->start + entry->size > fw->size ||
109 sizeof(struct resource_table) > entry->size) {
110 sproc_err(sproc, "bad size of fw or resource table\n");
111 return NULL;
114 /* we don't support any version beyond the first */
115 if (table->ver != 1) {
116 sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
117 return NULL;
120 /* make sure reserved bytes are zeroes */
121 if (table->reserved[0] || table->reserved[1]) {
122 sproc_err(sproc, "non zero reserved bytes\n");
123 return NULL;
126 /* make sure the offsets array isn't truncated */
127 if (table->num > SPROC_MAX_TOC_ENTRIES ||
128 table->num * sizeof(table->offset[0]) +
129 sizeof(struct resource_table) > entry->size) {
130 sproc_err(sproc, "resource table incomplete\n");
131 return NULL;
134 /* If the fw size has grown, release the previous fw allocation */
135 if (SPROC_FW_SIZE < fw->size) {
136 sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
137 SPROC_FW_SIZE, fw->size);
138 return NULL;
141 sproc->fw_size = fw->size;
142 *tablesz = entry->size;
144 return table;
147 /* Find the resource table inside the remote processor's firmware. */
148 static struct resource_table *
149 sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
151 struct sproc *sproc = rproc->priv;
152 const struct ste_toc_entry *entry;
154 if (!fw || !sproc->fw_addr)
155 return NULL;
157 entry = sproc_find_rsc_entry(sproc->fw_addr);
158 if (!entry) {
159 sproc_err(sproc, "resource table not found in fw\n");
160 return NULL;
163 return sproc->fw_addr + entry->start;
166 /* STE modem firmware handler operations */
167 const struct rproc_fw_ops sproc_fw_ops = {
168 .load = sproc_load_segments,
169 .find_rsc_table = sproc_find_rsc_table,
170 .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
173 /* Kick the modem with specified notification id */
174 static void sproc_kick(struct rproc *rproc, int vqid)
176 struct sproc *sproc = rproc->priv;
178 sproc_dbg(sproc, "kick vqid:%d\n", vqid);
181 * We need different notification IDs for RX and TX so add
182 * an offset on TX notification IDs.
184 sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
187 /* Received a kick from a modem, kick the virtqueue */
188 static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
190 struct sproc *sproc = mdev->drv_data;
192 if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
193 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
196 struct ste_modem_dev_cb sproc_dev_cb = {
197 .kick = sproc_kick_callback,
200 /* Start the STE modem */
201 static int sproc_start(struct rproc *rproc)
203 struct sproc *sproc = rproc->priv;
204 int i, err;
206 sproc_dbg(sproc, "start ste-modem\n");
208 /* Sanity test the max_notifyid */
209 if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
210 sproc_err(sproc, "Notification IDs too high:%d\n",
211 rproc->max_notifyid);
212 return -EINVAL;
215 /* Subscribe to notifications */
216 for (i = 0; i <= rproc->max_notifyid; i++) {
217 err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
218 if (err) {
219 sproc_err(sproc,
220 "subscription of kicks failed:%d\n", err);
221 return err;
225 /* Request modem start-up*/
226 return sproc->mdev->ops.power(sproc->mdev, true);
229 /* Stop the STE modem */
230 static int sproc_stop(struct rproc *rproc)
232 struct sproc *sproc = rproc->priv;
233 sproc_dbg(sproc, "stop ste-modem\n");
235 return sproc->mdev->ops.power(sproc->mdev, false);
238 static struct rproc_ops sproc_ops = {
239 .start = sproc_start,
240 .stop = sproc_stop,
241 .kick = sproc_kick,
244 /* STE modem device is unregistered */
245 static int sproc_drv_remove(struct platform_device *pdev)
247 struct ste_modem_device *mdev =
248 container_of(pdev, struct ste_modem_device, pdev);
249 struct sproc *sproc = mdev->drv_data;
251 sproc_dbg(sproc, "remove ste-modem\n");
253 /* Reset device callback functions */
254 sproc->mdev->ops.setup(sproc->mdev, NULL);
256 /* Unregister as remoteproc device */
257 rproc_del(sproc->rproc);
258 dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
259 sproc->fw_addr, sproc->fw_dma_addr);
260 rproc_put(sproc->rproc);
262 mdev->drv_data = NULL;
264 return 0;
267 /* Handle probe of a modem device */
268 static int sproc_probe(struct platform_device *pdev)
270 struct ste_modem_device *mdev =
271 container_of(pdev, struct ste_modem_device, pdev);
272 struct sproc *sproc;
273 struct rproc *rproc;
274 int err;
276 dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
278 if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
279 !mdev->ops.power) {
280 dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
281 return -EINVAL;
284 rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
285 SPROC_MODEM_FIRMWARE, sizeof(*sproc));
286 if (!rproc)
287 return -ENOMEM;
289 sproc = rproc->priv;
290 sproc->mdev = mdev;
291 sproc->rproc = rproc;
292 mdev->drv_data = sproc;
294 /* Provide callback functions to modem device */
295 sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
297 /* Set the STE-modem specific firmware handler */
298 rproc->fw_ops = &sproc_fw_ops;
301 * STE-modem requires the firmware to be located
302 * at the start of the shared memory region. So we need to
303 * reserve space for firmware at the start.
305 sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
306 &sproc->fw_dma_addr,
307 GFP_KERNEL);
308 if (!sproc->fw_addr) {
309 sproc_err(sproc, "Cannot allocate memory for fw\n");
310 err = -ENOMEM;
311 goto free_rproc;
314 /* Register as a remoteproc device */
315 err = rproc_add(rproc);
316 if (err)
317 goto free_mem;
319 return 0;
321 free_mem:
322 dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
323 sproc->fw_addr, sproc->fw_dma_addr);
324 free_rproc:
325 /* Reset device data upon error */
326 mdev->drv_data = NULL;
327 rproc_put(rproc);
328 return err;
331 static struct platform_driver sproc_driver = {
332 .driver = {
333 .name = SPROC_MODEM_NAME,
334 .owner = THIS_MODULE,
336 .probe = sproc_probe,
337 .remove = sproc_drv_remove,
340 module_platform_driver(sproc_driver);
341 MODULE_LICENSE("GPL v2");
342 MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");