1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright © 2010 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include "clock-target.h"
36 #include "usb-drv-as3525v2.h"
39 static int __in_ep_list
[NUM_IN_EP
] = {IN_EP_LIST
};
40 static int __out_ep_list
[NUM_OUT_EP
] = {OUT_EP_LIST
};
41 static int __in_ep_list_ep0
[NUM_IN_EP
+ 1] = {0, IN_EP_LIST
};
42 static int __out_ep_list_ep0
[NUM_OUT_EP
+ 1] = {0, OUT_EP_LIST
};
44 /* iterate through each in/out ep except EP0
45 * 'counter' is the counter, 'ep' is the actual value */
46 #define FOR_EACH_EP(list, size, counter, ep) \
47 for(counter = 0, ep = (list)[0]; \
49 counter++, ep = (list)[counter])
51 #define FOR_EACH_IN_EP_EX(include_ep0, counter, ep) \
52 FOR_EACH_EP(include_ep0 ? __in_ep_list_ep0 : __in_ep_list, \
53 include_ep0 ? NUM_IN_EP + 1: NUM_IN_EP, counter, ep)
55 #define FOR_EACH_OUT_EP_EX(include_ep0, counter, ep) \
56 FOR_EACH_EP(include_ep0 ? __out_ep_list_ep0 : __out_ep_list, \
57 include_ep0 ? NUM_OUT_EP + 1: NUM_OUT_EP, counter, ep)
59 #define FOR_EACH_IN_EP(counter, ep) \
60 FOR_EACH_IN_EP_EX(false, counter, ep)
62 #define FOR_EACH_IN_EP_AND_EP0(counter, ep) \
63 FOR_EACH_IN_EP_EX(true, counter, ep)
65 #define FOR_EACH_OUT_EP(counter, ep) \
66 FOR_EACH_OUT_EP_EX(false, counter, ep)
68 #define FOR_EACH_OUT_EP_AND_EP0(counter, ep) \
69 FOR_EACH_OUT_EP_EX(true, counter, ep)
71 /* store per endpoint, per direction, information */
74 bool active
; /* true is endpoint has been requested (true for EP0) */
75 unsigned int len
; /* length of the data buffer */
76 bool wait
; /* true if usb thread is blocked on completion */
77 bool busy
; /* true is a transfer is pending */
78 int status
; /* completion status (0 for success) */
79 struct wakeup complete
; /* wait object */
82 /* state of EP0 (to correctly schedule setup packet enqueing) */
85 /* Setup packet is enqueud, waiting for actual data */
87 /* Waiting for ack (either IN or OUT) */
89 /* Ack complete, waiting for data (either IN or OUT)
90 * This state is necessary because if both ack and data complete in the
91 * same interrupt, we might process data completion before ack completion
92 * so we need this bizarre state */
94 /* Setup packet complete, waiting for ack and data */
95 EP0_WAIT_DATA_ACK
= 3,
98 /* endpoints[ep_num][DIR_IN/DIR_OUT] */
99 static struct usb_endpoint endpoints
[USB_NUM_ENDPOINTS
][2];
100 /* setup packet for EP0 */
101 static struct usb_ctrlrequest ep0_setup_pkt USB_DEVBSS_ATTR
;
103 enum ep0state ep0_state
;
105 void usb_attach(void)
107 logf("usb-drv: attach");
111 static void usb_delay(void)
121 static void as3525v2_connect(void)
123 logf("usb-drv: init as3525v2");
124 /* 1) enable usb core clock */
125 CGU_PERI
|= CGU_USB_CLOCK_ENABLE
;
127 /* 2) enable usb phy clock */
129 CGU_USB
= 1<<5 /* enable */
130 | (CLK_DIV(AS3525_PLLA_FREQ
, 60000000)) << 2
131 | 1; /* source = PLLA */
133 /* 3) clear "stop pclk" */
136 /* 4) clear "power clamp" */
139 /* 5) clear "reset power down module" */
142 /* 6) set "power on program done" */
143 DCTL
|= DCTL_pwronprgdone
;
145 /* 7) core soft reset */
146 GRSTCTL
|= GRSTCTL_csftrst
;
148 /* 8) hclk soft reset */
149 GRSTCTL
|= GRSTCTL_hsftrst
;
151 /* 9) flush and reset everything */
154 /* 10) force device mode*/
155 GUSBCFG
&= ~GUSBCFG_force_host_mode
;
156 GUSBCFG
|= GUSBCFG_force_device_mode
;
158 /* 11) Do something that is probably CCU related but undocumented*/
159 CCU_USB_THINGY
&= ~0x1000;
161 CCU_USB_THINGY
&= ~0x300000;
163 /* 12) reset usb core parameters (dev addr, speed, ...) */
168 static void as3525v2_disconnect(void)
172 static void enable_device_interrupts(void)
174 /* Clear any pending interrupt */
175 GINTSTS
= 0xffffffff;
176 /* Clear any pending otg interrupt */
177 GOTGINT
= 0xffffffff;
178 /* Enable interrupts */
179 GINTMSK
= GINTMSK_usbreset
183 | GINTMSK_disconnect
;
186 static void flush_tx_fifos(int nums
)
190 GRSTCTL
= (nums
<< GRSTCTL_txfnum_bitp
)
191 | GRSTCTL_txfflsh_flush
;
192 while(GRSTCTL
& GRSTCTL_txfflsh_flush
&& i
< 0x300)
194 if(GRSTCTL
& GRSTCTL_txfflsh_flush
)
195 panicf("usb-drv: hang of flush tx fifos (%x)", nums
);
196 /* wait 3 phy clocks */
200 static void flush_rx_fifos(void)
204 GRSTCTL
= GRSTCTL_rxfflsh_flush
;
205 while(GRSTCTL
& GRSTCTL_rxfflsh_flush
&& i
< 0x300)
207 if(GRSTCTL
& GRSTCTL_rxfflsh_flush
)
208 panicf("usb-drv: hang of flush rx fifos");
209 /* wait 3 phy clocks */
213 static void prepare_setup_ep0(void)
215 logf("usb-drv: prepare EP0"),
217 clean_dcache_range((void*)&ep0_setup_pkt
, sizeof ep0_setup_pkt
); /* force write back */
218 DOEPDMA(0) = (unsigned long)&ep0_setup_pkt
; /* virtual address=physical address */
220 /* Setup EP0 OUT with the following parameters:
222 * setup packet count = 1
223 * transfer size = 8 (setup packet)
225 DOEPTSIZ(0) = (1 << DEPTSIZ0_supcnt_bitp
)
226 | (1 << DEPTSIZ0_pkcnt_bitp
)
229 /* Enable endpoint, clear nak */
230 DOEPCTL(0) |= DEPCTL_epena
| DEPCTL_cnak
;
232 if(!(DOEPCTL(0) & DEPCTL_epena
))
233 panicf("usb-drv: failed to enable EP0 !");
234 ep0_state
= EP0_WAIT_SETUP
;
237 static void handle_ep0_complete(bool is_ack
)
242 panicf("usb-drv: EP0 completion while waiting for SETUP");
245 /* everything is done, prepare next setup */
248 panicf("usb-drv: EP0 data completion while waiting for ACK");
252 panicf("usb-drv: EP0 ACK while waiting for data completion");
254 /* everything is done, prepare next setup */
257 case EP0_WAIT_DATA_ACK
:
260 ep0_state
= EP0_WAIT_DATA
;
262 ep0_state
= EP0_WAIT_ACK
;
265 panicf("usb-drv: invalid EP0 state");
267 logf("usb-drv: EP0 state updated to %d", ep0_state
);
270 static void handle_ep0_setup(void)
272 if(ep0_state
!= EP0_WAIT_SETUP
)
274 logf("usb-drv: EP0 SETUP while in state %d", ep0_state
);
275 DCTL
|= DCTL_sftdiscon
;
278 /* determine is there is a data phase */
279 if(ep0_setup_pkt
.wLength
== 0)
280 /* no: wait for ack */
281 ep0_state
= EP0_WAIT_ACK
;
283 /* yes: wait ack and data */
284 ep0_state
= EP0_WAIT_DATA_ACK
;
287 static void reset_endpoints(void)
290 /* disable all endpoints except EP0 */
291 FOR_EACH_IN_EP(i
, ep
)
292 if(DIEPCTL(ep
) & DEPCTL_epena
)
293 DIEPCTL(ep
) = DEPCTL_epdis
| DEPCTL_snak
;
297 FOR_EACH_OUT_EP(i
, ep
)
298 if(DOEPCTL(ep
) & DEPCTL_epena
)
299 DOEPCTL(ep
) = DEPCTL_epdis
| DEPCTL_snak
;
303 /* 64 bytes packet size, active endpoint */
304 DOEPCTL(0) = (DEPCTL_MPS_64
<< DEPCTL_mps_bitp
) | DEPCTL_usbactep
| DEPCTL_snak
;
305 DIEPCTL(0) = (DEPCTL_MPS_64
<< DEPCTL_mps_bitp
) | DEPCTL_usbactep
| DEPCTL_snak
;
310 static void cancel_all_transfers(bool cancel_ep0
)
312 logf("usb-drv: cancel all transfers");
313 int flags
= disable_irq_save();
315 FOR_EACH_IN_EP_EX(cancel_ep0
, i
, ep
)
317 endpoints
[ep
][DIR_IN
].status
= 1;
318 endpoints
[ep
][DIR_IN
].wait
= false;
319 endpoints
[ep
][DIR_IN
].busy
= false;
320 wakeup_signal(&endpoints
[ep
][DIR_IN
].complete
);
321 DIEPCTL(ep
) = (DIEPCTL(ep
) & ~DEPCTL_usbactep
) | DEPCTL_epdis
| DEPCTL_snak
;
323 FOR_EACH_OUT_EP_EX(cancel_ep0
, i
, ep
)
325 endpoints
[ep
][DIR_OUT
].status
= 1;
326 endpoints
[ep
][DIR_OUT
].wait
= false;
327 endpoints
[ep
][DIR_OUT
].busy
= false;
328 wakeup_signal(&endpoints
[ep
][DIR_OUT
].complete
);
329 DOEPCTL(ep
) = (DOEPCTL(ep
) & ~DEPCTL_usbactep
) | DEPCTL_epdis
| DEPCTL_snak
;
335 static void core_dev_init(void)
338 /* Restart the phy clock */
340 /* Set phy speed : high speed */
341 DCFG
= (DCFG
& ~bitm(DCFG
, devspd
)) | DCFG_devspd_hs_phy_hs
;
343 /* Check hardware capabilities */
344 if(extract(GHWCFG2
, arch
) != GHWCFG2_ARCH_INTERNAL_DMA
)
345 panicf("usb-drv: wrong architecture (%ld)", extract(GHWCFG2
, arch
));
346 if(extract(GHWCFG2
, hs_phy_type
) != GHWCFG2_PHY_TYPE_UTMI
)
347 panicf("usb-drv: wrong HS phy type (%ld)", extract(GHWCFG2
, hs_phy_type
));
348 if(extract(GHWCFG2
, fs_phy_type
) != GHWCFG2_PHY_TYPE_UNSUPPORTED
)
349 panicf("usb-drv: wrong FS phy type (%ld)", extract(GHWCFG2
, fs_phy_type
));
350 if(extract(GHWCFG4
, utmi_phy_data_width
) != 0x2)
351 panicf("usb-drv: wrong utmi data width (%ld)", extract(GHWCFG4
, utmi_phy_data_width
));
352 if(!(GHWCFG4
& GHWCFG4_ded_fifo_en
)) /* it seems to be multiple tx fifo support */
353 panicf("usb-drv: no multiple tx fifo");
355 #ifdef USE_CUSTOM_FIFO_LAYOUT
356 if(!(GHWCFG2
& GHWCFG2_dyn_fifo
))
357 panicf("usb-drv: no dynamic fifo");
358 if(GRXFSIZ
!= DATA_FIFO_DEPTH
)
359 panicf("usb-drv: wrong data fifo size");
360 #endif /* USE_CUSTOM_FIFO_LAYOUT */
362 /* do some logging */
364 logf("hwcfg1: %08lx", GHWCFG1);
365 logf("hwcfg2: %08lx", GHWCFG2);
366 logf("hwcfg3: %08lx", GHWCFG3);
367 logf("hwcfg4: %08lx", GHWCFG4);
370 if(USB_NUM_ENDPOINTS
!= extract(GHWCFG2
, num_ep
))
371 panicf("usb-drv: wrong endpoint number");
373 FOR_EACH_IN_EP_AND_EP0(i
, ep
)
375 int type
= (GHWCFG1
>> GHWCFG1_epdir_bitp(ep
)) & GHWCFG1_epdir_bits
;
376 if(type
!= GHWCFG1_EPDIR_BIDIR
&& type
!= GHWCFG1_EPDIR_IN
)
377 panicf("usb-drv: EP%d is no IN or BIDIR", ep
);
379 FOR_EACH_OUT_EP_AND_EP0(i
, ep
)
381 int type
= (GHWCFG1
>> GHWCFG1_epdir_bitp(ep
)) & GHWCFG1_epdir_bits
;
382 if(type
!= GHWCFG1_EPDIR_BIDIR
&& type
!= GHWCFG1_EPDIR_OUT
)
383 panicf("usb-drv: EP%d is no OUT or BIDIR", ep
);
386 /* Setup interrupt masks for endpoints */
387 /* Setup interrupt masks */
388 DOEPMSK
= DOEPINT_setup
| DOEPINT_xfercompl
| DOEPINT_ahberr
;
389 DIEPMSK
= DIEPINT_xfercompl
| DIEPINT_timeout
| DIEPINT_ahberr
;
390 DAINTMSK
= 0xffffffff;
394 /* fixme: threshold tweaking only takes place if we use multiple tx fifos it seems */
395 /* only dump them for now, leave threshold disabled */
397 logf("threshold control:");
398 logf(" non_iso_thr_en: %d", (DTHRCTL & DTHRCTL_non_iso_thr_en) ? 1 : 0);
399 logf(" iso_thr_en: %d", (DTHRCTL & DTHRCTL_iso_thr_en) ? 1 : 0);
400 logf(" tx_thr_len: %lu", extract(DTHRCTL, tx_thr_len));
401 logf(" rx_thr_en: %d", (DTHRCTL & DTHRCTL_rx_thr_en) ? 1 : 0);
402 logf(" rx_thr_len: %lu", extract(DTHRCTL, rx_thr_len));
405 /* enable USB interrupts */
406 enable_device_interrupts();
409 static void core_init(void)
412 DCTL
|= DCTL_sftdiscon
;
413 /* Select UTMI+ 16 */
414 GUSBCFG
|= GUSBCFG_phy_if
;
416 /* fixme: the current code is for internal DMA only, the clip+ architecture
417 * define the internal DMA model */
418 /* Set burstlen and enable DMA*/
419 GAHBCFG
= (GAHBCFG_INT_DMA_BURST_INCR4
<< GAHBCFG_hburstlen_bitp
)
420 | GAHBCFG_dma_enable
;
421 /* Disable HNP and SRP, not sure it's useful because we already forced dev mode */
422 GUSBCFG
&= ~(GUSBCFG_srpcap
| GUSBCFG_hnpcapp
);
424 /* perform device model specific init */
428 DCTL
&= ~DCTL_sftdiscon
;
431 static void enable_global_interrupts(void)
433 VIC_INT_ENABLE
= INTERRUPT_USB
;
434 GAHBCFG
|= GAHBCFG_glblintrmsk
;
437 static void disable_global_interrupts(void)
439 GAHBCFG
&= ~GAHBCFG_glblintrmsk
;
440 VIC_INT_EN_CLEAR
= INTERRUPT_USB
;
443 void usb_drv_init(void)
446 logf("usb_drv_init");
449 /* Enable PHY and clocks (but leave pullups disabled) */
451 logf("usb-drv: synopsis id: %lx", GSNPSID
);
454 FOR_EACH_IN_EP_AND_EP0(i
, ep
)
455 wakeup_init(&endpoints
[ep
][DIR_IN
].complete
);
456 FOR_EACH_OUT_EP_AND_EP0(i
, ep
)
457 wakeup_init(&endpoints
[ep
][DIR_OUT
].complete
);
458 /* Enable global interrupts */
459 enable_global_interrupts();
462 void usb_drv_exit(void)
464 logf("usb_drv_exit");
466 disable_global_interrupts();
467 as3525v2_disconnect();
471 static void handle_ep_int(int ep
, bool dir_in
)
473 struct usb_endpoint
*endpoint
= &endpoints
[ep
][dir_in
];
476 if(DIEPINT(ep
) & DIEPINT_ahberr
)
477 panicf("usb-drv: ahb error on EP%d IN", ep
);
478 if(DIEPINT(ep
) & DIEPINT_xfercompl
)
480 logf("usb-drv: xfer complete on EP%d IN", ep
);
483 endpoint
->busy
= false;
484 endpoint
->status
= 0;
485 /* works even for PE0 */
486 int transfered
= endpoint
->len
- (DIEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
);
487 logf("len=%d reg=%ld xfer=%d", endpoint
->len
,
488 (DIEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
),
490 invalidate_dcache_range((void *)DIEPDMA(ep
), transfered
);
491 DIEPCTL(ep
) |= DEPCTL_snak
;
492 /* handle EP0 state if necessary,
493 * this is a ack if length is 0 */
495 handle_ep0_complete(endpoint
->len
== 0);
496 usb_core_transfer_complete(ep
, USB_DIR_IN
, 0, transfered
);
497 wakeup_signal(&endpoint
->complete
);
500 if(DIEPINT(ep
) & DIEPINT_timeout
)
502 panicf("usb-drv: timeout on EP%d IN", ep
);
505 endpoint
->busy
= false;
506 endpoint
->status
= 1;
507 /* for safety, act as if no bytes as been transfered */
509 DIEPCTL(ep
) |= DEPCTL_snak
;
510 usb_core_transfer_complete(ep
, USB_DIR_IN
, 1, 0);
511 wakeup_signal(&endpoint
->complete
);
514 /* clear interrupts */
515 DIEPINT(ep
) = DIEPINT(ep
);
519 if(DOEPINT(ep
) & DOEPINT_ahberr
)
520 panicf("usb-drv: ahb error on EP%d OUT", ep
);
521 if(DOEPINT(ep
) & DOEPINT_xfercompl
)
523 logf("usb-drv: xfer complete on EP%d OUT", ep
);
526 endpoint
->busy
= false;
527 endpoint
->status
= 0;
528 /* works even for EP0 */
529 int transfered
= endpoint
->len
- (DOEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
);
530 logf("len=%d reg=%ld xfer=%d", endpoint
->len
,
531 (DOEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
),
533 invalidate_dcache_range((void *)DOEPDMA(ep
), transfered
);
534 /* handle EP0 state if necessary,
535 * this is a ack if length is 0 */
537 handle_ep0_complete(endpoint
->len
== 0);
539 DOEPCTL(ep
) |= DEPCTL_snak
;
540 usb_core_transfer_complete(ep
, USB_DIR_OUT
, 0, transfered
);
541 wakeup_signal(&endpoint
->complete
);
544 if(DOEPINT(ep
) & DOEPINT_setup
)
546 logf("usb-drv: setup on EP%d OUT", ep
);
548 panicf("usb-drv: setup not on EP0, this is impossible");
549 if((DOEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
) != 0)
551 logf("usb-drv: ignore spurious setup (xfersize=%d)", DOEPTSIZ(ep
) & DEPTSIZ_xfersize_bits
);
555 DOEPCTL(ep
) |= DEPCTL_snak
;
556 logf("DOEPCTL0=%lx", DOEPCTL(ep
));
557 logf("DOEPTSIZE0=%lx", DOEPTSIZ(ep
));
558 if(DOEPDMA(ep
) != 8 + (unsigned long)&ep0_setup_pkt
)
559 panicf("usb-drv: EP0 wrong DMA adr (%lx vs %lx)", (unsigned long)&ep0_setup_pkt
, DOEPDMA(ep
));
560 /* handle the set address here because of a bug in the usb core */
561 invalidate_dcache_range((void*)&ep0_setup_pkt
, sizeof ep0_setup_pkt
);
562 /* handle EP0 state */
564 logf(" rt=%x r=%x", ep0_setup_pkt
.bRequestType
, ep0_setup_pkt
.bRequest
);
565 if(ep0_setup_pkt
.bRequestType
== USB_TYPE_STANDARD
&&
566 ep0_setup_pkt
.bRequest
== USB_REQ_SET_ADDRESS
)
567 usb_drv_set_address(ep0_setup_pkt
.wValue
);
568 usb_core_control_request(&ep0_setup_pkt
);
571 /* clear interrupts */
572 DOEPINT(ep
) = DOEPINT(ep
);
576 static void handle_ep_ints(void)
578 logf("usb-drv: ep int");
579 /* we must read it */
580 unsigned long daint
= DAINT
;
583 FOR_EACH_IN_EP_AND_EP0(i
, ep
)
584 if(daint
& DAINT_IN_EP(ep
))
585 handle_ep_int(ep
, true);
586 FOR_EACH_OUT_EP_AND_EP0(i
, ep
)
587 if(daint
& DAINT_OUT_EP(ep
))
588 handle_ep_int(ep
, false);
590 /* write back to clear status */
594 /* interrupt service routine */
597 /* some bits in GINTSTS can be set even though we didn't enable the interrupt source
598 * so AND it with the actual mask */
599 unsigned long sts
= GINTSTS
& GINTMSK
;
601 if(sts
& GINTMSK_usbreset
)
603 logf("usb-drv: bus reset");
605 /* Clear the Remote Wakeup Signalling */
606 //DCTL &= ~DCTL_rmtwkupsig;
609 flush_tx_fifos(0x10);
613 /* Reset Device Address */
614 DCFG
&= ~bitm(DCFG
, devadr
);
616 usb_core_bus_reset();
619 if(sts
& GINTMSK_enumdone
)
621 logf("usb-drv: enum done");
624 if(usb_drv_port_speed())
629 /* fixme: change EP0 mps here */
633 if(sts
& (GINTMSK_outepintr
| GINTMSK_inepintr
))
638 if(sts
& GINTMSK_disconnect
)
640 panicf("usb-drv: disconnect");
641 cancel_all_transfers(true);
648 int usb_drv_port_speed(void)
650 switch(extract(DSTS
, enumspd
))
652 case DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ
:
654 case DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ
:
655 case DSTS_ENUMSPD_FS_PHY_48MHZ
:
658 case DSTS_ENUMSPD_LS_PHY_6MHZ
:
659 panicf("usb-drv: LS is not supported");
662 panicf("usb-drv: wtf is this speed ?");
667 int usb_drv_request_endpoint(int type
, int dir
)
671 logf("usb-drv: request endpoint (type=%d,dir=%s)", type
, dir
== USB_DIR_IN
? "IN" : "OUT");
675 void usb_drv_release_endpoint(int ep
)
677 //logf("usb-drv: release EP%d %s", EP_NUM(ep), EP_DIR(ep) == DIR_IN ? "IN" : "OUT");
678 endpoints
[EP_NUM(ep
)][EP_DIR(ep
)].active
= false;
681 void usb_drv_cancel_all_transfers()
683 cancel_all_transfers(false);
686 static int usb_drv_transfer(int ep
, void *ptr
, int len
, bool dir_in
, bool blocking
)
689 logf("usb-drv: xfer EP%d, len=%d, dir_in=%d, blocking=%d", ep
,
690 len
, dir_in
, blocking
);
692 volatile unsigned long *epctl
= dir_in
? &DIEPCTL(ep
) : &DOEPCTL(ep
);
693 volatile unsigned long *eptsiz
= dir_in
? &DIEPTSIZ(ep
) : &DOEPTSIZ(ep
);
694 volatile unsigned long *epdma
= dir_in
? &DIEPDMA(ep
) : &DOEPDMA(ep
);
695 struct usb_endpoint
*endpoint
= &endpoints
[ep
][dir_in
];
696 #define DEPCTL *epctl
697 #define DEPTSIZ *eptsiz
698 #define DEPDMA *epdma
700 if(DEPCTL
& DEPCTL_stall
)
702 logf("usb-drv: cannot receive/send on a stalled endpoint");
707 logf("usb-drv: EP%d %s is already busy", ep
, dir_in
? "IN" : "OUT");
709 endpoint
->busy
= true;
711 endpoint
->wait
= blocking
;
712 DEPCTL
|= DEPCTL_usbactep
;
715 int nb_packets
= (len
+ mps
- 1) / mps
;
718 DEPTSIZ
= 1 << DEPTSIZ_pkcnt_bitp
;
720 DEPTSIZ
= (nb_packets
<< DEPTSIZ_pkcnt_bitp
) | len
;
721 clean_dcache_range(ptr
, len
);
722 DEPDMA
= (unsigned long)ptr
;
723 DEPCTL
|= DEPCTL_epena
| DEPCTL_cnak
;
725 /* fixme: check if endpoint was really enabled ? */
726 if((DEPCTL
& DEPCTL_epena
) == 0)
727 panicf("usb-drv: couldn't start xfer on EP%d %s", ep
, dir_in
? "IN" : "OUT");
730 wakeup_wait(&endpoint
->complete
, TIMEOUT_BLOCK
);
731 if(endpoint
->status
!= 0)
740 int usb_drv_recv(int ep
, void *ptr
, int len
)
742 return usb_drv_transfer(ep
, ptr
, len
, false, false);
745 int usb_drv_send(int ep
, void *ptr
, int len
)
747 return usb_drv_transfer(ep
, ptr
, len
, true, true);
750 int usb_drv_send_nonblocking(int ep
, void *ptr
, int len
)
752 return usb_drv_transfer(ep
, ptr
, len
, true, false);
756 void usb_drv_set_test_mode(int mode
)
761 void usb_drv_set_address(int address
)
763 /* ignore it if addres is already set */
764 if(extract(DCFG
, devadr
) == 0)
766 logf("usb-drv: set address %x", address
);
767 DCFG
= (DCFG
& ~bitm(DCFG
, devadr
)) | (address
<< DCFG_devadr_bitp
);
771 void usb_drv_stall(int ep
, bool stall
, bool in
)
776 logf("usb-drv: %sstall EP%d %s", stall
? "" : "un", ep
, in
? "IN" : "OUT");
779 bool usb_drv_stalled(int ep
, bool in
)