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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
diff --git a/apps/main.c b/apps/main.c
index 7c43c0c..12a61dd 100644
--- a/apps/main.c
+++ b/apps/main.c
@@ -167,6 +167,8 @@ int main(void)
     global_status.last_volume_change = 0;
     /* no calls INIT_ATTR functions after this point anymore!
      * see definition of INIT_ATTR in config.h */
+    while(1)
+        dbg_ports();
     root_menu();
 }

diff --git a/firmware/export/config/sansafuzev2.h b/firmware/export/config/sansafuzev2.h
index a6c2264..5be9c68 100644
--- a/firmware/export/config/sansafuzev2.h
+++ b/firmware/export/config/sansafuzev2.h
@@ -47,7 +47,7 @@
 #define HAVE_TAGCACHE

 /* define this if you have LCD enable function */
-//#define HAVE_LCD_ENABLE
+#define HAVE_LCD_ENABLE

 /* Define this if your LCD can be put to sleep. HAVE_LCD_ENABLE
    should be defined as well.
@@ -148,13 +148,13 @@
 #define BATTERY_TYPES_COUNT  1          /* only one type */

 /* Charging implemented in a target-specific algorithm */
-#define CONFIG_CHARGING 0
+#define CONFIG_CHARGING CHARGING_TARGET

 /* define this if the unit can be powered or charged via USB */
 #define HAVE_USB_POWER

-/* Define this if you have an AMS AS3525*/
-#define CONFIG_CPU AS3525
+/* Define this if you have an AMS AS3525v2 */
+#define CONFIG_CPU AS3525v2

 /* Define how much SD sectors are reserved for OF */
 #define AMS_OF_SIZE 0xF000
@@ -178,7 +178,7 @@
 #define FIRMWARE_OFFSET_FILE_CRC    0x0
 #define FIRMWARE_OFFSET_FILE_DATA   0x8

-#ifndef BOOTLOADER
+#if 0//ndef BOOTLOADER
 #define HAVE_MULTIDRIVE
 #define NUM_DRIVES 2
 #define HAVE_HOTSWAP
diff --git a/firmware/target/arm/as3525/debug-as3525.c b/firmware/target/arm/as3525/debug-as3525.c
index 8c9ff15..9c687a6 100644
--- a/firmware/target/arm/as3525/debug-as3525.c
+++ b/firmware/target/arm/as3525/debug-as3525.c
@@ -399,6 +399,101 @@ end:
     return false;
 }

