xref: /linux/drivers/usb/host/oxu210hp-hcd.c (revision 97002d33afe7c66aea1c76c783fbb2e8712a9564)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2008 Rodolfo Giometti <giometti@linux.it>
4  * Copyright (c) 2008 Eurotech S.p.A. <info@eurtech.it>
5  *
6  * This code is *strongly* based on EHCI-HCD code by David Brownell since
7  * the chip is a quasi-EHCI compatible.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/dmapool.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/ioport.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/string_choices.h>
19 #include <linux/errno.h>
20 #include <linux/timer.h>
21 #include <linux/list.h>
22 #include <linux/interrupt.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/moduleparam.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/io.h>
28 #include <linux/iopoll.h>
29 
30 #include <asm/irq.h>
31 #include <linux/unaligned.h>
32 
33 #include <linux/irq.h>
34 #include <linux/platform_device.h>
35 
36 #define DRIVER_VERSION "0.0.50"
37 
38 #define OXU_DEVICEID			0x00
39 	#define OXU_REV_MASK		0xffff0000
40 	#define OXU_REV_SHIFT		16
41 	#define OXU_REV_2100		0x2100
42 	#define OXU_BO_SHIFT		8
43 	#define OXU_BO_MASK		(0x3 << OXU_BO_SHIFT)
44 	#define OXU_MAJ_REV_SHIFT	4
45 	#define OXU_MAJ_REV_MASK	(0xf << OXU_MAJ_REV_SHIFT)
46 	#define OXU_MIN_REV_SHIFT	0
47 	#define OXU_MIN_REV_MASK	(0xf << OXU_MIN_REV_SHIFT)
48 #define OXU_HOSTIFCONFIG		0x04
49 #define OXU_SOFTRESET			0x08
50 	#define OXU_SRESET		(1 << 0)
51 
52 #define OXU_PIOBURSTREADCTRL		0x0C
53 
54 #define OXU_CHIPIRQSTATUS		0x10
55 #define OXU_CHIPIRQEN_SET		0x14
56 #define OXU_CHIPIRQEN_CLR		0x18
57 	#define OXU_USBSPHLPWUI		0x00000080
58 	#define OXU_USBOTGLPWUI		0x00000040
59 	#define OXU_USBSPHI		0x00000002
60 	#define OXU_USBOTGI		0x00000001
61 
62 #define OXU_CLKCTRL_SET			0x1C
63 	#define OXU_SYSCLKEN		0x00000008
64 	#define OXU_USBSPHCLKEN		0x00000002
65 	#define OXU_USBOTGCLKEN		0x00000001
66 
67 #define OXU_ASO				0x68
68 	#define OXU_SPHPOEN		0x00000100
69 	#define OXU_OVRCCURPUPDEN	0x00000800
70 	#define OXU_ASO_OP		(1 << 10)
71 	#define OXU_COMPARATOR		0x000004000
72 
73 #define OXU_USBMODE			0x1A8
74 	#define OXU_VBPS		0x00000020
75 	#define OXU_ES_LITTLE		0x00000000
76 	#define OXU_CM_HOST_ONLY	0x00000003
77 
78 /*
79  * Proper EHCI structs & defines
80  */
81 
82 /* Magic numbers that can affect system performance */
83 #define EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
84 #define EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
85 #define EHCI_TUNE_RL_TT		0
86 #define EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
87 #define EHCI_TUNE_MULT_TT	1
88 #define EHCI_TUNE_FLS		2	/* (small) 256 frame schedule */
89 
90 struct oxu_hcd;
91 
92 /* EHCI register interface, corresponds to EHCI Revision 0.95 specification */
93 
94 /* Section 2.2 Host Controller Capability Registers */
95 struct ehci_caps {
96 	/* these fields are specified as 8 and 16 bit registers,
97 	 * but some hosts can't perform 8 or 16 bit PCI accesses.
98 	 */
99 	u32		hc_capbase;
100 #define HC_LENGTH(p)		(((p)>>00)&0x00ff)	/* bits 7:0 */
101 #define HC_VERSION(p)		(((p)>>16)&0xffff)	/* bits 31:16 */
102 	u32		hcs_params;     /* HCSPARAMS - offset 0x4 */
103 #define HCS_DEBUG_PORT(p)	(((p)>>20)&0xf)	/* bits 23:20, debug port? */
104 #define HCS_INDICATOR(p)	((p)&(1 << 16))	/* true: has port indicators */
105 #define HCS_N_CC(p)		(((p)>>12)&0xf)	/* bits 15:12, #companion HCs */
106 #define HCS_N_PCC(p)		(((p)>>8)&0xf)	/* bits 11:8, ports per CC */
107 #define HCS_PORTROUTED(p)	((p)&(1 << 7))	/* true: port routing */
108 #define HCS_PPC(p)		((p)&(1 << 4))	/* true: port power control */
109 #define HCS_N_PORTS(p)		(((p)>>0)&0xf)	/* bits 3:0, ports on HC */
110 
111 	u32		hcc_params;      /* HCCPARAMS - offset 0x8 */
112 #define HCC_EXT_CAPS(p)		(((p)>>8)&0xff)	/* for pci extended caps */
113 #define HCC_ISOC_CACHE(p)       ((p)&(1 << 7))  /* true: can cache isoc frame */
114 #define HCC_ISOC_THRES(p)       (((p)>>4)&0x7)  /* bits 6:4, uframes cached */
115 #define HCC_CANPARK(p)		((p)&(1 << 2))  /* true: can park on async qh */
116 #define HCC_PGM_FRAMELISTLEN(p) ((p)&(1 << 1))  /* true: periodic_size changes*/
117 #define HCC_64BIT_ADDR(p)       ((p)&(1))       /* true: can use 64-bit addr */
118 	u8		portroute[8];	 /* nibbles for routing - offset 0xC */
119 } __packed;
120 
121 
122 /* Section 2.3 Host Controller Operational Registers */
123 struct ehci_regs {
124 	/* USBCMD: offset 0x00 */
125 	u32		command;
126 /* 23:16 is r/w intr rate, in microframes; default "8" == 1/msec */
127 #define CMD_PARK	(1<<11)		/* enable "park" on async qh */
128 #define CMD_PARK_CNT(c)	(((c)>>8)&3)	/* how many transfers to park for */
129 #define CMD_LRESET	(1<<7)		/* partial reset (no ports, etc) */
130 #define CMD_IAAD	(1<<6)		/* "doorbell" interrupt async advance */
131 #define CMD_ASE		(1<<5)		/* async schedule enable */
132 #define CMD_PSE		(1<<4)		/* periodic schedule enable */
133 /* 3:2 is periodic frame list size */
134 #define CMD_RESET	(1<<1)		/* reset HC not bus */
135 #define CMD_RUN		(1<<0)		/* start/stop HC */
136 
137 	/* USBSTS: offset 0x04 */
138 	u32		status;
139 #define STS_ASS		(1<<15)		/* Async Schedule Status */
140 #define STS_PSS		(1<<14)		/* Periodic Schedule Status */
141 #define STS_RECL	(1<<13)		/* Reclamation */
142 #define STS_HALT	(1<<12)		/* Not running (any reason) */
143 /* some bits reserved */
144 	/* these STS_* flags are also intr_enable bits (USBINTR) */
145 #define STS_IAA		(1<<5)		/* Interrupted on async advance */
146 #define STS_FATAL	(1<<4)		/* such as some PCI access errors */
147 #define STS_FLR		(1<<3)		/* frame list rolled over */
148 #define STS_PCD		(1<<2)		/* port change detect */
149 #define STS_ERR		(1<<1)		/* "error" completion (overflow, ...) */
150 #define STS_INT		(1<<0)		/* "normal" completion (short, ...) */
151 
152 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
153 
154 	/* USBINTR: offset 0x08 */
155 	u32		intr_enable;
156 
157 	/* FRINDEX: offset 0x0C */
158 	u32		frame_index;	/* current microframe number */
159 	/* CTRLDSSEGMENT: offset 0x10 */
160 	u32		segment;	/* address bits 63:32 if needed */
161 	/* PERIODICLISTBASE: offset 0x14 */
162 	u32		frame_list;	/* points to periodic list */
163 	/* ASYNCLISTADDR: offset 0x18 */
164 	u32		async_next;	/* address of next async queue head */
165 
166 	u32		reserved[9];
167 
168 	/* CONFIGFLAG: offset 0x40 */
169 	u32		configured_flag;
170 #define FLAG_CF		(1<<0)		/* true: we'll support "high speed" */
171 
172 	/* PORTSC: offset 0x44 */
173 	u32		port_status[];	/* up to N_PORTS */
174 /* 31:23 reserved */
175 #define PORT_WKOC_E	(1<<22)		/* wake on overcurrent (enable) */
176 #define PORT_WKDISC_E	(1<<21)		/* wake on disconnect (enable) */
177 #define PORT_WKCONN_E	(1<<20)		/* wake on connect (enable) */
178 /* 19:16 for port testing */
179 #define PORT_LED_OFF	(0<<14)
180 #define PORT_LED_AMBER	(1<<14)
181 #define PORT_LED_GREEN	(2<<14)
182 #define PORT_LED_MASK	(3<<14)
183 #define PORT_OWNER	(1<<13)		/* true: companion hc owns this port */
184 #define PORT_POWER	(1<<12)		/* true: has power (see PPC) */
185 #define PORT_USB11(x) (((x)&(3<<10)) == (1<<10))	/* USB 1.1 device */
186 /* 11:10 for detecting lowspeed devices (reset vs release ownership) */
187 /* 9 reserved */
188 #define PORT_RESET	(1<<8)		/* reset port */
189 #define PORT_SUSPEND	(1<<7)		/* suspend port */
190 #define PORT_RESUME	(1<<6)		/* resume it */
191 #define PORT_OCC	(1<<5)		/* over current change */
192 #define PORT_OC		(1<<4)		/* over current active */
193 #define PORT_PEC	(1<<3)		/* port enable change */
194 #define PORT_PE		(1<<2)		/* port enable */
195 #define PORT_CSC	(1<<1)		/* connect status change */
196 #define PORT_CONNECT	(1<<0)		/* device connected */
197 #define PORT_RWC_BITS   (PORT_CSC | PORT_PEC | PORT_OCC)
198 } __packed;
199 
200 #define	QTD_NEXT(dma)	cpu_to_le32((u32)dma)
201 
202 /*
203  * EHCI Specification 0.95 Section 3.5
204  * QTD: describe data transfer components (buffer, direction, ...)
205  * See Fig 3-6 "Queue Element Transfer Descriptor Block Diagram".
206  *
207  * These are associated only with "QH" (Queue Head) structures,
208  * used with control, bulk, and interrupt transfers.
209  */
210 struct ehci_qtd {
211 	/* first part defined by EHCI spec */
212 	__le32			hw_next;		/* see EHCI 3.5.1 */
213 	__le32			hw_alt_next;		/* see EHCI 3.5.2 */
214 	__le32			hw_token;		/* see EHCI 3.5.3 */
215 #define	QTD_TOGGLE	(1 << 31)	/* data toggle */
216 #define	QTD_LENGTH(tok)	(((tok)>>16) & 0x7fff)
217 #define	QTD_IOC		(1 << 15)	/* interrupt on complete */
218 #define	QTD_CERR(tok)	(((tok)>>10) & 0x3)
219 #define	QTD_PID(tok)	(((tok)>>8) & 0x3)
220 #define	QTD_STS_ACTIVE	(1 << 7)	/* HC may execute this */
221 #define	QTD_STS_HALT	(1 << 6)	/* halted on error */
222 #define	QTD_STS_DBE	(1 << 5)	/* data buffer error (in HC) */
223 #define	QTD_STS_BABBLE	(1 << 4)	/* device was babbling (qtd halted) */
224 #define	QTD_STS_XACT	(1 << 3)	/* device gave illegal response */
225 #define	QTD_STS_MMF	(1 << 2)	/* incomplete split transaction */
226 #define	QTD_STS_STS	(1 << 1)	/* split transaction state */
227 #define	QTD_STS_PING	(1 << 0)	/* issue PING? */
228 	__le32			hw_buf[5];		/* see EHCI 3.5.4 */
229 	__le32			hw_buf_hi[5];		/* Appendix B */
230 
231 	/* the rest is HCD-private */
232 	dma_addr_t		qtd_dma;		/* qtd address */
233 	struct list_head	qtd_list;		/* sw qtd list */
234 	struct urb		*urb;			/* qtd's urb */
235 	size_t			length;			/* length of buffer */
236 
237 	u32			qtd_buffer_len;
238 	void			*buffer;
239 	dma_addr_t		buffer_dma;
240 	void			*transfer_buffer;
241 	void			*transfer_dma;
242 } __aligned(32);
243 
244 /* mask NakCnt+T in qh->hw_alt_next */
245 #define QTD_MASK cpu_to_le32 (~0x1f)
246 
247 #define IS_SHORT_READ(token) (QTD_LENGTH(token) != 0 && QTD_PID(token) == 1)
248 
249 /* Type tag from {qh, itd, sitd, fstn}->hw_next */
250 #define Q_NEXT_TYPE(dma) ((dma) & cpu_to_le32 (3 << 1))
251 
252 /* values for that type tag */
253 #define Q_TYPE_QH	cpu_to_le32 (1 << 1)
254 
255 /* next async queue entry, or pointer to interrupt/periodic QH */
256 #define	QH_NEXT(dma)	(cpu_to_le32(((u32)dma)&~0x01f)|Q_TYPE_QH)
257 
258 /* for periodic/async schedules and qtd lists, mark end of list */
259 #define	EHCI_LIST_END	cpu_to_le32(1) /* "null pointer" to hw */
260 
261 /*
262  * Entries in periodic shadow table are pointers to one of four kinds
263  * of data structure.  That's dictated by the hardware; a type tag is
264  * encoded in the low bits of the hardware's periodic schedule.  Use
265  * Q_NEXT_TYPE to get the tag.
266  *
267  * For entries in the async schedule, the type tag always says "qh".
268  */
269 union ehci_shadow {
270 	struct ehci_qh		*qh;		/* Q_TYPE_QH */
271 	__le32			*hw_next;	/* (all types) */
272 	void			*ptr;
273 };
274 
275 /*
276  * EHCI Specification 0.95 Section 3.6
277  * QH: describes control/bulk/interrupt endpoints
278  * See Fig 3-7 "Queue Head Structure Layout".
279  *
280  * These appear in both the async and (for interrupt) periodic schedules.
281  */
282 
283 struct ehci_qh {
284 	/* first part defined by EHCI spec */
285 	__le32			hw_next;	 /* see EHCI 3.6.1 */
286 	__le32			hw_info1;	/* see EHCI 3.6.2 */
287 #define	QH_HEAD		0x00008000
288 	__le32			hw_info2;	/* see EHCI 3.6.2 */
289 #define	QH_SMASK	0x000000ff
290 #define	QH_CMASK	0x0000ff00
291 #define	QH_HUBADDR	0x007f0000
292 #define	QH_HUBPORT	0x3f800000
293 #define	QH_MULT		0xc0000000
294 	__le32			hw_current;	 /* qtd list - see EHCI 3.6.4 */
295 
296 	/* qtd overlay (hardware parts of a struct ehci_qtd) */
297 	__le32			hw_qtd_next;
298 	__le32			hw_alt_next;
299 	__le32			hw_token;
300 	__le32			hw_buf[5];
301 	__le32			hw_buf_hi[5];
302 
303 	/* the rest is HCD-private */
304 	dma_addr_t		qh_dma;		/* address of qh */
305 	union ehci_shadow	qh_next;	/* ptr to qh; or periodic */
306 	struct list_head	qtd_list;	/* sw qtd list */
307 	struct ehci_qtd		*dummy;
308 	struct ehci_qh		*reclaim;	/* next to reclaim */
309 
310 	struct oxu_hcd		*oxu;
311 	struct kref		kref;
312 	unsigned int		stamp;
313 
314 	u8			qh_state;
315 #define	QH_STATE_LINKED		1		/* HC sees this */
316 #define	QH_STATE_UNLINK		2		/* HC may still see this */
317 #define	QH_STATE_IDLE		3		/* HC doesn't see this */
318 #define	QH_STATE_UNLINK_WAIT	4		/* LINKED and on reclaim q */
319 #define	QH_STATE_COMPLETING	5		/* don't touch token.HALT */
320 
321 	/* periodic schedule info */
322 	u8			usecs;		/* intr bandwidth */
323 	u8			gap_uf;		/* uframes split/csplit gap */
324 	u8			c_usecs;	/* ... split completion bw */
325 	u16			tt_usecs;	/* tt downstream bandwidth */
326 	unsigned short		period;		/* polling interval */
327 	unsigned short		start;		/* where polling starts */
328 #define NO_FRAME ((unsigned short)~0)			/* pick new start */
329 	struct usb_device	*dev;		/* access to TT */
330 } __aligned(32);
331 
332 /*
333  * Proper OXU210HP structs
334  */
335 
336 #define OXU_OTG_CORE_OFFSET	0x00400
337 #define OXU_OTG_CAP_OFFSET	(OXU_OTG_CORE_OFFSET + 0x100)
338 #define OXU_SPH_CORE_OFFSET	0x00800
339 #define OXU_SPH_CAP_OFFSET	(OXU_SPH_CORE_OFFSET + 0x100)
340 
341 #define OXU_OTG_MEM		0xE000
342 #define OXU_SPH_MEM		0x16000
343 
344 /* Only how many elements & element structure are specifies here. */
345 /* 2 host controllers are enabled - total size <= 28 kbytes */
346 #define	DEFAULT_I_TDPS		1024
347 #define QHEAD_NUM		16
348 #define QTD_NUM			32
349 #define SITD_NUM		8
350 #define MURB_NUM		8
351 
352 #define BUFFER_NUM		8
353 #define BUFFER_SIZE		512
354 
355 struct oxu_info {
356 	struct usb_hcd *hcd[2];
357 };
358 
359 struct oxu_buf {
360 	u8			buffer[BUFFER_SIZE];
361 } __aligned(BUFFER_SIZE);
362 
363 struct oxu_onchip_mem {
364 	struct oxu_buf		db_pool[BUFFER_NUM];
365 
366 	u32			frame_list[DEFAULT_I_TDPS];
367 	struct ehci_qh		qh_pool[QHEAD_NUM];
368 	struct ehci_qtd		qtd_pool[QTD_NUM];
369 } __aligned(4 << 10);
370 
371 #define	EHCI_MAX_ROOT_PORTS	15		/* see HCS_N_PORTS */
372 
373 struct oxu_murb {
374 	struct urb		urb;
375 	struct urb		*main;
376 	u8			last;
377 };
378 
379 struct oxu_hcd {				/* one per controller */
380 	unsigned int		is_otg:1;
381 
382 	u8			qh_used[QHEAD_NUM];
383 	u8			qtd_used[QTD_NUM];
384 	u8			db_used[BUFFER_NUM];
385 	u8			murb_used[MURB_NUM];
386 
387 	struct oxu_onchip_mem	__iomem *mem;
388 	spinlock_t		mem_lock;
389 
390 	struct timer_list	urb_timer;
391 
392 	struct ehci_caps __iomem *caps;
393 	struct ehci_regs __iomem *regs;
394 
395 	u32			hcs_params;	/* cached register copy */
396 	spinlock_t		lock;
397 
398 	/* async schedule support */
399 	struct ehci_qh		*async;
400 	struct ehci_qh		*reclaim;
401 	unsigned int		reclaim_ready:1;
402 	unsigned int		scanning:1;
403 
404 	/* periodic schedule support */
405 	unsigned int		periodic_size;
406 	__le32			*periodic;	/* hw periodic table */
407 	dma_addr_t		periodic_dma;
408 	unsigned int		i_thresh;	/* uframes HC might cache */
409 
410 	union ehci_shadow	*pshadow;	/* mirror hw periodic table */
411 	int			next_uframe;	/* scan periodic, start here */
412 	unsigned int		periodic_sched;	/* periodic activity count */
413 
414 	/* per root hub port */
415 	unsigned long		reset_done[EHCI_MAX_ROOT_PORTS];
416 	/* bit vectors (one bit per port) */
417 	unsigned long		bus_suspended;	/* which ports were
418 						 * already suspended at the
419 						 * start of a bus suspend
420 						 */
421 	unsigned long		companion_ports;/* which ports are dedicated
422 						 * to the companion controller
423 						 */
424 
425 	struct timer_list	watchdog;
426 	unsigned long		actions;
427 	unsigned int		stamp;
428 	unsigned long		next_statechange;
429 	u32			command;
430 
431 	/* SILICON QUIRKS */
432 	struct list_head	urb_list;	/* this is the head to urb
433 						 * queue that didn't get enough
434 						 * resources
435 						 */
436 	struct oxu_murb		*murb_pool;	/* murb per split big urb */
437 	unsigned int		urb_len;
438 
439 	u8			sbrn;		/* packed release number */
440 };
441 
442 #define EHCI_IAA_JIFFIES	(HZ/100)	/* arbitrary; ~10 msec */
443 #define EHCI_IO_JIFFIES		(HZ/10)		/* io watchdog > irq_thresh */
444 #define EHCI_ASYNC_JIFFIES      (HZ/20)		/* async idle timeout */
445 #define EHCI_SHRINK_JIFFIES     (HZ/200)	/* async qh unlink delay */
446 
447 enum ehci_timer_action {
448 	TIMER_IO_WATCHDOG,
449 	TIMER_IAA_WATCHDOG,
450 	TIMER_ASYNC_SHRINK,
451 	TIMER_ASYNC_OFF,
452 };
453 
454 /*
455  * Main defines
456  */
457 
458 #define oxu_dbg(oxu, fmt, args...) \
459 		dev_dbg(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
460 #define oxu_err(oxu, fmt, args...) \
461 		dev_err(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
462 #define oxu_info(oxu, fmt, args...) \
463 		dev_info(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
464 
465 #ifdef CONFIG_DYNAMIC_DEBUG
466 #define DEBUG
467 #endif
468 
469 static inline struct usb_hcd *oxu_to_hcd(struct oxu_hcd *oxu)
470 {
471 	return container_of((void *) oxu, struct usb_hcd, hcd_priv);
472 }
473 
474 static inline struct oxu_hcd *hcd_to_oxu(struct usb_hcd *hcd)
475 {
476 	return (struct oxu_hcd *) (hcd->hcd_priv);
477 }
478 
479 /*
480  * Debug stuff
481  */
482 
483 #undef OXU_URB_TRACE
484 #undef OXU_VERBOSE_DEBUG
485 
486 #ifdef OXU_VERBOSE_DEBUG
487 #define oxu_vdbg			oxu_dbg
488 #else
489 #define oxu_vdbg(oxu, fmt, args...)	/* Nop */
490 #endif
491 
492 #ifdef DEBUG
493 
494 static int __attribute__((__unused__))
495 dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
496 {
497 	return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
498 		label, label[0] ? " " : "", status,
499 		(status & STS_ASS) ? " Async" : "",
500 		(status & STS_PSS) ? " Periodic" : "",
501 		(status & STS_RECL) ? " Recl" : "",
502 		(status & STS_HALT) ? " Halt" : "",
503 		(status & STS_IAA) ? " IAA" : "",
504 		(status & STS_FATAL) ? " FATAL" : "",
505 		(status & STS_FLR) ? " FLR" : "",
506 		(status & STS_PCD) ? " PCD" : "",
507 		(status & STS_ERR) ? " ERR" : "",
508 		(status & STS_INT) ? " INT" : ""
509 		);
510 }
511 
512 static int __attribute__((__unused__))
513 dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
514 {
515 	return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
516 		label, label[0] ? " " : "", enable,
517 		(enable & STS_IAA) ? " IAA" : "",
518 		(enable & STS_FATAL) ? " FATAL" : "",
519 		(enable & STS_FLR) ? " FLR" : "",
520 		(enable & STS_PCD) ? " PCD" : "",
521 		(enable & STS_ERR) ? " ERR" : "",
522 		(enable & STS_INT) ? " INT" : ""
523 		);
524 }
525 
526 static const char *const fls_strings[] =
527     { "1024", "512", "256", "??" };
528 
529 static int dbg_command_buf(char *buf, unsigned len,
530 				const char *label, u32 command)
531 {
532 	return scnprintf(buf, len,
533 		"%s%scommand %06x %s=%d ithresh=%d%s%s%s%s period=%s%s %s",
534 		label, label[0] ? " " : "", command,
535 		(command & CMD_PARK) ? "park" : "(park)",
536 		CMD_PARK_CNT(command),
537 		(command >> 16) & 0x3f,
538 		(command & CMD_LRESET) ? " LReset" : "",
539 		(command & CMD_IAAD) ? " IAAD" : "",
540 		(command & CMD_ASE) ? " Async" : "",
541 		(command & CMD_PSE) ? " Periodic" : "",
542 		fls_strings[(command >> 2) & 0x3],
543 		(command & CMD_RESET) ? " Reset" : "",
544 		(command & CMD_RUN) ? "RUN" : "HALT"
545 		);
546 }
547 
548 static int dbg_port_buf(char *buf, unsigned len, const char *label,
549 				int port, u32 status)
550 {
551 	char	*sig;
552 
553 	/* signaling state */
554 	switch (status & (3 << 10)) {
555 	case 0 << 10:
556 		sig = "se0";
557 		break;
558 	case 1 << 10:
559 		sig = "k";	/* low speed */
560 		break;
561 	case 2 << 10:
562 		sig = "j";
563 		break;
564 	default:
565 		sig = "?";
566 		break;
567 	}
568 
569 	return scnprintf(buf, len,
570 		"%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s",
571 		label, label[0] ? " " : "", port, status,
572 		(status & PORT_POWER) ? " POWER" : "",
573 		(status & PORT_OWNER) ? " OWNER" : "",
574 		sig,
575 		(status & PORT_RESET) ? " RESET" : "",
576 		(status & PORT_SUSPEND) ? " SUSPEND" : "",
577 		(status & PORT_RESUME) ? " RESUME" : "",
578 		(status & PORT_OCC) ? " OCC" : "",
579 		(status & PORT_OC) ? " OC" : "",
580 		(status & PORT_PEC) ? " PEC" : "",
581 		(status & PORT_PE) ? " PE" : "",
582 		(status & PORT_CSC) ? " CSC" : "",
583 		(status & PORT_CONNECT) ? " CONNECT" : ""
584 	    );
585 }
586 
587 #else
588 
589 static inline int __attribute__((__unused__))
590 dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
591 { return 0; }
592 
593 static inline int __attribute__((__unused__))
594 dbg_command_buf(char *buf, unsigned len, const char *label, u32 command)
595 { return 0; }
596 
597 static inline int __attribute__((__unused__))
598 dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
599 { return 0; }
600 
601 static inline int __attribute__((__unused__))
602 dbg_port_buf(char *buf, unsigned len, const char *label, int port, u32 status)
603 { return 0; }
604 
605 #endif /* DEBUG */
606 
607 /* functions have the "wrong" filename when they're output... */
608 #define dbg_status(oxu, label, status) { \
609 	char _buf[80]; \
610 	dbg_status_buf(_buf, sizeof _buf, label, status); \
611 	oxu_dbg(oxu, "%s\n", _buf); \
612 }
613 
614 #define dbg_cmd(oxu, label, command) { \
615 	char _buf[80]; \
616 	dbg_command_buf(_buf, sizeof _buf, label, command); \
617 	oxu_dbg(oxu, "%s\n", _buf); \
618 }
619 
620 #define dbg_port(oxu, label, port, status) { \
621 	char _buf[80]; \
622 	dbg_port_buf(_buf, sizeof _buf, label, port, status); \
623 	oxu_dbg(oxu, "%s\n", _buf); \
624 }
625 
626 /*
627  * Module parameters
628  */
629 
630 /* Initial IRQ latency: faster than hw default */
631 static int log2_irq_thresh;			/* 0 to 6 */
632 module_param(log2_irq_thresh, int, S_IRUGO);
633 MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
634 
635 /* Initial park setting: slower than hw default */
636 static unsigned park;
637 module_param(park, uint, S_IRUGO);
638 MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
639 
640 /* For flakey hardware, ignore overcurrent indicators */
641 static bool ignore_oc;
642 module_param(ignore_oc, bool, S_IRUGO);
643 MODULE_PARM_DESC(ignore_oc, "ignore bogus hardware overcurrent indications");
644 
645 
646 static void ehci_work(struct oxu_hcd *oxu);
647 static int oxu_hub_control(struct usb_hcd *hcd,
648 				u16 typeReq, u16 wValue, u16 wIndex,
649 				char *buf, u16 wLength);
650 
651 /*
652  * Local functions
653  */
654 
655 /* Low level read/write registers functions */
656 static inline u32 oxu_readl(void __iomem *base, u32 reg)
657 {
658 	return readl(base + reg);
659 }
660 
661 static inline void oxu_writel(void __iomem *base, u32 reg, u32 val)
662 {
663 	writel(val, base + reg);
664 }
665 
666 static inline void timer_action_done(struct oxu_hcd *oxu,
667 					enum ehci_timer_action action)
668 {
669 	clear_bit(action, &oxu->actions);
670 }
671 
672 static inline void timer_action(struct oxu_hcd *oxu,
673 					enum ehci_timer_action action)
674 {
675 	if (!test_and_set_bit(action, &oxu->actions)) {
676 		unsigned long t;
677 
678 		switch (action) {
679 		case TIMER_IAA_WATCHDOG:
680 			t = EHCI_IAA_JIFFIES;
681 			break;
682 		case TIMER_IO_WATCHDOG:
683 			t = EHCI_IO_JIFFIES;
684 			break;
685 		case TIMER_ASYNC_OFF:
686 			t = EHCI_ASYNC_JIFFIES;
687 			break;
688 		case TIMER_ASYNC_SHRINK:
689 		default:
690 			t = EHCI_SHRINK_JIFFIES;
691 			break;
692 		}
693 		t += jiffies;
694 		/* all timings except IAA watchdog can be overridden.
695 		 * async queue SHRINK often precedes IAA.  while it's ready
696 		 * to go OFF neither can matter, and afterwards the IO
697 		 * watchdog stops unless there's still periodic traffic.
698 		 */
699 		if (action != TIMER_IAA_WATCHDOG
700 				&& t > oxu->watchdog.expires
701 				&& timer_pending(&oxu->watchdog))
702 			return;
703 		mod_timer(&oxu->watchdog, t);
704 	}
705 }
706 
707 /*
708  * handshake - spin reading hc until handshake completes or fails
709  * @ptr: address of hc register to be read
710  * @mask: bits to look at in result of read
711  * @done: value of those bits when handshake succeeds
712  * @usec: timeout in microseconds
713  *
714  * Returns negative errno, or zero on success
715  *
716  * Success happens when the "mask" bits have the specified value (hardware
717  * handshake done).  There are two failure modes:  "usec" have passed (major
718  * hardware flakeout), or the register reads as all-ones (hardware removed).
719  *
720  * That last failure should_only happen in cases like physical cardbus eject
721  * before driver shutdown. But it also seems to be caused by bugs in cardbus
722  * bridge shutdown:  shutting down the bridge before the devices using it.
723  */
724 static int handshake(struct oxu_hcd *oxu, void __iomem *ptr,
725 					u32 mask, u32 done, int usec)
726 {
727 	u32 result;
728 	int ret;
729 
730 	ret = readl_poll_timeout_atomic(ptr, result,
731 					((result & mask) == done ||
732 					 result == U32_MAX),
733 					1, usec);
734 	if (result == U32_MAX)		/* card removed */
735 		return -ENODEV;
736 
737 	return ret;
738 }
739 
740 /* Force HC to halt state from unknown (EHCI spec section 2.3) */
741 static int ehci_halt(struct oxu_hcd *oxu)
742 {
743 	u32	temp = readl(&oxu->regs->status);
744 
745 	/* disable any irqs left enabled by previous code */
746 	writel(0, &oxu->regs->intr_enable);
747 
748 	if ((temp & STS_HALT) != 0)
749 		return 0;
750 
751 	temp = readl(&oxu->regs->command);
752 	temp &= ~CMD_RUN;
753 	writel(temp, &oxu->regs->command);
754 	return handshake(oxu, &oxu->regs->status,
755 			  STS_HALT, STS_HALT, 16 * 125);
756 }
757 
758 /* Put TDI/ARC silicon into EHCI mode */
759 static void tdi_reset(struct oxu_hcd *oxu)
760 {
761 	u32 __iomem *reg_ptr;
762 	u32 tmp;
763 
764 	reg_ptr = (u32 __iomem *)(((u8 __iomem *)oxu->regs) + 0x68);
765 	tmp = readl(reg_ptr);
766 	tmp |= 0x3;
767 	writel(tmp, reg_ptr);
768 }
769 
770 /* Reset a non-running (STS_HALT == 1) controller */
771 static int ehci_reset(struct oxu_hcd *oxu)
772 {
773 	int	retval;
774 	u32	command = readl(&oxu->regs->command);
775 
776 	command |= CMD_RESET;
777 	dbg_cmd(oxu, "reset", command);
778 	writel(command, &oxu->regs->command);
779 	oxu_to_hcd(oxu)->state = HC_STATE_HALT;
780 	oxu->next_statechange = jiffies;
781 	retval = handshake(oxu, &oxu->regs->command,
782 			    CMD_RESET, 0, 250 * 1000);
783 
784 	if (retval)
785 		return retval;
786 
787 	tdi_reset(oxu);
788 
789 	return retval;
790 }
791 
792 /* Idle the controller (from running) */
793 static void ehci_quiesce(struct oxu_hcd *oxu)
794 {
795 	u32	temp;
796 
797 #ifdef DEBUG
798 	BUG_ON(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state));
799 #endif
800 
801 	/* wait for any schedule enables/disables to take effect */
802 	temp = readl(&oxu->regs->command) << 10;
803 	temp &= STS_ASS | STS_PSS;
804 	if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
805 				temp, 16 * 125) != 0) {
806 		oxu_to_hcd(oxu)->state = HC_STATE_HALT;
807 		return;
808 	}
809 
810 	/* then disable anything that's still active */
811 	temp = readl(&oxu->regs->command);
812 	temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
813 	writel(temp, &oxu->regs->command);
814 
815 	/* hardware can take 16 microframes to turn off ... */
816 	if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
817 				0, 16 * 125) != 0) {
818 		oxu_to_hcd(oxu)->state = HC_STATE_HALT;
819 		return;
820 	}
821 }
822 
823 static int check_reset_complete(struct oxu_hcd *oxu, int index,
824 				u32 __iomem *status_reg, int port_status)
825 {
826 	if (!(port_status & PORT_CONNECT)) {
827 		oxu->reset_done[index] = 0;
828 		return port_status;
829 	}
830 
831 	/* if reset finished and it's still not enabled -- handoff */
832 	if (!(port_status & PORT_PE)) {
833 		oxu_dbg(oxu, "Failed to enable port %d on root hub TT\n",
834 				index+1);
835 		return port_status;
836 	} else
837 		oxu_dbg(oxu, "port %d high speed\n", index + 1);
838 
839 	return port_status;
840 }
841 
842 static void ehci_hub_descriptor(struct oxu_hcd *oxu,
843 				struct usb_hub_descriptor *desc)
844 {
845 	int ports = HCS_N_PORTS(oxu->hcs_params);
846 	u16 temp;
847 
848 	desc->bDescriptorType = USB_DT_HUB;
849 	desc->bPwrOn2PwrGood = 10;	/* oxu 1.0, 2.3.9 says 20ms max */
850 	desc->bHubContrCurrent = 0;
851 
852 	desc->bNbrPorts = ports;
853 	temp = 1 + (ports / 8);
854 	desc->bDescLength = 7 + 2 * temp;
855 
856 	/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
857 	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
858 	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
859 
860 	temp = HUB_CHAR_INDV_PORT_OCPM;	/* per-port overcurrent reporting */
861 	if (HCS_PPC(oxu->hcs_params))
862 		temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
863 	else
864 		temp |= HUB_CHAR_NO_LPSM; /* no power switching */
865 	desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
866 }
867 
868 
869 /* Allocate an OXU210HP on-chip memory data buffer
870  *
871  * An on-chip memory data buffer is required for each OXU210HP USB transfer.
872  * Each transfer descriptor has one or more on-chip memory data buffers.
873  *
874  * Data buffers are allocated from a fix sized pool of data blocks.
875  * To minimise fragmentation and give reasonable memory utlisation,
876  * data buffers are allocated with sizes the power of 2 multiples of
877  * the block size, starting on an address a multiple of the allocated size.
878  *
879  * FIXME: callers of this function require a buffer to be allocated for
880  * len=0. This is a waste of on-chip memory and should be fix. Then this
881  * function should be changed to not allocate a buffer for len=0.
882  */
883 static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)
884 {
885 	int n_blocks;	/* minium blocks needed to hold len */
886 	int a_blocks;	/* blocks allocated */
887 	int i, j;
888 
889 	/* Don't allocate bigger than supported */
890 	if (len > BUFFER_SIZE * BUFFER_NUM) {
891 		oxu_err(oxu, "buffer too big (%d)\n", len);
892 		return -ENOMEM;
893 	}
894 
895 	spin_lock(&oxu->mem_lock);
896 
897 	/* Number of blocks needed to hold len */
898 	n_blocks = (len + BUFFER_SIZE - 1) / BUFFER_SIZE;
899 
900 	/* Round the number of blocks up to the power of 2 */
901 	for (a_blocks = 1; a_blocks < n_blocks; a_blocks <<= 1)
902 		;
903 
904 	/* Find a suitable available data buffer */
905 	for (i = 0; i < BUFFER_NUM;
906 			i += max_t(int, a_blocks, oxu->db_used[i])) {
907 
908 		/* Check all the required blocks are available */
909 		for (j = 0; j < a_blocks; j++)
910 			if (oxu->db_used[i + j])
911 				break;
912 
913 		if (j != a_blocks)
914 			continue;
915 
916 		/* Allocate blocks found! */
917 		qtd->buffer = (void *) &oxu->mem->db_pool[i];
918 		qtd->buffer_dma = virt_to_phys(qtd->buffer);
919 
920 		qtd->qtd_buffer_len = BUFFER_SIZE * a_blocks;
921 		oxu->db_used[i] = a_blocks;
922 
923 		spin_unlock(&oxu->mem_lock);
924 
925 		return 0;
926 	}
927 
928 	/* Failed */
929 
930 	spin_unlock(&oxu->mem_lock);
931 
932 	return -ENOMEM;
933 }
934 
935 static void oxu_buf_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
936 {
937 	int index;
938 
939 	spin_lock(&oxu->mem_lock);
940 
941 	index = (qtd->buffer - (void *) &oxu->mem->db_pool[0])
942 							 / BUFFER_SIZE;
943 	oxu->db_used[index] = 0;
944 	qtd->qtd_buffer_len = 0;
945 	qtd->buffer_dma = 0;
946 	qtd->buffer = NULL;
947 
948 	spin_unlock(&oxu->mem_lock);
949 }
950 
951 static inline void ehci_qtd_init(struct ehci_qtd *qtd, dma_addr_t dma)
952 {
953 	memset(qtd, 0, sizeof *qtd);
954 	qtd->qtd_dma = dma;
955 	qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
956 	qtd->hw_next = EHCI_LIST_END;
957 	qtd->hw_alt_next = EHCI_LIST_END;
958 	INIT_LIST_HEAD(&qtd->qtd_list);
959 }
960 
961 static inline void oxu_qtd_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
962 {
963 	int index;
964 
965 	if (qtd->buffer)
966 		oxu_buf_free(oxu, qtd);
967 
968 	spin_lock(&oxu->mem_lock);
969 
970 	index = qtd - &oxu->mem->qtd_pool[0];
971 	oxu->qtd_used[index] = 0;
972 
973 	spin_unlock(&oxu->mem_lock);
974 }
975 
976 static struct ehci_qtd *ehci_qtd_alloc(struct oxu_hcd *oxu)
977 {
978 	int i;
979 	struct ehci_qtd *qtd = NULL;
980 
981 	spin_lock(&oxu->mem_lock);
982 
983 	for (i = 0; i < QTD_NUM; i++)
984 		if (!oxu->qtd_used[i])
985 			break;
986 
987 	if (i < QTD_NUM) {
988 		qtd = (struct ehci_qtd *) &oxu->mem->qtd_pool[i];
989 		memset(qtd, 0, sizeof *qtd);
990 
991 		qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
992 		qtd->hw_next = EHCI_LIST_END;
993 		qtd->hw_alt_next = EHCI_LIST_END;
994 		INIT_LIST_HEAD(&qtd->qtd_list);
995 
996 		qtd->qtd_dma = virt_to_phys(qtd);
997 
998 		oxu->qtd_used[i] = 1;
999 	}
1000 
1001 	spin_unlock(&oxu->mem_lock);
1002 
1003 	return qtd;
1004 }
1005 
1006 static void oxu_qh_free(struct oxu_hcd *oxu, struct ehci_qh *qh)
1007 {
1008 	int index;
1009 
1010 	spin_lock(&oxu->mem_lock);
1011 
1012 	index = qh - &oxu->mem->qh_pool[0];
1013 	oxu->qh_used[index] = 0;
1014 
1015 	spin_unlock(&oxu->mem_lock);
1016 }
1017 
1018 static void qh_destroy(struct kref *kref)
1019 {
1020 	struct ehci_qh *qh = container_of(kref, struct ehci_qh, kref);
1021 	struct oxu_hcd *oxu = qh->oxu;
1022 
1023 	/* clean qtds first, and know this is not linked */
1024 	if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1025 		oxu_dbg(oxu, "unused qh not empty!\n");
1026 		BUG();
1027 	}
1028 	if (qh->dummy)
1029 		oxu_qtd_free(oxu, qh->dummy);
1030 	oxu_qh_free(oxu, qh);
1031 }
1032 
1033 static struct ehci_qh *oxu_qh_alloc(struct oxu_hcd *oxu)
1034 {
1035 	int i;
1036 	struct ehci_qh *qh = NULL;
1037 	struct ehci_qtd *dummy;
1038 
1039 	/* dummy td enables safe urb queuing; this takes mem_lock */
1040 	dummy = ehci_qtd_alloc(oxu);
1041 	if (!dummy) {
1042 		oxu_dbg(oxu, "no dummy td\n");
1043 		return NULL;
1044 	}
1045 
1046 	spin_lock(&oxu->mem_lock);
1047 
1048 	for (i = 0; i < QHEAD_NUM; i++)
1049 		if (!oxu->qh_used[i])
1050 			break;
1051 
1052 	if (i < QHEAD_NUM) {
1053 		qh = (struct ehci_qh *) &oxu->mem->qh_pool[i];
1054 		memset(qh, 0, sizeof *qh);
1055 
1056 		kref_init(&qh->kref);
1057 		qh->oxu = oxu;
1058 		qh->qh_dma = virt_to_phys(qh);
1059 		INIT_LIST_HEAD(&qh->qtd_list);
1060 		qh->dummy = dummy;
1061 
1062 		oxu->qh_used[i] = 1;
1063 	}
1064 
1065 	spin_unlock(&oxu->mem_lock);
1066 
1067 	if (!qh)
1068 		oxu_qtd_free(oxu, dummy);
1069 
1070 	return qh;
1071 }
1072 
1073 /* to share a qh (cpu threads, or hc) */
1074 static inline struct ehci_qh *qh_get(struct ehci_qh *qh)
1075 {
1076 	kref_get(&qh->kref);
1077 	return qh;
1078 }
1079 
1080 static inline void qh_put(struct ehci_qh *qh)
1081 {
1082 	kref_put(&qh->kref, qh_destroy);
1083 }
1084 
1085 static void oxu_murb_free(struct oxu_hcd *oxu, struct oxu_murb *murb)
1086 {
1087 	int index;
1088 
1089 	spin_lock(&oxu->mem_lock);
1090 
1091 	index = murb - &oxu->murb_pool[0];
1092 	oxu->murb_used[index] = 0;
1093 
1094 	spin_unlock(&oxu->mem_lock);
1095 }
1096 
1097 static struct oxu_murb *oxu_murb_alloc(struct oxu_hcd *oxu)
1098 
1099 {
1100 	int i;
1101 	struct oxu_murb *murb = NULL;
1102 
1103 	spin_lock(&oxu->mem_lock);
1104 
1105 	for (i = 0; i < MURB_NUM; i++)
1106 		if (!oxu->murb_used[i])
1107 			break;
1108 
1109 	if (i < MURB_NUM) {
1110 		murb = &(oxu->murb_pool)[i];
1111 
1112 		oxu->murb_used[i] = 1;
1113 	}
1114 
1115 	spin_unlock(&oxu->mem_lock);
1116 
1117 	return murb;
1118 }
1119 
1120 /* The queue heads and transfer descriptors are managed from pools tied
1121  * to each of the "per device" structures.
1122  * This is the initialisation and cleanup code.
1123  */
1124 static void ehci_mem_cleanup(struct oxu_hcd *oxu)
1125 {
1126 	kfree(oxu->murb_pool);
1127 	oxu->murb_pool = NULL;
1128 
1129 	if (oxu->async)
1130 		qh_put(oxu->async);
1131 	oxu->async = NULL;
1132 
1133 	timer_delete(&oxu->urb_timer);
1134 
1135 	oxu->periodic = NULL;
1136 
1137 	/* shadow periodic table */
1138 	kfree(oxu->pshadow);
1139 	oxu->pshadow = NULL;
1140 }
1141 
1142 /* Remember to add cleanup code (above) if you add anything here.
1143  */
1144 static int ehci_mem_init(struct oxu_hcd *oxu, gfp_t flags)
1145 {
1146 	int i;
1147 
1148 	for (i = 0; i < oxu->periodic_size; i++)
1149 		oxu->mem->frame_list[i] = EHCI_LIST_END;
1150 	for (i = 0; i < QHEAD_NUM; i++)
1151 		oxu->qh_used[i] = 0;
1152 	for (i = 0; i < QTD_NUM; i++)
1153 		oxu->qtd_used[i] = 0;
1154 
1155 	oxu->murb_pool = kzalloc_objs(struct oxu_murb, MURB_NUM, flags);
1156 	if (!oxu->murb_pool)
1157 		goto fail;
1158 
1159 	for (i = 0; i < MURB_NUM; i++)
1160 		oxu->murb_used[i] = 0;
1161 
1162 	oxu->async = oxu_qh_alloc(oxu);
1163 	if (!oxu->async)
1164 		goto fail;
1165 
1166 	oxu->periodic = (__le32 *) &oxu->mem->frame_list;
1167 	oxu->periodic_dma = virt_to_phys(oxu->periodic);
1168 
1169 	for (i = 0; i < oxu->periodic_size; i++)
1170 		oxu->periodic[i] = EHCI_LIST_END;
1171 
1172 	/* software shadow of hardware table */
1173 	oxu->pshadow = kcalloc(oxu->periodic_size, sizeof(void *), flags);
1174 	if (oxu->pshadow != NULL)
1175 		return 0;
1176 
1177 fail:
1178 	oxu_dbg(oxu, "couldn't init memory\n");
1179 	ehci_mem_cleanup(oxu);
1180 	return -ENOMEM;
1181 }
1182 
1183 /* Fill a qtd, returning how much of the buffer we were able to queue up.
1184  */
1185 static int qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf, size_t len,
1186 				int token, int maxpacket)
1187 {
1188 	int i, count;
1189 	u64 addr = buf;
1190 
1191 	/* one buffer entry per 4K ... first might be short or unaligned */
1192 	qtd->hw_buf[0] = cpu_to_le32((u32)addr);
1193 	qtd->hw_buf_hi[0] = cpu_to_le32((u32)(addr >> 32));
1194 	count = 0x1000 - (buf & 0x0fff);	/* rest of that page */
1195 	if (likely(len < count))		/* ... iff needed */
1196 		count = len;
1197 	else {
1198 		buf +=  0x1000;
1199 		buf &= ~0x0fff;
1200 
1201 		/* per-qtd limit: from 16K to 20K (best alignment) */
1202 		for (i = 1; count < len && i < 5; i++) {
1203 			addr = buf;
1204 			qtd->hw_buf[i] = cpu_to_le32((u32)addr);
1205 			qtd->hw_buf_hi[i] = cpu_to_le32((u32)(addr >> 32));
1206 			buf += 0x1000;
1207 			if ((count + 0x1000) < len)
1208 				count += 0x1000;
1209 			else
1210 				count = len;
1211 		}
1212 
1213 		/* short packets may only terminate transfers */
1214 		if (count != len)
1215 			count -= (count % maxpacket);
1216 	}
1217 	qtd->hw_token = cpu_to_le32((count << 16) | token);
1218 	qtd->length = count;
1219 
1220 	return count;
1221 }
1222 
1223 static inline void qh_update(struct oxu_hcd *oxu,
1224 				struct ehci_qh *qh, struct ehci_qtd *qtd)
1225 {
1226 	/* writes to an active overlay are unsafe */
1227 	BUG_ON(qh->qh_state != QH_STATE_IDLE);
1228 
1229 	qh->hw_qtd_next = QTD_NEXT(qtd->qtd_dma);
1230 	qh->hw_alt_next = EHCI_LIST_END;
1231 
1232 	/* Except for control endpoints, we make hardware maintain data
1233 	 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
1234 	 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
1235 	 * ever clear it.
1236 	 */
1237 	if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) {
1238 		unsigned	is_out, epnum;
1239 
1240 		is_out = !(qtd->hw_token & cpu_to_le32(1 << 8));
1241 		epnum = (le32_to_cpup(&qh->hw_info1) >> 8) & 0x0f;
1242 		if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
1243 			qh->hw_token &= ~cpu_to_le32(QTD_TOGGLE);
1244 			usb_settoggle(qh->dev, epnum, is_out, 1);
1245 		}
1246 	}
1247 
1248 	/* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
1249 	wmb();
1250 	qh->hw_token &= cpu_to_le32(QTD_TOGGLE | QTD_STS_PING);
1251 }
1252 
1253 /* If it weren't for a common silicon quirk (writing the dummy into the qh
1254  * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
1255  * recovery (including urb dequeue) would need software changes to a QH...
1256  */
1257 static void qh_refresh(struct oxu_hcd *oxu, struct ehci_qh *qh)
1258 {
1259 	struct ehci_qtd *qtd;
1260 
1261 	if (list_empty(&qh->qtd_list))
1262 		qtd = qh->dummy;
1263 	else {
1264 		qtd = list_entry(qh->qtd_list.next,
1265 				struct ehci_qtd, qtd_list);
1266 		/* first qtd may already be partially processed */
1267 		if (cpu_to_le32(qtd->qtd_dma) == qh->hw_current)
1268 			qtd = NULL;
1269 	}
1270 
1271 	if (qtd)
1272 		qh_update(oxu, qh, qtd);
1273 }
1274 
1275 static void qtd_copy_status(struct oxu_hcd *oxu, struct urb *urb,
1276 				size_t length, u32 token)
1277 {
1278 	/* count IN/OUT bytes, not SETUP (even short packets) */
1279 	if (likely(QTD_PID(token) != 2))
1280 		urb->actual_length += length - QTD_LENGTH(token);
1281 
1282 	/* don't modify error codes */
1283 	if (unlikely(urb->status != -EINPROGRESS))
1284 		return;
1285 
1286 	/* force cleanup after short read; not always an error */
1287 	if (unlikely(IS_SHORT_READ(token)))
1288 		urb->status = -EREMOTEIO;
1289 
1290 	/* serious "can't proceed" faults reported by the hardware */
1291 	if (token & QTD_STS_HALT) {
1292 		if (token & QTD_STS_BABBLE) {
1293 			/* FIXME "must" disable babbling device's port too */
1294 			urb->status = -EOVERFLOW;
1295 		} else if (token & QTD_STS_MMF) {
1296 			/* fs/ls interrupt xfer missed the complete-split */
1297 			urb->status = -EPROTO;
1298 		} else if (token & QTD_STS_DBE) {
1299 			urb->status = (QTD_PID(token) == 1) /* IN ? */
1300 				? -ENOSR  /* hc couldn't read data */
1301 				: -ECOMM; /* hc couldn't write data */
1302 		} else if (token & QTD_STS_XACT) {
1303 			/* timeout, bad crc, wrong PID, etc; retried */
1304 			if (QTD_CERR(token))
1305 				urb->status = -EPIPE;
1306 			else {
1307 				oxu_dbg(oxu, "devpath %s ep%d%s 3strikes\n",
1308 					urb->dev->devpath,
1309 					usb_pipeendpoint(urb->pipe),
1310 					usb_pipein(urb->pipe) ? "in" : "out");
1311 				urb->status = -EPROTO;
1312 			}
1313 		/* CERR nonzero + no errors + halt --> stall */
1314 		} else if (QTD_CERR(token))
1315 			urb->status = -EPIPE;
1316 		else	/* unknown */
1317 			urb->status = -EPROTO;
1318 
1319 		oxu_vdbg(oxu, "dev%d ep%d%s qtd token %08x --> status %d\n",
1320 			usb_pipedevice(urb->pipe),
1321 			usb_pipeendpoint(urb->pipe),
1322 			usb_pipein(urb->pipe) ? "in" : "out",
1323 			token, urb->status);
1324 	}
1325 }
1326 
1327 static void ehci_urb_done(struct oxu_hcd *oxu, struct urb *urb)
1328 __releases(oxu->lock)
1329 __acquires(oxu->lock)
1330 {
1331 	if (likely(urb->hcpriv != NULL)) {
1332 		struct ehci_qh	*qh = (struct ehci_qh *) urb->hcpriv;
1333 
1334 		/* S-mask in a QH means it's an interrupt urb */
1335 		if ((qh->hw_info2 & cpu_to_le32(QH_SMASK)) != 0) {
1336 
1337 			/* ... update hc-wide periodic stats (for usbfs) */
1338 			oxu_to_hcd(oxu)->self.bandwidth_int_reqs--;
1339 		}
1340 		qh_put(qh);
1341 	}
1342 
1343 	urb->hcpriv = NULL;
1344 	switch (urb->status) {
1345 	case -EINPROGRESS:		/* success */
1346 		urb->status = 0;
1347 		break;
1348 	default:			/* fault */
1349 		break;
1350 	case -EREMOTEIO:		/* fault or normal */
1351 		if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
1352 			urb->status = 0;
1353 		break;
1354 	case -ECONNRESET:		/* canceled */
1355 	case -ENOENT:
1356 		break;
1357 	}
1358 
1359 #ifdef OXU_URB_TRACE
1360 	oxu_dbg(oxu, "%s %s urb %p ep%d%s status %d len %d/%d\n",
1361 		__func__, urb->dev->devpath, urb,
1362 		usb_pipeendpoint(urb->pipe),
1363 		usb_pipein(urb->pipe) ? "in" : "out",
1364 		urb->status,
1365 		urb->actual_length, urb->transfer_buffer_length);
1366 #endif
1367 
1368 	/* complete() can reenter this HCD */
1369 	spin_unlock(&oxu->lock);
1370 	usb_hcd_giveback_urb(oxu_to_hcd(oxu), urb, urb->status);
1371 	spin_lock(&oxu->lock);
1372 }
1373 
1374 static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
1375 static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
1376 
1377 static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
1378 static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
1379 
1380 #define HALT_BIT cpu_to_le32(QTD_STS_HALT)
1381 
1382 /* Process and free completed qtds for a qh, returning URBs to drivers.
1383  * Chases up to qh->hw_current.  Returns number of completions called,
1384  * indicating how much "real" work we did.
1385  */
1386 static unsigned qh_completions(struct oxu_hcd *oxu, struct ehci_qh *qh)
1387 {
1388 	struct ehci_qtd *last = NULL, *end = qh->dummy;
1389 	struct ehci_qtd	*qtd, *tmp;
1390 	int stopped;
1391 	unsigned count = 0;
1392 	int do_status = 0;
1393 	u8 state;
1394 	struct oxu_murb *murb = NULL;
1395 
1396 	if (unlikely(list_empty(&qh->qtd_list)))
1397 		return count;
1398 
1399 	/* completions (or tasks on other cpus) must never clobber HALT
1400 	 * till we've gone through and cleaned everything up, even when
1401 	 * they add urbs to this qh's queue or mark them for unlinking.
1402 	 *
1403 	 * NOTE:  unlinking expects to be done in queue order.
1404 	 */
1405 	state = qh->qh_state;
1406 	qh->qh_state = QH_STATE_COMPLETING;
1407 	stopped = (state == QH_STATE_IDLE);
1408 
1409 	/* remove de-activated QTDs from front of queue.
1410 	 * after faults (including short reads), cleanup this urb
1411 	 * then let the queue advance.
1412 	 * if queue is stopped, handles unlinks.
1413 	 */
1414 	list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
1415 		struct urb *urb;
1416 		u32 token = 0;
1417 
1418 		urb = qtd->urb;
1419 
1420 		/* Clean up any state from previous QTD ...*/
1421 		if (last) {
1422 			if (likely(last->urb != urb)) {
1423 				if (last->urb->complete == NULL) {
1424 					murb = (struct oxu_murb *) last->urb;
1425 					last->urb = murb->main;
1426 					if (murb->last) {
1427 						ehci_urb_done(oxu, last->urb);
1428 						count++;
1429 					}
1430 					oxu_murb_free(oxu, murb);
1431 				} else {
1432 					ehci_urb_done(oxu, last->urb);
1433 					count++;
1434 				}
1435 			}
1436 			oxu_qtd_free(oxu, last);
1437 			last = NULL;
1438 		}
1439 
1440 		/* ignore urbs submitted during completions we reported */
1441 		if (qtd == end)
1442 			break;
1443 
1444 		/* hardware copies qtd out of qh overlay */
1445 		rmb();
1446 		token = le32_to_cpu(qtd->hw_token);
1447 
1448 		/* always clean up qtds the hc de-activated */
1449 		if ((token & QTD_STS_ACTIVE) == 0) {
1450 
1451 			if ((token & QTD_STS_HALT) != 0) {
1452 				stopped = 1;
1453 
1454 			/* magic dummy for some short reads; qh won't advance.
1455 			 * that silicon quirk can kick in with this dummy too.
1456 			 */
1457 			} else if (IS_SHORT_READ(token) &&
1458 					!(qtd->hw_alt_next & EHCI_LIST_END)) {
1459 				stopped = 1;
1460 				goto halt;
1461 			}
1462 
1463 		/* stop scanning when we reach qtds the hc is using */
1464 		} else if (likely(!stopped &&
1465 				HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) {
1466 			break;
1467 
1468 		} else {
1469 			stopped = 1;
1470 
1471 			if (unlikely(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)))
1472 				urb->status = -ESHUTDOWN;
1473 
1474 			/* ignore active urbs unless some previous qtd
1475 			 * for the urb faulted (including short read) or
1476 			 * its urb was canceled.  we may patch qh or qtds.
1477 			 */
1478 			if (likely(urb->status == -EINPROGRESS))
1479 				continue;
1480 
1481 			/* issue status after short control reads */
1482 			if (unlikely(do_status != 0)
1483 					&& QTD_PID(token) == 0 /* OUT */) {
1484 				do_status = 0;
1485 				continue;
1486 			}
1487 
1488 			/* token in overlay may be most current */
1489 			if (state == QH_STATE_IDLE
1490 					&& cpu_to_le32(qtd->qtd_dma)
1491 						== qh->hw_current)
1492 				token = le32_to_cpu(qh->hw_token);
1493 
1494 			/* force halt for unlinked or blocked qh, so we'll
1495 			 * patch the qh later and so that completions can't
1496 			 * activate it while we "know" it's stopped.
1497 			 */
1498 			if ((HALT_BIT & qh->hw_token) == 0) {
1499 halt:
1500 				qh->hw_token |= HALT_BIT;
1501 				wmb();
1502 			}
1503 		}
1504 
1505 		/* Remove it from the queue */
1506 		qtd_copy_status(oxu, urb->complete ?
1507 					urb : ((struct oxu_murb *) urb)->main,
1508 				qtd->length, token);
1509 		if ((usb_pipein(qtd->urb->pipe)) &&
1510 				(NULL != qtd->transfer_buffer))
1511 			memcpy(qtd->transfer_buffer, qtd->buffer, qtd->length);
1512 		do_status = (urb->status == -EREMOTEIO)
1513 				&& usb_pipecontrol(urb->pipe);
1514 
1515 		if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
1516 			last = list_entry(qtd->qtd_list.prev,
1517 					struct ehci_qtd, qtd_list);
1518 			last->hw_next = qtd->hw_next;
1519 		}
1520 		list_del(&qtd->qtd_list);
1521 		last = qtd;
1522 	}
1523 
1524 	/* last urb's completion might still need calling */
1525 	if (likely(last != NULL)) {
1526 		if (last->urb->complete == NULL) {
1527 			murb = (struct oxu_murb *) last->urb;
1528 			last->urb = murb->main;
1529 			if (murb->last) {
1530 				ehci_urb_done(oxu, last->urb);
1531 				count++;
1532 			}
1533 			oxu_murb_free(oxu, murb);
1534 		} else {
1535 			ehci_urb_done(oxu, last->urb);
1536 			count++;
1537 		}
1538 		oxu_qtd_free(oxu, last);
1539 	}
1540 
1541 	/* restore original state; caller must unlink or relink */
1542 	qh->qh_state = state;
1543 
1544 	/* be sure the hardware's done with the qh before refreshing
1545 	 * it after fault cleanup, or recovering from silicon wrongly
1546 	 * overlaying the dummy qtd (which reduces DMA chatter).
1547 	 */
1548 	if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) {
1549 		switch (state) {
1550 		case QH_STATE_IDLE:
1551 			qh_refresh(oxu, qh);
1552 			break;
1553 		case QH_STATE_LINKED:
1554 			/* should be rare for periodic transfers,
1555 			 * except maybe high bandwidth ...
1556 			 */
1557 			if ((cpu_to_le32(QH_SMASK)
1558 					& qh->hw_info2) != 0) {
1559 				intr_deschedule(oxu, qh);
1560 				(void) qh_schedule(oxu, qh);
1561 			} else
1562 				unlink_async(oxu, qh);
1563 			break;
1564 		/* otherwise, unlink already started */
1565 		}
1566 	}
1567 
1568 	return count;
1569 }
1570 
1571 /* High bandwidth multiplier, as encoded in highspeed endpoint descriptors */
1572 #define hb_mult(wMaxPacketSize)		(1 + (((wMaxPacketSize) >> 11) & 0x03))
1573 /* ... and packet size, for any kind of endpoint descriptor */
1574 #define max_packet(wMaxPacketSize)	((wMaxPacketSize) & 0x07ff)
1575 
1576 /* Reverse of qh_urb_transaction: free a list of TDs.
1577  * used for cleanup after errors, before HC sees an URB's TDs.
1578  */
1579 static void qtd_list_free(struct oxu_hcd *oxu,
1580 				struct urb *urb, struct list_head *head)
1581 {
1582 	struct ehci_qtd	*qtd, *temp;
1583 
1584 	list_for_each_entry_safe(qtd, temp, head, qtd_list) {
1585 		list_del(&qtd->qtd_list);
1586 		oxu_qtd_free(oxu, qtd);
1587 	}
1588 }
1589 
1590 /* Create a list of filled qtds for this URB; won't link into qh.
1591  */
1592 static struct list_head *qh_urb_transaction(struct oxu_hcd *oxu,
1593 						struct urb *urb,
1594 						struct list_head *head,
1595 						gfp_t flags)
1596 {
1597 	struct ehci_qtd	*qtd, *qtd_prev;
1598 	dma_addr_t buf;
1599 	int len, maxpacket;
1600 	int is_input;
1601 	u32 token;
1602 	void *transfer_buf = NULL;
1603 	int ret;
1604 
1605 	/*
1606 	 * URBs map to sequences of QTDs: one logical transaction
1607 	 */
1608 	qtd = ehci_qtd_alloc(oxu);
1609 	if (unlikely(!qtd))
1610 		return NULL;
1611 	list_add_tail(&qtd->qtd_list, head);
1612 	qtd->urb = urb;
1613 
1614 	token = QTD_STS_ACTIVE;
1615 	token |= (EHCI_TUNE_CERR << 10);
1616 	/* for split transactions, SplitXState initialized to zero */
1617 
1618 	len = urb->transfer_buffer_length;
1619 	is_input = usb_pipein(urb->pipe);
1620 	if (!urb->transfer_buffer && urb->transfer_buffer_length && is_input)
1621 		urb->transfer_buffer = phys_to_virt(urb->transfer_dma);
1622 
1623 	if (usb_pipecontrol(urb->pipe)) {
1624 		/* SETUP pid */
1625 		ret = oxu_buf_alloc(oxu, qtd, sizeof(struct usb_ctrlrequest));
1626 		if (ret)
1627 			goto cleanup;
1628 
1629 		qtd_fill(qtd, qtd->buffer_dma, sizeof(struct usb_ctrlrequest),
1630 				token | (2 /* "setup" */ << 8), 8);
1631 		memcpy(qtd->buffer, qtd->urb->setup_packet,
1632 				sizeof(struct usb_ctrlrequest));
1633 
1634 		/* ... and always at least one more pid */
1635 		token ^= QTD_TOGGLE;
1636 		qtd_prev = qtd;
1637 		qtd = ehci_qtd_alloc(oxu);
1638 		if (unlikely(!qtd))
1639 			goto cleanup;
1640 		qtd->urb = urb;
1641 		qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
1642 		list_add_tail(&qtd->qtd_list, head);
1643 
1644 		/* for zero length DATA stages, STATUS is always IN */
1645 		if (len == 0)
1646 			token |= (1 /* "in" */ << 8);
1647 	}
1648 
1649 	/*
1650 	 * Data transfer stage: buffer setup
1651 	 */
1652 
1653 	ret = oxu_buf_alloc(oxu, qtd, len);
1654 	if (ret)
1655 		goto cleanup;
1656 
1657 	buf = qtd->buffer_dma;
1658 	transfer_buf = urb->transfer_buffer;
1659 
1660 	if (!is_input)
1661 		memcpy(qtd->buffer, qtd->urb->transfer_buffer, len);
1662 
1663 	if (is_input)
1664 		token |= (1 /* "in" */ << 8);
1665 	/* else it's already initted to "out" pid (0 << 8) */
1666 
1667 	maxpacket = usb_maxpacket(urb->dev, urb->pipe);
1668 
1669 	/*
1670 	 * buffer gets wrapped in one or more qtds;
1671 	 * last one may be "short" (including zero len)
1672 	 * and may serve as a control status ack
1673 	 */
1674 	for (;;) {
1675 		int this_qtd_len;
1676 
1677 		this_qtd_len = qtd_fill(qtd, buf, len, token, maxpacket);
1678 		qtd->transfer_buffer = transfer_buf;
1679 		len -= this_qtd_len;
1680 		buf += this_qtd_len;
1681 		transfer_buf += this_qtd_len;
1682 		if (is_input)
1683 			qtd->hw_alt_next = oxu->async->hw_alt_next;
1684 
1685 		/* qh makes control packets use qtd toggle; maybe switch it */
1686 		if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
1687 			token ^= QTD_TOGGLE;
1688 
1689 		if (likely(len <= 0))
1690 			break;
1691 
1692 		qtd_prev = qtd;
1693 		qtd = ehci_qtd_alloc(oxu);
1694 		if (unlikely(!qtd))
1695 			goto cleanup;
1696 		if (likely(len > 0)) {
1697 			ret = oxu_buf_alloc(oxu, qtd, len);
1698 			if (ret)
1699 				goto cleanup;
1700 		}
1701 		qtd->urb = urb;
1702 		qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
1703 		list_add_tail(&qtd->qtd_list, head);
1704 	}
1705 
1706 	/* unless the bulk/interrupt caller wants a chance to clean
1707 	 * up after short reads, hc should advance qh past this urb
1708 	 */
1709 	if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
1710 				|| usb_pipecontrol(urb->pipe)))
1711 		qtd->hw_alt_next = EHCI_LIST_END;
1712 
1713 	/*
1714 	 * control requests may need a terminating data "status" ack;
1715 	 * bulk ones may need a terminating short packet (zero length).
1716 	 */
1717 	if (likely(urb->transfer_buffer_length != 0)) {
1718 		int	one_more = 0;
1719 
1720 		if (usb_pipecontrol(urb->pipe)) {
1721 			one_more = 1;
1722 			token ^= 0x0100;	/* "in" <--> "out"  */
1723 			token |= QTD_TOGGLE;	/* force DATA1 */
1724 		} else if (usb_pipebulk(urb->pipe)
1725 				&& (urb->transfer_flags & URB_ZERO_PACKET)
1726 				&& !(urb->transfer_buffer_length % maxpacket)) {
1727 			one_more = 1;
1728 		}
1729 		if (one_more) {
1730 			qtd_prev = qtd;
1731 			qtd = ehci_qtd_alloc(oxu);
1732 			if (unlikely(!qtd))
1733 				goto cleanup;
1734 			qtd->urb = urb;
1735 			qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
1736 			list_add_tail(&qtd->qtd_list, head);
1737 
1738 			/* never any data in such packets */
1739 			qtd_fill(qtd, 0, 0, token, 0);
1740 		}
1741 	}
1742 
1743 	/* by default, enable interrupt on urb completion */
1744 	qtd->hw_token |= cpu_to_le32(QTD_IOC);
1745 	return head;
1746 
1747 cleanup:
1748 	qtd_list_free(oxu, urb, head);
1749 	return NULL;
1750 }
1751 
1752 /* Each QH holds a qtd list; a QH is used for everything except iso.
1753  *
1754  * For interrupt urbs, the scheduler must set the microframe scheduling
1755  * mask(s) each time the QH gets scheduled.  For highspeed, that's
1756  * just one microframe in the s-mask.  For split interrupt transactions
1757  * there are additional complications: c-mask, maybe FSTNs.
1758  */
1759 static struct ehci_qh *qh_make(struct oxu_hcd *oxu,
1760 				struct urb *urb, gfp_t flags)
1761 {
1762 	struct ehci_qh *qh = oxu_qh_alloc(oxu);
1763 	u32 info1 = 0, info2 = 0;
1764 	int is_input, type;
1765 	int maxp = 0;
1766 
1767 	if (!qh)
1768 		return qh;
1769 
1770 	/*
1771 	 * init endpoint/device data for this QH
1772 	 */
1773 	info1 |= usb_pipeendpoint(urb->pipe) << 8;
1774 	info1 |= usb_pipedevice(urb->pipe) << 0;
1775 
1776 	is_input = usb_pipein(urb->pipe);
1777 	type = usb_pipetype(urb->pipe);
1778 	maxp = usb_maxpacket(urb->dev, urb->pipe);
1779 
1780 	/* Compute interrupt scheduling parameters just once, and save.
1781 	 * - allowing for high bandwidth, how many nsec/uframe are used?
1782 	 * - split transactions need a second CSPLIT uframe; same question
1783 	 * - splits also need a schedule gap (for full/low speed I/O)
1784 	 * - qh has a polling interval
1785 	 *
1786 	 * For control/bulk requests, the HC or TT handles these.
1787 	 */
1788 	if (type == PIPE_INTERRUPT) {
1789 		qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
1790 								is_input, 0,
1791 				hb_mult(maxp) * max_packet(maxp)));
1792 		qh->start = NO_FRAME;
1793 
1794 		if (urb->dev->speed == USB_SPEED_HIGH) {
1795 			qh->c_usecs = 0;
1796 			qh->gap_uf = 0;
1797 
1798 			qh->period = urb->interval >> 3;
1799 			if (qh->period == 0 && urb->interval != 1) {
1800 				/* NOTE interval 2 or 4 uframes could work.
1801 				 * But interval 1 scheduling is simpler, and
1802 				 * includes high bandwidth.
1803 				 */
1804 				oxu_dbg(oxu, "intr period %d uframes, NYET!\n",
1805 					urb->interval);
1806 				goto done;
1807 			}
1808 		} else {
1809 			struct usb_tt	*tt = urb->dev->tt;
1810 			int		think_time;
1811 
1812 			/* gap is f(FS/LS transfer times) */
1813 			qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
1814 					is_input, 0, maxp) / (125 * 1000);
1815 
1816 			/* FIXME this just approximates SPLIT/CSPLIT times */
1817 			if (is_input) {		/* SPLIT, gap, CSPLIT+DATA */
1818 				qh->c_usecs = qh->usecs + HS_USECS(0);
1819 				qh->usecs = HS_USECS(1);
1820 			} else {		/* SPLIT+DATA, gap, CSPLIT */
1821 				qh->usecs += HS_USECS(1);
1822 				qh->c_usecs = HS_USECS(0);
1823 			}
1824 
1825 			think_time = tt ? tt->think_time : 0;
1826 			qh->tt_usecs = NS_TO_US(think_time +
1827 					usb_calc_bus_time(urb->dev->speed,
1828 					is_input, 0, max_packet(maxp)));
1829 			qh->period = urb->interval;
1830 		}
1831 	}
1832 
1833 	/* support for tt scheduling, and access to toggles */
1834 	qh->dev = urb->dev;
1835 
1836 	/* using TT? */
1837 	switch (urb->dev->speed) {
1838 	case USB_SPEED_LOW:
1839 		info1 |= (1 << 12);	/* EPS "low" */
1840 		fallthrough;
1841 
1842 	case USB_SPEED_FULL:
1843 		/* EPS 0 means "full" */
1844 		if (type != PIPE_INTERRUPT)
1845 			info1 |= (EHCI_TUNE_RL_TT << 28);
1846 		if (type == PIPE_CONTROL) {
1847 			info1 |= (1 << 27);	/* for TT */
1848 			info1 |= 1 << 14;	/* toggle from qtd */
1849 		}
1850 		info1 |= maxp << 16;
1851 
1852 		info2 |= (EHCI_TUNE_MULT_TT << 30);
1853 		info2 |= urb->dev->ttport << 23;
1854 
1855 		/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets c-mask } */
1856 
1857 		break;
1858 
1859 	case USB_SPEED_HIGH:		/* no TT involved */
1860 		info1 |= (2 << 12);	/* EPS "high" */
1861 		if (type == PIPE_CONTROL) {
1862 			info1 |= (EHCI_TUNE_RL_HS << 28);
1863 			info1 |= 64 << 16;	/* usb2 fixed maxpacket */
1864 			info1 |= 1 << 14;	/* toggle from qtd */
1865 			info2 |= (EHCI_TUNE_MULT_HS << 30);
1866 		} else if (type == PIPE_BULK) {
1867 			info1 |= (EHCI_TUNE_RL_HS << 28);
1868 			info1 |= 512 << 16;	/* usb2 fixed maxpacket */
1869 			info2 |= (EHCI_TUNE_MULT_HS << 30);
1870 		} else {		/* PIPE_INTERRUPT */
1871 			info1 |= max_packet(maxp) << 16;
1872 			info2 |= hb_mult(maxp) << 30;
1873 		}
1874 		break;
1875 	default:
1876 		oxu_dbg(oxu, "bogus dev %p speed %d\n", urb->dev, urb->dev->speed);
1877 done:
1878 		qh_put(qh);
1879 		return NULL;
1880 	}
1881 
1882 	/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets s-mask } */
1883 
1884 	/* init as live, toggle clear, advance to dummy */
1885 	qh->qh_state = QH_STATE_IDLE;
1886 	qh->hw_info1 = cpu_to_le32(info1);
1887 	qh->hw_info2 = cpu_to_le32(info2);
1888 	usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
1889 	qh_refresh(oxu, qh);
1890 	return qh;
1891 }
1892 
1893 /* Move qh (and its qtds) onto async queue; maybe enable queue.
1894  */
1895 static void qh_link_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
1896 {
1897 	__le32 dma = QH_NEXT(qh->qh_dma);
1898 	struct ehci_qh *head;
1899 
1900 	/* (re)start the async schedule? */
1901 	head = oxu->async;
1902 	timer_action_done(oxu, TIMER_ASYNC_OFF);
1903 	if (!head->qh_next.qh) {
1904 		u32	cmd = readl(&oxu->regs->command);
1905 
1906 		if (!(cmd & CMD_ASE)) {
1907 			/* in case a clear of CMD_ASE didn't take yet */
1908 			(void)handshake(oxu, &oxu->regs->status,
1909 					STS_ASS, 0, 150);
1910 			cmd |= CMD_ASE | CMD_RUN;
1911 			writel(cmd, &oxu->regs->command);
1912 			oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
1913 			/* posted write need not be known to HC yet ... */
1914 		}
1915 	}
1916 
1917 	/* clear halt and/or toggle; and maybe recover from silicon quirk */
1918 	if (qh->qh_state == QH_STATE_IDLE)
1919 		qh_refresh(oxu, qh);
1920 
1921 	/* splice right after start */
1922 	qh->qh_next = head->qh_next;
1923 	qh->hw_next = head->hw_next;
1924 	wmb();
1925 
1926 	head->qh_next.qh = qh;
1927 	head->hw_next = dma;
1928 
1929 	qh->qh_state = QH_STATE_LINKED;
1930 	/* qtd completions reported later by interrupt */
1931 }
1932 
1933 #define	QH_ADDR_MASK	cpu_to_le32(0x7f)
1934 
1935 /*
1936  * For control/bulk/interrupt, return QH with these TDs appended.
1937  * Allocates and initializes the QH if necessary.
1938  * Returns null if it can't allocate a QH it needs to.
1939  * If the QH has TDs (urbs) already, that's great.
1940  */
1941 static struct ehci_qh *qh_append_tds(struct oxu_hcd *oxu,
1942 				struct urb *urb, struct list_head *qtd_list,
1943 				int epnum, void	**ptr)
1944 {
1945 	struct ehci_qh *qh = NULL;
1946 
1947 	qh = (struct ehci_qh *) *ptr;
1948 	if (unlikely(qh == NULL)) {
1949 		/* can't sleep here, we have oxu->lock... */
1950 		qh = qh_make(oxu, urb, GFP_ATOMIC);
1951 		*ptr = qh;
1952 	}
1953 	if (likely(qh != NULL)) {
1954 		struct ehci_qtd	*qtd;
1955 
1956 		if (unlikely(list_empty(qtd_list)))
1957 			qtd = NULL;
1958 		else
1959 			qtd = list_entry(qtd_list->next, struct ehci_qtd,
1960 					qtd_list);
1961 
1962 		/* control qh may need patching ... */
1963 		if (unlikely(epnum == 0)) {
1964 
1965 			/* usb_reset_device() briefly reverts to address 0 */
1966 			if (usb_pipedevice(urb->pipe) == 0)
1967 				qh->hw_info1 &= ~QH_ADDR_MASK;
1968 		}
1969 
1970 		/* just one way to queue requests: swap with the dummy qtd.
1971 		 * only hc or qh_refresh() ever modify the overlay.
1972 		 */
1973 		if (likely(qtd != NULL)) {
1974 			struct ehci_qtd	*dummy;
1975 			dma_addr_t dma;
1976 			__le32 token;
1977 
1978 			/* to avoid racing the HC, use the dummy td instead of
1979 			 * the first td of our list (becomes new dummy).  both
1980 			 * tds stay deactivated until we're done, when the
1981 			 * HC is allowed to fetch the old dummy (4.10.2).
1982 			 */
1983 			token = qtd->hw_token;
1984 			qtd->hw_token = HALT_BIT;
1985 			wmb();
1986 			dummy = qh->dummy;
1987 
1988 			dma = dummy->qtd_dma;
1989 			*dummy = *qtd;
1990 			dummy->qtd_dma = dma;
1991 
1992 			list_del(&qtd->qtd_list);
1993 			list_add(&dummy->qtd_list, qtd_list);
1994 			list_splice(qtd_list, qh->qtd_list.prev);
1995 
1996 			ehci_qtd_init(qtd, qtd->qtd_dma);
1997 			qh->dummy = qtd;
1998 
1999 			/* hc must see the new dummy at list end */
2000 			dma = qtd->qtd_dma;
2001 			qtd = list_entry(qh->qtd_list.prev,
2002 					struct ehci_qtd, qtd_list);
2003 			qtd->hw_next = QTD_NEXT(dma);
2004 
2005 			/* let the hc process these next qtds */
2006 			dummy->hw_token = (token & ~(0x80));
2007 			wmb();
2008 			dummy->hw_token = token;
2009 
2010 			urb->hcpriv = qh_get(qh);
2011 		}
2012 	}
2013 	return qh;
2014 }
2015 
2016 static int submit_async(struct oxu_hcd	*oxu, struct urb *urb,
2017 			struct list_head *qtd_list, gfp_t mem_flags)
2018 {
2019 	int epnum = urb->ep->desc.bEndpointAddress;
2020 	unsigned long flags;
2021 	struct ehci_qh *qh = NULL;
2022 	int rc = 0;
2023 #ifdef OXU_URB_TRACE
2024 	struct ehci_qtd	*qtd;
2025 
2026 	qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
2027 
2028 	oxu_dbg(oxu, "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2029 		__func__, urb->dev->devpath, urb,
2030 		epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
2031 		urb->transfer_buffer_length,
2032 		qtd, urb->ep->hcpriv);
2033 #endif
2034 
2035 	spin_lock_irqsave(&oxu->lock, flags);
2036 	if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
2037 		rc = -ESHUTDOWN;
2038 		goto done;
2039 	}
2040 
2041 	qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
2042 	if (unlikely(qh == NULL)) {
2043 		rc = -ENOMEM;
2044 		goto done;
2045 	}
2046 
2047 	/* Control/bulk operations through TTs don't need scheduling,
2048 	 * the HC and TT handle it when the TT has a buffer ready.
2049 	 */
2050 	if (likely(qh->qh_state == QH_STATE_IDLE))
2051 		qh_link_async(oxu, qh_get(qh));
2052 done:
2053 	spin_unlock_irqrestore(&oxu->lock, flags);
2054 	if (unlikely(qh == NULL))
2055 		qtd_list_free(oxu, urb, qtd_list);
2056 	return rc;
2057 }
2058 
2059 /* The async qh for the qtds being reclaimed are now unlinked from the HC */
2060 
2061 static void end_unlink_async(struct oxu_hcd *oxu)
2062 {
2063 	struct ehci_qh *qh = oxu->reclaim;
2064 	struct ehci_qh *next;
2065 
2066 	timer_action_done(oxu, TIMER_IAA_WATCHDOG);
2067 
2068 	qh->qh_state = QH_STATE_IDLE;
2069 	qh->qh_next.qh = NULL;
2070 	qh_put(qh);			/* refcount from reclaim */
2071 
2072 	/* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
2073 	next = qh->reclaim;
2074 	oxu->reclaim = next;
2075 	oxu->reclaim_ready = 0;
2076 	qh->reclaim = NULL;
2077 
2078 	qh_completions(oxu, qh);
2079 
2080 	if (!list_empty(&qh->qtd_list)
2081 			&& HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
2082 		qh_link_async(oxu, qh);
2083 	else {
2084 		qh_put(qh);		/* refcount from async list */
2085 
2086 		/* it's not free to turn the async schedule on/off; leave it
2087 		 * active but idle for a while once it empties.
2088 		 */
2089 		if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state)
2090 				&& oxu->async->qh_next.qh == NULL)
2091 			timer_action(oxu, TIMER_ASYNC_OFF);
2092 	}
2093 
2094 	if (next) {
2095 		oxu->reclaim = NULL;
2096 		start_unlink_async(oxu, next);
2097 	}
2098 }
2099 
2100 /* makes sure the async qh will become idle */
2101 /* caller must own oxu->lock */
2102 
2103 static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
2104 {
2105 	int cmd = readl(&oxu->regs->command);
2106 	struct ehci_qh *prev;
2107 
2108 #ifdef DEBUG
2109 	assert_spin_locked(&oxu->lock);
2110 	BUG_ON(oxu->reclaim || (qh->qh_state != QH_STATE_LINKED
2111 				&& qh->qh_state != QH_STATE_UNLINK_WAIT));
2112 #endif
2113 
2114 	/* stop async schedule right now? */
2115 	if (unlikely(qh == oxu->async)) {
2116 		/* can't get here without STS_ASS set */
2117 		if (oxu_to_hcd(oxu)->state != HC_STATE_HALT
2118 				&& !oxu->reclaim) {
2119 			/* ... and CMD_IAAD clear */
2120 			writel(cmd & ~CMD_ASE, &oxu->regs->command);
2121 			wmb();
2122 			/* handshake later, if we need to */
2123 			timer_action_done(oxu, TIMER_ASYNC_OFF);
2124 		}
2125 		return;
2126 	}
2127 
2128 	qh->qh_state = QH_STATE_UNLINK;
2129 	oxu->reclaim = qh = qh_get(qh);
2130 
2131 	prev = oxu->async;
2132 	while (prev->qh_next.qh != qh)
2133 		prev = prev->qh_next.qh;
2134 
2135 	prev->hw_next = qh->hw_next;
2136 	prev->qh_next = qh->qh_next;
2137 	wmb();
2138 
2139 	if (unlikely(oxu_to_hcd(oxu)->state == HC_STATE_HALT)) {
2140 		/* if (unlikely(qh->reclaim != 0))
2141 		 *	this will recurse, probably not much
2142 		 */
2143 		end_unlink_async(oxu);
2144 		return;
2145 	}
2146 
2147 	oxu->reclaim_ready = 0;
2148 	cmd |= CMD_IAAD;
2149 	writel(cmd, &oxu->regs->command);
2150 	(void) readl(&oxu->regs->command);
2151 	timer_action(oxu, TIMER_IAA_WATCHDOG);
2152 }
2153 
2154 static void scan_async(struct oxu_hcd *oxu)
2155 {
2156 	struct ehci_qh *qh;
2157 	enum ehci_timer_action action = TIMER_IO_WATCHDOG;
2158 
2159 	if (!++(oxu->stamp))
2160 		oxu->stamp++;
2161 	timer_action_done(oxu, TIMER_ASYNC_SHRINK);
2162 rescan:
2163 	qh = oxu->async->qh_next.qh;
2164 	if (likely(qh != NULL)) {
2165 		do {
2166 			/* clean any finished work for this qh */
2167 			if (!list_empty(&qh->qtd_list)
2168 					&& qh->stamp != oxu->stamp) {
2169 				int temp;
2170 
2171 				/* unlinks could happen here; completion
2172 				 * reporting drops the lock.  rescan using
2173 				 * the latest schedule, but don't rescan
2174 				 * qhs we already finished (no looping).
2175 				 */
2176 				qh = qh_get(qh);
2177 				qh->stamp = oxu->stamp;
2178 				temp = qh_completions(oxu, qh);
2179 				qh_put(qh);
2180 				if (temp != 0)
2181 					goto rescan;
2182 			}
2183 
2184 			/* unlink idle entries, reducing HC PCI usage as well
2185 			 * as HCD schedule-scanning costs.  delay for any qh
2186 			 * we just scanned, there's a not-unusual case that it
2187 			 * doesn't stay idle for long.
2188 			 * (plus, avoids some kind of re-activation race.)
2189 			 */
2190 			if (list_empty(&qh->qtd_list)) {
2191 				if (qh->stamp == oxu->stamp)
2192 					action = TIMER_ASYNC_SHRINK;
2193 				else if (!oxu->reclaim
2194 					    && qh->qh_state == QH_STATE_LINKED)
2195 					start_unlink_async(oxu, qh);
2196 			}
2197 
2198 			qh = qh->qh_next.qh;
2199 		} while (qh);
2200 	}
2201 	if (action == TIMER_ASYNC_SHRINK)
2202 		timer_action(oxu, TIMER_ASYNC_SHRINK);
2203 }
2204 
2205 /*
2206  * periodic_next_shadow - return "next" pointer on shadow list
2207  * @periodic: host pointer to qh/itd/sitd
2208  * @tag: hardware tag for type of this record
2209  */
2210 static union ehci_shadow *periodic_next_shadow(union ehci_shadow *periodic,
2211 						__le32 tag)
2212 {
2213 	switch (tag) {
2214 	default:
2215 	case Q_TYPE_QH:
2216 		return &periodic->qh->qh_next;
2217 	}
2218 }
2219 
2220 /* caller must hold oxu->lock */
2221 static void periodic_unlink(struct oxu_hcd *oxu, unsigned frame, void *ptr)
2222 {
2223 	union ehci_shadow *prev_p = &oxu->pshadow[frame];
2224 	__le32 *hw_p = &oxu->periodic[frame];
2225 	union ehci_shadow here = *prev_p;
2226 
2227 	/* find predecessor of "ptr"; hw and shadow lists are in sync */
2228 	while (here.ptr && here.ptr != ptr) {
2229 		prev_p = periodic_next_shadow(prev_p, Q_NEXT_TYPE(*hw_p));
2230 		hw_p = here.hw_next;
2231 		here = *prev_p;
2232 	}
2233 	/* an interrupt entry (at list end) could have been shared */
2234 	if (!here.ptr)
2235 		return;
2236 
2237 	/* update shadow and hardware lists ... the old "next" pointers
2238 	 * from ptr may still be in use, the caller updates them.
2239 	 */
2240 	*prev_p = *periodic_next_shadow(&here, Q_NEXT_TYPE(*hw_p));
2241 	*hw_p = *here.hw_next;
2242 }
2243 
2244 /* how many of the uframe's 125 usecs are allocated? */
2245 static unsigned short periodic_usecs(struct oxu_hcd *oxu,
2246 					unsigned frame, unsigned uframe)
2247 {
2248 	__le32 *hw_p = &oxu->periodic[frame];
2249 	union ehci_shadow *q = &oxu->pshadow[frame];
2250 	unsigned usecs = 0;
2251 
2252 	while (q->ptr) {
2253 		switch (Q_NEXT_TYPE(*hw_p)) {
2254 		case Q_TYPE_QH:
2255 		default:
2256 			/* is it in the S-mask? */
2257 			if (q->qh->hw_info2 & cpu_to_le32(1 << uframe))
2258 				usecs += q->qh->usecs;
2259 			/* ... or C-mask? */
2260 			if (q->qh->hw_info2 & cpu_to_le32(1 << (8 + uframe)))
2261 				usecs += q->qh->c_usecs;
2262 			hw_p = &q->qh->hw_next;
2263 			q = &q->qh->qh_next;
2264 			break;
2265 		}
2266 	}
2267 #ifdef DEBUG
2268 	if (usecs > 100)
2269 		oxu_err(oxu, "uframe %d sched overrun: %d usecs\n",
2270 						frame * 8 + uframe, usecs);
2271 #endif
2272 	return usecs;
2273 }
2274 
2275 static int enable_periodic(struct oxu_hcd *oxu)
2276 {
2277 	u32 cmd;
2278 	int status;
2279 
2280 	/* did clearing PSE did take effect yet?
2281 	 * takes effect only at frame boundaries...
2282 	 */
2283 	status = handshake(oxu, &oxu->regs->status, STS_PSS, 0, 9 * 125);
2284 	if (status != 0) {
2285 		oxu_to_hcd(oxu)->state = HC_STATE_HALT;
2286 		usb_hc_died(oxu_to_hcd(oxu));
2287 		return status;
2288 	}
2289 
2290 	cmd = readl(&oxu->regs->command) | CMD_PSE;
2291 	writel(cmd, &oxu->regs->command);
2292 	/* posted write ... PSS happens later */
2293 	oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
2294 
2295 	/* make sure ehci_work scans these */
2296 	oxu->next_uframe = readl(&oxu->regs->frame_index)
2297 		% (oxu->periodic_size << 3);
2298 	return 0;
2299 }
2300 
2301 static int disable_periodic(struct oxu_hcd *oxu)
2302 {
2303 	u32 cmd;
2304 	int status;
2305 
2306 	/* did setting PSE not take effect yet?
2307 	 * takes effect only at frame boundaries...
2308 	 */
2309 	status = handshake(oxu, &oxu->regs->status, STS_PSS, STS_PSS, 9 * 125);
2310 	if (status != 0) {
2311 		oxu_to_hcd(oxu)->state = HC_STATE_HALT;
2312 		usb_hc_died(oxu_to_hcd(oxu));
2313 		return status;
2314 	}
2315 
2316 	cmd = readl(&oxu->regs->command) & ~CMD_PSE;
2317 	writel(cmd, &oxu->regs->command);
2318 	/* posted write ... */
2319 
2320 	oxu->next_uframe = -1;
2321 	return 0;
2322 }
2323 
2324 /* periodic schedule slots have iso tds (normal or split) first, then a
2325  * sparse tree for active interrupt transfers.
2326  *
2327  * this just links in a qh; caller guarantees uframe masks are set right.
2328  * no FSTN support (yet; oxu 0.96+)
2329  */
2330 static int qh_link_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
2331 {
2332 	unsigned i;
2333 	unsigned period = qh->period;
2334 
2335 	dev_dbg(&qh->dev->dev,
2336 		"link qh%d-%04x/%p start %d [%d/%d us]\n",
2337 		period, le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
2338 		qh, qh->start, qh->usecs, qh->c_usecs);
2339 
2340 	/* high bandwidth, or otherwise every microframe */
2341 	if (period == 0)
2342 		period = 1;
2343 
2344 	for (i = qh->start; i < oxu->periodic_size; i += period) {
2345 		union ehci_shadow	*prev = &oxu->pshadow[i];
2346 		__le32			*hw_p = &oxu->periodic[i];
2347 		union ehci_shadow	here = *prev;
2348 		__le32			type = 0;
2349 
2350 		/* skip the iso nodes at list head */
2351 		while (here.ptr) {
2352 			type = Q_NEXT_TYPE(*hw_p);
2353 			if (type == Q_TYPE_QH)
2354 				break;
2355 			prev = periodic_next_shadow(prev, type);
2356 			hw_p = &here.qh->hw_next;
2357 			here = *prev;
2358 		}
2359 
2360 		/* sorting each branch by period (slow-->fast)
2361 		 * enables sharing interior tree nodes
2362 		 */
2363 		while (here.ptr && qh != here.qh) {
2364 			if (qh->period > here.qh->period)
2365 				break;
2366 			prev = &here.qh->qh_next;
2367 			hw_p = &here.qh->hw_next;
2368 			here = *prev;
2369 		}
2370 		/* link in this qh, unless some earlier pass did that */
2371 		if (qh != here.qh) {
2372 			qh->qh_next = here;
2373 			if (here.qh)
2374 				qh->hw_next = *hw_p;
2375 			wmb();
2376 			prev->qh = qh;
2377 			*hw_p = QH_NEXT(qh->qh_dma);
2378 		}
2379 	}
2380 	qh->qh_state = QH_STATE_LINKED;
2381 	qh_get(qh);
2382 
2383 	/* update per-qh bandwidth for usbfs */
2384 	oxu_to_hcd(oxu)->self.bandwidth_allocated += qh->period
2385 		? ((qh->usecs + qh->c_usecs) / qh->period)
2386 		: (qh->usecs * 8);
2387 
2388 	/* maybe enable periodic schedule processing */
2389 	if (!oxu->periodic_sched++)
2390 		return enable_periodic(oxu);
2391 
2392 	return 0;
2393 }
2394 
2395 static void qh_unlink_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
2396 {
2397 	unsigned i;
2398 	unsigned period;
2399 
2400 	/* FIXME:
2401 	 *   IF this isn't high speed
2402 	 *   and this qh is active in the current uframe
2403 	 *   (and overlay token SplitXstate is false?)
2404 	 * THEN
2405 	 *   qh->hw_info1 |= cpu_to_le32(1 << 7 "ignore");
2406 	 */
2407 
2408 	/* high bandwidth, or otherwise part of every microframe */
2409 	period = qh->period;
2410 	if (period == 0)
2411 		period = 1;
2412 
2413 	for (i = qh->start; i < oxu->periodic_size; i += period)
2414 		periodic_unlink(oxu, i, qh);
2415 
2416 	/* update per-qh bandwidth for usbfs */
2417 	oxu_to_hcd(oxu)->self.bandwidth_allocated -= qh->period
2418 		? ((qh->usecs + qh->c_usecs) / qh->period)
2419 		: (qh->usecs * 8);
2420 
2421 	dev_dbg(&qh->dev->dev,
2422 		"unlink qh%d-%04x/%p start %d [%d/%d us]\n",
2423 		qh->period,
2424 		le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
2425 		qh, qh->start, qh->usecs, qh->c_usecs);
2426 
2427 	/* qh->qh_next still "live" to HC */
2428 	qh->qh_state = QH_STATE_UNLINK;
2429 	qh->qh_next.ptr = NULL;
2430 	qh_put(qh);
2431 
2432 	/* maybe turn off periodic schedule */
2433 	oxu->periodic_sched--;
2434 	if (!oxu->periodic_sched)
2435 		(void) disable_periodic(oxu);
2436 }
2437 
2438 static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
2439 {
2440 	unsigned wait;
2441 
2442 	qh_unlink_periodic(oxu, qh);
2443 
2444 	/* simple/paranoid:  always delay, expecting the HC needs to read
2445 	 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
2446 	 * expect hub_wq to clean up after any CSPLITs we won't issue.
2447 	 * active high speed queues may need bigger delays...
2448 	 */
2449 	if (list_empty(&qh->qtd_list)
2450 		|| (cpu_to_le32(QH_CMASK) & qh->hw_info2) != 0)
2451 		wait = 2;
2452 	else
2453 		wait = 55;	/* worst case: 3 * 1024 */
2454 
2455 	udelay(wait);
2456 	qh->qh_state = QH_STATE_IDLE;
2457 	qh->hw_next = EHCI_LIST_END;
2458 	wmb();
2459 }
2460 
2461 static int check_period(struct oxu_hcd *oxu,
2462 			unsigned frame, unsigned uframe,
2463 			unsigned period, unsigned usecs)
2464 {
2465 	int claimed;
2466 
2467 	/* complete split running into next frame?
2468 	 * given FSTN support, we could sometimes check...
2469 	 */
2470 	if (uframe >= 8)
2471 		return 0;
2472 
2473 	/*
2474 	 * 80% periodic == 100 usec/uframe available
2475 	 * convert "usecs we need" to "max already claimed"
2476 	 */
2477 	usecs = 100 - usecs;
2478 
2479 	/* we "know" 2 and 4 uframe intervals were rejected; so
2480 	 * for period 0, check _every_ microframe in the schedule.
2481 	 */
2482 	if (unlikely(period == 0)) {
2483 		do {
2484 			for (uframe = 0; uframe < 7; uframe++) {
2485 				claimed = periodic_usecs(oxu, frame, uframe);
2486 				if (claimed > usecs)
2487 					return 0;
2488 			}
2489 		} while ((frame += 1) < oxu->periodic_size);
2490 
2491 	/* just check the specified uframe, at that period */
2492 	} else {
2493 		do {
2494 			claimed = periodic_usecs(oxu, frame, uframe);
2495 			if (claimed > usecs)
2496 				return 0;
2497 		} while ((frame += period) < oxu->periodic_size);
2498 	}
2499 
2500 	return 1;
2501 }
2502 
2503 static int check_intr_schedule(struct oxu_hcd	*oxu,
2504 				unsigned frame, unsigned uframe,
2505 				const struct ehci_qh *qh, __le32 *c_maskp)
2506 {
2507 	int retval = -ENOSPC;
2508 
2509 	if (qh->c_usecs && uframe >= 6)		/* FSTN territory? */
2510 		goto done;
2511 
2512 	if (!check_period(oxu, frame, uframe, qh->period, qh->usecs))
2513 		goto done;
2514 	if (!qh->c_usecs) {
2515 		retval = 0;
2516 		*c_maskp = 0;
2517 		goto done;
2518 	}
2519 
2520 done:
2521 	return retval;
2522 }
2523 
2524 /* "first fit" scheduling policy used the first time through,
2525  * or when the previous schedule slot can't be re-used.
2526  */
2527 static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
2528 {
2529 	int		status;
2530 	unsigned	uframe;
2531 	__le32		c_mask;
2532 	unsigned	frame;		/* 0..(qh->period - 1), or NO_FRAME */
2533 
2534 	qh_refresh(oxu, qh);
2535 	qh->hw_next = EHCI_LIST_END;
2536 	frame = qh->start;
2537 
2538 	/* reuse the previous schedule slots, if we can */
2539 	if (frame < qh->period) {
2540 		uframe = ffs(le32_to_cpup(&qh->hw_info2) & QH_SMASK);
2541 		status = check_intr_schedule(oxu, frame, --uframe,
2542 				qh, &c_mask);
2543 	} else {
2544 		uframe = 0;
2545 		c_mask = 0;
2546 		status = -ENOSPC;
2547 	}
2548 
2549 	/* else scan the schedule to find a group of slots such that all
2550 	 * uframes have enough periodic bandwidth available.
2551 	 */
2552 	if (status) {
2553 		/* "normal" case, uframing flexible except with splits */
2554 		if (qh->period) {
2555 			frame = qh->period - 1;
2556 			do {
2557 				for (uframe = 0; uframe < 8; uframe++) {
2558 					status = check_intr_schedule(oxu,
2559 							frame, uframe, qh,
2560 							&c_mask);
2561 					if (status == 0)
2562 						break;
2563 				}
2564 			} while (status && frame--);
2565 
2566 		/* qh->period == 0 means every uframe */
2567 		} else {
2568 			frame = 0;
2569 			status = check_intr_schedule(oxu, 0, 0, qh, &c_mask);
2570 		}
2571 		if (status)
2572 			goto done;
2573 		qh->start = frame;
2574 
2575 		/* reset S-frame and (maybe) C-frame masks */
2576 		qh->hw_info2 &= cpu_to_le32(~(QH_CMASK | QH_SMASK));
2577 		qh->hw_info2 |= qh->period
2578 			? cpu_to_le32(1 << uframe)
2579 			: cpu_to_le32(QH_SMASK);
2580 		qh->hw_info2 |= c_mask;
2581 	} else
2582 		oxu_dbg(oxu, "reused qh %p schedule\n", qh);
2583 
2584 	/* stuff into the periodic schedule */
2585 	status = qh_link_periodic(oxu, qh);
2586 done:
2587 	return status;
2588 }
2589 
2590 static int intr_submit(struct oxu_hcd *oxu, struct urb *urb,
2591 			struct list_head *qtd_list, gfp_t mem_flags)
2592 {
2593 	unsigned epnum;
2594 	unsigned long flags;
2595 	struct ehci_qh *qh;
2596 	int status = 0;
2597 	struct list_head	empty;
2598 
2599 	/* get endpoint and transfer/schedule data */
2600 	epnum = urb->ep->desc.bEndpointAddress;
2601 
2602 	spin_lock_irqsave(&oxu->lock, flags);
2603 
2604 	if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
2605 		status = -ESHUTDOWN;
2606 		goto done;
2607 	}
2608 
2609 	/* get qh and force any scheduling errors */
2610 	INIT_LIST_HEAD(&empty);
2611 	qh = qh_append_tds(oxu, urb, &empty, epnum, &urb->ep->hcpriv);
2612 	if (qh == NULL) {
2613 		status = -ENOMEM;
2614 		goto done;
2615 	}
2616 	if (qh->qh_state == QH_STATE_IDLE) {
2617 		status = qh_schedule(oxu, qh);
2618 		if (status != 0)
2619 			goto done;
2620 	}
2621 
2622 	/* then queue the urb's tds to the qh */
2623 	qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
2624 	BUG_ON(qh == NULL);
2625 
2626 	/* ... update usbfs periodic stats */
2627 	oxu_to_hcd(oxu)->self.bandwidth_int_reqs++;
2628 
2629 done:
2630 	spin_unlock_irqrestore(&oxu->lock, flags);
2631 	if (status)
2632 		qtd_list_free(oxu, urb, qtd_list);
2633 
2634 	return status;
2635 }
2636 
2637 static inline int itd_submit(struct oxu_hcd *oxu, struct urb *urb,
2638 						gfp_t mem_flags)
2639 {
2640 	oxu_dbg(oxu, "iso support is missing!\n");
2641 	return -ENOSYS;
2642 }
2643 
2644 static inline int sitd_submit(struct oxu_hcd *oxu, struct urb *urb,
2645 						gfp_t mem_flags)
2646 {
2647 	oxu_dbg(oxu, "split iso support is missing!\n");
2648 	return -ENOSYS;
2649 }
2650 
2651 static void scan_periodic(struct oxu_hcd *oxu)
2652 {
2653 	unsigned frame, clock, now_uframe, mod;
2654 	unsigned modified;
2655 
2656 	mod = oxu->periodic_size << 3;
2657 
2658 	/*
2659 	 * When running, scan from last scan point up to "now"
2660 	 * else clean up by scanning everything that's left.
2661 	 * Touches as few pages as possible:  cache-friendly.
2662 	 */
2663 	now_uframe = oxu->next_uframe;
2664 	if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
2665 		clock = readl(&oxu->regs->frame_index);
2666 	else
2667 		clock = now_uframe + mod - 1;
2668 	clock %= mod;
2669 
2670 	for (;;) {
2671 		union ehci_shadow	q, *q_p;
2672 		__le32			type, *hw_p;
2673 
2674 		/* don't scan past the live uframe */
2675 		frame = now_uframe >> 3;
2676 		if (frame != (clock >> 3)) {
2677 			/* safe to scan the whole frame at once */
2678 			now_uframe |= 0x07;
2679 		}
2680 
2681 restart:
2682 		/* scan each element in frame's queue for completions */
2683 		q_p = &oxu->pshadow[frame];
2684 		hw_p = &oxu->periodic[frame];
2685 		q.ptr = q_p->ptr;
2686 		type = Q_NEXT_TYPE(*hw_p);
2687 		modified = 0;
2688 
2689 		while (q.ptr != NULL) {
2690 			union ehci_shadow temp;
2691 
2692 			switch (type) {
2693 			case Q_TYPE_QH:
2694 				/* handle any completions */
2695 				temp.qh = qh_get(q.qh);
2696 				type = Q_NEXT_TYPE(q.qh->hw_next);
2697 				q = q.qh->qh_next;
2698 				modified = qh_completions(oxu, temp.qh);
2699 				if (unlikely(list_empty(&temp.qh->qtd_list)))
2700 					intr_deschedule(oxu, temp.qh);
2701 				qh_put(temp.qh);
2702 				break;
2703 			default:
2704 				oxu_dbg(oxu, "corrupt type %d frame %d shadow %p\n",
2705 					type, frame, q.ptr);
2706 				q.ptr = NULL;
2707 			}
2708 
2709 			/* assume completion callbacks modify the queue */
2710 			if (unlikely(modified))
2711 				goto restart;
2712 		}
2713 
2714 		/* Stop when we catch up to the HC */
2715 
2716 		/* FIXME:  this assumes we won't get lapped when
2717 		 * latencies climb; that should be rare, but...
2718 		 * detect it, and just go all the way around.
2719 		 * FLR might help detect this case, so long as latencies
2720 		 * don't exceed periodic_size msec (default 1.024 sec).
2721 		 */
2722 
2723 		/* FIXME: likewise assumes HC doesn't halt mid-scan */
2724 
2725 		if (now_uframe == clock) {
2726 			unsigned	now;
2727 
2728 			if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
2729 				break;
2730 			oxu->next_uframe = now_uframe;
2731 			now = readl(&oxu->regs->frame_index) % mod;
2732 			if (now_uframe == now)
2733 				break;
2734 
2735 			/* rescan the rest of this frame, then ... */
2736 			clock = now;
2737 		} else {
2738 			now_uframe++;
2739 			now_uframe %= mod;
2740 		}
2741 	}
2742 }
2743 
2744 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
2745  * The firmware seems to think that powering off is a wakeup event!
2746  * This routine turns off remote wakeup and everything else, on all ports.
2747  */
2748 static void ehci_turn_off_all_ports(struct oxu_hcd *oxu)
2749 {
2750 	int port = HCS_N_PORTS(oxu->hcs_params);
2751 
2752 	while (port--)
2753 		writel(PORT_RWC_BITS, &oxu->regs->port_status[port]);
2754 }
2755 
2756 static void ehci_port_power(struct oxu_hcd *oxu, int is_on)
2757 {
2758 	unsigned port;
2759 
2760 	if (!HCS_PPC(oxu->hcs_params))
2761 		return;
2762 
2763 	oxu_dbg(oxu, "...power%s ports...\n", str_up_down(is_on));
2764 	for (port = HCS_N_PORTS(oxu->hcs_params); port > 0; ) {
2765 		if (is_on)
2766 			oxu_hub_control(oxu_to_hcd(oxu), SetPortFeature,
2767 				USB_PORT_FEAT_POWER, port--, NULL, 0);
2768 		else
2769 			oxu_hub_control(oxu_to_hcd(oxu), ClearPortFeature,
2770 				USB_PORT_FEAT_POWER, port--, NULL, 0);
2771 	}
2772 
2773 	msleep(20);
2774 }
2775 
2776 /* Called from some interrupts, timers, and so on.
2777  * It calls driver completion functions, after dropping oxu->lock.
2778  */
2779 static void ehci_work(struct oxu_hcd *oxu)
2780 {
2781 	timer_action_done(oxu, TIMER_IO_WATCHDOG);
2782 	if (oxu->reclaim_ready)
2783 		end_unlink_async(oxu);
2784 
2785 	/* another CPU may drop oxu->lock during a schedule scan while
2786 	 * it reports urb completions.  this flag guards against bogus
2787 	 * attempts at re-entrant schedule scanning.
2788 	 */
2789 	if (oxu->scanning)
2790 		return;
2791 	oxu->scanning = 1;
2792 	scan_async(oxu);
2793 	if (oxu->next_uframe != -1)
2794 		scan_periodic(oxu);
2795 	oxu->scanning = 0;
2796 
2797 	/* the IO watchdog guards against hardware or driver bugs that
2798 	 * misplace IRQs, and should let us run completely without IRQs.
2799 	 * such lossage has been observed on both VT6202 and VT8235.
2800 	 */
2801 	if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) &&
2802 			(oxu->async->qh_next.ptr != NULL ||
2803 			 oxu->periodic_sched != 0))
2804 		timer_action(oxu, TIMER_IO_WATCHDOG);
2805 }
2806 
2807 static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
2808 {
2809 	/* if we need to use IAA and it's busy, defer */
2810 	if (qh->qh_state == QH_STATE_LINKED
2811 			&& oxu->reclaim
2812 			&& HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) {
2813 		struct ehci_qh		*last;
2814 
2815 		for (last = oxu->reclaim;
2816 				last->reclaim;
2817 				last = last->reclaim)
2818 			continue;
2819 		qh->qh_state = QH_STATE_UNLINK_WAIT;
2820 		last->reclaim = qh;
2821 
2822 	/* bypass IAA if the hc can't care */
2823 	} else if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && oxu->reclaim)
2824 		end_unlink_async(oxu);
2825 
2826 	/* something else might have unlinked the qh by now */
2827 	if (qh->qh_state == QH_STATE_LINKED)
2828 		start_unlink_async(oxu, qh);
2829 }
2830 
2831 /*
2832  * USB host controller methods
2833  */
2834 
2835 static irqreturn_t oxu210_hcd_irq(struct usb_hcd *hcd)
2836 {
2837 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
2838 	u32 status, pcd_status = 0;
2839 	int bh;
2840 
2841 	spin_lock(&oxu->lock);
2842 
2843 	status = readl(&oxu->regs->status);
2844 
2845 	/* e.g. cardbus physical eject */
2846 	if (status == ~(u32) 0) {
2847 		oxu_dbg(oxu, "device removed\n");
2848 		goto dead;
2849 	}
2850 
2851 	/* Shared IRQ? */
2852 	status &= INTR_MASK;
2853 	if (!status || unlikely(hcd->state == HC_STATE_HALT)) {
2854 		spin_unlock(&oxu->lock);
2855 		return IRQ_NONE;
2856 	}
2857 
2858 	/* clear (just) interrupts */
2859 	writel(status, &oxu->regs->status);
2860 	readl(&oxu->regs->command);	/* unblock posted write */
2861 	bh = 0;
2862 
2863 #ifdef OXU_VERBOSE_DEBUG
2864 	/* unrequested/ignored: Frame List Rollover */
2865 	dbg_status(oxu, "irq", status);
2866 #endif
2867 
2868 	/* INT, ERR, and IAA interrupt rates can be throttled */
2869 
2870 	/* normal [4.15.1.2] or error [4.15.1.1] completion */
2871 	if (likely((status & (STS_INT|STS_ERR)) != 0))
2872 		bh = 1;
2873 
2874 	/* complete the unlinking of some qh [4.15.2.3] */
2875 	if (status & STS_IAA) {
2876 		oxu->reclaim_ready = 1;
2877 		bh = 1;
2878 	}
2879 
2880 	/* remote wakeup [4.3.1] */
2881 	if (status & STS_PCD) {
2882 		unsigned i = HCS_N_PORTS(oxu->hcs_params);
2883 		pcd_status = status;
2884 
2885 		/* resume root hub? */
2886 		if (!(readl(&oxu->regs->command) & CMD_RUN))
2887 			usb_hcd_resume_root_hub(hcd);
2888 
2889 		while (i--) {
2890 			int pstatus = readl(&oxu->regs->port_status[i]);
2891 
2892 			if (pstatus & PORT_OWNER)
2893 				continue;
2894 			if (!(pstatus & PORT_RESUME)
2895 					|| oxu->reset_done[i] != 0)
2896 				continue;
2897 
2898 			/* start USB_RESUME_TIMEOUT resume signaling from this
2899 			 * port, and make hub_wq collect PORT_STAT_C_SUSPEND to
2900 			 * stop that signaling.
2901 			 */
2902 			oxu->reset_done[i] = jiffies +
2903 				msecs_to_jiffies(USB_RESUME_TIMEOUT);
2904 			oxu_dbg(oxu, "port %d remote wakeup\n", i + 1);
2905 			mod_timer(&hcd->rh_timer, oxu->reset_done[i]);
2906 		}
2907 	}
2908 
2909 	/* PCI errors [4.15.2.4] */
2910 	if (unlikely((status & STS_FATAL) != 0)) {
2911 		/* bogus "fatal" IRQs appear on some chips... why?  */
2912 		status = readl(&oxu->regs->status);
2913 		dbg_cmd(oxu, "fatal", readl(&oxu->regs->command));
2914 		dbg_status(oxu, "fatal", status);
2915 		if (status & STS_HALT) {
2916 			oxu_err(oxu, "fatal error\n");
2917 dead:
2918 			ehci_reset(oxu);
2919 			writel(0, &oxu->regs->configured_flag);
2920 			usb_hc_died(hcd);
2921 			/* generic layer kills/unlinks all urbs, then
2922 			 * uses oxu_stop to clean up the rest
2923 			 */
2924 			bh = 1;
2925 		}
2926 	}
2927 
2928 	if (bh)
2929 		ehci_work(oxu);
2930 	spin_unlock(&oxu->lock);
2931 	if (pcd_status & STS_PCD)
2932 		usb_hcd_poll_rh_status(hcd);
2933 	return IRQ_HANDLED;
2934 }
2935 
2936 static irqreturn_t oxu_irq(struct usb_hcd *hcd)
2937 {
2938 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
2939 	int ret = IRQ_HANDLED;
2940 
2941 	u32 status = oxu_readl(hcd->regs, OXU_CHIPIRQSTATUS);
2942 	u32 enable = oxu_readl(hcd->regs, OXU_CHIPIRQEN_SET);
2943 
2944 	/* Disable all interrupt */
2945 	oxu_writel(hcd->regs, OXU_CHIPIRQEN_CLR, enable);
2946 
2947 	if ((oxu->is_otg && (status & OXU_USBOTGI)) ||
2948 		(!oxu->is_otg && (status & OXU_USBSPHI)))
2949 		oxu210_hcd_irq(hcd);
2950 	else
2951 		ret = IRQ_NONE;
2952 
2953 	/* Enable all interrupt back */
2954 	oxu_writel(hcd->regs, OXU_CHIPIRQEN_SET, enable);
2955 
2956 	return ret;
2957 }
2958 
2959 static void oxu_watchdog(struct timer_list *t)
2960 {
2961 	struct oxu_hcd	*oxu = timer_container_of(oxu, t, watchdog);
2962 	unsigned long flags;
2963 
2964 	spin_lock_irqsave(&oxu->lock, flags);
2965 
2966 	/* lost IAA irqs wedge things badly; seen with a vt8235 */
2967 	if (oxu->reclaim) {
2968 		u32 status = readl(&oxu->regs->status);
2969 		if (status & STS_IAA) {
2970 			oxu_vdbg(oxu, "lost IAA\n");
2971 			writel(STS_IAA, &oxu->regs->status);
2972 			oxu->reclaim_ready = 1;
2973 		}
2974 	}
2975 
2976 	/* stop async processing after it's idled a bit */
2977 	if (test_bit(TIMER_ASYNC_OFF, &oxu->actions))
2978 		start_unlink_async(oxu, oxu->async);
2979 
2980 	/* oxu could run by timer, without IRQs ... */
2981 	ehci_work(oxu);
2982 
2983 	spin_unlock_irqrestore(&oxu->lock, flags);
2984 }
2985 
2986 /* One-time init, only for memory state.
2987  */
2988 static int oxu_hcd_init(struct usb_hcd *hcd)
2989 {
2990 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
2991 	u32 temp;
2992 	int retval;
2993 	u32 hcc_params;
2994 
2995 	spin_lock_init(&oxu->lock);
2996 
2997 	timer_setup(&oxu->watchdog, oxu_watchdog, 0);
2998 
2999 	/*
3000 	 * hw default: 1K periodic list heads, one per frame.
3001 	 * periodic_size can shrink by USBCMD update if hcc_params allows.
3002 	 */
3003 	oxu->periodic_size = DEFAULT_I_TDPS;
3004 	retval = ehci_mem_init(oxu, GFP_KERNEL);
3005 	if (retval < 0)
3006 		return retval;
3007 
3008 	/* controllers may cache some of the periodic schedule ... */
3009 	hcc_params = readl(&oxu->caps->hcc_params);
3010 	if (HCC_ISOC_CACHE(hcc_params))		/* full frame cache */
3011 		oxu->i_thresh = 8;
3012 	else					/* N microframes cached */
3013 		oxu->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
3014 
3015 	oxu->reclaim = NULL;
3016 	oxu->reclaim_ready = 0;
3017 	oxu->next_uframe = -1;
3018 
3019 	/*
3020 	 * dedicate a qh for the async ring head, since we couldn't unlink
3021 	 * a 'real' qh without stopping the async schedule [4.8].  use it
3022 	 * as the 'reclamation list head' too.
3023 	 * its dummy is used in hw_alt_next of many tds, to prevent the qh
3024 	 * from automatically advancing to the next td after short reads.
3025 	 */
3026 	oxu->async->qh_next.qh = NULL;
3027 	oxu->async->hw_next = QH_NEXT(oxu->async->qh_dma);
3028 	oxu->async->hw_info1 = cpu_to_le32(QH_HEAD);
3029 	oxu->async->hw_token = cpu_to_le32(QTD_STS_HALT);
3030 	oxu->async->hw_qtd_next = EHCI_LIST_END;
3031 	oxu->async->qh_state = QH_STATE_LINKED;
3032 	oxu->async->hw_alt_next = QTD_NEXT(oxu->async->dummy->qtd_dma);
3033 
3034 	/* clear interrupt enables, set irq latency */
3035 	if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
3036 		log2_irq_thresh = 0;
3037 	temp = 1 << (16 + log2_irq_thresh);
3038 	if (HCC_CANPARK(hcc_params)) {
3039 		/* HW default park == 3, on hardware that supports it (like
3040 		 * NVidia and ALI silicon), maximizes throughput on the async
3041 		 * schedule by avoiding QH fetches between transfers.
3042 		 *
3043 		 * With fast usb storage devices and NForce2, "park" seems to
3044 		 * make problems:  throughput reduction (!), data errors...
3045 		 */
3046 		if (park) {
3047 			park = min_t(unsigned int, park, 3);
3048 			temp |= CMD_PARK;
3049 			temp |= park << 8;
3050 		}
3051 		oxu_dbg(oxu, "park %d\n", park);
3052 	}
3053 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
3054 		/* periodic schedule size can be smaller than default */
3055 		temp &= ~(3 << 2);
3056 		temp |= (EHCI_TUNE_FLS << 2);
3057 	}
3058 	oxu->command = temp;
3059 
3060 	return 0;
3061 }
3062 
3063 /* Called during probe() after chip reset completes.
3064  */
3065 static int oxu_reset(struct usb_hcd *hcd)
3066 {
3067 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3068 
3069 	spin_lock_init(&oxu->mem_lock);
3070 	INIT_LIST_HEAD(&oxu->urb_list);
3071 	oxu->urb_len = 0;
3072 
3073 	if (oxu->is_otg) {
3074 		oxu->caps = hcd->regs + OXU_OTG_CAP_OFFSET;
3075 		oxu->regs = hcd->regs + OXU_OTG_CAP_OFFSET + \
3076 			HC_LENGTH(readl(&oxu->caps->hc_capbase));
3077 
3078 		oxu->mem = hcd->regs + OXU_SPH_MEM;
3079 	} else {
3080 		oxu->caps = hcd->regs + OXU_SPH_CAP_OFFSET;
3081 		oxu->regs = hcd->regs + OXU_SPH_CAP_OFFSET + \
3082 			HC_LENGTH(readl(&oxu->caps->hc_capbase));
3083 
3084 		oxu->mem = hcd->regs + OXU_OTG_MEM;
3085 	}
3086 
3087 	oxu->hcs_params = readl(&oxu->caps->hcs_params);
3088 	oxu->sbrn = 0x20;
3089 
3090 	return oxu_hcd_init(hcd);
3091 }
3092 
3093 static int oxu_run(struct usb_hcd *hcd)
3094 {
3095 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3096 	int retval;
3097 	u32 temp, hcc_params;
3098 
3099 	hcd->uses_new_polling = 1;
3100 
3101 	/* EHCI spec section 4.1 */
3102 	retval = ehci_reset(oxu);
3103 	if (retval != 0) {
3104 		ehci_mem_cleanup(oxu);
3105 		return retval;
3106 	}
3107 	writel(oxu->periodic_dma, &oxu->regs->frame_list);
3108 	writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
3109 
3110 	/* hcc_params controls whether oxu->regs->segment must (!!!)
3111 	 * be used; it constrains QH/ITD/SITD and QTD locations.
3112 	 * dma_pool consistent memory always uses segment zero.
3113 	 * streaming mappings for I/O buffers, like dma_map_single(),
3114 	 * can return segments above 4GB, if the device allows.
3115 	 *
3116 	 * NOTE:  the dma mask is visible through dev->dma_mask, so
3117 	 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
3118 	 * Scsi_Host.highmem_io, and so forth.  It's readonly to all
3119 	 * host side drivers though.
3120 	 */
3121 	hcc_params = readl(&oxu->caps->hcc_params);
3122 	if (HCC_64BIT_ADDR(hcc_params))
3123 		writel(0, &oxu->regs->segment);
3124 
3125 	oxu->command &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE |
3126 				CMD_ASE | CMD_RESET);
3127 	oxu->command |= CMD_RUN;
3128 	writel(oxu->command, &oxu->regs->command);
3129 	dbg_cmd(oxu, "init", oxu->command);
3130 
3131 	/*
3132 	 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
3133 	 * are explicitly handed to companion controller(s), so no TT is
3134 	 * involved with the root hub.  (Except where one is integrated,
3135 	 * and there's no companion controller unless maybe for USB OTG.)
3136 	 */
3137 	hcd->state = HC_STATE_RUNNING;
3138 	writel(FLAG_CF, &oxu->regs->configured_flag);
3139 	readl(&oxu->regs->command);	/* unblock posted writes */
3140 
3141 	temp = HC_VERSION(readl(&oxu->caps->hc_capbase));
3142 	oxu_info(oxu, "USB %x.%x started, quasi-EHCI %x.%02x, driver %s%s\n",
3143 		((oxu->sbrn & 0xf0)>>4), (oxu->sbrn & 0x0f),
3144 		temp >> 8, temp & 0xff, DRIVER_VERSION,
3145 		ignore_oc ? ", overcurrent ignored" : "");
3146 
3147 	writel(INTR_MASK, &oxu->regs->intr_enable); /* Turn On Interrupts */
3148 
3149 	return 0;
3150 }
3151 
3152 static void oxu_stop(struct usb_hcd *hcd)
3153 {
3154 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3155 
3156 	/* Turn off port power on all root hub ports. */
3157 	ehci_port_power(oxu, 0);
3158 
3159 	/* no more interrupts ... */
3160 	timer_delete_sync(&oxu->watchdog);
3161 
3162 	spin_lock_irq(&oxu->lock);
3163 	if (HC_IS_RUNNING(hcd->state))
3164 		ehci_quiesce(oxu);
3165 
3166 	ehci_reset(oxu);
3167 	writel(0, &oxu->regs->intr_enable);
3168 	spin_unlock_irq(&oxu->lock);
3169 
3170 	/* let companion controllers work when we aren't */
3171 	writel(0, &oxu->regs->configured_flag);
3172 
3173 	/* root hub is shut down separately (first, when possible) */
3174 	spin_lock_irq(&oxu->lock);
3175 	if (oxu->async)
3176 		ehci_work(oxu);
3177 	spin_unlock_irq(&oxu->lock);
3178 	ehci_mem_cleanup(oxu);
3179 
3180 	dbg_status(oxu, "oxu_stop completed", readl(&oxu->regs->status));
3181 }
3182 
3183 /* Kick in for silicon on any bus (not just pci, etc).
3184  * This forcibly disables dma and IRQs, helping kexec and other cases
3185  * where the next system software may expect clean state.
3186  */
3187 static void oxu_shutdown(struct usb_hcd *hcd)
3188 {
3189 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3190 
3191 	(void) ehci_halt(oxu);
3192 	ehci_turn_off_all_ports(oxu);
3193 
3194 	/* make BIOS/etc use companion controller during reboot */
3195 	writel(0, &oxu->regs->configured_flag);
3196 
3197 	/* unblock posted writes */
3198 	readl(&oxu->regs->configured_flag);
3199 }
3200 
3201 /* Non-error returns are a promise to giveback() the urb later
3202  * we drop ownership so next owner (or urb unlink) can get it
3203  *
3204  * urb + dev is in hcd.self.controller.urb_list
3205  * we're queueing TDs onto software and hardware lists
3206  *
3207  * hcd-specific init for hcpriv hasn't been done yet
3208  *
3209  * NOTE:  control, bulk, and interrupt share the same code to append TDs
3210  * to a (possibly active) QH, and the same QH scanning code.
3211  */
3212 static int __oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
3213 				gfp_t mem_flags)
3214 {
3215 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3216 	struct list_head qtd_list;
3217 
3218 	INIT_LIST_HEAD(&qtd_list);
3219 
3220 	switch (usb_pipetype(urb->pipe)) {
3221 	case PIPE_CONTROL:
3222 	case PIPE_BULK:
3223 	default:
3224 		if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
3225 			return -ENOMEM;
3226 		return submit_async(oxu, urb, &qtd_list, mem_flags);
3227 
3228 	case PIPE_INTERRUPT:
3229 		if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
3230 			return -ENOMEM;
3231 		return intr_submit(oxu, urb, &qtd_list, mem_flags);
3232 
3233 	case PIPE_ISOCHRONOUS:
3234 		if (urb->dev->speed == USB_SPEED_HIGH)
3235 			return itd_submit(oxu, urb, mem_flags);
3236 		else
3237 			return sitd_submit(oxu, urb, mem_flags);
3238 	}
3239 }
3240 
3241 /* This function is responsible for breaking URBs with big data size
3242  * into smaller size and processing small urbs in sequence.
3243  */
3244 static int oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
3245 				gfp_t mem_flags)
3246 {
3247 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3248 	int num, rem;
3249 	void *transfer_buffer;
3250 	struct urb *murb;
3251 	int i, ret;
3252 
3253 	/* If not bulk pipe just enqueue the URB */
3254 	if (!usb_pipebulk(urb->pipe))
3255 		return __oxu_urb_enqueue(hcd, urb, mem_flags);
3256 
3257 	/* Otherwise we should verify the USB transfer buffer size! */
3258 	transfer_buffer = urb->transfer_buffer;
3259 
3260 	num = urb->transfer_buffer_length / 4096;
3261 	rem = urb->transfer_buffer_length % 4096;
3262 	if (rem != 0)
3263 		num++;
3264 
3265 	/* If URB is smaller than 4096 bytes just enqueue it! */
3266 	if (num == 1)
3267 		return __oxu_urb_enqueue(hcd, urb, mem_flags);
3268 
3269 	/* Ok, we have more job to do! :) */
3270 
3271 	for (i = 0; i < num - 1; i++) {
3272 		/* Get free micro URB poll till a free urb is received */
3273 
3274 		do {
3275 			murb = (struct urb *) oxu_murb_alloc(oxu);
3276 			if (!murb)
3277 				schedule();
3278 		} while (!murb);
3279 
3280 		/* Coping the urb */
3281 		memcpy(murb, urb, sizeof(struct urb));
3282 
3283 		murb->transfer_buffer_length = 4096;
3284 		murb->transfer_buffer = transfer_buffer + i * 4096;
3285 
3286 		/* Null pointer for the encodes that this is a micro urb */
3287 		murb->complete = NULL;
3288 
3289 		((struct oxu_murb *) murb)->main = urb;
3290 		((struct oxu_murb *) murb)->last = 0;
3291 
3292 		/* This loop is to guarantee urb to be processed when there's
3293 		 * not enough resources at a particular time by retrying.
3294 		 */
3295 		do {
3296 			ret  = __oxu_urb_enqueue(hcd, murb, mem_flags);
3297 			if (ret)
3298 				schedule();
3299 		} while (ret);
3300 	}
3301 
3302 	/* Last urb requires special handling  */
3303 
3304 	/* Get free micro URB poll till a free urb is received */
3305 	do {
3306 		murb = (struct urb *) oxu_murb_alloc(oxu);
3307 		if (!murb)
3308 			schedule();
3309 	} while (!murb);
3310 
3311 	/* Coping the urb */
3312 	memcpy(murb, urb, sizeof(struct urb));
3313 
3314 	murb->transfer_buffer_length = rem > 0 ? rem : 4096;
3315 	murb->transfer_buffer = transfer_buffer + (num - 1) * 4096;
3316 
3317 	/* Null pointer for the encodes that this is a micro urb */
3318 	murb->complete = NULL;
3319 
3320 	((struct oxu_murb *) murb)->main = urb;
3321 	((struct oxu_murb *) murb)->last = 1;
3322 
3323 	do {
3324 		ret = __oxu_urb_enqueue(hcd, murb, mem_flags);
3325 		if (ret)
3326 			schedule();
3327 	} while (ret);
3328 
3329 	return ret;
3330 }
3331 
3332 /* Remove from hardware lists.
3333  * Completions normally happen asynchronously
3334  */
3335 static int oxu_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
3336 {
3337 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3338 	struct ehci_qh *qh;
3339 	unsigned long flags;
3340 
3341 	spin_lock_irqsave(&oxu->lock, flags);
3342 	switch (usb_pipetype(urb->pipe)) {
3343 	case PIPE_CONTROL:
3344 	case PIPE_BULK:
3345 	default:
3346 		qh = (struct ehci_qh *) urb->hcpriv;
3347 		if (!qh)
3348 			break;
3349 		unlink_async(oxu, qh);
3350 		break;
3351 
3352 	case PIPE_INTERRUPT:
3353 		qh = (struct ehci_qh *) urb->hcpriv;
3354 		if (!qh)
3355 			break;
3356 		switch (qh->qh_state) {
3357 		case QH_STATE_LINKED:
3358 			intr_deschedule(oxu, qh);
3359 			fallthrough;
3360 		case QH_STATE_IDLE:
3361 			qh_completions(oxu, qh);
3362 			break;
3363 		default:
3364 			oxu_dbg(oxu, "bogus qh %p state %d\n",
3365 					qh, qh->qh_state);
3366 			goto done;
3367 		}
3368 
3369 		/* reschedule QH iff another request is queued */
3370 		if (!list_empty(&qh->qtd_list)
3371 				&& HC_IS_RUNNING(hcd->state)) {
3372 			int status;
3373 
3374 			status = qh_schedule(oxu, qh);
3375 			spin_unlock_irqrestore(&oxu->lock, flags);
3376 
3377 			if (status != 0) {
3378 				/* shouldn't happen often, but ...
3379 				 * FIXME kill those tds' urbs
3380 				 */
3381 				dev_err(hcd->self.controller,
3382 					"can't reschedule qh %p, err %d\n", qh,
3383 					status);
3384 			}
3385 			return status;
3386 		}
3387 		break;
3388 	}
3389 done:
3390 	spin_unlock_irqrestore(&oxu->lock, flags);
3391 	return 0;
3392 }
3393 
3394 /* Bulk qh holds the data toggle */
3395 static void oxu_endpoint_disable(struct usb_hcd *hcd,
3396 					struct usb_host_endpoint *ep)
3397 {
3398 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3399 	unsigned long		flags;
3400 	struct ehci_qh		*qh, *tmp;
3401 
3402 	/* ASSERT:  any requests/urbs are being unlinked */
3403 	/* ASSERT:  nobody can be submitting urbs for this any more */
3404 
3405 rescan:
3406 	spin_lock_irqsave(&oxu->lock, flags);
3407 	qh = ep->hcpriv;
3408 	if (!qh)
3409 		goto done;
3410 
3411 	/* endpoints can be iso streams.  for now, we don't
3412 	 * accelerate iso completions ... so spin a while.
3413 	 */
3414 	if (qh->hw_info1 == 0) {
3415 		oxu_vdbg(oxu, "iso delay\n");
3416 		goto idle_timeout;
3417 	}
3418 
3419 	if (!HC_IS_RUNNING(hcd->state))
3420 		qh->qh_state = QH_STATE_IDLE;
3421 	switch (qh->qh_state) {
3422 	case QH_STATE_LINKED:
3423 		for (tmp = oxu->async->qh_next.qh;
3424 				tmp && tmp != qh;
3425 				tmp = tmp->qh_next.qh)
3426 			continue;
3427 		/* periodic qh self-unlinks on empty */
3428 		if (!tmp)
3429 			goto nogood;
3430 		unlink_async(oxu, qh);
3431 		fallthrough;
3432 	case QH_STATE_UNLINK:		/* wait for hw to finish? */
3433 idle_timeout:
3434 		spin_unlock_irqrestore(&oxu->lock, flags);
3435 		schedule_timeout_uninterruptible(1);
3436 		goto rescan;
3437 	case QH_STATE_IDLE:		/* fully unlinked */
3438 		if (list_empty(&qh->qtd_list)) {
3439 			qh_put(qh);
3440 			break;
3441 		}
3442 		fallthrough;
3443 	default:
3444 nogood:
3445 		/* caller was supposed to have unlinked any requests;
3446 		 * that's not our job.  just leak this memory.
3447 		 */
3448 		oxu_err(oxu, "qh %p (#%02x) state %d%s\n",
3449 			qh, ep->desc.bEndpointAddress, qh->qh_state,
3450 			list_empty(&qh->qtd_list) ? "" : "(has tds)");
3451 		break;
3452 	}
3453 	ep->hcpriv = NULL;
3454 done:
3455 	spin_unlock_irqrestore(&oxu->lock, flags);
3456 }
3457 
3458 static int oxu_get_frame(struct usb_hcd *hcd)
3459 {
3460 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3461 
3462 	return (readl(&oxu->regs->frame_index) >> 3) %
3463 		oxu->periodic_size;
3464 }
3465 
3466 /* Build "status change" packet (one or two bytes) from HC registers */
3467 static int oxu_hub_status_data(struct usb_hcd *hcd, char *buf)
3468 {
3469 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3470 	u32 temp, mask, status = 0;
3471 	int ports, i, retval = 1;
3472 	unsigned long flags;
3473 
3474 	/* if !PM, root hub timers won't get shut down ... */
3475 	if (!HC_IS_RUNNING(hcd->state))
3476 		return 0;
3477 
3478 	/* init status to no-changes */
3479 	buf[0] = 0;
3480 	ports = HCS_N_PORTS(oxu->hcs_params);
3481 	if (ports > 7) {
3482 		buf[1] = 0;
3483 		retval++;
3484 	}
3485 
3486 	/* Some boards (mostly VIA?) report bogus overcurrent indications,
3487 	 * causing massive log spam unless we completely ignore them.  It
3488 	 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
3489 	 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
3490 	 * PORT_POWER; that's surprising, but maybe within-spec.
3491 	 */
3492 	if (!ignore_oc)
3493 		mask = PORT_CSC | PORT_PEC | PORT_OCC;
3494 	else
3495 		mask = PORT_CSC | PORT_PEC;
3496 
3497 	/* no hub change reports (bit 0) for now (power, ...) */
3498 
3499 	/* port N changes (bit N)? */
3500 	spin_lock_irqsave(&oxu->lock, flags);
3501 	for (i = 0; i < ports; i++) {
3502 		temp = readl(&oxu->regs->port_status[i]);
3503 
3504 		/*
3505 		 * Return status information even for ports with OWNER set.
3506 		 * Otherwise hub_wq wouldn't see the disconnect event when a
3507 		 * high-speed device is switched over to the companion
3508 		 * controller by the user.
3509 		 */
3510 
3511 		if (!(temp & PORT_CONNECT))
3512 			oxu->reset_done[i] = 0;
3513 		if ((temp & mask) != 0 || ((temp & PORT_RESUME) != 0 &&
3514 				time_after_eq(jiffies, oxu->reset_done[i]))) {
3515 			if (i < 7)
3516 				buf[0] |= 1 << (i + 1);
3517 			else
3518 				buf[1] |= 1 << (i - 7);
3519 			status = STS_PCD;
3520 		}
3521 	}
3522 	/* FIXME autosuspend idle root hubs */
3523 	spin_unlock_irqrestore(&oxu->lock, flags);
3524 	return status ? retval : 0;
3525 }
3526 
3527 /* Returns the speed of a device attached to a port on the root hub. */
3528 static inline unsigned int oxu_port_speed(struct oxu_hcd *oxu,
3529 						unsigned int portsc)
3530 {
3531 	switch ((portsc >> 26) & 3) {
3532 	case 0:
3533 		return 0;
3534 	case 1:
3535 		return USB_PORT_STAT_LOW_SPEED;
3536 	case 2:
3537 	default:
3538 		return USB_PORT_STAT_HIGH_SPEED;
3539 	}
3540 }
3541 
3542 #define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
3543 static int oxu_hub_control(struct usb_hcd *hcd, u16 typeReq,
3544 				u16 wValue, u16 wIndex, char *buf, u16 wLength)
3545 {
3546 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3547 	int ports = HCS_N_PORTS(oxu->hcs_params);
3548 	u32 __iomem *status_reg = &oxu->regs->port_status[wIndex - 1];
3549 	u32 temp, status;
3550 	unsigned long	flags;
3551 	int retval = 0;
3552 	unsigned selector;
3553 
3554 	/*
3555 	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
3556 	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
3557 	 * (track current state ourselves) ... blink for diagnostics,
3558 	 * power, "this is the one", etc.  EHCI spec supports this.
3559 	 */
3560 
3561 	spin_lock_irqsave(&oxu->lock, flags);
3562 	switch (typeReq) {
3563 	case ClearHubFeature:
3564 		switch (wValue) {
3565 		case C_HUB_LOCAL_POWER:
3566 		case C_HUB_OVER_CURRENT:
3567 			/* no hub-wide feature/status flags */
3568 			break;
3569 		default:
3570 			goto error;
3571 		}
3572 		break;
3573 	case ClearPortFeature:
3574 		if (!wIndex || wIndex > ports)
3575 			goto error;
3576 		wIndex--;
3577 		temp = readl(status_reg);
3578 
3579 		/*
3580 		 * Even if OWNER is set, so the port is owned by the
3581 		 * companion controller, hub_wq needs to be able to clear
3582 		 * the port-change status bits (especially
3583 		 * USB_PORT_STAT_C_CONNECTION).
3584 		 */
3585 
3586 		switch (wValue) {
3587 		case USB_PORT_FEAT_ENABLE:
3588 			writel(temp & ~PORT_PE, status_reg);
3589 			break;
3590 		case USB_PORT_FEAT_C_ENABLE:
3591 			writel((temp & ~PORT_RWC_BITS) | PORT_PEC, status_reg);
3592 			break;
3593 		case USB_PORT_FEAT_SUSPEND:
3594 			if (temp & PORT_RESET)
3595 				goto error;
3596 			if (temp & PORT_SUSPEND) {
3597 				if ((temp & PORT_PE) == 0)
3598 					goto error;
3599 				/* resume signaling for 20 msec */
3600 				temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
3601 				writel(temp | PORT_RESUME, status_reg);
3602 				oxu->reset_done[wIndex] = jiffies
3603 						+ msecs_to_jiffies(20);
3604 			}
3605 			break;
3606 		case USB_PORT_FEAT_C_SUSPEND:
3607 			/* we auto-clear this feature */
3608 			break;
3609 		case USB_PORT_FEAT_POWER:
3610 			if (HCS_PPC(oxu->hcs_params))
3611 				writel(temp & ~(PORT_RWC_BITS | PORT_POWER),
3612 					  status_reg);
3613 			break;
3614 		case USB_PORT_FEAT_C_CONNECTION:
3615 			writel((temp & ~PORT_RWC_BITS) | PORT_CSC, status_reg);
3616 			break;
3617 		case USB_PORT_FEAT_C_OVER_CURRENT:
3618 			writel((temp & ~PORT_RWC_BITS) | PORT_OCC, status_reg);
3619 			break;
3620 		case USB_PORT_FEAT_C_RESET:
3621 			/* GetPortStatus clears reset */
3622 			break;
3623 		default:
3624 			goto error;
3625 		}
3626 		readl(&oxu->regs->command);	/* unblock posted write */
3627 		break;
3628 	case GetHubDescriptor:
3629 		ehci_hub_descriptor(oxu, (struct usb_hub_descriptor *)
3630 			buf);
3631 		break;
3632 	case GetHubStatus:
3633 		/* no hub-wide feature/status flags */
3634 		memset(buf, 0, 4);
3635 		break;
3636 	case GetPortStatus:
3637 		if (!wIndex || wIndex > ports)
3638 			goto error;
3639 		wIndex--;
3640 		status = 0;
3641 		temp = readl(status_reg);
3642 
3643 		/* wPortChange bits */
3644 		if (temp & PORT_CSC)
3645 			status |= USB_PORT_STAT_C_CONNECTION << 16;
3646 		if (temp & PORT_PEC)
3647 			status |= USB_PORT_STAT_C_ENABLE << 16;
3648 		if ((temp & PORT_OCC) && !ignore_oc)
3649 			status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3650 
3651 		/* whoever resumes must GetPortStatus to complete it!! */
3652 		if (temp & PORT_RESUME) {
3653 
3654 			/* Remote Wakeup received? */
3655 			if (!oxu->reset_done[wIndex]) {
3656 				/* resume signaling for 20 msec */
3657 				oxu->reset_done[wIndex] = jiffies
3658 						+ msecs_to_jiffies(20);
3659 				/* check the port again */
3660 				mod_timer(&oxu_to_hcd(oxu)->rh_timer,
3661 						oxu->reset_done[wIndex]);
3662 			}
3663 
3664 			/* resume completed? */
3665 			else if (time_after_eq(jiffies,
3666 					oxu->reset_done[wIndex])) {
3667 				status |= USB_PORT_STAT_C_SUSPEND << 16;
3668 				oxu->reset_done[wIndex] = 0;
3669 
3670 				/* stop resume signaling */
3671 				temp = readl(status_reg);
3672 				writel(temp & ~(PORT_RWC_BITS | PORT_RESUME),
3673 					status_reg);
3674 				retval = handshake(oxu, status_reg,
3675 					   PORT_RESUME, 0, 2000 /* 2msec */);
3676 				if (retval != 0) {
3677 					oxu_err(oxu,
3678 						"port %d resume error %d\n",
3679 						wIndex + 1, retval);
3680 					goto error;
3681 				}
3682 				temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
3683 			}
3684 		}
3685 
3686 		/* whoever resets must GetPortStatus to complete it!! */
3687 		if ((temp & PORT_RESET)
3688 				&& time_after_eq(jiffies,
3689 					oxu->reset_done[wIndex])) {
3690 			status |= USB_PORT_STAT_C_RESET << 16;
3691 			oxu->reset_done[wIndex] = 0;
3692 
3693 			/* force reset to complete */
3694 			writel(temp & ~(PORT_RWC_BITS | PORT_RESET),
3695 					status_reg);
3696 			/* REVISIT:  some hardware needs 550+ usec to clear
3697 			 * this bit; seems too long to spin routinely...
3698 			 */
3699 			retval = handshake(oxu, status_reg,
3700 					PORT_RESET, 0, 750);
3701 			if (retval != 0) {
3702 				oxu_err(oxu, "port %d reset error %d\n",
3703 					wIndex + 1, retval);
3704 				goto error;
3705 			}
3706 
3707 			/* see what we found out */
3708 			temp = check_reset_complete(oxu, wIndex, status_reg,
3709 					readl(status_reg));
3710 		}
3711 
3712 		/* transfer dedicated ports to the companion hc */
3713 		if ((temp & PORT_CONNECT) &&
3714 				test_bit(wIndex, &oxu->companion_ports)) {
3715 			temp &= ~PORT_RWC_BITS;
3716 			temp |= PORT_OWNER;
3717 			writel(temp, status_reg);
3718 			oxu_dbg(oxu, "port %d --> companion\n", wIndex + 1);
3719 			temp = readl(status_reg);
3720 		}
3721 
3722 		/*
3723 		 * Even if OWNER is set, there's no harm letting hub_wq
3724 		 * see the wPortStatus values (they should all be 0 except
3725 		 * for PORT_POWER anyway).
3726 		 */
3727 
3728 		if (temp & PORT_CONNECT) {
3729 			status |= USB_PORT_STAT_CONNECTION;
3730 			/* status may be from integrated TT */
3731 			status |= oxu_port_speed(oxu, temp);
3732 		}
3733 		if (temp & PORT_PE)
3734 			status |= USB_PORT_STAT_ENABLE;
3735 		if (temp & (PORT_SUSPEND|PORT_RESUME))
3736 			status |= USB_PORT_STAT_SUSPEND;
3737 		if (temp & PORT_OC)
3738 			status |= USB_PORT_STAT_OVERCURRENT;
3739 		if (temp & PORT_RESET)
3740 			status |= USB_PORT_STAT_RESET;
3741 		if (temp & PORT_POWER)
3742 			status |= USB_PORT_STAT_POWER;
3743 
3744 #ifndef	OXU_VERBOSE_DEBUG
3745 	if (status & ~0xffff)	/* only if wPortChange is interesting */
3746 #endif
3747 		dbg_port(oxu, "GetStatus", wIndex + 1, temp);
3748 		put_unaligned(cpu_to_le32(status), (__le32 *) buf);
3749 		break;
3750 	case SetHubFeature:
3751 		switch (wValue) {
3752 		case C_HUB_LOCAL_POWER:
3753 		case C_HUB_OVER_CURRENT:
3754 			/* no hub-wide feature/status flags */
3755 			break;
3756 		default:
3757 			goto error;
3758 		}
3759 		break;
3760 	case SetPortFeature:
3761 		selector = wIndex >> 8;
3762 		wIndex &= 0xff;
3763 		if (!wIndex || wIndex > ports)
3764 			goto error;
3765 		wIndex--;
3766 		temp = readl(status_reg);
3767 		if (temp & PORT_OWNER)
3768 			break;
3769 
3770 		temp &= ~PORT_RWC_BITS;
3771 		switch (wValue) {
3772 		case USB_PORT_FEAT_SUSPEND:
3773 			if ((temp & PORT_PE) == 0
3774 					|| (temp & PORT_RESET) != 0)
3775 				goto error;
3776 			if (device_may_wakeup(&hcd->self.root_hub->dev))
3777 				temp |= PORT_WAKE_BITS;
3778 			writel(temp | PORT_SUSPEND, status_reg);
3779 			break;
3780 		case USB_PORT_FEAT_POWER:
3781 			if (HCS_PPC(oxu->hcs_params))
3782 				writel(temp | PORT_POWER, status_reg);
3783 			break;
3784 		case USB_PORT_FEAT_RESET:
3785 			if (temp & PORT_RESUME)
3786 				goto error;
3787 			/* line status bits may report this as low speed,
3788 			 * which can be fine if this root hub has a
3789 			 * transaction translator built in.
3790 			 */
3791 			oxu_vdbg(oxu, "port %d reset\n", wIndex + 1);
3792 			temp |= PORT_RESET;
3793 			temp &= ~PORT_PE;
3794 
3795 			/*
3796 			 * caller must wait, then call GetPortStatus
3797 			 * usb 2.0 spec says 50 ms resets on root
3798 			 */
3799 			oxu->reset_done[wIndex] = jiffies
3800 					+ msecs_to_jiffies(50);
3801 			writel(temp, status_reg);
3802 			break;
3803 
3804 		/* For downstream facing ports (these):  one hub port is put
3805 		 * into test mode according to USB2 11.24.2.13, then the hub
3806 		 * must be reset (which for root hub now means rmmod+modprobe,
3807 		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
3808 		 * about the EHCI-specific stuff.
3809 		 */
3810 		case USB_PORT_FEAT_TEST:
3811 			if (!selector || selector > 5)
3812 				goto error;
3813 			ehci_quiesce(oxu);
3814 			ehci_halt(oxu);
3815 			temp |= selector << 16;
3816 			writel(temp, status_reg);
3817 			break;
3818 
3819 		default:
3820 			goto error;
3821 		}
3822 		readl(&oxu->regs->command);	/* unblock posted writes */
3823 		break;
3824 
3825 	default:
3826 error:
3827 		/* "stall" on error */
3828 		retval = -EPIPE;
3829 	}
3830 	spin_unlock_irqrestore(&oxu->lock, flags);
3831 	return retval;
3832 }
3833 
3834 #ifdef CONFIG_PM
3835 
3836 static int oxu_bus_suspend(struct usb_hcd *hcd)
3837 {
3838 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3839 	int port;
3840 	int mask;
3841 
3842 	oxu_dbg(oxu, "suspend root hub\n");
3843 
3844 	if (time_before(jiffies, oxu->next_statechange))
3845 		msleep(5);
3846 
3847 	port = HCS_N_PORTS(oxu->hcs_params);
3848 	spin_lock_irq(&oxu->lock);
3849 
3850 	/* stop schedules, clean any completed work */
3851 	if (HC_IS_RUNNING(hcd->state)) {
3852 		ehci_quiesce(oxu);
3853 		hcd->state = HC_STATE_QUIESCING;
3854 	}
3855 	oxu->command = readl(&oxu->regs->command);
3856 	if (oxu->reclaim)
3857 		oxu->reclaim_ready = 1;
3858 	ehci_work(oxu);
3859 
3860 	/* Unlike other USB host controller types, EHCI doesn't have
3861 	 * any notion of "global" or bus-wide suspend.  The driver has
3862 	 * to manually suspend all the active unsuspended ports, and
3863 	 * then manually resume them in the bus_resume() routine.
3864 	 */
3865 	oxu->bus_suspended = 0;
3866 	while (port--) {
3867 		u32 __iomem *reg = &oxu->regs->port_status[port];
3868 		u32 t1 = readl(reg) & ~PORT_RWC_BITS;
3869 		u32 t2 = t1;
3870 
3871 		/* keep track of which ports we suspend */
3872 		if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
3873 				!(t1 & PORT_SUSPEND)) {
3874 			t2 |= PORT_SUSPEND;
3875 			set_bit(port, &oxu->bus_suspended);
3876 		}
3877 
3878 		/* enable remote wakeup on all ports */
3879 		if (device_may_wakeup(&hcd->self.root_hub->dev))
3880 			t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
3881 		else
3882 			t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
3883 
3884 		if (t1 != t2) {
3885 			oxu_vdbg(oxu, "port %d, %08x -> %08x\n",
3886 				port + 1, t1, t2);
3887 			writel(t2, reg);
3888 		}
3889 	}
3890 
3891 	spin_unlock_irq(&oxu->lock);
3892 	/* turn off now-idle HC */
3893 	timer_delete_sync(&oxu->watchdog);
3894 	spin_lock_irq(&oxu->lock);
3895 	ehci_halt(oxu);
3896 	hcd->state = HC_STATE_SUSPENDED;
3897 
3898 	/* allow remote wakeup */
3899 	mask = INTR_MASK;
3900 	if (!device_may_wakeup(&hcd->self.root_hub->dev))
3901 		mask &= ~STS_PCD;
3902 	writel(mask, &oxu->regs->intr_enable);
3903 	readl(&oxu->regs->intr_enable);
3904 
3905 	oxu->next_statechange = jiffies + msecs_to_jiffies(10);
3906 	spin_unlock_irq(&oxu->lock);
3907 	return 0;
3908 }
3909 
3910 /* Caller has locked the root hub, and should reset/reinit on error */
3911 static int oxu_bus_resume(struct usb_hcd *hcd)
3912 {
3913 	struct oxu_hcd *oxu = hcd_to_oxu(hcd);
3914 	u32 temp;
3915 	int i;
3916 
3917 	if (time_before(jiffies, oxu->next_statechange))
3918 		msleep(5);
3919 	spin_lock_irq(&oxu->lock);
3920 
3921 	/* Ideally and we've got a real resume here, and no port's power
3922 	 * was lost.  (For PCI, that means Vaux was maintained.)  But we
3923 	 * could instead be restoring a swsusp snapshot -- so that BIOS was
3924 	 * the last user of the controller, not reset/pm hardware keeping
3925 	 * state we gave to it.
3926 	 */
3927 	temp = readl(&oxu->regs->intr_enable);
3928 	oxu_dbg(oxu, "resume root hub%s\n", temp ? "" : " after power loss");
3929 
3930 	/* at least some APM implementations will try to deliver
3931 	 * IRQs right away, so delay them until we're ready.
3932 	 */
3933 	writel(0, &oxu->regs->intr_enable);
3934 
3935 	/* re-init operational registers */
3936 	writel(0, &oxu->regs->segment);
3937 	writel(oxu->periodic_dma, &oxu->regs->frame_list);
3938 	writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
3939 
3940 	/* restore CMD_RUN, framelist size, and irq threshold */
3941 	writel(oxu->command, &oxu->regs->command);
3942 
3943 	/* Some controller/firmware combinations need a delay during which
3944 	 * they set up the port statuses.  See Bugzilla #8190. */
3945 	mdelay(8);
3946 
3947 	/* manually resume the ports we suspended during bus_suspend() */
3948 	i = HCS_N_PORTS(oxu->hcs_params);
3949 	while (i--) {
3950 		temp = readl(&oxu->regs->port_status[i]);
3951 		temp &= ~(PORT_RWC_BITS
3952 			| PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
3953 		if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
3954 			oxu->reset_done[i] = jiffies + msecs_to_jiffies(20);
3955 			temp |= PORT_RESUME;
3956 		}
3957 		writel(temp, &oxu->regs->port_status[i]);
3958 	}
3959 	i = HCS_N_PORTS(oxu->hcs_params);
3960 	mdelay(20);
3961 	while (i--) {
3962 		temp = readl(&oxu->regs->port_status[i]);
3963 		if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
3964 			temp &= ~(PORT_RWC_BITS | PORT_RESUME);
3965 			writel(temp, &oxu->regs->port_status[i]);
3966 			oxu_vdbg(oxu, "resumed port %d\n", i + 1);
3967 		}
3968 	}
3969 	(void) readl(&oxu->regs->command);
3970 
3971 	/* maybe re-activate the schedule(s) */
3972 	temp = 0;
3973 	if (oxu->async->qh_next.qh)
3974 		temp |= CMD_ASE;
3975 	if (oxu->periodic_sched)
3976 		temp |= CMD_PSE;
3977 	if (temp) {
3978 		oxu->command |= temp;
3979 		writel(oxu->command, &oxu->regs->command);
3980 	}
3981 
3982 	oxu->next_statechange = jiffies + msecs_to_jiffies(5);
3983 	hcd->state = HC_STATE_RUNNING;
3984 
3985 	/* Now we can safely re-enable irqs */
3986 	writel(INTR_MASK, &oxu->regs->intr_enable);
3987 
3988 	spin_unlock_irq(&oxu->lock);
3989 	return 0;
3990 }
3991 
3992 #else
3993 
3994 static int oxu_bus_suspend(struct usb_hcd *hcd)
3995 {
3996 	return 0;
3997 }
3998 
3999 static int oxu_bus_resume(struct usb_hcd *hcd)
4000 {
4001 	return 0;
4002 }
4003 
4004 #endif	/* CONFIG_PM */
4005 
4006 static const struct hc_driver oxu_hc_driver = {
4007 	.description =		"oxu210hp_hcd",
4008 	.product_desc =		"oxu210hp HCD",
4009 	.hcd_priv_size =	sizeof(struct oxu_hcd),
4010 
4011 	/*
4012 	 * Generic hardware linkage
4013 	 */
4014 	.irq =			oxu_irq,
4015 	.flags =		HCD_MEMORY | HCD_USB2,
4016 
4017 	/*
4018 	 * Basic lifecycle operations
4019 	 */
4020 	.reset =		oxu_reset,
4021 	.start =		oxu_run,
4022 	.stop =			oxu_stop,
4023 	.shutdown =		oxu_shutdown,
4024 
4025 	/*
4026 	 * Managing i/o requests and associated device resources
4027 	 */
4028 	.urb_enqueue =		oxu_urb_enqueue,
4029 	.urb_dequeue =		oxu_urb_dequeue,
4030 	.endpoint_disable =	oxu_endpoint_disable,
4031 
4032 	/*
4033 	 * Scheduling support
4034 	 */
4035 	.get_frame_number =	oxu_get_frame,
4036 
4037 	/*
4038 	 * Root hub support
4039 	 */
4040 	.hub_status_data =	oxu_hub_status_data,
4041 	.hub_control =		oxu_hub_control,
4042 	.bus_suspend =		oxu_bus_suspend,
4043 	.bus_resume =		oxu_bus_resume,
4044 };
4045 
4046 /*
4047  * Module stuff
4048  */
4049 
4050 static void oxu_configuration(struct platform_device *pdev, void __iomem *base)
4051 {
4052 	u32 tmp;
4053 
4054 	/* Initialize top level registers.
4055 	 * First write ever
4056 	 */
4057 	oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
4058 	oxu_writel(base, OXU_SOFTRESET, OXU_SRESET);
4059 	oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
4060 
4061 	tmp = oxu_readl(base, OXU_PIOBURSTREADCTRL);
4062 	oxu_writel(base, OXU_PIOBURSTREADCTRL, tmp | 0x0040);
4063 
4064 	oxu_writel(base, OXU_ASO, OXU_SPHPOEN | OXU_OVRCCURPUPDEN |
4065 					OXU_COMPARATOR | OXU_ASO_OP);
4066 
4067 	tmp = oxu_readl(base, OXU_CLKCTRL_SET);
4068 	oxu_writel(base, OXU_CLKCTRL_SET, tmp | OXU_SYSCLKEN | OXU_USBOTGCLKEN);
4069 
4070 	/* Clear all top interrupt enable */
4071 	oxu_writel(base, OXU_CHIPIRQEN_CLR, 0xff);
4072 
4073 	/* Clear all top interrupt status */
4074 	oxu_writel(base, OXU_CHIPIRQSTATUS, 0xff);
4075 
4076 	/* Enable all needed top interrupt except OTG SPH core */
4077 	oxu_writel(base, OXU_CHIPIRQEN_SET, OXU_USBSPHLPWUI | OXU_USBOTGLPWUI);
4078 }
4079 
4080 static int oxu_verify_id(struct platform_device *pdev, void __iomem *base)
4081 {
4082 	u32 id;
4083 	static const char * const bo[] = {
4084 		"reserved",
4085 		"128-pin LQFP",
4086 		"84-pin TFBGA",
4087 		"reserved",
4088 	};
4089 
4090 	/* Read controller signature register to find a match */
4091 	id = oxu_readl(base, OXU_DEVICEID);
4092 	dev_info(&pdev->dev, "device ID %x\n", id);
4093 	if ((id & OXU_REV_MASK) != (OXU_REV_2100 << OXU_REV_SHIFT))
4094 		return -1;
4095 
4096 	dev_info(&pdev->dev, "found device %x %s (%04x:%04x)\n",
4097 		id >> OXU_REV_SHIFT,
4098 		bo[(id & OXU_BO_MASK) >> OXU_BO_SHIFT],
4099 		(id & OXU_MAJ_REV_MASK) >> OXU_MAJ_REV_SHIFT,
4100 		(id & OXU_MIN_REV_MASK) >> OXU_MIN_REV_SHIFT);
4101 
4102 	return 0;
4103 }
4104 
4105 static const struct hc_driver oxu_hc_driver;
4106 static struct usb_hcd *oxu_create(struct platform_device *pdev,
4107 				unsigned long memstart, unsigned long memlen,
4108 				void __iomem *base, int irq, int otg)
4109 {
4110 	struct device *dev = &pdev->dev;
4111 
4112 	struct usb_hcd *hcd;
4113 	struct oxu_hcd *oxu;
4114 	int ret;
4115 
4116 	/* Set endian mode and host mode */
4117 	oxu_writel(base + (otg ? OXU_OTG_CORE_OFFSET : OXU_SPH_CORE_OFFSET),
4118 				OXU_USBMODE,
4119 				OXU_CM_HOST_ONLY | OXU_ES_LITTLE | OXU_VBPS);
4120 
4121 	hcd = usb_create_hcd(&oxu_hc_driver, dev,
4122 				otg ? "oxu210hp_otg" : "oxu210hp_sph");
4123 	if (!hcd)
4124 		return ERR_PTR(-ENOMEM);
4125 
4126 	hcd->rsrc_start = memstart;
4127 	hcd->rsrc_len = memlen;
4128 	hcd->regs = base;
4129 	hcd->irq = irq;
4130 	hcd->state = HC_STATE_HALT;
4131 
4132 	oxu = hcd_to_oxu(hcd);
4133 	oxu->is_otg = otg;
4134 
4135 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
4136 	if (ret < 0) {
4137 		usb_put_hcd(hcd);
4138 		return ERR_PTR(ret);
4139 	}
4140 
4141 	device_wakeup_enable(hcd->self.controller);
4142 	return hcd;
4143 }
4144 
4145 static int oxu_init(struct platform_device *pdev,
4146 				unsigned long memstart, unsigned long memlen,
4147 				void __iomem *base, int irq)
4148 {
4149 	struct oxu_info *info = platform_get_drvdata(pdev);
4150 	struct usb_hcd *hcd;
4151 	int ret;
4152 
4153 	/* First time configuration at start up */
4154 	oxu_configuration(pdev, base);
4155 
4156 	ret = oxu_verify_id(pdev, base);
4157 	if (ret) {
4158 		dev_err(&pdev->dev, "no devices found!\n");
4159 		return -ENODEV;
4160 	}
4161 
4162 	/* Create the OTG controller */
4163 	hcd = oxu_create(pdev, memstart, memlen, base, irq, 1);
4164 	if (IS_ERR(hcd)) {
4165 		dev_err(&pdev->dev, "cannot create OTG controller!\n");
4166 		ret = PTR_ERR(hcd);
4167 		goto error_create_otg;
4168 	}
4169 	info->hcd[0] = hcd;
4170 
4171 	/* Create the SPH host controller */
4172 	hcd = oxu_create(pdev, memstart, memlen, base, irq, 0);
4173 	if (IS_ERR(hcd)) {
4174 		dev_err(&pdev->dev, "cannot create SPH controller!\n");
4175 		ret = PTR_ERR(hcd);
4176 		goto error_create_sph;
4177 	}
4178 	info->hcd[1] = hcd;
4179 
4180 	oxu_writel(base, OXU_CHIPIRQEN_SET,
4181 		oxu_readl(base, OXU_CHIPIRQEN_SET) | 3);
4182 
4183 	return 0;
4184 
4185 error_create_sph:
4186 	usb_remove_hcd(info->hcd[0]);
4187 	usb_put_hcd(info->hcd[0]);
4188 
4189 error_create_otg:
4190 	return ret;
4191 }
4192 
4193 static int oxu_drv_probe(struct platform_device *pdev)
4194 {
4195 	struct resource *res;
4196 	void __iomem *base;
4197 	unsigned long memstart, memlen;
4198 	int irq, ret;
4199 	struct oxu_info *info;
4200 
4201 	if (usb_disabled())
4202 		return -ENODEV;
4203 
4204 	/*
4205 	 * Get the platform resources
4206 	 */
4207 	irq = platform_get_irq(pdev, 0);
4208 	if (irq < 0)
4209 		return irq;
4210 	dev_dbg(&pdev->dev, "IRQ resource %d\n", irq);
4211 
4212 	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
4213 	if (IS_ERR(base)) {
4214 		ret = PTR_ERR(base);
4215 		goto error;
4216 	}
4217 	memstart = res->start;
4218 	memlen = resource_size(res);
4219 
4220 	ret = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING);
4221 	if (ret) {
4222 		dev_err(&pdev->dev, "error setting irq type\n");
4223 		ret = -EFAULT;
4224 		goto error;
4225 	}
4226 
4227 	/* Allocate a driver data struct to hold useful info for both
4228 	 * SPH & OTG devices
4229 	 */
4230 	info = devm_kzalloc_obj(&pdev->dev, struct oxu_info);
4231 	if (!info) {
4232 		ret = -EFAULT;
4233 		goto error;
4234 	}
4235 	platform_set_drvdata(pdev, info);
4236 
4237 	ret = oxu_init(pdev, memstart, memlen, base, irq);
4238 	if (ret < 0) {
4239 		dev_dbg(&pdev->dev, "cannot init USB devices\n");
4240 		goto error;
4241 	}
4242 
4243 	dev_info(&pdev->dev, "devices enabled and running\n");
4244 	platform_set_drvdata(pdev, info);
4245 
4246 	return 0;
4247 
4248 error:
4249 	dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), ret);
4250 	return ret;
4251 }
4252 
4253 static void oxu_remove(struct platform_device *pdev, struct usb_hcd *hcd)
4254 {
4255 	usb_remove_hcd(hcd);
4256 	usb_put_hcd(hcd);
4257 }
4258 
4259 static void oxu_drv_remove(struct platform_device *pdev)
4260 {
4261 	struct oxu_info *info = platform_get_drvdata(pdev);
4262 
4263 	oxu_remove(pdev, info->hcd[0]);
4264 	oxu_remove(pdev, info->hcd[1]);
4265 }
4266 
4267 static void oxu_drv_shutdown(struct platform_device *pdev)
4268 {
4269 	oxu_drv_remove(pdev);
4270 }
4271 
4272 #if 0
4273 /* FIXME: TODO */
4274 static int oxu_drv_suspend(struct device *dev)
4275 {
4276 	struct platform_device *pdev = to_platform_device(dev);
4277 	struct usb_hcd *hcd = dev_get_drvdata(dev);
4278 
4279 	return 0;
4280 }
4281 
4282 static int oxu_drv_resume(struct device *dev)
4283 {
4284 	struct platform_device *pdev = to_platform_device(dev);
4285 	struct usb_hcd *hcd = dev_get_drvdata(dev);
4286 
4287 	return 0;
4288 }
4289 #else
4290 #define oxu_drv_suspend	NULL
4291 #define oxu_drv_resume	NULL
4292 #endif
4293 
4294 static struct platform_driver oxu_driver = {
4295 	.probe		= oxu_drv_probe,
4296 	.remove		= oxu_drv_remove,
4297 	.shutdown	= oxu_drv_shutdown,
4298 	.suspend	= oxu_drv_suspend,
4299 	.resume		= oxu_drv_resume,
4300 	.driver = {
4301 		.name = "oxu210hp-hcd",
4302 		.bus = &platform_bus_type
4303 	}
4304 };
4305 
4306 module_platform_driver(oxu_driver);
4307 
4308 MODULE_DESCRIPTION("Oxford OXU210HP HCD driver - ver. " DRIVER_VERSION);
4309 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
4310 MODULE_LICENSE("GPL");
4311