Linux 4.19.133
[linux/fpc-iii.git] / drivers / firmware / raspberrypi.c
blob44eb99807e337c20b6e72c7ec712593ea85629dd
1 /*
2 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
3 * property channel.
5 * Copyright © 2015 Broadcom
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/dma-mapping.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <soc/bcm2835/raspberrypi-firmware.h>
20 #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
21 #define MBOX_CHAN(msg) ((msg) & 0xf)
22 #define MBOX_DATA28(msg) ((msg) & ~0xf)
23 #define MBOX_CHAN_PROPERTY 8
25 static struct platform_device *rpi_hwmon;
27 struct rpi_firmware {
28 struct mbox_client cl;
29 struct mbox_chan *chan; /* The property channel. */
30 struct completion c;
31 u32 enabled;
34 static DEFINE_MUTEX(transaction_lock);
36 static void response_callback(struct mbox_client *cl, void *msg)
38 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
39 complete(&fw->c);
43 * Sends a request to the firmware through the BCM2835 mailbox driver,
44 * and synchronously waits for the reply.
46 static int
47 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
49 u32 message = MBOX_MSG(chan, data);
50 int ret;
52 WARN_ON(data & 0xf);
54 mutex_lock(&transaction_lock);
55 reinit_completion(&fw->c);
56 ret = mbox_send_message(fw->chan, &message);
57 if (ret >= 0) {
58 wait_for_completion(&fw->c);
59 ret = 0;
60 } else {
61 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
63 mutex_unlock(&transaction_lock);
65 return ret;
68 /**
69 * rpi_firmware_property_list - Submit firmware property list
70 * @fw: Pointer to firmware structure from rpi_firmware_get().
71 * @data: Buffer holding tags.
72 * @tag_size: Size of tags buffer.
74 * Submits a set of concatenated tags to the VPU firmware through the
75 * mailbox property interface.
77 * The buffer header and the ending tag are added by this function and
78 * don't need to be supplied, just the actual tags for your operation.
79 * See struct rpi_firmware_property_tag_header for the per-tag
80 * structure.
82 int rpi_firmware_property_list(struct rpi_firmware *fw,
83 void *data, size_t tag_size)
85 size_t size = tag_size + 12;
86 u32 *buf;
87 dma_addr_t bus_addr;
88 int ret;
90 /* Packets are processed a dword at a time. */
91 if (size & 3)
92 return -EINVAL;
94 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
95 GFP_ATOMIC);
96 if (!buf)
97 return -ENOMEM;
99 /* The firmware will error out without parsing in this case. */
100 WARN_ON(size >= 1024 * 1024);
102 buf[0] = size;
103 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
104 memcpy(&buf[2], data, tag_size);
105 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
106 wmb();
108 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
110 rmb();
111 memcpy(data, &buf[2], tag_size);
112 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
114 * The tag name here might not be the one causing the
115 * error, if there were multiple tags in the request.
116 * But single-tag is the most common, so go with it.
118 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
119 buf[2], buf[1]);
120 ret = -EINVAL;
123 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
125 return ret;
127 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
130 * rpi_firmware_property - Submit single firmware property
131 * @fw: Pointer to firmware structure from rpi_firmware_get().
132 * @tag: One of enum_mbox_property_tag.
133 * @tag_data: Tag data buffer.
134 * @buf_size: Buffer size.
136 * Submits a single tag to the VPU firmware through the mailbox
137 * property interface.
139 * This is a convenience wrapper around
140 * rpi_firmware_property_list() to avoid some of the
141 * boilerplate in property calls.
143 int rpi_firmware_property(struct rpi_firmware *fw,
144 u32 tag, void *tag_data, size_t buf_size)
146 struct rpi_firmware_property_tag_header *header;
147 int ret;
149 /* Some mailboxes can use over 1k bytes. Rather than checking
150 * size and using stack or kmalloc depending on requirements,
151 * just use kmalloc. Mailboxes don't get called enough to worry
152 * too much about the time taken in the allocation.
154 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
156 if (!data)
157 return -ENOMEM;
159 header = data;
160 header->tag = tag;
161 header->buf_size = buf_size;
162 header->req_resp_size = 0;
163 memcpy(data + sizeof(*header), tag_data, buf_size);
165 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
167 memcpy(tag_data, data + sizeof(*header), buf_size);
169 kfree(data);
171 return ret;
173 EXPORT_SYMBOL_GPL(rpi_firmware_property);
175 static void
176 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
178 u32 packet;
179 int ret = rpi_firmware_property(fw,
180 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
181 &packet, sizeof(packet));
183 if (ret == 0) {
184 struct tm tm;
186 time64_to_tm(packet, 0, &tm);
188 dev_info(fw->cl.dev,
189 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
190 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
191 tm.tm_hour, tm.tm_min);
195 static void
196 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
198 u32 packet;
199 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
200 &packet, sizeof(packet));
202 if (ret)
203 return;
205 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
206 -1, NULL, 0);
209 static int rpi_firmware_probe(struct platform_device *pdev)
211 struct device *dev = &pdev->dev;
212 struct rpi_firmware *fw;
214 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
215 if (!fw)
216 return -ENOMEM;
218 fw->cl.dev = dev;
219 fw->cl.rx_callback = response_callback;
220 fw->cl.tx_block = true;
222 fw->chan = mbox_request_channel(&fw->cl, 0);
223 if (IS_ERR(fw->chan)) {
224 int ret = PTR_ERR(fw->chan);
225 if (ret != -EPROBE_DEFER)
226 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
227 return ret;
230 init_completion(&fw->c);
232 platform_set_drvdata(pdev, fw);
234 rpi_firmware_print_firmware_revision(fw);
235 rpi_register_hwmon_driver(dev, fw);
237 return 0;
240 static int rpi_firmware_remove(struct platform_device *pdev)
242 struct rpi_firmware *fw = platform_get_drvdata(pdev);
244 platform_device_unregister(rpi_hwmon);
245 rpi_hwmon = NULL;
246 mbox_free_channel(fw->chan);
248 return 0;
252 * rpi_firmware_get - Get pointer to rpi_firmware structure.
253 * @firmware_node: Pointer to the firmware Device Tree node.
255 * Returns NULL is the firmware device is not ready.
257 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
259 struct platform_device *pdev = of_find_device_by_node(firmware_node);
261 if (!pdev)
262 return NULL;
264 return platform_get_drvdata(pdev);
266 EXPORT_SYMBOL_GPL(rpi_firmware_get);
268 static const struct of_device_id rpi_firmware_of_match[] = {
269 { .compatible = "raspberrypi,bcm2835-firmware", },
272 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
274 static struct platform_driver rpi_firmware_driver = {
275 .driver = {
276 .name = "raspberrypi-firmware",
277 .of_match_table = rpi_firmware_of_match,
279 .probe = rpi_firmware_probe,
280 .remove = rpi_firmware_remove,
282 module_platform_driver(rpi_firmware_driver);
284 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
285 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
286 MODULE_LICENSE("GPL v2");