ste_modem_rproc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include <linux/module.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/remoteproc.h>
  9. #include <linux/ste_modem_shm.h>
  10. #include "remoteproc_internal.h"
  11. #define SPROC_FW_SIZE (50 * 4096)
  12. #define SPROC_MAX_TOC_ENTRIES 32
  13. #define SPROC_MAX_NOTIFY_ID 14
  14. #define SPROC_RESOURCE_NAME "rsc-table"
  15. #define SPROC_MODEM_NAME "ste-modem"
  16. #define SPROC_MODEM_FIRMWARE SPROC_MODEM_NAME "-fw.bin"
  17. #define sproc_dbg(sproc, fmt, ...) \
  18. dev_dbg(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
  19. #define sproc_err(sproc, fmt, ...) \
  20. dev_err(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
  21. /* STE-modem control structure */
  22. struct sproc {
  23. struct rproc *rproc;
  24. struct ste_modem_device *mdev;
  25. int error;
  26. void *fw_addr;
  27. size_t fw_size;
  28. dma_addr_t fw_dma_addr;
  29. };
  30. /* STE-Modem firmware entry */
  31. struct ste_toc_entry {
  32. __le32 start;
  33. __le32 size;
  34. __le32 flags;
  35. __le32 entry_point;
  36. __le32 load_addr;
  37. char name[12];
  38. };
  39. /*
  40. * The Table Of Content is located at the start of the firmware image and
  41. * at offset zero in the shared memory region. The resource table typically
  42. * contains the initial boot image (boot strap) and other information elements
  43. * such as remoteproc resource table. Each entry is identified by a unique
  44. * name.
  45. */
  46. struct ste_toc {
  47. struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
  48. };
  49. /* Loads the firmware to shared memory. */
  50. static int sproc_load_segments(struct rproc *rproc, const struct firmware *fw)
  51. {
  52. struct sproc *sproc = rproc->priv;
  53. memcpy(sproc->fw_addr, fw->data, fw->size);
  54. return 0;
  55. }
  56. /* Find the entry for resource table in the Table of Content */
  57. static struct ste_toc_entry *sproc_find_rsc_entry(const struct firmware *fw)
  58. {
  59. int i;
  60. struct ste_toc *toc;
  61. if (!fw)
  62. return NULL;
  63. toc = (void *)fw->data;
  64. /* Search the table for the resource table */
  65. for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
  66. toc->table[i].start != 0xffffffff; i++) {
  67. if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
  68. sizeof(toc->table[i].name))) {
  69. if (toc->table[i].start > fw->size)
  70. return NULL;
  71. return &toc->table[i];
  72. }
  73. }
  74. return NULL;
  75. }
  76. /* Find the resource table inside the remote processor's firmware. */
  77. static struct resource_table *
  78. sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  79. int *tablesz)
  80. {
  81. struct sproc *sproc = rproc->priv;
  82. struct resource_table *table;
  83. struct ste_toc_entry *entry;
  84. entry = sproc_find_rsc_entry(fw);
  85. if (!entry) {
  86. sproc_err(sproc, "resource table not found in fw\n");
  87. return NULL;
  88. }
  89. table = (void *)(fw->data + entry->start);
  90. /* sanity check size and offset of resource table */
  91. if (entry->start > SPROC_FW_SIZE ||
  92. entry->size > SPROC_FW_SIZE ||
  93. fw->size > SPROC_FW_SIZE ||
  94. entry->start + entry->size > fw->size ||
  95. sizeof(struct resource_table) > entry->size) {
  96. sproc_err(sproc, "bad size of fw or resource table\n");
  97. return NULL;
  98. }
  99. /* we don't support any version beyond the first */
  100. if (table->ver != 1) {
  101. sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
  102. return NULL;
  103. }
  104. /* make sure reserved bytes are zeroes */
  105. if (table->reserved[0] || table->reserved[1]) {
  106. sproc_err(sproc, "non zero reserved bytes\n");
  107. return NULL;
  108. }
  109. /* make sure the offsets array isn't truncated */
  110. if (table->num > SPROC_MAX_TOC_ENTRIES ||
  111. table->num * sizeof(table->offset[0]) +
  112. sizeof(struct resource_table) > entry->size) {
  113. sproc_err(sproc, "resource table incomplete\n");
  114. return NULL;
  115. }
  116. /* If the fw size has grown, release the previous fw allocation */
  117. if (SPROC_FW_SIZE < fw->size) {
  118. sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
  119. SPROC_FW_SIZE, fw->size);
  120. return NULL;
  121. }
  122. sproc->fw_size = fw->size;
  123. *tablesz = entry->size;
  124. return table;
  125. }
  126. /* STE modem firmware handler operations */
  127. const struct rproc_fw_ops sproc_fw_ops = {
  128. .load = sproc_load_segments,
  129. .find_rsc_table = sproc_find_rsc_table,
  130. };
  131. /* Kick the modem with specified notification id */
  132. static void sproc_kick(struct rproc *rproc, int vqid)
  133. {
  134. struct sproc *sproc = rproc->priv;
  135. sproc_dbg(sproc, "kick vqid:%d\n", vqid);
  136. /*
  137. * We need different notification IDs for RX and TX so add
  138. * an offset on TX notification IDs.
  139. */
  140. sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
  141. }
  142. /* Received a kick from a modem, kick the virtqueue */
  143. static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
  144. {
  145. struct sproc *sproc = mdev->drv_data;
  146. if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
  147. sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
  148. }
  149. struct ste_modem_dev_cb sproc_dev_cb = {
  150. .kick = sproc_kick_callback,
  151. };
  152. /* Start the STE modem */
  153. static int sproc_start(struct rproc *rproc)
  154. {
  155. struct sproc *sproc = rproc->priv;
  156. int i, err;
  157. sproc_dbg(sproc, "start ste-modem\n");
  158. /* Sanity test the max_notifyid */
  159. if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
  160. sproc_err(sproc, "Notification IDs too high:%d\n",
  161. rproc->max_notifyid);
  162. return -EINVAL;
  163. }
  164. /* Subscribe to notifications */
  165. for (i = 0; i < rproc->max_notifyid; i++) {
  166. err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
  167. if (err) {
  168. sproc_err(sproc,
  169. "subscription of kicks failed:%d\n", err);
  170. return err;
  171. }
  172. }
  173. /* Request modem start-up*/
  174. return sproc->mdev->ops.power(sproc->mdev, true);
  175. }
  176. /* Stop the STE modem */
  177. static int sproc_stop(struct rproc *rproc)
  178. {
  179. struct sproc *sproc = rproc->priv;
  180. sproc_dbg(sproc, "stop ste-modem\n");
  181. return sproc->mdev->ops.power(sproc->mdev, false);
  182. }
  183. static struct rproc_ops sproc_ops = {
  184. .start = sproc_start,
  185. .stop = sproc_stop,
  186. .kick = sproc_kick,
  187. };
  188. /* STE modem device is unregistered */
  189. static int sproc_drv_remove(struct platform_device *pdev)
  190. {
  191. struct ste_modem_device *mdev =
  192. container_of(pdev, struct ste_modem_device, pdev);
  193. struct sproc *sproc = mdev->drv_data;
  194. sproc_dbg(sproc, "remove ste-modem\n");
  195. /* Reset device callback functions */
  196. sproc->mdev->ops.setup(sproc->mdev, NULL);
  197. /* Unregister as remoteproc device */
  198. rproc_del(sproc->rproc);
  199. rproc_put(sproc->rproc);
  200. mdev->drv_data = NULL;
  201. return 0;
  202. }
  203. /* Handle probe of a modem device */
  204. static int sproc_probe(struct platform_device *pdev)
  205. {
  206. struct ste_modem_device *mdev =
  207. container_of(pdev, struct ste_modem_device, pdev);
  208. struct sproc *sproc;
  209. struct rproc *rproc;
  210. int err;
  211. dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
  212. if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
  213. !mdev->ops.power) {
  214. dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
  215. return -EINVAL;
  216. }
  217. rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
  218. SPROC_MODEM_FIRMWARE, sizeof(*sproc));
  219. if (!rproc)
  220. return -ENOMEM;
  221. sproc = rproc->priv;
  222. sproc->mdev = mdev;
  223. sproc->rproc = rproc;
  224. mdev->drv_data = sproc;
  225. /* Provide callback functions to modem device */
  226. sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
  227. /* Set the STE-modem specific firmware handler */
  228. rproc->fw_ops = &sproc_fw_ops;
  229. /*
  230. * STE-modem requires the firmware to be located
  231. * at the start of the shared memory region. So we need to
  232. * reserve space for firmware at the start.
  233. */
  234. sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
  235. &sproc->fw_dma_addr,
  236. GFP_KERNEL);
  237. if (!sproc->fw_addr) {
  238. sproc_err(sproc, "Cannot allocate memory for fw\n");
  239. err = -ENOMEM;
  240. goto free_rproc;
  241. }
  242. /* Register as a remoteproc device */
  243. err = rproc_add(rproc);
  244. if (err)
  245. goto free_rproc;
  246. return 0;
  247. free_rproc:
  248. /* Reset device data upon error */
  249. mdev->drv_data = NULL;
  250. rproc_put(rproc);
  251. return err;
  252. }
  253. static struct platform_driver sproc_driver = {
  254. .driver = {
  255. .name = SPROC_MODEM_NAME,
  256. .owner = THIS_MODULE,
  257. },
  258. .probe = sproc_probe,
  259. .remove = sproc_drv_remove,
  260. };
  261. module_platform_driver(sproc_driver);
  262. MODULE_LICENSE("GPL v2");
  263. MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");