1/*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
3 *
4 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
7 *
8 * Based on code from 68332serial.c which was:
9 *
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
13 *
14 * Changes:
15 * 08/07/2003    Daniele Bellucci <bellucda@tiscali.it>
16 *               some cleanups in mcfrs_write.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/signal.h>
23#include <linux/sched.h>
24#include <linux/timer.h>
25#include <linux/wait.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/mm.h>
32#include <linux/kernel.h>
33#include <linux/serial.h>
34#include <linux/serialP.h>
35#include <linux/console.h>
36#include <linux/init.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
43#include <asm/semaphore.h>
44#include <asm/delay.h>
45#include <asm/coldfire.h>
46#include <asm/mcfsim.h>
47#include <asm/mcfuart.h>
48#include <asm/nettel.h>
49#include <asm/uaccess.h>
50#include "mcfserial.h"
51
52struct timer_list mcfrs_timer_struct;
53
54/*
55 *	Default console baud rate,  we use this as the default
56 *	for all ports so init can just open /dev/console and
57 *	keep going.  Perhaps one day the cflag settings for the
58 *	console can be used instead.
59 */
60#if defined(CONFIG_HW_FEITH)
61#define	CONSOLE_BAUD_RATE	38400
62#define	DEFAULT_CBAUD		B38400
63#elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB) || \
64      defined(CONFIG_M5329EVB) || defined(CONFIG_GILBARCO)
65#define CONSOLE_BAUD_RATE 	115200
66#define DEFAULT_CBAUD		B115200
67#elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
68      defined(CONFIG_senTec) || defined(CONFIG_SNEHA) || defined(CONFIG_AVNET)
69#define	CONSOLE_BAUD_RATE	19200
70#define	DEFAULT_CBAUD		B19200
71#endif
72
73#ifndef CONSOLE_BAUD_RATE
74#define	CONSOLE_BAUD_RATE	9600
75#define	DEFAULT_CBAUD		B9600
76#endif
77
78int mcfrs_console_inited = 0;
79int mcfrs_console_port = -1;
80int mcfrs_console_baud = CONSOLE_BAUD_RATE;
81int mcfrs_console_cbaud = DEFAULT_CBAUD;
82
83/*
84 *	Driver data structures.
85 */
86static struct tty_driver *mcfrs_serial_driver;
87
88/* number of characters left in xmit buffer before we ask for more */
89#define WAKEUP_CHARS 256
90
91/* Debugging...
92 */
93#undef SERIAL_DEBUG_OPEN
94#undef SERIAL_DEBUG_FLOW
95
96#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
97	defined(CONFIG_M520x) || defined(CONFIG_M532x)
98#define	IRQBASE	(MCFINT_VECBASE+MCFINT_UART0)
99#else
100#define	IRQBASE	73
101#endif
102
103/*
104 *	Configuration table, UARTs to look for at startup.
105 */
106static struct mcf_serial mcfrs_table[] = {
107	{  /* ttyS0 */
108		.magic = 0,
109		.addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
110		.irq = IRQBASE,
111		.flags = ASYNC_BOOT_AUTOCONF,
112	},
113#ifdef MCFUART_BASE2
114	{  /* ttyS1 */
115		.magic = 0,
116		.addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
117		.irq = IRQBASE+1,
118		.flags = ASYNC_BOOT_AUTOCONF,
119	},
120#endif
121#ifdef MCFUART_BASE3
122	{  /* ttyS2 */
123		.magic = 0,
124		.addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE3),
125		.irq = IRQBASE+2,
126		.flags = ASYNC_BOOT_AUTOCONF,
127	},
128#endif
129#ifdef MCFUART_BASE4
130	{  /* ttyS3 */
131		.magic = 0,
132		.addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE4),
133		.irq = IRQBASE+3,
134		.flags = ASYNC_BOOT_AUTOCONF,
135	},
136#endif
137};
138
139
140#define	NR_PORTS	(sizeof(mcfrs_table) / sizeof(struct mcf_serial))
141
142/*
143 * This is used to figure out the divisor speeds and the timeouts.
144 */
145static int mcfrs_baud_table[] = {
146	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
147	9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
148};
149#define MCFRS_BAUD_TABLE_SIZE \
150			(sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
151
152
153#ifdef CONFIG_MAGIC_SYSRQ
154/*
155 *	Magic system request keys. Used for debugging...
156 */
157extern int	magic_sysrq_key(int ch);
158#endif
159
160
161/*
162 *	Forware declarations...
163 */
164static void	mcfrs_change_speed(struct mcf_serial *info);
165static void	mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
166
167
168static inline int serial_paranoia_check(struct mcf_serial *info,
169					char *name, const char *routine)
170{
171#ifdef SERIAL_PARANOIA_CHECK
172	static const char badmagic[] =
173		"MCFRS(warning): bad magic number for serial struct %s in %s\n";
174	static const char badinfo[] =
175		"MCFRS(warning): null mcf_serial for %s in %s\n";
176
177	if (!info) {
178		printk(badinfo, name, routine);
179		return 1;
180	}
181	if (info->magic != SERIAL_MAGIC) {
182		printk(badmagic, name, routine);
183		return 1;
184	}
185#endif
186	return 0;
187}
188
189/*
190 *	Sets or clears DTR and RTS on the requested line.
191 */
192static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
193{
194	volatile unsigned char	*uartp;
195	unsigned long		flags;
196
197
198	local_irq_save(flags);
199	if (dtr >= 0) {
200#ifdef MCFPP_DTR0
201		if (info->line)
202			mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
203		else
204			mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
205#endif
206	}
207	if (rts >= 0) {
208		uartp = info->addr;
209		if (rts) {
210			info->sigs |= TIOCM_RTS;
211			uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
212		} else {
213			info->sigs &= ~TIOCM_RTS;
214			uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
215		}
216	}
217	local_irq_restore(flags);
218	return;
219}
220
221/*
222 *	Gets values of serial signals.
223 */
224static int mcfrs_getsignals(struct mcf_serial *info)
225{
226	volatile unsigned char	*uartp;
227	unsigned long		flags;
228	int			sigs;
229#if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
230	unsigned short		ppdata;
231#endif
232
233
234	local_irq_save(flags);
235	uartp = info->addr;
236	sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
237	sigs |= (info->sigs & TIOCM_RTS);
238
239#ifdef MCFPP_DCD0
240{
241	unsigned int ppdata;
242	ppdata = mcf_getppdata();
243	if (info->line == 0) {
244		sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
245		sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
246	} else if (info->line == 1) {
247		sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
248		sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
249	}
250}
251#endif
252
253	local_irq_restore(flags);
254	return(sigs);
255}
256
257/*
258 * ------------------------------------------------------------
259 * mcfrs_stop() and mcfrs_start()
260 *
261 * This routines are called before setting or resetting tty->stopped.
262 * They enable or disable transmitter interrupts, as necessary.
263 * ------------------------------------------------------------
264 */
265static void mcfrs_stop(struct tty_struct *tty)
266{
267	volatile unsigned char	*uartp;
268	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
269	unsigned long		flags;
270
271	if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
272		return;
273
274	local_irq_save(flags);
275	uartp = info->addr;
276	info->imr &= ~MCFUART_UIR_TXREADY;
277	uartp[MCFUART_UIMR] = info->imr;
278	local_irq_restore(flags);
279}
280
281static void mcfrs_start(struct tty_struct *tty)
282{
283	volatile unsigned char	*uartp;
284	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
285	unsigned long		flags;
286
287	if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
288		return;
289
290	local_irq_save(flags);
291	if (info->xmit_cnt && info->xmit_buf) {
292		uartp = info->addr;
293		info->imr |= MCFUART_UIR_TXREADY;
294		uartp[MCFUART_UIMR] = info->imr;
295	}
296	local_irq_restore(flags);
297}
298
299/*
300 * ----------------------------------------------------------------------
301 *
302 * Here starts the interrupt handling routines.  All of the following
303 * subroutines are declared as inline and are folded into
304 * mcfrs_interrupt().  They were separated out for readability's sake.
305 *
306 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
307 * runs with interrupts turned off.  People who may want to modify
308 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
309 * possible.  After you are done making modifications, it is not a bad
310 * idea to do:
311 *
312 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
313 *
314 * and look at the resulting assemble code in serial.s.
315 *
316 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
317 * -----------------------------------------------------------------------
318 */
319
320static inline void receive_chars(struct mcf_serial *info)
321{
322	volatile unsigned char	*uartp;
323	struct tty_struct	*tty = info->tty;
324	unsigned char		status, ch, flag;
325
326	if (!tty)
327		return;
328
329	uartp = info->addr;
330
331	while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
332		ch = uartp[MCFUART_URB];
333		info->stats.rx++;
334
335#ifdef CONFIG_MAGIC_SYSRQ
336		if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
337			if (magic_sysrq_key(ch))
338				continue;
339		}
340#endif
341
342		flag = TTY_NORMAL;
343		if (status & MCFUART_USR_RXERR) {
344			uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
345			if (status & MCFUART_USR_RXBREAK) {
346				info->stats.rxbreak++;
347				flag = TTY_BREAK;
348			} else if (status & MCFUART_USR_RXPARITY) {
349				info->stats.rxparity++;
350				flag = TTY_PARITY;
351			} else if (status & MCFUART_USR_RXOVERRUN) {
352				info->stats.rxoverrun++;
353				flag = TTY_OVERRUN;
354			} else if (status & MCFUART_USR_RXFRAMING) {
355				info->stats.rxframing++;
356				flag = TTY_FRAME;
357			}
358		}
359		tty_insert_flip_char(tty, ch, flag);
360	}
361	tty_schedule_flip(tty);
362	return;
363}
364
365static inline void transmit_chars(struct mcf_serial *info)
366{
367	volatile unsigned char	*uartp;
368
369	uartp = info->addr;
370
371	if (info->x_char) {
372		/* Send special char - probably flow control */
373		uartp[MCFUART_UTB] = info->x_char;
374		info->x_char = 0;
375		info->stats.tx++;
376	}
377
378	if ((info->xmit_cnt <= 0) || info->tty->stopped) {
379		info->imr &= ~MCFUART_UIR_TXREADY;
380		uartp[MCFUART_UIMR] = info->imr;
381		return;
382	}
383
384	while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
385		uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
386		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
387		info->stats.tx++;
388		if (--info->xmit_cnt <= 0)
389			break;
390	}
391
392	if (info->xmit_cnt < WAKEUP_CHARS)
393		schedule_work(&info->tqueue);
394	return;
395}
396
397/*
398 * This is the serial driver's generic interrupt routine
399 */
400irqreturn_t mcfrs_interrupt(int irq, void *dev_id)
401{
402	struct mcf_serial	*info;
403	unsigned char		isr;
404
405	info = &mcfrs_table[(irq - IRQBASE)];
406	isr = info->addr[MCFUART_UISR] & info->imr;
407
408	if (isr & MCFUART_UIR_RXREADY)
409		receive_chars(info);
410	if (isr & MCFUART_UIR_TXREADY)
411		transmit_chars(info);
412	return IRQ_HANDLED;
413}
414
415/*
416 * -------------------------------------------------------------------
417 * Here ends the serial interrupt routines.
418 * -------------------------------------------------------------------
419 */
420
421static void mcfrs_offintr(struct work_struct *work)
422{
423	struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue);
424	struct tty_struct *tty = info->tty;
425
426	if (tty)
427		tty_wakeup(tty);
428}
429
430
431/*
432 *	Change of state on a DCD line.
433 */
434void mcfrs_modem_change(struct mcf_serial *info, int dcd)
435{
436	if (info->count == 0)
437		return;
438
439	if (info->flags & ASYNC_CHECK_CD) {
440		if (dcd)
441			wake_up_interruptible(&info->open_wait);
442		else
443			schedule_work(&info->tqueue_hangup);
444	}
445}
446
447
448#ifdef MCFPP_DCD0
449
450unsigned short	mcfrs_ppstatus;
451
452/*
453 * This subroutine is called when the RS_TIMER goes off. It is used
454 * to monitor the state of the DCD lines - since they have no edge
455 * sensors and interrupt generators.
456 */
457static void mcfrs_timer(void)
458{
459	unsigned int	ppstatus, dcdval, i;
460
461	ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
462
463	if (ppstatus != mcfrs_ppstatus) {
464		for (i = 0; (i < 2); i++) {
465			dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
466			if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
467				mcfrs_modem_change(&mcfrs_table[i],
468					((ppstatus & dcdval) ? 0 : 1));
469			}
470		}
471	}
472	mcfrs_ppstatus = ppstatus;
473
474	/* Re-arm timer */
475	mcfrs_timer_struct.expires = jiffies + HZ/25;
476	add_timer(&mcfrs_timer_struct);
477}
478
479#endif	/* MCFPP_DCD0 */
480
481
482/*
483 * This routine is called from the scheduler tqueue when the interrupt
484 * routine has signalled that a hangup has occurred. The path of
485 * hangup processing is:
486 *
487 * 	serial interrupt routine -> (scheduler tqueue) ->
488 * 	do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
489 *
490 */
491static void do_serial_hangup(struct work_struct *work)
492{
493	struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue_hangup);
494	struct tty_struct *tty = info->tty;
495
496	if (tty)
497		tty_hangup(tty);
498}
499
500static int startup(struct mcf_serial * info)
501{
502	volatile unsigned char	*uartp;
503	unsigned long		flags;
504
505	if (info->flags & ASYNC_INITIALIZED)
506		return 0;
507
508	if (!info->xmit_buf) {
509		info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
510		if (!info->xmit_buf)
511			return -ENOMEM;
512	}
513
514	local_irq_save(flags);
515
516#ifdef SERIAL_DEBUG_OPEN
517	printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
518#endif
519
520	/*
521	 *	Reset UART, get it into known state...
522	 */
523	uartp = info->addr;
524	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
525	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
526	mcfrs_setsignals(info, 1, 1);
527
528	if (info->tty)
529		clear_bit(TTY_IO_ERROR, &info->tty->flags);
530	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
531
532	/*
533	 * and set the speed of the serial port
534	 */
535	mcfrs_change_speed(info);
536
537	/*
538	 * Lastly enable the UART transmitter and receiver, and
539	 * interrupt enables.
540	 */
541	info->imr = MCFUART_UIR_RXREADY;
542	uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
543	uartp[MCFUART_UIMR] = info->imr;
544
545	info->flags |= ASYNC_INITIALIZED;
546	local_irq_restore(flags);
547	return 0;
548}
549
550/*
551 * This routine will shutdown a serial port; interrupts are disabled, and
552 * DTR is dropped if the hangup on close termio flag is on.
553 */
554static void shutdown(struct mcf_serial * info)
555{
556	volatile unsigned char	*uartp;
557	unsigned long		flags;
558
559	if (!(info->flags & ASYNC_INITIALIZED))
560		return;
561
562#ifdef SERIAL_DEBUG_OPEN
563	printk("Shutting down serial port %d (irq %d)....\n", info->line,
564	       info->irq);
565#endif
566
567	local_irq_save(flags);
568
569	uartp = info->addr;
570	uartp[MCFUART_UIMR] = 0;  /* mask all interrupts */
571	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
572	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
573
574	if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
575		mcfrs_setsignals(info, 0, 0);
576
577	if (info->xmit_buf) {
578		free_page((unsigned long) info->xmit_buf);
579		info->xmit_buf = 0;
580	}
581
582	if (info->tty)
583		set_bit(TTY_IO_ERROR, &info->tty->flags);
584
585	info->flags &= ~ASYNC_INITIALIZED;
586	local_irq_restore(flags);
587}
588
589
590/*
591 * This routine is called to set the UART divisor registers to match
592 * the specified baud rate for a serial port.
593 */
594static void mcfrs_change_speed(struct mcf_serial *info)
595{
596	volatile unsigned char	*uartp;
597	unsigned int		baudclk, cflag;
598	unsigned long		flags;
599	unsigned char		mr1, mr2;
600	int			i;
601#ifdef	CONFIG_M5272
602	unsigned int		fraction;
603#endif
604
605	if (!info->tty || !info->tty->termios)
606		return;
607	cflag = info->tty->termios->c_cflag;
608	if (info->addr == 0)
609		return;
610
611
612	i = cflag & CBAUD;
613	if (i & CBAUDEX) {
614		i &= ~CBAUDEX;
615		if (i < 1 || i > 4)
616			info->tty->termios->c_cflag &= ~CBAUDEX;
617		else
618			i += 15;
619	}
620	if (i == 0) {
621		mcfrs_setsignals(info, 0, -1);
622		return;
623	}
624
625	/* compute the baudrate clock */
626#ifdef	CONFIG_M5272
627	/*
628	 * For the MCF5272, also compute the baudrate fraction.
629	 */
630	baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
631	fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
632	fraction *= 16;
633	fraction /= (32 * mcfrs_baud_table[i]);
634#else
635	baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
636#endif
637
638	info->baud = mcfrs_baud_table[i];
639
640	mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
641	mr2 = 0;
642
643	switch (cflag & CSIZE) {
644	case CS5:	mr1 |= MCFUART_MR1_CS5; break;
645	case CS6:	mr1 |= MCFUART_MR1_CS6; break;
646	case CS7:	mr1 |= MCFUART_MR1_CS7; break;
647	case CS8:
648	default:	mr1 |= MCFUART_MR1_CS8; break;
649	}
650
651	if (cflag & PARENB) {
652		if (cflag & CMSPAR) {
653			if (cflag & PARODD)
654				mr1 |= MCFUART_MR1_PARITYMARK;
655			else
656				mr1 |= MCFUART_MR1_PARITYSPACE;
657		} else {
658			if (cflag & PARODD)
659				mr1 |= MCFUART_MR1_PARITYODD;
660			else
661				mr1 |= MCFUART_MR1_PARITYEVEN;
662		}
663	} else {
664		mr1 |= MCFUART_MR1_PARITYNONE;
665	}
666
667	if (cflag & CSTOPB)
668		mr2 |= MCFUART_MR2_STOP2;
669	else
670		mr2 |= MCFUART_MR2_STOP1;
671
672	if (cflag & CRTSCTS) {
673		mr1 |= MCFUART_MR1_RXRTS;
674		mr2 |= MCFUART_MR2_TXCTS;
675	}
676
677	if (cflag & CLOCAL)
678		info->flags &= ~ASYNC_CHECK_CD;
679	else
680		info->flags |= ASYNC_CHECK_CD;
681
682	uartp = info->addr;
683
684	local_irq_save(flags);
685	/*
686	  Note: pg 12-16 of MCF5206e User's Manual states that a
687	  software reset should be performed prior to changing
688	  UMR1,2, UCSR, UACR, bit 7
689	*/
690	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;    /* reset RX */
691	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;    /* reset TX */
692	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;	/* reset MR pointer */
693	uartp[MCFUART_UMR] = mr1;
694	uartp[MCFUART_UMR] = mr2;
695	uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8;	/* set msb byte */
696	uartp[MCFUART_UBG2] = (baudclk & 0xff);		/* set lsb byte */
697#ifdef	CONFIG_M5272
698	uartp[MCFUART_UFPD] = (fraction & 0xf);		/* set fraction */
699#endif
700	uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
701	uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
702	mcfrs_setsignals(info, 1, -1);
703	local_irq_restore(flags);
704	return;
705}
706
707static void mcfrs_flush_chars(struct tty_struct *tty)
708{
709	volatile unsigned char	*uartp;
710	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
711	unsigned long		flags;
712
713	if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
714		return;
715
716	uartp = (volatile unsigned char *) info->addr;
717
718	/*
719	 * re-enable receiver interrupt
720	 */
721	local_irq_save(flags);
722	if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
723	    (info->flags & ASYNC_INITIALIZED) ) {
724		info->imr |= MCFUART_UIR_RXREADY;
725		uartp[MCFUART_UIMR] = info->imr;
726	}
727	local_irq_restore(flags);
728
729	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
730	    !info->xmit_buf)
731		return;
732
733	/* Enable transmitter */
734	local_irq_save(flags);
735	info->imr |= MCFUART_UIR_TXREADY;
736	uartp[MCFUART_UIMR] = info->imr;
737	local_irq_restore(flags);
738}
739
740static int mcfrs_write(struct tty_struct * tty,
741		    const unsigned char *buf, int count)
742{
743	volatile unsigned char	*uartp;
744	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
745	unsigned long		flags;
746	int			c, total = 0;
747
748
749	if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
750		return 0;
751
752	if (!tty || !info->xmit_buf)
753		return 0;
754
755	local_save_flags(flags);
756	while (1) {
757		local_irq_disable();
758		c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
759			((int)SERIAL_XMIT_SIZE) - info->xmit_head));
760		local_irq_restore(flags);
761
762		if (c <= 0)
763			break;
764
765		memcpy(info->xmit_buf + info->xmit_head, buf, c);
766
767		local_irq_disable();
768		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
769		info->xmit_cnt += c;
770		local_irq_restore(flags);
771
772		buf += c;
773		count -= c;
774		total += c;
775	}
776
777	local_irq_disable();
778	uartp = info->addr;
779	info->imr |= MCFUART_UIR_TXREADY;
780	uartp[MCFUART_UIMR] = info->imr;
781	local_irq_restore(flags);
782
783	return total;
784}
785
786static int mcfrs_write_room(struct tty_struct *tty)
787{
788	struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
789	int	ret;
790
791	if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
792		return 0;
793	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
794	if (ret < 0)
795		ret = 0;
796	return ret;
797}
798
799static int mcfrs_chars_in_buffer(struct tty_struct *tty)
800{
801	struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
802
803	if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
804		return 0;
805	return info->xmit_cnt;
806}
807
808static void mcfrs_flush_buffer(struct tty_struct *tty)
809{
810	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
811	unsigned long		flags;
812
813	if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
814		return;
815
816	local_irq_save(flags);
817	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
818	local_irq_restore(flags);
819
820	tty_wakeup(tty);
821}
822
823/*
824 * ------------------------------------------------------------
825 * mcfrs_throttle()
826 *
827 * This routine is called by the upper-layer tty layer to signal that
828 * incoming characters should be throttled.
829 * ------------------------------------------------------------
830 */
831static void mcfrs_throttle(struct tty_struct * tty)
832{
833	struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
834#ifdef SERIAL_DEBUG_THROTTLE
835	char	buf[64];
836
837	printk("throttle %s: %d....\n", tty_name(tty, buf),
838	       tty->ldisc.chars_in_buffer(tty));
839#endif
840
841	if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
842		return;
843
844	if (I_IXOFF(tty))
845		info->x_char = STOP_CHAR(tty);
846
847	/* Turn off RTS line (do this atomic) */
848}
849
850static void mcfrs_unthrottle(struct tty_struct * tty)
851{
852	struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
853#ifdef SERIAL_DEBUG_THROTTLE
854	char	buf[64];
855
856	printk("unthrottle %s: %d....\n", tty_name(tty, buf),
857	       tty->ldisc.chars_in_buffer(tty));
858#endif
859
860	if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
861		return;
862
863	if (I_IXOFF(tty)) {
864		if (info->x_char)
865			info->x_char = 0;
866		else
867			info->x_char = START_CHAR(tty);
868	}
869
870	/* Assert RTS line (do this atomic) */
871}
872
873/*
874 * ------------------------------------------------------------
875 * mcfrs_ioctl() and friends
876 * ------------------------------------------------------------
877 */
878
879static int get_serial_info(struct mcf_serial * info,
880			   struct serial_struct * retinfo)
881{
882	struct serial_struct tmp;
883
884	if (!retinfo)
885		return -EFAULT;
886	memset(&tmp, 0, sizeof(tmp));
887	tmp.type = info->type;
888	tmp.line = info->line;
889	tmp.port = (unsigned int) info->addr;
890	tmp.irq = info->irq;
891	tmp.flags = info->flags;
892	tmp.baud_base = info->baud_base;
893	tmp.close_delay = info->close_delay;
894	tmp.closing_wait = info->closing_wait;
895	tmp.custom_divisor = info->custom_divisor;
896	return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
897}
898
899static int set_serial_info(struct mcf_serial * info,
900			   struct serial_struct * new_info)
901{
902	struct serial_struct new_serial;
903	struct mcf_serial old_info;
904	int 	retval = 0;
905
906	if (!new_info)
907		return -EFAULT;
908	if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
909		return -EFAULT;
910	old_info = *info;
911
912	if (!capable(CAP_SYS_ADMIN)) {
913		if ((new_serial.baud_base != info->baud_base) ||
914		    (new_serial.type != info->type) ||
915		    (new_serial.close_delay != info->close_delay) ||
916		    ((new_serial.flags & ~ASYNC_USR_MASK) !=
917		     (info->flags & ~ASYNC_USR_MASK)))
918			return -EPERM;
919		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
920			       (new_serial.flags & ASYNC_USR_MASK));
921		info->custom_divisor = new_serial.custom_divisor;
922		goto check_and_exit;
923	}
924
925	if (info->count > 1)
926		return -EBUSY;
927
928	/*
929	 * OK, past this point, all the error checking has been done.
930	 * At this point, we start making changes.....
931	 */
932
933	info->baud_base = new_serial.baud_base;
934	info->flags = ((info->flags & ~ASYNC_FLAGS) |
935			(new_serial.flags & ASYNC_FLAGS));
936	info->type = new_serial.type;
937	info->close_delay = new_serial.close_delay;
938	info->closing_wait = new_serial.closing_wait;
939
940check_and_exit:
941	retval = startup(info);
942	return retval;
943}
944
945/*
946 * get_lsr_info - get line status register info
947 *
948 * Purpose: Let user call ioctl() to get info when the UART physically
949 * 	    is emptied.  On bus types like RS485, the transmitter must
950 * 	    release the bus after transmitting. This must be done when
951 * 	    the transmit shift register is empty, not be done when the
952 * 	    transmit holding register is empty.  This functionality
953 * 	    allows an RS485 driver to be written in user space.
954 */
955static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
956{
957	volatile unsigned char	*uartp;
958	unsigned long		flags;
959	unsigned char		status;
960
961	local_irq_save(flags);
962	uartp = info->addr;
963	status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
964	local_irq_restore(flags);
965
966	return put_user(status,value);
967}
968
969/*
970 * This routine sends a break character out the serial port.
971 */
972static void send_break(	struct mcf_serial * info, int duration)
973{
974	volatile unsigned char	*uartp;
975	unsigned long		flags;
976
977	if (!info->addr)
978		return;
979	set_current_state(TASK_INTERRUPTIBLE);
980	uartp = info->addr;
981
982	local_irq_save(flags);
983	uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
984	schedule_timeout(duration);
985	uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
986	local_irq_restore(flags);
987}
988
989static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
990{
991	struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
992
993	if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
994		return -ENODEV;
995	if (tty->flags & (1 << TTY_IO_ERROR))
996		return -EIO;
997
998	return mcfrs_getsignals(info);
999}
1000
1001static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1002			  unsigned int set, unsigned int clear)
1003{
1004	struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1005	int rts = -1, dtr = -1;
1006
1007	if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1008		return -ENODEV;
1009	if (tty->flags & (1 << TTY_IO_ERROR))
1010		return -EIO;
1011
1012	if (set & TIOCM_RTS)
1013		rts = 1;
1014	if (set & TIOCM_DTR)
1015		dtr = 1;
1016	if (clear & TIOCM_RTS)
1017		rts = 0;
1018	if (clear & TIOCM_DTR)
1019		dtr = 0;
1020
1021	mcfrs_setsignals(info, dtr, rts);
1022
1023	return 0;
1024}
1025
1026static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1027		    unsigned int cmd, unsigned long arg)
1028{
1029	struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1030	int retval, error;
1031
1032	if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1033		return -ENODEV;
1034
1035	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1036	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1037	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1038		if (tty->flags & (1 << TTY_IO_ERROR))
1039		    return -EIO;
1040	}
1041
1042	switch (cmd) {
1043		case TCSBRK:	/* SVID version: non-zero arg --> no break */
1044			retval = tty_check_change(tty);
1045			if (retval)
1046				return retval;
1047			tty_wait_until_sent(tty, 0);
1048			if (!arg)
1049				send_break(info, HZ/4);	/* 1/4 second */
1050			return 0;
1051		case TCSBRKP:	/* support for POSIX tcsendbreak() */
1052			retval = tty_check_change(tty);
1053			if (retval)
1054				return retval;
1055			tty_wait_until_sent(tty, 0);
1056			send_break(info, arg ? arg*(HZ/10) : HZ/4);
1057			return 0;
1058		case TIOCGSOFTCAR:
1059			error = put_user(C_CLOCAL(tty) ? 1 : 0,
1060				    (unsigned long *) arg);
1061			if (error)
1062				return error;
1063			return 0;
1064		case TIOCSSOFTCAR:
1065			get_user(arg, (unsigned long *) arg);
1066			tty->termios->c_cflag =
1067				((tty->termios->c_cflag & ~CLOCAL) |
1068				 (arg ? CLOCAL : 0));
1069			return 0;
1070		case TIOCGSERIAL:
1071			if (access_ok(VERIFY_WRITE, (void *) arg,
1072						sizeof(struct serial_struct)))
1073				return get_serial_info(info,
1074					       (struct serial_struct *) arg);
1075			return -EFAULT;
1076		case TIOCSSERIAL:
1077			return set_serial_info(info,
1078					       (struct serial_struct *) arg);
1079		case TIOCSERGETLSR: /* Get line status register */
1080			if (access_ok(VERIFY_WRITE, (void *) arg,
1081						sizeof(unsigned int)))
1082				return get_lsr_info(info, (unsigned int *) arg);
1083			return -EFAULT;
1084		case TIOCSERGSTRUCT:
1085			error = copy_to_user((struct mcf_serial *) arg,
1086				    info, sizeof(struct mcf_serial));
1087			if (error)
1088				return -EFAULT;
1089			return 0;
1090
1091#ifdef TIOCSET422
1092		case TIOCSET422: {
1093			unsigned int val;
1094			get_user(val, (unsigned int *) arg);
1095			mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1096			break;
1097		}
1098		case TIOCGET422: {
1099			unsigned int val;
1100			val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1101			put_user(val, (unsigned int *) arg);
1102			break;
1103		}
1104#endif
1105
1106		default:
1107			return -ENOIOCTLCMD;
1108		}
1109	return 0;
1110}
1111
1112static void mcfrs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1113{
1114	struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1115
1116	if (tty->termios->c_cflag == old_termios->c_cflag)
1117		return;
1118
1119	mcfrs_change_speed(info);
1120
1121	if ((old_termios->c_cflag & CRTSCTS) &&
1122	    !(tty->termios->c_cflag & CRTSCTS)) {
1123		tty->hw_stopped = 0;
1124		mcfrs_setsignals(info, -1, 1);
1125	}
1126}
1127
1128/*
1129 * ------------------------------------------------------------
1130 * mcfrs_close()
1131 *
1132 * This routine is called when the serial port gets closed.  First, we
1133 * wait for the last remaining data to be sent.  Then, we unlink its
1134 * S structure from the interrupt chain if necessary, and we free
1135 * that IRQ if nothing is left in the chain.
1136 * ------------------------------------------------------------
1137 */
1138static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1139{
1140	volatile unsigned char	*uartp;
1141	struct mcf_serial	*info = (struct mcf_serial *)tty->driver_data;
1142	unsigned long		flags;
1143
1144	if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1145		return;
1146
1147	local_irq_save(flags);
1148
1149	if (tty_hung_up_p(filp)) {
1150		local_irq_restore(flags);
1151		return;
1152	}
1153
1154#ifdef SERIAL_DEBUG_OPEN
1155	printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1156#endif
1157	if ((tty->count == 1) && (info->count != 1)) {
1158		/*
1159		 * Uh, oh.  tty->count is 1, which means that the tty
1160		 * structure will be freed.  Info->count should always
1161		 * be one in these conditions.  If it's greater than
1162		 * one, we've got real problems, since it means the
1163		 * serial port won't be shutdown.
1164		 */
1165		printk("MCFRS: bad serial port count; tty->count is 1, "
1166		       "info->count is %d\n", info->count);
1167		info->count = 1;
1168	}
1169	if (--info->count < 0) {
1170		printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1171		       info->line, info->count);
1172		info->count = 0;
1173	}
1174	if (info->count) {
1175		local_irq_restore(flags);
1176		return;
1177	}
1178	info->flags |= ASYNC_CLOSING;
1179
1180	/*
1181	 * Now we wait for the transmit buffer to clear; and we notify
1182	 * the line discipline to only process XON/XOFF characters.
1183	 */
1184	tty->closing = 1;
1185	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1186		tty_wait_until_sent(tty, info->closing_wait);
1187
1188	/*
1189	 * At this point we stop accepting input.  To do this, we
1190	 * disable the receive line status interrupts, and tell the
1191	 * interrupt driver to stop checking the data ready bit in the
1192	 * line status register.
1193	 */
1194	info->imr &= ~MCFUART_UIR_RXREADY;
1195	uartp = info->addr;
1196	uartp[MCFUART_UIMR] = info->imr;
1197
1198	shutdown(info);
1199	if (tty->driver->flush_buffer)
1200		tty->driver->flush_buffer(tty);
1201	tty_ldisc_flush(tty);
1202
1203	tty->closing = 0;
1204	info->event = 0;
1205	info->tty = 0;
1206	if (info->blocked_open) {
1207		if (info->close_delay) {
1208			msleep_interruptible(jiffies_to_msecs(info->close_delay));
1209		}
1210		wake_up_interruptible(&info->open_wait);
1211	}
1212	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1213	wake_up_interruptible(&info->close_wait);
1214	local_irq_restore(flags);
1215}
1216
1217/*
1218 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1219 */
1220static void
1221mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1222{
1223#ifdef	CONFIG_M5272
1224#define	MCF5272_FIFO_SIZE	25		/* fifo size + shift reg */
1225
1226	struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1227	volatile unsigned char *uartp;
1228	unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1229
1230	if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1231		return;
1232
1233	orig_jiffies = jiffies;
1234
1235	/*
1236	 * Set the check interval to be 1/5 of the approximate time
1237	 * to send the entire fifo, and make it at least 1.  The check
1238	 * interval should also be less than the timeout.
1239	 *
1240	 * Note: we have to use pretty tight timings here to satisfy
1241	 * the NIST-PCTS.
1242	 */
1243	fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1244	char_time = fifo_time / 5;
1245	if (char_time == 0)
1246		char_time = 1;
1247	if (timeout && timeout < char_time)
1248		char_time = timeout;
1249
1250	/*
1251	 * Clamp the timeout period at 2 * the time to empty the
1252	 * fifo.  Just to be safe, set the minimum at .5 seconds.
1253	 */
1254	fifo_time *= 2;
1255	if (fifo_time < (HZ/2))
1256		fifo_time = HZ/2;
1257	if (!timeout || timeout > fifo_time)
1258		timeout = fifo_time;
1259
1260	/*
1261	 * Account for the number of bytes in the UART
1262	 * transmitter FIFO plus any byte being shifted out.
1263	 */
1264	uartp = (volatile unsigned char *) info->addr;
1265	for (;;) {
1266		fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1267		if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1268				MCFUART_USR_TXEMPTY)) ==
1269			MCFUART_USR_TXREADY)
1270			fifo_cnt++;
1271		if (fifo_cnt == 0)
1272			break;
1273		msleep_interruptible(jiffies_to_msecs(char_time));
1274		if (signal_pending(current))
1275			break;
1276		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1277			break;
1278	}
1279#else
1280	/*
1281	 * For the other coldfire models, assume all data has been sent
1282	 */
1283#endif
1284}
1285
1286/*
1287 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1288 */
1289void mcfrs_hangup(struct tty_struct *tty)
1290{
1291	struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1292
1293	if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1294		return;
1295
1296	mcfrs_flush_buffer(tty);
1297	shutdown(info);
1298	info->event = 0;
1299	info->count = 0;
1300	info->flags &= ~ASYNC_NORMAL_ACTIVE;
1301	info->tty = 0;
1302	wake_up_interruptible(&info->open_wait);
1303}
1304
1305/*
1306 * ------------------------------------------------------------
1307 * mcfrs_open() and friends
1308 * ------------------------------------------------------------
1309 */
1310static int block_til_ready(struct tty_struct *tty, struct file * filp,
1311			   struct mcf_serial *info)
1312{
1313	DECLARE_WAITQUEUE(wait, current);
1314	int	retval;
1315	int	do_clocal = 0;
1316
1317	/*
1318	 * If the device is in the middle of being closed, then block
1319	 * until it's done, and then try again.
1320	 */
1321	if (info->flags & ASYNC_CLOSING) {
1322		interruptible_sleep_on(&info->close_wait);
1323#ifdef SERIAL_DO_RESTART
1324		if (info->flags & ASYNC_HUP_NOTIFY)
1325			return -EAGAIN;
1326		else
1327			return -ERESTARTSYS;
1328#else
1329		return -EAGAIN;
1330#endif
1331	}
1332
1333	/*
1334	 * If non-blocking mode is set, or the port is not enabled,
1335	 * then make the check up front and then exit.
1336	 */
1337	if ((filp->f_flags & O_NONBLOCK) ||
1338	    (tty->flags & (1 << TTY_IO_ERROR))) {
1339		info->flags |= ASYNC_NORMAL_ACTIVE;
1340		return 0;
1341	}
1342
1343	if (tty->termios->c_cflag & CLOCAL)
1344		do_clocal = 1;
1345
1346	/*
1347	 * Block waiting for the carrier detect and the line to become
1348	 * free (i.e., not in use by the callout).  While we are in
1349	 * this loop, info->count is dropped by one, so that
1350	 * mcfrs_close() knows when to free things.  We restore it upon
1351	 * exit, either normal or abnormal.
1352	 */
1353	retval = 0;
1354	add_wait_queue(&info->open_wait, &wait);
1355#ifdef SERIAL_DEBUG_OPEN
1356	printk("block_til_ready before block: ttyS%d, count = %d\n",
1357	       info->line, info->count);
1358#endif
1359	info->count--;
1360	info->blocked_open++;
1361	while (1) {
1362		local_irq_disable();
1363		mcfrs_setsignals(info, 1, 1);
1364		local_irq_enable();
1365		current->state = TASK_INTERRUPTIBLE;
1366		if (tty_hung_up_p(filp) ||
1367		    !(info->flags & ASYNC_INITIALIZED)) {
1368#ifdef SERIAL_DO_RESTART
1369			if (info->flags & ASYNC_HUP_NOTIFY)
1370				retval = -EAGAIN;
1371			else
1372				retval = -ERESTARTSYS;
1373#else
1374			retval = -EAGAIN;
1375#endif
1376			break;
1377		}
1378		if (!(info->flags & ASYNC_CLOSING) &&
1379		    (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1380			break;
1381		if (signal_pending(current)) {
1382			retval = -ERESTARTSYS;
1383			break;
1384		}
1385#ifdef SERIAL_DEBUG_OPEN
1386		printk("block_til_ready blocking: ttyS%d, count = %d\n",
1387		       info->line, info->count);
1388#endif
1389		schedule();
1390	}
1391	current->state = TASK_RUNNING;
1392	remove_wait_queue(&info->open_wait, &wait);
1393	if (!tty_hung_up_p(filp))
1394		info->count++;
1395	info->blocked_open--;
1396#ifdef SERIAL_DEBUG_OPEN
1397	printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1398	       info->line, info->count);
1399#endif
1400	if (retval)
1401		return retval;
1402	info->flags |= ASYNC_NORMAL_ACTIVE;
1403	return 0;
1404}
1405
1406/*
1407 * This routine is called whenever a serial port is opened. It
1408 * enables interrupts for a serial port, linking in its structure into
1409 * the IRQ chain.   It also performs the serial-specific
1410 * initialization for the tty structure.
1411 */
1412int mcfrs_open(struct tty_struct *tty, struct file * filp)
1413{
1414	struct mcf_serial	*info;
1415	int 			retval, line;
1416
1417	line = tty->index;
1418	if ((line < 0) || (line >= NR_PORTS))
1419		return -ENODEV;
1420	info = mcfrs_table + line;
1421	if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1422		return -ENODEV;
1423#ifdef SERIAL_DEBUG_OPEN
1424	printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1425#endif
1426	info->count++;
1427	tty->driver_data = info;
1428	info->tty = tty;
1429
1430	/*
1431	 * Start up serial port
1432	 */
1433	retval = startup(info);
1434	if (retval)
1435		return retval;
1436
1437	retval = block_til_ready(tty, filp, info);
1438	if (retval) {
1439#ifdef SERIAL_DEBUG_OPEN
1440		printk("mcfrs_open returning after block_til_ready with %d\n",
1441		       retval);
1442#endif
1443		return retval;
1444	}
1445
1446#ifdef SERIAL_DEBUG_OPEN
1447	printk("mcfrs_open %s successful...\n", tty->name);
1448#endif
1449	return 0;
1450}
1451
1452/*
1453 *	Based on the line number set up the internal interrupt stuff.
1454 */
1455static void mcfrs_irqinit(struct mcf_serial *info)
1456{
1457#if defined(CONFIG_M5272)
1458	volatile unsigned long	*icrp;
1459	volatile unsigned long	*portp;
1460	volatile unsigned char	*uartp;
1461
1462	uartp = info->addr;
1463	icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1464
1465	switch (info->line) {
1466	case 0:
1467		*icrp = 0xe0000000;
1468		break;
1469	case 1:
1470		*icrp = 0x0e000000;
1471		break;
1472	default:
1473		printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1474			info->line);
1475		return;
1476	}
1477
1478	/* Enable the output lines for the serial ports */
1479	portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1480	*portp = (*portp & ~0x000000ff) | 0x00000055;
1481	portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1482	*portp = (*portp & ~0x000003fc) | 0x000002a8;
1483#elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1484	volatile unsigned char *icrp, *uartp;
1485	volatile unsigned long *imrp;
1486
1487	uartp = info->addr;
1488
1489	icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1490		MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1491	*icrp = 0x30 + info->line; /* level 6, line based priority */
1492
1493	imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1494		MCFINTC_IMRL);
1495	*imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1496#if defined(CONFIG_M527x)
1497	{
1498		/*
1499		 * External Pin Mask Setting & Enable External Pin for Interface
1500		 * mrcbis@aliceposta.it
1501        	 */
1502		u16 *serpin_enable_mask;
1503		serpin_enable_mask = (u16 *) (MCF_IPSBAR + MCF_GPIO_PAR_UART);
1504		if (info->line == 0)
1505			*serpin_enable_mask |= UART0_ENABLE_MASK;
1506		else if (info->line == 1)
1507			*serpin_enable_mask |= UART1_ENABLE_MASK;
1508		else if (info->line == 2)
1509			*serpin_enable_mask |= UART2_ENABLE_MASK;
1510	}
1511#endif
1512#if defined(CONFIG_M528x)
1513	/* make sure PUAPAR is set for UART0 and UART1 */
1514	if (info->line < 2) {
1515		volatile unsigned char *portp = (volatile unsigned char *) (MCF_MBAR + MCF5282_GPIO_PUAPAR);
1516		*portp |= (0x03 << (info->line * 2));
1517	}
1518#endif
1519#elif defined(CONFIG_M520x)
1520	volatile unsigned char *icrp, *uartp;
1521	volatile unsigned long *imrp;
1522
1523	uartp = info->addr;
1524
1525	icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1526		MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1527	*icrp = 0x03;
1528
1529	imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1530		MCFINTC_IMRL);
1531	*imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1532	if (info->line < 2) {
1533		unsigned short *uart_par;
1534		uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1535		if (info->line == 0)
1536			*uart_par |=  MCF_GPIO_PAR_UART_PAR_UTXD0
1537				  | MCF_GPIO_PAR_UART_PAR_URXD0;
1538		else if (info->line == 1)
1539			*uart_par |=  MCF_GPIO_PAR_UART_PAR_UTXD1
1540				  | MCF_GPIO_PAR_UART_PAR_URXD1;
1541		} else if (info->line == 2) {
1542			unsigned char *feci2c_par;
1543			feci2c_par = (unsigned char *)(MCF_IPSBAR +  MCF_GPIO_PAR_FECI2C);
1544			*feci2c_par &= ~0x0F;
1545			*feci2c_par |=  MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1546				    | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1547		}
1548#elif defined(CONFIG_M532x)
1549	volatile unsigned char *uartp;
1550	uartp = info->addr;
1551	switch (info->line) {
1552	case 0:
1553		MCF_INTC0_ICR26 = 0x3;
1554		MCF_INTC0_CIMR = 26;
1555		/* GPIO initialization */
1556		MCF_GPIO_PAR_UART |= 0x000F;
1557		break;
1558	case 1:
1559		MCF_INTC0_ICR27 = 0x3;
1560		MCF_INTC0_CIMR = 27;
1561		/* GPIO initialization */
1562		MCF_GPIO_PAR_UART |= 0x0FF0;
1563		break;
1564	case 2:
1565		MCF_INTC0_ICR28 = 0x3;
1566		MCF_INTC0_CIMR = 28;
1567		/* GPIOs also must be initalized, depends on board */
1568		break;
1569	}
1570#else
1571	volatile unsigned char	*icrp, *uartp;
1572
1573	switch (info->line) {
1574	case 0:
1575		icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1576		*icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1577			MCFSIM_ICR_PRI1;
1578		mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1579		break;
1580	case 1:
1581		icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1582		*icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1583			MCFSIM_ICR_PRI2;
1584		mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1585		break;
1586	default:
1587		printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1588			info->line);
1589		return;
1590	}
1591
1592	uartp = info->addr;
1593	uartp[MCFUART_UIVR] = info->irq;
1594#endif
1595
1596	/* Clear mask, so no surprise interrupts. */
1597	uartp[MCFUART_UIMR] = 0;
1598
1599	if (request_irq(info->irq, mcfrs_interrupt, IRQF_DISABLED,
1600	    "ColdFire UART", NULL)) {
1601		printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1602			"vector=%d\n", info->line, info->irq);
1603	}
1604
1605	return;
1606}
1607
1608
1609char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1610
1611
1612/*
1613 * Serial stats reporting...
1614 */
1615int mcfrs_readproc(char *page, char **start, off_t off, int count,
1616		         int *eof, void *data)
1617{
1618	struct mcf_serial	*info;
1619	char			str[20];
1620	int			len, sigs, i;
1621
1622	len = sprintf(page, mcfrs_drivername);
1623	for (i = 0; (i < NR_PORTS); i++) {
1624		info = &mcfrs_table[i];
1625		len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1626			i, (unsigned int) info->addr, info->irq, info->baud);
1627		if (info->stats.rx || info->stats.tx)
1628			len += sprintf((page + len), "tx:%d rx:%d ",
1629			info->stats.tx, info->stats.rx);
1630		if (info->stats.rxframing)
1631			len += sprintf((page + len), "fe:%d ",
1632			info->stats.rxframing);
1633		if (info->stats.rxparity)
1634			len += sprintf((page + len), "pe:%d ",
1635			info->stats.rxparity);
1636		if (info->stats.rxbreak)
1637			len += sprintf((page + len), "brk:%d ",
1638			info->stats.rxbreak);
1639		if (info->stats.rxoverrun)
1640			len += sprintf((page + len), "oe:%d ",
1641			info->stats.rxoverrun);
1642
1643		str[0] = str[1] = 0;
1644		if ((sigs = mcfrs_getsignals(info))) {
1645			if (sigs & TIOCM_RTS)
1646				strcat(str, "|RTS");
1647			if (sigs & TIOCM_CTS)
1648				strcat(str, "|CTS");
1649			if (sigs & TIOCM_DTR)
1650				strcat(str, "|DTR");
1651			if (sigs & TIOCM_CD)
1652				strcat(str, "|CD");
1653		}
1654
1655		len += sprintf((page + len), "%s\n", &str[1]);
1656	}
1657
1658	return(len);
1659}
1660
1661
1662/* Finally, routines used to initialize the serial driver. */
1663
1664static void show_serial_version(void)
1665{
1666	printk(mcfrs_drivername);
1667}
1668
1669static const struct tty_operations mcfrs_ops = {
1670	.open = mcfrs_open,
1671	.close = mcfrs_close,
1672	.write = mcfrs_write,
1673	.flush_chars = mcfrs_flush_chars,
1674	.write_room = mcfrs_write_room,
1675	.chars_in_buffer = mcfrs_chars_in_buffer,
1676	.flush_buffer = mcfrs_flush_buffer,
1677	.ioctl = mcfrs_ioctl,
1678	.throttle = mcfrs_throttle,
1679	.unthrottle = mcfrs_unthrottle,
1680	.set_termios = mcfrs_set_termios,
1681	.stop = mcfrs_stop,
1682	.start = mcfrs_start,
1683	.hangup = mcfrs_hangup,
1684	.read_proc = mcfrs_readproc,
1685	.wait_until_sent = mcfrs_wait_until_sent,
1686 	.tiocmget = mcfrs_tiocmget,
1687	.tiocmset = mcfrs_tiocmset,
1688};
1689
1690/* mcfrs_init inits the driver */
1691static int __init
1692mcfrs_init(void)
1693{
1694	struct mcf_serial	*info;
1695	unsigned long		flags;
1696	int			i;
1697
1698	/* Setup base handler, and timer table. */
1699#ifdef MCFPP_DCD0
1700	init_timer(&mcfrs_timer_struct);
1701	mcfrs_timer_struct.function = mcfrs_timer;
1702	mcfrs_timer_struct.data = 0;
1703	mcfrs_timer_struct.expires = jiffies + HZ/25;
1704	add_timer(&mcfrs_timer_struct);
1705	mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1706#endif
1707	mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1708	if (!mcfrs_serial_driver)
1709		return -ENOMEM;
1710
1711	show_serial_version();
1712
1713	/* Initialize the tty_driver structure */
1714	mcfrs_serial_driver->owner = THIS_MODULE;
1715	mcfrs_serial_driver->name = "ttyS";
1716	mcfrs_serial_driver->driver_name = "mcfserial";
1717	mcfrs_serial_driver->major = TTY_MAJOR;
1718	mcfrs_serial_driver->minor_start = 64;
1719	mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1720	mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1721	mcfrs_serial_driver->init_termios = tty_std_termios;
1722
1723	mcfrs_serial_driver->init_termios.c_cflag =
1724		mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1725	mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1726
1727	tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1728
1729	if (tty_register_driver(mcfrs_serial_driver)) {
1730		printk("MCFRS: Couldn't register serial driver\n");
1731		put_tty_driver(mcfrs_serial_driver);
1732		return(-EBUSY);
1733	}
1734
1735	local_irq_save(flags);
1736
1737	/*
1738	 *	Configure all the attached serial ports.
1739	 */
1740	for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1741		info->magic = SERIAL_MAGIC;
1742		info->line = i;
1743		info->tty = 0;
1744		info->custom_divisor = 16;
1745		info->close_delay = 50;
1746		info->closing_wait = 3000;
1747		info->x_char = 0;
1748		info->event = 0;
1749		info->count = 0;
1750		info->blocked_open = 0;
1751		INIT_WORK(&info->tqueue, mcfrs_offintr);
1752		INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1753		init_waitqueue_head(&info->open_wait);
1754		init_waitqueue_head(&info->close_wait);
1755
1756		info->imr = 0;
1757		mcfrs_setsignals(info, 0, 0);
1758		mcfrs_irqinit(info);
1759
1760		printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1761			(unsigned int) info->addr, info->irq);
1762		printk(" is a builtin ColdFire UART\n");
1763	}
1764
1765	local_irq_restore(flags);
1766	return 0;
1767}
1768
1769module_init(mcfrs_init);
1770
1771/****************************************************************************/
1772/*                          Serial Console                                  */
1773/****************************************************************************/
1774
1775/*
1776 *	Quick and dirty UART initialization, for console output.
1777 */
1778
1779void mcfrs_init_console(void)
1780{
1781	volatile unsigned char	*uartp;
1782	unsigned int		clk;
1783
1784	/*
1785	 *	Reset UART, get it into known state...
1786	 */
1787	uartp = (volatile unsigned char *) (MCF_MBAR +
1788		(mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1789
1790	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
1791	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
1792	uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;  /* reset MR pointer */
1793
1794	/*
1795	 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1796	 */
1797	uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1798	uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1799
1800#ifdef	CONFIG_M5272
1801{
1802	/*
1803	 * For the MCF5272, also compute the baudrate fraction.
1804	 */
1805	int fraction = MCF_BUSCLK - (clk * 32 * mcfrs_console_baud);
1806	fraction *= 16;
1807	fraction /= (32 * mcfrs_console_baud);
1808	uartp[MCFUART_UFPD] = (fraction & 0xf);		/* set fraction */
1809	clk = (MCF_BUSCLK / mcfrs_console_baud) / 32;
1810}
1811#else
1812	clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1813#endif
1814
1815	uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8;  /* set msb baud */
1816	uartp[MCFUART_UBG2] = (clk & 0xff);  /* set lsb baud */
1817	uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1818	uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1819
1820	mcfrs_console_inited++;
1821	return;
1822}
1823
1824
1825/*
1826 *	Setup for console. Argument comes from the boot command line.
1827 */
1828
1829int mcfrs_console_setup(struct console *cp, char *arg)
1830{
1831	int		i, n = CONSOLE_BAUD_RATE;
1832
1833	if (!cp)
1834		return(-1);
1835
1836	if (!strncmp(cp->name, "ttyS", 4))
1837		mcfrs_console_port = cp->index;
1838	else if (!strncmp(cp->name, "cua", 3))
1839		mcfrs_console_port = cp->index;
1840	else
1841		return(-1);
1842
1843	if (arg)
1844		n = simple_strtoul(arg,NULL,0);
1845	for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1846		if (mcfrs_baud_table[i] == n)
1847			break;
1848	if (i < MCFRS_BAUD_TABLE_SIZE) {
1849		mcfrs_console_baud = n;
1850		mcfrs_console_cbaud = 0;
1851		if (i > 15) {
1852			mcfrs_console_cbaud |= CBAUDEX;
1853			i -= 15;
1854		}
1855		mcfrs_console_cbaud |= i;
1856	}
1857	mcfrs_init_console(); /* make sure baud rate changes */
1858	return(0);
1859}
1860
1861
1862static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1863{
1864	*index = c->index;
1865	return mcfrs_serial_driver;
1866}
1867
1868
1869/*
1870 *	Output a single character, using UART polled mode.
1871 *	This is used for console output.
1872 */
1873
1874void mcfrs_put_char(char ch)
1875{
1876	volatile unsigned char	*uartp;
1877	unsigned long		flags;
1878	int			i;
1879
1880	uartp = (volatile unsigned char *) (MCF_MBAR +
1881		(mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1882
1883	local_irq_save(flags);
1884	for (i = 0; (i < 0x10000); i++) {
1885		if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1886			break;
1887	}
1888	if (i < 0x10000) {
1889		uartp[MCFUART_UTB] = ch;
1890		for (i = 0; (i < 0x10000); i++)
1891			if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1892				break;
1893	}
1894	if (i >= 0x10000)
1895		mcfrs_init_console(); /* try and get it back */
1896	local_irq_restore(flags);
1897
1898	return;
1899}
1900
1901
1902/*
1903 * rs_console_write is registered for printk output.
1904 */
1905
1906void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1907{
1908	if (!mcfrs_console_inited)
1909		mcfrs_init_console();
1910	while (len-- > 0) {
1911		if (*p == '\n')
1912			mcfrs_put_char('\r');
1913		mcfrs_put_char(*p++);
1914	}
1915}
1916
1917/*
1918 * declare our consoles
1919 */
1920
1921struct console mcfrs_console = {
1922	.name		= "ttyS",
1923	.write		= mcfrs_console_write,
1924	.device		= mcfrs_console_device,
1925	.setup		= mcfrs_console_setup,
1926	.flags		= CON_PRINTBUFFER,
1927	.index		= -1,
1928};
1929
1930static int __init mcfrs_console_init(void)
1931{
1932	register_console(&mcfrs_console);
1933	return 0;
1934}
1935
1936console_initcall(mcfrs_console_init);
1937
1938/****************************************************************************/
1939