blob: 558c1ceb2b93fb046b96971235af929a00247dc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Milton Miller7f853352005-09-06 11:56:02 +10002 * polling mode stateless debugging stuff, originally for NS16550 Serial Ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * c 2001 PPC 64 Team, IBM Corp
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/config.h>
14#include <linux/types.h>
Milton Miller188d2ce2005-09-06 11:57:00 +100015#include <linux/sched.h>
Milton Miller8d927392005-09-06 11:57:27 +100016#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +110019void (*udbg_putc)(char c);
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110020int (*udbg_getc)(void);
Milton Millerc8f1c8b2005-09-06 11:56:42 +100021int (*udbg_getc_poll)(void);
22
Milton Miller8d927392005-09-06 11:57:27 +100023/* udbg library, used by xmon et al */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024void udbg_puts(const char *s)
25{
Milton Millerc8f1c8b2005-09-06 11:56:42 +100026 if (udbg_putc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 char c;
28
29 if (s && *s != '\0') {
30 while ((c = *s++) != '\0')
Milton Millerc8f1c8b2005-09-06 11:56:42 +100031 udbg_putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 }
33 }
34#if 0
35 else {
36 printk("%s", s);
37 }
38#endif
39}
40
41int udbg_write(const char *s, int n)
42{
43 int remain = n;
44 char c;
45
Milton Millerc8f1c8b2005-09-06 11:56:42 +100046 if (!udbg_putc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return 0;
48
49 if (s && *s != '\0') {
50 while (((c = *s++) != '\0') && (remain-- > 0)) {
Milton Millerc8f1c8b2005-09-06 11:56:42 +100051 udbg_putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53 }
54
55 return n - remain;
56}
57
58int udbg_read(char *buf, int buflen)
59{
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110060 char *p = buf;
61 int i, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Milton Millerc8f1c8b2005-09-06 11:56:42 +100063 if (!udbg_getc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65
66 for (i = 0; i < buflen; ++i) {
67 do {
Milton Millerc8f1c8b2005-09-06 11:56:42 +100068 c = udbg_getc();
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110069 if (c == -1 && i == 0)
70 return -1;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 } while (c == 0x11 || c == 0x13);
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110073 if (c == 0 || c == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 *p++ = c;
76 }
77
78 return i;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define UDBG_BUFSIZE 256
82void udbg_printf(const char *fmt, ...)
83{
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +110084 char buf[UDBG_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 va_list args;
86
87 va_start(args, fmt);
88 vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
89 udbg_puts(buf);
90 va_end(args);
91}
92
Kumar Galabe6b8432005-12-20 16:37:07 -060093void __init udbg_progress(char *s, unsigned short hex)
94{
95 udbg_puts(s);
96 udbg_puts("\n");
97}
98
Milton Miller8d927392005-09-06 11:57:27 +100099/*
100 * Early boot console based on udbg
101 */
102static void udbg_console_write(struct console *con, const char *s,
103 unsigned int n)
104{
105 udbg_write(s, n);
106}
107
108static struct console udbg_console = {
109 .name = "udbg",
110 .write = udbg_console_write,
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100111 .flags = CON_PRINTBUFFER | CON_ENABLED,
Milton Miller8d927392005-09-06 11:57:27 +1000112 .index = -1,
113};
114
Stephen Rothwell38c0ff02005-09-07 19:52:38 +1000115static int early_console_initialized;
116
Milton Miller8d927392005-09-06 11:57:27 +1000117void __init disable_early_printk(void)
118{
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100119#if 1
Stephen Rothwell38c0ff02005-09-07 19:52:38 +1000120 if (!early_console_initialized)
121 return;
Milton Miller8d927392005-09-06 11:57:27 +1000122 unregister_console(&udbg_console);
Stephen Rothwell38c0ff02005-09-07 19:52:38 +1000123 early_console_initialized = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100124#endif
Milton Miller8d927392005-09-06 11:57:27 +1000125}
126
127/* called by setup_system */
128void register_early_udbg_console(void)
129{
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100130 if (early_console_initialized)
131 return;
Stephen Rothwell38c0ff02005-09-07 19:52:38 +1000132 early_console_initialized = 1;
Milton Miller8d927392005-09-06 11:57:27 +1000133 register_console(&udbg_console);
134}
135
136#if 0 /* if you want to use this as a regular output console */
137console_initcall(register_udbg_console);
138#endif