Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / usb / host / xhci-pci-renesas.c
blobf97ac9f52bf4d9cc15b244e067e87e918847a249
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>
7 #include <linux/pci.h>
8 #include <linux/slab.h>
9 #include <asm/unaligned.h>
11 #include "xhci.h"
12 #include "xhci-trace.h"
13 #include "xhci-pci.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)
56 size_t i;
57 int err;
58 u8 fw_status;
59 bool data0_or_data1;
60 u32 status_reg;
62 if (rom)
63 status_reg = RENESAS_ROM_STATUS_MSB;
64 else
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);
78 if (err) {
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)))
84 break;
86 udelay(RENESAS_DELAY);
88 if (i == RENESAS_RETRY) {
89 dev_err(&dev->dev, "Timeout for Set DATAX step: %zd\n", step);
90 return -ETIMEDOUT;
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]));
100 if (err) {
101 dev_err(&dev->dev, "Write to DATAX failed: %d\n",
102 pcibios_err_to_errno(err));
103 return pcibios_err_to_errno(err);
106 udelay(100);
108 /* step+3. Set "Set DATAX". */
109 err = pci_write_config_byte(dev, status_reg, BIT(data0_or_data1));
110 if (err) {
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);
116 return 0;
119 static int renesas_fw_verify(const void *fw_data,
120 size_t length)
122 u16 fw_version_pointer;
123 u16 fw_version;
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).",
136 length);
137 return -EINVAL;
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.");
143 return -EINVAL;
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");
150 return -EINVAL;
153 fw_version = get_unaligned_le16(fw_data + fw_version_pointer);
154 pr_err("got firmware version: %02x.", fw_version);
156 return 0;
159 static bool renesas_check_rom(struct pci_dev *pdev)
161 u16 rom_status;
162 int retval;
164 /* Check if external ROM exists */
165 retval = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_status);
166 if (retval)
167 return false;
169 rom_status &= RENESAS_ROM_STATUS_ROM_EXISTS;
170 if (rom_status) {
171 dev_dbg(&pdev->dev, "External ROM exists\n");
172 return true; /* External ROM exists */
175 return false;
178 static int renesas_check_rom_state(struct pci_dev *pdev)
180 u16 rom_state;
181 u32 version;
182 int err;
184 /* check FW version */
185 err = pci_read_config_dword(pdev, RENESAS_FW_VERSION, &version);
186 if (err)
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);
197 if (err)
198 return pcibios_err_to_errno(err);
200 if (rom_state & BIT(15)) {
201 /* ROM exists */
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:
207 return 0;
209 case RENESAS_ROM_STATUS_NO_RESULT: /* No result yet */
210 return 0;
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..");
215 break;
219 return -EIO;
222 static int renesas_fw_check_running(struct pci_dev *pdev)
224 u8 fw_state;
225 int err;
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);
231 if (!err)
232 return err;
236 * Test if the device is actually needing the firmware. As most
237 * BIOSes will initialize the device for us. If the device is
238 * initialized.
240 err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
241 if (err)
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
247 * to give up.
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)
253 return 0;
255 dev_err(&pdev->dev,
256 "FW Download Lock is set and FW is not ready. Giving Up.");
257 return -EIO;
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) {
266 dev_err(&pdev->dev,
267 "FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
268 return -EIO;
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. */
277 return 1;
279 case RENESAS_FW_STATUS_SUCCESS: /* Success, device should be working. */
280 dev_dbg(&pdev->dev, "FW is ready.");
281 return 0;
283 case RENESAS_FW_STATUS_ERROR: /* Error State */
284 dev_err(&pdev->dev,
285 "hardware is in an error state. Giving up (poweroff/reboot needed).");
286 return -ENODEV;
288 default: /* All other states are marked as "Reserved states" */
289 dev_err(&pdev->dev,
290 "hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
291 (fw_state & RENESAS_FW_STATUS_RESULT) >> 4);
292 return -EINVAL;
296 static int renesas_fw_download(struct pci_dev *pdev,
297 const struct firmware *fw)
299 const u32 *fw_data = (const u32 *)fw->data;
300 size_t i;
301 int err;
302 u8 fw_status;
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);
316 if (err)
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);
322 if (err) {
323 dev_err(&pdev->dev,
324 "Firmware Download Step %zd failed at position %zd bytes with (%d).",
325 i, i * 4, err);
326 return err;
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,
337 &fw_status);
338 if (err)
339 return pcibios_err_to_errno(err);
340 if (!(fw_status & (BIT(0) | BIT(1))))
341 break;
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);
353 if (err)
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);
359 if (err)
360 return pcibios_err_to_errno(err);
361 if (fw_status & RENESAS_FW_STATUS_SUCCESS)
362 break;
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);
369 switch (err) {
370 case 0: /*
371 * we shouldn't end up here.
372 * maybe it took a little bit longer.
373 * But all should be well?
375 break;
377 case 1: /* (No result yet! */
378 dev_err(&pdev->dev, "FW Load timedout");
379 return -ETIMEDOUT;
381 default:
382 return err;
386 return 0;
389 static void renesas_rom_erase(struct pci_dev *pdev)
391 int retval, i;
392 u8 status;
394 dev_dbg(&pdev->dev, "Performing ROM Erase...\n");
395 retval = pci_write_config_dword(pdev, RENESAS_DATA0,
396 RENESAS_ROM_ERASE_MAGIC);
397 if (retval) {
398 dev_err(&pdev->dev, "ROM erase, magic word write failed: %d\n",
399 pcibios_err_to_errno(retval));
400 return;
403 retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
404 if (retval) {
405 dev_err(&pdev->dev, "ROM status read failed: %d\n",
406 pcibios_err_to_errno(retval));
407 return;
409 status |= RENESAS_ROM_STATUS_ERASE;
410 retval = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, status);
411 if (retval) {
412 dev_err(&pdev->dev, "ROM erase set word write failed\n");
413 return;
416 /* sleep a bit while ROM is erased */
417 msleep(20);
419 for (i = 0; i < RENESAS_RETRY; i++) {
420 retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS,
421 &status);
422 status &= RENESAS_ROM_STATUS_ERASE;
423 if (!status)
424 break;
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;
438 int err, i;
439 u8 status;
441 /* 2. Write magic word to Data0 */
442 err = pci_write_config_dword(pdev, RENESAS_DATA0,
443 RENESAS_ROM_WRITE_MAGIC);
444 if (err)
445 return false;
447 /* 3. Set External ROM access */
448 err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
449 RENESAS_ROM_STATUS_ACCESS);
450 if (err)
451 goto remove_bypass;
453 /* 4. Check the result */
454 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
455 if (err)
456 goto remove_bypass;
457 status &= GENMASK(6, 4);
458 if (status) {
459 dev_err(&pdev->dev,
460 "setting external rom failed: %x\n", status);
461 goto remove_bypass;
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);
467 if (err) {
468 dev_err(&pdev->dev,
469 "ROM Download Step %d failed at position %d bytes with (%d)\n",
470 i, i * 4, err);
471 goto remove_bypass;
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,
480 &status);
481 if (err)
482 goto remove_bypass;
483 if (!(status & (BIT(0) | BIT(1))))
484 break;
486 udelay(RENESAS_DELAY);
488 if (i == RENESAS_RETRY) {
489 dev_err(&pdev->dev, "Final Firmware ROM Download step timed out\n");
490 goto remove_bypass;
493 /* 17. Remove bypass */
494 err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
495 if (err)
496 return false;
498 udelay(10);
500 /* 18. check result */
501 for (i = 0; i < RENESAS_RETRY; i++) {
502 err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
503 if (err) {
504 dev_err(&pdev->dev, "Read ROM status failed:%d\n",
505 pcibios_err_to_errno(err));
506 return false;
508 status &= RENESAS_ROM_STATUS_RESULT;
509 if (status == RENESAS_ROM_STATUS_SUCCESS) {
510 dev_dbg(&pdev->dev, "Download ROM success\n");
511 break;
513 udelay(RENESAS_DELAY);
515 if (i == RENESAS_RETRY) { /* Timed out */
516 dev_err(&pdev->dev,
517 "Download to external ROM TO: %x\n", status);
518 return false;
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);
526 if (err) {
527 dev_err(&pdev->dev, "Set ROM execute failed: %d\n",
528 pcibios_err_to_errno(err));
529 return false;
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);
537 if (err)
538 return false;
539 if (!(status & RENESAS_ROM_STATUS_RELOAD))
540 break;
542 udelay(RENESAS_DELAY);
544 if (i == RENESAS_RETRY) {
545 dev_err(&pdev->dev, "ROM Exec timed out: %x\n", status);
546 return false;
549 return true;
551 remove_bypass:
552 pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
553 return false;
556 static int renesas_load_fw(struct pci_dev *pdev, const struct firmware *fw)
558 int err = 0;
559 bool rom;
561 /* Check if the device has external ROM */
562 rom = renesas_check_rom(pdev);
563 if (rom) {
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);
569 if (!rom) {
570 dev_dbg(&pdev->dev,
571 "ROM load failed, falling back on FW load\n");
572 } else {
573 dev_dbg(&pdev->dev,
574 "ROM load success\n");
575 goto exit;
579 err = renesas_fw_download(pdev, fw);
581 exit:
582 if (err)
583 dev_err(&pdev->dev, "firmware failed to download (%d).", err);
584 return 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;
594 int err;
596 err = renesas_fw_check_running(pdev);
597 /* Continue ahead, if the firmware is already running. */
598 if (err == 0)
599 return 0;
601 if (err != 1)
602 return err;
604 pci_dev_get(pdev);
605 err = request_firmware(&fw, fw_name, &pdev->dev);
606 pci_dev_put(pdev);
607 if (err) {
608 dev_err(&pdev->dev, "request_firmware failed: %d\n", err);
609 return err;
612 err = renesas_fw_verify(fw->data, fw->size);
613 if (err)
614 goto exit;
616 err = renesas_load_fw(pdev, fw);
617 exit:
618 release_firmware(fw);
619 return err;
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");