summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx233/debug-imx233.c
blob: 9ca33ff715da9848ed7df1f0b550bf4be44005e5 (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
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 by Amaury Pouly
 *
 * 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "cpu.h"
#include "system.h"
#include "kernel.h"
#include "dma-imx233.h"
#include "lcd.h"
#include "font.h"
#include "adc.h"
#include "power-imx233.h"
#include "clkctrl-imx233.h"
#include "powermgmt-imx233.h"
#include "rtc-imx233.h"
#include "dcp-imx233.h"
#include "pinctrl-imx233.h"
#include "ocotp-imx233.h"
#include "pwm-imx233.h"
#include "emi-imx233.h"
#include "audioin-imx233.h"
#include "audioout-imx233.h"
#include "timrot-imx233.h"
#include "string.h"
#include "stdio.h"
#include "button.h"
#include "button-lradc-imx233.h"

#define ACT_NONE    0
#define ACT_CANCEL  1
#define ACT_OK      2
#define ACT_PREV    3
#define ACT_NEXT    4
#define ACT_LEFT    5
#define ACT_RIGHT   6
#define ACT_REPEAT  0x1000

int my_get_action(int tmo)
{
    int btn = button_get_w_tmo(tmo);
    while(btn & BUTTON_REL)
        btn = button_get_w_tmo(tmo);
    bool repeat = btn & BUTTON_REPEAT;
    int act = ACT_NONE;
    switch(btn)
    {
        case BUTTON_POWER:
#if defined(BUTTON_BACK)
        case BUTTON_BACK:
#elif defined(BUTTON_LEFT)
        case BUTTON_LEFT:
#else
#error no key for ACT_CANCEL
#endif
            act = ACT_CANCEL;
            break;
#if defined(BUTTON_SELECT)
        case BUTTON_SELECT:
#elif defined(BUTTON_PLAY)
        case BUTTON_PLAY:
#elif defined(BUTTON_CENTER)
        case BUTTON_CENTER:
#else
#error no key for ACT_OK
#endif
            act = ACT_OK;
            break;
        case BUTTON_UP:
            act = ACT_PREV;
            break;
        case BUTTON_DOWN:
            act = ACT_NEXT;
            break;
        default:
            yield();
    }
    if(repeat)
        act |= ACT_REPEAT;
    return act;
}

static struct
{
    const char *name;
    unsigned chan;
} dbg_channels[] =
{
    { "i2c", APB_I2C },
    { "dac", APB_AUDIO_DAC },
    { "ssp1", APB_SSP(1) },
    { "ssp2", APB_SSP(2) },
};

static struct
{
    const char *name;
    unsigned src;
} dbg_irqs[] =
{
    { "vdd5v", INT_SRC_VDD5V },
    { "dac_dma", INT_SRC_DAC_DMA },
    { "dac_err", INT_SRC_DAC_ERROR },
    { "adc_dma", INT_SRC_ADC_DMA },
    { "adc_err", INT_SRC_ADC_ERROR },
    { "usbctrl", INT_SRC_USB_CTRL },
    { "ssp1_dma", INT_SRC_SSP1_DMA },
    { "ssp1_err", INT_SRC_SSP1_ERROR },
    { "gpio0", INT_SRC_GPIO0 },
    { "gpio1", INT_SRC_GPIO1 },
    { "gpio2", INT_SRC_GPIO2 },
#if IMX233_SUBTARGET >= 3780
    { "ssp2_dma", INT_SRC_SSP2_DMA },
    { "ssp2_err", INT_SRC_SSP2_ERROR },
    { "lcdif_dma", INT_SRC_LCDIF_DMA },
    { "lcdif_err", INT_SRC_LCDIF_ERROR },
    { "dcp", INT_SRC_DCP },
#endif
    { "i2c_dma", INT_SRC_I2C_DMA },
    { "i2c_err", INT_SRC_I2C_ERROR },
    { "timer0", INT_SRC_TIMER(0) },
    { "timer1", INT_SRC_TIMER(1) },
    { "timer2", INT_SRC_TIMER(2) },
    { "timer3", INT_SRC_TIMER(3) },
    { "touch_det", INT_SRC_TOUCH_DETECT },
    { "lradc_ch0", INT_SRC_LRADC_CHx(0) },
    { "lradc_ch1", INT_SRC_LRADC_CHx(1) },
    { "lradc_ch2", INT_SRC_LRADC_CHx(2) },
    { "lradc_ch3", INT_SRC_LRADC_CHx(3) },
    { "lradc_ch4", INT_SRC_LRADC_CHx(4) },
    { "lradc_ch5", INT_SRC_LRADC_CHx(5) },
    { "lradc_ch6", INT_SRC_LRADC_CHx(6) },
    { "lradc_ch7", INT_SRC_LRADC_CHx(7) },
    { "rtc_1msec", INT_SRC_RTC_1MSEC },
};

bool dbg_hw_info_dma(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 25);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();

        lcd_putsf(0, 0, "S C name bar      apb ahb una");
        for(unsigned i = 0; i < ARRAYLEN(dbg_channels); i++)
        {
            struct imx233_dma_info_t info = imx233_dma_get_info(dbg_channels[i].chan, DMA_INFO_ALL);
            lcd_putsf(0, i + 1, "%c %c %4s %8x %3x %3x %3x",
                info.gated ? 'g' : info.freezed ? 'f' : ' ',
                !info.int_enabled ? '-' : info.int_error ? 'e' : info.int_cmdcomplt ? 'c' : ' ',
                dbg_channels[i].name, info.bar, info.apb_bytes, info.ahb_bytes,
                info.nr_unaligned);
        }

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_power(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();

        struct imx233_power_info_t info = imx233_power_get_info(POWER_INFO_ALL);
        int line = 0;
        unsigned trg, bo;
        bool en;
        int linreg;
        char buf[16];
        
        lcd_putsf(0, line++, "name  value bo linreg");
#define DISP_REGULATOR(name) \
        imx233_power_get_regulator(REGULATOR_##name, &trg, &bo); \
        imx233_power_get_regulator_linreg(REGULATOR_##name, &en, &linreg); \
        if(en) snprintf(buf, sizeof(buf), "%d", linreg); \
        else snprintf(buf, sizeof(buf), " "); \
        lcd_putsf(0, line++, "%6s %4d %4d %s", #name, trg, bo, buf); \

        DISP_REGULATOR(VDDD);
#if IMX233_SUBTARGET >= 3700
        DISP_REGULATOR(VDDA);
        DISP_REGULATOR(VDDIO);
#endif
#if IMX233_SUBTARGET >= 3780
        DISP_REGULATOR(VDDMEM);
#endif
        lcd_putsf(0, line++, "DC-DC: pll: %d   freq: %d", info.dcdc_sel_pllclk, info.dcdc_freqsel);
        lcd_putsf(0, line++, "charge: %d mA  stop: %d mA", info.charge_current, info.stop_current);
        lcd_putsf(0, line++, "charging: %d  bat_adj: %d", info.charging, info.batt_adj);
        lcd_putsf(0, line++, "4.2: en: %d  dcdc: %d", info._4p2_enable, info._4p2_dcdc);
        lcd_putsf(0, line++, "4.2: cmptrip: %d dropout: %d", info._4p2_cmptrip, info._4p2_dropout);
        lcd_putsf(0, line++, "5V: pwd_4.2_charge: %d", info._5v_pwd_charge_4p2);
        lcd_putsf(0, line++, "5V: chargelim: %d mA", info._5v_charge_4p2_limit);
        lcd_putsf(0, line++, "5V: dcdc: %d  xfer: %d", info._5v_enable_dcdc, info._5v_dcdc_xfer);
        lcd_putsf(0, line++, "5V: thr: %d mV use: %d cmps: %d", info._5v_vbusvalid_thr,
            info._5v_vbusvalid_detect, info._5v_vbus_cmps);

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_lradc(void)
{
    lcd_setfont(FONT_SYSFIXED);
    
    while(1)
    {
        int button = my_get_action(HZ / 25);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();

        /* add battery readout in mV, this it is not the direct output of a channel */
        lcd_putsf(0, 0, "Battery(mV) %d", _battery_voltage());
        for(unsigned i = 0; i < NUM_ADC_CHANNELS; i++)
        {
            lcd_putsf(0, i + 1, "%s %d", adc_name(i), adc_read(i));
        }

        lcd_update();
        yield();
    }
}

static struct
{
    enum imx233_clock_t clk;
    const char *name;
    bool has_enable;
    bool has_bypass;
    bool has_idiv;
    bool has_fdiv;
    bool has_freq;
} dbg_clk[] =
{
    { CLK_PLL, "pll", true, false, false, false, true},
    { CLK_XTAL, "xtal", false, false, false, false, true},
#if IMX233_SUBTARGET >= 3700
    { CLK_PIX, "pix", true, true, true, true, true },
#endif
    { CLK_SSP, "ssp", true, true, true, false, true },
    { CLK_IO,  "io",  false, false, false, true, true },
    { CLK_CPU, "cpu", false, true, true, true, true },
    { CLK_HBUS, "hbus", false, false, true, true, true },
    { CLK_EMI, "emi", false, true, true, true, true },
    { CLK_XBUS, "xbus", false, false, true, false, true }
};

bool dbg_hw_info_clkctrl(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();

        /*               012345678901234567890123456789 */
        lcd_putsf(0, 0, "name en by idiv fdiv frequency");
        for(unsigned i = 0; i < ARRAYLEN(dbg_clk); i++)
        {
            #define c dbg_clk[i]
            lcd_putsf(0, i + 1, "%4s", c.name);
            if(c.has_enable)
                lcd_putsf(5, i + 1, "%2d", imx233_clkctrl_is_enabled(c.clk));
#if IMX233_SUBTARGET >= 3700
            if(c.has_bypass)
                lcd_putsf(8, i + 1, "%2d", imx233_clkctrl_get_bypass(c.clk));
#endif
            if(c.has_idiv && imx233_clkctrl_get_div(c.clk) != 0)
                lcd_putsf(10, i + 1, "%4d", imx233_clkctrl_get_div(c.clk));
#if IMX233_SUBTARGET >= 3700
            if(c.has_fdiv && imx233_clkctrl_get_frac_div(c.clk) != 0)
                lcd_putsf(16, i + 1, "%4d", imx233_clkctrl_get_frac_div(c.clk));
#endif
            if(c.has_freq)
                lcd_putsf(21, i + 1, "%9d", imx233_clkctrl_get_freq(c.clk));
            #undef c
        }
        int line = ARRAYLEN(dbg_clk) + 1;
        if(!imx233_clkctrl_is_auto_slow_enabled())
            lcd_putsf(0, line++, "auto-slow: disabled");
        else
            lcd_putsf(0, line++, "auto-slow: 1/%d", 1 << imx233_clkctrl_get_auto_slow_div());

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_powermgmt(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        struct imx233_powermgmt_info_t info = imx233_powermgmt_get_info();

        lcd_putsf(0, 0, "state: %s",
            info.state == DISCHARGING ? "discharging" :
#if CONFIG_CHARGING >= CHARGING_MONITOR
            info.state == CHARGE_STATE_DISABLED ? "disabled" :
            info.state == CHARGE_STATE_ERROR ? "error" :
#endif
#if CONFIG_CHARGING >= CHARGING_MONITOR
            info.state == TRICKLE ? "trickle" :
            info.state == TOPOFF ? "topoff" :
            info.state == CHARGING ? "charging" :
#endif
            "<unknown>");
        lcd_putsf(0, 1, "charging tmo: %d", info.charging_timeout);
        lcd_putsf(0, 2, "topoff tmo: %d", info.topoff_timeout);
        lcd_putsf(0, 3, "4p2ilimit tmo: %d", info.incr_4p2_ilimit_timeout);

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_rtc(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        struct imx233_rtc_info_t info = imx233_rtc_get_info();

        lcd_putsf(0, 0, "seconds: %lu", info.seconds);
        for(int i = 0; i < 6; i++)
            lcd_putsf(0, i + 1, "persistent%d: 0x%lx", i, info.persistent[i]);

        lcd_update();
        yield();
    }
}

#if IMX233_SUBTARGET >= 3780
bool dbg_hw_info_dcp(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        struct imx233_dcp_info_t info = imx233_dcp_get_info(DCP_INFO_ALL);

        lcd_putsf(0, 0, "crypto: %d  csc: %d", info.has_crypto, info.has_csc);
        lcd_putsf(0, 1, "keys: %d  channels: %d", info.num_keys, info.num_channels);
        lcd_putsf(0, 2, "ciphers: 0x%lx  hash: 0x%lx", info.ciphers, info.hashs);
        lcd_putsf(0, 3, "gather wr: %d  otp rdy: %d ch0merged: %d",
            info.gather_writes, info.otp_key_ready, info.ch0_merged);
        lcd_putsf(0, 4, "ctx switching: %d caching: %d", info.context_switching,
            info.context_caching);
        lcd_putsf(0, 5, "ch  irq ien en rdy pri sem cmdptr     a");
        int nr = HW_DCP_NUM_CHANNELS;
        for(int i = 0; i < nr; i++)
        {
            lcd_putsf(0, 6 + i, "%d    %d   %d   %d  %d   %d   %d  0x%08lx %d",
                i, info.channel[i].irq, info.channel[i].irq_en, info.channel[i].enable,
                info.channel[i].ready, info.channel[i].high_priority,
                info.channel[i].sema, info.channel[i].cmdptr, info.channel[i].acquired);
        }
        lcd_putsf(0, 6 + nr, "csc  %d   %d   %d      %d",
                  info.csc.irq, info.csc.irq_en, info.csc.enable, info.csc.priority);
        lcd_update();
        yield();
    }
}
#else
bool dbg_hw_info_dcp(void)
{
    return true;
}
#endif

/** Nested IRQ check code */
void INT_SOFTWARE(unsigned nr)
{
    imx233_icoll_force_irq(INT_SRC_SOFTWARE(nr), false);
    HW_DIGCTL_SCRATCH0 = nr;
    if(nr < 3)
    {
        imx233_icoll_force_irq(INT_SRC_SOFTWARE(nr + 1), true);
        udelay(10);
        if(HW_DIGCTL_SCRATCH0 == nr)
            panicf("Nestes IRQ bug (%d)", nr);
    }
}

void INT_SOFTWARE0(void) { INT_SOFTWARE(0); }
void INT_SOFTWARE1(void) { INT_SOFTWARE(1); }
void INT_SOFTWARE2(void) { INT_SOFTWARE(2); }
void INT_SOFTWARE3(void) { INT_SOFTWARE(3); }

static void check_nested_irq(void)
{
    /* Test protocol: setup SOFTWAREn IRQ as level n and apply:
     * - enable SOFTWARE0 and soft IRQ'it
     * - in SOFTWAREn, enable SOFTWARE(n+1) and soft IRQ'it, check it ran
     */
    for(int i = 0; i < 4; i++)
    {
        imx233_icoll_enable_interrupt(INT_SRC_SOFTWARE(i), true);
        imx233_icoll_set_priority(INT_SRC_SOFTWARE(i), i);
    }
    HW_DIGCTL_SCRATCH0 = 0;
    imx233_icoll_force_irq(INT_SRC_SOFTWARE(0), true);
    udelay(100);
    if(HW_DIGCTL_SCRATCH0 != 3)
        panicf("Nested IRQ broken (%lu)", HW_DIGCTL_SCRATCH0);
    for(int i = 0; i < 4; i++)
        imx233_icoll_enable_interrupt(INT_SRC_SOFTWARE(i), false);
}

bool dbg_hw_info_icoll(void)
{
    lcd_setfont(FONT_SYSFIXED);

    check_nested_irq();

    int first_irq = 0;
    int dbg_irqs_count = sizeof(dbg_irqs) / sizeof(dbg_irqs[0]);
    int line_count = lcd_getheight() / font_get(lcd_getfont())->height;

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
                first_irq++;
                if(first_irq >= dbg_irqs_count)
                    first_irq = dbg_irqs_count - 1;
                break;
            case ACT_PREV:
                first_irq--;
                if(first_irq < 0)
                    first_irq = 0;
                break;
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        for(int i = first_irq, j = 0; i < dbg_irqs_count && j < line_count; i++, j++)
        {
            struct imx233_icoll_irq_info_t info = imx233_icoll_get_irq_info(dbg_irqs[i].src);
            static char prio[4] = {'-', '+', '^', '!'};
            lcd_putsf(0, j, "%c%s", prio[info.priority & 3], dbg_irqs[i].name);
            if(info.enabled || info.freq > 0)
                lcd_putsf(10, j, "%d", info.freq);
        }
        lcd_update();
        yield();
    }
}

bool dbg_hw_info_pinctrl(void)
{
    lcd_setfont(FONT_SYSFIXED);

#ifdef IMX233_PINCTRL_DEBUG
    unsigned top_user = 0;
#endif
    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
#ifdef IMX233_PINCTRL_DEBUG
                top_user++;
                break;
#endif
            case ACT_PREV:
#ifdef IMX233_PINCTRL_DEBUG
                if(top_user > 0)
                    top_user--;
                break;
#endif
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        for(int i = 0; i < 4; i++)
            lcd_putsf(0, i, "DIN%d = 0x%08x", i, HW_PINCTRL_DINn(i));
#ifdef IMX233_PINCTRL_DEBUG
        unsigned cur_line = 6;
        unsigned last_line = lcd_getheight() / font_get(lcd_getfont())->height;
        unsigned cur_idx = 0;

        for(int bank = 0; bank < 4; bank++)
        for(int pin = 0; pin < 32; pin++)
        {
            const char *owner = imx233_pinctrl_blame(bank, pin);
            if(owner == NULL)
                continue;
            if(cur_idx++ >= top_user && cur_line < last_line)
                lcd_putsf(0, cur_line++, "B%dP%02d %s", bank, pin, owner);
        }
        if(cur_idx < top_user)
            top_user = cur_idx - 1;
#endif
        lcd_update();
        yield();
    }
}

struct
{
    const char *name;
    volatile uint32_t *addr;
} dbg_ocotp[] =
{
#if IMX233_SUBTARGET >= 3700
#define E(n,v) { .name = n, .addr = &v }
    E("CUST0", HW_OCOTP_CUSTn(0)), E("CUST1", HW_OCOTP_CUSTn(1)),
    E("CUST2", HW_OCOTP_CUSTn(2)), E("CUST0", HW_OCOTP_CUSTn(3)),
    E("HWCAP0", HW_OCOTP_HWCAPn(0)), E("HWCAP1", HW_OCOTP_HWCAPn(1)),
    E("HWCAP2", HW_OCOTP_HWCAPn(2)), E("HWCAP3", HW_OCOTP_HWCAPn(3)),
    E("HWCAP4", HW_OCOTP_HWCAPn(4)), E("HWCAP5", HW_OCOTP_HWCAPn(5)),
    E("SWCAP", HW_OCOTP_SWCAP), E("CUSTCAP", HW_OCOTP_CUSTCAP),
    E("OPS0", HW_OCOTP_OPSn(0)), E("OPS1", HW_OCOTP_OPSn(1)),
    E("OPS2", HW_OCOTP_OPSn(2)), E("OPS2", HW_OCOTP_OPSn(3)),
    E("UN0", HW_OCOTP_UNn(0)), E("UN1", HW_OCOTP_UNn(1)),
    E("UN2", HW_OCOTP_UNn(2)),
    E("ROM0", HW_OCOTP_ROMn(0)), E("ROM1", HW_OCOTP_ROMn(1)),
    E("ROM2", HW_OCOTP_ROMn(2)), E("ROM3", HW_OCOTP_ROMn(3)),
    E("ROM4", HW_OCOTP_ROMn(4)), E("ROM5", HW_OCOTP_ROMn(5)),
    E("ROM6", HW_OCOTP_ROMn(6)), E("ROM7", HW_OCOTP_ROMn(7)),
#undef E
#else
#define E(n,v) { .name = n, .addr = &v }
    E("LASERFUSE0", HW_RTC_LASERFUSEn(0)),
    E("LASERFUSE1", HW_RTC_LASERFUSEn(1)),
    E("LASERFUSE2", HW_RTC_LASERFUSEn(2)),
    E("LASERFUSE3", HW_RTC_LASERFUSEn(3)),
    E("LASERFUSE4", HW_RTC_LASERFUSEn(4)),
    E("LASERFUSE5", HW_RTC_LASERFUSEn(5)),
    E("LASERFUSE6", HW_RTC_LASERFUSEn(6)),
    E("LASERFUSE7", HW_RTC_LASERFUSEn(7)),
    E("LASERFUSE8", HW_RTC_LASERFUSEn(8)),
    E("LASERFUSE9", HW_RTC_LASERFUSEn(9)),
    E("LASERFUSE10", HW_RTC_LASERFUSEn(10)),
    E("LASERFUSE11", HW_RTC_LASERFUSEn(11)),
#undef E
#endif
};

bool dbg_hw_info_ocotp(void)
{
    lcd_setfont(FONT_SYSFIXED);

    unsigned top_user = 0;

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
                top_user++;
                break;
            case ACT_PREV:
                if(top_user > 0)
                    top_user--;
                break;
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        unsigned cur_line = 0;
        unsigned last_line = lcd_getheight() / font_get(lcd_getfont())->height;
        unsigned i = 0;

        for(i = 0; i < ARRAYLEN(dbg_ocotp); i++)
        {
            if(i >= top_user && cur_line < last_line)
            {
                lcd_putsf(0, cur_line, "%s", dbg_ocotp[i].name);
                lcd_putsf(8, cur_line++, "%x", imx233_ocotp_read(dbg_ocotp[i].addr));
            }
        }
        if(i < top_user)
            top_user = i - 1;
        
        lcd_update();
        yield();
    }
}

bool dbg_hw_info_pwm(void)
{
    lcd_setfont(FONT_SYSFIXED);
    bool detailled_view = false;

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
                detailled_view = !detailled_view;
                break;
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        int line = 0;
        if(detailled_view)
            lcd_putsf(0, line++, "c cdiv period active/x inactive");
        else
            lcd_putsf(0, line++, "pwm");
        for(int i = 0; i < IMX233_PWM_NR_CHANNELS; i++)
        {
            struct imx233_pwm_info_t info = imx233_pwm_get_info(i);
            if(!info.enabled)
                continue;
            if(detailled_view)
            {
                lcd_putsf(0, line++, "%d %4d %6d %6d/%c %6d/%c", i, info.cdiv,
                    info.period, info.active, info.active_state,
                    info.inactive, info.inactive_state);
            }
            else
            {
                char *prefix = "";
                int freq = imx233_clkctrl_get_freq(CLK_XTAL) * 1000 / info.cdiv / info.period;
                if(freq > 1000)
                {
                    prefix = "K";
                    freq /= 1000;
                }
                int duty = (info.inactive - info.active) * 100 / info.period;
                lcd_putsf(0, line++, "%d @%d %sHz, %d%% %c/%c", i, freq, prefix,
                    duty, info.active_state, info.inactive_state);
            }
        }

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_usb(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
                BF_SET(USBPHY_CTRL, ENOTGIDDETECT);
                BF_SET(USBPHY_CTRL, ENDEVPLUGINDETECT);
                break;
            case ACT_PREV:
                BF_CLR(USBPHY_CTRL, ENOTGIDDETECT);
                BF_CLR(USBPHY_CTRL, ENDEVPLUGINDETECT);
                break;
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        int line = 0;
#if IMX233_SUBTARGET >= 3700
        lcd_putsf(0, line++, "VBUS valid: %d/%d", BF_RD(POWER_STS, VBUSVALID), BF_RD(POWER_STS, VBUSVALID_STATUS));
        lcd_putsf(0, line++, "A valid: %d/%d", BF_RD(POWER_STS, AVALID), BF_RD(POWER_STS, AVALID_STATUS));
        lcd_putsf(0, line++, "B valid: %d/%d", BF_RD(POWER_STS, BVALID), BF_RD(POWER_STS, BVALID_STATUS));
#else
        lcd_putsf(0, line++, "VBUS valid: %d/%d", BF_RD(POWER_STS, VBUSVALID));
        lcd_putsf(0, line++, "A valid: %d/%d", BF_RD(POWER_STS, AVALID));
        lcd_putsf(0, line++, "B valid: %d/%d", BF_RD(POWER_STS, BVALID));
#endif
        lcd_putsf(0, line++, "session end: %d", BF_RD(POWER_STS, SESSEND));
        lcd_putsf(0, line++, "dev plugin: %d", BF_RD(USBPHY_STATUS, DEVPLUGIN_STATUS));
        lcd_putsf(0, line++, "OTG ID: %d", BF_RD(USBPHY_STATUS, OTGID_STATUS));

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_emi(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        struct imx233_emi_info_t info = imx233_emi_get_info();
        int line = 0;
        lcd_putsf(0, line++, "EMI");
        lcd_putsf(0, line++, "rows: %d", info.rows);
        lcd_putsf(0, line++, "columns: %d", info.columns);
        lcd_putsf(0, line++, "banks: %d", info.banks);
        lcd_putsf(0, line++, "chips: %d", info.chips);
        lcd_putsf(0, line++, "size: %d MiB", info.size / 1024 / 1024);
        lcd_putsf(0, line++, "cas: %d.%d", info.cas / 2, 5 * (info.cas % 2));

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_audio(void)
{
    static const char *hp_sel[2] = {"DAC", "Line1"};
    static const char *mux_sel[4] = {"Mic", "Line1", "HP", "Line2"};
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        struct imx233_audioout_info_t out = imx233_audioout_get_info();
        struct imx233_audioin_info_t in = imx233_audioin_get_info();
        int line = 0;
#define display_sys(st, sys, name) \
        if(st.sys) \
        { \
            char buffer[64]; \
            snprintf(buffer, 64, "%s: ", name); \
            for(int i = 0; i < 2; i++) \
            { \
                if(st.sys##mute[i]) \
                    strcat(buffer, "mute"); \
                else \
                    snprintf(buffer + strlen(buffer), 64,  "%d.%d", \
                        /* properly handle negative values ! */ \
                        st.sys##vol[i] / 10, (10 + (st.sys##vol[i]) % 10) % 10); \
                if(i == 0) \
                    strcat(buffer, " / "); \
                else \
                    strcat(buffer, " dB"); \
            } \
            lcd_putsf(0, line++, "%s", buffer); \
        } \
        else \
            lcd_putsf(0, line++, "%s: powered down", name);
        display_sys(out, dac, "DAC");
        display_sys(out, hp, "HP");
        display_sys(out, spkr, "SPKR");
        display_sys(in, adc, "ADC");
        display_sys(in, mux, "MUX");
        display_sys(in, mic, "MIC");
#undef display_sys
        lcd_putsf(0, line++, "capless: %d", out.capless);
        lcd_putsf(0, line++, "HP select: %s", hp_sel[out.hpselect]);
        lcd_putsf(0, line++, "MUX select: %s / %s", mux_sel[in.muxselect[0]], mux_sel[in.muxselect[1]]);

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_timrot(void)
{
    lcd_setfont(FONT_SYSFIXED);

    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
            case ACT_PREV:
            case ACT_OK:
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        int line = 0;
        for(int i = 0; i < 4; i++)
        {
            struct imx233_timrot_info_t info = imx233_timrot_get_info(i);
            const char *unit = NULL;
            const char *unit_prefix = "";
            int src_freq = 1;
            switch(info.src)
            {
                case BV_TIMROT_TIMCTRLn_SELECT__NEVER_TICK: src_freq = 0; break;
                case BV_TIMROT_TIMCTRLn_SELECT__PWM0: unit = "PWM0"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__PWM1: unit = "PWM1"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__PWM2: unit = "PWM2"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__PWM3: unit = "PWM3"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__PWM4: unit = "PWM4"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__ROTARYA: unit = "ROTA"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__ROTARYB: unit = "ROTB"; break;
                case BV_TIMROT_TIMCTRLn_SELECT__32KHZ_XTAL: src_freq = 32000; break;
                case BV_TIMROT_TIMCTRLn_SELECT__8KHZ_XTAL: src_freq = 8000; break;
                case BV_TIMROT_TIMCTRLn_SELECT__4KHZ_XTAL: src_freq = 4000; break;
                case BV_TIMROT_TIMCTRLn_SELECT__1KHZ_XTAL: src_freq = 1000; break;
                case BV_TIMROT_TIMCTRLn_SELECT__TICK_ALWAYS: 
                default: src_freq = 24000000 / (1 << info.prescale); break;
            }
            int count = info.reload ? info.fixed_count + 1 : info.run_count;
            if(src_freq == 0 || count == 0)
                continue;
            unsigned long long freq;
            if(info.reload)
                freq = (unsigned long long)src_freq * 1000 / count;
            else
                freq = count * 1000 / src_freq;

            if(freq >= 1000000000)
            {
                unit_prefix = "M";
                freq /= 1000000;
            }
            else if(freq >= 1000000)
            {
                unit_prefix = "k";
                freq /= 1000;
            }
            char str[32];
            if(freq % 1000)
                snprintf(str, sizeof(str), "%lu.%lu", (unsigned long)freq / 1000, (unsigned long)freq % 1000);
            else
                snprintf(str, sizeof(str), "%lu", (unsigned long)freq / 1000);
            char str2[32];
            if(unit)
                snprintf(str2, sizeof(str2), "%s%s(%s)", unit_prefix, info.reload ? "H" : "", unit);
            else
                snprintf(str2, sizeof(str2), "%s%s", unit_prefix, info.reload ? "Hz" : "s");
            lcd_putsf(0, line++, "%ctimer %d: %s %s", info.polarity ? '+' : '-', i, str, str2);
        }

        lcd_update();
        yield();
    }
}

bool dbg_hw_info_button(void)
{
    lcd_setfont(FONT_SYSFIXED);
#if IMX233_SUBTARGET >= 3700 && defined(IMX233_BUTTON_LRADC_VDDIO)
    int orig_vddio_val, orig_vddio_brownout;
    imx233_power_get_regulator(REGULATOR_VDDIO, &orig_vddio_val, &orig_vddio_brownout);
    int vddio_val = orig_vddio_val;
    int vddio_brownout = orig_vddio_brownout;
#endif

    while(1)
    {
        int btn = my_get_action(0);
        switch(btn)
        {
#if IMX233_SUBTARGET >= 3700 && defined(IMX233_BUTTON_LRADC_VDDIO)
            case ACT_PREV:
                vddio_val -= 100; /* mV */
                vddio_brownout -= 100; /* mV */
                imx233_power_set_regulator(REGULATOR_VDDIO, vddio_val, vddio_brownout);
                break;
            case ACT_NEXT:
                vddio_val += 100; /* mV */
                vddio_brownout += 100; /* mV */
                imx233_power_set_regulator(REGULATOR_VDDIO, vddio_val, vddio_brownout);
                break;
#endif
            case ACT_OK:
#if IMX233_SUBTARGET >= 3700 && defined(IMX233_BUTTON_LRADC_VDDIO)
                imx233_power_set_regulator(REGULATOR_VDDIO, orig_vddio_val, orig_vddio_brownout);
#endif
                lcd_setfont(FONT_UI);
                return true;
            case ACT_CANCEL:
#if IMX233_SUBTARGET >= 3700 && defined(IMX233_BUTTON_LRADC_VDDIO)
                imx233_power_set_regulator(REGULATOR_VDDIO, orig_vddio_val, orig_vddio_brownout);
#endif
                lcd_setfont(FONT_UI);
                return false;
        }

        lcd_clear_display();
        int line = 0;

#ifdef HAVE_BUTTON_DATA
        int data;
        btn = button_read_device(&data);
#else
        btn = button_read_device();
#endif
        lcd_putsf(0, line++, "raw buttons: %x", btn);
#ifdef IMX233_BUTTON_LRADC_CHANNEL
        lcd_putsf(0, line++, "raw lradc: %d", imx233_button_lradc_read_raw());
#ifdef IMX233_BUTTON_LRADC_VDDIO
        lcd_putsf(0, line++, "vddio: %d", imx233_button_lradc_read_vddio());
#endif
#endif
#ifdef HAS_BUTTON_HOLD
        lcd_putsf(0, line++, "hold: %d", button_hold());
#endif
#ifdef HAVE_HEADPHONE_DETECTION
        lcd_putsf(0, line++, "headphones: %d", headphones_inserted());
#endif
#ifdef HAVE_BUTTON_DATA
#ifdef HAVE_TOUCHSCREEN
        lcd_putsf(0, line++, "touch: x=%d y=%d", data >> 16, data & 0xffff);
#else
        lcd_putsf(0, line++, "data: %d", data);
#endif
#endif
        lcd_update();
        yield();
    }
}

static struct
{
    const char *name;
    bool (*fn)(void);
} debug_screens[] =
{
    {"clock", dbg_hw_info_clkctrl},
    {"dma", dbg_hw_info_dma},
    {"lradc", dbg_hw_info_lradc},
    {"power", dbg_hw_info_power},
    {"powermgmt", dbg_hw_info_powermgmt},
    {"rtc", dbg_hw_info_rtc},
    {"dcp", dbg_hw_info_dcp},
    {"pinctrl", dbg_hw_info_pinctrl},
    {"icoll", dbg_hw_info_icoll},
    {"ocotp", dbg_hw_info_ocotp},
    {"pwm", dbg_hw_info_pwm},
    {"usb", dbg_hw_info_usb},
    {"emi", dbg_hw_info_emi},
    {"audio", dbg_hw_info_audio},
    {"timrot", dbg_hw_info_timrot},
    {"button", dbg_hw_info_button},
    {"target", dbg_hw_target_info},
};

bool dbg_hw_info(void)
{
    int nr_lines = lcd_getheight() / font_get(lcd_getfont())->height;
    int len = ARRAYLEN(debug_screens);
    int top_visible = 0;
    int highlight = 0;
    while(1)
    {
        int button = my_get_action(HZ / 10);
        switch(button)
        {
            case ACT_NEXT:
                highlight = (highlight + 1) % len;
                break;
            case ACT_PREV:
                highlight = (highlight + len - 1) % len;
                break;
            case ACT_OK:
                // if the screen returns true, advance to next screen
                while(debug_screens[highlight].fn())
                    highlight = (highlight + 1) % len;
                lcd_setfont(FONT_UI);
                break;
            case ACT_CANCEL:
                return false;
        }
        // adjust top visible if needed
        if(highlight < top_visible)
            top_visible = highlight;
        else if(highlight >= top_visible + nr_lines)
            top_visible = highlight - nr_lines + 1;

        lcd_clear_display();

        for(int i = top_visible; i < len && i < top_visible + nr_lines; i++)
        {
            if(i == highlight)
            {
                lcd_set_foreground(LCD_BLACK);
                lcd_set_background(LCD_RGBPACK(255, 255, 0));
            }
            else
            {
                lcd_set_foreground(LCD_WHITE);
                lcd_set_background(LCD_BLACK);
            }
            lcd_putsf(0, i - top_visible, "%s", debug_screens[i].name);
        }
        lcd_set_foreground(LCD_WHITE);
        lcd_set_background(LCD_BLACK);

        lcd_update();
        yield();
    }
    return false;
}

bool dbg_ports(void)
{
    return false;
}