[PATCH 09/10] ARM: bcm2835: Add the mailbox property channel driver.

Eric Anholt eric at anholt.net
Mon Mar 2 12:54:43 PST 2015


Many of the operations with the firmware are done through this mailbox
channel pair with a specific packet format.  Notably, it's used for
clock control, which is apparently not actually totally possible to do
from the ARM side (some regs aren't addressable).  I need clock
control for the VC4 DRM driver, to turn on the 3D engine.

Signed-off-by: Eric Anholt <eric at anholt.net>
---
 drivers/mailbox/Makefile                         |   1 +
 drivers/mailbox/bcm2835-mailbox-property.c       | 172 +++++++++++++++++++++++
 include/linux/mailbox/bcm2835-mailbox-property.h | 106 ++++++++++++++
 3 files changed, 279 insertions(+)
 create mode 100644 drivers/mailbox/bcm2835-mailbox-property.c
 create mode 100644 include/linux/mailbox/bcm2835-mailbox-property.h

diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile
index 619d815..0318966 100644
--- a/drivers/mailbox/Makefile
+++ b/drivers/mailbox/Makefile
@@ -12,3 +12,4 @@ obj-$(CONFIG_ALTERA_MBOX)	+= mailbox-altera.o
 
 obj-$(CONFIG_BCM2835_MBOX)	+= bcm2835-mailbox.o
 obj-$(CONFIG_BCM2835_MBOX)	+= bcm2835-mailbox-power.o
+obj-$(CONFIG_BCM2835_MBOX)	+= bcm2835-mailbox-property.o
diff --git a/drivers/mailbox/bcm2835-mailbox-property.c b/drivers/mailbox/bcm2835-mailbox-property.c
new file mode 100644
index 0000000..d3590a9
--- /dev/null
+++ b/drivers/mailbox/bcm2835-mailbox-property.c
@@ -0,0 +1,172 @@
+/*
+ *  Copyright © 2015 Broadcom
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+/*
+ * Defines a module for accessing the property channel of the
+ * BCM2835 mailbox.
+ *
+ * The property interface lets you submit a bus address for a
+ * sequence of command packets to the firmware.
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/mailbox_client.h>
+#include <linux/mailbox/bcm2835-mailbox-property.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+static DEFINE_MUTEX(property_lock);
+
+struct bcm_mbox_property {
+	struct device *dev;
+	struct mbox_client cl;
+	struct mbox_chan *chan;
+	struct completion c;
+};
+
+struct bcm_mbox_property *mbox_property;
+
+static void response_callback(struct mbox_client *cl, void *mssg)
+{
+	complete(&mbox_property->c);
+}
+
+/*
+ * Requests the actual property transaction through the BCM2835
+ * mailbox driver.
+ */
+static int
+bcm_mbox_property_transaction(dma_addr_t bus_addr)
+{
+	int ret;
+
+	reinit_completion(&mbox_property->c);
+	ret = mbox_send_message(mbox_property->chan, (void *)bus_addr);
+	if (ret >= 0) {
+		wait_for_completion(&mbox_property->c);
+		ret = 0;
+	} else {
+		dev_err(mbox_property->dev, "mbox_send_message returned %d\n",
+			ret);
+	}
+
+	return ret;
+}
+
+/*
+ * Submits a set of concatenated tags to the VPU firmware through the
+ * mailbox property interface.
+ *
+ * The buffer header and the ending tag are added by this function and
+ * don't need to be supplied, just the actual tags for your operation.
+ * See struct bcm_mbox_property_tag_header for the per-tag structure.
+ */
+int bcm_mbox_property(void *data, size_t tag_size)
+{
+	size_t size = tag_size + 12;
+	void __iomem *buf;
+	dma_addr_t bus_addr;
+	int ret = 0;
+
+	/* Packets are processed a dword at a time. */
+	if (size & 3)
+		return -EINVAL;
+
+	buf = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &bus_addr, GFP_ATOMIC);
+	if (!buf)
+		return -ENOMEM;
+
+	/* The firmware will error out without parsing in this case. */
+	WARN_ON(size >= 1024 * 1024);
+
+	writel(size, buf);
+	writel(bcm_mbox_status_request, buf + 4);
+	memcpy_toio(buf + 8, data, tag_size);
+	writel(bcm_mbox_property_end, buf + size - 4);
+
+	mutex_lock(&property_lock);
+	ret = bcm_mbox_property_transaction(bus_addr);
+	mutex_unlock(&property_lock);
+
+	memcpy_fromio(data, buf + 8, tag_size);
+	if (readl(buf + 4) != bcm_mbox_status_success) {
+		/*
+		 * The tag name here might not be the one causing the
+		 * error, if there were multiple tags in the request.
+		 * But single-tag is the most common, so go with it.
+		 */
+		dev_err(mbox_property->dev,
+			"Request 0x%08x returned status 0x%08x\n",
+			readl(buf + 8), readl(buf + 4));
+		ret = -EINVAL;
+	}
+
+	dma_free_coherent(NULL, PAGE_ALIGN(size), buf, bus_addr);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(bcm_mbox_property);
+
+
+static int bcm2835_mbox_property_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	int ret = 0;
+
+	mbox_property = devm_kzalloc(dev, sizeof(*mbox_property),
+				       GFP_KERNEL);
+	if (!mbox_property) {
+		dev_err(dev, "Failed to allocate device memory\n");
+		return -ENOMEM;
+	}
+
+	mbox_property->cl.dev = dev;
+	mbox_property->cl.rx_callback = response_callback;
+	mbox_property->cl.tx_block = true;
+
+	mbox_property->chan = mbox_request_channel(&mbox_property->cl, 0);
+	if (!mbox_property->chan) {
+		dev_err(dev, "Failed to get mbox channel\n");
+		return -ENODEV;
+	}
+
+	init_completion(&mbox_property->c);
+
+	platform_set_drvdata(pdev, mbox_property);
+	mbox_property->dev = dev;
+
+	return ret;
+}
+
+static int bcm2835_mbox_property_remove(struct platform_device *pdev)
+{
+	mbox_free_channel(mbox_property->chan);
+
+	return 0;
+}
+
+static const struct of_device_id bcm2835_mbox_property_of_match[] = {
+	{ .compatible = "brcm,bcm2835-mbox-property", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, bcm2835_mbox_property_of_match);
+
+static struct platform_driver bcm2835_mbox_property_driver = {
+	.driver = {
+		.name = "bcm2835-mbox-property",
+		.owner = THIS_MODULE,
+		.of_match_table = bcm2835_mbox_property_of_match,
+	},
+	.probe		= bcm2835_mbox_property_probe,
+	.remove		= bcm2835_mbox_property_remove,
+};
+module_platform_driver(bcm2835_mbox_property_driver);
+
+MODULE_AUTHOR("Eric Anholt <eric at anholt.net>");
+MODULE_DESCRIPTION("BCM2835 mailbox property channel");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/mailbox/bcm2835-mailbox-property.h b/include/linux/mailbox/bcm2835-mailbox-property.h
new file mode 100644
index 0000000..783c069
--- /dev/null
+++ b/include/linux/mailbox/bcm2835-mailbox-property.h
@@ -0,0 +1,106 @@
+/*
+ *  Copyright © 2015 Broadcom
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+enum bcm_mbox_property_status {
+	bcm_mbox_status_request = 0,
+	bcm_mbox_status_success = 0x80000000,
+	bcm_mbox_status_error =   0x80000001,
+};
+
+struct bcm_mbox_property_tag_header {
+	/* One of enum_mbox_property_tag. */
+	u32 tag;
+
+	/* The number of bytes in the value buffer following this
+	 * struct.
+	 */
+	u32 buf_size;
+
+	/*
+	 * On submit, the length of the request (though it doesn't
+	 * appear to be currently used by the firmware).  On return,
+	 * the length of the response (always 4 byte aligned), with
+	 * the low bit set.
+	 */
+	u32 req_resp_size;
+};
+
+enum bcm_mbox_property_tag {
+	bcm_mbox_property_end = 0,
+	bcm_mbox_get_firmware_revision =                  0x00000001,
+
+	bcm_mbox_set_cursor_info =                        0x00008010,
+	bcm_mbox_set_cursor_state =                       0x00008011,
+
+	bcm_mbox_get_board_model =                        0x00010001,
+	bcm_mbox_get_board_revision =                     0x00010002,
+	bcm_mbox_get_board_mac_address =                  0x00010003,
+	bcm_mbox_get_board_serial =                       0x00010004,
+	bcm_mbox_get_arm_memory =                         0x00010005,
+	bcm_mbox_get_vc_memory =                          0x00010006,
+	bcm_mbox_get_clocks =                             0x00010007,
+	bcm_mbox_get_power_state =                        0x00020001,
+	bcm_mbox_get_timing =                             0x00020002,
+	bcm_mbox_set_power_state =                        0x00028001,
+	bcm_mbox_get_clock_state =                        0x00030001,
+	bcm_mbox_get_clock_rate =                         0x00030002,
+	bcm_mbox_get_voltage =                            0x00030003,
+	bcm_mbox_get_max_clock_rate =                     0x00030004,
+	bcm_mbox_get_max_voltage =                        0x00030005,
+	bcm_mbox_get_temperature =                        0x00030006,
+	bcm_mbox_get_min_clock_rate =                     0x00030007,
+	bcm_mbox_get_min_voltage =                        0x00030008,
+	bcm_mbox_get_turbo =                              0x00030009,
+	bcm_mbox_get_max_temperature =                    0x0003000a,
+	bcm_mbox_allocate_memory =                        0x0003000c,
+	bcm_mbox_lock_memory =                            0x0003000d,
+	bcm_mbox_unlock_memory =                          0x0003000e,
+	bcm_mbox_release_memory =                         0x0003000f,
+	bcm_mbox_execute_code =                           0x00030010,
+	bcm_mbox_get_dispmanx_resource_mem_handle =       0x00030014,
+	bcm_mbox_get_edid_block =                         0x00030020,
+	bcm_mbox_set_clock_state =                        0x00038001,
+	bcm_mbox_set_clock_rate =                         0x00038002,
+	bcm_mbox_set_voltage =                            0x00038003,
+	bcm_mbox_set_turbo =                              0x00038009,
+
+	/* Dispmanx tags */
+	bcm_mbox_framebuffer_allocate =                   0x00040001,
+	bcm_mbox_framebuffer_blank =                      0x00040002,
+	bcm_mbox_framebuffer_get_physical_width_height =  0x00040003,
+	bcm_mbox_framebuffer_get_virtual_width_height =   0x00040004,
+	bcm_mbox_framebuffer_get_depth =                  0x00040005,
+	bcm_mbox_framebuffer_get_pixel_order =            0x00040006,
+	bcm_mbox_framebuffer_get_alpha_mode =             0x00040007,
+	bcm_mbox_framebuffer_get_pitch =                  0x00040008,
+	bcm_mbox_framebuffer_get_virtual_offset =         0x00040009,
+	bcm_mbox_framebuffer_get_overscan =               0x0004000a,
+	bcm_mbox_framebuffer_get_palette =                0x0004000b,
+	bcm_mbox_framebuffer_release =                    0x00048001,
+	bcm_mbox_framebuffer_test_physical_width_height = 0x00044003,
+	bcm_mbox_framebuffer_test_virtual_width_height =  0x00044004,
+	bcm_mbox_framebuffer_test_depth =                 0x00044005,
+	bcm_mbox_framebuffer_test_pixel_order =           0x00044006,
+	bcm_mbox_framebuffer_test_alpha_mode =            0x00044007,
+	bcm_mbox_framebuffer_test_virtual_offset =        0x00044009,
+	bcm_mbox_framebuffer_test_overscan =              0x0004400a,
+	bcm_mbox_framebuffer_test_palette =               0x0004400b,
+	bcm_mbox_framebuffer_set_physical_width_height =  0x00048003,
+	bcm_mbox_framebuffer_set_virtual_width_height =   0x00048004,
+	bcm_mbox_framebuffer_set_depth =                  0x00048005,
+	bcm_mbox_framebuffer_set_pixel_order =            0x00048006,
+	bcm_mbox_framebuffer_set_alpha_mode =             0x00048007,
+	bcm_mbox_framebuffer_set_virtual_offset =         0x00048009,
+	bcm_mbox_framebuffer_set_overscan =               0x0004800a,
+	bcm_mbox_framebuffer_set_palette =                0x0004800b,
+
+	bcm_mbox_get_command_line =                       0x00050001,
+	bcm_mbox_get_dma_channels =                       0x00060001,
+};
+
+int bcm_mbox_property(void *data, size_t tag_size);
-- 
2.1.4




More information about the linux-rpi-kernel mailing list