2008-08-26 21:48:49 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Maurus Cuelenaere
|
|
|
|
*
|
|
|
|
* 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 "config.h"
|
2009-11-03 16:25:03 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2008-12-20 01:48:46 +00:00
|
|
|
#include "logf.h"
|
2008-08-26 21:48:49 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "usb_ch9.h"
|
|
|
|
#include "usb_drv.h"
|
|
|
|
#include "usb_core.h"
|
2008-11-21 12:50:06 +00:00
|
|
|
#include "usb-target.h"
|
2008-08-26 21:48:49 +00:00
|
|
|
#include "jz4740.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2008-12-31 18:15:24 +00:00
|
|
|
/*
|
|
|
|
The Jz4740 USB controller is called MUSBHSFC in the datasheet.
|
|
|
|
It also seems to be a more generic controller, with support for
|
2010-05-03 19:18:10 +00:00
|
|
|
up to 15 endpoints (the Jz4740 only has 5).
|
2008-12-31 18:15:24 +00:00
|
|
|
*/
|
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
#define EP_BUF_LEFT(ep) ((ep)->length - (ep)->sent)
|
|
|
|
#define EP_PTR(ep) ((void*)((unsigned int)(ep)->buf + (ep)->sent))
|
|
|
|
#define EP_NUMBER(ep) (((int)(ep) - (int)&endpoints[0])/sizeof(struct usb_endpoint))
|
|
|
|
#define EP_NUMBER2(ep) (EP_NUMBER((ep))/2)
|
2008-12-04 18:26:19 +00:00
|
|
|
#define TOTAL_EP() (sizeof(endpoints)/sizeof(struct usb_endpoint))
|
2008-12-31 01:11:04 +00:00
|
|
|
#define EP_IS_IN(ep) (EP_NUMBER((ep))%2)
|
2008-11-05 00:24:46 +00:00
|
|
|
|
|
|
|
enum ep_type
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
ep_control,
|
|
|
|
ep_bulk,
|
|
|
|
ep_interrupt,
|
|
|
|
ep_isochronous
|
2008-11-05 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_endpoint
|
|
|
|
{
|
|
|
|
void *buf;
|
2010-05-03 20:36:34 +00:00
|
|
|
size_t length;
|
2008-12-04 18:26:19 +00:00
|
|
|
union
|
|
|
|
{
|
2010-05-03 20:36:34 +00:00
|
|
|
size_t sent;
|
|
|
|
size_t received;
|
2008-12-04 18:26:19 +00:00
|
|
|
};
|
2009-01-21 20:58:33 +00:00
|
|
|
bool busy;
|
2008-11-05 00:24:46 +00:00
|
|
|
|
|
|
|
const enum ep_type type;
|
|
|
|
const bool use_dma;
|
|
|
|
|
2010-05-03 20:36:34 +00:00
|
|
|
const long fifo_addr;
|
2008-11-05 00:24:46 +00:00
|
|
|
unsigned short fifo_size;
|
2010-05-03 19:18:10 +00:00
|
|
|
|
2010-05-03 20:36:34 +00:00
|
|
|
bool wait;
|
2010-05-03 19:18:10 +00:00
|
|
|
struct wakeup wakeup;
|
2008-11-05 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char ep0_rx_buf[64];
|
|
|
|
static struct usb_endpoint endpoints[] =
|
2009-05-20 12:37:56 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
{ .type = ep_control, .fifo_addr = USB_FIFO_EP0, .fifo_size = 64 },
|
2010-05-03 20:36:34 +00:00
|
|
|
{ .type = ep_control, .fifo_addr = USB_FIFO_EP0, .buf = &ep0_rx_buf },
|
2010-05-03 19:18:10 +00:00
|
|
|
{ .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 },
|
|
|
|
{ .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 },
|
|
|
|
{ .type = ep_interrupt, .fifo_addr = USB_FIFO_EP2, .fifo_size = 64 },
|
2008-11-05 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void select_endpoint(int ep)
|
|
|
|
{
|
|
|
|
REG_USB_REG_INDEX = ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readFIFO(struct usb_endpoint *ep, unsigned int size)
|
2008-12-04 18:26:19 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(EP%d, %d)", __func__, EP_NUMBER2(ep), size);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
register unsigned char *ptr = (unsigned char*)EP_PTR(ep);
|
|
|
|
register unsigned int *ptr32 = (unsigned int*)ptr;
|
2008-12-04 19:44:03 +00:00
|
|
|
register unsigned int s = size >> 2;
|
2008-12-04 18:26:19 +00:00
|
|
|
register unsigned int x;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
if(size > 0)
|
|
|
|
{
|
2009-01-12 18:13:53 +00:00
|
|
|
if( ((unsigned int)ptr & 3) == 0 )
|
2008-12-04 18:26:19 +00:00
|
|
|
{
|
|
|
|
while(s--)
|
|
|
|
*ptr32++ = REG32(ep->fifo_addr);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
ptr = (unsigned char*)ptr32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(s--)
|
|
|
|
{
|
|
|
|
x = REG32(ep->fifo_addr);
|
2010-05-03 19:18:10 +00:00
|
|
|
*ptr++ = x & 0xFF; x >>= 8;
|
|
|
|
*ptr++ = x & 0xFF; x >>= 8;
|
|
|
|
*ptr++ = x & 0xFF; x >>= 8;
|
|
|
|
*ptr++ = x;
|
2008-12-04 18:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
s = size & 3;
|
|
|
|
while(s--)
|
|
|
|
*ptr++ = REG8(ep->fifo_addr);
|
|
|
|
}
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
static void writeFIFO(struct usb_endpoint *ep, size_t size)
|
2008-12-04 18:26:19 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(EP%d, %d)", __func__, EP_NUMBER2(ep), size);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
register unsigned int *d32 = (unsigned int *)EP_PTR(ep);
|
2010-05-03 19:18:10 +00:00
|
|
|
register size_t s = size >> 2;
|
2008-11-05 00:24:46 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(size > 0)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
while (s--)
|
|
|
|
REG32(ep->fifo_addr) = *d32++;
|
|
|
|
|
|
|
|
if( (s = size & 3) )
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
register unsigned char *d8 = (unsigned char *)d32;
|
2008-12-04 18:26:19 +00:00
|
|
|
while (s--)
|
2010-05-03 19:18:10 +00:00
|
|
|
REG8(ep->fifo_addr) = *d8++;
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
static void flushFIFO(struct usb_endpoint *ep)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d)", __func__, EP_NUMBER(ep));
|
2008-12-31 01:11:04 +00:00
|
|
|
|
|
|
|
switch (ep->type)
|
|
|
|
{
|
|
|
|
case ep_control:
|
|
|
|
break;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
case ep_bulk:
|
|
|
|
case ep_interrupt:
|
2010-05-03 19:18:10 +00:00
|
|
|
case ep_isochronous:
|
2008-12-31 01:11:04 +00:00
|
|
|
if(EP_IS_IN(ep))
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_INCSR |= (USB_INCSR_FF | USB_INCSR_CDT);
|
2008-12-31 01:11:04 +00:00
|
|
|
else
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_OUTCSR |= (USB_OUTCSR_FF | USB_OUTCSR_CDT);
|
2008-12-31 01:11:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 20:36:34 +00:00
|
|
|
static inline void ep_transfer_completed(struct usb_endpoint* ep)
|
|
|
|
{
|
|
|
|
ep->sent = 0;
|
|
|
|
ep->length = 0;
|
|
|
|
ep->buf = NULL;
|
|
|
|
ep->busy = false;
|
|
|
|
if(ep->wait)
|
|
|
|
wakeup_signal(&ep->wakeup);
|
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
static void EP0_send(void)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2009-05-20 12:37:56 +00:00
|
|
|
struct usb_endpoint* ep = &endpoints[0];
|
2008-12-31 18:15:24 +00:00
|
|
|
unsigned int length;
|
|
|
|
unsigned char csr0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 18:15:24 +00:00
|
|
|
select_endpoint(0);
|
|
|
|
csr0 = REG_USB_REG_CSR0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
if(ep->sent == 0)
|
2010-05-03 19:18:10 +00:00
|
|
|
length = MIN(ep->length, ep->fifo_size);
|
2008-11-05 00:24:46 +00:00
|
|
|
else
|
2010-05-03 19:18:10 +00:00
|
|
|
length = MIN(EP_BUF_LEFT(ep), ep->fifo_size);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
writeFIFO(ep, length);
|
|
|
|
ep->sent += length;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
if(ep->sent >= ep->length)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2008-12-31 18:15:24 +00:00
|
|
|
REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY | USB_CSR0_DATAEND); /* Set data end! */
|
2010-05-03 20:36:34 +00:00
|
|
|
usb_core_transfer_complete(0, USB_DIR_IN, 0, ep->sent);
|
|
|
|
ep_transfer_completed(ep);
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
2008-12-04 18:26:19 +00:00
|
|
|
else
|
2008-12-31 18:15:24 +00:00
|
|
|
REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY);
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void EP0_handler(void)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s()", __func__);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-12 18:13:53 +00:00
|
|
|
unsigned char csr0;
|
2010-05-03 20:36:34 +00:00
|
|
|
struct usb_endpoint *ep_send = &endpoints[0];
|
|
|
|
struct usb_endpoint *ep_recv = &endpoints[1];
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Read CSR0 */
|
|
|
|
select_endpoint(0);
|
|
|
|
csr0 = REG_USB_REG_CSR0;
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
/* Check for SentStall:
|
|
|
|
This bit is set when a STALL handshake is transmitted. The CPU should clear this bit.
|
2008-11-05 00:24:46 +00:00
|
|
|
*/
|
2009-01-12 18:13:53 +00:00
|
|
|
if(csr0 & USB_CSR0_SENTSTALL)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2008-12-31 18:15:24 +00:00
|
|
|
REG_USB_REG_CSR0 = csr0 & ~USB_CSR0_SENTSTALL;
|
2008-11-05 00:24:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
/* Check for SetupEnd:
|
|
|
|
This bit will be set when a control transaction ends before the DataEnd bit has been set.
|
|
|
|
An interrupt will be generated and the FIFO flushed at this time.
|
|
|
|
The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit.
|
|
|
|
*/
|
2009-01-12 18:13:53 +00:00
|
|
|
if(csr0 & USB_CSR0_SETUPEND)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2008-12-31 18:15:24 +00:00
|
|
|
REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDSETUPEND;
|
2008-11-05 00:24:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Call relevant routines for endpoint 0 state */
|
2010-05-03 20:36:34 +00:00
|
|
|
if(ep_send->busy)
|
|
|
|
EP0_send();
|
|
|
|
else if(csr0 & USB_CSR0_OUTPKTRDY) /* There is a packet in the fifo */
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2010-05-03 20:36:34 +00:00
|
|
|
readFIFO(ep_recv, REG_USB_REG_COUNT0);
|
|
|
|
REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY; /* clear OUTPKTRDY bit */
|
|
|
|
usb_core_control_request((struct usb_ctrlrequest*)ep_recv->buf);
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
static void EPIN_handler(unsigned int endpoint)
|
2009-02-23 16:10:46 +00:00
|
|
|
{
|
2009-05-20 12:37:56 +00:00
|
|
|
struct usb_endpoint* ep = &endpoints[endpoint*2];
|
2008-12-31 01:11:04 +00:00
|
|
|
unsigned int length, csr;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
select_endpoint(endpoint);
|
|
|
|
csr = REG_USB_REG_INCSR;
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d): 0x%x", __func__, endpoint, csr);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
if(!ep->busy)
|
2010-05-03 19:18:10 +00:00
|
|
|
{
|
|
|
|
logf("Entered EPIN handler without work!");
|
2008-12-31 01:11:04 +00:00
|
|
|
return;
|
2010-05-03 19:18:10 +00:00
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(csr & USB_INCSR_SENTSTALL)
|
|
|
|
{
|
|
|
|
REG_USB_REG_INCSR = csr & ~USB_INCSR_SENTSTALL;
|
|
|
|
return;
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(ep->use_dma)
|
2009-01-12 18:13:53 +00:00
|
|
|
return;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-12 18:13:53 +00:00
|
|
|
if(csr & USB_INCSR_FFNOTEMPT)
|
|
|
|
{
|
2009-02-23 16:10:46 +00:00
|
|
|
logf("FIFO is not empty! 0x%x", csr);
|
2009-01-12 18:13:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
logf("EP%d: %d -> %d", endpoint, ep->sent, ep->length);
|
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->sent == 0)
|
2010-05-03 19:18:10 +00:00
|
|
|
length = MIN(ep->length, ep->fifo_size);
|
2008-12-31 01:11:04 +00:00
|
|
|
else
|
2010-05-03 19:18:10 +00:00
|
|
|
length = MIN(EP_BUF_LEFT(ep), ep->fifo_size);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
writeFIFO(ep, length);
|
2009-01-12 18:13:53 +00:00
|
|
|
REG_USB_REG_INCSR = csr | USB_INCSR_INPKTRDY;
|
2008-12-31 01:11:04 +00:00
|
|
|
ep->sent += length;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->sent >= ep->length)
|
2008-12-31 18:15:24 +00:00
|
|
|
{
|
|
|
|
usb_core_transfer_complete(endpoint, USB_DIR_IN, 0, ep->sent);
|
2010-05-03 20:36:34 +00:00
|
|
|
ep_transfer_completed(ep);
|
2009-01-21 20:58:33 +00:00
|
|
|
logf("sent complete");
|
2008-12-31 18:15:24 +00:00
|
|
|
}
|
2008-12-31 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void EPOUT_handler(unsigned int endpoint)
|
|
|
|
{
|
2009-05-20 12:37:56 +00:00
|
|
|
struct usb_endpoint* ep = &endpoints[endpoint*2+1];
|
2008-12-31 01:11:04 +00:00
|
|
|
unsigned int size, csr;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
if(!ep->busy)
|
2010-05-03 19:18:10 +00:00
|
|
|
{
|
|
|
|
logf("Entered EPOUT handler without work!");
|
2008-12-31 01:11:04 +00:00
|
|
|
return;
|
2010-05-03 19:18:10 +00:00
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
select_endpoint(endpoint);
|
|
|
|
while((csr = REG_USB_REG_OUTCSR) & (USB_OUTCSR_SENTSTALL|USB_OUTCSR_OUTPKTRDY))
|
2008-12-31 18:15:24 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d): 0x%x", __func__, endpoint, csr);
|
2009-02-23 16:10:46 +00:00
|
|
|
if(csr & USB_OUTCSR_SENTSTALL)
|
|
|
|
{
|
|
|
|
logf("stall sent, flushing fifo..");
|
|
|
|
flushFIFO(ep);
|
|
|
|
REG_USB_REG_OUTCSR = csr & ~USB_OUTCSR_SENTSTALL;
|
|
|
|
return;
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(ep->use_dma)
|
|
|
|
return;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(csr & USB_OUTCSR_OUTPKTRDY) /* There is a packet in the fifo */
|
|
|
|
{
|
|
|
|
size = REG_USB_REG_OUTCOUNT;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
readFIFO(ep, size);
|
|
|
|
ep->received += size;
|
|
|
|
|
|
|
|
/*if(csr & USB_OUTCSR_FFFULL)
|
|
|
|
csr &= ~USB_OUTCSR_FFFULL;*/
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_OUTCSR = csr & ~USB_OUTCSR_OUTPKTRDY;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
logf("received: %d max length: %d", ep->received, ep->length);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(size < ep->fifo_size || ep->received >= ep->length)
|
|
|
|
{
|
|
|
|
usb_core_transfer_complete(endpoint, USB_DIR_OUT, 0, ep->received);
|
2010-05-03 20:36:34 +00:00
|
|
|
ep_transfer_completed(ep);
|
2009-02-23 16:10:46 +00:00
|
|
|
logf("receive transfer_complete");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-21 20:58:33 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
static void EPDMA_handler(int number)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
int endpoint = -1;
|
|
|
|
unsigned int size = 0;
|
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(number == USB_INTR_DMA_BULKIN)
|
|
|
|
endpoint = (REG_USB_REG_CNTL1 >> 4) & 0xF;
|
|
|
|
else if(number == USB_INTR_DMA_BULKOUT)
|
|
|
|
endpoint = (REG_USB_REG_CNTL2 >> 4) & 0xF;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
struct usb_endpoint* ep = &endpoints[endpoint];
|
|
|
|
logf("DMA_BULK%d %d", number, endpoint);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(number == USB_INTR_DMA_BULKIN)
|
|
|
|
size = (unsigned int)ep->buf - REG_USB_REG_ADDR1;
|
|
|
|
else if(number == USB_INTR_DMA_BULKOUT)
|
|
|
|
size = (unsigned int)ep->buf - REG_USB_REG_ADDR2;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
if(number == USB_INTR_DMA_BULKOUT)
|
|
|
|
{
|
|
|
|
/* Disable DMA */
|
|
|
|
REG_USB_REG_CNTL2 = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
__dcache_invalidate_all();
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
select_endpoint(endpoint);
|
|
|
|
/* Read out last packet manually */
|
|
|
|
unsigned int lpack_size = REG_USB_REG_OUTCOUNT;
|
|
|
|
if(lpack_size > 0)
|
2009-01-21 20:58:33 +00:00
|
|
|
{
|
2009-02-23 16:10:46 +00:00
|
|
|
ep->buf += ep->length - lpack_size;
|
|
|
|
readFIFO(ep, lpack_size);
|
|
|
|
REG_USB_REG_OUTCSR &= ~USB_OUTCSR_OUTPKTRDY;
|
2009-01-21 20:58:33 +00:00
|
|
|
}
|
2008-12-31 18:15:24 +00:00
|
|
|
}
|
2009-02-23 16:10:46 +00:00
|
|
|
else if(number == USB_INTR_DMA_BULKIN && size % ep->fifo_size)
|
2010-05-03 19:18:10 +00:00
|
|
|
{
|
2009-02-23 16:10:46 +00:00
|
|
|
/* If the last packet is less than MAXP, set INPKTRDY manually */
|
|
|
|
REG_USB_REG_INCSR |= USB_INCSR_INPKTRDY;
|
2010-05-03 19:18:10 +00:00
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
usb_core_transfer_complete(endpoint, EP_IS_IN(ep) ? USB_DIR_IN : USB_DIR_OUT,
|
|
|
|
0, ep->length);
|
2010-05-03 20:36:34 +00:00
|
|
|
ep_transfer_completed(ep);
|
2008-12-31 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
static void setup_endpoint(struct usb_endpoint *ep)
|
|
|
|
{
|
2009-02-23 16:10:46 +00:00
|
|
|
int csr, csrh;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
select_endpoint(EP_NUMBER2(ep));
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
ep->busy = false;
|
|
|
|
ep->wait = false;
|
2009-02-23 16:10:46 +00:00
|
|
|
ep->sent = 0;
|
|
|
|
ep->length = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->type == ep_bulk)
|
|
|
|
{
|
|
|
|
if(REG_USB_REG_POWER & USB_POWER_HSMODE)
|
|
|
|
ep->fifo_size = 512;
|
|
|
|
else
|
|
|
|
ep->fifo_size = 64;
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
if(EP_IS_IN(ep))
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2009-02-23 16:10:46 +00:00
|
|
|
csr = (USB_INCSR_FF | USB_INCSR_CDT);
|
|
|
|
csrh = USB_INCSRH_MODE;
|
2010-05-03 19:18:10 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->use_dma)
|
2009-02-23 16:10:46 +00:00
|
|
|
csrh |= (USB_INCSRH_DMAREQENAB | USB_INCSRH_AUTOSET | USB_INCSRH_DMAREQMODE);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-05-17 15:41:05 +00:00
|
|
|
if(ep->type == ep_interrupt)
|
|
|
|
csrh |= USB_INCSRH_FRCDATATOG;
|
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
REG_USB_REG_INMAXP = ep->fifo_size;
|
|
|
|
REG_USB_REG_INCSR = csr;
|
|
|
|
REG_USB_REG_INCSRH = csrh;
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_INTRINE |= USB_INTR_EP(EP_NUMBER2(ep));
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-23 16:10:46 +00:00
|
|
|
{
|
2008-12-31 01:11:04 +00:00
|
|
|
csr = (USB_OUTCSR_FF | USB_OUTCSR_CDT);
|
2009-02-23 16:10:46 +00:00
|
|
|
csrh = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->type == ep_interrupt)
|
2009-02-23 16:10:46 +00:00
|
|
|
csrh |= USB_OUTCSRH_DNYT;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
if(ep->use_dma)
|
2009-02-23 16:10:46 +00:00
|
|
|
csrh |= (USB_OUTCSRH_DMAREQENAB | USB_OUTCSRH_AUTOCLR | USB_OUTCSRH_DMAREQMODE);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
REG_USB_REG_OUTMAXP = ep->fifo_size;
|
|
|
|
REG_USB_REG_OUTCSR = csr;
|
|
|
|
REG_USB_REG_OUTCSRH = csrh;
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_INTROUTE |= USB_INTR_EP(EP_NUMBER2(ep));
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void udc_reset(void)
|
|
|
|
{
|
2008-12-31 18:15:24 +00:00
|
|
|
/* From the datasheet:
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
When a reset condition is detected on the USB, the controller performs the following actions:
|
|
|
|
* Sets FAddr to 0.
|
|
|
|
* Sets Index to 0.
|
|
|
|
* Flushes all endpoint FIFOs.
|
|
|
|
* Clears all control/status registers.
|
|
|
|
* Enables all endpoint interrupts.
|
|
|
|
* Generates a Reset interrupt.
|
2008-12-31 18:15:24 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s()", __func__);
|
2008-12-04 18:26:19 +00:00
|
|
|
|
2009-01-12 18:13:53 +00:00
|
|
|
unsigned int i;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Disable interrupts */
|
2008-12-04 18:26:19 +00:00
|
|
|
REG_USB_REG_INTRINE = 0;
|
2008-11-05 00:24:46 +00:00
|
|
|
REG_USB_REG_INTROUTE = 0;
|
|
|
|
REG_USB_REG_INTRUSBE = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
/* Disable DMA */
|
|
|
|
REG_USB_REG_CNTL1 = 0;
|
|
|
|
REG_USB_REG_CNTL2 = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-12 18:13:53 +00:00
|
|
|
/* High speed, softconnect and suspend/resume */
|
|
|
|
REG_USB_REG_POWER = (USB_POWER_SOFTCONN | USB_POWER_HSENAB | USB_POWER_SUSPENDM);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
/* Reset EP0 */
|
2008-11-05 00:24:46 +00:00
|
|
|
select_endpoint(0);
|
2008-12-04 18:26:19 +00:00
|
|
|
REG_USB_REG_CSR0 = (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_SVDSETUPEND);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
/* Reset other endpoints */
|
|
|
|
for(i=2; i<TOTAL_EP(); i++)
|
2008-11-05 00:24:46 +00:00
|
|
|
setup_endpoint(&endpoints[i]);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
/* Enable interrupts */
|
2010-05-03 19:18:10 +00:00
|
|
|
REG_USB_REG_INTRINE |= USB_INTR_EP0;
|
2008-12-04 18:26:19 +00:00
|
|
|
REG_USB_REG_INTRUSBE |= USB_INTR_RESET;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
usb_core_bus_reset();
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Interrupt handler */
|
|
|
|
void UDC(void)
|
|
|
|
{
|
|
|
|
/* Read interrupt registers */
|
2009-01-12 18:13:53 +00:00
|
|
|
unsigned char intrUSB = REG_USB_REG_INTRUSB & 0x07; /* Mask SOF */
|
|
|
|
unsigned short intrIn = REG_USB_REG_INTRIN;
|
|
|
|
unsigned short intrOut = REG_USB_REG_INTROUT;
|
|
|
|
unsigned char intrDMA = REG_USB_REG_INTR;
|
2008-11-05 00:24:46 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
logf("%x %x %x %x", intrUSB, intrIn, intrOut, intrDMA);
|
2008-11-05 00:24:46 +00:00
|
|
|
|
|
|
|
/* EPIN & EPOUT are all handled in DMA */
|
|
|
|
if(intrIn & USB_INTR_EP0)
|
|
|
|
EP0_handler();
|
2008-12-31 01:11:04 +00:00
|
|
|
if(intrIn & USB_INTR_INEP1)
|
|
|
|
EPIN_handler(1);
|
|
|
|
if(intrIn & USB_INTR_INEP2)
|
|
|
|
EPIN_handler(2);
|
|
|
|
if(intrOut & USB_INTR_OUTEP1)
|
|
|
|
EPOUT_handler(1);
|
|
|
|
if(intrOut & USB_INTR_OUTEP2)
|
|
|
|
EPOUT_handler(2);
|
2008-11-05 00:24:46 +00:00
|
|
|
if(intrUSB & USB_INTR_RESET)
|
|
|
|
udc_reset();
|
2009-01-12 18:13:53 +00:00
|
|
|
if(intrUSB & USB_INTR_SUSPEND)
|
|
|
|
{
|
|
|
|
logf("USB suspend");
|
|
|
|
}
|
|
|
|
if(intrUSB & USB_INTR_RESUME)
|
|
|
|
{
|
|
|
|
logf("USB resume");
|
|
|
|
}
|
2008-11-05 00:24:46 +00:00
|
|
|
if(intrDMA & USB_INTR_DMA_BULKIN)
|
2009-02-23 16:10:46 +00:00
|
|
|
EPDMA_handler(USB_INTR_DMA_BULKIN);
|
2008-11-05 00:24:46 +00:00
|
|
|
if(intrDMA & USB_INTR_DMA_BULKOUT)
|
2009-02-23 16:10:46 +00:00
|
|
|
EPDMA_handler(USB_INTR_DMA_BULKOUT);
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_drv_stalled(int endpoint, bool in)
|
|
|
|
{
|
2009-01-21 20:58:33 +00:00
|
|
|
endpoint &= 0x7F;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d, %s)", __func__, endpoint, in?"IN":"OUT");
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
select_endpoint(endpoint);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
if(endpoint == EP_CONTROL)
|
2008-11-05 00:24:46 +00:00
|
|
|
return (REG_USB_REG_CSR0 & USB_CSR0_SENDSTALL) != 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(in)
|
|
|
|
return (REG_USB_REG_INCSR & USB_INCSR_SENDSTALL) != 0;
|
|
|
|
else
|
|
|
|
return (REG_USB_REG_OUTCSR & USB_OUTCSR_SENDSTALL) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_stall(int endpoint, bool stall, bool in)
|
|
|
|
{
|
2009-01-21 20:58:33 +00:00
|
|
|
endpoint &= 0x7F;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d,%s,%s)", __func__, endpoint, stall?"Y":"N", in?"IN":"OUT");
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
select_endpoint(endpoint);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 19:44:03 +00:00
|
|
|
if(endpoint == EP_CONTROL)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
|
|
|
if(stall)
|
|
|
|
REG_USB_REG_CSR0 |= USB_CSR0_SENDSTALL;
|
|
|
|
else
|
|
|
|
REG_USB_REG_CSR0 &= ~USB_CSR0_SENDSTALL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(in)
|
|
|
|
{
|
|
|
|
if(stall)
|
|
|
|
REG_USB_REG_INCSR |= USB_INCSR_SENDSTALL;
|
|
|
|
else
|
2008-12-31 01:11:04 +00:00
|
|
|
REG_USB_REG_INCSR = (REG_USB_REG_INCSR & ~USB_INCSR_SENDSTALL) | USB_INCSR_CDT;
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(stall)
|
|
|
|
REG_USB_REG_OUTCSR |= USB_OUTCSR_SENDSTALL;
|
|
|
|
else
|
2008-12-31 01:11:04 +00:00
|
|
|
REG_USB_REG_OUTCSR = (REG_USB_REG_OUTCSR & ~USB_OUTCSR_SENDSTALL) | USB_OUTCSR_CDT;
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
bool usb_drv_connected(void)
|
|
|
|
{
|
2008-12-04 18:26:19 +00:00
|
|
|
return USB_DRV_CONNECTED();
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_detect(void)
|
|
|
|
{
|
2008-12-04 18:26:19 +00:00
|
|
|
return usb_drv_connected() ? USB_INSERTED : USB_EXTRACTED;
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init_device(void)
|
|
|
|
{
|
2009-01-21 20:58:33 +00:00
|
|
|
unsigned int i;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
USB_INIT_GPIO();
|
2009-01-21 20:58:33 +00:00
|
|
|
#ifdef USB_GPIO_IRQ
|
|
|
|
system_enable_irq(IRQ_GPIO_UDC_DETE);
|
|
|
|
#endif
|
2008-12-04 18:26:19 +00:00
|
|
|
system_enable_irq(IRQ_UDC);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
for(i=0; i<TOTAL_EP(); i++)
|
2010-05-03 19:18:10 +00:00
|
|
|
wakeup_init(&endpoints[i].wakeup);
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
#ifdef USB_GPIO_IRQ
|
2009-05-20 21:59:51 +00:00
|
|
|
static unsigned long last_tick;
|
2009-01-21 20:58:33 +00:00
|
|
|
void USB_GPIO_IRQ(void)
|
|
|
|
{
|
2009-05-20 21:59:51 +00:00
|
|
|
/* Prevent enabled-disabled bouncing */
|
|
|
|
if(current_tick - last_tick > HZ/16)
|
|
|
|
{
|
|
|
|
usb_status_event(usb_detect());
|
|
|
|
last_tick = current_tick;
|
|
|
|
}
|
2009-01-21 20:58:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
void usb_enable(bool on)
|
|
|
|
{
|
|
|
|
if(on)
|
|
|
|
usb_core_init();
|
|
|
|
else
|
|
|
|
usb_core_exit();
|
|
|
|
}
|
|
|
|
|
2009-01-21 20:58:33 +00:00
|
|
|
void usb_attach(void)
|
|
|
|
{
|
|
|
|
usb_enable(true);
|
|
|
|
}
|
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
void usb_drv_init(void)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s()", __func__);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Set this bit to allow the UDC entering low-power mode when
|
|
|
|
* there are no actions on the USB bus.
|
|
|
|
* UDC still works during this bit was set.
|
|
|
|
*/
|
|
|
|
//__cpm_stop_udc();
|
2008-08-26 21:48:49 +00:00
|
|
|
__cpm_start_udc();
|
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Enable the USB PHY */
|
|
|
|
REG_CPM_SCR |= CPM_SCR_USBPHY_ENABLE;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
/* Dis- and reconnect from USB */
|
|
|
|
REG_USB_REG_POWER &= ~USB_POWER_SOFTCONN;
|
|
|
|
REG_USB_REG_POWER |= USB_POWER_SOFTCONN;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
udc_reset();
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_exit(void)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s()", __func__);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-11-05 00:24:46 +00:00
|
|
|
/* Disable interrupts */
|
2010-05-03 19:18:10 +00:00
|
|
|
REG_USB_REG_INTRINE = 0;
|
2008-11-05 00:24:46 +00:00
|
|
|
REG_USB_REG_INTROUTE = 0;
|
|
|
|
REG_USB_REG_INTRUSBE = 0;
|
|
|
|
|
|
|
|
/* Disable DMA */
|
|
|
|
REG_USB_REG_CNTL1 = 0;
|
|
|
|
REG_USB_REG_CNTL2 = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
/* Disconnect from USB */
|
2008-08-26 21:48:49 +00:00
|
|
|
REG_USB_REG_POWER &= ~USB_POWER_SOFTCONN;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
/* Disable the USB PHY */
|
|
|
|
REG_CPM_SCR &= ~CPM_SCR_USBPHY_ENABLE;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
__cpm_stop_udc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_set_address(int address)
|
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d)", __func__, address);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-08-26 21:48:49 +00:00
|
|
|
REG_USB_REG_FADDR = address;
|
|
|
|
}
|
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length, bool blocking)
|
2008-11-21 12:50:06 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
if(ep->type == ep_control && ptr == NULL && length == 0)
|
2010-05-03 20:36:34 +00:00
|
|
|
return; /* ACK request, handled in the ISR */
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
int flags = disable_irq_save();
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
ep->buf = ptr;
|
|
|
|
ep->sent = 0;
|
|
|
|
ep->length = length;
|
|
|
|
ep->busy = true;
|
|
|
|
if(blocking)
|
|
|
|
ep->wait = true;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
if(ep->type == ep_control)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2008-12-04 18:26:19 +00:00
|
|
|
EP0_send();
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
2009-01-12 18:13:53 +00:00
|
|
|
else
|
2008-12-31 01:11:04 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
if(ep->use_dma)
|
2009-02-23 16:10:46 +00:00
|
|
|
{
|
|
|
|
//dma_cache_wback_inv((unsigned long)ptr, length);
|
|
|
|
__dcache_writeback_all();
|
2009-02-26 21:15:40 +00:00
|
|
|
REG_USB_REG_ADDR1 = PHYSADDR((unsigned long)ptr);
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_COUNT1 = length;
|
|
|
|
REG_USB_REG_CNTL1 = (USB_CNTL_INTR_EN | USB_CNTL_MODE_1 |
|
|
|
|
USB_CNTL_DIR_IN | USB_CNTL_ENA |
|
2010-05-03 19:18:10 +00:00
|
|
|
USB_CNTL_EP(EP_NUMBER2(ep)) | USB_CNTL_BURST_16);
|
2009-02-23 16:10:46 +00:00
|
|
|
}
|
|
|
|
else
|
2010-05-03 19:18:10 +00:00
|
|
|
EPIN_handler(EP_NUMBER2(ep));
|
|
|
|
}
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
restore_irq(flags);
|
|
|
|
|
|
|
|
if(blocking)
|
|
|
|
{
|
|
|
|
wakeup_wait(&ep->wakeup, TIMEOUT_BLOCK);
|
|
|
|
ep->wait = false;
|
2008-12-31 01:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
int usb_drv_send_nonblocking(int endpoint, void* ptr, int length)
|
2008-12-31 01:11:04 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
usb_drv_send_internal(&endpoints[(endpoint & 0x7F)*2], ptr, length, false);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_drv_send(int endpoint, void* ptr, int length)
|
|
|
|
{
|
|
|
|
logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
|
2009-01-21 20:58:33 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
usb_drv_send_internal(&endpoints[(endpoint & 0x7F)*2], ptr, length, true);
|
|
|
|
|
|
|
|
return 0;
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
int usb_drv_recv(int endpoint, void* ptr, int length)
|
2008-08-26 21:48:49 +00:00
|
|
|
{
|
2008-12-31 01:11:04 +00:00
|
|
|
int flags;
|
2010-05-03 19:18:10 +00:00
|
|
|
struct usb_endpoint *ep;
|
2008-12-31 01:11:04 +00:00
|
|
|
endpoint &= 0x7F;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
|
2008-08-26 21:48:49 +00:00
|
|
|
|
2010-05-03 20:36:34 +00:00
|
|
|
if(endpoint == EP_CONTROL)
|
|
|
|
return 0; /* all EP0 OUT transactions are handled within the ISR */
|
2009-01-12 18:13:53 +00:00
|
|
|
else
|
2008-12-31 01:11:04 +00:00
|
|
|
{
|
|
|
|
flags = disable_irq_save();
|
2010-05-03 19:18:10 +00:00
|
|
|
ep = &endpoints[endpoint*2+1];
|
|
|
|
|
|
|
|
ep->buf = ptr;
|
|
|
|
ep->received = 0;
|
|
|
|
ep->length = length;
|
|
|
|
ep->busy = true;
|
|
|
|
if(ep->use_dma)
|
2009-02-23 16:10:46 +00:00
|
|
|
{
|
|
|
|
//dma_cache_wback_inv((unsigned long)ptr, length);
|
|
|
|
__dcache_writeback_all();
|
2009-02-26 21:15:40 +00:00
|
|
|
REG_USB_REG_ADDR2 = PHYSADDR((unsigned long)ptr);
|
2009-02-23 16:10:46 +00:00
|
|
|
REG_USB_REG_COUNT2 = length;
|
|
|
|
REG_USB_REG_CNTL2 = (USB_CNTL_INTR_EN | USB_CNTL_MODE_1 |
|
|
|
|
USB_CNTL_ENA | USB_CNTL_EP(endpoint) |
|
|
|
|
USB_CNTL_BURST_16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
EPOUT_handler(endpoint);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2009-02-23 16:10:46 +00:00
|
|
|
restore_irq(flags);
|
2008-12-04 18:26:19 +00:00
|
|
|
return 0;
|
2008-12-31 01:11:04 +00:00
|
|
|
}
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
void usb_drv_set_test_mode(int mode)
|
2008-08-26 21:48:49 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d)", __func__, mode);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
switch(mode)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2008-12-04 18:26:19 +00:00
|
|
|
case 0:
|
|
|
|
REG_USB_REG_TESTMODE &= ~USB_TEST_ALL;
|
2008-11-05 00:24:46 +00:00
|
|
|
break;
|
2008-12-04 18:26:19 +00:00
|
|
|
case 1:
|
|
|
|
REG_USB_REG_TESTMODE |= USB_TEST_J;
|
2008-11-05 00:24:46 +00:00
|
|
|
break;
|
2008-12-04 18:26:19 +00:00
|
|
|
case 2:
|
|
|
|
REG_USB_REG_TESTMODE |= USB_TEST_K;
|
2008-09-17 21:44:47 +00:00
|
|
|
break;
|
2008-12-04 18:26:19 +00:00
|
|
|
case 3:
|
|
|
|
REG_USB_REG_TESTMODE |= USB_TEST_SE0NAK;
|
2008-11-05 00:24:46 +00:00
|
|
|
break;
|
2008-12-04 18:26:19 +00:00
|
|
|
case 4:
|
|
|
|
REG_USB_REG_TESTMODE |= USB_TEST_PACKET;
|
2008-09-14 16:26:08 +00:00
|
|
|
break;
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
int usb_drv_port_speed(void)
|
2008-08-26 21:48:49 +00:00
|
|
|
{
|
2009-01-12 18:13:53 +00:00
|
|
|
return (REG_USB_REG_POWER & USB_POWER_HSMODE) ? 1 : 0;
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
void usb_drv_cancel_all_transfers(void)
|
2008-08-26 21:48:49 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s()", __func__);
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
unsigned int i, flags;
|
|
|
|
flags = disable_irq_save();
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
for(i=0; i<TOTAL_EP(); i++)
|
2008-11-05 00:24:46 +00:00
|
|
|
{
|
2010-05-03 20:36:34 +00:00
|
|
|
if(i != 1) /* ep0 out needs special handling */
|
2008-12-31 18:15:24 +00:00
|
|
|
endpoints[i].buf = NULL;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
endpoints[i].sent = 0;
|
|
|
|
endpoints[i].length = 0;
|
2009-05-20 12:37:56 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
select_endpoint(i/2);
|
|
|
|
flushFIFO(&endpoints[i]);
|
2008-11-05 00:24:46 +00:00
|
|
|
}
|
2008-12-31 01:11:04 +00:00
|
|
|
restore_irq(flags);
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 18:26:19 +00:00
|
|
|
void usb_drv_release_endpoint(int ep)
|
2008-08-26 21:48:49 +00:00
|
|
|
{
|
2008-12-04 18:26:19 +00:00
|
|
|
(void)ep;
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d, %s)", __func__, (ep & 0x7F), (ep >> 7) ? "IN" : "OUT");
|
2008-08-26 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
int usb_drv_request_endpoint(int type, int dir)
|
2008-09-14 16:26:08 +00:00
|
|
|
{
|
2010-05-03 19:18:10 +00:00
|
|
|
logf("%s(%d, %s)", __func__, type, (dir == USB_DIR_IN) ? "IN" : "OUT");
|
2009-05-16 15:30:09 +00:00
|
|
|
|
2009-05-16 23:04:54 +00:00
|
|
|
dir &= USB_ENDPOINT_DIR_MASK;
|
|
|
|
type &= USB_ENDPOINT_XFERTYPE_MASK;
|
2009-05-16 15:30:09 +00:00
|
|
|
|
2008-12-31 01:11:04 +00:00
|
|
|
/* There are only 3+2 endpoints, so hardcode this ... */
|
2009-05-16 23:04:54 +00:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case USB_ENDPOINT_XFER_BULK:
|
2009-05-17 15:41:05 +00:00
|
|
|
if(dir == USB_DIR_IN)
|
2009-05-20 12:37:56 +00:00
|
|
|
return (1 | USB_DIR_IN);
|
2009-05-17 15:41:05 +00:00
|
|
|
else
|
|
|
|
return (1 | USB_DIR_OUT);
|
2009-05-16 23:04:54 +00:00
|
|
|
|
|
|
|
case USB_ENDPOINT_XFER_INT:
|
|
|
|
if(dir == USB_DIR_IN)
|
2009-05-20 12:37:56 +00:00
|
|
|
return (2 | USB_DIR_IN);
|
2009-05-16 23:04:54 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-14 16:26:08 +00:00
|
|
|
}
|