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;
115 static const struct usb_pipe_methods saf1761_otg_host_isoc_methods;
117 static saf1761_otg_cmd_t saf1761_host_setup_tx;
118 static saf1761_otg_cmd_t saf1761_host_bulk_data_rx;
119 static saf1761_otg_cmd_t saf1761_host_bulk_data_tx;
120 static saf1761_otg_cmd_t saf1761_host_intr_data_rx;
121 static saf1761_otg_cmd_t saf1761_host_intr_data_tx;
122 static saf1761_otg_cmd_t saf1761_host_isoc_data_rx;
123 static saf1761_otg_cmd_t saf1761_host_isoc_data_tx;
124 static saf1761_otg_cmd_t saf1761_device_setup_rx;
125 static saf1761_otg_cmd_t saf1761_device_data_rx;
126 static saf1761_otg_cmd_t saf1761_device_data_tx;
127 static saf1761_otg_cmd_t saf1761_device_data_tx_sync;
128 static void saf1761_otg_device_done(struct usb_xfer *, usb_error_t);
129 static void saf1761_otg_do_poll(struct usb_bus *);
130 static void saf1761_otg_standard_done(struct usb_xfer *);
131 static void saf1761_otg_intr_set(struct usb_xfer *, uint8_t);
132 static void saf1761_otg_root_intr(struct saf1761_otg_softc *);
135 * Here is a list of what the SAF1761 chip can support. The main
136 * limitation is that the sum of the buffer sizes must be less than
139 static const struct usb_hw_ep_profile saf1761_otg_ep_profile[] = {
142 .max_in_frame_size = 64,
143 .max_out_frame_size = 64,
145 .support_control = 1,
148 .max_in_frame_size = SOTG_HS_MAX_PACKET_SIZE,
149 .max_out_frame_size = SOTG_HS_MAX_PACKET_SIZE,
151 .support_interrupt = 1,
153 .support_isochronous = 1,
160 saf1761_otg_get_hw_ep_profile(struct usb_device *udev,
161 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
164 *ppf = saf1761_otg_ep_profile + 0;
165 } else if (ep_addr < 8) {
166 *ppf = saf1761_otg_ep_profile + 1;
173 saf1761_otg_pull_up(struct saf1761_otg_softc *sc)
175 /* activate pullup on D+, if possible */
177 if (!sc->sc_flags.d_pulled_up && sc->sc_flags.port_powered) {
180 sc->sc_flags.d_pulled_up = 1;
185 saf1761_otg_pull_down(struct saf1761_otg_softc *sc)
187 /* release pullup on D+, if possible */
189 if (sc->sc_flags.d_pulled_up) {
192 sc->sc_flags.d_pulled_up = 0;
197 saf1761_otg_wakeup_peer(struct saf1761_otg_softc *sc)
201 if (!(sc->sc_flags.status_suspend))
206 temp = SAF1761_READ_LE_4(sc, SOTG_MODE);
207 SAF1761_WRITE_LE_4(sc, SOTG_MODE, temp | SOTG_MODE_SNDRSU);
208 SAF1761_WRITE_LE_4(sc, SOTG_MODE, temp & ~SOTG_MODE_SNDRSU);
210 /* Wait 8ms for remote wakeup to complete. */
211 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
215 saf1761_host_channel_alloc(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
219 if (td->channel < SOTG_HOST_CHANNEL_MAX)
222 /* check if device is suspended */
223 if (SAF1761_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
224 return (1); /* busy - cannot transfer data */
226 switch (td->ep_type) {
228 for (x = 0; x != 32; x++) {
229 if (sc->sc_host_intr_map & (1 << x))
231 sc->sc_host_intr_map |= (1 << x);
232 td->channel = 32 + x;
237 for (x = 0; x != 32; x++) {
238 if (sc->sc_host_isoc_map & (1 << x))
240 sc->sc_host_isoc_map |= (1 << x);
246 for (x = 0; x != 32; x++) {
247 if (sc->sc_host_async_map & (1 << x))
249 sc->sc_host_async_map |= (1 << x);
250 td->channel = 64 + x;
259 saf1761_host_channel_free(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
263 if (td->channel >= SOTG_HOST_CHANNEL_MAX)
266 switch (td->ep_type) {
268 x = td->channel - 32;
269 td->channel = SOTG_HOST_CHANNEL_MAX;
270 sc->sc_host_intr_map &= ~(1 << x);
271 sc->sc_host_intr_suspend_map &= ~(1 << x);
272 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
273 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
277 td->channel = SOTG_HOST_CHANNEL_MAX;
278 sc->sc_host_isoc_map &= ~(1 << x);
279 sc->sc_host_isoc_suspend_map &= ~(1 << x);
280 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
281 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
284 x = td->channel - 64;
285 td->channel = SOTG_HOST_CHANNEL_MAX;
286 sc->sc_host_async_map &= ~(1 << x);
287 sc->sc_host_async_suspend_map &= ~(1 << x);
288 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
289 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
295 saf1761_peek_host_status_le_4(struct saf1761_otg_softc *sc, uint32_t offset)
301 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
302 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
303 retval = SAF1761_READ_LE_4(sc, offset);
307 DPRINTF("STAUS is zero at offset 0x%x\n", offset);
315 saf1761_read_host_memory(struct saf1761_otg_softc *sc,
316 struct saf1761_otg_td *td, uint32_t len)
318 struct usb_page_search buf_res;
325 offset = SOTG_DATA_ADDR(td->channel);
326 SAF1761_WRITE_LE_4(sc, SOTG_MEMORY_REG, offset);
327 SAF1761_90NS_DELAY(sc); /* read prefetch time is 90ns */
329 /* optimised read first */
331 usbd_get_page(td->pc, td->offset, &buf_res);
333 /* get correct length */
334 if (buf_res.length > len)
335 buf_res.length = len;
337 /* check buffer alignment */
338 if (((uintptr_t)buf_res.buffer) & 3)
341 count = buf_res.length & ~3;
345 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
346 offset, buf_res.buffer, count / 4);
351 /* update remainder and offset */
352 td->remainder -= count;
357 /* use bounce buffer */
358 bus_space_read_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
359 offset, sc->sc_bounce_buffer, (len + 3) / 4);
360 usbd_copy_in(td->pc, td->offset,
361 sc->sc_bounce_buffer, len);
363 /* update remainder and offset */
364 td->remainder -= len;
370 saf1761_write_host_memory(struct saf1761_otg_softc *sc,
371 struct saf1761_otg_td *td, uint32_t len)
373 struct usb_page_search buf_res;
380 offset = SOTG_DATA_ADDR(td->channel);
382 /* optimised write first */
384 usbd_get_page(td->pc, td->offset, &buf_res);
386 /* get correct length */
387 if (buf_res.length > len)
388 buf_res.length = len;
390 /* check buffer alignment */
391 if (((uintptr_t)buf_res.buffer) & 3)
394 count = buf_res.length & ~3;
398 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
399 offset, buf_res.buffer, count / 4);
404 /* update remainder and offset */
405 td->remainder -= count;
409 /* use bounce buffer */
410 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
411 bus_space_write_region_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
412 offset, sc->sc_bounce_buffer, (len + 3) / 4);
414 /* update remainder and offset */
415 td->remainder -= len;
421 saf1761_host_setup_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
428 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
429 pdt_addr = SOTG_PTD(td->channel);
431 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
433 DPRINTFN(5, "STATUS=0x%08x\n", status);
435 if (status & SOTG_PTD_DW3_ACTIVE) {
437 } else if (status & SOTG_PTD_DW3_HALTED) {
442 if (saf1761_host_channel_alloc(sc, td))
447 if (count != td->remainder) {
452 saf1761_write_host_memory(sc, td, count);
454 pdt_addr = SOTG_PTD(td->channel);
456 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
457 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
458 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
459 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
461 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
462 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
464 temp = SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8;
465 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
467 temp = td->dw1_value | (2 << 10) /* SETUP PID */ | (td->ep_index >> 1);
468 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
470 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
471 (td->max_packet_size << 18) /* wMaxPacketSize */ |
472 (count << 3) /* transfer count */ |
474 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
477 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
478 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
482 return (1); /* busy */
484 saf1761_host_channel_free(sc, td);
485 return (0); /* complete */
489 saf1761_host_bulk_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
494 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
499 pdt_addr = SOTG_PTD(td->channel);
501 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
503 DPRINTFN(5, "STATUS=0x%08x\n", status);
505 if (status & SOTG_PTD_DW3_ACTIVE) {
507 } else if (status & SOTG_PTD_DW3_HALTED) {
508 if (!(status & SOTG_PTD_DW3_ERRORS))
513 count = (status & SOTG_PTD_DW3_XFER_COUNT);
516 /* verify the packet byte count */
517 if (count != td->max_packet_size) {
518 if (count < td->max_packet_size) {
519 /* we have a short packet */
523 /* invalid USB packet */
530 /* verify the packet byte count */
531 if (count > td->remainder) {
532 /* invalid USB packet */
537 saf1761_read_host_memory(sc, td, count);
539 /* check if we are complete */
540 if ((td->remainder == 0) || got_short) {
543 /* else need to receive a zero length packet */
545 saf1761_host_channel_free(sc, td);
547 if (saf1761_host_channel_alloc(sc, td))
550 /* set toggle, if any */
551 if (td->set_toggle) {
556 /* receive one more packet */
558 pdt_addr = SOTG_PTD(td->channel);
560 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
561 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
562 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
563 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
565 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
567 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
569 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
570 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
572 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
573 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
575 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
576 (td->max_packet_size << 18) /* wMaxPacketSize */ |
577 (td->max_packet_size << 3) /* transfer count */ |
579 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
582 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
583 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
585 return (1); /* busy */
587 saf1761_host_channel_free(sc, td);
588 return (0); /* complete */
592 saf1761_host_bulk_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
598 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
601 pdt_addr = SOTG_PTD(td->channel);
603 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
605 DPRINTFN(5, "STATUS=0x%08x\n", status);
607 if (status & SOTG_PTD_DW3_ACTIVE) {
609 } else if (status & SOTG_PTD_DW3_HALTED) {
610 if (!(status & SOTG_PTD_DW3_ERRORS))
615 /* check remainder */
616 if (td->remainder == 0) {
619 /* else we need to transmit a short packet */
621 saf1761_host_channel_free(sc, td);
623 if (saf1761_host_channel_alloc(sc, td))
626 count = td->max_packet_size;
627 if (td->remainder < count) {
628 /* we have a short packet */
630 count = td->remainder;
633 saf1761_write_host_memory(sc, td, count);
635 /* set toggle, if any */
636 if (td->set_toggle) {
641 /* send one more packet */
643 pdt_addr = SOTG_PTD(td->channel);
645 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
646 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
647 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
648 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, 0);
650 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) |
652 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
654 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
655 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
657 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
658 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
660 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
661 (td->max_packet_size << 18) /* wMaxPacketSize */ |
662 (count << 3) /* transfer count */ |
664 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
667 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
668 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
672 return (1); /* busy */
674 saf1761_host_channel_free(sc, td);
675 return (0); /* complete */
679 saf1761_host_intr_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
684 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
689 pdt_addr = SOTG_PTD(td->channel);
691 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
693 DPRINTFN(5, "STATUS=0x%08x\n", status);
695 if (status & SOTG_PTD_DW3_ACTIVE) {
697 } else if (status & SOTG_PTD_DW3_HALTED) {
698 if (!(status & SOTG_PTD_DW3_ERRORS))
703 count = (status & SOTG_PTD_DW3_XFER_COUNT);
706 /* verify the packet byte count */
707 if (count != td->max_packet_size) {
708 if (count < td->max_packet_size) {
709 /* we have a short packet */
713 /* invalid USB packet */
720 /* verify the packet byte count */
721 if (count > td->remainder) {
722 /* invalid USB packet */
727 saf1761_read_host_memory(sc, td, count);
729 /* check if we are complete */
730 if ((td->remainder == 0) || got_short) {
733 /* else need to receive a zero length packet */
735 saf1761_host_channel_free(sc, td);
737 if (saf1761_host_channel_alloc(sc, td))
740 /* set toggle, if any */
741 if (td->set_toggle) {
746 /* receive one more packet */
748 pdt_addr = SOTG_PTD(td->channel);
750 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
751 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
753 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
754 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
756 temp = (1U << td->uframe); /* start split */
757 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
759 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
760 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
762 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) |
763 (td->interval & 0xF8);
764 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
766 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
767 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
769 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
770 (td->max_packet_size << 18) /* wMaxPacketSize */ |
771 (td->max_packet_size << 3) /* transfer count */ |
773 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
776 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
777 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
779 return (1); /* busy */
781 saf1761_host_channel_free(sc, td);
782 return (0); /* complete */
786 saf1761_host_intr_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
792 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
795 pdt_addr = SOTG_PTD(td->channel);
797 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
799 DPRINTFN(5, "STATUS=0x%08x\n", status);
801 if (status & SOTG_PTD_DW3_ACTIVE) {
803 } else if (status & SOTG_PTD_DW3_HALTED) {
804 if (!(status & SOTG_PTD_DW3_ERRORS))
810 /* check remainder */
811 if (td->remainder == 0) {
814 /* else we need to transmit a short packet */
816 saf1761_host_channel_free(sc, td);
818 if (saf1761_host_channel_alloc(sc, td))
821 count = td->max_packet_size;
822 if (td->remainder < count) {
823 /* we have a short packet */
825 count = td->remainder;
828 saf1761_write_host_memory(sc, td, count);
830 /* set toggle, if any */
831 if (td->set_toggle) {
836 /* send one more packet */
838 pdt_addr = SOTG_PTD(td->channel);
840 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
841 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
843 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
844 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
846 temp = (1U << td->uframe); /* start split */
847 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
849 temp = SOTG_PTD_DW3_ACTIVE | (td->toggle << 25) | SOTG_PTD_DW3_CERR_3;
850 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
852 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8) |
853 (td->interval & 0xF8);
854 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
856 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
857 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
859 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
860 (td->max_packet_size << 18) /* wMaxPacketSize */ |
861 (count << 3) /* transfer count */ |
863 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
866 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
867 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
871 return (1); /* busy */
873 saf1761_host_channel_free(sc, td);
874 return (0); /* complete */
878 saf1761_host_isoc_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
883 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
887 pdt_addr = SOTG_PTD(td->channel);
889 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
891 DPRINTFN(5, "STATUS=0x%08x\n", status);
893 if (status & SOTG_PTD_DW3_ACTIVE) {
895 } else if (status & SOTG_PTD_DW3_HALTED) {
898 count = (status & SOTG_PTD_DW3_XFER_COUNT);
900 /* verify the packet byte count */
901 if (count != td->max_packet_size) {
902 if (count < td->max_packet_size) {
903 /* we have a short packet */
906 /* invalid USB packet */
912 /* verify the packet byte count */
913 if (count > td->remainder) {
914 /* invalid USB packet */
919 saf1761_read_host_memory(sc, td, count);
923 if (saf1761_host_channel_alloc(sc, td))
926 /* receive one more packet */
928 pdt_addr = SOTG_PTD(td->channel);
930 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
931 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
933 temp = (0xFC << td->uframe) & 0xFF; /* complete split */
934 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, temp);
936 temp = (1U << td->uframe); /* start split */
937 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
939 temp = SOTG_PTD_DW3_ACTIVE | SOTG_PTD_DW3_CERR_3;
940 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
942 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
943 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
945 temp = td->dw1_value | (1 << 10) /* IN-PID */ | (td->ep_index >> 1);
946 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
948 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
949 (td->max_packet_size << 18) /* wMaxPacketSize */ |
950 (td->max_packet_size << 3) /* transfer count */ |
952 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
955 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
956 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
958 return (1); /* busy */
960 saf1761_host_channel_free(sc, td);
961 return (0); /* complete */
965 saf1761_host_isoc_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
971 if (td->channel < SOTG_HOST_CHANNEL_MAX) {
974 pdt_addr = SOTG_PTD(td->channel);
976 status = saf1761_peek_host_status_le_4(sc, pdt_addr + SOTG_PTD_DW3);
978 DPRINTFN(5, "STATUS=0x%08x\n", status);
980 if (status & SOTG_PTD_DW3_ACTIVE) {
982 } else if (status & SOTG_PTD_DW3_HALTED) {
988 if (saf1761_host_channel_alloc(sc, td))
991 count = td->max_packet_size;
992 if (td->remainder < count) {
993 /* we have a short packet */
995 count = td->remainder;
998 saf1761_write_host_memory(sc, td, count);
1000 /* send one more packet */
1002 pdt_addr = SOTG_PTD(td->channel);
1004 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW7, 0);
1005 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW6, 0);
1006 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW5, 0);
1008 temp = (1U << td->uframe); /* start split */
1009 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW4, temp);
1011 temp = SOTG_PTD_DW3_ACTIVE | SOTG_PTD_DW3_CERR_3;
1012 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW3, temp);
1014 temp = (SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
1015 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW2, temp);
1017 temp = td->dw1_value | (0 << 10) /* OUT-PID */ | (td->ep_index >> 1);
1018 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW1, temp);
1020 temp = (td->ep_index << 31) | (1 << 29) /* pkt-multiplier */ |
1021 (count << 18) /* wMaxPacketSize */ |
1022 (count << 3) /* transfer count */ |
1024 SAF1761_WRITE_LE_4(sc, pdt_addr + SOTG_PTD_DW0, temp);
1027 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
1028 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
1030 return (1); /* busy */
1032 saf1761_host_channel_free(sc, td);
1033 return (0); /* complete */
1037 saf1761_otg_set_address(struct saf1761_otg_softc *sc, uint8_t addr)
1039 DPRINTFN(5, "addr=%d\n", addr);
1041 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, addr | SOTG_ADDRESS_ENABLE);
1046 saf1761_read_device_fifo(struct saf1761_otg_softc *sc,
1047 struct saf1761_otg_td *td, uint32_t len)
1049 struct usb_page_search buf_res;
1052 /* optimised read first */
1054 usbd_get_page(td->pc, td->offset, &buf_res);
1056 /* get correct length */
1057 if (buf_res.length > len)
1058 buf_res.length = len;
1060 /* check buffer alignment */
1061 if (((uintptr_t)buf_res.buffer) & 3)
1064 count = buf_res.length & ~3;
1068 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
1069 SOTG_DATA_PORT, buf_res.buffer, count / 4);
1073 /* update remainder and offset */
1074 td->remainder -= count;
1075 td->offset += count;
1079 /* use bounce buffer */
1080 bus_space_read_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
1081 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
1082 usbd_copy_in(td->pc, td->offset,
1083 sc->sc_bounce_buffer, len);
1085 /* update remainder and offset */
1086 td->remainder -= len;
1092 saf1761_write_device_fifo(struct saf1761_otg_softc *sc,
1093 struct saf1761_otg_td *td, uint32_t len)
1095 struct usb_page_search buf_res;
1098 /* optimised write first */
1100 usbd_get_page(td->pc, td->offset, &buf_res);
1102 /* get correct length */
1103 if (buf_res.length > len)
1104 buf_res.length = len;
1106 /* check buffer alignment */
1107 if (((uintptr_t)buf_res.buffer) & 3)
1110 count = buf_res.length & ~3;
1114 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
1115 SOTG_DATA_PORT, buf_res.buffer, count / 4);
1119 /* update remainder and offset */
1120 td->remainder -= count;
1121 td->offset += count;
1124 /* use bounce buffer */
1125 usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, len);
1126 bus_space_write_multi_4((sc)->sc_io_tag, (sc)->sc_io_hdl,
1127 SOTG_DATA_PORT, sc->sc_bounce_buffer, (len + 3) / 4);
1129 /* update remainder and offset */
1130 td->remainder -= len;
1136 saf1761_device_setup_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1138 struct usb_device_request req;
1141 /* select the correct endpoint */
1142 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1144 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1146 /* check buffer status */
1147 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1150 /* get buffer length */
1151 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1153 DPRINTFN(5, "count=%u rem=%u\n", count, td->remainder);
1155 /* clear did stall */
1159 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
1161 /* verify data length */
1162 if (count != td->remainder) {
1163 DPRINTFN(0, "Invalid SETUP packet "
1164 "length, %d bytes\n", count);
1167 if (count != sizeof(req)) {
1168 DPRINTFN(0, "Unsupported SETUP packet "
1169 "length, %d bytes\n", count);
1173 saf1761_read_device_fifo(sc, td, sizeof(req));
1175 /* extract SETUP packet again */
1176 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1178 /* sneak peek the set address request */
1179 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1180 (req.bRequest == UR_SET_ADDRESS)) {
1181 sc->sc_dv_addr = req.wValue[0] & 0x7F;
1182 DPRINTF("Set address %d\n", sc->sc_dv_addr);
1184 sc->sc_dv_addr = 0xFF;
1186 return (0); /* complete */
1189 /* abort any ongoing transfer */
1190 if (!td->did_stall) {
1191 DPRINTFN(5, "stalling\n");
1194 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
1198 return (1); /* not complete */
1202 saf1761_device_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1205 uint8_t got_short = 0;
1207 if (td->ep_index == 0) {
1208 /* select the correct endpoint */
1209 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1211 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1213 /* check buffer status */
1214 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1216 if (td->remainder == 0) {
1218 * We are actually complete and have
1219 * received the next SETUP:
1221 DPRINTFN(5, "faking complete\n");
1222 return (0); /* complete */
1224 DPRINTFN(5, "SETUP packet while receiving data\n");
1226 * USB Host Aborted the transfer.
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_OUT);
1237 /* enable data stage */
1238 if (td->set_toggle) {
1240 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1243 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1245 /* check buffer status */
1246 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) == 0)
1247 return (1); /* not complete */
1249 /* get buffer length */
1250 count &= SOTG_BUF_LENGTH_BUFLEN_MASK;
1252 DPRINTFN(5, "rem=%u count=0x%04x\n", td->remainder, count);
1254 /* verify the packet byte count */
1255 if (count != td->max_packet_size) {
1256 if (count < td->max_packet_size) {
1257 /* we have a short packet */
1261 /* invalid USB packet */
1263 return (0); /* we are complete */
1266 /* verify the packet byte count */
1267 if (count > td->remainder) {
1268 /* invalid USB packet */
1270 return (0); /* we are complete */
1273 saf1761_read_device_fifo(sc, td, count);
1275 /* check if we are complete */
1276 if ((td->remainder == 0) || got_short) {
1277 if (td->short_pkt) {
1278 /* we are complete */
1281 /* else need to receive a zero length packet */
1283 return (1); /* not complete */
1287 saf1761_device_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1291 if (td->ep_index == 0) {
1292 /* select the correct endpoint */
1293 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1295 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1297 /* check buffer status */
1298 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1299 DPRINTFN(5, "SETUP abort\n");
1301 * USB Host Aborted the transfer.
1304 return (0); /* complete */
1307 /* select the correct endpoint */
1308 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1309 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1310 SOTG_EP_INDEX_DIR_IN);
1312 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1314 /* check buffer status */
1315 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1316 return (1); /* not complete */
1318 /* enable data stage */
1319 if (td->set_toggle) {
1321 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
1324 DPRINTFN(5, "rem=%u\n", td->remainder);
1326 count = td->max_packet_size;
1327 if (td->remainder < count) {
1328 /* we have a short packet */
1330 count = td->remainder;
1333 saf1761_write_device_fifo(sc, td, count);
1335 if (td->ep_index == 0) {
1336 if (count < SOTG_FS_MAX_PACKET_SIZE) {
1337 /* set end of packet */
1338 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1341 if (count < SOTG_HS_MAX_PACKET_SIZE) {
1342 /* set end of packet */
1343 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
1347 /* check remainder */
1348 if (td->remainder == 0) {
1349 if (td->short_pkt) {
1350 return (0); /* complete */
1352 /* else we need to transmit a short packet */
1354 return (1); /* not complete */
1358 saf1761_device_data_tx_sync(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
1362 if (td->ep_index == 0) {
1363 /* select the correct endpoint */
1364 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
1366 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1368 /* check buffer status */
1369 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0) {
1370 DPRINTFN(5, "Faking complete\n");
1371 return (0); /* complete */
1374 /* select the correct endpoint */
1375 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
1376 (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
1377 SOTG_EP_INDEX_DIR_IN);
1379 count = SAF1761_READ_LE_4(sc, SOTG_BUF_LENGTH);
1381 /* check buffer status */
1382 if ((count & SOTG_BUF_LENGTH_FILLED_MASK) != 0)
1383 return (1); /* busy */
1385 if (sc->sc_dv_addr != 0xFF) {
1386 /* write function address */
1387 saf1761_otg_set_address(sc, sc->sc_dv_addr);
1389 return (0); /* complete */
1393 saf1761_otg_xfer_do_fifo(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1395 struct saf1761_otg_td *td;
1400 td = xfer->td_transfer_cache;
1405 if ((td->func) (sc, td)) {
1406 /* operation in progress */
1409 if (((void *)td) == xfer->td_transfer_last) {
1412 if (td->error_any) {
1414 } else if (td->remainder > 0) {
1416 * We had a short transfer. If there is no alternate
1417 * next, stop processing !
1419 if (!td->alt_next) {
1424 * Fetch the next transfer descriptor.
1426 toggle = td->toggle;
1428 td->toggle = toggle;
1429 xfer->td_transfer_cache = td;
1434 /* compute all actual lengths */
1435 xfer->td_transfer_cache = NULL;
1436 sc->sc_xfer_complete = 1;
1440 saf1761_otg_xfer_do_complete(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
1442 struct saf1761_otg_td *td;
1446 td = xfer->td_transfer_cache;
1448 /* compute all actual lengths */
1449 saf1761_otg_standard_done(xfer);
1456 saf1761_otg_interrupt_poll_locked(struct saf1761_otg_softc *sc)
1458 struct usb_xfer *xfer;
1460 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
1461 saf1761_otg_xfer_do_fifo(sc, xfer);
1465 saf1761_otg_wait_suspend(struct saf1761_otg_softc *sc, uint8_t on)
1468 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IESUSP;
1469 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IERESM;
1471 sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IESUSP;
1472 sc->sc_intr_enable |= SOTG_DCINTERRUPT_IERESM;
1474 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1478 saf1761_otg_update_vbus(struct saf1761_otg_softc *sc)
1482 /* read fresh status */
1483 status = SAF1761_READ_LE_4(sc, SOTG_STATUS);
1485 DPRINTFN(4, "STATUS=0x%04x\n", status);
1487 if ((status & SOTG_STATUS_VBUS_VLD) &&
1488 (status & SOTG_STATUS_ID)) {
1489 /* VBUS present and device mode */
1490 if (!sc->sc_flags.status_vbus) {
1491 sc->sc_flags.status_vbus = 1;
1493 /* complete root HUB interrupt endpoint */
1494 saf1761_otg_root_intr(sc);
1497 /* VBUS not-present or host mode */
1498 if (sc->sc_flags.status_vbus) {
1499 sc->sc_flags.status_vbus = 0;
1500 sc->sc_flags.status_bus_reset = 0;
1501 sc->sc_flags.status_suspend = 0;
1502 sc->sc_flags.change_suspend = 0;
1503 sc->sc_flags.change_connect = 1;
1505 /* complete root HUB interrupt endpoint */
1506 saf1761_otg_root_intr(sc);
1512 saf1761_otg_interrupt_complete_locked(struct saf1761_otg_softc *sc)
1514 struct usb_xfer *xfer;
1516 /* scan for completion events */
1517 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1518 if (saf1761_otg_xfer_do_complete(sc, xfer))
1524 saf1761_otg_filter_interrupt(void *arg)
1526 struct saf1761_otg_softc *sc = arg;
1527 int retval = FILTER_HANDLED;
1531 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1533 hcstat = SAF1761_READ_LE_4(sc, SOTG_HCINTERRUPT);
1534 /* acknowledge all host controller interrupts */
1535 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT, hcstat);
1537 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT);
1538 /* acknowledge all device controller interrupts */
1539 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT,
1540 status & ~SAF1761_DCINTERRUPT_THREAD_IRQ);
1542 (void) SAF1761_READ_LE_4(sc, SOTG_ATL_PTD_DONE_PTD);
1543 (void) SAF1761_READ_LE_4(sc, SOTG_INT_PTD_DONE_PTD);
1544 (void) SAF1761_READ_LE_4(sc, SOTG_ISO_PTD_DONE_PTD);
1546 if (status & SAF1761_DCINTERRUPT_THREAD_IRQ)
1547 retval = FILTER_SCHEDULE_THREAD;
1549 /* poll FIFOs, if any */
1550 saf1761_otg_interrupt_poll_locked(sc);
1552 if (sc->sc_xfer_complete != 0)
1553 retval = FILTER_SCHEDULE_THREAD;
1555 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1561 saf1761_otg_interrupt(void *arg)
1563 struct saf1761_otg_softc *sc = arg;
1566 USB_BUS_LOCK(&sc->sc_bus);
1567 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1569 status = SAF1761_READ_LE_4(sc, SOTG_DCINTERRUPT) &
1570 SAF1761_DCINTERRUPT_THREAD_IRQ;
1572 /* acknowledge all device controller interrupts */
1573 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT, status);
1575 DPRINTF("DCINTERRUPT=0x%08x SOF=0x%08x "
1576 "FRINDEX=0x%08x\n", status,
1577 SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM),
1578 SAF1761_READ_LE_4(sc, SOTG_FRINDEX));
1580 /* update VBUS and ID bits, if any */
1581 if (status & SOTG_DCINTERRUPT_IEVBUS)
1582 saf1761_otg_update_vbus(sc);
1584 if (status & SOTG_DCINTERRUPT_IEBRST) {
1586 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1587 SOTG_UNLOCK_DEVICE_CODE);
1589 /* Enable device address */
1590 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS,
1591 SOTG_ADDRESS_ENABLE);
1593 sc->sc_flags.status_bus_reset = 1;
1594 sc->sc_flags.status_suspend = 0;
1595 sc->sc_flags.change_suspend = 0;
1596 sc->sc_flags.change_connect = 1;
1598 /* disable resume interrupt */
1599 saf1761_otg_wait_suspend(sc, 1);
1600 /* complete root HUB interrupt endpoint */
1601 saf1761_otg_root_intr(sc);
1604 * If "RESUME" and "SUSPEND" is set at the same time we
1605 * interpret that like "RESUME". Resume is set when there is
1606 * at least 3 milliseconds of inactivity on the USB BUS:
1608 if (status & SOTG_DCINTERRUPT_IERESM) {
1610 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE,
1611 SOTG_UNLOCK_DEVICE_CODE);
1613 if (sc->sc_flags.status_suspend) {
1614 sc->sc_flags.status_suspend = 0;
1615 sc->sc_flags.change_suspend = 1;
1616 /* disable resume interrupt */
1617 saf1761_otg_wait_suspend(sc, 1);
1618 /* complete root HUB interrupt endpoint */
1619 saf1761_otg_root_intr(sc);
1621 } else if (status & SOTG_DCINTERRUPT_IESUSP) {
1622 if (!sc->sc_flags.status_suspend) {
1623 sc->sc_flags.status_suspend = 1;
1624 sc->sc_flags.change_suspend = 1;
1625 /* enable resume interrupt */
1626 saf1761_otg_wait_suspend(sc, 0);
1627 /* complete root HUB interrupt endpoint */
1628 saf1761_otg_root_intr(sc);
1632 if (sc->sc_xfer_complete != 0) {
1633 sc->sc_xfer_complete = 0;
1635 /* complete FIFOs, if any */
1636 saf1761_otg_interrupt_complete_locked(sc);
1638 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1639 USB_BUS_UNLOCK(&sc->sc_bus);
1643 saf1761_otg_setup_standard_chain_sub(struct saf1761_otg_std_temp *temp)
1645 struct saf1761_otg_td *td;
1647 /* get current Transfer Descriptor */
1651 /* prepare for next TD */
1652 temp->td_next = td->obj_next;
1654 /* fill out the Transfer Descriptor */
1655 td->func = temp->func;
1657 td->offset = temp->offset;
1658 td->remainder = temp->len;
1660 td->error_stall = 0;
1662 td->did_stall = temp->did_stall;
1663 td->short_pkt = temp->short_pkt;
1664 td->alt_next = temp->setup_alt_next;
1665 td->channel = SOTG_HOST_CHANNEL_MAX;
1669 saf1761_otg_setup_standard_chain(struct usb_xfer *xfer)
1671 struct saf1761_otg_std_temp temp;
1672 struct saf1761_otg_softc *sc;
1673 struct saf1761_otg_td *td;
1680 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1681 xfer->address, UE_GET_ADDR(xfer->endpointno),
1682 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1684 temp.max_frame_size = xfer->max_frame_size;
1686 td = xfer->td_start[0];
1687 xfer->td_transfer_first = td;
1688 xfer->td_transfer_cache = td;
1694 temp.td_next = xfer->td_start[0];
1696 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
1697 xfer->flags_int.isochronous_xfr;
1698 temp.did_stall = !xfer->flags_int.control_stall;
1700 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
1702 sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1703 ep_no = (xfer->endpointno & UE_ADDR);
1704 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
1706 /* check if we should prepend a setup message */
1708 if (xfer->flags_int.control_xfr) {
1709 if (xfer->flags_int.control_hdr) {
1712 temp.func = &saf1761_host_setup_tx;
1714 temp.func = &saf1761_device_setup_rx;
1716 temp.len = xfer->frlengths[0];
1717 temp.pc = xfer->frbuffers + 0;
1718 temp.short_pkt = temp.len ? 1 : 0;
1719 /* check for last frame */
1720 if (xfer->nframes == 1) {
1721 /* no STATUS stage yet, SETUP is last */
1722 if (xfer->flags_int.control_act)
1723 temp.setup_alt_next = 0;
1725 saf1761_otg_setup_standard_chain_sub(&temp);
1732 if (x != xfer->nframes) {
1733 if (xfer->endpointno & UE_DIR_IN) {
1735 if (ep_type == UE_INTERRUPT)
1736 temp.func = &saf1761_host_intr_data_rx;
1737 else if (ep_type == UE_ISOCHRONOUS)
1738 temp.func = &saf1761_host_isoc_data_rx;
1740 temp.func = &saf1761_host_bulk_data_rx;
1743 temp.func = &saf1761_device_data_tx;
1748 if (ep_type == UE_INTERRUPT)
1749 temp.func = &saf1761_host_intr_data_tx;
1750 else if (ep_type == UE_ISOCHRONOUS)
1751 temp.func = &saf1761_host_isoc_data_tx;
1753 temp.func = &saf1761_host_bulk_data_tx;
1756 temp.func = &saf1761_device_data_rx;
1761 /* setup "pc" pointer */
1762 temp.pc = xfer->frbuffers + x;
1767 while (x != xfer->nframes) {
1769 /* DATA0 / DATA1 message */
1771 temp.len = xfer->frlengths[x];
1775 if (x == xfer->nframes) {
1776 if (xfer->flags_int.control_xfr) {
1777 if (xfer->flags_int.control_act) {
1778 temp.setup_alt_next = 0;
1781 temp.setup_alt_next = 0;
1784 if (temp.len == 0) {
1786 /* make sure that we send an USB packet */
1792 /* regular data transfer */
1794 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1797 saf1761_otg_setup_standard_chain_sub(&temp);
1799 if (xfer->flags_int.isochronous_xfr) {
1800 temp.offset += temp.len;
1802 /* get next Page Cache pointer */
1803 temp.pc = xfer->frbuffers + x;
1807 /* check for control transfer */
1808 if (xfer->flags_int.control_xfr) {
1809 /* always setup a valid "pc" pointer for status and sync */
1810 temp.pc = xfer->frbuffers + 0;
1813 temp.setup_alt_next = 0;
1815 /* check if we should append a status stage */
1816 if (!xfer->flags_int.control_act) {
1819 * Send a DATA1 message and invert the current
1820 * endpoint direction.
1822 if (xfer->endpointno & UE_DIR_IN) {
1824 temp.func = &saf1761_host_bulk_data_tx;
1827 temp.func = &saf1761_device_data_rx;
1832 temp.func = &saf1761_host_bulk_data_rx;
1835 temp.func = &saf1761_device_data_tx;
1842 saf1761_otg_setup_standard_chain_sub(&temp);
1844 /* data toggle should be DATA1 */
1849 /* we need a SYNC point after TX */
1850 temp.func = &saf1761_device_data_tx_sync;
1851 saf1761_otg_setup_standard_chain_sub(&temp);
1856 temp.pc = xfer->frbuffers + 0;
1859 temp.setup_alt_next = 0;
1861 /* we need a SYNC point after TX */
1862 temp.func = &saf1761_device_data_tx_sync;
1863 saf1761_otg_setup_standard_chain_sub(&temp);
1867 /* must have at least one frame! */
1869 xfer->td_transfer_last = td;
1872 /* get first again */
1873 td = xfer->td_transfer_first;
1874 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
1879 saf1761_otg_timeout(void *arg)
1881 struct usb_xfer *xfer = arg;
1883 DPRINTF("xfer=%p\n", xfer);
1885 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1887 /* transfer is transferred */
1888 saf1761_otg_device_done(xfer, USB_ERR_TIMEOUT);
1892 saf1761_otg_intr_set(struct usb_xfer *xfer, uint8_t set)
1894 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1895 uint8_t ep_no = (xfer->endpointno & UE_ADDR);
1898 DPRINTFN(15, "endpoint=%d set=%d\n", xfer->endpointno, set);
1901 mask = SOTG_DCINTERRUPT_IEPRX(0) |
1902 SOTG_DCINTERRUPT_IEPTX(0) |
1903 SOTG_DCINTERRUPT_IEP0SETUP;
1904 } else if (xfer->endpointno & UE_DIR_IN) {
1905 mask = SOTG_DCINTERRUPT_IEPTX(ep_no);
1907 mask = SOTG_DCINTERRUPT_IEPRX(ep_no);
1911 sc->sc_intr_enable |= mask;
1913 sc->sc_intr_enable &= ~mask;
1915 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
1919 saf1761_otg_start_standard_chain(struct usb_xfer *xfer)
1921 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
1925 USB_BUS_SPIN_LOCK(&sc->sc_bus);
1928 saf1761_otg_xfer_do_fifo(sc, xfer);
1930 if (xfer->td_transfer_cache != NULL) {
1932 * Only enable the endpoint interrupt when we are
1933 * actually waiting for data, hence we are dealing
1934 * with level triggered interrupts !
1936 saf1761_otg_intr_set(xfer, 1);
1938 /* put transfer on interrupt queue */
1939 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1941 /* start timeout, if any */
1942 if (xfer->timeout != 0) {
1943 usbd_transfer_timeout_ms(xfer,
1944 &saf1761_otg_timeout, xfer->timeout);
1947 /* catch completion, if any */
1948 saf1761_otg_interrupt_complete_locked(sc);
1950 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
1954 saf1761_otg_root_intr(struct saf1761_otg_softc *sc)
1958 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1960 /* set port bit - we only have one port */
1961 sc->sc_hub_idata[0] = 0x02;
1963 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1964 sizeof(sc->sc_hub_idata));
1968 saf1761_otg_standard_done_sub(struct usb_xfer *xfer)
1970 struct saf1761_otg_td *td;
1976 td = xfer->td_transfer_cache;
1979 len = td->remainder;
1981 /* store last data toggle */
1982 xfer->endpoint->toggle_next = td->toggle;
1984 if (xfer->aframes != xfer->nframes) {
1986 * Verify the length and subtract
1987 * the remainder from "frlengths[]":
1989 if (len > xfer->frlengths[xfer->aframes]) {
1992 xfer->frlengths[xfer->aframes] -= len;
1995 /* Check for transfer error */
1996 if (td->error_any) {
1997 /* the transfer is finished */
1998 error = (td->error_stall ?
1999 USB_ERR_STALLED : USB_ERR_IOERROR);
2003 /* Check for short transfer */
2005 if (xfer->flags_int.short_frames_ok ||
2006 xfer->flags_int.isochronous_xfr) {
2007 /* follow alt next */
2014 /* the transfer is finished */
2022 /* this USB frame is complete */
2028 /* update transfer cache */
2030 xfer->td_transfer_cache = td;
2036 saf1761_otg_standard_done(struct usb_xfer *xfer)
2038 usb_error_t err = 0;
2040 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2041 xfer, xfer->endpoint);
2045 xfer->td_transfer_cache = xfer->td_transfer_first;
2047 if (xfer->flags_int.control_xfr) {
2049 if (xfer->flags_int.control_hdr) {
2051 err = saf1761_otg_standard_done_sub(xfer);
2055 if (xfer->td_transfer_cache == NULL) {
2059 while (xfer->aframes != xfer->nframes) {
2061 err = saf1761_otg_standard_done_sub(xfer);
2064 if (xfer->td_transfer_cache == NULL) {
2069 if (xfer->flags_int.control_xfr &&
2070 !xfer->flags_int.control_act) {
2072 err = saf1761_otg_standard_done_sub(xfer);
2075 saf1761_otg_device_done(xfer, err);
2078 /*------------------------------------------------------------------------*
2079 * saf1761_otg_device_done
2081 * NOTE: this function can be called more than one time on the
2082 * same USB transfer!
2083 *------------------------------------------------------------------------*/
2085 saf1761_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
2087 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
2089 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2091 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2092 xfer, xfer->endpoint, error);
2094 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2096 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
2097 saf1761_otg_intr_set(xfer, 0);
2099 struct saf1761_otg_td *td;
2101 td = xfer->td_transfer_cache;
2104 saf1761_host_channel_free(sc, td);
2107 /* dequeue transfer and start next transfer */
2108 usbd_transfer_done(xfer, error);
2110 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2114 saf1761_otg_xfer_stall(struct usb_xfer *xfer)
2116 saf1761_otg_device_done(xfer, USB_ERR_STALLED);
2120 saf1761_otg_set_stall(struct usb_device *udev,
2121 struct usb_endpoint *ep, uint8_t *did_stall)
2123 struct saf1761_otg_softc *sc;
2128 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2131 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2136 DPRINTFN(5, "endpoint=%p\n", ep);
2139 sc = SAF1761_OTG_BUS2SC(udev->bus);
2141 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
2142 ep_dir = (ep->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT));
2143 ep_type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2145 if (ep_type == UE_CONTROL) {
2146 /* should not happen */
2149 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2151 /* select the correct endpoint */
2152 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2153 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2154 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2155 SOTG_EP_INDEX_DIR_OUT));
2158 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
2160 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2164 saf1761_otg_clear_stall_sub_locked(struct saf1761_otg_softc *sc,
2165 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2167 if (ep_type == UE_CONTROL) {
2168 /* clearing stall is not needed */
2171 /* select the correct endpoint */
2172 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2173 (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2174 ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
2175 SOTG_EP_INDEX_DIR_OUT));
2177 /* disable endpoint */
2178 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, 0);
2179 /* enable endpoint again - will clear data toggle */
2180 SAF1761_WRITE_LE_4(sc, SOTG_EP_TYPE, ep_type | SOTG_EP_TYPE_ENABLE);
2183 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_CLBUF);
2185 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_FUNC, 0);
2189 saf1761_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
2191 struct saf1761_otg_softc *sc;
2192 struct usb_endpoint_descriptor *ed;
2194 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2196 DPRINTFN(5, "endpoint=%p\n", ep);
2199 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2204 sc = SAF1761_OTG_BUS2SC(udev->bus);
2206 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2208 /* get endpoint descriptor */
2211 /* reset endpoint */
2212 saf1761_otg_clear_stall_sub_locked(sc,
2213 (ed->bEndpointAddress & UE_ADDR),
2214 (ed->bmAttributes & UE_XFERTYPE),
2215 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
2217 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2221 saf1761_otg_init(struct saf1761_otg_softc *sc)
2223 const struct usb_hw_ep_profile *pf;
2228 /* set up the bus structure */
2229 sc->sc_bus.usbrev = USB_REV_2_0;
2230 sc->sc_bus.methods = &saf1761_otg_bus_methods;
2232 USB_BUS_LOCK(&sc->sc_bus);
2234 /* Reset Host controller, including HW mode */
2235 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_ALL);
2239 /* Reset Host controller, including HW mode */
2240 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_HC);
2245 SAF1761_WRITE_LE_4(sc, SOTG_SW_RESET, 0);
2250 /* Enable interrupts */
2251 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_GLOBAL_INTR_EN |
2252 SOTG_HW_MODE_CTRL_COMN_INT;
2255 SAF1761_WRITE_LE_4(sc, SOTG_UNLOCK_DEVICE, SOTG_UNLOCK_DEVICE_CODE);
2258 * Set correct hardware mode, must be written twice if bus
2261 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2262 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2264 SAF1761_WRITE_LE_4(sc, SOTG_DCSCRATCH, 0xdeadbeef);
2265 SAF1761_WRITE_LE_4(sc, SOTG_HCSCRATCH, 0xdeadbeef);
2267 DPRINTF("DCID=0x%08x VEND_PROD=0x%08x HWMODE=0x%08x SCRATCH=0x%08x,0x%08x\n",
2268 SAF1761_READ_LE_4(sc, SOTG_DCCHIP_ID),
2269 SAF1761_READ_LE_4(sc, SOTG_VEND_PROD_ID),
2270 SAF1761_READ_LE_4(sc, SOTG_HW_MODE_CTRL),
2271 SAF1761_READ_LE_4(sc, SOTG_DCSCRATCH),
2272 SAF1761_READ_LE_4(sc, SOTG_HCSCRATCH));
2274 /* reset device controller */
2275 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_SFRESET);
2276 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2281 /* reset host controller */
2282 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_HCRESET);
2284 /* wait for reset to clear */
2285 for (x = 0; x != 10; x++) {
2286 if ((SAF1761_READ_LE_4(sc, SOTG_USBCMD) & SOTG_USBCMD_HCRESET) == 0)
2288 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
2291 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode |
2292 SOTG_HW_MODE_CTRL_ALL_ATX_RESET);
2297 SAF1761_WRITE_LE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
2303 saf1761_otg_pull_down(sc);
2305 /* wait 10ms for pulldown to stabilise */
2306 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2310 saf1761_otg_get_hw_ep_profile(NULL, &pf, x);
2314 /* select the correct endpoint */
2315 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2316 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2317 SOTG_EP_INDEX_DIR_IN);
2319 /* select the maximum packet size */
2320 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_in_frame_size);
2322 /* select the correct endpoint */
2323 SAF1761_WRITE_LE_4(sc, SOTG_EP_INDEX,
2324 (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
2325 SOTG_EP_INDEX_DIR_OUT);
2327 /* select the maximum packet size */
2328 SAF1761_WRITE_LE_4(sc, SOTG_EP_MAXPACKET, pf->max_out_frame_size);
2331 /* enable interrupts */
2332 SAF1761_WRITE_LE_4(sc, SOTG_MODE, SOTG_MODE_GLINTENA |
2333 SOTG_MODE_CLKAON | SOTG_MODE_WKUPCS);
2335 sc->sc_interrupt_cfg |=
2336 SOTG_INTERRUPT_CFG_CDBGMOD |
2337 SOTG_INTERRUPT_CFG_DDBGMODIN |
2338 SOTG_INTERRUPT_CFG_DDBGMODOUT;
2340 /* set default values */
2341 SAF1761_WRITE_LE_4(sc, SOTG_INTERRUPT_CFG, sc->sc_interrupt_cfg);
2343 /* enable VBUS and ID interrupt */
2344 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2345 SOTG_IRQ_ENABLE_CLR(0xFFFF));
2346 SAF1761_WRITE_LE_4(sc, SOTG_IRQ_ENABLE_SET_CLR,
2347 SOTG_IRQ_ENABLE_SET(SOTG_IRQ_ID | SOTG_IRQ_VBUS_VLD));
2349 /* enable interrupts */
2350 sc->sc_intr_enable = SOTG_DCINTERRUPT_IEVBUS |
2351 SOTG_DCINTERRUPT_IEBRST | SOTG_DCINTERRUPT_IESUSP;
2352 SAF1761_WRITE_LE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
2355 * Connect ATX port 1 to device controller, select external
2356 * charge pump and driver VBUS to +5V:
2358 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2359 SOTG_CTRL_CLR(0xFFFF));
2360 SAF1761_WRITE_LE_4(sc, SOTG_CTRL_SET_CLR,
2361 SOTG_CTRL_SET(SOTG_CTRL_SW_SEL_HC_DC |
2362 SOTG_CTRL_BDIS_ACON_EN | SOTG_CTRL_SEL_CP_EXT |
2363 SOTG_CTRL_VBUS_DRV));
2365 /* disable device address */
2366 SAF1761_WRITE_LE_4(sc, SOTG_ADDRESS, 0);
2368 /* enable host controller clock and preserve reserved bits */
2369 x = SAF1761_READ_LE_4(sc, SOTG_POWER_DOWN);
2370 SAF1761_WRITE_LE_4(sc, SOTG_POWER_DOWN, x | SOTG_POWER_DOWN_HC_CLK_EN);
2372 /* wait 10ms for clock */
2373 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2375 /* enable configuration flag */
2376 SAF1761_WRITE_LE_4(sc, SOTG_CONFIGFLAG, SOTG_CONFIGFLAG_ENABLE);
2378 /* clear RAM block */
2379 for (x = 0x400; x != 0x10000; x += 4)
2380 SAF1761_WRITE_LE_4(sc, x, 0);
2383 SAF1761_WRITE_LE_4(sc, SOTG_USBCMD, SOTG_USBCMD_RS);
2385 DPRINTF("USBCMD=0x%08x\n", SAF1761_READ_LE_4(sc, SOTG_USBCMD));
2387 /* make HC scan all PTDs */
2388 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_LAST_PTD, (1 << 31));
2389 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_LAST_PTD, (1 << 31));
2390 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_LAST_PTD, (1 << 31));
2392 /* skip all PTDs by default */
2393 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD, -1U);
2394 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD, -1U);
2395 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD, -1U);
2397 /* activate all PTD types */
2398 SAF1761_WRITE_LE_4(sc, SOTG_HCBUFFERSTATUS,
2399 SOTG_HCBUFFERSTATUS_ISO_BUF_FILL |
2400 SOTG_HCBUFFERSTATUS_INT_BUF_FILL |
2401 SOTG_HCBUFFERSTATUS_ATL_BUF_FILL);
2403 /* we don't use the AND mask */
2404 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_AND, 0);
2405 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_AND, 0);
2406 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_AND, 0);
2408 /* enable all PTD OR interrupts by default */
2409 SAF1761_WRITE_LE_4(sc, SOTG_ISO_IRQ_MASK_OR, -1U);
2410 SAF1761_WRITE_LE_4(sc, SOTG_INT_IRQ_MASK_OR, -1U);
2411 SAF1761_WRITE_LE_4(sc, SOTG_ATL_IRQ_MASK_OR, -1U);
2413 /* enable HC interrupts */
2414 SAF1761_WRITE_LE_4(sc, SOTG_HCINTERRUPT_ENABLE,
2415 SOTG_HCINTERRUPT_OTG_IRQ |
2416 SOTG_HCINTERRUPT_ISO_IRQ |
2417 SOTG_HCINTERRUPT_ALT_IRQ |
2418 SOTG_HCINTERRUPT_INT_IRQ);
2420 /* poll initial VBUS status */
2421 saf1761_otg_update_vbus(sc);
2423 USB_BUS_UNLOCK(&sc->sc_bus);
2425 /* catch any lost interrupts */
2427 saf1761_otg_do_poll(&sc->sc_bus);
2429 return (0); /* success */
2433 saf1761_otg_uninit(struct saf1761_otg_softc *sc)
2435 USB_BUS_LOCK(&sc->sc_bus);
2437 /* disable all interrupts */
2438 SAF1761_WRITE_LE_4(sc, SOTG_MODE, 0);
2440 sc->sc_flags.port_powered = 0;
2441 sc->sc_flags.status_vbus = 0;
2442 sc->sc_flags.status_bus_reset = 0;
2443 sc->sc_flags.status_suspend = 0;
2444 sc->sc_flags.change_suspend = 0;
2445 sc->sc_flags.change_connect = 1;
2447 saf1761_otg_pull_down(sc);
2448 USB_BUS_UNLOCK(&sc->sc_bus);
2452 saf1761_otg_suspend(struct saf1761_otg_softc *sc)
2458 saf1761_otg_resume(struct saf1761_otg_softc *sc)
2464 saf1761_otg_do_poll(struct usb_bus *bus)
2466 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
2468 USB_BUS_LOCK(&sc->sc_bus);
2469 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2470 saf1761_otg_interrupt_poll_locked(sc);
2471 saf1761_otg_interrupt_complete_locked(sc);
2472 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2473 USB_BUS_UNLOCK(&sc->sc_bus);
2476 /*------------------------------------------------------------------------*
2477 * saf1761_otg control support
2478 * saf1761_otg interrupt support
2479 * saf1761_otg bulk support
2480 *------------------------------------------------------------------------*/
2482 saf1761_otg_device_non_isoc_open(struct usb_xfer *xfer)
2488 saf1761_otg_device_non_isoc_close(struct usb_xfer *xfer)
2490 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2494 saf1761_otg_device_non_isoc_enter(struct usb_xfer *xfer)
2500 saf1761_otg_device_non_isoc_start(struct usb_xfer *xfer)
2503 saf1761_otg_setup_standard_chain(xfer);
2504 saf1761_otg_start_standard_chain(xfer);
2507 static const struct usb_pipe_methods saf1761_otg_non_isoc_methods =
2509 .open = saf1761_otg_device_non_isoc_open,
2510 .close = saf1761_otg_device_non_isoc_close,
2511 .enter = saf1761_otg_device_non_isoc_enter,
2512 .start = saf1761_otg_device_non_isoc_start,
2515 /*------------------------------------------------------------------------*
2516 * saf1761_otg device side isochronous support
2517 *------------------------------------------------------------------------*/
2519 saf1761_otg_device_isoc_open(struct usb_xfer *xfer)
2525 saf1761_otg_device_isoc_close(struct usb_xfer *xfer)
2527 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2531 saf1761_otg_device_isoc_enter(struct usb_xfer *xfer)
2533 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
2537 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2538 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2540 /* get the current frame index - we don't need the high bits */
2542 nframes = SAF1761_READ_LE_4(sc, SOTG_FRAME_NUM);
2545 * check if the frame index is within the window where the
2546 * frames will be inserted
2548 temp = (nframes - xfer->endpoint->isoc_next) & SOTG_FRAME_NUM_SOFR_MASK;
2550 if ((xfer->endpoint->is_synced == 0) ||
2551 (temp < xfer->nframes)) {
2553 * If there is data underflow or the pipe queue is
2554 * empty we schedule the transfer a few frames ahead
2555 * of the current frame position. Else two isochronous
2556 * transfers might overlap.
2558 xfer->endpoint->isoc_next = (nframes + 3) & SOTG_FRAME_NUM_SOFR_MASK;
2559 xfer->endpoint->is_synced = 1;
2560 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2563 * compute how many milliseconds the insertion is ahead of the
2564 * current frame position:
2566 temp = (xfer->endpoint->isoc_next - nframes) & SOTG_FRAME_NUM_SOFR_MASK;
2569 * pre-compute when the isochronous transfer will be finished:
2571 xfer->isoc_time_complete =
2572 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2575 /* compute frame number for next insertion */
2576 xfer->endpoint->isoc_next += xfer->nframes;
2579 saf1761_otg_setup_standard_chain(xfer);
2583 saf1761_otg_device_isoc_start(struct usb_xfer *xfer)
2585 /* start TD chain */
2586 saf1761_otg_start_standard_chain(xfer);
2589 static const struct usb_pipe_methods saf1761_otg_device_isoc_methods =
2591 .open = saf1761_otg_device_isoc_open,
2592 .close = saf1761_otg_device_isoc_close,
2593 .enter = saf1761_otg_device_isoc_enter,
2594 .start = saf1761_otg_device_isoc_start,
2597 /*------------------------------------------------------------------------*
2598 * saf1761_otg host side isochronous support
2599 *------------------------------------------------------------------------*/
2601 saf1761_otg_host_isoc_open(struct usb_xfer *xfer)
2607 saf1761_otg_host_isoc_close(struct usb_xfer *xfer)
2609 saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
2613 saf1761_otg_host_isoc_enter(struct usb_xfer *xfer)
2615 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
2619 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2620 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2622 /* get the current frame index - we don't need the high bits */
2624 nframes = (SAF1761_READ_LE_4(sc, SOTG_FRINDEX) & SOTG_FRINDEX_MASK) >> 3;
2627 * check if the frame index is within the window where the
2628 * frames will be inserted
2630 temp = (nframes - xfer->endpoint->isoc_next) & (SOTG_FRINDEX_MASK >> 3);
2632 if ((xfer->endpoint->is_synced == 0) ||
2633 (temp < xfer->nframes)) {
2635 * If there is data underflow or the pipe queue is
2636 * empty we schedule the transfer a few frames ahead
2637 * of the current frame position. Else two isochronous
2638 * transfers might overlap.
2640 xfer->endpoint->isoc_next = (nframes + 3) & (SOTG_FRINDEX_MASK >> 3);
2641 xfer->endpoint->is_synced = 1;
2642 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2645 * compute how many milliseconds the insertion is ahead of the
2646 * current frame position:
2648 temp = (xfer->endpoint->isoc_next - nframes) & (SOTG_FRINDEX_MASK >> 3);
2651 * pre-compute when the isochronous transfer will be finished:
2653 xfer->isoc_time_complete =
2654 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2657 /* compute frame number for next insertion */
2658 xfer->endpoint->isoc_next += xfer->nframes;
2661 saf1761_otg_setup_standard_chain(xfer);
2665 saf1761_otg_host_isoc_start(struct usb_xfer *xfer)
2667 /* start TD chain */
2668 saf1761_otg_start_standard_chain(xfer);
2671 static const struct usb_pipe_methods saf1761_otg_host_isoc_methods =
2673 .open = saf1761_otg_host_isoc_open,
2674 .close = saf1761_otg_host_isoc_close,
2675 .enter = saf1761_otg_host_isoc_enter,
2676 .start = saf1761_otg_host_isoc_start,
2679 /*------------------------------------------------------------------------*
2680 * saf1761_otg root control support
2681 *------------------------------------------------------------------------*
2682 * Simulate a hardware HUB by handling all the necessary requests.
2683 *------------------------------------------------------------------------*/
2685 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2687 static const struct usb_device_descriptor saf1761_otg_devd = {
2688 .bLength = sizeof(struct usb_device_descriptor),
2689 .bDescriptorType = UDESC_DEVICE,
2690 HSETW(.idVendor, 0x04cc),
2691 HSETW(.idProduct, 0x1761),
2692 .bcdUSB = {0x00, 0x02},
2693 .bDeviceClass = UDCLASS_HUB,
2694 .bDeviceSubClass = UDSUBCLASS_HUB,
2695 .bDeviceProtocol = UDPROTO_FSHUB,
2696 .bMaxPacketSize = 64,
2697 .bcdDevice = {0x00, 0x01},
2700 .bNumConfigurations = 1,
2703 static const struct usb_device_qualifier saf1761_otg_odevd = {
2704 .bLength = sizeof(struct usb_device_qualifier),
2705 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
2706 .bcdUSB = {0x00, 0x02},
2707 .bDeviceClass = UDCLASS_HUB,
2708 .bDeviceSubClass = UDSUBCLASS_HUB,
2709 .bDeviceProtocol = UDPROTO_FSHUB,
2710 .bMaxPacketSize0 = 0,
2711 .bNumConfigurations = 0,
2714 static const struct saf1761_otg_config_desc saf1761_otg_confd = {
2716 .bLength = sizeof(struct usb_config_descriptor),
2717 .bDescriptorType = UDESC_CONFIG,
2718 .wTotalLength[0] = sizeof(saf1761_otg_confd),
2720 .bConfigurationValue = 1,
2721 .iConfiguration = 0,
2722 .bmAttributes = UC_SELF_POWERED,
2726 .bLength = sizeof(struct usb_interface_descriptor),
2727 .bDescriptorType = UDESC_INTERFACE,
2729 .bInterfaceClass = UICLASS_HUB,
2730 .bInterfaceSubClass = UISUBCLASS_HUB,
2731 .bInterfaceProtocol = 0,
2735 .bLength = sizeof(struct usb_endpoint_descriptor),
2736 .bDescriptorType = UDESC_ENDPOINT,
2737 .bEndpointAddress = (UE_DIR_IN | SAF1761_OTG_INTR_ENDPT),
2738 .bmAttributes = UE_INTERRUPT,
2739 .wMaxPacketSize[0] = 8,
2744 static const struct usb_hub_descriptor_min saf1761_otg_hubd = {
2745 .bDescLength = sizeof(saf1761_otg_hubd),
2746 .bDescriptorType = UDESC_HUB,
2747 .bNbrPorts = SOTG_NUM_PORTS,
2748 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
2749 .bPwrOn2PwrGood = 50,
2750 .bHubContrCurrent = 0,
2751 .DeviceRemovable = {0}, /* port is removable */
2754 #define STRING_VENDOR \
2757 #define STRING_PRODUCT \
2758 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
2760 USB_MAKE_STRING_DESC(STRING_VENDOR, saf1761_otg_vendor);
2761 USB_MAKE_STRING_DESC(STRING_PRODUCT, saf1761_otg_product);
2764 saf1761_otg_roothub_exec(struct usb_device *udev,
2765 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2767 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(udev->bus);
2776 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2779 ptr = (const void *)&sc->sc_hub_temp;
2783 value = UGETW(req->wValue);
2784 index = UGETW(req->wIndex);
2786 /* demultiplex the control request */
2788 switch (req->bmRequestType) {
2789 case UT_READ_DEVICE:
2790 switch (req->bRequest) {
2791 case UR_GET_DESCRIPTOR:
2792 goto tr_handle_get_descriptor;
2794 goto tr_handle_get_config;
2796 goto tr_handle_get_status;
2802 case UT_WRITE_DEVICE:
2803 switch (req->bRequest) {
2804 case UR_SET_ADDRESS:
2805 goto tr_handle_set_address;
2807 goto tr_handle_set_config;
2808 case UR_CLEAR_FEATURE:
2809 goto tr_valid; /* nop */
2810 case UR_SET_DESCRIPTOR:
2811 goto tr_valid; /* nop */
2812 case UR_SET_FEATURE:
2818 case UT_WRITE_ENDPOINT:
2819 switch (req->bRequest) {
2820 case UR_CLEAR_FEATURE:
2821 switch (UGETW(req->wValue)) {
2822 case UF_ENDPOINT_HALT:
2823 goto tr_handle_clear_halt;
2824 case UF_DEVICE_REMOTE_WAKEUP:
2825 goto tr_handle_clear_wakeup;
2830 case UR_SET_FEATURE:
2831 switch (UGETW(req->wValue)) {
2832 case UF_ENDPOINT_HALT:
2833 goto tr_handle_set_halt;
2834 case UF_DEVICE_REMOTE_WAKEUP:
2835 goto tr_handle_set_wakeup;
2840 case UR_SYNCH_FRAME:
2841 goto tr_valid; /* nop */
2847 case UT_READ_ENDPOINT:
2848 switch (req->bRequest) {
2850 goto tr_handle_get_ep_status;
2856 case UT_WRITE_INTERFACE:
2857 switch (req->bRequest) {
2858 case UR_SET_INTERFACE:
2859 goto tr_handle_set_interface;
2860 case UR_CLEAR_FEATURE:
2861 goto tr_valid; /* nop */
2862 case UR_SET_FEATURE:
2868 case UT_READ_INTERFACE:
2869 switch (req->bRequest) {
2870 case UR_GET_INTERFACE:
2871 goto tr_handle_get_interface;
2873 goto tr_handle_get_iface_status;
2879 case UT_WRITE_CLASS_INTERFACE:
2880 case UT_WRITE_VENDOR_INTERFACE:
2884 case UT_READ_CLASS_INTERFACE:
2885 case UT_READ_VENDOR_INTERFACE:
2889 case UT_WRITE_CLASS_DEVICE:
2890 switch (req->bRequest) {
2891 case UR_CLEAR_FEATURE:
2893 case UR_SET_DESCRIPTOR:
2894 case UR_SET_FEATURE:
2901 case UT_WRITE_CLASS_OTHER:
2902 switch (req->bRequest) {
2903 case UR_CLEAR_FEATURE:
2904 if (index == SOTG_HOST_PORT_NUM)
2905 goto tr_handle_clear_port_feature_host;
2906 else if (index == SOTG_DEVICE_PORT_NUM)
2907 goto tr_handle_clear_port_feature_device;
2910 case UR_SET_FEATURE:
2911 if (index == SOTG_HOST_PORT_NUM)
2912 goto tr_handle_set_port_feature_host;
2913 else if (index == SOTG_DEVICE_PORT_NUM)
2914 goto tr_handle_set_port_feature_device;
2917 case UR_CLEAR_TT_BUFFER:
2927 case UT_READ_CLASS_OTHER:
2928 switch (req->bRequest) {
2929 case UR_GET_TT_STATE:
2930 goto tr_handle_get_tt_state;
2932 if (index == SOTG_HOST_PORT_NUM)
2933 goto tr_handle_get_port_status_host;
2934 else if (index == SOTG_DEVICE_PORT_NUM)
2935 goto tr_handle_get_port_status_device;
2943 case UT_READ_CLASS_DEVICE:
2944 switch (req->bRequest) {
2945 case UR_GET_DESCRIPTOR:
2946 goto tr_handle_get_class_descriptor;
2948 goto tr_handle_get_class_status;
2959 tr_handle_get_descriptor:
2960 switch (value >> 8) {
2964 len = sizeof(saf1761_otg_devd);
2965 ptr = (const void *)&saf1761_otg_devd;
2967 case UDESC_DEVICE_QUALIFIER:
2970 len = sizeof(saf1761_otg_odevd);
2971 ptr = (const void *)&saf1761_otg_odevd;
2976 len = sizeof(saf1761_otg_confd);
2977 ptr = (const void *)&saf1761_otg_confd;
2980 switch (value & 0xff) {
2981 case 0: /* Language table */
2982 len = sizeof(usb_string_lang_en);
2983 ptr = (const void *)&usb_string_lang_en;
2986 case 1: /* Vendor */
2987 len = sizeof(saf1761_otg_vendor);
2988 ptr = (const void *)&saf1761_otg_vendor;
2991 case 2: /* Product */
2992 len = sizeof(saf1761_otg_product);
2993 ptr = (const void *)&saf1761_otg_product;
3004 tr_handle_get_config:
3006 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
3009 tr_handle_get_status:
3011 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
3014 tr_handle_set_address:
3018 sc->sc_rt_addr = value;
3021 tr_handle_set_config:
3024 sc->sc_conf = value;
3027 tr_handle_get_interface:
3029 sc->sc_hub_temp.wValue[0] = 0;
3032 tr_handle_get_tt_state:
3033 tr_handle_get_class_status:
3034 tr_handle_get_iface_status:
3035 tr_handle_get_ep_status:
3037 USETW(sc->sc_hub_temp.wValue, 0);
3041 tr_handle_set_interface:
3042 tr_handle_set_wakeup:
3043 tr_handle_clear_wakeup:
3044 tr_handle_clear_halt:
3047 tr_handle_clear_port_feature_device:
3048 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
3051 case UHF_PORT_SUSPEND:
3052 saf1761_otg_wakeup_peer(sc);
3055 case UHF_PORT_ENABLE:
3056 sc->sc_flags.port_enabled = 0;
3060 case UHF_PORT_INDICATOR:
3061 case UHF_C_PORT_ENABLE:
3062 case UHF_C_PORT_OVER_CURRENT:
3063 case UHF_C_PORT_RESET:
3066 case UHF_PORT_POWER:
3067 sc->sc_flags.port_powered = 0;
3068 saf1761_otg_pull_down(sc);
3070 case UHF_C_PORT_CONNECTION:
3071 sc->sc_flags.change_connect = 0;
3073 case UHF_C_PORT_SUSPEND:
3074 sc->sc_flags.change_suspend = 0;
3077 err = USB_ERR_IOERROR;
3082 tr_handle_clear_port_feature_host:
3083 DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
3085 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3088 case UHF_PORT_ENABLE:
3089 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PED);
3091 case UHF_PORT_SUSPEND:
3092 if ((temp & SOTG_PORTSC1_SUSP) && (!(temp & SOTG_PORTSC1_FPR)))
3093 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_FPR);
3095 /* wait 20ms for resume sequence to complete */
3096 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3098 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~(SOTG_PORTSC1_SUSP |
3099 SOTG_PORTSC1_FPR | SOTG_PORTSC1_LS /* High Speed */ ));
3101 /* 4ms settle time */
3102 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3104 case UHF_PORT_INDICATOR:
3105 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PIC);
3108 case UHF_C_PORT_ENABLE:
3109 case UHF_C_PORT_OVER_CURRENT:
3110 case UHF_C_PORT_RESET:
3111 case UHF_C_PORT_SUSPEND:
3114 case UHF_PORT_POWER:
3115 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PP);
3117 case UHF_C_PORT_CONNECTION:
3118 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_ECSC);
3121 err = USB_ERR_IOERROR;
3126 tr_handle_set_port_feature_device:
3127 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
3130 case UHF_PORT_ENABLE:
3131 sc->sc_flags.port_enabled = 1;
3133 case UHF_PORT_SUSPEND:
3134 case UHF_PORT_RESET:
3136 case UHF_PORT_INDICATOR:
3139 case UHF_PORT_POWER:
3140 sc->sc_flags.port_powered = 1;
3143 err = USB_ERR_IOERROR;
3148 tr_handle_set_port_feature_host:
3149 DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
3151 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3154 case UHF_PORT_ENABLE:
3155 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PED);
3157 case UHF_PORT_SUSPEND:
3158 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_SUSP);
3160 case UHF_PORT_RESET:
3161 DPRINTFN(6, "reset port %d\n", index);
3163 /* Start reset sequence. */
3164 temp &= ~(SOTG_PORTSC1_PED | SOTG_PORTSC1_PR);
3166 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PR);
3168 /* Wait for reset to complete. */
3169 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3170 USB_MS_TO_TICKS(usb_port_root_reset_delay));
3172 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp);
3174 /* Wait for HC to complete reset. */
3175 usb_pause_mtx(&sc->sc_bus.bus_mtx, USB_MS_TO_TICKS(2));
3177 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3179 DPRINTF("After reset, status=0x%08x\n", temp);
3180 if (temp & SOTG_PORTSC1_PR) {
3181 device_printf(sc->sc_bus.bdev, "port reset timeout\n");
3182 err = USB_ERR_TIMEOUT;
3185 if (!(temp & SOTG_PORTSC1_PED)) {
3186 /* Not a high speed device, give up ownership.*/
3187 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PO);
3191 DPRINTF("port %d reset, status = 0x%08x\n", index, temp);
3193 case UHF_PORT_POWER:
3194 DPRINTFN(3, "set port power %d\n", index);
3195 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PP);
3199 DPRINTFN(3, "set port test %d\n", index);
3202 case UHF_PORT_INDICATOR:
3203 DPRINTFN(3, "set port ind %d\n", index);
3204 SAF1761_WRITE_LE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PIC);
3207 err = USB_ERR_IOERROR;
3212 tr_handle_get_port_status_device:
3214 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d\n", index);
3216 if (sc->sc_flags.status_vbus) {
3217 saf1761_otg_pull_up(sc);
3219 saf1761_otg_pull_down(sc);
3222 /* Select FULL-speed and Device Side Mode */
3224 value = UPS_PORT_MODE_DEVICE;
3226 if (sc->sc_flags.port_powered)
3227 value |= UPS_PORT_POWER;
3229 if (sc->sc_flags.port_enabled)
3230 value |= UPS_PORT_ENABLED;
3232 if (sc->sc_flags.status_vbus &&
3233 sc->sc_flags.status_bus_reset)
3234 value |= UPS_CURRENT_CONNECT_STATUS;
3236 if (sc->sc_flags.status_suspend)
3237 value |= UPS_SUSPEND;
3239 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
3243 if (sc->sc_flags.change_connect)
3244 value |= UPS_C_CONNECT_STATUS;
3246 if (sc->sc_flags.change_suspend)
3247 value |= UPS_C_SUSPEND;
3249 USETW(sc->sc_hub_temp.ps.wPortChange, value);
3250 len = sizeof(sc->sc_hub_temp.ps);
3253 tr_handle_get_port_status_host:
3255 temp = SAF1761_READ_LE_4(sc, SOTG_PORTSC1);
3257 DPRINTFN(9, "UR_GET_PORT_STATUS on port %d = 0x%08x\n", index, temp);
3261 if (temp & SOTG_PORTSC1_ECCS)
3262 i |= UPS_CURRENT_CONNECT_STATUS;
3263 if (temp & SOTG_PORTSC1_PED)
3264 i |= UPS_PORT_ENABLED;
3265 if ((temp & SOTG_PORTSC1_SUSP) && !(temp & SOTG_PORTSC1_FPR))
3267 if (temp & SOTG_PORTSC1_PR)
3269 if (temp & SOTG_PORTSC1_PP)
3270 i |= UPS_PORT_POWER;
3272 USETW(sc->sc_hub_temp.ps.wPortStatus, i);
3275 if (temp & SOTG_PORTSC1_ECSC)
3276 i |= UPS_C_CONNECT_STATUS;
3277 if (temp & SOTG_PORTSC1_FPR)
3280 i |= UPS_C_PORT_RESET;
3281 USETW(sc->sc_hub_temp.ps.wPortChange, i);
3282 len = sizeof(sc->sc_hub_temp.ps);
3285 tr_handle_get_class_descriptor:
3288 ptr = (const void *)&saf1761_otg_hubd;
3289 len = sizeof(saf1761_otg_hubd);
3293 err = USB_ERR_STALLED;
3301 saf1761_otg_xfer_setup(struct usb_setup_params *parm)
3303 struct saf1761_otg_softc *sc;
3304 struct usb_xfer *xfer;
3312 sc = SAF1761_OTG_BUS2SC(parm->udev->bus);
3313 xfer = parm->curr_xfer;
3316 * NOTE: This driver does not use any of the parameters that
3317 * are computed from the following values. Just set some
3318 * reasonable dummies:
3320 parm->hc_max_packet_size = 0x500;
3321 parm->hc_max_packet_count = 1;
3322 parm->hc_max_frame_size = 0x500;
3324 usbd_transfer_setup_sub(parm);
3327 * Compute maximum number of TDs:
3329 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
3331 if (ep_type == UE_CONTROL) {
3333 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
3336 ntd = xfer->nframes + 1 /* SYNC */ ;
3340 * check if "usbd_transfer_setup_sub" set an error
3346 * allocate transfer descriptors
3350 ep_no = xfer->endpointno & UE_ADDR;
3353 * Check profile stuff
3355 if (parm->udev->flags.usb_mode == USB_MODE_DEVICE) {
3356 const struct usb_hw_ep_profile *pf;
3358 saf1761_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
3361 /* should not happen */
3362 parm->err = USB_ERR_INVAL;
3367 dw1 = (xfer->address << 3) | (ep_type << 12);
3369 switch (parm->udev->speed) {
3370 case USB_SPEED_FULL:
3372 /* check if root HUB port is running High Speed */
3373 if (parm->udev->parent_hs_hub != NULL) {
3374 dw1 |= SOTG_PTD_DW1_ENABLE_SPLIT;
3375 dw1 |= (parm->udev->hs_port_no << 18);
3376 dw1 |= (parm->udev->hs_hub_addr << 25);
3377 if (parm->udev->speed == USB_SPEED_LOW)
3386 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
3388 for (n = 0; n != ntd; n++) {
3390 struct saf1761_otg_td *td;
3394 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
3397 td->max_packet_size = xfer->max_packet_size;
3398 td->ep_index = ep_no;
3399 td->ep_type = ep_type;
3400 td->dw1_value = dw1;
3401 if (ep_type == UE_ISOCHRONOUS) {
3402 if (parm->udev->speed == USB_SPEED_HIGH) {
3403 uint8_t uframe_index = (ntd - 1 - n);
3404 uframe_index <<= usbd_xfer_get_fps_shift(xfer);
3405 td->uframe = (uframe_index & 7);
3412 if (ep_type == UE_INTERRUPT) {
3413 if (xfer->interval > 32)
3414 td->interval = (32 / 2) << 3;
3416 td->interval = (xfer->interval / 2) << 3;
3420 td->obj_next = last_obj;
3424 parm->size[0] += sizeof(*td);
3427 xfer->td_start[0] = last_obj;
3431 saf1761_otg_xfer_unsetup(struct usb_xfer *xfer)
3436 saf1761_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3437 struct usb_endpoint *ep)
3441 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3443 edesc->bEndpointAddress, udev->flags.usb_mode);
3445 if (udev->parent_hub == NULL) {
3446 /* root HUB has special endpoint handling */
3450 /* Verify wMaxPacketSize */
3451 mps = UGETW(edesc->wMaxPacketSize);
3452 if (udev->speed == USB_SPEED_HIGH) {
3453 if ((mps >> 11) & 3) {
3454 DPRINTF("A packet multiplier different from "
3455 "1 is not supported\n");
3459 if (mps > SOTG_HS_MAX_PACKET_SIZE) {
3460 DPRINTF("Packet size %d bigger than %d\n",
3461 (int)mps, SOTG_HS_MAX_PACKET_SIZE);
3464 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3465 if (udev->speed != USB_SPEED_FULL &&
3466 udev->speed != USB_SPEED_HIGH) {
3470 switch (edesc->bmAttributes & UE_XFERTYPE) {
3471 case UE_ISOCHRONOUS:
3472 ep->methods = &saf1761_otg_device_isoc_methods;
3475 ep->methods = &saf1761_otg_non_isoc_methods;
3479 switch (edesc->bmAttributes & UE_XFERTYPE) {
3480 case UE_ISOCHRONOUS:
3481 ep->methods = &saf1761_otg_host_isoc_methods;
3484 ep->methods = &saf1761_otg_non_isoc_methods;
3491 saf1761_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3493 struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
3496 case USB_HW_POWER_SUSPEND:
3497 saf1761_otg_suspend(sc);
3499 case USB_HW_POWER_SHUTDOWN:
3500 saf1761_otg_uninit(sc);
3502 case USB_HW_POWER_RESUME:
3503 saf1761_otg_resume(sc);
3511 saf1761_otg_device_resume(struct usb_device *udev)
3513 struct saf1761_otg_softc *sc;
3514 struct saf1761_otg_td *td;
3515 struct usb_xfer *xfer;
3520 if (udev->flags.usb_mode != USB_MODE_HOST)
3523 sc = SAF1761_OTG_BUS2SC(udev->bus);
3525 USB_BUS_LOCK(&sc->sc_bus);
3526 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3528 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3530 if (xfer->xroot->udev != udev)
3533 td = xfer->td_transfer_cache;
3534 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3537 switch (td->ep_type) {
3539 x = td->channel - 32;
3540 sc->sc_host_intr_suspend_map &= ~(1 << x);
3541 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3542 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3544 case UE_ISOCHRONOUS:
3546 sc->sc_host_isoc_suspend_map &= ~(1 << x);
3547 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3548 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3551 x = td->channel - 64;
3552 sc->sc_host_async_suspend_map &= ~(1 << x);
3553 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3554 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3559 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3560 USB_BUS_UNLOCK(&sc->sc_bus);
3562 /* poll all transfers again to restart resumed ones */
3563 saf1761_otg_do_poll(&sc->sc_bus);
3567 saf1761_otg_device_suspend(struct usb_device *udev)
3569 struct saf1761_otg_softc *sc;
3570 struct saf1761_otg_td *td;
3571 struct usb_xfer *xfer;
3576 if (udev->flags.usb_mode != USB_MODE_HOST)
3579 sc = SAF1761_OTG_BUS2SC(udev->bus);
3581 USB_BUS_LOCK(&sc->sc_bus);
3582 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3584 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3586 if (xfer->xroot->udev != udev)
3589 td = xfer->td_transfer_cache;
3590 if (td == NULL || td->channel >= SOTG_HOST_CHANNEL_MAX)
3593 switch (td->ep_type) {
3595 x = td->channel - 32;
3596 sc->sc_host_intr_suspend_map |= (1 << x);
3597 SAF1761_WRITE_LE_4(sc, SOTG_INT_PTD_SKIP_PTD,
3598 (~sc->sc_host_intr_map) | sc->sc_host_intr_suspend_map);
3600 case UE_ISOCHRONOUS:
3602 sc->sc_host_isoc_suspend_map |= (1 << x);
3603 SAF1761_WRITE_LE_4(sc, SOTG_ISO_PTD_SKIP_PTD,
3604 (~sc->sc_host_isoc_map) | sc->sc_host_isoc_suspend_map);
3607 x = td->channel - 64;
3608 sc->sc_host_async_suspend_map |= (1 << x);
3609 SAF1761_WRITE_LE_4(sc, SOTG_ATL_PTD_SKIP_PTD,
3610 (~sc->sc_host_async_map) | sc->sc_host_async_suspend_map);
3615 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3616 USB_BUS_UNLOCK(&sc->sc_bus);
3619 static const struct usb_bus_methods saf1761_otg_bus_methods =
3621 .endpoint_init = &saf1761_otg_ep_init,
3622 .xfer_setup = &saf1761_otg_xfer_setup,
3623 .xfer_unsetup = &saf1761_otg_xfer_unsetup,
3624 .get_hw_ep_profile = &saf1761_otg_get_hw_ep_profile,
3625 .xfer_stall = &saf1761_otg_xfer_stall,
3626 .set_stall = &saf1761_otg_set_stall,
3627 .clear_stall = &saf1761_otg_clear_stall,
3628 .roothub_exec = &saf1761_otg_roothub_exec,
3629 .xfer_poll = &saf1761_otg_do_poll,
3630 .set_hw_power_sleep = saf1761_otg_set_hw_power_sleep,
3631 .device_resume = &saf1761_otg_device_resume,
3632 .device_suspend = &saf1761_otg_device_suspend,