mkamsboot: Use audio master irq_enrd0 usb_status bit instead of gpio on C200V2

FlySpray: FS#11085
Author: Tobias Diedrich

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25088 a1c6a512-1295-4272-9138-f99709370657
diff --git a/rbutil/mkamsboot/dualboot.c b/rbutil/mkamsboot/dualboot.c
index db94aaa..aab9a18 100644
--- a/rbutil/mkamsboot/dualboot.c
+++ b/rbutil/mkamsboot/dualboot.c
@@ -50,22 +50,28 @@
     0x01, 0x00, 0x83, 0xe2, 0x00, 0x20, 0xa0, 0xe3, 0x15, 0xff, 0x2f, 0xe1, 0x3c, 0x00, 0x00, 0x00,
     0x14, 0x00, 0x0f, 0xc8, 0x00, 0x00, 0x0b, 0xc8, 0x00, 0x00, 0x0d, 0xc8
 };
-unsigned char dualboot_c200v2[236] = {
-    0xd4, 0xf0, 0x9f, 0xe5, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea,
+unsigned char dualboot_c200v2[336] = {
+    0x38, 0xf1, 0x9f, 0xe5, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea,
     0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x1f, 0xe5,
     0x24, 0x10, 0x1f, 0xe5, 0x01, 0x20, 0x40, 0xe0, 0x18, 0x30, 0x1f, 0xe5, 0x01, 0x40, 0x50, 0xe4,
     0x01, 0x40, 0x43, 0xe4, 0x00, 0x00, 0x52, 0xe1, 0xfb, 0xff, 0xff, 0x1a, 0x2c, 0x30, 0x0f, 0xe5,
-    0x78, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0x90, 0xe5, 0x01, 0x18, 0x81, 0xe3, 0x00, 0x10, 0x80, 0xe5,
-    0x6c, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x14, 0x80, 0xe5, 0x08, 0x10, 0x90, 0xe5,
-    0x00, 0x00, 0x51, 0xe3, 0x08, 0x00, 0x00, 0x1a, 0x58, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0xa0, 0xe3,
-    0x00, 0x14, 0x80, 0xe5, 0x00, 0x11, 0x90, 0xe5, 0x00, 0x00, 0x51, 0xe3, 0x02, 0x00, 0x00, 0x0a,
-    0x78, 0x00, 0x1f, 0xe5, 0x78, 0x10, 0x1f, 0xe5, 0x01, 0x00, 0x00, 0xea, 0x8c, 0x00, 0x1f, 0xe5,
-    0x8c, 0x10, 0x1f, 0xe5, 0x84, 0x30, 0x1f, 0xe5, 0x02, 0x50, 0x83, 0xe2, 0x01, 0x40, 0x43, 0xe0,
-    0x01, 0x20, 0x50, 0xe4, 0x01, 0x20, 0x43, 0xe4, 0x04, 0x00, 0x53, 0xe1, 0xfb, 0xff, 0xff, 0x1a,
-    0x01, 0x00, 0x83, 0xe2, 0x00, 0x20, 0xa0, 0xe3, 0x15, 0xff, 0x2f, 0xe1, 0x3c, 0x00, 0x00, 0x00,
-    0x14, 0x00, 0x0f, 0xc8, 0x00, 0x00, 0x0b, 0xc8, 0x00, 0x00, 0x0d, 0xc8
+    0xdc, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0x90, 0xe5, 0x01, 0x18, 0x81, 0xe3, 0x00, 0x10, 0x80, 0xe5,
+    0xcc, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0x90, 0xe5, 0x02, 0x18, 0x81, 0xe3, 0x7f, 0x10, 0xc1, 0xe3,
+    0x00, 0x10, 0x80, 0xe5, 0xbc, 0x00, 0x9f, 0xe5, 0x00, 0x10, 0xa0, 0xe3, 0x24, 0x10, 0x80, 0xe5,
+    0x3c, 0x10, 0xa0, 0xe3, 0x1c, 0x10, 0x80, 0xe5, 0x00, 0x10, 0xa0, 0xe3, 0x20, 0x10, 0x80, 0xe5,
+    0x8c, 0x10, 0xa0, 0xe3, 0x04, 0x10, 0x80, 0xe5, 0x51, 0x20, 0xa0, 0xe3, 0x0c, 0x20, 0x80, 0xe5,
+    0x30, 0x10, 0x90, 0xe5, 0x01, 0x00, 0x11, 0xe3, 0xfc, 0xff, 0xff, 0x1a, 0x25, 0x10, 0xa0, 0xe3,
+    0x44, 0x10, 0x80, 0xe5, 0x02, 0x20, 0x82, 0xe3, 0x0c, 0x20, 0x80, 0xe5, 0x01, 0x10, 0xa0, 0xe3,
+    0x10, 0x10, 0x80, 0xe5, 0x10, 0x10, 0x90, 0xe5, 0x00, 0x00, 0x51, 0xe3, 0xfc, 0xff, 0xff, 0x1a,
+    0x00, 0x10, 0x90, 0xe5, 0x08, 0x00, 0x11, 0xe3, 0x08, 0x00, 0x00, 0x1a, 0x58, 0x00, 0x9f, 0xe5,
+    0x00, 0x10, 0xa0, 0xe3, 0x00, 0x14, 0x80, 0xe5, 0x00, 0x11, 0x90, 0xe5, 0x00, 0x00, 0x51, 0xe3,
+    0x02, 0x00, 0x00, 0x0a, 0xdc, 0x00, 0x1f, 0xe5, 0xdc, 0x10, 0x1f, 0xe5, 0x01, 0x00, 0x00, 0xea,
+    0xf0, 0x00, 0x1f, 0xe5, 0xf0, 0x10, 0x1f, 0xe5, 0xe8, 0x30, 0x1f, 0xe5, 0x02, 0x50, 0x83, 0xe2,
+    0x01, 0x40, 0x43, 0xe0, 0x01, 0x20, 0x50, 0xe4, 0x01, 0x20, 0x43, 0xe4, 0x04, 0x00, 0x53, 0xe1,
+    0xfb, 0xff, 0xff, 0x1a, 0x01, 0x00, 0x83, 0xe2, 0x00, 0x20, 0xa0, 0xe3, 0x15, 0xff, 0x2f, 0xe1,
+    0x3c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x0f, 0xc8, 0x00, 0x00, 0x07, 0xc8, 0x00, 0x00, 0x0d, 0xc8
 };
 unsigned char dualboot_m200v4[212] = {
     0xc0, 0xf0, 0x9f, 0xe5, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea,
diff --git a/rbutil/mkamsboot/dualboot.h b/rbutil/mkamsboot/dualboot.h
index d67ad2b..57306de 100644
--- a/rbutil/mkamsboot/dualboot.h
+++ b/rbutil/mkamsboot/dualboot.h
@@ -3,7 +3,7 @@
 extern unsigned char nrv2e_d8[168];
 extern unsigned char dualboot_clip[256];
 extern unsigned char dualboot_e200v2[236];
-extern unsigned char dualboot_c200v2[236];
+extern unsigned char dualboot_c200v2[336];
 extern unsigned char dualboot_m200v4[212];
 extern unsigned char dualboot_fuze[236];
 extern unsigned char dualboot_clipv2[280];
diff --git a/rbutil/mkamsboot/dualboot/dualboot.S b/rbutil/mkamsboot/dualboot/dualboot.S
index d1a9721..8bb2059 100644
--- a/rbutil/mkamsboot/dualboot/dualboot.S
+++ b/rbutil/mkamsboot/dualboot/dualboot.S
@@ -27,7 +27,26 @@
 .set GPIOC,     0xC80D0000
 .set GPIOD,     0xC80E0000
 .set CGU_PERI,  0xC80F0014
-
+.set I2C_BASE,  0xC8070000
+.set I2C_DATA,  0x00
+.set I2C_SLAD0, 0x04
+.set I2C_CNTRL, 0x0c
+.set I2C_DACNT, 0x10
+.set I2C_CPSR0, 0x1c
+.set I2C_CPSR1, 0x20
+.set I2C_IMR,   0x24
+.set I2C_SR,    0x30
+.set I2C_SADDR, 0x44
+.set AS3514_I2C_ADDR, 0x46
+.set AS3514_IRQ_ENRD0, 0x25
+.set PCLK, 24000000
+.set I2C_CLK, 400000
+.set I2C_PRESCALER, ((PCLK + I2C_CLK -1) / I2C_CLK)
+.set I2C_PRESCALER_LOW, (I2C_PRESCALER & 0xff)
+.set I2C_PRESCALER_HIGH, (I2C_PRESCALER >> 8)
+#if I2C_PRESCALER_HIGH > 3
+#error i2c prescaler too big!
+#endif
 
 /* Vectors */
 
@@ -80,9 +99,7 @@
 
 
 /* TODO : M200V4 ? */
-#if defined(SANSA_C200V2)
-#define USB_PIN 1   /* FIXME: not correct on some c200v2s */
-#elif defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
+#if defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
 #define USB_PIN 6
 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2)
 #define USB_PIN 3
@@ -96,6 +113,58 @@
         cmp     r1, #0
         bne     boot_of
 #endif
+#if defined(SANSA_C200V2)
+        /* Instead of checking the GPIO, check the audio master IRQ_ENRD0
+         * USB_STATUS bit on C200V2 */
+
+        ldr     r0, =CGU_PERI
+        ldr     r1, [r0]
+        /* enable i2c audio master clock */
+        orr     r1, r1, #(1<<17)
+        /* make sure 24MHz clk_main is selected */
+        bic     r1, r1, #0x7f
+        str     r1, [r0]
+
+        ldr     r0, =I2C_BASE
+        /* disable i2c interrupts */
+        mov     r1, #0
+        str     r1, [r0, #I2C_IMR]
+        /* setup prescaler */
+        mov     r1, #I2C_PRESCALER_LOW
+        str     r1, [r0, #I2C_CPSR0]
+        mov     r1, #I2C_PRESCALER_HIGH
+        str     r1, [r0, #I2C_CPSR1]
+        /* setup i2c slave address */
+        mov     r1, #(AS3514_I2C_ADDR << 1)
+        str     r1, [r0, #I2C_SLAD0]
+        mov     r2, #0x51
+        str     r2, [r0, #I2C_CNTRL]
+
+        /* wait for not busy */
+1:
+        ldr     r1, [r0, #I2C_SR]
+        tst     r1, #1
+        bne 1b
+
+        /* start read of irq_enrd0 */
+        mov     r1, #AS3514_IRQ_ENRD0
+        str     r1, [r0, #I2C_SADDR]
+        orr     r2, r2, #(1 << 1)
+        str     r2, [r0, #I2C_CNTRL]
+        mov     r1, #1
+        str     r1, [r0, #I2C_DACNT]
+
+        /* wait for transfer to finish */
+1:
+        ldr     r1, [r0, #I2C_DACNT]
+        cmp     r1, #0
+        bne     1b
+
+        /* load result and test USB_STATUS bit */
+        ldr     r1, [r0, #I2C_DATA]
+        tst     r1, #(1 << 3)
+        bne     boot_of
+#endif
 
         /* Here are model specific tests, for dual boot without a computer */
         /* All models use left button */