qcom_glink_rpm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2016-2017, Linaro Ltd
  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 and
  6. * only version 2 as 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. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/idr.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/list.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/rpmsg.h>
  24. #include <linux/slab.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/mailbox_client.h>
  27. #include "rpmsg_internal.h"
  28. #include "qcom_glink_native.h"
  29. #define RPM_TOC_SIZE 256
  30. #define RPM_TOC_MAGIC 0x67727430 /* grt0 */
  31. #define RPM_TOC_MAX_ENTRIES ((RPM_TOC_SIZE - sizeof(struct rpm_toc)) / \
  32. sizeof(struct rpm_toc_entry))
  33. #define RPM_TX_FIFO_ID 0x61703272 /* ap2r */
  34. #define RPM_RX_FIFO_ID 0x72326170 /* r2ap */
  35. #define to_rpm_pipe(p) container_of(p, struct glink_rpm_pipe, native)
  36. struct rpm_toc_entry {
  37. __le32 id;
  38. __le32 offset;
  39. __le32 size;
  40. } __packed;
  41. struct rpm_toc {
  42. __le32 magic;
  43. __le32 count;
  44. struct rpm_toc_entry entries[];
  45. } __packed;
  46. struct glink_rpm_pipe {
  47. struct qcom_glink_pipe native;
  48. void __iomem *tail;
  49. void __iomem *head;
  50. void __iomem *fifo;
  51. };
  52. static size_t glink_rpm_rx_avail(struct qcom_glink_pipe *glink_pipe)
  53. {
  54. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  55. unsigned int head;
  56. unsigned int tail;
  57. head = readl(pipe->head);
  58. tail = readl(pipe->tail);
  59. if (head < tail)
  60. return pipe->native.length - tail + head;
  61. else
  62. return head - tail;
  63. }
  64. static void glink_rpm_rx_peak(struct qcom_glink_pipe *glink_pipe,
  65. void *data, unsigned int offset, size_t count)
  66. {
  67. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  68. unsigned int tail;
  69. size_t len;
  70. tail = readl(pipe->tail);
  71. tail += offset;
  72. if (tail >= pipe->native.length)
  73. tail -= pipe->native.length;
  74. len = min_t(size_t, count, pipe->native.length - tail);
  75. if (len) {
  76. __ioread32_copy(data, pipe->fifo + tail,
  77. len / sizeof(u32));
  78. }
  79. if (len != count) {
  80. __ioread32_copy(data + len, pipe->fifo,
  81. (count - len) / sizeof(u32));
  82. }
  83. }
  84. static void glink_rpm_rx_advance(struct qcom_glink_pipe *glink_pipe,
  85. size_t count)
  86. {
  87. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  88. unsigned int tail;
  89. tail = readl(pipe->tail);
  90. tail += count;
  91. if (tail >= pipe->native.length)
  92. tail -= pipe->native.length;
  93. writel(tail, pipe->tail);
  94. }
  95. static size_t glink_rpm_tx_avail(struct qcom_glink_pipe *glink_pipe)
  96. {
  97. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  98. unsigned int head;
  99. unsigned int tail;
  100. head = readl(pipe->head);
  101. tail = readl(pipe->tail);
  102. if (tail <= head)
  103. return pipe->native.length - head + tail;
  104. else
  105. return tail - head;
  106. }
  107. static unsigned int glink_rpm_tx_write_one(struct glink_rpm_pipe *pipe,
  108. unsigned int head,
  109. const void *data, size_t count)
  110. {
  111. size_t len;
  112. len = min_t(size_t, count, pipe->native.length - head);
  113. if (len) {
  114. __iowrite32_copy(pipe->fifo + head, data,
  115. len / sizeof(u32));
  116. }
  117. if (len != count) {
  118. __iowrite32_copy(pipe->fifo, data + len,
  119. (count - len) / sizeof(u32));
  120. }
  121. head += count;
  122. if (head >= pipe->native.length)
  123. head -= pipe->native.length;
  124. return head;
  125. }
  126. static void glink_rpm_tx_write(struct qcom_glink_pipe *glink_pipe,
  127. const void *hdr, size_t hlen,
  128. const void *data, size_t dlen)
  129. {
  130. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  131. size_t tlen = hlen + dlen;
  132. size_t aligned_dlen;
  133. unsigned int head;
  134. char padding[8] = {0};
  135. size_t pad;
  136. /* Header length comes from glink native and is always 4 byte aligned */
  137. if (WARN(hlen % 4, "Glink Header length must be 4 bytes aligned\n"))
  138. return;
  139. /*
  140. * Move the unaligned tail of the message to the padding chunk, to
  141. * ensure word aligned accesses
  142. */
  143. aligned_dlen = ALIGN_DOWN(dlen, 4);
  144. if (aligned_dlen != dlen)
  145. memcpy(padding, data + aligned_dlen, dlen - aligned_dlen);
  146. head = readl(pipe->head);
  147. head = glink_rpm_tx_write_one(pipe, head, hdr, hlen);
  148. head = glink_rpm_tx_write_one(pipe, head, data, aligned_dlen);
  149. pad = ALIGN(tlen, 8) - ALIGN_DOWN(tlen, 4);
  150. if (pad)
  151. head = glink_rpm_tx_write_one(pipe, head, padding, pad);
  152. writel(head, pipe->head);
  153. }
  154. static int glink_rpm_parse_toc(struct device *dev,
  155. void __iomem *msg_ram,
  156. size_t msg_ram_size,
  157. struct glink_rpm_pipe *rx,
  158. struct glink_rpm_pipe *tx)
  159. {
  160. struct rpm_toc *toc;
  161. int num_entries;
  162. unsigned int id;
  163. size_t offset;
  164. size_t size;
  165. void *buf;
  166. int i;
  167. buf = kzalloc(RPM_TOC_SIZE, GFP_KERNEL);
  168. if (!buf)
  169. return -ENOMEM;
  170. __ioread32_copy(buf, msg_ram + msg_ram_size - RPM_TOC_SIZE,
  171. RPM_TOC_SIZE / sizeof(u32));
  172. toc = buf;
  173. if (le32_to_cpu(toc->magic) != RPM_TOC_MAGIC) {
  174. dev_err(dev, "RPM TOC has invalid magic\n");
  175. goto err_inval;
  176. }
  177. num_entries = le32_to_cpu(toc->count);
  178. if (num_entries > RPM_TOC_MAX_ENTRIES) {
  179. dev_err(dev, "Invalid number of toc entries\n");
  180. goto err_inval;
  181. }
  182. for (i = 0; i < num_entries; i++) {
  183. id = le32_to_cpu(toc->entries[i].id);
  184. offset = le32_to_cpu(toc->entries[i].offset);
  185. size = le32_to_cpu(toc->entries[i].size);
  186. if (offset > msg_ram_size || offset + size > msg_ram_size) {
  187. dev_err(dev, "TOC entry with invalid size\n");
  188. continue;
  189. }
  190. switch (id) {
  191. case RPM_RX_FIFO_ID:
  192. rx->native.length = size;
  193. rx->tail = msg_ram + offset;
  194. rx->head = msg_ram + offset + sizeof(u32);
  195. rx->fifo = msg_ram + offset + 2 * sizeof(u32);
  196. break;
  197. case RPM_TX_FIFO_ID:
  198. tx->native.length = size;
  199. tx->tail = msg_ram + offset;
  200. tx->head = msg_ram + offset + sizeof(u32);
  201. tx->fifo = msg_ram + offset + 2 * sizeof(u32);
  202. break;
  203. }
  204. }
  205. if (!rx->fifo || !tx->fifo) {
  206. dev_err(dev, "Unable to find rx and tx descriptors\n");
  207. goto err_inval;
  208. }
  209. kfree(buf);
  210. return 0;
  211. err_inval:
  212. kfree(buf);
  213. return -EINVAL;
  214. }
  215. static int glink_rpm_probe(struct platform_device *pdev)
  216. {
  217. struct qcom_glink *glink;
  218. struct glink_rpm_pipe *rx_pipe;
  219. struct glink_rpm_pipe *tx_pipe;
  220. struct device_node *np;
  221. void __iomem *msg_ram;
  222. size_t msg_ram_size;
  223. struct device *dev = &pdev->dev;
  224. struct resource r;
  225. int ret;
  226. rx_pipe = devm_kzalloc(&pdev->dev, sizeof(*rx_pipe), GFP_KERNEL);
  227. tx_pipe = devm_kzalloc(&pdev->dev, sizeof(*tx_pipe), GFP_KERNEL);
  228. if (!rx_pipe || !tx_pipe)
  229. return -ENOMEM;
  230. np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", 0);
  231. ret = of_address_to_resource(np, 0, &r);
  232. of_node_put(np);
  233. if (ret)
  234. return ret;
  235. msg_ram = devm_ioremap(dev, r.start, resource_size(&r));
  236. msg_ram_size = resource_size(&r);
  237. if (!msg_ram)
  238. return -ENOMEM;
  239. ret = glink_rpm_parse_toc(dev, msg_ram, msg_ram_size,
  240. rx_pipe, tx_pipe);
  241. if (ret)
  242. return ret;
  243. /* Pipe specific accessors */
  244. rx_pipe->native.avail = glink_rpm_rx_avail;
  245. rx_pipe->native.peak = glink_rpm_rx_peak;
  246. rx_pipe->native.advance = glink_rpm_rx_advance;
  247. tx_pipe->native.avail = glink_rpm_tx_avail;
  248. tx_pipe->native.write = glink_rpm_tx_write;
  249. writel(0, tx_pipe->head);
  250. writel(0, rx_pipe->tail);
  251. glink = qcom_glink_native_probe(&pdev->dev,
  252. 0,
  253. &rx_pipe->native,
  254. &tx_pipe->native,
  255. true);
  256. if (IS_ERR(glink))
  257. return PTR_ERR(glink);
  258. platform_set_drvdata(pdev, glink);
  259. return 0;
  260. }
  261. static int glink_rpm_remove(struct platform_device *pdev)
  262. {
  263. struct qcom_glink *glink = platform_get_drvdata(pdev);
  264. qcom_glink_native_remove(glink);
  265. return 0;
  266. }
  267. static const struct of_device_id glink_rpm_of_match[] = {
  268. { .compatible = "qcom,glink-rpm" },
  269. {}
  270. };
  271. MODULE_DEVICE_TABLE(of, glink_rpm_of_match);
  272. static struct platform_driver glink_rpm_driver = {
  273. .probe = glink_rpm_probe,
  274. .remove = glink_rpm_remove,
  275. .driver = {
  276. .name = "qcom_glink_rpm",
  277. .of_match_table = glink_rpm_of_match,
  278. },
  279. };
  280. static int __init glink_rpm_init(void)
  281. {
  282. return platform_driver_register(&glink_rpm_driver);
  283. }
  284. subsys_initcall(glink_rpm_init);
  285. static void __exit glink_rpm_exit(void)
  286. {
  287. platform_driver_unregister(&glink_rpm_driver);
  288. }
  289. module_exit(glink_rpm_exit);
  290. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@linaro.org>");
  291. MODULE_DESCRIPTION("Qualcomm GLINK RPM driver");
  292. MODULE_LICENSE("GPL v2");