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