summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_jz47xx/debug-jz4760.c
blob: 105d3fa6908596a7bca01611dc28f5885a772cc5 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2018 by Solomon Peachy
 *
 * 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 "config.h"
#include "system.h"
#include "cpu.h"
#include <stdarg.h>
#include <stdio.h>
#include "lcd.h"
#include "kernel.h"
#include "font.h"
#include "button.h"
#include "timefuncs.h"

#ifndef BOOTLOADER
#include "action.h"

static int line = 0;

/*
 * Clock Generation Module
 */
#define TO_MHZ(x) ((x)/1000000), ((x)%1000000)/10000
#define TO_KHZ(x) ((x)/1000), ((x)%1000)/10

#define DEBUG_CANCEL       ACTION_STD_CANCEL
#define DEBUG_NEXT         ACTION_STD_NEXT
#define DEBUG_LEFT_JUSTIFY ACTION_STD_OK
#define DEBUG_LEFT_SCROLL  ACTION_STD_MENU

/* if the possiblity exists to divide by zero protect with this macro */
#define DIV_FINITE(dividend, divisor) ((divisor == 0)? divisor : dividend/divisor)

#define ON "Enabled"
#define OFF "Disabled"

static bool dbg_btn(bool *done, int *x)
{
    bool cont = !*done;
    if (cont)
    {
        lcd_update();
        int button = get_action(CONTEXT_STD,HZ/10);
        switch(button)
        {
            case DEBUG_CANCEL:
                *done = true;
            case DEBUG_NEXT:
                cont = false;
            case DEBUG_LEFT_JUSTIFY:
                (*x) = 0;
                sleep(HZ/5);
                break;
            case DEBUG_LEFT_SCROLL:
                (*x)--;
        }
    }
    lcd_clear_display();
    return cont;
}

static void display_clocks(void)
{
    unsigned int cppcr0 = REG_CPM_CPPCR0;  /* PLL Control Register */
    unsigned int cppcr1 = REG_CPM_CPPCR1;  /* PLL Control Register */
    unsigned int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
    unsigned int od[4] = {1, 2, 4, 8};

    lcd_putsf(0, line++, "CPPCR0:0x%08x", cppcr0);
    if (cppcr0 & CPPCR0_PLLEN) {
        lcd_putsf(0, line++, "PLL0  :%3d.%02d MHz", TO_MHZ(__cpm_get_pllout()));
        lcd_putsf(0, line++, "m:n:o :%d:%d:%d",
                __cpm_get_pllm(),
                __cpm_get_plln(),
                od[__cpm_get_pllod()]
            );
    }
    lcd_putsf(0, line++, "CPPCR1:0x%08x", cppcr1);
    if (cppcr1 & CPPCR1_PLL1EN) {
        lcd_putsf(0, line++, "PLL1  :%3d.%02d MHz", TO_MHZ(__cpm_get_pll1out()));
        lcd_putsf(0, line++, "m:n:o :%d:%d:%d",
            __cpm_get_pll1m(),
            __cpm_get_pll1n(),
            od[__cpm_get_pll1od()]
        );
    }
    lcd_putsf(0, line++, "C:H:M:P:%d:%d:%d:%d",
            div[__cpm_get_cdiv()],
            div[__cpm_get_hdiv()],
            div[__cpm_get_mdiv()],
            div[__cpm_get_pdiv()]
        );
    lcd_putsf(0, line++, "I:P:M : %d:%d:%d",
            __cpm_get_i2sdiv()+1,
            __cpm_get_pixdiv()+1,
            __cpm_get_mscdiv()+1
        );
    lcd_putsf(0, line++, "CCLK  :%3d.%02d MHz", TO_MHZ(__cpm_get_cclk()));
    lcd_putsf(0, line++, "HCLK  :%3d.%02d MHz", TO_MHZ(__cpm_get_hclk()));
    lcd_putsf(0, line++, "MCLK  :%3d.%02d MHz", TO_MHZ(__cpm_get_mclk()));
    lcd_putsf(0, line++, "PCLK  :%3d.%02d MHz", TO_MHZ(__cpm_get_pclk()));
    lcd_putsf(0, line++, "PIXCLK:%6d.%02d KHz", TO_KHZ(__cpm_get_pixclk()));
    lcd_putsf(0, line++, "I2SCLK:%3d.%02d MHz", TO_MHZ(__cpm_get_i2sclk()));
    lcd_putsf(0, line++, "MSCCLK:%3d.%02d MHz", TO_MHZ(__cpm_get_mscclk()));
    lcd_putsf(0, line++, "EXTALCLK:%3d.%02d MHz", TO_MHZ(__cpm_get_extalclk()));
    lcd_putsf(0, line++, "RTCCLK:%3d.%02d KHz", TO_KHZ(__cpm_get_rtcclk()));
}

