ahci.c revision 1.12.6.9 1 /* $NetBSD: ahci.c,v 1.12.6.9 2014/12/03 22:40:54 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.9 2014/12/03 22:40:54 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
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 = kmem_alloc(sizeof(*xfer), KM_SLEEP);
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 .bLength = USB_DEVICE_DESCRIPTOR_SIZE,
536 .bDescriptorType = UDESC_DEVICE,
537 .bcdUSB = {0x01, 0x01},
538 .bDeviceClass = UDCLASS_HUB,
539 .bDeviceSubClass = UDSUBCLASS_HUB,
540 .bDeviceProtocol = 0,
541 .bMaxPacketSize = 64,
542 .idVendor = {
543 USB_VENDOR_SCANLOGIC & 0xff,
544 USB_VENDOR_SCANLOGIC >> 8
545 },
546 .idProduct = {0},
547 .bcdDevice = {0},
548 .iManufacturer = 1,
549 .iProduct = 2,
550 .iSerialNumber = 0,
551 .bNumConfigurations = 1
552 };
553
554 usb_config_descriptor_t ahci_confd = {
555 .bLength = USB_CONFIG_DESCRIPTOR_SIZE,
556 .bDescriptorType = UDESC_CONFIG,
557 .wTotalLength = USETWD(
558 USB_CONFIG_DESCRIPTOR_SIZE +
559 USB_INTERFACE_DESCRIPTOR_SIZE +
560 USB_ENDPOINT_DESCRIPTOR_SIZE),
561 .bNumInterface = 1,
562 .bConfigurationValue = 1,
563 .iConfiguration = 0,
564 .bmAttributes = UC_SELF_POWERED,
565 .bMaxPower = 250
566 };
567
568 usb_interface_descriptor_t ahci_ifcd = {
569 .bLength = USB_INTERFACE_DESCRIPTOR_SIZE,
570 .bDescriptorType = UDESC_INTERFACE,
571 .bInterfaceNumber = 0,
572 .bAlternateSetting = 0,
573 .bNumEndpoints = 1,
574 .bInterfaceClass = UICLASS_HUB,
575 .bInterfaceSubClass = UISUBCLASS_HUB,
576 .bInterfaceProtocol = 0,
577 .iInterface = 0
578 };
579
580 usb_endpoint_descriptor_t ahci_endpd = {
581 .bLength = USB_ENDPOINT_DESCRIPTOR_SIZE,
582 .bDescriptorType = UDESC_ENDPOINT,
583 .bEndpointAddress = UE_DIR_IN | AHCI_INTR_ENDPT,
584 .bmAttributes = UE_INTERRUPT,
585 .wMaxPacketSize = USETWD(8),
586 .bInterval = 255
587 };
588
589 usb_hub_descriptor_t ahci_hubd = {
590 .bDescLength = USB_HUB_DESCRIPTOR_SIZE,
591 .bDescriptorType = UDESC_HUB,
592 .bNbrPorts = 2,
593 .wHubCharacteristics = USETWD(0),
594 .bPwrOn2PwrGood = 0,
595 .bHubContrCurrent = 0,
596 .DeviceRemovable = { 0x00 },
597 .PortPowerCtrlMask = { 0x00 }
598 };
599
600 static int
601 ahci_str(usb_string_descriptor_t *p, int l, const char *s)
602 {
603 int i;
604
605 if (l == 0)
606 return 0;
607 p->bLength = 2 * strlen(s) + 2;
608 if (l == 1)
609 return 1;
610 p->bDescriptorType = UDESC_STRING;
611 l -= 2;
612 for (i = 0; s[i] && l > 1; i++, l -= 2)
613 USETW2(p->bString[i], 0, s[i]);
614 return 2 * i + 2;
615 }
616
617 usbd_status
618 ahci_root_ctrl_transfer(usbd_xfer_handle xfer)
619 {
620 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
621 usbd_status error;
622
623 DPRINTF(D_TRACE, ("SLRCtrans "));
624
625 /* Insert last in queue */
626 mutex_enter(&sc->sc_lock);
627 error = usb_insert_transfer(xfer);
628 mutex_exit(&sc->sc_lock);
629 if (error) {
630 DPRINTF(D_MSG, ("usb_insert_transfer returns err! "));
631 return error;
632 }
633
634 /*
635 * Pipe isn't running (otherwise error would be USBD_INPROG),
636 * so start it first.
637 */
638 return ahci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
639 }
640
641 usbd_status
642 ahci_root_ctrl_start(usbd_xfer_handle xfer)
643 {
644 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
645 usb_device_request_t *req;
646 int len, value, index, l, status;
647 int totlen = 0;
648 void *buf = NULL;
649 usb_port_status_t ps;
650 usbd_status error;
651
652
653 DPRINTF(D_TRACE, ("SLRCstart "));
654
655 req = &xfer->ux_request;
656
657 len = UGETW(req->wLength);
658 value = UGETW(req->wValue);
659 index = UGETW(req->wIndex);
660
661 if (len)
662 buf = KERNADDR(&xfer->ux_dmabuf, 0);
663
664 #ifdef AHCI_DEBUG
665 if ((ahci_debug & D_TRACE))
666 print_req_hub(req);
667 #endif
668
669 #define C(x,y) ((x) | ((y) << 8))
670 switch (C(req->bRequest, req->bmRequestType)) {
671 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
672 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(DEVICE)XXX "));
673 break;
674 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
675 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(INTERFACE)XXX "));
676 break;
677 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
678 DPRINTF(D_MSG, ("UR_CLEAR_FEATURE(ENDPOINT)XXX "));
679 break;
680 case C(UR_GET_CONFIG, UT_READ_DEVICE):
681 DPRINTF(D_MSG, ("UR_GET_CONFIG "));
682 if (len > 0) {
683 *(uint8_t *)buf = sc->sc_conf;
684 totlen = 1;
685 }
686 break;
687 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
688 switch (value >> 8) {
689 case UDESC_DEVICE:
690 DPRINTF(D_MSG, ("UDESC_DEVICE "));
691 if ((value & 0xff) != 0) {
692 error = USBD_IOERROR;
693 goto ret;
694 }
695 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
696 memcpy(buf, &ahci_devd, l);
697 break;
698 case UDESC_CONFIG:
699 DPRINTF(D_MSG, ("UDESC_CONFIG "));
700 if ((value & 0xff) != 0) {
701 error = USBD_IOERROR;
702 goto ret;
703 }
704 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
705 memcpy(buf, &ahci_confd, l);
706 buf = (char *)buf + l;
707 len -= l;
708
709 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
710 totlen += l;
711 memcpy(buf, &ahci_ifcd, l);
712 buf = (char *)buf + l;
713 len -= l;
714
715 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
716 totlen += l;
717 memcpy(buf, &ahci_endpd, l);
718 break;
719 case UDESC_STRING:
720 DPRINTF(D_MSG, ("UDESC_STR "));
721 if (len == 0)
722 break;
723 *(uint8_t *)buf = 0;
724 totlen = 1;
725 switch (value & 0xff) {
726 case 0:
727 break;
728 case 1: /* Vendor */
729 totlen = ahci_str(buf, len, "ADMTek");
730 break;
731 case 2: /* Product */
732 totlen = ahci_str(buf, len, "ADM5120 root hub");
733 break;
734 default:
735 printf("strerr%d ", value & 0xff);
736 break;
737 }
738 break;
739 default:
740 printf("unknownGetDescriptor=%x", value);
741 error = USBD_IOERROR;
742 break;
743 }
744 break;
745 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
746 /* Get Interface, 9.4.4 */
747 if (len > 0) {
748 *(uint8_t *)buf = 0;
749 totlen = 1;
750 }
751 break;
752 case C(UR_GET_STATUS, UT_READ_DEVICE):
753 /* Get Status from device, 9.4.5 */
754 if (len > 1) {
755 USETW(((usb_status_t *)buf)->wStatus, UDS_SELF_POWERED);
756 totlen = 2;
757 }
758 break;
759 case C(UR_GET_STATUS, UT_READ_INTERFACE):
760 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
761 /* Get Status from interface, endpoint, 9.4.5 */
762 if (len > 1) {
763 USETW(((usb_status_t *)buf)->wStatus, 0);
764 totlen = 2;
765 }
766 break;
767 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
768 /* Set Address, 9.4.6 */
769 DPRINTF(D_MSG, ("UR_SET_ADDRESS "));
770 if (value >= USB_MAX_DEVICES) {
771 error = USBD_IOERROR;
772 goto ret;
773 }
774 sc->sc_addr = value;
775 break;
776 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
777 /* Set Configuration, 9.4.7 */
778 DPRINTF(D_MSG, ("UR_SET_CONFIG "));
779 if (value != 0 && value != 1) {
780 error = USBD_IOERROR;
781 goto ret;
782 }
783 sc->sc_conf = value;
784 break;
785 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
786 /* Set Descriptor, 9.4.8, not supported */
787 DPRINTF(D_MSG, ("UR_SET_DESCRIPTOR,WRITE_DEVICE not supported\n"));
788 break;
789 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
790 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
791 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
792 /* Set Feature, 9.4.9, not supported */
793 DPRINTF(D_MSG, ("UR_SET_FEATURE not supported\n"));
794 error = USBD_IOERROR;
795 break;
796 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
797 /* Set Interface, 9.4.10, not supported */
798 break;
799 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
800 /* Synch Frame, 9.4.11, not supported */
801 break;
802
803 /*
804 * Hub specific requests
805 */
806 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
807 /* Clear Hub Feature, 11.16.2.1, not supported */
808 DPRINTF(D_MSG, ("ClearHubFeature not supported\n"));
809 break;
810 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
811
812 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
813 /* Clear Port Feature, 11.16.2.2 */
814 if (index != 1 && index != 2 ) {
815 error = USBD_IOERROR;
816 goto ret;
817 }
818 switch (value) {
819 case UHF_PORT_POWER:
820 DPRINTF(D_MSG, ("POWER_OFF "));
821 WPS(ADMHCD_LSDA);
822 break;
823 case UHF_PORT_SUSPEND:
824 DPRINTF(D_MSG, ("SUSPEND "));
825 WPS(ADMHCD_POCI);
826 break;
827 case UHF_PORT_ENABLE:
828 DPRINTF(D_MSG, ("ENABLE "));
829 WPS(ADMHCD_CCS);
830 break;
831 case UHF_C_PORT_CONNECTION:
832 WPS(ADMHCD_CSC);
833 break;
834 case UHF_C_PORT_RESET:
835 WPS(ADMHCD_PRSC);
836 break;
837 case UHF_C_PORT_SUSPEND:
838 WPS(ADMHCD_PSSC);
839 break;
840 case UHF_C_PORT_ENABLE:
841 WPS(ADMHCD_PESC);
842 break;
843 case UHF_C_PORT_OVER_CURRENT:
844 WPS(ADMHCD_OCIC);
845 break;
846 default:
847 printf("ClrPortFeatERR:value=0x%x ", value);
848 error = USBD_IOERROR;
849 break;
850 }
851 //DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change));
852 #undef WPS
853 break;
854 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
855 /* Get Bus State, 11.16.2.3, not supported */
856 /* shall return a STALL... */
857 break;
858 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
859 /* Get Hub Descriptor, 11.16.2.4 */
860 DPRINTF(D_MSG, ("UR_GET_DESCRIPTOR RCD"));
861 if ((value&0xff) != 0) {
862 error = USBD_IOERROR;
863 goto ret;
864 }
865 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
866 totlen = l;
867 memcpy(buf, &ahci_hubd, l);
868 break;
869 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
870 /* Get Hub Status, 11.16.2.5 */
871 DPRINTF(D_MSG, ("UR_GET_STATUS RCD"));
872 if (len != 4) {
873 error = USBD_IOERROR;
874 goto ret;
875 }
876 memset(buf, 0, len);
877 totlen = len;
878 break;
879 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
880 /* Get Port Status, 11.16.2.6 */
881 if ((index != 1 && index != 2) || len != 4) {
882 printf("index=%d,len=%d ", index, len);
883 error = USBD_IOERROR;
884 goto ret;
885 }
886 status = REG_READ(ADMHCD_REG_PORTSTATUS0+(index-1)*4);
887 DPRINTF(D_MSG, ("UR_GET_STATUS RCO=%x ", status));
888
889 //DPRINTF(D_XFER, ("ST=%04x,CH=%04x ", status, sc->sc_change));
890 USETW(ps.wPortStatus, status & (UPS_CURRENT_CONNECT_STATUS|UPS_PORT_ENABLED|UPS_SUSPEND|UPS_OVERCURRENT_INDICATOR|UPS_RESET|UPS_PORT_POWER|UPS_LOW_SPEED));
891 USETW(ps.wPortChange, (status>>16) & (UPS_C_CONNECT_STATUS|UPS_C_PORT_ENABLED|UPS_C_SUSPEND|UPS_C_OVERCURRENT_INDICATOR|UPS_C_PORT_RESET));
892 l = min(len, sizeof(ps));
893 memcpy(buf, &ps, l);
894 totlen = l;
895 break;
896 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
897 /* Set Hub Descriptor, 11.16.2.7, not supported */
898 /* STALL ? */
899 error = USBD_IOERROR;
900 break;
901 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
902 /* Set Hub Feature, 11.16.2.8, not supported */
903 break;
904 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
905 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
906 /* Set Port Feature, 11.16.2.9 */
907 if ((index != 1) && (index !=2)) {
908 printf("index=%d ", index);
909 error = USBD_IOERROR;
910 goto ret;
911 }
912 switch (value) {
913 case UHF_PORT_RESET:
914 DPRINTF(D_MSG, ("PORT_RESET "));
915 WPS(ADMHCD_PRS);
916 break;
917 case UHF_PORT_POWER:
918 DPRINTF(D_MSG, ("PORT_POWER "));
919 WPS(ADMHCD_PPS);
920 break;
921 case UHF_PORT_ENABLE:
922 DPRINTF(D_MSG, ("PORT_ENABLE "));
923 WPS(ADMHCD_PES);
924 break;
925 default:
926 printf("SetPortFeatERR=0x%x ", value);
927 error = USBD_IOERROR;
928 break;
929 }
930 #undef WPS
931 break;
932 default:
933 DPRINTF(D_MSG, ("ioerr(UR=%02x,UT=%02x) ",
934 req->bRequest, req->bmRequestType));
935 error = USBD_IOERROR;
936 goto ret;
937 }
938 xfer->ux_actlen = totlen;
939 error = USBD_NORMAL_COMPLETION;
940 ret:
941 xfer->ux_status = error;
942 mutex_enter(&sc->sc_lock);
943 usb_transfer_complete(xfer);
944 mutex_exit(&sc->sc_lock);
945 return USBD_IN_PROGRESS;
946 }
947
948 void
949 ahci_root_ctrl_abort(usbd_xfer_handle xfer)
950 {
951 DPRINTF(D_TRACE, ("SLRCabort "));
952 }
953
954 void
955 ahci_root_ctrl_close(usbd_pipe_handle pipe)
956 {
957 DPRINTF(D_TRACE, ("SLRCclose "));
958 }
959
960 void
961 ahci_root_ctrl_done(usbd_xfer_handle xfer)
962 {
963 DPRINTF(D_TRACE, ("SLRCdone\n"));
964 }
965
966 static usbd_status
967 ahci_root_intr_transfer(usbd_xfer_handle xfer)
968 {
969 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
970 usbd_status error;
971
972 DPRINTF(D_TRACE, ("SLRItransfer "));
973
974 /* Insert last in queue */
975 mutex_enter(&sc->sc_lock);
976 error = usb_insert_transfer(xfer);
977 mutex_exit(&sc->sc_lock);
978 if (error)
979 return error;
980
981 /*
982 * Pipe isn't running (otherwise error would be USBD_INPROG),
983 * start first.
984 */
985 return ahci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
986 }
987
988 static usbd_status
989 ahci_root_intr_start(usbd_xfer_handle xfer)
990 {
991 usbd_pipe_handle pipe = xfer->ux_pipe;
992 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
993
994 DPRINTF(D_TRACE, ("SLRIstart "));
995
996 sc->sc_interval = MS_TO_TICKS(xfer->ux_pipe->up_endpoint->ue_edesc->bInterval);
997 callout_reset(&sc->sc_poll_handle, sc->sc_interval, ahci_poll_hub, xfer);
998 sc->sc_intr_xfer = xfer;
999 return USBD_IN_PROGRESS;
1000 }
1001
1002 static void
1003 ahci_root_intr_abort(usbd_xfer_handle xfer)
1004 {
1005 DPRINTF(D_TRACE, ("SLRIabort "));
1006 }
1007
1008 static void
1009 ahci_root_intr_close(usbd_pipe_handle pipe)
1010 {
1011 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1012
1013 DPRINTF(D_TRACE, ("SLRIclose "));
1014
1015 callout_stop(&sc->sc_poll_handle);
1016 sc->sc_intr_xfer = NULL;
1017 }
1018
1019 static void
1020 ahci_root_intr_done(usbd_xfer_handle xfer)
1021 {
1022 //DPRINTF(D_XFER, ("RIdn "));
1023 }
1024
1025 static usbd_status
1026 ahci_device_ctrl_transfer(usbd_xfer_handle xfer)
1027 {
1028 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1029 usbd_status error;
1030
1031 DPRINTF(D_TRACE, ("C"));
1032
1033 mutex_enter(&sc->sc_lock);
1034 error = usb_insert_transfer(xfer);
1035 mutex_exit(&sc->sc_lock);
1036 if (error)
1037 return error;
1038
1039 return ahci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1040 }
1041
1042 static usbd_status
1043 ahci_device_ctrl_start(usbd_xfer_handle xfer)
1044 {
1045 usbd_status status = USBD_NORMAL_COMPLETION;
1046 int s, err;
1047 static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
1048 static struct admhcd_td td_v[4] __attribute__((aligned(16))), *td, *td1, *td2, *td3;
1049 static usb_dma_t reqdma;
1050 usbd_pipe_handle pipe = xfer->ux_pipe;
1051 usb_device_request_t *req = &xfer->ux_request;
1052 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1053 int len, isread;
1054
1055
1056 #if 0
1057 struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
1058 #endif
1059 mutex_enter(&sc->sc_lock);
1060 /* printf("ctrl_start>>>\n"); */
1061
1062 #ifdef DIAGNOSTIC
1063 if (!(xfer->ux_rqflags & URQ_REQUEST)) {
1064 /* XXX panic */
1065 printf("ahci_device_ctrl_transfer: not a request\n");
1066 return (USBD_INVAL);
1067 }
1068 #endif
1069
1070 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
1071 DPRINTF(D_TRACE, ("st "));
1072 if (!ep) {
1073 ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
1074 td = (struct admhcd_td *)KSEG1ADDR(&td_v[0]);
1075 td1 = (struct admhcd_td *)KSEG1ADDR(&td_v[1]);
1076 td2 = (struct admhcd_td *)KSEG1ADDR(&td_v[2]);
1077 td3 = (struct admhcd_td *)KSEG1ADDR(&td_v[3]);
1078 err = usb_allocmem(&sc->sc_bus,
1079 sizeof(usb_device_request_t),
1080 0, &reqdma);
1081 if (err)
1082 return (USBD_NOMEM);
1083
1084 /* printf("ep: %p\n",ep); */
1085 };
1086
1087 ep->control = pipe->up_dev->ud_addr | \
1088 ((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
1089 ((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
1090 memcpy(KERNADDR(&reqdma, 0), req, sizeof *req);
1091 /* printf("status: %x\n",REG_READ(ADMHCD_REG_PORTSTATUS0));
1092 printf("ep_control: %x\n",ep->control);
1093 printf("speed: %x\n",pipe->up_dev->ud_speed);
1094 printf("req: %p\n",req);
1095 printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
1096
1097 isread = req->bmRequestType & UT_READ;
1098 len = UGETW(req->wLength);
1099
1100 ep->next = ep;
1101
1102 td->buffer = DMAADDR(&reqdma,0) | 0xa0000000;
1103 td->buflen=sizeof(*req);
1104 td->control=ADMHCD_TD_SETUP | ADMHCD_TD_DATA0 | ADMHCD_TD_OWN;
1105
1106 if (len) {
1107 td->next = td1;
1108
1109 td1->buffer = DMAADDR(&xfer->ux_dmabuf,0) | 0xa0000000;
1110 td1->buflen = len;
1111 td1->next = td2;
1112 td1->control= (isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | ADMHCD_TD_DATA1 | ADMHCD_TD_R | ADMHCD_TD_OWN;
1113 } else {
1114 td1->control = 0;
1115 td->next = td2;
1116 };
1117
1118 td2->buffer = 0;
1119 td2->buflen= 0;
1120 td2->next = td3;
1121 td2->control = (isread?ADMHCD_TD_OUT:ADMHCD_TD_IN) | ADMHCD_TD_DATA1 | ADMHCD_TD_OWN;
1122
1123 td3->buffer = 0;
1124 td3->buflen= 0;
1125 td3->next = 0;
1126 td3->control = 0;
1127
1128 ep->head = td;
1129 ep->tail = td3;
1130 /*
1131 printf("ep: %p\n",ep);
1132 printf("ep->next: %p\n",ep->next);
1133 printf("ep->head: %p\n",ep->head);
1134 printf("ep->tail: %p\n",ep->tail);
1135 printf("td: %p\n",td);
1136 printf("td->next: %p\n",td->next);
1137 printf("td->buffer: %x\n",td->buffer);
1138 printf("td->buflen: %x\n",td->buflen);
1139 printf("td1: %p\n",td1);
1140 printf("td1->next: %p\n",td1->next);
1141 printf("td2: %p\n",td2);
1142 printf("td2->next: %p\n",td2->next);
1143 printf("td3: %p\n",td3);
1144 printf("td3->next: %p\n",td3->next);
1145 */
1146
1147 REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
1148 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
1149 /* printf("1: %x %x %x %x\n", ep->control, td->control, td1->control, td2->control); */
1150 s=100;
1151 while (s--) {
1152 delay_ms(10);
1153 /* printf("%x %x %x %x\n", ep->control, td->control, td1->control, td2->control);*/
1154 status = USBD_TIMEOUT;
1155 if (td->control & ADMHCD_TD_OWN) continue;
1156
1157 err = (td->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1158 if (err) {
1159 status = USBD_IOERROR;
1160 break;
1161 };
1162
1163 status = USBD_TIMEOUT;
1164 if (td1->control & ADMHCD_TD_OWN) continue;
1165 err = (td1->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1166 if (err) {
1167 status = USBD_IOERROR;
1168 break;
1169 };
1170
1171 status = USBD_TIMEOUT;
1172 if (td2->control & ADMHCD_TD_OWN) continue;
1173 err = (td2->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1174 if (err) {
1175 status = USBD_IOERROR;
1176 };
1177 status = USBD_NORMAL_COMPLETION;
1178 break;
1179
1180 };
1181 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
1182
1183 xfer->ux_actlen = len;
1184 xfer->ux_status = status;
1185
1186 /* printf("ctrl_start<<<\n"); */
1187
1188 usb_transfer_complete(xfer);
1189 mutex_exit(&sc->sc_lock);
1190 return USBD_NORMAL_COMPLETION;
1191 }
1192
1193 static void
1194 ahci_device_ctrl_abort(usbd_xfer_handle xfer)
1195 {
1196 DPRINTF(D_TRACE, ("Cab "));
1197 ahci_abort_xfer(xfer, USBD_CANCELLED);
1198 }
1199
1200 static void
1201 ahci_device_ctrl_close(usbd_pipe_handle pipe)
1202 {
1203 DPRINTF(D_TRACE, ("Ccl "));
1204 }
1205
1206 static void
1207 ahci_device_ctrl_done(usbd_xfer_handle xfer)
1208 {
1209 DPRINTF(D_TRACE, ("Cdn "));
1210 }
1211
1212 static usbd_status
1213 ahci_device_intr_transfer(usbd_xfer_handle xfer)
1214 {
1215 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1216 usbd_status error;
1217
1218 DPRINTF(D_TRACE, ("INTRtrans "));
1219
1220 mutex_enter(&sc->sc_lock);
1221 error = usb_insert_transfer(xfer);
1222 mutex_exit(&sc->sc_lock);
1223 if (error)
1224 return error;
1225
1226 return ahci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1227 }
1228
1229 static usbd_status
1230 ahci_device_intr_start(usbd_xfer_handle xfer)
1231 {
1232 usbd_pipe_handle pipe = xfer->ux_pipe;
1233 struct ahci_xfer *sx;
1234
1235 DPRINTF(D_TRACE, ("INTRstart "));
1236
1237 sx = kmem_intr_alloc(sizeof(*sx), KM_NOSLEEP);
1238 if (sx == NULL)
1239 goto reterr;
1240 memset(sx, 0, sizeof(*sx));
1241 sx->sx_xfer = xfer;
1242 xfer->ux_hcpriv = sx;
1243
1244 /* initialize callout */
1245 callout_init(&sx->sx_callout_t, 0);
1246 callout_reset(&sx->sx_callout_t,
1247 MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1248 ahci_poll_device, sx);
1249
1250 /* ACK */
1251 return USBD_IN_PROGRESS;
1252
1253 reterr:
1254 return USBD_IOERROR;
1255 }
1256
1257 static void
1258 ahci_poll_device(void *arg)
1259 {
1260 struct ahci_xfer *sx = (struct ahci_xfer *)arg;
1261 usbd_xfer_handle xfer = sx->sx_xfer;
1262 usbd_pipe_handle pipe = xfer->ux_pipe;
1263 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1264 void *buf;
1265 int pid;
1266 int r;
1267
1268 DPRINTF(D_TRACE, ("pldev"));
1269
1270 callout_reset(&sx->sx_callout_t,
1271 MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
1272 ahci_poll_device, sx);
1273
1274 /* interrupt transfer */
1275 pid = (UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN)
1276 ? ADMHCD_TD_IN : ADMHCD_TD_OUT;
1277 buf = KERNADDR(&xfer->ux_dmabuf, 0);
1278
1279 r = ahci_transaction(sc, pipe, pid, xfer->ux_length, buf, 0/*toggle*/);
1280 if (r < 0) {
1281 DPRINTF(D_MSG, ("%s error", __func__));
1282 return;
1283 }
1284 /* no change, return NAK */
1285 if (r == 0)
1286 return;
1287
1288 xfer->ux_status = USBD_NORMAL_COMPLETION;
1289 mutex_enter(&sc->sc_lock);
1290 usb_transfer_complete(xfer);
1291 mutex_exit(&sc->sc_lock);
1292 }
1293
1294 static void
1295 ahci_device_intr_abort(usbd_xfer_handle xfer)
1296 {
1297 struct ahci_xfer *sx;
1298
1299 DPRINTF(D_TRACE, ("INTRabort "));
1300
1301 sx = xfer->ux_hcpriv;
1302 if (sx) {
1303 callout_stop(&sx->sx_callout_t);
1304 kmem_intr_free(sx, sizeof(*sx));
1305 xfer->ux_hcpriv = NULL;
1306 } else {
1307 printf("%s: sx == NULL!\n", __func__);
1308 }
1309 ahci_abort_xfer(xfer, USBD_CANCELLED);
1310 }
1311
1312 static void
1313 ahci_device_intr_close(usbd_pipe_handle pipe)
1314 {
1315 DPRINTF(D_TRACE, ("INTRclose "));
1316 }
1317
1318 static void
1319 ahci_device_intr_done(usbd_xfer_handle xfer)
1320 {
1321 DPRINTF(D_TRACE, ("INTRdone "));
1322 }
1323
1324 static usbd_status
1325 ahci_device_isoc_transfer(usbd_xfer_handle xfer)
1326 {
1327 DPRINTF(D_TRACE, ("S"));
1328 return USBD_NORMAL_COMPLETION;
1329 }
1330
1331 static usbd_status
1332 ahci_device_isoc_start(usbd_xfer_handle xfer)
1333 {
1334 DPRINTF(D_TRACE, ("st "));
1335 return USBD_NORMAL_COMPLETION;
1336 }
1337
1338 static void
1339 ahci_device_isoc_abort(usbd_xfer_handle xfer)
1340 {
1341 DPRINTF(D_TRACE, ("Sab "));
1342 }
1343
1344 static void
1345 ahci_device_isoc_close(usbd_pipe_handle pipe)
1346 {
1347 DPRINTF(D_TRACE, ("Scl "));
1348 }
1349
1350 static void
1351 ahci_device_isoc_done(usbd_xfer_handle xfer)
1352 {
1353 DPRINTF(D_TRACE, ("Sdn "));
1354 }
1355
1356 static usbd_status
1357 ahci_device_bulk_transfer(usbd_xfer_handle xfer)
1358 {
1359 struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
1360 usbd_status error;
1361
1362 DPRINTF(D_TRACE, ("B"));
1363
1364 mutex_enter(&sc->sc_lock);
1365 error = usb_insert_transfer(xfer);
1366 mutex_exit(&sc->sc_lock);
1367 if (error)
1368 return error;
1369
1370 return ahci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1371 }
1372
1373 static usbd_status
1374 ahci_device_bulk_start(usbd_xfer_handle xfer)
1375 {
1376 #define NBULK_TDS 32
1377 static volatile int level = 0;
1378 usbd_status status = USBD_NORMAL_COMPLETION;
1379 int s, err;
1380 static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
1381 static struct admhcd_td td_v[NBULK_TDS] __attribute__((aligned(16))), *td[NBULK_TDS];
1382 usbd_pipe_handle pipe = xfer->ux_pipe;
1383 struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
1384 int endpt, i, len, tlen, segs, offset, isread, toggle, short_ok;
1385 struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
1386
1387 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
1388 DPRINTF(D_TRACE, ("st "));
1389
1390 #ifdef DIAGNOSTIC
1391 if (xfer->ux_rqflags & URQ_REQUEST) {
1392 /* XXX panic */
1393 printf("ohci_device_bulk_start: a request\n");
1394 return (USBD_INVAL);
1395 }
1396 #endif
1397
1398 mutex_enter(&sc->sc_lock);
1399 level++;
1400 /* printf("bulk_start>>>\n"); */
1401
1402 if (!ep) {
1403 ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
1404 for (i=0; i<NBULK_TDS; i++) {
1405 td[i] = (struct admhcd_td *)KSEG1ADDR(&td_v[i]);
1406 };
1407 /* printf("ep: %p\n",ep);*/
1408 };
1409 if (apipe->toggle == 0) {
1410 toggle = ADMHCD_TD_DATA0;
1411 } else {
1412 toggle = apipe->toggle;
1413 };
1414
1415 endpt = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1416 ep->control = pipe->up_dev->ud_addr | ((endpt & 0xf) << ADMHCD_ED_EPSHIFT)|\
1417 ((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
1418 ((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
1419
1420 short_ok = xfer->ux_flags & USBD_SHORT_XFER_OK?ADMHCD_TD_R:0;
1421 /* printf("level: %d\n",level);
1422 printf("short_xfer: %x\n",short_ok);
1423 printf("ep_control: %x\n",ep->control);
1424 printf("speed: %x\n",pipe->up_dev->ud_speed);
1425 printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
1426
1427 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1428 len = xfer->ux_length;
1429
1430 ep->next = ep;
1431
1432 i = 0;
1433 offset = 0;
1434 while ((len>0) || (i==0)) {
1435 tlen = min(len,4096);
1436 td[i]->buffer = DMAADDR(&xfer->ux_dmabuf,offset) | 0xa0000000;
1437 td[i]->buflen=tlen;
1438 td[i]->control=(isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | toggle | ADMHCD_TD_OWN | short_ok;
1439 td[i]->len=tlen;
1440 toggle = ADMHCD_TD_TOGGLE;
1441 len -= tlen;
1442 offset += tlen;
1443 td[i]->next = td[i+1];
1444 i++;
1445 };
1446
1447 td[i]->buffer = 0;
1448 td[i]->buflen = 0;
1449 td[i]->control = 0;
1450 td[i]->next = 0;
1451
1452 ep->head = td[0];
1453 ep->tail = td[i];
1454 segs = i;
1455 len = 0;
1456
1457 /* printf("segs: %d\n",segs);
1458 printf("ep: %p\n",ep);
1459 printf("ep->control: %x\n",ep->control);
1460 printf("ep->next: %p\n",ep->next);
1461 printf("ep->head: %p\n",ep->head);
1462 printf("ep->tail: %p\n",ep->tail);
1463 for (i=0; i<segs; i++) {
1464 printf("td[%d]: %p\n",i,td[i]);
1465 printf("td[%d]->control: %x\n",i,td[i]->control);
1466 printf("td[%d]->next: %p\n",i,td[i]->next);
1467 printf("td[%d]->buffer: %x\n",i,td[i]->buffer);
1468 printf("td[%d]->buflen: %x\n",i,td[i]->buflen);
1469 }; */
1470
1471 REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
1472 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
1473 i = 0;
1474 /* printf("1: %x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1475 s=100;
1476 err = 0;
1477 while (s--) {
1478 /* printf("%x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
1479 status = USBD_TIMEOUT;
1480 if (td[i]->control & ADMHCD_TD_OWN) {
1481 delay_ms(3);
1482 continue;
1483 };
1484
1485 len += td[i]->len - td[i]->buflen;
1486
1487 err = (td[i]->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
1488 if (err) {
1489 status = USBD_IOERROR;
1490 break;
1491 };
1492
1493 i++;
1494 if (i==segs) {
1495 status = USBD_NORMAL_COMPLETION;
1496 break;
1497 };
1498
1499 };
1500 REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
1501
1502 apipe->toggle = ((uint32_t)ep->head & 2)?ADMHCD_TD_DATA1:ADMHCD_TD_DATA0;
1503 /* printf("bulk_transfer_done: status: %x, err: %x, len: %x, toggle: %x\n", status,err,len,apipe->toggle); */
1504
1505 if (short_ok && (err == 0x9 || err == 0xd)) {
1506 /* printf("bulk_transfer_done: short_transfer fix\n"); */
1507 status = USBD_NORMAL_COMPLETION;
1508 };
1509 xfer->ux_actlen = len;
1510 xfer->ux_status = status;
1511
1512 level--;
1513 /* printf("bulk_start<<<\n"); */
1514
1515 usb_transfer_complete(xfer);
1516 mutex_exit(&sc->sc_lock);
1517
1518 return USBD_NORMAL_COMPLETION;
1519 }
1520
1521 static void
1522 ahci_device_bulk_abort(usbd_xfer_handle xfer)
1523 {
1524 DPRINTF(D_TRACE, ("Bab "));
1525 ahci_abort_xfer(xfer, USBD_CANCELLED);
1526 }
1527
1528 static void
1529 ahci_device_bulk_close(usbd_pipe_handle pipe)
1530 {
1531 DPRINTF(D_TRACE, ("Bcl "));
1532 }
1533
1534 static void
1535 ahci_device_bulk_done(usbd_xfer_handle xfer)
1536 {
1537 DPRINTF(D_TRACE, ("Bdn "));
1538 }
1539
1540 #define DATA0_RD (0x03)
1541 #define DATA0_WR (0x07)
1542 #define AHCI_TIMEOUT (5000)
1543
1544 /*
1545 * Do a transaction.
1546 * return 1 if ACK, 0 if NAK, -1 if error.
1547 */
1548 static int
1549 ahci_transaction(struct ahci_softc *sc, usbd_pipe_handle pipe,
1550 uint8_t pid, int len, u_char *buf, uint8_t toggle)
1551 {
1552 return -1;
1553 #if 0
1554 #ifdef AHCI_DEBUG
1555 char str[64];
1556 int i;
1557 #endif
1558 int timeout;
1559 int ls_via_hub = 0;
1560 int pl;
1561 uint8_t isr;
1562 uint8_t result = 0;
1563 uint8_t devaddr = pipe->up_dev->ud_addr;
1564 uint8_t endpointaddr = pipe->up_endpoint->ue_edesc->bEndpointAddress;
1565 uint8_t endpoint;
1566 uint8_t cmd = DATA0_RD;
1567
1568 endpoint = UE_GET_ADDR(endpointaddr);
1569 DPRINTF(D_XFER, ("\n(%x,%d%s%d,%d) ",
1570 pid, len, (pid == SL11_PID_IN) ? "<-" : "->", devaddr, endpoint));
1571
1572 /* Set registers */
1573 sl11write(sc, SL11_E0ADDR, 0x40);
1574 sl11write(sc, SL11_E0LEN, len);
1575 sl11write(sc, SL11_E0PID, (pid << 4) + endpoint);
1576 sl11write(sc, SL11_E0DEV, devaddr);
1577
1578 /* Set buffer unless PID_IN */
1579 if (pid != SL11_PID_IN) {
1580 if (len > 0)
1581 sl11write_region(sc, 0x40, buf, len);
1582 cmd = DATA0_WR;
1583 }
1584
1585 /* timing ? */
1586 pl = (len >> 3) + 3;
1587
1588 /* Low speed device via HUB */
1589 /* XXX does not work... */
1590 if ((sc->sc_fullspeed) && pipe->up_dev->ud_speed == USB_SPEED_LOW) {
1591 pl = len + 16;
1592 cmd |= SL11_EPCTRL_PREAMBLE;
1593
1594 /*
1595 * SL811HS/T rev 1.2 has a bug, when it got PID_IN
1596 * from LowSpeed device via HUB.
1597 */
1598 if (sc->sc_sltype == SLTYPE_SL811HS_R12 && pid == SL11_PID_IN) {
1599 ls_via_hub = 1;
1600 DPRINTF(D_MSG, ("LSvH "));
1601 }
1602 }
1603
1604 /* timing ? */
1605 if (sl11read(sc, SL811_CSOF) <= (uint8_t)pl)
1606 cmd |= SL11_EPCTRL_SOF;
1607
1608 /* Transfer */
1609 sl11write(sc, SL11_ISR, 0xff);
1610 sl11write(sc, SL11_E0CTRL, cmd | toggle);
1611
1612 /* Polling */
1613 for (timeout = AHCI_TIMEOUT; timeout; timeout--) {
1614 isr = sl11read(sc, SL11_ISR);
1615 if ((isr & SL11_ISR_USBA))
1616 break;
1617 }
1618
1619 /* Check result status */
1620 result = sl11read(sc, SL11_E0STAT);
1621 if (!(result & SL11_EPSTAT_NAK) && ls_via_hub) {
1622 /* Resend PID_IN within 20usec */
1623 sl11write(sc, SL11_ISR, 0xff);
1624 sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
1625 }
1626
1627 sl11write(sc, SL11_ISR, 0xff);
1628
1629 DPRINTF(D_XFER, ("t=%d i=%x ", AHCI_TIMEOUT - timeout, isr));
1630 #if AHCI_DEBUG
1631 snprintb(str, sizeof(str),
1632 "\20\x8STALL\7NAK\6OV\5SETUP\4DATA1\3TIMEOUT\2ERR\1ACK", result);
1633 DPRINTF(D_XFER, ("STAT=%s ", str));
1634 #endif
1635
1636 if ((result & SL11_EPSTAT_ERROR))
1637 return -1;
1638
1639 if ((result & SL11_EPSTAT_NAK))
1640 return 0;
1641
1642 /* Read buffer if PID_IN */
1643 if (pid == SL11_PID_IN && len > 0) {
1644 sl11read_region(sc, buf, 0x40, len);
1645 #if AHCI_DEBUG
1646 for (i = 0; i < len; i++)
1647 DPRINTF(D_XFER, ("%02X ", buf[i]));
1648 #endif
1649 }
1650
1651 return 1;
1652 #endif
1653 }
1654
1655 void
1656 ahci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1657 {
1658 xfer->ux_status = status;
1659 usb_transfer_complete(xfer);
1660 }
1661
1662 void
1663 ahci_device_clear_toggle(usbd_pipe_handle pipe)
1664 {
1665 struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
1666 apipe->toggle = 0;
1667 }
1668
1669 #ifdef AHCI_DEBUG
1670 void
1671 print_req(usb_device_request_t *r)
1672 {
1673 const char *xmes[]={
1674 "GETSTAT",
1675 "CLRFEAT",
1676 "res",
1677 "SETFEAT",
1678 "res",
1679 "SETADDR",
1680 "GETDESC",
1681 "SETDESC",
1682 "GETCONF",
1683 "SETCONF",
1684 "GETIN/F",
1685 "SETIN/F",
1686 "SYNC_FR"
1687 };
1688 int req, type, value, index, len;
1689
1690 req = r->bRequest;
1691 type = r->bmRequestType;
1692 value = UGETW(r->wValue);
1693 index = UGETW(r->wIndex);
1694 len = UGETW(r->wLength);
1695
1696 printf("%x,%s,v=%d,i=%d,l=%d ",
1697 type, xmes[req], value, index, len);
1698 }
1699
1700 void
1701 print_req_hub(usb_device_request_t *r)
1702 {
1703 struct {
1704 int req;
1705 int type;
1706 const char *str;
1707 } conf[] = {
1708 { 1, 0x20, "ClrHubFeat" },
1709 { 1, 0x23, "ClrPortFeat" },
1710 { 2, 0xa3, "GetBusState" },
1711 { 6, 0xa0, "GetHubDesc" },
1712 { 0, 0xa0, "GetHubStat" },
1713 { 0, 0xa3, "GetPortStat" },
1714 { 7, 0x20, "SetHubDesc" },
1715 { 3, 0x20, "SetHubFeat" },
1716 { 3, 0x23, "SetPortFeat" },
1717 {-1, 0, NULL},
1718 };
1719 int i;
1720 int value, index, len;
1721
1722 value = UGETW(r->wValue);
1723 index = UGETW(r->wIndex);
1724 len = UGETW(r->wLength);
1725 for (i = 0; ; i++) {
1726 if (conf[i].req == -1 )
1727 return print_req(r);
1728 if (r->bmRequestType == conf[i].type && r->bRequest == conf[i].req) {
1729 printf("%s", conf[i].str);
1730 break;
1731 }
1732 }
1733 printf(",v=%d,i=%d,l=%d ", value, index, len);
1734 }
1735
1736 void
1737 print_dumpreg(struct ahci_softc *sc)
1738 {
1739 #if 0
1740 printf("00=%02x,01=%02x,02=%02x,03=%02x,04=%02x,"
1741 "08=%02x,09=%02x,0A=%02x,0B=%02x,0C=%02x,",
1742 sl11read(sc, 0), sl11read(sc, 1),
1743 sl11read(sc, 2), sl11read(sc, 3),
1744 sl11read(sc, 4), sl11read(sc, 8),
1745 sl11read(sc, 9), sl11read(sc, 10),
1746 sl11read(sc, 11), sl11read(sc, 12)
1747 );
1748 printf("CR1=%02x,IER=%02x,0D=%02x,0E=%02x,0F=%02x ",
1749 sl11read(sc, 5), sl11read(sc, 6),
1750 sl11read(sc, 13), sl11read(sc, 14), sl11read(sc, 15)
1751 );
1752 #endif
1753 }
1754
1755 void
1756 print_xfer(usbd_xfer_handle xfer)
1757 {
1758 printf("xfer: length=%d, actlen=%d, flags=%x, timeout=%d,",
1759 xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
1760 printf("request{ ");
1761 print_req_hub(&xfer->ux_request);
1762 printf("} ");
1763 }
1764 #endif /* AHCI_DEBUG */
1765