68328serial.c revision c26f0115c0fd931e6420b6f565e5f9d154ec627e
1/* 68328serial.c: Serial port driver for 68328 microcontroller
2 *
3 * Copyright (C) 1995       David S. Miller    <davem@caip.rutgers.edu>
4 * Copyright (C) 1998       Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne     <jeff@uclinux.org>
6 * Copyright (C) 1999       Vladimir Gurevich  <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003  David McCullough   <davidm@snapgear.com>
8 * Copyright (C) 2002       Greg Ungerer       <gerg@snapgear.com>
9 *
10 * VZ Support/Fixes             Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support        Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support     Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port                 David McCullough
15 */
16
17#include <asm/dbg.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/serial.h>
21#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/timer.h>
24#include <linux/interrupt.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
27#include <linux/major.h>
28#include <linux/string.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/kernel.h>
32#include <linux/console.h>
33#include <linux/reboot.h>
34#include <linux/keyboard.h>
35#include <linux/init.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39#include <linux/gfp.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * 	 define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
60/* Turn off usage of real serial interrupt code, to "support" Copilot */
61#ifdef CONFIG_XCOPILOT_BUGS
62#undef USE_INTS
63#else
64#define USE_INTS
65#endif
66
67/*
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 *                                  -- Vladimir Gurevich
74 */
75#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
76
77/*
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real  328-based
82 * board I would assume that RXRE is the safest setting.
83 *
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
87 *
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
90 *
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 *                                  -- Vladimir Gurevich
94 */
95
96/* (es) */
97#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99#elif defined(CONFIG_M68328)
100#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
101#else
102#error Please, define the Rx interrupt events for your CPU
103#endif
104/* (/es) */
105
106/*
107 * This is our internal structure for each serial port's state.
108 *
109 * For definitions of the flags field, see serial.h
110 */
111struct m68k_serial {
112	struct tty_port		tport;
113	char			is_cons;	/* Is this our console. */
114	int			magic;
115	int			baud_base;
116	int			port;
117	int			irq;
118	int			flags;		/* defined in tty.h */
119	int			type;		/* UART type */
120	struct tty_struct	*tty;
121	int			custom_divisor;
122	int			x_char;		/* xon/xoff character */
123	int			close_delay;
124	unsigned short		closing_wait;
125	int			line;
126	unsigned char		*xmit_buf;
127	int			xmit_head;
128	int			xmit_tail;
129	int			xmit_cnt;
130};
131
132#define SERIAL_MAGIC 0x5301
133
134/*
135 * Define the number of ports supported and their irqs.
136 */
137#define NR_PORTS 1
138
139static struct m68k_serial m68k_soft[NR_PORTS];
140
141static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
142
143/* multiple ports are contiguous in memory */
144m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
145
146struct tty_driver *serial_driver;
147
148static void change_speed(struct m68k_serial *info);
149
150/*
151 *	Setup for console. Argument comes from the boot command line.
152 */
153
154/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
155#ifdef CONFIG_M68VZ328
156#define CONSOLE_BAUD_RATE	19200
157#define DEFAULT_CBAUD		B19200
158#endif
159
160
161#ifndef CONSOLE_BAUD_RATE
162#define	CONSOLE_BAUD_RATE	9600
163#define	DEFAULT_CBAUD		B9600
164#endif
165
166
167static int m68328_console_initted = 0;
168static int m68328_console_baud    = CONSOLE_BAUD_RATE;
169static int m68328_console_cbaud   = DEFAULT_CBAUD;
170
171
172static inline int serial_paranoia_check(struct m68k_serial *info,
173					char *name, const char *routine)
174{
175#ifdef SERIAL_PARANOIA_CHECK
176	static const char *badmagic =
177		"Warning: bad magic number for serial struct %s in %s\n";
178	static const char *badinfo =
179		"Warning: null m68k_serial for %s in %s\n";
180
181	if (!info) {
182		printk(badinfo, name, routine);
183		return 1;
184	}
185	if (info->magic != SERIAL_MAGIC) {
186		printk(badmagic, name, routine);
187		return 1;
188	}
189#endif
190	return 0;
191}
192
193/*
194 * This is used to figure out the divisor speeds and the timeouts
195 */
196static int baud_table[] = {
197	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
198	9600, 19200, 38400, 57600, 115200, 0 };
199
200/* Utility routines */
201static inline int get_baud(struct m68k_serial *ss)
202{
203	unsigned long result = 115200;
204	unsigned short int baud = uart_addr[ss->line].ubaud;
205	if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
206	result >>= GET_FIELD(baud, UBAUD_DIVIDE);
207
208	return result;
209}
210
211/*
212 * ------------------------------------------------------------
213 * rs_stop() and rs_start()
214 *
215 * This routines are called before setting or resetting tty->stopped.
216 * They enable or disable transmitter interrupts, as necessary.
217 * ------------------------------------------------------------
218 */
219static void rs_stop(struct tty_struct *tty)
220{
221	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
222	m68328_uart *uart = &uart_addr[info->line];
223	unsigned long flags;
224
225	if (serial_paranoia_check(info, tty->name, "rs_stop"))
226		return;
227
228	local_irq_save(flags);
229	uart->ustcnt &= ~USTCNT_TXEN;
230	local_irq_restore(flags);
231}
232
233static int rs_put_char(char ch)
234{
235	unsigned long flags;
236	int loops = 0;
237
238        local_irq_save(flags);
239
240	while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
241        	loops++;
242        	udelay(5);
243        }
244
245	UTX_TXDATA = ch;
246        udelay(5);
247        local_irq_restore(flags);
248        return 1;
249}
250
251static void rs_start(struct tty_struct *tty)
252{
253	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
254	m68328_uart *uart = &uart_addr[info->line];
255	unsigned long flags;
256
257	if (serial_paranoia_check(info, tty->name, "rs_start"))
258		return;
259
260	local_irq_save(flags);
261	if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
262#ifdef USE_INTS
263		uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
264#else
265		uart->ustcnt |= USTCNT_TXEN;
266#endif
267	}
268	local_irq_restore(flags);
269}
270
271static void receive_chars(struct m68k_serial *info, unsigned short rx)
272{
273	struct tty_struct *tty = info->tty;
274	m68328_uart *uart = &uart_addr[info->line];
275	unsigned char ch, flag;
276
277	/*
278	 * This do { } while() loop will get ALL chars out of Rx FIFO
279         */
280#ifndef CONFIG_XCOPILOT_BUGS
281	do {
282#endif
283		ch = GET_FIELD(rx, URX_RXDATA);
284
285		if(info->is_cons) {
286			if(URX_BREAK & rx) { /* whee, break received */
287				return;
288#ifdef CONFIG_MAGIC_SYSRQ
289			} else if (ch == 0x10) { /* ^P */
290				show_state();
291				show_free_areas(0);
292				show_buffers();
293/*				show_net_buffers(); */
294				return;
295			} else if (ch == 0x12) { /* ^R */
296				emergency_restart();
297				return;
298#endif /* CONFIG_MAGIC_SYSRQ */
299			}
300		}
301
302		if(!tty)
303			goto clear_and_exit;
304
305		flag = TTY_NORMAL;
306
307		if (rx & URX_PARITY_ERROR)
308			flag = TTY_PARITY;
309		else if (rx & URX_OVRUN)
310			flag = TTY_OVERRUN;
311		else if (rx & URX_FRAME_ERROR)
312			flag = TTY_FRAME;
313
314		tty_insert_flip_char(tty, ch, flag);
315#ifndef CONFIG_XCOPILOT_BUGS
316	} while((rx = uart->urx.w) & URX_DATA_READY);
317#endif
318
319	tty_schedule_flip(tty);
320
321clear_and_exit:
322	return;
323}
324
325static void transmit_chars(struct m68k_serial *info)
326{
327	m68328_uart *uart = &uart_addr[info->line];
328
329	if (info->x_char) {
330		/* Send next char */
331		uart->utx.b.txdata = info->x_char;
332		info->x_char = 0;
333		goto clear_and_return;
334	}
335
336	if((info->xmit_cnt <= 0) || info->tty->stopped) {
337		/* That's peculiar... TX ints off */
338		uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
339		goto clear_and_return;
340	}
341
342	/* Send char */
343	uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
344	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
345	info->xmit_cnt--;
346
347	if(info->xmit_cnt <= 0) {
348		/* All done for now... TX ints off */
349		uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
350		goto clear_and_return;
351	}
352
353clear_and_return:
354	/* Clear interrupt (should be auto)*/
355	return;
356}
357
358/*
359 * This is the serial driver's generic interrupt routine
360 */
361irqreturn_t rs_interrupt(int irq, void *dev_id)
362{
363	struct m68k_serial *info = dev_id;
364	m68328_uart *uart;
365	unsigned short rx;
366	unsigned short tx;
367
368	uart = &uart_addr[info->line];
369	rx = uart->urx.w;
370
371#ifdef USE_INTS
372	tx = uart->utx.w;
373
374	if (rx & URX_DATA_READY) receive_chars(info, rx);
375	if (tx & UTX_TX_AVAIL)   transmit_chars(info);
376#else
377	receive_chars(info, rx);
378#endif
379	return IRQ_HANDLED;
380}
381
382static int startup(struct m68k_serial * info)
383{
384	m68328_uart *uart = &uart_addr[info->line];
385	unsigned long flags;
386
387	if (info->flags & ASYNC_INITIALIZED)
388		return 0;
389
390	if (!info->xmit_buf) {
391		info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
392		if (!info->xmit_buf)
393			return -ENOMEM;
394	}
395
396	local_irq_save(flags);
397
398	/*
399	 * Clear the FIFO buffers and disable them
400	 * (they will be reenabled in change_speed())
401	 */
402
403	uart->ustcnt = USTCNT_UEN;
404	uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
405	(void)uart->urx.w;
406
407	/*
408	 * Finally, enable sequencing and interrupts
409	 */
410#ifdef USE_INTS
411	uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
412                 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
413#else
414	uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
415#endif
416
417	if (info->tty)
418		clear_bit(TTY_IO_ERROR, &info->tty->flags);
419	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
420
421	/*
422	 * and set the speed of the serial port
423	 */
424
425	change_speed(info);
426
427	info->flags |= ASYNC_INITIALIZED;
428	local_irq_restore(flags);
429	return 0;
430}
431
432/*
433 * This routine will shutdown a serial port; interrupts are disabled, and
434 * DTR is dropped if the hangup on close termio flag is on.
435 */
436static void shutdown(struct m68k_serial * info)
437{
438	m68328_uart *uart = &uart_addr[info->line];
439	unsigned long	flags;
440
441	uart->ustcnt = 0; /* All off! */
442	if (!(info->flags & ASYNC_INITIALIZED))
443		return;
444
445	local_irq_save(flags);
446
447	if (info->xmit_buf) {
448		free_page((unsigned long) info->xmit_buf);
449		info->xmit_buf = 0;
450	}
451
452	if (info->tty)
453		set_bit(TTY_IO_ERROR, &info->tty->flags);
454
455	info->flags &= ~ASYNC_INITIALIZED;
456	local_irq_restore(flags);
457}
458
459struct {
460	int divisor, prescale;
461}
462#ifndef CONFIG_M68VZ328
463 hw_baud_table[18] = {
464	{0,0}, /* 0 */
465	{0,0}, /* 50 */
466	{0,0}, /* 75 */
467	{0,0}, /* 110 */
468	{0,0}, /* 134 */
469	{0,0}, /* 150 */
470	{0,0}, /* 200 */
471	{7,0x26}, /* 300 */
472	{6,0x26}, /* 600 */
473	{5,0x26}, /* 1200 */
474	{0,0}, /* 1800 */
475	{4,0x26}, /* 2400 */
476	{3,0x26}, /* 4800 */
477	{2,0x26}, /* 9600 */
478	{1,0x26}, /* 19200 */
479	{0,0x26}, /* 38400 */
480	{1,0x38}, /* 57600 */
481	{0,0x38}, /* 115200 */
482};
483#else
484 hw_baud_table[18] = {
485                 {0,0}, /* 0 */
486                 {0,0}, /* 50 */
487                 {0,0}, /* 75 */
488                 {0,0}, /* 110 */
489                 {0,0}, /* 134 */
490                 {0,0}, /* 150 */
491                 {0,0}, /* 200 */
492                 {0,0}, /* 300 */
493                 {7,0x26}, /* 600 */
494                 {6,0x26}, /* 1200 */
495                 {0,0}, /* 1800 */
496                 {5,0x26}, /* 2400 */
497                 {4,0x26}, /* 4800 */
498                 {3,0x26}, /* 9600 */
499                 {2,0x26}, /* 19200 */
500                 {1,0x26}, /* 38400 */
501                 {0,0x26}, /* 57600 */
502                 {1,0x38}, /* 115200 */
503};
504#endif
505/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
506
507/*
508 * This routine is called to set the UART divisor registers to match
509 * the specified baud rate for a serial port.
510 */
511static void change_speed(struct m68k_serial *info)
512{
513	m68328_uart *uart = &uart_addr[info->line];
514	unsigned short port;
515	unsigned short ustcnt;
516	unsigned cflag;
517	int	i;
518
519	if (!info->tty || !info->tty->termios)
520		return;
521	cflag = info->tty->termios->c_cflag;
522	if (!(port = info->port))
523		return;
524
525	ustcnt = uart->ustcnt;
526	uart->ustcnt = ustcnt & ~USTCNT_TXEN;
527
528	i = cflag & CBAUD;
529        if (i & CBAUDEX) {
530                i = (i & ~CBAUDEX) + B38400;
531        }
532
533	uart->ubaud = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) |
534		PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
535
536	ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
537
538	if ((cflag & CSIZE) == CS8)
539		ustcnt |= USTCNT_8_7;
540
541	if (cflag & CSTOPB)
542		ustcnt |= USTCNT_STOP;
543
544	if (cflag & PARENB)
545		ustcnt |= USTCNT_PARITYEN;
546	if (cflag & PARODD)
547		ustcnt |= USTCNT_ODD_EVEN;
548
549#ifdef CONFIG_SERIAL_68328_RTS_CTS
550	if (cflag & CRTSCTS) {
551		uart->utx.w &= ~ UTX_NOCTS;
552	} else {
553		uart->utx.w |= UTX_NOCTS;
554	}
555#endif
556
557	ustcnt |= USTCNT_TXEN;
558
559	uart->ustcnt = ustcnt;
560	return;
561}
562
563/*
564 * Fair output driver allows a process to speak.
565 */
566static void rs_fair_output(void)
567{
568	int left;		/* Output no more than that */
569	unsigned long flags;
570	struct m68k_serial *info = &m68k_soft[0];
571	char c;
572
573	if (info == 0) return;
574	if (info->xmit_buf == 0) return;
575
576	local_irq_save(flags);
577	left = info->xmit_cnt;
578	while (left != 0) {
579		c = info->xmit_buf[info->xmit_tail];
580		info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
581		info->xmit_cnt--;
582		local_irq_restore(flags);
583
584		rs_put_char(c);
585
586		local_irq_save(flags);
587		left = min(info->xmit_cnt, left-1);
588	}
589
590	/* Last character is being transmitted now (hopefully). */
591	udelay(5);
592
593	local_irq_restore(flags);
594	return;
595}
596
597/*
598 * m68k_console_print is registered for printk.
599 */
600void console_print_68328(const char *p)
601{
602	char c;
603
604	while((c=*(p++)) != 0) {
605		if(c == '\n')
606			rs_put_char('\r');
607		rs_put_char(c);
608	}
609
610	/* Comment this if you want to have a strict interrupt-driven output */
611	rs_fair_output();
612
613	return;
614}
615
616static void rs_set_ldisc(struct tty_struct *tty)
617{
618	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
619
620	if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
621		return;
622
623	info->is_cons = (tty->termios->c_line == N_TTY);
624
625	printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
626}
627
628static void rs_flush_chars(struct tty_struct *tty)
629{
630	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
631	m68328_uart *uart = &uart_addr[info->line];
632	unsigned long flags;
633
634	if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
635		return;
636#ifndef USE_INTS
637	for(;;) {
638#endif
639
640	/* Enable transmitter */
641	local_irq_save(flags);
642
643	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
644			!info->xmit_buf) {
645		local_irq_restore(flags);
646		return;
647	}
648
649#ifdef USE_INTS
650	uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
651#else
652	uart->ustcnt |= USTCNT_TXEN;
653#endif
654
655#ifdef USE_INTS
656	if (uart->utx.w & UTX_TX_AVAIL) {
657#else
658	if (1) {
659#endif
660		/* Send char */
661		uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
662		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
663		info->xmit_cnt--;
664	}
665
666#ifndef USE_INTS
667	while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
668	}
669#endif
670	local_irq_restore(flags);
671}
672
673extern void console_printn(const char * b, int count);
674
675static int rs_write(struct tty_struct * tty,
676		    const unsigned char *buf, int count)
677{
678	int	c, total = 0;
679	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
680	m68328_uart *uart = &uart_addr[info->line];
681	unsigned long flags;
682
683	if (serial_paranoia_check(info, tty->name, "rs_write"))
684		return 0;
685
686	if (!tty || !info->xmit_buf)
687		return 0;
688
689	local_save_flags(flags);
690	while (1) {
691		local_irq_disable();
692		c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
693				   SERIAL_XMIT_SIZE - info->xmit_head));
694		local_irq_restore(flags);
695
696		if (c <= 0)
697			break;
698
699		memcpy(info->xmit_buf + info->xmit_head, buf, c);
700
701		local_irq_disable();
702		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
703		info->xmit_cnt += c;
704		local_irq_restore(flags);
705		buf += c;
706		count -= c;
707		total += c;
708	}
709
710	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
711		/* Enable transmitter */
712		local_irq_disable();
713#ifndef USE_INTS
714		while(info->xmit_cnt) {
715#endif
716
717		uart->ustcnt |= USTCNT_TXEN;
718#ifdef USE_INTS
719		uart->ustcnt |= USTCNT_TX_INTR_MASK;
720#else
721		while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
722#endif
723		if (uart->utx.w & UTX_TX_AVAIL) {
724			uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
725			info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
726			info->xmit_cnt--;
727		}
728
729#ifndef USE_INTS
730		}
731#endif
732		local_irq_restore(flags);
733	}
734
735	return total;
736}
737
738static int rs_write_room(struct tty_struct *tty)
739{
740	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
741	int	ret;
742
743	if (serial_paranoia_check(info, tty->name, "rs_write_room"))
744		return 0;
745	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
746	if (ret < 0)
747		ret = 0;
748	return ret;
749}
750
751static int rs_chars_in_buffer(struct tty_struct *tty)
752{
753	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
754
755	if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
756		return 0;
757	return info->xmit_cnt;
758}
759
760static void rs_flush_buffer(struct tty_struct *tty)
761{
762	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
763	unsigned long flags;
764
765	if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
766		return;
767	local_irq_save(flags);
768	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
769	local_irq_restore(flags);
770	tty_wakeup(tty);
771}
772
773/*
774 * ------------------------------------------------------------
775 * rs_throttle()
776 *
777 * This routine is called by the upper-layer tty layer to signal that
778 * incoming characters should be throttled.
779 * ------------------------------------------------------------
780 */
781static void rs_throttle(struct tty_struct * tty)
782{
783	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
784
785	if (serial_paranoia_check(info, tty->name, "rs_throttle"))
786		return;
787
788	if (I_IXOFF(tty))
789		info->x_char = STOP_CHAR(tty);
790
791	/* Turn off RTS line (do this atomic) */
792}
793
794static void rs_unthrottle(struct tty_struct * tty)
795{
796	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
797
798	if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
799		return;
800
801	if (I_IXOFF(tty)) {
802		if (info->x_char)
803			info->x_char = 0;
804		else
805			info->x_char = START_CHAR(tty);
806	}
807
808	/* Assert RTS line (do this atomic) */
809}
810
811/*
812 * ------------------------------------------------------------
813 * rs_ioctl() and friends
814 * ------------------------------------------------------------
815 */
816
817static int get_serial_info(struct m68k_serial * info,
818			   struct serial_struct * retinfo)
819{
820	struct serial_struct tmp;
821
822	if (!retinfo)
823		return -EFAULT;
824	memset(&tmp, 0, sizeof(tmp));
825	tmp.type = info->type;
826	tmp.line = info->line;
827	tmp.port = info->port;
828	tmp.irq = info->irq;
829	tmp.flags = info->flags;
830	tmp.baud_base = info->baud_base;
831	tmp.close_delay = info->close_delay;
832	tmp.closing_wait = info->closing_wait;
833	tmp.custom_divisor = info->custom_divisor;
834	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
835		return -EFAULT;
836
837	return 0;
838}
839
840static int set_serial_info(struct m68k_serial * info,
841			   struct serial_struct * new_info)
842{
843	struct serial_struct new_serial;
844	struct m68k_serial old_info;
845	int 			retval = 0;
846
847	if (!new_info)
848		return -EFAULT;
849	if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
850		return -EFAULT;
851	old_info = *info;
852
853	if (!capable(CAP_SYS_ADMIN)) {
854		if ((new_serial.baud_base != info->baud_base) ||
855		    (new_serial.type != info->type) ||
856		    (new_serial.close_delay != info->close_delay) ||
857		    ((new_serial.flags & ~ASYNC_USR_MASK) !=
858		     (info->flags & ~ASYNC_USR_MASK)))
859			return -EPERM;
860		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
861			       (new_serial.flags & ASYNC_USR_MASK));
862		info->custom_divisor = new_serial.custom_divisor;
863		goto check_and_exit;
864	}
865
866	if (info->tport.count > 1)
867		return -EBUSY;
868
869	/*
870	 * OK, past this point, all the error checking has been done.
871	 * At this point, we start making changes.....
872	 */
873
874	info->baud_base = new_serial.baud_base;
875	info->flags = ((info->flags & ~ASYNC_FLAGS) |
876			(new_serial.flags & ASYNC_FLAGS));
877	info->type = new_serial.type;
878	info->close_delay = new_serial.close_delay;
879	info->closing_wait = new_serial.closing_wait;
880
881check_and_exit:
882	retval = startup(info);
883	return retval;
884}
885
886/*
887 * get_lsr_info - get line status register info
888 *
889 * Purpose: Let user call ioctl() to get info when the UART physically
890 * 	    is emptied.  On bus types like RS485, the transmitter must
891 * 	    release the bus after transmitting. This must be done when
892 * 	    the transmit shift register is empty, not be done when the
893 * 	    transmit holding register is empty.  This functionality
894 * 	    allows an RS485 driver to be written in user space.
895 */
896static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
897{
898#ifdef CONFIG_SERIAL_68328_RTS_CTS
899	m68328_uart *uart = &uart_addr[info->line];
900#endif
901	unsigned char status;
902	unsigned long flags;
903
904	local_irq_save(flags);
905#ifdef CONFIG_SERIAL_68328_RTS_CTS
906	status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
907#else
908	status = 0;
909#endif
910	local_irq_restore(flags);
911	return put_user(status, value);
912}
913
914/*
915 * This routine sends a break character out the serial port.
916 */
917static void send_break(struct m68k_serial * info, unsigned int duration)
918{
919	m68328_uart *uart = &uart_addr[info->line];
920        unsigned long flags;
921        if (!info->port)
922                return;
923        local_irq_save(flags);
924#ifdef USE_INTS
925	uart->utx.w |= UTX_SEND_BREAK;
926	msleep_interruptible(duration);
927	uart->utx.w &= ~UTX_SEND_BREAK;
928#endif
929        local_irq_restore(flags);
930}
931
932static int rs_ioctl(struct tty_struct *tty,
933		    unsigned int cmd, unsigned long arg)
934{
935	struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
936	int retval;
937
938	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
939		return -ENODEV;
940
941	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
942	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
943	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
944		if (tty->flags & (1 << TTY_IO_ERROR))
945		    return -EIO;
946	}
947
948	switch (cmd) {
949		case TCSBRK:	/* SVID version: non-zero arg --> no break */
950			retval = tty_check_change(tty);
951			if (retval)
952				return retval;
953			tty_wait_until_sent(tty, 0);
954			if (!arg)
955				send_break(info, 250);	/* 1/4 second */
956			return 0;
957		case TCSBRKP:	/* support for POSIX tcsendbreak() */
958			retval = tty_check_change(tty);
959			if (retval)
960				return retval;
961			tty_wait_until_sent(tty, 0);
962			send_break(info, arg ? arg*(100) : 250);
963			return 0;
964		case TIOCGSERIAL:
965			return get_serial_info(info,
966				       (struct serial_struct *) arg);
967		case TIOCSSERIAL:
968			return set_serial_info(info,
969					       (struct serial_struct *) arg);
970		case TIOCSERGETLSR: /* Get line status register */
971			return get_lsr_info(info, (unsigned int *) arg);
972		case TIOCSERGSTRUCT:
973			if (copy_to_user((struct m68k_serial *) arg,
974				    info, sizeof(struct m68k_serial)))
975				return -EFAULT;
976			return 0;
977		default:
978			return -ENOIOCTLCMD;
979		}
980	return 0;
981}
982
983static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
984{
985	struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
986
987	change_speed(info);
988
989	if ((old_termios->c_cflag & CRTSCTS) &&
990	    !(tty->termios->c_cflag & CRTSCTS)) {
991		tty->hw_stopped = 0;
992		rs_start(tty);
993	}
994
995}
996
997/*
998 * ------------------------------------------------------------
999 * rs_close()
1000 *
1001 * This routine is called when the serial port gets closed.  First, we
1002 * wait for the last remaining data to be sent.  Then, we unlink its
1003 * S structure from the interrupt chain if necessary, and we free
1004 * that IRQ if nothing is left in the chain.
1005 * ------------------------------------------------------------
1006 */
1007static void rs_close(struct tty_struct *tty, struct file * filp)
1008{
1009	struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1010	struct tty_port *port = &info->tport;
1011	m68328_uart *uart = &uart_addr[info->line];
1012	unsigned long flags;
1013
1014	if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1015		return;
1016
1017	local_irq_save(flags);
1018
1019	if (tty_hung_up_p(filp)) {
1020		local_irq_restore(flags);
1021		return;
1022	}
1023
1024	if ((tty->count == 1) && (port->count != 1)) {
1025		/*
1026		 * Uh, oh.  tty->count is 1, which means that the tty
1027		 * structure will be freed.  Info->count should always
1028		 * be one in these conditions.  If it's greater than
1029		 * one, we've got real problems, since it means the
1030		 * serial port won't be shutdown.
1031		 */
1032		printk("rs_close: bad serial port count; tty->count is 1, "
1033		       "port->count is %d\n", port->count);
1034		port->count = 1;
1035	}
1036	if (--port->count < 0) {
1037		printk("rs_close: bad serial port count for ttyS%d: %d\n",
1038		       info->line, port->count);
1039		port->count = 0;
1040	}
1041	if (port->count) {
1042		local_irq_restore(flags);
1043		return;
1044	}
1045	info->flags |= ASYNC_CLOSING;
1046	/*
1047	 * Now we wait for the transmit buffer to clear; and we notify
1048	 * the line discipline to only process XON/XOFF characters.
1049	 */
1050	tty->closing = 1;
1051	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1052		tty_wait_until_sent(tty, info->closing_wait);
1053	/*
1054	 * At this point we stop accepting input.  To do this, we
1055	 * disable the receive line status interrupts, and tell the
1056	 * interrupt driver to stop checking the data ready bit in the
1057	 * line status register.
1058	 */
1059
1060	uart->ustcnt &= ~USTCNT_RXEN;
1061	uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1062
1063	shutdown(info);
1064	rs_flush_buffer(tty);
1065
1066	tty_ldisc_flush(tty);
1067	tty->closing = 0;
1068	info->tty = NULL;
1069#warning "This is not and has never been valid so fix it"
1070#if 0
1071	if (tty->ldisc.num != ldiscs[N_TTY].num) {
1072		if (tty->ldisc.close)
1073			(tty->ldisc.close)(tty);
1074		tty->ldisc = ldiscs[N_TTY];
1075		tty->termios->c_line = N_TTY;
1076		if (tty->ldisc.open)
1077			(tty->ldisc.open)(tty);
1078	}
1079#endif
1080	if (port->blocked_open) {
1081		if (info->close_delay) {
1082			msleep_interruptible(jiffies_to_msecs(info->close_delay));
1083		}
1084		wake_up_interruptible(&port->open_wait);
1085	}
1086	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1087	wake_up_interruptible(&port->close_wait);
1088	local_irq_restore(flags);
1089}
1090
1091/*
1092 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1093 */
1094void rs_hangup(struct tty_struct *tty)
1095{
1096	struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1097
1098	if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1099		return;
1100
1101	rs_flush_buffer(tty);
1102	shutdown(info);
1103	info->tport.count = 0;
1104	info->flags &= ~ASYNC_NORMAL_ACTIVE;
1105	info->tty = NULL;
1106	wake_up_interruptible(&info->tport.open_wait);
1107}
1108
1109/*
1110 * ------------------------------------------------------------
1111 * rs_open() and friends
1112 * ------------------------------------------------------------
1113 */
1114static int block_til_ready(struct tty_struct *tty, struct file * filp,
1115			   struct m68k_serial *info)
1116{
1117	struct tty_port *port = &info->tport;
1118	DECLARE_WAITQUEUE(wait, current);
1119	int		retval;
1120	int		do_clocal = 0;
1121
1122	/*
1123	 * If the device is in the middle of being closed, then block
1124	 * until it's done, and then try again.
1125	 */
1126	if (info->flags & ASYNC_CLOSING) {
1127		interruptible_sleep_on(&port->close_wait);
1128#ifdef SERIAL_DO_RESTART
1129		if (info->flags & ASYNC_HUP_NOTIFY)
1130			return -EAGAIN;
1131		else
1132			return -ERESTARTSYS;
1133#else
1134		return -EAGAIN;
1135#endif
1136	}
1137
1138	/*
1139	 * If non-blocking mode is set, or the port is not enabled,
1140	 * then make the check up front and then exit.
1141	 */
1142	if ((filp->f_flags & O_NONBLOCK) ||
1143	    (tty->flags & (1 << TTY_IO_ERROR))) {
1144		info->flags |= ASYNC_NORMAL_ACTIVE;
1145		return 0;
1146	}
1147
1148	if (tty->termios->c_cflag & CLOCAL)
1149		do_clocal = 1;
1150
1151	/*
1152	 * Block waiting for the carrier detect and the line to become
1153	 * free (i.e., not in use by the callout).  While we are in
1154	 * this loop, port->count is dropped by one, so that
1155	 * rs_close() knows when to free things.  We restore it upon
1156	 * exit, either normal or abnormal.
1157	 */
1158	retval = 0;
1159	add_wait_queue(&port->open_wait, &wait);
1160
1161	port->count--;
1162	port->blocked_open++;
1163	while (1) {
1164		current->state = TASK_INTERRUPTIBLE;
1165		if (tty_hung_up_p(filp) ||
1166		    !(info->flags & ASYNC_INITIALIZED)) {
1167#ifdef SERIAL_DO_RESTART
1168			if (info->flags & ASYNC_HUP_NOTIFY)
1169				retval = -EAGAIN;
1170			else
1171				retval = -ERESTARTSYS;
1172#else
1173			retval = -EAGAIN;
1174#endif
1175			break;
1176		}
1177		if (!(info->flags & ASYNC_CLOSING) && do_clocal)
1178			break;
1179                if (signal_pending(current)) {
1180			retval = -ERESTARTSYS;
1181			break;
1182		}
1183		tty_unlock();
1184		schedule();
1185		tty_lock();
1186	}
1187	current->state = TASK_RUNNING;
1188	remove_wait_queue(&port->open_wait, &wait);
1189	if (!tty_hung_up_p(filp))
1190		port->count++;
1191	port->blocked_open--;
1192
1193	if (retval)
1194		return retval;
1195	info->flags |= ASYNC_NORMAL_ACTIVE;
1196	return 0;
1197}
1198
1199/*
1200 * This routine is called whenever a serial port is opened.  It
1201 * enables interrupts for a serial port, linking in its S structure into
1202 * the IRQ chain.   It also performs the serial-specific
1203 * initialization for the tty structure.
1204 */
1205int rs_open(struct tty_struct *tty, struct file * filp)
1206{
1207	struct m68k_serial	*info;
1208	int retval;
1209
1210	info = &m68k_soft[tty->index];
1211
1212	if (serial_paranoia_check(info, tty->name, "rs_open"))
1213		return -ENODEV;
1214
1215	info->tport.count++;
1216	tty->driver_data = info;
1217	info->tty = tty;
1218
1219	/*
1220	 * Start up serial port
1221	 */
1222	retval = startup(info);
1223	if (retval)
1224		return retval;
1225
1226	return block_til_ready(tty, filp, info);
1227}
1228
1229/* Finally, routines used to initialize the serial driver. */
1230
1231static void show_serial_version(void)
1232{
1233	printk("MC68328 serial driver version 1.00\n");
1234}
1235
1236static const struct tty_operations rs_ops = {
1237	.open = rs_open,
1238	.close = rs_close,
1239	.write = rs_write,
1240	.flush_chars = rs_flush_chars,
1241	.write_room = rs_write_room,
1242	.chars_in_buffer = rs_chars_in_buffer,
1243	.flush_buffer = rs_flush_buffer,
1244	.ioctl = rs_ioctl,
1245	.throttle = rs_throttle,
1246	.unthrottle = rs_unthrottle,
1247	.set_termios = rs_set_termios,
1248	.stop = rs_stop,
1249	.start = rs_start,
1250	.hangup = rs_hangup,
1251	.set_ldisc = rs_set_ldisc,
1252};
1253
1254/* rs_init inits the driver */
1255static int __init
1256rs68328_init(void)
1257{
1258	unsigned long flags;
1259	int i;
1260	struct m68k_serial *info;
1261
1262	serial_driver = alloc_tty_driver(NR_PORTS);
1263	if (!serial_driver)
1264		return -ENOMEM;
1265
1266	show_serial_version();
1267
1268	/* Initialize the tty_driver structure */
1269	/* SPARC: Not all of this is exactly right for us. */
1270
1271	serial_driver->name = "ttyS";
1272	serial_driver->major = TTY_MAJOR;
1273	serial_driver->minor_start = 64;
1274	serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1275	serial_driver->subtype = SERIAL_TYPE_NORMAL;
1276	serial_driver->init_termios = tty_std_termios;
1277	serial_driver->init_termios.c_cflag =
1278			m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1279	serial_driver->flags = TTY_DRIVER_REAL_RAW;
1280	tty_set_operations(serial_driver, &rs_ops);
1281
1282	if (tty_register_driver(serial_driver)) {
1283		put_tty_driver(serial_driver);
1284		printk(KERN_ERR "Couldn't register serial driver\n");
1285		return -ENOMEM;
1286	}
1287
1288	local_irq_save(flags);
1289
1290	for(i=0;i<NR_PORTS;i++) {
1291
1292	    info = &m68k_soft[i];
1293	    tty_port_init(&info->tport);
1294	    info->magic = SERIAL_MAGIC;
1295	    info->port = (int) &uart_addr[i];
1296	    info->tty = NULL;
1297	    info->irq = uart_irqs[i];
1298	    info->custom_divisor = 16;
1299	    info->close_delay = 50;
1300	    info->closing_wait = 3000;
1301	    info->x_char = 0;
1302	    info->line = i;
1303	    info->is_cons = 1; /* Means shortcuts work */
1304
1305	    printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1306		   info->port, info->irq);
1307	    printk(" is a builtin MC68328 UART\n");
1308
1309#ifdef CONFIG_M68VZ328
1310		if (i > 0 )
1311			PJSEL &= 0xCF;  /* PSW enable second port output */
1312#endif
1313
1314	    if (request_irq(uart_irqs[i],
1315			    rs_interrupt,
1316			    0,
1317			    "M68328_UART", info))
1318                panic("Unable to attach 68328 serial interrupt\n");
1319	}
1320	local_irq_restore(flags);
1321	return 0;
1322}
1323
1324module_init(rs68328_init);
1325
1326
1327
1328static void m68328_set_baud(void)
1329{
1330	unsigned short ustcnt;
1331	int	i;
1332
1333	ustcnt = USTCNT;
1334	USTCNT = ustcnt & ~USTCNT_TXEN;
1335
1336again:
1337	for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1338		if (baud_table[i] == m68328_console_baud)
1339			break;
1340	if (i >= ARRAY_SIZE(baud_table)) {
1341		m68328_console_baud = 9600;
1342		goto again;
1343	}
1344
1345	UBAUD = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) |
1346		PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1347	ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1348	ustcnt |= USTCNT_8_7;
1349	ustcnt |= USTCNT_TXEN;
1350	USTCNT = ustcnt;
1351	m68328_console_initted = 1;
1352	return;
1353}
1354
1355
1356int m68328_console_setup(struct console *cp, char *arg)
1357{
1358	int		i, n = CONSOLE_BAUD_RATE;
1359
1360	if (!cp)
1361		return(-1);
1362
1363	if (arg)
1364		n = simple_strtoul(arg,NULL,0);
1365
1366	for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1367		if (baud_table[i] == n)
1368			break;
1369	if (i < ARRAY_SIZE(baud_table)) {
1370		m68328_console_baud = n;
1371		m68328_console_cbaud = 0;
1372		if (i > 15) {
1373			m68328_console_cbaud |= CBAUDEX;
1374			i -= 15;
1375		}
1376		m68328_console_cbaud |= i;
1377	}
1378
1379	m68328_set_baud(); /* make sure baud rate changes */
1380	return(0);
1381}
1382
1383
1384static struct tty_driver *m68328_console_device(struct console *c, int *index)
1385{
1386	*index = c->index;
1387	return serial_driver;
1388}
1389
1390
1391void m68328_console_write (struct console *co, const char *str,
1392			   unsigned int count)
1393{
1394	if (!m68328_console_initted)
1395		m68328_set_baud();
1396    while (count--) {
1397        if (*str == '\n')
1398           rs_put_char('\r');
1399        rs_put_char( *str++ );
1400    }
1401}
1402
1403
1404static struct console m68328_driver = {
1405	.name		= "ttyS",
1406	.write		= m68328_console_write,
1407	.device		= m68328_console_device,
1408	.setup		= m68328_console_setup,
1409	.flags		= CON_PRINTBUFFER,
1410	.index		= -1,
1411};
1412
1413
1414static int __init m68328_console_init(void)
1415{
1416	register_console(&m68328_driver);
1417	return 0;
1418}
1419
1420console_initcall(m68328_console_init);
1421