blob: 53dc17bdd54e0c3ef94f03a345e2d31e5a5bdfe6 [file] [log] [blame]
Sjur Brændelandec4d02d2012-09-20 18:32:56 +02001/*
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 */
26struct 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 */
36struct 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 */
52struct ste_toc {
53 struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
54};
55
56/* Loads the firmware to shared memory. */
57static 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 */
Sjur Brændelande4b514142013-02-21 18:15:35 +010067static const struct ste_toc_entry *sproc_find_rsc_entry(const void *data)
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020068{
69 int i;
Suman Anna9aa1cfc2015-02-27 17:18:21 -060070 const struct ste_toc *toc = data;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020071
72 /* Search the table for the resource table */
73 for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
74 toc->table[i].start != 0xffffffff; i++) {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020075 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
Sjur Brændelande4b514142013-02-21 18:15:35 +010076 sizeof(toc->table[i].name)))
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020077 return &toc->table[i];
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020078 }
79
80 return NULL;
81}
82
83/* Find the resource table inside the remote processor's firmware. */
84static struct resource_table *
85sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
86 int *tablesz)
87{
88 struct sproc *sproc = rproc->priv;
89 struct resource_table *table;
Sjur Brændelande4b514142013-02-21 18:15:35 +010090 const struct ste_toc_entry *entry;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020091
Sjur Brændelande4b514142013-02-21 18:15:35 +010092 if (!fw)
93 return NULL;
94
95 entry = sproc_find_rsc_entry(fw->data);
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020096 if (!entry) {
97 sproc_err(sproc, "resource table not found in fw\n");
98 return NULL;
99 }
100
101 table = (void *)(fw->data + entry->start);
102
103 /* sanity check size and offset of resource table */
104 if (entry->start > SPROC_FW_SIZE ||
105 entry->size > SPROC_FW_SIZE ||
106 fw->size > SPROC_FW_SIZE ||
107 entry->start + entry->size > fw->size ||
108 sizeof(struct resource_table) > entry->size) {
109 sproc_err(sproc, "bad size of fw or resource table\n");
110 return NULL;
111 }
112
113 /* we don't support any version beyond the first */
114 if (table->ver != 1) {
115 sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
116 return NULL;
117 }
118
119 /* make sure reserved bytes are zeroes */
120 if (table->reserved[0] || table->reserved[1]) {
121 sproc_err(sproc, "non zero reserved bytes\n");
122 return NULL;
123 }
124
125 /* make sure the offsets array isn't truncated */
126 if (table->num > SPROC_MAX_TOC_ENTRIES ||
127 table->num * sizeof(table->offset[0]) +
128 sizeof(struct resource_table) > entry->size) {
129 sproc_err(sproc, "resource table incomplete\n");
130 return NULL;
131 }
132
133 /* If the fw size has grown, release the previous fw allocation */
134 if (SPROC_FW_SIZE < fw->size) {
135 sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
136 SPROC_FW_SIZE, fw->size);
137 return NULL;
138 }
139
140 sproc->fw_size = fw->size;
141 *tablesz = entry->size;
142
143 return table;
144}
145
Sjur Brændelande4b514142013-02-21 18:15:35 +0100146/* Find the resource table inside the remote processor's firmware. */
147static struct resource_table *
148sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
149{
150 struct sproc *sproc = rproc->priv;
151 const struct ste_toc_entry *entry;
152
153 if (!fw || !sproc->fw_addr)
154 return NULL;
155
156 entry = sproc_find_rsc_entry(sproc->fw_addr);
157 if (!entry) {
158 sproc_err(sproc, "resource table not found in fw\n");
159 return NULL;
160 }
161
162 return sproc->fw_addr + entry->start;
163}
164
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200165/* STE modem firmware handler operations */
Jingoo Hanbd88acb2013-08-12 17:20:22 +0900166static const struct rproc_fw_ops sproc_fw_ops = {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200167 .load = sproc_load_segments,
168 .find_rsc_table = sproc_find_rsc_table,
Sjur Brændelande4b514142013-02-21 18:15:35 +0100169 .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200170};
171
172/* Kick the modem with specified notification id */
173static void sproc_kick(struct rproc *rproc, int vqid)
174{
175 struct sproc *sproc = rproc->priv;
176
177 sproc_dbg(sproc, "kick vqid:%d\n", vqid);
178
179 /*
180 * We need different notification IDs for RX and TX so add
181 * an offset on TX notification IDs.
182 */
183 sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
184}
185
186/* Received a kick from a modem, kick the virtqueue */
187static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
188{
189 struct sproc *sproc = mdev->drv_data;
190
191 if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
192 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
193}
194
Jingoo Hanbd88acb2013-08-12 17:20:22 +0900195static struct ste_modem_dev_cb sproc_dev_cb = {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200196 .kick = sproc_kick_callback,
197};
198
199/* Start the STE modem */
200static int sproc_start(struct rproc *rproc)
201{
202 struct sproc *sproc = rproc->priv;
203 int i, err;
204
205 sproc_dbg(sproc, "start ste-modem\n");
206
207 /* Sanity test the max_notifyid */
208 if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
209 sproc_err(sproc, "Notification IDs too high:%d\n",
210 rproc->max_notifyid);
211 return -EINVAL;
212 }
213
214 /* Subscribe to notifications */
Sjur Brændelandba7290e2013-02-21 18:15:37 +0100215 for (i = 0; i <= rproc->max_notifyid; i++) {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200216 err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
217 if (err) {
218 sproc_err(sproc,
219 "subscription of kicks failed:%d\n", err);
220 return err;
221 }
222 }
223
224 /* Request modem start-up*/
225 return sproc->mdev->ops.power(sproc->mdev, true);
226}
227
228/* Stop the STE modem */
229static int sproc_stop(struct rproc *rproc)
230{
231 struct sproc *sproc = rproc->priv;
Suman Anna9aa1cfc2015-02-27 17:18:21 -0600232
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200233 sproc_dbg(sproc, "stop ste-modem\n");
234
235 return sproc->mdev->ops.power(sproc->mdev, false);
236}
237
238static struct rproc_ops sproc_ops = {
239 .start = sproc_start,
240 .stop = sproc_stop,
241 .kick = sproc_kick,
242};
243
244/* STE modem device is unregistered */
245static int sproc_drv_remove(struct platform_device *pdev)
246{
247 struct ste_modem_device *mdev =
248 container_of(pdev, struct ste_modem_device, pdev);
249 struct sproc *sproc = mdev->drv_data;
250
251 sproc_dbg(sproc, "remove ste-modem\n");
252
253 /* Reset device callback functions */
254 sproc->mdev->ops.setup(sproc->mdev, NULL);
255
256 /* Unregister as remoteproc device */
257 rproc_del(sproc->rproc);
Dmitry Tarnyagin1cd425b2013-02-10 12:39:04 +0100258 dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
259 sproc->fw_addr, sproc->fw_dma_addr);
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200260 rproc_put(sproc->rproc);
261
262 mdev->drv_data = NULL;
263
264 return 0;
265}
266
267/* Handle probe of a modem device */
268static int sproc_probe(struct platform_device *pdev)
269{
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;
275
276 dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
277
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;
282 }
283
284 rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
285 SPROC_MODEM_FIRMWARE, sizeof(*sproc));
286 if (!rproc)
287 return -ENOMEM;
288
289 sproc = rproc->priv;
290 sproc->mdev = mdev;
291 sproc->rproc = rproc;
Suman Anna315491e2015-01-09 15:21:58 -0600292 rproc->has_iommu = false;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200293 mdev->drv_data = sproc;
294
295 /* Provide callback functions to modem device */
296 sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
297
298 /* Set the STE-modem specific firmware handler */
299 rproc->fw_ops = &sproc_fw_ops;
300
301 /*
302 * STE-modem requires the firmware to be located
303 * at the start of the shared memory region. So we need to
304 * reserve space for firmware at the start.
305 */
306 sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
307 &sproc->fw_dma_addr,
308 GFP_KERNEL);
309 if (!sproc->fw_addr) {
310 sproc_err(sproc, "Cannot allocate memory for fw\n");
311 err = -ENOMEM;
312 goto free_rproc;
313 }
314
315 /* Register as a remoteproc device */
316 err = rproc_add(rproc);
317 if (err)
Dmitry Tarnyagin1cd425b2013-02-10 12:39:04 +0100318 goto free_mem;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200319
320 return 0;
321
Dmitry Tarnyagin1cd425b2013-02-10 12:39:04 +0100322free_mem:
323 dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
324 sproc->fw_addr, sproc->fw_dma_addr);
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200325free_rproc:
326 /* Reset device data upon error */
327 mdev->drv_data = NULL;
328 rproc_put(rproc);
329 return err;
330}
331
332static struct platform_driver sproc_driver = {
333 .driver = {
334 .name = SPROC_MODEM_NAME,
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200335 },
336 .probe = sproc_probe,
337 .remove = sproc_drv_remove,
338};
339
340module_platform_driver(sproc_driver);
341MODULE_LICENSE("GPL v2");
342MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");