blob: 66983fe87509a7c8fb14fa4924aee66219fe95a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68k/atari/debug.c
3 *
4 * Atari debugging and serial console stuff
5 *
6 * Assembled of parts of former atari/config.c 97-12-18 by Roman Hodek
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive
10 * for more details.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/tty.h>
15#include <linux/console.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18
19#include <asm/atarihw.h>
20#include <asm/atariints.h>
21
22extern char m68k_debug_device[];
23
24/* Flag that Modem1 port is already initialized and used */
25int atari_MFP_init_done;
26/* Flag that Modem1 port is already initialized and used */
27int atari_SCC_init_done;
28/* Can be set somewhere, if a SCC master reset has already be done and should
29 * not be repeated; used by kgdb */
30int atari_SCC_reset_done;
31
32static struct console atari_console_driver = {
Roman Zippel6ff58012007-05-01 22:32:43 +020033 .name = "debug",
34 .flags = CON_PRINTBUFFER,
35 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38
Roman Zippel6ff58012007-05-01 22:32:43 +020039static inline void ata_mfp_out(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Roman Zippel6ff58012007-05-01 22:32:43 +020041 while (!(mfp.trn_stat & 0x80)) /* wait for tx buf empty */
42 barrier();
43 mfp.usart_dta = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Roman Zippel6ff58012007-05-01 22:32:43 +020046void atari_mfp_console_write(struct console *co, const char *str,
47 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Roman Zippel6ff58012007-05-01 22:32:43 +020049 while (count--) {
50 if (*str == '\n')
51 ata_mfp_out('\r');
52 ata_mfp_out(*str++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Roman Zippel6ff58012007-05-01 22:32:43 +020054}
55
56static inline void ata_scc_out(char c)
57{
58 do {
59 MFPDELAY();
60 } while (!(scc.cha_b_ctrl & 0x04)); /* wait for tx buf empty */
61 MFPDELAY();
62 scc.cha_b_data = c;
63}
64
65void atari_scc_console_write(struct console *co, const char *str,
66 unsigned int count)
67{
68 while (count--) {
69 if (*str == '\n')
70 ata_scc_out('\r');
71 ata_scc_out(*str++);
72 }
73}
74
75static inline void ata_midi_out(char c)
76{
77 while (!(acia.mid_ctrl & ACIA_TDRE)) /* wait for tx buf empty */
78 barrier();
79 acia.mid_data = c;
80}
81
82void atari_midi_console_write(struct console *co, const char *str,
83 unsigned int count)
84{
85 while (count--) {
86 if (*str == '\n')
87 ata_midi_out('\r');
88 ata_midi_out(*str++);
89 }
90}
91
92static int ata_par_out(char c)
93{
94 unsigned char tmp;
95 /* This a some-seconds timeout in case no printer is connected */
96 unsigned long i = loops_per_jiffy > 1 ? loops_per_jiffy : 10000000/HZ;
97
98 while ((mfp.par_dt_reg & 1) && --i) /* wait for BUSY == L */
99 ;
100 if (!i)
101 return 0;
102
103 sound_ym.rd_data_reg_sel = 15; /* select port B */
104 sound_ym.wd_data = c; /* put char onto port */
105 sound_ym.rd_data_reg_sel = 14; /* select port A */
106 tmp = sound_ym.rd_data_reg_sel;
107 sound_ym.wd_data = tmp & ~0x20; /* set strobe L */
108 MFPDELAY(); /* wait a bit */
109 sound_ym.wd_data = tmp | 0x20; /* set strobe H */
110 return 1;
111}
112
113static void atari_par_console_write(struct console *co, const char *str,
114 unsigned int count)
115{
116 static int printer_present = 1;
117
118 if (!printer_present)
119 return;
120
121 while (count--) {
122 if (*str == '\n') {
123 if (!ata_par_out('\r')) {
124 printer_present = 0;
125 return;
126 }
127 }
128 if (!ata_par_out(*str++)) {
129 printer_present = 0;
130 return;
131 }
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135#ifdef CONFIG_SERIAL_CONSOLE
136int atari_mfp_console_wait_key(struct console *co)
137{
Roman Zippel6ff58012007-05-01 22:32:43 +0200138 while (!(mfp.rcv_stat & 0x80)) /* wait for rx buf filled */
139 barrier();
140 return mfp.usart_dta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143int atari_scc_console_wait_key(struct console *co)
144{
Roman Zippel6ff58012007-05-01 22:32:43 +0200145 do {
146 MFPDELAY();
147 } while (!(scc.cha_b_ctrl & 0x01)); /* wait for rx buf filled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 MFPDELAY();
Roman Zippel6ff58012007-05-01 22:32:43 +0200149 return scc.cha_b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152int atari_midi_console_wait_key(struct console *co)
153{
Roman Zippel6ff58012007-05-01 22:32:43 +0200154 while (!(acia.mid_ctrl & ACIA_RDRF)) /* wait for rx buf filled */
155 barrier();
156 return acia.mid_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158#endif
159
Roman Zippel6ff58012007-05-01 22:32:43 +0200160/*
161 * The following two functions do a quick'n'dirty initialization of the MFP or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * SCC serial ports. They're used by the debugging interface, kgdb, and the
Roman Zippel6ff58012007-05-01 22:32:43 +0200163 * serial console code.
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifndef CONFIG_SERIAL_CONSOLE
Roman Zippel6ff58012007-05-01 22:32:43 +0200166static void __init atari_init_mfp_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#else
Roman Zippel6ff58012007-05-01 22:32:43 +0200168void atari_init_mfp_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif
170{
Roman Zippel6ff58012007-05-01 22:32:43 +0200171 /*
172 * timer values for 1200...115200 bps; > 38400 select 110, 134, or 150
173 * bps, resp., and work only correct if there's a RSVE or RSSPEED
174 */
175 static int baud_table[9] = { 16, 11, 8, 4, 2, 1, 175, 143, 128 };
176 int baud = cflag & CBAUD;
177 int parity = (cflag & PARENB) ? ((cflag & PARODD) ? 0x04 : 0x06) : 0;
178 int csize = ((cflag & CSIZE) == CS7) ? 0x20 : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Roman Zippel6ff58012007-05-01 22:32:43 +0200180 if (cflag & CBAUDEX)
181 baud += B38400;
182 if (baud < B1200 || baud > B38400+2)
183 baud = B9600; /* use default 9600bps for non-implemented rates */
184 baud -= B1200; /* baud_table[] starts at 1200bps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Roman Zippel6ff58012007-05-01 22:32:43 +0200186 mfp.trn_stat &= ~0x01; /* disable TX */
187 mfp.usart_ctr = parity | csize | 0x88; /* 1:16 clk mode, 1 stop bit */
188 mfp.tim_ct_cd &= 0x70; /* stop timer D */
189 mfp.tim_dt_d = baud_table[baud];
190 mfp.tim_ct_cd |= 0x01; /* start timer D, 1:4 */
191 mfp.trn_stat |= 0x01; /* enable TX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Roman Zippel6ff58012007-05-01 22:32:43 +0200193 atari_MFP_init_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Roman Zippel6ff58012007-05-01 22:32:43 +0200196#define SCC_WRITE(reg, val) \
197 do { \
198 scc.cha_b_ctrl = (reg); \
199 MFPDELAY(); \
200 scc.cha_b_ctrl = (val); \
201 MFPDELAY(); \
202 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/* loops_per_jiffy isn't initialized yet, so we can't use udelay(). This does a
205 * delay of ~ 60us. */
Roman Zippel6ff58012007-05-01 22:32:43 +0200206#define LONG_DELAY() \
207 do { \
208 int i; \
209 for (i = 100; i > 0; --i) \
210 MFPDELAY(); \
211 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213#ifndef CONFIG_SERIAL_CONSOLE
Roman Zippel6ff58012007-05-01 22:32:43 +0200214static void __init atari_init_scc_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#else
Roman Zippel6ff58012007-05-01 22:32:43 +0200216void atari_init_scc_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#endif
218{
Roman Zippel6ff58012007-05-01 22:32:43 +0200219 extern int atari_SCC_reset_done;
220 static int clksrc_table[9] =
221 /* reg 11: 0x50 = BRG, 0x00 = RTxC, 0x28 = TRxC */
222 { 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00, 0x00 };
223 static int brgsrc_table[9] =
224 /* reg 14: 0 = RTxC, 2 = PCLK */
225 { 2, 2, 2, 2, 2, 2, 0, 2, 2 };
226 static int clkmode_table[9] =
227 /* reg 4: 0x40 = x16, 0x80 = x32, 0xc0 = x64 */
228 { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x80 };
229 static int div_table[9] =
230 /* reg12 (BRG low) */
231 { 208, 138, 103, 50, 24, 11, 1, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Roman Zippel6ff58012007-05-01 22:32:43 +0200233 int baud = cflag & CBAUD;
234 int clksrc, clkmode, div, reg3, reg5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Roman Zippel6ff58012007-05-01 22:32:43 +0200236 if (cflag & CBAUDEX)
237 baud += B38400;
238 if (baud < B1200 || baud > B38400+2)
239 baud = B9600; /* use default 9600bps for non-implemented rates */
240 baud -= B1200; /* tables starts at 1200bps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Roman Zippel6ff58012007-05-01 22:32:43 +0200242 clksrc = clksrc_table[baud];
243 clkmode = clkmode_table[baud];
244 div = div_table[baud];
245 if (ATARIHW_PRESENT(TT_MFP) && baud >= 6) {
246 /* special treatment for TT, where rates >= 38400 are done via TRxC */
247 clksrc = 0x28; /* TRxC */
248 clkmode = baud == 6 ? 0xc0 :
249 baud == 7 ? 0x80 : /* really 76800bps */
250 0x40; /* really 153600bps */
251 div = 0;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Roman Zippel6ff58012007-05-01 22:32:43 +0200254 reg3 = (cflag & CSIZE) == CS8 ? 0xc0 : 0x40;
255 reg5 = (cflag & CSIZE) == CS8 ? 0x60 : 0x20 | 0x82 /* assert DTR/RTS */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Roman Zippel6ff58012007-05-01 22:32:43 +0200257 (void)scc.cha_b_ctrl; /* reset reg pointer */
258 SCC_WRITE(9, 0xc0); /* reset */
259 LONG_DELAY(); /* extra delay after WR9 access */
260 SCC_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03)
261 : 0 | 0x04 /* 1 stopbit */ | clkmode);
262 SCC_WRITE(3, reg3);
263 SCC_WRITE(5, reg5);
264 SCC_WRITE(9, 0); /* no interrupts */
265 LONG_DELAY(); /* extra delay after WR9 access */
266 SCC_WRITE(10, 0); /* NRZ mode */
267 SCC_WRITE(11, clksrc); /* main clock source */
268 SCC_WRITE(12, div); /* BRG value */
269 SCC_WRITE(13, 0); /* BRG high byte */
270 SCC_WRITE(14, brgsrc_table[baud]);
271 SCC_WRITE(14, brgsrc_table[baud] | (div ? 1 : 0));
272 SCC_WRITE(3, reg3 | 1);
273 SCC_WRITE(5, reg5 | 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Roman Zippel6ff58012007-05-01 22:32:43 +0200275 atari_SCC_reset_done = 1;
276 atari_SCC_init_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279#ifndef CONFIG_SERIAL_CONSOLE
Roman Zippel6ff58012007-05-01 22:32:43 +0200280static void __init atari_init_midi_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#else
Roman Zippel6ff58012007-05-01 22:32:43 +0200282void atari_init_midi_port(int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
284{
Roman Zippel6ff58012007-05-01 22:32:43 +0200285 int baud = cflag & CBAUD;
286 int csize = ((cflag & CSIZE) == CS8) ? 0x10 : 0x00;
287 /* warning 7N1 isn't possible! (instead 7O2 is used...) */
288 int parity = (cflag & PARENB) ? ((cflag & PARODD) ? 0x0c : 0x08) : 0x04;
289 int div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Roman Zippel6ff58012007-05-01 22:32:43 +0200291 /* 4800 selects 7812.5, 115200 selects 500000, all other (incl. 9600 as
292 * default) the standard MIDI speed 31250. */
293 if (cflag & CBAUDEX)
294 baud += B38400;
295 if (baud == B4800)
296 div = ACIA_DIV64; /* really 7812.5 bps */
297 else if (baud == B38400+2 /* 115200 */)
298 div = ACIA_DIV1; /* really 500 kbps (does that work??) */
299 else
300 div = ACIA_DIV16; /* 31250 bps, standard for MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Roman Zippel6ff58012007-05-01 22:32:43 +0200302 /* RTS low, ints disabled */
303 acia.mid_ctrl = div | csize | parity |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ((atari_switches & ATARI_SWITCH_MIDI) ?
305 ACIA_RHTID : ACIA_RLTID);
306}
307
308void __init atari_debug_init(void)
309{
Roman Zippel6ff58012007-05-01 22:32:43 +0200310 if (!strcmp(m68k_debug_device, "ser")) {
311 /* defaults to ser2 for a Falcon and ser1 otherwise */
312 strcpy(m68k_debug_device, MACH_IS_FALCON ? "ser2" : "ser1");
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Roman Zippel6ff58012007-05-01 22:32:43 +0200315 if (!strcmp(m68k_debug_device, "ser1")) {
316 /* ST-MFP Modem1 serial port */
317 atari_init_mfp_port(B9600|CS8);
318 atari_console_driver.write = atari_mfp_console_write;
319 } else if (!strcmp(m68k_debug_device, "ser2")) {
320 /* SCC Modem2 serial port */
321 atari_init_scc_port(B9600|CS8);
322 atari_console_driver.write = atari_scc_console_write;
323 } else if (!strcmp(m68k_debug_device, "midi")) {
324 /* MIDI port */
325 atari_init_midi_port(B9600|CS8);
326 atari_console_driver.write = atari_midi_console_write;
327 } else if (!strcmp(m68k_debug_device, "par")) {
328 /* parallel printer */
329 atari_turnoff_irq(IRQ_MFP_BUSY); /* avoid ints */
330 sound_ym.rd_data_reg_sel = 7; /* select mixer control */
331 sound_ym.wd_data = 0xff; /* sound off, ports are output */
332 sound_ym.rd_data_reg_sel = 15; /* select port B */
333 sound_ym.wd_data = 0; /* no char */
334 sound_ym.rd_data_reg_sel = 14; /* select port A */
335 sound_ym.wd_data = sound_ym.rd_data_reg_sel | 0x20; /* strobe H */
336 atari_console_driver.write = atari_par_console_write;
337 }
338 if (atari_console_driver.write)
339 register_console(&atari_console_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}