2007-09-21 15:51:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Linus Nielsen Feltzing
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2007-09-21 15:51:53 +00:00
|
|
|
*
|
|
|
|
* 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 "kernel.h"
|
|
|
|
#include "ata.h"
|
2008-04-01 07:26:24 +00:00
|
|
|
#include "usb.h"
|
2008-04-18 16:42:50 +00:00
|
|
|
#include "usb_core.h"
|
2008-04-20 03:35:21 +00:00
|
|
|
#include "usb_drv.h"
|
2008-07-18 23:28:54 +00:00
|
|
|
#include "usb-target.h"
|
2010-04-09 01:21:53 +00:00
|
|
|
#include "mc13783.h"
|
2009-03-22 02:13:27 +00:00
|
|
|
#include "ccm-imx31.h"
|
2009-01-19 13:41:25 +00:00
|
|
|
#include "avic-imx31.h"
|
2010-04-09 01:21:53 +00:00
|
|
|
#include "power-gigabeat-s.h"
|
2011-01-05 19:35:51 +00:00
|
|
|
#include <string.h>
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
static int usb_status = USB_EXTRACTED;
|
2011-01-05 19:35:51 +00:00
|
|
|
static bool bootloader_install_mode = false;
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2008-04-20 03:30:57 +00:00
|
|
|
static void enable_transceiver(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
if (GPIO1_DR & (1 << 30))
|
|
|
|
{
|
2010-06-30 02:02:46 +00:00
|
|
|
bitclr32(&GPIO3_DR, (1 << 16)); /* Reset ISP1504 */
|
2008-05-08 09:03:19 +00:00
|
|
|
sleep(HZ/100);
|
2010-06-30 02:02:46 +00:00
|
|
|
bitset32(&GPIO3_DR, (1 << 16));
|
2008-05-08 09:03:19 +00:00
|
|
|
sleep(HZ/10);
|
2010-06-30 02:02:46 +00:00
|
|
|
bitclr32(&GPIO1_DR, (1 << 30)); /* Select ISP1504 */
|
2008-04-20 03:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-30 02:02:46 +00:00
|
|
|
bitset32(&GPIO1_DR, (1 << 30)); /* Deselect ISP1504 */
|
2008-04-20 03:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:05:04 +00:00
|
|
|
/* Read the immediate state of the cable from the PMIC */
|
|
|
|
bool usb_plugged(void)
|
|
|
|
{
|
|
|
|
return mc13783_read(MC13783_INTERRUPT_SENSE0) & MC13783_USB4V4S;
|
|
|
|
}
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
void usb_connect_event(void)
|
2008-04-18 16:42:50 +00:00
|
|
|
{
|
2009-01-22 22:05:04 +00:00
|
|
|
int status = usb_plugged() ? USB_INSERTED : USB_EXTRACTED;
|
|
|
|
usb_status = status;
|
2008-12-21 18:10:36 +00:00
|
|
|
/* Notify power that USB charging is potentially available */
|
2009-01-22 22:05:04 +00:00
|
|
|
charger_usb_detect_event(status);
|
|
|
|
usb_status_event((status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED);
|
2008-04-18 16:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_detect(void)
|
|
|
|
{
|
|
|
|
return usb_status;
|
|
|
|
}
|
|
|
|
|
2007-09-21 15:51:53 +00:00
|
|
|
void usb_init_device(void)
|
|
|
|
{
|
2009-01-19 13:41:25 +00:00
|
|
|
/* Do one-time inits */
|
2008-04-20 03:30:57 +00:00
|
|
|
usb_drv_startup();
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* Initially poll */
|
|
|
|
usb_connect_event();
|
|
|
|
|
|
|
|
/* Enable PMIC event */
|
2008-12-21 18:10:36 +00:00
|
|
|
mc13783_enable_event(MC13783_USB_EVENT);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_enable(bool on)
|
|
|
|
{
|
2009-01-19 13:41:25 +00:00
|
|
|
/* Module clock should be on since since this could be called with
|
|
|
|
* OFF initially and writing module registers would hardlock otherwise. */
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(CG_USBOTG, CGM_ON_RUN_WAIT);
|
2009-01-19 13:41:25 +00:00
|
|
|
enable_transceiver(true);
|
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
usb_core_init();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usb_core_exit();
|
2008-04-20 03:30:57 +00:00
|
|
|
enable_transceiver(false);
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(CG_USBOTG, CGM_OFF);
|
2008-04-18 16:42:50 +00:00
|
|
|
}
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2009-01-19 13:41:25 +00:00
|
|
|
|
|
|
|
void usb_attach(void)
|
|
|
|
{
|
2011-01-05 19:35:51 +00:00
|
|
|
bootloader_install_mode = false;
|
|
|
|
|
|
|
|
if (usb_core_driver_enabled(USB_DRIVER_MASS_STORAGE))
|
|
|
|
{
|
|
|
|
/* Check if this will be bootloader install mode, exposing the
|
|
|
|
* boot partition instead of the data partition */
|
|
|
|
bootloader_install_mode =
|
|
|
|
(button_status() & USB_BL_INSTALL_MODE_BTN) != 0;
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:05:04 +00:00
|
|
|
usb_drv_attach();
|
2009-01-19 13:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((interrupt("IRQ"))) USB_OTG_HANDLER(void)
|
|
|
|
{
|
|
|
|
usb_drv_int(); /* Call driver handler */
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_int_enable(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
2009-03-22 01:50:48 +00:00
|
|
|
{
|
|
|
|
avic_enable_int(INT_USB_OTG, INT_TYPE_IRQ, INT_PRIO_DEFAULT,
|
|
|
|
USB_OTG_HANDLER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
avic_disable_int(INT_USB_OTG);
|
|
|
|
}
|
2009-01-19 13:41:25 +00:00
|
|
|
}
|
2009-01-22 22:05:04 +00:00
|
|
|
|
|
|
|
/* Called during the bus reset interrupt when in detect mode */
|
|
|
|
void usb_drv_usb_detect_event(void)
|
|
|
|
{
|
2009-01-29 01:28:34 +00:00
|
|
|
if (usb_drv_powered())
|
|
|
|
usb_status_event(USB_INSERTED);
|
2009-01-22 22:05:04 +00:00
|
|
|
}
|
2011-01-05 19:35:51 +00:00
|
|
|
|
|
|
|
/* Called when reading the MBR */
|
|
|
|
void usb_fix_mbr(unsigned char *mbr)
|
|
|
|
{
|
|
|
|
unsigned char* p = mbr + 0x1be;
|
|
|
|
char tmp[16];
|
|
|
|
|
|
|
|
/* The Gigabeat S factory partition table contains invalid values for the
|
|
|
|
"active" flag in the MBR. This prevents at least the Linux kernel
|
|
|
|
from accepting the partition table, so we fix it on-the-fly. */
|
|
|
|
p[0x00] &= 0x80;
|
|
|
|
p[0x10] &= 0x80;
|
|
|
|
p[0x20] &= 0x80;
|
|
|
|
p[0x30] &= 0x80;
|
|
|
|
|
|
|
|
if (bootloader_install_mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Windows ignores the partition flags and mounts the first partition it
|
|
|
|
sees when the device reports itself as removable. Swap the partitions
|
|
|
|
so the data partition appears to be partition 0. Mark the boot
|
|
|
|
partition 0 as hidden and make it partition 1. */
|
|
|
|
|
|
|
|
/* Mark the first partition as hidden */
|
|
|
|
p[0x04] |= 0x10;
|
|
|
|
|
|
|
|
/* Swap first and second partitions */
|
|
|
|
memcpy(tmp, &p[0x00], 16);
|
|
|
|
memcpy(&p[0x00], &p[0x10], 16);
|
|
|
|
memcpy(&p[0x10], tmp, 16);
|
|
|
|
}
|