blob: 83990326099b17e1b9c94defbb561678bc9ee724 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68k/mac/debug.c
3 *
4 * Shamelessly stolen (SCC code and general framework) from:
5 *
6 * linux/arch/m68k/atari/debug.c
7 *
8 * Atari debugging and serial console stuff
9 *
10 * Assembled of parts of former atari/config.c 97-12-18 by Roman Hodek
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file COPYING in the main directory of this archive
14 * for more details.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/sched.h>
19#include <linux/tty.h>
20#include <linux/console.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23
24#define BOOTINFO_COMPAT_1_0
25#include <asm/setup.h>
26#include <asm/bootinfo.h>
27#include <asm/machw.h>
28#include <asm/macints.h>
29
30extern char m68k_debug_device[];
31
32extern struct compat_bootinfo compat_boot_info;
33
34extern unsigned long mac_videobase;
35extern unsigned long mac_videodepth;
36extern unsigned long mac_rowbytes;
37
38extern void mac_serial_print(const char *);
39
40#define DEBUG_HEADS
41#undef DEBUG_SCREEN
42#define DEBUG_SERIAL
43
44/*
45 * These two auxiliary debug functions should go away ASAP. Only usage:
46 * before the console output is up (after head.S come some other crucial
47 * setup routines :-) it permits writing 'data' to the screen as bit patterns
48 * (good luck reading those). Helped to figure that the bootinfo contained
49 * garbage data on the amount and size of memory chunks ...
50 *
51 * The 'pos' argument now simply means 'linefeed after print' ...
52 */
53
54#ifdef DEBUG_SCREEN
Roman Zippel6ff58012007-05-01 22:32:43 +020055static int peng, line;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
58void mac_debugging_short(int pos, short num)
59{
60#ifdef DEBUG_SCREEN
61 unsigned char *pengoffset;
62 unsigned char *pptr;
63 int i;
64#endif
65
66#ifdef DEBUG_SERIAL
67 printk("debug: %d !\n", num);
68#endif
69
70#ifdef DEBUG_SCREEN
71 if (!MACH_IS_MAC) {
72 /* printk("debug: %d !\n", num); */
73 return;
74 }
75
76 /* calculate current offset */
Roman Zippel6ff58012007-05-01 22:32:43 +020077 pengoffset = (unsigned char *)mac_videobase +
78 (150+line*2) * mac_rowbytes) + 80 * peng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Roman Zippel6ff58012007-05-01 22:32:43 +020080 pptr = pengoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Roman Zippel6ff58012007-05-01 22:32:43 +020082 for (i = 0; i < 8 * sizeof(short); i++) { /* # of bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* value mask for bit i, reverse order */
Roman Zippel6ff58012007-05-01 22:32:43 +020084 *pptr++ = (num & (1 << (8*sizeof(short)-i-1)) ? 0xFF : 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 peng++;
88
89 if (pos) {
90 line++;
91 peng = 0;
92 }
93#endif
94}
95
96void mac_debugging_long(int pos, long addr)
97{
98#ifdef DEBUG_SCREEN
99 unsigned char *pengoffset;
100 unsigned char *pptr;
101 int i;
102#endif
103
104#ifdef DEBUG_SERIAL
105 printk("debug: #%ld !\n", addr);
106#endif
107
108#ifdef DEBUG_SCREEN
109 if (!MACH_IS_MAC) {
110 /* printk("debug: #%ld !\n", addr); */
111 return;
112 }
113
114 pengoffset=(unsigned char *)(mac_videobase+(150+line*2)*mac_rowbytes)
115 +80*peng;
116
Roman Zippel6ff58012007-05-01 22:32:43 +0200117 pptr = pengoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Roman Zippel6ff58012007-05-01 22:32:43 +0200119 for (i = 0; i < 8 * sizeof(long); i++) { /* # of bits */
120 *pptr++ = (addr & (1 << (8*sizeof(long)-i-1)) ? 0xFF : 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 peng++;
124
125 if (pos) {
126 line++;
127 peng = 0;
128 }
129#endif
130}
131
132#ifdef DEBUG_SERIAL
133/*
134 * TODO: serial debug code
135 */
136
Roman Zippel6ff58012007-05-01 22:32:43 +0200137struct mac_SCC {
138 u_char cha_b_ctrl;
139 u_char char_dummy1;
140 u_char cha_a_ctrl;
141 u_char char_dummy2;
142 u_char cha_b_data;
143 u_char char_dummy3;
144 u_char cha_a_data;
145};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147# define scc (*((volatile struct mac_SCC*)mac_bi_data.sccbase))
148
149/* Flag that serial port is already initialized and used */
150int mac_SCC_init_done;
151/* Can be set somewhere, if a SCC master reset has already be done and should
152 * not be repeated; used by kgdb */
153int mac_SCC_reset_done;
154
155static int scc_port = -1;
156
157static struct console mac_console_driver = {
Roman Zippel6ff58012007-05-01 22:32:43 +0200158 .name = "debug",
159 .flags = CON_PRINTBUFFER,
160 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163/*
164 * Crude hack to get console output to the screen before the framebuffer
165 * is initialized (happens a lot later in 2.1!).
166 * We just use the console routines declared in head.S, this will interfere
167 * with regular framebuffer console output and should be used exclusively
168 * to debug kernel problems manifesting before framebuffer init (aka WSOD)
169 *
170 * To keep this hack from interfering with the regular console driver, either
171 * deregister this driver before/on framebuffer console init, or silence this
172 * function after the fbcon driver is running (will lose console messages!?).
173 * To debug real early bugs, need to write a 'mac_register_console_hack()'
174 * that is called from start_kernel() before setup_arch() and just registers
175 * this driver if Mac.
176 */
177
Roman Zippel6ff58012007-05-01 22:32:43 +0200178void mac_debug_console_write(struct console *co, const char *str,
179 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 mac_serial_print(str);
182}
183
184
185
186/* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
187
188#define uSEC 1
189
Roman Zippel6ff58012007-05-01 22:32:43 +0200190static inline void mac_sccb_out(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Roman Zippel6ff58012007-05-01 22:32:43 +0200192 int i;
193
194 do {
195 for (i = uSEC; i > 0; --i)
196 barrier();
197 } while (!(scc.cha_b_ctrl & 0x04)); /* wait for tx buf empty */
198 for (i = uSEC; i > 0; --i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 barrier();
Roman Zippel6ff58012007-05-01 22:32:43 +0200200 scc.cha_b_data = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Roman Zippel6ff58012007-05-01 22:32:43 +0200203static inline void mac_scca_out(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Roman Zippel6ff58012007-05-01 22:32:43 +0200205 int i;
206
207 do {
208 for (i = uSEC; i > 0; --i)
209 barrier();
210 } while (!(scc.cha_a_ctrl & 0x04)); /* wait for tx buf empty */
211 for (i = uSEC; i > 0; --i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 barrier();
Roman Zippel6ff58012007-05-01 22:32:43 +0200213 scc.cha_a_data = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Roman Zippel6ff58012007-05-01 22:32:43 +0200216void mac_sccb_console_write(struct console *co, const char *str,
217 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Roman Zippel6ff58012007-05-01 22:32:43 +0200219 while (count--) {
220 if (*str == '\n')
221 mac_sccb_out('\r');
222 mac_sccb_out(*str++);
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Roman Zippel6ff58012007-05-01 22:32:43 +0200226void mac_scca_console_write(struct console *co, const char *str,
227 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Roman Zippel6ff58012007-05-01 22:32:43 +0200229 while (count--) {
230 if (*str == '\n')
231 mac_scca_out('\r');
232 mac_scca_out(*str++);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236
237/* The following two functions do a quick'n'dirty initialization of the MFP or
238 * SCC serial ports. They're used by the debugging interface, kgdb, and the
239 * serial console code. */
240#define SCCB_WRITE(reg,val) \
Roman Zippel6ff58012007-05-01 22:32:43 +0200241 do { \
242 int i; \
243 scc.cha_b_ctrl = (reg); \
244 for (i = uSEC; i > 0; --i) \
245 barrier(); \
246 scc.cha_b_ctrl = (val); \
247 for (i = uSEC; i > 0; --i) \
248 barrier(); \
249 } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#define SCCA_WRITE(reg,val) \
Roman Zippel6ff58012007-05-01 22:32:43 +0200252 do { \
253 int i; \
254 scc.cha_a_ctrl = (reg); \
255 for (i = uSEC; i > 0; --i) \
256 barrier(); \
257 scc.cha_a_ctrl = (val); \
258 for (i = uSEC; i > 0; --i) \
259 barrier(); \
260 } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/* loops_per_jiffy isn't initialized yet, so we can't use udelay(). This does a
263 * delay of ~ 60us. */
264/* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
Roman Zippel6ff58012007-05-01 22:32:43 +0200265#define LONG_DELAY() \
266 do { \
267 int i; \
268 for (i = 60*uSEC; i > 0; --i) \
269 barrier(); \
270 } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272#ifndef CONFIG_SERIAL_CONSOLE
Roman Zippel6ff58012007-05-01 22:32:43 +0200273static void __init mac_init_scc_port(int cflag, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#else
Roman Zippel6ff58012007-05-01 22:32:43 +0200275void mac_init_scc_port(int cflag, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif
277{
278 extern int mac_SCC_reset_done;
279
280 /*
281 * baud rates: 1200, 1800, 2400, 4800, 9600, 19.2k, 38.4k, 57.6k, 115.2k
282 */
283
284 static int clksrc_table[9] =
285 /* reg 11: 0x50 = BRG, 0x00 = RTxC, 0x28 = TRxC */
286 { 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00, 0x00 };
287 static int clkmode_table[9] =
288 /* reg 4: 0x40 = x16, 0x80 = x32, 0xc0 = x64 */
289 { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x80 };
290 static int div_table[9] =
291 /* reg12 (BRG low) */
292 { 94, 62, 46, 22, 10, 4, 1, 0, 0 };
293
Roman Zippel6ff58012007-05-01 22:32:43 +0200294 int baud = cflag & CBAUD;
295 int clksrc, clkmode, div, reg3, reg5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Roman Zippel6ff58012007-05-01 22:32:43 +0200297 if (cflag & CBAUDEX)
298 baud += B38400;
299 if (baud < B1200 || baud > B38400+2)
300 baud = B9600; /* use default 9600bps for non-implemented rates */
301 baud -= B1200; /* tables starts at 1200bps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Roman Zippel6ff58012007-05-01 22:32:43 +0200303 clksrc = clksrc_table[baud];
304 clkmode = clkmode_table[baud];
305 div = div_table[baud];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Roman Zippel6ff58012007-05-01 22:32:43 +0200307 reg3 = (((cflag & CSIZE) == CS8) ? 0xc0 : 0x40);
308 reg5 = (((cflag & CSIZE) == CS8) ? 0x60 : 0x20) | 0x82 /* assert DTR/RTS */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Roman Zippel6ff58012007-05-01 22:32:43 +0200310 if (port == 1) {
311 (void)scc.cha_b_ctrl; /* reset reg pointer */
312 SCCB_WRITE(9, 0xc0); /* reset */
313 LONG_DELAY(); /* extra delay after WR9 access */
314 SCCB_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
315 0x04 /* 1 stopbit */ |
316 clkmode);
317 SCCB_WRITE(3, reg3);
318 SCCB_WRITE(5, reg5);
319 SCCB_WRITE(9, 0); /* no interrupts */
320 LONG_DELAY(); /* extra delay after WR9 access */
321 SCCB_WRITE(10, 0); /* NRZ mode */
322 SCCB_WRITE(11, clksrc); /* main clock source */
323 SCCB_WRITE(12, div); /* BRG value */
324 SCCB_WRITE(13, 0); /* BRG high byte */
325 SCCB_WRITE(14, 1);
326 SCCB_WRITE(3, reg3 | 1);
327 SCCB_WRITE(5, reg5 | 8);
328 } else if (port == 0) {
329 (void)scc.cha_a_ctrl; /* reset reg pointer */
330 SCCA_WRITE(9, 0xc0); /* reset */
331 LONG_DELAY(); /* extra delay after WR9 access */
332 SCCA_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 0x04 /* 1 stopbit */ |
Roman Zippel6ff58012007-05-01 22:32:43 +0200334 clkmode);
335 SCCA_WRITE(3, reg3);
336 SCCA_WRITE(5, reg5);
337 SCCA_WRITE(9, 0); /* no interrupts */
338 LONG_DELAY(); /* extra delay after WR9 access */
339 SCCA_WRITE(10, 0); /* NRZ mode */
340 SCCA_WRITE(11, clksrc); /* main clock source */
341 SCCA_WRITE(12, div); /* BRG value */
342 SCCA_WRITE(13, 0); /* BRG high byte */
343 SCCA_WRITE(14, 1);
344 SCCA_WRITE(3, reg3 | 1);
345 SCCA_WRITE(5, reg5 | 8);
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Roman Zippel6ff58012007-05-01 22:32:43 +0200348 mac_SCC_reset_done = 1;
349 mac_SCC_init_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351#endif /* DEBUG_SERIAL */
352
Roman Zippel6ff58012007-05-01 22:32:43 +0200353void mac_init_scca_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 mac_init_scc_port(cflag, 0);
356}
357
Roman Zippel6ff58012007-05-01 22:32:43 +0200358void mac_init_sccb_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 mac_init_scc_port(cflag, 1);
361}
362
363void __init mac_debug_init(void)
364{
365#ifdef DEBUG_SERIAL
Roman Zippel6ff58012007-05-01 22:32:43 +0200366 if (!strcmp(m68k_debug_device, "ser") ||
367 !strcmp(m68k_debug_device, "ser1")) {
368 /* Mac modem port */
369 mac_init_scc_port(B9600|CS8, 0);
370 mac_console_driver.write = mac_scca_console_write;
371 scc_port = 0;
372 } else if (!strcmp(m68k_debug_device, "ser2")) {
373 /* Mac printer port */
374 mac_init_scc_port(B9600|CS8, 1);
375 mac_console_driver.write = mac_sccb_console_write;
376 scc_port = 1;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
379#ifdef DEBUG_HEADS
Roman Zippel6ff58012007-05-01 22:32:43 +0200380 if (!strcmp(m68k_debug_device, "scn") ||
381 !strcmp(m68k_debug_device, "con")) {
382 /* display, using head.S console routines */
383 mac_console_driver.write = mac_debug_console_write;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif
Roman Zippel6ff58012007-05-01 22:32:43 +0200386 if (mac_console_driver.write)
387 register_console(&mac_console_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}