162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-1.0+ */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * OHCI HCD (Host Controller Driver) for USB.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
662306a36Sopenharmony_ci * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * This file is licenced under the GPL.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * __hc32 and __hc16 are "Host Controller" types, they may be equivalent to
1362306a36Sopenharmony_ci * __leXX (normally) or __beXX (given OHCI_BIG_ENDIAN), depending on the
1462306a36Sopenharmony_ci * host controller implementation.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_citypedef __u32 __bitwise __hc32;
1762306a36Sopenharmony_citypedef __u16 __bitwise __hc16;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * OHCI Endpoint Descriptor (ED) ... holds TD queue
2162306a36Sopenharmony_ci * See OHCI spec, section 4.2
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * This is a "Queue Head" for those transfers, which is why
2462306a36Sopenharmony_ci * both EHCI and UHCI call similar structures a "QH".
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_cistruct ed {
2762306a36Sopenharmony_ci	/* first fields are hardware-specified */
2862306a36Sopenharmony_ci	__hc32			hwINFO;      /* endpoint config bitmap */
2962306a36Sopenharmony_ci	/* info bits defined by hcd */
3062306a36Sopenharmony_ci#define ED_DEQUEUE	(1 << 27)
3162306a36Sopenharmony_ci	/* info bits defined by the hardware */
3262306a36Sopenharmony_ci#define ED_ISO		(1 << 15)
3362306a36Sopenharmony_ci#define ED_SKIP		(1 << 14)
3462306a36Sopenharmony_ci#define ED_LOWSPEED	(1 << 13)
3562306a36Sopenharmony_ci#define ED_OUT		(0x01 << 11)
3662306a36Sopenharmony_ci#define ED_IN		(0x02 << 11)
3762306a36Sopenharmony_ci	__hc32			hwTailP;	/* tail of TD list */
3862306a36Sopenharmony_ci	__hc32			hwHeadP;	/* head of TD list (hc r/w) */
3962306a36Sopenharmony_ci#define ED_C		(0x02)			/* toggle carry */
4062306a36Sopenharmony_ci#define ED_H		(0x01)			/* halted */
4162306a36Sopenharmony_ci	__hc32			hwNextED;	/* next ED in list */
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	/* rest are purely for the driver's use */
4462306a36Sopenharmony_ci	dma_addr_t		dma;		/* addr of ED */
4562306a36Sopenharmony_ci	struct td		*dummy;		/* next TD to activate */
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	/* host's view of schedule */
4862306a36Sopenharmony_ci	struct ed		*ed_next;	/* on schedule or rm_list */
4962306a36Sopenharmony_ci	struct ed		*ed_prev;	/* for non-interrupt EDs */
5062306a36Sopenharmony_ci	struct list_head	td_list;	/* "shadow list" of our TDs */
5162306a36Sopenharmony_ci	struct list_head	in_use_list;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* create --> IDLE --> OPER --> ... --> IDLE --> destroy
5462306a36Sopenharmony_ci	 * usually:  OPER --> UNLINK --> (IDLE | OPER) --> ...
5562306a36Sopenharmony_ci	 */
5662306a36Sopenharmony_ci	u8			state;		/* ED_{IDLE,UNLINK,OPER} */
5762306a36Sopenharmony_ci#define ED_IDLE		0x00		/* NOT linked to HC */
5862306a36Sopenharmony_ci#define ED_UNLINK	0x01		/* being unlinked from hc */
5962306a36Sopenharmony_ci#define ED_OPER		0x02		/* IS linked to hc */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	u8			type;		/* PIPE_{BULK,...} */
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* periodic scheduling params (for intr and iso) */
6462306a36Sopenharmony_ci	u8			branch;
6562306a36Sopenharmony_ci	u16			interval;
6662306a36Sopenharmony_ci	u16			load;
6762306a36Sopenharmony_ci	u16			last_iso;	/* iso only */
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/* HC may see EDs on rm_list until next frame (frame_no == tick) */
7062306a36Sopenharmony_ci	u16			tick;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/* Detect TDs not added to the done queue */
7362306a36Sopenharmony_ci	unsigned		takeback_wdh_cnt;
7462306a36Sopenharmony_ci	struct td		*pending_td;
7562306a36Sopenharmony_ci#define	OKAY_TO_TAKEBACK(ohci, ed)			\
7662306a36Sopenharmony_ci		((int) (ohci->wdh_cnt - ed->takeback_wdh_cnt) >= 0)
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci} __attribute__ ((aligned(16)));
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#define ED_MASK	((u32)~0x0f)		/* strip hw status in low addr bits */
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/*
8462306a36Sopenharmony_ci * OHCI Transfer Descriptor (TD) ... one per transfer segment
8562306a36Sopenharmony_ci * See OHCI spec, sections 4.3.1 (general = control/bulk/interrupt)
8662306a36Sopenharmony_ci * and 4.3.2 (iso)
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_cistruct td {
8962306a36Sopenharmony_ci	/* first fields are hardware-specified */
9062306a36Sopenharmony_ci	__hc32		hwINFO;		/* transfer info bitmask */
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/* hwINFO bits for both general and iso tds: */
9362306a36Sopenharmony_ci#define TD_CC       0xf0000000			/* condition code */
9462306a36Sopenharmony_ci#define TD_CC_GET(td_p) ((td_p >>28) & 0x0f)
9562306a36Sopenharmony_ci//#define TD_CC_SET(td_p, cc) (td_p) = ((td_p) & 0x0fffffff) | (((cc) & 0x0f) << 28)
9662306a36Sopenharmony_ci#define TD_DI       0x00E00000			/* frames before interrupt */
9762306a36Sopenharmony_ci#define TD_DI_SET(X) (((X) & 0x07)<< 21)
9862306a36Sopenharmony_ci	/* these two bits are available for definition/use by HCDs in both
9962306a36Sopenharmony_ci	 * general and iso tds ... others are available for only one type
10062306a36Sopenharmony_ci	 */
10162306a36Sopenharmony_ci#define TD_DONE     0x00020000			/* retired to donelist */
10262306a36Sopenharmony_ci#define TD_ISO      0x00010000			/* copy of ED_ISO */
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	/* hwINFO bits for general tds: */
10562306a36Sopenharmony_ci#define TD_EC       0x0C000000			/* error count */
10662306a36Sopenharmony_ci#define TD_T        0x03000000			/* data toggle state */
10762306a36Sopenharmony_ci#define TD_T_DATA0  0x02000000				/* DATA0 */
10862306a36Sopenharmony_ci#define TD_T_DATA1  0x03000000				/* DATA1 */
10962306a36Sopenharmony_ci#define TD_T_TOGGLE 0x00000000				/* uses ED_C */
11062306a36Sopenharmony_ci#define TD_DP       0x00180000			/* direction/pid */
11162306a36Sopenharmony_ci#define TD_DP_SETUP 0x00000000			/* SETUP pid */
11262306a36Sopenharmony_ci#define TD_DP_IN    0x00100000				/* IN pid */
11362306a36Sopenharmony_ci#define TD_DP_OUT   0x00080000				/* OUT pid */
11462306a36Sopenharmony_ci							/* 0x00180000 rsvd */
11562306a36Sopenharmony_ci#define TD_R        0x00040000			/* round: short packets OK? */
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/* (no hwINFO #defines yet for iso tds) */
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	__hc32		hwCBP;		/* Current Buffer Pointer (or 0) */
12062306a36Sopenharmony_ci	__hc32		hwNextTD;	/* Next TD Pointer */
12162306a36Sopenharmony_ci	__hc32		hwBE;		/* Memory Buffer End Pointer */
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* PSW is only for ISO.  Only 1 PSW entry is used, but on
12462306a36Sopenharmony_ci	 * big-endian PPC hardware that's the second entry.
12562306a36Sopenharmony_ci	 */
12662306a36Sopenharmony_ci#define MAXPSW	2
12762306a36Sopenharmony_ci	__hc16		hwPSW [MAXPSW];
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/* rest are purely for the driver's use */
13062306a36Sopenharmony_ci	__u8		index;
13162306a36Sopenharmony_ci	struct ed	*ed;
13262306a36Sopenharmony_ci	struct td	*td_hash;	/* dma-->td hashtable */
13362306a36Sopenharmony_ci	struct td	*next_dl_td;
13462306a36Sopenharmony_ci	struct urb	*urb;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	dma_addr_t	td_dma;		/* addr of this TD */
13762306a36Sopenharmony_ci	dma_addr_t	data_dma;	/* addr of data it points to */
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	struct list_head td_list;	/* "shadow list", TDs on same ED */
14062306a36Sopenharmony_ci} __attribute__ ((aligned(32)));	/* c/b/i need 16; only iso needs 32 */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#define TD_MASK	((u32)~0x1f)		/* strip hw status in low addr bits */
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Hardware transfer status codes -- CC from td->hwINFO or td->hwPSW
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_ci#define TD_CC_NOERROR      0x00
14862306a36Sopenharmony_ci#define TD_CC_CRC          0x01
14962306a36Sopenharmony_ci#define TD_CC_BITSTUFFING  0x02
15062306a36Sopenharmony_ci#define TD_CC_DATATOGGLEM  0x03
15162306a36Sopenharmony_ci#define TD_CC_STALL        0x04
15262306a36Sopenharmony_ci#define TD_DEVNOTRESP      0x05
15362306a36Sopenharmony_ci#define TD_PIDCHECKFAIL    0x06
15462306a36Sopenharmony_ci#define TD_UNEXPECTEDPID   0x07
15562306a36Sopenharmony_ci#define TD_DATAOVERRUN     0x08
15662306a36Sopenharmony_ci#define TD_DATAUNDERRUN    0x09
15762306a36Sopenharmony_ci    /* 0x0A, 0x0B reserved for hardware */
15862306a36Sopenharmony_ci#define TD_BUFFEROVERRUN   0x0C
15962306a36Sopenharmony_ci#define TD_BUFFERUNDERRUN  0x0D
16062306a36Sopenharmony_ci    /* 0x0E, 0x0F reserved for HCD */
16162306a36Sopenharmony_ci#define TD_NOTACCESSED     0x0F
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/* map OHCI TD status codes (CC) to errno values */
16562306a36Sopenharmony_cistatic const int __maybe_unused cc_to_error [16] = {
16662306a36Sopenharmony_ci	/* No  Error  */               0,
16762306a36Sopenharmony_ci	/* CRC Error  */               -EILSEQ,
16862306a36Sopenharmony_ci	/* Bit Stuff  */               -EPROTO,
16962306a36Sopenharmony_ci	/* Data Togg  */               -EILSEQ,
17062306a36Sopenharmony_ci	/* Stall      */               -EPIPE,
17162306a36Sopenharmony_ci	/* DevNotResp */               -ETIME,
17262306a36Sopenharmony_ci	/* PIDCheck   */               -EPROTO,
17362306a36Sopenharmony_ci	/* UnExpPID   */               -EPROTO,
17462306a36Sopenharmony_ci	/* DataOver   */               -EOVERFLOW,
17562306a36Sopenharmony_ci	/* DataUnder  */               -EREMOTEIO,
17662306a36Sopenharmony_ci	/* (for hw)   */               -EIO,
17762306a36Sopenharmony_ci	/* (for hw)   */               -EIO,
17862306a36Sopenharmony_ci	/* BufferOver */               -ECOMM,
17962306a36Sopenharmony_ci	/* BuffUnder  */               -ENOSR,
18062306a36Sopenharmony_ci	/* (for HCD)  */               -EALREADY,
18162306a36Sopenharmony_ci	/* (for HCD)  */               -EALREADY
18262306a36Sopenharmony_ci};
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci/*
18662306a36Sopenharmony_ci * The HCCA (Host Controller Communications Area) is a 256 byte
18762306a36Sopenharmony_ci * structure defined section 4.4.1 of the OHCI spec. The HC is
18862306a36Sopenharmony_ci * told the base address of it.  It must be 256-byte aligned.
18962306a36Sopenharmony_ci */
19062306a36Sopenharmony_cistruct ohci_hcca {
19162306a36Sopenharmony_ci#define NUM_INTS 32
19262306a36Sopenharmony_ci	__hc32	int_table [NUM_INTS];	/* periodic schedule */
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	/*
19562306a36Sopenharmony_ci	 * OHCI defines u16 frame_no, followed by u16 zero pad.
19662306a36Sopenharmony_ci	 * Since some processors can't do 16 bit bus accesses,
19762306a36Sopenharmony_ci	 * portable access must be a 32 bits wide.
19862306a36Sopenharmony_ci	 */
19962306a36Sopenharmony_ci	__hc32	frame_no;		/* current frame number */
20062306a36Sopenharmony_ci	__hc32	done_head;		/* info returned for an interrupt */
20162306a36Sopenharmony_ci	u8	reserved_for_hc [116];
20262306a36Sopenharmony_ci	u8	what [4];		/* spec only identifies 252 bytes :) */
20362306a36Sopenharmony_ci} __attribute__ ((aligned(256)));
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci/*
20662306a36Sopenharmony_ci * This is the structure of the OHCI controller's memory mapped I/O region.
20762306a36Sopenharmony_ci * You must use readl() and writel() (in <asm/io.h>) to access these fields!!
20862306a36Sopenharmony_ci * Layout is in section 7 (and appendix B) of the spec.
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_cistruct ohci_regs {
21162306a36Sopenharmony_ci	/* control and status registers (section 7.1) */
21262306a36Sopenharmony_ci	__hc32	revision;
21362306a36Sopenharmony_ci	__hc32	control;
21462306a36Sopenharmony_ci	__hc32	cmdstatus;
21562306a36Sopenharmony_ci	__hc32	intrstatus;
21662306a36Sopenharmony_ci	__hc32	intrenable;
21762306a36Sopenharmony_ci	__hc32	intrdisable;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	/* memory pointers (section 7.2) */
22062306a36Sopenharmony_ci	__hc32	hcca;
22162306a36Sopenharmony_ci	__hc32	ed_periodcurrent;
22262306a36Sopenharmony_ci	__hc32	ed_controlhead;
22362306a36Sopenharmony_ci	__hc32	ed_controlcurrent;
22462306a36Sopenharmony_ci	__hc32	ed_bulkhead;
22562306a36Sopenharmony_ci	__hc32	ed_bulkcurrent;
22662306a36Sopenharmony_ci	__hc32	donehead;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	/* frame counters (section 7.3) */
22962306a36Sopenharmony_ci	__hc32	fminterval;
23062306a36Sopenharmony_ci	__hc32	fmremaining;
23162306a36Sopenharmony_ci	__hc32	fmnumber;
23262306a36Sopenharmony_ci	__hc32	periodicstart;
23362306a36Sopenharmony_ci	__hc32	lsthresh;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* Root hub ports (section 7.4) */
23662306a36Sopenharmony_ci	struct	ohci_roothub_regs {
23762306a36Sopenharmony_ci		__hc32	a;
23862306a36Sopenharmony_ci		__hc32	b;
23962306a36Sopenharmony_ci		__hc32	status;
24062306a36Sopenharmony_ci#define MAX_ROOT_PORTS	15	/* maximum OHCI root hub ports (RH_A_NDP) */
24162306a36Sopenharmony_ci		__hc32	portstatus [MAX_ROOT_PORTS];
24262306a36Sopenharmony_ci	} roothub;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	/* and optional "legacy support" registers (appendix B) at 0x0100 */
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci} __attribute__ ((aligned(32)));
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/* OHCI CONTROL AND STATUS REGISTER MASKS */
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci/*
25262306a36Sopenharmony_ci * HcControl (control) register masks
25362306a36Sopenharmony_ci */
25462306a36Sopenharmony_ci#define OHCI_CTRL_CBSR	(3 << 0)	/* control/bulk service ratio */
25562306a36Sopenharmony_ci#define OHCI_CTRL_PLE	(1 << 2)	/* periodic list enable */
25662306a36Sopenharmony_ci#define OHCI_CTRL_IE	(1 << 3)	/* isochronous enable */
25762306a36Sopenharmony_ci#define OHCI_CTRL_CLE	(1 << 4)	/* control list enable */
25862306a36Sopenharmony_ci#define OHCI_CTRL_BLE	(1 << 5)	/* bulk list enable */
25962306a36Sopenharmony_ci#define OHCI_CTRL_HCFS	(3 << 6)	/* host controller functional state */
26062306a36Sopenharmony_ci#define OHCI_CTRL_IR	(1 << 8)	/* interrupt routing */
26162306a36Sopenharmony_ci#define OHCI_CTRL_RWC	(1 << 9)	/* remote wakeup connected */
26262306a36Sopenharmony_ci#define OHCI_CTRL_RWE	(1 << 10)	/* remote wakeup enable */
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci/* pre-shifted values for HCFS */
26562306a36Sopenharmony_ci#	define OHCI_USB_RESET	(0 << 6)
26662306a36Sopenharmony_ci#	define OHCI_USB_RESUME	(1 << 6)
26762306a36Sopenharmony_ci#	define OHCI_USB_OPER	(2 << 6)
26862306a36Sopenharmony_ci#	define OHCI_USB_SUSPEND	(3 << 6)
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci/*
27162306a36Sopenharmony_ci * HcCommandStatus (cmdstatus) register masks
27262306a36Sopenharmony_ci */
27362306a36Sopenharmony_ci#define OHCI_HCR	(1 << 0)	/* host controller reset */
27462306a36Sopenharmony_ci#define OHCI_CLF	(1 << 1)	/* control list filled */
27562306a36Sopenharmony_ci#define OHCI_BLF	(1 << 2)	/* bulk list filled */
27662306a36Sopenharmony_ci#define OHCI_OCR	(1 << 3)	/* ownership change request */
27762306a36Sopenharmony_ci#define OHCI_SOC	(3 << 16)	/* scheduling overrun count */
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci/*
28062306a36Sopenharmony_ci * masks used with interrupt registers:
28162306a36Sopenharmony_ci * HcInterruptStatus (intrstatus)
28262306a36Sopenharmony_ci * HcInterruptEnable (intrenable)
28362306a36Sopenharmony_ci * HcInterruptDisable (intrdisable)
28462306a36Sopenharmony_ci */
28562306a36Sopenharmony_ci#define OHCI_INTR_SO	(1 << 0)	/* scheduling overrun */
28662306a36Sopenharmony_ci#define OHCI_INTR_WDH	(1 << 1)	/* writeback of done_head */
28762306a36Sopenharmony_ci#define OHCI_INTR_SF	(1 << 2)	/* start frame */
28862306a36Sopenharmony_ci#define OHCI_INTR_RD	(1 << 3)	/* resume detect */
28962306a36Sopenharmony_ci#define OHCI_INTR_UE	(1 << 4)	/* unrecoverable error */
29062306a36Sopenharmony_ci#define OHCI_INTR_FNO	(1 << 5)	/* frame number overflow */
29162306a36Sopenharmony_ci#define OHCI_INTR_RHSC	(1 << 6)	/* root hub status change */
29262306a36Sopenharmony_ci#define OHCI_INTR_OC	(1 << 30)	/* ownership change */
29362306a36Sopenharmony_ci#define OHCI_INTR_MIE	(1 << 31)	/* master interrupt enable */
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci/* OHCI ROOT HUB REGISTER MASKS */
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci/* roothub.portstatus [i] bits */
29962306a36Sopenharmony_ci#define RH_PS_CCS            0x00000001		/* current connect status */
30062306a36Sopenharmony_ci#define RH_PS_PES            0x00000002		/* port enable status*/
30162306a36Sopenharmony_ci#define RH_PS_PSS            0x00000004		/* port suspend status */
30262306a36Sopenharmony_ci#define RH_PS_POCI           0x00000008		/* port over current indicator */
30362306a36Sopenharmony_ci#define RH_PS_PRS            0x00000010		/* port reset status */
30462306a36Sopenharmony_ci#define RH_PS_PPS            0x00000100		/* port power status */
30562306a36Sopenharmony_ci#define RH_PS_LSDA           0x00000200		/* low speed device attached */
30662306a36Sopenharmony_ci#define RH_PS_CSC            0x00010000		/* connect status change */
30762306a36Sopenharmony_ci#define RH_PS_PESC           0x00020000		/* port enable status change */
30862306a36Sopenharmony_ci#define RH_PS_PSSC           0x00040000		/* port suspend status change */
30962306a36Sopenharmony_ci#define RH_PS_OCIC           0x00080000		/* over current indicator change */
31062306a36Sopenharmony_ci#define RH_PS_PRSC           0x00100000		/* port reset status change */
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci/* roothub.status bits */
31362306a36Sopenharmony_ci#define RH_HS_LPS	     0x00000001		/* local power status */
31462306a36Sopenharmony_ci#define RH_HS_OCI	     0x00000002		/* over current indicator */
31562306a36Sopenharmony_ci#define RH_HS_DRWE	     0x00008000		/* device remote wakeup enable */
31662306a36Sopenharmony_ci#define RH_HS_LPSC	     0x00010000		/* local power status change */
31762306a36Sopenharmony_ci#define RH_HS_OCIC	     0x00020000		/* over current indicator change */
31862306a36Sopenharmony_ci#define RH_HS_CRWE	     0x80000000		/* clear remote wakeup enable */
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci/* roothub.b masks */
32162306a36Sopenharmony_ci#define RH_B_DR		0x0000ffff		/* device removable flags */
32262306a36Sopenharmony_ci#define RH_B_PPCM	0xffff0000		/* port power control mask */
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci/* roothub.a masks */
32562306a36Sopenharmony_ci#define	RH_A_NDP	(0xff << 0)		/* number of downstream ports */
32662306a36Sopenharmony_ci#define	RH_A_PSM	(1 << 8)		/* power switching mode */
32762306a36Sopenharmony_ci#define	RH_A_NPS	(1 << 9)		/* no power switching */
32862306a36Sopenharmony_ci#define	RH_A_DT		(1 << 10)		/* device type (mbz) */
32962306a36Sopenharmony_ci#define	RH_A_OCPM	(1 << 11)		/* over current protection mode */
33062306a36Sopenharmony_ci#define	RH_A_NOCP	(1 << 12)		/* no over current protection */
33162306a36Sopenharmony_ci#define	RH_A_POTPGT	(0xff << 24)		/* power on to power good time */
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci/* hcd-private per-urb state */
33562306a36Sopenharmony_citypedef struct urb_priv {
33662306a36Sopenharmony_ci	struct ed		*ed;
33762306a36Sopenharmony_ci	u16			length;		// # tds in this request
33862306a36Sopenharmony_ci	u16			td_cnt;		// tds already serviced
33962306a36Sopenharmony_ci	struct list_head	pending;
34062306a36Sopenharmony_ci	struct td		*td[];		// all TDs in this request
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci} urb_priv_t;
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci#define TD_HASH_SIZE    64    /* power'o'two */
34562306a36Sopenharmony_ci// sizeof (struct td) ~= 64 == 2^6 ...
34662306a36Sopenharmony_ci#define TD_HASH_FUNC(td_dma) ((td_dma ^ (td_dma >> 6)) % TD_HASH_SIZE)
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci/*
35062306a36Sopenharmony_ci * This is the full ohci controller description
35162306a36Sopenharmony_ci *
35262306a36Sopenharmony_ci * Note how the "proper" USB information is just
35362306a36Sopenharmony_ci * a subset of what the full implementation needs. (Linus)
35462306a36Sopenharmony_ci */
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cienum ohci_rh_state {
35762306a36Sopenharmony_ci	OHCI_RH_HALTED,
35862306a36Sopenharmony_ci	OHCI_RH_SUSPENDED,
35962306a36Sopenharmony_ci	OHCI_RH_RUNNING
36062306a36Sopenharmony_ci};
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_cistruct ohci_hcd {
36362306a36Sopenharmony_ci	spinlock_t		lock;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	/*
36662306a36Sopenharmony_ci	 * I/O memory used to communicate with the HC (dma-consistent)
36762306a36Sopenharmony_ci	 */
36862306a36Sopenharmony_ci	struct ohci_regs __iomem *regs;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	/*
37162306a36Sopenharmony_ci	 * main memory used to communicate with the HC (dma-consistent).
37262306a36Sopenharmony_ci	 * hcd adds to schedule for a live hc any time, but removals finish
37362306a36Sopenharmony_ci	 * only at the start of the next frame.
37462306a36Sopenharmony_ci	 */
37562306a36Sopenharmony_ci	struct ohci_hcca	*hcca;
37662306a36Sopenharmony_ci	dma_addr_t		hcca_dma;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	struct ed		*ed_rm_list;		/* to be removed */
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	struct ed		*ed_bulktail;		/* last in bulk list */
38162306a36Sopenharmony_ci	struct ed		*ed_controltail;	/* last in ctrl list */
38262306a36Sopenharmony_ci	struct ed		*periodic [NUM_INTS];	/* shadow int_table */
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	void (*start_hnp)(struct ohci_hcd *ohci);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/*
38762306a36Sopenharmony_ci	 * memory management for queue data structures
38862306a36Sopenharmony_ci	 *
38962306a36Sopenharmony_ci	 * @td_cache and @ed_cache are %NULL if &usb_hcd.localmem_pool is used.
39062306a36Sopenharmony_ci	 */
39162306a36Sopenharmony_ci	struct dma_pool		*td_cache;
39262306a36Sopenharmony_ci	struct dma_pool		*ed_cache;
39362306a36Sopenharmony_ci	struct td		*td_hash [TD_HASH_SIZE];
39462306a36Sopenharmony_ci	struct td		*dl_start, *dl_end;	/* the done list */
39562306a36Sopenharmony_ci	struct list_head	pending;
39662306a36Sopenharmony_ci	struct list_head	eds_in_use;	/* all EDs with at least 1 TD */
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	/*
39962306a36Sopenharmony_ci	 * driver state
40062306a36Sopenharmony_ci	 */
40162306a36Sopenharmony_ci	enum ohci_rh_state	rh_state;
40262306a36Sopenharmony_ci	int			num_ports;
40362306a36Sopenharmony_ci	int			load [NUM_INTS];
40462306a36Sopenharmony_ci	u32			hc_control;	/* copy of hc control reg */
40562306a36Sopenharmony_ci	unsigned long		next_statechange;	/* suspend/resume */
40662306a36Sopenharmony_ci	u32			fminterval;		/* saved register */
40762306a36Sopenharmony_ci	unsigned		autostop:1;	/* rh auto stopping/stopped */
40862306a36Sopenharmony_ci	unsigned		working:1;
40962306a36Sopenharmony_ci	unsigned		restart_work:1;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	unsigned long		flags;		/* for HC bugs */
41262306a36Sopenharmony_ci#define	OHCI_QUIRK_AMD756	0x01			/* erratum #4 */
41362306a36Sopenharmony_ci#define	OHCI_QUIRK_SUPERIO	0x02			/* natsemi */
41462306a36Sopenharmony_ci#define	OHCI_QUIRK_INITRESET	0x04			/* SiS, OPTi, ... */
41562306a36Sopenharmony_ci#define	OHCI_QUIRK_BE_DESC	0x08			/* BE descriptors */
41662306a36Sopenharmony_ci#define	OHCI_QUIRK_BE_MMIO	0x10			/* BE registers */
41762306a36Sopenharmony_ci#define	OHCI_QUIRK_ZFMICRO	0x20			/* Compaq ZFMicro chipset*/
41862306a36Sopenharmony_ci#define	OHCI_QUIRK_NEC		0x40			/* lost interrupts */
41962306a36Sopenharmony_ci#define	OHCI_QUIRK_FRAME_NO	0x80			/* no big endian frame_no shift */
42062306a36Sopenharmony_ci#define	OHCI_QUIRK_HUB_POWER	0x100			/* distrust firmware power/oc setup */
42162306a36Sopenharmony_ci#define	OHCI_QUIRK_AMD_PLL	0x200			/* AMD PLL quirk*/
42262306a36Sopenharmony_ci#define	OHCI_QUIRK_AMD_PREFETCH	0x400			/* pre-fetch for ISO transfer */
42362306a36Sopenharmony_ci#define	OHCI_QUIRK_GLOBAL_SUSPEND	0x800		/* must suspend ports */
42462306a36Sopenharmony_ci#define	OHCI_QUIRK_QEMU		0x1000			/* relax timing expectations */
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	// there are also chip quirks/bugs in init logic
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	unsigned		prev_frame_no;
42962306a36Sopenharmony_ci	unsigned		wdh_cnt, prev_wdh_cnt;
43062306a36Sopenharmony_ci	u32			prev_donehead;
43162306a36Sopenharmony_ci	struct timer_list	io_watchdog;
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	struct work_struct	nec_work;	/* Worker for NEC quirk */
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	struct dentry		*debug_dir;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	/* platform-specific data -- must come last */
43862306a36Sopenharmony_ci	unsigned long           priv[] __aligned(sizeof(s64));
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci};
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci#ifdef CONFIG_USB_PCI
44362306a36Sopenharmony_cistatic inline int quirk_nec(struct ohci_hcd *ohci)
44462306a36Sopenharmony_ci{
44562306a36Sopenharmony_ci	return ohci->flags & OHCI_QUIRK_NEC;
44662306a36Sopenharmony_ci}
44762306a36Sopenharmony_cistatic inline int quirk_zfmicro(struct ohci_hcd *ohci)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	return ohci->flags & OHCI_QUIRK_ZFMICRO;
45062306a36Sopenharmony_ci}
45162306a36Sopenharmony_cistatic inline int quirk_amdiso(struct ohci_hcd *ohci)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	return ohci->flags & OHCI_QUIRK_AMD_PLL;
45462306a36Sopenharmony_ci}
45562306a36Sopenharmony_cistatic inline int quirk_amdprefetch(struct ohci_hcd *ohci)
45662306a36Sopenharmony_ci{
45762306a36Sopenharmony_ci	return ohci->flags & OHCI_QUIRK_AMD_PREFETCH;
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci#else
46062306a36Sopenharmony_cistatic inline int quirk_nec(struct ohci_hcd *ohci)
46162306a36Sopenharmony_ci{
46262306a36Sopenharmony_ci	return 0;
46362306a36Sopenharmony_ci}
46462306a36Sopenharmony_cistatic inline int quirk_zfmicro(struct ohci_hcd *ohci)
46562306a36Sopenharmony_ci{
46662306a36Sopenharmony_ci	return 0;
46762306a36Sopenharmony_ci}
46862306a36Sopenharmony_cistatic inline int quirk_amdiso(struct ohci_hcd *ohci)
46962306a36Sopenharmony_ci{
47062306a36Sopenharmony_ci	return 0;
47162306a36Sopenharmony_ci}
47262306a36Sopenharmony_cistatic inline int quirk_amdprefetch(struct ohci_hcd *ohci)
47362306a36Sopenharmony_ci{
47462306a36Sopenharmony_ci	return 0;
47562306a36Sopenharmony_ci}
47662306a36Sopenharmony_ci#endif
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/* convert between an hcd pointer and the corresponding ohci_hcd */
47962306a36Sopenharmony_cistatic inline struct ohci_hcd *hcd_to_ohci (struct usb_hcd *hcd)
48062306a36Sopenharmony_ci{
48162306a36Sopenharmony_ci	return (struct ohci_hcd *) (hcd->hcd_priv);
48262306a36Sopenharmony_ci}
48362306a36Sopenharmony_cistatic inline struct usb_hcd *ohci_to_hcd (const struct ohci_hcd *ohci)
48462306a36Sopenharmony_ci{
48562306a36Sopenharmony_ci	return container_of ((void *) ohci, struct usb_hcd, hcd_priv);
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci#define ohci_dbg(ohci, fmt, args...) \
49162306a36Sopenharmony_ci	dev_dbg (ohci_to_hcd(ohci)->self.controller , fmt , ## args )
49262306a36Sopenharmony_ci#define ohci_err(ohci, fmt, args...) \
49362306a36Sopenharmony_ci	dev_err (ohci_to_hcd(ohci)->self.controller , fmt , ## args )
49462306a36Sopenharmony_ci#define ohci_info(ohci, fmt, args...) \
49562306a36Sopenharmony_ci	dev_info (ohci_to_hcd(ohci)->self.controller , fmt , ## args )
49662306a36Sopenharmony_ci#define ohci_warn(ohci, fmt, args...) \
49762306a36Sopenharmony_ci	dev_warn (ohci_to_hcd(ohci)->self.controller , fmt , ## args )
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci/*
50262306a36Sopenharmony_ci * While most USB host controllers implement their registers and
50362306a36Sopenharmony_ci * in-memory communication descriptors in little-endian format,
50462306a36Sopenharmony_ci * a minority (notably the IBM STB04XXX and the Motorola MPC5200
50562306a36Sopenharmony_ci * processors) implement them in big endian format.
50662306a36Sopenharmony_ci *
50762306a36Sopenharmony_ci * In addition some more exotic implementations like the Toshiba
50862306a36Sopenharmony_ci * Spider (aka SCC) cell southbridge are "mixed" endian, that is,
50962306a36Sopenharmony_ci * they have a different endianness for registers vs. in-memory
51062306a36Sopenharmony_ci * descriptors.
51162306a36Sopenharmony_ci *
51262306a36Sopenharmony_ci * This attempts to support either format at compile time without a
51362306a36Sopenharmony_ci * runtime penalty, or both formats with the additional overhead
51462306a36Sopenharmony_ci * of checking a flag bit.
51562306a36Sopenharmony_ci *
51662306a36Sopenharmony_ci * That leads to some tricky Kconfig rules howevber. There are
51762306a36Sopenharmony_ci * different defaults based on some arch/ppc platforms, though
51862306a36Sopenharmony_ci * the basic rules are:
51962306a36Sopenharmony_ci *
52062306a36Sopenharmony_ci * Controller type              Kconfig options needed
52162306a36Sopenharmony_ci * ---------------              ----------------------
52262306a36Sopenharmony_ci * little endian                CONFIG_USB_OHCI_LITTLE_ENDIAN
52362306a36Sopenharmony_ci *
52462306a36Sopenharmony_ci * fully big endian             CONFIG_USB_OHCI_BIG_ENDIAN_DESC _and_
52562306a36Sopenharmony_ci *                              CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
52662306a36Sopenharmony_ci *
52762306a36Sopenharmony_ci * mixed endian                 CONFIG_USB_OHCI_LITTLE_ENDIAN _and_
52862306a36Sopenharmony_ci *                              CONFIG_USB_OHCI_BIG_ENDIAN_{MMIO,DESC}
52962306a36Sopenharmony_ci *
53062306a36Sopenharmony_ci * (If you have a mixed endian controller, you -must- also define
53162306a36Sopenharmony_ci * CONFIG_USB_OHCI_LITTLE_ENDIAN or things will not work when building
53262306a36Sopenharmony_ci * both your mixed endian and a fully big endian controller support in
53362306a36Sopenharmony_ci * the same kernel image).
53462306a36Sopenharmony_ci */
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
53762306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_LITTLE_ENDIAN
53862306a36Sopenharmony_ci#define big_endian_desc(ohci)	(ohci->flags & OHCI_QUIRK_BE_DESC)
53962306a36Sopenharmony_ci#else
54062306a36Sopenharmony_ci#define big_endian_desc(ohci)	1		/* only big endian */
54162306a36Sopenharmony_ci#endif
54262306a36Sopenharmony_ci#else
54362306a36Sopenharmony_ci#define big_endian_desc(ohci)	0		/* only little endian */
54462306a36Sopenharmony_ci#endif
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
54762306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_LITTLE_ENDIAN
54862306a36Sopenharmony_ci#define big_endian_mmio(ohci)	(ohci->flags & OHCI_QUIRK_BE_MMIO)
54962306a36Sopenharmony_ci#else
55062306a36Sopenharmony_ci#define big_endian_mmio(ohci)	1		/* only big endian */
55162306a36Sopenharmony_ci#endif
55262306a36Sopenharmony_ci#else
55362306a36Sopenharmony_ci#define big_endian_mmio(ohci)	0		/* only little endian */
55462306a36Sopenharmony_ci#endif
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci/*
55762306a36Sopenharmony_ci * Big-endian read/write functions are arch-specific.
55862306a36Sopenharmony_ci * Other arches can be added if/when they're needed.
55962306a36Sopenharmony_ci *
56062306a36Sopenharmony_ci */
56162306a36Sopenharmony_cistatic inline unsigned int _ohci_readl (const struct ohci_hcd *ohci,
56262306a36Sopenharmony_ci					__hc32 __iomem * regs)
56362306a36Sopenharmony_ci{
56462306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
56562306a36Sopenharmony_ci	return big_endian_mmio(ohci) ?
56662306a36Sopenharmony_ci		readl_be (regs) :
56762306a36Sopenharmony_ci		readl (regs);
56862306a36Sopenharmony_ci#else
56962306a36Sopenharmony_ci	return readl (regs);
57062306a36Sopenharmony_ci#endif
57162306a36Sopenharmony_ci}
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_cistatic inline void _ohci_writel (const struct ohci_hcd *ohci,
57462306a36Sopenharmony_ci				 const unsigned int val, __hc32 __iomem *regs)
57562306a36Sopenharmony_ci{
57662306a36Sopenharmony_ci#ifdef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
57762306a36Sopenharmony_ci	big_endian_mmio(ohci) ?
57862306a36Sopenharmony_ci		writel_be (val, regs) :
57962306a36Sopenharmony_ci		writel (val, regs);
58062306a36Sopenharmony_ci#else
58162306a36Sopenharmony_ci		writel (val, regs);
58262306a36Sopenharmony_ci#endif
58362306a36Sopenharmony_ci}
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci#define ohci_readl(o,r)		_ohci_readl(o,r)
58662306a36Sopenharmony_ci#define ohci_writel(o,v,r)	_ohci_writel(o,v,r)
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci/* cpu to ohci */
59262306a36Sopenharmony_cistatic inline __hc16 cpu_to_hc16 (const struct ohci_hcd *ohci, const u16 x)
59362306a36Sopenharmony_ci{
59462306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
59562306a36Sopenharmony_ci		(__force __hc16)cpu_to_be16(x) :
59662306a36Sopenharmony_ci		(__force __hc16)cpu_to_le16(x);
59762306a36Sopenharmony_ci}
59862306a36Sopenharmony_ci
59962306a36Sopenharmony_cistatic inline __hc16 cpu_to_hc16p (const struct ohci_hcd *ohci, const u16 *x)
60062306a36Sopenharmony_ci{
60162306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
60262306a36Sopenharmony_ci		cpu_to_be16p(x) :
60362306a36Sopenharmony_ci		cpu_to_le16p(x);
60462306a36Sopenharmony_ci}
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_cistatic inline __hc32 cpu_to_hc32 (const struct ohci_hcd *ohci, const u32 x)
60762306a36Sopenharmony_ci{
60862306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
60962306a36Sopenharmony_ci		(__force __hc32)cpu_to_be32(x) :
61062306a36Sopenharmony_ci		(__force __hc32)cpu_to_le32(x);
61162306a36Sopenharmony_ci}
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_cistatic inline __hc32 cpu_to_hc32p (const struct ohci_hcd *ohci, const u32 *x)
61462306a36Sopenharmony_ci{
61562306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
61662306a36Sopenharmony_ci		cpu_to_be32p(x) :
61762306a36Sopenharmony_ci		cpu_to_le32p(x);
61862306a36Sopenharmony_ci}
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci/* ohci to cpu */
62162306a36Sopenharmony_cistatic inline u16 hc16_to_cpu (const struct ohci_hcd *ohci, const __hc16 x)
62262306a36Sopenharmony_ci{
62362306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
62462306a36Sopenharmony_ci		be16_to_cpu((__force __be16)x) :
62562306a36Sopenharmony_ci		le16_to_cpu((__force __le16)x);
62662306a36Sopenharmony_ci}
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_cistatic inline u16 hc16_to_cpup (const struct ohci_hcd *ohci, const __hc16 *x)
62962306a36Sopenharmony_ci{
63062306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
63162306a36Sopenharmony_ci		be16_to_cpup((__force __be16 *)x) :
63262306a36Sopenharmony_ci		le16_to_cpup((__force __le16 *)x);
63362306a36Sopenharmony_ci}
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_cistatic inline u32 hc32_to_cpu (const struct ohci_hcd *ohci, const __hc32 x)
63662306a36Sopenharmony_ci{
63762306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
63862306a36Sopenharmony_ci		be32_to_cpu((__force __be32)x) :
63962306a36Sopenharmony_ci		le32_to_cpu((__force __le32)x);
64062306a36Sopenharmony_ci}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_cistatic inline u32 hc32_to_cpup (const struct ohci_hcd *ohci, const __hc32 *x)
64362306a36Sopenharmony_ci{
64462306a36Sopenharmony_ci	return big_endian_desc(ohci) ?
64562306a36Sopenharmony_ci		be32_to_cpup((__force __be32 *)x) :
64662306a36Sopenharmony_ci		le32_to_cpup((__force __le32 *)x);
64762306a36Sopenharmony_ci}
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci/*
65262306a36Sopenharmony_ci * The HCCA frame number is 16 bits, but is accessed as 32 bits since not all
65362306a36Sopenharmony_ci * hardware handles 16 bit reads.  Depending on the SoC implementation, the
65462306a36Sopenharmony_ci * frame number can wind up in either bits [31:16] (default) or
65562306a36Sopenharmony_ci * [15:0] (OHCI_QUIRK_FRAME_NO) on big endian hosts.
65662306a36Sopenharmony_ci *
65762306a36Sopenharmony_ci * Somewhat similarly, the 16-bit PSW fields in a transfer descriptor are
65862306a36Sopenharmony_ci * reordered on BE.
65962306a36Sopenharmony_ci */
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_cistatic inline u16 ohci_frame_no(const struct ohci_hcd *ohci)
66262306a36Sopenharmony_ci{
66362306a36Sopenharmony_ci	u32 tmp;
66462306a36Sopenharmony_ci	if (big_endian_desc(ohci)) {
66562306a36Sopenharmony_ci		tmp = be32_to_cpup((__force __be32 *)&ohci->hcca->frame_no);
66662306a36Sopenharmony_ci		if (!(ohci->flags & OHCI_QUIRK_FRAME_NO))
66762306a36Sopenharmony_ci			tmp >>= 16;
66862306a36Sopenharmony_ci	} else
66962306a36Sopenharmony_ci		tmp = le32_to_cpup((__force __le32 *)&ohci->hcca->frame_no);
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	return (u16)tmp;
67262306a36Sopenharmony_ci}
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_cistatic inline __hc16 *ohci_hwPSWp(const struct ohci_hcd *ohci,
67562306a36Sopenharmony_ci                                 const struct td *td, int index)
67662306a36Sopenharmony_ci{
67762306a36Sopenharmony_ci	return (__hc16 *)(big_endian_desc(ohci) ?
67862306a36Sopenharmony_ci			&td->hwPSW[index ^ 1] : &td->hwPSW[index]);
67962306a36Sopenharmony_ci}
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_cistatic inline u16 ohci_hwPSW(const struct ohci_hcd *ohci,
68262306a36Sopenharmony_ci                               const struct td *td, int index)
68362306a36Sopenharmony_ci{
68462306a36Sopenharmony_ci	return hc16_to_cpup(ohci, ohci_hwPSWp(ohci, td, index));
68562306a36Sopenharmony_ci}
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci#define	FI			0x2edf		/* 12000 bits per frame (-1) */
69062306a36Sopenharmony_ci#define	FSMP(fi)		(0x7fff & ((6 * ((fi) - 210)) / 7))
69162306a36Sopenharmony_ci#define	FIT			(1 << 31)
69262306a36Sopenharmony_ci#define LSTHRESH		0x628		/* lowspeed bit threshold */
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_cistatic inline void periodic_reinit (struct ohci_hcd *ohci)
69562306a36Sopenharmony_ci{
69662306a36Sopenharmony_ci	u32	fi = ohci->fminterval & 0x03fff;
69762306a36Sopenharmony_ci	u32	fit = ohci_readl(ohci, &ohci->regs->fminterval) & FIT;
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	ohci_writel (ohci, (fit ^ FIT) | ohci->fminterval,
70062306a36Sopenharmony_ci						&ohci->regs->fminterval);
70162306a36Sopenharmony_ci	ohci_writel (ohci, ((9 * fi) / 10) & 0x3fff,
70262306a36Sopenharmony_ci						&ohci->regs->periodicstart);
70362306a36Sopenharmony_ci}
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
70662306a36Sopenharmony_ci * The erratum (#4) description is incorrect.  AMD's workaround waits
70762306a36Sopenharmony_ci * till some bits (mostly reserved) are clear; ok for all revs.
70862306a36Sopenharmony_ci */
70962306a36Sopenharmony_ci#define read_roothub(hc, register, mask) ({ \
71062306a36Sopenharmony_ci	u32 temp = ohci_readl (hc, &hc->regs->roothub.register); \
71162306a36Sopenharmony_ci	if (temp == -1) \
71262306a36Sopenharmony_ci		hc->rh_state = OHCI_RH_HALTED; \
71362306a36Sopenharmony_ci	else if (hc->flags & OHCI_QUIRK_AMD756) \
71462306a36Sopenharmony_ci		while (temp & mask) \
71562306a36Sopenharmony_ci			temp = ohci_readl (hc, &hc->regs->roothub.register); \
71662306a36Sopenharmony_ci	temp; })
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_cistatic inline u32 roothub_a (struct ohci_hcd *hc)
71962306a36Sopenharmony_ci	{ return read_roothub (hc, a, 0xfc0fe000); }
72062306a36Sopenharmony_cistatic inline u32 roothub_b (struct ohci_hcd *hc)
72162306a36Sopenharmony_ci	{ return ohci_readl (hc, &hc->regs->roothub.b); }
72262306a36Sopenharmony_cistatic inline u32 roothub_status (struct ohci_hcd *hc)
72362306a36Sopenharmony_ci	{ return ohci_readl (hc, &hc->regs->roothub.status); }
72462306a36Sopenharmony_cistatic inline u32 roothub_portstatus (struct ohci_hcd *hc, int i)
72562306a36Sopenharmony_ci	{ return read_roothub (hc, portstatus [i], 0xffe0fce0); }
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci/* Declarations of things exported for use by ohci platform drivers */
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_cistruct ohci_driver_overrides {
73062306a36Sopenharmony_ci	const char	*product_desc;
73162306a36Sopenharmony_ci	size_t		extra_priv_size;
73262306a36Sopenharmony_ci	int		(*reset)(struct usb_hcd *hcd);
73362306a36Sopenharmony_ci};
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ciextern void	ohci_init_driver(struct hc_driver *drv,
73662306a36Sopenharmony_ci				const struct ohci_driver_overrides *over);
73762306a36Sopenharmony_ciextern int	ohci_restart(struct ohci_hcd *ohci);
73862306a36Sopenharmony_ciextern int	ohci_setup(struct usb_hcd *hcd);
73962306a36Sopenharmony_ciextern int	ohci_suspend(struct usb_hcd *hcd, bool do_wakeup);
74062306a36Sopenharmony_ciextern int	ohci_resume(struct usb_hcd *hcd, bool hibernated);
74162306a36Sopenharmony_ciextern int	ohci_hub_control(struct usb_hcd	*hcd, u16 typeReq, u16 wValue,
74262306a36Sopenharmony_ci				 u16 wIndex, char *buf, u16 wLength);
74362306a36Sopenharmony_ciextern int	ohci_hub_status_data(struct usb_hcd *hcd, char *buf);
744