serial167.c 67.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * linux/drivers/char/serial167.c
 *
 * Driver for MVME166/7 board serial ports, which are via a CD2401.
 * Based very much on cyclades.c.
 *
 * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
 *
 * ==============================================================
 *
 * static char rcsid[] =
 * "$Revision: 1.36.1.4 $$Date: 1995/03/29 06:14:14 $";
 *
 *  linux/kernel/cyclades.c
 *
 * Maintained by Marcio Saito (cyclades@netcom.com) and
 * Randolph Bentson (bentson@grieg.seaslug.org)
 *
 * Much of the design and some of the code came from serial.c
 * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
 * and then fixed as suggested by Michael K. Johnson 12/12/92.
 *
 * This version does not support shared irq's.
 *
 * $Log: cyclades.c,v $
 * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
 * disambiguate between Cyclom-16Y and Cyclom-32Ye;
 *
 * Changes:
 *
 * 200 lines of changes record removed - RGH 11-10-95, starting work on
 * converting this to drive serial ports on mvme166 (cd2401).
 *
 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
 * - get rid of verify_area
 * - use get_user to access memory from userspace in set_threshold,
 *   set_default_threshold and set_timeout
 * - don't use the panic function in serial167_init
 * - do resource release on failure on serial167_init
 * - include missing restore_flags in mvme167_serial_console_setup
 *
 * Kars de Jong <jongk@linux-m68k.org> - 2004/09/06
 * - replace bottom half handler with task queue handler
 */

#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/tty.h>
#include <linux/interrupt.h>
#include <linux/serial.h>
#include <linux/serialP.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
55
#include <linux/smp_lock.h>
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62 63 64 65
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/serial167.h>
#include <linux/delay.h>
#include <linux/major.h>
#include <linux/mm.h>
#include <linux/console.h>
#include <linux/module.h>
#include <linux/bitops.h>
66
#include <linux/tty_flip.h>
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#include <asm/system.h>
#include <asm/io.h>
#include <asm/mvme16xhw.h>
#include <asm/bootinfo.h>
#include <asm/setup.h>

#include <linux/types.h>
#include <linux/kernel.h>

#include <asm/uaccess.h>
#include <linux/init.h>

#define SERIAL_PARANOIA_CHECK
#undef  SERIAL_DEBUG_OPEN
#undef  SERIAL_DEBUG_THROTTLE
#undef  SERIAL_DEBUG_OTHER
#undef  SERIAL_DEBUG_IO
#undef  SERIAL_DEBUG_COUNT
#undef  SERIAL_DEBUG_DTR
#undef  CYCLOM_16Y_HACK
#define  CYCLOM_ENABLE_MONITORING

#define WAKEUP_CHARS 256

#define STD_COM_FLAGS (0)

static struct tty_driver *cy_serial_driver;
extern int serial_console;
static struct cyclades_port *serial_console_info = NULL;
static unsigned int serial_console_cflag = 0;
u_char initial_console_speed;

/* Base address of cd2401 chip on mvme166/7 */

#define BASE_ADDR (0xfff45000)
#define pcc2chip	((volatile u_char *)0xfff42000)
#define PccSCCMICR	0x1d
#define PccSCCTICR	0x1e
#define PccSCCRICR	0x1f
#define PccTPIACKR	0x25
#define PccRPIACKR	0x27
#define PccIMLR		0x3f

/* This is the per-port data structure */
struct cyclades_port cy_port[] = {
Jiri Slaby's avatar
Jiri Slaby committed
113 114 115 116 117
	/* CARD#  */
	{-1},			/* ttyS0 */
	{-1},			/* ttyS1 */
	{-1},			/* ttyS2 */
	{-1},			/* ttyS3 */
Linus Torvalds's avatar
Linus Torvalds committed
118
};
Jiri Slaby's avatar
Jiri Slaby committed
119

120
#define NR_PORTS        ARRAY_SIZE(cy_port)
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130

/*
 * This is used to look up the divisor speeds and the timeouts
 * We're normally limited to 15 distinct baud rates.  The extra
 * are accessed via settings in info->flags.
 *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
 *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
 *                                                  HI            VHI
 */
static int baud_table[] = {
Jiri Slaby's avatar
Jiri Slaby committed
131 132 133 134
	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
	1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
	0
};
Linus Torvalds's avatar
Linus Torvalds committed
135 136

