2010-05-18 09:58:52 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright © 2010 Amaury Pouly
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
#include "usb_drv.h"
|
|
|
|
#include "as3525v2.h"
|
|
|
|
#include "clock-target.h"
|
|
|
|
#include "ascodec.h"
|
|
|
|
#include "as3514.h"
|
2010-06-12 05:26:23 +00:00
|
|
|
#include "stdbool.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "stdio.h"
|
2010-05-18 09:58:52 +00:00
|
|
|
#include "panic.h"
|
2010-06-12 05:26:23 +00:00
|
|
|
#include "mmu-arm.h"
|
|
|
|
#include "system.h"
|
2010-09-07 20:44:26 +00:00
|
|
|
//#define LOGF_ENABLE
|
2010-05-18 09:58:52 +00:00
|
|
|
#include "logf.h"
|
|
|
|
#include "usb-drv-as3525v2.h"
|
2010-06-19 20:39:32 +00:00
|
|
|
#include "usb_core.h"
|
|
|
|
|
2010-09-08 04:27:01 +00:00
|
|
|
static const uint8_t in_ep_list[NUM_IN_EP + 1] = {0, IN_EP_LIST};
|
|
|
|
static const uint8_t out_ep_list[NUM_OUT_EP + 1] = {0, OUT_EP_LIST};
|
2010-06-19 20:39:32 +00:00
|
|
|
|
|
|
|
/* iterate through each in/out ep except EP0
|
2010-09-08 04:20:37 +00:00
|
|
|
* 'i' is the counter, 'ep' is the actual value */
|
|
|
|
#define FOR_EACH_EP(list, start, i, ep) \
|
|
|
|
for(ep = list[i = start]; \
|
|
|
|
i < (sizeof(list)/sizeof(*list)); \
|
|
|
|
i++, ep = list[i])
|
|
|
|
|
|
|
|
#define FOR_EACH_IN_EP_EX(include_ep0, i, ep) \
|
|
|
|
FOR_EACH_EP(in_ep_list, (include_ep0) ? 0 : 1, i, ep)
|
|
|
|
|
|
|
|
#define FOR_EACH_OUT_EP_EX(include_ep0, i, ep) \
|
|
|
|
FOR_EACH_EP(out_ep_list, (include_ep0) ? 0 : 1, i, ep)
|
|
|
|
|
|
|
|
#define FOR_EACH_IN_EP(i, ep) FOR_EACH_IN_EP_EX (false, i, ep)
|
|
|
|
#define FOR_EACH_IN_EP_AND_EP0(i, ep) FOR_EACH_IN_EP_EX (true, i, ep)
|
|
|
|
#define FOR_EACH_OUT_EP(i, ep) FOR_EACH_OUT_EP_EX(false, i, ep)
|
|
|
|
#define FOR_EACH_OUT_EP_AND_EP0(i, ep) FOR_EACH_OUT_EP_EX(true, i, ep)
|
2010-06-23 22:03:31 +00:00
|
|
|
|
2010-06-29 18:58:09 +00:00
|
|
|
/* store per endpoint, per direction, information */
|
2010-05-18 09:58:52 +00:00
|
|
|
struct usb_endpoint
|
|
|
|
{
|
2010-06-29 18:58:09 +00:00
|
|
|
unsigned int len; /* length of the data buffer */
|
2011-03-02 08:49:38 +00:00
|
|
|
struct semaphore complete; /* wait object */
|
2010-09-08 05:16:40 +00:00
|
|
|
int8_t status; /* completion status (0 for success) */
|
|
|
|
bool active; /* true is endpoint has been requested (true for EP0) */
|
2010-06-29 18:58:09 +00:00
|
|
|
bool wait; /* true if usb thread is blocked on completion */
|
|
|
|
bool busy; /* true is a transfer is pending */
|
2010-05-18 09:58:52 +00:00
|
|
|
};
|
|
|
|
|
2010-06-29 18:58:09 +00:00
|
|
|
/* state of EP0 (to correctly schedule setup packet enqueing) */
|
|
|
|
enum ep0state
|
|
|
|
{
|
|
|
|
/* Setup packet is enqueud, waiting for actual data */
|
|
|
|
EP0_WAIT_SETUP = 0,
|
|
|
|
/* Waiting for ack (either IN or OUT) */
|
|
|
|
EP0_WAIT_ACK = 1,
|
|
|
|
/* Ack complete, waiting for data (either IN or OUT)
|
|
|
|
* This state is necessary because if both ack and data complete in the
|
|
|
|
* same interrupt, we might process data completion before ack completion
|
|
|
|
* so we need this bizarre state */
|
|
|
|
EP0_WAIT_DATA = 2,
|
|
|
|
/* Setup packet complete, waiting for ack and data */
|
|
|
|
EP0_WAIT_DATA_ACK = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* endpoints[ep_num][DIR_IN/DIR_OUT] */
|
2010-06-23 22:04:04 +00:00
|
|
|
static struct usb_endpoint endpoints[USB_NUM_ENDPOINTS][2];
|
2010-06-29 18:58:09 +00:00
|
|
|
/* setup packet for EP0 */
|
2010-09-08 01:59:41 +00:00
|
|
|
static struct usb_ctrlrequest _ep0_setup_pkt __attribute__((aligned(32)));
|
|
|
|
static struct usb_ctrlrequest *ep0_setup_pkt = AS3525_UNCACHED_ADDR(&_ep0_setup_pkt);
|
2010-09-07 18:41:12 +00:00
|
|
|
|
2010-06-29 18:58:09 +00:00
|
|
|
/* state of EP0 */
|
2010-07-30 23:47:49 +00:00
|
|
|
static enum ep0state ep0_state;
|
2010-05-18 09:58:52 +00:00
|
|
|
|
|
|
|
void usb_attach(void)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: attach");
|
2011-02-04 05:46:55 +00:00
|
|
|
/* Nothing to do */
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 13:00:16 +00:00
|
|
|
static inline void usb_delay(void)
|
2010-05-18 09:58:52 +00:00
|
|
|
{
|
2011-05-03 17:17:23 +00:00
|
|
|
register int i = 0;
|
|
|
|
asm volatile(
|
|
|
|
"1: nop \n"
|
|
|
|
" add %0, %0, #1 \n"
|
|
|
|
" cmp %0, #0x300 \n"
|
|
|
|
" bne 1b \n"
|
|
|
|
: "+r"(i)
|
|
|
|
);
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void as3525v2_connect(void)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: init as3525v2");
|
2010-05-18 09:58:52 +00:00
|
|
|
/* 1) enable usb core clock */
|
2010-07-02 06:00:00 +00:00
|
|
|
bitset32(&CGU_PERI, CGU_USB_CLOCK_ENABLE);
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 2) enable usb phy clock */
|
2010-09-07 18:41:12 +00:00
|
|
|
CCU_USB = (CCU_USB & ~(3<<24)) | (1 << 24); /* ?? */
|
2010-06-21 20:25:37 +00:00
|
|
|
/* PHY clock */
|
2010-09-02 13:00:16 +00:00
|
|
|
CGU_USB = 1<<5 /* enable */
|
2010-09-07 18:41:12 +00:00
|
|
|
| 0 << 2
|
2010-09-08 01:59:41 +00:00
|
|
|
| 0; /* source = ? (24MHz crystal?) */
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 3) clear "stop pclk" */
|
2010-06-19 20:39:57 +00:00
|
|
|
PCGCCTL &= ~0x1;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 4) clear "power clamp" */
|
2010-06-19 20:39:57 +00:00
|
|
|
PCGCCTL &= ~0x4;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 5) clear "reset power down module" */
|
2010-06-19 20:39:57 +00:00
|
|
|
PCGCCTL &= ~0x8;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 6) set "power on program done" */
|
2010-06-19 20:39:57 +00:00
|
|
|
DCTL |= DCTL_pwronprgdone;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 7) core soft reset */
|
2010-06-19 20:39:57 +00:00
|
|
|
GRSTCTL |= GRSTCTL_csftrst;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 8) hclk soft reset */
|
2010-06-19 20:39:57 +00:00
|
|
|
GRSTCTL |= GRSTCTL_hsftrst;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 9) flush and reset everything */
|
2010-06-19 20:39:57 +00:00
|
|
|
GRSTCTL |= 0x3f;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 10) force device mode*/
|
2010-06-19 20:39:57 +00:00
|
|
|
GUSBCFG &= ~GUSBCFG_force_host_mode;
|
|
|
|
GUSBCFG |= GUSBCFG_force_device_mode;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
/* 11) Do something that is probably CCU related but undocumented*/
|
2010-09-12 15:32:46 +00:00
|
|
|
CCU_USB |= 0x1000;
|
2010-07-22 13:07:56 +00:00
|
|
|
CCU_USB &= ~0x300000;
|
2010-05-20 14:54:32 +00:00
|
|
|
usb_delay();
|
2010-05-18 09:58:52 +00:00
|
|
|
/* 12) reset usb core parameters (dev addr, speed, ...) */
|
2010-06-19 20:39:57 +00:00
|
|
|
DCFG = 0;
|
2010-05-18 09:58:52 +00:00
|
|
|
usb_delay();
|
|
|
|
}
|
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
static void as3525v2_disconnect(void)
|
|
|
|
{
|
2011-01-28 02:30:03 +00:00
|
|
|
/* Disconnect */
|
|
|
|
DCTL |= DCTL_sftdiscon;
|
2011-01-29 00:25:42 +00:00
|
|
|
sleep(HZ/20);
|
2010-09-07 18:41:12 +00:00
|
|
|
/* Disable clock */
|
|
|
|
CGU_USB = 0;
|
|
|
|
usb_delay();
|
|
|
|
bitclr32(&CGU_PERI, CGU_USB_CLOCK_ENABLE);
|
2010-06-23 22:04:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:57 +00:00
|
|
|
static void enable_device_interrupts(void)
|
2010-05-20 14:54:32 +00:00
|
|
|
{
|
|
|
|
/* Clear any pending interrupt */
|
2010-06-19 20:39:57 +00:00
|
|
|
GINTSTS = 0xffffffff;
|
2010-06-19 20:39:32 +00:00
|
|
|
/* Clear any pending otg interrupt */
|
2010-06-19 20:39:57 +00:00
|
|
|
GOTGINT = 0xffffffff;
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Enable interrupts */
|
2010-06-19 20:39:57 +00:00
|
|
|
GINTMSK = GINTMSK_usbreset
|
2010-09-02 13:00:16 +00:00
|
|
|
| GINTMSK_enumdone
|
|
|
|
| GINTMSK_inepintr
|
|
|
|
| GINTMSK_outepintr
|
|
|
|
| GINTMSK_disconnect
|
|
|
|
| GINTMSK_usbsuspend
|
|
|
|
| GINTMSK_wkupintr
|
|
|
|
| GINTMSK_otgintr;
|
2010-05-20 14:54:32 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:57 +00:00
|
|
|
static void flush_tx_fifos(int nums)
|
2010-05-19 08:33:50 +00:00
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-06-21 20:25:37 +00:00
|
|
|
GRSTCTL = (nums << GRSTCTL_txfnum_bitp)
|
|
|
|
| GRSTCTL_txfflsh_flush;
|
2010-06-19 20:39:57 +00:00
|
|
|
while(GRSTCTL & GRSTCTL_txfflsh_flush && i < 0x300)
|
2010-05-19 08:33:50 +00:00
|
|
|
i++;
|
2010-06-19 20:39:57 +00:00
|
|
|
if(GRSTCTL & GRSTCTL_txfflsh_flush)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: hang of flush tx fifos (%x)", nums);
|
2010-05-19 08:33:50 +00:00
|
|
|
/* wait 3 phy clocks */
|
2010-06-12 05:26:23 +00:00
|
|
|
udelay(1);
|
2010-05-19 08:33:50 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
static void prepare_setup_ep0(void)
|
|
|
|
{
|
2010-06-29 19:02:51 +00:00
|
|
|
logf("usb-drv: prepare EP0");
|
2010-06-23 22:04:04 +00:00
|
|
|
/* setup DMA */
|
2010-09-08 01:59:41 +00:00
|
|
|
DOEPDMA(0) = (unsigned long)AS3525_PHYSICAL_ADDR(&_ep0_setup_pkt);
|
2010-06-23 22:04:04 +00:00
|
|
|
|
|
|
|
/* Setup EP0 OUT with the following parameters:
|
|
|
|
* packet count = 1
|
|
|
|
* setup packet count = 1
|
|
|
|
* transfer size = 8 (setup packet)
|
|
|
|
*/
|
|
|
|
DOEPTSIZ(0) = (1 << DEPTSIZ0_supcnt_bitp)
|
|
|
|
| (1 << DEPTSIZ0_pkcnt_bitp)
|
|
|
|
| 8;
|
|
|
|
|
|
|
|
/* Enable endpoint, clear nak */
|
2010-06-29 18:58:09 +00:00
|
|
|
ep0_state = EP0_WAIT_SETUP;
|
2010-09-08 04:37:41 +00:00
|
|
|
DOEPCTL(0) |= DEPCTL_epena | DEPCTL_cnak;
|
2010-06-29 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_ep0_complete(bool is_ack)
|
|
|
|
{
|
|
|
|
switch(ep0_state)
|
|
|
|
{
|
|
|
|
case EP0_WAIT_SETUP:
|
|
|
|
panicf("usb-drv: EP0 completion while waiting for SETUP");
|
|
|
|
case EP0_WAIT_ACK:
|
|
|
|
if(is_ack)
|
|
|
|
/* everything is done, prepare next setup */
|
|
|
|
prepare_setup_ep0();
|
|
|
|
else
|
|
|
|
panicf("usb-drv: EP0 data completion while waiting for ACK");
|
|
|
|
break;
|
|
|
|
case EP0_WAIT_DATA:
|
|
|
|
if(is_ack)
|
|
|
|
panicf("usb-drv: EP0 ACK while waiting for data completion");
|
|
|
|
else
|
|
|
|
/* everything is done, prepare next setup */
|
|
|
|
prepare_setup_ep0();
|
|
|
|
break;
|
|
|
|
case EP0_WAIT_DATA_ACK:
|
|
|
|
/* update state */
|
|
|
|
if(is_ack)
|
|
|
|
ep0_state = EP0_WAIT_DATA;
|
|
|
|
else
|
|
|
|
ep0_state = EP0_WAIT_ACK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panicf("usb-drv: invalid EP0 state");
|
|
|
|
}
|
|
|
|
logf("usb-drv: EP0 state updated to %d", ep0_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_ep0_setup(void)
|
|
|
|
{
|
|
|
|
if(ep0_state != EP0_WAIT_SETUP)
|
|
|
|
{
|
|
|
|
logf("usb-drv: EP0 SETUP while in state %d", ep0_state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* determine is there is a data phase */
|
2010-09-08 01:59:41 +00:00
|
|
|
if(ep0_setup_pkt->wLength == 0)
|
2010-06-29 18:58:09 +00:00
|
|
|
/* no: wait for ack */
|
|
|
|
ep0_state = EP0_WAIT_ACK;
|
|
|
|
else
|
|
|
|
/* yes: wait ack and data */
|
|
|
|
ep0_state = EP0_WAIT_DATA_ACK;
|
2010-09-02 13:00:16 +00:00
|
|
|
logf("usb-drv: EP0 state updated to %d", ep0_state);
|
2010-06-23 22:04:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:32 +00:00
|
|
|
static void reset_endpoints(void)
|
|
|
|
{
|
2010-09-08 04:20:37 +00:00
|
|
|
unsigned i;
|
|
|
|
int ep;
|
2010-06-19 20:39:32 +00:00
|
|
|
/* disable all endpoints except EP0 */
|
2010-09-02 13:00:16 +00:00
|
|
|
FOR_EACH_IN_EP_AND_EP0(i, ep)
|
|
|
|
{
|
|
|
|
endpoints[ep][DIR_IN].active = false;
|
|
|
|
endpoints[ep][DIR_IN].busy = false;
|
2010-09-08 03:31:18 +00:00
|
|
|
endpoints[ep][DIR_IN].status = -1;
|
2010-09-02 13:00:16 +00:00
|
|
|
if(endpoints[ep][DIR_IN].wait)
|
2011-01-24 15:03:56 +00:00
|
|
|
{
|
|
|
|
endpoints[ep][DIR_IN].wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoints[ep][DIR_IN].complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-06-19 20:39:57 +00:00
|
|
|
if(DIEPCTL(ep) & DEPCTL_epena)
|
2010-09-07 21:01:16 +00:00
|
|
|
DIEPCTL(ep) = DEPCTL_snak;
|
2010-06-19 20:39:32 +00:00
|
|
|
else
|
2010-06-19 20:39:57 +00:00
|
|
|
DIEPCTL(ep) = 0;
|
2010-09-02 13:00:16 +00:00
|
|
|
}
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-02 13:00:16 +00:00
|
|
|
FOR_EACH_OUT_EP_AND_EP0(i, ep)
|
|
|
|
{
|
|
|
|
endpoints[ep][DIR_OUT].active = false;
|
|
|
|
endpoints[ep][DIR_OUT].busy = false;
|
2010-09-08 03:31:18 +00:00
|
|
|
endpoints[ep][DIR_OUT].status = -1;
|
2010-09-02 13:00:16 +00:00
|
|
|
if(endpoints[ep][DIR_OUT].wait)
|
2011-01-24 15:03:56 +00:00
|
|
|
{
|
|
|
|
endpoints[ep][DIR_OUT].wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoints[ep][DIR_OUT].complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-06-19 20:39:57 +00:00
|
|
|
if(DOEPCTL(ep) & DEPCTL_epena)
|
2010-09-07 21:01:16 +00:00
|
|
|
DOEPCTL(ep) = DEPCTL_snak;
|
2010-06-19 20:39:32 +00:00
|
|
|
else
|
2010-06-19 20:39:57 +00:00
|
|
|
DOEPCTL(ep) = 0;
|
2010-09-02 13:00:16 +00:00
|
|
|
}
|
2010-06-19 20:39:32 +00:00
|
|
|
/* 64 bytes packet size, active endpoint */
|
2010-06-29 18:58:09 +00:00
|
|
|
DOEPCTL(0) = (DEPCTL_MPS_64 << DEPCTL_mps_bitp) | DEPCTL_usbactep | DEPCTL_snak;
|
|
|
|
DIEPCTL(0) = (DEPCTL_MPS_64 << DEPCTL_mps_bitp) | DEPCTL_usbactep | DEPCTL_snak;
|
2010-09-07 20:44:26 +00:00
|
|
|
/* Setup next chain for IN eps */
|
|
|
|
FOR_EACH_IN_EP_AND_EP0(i, ep)
|
|
|
|
{
|
2010-10-14 19:15:22 +00:00
|
|
|
int next_ep = in_ep_list[(i + 1) % (NUM_IN_EP + 1)];
|
2010-09-07 20:44:26 +00:00
|
|
|
DIEPCTL(ep) = (DIEPCTL(ep) & ~bitm(DEPCTL, nextep)) | (next_ep << DEPCTL_nextep_bitp);
|
|
|
|
}
|
2010-06-19 20:39:32 +00:00
|
|
|
}
|
|
|
|
|
2010-06-27 22:06:30 +00:00
|
|
|
static void cancel_all_transfers(bool cancel_ep0)
|
|
|
|
{
|
|
|
|
logf("usb-drv: cancel all transfers");
|
|
|
|
int flags = disable_irq_save();
|
2010-09-08 04:20:37 +00:00
|
|
|
int ep;
|
|
|
|
unsigned i;
|
|
|
|
|
2010-06-27 22:06:30 +00:00
|
|
|
FOR_EACH_IN_EP_EX(cancel_ep0, i, ep)
|
|
|
|
{
|
2010-09-08 03:31:18 +00:00
|
|
|
endpoints[ep][DIR_IN].status = -1;
|
2010-06-27 22:06:30 +00:00
|
|
|
endpoints[ep][DIR_IN].busy = false;
|
2011-01-24 15:03:56 +00:00
|
|
|
if(endpoints[ep][DIR_IN].wait)
|
|
|
|
{
|
|
|
|
endpoints[ep][DIR_IN].wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoints[ep][DIR_IN].complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-09-07 21:01:16 +00:00
|
|
|
DIEPCTL(ep) = (DIEPCTL(ep) & ~DEPCTL_usbactep) | DEPCTL_snak;
|
2010-06-27 22:06:30 +00:00
|
|
|
}
|
|
|
|
FOR_EACH_OUT_EP_EX(cancel_ep0, i, ep)
|
|
|
|
{
|
2010-09-08 03:31:18 +00:00
|
|
|
endpoints[ep][DIR_OUT].status = -1;
|
2010-06-27 22:06:30 +00:00
|
|
|
endpoints[ep][DIR_OUT].busy = false;
|
2011-01-24 15:03:56 +00:00
|
|
|
if(endpoints[ep][DIR_OUT].wait)
|
|
|
|
{
|
|
|
|
endpoints[ep][DIR_OUT].wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoints[ep][DIR_OUT].complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-09-07 21:01:16 +00:00
|
|
|
DOEPCTL(ep) = (DOEPCTL(ep) & ~DEPCTL_usbactep) | DEPCTL_snak;
|
2010-06-27 22:06:30 +00:00
|
|
|
}
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-06-27 22:06:30 +00:00
|
|
|
restore_irq(flags);
|
|
|
|
}
|
|
|
|
|
2010-05-20 14:54:32 +00:00
|
|
|
static void core_dev_init(void)
|
|
|
|
{
|
2010-09-08 04:20:37 +00:00
|
|
|
int ep;
|
|
|
|
unsigned int i;
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Restart the phy clock */
|
2010-06-19 20:39:57 +00:00
|
|
|
PCGCCTL = 0;
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Set phy speed : high speed */
|
2010-09-07 18:41:12 +00:00
|
|
|
DCFG = (DCFG & ~bitm(DCFG, devspd)) | DCFG_devspd_hs_phy_hs;
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Check hardware capabilities */
|
2010-06-19 20:40:07 +00:00
|
|
|
if(extract(GHWCFG2, arch) != GHWCFG2_ARCH_INTERNAL_DMA)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: wrong architecture (%ld)", extract(GHWCFG2, arch));
|
2010-06-19 20:40:07 +00:00
|
|
|
if(extract(GHWCFG2, hs_phy_type) != GHWCFG2_PHY_TYPE_UTMI)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: wrong HS phy type (%ld)", extract(GHWCFG2, hs_phy_type));
|
2010-06-19 20:40:07 +00:00
|
|
|
if(extract(GHWCFG2, fs_phy_type) != GHWCFG2_PHY_TYPE_UNSUPPORTED)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: wrong FS phy type (%ld)", extract(GHWCFG2, fs_phy_type));
|
2010-06-19 20:40:07 +00:00
|
|
|
if(extract(GHWCFG4, utmi_phy_data_width) != 0x2)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: wrong utmi data width (%ld)", extract(GHWCFG4, utmi_phy_data_width));
|
2010-06-19 20:40:07 +00:00
|
|
|
if(!(GHWCFG4 & GHWCFG4_ded_fifo_en)) /* it seems to be multiple tx fifo support */
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: no multiple tx fifo");
|
2010-05-19 08:33:50 +00:00
|
|
|
|
2010-06-19 20:39:57 +00:00
|
|
|
#ifdef USE_CUSTOM_FIFO_LAYOUT
|
2010-06-19 20:40:07 +00:00
|
|
|
if(!(GHWCFG2 & GHWCFG2_dyn_fifo))
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: no dynamic fifo");
|
2010-06-19 20:39:57 +00:00
|
|
|
if(GRXFSIZ != DATA_FIFO_DEPTH)
|
2010-06-23 22:04:04 +00:00
|
|
|
panicf("usb-drv: wrong data fifo size");
|
2010-06-19 20:39:57 +00:00
|
|
|
#endif /* USE_CUSTOM_FIFO_LAYOUT */
|
2010-06-19 20:39:38 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
if(USB_NUM_ENDPOINTS != extract(GHWCFG2, num_ep))
|
|
|
|
panicf("usb-drv: wrong endpoint number");
|
2010-06-19 20:39:38 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
FOR_EACH_IN_EP_AND_EP0(i, ep)
|
2010-06-19 20:39:38 +00:00
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
int type = (GHWCFG1 >> GHWCFG1_epdir_bitp(ep)) & GHWCFG1_epdir_bits;
|
|
|
|
if(type != GHWCFG1_EPDIR_BIDIR && type != GHWCFG1_EPDIR_IN)
|
|
|
|
panicf("usb-drv: EP%d is no IN or BIDIR", ep);
|
2010-06-19 20:39:38 +00:00
|
|
|
}
|
2010-06-23 22:04:04 +00:00
|
|
|
FOR_EACH_OUT_EP_AND_EP0(i, ep)
|
2010-05-19 08:33:50 +00:00
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
int type = (GHWCFG1 >> GHWCFG1_epdir_bitp(ep)) & GHWCFG1_epdir_bits;
|
|
|
|
if(type != GHWCFG1_EPDIR_BIDIR && type != GHWCFG1_EPDIR_OUT)
|
|
|
|
panicf("usb-drv: EP%d is no OUT or BIDIR", ep);
|
2010-05-19 08:33:50 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 18:41:12 +00:00
|
|
|
/* Setup FIFOs */
|
2010-09-08 00:29:22 +00:00
|
|
|
GRXFSIZ = 512;
|
|
|
|
GNPTXFSIZ = MAKE_FIFOSIZE_DATA(512, 512);
|
2010-09-07 18:41:12 +00:00
|
|
|
|
2010-06-19 20:39:38 +00:00
|
|
|
/* Setup interrupt masks for endpoints */
|
2010-06-19 20:39:32 +00:00
|
|
|
/* Setup interrupt masks */
|
2010-06-23 22:03:31 +00:00
|
|
|
DOEPMSK = DOEPINT_setup | DOEPINT_xfercompl | DOEPINT_ahberr;
|
|
|
|
DIEPMSK = DIEPINT_xfercompl | DIEPINT_timeout | DIEPINT_ahberr;
|
2010-06-19 20:39:57 +00:00
|
|
|
DAINTMSK = 0xffffffff;
|
2010-05-19 08:33:50 +00:00
|
|
|
|
2010-06-19 20:39:32 +00:00
|
|
|
reset_endpoints();
|
2010-05-19 08:33:50 +00:00
|
|
|
|
2010-09-07 18:41:12 +00:00
|
|
|
prepare_setup_ep0();
|
|
|
|
|
2010-05-20 14:54:32 +00:00
|
|
|
/* enable USB interrupts */
|
2010-06-19 20:39:57 +00:00
|
|
|
enable_device_interrupts();
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void core_init(void)
|
|
|
|
{
|
2010-06-19 20:39:38 +00:00
|
|
|
/* Disconnect */
|
2010-06-19 20:39:57 +00:00
|
|
|
DCTL |= DCTL_sftdiscon;
|
2010-06-12 05:26:23 +00:00
|
|
|
/* Select UTMI+ 16 */
|
2010-06-19 20:39:57 +00:00
|
|
|
GUSBCFG |= GUSBCFG_phy_if;
|
2010-09-02 13:00:16 +00:00
|
|
|
GUSBCFG = (GUSBCFG & ~bitm(GUSBCFG, toutcal)) | 7 << GUSBCFG_toutcal_bitp;
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-05-18 09:58:52 +00:00
|
|
|
/* fixme: the current code is for internal DMA only, the clip+ architecture
|
|
|
|
* define the internal DMA model */
|
2010-06-12 05:26:23 +00:00
|
|
|
/* Set burstlen and enable DMA*/
|
2010-09-02 13:00:16 +00:00
|
|
|
GAHBCFG = (GAHBCFG_INT_DMA_BURST_INCR << GAHBCFG_hburstlen_bitp)
|
2010-06-19 20:39:57 +00:00
|
|
|
| GAHBCFG_dma_enable;
|
2010-05-18 09:58:52 +00:00
|
|
|
/* Disable HNP and SRP, not sure it's useful because we already forced dev mode */
|
2010-06-19 20:39:57 +00:00
|
|
|
GUSBCFG &= ~(GUSBCFG_srpcap | GUSBCFG_hnpcapp);
|
2010-05-19 08:33:50 +00:00
|
|
|
|
|
|
|
/* perform device model specific init */
|
|
|
|
core_dev_init();
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-06-19 20:39:38 +00:00
|
|
|
/* Reconnect */
|
2010-06-19 20:39:57 +00:00
|
|
|
DCTL &= ~DCTL_sftdiscon;
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:57 +00:00
|
|
|
static void enable_global_interrupts(void)
|
2010-05-20 14:54:32 +00:00
|
|
|
{
|
|
|
|
VIC_INT_ENABLE = INTERRUPT_USB;
|
2010-06-19 20:39:57 +00:00
|
|
|
GAHBCFG |= GAHBCFG_glblintrmsk;
|
2010-05-20 14:54:32 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:57 +00:00
|
|
|
static void disable_global_interrupts(void)
|
2010-05-20 14:54:32 +00:00
|
|
|
{
|
2010-06-19 20:39:57 +00:00
|
|
|
GAHBCFG &= ~GAHBCFG_glblintrmsk;
|
2010-05-20 14:54:32 +00:00
|
|
|
VIC_INT_EN_CLEAR = INTERRUPT_USB;
|
|
|
|
}
|
|
|
|
|
2010-05-18 09:58:52 +00:00
|
|
|
void usb_drv_init(void)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
unsigned i, ep;
|
2010-05-18 09:58:52 +00:00
|
|
|
logf("usb_drv_init");
|
2010-06-23 22:04:04 +00:00
|
|
|
/* Boost cpu */
|
|
|
|
cpu_boost(1);
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Enable PHY and clocks (but leave pullups disabled) */
|
2010-05-18 09:58:52 +00:00
|
|
|
as3525v2_connect();
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: synopsis id: %lx", GSNPSID);
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Core init */
|
2010-05-18 09:58:52 +00:00
|
|
|
core_init();
|
2010-06-23 22:04:04 +00:00
|
|
|
FOR_EACH_IN_EP_AND_EP0(i, ep)
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_init(&endpoints[ep][DIR_IN].complete, 1, 0);
|
2010-06-23 22:04:04 +00:00
|
|
|
FOR_EACH_OUT_EP_AND_EP0(i, ep)
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_init(&endpoints[ep][DIR_OUT].complete, 1, 0);
|
2010-05-20 14:54:32 +00:00
|
|
|
/* Enable global interrupts */
|
2010-06-19 20:39:57 +00:00
|
|
|
enable_global_interrupts();
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_exit(void)
|
|
|
|
{
|
|
|
|
logf("usb_drv_exit");
|
2010-06-19 20:49:06 +00:00
|
|
|
|
|
|
|
disable_global_interrupts();
|
2010-06-23 22:04:04 +00:00
|
|
|
as3525v2_disconnect();
|
|
|
|
cpu_boost(0);
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-09-08 05:43:05 +00:00
|
|
|
static void handle_ep_in_int(int ep)
|
2010-06-21 20:25:46 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
struct usb_endpoint *endpoint = &endpoints[ep][DIR_IN];
|
|
|
|
unsigned long sts = DIEPINT(ep);
|
|
|
|
if(sts & DIEPINT_ahberr)
|
|
|
|
panicf("usb-drv: ahb error on EP%d IN", ep);
|
|
|
|
if(sts & DIEPINT_xfercompl)
|
2010-06-19 20:39:38 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
if(endpoint->busy)
|
2010-06-23 22:03:31 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
endpoint->busy = false;
|
|
|
|
endpoint->status = 0;
|
|
|
|
/* works even for EP0 */
|
|
|
|
int size = (DIEPTSIZ(ep) & DEPTSIZ_xfersize_bits);
|
|
|
|
int transfered = endpoint->len - size;
|
2011-01-27 12:15:46 +00:00
|
|
|
logf("len=%d reg=%d xfer=%d", endpoint->len, size, transfered);
|
2010-09-08 05:43:05 +00:00
|
|
|
/* handle EP0 state if necessary,
|
|
|
|
* this is a ack if length is 0 */
|
|
|
|
if(ep == 0)
|
|
|
|
handle_ep0_complete(endpoint->len == 0);
|
|
|
|
endpoint->len = size;
|
|
|
|
usb_core_transfer_complete(ep, USB_DIR_IN, 0, transfered);
|
2011-01-24 15:03:56 +00:00
|
|
|
if(endpoint->wait)
|
|
|
|
{
|
|
|
|
endpoint->wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoint->complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-06-23 22:03:31 +00:00
|
|
|
}
|
2010-09-08 05:43:05 +00:00
|
|
|
}
|
|
|
|
if(sts & DIEPINT_timeout)
|
|
|
|
{
|
|
|
|
panicf("usb-drv: timeout on EP%d IN", ep);
|
|
|
|
if(endpoint->busy)
|
2010-06-23 22:03:31 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
endpoint->busy = false;
|
|
|
|
endpoint->status = -1;
|
|
|
|
/* for safety, act as if no bytes as been transfered */
|
|
|
|
endpoint->len = 0;
|
|
|
|
usb_core_transfer_complete(ep, USB_DIR_IN, 1, 0);
|
2011-01-24 15:03:56 +00:00
|
|
|
if(endpoint->wait)
|
|
|
|
{
|
|
|
|
endpoint->wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoint->complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-06-23 22:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 05:43:05 +00:00
|
|
|
/* clear interrupts */
|
|
|
|
DIEPINT(ep) = sts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_ep_out_int(int ep)
|
|
|
|
{
|
|
|
|
struct usb_endpoint *endpoint = &endpoints[ep][DIR_OUT];
|
|
|
|
unsigned long sts = DOEPINT(ep);
|
|
|
|
if(sts & DOEPINT_ahberr)
|
|
|
|
panicf("usb-drv: ahb error on EP%d OUT", ep);
|
|
|
|
if(sts & DOEPINT_xfercompl)
|
2010-06-23 22:03:31 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
logf("usb-drv: xfer complete on EP%d OUT", ep);
|
|
|
|
if(endpoint->busy)
|
2010-06-23 22:03:31 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
endpoint->busy = false;
|
|
|
|
endpoint->status = 0;
|
|
|
|
/* works even for EP0 */
|
|
|
|
int transfered = endpoint->len - (DOEPTSIZ(ep) & DEPTSIZ_xfersize_bits);
|
|
|
|
logf("len=%d reg=%ld xfer=%d", endpoint->len,
|
|
|
|
(DOEPTSIZ(ep) & DEPTSIZ_xfersize_bits),
|
|
|
|
transfered);
|
|
|
|
/* handle EP0 state if necessary,
|
|
|
|
* this is a ack if length is 0 */
|
|
|
|
if(ep == 0)
|
|
|
|
handle_ep0_complete(endpoint->len == 0);
|
|
|
|
usb_core_transfer_complete(ep, USB_DIR_OUT, 0, transfered);
|
2011-01-24 15:03:56 +00:00
|
|
|
if(endpoint->wait)
|
|
|
|
{
|
|
|
|
endpoint->wait = false;
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&endpoint->complete);
|
2011-01-24 15:03:56 +00:00
|
|
|
}
|
2010-06-23 22:03:31 +00:00
|
|
|
}
|
2010-09-08 05:43:05 +00:00
|
|
|
}
|
|
|
|
if(sts & DOEPINT_setup)
|
|
|
|
{
|
|
|
|
logf("usb-drv: setup on EP%d OUT", ep);
|
|
|
|
if(ep != 0)
|
|
|
|
panicf("usb-drv: setup not on EP0, this is impossible");
|
|
|
|
if((DOEPTSIZ(ep) & DEPTSIZ_xfersize_bits) != 0)
|
2010-06-23 22:03:31 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
logf("usb-drv: ignore spurious setup (xfersize=%ld)", DOEPTSIZ(ep) & DEPTSIZ_xfersize_bits);
|
|
|
|
prepare_setup_ep0();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* handle EP0 state */
|
|
|
|
handle_ep0_setup();
|
|
|
|
logf(" rt=%x r=%x", ep0_setup_pkt->bRequestType, ep0_setup_pkt->bRequest);
|
|
|
|
/* handle set address */
|
|
|
|
if(ep0_setup_pkt->bRequestType == USB_TYPE_STANDARD &&
|
|
|
|
ep0_setup_pkt->bRequest == USB_REQ_SET_ADDRESS)
|
2010-06-29 18:58:09 +00:00
|
|
|
{
|
2010-09-08 05:43:05 +00:00
|
|
|
/* Set address now */
|
|
|
|
DCFG = (DCFG & ~bitm(DCFG, devadr)) | (ep0_setup_pkt->wValue << DCFG_devadr_bitp);
|
2010-06-29 18:58:09 +00:00
|
|
|
}
|
2010-09-08 05:43:05 +00:00
|
|
|
usb_core_control_request(ep0_setup_pkt);
|
2010-06-23 22:03:31 +00:00
|
|
|
}
|
2010-06-19 20:39:38 +00:00
|
|
|
}
|
2010-09-08 05:43:05 +00:00
|
|
|
/* clear interrupts */
|
|
|
|
DOEPINT(ep) = sts;
|
2010-06-12 05:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
static void handle_ep_ints(void)
|
2010-06-12 05:26:23 +00:00
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: ep int");
|
2010-06-23 22:03:31 +00:00
|
|
|
/* we must read it */
|
|
|
|
unsigned long daint = DAINT;
|
|
|
|
unsigned i, ep;
|
|
|
|
|
|
|
|
FOR_EACH_IN_EP_AND_EP0(i, ep)
|
|
|
|
if(daint & DAINT_IN_EP(ep))
|
2010-09-08 05:43:05 +00:00
|
|
|
handle_ep_in_int(ep);
|
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
FOR_EACH_OUT_EP_AND_EP0(i, ep)
|
|
|
|
if(daint & DAINT_OUT_EP(ep))
|
2010-09-08 05:43:05 +00:00
|
|
|
handle_ep_out_int(ep);
|
2010-06-23 22:03:31 +00:00
|
|
|
|
|
|
|
/* write back to clear status */
|
|
|
|
DAINT = daint;
|
2010-06-12 05:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 09:58:52 +00:00
|
|
|
/* interrupt service routine */
|
|
|
|
void INT_USB(void)
|
|
|
|
{
|
2010-06-12 05:26:23 +00:00
|
|
|
/* some bits in GINTSTS can be set even though we didn't enable the interrupt source
|
|
|
|
* so AND it with the actual mask */
|
2010-06-19 20:39:57 +00:00
|
|
|
unsigned long sts = GINTSTS & GINTMSK;
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
if(sts & GINTMSK_usbreset)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: bus reset");
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
/* Clear the Remote Wakeup Signalling */
|
2010-09-02 13:00:16 +00:00
|
|
|
DCTL &= ~DCTL_rmtwkupsig;
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
/* Flush FIFOs */
|
|
|
|
flush_tx_fifos(0x10);
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-09-07 18:41:12 +00:00
|
|
|
/* Flush the Learning Queue */
|
|
|
|
GRSTCTL = GRSTCTL_intknqflsh;
|
2010-06-23 22:03:31 +00:00
|
|
|
|
|
|
|
/* Reset Device Address */
|
2010-06-23 22:04:04 +00:00
|
|
|
DCFG &= ~bitm(DCFG, devadr);
|
2010-06-23 22:03:31 +00:00
|
|
|
|
2010-09-07 18:41:12 +00:00
|
|
|
reset_endpoints();
|
|
|
|
prepare_setup_ep0();
|
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
usb_core_bus_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sts & GINTMSK_enumdone)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: enum done");
|
2010-06-23 22:03:31 +00:00
|
|
|
|
|
|
|
/* read speed */
|
2010-06-27 22:06:30 +00:00
|
|
|
if(usb_drv_port_speed())
|
|
|
|
logf("usb-drv: HS");
|
|
|
|
else
|
|
|
|
logf("usb-drv: FS");
|
2010-09-02 13:00:16 +00:00
|
|
|
}
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-02 13:00:16 +00:00
|
|
|
if(sts & GINTMSK_otgintr)
|
|
|
|
{
|
|
|
|
logf("usb-drv: otg int");
|
|
|
|
GOTGINT = 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2010-06-23 22:03:31 +00:00
|
|
|
if(sts & (GINTMSK_outepintr | GINTMSK_inepintr))
|
|
|
|
{
|
|
|
|
handle_ep_ints();
|
|
|
|
}
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
if(sts & GINTMSK_disconnect)
|
2010-06-27 22:06:30 +00:00
|
|
|
{
|
|
|
|
cancel_all_transfers(true);
|
|
|
|
}
|
2010-06-12 05:26:23 +00:00
|
|
|
|
2010-09-02 13:00:16 +00:00
|
|
|
GINTSTS = sts;
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:28 +00:00
|
|
|
int usb_drv_port_speed(void)
|
|
|
|
{
|
2010-09-08 06:13:07 +00:00
|
|
|
static const uint8_t speed[4] = {
|
|
|
|
[DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ] = 1,
|
|
|
|
[DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ] = 0,
|
|
|
|
[DSTS_ENUMSPD_FS_PHY_48MHZ] = 0,
|
|
|
|
[DSTS_ENUMSPD_LS_PHY_6MHZ] = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned enumspd = extract(DSTS, enumspd);
|
|
|
|
|
|
|
|
if(enumspd == DSTS_ENUMSPD_LS_PHY_6MHZ)
|
|
|
|
panicf("usb-drv: LS is not supported");
|
|
|
|
|
|
|
|
return speed[enumspd & 3];
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 20:44:26 +00:00
|
|
|
static unsigned long usb_drv_mps_by_type(int type)
|
|
|
|
{
|
2010-09-08 06:13:07 +00:00
|
|
|
static const uint16_t mps[4][2] = {
|
|
|
|
/* type fs hs */
|
|
|
|
[USB_ENDPOINT_XFER_CONTROL] = { 64, 64 },
|
|
|
|
[USB_ENDPOINT_XFER_ISOC] = { 1023, 1024 },
|
|
|
|
[USB_ENDPOINT_XFER_BULK] = { 64, 512 },
|
|
|
|
[USB_ENDPOINT_XFER_INT] = { 64, 1024 },
|
|
|
|
};
|
|
|
|
return mps[type & 3][usb_drv_port_speed() & 1];
|
2010-09-07 20:44:26 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 20:39:28 +00:00
|
|
|
int usb_drv_request_endpoint(int type, int dir)
|
|
|
|
{
|
2010-09-08 04:20:37 +00:00
|
|
|
int ep, ret = -1;
|
|
|
|
unsigned i;
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: request endpoint (type=%d,dir=%s)", type, dir == USB_DIR_IN ? "IN" : "OUT");
|
2010-09-07 20:44:26 +00:00
|
|
|
|
|
|
|
if(dir == USB_DIR_IN)
|
|
|
|
FOR_EACH_IN_EP(i, ep)
|
|
|
|
{
|
|
|
|
if(endpoints[ep][DIR_IN].active)
|
|
|
|
continue;
|
|
|
|
endpoints[ep][DIR_IN].active = true;
|
|
|
|
ret = ep | dir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
FOR_EACH_OUT_EP(i, ep)
|
|
|
|
{
|
|
|
|
if(endpoints[ep][DIR_OUT].active)
|
|
|
|
continue;
|
|
|
|
endpoints[ep][DIR_OUT].active = true;
|
|
|
|
ret = ep | dir;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-07 20:44:26 +00:00
|
|
|
if(ret == -1)
|
|
|
|
{
|
|
|
|
logf("usb-drv: request failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long data = DEPCTL_setd0pid | (type << DEPCTL_eptype_bitp)
|
2010-09-07 21:01:16 +00:00
|
|
|
| (usb_drv_mps_by_type(type) << DEPCTL_mps_bitp)
|
|
|
|
| DEPCTL_usbactep | DEPCTL_snak;
|
2010-09-07 20:44:26 +00:00
|
|
|
unsigned long mask = ~(bitm(DEPCTL, eptype) | bitm(DEPCTL, mps));
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-07 20:44:26 +00:00
|
|
|
if(dir == USB_DIR_IN) DIEPCTL(ep) = (DIEPCTL(ep) & mask) | data;
|
|
|
|
else DOEPCTL(ep) = (DOEPCTL(ep) & mask) | data;
|
|
|
|
|
|
|
|
return ret;
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_release_endpoint(int ep)
|
|
|
|
{
|
2010-09-07 20:44:26 +00:00
|
|
|
logf("usb-drv: release EP%d %s", EP_NUM(ep), EP_DIR(ep) == DIR_IN ? "IN" : "OUT");
|
2010-06-23 22:04:04 +00:00
|
|
|
endpoints[EP_NUM(ep)][EP_DIR(ep)].active = false;
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
2010-06-27 22:06:30 +00:00
|
|
|
void usb_drv_cancel_all_transfers()
|
2010-06-19 20:39:28 +00:00
|
|
|
{
|
2010-06-27 22:06:30 +00:00
|
|
|
cancel_all_transfers(false);
|
2010-06-23 22:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_drv_transfer(int ep, void *ptr, int len, bool dir_in, bool blocking)
|
|
|
|
{
|
|
|
|
ep = EP_NUM(ep);
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-07 20:50:23 +00:00
|
|
|
logf("usb-drv: xfer EP%d, len=%d, dir_in=%d, blocking=%d", ep,
|
|
|
|
len, dir_in, blocking);
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2011-01-27 12:15:46 +00:00
|
|
|
/* disable interrupts to avoid any race */
|
|
|
|
int oldlevel = disable_irq_save();
|
2011-01-24 15:34:14 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
volatile unsigned long *epctl = dir_in ? &DIEPCTL(ep) : &DOEPCTL(ep);
|
|
|
|
volatile unsigned long *eptsiz = dir_in ? &DIEPTSIZ(ep) : &DOEPTSIZ(ep);
|
|
|
|
volatile unsigned long *epdma = dir_in ? &DIEPDMA(ep) : &DOEPDMA(ep);
|
|
|
|
struct usb_endpoint *endpoint = &endpoints[ep][dir_in];
|
|
|
|
#define DEPCTL *epctl
|
|
|
|
#define DEPTSIZ *eptsiz
|
|
|
|
#define DEPDMA *epdma
|
|
|
|
|
2010-06-27 22:06:30 +00:00
|
|
|
if(endpoint->busy)
|
2010-09-07 20:50:23 +00:00
|
|
|
logf("usb-drv: EP%d %s is already busy", ep, dir_in ? "IN" : "OUT");
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
endpoint->busy = true;
|
|
|
|
endpoint->len = len;
|
|
|
|
endpoint->wait = blocking;
|
2010-09-08 03:31:18 +00:00
|
|
|
endpoint->status = -1;
|
2010-09-07 20:44:26 +00:00
|
|
|
|
|
|
|
DEPCTL &= ~DEPCTL_stall;
|
|
|
|
DEPCTL |= DEPCTL_usbactep;
|
|
|
|
|
|
|
|
int mps = usb_drv_mps_by_type(extract(DEPCTL, eptype));
|
2010-06-23 22:04:04 +00:00
|
|
|
int nb_packets = (len + mps - 1) / mps;
|
|
|
|
|
|
|
|
if(len == 0)
|
2010-09-07 18:41:12 +00:00
|
|
|
{
|
|
|
|
DEPDMA = 0x10000000;
|
2010-06-23 22:04:04 +00:00
|
|
|
DEPTSIZ = 1 << DEPTSIZ_pkcnt_bitp;
|
2010-09-07 18:41:12 +00:00
|
|
|
}
|
2010-06-23 22:04:04 +00:00
|
|
|
else
|
2010-09-07 18:41:12 +00:00
|
|
|
{
|
|
|
|
DEPDMA = (unsigned long)AS3525_PHYSICAL_ADDR(ptr);
|
2010-06-23 22:04:04 +00:00
|
|
|
DEPTSIZ = (nb_packets << DEPTSIZ_pkcnt_bitp) | len;
|
2010-09-08 00:29:22 +00:00
|
|
|
if(dir_in)
|
|
|
|
clean_dcache_range(ptr, len);
|
|
|
|
else
|
2010-09-08 01:03:05 +00:00
|
|
|
dump_dcache_range(ptr, len);
|
2010-09-07 18:41:12 +00:00
|
|
|
}
|
2010-06-23 22:04:04 +00:00
|
|
|
|
2010-09-02 13:00:16 +00:00
|
|
|
logf("pkt=%d dma=%lx", nb_packets, DEPDMA);
|
2010-09-08 04:37:41 +00:00
|
|
|
|
2010-09-07 20:44:26 +00:00
|
|
|
DEPCTL |= DEPCTL_epena | DEPCTL_cnak;
|
|
|
|
|
2011-01-27 12:15:46 +00:00
|
|
|
/* restore interrupts */
|
|
|
|
restore_irq(oldlevel);
|
2011-01-24 15:34:14 +00:00
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
if(blocking)
|
2010-09-08 03:31:18 +00:00
|
|
|
{
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_wait(&endpoint->complete, TIMEOUT_BLOCK);
|
2010-09-08 03:31:18 +00:00
|
|
|
return endpoint->status;
|
|
|
|
}
|
|
|
|
|
2010-06-23 22:04:04 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#undef DEPCTL
|
|
|
|
#undef DEPTSIZ
|
|
|
|
#undef DEPDMA
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_drv_recv(int ep, void *ptr, int len)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
return usb_drv_transfer(ep, ptr, len, false, false);
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_drv_send(int ep, void *ptr, int len)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
return usb_drv_transfer(ep, ptr, len, true, true);
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_drv_send_nonblocking(int ep, void *ptr, int len)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
return usb_drv_transfer(ep, ptr, len, true, false);
|
2010-06-19 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-18 09:58:52 +00:00
|
|
|
void usb_drv_set_test_mode(int mode)
|
|
|
|
{
|
2010-09-07 20:44:26 +00:00
|
|
|
/* there is a perfect matching between usb test mode code
|
|
|
|
* and the register field value */
|
|
|
|
DCTL = (DCTL & ~bitm(DCTL, tstctl)) | (mode << DCTL_tstctl_bitp);
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_set_address(int address)
|
|
|
|
{
|
2010-09-07 20:44:26 +00:00
|
|
|
(void) address;
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_stall(int ep, bool stall, bool in)
|
|
|
|
{
|
2010-06-23 22:04:04 +00:00
|
|
|
logf("usb-drv: %sstall EP%d %s", stall ? "" : "un", ep, in ? "IN" : "OUT");
|
2010-09-07 20:44:26 +00:00
|
|
|
if(in)
|
|
|
|
{
|
|
|
|
if(stall) DIEPCTL(ep) |= DEPCTL_stall;
|
|
|
|
else DIEPCTL(ep) &= ~DEPCTL_stall;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(stall) DOEPCTL(ep) |= DEPCTL_stall;
|
|
|
|
else DOEPCTL(ep) &= ~DEPCTL_stall;
|
|
|
|
}
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_drv_stalled(int ep, bool in)
|
|
|
|
{
|
2010-09-08 01:50:36 +00:00
|
|
|
return (in ? DIEPCTL(ep) : DOEPCTL(ep)) & DEPCTL_stall;
|
2010-05-18 09:58:52 +00:00
|
|
|
}
|