2006-10-05 10:58:51 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
|
|
|
* iPod driver based on code from the ipodlinux project - http://ipodlinux.org
|
|
|
|
* Adapted for Rockbox in January 2006
|
|
|
|
* Original file: podzilla/usb.c
|
|
|
|
* Copyright (C) 2005 Adam Johnston
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "usb.h"
|
2007-08-27 16:26:56 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "ata.h"
|
2007-08-27 16:30:23 +00:00
|
|
|
#include "string.h"
|
2007-08-27 21:59:57 +00:00
|
|
|
#include "arcotg_udc.h"
|
2007-02-04 21:46:01 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
#include "usbstack.h"
|
|
|
|
#endif
|
|
|
|
|
2006-10-05 10:58:51 +00:00
|
|
|
void usb_init_device(void)
|
|
|
|
{
|
2007-09-05 15:52:49 +00:00
|
|
|
int r0;
|
2007-11-05 17:19:00 +00:00
|
|
|
GPO32_ENABLE |= 0x200;
|
2007-09-05 15:52:49 +00:00
|
|
|
|
2007-08-27 18:21:05 +00:00
|
|
|
outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
|
2007-09-05 15:52:49 +00:00
|
|
|
DEV_EN |= DEV_USB;
|
|
|
|
|
2007-08-27 18:21:05 +00:00
|
|
|
DEV_RS |= DEV_USB; /* reset usb start */
|
|
|
|
DEV_RS &=~DEV_USB;/* reset usb end */
|
2007-09-05 15:52:49 +00:00
|
|
|
|
2007-10-16 10:48:16 +00:00
|
|
|
DEV_INIT2 |= INIT_USB;
|
2007-09-05 15:52:49 +00:00
|
|
|
while ((inl(0x70000028) & 0x80) == 0);
|
|
|
|
|
2007-08-27 18:21:05 +00:00
|
|
|
UDC_PORTSC1 |= PORTSCX_PORT_RESET;
|
2007-09-05 15:52:49 +00:00
|
|
|
while ((UDC_PORTSC1 & PORTSCX_PORT_RESET) != 0);
|
|
|
|
|
|
|
|
UDC_OTGSC |= 0x5F000000;
|
2007-08-27 18:21:05 +00:00
|
|
|
if( (UDC_OTGSC & 0x100) == 0) {
|
2007-09-05 15:52:49 +00:00
|
|
|
UDC_USBMODE &=~ USB_MODE_CTRL_MODE_HOST;
|
|
|
|
UDC_USBMODE |= USB_MODE_CTRL_MODE_DEVICE;
|
|
|
|
outl(inl(0x70000028) | 0x4000, 0x70000028);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
} else {
|
|
|
|
UDC_USBMODE |= USB_MODE_CTRL_MODE_DEVICE;
|
|
|
|
outl(inl(0x70000028) &~0x4000, 0x70000028);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UDC_USBCMD |= USB_CMD_CTRL_RESET;
|
|
|
|
while((UDC_USBCMD & USB_CMD_CTRL_RESET) != 0);
|
|
|
|
|
|
|
|
r0 = UDC_PORTSC1;
|
|
|
|
|
2007-08-27 18:21:05 +00:00
|
|
|
/* Note from IPL source (referring to next 5 lines of code:
|
|
|
|
THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
|
2007-10-16 10:48:16 +00:00
|
|
|
DEV_INIT2 |= INIT_USB;
|
2007-09-05 15:52:49 +00:00
|
|
|
DEV_EN |= DEV_USB;
|
|
|
|
while ((inl(0x70000028) & 0x80) == 0);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
|
|
|
|
udelay(0x186A0);
|
|
|
|
|
|
|
|
#ifndef HAVE_USBSTACK
|
2007-08-27 18:21:05 +00:00
|
|
|
dr_controller_setup();
|
|
|
|
#endif
|
2007-08-01 22:44:02 +00:00
|
|
|
|
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
/* GPIO C bit 1 is firewire detect */
|
|
|
|
GPIOC_ENABLE |= 0x02;
|
|
|
|
GPIOC_OUTPUT_EN &= ~0x02;
|
|
|
|
#endif
|
2006-10-05 10:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_enable(bool on)
|
|
|
|
{
|
2007-08-28 21:32:28 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2007-09-05 16:16:49 +00:00
|
|
|
if (!on) {
|
|
|
|
usb_stack_stop();
|
|
|
|
}
|
2007-09-05 15:52:49 +00:00
|
|
|
#else
|
2006-12-20 15:28:32 +00:00
|
|
|
/* This device specific code will eventually give way to proper USB
|
2007-08-01 22:44:02 +00:00
|
|
|
handling, which should be the same for all PP502x targets. */
|
2006-10-05 10:58:51 +00:00
|
|
|
if (on)
|
|
|
|
{
|
2007-09-27 02:50:50 +00:00
|
|
|
#if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined (IRIVER_H10_5GB) ||\
|
|
|
|
defined(SANSA_C200)
|
2006-12-20 15:28:32 +00:00
|
|
|
/* For the H10 and iPod, we can only do one thing with USB mode - reboot
|
|
|
|
into the flash-based disk-mode. This does not return. */
|
|
|
|
|
|
|
|
#if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
|
|
|
|
if(button_status()==BUTTON_RIGHT)
|
2007-08-28 21:32:28 +00:00
|
|
|
#endif /* defined(IRIVER_H10) || defined (IRIVER_H10_5GB) */
|
2006-12-20 15:28:32 +00:00
|
|
|
{
|
2007-09-28 07:56:46 +00:00
|
|
|
#ifndef HAVE_FLASH_STORAGE
|
2006-12-20 15:28:32 +00:00
|
|
|
ata_sleepnow(); /* Immediately spindown the disk. */
|
|
|
|
sleep(HZ*2);
|
2007-09-28 07:56:46 +00:00
|
|
|
#endif
|
2007-08-01 22:44:02 +00:00
|
|
|
|
|
|
|
#ifdef IPOD_ARCH /* The following code is based on ipodlinux */
|
|
|
|
#if CONFIG_CPU == PP5020
|
|
|
|
memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
|
|
|
|
#elif CONFIG_CPU == PP5022
|
|
|
|
memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
|
2007-08-28 21:32:28 +00:00
|
|
|
#endif /* CONFIG_CPU */
|
|
|
|
#endif /* IPOD_ARCH */
|
2007-08-01 22:44:02 +00:00
|
|
|
|
2006-12-20 15:28:32 +00:00
|
|
|
system_reboot(); /* Reboot */
|
|
|
|
}
|
2007-08-28 21:32:28 +00:00
|
|
|
#endif /*defined(IPOD_ARCH) || defined(IRIVER_H10) || defined (IRIVER_H10_5GB)*/
|
2006-10-05 10:58:51 +00:00
|
|
|
}
|
2007-08-28 21:32:28 +00:00
|
|
|
#endif /* !HAVE_USBSTACK */
|
2006-10-05 10:58:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
int usb_detect(void)
|
2006-10-05 10:58:51 +00:00
|
|
|
{
|
2007-09-04 08:03:07 +00:00
|
|
|
static int countdown = 0;
|
|
|
|
static int status = USB_EXTRACTED;
|
2007-02-04 21:46:01 +00:00
|
|
|
static bool prev_usbstatus1 = false;
|
2007-09-04 08:03:07 +00:00
|
|
|
bool usbstatus1, usbstatus2;
|
2006-10-05 10:58:51 +00:00
|
|
|
|
2007-08-01 22:44:02 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
/* GPIO C bit 1 is firewire detect */
|
|
|
|
if (!(GPIOC_INPUT_VAL & 0x02))
|
2007-09-04 08:03:07 +00:00
|
|
|
return USB_INSERTED;
|
2007-08-01 22:44:02 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
if (countdown > 0)
|
|
|
|
{
|
|
|
|
countdown--;
|
|
|
|
|
|
|
|
usbstatus2 = (UDC_PORTSC1 & PORTSCX_CURRENT_CONNECT_STATUS) ? true : false;
|
|
|
|
if ((countdown == 0) || usbstatus2)
|
|
|
|
{
|
|
|
|
countdown = 0;
|
|
|
|
status = usbstatus2 ? USB_INSERTED : USB_POWERED;
|
2007-09-04 23:29:42 +00:00
|
|
|
#ifndef HAVE_USBSTACK
|
2007-09-04 08:03:07 +00:00
|
|
|
dr_controller_stop();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-02-16 22:13:21 +00:00
|
|
|
/* UDC_ID should have the bit format:
|
2006-12-20 15:28:32 +00:00
|
|
|
[31:24] = 0x0
|
|
|
|
[23:16] = 0x22 (Revision number)
|
|
|
|
[15:14] = 0x3 (Reserved)
|
|
|
|
[13:8] = 0x3a (NID - 1's compliment of ID)
|
|
|
|
[7:6] = 0x0 (Reserved)
|
|
|
|
[5:0] = 0x05 (ID) */
|
2007-02-16 22:13:21 +00:00
|
|
|
if (UDC_ID != 0x22FA05) {
|
2007-09-04 08:03:07 +00:00
|
|
|
/* This should never occur - do we even need to test? */
|
|
|
|
return USB_EXTRACTED;
|
2006-10-05 10:58:51 +00:00
|
|
|
}
|
2007-02-04 21:46:01 +00:00
|
|
|
|
2007-02-16 22:13:21 +00:00
|
|
|
usbstatus1 = (UDC_OTGSC & 0x800) ? true : false;
|
2007-09-04 08:03:07 +00:00
|
|
|
|
|
|
|
if (usbstatus1 == prev_usbstatus1)
|
|
|
|
{
|
|
|
|
/* Nothing has changed, so just return previous status */
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
prev_usbstatus1 = usbstatus1;
|
|
|
|
|
|
|
|
if (!usbstatus1)
|
|
|
|
{ /* We have just been disconnected */
|
|
|
|
status = USB_EXTRACTED;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We now know that we have just been connected to either a charger
|
|
|
|
or a computer */
|
2007-02-04 21:46:01 +00:00
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
|
|
|
|
{
|
|
|
|
/* The user wants to charge, so it doesn't matter what we are
|
|
|
|
connected to. */
|
|
|
|
|
|
|
|
status = USB_POWERED;
|
|
|
|
return status;
|
2007-02-04 21:46:01 +00:00
|
|
|
}
|
2007-09-04 08:03:07 +00:00
|
|
|
|
|
|
|
/* Run the USB controller for long enough to detect if we're connected
|
|
|
|
to a computer, then stop it again. */
|
|
|
|
|
2007-09-05 15:52:49 +00:00
|
|
|
#ifndef HAVE_USBSTACK
|
2007-09-04 08:03:07 +00:00
|
|
|
dr_controller_run();
|
2007-09-05 15:52:49 +00:00
|
|
|
#else
|
|
|
|
usb_stack_start();
|
2007-09-04 23:29:42 +00:00
|
|
|
#endif
|
2007-09-04 08:03:07 +00:00
|
|
|
|
|
|
|
/* Wait for 50 ticks (500ms) before deciding there is no computer
|
|
|
|
attached. The required value varied a lot between different users
|
|
|
|
when this feature was being tested. */
|
|
|
|
|
|
|
|
countdown = 50;
|
|
|
|
|
|
|
|
return status;
|
2006-10-05 10:58:51 +00:00
|
|
|
}
|