#if 0
Jiri Slaby's avatar
Jiri Slaby committed
137 138 139 140 141 142
static char baud_co[] = {	/* 25 MHz clock option table */
	/* value =>    00    01   02    03    04 */
	/* divide by    8    32   128   512  2048 */
	0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
	0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Linus Torvalds's avatar
Linus Torvalds committed
143

Jiri Slaby's avatar
Jiri Slaby committed
144 145 146 147
static char baud_bpr[] = {	/* 25 MHz baud rate period table */
	0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
	0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
};
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152
#endif

/* I think 166 brd clocks 2401 at 20MHz.... */

/* These values are written directly to tcor, and >> 5 for writing to rcor */
Jiri Slaby's avatar
Jiri Slaby committed
153 154 155 156
static u_char baud_co[] = {	/* 20 MHz clock option table */
	0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x60, 0x60, 0x40,
	0x40, 0x40, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Linus Torvalds's avatar
Linus Torvalds committed
157 158

/* These values written directly to tbpr/rbpr */
Jiri Slaby's avatar
Jiri Slaby committed
159 160 161 162
static u_char baud_bpr[] = {	/* 20 MHz baud rate period table */
	0x00, 0xc0, 0x80, 0x58, 0x6c, 0x40, 0xc0, 0x81, 0x40, 0x81,
	0x57, 0x40, 0x81, 0x40, 0x81, 0x40, 0x2b, 0x20, 0x15, 0x10
};
Linus Torvalds's avatar
Linus Torvalds committed
163

Jiri Slaby's avatar
Jiri Slaby committed
164 165 166 167
static u_char baud_cor4[] = {	/* receive threshold */
	0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
	0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07
};
Linus Torvalds's avatar
Linus Torvalds committed
168 169

static void shutdown(struct cyclades_port *);
Jiri Slaby's avatar
Jiri Slaby committed
170
static int startup(struct cyclades_port *);
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173 174 175 176 177 178 179
static void cy_throttle(struct tty_struct *);
static void cy_unthrottle(struct tty_struct *);
static void config_setup(struct cyclades_port *);
#ifdef CYCLOM_SHOW_STATUS
static void show_status(int);
#endif

#ifdef CONFIG_REMOTE_DEBUG
static void debug_setup(void);
Jiri Slaby's avatar
Jiri Slaby committed
180
void queueDebugChar(int c);
Linus Torvalds's avatar
Linus Torvalds committed
181 182 183 184 185 186
int getDebugChar(void);

#define DEBUG_PORT	1
#define DEBUG_LEN	256

typedef struct {
Jiri Slaby's avatar
Jiri Slaby committed
187 188 189
	int in;
	int out;
	unsigned char buf[DEBUG_LEN];
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192 193 194 195 196 197 198 199 200 201
} debugq;

debugq debugiq;
#endif

/*
 * I have my own version of udelay(), as it is needed when initialising
 * the chip, before the delay loop has been calibrated.  Should probably
 * reference one of the vmechip2 or pccchip2 counter for an accurate
 * delay, but this wild guess will do for now.
 */

Jiri Slaby's avatar
Jiri Slaby committed
202
void my_udelay(long us)
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206 207 208 209 210 211 212
{
	u_char x;
	volatile u_char *p = &x;
	int i;

	while (us--)
		for (i = 100; i; i--)
			x |= *p;
}

Jiri Slaby's avatar
Jiri Slaby committed
213 214
static inline int serial_paranoia_check(struct cyclades_port *info, char *name,
		const char *routine)
Linus Torvalds's avatar
Linus Torvalds committed
215 216
{
#ifdef SERIAL_PARANOIA_CHECK
Jiri Slaby's avatar
Jiri Slaby committed
217 218 219 220 221 222
	if (!info) {
		printk("Warning: null cyclades_port for (%s) in %s\n", name,
				routine);
		return 1;
	}

223
	if (info < &cy_port[0] || info >= &cy_port[NR_PORTS]) {
Jiri Slaby's avatar
Jiri Slaby committed
224 225 226 227 228 229 230 231 232 233
		printk("Warning: cyclades_port out of range for (%s) in %s\n",
				name, routine);
		return 1;
	}

	if (info->magic != CYCLADES_MAGIC) {
		printk("Warning: bad magic number for serial struct (%s) in "
				"%s\n", name, routine);
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
234 235
#endif
	return 0;
Jiri Slaby's avatar
Jiri Slaby committed
236
}				/* serial_paranoia_check */
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239 240 241

#if 0
/* The following diagnostic routines allow the driver to spew
   information on the screen, even (especially!) during interrupts.
 */
Jiri Slaby's avatar
Jiri Slaby committed
242 243 244 245
void SP(char *data)
{
	unsigned long flags;
	local_irq_save(flags);
Anirban Sinha's avatar
Anirban Sinha committed
246
	printk(KERN_EMERG "%s", data);
Jiri Slaby's avatar
Jiri Slaby committed
247
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
248
}
Jiri Slaby's avatar
Jiri Slaby committed
249

Linus Torvalds's avatar
Linus Torvalds committed
250
char scrn[2];
Jiri Slaby's avatar
Jiri Slaby committed
251 252 253 254 255
void CP(char data)
{
	unsigned long flags;
	local_irq_save(flags);
	scrn[0] = data;
Anirban Sinha's avatar
Anirban Sinha committed
256
	printk(KERN_EMERG "%c", scrn);
Jiri Slaby's avatar
Jiri Slaby committed
257 258
	local_irq_restore(flags);
}				/* CP */
Linus Torvalds's avatar
Linus Torvalds committed
259

Jiri Slaby's avatar
Jiri Slaby committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
void CP1(int data)
{
	(data < 10) ? CP(data + '0') : CP(data + 'A' - 10);
}				/* CP1 */
void CP2(int data)
{
	CP1((data >> 4) & 0x0f);
	CP1(data & 0x0f);
}				/* CP2 */
void CP4(int data)
{
	CP2((data >> 8) & 0xff);
	CP2(data & 0xff);
}				/* CP4 */
void CP8(long data)
{
	CP4((data >> 16) & 0xffff);
	CP4(data & 0xffff);
}				/* CP8 */
Linus Torvalds's avatar
Linus Torvalds committed
279 280 281 282 283 284 285
#endif

/* This routine waits up to 1000 micro-seconds for the previous
   command to the Cirrus chip to complete and then issues the
   new command.  An error is returned if the previous command
   didn't finish within the time limit.
 */
Jiri Slaby's avatar
Jiri Slaby committed
286
u_short write_cy_cmd(volatile u_char * base_addr, u_char cmd)
Linus Torvalds's avatar
Linus Torvalds committed
287
{
Jiri Slaby's avatar
Jiri Slaby committed
288 289
	unsigned long flags;
	volatile int i;
Linus Torvalds's avatar
Linus Torvalds committed
290

Jiri Slaby's avatar
Jiri Slaby committed
291
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
292
	/* Check to see that the previous command has completed */
Jiri Slaby's avatar
Jiri Slaby committed
293 294 295 296 297
	for (i = 0; i < 100; i++) {
		if (base_addr[CyCCR] == 0) {
			break;
		}
		my_udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
298 299
	}
	/* if the CCR never cleared, the previous command
Jiri Slaby's avatar
Jiri Slaby committed
300 301 302 303
	   didn't finish within the "reasonable time" */
	if (i == 10) {
		local_irq_restore(flags);
		return (-1);
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306 307
	}

	/* Issue the new command */
	base_addr[CyCCR] = cmd;
Jiri Slaby's avatar
Jiri Slaby committed
308 309 310
	local_irq_restore(flags);
	return (0);
}				/* write_cy_cmd */
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313 314

/* cy_start and cy_stop provide software output flow control as a
   function of XON/XOFF, software CTS, and other such stuff. */

Jiri Slaby's avatar
Jiri Slaby committed
315
static void cy_stop(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
316
{
Alan Cox's avatar
Alan Cox committed
317
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
318 319 320
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
321 322

#ifdef SERIAL_DEBUG_OTHER
Jiri Slaby's avatar
Jiri Slaby committed
323
	printk("cy_stop %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
324 325
#endif

Jiri Slaby's avatar
Jiri Slaby committed
326 327
	if (serial_paranoia_check(info, tty->name, "cy_stop"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
328

Jiri Slaby's avatar
Jiri Slaby committed
329
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
330

Jiri Slaby's avatar
Jiri Slaby committed
331 332 333 334 335
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) (channel);	/* index channel */
	base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
	local_irq_restore(flags);
}				/* cy_stop */
Linus Torvalds's avatar
Linus Torvalds committed
336

Jiri Slaby's avatar
Jiri Slaby committed
337
static void cy_start(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
338
{
Alan Cox's avatar
Alan Cox committed
339
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
340 341 342
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
343 344

#ifdef SERIAL_DEBUG_OTHER
Jiri Slaby's avatar
Jiri Slaby committed
345
	printk("cy_start %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
346 347
#endif

Jiri Slaby's avatar
Jiri Slaby committed
348 349
	if (serial_paranoia_check(info, tty->name, "cy_start"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
350

Jiri Slaby's avatar
Jiri Slaby committed
351
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
352

Jiri Slaby's avatar
Jiri Slaby committed
353 354 355 356 357
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) (channel);
	base_addr[CyIER] |= CyTxMpty;
	local_irq_restore(flags);
}				/* cy_start */
Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362

/* The real interrupt service routines are called
   whenever the card wants its hand held--chars
   received, out buffer empty, modem change, etc.
 */
Jiri Slaby's avatar
Jiri Slaby committed
363
static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
364
{
Jiri Slaby's avatar
Jiri Slaby committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	struct tty_struct *tty;
	struct cyclades_port *info;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	unsigned char err, rfoc;
	int channel;
	char data;

	/* determine the channel and change to that context */
	channel = (u_short) (base_addr[CyLICR] >> 2);
	info = &cy_port[channel];
	info->last_active = jiffies;

	if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
		/* This is a receive timeout interrupt, ignore it */
		base_addr[CyREOIR] = CyNOTRANS;
		return IRQ_HANDLED;
	}
Linus Torvalds's avatar
Linus Torvalds committed
382

Jiri Slaby's avatar
Jiri Slaby committed
383 384
	/* Read a byte of data if there is any - assume the error
	 * is associated with this character */
Linus Torvalds's avatar
Linus Torvalds committed
385

Jiri Slaby's avatar
Jiri Slaby committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	if ((rfoc = base_addr[CyRFOC]) != 0)
		data = base_addr[CyRDR];
	else
		data = 0;

	/* if there is nowhere to put the data, discard it */
	if (info->tty == 0) {
		base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
		return IRQ_HANDLED;
	} else {		/* there is an open port for this data */
		tty = info->tty;
		if (err & info->ignore_status_mask) {
			base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
			return IRQ_HANDLED;
		}
		if (tty_buffer_request_room(tty, 1) != 0) {
			if (err & info->read_status_mask) {
				if (err & CyBREAK) {
					tty_insert_flip_char(tty, data,
							     TTY_BREAK);
					if (info->flags & ASYNC_SAK) {
						do_SAK(tty);
					}
				} else if (err & CyFRAME) {
					tty_insert_flip_char(tty, data,
							     TTY_FRAME);
				} else if (err & CyPARITY) {
					tty_insert_flip_char(tty, data,
							     TTY_PARITY);
				} else if (err & CyOVERRUN) {
					tty_insert_flip_char(tty, 0,
							     TTY_OVERRUN);
					/*
					   If the flip buffer itself is
420
					   overflowing, we still lose
Jiri Slaby's avatar
Jiri Slaby committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
					   the next incoming character.
					 */
					if (tty_buffer_request_room(tty, 1) !=
					    0) {
						tty_insert_flip_char(tty, data,
								     TTY_FRAME);
					}
					/* These two conditions may imply */
					/* a normal read should be done. */
					/* else if(data & CyTIMEOUT) */
					/* else if(data & CySPECHAR) */
				} else {
					tty_insert_flip_char(tty, 0,
							     TTY_NORMAL);
				}
			} else {
				tty_insert_flip_char(tty, data, TTY_NORMAL);
			}
		} else {
			/* there was a software buffer overrun
			   and nothing could be done about it!!! */
		}
	}
	tty_schedule_flip(tty);
	/* end of service */
Linus Torvalds's avatar
Linus Torvalds committed
446 447
	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
Jiri Slaby's avatar
Jiri Slaby committed
448 449 450
}				/* cy_rxerr_interrupt */

static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
451
{
Jiri Slaby's avatar
Jiri Slaby committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	struct cyclades_port *info;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	int mdm_change;
	int mdm_status;

	/* determine the channel and change to that context */
	channel = (u_short) (base_addr[CyLICR] >> 2);
	info = &cy_port[channel];
	info->last_active = jiffies;

	mdm_change = base_addr[CyMISR];
	mdm_status = base_addr[CyMSVR1];

	if (info->tty == 0) {	/* nowhere to put the data, ignore it */
		;
	} else {
		if ((mdm_change & CyDCD)
		    && (info->flags & ASYNC_CHECK_CD)) {
			if (mdm_status & CyDCD) {
Linus Torvalds's avatar
Linus Torvalds committed
472
/* CP('!'); */
473
				wake_up_interruptible(&info->open_wait);
Jiri Slaby's avatar
Jiri Slaby committed
474
			} else {
Linus Torvalds's avatar
Linus Torvalds committed
475
/* CP('@'); */
476 477 478
				tty_hangup(info->tty);
				wake_up_interruptible(&info->open_wait);
				info->flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby's avatar
Jiri Slaby committed
479
			}
Linus Torvalds's avatar
Linus Torvalds committed
480
		}
Jiri Slaby's avatar
Jiri Slaby committed
481 482 483 484 485 486 487
		if ((mdm_change & CyCTS)
		    && (info->flags & ASYNC_CTS_FLOW)) {
			if (info->tty->stopped) {
				if (mdm_status & CyCTS) {
					/* !!! cy_start isn't used because... */
					info->tty->stopped = 0;
					base_addr[CyIER] |= CyTxMpty;
488
					tty_wakeup(info->tty);
Jiri Slaby's avatar
Jiri Slaby committed
489 490 491 492 493 494 495 496 497 498 499
				}
			} else {
				if (!(mdm_status & CyCTS)) {
					/* !!! cy_stop isn't used because... */
					info->tty->stopped = 1;
					base_addr[CyIER] &=
					    ~(CyTxMpty | CyTxRdy);
				}
			}
		}
		if (mdm_status & CyDSR) {
Linus Torvalds's avatar
Linus Torvalds committed
500 501
		}
	}
Jiri Slaby's avatar
Jiri Slaby committed
502 503 504
	base_addr[CyMEOIR] = 0;
	return IRQ_HANDLED;
}				/* cy_modem_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
505

Jiri Slaby's avatar
Jiri Slaby committed
506
static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
507
{
Jiri Slaby's avatar
Jiri Slaby committed
508 509 510 511 512
	struct cyclades_port *info;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	int char_count, saved_cnt;
	int outch;
Linus Torvalds's avatar
Linus Torvalds committed
513

Jiri Slaby's avatar
Jiri Slaby committed
514 515
	/* determine the channel and change to that context */
	channel = (u_short) (base_addr[CyLICR] >> 2);
Linus Torvalds's avatar
Linus Torvalds committed
516 517

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
518 519 520
	if (channel == DEBUG_PORT) {
		panic("TxInt on debug port!!!");
	}
Linus Torvalds's avatar
Linus Torvalds committed
521
#endif
Jiri Slaby's avatar
Jiri Slaby committed
522 523 524 525 526 527
	/* validate the port number (as configured and open) */
	if ((channel < 0) || (NR_PORTS <= channel)) {
		base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
		base_addr[CyTEOIR] = CyNOTRANS;
		return IRQ_HANDLED;
	}
528
	info = &cy_port[channel];
Jiri Slaby's avatar
Jiri Slaby committed
529 530 531 532 533 534 535 536 537 538 539 540
	info->last_active = jiffies;
	if (info->tty == 0) {
		base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
		base_addr[CyTEOIR] = CyNOTRANS;
		return IRQ_HANDLED;
	}

	/* load the on-chip space available for outbound data */
	saved_cnt = char_count = base_addr[CyTFTC];

	if (info->x_char) {	/* send special char */
		outch = info->x_char;
Linus Torvalds's avatar
Linus Torvalds committed
541 542
		base_addr[CyTDR] = outch;
		char_count--;
Jiri Slaby's avatar
Jiri Slaby committed
543
		info->x_char = 0;
Linus Torvalds's avatar
Linus Torvalds committed
544 545
	}

Jiri Slaby's avatar
Jiri Slaby committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	if (info->x_break) {
		/*  The Cirrus chip requires the "Embedded Transmit
		   Commands" of start break, delay, and end break
		   sequences to be sent.  The duration of the
		   break is given in TICs, which runs at HZ
		   (typically 100) and the PPR runs at 200 Hz,
		   so the delay is duration * 200/HZ, and thus a
		   break can run from 1/100 sec to about 5/4 sec.
		   Need to check these values - RGH 141095.
		 */
		base_addr[CyTDR] = 0;	/* start break */
		base_addr[CyTDR] = 0x81;
		base_addr[CyTDR] = 0;	/* delay a bit */
		base_addr[CyTDR] = 0x82;
		base_addr[CyTDR] = info->x_break * 200 / HZ;
		base_addr[CyTDR] = 0;	/* terminate break */
		base_addr[CyTDR] = 0x83;
		char_count -= 7;
		info->x_break = 0;
	}

	while (char_count > 0) {
		if (!info->xmit_cnt) {
			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
			break;
		}
		if (info->xmit_buf == 0) {
			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
			break;
		}
		if (info->tty->stopped || info->tty->hw_stopped) {
			base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
			break;
		}
		/* Because the Embedded Transmit Commands have been
		   enabled, we must check to see if the escape
		   character, NULL, is being sent.  If it is, we
		   must ensure that there is room for it to be
		   doubled in the output stream.  Therefore we
		   no longer advance the pointer when the character
		   is fetched, but rather wait until after the check
		   for a NULL output character. (This is necessary
		   because there may not be room for the two chars
		   needed to send a NULL.
		 */
		outch = info->xmit_buf[info->xmit_tail];
		if (outch) {
			info->xmit_cnt--;
			info->xmit_tail = (info->xmit_tail + 1)
			    & (PAGE_SIZE - 1);
			base_addr[CyTDR] = outch;
			char_count--;
		} else {
			if (char_count > 1) {
				info->xmit_cnt--;
				info->xmit_tail = (info->xmit_tail + 1)
				    & (PAGE_SIZE - 1);
				base_addr[CyTDR] = outch;
				base_addr[CyTDR] = 0;
				char_count--;
				char_count--;
			} else {
				break;
			}
		}
	}

613 614 615
	if (info->xmit_cnt < WAKEUP_CHARS)
		tty_wakeup(info->tty);

Jiri Slaby's avatar
Jiri Slaby committed
616 617 618
	base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
}				/* cy_tx_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
619

Jiri Slaby's avatar
Jiri Slaby committed
620
static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
621
{
Jiri Slaby's avatar
Jiri Slaby committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635
	struct tty_struct *tty;
	struct cyclades_port *info;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	char data;
	int char_count;
	int save_cnt;
	int len;

	/* determine the channel and change to that context */
	channel = (u_short) (base_addr[CyLICR] >> 2);
	info = &cy_port[channel];
	info->last_active = jiffies;
	save_cnt = char_count = base_addr[CyRFOC];
Linus Torvalds's avatar
Linus Torvalds committed
636 637

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
638 639 640 641 642 643
	if (channel == DEBUG_PORT) {
		while (char_count--) {
			data = base_addr[CyRDR];
			queueDebugChar(data);
		}
	} else
Linus Torvalds's avatar
Linus Torvalds committed
644
#endif
Jiri Slaby's avatar
Jiri Slaby committed
645 646 647 648 649 650 651 652
		/* if there is nowhere to put the data, discard it */
	if (info->tty == 0) {
		while (char_count--) {
			data = base_addr[CyRDR];
		}
	} else {		/* there is an open port for this data */
		tty = info->tty;
		/* load # characters available from the chip */
Linus Torvalds's avatar
Linus Torvalds committed
653 654

#ifdef CYCLOM_ENABLE_MONITORING
Jiri Slaby's avatar
Jiri Slaby committed
655 656 657 658 659 660
		++info->mon.int_count;
		info->mon.char_count += char_count;
		if (char_count > info->mon.char_max)
			info->mon.char_max = char_count;
		info->mon.char_last = char_count;
#endif
661
		while (char_count--) {
Jiri Slaby's avatar
Jiri Slaby committed
662 663
			data = base_addr[CyRDR];
			tty_insert_flip_char(tty, data, TTY_NORMAL);
Linus Torvalds's avatar
Linus Torvalds committed
664
#ifdef CYCLOM_16Y_HACK
Jiri Slaby's avatar
Jiri Slaby committed
665
			udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
666
#endif
Jiri Slaby's avatar
Jiri Slaby committed
667 668 669 670 671 672 673
		}
		tty_schedule_flip(tty);
	}
	/* end of service */
	base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
}				/* cy_rx_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
674 675 676 677

/* This is called whenever a port becomes active;
   interrupts are enabled and DTR & RTS are turned on.
 */
Jiri Slaby's avatar
Jiri Slaby committed
678
static int startup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
679
{
Jiri Slaby's avatar
Jiri Slaby committed
680 681 682
	unsigned long flags;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
683

Jiri Slaby's avatar
Jiri Slaby committed
684 685 686
	if (info->flags & ASYNC_INITIALIZED) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
687

Jiri Slaby's avatar
Jiri Slaby committed
688 689 690 691 692
	if (!info->type) {
		if (info->tty) {
			set_bit(TTY_IO_ERROR, &info->tty->flags);
		}
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
693
	}
Jiri Slaby's avatar
Jiri Slaby committed
694 695 696 697 698
	if (!info->xmit_buf) {
		info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
		if (!info->xmit_buf) {
			return -ENOMEM;
		}
Linus Torvalds's avatar
Linus Torvalds committed
699 700
	}

Jiri Slaby's avatar
Jiri Slaby committed
701
	config_setup(info);
Linus Torvalds's avatar
Linus Torvalds committed
702

Jiri Slaby's avatar
Jiri Slaby committed
703
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
704 705

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
706
	printk("startup channel %d\n", channel);
Linus Torvalds's avatar
Linus Torvalds committed
707 708
#endif

Jiri Slaby's avatar
Jiri Slaby committed
709 710 711
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) channel;
	write_cy_cmd(base_addr, CyENB_RCVR | CyENB_XMTR);
Linus Torvalds's avatar
Linus Torvalds committed
712

Jiri Slaby's avatar
Jiri Slaby committed
713
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717 718
	base_addr[CyMSVR1] = CyRTS;
/* CP('S');CP('1'); */
	base_addr[CyMSVR2] = CyDTR;

#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
719 720 721
	printk("cyc: %d: raising DTR\n", __LINE__);
	printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
	       base_addr[CyMSVR2]);
Linus Torvalds's avatar
Linus Torvalds committed
722 723 724 725 726
#endif

	base_addr[CyIER] |= CyRxData;
	info->flags |= ASYNC_INITIALIZED;

Jiri Slaby's avatar
Jiri Slaby committed
727 728
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
729 730 731
	}
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;

Jiri Slaby's avatar
Jiri Slaby committed
732
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
733 734

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
735
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
736
#endif
Jiri Slaby's avatar
Jiri Slaby committed
737 738
	return 0;
}				/* startup */
Linus Torvalds's avatar
Linus Torvalds committed
739

Jiri Slaby's avatar
Jiri Slaby committed
740
void start_xmit(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
741
{
Jiri Slaby's avatar
Jiri Slaby committed
742 743 744
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
745

Jiri Slaby's avatar
Jiri Slaby committed
746 747
	channel = info->line;
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
748 749
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
750 751
	local_irq_restore(flags);
}				/* start_xmit */
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754 755 756

/*
 * This routine shuts down a serial port; interrupts are disabled,
 * and DTR is dropped if the hangup on close termio flag is on.
 */
Jiri Slaby's avatar
Jiri Slaby committed
757
static void shutdown(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
758
{
Jiri Slaby's avatar
Jiri Slaby committed
759 760 761
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
762

Jiri Slaby's avatar
Jiri Slaby committed
763
	if (!(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds's avatar
Linus Torvalds committed
764
/* CP('$'); */
Jiri Slaby's avatar
Jiri Slaby committed
765 766
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
767

Jiri Slaby's avatar
Jiri Slaby committed
768
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
769 770

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
771
	printk("shutdown channel %d\n", channel);
Linus Torvalds's avatar
Linus Torvalds committed
772 773
#endif

Jiri Slaby's avatar
Jiri Slaby committed
774 775 776 777 778 779 780 781 782 783
	/* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
	   SENT BEFORE DROPPING THE LINE !!!  (Perhaps
	   set some flag that is read when XMTY happens.)
	   Other choices are to delay some fixed interval
	   or schedule some later processing.
	 */
	local_irq_save(flags);
	if (info->xmit_buf) {
		free_page((unsigned long)info->xmit_buf);
		info->xmit_buf = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
784 785
	}

Jiri Slaby's avatar
Jiri Slaby committed
786
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
787
	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
Jiri Slaby's avatar
Jiri Slaby committed
788
		base_addr[CyMSVR1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
789
/* CP('C');CP('1'); */
Jiri Slaby's avatar
Jiri Slaby committed
790
		base_addr[CyMSVR2] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
791
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
792 793 794
		printk("cyc: %d: dropping DTR\n", __LINE__);
		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
		       base_addr[CyMSVR2]);
Linus Torvalds's avatar
Linus Torvalds committed
795
#endif
Jiri Slaby's avatar
Jiri Slaby committed
796 797 798 799
	}
	write_cy_cmd(base_addr, CyDIS_RCVR);
	/* it may be appropriate to clear _XMIT at
	   some later date (after testing)!!! */
Linus Torvalds's avatar
Linus Torvalds committed
800

Jiri Slaby's avatar
Jiri Slaby committed
801 802
	if (info->tty) {
		set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
803 804
	}
	info->flags &= ~ASYNC_INITIALIZED;
Jiri Slaby's avatar
Jiri Slaby committed
805
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
806 807

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
808
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
809
#endif
Jiri Slaby's avatar
Jiri Slaby committed
810
}				/* shutdown */
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813 814

/*
 * This routine finds or computes the various line characteristics.
 */
Jiri Slaby's avatar
Jiri Slaby committed
815
static void config_setup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
816
{
Jiri Slaby's avatar
Jiri Slaby committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
	unsigned cflag;
	int i;
	unsigned char ti, need_init_chan = 0;

	if (!info->tty || !info->tty->termios) {
		return;
	}
	if (info->line == -1) {
		return;
	}
	cflag = info->tty->termios->c_cflag;

	/* baud rate */
	i = cflag & CBAUD;
Linus Torvalds's avatar
Linus Torvalds committed
834 835 836 837 838 839 840 841 842
#ifdef CBAUDEX
/* Starting with kernel 1.1.65, there is direct support for
   higher baud rates.  The following code supports those
   changes.  The conditional aspect allows this driver to be
   used for earlier as well as later kernel versions.  (The
   mapping is slightly different from serial.c because there
   is still the possibility of supporting 75 kbit/sec with
   the Cyclades board.)
 */
Jiri Slaby's avatar
Jiri Slaby committed
843 844 845 846 847
	if (i & CBAUDEX) {
		if (i == B57600)
			i = 16;
		else if (i == B115200)
			i = 18;
Linus Torvalds's avatar
Linus Torvalds committed
848
#ifdef B78600
Jiri Slaby's avatar
Jiri Slaby committed
849 850
		else if (i == B78600)
			i = 17;
Linus Torvalds's avatar
Linus Torvalds committed
851
#endif
Jiri Slaby's avatar
Jiri Slaby committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
		else
			info->tty->termios->c_cflag &= ~CBAUDEX;
	}
#endif
	if (i == 15) {
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
			i += 1;
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
			i += 3;
	}
	/* Don't ever change the speed of the console port.  It will
	 * run at the speed specified in bootinfo, or at 19.2K */
	/* Actually, it should run at whatever speed 166Bug was using */
	/* Note info->timeout isn't used at present */
	if (info != serial_console_info) {
		info->tbpr = baud_bpr[i];	/* Tx BPR */
		info->tco = baud_co[i];	/* Tx CO */
		info->rbpr = baud_bpr[i];	/* Rx BPR */
		info->rco = baud_co[i] >> 5;	/* Rx CO */
		if (baud_table[i] == 134) {
			info->timeout =
			    (info->xmit_fifo_size * HZ * 30 / 269) + 2;
			/* get it right for 134.5 baud */
		} else if (baud_table[i]) {
			info->timeout =
			    (info->xmit_fifo_size * HZ * 15 / baud_table[i]) +
			    2;
			/* this needs to be propagated into the card info */
		} else {
			info->timeout = 0;
		}
	}
	/* By tradition (is it a standard?) a baud rate of zero
	   implies the line should be/has been closed.  A bit
	   later in this routine such a test is performed. */

	/* byte size and parity */
	info->cor7 = 0;
	info->cor6 = 0;
	info->cor5 = 0;
	info->cor4 = (info->default_threshold ? info->default_threshold : baud_cor4[i]);	/* receive threshold */
	/* Following two lines added 101295, RGH. */
	/* It is obviously wrong to access CyCORx, and not info->corx here,
	 * try and remember to fix it later! */
	channel = info->line;
	base_addr[CyCAR] = (u_char) channel;
	if (C_CLOCAL(info->tty)) {
		if (base_addr[CyIER] & CyMdmCh)
			base_addr[CyIER] &= ~CyMdmCh;	/* without modem intr */
		/* ignore 1->0 modem transitions */
		if (base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD))
			base_addr[CyCOR4] &= ~(CyDSR | CyCTS | CyDCD);
		/* ignore 0->1 modem transitions */
		if (base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD))
			base_addr[CyCOR5] &= ~(CyDSR | CyCTS | CyDCD);
Linus Torvalds's avatar
Linus Torvalds committed
907
	} else {
Jiri Slaby's avatar
Jiri Slaby committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
		if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
			base_addr[CyIER] |= CyMdmCh;	/* with modem intr */
		/* act on 1->0 modem transitions */
		if ((base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD)) !=
		    (CyDSR | CyCTS | CyDCD))
			base_addr[CyCOR4] |= CyDSR | CyCTS | CyDCD;
		/* act on 0->1 modem transitions */
		if ((base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD)) !=
		    (CyDSR | CyCTS | CyDCD))
			base_addr[CyCOR5] |= CyDSR | CyCTS | CyDCD;
	}
	info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
	info->cor2 = CyETC;
	switch (cflag & CSIZE) {
	case CS5:
		info->cor1 = Cy_5_BITS;
		break;
	case CS6:
		info->cor1 = Cy_6_BITS;
		break;
	case CS7:
		info->cor1 = Cy_7_BITS;
		break;
	case CS8:
		info->cor1 = Cy_8_BITS;
		break;
	}
	if (cflag & PARENB) {
		if (cflag & PARODD) {
			info->cor1 |= CyPARITY_O;
		} else {
			info->cor1 |= CyPARITY_E;
		}
	} else {
		info->cor1 |= CyPARITY_NONE;
	}

	/* CTS flow control flag */
Linus Torvalds's avatar
Linus Torvalds committed
946
#if 0
Jiri Slaby's avatar
Jiri Slaby committed
947 948 949 950 951 952 953 954 955 956 957 958 959
	/* Don't complcate matters for now! RGH 141095 */
	if (cflag & CRTSCTS) {
		info->flags |= ASYNC_CTS_FLOW;
		info->cor2 |= CyCtsAE;
	} else {
		info->flags &= ~ASYNC_CTS_FLOW;
		info->cor2 &= ~CyCtsAE;
	}
#endif
	if (cflag & CLOCAL)
		info->flags &= ~ASYNC_CHECK_CD;
	else
		info->flags |= ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
960 961 962 963 964 965 966 967 968 969 970

     /***********************************************
	The hardware option, CyRtsAO, presents RTS when
	the chip has characters to send.  Since most modems
	use RTS as reverse (inbound) flow control, this
	option is not used.  If inbound flow control is
	necessary, DTR can be programmed to provide the
	appropriate signals for use with a non-standard
	cable.  Contact Marcio Saito for details.
     ***********************************************/

Jiri Slaby's avatar
Jiri Slaby committed
971
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
972

Jiri Slaby's avatar
Jiri Slaby committed
973 974
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
975 976 977

	/* CyCMR set once only in mvme167_init_serial() */
	if (base_addr[CyLICR] != channel << 2)
Jiri Slaby's avatar
Jiri Slaby committed
978
		base_addr[CyLICR] = channel << 2;
Linus Torvalds's avatar
Linus Torvalds committed
979
	if (base_addr[CyLIVR] != 0x5c)
Jiri Slaby's avatar
Jiri Slaby committed
980
		base_addr[CyLIVR] = 0x5c;
Linus Torvalds's avatar
Linus Torvalds committed
981

Jiri Slaby's avatar
Jiri Slaby committed
982
	/* tx and rx baud rate */
Linus Torvalds's avatar
Linus Torvalds committed
983 984

	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
985
		need_init_chan = 1;
Linus Torvalds's avatar
Linus Torvalds committed
986
	if (base_addr[CyTCOR] != info->tco)
Jiri Slaby's avatar
Jiri Slaby committed
987
		base_addr[CyTCOR] = info->tco;
Linus Torvalds's avatar
Linus Torvalds committed
988
	if (base_addr[CyTBPR] != info->tbpr)
Jiri Slaby's avatar
Jiri Slaby committed
989
		base_addr[CyTBPR] = info->tbpr;
Linus Torvalds's avatar
Linus Torvalds committed
990
	if (base_addr[CyRCOR] != info->rco)
Jiri Slaby's avatar
Jiri Slaby committed
991
		base_addr[CyRCOR] = info->rco;
Linus Torvalds's avatar
Linus Torvalds committed
992
	if (base_addr[CyRBPR] != info->rbpr)
Jiri Slaby's avatar
Jiri Slaby committed
993
		base_addr[CyRBPR] = info->rbpr;
Linus Torvalds's avatar
Linus Torvalds committed
994 995 996 997

	/* set line characteristics  according configuration */

	if (base_addr[CySCHR1] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
998
		base_addr[CySCHR1] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
999
	if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1000
		base_addr[CySCHR2] = STOP_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1001
	if (base_addr[CySCRL] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1002
		base_addr[CySCRL] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1003
	if (base_addr[CySCRH] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1004
		base_addr[CySCRH] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1005
	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
1006
		base_addr[CyCOR1] = info->cor1;
Linus Torvalds's avatar
Linus Torvalds committed
1007
	if (base_addr[CyCOR2] != info->cor2)
Jiri Slaby's avatar
Jiri Slaby committed
1008
		base_addr[CyCOR2] = info->cor2;
Linus Torvalds's avatar
Linus Torvalds committed
1009
	if (base_addr[CyCOR3] != info->cor3)
Jiri Slaby's avatar
Jiri Slaby committed
1010
		base_addr[CyCOR3] = info->cor3;
Linus Torvalds's avatar
Linus Torvalds committed
1011
	if (base_addr[CyCOR4] != info->cor4)
Jiri Slaby's avatar
Jiri Slaby committed
1012
		base_addr[CyCOR4] = info->cor4;
Linus Torvalds's avatar
Linus Torvalds committed
1013
	if (base_addr[CyCOR5] != info->cor5)
Jiri Slaby's avatar
Jiri Slaby committed
1014
		base_addr[CyCOR5] = info->cor5;
Linus Torvalds's avatar
Linus Torvalds committed
1015
	if (base_addr[CyCOR6] != info->cor6)
Jiri Slaby's avatar
Jiri Slaby committed
1016
		base_addr[CyCOR6] = info->cor6;
Linus Torvalds's avatar
Linus Torvalds committed
1017
	if (base_addr[CyCOR7] != info->cor7)
Jiri Slaby's avatar
Jiri Slaby committed
1018
		base_addr[CyCOR7] = info->cor7;
Linus Torvalds's avatar
Linus Torvalds committed
1019 1020

	if (need_init_chan)
Jiri Slaby's avatar
Jiri Slaby committed
1021
		write_cy_cmd(base_addr, CyINIT_CHAN);
Linus Torvalds's avatar
Linus Torvalds committed
1022

Jiri Slaby's avatar
Jiri Slaby committed
1023
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
1024 1025 1026 1027

	/* 2ms default rx timeout */
	ti = info->default_timeout ? info->default_timeout : 0x02;
	if (base_addr[CyRTPRL] != ti)
Jiri Slaby's avatar
Jiri Slaby committed
1028
		base_addr[CyRTPRL] = ti;
Linus Torvalds's avatar
Linus Torvalds committed
1029
	if (base_addr[CyRTPRH] != 0)
Jiri Slaby's avatar
Jiri Slaby committed
1030
		base_addr[CyRTPRH] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1031 1032

	/* Set up RTS here also ????? RGH 141095 */
Jiri Slaby's avatar
Jiri Slaby committed
1033 1034 1035
	if (i == 0) {		/* baud rate is zero, turn off line */
		if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
			base_addr[CyMSVR2] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1036
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1037 1038 1039
		printk("cyc: %d: dropping DTR\n", __LINE__);
		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
		       base_addr[CyMSVR2]);
Linus Torvalds's avatar
Linus Torvalds committed
1040
#endif
Jiri Slaby's avatar
Jiri Slaby committed
1041 1042 1043
	} else {
		if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
			base_addr[CyMSVR2] = CyDTR;
Linus Torvalds's avatar
Linus Torvalds committed
1044
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1045 1046 1047
		printk("cyc: %d: raising DTR\n", __LINE__);
		printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
		       base_addr[CyMSVR2]);
Linus Torvalds's avatar
Linus Torvalds committed
1048 1049 1050
#endif
	}

Jiri Slaby's avatar
Jiri Slaby committed
1051 1052
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1053 1054
	}

Jiri Slaby's avatar
Jiri Slaby committed
1055
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1056

Jiri Slaby's avatar
Jiri Slaby committed
1057
}				/* config_setup */
Linus Torvalds's avatar
Linus Torvalds committed
1058

1059
static int cy_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds's avatar
Linus Torvalds committed
1060
{
Alan Cox's avatar
Alan Cox committed
1061
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1062
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1063 1064

#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1065
	printk("cy_put_char %s(0x%02x)\n", tty->name, ch);
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1068
	if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1069
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1070

Jiri Slaby's avatar
Jiri Slaby committed
1071
	if (!info->xmit_buf)
1072
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1073

Jiri Slaby's avatar
Jiri Slaby committed
1074
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1075
	if (info->xmit_cnt >= PAGE_SIZE - 1) {
Jiri Slaby's avatar
Jiri Slaby committed
1076
		local_irq_restore(flags);
1077
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079 1080 1081 1082
	}

	info->xmit_buf[info->xmit_head++] = ch;
	info->xmit_head &= PAGE_SIZE - 1;
	info->xmit_cnt++;
Jiri Slaby's avatar
Jiri Slaby committed
1083
	local_irq_restore(flags);
1084
	return 1;
Jiri Slaby's avatar
Jiri Slaby committed
1085
}				/* cy_put_char */
Linus Torvalds's avatar
Linus Torvalds committed
1086

Jiri Slaby's avatar
Jiri Slaby committed
1087
static void cy_flush_chars(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1088
{
Alan Cox's avatar
Alan Cox committed
1089
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1090 1091 1092 1093
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;

Linus Torvalds's avatar
Linus Torvalds committed
1094
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1095
	printk("cy_flush_chars %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1096 1097
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1098 1099
	if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1100

Jiri Slaby's avatar
Jiri Slaby committed
1101 1102 1103
	if (info->xmit_cnt <= 0 || tty->stopped
	    || tty->hw_stopped || !info->xmit_buf)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1104

Jiri Slaby's avatar
Jiri Slaby committed
1105
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1106

Jiri Slaby's avatar
Jiri Slaby committed
1107
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1108 1109
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
1110 1111
	local_irq_restore(flags);
}				/* cy_flush_chars */
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114 1115 1116 1117 1118

/* This routine gets called when tty_write has put something into
    the write_queue.  If the port is not already transmitting stuff,
    start it off by enabling interrupts.  The interrupt service
    routine will then ensure that the characters are sent.  If the
    port is already active, there is no need to kick it.
 */
Jiri Slaby's avatar
Jiri Slaby committed
1119
static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1120
{
Alan Cox's avatar
Alan Cox committed
1121
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1122 1123
	unsigned long flags;
	int c, total = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125

#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1126
	printk("cy_write %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1129 1130 1131
	if (serial_paranoia_check(info, tty->name, "cy_write")) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1132

Jiri Slaby's avatar
Jiri Slaby committed
1133 1134 1135
	if (!info->xmit_buf) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1136

Jiri Slaby's avatar
Jiri Slaby committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	while (1) {
		local_irq_save(flags);
		c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
					  SERIAL_XMIT_SIZE - info->xmit_head));
		if (c <= 0) {
			local_irq_restore(flags);
			break;
		}

		memcpy(info->xmit_buf + info->xmit_head, buf, c);
		info->xmit_head =
		    (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
		info->xmit_cnt += c;
		local_irq_restore(flags);

		buf += c;
		count -= c;
		total += c;
	}

	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
		start_xmit(info);
	}
	return total;
}				/* cy_write */

static int cy_write_room(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1164
{
Alan Cox's avatar
Alan Cox committed
1165
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1166 1167
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
1168
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1169
	printk("cy_write_room %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1172 1173 1174 1175 1176 1177 1178
	if (serial_paranoia_check(info, tty->name, "cy_write_room"))
		return 0;
	ret = PAGE_SIZE - info->xmit_cnt - 1;
	if (ret < 0)
		ret = 0;
	return ret;
}				/* cy_write_room */
Linus Torvalds's avatar
Linus Torvalds committed
1179

Jiri Slaby's avatar
Jiri Slaby committed
1180
static int cy_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1181
{