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