+#if defined(SANSA_FUZE)
+#define DBOP_PRECHARGE 0x80FF
+#elif defined(SANSA_FUZEV2)
+#define DBOP_PRECHARGE swap16(0x80FF)
+#elif defined(SANSA_E200V2) || defined(SANSA_C200V2)
+#define DBOP_PRECHARGE 0xF0FF
+#endif
+
+static short int dbop_input_value = 0;
+
+/* read the DBOP data pins */
+unsigned short dbop_read_input(void)
+{
+    unsigned int dbop_ctrl_old = DBOP_CTRL;
+    unsigned int dbop_timpol23_old = DBOP_TIMPOL_23;
+
+    /* make sure that the DBOP FIFO is empty */
+    while ((DBOP_STAT & (1<<10)) == 0);
+
+    /* write DBOP_DOUT to pre-charge DBOP data lines with a defined level */
+    DBOP_TIMPOL_23 = 0xe007e007;    /* no strobe towards lcd */
+    int delay = 1000;
+    while (delay--) asm volatile ("nop\n");
+    DBOP_CTRL = (1 << 19) |         /* tri-state output */
+                (1 << 16) |         /* enw=1 (enable write) */
+                (1 << 12);          /* ow=1 (16-bit data width) */
+    DBOP_DOUT = DBOP_PRECHARGE;
+    while ((DBOP_STAT & (1<<10)) == 0);
+
+#if defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2)
+    delay = 1000;
+    while (delay--) asm volatile ("nop\n");
+#endif
+
+    /* perform a DBOP read */
+    DBOP_CTRL = (1 << 19) |         /* tri-state output */
+                (1 << 15) |         /* strd=1 (start read) */
+                (1 << 12) |         /* ow=1 (16-bit data width) */
+                (31 << 0);          /* rs_t=31 (read DBOP at end of cycle) */
+    while ((DBOP_STAT & (1<<16)) == 0);
+    dbop_input_value = DBOP_DIN;
+
+    /* restore previous values */
+    DBOP_TIMPOL_23 = dbop_timpol23_old;
+    DBOP_CTRL = dbop_ctrl_old;
+    delay = 1000;
+    while (delay--) asm volatile ("nop\n");
+
+    return dbop_input_value;
+}
+
+/* for the debug menu */
+unsigned short dbop_debug(void)
+{
+    return dbop_input_value;
+}
+
+unsigned read_gpioa_67(void)
+{
+    unsigned ret = 0;
+    GPIOA_DIR &= ~(1<<6);
+    if (GPIOA_PIN(6) != 0)
+        ret |= 0x40;
+    GPIOA_DIR &= ~(1<<7);
+    if (GPIOA_PIN(7) != 0)
+        ret |= 0x80;
+    return ret;
+}
+
+static void delay(int x)
+{
+    volatile int i;
+    for(i = 0; i<x; i++)
+        nop;
+}
+
+static unsigned short dbop_read_(void)
+{
+    unsigned long old = DBOP_CTRL;
+    DBOP_CTRL &= ~(1<<16|1<<14|1<<13); // not write, single word
+    DBOP_CTRL |= 1<<12|1<<19; // 16bit bus
+    delay(500);
+    DBOP_TIMPOL_01 = 0xe167e167;
+    DBOP_TIMPOL_23 = 0xe167006e;
+    DBOP_CTRL |= 1<<15; // start read
+    while(!(DBOP_STAT & 1<<16)) ; // wait data valid
+    unsigned short ret = DBOP_DIN;
+    delay(500);
+    DBOP_CTRL = old;
+    delay(500);
+    DBOP_TIMPOL_01 = 0x60036;
+    DBOP_TIMPOL_23 = 0xA12FE037;
+    return ret;
+}
+
 bool __dbg_ports(void)
 {
     int line;
@@ -408,20 +503,55 @@ bool __dbg_ports(void)

     while(1)
     {
+        volatile int delay = 0;
+        unsigned gpiod = GPIOD_DATA;
+        unsigned gpioa_dir = GPIOA_DIR;
         line = 0;
         lcd_puts(0, line++, "[GPIO Values and Directions]");
+        lcd_putsf(0, line++, "GPIOA_670: %2x", read_gpioa_67());
+        CCU_IO &= ~(1<<12);
+        GPIOB_DIR |= 1<<3;
+        GPIOB_PIN(3) = 1<<3;
+        GPIOC_DIR = 0;
+        GPIOB_DIR &= ~(1<<1);
+        GPIOB_DIR |= 1<<0;
+        GPIOB_PIN(0) = 1;
+        for(delay = 500; delay; delay--)
+            nop;
+        lcd_putsf(0, line++, "GPIOC0: %2x DIR: %2x", GPIOC_DATA, GPIOC_DIR);
+        lcd_putsf(0, line++, "GPIOD0: %2x DIR: %2x", GPIOD_DATA, GPIOD_DIR);
+        GPIOB_PIN(0) = 0;
+        GPIOD_DIR = 0xff;
+        nop;nop;nop;nop;nop;
+        nop;nop;nop;nop;nop;
+        GPIOA_DIR &= ~(1<<6|1<<7);
+        GPIOD_DATA = 0;
+        nop;nop;nop;nop;nop;
+        nop;nop;nop;nop;nop;
+        GPIOD_DIR = 0;
+        lcd_putsf(0, line++, "GPIOA_67: %2x", read_gpioa_67());
         lcd_putsf(0, line++, "GPIOA: %2x DIR: %2x", GPIOA_DATA, GPIOA_DIR);
         lcd_putsf(0, line++, "GPIOB: %2x DIR: %2x", GPIOB_DATA, GPIOB_DIR);
         lcd_putsf(0, line++, "GPIOC: %2x DIR: %2x", GPIOC_DATA, GPIOC_DIR);
         lcd_putsf(0, line++, "GPIOD: %2x DIR: %2x", GPIOD_DATA, GPIOD_DIR);
-#ifdef DEBUG_DBOP
+#if defined(DEBUG_DBOP) || defined(SANSA_FUZEV2)
         line++;
         lcd_puts(0, line++, "[DBOP_DIN]");
-        lcd_putsf(0, line++, "DBOP_DIN: %4x", dbop_debug());
+        lcd_putsf(0, line++, "DBOP_DIN: %4x", dbop_read_());
 #endif
         line++;
         lcd_puts(0, line++, "[CP15]");
         lcd_putsf(0, line++, "CP15: 0x%8x", read_cp15());
+        GPIOD_DIR = 0xff;
+        nop;nop;nop;nop;nop;
+        nop;nop;nop;nop;nop;
+        GPIOD_DATA = gpiod;
+        GPIOA_DIR = gpioa_dir;
+        nop;nop;nop;nop;nop;
+        nop;nop;nop;nop;nop;
+        GPIOD_DIR = 0;
+        lcd_putsf(0, line++, "GPIOA_672: %2x", read_gpioa_67());
+        CCU_IO |= 1<<12;
         lcd_update();
         if (button_get_w_tmo(HZ/10) == (DEBUG_CANCEL|BUTTON_REL))
             break;
diff --git a/firmware/target/arm/as3525/fmradio-i2c-as3525.c b/firmware/target/arm/as3525/fmradio-i2c-as3525.c
index 622f339..64c04a5 100644
--- a/firmware/target/arm/as3525/fmradio-i2c-as3525.c
+++ b/firmware/target/arm/as3525/fmradio-i2c-as3525.c
@@ -49,7 +49,7 @@
 #define I2C_SCL_PIN 7
 #define I2C_SDA_PIN 6

-#elif   defined(SANSA_FUZE) || defined(SANSA_E200V2)
+#elif   defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2)
 #define I2C_GPIO(x) GPIOA_PIN(x)
 #define I2C_GPIO_DIR GPIOA_DIR
 #define I2C_SCL_PIN 6
diff --git a/firmware/target/arm/as3525/powermgmt-target.h b/firmware/target/arm/as3525/powermgmt-target.h
index 5460aba..511bcc7 100644
--- a/firmware/target/arm/as3525/powermgmt-target.h
+++ b/firmware/target/arm/as3525/powermgmt-target.h
@@ -50,7 +50,7 @@
 #define CHARGER_TOTAL_TIMER (4*3600*2)
 #define ADC_BATTERY         ADC_BVDD

-#elif defined(SANSA_FUZE)
+#elif defined(SANSA_FUZE) || defined(SANSA_FUZEV2) /* FIXME */

 /* Check if topped-off and monitor voltage while plugged. */
 #define BATT_FULL_VOLTAGE   4160
diff --git a/firmware/target/arm/as3525/sansa-fuzev2/backlight-fuzev2.c b/firmware/target/arm/as3525/sansa-fuzev2/backlight-fuzev2.c
index 56b375b..ed31e44 100644
--- a/firmware/target/arm/as3525/sansa-fuzev2/backlight-fuzev2.c
+++ b/firmware/target/arm/as3525/sansa-fuzev2/backlight-fuzev2.c
@@ -45,6 +45,8 @@ bool _backlight_init(void)
 {
     GPIOB_DIR |= 1<<5; /* for buttonlight, stuff below seems to be needed
                           for buttonlight as well*/
+                          
+    ascodec_write(0x25, ascodec_read(0x25) | 2);    /* lcd power */
     ascodec_write(0x1c, 8|1);
     ascodec_write(27, 0xff);
     return true;
@@ -66,11 +68,14 @@ void _backlight_on(void)
 /* not functional */
 void _backlight_off(void)
 {
+#if 0
     ascodec_write(0x1c, 0);
+    ascodec_write(0x25, ascodec_read(0x25) | ~2);    /* lcd power */
     ascodec_write(27, 0);
 #ifdef HAVE_LCD_ENABLE
     lcd_enable(false); /* power off visible display */
 #endif
+#endif
 }

 void _buttonlight_on(void)
@@ -84,3 +89,36 @@ void _buttonlight_off(void)
     GPIOB_PIN(5) = 0;
     buttonlight_is_on = 0;
 }
+
+void blink(int n)
+{
+    while(n--)
+    {
+        int i = 8;
+        while(i--)
+        {
+            if(i&1) _buttonlight_on();
+            else    _buttonlight_off();
+            int delay = 0x10000;
+            while(delay--);
+        }
+    }
+}
+
+static void show(unsigned char c)
+{
+    blink(1);
+    if(c) blink(1);
+
+    int delay = 0x100000;
+    while(delay--);
+}
+
+void show_hword(unsigned short x)
+{
+    while(x)
+    {
+        show(x & 1);
+        x >>= 1;
+    }
+}
diff --git a/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c b/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
index 4a5948a..a54eed3 100644
--- a/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
+++ b/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
@@ -127,8 +127,16 @@ static void as3525_dbop_init(void)

     /* TODO: The OF calls some other functions here, but maybe not important */
 #endif
-    REG(0xC810000C) |= 0x1000; /* CCU_IO |= 1<<12 */
-    CGU_DBOP |= /*(1<<3)*/ 0x18 | AS3525_DBOP_DIV;
+    GPIOB_AFSEL = 0xfc;
+    GPIOC_AFSEL = 0xff;
+
+    GPIOB_DIR |= 1<<4;
+    GPIOB_PIN(4) = 1<<4;
+
+    CCU_IO |= 0x1000; /* CCU_IO |= 1<<12 */
+
+    CGU_DBOP = (1<<4) /* ? */ | (1<<3) | AS3525_DBOP_DIV;
+
     DBOP_TIMPOL_01 = 0xE12FE12F;
     DBOP_TIMPOL_23 = 0xE12F0036;
     DBOP_CTRL = 0x41004;
@@ -136,9 +144,11 @@ static void as3525_dbop_init(void)
     DBOP_CTRL = 0x51004;
     DBOP_TIMPOL_01 = 0x60036;
     DBOP_TIMPOL_23 = 0xA12FE037;
+
     /* OF sets up dma and more after here */
 }

+#if 0
 static inline void dbop_set_mode(int mode)
 {
     int delay = 10;
@@ -185,8 +195,15 @@ static void dbop_write_data(const int16_t* p_bytes, int count)
     if (count > 0)
         dbop_write_data((int16_t*)data, 1);
 }
+#endif

-static void lcd_write_cmd(short cmd)
+static void lcd_write_pixel(unsigned short value)
+{
+    DBOP_DOUT = swap16(value);
+    while ((DBOP_STAT & (1<<10)) == 0);
+}
+
+static void lcd_write_cmd(unsigned short cmd)
 {
 #if 0
     /* Write register */
@@ -198,20 +215,19 @@ static void lcd_write_cmd(short cmd)
     DBOP_TIMPOL_23 = 0xa167e06f;
 #elif 1
     volatile int i;
-    for(i=0;i<20;i++) nop;
+    for(i=0;i<0x20;i++) asm volatile ("nop\n");

     int r3 = 0x2000;
-    DBOP_CTRL |= r3;
+    DBOP_CTRL |= r3; //2 serial words
     r3 >>= 1;
-    DBOP_CTRL &= ~r3;
+    DBOP_CTRL &= ~r3; // 8 bit data width
     r3 <<= 2;
-    DBOP_CTRL &= ~r3;
+    DBOP_CTRL &= ~r3; //2 serial words
     DBOP_TIMPOL_23 = 0xA12F0036;
-    cmd = swap16(cmd);
-    DBOP_DOUT16 = cmd;
+    DBOP_DOUT = swap16(cmd);

     while ((DBOP_STAT & (1<<10)) == 0);
-    for(i=0;i<20;i++) nop;
+    for(i=0;i<0x20;i++) asm volatile ("nop\n");
     DBOP_TIMPOL_23 = 0xA12FE037;
 #else
     int i;
@@ -225,9 +241,8 @@ static void lcd_write_cmd(short cmd)

 static void lcd_write_reg(int reg, int value)
 {
-    int16_t data = value;
-    lcd_write_cmd(reg);
-    dbop_write_data(&data, 1);
+    lcd_write_cmd((unsigned short)reg);
+    lcd_write_pixel((unsigned short)value);
 }

 /*** hardware configuration ***/
@@ -262,21 +277,95 @@ void lcd_set_flip(bool yesno)
 }
 #endif

+
+static char gpioa4 = 0;
+#if 0
+static void init_gpio(void)
+{
+    GPIOB_DIR |= 1<<4;
+    GPIOB_PIN(4) = 4;
+
+    GPIOA_DIR &= ~(1<<4);
+    gpioa4 = GPIOA_PIN(4);
+
+}
+#endif
+
+static void GPIOA3_toggle_dir_clear_pin(int input)
+{
+    if (input)
+        GPIOA_DIR &= ~(1<<3);
+    else
+        GPIOA_DIR |= (1<<3);
+
+    GPIOA_PIN(3) = 0;
+}
+
+static void ssp_format(bool fmt)
+{
+    (void)SSP_CR0;
+    SSP_CR1 &= ~(1<<0); // normal serial port enabled
+    SSP_CR0 = fmt ? 0x648F : 3; // 16 bits + divider (serial clk) : 4bit
+    SSP_CR1 = 1<<1; //op enable;
+}
+
+#if 1 //defined(HAVE_LCD_ENABLE)
+void lcd_enable(bool on)
+{
+    if (display_on == on)
+        return;
+
+    if(on)
+    {
+        lcd_write_reg(R_START_OSC, 1);
+        sleep(1);
+        lcd_write_reg(R_POWER_CONTROL1, 0);
+        lcd_write_reg(R_POWER_CONTROL2, 0x3704);
+        lcd_write_reg(0x14, 0x1a1b);
+        lcd_write_reg(R_POWER_CONTROL1, 0x3860);
+        lcd_write_reg(R_POWER_CONTROL4, 0x40);
+        sleep(1);
+        lcd_write_reg(R_POWER_CONTROL4, 0x60);
+        sleep(5);
+        lcd_write_reg(R_POWER_CONTROL4, 0x70);
+        sleep(4);
+        lcd_write_reg(R_DISP_CONTROL1, 0x11);
+        sleep(4);
+        lcd_write_reg(R_DISP_CONTROL1, 0x13 | r_disp_control_rev);
+        display_on = true;
+        //lcd_update();      /* Resync display */
+        //send_event(LCD_EVENT_ACTIVATION, NULL);
+        sleep(0);
+
+    }
+    else
+    {
+        lcd_write_reg(R_DISP_CONTROL1, 0x22);
+        lcd_write_reg(R_DISP_CONTROL1, 0);
+        lcd_write_reg(R_POWER_CONTROL1, 1);
+        display_on = false;
+    }
+}
+#endif
 static void _display_on(void)
 {
     /* Initialise in the same way as the original firmare */
-
+#if 1
     lcd_write_reg(R_DISP_CONTROL1, 0);
     lcd_write_reg(R_POWER_CONTROL4, 0);
+    sleep(1);

     lcd_write_reg(R_POWER_CONTROL2, 0x3704);
     lcd_write_reg(0x14, 0x1a1b);
     lcd_write_reg(R_POWER_CONTROL1, 0x3860);
     lcd_write_reg(R_POWER_CONTROL4, 0x40);
+    sleep(1);

     lcd_write_reg(R_POWER_CONTROL4, 0x60);
+    sleep(1);

     lcd_write_reg(R_POWER_CONTROL4, 0x70);
+    sleep(1);
     lcd_write_reg(R_DRV_OUTPUT_CONTROL, 277);
     lcd_write_reg(R_DRV_WAVEFORM_CONTROL, (7<<8));
     lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
@@ -304,67 +393,182 @@ static void _display_on(void)
     lcd_write_reg(0x48, 0x0);

     lcd_write_reg(R_DISP_CONTROL1, 0x11);
+    sleep(4);
+#else
+    lcd_write_reg(0x80, 0x8d);
+    lcd_write_reg(0x92, 0x10);
+    lcd_write_reg(0x15, 0x0);
+    lcd_write_reg(0x7, 0x12);
+    sleep(1);
+    lcd_write_reg(0x7, 0);
+    lcd_write_reg(0x11, 0x1b);
+    lcd_write_reg(0x12, 0x101);
+    lcd_write_reg(0x13, 0x66);
+    lcd_write_reg(0x14, 0x4656);
+    lcd_write_reg(0x10, 0x800);
+    sleep(2);
+    lcd_write_reg(0x11, 0x11b);
+    sleep(2);
+    lcd_write_reg(0x11, 0x306+0x15);
+    sleep(2);
+    lcd_write_reg(0x11, 0x687+0x94);
+    sleep(2);
+    lcd_write_reg(0x11, 0xf1b);
+    sleep(4);
+    lcd_write_reg(0x11, 0xf1b+0x20);
+    sleep(6);
+    lcd_write_reg(0x1, 0x116);
+    lcd_write_reg(0x2, 0x100);
+    lcd_write_reg(0x3, 0x1030);
+    lcd_write_reg(0x7, 0x1030-0x1e);
+    lcd_write_reg(0x8, 0x808);
+    lcd_write_reg(0xb, 0x1030+0xd5);
+    lcd_write_reg(0xc, 0x0);
+    lcd_write_reg(0xf, 0x1a1b-0x1a);
+    sleep(2);
+    lcd_write_reg(0x15, 0x70);
+    lcd_write_reg(0x30, 0x0);
+    lcd_write_reg(0x34, 0xaf);
+    lcd_write_reg(0x35, 0x0);
+    lcd_write_reg(0x36, 0xe5);
+    lcd_write_reg(0x37, 0xa);
+    lcd_write_reg(0x38, 0xaf);
+    lcd_write_reg(0x39, 0x0);
+    lcd_write_reg(0x50, 0x101);
+    lcd_write_reg(0x51, 0x500);
+    lcd_write_reg(0x52, 0x500);
+    lcd_write_reg(0x53, 1 << 10); //
+    lcd_write_reg(0x54, 0x808-3);
+    lcd_write_reg(0x55, 0x9);
+    lcd_write_reg(0x56, 0xf00);
+    lcd_write_reg(0x57, 0xb);
+    lcd_write_reg(0x58, 0x0);
+    lcd_write_reg(0x59, 0x2);
+    lcd_write_reg(0x7, 0x12);
+    sleep(1);
+    lcd_write_reg(0x7, 0x13);
+#endif
     lcd_write_reg(R_DISP_CONTROL1, 0x13 | r_disp_control_rev);

-    display_on = true;  /* must be done before calling lcd_update() */
+    display_on = false;
+    lcd_enable(true);
     lcd_update();
 }

+static void lcd_write_red_pixel(void);
+unsigned char dbop_read(void)
+{
+    lcd_write_red_pixel();
+    lcd_delay(100);
+    DBOP_CTRL &= ~(1<<16); // not write
+    DBOP_TIMPOL_01 = 0xE12F0036;
+    DBOP_TIMPOL_23 = 0xA12FE037;
+    DBOP_CTRL |= 1<<15; // start read
+    while(!(DBOP_STAT & 1<<16)) ; // wait data valid
+    unsigned char ret = DBOP_DIN;
+    DBOP_CTRL |= 1<<16; // write
+    DBOP_TIMPOL_01 = 0x60036;
+    DBOP_TIMPOL_23 = 0xA12FE037;
+    return ret;
+}
+
+void show_hword(unsigned short);
 void lcd_init_device(void)
 {
     as3525_dbop_init();

-    GPIOA_DIR |= (0x20|0x1);
-    GPIOA_DIR &= ~(1<<3);
-    GPIOA_PIN(3) = 0;
-    GPIOA_PIN(0) = 1;
-    GPIOA_PIN(4) = 0;
+    GPIOA_DIR |= 1<<5;
+    GPIOA_DIR |= 1<<0;
+    GPIOA_PIN(5) = 0;
+
+    GPIOA3_toggle_dir_clear_pin(1);
+
+    if (gpioa4 == 0)
+        GPIOA_DIR |= 1<<4;
+    else
+        GPIOA_DIR |= 1<<0;
+
+    GPIOA3_toggle_dir_clear_pin(1);
+
+    if (gpioa4)
+        GPIOA_PIN(4) = 0;
+    else
+        GPIOA_PIN(0) = 0;
+
+    GPIOA_DIR |= 1<<0;
+    GPIOA_PIN(0) = 0;
+
+    GPIOB_DIR |= 1<<5;
+
+    /* mess with ssp and gpiob */
+    CGU_PERI |= CGU_SSP_CLOCK_ENABLE;
+    SSP_IMSC = 0;       /* No interrupts */
+#if 0
+    SSP_CPSR = AS3525_SSP_PRESCALER;    /* OF = 0x10 */
+#else
+    SSP_CPSR = 2; /* 32MHz */
+#endif
+    ssp_format(false);
+
+    /* */
+    GPIOB_PIN(6) = 1<<6;
+    GPIOB_DIR |= 1<<6;
+    GPIOB_PIN(6) = 1<<6;
+
+    //bool
+    SSP_DATA = 0;
+    SSP_DATA;

     CCU_IO &= ~(0x1000);
-    GPIOB_DIR |= 0x2f;
+
+    GPIOB_DIR |= 0xf;
     GPIOB_PIN(0) = 1<<0;
     GPIOB_PIN(1) = 1<<1;
     GPIOB_PIN(2) = 1<<2;
     GPIOB_PIN(3) = 1<<3;
-    GPIOA_PIN(4) = 1<<4;
-    GPIOA_PIN(5) = 1<<5;

-    _display_on();
-}
-
-#if defined(HAVE_LCD_ENABLE)
-void lcd_enable(bool on)
-{
-    if (display_on == on)
-        return;
-
-    if(on)
+    if (gpioa4)
     {
-        lcd_write_reg(R_START_OSC, 1);
-        lcd_write_reg(R_POWER_CONTROL1, 0);
-        lcd_write_reg(R_POWER_CONTROL2, 0x3704);
-        lcd_write_reg(0x14, 0x1a1b);
-        lcd_write_reg(R_POWER_CONTROL1, 0x3860);
-        lcd_write_reg(R_POWER_CONTROL4, 0x40);
-        lcd_write_reg(R_POWER_CONTROL4, 0x60);
-        lcd_write_reg(R_POWER_CONTROL4, 112);
-        lcd_write_reg(R_DISP_CONTROL1, 0x11);
-        lcd_write_reg(R_DISP_CONTROL1, 0x13 | r_disp_control_rev);
-        display_on = true;
-        lcd_update();      /* Resync display */
-        send_event(LCD_EVENT_ACTIVATION, NULL);
-        sleep(0);
-
+        GPIOA_DIR |= 0x10;
+        GPIOA_PIN(4) = 1<<4;
     }
     else
     {
-        lcd_write_reg(R_DISP_CONTROL1, 0x22);
-        lcd_write_reg(R_DISP_CONTROL1, 0);
-        lcd_write_reg(R_POWER_CONTROL1, 1);
-        display_on = false;
+        GPIOA_DIR |= 1<<0;
+        GPIOA_PIN(0) = 1;
     }
+
+    sleep(1);
+
+    GPIOA_PIN(5) = 1<<5;
+
+    sleep(1);
+
+    //
+    GPIOA_PIN(0) = 1<<0;
+    lcd_write_cmd(0);
+    ssp_format(true);
+    SSP_DATA = 0xffff;
+    SSP_DATA = 0xffff;
+    GPIOA_PIN(0) = 0;
+    volatile int i = 24;
+    while(i--) asm volatile("nop");
+    unsigned short val = ((dbop_read()&0xff) << 8) | (dbop_read()&0xff);
+    val -= 340;
+    if(val == 0)
+        panicf("OTHER LCD");
+    SSP_DATA = 0;
+    SSP_DATA = 0;
+    ssp_format(false);
+    GPIOA_PIN(0) = 1<<0;
+    //show_hword(val);
+    //sleep(HZ);
+
+    CCU_IO |= 0x1000;
+
+    _display_on();
 }
-#endif
+

 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
 bool lcd_active(void)
@@ -476,6 +680,7 @@ void lcd_blit_yuv(unsigned char * const src[3],
     }
 }

