1/*
2 * linux/drivers/char/serial167.c
3 *
4 * Driver for MVME166/7 board serial ports, which are via a CD2401.
5 * Based very much on cyclades.c.
6 *
7 * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
8 *
9 * ==============================================================
10 *
11 * static char rcsid[] =
12 * "$Revision: 1.1.1.1 $$Date: 2007/08/03 18:52:28 $";
13 *
14 *  linux/kernel/cyclades.c
15 *
16 * Maintained by Marcio Saito (cyclades@netcom.com) and
17 * Randolph Bentson (bentson@grieg.seaslug.org)
18 *
19 * Much of the design and some of the code came from serial.c
20 * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
21 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
22 * and then fixed as suggested by Michael K. Johnson 12/12/92.
23 *
24 * This version does not support shared irq's.
25 *
26 * $Log: serial167.c,v $
27 * Revision 1.1.1.1  2007/08/03 18:52:28  rnuti
28 * Importing Linux MIPS Kernel 2.6.22
29 *
30 * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
31 * disambiguate between Cyclom-16Y and Cyclom-32Ye;
32 *
33 * Changes:
34 *
35 * 200 lines of changes record removed - RGH 11-10-95, starting work on
36 * converting this to drive serial ports on mvme166 (cd2401).
37 *
38 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
39 * - get rid of verify_area
40 * - use get_user to access memory from userspace in set_threshold,
41 *   set_default_threshold and set_timeout
42 * - don't use the panic function in serial167_init
43 * - do resource release on failure on serial167_init
44 * - include missing restore_flags in mvme167_serial_console_setup
45 *
46 * Kars de Jong <jongk@linux-m68k.org> - 2004/09/06
47 * - replace bottom half handler with task queue handler
48 */
49
50#include <linux/errno.h>
51#include <linux/signal.h>
52#include <linux/sched.h>
53#include <linux/timer.h>
54#include <linux/tty.h>
55#include <linux/interrupt.h>
56#include <linux/serial.h>
57#include <linux/serialP.h>
58#include <linux/string.h>
59#include <linux/fcntl.h>
60#include <linux/ptrace.h>
61#include <linux/serial167.h>
62#include <linux/delay.h>
63#include <linux/major.h>
64#include <linux/mm.h>
65#include <linux/console.h>
66#include <linux/module.h>
67#include <linux/bitops.h>
68#include <linux/tty_flip.h>
69
70#include <asm/system.h>
71#include <asm/io.h>
72#include <asm/mvme16xhw.h>
73#include <asm/bootinfo.h>
74#include <asm/setup.h>
75
76#include <linux/types.h>
77#include <linux/kernel.h>
78
79#include <asm/uaccess.h>
80#include <linux/init.h>
81
82#define SERIAL_PARANOIA_CHECK
83#undef  SERIAL_DEBUG_OPEN
84#undef  SERIAL_DEBUG_THROTTLE
85#undef  SERIAL_DEBUG_OTHER
86#undef  SERIAL_DEBUG_IO
87#undef  SERIAL_DEBUG_COUNT
88#undef  SERIAL_DEBUG_DTR
89#undef  CYCLOM_16Y_HACK
90#define  CYCLOM_ENABLE_MONITORING
91
92#define WAKEUP_CHARS 256
93
94#define STD_COM_FLAGS (0)
95
96#define SERIAL_TYPE_NORMAL  1
97
98static struct tty_driver *cy_serial_driver;
99extern int serial_console;
100static struct cyclades_port *serial_console_info = NULL;
101static unsigned int serial_console_cflag = 0;
102u_char initial_console_speed;
103
104/* Base address of cd2401 chip on mvme166/7 */
105
106#define BASE_ADDR (0xfff45000)
107#define pcc2chip	((volatile u_char *)0xfff42000)
108#define PccSCCMICR	0x1d
109#define PccSCCTICR	0x1e
110#define PccSCCRICR	0x1f
111#define PccTPIACKR	0x25
112#define PccRPIACKR	0x27
113#define PccIMLR		0x3f
114
115/* This is the per-port data structure */
116struct cyclades_port cy_port[] = {
117	/* CARD#  */
118	{-1},			/* ttyS0 */
119	{-1},			/* ttyS1 */
120	{-1},			/* ttyS2 */
121	{-1},			/* ttyS3 */
122};
123
124#define NR_PORTS        ARRAY_SIZE(cy_port)
125
126/*
127 * This is used to look up the divisor speeds and the timeouts
128 * We're normally limited to 15 distinct baud rates.  The extra
129 * are accessed via settings in info->flags.
130 *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
131 *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
132 *                                                  HI            VHI
133 */
134static int baud_table[] = {
135	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
136	1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
137	0
138};
139
140
141/* I think 166 brd clocks 2401 at 20MHz.... */
142
143/* These values are written directly to tcor, and >> 5 for writing to rcor */
144static u_char baud_co[] = {	/* 20 MHz clock option table */
145	0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x60, 0x60, 0x40,
146	0x40, 0x40, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
147};
148
149/* These values written directly to tbpr/rbpr */
150static u_char baud_bpr[] = {	/* 20 MHz baud rate period table */
151	0x00, 0xc0, 0x80, 0x58, 0x6c, 0x40, 0xc0, 0x81, 0x40, 0x81,
152	0x57, 0x40, 0x81, 0x40, 0x81, 0x40, 0x2b, 0x20, 0x15, 0x10
153};
154
155static u_char baud_cor4[] = {	/* receive threshold */
156	0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
157	0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07
158};
159
160static void shutdown(struct cyclades_port *);
161static int startup(struct cyclades_port *);
162static void cy_throttle(struct tty_struct *);
163static void cy_unthrottle(struct tty_struct *);
164static void config_setup(struct cyclades_port *);
165extern void console_print(const char *);
166#ifdef CYCLOM_SHOW_STATUS
167static void show_status(int);
168#endif
169
170#ifdef CONFIG_REMOTE_DEBUG
171static void debug_setup(void);
172void queueDebugChar(int c);
173int getDebugChar(void);
174
175#define DEBUG_PORT	1
176#define DEBUG_LEN	256
177
178typedef struct {
179	int in;
180	int out;
181	unsigned char buf[DEBUG_LEN];
182} debugq;
183
184debugq debugiq;
185#endif
186
187/*
188 * I have my own version of udelay(), as it is needed when initialising
189 * the chip, before the delay loop has been calibrated.  Should probably
190 * reference one of the vmechip2 or pccchip2 counter for an accurate
191 * delay, but this wild guess will do for now.
192 */
193
194void my_udelay(long us)
195{
196	u_char x;
197	volatile u_char *p = &x;
198	int i;
199
200	while (us--)
201		for (i = 100; i; i--)
202			x |= *p;
203}
204
205static inline int serial_paranoia_check(struct cyclades_port *info, char *name,
206		const char *routine)
207{
208#ifdef SERIAL_PARANOIA_CHECK
209	if (!info) {
210		printk("Warning: null cyclades_port for (%s) in %s\n", name,
211				routine);
212		return 1;
213	}
214
215	if ((long)info < (long)(&cy_port[0])
216	    || (long)(&cy_port[NR_PORTS]) < (long)info) {
217		printk("Warning: cyclades_port out of range for (%s) in %s\n",
218				name, routine);
219		return 1;
220	}
221
222	if (info->magic != CYCLADES_MAGIC) {
223		printk("Warning: bad magic number for serial struct (%s) in "
224				"%s\n", name, routine);
225		return 1;
226	}
227#endif
228	return 0;
229}				/* serial_paranoia_check */
230
231
232/* This routine waits up to 1000 micro-seconds for the previous
233   command to the Cirrus chip to complete and then issues the
234   new command.  An error is returned if the previous command
235   didn't finish within the time limit.
236 */
237u_short write_cy_cmd(volatile u_char * base_addr, u_char cmd)
238{
239	unsigned long flags;
240	volatile int i;
241
242	local_irq_save(flags);
243	/* Check to see that the previous command has completed */
244	for (i = 0; i < 100; i++) {
245		if (base_addr[CyCCR] == 0) {
246			break;
247		}
248		my_udelay(10L);
249	}
250	/* if the CCR never cleared, the previous command
251	   didn't finish within the "reasonable time" */
252	if (i == 10) {
253		local_irq_restore(flags);
254		return (-1);
255	}
256
257	/* Issue the new command */
258	base_addr[CyCCR] = cmd;
259	local_irq_restore(flags);
260	return (0);
261}				/* write_cy_cmd */
262
263/* cy_start and cy_stop provide software output flow control as a
264   function of XON/XOFF, software CTS, and other such stuff. */
265
266static void cy_stop(struct tty_struct *tty)
267{
268	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
269	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
270	int channel;
271	unsigned long flags;
272
273#ifdef SERIAL_DEBUG_OTHER
274	printk("cy_stop %s\n", tty->name);	/* */
275#endif
276
277	if (serial_paranoia_check(info, tty->name, "cy_stop"))
278		return;
279
280	channel = info->line;
281
282	local_irq_save(flags);
283	base_addr[CyCAR] = (u_char) (channel);	/* index channel */
284	base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
285	local_irq_restore(flags);
286}				/* cy_stop */
287
288static void cy_start(struct tty_struct *tty)
289{
290	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
291	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
292	int channel;
293	unsigned long flags;
294
295#ifdef SERIAL_DEBUG_OTHER
296	printk("cy_start %s\n", tty->name);	/* */
297#endif
298
299	if (serial_paranoia_check(info, tty->name, "cy_start"))
300		return;
301
302	channel = info->line;
303
304	local_irq_save(flags);
305	base_addr[CyCAR] = (u_char) (channel);
306	base_addr[CyIER] |= CyTxMpty;
307	local_irq_restore(flags);
308}				/* cy_start */
309
310/*
311 * This routine is used by the interrupt handler to schedule
312 * processing in the software interrupt portion of the driver
313 * (also known as the "bottom half").  This can be called any
314 * number of times for any channel without harm.
315 */
316static inline void cy_sched_event(struct cyclades_port *info, int event)
317{
318	info->event |= 1 << event;	/* remember what kind of event and who */
319	schedule_work(&info->tqueue);
320}				/* cy_sched_event */
321
322/* The real interrupt service routines are called
323   whenever the card wants its hand held--chars
324   received, out buffer empty, modem change, etc.
325 */
326static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
327{
328	struct tty_struct *tty;
329	struct cyclades_port *info;
330	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
331	unsigned char err, rfoc;
332	int channel;
333	char data;
334
335	/* determine the channel and change to that context */
336	channel = (u_short) (base_addr[CyLICR] >> 2);
337	info = &cy_port[channel];
338	info->last_active = jiffies;
339
340	if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
341		/* This is a receive timeout interrupt, ignore it */
342		base_addr[CyREOIR] = CyNOTRANS;
343		return IRQ_HANDLED;
344	}
345
346	/* Read a byte of data if there is any - assume the error
347	 * is associated with this character */
348
349	if ((rfoc = base_addr[CyRFOC]) != 0)
350		data = base_addr[CyRDR];
351	else
352		data = 0;
353
354	/* if there is nowhere to put the data, discard it */
355	if (info->tty == 0) {
356		base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
357		return IRQ_HANDLED;
358	} else {		/* there is an open port for this data */
359		tty = info->tty;
360		if (err & info->ignore_status_mask) {
361			base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
362			return IRQ_HANDLED;
363		}
364		if (tty_buffer_request_room(tty, 1) != 0) {
365			if (err & info->read_status_mask) {
366				if (err & CyBREAK) {
367					tty_insert_flip_char(tty, data,
368							     TTY_BREAK);
369					if (info->flags & ASYNC_SAK) {
370						do_SAK(tty);
371					}
372				} else if (err & CyFRAME) {
373					tty_insert_flip_char(tty, data,
374							     TTY_FRAME);
375				} else if (err & CyPARITY) {
376					tty_insert_flip_char(tty, data,
377							     TTY_PARITY);
378				} else if (err & CyOVERRUN) {
379					tty_insert_flip_char(tty, 0,
380							     TTY_OVERRUN);
381					/*
382					   If the flip buffer itself is
383					   overflowing, we still loose
384					   the next incoming character.
385					 */
386					if (tty_buffer_request_room(tty, 1) !=
387					    0) {
388						tty_insert_flip_char(tty, data,
389								     TTY_FRAME);
390					}
391					/* These two conditions may imply */
392					/* a normal read should be done. */
393					/* else if(data & CyTIMEOUT) */
394					/* else if(data & CySPECHAR) */
395				} else {
396					tty_insert_flip_char(tty, 0,
397							     TTY_NORMAL);
398				}
399			} else {
400				tty_insert_flip_char(tty, data, TTY_NORMAL);
401			}
402		} else {
403			/* there was a software buffer overrun
404			   and nothing could be done about it!!! */
405		}
406	}
407	tty_schedule_flip(tty);
408	/* end of service */
409	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
410	return IRQ_HANDLED;
411}				/* cy_rxerr_interrupt */
412
413static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
414{
415	struct cyclades_port *info;
416	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
417	int channel;
418	int mdm_change;
419	int mdm_status;
420
421	/* determine the channel and change to that context */
422	channel = (u_short) (base_addr[CyLICR] >> 2);
423	info = &cy_port[channel];
424	info->last_active = jiffies;
425
426	mdm_change = base_addr[CyMISR];
427	mdm_status = base_addr[CyMSVR1];
428
429	if (info->tty == 0) {	/* nowhere to put the data, ignore it */
430		;
431	} else {
432		if ((mdm_change & CyDCD)
433		    && (info->flags & ASYNC_CHECK_CD)) {
434			if (mdm_status & CyDCD) {
435/* CP('!'); */
436				cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
437			} else {
438/* CP('@'); */
439				cy_sched_event(info, Cy_EVENT_HANGUP);
440			}
441		}
442		if ((mdm_change & CyCTS)
443		    && (info->flags & ASYNC_CTS_FLOW)) {
444			if (info->tty->stopped) {
445				if (mdm_status & CyCTS) {
446					/* !!! cy_start isn't used because... */
447					info->tty->stopped = 0;
448					base_addr[CyIER] |= CyTxMpty;
449					cy_sched_event(info,
450						       Cy_EVENT_WRITE_WAKEUP);
451				}
452			} else {
453				if (!(mdm_status & CyCTS)) {
454					/* !!! cy_stop isn't used because... */
455					info->tty->stopped = 1;
456					base_addr[CyIER] &=
457					    ~(CyTxMpty | CyTxRdy);
458				}
459			}
460		}
461		if (mdm_status & CyDSR) {
462		}
463	}
464	base_addr[CyMEOIR] = 0;
465	return IRQ_HANDLED;
466}				/* cy_modem_interrupt */
467
468static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
469{
470	struct cyclades_port *info;
471	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
472	int channel;
473	int char_count, saved_cnt;
474	int outch;
475
476	/* determine the channel and change to that context */
477	channel = (u_short) (base_addr[CyLICR] >> 2);
478
479#ifdef CONFIG_REMOTE_DEBUG
480	if (channel == DEBUG_PORT) {
481		panic("TxInt on debug port!!!");
482	}
483#endif
484
485	info = &cy_port[channel];
486
487	/* validate the port number (as configured and open) */
488	if ((channel < 0) || (NR_PORTS <= channel)) {
489		base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
490		base_addr[CyTEOIR] = CyNOTRANS;
491		return IRQ_HANDLED;
492	}
493	info->last_active = jiffies;
494	if (info->tty == 0) {
495		base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
496		if (info->xmit_cnt < WAKEUP_CHARS) {
497			cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
498		}
499		base_addr[CyTEOIR] = CyNOTRANS;
500		return IRQ_HANDLED;
501	}
502
503	/* load the on-chip space available for outbound data */
504	saved_cnt = char_count = base_addr[CyTFTC];
505
506	if (info->x_char) {	/* send special char */
507		outch = info->x_char;
508		base_addr[CyTDR] = outch;
509		char_count--;
510		info->x_char = 0;
511	}
512
513	if (info->x_break) {
514		/*  The Cirrus chip requires the "Embedded Transmit
515		   Commands" of start break, delay, and end break
516		   sequences to be sent.  The duration of the
517		   break is given in TICs, which runs at HZ
518		   (typically 100) and the PPR runs at 200 Hz,
519		   so the delay is duration * 200/HZ, and thus a
520		   break can run from 1/100 sec to about 5/4 sec.
521		   Need to check these values - RGH 141095.
522		 */
523		base_addr[CyTDR] = 0;	/* start break */
524		base_addr[CyTDR] = 0x81;
525		base_addr[CyTDR] = 0;	/* delay a bit */
526		base_addr[CyTDR] = 0x82;
527		base_addr[CyTDR] = info->x_break * 200 / HZ;
528		base_addr[CyTDR] = 0;	/* terminate break */
529		base_addr[CyTDR] = 0x83;
530		char_count -= 7;
531		info->x_break = 0;
532	}
533
534	while (char_count > 0) {
535		if (!info->xmit_cnt) {
536			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
537			break;
538		}
539		if (info->xmit_buf == 0) {
540			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
541			break;
542		}
543		if (info->tty->stopped || info->tty->hw_stopped) {
544			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
545			break;
546		}
547		/* Because the Embedded Transmit Commands have been
548		   enabled, we must check to see if the escape
549		   character, NULL, is being sent.  If it is, we
550		   must ensure that there is room for it to be
551		   doubled in the output stream.  Therefore we
552		   no longer advance the pointer when the character
553		   is fetched, but rather wait until after the check
554		   for a NULL output character. (This is necessary
555		   because there may not be room for the two chars
556		   needed to send a NULL.
557		 */
558		outch = info->xmit_buf[info->xmit_tail];
559		if (outch) {
560			info->xmit_cnt--;
561			info->xmit_tail = (info->xmit_tail + 1)
562			    & (PAGE_SIZE - 1);
563			base_addr[CyTDR] = outch;
564			char_count--;
565		} else {
566			if (char_count > 1) {
567				info->xmit_cnt--;
568				info->xmit_tail = (info->xmit_tail + 1)
569				    & (PAGE_SIZE - 1);
570				base_addr[CyTDR] = outch;
571				base_addr[CyTDR] = 0;
572				char_count--;
573				char_count--;
574			} else {
575				break;
576			}
577		}
578	}
579
580	if (info->xmit_cnt < WAKEUP_CHARS) {
581		cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
582	}
583	base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
584	return IRQ_HANDLED;
585}				/* cy_tx_interrupt */
586
587static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
588{
589	struct tty_struct *tty;
590	struct cyclades_port *info;
591	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
592	int channel;
593	char data;
594	int char_count;
595	int save_cnt;
596	int len;
597
598	/* determine the channel and change to that context */
599	channel = (u_short) (base_addr[CyLICR] >> 2);
600	info = &cy_port[channel];
601	info->last_active = jiffies;
602	save_cnt = char_count = base_addr[CyRFOC];
603
604#ifdef CONFIG_REMOTE_DEBUG
605	if (channel == DEBUG_PORT) {
606		while (char_count--) {
607			data = base_addr[CyRDR];
608			queueDebugChar(data);
609		}
610	} else
611#endif
612		/* if there is nowhere to put the data, discard it */
613	if (info->tty == 0) {
614		while (char_count--) {
615			data = base_addr[CyRDR];
616		}
617	} else {		/* there is an open port for this data */
618		tty = info->tty;
619		/* load # characters available from the chip */
620
621#ifdef CYCLOM_ENABLE_MONITORING
622		++info->mon.int_count;
623		info->mon.char_count += char_count;
624		if (char_count > info->mon.char_max)
625			info->mon.char_max = char_count;
626		info->mon.char_last = char_count;
627#endif
628		len = tty_buffer_request_room(tty, char_count);
629		while (len--) {
630			data = base_addr[CyRDR];
631			tty_insert_flip_char(tty, data, TTY_NORMAL);
632#ifdef CYCLOM_16Y_HACK
633			udelay(10L);
634#endif
635		}
636		tty_schedule_flip(tty);
637	}
638	/* end of service */
639	base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
640	return IRQ_HANDLED;
641}				/* cy_rx_interrupt */
642
643/*
644 * This routine is used to handle the "bottom half" processing for the
645 * serial driver, known also the "software interrupt" processing.
646 * This processing is done at the kernel interrupt level, after the
647 * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
648 * is where time-consuming activities which can not be done in the
649 * interrupt driver proper are done; the interrupt driver schedules
650 * them using cy_sched_event(), and they get done here.
651 *
652 * This is done through one level of indirection--the task queue.
653 * When a hardware interrupt service routine wants service by the
654 * driver's bottom half, it enqueues the appropriate tq_struct (one
655 * per port) to the keventd work queue and sets a request flag
656 * that the work queue be processed.
657 *
658 * Although this may seem unwieldy, it gives the system a way to
659 * pass an argument (in this case the pointer to the cyclades_port
660 * structure) to the bottom half of the driver.  Previous kernels
661 * had to poll every port to see if that port needed servicing.
662 */
663static void do_softint(struct work_struct *ugly_api)
664{
665	struct cyclades_port *info =
666	    container_of(ugly_api, struct cyclades_port, tqueue);
667	struct tty_struct *tty;
668
669	tty = info->tty;
670	if (!tty)
671		return;
672
673	if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
674		tty_hangup(info->tty);
675		wake_up_interruptible(&info->open_wait);
676		info->flags &= ~ASYNC_NORMAL_ACTIVE;
677	}
678	if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
679		wake_up_interruptible(&info->open_wait);
680	}
681	if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
682		tty_wakeup(tty);
683	}
684}				/* do_softint */
685
686/* This is called whenever a port becomes active;
687   interrupts are enabled and DTR & RTS are turned on.
688 */
689static int startup(struct cyclades_port *info)
690{
691	unsigned long flags;
692	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
693	int channel;
694
695	if (info->flags & ASYNC_INITIALIZED) {
696		return 0;
697	}
698
699	if (!info->type) {
700		if (info->tty) {
701			set_bit(TTY_IO_ERROR, &info->tty->flags);
702		}
703		return 0;
704	}
705	if (!info->xmit_buf) {
706		info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
707		if (!info->xmit_buf) {
708			return -ENOMEM;
709		}
710	}
711
712	config_setup(info);
713
714	channel = info->line;
715
716#ifdef SERIAL_DEBUG_OPEN
717	printk("startup channel %d\n", channel);
718#endif
719
720	local_irq_save(flags);
721	base_addr[CyCAR] = (u_char) channel;
722	write_cy_cmd(base_addr, CyENB_RCVR | CyENB_XMTR);
723
724	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
725	base_addr[CyMSVR1] = CyRTS;
726/* CP('S');CP('1'); */
727	base_addr[CyMSVR2] = CyDTR;
728
729#ifdef SERIAL_DEBUG_DTR
730	printk("cyc: %d: raising DTR\n", __LINE__);
731	printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
732	       base_addr[CyMSVR2]);
733#endif
734
735	base_addr[CyIER] |= CyRxData;
736	info->flags |= ASYNC_INITIALIZED;
737
738	if (info->tty) {
739		clear_bit(TTY_IO_ERROR, &info->tty->flags);
740	}
741	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
742
743	local_irq_restore(flags);
744
745#ifdef SERIAL_DEBUG_OPEN
746	printk(" done\n");
747#endif
748	return 0;
749}				/* startup */
750
751void start_xmit(struct cyclades_port *info)
752{
753	unsigned long flags;
754	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
755	int channel;
756
757	channel = info->line;
758	local_irq_save(flags);
759	base_addr[CyCAR] = channel;
760	base_addr[CyIER] |= CyTxMpty;
761	local_irq_restore(flags);
762}				/* start_xmit */
763
764/*
765 * This routine shuts down a serial port; interrupts are disabled,
766 * and DTR is dropped if the hangup on close termio flag is on.
767 */
768static void shutdown(struct cyclades_port *info)
769{
770	unsigned long flags;
771	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
772	int channel;
773
774	if (!(info->flags & ASYNC_INITIALIZED)) {
775/* CP('$'); */
776		return;
777	}
778
779	channel = info->line;
780
781#ifdef SERIAL_DEBUG_OPEN
782	printk("shutdown channel %d\n", channel);
783#endif
784
785	/* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
786	   SENT BEFORE DROPPING THE LINE !!!  (Perhaps
787	   set some flag that is read when XMTY happens.)
788	   Other choices are to delay some fixed interval
789	   or schedule some later processing.
790	 */
791	local_irq_save(flags);
792	if (info->xmit_buf) {
793		free_page((unsigned long)info->xmit_buf);
794		info->xmit_buf = NULL;
795	}
796
797	base_addr[CyCAR] = (u_char) channel;
798	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
799		base_addr[CyMSVR1] = 0;
800/* CP('C');CP('1'); */
801		base_addr[CyMSVR2] = 0;
802#ifdef SERIAL_DEBUG_DTR
803		printk("cyc: %d: dropping DTR\n", __LINE__);
804		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
805		       base_addr[CyMSVR2]);
806#endif
807	}
808	write_cy_cmd(base_addr, CyDIS_RCVR);
809	/* it may be appropriate to clear _XMIT at
810	   some later date (after testing)!!! */
811
812	if (info->tty) {
813		set_bit(TTY_IO_ERROR, &info->tty->flags);
814	}
815	info->flags &= ~ASYNC_INITIALIZED;
816	local_irq_restore(flags);
817
818#ifdef SERIAL_DEBUG_OPEN
819	printk(" done\n");
820#endif
821}				/* shutdown */
822
823/*
824 * This routine finds or computes the various line characteristics.
825 */
826static void config_setup(struct cyclades_port *info)
827{
828	unsigned long flags;
829	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
830	int channel;
831	unsigned cflag;
832	int i;
833	unsigned char ti, need_init_chan = 0;
834
835	if (!info->tty || !info->tty->termios) {
836		return;
837	}
838	if (info->line == -1) {
839		return;
840	}
841	cflag = info->tty->termios->c_cflag;
842
843	/* baud rate */
844	i = cflag & CBAUD;
845#ifdef CBAUDEX
846/* Starting with kernel 1.1.65, there is direct support for
847   higher baud rates.  The following code supports those
848   changes.  The conditional aspect allows this driver to be
849   used for earlier as well as later kernel versions.  (The
850   mapping is slightly different from serial.c because there
851   is still the possibility of supporting 75 kbit/sec with
852   the Cyclades board.)
853 */
854	if (i & CBAUDEX) {
855		if (i == B57600)
856			i = 16;
857		else if (i == B115200)
858			i = 18;
859#ifdef B78600
860		else if (i == B78600)
861			i = 17;
862#endif
863		else
864			info->tty->termios->c_cflag &= ~CBAUDEX;
865	}
866#endif
867	if (i == 15) {
868		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
869			i += 1;
870		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
871			i += 3;
872	}
873	/* Don't ever change the speed of the console port.  It will
874	 * run at the speed specified in bootinfo, or at 19.2K */
875	/* Actually, it should run at whatever speed 166Bug was using */
876	/* Note info->timeout isn't used at present */
877	if (info != serial_console_info) {
878		info->tbpr = baud_bpr[i];	/* Tx BPR */
879		info->tco = baud_co[i];	/* Tx CO */
880		info->rbpr = baud_bpr[i];	/* Rx BPR */
881		info->rco = baud_co[i] >> 5;	/* Rx CO */
882		if (baud_table[i] == 134) {
883			info->timeout =
884			    (info->xmit_fifo_size * HZ * 30 / 269) + 2;
885			/* get it right for 134.5 baud */
886		} else if (baud_table[i]) {
887			info->timeout =
888			    (info->xmit_fifo_size * HZ * 15 / baud_table[i]) +
889			    2;
890			/* this needs to be propagated into the card info */
891		} else {
892			info->timeout = 0;
893		}
894	}
895	/* By tradition (is it a standard?) a baud rate of zero
896	   implies the line should be/has been closed.  A bit
897	   later in this routine such a test is performed. */
898
899	/* byte size and parity */
900	info->cor7 = 0;
901	info->cor6 = 0;
902	info->cor5 = 0;
903	info->cor4 = (info->default_threshold ? info->default_threshold : baud_cor4[i]);	/* receive threshold */
904	/* Following two lines added 101295, RGH. */
905	/* It is obviously wrong to access CyCORx, and not info->corx here,
906	 * try and remember to fix it later! */
907	channel = info->line;
908	base_addr[CyCAR] = (u_char) channel;
909	if (C_CLOCAL(info->tty)) {
910		if (base_addr[CyIER] & CyMdmCh)
911			base_addr[CyIER] &= ~CyMdmCh;	/* without modem intr */
912		/* ignore 1->0 modem transitions */
913		if (base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD))
914			base_addr[CyCOR4] &= ~(CyDSR | CyCTS | CyDCD);
915		/* ignore 0->1 modem transitions */
916		if (base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD))
917			base_addr[CyCOR5] &= ~(CyDSR | CyCTS | CyDCD);
918	} else {
919		if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
920			base_addr[CyIER] |= CyMdmCh;	/* with modem intr */
921		/* act on 1->0 modem transitions */
922		if ((base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD)) !=
923		    (CyDSR | CyCTS | CyDCD))
924			base_addr[CyCOR4] |= CyDSR | CyCTS | CyDCD;
925		/* act on 0->1 modem transitions */
926		if ((base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD)) !=
927		    (CyDSR | CyCTS | CyDCD))
928			base_addr[CyCOR5] |= CyDSR | CyCTS | CyDCD;
929	}
930	info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
931	info->cor2 = CyETC;
932	switch (cflag & CSIZE) {
933	case CS5:
934		info->cor1 = Cy_5_BITS;
935		break;
936	case CS6:
937		info->cor1 = Cy_6_BITS;
938		break;
939	case CS7:
940		info->cor1 = Cy_7_BITS;
941		break;
942	case CS8:
943		info->cor1 = Cy_8_BITS;
944		break;
945	}
946	if (cflag & PARENB) {
947		if (cflag & PARODD) {
948			info->cor1 |= CyPARITY_O;
949		} else {
950			info->cor1 |= CyPARITY_E;
951		}
952	} else {
953		info->cor1 |= CyPARITY_NONE;
954	}
955
956	/* CTS flow control flag */
957	if (cflag & CLOCAL)
958		info->flags &= ~ASYNC_CHECK_CD;
959	else
960		info->flags |= ASYNC_CHECK_CD;
961
962     /***********************************************
963	The hardware option, CyRtsAO, presents RTS when
964	the chip has characters to send.  Since most modems
965	use RTS as reverse (inbound) flow control, this
966	option is not used.  If inbound flow control is
967	necessary, DTR can be programmed to provide the
968	appropriate signals for use with a non-standard
969	cable.  Contact Marcio Saito for details.
970     ***********************************************/
971
972	channel = info->line;
973
974	local_irq_save(flags);
975	base_addr[CyCAR] = (u_char) channel;
976
977	/* CyCMR set once only in mvme167_init_serial() */
978	if (base_addr[CyLICR] != channel << 2)
979		base_addr[CyLICR] = channel << 2;
980	if (base_addr[CyLIVR] != 0x5c)
981		base_addr[CyLIVR] = 0x5c;
982
983	/* tx and rx baud rate */
984
985	if (base_addr[CyCOR1] != info->cor1)
986		need_init_chan = 1;
987	if (base_addr[CyTCOR] != info->tco)
988		base_addr[CyTCOR] = info->tco;
989	if (base_addr[CyTBPR] != info->tbpr)
990		base_addr[CyTBPR] = info->tbpr;
991	if (base_addr[CyRCOR] != info->rco)
992		base_addr[CyRCOR] = info->rco;
993	if (base_addr[CyRBPR] != info->rbpr)
994		base_addr[CyRBPR] = info->rbpr;
995
996	/* set line characteristics  according configuration */
997
998	if (base_addr[CySCHR1] != START_CHAR(info->tty))
999		base_addr[CySCHR1] = START_CHAR(info->tty);
1000	if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
1001		base_addr[CySCHR2] = STOP_CHAR(info->tty);
1002	if (base_addr[CySCRL] != START_CHAR(info->tty))
1003		base_addr[CySCRL] = START_CHAR(info->tty);
1004	if (base_addr[CySCRH] != START_CHAR(info->tty))
1005		base_addr[CySCRH] = START_CHAR(info->tty);
1006	if (base_addr[CyCOR1] != info->cor1)
1007		base_addr[CyCOR1] = info->cor1;
1008	if (base_addr[CyCOR2] != info->cor2)
1009		base_addr[CyCOR2] = info->cor2;
1010	if (base_addr[CyCOR3] != info->cor3)
1011		base_addr[CyCOR3] = info->cor3;
1012	if (base_addr[CyCOR4] != info->cor4)
1013		base_addr[CyCOR4] = info->cor4;
1014	if (base_addr[CyCOR5] != info->cor5)
1015		base_addr[CyCOR5] = info->cor5;
1016	if (base_addr[CyCOR6] != info->cor6)
1017		base_addr[CyCOR6] = info->cor6;
1018	if (base_addr[CyCOR7] != info->cor7)
1019		base_addr[CyCOR7] = info->cor7;
1020
1021	if (need_init_chan)
1022		write_cy_cmd(base_addr, CyINIT_CHAN);
1023
1024	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
1025
1026	/* 2ms default rx timeout */
1027	ti = info->default_timeout ? info->default_timeout : 0x02;
1028	if (base_addr[CyRTPRL] != ti)
1029		base_addr[CyRTPRL] = ti;
1030	if (base_addr[CyRTPRH] != 0)
1031		base_addr[CyRTPRH] = 0;
1032
1033	/* Set up RTS here also ????? RGH 141095 */
1034	if (i == 0) {		/* baud rate is zero, turn off line */
1035		if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
1036			base_addr[CyMSVR2] = 0;
1037#ifdef SERIAL_DEBUG_DTR
1038		printk("cyc: %d: dropping DTR\n", __LINE__);
1039		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1040		       base_addr[CyMSVR2]);
1041#endif
1042	} else {
1043		if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
1044			base_addr[CyMSVR2] = CyDTR;
1045#ifdef SERIAL_DEBUG_DTR
1046		printk("cyc: %d: raising DTR\n", __LINE__);
1047		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1048		       base_addr[CyMSVR2]);
1049#endif
1050	}
1051
1052	if (info->tty) {
1053		clear_bit(TTY_IO_ERROR, &info->tty->flags);
1054	}
1055
1056	local_irq_restore(flags);
1057
1058}				/* config_setup */
1059
1060static void cy_put_char(struct tty_struct *tty, unsigned char ch)
1061{
1062	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1063	unsigned long flags;
1064
1065#ifdef SERIAL_DEBUG_IO
1066	printk("cy_put_char %s(0x%02x)\n", tty->name, ch);
1067#endif
1068
1069	if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1070		return;
1071
1072	if (!info->xmit_buf)
1073		return;
1074
1075	local_irq_save(flags);
1076	if (info->xmit_cnt >= PAGE_SIZE - 1) {
1077		local_irq_restore(flags);
1078		return;
1079	}
1080
1081	info->xmit_buf[info->xmit_head++] = ch;
1082	info->xmit_head &= PAGE_SIZE - 1;
1083	info->xmit_cnt++;
1084	local_irq_restore(flags);
1085}				/* cy_put_char */
1086
1087static void cy_flush_chars(struct tty_struct *tty)
1088{
1089	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1090	unsigned long flags;
1091	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1092	int channel;
1093
1094#ifdef SERIAL_DEBUG_IO
1095	printk("cy_flush_chars %s\n", tty->name);	/* */
1096#endif
1097
1098	if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1099		return;
1100
1101	if (info->xmit_cnt <= 0 || tty->stopped
1102	    || tty->hw_stopped || !info->xmit_buf)
1103		return;
1104
1105	channel = info->line;
1106
1107	local_irq_save(flags);
1108	base_addr[CyCAR] = channel;
1109	base_addr[CyIER] |= CyTxMpty;
1110	local_irq_restore(flags);
1111}				/* cy_flush_chars */
1112
1113/* This routine gets called when tty_write has put something into
1114    the write_queue.  If the port is not already transmitting stuff,
1115    start it off by enabling interrupts.  The interrupt service
1116    routine will then ensure that the characters are sent.  If the
1117    port is already active, there is no need to kick it.
1118 */
1119static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1120{
1121	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1122	unsigned long flags;
1123	int c, total = 0;
1124
1125#ifdef SERIAL_DEBUG_IO
1126	printk("cy_write %s\n", tty->name);	/* */
1127#endif
1128
1129	if (serial_paranoia_check(info, tty->name, "cy_write")) {
1130		return 0;
1131	}
1132
1133	if (!info->xmit_buf) {
1134		return 0;
1135	}
1136
1137	while (1) {
1138		local_irq_save(flags);
1139		c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1140					  SERIAL_XMIT_SIZE - info->xmit_head));
1141		if (c <= 0) {
1142			local_irq_restore(flags);
1143			break;
1144		}
1145
1146		memcpy(info->xmit_buf + info->xmit_head, buf, c);
1147		info->xmit_head =
1148		    (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
1149		info->xmit_cnt += c;
1150		local_irq_restore(flags);
1151
1152		buf += c;
1153		count -= c;
1154		total += c;
1155	}
1156
1157	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1158		start_xmit(info);
1159	}
1160	return total;
1161}				/* cy_write */
1162
1163static int cy_write_room(struct tty_struct *tty)
1164{
1165	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1166	int ret;
1167
1168#ifdef SERIAL_DEBUG_IO
1169	printk("cy_write_room %s\n", tty->name);	/* */
1170#endif
1171
1172	if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1173		return 0;
1174	ret = PAGE_SIZE - info->xmit_cnt - 1;
1175	if (ret < 0)
1176		ret = 0;
1177	return ret;
1178}				/* cy_write_room */
1179
1180static int cy_chars_in_buffer(struct tty_struct *tty)
1181{
1182	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1183
1184#ifdef SERIAL_DEBUG_IO
1185	printk("cy_chars_in_buffer %s %d\n", tty->name, info->xmit_cnt);	/* */
1186#endif
1187
1188	if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1189		return 0;
1190
1191	return info->xmit_cnt;
1192}				/* cy_chars_in_buffer */
1193
1194static void cy_flush_buffer(struct tty_struct *tty)
1195{
1196	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1197	unsigned long flags;
1198
1199#ifdef SERIAL_DEBUG_IO
1200	printk("cy_flush_buffer %s\n", tty->name);	/* */
1201#endif
1202
1203	if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1204		return;
1205	local_irq_save(flags);
1206	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1207	local_irq_restore(flags);
1208	tty_wakeup(tty);
1209}				/* cy_flush_buffer */
1210
1211/* This routine is called by the upper-layer tty layer to signal
1212   that incoming characters should be throttled or that the
1213   throttle should be released.
1214 */
1215static void cy_throttle(struct tty_struct *tty)
1216{
1217	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1218	unsigned long flags;
1219	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1220	int channel;
1221
1222#ifdef SERIAL_DEBUG_THROTTLE
1223	char buf[64];
1224
1225	printk("throttle %s: %d....\n", tty_name(tty, buf),
1226	       tty->ldisc.chars_in_buffer(tty));
1227	printk("cy_throttle %s\n", tty->name);
1228#endif
1229
1230	if (serial_paranoia_check(info, tty->name, "cy_nthrottle")) {
1231		return;
1232	}
1233
1234	if (I_IXOFF(tty)) {
1235		info->x_char = STOP_CHAR(tty);
1236		/* Should use the "Send Special Character" feature!!! */
1237	}
1238
1239	channel = info->line;
1240
1241	local_irq_save(flags);
1242	base_addr[CyCAR] = (u_char) channel;
1243	base_addr[CyMSVR1] = 0;
1244	local_irq_restore(flags);
1245}				/* cy_throttle */
1246
1247static void cy_unthrottle(struct tty_struct *tty)
1248{
1249	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1250	unsigned long flags;
1251	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1252	int channel;
1253
1254#ifdef SERIAL_DEBUG_THROTTLE
1255	char buf[64];
1256
1257	printk("throttle %s: %d....\n", tty_name(tty, buf),
1258	       tty->ldisc.chars_in_buffer(tty));
1259	printk("cy_unthrottle %s\n", tty->name);
1260#endif
1261
1262	if (serial_paranoia_check(info, tty->name, "cy_nthrottle")) {
1263		return;
1264	}
1265
1266	if (I_IXOFF(tty)) {
1267		info->x_char = START_CHAR(tty);
1268		/* Should use the "Send Special Character" feature!!! */
1269	}
1270
1271	channel = info->line;
1272
1273	local_irq_save(flags);
1274	base_addr[CyCAR] = (u_char) channel;
1275	base_addr[CyMSVR1] = CyRTS;
1276	local_irq_restore(flags);
1277}				/* cy_unthrottle */
1278
1279static int
1280get_serial_info(struct cyclades_port *info,
1281		struct serial_struct __user * retinfo)
1282{
1283	struct serial_struct tmp;
1284
1285/* CP('g'); */
1286	if (!retinfo)
1287		return -EFAULT;
1288	memset(&tmp, 0, sizeof(tmp));
1289	tmp.type = info->type;
1290	tmp.line = info->line;
1291	tmp.port = info->line;
1292	tmp.irq = 0;
1293	tmp.flags = info->flags;
1294	tmp.baud_base = 0;	/*!!! */
1295	tmp.close_delay = info->close_delay;
1296	tmp.custom_divisor = 0;	/*!!! */
1297	tmp.hub6 = 0;		/*!!! */
1298	return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1299}				/* get_serial_info */
1300
1301static int
1302set_serial_info(struct cyclades_port *info,
1303		struct serial_struct __user * new_info)
1304{
1305	struct serial_struct new_serial;
1306	struct cyclades_port old_info;
1307
1308/* CP('s'); */
1309	if (!new_info)
1310		return -EFAULT;
1311	if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1312		return -EFAULT;
1313	old_info = *info;
1314
1315	if (!capable(CAP_SYS_ADMIN)) {
1316		if ((new_serial.close_delay != info->close_delay) ||
1317		    ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1318		     (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1319			return -EPERM;
1320		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1321			       (new_serial.flags & ASYNC_USR_MASK));
1322		goto check_and_exit;
1323	}
1324
1325	/*
1326	 * OK, past this point, all the error checking has been done.
1327	 * At this point, we start making changes.....
1328	 */
1329
1330	info->flags = ((info->flags & ~ASYNC_FLAGS) |
1331		       (new_serial.flags & ASYNC_FLAGS));
1332	info->close_delay = new_serial.close_delay;
1333
1334check_and_exit:
1335	if (info->flags & ASYNC_INITIALIZED) {
1336		config_setup(info);
1337		return 0;
1338	}
1339	return startup(info);
1340}				/* set_serial_info */
1341
1342static int cy_tiocmget(struct tty_struct *tty, struct file *file)
1343{
1344	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1345	int channel;
1346	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1347	unsigned long flags;
1348	unsigned char status;
1349
1350	channel = info->line;
1351
1352	local_irq_save(flags);
1353	base_addr[CyCAR] = (u_char) channel;
1354	status = base_addr[CyMSVR1] | base_addr[CyMSVR2];
1355	local_irq_restore(flags);
1356
1357	return ((status & CyRTS) ? TIOCM_RTS : 0)
1358	    | ((status & CyDTR) ? TIOCM_DTR : 0)
1359	    | ((status & CyDCD) ? TIOCM_CAR : 0)
1360	    | ((status & CyDSR) ? TIOCM_DSR : 0)
1361	    | ((status & CyCTS) ? TIOCM_CTS : 0);
1362}				/* cy_tiocmget */
1363
1364static int
1365cy_tiocmset(struct tty_struct *tty, struct file *file,
1366	    unsigned int set, unsigned int clear)
1367{
1368	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1369	int channel;
1370	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1371	unsigned long flags;
1372
1373	channel = info->line;
1374
1375	if (set & TIOCM_RTS) {
1376		local_irq_save(flags);
1377		base_addr[CyCAR] = (u_char) channel;
1378		base_addr[CyMSVR1] = CyRTS;
1379		local_irq_restore(flags);
1380	}
1381	if (set & TIOCM_DTR) {
1382		local_irq_save(flags);
1383		base_addr[CyCAR] = (u_char) channel;
1384/* CP('S');CP('2'); */
1385		base_addr[CyMSVR2] = CyDTR;
1386#ifdef SERIAL_DEBUG_DTR
1387		printk("cyc: %d: raising DTR\n", __LINE__);
1388		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1389		       base_addr[CyMSVR2]);
1390#endif
1391		local_irq_restore(flags);
1392	}
1393
1394	if (clear & TIOCM_RTS) {
1395		local_irq_save(flags);
1396		base_addr[CyCAR] = (u_char) channel;
1397		base_addr[CyMSVR1] = 0;
1398		local_irq_restore(flags);
1399	}
1400	if (clear & TIOCM_DTR) {
1401		local_irq_save(flags);
1402		base_addr[CyCAR] = (u_char) channel;
1403/* CP('C');CP('2'); */
1404		base_addr[CyMSVR2] = 0;
1405#ifdef SERIAL_DEBUG_DTR
1406		printk("cyc: %d: dropping DTR\n", __LINE__);
1407		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1408		       base_addr[CyMSVR2]);
1409#endif
1410		local_irq_restore(flags);
1411	}
1412
1413	return 0;
1414}				/* set_modem_info */
1415
1416static void send_break(struct cyclades_port *info, int duration)
1417{				/* Let the transmit ISR take care of this (since it
1418				   requires stuffing characters into the output stream).
1419				 */
1420	info->x_break = duration;
1421	if (!info->xmit_cnt) {
1422		start_xmit(info);
1423	}
1424}				/* send_break */
1425
1426static int
1427get_mon_info(struct cyclades_port *info, struct cyclades_monitor __user * mon)
1428{
1429
1430	if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
1431		return -EFAULT;
1432	info->mon.int_count = 0;
1433	info->mon.char_count = 0;
1434	info->mon.char_max = 0;
1435	info->mon.char_last = 0;
1436	return 0;
1437}
1438
1439static int set_threshold(struct cyclades_port *info, unsigned long __user * arg)
1440{
1441	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1442	unsigned long value;
1443	int channel;
1444
1445	if (get_user(value, arg))
1446		return -EFAULT;
1447
1448	channel = info->line;
1449	info->cor4 &= ~CyREC_FIFO;
1450	info->cor4 |= value & CyREC_FIFO;
1451	base_addr[CyCOR4] = info->cor4;
1452	return 0;
1453}
1454
1455static int
1456get_threshold(struct cyclades_port *info, unsigned long __user * value)
1457{
1458	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1459	int channel;
1460	unsigned long tmp;
1461
1462	channel = info->line;
1463
1464	tmp = base_addr[CyCOR4] & CyREC_FIFO;
1465	return put_user(tmp, value);
1466}
1467
1468static int
1469set_default_threshold(struct cyclades_port *info, unsigned long __user * arg)
1470{
1471	unsigned long value;
1472
1473	if (get_user(value, arg))
1474		return -EFAULT;
1475
1476	info->default_threshold = value & 0x0f;
1477	return 0;
1478}
1479
1480static int
1481get_default_threshold(struct cyclades_port *info, unsigned long __user * value)
1482{
1483	return put_user(info->default_threshold, value);
1484}
1485
1486static int set_timeout(struct cyclades_port *info, unsigned long __user * arg)
1487{
1488	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1489	int channel;
1490	unsigned long value;
1491
1492	if (get_user(value, arg))
1493		return -EFAULT;
1494
1495	channel = info->line;
1496
1497	base_addr[CyRTPRL] = value & 0xff;
1498	base_addr[CyRTPRH] = (value >> 8) & 0xff;
1499	return 0;
1500}
1501
1502static int get_timeout(struct cyclades_port *info, unsigned long __user * value)
1503{
1504	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1505	int channel;
1506	unsigned long tmp;
1507
1508	channel = info->line;
1509
1510	tmp = base_addr[CyRTPRL];
1511	return put_user(tmp, value);
1512}
1513
1514static int set_default_timeout(struct cyclades_port *info, unsigned long value)
1515{
1516	info->default_timeout = value & 0xff;
1517	return 0;
1518}
1519
1520static int
1521get_default_timeout(struct cyclades_port *info, unsigned long __user * value)
1522{
1523	return put_user(info->default_timeout, value);
1524}
1525
1526static int
1527cy_ioctl(struct tty_struct *tty, struct file *file,
1528	 unsigned int cmd, unsigned long arg)
1529{
1530	unsigned long val;
1531	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1532	int ret_val = 0;
1533	void __user *argp = (void __user *)arg;
1534
1535#ifdef SERIAL_DEBUG_OTHER
1536	printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg);	/* */
1537#endif
1538
1539	switch (cmd) {
1540	case CYGETMON:
1541		ret_val = get_mon_info(info, argp);
1542		break;
1543	case CYGETTHRESH:
1544		ret_val = get_threshold(info, argp);
1545		break;
1546	case CYSETTHRESH:
1547		ret_val = set_threshold(info, argp);
1548		break;
1549	case CYGETDEFTHRESH:
1550		ret_val = get_default_threshold(info, argp);
1551		break;
1552	case CYSETDEFTHRESH:
1553		ret_val = set_default_threshold(info, argp);
1554		break;
1555	case CYGETTIMEOUT:
1556		ret_val = get_timeout(info, argp);
1557		break;
1558	case CYSETTIMEOUT:
1559		ret_val = set_timeout(info, argp);
1560		break;
1561	case CYGETDEFTIMEOUT:
1562		ret_val = get_default_timeout(info, argp);
1563		break;
1564	case CYSETDEFTIMEOUT:
1565		ret_val = set_default_timeout(info, (unsigned long)arg);
1566		break;
1567	case TCSBRK:		/* SVID version: non-zero arg --> no break */
1568		ret_val = tty_check_change(tty);
1569		if (ret_val)
1570			break;
1571		tty_wait_until_sent(tty, 0);
1572		if (!arg)
1573			send_break(info, HZ / 4);	/* 1/4 second */
1574		break;
1575	case TCSBRKP:		/* support for POSIX tcsendbreak() */
1576		ret_val = tty_check_change(tty);
1577		if (ret_val)
1578			break;
1579		tty_wait_until_sent(tty, 0);
1580		send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1581		break;
1582
1583/* The following commands are incompletely implemented!!! */
1584	case TIOCGSOFTCAR:
1585		ret_val =
1586		    put_user(C_CLOCAL(tty) ? 1 : 0,
1587			     (unsigned long __user *)argp);
1588		break;
1589	case TIOCSSOFTCAR:
1590		ret_val = get_user(val, (unsigned long __user *)argp);
1591		if (ret_val)
1592			break;
1593		tty->termios->c_cflag =
1594		    ((tty->termios->c_cflag & ~CLOCAL) | (val ? CLOCAL : 0));
1595		break;
1596	case TIOCGSERIAL:
1597		ret_val = get_serial_info(info, argp);
1598		break;
1599	case TIOCSSERIAL:
1600		ret_val = set_serial_info(info, argp);
1601		break;
1602	default:
1603		ret_val = -ENOIOCTLCMD;
1604	}
1605
1606#ifdef SERIAL_DEBUG_OTHER
1607	printk("cy_ioctl done\n");
1608#endif
1609
1610	return ret_val;
1611}				/* cy_ioctl */
1612
1613static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1614{
1615	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1616
1617#ifdef SERIAL_DEBUG_OTHER
1618	printk("cy_set_termios %s\n", tty->name);
1619#endif
1620
1621	if (tty->termios->c_cflag == old_termios->c_cflag)
1622		return;
1623	config_setup(info);
1624
1625	if ((old_termios->c_cflag & CRTSCTS) &&
1626	    !(tty->termios->c_cflag & CRTSCTS)) {
1627		tty->stopped = 0;
1628		cy_start(tty);
1629	}
1630#ifdef tytso_patch_94Nov25_1726
1631	if (!(old_termios->c_cflag & CLOCAL) &&
1632	    (tty->termios->c_cflag & CLOCAL))
1633		wake_up_interruptible(&info->open_wait);
1634#endif
1635}				/* cy_set_termios */
1636
1637static void cy_close(struct tty_struct *tty, struct file *filp)
1638{
1639	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1640
1641/* CP('C'); */
1642#ifdef SERIAL_DEBUG_OTHER
1643	printk("cy_close %s\n", tty->name);
1644#endif
1645
1646	if (!info || serial_paranoia_check(info, tty->name, "cy_close")) {
1647		return;
1648	}
1649#ifdef SERIAL_DEBUG_OPEN
1650	printk("cy_close %s, count = %d\n", tty->name, info->count);
1651#endif
1652
1653	if ((tty->count == 1) && (info->count != 1)) {
1654		/*
1655		 * Uh, oh.  tty->count is 1, which means that the tty
1656		 * structure will be freed.  Info->count should always
1657		 * be one in these conditions.  If it's greater than
1658		 * one, we've got real problems, since it means the
1659		 * serial port won't be shutdown.
1660		 */
1661		printk("cy_close: bad serial port count; tty->count is 1, "
1662		       "info->count is %d\n", info->count);
1663		info->count = 1;
1664	}
1665#ifdef SERIAL_DEBUG_COUNT
1666	printk("cyc: %d: decrementing count to %d\n", __LINE__,
1667	       info->count - 1);
1668#endif
1669	if (--info->count < 0) {
1670		printk("cy_close: bad serial port count for ttys%d: %d\n",
1671		       info->line, info->count);
1672#ifdef SERIAL_DEBUG_COUNT
1673		printk("cyc: %d: setting count to 0\n", __LINE__);
1674#endif
1675		info->count = 0;
1676	}
1677	if (info->count)
1678		return;
1679	info->flags |= ASYNC_CLOSING;
1680	if (info->flags & ASYNC_INITIALIZED)
1681		tty_wait_until_sent(tty, 3000);	/* 30 seconds timeout */
1682	shutdown(info);
1683	if (tty->driver->flush_buffer)
1684		tty->driver->flush_buffer(tty);
1685	tty_ldisc_flush(tty);
1686	info->event = 0;
1687	info->tty = NULL;
1688	if (info->blocked_open) {
1689		if (info->close_delay) {
1690			msleep_interruptible(jiffies_to_msecs
1691					     (info->close_delay));
1692		}
1693		wake_up_interruptible(&info->open_wait);
1694	}
1695	info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1696	wake_up_interruptible(&info->close_wait);
1697
1698#ifdef SERIAL_DEBUG_OTHER
1699	printk("cy_close done\n");
1700#endif
1701}				/* cy_close */
1702
1703/*
1704 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
1705 */
1706void cy_hangup(struct tty_struct *tty)
1707{
1708	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1709
1710#ifdef SERIAL_DEBUG_OTHER
1711	printk("cy_hangup %s\n", tty->name);	/* */
1712#endif
1713
1714	if (serial_paranoia_check(info, tty->name, "cy_hangup"))
1715		return;
1716
1717	shutdown(info);
1718	info->flags &= ~ASYNC_NORMAL_ACTIVE;
1719	wake_up_interruptible(&info->open_wait);
1720}				/* cy_hangup */
1721
1722/*
1723 * ------------------------------------------------------------
1724 * cy_open() and friends
1725 * ------------------------------------------------------------
1726 */
1727
1728static int
1729block_til_ready(struct tty_struct *tty, struct file *filp,
1730		struct cyclades_port *info)
1731{
1732	DECLARE_WAITQUEUE(wait, current);
1733	unsigned long flags;
1734	int channel;
1735	int retval;
1736	volatile u_char *base_addr = (u_char *) BASE_ADDR;
1737
1738	/*
1739	 * If the device is in the middle of being closed, then block
1740	 * until it's done, and then try again.
1741	 */
1742	if (info->flags & ASYNC_CLOSING) {
1743		interruptible_sleep_on(&info->close_wait);
1744		if (info->flags & ASYNC_HUP_NOTIFY) {
1745			return -EAGAIN;
1746		} else {
1747			return -ERESTARTSYS;
1748		}
1749	}
1750
1751	/*
1752	 * If non-blocking mode is set, then make the check up front
1753	 * and then exit.
1754	 */
1755	if (filp->f_flags & O_NONBLOCK) {
1756		info->flags |= ASYNC_NORMAL_ACTIVE;
1757		return 0;
1758	}
1759
1760	/*
1761	 * Block waiting for the carrier detect and the line to become
1762	 * free (i.e., not in use by the callout).  While we are in
1763	 * this loop, info->count is dropped by one, so that
1764	 * cy_close() knows when to free things.  We restore it upon
1765	 * exit, either normal or abnormal.
1766	 */
1767	retval = 0;
1768	add_wait_queue(&info->open_wait, &wait);
1769#ifdef SERIAL_DEBUG_OPEN
1770	printk("block_til_ready before block: %s, count = %d\n",
1771	       tty->name, info->count);
1772	/**/
1773#endif
1774	    info->count--;
1775#ifdef SERIAL_DEBUG_COUNT
1776	printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count);
1777#endif
1778	info->blocked_open++;
1779
1780	channel = info->line;
1781
1782	while (1) {
1783		local_irq_save(flags);
1784		base_addr[CyCAR] = (u_char) channel;
1785		base_addr[CyMSVR1] = CyRTS;
1786/* CP('S');CP('4'); */
1787		base_addr[CyMSVR2] = CyDTR;
1788#ifdef SERIAL_DEBUG_DTR
1789		printk("cyc: %d: raising DTR\n", __LINE__);
1790		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1791		       base_addr[CyMSVR2]);
1792#endif
1793		local_irq_restore(flags);
1794		set_current_state(TASK_INTERRUPTIBLE);
1795		if (tty_hung_up_p(filp)
1796		    || !(info->flags & ASYNC_INITIALIZED)) {
1797			if (info->flags & ASYNC_HUP_NOTIFY) {
1798				retval = -EAGAIN;
1799			} else {
1800				retval = -ERESTARTSYS;
1801			}
1802			break;
1803		}
1804		local_irq_save(flags);
1805		base_addr[CyCAR] = (u_char) channel;
1806/* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1] & CyDCD) ); */
1807		if (!(info->flags & ASYNC_CLOSING)
1808		    && (C_CLOCAL(tty)
1809			|| (base_addr[CyMSVR1] & CyDCD))) {
1810			local_irq_restore(flags);
1811			break;
1812		}
1813		local_irq_restore(flags);
1814		if (signal_pending(current)) {
1815			retval = -ERESTARTSYS;
1816			break;
1817		}
1818#ifdef SERIAL_DEBUG_OPEN
1819		printk("block_til_ready blocking: %s, count = %d\n",
1820		       tty->name, info->count);
1821		/**/
1822#endif
1823		    schedule();
1824	}
1825	__set_current_state(TASK_RUNNING);
1826	remove_wait_queue(&info->open_wait, &wait);
1827	if (!tty_hung_up_p(filp)) {
1828		info->count++;
1829#ifdef SERIAL_DEBUG_COUNT
1830		printk("cyc: %d: incrementing count to %d\n", __LINE__,
1831		       info->count);
1832#endif
1833	}
1834	info->blocked_open--;
1835#ifdef SERIAL_DEBUG_OPEN
1836	printk("block_til_ready after blocking: %s, count = %d\n",
1837	       tty->name, info->count);
1838	/**/
1839#endif
1840	    if (retval)
1841		return retval;
1842	info->flags |= ASYNC_NORMAL_ACTIVE;
1843	return 0;
1844}				/* block_til_ready */
1845
1846/*
1847 * This routine is called whenever a serial port is opened.  It
1848 * performs the serial-specific initialization for the tty structure.
1849 */
1850int cy_open(struct tty_struct *tty, struct file *filp)
1851{
1852	struct cyclades_port *info;
1853	int retval, line;
1854
1855/* CP('O'); */
1856	line = tty->index;
1857	if ((line < 0) || (NR_PORTS <= line)) {
1858		return -ENODEV;
1859	}
1860	info = &cy_port[line];
1861	if (info->line < 0) {
1862		return -ENODEV;
1863	}
1864#ifdef SERIAL_DEBUG_OTHER
1865	printk("cy_open %s\n", tty->name);	/* */
1866#endif
1867	if (serial_paranoia_check(info, tty->name, "cy_open")) {
1868		return -ENODEV;
1869	}
1870#ifdef SERIAL_DEBUG_OPEN
1871	printk("cy_open %s, count = %d\n", tty->name, info->count);
1872	/**/
1873#endif
1874	    info->count++;
1875#ifdef SERIAL_DEBUG_COUNT
1876	printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
1877#endif
1878	tty->driver_data = info;
1879	info->tty = tty;
1880
1881	/*
1882	 * Start up serial port
1883	 */
1884	retval = startup(info);
1885	if (retval) {
1886		return retval;
1887	}
1888
1889	retval = block_til_ready(tty, filp, info);
1890	if (retval) {
1891#ifdef SERIAL_DEBUG_OPEN
1892		printk("cy_open returning after block_til_ready with %d\n",
1893		       retval);
1894#endif
1895		return retval;
1896	}
1897#ifdef SERIAL_DEBUG_OPEN
1898	printk("cy_open done\n");
1899	/**/
1900#endif
1901	    return 0;
1902}				/* cy_open */
1903
1904/*
1905 * ---------------------------------------------------------------------
1906 * serial167_init() and friends
1907 *
1908 * serial167_init() is called at boot-time to initialize the serial driver.
1909 * ---------------------------------------------------------------------
1910 */
1911
1912/*
1913 * This routine prints out the appropriate serial driver version
1914 * number, and identifies which options were configured into this
1915 * driver.
1916 */
1917static void show_version(void)
1918{
1919	printk("MVME166/167 cd2401 driver\n");
1920}				/* show_version */
1921
1922/* initialize chips on card -- return number of valid
1923   chips (which is number of ports/4) */
1924
1925/*
1926 * This initialises the hardware to a reasonable state.  It should
1927 * probe the chip first so as to copy 166-Bug setup as a default for
1928 * port 0.  It initialises CMR to CyASYNC; that is never done again, so
1929 * as to limit the number of CyINIT_CHAN commands in normal running.
1930 *
1931 * ... I wonder what I should do if this fails ...
1932 */
1933
1934void mvme167_serial_console_setup(int cflag)
1935{
1936	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1937	int ch;
1938	u_char spd;
1939	u_char rcor, rbpr, badspeed = 0;
1940	unsigned long flags;
1941
1942	local_irq_save(flags);
1943
1944	/*
1945	 * First probe channel zero of the chip, to see what speed has
1946	 * been selected.
1947	 */
1948
1949	base_addr[CyCAR] = 0;
1950
1951	rcor = base_addr[CyRCOR] << 5;
1952	rbpr = base_addr[CyRBPR];
1953
1954	for (spd = 0; spd < sizeof(baud_bpr); spd++)
1955		if (rbpr == baud_bpr[spd] && rcor == baud_co[spd])
1956			break;
1957	if (spd >= sizeof(baud_bpr)) {
1958		spd = 14;	/* 19200 */
1959		badspeed = 1;	/* Failed to identify speed */
1960	}
1961	initial_console_speed = spd;
1962
1963	/* OK, we have chosen a speed, now reset and reinitialise */
1964
1965	my_udelay(20000L);	/* Allow time for any active o/p to complete */
1966	if (base_addr[CyCCR] != 0x00) {
1967		local_irq_restore(flags);
1968		/* printk(" chip is never idle (CCR != 0)\n"); */
1969		return;
1970	}
1971
1972	base_addr[CyCCR] = CyCHIP_RESET;	/* Reset the chip */
1973	my_udelay(1000L);
1974
1975	if (base_addr[CyGFRCR] == 0x00) {
1976		local_irq_restore(flags);
1977		/* printk(" chip is not responding (GFRCR stayed 0)\n"); */
1978		return;
1979	}
1980
1981	/*
1982	 * System clock is 20Mhz, divided by 2048, so divide by 10 for a 1.0ms
1983	 * tick
1984	 */
1985
1986	base_addr[CyTPR] = 10;
1987
1988	base_addr[CyPILR1] = 0x01;	/* Interrupt level for modem change */
1989	base_addr[CyPILR2] = 0x02;	/* Interrupt level for tx ints */
1990	base_addr[CyPILR3] = 0x03;	/* Interrupt level for rx ints */
1991
1992	/*
1993	 * Attempt to set up all channels to something reasonable, and
1994	 * bang out a INIT_CHAN command.  We should then be able to limit
1995	 * the ammount of fiddling we have to do in normal running.
1996	 */
1997
1998	for (ch = 3; ch >= 0; ch--) {
1999		base_addr[CyCAR] = (u_char) ch;
2000		base_addr[CyIER] = 0;
2001		base_addr[CyCMR] = CyASYNC;
2002		base_addr[CyLICR] = (u_char) ch << 2;
2003		base_addr[CyLIVR] = 0x5c;
2004		base_addr[CyTCOR] = baud_co[spd];
2005		base_addr[CyTBPR] = baud_bpr[spd];
2006		base_addr[CyRCOR] = baud_co[spd] >> 5;
2007		base_addr[CyRBPR] = baud_bpr[spd];
2008		base_addr[CySCHR1] = 'Q' & 0x1f;
2009		base_addr[CySCHR2] = 'X' & 0x1f;
2010		base_addr[CySCRL] = 0;
2011		base_addr[CySCRH] = 0;
2012		base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
2013		base_addr[CyCOR2] = 0;
2014		base_addr[CyCOR3] = Cy_1_STOP;
2015		base_addr[CyCOR4] = baud_cor4[spd];
2016		base_addr[CyCOR5] = 0;
2017		base_addr[CyCOR6] = 0;
2018		base_addr[CyCOR7] = 0;
2019		base_addr[CyRTPRL] = 2;
2020		base_addr[CyRTPRH] = 0;
2021		base_addr[CyMSVR1] = 0;
2022		base_addr[CyMSVR2] = 0;
2023		write_cy_cmd(base_addr, CyINIT_CHAN | CyDIS_RCVR | CyDIS_XMTR);
2024	}
2025
2026	/*
2027	 * Now do specials for channel zero....
2028	 */
2029
2030	base_addr[CyMSVR1] = CyRTS;
2031	base_addr[CyMSVR2] = CyDTR;
2032	base_addr[CyIER] = CyRxData;
2033	write_cy_cmd(base_addr, CyENB_RCVR | CyENB_XMTR);
2034
2035	local_irq_restore(flags);
2036
2037	my_udelay(20000L);	/* Let it all settle down */
2038
2039	printk("CD2401 initialised,  chip is rev 0x%02x\n", base_addr[CyGFRCR]);
2040	if (badspeed)
2041		printk
2042		    ("  WARNING:  Failed to identify line speed, rcor=%02x,rbpr=%02x\n",
2043		     rcor >> 5, rbpr);
2044}				/* serial_console_init */
2045
2046static const struct tty_operations cy_ops = {
2047	.open = cy_open,
2048	.close = cy_close,
2049	.write = cy_write,
2050	.put_char = cy_put_char,
2051	.flush_chars = cy_flush_chars,
2052	.write_room = cy_write_room,
2053	.chars_in_buffer = cy_chars_in_buffer,
2054	.flush_buffer = cy_flush_buffer,
2055	.ioctl = cy_ioctl,
2056	.throttle = cy_throttle,
2057	.unthrottle = cy_unthrottle,
2058	.set_termios = cy_set_termios,
2059	.stop = cy_stop,
2060	.start = cy_start,
2061	.hangup = cy_hangup,
2062	.tiocmget = cy_tiocmget,
2063	.tiocmset = cy_tiocmset,
2064};
2065
2066/* The serial driver boot-time initialization code!
2067    Hardware I/O ports are mapped to character special devices on a
2068    first found, first allocated manner.  That is, this code searches
2069    for Cyclom cards in the system.  As each is found, it is probed
2070    to discover how many chips (and thus how many ports) are present.
2071    These ports are mapped to the tty ports 64 and upward in monotonic
2072    fashion.  If an 8-port card is replaced with a 16-port card, the
2073    port mapping on a following card will shift.
2074
2075    This approach is different from what is used in the other serial
2076    device driver because the Cyclom is more properly a multiplexer,
2077    not just an aggregation of serial ports on one card.
2078
2079    If there are more cards with more ports than have been statically
2080    allocated above, a warning is printed and the extra ports are ignored.
2081 */
2082static int __init serial167_init(void)
2083{
2084	struct cyclades_port *info;
2085	int ret = 0;
2086	int good_ports = 0;
2087	int port_num = 0;
2088	int index;
2089	int DefSpeed;
2090#ifdef notyet
2091	struct sigaction sa;
2092#endif
2093
2094	if (!(mvme16x_config & MVME16x_CONFIG_GOT_CD2401))
2095		return 0;
2096
2097	cy_serial_driver = alloc_tty_driver(NR_PORTS);
2098	if (!cy_serial_driver)
2099		return -ENOMEM;
2100
2101
2102	show_version();
2103
2104	/* Has "console=0,9600n8" been used in bootinfo to change speed? */
2105	if (serial_console_cflag)
2106		DefSpeed = serial_console_cflag & 0017;
2107	else {
2108		DefSpeed = initial_console_speed;
2109		serial_console_info = &cy_port[0];
2110		serial_console_cflag = DefSpeed | CS8;
2111	}
2112
2113	/* Initialize the tty_driver structure */
2114
2115	cy_serial_driver->owner = THIS_MODULE;
2116	cy_serial_driver->name = "ttyS";
2117	cy_serial_driver->major = TTY_MAJOR;
2118	cy_serial_driver->minor_start = 64;
2119	cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2120	cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
2121	cy_serial_driver->init_termios = tty_std_termios;
2122	cy_serial_driver->init_termios.c_cflag =
2123	    B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2124	cy_serial_driver->flags = TTY_DRIVER_REAL_RAW;
2125	tty_set_operations(cy_serial_driver, &cy_ops);
2126
2127	ret = tty_register_driver(cy_serial_driver);
2128	if (ret) {
2129		printk(KERN_ERR "Couldn't register MVME166/7 serial driver\n");
2130		put_tty_driver(cy_serial_driver);
2131		return ret;
2132	}
2133
2134	port_num = 0;
2135	info = cy_port;
2136	for (index = 0; index < 1; index++) {
2137
2138		good_ports = 4;
2139
2140		if (port_num < NR_PORTS) {
2141			while (good_ports-- && port_num < NR_PORTS) {
2142		/*** initialize port ***/
2143				info->magic = CYCLADES_MAGIC;
2144				info->type = PORT_CIRRUS;
2145				info->card = index;
2146				info->line = port_num;
2147				info->flags = STD_COM_FLAGS;
2148				info->tty = NULL;
2149				info->xmit_fifo_size = 12;
2150				info->cor1 = CyPARITY_NONE | Cy_8_BITS;
2151				info->cor2 = CyETC;
2152				info->cor3 = Cy_1_STOP;
2153				info->cor4 = 0x08;	/* _very_ small receive threshold */
2154				info->cor5 = 0;
2155				info->cor6 = 0;
2156				info->cor7 = 0;
2157				info->tbpr = baud_bpr[DefSpeed];	/* Tx BPR */
2158				info->tco = baud_co[DefSpeed];	/* Tx CO */
2159				info->rbpr = baud_bpr[DefSpeed];	/* Rx BPR */
2160				info->rco = baud_co[DefSpeed] >> 5;	/* Rx CO */
2161				info->close_delay = 0;
2162				info->x_char = 0;
2163				info->event = 0;
2164				info->count = 0;
2165#ifdef SERIAL_DEBUG_COUNT
2166				printk("cyc: %d: setting count to 0\n",
2167				       __LINE__);
2168#endif
2169				info->blocked_open = 0;
2170				info->default_threshold = 0;
2171				info->default_timeout = 0;
2172				INIT_WORK(&info->tqueue, do_softint);
2173				init_waitqueue_head(&info->open_wait);
2174				init_waitqueue_head(&info->close_wait);
2175				/* info->session */
2176				/* info->pgrp */
2177/*** !!!!!!!! this may expose new bugs !!!!!!!!! *********/
2178				info->read_status_mask =
2179				    CyTIMEOUT | CySPECHAR | CyBREAK | CyPARITY |
2180				    CyFRAME | CyOVERRUN;
2181				/* info->timeout */
2182
2183				printk("ttyS%d ", info->line);
2184				port_num++;
2185				info++;
2186				if (!(port_num & 7)) {
2187					printk("\n               ");
2188				}
2189			}
2190		}
2191		printk("\n");
2192	}
2193	while (port_num < NR_PORTS) {
2194		info->line = -1;
2195		port_num++;
2196		info++;
2197	}
2198#ifdef CONFIG_REMOTE_DEBUG
2199	debug_setup();
2200#endif
2201	ret = request_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt, 0,
2202			  "cd2401_errors", cd2401_rxerr_interrupt);
2203	if (ret) {
2204		printk(KERN_ERR "Could't get cd2401_errors IRQ");
2205		goto cleanup_serial_driver;
2206	}
2207
2208	ret = request_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt, 0,
2209			  "cd2401_modem", cd2401_modem_interrupt);
2210	if (ret) {
2211		printk(KERN_ERR "Could't get cd2401_modem IRQ");
2212		goto cleanup_irq_cd2401_errors;
2213	}
2214
2215	ret = request_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt, 0,
2216			  "cd2401_txints", cd2401_tx_interrupt);
2217	if (ret) {
2218		printk(KERN_ERR "Could't get cd2401_txints IRQ");
2219		goto cleanup_irq_cd2401_modem;
2220	}
2221
2222	ret = request_irq(MVME167_IRQ_SER_RX, cd2401_rx_interrupt, 0,
2223			  "cd2401_rxints", cd2401_rx_interrupt);
2224	if (ret) {
2225		printk(KERN_ERR "Could't get cd2401_rxints IRQ");
2226		goto cleanup_irq_cd2401_txints;
2227	}
2228
2229	/* Now we have registered the interrupt handlers, allow the interrupts */
2230
2231	pcc2chip[PccSCCMICR] = 0x15;	/* Serial ints are level 5 */
2232	pcc2chip[PccSCCTICR] = 0x15;
2233	pcc2chip[PccSCCRICR] = 0x15;
2234
2235	pcc2chip[PccIMLR] = 3;	/* Allow PCC2 ints above 3!? */
2236
2237	return 0;
2238cleanup_irq_cd2401_txints:
2239	free_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt);
2240cleanup_irq_cd2401_modem:
2241	free_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt);
2242cleanup_irq_cd2401_errors:
2243	free_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt);
2244cleanup_serial_driver:
2245	if (tty_unregister_driver(cy_serial_driver))
2246		printk(KERN_ERR
2247		       "Couldn't unregister MVME166/7 serial driver\n");
2248	put_tty_driver(cy_serial_driver);
2249	return ret;
2250}				/* serial167_init */
2251
2252module_init(serial167_init);
2253
2254#ifdef CYCLOM_SHOW_STATUS
2255static void show_status(int line_num)
2256{
2257	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2258	int channel;
2259	struct cyclades_port *info;
2260	unsigned long flags;
2261
2262	info = &cy_port[line_num];
2263	channel = info->line;
2264	printk("  channel %d\n", channel);
2265	/**/ printk(" cy_port\n");
2266	printk("  card line flags = %d %d %x\n",
2267	       info->card, info->line, info->flags);
2268	printk
2269	    ("  *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
2270	     (long)info->tty, info->read_status_mask, info->timeout,
2271	     info->xmit_fifo_size);
2272	printk("  cor1,cor2,cor3,cor4,cor5,cor6,cor7 = %x %x %x %x %x %x %x\n",
2273	       info->cor1, info->cor2, info->cor3, info->cor4, info->cor5,
2274	       info->cor6, info->cor7);
2275	printk("  tbpr,tco,rbpr,rco = %d %d %d %d\n", info->tbpr, info->tco,
2276	       info->rbpr, info->rco);
2277	printk("  close_delay event count = %d %d %d\n", info->close_delay,
2278	       info->event, info->count);
2279	printk("  x_char blocked_open = %x %x\n", info->x_char,
2280	       info->blocked_open);
2281	printk("  open_wait = %lx %lx %lx\n", (long)info->open_wait);
2282
2283	local_irq_save(flags);
2284
2285/* Global Registers */
2286
2287	printk(" CyGFRCR %x\n", base_addr[CyGFRCR]);
2288	printk(" CyCAR %x\n", base_addr[CyCAR]);
2289	printk(" CyRISR %x\n", base_addr[CyRISR]);
2290	printk(" CyTISR %x\n", base_addr[CyTISR]);
2291	printk(" CyMISR %x\n", base_addr[CyMISR]);
2292	printk(" CyRIR %x\n", base_addr[CyRIR]);
2293	printk(" CyTIR %x\n", base_addr[CyTIR]);
2294	printk(" CyMIR %x\n", base_addr[CyMIR]);
2295	printk(" CyTPR %x\n", base_addr[CyTPR]);
2296
2297	base_addr[CyCAR] = (u_char) channel;
2298
2299/* Virtual Registers */
2300
2301
2302/* Channel Registers */
2303
2304	printk(" CyCCR %x\n", base_addr[CyCCR]);
2305	printk(" CyIER %x\n", base_addr[CyIER]);
2306	printk(" CyCOR1 %x\n", base_addr[CyCOR1]);
2307	printk(" CyCOR2 %x\n", base_addr[CyCOR2]);
2308	printk(" CyCOR3 %x\n", base_addr[CyCOR3]);
2309	printk(" CyCOR4 %x\n", base_addr[CyCOR4]);
2310	printk(" CyCOR5 %x\n", base_addr[CyCOR5]);
2311	printk(" CySCHR1 %x\n", base_addr[CySCHR1]);
2312	printk(" CySCHR2 %x\n", base_addr[CySCHR2]);
2313	printk(" CyRTPRL %x\n", base_addr[CyRTPRL]);
2314	printk(" CyRTPRH %x\n", base_addr[CyRTPRH]);
2315	printk(" CyMSVR1 %x\n", base_addr[CyMSVR1]);
2316	printk(" CyMSVR2 %x\n", base_addr[CyMSVR2]);
2317	printk(" CyRBPR %x\n", base_addr[CyRBPR]);
2318	printk(" CyRCOR %x\n", base_addr[CyRCOR]);
2319	printk(" CyTBPR %x\n", base_addr[CyTBPR]);
2320	printk(" CyTCOR %x\n", base_addr[CyTCOR]);
2321
2322	local_irq_restore(flags);
2323}				/* show_status */
2324#endif
2325
2326
2327/*
2328 * The following is probably out of date for 2.1.x serial console stuff.
2329 *
2330 * The console is registered early on from arch/m68k/kernel/setup.c, and
2331 * it therefore relies on the chip being setup correctly by 166-Bug.  This
2332 * seems reasonable, as the serial port has been used to invoke the system
2333 * boot.  It also means that this function must not rely on any data
2334 * initialisation performed by serial167_init() etc.
2335 *
2336 * Of course, once the console has been registered, we had better ensure
2337 * that serial167_init() doesn't leave the chip non-functional.
2338 *
2339 * The console must be locked when we get here.
2340 */
2341
2342void serial167_console_write(struct console *co, const char *str,
2343			     unsigned count)
2344{
2345	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2346	unsigned long flags;
2347	volatile u_char sink;
2348	u_char ier;
2349	int port;
2350	u_char do_lf = 0;
2351	int i = 0;
2352
2353	local_irq_save(flags);
2354
2355	/* Ensure transmitter is enabled! */
2356
2357	port = 0;
2358	base_addr[CyCAR] = (u_char) port;
2359	while (base_addr[CyCCR])
2360		;
2361	base_addr[CyCCR] = CyENB_XMTR;
2362
2363	ier = base_addr[CyIER];
2364	base_addr[CyIER] = CyTxMpty;
2365
2366	while (1) {
2367		if (pcc2chip[PccSCCTICR] & 0x20) {
2368			/* We have a Tx int. Acknowledge it */
2369			sink = pcc2chip[PccTPIACKR];
2370			if ((base_addr[CyLICR] >> 2) == port) {
2371				if (i == count) {
2372					/* Last char of string is now output */
2373					base_addr[CyTEOIR] = CyNOTRANS;
2374					break;
2375				}
2376				if (do_lf) {
2377					base_addr[CyTDR] = '\n';
2378					str++;
2379					i++;
2380					do_lf = 0;
2381				} else if (*str == '\n') {
2382					base_addr[CyTDR] = '\r';
2383					do_lf = 1;
2384				} else {
2385					base_addr[CyTDR] = *str++;
2386					i++;
2387				}
2388				base_addr[CyTEOIR] = 0;
2389			} else
2390				base_addr[CyTEOIR] = CyNOTRANS;
2391		}
2392	}
2393
2394	base_addr[CyIER] = ier;
2395
2396	local_irq_restore(flags);
2397}
2398
2399static struct tty_driver *serial167_console_device(struct console *c,
2400						   int *index)
2401{
2402	*index = c->index;
2403	return cy_serial_driver;
2404}
2405
2406static int __init serial167_console_setup(struct console *co, char *options)
2407{
2408	return 0;
2409}
2410
2411static struct console sercons = {
2412	.name = "ttyS",
2413	.write = serial167_console_write,
2414	.device = serial167_console_device,
2415	.setup = serial167_console_setup,
2416	.flags = CON_PRINTBUFFER,
2417	.index = -1,
2418};
2419
2420static int __init serial167_console_init(void)
2421{
2422	if (vme_brdtype == VME_TYPE_MVME166 ||
2423	    vme_brdtype == VME_TYPE_MVME167 ||
2424	    vme_brdtype == VME_TYPE_MVME177) {
2425		mvme167_serial_console_setup(0);
2426		register_console(&sercons);
2427	}
2428	return 0;
2429}
2430
2431console_initcall(serial167_console_init);
2432
2433#ifdef CONFIG_REMOTE_DEBUG
2434void putDebugChar(int c)
2435{
2436	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2437	unsigned long flags;
2438	volatile u_char sink;
2439	u_char ier;
2440	int port;
2441
2442	local_irq_save(flags);
2443
2444	/* Ensure transmitter is enabled! */
2445
2446	port = DEBUG_PORT;
2447	base_addr[CyCAR] = (u_char) port;
2448	while (base_addr[CyCCR])
2449		;
2450	base_addr[CyCCR] = CyENB_XMTR;
2451
2452	ier = base_addr[CyIER];
2453	base_addr[CyIER] = CyTxMpty;
2454
2455	while (1) {
2456		if (pcc2chip[PccSCCTICR] & 0x20) {
2457			/* We have a Tx int. Acknowledge it */
2458			sink = pcc2chip[PccTPIACKR];
2459			if ((base_addr[CyLICR] >> 2) == port) {
2460				base_addr[CyTDR] = c;
2461				base_addr[CyTEOIR] = 0;
2462				break;
2463			} else
2464				base_addr[CyTEOIR] = CyNOTRANS;
2465		}
2466	}
2467
2468	base_addr[CyIER] = ier;
2469
2470	local_irq_restore(flags);
2471}
2472
2473int getDebugChar()
2474{
2475	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2476	unsigned long flags;
2477	volatile u_char sink;
2478	u_char ier;
2479	int port;
2480	int i, c;
2481
2482	i = debugiq.out;
2483	if (i != debugiq.in) {
2484		c = debugiq.buf[i];
2485		if (++i == DEBUG_LEN)
2486			i = 0;
2487		debugiq.out = i;
2488		return c;
2489	}
2490	/* OK, nothing in queue, wait in poll loop */
2491
2492	local_irq_save(flags);
2493
2494	/* Ensure receiver is enabled! */
2495
2496	port = DEBUG_PORT;
2497	base_addr[CyCAR] = (u_char) port;
2498	ier = base_addr[CyIER];
2499	base_addr[CyIER] = CyRxData;
2500
2501	while (1) {
2502		if (pcc2chip[PccSCCRICR] & 0x20) {
2503			/* We have a Rx int. Acknowledge it */
2504			sink = pcc2chip[PccRPIACKR];
2505			if ((base_addr[CyLICR] >> 2) == port) {
2506				int cnt = base_addr[CyRFOC];
2507				while (cnt-- > 0) {
2508					c = base_addr[CyRDR];
2509					if (c == 0)
2510						printk
2511						    ("!! debug char is null (cnt=%d) !!",
2512						     cnt);
2513					else
2514						queueDebugChar(c);
2515				}
2516				base_addr[CyREOIR] = 0;
2517				i = debugiq.out;
2518				if (i == debugiq.in)
2519					panic("Debug input queue empty!");
2520				c = debugiq.buf[i];
2521				if (++i == DEBUG_LEN)
2522					i = 0;
2523				debugiq.out = i;
2524				break;
2525			} else
2526				base_addr[CyREOIR] = CyNOTRANS;
2527		}
2528	}
2529
2530	base_addr[CyIER] = ier;
2531
2532	local_irq_restore(flags);
2533
2534	return (c);
2535}
2536
2537void queueDebugChar(int c)
2538{
2539	int i;
2540
2541	i = debugiq.in;
2542	debugiq.buf[i] = c;
2543	if (++i == DEBUG_LEN)
2544		i = 0;
2545	if (i != debugiq.out)
2546		debugiq.in = i;
2547}
2548
2549static void debug_setup()
2550{
2551	unsigned long flags;
2552	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2553	int i, cflag;
2554
2555	cflag = B19200;
2556
2557	local_irq_save(flags);
2558
2559	for (i = 0; i < 4; i++) {
2560		base_addr[CyCAR] = i;
2561		base_addr[CyLICR] = i << 2;
2562	}
2563
2564	debugiq.in = debugiq.out = 0;
2565
2566	base_addr[CyCAR] = DEBUG_PORT;
2567
2568	/* baud rate */
2569	i = cflag & CBAUD;
2570
2571	base_addr[CyIER] = 0;
2572
2573	base_addr[CyCMR] = CyASYNC;
2574	base_addr[CyLICR] = DEBUG_PORT << 2;
2575	base_addr[CyLIVR] = 0x5c;
2576
2577	/* tx and rx baud rate */
2578
2579	base_addr[CyTCOR] = baud_co[i];
2580	base_addr[CyTBPR] = baud_bpr[i];
2581	base_addr[CyRCOR] = baud_co[i] >> 5;
2582	base_addr[CyRBPR] = baud_bpr[i];
2583
2584	/* set line characteristics  according configuration */
2585
2586	base_addr[CySCHR1] = 0;
2587	base_addr[CySCHR2] = 0;
2588	base_addr[CySCRL] = 0;
2589	base_addr[CySCRH] = 0;
2590	base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
2591	base_addr[CyCOR2] = 0;
2592	base_addr[CyCOR3] = Cy_1_STOP;
2593	base_addr[CyCOR4] = baud_cor4[i];
2594	base_addr[CyCOR5] = 0;
2595	base_addr[CyCOR6] = 0;
2596	base_addr[CyCOR7] = 0;
2597
2598	write_cy_cmd(base_addr, CyINIT_CHAN);
2599	write_cy_cmd(base_addr, CyENB_RCVR);
2600
2601	base_addr[CyCAR] = DEBUG_PORT;	/* !!! Is this needed? */
2602
2603	base_addr[CyRTPRL] = 2;
2604	base_addr[CyRTPRH] = 0;
2605
2606	base_addr[CyMSVR1] = CyRTS;
2607	base_addr[CyMSVR2] = CyDTR;
2608
2609	base_addr[CyIER] = CyRxData;
2610
2611	local_irq_restore(flags);
2612
2613}				/* debug_setup */
2614
2615#endif
2616
2617MODULE_LICENSE("GPL");
2618