Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 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 */ |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 67 | static const struct ste_toc_entry *sproc_find_rsc_entry(const void *data) |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 68 | { |
| 69 | int i; |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 70 | const struct ste_toc *toc; |
| 71 | toc = data; |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 72 | |
| 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ændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 76 | if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME, |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 77 | sizeof(toc->table[i].name))) |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 78 | return &toc->table[i]; |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 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) |
| 88 | { |
| 89 | struct sproc *sproc = rproc->priv; |
| 90 | struct resource_table *table; |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 91 | const struct ste_toc_entry *entry; |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 92 | |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 93 | if (!fw) |
| 94 | return NULL; |
| 95 | |
| 96 | entry = sproc_find_rsc_entry(fw->data); |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 97 | 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ændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 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) |
| 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ændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 166 | /* STE modem firmware handler operations */ |
Jingoo Han | bd88acb | 2013-08-12 17:20:22 +0900 | [diff] [blame] | 167 | static const struct rproc_fw_ops sproc_fw_ops = { |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 168 | .load = sproc_load_segments, |
| 169 | .find_rsc_table = sproc_find_rsc_table, |
Sjur Brændeland | e4b5141 | 2013-02-21 18:15:35 +0100 | [diff] [blame] | 170 | .find_loaded_rsc_table = sproc_find_loaded_rsc_table, |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /* Kick the modem with specified notification id */ |
| 174 | static 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 */ |
| 188 | static 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 Han | bd88acb | 2013-08-12 17:20:22 +0900 | [diff] [blame] | 196 | static struct ste_modem_dev_cb sproc_dev_cb = { |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 197 | .kick = sproc_kick_callback, |
| 198 | }; |
| 199 | |
| 200 | /* Start the STE modem */ |
| 201 | static 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ændeland | ba7290e | 2013-02-21 18:15:37 +0100 | [diff] [blame] | 216 | for (i = 0; i <= rproc->max_notifyid; i++) { |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 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; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* Request modem start-up*/ |
| 226 | return sproc->mdev->ops.power(sproc->mdev, true); |
| 227 | } |
| 228 | |
| 229 | /* Stop the STE modem */ |
| 230 | static 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 | |
| 238 | static 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 */ |
| 245 | static 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 Tarnyagin | 1cd425b | 2013-02-10 12:39:04 +0100 | [diff] [blame] | 258 | dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE, |
| 259 | sproc->fw_addr, sproc->fw_dma_addr); |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 260 | rproc_put(sproc->rproc); |
| 261 | |
| 262 | mdev->drv_data = NULL; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* Handle probe of a modem device */ |
| 268 | static 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 Tarnyagin | 1cd425b | 2013-02-10 12:39:04 +0100 | [diff] [blame] | 317 | goto free_mem; |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | |
Dmitry Tarnyagin | 1cd425b | 2013-02-10 12:39:04 +0100 | [diff] [blame] | 321 | free_mem: |
| 322 | dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE, |
| 323 | sproc->fw_addr, sproc->fw_dma_addr); |
Sjur Brændeland | ec4d02d | 2012-09-20 18:32:56 +0200 | [diff] [blame] | 324 | free_rproc: |
| 325 | /* Reset device data upon error */ |
| 326 | mdev->drv_data = NULL; |
| 327 | rproc_put(rproc); |
| 328 | return err; |
| 329 | } |
| 330 | |
| 331 | static 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 | |
| 340 | module_platform_driver(sproc_driver); |
| 341 | MODULE_LICENSE("GPL v2"); |
| 342 | MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework"); |