blob: c4ac9104dd8e4ec1c866e1247926cce7e4f9d24a [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ændelande4b51412013-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;
Sjur Brændelande4b51412013-02-21 18:15:35 +010070 const struct ste_toc *toc;
71 toc = data;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020072
73 /* Search the table for the resource table */
74 for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
75 toc->table[i].start != 0xffffffff; i++) {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020076 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
Sjur Brændelande4b51412013-02-21 18:15:35 +010077 sizeof(toc->table[i].name)))
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020078 return &toc->table[i];
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020079 }
80
81 return NULL;
82}
83
84/* Find the resource table inside the remote processor's firmware. */
85static struct resource_table *
86sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
87 int *tablesz)
88{
89 struct sproc *sproc = rproc->priv;
90 struct resource_table *table;
Sjur Brændelande4b51412013-02-21 18:15:35 +010091 const struct ste_toc_entry *entry;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020092
Sjur Brændelande4b51412013-02-21 18:15:35 +010093 if (!fw)
94 return NULL;
95
96 entry = sproc_find_rsc_entry(fw->data);
Sjur Brændelandec4d02d2012-09-20 18:32:56 +020097 if (!entry) {
98 sproc_err(sproc, "resource table not found in fw\n");
99 return NULL;
100 }
101
102 table = (void *)(fw->data + entry->start);
103
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;
112 }
113
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;
118 }
119
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;
124 }
125
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;
132 }
133
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;
139 }
140
141 sproc->fw_size = fw->size;
142 *tablesz = entry->size;
143
144 return table;
145}
146
Sjur Brændelande4b51412013-02-21 18:15:35 +0100147/* Find the resource table inside the remote processor's firmware. */
148static struct resource_table *
149sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
150{
151 struct sproc *sproc = rproc->priv;
152 const struct ste_toc_entry *entry;
153
154 if (!fw || !sproc->fw_addr)
155 return NULL;
156
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;
161 }
162
163 return sproc->fw_addr + entry->start;
164}
165
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200166/* STE modem firmware handler operations */
Jingoo Hanbd88acb2013-08-12 17:20:22 +0900167static const struct rproc_fw_ops sproc_fw_ops = {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200168 .load = sproc_load_segments,
169 .find_rsc_table = sproc_find_rsc_table,
Sjur Brændelande4b51412013-02-21 18:15:35 +0100170 .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200171};
172
173/* Kick the modem with specified notification id */
174static void sproc_kick(struct rproc *rproc, int vqid)
175{
176 struct sproc *sproc = rproc->priv;
177
178 sproc_dbg(sproc, "kick vqid:%d\n", vqid);
179
180 /*
181 * We need different notification IDs for RX and TX so add
182 * an offset on TX notification IDs.
183 */
184 sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
185}
186
187/* Received a kick from a modem, kick the virtqueue */
188static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
189{
190 struct sproc *sproc = mdev->drv_data;
191
192 if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
193 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
194}
195
Jingoo Hanbd88acb2013-08-12 17:20:22 +0900196static struct ste_modem_dev_cb sproc_dev_cb = {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200197 .kick = sproc_kick_callback,
198};
199
200/* Start the STE modem */
201static int sproc_start(struct rproc *rproc)
202{
203 struct sproc *sproc = rproc->priv;
204 int i, err;
205
206 sproc_dbg(sproc, "start ste-modem\n");
207
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;
213 }
214
215 /* Subscribe to notifications */
Sjur Brændelandba7290e2013-02-21 18:15:37 +0100216 for (i = 0; i <= rproc->max_notifyid; i++) {
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200217 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;
222 }
223 }
224
225 /* Request modem start-up*/
226 return sproc->mdev->ops.power(sproc->mdev, true);
227}
228
229/* Stop the STE modem */
230static int sproc_stop(struct rproc *rproc)
231{
232 struct sproc *sproc = rproc->priv;
233 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;
292 mdev->drv_data = sproc;
293
294 /* Provide callback functions to modem device */
295 sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
296
297 /* Set the STE-modem specific firmware handler */
298 rproc->fw_ops = &sproc_fw_ops;
299
300 /*
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.
304 */
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;
312 }
313
314 /* Register as a remoteproc device */
315 err = rproc_add(rproc);
316 if (err)
Dmitry Tarnyagin1cd425b2013-02-10 12:39:04 +0100317 goto free_mem;
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200318
319 return 0;
320
Dmitry Tarnyagin1cd425b2013-02-10 12:39:04 +0100321free_mem:
322 dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
323 sproc->fw_addr, sproc->fw_dma_addr);
Sjur Brændelandec4d02d2012-09-20 18:32:56 +0200324free_rproc:
325 /* Reset device data upon error */
326 mdev->drv_data = NULL;
327 rproc_put(rproc);
328 return err;
329}
330
331static struct platform_driver sproc_driver = {
332 .driver = {
333 .name = SPROC_MODEM_NAME,
334 .owner = THIS_MODULE,
335 },
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");