raspberrypi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Defines interfaces for interacting wtih the Raspberry Pi firmware's
  3. * property channel.
  4. *
  5. * Copyright © 2015 Broadcom
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/dma-mapping.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <soc/bcm2835/raspberrypi-firmware.h>
  17. #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  18. #define MBOX_CHAN(msg) ((msg) & 0xf)
  19. #define MBOX_DATA28(msg) ((msg) & ~0xf)
  20. #define MBOX_CHAN_PROPERTY 8
  21. struct rpi_firmware {
  22. struct mbox_client cl;
  23. struct mbox_chan *chan; /* The property channel. */
  24. struct completion c;
  25. u32 enabled;
  26. };
  27. static DEFINE_MUTEX(transaction_lock);
  28. static void response_callback(struct mbox_client *cl, void *msg)
  29. {
  30. struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
  31. complete(&fw->c);
  32. }
  33. /*
  34. * Sends a request to the firmware through the BCM2835 mailbox driver,
  35. * and synchronously waits for the reply.
  36. */
  37. static int
  38. rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
  39. {
  40. u32 message = MBOX_MSG(chan, data);
  41. int ret;
  42. WARN_ON(data & 0xf);
  43. mutex_lock(&transaction_lock);
  44. reinit_completion(&fw->c);
  45. ret = mbox_send_message(fw->chan, &message);
  46. if (ret >= 0) {
  47. wait_for_completion(&fw->c);
  48. ret = 0;
  49. } else {
  50. dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
  51. }
  52. mutex_unlock(&transaction_lock);
  53. return ret;
  54. }
  55. /**
  56. * rpi_firmware_property_list - Submit firmware property list
  57. * @fw: Pointer to firmware structure from rpi_firmware_get().
  58. * @data: Buffer holding tags.
  59. * @tag_size: Size of tags buffer.
  60. *
  61. * Submits a set of concatenated tags to the VPU firmware through the
  62. * mailbox property interface.
  63. *
  64. * The buffer header and the ending tag are added by this function and
  65. * don't need to be supplied, just the actual tags for your operation.
  66. * See struct rpi_firmware_property_tag_header for the per-tag
  67. * structure.
  68. */
  69. int rpi_firmware_property_list(struct rpi_firmware *fw,
  70. void *data, size_t tag_size)
  71. {
  72. size_t size = tag_size + 12;
  73. u32 *buf;
  74. dma_addr_t bus_addr;
  75. int ret;
  76. /* Packets are processed a dword at a time. */
  77. if (size & 3)
  78. return -EINVAL;
  79. buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
  80. GFP_ATOMIC);
  81. if (!buf)
  82. return -ENOMEM;
  83. /* The firmware will error out without parsing in this case. */
  84. WARN_ON(size >= 1024 * 1024);
  85. buf[0] = size;
  86. buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
  87. memcpy(&buf[2], data, tag_size);
  88. buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
  89. wmb();
  90. ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
  91. rmb();
  92. memcpy(data, &buf[2], tag_size);
  93. if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
  94. /*
  95. * The tag name here might not be the one causing the
  96. * error, if there were multiple tags in the request.
  97. * But single-tag is the most common, so go with it.
  98. */
  99. dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
  100. buf[2], buf[1]);
  101. ret = -EINVAL;
  102. }
  103. dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
  107. /**
  108. * rpi_firmware_property - Submit single firmware property
  109. * @fw: Pointer to firmware structure from rpi_firmware_get().
  110. * @tag: One of enum_mbox_property_tag.
  111. * @tag_data: Tag data buffer.
  112. * @buf_size: Buffer size.
  113. *
  114. * Submits a single tag to the VPU firmware through the mailbox
  115. * property interface.
  116. *
  117. * This is a convenience wrapper around
  118. * rpi_firmware_property_list() to avoid some of the
  119. * boilerplate in property calls.
  120. */
  121. int rpi_firmware_property(struct rpi_firmware *fw,
  122. u32 tag, void *tag_data, size_t buf_size)
  123. {
  124. /* Single tags are very small (generally 8 bytes), so the
  125. * stack should be safe.
  126. */
  127. u8 data[buf_size + sizeof(struct rpi_firmware_property_tag_header)];
  128. struct rpi_firmware_property_tag_header *header =
  129. (struct rpi_firmware_property_tag_header *)data;
  130. int ret;
  131. header->tag = tag;
  132. header->buf_size = buf_size;
  133. header->req_resp_size = 0;
  134. memcpy(data + sizeof(struct rpi_firmware_property_tag_header),
  135. tag_data, buf_size);
  136. ret = rpi_firmware_property_list(fw, &data, sizeof(data));
  137. memcpy(tag_data,
  138. data + sizeof(struct rpi_firmware_property_tag_header),
  139. buf_size);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(rpi_firmware_property);
  143. static void
  144. rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
  145. {
  146. u32 packet;
  147. int ret = rpi_firmware_property(fw,
  148. RPI_FIRMWARE_GET_FIRMWARE_REVISION,
  149. &packet, sizeof(packet));
  150. if (ret == 0) {
  151. struct tm tm;
  152. time_to_tm(packet, 0, &tm);
  153. dev_info(fw->cl.dev,
  154. "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
  155. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  156. tm.tm_hour, tm.tm_min);
  157. }
  158. }
  159. static int rpi_firmware_probe(struct platform_device *pdev)
  160. {
  161. struct device *dev = &pdev->dev;
  162. struct rpi_firmware *fw;
  163. fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
  164. if (!fw)
  165. return -ENOMEM;
  166. fw->cl.dev = dev;
  167. fw->cl.rx_callback = response_callback;
  168. fw->cl.tx_block = true;
  169. fw->chan = mbox_request_channel(&fw->cl, 0);
  170. if (IS_ERR(fw->chan)) {
  171. int ret = PTR_ERR(fw->chan);
  172. if (ret != -EPROBE_DEFER)
  173. dev_err(dev, "Failed to get mbox channel: %d\n", ret);
  174. return ret;
  175. }
  176. init_completion(&fw->c);
  177. platform_set_drvdata(pdev, fw);
  178. rpi_firmware_print_firmware_revision(fw);
  179. return 0;
  180. }
  181. static int rpi_firmware_remove(struct platform_device *pdev)
  182. {
  183. struct rpi_firmware *fw = platform_get_drvdata(pdev);
  184. mbox_free_channel(fw->chan);
  185. return 0;
  186. }
  187. /**
  188. * rpi_firmware_get - Get pointer to rpi_firmware structure.
  189. * @firmware_node: Pointer to the firmware Device Tree node.
  190. *
  191. * Returns NULL is the firmware device is not ready.
  192. */
  193. struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
  194. {
  195. struct platform_device *pdev = of_find_device_by_node(firmware_node);
  196. if (!pdev)
  197. return NULL;
  198. return platform_get_drvdata(pdev);
  199. }
  200. EXPORT_SYMBOL_GPL(rpi_firmware_get);
  201. static const struct of_device_id rpi_firmware_of_match[] = {
  202. { .compatible = "raspberrypi,bcm2835-firmware", },
  203. {},
  204. };
  205. MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
  206. static struct platform_driver rpi_firmware_driver = {
  207. .driver = {
  208. .name = "raspberrypi-firmware",
  209. .of_match_table = rpi_firmware_of_match,
  210. },
  211. .probe = rpi_firmware_probe,
  212. .remove = rpi_firmware_remove,
  213. };
  214. module_platform_driver(rpi_firmware_driver);
  215. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  216. MODULE_DESCRIPTION("Raspberry Pi firmware driver");
  217. MODULE_LICENSE("GPL v2");