1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2020 Linaro Limited */
4 #include <linux/acpi.h>
5 #include <linux/firmware.h>
6 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <asm/unaligned.h>
12 #include "xhci-trace.h"
15 #define RENESAS_FW_VERSION 0x6C
16 #define RENESAS_ROM_CONFIG 0xF0
17 #define RENESAS_FW_STATUS 0xF4
18 #define RENESAS_FW_STATUS_MSB 0xF5
19 #define RENESAS_ROM_STATUS 0xF6
20 #define RENESAS_ROM_STATUS_MSB 0xF7
21 #define RENESAS_DATA0 0xF8
22 #define RENESAS_DATA1 0xFC
24 #define RENESAS_FW_VERSION_FIELD GENMASK(23, 7)
25 #define RENESAS_FW_VERSION_OFFSET 8
27 #define RENESAS_FW_STATUS_DOWNLOAD_ENABLE BIT(0)
28 #define RENESAS_FW_STATUS_LOCK BIT(1)
29 #define RENESAS_FW_STATUS_RESULT GENMASK(6, 4)
30 #define RENESAS_FW_STATUS_INVALID 0
31 #define RENESAS_FW_STATUS_SUCCESS BIT(4)
32 #define RENESAS_FW_STATUS_ERROR BIT(5)
33 #define RENESAS_FW_STATUS_SET_DATA0 BIT(8)
34 #define RENESAS_FW_STATUS_SET_DATA1 BIT(9)
36 #define RENESAS_ROM_STATUS_ACCESS BIT(0)
37 #define RENESAS_ROM_STATUS_ERASE BIT(1)
38 #define RENESAS_ROM_STATUS_RELOAD BIT(2)
39 #define RENESAS_ROM_STATUS_RESULT GENMASK(6, 4)
40 #define RENESAS_ROM_STATUS_NO_RESULT 0
41 #define RENESAS_ROM_STATUS_SUCCESS BIT(4)
42 #define RENESAS_ROM_STATUS_ERROR BIT(5)
43 #define RENESAS_ROM_STATUS_SET_DATA0 BIT(8)
44 #define RENESAS_ROM_STATUS_SET_DATA1 BIT(9)
45 #define RENESAS_ROM_STATUS_ROM_EXISTS BIT(15)
47 #define RENESAS_ROM_ERASE_MAGIC 0x5A65726F
48 #define RENESAS_ROM_WRITE_MAGIC 0x53524F4D
50 #define RENESAS_RETRY 10000
51 #define RENESAS_DELAY 10
53 static int renesas_fw_download_image(struct pci_dev *dev,
54 const u32 *fw, size_t step, bool rom)
63 status_reg = RENESAS_ROM_STATUS_MSB;
65 status_reg = RENESAS_FW_STATUS_MSB;
68 * The hardware does alternate between two 32-bit pages.
69 * (This is because each row of the firmware is 8 bytes).
71 * for even steps we use DATA0, for odd steps DATA1.
73 data0_or_data1 = (step & 1) == 1;
75 /* step+1. Read "Set DATAX" and confirm it is cleared. */
76 for (i = 0; i < RENESAS_RETRY; i++) {
77 err = pci_read_config_byte(dev, status_reg, &fw_status);
79 dev_err(&dev->dev, "Read Status failed: %d\n",
80 pcibios_err_to_errno(err));
81 return pcibios_err_to_errno(err);
83 if (!(fw_status & BIT(data0_or_data1)))
86 udelay(RENESAS_DELAY);
88 if (i == RENESAS_RETRY) {
89 dev_err(&dev->dev, "Timeout for Set DATAX step: %zd\n", step);
94 * step+2. Write FW data to "DATAX".
95 * "LSB is left" => force little endian
97 err = pci_write_config_dword(dev, data0_or_data1 ?
98 RENESAS_DATA1 : RENESAS_DATA0,
99 (__force u32)cpu_to_le32(fw[step]));
101 dev_err(&dev->dev, "Write to DATAX failed: %d\n",
102 pcibios_err_to_errno(err));
103 return pcibios_err_to_errno(err);
108 /* step+3. Set "Set DATAX". */
109 err = pci_write_config_byte(dev, status_reg, BIT(data0_or_data1));
111 dev_err(&dev->dev, "Write config for DATAX failed: %d\n",
112 pcibios_err_to_errno(err));
113 return pcibios_err_to_errno(err);
119 static int renesas_fw_verify(const void *fw_data,
122 u16 fw_version_pointer;
125 * The Firmware's Data Format is describe in
126 * "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
130 * The bootrom chips of the big brother have sizes up to 64k, let's
131 * assume that's the biggest the firmware can get.
133 if (length < 0x1000 || length >= 0x10000) {
134 pr_err("firmware is size %zd is not (4k - 64k).",
139 /* The First 2 bytes are fixed value (55aa). "LSB on Left" */
140 if (get_unaligned_le16(fw_data) != 0x55aa) {
141 pr_err("no valid firmware header found.");
145 /* verify the firmware version position and print it. */
146 fw_version_pointer = get_unaligned_le16(fw_data + 4);
147 if (fw_version_pointer + 2 >= length) {
148 pr_err("fw ver pointer is outside of the firmware image");
155 static bool renesas_check_rom(struct pci_dev *pdev)
160 /* Check if external ROM exists */
161 retval = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_status);
165 rom_status &= RENESAS_ROM_STATUS_ROM_EXISTS;
167 dev_dbg(&pdev->dev, "External ROM exists\n");
168 return true; /* External ROM exists */
174 static int renesas_check_rom_state(struct pci_dev *pdev)
180 /* check FW version */
181 err = pci_read_config_dword(pdev, RENESAS_FW_VERSION, &version);
183 return pcibios_err_to_errno(err);
185 version &= RENESAS_FW_VERSION_FIELD;
186 version = version >> RENESAS_FW_VERSION_OFFSET;
187 dev_dbg(&pdev->dev, "Found ROM version: %x\n", version);
190 * Test if ROM is present and loaded, if so we can skip everything
192 err = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_state);
194 return pcibios_err_to_errno(err);
196 if (rom_state & RENESAS_ROM_STATUS_ROM_EXISTS) {
198 dev_dbg(&pdev->dev, "ROM exists\n");
200 /* Check the "Result Code" Bits (6:4) and act accordingly */
201 switch (rom_state & RENESAS_ROM_STATUS_RESULT) {
202 case RENESAS_ROM_STATUS_SUCCESS:
205 case RENESAS_ROM_STATUS_NO_RESULT: /* No result yet */
206 dev_dbg(&pdev->dev, "Unknown ROM status ...\n");
209 case RENESAS_ROM_STATUS_ERROR: /* Error State */
210 default: /* All other states are marked as "Reserved states" */
211 dev_err(&pdev->dev, "Invalid ROM..");
219 static int renesas_fw_check_running(struct pci_dev *pdev)
225 * Test if the device is actually needing the firmware. As most
226 * BIOSes will initialize the device for us. If the device is
229 err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
231 return pcibios_err_to_errno(err);
234 * Check if "FW Download Lock" is locked. If it is and the FW is
235 * ready we can simply continue. If the FW is not ready, we have
238 if (fw_state & RENESAS_FW_STATUS_LOCK) {
239 dev_dbg(&pdev->dev, "FW Download Lock is engaged.");
241 if (fw_state & RENESAS_FW_STATUS_SUCCESS)
245 "FW Download Lock is set and FW is not ready. Giving Up.");
250 * Check if "FW Download Enable" is set. If someone (us?) tampered
251 * with it and it can't be reset, we have to give up too... and
252 * ask for a forgiveness and a reboot.
254 if (fw_state & RENESAS_FW_STATUS_DOWNLOAD_ENABLE) {
256 "FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
260 /* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
261 switch (fw_state & RENESAS_FW_STATUS_RESULT) {
262 case 0: /* No result yet */
263 dev_dbg(&pdev->dev, "FW is not ready/loaded yet.");
265 /* tell the caller, that this device needs the firmware. */
268 case RENESAS_FW_STATUS_SUCCESS: /* Success, device should be working. */
269 dev_dbg(&pdev->dev, "FW is ready.");
272 case RENESAS_FW_STATUS_ERROR: /* Error State */
274 "hardware is in an error state. Giving up (poweroff/reboot needed).");
277 default: /* All other states are marked as "Reserved states" */
279 "hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
280 (fw_state & RENESAS_FW_STATUS_RESULT) >> 4);
285 static int renesas_fw_download(struct pci_dev *pdev,
286 const struct firmware *fw)
288 const u32 *fw_data = (const u32 *)fw->data;
294 * For more information and the big picture: please look at the
295 * "Firmware Download Sequence" in "7.1 FW Download Interface"
296 * of R19UH0078EJ0500 Rev.5.00 page 131
300 * 0. Set "FW Download Enable" bit in the
301 * "FW Download Control & Status Register" at 0xF4
303 err = pci_write_config_byte(pdev, RENESAS_FW_STATUS,
304 RENESAS_FW_STATUS_DOWNLOAD_ENABLE);
306 return pcibios_err_to_errno(err);
308 /* 1 - 10 follow one step after the other. */
309 for (i = 0; i < fw->size / 4; i++) {
310 err = renesas_fw_download_image(pdev, fw_data, i, false);
313 "Firmware Download Step %zd failed at position %zd bytes with (%d).",
320 * This sequence continues until the last data is written to
321 * "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
322 * is cleared by the hardware beforehand.
324 for (i = 0; i < RENESAS_RETRY; i++) {
325 err = pci_read_config_byte(pdev, RENESAS_FW_STATUS_MSB,
328 return pcibios_err_to_errno(err);
329 if (!(fw_status & (BIT(0) | BIT(1))))
332 udelay(RENESAS_DELAY);
334 if (i == RENESAS_RETRY)
335 dev_warn(&pdev->dev, "Final Firmware Download step timed out.");
338 * 11. After finishing writing the last data of FW, the
339 * System Software must clear "FW Download Enable"
341 err = pci_write_config_byte(pdev, RENESAS_FW_STATUS, 0);
343 return pcibios_err_to_errno(err);
345 /* 12. Read "Result Code" and confirm it is good. */
346 for (i = 0; i < RENESAS_RETRY; i++) {
347 err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_status);
349 return pcibios_err_to_errno(err);
350 if (fw_status & RENESAS_FW_STATUS_SUCCESS)
353 udelay(RENESAS_DELAY);
355 if (i == RENESAS_RETRY) {
356 /* Timed out / Error - let's see if we can fix this */
357 err = renesas_fw_check_running(pdev);
360 * we shouldn't end up here.
361 * maybe it took a little bit longer.
362 * But all should be well?
366 case 1: /* (No result yet! */
367 dev_err(&pdev->dev, "FW Load timedout");
378 static void renesas_rom_erase(struct pci_dev *pdev)
383 dev_dbg(&pdev->dev, "Performing ROM Erase...\n");
384 retval = pci_write_config_dword(pdev, RENESAS_DATA0,
385 RENESAS_ROM_ERASE_MAGIC);
387 dev_err(&pdev->dev, "ROM erase, magic word write failed: %d\n",
388 pcibios_err_to_errno(retval));
392 retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
394 dev_err(&pdev->dev, "ROM status read failed: %d\n",
395 pcibios_err_to_errno(retval));
398 status |= RENESAS_ROM_STATUS_ERASE;
399 retval = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, status);
401 dev_err(&pdev->dev, "ROM erase set word write failed\n");
405 /* sleep a bit while ROM is erased */
408 for (i = 0; i < RENESAS_RETRY; i++) {
409 retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS,
411 status &= RENESAS_ROM_STATUS_ERASE;
415 mdelay(RENESAS_DELAY);
418 if (i == RENESAS_RETRY)
419 dev_dbg(&pdev->dev, "Chip erase timedout: %x\n", status);
421 dev_dbg(&pdev->dev, "ROM Erase... Done success\n");
424 static bool renesas_setup_rom(struct pci_dev *pdev, const struct firmware *fw)
426 const u32 *fw_data = (const u32 *)fw->data;
430 /* 2. Write magic word to Data0 */
431 err = pci_write_config_dword(pdev, RENESAS_DATA0,
432 RENESAS_ROM_WRITE_MAGIC);
436 /* 3. Set External ROM access */
437 err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
438 RENESAS_ROM_STATUS_ACCESS);
442 /* 4. Check the result */
443 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
446 status &= GENMASK(6, 4);
449 "setting external rom failed: %x\n", status);
453 /* 5 to 16 Write FW to DATA0/1 while checking SetData0/1 */
454 for (i = 0; i < fw->size / 4; i++) {
455 err = renesas_fw_download_image(pdev, fw_data, i, true);
458 "ROM Download Step %d failed at position %d bytes with (%d)\n",
465 * wait till DATA0/1 is cleared
467 for (i = 0; i < RENESAS_RETRY; i++) {
468 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS_MSB,
472 if (!(status & (BIT(0) | BIT(1))))
475 udelay(RENESAS_DELAY);
477 if (i == RENESAS_RETRY) {
478 dev_err(&pdev->dev, "Final Firmware ROM Download step timed out\n");
482 /* 17. Remove bypass */
483 err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
489 /* 18. check result */
490 for (i = 0; i < RENESAS_RETRY; i++) {
491 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
493 dev_err(&pdev->dev, "Read ROM status failed:%d\n",
494 pcibios_err_to_errno(err));
497 status &= RENESAS_ROM_STATUS_RESULT;
498 if (status == RENESAS_ROM_STATUS_SUCCESS) {
499 dev_dbg(&pdev->dev, "Download ROM success\n");
502 udelay(RENESAS_DELAY);
504 if (i == RENESAS_RETRY) { /* Timed out */
506 "Download to external ROM TO: %x\n", status);
510 dev_dbg(&pdev->dev, "Download to external ROM succeeded\n");
512 /* Last step set Reload */
513 err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
514 RENESAS_ROM_STATUS_RELOAD);
516 dev_err(&pdev->dev, "Set ROM execute failed: %d\n",
517 pcibios_err_to_errno(err));
522 * wait till Reload is cleared
524 for (i = 0; i < RENESAS_RETRY; i++) {
525 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
528 if (!(status & RENESAS_ROM_STATUS_RELOAD))
531 udelay(RENESAS_DELAY);
533 if (i == RENESAS_RETRY) {
534 dev_err(&pdev->dev, "ROM Exec timed out: %x\n", status);
541 pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
545 static int renesas_load_fw(struct pci_dev *pdev, const struct firmware *fw)
550 /* Check if the device has external ROM */
551 rom = renesas_check_rom(pdev);
553 /* perform chip erase first */
554 renesas_rom_erase(pdev);
556 /* lets try loading fw on ROM first */
557 rom = renesas_setup_rom(pdev, fw);
560 "ROM load failed, falling back on FW load\n");
563 "ROM load success\n");
568 err = renesas_fw_download(pdev, fw);
572 dev_err(&pdev->dev, "firmware failed to download (%d).", err);
576 int renesas_xhci_check_request_fw(struct pci_dev *pdev,
577 const struct pci_device_id *id)
579 struct xhci_driver_data *driver_data =
580 (struct xhci_driver_data *)id->driver_data;
581 const char *fw_name = driver_data->firmware;
582 const struct firmware *fw;
586 /* Check if device has ROM and loaded, if so skip everything */
587 has_rom = renesas_check_rom(pdev);
589 err = renesas_check_rom_state(pdev);
592 else if (err != -ENOENT)
596 err = renesas_fw_check_running(pdev);
597 /* Continue ahead, if the firmware is already running. */
601 /* no firmware interface available */
603 return has_rom ? 0 : err;
606 err = firmware_reject_nowarn(&fw, fw_name, &pdev->dev);
610 dev_info(&pdev->dev, "failed to load firmware %s, fallback to ROM\n",
614 dev_err(&pdev->dev, "failed to load firmware %s: %d\n",
619 err = renesas_fw_verify(fw->data, fw->size);
623 err = renesas_load_fw(pdev, fw);
625 release_firmware(fw);
628 EXPORT_SYMBOL_GPL(renesas_xhci_check_request_fw);
630 MODULE_LICENSE("GPL v2");