static void display_enabled_clocks(void)
{
    unsigned long lcr = REG_CPM_LCR;
    unsigned long clkgr0 = REG_CPM_CLKGR0;

    lcd_putsf(0, line++, "Low Power Mode : %s",
            ((lcr & LCR_LPM_MASK) == LCR_LPM_IDLE) ?
            "IDLE" : (((lcr & LCR_LPM_MASK) == LCR_LPM_SLEEP) ? "SLEEP" : "HIBERNATE")
          );

    lcd_putsf(0, line++, "Doze Mode      : %s",
            (lcr & LCR_DOZE) ? "ON" : "OFF");
    if (lcr & LCR_DOZE)
        lcd_putsf(0, line++, "     duty      : %d", (int)((lcr & LCR_DUTY_MASK) >> LCR_DUTY_LSB));

    lcd_putsf(0, line++, "IPU   : %s",
            (clkgr0 & CLKGR0_IPU) ? "stopped" : "running");
    lcd_putsf(0, line++, "DMAC  : %s",
            (clkgr0 & CLKGR0_DMAC) ? "stopped" : "running");
    lcd_putsf(0, line++, "UHC   : %s",
            (clkgr0 & CLKGR0_UHC) ? "stopped" : "running");
    lcd_putsf(0, line++, "LCD   : %s",
            (clkgr0 & CLKGR0_LCD) ? "stopped" : "running");
    lcd_putsf(0, line++, "CIM   : %s",
            (clkgr0 & CLKGR0_CIM) ? "stopped" : "running");
    lcd_putsf(0, line++, "SADC  : %s",
            (clkgr0 & CLKGR0_SADC) ? "stopped" : "running");
    lcd_putsf(0, line++, "MSC0  : %s",
            (clkgr0 & CLKGR0_MSC0) ? "stopped" : "running");
    lcd_putsf(0, line++, "MSC1  : %s",
            (clkgr0 & CLKGR0_MSC1) ? "stopped" : "running");
    lcd_putsf(0, line++, "MSC2  : %s",
            (clkgr0 & CLKGR0_MSC2) ? "stopped" : "running");
    lcd_putsf(0, line++, "AIC   : %s",
            (clkgr0 & CLKGR0_AIC) ? "stopped" : "running");
    lcd_putsf(0, line++, "SSI1  : %s",
            (clkgr0 & CLKGR0_SSI1) ? "stopped" : "running");
    lcd_putsf(0, line++, "SSI2  : %s",
            (clkgr0 & CLKGR0_SSI2) ? "stopped" : "running");
    lcd_putsf(0, line++, "I2C0  : %s",
            (clkgr0 & CLKGR0_I2C0) ? "stopped" : "running");
    lcd_putsf(0, line++, "I2C1  : %s",
            (clkgr0 & CLKGR0_I2C1) ? "stopped" : "running");
    lcd_putsf(0, line++, "UART1 : %s",
            (clkgr0 & CLKGR0_UART1) ? "stopped" : "running");
    lcd_putsf(0, line++, "UART0 : %s",
            (clkgr0 & CLKGR0_UART0) ? "stopped" : "running");
}

bool dbg_ports(void)
{
#if CONFIG_CPU == JZ4760B
    int line, i, j, cur;
    int x = 0;
    const int last_port = 5;
    bool done = false;

    long data, dir;
    long fun, intr;
    long lvl;


    lcd_clear_display();
    lcd_setfont(FONT_SYSFIXED);

    while(!done)
    {
        i = 0;
        while(dbg_btn(&done, &x))
        {
            i %= last_port; /*PORT: A B C D E F */
            while(dbg_btn(&done, &x))
            {
                line = 0;
                lcd_puts(x, line++, "[GPIO Vals and Dirs]");
                for (j = i; j < i + 2; j++)
                {
                    cur = j % last_port;
                    dir = REG_GPIO_PXDIR(cur);
                    data = REG_GPIO_PXDAT(cur);
                    fun = REG_GPIO_PXFUN(cur);
                    intr = REG_GPIO_PXIM(cur);
                    lvl  = REG_GPIO_PXPIN(cur);

                    lcd_putsf(x, line++, "[%s%c]: %8x", "GPIO", 'A' + cur, lvl);
                    lcd_putsf(x, line++, "DIR: %8x FUN: %8x", dir, fun);
                    lcd_putsf(x, line++, "DAT: %8x INT: %8x", data, intr);
                    line++;
                }
            }
            i++;
        }

    }
    lcd_setfont(FONT_UI);
#endif

    return false;
}

