File: /usr/src/linux/arch/ppc/8xx_io/uart.c

1     /*
2      * BK Id: SCCS/s.uart.c 1.14 06/27/01 14:49:55 trini
3      */
4     /*
5      *  UART driver for MPC860 CPM SCC or SMC
6      *  Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
7      *
8      * I used the serial.c driver as the framework for this driver.
9      * Give credit to those guys.
10      * The original code was written for the MBX860 board.  I tried to make
11      * it generic, but there may be some assumptions in the structures that
12      * have to be fixed later.
13      * To save porting time, I did not bother to change any object names
14      * that are not accessed outside of this file.
15      * It still needs lots of work........When it was easy, I included code
16      * to support the SCCs, but this has never been tested, nor is it complete.
17      * Only the SCCs support modem control, so that is not complete either.
18      *
19      * This module exports the following rs232 io functions:
20      *
21      *	int rs_8xx_init(void);
22      */
23     
24     #include <linux/config.h>
25     #include <linux/module.h>
26     #include <linux/errno.h>
27     #include <linux/signal.h>
28     #include <linux/sched.h>
29     #include <linux/timer.h>
30     #include <linux/interrupt.h>
31     #include <linux/tty.h>
32     #include <linux/tty_flip.h>
33     #include <linux/serial.h>
34     #include <linux/serialP.h>
35     #include <linux/major.h>
36     #include <linux/string.h>
37     #include <linux/fcntl.h>
38     #include <linux/ptrace.h>
39     #include <linux/mm.h>
40     #include <linux/slab.h>
41     #include <linux/init.h>
42     #include <linux/delay.h>
43     #include <asm/uaccess.h>
44     #include <asm/8xx_immap.h>
45     #include <asm/mpc8xx.h>
46     #include "commproc.h"
47     #ifdef CONFIG_MAGIC_SYSRQ
48     #include <linux/sysrq.h>
49     #endif
50     
51     #ifdef CONFIG_KGDB
52     extern void breakpoint(void);
53     extern void set_debug_traps(void);
54     extern int  kgdb_output_string (const char* s, unsigned int count);
55     #endif
56     
57     #ifdef CONFIG_SERIAL_CONSOLE
58     #include <linux/console.h>
59     
60     /* this defines the index into rs_table for the port to use
61     */
62     # ifndef CONFIG_SERIAL_CONSOLE_PORT
63     #  ifdef CONFIG_SCC3_ENET
64     #   ifdef CONFIG_CONS_SMC2
65     #    define CONFIG_SERIAL_CONSOLE_PORT	0	/* Console on SMC2 is 1st port */
66     #   else
67     #    error "Can't use SMC1 for console with Ethernet on SCC3"
68     #   endif
69     #  else	/* ! CONFIG_SCC3_ENET */
70     #   ifdef CONFIG_CONS_SMC2			/* Console on SMC2 */
71     #    define CONFIG_SERIAL_CONSOLE_PORT	1
72     #   else					/* Console on SMC1 */
73     #    define CONFIG_SERIAL_CONSOLE_PORT	0
74     #   endif /* CONFIG_CONS_SMC2 */
75     #  endif  /* CONFIG_SCC3_ENET */
76     # endif	  /* CONFIG_SERIAL_CONSOLE_PORT */
77     #endif	  /* CONFIG_SERIAL_CONSOLE */
78     
79     #if 0
80     /* SCC2 for console
81     */
82     #undef CONFIG_SERIAL_CONSOLE_PORT
83     #define CONFIG_SERIAL_CONSOLE_PORT	2
84     #endif
85     
86     #define TX_WAKEUP	ASYNC_SHARE_IRQ
87     
88     static char *serial_name = "CPM UART driver";
89     static char *serial_version = "0.03";
90     
91     static DECLARE_TASK_QUEUE(tq_serial);
92     
93     static struct tty_driver serial_driver, callout_driver;
94     static int serial_refcount;
95     static int serial_console_setup(struct console *co, char *options);
96     
97     static int serial_console_wait_key(struct console *co);
98     static void serial_console_write(struct console *c, const char *s,
99     				unsigned count);
100     static kdev_t serial_console_device(struct console *c);
101     
102     #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
103     static unsigned long break_pressed; /* break, really ... */
104     #endif
105     
106     /*
107      * Serial driver configuration section.  Here are the various options:
108      */
109     #define SERIAL_PARANOIA_CHECK
110     #define CONFIG_SERIAL_NOPAUSE_IO
111     #define SERIAL_DO_RESTART
112     
113     /* Set of debugging defines */
114     
115     #undef SERIAL_DEBUG_INTR
116     #undef SERIAL_DEBUG_OPEN
117     #undef SERIAL_DEBUG_FLOW
118     #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
119     
120     #define _INLINE_ inline
121       
122     #define DBG_CNT(s)
123     
124     /* We overload some of the items in the data structure to meet our
125      * needs.  For example, the port address is the CPM parameter ram
126      * offset for the SCC or SMC.  The maximum number of ports is 4 SCCs and
127      * 2 SMCs.  The "hub6" field is used to indicate the channel number, with
128      * a flag indicating SCC or SMC, and the number is used as an index into
129      * the CPM parameter area for this device.
130      * The "type" field is currently set to 0, for PORT_UNKNOWN.  It is
131      * not currently used.  I should probably use it to indicate the port
132      * type of SMC or SCC.
133      * The SMCs do not support any modem control signals.
134      */
135     #define smc_scc_num	hub6
136     #define NUM_IS_SCC	((int)0x00010000)
137     #define PORT_NUM(P)	((P) & 0x0000ffff)
138     
139     /* Processors other than the 860 only get SMCs configured by default.
140      * Either they don't have SCCs or they are allocated somewhere else.
141      * Of course, there are now 860s without some SCCs, so we will need to
142      * address that someday.
143      * The Embedded Planet Multimedia I/O cards use TDM interfaces to the
144      * stereo codec parts, and we use SMC2 to help support that.
145      */
146     static struct serial_state rs_table[] = {
147     	/* UART CLK   PORT          IRQ      FLAGS  NUM   */
148     #ifndef CONFIG_SCC3_ENET	/* SMC1 not usable with Ethernet on SCC3 */
149       	{ 0,     0, PROFF_SMC1, CPMVEC_SMC1,   0,    0 },    /* SMC1 ttyS0 */
150     #endif
151     #if !defined(CONFIG_USB_MPC8xx) && !defined(CONFIG_USB_CLIENT_MPC8xx)
152     # ifdef CONFIG_SMC2_UART
153       	{ 0,     0, PROFF_SMC2, CPMVEC_SMC2,   0,    1 },    /* SMC2 ttyS1 */
154     # endif
155     # ifdef CONFIG_USE_SCC_IO
156       	{ 0,     0, PROFF_SCC2, CPMVEC_SCC2,   0,    (NUM_IS_SCC | 1) },    /* SCC2 ttyS2 */
157       	{ 0,     0, PROFF_SCC3, CPMVEC_SCC3,   0,    (NUM_IS_SCC | 2) },    /* SCC3 ttyS3 */
158     # endif
159       #else /* CONFIG_USB_xxx */
160     # ifdef CONFIG_USE_SCC_IO
161       	{ 0,     0, PROFF_SCC3, CPMVEC_SCC3,   0,    (NUM_IS_SCC | 2) },    /* SCC3 ttyS3 */
162     # endif
163     #endif	/* CONFIG_USB_xxx */
164     };
165     
166     #define NR_PORTS	(sizeof(rs_table)/sizeof(struct serial_state))
167     
168     static struct tty_struct *serial_table[NR_PORTS];
169     static struct termios *serial_termios[NR_PORTS];
170     static struct termios *serial_termios_locked[NR_PORTS];
171     
172     /* The number of buffer descriptors and their sizes.
173     */
174     #define RX_NUM_FIFO	4
175     #define RX_BUF_SIZE	32
176     #define TX_NUM_FIFO	4
177     #define TX_BUF_SIZE	32
178     
179     #ifndef MIN
180     #define MIN(a,b)	((a) < (b) ? (a) : (b))
181     #endif
182     
183     /* The async_struct in serial.h does not really give us what we
184      * need, so define our own here.
185      */
186     typedef struct serial_info {
187     	int			magic;
188     	int			flags;
189     	struct serial_state	*state;
190     	struct tty_struct 	*tty;
191     	int			read_status_mask;
192     	int			ignore_status_mask;
193     	int			timeout;
194     	int			line;
195     	int			x_char;	/* xon/xoff character */
196     	int			close_delay;
197     	unsigned short		closing_wait;
198     	unsigned short		closing_wait2;
199     	unsigned long		event;
200     	unsigned long		last_active;
201     	int			blocked_open; /* # of blocked opens */
202     	long			session; /* Session of opening process */
203     	long			pgrp; /* pgrp of opening process */
204     	struct tq_struct	tqueue;
205     	struct tq_struct	tqueue_hangup;
206     	wait_queue_head_t	open_wait;
207     	wait_queue_head_t	close_wait;
208     
209     	/* CPM Buffer Descriptor pointers.
210     	*/
211     	cbd_t			*rx_bd_base;
212     	cbd_t			*rx_cur;
213     	cbd_t			*tx_bd_base;
214     	cbd_t			*tx_cur;
215     } ser_info_t;
216     
217     static struct console sercons = {
218     	name:		"ttyS",
219     	write:		serial_console_write,
220     	device:		serial_console_device,
221     	wait_key:	serial_console_wait_key,
222     	setup:		serial_console_setup,
223     	flags:		CON_PRINTBUFFER,
224     	index:		CONFIG_SERIAL_CONSOLE_PORT,
225     };
226     
227     static void change_speed(ser_info_t *info);
228     static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout);
229     
230     static inline int serial_paranoia_check(ser_info_t *info,
231     					kdev_t device, const char *routine)
232     {
233     #ifdef SERIAL_PARANOIA_CHECK
234     	static const char *badmagic =
235     		"Warning: bad magic number for serial struct (%s) in %s\n";
236     	static const char *badinfo =
237     		"Warning: null async_struct for (%s) in %s\n";
238     
239     	if (!info) {
240     		printk(badinfo, kdevname(device), routine);
241     		return 1;
242     	}
243     	if (info->magic != SERIAL_MAGIC) {
244     		printk(badmagic, kdevname(device), routine);
245     		return 1;
246     	}
247     #endif
248     	return 0;
249     }
250     
251     /*
252      * This is used to figure out the divisor speeds and the timeouts,
253      * indexed by the termio value.  The generic CPM functions are responsible
254      * for setting and assigning baud rate generators for us.
255      */
256     static int baud_table[] = {
257     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
258     	9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
259     
260     
261     /*
262      * ------------------------------------------------------------
263      * rs_stop() and rs_start()
264      *
265      * This routines are called before setting or resetting tty->stopped.
266      * They enable or disable transmitter interrupts, as necessary.
267      * ------------------------------------------------------------
268      */
269     static void rs_8xx_stop(struct tty_struct *tty)
270     {
271     	ser_info_t *info = (ser_info_t *)tty->driver_data;
272     	int	idx;
273     	unsigned long flags;
274     	volatile scc_t	*sccp;
275     	volatile smc_t	*smcp;
276     
277     	if (serial_paranoia_check(info, tty->device, "rs_stop"))
278     		return;
279     	
280     	save_flags(flags); cli();
281     	idx = PORT_NUM(info->state->smc_scc_num);
282     	if (info->state->smc_scc_num & NUM_IS_SCC) {
283     		sccp = &cpmp->cp_scc[idx];
284     		sccp->scc_sccm &= ~UART_SCCM_TX;
285     	}
286     	else {
287     		smcp = &cpmp->cp_smc[idx];
288     		smcp->smc_smcm &= ~SMCM_TX;
289     	}
290     	restore_flags(flags);
291     }
292     
293     static void rs_8xx_start(struct tty_struct *tty)
294     {
295     	ser_info_t *info = (ser_info_t *)tty->driver_data;
296     	int	idx;
297     	unsigned long flags;
298     	volatile scc_t	*sccp;
299     	volatile smc_t	*smcp;
300     
301     	if (serial_paranoia_check(info, tty->device, "rs_stop"))
302     		return;
303     	
304     	idx = PORT_NUM(info->state->smc_scc_num);
305     	save_flags(flags); cli();
306     	if (info->state->smc_scc_num & NUM_IS_SCC) {
307     		sccp = &cpmp->cp_scc[idx];
308     		sccp->scc_sccm |= UART_SCCM_TX;
309     	}
310     	else {
311     		smcp = &cpmp->cp_smc[idx];
312     		smcp->smc_smcm |= SMCM_TX;
313     	}
314     	restore_flags(flags);
315     }
316     
317     /*
318      * ----------------------------------------------------------------------
319      *
320      * Here starts the interrupt handling routines.  All of the following
321      * subroutines are declared as inline and are folded into
322      * rs_interrupt().  They were separated out for readability's sake.
323      *
324      * Note: rs_interrupt() is a "fast" interrupt, which means that it
325      * runs with interrupts turned off.  People who may want to modify
326      * rs_interrupt() should try to keep the interrupt handler as fast as
327      * possible.  After you are done making modifications, it is not a bad
328      * idea to do:
329      * 
330      * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
331      *
332      * and look at the resulting assemble code in serial.s.
333      *
334      * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
335      * -----------------------------------------------------------------------
336      */
337     
338     /*
339      * This routine is used by the interrupt handler to schedule
340      * processing in the software interrupt portion of the driver.
341      */
342     static _INLINE_ void rs_sched_event(ser_info_t *info,
343     				  int event)
344     {
345     	info->event |= 1 << event;
346     	queue_task(&info->tqueue, &tq_serial);
347     	mark_bh(SERIAL_BH);
348     }
349     
350     static _INLINE_ void receive_chars(ser_info_t *info, struct pt_regs *regs)
351     {
352     	struct tty_struct *tty = info->tty;
353     	unsigned char ch, *cp;
354     	/*int	ignored = 0;*/
355     	int	i;
356     	ushort	status;
357     	struct	async_icount *icount;
358     	volatile cbd_t	*bdp;
359     
360     	icount = &info->state->icount;
361     
362     	/* Just loop through the closed BDs and copy the characters into
363     	 * the buffer.
364     	 */
365     	bdp = info->rx_cur;
366     	for (;;) {
367     		if (bdp->cbd_sc & BD_SC_EMPTY)	/* If this one is empty */
368     			break;			/*   we are all done */
369     
370     		/* The read status mask tell us what we should do with
371     		 * incoming characters, especially if errors occur.
372     		 * One special case is the use of BD_SC_EMPTY.  If
373     		 * this is not set, we are supposed to be ignoring
374     		 * inputs.  In this case, just mark the buffer empty and
375     		 * continue.
376     		if (!(info->read_status_mask & BD_SC_EMPTY)) {
377     			bdp->cbd_sc |= BD_SC_EMPTY;
378     			bdp->cbd_sc &=
379     				~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
380     
381     			if (bdp->cbd_sc & BD_SC_WRAP)
382     				bdp = info->rx_bd_base;
383     			else
384     				bdp++;
385     			continue;
386     		}
387     		 */
388     
389     		/* Get the number of characters and the buffer pointer.
390     		*/
391     		i = bdp->cbd_datlen;
392     		cp = (unsigned char *)__va(bdp->cbd_bufaddr);
393     		status = bdp->cbd_sc;
394     
395     		/* Check to see if there is room in the tty buffer for
396     		 * the characters in our BD buffer.  If not, we exit
397     		 * now, leaving the BD with the characters.  We'll pick
398     		 * them up again on the next receive interrupt (which could
399     		 * be a timeout).
400     		 */
401     		if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
402     			break;
403     
404     		while (i-- > 0) {
405     			ch = *cp++;
406     			*tty->flip.char_buf_ptr = ch;
407     			icount->rx++;
408     
409     #ifdef SERIAL_DEBUG_INTR
410     			printk("DR%02x:%02x...", ch, status);
411     #endif
412     			*tty->flip.flag_buf_ptr = 0;
413     			if (status & (BD_SC_BR | BD_SC_FR |
414     				       BD_SC_PR | BD_SC_OV)) {
415     				/*
416     				 * For statistics only
417     				 */
418     				if (status & BD_SC_BR)
419     					icount->brk++;
420     				else if (status & BD_SC_PR)
421     					icount->parity++;
422     				else if (status & BD_SC_FR)
423     					icount->frame++;
424     				if (status & BD_SC_OV)
425     					icount->overrun++;
426     
427     				/*
428     				 * Now check to see if character should be
429     				 * ignored, and mask off conditions which
430     				 * should be ignored.
431     				if (status & info->ignore_status_mask) {
432     					if (++ignored > 100)
433     						break;
434     					continue;
435     				}
436     				 */
437     				status &= info->read_status_mask;
438     
439     				if (status & (BD_SC_BR)) {
440     #ifdef SERIAL_DEBUG_INTR
441     					printk("handling break....");
442     #endif
443     					*tty->flip.flag_buf_ptr = TTY_BREAK;
444     					if (info->flags & ASYNC_SAK)
445     						do_SAK(tty);
446     				} else if (status & BD_SC_PR)
447     					*tty->flip.flag_buf_ptr = TTY_PARITY;
448     				else if (status & BD_SC_FR)
449     					*tty->flip.flag_buf_ptr = TTY_FRAME;
450     				if (status & BD_SC_OV) {
451     					/*
452     					 * Overrun is special, since it's
453     					 * reported immediately, and doesn't
454     					 * affect the current character
455     					 */
456     					if (tty->flip.count < TTY_FLIPBUF_SIZE) {
457     						tty->flip.count++;
458     						tty->flip.flag_buf_ptr++;
459     						tty->flip.char_buf_ptr++;
460     						*tty->flip.flag_buf_ptr =
461     								TTY_OVERRUN;
462     					}
463     				}
464     			}
465     #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
466     			if (break_pressed && info->line == sercons.index) {
467     				if (ch != 0 && time_before(jiffies, 
468     							break_pressed + HZ*5)) {
469     					handle_sysrq(ch, regs, NULL, NULL);
470     					break_pressed = 0;
471     					goto ignore_char;
472     				} else
473     					break_pressed = 0;
474     			}
475     #endif
476     			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
477     				break;
478     
479     			tty->flip.flag_buf_ptr++;
480     			tty->flip.char_buf_ptr++;
481     			tty->flip.count++;
482     		}
483     
484     	ignore_char:	
485     		/* This BD is ready to be used again.  Clear status.
486     		 * Get next BD.
487     		 */
488     		bdp->cbd_sc |= BD_SC_EMPTY;
489     		bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
490     
491     		if (bdp->cbd_sc & BD_SC_WRAP)
492     			bdp = info->rx_bd_base;
493     		else
494     			bdp++;
495     	}
496     	info->rx_cur = (cbd_t *)bdp;
497     
498     	queue_task(&tty->flip.tqueue, &tq_timer);
499     }
500     
501     static _INLINE_ void receive_break(ser_info_t *info, struct pt_regs *regs)
502     {
503     	struct tty_struct *tty = info->tty;
504     
505     	info->state->icount.brk++;
506     
507     #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
508     	if (info->line == sercons.index) {
509     		if (!break_pressed) {
510     			break_pressed = jiffies;
511     			return;
512     		} else
513     			break_pressed = 0;
514     	}
515     #endif
516     
517     	/* Check to see if there is room in the tty buffer for
518     	 * the break.  If not, we exit now, losing the break.  FIXME
519     	 */
520     	if ((tty->flip.count + 1) >= TTY_FLIPBUF_SIZE)
521     		return;
522     	*(tty->flip.flag_buf_ptr++) = TTY_BREAK;
523     	*(tty->flip.char_buf_ptr++) = 0;
524     	tty->flip.count++;
525     
526     	queue_task(&tty->flip.tqueue, &tq_timer);
527     }
528     
529     static _INLINE_ void transmit_chars(ser_info_t *info, struct pt_regs *regs)
530     {
531     	
532     	if ((info->flags & TX_WAKEUP) ||
533     	    (info->tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
534     		rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
535     	}
536     
537     #ifdef SERIAL_DEBUG_INTR
538     	printk("THRE...");
539     #endif
540     }
541     
542     #ifdef notdef
543     	/* I need to do this for the SCCs, so it is left as a reminder.
544     	*/
545     static _INLINE_ void check_modem_status(struct async_struct *info)
546     {
547     	int	status;
548     	struct	async_icount *icount;
549     	
550     	status = serial_in(info, UART_MSR);
551     
552     	if (status & UART_MSR_ANY_DELTA) {
553     		icount = &info->state->icount;
554     		/* update input line counters */
555     		if (status & UART_MSR_TERI)
556     			icount->rng++;
557     		if (status & UART_MSR_DDSR)
558     			icount->dsr++;
559     		if (status & UART_MSR_DDCD) {
560     			icount->dcd++;
561     #ifdef CONFIG_HARD_PPS
562     			if ((info->flags & ASYNC_HARDPPS_CD) &&
563     			    (status & UART_MSR_DCD))
564     				hardpps();
565     #endif
566     		}
567     		if (status & UART_MSR_DCTS)
568     			icount->cts++;
569     		wake_up_interruptible(&info->delta_msr_wait);
570     	}
571     
572     	if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
573     #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
574     		printk("ttys%d CD now %s...", info->line,
575     		       (status & UART_MSR_DCD) ? "on" : "off");
576     #endif		
577     		if (status & UART_MSR_DCD)
578     			wake_up_interruptible(&info->open_wait);
579     		else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
580     			   (info->flags & ASYNC_CALLOUT_NOHUP))) {
581     #ifdef SERIAL_DEBUG_OPEN
582     			printk("scheduling hangup...");
583     #endif
584     			MOD_INC_USE_COUNT;
585     			if (schedule_task(&info->tqueue_hangup) == 0)
586     				MOD_DEC_USE_COUNT;
587     		}
588     	}
589     	if (info->flags & ASYNC_CTS_FLOW) {
590     		if (info->tty->hw_stopped) {
591     			if (status & UART_MSR_CTS) {
592     #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
593     				printk("CTS tx start...");
594     #endif
595     				info->tty->hw_stopped = 0;
596     				info->IER |= UART_IER_THRI;
597     				serial_out(info, UART_IER, info->IER);
598     				rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
599     				return;
600     			}
601     		} else {
602     			if (!(status & UART_MSR_CTS)) {
603     #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
604     				printk("CTS tx stop...");
605     #endif
606     				info->tty->hw_stopped = 1;
607     				info->IER &= ~UART_IER_THRI;
608     				serial_out(info, UART_IER, info->IER);
609     			}
610     		}
611     	}
612     }
613     #endif
614     
615     /*
616      * This is the serial driver's interrupt routine for a single port
617      */
618     static void rs_8xx_interrupt(void *dev_id, struct pt_regs *regs)
619     {
620     	u_char	events;
621     	int	idx;
622     	ser_info_t *info;
623     	volatile smc_t	*smcp;
624     	volatile scc_t	*sccp;
625     	
626     	info = (ser_info_t *)dev_id;
627     
628     	idx = PORT_NUM(info->state->smc_scc_num);
629     	if (info->state->smc_scc_num & NUM_IS_SCC) {
630     		sccp = &cpmp->cp_scc[idx];
631     		events = sccp->scc_scce;
632     		if (events & SMCM_BRKE)
633     			receive_break(info, regs);
634     		if (events & SCCM_RX)
635     			receive_chars(info, regs);
636     		if (events & SCCM_TX)
637     			transmit_chars(info, regs);
638     		sccp->scc_scce = events;
639     	}
640     	else {
641     		smcp = &cpmp->cp_smc[idx];
642     		events = smcp->smc_smce;
643     		if (events & SMCM_BRKE)
644     			receive_break(info, regs);
645     		if (events & SMCM_RX)
646     			receive_chars(info, regs);
647     		if (events & SMCM_TX)
648     			transmit_chars(info, regs);
649     		smcp->smc_smce = events;
650     	}
651     	
652     #ifdef SERIAL_DEBUG_INTR
653     	printk("rs_interrupt_single(%d, %x)...",
654     					info->state->smc_scc_num, events);
655     #endif
656     #ifdef modem_control
657     	check_modem_status(info);
658     #endif
659     	info->last_active = jiffies;
660     #ifdef SERIAL_DEBUG_INTR
661     	printk("end.\n");
662     #endif
663     }
664     
665     
666     /*
667      * -------------------------------------------------------------------
668      * Here ends the serial interrupt routines.
669      * -------------------------------------------------------------------
670      */
671     
672     /*
673      * This routine is used to handle the "bottom half" processing for the
674      * serial driver, known also the "software interrupt" processing.
675      * This processing is done at the kernel interrupt level, after the
676      * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
677      * is where time-consuming activities which can not be done in the
678      * interrupt driver proper are done; the interrupt driver schedules
679      * them using rs_sched_event(), and they get done here.
680      */
681     static void do_serial_bh(void)
682     {
683     	run_task_queue(&tq_serial);
684     }
685     
686     static void do_softint(void *private_)
687     {
688     	ser_info_t	*info = (ser_info_t *) private_;
689     	struct tty_struct	*tty;
690     	
691     	tty = info->tty;
692     	if (!tty)
693     		return;
694     
695     	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
696     		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
697     		    tty->ldisc.write_wakeup)
698     			(tty->ldisc.write_wakeup)(tty);
699     		wake_up_interruptible(&tty->write_wait);
700     	}
701     }
702     
703     /*
704      * This routine is called from the scheduler tqueue when the interrupt
705      * routine has signalled that a hangup has occurred.  The path of
706      * hangup processing is:
707      *
708      * 	serial interrupt routine -> (scheduler tqueue) ->
709      * 	do_serial_hangup() -> tty->hangup() -> rs_hangup()
710      * 
711      */
712     static void do_serial_hangup(void *private_)
713     {
714     	struct async_struct	*info = (struct async_struct *) private_;
715     	struct tty_struct	*tty;
716     	
717     	tty = info->tty;
718     	if (tty)
719     		tty_hangup(tty);
720     	MOD_DEC_USE_COUNT;
721     }
722     
723     /*static void rs_8xx_timer(void)
724     {
725     	printk("rs_8xx_timer\n");
726     }*/
727     
728     
729     static int startup(ser_info_t *info)
730     {
731     	unsigned long flags;
732     	int	retval=0;
733     	int	idx;
734     	struct serial_state *state= info->state;
735     	volatile smc_t		*smcp;
736     	volatile scc_t		*sccp;
737     	volatile smc_uart_t	*up;
738     	volatile scc_uart_t	*scup;
739     
740     
741     	save_flags(flags); cli();
742     
743     	if (info->flags & ASYNC_INITIALIZED) {
744     		goto errout;
745     	}
746     
747     #ifdef maybe
748     	if (!state->port || !state->type) {
749     		if (info->tty)
750     			set_bit(TTY_IO_ERROR, &info->tty->flags);
751     		goto errout;
752     	}
753     #endif
754     
755     #ifdef SERIAL_DEBUG_OPEN
756     	printk("starting up ttys%d (irq %d)...", info->line, state->irq);
757     #endif
758     
759     
760     #ifdef modem_control
761     	info->MCR = 0;
762     	if (info->tty->termios->c_cflag & CBAUD)
763     		info->MCR = UART_MCR_DTR | UART_MCR_RTS;
764     #endif
765     	
766     	if (info->tty)
767     		clear_bit(TTY_IO_ERROR, &info->tty->flags);
768     
769     	/*
770     	 * and set the speed of the serial port
771     	 */
772     	change_speed(info);
773     	
774     	idx = PORT_NUM(info->state->smc_scc_num);
775     	if (info->state->smc_scc_num & NUM_IS_SCC) {
776     		sccp = &cpmp->cp_scc[idx];
777     		scup = (scc_uart_t *)&cpmp->cp_dparam[state->port];
778     		scup->scc_genscc.scc_mrblr = RX_BUF_SIZE;
779     		scup->scc_maxidl = RX_BUF_SIZE;
780     		sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
781     		sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
782     	}
783     	else {
784     		smcp = &cpmp->cp_smc[idx];
785     
786     		/* Enable interrupts and I/O.
787     		*/
788     		smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
789     		smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
790     
791     		/* We can tune the buffer length and idle characters
792     		 * to take advantage of the entire incoming buffer size.
793     		 * If mrblr is something other than 1, maxidl has to be
794     		 * non-zero or we never get an interrupt.  The maxidl
795     		 * is the number of character times we wait after reception
796     		 * of the last character before we decide no more characters
797     		 * are coming.
798     		 */
799     		up = (smc_uart_t *)&cpmp->cp_dparam[state->port];
800     		up->smc_mrblr = RX_BUF_SIZE;
801     		up->smc_maxidl = RX_BUF_SIZE;
802     		up->smc_brkcr = 1;	/* number of break chars */
803     	}
804     
805     	info->flags |= ASYNC_INITIALIZED;
806     	restore_flags(flags);
807     	return 0;
808     	
809     errout:
810     	restore_flags(flags);
811     	return retval;
812     }
813     
814     /*
815      * This routine will shutdown a serial port; interrupts are disabled, and
816      * DTR is dropped if the hangup on close termio flag is on.
817      */
818     static void shutdown(ser_info_t * info)
819     {
820     	unsigned long	flags;
821     	struct serial_state *state;
822     	int		idx;
823     	volatile smc_t	*smcp;
824     	volatile scc_t	*sccp;
825     
826     	if (!(info->flags & ASYNC_INITIALIZED))
827     		return;
828     
829     	state = info->state;
830     
831     #ifdef SERIAL_DEBUG_OPEN
832     	printk("Shutting down serial port %d (irq %d)....", info->line,
833     	       state->irq);
834     #endif
835     	
836     	save_flags(flags); cli(); /* Disable interrupts */
837     
838     	idx = PORT_NUM(state->smc_scc_num);
839     	if (state->smc_scc_num & NUM_IS_SCC) {
840     		sccp = &cpmp->cp_scc[idx];
841     		sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
842     #ifdef CONFIG_SERIAL_CONSOLE
843     		/* We can't disable the transmitter if this is the
844     		 * system console.
845     		 */
846     		if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
847     #endif
848     			sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
849     	}
850     	else {
851     		smcp = &cpmp->cp_smc[idx];
852     
853     		/* Disable interrupts and I/O.
854     		*/
855     		smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
856     #ifdef CONFIG_SERIAL_CONSOLE
857     		/* We can't disable the transmitter if this is the
858     		 * system console.
859     		 */
860     		if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
861     #endif
862     			smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
863     	}
864     	
865     	if (info->tty)
866     		set_bit(TTY_IO_ERROR, &info->tty->flags);
867     
868     	info->flags &= ~ASYNC_INITIALIZED;
869     	restore_flags(flags);
870     }
871     
872     /*
873      * This routine is called to set the UART divisor registers to match
874      * the specified baud rate for a serial port.
875      */
876     static void change_speed(ser_info_t *info)
877     {
878     	int	baud_rate;
879     	unsigned cflag, cval, scval, prev_mode;
880     	int	i, bits, sbits, idx;
881     	unsigned long	flags;
882     	struct serial_state *state;
883     	volatile smc_t	*smcp;
884     	volatile scc_t	*sccp;
885     
886     	if (!info->tty || !info->tty->termios)
887     		return;
888     	cflag = info->tty->termios->c_cflag;
889     
890     	state = info->state;
891     
892     	/* Character length programmed into the mode register is the
893     	 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
894     	 * 1 or 2 stop bits, minus 1.
895     	 * The value 'bits' counts this for us.
896     	 */
897     	cval = 0;
898     	scval = 0;
899     
900     	/* byte size and parity */
901     	switch (cflag & CSIZE) {
902     	      case CS5: bits = 5; break;
903     	      case CS6: bits = 6; break;
904     	      case CS7: bits = 7; break;
905     	      case CS8: bits = 8; break;
906     	      /* Never happens, but GCC is too dumb to figure it out */
907     	      default:  bits = 8; break;
908     	}
909     	sbits = bits - 5;
910     
911     	if (cflag & CSTOPB) {
912     		cval |= SMCMR_SL;	/* Two stops */
913     		scval |= SCU_PMSR_SL;
914     		bits++;
915     	}
916     	if (cflag & PARENB) {
917     		cval |= SMCMR_PEN;
918     		scval |= SCU_PMSR_PEN;
919     		bits++;
920     	}
921     	if (!(cflag & PARODD)) {
922     		cval |= SMCMR_PM_EVEN;
923     		scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
924     	}
925     
926     	/* Determine divisor based on baud rate */
927     	i = cflag & CBAUD;
928     	if (i >= (sizeof(baud_table)/sizeof(int)))
929     		baud_rate = 9600;
930     	else
931     		baud_rate = baud_table[i];
932     
933     	info->timeout = (TX_BUF_SIZE*HZ*bits);
934     	info->timeout += HZ/50;		/* Add .02 seconds of slop */
935     
936     #ifdef modem_control
937     	/* CTS flow control flag and modem status interrupts */
938     	info->IER &= ~UART_IER_MSI;
939     	if (info->flags & ASYNC_HARDPPS_CD)
940     		info->IER |= UART_IER_MSI;
941     	if (cflag & CRTSCTS) {
942     		info->flags |= ASYNC_CTS_FLOW;
943     		info->IER |= UART_IER_MSI;
944     	} else
945     		info->flags &= ~ASYNC_CTS_FLOW;
946     	if (cflag & CLOCAL)
947     		info->flags &= ~ASYNC_CHECK_CD;
948     	else {
949     		info->flags |= ASYNC_CHECK_CD;
950     		info->IER |= UART_IER_MSI;
951     	}
952     	serial_out(info, UART_IER, info->IER);
953     #endif
954     
955     	/*
956     	 * Set up parity check flag
957     	 */
958     #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
959     
960     	info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
961     	if (I_INPCK(info->tty))
962     		info->read_status_mask |= BD_SC_FR | BD_SC_PR;
963     	if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
964     		info->read_status_mask |= BD_SC_BR;
965     	
966     	/*
967     	 * Characters to ignore
968     	 */
969     	info->ignore_status_mask = 0;
970     	if (I_IGNPAR(info->tty))
971     		info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
972     	if (I_IGNBRK(info->tty)) {
973     		info->ignore_status_mask |= BD_SC_BR;
974     		/*
975     		 * If we're ignore parity and break indicators, ignore 
976     		 * overruns too.  (For real raw support).
977     		 */
978     		if (I_IGNPAR(info->tty))
979     			info->ignore_status_mask |= BD_SC_OV;
980     	}
981     	/*
982     	 * !!! ignore all characters if CREAD is not set
983     	 */
984     	if ((cflag & CREAD) == 0)
985     		info->read_status_mask &= ~BD_SC_EMPTY;
986     	save_flags(flags); cli();
987     
988     	/* Start bit has not been added (so don't, because we would just
989     	 * subtract it later), and we need to add one for the number of
990     	 * stops bits (there is always at least one).
991     	 */
992     	bits++;
993     	idx = PORT_NUM(state->smc_scc_num);
994     	if (state->smc_scc_num & NUM_IS_SCC) {
995     		sccp = &cpmp->cp_scc[idx];
996     		sccp->scc_pmsr = (sbits << 12) | scval;
997     	}
998     	else {
999     		smcp = &cpmp->cp_smc[idx];
1000     
1001     		/* Set the mode register.  We want to keep a copy of the
1002     		 * enables, because we want to put them back if they were
1003     		 * present.
1004     		 */
1005     		prev_mode = smcp->smc_smcmr;
1006     		smcp->smc_smcmr = smcr_mk_clen(bits) | cval |  SMCMR_SM_UART;
1007     		smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
1008     	}
1009     
1010     	m8xx_cpm_setbrg((state - rs_table), baud_rate);
1011     
1012     	restore_flags(flags);
1013     }
1014     
1015     static void rs_8xx_put_char(struct tty_struct *tty, unsigned char ch)
1016     {
1017     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1018     	volatile cbd_t	*bdp;
1019     
1020     	if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1021     		return;
1022     
1023     	if (!tty)
1024     		return;
1025     
1026     	bdp = info->tx_cur;
1027     	while (bdp->cbd_sc & BD_SC_READY);
1028     
1029     	*((char *)__va(bdp->cbd_bufaddr)) = ch;
1030     	bdp->cbd_datlen = 1;
1031     	bdp->cbd_sc |= BD_SC_READY;
1032     
1033     	/* Get next BD.
1034     	*/
1035     	if (bdp->cbd_sc & BD_SC_WRAP)
1036     		bdp = info->tx_bd_base;
1037     	else
1038     		bdp++;
1039     
1040     	info->tx_cur = (cbd_t *)bdp;
1041     
1042     }
1043     
1044     static int rs_8xx_write(struct tty_struct * tty, int from_user,
1045     		    const unsigned char *buf, int count)
1046     {
1047     	int	c, ret = 0;
1048     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1049     	volatile cbd_t *bdp;
1050     
1051     #ifdef CONFIG_KGDB
1052             /* Try to let stub handle output. Returns true if it did. */ 
1053             if (kgdb_output_string(buf, count))
1054                 return ret;
1055     #endif
1056     
1057     	if (serial_paranoia_check(info, tty->device, "rs_write"))
1058     		return 0;
1059     
1060     	if (!tty) 
1061     		return 0;
1062     
1063     	bdp = info->tx_cur;
1064     
1065     	while (1) {
1066     		c = MIN(count, TX_BUF_SIZE);
1067     
1068     		if (c <= 0)
1069     			break;
1070     
1071     		if (bdp->cbd_sc & BD_SC_READY) {
1072     			info->flags |= TX_WAKEUP;
1073     			break;
1074     		}
1075     
1076     		if (from_user) {
1077     			if (copy_from_user(__va(bdp->cbd_bufaddr), buf, c)) {
1078     				if (!ret)
1079     					ret = -EFAULT;
1080     				break;
1081     			}
1082     		} else {
1083     			memcpy(__va(bdp->cbd_bufaddr), buf, c);
1084     		}
1085     
1086     		bdp->cbd_datlen = c;
1087     		bdp->cbd_sc |= BD_SC_READY;
1088     
1089     		buf += c;
1090     		count -= c;
1091     		ret += c;
1092     
1093     		/* Get next BD.
1094     		*/
1095     		if (bdp->cbd_sc & BD_SC_WRAP)
1096     			bdp = info->tx_bd_base;
1097     		else
1098     			bdp++;
1099     		info->tx_cur = (cbd_t *)bdp;
1100     	}
1101     	return ret;
1102     }
1103     
1104     static int rs_8xx_write_room(struct tty_struct *tty)
1105     {
1106     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1107     	int	ret;
1108     
1109     	if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1110     		return 0;
1111     
1112     	if ((info->tx_cur->cbd_sc & BD_SC_READY) == 0) {
1113     		info->flags &= ~TX_WAKEUP;
1114     		ret = TX_BUF_SIZE;
1115     	}
1116     	else {
1117     		info->flags |= TX_WAKEUP;
1118     		ret = 0;
1119     	}
1120     	return ret;
1121     }
1122     
1123     /* I could track this with transmit counters....maybe later.
1124     */
1125     static int rs_8xx_chars_in_buffer(struct tty_struct *tty)
1126     {
1127     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1128     				
1129     	if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1130     		return 0;
1131     	return 0;
1132     }
1133     
1134     static void rs_8xx_flush_buffer(struct tty_struct *tty)
1135     {
1136     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1137     				
1138     	if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1139     		return;
1140     
1141     	/* There is nothing to "flush", whatever we gave the CPM
1142     	 * is on its way out.
1143     	 */
1144     	wake_up_interruptible(&tty->write_wait);
1145     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1146     	    tty->ldisc.write_wakeup)
1147     		(tty->ldisc.write_wakeup)(tty);
1148     	info->flags &= ~TX_WAKEUP;
1149     }
1150     
1151     /*
1152      * This function is used to send a high-priority XON/XOFF character to
1153      * the device
1154      */
1155     static void rs_8xx_send_xchar(struct tty_struct *tty, char ch)
1156     {
1157     	volatile cbd_t	*bdp;
1158     
1159     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1160     
1161     	if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1162     		return;
1163     
1164     	bdp = info->tx_cur;
1165     	while (bdp->cbd_sc & BD_SC_READY);
1166     
1167     	*((char *)__va(bdp->cbd_bufaddr)) = ch;
1168     	bdp->cbd_datlen = 1;
1169     	bdp->cbd_sc |= BD_SC_READY;
1170     
1171     	/* Get next BD.
1172     	*/
1173     	if (bdp->cbd_sc & BD_SC_WRAP)
1174     		bdp = info->tx_bd_base;
1175     	else
1176     		bdp++;
1177     
1178     	info->tx_cur = (cbd_t *)bdp;
1179     }
1180     
1181     /*
1182      * ------------------------------------------------------------
1183      * rs_throttle()
1184      * 
1185      * This routine is called by the upper-layer tty layer to signal that
1186      * incoming characters should be throttled.
1187      * ------------------------------------------------------------
1188      */
1189     static void rs_8xx_throttle(struct tty_struct * tty)
1190     {
1191     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1192     #ifdef SERIAL_DEBUG_THROTTLE
1193     	char	buf[64];
1194     	
1195     	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1196     	       tty->ldisc.chars_in_buffer(tty));
1197     #endif
1198     
1199     	if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1200     		return;
1201     	
1202     	if (I_IXOFF(tty))
1203     		rs_8xx_send_xchar(tty, STOP_CHAR(tty));
1204     
1205     #ifdef modem_control
1206     	if (tty->termios->c_cflag & CRTSCTS)
1207     		info->MCR &= ~UART_MCR_RTS;
1208     
1209     	cli();
1210     	serial_out(info, UART_MCR, info->MCR);
1211     	sti();
1212     #endif
1213     }
1214     
1215     static void rs_8xx_unthrottle(struct tty_struct * tty)
1216     {
1217     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1218     #ifdef SERIAL_DEBUG_THROTTLE
1219     	char	buf[64];
1220     	
1221     	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1222     	       tty->ldisc.chars_in_buffer(tty));
1223     #endif
1224     
1225     	if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1226     		return;
1227     	
1228     	if (I_IXOFF(tty)) {
1229     		if (info->x_char)
1230     			info->x_char = 0;
1231     		else
1232     			rs_8xx_send_xchar(tty, START_CHAR(tty));
1233     	}
1234     #ifdef modem_control
1235     	if (tty->termios->c_cflag & CRTSCTS)
1236     		info->MCR |= UART_MCR_RTS;
1237     	cli();
1238     	serial_out(info, UART_MCR, info->MCR);
1239     	sti();
1240     #endif
1241     }
1242     
1243     /*
1244      * ------------------------------------------------------------
1245      * rs_ioctl() and friends
1246      * ------------------------------------------------------------
1247      */
1248     
1249     #ifdef maybe
1250     /*
1251      * get_lsr_info - get line status register info
1252      *
1253      * Purpose: Let user call ioctl() to get info when the UART physically
1254      * 	    is emptied.  On bus types like RS485, the transmitter must
1255      * 	    release the bus after transmitting. This must be done when
1256      * 	    the transmit shift register is empty, not be done when the
1257      * 	    transmit holding register is empty.  This functionality
1258      * 	    allows an RS485 driver to be written in user space. 
1259      */
1260     static int get_lsr_info(struct async_struct * info, unsigned int *value)
1261     {
1262     	unsigned char status;
1263     	unsigned int result;
1264     
1265     	cli();
1266     	status = serial_in(info, UART_LSR);
1267     	sti();
1268     	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1269     	return put_user(result,value);
1270     }
1271     #endif
1272     
1273     static int get_modem_info(ser_info_t *info, unsigned int *value)
1274     {
1275     	unsigned int result = 0;
1276     #ifdef modem_control
1277     	unsigned char control, status;
1278     
1279     	control = info->MCR;
1280     	cli();
1281     	status = serial_in(info, UART_MSR);
1282     	sti();
1283     	result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1284     		| ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1285     #ifdef TIOCM_OUT1
1286     		| ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1287     		| ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1288     #endif
1289     		| ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1290     		| ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1291     		| ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1292     		| ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1293     #endif
1294     	return put_user(result,value);
1295     }
1296     
1297     static int set_modem_info(ser_info_t *info, unsigned int cmd,
1298     			  unsigned int *value)
1299     {
1300     	int error;
1301     	unsigned int arg;
1302     
1303     	error = get_user(arg, value);
1304     	if (error)
1305     		return error;
1306     #ifdef modem_control
1307     	switch (cmd) {
1308     	case TIOCMBIS: 
1309     		if (arg & TIOCM_RTS)
1310     			info->MCR |= UART_MCR_RTS;
1311     		if (arg & TIOCM_DTR)
1312     			info->MCR |= UART_MCR_DTR;
1313     #ifdef TIOCM_OUT1
1314     		if (arg & TIOCM_OUT1)
1315     			info->MCR |= UART_MCR_OUT1;
1316     		if (arg & TIOCM_OUT2)
1317     			info->MCR |= UART_MCR_OUT2;
1318     #endif
1319     		break;
1320     	case TIOCMBIC:
1321     		if (arg & TIOCM_RTS)
1322     			info->MCR &= ~UART_MCR_RTS;
1323     		if (arg & TIOCM_DTR)
1324     			info->MCR &= ~UART_MCR_DTR;
1325     #ifdef TIOCM_OUT1
1326     		if (arg & TIOCM_OUT1)
1327     			info->MCR &= ~UART_MCR_OUT1;
1328     		if (arg & TIOCM_OUT2)
1329     			info->MCR &= ~UART_MCR_OUT2;
1330     #endif
1331     		break;
1332     	case TIOCMSET:
1333     		info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1334     #ifdef TIOCM_OUT1
1335     					    UART_MCR_OUT1 |
1336     					    UART_MCR_OUT2 |
1337     #endif
1338     					    UART_MCR_DTR))
1339     			     | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1340     #ifdef TIOCM_OUT1
1341     			     | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1342     			     | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1343     #endif
1344     			     | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1345     		break;
1346     	default:
1347     		return -EINVAL;
1348     	}
1349     	cli();
1350     	serial_out(info, UART_MCR, info->MCR);
1351     	sti();
1352     #endif
1353     	return 0;
1354     }
1355     
1356     /* Sending a break is a two step process on the SMC/SCC.  It is accomplished
1357      * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1358      * command.  We take advantage of the begin/end functions to make this
1359      * happen.
1360      */
1361     static ushort	smc_chan_map[] = {
1362     	CPM_CR_CH_SMC1,
1363     	CPM_CR_CH_SMC2
1364     };
1365     
1366     static ushort	scc_chan_map[] = {
1367     	CPM_CR_CH_SCC1,
1368     	CPM_CR_CH_SCC2,
1369     	CPM_CR_CH_SCC3,
1370     	CPM_CR_CH_SCC4
1371     };
1372     
1373     static void begin_break(ser_info_t *info)
1374     {
1375     	volatile cpm8xx_t *cp;
1376     	ushort	chan;
1377     	int	idx;
1378     
1379     	cp = cpmp;
1380     
1381     	idx = PORT_NUM(info->state->smc_scc_num);
1382     	if (info->state->smc_scc_num & NUM_IS_SCC)
1383     		chan = scc_chan_map[idx];
1384     	else
1385     		chan = smc_chan_map[idx];
1386     	cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1387     	while (cp->cp_cpcr & CPM_CR_FLG);
1388     }
1389     
1390     static void end_break(ser_info_t *info)
1391     {
1392     	volatile cpm8xx_t *cp;
1393     	ushort	chan;
1394     	int	idx;
1395     
1396     	cp = cpmp;
1397     
1398     	idx = PORT_NUM(info->state->smc_scc_num);
1399     	if (info->state->smc_scc_num & NUM_IS_SCC)
1400     		chan = scc_chan_map[idx];
1401     	else
1402     		chan = smc_chan_map[idx];
1403     	cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1404     	while (cp->cp_cpcr & CPM_CR_FLG);
1405     }
1406     
1407     /*
1408      * This routine sends a break character out the serial port.
1409      */
1410     static void send_break(ser_info_t *info, int duration)
1411     {
1412     	current->state = TASK_INTERRUPTIBLE;
1413     #ifdef SERIAL_DEBUG_SEND_BREAK
1414     	printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1415     #endif
1416     	begin_break(info);
1417     	schedule_timeout(duration);
1418     	end_break(info);
1419     #ifdef SERIAL_DEBUG_SEND_BREAK
1420     	printk("done jiffies=%lu\n", jiffies);
1421     #endif
1422     }
1423     
1424     
1425     static int rs_8xx_ioctl(struct tty_struct *tty, struct file * file,
1426     		    unsigned int cmd, unsigned long arg)
1427     {
1428     	int error;
1429     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1430     	int retval;
1431     	struct async_icount cnow;	/* kernel counter temps */
1432     	struct serial_icounter_struct *p_cuser;	/* user space */
1433     
1434     	if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1435     		return -ENODEV;
1436     
1437     	if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1438     		if (tty->flags & (1 << TTY_IO_ERROR))
1439     		    return -EIO;
1440     	}
1441     	
1442     	switch (cmd) {
1443     		case TCSBRK:	/* SVID version: non-zero arg --> no break */
1444     			retval = tty_check_change(tty);
1445     			if (retval)
1446     				return retval;
1447     			tty_wait_until_sent(tty, 0);
1448     			if (signal_pending(current))
1449     				return -EINTR;
1450     			if (!arg) {
1451     				send_break(info, HZ/4);	/* 1/4 second */
1452     				if (signal_pending(current))
1453     					return -EINTR;
1454     			}
1455     			return 0;
1456     		case TCSBRKP:	/* support for POSIX tcsendbreak() */
1457     			retval = tty_check_change(tty);
1458     			if (retval)
1459     				return retval;
1460     			tty_wait_until_sent(tty, 0);
1461     			if (signal_pending(current))
1462     				return -EINTR;
1463     			send_break(info, arg ? arg*(HZ/10) : HZ/4);
1464     			if (signal_pending(current))
1465     				return -EINTR;
1466     			return 0;
1467     		case TIOCSBRK:
1468     			retval = tty_check_change(tty);
1469     			if (retval)
1470     				return retval;
1471     			tty_wait_until_sent(tty, 0);
1472     			begin_break(info);
1473     			return 0;
1474     		case TIOCCBRK:
1475     			retval = tty_check_change(tty);
1476     			if (retval)
1477     				return retval;
1478     			end_break(info);
1479     			return 0;
1480     		case TIOCGSOFTCAR:
1481     			return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1482     		case TIOCSSOFTCAR:
1483     			error = get_user(arg, (unsigned int *) arg);
1484     			if (error)
1485     				return error;
1486     			tty->termios->c_cflag =
1487     				((tty->termios->c_cflag & ~CLOCAL) |
1488     				 (arg ? CLOCAL : 0));
1489     			return 0;
1490     		case TIOCMGET:
1491     			return get_modem_info(info, (unsigned int *) arg);
1492     		case TIOCMBIS:
1493     		case TIOCMBIC:
1494     		case TIOCMSET:
1495     			return set_modem_info(info, cmd, (unsigned int *) arg);
1496     #ifdef maybe
1497     		case TIOCSERGETLSR: /* Get line status register */
1498     			return get_lsr_info(info, (unsigned int *) arg);
1499     #endif
1500     		/*
1501     		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1502     		 * - mask passed in arg for lines of interest
1503      		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1504     		 * Caller should use TIOCGICOUNT to see which one it was
1505     		 */
1506     		 case TIOCMIWAIT:
1507     #ifdef modem_control
1508     			cli();
1509     			/* note the counters on entry */
1510     			cprev = info->state->icount;
1511     			sti();
1512     			while (1) {
1513     				interruptible_sleep_on(&info->delta_msr_wait);
1514     				/* see if a signal did it */
1515     				if (signal_pending(current))
1516     					return -ERESTARTSYS;
1517     				cli();
1518     				cnow = info->state->icount; /* atomic copy */
1519     				sti();
1520     				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1521     				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1522     					return -EIO; /* no change => error */
1523     				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1524     				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1525     				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1526     				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1527     					return 0;
1528     				}
1529     				cprev = cnow;
1530     			}
1531     			/* NOTREACHED */
1532     #else
1533     			return 0;
1534     #endif
1535     
1536     		/* 
1537     		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1538     		 * Return: write counters to the user passed counter struct
1539     		 * NB: both 1->0 and 0->1 transitions are counted except for
1540     		 *     RI where only 0->1 is counted.
1541     		 */
1542     		case TIOCGICOUNT:
1543     			cli();
1544     			cnow = info->state->icount;
1545     			sti();
1546     			p_cuser = (struct serial_icounter_struct *) arg;
1547     			error = put_user(cnow.cts, &p_cuser->cts);
1548     			if (error) return error;
1549     			error = put_user(cnow.dsr, &p_cuser->dsr);
1550     			if (error) return error;
1551     			error = put_user(cnow.rng, &p_cuser->rng);
1552     			if (error) return error;
1553     			error = put_user(cnow.dcd, &p_cuser->dcd);
1554     			if (error) return error;
1555     			return 0;
1556     
1557     		default:
1558     			return -ENOIOCTLCMD;
1559     		}
1560     	return 0;
1561     }
1562     
1563     /* FIX UP modem control here someday......
1564     */
1565     static void rs_8xx_set_termios(struct tty_struct *tty, struct termios *old_termios)
1566     {
1567     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1568     
1569     	if (   (tty->termios->c_cflag == old_termios->c_cflag)
1570     	    && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1571     		== RELEVANT_IFLAG(old_termios->c_iflag)))
1572     	  return;
1573     
1574     	change_speed(info);
1575     
1576     #ifdef modem_control
1577     	/* Handle transition to B0 status */
1578     	if ((old_termios->c_cflag & CBAUD) &&
1579     	    !(tty->termios->c_cflag & CBAUD)) {
1580     		info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1581     		cli();
1582     		serial_out(info, UART_MCR, info->MCR);
1583     		sti();
1584     	}
1585     	
1586     	/* Handle transition away from B0 status */
1587     	if (!(old_termios->c_cflag & CBAUD) &&
1588     	    (tty->termios->c_cflag & CBAUD)) {
1589     		info->MCR |= UART_MCR_DTR;
1590     		if (!tty->hw_stopped ||
1591     		    !(tty->termios->c_cflag & CRTSCTS)) {
1592     			info->MCR |= UART_MCR_RTS;
1593     		}
1594     		cli();
1595     		serial_out(info, UART_MCR, info->MCR);
1596     		sti();
1597     	}
1598     	
1599     	/* Handle turning off CRTSCTS */
1600     	if ((old_termios->c_cflag & CRTSCTS) &&
1601     	    !(tty->termios->c_cflag & CRTSCTS)) {
1602     		tty->hw_stopped = 0;
1603     		rs_8xx_start(tty);
1604     	}
1605     #endif
1606     
1607     #if 0
1608     	/*
1609     	 * No need to wake up processes in open wait, since they
1610     	 * sample the CLOCAL flag once, and don't recheck it.
1611     	 * XXX  It's not clear whether the current behavior is correct
1612     	 * or not.  Hence, this may change.....
1613     	 */
1614     	if (!(old_termios->c_cflag & CLOCAL) &&
1615     	    (tty->termios->c_cflag & CLOCAL))
1616     		wake_up_interruptible(&info->open_wait);
1617     #endif
1618     }
1619     
1620     /*
1621      * ------------------------------------------------------------
1622      * rs_close()
1623      * 
1624      * This routine is called when the serial port gets closed.  First, we
1625      * wait for the last remaining data to be sent.  Then, we unlink its
1626      * async structure from the interrupt chain if necessary, and we free
1627      * that IRQ if nothing is left in the chain.
1628      * ------------------------------------------------------------
1629      */
1630     static void rs_8xx_close(struct tty_struct *tty, struct file * filp)
1631     {
1632     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1633     	struct serial_state *state;
1634     	unsigned long	flags;
1635     	int		idx;
1636     	volatile smc_t	*smcp;
1637     	volatile scc_t	*sccp;
1638     
1639     	if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1640     		return;
1641     
1642     	state = info->state;
1643     	
1644     	save_flags(flags); cli();
1645     	
1646     	if (tty_hung_up_p(filp)) {
1647     		DBG_CNT("before DEC-hung");
1648     		MOD_DEC_USE_COUNT;
1649     		restore_flags(flags);
1650     		return;
1651     	}
1652     	
1653     #ifdef SERIAL_DEBUG_OPEN
1654     	printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1655     #endif
1656     	if ((tty->count == 1) && (state->count != 1)) {
1657     		/*
1658     		 * Uh, oh.  tty->count is 1, which means that the tty
1659     		 * structure will be freed.  state->count should always
1660     		 * be one in these conditions.  If it's greater than
1661     		 * one, we've got real problems, since it means the
1662     		 * serial port won't be shutdown.
1663     		 */
1664     		printk("rs_close: bad serial port count; tty->count is 1, "
1665     		       "state->count is %d\n", state->count);
1666     		state->count = 1;
1667     	}
1668     	if (--state->count < 0) {
1669     		printk("rs_close: bad serial port count for ttys%d: %d\n",
1670     		       info->line, state->count);
1671     		state->count = 0;
1672     	}
1673     	if (state->count) {
1674     		DBG_CNT("before DEC-2");
1675     		MOD_DEC_USE_COUNT;
1676     		restore_flags(flags);
1677     		return;
1678     	}
1679     	info->flags |= ASYNC_CLOSING;
1680     	/*
1681     	 * Save the termios structure, since this port may have
1682     	 * separate termios for callout and dialin.
1683     	 */
1684     	if (info->flags & ASYNC_NORMAL_ACTIVE)
1685     		info->state->normal_termios = *tty->termios;
1686     	if (info->flags & ASYNC_CALLOUT_ACTIVE)
1687     		info->state->callout_termios = *tty->termios;
1688     	/*
1689     	 * Now we wait for the transmit buffer to clear; and we notify 
1690     	 * the line discipline to only process XON/XOFF characters.
1691     	 */
1692     	tty->closing = 1;
1693     	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1694     		tty_wait_until_sent(tty, info->closing_wait);
1695     	/*
1696     	 * At this point we stop accepting input.  To do this, we
1697     	 * disable the receive line status interrupts, and tell the
1698     	 * interrupt driver to stop checking the data ready bit in the
1699     	 * line status register.
1700     	 */
1701     	info->read_status_mask &= ~BD_SC_EMPTY;
1702     	if (info->flags & ASYNC_INITIALIZED) {
1703     		idx = PORT_NUM(info->state->smc_scc_num);
1704     		if (info->state->smc_scc_num & NUM_IS_SCC) {
1705     			sccp = &cpmp->cp_scc[idx];
1706     			sccp->scc_sccm &= ~UART_SCCM_RX;
1707     			sccp->scc_gsmrl &= ~SCC_GSMRL_ENR;
1708     		}
1709     		else {
1710     			smcp = &cpmp->cp_smc[idx];
1711     			smcp->smc_smcm &= ~SMCM_RX;
1712     			smcp->smc_smcmr &= ~SMCMR_REN;
1713     		}
1714     		/*
1715     		 * Before we drop DTR, make sure the UART transmitter
1716     		 * has completely drained; this is especially
1717     		 * important if there is a transmit FIFO!
1718     		 */
1719     		rs_8xx_wait_until_sent(tty, info->timeout);
1720     	}
1721     	shutdown(info);
1722     	if (tty->driver.flush_buffer)
1723     		tty->driver.flush_buffer(tty);
1724     	if (tty->ldisc.flush_buffer)
1725     		tty->ldisc.flush_buffer(tty);
1726     	tty->closing = 0;
1727     	info->event = 0;
1728     	info->tty = 0;
1729     	if (info->blocked_open) {
1730     		if (info->close_delay) {
1731     			current->state = TASK_INTERRUPTIBLE;
1732     			schedule_timeout(info->close_delay);
1733     		}
1734     		wake_up_interruptible(&info->open_wait);
1735     	}
1736     	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1737     			 ASYNC_CLOSING);
1738     	wake_up_interruptible(&info->close_wait);
1739     	MOD_DEC_USE_COUNT;
1740     	restore_flags(flags);
1741     }
1742     
1743     /*
1744      * rs_wait_until_sent() --- wait until the transmitter is empty
1745      */
1746     static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout)
1747     {
1748     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1749     	unsigned long orig_jiffies, char_time;
1750     	/*int lsr;*/
1751     	volatile cbd_t *bdp;
1752     	
1753     	if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1754     		return;
1755     
1756     #ifdef maybe
1757     	if (info->state->type == PORT_UNKNOWN)
1758     		return;
1759     #endif
1760     
1761     	orig_jiffies = jiffies;
1762     	/*
1763     	 * Set the check interval to be 1/5 of the estimated time to
1764     	 * send a single character, and make it at least 1.  The check
1765     	 * interval should also be less than the timeout.
1766     	 * 
1767     	 * Note: we have to use pretty tight timings here to satisfy
1768     	 * the NIST-PCTS.
1769     	 */
1770     	char_time = 1;
1771     	if (timeout)
1772     		char_time = MIN(char_time, timeout);
1773     #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1774     	printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1775     	printk("jiff=%lu...", jiffies);
1776     #endif
1777     
1778     	/* We go through the loop at least once because we can't tell
1779     	 * exactly when the last character exits the shifter.  There can
1780     	 * be at least two characters waiting to be sent after the buffers
1781     	 * are empty.
1782     	 */
1783     	do {
1784     #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1785     		printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1786     #endif
1787     		current->state = TASK_INTERRUPTIBLE;
1788     /*		current->counter = 0;	 make us low-priority */
1789     		schedule_timeout(char_time);
1790     		if (signal_pending(current))
1791     			break;
1792     		if (timeout && ((orig_jiffies + timeout) < jiffies))
1793     			break;
1794     
1795     		/* The 'tx_cur' is really the next buffer to send.  We
1796     		 * have to back up to the previous BD and wait for it
1797     		 * to go.  This isn't perfect, because all this indicates
1798     		 * is the buffer is available.  There are still characters
1799     		 * in the CPM FIFO.
1800     		 */
1801     		bdp = info->tx_cur;
1802     		if (bdp == info->tx_bd_base)
1803     			bdp += (TX_NUM_FIFO-1);
1804     		else
1805     			bdp--;
1806     	} while (bdp->cbd_sc & BD_SC_READY);
1807     	current->state = TASK_RUNNING;
1808     #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1809     	printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1810     #endif
1811     }
1812     
1813     /*
1814      * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1815      */
1816     static void rs_8xx_hangup(struct tty_struct *tty)
1817     {
1818     	ser_info_t *info = (ser_info_t *)tty->driver_data;
1819     	struct serial_state *state = info->state;
1820     	
1821     	if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1822     		return;
1823     
1824     	state = info->state;
1825     	
1826     	rs_8xx_flush_buffer(tty);
1827     	shutdown(info);
1828     	info->event = 0;
1829     	state->count = 0;
1830     	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1831     	info->tty = 0;
1832     	wake_up_interruptible(&info->open_wait);
1833     }
1834     
1835     /*
1836      * ------------------------------------------------------------
1837      * rs_open() and friends
1838      * ------------------------------------------------------------
1839      */
1840     static int block_til_ready(struct tty_struct *tty, struct file * filp,
1841     			   ser_info_t *info)
1842     {
1843     #ifdef DO_THIS_LATER
1844     	DECLARE_WAITQUEUE(wait, current);
1845     #endif
1846     	struct serial_state *state = info->state;
1847     	int		retval;
1848     	int		do_clocal = 0;
1849     
1850     	/*
1851     	 * If the device is in the middle of being closed, then block
1852     	 * until it's done, and then try again.
1853     	 */
1854     	if (tty_hung_up_p(filp) ||
1855     	    (info->flags & ASYNC_CLOSING)) {
1856     		if (info->flags & ASYNC_CLOSING)
1857     			interruptible_sleep_on(&info->close_wait);
1858     #ifdef SERIAL_DO_RESTART
1859     		if (info->flags & ASYNC_HUP_NOTIFY)
1860     			return -EAGAIN;
1861     		else
1862     			return -ERESTARTSYS;
1863     #else
1864     		return -EAGAIN;
1865     #endif
1866     	}
1867     
1868     	/*
1869     	 * If this is a callout device, then just make sure the normal
1870     	 * device isn't being used.
1871     	 */
1872     	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1873     		if (info->flags & ASYNC_NORMAL_ACTIVE)
1874     			return -EBUSY;
1875     		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1876     		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1877     		    (info->session != current->session))
1878     		    return -EBUSY;
1879     		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1880     		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1881     		    (info->pgrp != current->pgrp))
1882     		    return -EBUSY;
1883     		info->flags |= ASYNC_CALLOUT_ACTIVE;
1884     		return 0;
1885     	}
1886     	
1887     	/*
1888     	 * If non-blocking mode is set, or the port is not enabled,
1889     	 * then make the check up front and then exit.
1890     	 * If this is an SMC port, we don't have modem control to wait
1891     	 * for, so just get out here.
1892     	 */
1893     	if ((filp->f_flags & O_NONBLOCK) ||
1894     	    (tty->flags & (1 << TTY_IO_ERROR)) ||
1895     	    !(info->state->smc_scc_num & NUM_IS_SCC)) {
1896     		if (info->flags & ASYNC_CALLOUT_ACTIVE)
1897     			return -EBUSY;
1898     		info->flags |= ASYNC_NORMAL_ACTIVE;
1899     		return 0;
1900     	}
1901     
1902     	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1903     		if (state->normal_termios.c_cflag & CLOCAL)
1904     			do_clocal = 1;
1905     	} else {
1906     		if (tty->termios->c_cflag & CLOCAL)
1907     			do_clocal = 1;
1908     	}
1909     	
1910     	/*
1911     	 * Block waiting for the carrier detect and the line to become
1912     	 * free (i.e., not in use by the callout).  While we are in
1913     	 * this loop, state->count is dropped by one, so that
1914     	 * rs_close() knows when to free things.  We restore it upon
1915     	 * exit, either normal or abnormal.
1916     	 */
1917     	retval = 0;
1918     #ifdef DO_THIS_LATER
1919     	add_wait_queue(&info->open_wait, &wait);
1920     #ifdef SERIAL_DEBUG_OPEN
1921     	printk("block_til_ready before block: ttys%d, count = %d\n",
1922     	       state->line, state->count);
1923     #endif
1924     	cli();
1925     	if (!tty_hung_up_p(filp)) 
1926     		state->count--;
1927     	sti();
1928     	info->blocked_open++;
1929     	while (1) {
1930     		cli();
1931     		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1932     		    (tty->termios->c_cflag & CBAUD))
1933     			serial_out(info, UART_MCR,
1934     				   serial_inp(info, UART_MCR) |
1935     				   (UART_MCR_DTR | UART_MCR_RTS));
1936     		sti();
1937     		set_current_state(TASK_INTERRUPTIBLE);
1938     		if (tty_hung_up_p(filp) ||
1939     		    !(info->flags & ASYNC_INITIALIZED)) {
1940     #ifdef SERIAL_DO_RESTART
1941     			if (info->flags & ASYNC_HUP_NOTIFY)
1942     				retval = -EAGAIN;
1943     			else
1944     				retval = -ERESTARTSYS;	
1945     #else
1946     			retval = -EAGAIN;
1947     #endif
1948     			break;
1949     		}
1950     		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1951     		    !(info->flags & ASYNC_CLOSING) &&
1952     		    (do_clocal || (serial_in(info, UART_MSR) &
1953     				   UART_MSR_DCD)))
1954     			break;
1955     		if (signal_pending(current)) {
1956     			retval = -ERESTARTSYS;
1957     			break;
1958     		}
1959     #ifdef SERIAL_DEBUG_OPEN
1960     		printk("block_til_ready blocking: ttys%d, count = %d\n",
1961     		       info->line, state->count);
1962     #endif
1963     		schedule();
1964     	}
1965     	current->state = TASK_RUNNING;
1966     	remove_wait_queue(&info->open_wait, &wait);
1967     	if (!tty_hung_up_p(filp))
1968     		state->count++;
1969     	info->blocked_open--;
1970     #ifdef SERIAL_DEBUG_OPEN
1971     	printk("block_til_ready after blocking: ttys%d, count = %d\n",
1972     	       info->line, state->count);
1973     #endif
1974     #endif /* DO_THIS_LATER */
1975     	if (retval)
1976     		return retval;
1977     	info->flags |= ASYNC_NORMAL_ACTIVE;
1978     	return 0;
1979     }
1980     
1981     static int get_async_struct(int line, ser_info_t **ret_info)
1982     {
1983     	struct serial_state *sstate;
1984     
1985     	sstate = rs_table + line;
1986     	if (sstate->info) {
1987     		sstate->count++;
1988     		*ret_info = (ser_info_t *)sstate->info;
1989     		return 0;
1990     	}
1991     	else {
1992     		return -ENOMEM;
1993     	}
1994     }
1995     
1996     /*
1997      * This routine is called whenever a serial port is opened.  It
1998      * enables interrupts for a serial port, linking in its async structure into
1999      * the IRQ chain.   It also performs the serial-specific
2000      * initialization for the tty structure.
2001      */
2002     static int rs_8xx_open(struct tty_struct *tty, struct file * filp)
2003     {
2004     	ser_info_t	*info;
2005     	int 		retval, line;
2006     
2007     	line = MINOR(tty->device) - tty->driver.minor_start;
2008     	if ((line < 0) || (line >= NR_PORTS))
2009     		return -ENODEV;
2010     	retval = get_async_struct(line, &info);
2011     	if (retval)
2012     		return retval;
2013     	if (serial_paranoia_check(info, tty->device, "rs_open"))
2014     		return -ENODEV;
2015     
2016     #ifdef SERIAL_DEBUG_OPEN
2017     	printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2018     	       info->state->count);
2019     #endif
2020     	tty->driver_data = info;
2021     	info->tty = tty;
2022     
2023     	/*
2024     	 * Start up serial port
2025     	 */
2026     	retval = startup(info);
2027     	if (retval)
2028     		return retval;
2029     
2030     	MOD_INC_USE_COUNT;
2031     	retval = block_til_ready(tty, filp, info);
2032     	if (retval) {
2033     #ifdef SERIAL_DEBUG_OPEN
2034     		printk("rs_open returning after block_til_ready with %d\n",
2035     		       retval);
2036     #endif
2037     		MOD_DEC_USE_COUNT;
2038     		return retval;
2039     	}
2040     
2041     	if ((info->state->count == 1) &&
2042     	    (info->flags & ASYNC_SPLIT_TERMIOS)) {
2043     		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2044     			*tty->termios = info->state->normal_termios;
2045     		else 
2046     			*tty->termios = info->state->callout_termios;
2047     		change_speed(info);
2048     	}
2049     
2050     	info->session = current->session;
2051     	info->pgrp = current->pgrp;
2052     
2053     #ifdef SERIAL_DEBUG_OPEN
2054     	printk("rs_open ttys%d successful...", info->line);
2055     #endif
2056     	return 0;
2057     }
2058     
2059     /*
2060      * /proc fs routines....
2061      */
2062     
2063     static int inline line_info(char *buf, struct serial_state *state)
2064     {
2065     #ifdef notdef
2066     	struct async_struct *info = state->info, scr_info;
2067     	char	stat_buf[30], control, status;
2068     #endif
2069     	int	ret;
2070     
2071     	ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2072     		      state->line,
2073     		      (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC",
2074     		      (unsigned int)(state->port), state->irq);
2075     
2076     	if (!state->port || (state->type == PORT_UNKNOWN)) {
2077     		ret += sprintf(buf+ret, "\n");
2078     		return ret;
2079     	}
2080     
2081     #ifdef notdef
2082     	/*
2083     	 * Figure out the current RS-232 lines
2084     	 */
2085     	if (!info) {
2086     		info = &scr_info;	/* This is just for serial_{in,out} */
2087     
2088     		info->magic = SERIAL_MAGIC;
2089     		info->port = state->port;
2090     		info->flags = state->flags;
2091     		info->quot = 0;
2092     		info->tty = 0;
2093     	}
2094     	cli();
2095     	status = serial_in(info, UART_MSR);
2096     	control = info ? info->MCR : serial_in(info, UART_MCR);
2097     	sti();
2098     	
2099     	stat_buf[0] = 0;
2100     	stat_buf[1] = 0;
2101     	if (control & UART_MCR_RTS)
2102     		strcat(stat_buf, "|RTS");
2103     	if (status & UART_MSR_CTS)
2104     		strcat(stat_buf, "|CTS");
2105     	if (control & UART_MCR_DTR)
2106     		strcat(stat_buf, "|DTR");
2107     	if (status & UART_MSR_DSR)
2108     		strcat(stat_buf, "|DSR");
2109     	if (status & UART_MSR_DCD)
2110     		strcat(stat_buf, "|CD");
2111     	if (status & UART_MSR_RI)
2112     		strcat(stat_buf, "|RI");
2113     
2114     	if (info->quot) {
2115     		ret += sprintf(buf+ret, " baud:%d",
2116     			       state->baud_base / info->quot);
2117     	}
2118     
2119     	ret += sprintf(buf+ret, " tx:%d rx:%d",
2120     		      state->icount.tx, state->icount.rx);
2121     
2122     	if (state->icount.frame)
2123     		ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2124     	
2125     	if (state->icount.parity)
2126     		ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2127     	
2128     	if (state->icount.brk)
2129     		ret += sprintf(buf+ret, " brk:%d", state->icount.brk);	
2130     
2131     	if (state->icount.overrun)
2132     		ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2133     
2134     	/*
2135     	 * Last thing is the RS-232 status lines
2136     	 */
2137     	ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2138     #endif
2139     	return ret;
2140     }
2141     
2142     int rs_8xx_read_proc(char *page, char **start, off_t off, int count,
2143     		 int *eof, void *data)
2144     {
2145     	int i, len = 0;
2146     	off_t	begin = 0;
2147     
2148     	len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2149     	for (i = 0; i < NR_PORTS && len < 4000; i++) {
2150     		len += line_info(page + len, &rs_table[i]);
2151     		if (len+begin > off+count)
2152     			goto done;
2153     		if (len+begin < off) {
2154     			begin += len;
2155     			len = 0;
2156     		}
2157     	}
2158     	*eof = 1;
2159     done:
2160     	if (off >= len+begin)
2161     		return 0;
2162     	*start = page + (begin-off);
2163     	return ((count < begin+len-off) ? count : begin+len-off);
2164     }
2165     
2166     /*
2167      * ---------------------------------------------------------------------
2168      * rs_init() and friends
2169      *
2170      * rs_init() is called at boot-time to initialize the serial driver.
2171      * ---------------------------------------------------------------------
2172      */
2173     
2174     /*
2175      * This routine prints out the appropriate serial driver version
2176      * number, and identifies which options were configured into this
2177      * driver.
2178      */
2179     static _INLINE_ void show_serial_version(void)
2180     {
2181      	printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2182     }
2183     
2184     
2185     /*
2186      * The serial console driver used during boot.  Note that these names
2187      * clash with those found in "serial.c", so we currently can't support
2188      * the 16xxx uarts and these at the same time.  I will fix this to become
2189      * an indirect function call from tty_io.c (or something).
2190      */
2191     
2192     #ifdef CONFIG_SERIAL_CONSOLE
2193     
2194     /*
2195      * Print a string to the serial port trying not to disturb any possible
2196      * real use of the port...
2197      */
2198     static void my_console_write(int idx, const char *s,
2199     				unsigned count)
2200     {
2201     	struct		serial_state	*ser;
2202     	ser_info_t			*info;
2203     	unsigned			i;
2204     	volatile	cbd_t		*bdp, *bdbase;
2205     	volatile	smc_uart_t	*up;
2206     	volatile	u_char		*cp;
2207     
2208     	ser = rs_table + idx;
2209     
2210     	/* If the port has been initialized for general use, we have
2211     	 * to use the buffer descriptors allocated there.  Otherwise,
2212     	 * we simply use the single buffer allocated.
2213     	 */
2214     	if ((info = (ser_info_t *)ser->info) != NULL) {
2215     		bdp = info->tx_cur;
2216     		bdbase = info->tx_bd_base;
2217     	}
2218     	else {
2219     		/* Pointer to UART in parameter ram.
2220     		*/
2221     		up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2222     
2223     		/* Get the address of the host memory buffer.
2224     		 */
2225     		bdp = bdbase = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2226     	}
2227     
2228     	/*
2229     	 * We need to gracefully shut down the transmitter, disable
2230     	 * interrupts, then send our bytes out.
2231     	 */
2232     
2233     	/*
2234     	 * Now, do each character.  This is not as bad as it looks
2235     	 * since this is a holding FIFO and not a transmitting FIFO.
2236     	 * We could add the complexity of filling the entire transmit
2237     	 * buffer, but we would just wait longer between accesses......
2238     	 */
2239     	for (i = 0; i < count; i++, s++) {
2240     		/* Wait for transmitter fifo to empty.
2241     		 * Ready indicates output is ready, and xmt is doing
2242     		 * that, not that it is ready for us to send.
2243     		 */
2244     		while (bdp->cbd_sc & BD_SC_READY);
2245     
2246     		/* Send the character out.
2247     		 * If the buffer address is in the CPM DPRAM, don't
2248     		 * convert it.
2249     		 */
2250     		if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2251     			cp = (u_char *)(bdp->cbd_bufaddr);
2252     		else
2253     			cp = __va(bdp->cbd_bufaddr);
2254     		*cp = *s;
2255     		
2256     		bdp->cbd_datlen = 1;
2257     		bdp->cbd_sc |= BD_SC_READY;
2258     
2259     		if (bdp->cbd_sc & BD_SC_WRAP)
2260     			bdp = bdbase;
2261     		else
2262     			bdp++;
2263     
2264     		/* if a LF, also do CR... */
2265     		if (*s == 10) {
2266     			while (bdp->cbd_sc & BD_SC_READY);
2267     			cp = __va(bdp->cbd_bufaddr);
2268     			*cp = 13;
2269     			bdp->cbd_datlen = 1;
2270     			bdp->cbd_sc |= BD_SC_READY;
2271     
2272     			if (bdp->cbd_sc & BD_SC_WRAP) {
2273     				bdp = bdbase;
2274     			}
2275     			else {
2276     				bdp++;
2277     			}
2278     		}
2279     	}
2280     
2281     	/*
2282     	 * Finally, Wait for transmitter & holding register to empty
2283     	 *  and restore the IER
2284     	 */
2285     	while (bdp->cbd_sc & BD_SC_READY);
2286     
2287     	if (info)
2288     		info->tx_cur = (cbd_t *)bdp;
2289     }
2290     
2291     static void serial_console_write(struct console *c, const char *s,
2292     				unsigned count)
2293     {
2294     #ifdef CONFIG_KGDB
2295     	/* Try to let stub handle output. Returns true if it did. */ 
2296     	if (kgdb_output_string(s, count))
2297     		return;
2298     #endif
2299     	my_console_write(c->index, s, count);
2300     }
2301     
2302     #ifdef CONFIG_XMON
2303     int
2304     xmon_8xx_write(const char *s, unsigned count)
2305     {
2306     	my_console_write(0, s, count);
2307     	return(count);
2308     }
2309     #endif
2310     
2311     #ifdef CONFIG_KGDB
2312     void
2313     putDebugChar(char ch)
2314     {
2315     	my_console_write(0, &ch, 1);
2316     }
2317     #endif
2318     
2319     /*
2320      * Receive character from the serial port.  This only works well
2321      * before the port is initialized for real use.
2322      */
2323     static int my_console_wait_key(int idx, int xmon, char *obuf)
2324     {
2325     	struct serial_state		*ser;
2326     	u_char				c, *cp;
2327     	ser_info_t			*info;
2328     	volatile	cbd_t		*bdp;
2329     	volatile	smc_uart_t	*up;
2330     	int				i;
2331     
2332     	ser = rs_table + idx;
2333     
2334     	/* Pointer to UART in parameter ram.
2335     	*/
2336     	up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2337     
2338     	/* Get the address of the host memory buffer.
2339     	 * If the port has been initialized for general use, we must
2340     	 * use information from the port structure.
2341     	 */
2342     	if ((info = (ser_info_t *)ser->info))
2343     		bdp = info->rx_cur;
2344     	else
2345     		bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2346     
2347     	/*
2348     	 * We need to gracefully shut down the receiver, disable
2349     	 * interrupts, then read the input.
2350     	 * XMON just wants a poll.  If no character, return -1, else
2351     	 * return the character.
2352     	 */
2353     	if (!xmon) {
2354     		while (bdp->cbd_sc & BD_SC_EMPTY);
2355     	}
2356     	else {
2357     		if (bdp->cbd_sc & BD_SC_EMPTY)
2358     			return -1;
2359     	}
2360     
2361     	/* If the buffer address is in the CPM DPRAM, don't
2362     	 * convert it.
2363     	 */
2364     	if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2365     		cp = (u_char *)(bdp->cbd_bufaddr);
2366     	else
2367     		cp = __va(bdp->cbd_bufaddr);
2368     
2369     	if (obuf) {
2370     		i = c = bdp->cbd_datlen;
2371     		while (i-- > 0)
2372     			*obuf++ = *cp++;
2373     	}
2374     	else {
2375     		c = *cp;
2376     	}
2377     	bdp->cbd_sc |= BD_SC_EMPTY;
2378     
2379     	if (info) {
2380     		if (bdp->cbd_sc & BD_SC_WRAP) {
2381     			bdp = info->rx_bd_base;
2382     		}
2383     		else {
2384     			bdp++;
2385     		}
2386     		info->rx_cur = (cbd_t *)bdp;
2387     	}
2388     
2389     	return((int)c);
2390     }
2391     
2392     static int serial_console_wait_key(struct console *co)
2393     {
2394     	return(my_console_wait_key(co->index, 0, NULL));
2395     }
2396     
2397     #ifdef CONFIG_XMON
2398     int
2399     xmon_8xx_read_poll(void)
2400     {
2401     	return(my_console_wait_key(0, 1, NULL));
2402     }
2403     
2404     int
2405     xmon_8xx_read_char(void)
2406     {
2407     	return(my_console_wait_key(0, 0, NULL));
2408     }
2409     #endif
2410     
2411     #ifdef CONFIG_KGDB
2412     static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2413     static int kgdb_chars;
2414     
2415     unsigned char
2416     getDebugChar(void)
2417     {
2418     	if (kgdb_chars <= 0) {
2419     		kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2420     		kgdp = kgdb_buf;
2421     	}
2422     	kgdb_chars--;
2423     
2424     	return(*kgdp++);
2425     }
2426     
2427     void kgdb_interruptible(int state)
2428     {
2429     }
2430     void kgdb_map_scc(void)
2431     {
2432     	struct		serial_state *ser;
2433     	uint		mem_addr;
2434     	volatile	cbd_t		*bdp;
2435     	volatile	smc_uart_t	*up;
2436     
2437     	cpmp = (cpm8xx_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2438     
2439     	/* To avoid data cache CPM DMA coherency problems, allocate a
2440     	 * buffer in the CPM DPRAM.  This will work until the CPM and
2441     	 * serial ports are initialized.  At that time a memory buffer
2442     	 * will be allocated.
2443     	 * The port is already initialized from the boot procedure, all
2444     	 * we do here is give it a different buffer and make it a FIFO.
2445     	 */
2446     
2447     	ser = rs_table;
2448     
2449     	/* Right now, assume we are using SMCs.
2450     	*/
2451     	up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2452     
2453     	/* Allocate space for an input FIFO, plus a few bytes for output.
2454     	 * Allocate bytes to maintain word alignment.
2455     	 */
2456     	mem_addr = (uint)(&cpmp->cp_dpmem[0x1000]);
2457     
2458     	/* Set the physical address of the host memory buffers in
2459     	 * the buffer descriptors.
2460     	 */
2461     	bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2462     	bdp->cbd_bufaddr = mem_addr;
2463     
2464     	bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2465     	bdp->cbd_bufaddr = mem_addr+RX_BUF_SIZE;
2466     
2467     	up->smc_mrblr = RX_BUF_SIZE;		/* receive buffer length */
2468     	up->smc_maxidl = RX_BUF_SIZE;
2469     }
2470     #endif
2471     
2472     static kdev_t serial_console_device(struct console *c)
2473     {
2474     	return MKDEV(TTY_MAJOR, 64 + c->index);
2475     }
2476     
2477     /*
2478      *	Register console.
2479      */
2480     long __init console_8xx_init(long kmem_start, long kmem_end)
2481     {
2482     	register_console(&sercons);
2483     	return kmem_start;
2484     }
2485     
2486     #endif
2487     
2488     /* Index in baud rate table of the default console baud rate.
2489     */
2490     static	int	baud_idx;
2491     
2492     /*
2493      * The serial driver boot-time initialization code!
2494      */
2495     int __init rs_8xx_init(void)
2496     {
2497     	struct serial_state * state;
2498     	ser_info_t	*info;
2499     	uint		mem_addr, dp_addr, iobits;
2500     	int		i, j, idx;
2501     	ushort		chan;
2502     	volatile	cbd_t		*bdp;
2503     	volatile	cpm8xx_t	*cp;
2504     	volatile	smc_t		*sp;
2505     	volatile	smc_uart_t	*up;
2506     	volatile	scc_t		*scp;
2507     	volatile	scc_uart_t	*sup;
2508     	volatile	immap_t		*immap;
2509     	
2510     	init_bh(SERIAL_BH, do_serial_bh);
2511     
2512     	show_serial_version();
2513     
2514     	/* Initialize the tty_driver structure */
2515     	
2516     	__clear_user(&serial_driver,sizeof(struct tty_driver));
2517     	serial_driver.magic = TTY_DRIVER_MAGIC;
2518     	serial_driver.driver_name = "serial";
2519     	serial_driver.name = "ttyS";
2520     	serial_driver.major = TTY_MAJOR;
2521     	serial_driver.minor_start = 64;
2522     	serial_driver.num = NR_PORTS;
2523     	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2524     	serial_driver.subtype = SERIAL_TYPE_NORMAL;
2525     	serial_driver.init_termios = tty_std_termios;
2526     	serial_driver.init_termios.c_cflag =
2527     		baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2528     	serial_driver.flags = TTY_DRIVER_REAL_RAW;
2529     	serial_driver.refcount = &serial_refcount;
2530     	serial_driver.table = serial_table;
2531     	serial_driver.termios = serial_termios;
2532     	serial_driver.termios_locked = serial_termios_locked;
2533     
2534     	serial_driver.open = rs_8xx_open;
2535     	serial_driver.close = rs_8xx_close;
2536     	serial_driver.write = rs_8xx_write;
2537     	serial_driver.put_char = rs_8xx_put_char;
2538     	serial_driver.write_room = rs_8xx_write_room;
2539     	serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer;
2540     	serial_driver.flush_buffer = rs_8xx_flush_buffer;
2541     	serial_driver.ioctl = rs_8xx_ioctl;
2542     	serial_driver.throttle = rs_8xx_throttle;
2543     	serial_driver.unthrottle = rs_8xx_unthrottle;
2544     	serial_driver.send_xchar = rs_8xx_send_xchar;
2545     	serial_driver.set_termios = rs_8xx_set_termios;
2546     	serial_driver.stop = rs_8xx_stop;
2547     	serial_driver.start = rs_8xx_start;
2548     	serial_driver.hangup = rs_8xx_hangup;
2549     	serial_driver.wait_until_sent = rs_8xx_wait_until_sent;
2550     	serial_driver.read_proc = rs_8xx_read_proc;
2551     	
2552     	/*
2553     	 * The callout device is just like normal device except for
2554     	 * major number and the subtype code.
2555     	 */
2556     	callout_driver = serial_driver;
2557     	callout_driver.name = "cua";
2558     	callout_driver.major = TTYAUX_MAJOR;
2559     	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2560     	callout_driver.read_proc = 0;
2561     	callout_driver.proc_entry = 0;
2562     
2563     	if (tty_register_driver(&serial_driver))
2564     		panic("Couldn't register serial driver\n");
2565     	if (tty_register_driver(&callout_driver))
2566     		panic("Couldn't register callout driver\n");
2567     	
2568     	cp = cpmp;	/* Get pointer to Communication Processor */
2569     	immap = (immap_t *)IMAP_ADDR;	/* and to internal registers */
2570     
2571     
2572     	/* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2573     	 */
2574     #ifdef CONFIG_USE_SCC_IO
2575     #ifndef CONFIG_MBX
2576     	/* The "standard" configuration through the 860.
2577     	*/
2578     	immap->im_ioport.iop_papar |= 0x00fc;
2579     	immap->im_ioport.iop_padir &= ~0x00fc;
2580     	immap->im_ioport.iop_paodr &= ~0x00fc;
2581     #else
2582     	/* On the MBX, SCC3 is through Port D.
2583     	*/
2584     	immap->im_ioport.iop_papar |= 0x000c;	/* SCC2 on port A */
2585     	immap->im_ioport.iop_padir &= ~0x000c;
2586     	immap->im_ioport.iop_paodr &= ~0x000c;
2587     
2588     	immap->im_ioport.iop_pdpar |= 0x0030;	/* SCC3 on port D */
2589     #endif
2590     
2591     	/* Since we don't yet do modem control, connect the port C pins
2592     	 * as general purpose I/O.  This will assert CTS and CD for the
2593     	 * SCC ports.
2594     	 */
2595     	immap->im_ioport.iop_pcdir |= 0x03c6;
2596     	immap->im_ioport.iop_pcpar &= ~0x03c6;
2597     
2598     	/* Connect SCC2 and SCC3 to NMSI.  Connect BRG3 to SCC2 and
2599     	 * BRG4 to SCC3.
2600     	 */
2601     	cp->cp_sicr &= ~0x00ffff00;
2602     	cp->cp_sicr |= 0x001b1200;
2603     
2604     #ifdef CONFIG_PP04
2605     	/* Frequentis PP04 forced to RS-232 until we know better.
2606     	 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2607     	 */
2608     	immap->im_ioport.iop_pcdir |= 0x000c;
2609     	immap->im_ioport.iop_pcpar &= ~0x000c;
2610     	immap->im_ioport.iop_pcdat &= ~0x000c;
2611     
2612     	/* This enables the TX driver.
2613     	*/
2614     	cp->cp_pbpar &= ~0x6000;
2615     	cp->cp_pbdat &= ~0x6000;
2616     #endif
2617     #endif
2618     
2619     	for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2620     		state->magic = SSTATE_MAGIC;
2621     		state->line = i;
2622     		state->type = PORT_UNKNOWN;
2623     		state->custom_divisor = 0;
2624     		state->close_delay = 5*HZ/10;
2625     		state->closing_wait = 30*HZ;
2626     		state->callout_termios = callout_driver.init_termios;
2627     		state->normal_termios = serial_driver.init_termios;
2628     		state->icount.cts = state->icount.dsr = 
2629     			state->icount.rng = state->icount.dcd = 0;
2630     		state->icount.rx = state->icount.tx = 0;
2631     		state->icount.frame = state->icount.parity = 0;
2632     		state->icount.overrun = state->icount.brk = 0;
2633     		printk(KERN_INFO "ttyS%02d at 0x%04x is a %s\n",
2634     		       i, (unsigned int)(state->port),
2635     		       (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");
2636     #ifdef CONFIG_SERIAL_CONSOLE
2637     		/* If we just printed the message on the console port, and
2638     		 * we are about to initialize it for general use, we have
2639     		 * to wait a couple of character times for the CR/NL to
2640     		 * make it out of the transmit buffer.
2641     		 */
2642     		if (i == CONFIG_SERIAL_CONSOLE_PORT)
2643     			mdelay(2);
2644     #endif
2645     		info = kmalloc(sizeof(ser_info_t), GFP_KERNEL);
2646     		if (info) {
2647     			__clear_user(info,sizeof(ser_info_t));
2648     			init_waitqueue_head(&info->open_wait);
2649     			init_waitqueue_head(&info->close_wait);
2650     			info->magic = SERIAL_MAGIC;
2651     			info->flags = state->flags;
2652     			info->tqueue.routine = do_softint;
2653     			info->tqueue.data = info;
2654     			info->tqueue_hangup.routine = do_serial_hangup;
2655     			info->tqueue_hangup.data = info;
2656     			info->line = i;
2657     			info->state = state;
2658     			state->info = (struct async_struct *)info;
2659     
2660     			/* We need to allocate a transmit and receive buffer
2661     			 * descriptors from dual port ram, and a character
2662     			 * buffer area from host mem.
2663     			 */
2664     			dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * RX_NUM_FIFO);
2665     
2666     			/* Allocate space for FIFOs in the host memory.
2667     			*/
2668     			mem_addr = m8xx_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE);
2669     
2670     			/* Set the physical address of the host memory
2671     			 * buffers in the buffer descriptors, and the
2672     			 * virtual address for us to work with.
2673     			 */
2674     			bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2675     			info->rx_cur = info->rx_bd_base = (cbd_t *)bdp;
2676     
2677     			for (j=0; j<(RX_NUM_FIFO-1); j++) {
2678     				bdp->cbd_bufaddr = __pa(mem_addr);
2679     				bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2680     				mem_addr += RX_BUF_SIZE;
2681     				bdp++;
2682     			}
2683     			bdp->cbd_bufaddr = __pa(mem_addr);
2684     			bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2685     
2686     			idx = PORT_NUM(info->state->smc_scc_num);
2687     			if (info->state->smc_scc_num & NUM_IS_SCC) {
2688     				scp = &cp->cp_scc[idx];
2689     				sup = (scc_uart_t *)&cp->cp_dparam[state->port];
2690     				sup->scc_genscc.scc_rbase = dp_addr;
2691     			}
2692     			else {
2693     				sp = &cp->cp_smc[idx];
2694     				up = (smc_uart_t *)&cp->cp_dparam[state->port];
2695     				up->smc_rbase = dp_addr;
2696     			}
2697     
2698     			dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * TX_NUM_FIFO);
2699     
2700     			/* Allocate space for FIFOs in the host memory.
2701     			*/
2702     			mem_addr = m8xx_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE);
2703     
2704     			/* Set the physical address of the host memory
2705     			 * buffers in the buffer descriptors, and the
2706     			 * virtual address for us to work with.
2707     			 */
2708     			bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2709     			info->tx_cur = info->tx_bd_base = (cbd_t *)bdp;
2710     
2711     			for (j=0; j<(TX_NUM_FIFO-1); j++) {
2712     				bdp->cbd_bufaddr = __pa(mem_addr);
2713     				bdp->cbd_sc = BD_SC_INTRPT;
2714     				mem_addr += TX_BUF_SIZE;
2715     				bdp++;
2716     			}
2717     			bdp->cbd_bufaddr = __pa(mem_addr);
2718     			bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2719     
2720     			if (info->state->smc_scc_num & NUM_IS_SCC) {
2721     				sup->scc_genscc.scc_tbase = dp_addr;
2722     
2723     				/* Set up the uart parameters in the
2724     				 * parameter ram.
2725     				 */
2726     				sup->scc_genscc.scc_rfcr = SMC_EB;
2727     				sup->scc_genscc.scc_tfcr = SMC_EB;
2728     
2729     				/* Set this to 1 for now, so we get single
2730     				 * character interrupts.  Using idle charater
2731     				 * time requires some additional tuning.
2732     				 */
2733     				sup->scc_genscc.scc_mrblr = 1;
2734     				sup->scc_maxidl = 0;
2735     				sup->scc_brkcr = 1;
2736     				sup->scc_parec = 0;
2737     				sup->scc_frmec = 0;
2738     				sup->scc_nosec = 0;
2739     				sup->scc_brkec = 0;
2740     				sup->scc_uaddr1 = 0;
2741     				sup->scc_uaddr2 = 0;
2742     				sup->scc_toseq = 0;
2743     				sup->scc_char1 = 0x8000;
2744     				sup->scc_char2 = 0x8000;
2745     				sup->scc_char3 = 0x8000;
2746     				sup->scc_char4 = 0x8000;
2747     				sup->scc_char5 = 0x8000;
2748     				sup->scc_char6 = 0x8000;
2749     				sup->scc_char7 = 0x8000;
2750     				sup->scc_char8 = 0x8000;
2751     				sup->scc_rccm = 0xc0ff;
2752     
2753     				/* Send the CPM an initialize command.
2754     				*/
2755     				chan = scc_chan_map[idx];
2756     
2757     				cp->cp_cpcr = mk_cr_cmd(chan,
2758     						CPM_CR_INIT_TRX) | CPM_CR_FLG;
2759     				while (cp->cp_cpcr & CPM_CR_FLG);
2760     
2761     				/* Set UART mode, 8 bit, no parity, one stop.
2762     				 * Enable receive and transmit.
2763     				 */
2764     				scp->scc_gsmrh = 0;
2765     				scp->scc_gsmrl = 
2766     					(SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2767     
2768     				/* Disable all interrupts and clear all pending
2769     				 * events.
2770     				 */
2771     				scp->scc_sccm = 0;
2772     				scp->scc_scce = 0xffff;
2773     				scp->scc_dsr = 0x7e7e;
2774     				scp->scc_pmsr = 0x3000;
2775     
2776     				/* If the port is the console, enable Rx and Tx.
2777     				*/
2778     #ifdef CONFIG_SERIAL_CONSOLE
2779     				if (i == CONFIG_SERIAL_CONSOLE_PORT)
2780     					scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2781     #endif
2782     			}
2783     			else {
2784     				/* Configure SMCs Tx/Rx instead of port B
2785     				 * parallel I/O.  On 823/850 these are on
2786     				 * port A for SMC2.
2787     				 */
2788     #ifndef CONFIG_ALTSMC2
2789     				iobits = 0xc0 << (idx * 4);
2790     				cp->cp_pbpar |= iobits;
2791     				cp->cp_pbdir &= ~iobits;
2792     				cp->cp_pbodr &= ~iobits;
2793     #else
2794     				iobits = 0xc0;
2795     				if (idx == 0) {
2796     					/* SMC1 on Port B, like all 8xx.
2797     					*/
2798     					cp->cp_pbpar |= iobits;
2799     					cp->cp_pbdir &= ~iobits;
2800     					cp->cp_pbodr &= ~iobits;
2801     				}
2802     				else {
2803     					/* SMC2 is on Port A.
2804     					*/
2805     					immap->im_ioport.iop_papar |= iobits;
2806     					immap->im_ioport.iop_padir &= ~iobits;
2807     					immap->im_ioport.iop_paodr &= ~iobits;
2808     				}
2809     #endif /* CONFIG_ALTSMC2 */
2810     
2811     				/* Connect the baud rate generator to the
2812     				 * SMC based upon index in rs_table.  Also
2813     				 * make sure it is connected to NMSI.
2814     				 */
2815     				cp->cp_simode &= ~(0xffff << (idx * 16));
2816     				cp->cp_simode |= (i << ((idx * 16) + 12));
2817     
2818     				up->smc_tbase = dp_addr;
2819     
2820     				/* Set up the uart parameters in the
2821     				 * parameter ram.
2822     				 */
2823     				up->smc_rfcr = SMC_EB;
2824     				up->smc_tfcr = SMC_EB;
2825     
2826     				/* Set this to 1 for now, so we get single
2827     				 * character interrupts.  Using idle charater
2828     				 * time requires some additional tuning.
2829     				 */
2830     				up->smc_mrblr = 1;
2831     				up->smc_maxidl = 0;
2832     				up->smc_brkcr = 1;
2833     
2834     				/* Send the CPM an initialize command.
2835     				*/
2836     				chan = smc_chan_map[idx];
2837     
2838     				cp->cp_cpcr = mk_cr_cmd(chan,
2839     						CPM_CR_INIT_TRX) | CPM_CR_FLG;
2840     				while (cp->cp_cpcr & CPM_CR_FLG);
2841     
2842     				/* Set UART mode, 8 bit, no parity, one stop.
2843     				 * Enable receive and transmit.
2844     				 */
2845     				sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2846     
2847     				/* Disable all interrupts and clear all pending
2848     				 * events.
2849     				 */
2850     				sp->smc_smcm = 0;
2851     				sp->smc_smce = 0xff;
2852     
2853     				/* If the port is the console, enable Rx and Tx.
2854     				*/
2855     #ifdef CONFIG_SERIAL_CONSOLE
2856     				if (i == CONFIG_SERIAL_CONSOLE_PORT)
2857     					sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2858     #endif
2859     			}
2860     
2861     			/* Install interrupt handler.
2862     			*/
2863     			cpm_install_handler(state->irq, rs_8xx_interrupt, info);
2864     
2865     			/* Set up the baud rate generator.
2866     			*/
2867     			m8xx_cpm_setbrg(i, baud_table[baud_idx]);
2868     
2869     		}
2870     	}
2871     
2872     	return 0;
2873     }
2874     
2875     /* This must always be called before the rs_8xx_init() function, otherwise
2876      * it blows away the port control information.
2877     */
2878     static int __init serial_console_setup(struct console *co, char *options)
2879     {
2880     	struct		serial_state *ser;
2881     	uint		mem_addr, dp_addr, bidx, idx;
2882     	ushort		chan;
2883     	volatile	cbd_t		*bdp;
2884     	volatile	cpm8xx_t	*cp;
2885     	volatile	smc_t		*sp;
2886     	volatile	scc_t		*scp;
2887     	volatile	smc_uart_t	*up;
2888     	volatile	scc_uart_t	*sup;
2889     	bd_t				*bd;
2890     
2891     	bd = (bd_t *)__res;
2892     
2893     	for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2894     		if (bd->bi_baudrate == baud_table[bidx])
2895     			break;
2896     	/* make sure we have a useful value */
2897     	if (bidx == (sizeof(baud_table) / sizeof(int)))
2898     		bidx = 13;	/* B9600 */
2899     
2900     	co->cflag = CREAD|CLOCAL|bidx|CS8;
2901     	baud_idx = bidx;
2902     
2903     	ser = rs_table + co->index;
2904     
2905     	cp = cpmp;	/* Get pointer to Communication Processor */
2906     
2907     	idx = PORT_NUM(ser->smc_scc_num);
2908     	if (ser->smc_scc_num & NUM_IS_SCC) {
2909     		scp = &cp->cp_scc[idx];
2910     		sup = (scc_uart_t *)&cp->cp_dparam[ser->port];
2911     	}
2912     	else {
2913     		sp = &cp->cp_smc[idx];
2914     		up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2915     	}
2916     
2917     	/* When we get here, the CPM has been reset, so we need
2918     	 * to configure the port.
2919     	 * We need to allocate a transmit and receive buffer descriptor
2920     	 * from dual port ram, and a character buffer area from host mem.
2921     	 */
2922     
2923     	/* Allocate space for two buffer descriptors in the DP ram.
2924     	*/
2925     	dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * 2);
2926     
2927     	/* Allocate space for two 2 byte FIFOs in the host memory.
2928     	*/
2929     	mem_addr = m8xx_cpm_hostalloc(8);
2930     
2931     	/* Set the physical address of the host memory buffers in
2932     	 * the buffer descriptors.
2933     	 */
2934     	bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2935     	bdp->cbd_bufaddr = __pa(mem_addr);
2936     	(bdp+1)->cbd_bufaddr = __pa(mem_addr+4);
2937     
2938     	/* For the receive, set empty and wrap.
2939     	 * For transmit, set wrap.
2940     	 */
2941     	bdp->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
2942     	(bdp+1)->cbd_sc = BD_SC_WRAP;
2943     
2944     	/* Set up the uart parameters in the parameter ram.
2945     	*/
2946     	if (ser->smc_scc_num & NUM_IS_SCC) {
2947     
2948     		sup->scc_genscc.scc_rbase = dp_addr;
2949     		sup->scc_genscc.scc_tbase = dp_addr + sizeof(cbd_t);
2950     
2951     		/* Set up the uart parameters in the
2952     		 * parameter ram.
2953     		 */
2954     		sup->scc_genscc.scc_rfcr = SMC_EB;
2955     		sup->scc_genscc.scc_tfcr = SMC_EB;
2956     
2957     		/* Set this to 1 for now, so we get single
2958     		 * character interrupts.  Using idle charater
2959     		 * time requires some additional tuning.
2960     		 */
2961     		sup->scc_genscc.scc_mrblr = 1;
2962     		sup->scc_maxidl = 0;
2963     		sup->scc_brkcr = 1;
2964     		sup->scc_parec = 0;
2965     		sup->scc_frmec = 0;
2966     		sup->scc_nosec = 0;
2967     		sup->scc_brkec = 0;
2968     		sup->scc_uaddr1 = 0;
2969     		sup->scc_uaddr2 = 0;
2970     		sup->scc_toseq = 0;
2971     		sup->scc_char1 = 0x8000;
2972     		sup->scc_char2 = 0x8000;
2973     		sup->scc_char3 = 0x8000;
2974     		sup->scc_char4 = 0x8000;
2975     		sup->scc_char5 = 0x8000;
2976     		sup->scc_char6 = 0x8000;
2977     		sup->scc_char7 = 0x8000;
2978     		sup->scc_char8 = 0x8000;
2979     		sup->scc_rccm = 0xc0ff;
2980     
2981     		/* Send the CPM an initialize command.
2982     		*/
2983     		chan = scc_chan_map[idx];
2984     
2985     		cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2986     		while (cp->cp_cpcr & CPM_CR_FLG);
2987     
2988     		/* Set UART mode, 8 bit, no parity, one stop.
2989     		 * Enable receive and transmit.
2990     		 */
2991     		scp->scc_gsmrh = 0;
2992     		scp->scc_gsmrl = 
2993     			(SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2994     
2995     		/* Disable all interrupts and clear all pending
2996     		 * events.
2997     		 */
2998     		scp->scc_sccm = 0;
2999     		scp->scc_scce = 0xffff;
3000     		scp->scc_dsr = 0x7e7e;
3001     		scp->scc_pmsr = 0x3000;
3002     
3003     		scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
3004     
3005     	}
3006     	else {
3007     		up->smc_rbase = dp_addr;	/* Base of receive buffer desc. */
3008     		up->smc_tbase = dp_addr+sizeof(cbd_t);	/* Base of xmt buffer desc. */
3009     		up->smc_rfcr = SMC_EB;
3010     		up->smc_tfcr = SMC_EB;
3011     
3012     		/* Set this to 1 for now, so we get single character interrupts.
3013     		*/
3014     		up->smc_mrblr = 1;		/* receive buffer length */
3015     		up->smc_maxidl = 0;		/* wait forever for next char */
3016     
3017     		/* Send the CPM an initialize command.
3018     		*/
3019     		chan = smc_chan_map[idx];
3020     		cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3021     		printk("%s", "");
3022     		while (cp->cp_cpcr & CPM_CR_FLG);
3023     
3024     		/* Set UART mode, 8 bit, no parity, one stop.
3025     		 * Enable receive and transmit.
3026     		 */
3027     		sp->smc_smcmr = smcr_mk_clen(9) |  SMCMR_SM_UART;
3028     
3029     		/* And finally, enable Rx and Tx.
3030     		*/
3031     		sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
3032     	}
3033     
3034     	/* Set up the baud rate generator.
3035     	*/
3036     	m8xx_cpm_setbrg((ser - rs_table), bd->bi_baudrate);
3037     
3038     	return 0;
3039     }
3040     
3041