1 /*
2  * NS16550 Serial Port (uart) debugging stuff.
3  *
4  * c 2001 PPC 64 Team, IBM Corp
5  *
6  * NOTE: I am trying to make this code avoid any static data references to
7  *  simplify debugging early boot.  We'll see how that goes...
8  *
9  * To use this call udbg_init() first.  It will init the uart to 9600 8N1.
10  * You may need to update the COM1 define if your uart is at a different addr.
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17 
18 #include <stdarg.h>
19 #define WANT_PPCDBG_TAB /* Only defined here */
20 #include <asm/ppcdebug.h>
21 #include <asm/processor.h>
22 #include <asm/naca.h>
23 #include <asm/uaccess.h>
24 #include <asm/machdep.h>
25 
26 struct NS16550 {
27 	/* this struct must be packed */
28 	unsigned char rbr;  /* 0 */
29 	unsigned char ier;  /* 1 */
30 	unsigned char fcr;  /* 2 */
31 	unsigned char lcr;  /* 3 */
32 	unsigned char mcr;  /* 4 */
33 	unsigned char lsr;  /* 5 */
34 	unsigned char msr;  /* 6 */
35 	unsigned char scr;  /* 7 */
36 };
37 
38 #define thr rbr
39 #define iir fcr
40 #define dll rbr
41 #define dlm ier
42 #define dlab lcr
43 
44 #define LSR_DR   0x01  /* Data ready */
45 #define LSR_OE   0x02  /* Overrun */
46 #define LSR_PE   0x04  /* Parity error */
47 #define LSR_FE   0x08  /* Framing error */
48 #define LSR_BI   0x10  /* Break */
49 #define LSR_THRE 0x20  /* Xmit holding register empty */
50 #define LSR_TEMT 0x40  /* Xmitter empty */
51 #define LSR_ERR  0x80  /* Error */
52 
53 volatile struct NS16550 *udbg_comport;
54 
55 void
udbg_init_uart(void * comport)56 udbg_init_uart(void *comport)
57 {
58 	if (comport) {
59 		udbg_comport = (struct NS16550 *)comport;
60 		udbg_comport->lcr = 0x00; eieio();
61 		udbg_comport->ier = 0xFF; eieio();
62 		udbg_comport->ier = 0x00; eieio();
63 		udbg_comport->lcr = 0x80; eieio();	/* Access baud rate */
64 		udbg_comport->dll = 12;   eieio();	/* 1 = 115200,  2 = 57600, 3 = 38400, 12 = 9600 baud */
65 		udbg_comport->dlm = 0;    eieio();	/* dll >> 8 which should be zero for fast rates; */
66 		udbg_comport->lcr = 0x03; eieio();	/* 8 data, 1 stop, no parity */
67 		udbg_comport->mcr = 0x03; eieio();	/* RTS/DTR */
68 		udbg_comport->fcr = 0x07; eieio();	/* Clear & enable FIFOs */
69 	}
70 }
71 
72 void
udbg_putc(unsigned char c)73 udbg_putc(unsigned char c)
74 {
75 	if ( udbg_comport ) {
76 		while ((udbg_comport->lsr & LSR_THRE) == 0)
77 			/* wait for idle */;
78 		udbg_comport->thr = c; eieio();
79 		if (c == '\n') {
80 			/* Also put a CR.  This is for convenience. */
81 			while ((udbg_comport->lsr & LSR_THRE) == 0)
82 				/* wait for idle */;
83 			udbg_comport->thr = '\r'; eieio();
84 		}
85 	} else if (systemcfg->platform == PLATFORM_ISERIES_LPAR) {
86 		/* ToDo: switch this via ppc_md */
87 		printk("%c", c);
88 	}
89 }
90 
udbg_getc_poll(void)91 int udbg_getc_poll(void)
92 {
93 	if (udbg_comport) {
94 		if ((udbg_comport->lsr & LSR_DR) != 0)
95 			return udbg_comport->rbr;
96 		else
97 			return -1;
98 	}
99 	return -1;
100 }
101 
102 unsigned char
udbg_getc(void)103 udbg_getc(void)
104 {
105 	if ( udbg_comport ) {
106 		while ((udbg_comport->lsr & LSR_DR) == 0)
107 			/* wait for char */;
108 		return udbg_comport->rbr;
109 	}
110 	return 0;
111 }
112 
113 void
udbg_puts(const char * s)114 udbg_puts(const char *s)
115 {
116 	if (ppc_md.udbg_putc) {
117 		char c;
118 
119 		if (s && *s != '\0') {
120 			while ((c = *s++) != '\0')
121 				ppc_md.udbg_putc(c);
122 		}
123 	} else {
124 		printk("%s", s);
125 	}
126 }
127 
128 int
udbg_write(const char * s,int n)129 udbg_write(const char *s, int n)
130 {
131 	int remain = n;
132 	char c;
133 
134 	if (!ppc_md.udbg_putc)
135 		return 0;
136 
137 	if ( s && *s != '\0' ) {
138 		while ( (( c = *s++ ) != '\0') && (remain-- > 0)) {
139 			ppc_md.udbg_putc(c);
140 		}
141 	}
142 	return n - remain;
143 }
144 
145 int
udbg_read(char * buf,int buflen)146 udbg_read(char *buf, int buflen) {
147 	char c, *p = buf;
148 	int i;
149 	if (!ppc_md.udbg_putc)
150 		for (;;);	/* stop here for cpuctl */
151 	for (i = 0; i < buflen; ++i) {
152 		do {
153 			c = ppc_md.udbg_getc();
154 		} while (c == 0x11 || c == 0x13);
155 		*p++ = c;
156 	}
157 	return i;
158 }
159 
160 void
udbg_console_write(struct console * con,const char * s,unsigned int n)161 udbg_console_write(struct console *con, const char *s, unsigned int n)
162 {
163 	udbg_write(s, n);
164 }
165 
166 void
udbg_puthex(unsigned long val)167 udbg_puthex(unsigned long val)
168 {
169 	int i, nibbles = sizeof(val)*2;
170 	unsigned char buf[sizeof(val)*2+1];
171 	for (i = nibbles-1;  i >= 0;  i--) {
172 		buf[i] = (val & 0xf) + '0';
173 		if (buf[i] > '9')
174 		    buf[i] += ('a'-'0'-10);
175 		val >>= 4;
176 	}
177 	buf[nibbles] = '\0';
178 	udbg_puts(buf);
179 }
180 
181 void
udbg_printSP(const char * s)182 udbg_printSP(const char *s)
183 {
184 	if (systemcfg->platform == PLATFORM_PSERIES) {
185 		unsigned long sp;
186 		asm("mr %0,1" : "=r" (sp) :);
187 		if (s)
188 			udbg_puts(s);
189 		udbg_puthex(sp);
190 	}
191 }
192 
193 void
udbg_printf(const char * fmt,...)194 udbg_printf(const char *fmt, ...)
195 {
196 	unsigned char buf[256];
197 
198 	va_list args;
199 	va_start(args, fmt);
200 
201 	vsprintf(buf, fmt, args);
202 	udbg_puts(buf);
203 
204 	va_end(args);
205 }
206 
207 /* Special print used by PPCDBG() macro */
208 void
udbg_ppcdbg(unsigned long debug_flags,const char * fmt,...)209 udbg_ppcdbg(unsigned long debug_flags, const char *fmt, ...)
210 {
211 	unsigned long active_debugs = debug_flags & naca->debug_switch;
212 
213 	if ( active_debugs ) {
214 		va_list ap;
215 		unsigned char buf[256];
216 		unsigned long i, len = 0;
217 
218 		for(i=0; i < PPCDBG_NUM_FLAGS ;i++) {
219 			if (((1U << i) & active_debugs) &&
220 			    trace_names[i]) {
221 				len += strlen(trace_names[i]);
222 				udbg_puts(trace_names[i]);
223 				break;
224 			}
225 		}
226 		sprintf(buf, " [%s]: ", current->comm);
227 		len += strlen(buf);
228 		udbg_puts(buf);
229 
230 		while(len < 18) {
231 			udbg_puts(" ");
232 			len++;
233 		}
234 
235 		va_start(ap, fmt);
236 		vsprintf(buf, fmt, ap);
237 		udbg_puts(buf);
238 
239 		va_end(ap);
240 	}
241 }
242 
243 unsigned long
udbg_ifdebug(unsigned long flags)244 udbg_ifdebug(unsigned long flags)
245 {
246 	return (flags & naca->debug_switch);
247 }
248