+static volatile bool lcd_blocked=false;
 /* Update the display.
    This must be called after all other LCD functions that change the display. */
 void lcd_update(void)
@@ -490,13 +695,18 @@ void lcd_update(void)

     lcd_write_cmd(R_WRITE_DATA_2_GRAM);

-    lcd_update_rect(0,0, LCD_WIDTH, LCD_HEIGHT);
-    //dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
+    int i,j;
+    for(i = 0; i < LCD_FBHEIGHT; i++)
+        for(j = 0; j < LCD_FBWIDTH; j++)
+            lcd_write_pixel(lcd_framebuffer[i][j]);
 }

 /* Update a fraction of the display. */
 void lcd_update_rect(int x, int y, int width, int height)
 {
+    lcd_update();
+    return;
+
     const fb_data *ptr;

     if (!display_on)
@@ -537,8 +747,43 @@ void lcd_update_rect(int x, int y, int width, int height)

     do
     {
-        dbop_write_data(ptr, width);
+        //dbop_write_data(ptr, width);
         ptr += LCD_WIDTH;
     }
     while (--height > 0);
 }
+
+void lcd_write_red_pixel(void)
+{
+    lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
+    /* Set start position and window */
+    lcd_window_x(0, 1);
+    lcd_window_y(0, 1);
+
+    lcd_write_cmd(R_WRITE_DATA_2_GRAM);
+
+    lcd_write_pixel(0x80FF);
+
+}
+
+
+unsigned short dbop_debug_(void)
+{
+    GPIOA_PIN(0) = 1<<0;
+    lcd_write_cmd(0);
+    ssp_format(true);
+    SSP_DATA = 0xffff;
+    SSP_DATA = 0xffff;
+    GPIOA_PIN(0) = 0;
+    volatile int i = 24;
+    while(i--) asm volatile("nop");
+    unsigned short ret = ((dbop_read()&0xff) << 8) | (dbop_read()&0xff);
+    ret -= 340;
+    if(ret == 0)
+        panicf("OTHER LCD");
+    SSP_DATA = 0;
+    SSP_DATA = 0;
+    ssp_format(false);
+    GPIOA_PIN(0) = 1<<0;
+    return ret;
+}