Index: tools/configure
===================================================================
--- tools/configure	(.../trunk)	(revision 9)
+++ tools/configure	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -808,6 +808,10 @@
     # toolset is the tools within the tools directory that we build for
     # this particular target.
     toolset=$ipodbitmaptools
+	# architecture, manufacturer and model for the target-tree build
+    t_cpu="arm"
+    t_manufacturer="apple"
+    t_model="ipod4g"
     ;;
     
    18)
Index: firmware/export/config-ipod4g.h
===================================================================
--- firmware/export/config-ipod4g.h	(.../trunk)	(revision 9)
+++ firmware/export/config-ipod4g.h	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -1,7 +1,7 @@
 /*
  * This config file is for the Apple iPod Color/Photo
  */
-#define IPOD_ARCH 1
+#define TARGET_TREE /* this target is using the target tree system */
 
 /* For Rolo and boot loader */
 #define MODEL_NUMBER 8
Index: firmware/SOURCES
===================================================================
--- firmware/SOURCES	(.../trunk)	(revision 9)
+++ firmware/SOURCES	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -292,3 +292,15 @@
 target/arm/gigabeat/meg-fx/sc606-meg-fx.c
 #endif
 #endif
+
+#ifdef IPOD_4G
+#ifndef SIMULATOR
+drivers/pcf50605.c
+target/arm/ata-pp5020.c
+target/arm/apple/ipod4g/adc-ipod4g.c
+target/arm/apple/ipod4g/backlight-ipod4g.c
+target/arm/apple/ipod4g/button-ipod4g.c
+target/arm/apple/ipod4g/power-ipod4g.c
+target/arm/apple/ipod4g/usb-ipod4g.c
+#endif
+#endif
Index: firmware/target/arm/apple/ipod4g/button-ipod4g.c
===================================================================
--- firmware/target/arm/apple/ipod4g/button-ipod4g.c	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/button-ipod4g.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,220 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: button-h10.c,v 1.4 2006-08-22 20:17:09 barrywardell Exp $
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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 <stdlib.h>
+#include "config.h"
+#include "cpu.h"
+#include "system.h"
+#include "button.h"
+#include "kernel.h"
+#include "backlight.h"
+#include "adc.h"
+#include "system.h"
+
+
+static long lastbtn;   /* Last valid button status */
+static long last_read; /* Last button status, for debouncing/filtering */
+#ifdef HAVE_LCD_BITMAP
+static bool flipped;  /* buttons can be flipped to match the LCD flip */
+#endif
+
+/* Variable to use for setting button status in interrupt handler */
+int int_btn = BUTTON_NONE;
+
+
+static void opto_i2c_init(void)
+{
+    int i, curr_value;
+
+    /* wait for value to settle */
+    i = 1000;
+    curr_value = (inl(0x7000c104) << 16) >> 24;
+    while (i > 0)
+    {
+        int new_value = (inl(0x7000c104) << 16) >> 24;
+
+        if (new_value != curr_value) {
+            i = 10000;
+            curr_value = new_value;
+        }
+        else {
+            i--;
+        }
+    }
+
+    GPIOB_OUTPUT_VAL |= 0x10;
+    DEV_EN |= 0x10000;
+    DEV_RS |= 0x10000;
+    udelay(5);
+    DEV_RS &= ~0x10000; /* finish reset */
+
+    outl(0xffffffff, 0x7000c120);
+    outl(0xffffffff, 0x7000c124);
+    outl(0xc00a1f00, 0x7000c100);
+    outl(0x1000000, 0x7000c104);
+}
+
+static inline int ipod_4g_button_read(void)
+{
+    /* The ipodlinux source had a udelay(250) here, but testing has shown that
+       it is not needed - tested on Nano, Color/Photo and Video. */
+    /* udelay(250);*/
+
+    int btn = BUTTON_NONE;
+    unsigned reg = 0x7000c104;
+    if ((inl(0x7000c104) & 0x4000000) != 0) {
+        unsigned status = inl(0x7000c140);
+
+        reg = reg + 0x3C;      /* 0x7000c140 */
+        outl(0x0, 0x7000c140); /* clear interrupt status? */
+
+        if ((status & 0x800000ff) == 0x8000001a) {
+            static int old_wheel_value IDATA_ATTR = -1;
+            static int wheel_repeat = 0;
+
+            if (status & 0x100)
+                btn |= BUTTON_SELECT;
+            if (status & 0x200)
+                btn |= BUTTON_RIGHT;
+            if (status & 0x400)
+                btn |= BUTTON_LEFT;
+            if (status & 0x800)
+                btn |= BUTTON_PLAY;
+            if (status & 0x1000)
+                btn |= BUTTON_MENU;
+            if (status & 0x40000000) {
+                /* NB: highest wheel = 0x5F, clockwise increases */
+                int new_wheel_value = (status << 9) >> 25;
+                backlight_on();
+                /* The queue should have no other events when scrolling */
+                if (queue_empty(&button_queue) && old_wheel_value >= 0) {
+
+                    /* This is for later = BUTTON_SCROLL_TOUCH;*/
+                    int wheel_delta = new_wheel_value - old_wheel_value;
+                    unsigned long data;
+                    int wheel_keycode;
+
+                    if (wheel_delta < -48)
+                        wheel_delta += 96; /* Forward wrapping case */
+                    else if (wheel_delta > 48)
+                        wheel_delta -= 96; /* Backward wrapping case */
+
+                    if (wheel_delta > 4) {
+                        wheel_keycode = BUTTON_SCROLL_FWD;
+                    } else if (wheel_delta < -4) {
+                        wheel_keycode = BUTTON_SCROLL_BACK;
+                    } else goto wheel_end;
+
+                    data = (wheel_delta << 16) | new_wheel_value;
+                    queue_post(&button_queue, wheel_keycode | wheel_repeat,
+                            (void *)data);
+
+                    if (!wheel_repeat) wheel_repeat = BUTTON_REPEAT;
+                }
+
+                old_wheel_value = new_wheel_value;
+            } else if (old_wheel_value >= 0) {
+                /* scroll wheel up */
+                old_wheel_value = -1;
+                wheel_repeat = 0;
+            }
+
+        } else if (status == 0xffffffff) {
+            opto_i2c_init();
+        }
+    }
+
+wheel_end:
+
+    if ((inl(reg) & 0x8000000) != 0) {
+        outl(0xffffffff, 0x7000c120);
+        outl(0xffffffff, 0x7000c124);
+    }
+    return btn;
+}
+
+void ipod_4g_button_int(void)
+{
+    CPU_HI_INT_CLR = I2C_MASK;
+    /* The following delay was 250 in the ipodlinux source, but 50 seems to 
+       work fine - tested on Nano, Color/Photo and Video. */
+    udelay(50); 
+    outl(0x0, 0x7000c140); 
+    int_btn = ipod_4g_button_read();
+    outl(inl(0x7000c104) | 0xC000000, 0x7000c104);
+    outl(0x400a1f00, 0x7000c100);
+
+    GPIOB_OUTPUT_VAL |= 0x10;
+    CPU_INT_EN = 0x40000000;
+    CPU_HI_INT_EN = I2C_MASK;
+}
+
+void button_init_device(void)
+{
+    opto_i2c_init();
+    /* hold button - enable as input */
+    GPIOA_ENABLE |= 0x20;
+    GPIOA_OUTPUT_EN &= ~0x20; 
+    /* hold button - set interrupt levels */
+    GPIOA_INT_LEV = ~(GPIOA_INPUT_VAL & 0x20);
+    GPIOA_INT_CLR = GPIOA_INT_STAT & 0x20;
+    /* enable interrupts */
+    GPIOA_INT_EN = 0x20;
+    /* unmask interrupt */
+    CPU_INT_EN = 0x40000000;
+    CPU_HI_INT_EN = I2C_MASK;
+}
+
+int button_read_device(void)
+{
+    static bool hold_button = false;
+    bool hold_button_old;
+	int btn = BUTTON_NONE;
+    int retval;
+
+    /* normal buttons */
+    hold_button_old = hold_button;
+    hold_button = button_hold();
+
+    if (hold_button != hold_button_old)
+        backlight_hold_changed(hold_button);
+
+    /* The int_btn variable is set in the button interrupt handler */
+    btn = int_btn;
+
+#ifdef HAVE_LCD_BITMAP
+    if (btn && flipped)
+        btn = button_flip(btn); /* swap upside down */
+#endif
+
+    /* Filter the button status. It is only accepted if we get the same
+       status twice in a row. */
+    if (btn != last_read)
+        retval = lastbtn;
+    else
+        retval = btn;
+    last_read = btn;
+
+    return retval;
+}
+
+bool button_hold(void)
+{
+    return (GPIOA_INPUT_VAL & 0x20)?false:true;
+}
Index: firmware/target/arm/apple/ipod4g/adc-target.h
===================================================================
--- firmware/target/arm/apple/ipod4g/adc-target.h	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/adc-target.h	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,30 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: adc-target.h,v 1.1 2006-08-22 20:17:09 barrywardell Exp $
+ *
+ * Copyright (C) 2002 by Linus Nielsen Feltzing
+ *
+ * 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.
+ *
+ ****************************************************************************/
+#ifndef _ADC_TARGET_H_
+#define _ADC_TARGET_H_
+
+#define NUM_ADC_CHANNELS 1
+
+#define ADC_BATTERY 0
+#define ADC_UNREG_POWER ADC_BATTERY /* For compatibility */
+
+/* Force a scan now */
+unsigned short adc_scan(int channel);
+
+#endif
Index: firmware/target/arm/apple/ipod4g/power-ipod4g.c
===================================================================
--- firmware/target/arm/apple/ipod4g/power-ipod4g.c	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/power-ipod4g.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,65 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: power-h10.c,v 1.2 2006-08-11 09:51:04 bger Exp $
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Created from power.c using some iPod code, and some custom stuff based on 
+   GPIO analysis 
+*/
+
+#include "config.h"
+#include "cpu.h"
+#include <stdbool.h>
+#include "adc.h"
+#include "kernel.h"
+#include "system.h"
+#include "power.h"
+#include "hwcompat.h"
+#include "logf.h"
+#include "usb.h"
+
+bool charger_inserted(void)
+{
+    /* TODO: Needs to be filled. */
+    return false;
+}
+
+void ide_power_enable(bool on)
+{
+    (void)on;
+    /* We do nothing on the iPod */
+}
+
+bool ide_powered(void)
+{
+    /* pretend we are always powered - we don't turn it off on the ipod */
+    return true;
+}
+
+void power_init(void)
+{
+    pcf50605_init();
+}
+
+void power_off(void)
+{
+#ifndef BOOTLOADER
+    /* We don't turn off the ipod, we put it in a deep sleep */
+    /* pcf50605_standby_mode(); */
+    pcf50605_standby_mode();
+#endif
+}
Index: firmware/target/arm/apple/ipod4g/backlight-target.h
===================================================================
--- firmware/target/arm/apple/ipod4g/backlight-target.h	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/backlight-target.h	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,25 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: backlight-target.h,v 1.3 2006-07-27 13:07:39 linus Exp $
+ *
+ * Copyright (C) 2006 by Linus Nielsen Feltzing
+ *
+ * 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.
+ *
+ ****************************************************************************/
+#ifndef BACKLIGHT_TARGET_H
+#define BACKLIGHT_TARGET_H
+
+void __backlight_on(void);
+void __backlight_off(void);
+
+#endif
Index: firmware/target/arm/apple/ipod4g/usb-target.h
===================================================================
--- firmware/target/arm/apple/ipod4g/usb-target.h	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/usb-target.h	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,26 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: usb-target.h,v 1.1 2006-03-22 11:06:45 linus Exp $
+ *
+ * Copyright (C) 2006 by Linus Nielsen Feltzing
+ *
+ * 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.
+ *
+ ****************************************************************************/
+#ifndef USB_TARGET_H
+#define USB_TARGET_H
+
+bool usb_init_device(void);
+bool usb_detect(void);
+void usb_enable(bool on);
+
+#endif
Index: firmware/target/arm/apple/ipod4g/button-target.h
===================================================================
--- firmware/target/arm/apple/ipod4g/button-target.h	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/button-target.h	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,55 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: button-target.h,v 1.1 2006-07-27 13:27:25 linus Exp $
+ *
+ * Copyright (C) 2006 by Linus Nielsen Feltzing
+ *
+ * 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.
+ *
+ ****************************************************************************/
+#ifndef _BUTTON_TARGET_H_
+#define _BUTTON_TARGET_H_
+
+#include <stdbool.h>
+#include "config.h"
+
+#define HAS_BUTTON_HOLD
+
+bool button_hold(void);
+
+/* iPod 4G specific button codes */
+
+    /* Main unit's buttons */
+#define BUTTON_SELECT       0x00000001
+#define BUTTON_MENU         0x00000002
+
+#define BUTTON_LEFT         0x00000004
+#define BUTTON_RIGHT        0x00000008
+#define BUTTON_SCROLL_FWD   0x00000010
+#define BUTTON_SCROLL_BACK  0x00000020
+
+#define BUTTON_PLAY         0x00000040
+
+#define BUTTON_MAIN (BUTTON_SELECT|BUTTON_MENU\
+                |BUTTON_LEFT|BUTTON_RIGHT|BUTTON_SCROLL_FWD\
+                |BUTTON_SCROLL_BACK|BUTTON_PLAY)
+
+#define BUTTON_REMOTE 0
+
+#define POWEROFF_BUTTON BUTTON_PLAY
+#define POWEROFF_COUNT 40
+
+/* This is for later
+#define  BUTTON_SCROLL_TOUCH 0x00000200
+*/
+
+#endif /* _BUTTON_TARGET_H_ */
Index: firmware/target/arm/apple/ipod4g/adc-ipod4g.c
===================================================================
--- firmware/target/arm/apple/ipod4g/adc-ipod4g.c	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/adc-ipod4g.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,79 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: adc-h10.c,v 1.2 2006-08-22 20:17:09 barrywardell Exp $
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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 "system.h"
+#include "kernel.h"
+#include "thread.h"
+#include "adc.h"
+
+struct adc_struct {
+    long timeout;
+    void (*conversion)(unsigned short *data);
+    short channelnum;
+    unsigned short data;
+};
+
+static struct adc_struct adcdata[NUM_ADC_CHANNELS] IDATA_ATTR;
+
+static unsigned short _adc_read(struct adc_struct *adc)
+{
+    if (adc->timeout < current_tick) {
+        unsigned char data[2];
+        unsigned short value;
+        /* 5x per 2 seconds */
+        adc->timeout = current_tick + (HZ * 2 / 5);
+
+        /* ADCC1, 10 bit, start */
+        pcf50605_write(0x2f, (adc->channelnum << 1) | 0x1);
+        pcf50605_read_multiple(0x30, data, 2); /* ADCS1, ADCS2 */
+        value   = data[0];
+        value <<= 2;
+        value  |= data[1] & 0x3;
+
+        if (adc->conversion) {
+            adc->conversion(&value);
+        }
+        adc->data = value;
+        return value;
+    } else {
+        return adc->data;
+    }
+}
+
+/* Force an ADC scan _now_ */
+unsigned short adc_scan(int channel) {
+    struct adc_struct *adc = &adcdata[channel];
+    adc->timeout = 0;
+    return _adc_read(adc);
+}
+
+/* Retrieve the ADC value, only does a scan periodically */
+unsigned short adc_read(int channel) {
+    return _adc_read(&adcdata[channel]);
+}
+
+void adc_init(void)
+{
+    struct adc_struct *adc_battery = &adcdata[ADC_BATTERY];
+    adc_battery->channelnum = 0x2; /* ADCVIN1, resistive divider */
+    adc_battery->timeout = 0;
+    _adc_read(adc_battery);
+}
Index: firmware/target/arm/apple/ipod4g/backlight-ipod4g.c
===================================================================
--- firmware/target/arm/apple/ipod4g/backlight-ipod4g.c	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/backlight-ipod4g.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,39 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: backlight-h10.c,v 1.2 2006-08-11 09:51:04 bger Exp $
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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 "system.h"
+#include "backlight.h"
+
+void __backlight_on(void)
+{
+    /* brightness full */
+    outl(0x80000000 | (0xff << 16), 0x7000a010);
+
+    /* set port b bit 3 on */
+    outl(((0x100 | 1) << 3), 0x6000d824);
+}
+
+void __backlight_off(void)
+{
+    /* fades backlight off on 4g */
+    outl(inl(0x70000084) & ~0x2000000, 0x70000084);
+    outl(0x80000000, 0x7000a010);
+}
Index: firmware/target/arm/apple/ipod4g/usb-ipod4g.c
===================================================================
--- firmware/target/arm/apple/ipod4g/usb-ipod4g.c	(.../trunk)	(revision 0)
+++ firmware/target/arm/apple/ipod4g/usb-ipod4g.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -0,0 +1,105 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: usb-h10.c,v 1.2 2006-08-11 09:51:04 bger Exp $
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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"
+#ifdef HAVE_MMC
+#include "ata_mmc.h"
+#endif
+
+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 */
+	    unsigned char* storage_ptr = (unsigned char *)0x40017F00;
+	    memcpy(storage_ptr, "diskmode\0\0hotstuff\0\0\1", 21);
+	    DEV_RS |= 4; /* Reboot */
+    }
+}
+
+bool usb_detect(void)
+{
+    /* 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;
+    }
+    return (USB_STATUS & 0x800)?true:false;
+}
+
+
+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);    
+}
Index: firmware/usb.c
===================================================================
--- firmware/usb.c	(.../trunk)	(revision 9)
+++ firmware/usb.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -179,8 +179,7 @@
     if (on)
     {
       /* The following code is copied from ipodlinux */
-#if defined(IPOD_COLOR) || defined(IPOD_3G) || \
-    defined(IPOD_4G) || defined(IPOD_MINI)
+#if defined(IPOD_COLOR) || defined(IPOD_3G) || 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;
Index: firmware/drivers/button.c
===================================================================
--- firmware/drivers/button.c	(.../trunk)	(revision 9)
+++ firmware/drivers/button.c	(.../branches/ipod4g_target_tree)	(revision 9)
@@ -87,12 +87,12 @@
 static bool remote_button_hold_only(void);
 #endif
 
-#if CONFIG_KEYPAD == IPOD_4G_PAD
+#if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_4G)
 /* Variable to use for setting button status in interrupt handler */
 int int_btn = BUTTON_NONE;
 #endif
 
-#if (CONFIG_KEYPAD == IPOD_4G_PAD) && !defined(IPOD_MINI)
+#if (CONFIG_KEYPAD == IPOD_4G_PAD) && !defined(IPOD_MINI) && !defined(IPOD_4G)
 static void opto_i2c_init(void)
 {
     int i, curr_value;
@@ -693,7 +693,7 @@
 /*
  * helper function to swap LEFT/RIGHT, UP/DOWN (if present), and F1/F3 (Recorder)
  */
-static int button_flip(int button)
+int button_flip(int button)
 {
     int newbutton;
 
@@ -1265,7 +1265,7 @@
     return retval;
 }
 
-#if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD)
+#if ((CONFIG_KEYPAD == IPOD_4G_PAD) && !defined(IPOD_4G)) || (CONFIG_KEYPAD == IPOD_3G_PAD)
 bool button_hold(void)
 {
     return (GPIOA_INPUT_VAL & 0x20)?false:true;
