raspberrypi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Defines interfaces for interacting wtih the Raspberry Pi firmware's
  4. * property channel.
  5. *
  6. * Copyright © 2015 Broadcom
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/kref.h>
  10. #include <linux/mailbox_client.h>
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <soc/bcm2835/raspberrypi-firmware.h>
  16. #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  17. #define MBOX_CHAN(msg) ((msg) & 0xf)
  18. #define MBOX_DATA28(msg) ((msg) & ~0xf)
  19. #define MBOX_CHAN_PROPERTY 8
  20. static struct platform_device *rpi_hwmon;
  21. static struct platform_device *rpi_clk;
  22. struct rpi_firmware {
  23. struct mbox_client cl;
  24. struct mbox_chan *chan; /* The property channel. */
  25. struct completion c;
  26. u32 enabled;
  27. struct kref consumers;
  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. if (wait_for_completion_timeout(&fw->c, HZ)) {
  50. ret = 0;
  51. } else {
  52. ret = -ETIMEDOUT;
  53. WARN_ONCE(1, "Firmware transaction timeout");
  54. }
  55. } else {
  56. dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
  57. }
  58. mutex_unlock(&transaction_lock);
  59. return ret;
  60. }
  61. /**
  62. * rpi_firmware_property_list - Submit firmware property list
  63. * @fw: Pointer to firmware structure from rpi_firmware_get().
  64. * @data: Buffer holding tags.
  65. * @tag_size: Size of tags buffer.
  66. *
  67. * Submits a set of concatenated tags to the VPU firmware through the
  68. * mailbox property interface.
  69. *
  70. * The buffer header and the ending tag are added by this function and
  71. * don't need to be supplied, just the actual tags for your operation.
  72. * See struct rpi_firmware_property_tag_header for the per-tag
  73. * structure.
  74. */
  75. int rpi_firmware_property_list(struct rpi_firmware *fw,
  76. void *data, size_t tag_size)
  77. {
  78. size_t size = tag_size + 12;
  79. u32 *buf;
  80. dma_addr_t bus_addr;
  81. int ret;
  82. /* Packets are processed a dword at a time. */
  83. if (size & 3)
  84. return -EINVAL;
  85. buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
  86. GFP_ATOMIC);
  87. if (!buf)
  88. return -ENOMEM;
  89. /* The firmware will error out without parsing in this case. */
  90. WARN_ON(size >= 1024 * 1024);
  91. buf[0] = size;
  92. buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
  93. memcpy(&buf[2], data, tag_size);
  94. buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
  95. wmb();
  96. ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
  97. rmb();
  98. memcpy(data, &buf[2], tag_size);
  99. if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
  100. /*
  101. * The tag name here might not be the one causing the
  102. * error, if there were multiple tags in the request.
  103. * But single-tag is the most common, so go with it.
  104. */
  105. dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
  106. buf[2], buf[1]);
  107. ret = -EINVAL;
  108. }
  109. dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
  113. /**
  114. * rpi_firmware_property - Submit single firmware property
  115. * @fw: Pointer to firmware structure from rpi_firmware_get().
  116. * @tag: One of enum_mbox_property_tag.
  117. * @tag_data: Tag data buffer.
  118. * @buf_size: Buffer size.
  119. *
  120. * Submits a single tag to the VPU firmware through the mailbox
  121. * property interface.
  122. *
  123. * This is a convenience wrapper around
  124. * rpi_firmware_property_list() to avoid some of the
  125. * boilerplate in property calls.
  126. */
  127. int rpi_firmware_property(struct rpi_firmware *fw,
  128. u32 tag, void *tag_data, size_t buf_size)
  129. {
  130. struct rpi_firmware_property_tag_header *header;
  131. int ret;
  132. /* Some mailboxes can use over 1k bytes. Rather than checking
  133. * size and using stack or kmalloc depending on requirements,
  134. * just use kmalloc. Mailboxes don't get called enough to worry
  135. * too much about the time taken in the allocation.
  136. */
  137. void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
  138. if (!data)
  139. return -ENOMEM;
  140. header = data;
  141. header->tag = tag;
  142. header->buf_size = buf_size;
  143. header->req_resp_size = 0;
  144. memcpy(data + sizeof(*header), tag_data, buf_size);
  145. ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
  146. memcpy(tag_data, data + sizeof(*header), buf_size);
  147. kfree(data);
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(rpi_firmware_property);
  151. static void
  152. rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
  153. {
  154. time64_t date_and_time;
  155. u32 packet;
  156. int ret = rpi_firmware_property(fw,
  157. RPI_FIRMWARE_GET_FIRMWARE_REVISION,
  158. &packet, sizeof(packet));
  159. if (ret)
  160. return;
  161. /* This is not compatible with y2038 */
  162. date_and_time = packet;
  163. dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
  164. }
  165. static void
  166. rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
  167. {
  168. u32 packet;
  169. int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
  170. &packet, sizeof(packet));
  171. if (ret)
  172. return;
  173. rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
  174. -1, NULL, 0);
  175. }
  176. static void rpi_register_clk_driver(struct device *dev)
  177. {
  178. struct device_node *firmware;
  179. /*
  180. * Earlier DTs don't have a node for the firmware clocks but
  181. * rely on us creating a platform device by hand. If we do
  182. * have a node for the firmware clocks, just bail out here.
  183. */
  184. firmware = of_get_compatible_child(dev->of_node,
  185. "raspberrypi,firmware-clocks");
  186. if (firmware) {
  187. of_node_put(firmware);
  188. return;
  189. }
  190. rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
  191. -1, NULL, 0);
  192. }
  193. static void rpi_firmware_delete(struct kref *kref)
  194. {
  195. struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
  196. consumers);
  197. mbox_free_channel(fw->chan);
  198. kfree(fw);
  199. }
  200. void rpi_firmware_put(struct rpi_firmware *fw)
  201. {
  202. kref_put(&fw->consumers, rpi_firmware_delete);
  203. }
  204. EXPORT_SYMBOL_GPL(rpi_firmware_put);
  205. static int rpi_firmware_probe(struct platform_device *pdev)
  206. {
  207. struct device *dev = &pdev->dev;
  208. struct rpi_firmware *fw;
  209. /*
  210. * Memory will be freed by rpi_firmware_delete() once all users have
  211. * released their firmware handles. Don't use devm_kzalloc() here.
  212. */
  213. fw = kzalloc(sizeof(*fw), GFP_KERNEL);
  214. if (!fw)
  215. return -ENOMEM;
  216. fw->cl.dev = dev;
  217. fw->cl.rx_callback = response_callback;
  218. fw->cl.tx_block = true;
  219. fw->chan = mbox_request_channel(&fw->cl, 0);
  220. if (IS_ERR(fw->chan)) {
  221. int ret = PTR_ERR(fw->chan);
  222. if (ret != -EPROBE_DEFER)
  223. dev_err(dev, "Failed to get mbox channel: %d\n", ret);
  224. return ret;
  225. }
  226. init_completion(&fw->c);
  227. kref_init(&fw->consumers);
  228. platform_set_drvdata(pdev, fw);
  229. rpi_firmware_print_firmware_revision(fw);
  230. rpi_register_hwmon_driver(dev, fw);
  231. rpi_register_clk_driver(dev);
  232. return 0;
  233. }
  234. static void rpi_firmware_shutdown(struct platform_device *pdev)
  235. {
  236. struct rpi_firmware *fw = platform_get_drvdata(pdev);
  237. if (!fw)
  238. return;
  239. rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
  240. }
  241. static int rpi_firmware_remove(struct platform_device *pdev)
  242. {
  243. struct rpi_firmware *fw = platform_get_drvdata(pdev);
  244. platform_device_unregister(rpi_hwmon);
  245. rpi_hwmon = NULL;
  246. platform_device_unregister(rpi_clk);
  247. rpi_clk = NULL;
  248. rpi_firmware_put(fw);
  249. return 0;
  250. }
  251. /**
  252. * rpi_firmware_get - Get pointer to rpi_firmware structure.
  253. * @firmware_node: Pointer to the firmware Device Tree node.
  254. *
  255. * The reference to rpi_firmware has to be released with rpi_firmware_put().
  256. *
  257. * Returns NULL is the firmware device is not ready.
  258. */
  259. struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
  260. {
  261. struct platform_device *pdev = of_find_device_by_node(firmware_node);
  262. struct rpi_firmware *fw;
  263. if (!pdev)
  264. return NULL;
  265. fw = platform_get_drvdata(pdev);
  266. if (!fw)
  267. goto err_put_device;
  268. if (!kref_get_unless_zero(&fw->consumers))
  269. goto err_put_device;
  270. put_device(&pdev->dev);
  271. return fw;
  272. err_put_device:
  273. put_device(&pdev->dev);
  274. return NULL;
  275. }
  276. EXPORT_SYMBOL_GPL(rpi_firmware_get);
  277. static const struct of_device_id rpi_firmware_of_match[] = {
  278. { .compatible = "raspberrypi,bcm2835-firmware", },
  279. {},
  280. };
  281. MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
  282. static struct platform_driver rpi_firmware_driver = {
  283. .driver = {
  284. .name = "raspberrypi-firmware",
  285. .of_match_table = rpi_firmware_of_match,
  286. },
  287. .probe = rpi_firmware_probe,
  288. .shutdown = rpi_firmware_shutdown,
  289. .remove = rpi_firmware_remove,
  290. };
  291. module_platform_driver(rpi_firmware_driver);
  292. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  293. MODULE_DESCRIPTION("Raspberry Pi firmware driver");
  294. MODULE_LICENSE("GPL v2");