xref: /freebsd/sys/dev/usb/controller/uhci.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
502ac6454SAndrew Thompson  * All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
802ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
902ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1002ac6454SAndrew Thompson  *
1102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1302ac6454SAndrew Thompson  * are met:
1402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2102ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2202ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2302ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2402ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2502ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2602ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2702ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2802ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2902ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3002ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #ifndef _UHCI_H_
3402ac6454SAndrew Thompson #define	_UHCI_H_
3502ac6454SAndrew Thompson 
36ab42e8b2SAndrew Thompson #define	UHCI_MAX_DEVICES MIN(USB_MAX_DEVICES, 128)
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson #define	UHCI_FRAMELIST_COUNT	1024	/* units */
3902ac6454SAndrew Thompson #define	UHCI_FRAMELIST_ALIGN	4096	/* bytes */
4002ac6454SAndrew Thompson 
4102ac6454SAndrew Thompson /* Structures alignment (bytes) */
4202ac6454SAndrew Thompson #define	UHCI_TD_ALIGN		16
4302ac6454SAndrew Thompson #define	UHCI_QH_ALIGN		16
4402ac6454SAndrew Thompson 
4502ac6454SAndrew Thompson #if	((USB_PAGE_SIZE < UHCI_TD_ALIGN) || (UHCI_TD_ALIGN == 0) ||	\
4602ac6454SAndrew Thompson 	(USB_PAGE_SIZE < UHCI_QH_ALIGN) || (UHCI_QH_ALIGN == 0))
4702ac6454SAndrew Thompson #error	"Invalid USB page size!"
4802ac6454SAndrew Thompson #endif
4902ac6454SAndrew Thompson 
5002ac6454SAndrew Thompson typedef uint32_t uhci_physaddr_t;
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson #define	UHCI_PTR_T		0x00000001
5302ac6454SAndrew Thompson #define	UHCI_PTR_TD		0x00000000
5402ac6454SAndrew Thompson #define	UHCI_PTR_QH		0x00000002
5502ac6454SAndrew Thompson #define	UHCI_PTR_VF		0x00000004
5602ac6454SAndrew Thompson 
5702ac6454SAndrew Thompson /*
5802ac6454SAndrew Thompson  * The Queue Heads (QH) and Transfer Descriptors (TD) are accessed by
5902ac6454SAndrew Thompson  * both the CPU and the USB-controller which run concurrently. Great
6002ac6454SAndrew Thompson  * care must be taken. When the data-structures are linked into the
6102ac6454SAndrew Thompson  * USB controller's frame list, the USB-controller "owns" the
6202ac6454SAndrew Thompson  * td_status and qh_elink fields, which will not be written by the
6302ac6454SAndrew Thompson  * CPU.
6402ac6454SAndrew Thompson  *
6502ac6454SAndrew Thompson  */
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson struct uhci_td {
6802ac6454SAndrew Thompson /*
6902ac6454SAndrew Thompson  * Data used by the UHCI controller.
7002ac6454SAndrew Thompson  * volatile is used in order to mantain struct members ordering.
7102ac6454SAndrew Thompson  */
7202ac6454SAndrew Thompson 	volatile uint32_t td_next;
7302ac6454SAndrew Thompson 	volatile uint32_t td_status;
7402ac6454SAndrew Thompson #define	UHCI_TD_GET_ACTLEN(s)	(((s) + 1) & 0x3ff)
7502ac6454SAndrew Thompson #define	UHCI_TD_ZERO_ACTLEN(t)	((t) | 0x3ff)
7602ac6454SAndrew Thompson #define	UHCI_TD_BITSTUFF	0x00020000
7702ac6454SAndrew Thompson #define	UHCI_TD_CRCTO		0x00040000
7802ac6454SAndrew Thompson #define	UHCI_TD_NAK		0x00080000
7902ac6454SAndrew Thompson #define	UHCI_TD_BABBLE		0x00100000
8002ac6454SAndrew Thompson #define	UHCI_TD_DBUFFER		0x00200000
8102ac6454SAndrew Thompson #define	UHCI_TD_STALLED		0x00400000
8202ac6454SAndrew Thompson #define	UHCI_TD_ACTIVE		0x00800000
8302ac6454SAndrew Thompson #define	UHCI_TD_IOC		0x01000000
8402ac6454SAndrew Thompson #define	UHCI_TD_IOS		0x02000000
8502ac6454SAndrew Thompson #define	UHCI_TD_LS		0x04000000
8602ac6454SAndrew Thompson #define	UHCI_TD_GET_ERRCNT(s)	(((s) >> 27) & 3)
8702ac6454SAndrew Thompson #define	UHCI_TD_SET_ERRCNT(n)	((n) << 27)
8802ac6454SAndrew Thompson #define	UHCI_TD_SPD		0x20000000
8902ac6454SAndrew Thompson 	volatile uint32_t td_token;
9002ac6454SAndrew Thompson #define	UHCI_TD_PID		0x000000ff
9102ac6454SAndrew Thompson #define	UHCI_TD_PID_IN		0x00000069
9202ac6454SAndrew Thompson #define	UHCI_TD_PID_OUT		0x000000e1
9302ac6454SAndrew Thompson #define	UHCI_TD_PID_SETUP	0x0000002d
9402ac6454SAndrew Thompson #define	UHCI_TD_GET_PID(s)	((s) & 0xff)
9502ac6454SAndrew Thompson #define	UHCI_TD_SET_DEVADDR(a)	((a) << 8)
9602ac6454SAndrew Thompson #define	UHCI_TD_GET_DEVADDR(s)	(((s) >> 8) & 0x7f)
9702ac6454SAndrew Thompson #define	UHCI_TD_SET_ENDPT(e)	(((e) & 0xf) << 15)
9802ac6454SAndrew Thompson #define	UHCI_TD_GET_ENDPT(s)	(((s) >> 15) & 0xf)
9902ac6454SAndrew Thompson #define	UHCI_TD_SET_DT(t)	((t) << 19)
10002ac6454SAndrew Thompson #define	UHCI_TD_GET_DT(s)	(((s) >> 19) & 1)
101db8409e0SHans Petter Selasky #define	UHCI_TD_SET_MAXLEN(l)	(((l)-1U) << 21)
10202ac6454SAndrew Thompson #define	UHCI_TD_GET_MAXLEN(s)	((((s) >> 21) + 1) & 0x7ff)
10302ac6454SAndrew Thompson #define	UHCI_TD_MAXLEN_MASK	0xffe00000
10402ac6454SAndrew Thompson 	volatile uint32_t td_buffer;
10502ac6454SAndrew Thompson /*
10602ac6454SAndrew Thompson  * Extra information needed:
10702ac6454SAndrew Thompson  */
10802ac6454SAndrew Thompson 	struct uhci_td *next;
10902ac6454SAndrew Thompson 	struct uhci_td *prev;
11002ac6454SAndrew Thompson 	struct uhci_td *obj_next;
111760bc48eSAndrew Thompson 	struct usb_page_cache *page_cache;
112760bc48eSAndrew Thompson 	struct usb_page_cache *fix_pc;
11302ac6454SAndrew Thompson 	uint32_t td_self;
11402ac6454SAndrew Thompson 	uint16_t len;
11502ac6454SAndrew Thompson } __aligned(UHCI_TD_ALIGN);
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson typedef struct uhci_td uhci_td_t;
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson #define	UHCI_TD_ERROR	(UHCI_TD_BITSTUFF | UHCI_TD_CRCTO | 		\
12002ac6454SAndrew Thompson 			UHCI_TD_BABBLE | UHCI_TD_DBUFFER | UHCI_TD_STALLED)
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson #define	UHCI_TD_SETUP(len, endp, dev)	(UHCI_TD_SET_MAXLEN(len) |	\
12302ac6454SAndrew Thompson 					UHCI_TD_SET_ENDPT(endp) |	\
12402ac6454SAndrew Thompson 					UHCI_TD_SET_DEVADDR(dev) |	\
12502ac6454SAndrew Thompson 					UHCI_TD_PID_SETUP)
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson #define	UHCI_TD_OUT(len, endp, dev, dt)	(UHCI_TD_SET_MAXLEN(len) |	\
12802ac6454SAndrew Thompson 					UHCI_TD_SET_ENDPT(endp) |	\
12902ac6454SAndrew Thompson 					UHCI_TD_SET_DEVADDR(dev) |	\
13002ac6454SAndrew Thompson 					UHCI_TD_PID_OUT | UHCI_TD_SET_DT(dt))
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson #define	UHCI_TD_IN(len, endp, dev, dt)	(UHCI_TD_SET_MAXLEN(len) |	\
13302ac6454SAndrew Thompson 					UHCI_TD_SET_ENDPT(endp) |	\
13402ac6454SAndrew Thompson 					UHCI_TD_SET_DEVADDR(dev) |	\
13502ac6454SAndrew Thompson 					UHCI_TD_PID_IN | UHCI_TD_SET_DT(dt))
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson struct uhci_qh {
13802ac6454SAndrew Thompson /*
13902ac6454SAndrew Thompson  * Data used by the UHCI controller.
14002ac6454SAndrew Thompson  */
14102ac6454SAndrew Thompson 	volatile uint32_t qh_h_next;
14202ac6454SAndrew Thompson 	volatile uint32_t qh_e_next;
14302ac6454SAndrew Thompson /*
14402ac6454SAndrew Thompson  * Extra information needed:
14502ac6454SAndrew Thompson  */
14602ac6454SAndrew Thompson 	struct uhci_qh *h_next;
14702ac6454SAndrew Thompson 	struct uhci_qh *h_prev;
14802ac6454SAndrew Thompson 	struct uhci_qh *obj_next;
14902ac6454SAndrew Thompson 	struct uhci_td *e_next;
150760bc48eSAndrew Thompson 	struct usb_page_cache *page_cache;
15102ac6454SAndrew Thompson 	uint32_t qh_self;
15202ac6454SAndrew Thompson 	uint16_t intr_pos;
15302ac6454SAndrew Thompson } __aligned(UHCI_QH_ALIGN);
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson typedef struct uhci_qh uhci_qh_t;
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson /* Maximum number of isochronous TD's and QH's interrupt */
15802ac6454SAndrew Thompson #define	UHCI_VFRAMELIST_COUNT	128
15902ac6454SAndrew Thompson #define	UHCI_IFRAMELIST_COUNT	(2 * UHCI_VFRAMELIST_COUNT)
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson #if	(((UHCI_VFRAMELIST_COUNT & (UHCI_VFRAMELIST_COUNT-1)) != 0) ||	\
16202ac6454SAndrew Thompson 	(UHCI_VFRAMELIST_COUNT > UHCI_FRAMELIST_COUNT))
16302ac6454SAndrew Thompson #error	"UHCI_VFRAMELIST_COUNT is not power of two"
16402ac6454SAndrew Thompson #error	"or UHCI_VFRAMELIST_COUNT > UHCI_FRAMELIST_COUNT"
16502ac6454SAndrew Thompson #endif
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson #if (UHCI_VFRAMELIST_COUNT < USB_MAX_FS_ISOC_FRAMES_PER_XFER)
16802ac6454SAndrew Thompson #error "maximum number of full-speed isochronous frames is higher than supported!"
16902ac6454SAndrew Thompson #endif
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson struct uhci_config_desc {
172760bc48eSAndrew Thompson 	struct usb_config_descriptor confd;
173760bc48eSAndrew Thompson 	struct usb_interface_descriptor ifcd;
174760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor endpd;
17502ac6454SAndrew Thompson } __packed;
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson union uhci_hub_desc {
178760bc48eSAndrew Thompson 	struct usb_status stat;
179760bc48eSAndrew Thompson 	struct usb_port_status ps;
18002ac6454SAndrew Thompson 	uint8_t	temp[128];
18102ac6454SAndrew Thompson };
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson struct uhci_hw_softc {
184760bc48eSAndrew Thompson 	struct usb_page_cache pframes_pc;
185760bc48eSAndrew Thompson 	struct usb_page_cache isoc_start_pc[UHCI_VFRAMELIST_COUNT];
186760bc48eSAndrew Thompson 	struct usb_page_cache intr_start_pc[UHCI_IFRAMELIST_COUNT];
187760bc48eSAndrew Thompson 	struct usb_page_cache ls_ctl_start_pc;
188760bc48eSAndrew Thompson 	struct usb_page_cache fs_ctl_start_pc;
189760bc48eSAndrew Thompson 	struct usb_page_cache bulk_start_pc;
190760bc48eSAndrew Thompson 	struct usb_page_cache last_qh_pc;
191760bc48eSAndrew Thompson 	struct usb_page_cache last_td_pc;
19202ac6454SAndrew Thompson 
193760bc48eSAndrew Thompson 	struct usb_page pframes_pg;
194760bc48eSAndrew Thompson 	struct usb_page isoc_start_pg[UHCI_VFRAMELIST_COUNT];
195760bc48eSAndrew Thompson 	struct usb_page intr_start_pg[UHCI_IFRAMELIST_COUNT];
196760bc48eSAndrew Thompson 	struct usb_page ls_ctl_start_pg;
197760bc48eSAndrew Thompson 	struct usb_page fs_ctl_start_pg;
198760bc48eSAndrew Thompson 	struct usb_page bulk_start_pg;
199760bc48eSAndrew Thompson 	struct usb_page last_qh_pg;
200760bc48eSAndrew Thompson 	struct usb_page last_td_pg;
20102ac6454SAndrew Thompson };
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson typedef struct uhci_softc {
20402ac6454SAndrew Thompson 	struct uhci_hw_softc sc_hw;
205760bc48eSAndrew Thompson 	struct usb_bus sc_bus;		/* base device */
20602ac6454SAndrew Thompson 	union uhci_hub_desc sc_hub_desc;
207760bc48eSAndrew Thompson 	struct usb_callout sc_root_intr;
20802ac6454SAndrew Thompson 
209760bc48eSAndrew Thompson 	struct usb_device *sc_devices[UHCI_MAX_DEVICES];
21039307315SAndrew Thompson 	/* pointer to last TD for isochronous */
21139307315SAndrew Thompson 	struct uhci_td *sc_isoc_p_last[UHCI_VFRAMELIST_COUNT];
21239307315SAndrew Thompson 	/* pointer to last QH for interrupt */
21339307315SAndrew Thompson 	struct uhci_qh *sc_intr_p_last[UHCI_IFRAMELIST_COUNT];
21439307315SAndrew Thompson 	/* pointer to last QH for low speed control */
21539307315SAndrew Thompson 	struct uhci_qh *sc_ls_ctl_p_last;
21639307315SAndrew Thompson 	/* pointer to last QH for full speed control */
21739307315SAndrew Thompson 	struct uhci_qh *sc_fs_ctl_p_last;
21839307315SAndrew Thompson 	/* pointer to last QH for bulk */
21939307315SAndrew Thompson 	struct uhci_qh *sc_bulk_p_last;
22002ac6454SAndrew Thompson 	struct uhci_qh *sc_reclaim_qh_p;
22102ac6454SAndrew Thompson 	struct uhci_qh *sc_last_qh_p;
22202ac6454SAndrew Thompson 	struct uhci_td *sc_last_td_p;
22302ac6454SAndrew Thompson 	struct resource *sc_io_res;
22402ac6454SAndrew Thompson 	struct resource *sc_irq_res;
22502ac6454SAndrew Thompson 	void   *sc_intr_hdl;
22602ac6454SAndrew Thompson 	device_t sc_dev;
22702ac6454SAndrew Thompson 	bus_size_t sc_io_size;
22802ac6454SAndrew Thompson 	bus_space_tag_t sc_io_tag;
22902ac6454SAndrew Thompson 	bus_space_handle_t sc_io_hdl;
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson 	uint32_t sc_loops;		/* number of QHs that wants looping */
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson 	uint16_t sc_intr_stat[UHCI_IFRAMELIST_COUNT];
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 	uint8_t	sc_addr;		/* device address */
23602ac6454SAndrew Thompson 	uint8_t	sc_conf;		/* device configuration */
23702ac6454SAndrew Thompson 	uint8_t	sc_isreset;		/* bits set if a root hub is reset */
23802ac6454SAndrew Thompson 	uint8_t	sc_isresumed;		/* bits set if a port was resumed */
23902ac6454SAndrew Thompson 	uint8_t	sc_hub_idata[1];
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson 	char	sc_vendor[16];		/* vendor string for root hub */
24202ac6454SAndrew Thompson } uhci_softc_t;
24302ac6454SAndrew Thompson 
244e0a69b51SAndrew Thompson usb_bus_mem_cb_t uhci_iterate_hw_softc;
24502ac6454SAndrew Thompson 
246e0a69b51SAndrew Thompson usb_error_t uhci_init(uhci_softc_t *sc);
24702ac6454SAndrew Thompson void	uhci_reset(uhci_softc_t *sc);
24802ac6454SAndrew Thompson void	uhci_interrupt(uhci_softc_t *sc);
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson #endif					/* _UHCI_H_ */
251