801-usb-xhci-add-firmware-loader-for-uPD720201-and-uPD72.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. From 419992bae5aaa4e06402e0b7c79fcf7bcb6b4764 Mon Sep 17 00:00:00 2001
  2. From: Christian Lamparter <chunkeey@googlemail.com>
  3. Date: Thu, 2 Jun 2016 00:48:46 +0200
  4. Subject: [PATCH] usb: xhci: add firmware loader for uPD720201 and uPD720202
  5. w/o ROM
  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. The "Firmware Download Sequence" is describe in chapter
  12. "7.1 FW Download Interface" R19UH0078EJ0500 Rev.5.00 page 131.
  13. The firmware "K2013080.mem" is available from a USB3.0 Host to
  14. PCIe Adapter (PP2U-E card) "Firmware download" archive. An
  15. alternative version can be sourced from Netgear's WNDR4700 GPL
  16. archives.
  17. The release notes of the PP2U-E's "Firmware Download" ver 2.0.1.3
  18. (2012-06-15) state that the firmware is for the following devices:
  19. - uPD720201 ES 2.0 sample whose revision ID is 2.
  20. - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
  21. - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
  22. If someone from Renesas is listening: It would be great, if these
  23. firmwares could be added to linux-firmware.git.
  24. Cc: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
  25. Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
  26. ---
  27. drivers/usb/host/xhci-pci.c | 492 ++++++++++++++++++++++++++++++++++++++++++++
  28. 1 file changed, 492 insertions(+)
  29. --- a/drivers/usb/host/xhci-pci.c
  30. +++ b/drivers/usb/host/xhci-pci.c
  31. @@ -24,6 +24,8 @@
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/acpi.h>
  35. +#include <linux/firmware.h>
  36. +#include <asm/unaligned.h>
  37. #include "xhci.h"
  38. #include "xhci-trace.h"
  39. @@ -229,6 +231,458 @@ static void xhci_pme_acpi_rtd3_enable(st
  40. static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
  41. #endif /* CONFIG_ACPI */
  42. +static const struct renesas_fw_entry {
  43. + const char *firmware_name;
  44. + u16 device;
  45. + u8 revision;
  46. + u16 expected_version;
  47. +} renesas_fw_table[] = {
  48. + /*
  49. + * Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
  50. + * are listed in R19UH0078EJ0500 Rev.5.00 as devices which
  51. + * need the software loader.
  52. + *
  53. + * PP2U/ReleaseNote_USB3-201-202-FW.txt:
  54. + *
  55. + * Note: This firmware is for the following devices.
  56. + * - uPD720201 ES 2.0 sample whose revision ID is 2.
  57. + * - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
  58. + * - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
  59. + */
  60. + { "K2013080.mem", 0x0014, 0x02, 0x2013 },
  61. + { "K2013080.mem", 0x0014, 0x03, 0x2013 },
  62. + { "K2013080.mem", 0x0015, 0x02, 0x2013 },
  63. +};
  64. +
  65. +static const struct renesas_fw_entry *renesas_needs_fw_dl(struct pci_dev *dev)
  66. +{
  67. + const struct renesas_fw_entry *entry;
  68. + size_t i;
  69. +
  70. + /* This loader will only work with a RENESAS device. */
  71. + if (!(dev->vendor == PCI_VENDOR_ID_RENESAS))
  72. + return NULL;
  73. +
  74. + for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
  75. + entry = &renesas_fw_table[i];
  76. + if (entry->device == dev->device &&
  77. + entry->revision == dev->revision)
  78. + return entry;
  79. + }
  80. +
  81. + return NULL;
  82. +}
  83. +
  84. +static int renesas_fw_download_image(struct pci_dev *dev,
  85. + const u32 *fw,
  86. + size_t step)
  87. +{
  88. + size_t i;
  89. + int err;
  90. + u8 fw_status;
  91. + bool data0_or_data1;
  92. +
  93. + /*
  94. + * The hardware does alternate between two 32-bit pages.
  95. + * (This is because each row of the firmware is 8 bytes).
  96. + *
  97. + * for even steps we use DATA0, for odd steps DATA1.
  98. + */
  99. + data0_or_data1 = (step & 1) == 1;
  100. +
  101. + /* step+1. Read "Set DATAX" and confirm it is cleared. */
  102. + for (i = 0; i < 10000; i++) {
  103. + err = pci_read_config_byte(dev, 0xF5, &fw_status);
  104. + if (err)
  105. + return pcibios_err_to_errno(err);
  106. + if (!(fw_status & BIT(data0_or_data1)))
  107. + break;
  108. +
  109. + udelay(1);
  110. + }
  111. + if (i == 10000)
  112. + return -ETIMEDOUT;
  113. +
  114. + /*
  115. + * step+2. Write FW data to "DATAX".
  116. + * "LSB is left" => force little endian
  117. + */
  118. + err = pci_write_config_dword(dev, data0_or_data1 ? 0xFC : 0xF8,
  119. + (__force u32) cpu_to_le32(fw[step]));
  120. + if (err)
  121. + return pcibios_err_to_errno(err);
  122. +
  123. + udelay(100);
  124. +
  125. + /* step+3. Set "Set DATAX". */
  126. + err = pci_write_config_byte(dev, 0xF5, BIT(data0_or_data1));
  127. + if (err)
  128. + return pcibios_err_to_errno(err);
  129. +
  130. + return 0;
  131. +}
  132. +
  133. +static int renesas_fw_verify(struct pci_dev *dev,
  134. + const void *fw_data,
  135. + size_t length)
  136. +{
  137. + const struct renesas_fw_entry *entry = renesas_needs_fw_dl(dev);
  138. + u16 fw_version_pointer;
  139. + u16 fw_version;
  140. +
  141. + if (!entry)
  142. + return -EINVAL;
  143. +
  144. + /*
  145. + * The Firmware's Data Format is describe in
  146. + * "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
  147. + */
  148. +
  149. + /* "Each row is 8 bytes". => firmware size must be a multiple of 8. */
  150. + if (length % 8 != 0) {
  151. + dev_err(&dev->dev, "firmware size is not a multipe of 8.");
  152. + return -EINVAL;
  153. + }
  154. +
  155. + /*
  156. + * The bootrom chips of the big brother have sizes up to 64k, let's
  157. + * assume that's the biggest the firmware can get.
  158. + */
  159. + if (length < 0x1000 || length >= 0x10000) {
  160. + dev_err(&dev->dev, "firmware is size %zd is not (4k - 64k).",
  161. + length);
  162. + return -EINVAL;
  163. + }
  164. +
  165. + /* The First 2 bytes are fixed value (55aa). "LSB on Left" */
  166. + if (get_unaligned_le16(fw_data) != 0x55aa) {
  167. + dev_err(&dev->dev, "no valid firmware header found.");
  168. + return -EINVAL;
  169. + }
  170. +
  171. + /* verify the firmware version position and print it. */
  172. + fw_version_pointer = get_unaligned_le16(fw_data + 4);
  173. + if (fw_version_pointer + 2 >= length) {
  174. + dev_err(&dev->dev, "firmware version pointer is outside of the firmware image.");
  175. + return -EINVAL;
  176. + }
  177. +
  178. + fw_version = get_unaligned_le16(fw_data + fw_version_pointer);
  179. + dev_dbg(&dev->dev, "got firmware version: %02x.", fw_version);
  180. +
  181. + if (fw_version != entry->expected_version) {
  182. + dev_err(&dev->dev, "firmware version mismatch, expected version: %02x.",
  183. + entry->expected_version);
  184. + return -EINVAL;
  185. + }
  186. +
  187. + return 0;
  188. +}
  189. +
  190. +static int renesas_fw_check_running(struct pci_dev *pdev)
  191. +{
  192. + int err;
  193. + u8 fw_state;
  194. +
  195. + /*
  196. + * Test if the device is actually needing the firmware. As most
  197. + * BIOSes will initialize the device for us. If the device is
  198. + * initialized.
  199. + */
  200. + err = pci_read_config_byte(pdev, 0xF4, &fw_state);
  201. + if (err)
  202. + return pcibios_err_to_errno(err);
  203. +
  204. + /*
  205. + * Check if "FW Download Lock" is locked. If it is and the FW is
  206. + * ready we can simply continue. If the FW is not ready, we have
  207. + * to give up.
  208. + */
  209. + if (fw_state & BIT(1)) {
  210. + dev_dbg(&pdev->dev, "FW Download Lock is engaged.");
  211. +
  212. + if (fw_state & BIT(4))
  213. + return 0;
  214. +
  215. + dev_err(&pdev->dev, "FW Download Lock is set and FW is not ready. Giving Up.");
  216. + return -EIO;
  217. + }
  218. +
  219. + /*
  220. + * Check if "FW Download Enable" is set. If someone (us?) tampered
  221. + * with it and it can't be resetted, we have to give up too... and
  222. + * ask for a forgiveness and a reboot.
  223. + */
  224. + if (fw_state & BIT(0)) {
  225. + dev_err(&pdev->dev, "FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
  226. + return -EIO;
  227. + }
  228. +
  229. + /* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
  230. + switch ((fw_state & 0x70)) {
  231. + case 0: /* No result yet */
  232. + dev_dbg(&pdev->dev, "FW is not ready/loaded yet.");
  233. +
  234. + /* tell the caller, that this device needs the firmware. */
  235. + return 1;
  236. +
  237. + case BIT(4): /* Success, device should be working. */
  238. + dev_dbg(&pdev->dev, "FW is ready.");
  239. + return 0;
  240. +
  241. + case BIT(5): /* Error State */
  242. + dev_err(&pdev->dev, "hardware is in an error state. Giving up (poweroff/reboot needed).");
  243. + return -ENODEV;
  244. +
  245. + default: /* All other states are marked as "Reserved states" */
  246. + dev_err(&pdev->dev, "hardware is in an invalid state %x. Giving up (poweroff/reboot needed).",
  247. + (fw_state & 0x70) >> 4);
  248. + return -EINVAL;
  249. + }
  250. +}
  251. +
  252. +static int renesas_hw_check_run_stop_busy(struct pci_dev *pdev)
  253. +{
  254. +#if 0
  255. + u32 val;
  256. +
  257. + /*
  258. + * 7.1.3 Note 3: "... must not set 'FW Download Enable' when
  259. + * 'RUN/STOP' of USBCMD Register is set"
  260. + */
  261. + val = readl(hcd->regs + 0x20);
  262. + if (val & BIT(0)) {
  263. + dev_err(&pdev->dev, "hardware is busy and can't receive a FW.");
  264. + return -EBUSY;
  265. + }
  266. +#endif
  267. + return 0;
  268. +}
  269. +
  270. +static int renesas_fw_download(struct pci_dev *pdev,
  271. + const struct firmware *fw, unsigned int retry_counter)
  272. +{
  273. + const u32 *fw_data = (const u32 *) fw->data;
  274. + size_t i;
  275. + int err;
  276. + u8 fw_status;
  277. +
  278. + /*
  279. + * For more information and the big picture: please look at the
  280. + * "Firmware Download Sequence" in "7.1 FW Download Interface"
  281. + * of R19UH0078EJ0500 Rev.5.00 page 131
  282. + */
  283. + err = renesas_hw_check_run_stop_busy(pdev);
  284. + if (err)
  285. + return err;
  286. +
  287. + /*
  288. + * 0. Set "FW Download Enable" bit in the
  289. + * "FW Download Control & Status Register" at 0xF4
  290. + */
  291. + err = pci_write_config_byte(pdev, 0xF4, BIT(0));
  292. + if (err)
  293. + return pcibios_err_to_errno(err);
  294. +
  295. + /* 1 - 10 follow one step after the other. */
  296. + for (i = 0; i < fw->size / 4; i++) {
  297. + err = renesas_fw_download_image(pdev, fw_data, i);
  298. + if (err) {
  299. + dev_err(&pdev->dev, "Firmware Download Step %zd failed at position %zd bytes with (%d).",
  300. + i, i * 4, err);
  301. + return err;
  302. + }
  303. + }
  304. +
  305. + /*
  306. + * This sequence continues until the last data is written to
  307. + * "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
  308. + * is cleared by the hardware beforehand.
  309. + */
  310. + for (i = 0; i < 10000; i++) {
  311. + err = pci_read_config_byte(pdev, 0xF5, &fw_status);
  312. + if (err)
  313. + return pcibios_err_to_errno(err);
  314. + if (!(fw_status & (BIT(0) | BIT(1))))
  315. + break;
  316. +
  317. + udelay(1);
  318. + }
  319. + if (i == 10000)
  320. + dev_warn(&pdev->dev, "Final Firmware Download step timed out.");
  321. +
  322. + /*
  323. + * 11. After finishing writing the last data of FW, the
  324. + * System Software must clear "FW Download Enable"
  325. + */
  326. + err = pci_write_config_byte(pdev, 0xF4, 0);
  327. + if (err)
  328. + return pcibios_err_to_errno(err);
  329. +
  330. + /* 12. Read "Result Code" and confirm it is good. */
  331. + for (i = 0; i < 10000; i++) {
  332. + err = pci_read_config_byte(pdev, 0xF4, &fw_status);
  333. + if (err)
  334. + return pcibios_err_to_errno(err);
  335. + if (fw_status & BIT(4))
  336. + break;
  337. +
  338. + udelay(1);
  339. + }
  340. + if (i == 10000) {
  341. + /* Timed out / Error - let's see if we can fix this */
  342. + err = renesas_fw_check_running(pdev);
  343. + switch (err) {
  344. + case 0: /*
  345. + * we shouldn't end up here.
  346. + * maybe it took a little bit longer.
  347. + * But all should be well?
  348. + */
  349. + break;
  350. +
  351. + case 1: /* (No result yet? - we can try to retry) */
  352. + if (retry_counter < 10) {
  353. + retry_counter++;
  354. + dev_warn(&pdev->dev, "Retry Firmware download: %d try.",
  355. + retry_counter);
  356. + return renesas_fw_download(pdev, fw,
  357. + retry_counter);
  358. + }
  359. + return -ETIMEDOUT;
  360. +
  361. + default:
  362. + return err;
  363. + }
  364. + }
  365. + /*
  366. + * Optional last step: Engage Firmware Lock
  367. + *
  368. + * err = pci_write_config_byte(pdev, 0xF4, BIT(2));
  369. + * if (err)
  370. + * return pcibios_err_to_errno(err);
  371. + */
  372. +
  373. + return 0;
  374. +}
  375. +
  376. +struct renesas_fw_ctx {
  377. + struct pci_dev *pdev;
  378. + const struct pci_device_id *id;
  379. + bool resume;
  380. +};
  381. +
  382. +static int xhci_pci_probe(struct pci_dev *pdev,
  383. + const struct pci_device_id *id);
  384. +
  385. +static void renesas_fw_callback(const struct firmware *fw,
  386. + void *context)
  387. +{
  388. + struct renesas_fw_ctx *ctx = context;
  389. + struct pci_dev *pdev = ctx->pdev;
  390. + struct device *parent = pdev->dev.parent;
  391. + int err = -ENOENT;
  392. +
  393. + if (fw) {
  394. + err = renesas_fw_verify(pdev, fw->data, fw->size);
  395. + if (!err) {
  396. + err = renesas_fw_download(pdev, fw, 0);
  397. + release_firmware(fw);
  398. + if (!err) {
  399. + if (ctx->resume)
  400. + return;
  401. +
  402. + err = xhci_pci_probe(pdev, ctx->id);
  403. + if (!err) {
  404. + /* everything worked */
  405. + devm_kfree(&pdev->dev, ctx);
  406. + return;
  407. + }
  408. +
  409. + /* in case of an error - fall through */
  410. + } else {
  411. + dev_err(&pdev->dev, "firmware failed to download (%d).",
  412. + err);
  413. + }
  414. + }
  415. + } else {
  416. + dev_err(&pdev->dev, "firmware failed to load (%d).", err);
  417. + }
  418. +
  419. + dev_info(&pdev->dev, "Unloading driver");
  420. +
  421. + if (parent)
  422. + device_lock(parent);
  423. +
  424. + device_release_driver(&pdev->dev);
  425. +
  426. + if (parent)
  427. + device_unlock(parent);
  428. +
  429. + pci_dev_put(pdev);
  430. +}
  431. +
  432. +static int renesas_fw_alive_check(struct pci_dev *pdev)
  433. +{
  434. + const struct renesas_fw_entry *entry;
  435. + int err;
  436. +
  437. + /* check if we have a eligible RENESAS' uPD720201/2 w/o FW. */
  438. + entry = renesas_needs_fw_dl(pdev);
  439. + if (!entry)
  440. + return 0;
  441. +
  442. + err = renesas_fw_check_running(pdev);
  443. + /* Also go ahead, if the firmware is running */
  444. + if (err == 0)
  445. + return 0;
  446. +
  447. + /* At this point, we can be sure that the FW isn't ready. */
  448. + return err;
  449. +}
  450. +
  451. +static int renesas_fw_download_to_hw(struct pci_dev *pdev,
  452. + const struct pci_device_id *id,
  453. + bool do_resume)
  454. +{
  455. + const struct renesas_fw_entry *entry;
  456. + struct renesas_fw_ctx *ctx;
  457. + int err;
  458. +
  459. + /* check if we have a eligible RENESAS' uPD720201/2 w/o FW. */
  460. + entry = renesas_needs_fw_dl(pdev);
  461. + if (!entry)
  462. + return 0;
  463. +
  464. + err = renesas_fw_check_running(pdev);
  465. + /* Continue ahead, if the firmware is already running. */
  466. + if (err == 0)
  467. + return 0;
  468. +
  469. + if (err != 1)
  470. + return err;
  471. +
  472. + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  473. + if (!ctx)
  474. + return -ENOMEM;
  475. + ctx->pdev = pdev;
  476. + ctx->resume = do_resume;
  477. + ctx->id = id;
  478. +
  479. + pci_dev_get(pdev);
  480. + err = request_firmware_nowait(THIS_MODULE, 1, entry->firmware_name,
  481. + &pdev->dev, GFP_KERNEL, ctx, renesas_fw_callback);
  482. + if (err) {
  483. + pci_dev_put(pdev);
  484. + return err;
  485. + }
  486. +
  487. + /*
  488. + * The renesas_fw_callback() callback will continue the probe
  489. + * process, once it aquires the firmware.
  490. + */
  491. + return 1;
  492. +}
  493. +
  494. /* called during probe() after chip reset completes */
  495. static int xhci_pci_setup(struct usb_hcd *hcd)
  496. {
  497. @@ -268,6 +722,22 @@ static int xhci_pci_probe(struct pci_dev
  498. struct hc_driver *driver;
  499. struct usb_hcd *hcd;
  500. + /*
  501. + * Check if this device is a RENESAS uPD720201/2 device.
  502. + * Otherwise, we can continue with xhci_pci_probe as usual.
  503. + */
  504. + retval = renesas_fw_download_to_hw(dev, id, false);
  505. + switch (retval) {
  506. + case 0:
  507. + break;
  508. +
  509. + case 1: /* let it load the firmware and recontinue the probe. */
  510. + return 0;
  511. +
  512. + default:
  513. + return retval;
  514. + };
  515. +
  516. driver = (struct hc_driver *)id->driver_data;
  517. /* Prevent runtime suspending between USB-2 and USB-3 initialization */
  518. @@ -325,6 +795,16 @@ static void xhci_pci_remove(struct pci_d
  519. {
  520. struct xhci_hcd *xhci;
  521. + if (renesas_fw_alive_check(dev)) {
  522. + /*
  523. + * bail out early, if this was a renesas device w/o FW.
  524. + * Else we might hit the NMI watchdog in xhci_handsake
  525. + * during xhci_reset as part of the driver's unloading.
  526. + * which we forced in the renesas_fw_callback().
  527. + */
  528. + return;
  529. + }
  530. +
  531. xhci = hcd_to_xhci(pci_get_drvdata(dev));
  532. xhci->xhc_state |= XHCI_STATE_REMOVING;
  533. if (xhci->shared_hcd) {