Skip to content

Commit 8bd5741

Browse files
chunkeeygregkh
authored andcommitted
usb: renesas-xhci: Add the renesas xhci driver
This add a new driver for renesas xhci which is basically a firmware loader for uPD720201 and uPD720202 w/o ROM. The xhci-pci driver will invoke this driver for loading/unloading on relevant devices. 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. [vkoul: fixed comments: used macros for timeout count and delay removed renesas_fw_alive_check cleaned renesas_fw_callback removed recursion for renesas_fw_download add register defines and field names move to a separate file make fw loader as sync probe so that we execute in probe and prevent race make xhci-pci-renesas a seprate module] Signed-off-by: Christian Lamparter <[email protected]> Signed-off-by: Bjorn Andersson <[email protected]> Signed-off-by: Vinod Koul <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ff4c65c commit 8bd5741

File tree

4 files changed

+403
-0
lines changed

4 files changed

+403
-0
lines changed

drivers/usb/host/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ config USB_XHCI_PCI
4242
depends on USB_PCI
4343
default y
4444

45+
config USB_XHCI_PCI_RENESAS
46+
tristate "Support for additional Renesas xHCI controller with firwmare"
47+
depends on USB_XHCI_PCI
48+
---help---
49+
Say 'Y' to enable the support for the Renesas xHCI controller with
50+
firwmare. Make sure you have the firwmare for the device and
51+
installed on your system for this device to work.
52+
If unsure, say 'N'.
53+
4554
config USB_XHCI_PLATFORM
4655
tristate "Generic xHCI driver for a platform device"
4756
select USB_XHCI_RCAR if ARCH_RENESAS

drivers/usb/host/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ obj-$(CONFIG_USB_UHCI_HCD) += uhci-hcd.o
7272
obj-$(CONFIG_USB_FHCI_HCD) += fhci.o
7373
obj-$(CONFIG_USB_XHCI_HCD) += xhci-hcd.o
7474
obj-$(CONFIG_USB_XHCI_PCI) += xhci-pci.o
75+
obj-$(CONFIG_USB_XHCI_PCI_RENESAS) += xhci-pci-renesas.o
7576
obj-$(CONFIG_USB_XHCI_PLATFORM) += xhci-plat-hcd.o
7677
obj-$(CONFIG_USB_XHCI_HISTB) += xhci-histb.o
7778
obj-$(CONFIG_USB_XHCI_MTK) += xhci-mtk.o