extern uint32_t irqstackend,irqstackbegin;

bool dbg_hw_info(void)
{
    int btn = 0;
#ifdef HAVE_TOUCHSCREEN
    int touch;
#endif
    struct tm *cur_time;

    lcd_setfont(FONT_UI);
    while(btn ^ BUTTON_POWER)
    {
        lcd_clear_display();
        line = 0;

        uint32_t *ptr = &irqstackbegin;
        for ( ; ptr < &irqstackend && *ptr == 0xDEADBEEF; ptr++) {}

        lcd_putsf(0, line++, "IRQ stack max: %d", (uint32_t)&irqstackend - (uint32_t)ptr);

        display_clocks();
        display_enabled_clocks();
#ifdef HAVE_TOUCHSCREEN
        btn = button_read_device(&touch);
        lcd_putsf(0, line++, "X: %d Y: %d BTN: 0x%X", touch>>16, touch&0xFFFF, btn);
#else
        btn = button_read_device();
#endif
        cur_time = get_time();
        lcd_putsf(0, line++, "%02d/%02d/%04d %02d:%02d:%02d", cur_time->tm_mday,
               cur_time->tm_mon, cur_time->tm_year, cur_time->tm_hour,
               cur_time->tm_min, cur_time->tm_sec);
        lcd_update();
        sleep(HZ/16);
    }
    return true;
}
#endif

#ifdef BOOTLOADER
#define WITH_SERIAL
#endif

#ifdef WITH_SERIAL
void serial_putc (const char c)
{
        volatile u8 *uart_lsr = (volatile u8 *)(CFG_UART_BASE + OFF_LSR);
        volatile u8 *uart_tdr = (volatile u8 *)(CFG_UART_BASE + OFF_TDR);

        if (c == '\n') serial_putc ('\r');

        /* Wait for fifo to shift out some bytes */
        while ( !((*uart_lsr & (UARTLSR_TDRQ | UARTLSR_TEMT)) == 0x60) );

        *uart_tdr = (u8)c;
}

void serial_puts (const char *s)
{
        while (*s) {
                serial_putc (*s++);
        }
}

void serial_putsf(const char *format, ...)
{
    static char printfbuf[256];
    int len;
    unsigned char *ptr;
    va_list ap;
    va_start(ap, format);

    ptr = printfbuf;
    len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
    va_end(ap);
    (void)len;

    serial_puts(ptr);
    serial_putc('\n');
}

void serial_put_hex(unsigned int  d)
{
        char c[12];
        int i;
        for(i = 0; i < 8;i++)
        {
                c[i] = (d >> ((7 - i) * 4)) & 0xf;
                if(c[i] < 10)
                        c[i] += 0x30;
                else
                        c[i] += (0x41 - 10);
        }
        c[8] = '\n';
        c[9] = 0;
        serial_puts(c);

}
void serial_put_dec(unsigned int  d)
{
        char c[16];
        int i;
        int j = 0;
        int x = d;

        while (x /= 10)
                j++;

        for (i = j; i >= 0; i--) {
                c[i] = d % 10;
                c[i] += 0x30;
                d /= 10;
        }
        c[j + 1] = '\n';
        c[j + 2] = 0;
        serial_puts(c);
}

void serial_dump_data(unsigned char* data, int len)
{
        int i;
        for(i=0; i<len; i++)
        {
                unsigned char a = ((*data)>>4) & 0xf;
                if(a < 10)
                        a += 0x30;
                else
                        a += (0x41 - 10);
                serial_putc( a );

                a = (*data) & 0xf;
                if(a < 10)
                        a += 0x30;
                else
                        a += (0x41 - 10);
                serial_putc( a );

                serial_putc( ' ' );

                data++;
        }

        serial_putc( '\n' );
}
#endif