102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 4718cf2ccSPedro 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 _UHCI_H_ 3502ac6454SAndrew Thompson #define _UHCI_H_ 3602ac6454SAndrew Thompson 37ab42e8b2SAndrew Thompson #define UHCI_MAX_DEVICES MIN(USB_MAX_DEVICES, 128) 3802ac6454SAndrew Thompson 3902ac6454SAndrew Thompson #define UHCI_FRAMELIST_COUNT 1024 /* units */ 4002ac6454SAndrew Thompson #define UHCI_FRAMELIST_ALIGN 4096 /* bytes */ 4102ac6454SAndrew Thompson 4202ac6454SAndrew Thompson /* Structures alignment (bytes) */ 4302ac6454SAndrew Thompson #define UHCI_TD_ALIGN 16 4402ac6454SAndrew Thompson #define UHCI_QH_ALIGN 16 4502ac6454SAndrew Thompson 4602ac6454SAndrew Thompson #if ((USB_PAGE_SIZE < UHCI_TD_ALIGN) || (UHCI_TD_ALIGN == 0) || \ 4702ac6454SAndrew Thompson (USB_PAGE_SIZE < UHCI_QH_ALIGN) || (UHCI_QH_ALIGN == 0)) 4802ac6454SAndrew Thompson #error "Invalid USB page size!" 4902ac6454SAndrew Thompson #endif 5002ac6454SAndrew Thompson 5102ac6454SAndrew Thompson typedef uint32_t uhci_physaddr_t; 5202ac6454SAndrew Thompson 5302ac6454SAndrew Thompson #define UHCI_PTR_T 0x00000001 5402ac6454SAndrew Thompson #define UHCI_PTR_TD 0x00000000 5502ac6454SAndrew Thompson #define UHCI_PTR_QH 0x00000002 5602ac6454SAndrew Thompson #define UHCI_PTR_VF 0x00000004 5702ac6454SAndrew Thompson 5802ac6454SAndrew Thompson /* 5902ac6454SAndrew Thompson * The Queue Heads (QH) and Transfer Descriptors (TD) are accessed by 6002ac6454SAndrew Thompson * both the CPU and the USB-controller which run concurrently. Great 6102ac6454SAndrew Thompson * care must be taken. When the data-structures are linked into the 6202ac6454SAndrew Thompson * USB controller's frame list, the USB-controller "owns" the 6302ac6454SAndrew Thompson * td_status and qh_elink fields, which will not be written by the 6402ac6454SAndrew Thompson * CPU. 6502ac6454SAndrew Thompson * 6602ac6454SAndrew Thompson */ 6702ac6454SAndrew Thompson 6802ac6454SAndrew Thompson struct uhci_td { 6902ac6454SAndrew Thompson /* 7002ac6454SAndrew Thompson * Data used by the UHCI controller. 7102ac6454SAndrew Thompson * volatile is used in order to mantain struct members ordering. 7202ac6454SAndrew Thompson */ 7302ac6454SAndrew Thompson volatile uint32_t td_next; 7402ac6454SAndrew Thompson volatile uint32_t td_status; 7502ac6454SAndrew Thompson #define UHCI_TD_GET_ACTLEN(s) (((s) + 1) & 0x3ff) 7602ac6454SAndrew Thompson #define UHCI_TD_ZERO_ACTLEN(t) ((t) | 0x3ff) 7702ac6454SAndrew Thompson #define UHCI_TD_BITSTUFF 0x00020000 7802ac6454SAndrew Thompson #define UHCI_TD_CRCTO 0x00040000 7902ac6454SAndrew Thompson #define UHCI_TD_NAK 0x00080000 8002ac6454SAndrew Thompson #define UHCI_TD_BABBLE 0x00100000 8102ac6454SAndrew Thompson #define UHCI_TD_DBUFFER 0x00200000 8202ac6454SAndrew Thompson #define UHCI_TD_STALLED 0x00400000 8302ac6454SAndrew Thompson #define UHCI_TD_ACTIVE 0x00800000 8402ac6454SAndrew Thompson #define UHCI_TD_IOC 0x01000000 8502ac6454SAndrew Thompson #define UHCI_TD_IOS 0x02000000 8602ac6454SAndrew Thompson #define UHCI_TD_LS 0x04000000 8702ac6454SAndrew Thompson #define UHCI_TD_GET_ERRCNT(s) (((s) >> 27) & 3) 8802ac6454SAndrew Thompson #define UHCI_TD_SET_ERRCNT(n) ((n) << 27) 8902ac6454SAndrew Thompson #define UHCI_TD_SPD 0x20000000 9002ac6454SAndrew Thompson volatile uint32_t td_token; 9102ac6454SAndrew Thompson #define UHCI_TD_PID 0x000000ff 9202ac6454SAndrew Thompson #define UHCI_TD_PID_IN 0x00000069 9302ac6454SAndrew Thompson #define UHCI_TD_PID_OUT 0x000000e1 9402ac6454SAndrew Thompson #define UHCI_TD_PID_SETUP 0x0000002d 9502ac6454SAndrew Thompson #define UHCI_TD_GET_PID(s) ((s) & 0xff) 9602ac6454SAndrew Thompson #define UHCI_TD_SET_DEVADDR(a) ((a) << 8) 9702ac6454SAndrew Thompson #define UHCI_TD_GET_DEVADDR(s) (((s) >> 8) & 0x7f) 9802ac6454SAndrew Thompson #define UHCI_TD_SET_ENDPT(e) (((e) & 0xf) << 15) 9902ac6454SAndrew Thompson #define UHCI_TD_GET_ENDPT(s) (((s) >> 15) & 0xf) 10002ac6454SAndrew Thompson #define UHCI_TD_SET_DT(t) ((t) << 19) 10102ac6454SAndrew Thompson #define UHCI_TD_GET_DT(s) (((s) >> 19) & 1) 102db8409e0SHans Petter Selasky #define UHCI_TD_SET_MAXLEN(l) (((l)-1U) << 21) 10302ac6454SAndrew Thompson #define UHCI_TD_GET_MAXLEN(s) ((((s) >> 21) + 1) & 0x7ff) 10402ac6454SAndrew Thompson #define UHCI_TD_MAXLEN_MASK 0xffe00000 10502ac6454SAndrew Thompson volatile uint32_t td_buffer; 10602ac6454SAndrew Thompson /* 10702ac6454SAndrew Thompson * Extra information needed: 10802ac6454SAndrew Thompson */ 10902ac6454SAndrew Thompson struct uhci_td *next; 11002ac6454SAndrew Thompson struct uhci_td *prev; 11102ac6454SAndrew Thompson struct uhci_td *obj_next; 112760bc48eSAndrew Thompson struct usb_page_cache *page_cache; 113760bc48eSAndrew Thompson struct usb_page_cache *fix_pc; 11402ac6454SAndrew Thompson uint32_t td_self; 11502ac6454SAndrew Thompson uint16_t len; 11602ac6454SAndrew Thompson } __aligned(UHCI_TD_ALIGN); 11702ac6454SAndrew Thompson 11802ac6454SAndrew Thompson typedef struct uhci_td uhci_td_t; 11902ac6454SAndrew Thompson 12002ac6454SAndrew Thompson #define UHCI_TD_ERROR (UHCI_TD_BITSTUFF | UHCI_TD_CRCTO | \ 12102ac6454SAndrew Thompson UHCI_TD_BABBLE | UHCI_TD_DBUFFER | UHCI_TD_STALLED) 12202ac6454SAndrew Thompson 12302ac6454SAndrew Thompson #define UHCI_TD_SETUP(len, endp, dev) (UHCI_TD_SET_MAXLEN(len) | \ 12402ac6454SAndrew Thompson UHCI_TD_SET_ENDPT(endp) | \ 12502ac6454SAndrew Thompson UHCI_TD_SET_DEVADDR(dev) | \ 12602ac6454SAndrew Thompson UHCI_TD_PID_SETUP) 12702ac6454SAndrew Thompson 12802ac6454SAndrew Thompson #define UHCI_TD_OUT(len, endp, dev, dt) (UHCI_TD_SET_MAXLEN(len) | \ 12902ac6454SAndrew Thompson UHCI_TD_SET_ENDPT(endp) | \ 13002ac6454SAndrew Thompson UHCI_TD_SET_DEVADDR(dev) | \ 13102ac6454SAndrew Thompson UHCI_TD_PID_OUT | UHCI_TD_SET_DT(dt)) 13202ac6454SAndrew Thompson 13302ac6454SAndrew Thompson #define UHCI_TD_IN(len, endp, dev, dt) (UHCI_TD_SET_MAXLEN(len) | \ 13402ac6454SAndrew Thompson UHCI_TD_SET_ENDPT(endp) | \ 13502ac6454SAndrew Thompson UHCI_TD_SET_DEVADDR(dev) | \ 13602ac6454SAndrew Thompson UHCI_TD_PID_IN | UHCI_TD_SET_DT(dt)) 13702ac6454SAndrew Thompson 13802ac6454SAndrew Thompson struct uhci_qh { 13902ac6454SAndrew Thompson /* 14002ac6454SAndrew Thompson * Data used by the UHCI controller. 14102ac6454SAndrew Thompson */ 14202ac6454SAndrew Thompson volatile uint32_t qh_h_next; 14302ac6454SAndrew Thompson volatile uint32_t qh_e_next; 14402ac6454SAndrew Thompson /* 14502ac6454SAndrew Thompson * Extra information needed: 14602ac6454SAndrew Thompson */ 14702ac6454SAndrew Thompson struct uhci_qh *h_next; 14802ac6454SAndrew Thompson struct uhci_qh *h_prev; 14902ac6454SAndrew Thompson struct uhci_qh *obj_next; 15002ac6454SAndrew Thompson struct uhci_td *e_next; 151760bc48eSAndrew Thompson struct usb_page_cache *page_cache; 15202ac6454SAndrew Thompson uint32_t qh_self; 15302ac6454SAndrew Thompson uint16_t intr_pos; 15402ac6454SAndrew Thompson } __aligned(UHCI_QH_ALIGN); 15502ac6454SAndrew Thompson 15602ac6454SAndrew Thompson typedef struct uhci_qh uhci_qh_t; 15702ac6454SAndrew Thompson 15802ac6454SAndrew Thompson /* Maximum number of isochronous TD's and QH's interrupt */ 15902ac6454SAndrew Thompson #define UHCI_VFRAMELIST_COUNT 128 16002ac6454SAndrew Thompson #define UHCI_IFRAMELIST_COUNT (2 * UHCI_VFRAMELIST_COUNT) 16102ac6454SAndrew Thompson 16202ac6454SAndrew Thompson #if (((UHCI_VFRAMELIST_COUNT & (UHCI_VFRAMELIST_COUNT-1)) != 0) || \ 16302ac6454SAndrew Thompson (UHCI_VFRAMELIST_COUNT > UHCI_FRAMELIST_COUNT)) 16402ac6454SAndrew Thompson #error "UHCI_VFRAMELIST_COUNT is not power of two" 16502ac6454SAndrew Thompson #error "or UHCI_VFRAMELIST_COUNT > UHCI_FRAMELIST_COUNT" 16602ac6454SAndrew Thompson #endif 16702ac6454SAndrew Thompson 16802ac6454SAndrew Thompson #if (UHCI_VFRAMELIST_COUNT < USB_MAX_FS_ISOC_FRAMES_PER_XFER) 16902ac6454SAndrew Thompson #error "maximum number of full-speed isochronous frames is higher than supported!" 17002ac6454SAndrew Thompson #endif 17102ac6454SAndrew Thompson 17202ac6454SAndrew Thompson struct uhci_config_desc { 173760bc48eSAndrew Thompson struct usb_config_descriptor confd; 174760bc48eSAndrew Thompson struct usb_interface_descriptor ifcd; 175760bc48eSAndrew Thompson struct usb_endpoint_descriptor endpd; 17602ac6454SAndrew Thompson } __packed; 17702ac6454SAndrew Thompson 17802ac6454SAndrew Thompson union uhci_hub_desc { 179760bc48eSAndrew Thompson struct usb_status stat; 180760bc48eSAndrew Thompson struct usb_port_status ps; 18102ac6454SAndrew Thompson uint8_t temp[128]; 18202ac6454SAndrew Thompson }; 18302ac6454SAndrew Thompson 18402ac6454SAndrew Thompson struct uhci_hw_softc { 185760bc48eSAndrew Thompson struct usb_page_cache pframes_pc; 186760bc48eSAndrew Thompson struct usb_page_cache isoc_start_pc[UHCI_VFRAMELIST_COUNT]; 187760bc48eSAndrew Thompson struct usb_page_cache intr_start_pc[UHCI_IFRAMELIST_COUNT]; 188760bc48eSAndrew Thompson struct usb_page_cache ls_ctl_start_pc; 189760bc48eSAndrew Thompson struct usb_page_cache fs_ctl_start_pc; 190760bc48eSAndrew Thompson struct usb_page_cache bulk_start_pc; 191760bc48eSAndrew Thompson struct usb_page_cache last_qh_pc; 192760bc48eSAndrew Thompson struct usb_page_cache last_td_pc; 19302ac6454SAndrew Thompson 194760bc48eSAndrew Thompson struct usb_page pframes_pg; 195760bc48eSAndrew Thompson struct usb_page isoc_start_pg[UHCI_VFRAMELIST_COUNT]; 196760bc48eSAndrew Thompson struct usb_page intr_start_pg[UHCI_IFRAMELIST_COUNT]; 197760bc48eSAndrew Thompson struct usb_page ls_ctl_start_pg; 198760bc48eSAndrew Thompson struct usb_page fs_ctl_start_pg; 199760bc48eSAndrew Thompson struct usb_page bulk_start_pg; 200760bc48eSAndrew Thompson struct usb_page last_qh_pg; 201760bc48eSAndrew Thompson struct usb_page last_td_pg; 20202ac6454SAndrew Thompson }; 20302ac6454SAndrew Thompson 20402ac6454SAndrew Thompson typedef struct uhci_softc { 20502ac6454SAndrew Thompson struct uhci_hw_softc sc_hw; 206760bc48eSAndrew Thompson struct usb_bus sc_bus; /* base device */ 20702ac6454SAndrew Thompson union uhci_hub_desc sc_hub_desc; 208760bc48eSAndrew Thompson struct usb_callout sc_root_intr; 20902ac6454SAndrew Thompson 210760bc48eSAndrew Thompson struct usb_device *sc_devices[UHCI_MAX_DEVICES]; 21139307315SAndrew Thompson /* pointer to last TD for isochronous */ 21239307315SAndrew Thompson struct uhci_td *sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]; 21339307315SAndrew Thompson /* pointer to last QH for interrupt */ 21439307315SAndrew Thompson struct uhci_qh *sc_intr_p_last[UHCI_IFRAMELIST_COUNT]; 21539307315SAndrew Thompson /* pointer to last QH for low speed control */ 21639307315SAndrew Thompson struct uhci_qh *sc_ls_ctl_p_last; 21739307315SAndrew Thompson /* pointer to last QH for full speed control */ 21839307315SAndrew Thompson struct uhci_qh *sc_fs_ctl_p_last; 21939307315SAndrew Thompson /* pointer to last QH for bulk */ 22039307315SAndrew Thompson struct uhci_qh *sc_bulk_p_last; 22102ac6454SAndrew Thompson struct uhci_qh *sc_reclaim_qh_p; 22202ac6454SAndrew Thompson struct uhci_qh *sc_last_qh_p; 22302ac6454SAndrew Thompson struct uhci_td *sc_last_td_p; 22402ac6454SAndrew Thompson struct resource *sc_io_res; 22502ac6454SAndrew Thompson struct resource *sc_irq_res; 22602ac6454SAndrew Thompson void *sc_intr_hdl; 22702ac6454SAndrew Thompson device_t sc_dev; 22802ac6454SAndrew Thompson bus_size_t sc_io_size; 22902ac6454SAndrew Thompson bus_space_tag_t sc_io_tag; 23002ac6454SAndrew Thompson bus_space_handle_t sc_io_hdl; 23102ac6454SAndrew Thompson 23202ac6454SAndrew Thompson uint32_t sc_loops; /* number of QHs that wants looping */ 23302ac6454SAndrew Thompson 23402ac6454SAndrew Thompson uint16_t sc_intr_stat[UHCI_IFRAMELIST_COUNT]; 23502ac6454SAndrew Thompson 23602ac6454SAndrew Thompson uint8_t sc_addr; /* device address */ 23702ac6454SAndrew Thompson uint8_t sc_conf; /* device configuration */ 23802ac6454SAndrew Thompson uint8_t sc_isreset; /* bits set if a root hub is reset */ 23902ac6454SAndrew Thompson uint8_t sc_isresumed; /* bits set if a port was resumed */ 24002ac6454SAndrew Thompson uint8_t sc_hub_idata[1]; 24102ac6454SAndrew Thompson 24202ac6454SAndrew Thompson char sc_vendor[16]; /* vendor string for root hub */ 24302ac6454SAndrew Thompson } uhci_softc_t; 24402ac6454SAndrew Thompson 245e0a69b51SAndrew Thompson usb_bus_mem_cb_t uhci_iterate_hw_softc; 24602ac6454SAndrew Thompson 247e0a69b51SAndrew Thompson usb_error_t uhci_init(uhci_softc_t *sc); 24802ac6454SAndrew Thompson void uhci_reset(uhci_softc_t *sc); 24902ac6454SAndrew Thompson void uhci_interrupt(uhci_softc_t *sc); 25002ac6454SAndrew Thompson 25102ac6454SAndrew Thompson #endif /* _UHCI_H_ */ 252