xref: /freebsd/sys/dev/usb/controller/ohci.h (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4*718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
602ac6454SAndrew Thompson  * All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
902ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
1002ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1302ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1402ac6454SAndrew Thompson  * are met:
1502ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1702ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1802ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1902ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2002ac6454SAndrew Thompson  *
2102ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2202ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2302ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2402ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2502ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2602ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2702ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2802ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2902ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3002ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3102ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson #ifndef _OHCI_H_
3502ac6454SAndrew Thompson #define	_OHCI_H_
3602ac6454SAndrew Thompson 
37ab42e8b2SAndrew Thompson #define	OHCI_MAX_DEVICES MIN(USB_MAX_DEVICES, 128)
3802ac6454SAndrew Thompson 
3902ac6454SAndrew Thompson #define	OHCI_NO_INTRS		32
4002ac6454SAndrew Thompson #define	OHCI_HCCA_SIZE		256
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson /* Structures alignment (bytes) */
4302ac6454SAndrew Thompson #define	OHCI_HCCA_ALIGN		256
4402ac6454SAndrew Thompson #define	OHCI_ED_ALIGN		16
4502ac6454SAndrew Thompson #define	OHCI_TD_ALIGN		16
4602ac6454SAndrew Thompson #define	OHCI_ITD_ALIGN		32
4702ac6454SAndrew Thompson 
4802ac6454SAndrew Thompson #define	OHCI_PAGE_SIZE		0x1000
4902ac6454SAndrew Thompson #define	OHCI_PAGE(x)		((x) &~ 0xfff)
5002ac6454SAndrew Thompson #define	OHCI_PAGE_OFFSET(x)	((x) & 0xfff)
5102ac6454SAndrew Thompson #define	OHCI_PAGE_MASK(x)	((x) & 0xfff)
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #if	((USB_PAGE_SIZE < OHCI_ED_ALIGN) || (OHCI_ED_ALIGN == 0) ||	\
5402ac6454SAndrew Thompson 	(USB_PAGE_SIZE < OHCI_TD_ALIGN) || (OHCI_TD_ALIGN == 0) ||	\
5502ac6454SAndrew Thompson 	(USB_PAGE_SIZE < OHCI_ITD_ALIGN) || (OHCI_ITD_ALIGN == 0) ||	\
5602ac6454SAndrew Thompson 	(USB_PAGE_SIZE < OHCI_PAGE_SIZE) || (OHCI_PAGE_SIZE == 0))
5702ac6454SAndrew Thompson #error	"Invalid USB page size!"
5802ac6454SAndrew Thompson #endif
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson #define	OHCI_VIRTUAL_FRAMELIST_COUNT 128/* dummy */
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #if (OHCI_VIRTUAL_FRAMELIST_COUNT < USB_MAX_FS_ISOC_FRAMES_PER_XFER)
6302ac6454SAndrew Thompson #error "maximum number of full-speed isochronous frames is higher than supported!"
6402ac6454SAndrew Thompson #endif
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson struct ohci_hcca {
6702ac6454SAndrew Thompson 	volatile uint32_t hcca_interrupt_table[OHCI_NO_INTRS];
6802ac6454SAndrew Thompson 	volatile uint32_t hcca_frame_number;
6902ac6454SAndrew Thompson 	volatile uint32_t hcca_done_head;
7002ac6454SAndrew Thompson #define	OHCI_DONE_INTRS		1
7102ac6454SAndrew Thompson } __aligned(OHCI_HCCA_ALIGN);
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson typedef struct ohci_hcca ohci_hcca_t;
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson struct ohci_ed {
7602ac6454SAndrew Thompson 	volatile uint32_t ed_flags;
7702ac6454SAndrew Thompson #define	OHCI_ED_GET_FA(s)	((s) & 0x7f)
7802ac6454SAndrew Thompson #define	OHCI_ED_ADDRMASK	0x0000007f
7902ac6454SAndrew Thompson #define	OHCI_ED_SET_FA(s)	(s)
8002ac6454SAndrew Thompson #define	OHCI_ED_GET_EN(s)	(((s) >> 7) & 0xf)
8102ac6454SAndrew Thompson #define	OHCI_ED_SET_EN(s)	((s) << 7)
8202ac6454SAndrew Thompson #define	OHCI_ED_DIR_MASK	0x00001800
8302ac6454SAndrew Thompson #define	OHCI_ED_DIR_TD		0x00000000
8402ac6454SAndrew Thompson #define	OHCI_ED_DIR_OUT		0x00000800
8502ac6454SAndrew Thompson #define	OHCI_ED_DIR_IN		0x00001000
8602ac6454SAndrew Thompson #define	OHCI_ED_SPEED		0x00002000
8702ac6454SAndrew Thompson #define	OHCI_ED_SKIP		0x00004000
8802ac6454SAndrew Thompson #define	OHCI_ED_FORMAT_GEN	0x00000000
8902ac6454SAndrew Thompson #define	OHCI_ED_FORMAT_ISO	0x00008000
9002ac6454SAndrew Thompson #define	OHCI_ED_GET_MAXP(s)	(((s) >> 16) & 0x07ff)
9102ac6454SAndrew Thompson #define	OHCI_ED_SET_MAXP(s)	((s) << 16)
9202ac6454SAndrew Thompson #define	OHCI_ED_MAXPMASK	(0x7ff << 16)
9302ac6454SAndrew Thompson 	volatile uint32_t ed_tailp;
9402ac6454SAndrew Thompson 	volatile uint32_t ed_headp;
9502ac6454SAndrew Thompson #define	OHCI_HALTED		0x00000001
9602ac6454SAndrew Thompson #define	OHCI_TOGGLECARRY	0x00000002
9702ac6454SAndrew Thompson #define	OHCI_HEADMASK		0xfffffffc
9802ac6454SAndrew Thompson 	volatile uint32_t ed_next;
9902ac6454SAndrew Thompson /*
10002ac6454SAndrew Thompson  * Extra information needed:
10102ac6454SAndrew Thompson  */
10202ac6454SAndrew Thompson 	struct ohci_ed *next;
10302ac6454SAndrew Thompson 	struct ohci_ed *prev;
10402ac6454SAndrew Thompson 	struct ohci_ed *obj_next;
105760bc48eSAndrew Thompson 	struct usb_page_cache *page_cache;
10602ac6454SAndrew Thompson 	uint32_t ed_self;
10702ac6454SAndrew Thompson } __aligned(OHCI_ED_ALIGN);
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson typedef struct ohci_ed ohci_ed_t;
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson struct ohci_td {
11202ac6454SAndrew Thompson 	volatile uint32_t td_flags;
11302ac6454SAndrew Thompson #define	OHCI_TD_R		0x00040000	/* Buffer Rounding  */
11402ac6454SAndrew Thompson #define	OHCI_TD_DP_MASK		0x00180000	/* Direction / PID */
11502ac6454SAndrew Thompson #define	OHCI_TD_SETUP		0x00000000
11602ac6454SAndrew Thompson #define	OHCI_TD_OUT		0x00080000
11702ac6454SAndrew Thompson #define	OHCI_TD_IN		0x00100000
11802ac6454SAndrew Thompson #define	OHCI_TD_GET_DI(x)	(((x) >> 21) & 7)	/* Delay Interrupt */
11902ac6454SAndrew Thompson #define	OHCI_TD_SET_DI(x)	((x) << 21)
12002ac6454SAndrew Thompson #define	OHCI_TD_NOINTR		0x00e00000
12102ac6454SAndrew Thompson #define	OHCI_TD_INTR_MASK	0x00e00000
12202ac6454SAndrew Thompson #define	OHCI_TD_TOGGLE_CARRY	0x00000000
12302ac6454SAndrew Thompson #define	OHCI_TD_TOGGLE_0	0x02000000
12402ac6454SAndrew Thompson #define	OHCI_TD_TOGGLE_1	0x03000000
12502ac6454SAndrew Thompson #define	OHCI_TD_TOGGLE_MASK	0x03000000
12602ac6454SAndrew Thompson #define	OHCI_TD_GET_EC(x)	(((x) >> 26) & 3)	/* Error Count */
12702ac6454SAndrew Thompson #define	OHCI_TD_GET_CC(x)	((x) >> 28)	/* Condition Code */
12802ac6454SAndrew Thompson #define	OHCI_TD_SET_CC(x)	((x) << 28)
12902ac6454SAndrew Thompson #define	OHCI_TD_NOCC		0xf0000000
13002ac6454SAndrew Thompson 	volatile uint32_t td_cbp;	/* Current Buffer Pointer */
13102ac6454SAndrew Thompson 	volatile uint32_t td_next;	/* Next TD */
13202ac6454SAndrew Thompson #define	OHCI_TD_NEXT_END	0
13302ac6454SAndrew Thompson 	volatile uint32_t td_be;	/* Buffer End */
13402ac6454SAndrew Thompson /*
13502ac6454SAndrew Thompson  * Extra information needed:
13602ac6454SAndrew Thompson  */
13702ac6454SAndrew Thompson 	struct ohci_td *obj_next;
13802ac6454SAndrew Thompson 	struct ohci_td *alt_next;
139760bc48eSAndrew Thompson 	struct usb_page_cache *page_cache;
14002ac6454SAndrew Thompson 	uint32_t td_self;
14102ac6454SAndrew Thompson 	uint16_t len;
14202ac6454SAndrew Thompson } __aligned(OHCI_TD_ALIGN);
14302ac6454SAndrew Thompson 
14402ac6454SAndrew Thompson typedef struct ohci_td ohci_td_t;
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson struct ohci_itd {
14702ac6454SAndrew Thompson 	volatile uint32_t itd_flags;
14802ac6454SAndrew Thompson #define	OHCI_ITD_GET_SF(x)	((x) & 0x0000ffff)
14902ac6454SAndrew Thompson #define	OHCI_ITD_SET_SF(x)	((x) & 0xffff)
15002ac6454SAndrew Thompson #define	OHCI_ITD_GET_DI(x)	(((x) >> 21) & 7)	/* Delay Interrupt */
15102ac6454SAndrew Thompson #define	OHCI_ITD_SET_DI(x)	((x) << 21)
15202ac6454SAndrew Thompson #define	OHCI_ITD_NOINTR		0x00e00000
15302ac6454SAndrew Thompson #define	OHCI_ITD_GET_FC(x)	((((x) >> 24) & 7)+1)	/* Frame Count */
15402ac6454SAndrew Thompson #define	OHCI_ITD_SET_FC(x)	(((x)-1) << 24)
15502ac6454SAndrew Thompson #define	OHCI_ITD_GET_CC(x)	((x) >> 28)	/* Condition Code */
15602ac6454SAndrew Thompson #define	OHCI_ITD_NOCC		0xf0000000
15702ac6454SAndrew Thompson #define	OHCI_ITD_NOFFSET	8
15802ac6454SAndrew Thompson 	volatile uint32_t itd_bp0;	/* Buffer Page 0 */
15902ac6454SAndrew Thompson 	volatile uint32_t itd_next;	/* Next ITD */
16002ac6454SAndrew Thompson 	volatile uint32_t itd_be;	/* Buffer End */
16102ac6454SAndrew Thompson 	volatile uint16_t itd_offset[OHCI_ITD_NOFFSET];	/* Buffer offsets and
16202ac6454SAndrew Thompson 							 * Status */
16302ac6454SAndrew Thompson #define	OHCI_ITD_PAGE_SELECT	0x00001000
16402ac6454SAndrew Thompson #define	OHCI_ITD_MK_OFFS(len)	(0xe000 | ((len) & 0x1fff))
16502ac6454SAndrew Thompson #define	OHCI_ITD_PSW_LENGTH(x)	((x) & 0xfff)	/* Transfer length */
16602ac6454SAndrew Thompson #define	OHCI_ITD_PSW_GET_CC(x)	((x) >> 12)	/* Condition Code */
16702ac6454SAndrew Thompson /*
16802ac6454SAndrew Thompson  * Extra information needed:
16902ac6454SAndrew Thompson  */
17002ac6454SAndrew Thompson 	struct ohci_itd *obj_next;
171760bc48eSAndrew Thompson 	struct usb_page_cache *page_cache;
17202ac6454SAndrew Thompson 	uint32_t itd_self;
17302ac6454SAndrew Thompson 	uint8_t	frames;
17402ac6454SAndrew Thompson } __aligned(OHCI_ITD_ALIGN);
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson typedef struct ohci_itd ohci_itd_t;
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson #define	OHCI_CC_NO_ERROR		0
17902ac6454SAndrew Thompson #define	OHCI_CC_CRC			1
18002ac6454SAndrew Thompson #define	OHCI_CC_BIT_STUFFING		2
18102ac6454SAndrew Thompson #define	OHCI_CC_DATA_TOGGLE_MISMATCH	3
18202ac6454SAndrew Thompson #define	OHCI_CC_STALL			4
18302ac6454SAndrew Thompson #define	OHCI_CC_DEVICE_NOT_RESPONDING	5
18402ac6454SAndrew Thompson #define	OHCI_CC_PID_CHECK_FAILURE	6
18502ac6454SAndrew Thompson #define	OHCI_CC_UNEXPECTED_PID		7
18602ac6454SAndrew Thompson #define	OHCI_CC_DATA_OVERRUN		8
18702ac6454SAndrew Thompson #define	OHCI_CC_DATA_UNDERRUN		9
18802ac6454SAndrew Thompson #define	OHCI_CC_BUFFER_OVERRUN		12
18902ac6454SAndrew Thompson #define	OHCI_CC_BUFFER_UNDERRUN		13
19002ac6454SAndrew Thompson #define	OHCI_CC_NOT_ACCESSED		15
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson /* Some delay needed when changing certain registers. */
19302ac6454SAndrew Thompson #define	OHCI_ENABLE_POWER_DELAY		5
19402ac6454SAndrew Thompson #define	OHCI_READ_DESC_DELAY		5
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson #define	OHCI_NO_EDS			(2*OHCI_NO_INTRS)
19702ac6454SAndrew Thompson 
19802ac6454SAndrew Thompson struct ohci_hw_softc {
199760bc48eSAndrew Thompson 	struct usb_page_cache hcca_pc;
200760bc48eSAndrew Thompson 	struct usb_page_cache ctrl_start_pc;
201760bc48eSAndrew Thompson 	struct usb_page_cache bulk_start_pc;
202760bc48eSAndrew Thompson 	struct usb_page_cache isoc_start_pc;
203760bc48eSAndrew Thompson 	struct usb_page_cache intr_start_pc[OHCI_NO_EDS];
20402ac6454SAndrew Thompson 
205760bc48eSAndrew Thompson 	struct usb_page hcca_pg;
206760bc48eSAndrew Thompson 	struct usb_page ctrl_start_pg;
207760bc48eSAndrew Thompson 	struct usb_page bulk_start_pg;
208760bc48eSAndrew Thompson 	struct usb_page isoc_start_pg;
209760bc48eSAndrew Thompson 	struct usb_page intr_start_pg[OHCI_NO_EDS];
21002ac6454SAndrew Thompson };
21102ac6454SAndrew Thompson 
21202ac6454SAndrew Thompson struct ohci_config_desc {
213760bc48eSAndrew Thompson 	struct usb_config_descriptor confd;
214760bc48eSAndrew Thompson 	struct usb_interface_descriptor ifcd;
215760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor endpd;
21602ac6454SAndrew Thompson } __packed;
21702ac6454SAndrew Thompson 
21802ac6454SAndrew Thompson union ohci_hub_desc {
219760bc48eSAndrew Thompson 	struct usb_status stat;
220760bc48eSAndrew Thompson 	struct usb_port_status ps;
221760bc48eSAndrew Thompson 	struct usb_hub_descriptor hubd;
22202ac6454SAndrew Thompson 	uint8_t	temp[128];
22302ac6454SAndrew Thompson };
22402ac6454SAndrew Thompson 
22502ac6454SAndrew Thompson typedef struct ohci_softc {
22602ac6454SAndrew Thompson 	struct ohci_hw_softc sc_hw;
227760bc48eSAndrew Thompson 	struct usb_bus sc_bus;		/* base device */
228760bc48eSAndrew Thompson 	struct usb_callout sc_tmo_rhsc;
22902ac6454SAndrew Thompson 	union ohci_hub_desc sc_hub_desc;
23002ac6454SAndrew Thompson 
231760bc48eSAndrew Thompson 	struct usb_device *sc_devices[OHCI_MAX_DEVICES];
23202ac6454SAndrew Thompson 	struct resource *sc_io_res;
23302ac6454SAndrew Thompson 	struct resource *sc_irq_res;
23402ac6454SAndrew Thompson 	struct ohci_hcca *sc_hcca_p;
23502ac6454SAndrew Thompson 	struct ohci_ed *sc_ctrl_p_last;
23602ac6454SAndrew Thompson 	struct ohci_ed *sc_bulk_p_last;
23702ac6454SAndrew Thompson 	struct ohci_ed *sc_isoc_p_last;
23802ac6454SAndrew Thompson 	struct ohci_ed *sc_intr_p_last[OHCI_NO_EDS];
23902ac6454SAndrew Thompson 	void   *sc_intr_hdl;
24002ac6454SAndrew Thompson 	device_t sc_dev;
24102ac6454SAndrew Thompson 	bus_size_t sc_io_size;
24202ac6454SAndrew Thompson 	bus_space_tag_t sc_io_tag;
24302ac6454SAndrew Thompson 	bus_space_handle_t sc_io_hdl;
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	uint32_t sc_eintrs;		/* enabled interrupts */
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 	uint16_t sc_intr_stat[OHCI_NO_EDS];
24802ac6454SAndrew Thompson 	uint16_t sc_id_vendor;
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	uint8_t	sc_noport;
25102ac6454SAndrew Thompson 	uint8_t	sc_addr;		/* device address */
25202ac6454SAndrew Thompson 	uint8_t	sc_conf;		/* device configuration */
25302ac6454SAndrew Thompson 	uint8_t	sc_hub_idata[32];
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson 	char	sc_vendor[16];
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson } ohci_softc_t;
25802ac6454SAndrew Thompson 
259e0a69b51SAndrew Thompson usb_bus_mem_cb_t ohci_iterate_hw_softc;
26002ac6454SAndrew Thompson 
261e0a69b51SAndrew Thompson usb_error_t ohci_init(ohci_softc_t *sc);
26202ac6454SAndrew Thompson void	ohci_detach(struct ohci_softc *sc);
26302ac6454SAndrew Thompson void	ohci_interrupt(ohci_softc_t *sc);
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson #endif					/* _OHCI_H_ */
266