14e3d6065SEric Anholt /*
24e3d6065SEric Anholt  * Defines interfaces for interacting wtih the Raspberry Pi firmware's
34e3d6065SEric Anholt  * property channel.
44e3d6065SEric Anholt  *
54e3d6065SEric Anholt  * Copyright © 2015 Broadcom
64e3d6065SEric Anholt  *
74e3d6065SEric Anholt  * This program is free software; you can redistribute it and/or modify
84e3d6065SEric Anholt  * it under the terms of the GNU General Public License version 2 as
94e3d6065SEric Anholt  * published by the Free Software Foundation.
104e3d6065SEric Anholt  */
114e3d6065SEric Anholt 
124e3d6065SEric Anholt #include <linux/dma-mapping.h>
134e3d6065SEric Anholt #include <linux/mailbox_client.h>
144e3d6065SEric Anholt #include <linux/module.h>
154e3d6065SEric Anholt #include <linux/of_platform.h>
164e3d6065SEric Anholt #include <linux/platform_device.h>
174e3d6065SEric Anholt #include <soc/bcm2835/raspberrypi-firmware.h>
184e3d6065SEric Anholt 
194e3d6065SEric Anholt #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
204e3d6065SEric Anholt #define MBOX_CHAN(msg)			((msg) & 0xf)
214e3d6065SEric Anholt #define MBOX_DATA28(msg)		((msg) & ~0xf)
224e3d6065SEric Anholt #define MBOX_CHAN_PROPERTY		8
234e3d6065SEric Anholt 
24a1547e0bSKees Cook #define MAX_RPI_FW_PROP_BUF_SIZE	32
25a1547e0bSKees Cook 
2670eea1bbSStefan Wahren static struct platform_device *rpi_hwmon;
2770eea1bbSStefan Wahren 
284e3d6065SEric Anholt struct rpi_firmware {
294e3d6065SEric Anholt 	struct mbox_client cl;
304e3d6065SEric Anholt 	struct mbox_chan *chan; /* The property channel. */
314e3d6065SEric Anholt 	struct completion c;
324e3d6065SEric Anholt 	u32 enabled;
334e3d6065SEric Anholt };
344e3d6065SEric Anholt 
354e3d6065SEric Anholt static DEFINE_MUTEX(transaction_lock);
364e3d6065SEric Anholt 
374e3d6065SEric Anholt static void response_callback(struct mbox_client *cl, void *msg)
384e3d6065SEric Anholt {
394e3d6065SEric Anholt 	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
404e3d6065SEric Anholt 	complete(&fw->c);
414e3d6065SEric Anholt }
424e3d6065SEric Anholt 
434e3d6065SEric Anholt /*
444e3d6065SEric Anholt  * Sends a request to the firmware through the BCM2835 mailbox driver,
454e3d6065SEric Anholt  * and synchronously waits for the reply.
464e3d6065SEric Anholt  */
474e3d6065SEric Anholt static int
484e3d6065SEric Anholt rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
494e3d6065SEric Anholt {
504e3d6065SEric Anholt 	u32 message = MBOX_MSG(chan, data);
514e3d6065SEric Anholt 	int ret;
524e3d6065SEric Anholt 
534e3d6065SEric Anholt 	WARN_ON(data & 0xf);
544e3d6065SEric Anholt 
554e3d6065SEric Anholt 	mutex_lock(&transaction_lock);
564e3d6065SEric Anholt 	reinit_completion(&fw->c);
574e3d6065SEric Anholt 	ret = mbox_send_message(fw->chan, &message);
584e3d6065SEric Anholt 	if (ret >= 0) {
594e3d6065SEric Anholt 		wait_for_completion(&fw->c);
604e3d6065SEric Anholt 		ret = 0;
614e3d6065SEric Anholt 	} else {
624e3d6065SEric Anholt 		dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
634e3d6065SEric Anholt 	}
644e3d6065SEric Anholt 	mutex_unlock(&transaction_lock);
654e3d6065SEric Anholt 
664e3d6065SEric Anholt 	return ret;
674e3d6065SEric Anholt }
684e3d6065SEric Anholt 
694e3d6065SEric Anholt /**
704e3d6065SEric Anholt  * rpi_firmware_property_list - Submit firmware property list
714e3d6065SEric Anholt  * @fw:		Pointer to firmware structure from rpi_firmware_get().
724e3d6065SEric Anholt  * @data:	Buffer holding tags.
734e3d6065SEric Anholt  * @tag_size:	Size of tags buffer.
744e3d6065SEric Anholt  *
754e3d6065SEric Anholt  * Submits a set of concatenated tags to the VPU firmware through the
764e3d6065SEric Anholt  * mailbox property interface.
774e3d6065SEric Anholt  *
784e3d6065SEric Anholt  * The buffer header and the ending tag are added by this function and
794e3d6065SEric Anholt  * don't need to be supplied, just the actual tags for your operation.
804e3d6065SEric Anholt  * See struct rpi_firmware_property_tag_header for the per-tag
814e3d6065SEric Anholt  * structure.
824e3d6065SEric Anholt  */
834e3d6065SEric Anholt int rpi_firmware_property_list(struct rpi_firmware *fw,
844e3d6065SEric Anholt 			       void *data, size_t tag_size)
854e3d6065SEric Anholt {
864e3d6065SEric Anholt 	size_t size = tag_size + 12;
874e3d6065SEric Anholt 	u32 *buf;
884e3d6065SEric Anholt 	dma_addr_t bus_addr;
894e3d6065SEric Anholt 	int ret;
904e3d6065SEric Anholt 
914e3d6065SEric Anholt 	/* Packets are processed a dword at a time. */
924e3d6065SEric Anholt 	if (size & 3)
934e3d6065SEric Anholt 		return -EINVAL;
944e3d6065SEric Anholt 
954e3d6065SEric Anholt 	buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
964e3d6065SEric Anholt 				 GFP_ATOMIC);
974e3d6065SEric Anholt 	if (!buf)
984e3d6065SEric Anholt 		return -ENOMEM;
994e3d6065SEric Anholt 
1004e3d6065SEric Anholt 	/* The firmware will error out without parsing in this case. */
1014e3d6065SEric Anholt 	WARN_ON(size >= 1024 * 1024);
1024e3d6065SEric Anholt 
1034e3d6065SEric Anholt 	buf[0] = size;
1044e3d6065SEric Anholt 	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
1054e3d6065SEric Anholt 	memcpy(&buf[2], data, tag_size);
1064e3d6065SEric Anholt 	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
1074e3d6065SEric Anholt 	wmb();
1084e3d6065SEric Anholt 
1094e3d6065SEric Anholt 	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
1104e3d6065SEric Anholt 
1114e3d6065SEric Anholt 	rmb();
1124e3d6065SEric Anholt 	memcpy(data, &buf[2], tag_size);
1134e3d6065SEric Anholt 	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
1144e3d6065SEric Anholt 		/*
1154e3d6065SEric Anholt 		 * The tag name here might not be the one causing the
1164e3d6065SEric Anholt 		 * error, if there were multiple tags in the request.
1174e3d6065SEric Anholt 		 * But single-tag is the most common, so go with it.
1184e3d6065SEric Anholt 		 */
1194e3d6065SEric Anholt 		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
1204e3d6065SEric Anholt 			buf[2], buf[1]);
1214e3d6065SEric Anholt 		ret = -EINVAL;
1224e3d6065SEric Anholt 	}
1234e3d6065SEric Anholt 
1244e3d6065SEric Anholt 	dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
1254e3d6065SEric Anholt 
1264e3d6065SEric Anholt 	return ret;
1274e3d6065SEric Anholt }
1284e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
1294e3d6065SEric Anholt 
1304e3d6065SEric Anholt /**
1314e3d6065SEric Anholt  * rpi_firmware_property - Submit single firmware property
1324e3d6065SEric Anholt  * @fw:		Pointer to firmware structure from rpi_firmware_get().
1334e3d6065SEric Anholt  * @tag:	One of enum_mbox_property_tag.
1344e3d6065SEric Anholt  * @tag_data:	Tag data buffer.
1354e3d6065SEric Anholt  * @buf_size:	Buffer size.
1364e3d6065SEric Anholt  *
1374e3d6065SEric Anholt  * Submits a single tag to the VPU firmware through the mailbox
1384e3d6065SEric Anholt  * property interface.
1394e3d6065SEric Anholt  *
1404e3d6065SEric Anholt  * This is a convenience wrapper around
1414e3d6065SEric Anholt  * rpi_firmware_property_list() to avoid some of the
1424e3d6065SEric Anholt  * boilerplate in property calls.
1434e3d6065SEric Anholt  */
1444e3d6065SEric Anholt int rpi_firmware_property(struct rpi_firmware *fw,
1454e3d6065SEric Anholt 			  u32 tag, void *tag_data, size_t buf_size)
1464e3d6065SEric Anholt {
1474e3d6065SEric Anholt 	/* Single tags are very small (generally 8 bytes), so the
1484e3d6065SEric Anholt 	 * stack should be safe.
1494e3d6065SEric Anholt 	 */
150a1547e0bSKees Cook 	u8 data[sizeof(struct rpi_firmware_property_tag_header) +
151a1547e0bSKees Cook 		MAX_RPI_FW_PROP_BUF_SIZE];
1524e3d6065SEric Anholt 	struct rpi_firmware_property_tag_header *header =
1534e3d6065SEric Anholt 		(struct rpi_firmware_property_tag_header *)data;
1544e3d6065SEric Anholt 	int ret;
1554e3d6065SEric Anholt 
156a1547e0bSKees Cook 	if (WARN_ON(buf_size > sizeof(data) - sizeof(*header)))
157a1547e0bSKees Cook 		return -EINVAL;
158a1547e0bSKees Cook 
1594e3d6065SEric Anholt 	header->tag = tag;
1604e3d6065SEric Anholt 	header->buf_size = buf_size;
1614e3d6065SEric Anholt 	header->req_resp_size = 0;
1624e3d6065SEric Anholt 	memcpy(data + sizeof(struct rpi_firmware_property_tag_header),
1634e3d6065SEric Anholt 	       tag_data, buf_size);
1644e3d6065SEric Anholt 
165a1547e0bSKees Cook 	ret = rpi_firmware_property_list(fw, &data, buf_size + sizeof(*header));
1664e3d6065SEric Anholt 	memcpy(tag_data,
1674e3d6065SEric Anholt 	       data + sizeof(struct rpi_firmware_property_tag_header),
1684e3d6065SEric Anholt 	       buf_size);
1694e3d6065SEric Anholt 
1704e3d6065SEric Anholt 	return ret;
1714e3d6065SEric Anholt }
1724e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_property);
1734e3d6065SEric Anholt 
1744e3d6065SEric Anholt static void
1754e3d6065SEric Anholt rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
1764e3d6065SEric Anholt {
1774e3d6065SEric Anholt 	u32 packet;
1784e3d6065SEric Anholt 	int ret = rpi_firmware_property(fw,
1794e3d6065SEric Anholt 					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
1804e3d6065SEric Anholt 					&packet, sizeof(packet));
1814e3d6065SEric Anholt 
1824e3d6065SEric Anholt 	if (ret == 0) {
1834e3d6065SEric Anholt 		struct tm tm;
1844e3d6065SEric Anholt 
1859107ee6aSArnd Bergmann 		time64_to_tm(packet, 0, &tm);
1864e3d6065SEric Anholt 
1874e3d6065SEric Anholt 		dev_info(fw->cl.dev,
1884e3d6065SEric Anholt 			 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
1894e3d6065SEric Anholt 			 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
1904e3d6065SEric Anholt 			 tm.tm_hour, tm.tm_min);
1914e3d6065SEric Anholt 	}
1924e3d6065SEric Anholt }
1934e3d6065SEric Anholt 
19470eea1bbSStefan Wahren static void
19570eea1bbSStefan Wahren rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
19670eea1bbSStefan Wahren {
19770eea1bbSStefan Wahren 	u32 packet;
19870eea1bbSStefan Wahren 	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
19970eea1bbSStefan Wahren 					&packet, sizeof(packet));
20070eea1bbSStefan Wahren 
20170eea1bbSStefan Wahren 	if (ret)
20270eea1bbSStefan Wahren 		return;
20370eea1bbSStefan Wahren 
20470eea1bbSStefan Wahren 	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
20570eea1bbSStefan Wahren 						  -1, NULL, 0);
20670eea1bbSStefan Wahren }
20770eea1bbSStefan Wahren 
2084e3d6065SEric Anholt static int rpi_firmware_probe(struct platform_device *pdev)
2094e3d6065SEric Anholt {
2104e3d6065SEric Anholt 	struct device *dev = &pdev->dev;
2114e3d6065SEric Anholt 	struct rpi_firmware *fw;
2124e3d6065SEric Anholt 
2134e3d6065SEric Anholt 	fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
2144e3d6065SEric Anholt 	if (!fw)
2154e3d6065SEric Anholt 		return -ENOMEM;
2164e3d6065SEric Anholt 
2174e3d6065SEric Anholt 	fw->cl.dev = dev;
2184e3d6065SEric Anholt 	fw->cl.rx_callback = response_callback;
2194e3d6065SEric Anholt 	fw->cl.tx_block = true;
2204e3d6065SEric Anholt 
2214e3d6065SEric Anholt 	fw->chan = mbox_request_channel(&fw->cl, 0);
2224e3d6065SEric Anholt 	if (IS_ERR(fw->chan)) {
2234e3d6065SEric Anholt 		int ret = PTR_ERR(fw->chan);
2244e3d6065SEric Anholt 		if (ret != -EPROBE_DEFER)
2254e3d6065SEric Anholt 			dev_err(dev, "Failed to get mbox channel: %d\n", ret);
2264e3d6065SEric Anholt 		return ret;
2274e3d6065SEric Anholt 	}
2284e3d6065SEric Anholt 
2294e3d6065SEric Anholt 	init_completion(&fw->c);
2304e3d6065SEric Anholt 
2314e3d6065SEric Anholt 	platform_set_drvdata(pdev, fw);
2324e3d6065SEric Anholt 
2334e3d6065SEric Anholt 	rpi_firmware_print_firmware_revision(fw);
23470eea1bbSStefan Wahren 	rpi_register_hwmon_driver(dev, fw);
2354e3d6065SEric Anholt 
2364e3d6065SEric Anholt 	return 0;
2374e3d6065SEric Anholt }
2384e3d6065SEric Anholt 
2394e3d6065SEric Anholt static int rpi_firmware_remove(struct platform_device *pdev)
2404e3d6065SEric Anholt {
2414e3d6065SEric Anholt 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
2424e3d6065SEric Anholt 
24370eea1bbSStefan Wahren 	platform_device_unregister(rpi_hwmon);
24470eea1bbSStefan Wahren 	rpi_hwmon = NULL;
2454e3d6065SEric Anholt 	mbox_free_channel(fw->chan);
2464e3d6065SEric Anholt 
2474e3d6065SEric Anholt 	return 0;
2484e3d6065SEric Anholt }
2494e3d6065SEric Anholt 
2504e3d6065SEric Anholt /**
2514e3d6065SEric Anholt  * rpi_firmware_get - Get pointer to rpi_firmware structure.
2524e3d6065SEric Anholt  * @firmware_node:    Pointer to the firmware Device Tree node.
2534e3d6065SEric Anholt  *
2544e3d6065SEric Anholt  * Returns NULL is the firmware device is not ready.
2554e3d6065SEric Anholt  */
2564e3d6065SEric Anholt struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
2574e3d6065SEric Anholt {
2584e3d6065SEric Anholt 	struct platform_device *pdev = of_find_device_by_node(firmware_node);
2594e3d6065SEric Anholt 
2604e3d6065SEric Anholt 	if (!pdev)
2614e3d6065SEric Anholt 		return NULL;
2624e3d6065SEric Anholt 
2634e3d6065SEric Anholt 	return platform_get_drvdata(pdev);
2644e3d6065SEric Anholt }
2654e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_get);
2664e3d6065SEric Anholt 
2674e3d6065SEric Anholt static const struct of_device_id rpi_firmware_of_match[] = {
2684e3d6065SEric Anholt 	{ .compatible = "raspberrypi,bcm2835-firmware", },
2694e3d6065SEric Anholt 	{},
2704e3d6065SEric Anholt };
2714e3d6065SEric Anholt MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
2724e3d6065SEric Anholt 
2734e3d6065SEric Anholt static struct platform_driver rpi_firmware_driver = {
2744e3d6065SEric Anholt 	.driver = {
2754e3d6065SEric Anholt 		.name = "raspberrypi-firmware",
2764e3d6065SEric Anholt 		.of_match_table = rpi_firmware_of_match,
2774e3d6065SEric Anholt 	},
2784e3d6065SEric Anholt 	.probe		= rpi_firmware_probe,
2794e3d6065SEric Anholt 	.remove		= rpi_firmware_remove,
2804e3d6065SEric Anholt };
2814e3d6065SEric Anholt module_platform_driver(rpi_firmware_driver);
2824e3d6065SEric Anholt 
2834e3d6065SEric Anholt MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
2844e3d6065SEric Anholt MODULE_DESCRIPTION("Raspberry Pi firmware driver");
2854e3d6065SEric Anholt MODULE_LICENSE("GPL v2");
286