1184610Salfred/* $FreeBSD$ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 1998 The NetBSD Foundation, Inc.
4184610Salfred * All rights reserved.
5184610Salfred *
6184610Salfred * This code is derived from software contributed to The NetBSD Foundation
7184610Salfred * by Lennart Augustsson (lennart@augustsson.net) at
8184610Salfred * Carlstedt Research & Technology.
9184610Salfred *
10184610Salfred * Redistribution and use in source and binary forms, with or without
11184610Salfred * modification, are permitted provided that the following conditions
12184610Salfred * are met:
13184610Salfred * 1. Redistributions of source code must retain the above copyright
14184610Salfred *    notice, this list of conditions and the following disclaimer.
15184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
16184610Salfred *    notice, this list of conditions and the following disclaimer in the
17184610Salfred *    documentation and/or other materials provided with the distribution.
18184610Salfred *
19184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22184610Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29184610Salfred * POSSIBILITY OF SUCH DAMAGE.
30184610Salfred */
31184610Salfred
32184610Salfred#ifndef _OHCI_H_
33184610Salfred#define	_OHCI_H_
34184610Salfred
35190174Sthompsa#define	OHCI_MAX_DEVICES MIN(USB_MAX_DEVICES, 128)
36187170Sthompsa
37184610Salfred#define	OHCI_NO_INTRS		32
38184610Salfred#define	OHCI_HCCA_SIZE		256
39184610Salfred
40184610Salfred/* Structures alignment (bytes) */
41184610Salfred#define	OHCI_HCCA_ALIGN		256
42184610Salfred#define	OHCI_ED_ALIGN		16
43184610Salfred#define	OHCI_TD_ALIGN		16
44184610Salfred#define	OHCI_ITD_ALIGN		32
45184610Salfred
46184610Salfred#define	OHCI_PAGE_SIZE		0x1000
47184610Salfred#define	OHCI_PAGE(x)		((x) &~ 0xfff)
48184610Salfred#define	OHCI_PAGE_OFFSET(x)	((x) & 0xfff)
49184610Salfred#define	OHCI_PAGE_MASK(x)	((x) & 0xfff)
50184610Salfred
51184610Salfred#if	((USB_PAGE_SIZE < OHCI_ED_ALIGN) || (OHCI_ED_ALIGN == 0) ||	\
52184610Salfred	(USB_PAGE_SIZE < OHCI_TD_ALIGN) || (OHCI_TD_ALIGN == 0) ||	\
53184610Salfred	(USB_PAGE_SIZE < OHCI_ITD_ALIGN) || (OHCI_ITD_ALIGN == 0) ||	\
54184610Salfred	(USB_PAGE_SIZE < OHCI_PAGE_SIZE) || (OHCI_PAGE_SIZE == 0))
55184610Salfred#error	"Invalid USB page size!"
56184610Salfred#endif
57184610Salfred
58184610Salfred#define	OHCI_VIRTUAL_FRAMELIST_COUNT 128/* dummy */
59184610Salfred
60184610Salfred#if (OHCI_VIRTUAL_FRAMELIST_COUNT < USB_MAX_FS_ISOC_FRAMES_PER_XFER)
61184610Salfred#error "maximum number of full-speed isochronous frames is higher than supported!"
62184610Salfred#endif
63184610Salfred
64184610Salfredstruct ohci_hcca {
65184610Salfred	volatile uint32_t hcca_interrupt_table[OHCI_NO_INTRS];
66184610Salfred	volatile uint32_t hcca_frame_number;
67184610Salfred	volatile uint32_t hcca_done_head;
68184610Salfred#define	OHCI_DONE_INTRS		1
69184610Salfred} __aligned(OHCI_HCCA_ALIGN);
70184610Salfred
71184610Salfredtypedef struct ohci_hcca ohci_hcca_t;
72184610Salfred
73184610Salfredstruct ohci_ed {
74184610Salfred	volatile uint32_t ed_flags;
75184610Salfred#define	OHCI_ED_GET_FA(s)	((s) & 0x7f)
76184610Salfred#define	OHCI_ED_ADDRMASK	0x0000007f
77184610Salfred#define	OHCI_ED_SET_FA(s)	(s)
78184610Salfred#define	OHCI_ED_GET_EN(s)	(((s) >> 7) & 0xf)
79184610Salfred#define	OHCI_ED_SET_EN(s)	((s) << 7)
80184610Salfred#define	OHCI_ED_DIR_MASK	0x00001800
81184610Salfred#define	OHCI_ED_DIR_TD		0x00000000
82184610Salfred#define	OHCI_ED_DIR_OUT		0x00000800
83184610Salfred#define	OHCI_ED_DIR_IN		0x00001000
84184610Salfred#define	OHCI_ED_SPEED		0x00002000
85184610Salfred#define	OHCI_ED_SKIP		0x00004000
86184610Salfred#define	OHCI_ED_FORMAT_GEN	0x00000000
87184610Salfred#define	OHCI_ED_FORMAT_ISO	0x00008000
88184610Salfred#define	OHCI_ED_GET_MAXP(s)	(((s) >> 16) & 0x07ff)
89184610Salfred#define	OHCI_ED_SET_MAXP(s)	((s) << 16)
90184610Salfred#define	OHCI_ED_MAXPMASK	(0x7ff << 16)
91184610Salfred	volatile uint32_t ed_tailp;
92184610Salfred	volatile uint32_t ed_headp;
93184610Salfred#define	OHCI_HALTED		0x00000001
94184610Salfred#define	OHCI_TOGGLECARRY	0x00000002
95184610Salfred#define	OHCI_HEADMASK		0xfffffffc
96184610Salfred	volatile uint32_t ed_next;
97184610Salfred/*
98184610Salfred * Extra information needed:
99184610Salfred */
100184610Salfred	struct ohci_ed *next;
101184610Salfred	struct ohci_ed *prev;
102184610Salfred	struct ohci_ed *obj_next;
103192984Sthompsa	struct usb_page_cache *page_cache;
104184610Salfred	uint32_t ed_self;
105184610Salfred} __aligned(OHCI_ED_ALIGN);
106184610Salfred
107184610Salfredtypedef struct ohci_ed ohci_ed_t;
108184610Salfred
109184610Salfredstruct ohci_td {
110184610Salfred	volatile uint32_t td_flags;
111184610Salfred#define	OHCI_TD_R		0x00040000	/* Buffer Rounding  */
112184610Salfred#define	OHCI_TD_DP_MASK		0x00180000	/* Direction / PID */
113184610Salfred#define	OHCI_TD_SETUP		0x00000000
114184610Salfred#define	OHCI_TD_OUT		0x00080000
115184610Salfred#define	OHCI_TD_IN		0x00100000
116184610Salfred#define	OHCI_TD_GET_DI(x)	(((x) >> 21) & 7)	/* Delay Interrupt */
117184610Salfred#define	OHCI_TD_SET_DI(x)	((x) << 21)
118184610Salfred#define	OHCI_TD_NOINTR		0x00e00000
119184610Salfred#define	OHCI_TD_INTR_MASK	0x00e00000
120184610Salfred#define	OHCI_TD_TOGGLE_CARRY	0x00000000
121184610Salfred#define	OHCI_TD_TOGGLE_0	0x02000000
122184610Salfred#define	OHCI_TD_TOGGLE_1	0x03000000
123184610Salfred#define	OHCI_TD_TOGGLE_MASK	0x03000000
124184610Salfred#define	OHCI_TD_GET_EC(x)	(((x) >> 26) & 3)	/* Error Count */
125184610Salfred#define	OHCI_TD_GET_CC(x)	((x) >> 28)	/* Condition Code */
126184610Salfred#define	OHCI_TD_SET_CC(x)	((x) << 28)
127184610Salfred#define	OHCI_TD_NOCC		0xf0000000
128184610Salfred	volatile uint32_t td_cbp;	/* Current Buffer Pointer */
129184610Salfred	volatile uint32_t td_next;	/* Next TD */
130184610Salfred#define	OHCI_TD_NEXT_END	0
131184610Salfred	volatile uint32_t td_be;	/* Buffer End */
132184610Salfred/*
133184610Salfred * Extra information needed:
134184610Salfred */
135184610Salfred	struct ohci_td *obj_next;
136184610Salfred	struct ohci_td *alt_next;
137192984Sthompsa	struct usb_page_cache *page_cache;
138184610Salfred	uint32_t td_self;
139184610Salfred	uint16_t len;
140184610Salfred} __aligned(OHCI_TD_ALIGN);
141184610Salfred
142184610Salfredtypedef struct ohci_td ohci_td_t;
143184610Salfred
144184610Salfredstruct ohci_itd {
145184610Salfred	volatile uint32_t itd_flags;
146184610Salfred#define	OHCI_ITD_GET_SF(x)	((x) & 0x0000ffff)
147184610Salfred#define	OHCI_ITD_SET_SF(x)	((x) & 0xffff)
148184610Salfred#define	OHCI_ITD_GET_DI(x)	(((x) >> 21) & 7)	/* Delay Interrupt */
149184610Salfred#define	OHCI_ITD_SET_DI(x)	((x) << 21)
150184610Salfred#define	OHCI_ITD_NOINTR		0x00e00000
151184610Salfred#define	OHCI_ITD_GET_FC(x)	((((x) >> 24) & 7)+1)	/* Frame Count */
152184610Salfred#define	OHCI_ITD_SET_FC(x)	(((x)-1) << 24)
153184610Salfred#define	OHCI_ITD_GET_CC(x)	((x) >> 28)	/* Condition Code */
154184610Salfred#define	OHCI_ITD_NOCC		0xf0000000
155184610Salfred#define	OHCI_ITD_NOFFSET	8
156184610Salfred	volatile uint32_t itd_bp0;	/* Buffer Page 0 */
157184610Salfred	volatile uint32_t itd_next;	/* Next ITD */
158184610Salfred	volatile uint32_t itd_be;	/* Buffer End */
159184610Salfred	volatile uint16_t itd_offset[OHCI_ITD_NOFFSET];	/* Buffer offsets and
160184610Salfred							 * Status */
161184610Salfred#define	OHCI_ITD_PAGE_SELECT	0x00001000
162184610Salfred#define	OHCI_ITD_MK_OFFS(len)	(0xe000 | ((len) & 0x1fff))
163184610Salfred#define	OHCI_ITD_PSW_LENGTH(x)	((x) & 0xfff)	/* Transfer length */
164184610Salfred#define	OHCI_ITD_PSW_GET_CC(x)	((x) >> 12)	/* Condition Code */
165184610Salfred/*
166184610Salfred * Extra information needed:
167184610Salfred */
168184610Salfred	struct ohci_itd *obj_next;
169192984Sthompsa	struct usb_page_cache *page_cache;
170184610Salfred	uint32_t itd_self;
171184610Salfred	uint8_t	frames;
172184610Salfred} __aligned(OHCI_ITD_ALIGN);
173184610Salfred
174184610Salfredtypedef struct ohci_itd ohci_itd_t;
175184610Salfred
176184610Salfred#define	OHCI_CC_NO_ERROR		0
177184610Salfred#define	OHCI_CC_CRC			1
178184610Salfred#define	OHCI_CC_BIT_STUFFING		2
179184610Salfred#define	OHCI_CC_DATA_TOGGLE_MISMATCH	3
180184610Salfred#define	OHCI_CC_STALL			4
181184610Salfred#define	OHCI_CC_DEVICE_NOT_RESPONDING	5
182184610Salfred#define	OHCI_CC_PID_CHECK_FAILURE	6
183184610Salfred#define	OHCI_CC_UNEXPECTED_PID		7
184184610Salfred#define	OHCI_CC_DATA_OVERRUN		8
185184610Salfred#define	OHCI_CC_DATA_UNDERRUN		9
186184610Salfred#define	OHCI_CC_BUFFER_OVERRUN		12
187184610Salfred#define	OHCI_CC_BUFFER_UNDERRUN		13
188184610Salfred#define	OHCI_CC_NOT_ACCESSED		15
189184610Salfred
190184610Salfred/* Some delay needed when changing certain registers. */
191184610Salfred#define	OHCI_ENABLE_POWER_DELAY		5
192184610Salfred#define	OHCI_READ_DESC_DELAY		5
193184610Salfred
194184610Salfred#define	OHCI_NO_EDS			(2*OHCI_NO_INTRS)
195184610Salfred
196184610Salfredstruct ohci_hw_softc {
197192984Sthompsa	struct usb_page_cache hcca_pc;
198192984Sthompsa	struct usb_page_cache ctrl_start_pc;
199192984Sthompsa	struct usb_page_cache bulk_start_pc;
200192984Sthompsa	struct usb_page_cache isoc_start_pc;
201192984Sthompsa	struct usb_page_cache intr_start_pc[OHCI_NO_EDS];
202184610Salfred
203192984Sthompsa	struct usb_page hcca_pg;
204192984Sthompsa	struct usb_page ctrl_start_pg;
205192984Sthompsa	struct usb_page bulk_start_pg;
206192984Sthompsa	struct usb_page isoc_start_pg;
207192984Sthompsa	struct usb_page intr_start_pg[OHCI_NO_EDS];
208184610Salfred};
209184610Salfred
210184610Salfredstruct ohci_config_desc {
211192984Sthompsa	struct usb_config_descriptor confd;
212192984Sthompsa	struct usb_interface_descriptor ifcd;
213192984Sthompsa	struct usb_endpoint_descriptor endpd;
214184610Salfred} __packed;
215184610Salfred
216184610Salfredunion ohci_hub_desc {
217192984Sthompsa	struct usb_status stat;
218192984Sthompsa	struct usb_port_status ps;
219192984Sthompsa	struct usb_hub_descriptor hubd;
220184610Salfred	uint8_t	temp[128];
221184610Salfred};
222184610Salfred
223184610Salfredtypedef struct ohci_softc {
224184610Salfred	struct ohci_hw_softc sc_hw;
225192984Sthompsa	struct usb_bus sc_bus;		/* base device */
226192984Sthompsa	struct usb_callout sc_tmo_rhsc;
227184610Salfred	union ohci_hub_desc sc_hub_desc;
228184610Salfred
229192984Sthompsa	struct usb_device *sc_devices[OHCI_MAX_DEVICES];
230184610Salfred	struct resource *sc_io_res;
231184610Salfred	struct resource *sc_irq_res;
232184610Salfred	struct ohci_hcca *sc_hcca_p;
233184610Salfred	struct ohci_ed *sc_ctrl_p_last;
234184610Salfred	struct ohci_ed *sc_bulk_p_last;
235184610Salfred	struct ohci_ed *sc_isoc_p_last;
236184610Salfred	struct ohci_ed *sc_intr_p_last[OHCI_NO_EDS];
237184610Salfred	void   *sc_intr_hdl;
238184610Salfred	device_t sc_dev;
239184610Salfred	bus_size_t sc_io_size;
240184610Salfred	bus_space_tag_t sc_io_tag;
241184610Salfred	bus_space_handle_t sc_io_hdl;
242184610Salfred
243184610Salfred	uint32_t sc_eintrs;		/* enabled interrupts */
244184610Salfred
245184610Salfred	uint16_t sc_intr_stat[OHCI_NO_EDS];
246184610Salfred	uint16_t sc_id_vendor;
247184610Salfred
248184610Salfred	uint8_t	sc_noport;
249184610Salfred	uint8_t	sc_addr;		/* device address */
250184610Salfred	uint8_t	sc_conf;		/* device configuration */
251184610Salfred	uint8_t	sc_hub_idata[32];
252184610Salfred
253184610Salfred	char	sc_vendor[16];
254184610Salfred
255184610Salfred} ohci_softc_t;
256184610Salfred
257193045Sthompsausb_bus_mem_cb_t ohci_iterate_hw_softc;
258184610Salfred
259193045Sthompsausb_error_t ohci_init(ohci_softc_t *sc);
260184610Salfredvoid	ohci_detach(struct ohci_softc *sc);
261184610Salfredvoid	ohci_interrupt(ohci_softc_t *sc);
262184610Salfred
263184610Salfred#endif					/* _OHCI_H_ */
264