| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Defines interfaces for interacting wtih the Raspberry Pi firmware's | 
|  | 3 | * property channel. | 
|  | 4 | * | 
|  | 5 | * Copyright © 2015 Broadcom | 
|  | 6 | * | 
|  | 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. | 
|  | 10 | */ | 
|  | 11 |  | 
|  | 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> | 
|  | 19 |  | 
|  | 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 | 
|  | 24 |  | 
|  | 25 | static struct platform_device *rpi_hwmon; | 
|  | 26 |  | 
|  | 27 | struct rpi_firmware { | 
|  | 28 | struct mbox_client cl; | 
|  | 29 | struct mbox_chan *chan; /* The property channel. */ | 
|  | 30 | struct completion c; | 
|  | 31 | u32 enabled; | 
|  | 32 | }; | 
|  | 33 |  | 
|  | 34 | static DEFINE_MUTEX(transaction_lock); | 
|  | 35 |  | 
|  | 36 | static void response_callback(struct mbox_client *cl, void *msg) | 
|  | 37 | { | 
|  | 38 | struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl); | 
|  | 39 | complete(&fw->c); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | /* | 
|  | 43 | * Sends a request to the firmware through the BCM2835 mailbox driver, | 
|  | 44 | * and synchronously waits for the reply. | 
|  | 45 | */ | 
|  | 46 | static int | 
|  | 47 | rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data) | 
|  | 48 | { | 
|  | 49 | u32 message = MBOX_MSG(chan, data); | 
|  | 50 | int ret; | 
|  | 51 |  | 
|  | 52 | WARN_ON(data & 0xf); | 
|  | 53 |  | 
|  | 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); | 
|  | 62 | } | 
|  | 63 | mutex_unlock(&transaction_lock); | 
|  | 64 |  | 
|  | 65 | return ret; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 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. | 
|  | 73 | * | 
|  | 74 | * Submits a set of concatenated tags to the VPU firmware through the | 
|  | 75 | * mailbox property interface. | 
|  | 76 | * | 
|  | 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. | 
|  | 81 | */ | 
|  | 82 | int rpi_firmware_property_list(struct rpi_firmware *fw, | 
|  | 83 | void *data, size_t tag_size) | 
|  | 84 | { | 
|  | 85 | size_t size = tag_size + 12; | 
|  | 86 | u32 *buf; | 
|  | 87 | dma_addr_t bus_addr; | 
|  | 88 | int ret; | 
|  | 89 |  | 
|  | 90 | /* Packets are processed a dword at a time. */ | 
|  | 91 | if (size & 3) | 
|  | 92 | return -EINVAL; | 
|  | 93 |  | 
|  | 94 | buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr, | 
|  | 95 | GFP_ATOMIC); | 
|  | 96 | if (!buf) | 
|  | 97 | return -ENOMEM; | 
|  | 98 |  | 
|  | 99 | /* The firmware will error out without parsing in this case. */ | 
|  | 100 | WARN_ON(size >= 1024 * 1024); | 
|  | 101 |  | 
|  | 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(); | 
|  | 107 |  | 
|  | 108 | ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr); | 
|  | 109 |  | 
|  | 110 | rmb(); | 
|  | 111 | memcpy(data, &buf[2], tag_size); | 
|  | 112 | if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) { | 
|  | 113 | /* | 
|  | 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. | 
|  | 117 | */ | 
|  | 118 | dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n", | 
|  | 119 | buf[2], buf[1]); | 
|  | 120 | ret = -EINVAL; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr); | 
|  | 124 |  | 
|  | 125 | return ret; | 
|  | 126 | } | 
|  | 127 | EXPORT_SYMBOL_GPL(rpi_firmware_property_list); | 
|  | 128 |  | 
|  | 129 | /** | 
|  | 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. | 
|  | 135 | * | 
|  | 136 | * Submits a single tag to the VPU firmware through the mailbox | 
|  | 137 | * property interface. | 
|  | 138 | * | 
|  | 139 | * This is a convenience wrapper around | 
|  | 140 | * rpi_firmware_property_list() to avoid some of the | 
|  | 141 | * boilerplate in property calls. | 
|  | 142 | */ | 
|  | 143 | int rpi_firmware_property(struct rpi_firmware *fw, | 
|  | 144 | u32 tag, void *tag_data, size_t buf_size) | 
|  | 145 | { | 
|  | 146 | struct rpi_firmware_property_tag_header *header; | 
|  | 147 | int ret; | 
|  | 148 |  | 
|  | 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. | 
|  | 153 | */ | 
|  | 154 | void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL); | 
|  | 155 |  | 
|  | 156 | if (!data) | 
|  | 157 | return -ENOMEM; | 
|  | 158 |  | 
|  | 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); | 
|  | 164 |  | 
|  | 165 | ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header)); | 
|  | 166 |  | 
|  | 167 | memcpy(tag_data, data + sizeof(*header), buf_size); | 
|  | 168 |  | 
|  | 169 | kfree(data); | 
|  | 170 |  | 
|  | 171 | return ret; | 
|  | 172 | } | 
|  | 173 | EXPORT_SYMBOL_GPL(rpi_firmware_property); | 
|  | 174 |  | 
|  | 175 | static void | 
|  | 176 | rpi_firmware_print_firmware_revision(struct rpi_firmware *fw) | 
|  | 177 | { | 
|  | 178 | u32 packet; | 
|  | 179 | int ret = rpi_firmware_property(fw, | 
|  | 180 | RPI_FIRMWARE_GET_FIRMWARE_REVISION, | 
|  | 181 | &packet, sizeof(packet)); | 
|  | 182 |  | 
|  | 183 | if (ret == 0) { | 
|  | 184 | struct tm tm; | 
|  | 185 |  | 
|  | 186 | time64_to_tm(packet, 0, &tm); | 
|  | 187 |  | 
|  | 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); | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | static void | 
|  | 196 | rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw) | 
|  | 197 | { | 
|  | 198 | u32 packet; | 
|  | 199 | int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED, | 
|  | 200 | &packet, sizeof(packet)); | 
|  | 201 |  | 
|  | 202 | if (ret) | 
|  | 203 | return; | 
|  | 204 |  | 
|  | 205 | rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon", | 
|  | 206 | -1, NULL, 0); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | static int rpi_firmware_probe(struct platform_device *pdev) | 
|  | 210 | { | 
|  | 211 | struct device *dev = &pdev->dev; | 
|  | 212 | struct rpi_firmware *fw; | 
|  | 213 |  | 
|  | 214 | fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL); | 
|  | 215 | if (!fw) | 
|  | 216 | return -ENOMEM; | 
|  | 217 |  | 
|  | 218 | fw->cl.dev = dev; | 
|  | 219 | fw->cl.rx_callback = response_callback; | 
|  | 220 | fw->cl.tx_block = true; | 
|  | 221 |  | 
|  | 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; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | init_completion(&fw->c); | 
|  | 231 |  | 
|  | 232 | platform_set_drvdata(pdev, fw); | 
|  | 233 |  | 
|  | 234 | rpi_firmware_print_firmware_revision(fw); | 
|  | 235 | rpi_register_hwmon_driver(dev, fw); | 
|  | 236 |  | 
|  | 237 | return 0; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | static int rpi_firmware_remove(struct platform_device *pdev) | 
|  | 241 | { | 
|  | 242 | struct rpi_firmware *fw = platform_get_drvdata(pdev); | 
|  | 243 |  | 
|  | 244 | platform_device_unregister(rpi_hwmon); | 
|  | 245 | rpi_hwmon = NULL; | 
|  | 246 | mbox_free_channel(fw->chan); | 
|  | 247 |  | 
|  | 248 | return 0; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | /** | 
|  | 252 | * rpi_firmware_get - Get pointer to rpi_firmware structure. | 
|  | 253 | * @firmware_node:    Pointer to the firmware Device Tree node. | 
|  | 254 | * | 
|  | 255 | * Returns NULL is the firmware device is not ready. | 
|  | 256 | */ | 
|  | 257 | struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node) | 
|  | 258 | { | 
|  | 259 | struct platform_device *pdev = of_find_device_by_node(firmware_node); | 
|  | 260 |  | 
|  | 261 | if (!pdev) | 
|  | 262 | return NULL; | 
|  | 263 |  | 
|  | 264 | return platform_get_drvdata(pdev); | 
|  | 265 | } | 
|  | 266 | EXPORT_SYMBOL_GPL(rpi_firmware_get); | 
|  | 267 |  | 
|  | 268 | static const struct of_device_id rpi_firmware_of_match[] = { | 
|  | 269 | { .compatible = "raspberrypi,bcm2835-firmware", }, | 
|  | 270 | {}, | 
|  | 271 | }; | 
|  | 272 | MODULE_DEVICE_TABLE(of, rpi_firmware_of_match); | 
|  | 273 |  | 
|  | 274 | static struct platform_driver rpi_firmware_driver = { | 
|  | 275 | .driver = { | 
|  | 276 | .name = "raspberrypi-firmware", | 
|  | 277 | .of_match_table = rpi_firmware_of_match, | 
|  | 278 | }, | 
|  | 279 | .probe		= rpi_firmware_probe, | 
|  | 280 | .remove		= rpi_firmware_remove, | 
|  | 281 | }; | 
|  | 282 | module_platform_driver(rpi_firmware_driver); | 
|  | 283 |  | 
|  | 284 | MODULE_AUTHOR("Eric Anholt <eric@anholt.net>"); | 
|  | 285 | MODULE_DESCRIPTION("Raspberry Pi firmware driver"); | 
|  | 286 | MODULE_LICENSE("GPL v2"); |