3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * This file contains the driver for the SAF1761 series USB OTG
36 * Datasheet is available from:
37 * http://www.nxp.com/products/automotive/multimedia/usb/SAF1761BE.html
40 #ifdef USB_GLOBAL_INCLUDE_FILE
41 #include USB_GLOBAL_INCLUDE_FILE
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
65 #define USB_DEBUG_VAR saf1761_otg_debug
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_debug.h>
69 #include <dev/usb/usb_busdma.h>
70 #include <dev/usb/usb_process.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_device.h>
73 #include <dev/usb/usb_hub.h>
74 #include <dev/usb/usb_util.h>
76 #include <dev/usb/usb_controller.h>
77 #include <dev/usb/usb_bus.h>
78 #endif /* USB_GLOBAL_INCLUDE_FILE */
80 #include <dev/usb/controller/saf1761_otg.h>
81 #include <dev/usb/controller/saf1761_otg_reg.h>
83 #define SAF1761_OTG_BUS2SC(bus) \
84 ((struct saf1761_otg_softc *)(((uint8_t *)(bus)) - \
85 ((uint8_t *)&(((struct saf1761_otg_softc *)0)->sc_bus))))
87 #define SAF1761_OTG_PC2UDEV(pc) \
88 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
90 #define SAF1761_DCINTERRUPT_THREAD_IRQ \
91 (SOTG_DCINTERRUPT_IEVBUS | SOTG_DCINTERRUPT_IEBRST | \
92 SOTG_DCINTERRUPT_IERESM | SOTG_DCINTERRUPT_IESUSP)
95 static int saf1761_otg_debug = 0;
96 static int saf1761_otg_forcefs = 0;
99 SYSCTL_NODE(_hw_usb, OID_AUTO, saf1761_otg, CTLFLAG_RW, 0,
102 SYSCTL_INT(_hw_usb_saf1761_otg, OID_AUTO, debug, CTLFLAG_RW,
103 &saf1761_otg_debug, 0, "SAF1761 DCI debug level");
104 SYSCTL_INT(_hw_usb_saf1761_otg, OID_AUTO, forcefs, CTLFLAG_RW,
105 &saf1761_otg_forcefs, 0, "SAF1761 DCI force FULL speed");
108 #define SAF1761_OTG_INTR_ENDPT 1
112 static const struct usb_bus_methods saf1761_otg_bus_methods;
113 static const struct usb_pipe_methods saf1761_otg_non_isoc_methods;
114 static const struct usb_pipe_methods saf1761_otg_device_isoc_methods;
116 static saf1761_otg_cmd_t saf1761_host_setup_tx;
117 static saf1761_otg_cmd_t saf1761_host_bulk_data_rx;
118 static saf1761_otg_cmd_t saf1761_host_bulk_data_tx;
119 static saf1761_otg_cmd_t saf1761_host_intr_data_rx;
120 static saf1761_otg_cmd_t saf1761_host_intr_data_tx;
121 static saf1761_otg_cmd_t saf1761_host_isoc_data_rx;
122 static saf1761_otg_cmd_t saf1761_host_isoc_data_tx;
123 static saf1761_otg_cmd_t saf1761_device_setup_rx;
124 static saf1761_otg_cmd_t saf1761_device_data_rx;
125 static saf1761_otg_cmd_t saf1761_device_data_tx;
126 static saf1761_otg_cmd_t saf1761_device_data_tx_sync;
127 static void saf1761_otg_device_done(struct usb_xfer *, usb_error_t);
128 static void saf1761_otg_do_poll(struct usb_bus *);
129 static void saf1761_otg_standard_done(struct usb_xfer *);
130 static void saf1761_otg_intr_set(struct usb_xfer *, uint8_t);
131 static void saf1761_otg_root_intr(struct saf1761_otg_softc *);
134 * Here is a list of what the SAF1761 chip can support. The main
135 * limitation is that the sum of the buffer sizes must be less than
138 static const struct usb_hw_ep_profile saf1761_otg_ep_profile[] = {
141 .max_in_frame_size = 64,
142 .max_out_frame_size = 64,
144 .support_control = 1,
147 .max_in_frame_size = SOTG_HS_MAX_PACKET_SIZE,
148 .max_out_frame_size = SOTG_HS_MAX_PACKET_SIZE,
150 .support_interrupt = 1,
152 .support_isochronous = 1,
159 saf1761_otg_get_hw_ep_profile(struct usb_device *udev,
160 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
163 *ppf = saf1761_otg_ep_profile + 0;
164 } else if (ep_addr < 8) {
165 *ppf = saf1761_otg_ep_profile + 1;
172 saf1761_otg_pull_up(struct saf1761_otg_softc *sc)
174 /* activate pullup on D+, if possible */
176 if (!sc->sc_flags.d_pulled_up && sc->sc_flags.port_powered) {
179 sc->sc_flags.d_pulled_up = 1;
184 saf1761_otg_pull_down(struct saf1761_otg_softc *sc)
186 /* release pullup on D+, if possible */
188 if (sc->sc_flags.d_pulled_up) {
191 sc->sc_flags.d_pulled_up = 0;
196 saf1761_otg_wakeup_peer(struct saf1761_otg_softc *sc)
200 if (!(sc->sc_flags.status_suspend))
205 temp = SAF1761_READ_LE_4(sc, SOTG_MODE);
206 SAF1761_WRITE_LE_4(sc, SOTG_MODE, temp | SOTG_MODE_SNDRSU);
207 SAF1761_WRITE_LE_4(sc, SOTG_MODE, temp & ~SOTG_MODE_SNDRSU);
209 /* Wait 8ms for remote wakeup to complete. */
210 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
214 saf1761_host_channel_alloc(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
218 if (td->channel < SOTG_HOST_CHANNEL_MAX)
221 /* check if device is suspended */
222 if (SAF1761_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
223 return (1); /* busy - cannot transfer data */
225 switch (td->ep_type) {
227 for (x = 0; x != 32; x++) {
228 if (sc->sc_host_intr_map & (1 << x))
230 sc->sc_host_intr_map |= (1 << x);
231 td->channel = 32 + x;
236 for (x = 0; x != 32; x++) {
237 if (sc->sc_host_isoc_map & (1 << x))
239 sc->sc_host_isoc_map |= (1 << x);
245 for (x = 0; x != 32; x++) {
246 if (sc->sc_host_async_map & (1 << x))
248 sc->sc_host_async_map |= (1 << x);
249 td->channel = 64 + x;
258 saf1761_host_channel_free(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
262 if (td->channel >= SOTG_HOST_CHANNEL_MAX)
265 switch (td->ep_type) {
267 x = td->channel - 32;
268 td->channel = SOTG_HOST_CHANNEL_MAX;
269 sc->sc_host_intr_map &= ~(1 << x);
270 sc->sc_host_intr_suspend_map &= ~(1 << x);
271 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
272 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
276 td->channel = SOTG_HOST_CHANNEL_MAX;
277 sc->sc_host_isoc_map &= ~(1 << x);
278 sc->sc_host_isoc_suspend_map &= ~(1 << x);
279 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
280 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
283 x = td->channel - 64;
284 td->channel = SOTG_HOST_CHANNEL_MAX;
285 sc->sc_host_async_map &= ~(1 << x);
286 sc->sc_host_async_suspend_map &= ~(1 << x);
287 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
288 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
294 saf1761_peek_host_status_le_4(struct saf1761_otg_softc *sc, uint32_t offset)
300 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
301 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
302 retval = SAF1761_READ_LE_4(sc, offset);
306 DPRINTF("STAUS is zero at offset 0x%x\n", offset);
314 saf1761_read_host_memory(struct saf1761_otg_softc *sc,
315 struct saf1761_otg_td *td, uint32_t len)
317 struct usb_page_search buf_res;
324 offset = SOTG_DATA_ADDR(td->channel);
325 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
326 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
328 /* optimised read first */
330 usbd_get_page(td->pc, td->offset, &buf_res);
332 /* get correct length */
333 if (buf_res.length > len)
334 buf_res.length = len;
336 /* check buffer alignment */
337 if (((uintptr_t)buf_res.buffer) & 3)
340 count = buf_res.length & ~3;
344 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
345 offset, buf_res.buffer, count / 4);
350 /* update remainder and offset */
351 td->remainder -= count;
356 /* use bounce buffer */
357 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
358 offset, sc->sc_bounce_buffer, (len + 3) / 4);
359 usbd_copy_in(td->pc, td->offset,
360 sc->sc_bounce_buffer, len);
362 /* update remainder and offset */
363 td->remainder -= len;
369 saf1761_write_host_memory(struct saf1761_otg_softc *sc,
370 struct saf1761_otg_td *td, uint32_t len)
372 struct usb_page_search buf_res;
379 offset = SOTG_DATA_ADDR(td->channel);
381 /* optimised write first */
383 usbd_get_page(td->pc, td->offset, &buf_res);
385 /* get correct length */
386 if (buf_res.length > len)
387 buf_res.length = len;
389 /* check buffer alignment */
390 if (((uintptr_t)buf_res.buffer) & 3)
393 count = buf_res.length & ~3;
397 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
398 offset, buf_res.buffer, count / 4);
403 /* update remainder and offset */
404 td->remainder -= count;
408 /* use bounce buffer */
409 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
410 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
411 offset, sc->sc_bounce_buffer, (len + 3) / 4);
413 /* update remainder and offset */
414 td->remainder -= len;
420 saf1761_host_setup_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
427 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
428 pdt_addr = SOTG_PTD(td->channel);
430 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
432 DPRINTFN(5, "STATUS=0x%08x\n", status);
434 if (status & SOTG_PTD_DW3_ACTIVE) {
436 } else if (status & SOTG_PTD_DW3_HALTED) {
441 if (saf1761_host_channel_alloc(sc, td))
446 if (count != td->remainder) {
451 saf1761_write_host_memory(sc, td, count);
453 pdt_addr = SOTG_PTD(td->channel);
455 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
456 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
457 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
458 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
460 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
461 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
463 temp = SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8;
464 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
466 temp = td->dw1_value | (2 << 10) /* SETUP PID */ | (td->ep_index >> 1);
467 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
469 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
470 (td->max_packet_size << 18) /* wMaxPacketSize */ |
471 (count << 3) /* transfer count */ |
473 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
476 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
477 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
481 return (1); /* busy */
483 saf1761_host_channel_free(sc, td);
484 return (0); /* complete */
488 saf1761_host_bulk_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
493 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
498 pdt_addr = SOTG_PTD(td->channel);
500 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
502 DPRINTFN(5, "STATUS=0x%08x\n", status);
504 if (status & SOTG_PTD_DW3_ACTIVE) {
506 } else if (status & SOTG_PTD_DW3_HALTED) {
507 if (!(status & SOTG_PTD_DW3_ERRORS))
512 count = (status & SOTG_PTD_DW3_XFER_COUNT);
515 /* verify the packet byte count */
516 if (count != td->max_packet_size) {
517 if (count < td->max_packet_size) {
518 /* we have a short packet */
522 /* invalid USB packet */
529 /* verify the packet byte count */
530 if (count > td->remainder) {
531 /* invalid USB packet */
536 saf1761_read_host_memory(sc, td, count);
538 /* check if we are complete */
539 if ((td->remainder == 0) || got_short) {
542 /* else need to receive a zero length packet */
544 saf1761_host_channel_free(sc, td);
546 if (saf1761_host_channel_alloc(sc, td))
549 /* set toggle, if any */
550 if (td->set_toggle) {
555 /* receive one more packet */
557 pdt_addr = SOTG_PTD(td->channel);
559 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
560 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
561 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
562 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
564 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
566 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
568 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
569 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
571 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
572 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
574 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
575 (td->max_packet_size << 18) /* wMaxPacketSize */ |
576 (td->max_packet_size << 3) /* transfer count */ |
578 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
581 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
582 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
584 return (1); /* busy */
586 saf1761_host_channel_free(sc, td);
587 return (0); /* complete */
591 saf1761_host_bulk_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
597 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
600 pdt_addr = SOTG_PTD(td->channel);
602 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
604 DPRINTFN(5, "STATUS=0x%08x\n", status);
606 if (status & SOTG_PTD_DW3_ACTIVE) {
608 } else if (status & SOTG_PTD_DW3_HALTED) {
609 if (!(status & SOTG_PTD_DW3_ERRORS))
614 /* check remainder */
615 if (td->remainder == 0) {
618 /* else we need to transmit a short packet */
620 saf1761_host_channel_free(sc, td);
622 if (saf1761_host_channel_alloc(sc, td))
625 count = td->max_packet_size;
626 if (td->remainder < count) {
627 /* we have a short packet */
629 count = td->remainder;
632 saf1761_write_host_memory(sc, td, count);
634 /* set toggle, if any */
635 if (td->set_toggle) {
640 /* send one more packet */
642 pdt_addr = SOTG_PTD(td->channel);
644 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
645 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
646 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
647 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
649 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
651 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
653 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
654 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
656 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
657 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
659 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
660 (td->max_packet_size << 18) /* wMaxPacketSize */ |
661 (count << 3) /* transfer count */ |
663 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
666 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
667 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
671 return (1); /* busy */
673 saf1761_host_channel_free(sc, td);
674 return (0); /* complete */
678 saf1761_host_intr_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
683 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
688 pdt_addr = SOTG_PTD(td->channel);
690 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
692 DPRINTFN(5, "STATUS=0x%08x\n", status);
694 if (status & SOTG_PTD_DW3_ACTIVE) {
696 } else if (status & SOTG_PTD_DW3_HALTED) {
697 if (!(status & SOTG_PTD_DW3_ERRORS))
702 count = (status & SOTG_PTD_DW3_XFER_COUNT);
705 /* verify the packet byte count */
706 if (count != td->max_packet_size) {
707 if (count < td->max_packet_size) {
708 /* we have a short packet */
712 /* invalid USB packet */
719 /* verify the packet byte count */
720 if (count > td->remainder) {
721 /* invalid USB packet */
726 saf1761_read_host_memory(sc, td, count);
728 /* check if we are complete */
729 if ((td->remainder == 0) || got_short) {
732 /* else need to receive a zero length packet */
734 saf1761_host_channel_free(sc, td);
736 if (saf1761_host_channel_alloc(sc, td))
739 /* set toggle, if any */
740 if (td->set_toggle) {
745 /* receive one more packet */
747 pdt_addr = SOTG_PTD(td->channel);
749 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
750 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
752 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
753 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
755 temp = (1U << td->uframe); /* start split */
756 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
758 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
759 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
761 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) | (td->interval & 0xF8);
762 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
764 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
765 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
767 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
768 (td->max_packet_size << 18) /* wMaxPacketSize */ |
769 (td->max_packet_size << 3) /* transfer count */ |
771 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
774 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
775 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
777 return (1); /* busy */
779 saf1761_host_channel_free(sc, td);
780 return (0); /* complete */
784 saf1761_host_intr_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
790 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
793 pdt_addr = SOTG_PTD(td->channel);
795 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
797 DPRINTFN(5, "STATUS=0x%08x\n", status);
799 if (status & SOTG_PTD_DW3_ACTIVE) {
801 } else if (status & SOTG_PTD_DW3_HALTED) {
802 if (!(status & SOTG_PTD_DW3_ERRORS))
808 /* check remainder */
809 if (td->remainder == 0) {
812 /* else we need to transmit a short packet */
814 saf1761_host_channel_free(sc, td);
816 if (saf1761_host_channel_alloc(sc, td))
819 count = td->max_packet_size;
820 if (td->remainder < count) {
821 /* we have a short packet */
823 count = td->remainder;
826 saf1761_write_host_memory(sc, td, count);
828 /* set toggle, if any */
829 if (td->set_toggle) {
834 /* send one more packet */
836 pdt_addr = SOTG_PTD(td->channel);
838 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
839 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
841 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
842 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
844 temp = (1U << td->uframe); /* start split */
845 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
847 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
848 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
850 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) | (td->interval & 0xF8);
851 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
853 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
854 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
856 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
857 (td->max_packet_size << 18) /* wMaxPacketSize */ |
858 (count << 3) /* transfer count */ |
860 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
863 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
864 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
868 return (1); /* busy */
870 saf1761_host_channel_free(sc, td);
871 return (0); /* complete */
875 saf1761_host_isoc_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
878 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
879 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
881 return (1); /* busy */
885 saf1761_host_isoc_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
888 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
889 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
891 return (1); /* busy */
895 saf1761_otg_set_address(struct saf1761_otg_softc *sc, uint8_t addr)
897 DPRINTFN(5, "addr=%d\n", addr);
899 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, addr | SOTG_ADDRESS_ENABLE);
904 saf1761_read_device_fifo(struct saf1761_otg_softc *sc,
905 struct saf1761_otg_td *td, uint32_t len)
907 struct usb_page_search buf_res;
910 /* optimised read first */
912 usbd_get_page(td->pc, td->offset, &buf_res);
914 /* get correct length */
915 if (buf_res.length > len)
916 buf_res.length = len;
918 /* check buffer alignment */
919 if (((uintptr_t)buf_res.buffer) & 3)
922 count = buf_res.length & ~3;
926 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
927 SOTG_DATA_PORT, buf_res.buffer, count / 4);
931 /* update remainder and offset */
932 td->remainder -= count;
937 /* use bounce buffer */
938 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
939 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
940 usbd_copy_in(td->pc, td->offset,
941 sc->sc_bounce_buffer, len);
943 /* update remainder and offset */
944 td->remainder -= len;
950 saf1761_write_device_fifo(struct saf1761_otg_softc *sc,
951 struct saf1761_otg_td *td, uint32_t len)
953 struct usb_page_search buf_res;
956 /* optimised write first */
958 usbd_get_page(td->pc, td->offset, &buf_res);
960 /* get correct length */
961 if (buf_res.length > len)
962 buf_res.length = len;
964 /* check buffer alignment */
965 if (((uintptr_t)buf_res.buffer) & 3)
968 count = buf_res.length & ~3;
972 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
973 SOTG_DATA_PORT, buf_res.buffer, count / 4);
977 /* update remainder and offset */
978 td->remainder -= count;
982 /* use bounce buffer */
983 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
984 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
985 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
987 /* update remainder and offset */
988 td->remainder -= len;
994 saf1761_device_setup_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
996 struct usb_device_request req;
999 /* select the correct endpoint */
1000 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1002 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1004 /* check buffer status */
1005 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1008 /* get buffer length */
1009 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1011 DPRINTFN(5, "count=%u rem=%u\n", count, td->remainder);
1013 /* clear did stall */
1017 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
1019 /* verify data length */
1020 if (count != td->remainder) {
1021 DPRINTFN(0, "Invalid SETUP packet "
1022 "length, %d bytes\n", count);
1025 if (count != sizeof(req)) {
1026 DPRINTFN(0, "Unsupported SETUP packet "
1027 "length, %d bytes\n", count);
1031 saf1761_read_device_fifo(sc, td, sizeof(req));
1033 /* extract SETUP packet again */
1034 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1036 /* sneak peek the set address request */
1037 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1038 (req.bRequest == UR_SET_ADDRESS)) {
1039 sc->sc_dv_addr = req.wValue[0] & 0x7F;
1040 DPRINTF("Set address %d\n", sc->sc_dv_addr);
1042 sc->sc_dv_addr = 0xFF;
1044 return (0); /* complete */
1047 /* abort any ongoing transfer */
1048 if (!td->did_stall) {
1049 DPRINTFN(5, "stalling\n");
1052 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
1056 return (1); /* not complete */
1060 saf1761_device_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1063 uint8_t got_short = 0;
1065 if (td->ep_index == 0) {
1066 /* select the correct endpoint */
1067 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1069 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1071 /* check buffer status */
1072 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1074 if (td->remainder == 0) {
1076 * We are actually complete and have
1077 * received the next SETUP:
1079 DPRINTFN(5, "faking complete\n");
1080 return (0); /* complete */
1082 DPRINTFN(5, "SETUP packet while receiving data\n");
1084 * USB Host Aborted the transfer.
1087 return (0); /* complete */
1090 /* select the correct endpoint */
1091 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1092 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1093 SOTG_EP_INDEX_DIR_OUT);
1095 /* enable data stage */
1096 if (td->set_toggle) {
1098 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1101 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1103 /* check buffer status */
1104 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1105 return (1); /* not complete */
1107 /* get buffer length */
1108 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1110 DPRINTFN(5, "rem=%u count=0x%04x\n", td->remainder, count);
1112 /* verify the packet byte count */
1113 if (count != td->max_packet_size) {
1114 if (count < td->max_packet_size) {
1115 /* we have a short packet */
1119 /* invalid USB packet */
1121 return (0); /* we are complete */
1124 /* verify the packet byte count */
1125 if (count > td->remainder) {
1126 /* invalid USB packet */
1128 return (0); /* we are complete */
1131 saf1761_read_device_fifo(sc, td, count);
1133 /* check if we are complete */
1134 if ((td->remainder == 0) || got_short) {
1135 if (td->short_pkt) {
1136 /* we are complete */
1139 /* else need to receive a zero length packet */
1141 return (1); /* not complete */
1145 saf1761_device_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1149 if (td->ep_index == 0) {
1150 /* select the correct endpoint */
1151 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1153 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1155 /* check buffer status */
1156 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1157 DPRINTFN(5, "SETUP abort\n");
1159 * USB Host Aborted the transfer.
1162 return (0); /* complete */
1165 /* select the correct endpoint */
1166 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1167 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1168 SOTG_EP_INDEX_DIR_IN);
1170 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1172 /* check buffer status */
1173 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1174 return (1); /* not complete */
1176 /* enable data stage */
1177 if (td->set_toggle) {
1179 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1182 DPRINTFN(5, "rem=%u\n", td->remainder);
1184 count = td->max_packet_size;
1185 if (td->remainder < count) {
1186 /* we have a short packet */
1188 count = td->remainder;
1191 saf1761_write_device_fifo(sc, td, count);
1193 if (td->ep_index == 0) {
1194 if (count < SOTG_FS_MAX_PACKET_SIZE) {
1195 /* set end of packet */
1196 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1199 if (count < SOTG_HS_MAX_PACKET_SIZE) {
1200 /* set end of packet */
1201 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1205 /* check remainder */
1206 if (td->remainder == 0) {
1207 if (td->short_pkt) {
1208 return (0); /* complete */
1210 /* else we need to transmit a short packet */
1212 return (1); /* not complete */
1216 saf1761_device_data_tx_sync(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1220 if (td->ep_index == 0) {
1221 /* select the correct endpoint */
1222 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1224 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1226 /* check buffer status */
1227 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1228 DPRINTFN(5, "Faking complete\n");
1229 return (0); /* complete */
1232 /* select the correct endpoint */
1233 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1234 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1235 SOTG_EP_INDEX_DIR_IN);
1237 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1239 /* check buffer status */
1240 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1241 return (1); /* busy */
1243 if (sc->sc_dv_addr != 0xFF) {
1244 /* write function address */
1245 saf1761_otg_set_address(sc, sc->sc_dv_addr);
1247 return (0); /* complete */
1251 saf1761_otg_xfer_do_fifo(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1253 struct saf1761_otg_td *td;
1258 td = xfer->td_transfer_cache;
1263 if ((td->func) (sc, td)) {
1264 /* operation in progress */
1267 if (((void *)td) == xfer->td_transfer_last) {
1270 if (td->error_any) {
1272 } else if (td->remainder > 0) {
1274 * We had a short transfer. If there is no alternate
1275 * next, stop processing !
1277 if (!td->alt_next) {
1282 * Fetch the next transfer descriptor.
1284 toggle = td->toggle;
1286 td->toggle = toggle;
1287 xfer->td_transfer_cache = td;
1292 /* compute all actual lengths */
1293 xfer->td_transfer_cache = NULL;
1294 sc->sc_xfer_complete = 1;
1298 saf1761_otg_xfer_do_complete(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1300 struct saf1761_otg_td *td;
1304 td = xfer->td_transfer_cache;
1306 /* compute all actual lengths */
1307 saf1761_otg_standard_done(xfer);
1314 saf1761_otg_interrupt_poll_locked(struct saf1761_otg_softc *sc)
1316 struct usb_xfer *xfer;
1318 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
1319 saf1761_otg_xfer_do_fifo(sc, xfer);
1323 saf1761_otg_wait_suspend(struct saf1761_otg_softc *sc, uint8_t on)
1326 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IESUSP;
1327 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IERESM;
1329 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IESUSP;
1330 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IERESM;
1332 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1336 saf1761_otg_update_vbus(struct saf1761_otg_softc *sc)
1340 /* read fresh status */
1341 status = SAF1761_READ_LE_4(sc, SOTG_STATUS);
1343 DPRINTFN(4, "STATUS=0x%04x\n", status);
1345 if ((status & SOTG_STATUS_VBUS_VLD) &&
1346 (status & SOTG_STATUS_ID)) {
1347 /* VBUS present and device mode */
1348 if (!sc->sc_flags.status_vbus) {
1349 sc->sc_flags.status_vbus = 1;
1351 /* complete root HUB interrupt endpoint */
1352 saf1761_otg_root_intr(sc);
1355 /* VBUS not-present or host mode */
1356 if (sc->sc_flags.status_vbus) {
1357 sc->sc_flags.status_vbus = 0;
1358 sc->sc_flags.status_bus_reset = 0;
1359 sc->sc_flags.status_suspend = 0;
1360 sc->sc_flags.change_suspend = 0;
1361 sc->sc_flags.change_connect = 1;
1363 /* complete root HUB interrupt endpoint */
1364 saf1761_otg_root_intr(sc);
1370 saf1761_otg_interrupt_complete_locked(struct saf1761_otg_softc *sc)
1372 struct usb_xfer *xfer;
1374 /* scan for completion events */
1375 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1376 if (saf1761_otg_xfer_do_complete(sc, xfer))
1382 saf1761_otg_filter_interrupt(void *arg)
1384 struct saf1761_otg_softc *sc = arg;
1385 int retval = FILTER_HANDLED;
1389 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1391 hcstat = SAF1761_READ_LE_4(sc, SOTG_HCINTERRUPT);
1392 /* acknowledge all host controller interrupts */
1393 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT, hcstat);
1395 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT);
1396 /* acknowledge all device controller interrupts */
1397 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT,
1398 status & ~SAF1761_DCINTERRUPT_THREAD_IRQ);
1400 (void) SAF1761_READ_LE_4(sc, SOTG_ATL_PTD_DONE_PTD);
1401 (void) SAF1761_READ_LE_4(sc, SOTG_INT_PTD_DONE_PTD);
1402 (void) SAF1761_READ_LE_4(sc, SOTG_ISO_PTD_DONE_PTD);
1404 if (status & SAF1761_DCINTERRUPT_THREAD_IRQ)
1405 retval = FILTER_SCHEDULE_THREAD;
1407 /* poll FIFOs, if any */
1408 saf1761_otg_interrupt_poll_locked(sc);
1410 if (sc->sc_xfer_complete != 0)
1411 retval = FILTER_SCHEDULE_THREAD;
1413 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1419 saf1761_otg_interrupt(void *arg)
1421 struct saf1761_otg_softc *sc = arg;
1424 USB_BUS_LOCK(&sc->sc_bus);
1425 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1427 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT) &
1428 SAF1761_DCINTERRUPT_THREAD_IRQ;
1430 /* acknowledge all device controller interrupts */
1431 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT, status);
1433 DPRINTF("DCINTERRUPT=0x%08x SOF=0x%08x "
1434 "FRINDEX=0x%08x\n", status,
1435 SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM),
1436 SAF1761_READ_LE_4(sc, SOTG_FRINDEX));
1438 /* update VBUS and ID bits, if any */
1439 if (status & SOTG_DCINTERRUPT_IEVBUS)
1440 saf1761_otg_update_vbus(sc);
1442 if (status & SOTG_DCINTERRUPT_IEBRST) {
1444 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1445 SOTG_UNLOCK_DEVICE_CODE);
1447 /* Enable device address */
1448 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS,
1449 SOTG_ADDRESS_ENABLE);
1451 sc->sc_flags.status_bus_reset = 1;
1452 sc->sc_flags.status_suspend = 0;
1453 sc->sc_flags.change_suspend = 0;
1454 sc->sc_flags.change_connect = 1;
1456 /* disable resume interrupt */
1457 saf1761_otg_wait_suspend(sc, 1);
1458 /* complete root HUB interrupt endpoint */
1459 saf1761_otg_root_intr(sc);
1462 * If "RESUME" and "SUSPEND" is set at the same time we
1463 * interpret that like "RESUME". Resume is set when there is
1464 * at least 3 milliseconds of inactivity on the USB BUS:
1466 if (status & SOTG_DCINTERRUPT_IERESM) {
1468 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1469 SOTG_UNLOCK_DEVICE_CODE);
1471 if (sc->sc_flags.status_suspend) {
1472 sc->sc_flags.status_suspend = 0;
1473 sc->sc_flags.change_suspend = 1;
1474 /* disable resume interrupt */
1475 saf1761_otg_wait_suspend(sc, 1);
1476 /* complete root HUB interrupt endpoint */
1477 saf1761_otg_root_intr(sc);
1479 } else if (status & SOTG_DCINTERRUPT_IESUSP) {
1480 if (!sc->sc_flags.status_suspend) {
1481 sc->sc_flags.status_suspend = 1;
1482 sc->sc_flags.change_suspend = 1;
1483 /* enable resume interrupt */
1484 saf1761_otg_wait_suspend(sc, 0);
1485 /* complete root HUB interrupt endpoint */
1486 saf1761_otg_root_intr(sc);
1490 if (sc->sc_xfer_complete != 0) {
1491 sc->sc_xfer_complete = 0;
1493 /* complete FIFOs, if any */
1494 saf1761_otg_interrupt_complete_locked(sc);
1496 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1497 USB_BUS_UNLOCK(&sc->sc_bus);
1501 saf1761_otg_setup_standard_chain_sub(struct saf1761_otg_std_temp *temp)
1503 struct saf1761_otg_td *td;
1505 /* get current Transfer Descriptor */
1509 /* prepare for next TD */
1510 temp->td_next = td->obj_next;
1512 /* fill out the Transfer Descriptor */
1513 td->func = temp->func;
1515 td->offset = temp->offset;
1516 td->remainder = temp->len;
1518 td->error_stall = 0;
1520 td->did_stall = temp->did_stall;
1521 td->short_pkt = temp->short_pkt;
1522 td->alt_next = temp->setup_alt_next;
1523 td->channel = SOTG_HOST_CHANNEL_MAX;
1527 saf1761_otg_setup_standard_chain(struct usb_xfer *xfer)
1529 struct saf1761_otg_std_temp temp;
1530 struct saf1761_otg_softc *sc;
1531 struct saf1761_otg_td *td;
1538 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1539 xfer->address, UE_GET_ADDR(xfer->endpointno),
1540 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1542 temp.max_frame_size = xfer->max_frame_size;
1544 td = xfer->td_start[0];
1545 xfer->td_transfer_first = td;
1546 xfer->td_transfer_cache = td;
1552 temp.td_next = xfer->td_start[0];
1554 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
1555 xfer->flags_int.isochronous_xfr;
1556 temp.did_stall = !xfer->flags_int.control_stall;
1558 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
1560 sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1561 ep_no = (xfer->endpointno & UE_ADDR);
1562 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
1564 /* check if we should prepend a setup message */
1566 if (xfer->flags_int.control_xfr) {
1567 if (xfer->flags_int.control_hdr) {
1570 temp.func = &saf1761_host_setup_tx;
1572 temp.func = &saf1761_device_setup_rx;
1574 temp.len = xfer->frlengths[0];
1575 temp.pc = xfer->frbuffers + 0;
1576 temp.short_pkt = temp.len ? 1 : 0;
1577 /* check for last frame */
1578 if (xfer->nframes == 1) {
1579 /* no STATUS stage yet, SETUP is last */
1580 if (xfer->flags_int.control_act)
1581 temp.setup_alt_next = 0;
1583 saf1761_otg_setup_standard_chain_sub(&temp);
1590 if (x != xfer->nframes) {
1591 if (xfer->endpointno & UE_DIR_IN) {
1593 if (ep_type == UE_INTERRUPT)
1594 temp.func = &saf1761_host_intr_data_rx;
1595 else if (ep_type == UE_ISOCHRONOUS)
1596 temp.func = &saf1761_host_isoc_data_rx;
1598 temp.func = &saf1761_host_bulk_data_rx;
1601 temp.func = &saf1761_device_data_tx;
1606 if (ep_type == UE_INTERRUPT)
1607 temp.func = &saf1761_host_intr_data_tx;
1608 else if (ep_type == UE_ISOCHRONOUS)
1609 temp.func = &saf1761_host_isoc_data_tx;
1611 temp.func = &saf1761_host_bulk_data_tx;
1614 temp.func = &saf1761_device_data_rx;
1619 /* setup "pc" pointer */
1620 temp.pc = xfer->frbuffers + x;
1625 while (x != xfer->nframes) {
1627 /* DATA0 / DATA1 message */
1629 temp.len = xfer->frlengths[x];
1633 if (x == xfer->nframes) {
1634 if (xfer->flags_int.control_xfr) {
1635 if (xfer->flags_int.control_act) {
1636 temp.setup_alt_next = 0;
1639 temp.setup_alt_next = 0;
1642 if (temp.len == 0) {
1644 /* make sure that we send an USB packet */
1650 /* regular data transfer */
1652 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1655 saf1761_otg_setup_standard_chain_sub(&temp);
1657 if (xfer->flags_int.isochronous_xfr) {
1658 temp.offset += temp.len;
1660 /* get next Page Cache pointer */
1661 temp.pc = xfer->frbuffers + x;
1665 /* check for control transfer */
1666 if (xfer->flags_int.control_xfr) {
1667 /* always setup a valid "pc" pointer for status and sync */
1668 temp.pc = xfer->frbuffers + 0;
1671 temp.setup_alt_next = 0;
1673 /* check if we should append a status stage */
1674 if (!xfer->flags_int.control_act) {
1677 * Send a DATA1 message and invert the current
1678 * endpoint direction.
1680 if (xfer->endpointno & UE_DIR_IN) {
1682 temp.func = &saf1761_host_bulk_data_tx;
1685 temp.func = &saf1761_device_data_rx;
1690 temp.func = &saf1761_host_bulk_data_rx;
1693 temp.func = &saf1761_device_data_tx;
1700 saf1761_otg_setup_standard_chain_sub(&temp);
1702 /* data toggle should be DATA1 */
1707 /* we need a SYNC point after TX */
1708 temp.func = &saf1761_device_data_tx_sync;
1709 saf1761_otg_setup_standard_chain_sub(&temp);
1714 temp.pc = xfer->frbuffers + 0;
1717 temp.setup_alt_next = 0;
1719 /* we need a SYNC point after TX */
1720 temp.func = &saf1761_device_data_tx_sync;
1721 saf1761_otg_setup_standard_chain_sub(&temp);
1725 /* must have at least one frame! */
1727 xfer->td_transfer_last = td;
1730 /* get first again */
1731 td = xfer->td_transfer_first;
1732 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
1737 saf1761_otg_timeout(void *arg)
1739 struct usb_xfer *xfer = arg;
1741 DPRINTF("xfer=%p\n", xfer);
1743 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1745 /* transfer is transferred */
1746 saf1761_otg_device_done(xfer, USB_ERR_TIMEOUT);
1750 saf1761_otg_intr_set(struct usb_xfer *xfer, uint8_t set)
1752 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1753 uint8_t ep_no = (xfer->endpointno & UE_ADDR);
1756 DPRINTFN(15, "endpoint=%d set=%d\n", xfer->endpointno, set);
1759 mask = SOTG_DCINTERRUPT_IEPRX(0) |
1760 SOTG_DCINTERRUPT_IEPTX(0) |
1761 SOTG_DCINTERRUPT_IEP0SETUP;
1762 } else if (xfer->endpointno & UE_DIR_IN) {
1763 mask = SOTG_DCINTERRUPT_IEPTX(ep_no);
1765 mask = SOTG_DCINTERRUPT_IEPRX(ep_no);
1769 sc->sc_intr_enable |= mask;
1771 sc->sc_intr_enable &= ~mask;
1773 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1777 saf1761_otg_start_standard_chain(struct usb_xfer *xfer)
1779 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1783 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1786 saf1761_otg_xfer_do_fifo(sc, xfer);
1788 if (xfer->td_transfer_cache != NULL) {
1790 * Only enable the endpoint interrupt when we are
1791 * actually waiting for data, hence we are dealing
1792 * with level triggered interrupts !
1794 saf1761_otg_intr_set(xfer, 1);
1796 /* put transfer on interrupt queue */
1797 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1799 /* start timeout, if any */
1800 if (xfer->timeout != 0) {
1801 usbd_transfer_timeout_ms(xfer,
1802 &saf1761_otg_timeout, xfer->timeout);
1805 /* catch completion, if any */
1806 saf1761_otg_interrupt_complete_locked(sc);
1808 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1812 saf1761_otg_root_intr(struct saf1761_otg_softc *sc)
1816 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1818 /* set port bit - we only have one port */
1819 sc->sc_hub_idata[0] = 0x02;
1821 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1822 sizeof(sc->sc_hub_idata));
1826 saf1761_otg_standard_done_sub(struct usb_xfer *xfer)
1828 struct saf1761_otg_td *td;
1834 td = xfer->td_transfer_cache;
1837 len = td->remainder;
1839 /* store last data toggle */
1840 xfer->endpoint->toggle_next = td->toggle;
1842 if (xfer->aframes != xfer->nframes) {
1844 * Verify the length and subtract
1845 * the remainder from "frlengths[]":
1847 if (len > xfer->frlengths[xfer->aframes]) {
1850 xfer->frlengths[xfer->aframes] -= len;
1853 /* Check for transfer error */
1854 if (td->error_any) {
1855 /* the transfer is finished */
1856 error = (td->error_stall ?
1857 USB_ERR_STALLED : USB_ERR_IOERROR);
1861 /* Check for short transfer */
1863 if (xfer->flags_int.short_frames_ok ||
1864 xfer->flags_int.isochronous_xfr) {
1865 /* follow alt next */
1872 /* the transfer is finished */
1880 /* this USB frame is complete */
1886 /* update transfer cache */
1888 xfer->td_transfer_cache = td;
1894 saf1761_otg_standard_done(struct usb_xfer *xfer)
1896 usb_error_t err = 0;
1898 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1899 xfer, xfer->endpoint);
1903 xfer->td_transfer_cache = xfer->td_transfer_first;
1905 if (xfer->flags_int.control_xfr) {
1907 if (xfer->flags_int.control_hdr) {
1909 err = saf1761_otg_standard_done_sub(xfer);
1913 if (xfer->td_transfer_cache == NULL) {
1917 while (xfer->aframes != xfer->nframes) {
1919 err = saf1761_otg_standard_done_sub(xfer);
1922 if (xfer->td_transfer_cache == NULL) {
1927 if (xfer->flags_int.control_xfr &&
1928 !xfer->flags_int.control_act) {
1930 err = saf1761_otg_standard_done_sub(xfer);
1933 saf1761_otg_device_done(xfer, err);
1936 /*------------------------------------------------------------------------*
1937 * saf1761_otg_device_done
1939 * NOTE: this function can be called more than one time on the
1940 * same USB transfer!
1941 *------------------------------------------------------------------------*/
1943 saf1761_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
1945 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1947 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1949 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1950 xfer, xfer->endpoint, error);
1952 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1954 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1955 saf1761_otg_intr_set(xfer, 0);
1957 struct saf1761_otg_td *td;
1959 td = xfer->td_transfer_first;
1962 saf1761_host_channel_free(sc, td);
1965 /* dequeue transfer and start next transfer */
1966 usbd_transfer_done(xfer, error);
1968 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1972 saf1761_otg_xfer_stall(struct usb_xfer *xfer)
1974 saf1761_otg_device_done(xfer, USB_ERR_STALLED);
1978 saf1761_otg_set_stall(struct usb_device *udev,
1979 struct usb_endpoint *ep, uint8_t *did_stall)
1981 struct saf1761_otg_softc *sc;
1986 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1989 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1994 DPRINTFN(5, "endpoint=%p\n", ep);
1997 sc = SAF1761_OTG_BUS2SC(udev->bus);
1999 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
2000 ep_dir = (ep->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT));
2001 ep_type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2003 if (ep_type == UE_CONTROL) {
2004 /* should not happen */
2007 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2009 /* select the correct endpoint */
2010 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2011 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2012 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2013 SOTG_EP_INDEX_DIR_OUT));
2016 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
2018 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2022 saf1761_otg_clear_stall_sub_locked(struct saf1761_otg_softc *sc,
2023 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2025 if (ep_type == UE_CONTROL) {
2026 /* clearing stall is not needed */
2029 /* select the correct endpoint */
2030 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2031 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2032 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2033 SOTG_EP_INDEX_DIR_OUT));
2035 /* disable endpoint */
2036 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, 0);
2037 /* enable endpoint again - will clear data toggle */
2038 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, ep_type | SOTG_EP_TYPE_ENABLE);
2041 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_CLBUF);
2043 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
2047 saf1761_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
2049 struct saf1761_otg_softc *sc;
2050 struct usb_endpoint_descriptor *ed;
2052 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2054 DPRINTFN(5, "endpoint=%p\n", ep);
2057 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2062 sc = SAF1761_OTG_BUS2SC(udev->bus);
2064 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2066 /* get endpoint descriptor */
2069 /* reset endpoint */
2070 saf1761_otg_clear_stall_sub_locked(sc,
2071 (ed->bEndpointAddress & UE_ADDR),
2072 (ed->bmAttributes & UE_XFERTYPE),
2073 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
2075 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2079 saf1761_otg_init(struct saf1761_otg_softc *sc)
2081 const struct usb_hw_ep_profile *pf;
2086 /* set up the bus structure */
2087 sc->sc_bus.usbrev = USB_REV_2_0;
2088 sc->sc_bus.methods = &saf1761_otg_bus_methods;
2090 USB_BUS_LOCK(&sc->sc_bus);
2092 /* Reset Host controller, including HW mode */
2093 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_ALL);
2097 /* Reset Host controller, including HW mode */
2098 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_HC);
2103 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, 0);
2108 /* Enable interrupts */
2109 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_GLOBAL_INTR_EN |
2110 SOTG_HW_MODE_CTRL_COMN_INT;
2113 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE, SOTG_UNLOCK_DEVICE_CODE);
2116 * Set correct hardware mode, must be written twice if bus
2119 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2120 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2122 SAF1761_WRITE_LE_4(sc, SOTG_DCSCRATCH, 0xdeadbeef);
2123 SAF1761_WRITE_LE_4(sc, SOTG_HCSCRATCH, 0xdeadbeef);
2125 DPRINTF("DCID=0x%08x VEND_PROD=0x%08x HWMODE=0x%08x SCRATCH=0x%08x,0x%08x\n",
2126 SAF1761_READ_LE_4(sc, SOTG_DCCHIP_ID),
2127 SAF1761_READ_LE_4(sc, SOTG_VEND_PROD_ID),
2128 SAF1761_READ_LE_4(sc, SOTG_HW_MODE_CTRL),
2129 SAF1761_READ_LE_4(sc, SOTG_DCSCRATCH),
2130 SAF1761_READ_LE_4(sc, SOTG_HCSCRATCH));
2132 /* reset device controller */
2133 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_SFRESET);
2134 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2139 /* reset host controller */
2140 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_HCRESET);
2142 /* wait for reset to clear */
2143 for (x = 0; x != 10; x++) {
2144 if ((SAF1761_READ_LE_4(sc, SOTG_USBCMD) & SOTG_USBCMD_HCRESET) == 0)
2146 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
2149 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode |
2150 SOTG_HW_MODE_CTRL_ALL_ATX_RESET);
2155 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2161 saf1761_otg_pull_down(sc);
2163 /* wait 10ms for pulldown to stabilise */
2164 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2168 saf1761_otg_get_hw_ep_profile(NULL, &pf, x);
2172 /* select the correct endpoint */
2173 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2174 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2175 SOTG_EP_INDEX_DIR_IN);
2177 /* select the maximum packet size */
2178 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_in_frame_size);
2180 /* select the correct endpoint */
2181 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2182 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2183 SOTG_EP_INDEX_DIR_OUT);
2185 /* select the maximum packet size */
2186 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_out_frame_size);
2189 /* enable interrupts */
2190 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_GLINTENA |
2191 SOTG_MODE_CLKAON | SOTG_MODE_WKUPCS);
2193 sc->sc_interrupt_cfg |=
2194 SOTG_INTERRUPT_CFG_CDBGMOD |
2195 SOTG_INTERRUPT_CFG_DDBGMODIN |
2196 SOTG_INTERRUPT_CFG_DDBGMODOUT;
2198 /* set default values */
2199 SAF1761_WRITE_LE_4(sc, SOTG_INTERRUPT_CFG, sc->sc_interrupt_cfg);
2201 /* enable VBUS and ID interrupt */
2202 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2203 SOTG_IRQ_ENABLE_CLR(0xFFFF));
2204 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2205 SOTG_IRQ_ENABLE_SET(SOTG_IRQ_ID | SOTG_IRQ_VBUS_VLD));
2207 /* enable interrupts */
2208 sc->sc_intr_enable = SOTG_DCINTERRUPT_IEVBUS |
2209 SOTG_DCINTERRUPT_IEBRST | SOTG_DCINTERRUPT_IESUSP;
2210 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
2213 * Connect ATX port 1 to device controller, select external
2214 * charge pump and driver VBUS to +5V:
2216 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2217 SOTG_CTRL_CLR(0xFFFF));
2218 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2219 SOTG_CTRL_SET(SOTG_CTRL_SW_SEL_HC_DC |
2220 SOTG_CTRL_BDIS_ACON_EN | SOTG_CTRL_SEL_CP_EXT |
2221 SOTG_CTRL_VBUS_DRV));
2223 /* disable device address */
2224 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, 0);
2226 /* enable host controller clock and preserve reserved bits */
2227 x = SAF1761_READ_LE_4(sc, SOTG_POWER_DOWN);
2228 SAF1761_WRITE_LE_4(sc, SOTG_POWER_DOWN, x | SOTG_POWER_DOWN_HC_CLK_EN);
2230 /* wait 10ms for clock */
2231 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2233 /* enable configuration flag */
2234 SAF1761_WRITE_LE_4(sc, SOTG_CONFIGFLAG, SOTG_CONFIGFLAG_ENABLE);
2236 /* clear RAM block */
2237 for (x = 0x400; x != 0x10000; x += 4)
2238 SAF1761_WRITE_LE_4(sc, x, 0);
2241 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_RS);
2243 DPRINTF("USBCMD=0x%08x\n", SAF1761_READ_LE_4(sc, SOTG_USBCMD));
2245 /* make HC scan all PTDs */
2246 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_LAST_PTD, (1 << 31));
2247 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_LAST_PTD, (1 << 31));
2248 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_LAST_PTD, (1 << 31));
2250 /* skip all PTDs by default */
2251 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD, -1U);
2252 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD, -1U);
2253 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD, -1U);
2255 /* activate all PTD types */
2256 SAF1761_WRITE_LE_4(sc, SOTG_HCBUFFERSTATUS,
2257 SOTG_HCBUFFERSTATUS_ISO_BUF_FILL |
2258 SOTG_HCBUFFERSTATUS_INT_BUF_FILL |
2259 SOTG_HCBUFFERSTATUS_ATL_BUF_FILL);
2261 /* we don't use the AND mask */
2262 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_AND, 0);
2263 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_AND, 0);
2264 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_AND, 0);
2266 /* enable all PTD OR interrupts by default */
2267 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_OR, -1U);
2268 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_OR, -1U);
2269 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_OR, -1U);
2271 /* enable HC interrupts */
2272 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT_ENABLE,
2273 SOTG_HCINTERRUPT_OTG_IRQ |
2274 SOTG_HCINTERRUPT_ISO_IRQ |
2275 SOTG_HCINTERRUPT_ALT_IRQ |
2276 SOTG_HCINTERRUPT_INT_IRQ);
2278 /* poll initial VBUS status */
2279 saf1761_otg_update_vbus(sc);
2281 USB_BUS_UNLOCK(&sc->sc_bus);
2283 /* catch any lost interrupts */
2285 saf1761_otg_do_poll(&sc->sc_bus);
2287 return (0); /* success */
2291 saf1761_otg_uninit(struct saf1761_otg_softc *sc)
2293 USB_BUS_LOCK(&sc->sc_bus);
2295 /* disable all interrupts */
2296 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2298 sc->sc_flags.port_powered = 0;
2299 sc->sc_flags.status_vbus = 0;
2300 sc->sc_flags.status_bus_reset = 0;
2301 sc->sc_flags.status_suspend = 0;
2302 sc->sc_flags.change_suspend = 0;
2303 sc->sc_flags.change_connect = 1;
2305 saf1761_otg_pull_down(sc);
2306 USB_BUS_UNLOCK(&sc->sc_bus);
2310 saf1761_otg_suspend(struct saf1761_otg_softc *sc)
2316 saf1761_otg_resume(struct saf1761_otg_softc *sc)
2322 saf1761_otg_do_poll(struct usb_bus *bus)
2324 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
2326 USB_BUS_LOCK(&sc->sc_bus);
2327 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2328 saf1761_otg_interrupt_poll_locked(sc);
2329 saf1761_otg_interrupt_complete_locked(sc);
2330 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2331 USB_BUS_UNLOCK(&sc->sc_bus);
2334 /*------------------------------------------------------------------------*
2335 * saf1761_otg control support
2336 * saf1761_otg interrupt support
2337 * saf1761_otg bulk support
2338 *------------------------------------------------------------------------*/
2340 saf1761_otg_device_non_isoc_open(struct usb_xfer *xfer)
2346 saf1761_otg_device_non_isoc_close(struct usb_xfer *xfer)
2348 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2352 saf1761_otg_device_non_isoc_enter(struct usb_xfer *xfer)
2358 saf1761_otg_device_non_isoc_start(struct usb_xfer *xfer)
2361 saf1761_otg_setup_standard_chain(xfer);
2362 saf1761_otg_start_standard_chain(xfer);
2365 static const struct usb_pipe_methods saf1761_otg_non_isoc_methods =
2367 .open = saf1761_otg_device_non_isoc_open,
2368 .close = saf1761_otg_device_non_isoc_close,
2369 .enter = saf1761_otg_device_non_isoc_enter,
2370 .start = saf1761_otg_device_non_isoc_start,
2373 /*------------------------------------------------------------------------*
2374 * saf1761_otg isochronous support
2375 *------------------------------------------------------------------------*/
2377 saf1761_otg_device_isoc_open(struct usb_xfer *xfer)
2383 saf1761_otg_device_isoc_close(struct usb_xfer *xfer)
2385 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2389 saf1761_otg_device_isoc_enter(struct usb_xfer *xfer)
2391 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
2395 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2396 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2398 /* get the current frame index - we don't need the high bits */
2400 nframes = SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM);
2403 * check if the frame index is within the window where the
2404 * frames will be inserted
2406 temp = (nframes - xfer->endpoint->isoc_next) & SOTG_FRAME_NUM_SOFR_MASK;
2408 if ((xfer->endpoint->is_synced == 0) ||
2409 (temp < xfer->nframes)) {
2411 * If there is data underflow or the pipe queue is
2412 * empty we schedule the transfer a few frames ahead
2413 * of the current frame position. Else two isochronous
2414 * transfers might overlap.
2416 xfer->endpoint->isoc_next = (nframes + 3) & SOTG_FRAME_NUM_SOFR_MASK;
2417 xfer->endpoint->is_synced = 1;
2418 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2421 * compute how many milliseconds the insertion is ahead of the
2422 * current frame position:
2424 temp = (xfer->endpoint->isoc_next - nframes) & SOTG_FRAME_NUM_SOFR_MASK;
2427 * pre-compute when the isochronous transfer will be finished:
2429 xfer->isoc_time_complete =
2430 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2433 /* compute frame number for next insertion */
2434 xfer->endpoint->isoc_next += xfer->nframes;
2437 saf1761_otg_setup_standard_chain(xfer);
2441 saf1761_otg_device_isoc_start(struct usb_xfer *xfer)
2443 /* start TD chain */
2444 saf1761_otg_start_standard_chain(xfer);
2447 static const struct usb_pipe_methods saf1761_otg_device_isoc_methods =
2449 .open = saf1761_otg_device_isoc_open,
2450 .close = saf1761_otg_device_isoc_close,
2451 .enter = saf1761_otg_device_isoc_enter,
2452 .start = saf1761_otg_device_isoc_start,
2455 /*------------------------------------------------------------------------*
2456 * saf1761_otg root control support
2457 *------------------------------------------------------------------------*
2458 * Simulate a hardware HUB by handling all the necessary requests.
2459 *------------------------------------------------------------------------*/
2461 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2463 static const struct usb_device_descriptor saf1761_otg_devd = {
2464 .bLength = sizeof(struct usb_device_descriptor),
2465 .bDescriptorType = UDESC_DEVICE,
2466 HSETW(.idVendor, 0x04cc),
2467 HSETW(.idProduct, 0x1761),
2468 .bcdUSB = {0x00, 0x02},
2469 .bDeviceClass = UDCLASS_HUB,
2470 .bDeviceSubClass = UDSUBCLASS_HUB,
2471 .bDeviceProtocol = UDPROTO_FSHUB,
2472 .bMaxPacketSize = 64,
2473 .bcdDevice = {0x00, 0x01},
2476 .bNumConfigurations = 1,
2479 static const struct usb_device_qualifier saf1761_otg_odevd = {
2480 .bLength = sizeof(struct usb_device_qualifier),
2481 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
2482 .bcdUSB = {0x00, 0x02},
2483 .bDeviceClass = UDCLASS_HUB,
2484 .bDeviceSubClass = UDSUBCLASS_HUB,
2485 .bDeviceProtocol = UDPROTO_FSHUB,
2486 .bMaxPacketSize0 = 0,
2487 .bNumConfigurations = 0,
2490 static const struct saf1761_otg_config_desc saf1761_otg_confd = {
2492 .bLength = sizeof(struct usb_config_descriptor),
2493 .bDescriptorType = UDESC_CONFIG,
2494 .wTotalLength[0] = sizeof(saf1761_otg_confd),
2496 .bConfigurationValue = 1,
2497 .iConfiguration = 0,
2498 .bmAttributes = UC_SELF_POWERED,
2502 .bLength = sizeof(struct usb_interface_descriptor),
2503 .bDescriptorType = UDESC_INTERFACE,
2505 .bInterfaceClass = UICLASS_HUB,
2506 .bInterfaceSubClass = UISUBCLASS_HUB,
2507 .bInterfaceProtocol = 0,
2511 .bLength = sizeof(struct usb_endpoint_descriptor),
2512 .bDescriptorType = UDESC_ENDPOINT,
2513 .bEndpointAddress = (UE_DIR_IN | SAF1761_OTG_INTR_ENDPT),
2514 .bmAttributes = UE_INTERRUPT,
2515 .wMaxPacketSize[0] = 8,
2520 static const struct usb_hub_descriptor_min saf1761_otg_hubd = {
2521 .bDescLength = sizeof(saf1761_otg_hubd),
2522 .bDescriptorType = UDESC_HUB,
2523 .bNbrPorts = SOTG_NUM_PORTS,
2524 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
2525 .bPwrOn2PwrGood = 50,
2526 .bHubContrCurrent = 0,
2527 .DeviceRemovable = {0}, /* port is removable */
2530 #define STRING_VENDOR \
2533 #define STRING_PRODUCT \
2534 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
2536 USB_MAKE_STRING_DESC(STRING_VENDOR, saf1761_otg_vendor);
2537 USB_MAKE_STRING_DESC(STRING_PRODUCT, saf1761_otg_product);
2540 saf1761_otg_roothub_exec(struct usb_device *udev,
2541 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2543 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(udev->bus);
2552 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2555 ptr = (const void *)&sc->sc_hub_temp;
2559 value = UGETW(req->wValue);
2560 index = UGETW(req->wIndex);
2562 /* demultiplex the control request */
2564 switch (req->bmRequestType) {
2565 case UT_READ_DEVICE:
2566 switch (req->bRequest) {
2567 case UR_GET_DESCRIPTOR:
2568 goto tr_handle_get_descriptor;
2570 goto tr_handle_get_config;
2572 goto tr_handle_get_status;
2578 case UT_WRITE_DEVICE:
2579 switch (req->bRequest) {
2580 case UR_SET_ADDRESS:
2581 goto tr_handle_set_address;
2583 goto tr_handle_set_config;
2584 case UR_CLEAR_FEATURE:
2585 goto tr_valid; /* nop */
2586 case UR_SET_DESCRIPTOR:
2587 goto tr_valid; /* nop */
2588 case UR_SET_FEATURE:
2594 case UT_WRITE_ENDPOINT:
2595 switch (req->bRequest) {
2596 case UR_CLEAR_FEATURE:
2597 switch (UGETW(req->wValue)) {
2598 case UF_ENDPOINT_HALT:
2599 goto tr_handle_clear_halt;
2600 case UF_DEVICE_REMOTE_WAKEUP:
2601 goto tr_handle_clear_wakeup;
2606 case UR_SET_FEATURE:
2607 switch (UGETW(req->wValue)) {
2608 case UF_ENDPOINT_HALT:
2609 goto tr_handle_set_halt;
2610 case UF_DEVICE_REMOTE_WAKEUP:
2611 goto tr_handle_set_wakeup;
2616 case UR_SYNCH_FRAME:
2617 goto tr_valid; /* nop */
2623 case UT_READ_ENDPOINT:
2624 switch (req->bRequest) {
2626 goto tr_handle_get_ep_status;
2632 case UT_WRITE_INTERFACE:
2633 switch (req->bRequest) {
2634 case UR_SET_INTERFACE:
2635 goto tr_handle_set_interface;
2636 case UR_CLEAR_FEATURE:
2637 goto tr_valid; /* nop */
2638 case UR_SET_FEATURE:
2644 case UT_READ_INTERFACE:
2645 switch (req->bRequest) {
2646 case UR_GET_INTERFACE:
2647 goto tr_handle_get_interface;
2649 goto tr_handle_get_iface_status;
2655 case UT_WRITE_CLASS_INTERFACE:
2656 case UT_WRITE_VENDOR_INTERFACE:
2660 case UT_READ_CLASS_INTERFACE:
2661 case UT_READ_VENDOR_INTERFACE:
2665 case UT_WRITE_CLASS_DEVICE:
2666 switch (req->bRequest) {
2667 case UR_CLEAR_FEATURE:
2669 case UR_SET_DESCRIPTOR:
2670 case UR_SET_FEATURE:
2677 case UT_WRITE_CLASS_OTHER:
2678 switch (req->bRequest) {
2679 case UR_CLEAR_FEATURE:
2680 if (index == SOTG_HOST_PORT_NUM)
2681 goto tr_handle_clear_port_feature_host;
2682 else if (index == SOTG_DEVICE_PORT_NUM)
2683 goto tr_handle_clear_port_feature_device;
2686 case UR_SET_FEATURE:
2687 if (index == SOTG_HOST_PORT_NUM)
2688 goto tr_handle_set_port_feature_host;
2689 else if (index == SOTG_DEVICE_PORT_NUM)
2690 goto tr_handle_set_port_feature_device;
2693 case UR_CLEAR_TT_BUFFER:
2703 case UT_READ_CLASS_OTHER:
2704 switch (req->bRequest) {
2705 case UR_GET_TT_STATE:
2706 goto tr_handle_get_tt_state;
2708 if (index == SOTG_HOST_PORT_NUM)
2709 goto tr_handle_get_port_status_host;
2710 else if (index == SOTG_DEVICE_PORT_NUM)
2711 goto tr_handle_get_port_status_device;
2719 case UT_READ_CLASS_DEVICE:
2720 switch (req->bRequest) {
2721 case UR_GET_DESCRIPTOR:
2722 goto tr_handle_get_class_descriptor;
2724 goto tr_handle_get_class_status;
2735 tr_handle_get_descriptor:
2736 switch (value >> 8) {
2740 len = sizeof(saf1761_otg_devd);
2741 ptr = (const void *)&saf1761_otg_devd;
2743 case UDESC_DEVICE_QUALIFIER:
2746 len = sizeof(saf1761_otg_odevd);
2747 ptr = (const void *)&saf1761_otg_odevd;
2752 len = sizeof(saf1761_otg_confd);
2753 ptr = (const void *)&saf1761_otg_confd;
2756 switch (value & 0xff) {
2757 case 0: /* Language table */
2758 len = sizeof(usb_string_lang_en);
2759 ptr = (const void *)&usb_string_lang_en;
2762 case 1: /* Vendor */
2763 len = sizeof(saf1761_otg_vendor);
2764 ptr = (const void *)&saf1761_otg_vendor;
2767 case 2: /* Product */
2768 len = sizeof(saf1761_otg_product);
2769 ptr = (const void *)&saf1761_otg_product;
2780 tr_handle_get_config:
2782 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2785 tr_handle_get_status:
2787 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2790 tr_handle_set_address:
2794 sc->sc_rt_addr = value;
2797 tr_handle_set_config:
2800 sc->sc_conf = value;
2803 tr_handle_get_interface:
2805 sc->sc_hub_temp.wValue[0] = 0;
2808 tr_handle_get_tt_state:
2809 tr_handle_get_class_status:
2810 tr_handle_get_iface_status:
2811 tr_handle_get_ep_status:
2813 USETW(sc->sc_hub_temp.wValue, 0);
2817 tr_handle_set_interface:
2818 tr_handle_set_wakeup:
2819 tr_handle_clear_wakeup:
2820 tr_handle_clear_halt:
2823 tr_handle_clear_port_feature_device:
2824 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
2827 case UHF_PORT_SUSPEND:
2828 saf1761_otg_wakeup_peer(sc);
2831 case UHF_PORT_ENABLE:
2832 sc->sc_flags.port_enabled = 0;
2836 case UHF_PORT_INDICATOR:
2837 case UHF_C_PORT_ENABLE:
2838 case UHF_C_PORT_OVER_CURRENT:
2839 case UHF_C_PORT_RESET:
2842 case UHF_PORT_POWER:
2843 sc->sc_flags.port_powered = 0;
2844 saf1761_otg_pull_down(sc);
2846 case UHF_C_PORT_CONNECTION:
2847 sc->sc_flags.change_connect = 0;
2849 case UHF_C_PORT_SUSPEND:
2850 sc->sc_flags.change_suspend = 0;
2853 err = USB_ERR_IOERROR;
2858 tr_handle_clear_port_feature_host:
2859 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
2861 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2864 case UHF_PORT_ENABLE:
2865 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PED);
2867 case UHF_PORT_SUSPEND:
2868 if ((temp & SOTG_PORTSC1_SUSP) && (!(temp & SOTG_PORTSC1_FPR)))
2869 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_FPR);
2871 /* wait 20ms for resume sequence to complete */
2872 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2874 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~(SOTG_PORTSC1_SUSP |
2875 SOTG_PORTSC1_FPR | SOTG_PORTSC1_LS /* High Speed */ ));
2877 /* 4ms settle time */
2878 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
2880 case UHF_PORT_INDICATOR:
2881 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PIC);
2884 case UHF_C_PORT_ENABLE:
2885 case UHF_C_PORT_OVER_CURRENT:
2886 case UHF_C_PORT_RESET:
2887 case UHF_C_PORT_SUSPEND:
2890 case UHF_PORT_POWER:
2891 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PP);
2893 case UHF_C_PORT_CONNECTION:
2894 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_ECSC);
2897 err = USB_ERR_IOERROR;
2902 tr_handle_set_port_feature_device:
2903 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
2906 case UHF_PORT_ENABLE:
2907 sc->sc_flags.port_enabled = 1;
2909 case UHF_PORT_SUSPEND:
2910 case UHF_PORT_RESET:
2912 case UHF_PORT_INDICATOR:
2915 case UHF_PORT_POWER:
2916 sc->sc_flags.port_powered = 1;
2919 err = USB_ERR_IOERROR;
2924 tr_handle_set_port_feature_host:
2925 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
2927 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2930 case UHF_PORT_ENABLE:
2931 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PED);
2933 case UHF_PORT_SUSPEND:
2934 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_SUSP);
2936 case UHF_PORT_RESET:
2937 DPRINTFN(6, "reset port %d\n", index);
2939 /* Start reset sequence. */
2940 temp &= ~(SOTG_PORTSC1_PED | SOTG_PORTSC1_PR);
2942 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PR);
2944 /* Wait for reset to complete. */
2945 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2946 USB_MS_TO_TICKS(usb_port_root_reset_delay));
2948 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp);
2950 /* Wait for HC to complete reset. */
2951 usb_pause_mtx(&sc->sc_bus.bus_mtx, USB_MS_TO_TICKS(2));
2953 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2955 DPRINTF("After reset, status=0x%08x\n", temp);
2956 if (temp & SOTG_PORTSC1_PR) {
2957 device_printf(sc->sc_bus.bdev, "port reset timeout\n");
2958 err = USB_ERR_TIMEOUT;
2961 if (!(temp & SOTG_PORTSC1_PED)) {
2962 /* Not a high speed device, give up ownership.*/
2963 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PO);
2967 DPRINTF("port %d reset, status = 0x%08x\n", index, temp);
2969 case UHF_PORT_POWER:
2970 DPRINTFN(3, "set port power %d\n", index);
2971 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PP);
2975 DPRINTFN(3, "set port test %d\n", index);
2978 case UHF_PORT_INDICATOR:
2979 DPRINTFN(3, "set port ind %d\n", index);
2980 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PIC);
2983 err = USB_ERR_IOERROR;
2988 tr_handle_get_port_status_device:
2990 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d\n", index);
2992 if (sc->sc_flags.status_vbus) {
2993 saf1761_otg_pull_up(sc);
2995 saf1761_otg_pull_down(sc);
2998 /* Select FULL-speed and Device Side Mode */
3000 value = UPS_PORT_MODE_DEVICE;
3002 if (sc->sc_flags.port_powered)
3003 value |= UPS_PORT_POWER;
3005 if (sc->sc_flags.port_enabled)
3006 value |= UPS_PORT_ENABLED;
3008 if (sc->sc_flags.status_vbus &&
3009 sc->sc_flags.status_bus_reset)
3010 value |= UPS_CURRENT_CONNECT_STATUS;
3012 if (sc->sc_flags.status_suspend)
3013 value |= UPS_SUSPEND;
3015 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
3019 if (sc->sc_flags.change_connect)
3020 value |= UPS_C_CONNECT_STATUS;
3022 if (sc->sc_flags.change_suspend)
3023 value |= UPS_C_SUSPEND;
3025 USETW(sc->sc_hub_temp.ps.wPortChange, value);
3026 len = sizeof(sc->sc_hub_temp.ps);
3029 tr_handle_get_port_status_host:
3031 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3033 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d = 0x%08x\n", index, temp);
3037 if (temp & SOTG_PORTSC1_ECCS)
3038 i |= UPS_CURRENT_CONNECT_STATUS;
3039 if (temp & SOTG_PORTSC1_PED)
3040 i |= UPS_PORT_ENABLED;
3041 if ((temp & SOTG_PORTSC1_SUSP) && !(temp & SOTG_PORTSC1_FPR))
3043 if (temp & SOTG_PORTSC1_PR)
3045 if (temp & SOTG_PORTSC1_PP)
3046 i |= UPS_PORT_POWER;
3048 USETW(sc->sc_hub_temp.ps.wPortStatus, i);
3051 if (temp & SOTG_PORTSC1_ECSC)
3052 i |= UPS_C_CONNECT_STATUS;
3053 if (temp & SOTG_PORTSC1_FPR)
3056 i |= UPS_C_PORT_RESET;
3057 USETW(sc->sc_hub_temp.ps.wPortChange, i);
3058 len = sizeof(sc->sc_hub_temp.ps);
3061 tr_handle_get_class_descriptor:
3064 ptr = (const void *)&saf1761_otg_hubd;
3065 len = sizeof(saf1761_otg_hubd);
3069 err = USB_ERR_STALLED;
3077 saf1761_otg_xfer_setup(struct usb_setup_params *parm)
3079 struct saf1761_otg_softc *sc;
3080 struct usb_xfer *xfer;
3088 sc = SAF1761_OTG_BUS2SC(parm->udev->bus);
3089 xfer = parm->curr_xfer;
3092 * NOTE: This driver does not use any of the parameters that
3093 * are computed from the following values. Just set some
3094 * reasonable dummies:
3096 parm->hc_max_packet_size = 0x500;
3097 parm->hc_max_packet_count = 1;
3098 parm->hc_max_frame_size = 0x500;
3100 usbd_transfer_setup_sub(parm);
3103 * Compute maximum number of TDs:
3105 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
3107 if (ep_type == UE_CONTROL) {
3109 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
3112 ntd = xfer->nframes + 1 /* SYNC */ ;
3116 * check if "usbd_transfer_setup_sub" set an error
3122 * allocate transfer descriptors
3126 ep_no = xfer->endpointno & UE_ADDR;
3129 * Check profile stuff
3131 if (parm->udev->flags.usb_mode == USB_MODE_DEVICE) {
3132 const struct usb_hw_ep_profile *pf;
3134 saf1761_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
3137 /* should not happen */
3138 parm->err = USB_ERR_INVAL;
3143 dw1 = (xfer->address << 3) | (ep_type << 12);
3145 switch (parm->udev->speed) {
3146 case USB_SPEED_FULL:
3148 /* check if root HUB port is running High Speed */
3149 if (parm->udev->parent_hs_hub != NULL) {
3150 dw1 |= SOTG_PTD_DW1_ENABLE_SPLIT;
3151 dw1 |= (parm->udev->hs_port_no << 18);
3152 dw1 |= (parm->udev->hs_hub_addr << 25);
3153 if (parm->udev->speed == USB_SPEED_LOW)
3162 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
3164 for (n = 0; n != ntd; n++) {
3166 struct saf1761_otg_td *td;
3170 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
3173 td->max_packet_size = xfer->max_packet_size;
3174 td->ep_index = ep_no;
3175 td->ep_type = ep_type;
3176 td->dw1_value = dw1;
3178 if (ep_type == UE_INTERRUPT) {
3179 if (xfer->interval > 32)
3180 td->interval = (32 / 2) << 3;
3182 td->interval = (xfer->interval / 2) << 3;
3186 td->obj_next = last_obj;
3190 parm->size[0] += sizeof(*td);
3193 xfer->td_start[0] = last_obj;
3197 saf1761_otg_xfer_unsetup(struct usb_xfer *xfer)
3202 saf1761_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3203 struct usb_endpoint *ep)
3205 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3207 edesc->bEndpointAddress, udev->flags.usb_mode);
3209 if (udev->parent_hub == NULL) {
3210 /* root HUB has special endpoint handling */
3214 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3215 if (udev->speed != USB_SPEED_FULL &&
3216 udev->speed != USB_SPEED_HIGH) {
3220 switch (edesc->bmAttributes & UE_XFERTYPE) {
3221 case UE_ISOCHRONOUS:
3222 ep->methods = &saf1761_otg_device_isoc_methods;
3225 ep->methods = &saf1761_otg_non_isoc_methods;
3229 switch (edesc->bmAttributes & UE_XFERTYPE) {
3233 ep->methods = &saf1761_otg_non_isoc_methods;
3243 saf1761_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3245 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
3248 case USB_HW_POWER_SUSPEND:
3249 saf1761_otg_suspend(sc);
3251 case USB_HW_POWER_SHUTDOWN:
3252 saf1761_otg_uninit(sc);
3254 case USB_HW_POWER_RESUME:
3255 saf1761_otg_resume(sc);
3263 saf1761_otg_device_resume(struct usb_device *udev)
3265 struct saf1761_otg_softc *sc;
3266 struct saf1761_otg_td *td;
3267 struct usb_xfer *xfer;
3272 if (udev->flags.usb_mode != USB_MODE_HOST)
3275 sc = SAF1761_OTG_BUS2SC(udev->bus);
3277 USB_BUS_LOCK(&sc->sc_bus);
3278 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3280 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3282 if (xfer->xroot->udev != udev)
3285 td = xfer->td_transfer_cache;
3286 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3289 switch (td->ep_type) {
3291 x = td->channel - 32;
3292 sc->sc_host_intr_suspend_map &= ~(1 << x);
3293 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3294 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3296 case UE_ISOCHRONOUS:
3298 sc->sc_host_isoc_suspend_map &= ~(1 << x);
3299 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3300 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3303 x = td->channel - 64;
3304 sc->sc_host_async_suspend_map &= ~(1 << x);
3305 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3306 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3311 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3312 USB_BUS_UNLOCK(&sc->sc_bus);
3314 /* poll all transfers again to restart resumed ones */
3315 saf1761_otg_do_poll(&sc->sc_bus);
3319 saf1761_otg_device_suspend(struct usb_device *udev)
3321 struct saf1761_otg_softc *sc;
3322 struct saf1761_otg_td *td;
3323 struct usb_xfer *xfer;
3328 if (udev->flags.usb_mode != USB_MODE_HOST)
3331 sc = SAF1761_OTG_BUS2SC(udev->bus);
3333 USB_BUS_LOCK(&sc->sc_bus);
3334 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3336 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3338 if (xfer->xroot->udev != udev)
3341 td = xfer->td_transfer_cache;
3342 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3345 switch (td->ep_type) {
3347 x = td->channel - 32;
3348 sc->sc_host_intr_suspend_map |= (1 << x);
3349 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3350 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3352 case UE_ISOCHRONOUS:
3354 sc->sc_host_isoc_suspend_map |= (1 << x);
3355 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3356 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3359 x = td->channel - 64;
3360 sc->sc_host_async_suspend_map |= (1 << x);
3361 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3362 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3367 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3368 USB_BUS_UNLOCK(&sc->sc_bus);
3371 static const struct usb_bus_methods saf1761_otg_bus_methods =
3373 .endpoint_init = &saf1761_otg_ep_init,
3374 .xfer_setup = &saf1761_otg_xfer_setup,
3375 .xfer_unsetup = &saf1761_otg_xfer_unsetup,
3376 .get_hw_ep_profile = &saf1761_otg_get_hw_ep_profile,
3377 .xfer_stall = &saf1761_otg_xfer_stall,
3378 .set_stall = &saf1761_otg_set_stall,
3379 .clear_stall = &saf1761_otg_clear_stall,
3380 .roothub_exec = &saf1761_otg_roothub_exec,
3381 .xfer_poll = &saf1761_otg_do_poll,
3382 .set_hw_power_sleep = saf1761_otg_set_hw_power_sleep,
3383 .device_resume = &saf1761_otg_device_resume,
3384 .device_suspend = &saf1761_otg_device_suspend,