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
;
126 * The Firmware's Data Format is describe in
127 * "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
131 * The bootrom chips of the big brother have sizes up to 64k, let's
132 * assume that's the biggest the firmware can get.
134 if (length
< 0x1000 || length
>= 0x10000) {
135 pr_err("firmware is size %zd is not (4k - 64k).",
140 /* The First 2 bytes are fixed value (55aa). "LSB on Left" */
141 if (get_unaligned_le16(fw_data
) != 0x55aa) {
142 pr_err("no valid firmware header found.");
146 /* verify the firmware version position and print it. */
147 fw_version_pointer
= get_unaligned_le16(fw_data
+ 4);
148 if (fw_version_pointer
+ 2 >= length
) {
149 pr_err("fw ver pointer is outside of the firmware image");
153 fw_version
= get_unaligned_le16(fw_data
+ fw_version_pointer
);
154 pr_err("got firmware version: %02x.", fw_version
);
159 static bool renesas_check_rom(struct pci_dev
*pdev
)
164 /* Check if external ROM exists */
165 retval
= pci_read_config_word(pdev
, RENESAS_ROM_STATUS
, &rom_status
);
169 rom_status
&= RENESAS_ROM_STATUS_ROM_EXISTS
;
171 dev_dbg(&pdev
->dev
, "External ROM exists\n");
172 return true; /* External ROM exists */
178 static int renesas_check_rom_state(struct pci_dev
*pdev
)
184 /* check FW version */
185 err
= pci_read_config_dword(pdev
, RENESAS_FW_VERSION
, &version
);
187 return pcibios_err_to_errno(err
);
189 version
&= RENESAS_FW_VERSION_FIELD
;
190 version
= version
>> RENESAS_FW_VERSION_OFFSET
;
191 dev_dbg(&pdev
->dev
, "Found ROM version: %x\n", version
);
194 * Test if ROM is present and loaded, if so we can skip everything
196 err
= pci_read_config_word(pdev
, RENESAS_ROM_STATUS
, &rom_state
);
198 return pcibios_err_to_errno(err
);
200 if (rom_state
& BIT(15)) {
202 dev_dbg(&pdev
->dev
, "ROM exists\n");
204 /* Check the "Result Code" Bits (6:4) and act accordingly */
205 switch (rom_state
& RENESAS_ROM_STATUS_RESULT
) {
206 case RENESAS_ROM_STATUS_SUCCESS
:
209 case RENESAS_ROM_STATUS_NO_RESULT
: /* No result yet */
212 case RENESAS_ROM_STATUS_ERROR
: /* Error State */
213 default: /* All other states are marked as "Reserved states" */
214 dev_err(&pdev
->dev
, "Invalid ROM..");
222 static int renesas_fw_check_running(struct pci_dev
*pdev
)
227 /* Check if device has ROM and loaded, if so skip everything */
228 err
= renesas_check_rom(pdev
);
229 if (err
) { /* we have rom */
230 err
= renesas_check_rom_state(pdev
);
236 * Test if the device is actually needing the firmware. As most
237 * BIOSes will initialize the device for us. If the device is
240 err
= pci_read_config_byte(pdev
, RENESAS_FW_STATUS
, &fw_state
);
242 return pcibios_err_to_errno(err
);
245 * Check if "FW Download Lock" is locked. If it is and the FW is
246 * ready we can simply continue. If the FW is not ready, we have
249 if (fw_state
& RENESAS_FW_STATUS_LOCK
) {
250 dev_dbg(&pdev
->dev
, "FW Download Lock is engaged.");
252 if (fw_state
& RENESAS_FW_STATUS_SUCCESS
)
256 "FW Download Lock is set and FW is not ready. Giving Up.");
261 * Check if "FW Download Enable" is set. If someone (us?) tampered
262 * with it and it can't be reset, we have to give up too... and
263 * ask for a forgiveness and a reboot.
265 if (fw_state
& RENESAS_FW_STATUS_DOWNLOAD_ENABLE
) {
267 "FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
271 /* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
272 switch (fw_state
& RENESAS_FW_STATUS_RESULT
) {
273 case 0: /* No result yet */
274 dev_dbg(&pdev
->dev
, "FW is not ready/loaded yet.");
276 /* tell the caller, that this device needs the firmware. */
279 case RENESAS_FW_STATUS_SUCCESS
: /* Success, device should be working. */
280 dev_dbg(&pdev
->dev
, "FW is ready.");
283 case RENESAS_FW_STATUS_ERROR
: /* Error State */
285 "hardware is in an error state. Giving up (poweroff/reboot needed).");
288 default: /* All other states are marked as "Reserved states" */
290 "hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
291 (fw_state
& RENESAS_FW_STATUS_RESULT
) >> 4);
296 static int renesas_fw_download(struct pci_dev
*pdev
,
297 const struct firmware
*fw
)
299 const u32
*fw_data
= (const u32
*)fw
->data
;
305 * For more information and the big picture: please look at the
306 * "Firmware Download Sequence" in "7.1 FW Download Interface"
307 * of R19UH0078EJ0500 Rev.5.00 page 131
311 * 0. Set "FW Download Enable" bit in the
312 * "FW Download Control & Status Register" at 0xF4
314 err
= pci_write_config_byte(pdev
, RENESAS_FW_STATUS
,
315 RENESAS_FW_STATUS_DOWNLOAD_ENABLE
);
317 return pcibios_err_to_errno(err
);
319 /* 1 - 10 follow one step after the other. */
320 for (i
= 0; i
< fw
->size
/ 4; i
++) {
321 err
= renesas_fw_download_image(pdev
, fw_data
, i
, false);
324 "Firmware Download Step %zd failed at position %zd bytes with (%d).",
331 * This sequence continues until the last data is written to
332 * "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
333 * is cleared by the hardware beforehand.
335 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
336 err
= pci_read_config_byte(pdev
, RENESAS_FW_STATUS_MSB
,
339 return pcibios_err_to_errno(err
);
340 if (!(fw_status
& (BIT(0) | BIT(1))))
343 udelay(RENESAS_DELAY
);
345 if (i
== RENESAS_RETRY
)
346 dev_warn(&pdev
->dev
, "Final Firmware Download step timed out.");
349 * 11. After finishing writing the last data of FW, the
350 * System Software must clear "FW Download Enable"
352 err
= pci_write_config_byte(pdev
, RENESAS_FW_STATUS
, 0);
354 return pcibios_err_to_errno(err
);
356 /* 12. Read "Result Code" and confirm it is good. */
357 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
358 err
= pci_read_config_byte(pdev
, RENESAS_FW_STATUS
, &fw_status
);
360 return pcibios_err_to_errno(err
);
361 if (fw_status
& RENESAS_FW_STATUS_SUCCESS
)
364 udelay(RENESAS_DELAY
);
366 if (i
== RENESAS_RETRY
) {
367 /* Timed out / Error - let's see if we can fix this */
368 err
= renesas_fw_check_running(pdev
);
371 * we shouldn't end up here.
372 * maybe it took a little bit longer.
373 * But all should be well?
377 case 1: /* (No result yet! */
378 dev_err(&pdev
->dev
, "FW Load timedout");
389 static void renesas_rom_erase(struct pci_dev
*pdev
)
394 dev_dbg(&pdev
->dev
, "Performing ROM Erase...\n");
395 retval
= pci_write_config_dword(pdev
, RENESAS_DATA0
,
396 RENESAS_ROM_ERASE_MAGIC
);
398 dev_err(&pdev
->dev
, "ROM erase, magic word write failed: %d\n",
399 pcibios_err_to_errno(retval
));
403 retval
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS
, &status
);
405 dev_err(&pdev
->dev
, "ROM status read failed: %d\n",
406 pcibios_err_to_errno(retval
));
409 status
|= RENESAS_ROM_STATUS_ERASE
;
410 retval
= pci_write_config_byte(pdev
, RENESAS_ROM_STATUS
, status
);
412 dev_err(&pdev
->dev
, "ROM erase set word write failed\n");
416 /* sleep a bit while ROM is erased */
419 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
420 retval
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS
,
422 status
&= RENESAS_ROM_STATUS_ERASE
;
426 mdelay(RENESAS_DELAY
);
429 if (i
== RENESAS_RETRY
)
430 dev_dbg(&pdev
->dev
, "Chip erase timedout: %x\n", status
);
432 dev_dbg(&pdev
->dev
, "ROM Erase... Done success\n");
435 static bool renesas_setup_rom(struct pci_dev
*pdev
, const struct firmware
*fw
)
437 const u32
*fw_data
= (const u32
*)fw
->data
;
441 /* 2. Write magic word to Data0 */
442 err
= pci_write_config_dword(pdev
, RENESAS_DATA0
,
443 RENESAS_ROM_WRITE_MAGIC
);
447 /* 3. Set External ROM access */
448 err
= pci_write_config_byte(pdev
, RENESAS_ROM_STATUS
,
449 RENESAS_ROM_STATUS_ACCESS
);
453 /* 4. Check the result */
454 err
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS
, &status
);
457 status
&= GENMASK(6, 4);
460 "setting external rom failed: %x\n", status
);
464 /* 5 to 16 Write FW to DATA0/1 while checking SetData0/1 */
465 for (i
= 0; i
< fw
->size
/ 4; i
++) {
466 err
= renesas_fw_download_image(pdev
, fw_data
, i
, true);
469 "ROM Download Step %d failed at position %d bytes with (%d)\n",
476 * wait till DATA0/1 is cleared
478 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
479 err
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS_MSB
,
483 if (!(status
& (BIT(0) | BIT(1))))
486 udelay(RENESAS_DELAY
);
488 if (i
== RENESAS_RETRY
) {
489 dev_err(&pdev
->dev
, "Final Firmware ROM Download step timed out\n");
493 /* 17. Remove bypass */
494 err
= pci_write_config_byte(pdev
, RENESAS_ROM_STATUS
, 0);
500 /* 18. check result */
501 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
502 err
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS
, &status
);
504 dev_err(&pdev
->dev
, "Read ROM status failed:%d\n",
505 pcibios_err_to_errno(err
));
508 status
&= RENESAS_ROM_STATUS_RESULT
;
509 if (status
== RENESAS_ROM_STATUS_SUCCESS
) {
510 dev_dbg(&pdev
->dev
, "Download ROM success\n");
513 udelay(RENESAS_DELAY
);
515 if (i
== RENESAS_RETRY
) { /* Timed out */
517 "Download to external ROM TO: %x\n", status
);
521 dev_dbg(&pdev
->dev
, "Download to external ROM succeeded\n");
523 /* Last step set Reload */
524 err
= pci_write_config_byte(pdev
, RENESAS_ROM_STATUS
,
525 RENESAS_ROM_STATUS_RELOAD
);
527 dev_err(&pdev
->dev
, "Set ROM execute failed: %d\n",
528 pcibios_err_to_errno(err
));
533 * wait till Reload is cleared
535 for (i
= 0; i
< RENESAS_RETRY
; i
++) {
536 err
= pci_read_config_byte(pdev
, RENESAS_ROM_STATUS
, &status
);
539 if (!(status
& RENESAS_ROM_STATUS_RELOAD
))
542 udelay(RENESAS_DELAY
);
544 if (i
== RENESAS_RETRY
) {
545 dev_err(&pdev
->dev
, "ROM Exec timed out: %x\n", status
);
552 pci_write_config_byte(pdev
, RENESAS_ROM_STATUS
, 0);
556 static int renesas_load_fw(struct pci_dev
*pdev
, const struct firmware
*fw
)
561 /* Check if the device has external ROM */
562 rom
= renesas_check_rom(pdev
);
564 /* perform chip erase first */
565 renesas_rom_erase(pdev
);
567 /* lets try loading fw on ROM first */
568 rom
= renesas_setup_rom(pdev
, fw
);
571 "ROM load failed, falling back on FW load\n");
574 "ROM load success\n");
579 err
= renesas_fw_download(pdev
, fw
);
583 dev_err(&pdev
->dev
, "firmware failed to download (%d).", err
);
587 int renesas_xhci_check_request_fw(struct pci_dev
*pdev
,
588 const struct pci_device_id
*id
)
590 struct xhci_driver_data
*driver_data
=
591 (struct xhci_driver_data
*)id
->driver_data
;
592 const char *fw_name
= driver_data
->firmware
;
593 const struct firmware
*fw
;
596 err
= renesas_fw_check_running(pdev
);
597 /* Continue ahead, if the firmware is already running. */
605 err
= request_firmware(&fw
, fw_name
, &pdev
->dev
);
608 dev_err(&pdev
->dev
, "request_firmware failed: %d\n", err
);
612 err
= renesas_fw_verify(fw
->data
, fw
->size
);
616 err
= renesas_load_fw(pdev
, fw
);
618 release_firmware(fw
);
621 EXPORT_SYMBOL_GPL(renesas_xhci_check_request_fw
);
623 void renesas_xhci_pci_exit(struct pci_dev
*dev
)
626 EXPORT_SYMBOL_GPL(renesas_xhci_pci_exit
);
628 MODULE_LICENSE("GPL v2");