From 07af47eca912f3b56f013749a268718d78656961 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Thu, 26 Aug 2021 20:41:45 +0300 Subject: Added debug flags and simple debug serial driver --- common/debug.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 common/debug.c (limited to 'common/debug.c') diff --git a/common/debug.c b/common/debug.c new file mode 100644 index 0000000..be71cd2 --- /dev/null +++ b/common/debug.c @@ -0,0 +1,77 @@ +#include +#include + +#ifdef DEBUG + +/* if there arises a need for more supported serial drivers, I should probably + * try to implement some kind of basic driver subsystem, but this is good enough + * for now. */ + +struct __packed ns16550a { + union { + struct { + uint8_t data; + uint8_t irq; + }; + + struct { + uint8_t lsbr; + uint8_t msbr; + }; + }; + + uint8_t irq_id; + uint8_t lcr; + uint8_t mcr; + uint8_t lsr; + uint8_t msr; + uint8_t scr; +}; + +#define LSR_DR (1 << 0) +#define LSR_OE (1 << 1) +#define LSR_PE (1 << 2) +#define LSR_FE (1 << 3) +#define LSR_BI (1 << 4) +#define LSR_THRE (1 << 5) +#define LSR_TEMT (1 << 6) +#define LSR_ERR (1 << 7) + +static struct ns16550a *port = 0; + +int __serial_tx_empty() +{ + return port->lsr & LSR_THRE; +} + +void __serial_putchar(char c) +{ + if (!port) + return; + + while(__serial_tx_empty() == 0); + + port->data = c; +} + +void dbg_init(void *pt, enum serial_dev_t dev) +{ + switch (dev) { + case NS16550A: + port = pt; + break; + } + + /* in the future possibly configure the serial connection, though the + * defaults (set by U-boot) seem to work alright */ +} + +void dbg(const char *fmt, ...) +{ + /* do a proper implementation later, for now just dump stuff */ + while(*fmt){ + __serial_putchar(*fmt++); + } +} + +#endif /* DEBUG */ -- cgit v1.3