]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/remoteproc/ste_modem_rproc.c
Merge remote-tracking branch 'regulator/topic/max8952' into v3.9-rc8
[karo-tx-linux.git] / drivers / remoteproc / ste_modem_rproc.c
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  */
6
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"
12
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"
19
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__)
24
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;
33 };
34
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];
43 };
44
45 /*
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.
51  */
52 struct ste_toc {
53         struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
54 };
55
56 /* Loads the firmware to shared memory. */
57 static int sproc_load_segments(struct rproc *rproc, const struct firmware *fw)
58 {
59         struct sproc *sproc = rproc->priv;
60
61         memcpy(sproc->fw_addr, fw->data, fw->size);
62
63         return 0;
64 }
65
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)
68 {
69         int i;
70         struct ste_toc *toc;
71
72         if (!fw)
73                 return NULL;
74
75         toc = (void *)fw->data;
76
77         /* Search the table for the resource table */
78         for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
79                     toc->table[i].start != 0xffffffff; i++) {
80
81                 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
82                              sizeof(toc->table[i].name))) {
83                         if (toc->table[i].start > fw->size)
84                                 return NULL;
85                         return &toc->table[i];
86                 }
87         }
88
89         return NULL;
90 }
91
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,
95                      int *tablesz)
96 {
97         struct sproc *sproc = rproc->priv;
98         struct resource_table *table;
99         struct ste_toc_entry *entry;
100
101         entry = sproc_find_rsc_entry(fw);
102         if (!entry) {
103                 sproc_err(sproc, "resource table not found in fw\n");
104                 return NULL;
105         }
106
107         table = (void *)(fw->data + entry->start);
108
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");
116                 return NULL;
117         }
118
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);
122                 return NULL;
123         }
124
125         /* make sure reserved bytes are zeroes */
126         if (table->reserved[0] || table->reserved[1]) {
127                 sproc_err(sproc, "non zero reserved bytes\n");
128                 return NULL;
129         }
130
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");
136                 return NULL;
137         }
138
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);
143                 return NULL;
144         }
145
146         sproc->fw_size = fw->size;
147         *tablesz = entry->size;
148
149         return table;
150 }
151
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,
156 };
157
158 /* Kick the modem with specified notification id */
159 static void sproc_kick(struct rproc *rproc, int vqid)
160 {
161         struct sproc *sproc = rproc->priv;
162
163         sproc_dbg(sproc, "kick vqid:%d\n", vqid);
164
165         /*
166          * We need different notification IDs for RX and TX so add
167          * an offset on TX notification IDs.
168          */
169         sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
170 }
171
172 /* Received a kick from a modem, kick the virtqueue */
173 static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
174 {
175         struct sproc *sproc = mdev->drv_data;
176
177         if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
178                 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
179 }
180
181 struct ste_modem_dev_cb sproc_dev_cb = {
182         .kick = sproc_kick_callback,
183 };
184
185 /* Start the STE modem */
186 static int sproc_start(struct rproc *rproc)
187 {
188         struct sproc *sproc = rproc->priv;
189         int i, err;
190
191         sproc_dbg(sproc, "start ste-modem\n");
192
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);
197                 return -EINVAL;
198         }
199
200         /* Subscribe to notifications */
201         for (i = 0; i < rproc->max_notifyid; i++) {
202                 err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
203                 if (err) {
204                         sproc_err(sproc,
205                                   "subscription of kicks failed:%d\n", err);
206                         return err;
207                 }
208         }
209
210         /* Request modem start-up*/
211         return sproc->mdev->ops.power(sproc->mdev, true);
212 }
213
214 /* Stop the STE modem */
215 static int sproc_stop(struct rproc *rproc)
216 {
217         struct sproc *sproc = rproc->priv;
218         sproc_dbg(sproc, "stop ste-modem\n");
219
220         return sproc->mdev->ops.power(sproc->mdev, false);
221 }
222
223 static struct rproc_ops sproc_ops = {
224         .start          = sproc_start,
225         .stop           = sproc_stop,
226         .kick           = sproc_kick,
227 };
228
229 /* STE modem device is unregistered */
230 static int sproc_drv_remove(struct platform_device *pdev)
231 {
232         struct ste_modem_device *mdev =
233                 container_of(pdev, struct ste_modem_device, pdev);
234         struct sproc *sproc = mdev->drv_data;
235
236         sproc_dbg(sproc, "remove ste-modem\n");
237
238         /* Reset device callback functions */
239         sproc->mdev->ops.setup(sproc->mdev, NULL);
240
241         /* Unregister as remoteproc device */
242         rproc_del(sproc->rproc);
243         dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
244                           sproc->fw_addr, sproc->fw_dma_addr);
245         rproc_put(sproc->rproc);
246
247         mdev->drv_data = NULL;
248
249         return 0;
250 }
251
252 /* Handle probe of a modem device */
253 static int sproc_probe(struct platform_device *pdev)
254 {
255         struct ste_modem_device *mdev =
256                 container_of(pdev, struct ste_modem_device, pdev);
257         struct sproc *sproc;
258         struct rproc *rproc;
259         int err;
260
261         dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
262
263         if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
264             !mdev->ops.power) {
265                 dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
266                 return -EINVAL;
267         }
268
269         rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
270                             SPROC_MODEM_FIRMWARE, sizeof(*sproc));
271         if (!rproc)
272                 return -ENOMEM;
273
274         sproc = rproc->priv;
275         sproc->mdev = mdev;
276         sproc->rproc = rproc;
277         mdev->drv_data = sproc;
278
279         /* Provide callback functions to modem device */
280         sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
281
282         /* Set the STE-modem specific firmware handler */
283         rproc->fw_ops = &sproc_fw_ops;
284
285         /*
286          * STE-modem requires the firmware to be located
287          * at the start of the shared memory region. So we need to
288          * reserve space for firmware at the start.
289          */
290         sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
291                                             &sproc->fw_dma_addr,
292                                             GFP_KERNEL);
293         if (!sproc->fw_addr) {
294                 sproc_err(sproc, "Cannot allocate memory for fw\n");
295                 err = -ENOMEM;
296                 goto free_rproc;
297         }
298
299         /* Register as a remoteproc device */
300         err = rproc_add(rproc);
301         if (err)
302                 goto free_mem;
303
304         return 0;
305
306 free_mem:
307         dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
308                           sproc->fw_addr, sproc->fw_dma_addr);
309 free_rproc:
310         /* Reset device data upon error */
311         mdev->drv_data = NULL;
312         rproc_put(rproc);
313         return err;
314 }
315
316 static struct platform_driver sproc_driver = {
317         .driver = {
318                 .name   = SPROC_MODEM_NAME,
319                 .owner  = THIS_MODULE,
320         },
321         .probe  = sproc_probe,
322         .remove = sproc_drv_remove,
323 };
324
325 module_platform_driver(sproc_driver);
326 MODULE_LICENSE("GPL v2");
327 MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");