blob: 794389e3099a53c6e85019f14aa7a35af7f94775 [file] [log] [blame]
Patrick Williams8dd68482022-10-04 07:57:18 -05001From 83ba88292211394ce6b3a21fbc0f702dae543290 Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Rui Miguel Silva <rui.silva@linaro.org>
3Date: Mon, 28 Jun 2021 23:31:25 +0100
Patrick Williams8dd68482022-10-04 07:57:18 -05004Subject: [PATCH 04/26] usb: add isp1760 family driver
Brad Bishopbec4ebc2022-08-03 09:55:16 -04005
6ISP1760/61/63 are a family of usb controllers, blah, blah, more info
7here.
8
9Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Patrick Williams8dd68482022-10-04 07:57:18 -050010Upstream-Status: Accepted [2022.10-rc1]
Brad Bishopbec4ebc2022-08-03 09:55:16 -040011---
12 Makefile | 1 +
13 drivers/usb/Kconfig | 2 +
14 drivers/usb/common/Makefile | 1 +
15 drivers/usb/isp1760/Kconfig | 12 +
16 drivers/usb/isp1760/Makefile | 6 +
17 drivers/usb/isp1760/isp1760-core.c | 378 ++++
18 drivers/usb/isp1760/isp1760-core.h | 96 +
19 drivers/usb/isp1760/isp1760-hcd.c | 2574 +++++++++++++++++++++++++++
20 drivers/usb/isp1760/isp1760-hcd.h | 82 +
21 drivers/usb/isp1760/isp1760-if.c | 127 ++
22 drivers/usb/isp1760/isp1760-regs.h | 292 +++
23 drivers/usb/isp1760/isp1760-uboot.c | 76 +
24 drivers/usb/isp1760/isp1760-uboot.h | 27 +
25 13 files changed, 3674 insertions(+)
26 create mode 100644 drivers/usb/isp1760/Kconfig
27 create mode 100644 drivers/usb/isp1760/Makefile
28 create mode 100644 drivers/usb/isp1760/isp1760-core.c
29 create mode 100644 drivers/usb/isp1760/isp1760-core.h
30 create mode 100644 drivers/usb/isp1760/isp1760-hcd.c
31 create mode 100644 drivers/usb/isp1760/isp1760-hcd.h
32 create mode 100644 drivers/usb/isp1760/isp1760-if.c
33 create mode 100644 drivers/usb/isp1760/isp1760-regs.h
34 create mode 100644 drivers/usb/isp1760/isp1760-uboot.c
35 create mode 100644 drivers/usb/isp1760/isp1760-uboot.h
36
37diff --git a/Makefile b/Makefile
Patrick Williams8dd68482022-10-04 07:57:18 -050038index 98867fbe06..67851020f5 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040039--- a/Makefile
40+++ b/Makefile
Patrick Williams92b42cb2022-09-03 06:53:57 -050041@@ -841,6 +841,7 @@ libs-y += drivers/usb/host/
Brad Bishopbec4ebc2022-08-03 09:55:16 -040042 libs-y += drivers/usb/mtu3/
43 libs-y += drivers/usb/musb/
44 libs-y += drivers/usb/musb-new/
45+libs-y += drivers/usb/isp1760/
46 libs-y += drivers/usb/phy/
47 libs-y += drivers/usb/ulpi/
48 ifdef CONFIG_POST
49diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
Patrick Williams8dd68482022-10-04 07:57:18 -050050index ab1d061bd0..bbe07be02c 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040051--- a/drivers/usb/Kconfig
52+++ b/drivers/usb/Kconfig
53@@ -78,6 +78,8 @@ source "drivers/usb/musb/Kconfig"
54
55 source "drivers/usb/musb-new/Kconfig"
56
57+source "drivers/usb/isp1760/Kconfig"
58+
59 source "drivers/usb/emul/Kconfig"
60
61 source "drivers/usb/phy/Kconfig"
62diff --git a/drivers/usb/common/Makefile b/drivers/usb/common/Makefile
Patrick Williams8dd68482022-10-04 07:57:18 -050063index dc05cb0a50..f08b064d24 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040064--- a/drivers/usb/common/Makefile
65+++ b/drivers/usb/common/Makefile
66@@ -4,6 +4,7 @@
67 #
68
69 obj-$(CONFIG_$(SPL_)DM_USB) += common.o
70+obj-$(CONFIG_USB_ISP1760) += usb_urb.o
71 obj-$(CONFIG_USB_MUSB_HCD) += usb_urb.o
72 obj-$(CONFIG_USB_MUSB_UDC) += usb_urb.o
73 obj-$(CONFIG_USB_EHCI_FSL) += fsl-dt-fixup.o fsl-errata.o
74diff --git a/drivers/usb/isp1760/Kconfig b/drivers/usb/isp1760/Kconfig
75new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -050076index 0000000000..993d71e74c
Brad Bishopbec4ebc2022-08-03 09:55:16 -040077--- /dev/null
78+++ b/drivers/usb/isp1760/Kconfig
79@@ -0,0 +1,12 @@
80+# SPDX-License-Identifier: GPL-2.0
81+
82+config USB_ISP1760
83+ tristate "NXP ISP 1760/1761/1763 support"
84+ select DM_USB
85+ select USB_HOST
86+ help
87+ Say Y or M here if your system as an ISP1760/1761/1763 USB host
88+ controller.
89+
90+ This USB controller is usually attached to a non-DMA-Master
91+ capable bus.
92diff --git a/drivers/usb/isp1760/Makefile b/drivers/usb/isp1760/Makefile
93new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -050094index 0000000000..2c809c01b1
Brad Bishopbec4ebc2022-08-03 09:55:16 -040095--- /dev/null
96+++ b/drivers/usb/isp1760/Makefile
97@@ -0,0 +1,6 @@
98+# SPDX-License-Identifier: GPL-2.0
99+isp1760-y := isp1760-core.o isp1760-if.o isp1760-uboot.o isp1760-hcd.o
100+
101+#isp1760-hcd.o
102+
103+obj-$(CONFIG_USB_ISP1760) += isp1760.o
104diff --git a/drivers/usb/isp1760/isp1760-core.c b/drivers/usb/isp1760/isp1760-core.c
105new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500106index 0000000000..3080595549
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400107--- /dev/null
108+++ b/drivers/usb/isp1760/isp1760-core.c
109@@ -0,0 +1,378 @@
110+// SPDX-License-Identifier: GPL-2.0
111+/*
112+ * Driver for the NXP ISP1760 chip
113+ *
114+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
115+ *
116+ * This is based on linux kernel driver, original developed:
117+ * Copyright 2014 Laurent Pinchart
118+ * Copyright 2007 Sebastian Siewior
119+ *
120+ */
121+
122+#include <dm.h>
123+#include <dm/device-internal.h>
124+#include <dm/device_compat.h>
125+#include <dm/devres.h>
126+#include <linux/compat.h>
127+#include <linux/delay.h>
128+#include <linux/io.h>
129+#include <linux/kernel.h>
130+#include <regmap.h>
131+#include <usb.h>
132+
133+#include "isp1760-core.h"
134+#include "isp1760-hcd.h"
135+#include "isp1760-regs.h"
136+
137+#define msleep(a) udelay(a * 1000)
138+
139+static int isp1760_init_core(struct isp1760_device *isp)
140+{
141+ struct isp1760_hcd *hcd = &isp->hcd;
142+
143+ /*
144+ * Reset the host controller, including the CPU interface
145+ * configuration.
146+ */
147+ isp1760_field_set(hcd->fields, SW_RESET_RESET_ALL);
148+ msleep(100);
149+
150+ /* Setup HW Mode Control: This assumes a level active-low interrupt */
151+ if ((isp->devflags & ISP1760_FLAG_ANALOG_OC) && hcd->is_isp1763)
152+ return -EINVAL;
153+
154+ if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
155+ isp1760_field_clear(hcd->fields, HW_DATA_BUS_WIDTH);
156+ if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_8)
157+ isp1760_field_set(hcd->fields, HW_DATA_BUS_WIDTH);
158+ if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
159+ isp1760_field_set(hcd->fields, HW_ANA_DIGI_OC);
160+ if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
161+ isp1760_field_set(hcd->fields, HW_DACK_POL_HIGH);
162+ if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
163+ isp1760_field_set(hcd->fields, HW_DREQ_POL_HIGH);
164+ if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
165+ isp1760_field_set(hcd->fields, HW_INTR_HIGH_ACT);
166+ if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
167+ isp1760_field_set(hcd->fields, HW_INTR_EDGE_TRIG);
168+
169+ /*
170+ * The ISP1761 has a dedicated DC IRQ line but supports sharing the HC
171+ * IRQ line for both the host and device controllers. Hardcode IRQ
172+ * sharing for now and disable the DC interrupts globally to avoid
173+ * spurious interrupts during HCD registration.
174+ */
175+ if (isp->devflags & ISP1760_FLAG_ISP1761) {
176+ isp1760_reg_write(hcd->regs, ISP176x_DC_MODE, 0);
177+ isp1760_field_set(hcd->fields, HW_COMN_IRQ);
178+ }
179+
180+ /*
181+ * PORT 1 Control register of the ISP1760 is the OTG control register
182+ * on ISP1761.
183+ *
184+ * TODO: Really support OTG. For now we configure port 1 in device mode
185+ */
186+ if (((isp->devflags & ISP1760_FLAG_ISP1761) ||
187+ (isp->devflags & ISP1760_FLAG_ISP1763)) &&
188+ (isp->devflags & ISP1760_FLAG_PERIPHERAL_EN)) {
189+ isp1760_field_set(hcd->fields, HW_DM_PULLDOWN);
190+ isp1760_field_set(hcd->fields, HW_DP_PULLDOWN);
191+ isp1760_field_set(hcd->fields, HW_OTG_DISABLE);
192+ } else {
193+ isp1760_field_set(hcd->fields, HW_SW_SEL_HC_DC);
194+ isp1760_field_set(hcd->fields, HW_VBUS_DRV);
195+ isp1760_field_set(hcd->fields, HW_SEL_CP_EXT);
196+ }
197+
198+ printf( "%s bus width: %u, oc: %s\n",
199+ hcd->is_isp1763 ? "isp1763" : "isp1760",
200+ isp->devflags & ISP1760_FLAG_BUS_WIDTH_8 ? 8 :
201+ isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
202+ hcd->is_isp1763 ? "not available" :
203+ isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
204+
205+ return 0;
206+}
207+
208+void isp1760_set_pullup(struct isp1760_device *isp, bool enable)
209+{
210+ struct isp1760_hcd *hcd = &isp->hcd;
211+
212+ if (enable)
213+ isp1760_field_set(hcd->fields, HW_DP_PULLUP);
214+ else
215+ isp1760_field_set(hcd->fields, HW_DP_PULLUP_CLEAR);
216+}
217+
218+/*
219+ * ISP1760/61:
220+ *
221+ * 60kb divided in:
222+ * - 32 blocks @ 256 bytes
223+ * - 20 blocks @ 1024 bytes
224+ * - 4 blocks @ 8192 bytes
225+ */
226+static const struct isp1760_memory_layout isp176x_memory_conf = {
227+ .blocks[0] = 32,
228+ .blocks_size[0] = 256,
229+ .blocks[1] = 20,
230+ .blocks_size[1] = 1024,
231+ .blocks[2] = 4,
232+ .blocks_size[2] = 8192,
233+
234+ .slot_num = 32,
235+ .payload_blocks = 32 + 20 + 4,
236+ .payload_area_size = 0xf000,
237+};
238+
239+/*
240+ * ISP1763:
241+ *
242+ * 20kb divided in:
243+ * - 8 blocks @ 256 bytes
244+ * - 2 blocks @ 1024 bytes
245+ * - 4 blocks @ 4096 bytes
246+ */
247+static const struct isp1760_memory_layout isp1763_memory_conf = {
248+ .blocks[0] = 8,
249+ .blocks_size[0] = 256,
250+ .blocks[1] = 2,
251+ .blocks_size[1] = 1024,
252+ .blocks[2] = 4,
253+ .blocks_size[2] = 4096,
254+
255+ .slot_num = 16,
256+ .payload_blocks = 8 + 2 + 4,
257+ .payload_area_size = 0x5000,
258+};
259+
260+static const struct regmap_config isp1760_hc_regmap_conf = {
261+ .width = REGMAP_SIZE_16,
262+};
263+
264+static const struct reg_field isp1760_hc_reg_fields[] = {
265+ [HCS_PPC] = REG_FIELD(ISP176x_HC_HCSPARAMS, 4, 4),
266+ [HCS_N_PORTS] = REG_FIELD(ISP176x_HC_HCSPARAMS, 0, 3),
267+ [HCC_ISOC_CACHE] = REG_FIELD(ISP176x_HC_HCCPARAMS, 7, 7),
268+ [HCC_ISOC_THRES] = REG_FIELD(ISP176x_HC_HCCPARAMS, 4, 6),
269+ [CMD_LRESET] = REG_FIELD(ISP176x_HC_USBCMD, 7, 7),
270+ [CMD_RESET] = REG_FIELD(ISP176x_HC_USBCMD, 1, 1),
271+ [CMD_RUN] = REG_FIELD(ISP176x_HC_USBCMD, 0, 0),
272+ [STS_PCD] = REG_FIELD(ISP176x_HC_USBSTS, 2, 2),
273+ [HC_FRINDEX] = REG_FIELD(ISP176x_HC_FRINDEX, 0, 13),
274+ [FLAG_CF] = REG_FIELD(ISP176x_HC_CONFIGFLAG, 0, 0),
275+ [HC_ISO_PTD_DONEMAP] = REG_FIELD(ISP176x_HC_ISO_PTD_DONEMAP, 0, 31),
276+ [HC_ISO_PTD_SKIPMAP] = REG_FIELD(ISP176x_HC_ISO_PTD_SKIPMAP, 0, 31),
277+ [HC_ISO_PTD_LASTPTD] = REG_FIELD(ISP176x_HC_ISO_PTD_LASTPTD, 0, 31),
278+ [HC_INT_PTD_DONEMAP] = REG_FIELD(ISP176x_HC_INT_PTD_DONEMAP, 0, 31),
279+ [HC_INT_PTD_SKIPMAP] = REG_FIELD(ISP176x_HC_INT_PTD_SKIPMAP, 0, 31),
280+ [HC_INT_PTD_LASTPTD] = REG_FIELD(ISP176x_HC_INT_PTD_LASTPTD, 0, 31),
281+ [HC_ATL_PTD_DONEMAP] = REG_FIELD(ISP176x_HC_ATL_PTD_DONEMAP, 0, 31),
282+ [HC_ATL_PTD_SKIPMAP] = REG_FIELD(ISP176x_HC_ATL_PTD_SKIPMAP, 0, 31),
283+ [HC_ATL_PTD_LASTPTD] = REG_FIELD(ISP176x_HC_ATL_PTD_LASTPTD, 0, 31),
284+ [PORT_OWNER] = REG_FIELD(ISP176x_HC_PORTSC1, 13, 13),
285+ [PORT_POWER] = REG_FIELD(ISP176x_HC_PORTSC1, 12, 12),
286+ [PORT_LSTATUS] = REG_FIELD(ISP176x_HC_PORTSC1, 10, 11),
287+ [PORT_RESET] = REG_FIELD(ISP176x_HC_PORTSC1, 8, 8),
288+ [PORT_SUSPEND] = REG_FIELD(ISP176x_HC_PORTSC1, 7, 7),
289+ [PORT_RESUME] = REG_FIELD(ISP176x_HC_PORTSC1, 6, 6),
290+ [PORT_PE] = REG_FIELD(ISP176x_HC_PORTSC1, 2, 2),
291+ [PORT_CSC] = REG_FIELD(ISP176x_HC_PORTSC1, 1, 1),
292+ [PORT_CONNECT] = REG_FIELD(ISP176x_HC_PORTSC1, 0, 0),
293+ [ALL_ATX_RESET] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 31, 31),
294+ [HW_ANA_DIGI_OC] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 15, 15),
295+ [HW_COMN_IRQ] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 10, 10),
296+ [HW_DATA_BUS_WIDTH] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 8, 8),
297+ [HW_DACK_POL_HIGH] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 6, 6),
298+ [HW_DREQ_POL_HIGH] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 5, 5),
299+ [HW_INTR_HIGH_ACT] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 2, 2),
300+ [HW_INTR_EDGE_TRIG] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 1, 1),
301+ [HW_GLOBAL_INTR_EN] = REG_FIELD(ISP176x_HC_HW_MODE_CTRL, 0, 0),
302+ [HC_CHIP_REV] = REG_FIELD(ISP176x_HC_CHIP_ID, 16, 31),
303+ [HC_CHIP_ID_HIGH] = REG_FIELD(ISP176x_HC_CHIP_ID, 8, 15),
304+ [HC_CHIP_ID_LOW] = REG_FIELD(ISP176x_HC_CHIP_ID, 0, 7),
305+ [HC_SCRATCH] = REG_FIELD(ISP176x_HC_SCRATCH, 0, 31),
306+ [SW_RESET_RESET_ALL] = REG_FIELD(ISP176x_HC_RESET, 0, 0),
307+ [ISO_BUF_FILL] = REG_FIELD(ISP176x_HC_BUFFER_STATUS, 2, 2),
308+ [INT_BUF_FILL] = REG_FIELD(ISP176x_HC_BUFFER_STATUS, 1, 1),
309+ [ATL_BUF_FILL] = REG_FIELD(ISP176x_HC_BUFFER_STATUS, 0, 0),
310+ [MEM_BANK_SEL] = REG_FIELD(ISP176x_HC_MEMORY, 16, 17),
311+ [MEM_START_ADDR] = REG_FIELD(ISP176x_HC_MEMORY, 0, 15),
312+ [HC_INTERRUPT] = REG_FIELD(ISP176x_HC_INTERRUPT, 0, 9),
313+ [HC_ATL_IRQ_ENABLE] = REG_FIELD(ISP176x_HC_INTERRUPT_ENABLE, 8, 8),
314+ [HC_INT_IRQ_ENABLE] = REG_FIELD(ISP176x_HC_INTERRUPT_ENABLE, 7, 7),
315+ [HC_ISO_IRQ_MASK_OR] = REG_FIELD(ISP176x_HC_ISO_IRQ_MASK_OR, 0, 31),
316+ [HC_INT_IRQ_MASK_OR] = REG_FIELD(ISP176x_HC_INT_IRQ_MASK_OR, 0, 31),
317+ [HC_ATL_IRQ_MASK_OR] = REG_FIELD(ISP176x_HC_ATL_IRQ_MASK_OR, 0, 31),
318+ [HC_ISO_IRQ_MASK_AND] = REG_FIELD(ISP176x_HC_ISO_IRQ_MASK_AND, 0, 31),
319+ [HC_INT_IRQ_MASK_AND] = REG_FIELD(ISP176x_HC_INT_IRQ_MASK_AND, 0, 31),
320+ [HC_ATL_IRQ_MASK_AND] = REG_FIELD(ISP176x_HC_ATL_IRQ_MASK_AND, 0, 31),
321+ [HW_OTG_DISABLE] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 10, 10),
322+ [HW_SW_SEL_HC_DC] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 7, 7),
323+ [HW_VBUS_DRV] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 4, 4),
324+ [HW_SEL_CP_EXT] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 3, 3),
325+ [HW_DM_PULLDOWN] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 2, 2),
326+ [HW_DP_PULLDOWN] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 1, 1),
327+ [HW_DP_PULLUP] = REG_FIELD(ISP176x_HC_OTG_CTRL_SET, 0, 0),
328+ [HW_OTG_DISABLE_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 10, 10),
329+ [HW_SW_SEL_HC_DC_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 7, 7),
330+ [HW_VBUS_DRV_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 4, 4),
331+ [HW_SEL_CP_EXT_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 3, 3),
332+ [HW_DM_PULLDOWN_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 2, 2),
333+ [HW_DP_PULLDOWN_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 1, 1),
334+ [HW_DP_PULLUP_CLEAR] = REG_FIELD(ISP176x_HC_OTG_CTRL_CLEAR, 0, 0),
335+};
336+
337+static const struct regmap_config isp1763_hc_regmap_conf = {
338+ .width = REGMAP_SIZE_16,
339+};
340+
341+static const struct reg_field isp1763_hc_reg_fields[] = {
342+ [CMD_LRESET] = REG_FIELD(ISP1763_HC_USBCMD, 7, 7),
343+ [CMD_RESET] = REG_FIELD(ISP1763_HC_USBCMD, 1, 1),
344+ [CMD_RUN] = REG_FIELD(ISP1763_HC_USBCMD, 0, 0),
345+ [STS_PCD] = REG_FIELD(ISP1763_HC_USBSTS, 2, 2),
346+ [HC_FRINDEX] = REG_FIELD(ISP1763_HC_FRINDEX, 0, 13),
347+ [FLAG_CF] = REG_FIELD(ISP1763_HC_CONFIGFLAG, 0, 0),
348+ [HC_ISO_PTD_DONEMAP] = REG_FIELD(ISP1763_HC_ISO_PTD_DONEMAP, 0, 15),
349+ [HC_ISO_PTD_SKIPMAP] = REG_FIELD(ISP1763_HC_ISO_PTD_SKIPMAP, 0, 15),
350+ [HC_ISO_PTD_LASTPTD] = REG_FIELD(ISP1763_HC_ISO_PTD_LASTPTD, 0, 15),
351+ [HC_INT_PTD_DONEMAP] = REG_FIELD(ISP1763_HC_INT_PTD_DONEMAP, 0, 15),
352+ [HC_INT_PTD_SKIPMAP] = REG_FIELD(ISP1763_HC_INT_PTD_SKIPMAP, 0, 15),
353+ [HC_INT_PTD_LASTPTD] = REG_FIELD(ISP1763_HC_INT_PTD_LASTPTD, 0, 15),
354+ [HC_ATL_PTD_DONEMAP] = REG_FIELD(ISP1763_HC_ATL_PTD_DONEMAP, 0, 15),
355+ [HC_ATL_PTD_SKIPMAP] = REG_FIELD(ISP1763_HC_ATL_PTD_SKIPMAP, 0, 15),
356+ [HC_ATL_PTD_LASTPTD] = REG_FIELD(ISP1763_HC_ATL_PTD_LASTPTD, 0, 15),
357+ [PORT_OWNER] = REG_FIELD(ISP1763_HC_PORTSC1, 13, 13),
358+ [PORT_POWER] = REG_FIELD(ISP1763_HC_PORTSC1, 12, 12),
359+ [PORT_LSTATUS] = REG_FIELD(ISP1763_HC_PORTSC1, 10, 11),
360+ [PORT_RESET] = REG_FIELD(ISP1763_HC_PORTSC1, 8, 8),
361+ [PORT_SUSPEND] = REG_FIELD(ISP1763_HC_PORTSC1, 7, 7),
362+ [PORT_RESUME] = REG_FIELD(ISP1763_HC_PORTSC1, 6, 6),
363+ [PORT_PE] = REG_FIELD(ISP1763_HC_PORTSC1, 2, 2),
364+ [PORT_CSC] = REG_FIELD(ISP1763_HC_PORTSC1, 1, 1),
365+ [PORT_CONNECT] = REG_FIELD(ISP1763_HC_PORTSC1, 0, 0),
366+ [HW_DATA_BUS_WIDTH] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 4, 4),
367+ [HW_DACK_POL_HIGH] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 6, 6),
368+ [HW_DREQ_POL_HIGH] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 5, 5),
369+ [HW_INTF_LOCK] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 3, 3),
370+ [HW_INTR_HIGH_ACT] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 2, 2),
371+ [HW_INTR_EDGE_TRIG] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 1, 1),
372+ [HW_GLOBAL_INTR_EN] = REG_FIELD(ISP1763_HC_HW_MODE_CTRL, 0, 0),
373+ [SW_RESET_RESET_ATX] = REG_FIELD(ISP1763_HC_RESET, 3, 3),
374+ [SW_RESET_RESET_ALL] = REG_FIELD(ISP1763_HC_RESET, 0, 0),
375+ [HC_CHIP_ID_HIGH] = REG_FIELD(ISP1763_HC_CHIP_ID, 0, 15),
376+ [HC_CHIP_ID_LOW] = REG_FIELD(ISP1763_HC_CHIP_REV, 8, 15),
377+ [HC_CHIP_REV] = REG_FIELD(ISP1763_HC_CHIP_REV, 0, 7),
378+ [HC_SCRATCH] = REG_FIELD(ISP1763_HC_SCRATCH, 0, 15),
379+ [ISO_BUF_FILL] = REG_FIELD(ISP1763_HC_BUFFER_STATUS, 2, 2),
380+ [INT_BUF_FILL] = REG_FIELD(ISP1763_HC_BUFFER_STATUS, 1, 1),
381+ [ATL_BUF_FILL] = REG_FIELD(ISP1763_HC_BUFFER_STATUS, 0, 0),
382+ [MEM_START_ADDR] = REG_FIELD(ISP1763_HC_MEMORY, 0, 15),
383+ [HC_DATA] = REG_FIELD(ISP1763_HC_DATA, 0, 15),
384+ [HC_INTERRUPT] = REG_FIELD(ISP1763_HC_INTERRUPT, 0, 10),
385+ [HC_ATL_IRQ_ENABLE] = REG_FIELD(ISP1763_HC_INTERRUPT_ENABLE, 8, 8),
386+ [HC_INT_IRQ_ENABLE] = REG_FIELD(ISP1763_HC_INTERRUPT_ENABLE, 7, 7),
387+ [HC_ISO_IRQ_MASK_OR] = REG_FIELD(ISP1763_HC_ISO_IRQ_MASK_OR, 0, 15),
388+ [HC_INT_IRQ_MASK_OR] = REG_FIELD(ISP1763_HC_INT_IRQ_MASK_OR, 0, 15),
389+ [HC_ATL_IRQ_MASK_OR] = REG_FIELD(ISP1763_HC_ATL_IRQ_MASK_OR, 0, 15),
390+ [HC_ISO_IRQ_MASK_AND] = REG_FIELD(ISP1763_HC_ISO_IRQ_MASK_AND, 0, 15),
391+ [HC_INT_IRQ_MASK_AND] = REG_FIELD(ISP1763_HC_INT_IRQ_MASK_AND, 0, 15),
392+ [HC_ATL_IRQ_MASK_AND] = REG_FIELD(ISP1763_HC_ATL_IRQ_MASK_AND, 0, 15),
393+ [HW_HC_2_DIS] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 15, 15),
394+ [HW_OTG_DISABLE] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 10, 10),
395+ [HW_SW_SEL_HC_DC] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 7, 7),
396+ [HW_VBUS_DRV] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 4, 4),
397+ [HW_SEL_CP_EXT] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 3, 3),
398+ [HW_DM_PULLDOWN] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 2, 2),
399+ [HW_DP_PULLDOWN] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 1, 1),
400+ [HW_DP_PULLUP] = REG_FIELD(ISP1763_HC_OTG_CTRL_SET, 0, 0),
401+ [HW_HC_2_DIS_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 15, 15),
402+ [HW_OTG_DISABLE_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 10, 10),
403+ [HW_SW_SEL_HC_DC_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 7, 7),
404+ [HW_VBUS_DRV_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 4, 4),
405+ [HW_SEL_CP_EXT_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 3, 3),
406+ [HW_DM_PULLDOWN_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 2, 2),
407+ [HW_DP_PULLDOWN_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 1, 1),
408+ [HW_DP_PULLUP_CLEAR] = REG_FIELD(ISP1763_HC_OTG_CTRL_CLEAR, 0, 0),
409+};
410+
411+int isp1760_register(struct isp1760_device *isp, struct resource *mem, int irq,
412+ unsigned long irqflags)
413+{
414+ const struct regmap_config *hc_regmap;
415+ const struct reg_field *hc_reg_fields;
416+ struct isp1760_hcd *hcd;
417+ struct regmap_field *f;
418+ unsigned int devflags;
419+ struct udevice *dev;
420+ int ret;
421+ int i;
422+
423+ hcd = &isp->hcd;
424+ devflags = isp->devflags;
425+ dev = isp->dev;
426+
427+ hcd->is_isp1763 = !!(devflags & ISP1760_FLAG_ISP1763);
428+
429+ if (!hcd->is_isp1763 && (devflags & ISP1760_FLAG_BUS_WIDTH_8)) {
430+ dev_err(dev, "isp1760/61 do not support data width 8\n");
431+ return -EINVAL;
432+ }
433+
434+ if (hcd->is_isp1763) {
435+ hc_regmap = &isp1763_hc_regmap_conf;
436+ hc_reg_fields = &isp1763_hc_reg_fields[0];
437+ } else {
438+ hc_regmap = &isp1760_hc_regmap_conf;
439+ hc_reg_fields = &isp1760_hc_reg_fields[0];
440+ }
441+
442+ hcd->base = devm_ioremap(dev, mem->start, resource_size(mem));
443+ if (IS_ERR(hcd->base))
444+ return PTR_ERR(hcd->base);
445+
446+ hcd->regs = devm_regmap_init(dev, NULL, NULL, hc_regmap);
447+ if (IS_ERR(hcd->regs))
448+ return PTR_ERR(hcd->regs);
449+
450+ for (i = 0; i < HC_FIELD_MAX; i++) {
451+ f = devm_regmap_field_alloc(dev, hcd->regs, hc_reg_fields[i]);
452+ if (IS_ERR(f))
453+ return PTR_ERR(f);
454+
455+ hcd->fields[i] = f;
456+ }
457+
458+ if (hcd->is_isp1763)
459+ hcd->memory_layout = &isp1763_memory_conf;
460+ else
461+ hcd->memory_layout = &isp176x_memory_conf;
462+
463+ ret = isp1760_init_core(isp);
464+ if (ret < 0)
465+ return ret;
466+
467+ hcd->dev = dev;
468+
469+ ret = isp1760_hcd_register(hcd, mem, irq, irqflags, dev);
470+ if (ret < 0)
471+ return ret;
472+
473+ ret = isp1760_hcd_lowlevel_init(hcd);
474+ if (ret < 0)
475+ return ret;
476+
477+ dev_set_drvdata(dev, isp);
478+
479+ return 0;
480+}
481+
482+void isp1760_unregister(struct isp1760_device *isp)
483+{
484+ isp1760_hcd_unregister(&isp->hcd);
485+
486+ return;
487+}
488diff --git a/drivers/usb/isp1760/isp1760-core.h b/drivers/usb/isp1760/isp1760-core.h
489new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500490index 0000000000..0a60e30b5f
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400491--- /dev/null
492+++ b/drivers/usb/isp1760/isp1760-core.h
493@@ -0,0 +1,96 @@
494+/* SPDX-License-Identifier: GPL-2.0 */
495+/*
496+ * Driver for the NXP ISP1760 chip
497+ *
498+ * Copyright 2021 Linaro, Rui Miguel Silva
499+ * Copyright 2014 Laurent Pinchart
500+ * Copyright 2007 Sebastian Siewior
501+ *
502+ * Contacts:
503+ * Sebastian Siewior <bigeasy@linutronix.de>
504+ * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
505+ * Rui Miguel Silva <rui.silva@linaro.org>
506+ */
507+
508+#ifndef _ISP1760_CORE_H_
509+#define _ISP1760_CORE_H_
510+
511+#include <linux/compat.h>
512+#include <linux/ioport.h>
513+#include <regmap.h>
514+
515+#include "isp1760-hcd.h"
516+
517+struct device;
518+struct gpio_desc;
519+
520+/*
521+ * Device flags that can vary from board to board. All of these
522+ * indicate the most "atypical" case, so that a devflags of 0 is
523+ * a sane default configuration.
524+ */
525+#define ISP1760_FLAG_BUS_WIDTH_16 0x00000002 /* 16-bit data bus width */
526+#define ISP1760_FLAG_PERIPHERAL_EN 0x00000004 /* Port 1 supports Peripheral mode*/
527+#define ISP1760_FLAG_ANALOG_OC 0x00000008 /* Analog overcurrent */
528+#define ISP1760_FLAG_DACK_POL_HIGH 0x00000010 /* DACK active high */
529+#define ISP1760_FLAG_DREQ_POL_HIGH 0x00000020 /* DREQ active high */
530+#define ISP1760_FLAG_ISP1761 0x00000040 /* Chip is ISP1761 */
531+#define ISP1760_FLAG_INTR_POL_HIGH 0x00000080 /* Interrupt polarity active high */
532+#define ISP1760_FLAG_INTR_EDGE_TRIG 0x00000100 /* Interrupt edge triggered */
533+#define ISP1760_FLAG_ISP1763 0x00000200 /* Chip is ISP1763 */
534+#define ISP1760_FLAG_BUS_WIDTH_8 0x00000400 /* 8-bit data bus width */
535+
536+struct isp1760_device {
537+ struct udevice *dev;
538+
539+ unsigned int devflags;
540+ struct gpio_desc *rst_gpio;
541+
542+ struct isp1760_hcd hcd;
543+};
544+
545+int isp1760_register(struct isp1760_device *isp, struct resource *mem, int irq,
546+ unsigned long irqflags);
547+void isp1760_unregister(struct isp1760_device *isp);
548+
549+void isp1760_set_pullup(struct isp1760_device *isp, bool enable);
550+
551+static inline u32 isp1760_field_read(struct regmap_field **fields, u32 field)
552+{
553+ unsigned int val;
554+
555+ regmap_field_read(fields[field], &val);
556+
557+ return val;
558+}
559+
560+static inline void isp1760_field_write(struct regmap_field **fields, u32 field,
561+ u32 val)
562+{
563+ regmap_field_write(fields[field], val);
564+}
565+
566+static inline void isp1760_field_set(struct regmap_field **fields, u32 field)
567+{
568+ isp1760_field_write(fields, field, 0xFFFFFFFF);
569+}
570+
571+static inline void isp1760_field_clear(struct regmap_field **fields, u32 field)
572+{
573+ isp1760_field_write(fields, field, 0);
574+}
575+
576+static inline u32 isp1760_reg_read(struct regmap *regs, u32 reg)
577+{
578+ unsigned int val;
579+
580+ regmap_read(regs, reg, &val);
581+
582+ return val;
583+}
584+
585+static inline void isp1760_reg_write(struct regmap *regs, u32 reg, u32 val)
586+{
587+ regmap_write(regs, reg, val);
588+}
589+#endif
590diff --git a/drivers/usb/isp1760/isp1760-hcd.c b/drivers/usb/isp1760/isp1760-hcd.c
591new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500592index 0000000000..b1d86dd69b
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400593--- /dev/null
594+++ b/drivers/usb/isp1760/isp1760-hcd.c
595@@ -0,0 +1,2574 @@
596+// SPDX-License-Identifier: GPL-2.0
597+/*
598+ * Driver for the NXP ISP1760 chip
599+ *
600+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
601+ *
602+ */
603+
604+#include <hexdump.h>
605+#include <common.h>
606+#include <asm/cache.h>
607+#include <cpu_func.h>
608+#include <dm.h>
609+#include <dm/device-internal.h>
610+#include <dm/device_compat.h>
611+#include <linux/bug.h>
612+#include <linux/kernel.h>
613+#include <linux/list.h>
614+#include <linux/usb/usb_urb_compat.h>
615+#include <usb.h>
616+#include <linux/io.h>
617+#include <linux/iopoll.h>
618+#include <asm/unaligned.h>
619+
620+#include "isp1760-core.h"
621+#include "isp1760-hcd.h"
622+#include "isp1760-regs.h"
623+#include "isp1760-uboot.h"
624+
625+static struct kmem_cache *qtd_cachep;
626+static struct kmem_cache *qh_cachep;
627+static struct kmem_cache *urb_listitem_cachep;
628+
629+typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
630+ struct isp1760_qtd *qtd);
631+
632+static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
633+{
634+ return hcd->hcd_priv;
635+}
636+
637+#define dw_to_le32(x) (cpu_to_le32((__force u32)x))
638+#define le32_to_dw(x) ((__force __dw)(le32_to_cpu(x)))
639+
640+/* urb state*/
641+#define DELETE_URB (0x0008)
642+#define NO_TRANSFER_ACTIVE (0xffffffff)
643+
644+/* Philips Proprietary Transfer Descriptor (PTD) */
645+typedef __u32 __bitwise __dw;
646+struct ptd {
647+ __dw dw0;
648+ __dw dw1;
649+ __dw dw2;
650+ __dw dw3;
651+ __dw dw4;
652+ __dw dw5;
653+ __dw dw6;
654+ __dw dw7;
655+};
656+
657+struct ptd_le32 {
658+ __le32 dw0;
659+ __le32 dw1;
660+ __le32 dw2;
661+ __le32 dw3;
662+ __le32 dw4;
663+ __le32 dw5;
664+ __le32 dw6;
665+ __le32 dw7;
666+};
667+
668+#define PTD_OFFSET 0x0400
669+#define ISO_PTD_OFFSET 0x0400
670+#define INT_PTD_OFFSET 0x0800
671+#define ATL_PTD_OFFSET 0x0c00
672+#define PAYLOAD_OFFSET 0x1000
673+
674+#define ISP_BANK_0 0x00
675+#define ISP_BANK_1 0x01
676+#define ISP_BANK_2 0x02
677+#define ISP_BANK_3 0x03
678+
679+#define TO_DW(x) ((__force __dw)x)
680+#define TO_U32(x) ((__force u32)x)
681+
682+ /* ATL */
683+ /* DW0 */
684+#define DW0_VALID_BIT TO_DW(1)
685+#define FROM_DW0_VALID(x) (TO_U32(x) & 0x01)
686+#define TO_DW0_LENGTH(x) TO_DW((((u32)x) << 3))
687+#define TO_DW0_MAXPACKET(x) TO_DW((((u32)x) << 18))
688+#define TO_DW0_MULTI(x) TO_DW((((u32)x) << 29))
689+#define TO_DW0_ENDPOINT(x) TO_DW((((u32)x) << 31))
690+/* DW1 */
691+#define TO_DW1_DEVICE_ADDR(x) TO_DW((((u32)x) << 3))
692+#define TO_DW1_PID_TOKEN(x) TO_DW((((u32)x) << 10))
693+#define DW1_TRANS_BULK TO_DW(((u32)2 << 12))
694+#define DW1_TRANS_INT TO_DW(((u32)3 << 12))
695+#define DW1_TRANS_SPLIT TO_DW(((u32)1 << 14))
696+#define DW1_SE_USB_LOSPEED TO_DW(((u32)2 << 16))
697+#define TO_DW1_PORT_NUM(x) TO_DW((((u32)x) << 18))
698+#define TO_DW1_HUB_NUM(x) TO_DW((((u32)x) << 25))
699+/* DW2 */
700+#define TO_DW2_DATA_START_ADDR(x) TO_DW((((u32)x) << 8))
701+#define TO_DW2_RL(x) TO_DW(((x) << 25))
702+#define FROM_DW2_RL(x) ((TO_U32(x) >> 25) & 0xf)
703+/* DW3 */
704+#define FROM_DW3_NRBYTESTRANSFERRED(x) TO_U32((x) & 0x3fff)
705+#define FROM_DW3_SCS_NRBYTESTRANSFERRED(x) TO_U32((x) & 0x07ff)
706+#define TO_DW3_NAKCOUNT(x) TO_DW(((x) << 19))
707+#define FROM_DW3_NAKCOUNT(x) ((TO_U32(x) >> 19) & 0xf)
708+#define TO_DW3_CERR(x) TO_DW(((x) << 23))
709+#define FROM_DW3_CERR(x) ((TO_U32(x) >> 23) & 0x3)
710+#define TO_DW3_DATA_TOGGLE(x) TO_DW(((x) << 25))
711+#define FROM_DW3_DATA_TOGGLE(x) ((TO_U32(x) >> 25) & 0x1)
712+#define TO_DW3_PING(x) TO_DW(((x) << 26))
713+#define FROM_DW3_PING(x) ((TO_U32(x) >> 26) & 0x1)
714+#define DW3_ERROR_BIT TO_DW((1 << 28))
715+#define DW3_BABBLE_BIT TO_DW((1 << 29))
716+#define DW3_HALT_BIT TO_DW((1 << 30))
717+#define DW3_ACTIVE_BIT TO_DW((1 << 31))
718+#define FROM_DW3_ACTIVE(x) ((TO_U32(x) >> 31) & 0x01)
719+
720+#define INT_UNDERRUN (1 << 2)
721+#define INT_BABBLE (1 << 1)
722+#define INT_EXACT (1 << 0)
723+
724+#define SETUP_PID (2)
725+#define IN_PID (1)
726+#define OUT_PID (0)
727+
728+/* Errata 1 */
729+#define RL_COUNTER (0)
730+#define NAK_COUNTER (0)
731+#define ERR_COUNTER (3)
732+
733+struct isp1760_qtd {
734+ u8 packet_type;
735+ void *data_buffer;
736+ u32 payload_addr;
737+
738+ /* the rest is HCD-private */
739+ struct list_head qtd_list;
740+ struct urb *urb;
741+ size_t length;
742+ size_t actual_length;
743+
744+ /* QTD_ENQUEUED: waiting for transfer (inactive) */
745+ /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
746+ /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
747+ interrupt handler may touch this qtd! */
748+ /* QTD_XFER_COMPLETE: payload has been transferred successfully */
749+ /* QTD_RETIRE: transfer error/abort qtd */
750+#define QTD_ENQUEUED 0
751+#define QTD_PAYLOAD_ALLOC 1
752+#define QTD_XFER_STARTED 2
753+#define QTD_XFER_COMPLETE 3
754+#define QTD_RETIRE 4
755+ u32 status;
756+};
757+
758+/* Queue head, one for each active endpoint */
759+struct isp1760_qh {
760+ struct list_head qh_list;
761+ struct list_head qtd_list;
762+ int epnum;
763+ u32 toggle;
764+ u32 ping;
765+ int slot;
766+ int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
767+};
768+
769+struct urb_listitem {
770+ struct list_head urb_list;
771+ struct urb *urb;
772+};
773+
774+static const u32 isp1763_hc_portsc1_fields[] = {
775+ [PORT_OWNER] = BIT(13),
776+ [PORT_POWER] = BIT(12),
777+ [PORT_LSTATUS] = BIT(10),
778+ [PORT_RESET] = BIT(8),
779+ [PORT_SUSPEND] = BIT(7),
780+ [PORT_RESUME] = BIT(6),
781+ [PORT_PE] = BIT(2),
782+ [PORT_CSC] = BIT(1),
783+ [PORT_CONNECT] = BIT(0),
784+};
785+
786+static struct descriptor {
787+ struct usb_device_descriptor device;
788+ struct usb_config_descriptor config;
789+ struct usb_interface_descriptor interface;
790+ struct usb_endpoint_descriptor endpoint;
791+} __packed rh_descriptor = {
792+ {
793+ /* usb 2.0 root hub device descriptor */
794+ 0x12, /* __u8 bLength; */
795+ USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
796+ 0x0002, /* __le16 bcdUSB; v2.0 */
797+
798+ 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
799+ 0x00, /* __u8 bDeviceSubClass; */
800+ 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
801+ 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
802+
803+ 0x6b1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
804+ 0x0200, /* __le16 idProduct; device 0x0002 */
805+ 0x0001, /* __le16 bcdDevice */
806+
807+ 0x03, /* __u8 iManufacturer; */
808+ 0x02, /* __u8 iProduct; */
809+ 0x01, /* __u8 iSerialNumber; */
810+ 0x01 /* __u8 bNumConfigurations; */
811+ }, {
812+ /* one configuration */
813+ 0x09, /* __u8 bLength; */
814+ USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
815+ 0x1900, /* __le16 wTotalLength; */
816+ 0x01, /* __u8 bNumInterfaces; (1) */
817+ 0x01, /* __u8 bConfigurationValue; */
818+ 0x00, /* __u8 iConfiguration; */
819+ 0xc0, /* __u8 bmAttributes;
820+ Bit 7: must be set,
821+ 6: Self-powered,
822+ 5: Remote wakeup,
823+ 4..0: resvd */
824+ 0x00, /* __u8 MaxPower; */
825+ }, {
826+ /* one interface */
827+ 0x09, /* __u8 if_bLength; */
828+ USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
829+ 0x00, /* __u8 if_bInterfaceNumber; */
830+ 0x00, /* __u8 if_bAlternateSetting; */
831+ 0x01, /* __u8 if_bNumEndpoints; */
832+ 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
833+ 0x00, /* __u8 if_bInterfaceSubClass; */
834+ 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
835+ 0x00, /* __u8 if_iInterface; */
836+ }, {
837+ /* one endpoint (status change endpoint) */
838+ 0x07, /* __u8 ep_bLength; */
839+ USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
840+ 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
841+ 0x03, /* __u8 ep_bmAttributes; Interrupt */
842+ /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
843+ * see hub.c:hub_configure() for details. */
844+ (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
845+ 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
846+ },
847+};
848+
849+/*
850+ * Access functions for isp176x registers regmap fields
851+ */
852+static u32 isp1760_hcd_read(struct usb_hcd *hcd, u32 field)
853+{
854+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
855+
856+ return isp1760_field_read(priv->fields, field);
857+}
858+
859+/*
860+ * We need, in isp1763, to write directly the values to the portsc1
861+ * register so it will make the other values to trigger.
862+ */
863+static void isp1760_hcd_portsc1_set_clear(struct isp1760_hcd *priv, u32 field,
864+ u32 val)
865+{
866+ u32 bit = isp1763_hc_portsc1_fields[field];
867+ u32 port_status = readl(priv->base + ISP1763_HC_PORTSC1);
868+
869+ if (val)
870+ writel(port_status | bit, priv->base + ISP1763_HC_PORTSC1);
871+ else
872+ writel(port_status & ~bit, priv->base + ISP1763_HC_PORTSC1);
873+}
874+
875+static void isp1760_hcd_write(struct usb_hcd *hcd, u32 field, u32 val)
876+{
877+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
878+
879+ if (unlikely(priv->is_isp1763 &&
880+ (field >= PORT_OWNER && field <= PORT_CONNECT)))
881+ return isp1760_hcd_portsc1_set_clear(priv, field, val);
882+
883+ isp1760_field_write(priv->fields, field, val);
884+}
885+
886+static void isp1760_hcd_set(struct usb_hcd *hcd, u32 field)
887+{
888+ isp1760_hcd_write(hcd, field, 0xFFFFFFFF);
889+}
890+
891+static void isp1760_hcd_clear(struct usb_hcd *hcd, u32 field)
892+{
893+ isp1760_hcd_write(hcd, field, 0);
894+}
895+
896+static int isp1760_hcd_set_and_wait(struct usb_hcd *hcd, u32 field,
897+ u32 timeout_us)
898+{
899+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
900+ u32 val;
901+
902+ isp1760_hcd_set(hcd, field);
903+
904+ return regmap_field_read_poll_timeout(priv->fields[field], val,
905+ val, 10, timeout_us);
906+}
907+
908+static int isp1760_hcd_set_and_wait_swap(struct usb_hcd *hcd, u32 field,
909+ u32 timeout_us)
910+{
911+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
912+ u32 val;
913+
914+ isp1760_hcd_set(hcd, field);
915+
916+ return regmap_field_read_poll_timeout(priv->fields[field], val,
917+ !val, 10, timeout_us);
918+}
919+
920+static int isp1760_hcd_clear_and_wait(struct usb_hcd *hcd, u32 field,
921+ u32 timeout_us)
922+{
923+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
924+ u32 val;
925+
926+ isp1760_hcd_clear(hcd, field);
927+
928+ return regmap_field_read_poll_timeout(priv->fields[field], val,
929+ !val, 10, timeout_us);
930+}
931+
932+static bool isp1760_hcd_is_set(struct usb_hcd *hcd, u32 field)
933+{
934+ return !!isp1760_hcd_read(hcd, field);
935+}
936+
937+static bool isp1760_hcd_ppc_is_set(struct usb_hcd *hcd)
938+{
939+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
940+
941+ if (priv->is_isp1763)
942+ return true;
943+
944+ return isp1760_hcd_is_set(hcd, HCS_PPC);
945+}
946+
947+static u32 isp1760_hcd_n_ports(struct usb_hcd *hcd)
948+{
949+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
950+
951+ if (priv->is_isp1763)
952+ return 1;
953+
954+ return isp1760_hcd_read(hcd, HCS_N_PORTS);
955+}
956+
957+/*
958+ * Access functions for isp176x memory (offset >= 0x0400).
959+ *
960+ * bank_reads8() reads memory locations prefetched by an earlier write to
961+ * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
962+ * bank optimizations, you should use the more generic mem_read() below.
963+ *
964+ * For access to ptd memory, use the specialized ptd_read() and ptd_write()
965+ * below.
966+ *
967+ * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
968+ * doesn't quite work because some people have to enforce 32-bit access
969+ */
970+static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
971+ __u32 *dst, u32 bytes)
972+{
973+ __u32 __iomem *src;
974+ u32 val;
975+ __u8 *src_byteptr;
976+ __u8 *dst_byteptr;
977+
978+ src = src_base + (bank_addr | src_offset);
979+
980+ if (src_offset < PAYLOAD_OFFSET) {
981+ while (bytes >= 4) {
982+ *dst = readl_relaxed(src);
983+ bytes -= 4;
984+ src++;
985+ dst++;
986+ }
987+ } else {
988+ while (bytes >= 4) {
989+ *dst = __raw_readl(src);
990+ bytes -= 4;
991+ src++;
992+ dst++;
993+ }
994+ }
995+
996+ if (!bytes)
997+ return;
998+
999+ /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
1000+ * allocated.
1001+ */
1002+ if (src_offset < PAYLOAD_OFFSET)
1003+ val = readl_relaxed(src);
1004+ else
1005+ val = __raw_readl(src);
1006+
1007+ dst_byteptr = (void *) dst;
1008+ src_byteptr = (void *) &val;
1009+ while (bytes > 0) {
1010+ *dst_byteptr = *src_byteptr;
1011+ dst_byteptr++;
1012+ src_byteptr++;
1013+ bytes--;
1014+ }
1015+}
1016+
1017+static void isp1760_mem_read(struct usb_hcd *hcd, u32 src_offset, void *dst,
1018+ u32 bytes)
1019+{
1020+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1021+
1022+ isp1760_hcd_write(hcd, MEM_BANK_SEL, ISP_BANK_0);
1023+ isp1760_hcd_write(hcd, MEM_START_ADDR, src_offset);
1024+ ndelay(100);
1025+
1026+ bank_reads8(priv->base, src_offset, ISP_BANK_0, dst, bytes);
1027+}
1028+
1029+/*
1030+ * ISP1763 does not have the banks direct host controller memory access,
1031+ * needs to use the HC_DATA register. Add data read/write according to this,
1032+ * and also adjust 16bit access.
1033+ */
1034+static void isp1763_mem_read(struct usb_hcd *hcd, u16 srcaddr,
1035+ u16 *dstptr, u32 bytes)
1036+{
1037+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1038+
1039+
1040+ /* Write the starting device address to the hcd memory register */
1041+ isp1760_reg_write(priv->regs, ISP1763_HC_MEMORY, srcaddr);
1042+ ndelay(100); /* Delay between consecutive access */
1043+
1044+ /* As long there are at least 16-bit to read ... */
1045+ while (bytes >= 2) {
1046+ *dstptr = __raw_readw(priv->base + ISP1763_HC_DATA);
1047+ bytes -= 2;
1048+ dstptr++;
1049+ }
1050+
1051+ /* If there are no more bytes to read, return */
1052+ if (bytes <= 0)
1053+ return;
1054+
1055+ *((u8 *)dstptr) = (u8)(readw(priv->base + ISP1763_HC_DATA) & 0xFF);
1056+}
1057+
1058+static void mem_read(struct usb_hcd *hcd, u32 src_offset, __u32 *dst,
1059+ u32 bytes)
1060+{
1061+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1062+
1063+ if (!priv->is_isp1763)
1064+ return isp1760_mem_read(hcd, src_offset, (u16 *)dst, bytes);
1065+
1066+ isp1763_mem_read(hcd, (u16)src_offset, (u16 *)dst, bytes);
1067+}
1068+
1069+static void isp1760_mem_write(void __iomem *dst_base, u32 dst_offset,
1070+ __u32 const *src, u32 bytes)
1071+{
1072+ __u32 __iomem *dst;
1073+
1074+ dst = dst_base + dst_offset;
1075+
1076+ if (dst_offset < PAYLOAD_OFFSET) {
1077+ while (bytes >= 4) {
1078+ writel_relaxed(*src, dst);
1079+ bytes -= 4;
1080+ src++;
1081+ dst++;
1082+ }
1083+ } else {
1084+ while (bytes >= 4) {
1085+ __raw_writel(*src, dst);
1086+ bytes -= 4;
1087+ src++;
1088+ dst++;
1089+ }
1090+ }
1091+
1092+ if (!bytes)
1093+ return;
1094+ /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
1095+ * extra bytes should not be read by the HW.
1096+ */
1097+
1098+ if (dst_offset < PAYLOAD_OFFSET)
1099+ writel_relaxed(*src, dst);
1100+ else
1101+ __raw_writel(*src, dst);
1102+}
1103+
1104+static void isp1763_mem_write(struct usb_hcd *hcd, u16 dstaddr, u16 *src,
1105+ u32 bytes)
1106+{
1107+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1108+
1109+ /* Write the starting device address to the hcd memory register */
1110+ isp1760_reg_write(priv->regs, ISP1763_HC_MEMORY, dstaddr);
1111+ ndelay(100); /* Delay between consecutive access */
1112+
1113+ while (bytes >= 2) {
1114+ /* Get and write the data; then adjust the data ptr and len */
1115+ __raw_writew(*src, priv->base + ISP1763_HC_DATA);
1116+ bytes -= 2;
1117+ src++;
1118+ }
1119+
1120+ /* If there are no more bytes to process, return */
1121+ if (bytes <= 0)
1122+ return;
1123+
1124+ /*
1125+ * The only way to get here is if there is a single byte left,
1126+ * get it and write it to the data reg;
1127+ */
1128+ writew(*((u8 *)src), priv->base + ISP1763_HC_DATA);
1129+}
1130+
1131+static void mem_write(struct usb_hcd *hcd, u32 dst_offset, __u32 *src,
1132+ u32 bytes)
1133+{
1134+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1135+
1136+ if (!priv->is_isp1763)
1137+ return isp1760_mem_write(priv->base, dst_offset, src, bytes);
1138+
1139+ isp1763_mem_write(hcd, dst_offset, (u16 *)src, bytes);
1140+}
1141+
1142+/*
1143+ * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
1144+ * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
1145+ */
1146+static void isp1760_ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
1147+ struct ptd *ptd)
1148+{
1149+ u16 src_offset = ptd_offset + slot * sizeof(*ptd);
1150+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1151+
1152+ isp1760_hcd_write(hcd, MEM_BANK_SEL, ISP_BANK_0);
1153+ isp1760_hcd_write(hcd, MEM_START_ADDR, src_offset);
1154+ ndelay(90);
1155+
1156+ bank_reads8(priv->base, src_offset, ISP_BANK_0, (void *)ptd,
1157+ sizeof(*ptd));
1158+}
1159+
1160+static void isp1763_ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
1161+ struct ptd *ptd)
1162+{
1163+ u16 src_offset = ptd_offset + slot * sizeof(*ptd);
1164+ struct ptd_le32 le32_ptd;
1165+
1166+ isp1763_mem_read(hcd, src_offset, (u16 *)&le32_ptd, sizeof(le32_ptd));
1167+ /* Normalize the data obtained */
1168+ ptd->dw0 = le32_to_dw(le32_ptd.dw0);
1169+ ptd->dw1 = le32_to_dw(le32_ptd.dw1);
1170+ ptd->dw2 = le32_to_dw(le32_ptd.dw2);
1171+ ptd->dw3 = le32_to_dw(le32_ptd.dw3);
1172+ ptd->dw4 = le32_to_dw(le32_ptd.dw4);
1173+ ptd->dw5 = le32_to_dw(le32_ptd.dw5);
1174+ ptd->dw6 = le32_to_dw(le32_ptd.dw6);
1175+ ptd->dw7 = le32_to_dw(le32_ptd.dw7);
1176+}
1177+
1178+static void ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
1179+ struct ptd *ptd)
1180+{
1181+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1182+
1183+ if (!priv->is_isp1763)
1184+ return isp1760_ptd_read(hcd, ptd_offset, slot, ptd);
1185+
1186+ isp1763_ptd_read(hcd, ptd_offset, slot, ptd);
1187+}
1188+
1189+static void isp1763_ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
1190+ struct ptd *cpu_ptd)
1191+{
1192+ u16 dst_offset = ptd_offset + slot * sizeof(*cpu_ptd);
1193+ struct ptd_le32 ptd;
1194+
1195+ ptd.dw0 = dw_to_le32(cpu_ptd->dw0);
1196+ ptd.dw1 = dw_to_le32(cpu_ptd->dw1);
1197+ ptd.dw2 = dw_to_le32(cpu_ptd->dw2);
1198+ ptd.dw3 = dw_to_le32(cpu_ptd->dw3);
1199+ ptd.dw4 = dw_to_le32(cpu_ptd->dw4);
1200+ ptd.dw5 = dw_to_le32(cpu_ptd->dw5);
1201+ ptd.dw6 = dw_to_le32(cpu_ptd->dw6);
1202+ ptd.dw7 = dw_to_le32(cpu_ptd->dw7);
1203+
1204+ isp1763_mem_write(hcd, dst_offset, (u16 *)&ptd.dw0,
1205+ 8 * sizeof(ptd.dw0));
1206+}
1207+
1208+static void isp1760_ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
1209+ struct ptd *ptd)
1210+{
1211+ u32 dst_offset = ptd_offset + slot * sizeof(*ptd);
1212+
1213+ /*
1214+ * Make sure dw0 gets written last (after other dw's and after payload)
1215+ * since it contains the enable bit
1216+ */
1217+ isp1760_mem_write(base, dst_offset + sizeof(ptd->dw0),
1218+ (__force u32 *)&ptd->dw1, 7 * sizeof(ptd->dw1));
1219+ wmb();
1220+ isp1760_mem_write(base, dst_offset, (__force u32 *)&ptd->dw0,
1221+ sizeof(ptd->dw0));
1222+}
1223+
1224+static void ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
1225+ struct ptd *ptd)
1226+{
1227+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1228+
1229+ if (!priv->is_isp1763)
1230+ return isp1760_ptd_write(priv->base, ptd_offset, slot, ptd);
1231+
1232+ isp1763_ptd_write(hcd, ptd_offset, slot, ptd);
1233+}
1234+
1235+/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
1236+static void init_memory(struct isp1760_hcd *priv)
1237+{
1238+ const struct isp1760_memory_layout *mem = priv->memory_layout;
1239+ int i, j, curr;
1240+ u32 payload_addr;
1241+
1242+ payload_addr = PAYLOAD_OFFSET;
1243+
1244+ for (i = 0, curr = 0; i < ARRAY_SIZE(mem->blocks); i++) {
1245+ for (j = 0; j < mem->blocks[i]; j++, curr++) {
1246+ priv->memory_pool[curr + j].start = payload_addr;
1247+ priv->memory_pool[curr + j].size = mem->blocks_size[i];
1248+ priv->memory_pool[curr + j].free = 1;
1249+ payload_addr += priv->memory_pool[curr + j].size;
1250+ }
1251+ }
1252+
1253+ WARN_ON(payload_addr - priv->memory_pool[0].start >
1254+ mem->payload_area_size);
1255+}
1256+
1257+static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
1258+{
1259+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1260+ const struct isp1760_memory_layout *mem = priv->memory_layout;
1261+ int i;
1262+
1263+ WARN_ON(qtd->payload_addr);
1264+
1265+ if (!qtd->length)
1266+ return;
1267+
1268+ for (i = 0; i < mem->payload_blocks; i++) {
1269+ if (priv->memory_pool[i].size >= qtd->length &&
1270+ priv->memory_pool[i].free) {
1271+ priv->memory_pool[i].free = 0;
1272+ qtd->payload_addr = priv->memory_pool[i].start;
1273+ return;
1274+ }
1275+ }
1276+}
1277+
1278+static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
1279+{
1280+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1281+ const struct isp1760_memory_layout *mem = priv->memory_layout;
1282+ int i;
1283+
1284+ if (!qtd->payload_addr)
1285+ return;
1286+
1287+ for (i = 0; i < mem->payload_blocks; i++) {
1288+ if (priv->memory_pool[i].start == qtd->payload_addr) {
1289+ WARN_ON(priv->memory_pool[i].free);
1290+ priv->memory_pool[i].free = 1;
1291+ qtd->payload_addr = 0;
1292+ return;
1293+ }
1294+ }
1295+
1296+ WARN_ON(1);
1297+ qtd->payload_addr = 0;
1298+}
1299+
1300+/* reset a non-running (STS_HALT == 1) controller */
1301+static int ehci_reset(struct usb_hcd *hcd)
1302+{
1303+ return isp1760_hcd_set_and_wait_swap(hcd, CMD_RESET, 250 * 1000);
1304+}
1305+
1306+static struct isp1760_qh *qh_alloc(gfp_t flags)
1307+{
1308+ struct isp1760_qh *qh;
1309+
1310+ qh = kmem_cache_alloc(qh_cachep, flags);
1311+ if (!qh)
1312+ return NULL;
1313+
1314+ memset(qh, '\0', qh_cachep->sz);
1315+ INIT_LIST_HEAD(&qh->qh_list);
1316+ INIT_LIST_HEAD(&qh->qtd_list);
1317+ qh->slot = -1;
1318+
1319+ return qh;
1320+}
1321+
1322+static void qh_free(struct isp1760_qh *qh)
1323+{
1324+ WARN_ON(!list_empty(&qh->qtd_list));
1325+ WARN_ON(qh->slot > -1);
1326+ kmem_cache_free(qh_cachep, qh);
1327+}
1328+
1329+/* one-time init, only for memory state */
1330+static int priv_init(struct usb_hcd *hcd)
1331+{
1332+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1333+ u32 isoc_cache;
1334+ u32 isoc_thres;
1335+ int i;
1336+
1337+ spin_lock_init(&priv->lock);
1338+
1339+ for (i = 0; i < QH_END; i++)
1340+ INIT_LIST_HEAD(&priv->qh_list[i]);
1341+
1342+ /*
1343+ * hw default: 1K periodic list heads, one per frame.
1344+ * periodic_size can shrink by USBCMD update if hcc_params allows.
1345+ */
1346+ priv->periodic_size = DEFAULT_I_TDPS;
1347+
1348+ if (priv->is_isp1763) {
1349+ priv->i_thresh = 2;
1350+ return 0;
1351+ }
1352+
1353+ /* controllers may cache some of the periodic schedule ... */
1354+ isoc_cache = isp1760_hcd_read(hcd, HCC_ISOC_CACHE);
1355+ isoc_thres = isp1760_hcd_read(hcd, HCC_ISOC_THRES);
1356+
1357+ /* full frame cache */
1358+ if (isoc_cache)
1359+ priv->i_thresh = 8;
1360+ else /* N microframes cached */
1361+ priv->i_thresh = 2 + isoc_thres;
1362+
1363+ return 0;
1364+}
1365+
1366+static int isp1760_hc_setup(struct usb_hcd *hcd)
1367+{
1368+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1369+ u32 atx_reset;
1370+ int result;
1371+ u32 scratch;
1372+ u32 pattern;
1373+
1374+ if (priv->is_isp1763)
1375+ pattern = 0xcafe;
1376+ else
1377+ pattern = 0xdeadcafe;
1378+
1379+ isp1760_hcd_write(hcd, HC_SCRATCH, pattern);
1380+
1381+ /* Change bus pattern */
1382+ scratch = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
1383+ scratch = isp1760_hcd_read(hcd, HC_SCRATCH);
1384+ if (scratch != pattern) {
1385+ printf("Scratch test failed. 0x%08x\n", scratch);
1386+ return -ENODEV;
1387+ }
1388+
1389+ /*
1390+ * The RESET_HC bit in the SW_RESET register is supposed to reset the
1391+ * host controller without touching the CPU interface registers, but at
1392+ * least on the ISP1761 it seems to behave as the RESET_ALL bit and
1393+ * reset the whole device. We thus can't use it here, so let's reset
1394+ * the host controller through the EHCI USB Command register. The device
1395+ * has been reset in core code anyway, so this shouldn't matter.
1396+ */
1397+ isp1760_hcd_clear(hcd, ISO_BUF_FILL);
1398+ isp1760_hcd_clear(hcd, INT_BUF_FILL);
1399+ isp1760_hcd_clear(hcd, ATL_BUF_FILL);
1400+
1401+ isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
1402+ isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
1403+ isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
1404+
1405+ result = ehci_reset(hcd);
1406+ if (result)
1407+ return result;
1408+
1409+ /* Step 11 passed */
1410+
1411+ /* ATL reset */
1412+ if (priv->is_isp1763)
1413+ atx_reset = SW_RESET_RESET_ATX;
1414+ else
1415+ atx_reset = ALL_ATX_RESET;
1416+
1417+ isp1760_hcd_set(hcd, atx_reset);
1418+ mdelay(10);
1419+ isp1760_hcd_clear(hcd, atx_reset);
1420+
1421+ if (priv->is_isp1763) {
1422+ isp1760_hcd_set(hcd, HW_OTG_DISABLE);
1423+ isp1760_hcd_set(hcd, HW_SW_SEL_HC_DC_CLEAR);
1424+ isp1760_hcd_set(hcd, HW_HC_2_DIS_CLEAR);
1425+ isp1760_hcd_set(hcd, HW_DM_PULLDOWN);
1426+ isp1760_hcd_set(hcd, HW_DP_PULLDOWN);
1427+ mdelay(10);
1428+
1429+ isp1760_hcd_set(hcd, HW_INTF_LOCK);
1430+ }
1431+
1432+ isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
1433+ isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
1434+
1435+ return priv_init(hcd);
1436+}
1437+
1438+static u32 base_to_chip(u32 base)
1439+{
1440+ return ((base - 0x400) >> 3);
1441+}
1442+
1443+static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
1444+{
1445+ struct urb *urb;
1446+
1447+ if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
1448+ return 1;
1449+
1450+ urb = qtd->urb;
1451+ qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
1452+
1453+ return (qtd->urb != urb);
1454+}
1455+
1456+/* magic numbers that can affect system performance */
1457+#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
1458+#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
1459+#define EHCI_TUNE_RL_TT 0
1460+#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
1461+#define EHCI_TUNE_MULT_TT 1
1462+#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
1463+
1464+static void create_ptd_atl(struct isp1760_qh *qh, struct isp1760_qtd *qtd,
1465+ struct ptd *ptd)
1466+{
1467+ u32 maxpacket;
1468+ u32 multi;
1469+ u32 rl = RL_COUNTER;
1470+ u32 nak = NAK_COUNTER;
1471+ u8 portnr;
1472+ u8 hubaddr;
1473+
1474+ memset(ptd, 0, sizeof(*ptd));
1475+
1476+ /* according to 3.6.2, max packet len can not be > 0x400 */
1477+ maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe);
1478+ multi = 1 + ((maxpacket >> 11) & 0x3);
1479+ maxpacket &= 0x7ff;
1480+
1481+ /* DW0 */
1482+ ptd->dw0 = DW0_VALID_BIT;
1483+ ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
1484+ ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
1485+ ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
1486+
1487+ /* DW1 */
1488+ ptd->dw1 = TO_DW((usb_pipeendpoint(qtd->urb->pipe) >> 1));
1489+ ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
1490+ ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
1491+
1492+ if (usb_pipebulk(qtd->urb->pipe))
1493+ ptd->dw1 |= DW1_TRANS_BULK;
1494+ else if (usb_pipeint(qtd->urb->pipe))
1495+ ptd->dw1 |= DW1_TRANS_INT;
1496+
1497+ if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
1498+ /* split transaction */
1499+
1500+ ptd->dw1 |= DW1_TRANS_SPLIT;
1501+ if (qtd->urb->dev->speed == USB_SPEED_LOW)
1502+ ptd->dw1 |= DW1_SE_USB_LOSPEED;
1503+
1504+ if (!qtd->urb->dev->dev->parent_priv_) {
1505+ portnr = qtd->urb->dev->portnr;
1506+ hubaddr = qtd->urb->dev->devnum;
1507+ } else {
1508+ usb_find_usb2_hub_address_port(qtd->urb->dev, &hubaddr,
1509+ &portnr);
1510+ }
1511+
1512+ ptd->dw1 |= TO_DW1_PORT_NUM(portnr);
1513+ ptd->dw1 |= TO_DW1_HUB_NUM(hubaddr);
1514+
1515+ /* SE bit for Split INT transfers */
1516+ if (usb_pipeint(qtd->urb->pipe) &&
1517+ (qtd->urb->dev->speed == USB_SPEED_LOW))
1518+ ptd->dw1 |= DW1_SE_USB_LOSPEED;
1519+
1520+ rl = 0;
1521+ nak = 0;
1522+ } else {
1523+ ptd->dw0 |= TO_DW0_MULTI(multi);
1524+ if (usb_pipecontrol(qtd->urb->pipe) ||
1525+ usb_pipebulk(qtd->urb->pipe))
1526+ ptd->dw3 |= TO_DW3_PING(qh->ping);
1527+ }
1528+ /* DW2 */
1529+ ptd->dw2 = 0;
1530+ ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
1531+ ptd->dw2 |= TO_DW2_RL(rl);
1532+
1533+ /* DW3 */
1534+ ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
1535+ ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
1536+
1537+ if (usb_pipecontrol(qtd->urb->pipe)) {
1538+ if (qtd->data_buffer == qtd->urb->setup_packet) {
1539+ ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
1540+ } else if (last_qtd_of_urb(qtd, qh)) {
1541+ ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
1542+ }
1543+ }
1544+
1545+ ptd->dw3 |= DW3_ACTIVE_BIT;
1546+ /* Cerr */
1547+ ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
1548+}
1549+
1550+static void transform_add_int(struct isp1760_qh *qh, struct isp1760_qtd *qtd,
1551+ struct ptd *ptd)
1552+{
1553+ struct usb_host_endpoint *hep = qtd->urb->ep;
1554+ struct usb_endpoint_descriptor *epd = &hep->desc;
1555+ u32 usof;
1556+ u32 period;
1557+
1558+ /*
1559+ * Most of this is guessing. ISP1761 datasheet is quite unclear, and
1560+ * the algorithm from the original Philips driver code, which was
1561+ * pretty much used in this driver before as well, is quite horrendous
1562+ * and, i believe, incorrect. The code below follows the datasheet and
1563+ * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
1564+ * more reliable this way (fingers crossed...).
1565+ */
1566+
1567+ if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
1568+ /* urb->interval is in units of microframes (1/8 ms) */
1569+ period = epd->bInterval >> 3;
1570+
1571+ if (epd->bInterval > 4)
1572+ usof = 0x01; /* One bit set =>
1573+ interval 1 ms * uFrame-match */
1574+ else if (epd->bInterval > 2)
1575+ usof = 0x22; /* Two bits set => interval 1/2 ms */
1576+ else if (epd->bInterval > 1)
1577+ usof = 0x55; /* Four bits set => interval 1/4 ms */
1578+ else
1579+ usof = 0xff; /* All bits set => interval 1/8 ms */
1580+ } else {
1581+ /* urb->interval is in units of frames (1 ms) */
1582+ period = epd->bInterval;
1583+ usof = 0x0f; /* Execute Start Split on any of the
1584+ four first uFrames */
1585+
1586+ /*
1587+ * First 8 bits in dw5 is uSCS and "specifies which uSOF the
1588+ * complete split needs to be sent. Valid only for IN." Also,
1589+ * "All bits can be set to one for every transfer." (p 82,
1590+ * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
1591+ * that number come from? 0xff seems to work fine...
1592+ */
1593+ /* ptd->dw5 = 0x1c; */
1594+ ptd->dw5 = TO_DW(0xff); /* Execute Complete Split on any uFrame */
1595+ }
1596+
1597+ period = period >> 1;/* Ensure equal or shorter period than requested */
1598+ period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
1599+
1600+ ptd->dw2 |= TO_DW(period);
1601+ ptd->dw4 = TO_DW(usof);
1602+}
1603+
1604+static void create_ptd_int(struct isp1760_qh *qh, struct isp1760_qtd *qtd,
1605+ struct ptd *ptd)
1606+{
1607+ create_ptd_atl(qh, qtd, ptd);
1608+ transform_add_int(qh, qtd, ptd);
1609+}
1610+
1611+static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
1612+{
1613+ if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
1614+ void *ptr;
1615+ for (ptr = urb->transfer_buffer;
1616+ ptr < urb->transfer_buffer + urb->transfer_buffer_length;
1617+ ptr += PAGE_SIZE)
1618+ flush_dcache_range((unsigned long)ptr,
1619+ (unsigned long)ptr + PAGE_SIZE);
1620+ }
1621+
1622+ /* complete() can reenter this HCD */
1623+ usb_hcd_unlink_urb_from_ep(hcd, urb);
1624+ usb_hcd_giveback_urb(hcd, urb, urb->status);
1625+}
1626+
1627+static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
1628+ u8 packet_type)
1629+{
1630+ struct isp1760_qtd *qtd;
1631+
1632+ qtd = kmem_cache_alloc(qtd_cachep, flags);
1633+ if (!qtd)
1634+ return NULL;
1635+
1636+ memset(qtd, '\0', sizeof(*qtd));
1637+ INIT_LIST_HEAD(&qtd->qtd_list);
1638+ qtd->urb = urb;
1639+ qtd->packet_type = packet_type;
1640+ qtd->status = QTD_ENQUEUED;
1641+ qtd->actual_length = 0;
1642+
1643+ return qtd;
1644+}
1645+
1646+static void qtd_free(struct isp1760_qtd *qtd)
1647+{
1648+ WARN_ON(qtd->payload_addr);
1649+ kmem_cache_free(qtd_cachep, qtd);
1650+}
1651+
1652+static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
1653+ struct isp1760_slotinfo *slots,
1654+ struct isp1760_qtd *qtd, struct isp1760_qh *qh,
1655+ struct ptd *ptd)
1656+{
1657+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1658+ const struct isp1760_memory_layout *mem = priv->memory_layout;
1659+ int skip_map;
1660+
1661+ WARN_ON((slot < 0) || (slot > mem->slot_num - 1));
1662+ WARN_ON(qtd->length && !qtd->payload_addr);
1663+ WARN_ON(slots[slot].qtd);
1664+ WARN_ON(slots[slot].qh);
1665+ WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
1666+
1667+ if (priv->is_isp1763)
1668+ ndelay(100);
1669+
1670+ /* Make sure done map has not triggered from some unlinked transfer */
1671+ if (ptd_offset == ATL_PTD_OFFSET) {
1672+ skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1673+ isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP,
1674+ skip_map | (1 << slot));
1675+ priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
1676+ priv->atl_done_map &= ~(1 << slot);
1677+ } else {
1678+ skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1679+ isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP,
1680+ skip_map | (1 << slot));
1681+ priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
1682+ priv->int_done_map &= ~(1 << slot);
1683+ }
1684+
1685+ skip_map &= ~(1 << slot);
1686+ qh->slot = slot;
1687+ qtd->status = QTD_XFER_STARTED;
1688+ slots[slot].qtd = qtd;
1689+ slots[slot].qh = qh;
1690+
1691+ ptd_write(hcd, ptd_offset, slot, ptd);
1692+
1693+ if (ptd_offset == ATL_PTD_OFFSET)
1694+ isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
1695+ else
1696+ isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
1697+}
1698+
1699+static int is_short_bulk(struct isp1760_qtd *qtd)
1700+{
1701+ return (usb_pipebulk(qtd->urb->pipe) &&
1702+ (qtd->actual_length < qtd->length));
1703+}
1704+
1705+static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
1706+ struct list_head *urb_list)
1707+{
1708+ struct isp1760_qtd *qtd, *qtd_next;
1709+ struct urb_listitem *urb_listitem;
1710+ int last_qtd;
1711+
1712+ list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
1713+ if (qtd->status < QTD_XFER_COMPLETE)
1714+ break;
1715+
1716+ last_qtd = last_qtd_of_urb(qtd, qh);
1717+
1718+ if ((!last_qtd) && (qtd->status == QTD_RETIRE))
1719+ qtd_next->status = QTD_RETIRE;
1720+
1721+ if (qtd->status == QTD_XFER_COMPLETE) {
1722+ if (qtd->actual_length) {
1723+ switch (qtd->packet_type) {
1724+ case IN_PID:
1725+ mem_read(hcd, qtd->payload_addr,
1726+ qtd->data_buffer,
1727+ qtd->actual_length);
1728+ fallthrough;
1729+ case OUT_PID:
1730+ qtd->urb->actual_length +=
1731+ qtd->actual_length;
1732+ fallthrough;
1733+ case SETUP_PID:
1734+ break;
1735+ }
1736+ }
1737+
1738+ if (is_short_bulk(qtd)) {
1739+ if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
1740+ qtd->urb->status = -EREMOTEIO;
1741+ if (!last_qtd)
1742+ qtd_next->status = QTD_RETIRE;
1743+ }
1744+ }
1745+
1746+ if (qtd->payload_addr)
1747+ free_mem(hcd, qtd);
1748+
1749+ if (last_qtd) {
1750+ if ((qtd->status == QTD_RETIRE) &&
1751+ (qtd->urb->status == -EINPROGRESS))
1752+ qtd->urb->status = -EPIPE;
1753+ /* Defer calling of urb_done() since it releases lock */
1754+ urb_listitem = kmem_cache_alloc(urb_listitem_cachep,
1755+ GFP_ATOMIC);
1756+ if (unlikely(!urb_listitem))
1757+ break; /* Try again on next call */
1758+ urb_listitem->urb = qtd->urb;
1759+ list_add_tail(&urb_listitem->urb_list, urb_list);
1760+ }
1761+
1762+ list_del(&qtd->qtd_list);
1763+ qtd_free(qtd);
1764+ }
1765+}
1766+
1767+#define ENQUEUE_DEPTH 2
1768+static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
1769+{
1770+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1771+ const struct isp1760_memory_layout *mem = priv->memory_layout;
1772+ int slot_num = mem->slot_num;
1773+ int ptd_offset;
1774+ struct isp1760_slotinfo *slots;
1775+ int curr_slot, free_slot;
1776+ int n;
1777+ struct ptd ptd;
1778+ struct isp1760_qtd *qtd;
1779+
1780+ if (unlikely(list_empty(&qh->qtd_list)))
1781+ return;
1782+
1783+ /* Make sure this endpoint's TT buffer is clean before queueing ptds */
1784+ if (qh->tt_buffer_dirty)
1785+ return;
1786+
1787+ if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
1788+ qtd_list)->urb->pipe)) {
1789+ ptd_offset = INT_PTD_OFFSET;
1790+ slots = priv->int_slots;
1791+ } else {
1792+ ptd_offset = ATL_PTD_OFFSET;
1793+ slots = priv->atl_slots;
1794+ }
1795+
1796+ free_slot = -1;
1797+ for (curr_slot = 0; curr_slot < slot_num; curr_slot++) {
1798+ if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
1799+ free_slot = curr_slot;
1800+ if (slots[curr_slot].qh == qh)
1801+ break;
1802+ }
1803+
1804+ n = 0;
1805+ list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1806+ if (qtd->status == QTD_ENQUEUED) {
1807+ WARN_ON(qtd->payload_addr);
1808+ alloc_mem(hcd, qtd);
1809+ if ((qtd->length) && (!qtd->payload_addr))
1810+ break;
1811+
1812+ if (qtd->length && (qtd->packet_type == SETUP_PID ||
1813+ qtd->packet_type == OUT_PID)) {
1814+ mem_write(hcd, qtd->payload_addr,
1815+ qtd->data_buffer, qtd->length);
1816+ }
1817+
1818+ qtd->status = QTD_PAYLOAD_ALLOC;
1819+ }
1820+
1821+ if (qtd->status == QTD_PAYLOAD_ALLOC) {
1822+/*
1823+ if ((curr_slot > 31) && (free_slot == -1))
1824+ printf("%s: No slot "
1825+ "available for transfer\n", __func__);
1826+*/
1827+ /* Start xfer for this endpoint if not already done */
1828+ if ((curr_slot > slot_num - 1) && (free_slot > -1)) {
1829+ if (usb_pipeint(qtd->urb->pipe))
1830+ create_ptd_int(qh, qtd, &ptd);
1831+ else
1832+ create_ptd_atl(qh, qtd, &ptd);
1833+
1834+ start_bus_transfer(hcd, ptd_offset, free_slot,
1835+ slots, qtd, qh, &ptd);
1836+ curr_slot = free_slot;
1837+ }
1838+
1839+ n++;
1840+ if (n >= ENQUEUE_DEPTH)
1841+ break;
1842+ }
1843+ }
1844+}
1845+
1846+static void schedule_ptds(struct usb_hcd *hcd)
1847+{
1848+ struct isp1760_hcd *priv;
1849+ struct isp1760_qh *qh, *qh_next;
1850+ struct list_head *ep_queue;
1851+ LIST_HEAD(urb_list);
1852+ struct urb_listitem *urb_listitem, *urb_listitem_next;
1853+ int i;
1854+
1855+ if (!hcd) {
1856+ WARN_ON(1);
1857+ return;
1858+ }
1859+
1860+ priv = hcd_to_priv(hcd);
1861+
1862+ /*
1863+ * check finished/retired xfers, transfer payloads, call urb_done()
1864+ */
1865+ for (i = 0; i < QH_END; i++) {
1866+ ep_queue = &priv->qh_list[i];
1867+ list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1868+ collect_qtds(hcd, qh, &urb_list);
1869+ }
1870+
1871+ list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
1872+ urb_list) {
1873+ isp1760_urb_done(hcd, urb_listitem->urb);
1874+ kmem_cache_free(urb_listitem_cachep, urb_listitem);
1875+ }
1876+
1877+ /*
1878+ * Schedule packets for transfer.
1879+ *
1880+ * According to USB2.0 specification:
1881+ *
1882+ * 1st prio: interrupt xfers, up to 80 % of bandwidth
1883+ * 2nd prio: control xfers
1884+ * 3rd prio: bulk xfers
1885+ *
1886+ * ... but let's use a simpler scheme here (mostly because ISP1761 doc
1887+ * is very unclear on how to prioritize traffic):
1888+ *
1889+ * 1) Enqueue any queued control transfers, as long as payload chip mem
1890+ * and PTD ATL slots are available.
1891+ * 2) Enqueue any queued INT transfers, as long as payload chip mem
1892+ * and PTD INT slots are available.
1893+ * 3) Enqueue any queued bulk transfers, as long as payload chip mem
1894+ * and PTD ATL slots are available.
1895+ *
1896+ * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
1897+ * conservation of chip mem and performance.
1898+ *
1899+ * I'm sure this scheme could be improved upon!
1900+ */
1901+ for (i = 0; i < QH_END; i++) {
1902+ ep_queue = &priv->qh_list[i];
1903+ list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1904+ enqueue_qtds(hcd, qh);
1905+ }
1906+}
1907+
1908+#define PTD_STATE_QTD_DONE 1
1909+#define PTD_STATE_QTD_RELOAD 2
1910+#define PTD_STATE_URB_RETIRE 3
1911+
1912+static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1913+ struct urb *urb)
1914+{
1915+ u32 dw4;
1916+ int i;
1917+
1918+ dw4 = TO_U32(ptd->dw4);
1919+ dw4 >>= 8;
1920+
1921+ /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1922+ need to handle these errors? Is it done in hardware? */
1923+ if (ptd->dw3 & DW3_HALT_BIT) {
1924+
1925+ urb->status = -EPROTO; /* Default unknown error */
1926+
1927+ for (i = 0; i < 8; i++) {
1928+ switch (dw4 & 0x7) {
1929+ case INT_UNDERRUN:
1930+ printf("underrun during uFrame %d\n", i);
1931+ urb->status = -ECOMM; /* Could not write data */
1932+ break;
1933+ case INT_EXACT:
1934+ printf("transaction error uFrame %d\n", i);
1935+ urb->status = -EPROTO; /* timeout, bad CRC, PID
1936+ error etc. */
1937+ break;
1938+ case INT_BABBLE:
1939+ printf("babble error during uFrame %d\n", i);
1940+ urb->status = -EOVERFLOW;
1941+ break;
1942+ }
1943+ dw4 >>= 3;
1944+ }
1945+
1946+ return PTD_STATE_URB_RETIRE;
1947+ }
1948+
1949+ return PTD_STATE_QTD_DONE;
1950+}
1951+
1952+static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1953+ struct urb *urb)
1954+{
1955+ WARN_ON(!ptd);
1956+ if (ptd->dw3 & DW3_HALT_BIT) {
1957+ if (ptd->dw3 & DW3_BABBLE_BIT)
1958+ urb->status = -EOVERFLOW;
1959+ else if (FROM_DW3_CERR(ptd->dw3))
1960+ urb->status = -EPIPE; /* Stall */
1961+ else
1962+ urb->status = -EPROTO; /* Unknown */
1963+
1964+ /* usefull debug
1965+ printf("%s: ptd error:\n"
1966+ " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1967+ " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1968+ __func__,
1969+ ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1970+ ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1971+ */
1972+
1973+ return PTD_STATE_URB_RETIRE;
1974+ }
1975+
1976+ /* Transfer Error, *but* active and no HALT -> reload */
1977+ if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT))
1978+ return PTD_STATE_QTD_RELOAD;
1979+
1980+ /*
1981+ * NAKs are handled in HW by the chip. Usually if the
1982+ * device is not able to send data fast enough.
1983+ * This happens mostly on slower hardware.
1984+ */
1985+ if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT))
1986+ return PTD_STATE_QTD_RELOAD;
1987+
1988+ return PTD_STATE_QTD_DONE;
1989+}
1990+
1991+static void handle_done_ptds(struct usb_hcd *hcd)
1992+{
1993+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
1994+ struct isp1760_slotinfo *slots;
1995+ struct isp1760_qtd *qtd;
1996+ struct isp1760_qh *qh;
1997+ struct ptd ptd;
1998+ u32 ptd_offset;
1999+ int modified;
2000+ int skip_map;
2001+ int state;
2002+ int slot;
2003+
2004+ skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
2005+ priv->int_done_map &= ~skip_map;
2006+ skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
2007+ priv->atl_done_map &= ~skip_map;
2008+
2009+ modified = priv->int_done_map || priv->atl_done_map;
2010+
2011+ while (priv->int_done_map || priv->atl_done_map) {
2012+ if (priv->int_done_map) {
2013+ /* INT ptd */
2014+ slot = __ffs(priv->int_done_map);
2015+ priv->int_done_map &= ~(1 << slot);
2016+ slots = priv->int_slots;
2017+ /* This should not trigger, and could be removed if
2018+ noone have any problems with it triggering: */
2019+ if (!slots[slot].qh) {
2020+ WARN_ON(1);
2021+ continue;
2022+ }
2023+ ptd_offset = INT_PTD_OFFSET;
2024+ ptd_read(hcd, INT_PTD_OFFSET, slot, &ptd);
2025+ state = check_int_transfer(hcd, &ptd,
2026+ slots[slot].qtd->urb);
2027+ } else {
2028+ /* ATL ptd */
2029+ slot = __ffs(priv->atl_done_map);
2030+ priv->atl_done_map &= ~(1 << slot);
2031+ slots = priv->atl_slots;
2032+ /* This should not trigger, and could be removed if
2033+ noone have any problems with it triggering: */
2034+ if (!slots[slot].qh) {
2035+ WARN_ON(1);
2036+ continue;
2037+ }
2038+ ptd_offset = ATL_PTD_OFFSET;
2039+ ptd_read(hcd, ATL_PTD_OFFSET, slot, &ptd);
2040+ state = check_atl_transfer(hcd, &ptd,
2041+ slots[slot].qtd->urb);
2042+ }
2043+
2044+ qtd = slots[slot].qtd;
2045+ slots[slot].qtd = NULL;
2046+ qh = slots[slot].qh;
2047+ slots[slot].qh = NULL;
2048+ qh->slot = -1;
2049+
2050+ WARN_ON(qtd->status != QTD_XFER_STARTED);
2051+
2052+ switch (state) {
2053+ case PTD_STATE_QTD_DONE:
2054+ if ((usb_pipeint(qtd->urb->pipe)) &&
2055+ (qtd->urb->dev->speed != USB_SPEED_HIGH))
2056+ qtd->actual_length =
2057+ FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
2058+ else
2059+ qtd->actual_length =
2060+ FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
2061+
2062+ qtd->status = QTD_XFER_COMPLETE;
2063+
2064+ if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
2065+ is_short_bulk(qtd))
2066+ qtd = NULL;
2067+ else
2068+ qtd = list_entry(qtd->qtd_list.next,
2069+ typeof(*qtd), qtd_list);
2070+
2071+ qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
2072+ qh->ping = FROM_DW3_PING(ptd.dw3);
2073+
2074+ break;
2075+
2076+ case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
2077+ qtd->status = QTD_PAYLOAD_ALLOC;
2078+ ptd.dw0 |= DW0_VALID_BIT;
2079+ /* RL counter = ERR counter */
2080+ ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
2081+ ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
2082+ ptd.dw3 &= ~TO_DW3_CERR(3);
2083+ ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
2084+
2085+ qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
2086+ qh->ping = FROM_DW3_PING(ptd.dw3);
2087+ break;
2088+
2089+ case PTD_STATE_URB_RETIRE:
2090+ qtd->status = QTD_RETIRE;
2091+ qtd = NULL;
2092+ qh->toggle = 0;
2093+ qh->ping = 0;
2094+ break;
2095+
2096+ default:
2097+ WARN_ON(1);
2098+ continue;
2099+ }
2100+
2101+ if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
2102+ if (slots == priv->int_slots) {
2103+ if (state == PTD_STATE_QTD_RELOAD)
2104+ dev_err(priv->dev,
2105+ "%s: PTD_STATE_QTD_RELOAD on "
2106+ "interrupt packet\n", __func__);
2107+ if (state != PTD_STATE_QTD_RELOAD)
2108+ create_ptd_int(qh, qtd, &ptd);
2109+ } else {
2110+ if (state != PTD_STATE_QTD_RELOAD)
2111+ create_ptd_atl(qh, qtd, &ptd);
2112+ }
2113+
2114+ start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
2115+ qh, &ptd);
2116+ }
2117+ }
2118+
2119+ if (modified)
2120+ schedule_ptds(hcd);
2121+}
2122+
2123+static irqreturn_t isp1760_irq(int irq, void *__hci)
2124+{
2125+ struct usb_hcd *hcd = __hci;
2126+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2127+ irqreturn_t irqret = IRQ_NONE;
2128+ u32 int_reg;
2129+ u32 imask;
2130+
2131+ imask = isp1760_hcd_read(hcd, HC_INTERRUPT);
2132+ if (unlikely(!imask))
2133+ return irqret;
2134+
2135+ int_reg = priv->is_isp1763 ? ISP1763_HC_INTERRUPT :
2136+ ISP176x_HC_INTERRUPT;
2137+ isp1760_reg_write(priv->regs, int_reg, imask);
2138+
2139+ priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
2140+ priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
2141+
2142+ handle_done_ptds(hcd);
2143+
2144+ irqret = IRQ_HANDLED;
2145+
2146+ return irqret;
2147+}
2148+
2149+static int isp1763_run(struct usb_hcd *hcd)
2150+{
2151+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2152+ int retval;
2153+ u32 chipid_h;
2154+ u32 chipid_l;
2155+ u32 chip_rev;
2156+ u32 ptd_atl_int;
2157+ u32 ptd_iso;
2158+
2159+ chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
2160+ chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
2161+ chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
2162+ printf("USB ISP %02x%02x HW rev. %d started\n", chipid_h,
2163+ chipid_l, chip_rev);
2164+
2165+ isp1760_hcd_clear(hcd, ISO_BUF_FILL);
2166+ isp1760_hcd_clear(hcd, INT_BUF_FILL);
2167+ isp1760_hcd_clear(hcd, ATL_BUF_FILL);
2168+
2169+ isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
2170+ isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
2171+ isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
2172+ ndelay(100);
2173+ isp1760_hcd_clear(hcd, HC_ATL_PTD_DONEMAP);
2174+ isp1760_hcd_clear(hcd, HC_INT_PTD_DONEMAP);
2175+ isp1760_hcd_clear(hcd, HC_ISO_PTD_DONEMAP);
2176+
2177+ isp1760_hcd_set(hcd, HW_OTG_DISABLE);
2178+ isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(7));
2179+ isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(15));
2180+ mdelay(10);
2181+
2182+ isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
2183+ isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
2184+
2185+ isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
2186+
2187+ isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
2188+ isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
2189+ isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
2190+
2191+ isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
2192+ isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
2193+ isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
2194+
2195+ ptd_atl_int = 0x8000;
2196+ ptd_iso = 0x0001;
2197+
2198+ isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
2199+ isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
2200+ isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
2201+
2202+ isp1760_hcd_set(hcd, ATL_BUF_FILL);
2203+ isp1760_hcd_set(hcd, INT_BUF_FILL);
2204+
2205+ isp1760_hcd_clear(hcd, CMD_LRESET);
2206+ isp1760_hcd_clear(hcd, CMD_RESET);
2207+
2208+ retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
2209+ if (retval)
2210+ return retval;
2211+
2212+ down_write(&ehci_cf_port_reset_rwsem);
2213+ retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
2214+ up_write(&ehci_cf_port_reset_rwsem);
2215+ retval = 0;
2216+ if (retval)
2217+ return retval;
2218+
2219+ return 0;
2220+}
2221+
2222+static int isp1760_run(struct usb_hcd *hcd)
2223+{
2224+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2225+ int retval;
2226+ u32 chipid_h;
2227+ u32 chipid_l;
2228+ u32 chip_rev;
2229+ u32 ptd_atl_int;
2230+ u32 ptd_iso;
2231+
2232+ /*
2233+ * ISP1763 have some differences in the setup and order to enable
2234+ * the ports, disable otg, setup buffers, and ATL, INT, ISO status.
2235+ * So, just handle it a separate sequence.
2236+ */
2237+ if (priv->is_isp1763)
2238+ return isp1763_run(hcd);
2239+
2240+ /* Set PTD interrupt AND & OR maps */
2241+ isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
2242+ isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
2243+ isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
2244+
2245+ isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
2246+ isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
2247+ isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
2248+
2249+ /* step 23 passed */
2250+
2251+ isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
2252+
2253+ isp1760_hcd_clear(hcd, CMD_LRESET);
2254+ isp1760_hcd_clear(hcd, CMD_RESET);
2255+
2256+ retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
2257+ if (retval)
2258+ return retval;
2259+
2260+ /*
2261+ * XXX
2262+ * Spec says to write FLAG_CF as last config action, priv code grabs
2263+ * the semaphore while doing so.
2264+ */
2265+ down_write(&ehci_cf_port_reset_rwsem);
2266+
2267+ retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
2268+ up_write(&ehci_cf_port_reset_rwsem);
2269+ if (retval)
2270+ return retval;
2271+
2272+ chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
2273+ chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
2274+ chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
2275+ dev_info(priv->dev, "USB ISP %02x%02x HW rev. %d started\n",
2276+ chipid_h, chipid_l, chip_rev);
2277+
2278+ /* PTD Register Init Part 2, Step 28 */
2279+
2280+ /* Setup registers controlling PTD checking */
2281+ ptd_atl_int = 0x80000000;
2282+ ptd_iso = 0x00000001;
2283+
2284+ isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
2285+ isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
2286+ isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
2287+
2288+ isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
2289+ isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
2290+ isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
2291+
2292+ isp1760_hcd_set(hcd, ATL_BUF_FILL);
2293+ isp1760_hcd_set(hcd, INT_BUF_FILL);
2294+
2295+ /* GRR this is run-once init(), being done every time the HC starts.
2296+ * So long as they're part of class devices, we can't do it init()
2297+ * since the class device isn't created that early.
2298+ */
2299+ return 0;
2300+}
2301+
2302+static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
2303+{
2304+ qtd->data_buffer = databuffer;
2305+
2306+ qtd->length = len;
2307+
2308+ return qtd->length;
2309+}
2310+
2311+static void qtd_list_free(struct list_head *qtd_list)
2312+{
2313+ struct isp1760_qtd *qtd, *qtd_next;
2314+
2315+ list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
2316+ list_del(&qtd->qtd_list);
2317+ qtd_free(qtd);
2318+ }
2319+}
2320+
2321+/*
2322+ * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
2323+ * Also calculate the PID type (SETUP/IN/OUT) for each packet.
2324+ */
2325+#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2326+static void packetize_urb(struct usb_hcd *hcd,
2327+ struct urb *urb, struct list_head *head, gfp_t flags)
2328+{
2329+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2330+ const struct isp1760_memory_layout *mem = priv->memory_layout;
2331+ struct isp1760_qtd *qtd;
2332+ void *buf;
2333+ int len, maxpacketsize;
2334+ u8 packet_type;
2335+
2336+ /*
2337+ * URBs map to sequences of QTDs: one logical transaction
2338+ */
2339+
2340+ if (!urb->transfer_buffer && urb->transfer_buffer_length) {
2341+ /* XXX This looks like usb storage / SCSI bug */
2342+ dev_err(priv->dev, "buf is null, dma is %08lx len is %d\n",
2343+ (long unsigned)urb->transfer_dma,
2344+ urb->transfer_buffer_length);
2345+ WARN_ON(1);
2346+ }
2347+
2348+ if (usb_pipein(urb->pipe))
2349+ packet_type = IN_PID;
2350+ else
2351+ packet_type = OUT_PID;
2352+
2353+ if (usb_pipecontrol(urb->pipe)) {
2354+ qtd = qtd_alloc(flags, urb, SETUP_PID);
2355+ if (!qtd)
2356+ goto cleanup;
2357+ qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
2358+ list_add_tail(&qtd->qtd_list, head);
2359+
2360+ /* for zero length DATA stages, STATUS is always IN */
2361+ if (urb->transfer_buffer_length == 0)
2362+ packet_type = IN_PID;
2363+ }
2364+
2365+ maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe));
2366+
2367+ /*
2368+ * buffer gets wrapped in one or more qtds;
2369+ * last one may be "short" (including zero len)
2370+ * and may serve as a control status ack
2371+ */
2372+ buf = urb->transfer_buffer;
2373+ len = urb->transfer_buffer_length;
2374+
2375+ for (;;) {
2376+ int this_qtd_len;
2377+
2378+ qtd = qtd_alloc(flags, urb, packet_type);
2379+ if (!qtd)
2380+ goto cleanup;
2381+
2382+ if (len > mem->blocks_size[ISP176x_BLOCK_NUM - 1])
2383+ len = mem->blocks_size[ISP176x_BLOCK_NUM - 1];
2384+
2385+ this_qtd_len = qtd_fill(qtd, buf, len);
2386+ list_add_tail(&qtd->qtd_list, head);
2387+
2388+ len -= this_qtd_len;
2389+ buf += this_qtd_len;
2390+
2391+ if (len <= 0)
2392+ break;
2393+ }
2394+
2395+ /*
2396+ * control requests may need a terminating data "status" ack;
2397+ * bulk ones may need a terminating short packet (zero length).
2398+ */
2399+ if (urb->transfer_buffer_length != 0) {
2400+ int one_more = 0;
2401+
2402+ if (usb_pipecontrol(urb->pipe)) {
2403+ one_more = 1;
2404+ if (packet_type == IN_PID)
2405+ packet_type = OUT_PID;
2406+ else
2407+ packet_type = IN_PID;
2408+ } else if (usb_pipebulk(urb->pipe)
2409+ && (urb->transfer_flags & URB_ZERO_PACKET)
2410+ && !(urb->transfer_buffer_length %
2411+ maxpacketsize)) {
2412+ one_more = 1;
2413+ }
2414+ if (one_more) {
2415+ qtd = qtd_alloc(flags, urb, packet_type);
2416+ if (!qtd)
2417+ goto cleanup;
2418+
2419+ /* never any data in such packets */
2420+ qtd_fill(qtd, NULL, 0);
2421+ list_add_tail(&qtd->qtd_list, head);
2422+ }
2423+ }
2424+
2425+ return;
2426+
2427+cleanup:
2428+ qtd_list_free(head);
2429+}
2430+
2431+static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2432+ gfp_t mem_flags)
2433+{
2434+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2435+ struct isp1760_qh *qh = NULL;
2436+ struct list_head *ep_queue;
2437+ LIST_HEAD(new_qtds);
2438+ int qh_in_queue;
2439+ int retval;
2440+ int epnum;
2441+
2442+ switch (usb_pipetype(urb->pipe)) {
2443+ case PIPE_CONTROL:
2444+ ep_queue = &priv->qh_list[QH_CONTROL];
2445+ break;
2446+ case PIPE_BULK:
2447+ ep_queue = &priv->qh_list[QH_BULK];
2448+ break;
2449+ case PIPE_INTERRUPT:
2450+ ep_queue = &priv->qh_list[QH_INTERRUPT];
2451+ break;
2452+ case PIPE_ISOCHRONOUS:
2453+ printf("isochronous USB packets not yet supported\n");
2454+ return -EPIPE;
2455+ default:
2456+ printf("unknown pipe type\n");
2457+ return -EPIPE;
2458+ }
2459+
2460+ if (usb_pipein(urb->pipe))
2461+ urb->actual_length = 0;
2462+
2463+ packetize_urb(hcd, urb, &new_qtds, mem_flags);
2464+ if (list_empty(&new_qtds))
2465+ return -ENOMEM;
2466+
2467+ retval = usb_hcd_link_urb_to_ep(hcd, urb);
2468+ if (retval) {
2469+ qtd_list_free(&new_qtds);
2470+ goto out;
2471+ }
2472+
2473+ epnum = usb_pipeendpoint(urb->pipe);
2474+
2475+ qh_in_queue = 0;
2476+ list_for_each_entry(qh, ep_queue, qh_list) {
2477+ if (qh->epnum == epnum) {
2478+ qh_in_queue = 1;
2479+ break;
2480+ }
2481+ }
2482+
2483+ if (!qh_in_queue) {
2484+ qh = qh_alloc(GFP_ATOMIC);
2485+ if (!qh) {
2486+ retval = -ENOMEM;
2487+ usb_hcd_unlink_urb_from_ep(hcd, urb);
2488+ qtd_list_free(&new_qtds);
2489+ goto out;
2490+ }
2491+
2492+ qh->epnum = epnum;
2493+ list_add_tail(&qh->qh_list, ep_queue);
2494+ urb->ep->hcpriv = qh;
2495+ }
2496+
2497+ list_splice_tail(&new_qtds, &qh->qtd_list);
2498+ schedule_ptds(hcd);
2499+
2500+out:
2501+ return retval;
2502+}
2503+
2504+static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
2505+ struct isp1760_qh *qh)
2506+{
2507+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2508+ int skip_map;
2509+
2510+ WARN_ON(qh->slot == -1);
2511+
2512+ /* We need to forcefully reclaim the slot since some transfers never
2513+ return, e.g. interrupt transfers and NAKed bulk transfers. */
2514+ if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
2515+ skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
2516+ skip_map |= (1 << qh->slot);
2517+ isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
2518+ ndelay(100);
2519+ priv->atl_slots[qh->slot].qh = NULL;
2520+ priv->atl_slots[qh->slot].qtd = NULL;
2521+ } else {
2522+ skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
2523+ skip_map |= (1 << qh->slot);
2524+ isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
2525+ priv->int_slots[qh->slot].qh = NULL;
2526+ priv->int_slots[qh->slot].qtd = NULL;
2527+ }
2528+
2529+ qh->slot = -1;
2530+}
2531+
2532+/*
2533+ * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
2534+ * any active transfer belonging to the urb in the process.
2535+ */
2536+static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
2537+ struct isp1760_qtd *qtd)
2538+{
2539+ struct urb *urb;
2540+ int urb_was_running;
2541+
2542+ urb = qtd->urb;
2543+ urb_was_running = 0;
2544+ list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
2545+ if (qtd->urb != urb)
2546+ break;
2547+
2548+ if (qtd->status >= QTD_XFER_STARTED)
2549+ urb_was_running = 1;
2550+ if (last_qtd_of_urb(qtd, qh) &&
2551+ (qtd->status >= QTD_XFER_COMPLETE))
2552+ urb_was_running = 0;
2553+
2554+ if (qtd->status == QTD_XFER_STARTED)
2555+ kill_transfer(hcd, urb, qh);
2556+ qtd->status = QTD_RETIRE;
2557+ }
2558+}
2559+
2560+int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2561+{
2562+ struct isp1760_qtd *qtd;
2563+ struct isp1760_qh *qh;
2564+ int retval = 0;
2565+
2566+ retval = usb_hcd_check_unlink_urb(hcd, urb, status);
2567+ if (retval)
2568+ goto out;
2569+
2570+ qh = urb->ep->hcpriv;
2571+ if (!qh) {
2572+ retval = -EINVAL;
2573+ goto out;
2574+ }
2575+
2576+ list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
2577+ if (qtd->urb == urb) {
2578+ dequeue_urb_from_qtd(hcd, qh, qtd);
2579+ list_move(&qtd->qtd_list, &qh->qtd_list);
2580+ break;
2581+ }
2582+
2583+ urb->status = status;
2584+ schedule_ptds(hcd);
2585+
2586+out:
2587+ return retval;
2588+}
2589+
2590+static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
2591+ struct usb_hub_descriptor *desc)
2592+{
2593+ int ports;
2594+ u16 temp;
2595+
2596+ ports = isp1760_hcd_n_ports(priv->hcd);
2597+
2598+ desc->bDescriptorType = USB_DT_HUB;
2599+ /* priv 1.0, 2.3.9 says 20ms max */
2600+ desc->bPwrOn2PwrGood = 10;
2601+ desc->bHubContrCurrent = 0;
2602+
2603+ desc->bNbrPorts = ports;
2604+ temp = 1 + (ports / 8);
2605+ desc->bLength = 7 + 2 * temp;
2606+
2607+ /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
2608+ memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
2609+ memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
2610+
2611+ /* per-port overcurrent reporting */
2612+ temp = HUB_CHAR_INDV_PORT_OCPM;
2613+ if (isp1760_hcd_ppc_is_set(priv->hcd))
2614+ /* per-port power control */
2615+ temp |= HUB_CHAR_INDV_PORT_LPSM;
2616+ else
2617+ /* no power switching */
2618+ temp |= HUB_CHAR_NO_LPSM;
2619+ desc->wHubCharacteristics = cpu_to_le16(temp);
2620+}
2621+
2622+#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
2623+
2624+static void check_reset_complete(struct usb_hcd *hcd, int index)
2625+{
2626+ if (!(isp1760_hcd_is_set(hcd, PORT_CONNECT)))
2627+ return;
2628+
2629+ /* if reset finished and it's still not enabled -- handoff */
2630+ if (!isp1760_hcd_is_set(hcd, PORT_PE)) {
2631+ printf("port %d full speed --> companion\n", index + 1);
2632+
2633+ isp1760_hcd_set(hcd, PORT_OWNER);
2634+
2635+ isp1760_hcd_clear(hcd, PORT_CSC);
2636+ } else {
2637+ printf("port %d high speed\n", index + 1);
2638+ }
2639+
2640+ return;
2641+}
2642+
2643+static int isp1760_hub_control(struct usb_hcd *hcd, struct usb_device *dev,
2644+ unsigned long pipe, void *buffer, int length,
2645+ struct devrequest *setup)
2646+{
2647+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2648+ u16 typeReq, wValue, wIndex;
2649+ unsigned long flags;
2650+ char *buf = buffer;
2651+ void *src = NULL;
2652+ int src_len = 0;
2653+ int retval = 0;
2654+ u32 status;
2655+ int ports;
2656+
2657+ if (!setup)
2658+ return -EINVAL;
2659+
2660+ ports = isp1760_hcd_n_ports(hcd);
2661+
2662+ typeReq = setup->request | (setup->requesttype << 8);
2663+ wValue = le16_to_cpu(setup->value);
2664+ wIndex = le16_to_cpu(setup->index);
2665+
2666+ /*
2667+ * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
2668+ * HCS_INDICATOR may say we can change LEDs to off/amber/green.
2669+ * (track current state ourselves) ... blink for diagnostics,
2670+ * power, "this is the one", etc. EHCI spec supports this.
2671+ */
2672+
2673+ switch (typeReq) {
2674+ case DeviceOutRequest | USB_REQ_SET_ADDRESS:
2675+ break;
2676+ case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
2677+ /* Nothing to do */
2678+ break;
2679+ case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2680+ switch (wValue & 0xff00) {
2681+ case USB_DT_DEVICE << 8:
2682+ src = &rh_descriptor.device;
2683+ src_len = 0x12;
2684+ break;
2685+ case USB_DT_CONFIG << 8:
2686+ src = &rh_descriptor.config;
2687+ src_len = 0x09;
2688+ break;
2689+ case USB_DT_STRING << 8:
2690+ switch (wValue & 0xff) {
2691+ case 0: /* Language */
2692+ src = "\4\3\19\4";
2693+ src_len = 4;
2694+ break;
2695+ case 1: /* Vendor String */
2696+ src = "\16\3U\0-\0B\0o\0o\0t\0";
2697+ src_len = 14;
2698+ break;
2699+ case 2: /* Product Name */
2700+ src = "\52\3I\0S\0P\0-\0 "
2701+ "\0H\0o\0s\0t\0 "
2702+ "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
2703+ src_len = 42;
2704+ break;
2705+ default:
2706+ goto error;
2707+ }
2708+ break;
2709+ }
2710+ break;
2711+ case ClearHubFeature:
2712+ switch (wValue) {
2713+ case C_HUB_LOCAL_POWER:
2714+ case C_HUB_OVER_CURRENT:
2715+ /* no hub-wide feature/status flags */
2716+ break;
2717+ default:
2718+ goto error;
2719+ }
2720+ break;
2721+ case ClearPortFeature:
2722+ if (!wIndex || wIndex > ports)
2723+ goto error;
2724+ wIndex--;
2725+
2726+ /*
2727+ * Even if OWNER is set, so the port is owned by the
2728+ * companion controller, hub_wq needs to be able to clear
2729+ * the port-change status bits (especially
2730+ * USB_PORT_STAT_C_CONNECTION).
2731+ */
2732+
2733+ switch (wValue) {
2734+ case USB_PORT_FEAT_ENABLE:
2735+ isp1760_hcd_clear(hcd, PORT_PE);
2736+ break;
2737+ case USB_PORT_FEAT_C_ENABLE:
2738+ /* XXX error? */
2739+ break;
2740+ case USB_PORT_FEAT_SUSPEND:
2741+ if (isp1760_hcd_is_set(hcd, PORT_RESET))
2742+ goto error;
2743+
2744+ if (isp1760_hcd_is_set(hcd, PORT_SUSPEND)) {
2745+ if (!isp1760_hcd_is_set(hcd, PORT_PE))
2746+ goto error;
2747+ /* resume signaling for 20 msec */
2748+ isp1760_hcd_clear(hcd, PORT_CSC);
2749+ isp1760_hcd_set(hcd, PORT_RESUME);
2750+
2751+ priv->reset_done = get_timer(0) + 40;
2752+ }
2753+ break;
2754+ case USB_PORT_FEAT_C_SUSPEND:
2755+ /* we auto-clear this feature */
2756+ break;
2757+ case USB_PORT_FEAT_POWER:
2758+ if (isp1760_hcd_ppc_is_set(hcd))
2759+ isp1760_hcd_clear(hcd, PORT_POWER);
2760+ break;
2761+ case USB_PORT_FEAT_C_CONNECTION:
2762+ isp1760_hcd_set(hcd, PORT_CSC);
2763+ break;
2764+ case USB_PORT_FEAT_C_OVER_CURRENT:
2765+ /* XXX error ?*/
2766+ break;
2767+ case USB_PORT_FEAT_C_RESET:
2768+ /* GetPortStatus clears reset */
2769+ break;
2770+ default:
2771+ goto error;
2772+ }
2773+ isp1760_hcd_read(hcd, CMD_RUN);
2774+ break;
2775+ case GetHubDescriptor:
2776+ isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *) buf);
2777+ break;
2778+ case GetHubStatus:
2779+ /* no hub-wide feature/status flags */
2780+ memset(buf, 0, 4);
2781+ break;
2782+ case GetPortStatus:
2783+ if (!wIndex || wIndex > ports)
2784+ goto error;
2785+ wIndex--;
2786+ status = 0;
2787+
2788+ /* wPortChange bits */
2789+ if (isp1760_hcd_is_set(hcd, PORT_CSC))
2790+ status |= USB_PORT_STAT_C_CONNECTION << 16;
2791+
2792+ /* whoever resumes must GetPortStatus to complete it!! */
2793+ if (isp1760_hcd_is_set(hcd, PORT_RESUME)) {
2794+ status |= USB_PORT_STAT_C_SUSPEND << 16;
2795+
2796+ if (!priv->reset_done) {
2797+ priv->reset_done = get_timer(0) + 20;
2798+ } else if (get_timer(0) > priv->reset_done) {
2799+ /* stop resume signaling */
2800+ isp1760_hcd_clear(hcd, PORT_CSC);
2801+
2802+ retval = isp1760_hcd_clear_and_wait(hcd,
2803+ PORT_RESUME, 2000);
2804+ if (retval != 0) {
2805+ printf("port %d resume error %d\n",
2806+ wIndex + 1, retval);
2807+ goto error;
2808+ }
2809+ }
2810+ }
2811+
2812+ /* whoever resets must GetPortStatus to complete it!! */
2813+ if (isp1760_hcd_is_set(hcd, PORT_RESET) &&
2814+ get_timer(0) > priv->reset_done) {
2815+ status |= USB_PORT_STAT_C_RESET << 16;
2816+ priv->reset_done = 0;
2817+
2818+ /* force reset to complete */
2819+ /* REVISIT: some hardware needs 550+ usec to clear
2820+ * this bit; seems too long to spin routinely...
2821+ */
2822+ retval = isp1760_hcd_clear_and_wait(hcd, PORT_RESET,
2823+ 750);
2824+ if (retval != 0) {
2825+ printf("port %d reset error %d\n", wIndex + 1,
2826+ retval);
2827+ goto error;
2828+ }
2829+
2830+ /* see what we found out */
2831+ check_reset_complete(hcd, wIndex);
2832+ }
2833+ /*
2834+ * Even if OWNER is set, there's no harm letting hub_wq
2835+ * see the wPortStatus values (they should all be 0 except
2836+ * for PORT_POWER anyway).
2837+ */
2838+
2839+ if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2840+ printf("PORT_OWNER is set\n");
2841+
2842+ if (isp1760_hcd_is_set(hcd, PORT_CONNECT)) {
2843+ status |= USB_PORT_STAT_CONNECTION;
2844+
2845+ /* status may be from integrated TT */
2846+ status |= USB_PORT_STAT_HIGH_SPEED;
2847+ }
2848+ if (isp1760_hcd_is_set(hcd, PORT_PE))
2849+ status |= USB_PORT_STAT_ENABLE;
2850+ if (isp1760_hcd_is_set(hcd, PORT_SUSPEND) &&
2851+ isp1760_hcd_is_set(hcd, PORT_RESUME))
2852+ status |= USB_PORT_STAT_SUSPEND;
2853+ if (isp1760_hcd_is_set(hcd, PORT_RESET))
2854+ status |= USB_PORT_STAT_RESET;
2855+ if (isp1760_hcd_is_set(hcd, PORT_POWER))
2856+ status |= USB_PORT_STAT_POWER;
2857+
2858+ put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2859+ break;
2860+ case SetHubFeature:
2861+ switch (wValue) {
2862+ case C_HUB_LOCAL_POWER:
2863+ case C_HUB_OVER_CURRENT:
2864+ /* no hub-wide feature/status flags */
2865+ break;
2866+ default:
2867+ goto error;
2868+ }
2869+ break;
2870+ case SetPortFeature:
2871+ wIndex &= 0xff;
2872+ if (!wIndex || wIndex > ports)
2873+ goto error;
2874+ wIndex--;
2875+
2876+ if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2877+ break;
2878+
2879+ switch (wValue) {
2880+ case USB_PORT_FEAT_ENABLE:
2881+ isp1760_hcd_set(hcd, PORT_PE);
2882+ break;
2883+
2884+ case USB_PORT_FEAT_SUSPEND:
2885+ if (!isp1760_hcd_is_set(hcd, PORT_PE) ||
2886+ isp1760_hcd_is_set(hcd, PORT_RESET))
2887+ goto error;
2888+
2889+ isp1760_hcd_set(hcd, PORT_SUSPEND);
2890+ break;
2891+ case USB_PORT_FEAT_POWER:
2892+ if (isp1760_hcd_ppc_is_set(hcd))
2893+ isp1760_hcd_set(hcd, PORT_POWER);
2894+ break;
2895+ case USB_PORT_FEAT_RESET:
2896+ if (isp1760_hcd_is_set(hcd, PORT_RESUME))
2897+ goto error;
2898+ /* line status bits may report this as low speed,
2899+ * which can be fine if this root hub has a
2900+ * transaction translator built in.
2901+ */
2902+ if ((isp1760_hcd_is_set(hcd, PORT_CONNECT) &&
2903+ !isp1760_hcd_is_set(hcd, PORT_PE)) &&
2904+ (isp1760_hcd_read(hcd, PORT_LSTATUS) == 1)) {
2905+ isp1760_hcd_set(hcd, PORT_OWNER);
2906+ } else {
2907+ isp1760_hcd_set(hcd, PORT_RESET);
2908+ isp1760_hcd_clear(hcd, PORT_PE);
2909+
2910+ priv->reset_done = get_timer(0) + 50;
2911+ }
2912+ break;
2913+ default:
2914+ goto error;
2915+ }
2916+ break;
2917+
2918+ default:
2919+ printf("root: unknown request: 0x%0x\n", typeReq);
2920+ goto error;
2921+ }
2922+ spin_unlock_irqrestore(&priv->lock, flags);
2923+
2924+ if (src_len) {
2925+ length = min(src_len, length);
2926+
2927+ if (src != NULL && length > 0)
2928+ memcpy(buffer, src, length);
2929+ else
2930+ printf("zero copy USB descriptor\n");
2931+ }
2932+
2933+ dev->act_len = length;
2934+ dev->status = 0;
2935+
2936+ return 0;
2937+
2938+error:
2939+ /* "stall" on error */
2940+ dev->act_len = 0;
2941+ dev->status = USB_ST_STALLED;
2942+ return -EPIPE;
2943+}
2944+
2945+#ifndef __UBOOT__
2946+static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
2947+{
2948+ u32 status = 0;
2949+ int retval = 1;
2950+
2951+ /* init status to no-changes */
2952+ buf[0] = 0;
2953+
2954+ if (isp1760_hcd_is_set(hcd, PORT_OWNER) &&
2955+ isp1760_hcd_is_set(hcd, PORT_CSC)) {
2956+ isp1760_hcd_clear(hcd, PORT_CSC);
2957+ goto done;
2958+ }
2959+
2960+done:
2961+ return status ? retval : 0;
2962+}
2963+
2964+static void isp1760_endpoint_disable(struct usb_hcd *hcd,
2965+ struct usb_host_endpoint *ep)
2966+{
2967+ struct isp1760_qh *qh, *qh_iter;
2968+ unsigned long spinflags;
2969+ int i;
2970+
2971+ qh = ep->hcpriv;
2972+ if (!qh)
2973+ return;
2974+
2975+ WARN_ON(!list_empty(&qh->qtd_list));
2976+
2977+ for (i = 0; i < QH_END; i++)
2978+ list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
2979+ if (qh_iter == qh) {
2980+ list_del(&qh_iter->qh_list);
2981+ i = QH_END;
2982+ break;
2983+ }
2984+ qh_free(qh);
2985+ ep->hcpriv = NULL;
2986+
2987+ schedule_ptds(hcd);
2988+}
2989+
2990+static int isp1760_get_frame(struct usb_hcd *hcd)
2991+{
2992+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
2993+ u32 fr;
2994+
2995+ fr = isp1760_hcd_read(hcd, HC_FRINDEX);
2996+ return (fr >> 3) % priv->periodic_size;
2997+}
2998+
2999+static void isp1760_stop(struct usb_hcd *hcd)
3000+{
3001+ struct isp1760_hcd *priv = hcd_to_priv(hcd);
3002+
3003+ msleep(20);
3004+
3005+ spin_lock_irq(&priv->lock);
3006+ ehci_reset(hcd);
3007+ /* Disable IRQ */
3008+ isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
3009+ spin_unlock_irq(&priv->lock);
3010+
3011+ isp1760_hcd_clear(hcd, FLAG_CF);
3012+}
3013+
3014+static void isp1760_shutdown(struct usb_hcd *hcd)
3015+{
3016+ isp1760_stop(hcd);
3017+
3018+ isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
3019+
3020+ isp1760_hcd_clear(hcd, CMD_RUN);
3021+}
3022+
3023+static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
3024+ struct usb_host_endpoint *ep)
3025+{
3026+ struct isp1760_qh *qh = ep->hcpriv;
3027+ unsigned long spinflags;
3028+
3029+ if (!qh)
3030+ return;
3031+
3032+ qh->tt_buffer_dirty = 0;
3033+ schedule_ptds(hcd);
3034+}
3035+
3036+
3037+static const struct hc_driver isp1760_hc_driver = {
3038+ .description = "isp1760-hcd",
3039+ .product_desc = "NXP ISP1760 USB Host Controller",
3040+ .hcd_priv_size = sizeof(struct isp1760_hcd *),
3041+ .irq = isp1760_irq,
3042+ .flags = HCD_MEMORY | HCD_USB2,
3043+ .reset = isp1760_hc_setup,
3044+ .start = isp1760_run,
3045+ .stop = isp1760_stop,
3046+ .shutdown = isp1760_shutdown,
3047+ .urb_enqueue = isp1760_urb_enqueue,
3048+ .urb_dequeue = isp1760_urb_dequeue,
3049+ .endpoint_disable = isp1760_endpoint_disable,
3050+ .get_frame_number = isp1760_get_frame,
3051+ .hub_status_data = isp1760_hub_status_data,
3052+ .hub_control = isp1760_hub_control,
3053+ .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
3054+};
3055+#endif // __UBOOT__
3056+
3057+int __init isp1760_init_kmem_once(void)
3058+{
3059+ urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
3060+ sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
3061+ SLAB_MEM_SPREAD, NULL);
3062+
3063+ if (!urb_listitem_cachep)
3064+ return -ENOMEM;
3065+
3066+ qtd_cachep = kmem_cache_create("isp1760_qtd",
3067+ sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
3068+ SLAB_MEM_SPREAD, NULL);
3069+
3070+ if (!qtd_cachep)
3071+ return -ENOMEM;
3072+
3073+ qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
3074+ 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
3075+
3076+ if (!qh_cachep) {
3077+ kmem_cache_destroy(qtd_cachep);
3078+ return -ENOMEM;
3079+ }
3080+
3081+ return 0;
3082+}
3083+
3084+void isp1760_deinit_kmem_cache(void)
3085+{
3086+ kmem_cache_destroy(qtd_cachep);
3087+ kmem_cache_destroy(qh_cachep);
3088+ kmem_cache_destroy(urb_listitem_cachep);
3089+}
3090+
3091+int isp1760_hcd_lowlevel_init(struct isp1760_hcd *priv)
3092+{
3093+ int ret;
3094+
3095+ ret = isp1760_hc_setup(priv->hcd);
3096+ if (ret < 0)
3097+ return ret;
3098+
3099+ ret = isp1760_run(priv->hcd);
3100+ if (ret < 0)
3101+ return ret;
3102+
3103+ return 0;
3104+}
3105+
3106+static const struct usb_urb_ops isp1760_urb_ops = {
3107+ .urb_enqueue = isp1760_urb_enqueue,
3108+ .urb_dequeue = isp1760_urb_dequeue,
3109+ .hub_control = isp1760_hub_control,
3110+ .isr = isp1760_irq,
3111+};
3112+
3113+int isp1760_hcd_register(struct isp1760_hcd *priv, struct resource *mem,
3114+ int irq, unsigned long irqflags,
3115+ struct udevice *dev)
3116+{
3117+ const struct isp1760_memory_layout *mem_layout = priv->memory_layout;
3118+ struct isp1760_host_data *host = dev_get_priv(dev);
3119+ struct usb_hcd *hcd = &host->hcd;
3120+ int ret;
3121+
3122+ priv->hcd = hcd;
3123+
3124+ hcd->hcd_priv = priv;
3125+
3126+ priv->hcd = hcd;
3127+
3128+ hcd->urb_ops = &isp1760_urb_ops;
3129+
3130+ priv->atl_slots = kcalloc(mem_layout->slot_num,
3131+ sizeof(struct isp1760_slotinfo), GFP_KERNEL);
3132+ if (!priv->atl_slots)
3133+ return -ENOMEM;
3134+
3135+ priv->int_slots = kcalloc(mem_layout->slot_num,
3136+ sizeof(struct isp1760_slotinfo), GFP_KERNEL);
3137+ if (!priv->int_slots) {
3138+ ret = -ENOMEM;
3139+ goto free_atl_slots;
3140+ }
3141+
3142+ host->host_speed = USB_SPEED_HIGH;
3143+
3144+ init_memory(priv);
3145+
3146+ return 0;
3147+
3148+free_atl_slots:
3149+ kfree(priv->atl_slots);
3150+
3151+ return ret;
3152+}
3153+
3154+void isp1760_hcd_unregister(struct isp1760_hcd *priv)
3155+{
3156+ struct isp1760_qh *qh, *qh_next;
3157+ int i;
3158+
3159+ for (i = 0; i < QH_END; i++)
3160+ list_for_each_entry_safe(qh, qh_next, &priv->qh_list[i],
3161+ qh_list) {
3162+ qtd_list_free(&qh->qtd_list);
3163+ list_del(&qh->qh_list);
3164+ qh_free(qh);
3165+ }
3166+
3167+ kfree(priv->atl_slots);
3168+ kfree(priv->int_slots);
3169+}
3170diff --git a/drivers/usb/isp1760/isp1760-hcd.h b/drivers/usb/isp1760/isp1760-hcd.h
3171new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05003172index 0000000000..00f5ca8c1f
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003173--- /dev/null
3174+++ b/drivers/usb/isp1760/isp1760-hcd.h
3175@@ -0,0 +1,82 @@
3176+/* SPDX-License-Identifier: GPL-2.0 */
3177+#ifndef _ISP1760_HCD_H_
3178+#define _ISP1760_HCD_H_
3179+
3180+#include <regmap.h>
3181+
3182+#include "isp1760-regs.h"
3183+
3184+struct isp1760_qh;
3185+struct isp1760_qtd;
3186+struct resource;
3187+struct usb_hcd;
3188+
3189+struct isp1760_slotinfo {
3190+ struct isp1760_qh *qh;
3191+ struct isp1760_qtd *qtd;
3192+ unsigned long timestamp;
3193+};
3194+
3195+/* chip memory management */
3196+#define ISP176x_BLOCK_MAX (32 + 20 + 4)
3197+#define ISP176x_BLOCK_NUM 3
3198+
3199+struct isp1760_memory_layout {
3200+ unsigned int blocks[ISP176x_BLOCK_NUM];
3201+ unsigned int blocks_size[ISP176x_BLOCK_NUM];
3202+
3203+ unsigned int slot_num;
3204+ unsigned int payload_blocks;
3205+ unsigned int payload_area_size;
3206+};
3207+
3208+struct isp1760_memory_chunk {
3209+ unsigned int start;
3210+ unsigned int size;
3211+ unsigned int free;
3212+};
3213+
3214+enum isp1760_queue_head_types {
3215+ QH_CONTROL,
3216+ QH_BULK,
3217+ QH_INTERRUPT,
3218+ QH_END
3219+};
3220+
3221+struct isp1760_hcd {
3222+ struct usb_hcd *hcd;
3223+ struct udevice *dev;
3224+
3225+ void __iomem *base;
3226+
3227+ struct regmap *regs;
3228+ struct regmap_field *fields[HC_FIELD_MAX];
3229+
3230+ bool is_isp1763;
3231+ const struct isp1760_memory_layout *memory_layout;
3232+
3233+ spinlock_t lock;
3234+ struct isp1760_slotinfo *atl_slots;
3235+ int atl_done_map;
3236+ struct isp1760_slotinfo *int_slots;
3237+ int int_done_map;
3238+ struct isp1760_memory_chunk memory_pool[ISP176x_BLOCK_MAX];
3239+ struct list_head qh_list[QH_END];
3240+
3241+ /* periodic schedule support */
3242+#define DEFAULT_I_TDPS 1024
3243+ unsigned periodic_size;
3244+ unsigned i_thresh;
3245+ unsigned long reset_done;
3246+ unsigned long next_statechange;
3247+};
3248+
3249+int isp1760_hcd_register(struct isp1760_hcd *priv, struct resource *mem,
3250+ int irq, unsigned long irqflags, struct udevice *dev);
3251+void isp1760_hcd_unregister(struct isp1760_hcd *priv);
3252+int isp1760_hcd_lowlevel_init(struct isp1760_hcd *priv);
3253+
3254+int isp1760_init_kmem_once(void);
3255+void isp1760_deinit_kmem_cache(void);
3256+
3257+#endif /* _ISP1760_HCD_H_ */
3258diff --git a/drivers/usb/isp1760/isp1760-if.c b/drivers/usb/isp1760/isp1760-if.c
3259new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05003260index 0000000000..c610da6b23
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003261--- /dev/null
3262+++ b/drivers/usb/isp1760/isp1760-if.c
3263@@ -0,0 +1,127 @@
3264+// SPDX-License-Identifier: GPL-2.0
3265+/*
3266+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
3267+ *
3268+ * based on original code from:
3269+ * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
3270+ */
3271+
3272+#include <common.h>
3273+#include <dm.h>
3274+#include <dm/device-internal.h>
3275+#include <dm/device_compat.h>
3276+#include <dm/devres.h>
3277+#include <dm/lists.h>
3278+#include <linux/bug.h>
3279+#include <linux/io.h>
3280+#include <linux/kernel.h>
3281+#include <linux/usb/otg.h>
3282+#include <log.h>
3283+#include <usb.h>
3284+
3285+#include "isp1760-core.h"
3286+#include "isp1760-regs.h"
3287+#include "isp1760-uboot.h"
3288+
3289+
3290+static int isp1760_of_to_plat(struct udevice *dev)
3291+{
3292+ struct isp1760_device *isp = dev_get_plat(dev);
3293+ unsigned int devflags = 0;
3294+ u32 bus_width = 0;
3295+ ofnode dp;
3296+
3297+
3298+ if (!dev_has_ofnode(dev)) {
3299+ /* select isp1763 as the default device */
3300+ devflags = ISP1760_FLAG_ISP1763 | ISP1760_FLAG_BUS_WIDTH_16;
3301+ pr_err("isp1760: no platform data\n");
3302+ goto isp_setup;
3303+ }
3304+
3305+ dp = dev_ofnode(dev);
3306+
3307+ if (ofnode_device_is_compatible(dp, "nxp,usb-isp1761"))
3308+ devflags |= ISP1760_FLAG_ISP1761;
3309+
3310+ if (ofnode_device_is_compatible(dp, "nxp,usb-isp1763"))
3311+ devflags |= ISP1760_FLAG_ISP1763;
3312+
3313+ /*
3314+ * Some systems wire up only 8 of 16 data lines or
3315+ * 16 of the 32 data lines
3316+ */
3317+ bus_width = ofnode_read_u32_default(dp, "bus-width", 16);
3318+ if (bus_width == 16)
3319+ devflags |= ISP1760_FLAG_BUS_WIDTH_16;
3320+ else if (bus_width == 8)
3321+ devflags |= ISP1760_FLAG_BUS_WIDTH_8;
3322+
3323+ if (usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_PERIPHERAL)
3324+ devflags |= ISP1760_FLAG_PERIPHERAL_EN;
3325+
3326+ if (ofnode_read_bool(dp, "analog-oc"))
3327+ devflags |= ISP1760_FLAG_ANALOG_OC;
3328+
3329+ if (ofnode_read_bool(dp, "dack-polarity"))
3330+ devflags |= ISP1760_FLAG_DACK_POL_HIGH;
3331+
3332+ if (ofnode_read_bool(dp, "dreq-polarity"))
3333+ devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
3334+
3335+isp_setup:
3336+ isp->devflags = devflags;
3337+ isp->dev = dev;
3338+
3339+ return 0;
3340+}
3341+
3342+static int isp1760_plat_probe(struct udevice *dev)
3343+{
3344+ struct isp1760_device *isp = dev_get_plat(dev);
3345+ struct resource mem_res;
3346+ struct resource irq_res;
3347+ int ret;
3348+
3349+ dev_read_resource(dev, 0, &mem_res);
3350+ dev_read_resource(dev, 1, &irq_res);
3351+
3352+ isp1760_init_kmem_once();
3353+
3354+ ret = isp1760_register(isp, &mem_res, irq_res.start, irq_res.flags);
3355+ if (ret < 0) {
3356+ isp1760_deinit_kmem_cache();
3357+ return ret;
3358+ }
3359+
3360+ return 0;
3361+}
3362+
3363+static int isp1760_plat_remove(struct udevice *dev)
3364+{
3365+ struct isp1760_device *isp = dev_get_plat(dev);
3366+
3367+ isp1760_deinit_kmem_cache();
3368+ isp1760_unregister(isp);
3369+
3370+ return 0;
3371+}
3372+
3373+static const struct udevice_id isp1760_ids[] = {
3374+ { .compatible = "nxp,usb-isp1760", },
3375+ { .compatible = "nxp,usb-isp1761", },
3376+ { .compatible = "nxp,usb-isp1763", },
3377+ { },
3378+};
3379+
3380+U_BOOT_DRIVER(isp1760) = {
3381+ .name = "isp1760",
3382+ .id = UCLASS_USB,
3383+ .of_match = isp1760_ids,
3384+ .of_to_plat = isp1760_of_to_plat,
3385+ .ops = &isp1760_usb_ops,
3386+ .probe = isp1760_plat_probe,
3387+ .remove = isp1760_plat_remove,
3388+ .plat_auto = sizeof(struct isp1760_device),
3389+ .priv_auto = sizeof(struct isp1760_host_data),
3390+};
3391diff --git a/drivers/usb/isp1760/isp1760-regs.h b/drivers/usb/isp1760/isp1760-regs.h
3392new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05003393index 0000000000..94ea60c20b
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003394--- /dev/null
3395+++ b/drivers/usb/isp1760/isp1760-regs.h
3396@@ -0,0 +1,292 @@
3397+/* SPDX-License-Identifier: GPL-2.0 */
3398+/*
3399+ * Driver for the NXP ISP1760 chip
3400+ *
3401+ * Copyright 2021 Linaro, Rui Miguel Silva
3402+ * Copyright 2014 Laurent Pinchart
3403+ * Copyright 2007 Sebastian Siewior
3404+ *
3405+ * Contacts:
3406+ * Sebastian Siewior <bigeasy@linutronix.de>
3407+ * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3408+ * Rui Miguel Silva <rui.silva@linaro.org>
3409+ */
3410+
3411+#ifndef _ISP176x_REGS_H_
3412+#define _ISP176x_REGS_H_
3413+
3414+/* -----------------------------------------------------------------------------
3415+ * Host Controller
3416+ */
3417+
3418+/* ISP1760/31 */
3419+/* EHCI capability registers */
3420+#define ISP176x_HC_VERSION 0x002
3421+#define ISP176x_HC_HCSPARAMS 0x004
3422+#define ISP176x_HC_HCCPARAMS 0x008
3423+
3424+/* EHCI operational registers */
3425+#define ISP176x_HC_USBCMD 0x020
3426+#define ISP176x_HC_USBSTS 0x024
3427+#define ISP176x_HC_FRINDEX 0x02c
3428+
3429+#define ISP176x_HC_CONFIGFLAG 0x060
3430+#define ISP176x_HC_PORTSC1 0x064
3431+
3432+#define ISP176x_HC_ISO_PTD_DONEMAP 0x130
3433+#define ISP176x_HC_ISO_PTD_SKIPMAP 0x134
3434+#define ISP176x_HC_ISO_PTD_LASTPTD 0x138
3435+#define ISP176x_HC_INT_PTD_DONEMAP 0x140
3436+#define ISP176x_HC_INT_PTD_SKIPMAP 0x144
3437+#define ISP176x_HC_INT_PTD_LASTPTD 0x148
3438+#define ISP176x_HC_ATL_PTD_DONEMAP 0x150
3439+#define ISP176x_HC_ATL_PTD_SKIPMAP 0x154
3440+#define ISP176x_HC_ATL_PTD_LASTPTD 0x158
3441+
3442+/* Configuration Register */
3443+#define ISP176x_HC_HW_MODE_CTRL 0x300
3444+#define ISP176x_HC_CHIP_ID 0x304
3445+#define ISP176x_HC_SCRATCH 0x308
3446+#define ISP176x_HC_RESET 0x30c
3447+#define ISP176x_HC_BUFFER_STATUS 0x334
3448+#define ISP176x_HC_MEMORY 0x33c
3449+
3450+/* Interrupt Register */
3451+#define ISP176x_HC_INTERRUPT 0x310
3452+#define ISP176x_HC_INTERRUPT_ENABLE 0x314
3453+#define ISP176x_HC_ISO_IRQ_MASK_OR 0x318
3454+#define ISP176x_HC_INT_IRQ_MASK_OR 0x31c
3455+#define ISP176x_HC_ATL_IRQ_MASK_OR 0x320
3456+#define ISP176x_HC_ISO_IRQ_MASK_AND 0x324
3457+#define ISP176x_HC_INT_IRQ_MASK_AND 0x328
3458+#define ISP176x_HC_ATL_IRQ_MASK_AND 0x32c
3459+
3460+#define ISP176x_HC_OTG_CTRL_SET 0x374
3461+#define ISP176x_HC_OTG_CTRL_CLEAR 0x376
3462+
3463+enum isp176x_host_controller_fields {
3464+ /* HC_PORTSC1 */
3465+ PORT_OWNER, PORT_POWER, PORT_LSTATUS, PORT_RESET, PORT_SUSPEND,
3466+ PORT_RESUME, PORT_PE, PORT_CSC, PORT_CONNECT,
3467+ /* HC_HCSPARAMS */
3468+ HCS_PPC, HCS_N_PORTS,
3469+ /* HC_HCCPARAMS */
3470+ HCC_ISOC_CACHE, HCC_ISOC_THRES,
3471+ /* HC_USBCMD */
3472+ CMD_LRESET, CMD_RESET, CMD_RUN,
3473+ /* HC_USBSTS */
3474+ STS_PCD,
3475+ /* HC_FRINDEX */
3476+ HC_FRINDEX,
3477+ /* HC_CONFIGFLAG */
3478+ FLAG_CF,
3479+ /* ISO/INT/ATL PTD */
3480+ HC_ISO_PTD_DONEMAP, HC_ISO_PTD_SKIPMAP, HC_ISO_PTD_LASTPTD,
3481+ HC_INT_PTD_DONEMAP, HC_INT_PTD_SKIPMAP, HC_INT_PTD_LASTPTD,
3482+ HC_ATL_PTD_DONEMAP, HC_ATL_PTD_SKIPMAP, HC_ATL_PTD_LASTPTD,
3483+ /* HC_HW_MODE_CTRL */
3484+ ALL_ATX_RESET, HW_ANA_DIGI_OC, HW_DEV_DMA, HW_COMN_IRQ, HW_COMN_DMA,
3485+ HW_DATA_BUS_WIDTH, HW_DACK_POL_HIGH, HW_DREQ_POL_HIGH, HW_INTR_HIGH_ACT,
3486+ HW_INTF_LOCK, HW_INTR_EDGE_TRIG, HW_GLOBAL_INTR_EN,
3487+ /* HC_CHIP_ID */
3488+ HC_CHIP_ID_HIGH, HC_CHIP_ID_LOW, HC_CHIP_REV,
3489+ /* HC_SCRATCH */
3490+ HC_SCRATCH,
3491+ /* HC_RESET */
3492+ SW_RESET_RESET_ATX, SW_RESET_RESET_HC, SW_RESET_RESET_ALL,
3493+ /* HC_BUFFER_STATUS */
3494+ ISO_BUF_FILL, INT_BUF_FILL, ATL_BUF_FILL,
3495+ /* HC_MEMORY */
3496+ MEM_BANK_SEL, MEM_START_ADDR,
3497+ /* HC_DATA */
3498+ HC_DATA,
3499+ /* HC_INTERRUPT */
3500+ HC_INTERRUPT,
3501+ /* HC_INTERRUPT_ENABLE */
3502+ HC_INT_IRQ_ENABLE, HC_ATL_IRQ_ENABLE,
3503+ /* INTERRUPT MASKS */
3504+ HC_ISO_IRQ_MASK_OR, HC_INT_IRQ_MASK_OR, HC_ATL_IRQ_MASK_OR,
3505+ HC_ISO_IRQ_MASK_AND, HC_INT_IRQ_MASK_AND, HC_ATL_IRQ_MASK_AND,
3506+ /* HW_OTG_CTRL_SET */
3507+ HW_OTG_DISABLE, HW_SW_SEL_HC_DC, HW_VBUS_DRV, HW_SEL_CP_EXT,
3508+ HW_DM_PULLDOWN, HW_DP_PULLDOWN, HW_DP_PULLUP, HW_HC_2_DIS,
3509+ /* HW_OTG_CTRL_CLR */
3510+ HW_OTG_DISABLE_CLEAR, HW_SW_SEL_HC_DC_CLEAR, HW_VBUS_DRV_CLEAR,
3511+ HW_SEL_CP_EXT_CLEAR, HW_DM_PULLDOWN_CLEAR, HW_DP_PULLDOWN_CLEAR,
3512+ HW_DP_PULLUP_CLEAR, HW_HC_2_DIS_CLEAR,
3513+ /* Last element */
3514+ HC_FIELD_MAX,
3515+};
3516+
3517+/* ISP1763 */
3518+/* EHCI operational registers */
3519+#define ISP1763_HC_USBCMD 0x8c
3520+#define ISP1763_HC_USBSTS 0x90
3521+#define ISP1763_HC_FRINDEX 0x98
3522+
3523+#define ISP1763_HC_CONFIGFLAG 0x9c
3524+#define ISP1763_HC_PORTSC1 0xa0
3525+
3526+#define ISP1763_HC_ISO_PTD_DONEMAP 0xa4
3527+#define ISP1763_HC_ISO_PTD_SKIPMAP 0xa6
3528+#define ISP1763_HC_ISO_PTD_LASTPTD 0xa8
3529+#define ISP1763_HC_INT_PTD_DONEMAP 0xaa
3530+#define ISP1763_HC_INT_PTD_SKIPMAP 0xac
3531+#define ISP1763_HC_INT_PTD_LASTPTD 0xae
3532+#define ISP1763_HC_ATL_PTD_DONEMAP 0xb0
3533+#define ISP1763_HC_ATL_PTD_SKIPMAP 0xb2
3534+#define ISP1763_HC_ATL_PTD_LASTPTD 0xb4
3535+
3536+/* Configuration Register */
3537+#define ISP1763_HC_HW_MODE_CTRL 0xb6
3538+#define ISP1763_HC_CHIP_REV 0x70
3539+#define ISP1763_HC_CHIP_ID 0x72
3540+#define ISP1763_HC_SCRATCH 0x78
3541+#define ISP1763_HC_RESET 0xb8
3542+#define ISP1763_HC_BUFFER_STATUS 0xba
3543+#define ISP1763_HC_MEMORY 0xc4
3544+#define ISP1763_HC_DATA 0xc6
3545+
3546+/* Interrupt Register */
3547+#define ISP1763_HC_INTERRUPT 0xd4
3548+#define ISP1763_HC_INTERRUPT_ENABLE 0xd6
3549+#define ISP1763_HC_ISO_IRQ_MASK_OR 0xd8
3550+#define ISP1763_HC_INT_IRQ_MASK_OR 0xda
3551+#define ISP1763_HC_ATL_IRQ_MASK_OR 0xdc
3552+#define ISP1763_HC_ISO_IRQ_MASK_AND 0xde
3553+#define ISP1763_HC_INT_IRQ_MASK_AND 0xe0
3554+#define ISP1763_HC_ATL_IRQ_MASK_AND 0xe2
3555+
3556+#define ISP1763_HC_OTG_CTRL_SET 0xe4
3557+#define ISP1763_HC_OTG_CTRL_CLEAR 0xe6
3558+
3559+/* -----------------------------------------------------------------------------
3560+ * Peripheral Controller
3561+ */
3562+
3563+#define DC_IEPTX(n) (1 << (11 + 2 * (n)))
3564+#define DC_IEPRX(n) (1 << (10 + 2 * (n)))
3565+#define DC_IEPRXTX(n) (3 << (10 + 2 * (n)))
3566+
3567+#define ISP176x_DC_CDBGMOD_ACK BIT(6)
3568+#define ISP176x_DC_DDBGMODIN_ACK BIT(4)
3569+#define ISP176x_DC_DDBGMODOUT_ACK BIT(2)
3570+
3571+#define ISP176x_DC_IEP0SETUP BIT(8)
3572+#define ISP176x_DC_IEVBUS BIT(7)
3573+#define ISP176x_DC_IEHS_STA BIT(5)
3574+#define ISP176x_DC_IERESM BIT(4)
3575+#define ISP176x_DC_IESUSP BIT(3)
3576+#define ISP176x_DC_IEBRST BIT(0)
3577+
3578+#define ISP176x_DC_ENDPTYP_ISOC 0x01
3579+#define ISP176x_DC_ENDPTYP_BULK 0x02
3580+#define ISP176x_DC_ENDPTYP_INTERRUPT 0x03
3581+
3582+/* Initialization Registers */
3583+#define ISP176x_DC_ADDRESS 0x0200
3584+#define ISP176x_DC_MODE 0x020c
3585+#define ISP176x_DC_INTCONF 0x0210
3586+#define ISP176x_DC_DEBUG 0x0212
3587+#define ISP176x_DC_INTENABLE 0x0214
3588+
3589+/* Data Flow Registers */
3590+#define ISP176x_DC_EPMAXPKTSZ 0x0204
3591+#define ISP176x_DC_EPTYPE 0x0208
3592+
3593+#define ISP176x_DC_BUFLEN 0x021c
3594+#define ISP176x_DC_BUFSTAT 0x021e
3595+#define ISP176x_DC_DATAPORT 0x0220
3596+
3597+#define ISP176x_DC_CTRLFUNC 0x0228
3598+#define ISP176x_DC_EPINDEX 0x022c
3599+
3600+/* DMA Registers */
3601+#define ISP176x_DC_DMACMD 0x0230
3602+#define ISP176x_DC_DMATXCOUNT 0x0234
3603+#define ISP176x_DC_DMACONF 0x0238
3604+#define ISP176x_DC_DMAHW 0x023c
3605+#define ISP176x_DC_DMAINTREASON 0x0250
3606+#define ISP176x_DC_DMAINTEN 0x0254
3607+#define ISP176x_DC_DMAEP 0x0258
3608+#define ISP176x_DC_DMABURSTCOUNT 0x0264
3609+
3610+/* General Registers */
3611+#define ISP176x_DC_INTERRUPT 0x0218
3612+#define ISP176x_DC_CHIPID 0x0270
3613+#define ISP176x_DC_FRAMENUM 0x0274
3614+#define ISP176x_DC_SCRATCH 0x0278
3615+#define ISP176x_DC_UNLOCKDEV 0x027c
3616+#define ISP176x_DC_INTPULSEWIDTH 0x0280
3617+#define ISP176x_DC_TESTMODE 0x0284
3618+
3619+enum isp176x_device_controller_fields {
3620+ /* DC_ADDRESS */
3621+ DC_DEVEN, DC_DEVADDR,
3622+ /* DC_MODE */
3623+ DC_VBUSSTAT, DC_SFRESET, DC_GLINTENA,
3624+ /* DC_INTCONF */
3625+ DC_CDBGMOD_ACK, DC_DDBGMODIN_ACK, DC_DDBGMODOUT_ACK, DC_INTPOL,
3626+ /* DC_INTENABLE */
3627+ DC_IEPRXTX_7, DC_IEPRXTX_6, DC_IEPRXTX_5, DC_IEPRXTX_4, DC_IEPRXTX_3,
3628+ DC_IEPRXTX_2, DC_IEPRXTX_1, DC_IEPRXTX_0,
3629+ DC_IEP0SETUP, DC_IEVBUS, DC_IEHS_STA, DC_IERESM, DC_IESUSP, DC_IEBRST,
3630+ /* DC_EPINDEX */
3631+ DC_EP0SETUP, DC_ENDPIDX, DC_EPDIR,
3632+ /* DC_CTRLFUNC */
3633+ DC_CLBUF, DC_VENDP, DC_DSEN, DC_STATUS, DC_STALL,
3634+ /* DC_BUFLEN */
3635+ DC_BUFLEN,
3636+ /* DC_EPMAXPKTSZ */
3637+ DC_FFOSZ,
3638+ /* DC_EPTYPE */
3639+ DC_EPENABLE, DC_ENDPTYP,
3640+ /* DC_FRAMENUM */
3641+ DC_FRAMENUM, DC_UFRAMENUM,
3642+ /* DC_CHIP_ID */
3643+ DC_CHIP_ID_HIGH, DC_CHIP_ID_LOW,
3644+ /* DC_SCRATCH */
3645+ DC_SCRATCH,
3646+ /* Last element */
3647+ DC_FIELD_MAX,
3648+};
3649+
3650+/* ISP1763 */
3651+/* Initialization Registers */
3652+#define ISP1763_DC_ADDRESS 0x00
3653+#define ISP1763_DC_MODE 0x0c
3654+#define ISP1763_DC_INTCONF 0x10
3655+#define ISP1763_DC_INTENABLE 0x14
3656+
3657+/* Data Flow Registers */
3658+#define ISP1763_DC_EPMAXPKTSZ 0x04
3659+#define ISP1763_DC_EPTYPE 0x08
3660+
3661+#define ISP1763_DC_BUFLEN 0x1c
3662+#define ISP1763_DC_BUFSTAT 0x1e
3663+#define ISP1763_DC_DATAPORT 0x20
3664+
3665+#define ISP1763_DC_CTRLFUNC 0x28
3666+#define ISP1763_DC_EPINDEX 0x2c
3667+
3668+/* DMA Registers */
3669+#define ISP1763_DC_DMACMD 0x30
3670+#define ISP1763_DC_DMATXCOUNT 0x34
3671+#define ISP1763_DC_DMACONF 0x38
3672+#define ISP1763_DC_DMAHW 0x3c
3673+#define ISP1763_DC_DMAINTREASON 0x50
3674+#define ISP1763_DC_DMAINTEN 0x54
3675+#define ISP1763_DC_DMAEP 0x58
3676+#define ISP1763_DC_DMABURSTCOUNT 0x64
3677+
3678+/* General Registers */
3679+#define ISP1763_DC_INTERRUPT 0x18
3680+#define ISP1763_DC_CHIPID_LOW 0x70
3681+#define ISP1763_DC_CHIPID_HIGH 0x72
3682+#define ISP1763_DC_FRAMENUM 0x74
3683+#define ISP1763_DC_SCRATCH 0x78
3684+#define ISP1763_DC_UNLOCKDEV 0x7c
3685+#define ISP1763_DC_INTPULSEWIDTH 0x80
3686+#define ISP1763_DC_TESTMODE 0x84
3687+
3688+#endif
3689diff --git a/drivers/usb/isp1760/isp1760-uboot.c b/drivers/usb/isp1760/isp1760-uboot.c
3690new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05003691index 0000000000..7635210fe2
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003692--- /dev/null
3693+++ b/drivers/usb/isp1760/isp1760-uboot.c
3694@@ -0,0 +1,76 @@
3695+// SPDX-License-Identifier: GPL-2.0
3696+/*
3697+ * Driver for the NXP ISP1760 chip
3698+ *
3699+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
3700+ *
3701+ */
3702+
3703+#include <common.h>
3704+#include <dm.h>
3705+#include <dm/device-internal.h>
3706+#include <dm/device_compat.h>
3707+#include <dm/devres.h>
3708+#include <dm/lists.h>
3709+#include <linux/bug.h>
3710+#include <linux/io.h>
3711+#include <linux/kernel.h>
3712+#include <linux/usb/otg.h>
3713+#include <linux/usb/usb_urb_compat.h>
3714+#include <log.h>
3715+#include <usb.h>
3716+
3717+#include "isp1760-core.h"
3718+#include "isp1760-hcd.h"
3719+#include "isp1760-regs.h"
3720+#include "isp1760-uboot.h"
3721+
3722+static int isp1760_msg_submit_control(struct udevice *dev,
3723+ struct usb_device *udev,
3724+ unsigned long pipe, void *buffer,
3725+ int length, struct devrequest *setup)
3726+{
3727+ struct isp1760_host_data *host = dev_get_priv(dev);
3728+
3729+ return usb_urb_submit_control(&host->hcd, &host->urb, &host->hep, udev,
3730+ pipe, buffer, length, setup, 0,
3731+ host->host_speed);
3732+}
3733+
3734+static int isp1760_msg_submit_bulk(struct udevice *dev, struct usb_device *udev,
3735+ unsigned long pipe, void *buffer, int length)
3736+{
3737+ struct isp1760_host_data *host = dev_get_priv(dev);
3738+
3739+ return usb_urb_submit_bulk(&host->hcd, &host->urb, &host->hep, udev,
3740+ pipe, buffer, length);
3741+}
3742+
3743+static int isp1760_msg_submit_irq(struct udevice *dev, struct usb_device *udev,
3744+ unsigned long pipe, void *buffer, int length,
3745+ int interval, bool nonblock)
3746+{
3747+ struct isp1760_host_data *host = dev_get_priv(dev);
3748+
3749+ return usb_urb_submit_irq(&host->hcd, &host->urb, &host->hep, udev,
3750+ pipe, buffer, length, interval);
3751+}
3752+
3753+static int isp1760_get_max_xfer_size(struct udevice *dev, size_t *size)
3754+{
3755+ struct isp1760_host_data *host = dev_get_priv(dev);
3756+ struct isp1760_hcd *priv = host->hcd.hcd_priv;
3757+ const struct isp1760_memory_layout *mem = priv->memory_layout;
3758+
3759+ *size = mem->blocks_size[ISP176x_BLOCK_NUM - 1];
3760+
3761+ return 0;
3762+}
3763+
3764+
3765+struct dm_usb_ops isp1760_usb_ops = {
3766+ .control = isp1760_msg_submit_control,
3767+ .bulk = isp1760_msg_submit_bulk,
3768+ .interrupt = isp1760_msg_submit_irq,
3769+ .get_max_xfer_size = isp1760_get_max_xfer_size,
3770+};
3771diff --git a/drivers/usb/isp1760/isp1760-uboot.h b/drivers/usb/isp1760/isp1760-uboot.h
3772new file mode 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05003773index 0000000000..2486de6f9e
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003774--- /dev/null
3775+++ b/drivers/usb/isp1760/isp1760-uboot.h
3776@@ -0,0 +1,27 @@
3777+// SPDX-License-Identifier: GPL-2.0
3778+/*
3779+ * Driver for the NXP ISP1760 chip
3780+ *
3781+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
3782+ *
3783+ */
3784+
3785+#ifndef __ISP1760_UBOOT_H__
3786+#define __ISP1760_UBOOT_H__
3787+
3788+#include <linux/usb/usb_urb_compat.h>
3789+#include <usb.h>
3790+
3791+#include "isp1760-core.h"
3792+
3793+struct isp1760_host_data {
3794+ struct isp1760_hcd *priv;
3795+ struct usb_hcd hcd;
3796+ enum usb_device_speed host_speed;
3797+ struct usb_host_endpoint hep;
3798+ struct urb urb;
3799+};
3800+
3801+extern struct dm_usb_ops isp1760_usb_ops;
3802+
3803+#endif
3804--
Patrick Williams8dd68482022-10-04 07:57:18 -050038052.17.1
Brad Bishopbec4ebc2022-08-03 09:55:16 -04003806