xref: /freebsd/sys/dev/usb/controller/dwc_otg.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _DWC_OTG_H_
30 #define	_DWC_OTG_H_
31 
32 #define	DWC_OTG_MAX_DEVICES MIN(USB_MAX_DEVICES, 32)
33 #define	DWC_OTG_FRAME_MASK 0x7FF
34 #define	DWC_OTG_MAX_TXP 4
35 #define	DWC_OTG_MAX_TXN (0x200 * DWC_OTG_MAX_TXP)
36 #define	DWC_OTG_MAX_CHANNELS 16
37 #define	DWC_OTG_MAX_ENDPOINTS 16
38 #define	DWC_OTG_HOST_TIMER_RATE 10 /* ms */
39 #define	DWC_OTG_TT_SLOT_MAX 8
40 #define	DWC_OTG_SLOT_IDLE_MAX 3
41 #define	DWC_OTG_SLOT_IDLE_MIN 2
42 #ifndef DWC_OTG_TX_MAX_FIFO_SIZE
43 #define	DWC_OTG_TX_MAX_FIFO_SIZE DWC_OTG_MAX_TXN
44 #endif
45 
46 #define	DWC_OTG_READ_4(sc, reg) \
47   bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg)
48 
49 #define	DWC_OTG_WRITE_4(sc, reg, data)	\
50   bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data)
51 
52 struct dwc_otg_td;
53 struct dwc_otg_softc;
54 
55 typedef uint8_t (dwc_otg_cmd_t)(struct dwc_otg_softc *sc, struct dwc_otg_td *td);
56 
57 struct dwc_otg_td {
58 	struct dwc_otg_td *obj_next;
59 	dwc_otg_cmd_t *func;
60 	struct usb_page_cache *pc;
61 	uint32_t tx_bytes;
62 	uint32_t offset;
63 	uint32_t remainder;
64 	uint32_t hcchar;		/* HOST CFG */
65 	uint32_t hcsplt;		/* HOST CFG */
66 	uint16_t max_packet_size;	/* packet_size */
67 	uint16_t npkt;
68 	uint8_t max_packet_count;	/* packet_count */
69 	uint8_t errcnt;
70 	uint8_t tmr_res;
71 	uint8_t tmr_val;
72 	uint8_t	ep_no;
73 	uint8_t ep_type;
74 	uint8_t channel[3];
75 	uint8_t tt_index;		/* TT data */
76 	uint8_t tt_start_slot;		/* TT data */
77 	uint8_t tt_complete_slot;	/* TT data */
78 	uint8_t tt_xactpos;		/* TT data */
79 	uint8_t state;
80 #define	DWC_CHAN_ST_START 0
81 #define	DWC_CHAN_ST_WAIT_ANE 1
82 #define	DWC_CHAN_ST_WAIT_S_ANE 2
83 #define	DWC_CHAN_ST_WAIT_C_ANE 3
84 #define	DWC_CHAN_ST_WAIT_C_PKT 4
85 #define	DWC_CHAN_ST_TX_WAIT_ISOC 5
86 	uint8_t	error_any:1;
87 	uint8_t	error_stall:1;
88 	uint8_t	alt_next:1;
89 	uint8_t	short_pkt:1;
90 	uint8_t	did_stall:1;
91 	uint8_t toggle:1;
92 	uint8_t set_toggle:1;
93 	uint8_t got_short:1;
94 	uint8_t tt_scheduled:1;
95 	uint8_t did_nak:1;
96 };
97 
98 struct dwc_otg_tt_info {
99 	uint8_t slot_index;
100 };
101 
102 struct dwc_otg_std_temp {
103 	dwc_otg_cmd_t *func;
104 	struct usb_page_cache *pc;
105 	struct dwc_otg_td *td;
106 	struct dwc_otg_td *td_next;
107 	uint32_t len;
108 	uint32_t offset;
109 	uint16_t max_frame_size;
110 	uint8_t	short_pkt;
111 
112 	/*
113 	 * short_pkt = 0: transfer should be short terminated
114 	 * short_pkt = 1: transfer should not be short terminated
115 	 */
116 	uint8_t	setup_alt_next;
117 	uint8_t did_stall;
118 	uint8_t bulk_or_control;
119 };
120 
121 struct dwc_otg_config_desc {
122 	struct usb_config_descriptor confd;
123 	struct usb_interface_descriptor ifcd;
124 	struct usb_endpoint_descriptor endpd;
125 } __packed;
126 
127 union dwc_otg_hub_temp {
128 	uWord	wValue;
129 	struct usb_port_status ps;
130 };
131 
132 struct dwc_otg_flags {
133 	uint8_t	change_connect:1;
134 	uint8_t	change_suspend:1;
135 	uint8_t change_reset:1;
136 	uint8_t change_enabled:1;
137 	uint8_t change_over_current:1;
138 	uint8_t	status_suspend:1;	/* set if suspended */
139 	uint8_t	status_vbus:1;		/* set if present */
140 	uint8_t	status_bus_reset:1;	/* set if reset complete */
141 	uint8_t	status_high_speed:1;	/* set if High Speed is selected */
142 	uint8_t	status_low_speed:1;	/* set if Low Speed is selected */
143 	uint8_t status_device_mode:1;	/* set if device mode */
144 	uint8_t	self_powered:1;
145 	uint8_t	clocks_off:1;
146 	uint8_t	port_powered:1;
147 	uint8_t	port_enabled:1;
148 	uint8_t port_over_current:1;
149 	uint8_t	d_pulled_up:1;
150 };
151 
152 struct dwc_otg_profile {
153 	struct usb_hw_ep_profile usb;
154 	uint16_t max_buffer;
155 };
156 
157 struct dwc_otg_chan_state {
158 	uint16_t allocated;
159 	uint16_t wait_halted;
160 	uint32_t hcint;
161 };
162 
163 struct dwc_otg_softc {
164 	struct usb_bus sc_bus;
165 	union dwc_otg_hub_temp sc_hub_temp;
166 	struct dwc_otg_profile sc_hw_ep_profile[DWC_OTG_MAX_ENDPOINTS];
167 	struct dwc_otg_tt_info sc_tt_info[DWC_OTG_MAX_DEVICES];
168 	struct usb_callout sc_timer;
169 
170 	struct usb_device *sc_devices[DWC_OTG_MAX_DEVICES];
171 	struct resource *sc_io_res;
172 	struct resource *sc_irq_res;
173 	void   *sc_intr_hdl;
174 	bus_size_t sc_io_size;
175 	bus_space_tag_t sc_io_tag;
176 	bus_space_handle_t sc_io_hdl;
177 
178 	uint32_t sc_bounce_buffer[MAX(512 * DWC_OTG_MAX_TXP, 1024) / 4];
179 
180 	uint32_t sc_fifo_size;
181 	uint32_t sc_irq_mask;
182 	uint32_t sc_last_rx_status;
183 	uint32_t sc_out_ctl[DWC_OTG_MAX_ENDPOINTS];
184 	uint32_t sc_in_ctl[DWC_OTG_MAX_ENDPOINTS];
185 	struct dwc_otg_chan_state sc_chan_state[DWC_OTG_MAX_CHANNELS];
186 	uint32_t sc_tmr_val;
187 	uint32_t sc_hprt_val;
188 	uint32_t sc_xfer_complete;
189 
190 	uint16_t sc_current_rx_bytes;
191 	uint16_t sc_current_rx_fifo;
192 
193 	uint16_t sc_active_rx_ep;
194 	uint16_t sc_last_frame_num;
195 
196 	uint8_t sc_phy_type;
197 	uint8_t sc_phy_bits;
198 #define	DWC_OTG_PHY_ULPI 1
199 #define	DWC_OTG_PHY_HSIC 2
200 #define	DWC_OTG_PHY_INTERNAL 3
201 #define	DWC_OTG_PHY_UTMI 4
202 
203 	uint8_t sc_timer_active;
204 	uint8_t	sc_dev_ep_max;
205 	uint8_t sc_dev_in_ep_max;
206 	uint8_t	sc_host_ch_max;
207 	uint8_t sc_needsof;
208 	uint8_t	sc_rt_addr;		/* root HUB address */
209 	uint8_t	sc_conf;		/* root HUB config */
210 	uint8_t sc_mode;		/* mode of operation */
211 #define	DWC_MODE_OTG 0		/* both modes */
212 #define	DWC_MODE_DEVICE 1	/* device only */
213 #define	DWC_MODE_HOST  2	/* host only */
214 
215 	uint8_t	sc_hub_idata[1];
216 
217 	struct dwc_otg_flags sc_flags;
218 };
219 
220 /* prototypes */
221 
222 driver_filter_t dwc_otg_filter_interrupt;
223 driver_intr_t dwc_otg_interrupt;
224 int dwc_otg_init(struct dwc_otg_softc *);
225 void dwc_otg_uninit(struct dwc_otg_softc *);
226 
227 #endif		/* _DWC_OTG_H_ */
228