Re: [PATCH v4 1/4] usb: xhci: add firmware loader for uPD720201 and uPD720202 w/o ROM

2019-06-30 Thread Vinod Koul
On 29-06-19, 00:40, Mathias Nyman wrote:
> On 26.6.2019 10.55, Vinod Koul wrote:
> > From: Christian Lamparter 
> > 
> > This patch adds a firmware loader for the uPD720201K8-711-BAC-A
> > and uPD720202K8-711-BAA-A variant. Both of these chips are listed
> > in Renesas' R19UH0078EJ0500 Rev.5.00 "User's Manual: Hardware" as
> > devices which need the firmware loader on page 2 in order to
> > work as they "do not support the External ROM".
> > 
> > The "Firmware Download Sequence" is describe in chapter
> > "7.1 FW Download Interface" R19UH0078EJ0500 Rev.5.00 page 131.
> > 
> > The firmware "K2013080.mem" is available from a USB3.0 Host to
> > PCIe Adapter (PP2U-E card) "Firmware download" archive. An
> > alternative version can be sourced from Netgear's WNDR4700 GPL
> > archives.
> > 
> > The release notes of the PP2U-E's "Firmware Download" ver 2.0.1.3
> > (2012-06-15) state that the firmware is for the following devices:
> >   - uPD720201 ES 2.0 sample whose revision ID is 2.
> >   - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
> >   - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
> > 
> > Cc: Yoshihiro Shimoda 
> > Signed-off-by: Christian Lamparter 
> > Signed-off-by: Bjorn Andersson 
> > [vkoul: fixed comments:
> > used macros for timeout count and delay
> > removed renesas_fw_alive_check
> > cleaned renesas_fw_callback
> > removed recurion for renesas_fw_download
> > added MODULE_FIRMWARE]
> > Tested-by: Christian Lamparter 
> > Signed-off-by: Vinod Koul 
> > ---
> >   drivers/usb/host/xhci-pci.c | 454 
> >   1 file changed, 454 insertions(+)
> > 
> > diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
> > index c2fe218e051f..237df5c47fca 100644
> > --- a/drivers/usb/host/xhci-pci.c
> > +++ b/drivers/usb/host/xhci-pci.c
> > @@ -12,6 +12,8 @@
> >   #include 
> >   #include 
> >   #include 
> > +#include 
> > +#include 
> >   #include "xhci.h"
> >   #include "xhci-trace.h"
> > @@ -55,6 +57,9 @@
> >   #define PCI_DEVICE_ID_AMD_PROMONTORYA_1   0x43bc
> >   #define PCI_DEVICE_ID_ASMEDIA_1042A_XHCI  0x1142
> > +#define RENESAS_RETRY  1
> > +#define RENESAS_DELAY  10
> > +
> >   static const char hcd_name[] = "xhci_hcd";
> >   static struct hc_driver __read_mostly xhci_pci_hc_driver;
> > @@ -279,6 +284,429 @@ static void xhci_pme_acpi_rtd3_enable(struct pci_dev 
> > *dev)
> >   static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
> >   #endif /* CONFIG_ACPI */
> > +static const struct renesas_fw_entry {
> > +   const char *firmware_name;
> > +   u16 device;
> > +   u8 revision;
> > +   u16 expected_version;
> > +} renesas_fw_table[] = {
> > +   /*
> > +* Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
> > +* are listed in R19UH0078EJ0500 Rev.5.00 as devices which
> > +* need the software loader.
> > +*
> > +* PP2U/ReleaseNote_USB3-201-202-FW.txt:
> > +*
> > +* Note: This firmware is for the following devices.
> > +*  - uPD720201 ES 2.0 sample whose revision ID is 2.
> > +*  - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
> > +*  - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
> > +*/
> > +   { "K2013080.mem", 0x0014, 0x02, 0x2013 },
> > +   { "K2013080.mem", 0x0014, 0x03, 0x2013 },
> > +   { "K2013080.mem", 0x0015, 0x02, 0x2013 },
> > +};
> > +
> > +MODULE_FIRMWARE("K2013080.mem");
> > +
> > +static const struct renesas_fw_entry *renesas_needs_fw_dl(struct pci_dev 
> > *dev)
> > +{
> > +   const struct renesas_fw_entry *entry;
> > +   size_t i;
> > +
> > +   /* This loader will only work with a RENESAS device. */
> > +   if (!(dev->vendor == PCI_VENDOR_ID_RENESAS))
> > +   return NULL;
> > +
> > +   for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
> > +   entry = _fw_table[i];
> > +   if (entry->device == dev->device &&
> > +   entry->revision == dev->revision)
> > +   return entry;
> > +   }
> > +
> > +   return NULL;
> > +}
> > +
> > +static int renesas_fw_download_image(struct pci_dev *dev,
> > +const u32 *fw,
> > +size_t step)
> > +{
> > +   size_t i;
> > +   int err;
> > +   u8 fw_status;
> > +   bool data0_or_data1;
> > +
> > +   /*
> > +* The hardware does alternate between two 32-bit pages.
> > +* (This is because each row of the firmware is 8 bytes).
> > +*
> > +* for even steps we use DATA0, for odd steps DATA1.
> > +*/
> > +   data0_or_data1 = (step & 1) == 1;
> > +
> > +   /* step+1. Read "Set DATAX" and confirm it is cleared. */
> > +   for (i = 0; i < RENESAS_RETRY; i++) {
> > +   err = pci_read_config_byte(dev, 0xF5, _status);
> > +   if (err)
> > +   return pcibios_err_to_errno(err);
> > +   if (!(fw_status & BIT(data0_or_data1)))
> > +   break;
> > +
> > +   

Re: [PATCH v4 1/4] usb: xhci: add firmware loader for uPD720201 and uPD720202 w/o ROM

2019-06-28 Thread Mathias Nyman

On 26.6.2019 10.55, Vinod Koul wrote:

From: Christian Lamparter 

This patch adds a firmware loader for the uPD720201K8-711-BAC-A
and uPD720202K8-711-BAA-A variant. Both of these chips are listed
in Renesas' R19UH0078EJ0500 Rev.5.00 "User's Manual: Hardware" as
devices which need the firmware loader on page 2 in order to
work as they "do not support the External ROM".

The "Firmware Download Sequence" is describe in chapter
"7.1 FW Download Interface" R19UH0078EJ0500 Rev.5.00 page 131.

The firmware "K2013080.mem" is available from a USB3.0 Host to
PCIe Adapter (PP2U-E card) "Firmware download" archive. An
alternative version can be sourced from Netgear's WNDR4700 GPL
archives.

The release notes of the PP2U-E's "Firmware Download" ver 2.0.1.3
(2012-06-15) state that the firmware is for the following devices:
  - uPD720201 ES 2.0 sample whose revision ID is 2.
  - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
  - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.

Cc: Yoshihiro Shimoda 
Signed-off-by: Christian Lamparter 
Signed-off-by: Bjorn Andersson 
[vkoul: fixed comments:
used macros for timeout count and delay
removed renesas_fw_alive_check
cleaned renesas_fw_callback
removed recurion for renesas_fw_download
added MODULE_FIRMWARE]
Tested-by: Christian Lamparter 
Signed-off-by: Vinod Koul 
---
  drivers/usb/host/xhci-pci.c | 454 
  1 file changed, 454 insertions(+)

diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index c2fe218e051f..237df5c47fca 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -12,6 +12,8 @@
  #include 
  #include 
  #include 
+#include 
+#include 
  
  #include "xhci.h"

  #include "xhci-trace.h"
@@ -55,6 +57,9 @@
  #define PCI_DEVICE_ID_AMD_PROMONTORYA_1   0x43bc
  #define PCI_DEVICE_ID_ASMEDIA_1042A_XHCI  0x1142
  
+#define RENESAS_RETRY	1

+#define RENESAS_DELAY  10
+
  static const char hcd_name[] = "xhci_hcd";
  
  static struct hc_driver __read_mostly xhci_pci_hc_driver;

@@ -279,6 +284,429 @@ static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev)
  static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
  #endif /* CONFIG_ACPI */
  
+static const struct renesas_fw_entry {

+   const char *firmware_name;
+   u16 device;
+   u8 revision;
+   u16 expected_version;
+} renesas_fw_table[] = {
+   /*
+* Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
+* are listed in R19UH0078EJ0500 Rev.5.00 as devices which
+* need the software loader.
+*
+* PP2U/ReleaseNote_USB3-201-202-FW.txt:
+*
+* Note: This firmware is for the following devices.
+*  - uPD720201 ES 2.0 sample whose revision ID is 2.
+*  - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
+*  - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
+*/
+   { "K2013080.mem", 0x0014, 0x02, 0x2013 },
+   { "K2013080.mem", 0x0014, 0x03, 0x2013 },
+   { "K2013080.mem", 0x0015, 0x02, 0x2013 },
+};
+
+MODULE_FIRMWARE("K2013080.mem");
+
+static const struct renesas_fw_entry *renesas_needs_fw_dl(struct pci_dev *dev)
+{
+   const struct renesas_fw_entry *entry;
+   size_t i;
+
+   /* This loader will only work with a RENESAS device. */
+   if (!(dev->vendor == PCI_VENDOR_ID_RENESAS))
+   return NULL;
+
+   for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
+   entry = _fw_table[i];
+   if (entry->device == dev->device &&
+   entry->revision == dev->revision)
+   return entry;
+   }
+
+   return NULL;
+}
+
+static int renesas_fw_download_image(struct pci_dev *dev,
+const u32 *fw,
+size_t step)
+{
+   size_t i;
+   int err;
+   u8 fw_status;
+   bool data0_or_data1;
+
+   /*
+* The hardware does alternate between two 32-bit pages.
+* (This is because each row of the firmware is 8 bytes).
+*
+* for even steps we use DATA0, for odd steps DATA1.
+*/
+   data0_or_data1 = (step & 1) == 1;
+
+   /* step+1. Read "Set DATAX" and confirm it is cleared. */
+   for (i = 0; i < RENESAS_RETRY; i++) {
+   err = pci_read_config_byte(dev, 0xF5, _status);
+   if (err)
+   return pcibios_err_to_errno(err);
+   if (!(fw_status & BIT(data0_or_data1)))
+   break;
+
+   udelay(RENESAS_DELAY);
+   }
+   if (i == RENESAS_RETRY)
+   return -ETIMEDOUT;
+
+   /*
+* step+2. Write FW data to "DATAX".
+* "LSB is left" => force little endian
+*/
+   err = pci_write_config_dword(dev, data0_or_data1 ? 0xFC : 0xF8,
+

[PATCH v4 1/4] usb: xhci: add firmware loader for uPD720201 and uPD720202 w/o ROM

2019-06-26 Thread Vinod Koul
From: Christian Lamparter 

This patch adds a firmware loader for the uPD720201K8-711-BAC-A
and uPD720202K8-711-BAA-A variant. Both of these chips are listed
in Renesas' R19UH0078EJ0500 Rev.5.00 "User's Manual: Hardware" as
devices which need the firmware loader on page 2 in order to
work as they "do not support the External ROM".

The "Firmware Download Sequence" is describe in chapter
"7.1 FW Download Interface" R19UH0078EJ0500 Rev.5.00 page 131.

The firmware "K2013080.mem" is available from a USB3.0 Host to
PCIe Adapter (PP2U-E card) "Firmware download" archive. An
alternative version can be sourced from Netgear's WNDR4700 GPL
archives.

The release notes of the PP2U-E's "Firmware Download" ver 2.0.1.3
(2012-06-15) state that the firmware is for the following devices:
 - uPD720201 ES 2.0 sample whose revision ID is 2.
 - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
 - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.

Cc: Yoshihiro Shimoda 
Signed-off-by: Christian Lamparter 
Signed-off-by: Bjorn Andersson 
[vkoul: fixed comments:
used macros for timeout count and delay
removed renesas_fw_alive_check
cleaned renesas_fw_callback
removed recurion for renesas_fw_download
added MODULE_FIRMWARE]
Tested-by: Christian Lamparter 
Signed-off-by: Vinod Koul 
---
 drivers/usb/host/xhci-pci.c | 454 
 1 file changed, 454 insertions(+)

diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index c2fe218e051f..237df5c47fca 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -12,6 +12,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "xhci.h"
 #include "xhci-trace.h"
@@ -55,6 +57,9 @@
 #define PCI_DEVICE_ID_AMD_PROMONTORYA_10x43bc
 #define PCI_DEVICE_ID_ASMEDIA_1042A_XHCI   0x1142
 
+#define RENESAS_RETRY  1
+#define RENESAS_DELAY  10
+
 static const char hcd_name[] = "xhci_hcd";
 
 static struct hc_driver __read_mostly xhci_pci_hc_driver;
@@ -279,6 +284,429 @@ static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev)
 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
 #endif /* CONFIG_ACPI */
 
+static const struct renesas_fw_entry {
+   const char *firmware_name;
+   u16 device;
+   u8 revision;
+   u16 expected_version;
+} renesas_fw_table[] = {
+   /*
+* Only the uPD720201K8-711-BAC-A or uPD720202K8-711-BAA-A
+* are listed in R19UH0078EJ0500 Rev.5.00 as devices which
+* need the software loader.
+*
+* PP2U/ReleaseNote_USB3-201-202-FW.txt:
+*
+* Note: This firmware is for the following devices.
+*  - uPD720201 ES 2.0 sample whose revision ID is 2.
+*  - uPD720201 ES 2.1 sample & CS sample & Mass product, ID is 3.
+*  - uPD720202 ES 2.0 sample & CS sample & Mass product, ID is 2.
+*/
+   { "K2013080.mem", 0x0014, 0x02, 0x2013 },
+   { "K2013080.mem", 0x0014, 0x03, 0x2013 },
+   { "K2013080.mem", 0x0015, 0x02, 0x2013 },
+};
+
+MODULE_FIRMWARE("K2013080.mem");
+
+static const struct renesas_fw_entry *renesas_needs_fw_dl(struct pci_dev *dev)
+{
+   const struct renesas_fw_entry *entry;
+   size_t i;
+
+   /* This loader will only work with a RENESAS device. */
+   if (!(dev->vendor == PCI_VENDOR_ID_RENESAS))
+   return NULL;
+
+   for (i = 0; i < ARRAY_SIZE(renesas_fw_table); i++) {
+   entry = _fw_table[i];
+   if (entry->device == dev->device &&
+   entry->revision == dev->revision)
+   return entry;
+   }
+
+   return NULL;
+}
+
+static int renesas_fw_download_image(struct pci_dev *dev,
+const u32 *fw,
+size_t step)
+{
+   size_t i;
+   int err;
+   u8 fw_status;
+   bool data0_or_data1;
+
+   /*
+* The hardware does alternate between two 32-bit pages.
+* (This is because each row of the firmware is 8 bytes).
+*
+* for even steps we use DATA0, for odd steps DATA1.
+*/
+   data0_or_data1 = (step & 1) == 1;
+
+   /* step+1. Read "Set DATAX" and confirm it is cleared. */
+   for (i = 0; i < RENESAS_RETRY; i++) {
+   err = pci_read_config_byte(dev, 0xF5, _status);
+   if (err)
+   return pcibios_err_to_errno(err);
+   if (!(fw_status & BIT(data0_or_data1)))
+   break;
+
+   udelay(RENESAS_DELAY);
+   }
+   if (i == RENESAS_RETRY)
+   return -ETIMEDOUT;
+
+   /*
+* step+2. Write FW data to "DATAX".
+* "LSB is left" => force little endian
+*/
+   err = pci_write_config_dword(dev, data0_or_data1 ? 0xFC : 0xF8,
+(__force u32)cpu_to_le32(fw[step]));
+   if (err)
+