Merge branch 'docs-next' of git://git.lwn.net/linux-2.6
[sfrench/cifs-2.6.git] / arch / m68k / mac / debug.c
1 /*
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
17 #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/macints.h>
28
29 extern unsigned long mac_videobase;
30 extern unsigned long mac_videodepth;
31 extern unsigned long mac_rowbytes;
32
33 extern void mac_serial_print(const char *);
34
35 #define DEBUG_HEADS
36 #undef DEBUG_SCREEN
37 #define DEBUG_SERIAL
38
39 /*
40  * These two auxiliary debug functions should go away ASAP. Only usage:
41  * before the console output is up (after head.S come some other crucial
42  * setup routines :-) it permits writing 'data' to the screen as bit patterns
43  * (good luck reading those). Helped to figure that the bootinfo contained
44  * garbage data on the amount and size of memory chunks ...
45  *
46  * The 'pos' argument now simply means 'linefeed after print' ...
47  */
48
49 #ifdef DEBUG_SCREEN
50 static int peng, line;
51 #endif
52
53 #if 0
54
55 void mac_debugging_short(int pos, short num)
56 {
57 #ifdef DEBUG_SCREEN
58         unsigned char *pengoffset;
59         unsigned char *pptr;
60         int i;
61 #endif
62
63 #ifdef DEBUG_SERIAL
64         printk("debug: %d !\n", num);
65 #endif
66
67 #ifdef DEBUG_SCREEN
68         if (!MACH_IS_MAC) {
69                 /* printk("debug: %d !\n", num); */
70                 return;
71         }
72
73         /* calculate current offset */
74         pengoffset = (unsigned char *)mac_videobase +
75                 (150+line*2) * mac_rowbytes + 80 * peng;
76
77         pptr = pengoffset;
78
79         for (i = 0; i < 8 * sizeof(short); i++) { /* # of bits */
80                 /*        value        mask for bit i, reverse order */
81                 *pptr++ = (num & (1 << (8*sizeof(short)-i-1)) ? 0xFF : 0x00);
82         }
83
84         peng++;
85
86         if (pos) {
87                 line++;
88                 peng = 0;
89         }
90 #endif
91 }
92
93 void mac_debugging_long(int pos, long addr)
94 {
95 #ifdef DEBUG_SCREEN
96         unsigned char *pengoffset;
97         unsigned char *pptr;
98         int i;
99 #endif
100
101 #ifdef DEBUG_SERIAL
102         printk("debug: #%ld !\n", addr);
103 #endif
104
105 #ifdef DEBUG_SCREEN
106         if (!MACH_IS_MAC) {
107                 /* printk("debug: #%ld !\n", addr); */
108                 return;
109         }
110
111         pengoffset=(unsigned char *)(mac_videobase+(150+line*2)*mac_rowbytes)
112                     +80*peng;
113
114         pptr = pengoffset;
115
116         for (i = 0; i < 8 * sizeof(long); i++) { /* # of bits */
117                 *pptr++ = (addr & (1 << (8*sizeof(long)-i-1)) ? 0xFF : 0x00);
118         }
119
120         peng++;
121
122         if (pos) {
123                 line++;
124                 peng = 0;
125         }
126 #endif
127 }
128
129 #endif  /*  0  */
130
131 #ifdef DEBUG_SERIAL
132 /*
133  * TODO: serial debug code
134  */
135
136 struct mac_SCC {
137         u_char cha_b_ctrl;
138         u_char char_dummy1;
139         u_char cha_a_ctrl;
140         u_char char_dummy2;
141         u_char cha_b_data;
142         u_char char_dummy3;
143         u_char cha_a_data;
144 };
145
146 # define scc (*((volatile struct mac_SCC*)mac_bi_data.sccbase))
147
148 static int scc_port = -1;
149
150 static struct console mac_console_driver = {
151         .name   = "debug",
152         .flags  = CON_PRINTBUFFER,
153         .index  = -1,
154 };
155
156 /*
157  * Crude hack to get console output to the screen before the framebuffer
158  * is initialized (happens a lot later in 2.1!).
159  * We just use the console routines declared in head.S, this will interfere
160  * with regular framebuffer console output and should be used exclusively
161  * to debug kernel problems manifesting before framebuffer init (aka WSOD)
162  *
163  * To keep this hack from interfering with the regular console driver, either
164  * deregister this driver before/on framebuffer console init, or silence this
165  * function after the fbcon driver is running (will lose console messages!?).
166  * To debug real early bugs, need to write a 'mac_register_console_hack()'
167  * that is called from start_kernel() before setup_arch() and just registers
168  * this driver if Mac.
169  */
170
171 static void mac_debug_console_write(struct console *co, const char *str,
172                                     unsigned int count)
173 {
174         mac_serial_print(str);
175 }
176
177
178
179 /* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
180
181 #define uSEC 1
182
183 static inline void mac_sccb_out(char c)
184 {
185         int i;
186
187         do {
188                 for (i = uSEC; i > 0; --i)
189                         barrier();
190         } while (!(scc.cha_b_ctrl & 0x04)); /* wait for tx buf empty */
191         for (i = uSEC; i > 0; --i)
192                 barrier();
193         scc.cha_b_data = c;
194 }
195
196 static inline void mac_scca_out(char c)
197 {
198         int i;
199
200         do {
201                 for (i = uSEC; i > 0; --i)
202                         barrier();
203         } while (!(scc.cha_a_ctrl & 0x04)); /* wait for tx buf empty */
204         for (i = uSEC; i > 0; --i)
205                 barrier();
206         scc.cha_a_data = c;
207 }
208
209 static void mac_sccb_console_write(struct console *co, const char *str,
210                                    unsigned int count)
211 {
212         while (count--) {
213                 if (*str == '\n')
214                         mac_sccb_out('\r');
215                 mac_sccb_out(*str++);
216         }
217 }
218
219 static void mac_scca_console_write(struct console *co, const char *str,
220                                    unsigned int count)
221 {
222         while (count--) {
223                 if (*str == '\n')
224                         mac_scca_out('\r');
225                 mac_scca_out(*str++);
226         }
227 }
228
229
230 /* The following two functions do a quick'n'dirty initialization of the MFP or
231  * SCC serial ports. They're used by the debugging interface, kgdb, and the
232  * serial console code. */
233 #define SCCB_WRITE(reg,val)                             \
234         do {                                            \
235                 int i;                                  \
236                 scc.cha_b_ctrl = (reg);                 \
237                 for (i = uSEC; i > 0; --i)              \
238                         barrier();                      \
239                 scc.cha_b_ctrl = (val);                 \
240                 for (i = uSEC; i > 0; --i)              \
241                         barrier();                      \
242         } while(0)
243
244 #define SCCA_WRITE(reg,val)                             \
245         do {                                            \
246                 int i;                                  \
247                 scc.cha_a_ctrl = (reg);                 \
248                 for (i = uSEC; i > 0; --i)              \
249                         barrier();                      \
250                 scc.cha_a_ctrl = (val);                 \
251                 for (i = uSEC; i > 0; --i)              \
252                         barrier();                      \
253         } while(0)
254
255 /* loops_per_jiffy isn't initialized yet, so we can't use udelay(). This does a
256  * delay of ~ 60us. */
257 /* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
258 #define LONG_DELAY()                                    \
259         do {                                            \
260                 int i;                                  \
261                 for (i = 60*uSEC; i > 0; --i)           \
262                     barrier();                          \
263         } while(0)
264
265 static void __init mac_init_scc_port(int cflag, int port)
266 {
267         /*
268          * baud rates: 1200, 1800, 2400, 4800, 9600, 19.2k, 38.4k, 57.6k, 115.2k
269          */
270
271         static int clksrc_table[9] =
272                 /* reg 11: 0x50 = BRG, 0x00 = RTxC, 0x28 = TRxC */
273                 { 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00, 0x00 };
274         static int clkmode_table[9] =
275                 /* reg 4: 0x40 = x16, 0x80 = x32, 0xc0 = x64 */
276                 { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x80 };
277         static int div_table[9] =
278                 /* reg12 (BRG low) */
279                 { 94, 62, 46, 22, 10, 4, 1, 0, 0 };
280
281         int baud = cflag & CBAUD;
282         int clksrc, clkmode, div, reg3, reg5;
283
284         if (cflag & CBAUDEX)
285                 baud += B38400;
286         if (baud < B1200 || baud > B38400+2)
287                 baud = B9600; /* use default 9600bps for non-implemented rates */
288         baud -= B1200; /* tables starts at 1200bps */
289
290         clksrc  = clksrc_table[baud];
291         clkmode = clkmode_table[baud];
292         div     = div_table[baud];
293
294         reg3 = (((cflag & CSIZE) == CS8) ? 0xc0 : 0x40);
295         reg5 = (((cflag & CSIZE) == CS8) ? 0x60 : 0x20) | 0x82 /* assert DTR/RTS */;
296
297         if (port == 1) {
298                 (void)scc.cha_b_ctrl;   /* reset reg pointer */
299                 SCCB_WRITE(9, 0xc0);    /* reset */
300                 LONG_DELAY();           /* extra delay after WR9 access */
301                 SCCB_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
302                            0x04 /* 1 stopbit */ |
303                            clkmode);
304                 SCCB_WRITE(3, reg3);
305                 SCCB_WRITE(5, reg5);
306                 SCCB_WRITE(9, 0);       /* no interrupts */
307                 LONG_DELAY();           /* extra delay after WR9 access */
308                 SCCB_WRITE(10, 0);      /* NRZ mode */
309                 SCCB_WRITE(11, clksrc); /* main clock source */
310                 SCCB_WRITE(12, div);    /* BRG value */
311                 SCCB_WRITE(13, 0);      /* BRG high byte */
312                 SCCB_WRITE(14, 1);
313                 SCCB_WRITE(3, reg3 | 1);
314                 SCCB_WRITE(5, reg5 | 8);
315         } else if (port == 0) {
316                 (void)scc.cha_a_ctrl;   /* reset reg pointer */
317                 SCCA_WRITE(9, 0xc0);    /* reset */
318                 LONG_DELAY();           /* extra delay after WR9 access */
319                 SCCA_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
320                           0x04 /* 1 stopbit */ |
321                           clkmode);
322                 SCCA_WRITE(3, reg3);
323                 SCCA_WRITE(5, reg5);
324                 SCCA_WRITE(9, 0);       /* no interrupts */
325                 LONG_DELAY();           /* extra delay after WR9 access */
326                 SCCA_WRITE(10, 0);      /* NRZ mode */
327                 SCCA_WRITE(11, clksrc); /* main clock source */
328                 SCCA_WRITE(12, div);    /* BRG value */
329                 SCCA_WRITE(13, 0);      /* BRG high byte */
330                 SCCA_WRITE(14, 1);
331                 SCCA_WRITE(3, reg3 | 1);
332                 SCCA_WRITE(5, reg5 | 8);
333         }
334 }
335 #endif /* DEBUG_SERIAL */
336
337 static int __init mac_debug_setup(char *arg)
338 {
339         if (!MACH_IS_MAC)
340                 return 0;
341
342 #ifdef DEBUG_SERIAL
343         if (!strcmp(arg, "ser") || !strcmp(arg, "ser1")) {
344                 /* Mac modem port */
345                 mac_init_scc_port(B9600|CS8, 0);
346                 mac_console_driver.write = mac_scca_console_write;
347                 scc_port = 0;
348         } else if (!strcmp(arg, "ser2")) {
349                 /* Mac printer port */
350                 mac_init_scc_port(B9600|CS8, 1);
351                 mac_console_driver.write = mac_sccb_console_write;
352                 scc_port = 1;
353         }
354 #endif
355 #ifdef DEBUG_HEADS
356         if (!strcmp(arg, "scn") || !strcmp(arg, "con")) {
357                 /* display, using head.S console routines */
358                 mac_console_driver.write = mac_debug_console_write;
359         }
360 #endif
361         if (mac_console_driver.write)
362                 register_console(&mac_console_driver);
363         return 0;
364 }
365
366 early_param("debug", mac_debug_setup);