summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/ipod/usb-ipod.c
blob: c481355768163df4f33c2adf1a2a8689e59f1646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
 *             __________               __   ___.
 *   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 "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "system.h"
#include "debug.h"
#include "ata.h"
#include "fat.h"
#include "disk.h"
#include "panic.h"
#include "lcd.h"
#include "adc.h"
#include "usb.h"
#include "button.h"
#include "sprintf.h"
#include "string.h"
#include "hwcompat.h"
 
void usb_init_device(void)
{
    int r0;
    outl(inl(0x70000084) | 0x200, 0x70000084);

    outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
    outl(inl(0x6000600C) | 0x400000, 0x6000600C);

    outl(inl(0x60006004) | 0x400000, 0x60006004);   /* reset usb start */
    outl(inl(0x60006004) & ~0x400000, 0x60006004);  /* reset usb end */

    outl(inl(0x70000020) | 0x80000000, 0x70000020);
    while ((inl(0x70000028) & 0x80) == 0);

    outl(inl(0xc5000184) | 0x100, 0xc5000184);
    while ((inl(0xc5000184) & 0x100) != 0);

    outl(inl(0xc50001A4) | 0x5F000000, 0xc50001A4);
    if ((inl(0xc50001A4) & 0x100) == 0) {
        outl(inl(0xc50001A8) & ~0x3, 0xc50001A8);
        outl(inl(0xc50001A8) | 0x2, 0xc50001A8);
        outl(inl(0x70000028) | 0x4000, 0x70000028);
        outl(inl(0x70000028) | 0x2, 0x70000028);
    } else {
        outl(inl(0xc50001A8) | 0x3, 0xc50001A8);
        outl(inl(0x70000028) &~0x4000, 0x70000028);
        outl(inl(0x70000028) | 0x2, 0x70000028);
    }
    outl(inl(0xc5000140) | 0x2, 0xc5000140);
    while((inl(0xc5000140) & 0x2) != 0);
    r0 = inl(0xc5000184);

    /* Note from IPL source (referring to next 5 lines of code: 
       THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
    outl(inl(0x70000020) | 0x80000000, 0x70000020);
    outl(inl(0x6000600C) | 0x400000, 0x6000600C);
    while ((inl(0x70000028) & 0x80) == 0);
    outl(inl(0x70000028) | 0x2, 0x70000028);

    udelay(0x186A0);
}

void usb_enable(bool on)
{
    /* For the ipod, we can only do one thing with USB mode - reboot
       into Apple's flash-based disk-mode.  This does not return. */
    if (on)
    {
      /* The following code is copied from ipodlinux */
#if defined(IPOD_COLOR) || defined(IPOD_3G) || \
    defined(IPOD_4G) || defined(IPOD_MINI)
        unsigned char* storage_ptr = (unsigned char *)0x40017F00;
#elif defined(IPOD_NANO) || defined(IPOD_VIDEO) || defined(IPOD_MINI2G)
        unsigned char* storage_ptr = (unsigned char *)0x4001FF00;
#endif
        memcpy(storage_ptr, "diskmode\0\0hotstuff\0\0\1", 21);
        DEV_RS |= 4; /* Reboot */
    }
}

bool usb_detect(void)
{
    bool current_status;

    /* The following check is in the ipodlinux source, with the
       comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
    if (USB2D_IDENT != 0x22FA05) {
        return false;
    }
    current_status = (USB_STATUS & 0x800)?true:false;
    
    return current_status;
}