drivers/usb/host/xhci-pci-renesas.c

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2019-2020 Linaro Limited */
3+
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 <linux/unaligned/access_ok.h>
10+
11+
#include "xhci.h"
12+
#include "xhci-trace.h"
13+
#include "xhci-pci.h"
14+
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
23+
24+
#define RENESAS_FW_VERSION_FIELD GENMASK(23, 7)
25+
#define RENESAS_FW_VERSION_OFFSET 8
26+
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)
35+
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)
46+
47+
#define RENESAS_ROM_ERASE_MAGIC 0x5A65726F
48+
#define RENESAS_ROM_WRITE_MAGIC 0x53524F4D
49+
50+
#define RENESAS_RETRY 10000
51+
#define RENESAS_DELAY 10
52+
53+
static int renesas_fw_download_image(struct pci_dev *dev,
54+
const u32 *fw, size_t step)
55+
{
56+
size_t i;
57+
int err;
58+
u8 fw_status;
59+
bool data0_or_data1;
60+
61+
/*
62+
* The hardware does alternate between two 32-bit pages.
63+
* (This is because each row of the firmware is 8 bytes).
64+
*
65+
* for even steps we use DATA0, for odd steps DATA1.
66+
*/
67+
data0_or_data1 = (step & 1) == 1;
68+
69+
/* step+1. Read "Set DATAX" and confirm it is cleared. */
70+
for (i = 0; i < RENESAS_RETRY; i++) {
71+
err = pci_read_config_byte(dev, RENESAS_FW_STATUS_MSB,
72+
&fw_status);
73+
if (err) {
74+
dev_err(&dev->dev, "Read Status failed: %d\n",
75+
pcibios_err_to_errno(err));
76+
return pcibios_err_to_errno(err);
77+
}
78+
if (!(fw_status & BIT(data0_or_data1)))
79+
break;
80+
81+
udelay(RENESAS_DELAY);
82+
}
83+
if (i == RENESAS_RETRY) {
84+
dev_err(&dev->dev, "Timeout for Set DATAX step: %zd\n", step);
85+
return -ETIMEDOUT;
86+
}
87+
88+
/*
89+
* step+2. Write FW data to "DATAX".
90+
* "LSB is left" => force little endian
91+
*/
92+
err = pci_write_config_dword(dev, data0_or_data1 ?
93+
RENESAS_DATA1 : RENESAS_DATA0,
94+
(__force u32)cpu_to_le32(fw[step]));
95+
if (err) {
96+
dev_err(&dev->dev, "Write to DATAX failed: %d\n",
97+
pcibios_err_to_errno(err));
98+
return pcibios_err_to_errno(err);
99+
}
100+
101+
udelay(100);
102+
103+
/* step+3. Set "Set DATAX". */
104+
err = pci_write_config_byte(dev, RENESAS_FW_STATUS_MSB,
105+
BIT(data0_or_data1));
106+
if (err) {
107+
dev_err(&dev->dev, "Write config for DATAX failed: %d\n",
108+
pcibios_err_to_errno(err));
109+
return pcibios_err_to_errno(err);
110+
}
111+
112+
return 0;
113+
}
114+
115+
static int renesas_fw_verify(const void *fw_data,
116+
size_t length)
117+
{
118+
u16 fw_version_pointer;
119+
u16 fw_version;
120+
121+
/*
122+
* The Firmware's Data Format is describe in
123+
* "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
124+
*/
125+
126+
/*
127+
* The bootrom chips of the big brother have sizes up to 64k, let's
128+
* assume that's the biggest the firmware can get.
129+
*/
130+
if (length < 0x1000 || length >= 0x10000) {
131+
pr_err("firmware is size %zd is not (4k - 64k).",
132+
length);
133+
return -EINVAL;
134+
}
135+
136+
/* The First 2 bytes are fixed value (55aa). "LSB on Left" */
137+
if (get_unaligned_le16(fw_data) != 0x55aa) {
138+
pr_err("no valid firmware header found.");
139+
return -EINVAL;
140+
}
141+
142+
/* verify the firmware version position and print it. */
143+
fw_version_pointer = get_unaligned_le16(fw_data + 4);
144+
if (fw_version_pointer + 2 >= length) {
145+
pr_err("fw ver pointer is outside of the firmware image");
146+
return -EINVAL;
147+
}
148+
149+
fw_version = get_unaligned_le16(fw_data + fw_version_pointer);
150+
pr_err("got firmware version: %02x.", fw_version);
151+
152+
return 0;
153+
}
154+
155+
static int renesas_fw_check_running(struct pci_dev *pdev)
156+
{
157+
int err;
158+
u8 fw_state;
159+
160+
/*
161+
* Test if the device is actually needing the firmware. As most
162+
* BIOSes will initialize the device for us. If the device is
163+
* initialized.
164+
*/
165+
err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
166+
if (err)
167+
return pcibios_err_to_errno(err);
168+
169+
/*
170+
* Check if "FW Download Lock" is locked. If it is and the FW is
171+
* ready we can simply continue. If the FW is not ready, we have
172+
* to give up.
173+
*/
174+
if (fw_state & RENESAS_FW_STATUS_LOCK) {
175+
dev_dbg(&pdev->dev, "FW Download Lock is engaged.");
176+
177+
if (fw_state & RENESAS_FW_STATUS_SUCCESS)
178+
return 0;
179+
180+
dev_err(&pdev->dev,
181+
"FW Download Lock is set and FW is not ready. Giving Up.");
182+
return -EIO;
183+
}
184+
185+
/*
186+
* Check if "FW Download Enable" is set. If someone (us?) tampered
187+
* with it and it can't be reset, we have to give up too... and
188+
* ask for a forgiveness and a reboot.
189+
*/
190+
if (fw_state & RENESAS_FW_STATUS_DOWNLOAD_ENABLE) {
191+
dev_err(&pdev->dev,
192+
"FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
193+
return -EIO;
194+
}
195+
196+
/* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
197+
switch (fw_state & RENESAS_FW_STATUS_RESULT) {
198+
case 0: /* No result yet */
199+
dev_dbg(&pdev->dev, "FW is not ready/loaded yet.");
200+
201+
/* tell the caller, that this device needs the firmware. */
202+
return 1;
203+
204+
case RENESAS_FW_STATUS_SUCCESS: /* Success, device should be working. */
205+
dev_dbg(&pdev->dev, "FW is ready.");
206+
return 0;
207+
208+
case RENESAS_FW_STATUS_ERROR: /* Error State */
209+
dev_err(&pdev->dev,
210+
"hardware is in an error state. Giving up (poweroff/reboot needed).");
211+
return -ENODEV;
212+
213+
default: /* All other states are marked as "Reserved states" */
214+
dev_err(&pdev->dev,
215+
"hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
216+
(fw_state & RENESAS_FW_STATUS_RESULT) >> 4);
217+
return -EINVAL;
218+
}
219+
}
220+
221+
static int renesas_fw_download(struct pci_dev *pdev,
222+
const struct firmware *fw)
223+
{
224+
const u32 *fw_data = (const u32 *)fw->data;
225+
size_t i;
226+
int err;
227+
u8 fw_status;
228+
229+
/*
230+
* For more information and the big picture: please look at the
231+
* "Firmware Download Sequence" in "7.1 FW Download Interface"
232+
* of R19UH0078EJ0500 Rev.5.00 page 131
233+
*/
234+
235+
/*
236+
* 0. Set "FW Download Enable" bit in the
237+
* "FW Download Control & Status Register" at 0xF4
238+
*/
239+
err = pci_write_config_byte(pdev, RENESAS_FW_STATUS,
240+
RENESAS_FW_STATUS_DOWNLOAD_ENABLE);
241+
if (err)
242+
return pcibios_err_to_errno(err);
243+
244+
/* 1 - 10 follow one step after the other. */
245+
for (i = 0; i < fw->size / 4; i++) {
246+
err = renesas_fw_download_image(pdev, fw_data, i);
247+
if (err) {
248+
dev_err(&pdev->dev,
249+
"Firmware Download Step %zd failed at position %zd bytes with (%d).",
250+
i, i * 4, err);
251+
return err;
252+
}
253+
}
254+
255+
/*
256+
* This sequence continues until the last data is written to
257+
* "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
258+
* is cleared by the hardware beforehand.
259+
*/
260+
for (i = 0; i < RENESAS_RETRY; i++) {
261+
err = pci_read_config_byte(pdev, RENESAS_FW_STATUS_MSB,
262+
&fw_status);
263+
if (err)
264+
return pcibios_err_to_errno(err);
265+
if (!(fw_status & (BIT(0) | BIT(1))))
266+
break;
267+
268+
udelay(RENESAS_DELAY);
269+
}
270+
if (i == RENESAS_RETRY)
271+
dev_warn(&pdev->dev, "Final Firmware Download step timed out.");
272+
273+
/*
274+
* 11. After finishing writing the last data of FW, the
275+
* System Software must clear "FW Download Enable"
276+
*/
277+
err = pci_write_config_byte(pdev, RENESAS_FW_STATUS, 0);
278+
if (err)
279+
return pcibios_err_to_errno(err);
280+
281+
/* 12. Read "Result Code" and confirm it is good. */
282+
for (i = 0; i < RENESAS_RETRY; i++) {
283+
err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_status);
284+
if (err)
285+
return pcibios_err_to_errno(err);
286+
if (fw_status & RENESAS_FW_STATUS_SUCCESS)
287+
break;
288+
289+
udelay(RENESAS_DELAY);
290+
}
291+
if (i == RENESAS_RETRY) {
292+
/* Timed out / Error - let's see if we can fix this */
293+
err = renesas_fw_check_running(pdev);
294+
switch (err) {
295+
case 0: /*
296+
* we shouldn't end up here.
297+
* maybe it took a little bit longer.
298+
* But all should be well?
299+
*/
300+
break;
301+
302+
case 1: /* (No result yet! */
303+
dev_err(&pdev->dev, "FW Load timedout");
304+
return -ETIMEDOUT;
305+
306+
default:
307+
return err;
308+
}
309+
}
310+
311+
return 0;
312+
}
313+
314+
static int renesas_load_fw(struct pci_dev *pdev, const struct firmware *fw)
315+
{
316+
int err = 0;
317+
318+
err = renesas_fw_download(pdev, fw);
319+
if (err)
320+
dev_err(&pdev->dev, "firmware failed to download (%d).", err);
321+
return err;
322+
}
323+
324+
int renesas_xhci_check_request_fw(struct pci_dev *pdev,
325+
const struct pci_device_id *id)
326+
{
327+
struct xhci_driver_data *driver_data =
328+
(struct xhci_driver_data *)id->driver_data;
329+
const char *fw_name = driver_data->firmware;
330+
const struct firmware *fw;
331+
int err;
332+
333+
err = renesas_fw_check_running(pdev);
334+
/* Continue ahead, if the firmware is already running. */
335+
if (err == 0)
336+
return 0;
337+
338+
if (err != 1)
339+
return err;
340+
341+
pci_dev_get(pdev);
342+
err = request_firmware(&fw, fw_name, &pdev->dev);
343+
pci_dev_put(pdev);
344+
if (err) {
345+
dev_err(&pdev->dev, "request_firmware failed: %d\n", err);
346+
return err;
347+
}
348+
349+
err = renesas_fw_verify(fw->data, fw->size);
350+
if (err)
351+
goto exit;
352+
353+
err = renesas_load_fw(pdev, fw);
354+
exit:
355+
release_firmware(fw);
356+
return err;
357+
}
358+
EXPORT_SYMBOL_GPL(renesas_xhci_check_request_fw);
359+
360+
void renesas_xhci_pci_exit(struct pci_dev *dev)
361+
{
362+
}
363+
EXPORT_SYMBOL_GPL(renesas_xhci_pci_exit);
364+
365+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)