raspberrypi.c 7.1 KB

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