mtk_rpmsg.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2019 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
  12. */
  13. /*
  14. * The MediaTek Mbox communication solution provides point-to-point
  15. * channels for clients to send and receive packet based data.
  16. */
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include "rpmsg_internal.h"
  20. #include <mt-plat/mtk-mbox.h>
  21. #include <linux/rpmsg/mtk_rpmsg.h>
  22. #define to_mtk_rpmsg_device(r) container_of(r, struct mtk_rpmsg_device, rpdev)
  23. #define to_mtk_rpmsg_endpoint(r) container_of(r, struct mtk_rpmsg_endpoint, ept)
  24. int mtk_mbox_send(struct mtk_rpmsg_endpoint *mept,
  25. struct mtk_rpmsg_channel_info *mchan, void *buf,
  26. unsigned int len, unsigned int wait)
  27. {
  28. struct mtk_rpmsg_device *mdev;
  29. struct mtk_mbox_device *mbdev;
  30. unsigned int status;
  31. unsigned long flags;
  32. int ret;
  33. if (WARN_ON(len > mchan->send_slot_size || WARN_ON(!buf))) {
  34. pr_err("mbox:%u warning\n", mchan->mbox);
  35. return -EINVAL;
  36. }
  37. mdev = mept->mdev;
  38. mbdev = mept->mdev->mbdev;
  39. spin_lock_irqsave(&mchan->channel_lock, flags);
  40. status = mtk_mbox_check_send_irq(mbdev, mchan->mbox,
  41. mchan->send_pin_index);
  42. if (status != 0) {
  43. spin_unlock_irqrestore(&mchan->channel_lock, flags);
  44. return MBOX_PIN_BUSY;
  45. }
  46. spin_unlock_irqrestore(&mchan->channel_lock, flags);
  47. ret = mtk_mbox_write(mbdev, mchan->mbox, mchan->send_slot, buf,
  48. len * MBOX_SLOT_SIZE);
  49. if (ret != MBOX_DONE)
  50. return ret;
  51. /*
  52. * Ensure that all writes to SRAM are committed before sending the
  53. * interrupt to mbox.
  54. */
  55. mb();
  56. ret = mtk_mbox_trigger_irq(mbdev, mchan->mbox,
  57. 0x1 << mchan->send_pin_index);
  58. if (ret != MBOX_DONE)
  59. return ret;
  60. return ret;
  61. }
  62. EXPORT_SYMBOL_GPL(mtk_mbox_send);
  63. static struct mtk_rpmsg_operations mtk_rpmsg_ops = {
  64. .mbox_send = mtk_mbox_send,
  65. // .send_ipi = mtk_mbox_send,
  66. // .register_ipi = scp_ipi_register,
  67. // .unregister_ipi = scp_ipi_unregister,
  68. };
  69. static void __ept_release(struct kref *kref)
  70. {
  71. struct rpmsg_endpoint *ept;
  72. ept = container_of(kref, struct rpmsg_endpoint, refcount);
  73. kfree(to_mtk_rpmsg_endpoint(ept));
  74. }
  75. static void mtk_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  76. {
  77. kref_put(&ept->refcount, __ept_release);
  78. }
  79. static int mtk_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  80. {
  81. struct mtk_rpmsg_device *mdev;
  82. struct mtk_rpmsg_channel_info *mchan;
  83. mdev = to_mtk_rpmsg_endpoint(ept)->mdev;
  84. mchan = to_mtk_rpmsg_endpoint(ept)->mchan;
  85. return mdev->ops->mbox_send(to_mtk_rpmsg_endpoint(ept),
  86. mchan, data, len, 1);
  87. }
  88. static int mtk_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  89. {
  90. struct mtk_rpmsg_device *mdev;
  91. struct mtk_rpmsg_channel_info *mchan;
  92. mdev = to_mtk_rpmsg_endpoint(ept)->mdev;
  93. mchan = to_mtk_rpmsg_endpoint(ept)->mchan;
  94. return mdev->ops->mbox_send(to_mtk_rpmsg_endpoint(ept),
  95. mchan, data, len, 0);
  96. }
  97. static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops = {
  98. .destroy_ept = mtk_rpmsg_destroy_ept,
  99. .send = mtk_rpmsg_send,
  100. .trysend = mtk_rpmsg_trysend,
  101. };
  102. /*
  103. * create mtk rpmsg channel
  104. */
  105. struct mtk_rpmsg_channel_info *
  106. mtk_rpmsg_create_channel(struct mtk_rpmsg_device *mdev, u32 chan_id, char *name)
  107. {
  108. struct mtk_rpmsg_channel_info *mchan;
  109. struct mtk_mbox_device *mbdev;
  110. struct mtk_mbox_pin_send *msend;
  111. struct mtk_mbox_pin_recv *mrecv;
  112. unsigned int i, count, found;
  113. //malloc mtk_rpmsg_channel_info mchan
  114. mchan = kzalloc(sizeof(*mchan), GFP_KERNEL);
  115. if (!mchan)
  116. return NULL;
  117. mbdev = mdev->mbdev;
  118. spin_lock_init(&mchan->channel_lock);
  119. mchan->info.src = chan_id;
  120. //mchan->info.dst = dst;
  121. //copy name to rpdev, only dev no need now
  122. //strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
  123. //copy name to channel info
  124. strlcpy(mchan->info.name, name, RPMSG_NAME_SIZE);
  125. count = mbdev->recv_count;
  126. found = 0;
  127. for (i = 0; i < count; ++i) {
  128. mrecv = &(mbdev->pin_recv_table[i]);
  129. if (chan_id == mrecv->chan_id) {
  130. mchan->mbox = mrecv->mbox;
  131. mchan->recv_slot = mrecv->offset;
  132. mchan->recv_slot_size = mrecv->msg_size;
  133. mchan->recv_pin_index = mrecv->pin_index;
  134. mchan->recv_pin_offset = i;
  135. //TODO if not found
  136. found = 1;
  137. }
  138. }
  139. count = mbdev->send_count;
  140. found = 0;
  141. for (i = 0; i < count; ++i) {
  142. msend = &(mbdev->pin_send_table[i]);
  143. if (chan_id == msend->chan_id) {
  144. mchan->mbox = msend->mbox;
  145. mchan->send_slot = msend->offset;
  146. mchan->send_slot_size = msend->msg_size;
  147. mchan->send_pin_index = msend->pin_index;
  148. mchan->send_pin_offset = i;
  149. //TODO if not found
  150. found = 1;
  151. }
  152. }
  153. return mchan;
  154. }
  155. EXPORT_SYMBOL_GPL(mtk_rpmsg_create_channel);
  156. /*
  157. * create endpoint
  158. */
  159. static struct rpmsg_endpoint *
  160. __rpmsg_create_ept(struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
  161. struct rpmsg_channel_info chinfo)
  162. {
  163. struct mtk_rpmsg_endpoint *mept;
  164. struct rpmsg_endpoint *ept;
  165. mept = kzalloc(sizeof(*mept), GFP_KERNEL);
  166. if (!mept)
  167. return NULL;
  168. mept->mdev = to_mtk_rpmsg_device(rpdev);
  169. mept->mchan = (struct mtk_rpmsg_channel_info *)priv;
  170. ept = &mept->ept;
  171. kref_init(&ept->refcount);
  172. ept->rpdev = rpdev;
  173. ept->cb = cb;
  174. //ept->priv = priv;
  175. ept->ops = &mtk_rpmsg_endpoint_ops;
  176. ept->addr = chinfo.src;
  177. return ept;
  178. }
  179. static struct rpmsg_endpoint *
  180. mtk_rpmsg_create_ept(struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
  181. struct rpmsg_channel_info chinfo)
  182. {
  183. return __rpmsg_create_ept(rpdev, cb, priv, chinfo);
  184. }
  185. static const struct rpmsg_device_ops mtk_rpmsg_device_ops = {
  186. .create_ept = mtk_rpmsg_create_ept,
  187. };
  188. static void mtk_rpmsg_release_device(struct device *dev)
  189. {
  190. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  191. struct mtk_rpmsg_device *mdev = to_mtk_rpmsg_device(rpdev);
  192. kfree(mdev);
  193. }
  194. /*
  195. * create mtk rpmsg device
  196. */
  197. struct mtk_rpmsg_device *
  198. mtk_rpmsg_create_device(struct platform_device *pdev,
  199. struct mtk_mbox_device *mbdev, unsigned int ipc_chan_id)
  200. {
  201. struct rpmsg_device *rpdev;
  202. struct mtk_rpmsg_device *mdev;
  203. int ret;
  204. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  205. if (!mdev)
  206. return NULL;
  207. mdev->pdev = pdev;
  208. mdev->mbdev = mbdev;
  209. mdev->ops = &mtk_rpmsg_ops;
  210. rpdev = &mdev->rpdev;
  211. rpdev->ops = &mtk_rpmsg_device_ops;
  212. rpdev->src = ipc_chan_id;
  213. //rpdev->dst = info->dst;
  214. //register RPMSG device
  215. #if 0
  216. rpdev->dev.of_node =
  217. mtk_rpmsg_match_device_subnode
  218. (pdev->dev.of_node, info->name);
  219. #endif
  220. rpdev->dev.parent = &pdev->dev;
  221. rpdev->dev.release = mtk_rpmsg_release_device;
  222. ret = rpmsg_register_device(rpdev);
  223. if (ret) {
  224. kfree(mdev);
  225. return NULL;
  226. }
  227. return mdev;
  228. }
  229. EXPORT_SYMBOL_GPL(mtk_rpmsg_create_device);
  230. //MODULE_LICENSE("GPL v2");
  231. //MODULE_DESCRIPTION("MediaTek rpmsg driver");