xref: /netbsd/sys/arch/mips/adm5120/dev/ahci.c (revision c1eaee22)
1 /*	$NetBSD: ahci.c,v 1.32 2022/10/23 06:29:01 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions 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
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  * 3. The names of the authors may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  */
33 /*
34  * Copyright (c) 2001 The NetBSD Foundation, Inc.
35  * All rights reserved.
36  *
37  * This code is derived from software contributed to The NetBSD Foundation
38  * by Tetsuya Isaki.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  * POSSIBILITY OF SUCH DAMAGE.
60  */
61 
62 /*
63  * !! HIGHLY EXPERIMENTAL CODE !!
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: ahci.c,v 1.32 2022/10/23 06:29:01 skrll Exp $");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/proc.h>
73 #include <sys/device.h>
74 #include <sys/kmem.h>
75 
76 #include <sys/bus.h>
77 #include <machine/cpu.h>
78 
79 #include <dev/usb/usb.h>
80 #include <dev/usb/usbdi.h>
81 #include <dev/usb/usbdivar.h>
82 #include <dev/usb/usb_mem.h>
83 #include <dev/usb/usbdevs.h>
84 #include <dev/usb/usbroothub.h>
85 
86 #include <mips/adm5120/include/adm5120reg.h>
87 #include <mips/adm5120/include/adm5120var.h>
88 #include <mips/adm5120/include/adm5120_obiovar.h>
89 
90 #include <mips/adm5120/dev/ahcireg.h>
91 #include <mips/adm5120/dev/ahcivar.h>
92 
93 static usbd_status	ahci_open(struct usbd_pipe *);
94 static void		ahci_softintr(void *);
95 static void		ahci_poll(struct usbd_bus *);
96 static void		ahci_poll_hub(void *);
97 static void		ahci_poll_device(void *arg);
98 static struct usbd_xfer *
99 			ahci_allocx(struct usbd_bus *, unsigned int);
100 static void		ahci_freex(struct usbd_bus *, struct usbd_xfer *);
101 static void		ahci_abortx(struct usbd_xfer *);
102 
103 static void		ahci_get_lock(struct usbd_bus *, kmutex_t **);
104 static int		ahci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
105     void *, int);
106 
107 static usbd_status	ahci_root_intr_transfer(struct usbd_xfer *);
108 static usbd_status	ahci_root_intr_start(struct usbd_xfer *);
109 static void		ahci_root_intr_abort(struct usbd_xfer *);
110 static void		ahci_root_intr_close(struct usbd_pipe *);
111 static void		ahci_root_intr_done(struct usbd_xfer *);
112 
113 static usbd_status	ahci_device_ctrl_transfer(struct usbd_xfer *);
114 static usbd_status	ahci_device_ctrl_start(struct usbd_xfer *);
115 static void		ahci_device_ctrl_abort(struct usbd_xfer *);
116 static void		ahci_device_ctrl_close(struct usbd_pipe *);
117 static void		ahci_device_ctrl_done(struct usbd_xfer *);
118 
119 static usbd_status	ahci_device_intr_transfer(struct usbd_xfer *);
120 static usbd_status	ahci_device_intr_start(struct usbd_xfer *);
121 static void		ahci_device_intr_abort(struct usbd_xfer *);
122 static void		ahci_device_intr_close(struct usbd_pipe *);
123 static void		ahci_device_intr_done(struct usbd_xfer *);
124 
125 static usbd_status	ahci_device_isoc_transfer(struct usbd_xfer *);
126 static usbd_status	ahci_device_isoc_start(struct usbd_xfer *);
127 static void		ahci_device_isoc_abort(struct usbd_xfer *);
128 static void		ahci_device_isoc_close(struct usbd_pipe *);
129 static void		ahci_device_isoc_done(struct usbd_xfer *);
130 
131 static usbd_status	ahci_device_bulk_transfer(struct usbd_xfer *);
132 static usbd_status	ahci_device_bulk_start(struct usbd_xfer *);
133 static void		ahci_device_bulk_abort(struct usbd_xfer *);
134 static void		ahci_device_bulk_close(struct usbd_pipe *);
135 static void		ahci_device_bulk_done(struct usbd_xfer *);
136 
137 static int		ahci_transaction(struct ahci_softc *,
138 	struct usbd_pipe *, uint8_t, int, u_char *, uint8_t);
139 static void		ahci_noop(struct usbd_pipe *);
140 static void		ahci_device_clear_toggle(struct usbd_pipe *);
141 
142 extern int usbdebug;
143 extern int uhubdebug;
144 extern int umassdebug;
145 int ahci_dummy;
146 
147 #define AHCI_DEBUG
148 
149 #ifdef AHCI_DEBUG
150 #define D_TRACE	(0x0001)	/* function trace */
151 #define D_MSG	(0x0002)	/* debug messages */
152 #define D_XFER	(0x0004)	/* transfer messages (noisy!) */
153 #define D_MEM	(0x0008)	/* memory allocation */
154 
155 int ahci_debug = 0;
156 #define DPRINTF(z,x)	if((ahci_debug&(z))!=0)printf x
157 void		print_req(usb_device_request_t *);
158 void		print_req_hub(usb_device_request_t *);
159 void		print_dumpreg(struct ahci_softc *);
160 void		print_xfer(struct usbd_xfer *);
161 #else
162 #define DPRINTF(z,x)
163 #endif
164 
165 
166 struct usbd_bus_methods ahci_bus_methods = {
167 	.ubm_open = ahci_open,
168 	.ubm_softint = ahci_softintr,
169 	.ubm_dopoll = ahci_poll,
170 	.ubm_allocx = ahci_allocx,
171 	.ubm_freex = ahci_freex,
172 	.ubm_abortx = ahci_abortx,
173 	.ubm_getlock = ahci_get_lock,
174 	.ubm_rhctrl = ahci_roothub_ctrl,
175 };
176 
177 struct usbd_pipe_methods ahci_root_intr_methods = {
178 	.upm_transfer = ahci_root_intr_transfer,
179 	.upm_start = ahci_root_intr_start,
180 	.upm_abort = ahci_root_intr_abort,
181 	.upm_close = ahci_root_intr_close,
182 	.upm_cleartoggle = ahci_noop,
183 	.upm_done = ahci_root_intr_done,
184 };
185 
186 struct usbd_pipe_methods ahci_device_ctrl_methods = {
187 	.upm_transfer = ahci_device_ctrl_transfer,
188 	.upm_start = ahci_device_ctrl_start,
189 	.upm_abort = ahci_device_ctrl_abort,
190 	.upm_close = ahci_device_ctrl_close,
191 	.upm_cleartoggle = ahci_noop,
192 	.upm_done = ahci_device_ctrl_done,
193 };
194 
195 struct usbd_pipe_methods ahci_device_intr_methods = {
196 	.upm_transfer = ahci_device_intr_transfer,
197 	.upm_start = ahci_device_intr_start,
198 	.upm_abort = ahci_device_intr_abort,
199 	.upm_close = ahci_device_intr_close,
200 	.upm_cleartoggle = ahci_device_clear_toggle,
201 	.upm_done = ahci_device_intr_done,
202 };
203 
204 struct usbd_pipe_methods ahci_device_isoc_methods = {
205 	.upm_transfer = ahci_device_isoc_transfer,
206 	.upm_start = ahci_device_isoc_start,
207 	.upm_abort = ahci_device_isoc_abort,
208 	.upm_close = ahci_device_isoc_close,
209 	.upm_cleartoggle = ahci_noop,
210 	.upm_done = ahci_device_isoc_done,
211 };
212 
213 struct usbd_pipe_methods ahci_device_bulk_methods = {
214 	.upm_transfer = ahci_device_bulk_transfer,
215 	.upm_start = ahci_device_bulk_start,
216 	.upm_abort = ahci_device_bulk_abort,
217 	.upm_close = ahci_device_bulk_close,
218 	.upm_cleartoggle = ahci_device_clear_toggle,
219 	.upm_done = ahci_device_bulk_done,
220 };
221 
222 struct ahci_pipe {
223 	struct usbd_pipe pipe;
224 	uint32_t toggle;
225 };
226 
227 static int	ahci_match(device_t, cfdata_t, void *);
228 static void	ahci_attach(device_t, device_t, void *);
229 
230 CFATTACH_DECL_NEW(ahci, sizeof(struct ahci_softc),
231     ahci_match, ahci_attach, NULL, NULL);
232 
233 static int
ahci_match(device_t parent,struct cfdata * cf,void * aux)234 ahci_match(device_t parent, struct cfdata *cf, void *aux)
235 {
236 	struct obio_attach_args *aa = aux;
237 
238 	if (strcmp(aa->oba_name, cf->cf_name) == 0)
239 		return 1;
240 
241 	return 0;
242 }
243 
244 #define	REG_READ(o)	bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
245 #define	REG_WRITE(o,v)	bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
246 
247 /*
248  * Attach SL11H/SL811HS. Return 0 if success.
249  */
250 void
ahci_attach(device_t parent,device_t self,void * aux)251 ahci_attach(device_t parent, device_t self, void *aux)
252 {
253 	struct obio_attach_args *aa = aux;
254 	struct ahci_softc *sc = device_private(self);
255 
256 	printf("\n");
257 	sc->sc_dmat = aa->oba_dt;
258 	sc->sc_st = aa->oba_st;
259 
260 	/* Initialize sc */
261 	sc->sc_bus.ub_revision = USBREV_1_1;
262 	sc->sc_bus.ub_methods = &ahci_bus_methods;
263 	sc->sc_bus.ub_pipesize = sizeof(struct ahci_pipe);
264 	sc->sc_bus.ub_dmatag = sc->sc_dmat;
265 	sc->sc_bus.ub_usedma = true;
266 
267 	/* Map the device. */
268 	if (bus_space_map(sc->sc_st, aa->oba_addr,
269 	    512, 0, &sc->sc_ioh) != 0) {
270 		aprint_error_dev(self, "unable to map device\n");
271 		return;
272 	}
273 
274 	/* Hook up the interrupt handler. */
275 	sc->sc_ih = adm5120_intr_establish(aa->oba_irq, INTR_IRQ, ahci_intr, sc);
276 
277 	if (sc->sc_ih == NULL) {
278 		aprint_error_dev(self,
279 		    "unable to register interrupt handler\n");
280 		return;
281 	}
282 
283 	SIMPLEQ_INIT(&sc->sc_free_xfers);
284 
285 	callout_init(&sc->sc_poll_handle, 0);
286 	callout_setfunc(&sc->sc_poll_handle, ahci_poll_hub, sc);
287 
288 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
289 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED /* XXXNH */);
290 
291 	REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* disable interrupts */
292 	REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_SW_RESET); /* reset */
293 	delay_ms(10);
294 	while (REG_READ(ADMHCD_REG_CONTROL) & ADMHCD_SW_RESET)
295 		delay_ms(1);
296 
297 	REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_HOST_EN);
298 	REG_WRITE(ADMHCD_REG_HOSTHEAD, 0x00000000);
299 	REG_WRITE(ADMHCD_REG_FMINTERVAL, 0x20002edf);
300 	REG_WRITE(ADMHCD_REG_LSTHRESH, 0x628);
301 	REG_WRITE(ADMHCD_REG_RHDESCR, ADMHCD_NPS | ADMHCD_LPSC);
302 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
303 
304 	REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* XXX: enable interrupts */
305 
306 #ifdef USB_DEBUG
307 	/* usbdebug = 0x7f;
308 	uhubdebug = 0x7f;
309 	umassdebug = 0xffffffff; */
310 #endif
311 
312 	/* Attach USB devices */
313 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
314 
315 }
316 
317 int
ahci_intr(void * arg)318 ahci_intr(void *arg)
319 {
320 #if 0
321 	struct ahci_softc *sc = arg;
322 	uint8_t r;
323 #ifdef AHCI_DEBUG
324 	char bitbuf[256];
325 #endif
326 
327 	r = sl11read(sc, SL11_ISR);
328 
329 	sl11write(sc, SL11_ISR, SL11_ISR_DATA | SL11_ISR_SOFTIMER);
330 
331 	if ((r & SL11_ISR_RESET)) {
332 		sc->sc_flags |= AHCDF_RESET;
333 		sl11write(sc, SL11_ISR, SL11_ISR_RESET);
334 	}
335 	if ((r & SL11_ISR_INSERT)) {
336 		sc->sc_flags |= AHCDF_INSERT;
337 		sl11write(sc, SL11_ISR, SL11_ISR_INSERT);
338 	}
339 
340 #ifdef AHCI_DEBUG
341 	snprintb(bitbuf, sizeof(bitbuf),
342 	    ((sl11read(sc, SL11_CTRL) & SL11_CTRL_SUSPEND)
343 	    ? "\20\x8""D+\7RESUME\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"
344 	    : "\20\x8""D+\7RESET\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"),
345 	    r);
346 
347 	DPRINTF(D_XFER, ("I=%s ", bitbuf));
348 #endif /* AHCI_DEBUG */
349 #endif
350 
351 	return 0;
352 }
353 
354 usbd_status
ahci_open(struct usbd_pipe * pipe)355 ahci_open(struct usbd_pipe *pipe)
356 {
357 	struct usbd_device *dev = pipe->up_dev;
358 	struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
359 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
360 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
361 
362 	DPRINTF(D_TRACE, ("ahci_open(addr=%d,ep=%d,scaddr=%d)",
363 		dev->ud_addr, ed->bEndpointAddress, rhaddr));
364 
365 	apipe->toggle=0;
366 
367 	if (dev->ud_addr == rhaddr) {
368 		switch (ed->bEndpointAddress) {
369 		case USB_CONTROL_ENDPOINT:
370 			pipe->up_methods = &roothub_ctrl_methods;
371 			break;
372 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
373 			pipe->up_methods = &ahci_root_intr_methods;
374 			break;
375 		default:
376 			printf("open:endpointErr!\n");
377 			return USBD_INVAL;
378 		}
379 	} else {
380 		switch (ed->bmAttributes & UE_XFERTYPE) {
381 		case UE_CONTROL:
382 			DPRINTF(D_MSG, ("control "));
383 			pipe->up_methods = &ahci_device_ctrl_methods;
384 			break;
385 		case UE_INTERRUPT:
386 			DPRINTF(D_MSG, ("interrupt "));
387 			pipe->up_methods = &ahci_device_intr_methods;
388 			break;
389 		case UE_ISOCHRONOUS:
390 			DPRINTF(D_MSG, ("isochronous "));
391 			pipe->up_methods = &ahci_device_isoc_methods;
392 			break;
393 		case UE_BULK:
394 			DPRINTF(D_MSG, ("bluk "));
395 			pipe->up_methods = &ahci_device_bulk_methods;
396 			break;
397 		}
398 	}
399 	return USBD_NORMAL_COMPLETION;
400 }
401 
402 void
ahci_softintr(void * arg)403 ahci_softintr(void *arg)
404 {
405 	DPRINTF(D_TRACE, ("%s()", __func__));
406 }
407 
408 void
ahci_poll(struct usbd_bus * bus)409 ahci_poll(struct usbd_bus *bus)
410 {
411 	DPRINTF(D_TRACE, ("%s()", __func__));
412 }
413 
414 #define AHCI_BUS2SC(bus)	((bus)->ub_hcpriv)
415 #define AHCI_PIPE2SC(pipe)	AHCI_BUS2SC((pipe)->up_dev->ud_bus)
416 #define AHCI_XFER2SC(xfer)	AHCI_BUS2SC((xfer)->ux_bus)
417 #define AHCI_APIPE2SC(ap)	AHCI_BUS2SC((d)->pipe.up_dev->ud_bus)
418 
419 /*
420  * Emulation of interrupt transfer for status change endpoint
421  * of root hub.
422  */
423 void
ahci_poll_hub(void * arg)424 ahci_poll_hub(void *arg)
425 {
426 	struct ahci_softc *sc = arg;
427 	struct usbd_xfer *xfer;
428 	u_char *p;
429 	static int p0_state=0;
430 	static int p1_state=0;
431 
432 	mutex_enter(&sc->sc_lock);
433 
434 	/*
435 	 * If the intr xfer has completed or been synchronously
436 	 * aborted, we have nothing to do.
437 	 */
438 	xfer = sc->sc_intr_xfer;
439 	if (xfer == NULL)
440 		goto out;
441 	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
442 
443 	/*
444 	 * If the intr xfer for which we were scheduled is done, and
445 	 * another intr xfer has been submitted, let that one be dealt
446 	 * with when the callout fires again.
447 	 *
448 	 * The call to callout_pending is racy, but the transition
449 	 * from pending to invoking happens atomically.  The
450 	 * callout_ack ensures callout_invoking does not return true
451 	 * due to this invocation of the callout; the lock ensures the
452 	 * next invocation of the callout cannot callout_ack (unless it
453 	 * had already run to completion and nulled sc->sc_intr_xfer,
454 	 * in which case would have bailed out already).
455 	 */
456 	callout_ack(&sc->sc_poll_handle);
457 	if (callout_pending(&sc->sc_poll_handle) ||
458 	    callout_invoking(&sc->sc_poll_handle))
459 		goto out;
460 
461 	/* USB spec 11.13.3 (p.260) */
462 	p = KERNADDR(&xfer->ux_dmabuf, 0);
463 	p[0] = 0;
464 	if ((REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS) != p0_state) {
465 		p[0] = 2;
466 		DPRINTF(D_TRACE, ("!"));
467 		p0_state=(REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS);
468 	};
469 	if ((REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS) != p1_state) {
470 		p[0] = 2;
471 		DPRINTF(D_TRACE, ("@"));
472 		p1_state=(REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS);
473 	};
474 
475 	/* no change, return NAK and try again later */
476 	if (p[0] == 0) {
477 		callout_schedule(&sc->sc_poll_handle, sc->sc_interval);
478 		goto out;
479 	}
480 
481 	/*
482 	 * Interrupt completed, and the xfer has not been completed or
483 	 * synchronously aborted.  Complete the xfer now.
484 	 *
485 	 * XXX Set ux_isdone if DIAGNOSTIC?
486 	 */
487 	xfer->ux_actlen = 1;
488 	xfer->ux_status = USBD_NORMAL_COMPLETION;
489 	usb_transfer_complete(xfer);
490 
491 out:	mutex_exit(&sc->sc_lock);
492 }
493 
494 struct usbd_xfer *
ahci_allocx(struct usbd_bus * bus,unsigned int nframes)495 ahci_allocx(struct usbd_bus *bus, unsigned int nframes)
496 {
497 	struct ahci_softc *sc = AHCI_BUS2SC(bus);
498 	struct usbd_xfer *xfer;
499 
500 	DPRINTF(D_MEM, ("SLallocx"));
501 
502 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
503 	if (xfer) {
504 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, ux_next);
505 #ifdef DIAGNOSTIC
506 		if (xfer->ux_state != XFER_FREE) {
507 			printf("ahci_allocx: xfer=%p not free, 0x%08x\n",
508 				xfer, xfer->ux_state);
509 		}
510 #endif
511 	} else {
512 		xfer = kmem_alloc(sizeof(*xfer), KM_SLEEP);
513 	}
514 
515 	memset(xfer, 0, sizeof(*xfer));
516 #ifdef DIAGNOSTIC
517 	xfer->ux_state = XFER_BUSY;
518 #endif
519 
520 	return xfer;
521 }
522 
523 void
ahci_freex(struct usbd_bus * bus,struct usbd_xfer * xfer)524 ahci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
525 {
526 	struct ahci_softc *sc = AHCI_BUS2SC(bus);
527 
528 	DPRINTF(D_MEM, ("SLfreex"));
529 
530 #ifdef DIAGNOSTIC
531 	if (xfer->ux_state != XFER_BUSY &&
532 	    xfer->ux_status != USBD_NOT_STARTED) {
533 		printf("ahci_freex: xfer=%p not busy, 0x%08x\n",
534 			xfer, xfer->ux_state);
535 		return;
536 	}
537 	xfer->ux_state = XFER_FREE;
538 #endif
539 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, ux_next);
540 }
541 
542 static void
ahci_get_lock(struct usbd_bus * bus,kmutex_t ** lock)543 ahci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
544 {
545 	struct ahci_softc *sc = AHCI_BUS2SC(bus);
546 
547 	*lock = &sc->sc_lock;
548 }
549 
550 void
ahci_noop(struct usbd_pipe * pipe)551 ahci_noop(struct usbd_pipe *pipe)
552 {
553 	DPRINTF(D_TRACE, ("%s()", __func__));
554 }
555 
556 /*
557  * Data structures and routines to emulate the root hub.
558  */
559 
560 static int
ahci_roothub_ctrl(struct usbd_bus * bus,usb_device_request_t * req,void * buf,int buflen)561 ahci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
562     void *buf, int buflen)
563 {
564 	struct ahci_softc *sc = AHCI_BUS2SC(bus);
565 	uint16_t len, value, index;
566 	usb_port_status_t ps;
567 	int totlen = 0;
568 	int status;
569 
570 	DPRINTF(D_TRACE, ("SLRCstart "));
571 
572 	len = UGETW(req->wLength);
573 	value = UGETW(req->wValue);
574 	index = UGETW(req->wIndex);
575 
576 #define C(x,y) ((x) | ((y) << 8))
577 	switch (C(req->bRequest, req->bmRequestType)) {
578 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
579 		switch (value) {
580 #define sd ((usb_string_descriptor_t *)buf)
581 		case C(2, UDESC_STRING):
582 			/* Product */
583 			totlen = usb_makestrdesc(sd, len, "ADM5120 root hub");
584 			break;
585 		default:
586 			printf("unknownGetDescriptor=%x", value);
587 			/* FALLTHROUGH */
588 		case C(0, UDESC_DEVICE):
589 		case C(1, UDESC_STRING):
590 			/* default from usbroothub */
591 			return buflen;
592 		}
593 		break;
594 	/*
595 	 * Hub specific requests
596 	 */
597 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
598 		/* Clear Hub Feature, 11.16.2.1, not supported */
599 		DPRINTF(D_MSG, ("ClearHubFeature not supported\n"));
600 		break;
601 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
602 
603 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
604 		/* Clear Port Feature, 11.16.2.2 */
605 		if (index != 1 && index != 2 ) {
606 			return -1;
607 		}
608 		switch (value) {
609 		case UHF_PORT_POWER:
610 			DPRINTF(D_MSG, ("POWER_OFF "));
611 			WPS(ADMHCD_LSDA);
612 			break;
613 		case UHF_PORT_SUSPEND:
614 			DPRINTF(D_MSG, ("SUSPEND "));
615 			WPS(ADMHCD_POCI);
616 			break;
617 		case UHF_PORT_ENABLE:
618 			DPRINTF(D_MSG, ("ENABLE "));
619 			WPS(ADMHCD_CCS);
620 			break;
621 		case UHF_C_PORT_CONNECTION:
622 			WPS(ADMHCD_CSC);
623 			break;
624 		case UHF_C_PORT_RESET:
625 			WPS(ADMHCD_PRSC);
626 			break;
627 		case UHF_C_PORT_SUSPEND:
628 			WPS(ADMHCD_PSSC);
629 			break;
630 		case UHF_C_PORT_ENABLE:
631 			WPS(ADMHCD_PESC);
632 			break;
633 		case UHF_C_PORT_OVER_CURRENT:
634 			WPS(ADMHCD_OCIC);
635 			break;
636 		default:
637 			printf("ClrPortFeatERR:value=0x%x ", value);
638 			return -1;
639 		}
640 		//DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change));
641 #undef WPS
642 		break;
643 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
644 		/* Get Bus State, 11.16.2.3, not supported */
645 		/* shall return a STALL... */
646 		break;
647 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
648 		/* Get Hub Descriptor, 11.16.2.4 */
649 		DPRINTF(D_MSG, ("UR_GET_DESCRIPTOR RCD"));
650 		if ((value&0xff) != 0) {
651 			return -1;
652 		}
653 		usb_hub_descriptor_t hubd;
654 
655 		totlen = uimin(buflen, sizeof(hubd));
656 		memcpy(&hubd, buf, totlen);
657 		hubd.bNbrPorts = 2;
658 		USETW(hubd.wHubCharacteristics, 0);
659 		hubd.bPwrOn2PwrGood = 0;
660 		memcpy(buf, &hubd, totlen);
661 		break;
662 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
663 		/* Get Hub Status, 11.16.2.5 */
664 		DPRINTF(D_MSG, ("UR_GET_STATUS RCD"));
665 		if (len != 4) {
666 			return -1;
667 		}
668 		memset(buf, 0, len);
669 		totlen = len;
670 		break;
671 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
672 		/* Get Port Status, 11.16.2.6 */
673 		if ((index != 1 && index != 2)  || len != 4) {
674 			printf("index=%d,len=%d ", index, len);
675 			return -1;
676 		}
677 		status = REG_READ(ADMHCD_REG_PORTSTATUS0+(index-1)*4);
678 		DPRINTF(D_MSG, ("UR_GET_STATUS RCO=%x ", status));
679 
680 		//DPRINTF(D_XFER, ("ST=%04x,CH=%04x ", status, sc->sc_change));
681 		USETW(ps.wPortStatus, status  & (UPS_CURRENT_CONNECT_STATUS|UPS_PORT_ENABLED|UPS_SUSPEND|UPS_OVERCURRENT_INDICATOR|UPS_RESET|UPS_PORT_POWER|UPS_LOW_SPEED));
682 		USETW(ps.wPortChange, (status>>16) & (UPS_C_CONNECT_STATUS|UPS_C_PORT_ENABLED|UPS_C_SUSPEND|UPS_C_OVERCURRENT_INDICATOR|UPS_C_PORT_RESET));
683 		totlen = uimin(len, sizeof(ps));
684 		memcpy(buf, &ps, totlen);
685 		break;
686 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
687 		/* Set Hub Descriptor, 11.16.2.7, not supported */
688 		/* STALL ? */
689 		return -1;
690 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
691 		/* Set Hub Feature, 11.16.2.8, not supported */
692 		break;
693 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
694 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
695 		/* Set Port Feature, 11.16.2.9 */
696 		if ((index != 1) && (index !=2)) {
697 			printf("index=%d ", index);
698 			return -1;
699 		}
700 		switch (value) {
701 		case UHF_PORT_RESET:
702 			DPRINTF(D_MSG, ("PORT_RESET "));
703 			WPS(ADMHCD_PRS);
704 			break;
705 		case UHF_PORT_POWER:
706 			DPRINTF(D_MSG, ("PORT_POWER "));
707 			WPS(ADMHCD_PPS);
708 			break;
709 		case UHF_PORT_ENABLE:
710 			DPRINTF(D_MSG, ("PORT_ENABLE "));
711 			WPS(ADMHCD_PES);
712 			break;
713 		default:
714 			printf("SetPortFeatERR=0x%x ", value);
715 			return -1;
716 		}
717 #undef WPS
718 		break;
719 	default:
720 		DPRINTF(D_MSG, ("ioerr(UR=%02x,UT=%02x) ",
721 			req->bRequest, req->bmRequestType));
722 		/* default from usbroothub */
723 		return buflen;
724 	}
725 
726 	return totlen;
727 }
728 
729 static usbd_status
ahci_root_intr_transfer(struct usbd_xfer * xfer)730 ahci_root_intr_transfer(struct usbd_xfer *xfer)
731 {
732 
733 	DPRINTF(D_TRACE, ("SLRItransfer "));
734 
735 	/* Pipe isn't running, start first.  */
736 	return ahci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
737 }
738 
739 static usbd_status
ahci_root_intr_start(struct usbd_xfer * xfer)740 ahci_root_intr_start(struct usbd_xfer *xfer)
741 {
742 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
743 
744 	DPRINTF(D_TRACE, ("SLRIstart "));
745 
746 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
747 
748 	KASSERT(sc->sc_intr_xfer == NULL);
749 	sc->sc_interval = MS_TO_TICKS(xfer->ux_pipe->up_endpoint->ue_edesc->bInterval);
750 	callout_schedule(&sc->sc_poll_handle, sc->sc_interval);
751 	sc->sc_intr_xfer = xfer;
752 	xfer->ux_status = USBD_IN_PROGRESS;
753 
754 	return USBD_IN_PROGRESS;
755 }
756 
757 static void
ahci_root_intr_abort(struct usbd_xfer * xfer)758 ahci_root_intr_abort(struct usbd_xfer *xfer)
759 {
760 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
761 
762 	DPRINTF(D_TRACE, ("SLRIabort "));
763 
764 	KASSERT(mutex_owned(&sc->sc_lock));
765 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
766 
767 	/*
768 	 * Try to stop the callout before it starts.  If we got in too
769 	 * late, too bad; but if the callout had yet to run and time
770 	 * out the xfer, cancel it ourselves.
771 	 */
772 	callout_stop(&sc->sc_poll_handle);
773 	if (sc->sc_intr_xfer == NULL)
774 		return;
775 
776 	KASSERT(sc->sc_intr_xfer == xfer);
777 	xfer->ux_status = USBD_CANCELLED;
778 	usb_transfer_complete(xfer);
779 }
780 
781 static void
ahci_root_intr_close(struct usbd_pipe * pipe)782 ahci_root_intr_close(struct usbd_pipe *pipe)
783 {
784 	struct ahci_softc *sc __diagused = AHCI_PIPE2SC(pipe);
785 
786 	DPRINTF(D_TRACE, ("SLRIclose "));
787 
788 	KASSERT(mutex_owned(&sc->sc_lock));
789 
790 	/*
791 	 * The caller must arrange to have aborted the pipe already, so
792 	 * there can be no intr xfer in progress.  The callout may
793 	 * still be pending from a prior intr xfer -- if it has already
794 	 * fired, it will see there is nothing to do, and do nothing.
795 	 */
796 	KASSERT(sc->sc_intr_xfer == NULL);
797 	KASSERT(!callout_pending(&sc->sc_poll_handle));
798 }
799 
800 static void
ahci_root_intr_done(struct usbd_xfer * xfer)801 ahci_root_intr_done(struct usbd_xfer *xfer)
802 {
803 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
804 
805 	//DPRINTF(D_XFER, ("RIdn "));
806 
807 	KASSERT(mutex_owned(&sc->sc_lock));
808 
809 	/* Claim the xfer so it doesn't get completed again.  */
810 	KASSERT(sc->sc_intr_xfer == xfer);
811 	KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
812 	sc->sc_intr_xfer = NULL;
813 }
814 
815 static usbd_status
ahci_device_ctrl_transfer(struct usbd_xfer * xfer)816 ahci_device_ctrl_transfer(struct usbd_xfer *xfer)
817 {
818 
819 	DPRINTF(D_TRACE, ("C"));
820 
821 	return ahci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
822 }
823 
824 static usbd_status
ahci_device_ctrl_start(struct usbd_xfer * xfer)825 ahci_device_ctrl_start(struct usbd_xfer *xfer)
826 {
827 	usbd_status status =  USBD_NORMAL_COMPLETION;
828 	int s, err;
829 	static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
830 	static struct admhcd_td td_v[4] __attribute__((aligned(16))), *td, *td1, *td2, *td3;
831 	static usb_dma_t reqdma;
832 	struct usbd_pipe *pipe = xfer->ux_pipe;
833 	usb_device_request_t *req = &xfer->ux_request;
834 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
835 	int len, isread;
836 
837 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
838 
839 #if 0
840 	struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
841 #endif
842 /*	printf("ctrl_start>>>\n"); */
843 
844 #ifdef DIAGNOSTIC
845 	if (!(xfer->ux_rqflags & URQ_REQUEST)) {
846 		/* XXX panic */
847 		printf("ahci_device_ctrl_transfer: not a request\n");
848 		return USBD_INVAL;
849 	}
850 #endif
851 
852 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
853 	DPRINTF(D_TRACE, ("st "));
854 	if (!ep) {
855 		ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
856 		td = (struct admhcd_td *)KSEG1ADDR(&td_v[0]);
857 		td1 = (struct admhcd_td *)KSEG1ADDR(&td_v[1]);
858 		td2 = (struct admhcd_td *)KSEG1ADDR(&td_v[2]);
859 		td3 = (struct admhcd_td *)KSEG1ADDR(&td_v[3]);
860 		err = usb_allocmem(sc->sc_bus.ub_dmatag,
861 			sizeof(usb_device_request_t),
862 			0, USBMALLOC_COHERENT, &reqdma);
863 		if (err)
864 			return USBD_NOMEM;
865 
866 		/* printf("ep: %p\n",ep); */
867 	};
868 
869 	ep->control =  pipe->up_dev->ud_addr | \
870 		((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
871 		((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
872 	memcpy(KERNADDR(&reqdma, 0), req, sizeof(*req));
873 /* 	printf("status: %x\n",REG_READ(ADMHCD_REG_PORTSTATUS0));
874 	printf("ep_control: %x\n",ep->control);
875 	printf("speed: %x\n",pipe->up_dev->ud_speed);
876 	printf("req: %p\n",req);
877 	printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
878 
879 	isread = req->bmRequestType & UT_READ;
880 	len = UGETW(req->wLength);
881 
882 	ep->next = ep;
883 
884 	td->buffer = DMAADDR(&reqdma,0) | 0xa0000000;
885 	td->buflen=sizeof(*req);
886 	td->control=ADMHCD_TD_SETUP | ADMHCD_TD_DATA0 | ADMHCD_TD_OWN;
887 
888 	if (len) {
889 		td->next = td1;
890 
891 		td1->buffer = DMAADDR(&xfer->ux_dmabuf,0) | 0xa0000000;
892 		td1->buflen = len;
893 		td1->next = td2;
894 		td1->control= (isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | ADMHCD_TD_DATA1 | ADMHCD_TD_R | ADMHCD_TD_OWN;
895 	} else {
896 		td1->control = 0;
897 		td->next = td2;
898 	};
899 
900 	td2->buffer = 0;
901 	td2->buflen= 0;
902 	td2->next = td3;
903 	td2->control = (isread?ADMHCD_TD_OUT:ADMHCD_TD_IN) | ADMHCD_TD_DATA1 | ADMHCD_TD_OWN;
904 
905 	td3->buffer = 0;
906 	td3->buflen= 0;
907 	td3->next = 0;
908 	td3->control = 0;
909 
910 	ep->head = td;
911 	ep->tail = td3;
912 /*
913 	printf("ep: %p\n",ep);
914 	printf("ep->next: %p\n",ep->next);
915 	printf("ep->head: %p\n",ep->head);
916 	printf("ep->tail: %p\n",ep->tail);
917 	printf("td: %p\n",td);
918 	printf("td->next: %p\n",td->next);
919 	printf("td->buffer: %x\n",td->buffer);
920 	printf("td->buflen: %x\n",td->buflen);
921 	printf("td1: %p\n",td1);
922 	printf("td1->next: %p\n",td1->next);
923 	printf("td2: %p\n",td2);
924 	printf("td2->next: %p\n",td2->next);
925 	printf("td3: %p\n",td3);
926 	printf("td3->next: %p\n",td3->next);
927 */
928 
929 	REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
930 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
931 /*	printf("1: %x %x %x %x\n", ep->control, td->control, td1->control, td2->control); */
932 	s=100;
933 	while (s--) {
934 		delay_ms(10);
935 /*                printf("%x %x %x %x\n", ep->control, td->control, td1->control, td2->control);*/
936 		status = USBD_TIMEOUT;
937 		if (td->control & ADMHCD_TD_OWN) continue;
938 
939 		err = (td->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
940 		if (err) {
941 			status = USBD_IOERROR;
942 			break;
943 		};
944 
945 		status = USBD_TIMEOUT;
946 		if (td1->control & ADMHCD_TD_OWN) continue;
947 		err = (td1->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
948 		if (err) {
949 			status = USBD_IOERROR;
950 			break;
951 		};
952 
953 		status = USBD_TIMEOUT;
954 		if (td2->control & ADMHCD_TD_OWN) continue;
955 		err = (td2->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
956 		if (err) {
957 			status = USBD_IOERROR;
958 		};
959 		status = USBD_NORMAL_COMPLETION;
960 		break;
961 
962 	};
963 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
964 
965 	xfer->ux_actlen = len;
966 	xfer->ux_status = status;
967 
968 /* 	printf("ctrl_start<<<\n"); */
969 
970 	usb_transfer_complete(xfer);
971 
972 	usb_freemem(&reqdma);
973 
974 	return USBD_NORMAL_COMPLETION;
975 }
976 
977 static void
ahci_device_ctrl_abort(struct usbd_xfer * xfer)978 ahci_device_ctrl_abort(struct usbd_xfer *xfer)
979 {
980 	DPRINTF(D_TRACE, ("Cab "));
981 	usbd_xfer_abort(xfer);
982 }
983 
984 static void
ahci_device_ctrl_close(struct usbd_pipe * pipe)985 ahci_device_ctrl_close(struct usbd_pipe *pipe)
986 {
987 	DPRINTF(D_TRACE, ("Ccl "));
988 }
989 
990 static void
ahci_device_ctrl_done(struct usbd_xfer * xfer)991 ahci_device_ctrl_done(struct usbd_xfer *xfer)
992 {
993 	DPRINTF(D_TRACE, ("Cdn "));
994 }
995 
996 static usbd_status
ahci_device_intr_transfer(struct usbd_xfer * xfer)997 ahci_device_intr_transfer(struct usbd_xfer *xfer)
998 {
999 
1000 	DPRINTF(D_TRACE, ("INTRtrans "));
1001 
1002 	return ahci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1003 }
1004 
1005 static usbd_status
ahci_device_intr_start(struct usbd_xfer * xfer)1006 ahci_device_intr_start(struct usbd_xfer *xfer)
1007 {
1008 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
1009 	struct usbd_pipe *pipe = xfer->ux_pipe;
1010 	struct ahci_xfer *sx;
1011 
1012 	DPRINTF(D_TRACE, ("INTRstart "));
1013 
1014 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1015 
1016 	sx = kmem_intr_alloc(sizeof(*sx), KM_NOSLEEP);
1017 	if (sx == NULL)
1018 		goto reterr;
1019 	memset(sx, 0, sizeof(*sx));
1020 	sx->sx_xfer = xfer;
1021 	xfer->ux_hcpriv = sx;
1022 
1023 	/* initialize callout */
1024 	callout_init(&sx->sx_callout_t, 0);
1025 	callout_reset(&sx->sx_callout_t,
1026 		MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1027 		ahci_poll_device, sx);
1028 
1029 	/* ACK */
1030 	return USBD_IN_PROGRESS;
1031 
1032  reterr:
1033 	return USBD_IOERROR;
1034 }
1035 
1036 static void
ahci_poll_device(void * arg)1037 ahci_poll_device(void *arg)
1038 {
1039 	struct ahci_xfer *sx = (struct ahci_xfer *)arg;
1040 	struct usbd_xfer *xfer = sx->sx_xfer;
1041 	struct usbd_pipe *pipe = xfer->ux_pipe;
1042 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
1043 	void *buf;
1044 	int pid;
1045 	int r;
1046 
1047 	DPRINTF(D_TRACE, ("pldev"));
1048 
1049 	callout_reset(&sx->sx_callout_t,
1050 		MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1051 		ahci_poll_device, sx);
1052 
1053 	/* interrupt transfer */
1054 	pid = (UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN)
1055 	    ? ADMHCD_TD_IN : ADMHCD_TD_OUT;
1056 	buf = KERNADDR(&xfer->ux_dmabuf, 0);
1057 
1058 	r = ahci_transaction(sc, pipe, pid, xfer->ux_length, buf, 0/*toggle*/);
1059 	if (r < 0) {
1060 		DPRINTF(D_MSG, ("%s error", __func__));
1061 		return;
1062 	}
1063 	/* no change, return NAK */
1064 	if (r == 0)
1065 		return;
1066 
1067 	xfer->ux_status = USBD_NORMAL_COMPLETION;
1068 	mutex_enter(&sc->sc_lock);
1069 	usb_transfer_complete(xfer);
1070 	mutex_exit(&sc->sc_lock);
1071 }
1072 
1073 static void
ahci_device_intr_abort(struct usbd_xfer * xfer)1074 ahci_device_intr_abort(struct usbd_xfer *xfer)
1075 {
1076 	struct ahci_xfer *sx;
1077 
1078 	DPRINTF(D_TRACE, ("INTRabort "));
1079 
1080 	sx = xfer->ux_hcpriv;
1081 	if (sx) {
1082 		callout_stop(&sx->sx_callout_t);
1083 		kmem_intr_free(sx, sizeof(*sx));
1084 		xfer->ux_hcpriv = NULL;
1085 	} else {
1086 		printf("%s: sx == NULL!\n", __func__);
1087 	}
1088 	usbd_xfer_abort(xfer);
1089 }
1090 
1091 static void
ahci_device_intr_close(struct usbd_pipe * pipe)1092 ahci_device_intr_close(struct usbd_pipe *pipe)
1093 {
1094 	DPRINTF(D_TRACE, ("INTRclose "));
1095 }
1096 
1097 static void
ahci_device_intr_done(struct usbd_xfer * xfer)1098 ahci_device_intr_done(struct usbd_xfer *xfer)
1099 {
1100 	DPRINTF(D_TRACE, ("INTRdone "));
1101 }
1102 
1103 static usbd_status
ahci_device_isoc_transfer(struct usbd_xfer * xfer)1104 ahci_device_isoc_transfer(struct usbd_xfer *xfer)
1105 {
1106 	DPRINTF(D_TRACE, ("S"));
1107 	return USBD_NORMAL_COMPLETION;
1108 }
1109 
1110 static usbd_status
ahci_device_isoc_start(struct usbd_xfer * xfer)1111 ahci_device_isoc_start(struct usbd_xfer *xfer)
1112 {
1113 	DPRINTF(D_TRACE, ("st "));
1114 	return USBD_NORMAL_COMPLETION;
1115 }
1116 
1117 static void
ahci_device_isoc_abort(struct usbd_xfer * xfer)1118 ahci_device_isoc_abort(struct usbd_xfer *xfer)
1119 {
1120 	DPRINTF(D_TRACE, ("Sab "));
1121 }
1122 
1123 static void
ahci_device_isoc_close(struct usbd_pipe * pipe)1124 ahci_device_isoc_close(struct usbd_pipe *pipe)
1125 {
1126 	DPRINTF(D_TRACE, ("Scl "));
1127 }
1128 
1129 static void
ahci_device_isoc_done(struct usbd_xfer * xfer)1130 ahci_device_isoc_done(struct usbd_xfer *xfer)
1131 {
1132 	DPRINTF(D_TRACE, ("Sdn "));
1133 }
1134 
1135 static usbd_status
ahci_device_bulk_transfer(struct usbd_xfer * xfer)1136 ahci_device_bulk_transfer(struct usbd_xfer *xfer)
1137 {
1138 
1139 	DPRINTF(D_TRACE, ("B"));
1140 
1141 	return ahci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1142 }
1143 
1144 static usbd_status
ahci_device_bulk_start(struct usbd_xfer * xfer)1145 ahci_device_bulk_start(struct usbd_xfer *xfer)
1146 {
1147 #define NBULK_TDS 32
1148 	static volatile int level = 0;
1149 	usbd_status status =  USBD_NORMAL_COMPLETION;
1150 	int s, err;
1151 	static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
1152 	static struct admhcd_td td_v[NBULK_TDS] __attribute__((aligned(16))), *td[NBULK_TDS];
1153 	struct usbd_pipe *pipe = xfer->ux_pipe;
1154 	struct ahci_softc *sc = AHCI_XFER2SC(xfer);
1155 	int endpt, i, len, tlen, segs, offset, isread, toggle, short_ok;
1156 	struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
1157 
1158 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
1159 	DPRINTF(D_TRACE, ("st "));
1160 
1161 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1162 
1163 #ifdef DIAGNOSTIC
1164 	if (xfer->ux_rqflags & URQ_REQUEST) {
1165 		/* XXX panic */
1166 		printf("ohci_device_bulk_start: a request\n");
1167 		return USBD_INVAL;
1168 	}
1169 #endif
1170 
1171 	level++;
1172 /* 	printf("bulk_start>>>\n"); */
1173 
1174 	if (!ep) {
1175 		ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
1176 		for (i=0; i<NBULK_TDS; i++) {
1177 			td[i] = (struct admhcd_td *)KSEG1ADDR(&td_v[i]);
1178 		};
1179 /*		printf("ep: %p\n",ep);*/
1180 	};
1181 	if (apipe->toggle == 0) {
1182 		toggle = ADMHCD_TD_DATA0;
1183 	} else {
1184 		toggle = apipe->toggle;
1185 	};
1186 
1187 	endpt = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1188 	ep->control = pipe->up_dev->ud_addr | ((endpt & 0xf) << ADMHCD_ED_EPSHIFT)|\
1189 		((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
1190 		((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
1191 
1192 	short_ok = xfer->ux_flags & USBD_SHORT_XFER_OK?ADMHCD_TD_R:0;
1193 /*	printf("level: %d\n",level);
1194 	printf("short_xfer: %x\n",short_ok);
1195 	printf("ep_control: %x\n",ep->control);
1196 	printf("speed: %x\n",pipe->up_dev->ud_speed);
1197 	printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
1198 
1199 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1200 	len = xfer->ux_length;
1201 
1202 	ep->next = ep;
1203 
1204 	i = 0;
1205 	offset = 0;
1206 	while (len > 0 || i == 0) {
1207 		tlen = uimin(len,4096);
1208 		td[i]->buffer = DMAADDR(&xfer->ux_dmabuf, offset) | 0xa0000000;
1209 		td[i]->buflen = tlen;
1210 		td[i]->control = (isread ? ADMHCD_TD_IN : ADMHCD_TD_OUT) |
1211 		    toggle | ADMHCD_TD_OWN | short_ok;
1212 		td[i]->len = tlen;
1213 		toggle = ADMHCD_TD_TOGGLE;
1214 		len -= tlen;
1215 		offset += tlen;
1216 		td[i]->next = td[i + 1];
1217 		i++;
1218 	};
1219 
1220 	td[i]->buffer = 0;
1221 	td[i]->buflen = 0;
1222 	td[i]->control = 0;
1223 	td[i]->next = 0;
1224 
1225 	ep->head = td[0];
1226 	ep->tail = td[i];
1227 	segs = i;
1228 	len = 0;
1229 
1230 	if (xfer->ux_length)
1231 		usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
1232 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1233 
1234 /*	printf("segs: %d\n",segs);
1235 	printf("ep: %p\n",ep);
1236 	printf("ep->control: %x\n",ep->control);
1237 	printf("ep->next: %p\n",ep->next);
1238 	printf("ep->head: %p\n",ep->head);
1239 	printf("ep->tail: %p\n",ep->tail);
1240 	for (i=0; i<segs; i++) {
1241 		printf("td[%d]: %p\n",i,td[i]);
1242 		printf("td[%d]->control: %x\n",i,td[i]->control);
1243 		printf("td[%d]->next: %p\n",i,td[i]->next);
1244 		printf("td[%d]->buffer: %x\n",i,td[i]->buffer);
1245 		printf("td[%d]->buflen: %x\n",i,td[i]->buflen);
1246 	}; */
1247 
1248 	REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
1249 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
1250 	i = 0;
1251 /*	printf("1: %x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1252 	s=100;
1253 	err = 0;
1254 	while (s--) {
1255 /*                printf("%x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1256 		status = USBD_TIMEOUT;
1257 		if (td[i]->control & ADMHCD_TD_OWN) {
1258 			delay_ms(3);
1259 			continue;
1260 		};
1261 
1262 		len += td[i]->len - td[i]->buflen;
1263 
1264 		err = (td[i]->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1265 		if (err) {
1266 			status = USBD_IOERROR;
1267 			break;
1268 		};
1269 
1270 		i++;
1271 		if (i==segs) {
1272 			status = USBD_NORMAL_COMPLETION;
1273 			break;
1274 		};
1275 
1276 	};
1277 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
1278 
1279 	apipe->toggle = ((uint32_t)ep->head & 2)?ADMHCD_TD_DATA1:ADMHCD_TD_DATA0;
1280 /*	printf("bulk_transfer_done: status: %x, err: %x, len: %x, toggle: %x\n", status,err,len,apipe->toggle); */
1281 
1282 	if (short_ok && (err == 0x9 || err == 0xd)) {
1283 /*		printf("bulk_transfer_done: short_transfer fix\n"); */
1284 		status = USBD_NORMAL_COMPLETION;
1285 	};
1286 	xfer->ux_actlen = len;
1287 	xfer->ux_status = status;
1288 
1289 	level--;
1290 /*	printf("bulk_start<<<\n"); */
1291 
1292 	if (xfer->ux_length)
1293 		usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
1294 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1295 
1296 	usb_transfer_complete(xfer);
1297 
1298 	return USBD_NORMAL_COMPLETION;
1299 }
1300 
1301 static void
ahci_device_bulk_abort(struct usbd_xfer * xfer)1302 ahci_device_bulk_abort(struct usbd_xfer *xfer)
1303 {
1304 	DPRINTF(D_TRACE, ("Bab "));
1305 	usbd_xfer_abort(xfer);
1306 }
1307 
1308 static void
ahci_device_bulk_close(struct usbd_pipe * pipe)1309 ahci_device_bulk_close(struct usbd_pipe *pipe)
1310 {
1311 	DPRINTF(D_TRACE, ("Bcl "));
1312 }
1313 
1314 static void
ahci_device_bulk_done(struct usbd_xfer * xfer)1315 ahci_device_bulk_done(struct usbd_xfer *xfer)
1316 {
1317 	DPRINTF(D_TRACE, ("Bdn "));
1318 }
1319 
1320 #define DATA0_RD	(0x03)
1321 #define DATA0_WR	(0x07)
1322 #define AHCI_TIMEOUT	(5000)
1323 
1324 /*
1325  * Do a transaction.
1326  * return 1 if ACK, 0 if NAK, -1 if error.
1327  */
1328 static int
ahci_transaction(struct ahci_softc * sc,struct usbd_pipe * pipe,uint8_t pid,int len,u_char * buf,uint8_t toggle)1329 ahci_transaction(struct ahci_softc *sc, struct usbd_pipe *pipe,
1330 	uint8_t pid, int len, u_char *buf, uint8_t toggle)
1331 {
1332 	return -1;
1333 #if 0
1334 #ifdef AHCI_DEBUG
1335 	char str[64];
1336 	int i;
1337 #endif
1338 	int timeout;
1339 	int ls_via_hub = 0;
1340 	int pl;
1341 	uint8_t isr;
1342 	uint8_t result = 0;
1343 	uint8_t devaddr = pipe->up_dev->ud_addr;
1344 	uint8_t endpointaddr = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1345 	uint8_t endpoint;
1346 	uint8_t cmd = DATA0_RD;
1347 
1348 	endpoint = UE_GET_ADDR(endpointaddr);
1349 	DPRINTF(D_XFER, ("\n(%x,%d%s%d,%d) ",
1350 		pid, len, (pid == SL11_PID_IN) ? "<-" : "->", devaddr, endpoint));
1351 
1352 	/* Set registers */
1353 	sl11write(sc, SL11_E0ADDR, 0x40);
1354 	sl11write(sc, SL11_E0LEN,  len);
1355 	sl11write(sc, SL11_E0PID,  (pid << 4) + endpoint);
1356 	sl11write(sc, SL11_E0DEV,  devaddr);
1357 
1358 	/* Set buffer unless PID_IN */
1359 	if (pid != SL11_PID_IN) {
1360 		if (len > 0)
1361 			sl11write_region(sc, 0x40, buf, len);
1362 		cmd = DATA0_WR;
1363 	}
1364 
1365 	/* timing ? */
1366 	pl = (len >> 3) + 3;
1367 
1368 	/* Low speed device via HUB */
1369 	/* XXX does not work... */
1370 	if ((sc->sc_fullspeed) && pipe->up_dev->ud_speed == USB_SPEED_LOW) {
1371 		pl = len + 16;
1372 		cmd |= SL11_EPCTRL_PREAMBLE;
1373 
1374 		/*
1375 		 * SL811HS/T rev 1.2 has a bug, when it got PID_IN
1376 		 * from LowSpeed device via HUB.
1377 		 */
1378 		if (sc->sc_sltype == SLTYPE_SL811HS_R12 && pid == SL11_PID_IN) {
1379 			ls_via_hub = 1;
1380 			DPRINTF(D_MSG, ("LSvH "));
1381 		}
1382 	}
1383 
1384 	/* timing ? */
1385 	if (sl11read(sc, SL811_CSOF) <= (uint8_t)pl)
1386 		cmd |= SL11_EPCTRL_SOF;
1387 
1388 	/* Transfer */
1389 	sl11write(sc, SL11_ISR, 0xff);
1390 	sl11write(sc, SL11_E0CTRL, cmd | toggle);
1391 
1392 	/* Polling */
1393 	for (timeout = AHCI_TIMEOUT; timeout; timeout--) {
1394 		isr = sl11read(sc, SL11_ISR);
1395 		if ((isr & SL11_ISR_USBA))
1396 			break;
1397 	}
1398 
1399 	/* Check result status */
1400 	result = sl11read(sc, SL11_E0STAT);
1401 	if (!(result & SL11_EPSTAT_NAK) && ls_via_hub) {
1402 		/* Resend PID_IN within 20usec */
1403 		sl11write(sc, SL11_ISR, 0xff);
1404 		sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
1405 	}
1406 
1407 	sl11write(sc, SL11_ISR, 0xff);
1408 
1409 	DPRINTF(D_XFER, ("t=%d i=%x ", AHCI_TIMEOUT - timeout, isr));
1410 #if AHCI_DEBUG
1411 	snprintb(str, sizeof(str),
1412 	    "\20\x8STALL\7NAK\6OV\5SETUP\4DATA1\3TIMEOUT\2ERR\1ACK", result);
1413 	DPRINTF(D_XFER, ("STAT=%s ", str));
1414 #endif
1415 
1416 	if ((result & SL11_EPSTAT_ERROR))
1417 		return -1;
1418 
1419 	if ((result & SL11_EPSTAT_NAK))
1420 		return 0;
1421 
1422 	/* Read buffer if PID_IN */
1423 	if (pid == SL11_PID_IN && len > 0) {
1424 		sl11read_region(sc, buf, 0x40, len);
1425 #if AHCI_DEBUG
1426 		for (i = 0; i < len; i++)
1427 			DPRINTF(D_XFER, ("%02X ", buf[i]));
1428 #endif
1429 	}
1430 
1431 	return 1;
1432 #endif
1433 }
1434 
1435 static void
ahci_abortx(struct usbd_xfer * xfer)1436 ahci_abortx(struct usbd_xfer *xfer)
1437 {
1438 	/*
1439 	 * XXX This is totally busted; there's no way it can possibly
1440 	 * work!  All transfers are busy-waited, it seems, so there is
1441 	 * no opportunity to abort.
1442 	 */
1443 	KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
1444 }
1445 
1446 void
ahci_device_clear_toggle(struct usbd_pipe * pipe)1447 ahci_device_clear_toggle(struct usbd_pipe *pipe)
1448 {
1449 	struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
1450 	apipe->toggle = 0;
1451 }
1452 
1453 #ifdef AHCI_DEBUG
1454 void
print_req(usb_device_request_t * r)1455 print_req(usb_device_request_t *r)
1456 {
1457 	const char *xmes[]={
1458 		"GETSTAT",
1459 		"CLRFEAT",
1460 		"res",
1461 		"SETFEAT",
1462 		"res",
1463 		"SETADDR",
1464 		"GETDESC",
1465 		"SETDESC",
1466 		"GETCONF",
1467 		"SETCONF",
1468 		"GETIN/F",
1469 		"SETIN/F",
1470 		"SYNC_FR"
1471 	};
1472 	int req, type, value, index, len;
1473 
1474 	req   = r->bRequest;
1475 	type  = r->bmRequestType;
1476 	value = UGETW(r->wValue);
1477 	index = UGETW(r->wIndex);
1478 	len   = UGETW(r->wLength);
1479 
1480 	printf("%x,%s,v=%d,i=%d,l=%d ",
1481 		type, xmes[req], value, index, len);
1482 }
1483 
1484 void
print_req_hub(usb_device_request_t * r)1485 print_req_hub(usb_device_request_t *r)
1486 {
1487 	struct {
1488 		int req;
1489 		int type;
1490 		const char *str;
1491 	} conf[] = {
1492 		{ 1, 0x20, "ClrHubFeat"  },
1493 		{ 1, 0x23, "ClrPortFeat" },
1494 		{ 2, 0xa3, "GetBusState" },
1495 		{ 6, 0xa0, "GetHubDesc"  },
1496 		{ 0, 0xa0, "GetHubStat"  },
1497 		{ 0, 0xa3, "GetPortStat" },
1498 		{ 7, 0x20, "SetHubDesc"  },
1499 		{ 3, 0x20, "SetHubFeat"  },
1500 		{ 3, 0x23, "SetPortFeat" },
1501 		{-1, 0, NULL},
1502 	};
1503 	int i;
1504 	int value, index, len;
1505 
1506 	value = UGETW(r->wValue);
1507 	index = UGETW(r->wIndex);
1508 	len   = UGETW(r->wLength);
1509 	for (i = 0; ; i++) {
1510 		if (conf[i].req == -1 )
1511 			return print_req(r);
1512 		if (r->bmRequestType == conf[i].type && r->bRequest == conf[i].req) {
1513 			printf("%s", conf[i].str);
1514 			break;
1515 		}
1516 	}
1517 	printf(",v=%d,i=%d,l=%d ", value, index, len);
1518 }
1519 
1520 void
print_dumpreg(struct ahci_softc * sc)1521 print_dumpreg(struct ahci_softc *sc)
1522 {
1523 #if 0
1524 	printf("00=%02x,01=%02x,02=%02x,03=%02x,04=%02x,"
1525 	       "08=%02x,09=%02x,0A=%02x,0B=%02x,0C=%02x,",
1526 		sl11read(sc, 0),  sl11read(sc, 1),
1527 		sl11read(sc, 2),  sl11read(sc, 3),
1528 		sl11read(sc, 4),  sl11read(sc, 8),
1529 		sl11read(sc, 9),  sl11read(sc, 10),
1530 		sl11read(sc, 11), sl11read(sc, 12)
1531 	);
1532 	printf("CR1=%02x,IER=%02x,0D=%02x,0E=%02x,0F=%02x ",
1533 		sl11read(sc, 5), sl11read(sc, 6),
1534 		sl11read(sc, 13), sl11read(sc, 14), sl11read(sc, 15)
1535 	);
1536 #endif
1537 }
1538 
1539 void
print_xfer(struct usbd_xfer * xfer)1540 print_xfer(struct usbd_xfer *xfer)
1541 {
1542 	printf("xfer: length=%d, actlen=%d, flags=%x, timeout=%d,",
1543 		xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
1544 	printf("request{ ");
1545 	print_req_hub(&xfer->ux_request);
1546 	printf("} ");
1547 }
1548 #endif /* AHCI_DEBUG */
1549