ahci.c revision 1.12.6.5 1 /* $NetBSD: ahci.c,v 1.12.6.5 2014/12/03 12:52:05 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.12.6.5 2014/12/03 12:52:05 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/malloc.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
85 #include <mips/adm5120/include/adm5120reg.h>
86 #include <mips/adm5120/include/adm5120var.h>
87 #include <mips/adm5120/include/adm5120_obiovar.h>
88
89 #include <mips/adm5120/dev/ahcireg.h>
90 #include <mips/adm5120/dev/ahcivar.h>
91
92 static usbd_status ahci_open(usbd_pipe_handle);
93 static void ahci_softintr(void *);
94 static void ahci_poll(struct usbd_bus *);
95 static void ahci_poll_hub(void *);
96 static void ahci_poll_device(void *arg);
97 static usbd_xfer_handle ahci_allocx(struct usbd_bus *);
98 static void ahci_freex(struct usbd_bus *, usbd_xfer_handle);
99
100 static void ahci_get_lock(struct usbd_bus *, kmutex_t **);
101
102 static int ahci_str(usb_string_descriptor_t *, int, const char *);
103
104 static usbd_status ahci_root_ctrl_transfer(usbd_xfer_handle);
105 static usbd_status ahci_root_ctrl_start(usbd_xfer_handle);
106 static void ahci_root_ctrl_abort(usbd_xfer_handle);
107 static void ahci_root_ctrl_close(usbd_pipe_handle);
108 static void ahci_root_ctrl_done(usbd_xfer_handle);
109
110 static usbd_status ahci_root_intr_transfer(usbd_xfer_handle);
111 static usbd_status ahci_root_intr_start(usbd_xfer_handle);
112 static void ahci_root_intr_abort(usbd_xfer_handle);
113 static void ahci_root_intr_close(usbd_pipe_handle);
114 static void ahci_root_intr_done(usbd_xfer_handle);
115
116 static usbd_status ahci_device_ctrl_transfer(usbd_xfer_handle);
117 static usbd_status ahci_device_ctrl_start(usbd_xfer_handle);
118 static void ahci_device_ctrl_abort(usbd_xfer_handle);
119 static void ahci_device_ctrl_close(usbd_pipe_handle);
120 static void ahci_device_ctrl_done(usbd_xfer_handle);
121
122 static usbd_status ahci_device_intr_transfer(usbd_xfer_handle);
123 static usbd_status ahci_device_intr_start(usbd_xfer_handle);
124 static void ahci_device_intr_abort(usbd_xfer_handle);
125 static void ahci_device_intr_close(usbd_pipe_handle);
126 static void ahci_device_intr_done(usbd_xfer_handle);
127
128 static usbd_status ahci_device_isoc_transfer(usbd_xfer_handle);
129 static usbd_status ahci_device_isoc_start(usbd_xfer_handle);
130 static void ahci_device_isoc_abort(usbd_xfer_handle);
131 static void ahci_device_isoc_close(usbd_pipe_handle);
132 static void ahci_device_isoc_done(usbd_xfer_handle);
133
134 static usbd_status ahci_device_bulk_transfer(usbd_xfer_handle);
135 static usbd_status ahci_device_bulk_start(usbd_xfer_handle);
136 static void ahci_device_bulk_abort(usbd_xfer_handle);
137 static void ahci_device_bulk_close(usbd_pipe_handle);
138 static void ahci_device_bulk_done(usbd_xfer_handle);
139
140 static int ahci_transaction(struct ahci_softc *,
141 usbd_pipe_handle, uint8_t, int, u_char *, uint8_t);
142 static void ahci_noop(usbd_pipe_handle);
143 static void ahci_abort_xfer(usbd_xfer_handle, usbd_status);
144 static void ahci_device_clear_toggle(usbd_pipe_handle);
145
146 extern int usbdebug;
147 extern int uhubdebug;
148 extern int umassdebug;
149 int ahci_dummy;
150
151 #define AHCI_DEBUG
152
153 /* For root hub */
154 #define AHCI_INTR_ENDPT (1)
155
156 #ifdef AHCI_DEBUG
157 #define D_TRACE (0x0001) /* function trace */
158 #define D_MSG (0x0002) /* debug messages */
159 #define D_XFER (0x0004) /* transfer messages (noisy!) */
160 #define D_MEM (0x0008) /* memory allocation */
161
162 int ahci_debug = 0;
163 #define DPRINTF(z,x) if((ahci_debug&(z))!=0)printf x
164 void print_req(usb_device_request_t *);
165 void print_req_hub(usb_device_request_t *);
166 void print_dumpreg(struct ahci_softc *);
167 void print_xfer(usbd_xfer_handle);
168 #else
169 #define DPRINTF(z,x)
170 #endif
171
172
173 struct usbd_bus_methods ahci_bus_methods = {
174 .ubm_open = ahci_open,
175 .ubm_softint = ahci_softintr,
176 .ubm_dopoll = ahci_poll,
177 .ubm_allocx = ahci_allocx,
178 .ubm_freex = ahci_freex,
179 .ubm_getlock = ahci_get_lock,
180 };
181
182 struct usbd_pipe_methods ahci_root_ctrl_methods = {
183 .upm_transfer = ahci_root_ctrl_transfer,
184 .upm_start = ahci_root_ctrl_start,
185 .upm_abort = ahci_root_ctrl_abort,
186 .upm_close = ahci_root_ctrl_close,
187 .upm_cleartoggle = ahci_noop,
188 .upm_done = ahci_root_ctrl_done,
189 };
190
191 struct usbd_pipe_methods ahci_root_intr_methods = {
192 .upm_transfer = ahci_root_intr_transfer,
193 .upm_start = ahci_root_intr_start,
194 .upm_abort = ahci_root_intr_abort,
195 .upm_close = ahci_root_intr_close,
196 .upm_cleartoggle = ahci_noop,
197 .upm_done = ahci_root_intr_done,
198 };
199
200 struct usbd_pipe_methods ahci_device_ctrl_methods = {
201 .upm_transfer = ahci_device_ctrl_transfer,
202 .upm_start = ahci_device_ctrl_start,
203 .upm_abort = ahci_device_ctrl_abort,
204 .upm_close = ahci_device_ctrl_close,
205 .upm_cleartoggle = ahci_noop,
206 .upm_done = ahci_device_ctrl_done,
207 };
208
209 struct usbd_pipe_methods ahci_device_intr_methods = {
210 .upm_transfer = ahci_device_intr_transfer,
211 .upm_start = ahci_device_intr_start,
212 .upm_abort = ahci_device_intr_abort,
213 .upm_close = ahci_device_intr_close,
214 .upm_cleartoggle = ahci_device_clear_toggle,
215 .upm_done = ahci_device_intr_done,
216 };
217
218 struct usbd_pipe_methods ahci_device_isoc_methods = {
219 .upm_transfer = ahci_device_isoc_transfer,
220 .upm_start = ahci_device_isoc_start,
221 .upm_abort = ahci_device_isoc_abort,
222 .upm_close = ahci_device_isoc_close,
223 .upm_cleartoggle = ahci_noop,
224 .upm_done = ahci_device_isoc_done,
225 };
226
227 struct usbd_pipe_methods ahci_device_bulk_methods = {
228 .upm_transfer = ahci_device_bulk_transfer,
229 .upm_start = ahci_device_bulk_start,
230 .upm_abort = ahci_device_bulk_abort,
231 .upm_close = ahci_device_bulk_close,
232 .upm_cleartoggle = ahci_device_clear_toggle,
233 .upm_done = ahci_device_bulk_done,
234 };
235
236 struct ahci_pipe {
237 struct usbd_pipe pipe;
238 uint32_t toggle;
239 };
240
241 static int ahci_match(device_t, cfdata_t, void *);
242 static void ahci_attach(device_t, device_t, void *);
243
244 CFATTACH_DECL_NEW(ahci, sizeof(struct ahci_softc),
245 ahci_match, ahci_attach, NULL, NULL);
246
247 static int
248 ahci_match(device_t parent, struct cfdata *cf, void *aux)
249 {
250 struct obio_attach_args *aa = aux;
251
252 if (strcmp(aa->oba_name, cf->cf_name) == 0)
253 return (1);
254
255 return (0);
256 }
257
258 #define REG_READ(o) bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
259 #define REG_WRITE(o,v) bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
260
261 /*
262 * Attach SL11H/SL811HS. Return 0 if success.
263 */
264 void
265 ahci_attach(device_t parent, device_t self, void *aux)
266 {
267 struct obio_attach_args *aa = aux;
268 struct ahci_softc *sc = device_private(self);
269
270 printf("\n");
271 sc->sc_dmat = aa->oba_dt;
272 sc->sc_st = aa->oba_st;
273
274 /* Initialize sc */
275 sc->sc_bus.ub_revision = USBREV_1_1;
276 sc->sc_bus.ub_methods = &ahci_bus_methods;
277 sc->sc_bus.ub_pipesize = sizeof(struct ahci_pipe);
278 sc->sc_bus.ub_dmatag = sc->sc_dmat;
279 sc->sc_bus.ub_usedma = true;
280
281 /* Map the device. */
282 if (bus_space_map(sc->sc_st, aa->oba_addr,
283 512, 0, &sc->sc_ioh) != 0) {
284 aprint_error_dev(self, "unable to map device\n");
285 return;
286 }
287
288 /* Hook up the interrupt handler. */
289 sc->sc_ih = adm5120_intr_establish(aa->oba_irq, INTR_IRQ, ahci_intr, sc);
290
291 if (sc->sc_ih == NULL) {
292 aprint_error_dev(self,
293 "unable to register interrupt handler\n");
294 return;
295 }
296
297 SIMPLEQ_INIT(&sc->sc_free_xfers);
298
299 callout_init(&sc->sc_poll_handle, 0);
300
301 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
302 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED /* XXXNH */);
303
304 REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* disable interrupts */
305 REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_SW_RESET); /* reset */
306 delay_ms(10);
307 while (REG_READ(ADMHCD_REG_CONTROL) & ADMHCD_SW_RESET)
308 delay_ms(1);
309
310 REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_HOST_EN);
311 REG_WRITE(ADMHCD_REG_HOSTHEAD, 0x00000000);
312 REG_WRITE(ADMHCD_REG_FMINTERVAL, 0x20002edf);
313 REG_WRITE(ADMHCD_REG_LSTHRESH, 0x628);
314 REG_WRITE(ADMHCD_REG_RHDESCR, ADMHCD_NPS | ADMHCD_LPSC);
315 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
316
317 REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* XXX: enable interrupts */
318
319 #ifdef USB_DEBUG
320 /* usbdebug = 0x7f;
321 uhubdebug = 0x7f;
322 umassdebug = 0xffffffff; */
323 #endif
324
325 /* Attach USB devices */
326 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
327
328 }
329
330 int
331 ahci_intr(void *arg)
332 {
333 #if 0
334 struct ahci_softc *sc = arg;
335 uint8_t r;
336 #ifdef AHCI_DEBUG
337 char bitbuf[256];
338 #endif
339
340 r = sl11read(sc, SL11_ISR);
341
342 sl11write(sc, SL11_ISR, SL11_ISR_DATA | SL11_ISR_SOFTIMER);
343
344 if ((r & SL11_ISR_RESET)) {
345 sc->sc_flags |= AHCDF_RESET;
346 sl11write(sc, SL11_ISR, SL11_ISR_RESET);
347 }
348 if ((r & SL11_ISR_INSERT)) {
349 sc->sc_flags |= AHCDF_INSERT;
350 sl11write(sc, SL11_ISR, SL11_ISR_INSERT);
351 }
352
353 #ifdef AHCI_DEBUG
354 snprintb(bitbuf, sizeof(bitbuf),
355 ((sl11read(sc, SL11_CTRL) & SL11_CTRL_SUSPEND)
356 ? "\20\x8""D+\7RESUME\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"
357 : "\20\x8""D+\7RESET\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"),
358 r);
359
360 DPRINTF(D_XFER, ("I=%s ", bitbuf));
361 #endif /* AHCI_DEBUG */
362 #endif
363
364 return 0;
365 }
366
367 usbd_status
368 ahci_open(usbd_pipe_handle pipe)
369 {
370 usbd_device_handle dev = pipe->up_dev;
371 struct ahci_softc *sc = (struct ahci_softc *)dev->ud_bus;
372 struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
373 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
374
375 DPRINTF(D_TRACE, ("ahci_open(addr=%d,ep=%d,scaddr=%d)",
376 dev->ud_addr, ed->bEndpointAddress, sc->sc_addr));
377
378 apipe->toggle=0;
379
380 if (dev->ud_addr == sc->sc_addr) {
381 switch (ed->bEndpointAddress) {
382 case USB_CONTROL_ENDPOINT:
383 pipe->up_methods = &ahci_root_ctrl_methods;
384 break;
385 case UE_DIR_IN | AHCI_INTR_ENDPT:
386 pipe->up_methods = &ahci_root_intr_methods;
387 break;
388 default:
389 printf("open:endpointErr!\n");
390 return USBD_INVAL;
391 }
392 } else {
393 switch (ed->bmAttributes & UE_XFERTYPE) {
394 case UE_CONTROL:
395 DPRINTF(D_MSG, ("control "));
396 pipe->up_methods = &ahci_device_ctrl_methods;
397 break;
398 case UE_INTERRUPT:
399 DPRINTF(D_MSG, ("interrupt "));
400 pipe->up_methods = &ahci_device_intr_methods;
401 break;
402 case UE_ISOCHRONOUS:
403 DPRINTF(D_MSG, ("isochronous "));
404 pipe->up_methods = &ahci_device_isoc_methods;
405 break;
406 case UE_BULK:
407 DPRINTF(D_MSG, ("bluk "));
408 pipe->up_methods = &ahci_device_bulk_methods;
409 break;
410 }
411 }
412 return USBD_NORMAL_COMPLETION;
413 }
414
415 void
416 ahci_softintr(void *arg)
417 {
418 DPRINTF(D_TRACE, ("%s()", __func__));
419 }
420
421 void
422 ahci_poll(struct usbd_bus *bus)
423 {
424 DPRINTF(D_TRACE, ("%s()", __func__));
425 }
426
427 /*
428 * Emulation of interrupt transfer for status change endpoint
429 * of root hub.
430 */
431 void
432 ahci_poll_hub(void *arg)
433 {
434 usbd_xfer_handle xfer = arg;
435 usbd_pipe_handle pipe = xfer->ux_pipe;
436 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
437 u_char *p;
438 static int p0_state=0;
439 static int p1_state=0;
440
441 callout_reset(&sc->sc_poll_handle, sc->sc_interval, ahci_poll_hub, xfer);
442
443 /* USB spec 11.13.3 (p.260) */
444 p = KERNADDR(&xfer->ux_dmabuf, 0);
445 p[0] = 0;
446 if ((REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS) != p0_state) {
447 p[0] = 2;
448 DPRINTF(D_TRACE, ("!"));
449 p0_state=(REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS);
450 };
451 if ((REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS) != p1_state) {
452 p[0] = 2;
453 DPRINTF(D_TRACE, ("@"));
454 p1_state=(REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS);
455 };
456
457 /* no change, return NAK */
458 if (p[0] == 0)
459 return;
460
461 xfer->ux_actlen = 1;
462 xfer->ux_status = USBD_NORMAL_COMPLETION;
463 mutex_enter(&sc->sc_lock);
464 usb_transfer_complete(xfer);
465 mutex_exit(&sc->sc_lock);
466 }
467
468 usbd_xfer_handle
469 ahci_allocx(struct usbd_bus *bus)
470 {
471 struct ahci_softc *sc = (struct ahci_softc *)bus;
472 usbd_xfer_handle xfer;
473
474 DPRINTF(D_MEM, ("SLallocx"));
475
476 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
477 if (xfer) {
478 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, ux_next);
479 #ifdef DIAGNOSTIC
480 if (xfer->ux_state != XFER_FREE) {
481 printf("ahci_allocx: xfer=%p not free, 0x%08x\n",
482 xfer, xfer->ux_state);
483 }
484 #endif
485 } else {
486 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
487 }
488
489 if (xfer) {
490 memset(xfer, 0, sizeof(*xfer));
491 #ifdef DIAGNOSTIC
492 xfer->ux_state = XFER_BUSY;
493 #endif
494 }
495
496 return xfer;
497 }
498
499 void
500 ahci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
501 {
502 struct ahci_softc *sc = (struct ahci_softc *)bus;
503
504 DPRINTF(D_MEM, ("SLfreex"));
505
506 #ifdef DIAGNOSTIC
507 if (xfer->ux_state != XFER_BUSY) {
508 printf("ahci_freex: xfer=%p not busy, 0x%08x\n",
509 xfer, xfer->ux_state);
510 return;
511 }
512 xfer->ux_state = XFER_FREE;
513 #endif
514 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, ux_next);
515 }
516
517 static void
518 ahci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
519 {
520 struct ahci_softc *sc = bus->ub_hcpriv;
521
522 *lock = &sc->sc_lock;
523 }
524
525 void
526 ahci_noop(usbd_pipe_handle pipe)
527 {
528 DPRINTF(D_TRACE, ("%s()", __func__));
529 }
530
531 /*
532 * Data structures and routines to emulate the root hub.
533 */
534 usb_device_descriptor_t ahci_devd = {
535 USB_DEVICE_DESCRIPTOR_SIZE,
536 UDESC_DEVICE, /* type */
537 {0x01, 0x01}, /* USB version */
538 UDCLASS_HUB, /* class */
539 UDSUBCLASS_HUB, /* subclass */
540 0, /* protocol */
541 64, /* max packet */
542 {USB_VENDOR_SCANLOGIC & 0xff, /* vendor ID (low) */
543 USB_VENDOR_SCANLOGIC >> 8 }, /* vendor ID (high) */
544 {0} /* ? */, /* product ID */
545 {0}, /* device */
546 1, /* index to manufacturer */
547 2, /* index to product */
548 0, /* index to serial number */
549 1 /* number of configurations */
550 };
551
552 usb_config_descriptor_t ahci_confd = {
553 USB_CONFIG_DESCRIPTOR_SIZE,
554 UDESC_CONFIG,
555 {USB_CONFIG_DESCRIPTOR_SIZE +
556 USB_INTERFACE_DESCRIPTOR_SIZE +
557 USB_ENDPOINT_DESCRIPTOR_SIZE},
558 1, /* number of interfaces */
559 1, /* configuration value */
560 0, /* index to configuration */
561 UC_SELF_POWERED, /* attributes */
562 250 /* max current is 500mA... */
563 };
564
565 usb_interface_descriptor_t ahci_ifcd = {
566 USB_INTERFACE_DESCRIPTOR_SIZE,
567 UDESC_INTERFACE,
568 0, /* interface number */
569 0, /* alternate setting */
570 1, /* number of endpoint */
571 UICLASS_HUB, /* class */
572 UISUBCLASS_HUB, /* subclass */
573 0, /* protocol */
574 0 /* index to interface */
575 };
576
577 usb_endpoint_descriptor_t ahci_endpd = {
578 USB_ENDPOINT_DESCRIPTOR_SIZE,
579 UDESC_ENDPOINT,
580 UE_DIR_IN | AHCI_INTR_ENDPT, /* endpoint address */
581 UE_INTERRUPT, /* attributes */
582 {8}, /* max packet size */
583 255 /* interval */
584 };
585
586 usb_hub_descriptor_t ahci_hubd = {
587 USB_HUB_DESCRIPTOR_SIZE,
588 UDESC_HUB,
589 2, /* number of ports */
590 { 0, 0}, /* hub characteristics */
591 0, /* 5:power on to power good */
592 0, /* 6:maximum current */
593 { 0x00 }, /* both ports are removable */
594 { 0x00 } /* port power control mask */
595 };
596
597 static int
598 ahci_str(usb_string_descriptor_t *p, int l, const char *s)
599 {
600 int i;
601
602 if (l == 0)
603 return 0;
604 p->bLength = 2 * strlen(s) + 2;
605 if (l == 1)
606 return 1;
607 p->bDescriptorType = UDESC_STRING;
608 l -= 2;
609 for (i = 0; s[i] && l > 1; i++, l -= 2)
610 USETW2(p->bString[i], 0, s[i]);
611 return 2 * i + 2;
612 }
613
614 usbd_status
615 ahci_root_ctrl_transfer(usbd_xfer_handle xfer)
616 {
617 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
618 usbd_status error;
619
620 DPRINTF(D_TRACE, ("SLRCtrans "));
621
622 /* Insert last in queue */
623 mutex_enter(&sc->sc_lock);
624 error = usb_insert_transfer(xfer);
625 mutex_exit(&sc->sc_lock);
626 if (error) {
627 DPRINTF(D_MSG, ("usb_insert_transfer returns err! "));
628 return error;
629 }
630
631 /*
632 * Pipe isn't running (otherwise error would be USBD_INPROG),
633 * so start it first.
634 */
635 return ahci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
636 }
637
638 usbd_status
639 ahci_root_ctrl_start(usbd_xfer_handle xfer)
640 {
641 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
642 usb_device_request_t *req;
643 int len, value, index, l, status;
644 int totlen = 0;
645 void *buf = NULL;
646 usb_port_status_t ps;
647 usbd_status error;
648
649
650 DPRINTF(D_TRACE, ("SLRCstart "));
651
652 req = &xfer->ux_request;
653
654 len = UGETW(req->wLength);
655 value = UGETW(req->wValue);
656 index = UGETW(req->wIndex);
657
658 if (len)
659 buf = KERNADDR(&xfer->ux_dmabuf, 0);
660
661 #ifdef AHCI_DEBUG
662 if ((ahci_debug & D_TRACE))
663 print_req_hub(req);
664 #endif
665
666 #define C(x,y) ((x) | ((y) << 8))
667 switch (C(req->bRequest, req->bmRequestType)) {
668 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
669 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(DEVICE)XXX "));
670 break;
671 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
672 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(INTERFACE)XXX "));
673 break;
674 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
675 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(ENDPOINT)XXX "));
676 break;
677 case C(UR_GET_CONFIG, UT_READ_DEVICE):
678 DPRINTF(D_MSG, ("UR_GET_CONFIG "));
679 if (len > 0) {
680 *(uint8_t *)buf = sc->sc_conf;
681 totlen = 1;
682 }
683 break;
684 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
685 switch (value >> 8) {
686 case UDESC_DEVICE:
687 DPRINTF(D_MSG, ("UDESC_DEVICE "));
688 if ((value & 0xff) != 0) {
689 error = USBD_IOERROR;
690 goto ret;
691 }
692 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
693 memcpy(buf, &ahci_devd, l);
694 break;
695 case UDESC_CONFIG:
696 DPRINTF(D_MSG, ("UDESC_CONFIG "));
697 if ((value & 0xff) != 0) {
698 error = USBD_IOERROR;
699 goto ret;
700 }
701 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
702 memcpy(buf, &ahci_confd, l);
703 buf = (char *)buf + l;
704 len -= l;
705
706 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
707 totlen += l;
708 memcpy(buf, &ahci_ifcd, l);
709 buf = (char *)buf + l;
710 len -= l;
711
712 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
713 totlen += l;
714 memcpy(buf, &ahci_endpd, l);
715 break;
716 case UDESC_STRING:
717 DPRINTF(D_MSG, ("UDESC_STR "));
718 if (len == 0)
719 break;
720 *(uint8_t *)buf = 0;
721 totlen = 1;
722 switch (value & 0xff) {
723 case 0:
724 break;
725 case 1: /* Vendor */
726 totlen = ahci_str(buf, len, "ADMTek");
727 break;
728 case 2: /* Product */
729 totlen = ahci_str(buf, len, "ADM5120 root hub");
730 break;
731 default:
732 printf("strerr%d ", value & 0xff);
733 break;
734 }
735 break;
736 default:
737 printf("unknownGetDescriptor=%x", value);
738 error = USBD_IOERROR;
739 break;
740 }
741 break;
742 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
743 /* Get Interface, 9.4.4 */
744 if (len > 0) {
745 *(uint8_t *)buf = 0;
746 totlen = 1;
747 }
748 break;
749 case C(UR_GET_STATUS, UT_READ_DEVICE):
750 /* Get Status from device, 9.4.5 */
751 if (len > 1) {
752 USETW(((usb_status_t *)buf)->wStatus, UDS_SELF_POWERED);
753 totlen = 2;
754 }
755 break;
756 case C(UR_GET_STATUS, UT_READ_INTERFACE):
757 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
758 /* Get Status from interface, endpoint, 9.4.5 */
759 if (len > 1) {
760 USETW(((usb_status_t *)buf)->wStatus, 0);
761 totlen = 2;
762 }
763 break;
764 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
765 /* Set Address, 9.4.6 */
766 DPRINTF(D_MSG, ("UR_SET_ADDRESS "));
767 if (value >= USB_MAX_DEVICES) {
768 error = USBD_IOERROR;
769 goto ret;
770 }
771 sc->sc_addr = value;
772 break;
773 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
774 /* Set Configuration, 9.4.7 */
775 DPRINTF(D_MSG, ("UR_SET_CONFIG "));
776 if (value != 0 && value != 1) {
777 error = USBD_IOERROR;
778 goto ret;
779 }
780 sc->sc_conf = value;
781 break;
782 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
783 /* Set Descriptor, 9.4.8, not supported */
784 DPRINTF(D_MSG, ("UR_SET_DESCRIPTOR,WRITE_DEVICE not supported\n"));
785 break;
786 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
787 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
788 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
789 /* Set Feature, 9.4.9, not supported */
790 DPRINTF(D_MSG, ("UR_SET_FEATURE not supported\n"));
791 error = USBD_IOERROR;
792 break;
793 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
794 /* Set Interface, 9.4.10, not supported */
795 break;
796 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
797 /* Synch Frame, 9.4.11, not supported */
798 break;
799
800 /*
801 * Hub specific requests
802 */
803 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
804 /* Clear Hub Feature, 11.16.2.1, not supported */
805 DPRINTF(D_MSG, ("ClearHubFeature not supported\n"));
806 break;
807 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
808
809 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
810 /* Clear Port Feature, 11.16.2.2 */
811 if (index != 1 && index != 2 ) {
812 error = USBD_IOERROR;
813 goto ret;
814 }
815 switch (value) {
816 case UHF_PORT_POWER:
817 DPRINTF(D_MSG, ("POWER_OFF "));
818 WPS(ADMHCD_LSDA);
819 break;
820 case UHF_PORT_SUSPEND:
821 DPRINTF(D_MSG, ("SUSPEND "));
822 WPS(ADMHCD_POCI);
823 break;
824 case UHF_PORT_ENABLE:
825 DPRINTF(D_MSG, ("ENABLE "));
826 WPS(ADMHCD_CCS);
827 break;
828 case UHF_C_PORT_CONNECTION:
829 WPS(ADMHCD_CSC);
830 break;
831 case UHF_C_PORT_RESET:
832 WPS(ADMHCD_PRSC);
833 break;
834 case UHF_C_PORT_SUSPEND:
835 WPS(ADMHCD_PSSC);
836 break;
837 case UHF_C_PORT_ENABLE:
838 WPS(ADMHCD_PESC);
839 break;
840 case UHF_C_PORT_OVER_CURRENT:
841 WPS(ADMHCD_OCIC);
842 break;
843 default:
844 printf("ClrPortFeatERR:value=0x%x ", value);
845 error = USBD_IOERROR;
846 break;
847 }
848 //DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change));
849 #undef WPS
850 break;
851 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
852 /* Get Bus State, 11.16.2.3, not supported */
853 /* shall return a STALL... */
854 break;
855 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
856 /* Get Hub Descriptor, 11.16.2.4 */
857 DPRINTF(D_MSG, ("UR_GET_DESCRIPTOR RCD"));
858 if ((value&0xff) != 0) {
859 error = USBD_IOERROR;
860 goto ret;
861 }
862 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
863 totlen = l;
864 memcpy(buf, &ahci_hubd, l);
865 break;
866 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
867 /* Get Hub Status, 11.16.2.5 */
868 DPRINTF(D_MSG, ("UR_GET_STATUS RCD"));
869 if (len != 4) {
870 error = USBD_IOERROR;
871 goto ret;
872 }
873 memset(buf, 0, len);
874 totlen = len;
875 break;
876 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
877 /* Get Port Status, 11.16.2.6 */
878 if ((index != 1 && index != 2) || len != 4) {
879 printf("index=%d,len=%d ", index, len);
880 error = USBD_IOERROR;
881 goto ret;
882 }
883 status = REG_READ(ADMHCD_REG_PORTSTATUS0+(index-1)*4);
884 DPRINTF(D_MSG, ("UR_GET_STATUS RCO=%x ", status));
885
886 //DPRINTF(D_XFER, ("ST=%04x,CH=%04x ", status, sc->sc_change));
887 USETW(ps.wPortStatus, status & (UPS_CURRENT_CONNECT_STATUS|UPS_PORT_ENABLED|UPS_SUSPEND|UPS_OVERCURRENT_INDICATOR|UPS_RESET|UPS_PORT_POWER|UPS_LOW_SPEED));
888 USETW(ps.wPortChange, (status>>16) & (UPS_C_CONNECT_STATUS|UPS_C_PORT_ENABLED|UPS_C_SUSPEND|UPS_C_OVERCURRENT_INDICATOR|UPS_C_PORT_RESET));
889 l = min(len, sizeof(ps));
890 memcpy(buf, &ps, l);
891 totlen = l;
892 break;
893 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
894 /* Set Hub Descriptor, 11.16.2.7, not supported */
895 /* STALL ? */
896 error = USBD_IOERROR;
897 break;
898 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
899 /* Set Hub Feature, 11.16.2.8, not supported */
900 break;
901 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
902 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
903 /* Set Port Feature, 11.16.2.9 */
904 if ((index != 1) && (index !=2)) {
905 printf("index=%d ", index);
906 error = USBD_IOERROR;
907 goto ret;
908 }
909 switch (value) {
910 case UHF_PORT_RESET:
911 DPRINTF(D_MSG, ("PORT_RESET "));
912 WPS(ADMHCD_PRS);
913 break;
914 case UHF_PORT_POWER:
915 DPRINTF(D_MSG, ("PORT_POWER "));
916 WPS(ADMHCD_PPS);
917 break;
918 case UHF_PORT_ENABLE:
919 DPRINTF(D_MSG, ("PORT_ENABLE "));
920 WPS(ADMHCD_PES);
921 break;
922 default:
923 printf("SetPortFeatERR=0x%x ", value);
924 error = USBD_IOERROR;
925 break;
926 }
927 #undef WPS
928 break;
929 default:
930 DPRINTF(D_MSG, ("ioerr(UR=%02x,UT=%02x) ",
931 req->bRequest, req->bmRequestType));
932 error = USBD_IOERROR;
933 goto ret;
934 }
935 xfer->ux_actlen = totlen;
936 error = USBD_NORMAL_COMPLETION;
937 ret:
938 xfer->ux_status = error;
939 mutex_enter(&sc->sc_lock);
940 usb_transfer_complete(xfer);
941 mutex_exit(&sc->sc_lock);
942 return USBD_IN_PROGRESS;
943 }
944
945 void
946 ahci_root_ctrl_abort(usbd_xfer_handle xfer)
947 {
948 DPRINTF(D_TRACE, ("SLRCabort "));
949 }
950
951 void
952 ahci_root_ctrl_close(usbd_pipe_handle pipe)
953 {
954 DPRINTF(D_TRACE, ("SLRCclose "));
955 }
956
957 void
958 ahci_root_ctrl_done(usbd_xfer_handle xfer)
959 {
960 DPRINTF(D_TRACE, ("SLRCdone\n"));
961 }
962
963 static usbd_status
964 ahci_root_intr_transfer(usbd_xfer_handle xfer)
965 {
966 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
967 usbd_status error;
968
969 DPRINTF(D_TRACE, ("SLRItransfer "));
970
971 /* Insert last in queue */
972 mutex_enter(&sc->sc_lock);
973 error = usb_insert_transfer(xfer);
974 mutex_exit(&sc->sc_lock);
975 if (error)
976 return error;
977
978 /*
979 * Pipe isn't running (otherwise error would be USBD_INPROG),
980 * start first.
981 */
982 return ahci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
983 }
984
985 static usbd_status
986 ahci_root_intr_start(usbd_xfer_handle xfer)
987 {
988 usbd_pipe_handle pipe = xfer->ux_pipe;
989 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
990
991 DPRINTF(D_TRACE, ("SLRIstart "));
992
993 sc->sc_interval = MS_TO_TICKS(xfer->ux_pipe->up_endpoint->ue_edesc->bInterval);
994 callout_reset(&sc->sc_poll_handle, sc->sc_interval, ahci_poll_hub, xfer);
995 sc->sc_intr_xfer = xfer;
996 return USBD_IN_PROGRESS;
997 }
998
999 static void
1000 ahci_root_intr_abort(usbd_xfer_handle xfer)
1001 {
1002 DPRINTF(D_TRACE, ("SLRIabort "));
1003 }
1004
1005 static void
1006 ahci_root_intr_close(usbd_pipe_handle pipe)
1007 {
1008 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1009
1010 DPRINTF(D_TRACE, ("SLRIclose "));
1011
1012 callout_stop(&sc->sc_poll_handle);
1013 sc->sc_intr_xfer = NULL;
1014 }
1015
1016 static void
1017 ahci_root_intr_done(usbd_xfer_handle xfer)
1018 {
1019 //DPRINTF(D_XFER, ("RIdn "));
1020 }
1021
1022 static usbd_status
1023 ahci_device_ctrl_transfer(usbd_xfer_handle xfer)
1024 {
1025 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1026 usbd_status error;
1027
1028 DPRINTF(D_TRACE, ("C"));
1029
1030 mutex_enter(&sc->sc_lock);
1031 error = usb_insert_transfer(xfer);
1032 mutex_exit(&sc->sc_lock);
1033 if (error)
1034 return error;
1035
1036 return ahci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1037 }
1038
1039 static usbd_status
1040 ahci_device_ctrl_start(usbd_xfer_handle xfer)
1041 {
1042 usbd_status status = USBD_NORMAL_COMPLETION;
1043 int s, err;
1044 static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
1045 static struct admhcd_td td_v[4] __attribute__((aligned(16))), *td, *td1, *td2, *td3;
1046 static usb_dma_t reqdma;
1047 usbd_pipe_handle pipe = xfer->ux_pipe;
1048 usb_device_request_t *req = &xfer->ux_request;
1049 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1050 int len, isread;
1051
1052
1053 #if 0
1054 struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
1055 #endif
1056 mutex_enter(&sc->sc_lock);
1057 /* printf("ctrl_start>>>\n"); */
1058
1059 #ifdef DIAGNOSTIC
1060 if (!(xfer->ux_rqflags & URQ_REQUEST)) {
1061 /* XXX panic */
1062 printf("ahci_device_ctrl_transfer: not a request\n");
1063 return (USBD_INVAL);
1064 }
1065 #endif
1066
1067 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
1068 DPRINTF(D_TRACE, ("st "));
1069 if (!ep) {
1070 ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
1071 td = (struct admhcd_td *)KSEG1ADDR(&td_v[0]);
1072 td1 = (struct admhcd_td *)KSEG1ADDR(&td_v[1]);
1073 td2 = (struct admhcd_td *)KSEG1ADDR(&td_v[2]);
1074 td3 = (struct admhcd_td *)KSEG1ADDR(&td_v[3]);
1075 err = usb_allocmem(&sc->sc_bus,
1076 sizeof(usb_device_request_t),
1077 0, &reqdma);
1078 if (err)
1079 return (USBD_NOMEM);
1080
1081 /* printf("ep: %p\n",ep); */
1082 };
1083
1084 ep->control = pipe->up_dev->ud_addr | \
1085 ((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
1086 ((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
1087 memcpy(KERNADDR(&reqdma, 0), req, sizeof *req);
1088 /* printf("status: %x\n",REG_READ(ADMHCD_REG_PORTSTATUS0));
1089 printf("ep_control: %x\n",ep->control);
1090 printf("speed: %x\n",pipe->up_dev->ud_speed);
1091 printf("req: %p\n",req);
1092 printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
1093
1094 isread = req->bmRequestType & UT_READ;
1095 len = UGETW(req->wLength);
1096
1097 ep->next = ep;
1098
1099 td->buffer = DMAADDR(&reqdma,0) | 0xa0000000;
1100 td->buflen=sizeof(*req);
1101 td->control=ADMHCD_TD_SETUP | ADMHCD_TD_DATA0 | ADMHCD_TD_OWN;
1102
1103 if (len) {
1104 td->next = td1;
1105
1106 td1->buffer = DMAADDR(&xfer->ux_dmabuf,0) | 0xa0000000;
1107 td1->buflen = len;
1108 td1->next = td2;
1109 td1->control= (isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | ADMHCD_TD_DATA1 | ADMHCD_TD_R | ADMHCD_TD_OWN;
1110 } else {
1111 td1->control = 0;
1112 td->next = td2;
1113 };
1114
1115 td2->buffer = 0;
1116 td2->buflen= 0;
1117 td2->next = td3;
1118 td2->control = (isread?ADMHCD_TD_OUT:ADMHCD_TD_IN) | ADMHCD_TD_DATA1 | ADMHCD_TD_OWN;
1119
1120 td3->buffer = 0;
1121 td3->buflen= 0;
1122 td3->next = 0;
1123 td3->control = 0;
1124
1125 ep->head = td;
1126 ep->tail = td3;
1127 /*
1128 printf("ep: %p\n",ep);
1129 printf("ep->next: %p\n",ep->next);
1130 printf("ep->head: %p\n",ep->head);
1131 printf("ep->tail: %p\n",ep->tail);
1132 printf("td: %p\n",td);
1133 printf("td->next: %p\n",td->next);
1134 printf("td->buffer: %x\n",td->buffer);
1135 printf("td->buflen: %x\n",td->buflen);
1136 printf("td1: %p\n",td1);
1137 printf("td1->next: %p\n",td1->next);
1138 printf("td2: %p\n",td2);
1139 printf("td2->next: %p\n",td2->next);
1140 printf("td3: %p\n",td3);
1141 printf("td3->next: %p\n",td3->next);
1142 */
1143
1144 REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
1145 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
1146 /* printf("1: %x %x %x %x\n", ep->control, td->control, td1->control, td2->control); */
1147 s=100;
1148 while (s--) {
1149 delay_ms(10);
1150 /* printf("%x %x %x %x\n", ep->control, td->control, td1->control, td2->control);*/
1151 status = USBD_TIMEOUT;
1152 if (td->control & ADMHCD_TD_OWN) continue;
1153
1154 err = (td->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1155 if (err) {
1156 status = USBD_IOERROR;
1157 break;
1158 };
1159
1160 status = USBD_TIMEOUT;
1161 if (td1->control & ADMHCD_TD_OWN) continue;
1162 err = (td1->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1163 if (err) {
1164 status = USBD_IOERROR;
1165 break;
1166 };
1167
1168 status = USBD_TIMEOUT;
1169 if (td2->control & ADMHCD_TD_OWN) continue;
1170 err = (td2->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1171 if (err) {
1172 status = USBD_IOERROR;
1173 };
1174 status = USBD_NORMAL_COMPLETION;
1175 break;
1176
1177 };
1178 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
1179
1180 xfer->ux_actlen = len;
1181 xfer->ux_status = status;
1182
1183 /* printf("ctrl_start<<<\n"); */
1184
1185 usb_transfer_complete(xfer);
1186 mutex_exit(&sc->sc_lock);
1187 return USBD_NORMAL_COMPLETION;
1188 }
1189
1190 static void
1191 ahci_device_ctrl_abort(usbd_xfer_handle xfer)
1192 {
1193 DPRINTF(D_TRACE, ("Cab "));
1194 ahci_abort_xfer(xfer, USBD_CANCELLED);
1195 }
1196
1197 static void
1198 ahci_device_ctrl_close(usbd_pipe_handle pipe)
1199 {
1200 DPRINTF(D_TRACE, ("Ccl "));
1201 }
1202
1203 static void
1204 ahci_device_ctrl_done(usbd_xfer_handle xfer)
1205 {
1206 DPRINTF(D_TRACE, ("Cdn "));
1207 }
1208
1209 static usbd_status
1210 ahci_device_intr_transfer(usbd_xfer_handle xfer)
1211 {
1212 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1213 usbd_status error;
1214
1215 DPRINTF(D_TRACE, ("INTRtrans "));
1216
1217 mutex_enter(&sc->sc_lock);
1218 error = usb_insert_transfer(xfer);
1219 mutex_exit(&sc->sc_lock);
1220 if (error)
1221 return error;
1222
1223 return ahci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1224 }
1225
1226 static usbd_status
1227 ahci_device_intr_start(usbd_xfer_handle xfer)
1228 {
1229 usbd_pipe_handle pipe = xfer->ux_pipe;
1230 struct ahci_xfer *sx;
1231
1232 DPRINTF(D_TRACE, ("INTRstart "));
1233
1234 sx = malloc(sizeof(*sx), M_USB, M_NOWAIT);
1235 if (sx == NULL)
1236 goto reterr;
1237 memset(sx, 0, sizeof(*sx));
1238 sx->sx_xfer = xfer;
1239 xfer->ux_hcpriv = sx;
1240
1241 /* initialize callout */
1242 callout_init(&sx->sx_callout_t, 0);
1243 callout_reset(&sx->sx_callout_t,
1244 MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1245 ahci_poll_device, sx);
1246
1247 /* ACK */
1248 return USBD_IN_PROGRESS;
1249
1250 reterr:
1251 return USBD_IOERROR;
1252 }
1253
1254 static void
1255 ahci_poll_device(void *arg)
1256 {
1257 struct ahci_xfer *sx = (struct ahci_xfer *)arg;
1258 usbd_xfer_handle xfer = sx->sx_xfer;
1259 usbd_pipe_handle pipe = xfer->ux_pipe;
1260 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1261 void *buf;
1262 int pid;
1263 int r;
1264
1265 DPRINTF(D_TRACE, ("pldev"));
1266
1267 callout_reset(&sx->sx_callout_t,
1268 MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1269 ahci_poll_device, sx);
1270
1271 /* interrupt transfer */
1272 pid = (UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN)
1273 ? ADMHCD_TD_IN : ADMHCD_TD_OUT;
1274 buf = KERNADDR(&xfer->ux_dmabuf, 0);
1275
1276 r = ahci_transaction(sc, pipe, pid, xfer->ux_length, buf, 0/*toggle*/);
1277 if (r < 0) {
1278 DPRINTF(D_MSG, ("%s error", __func__));
1279 return;
1280 }
1281 /* no change, return NAK */
1282 if (r == 0)
1283 return;
1284
1285 xfer->ux_status = USBD_NORMAL_COMPLETION;
1286 mutex_enter(&sc->sc_lock);
1287 usb_transfer_complete(xfer);
1288 mutex_exit(&sc->sc_lock);
1289 }
1290
1291 static void
1292 ahci_device_intr_abort(usbd_xfer_handle xfer)
1293 {
1294 struct ahci_xfer *sx;
1295
1296 DPRINTF(D_TRACE, ("INTRabort "));
1297
1298 sx = xfer->ux_hcpriv;
1299 if (sx) {
1300 callout_stop(&sx->sx_callout_t);
1301 free(sx, M_USB);
1302 xfer->ux_hcpriv = NULL;
1303 } else {
1304 printf("%s: sx == NULL!\n", __func__);
1305 }
1306 ahci_abort_xfer(xfer, USBD_CANCELLED);
1307 }
1308
1309 static void
1310 ahci_device_intr_close(usbd_pipe_handle pipe)
1311 {
1312 DPRINTF(D_TRACE, ("INTRclose "));
1313 }
1314
1315 static void
1316 ahci_device_intr_done(usbd_xfer_handle xfer)
1317 {
1318 DPRINTF(D_TRACE, ("INTRdone "));
1319 }
1320
1321 static usbd_status
1322 ahci_device_isoc_transfer(usbd_xfer_handle xfer)
1323 {
1324 DPRINTF(D_TRACE, ("S"));
1325 return USBD_NORMAL_COMPLETION;
1326 }
1327
1328 static usbd_status
1329 ahci_device_isoc_start(usbd_xfer_handle xfer)
1330 {
1331 DPRINTF(D_TRACE, ("st "));
1332 return USBD_NORMAL_COMPLETION;
1333 }
1334
1335 static void
1336 ahci_device_isoc_abort(usbd_xfer_handle xfer)
1337 {
1338 DPRINTF(D_TRACE, ("Sab "));
1339 }
1340
1341 static void
1342 ahci_device_isoc_close(usbd_pipe_handle pipe)
1343 {
1344 DPRINTF(D_TRACE, ("Scl "));
1345 }
1346
1347 static void
1348 ahci_device_isoc_done(usbd_xfer_handle xfer)
1349 {
1350 DPRINTF(D_TRACE, ("Sdn "));
1351 }
1352
1353 static usbd_status
1354 ahci_device_bulk_transfer(usbd_xfer_handle xfer)
1355 {
1356 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1357 usbd_status error;
1358
1359 DPRINTF(D_TRACE, ("B"));
1360
1361 mutex_enter(&sc->sc_lock);
1362 error = usb_insert_transfer(xfer);
1363 mutex_exit(&sc->sc_lock);
1364 if (error)
1365 return error;
1366
1367 return ahci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1368 }
1369
1370 static usbd_status
1371 ahci_device_bulk_start(usbd_xfer_handle xfer)
1372 {
1373 #define NBULK_TDS 32
1374 static volatile int level = 0;
1375 usbd_status status = USBD_NORMAL_COMPLETION;
1376 int s, err;
1377 static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
1378 static struct admhcd_td td_v[NBULK_TDS] __attribute__((aligned(16))), *td[NBULK_TDS];
1379 usbd_pipe_handle pipe = xfer->ux_pipe;
1380 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1381 int endpt, i, len, tlen, segs, offset, isread, toggle, short_ok;
1382 struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
1383
1384 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
1385 DPRINTF(D_TRACE, ("st "));
1386
1387 #ifdef DIAGNOSTIC
1388 if (xfer->ux_rqflags & URQ_REQUEST) {
1389 /* XXX panic */
1390 printf("ohci_device_bulk_start: a request\n");
1391 return (USBD_INVAL);
1392 }
1393 #endif
1394
1395 mutex_enter(&sc->sc_lock);
1396 level++;
1397 /* printf("bulk_start>>>\n"); */
1398
1399 if (!ep) {
1400 ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
1401 for (i=0; i<NBULK_TDS; i++) {
1402 td[i] = (struct admhcd_td *)KSEG1ADDR(&td_v[i]);
1403 };
1404 /* printf("ep: %p\n",ep);*/
1405 };
1406 if (apipe->toggle == 0) {
1407 toggle = ADMHCD_TD_DATA0;
1408 } else {
1409 toggle = apipe->toggle;
1410 };
1411
1412 endpt = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1413 ep->control = pipe->up_dev->ud_addr | ((endpt & 0xf) << ADMHCD_ED_EPSHIFT)|\
1414 ((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
1415 ((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
1416
1417 short_ok = xfer->ux_flags & USBD_SHORT_XFER_OK?ADMHCD_TD_R:0;
1418 /* printf("level: %d\n",level);
1419 printf("short_xfer: %x\n",short_ok);
1420 printf("ep_control: %x\n",ep->control);
1421 printf("speed: %x\n",pipe->up_dev->ud_speed);
1422 printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
1423
1424 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1425 len = xfer->ux_length;
1426
1427 ep->next = ep;
1428
1429 i = 0;
1430 offset = 0;
1431 while ((len>0) || (i==0)) {
1432 tlen = min(len,4096);
1433 td[i]->buffer = DMAADDR(&xfer->ux_dmabuf,offset) | 0xa0000000;
1434 td[i]->buflen=tlen;
1435 td[i]->control=(isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | toggle | ADMHCD_TD_OWN | short_ok;
1436 td[i]->len=tlen;
1437 toggle = ADMHCD_TD_TOGGLE;
1438 len -= tlen;
1439 offset += tlen;
1440 td[i]->next = td[i+1];
1441 i++;
1442 };
1443
1444 td[i]->buffer = 0;
1445 td[i]->buflen = 0;
1446 td[i]->control = 0;
1447 td[i]->next = 0;
1448
1449 ep->head = td[0];
1450 ep->tail = td[i];
1451 segs = i;
1452 len = 0;
1453
1454 /* printf("segs: %d\n",segs);
1455 printf("ep: %p\n",ep);
1456 printf("ep->control: %x\n",ep->control);
1457 printf("ep->next: %p\n",ep->next);
1458 printf("ep->head: %p\n",ep->head);
1459 printf("ep->tail: %p\n",ep->tail);
1460 for (i=0; i<segs; i++) {
1461 printf("td[%d]: %p\n",i,td[i]);
1462 printf("td[%d]->control: %x\n",i,td[i]->control);
1463 printf("td[%d]->next: %p\n",i,td[i]->next);
1464 printf("td[%d]->buffer: %x\n",i,td[i]->buffer);
1465 printf("td[%d]->buflen: %x\n",i,td[i]->buflen);
1466 }; */
1467
1468 REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
1469 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
1470 i = 0;
1471 /* printf("1: %x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1472 s=100;
1473 err = 0;
1474 while (s--) {
1475 /* printf("%x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1476 status = USBD_TIMEOUT;
1477 if (td[i]->control & ADMHCD_TD_OWN) {
1478 delay_ms(3);
1479 continue;
1480 };
1481
1482 len += td[i]->len - td[i]->buflen;
1483
1484 err = (td[i]->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1485 if (err) {
1486 status = USBD_IOERROR;
1487 break;
1488 };
1489
1490 i++;
1491 if (i==segs) {
1492 status = USBD_NORMAL_COMPLETION;
1493 break;
1494 };
1495
1496 };
1497 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
1498
1499 apipe->toggle = ((uint32_t)ep->head & 2)?ADMHCD_TD_DATA1:ADMHCD_TD_DATA0;
1500 /* printf("bulk_transfer_done: status: %x, err: %x, len: %x, toggle: %x\n", status,err,len,apipe->toggle); */
1501
1502 if (short_ok && (err == 0x9 || err == 0xd)) {
1503 /* printf("bulk_transfer_done: short_transfer fix\n"); */
1504 status = USBD_NORMAL_COMPLETION;
1505 };
1506 xfer->ux_actlen = len;
1507 xfer->ux_status = status;
1508
1509 level--;
1510 /* printf("bulk_start<<<\n"); */
1511
1512 usb_transfer_complete(xfer);
1513 mutex_exit(&sc->sc_lock);
1514
1515 return USBD_NORMAL_COMPLETION;
1516 }
1517
1518 static void
1519 ahci_device_bulk_abort(usbd_xfer_handle xfer)
1520 {
1521 DPRINTF(D_TRACE, ("Bab "));
1522 ahci_abort_xfer(xfer, USBD_CANCELLED);
1523 }
1524
1525 static void
1526 ahci_device_bulk_close(usbd_pipe_handle pipe)
1527 {
1528 DPRINTF(D_TRACE, ("Bcl "));
1529 }
1530
1531 static void
1532 ahci_device_bulk_done(usbd_xfer_handle xfer)
1533 {
1534 DPRINTF(D_TRACE, ("Bdn "));
1535 }
1536
1537 #define DATA0_RD (0x03)
1538 #define DATA0_WR (0x07)
1539 #define AHCI_TIMEOUT (5000)
1540
1541 /*
1542 * Do a transaction.
1543 * return 1 if ACK, 0 if NAK, -1 if error.
1544 */
1545 static int
1546 ahci_transaction(struct ahci_softc *sc, usbd_pipe_handle pipe,
1547 uint8_t pid, int len, u_char *buf, uint8_t toggle)
1548 {
1549 return -1;
1550 #if 0
1551 #ifdef AHCI_DEBUG
1552 char str[64];
1553 int i;
1554 #endif
1555 int timeout;
1556 int ls_via_hub = 0;
1557 int pl;
1558 uint8_t isr;
1559 uint8_t result = 0;
1560 uint8_t devaddr = pipe->up_dev->ud_addr;
1561 uint8_t endpointaddr = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1562 uint8_t endpoint;
1563 uint8_t cmd = DATA0_RD;
1564
1565 endpoint = UE_GET_ADDR(endpointaddr);
1566 DPRINTF(D_XFER, ("\n(%x,%d%s%d,%d) ",
1567 pid, len, (pid == SL11_PID_IN) ? "<-" : "->", devaddr, endpoint));
1568
1569 /* Set registers */
1570 sl11write(sc, SL11_E0ADDR, 0x40);
1571 sl11write(sc, SL11_E0LEN, len);
1572 sl11write(sc, SL11_E0PID, (pid << 4) + endpoint);
1573 sl11write(sc, SL11_E0DEV, devaddr);
1574
1575 /* Set buffer unless PID_IN */
1576 if (pid != SL11_PID_IN) {
1577 if (len > 0)
1578 sl11write_region(sc, 0x40, buf, len);
1579 cmd = DATA0_WR;
1580 }
1581
1582 /* timing ? */
1583 pl = (len >> 3) + 3;
1584
1585 /* Low speed device via HUB */
1586 /* XXX does not work... */
1587 if ((sc->sc_fullspeed) && pipe->up_dev->ud_speed == USB_SPEED_LOW) {
1588 pl = len + 16;
1589 cmd |= SL11_EPCTRL_PREAMBLE;
1590
1591 /*
1592 * SL811HS/T rev 1.2 has a bug, when it got PID_IN
1593 * from LowSpeed device via HUB.
1594 */
1595 if (sc->sc_sltype == SLTYPE_SL811HS_R12 && pid == SL11_PID_IN) {
1596 ls_via_hub = 1;
1597 DPRINTF(D_MSG, ("LSvH "));
1598 }
1599 }
1600
1601 /* timing ? */
1602 if (sl11read(sc, SL811_CSOF) <= (uint8_t)pl)
1603 cmd |= SL11_EPCTRL_SOF;
1604
1605 /* Transfer */
1606 sl11write(sc, SL11_ISR, 0xff);
1607 sl11write(sc, SL11_E0CTRL, cmd | toggle);
1608
1609 /* Polling */
1610 for (timeout = AHCI_TIMEOUT; timeout; timeout--) {
1611 isr = sl11read(sc, SL11_ISR);
1612 if ((isr & SL11_ISR_USBA))
1613 break;
1614 }
1615
1616 /* Check result status */
1617 result = sl11read(sc, SL11_E0STAT);
1618 if (!(result & SL11_EPSTAT_NAK) && ls_via_hub) {
1619 /* Resend PID_IN within 20usec */
1620 sl11write(sc, SL11_ISR, 0xff);
1621 sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
1622 }
1623
1624 sl11write(sc, SL11_ISR, 0xff);
1625
1626 DPRINTF(D_XFER, ("t=%d i=%x ", AHCI_TIMEOUT - timeout, isr));
1627 #if AHCI_DEBUG
1628 snprintb(str, sizeof(str),
1629 "\20\x8STALL\7NAK\6OV\5SETUP\4DATA1\3TIMEOUT\2ERR\1ACK", result);
1630 DPRINTF(D_XFER, ("STAT=%s ", str));
1631 #endif
1632
1633 if ((result & SL11_EPSTAT_ERROR))
1634 return -1;
1635
1636 if ((result & SL11_EPSTAT_NAK))
1637 return 0;
1638
1639 /* Read buffer if PID_IN */
1640 if (pid == SL11_PID_IN && len > 0) {
1641 sl11read_region(sc, buf, 0x40, len);
1642 #if AHCI_DEBUG
1643 for (i = 0; i < len; i++)
1644 DPRINTF(D_XFER, ("%02X ", buf[i]));
1645 #endif
1646 }
1647
1648 return 1;
1649 #endif
1650 }
1651
1652 void
1653 ahci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1654 {
1655 xfer->ux_status = status;
1656 usb_transfer_complete(xfer);
1657 }
1658
1659 void
1660 ahci_device_clear_toggle(usbd_pipe_handle pipe)
1661 {
1662 struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
1663 apipe->toggle = 0;
1664 }
1665
1666 #ifdef AHCI_DEBUG
1667 void
1668 print_req(usb_device_request_t *r)
1669 {
1670 const char *xmes[]={
1671 "GETSTAT",
1672 "CLRFEAT",
1673 "res",
1674 "SETFEAT",
1675 "res",
1676 "SETADDR",
1677 "GETDESC",
1678 "SETDESC",
1679 "GETCONF",
1680 "SETCONF",
1681 "GETIN/F",
1682 "SETIN/F",
1683 "SYNC_FR"
1684 };
1685 int req, type, value, index, len;
1686
1687 req = r->bRequest;
1688 type = r->bmRequestType;
1689 value = UGETW(r->wValue);
1690 index = UGETW(r->wIndex);
1691 len = UGETW(r->wLength);
1692
1693 printf("%x,%s,v=%d,i=%d,l=%d ",
1694 type, xmes[req], value, index, len);
1695 }
1696
1697 void
1698 print_req_hub(usb_device_request_t *r)
1699 {
1700 struct {
1701 int req;
1702 int type;
1703 const char *str;
1704 } conf[] = {
1705 { 1, 0x20, "ClrHubFeat" },
1706 { 1, 0x23, "ClrPortFeat" },
1707 { 2, 0xa3, "GetBusState" },
1708 { 6, 0xa0, "GetHubDesc" },
1709 { 0, 0xa0, "GetHubStat" },
1710 { 0, 0xa3, "GetPortStat" },
1711 { 7, 0x20, "SetHubDesc" },
1712 { 3, 0x20, "SetHubFeat" },
1713 { 3, 0x23, "SetPortFeat" },
1714 {-1, 0, NULL},
1715 };
1716 int i;
1717 int value, index, len;
1718
1719 value = UGETW(r->wValue);
1720 index = UGETW(r->wIndex);
1721 len = UGETW(r->wLength);
1722 for (i = 0; ; i++) {
1723 if (conf[i].req == -1 )
1724 return print_req(r);
1725 if (r->bmRequestType == conf[i].type && r->bRequest == conf[i].req) {
1726 printf("%s", conf[i].str);
1727 break;
1728 }
1729 }
1730 printf(",v=%d,i=%d,l=%d ", value, index, len);
1731 }
1732
1733 void
1734 print_dumpreg(struct ahci_softc *sc)
1735 {
1736 #if 0
1737 printf("00=%02x,01=%02x,02=%02x,03=%02x,04=%02x,"
1738 "08=%02x,09=%02x,0A=%02x,0B=%02x,0C=%02x,",
1739 sl11read(sc, 0), sl11read(sc, 1),
1740 sl11read(sc, 2), sl11read(sc, 3),
1741 sl11read(sc, 4), sl11read(sc, 8),
1742 sl11read(sc, 9), sl11read(sc, 10),
1743 sl11read(sc, 11), sl11read(sc, 12)
1744 );
1745 printf("CR1=%02x,IER=%02x,0D=%02x,0E=%02x,0F=%02x ",
1746 sl11read(sc, 5), sl11read(sc, 6),
1747 sl11read(sc, 13), sl11read(sc, 14), sl11read(sc, 15)
1748 );
1749 #endif
1750 }
1751
1752 void
1753 print_xfer(usbd_xfer_handle xfer)
1754 {
1755 printf("xfer: length=%d, actlen=%d, flags=%x, timeout=%d,",
1756 xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
1757 printf("request{ ");
1758 print_req_hub(&xfer->ux_request);
1759 printf("} ");
1760 }
1761 #endif /* AHCI_DEBUG */
1762