kernel: bump 5.4 to 5.4.123
[openwrt/openwrt.git] / target / linux / apm821xx / patches-5.4 / 801-usb-xhci-add-firmware-loader-for-uPD720201-and-uPD72.patch
1 Subject: [PATCH v2 1/5] usb: xhci: add firmware loader for uPD720201 and uPD720202 w/o ROM
2 Date: Fri, 21 Jun 2019 14:29:09 +0530
3 Message-Id: <20190621085913.8722-2-vkoul@kernel.org>
4 From: Christian Lamparter <chunkeey@googlemail.com>
5
6 This patch adds a firmware loader for the uPD720201K8-711-BAC-A
7 and uPD720202K8-711-BAA-A variant. Both of these chips are listed
8 in Renesas' R19UH0078EJ0500 Rev.5.00 "User's Manual: Hardware" as
9 devices which need the firmware loader on page 2 in order to
10 work as they "do not support the External ROM".
11
12 The "Firmware Download Sequence" is describe in chapter
13 "7.1 FW Download Interface" R19UH0078EJ0500 Rev.5.00 page 131.
14
15 The firmware "K2013080.mem" is available from a USB3.0 Host to
16 PCIe Adapter (PP2U-E card) "Firmware download" archive. An
17 alternative version can be sourced from Netgear's WNDR4700 GPL
18 archives.
19
20 The release notes of the PP2U-E's "Firmware Download" ver 2.0.1.3
21 (2012-06-15) state that the firmware is for the following devices:
22 - uPD720201 ES 2.0 sample whose revision ID is 2.
23 - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
24 - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
25
26 Cc: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
27 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
28 Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
29 [vkoul: fixed comments:
30 used macros for timeout count and delay
31 removed renesas_fw_alive_check
32 cleaned renesas_fw_callback
33 removed recurion for renesas_fw_download
34 added MODULE_FIRMWARE]
35 Signed-off-by: Vinod Koul <vkoul@kernel.org>
36 ---
37 drivers/usb/host/xhci-pci.c | 458 ++++++++++++++++++++++++++++++++++++
38 1 file changed, 458 insertions(+)
39
40 --- a/drivers/usb/host/xhci-pci.c
41 +++ b/drivers/usb/host/xhci-pci.c
42 @@ -12,6 +12,8 @@
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/acpi.h>
46 +#include <linux/firmware.h>
47 +#include <linux/unaligned/access_ok.h>
48
49 #include "xhci.h"
50 #include "xhci-trace.h"
51 @@ -64,6 +66,44 @@
52 #define PCI_DEVICE_ID_ASMEDIA_2142_XHCI 0x2142
53 #define PCI_DEVICE_ID_ASMEDIA_3242_XHCI 0x3242
54
55 +#define RENESAS_FW_VERSION 0x6C
56 +#define RENESAS_ROM_CONFIG 0xF0
57 +#define RENESAS_FW_STATUS 0xF4
58 +#define RENESAS_FW_STATUS_MSB 0xF5
59 +#define RENESAS_ROM_STATUS 0xF6
60 +#define RENESAS_ROM_STATUS_MSB 0xF7
61 +#define RENESAS_DATA0 0xF8
62 +#define RENESAS_DATA1 0xFC
63 +
64 +#define RENESAS_FW_VERSION_FIELD GENMASK(23, 7)
65 +#define RENESAS_FW_VERSION_OFFSET 8
66 +
67 +#define RENESAS_FW_STATUS_DOWNLOAD_ENABLE BIT(0)
68 +#define RENESAS_FW_STATUS_LOCK BIT(1)
69 +#define RENESAS_FW_STATUS_RESULT GENMASK(6, 4)
70 + #define RENESAS_FW_STATUS_INVALID 0
71 + #define RENESAS_FW_STATUS_SUCCESS BIT(4)
72 + #define RENESAS_FW_STATUS_ERROR BIT(5)
73 +#define RENESAS_FW_STATUS_SET_DATA0 BIT(8)
74 +#define RENESAS_FW_STATUS_SET_DATA1 BIT(9)
75 +
76 +#define RENESAS_ROM_STATUS_ACCESS BIT(0)
77 +#define RENESAS_ROM_STATUS_ERASE BIT(1)
78 +#define RENESAS_ROM_STATUS_RELOAD BIT(2)
79 +#define RENESAS_ROM_STATUS_RESULT GENMASK(6, 4)
80 + #define RENESAS_ROM_STATUS_INVALID 0
81 + #define RENESAS_ROM_STATUS_SUCCESS BIT(4)
82 + #define RENESAS_ROM_STATUS_ERROR BIT(5)
83 +#define RENESAS_ROM_STATUS_SET_DATA0 BIT(8)
84 +#define RENESAS_ROM_STATUS_SET_DATA1 BIT(9)
85 +#define RENESAS_ROM_STATUS_ROM_EXISTS BIT(15)
86 +
87 +#define RENESAS_ROM_ERASE_MAGIC 0x5A65726F
88 +#define RENESAS_ROM_WRITE_MAGIC 0x53524F4D
89 +
90 +#define RENESAS_RETRY 1000
91 +#define RENESAS_DELAY 10
92 +
93 static const char hcd_name[] = "xhci_hcd";
94
95 static struct hc_driver __read_mostly xhci_pci_hc_driver;
96 @@ -309,6 +349,873 @@ static void xhci_pme_acpi_rtd3_enable(st
97 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
98 #endif /* CONFIG_ACPI */
99
100 +static const struct renesas_fw_entry {
101 + const char *firmware_name;
102 + u16 device;
103 + u8 revision;
104 + u16 expected_version;
105 +} renesas_fw_table[] = {
106 + /*
107 + * Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
108 + * are listed in R19UH0078EJ0500 Rev.5.00 as devices which
109 + * need the software loader.
110 + *
111 + * PP2U/ReleaseNote_USB3-201-202-FW.txt:
112 + *
113 + * Note: This firmware is for the following devices.
114 + * - uPD720201 ES 2.0 sample whose revision ID is 2.
115 + * - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
116 + * - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
117 + *
118 + * Entry expected_version should be kept in increasing order for a
119 + * chip, so that driver will pick first version and if that fails
120 + * then next one will be picked
121 + */
122 + { "K2013080.mem", 0x0014, 0x02, 0x2013 },
123 + { "K2013080.mem", 0x0014, 0x03, 0x2013 },
124 + { "K2026090.mem", 0x0014, 0x03, 0x2026 },
125 + { "K2013080.mem", 0x0015, 0x02, 0x2013 },
126 + { "K2026090.mem", 0x0015, 0x02, 0x2026 },
127 +};
128 +
129 +MODULE_FIRMWARE("K2013080.mem");
130 +MODULE_FIRMWARE("K2026090.mem");
131 +
132 +static const struct renesas_fw_entry *renesas_needs_fw_dl(struct pci_dev *dev)
133 +{
134 + const struct renesas_fw_entry *entry;
135 + size_t i;
136 +
137 + /* This loader will only work with a RENESAS device. */
138 + if (!(dev->vendor == PCI_VENDOR_ID_RENESAS))
139 + return NULL;
140 +
141 + for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
142 + entry = &renesas_fw_table[i];
143 + if (entry->device == dev->device &&
144 + entry->revision == dev->revision)
145 + return entry;
146 + }
147 +
148 + return NULL;
149 +}
150 +
151 +static const struct
152 +renesas_fw_entry *renesas_get_next_entry(struct pci_dev *dev,
153 + const struct renesas_fw_entry *entry)
154 +{
155 + const struct renesas_fw_entry *next_entry;
156 + size_t i;
157 +
158 + for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
159 + next_entry = &renesas_fw_table[i];
160 + if (next_entry->device == dev->device &&
161 + next_entry->revision == dev->revision &&
162 + next_entry->expected_version > entry->expected_version)
163 + return next_entry;
164 + }
165 +
166 + return NULL;
167 +}
168 +
169 +static int renesas_fw_download_image(struct pci_dev *dev,
170 + const u32 *fw,
171 + size_t step)
172 +{
173 + size_t i;
174 + int err;
175 + u8 fw_status;
176 + bool data0_or_data1;
177 +
178 + /*
179 + * The hardware does alternate between two 32-bit pages.
180 + * (This is because each row of the firmware is 8 bytes).
181 + *
182 + * for even steps we use DATA0, for odd steps DATA1.
183 + */
184 + data0_or_data1 = (step & 1) == 1;
185 +
186 + /* step+1. Read "Set DATAX" and confirm it is cleared. */
187 + for (i = 0; i < RENESAS_RETRY; i++) {
188 + err = pci_read_config_byte(dev, RENESAS_FW_STATUS_MSB,
189 + &fw_status);
190 + if (err)
191 + return pcibios_err_to_errno(err);
192 + if (!(fw_status & BIT(data0_or_data1)))
193 + break;
194 +
195 + udelay(RENESAS_DELAY);
196 + }
197 + if (i == RENESAS_RETRY)
198 + return -ETIMEDOUT;
199 +
200 + /*
201 + * step+2. Write FW data to "DATAX".
202 + * "LSB is left" => force little endian
203 + */
204 + err = pci_write_config_dword(dev, data0_or_data1 ?
205 + RENESAS_DATA1 : RENESAS_DATA0,
206 + (__force u32)cpu_to_le32(fw[step]));
207 + if (err)
208 + return pcibios_err_to_errno(err);
209 +
210 + udelay(100);
211 +
212 + /* step+3. Set "Set DATAX". */
213 + err = pci_write_config_byte(dev, RENESAS_FW_STATUS_MSB,
214 + BIT(data0_or_data1));
215 + if (err)
216 + return pcibios_err_to_errno(err);
217 +
218 + return 0;
219 +}
220 +
221 +static int renesas_fw_verify(struct pci_dev *dev,
222 + const void *fw_data,
223 + size_t length)
224 +{
225 + const struct renesas_fw_entry *entry = renesas_needs_fw_dl(dev);
226 + u16 fw_version_pointer;
227 + u16 fw_version;
228 +
229 + if (!entry)
230 + return -EINVAL;
231 +
232 + /*
233 + * The Firmware's Data Format is describe in
234 + * "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
235 + */
236 +
237 + /* "Each row is 8 bytes". => firmware size must be a multiple of 8. */
238 + if (length % 8 != 0)
239 + dev_warn(&dev->dev, "firmware size is not a multiple of 8.");
240 +
241 + /*
242 + * The bootrom chips of the big brother have sizes up to 64k, let's
243 + * assume that's the biggest the firmware can get.
244 + */
245 + if (length < 0x1000 || length >= 0x10000) {
246 + dev_err(&dev->dev, "firmware is size %zd is not (4k - 64k).",
247 + length);
248 + return -EINVAL;
249 + }
250 +
251 + /* The First 2 bytes are fixed value (55aa). "LSB on Left" */
252 + if (get_unaligned_le16(fw_data) != 0x55aa) {
253 + dev_err(&dev->dev, "no valid firmware header found.");
254 + return -EINVAL;
255 + }
256 +
257 + /* verify the firmware version position and print it. */
258 + fw_version_pointer = get_unaligned_le16(fw_data + 4);
259 + if (fw_version_pointer + 2 >= length) {
260 + dev_err(&dev->dev,
261 + "firmware version pointer is outside of the firmware image.");
262 + return -EINVAL;
263 + }
264 +
265 + fw_version = get_unaligned_le16(fw_data + fw_version_pointer);
266 + dev_dbg(&dev->dev, "got firmware version: %02x.", fw_version);
267 +
268 + if (fw_version != entry->expected_version) {
269 + dev_err(&dev->dev,
270 + "firmware version mismatch, expected version: %02x.",
271 + entry->expected_version);
272 + return -EINVAL;
273 + }
274 +
275 + return 0;
276 +}
277 +
278 +static int renesas_check_rom_state(struct pci_dev *pdev)
279 +{
280 + const struct renesas_fw_entry *entry;
281 + u16 rom_state;
282 + u32 version;
283 + bool valid_version = false;
284 + int err, i;
285 +
286 + /* check FW version */
287 + err = pci_read_config_dword(pdev, RENESAS_FW_VERSION, &version);
288 + if (err)
289 + return pcibios_err_to_errno(err);
290 +
291 + version &= RENESAS_FW_VERSION_FIELD;
292 + version = version >> RENESAS_FW_VERSION_OFFSET;
293 + dev_dbg(&pdev->dev, "Found FW version loaded is %x\n", version);
294 +
295 + /* treat version in renesas_fw_table as correct ones */
296 + for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
297 + entry = &renesas_fw_table[i];
298 + if (version == entry->expected_version) {
299 + dev_dbg(&pdev->dev, "Detected valid ROM version..\n");
300 + valid_version = true;
301 + }
302 + }
303 +
304 + /*
305 + * Test if ROM is present and loaded, if so we can skip everything
306 + */
307 + err = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_state);
308 + if (err)
309 + return pcibios_err_to_errno(err);
310 +
311 + if (rom_state & BIT(15)) {
312 + /* ROM exists */
313 + dev_dbg(&pdev->dev, "ROM exists\n");
314 +
315 + /* Check the "Result Code" Bits (6:4) and act accordingly */
316 + switch (rom_state & RENESAS_ROM_STATUS_RESULT) {
317 + case RENESAS_ROM_STATUS_SUCCESS:
318 + dev_dbg(&pdev->dev, "Success ROM load...");
319 + /* we have valid version and status so success */
320 + if (valid_version)
321 + return 0;
322 + break;
323 +
324 + case RENESAS_ROM_STATUS_INVALID: /* No result yet */
325 + dev_dbg(&pdev->dev, "No result as it is ROM...");
326 + /* we have valid version and status so success */
327 + if (valid_version)
328 + return 0;
329 + break;
330 +
331 + case RENESAS_ROM_STATUS_ERROR: /* Error State */
332 + default: /* All other states are marked as "Reserved states" */
333 + dev_err(&pdev->dev, "Invalid ROM..");
334 + break;
335 + }
336 + }
337 +
338 + return -EIO;
339 +}
340 +
341 +static int renesas_fw_check_running(struct pci_dev *pdev)
342 +{
343 + int err;
344 + u8 fw_state;
345 +
346 + /* Check if device has ROM and loaded, if so skip everything */
347 + err = renesas_check_rom_state(pdev);
348 + if (!err)
349 + return err;
350 +
351 + /*
352 + * Test if the device is actually needing the firmware. As most
353 + * BIOSes will initialize the device for us. If the device is
354 + * initialized.
355 + */
356 + err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
357 + if (err)
358 + return pcibios_err_to_errno(err);
359 +
360 + /*
361 + * Check if "FW Download Lock" is locked. If it is and the FW is
362 + * ready we can simply continue. If the FW is not ready, we have
363 + * to give up.
364 + */
365 + if (fw_state & RENESAS_FW_STATUS_LOCK) {
366 + dev_dbg(&pdev->dev, "FW Download Lock is engaged.");
367 +
368 + if (fw_state & RENESAS_FW_STATUS_SUCCESS)
369 + return 0;
370 +
371 + dev_err(&pdev->dev,
372 + "FW Download Lock is set and FW is not ready. Giving Up.");
373 + return -EIO;
374 + }
375 +
376 + /*
377 + * Check if "FW Download Enable" is set. If someone (us?) tampered
378 + * with it and it can't be resetted, we have to give up too... and
379 + * ask for a forgiveness and a reboot.
380 + */
381 + if (fw_state & RENESAS_FW_STATUS_DOWNLOAD_ENABLE) {
382 + dev_err(&pdev->dev,
383 + "FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
384 + return -EIO;
385 + }
386 +
387 + /* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
388 + switch (fw_state & RENESAS_FW_STATUS_RESULT) {
389 + case 0: /* No result yet */
390 + dev_dbg(&pdev->dev, "FW is not ready/loaded yet.");
391 +
392 + /* tell the caller, that this device needs the firmware. */
393 + return 1;
394 +
395 + case RENESAS_FW_STATUS_SUCCESS: /* Success, device should be working. */
396 + dev_dbg(&pdev->dev, "FW is ready.");
397 + return 0;
398 +
399 + case RENESAS_FW_STATUS_ERROR: /* Error State */
400 + dev_err(&pdev->dev,
401 + "hardware is in an error state. Giving up (poweroff/reboot needed).");
402 + return -ENODEV;
403 +
404 + default: /* All other states are marked as "Reserved states" */
405 + dev_err(&pdev->dev,
406 + "hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
407 + (fw_state & RENESAS_FW_STATUS_RESULT) >> 4);
408 + return -EINVAL;
409 + }
410 +}
411 +
412 +static int renesas_fw_download(struct pci_dev *pdev,
413 + const struct firmware *fw)
414 +{
415 + const u32 *fw_data = (const u32 *)fw->data;
416 + size_t i;
417 + int err;
418 + u8 fw_status;
419 +
420 + /*
421 + * For more information and the big picture: please look at the
422 + * "Firmware Download Sequence" in "7.1 FW Download Interface"
423 + * of R19UH0078EJ0500 Rev.5.00 page 131
424 + */
425 +
426 + /*
427 + * 0. Set "FW Download Enable" bit in the
428 + * "FW Download Control & Status Register" at 0xF4
429 + */
430 + err = pci_write_config_byte(pdev, RENESAS_FW_STATUS,
431 + RENESAS_FW_STATUS_DOWNLOAD_ENABLE);
432 + if (err)
433 + return pcibios_err_to_errno(err);
434 +
435 + /* 1 - 10 follow one step after the other. */
436 + for (i = 0; i < fw->size / 4; i++) {
437 + err = renesas_fw_download_image(pdev, fw_data, i);
438 + if (err) {
439 + dev_err(&pdev->dev,
440 + "Firmware Download Step %zd failed at position %zd bytes with (%d).",
441 + i, i * 4, err);
442 + return err;
443 + }
444 + }
445 +
446 + /*
447 + * This sequence continues until the last data is written to
448 + * "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
449 + * is cleared by the hardware beforehand.
450 + */
451 + for (i = 0; i < RENESAS_RETRY; i++) {
452 + err = pci_read_config_byte(pdev, RENESAS_FW_STATUS_MSB,
453 + &fw_status);
454 + if (err)
455 + return pcibios_err_to_errno(err);
456 + if (!(fw_status & (BIT(0) | BIT(1))))
457 + break;
458 +
459 + udelay(RENESAS_DELAY);
460 + }
461 + if (i == RENESAS_RETRY)
462 + dev_warn(&pdev->dev, "Final Firmware Download step timed out.");
463 +
464 + /*
465 + * 11. After finishing writing the last data of FW, the
466 + * System Software must clear "FW Download Enable"
467 + */
468 + err = pci_write_config_byte(pdev, RENESAS_FW_STATUS, 0);
469 + if (err)
470 + return pcibios_err_to_errno(err);
471 +
472 + /* 12. Read "Result Code" and confirm it is good. */
473 + for (i = 0; i < RENESAS_RETRY; i++) {
474 + err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_status);
475 + if (err)
476 + return pcibios_err_to_errno(err);
477 + if (fw_status & RENESAS_FW_STATUS_SUCCESS)
478 + break;
479 +
480 + udelay(RENESAS_DELAY);
481 + }
482 + if (i == RENESAS_RETRY) {
483 + /* Timed out / Error - let's see if we can fix this */
484 + err = renesas_fw_check_running(pdev);
485 + switch (err) {
486 + case 0: /*
487 + * we shouldn't end up here.
488 + * maybe it took a little bit longer.
489 + * But all should be well?
490 + */
491 + break;
492 +
493 + case 1: /* (No result yet! */
494 + return -ETIMEDOUT;
495 +
496 + default:
497 + return err;
498 + }
499 + }
500 + /*
501 + * Optional last step: Engage Firmware Lock
502 + *
503 + * err = pci_write_config_byte(pdev, 0xF4, BIT(2));
504 + * if (err)
505 + * return pcibios_err_to_errno(err);
506 + */
507 +
508 + return 0;
509 +}
510 +
511 +struct renesas_fw_ctx {
512 + struct pci_dev *pdev;
513 + const struct pci_device_id *id;
514 + bool resume;
515 + const struct renesas_fw_entry *entry;
516 +};
517 +
518 +static int xhci_pci_probe(struct pci_dev *pdev,
519 + const struct pci_device_id *id);
520 +
521 +static bool renesas_check_rom(struct pci_dev *pdev)
522 +{
523 + u16 rom_status;
524 + int retval;
525 +
526 + /* 1. Check if external ROM exists */
527 + retval = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_status);
528 + if (retval)
529 + return false;
530 +
531 + rom_status &= RENESAS_ROM_STATUS_ROM_EXISTS;
532 + if (rom_status) {
533 + dev_dbg(&pdev->dev, "External ROM exists\n");
534 + return true; /* External ROM exists */
535 + }
536 +
537 + return false;
538 +}
539 +
540 +static void renesas_rom_erase(struct pci_dev *pdev)
541 +{
542 + int retval, i;
543 + u8 status;
544 +
545 + dev_dbg(&pdev->dev, "Performing ROM Erase...\n");
546 + retval = pci_write_config_dword(pdev, RENESAS_DATA0,
547 + RENESAS_ROM_ERASE_MAGIC);
548 + if (retval) {
549 + dev_err(&pdev->dev, "ROM erase, magic word write failed: %d\n",
550 + pcibios_err_to_errno(retval));
551 + return;
552 + }
553 +
554 + retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
555 + if (retval) {
556 + dev_err(&pdev->dev, "ROM status read failed: %d\n",
557 + pcibios_err_to_errno(retval));
558 + return;
559 + }
560 + status |= RENESAS_ROM_STATUS_ERASE;
561 + retval = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, status);
562 + if (retval) {
563 + dev_err(&pdev->dev, "ROM erase set word write failed\n");
564 + return;
565 + }
566 +
567 + /* sleep a bit while ROM is erased */
568 + msleep(20);
569 +
570 + for (i = 0; i < RENESAS_RETRY; i++) {
571 + retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS,
572 + &status);
573 + status &= RENESAS_ROM_STATUS_ERASE;
574 + if (!status)
575 + break;
576 +
577 + mdelay(RENESAS_DELAY);
578 + }
579 +
580 + if (i == RENESAS_RETRY)
581 + dev_dbg(&pdev->dev, "Chip erase timedout: %x\n", status);
582 +
583 + dev_dbg(&pdev->dev, "ROM Erase... Done success\n");
584 +}
585 +
586 +static bool renesas_download_rom(struct pci_dev *pdev,
587 + const u32 *fw, size_t step)
588 +{
589 + bool data0_or_data1;
590 + u8 fw_status;
591 + size_t i;
592 + int err;
593 +
594 + /*
595 + * The hardware does alternate between two 32-bit pages.
596 + * (This is because each row of the firmware is 8 bytes).
597 + *
598 + * for even steps we use DATA0, for odd steps DATA1.
599 + */
600 + data0_or_data1 = (step & 1) == 1;
601 +
602 + /* Read "Set DATAX" and confirm it is cleared. */
603 + for (i = 0; i < RENESAS_RETRY; i++) {
604 + err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS_MSB,
605 + &fw_status);
606 + if (err) {
607 + dev_err(&pdev->dev, "Read ROM Status failed: %d\n",
608 + pcibios_err_to_errno(err));
609 + return false;
610 + }
611 + if (!(fw_status & BIT(data0_or_data1)))
612 + break;
613 +
614 + udelay(RENESAS_DELAY);
615 + }
616 + if (i == RENESAS_RETRY) {
617 + dev_err(&pdev->dev, "Timeout for Set DATAX step: %zd\n", step);
618 + return false;
619 + }
620 +
621 + /*
622 + * Write FW data to "DATAX".
623 + * "LSB is left" => force little endian
624 + */
625 + err = pci_write_config_dword(pdev, data0_or_data1 ?
626 + RENESAS_DATA1 : RENESAS_DATA0,
627 + (__force u32)cpu_to_le32(fw[step]));
628 + if (err) {
629 + dev_err(&pdev->dev, "Write to DATAX failed: %d\n",
630 + pcibios_err_to_errno(err));
631 + return false;
632 + }
633 +
634 + udelay(100);
635 +
636 + /* Set "Set DATAX". */
637 + err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS_MSB,
638 + BIT(data0_or_data1));
639 + if (err) {
640 + dev_err(&pdev->dev, "Write config for DATAX failed: %d\n",
641 + pcibios_err_to_errno(err));
642 + return false;
643 + }
644 +
645 + return true;
646 +}
647 +
648 +static bool renesas_setup_rom(struct pci_dev *pdev, const struct firmware *fw)
649 +{
650 + const u32 *fw_data = (const u32 *)fw->data;
651 + int err, i;
652 + u8 status;
653 +
654 + /* 2. Write magic word to Data0 */
655 + err = pci_write_config_dword(pdev, RENESAS_DATA0,
656 + RENESAS_ROM_WRITE_MAGIC);
657 + if (err)
658 + return false;
659 +
660 + /* 3. Set External ROM access */
661 + err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
662 + RENESAS_ROM_STATUS_ACCESS);
663 + if (err)
664 + goto remove_bypass;
665 +
666 + /* 4. Check the result */
667 + err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
668 + if (err)
669 + goto remove_bypass;
670 + status &= GENMASK(6, 4);
671 + if (status) {
672 + dev_err(&pdev->dev,
673 + "setting external rom failed: %x\n", status);
674 + goto remove_bypass;
675 + }
676 +
677 + /* 5 to 16 Write FW to DATA0/1 while checking SetData0/1 */
678 + for (i = 0; i < fw->size / 4; i++) {
679 + err = renesas_download_rom(pdev, fw_data, i);
680 + if (!err) {
681 + dev_err(&pdev->dev,
682 + "ROM Download Step %d failed at position %d bytes\n",
683 + i, i * 4);
684 + goto remove_bypass;
685 + }
686 + }
687 +
688 + /*
689 + * wait till DATA0/1 is cleared
690 + */
691 + for (i = 0; i < RENESAS_RETRY; i++) {
692 + err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS_MSB,
693 + &status);
694 + if (err)
695 + goto remove_bypass;
696 + if (!(status & (BIT(0) | BIT(1))))
697 + break;
698 +
699 + udelay(RENESAS_DELAY);
700 + }
701 + if (i == RENESAS_RETRY) {
702 + dev_err(&pdev->dev, "Final Firmware ROM Download step timed out\n");
703 + goto remove_bypass;
704 + }
705 +
706 + /* 17. Remove bypass */
707 + err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
708 + if (err)
709 + return false;
710 +
711 + udelay(10);
712 +
713 + /* 18. check result */
714 + for (i = 0; i < RENESAS_RETRY; i++) {
715 + err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
716 + if (err) {
717 + dev_err(&pdev->dev, "Read ROM status failed:%d\n",
718 + pcibios_err_to_errno(err));
719 + return false;
720 + }
721 + status &= RENESAS_ROM_STATUS_RESULT;
722 + if (status == RENESAS_ROM_STATUS_SUCCESS) {
723 + dev_dbg(&pdev->dev, "Download ROM success\n");
724 + break;
725 + }
726 + udelay(RENESAS_DELAY);
727 + }
728 + if (i == RENESAS_RETRY) { /* Timed out */
729 + dev_err(&pdev->dev,
730 + "Download to external ROM TO: %x\n", status);
731 + return false;
732 + }
733 +
734 + dev_dbg(&pdev->dev, "Download to external ROM scuceeded\n");
735 +
736 + /* Last step set Reload */
737 + err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
738 + RENESAS_ROM_STATUS_RELOAD);
739 + if (err) {
740 + dev_err(&pdev->dev, "Set ROM execute failed: %d\n",
741 + pcibios_err_to_errno(err));
742 + return false;
743 + }
744 +
745 + /*
746 + * wait till Reload is cleared
747 + */
748 + for (i = 0; i < RENESAS_RETRY; i++) {
749 + err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
750 + if (err)
751 + return false;
752 + if (!(status & RENESAS_ROM_STATUS_RELOAD))
753 + break;
754 +
755 + udelay(RENESAS_DELAY);
756 + }
757 + if (i == RENESAS_RETRY) {
758 + dev_err(&pdev->dev, "ROM Exec timed out: %x\n", status);
759 + return false;
760 + }
761 +
762 + return true;
763 +
764 +remove_bypass:
765 + pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
766 + return false;
767 +}
768 +
769 +static void renesas_fw_callback(const struct firmware *fw,
770 + void *context)
771 +{
772 + struct renesas_fw_ctx *ctx = context;
773 + struct pci_dev *pdev = ctx->pdev;
774 + struct device *parent = pdev->dev.parent;
775 + const struct renesas_fw_entry *next_entry;
776 + bool rom;
777 + int err;
778 +
779 + if (!fw) {
780 + dev_err(&pdev->dev, "firmware failed to load\n");
781 + /*
782 + * we didn't find firmware, check if we have another
783 + * entry for this device
784 + */
785 + next_entry = renesas_get_next_entry(ctx->pdev, ctx->entry);
786 + if (next_entry) {
787 + ctx->entry = next_entry;
788 + dev_dbg(&pdev->dev, "Found next entry, requesting: %s\n",
789 + next_entry->firmware_name);
790 + request_firmware_nowait(THIS_MODULE, 1,
791 + next_entry->firmware_name,
792 + &pdev->dev, GFP_KERNEL,
793 + ctx, renesas_fw_callback);
794 + return;
795 + } else {
796 + goto cleanup;
797 + }
798 + }
799 +
800 + err = renesas_fw_verify(pdev, fw->data, fw->size);
801 + if (err)
802 + goto cleanup;
803 +
804 + /* Check if the device has external ROM */
805 + rom = renesas_check_rom(pdev);
806 + if (rom) {
807 + /* perfrom chip erase first */
808 + renesas_rom_erase(pdev);
809 +
810 + /* lets try loading fw on ROM first */
811 + rom = renesas_setup_rom(pdev, fw);
812 + if (!rom) {
813 + dev_err(&pdev->dev,
814 + "ROM load failed, falling back on FW load\n");
815 + } else {
816 + dev_dbg(&pdev->dev, "ROM load done..\n");
817 +
818 + release_firmware(fw);
819 + goto do_probe;
820 + }
821 + }
822 +
823 + err = renesas_fw_download(pdev, fw);
824 + release_firmware(fw);
825 + if (err) {
826 + dev_err(&pdev->dev, "firmware failed to download (%d).", err);
827 + goto cleanup;
828 + }
829 +
830 +do_probe:
831 + if (ctx->resume)
832 + return;
833 +
834 + err = xhci_pci_probe(pdev, ctx->id);
835 + if (!err) {
836 + /* everything worked */
837 + devm_kfree(&pdev->dev, ctx);
838 + return;
839 + }
840 +
841 +cleanup:
842 + /* in case of an error - fall through */
843 + dev_info(&pdev->dev, "Unloading driver");
844 +
845 + if (parent)
846 + device_lock(parent);
847 +
848 + device_release_driver(&pdev->dev);
849 +
850 + if (parent)
851 + device_unlock(parent);
852 +
853 + pci_dev_put(pdev);
854 +}
855 +
856 +static int renesas_fw_alive_check(struct pci_dev *pdev)
857 +{
858 + const struct renesas_fw_entry *entry;
859 +
860 + /* check if we have a eligible RENESAS' uPD720201/2 w/o FW. */
861 + entry = renesas_needs_fw_dl(pdev);
862 + if (!entry)
863 + return 0;
864 +
865 + return renesas_fw_check_running(pdev);
866 +}
867 +
868 +static int renesas_fw_download_to_hw(struct pci_dev *pdev,
869 + const struct pci_device_id *id,
870 + bool do_resume)
871 +{
872 + const struct renesas_fw_entry *entry;
873 + struct renesas_fw_ctx *ctx;
874 + int err;
875 +
876 + /* check if we have a eligible RENESAS' uPD720201/2 w/o FW. */
877 + entry = renesas_needs_fw_dl(pdev);
878 + if (!entry)
879 + return 0;
880 +
881 + err = renesas_fw_check_running(pdev);
882 + /* Continue ahead, if the firmware is already running. */
883 + if (err == 0)
884 + return 0;
885 +
886 + if (err != 1)
887 + return err;
888 +
889 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
890 + if (!ctx)
891 + return -ENOMEM;
892 + ctx->pdev = pdev;
893 + ctx->resume = do_resume;
894 + ctx->id = id;
895 + ctx->entry = entry;
896 +
897 + pci_dev_get(pdev);
898 + err = request_firmware_nowait(THIS_MODULE, 1, entry->firmware_name,
899 + &pdev->dev, GFP_KERNEL,
900 + ctx, renesas_fw_callback);
901 + if (err) {
902 + pci_dev_put(pdev);
903 + return err;
904 + }
905 +
906 + /*
907 + * The renesas_fw_callback() callback will continue the probe
908 + * process, once it aquires the firmware.
909 + */
910 + return 1;
911 +}
912 +
913 +static int renesas_check_if_fw_dl_is_needed(struct pci_dev *pdev)
914 +{
915 + int err;
916 + u8 fw_state;
917 +
918 + /*
919 + * Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
920 + * are listed in R19UH0078EJ0500 Rev.5.00 as devices which
921 + * need a firmware in order to work.
922 + *
923 + * - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
924 + * - uPD720201 ES 2.0 sample whose revision ID is 2.
925 + * - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
926 + */
927 + if (!((pdev->vendor == PCI_VENDOR_ID_RENESAS) &&
928 + ((pdev->device == 0x0015 && pdev->revision == 0x02) ||
929 + (pdev->device == 0x0014 &&
930 + (pdev->revision == 0x02 || pdev->revision == 0x03)))))
931 + return 0;
932 +
933 + /* Check if device has ROM and loaded, if so skip everything */
934 + err = renesas_check_rom_state(pdev);
935 + if (!err)
936 + return err;
937 +
938 + /*
939 + * Test if the firmware was uploaded and is running.
940 + * As most BIOSes will initialize the device for us.
941 + */
942 + err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
943 + if (err)
944 + return pcibios_err_to_errno(err);
945 +
946 + /* Check the "Result Code" Bits (6:4) and act accordingly */
947 + switch (fw_state & RENESAS_FW_STATUS_RESULT) {
948 + case 0: /* No result yet */
949 + dev_err(&pdev->dev, "FW is not ready/loaded yet.");
950 + return -ENODEV;
951 +
952 + case RENESAS_FW_STATUS_SUCCESS:
953 + dev_dbg(&pdev->dev, "FW is ready.");
954 + return 0;
955 +
956 + case RENESAS_FW_STATUS_ERROR:
957 + dev_err(&pdev->dev, "HW is in an error state.");
958 + return -ENODEV;
959 +
960 + default: /* All other states are marked as "Reserved states" */
961 + dev_err(&pdev->dev, "HW is in an invalid state (%x).",
962 + (fw_state & 0x70) >> 4);
963 + return -EINVAL;
964 + }
965 +}
966 +
967 /* called during probe() after chip reset completes */
968 static int xhci_pci_setup(struct usb_hcd *hcd)
969 {
970 @@ -350,6 +1257,27 @@ static int xhci_pci_probe(struct pci_dev
971 struct hc_driver *driver;
972 struct usb_hcd *hcd;
973
974 + /*
975 + * Check if this device is a RENESAS uPD720201/2 device.
976 + * Otherwise, we can continue with xhci_pci_probe as usual.
977 + */
978 + retval = renesas_fw_download_to_hw(dev, id, false);
979 + switch (retval) {
980 + case 0:
981 + break;
982 +
983 + case 1: /* let it load the firmware and recontinue the probe. */
984 + return 0;
985 +
986 + default:
987 + return retval;
988 + };
989 +
990 + /* Check if this device is a RENESAS uPD720201/2 device. */
991 + retval = renesas_check_if_fw_dl_is_needed(dev);
992 + if (retval)
993 + return retval;
994 +
995 driver = (struct hc_driver *)id->driver_data;
996
997 /* Prevent runtime suspending between USB-2 and USB-3 initialization */
998 @@ -411,6 +1339,16 @@ static void xhci_pci_remove(struct pci_d
999 {
1000 struct xhci_hcd *xhci;
1001
1002 + if (renesas_fw_alive_check(dev)) {
1003 + /*
1004 + * bail out early, if this was a renesas device w/o FW.
1005 + * Else we might hit the NMI watchdog in xhci_handsake
1006 + * during xhci_reset as part of the driver's unloading.
1007 + * which we forced in the renesas_fw_callback().
1008 + */
1009 + return;
1010 + }
1011 +
1012 xhci = hcd_to_xhci(pci_get_drvdata(dev));
1013 xhci->xhc_state |= XHCI_STATE_REMOVING;
1014
1015 @@ -550,6 +1488,11 @@ static int xhci_pci_resume(struct usb_hc
1016 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
1017 usb_enable_intel_xhci_ports(pdev);
1018
1019 + /* Check if this device is a RENESAS uPD720201/2 device. */
1020 + retval = renesas_check_if_fw_dl_is_needed(pdev);
1021 + if (retval)
1022 + return retval;
1023 +
1024 if (xhci->quirks & XHCI_SSIC_PORT_UNUSED)
1025 xhci_ssic_port_unused_quirk(hcd, false);
1026