raspberrypi.c 10 KB

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