1 /*
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 
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/tty.h>
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 
20 #include <asm/atarihw.h>
21 #include <asm/atariints.h>
22 
23 extern char m68k_debug_device[];
24 
25 /* Flag that Modem1 port is already initialized and used */
26 int atari_MFP_init_done = 0;
27 /* Flag that Modem1 port is already initialized and used */
28 int atari_SCC_init_done = 0;
29 /* Can be set somewhere, if a SCC master reset has already be done and should
30  * not be repeated; used by kgdb */
31 int atari_SCC_reset_done = 0;
32 
33 static struct console atari_console_driver = {
34 	name:		"debug",
35 	flags:		CON_PRINTBUFFER,
36 	index:		-1,
37 };
38 
39 
ata_mfp_out(char c)40 static inline void ata_mfp_out (char c)
41 {
42     while (!(mfp.trn_stat & 0x80)) /* wait for tx buf empty */
43 	barrier ();
44     mfp.usart_dta = c;
45 }
46 
atari_mfp_console_write(struct console * co,const char * str,unsigned int count)47 void atari_mfp_console_write (struct console *co, const char *str,
48 			      unsigned int count)
49 {
50     while (count--) {
51 	if (*str == '\n')
52 	    ata_mfp_out( '\r' );
53 	ata_mfp_out( *str++ );
54     }
55 }
56 
ata_scc_out(char c)57 static inline void ata_scc_out (char c)
58 {
59     do {
60 	MFPDELAY();
61     } while (!(scc.cha_b_ctrl & 0x04)); /* wait for tx buf empty */
62     MFPDELAY();
63     scc.cha_b_data = c;
64 }
65 
atari_scc_console_write(struct console * co,const char * str,unsigned int count)66 void atari_scc_console_write (struct console *co, const char *str,
67 			      unsigned int count)
68 {
69     while (count--) {
70 	if (*str == '\n')
71 	    ata_scc_out( '\r' );
72 	ata_scc_out( *str++ );
73     }
74 }
75 
ata_midi_out(char c)76 static inline void ata_midi_out (char c)
77 {
78     while (!(acia.mid_ctrl & ACIA_TDRE)) /* wait for tx buf empty */
79 	barrier ();
80     acia.mid_data = c;
81 }
82 
atari_midi_console_write(struct console * co,const char * str,unsigned int count)83 void atari_midi_console_write (struct console *co, const char *str,
84 			       unsigned int count)
85 {
86     while (count--) {
87 	if (*str == '\n')
88 	    ata_midi_out( '\r' );
89 	ata_midi_out( *str++ );
90     }
91 }
92 
ata_par_out(char c)93 static int ata_par_out (char c)
94 {
95     unsigned char tmp;
96     /* This a some-seconds timeout in case no printer is connected */
97     unsigned long i = loops_per_jiffy > 1 ? loops_per_jiffy : 10000000/HZ;
98 
99     while( (mfp.par_dt_reg & 1) && --i ) /* wait for BUSY == L */
100 	;
101     if (!i) 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 
atari_par_console_write(struct console * co,const char * str,unsigned int count)113 static 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 	if (!ata_par_out( *str++ )) {
128 	    printer_present = 0;
129 	    return;
130 	}
131     }
132 }
133 
134 #ifdef CONFIG_SERIAL_CONSOLE
atari_mfp_console_wait_key(struct console * co)135 int atari_mfp_console_wait_key(struct console *co)
136 {
137     while( !(mfp.rcv_stat & 0x80) ) /* wait for rx buf filled */
138 	barrier();
139     return( mfp.usart_dta );
140 }
141 
atari_scc_console_wait_key(struct console * co)142 int atari_scc_console_wait_key(struct console *co)
143 {
144     do {
145 	MFPDELAY();
146     } while( !(scc.cha_b_ctrl & 0x01) ); /* wait for rx buf filled */
147     MFPDELAY();
148     return( scc.cha_b_data );
149 }
150 
atari_midi_console_wait_key(struct console * co)151 int atari_midi_console_wait_key(struct console *co)
152 {
153     while( !(acia.mid_ctrl & ACIA_RDRF) ) /* wait for rx buf filled */
154 	barrier();
155     return( acia.mid_data );
156 }
157 #endif
158 
159 /* The following two functions do a quick'n'dirty initialization of the MFP or
160  * SCC serial ports. They're used by the debugging interface, kgdb, and the
161  * serial console code. */
162 #ifndef CONFIG_SERIAL_CONSOLE
atari_init_mfp_port(int cflag)163 static void __init atari_init_mfp_port( int cflag )
164 #else
165 void atari_init_mfp_port( int cflag )
166 #endif
167 {
168     /* timer values for 1200...115200 bps; > 38400 select 110, 134, or 150
169      * bps, resp., and work only correct if there's a RSVE or RSSPEED */
170     static int baud_table[9] = { 16, 11, 8, 4, 2, 1, 175, 143, 128 };
171     int baud = cflag & CBAUD;
172     int parity = (cflag & PARENB) ? ((cflag & PARODD) ? 0x04 : 0x06) : 0;
173     int csize = ((cflag & CSIZE) == CS7) ? 0x20 : 0x00;
174 
175     if (cflag & CBAUDEX)
176 	baud += B38400;
177     if (baud < B1200 || baud > B38400+2)
178 	baud = B9600; /* use default 9600bps for non-implemented rates */
179     baud -= B1200; /* baud_table[] starts at 1200bps */
180 
181     mfp.trn_stat &= ~0x01; /* disable TX */
182     mfp.usart_ctr = parity | csize | 0x88; /* 1:16 clk mode, 1 stop bit */
183     mfp.tim_ct_cd &= 0x70;  /* stop timer D */
184     mfp.tim_dt_d = baud_table[baud];
185     mfp.tim_ct_cd |= 0x01;  /* start timer D, 1:4 */
186     mfp.trn_stat |= 0x01;  /* enable TX */
187 
188     atari_MFP_init_done = 1;
189 }
190 
191 #define SCC_WRITE(reg,val)				\
192     do {						\
193 	scc.cha_b_ctrl = (reg);				\
194 	MFPDELAY();					\
195 	scc.cha_b_ctrl = (val);				\
196 	MFPDELAY();					\
197     } while(0)
198 
199 /* loops_per_jiffy isn't initialized yet, so we can't use udelay(). This does a
200  * delay of ~ 60us. */
201 #define LONG_DELAY()				\
202     do {					\
203 	int i;					\
204 	for( i = 100; i > 0; --i )		\
205 	    MFPDELAY();				\
206     } while(0)
207 
208 #ifndef CONFIG_SERIAL_CONSOLE
atari_init_scc_port(int cflag)209 static void __init atari_init_scc_port( int cflag )
210 #else
211 void atari_init_scc_port( int cflag )
212 #endif
213 {
214     extern int atari_SCC_reset_done;
215     static int clksrc_table[9] =
216 	/* reg 11: 0x50 = BRG, 0x00 = RTxC, 0x28 = TRxC */
217     	{ 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00, 0x00 };
218     static int brgsrc_table[9] =
219 	/* reg 14: 0 = RTxC, 2 = PCLK */
220     	{ 2, 2, 2, 2, 2, 2, 0, 2, 2 };
221     static int clkmode_table[9] =
222 	/* reg 4: 0x40 = x16, 0x80 = x32, 0xc0 = x64 */
223     	{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x80 };
224     static int div_table[9] =
225 	/* reg12 (BRG low) */
226     	{ 208, 138, 103, 50, 24, 11, 1, 0, 0 };
227 
228     int baud = cflag & CBAUD;
229     int clksrc, clkmode, div, reg3, reg5;
230 
231     if (cflag & CBAUDEX)
232 	baud += B38400;
233     if (baud < B1200 || baud > B38400+2)
234 	baud = B9600; /* use default 9600bps for non-implemented rates */
235     baud -= B1200; /* tables starts at 1200bps */
236 
237     clksrc  = clksrc_table[baud];
238     clkmode = clkmode_table[baud];
239     div     = div_table[baud];
240     if (ATARIHW_PRESENT(TT_MFP) && baud >= 6) {
241 	/* special treatment for TT, where rates >= 38400 are done via TRxC */
242 	clksrc = 0x28; /* TRxC */
243 	clkmode = baud == 6 ? 0xc0 :
244 		  baud == 7 ? 0x80 : /* really 76800bps */
245 			      0x40;  /* really 153600bps */
246 	div = 0;
247     }
248 
249     reg3 = (cflag & CSIZE) == CS8 ? 0xc0 : 0x40;
250     reg5 = (cflag & CSIZE) == CS8 ? 0x60 : 0x20 | 0x82 /* assert DTR/RTS */;
251 
252     (void)scc.cha_b_ctrl;	/* reset reg pointer */
253     SCC_WRITE( 9, 0xc0 );	/* reset */
254     LONG_DELAY();		/* extra delay after WR9 access */
255     SCC_WRITE( 4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
256 		  0x04 /* 1 stopbit */ |
257 		  clkmode );
258     SCC_WRITE( 3, reg3 );
259     SCC_WRITE( 5, reg5 );
260     SCC_WRITE( 9, 0 );		/* no interrupts */
261     LONG_DELAY();		/* extra delay after WR9 access */
262     SCC_WRITE( 10, 0 );		/* NRZ mode */
263     SCC_WRITE( 11, clksrc );	/* main clock source */
264     SCC_WRITE( 12, div );	/* BRG value */
265     SCC_WRITE( 13, 0 );		/* BRG high byte */
266     SCC_WRITE( 14, brgsrc_table[baud] );
267     SCC_WRITE( 14, brgsrc_table[baud] | (div ? 1 : 0) );
268     SCC_WRITE( 3, reg3 | 1 );
269     SCC_WRITE( 5, reg5 | 8 );
270 
271     atari_SCC_reset_done = 1;
272     atari_SCC_init_done = 1;
273 }
274 
275 #ifndef CONFIG_SERIAL_CONSOLE
atari_init_midi_port(int cflag)276 static void __init atari_init_midi_port( int cflag )
277 #else
278 void atari_init_midi_port( int cflag )
279 #endif
280 {
281     int baud = cflag & CBAUD;
282     int csize = ((cflag & CSIZE) == CS8) ? 0x10 : 0x00;
283     /* warning 7N1 isn't possible! (instead 7O2 is used...) */
284     int parity = (cflag & PARENB) ? ((cflag & PARODD) ? 0x0c : 0x08) : 0x04;
285     int div;
286 
287     /* 4800 selects 7812.5, 115200 selects 500000, all other (incl. 9600 as
288      * default) the standard MIDI speed 31250. */
289     if (cflag & CBAUDEX)
290 	baud += B38400;
291     if (baud == B4800)
292 	div = ACIA_DIV64; /* really 7812.5 bps */
293     else if (baud == B38400+2 /* 115200 */)
294 	div = ACIA_DIV1; /* really 500 kbps (does that work??) */
295     else
296 	div = ACIA_DIV16; /* 31250 bps, standard for MIDI */
297 
298     /* RTS low, ints disabled */
299     acia.mid_ctrl = div | csize | parity |
300 		    ((atari_switches & ATARI_SWITCH_MIDI) ?
301 		     ACIA_RHTID : ACIA_RLTID);
302 }
303 
atari_debug_init(void)304 void __init atari_debug_init(void)
305 {
306     if (!strcmp( m68k_debug_device, "ser" )) {
307 	/* defaults to ser2 for a Falcon and ser1 otherwise */
308 	strcpy( m68k_debug_device, MACH_IS_FALCON ? "ser2" : "ser1" );
309 
310     }
311 
312     if (!strcmp( m68k_debug_device, "ser1" )) {
313 	/* ST-MFP Modem1 serial port */
314 	atari_init_mfp_port( B9600|CS8 );
315 	atari_console_driver.write = atari_mfp_console_write;
316     }
317     else if (!strcmp( m68k_debug_device, "ser2" )) {
318 	/* SCC Modem2 serial port */
319 	atari_init_scc_port( B9600|CS8 );
320 	atari_console_driver.write = atari_scc_console_write;
321     }
322     else if (!strcmp( m68k_debug_device, "midi" )) {
323 	/* MIDI port */
324 	atari_init_midi_port( B9600|CS8 );
325 	atari_console_driver.write = atari_midi_console_write;
326     }
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);
340 }
341 
342 /*
343  * Local variables:
344  *  c-indent-level: 4
345  *  tab-width: 8
346  * End:
347  */
348