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_memory_le_4(struct saf1761_otg_softc *sc, uint32_t offset)
296 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
297 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
298 return (SAF1761_READ_LE_4(sc, offset));
302 saf1761_read_host_memory(struct saf1761_otg_softc *sc,
303 struct saf1761_otg_td *td, uint32_t len)
305 struct usb_page_search buf_res;
312 offset = SOTG_DATA_ADDR(td->channel);
313 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
314 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
316 /* optimised read first */
318 usbd_get_page(td->pc, td->offset, &buf_res);
320 /* get correct length */
321 if (buf_res.length > len)
322 buf_res.length = len;
324 /* check buffer alignment */
325 if (((uintptr_t)buf_res.buffer) & 3)
328 count = buf_res.length & ~3;
332 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
333 offset, buf_res.buffer, count / 4);
338 /* update remainder and offset */
339 td->remainder -= count;
344 /* use bounce buffer */
345 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
346 offset, sc->sc_bounce_buffer, (len + 3) / 4);
347 usbd_copy_in(td->pc, td->offset,
348 sc->sc_bounce_buffer, len);
350 /* update remainder and offset */
351 td->remainder -= len;
357 saf1761_write_host_memory(struct saf1761_otg_softc *sc,
358 struct saf1761_otg_td *td, uint32_t len)
360 struct usb_page_search buf_res;
367 offset = SOTG_DATA_ADDR(td->channel);
369 /* optimised write first */
371 usbd_get_page(td->pc, td->offset, &buf_res);
373 /* get correct length */
374 if (buf_res.length > len)
375 buf_res.length = len;
377 /* check buffer alignment */
378 if (((uintptr_t)buf_res.buffer) & 3)
381 count = buf_res.length & ~3;
385 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
386 offset, buf_res.buffer, count / 4);
391 /* update remainder and offset */
392 td->remainder -= count;
396 /* use bounce buffer */
397 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
398 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
399 offset, sc->sc_bounce_buffer, (len + 3) / 4);
401 /* update remainder and offset */
402 td->remainder -= len;
408 saf1761_host_setup_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
415 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
416 pdt_addr = SOTG_PTD(td->channel);
418 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
420 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
422 DPRINTFN(5, "STATUS=0x%08x\n", status);
424 if (status & SOTG_PTD_DW3_ACTIVE) {
426 } else if (status & SOTG_PTD_DW3_HALTED) {
431 if (saf1761_host_channel_alloc(sc, td))
436 if (count != td->remainder) {
441 saf1761_write_host_memory(sc, td, count);
443 pdt_addr = SOTG_PTD(td->channel);
445 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
446 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
447 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
448 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
450 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
451 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
453 temp = SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8;
454 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
456 temp = td->dw1_value | (2 << 10) /* SETUP PID */ | (td->ep_index >> 1);
457 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
459 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
460 (td->max_packet_size << 18) /* wMaxPacketSize */ |
461 (count << 3) /* transfer count */ |
463 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
466 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
467 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
471 return (1); /* busy */
473 saf1761_host_channel_free(sc, td);
474 return (0); /* complete */
478 saf1761_host_bulk_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
483 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
488 pdt_addr = SOTG_PTD(td->channel);
490 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
492 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
494 DPRINTFN(5, "STATUS=0x%08x\n", status);
496 if (status & SOTG_PTD_DW3_ACTIVE) {
498 } else if (status & SOTG_PTD_DW3_HALTED) {
499 if (!(status & SOTG_PTD_DW3_ERRORS))
504 count = (status & SOTG_PTD_DW3_XFER_COUNT);
507 /* verify the packet byte count */
508 if (count != td->max_packet_size) {
509 if (count < td->max_packet_size) {
510 /* we have a short packet */
514 /* invalid USB packet */
521 /* verify the packet byte count */
522 if (count > td->remainder) {
523 /* invalid USB packet */
528 saf1761_read_host_memory(sc, td, count);
530 /* check if we are complete */
531 if ((td->remainder == 0) || got_short) {
534 /* else need to receive a zero length packet */
536 saf1761_host_channel_free(sc, td);
538 if (saf1761_host_channel_alloc(sc, td))
541 /* set toggle, if any */
542 if (td->set_toggle) {
547 /* receive one more packet */
549 pdt_addr = SOTG_PTD(td->channel);
551 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
552 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
553 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
554 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
556 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
558 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
560 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
561 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
563 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
564 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
566 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
567 (td->max_packet_size << 18) /* wMaxPacketSize */ |
568 (td->max_packet_size << 3) /* transfer count */ |
570 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
573 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
574 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
576 return (1); /* busy */
578 saf1761_host_channel_free(sc, td);
579 return (0); /* complete */
583 saf1761_host_bulk_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
589 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
592 pdt_addr = SOTG_PTD(td->channel);
594 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
596 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
598 DPRINTFN(5, "STATUS=0x%08x\n", status);
600 if (status & SOTG_PTD_DW3_ACTIVE) {
602 } else if (status & SOTG_PTD_DW3_HALTED) {
603 if (!(status & SOTG_PTD_DW3_ERRORS))
608 /* check remainder */
609 if (td->remainder == 0) {
612 /* else we need to transmit a short packet */
614 saf1761_host_channel_free(sc, td);
616 if (saf1761_host_channel_alloc(sc, td))
619 count = td->max_packet_size;
620 if (td->remainder < count) {
621 /* we have a short packet */
623 count = td->remainder;
626 saf1761_write_host_memory(sc, td, count);
628 /* set toggle, if any */
629 if (td->set_toggle) {
634 /* send one more packet */
636 pdt_addr = SOTG_PTD(td->channel);
638 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
639 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
640 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
641 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
643 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
645 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
647 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
648 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
650 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
651 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
653 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
654 (td->max_packet_size << 18) /* wMaxPacketSize */ |
655 (count << 3) /* transfer count */ |
657 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
660 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
661 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
665 return (1); /* busy */
667 saf1761_host_channel_free(sc, td);
668 return (0); /* complete */
672 saf1761_host_intr_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
677 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
682 pdt_addr = SOTG_PTD(td->channel);
684 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
686 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
688 DPRINTFN(5, "STATUS=0x%08x\n", status);
690 if (status & SOTG_PTD_DW3_ACTIVE) {
692 } else if (status & SOTG_PTD_DW3_HALTED) {
693 if (!(status & SOTG_PTD_DW3_ERRORS))
698 count = (status & SOTG_PTD_DW3_XFER_COUNT);
701 /* verify the packet byte count */
702 if (count != td->max_packet_size) {
703 if (count < td->max_packet_size) {
704 /* we have a short packet */
708 /* invalid USB packet */
715 /* verify the packet byte count */
716 if (count > td->remainder) {
717 /* invalid USB packet */
722 saf1761_read_host_memory(sc, td, count);
724 /* check if we are complete */
725 if ((td->remainder == 0) || got_short) {
728 /* else need to receive a zero length packet */
730 saf1761_host_channel_free(sc, td);
732 if (saf1761_host_channel_alloc(sc, td))
735 /* set toggle, if any */
736 if (td->set_toggle) {
741 /* receive one more packet */
743 pdt_addr = SOTG_PTD(td->channel);
745 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
746 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
748 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
749 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
751 temp = (1U << td->uframe); /* start split */
752 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
754 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
755 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
757 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) | (td->interval & 0xF8);
758 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
760 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
761 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
763 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
764 (td->max_packet_size << 18) /* wMaxPacketSize */ |
765 (td->max_packet_size << 3) /* transfer count */ |
767 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
770 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
771 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
773 return (1); /* busy */
775 saf1761_host_channel_free(sc, td);
776 return (0); /* complete */
780 saf1761_host_intr_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
786 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
789 pdt_addr = SOTG_PTD(td->channel);
791 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
793 status = saf1761_peek_host_memory_le_4(sc, pdt_addr + SOTG_PTD_DW3);
795 DPRINTFN(5, "STATUS=0x%08x\n", status);
797 if (status & SOTG_PTD_DW3_ACTIVE) {
799 } else if (status & SOTG_PTD_DW3_HALTED) {
800 if (!(status & SOTG_PTD_DW3_ERRORS))
806 /* check remainder */
807 if (td->remainder == 0) {
810 /* else we need to transmit a short packet */
812 saf1761_host_channel_free(sc, td);
814 if (saf1761_host_channel_alloc(sc, td))
817 count = td->max_packet_size;
818 if (td->remainder < count) {
819 /* we have a short packet */
821 count = td->remainder;
824 saf1761_write_host_memory(sc, td, count);
826 /* set toggle, if any */
827 if (td->set_toggle) {
832 /* send one more packet */
834 pdt_addr = SOTG_PTD(td->channel);
836 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
837 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
839 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
840 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
842 temp = (1U << td->uframe); /* start split */
843 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
845 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
846 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
848 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) | (td->interval & 0xF8);
849 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
851 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
852 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
854 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
855 (td->max_packet_size << 18) /* wMaxPacketSize */ |
856 (count << 3) /* transfer count */ |
858 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
861 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
862 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
866 return (1); /* busy */
868 saf1761_host_channel_free(sc, td);
869 return (0); /* complete */
873 saf1761_host_isoc_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
876 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
877 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
879 return (1); /* busy */
883 saf1761_host_isoc_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
886 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
887 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
889 return (1); /* busy */
893 saf1761_otg_set_address(struct saf1761_otg_softc *sc, uint8_t addr)
895 DPRINTFN(5, "addr=%d\n", addr);
897 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, addr | SOTG_ADDRESS_ENABLE);
902 saf1761_read_device_fifo(struct saf1761_otg_softc *sc,
903 struct saf1761_otg_td *td, uint32_t len)
905 struct usb_page_search buf_res;
908 /* optimised read first */
910 usbd_get_page(td->pc, td->offset, &buf_res);
912 /* get correct length */
913 if (buf_res.length > len)
914 buf_res.length = len;
916 /* check buffer alignment */
917 if (((uintptr_t)buf_res.buffer) & 3)
920 count = buf_res.length & ~3;
924 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
925 SOTG_DATA_PORT, buf_res.buffer, count / 4);
929 /* update remainder and offset */
930 td->remainder -= count;
935 /* use bounce buffer */
936 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
937 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
938 usbd_copy_in(td->pc, td->offset,
939 sc->sc_bounce_buffer, len);
941 /* update remainder and offset */
942 td->remainder -= len;
948 saf1761_write_device_fifo(struct saf1761_otg_softc *sc,
949 struct saf1761_otg_td *td, uint32_t len)
951 struct usb_page_search buf_res;
954 /* optimised write first */
956 usbd_get_page(td->pc, td->offset, &buf_res);
958 /* get correct length */
959 if (buf_res.length > len)
960 buf_res.length = len;
962 /* check buffer alignment */
963 if (((uintptr_t)buf_res.buffer) & 3)
966 count = buf_res.length & ~3;
970 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
971 SOTG_DATA_PORT, buf_res.buffer, count / 4);
975 /* update remainder and offset */
976 td->remainder -= count;
980 /* use bounce buffer */
981 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
982 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
983 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
985 /* update remainder and offset */
986 td->remainder -= len;
992 saf1761_device_setup_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
994 struct usb_device_request req;
997 /* select the correct endpoint */
998 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1000 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1002 /* check buffer status */
1003 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1006 /* get buffer length */
1007 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1009 DPRINTFN(5, "count=%u rem=%u\n", count, td->remainder);
1011 /* clear did stall */
1015 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
1017 /* verify data length */
1018 if (count != td->remainder) {
1019 DPRINTFN(0, "Invalid SETUP packet "
1020 "length, %d bytes\n", count);
1023 if (count != sizeof(req)) {
1024 DPRINTFN(0, "Unsupported SETUP packet "
1025 "length, %d bytes\n", count);
1029 saf1761_read_device_fifo(sc, td, sizeof(req));
1031 /* extract SETUP packet again */
1032 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1034 /* sneak peek the set address request */
1035 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1036 (req.bRequest == UR_SET_ADDRESS)) {
1037 sc->sc_dv_addr = req.wValue[0] & 0x7F;
1038 DPRINTF("Set address %d\n", sc->sc_dv_addr);
1040 sc->sc_dv_addr = 0xFF;
1042 return (0); /* complete */
1045 /* abort any ongoing transfer */
1046 if (!td->did_stall) {
1047 DPRINTFN(5, "stalling\n");
1050 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
1054 return (1); /* not complete */
1058 saf1761_device_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1061 uint8_t got_short = 0;
1063 if (td->ep_index == 0) {
1064 /* select the correct endpoint */
1065 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1067 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1069 /* check buffer status */
1070 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1072 if (td->remainder == 0) {
1074 * We are actually complete and have
1075 * received the next SETUP:
1077 DPRINTFN(5, "faking complete\n");
1078 return (0); /* complete */
1080 DPRINTFN(5, "SETUP packet while receiving data\n");
1082 * USB Host Aborted the transfer.
1085 return (0); /* complete */
1088 /* select the correct endpoint */
1089 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1090 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1091 SOTG_EP_INDEX_DIR_OUT);
1093 /* enable data stage */
1094 if (td->set_toggle) {
1096 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1099 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1101 /* check buffer status */
1102 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1103 return (1); /* not complete */
1105 /* get buffer length */
1106 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1108 DPRINTFN(5, "rem=%u count=0x%04x\n", td->remainder, count);
1110 /* verify the packet byte count */
1111 if (count != td->max_packet_size) {
1112 if (count < td->max_packet_size) {
1113 /* we have a short packet */
1117 /* invalid USB packet */
1119 return (0); /* we are complete */
1122 /* verify the packet byte count */
1123 if (count > td->remainder) {
1124 /* invalid USB packet */
1126 return (0); /* we are complete */
1129 saf1761_read_device_fifo(sc, td, count);
1131 /* check if we are complete */
1132 if ((td->remainder == 0) || got_short) {
1133 if (td->short_pkt) {
1134 /* we are complete */
1137 /* else need to receive a zero length packet */
1139 return (1); /* not complete */
1143 saf1761_device_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1147 if (td->ep_index == 0) {
1148 /* select the correct endpoint */
1149 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1151 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1153 /* check buffer status */
1154 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1155 DPRINTFN(5, "SETUP abort\n");
1157 * USB Host Aborted the transfer.
1160 return (0); /* complete */
1163 /* select the correct endpoint */
1164 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1165 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1166 SOTG_EP_INDEX_DIR_IN);
1168 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1170 /* check buffer status */
1171 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1172 return (1); /* not complete */
1174 /* enable data stage */
1175 if (td->set_toggle) {
1177 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1180 DPRINTFN(5, "rem=%u\n", td->remainder);
1182 count = td->max_packet_size;
1183 if (td->remainder < count) {
1184 /* we have a short packet */
1186 count = td->remainder;
1189 saf1761_write_device_fifo(sc, td, count);
1191 if (td->ep_index == 0) {
1192 if (count < SOTG_FS_MAX_PACKET_SIZE) {
1193 /* set end of packet */
1194 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1197 if (count < SOTG_HS_MAX_PACKET_SIZE) {
1198 /* set end of packet */
1199 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1203 /* check remainder */
1204 if (td->remainder == 0) {
1205 if (td->short_pkt) {
1206 return (0); /* complete */
1208 /* else we need to transmit a short packet */
1210 return (1); /* not complete */
1214 saf1761_device_data_tx_sync(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1218 if (td->ep_index == 0) {
1219 /* select the correct endpoint */
1220 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1222 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1224 /* check buffer status */
1225 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1226 DPRINTFN(5, "Faking complete\n");
1227 return (0); /* complete */
1230 /* select the correct endpoint */
1231 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1232 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1233 SOTG_EP_INDEX_DIR_IN);
1235 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1237 /* check buffer status */
1238 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1239 return (1); /* busy */
1241 if (sc->sc_dv_addr != 0xFF) {
1242 /* write function address */
1243 saf1761_otg_set_address(sc, sc->sc_dv_addr);
1245 return (0); /* complete */
1249 saf1761_otg_xfer_do_fifo(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1251 struct saf1761_otg_td *td;
1256 td = xfer->td_transfer_cache;
1261 if ((td->func) (sc, td)) {
1262 /* operation in progress */
1265 if (((void *)td) == xfer->td_transfer_last) {
1268 if (td->error_any) {
1270 } else if (td->remainder > 0) {
1272 * We had a short transfer. If there is no alternate
1273 * next, stop processing !
1275 if (!td->alt_next) {
1280 * Fetch the next transfer descriptor.
1282 toggle = td->toggle;
1284 td->toggle = toggle;
1285 xfer->td_transfer_cache = td;
1290 /* compute all actual lengths */
1291 xfer->td_transfer_cache = NULL;
1292 sc->sc_xfer_complete = 1;
1296 saf1761_otg_xfer_do_complete(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1298 struct saf1761_otg_td *td;
1302 td = xfer->td_transfer_cache;
1304 /* compute all actual lengths */
1305 saf1761_otg_standard_done(xfer);
1312 saf1761_otg_interrupt_poll_locked(struct saf1761_otg_softc *sc)
1314 struct usb_xfer *xfer;
1316 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
1317 saf1761_otg_xfer_do_fifo(sc, xfer);
1321 saf1761_otg_wait_suspend(struct saf1761_otg_softc *sc, uint8_t on)
1324 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IESUSP;
1325 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IERESM;
1327 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IESUSP;
1328 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IERESM;
1330 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1334 saf1761_otg_update_vbus(struct saf1761_otg_softc *sc)
1338 /* read fresh status */
1339 status = SAF1761_READ_LE_4(sc, SOTG_STATUS);
1341 DPRINTFN(4, "STATUS=0x%04x\n", status);
1343 if ((status & SOTG_STATUS_VBUS_VLD) &&
1344 (status & SOTG_STATUS_ID)) {
1345 /* VBUS present and device mode */
1346 if (!sc->sc_flags.status_vbus) {
1347 sc->sc_flags.status_vbus = 1;
1349 /* complete root HUB interrupt endpoint */
1350 saf1761_otg_root_intr(sc);
1353 /* VBUS not-present or host mode */
1354 if (sc->sc_flags.status_vbus) {
1355 sc->sc_flags.status_vbus = 0;
1356 sc->sc_flags.status_bus_reset = 0;
1357 sc->sc_flags.status_suspend = 0;
1358 sc->sc_flags.change_suspend = 0;
1359 sc->sc_flags.change_connect = 1;
1361 /* complete root HUB interrupt endpoint */
1362 saf1761_otg_root_intr(sc);
1368 saf1761_otg_interrupt_complete_locked(struct saf1761_otg_softc *sc)
1370 struct usb_xfer *xfer;
1372 /* scan for completion events */
1373 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1374 if (saf1761_otg_xfer_do_complete(sc, xfer))
1380 saf1761_otg_filter_interrupt(void *arg)
1382 struct saf1761_otg_softc *sc = arg;
1383 int retval = FILTER_HANDLED;
1387 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1389 hcstat = SAF1761_READ_LE_4(sc, SOTG_HCINTERRUPT);
1390 /* acknowledge all host controller interrupts */
1391 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT, hcstat);
1393 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT);
1394 /* acknowledge all device controller interrupts */
1395 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT,
1396 status & ~SAF1761_DCINTERRUPT_THREAD_IRQ);
1398 (void) SAF1761_READ_LE_4(sc, SOTG_ATL_PTD_DONE_PTD);
1399 (void) SAF1761_READ_LE_4(sc, SOTG_INT_PTD_DONE_PTD);
1400 (void) SAF1761_READ_LE_4(sc, SOTG_ISO_PTD_DONE_PTD);
1402 if (status & SAF1761_DCINTERRUPT_THREAD_IRQ)
1403 retval = FILTER_SCHEDULE_THREAD;
1405 /* poll FIFOs, if any */
1406 saf1761_otg_interrupt_poll_locked(sc);
1408 if (sc->sc_xfer_complete != 0)
1409 retval = FILTER_SCHEDULE_THREAD;
1411 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1417 saf1761_otg_interrupt(void *arg)
1419 struct saf1761_otg_softc *sc = arg;
1422 USB_BUS_LOCK(&sc->sc_bus);
1423 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1425 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT) &
1426 SAF1761_DCINTERRUPT_THREAD_IRQ;
1428 /* acknowledge all device controller interrupts */
1429 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT, status);
1431 DPRINTF("DCINTERRUPT=0x%08x SOF=0x%08x "
1432 "FRINDEX=0x%08x\n", status,
1433 SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM),
1434 SAF1761_READ_LE_4(sc, SOTG_FRINDEX));
1436 /* update VBUS and ID bits, if any */
1437 if (status & SOTG_DCINTERRUPT_IEVBUS)
1438 saf1761_otg_update_vbus(sc);
1440 if (status & SOTG_DCINTERRUPT_IEBRST) {
1442 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1443 SOTG_UNLOCK_DEVICE_CODE);
1445 /* Enable device address */
1446 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS,
1447 SOTG_ADDRESS_ENABLE);
1449 sc->sc_flags.status_bus_reset = 1;
1450 sc->sc_flags.status_suspend = 0;
1451 sc->sc_flags.change_suspend = 0;
1452 sc->sc_flags.change_connect = 1;
1454 /* disable resume interrupt */
1455 saf1761_otg_wait_suspend(sc, 1);
1456 /* complete root HUB interrupt endpoint */
1457 saf1761_otg_root_intr(sc);
1460 * If "RESUME" and "SUSPEND" is set at the same time we
1461 * interpret that like "RESUME". Resume is set when there is
1462 * at least 3 milliseconds of inactivity on the USB BUS:
1464 if (status & SOTG_DCINTERRUPT_IERESM) {
1466 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1467 SOTG_UNLOCK_DEVICE_CODE);
1469 if (sc->sc_flags.status_suspend) {
1470 sc->sc_flags.status_suspend = 0;
1471 sc->sc_flags.change_suspend = 1;
1472 /* disable resume interrupt */
1473 saf1761_otg_wait_suspend(sc, 1);
1474 /* complete root HUB interrupt endpoint */
1475 saf1761_otg_root_intr(sc);
1477 } else if (status & SOTG_DCINTERRUPT_IESUSP) {
1478 if (!sc->sc_flags.status_suspend) {
1479 sc->sc_flags.status_suspend = 1;
1480 sc->sc_flags.change_suspend = 1;
1481 /* enable resume interrupt */
1482 saf1761_otg_wait_suspend(sc, 0);
1483 /* complete root HUB interrupt endpoint */
1484 saf1761_otg_root_intr(sc);
1488 if (sc->sc_xfer_complete != 0) {
1489 sc->sc_xfer_complete = 0;
1491 /* complete FIFOs, if any */
1492 saf1761_otg_interrupt_complete_locked(sc);
1494 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1495 USB_BUS_UNLOCK(&sc->sc_bus);
1499 saf1761_otg_setup_standard_chain_sub(struct saf1761_otg_std_temp *temp)
1501 struct saf1761_otg_td *td;
1503 /* get current Transfer Descriptor */
1507 /* prepare for next TD */
1508 temp->td_next = td->obj_next;
1510 /* fill out the Transfer Descriptor */
1511 td->func = temp->func;
1513 td->offset = temp->offset;
1514 td->remainder = temp->len;
1516 td->error_stall = 0;
1518 td->did_stall = temp->did_stall;
1519 td->short_pkt = temp->short_pkt;
1520 td->alt_next = temp->setup_alt_next;
1521 td->channel = SOTG_HOST_CHANNEL_MAX;
1525 saf1761_otg_setup_standard_chain(struct usb_xfer *xfer)
1527 struct saf1761_otg_std_temp temp;
1528 struct saf1761_otg_softc *sc;
1529 struct saf1761_otg_td *td;
1536 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1537 xfer->address, UE_GET_ADDR(xfer->endpointno),
1538 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1540 temp.max_frame_size = xfer->max_frame_size;
1542 td = xfer->td_start[0];
1543 xfer->td_transfer_first = td;
1544 xfer->td_transfer_cache = td;
1550 temp.td_next = xfer->td_start[0];
1552 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
1553 xfer->flags_int.isochronous_xfr;
1554 temp.did_stall = !xfer->flags_int.control_stall;
1556 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
1558 sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1559 ep_no = (xfer->endpointno & UE_ADDR);
1560 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
1562 /* check if we should prepend a setup message */
1564 if (xfer->flags_int.control_xfr) {
1565 if (xfer->flags_int.control_hdr) {
1568 temp.func = &saf1761_host_setup_tx;
1570 temp.func = &saf1761_device_setup_rx;
1572 temp.len = xfer->frlengths[0];
1573 temp.pc = xfer->frbuffers + 0;
1574 temp.short_pkt = temp.len ? 1 : 0;
1575 /* check for last frame */
1576 if (xfer->nframes == 1) {
1577 /* no STATUS stage yet, SETUP is last */
1578 if (xfer->flags_int.control_act)
1579 temp.setup_alt_next = 0;
1581 saf1761_otg_setup_standard_chain_sub(&temp);
1588 if (x != xfer->nframes) {
1589 if (xfer->endpointno & UE_DIR_IN) {
1591 if (ep_type == UE_INTERRUPT)
1592 temp.func = &saf1761_host_intr_data_rx;
1593 else if (ep_type == UE_ISOCHRONOUS)
1594 temp.func = &saf1761_host_isoc_data_rx;
1596 temp.func = &saf1761_host_bulk_data_rx;
1599 temp.func = &saf1761_device_data_tx;
1604 if (ep_type == UE_INTERRUPT)
1605 temp.func = &saf1761_host_intr_data_tx;
1606 else if (ep_type == UE_ISOCHRONOUS)
1607 temp.func = &saf1761_host_isoc_data_tx;
1609 temp.func = &saf1761_host_bulk_data_tx;
1612 temp.func = &saf1761_device_data_rx;
1617 /* setup "pc" pointer */
1618 temp.pc = xfer->frbuffers + x;
1623 while (x != xfer->nframes) {
1625 /* DATA0 / DATA1 message */
1627 temp.len = xfer->frlengths[x];
1631 if (x == xfer->nframes) {
1632 if (xfer->flags_int.control_xfr) {
1633 if (xfer->flags_int.control_act) {
1634 temp.setup_alt_next = 0;
1637 temp.setup_alt_next = 0;
1640 if (temp.len == 0) {
1642 /* make sure that we send an USB packet */
1648 /* regular data transfer */
1650 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1653 saf1761_otg_setup_standard_chain_sub(&temp);
1655 if (xfer->flags_int.isochronous_xfr) {
1656 temp.offset += temp.len;
1658 /* get next Page Cache pointer */
1659 temp.pc = xfer->frbuffers + x;
1663 /* check for control transfer */
1664 if (xfer->flags_int.control_xfr) {
1665 /* always setup a valid "pc" pointer for status and sync */
1666 temp.pc = xfer->frbuffers + 0;
1669 temp.setup_alt_next = 0;
1671 /* check if we should append a status stage */
1672 if (!xfer->flags_int.control_act) {
1675 * Send a DATA1 message and invert the current
1676 * endpoint direction.
1678 if (xfer->endpointno & UE_DIR_IN) {
1680 temp.func = &saf1761_host_bulk_data_tx;
1683 temp.func = &saf1761_device_data_rx;
1688 temp.func = &saf1761_host_bulk_data_rx;
1691 temp.func = &saf1761_device_data_tx;
1698 saf1761_otg_setup_standard_chain_sub(&temp);
1700 /* data toggle should be DATA1 */
1705 /* we need a SYNC point after TX */
1706 temp.func = &saf1761_device_data_tx_sync;
1707 saf1761_otg_setup_standard_chain_sub(&temp);
1712 temp.pc = xfer->frbuffers + 0;
1715 temp.setup_alt_next = 0;
1717 /* we need a SYNC point after TX */
1718 temp.func = &saf1761_device_data_tx_sync;
1719 saf1761_otg_setup_standard_chain_sub(&temp);
1723 /* must have at least one frame! */
1725 xfer->td_transfer_last = td;
1728 /* get first again */
1729 td = xfer->td_transfer_first;
1730 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
1735 saf1761_otg_timeout(void *arg)
1737 struct usb_xfer *xfer = arg;
1739 DPRINTF("xfer=%p\n", xfer);
1741 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1743 /* transfer is transferred */
1744 saf1761_otg_device_done(xfer, USB_ERR_TIMEOUT);
1748 saf1761_otg_intr_set(struct usb_xfer *xfer, uint8_t set)
1750 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1751 uint8_t ep_no = (xfer->endpointno & UE_ADDR);
1754 DPRINTFN(15, "endpoint=%d set=%d\n", xfer->endpointno, set);
1757 mask = SOTG_DCINTERRUPT_IEPRX(0) |
1758 SOTG_DCINTERRUPT_IEPTX(0) |
1759 SOTG_DCINTERRUPT_IEP0SETUP;
1760 } else if (xfer->endpointno & UE_DIR_IN) {
1761 mask = SOTG_DCINTERRUPT_IEPTX(ep_no);
1763 mask = SOTG_DCINTERRUPT_IEPRX(ep_no);
1767 sc->sc_intr_enable |= mask;
1769 sc->sc_intr_enable &= ~mask;
1771 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1775 saf1761_otg_start_standard_chain(struct usb_xfer *xfer)
1777 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1781 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1784 saf1761_otg_xfer_do_fifo(sc, xfer);
1786 if (xfer->td_transfer_cache != NULL) {
1788 * Only enable the endpoint interrupt when we are
1789 * actually waiting for data, hence we are dealing
1790 * with level triggered interrupts !
1792 saf1761_otg_intr_set(xfer, 1);
1794 /* put transfer on interrupt queue */
1795 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1797 /* start timeout, if any */
1798 if (xfer->timeout != 0) {
1799 usbd_transfer_timeout_ms(xfer,
1800 &saf1761_otg_timeout, xfer->timeout);
1803 /* catch completion, if any */
1804 saf1761_otg_interrupt_complete_locked(sc);
1806 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1810 saf1761_otg_root_intr(struct saf1761_otg_softc *sc)
1814 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1816 /* set port bit - we only have one port */
1817 sc->sc_hub_idata[0] = 0x02;
1819 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1820 sizeof(sc->sc_hub_idata));
1824 saf1761_otg_standard_done_sub(struct usb_xfer *xfer)
1826 struct saf1761_otg_td *td;
1832 td = xfer->td_transfer_cache;
1835 len = td->remainder;
1837 /* store last data toggle */
1838 xfer->endpoint->toggle_next = td->toggle;
1840 if (xfer->aframes != xfer->nframes) {
1842 * Verify the length and subtract
1843 * the remainder from "frlengths[]":
1845 if (len > xfer->frlengths[xfer->aframes]) {
1848 xfer->frlengths[xfer->aframes] -= len;
1851 /* Check for transfer error */
1852 if (td->error_any) {
1853 /* the transfer is finished */
1854 error = (td->error_stall ?
1855 USB_ERR_STALLED : USB_ERR_IOERROR);
1859 /* Check for short transfer */
1861 if (xfer->flags_int.short_frames_ok ||
1862 xfer->flags_int.isochronous_xfr) {
1863 /* follow alt next */
1870 /* the transfer is finished */
1878 /* this USB frame is complete */
1884 /* update transfer cache */
1886 xfer->td_transfer_cache = td;
1892 saf1761_otg_standard_done(struct usb_xfer *xfer)
1894 usb_error_t err = 0;
1896 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1897 xfer, xfer->endpoint);
1901 xfer->td_transfer_cache = xfer->td_transfer_first;
1903 if (xfer->flags_int.control_xfr) {
1905 if (xfer->flags_int.control_hdr) {
1907 err = saf1761_otg_standard_done_sub(xfer);
1911 if (xfer->td_transfer_cache == NULL) {
1915 while (xfer->aframes != xfer->nframes) {
1917 err = saf1761_otg_standard_done_sub(xfer);
1920 if (xfer->td_transfer_cache == NULL) {
1925 if (xfer->flags_int.control_xfr &&
1926 !xfer->flags_int.control_act) {
1928 err = saf1761_otg_standard_done_sub(xfer);
1931 saf1761_otg_device_done(xfer, err);
1934 /*------------------------------------------------------------------------*
1935 * saf1761_otg_device_done
1937 * NOTE: this function can be called more than one time on the
1938 * same USB transfer!
1939 *------------------------------------------------------------------------*/
1941 saf1761_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
1943 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1945 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1947 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1948 xfer, xfer->endpoint, error);
1950 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1952 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1953 saf1761_otg_intr_set(xfer, 0);
1955 struct saf1761_otg_td *td;
1957 td = xfer->td_transfer_first;
1960 saf1761_host_channel_free(sc, td);
1963 /* dequeue transfer and start next transfer */
1964 usbd_transfer_done(xfer, error);
1966 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1970 saf1761_otg_xfer_stall(struct usb_xfer *xfer)
1972 saf1761_otg_device_done(xfer, USB_ERR_STALLED);
1976 saf1761_otg_set_stall(struct usb_device *udev,
1977 struct usb_endpoint *ep, uint8_t *did_stall)
1979 struct saf1761_otg_softc *sc;
1984 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1987 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1992 DPRINTFN(5, "endpoint=%p\n", ep);
1995 sc = SAF1761_OTG_BUS2SC(udev->bus);
1997 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
1998 ep_dir = (ep->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT));
1999 ep_type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2001 if (ep_type == UE_CONTROL) {
2002 /* should not happen */
2005 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2007 /* select the correct endpoint */
2008 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2009 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2010 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2011 SOTG_EP_INDEX_DIR_OUT));
2014 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
2016 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2020 saf1761_otg_clear_stall_sub_locked(struct saf1761_otg_softc *sc,
2021 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2023 if (ep_type == UE_CONTROL) {
2024 /* clearing stall is not needed */
2027 /* select the correct endpoint */
2028 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2029 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2030 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2031 SOTG_EP_INDEX_DIR_OUT));
2033 /* disable endpoint */
2034 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, 0);
2035 /* enable endpoint again - will clear data toggle */
2036 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, ep_type | SOTG_EP_TYPE_ENABLE);
2039 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_CLBUF);
2041 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
2045 saf1761_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
2047 struct saf1761_otg_softc *sc;
2048 struct usb_endpoint_descriptor *ed;
2050 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2052 DPRINTFN(5, "endpoint=%p\n", ep);
2055 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2060 sc = SAF1761_OTG_BUS2SC(udev->bus);
2062 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2064 /* get endpoint descriptor */
2067 /* reset endpoint */
2068 saf1761_otg_clear_stall_sub_locked(sc,
2069 (ed->bEndpointAddress & UE_ADDR),
2070 (ed->bmAttributes & UE_XFERTYPE),
2071 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
2073 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2077 saf1761_otg_init(struct saf1761_otg_softc *sc)
2079 const struct usb_hw_ep_profile *pf;
2084 /* set up the bus structure */
2085 sc->sc_bus.usbrev = USB_REV_2_0;
2086 sc->sc_bus.methods = &saf1761_otg_bus_methods;
2088 USB_BUS_LOCK(&sc->sc_bus);
2090 /* Reset Host controller, including HW mode */
2091 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_ALL);
2095 /* Reset Host controller, including HW mode */
2096 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_HC);
2101 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, 0);
2106 /* Enable interrupts */
2107 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_GLOBAL_INTR_EN |
2108 SOTG_HW_MODE_CTRL_COMN_INT;
2111 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE, SOTG_UNLOCK_DEVICE_CODE);
2114 * Set correct hardware mode, must be written twice if bus
2117 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2118 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2120 SAF1761_WRITE_LE_4(sc, SOTG_DCSCRATCH, 0xdeadbeef);
2121 SAF1761_WRITE_LE_4(sc, SOTG_HCSCRATCH, 0xdeadbeef);
2123 DPRINTF("DCID=0x%08x VEND_PROD=0x%08x HWMODE=0x%08x SCRATCH=0x%08x,0x%08x\n",
2124 SAF1761_READ_LE_4(sc, SOTG_DCCHIP_ID),
2125 SAF1761_READ_LE_4(sc, SOTG_VEND_PROD_ID),
2126 SAF1761_READ_LE_4(sc, SOTG_HW_MODE_CTRL),
2127 SAF1761_READ_LE_4(sc, SOTG_DCSCRATCH),
2128 SAF1761_READ_LE_4(sc, SOTG_HCSCRATCH));
2130 /* reset device controller */
2131 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_SFRESET);
2132 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2137 /* reset host controller */
2138 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_HCRESET);
2140 /* wait for reset to clear */
2141 for (x = 0; x != 10; x++) {
2142 if ((SAF1761_READ_LE_4(sc, SOTG_USBCMD) & SOTG_USBCMD_HCRESET) == 0)
2144 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
2147 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode |
2148 SOTG_HW_MODE_CTRL_ALL_ATX_RESET);
2153 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2159 saf1761_otg_pull_down(sc);
2161 /* wait 10ms for pulldown to stabilise */
2162 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2166 saf1761_otg_get_hw_ep_profile(NULL, &pf, x);
2170 /* select the correct endpoint */
2171 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2172 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2173 SOTG_EP_INDEX_DIR_IN);
2175 /* select the maximum packet size */
2176 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_in_frame_size);
2178 /* select the correct endpoint */
2179 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2180 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2181 SOTG_EP_INDEX_DIR_OUT);
2183 /* select the maximum packet size */
2184 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_out_frame_size);
2187 /* enable interrupts */
2188 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_GLINTENA |
2189 SOTG_MODE_CLKAON | SOTG_MODE_WKUPCS);
2191 sc->sc_interrupt_cfg |=
2192 SOTG_INTERRUPT_CFG_CDBGMOD |
2193 SOTG_INTERRUPT_CFG_DDBGMODIN |
2194 SOTG_INTERRUPT_CFG_DDBGMODOUT;
2196 /* set default values */
2197 SAF1761_WRITE_LE_4(sc, SOTG_INTERRUPT_CFG, sc->sc_interrupt_cfg);
2199 /* enable VBUS and ID interrupt */
2200 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2201 SOTG_IRQ_ENABLE_CLR(0xFFFF));
2202 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2203 SOTG_IRQ_ENABLE_SET(SOTG_IRQ_ID | SOTG_IRQ_VBUS_VLD));
2205 /* enable interrupts */
2206 sc->sc_intr_enable = SOTG_DCINTERRUPT_IEVBUS |
2207 SOTG_DCINTERRUPT_IEBRST | SOTG_DCINTERRUPT_IESUSP;
2208 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
2211 * Connect ATX port 1 to device controller, select external
2212 * charge pump and driver VBUS to +5V:
2214 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2215 SOTG_CTRL_CLR(0xFFFF));
2216 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2217 SOTG_CTRL_SET(SOTG_CTRL_SW_SEL_HC_DC |
2218 SOTG_CTRL_BDIS_ACON_EN | SOTG_CTRL_SEL_CP_EXT |
2219 SOTG_CTRL_VBUS_DRV));
2221 /* disable device address */
2222 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, 0);
2224 /* enable host controller clock and preserve reserved bits */
2225 x = SAF1761_READ_LE_4(sc, SOTG_POWER_DOWN);
2226 SAF1761_WRITE_LE_4(sc, SOTG_POWER_DOWN, x | SOTG_POWER_DOWN_HC_CLK_EN);
2228 /* wait 10ms for clock */
2229 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2231 /* enable configuration flag */
2232 SAF1761_WRITE_LE_4(sc, SOTG_CONFIGFLAG, SOTG_CONFIGFLAG_ENABLE);
2234 /* clear RAM block */
2235 for (x = 0x400; x != 0x10000; x += 4)
2236 SAF1761_WRITE_LE_4(sc, x, 0);
2239 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_RS);
2241 DPRINTF("USBCMD=0x%08x\n", SAF1761_READ_LE_4(sc, SOTG_USBCMD));
2243 /* make HC scan all PTDs */
2244 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_LAST_PTD, (1 << 31));
2245 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_LAST_PTD, (1 << 31));
2246 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_LAST_PTD, (1 << 31));
2248 /* skip all PTDs by default */
2249 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD, -1U);
2250 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD, -1U);
2251 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD, -1U);
2253 /* activate all PTD types */
2254 SAF1761_WRITE_LE_4(sc, SOTG_HCBUFFERSTATUS,
2255 SOTG_HCBUFFERSTATUS_ISO_BUF_FILL |
2256 SOTG_HCBUFFERSTATUS_INT_BUF_FILL |
2257 SOTG_HCBUFFERSTATUS_ATL_BUF_FILL);
2259 /* we don't use the AND mask */
2260 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_AND, 0);
2261 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_AND, 0);
2262 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_AND, 0);
2264 /* enable all PTD OR interrupts by default */
2265 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_OR, -1U);
2266 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_OR, -1U);
2267 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_OR, -1U);
2269 /* enable HC interrupts */
2270 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT_ENABLE,
2271 SOTG_HCINTERRUPT_OTG_IRQ |
2272 SOTG_HCINTERRUPT_ISO_IRQ |
2273 SOTG_HCINTERRUPT_ALT_IRQ |
2274 SOTG_HCINTERRUPT_INT_IRQ);
2276 /* poll initial VBUS status */
2277 saf1761_otg_update_vbus(sc);
2279 USB_BUS_UNLOCK(&sc->sc_bus);
2281 /* catch any lost interrupts */
2283 saf1761_otg_do_poll(&sc->sc_bus);
2285 return (0); /* success */
2289 saf1761_otg_uninit(struct saf1761_otg_softc *sc)
2291 USB_BUS_LOCK(&sc->sc_bus);
2293 /* disable all interrupts */
2294 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2296 sc->sc_flags.port_powered = 0;
2297 sc->sc_flags.status_vbus = 0;
2298 sc->sc_flags.status_bus_reset = 0;
2299 sc->sc_flags.status_suspend = 0;
2300 sc->sc_flags.change_suspend = 0;
2301 sc->sc_flags.change_connect = 1;
2303 saf1761_otg_pull_down(sc);
2304 USB_BUS_UNLOCK(&sc->sc_bus);
2308 saf1761_otg_suspend(struct saf1761_otg_softc *sc)
2314 saf1761_otg_resume(struct saf1761_otg_softc *sc)
2320 saf1761_otg_do_poll(struct usb_bus *bus)
2322 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
2324 USB_BUS_LOCK(&sc->sc_bus);
2325 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2326 saf1761_otg_interrupt_poll_locked(sc);
2327 saf1761_otg_interrupt_complete_locked(sc);
2328 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2329 USB_BUS_UNLOCK(&sc->sc_bus);
2332 /*------------------------------------------------------------------------*
2333 * saf1761_otg control support
2334 * saf1761_otg interrupt support
2335 * saf1761_otg bulk support
2336 *------------------------------------------------------------------------*/
2338 saf1761_otg_device_non_isoc_open(struct usb_xfer *xfer)
2344 saf1761_otg_device_non_isoc_close(struct usb_xfer *xfer)
2346 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2350 saf1761_otg_device_non_isoc_enter(struct usb_xfer *xfer)
2356 saf1761_otg_device_non_isoc_start(struct usb_xfer *xfer)
2359 saf1761_otg_setup_standard_chain(xfer);
2360 saf1761_otg_start_standard_chain(xfer);
2363 static const struct usb_pipe_methods saf1761_otg_non_isoc_methods =
2365 .open = saf1761_otg_device_non_isoc_open,
2366 .close = saf1761_otg_device_non_isoc_close,
2367 .enter = saf1761_otg_device_non_isoc_enter,
2368 .start = saf1761_otg_device_non_isoc_start,
2371 /*------------------------------------------------------------------------*
2372 * saf1761_otg isochronous support
2373 *------------------------------------------------------------------------*/
2375 saf1761_otg_device_isoc_open(struct usb_xfer *xfer)
2381 saf1761_otg_device_isoc_close(struct usb_xfer *xfer)
2383 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2387 saf1761_otg_device_isoc_enter(struct usb_xfer *xfer)
2389 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
2393 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2394 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2396 /* get the current frame index - we don't need the high bits */
2398 nframes = SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM);
2401 * check if the frame index is within the window where the
2402 * frames will be inserted
2404 temp = (nframes - xfer->endpoint->isoc_next) & SOTG_FRAME_NUM_SOFR_MASK;
2406 if ((xfer->endpoint->is_synced == 0) ||
2407 (temp < xfer->nframes)) {
2409 * If there is data underflow or the pipe queue is
2410 * empty we schedule the transfer a few frames ahead
2411 * of the current frame position. Else two isochronous
2412 * transfers might overlap.
2414 xfer->endpoint->isoc_next = (nframes + 3) & SOTG_FRAME_NUM_SOFR_MASK;
2415 xfer->endpoint->is_synced = 1;
2416 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2419 * compute how many milliseconds the insertion is ahead of the
2420 * current frame position:
2422 temp = (xfer->endpoint->isoc_next - nframes) & SOTG_FRAME_NUM_SOFR_MASK;
2425 * pre-compute when the isochronous transfer will be finished:
2427 xfer->isoc_time_complete =
2428 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2431 /* compute frame number for next insertion */
2432 xfer->endpoint->isoc_next += xfer->nframes;
2435 saf1761_otg_setup_standard_chain(xfer);
2439 saf1761_otg_device_isoc_start(struct usb_xfer *xfer)
2441 /* start TD chain */
2442 saf1761_otg_start_standard_chain(xfer);
2445 static const struct usb_pipe_methods saf1761_otg_device_isoc_methods =
2447 .open = saf1761_otg_device_isoc_open,
2448 .close = saf1761_otg_device_isoc_close,
2449 .enter = saf1761_otg_device_isoc_enter,
2450 .start = saf1761_otg_device_isoc_start,
2453 /*------------------------------------------------------------------------*
2454 * saf1761_otg root control support
2455 *------------------------------------------------------------------------*
2456 * Simulate a hardware HUB by handling all the necessary requests.
2457 *------------------------------------------------------------------------*/
2459 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2461 static const struct usb_device_descriptor saf1761_otg_devd = {
2462 .bLength = sizeof(struct usb_device_descriptor),
2463 .bDescriptorType = UDESC_DEVICE,
2464 HSETW(.idVendor, 0x04cc),
2465 HSETW(.idProduct, 0x1761),
2466 .bcdUSB = {0x00, 0x02},
2467 .bDeviceClass = UDCLASS_HUB,
2468 .bDeviceSubClass = UDSUBCLASS_HUB,
2469 .bDeviceProtocol = UDPROTO_FSHUB,
2470 .bMaxPacketSize = 64,
2471 .bcdDevice = {0x00, 0x01},
2474 .bNumConfigurations = 1,
2477 static const struct usb_device_qualifier saf1761_otg_odevd = {
2478 .bLength = sizeof(struct usb_device_qualifier),
2479 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
2480 .bcdUSB = {0x00, 0x02},
2481 .bDeviceClass = UDCLASS_HUB,
2482 .bDeviceSubClass = UDSUBCLASS_HUB,
2483 .bDeviceProtocol = UDPROTO_FSHUB,
2484 .bMaxPacketSize0 = 0,
2485 .bNumConfigurations = 0,
2488 static const struct saf1761_otg_config_desc saf1761_otg_confd = {
2490 .bLength = sizeof(struct usb_config_descriptor),
2491 .bDescriptorType = UDESC_CONFIG,
2492 .wTotalLength[0] = sizeof(saf1761_otg_confd),
2494 .bConfigurationValue = 1,
2495 .iConfiguration = 0,
2496 .bmAttributes = UC_SELF_POWERED,
2500 .bLength = sizeof(struct usb_interface_descriptor),
2501 .bDescriptorType = UDESC_INTERFACE,
2503 .bInterfaceClass = UICLASS_HUB,
2504 .bInterfaceSubClass = UISUBCLASS_HUB,
2505 .bInterfaceProtocol = 0,
2509 .bLength = sizeof(struct usb_endpoint_descriptor),
2510 .bDescriptorType = UDESC_ENDPOINT,
2511 .bEndpointAddress = (UE_DIR_IN | SAF1761_OTG_INTR_ENDPT),
2512 .bmAttributes = UE_INTERRUPT,
2513 .wMaxPacketSize[0] = 8,
2518 static const struct usb_hub_descriptor_min saf1761_otg_hubd = {
2519 .bDescLength = sizeof(saf1761_otg_hubd),
2520 .bDescriptorType = UDESC_HUB,
2521 .bNbrPorts = SOTG_NUM_PORTS,
2522 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
2523 .bPwrOn2PwrGood = 50,
2524 .bHubContrCurrent = 0,
2525 .DeviceRemovable = {0}, /* port is removable */
2528 #define STRING_VENDOR \
2531 #define STRING_PRODUCT \
2532 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
2534 USB_MAKE_STRING_DESC(STRING_VENDOR, saf1761_otg_vendor);
2535 USB_MAKE_STRING_DESC(STRING_PRODUCT, saf1761_otg_product);
2538 saf1761_otg_roothub_exec(struct usb_device *udev,
2539 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2541 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(udev->bus);
2550 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2553 ptr = (const void *)&sc->sc_hub_temp;
2557 value = UGETW(req->wValue);
2558 index = UGETW(req->wIndex);
2560 /* demultiplex the control request */
2562 switch (req->bmRequestType) {
2563 case UT_READ_DEVICE:
2564 switch (req->bRequest) {
2565 case UR_GET_DESCRIPTOR:
2566 goto tr_handle_get_descriptor;
2568 goto tr_handle_get_config;
2570 goto tr_handle_get_status;
2576 case UT_WRITE_DEVICE:
2577 switch (req->bRequest) {
2578 case UR_SET_ADDRESS:
2579 goto tr_handle_set_address;
2581 goto tr_handle_set_config;
2582 case UR_CLEAR_FEATURE:
2583 goto tr_valid; /* nop */
2584 case UR_SET_DESCRIPTOR:
2585 goto tr_valid; /* nop */
2586 case UR_SET_FEATURE:
2592 case UT_WRITE_ENDPOINT:
2593 switch (req->bRequest) {
2594 case UR_CLEAR_FEATURE:
2595 switch (UGETW(req->wValue)) {
2596 case UF_ENDPOINT_HALT:
2597 goto tr_handle_clear_halt;
2598 case UF_DEVICE_REMOTE_WAKEUP:
2599 goto tr_handle_clear_wakeup;
2604 case UR_SET_FEATURE:
2605 switch (UGETW(req->wValue)) {
2606 case UF_ENDPOINT_HALT:
2607 goto tr_handle_set_halt;
2608 case UF_DEVICE_REMOTE_WAKEUP:
2609 goto tr_handle_set_wakeup;
2614 case UR_SYNCH_FRAME:
2615 goto tr_valid; /* nop */
2621 case UT_READ_ENDPOINT:
2622 switch (req->bRequest) {
2624 goto tr_handle_get_ep_status;
2630 case UT_WRITE_INTERFACE:
2631 switch (req->bRequest) {
2632 case UR_SET_INTERFACE:
2633 goto tr_handle_set_interface;
2634 case UR_CLEAR_FEATURE:
2635 goto tr_valid; /* nop */
2636 case UR_SET_FEATURE:
2642 case UT_READ_INTERFACE:
2643 switch (req->bRequest) {
2644 case UR_GET_INTERFACE:
2645 goto tr_handle_get_interface;
2647 goto tr_handle_get_iface_status;
2653 case UT_WRITE_CLASS_INTERFACE:
2654 case UT_WRITE_VENDOR_INTERFACE:
2658 case UT_READ_CLASS_INTERFACE:
2659 case UT_READ_VENDOR_INTERFACE:
2663 case UT_WRITE_CLASS_DEVICE:
2664 switch (req->bRequest) {
2665 case UR_CLEAR_FEATURE:
2667 case UR_SET_DESCRIPTOR:
2668 case UR_SET_FEATURE:
2675 case UT_WRITE_CLASS_OTHER:
2676 switch (req->bRequest) {
2677 case UR_CLEAR_FEATURE:
2678 if (index == SOTG_HOST_PORT_NUM)
2679 goto tr_handle_clear_port_feature_host;
2680 else if (index == SOTG_DEVICE_PORT_NUM)
2681 goto tr_handle_clear_port_feature_device;
2684 case UR_SET_FEATURE:
2685 if (index == SOTG_HOST_PORT_NUM)
2686 goto tr_handle_set_port_feature_host;
2687 else if (index == SOTG_DEVICE_PORT_NUM)
2688 goto tr_handle_set_port_feature_device;
2691 case UR_CLEAR_TT_BUFFER:
2701 case UT_READ_CLASS_OTHER:
2702 switch (req->bRequest) {
2703 case UR_GET_TT_STATE:
2704 goto tr_handle_get_tt_state;
2706 if (index == SOTG_HOST_PORT_NUM)
2707 goto tr_handle_get_port_status_host;
2708 else if (index == SOTG_DEVICE_PORT_NUM)
2709 goto tr_handle_get_port_status_device;
2717 case UT_READ_CLASS_DEVICE:
2718 switch (req->bRequest) {
2719 case UR_GET_DESCRIPTOR:
2720 goto tr_handle_get_class_descriptor;
2722 goto tr_handle_get_class_status;
2733 tr_handle_get_descriptor:
2734 switch (value >> 8) {
2738 len = sizeof(saf1761_otg_devd);
2739 ptr = (const void *)&saf1761_otg_devd;
2741 case UDESC_DEVICE_QUALIFIER:
2744 len = sizeof(saf1761_otg_odevd);
2745 ptr = (const void *)&saf1761_otg_odevd;
2750 len = sizeof(saf1761_otg_confd);
2751 ptr = (const void *)&saf1761_otg_confd;
2754 switch (value & 0xff) {
2755 case 0: /* Language table */
2756 len = sizeof(usb_string_lang_en);
2757 ptr = (const void *)&usb_string_lang_en;
2760 case 1: /* Vendor */
2761 len = sizeof(saf1761_otg_vendor);
2762 ptr = (const void *)&saf1761_otg_vendor;
2765 case 2: /* Product */
2766 len = sizeof(saf1761_otg_product);
2767 ptr = (const void *)&saf1761_otg_product;
2778 tr_handle_get_config:
2780 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2783 tr_handle_get_status:
2785 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2788 tr_handle_set_address:
2792 sc->sc_rt_addr = value;
2795 tr_handle_set_config:
2798 sc->sc_conf = value;
2801 tr_handle_get_interface:
2803 sc->sc_hub_temp.wValue[0] = 0;
2806 tr_handle_get_tt_state:
2807 tr_handle_get_class_status:
2808 tr_handle_get_iface_status:
2809 tr_handle_get_ep_status:
2811 USETW(sc->sc_hub_temp.wValue, 0);
2815 tr_handle_set_interface:
2816 tr_handle_set_wakeup:
2817 tr_handle_clear_wakeup:
2818 tr_handle_clear_halt:
2821 tr_handle_clear_port_feature_device:
2822 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
2825 case UHF_PORT_SUSPEND:
2826 saf1761_otg_wakeup_peer(sc);
2829 case UHF_PORT_ENABLE:
2830 sc->sc_flags.port_enabled = 0;
2834 case UHF_PORT_INDICATOR:
2835 case UHF_C_PORT_ENABLE:
2836 case UHF_C_PORT_OVER_CURRENT:
2837 case UHF_C_PORT_RESET:
2840 case UHF_PORT_POWER:
2841 sc->sc_flags.port_powered = 0;
2842 saf1761_otg_pull_down(sc);
2844 case UHF_C_PORT_CONNECTION:
2845 sc->sc_flags.change_connect = 0;
2847 case UHF_C_PORT_SUSPEND:
2848 sc->sc_flags.change_suspend = 0;
2851 err = USB_ERR_IOERROR;
2856 tr_handle_clear_port_feature_host:
2857 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
2859 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2862 case UHF_PORT_ENABLE:
2863 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PED);
2865 case UHF_PORT_SUSPEND:
2866 if ((temp & SOTG_PORTSC1_SUSP) && (!(temp & SOTG_PORTSC1_FPR)))
2867 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_FPR);
2869 /* wait 20ms for resume sequence to complete */
2870 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2872 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~(SOTG_PORTSC1_SUSP |
2873 SOTG_PORTSC1_FPR | SOTG_PORTSC1_LS /* High Speed */ ));
2875 /* 4ms settle time */
2876 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
2878 case UHF_PORT_INDICATOR:
2879 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PIC);
2882 case UHF_C_PORT_ENABLE:
2883 case UHF_C_PORT_OVER_CURRENT:
2884 case UHF_C_PORT_RESET:
2885 case UHF_C_PORT_SUSPEND:
2888 case UHF_PORT_POWER:
2889 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PP);
2891 case UHF_C_PORT_CONNECTION:
2892 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_ECSC);
2895 err = USB_ERR_IOERROR;
2900 tr_handle_set_port_feature_device:
2901 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
2904 case UHF_PORT_ENABLE:
2905 sc->sc_flags.port_enabled = 1;
2907 case UHF_PORT_SUSPEND:
2908 case UHF_PORT_RESET:
2910 case UHF_PORT_INDICATOR:
2913 case UHF_PORT_POWER:
2914 sc->sc_flags.port_powered = 1;
2917 err = USB_ERR_IOERROR;
2922 tr_handle_set_port_feature_host:
2923 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
2925 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2928 case UHF_PORT_ENABLE:
2929 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PED);
2931 case UHF_PORT_SUSPEND:
2932 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_SUSP);
2934 case UHF_PORT_RESET:
2935 DPRINTFN(6, "reset port %d\n", index);
2937 /* Start reset sequence. */
2938 temp &= ~(SOTG_PORTSC1_PED | SOTG_PORTSC1_PR);
2940 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PR);
2942 /* Wait for reset to complete. */
2943 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2944 USB_MS_TO_TICKS(usb_port_root_reset_delay));
2946 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp);
2948 /* Wait for HC to complete reset. */
2949 usb_pause_mtx(&sc->sc_bus.bus_mtx, USB_MS_TO_TICKS(2));
2951 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
2953 DPRINTF("After reset, status=0x%08x\n", temp);
2954 if (temp & SOTG_PORTSC1_PR) {
2955 device_printf(sc->sc_bus.bdev, "port reset timeout\n");
2956 err = USB_ERR_TIMEOUT;
2959 if (!(temp & SOTG_PORTSC1_PED)) {
2960 /* Not a high speed device, give up ownership.*/
2961 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PO);
2965 DPRINTF("port %d reset, status = 0x%08x\n", index, temp);
2967 case UHF_PORT_POWER:
2968 DPRINTFN(3, "set port power %d\n", index);
2969 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PP);
2973 DPRINTFN(3, "set port test %d\n", index);
2976 case UHF_PORT_INDICATOR:
2977 DPRINTFN(3, "set port ind %d\n", index);
2978 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PIC);
2981 err = USB_ERR_IOERROR;
2986 tr_handle_get_port_status_device:
2988 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d\n", index);
2990 if (sc->sc_flags.status_vbus) {
2991 saf1761_otg_pull_up(sc);
2993 saf1761_otg_pull_down(sc);
2996 /* Select FULL-speed and Device Side Mode */
2998 value = UPS_PORT_MODE_DEVICE;
3000 if (sc->sc_flags.port_powered)
3001 value |= UPS_PORT_POWER;
3003 if (sc->sc_flags.port_enabled)
3004 value |= UPS_PORT_ENABLED;
3006 if (sc->sc_flags.status_vbus &&
3007 sc->sc_flags.status_bus_reset)
3008 value |= UPS_CURRENT_CONNECT_STATUS;
3010 if (sc->sc_flags.status_suspend)
3011 value |= UPS_SUSPEND;
3013 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
3017 if (sc->sc_flags.change_connect)
3018 value |= UPS_C_CONNECT_STATUS;
3020 if (sc->sc_flags.change_suspend)
3021 value |= UPS_C_SUSPEND;
3023 USETW(sc->sc_hub_temp.ps.wPortChange, value);
3024 len = sizeof(sc->sc_hub_temp.ps);
3027 tr_handle_get_port_status_host:
3029 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3031 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d = 0x%08x\n", index, temp);
3035 if (temp & SOTG_PORTSC1_ECCS)
3036 i |= UPS_CURRENT_CONNECT_STATUS;
3037 if (temp & SOTG_PORTSC1_PED)
3038 i |= UPS_PORT_ENABLED;
3039 if ((temp & SOTG_PORTSC1_SUSP) && !(temp & SOTG_PORTSC1_FPR))
3041 if (temp & SOTG_PORTSC1_PR)
3043 if (temp & SOTG_PORTSC1_PP)
3044 i |= UPS_PORT_POWER;
3046 USETW(sc->sc_hub_temp.ps.wPortStatus, i);
3049 if (temp & SOTG_PORTSC1_ECSC)
3050 i |= UPS_C_CONNECT_STATUS;
3051 if (temp & SOTG_PORTSC1_FPR)
3054 i |= UPS_C_PORT_RESET;
3055 USETW(sc->sc_hub_temp.ps.wPortChange, i);
3056 len = sizeof(sc->sc_hub_temp.ps);
3059 tr_handle_get_class_descriptor:
3062 ptr = (const void *)&saf1761_otg_hubd;
3063 len = sizeof(saf1761_otg_hubd);
3067 err = USB_ERR_STALLED;
3075 saf1761_otg_xfer_setup(struct usb_setup_params *parm)
3077 struct saf1761_otg_softc *sc;
3078 struct usb_xfer *xfer;
3086 sc = SAF1761_OTG_BUS2SC(parm->udev->bus);
3087 xfer = parm->curr_xfer;
3090 * NOTE: This driver does not use any of the parameters that
3091 * are computed from the following values. Just set some
3092 * reasonable dummies:
3094 parm->hc_max_packet_size = 0x500;
3095 parm->hc_max_packet_count = 1;
3096 parm->hc_max_frame_size = 0x500;
3098 usbd_transfer_setup_sub(parm);
3101 * Compute maximum number of TDs:
3103 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
3105 if (ep_type == UE_CONTROL) {
3107 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
3110 ntd = xfer->nframes + 1 /* SYNC */ ;
3114 * check if "usbd_transfer_setup_sub" set an error
3120 * allocate transfer descriptors
3124 ep_no = xfer->endpointno & UE_ADDR;
3127 * Check profile stuff
3129 if (parm->udev->flags.usb_mode == USB_MODE_DEVICE) {
3130 const struct usb_hw_ep_profile *pf;
3132 saf1761_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
3135 /* should not happen */
3136 parm->err = USB_ERR_INVAL;
3141 dw1 = (xfer->address << 3) | (ep_type << 12);
3143 switch (parm->udev->speed) {
3144 case USB_SPEED_FULL:
3146 /* check if root HUB port is running High Speed */
3147 if (parm->udev->parent_hs_hub != NULL) {
3148 dw1 |= SOTG_PTD_DW1_ENABLE_SPLIT;
3149 dw1 |= (parm->udev->hs_port_no << 18);
3150 dw1 |= (parm->udev->hs_hub_addr << 25);
3151 if (parm->udev->speed == USB_SPEED_LOW)
3160 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
3162 for (n = 0; n != ntd; n++) {
3164 struct saf1761_otg_td *td;
3168 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
3171 td->max_packet_size = xfer->max_packet_size;
3172 td->ep_index = ep_no;
3173 td->ep_type = ep_type;
3174 td->dw1_value = dw1;
3176 if (ep_type == UE_INTERRUPT) {
3177 if (xfer->interval > 32)
3178 td->interval = (32 / 2) << 3;
3180 td->interval = (xfer->interval / 2) << 3;
3184 td->obj_next = last_obj;
3188 parm->size[0] += sizeof(*td);
3191 xfer->td_start[0] = last_obj;
3195 saf1761_otg_xfer_unsetup(struct usb_xfer *xfer)
3200 saf1761_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3201 struct usb_endpoint *ep)
3203 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3205 edesc->bEndpointAddress, udev->flags.usb_mode);
3207 if (udev->parent_hub == NULL) {
3208 /* root HUB has special endpoint handling */
3212 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3213 if (udev->speed != USB_SPEED_FULL &&
3214 udev->speed != USB_SPEED_HIGH) {
3218 switch (edesc->bmAttributes & UE_XFERTYPE) {
3219 case UE_ISOCHRONOUS:
3220 ep->methods = &saf1761_otg_device_isoc_methods;
3223 ep->methods = &saf1761_otg_non_isoc_methods;
3227 switch (edesc->bmAttributes & UE_XFERTYPE) {
3231 ep->methods = &saf1761_otg_non_isoc_methods;
3241 saf1761_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3243 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
3246 case USB_HW_POWER_SUSPEND:
3247 saf1761_otg_suspend(sc);
3249 case USB_HW_POWER_SHUTDOWN:
3250 saf1761_otg_uninit(sc);
3252 case USB_HW_POWER_RESUME:
3253 saf1761_otg_resume(sc);
3261 saf1761_otg_device_resume(struct usb_device *udev)
3263 struct saf1761_otg_softc *sc;
3264 struct saf1761_otg_td *td;
3265 struct usb_xfer *xfer;
3270 if (udev->flags.usb_mode != USB_MODE_HOST)
3273 sc = SAF1761_OTG_BUS2SC(udev->bus);
3275 USB_BUS_LOCK(&sc->sc_bus);
3276 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3278 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3280 if (xfer->xroot->udev != udev)
3283 td = xfer->td_transfer_cache;
3284 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3287 switch (td->ep_type) {
3289 x = td->channel - 32;
3290 sc->sc_host_intr_suspend_map &= ~(1 << x);
3291 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3292 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3294 case UE_ISOCHRONOUS:
3296 sc->sc_host_isoc_suspend_map &= ~(1 << x);
3297 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3298 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3301 x = td->channel - 64;
3302 sc->sc_host_async_suspend_map &= ~(1 << x);
3303 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3304 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3309 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3310 USB_BUS_UNLOCK(&sc->sc_bus);
3312 /* poll all transfers again to restart resumed ones */
3313 saf1761_otg_do_poll(&sc->sc_bus);
3317 saf1761_otg_device_suspend(struct usb_device *udev)
3319 struct saf1761_otg_softc *sc;
3320 struct saf1761_otg_td *td;
3321 struct usb_xfer *xfer;
3326 if (udev->flags.usb_mode != USB_MODE_HOST)
3329 sc = SAF1761_OTG_BUS2SC(udev->bus);
3331 USB_BUS_LOCK(&sc->sc_bus);
3332 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3334 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3336 if (xfer->xroot->udev != udev)
3339 td = xfer->td_transfer_cache;
3340 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3343 switch (td->ep_type) {
3345 x = td->channel - 32;
3346 sc->sc_host_intr_suspend_map |= (1 << x);
3347 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3348 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3350 case UE_ISOCHRONOUS:
3352 sc->sc_host_isoc_suspend_map |= (1 << x);
3353 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3354 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3357 x = td->channel - 64;
3358 sc->sc_host_async_suspend_map |= (1 << x);
3359 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3360 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3365 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3366 USB_BUS_UNLOCK(&sc->sc_bus);
3369 static const struct usb_bus_methods saf1761_otg_bus_methods =
3371 .endpoint_init = &saf1761_otg_ep_init,
3372 .xfer_setup = &saf1761_otg_xfer_setup,
3373 .xfer_unsetup = &saf1761_otg_xfer_unsetup,
3374 .get_hw_ep_profile = &saf1761_otg_get_hw_ep_profile,
3375 .xfer_stall = &saf1761_otg_xfer_stall,
3376 .set_stall = &saf1761_otg_set_stall,
3377 .clear_stall = &saf1761_otg_clear_stall,
3378 .roothub_exec = &saf1761_otg_roothub_exec,
3379 .xfer_poll = &saf1761_otg_do_poll,
3380 .set_hw_power_sleep = saf1761_otg_set_hw_power_sleep,
3381 .device_resume = &saf1761_otg_device_resume,
3382 .device_suspend = &saf1761_otg_device_suspend,