serial167.c 70.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 55 56 57 58 59 60 61 62 63 64
/*
 * 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>
#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>
65
#include <linux/tty_flip.h>
Linus Torvalds's avatar
Linus Torvalds committed
66 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

#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
112 113 114 115 116
	/* CARD#  */
	{-1},			/* ttyS0 */
	{-1},			/* ttyS1 */
	{-1},			/* ttyS2 */
	{-1},			/* ttyS3 */
Linus Torvalds's avatar
Linus Torvalds committed
117
};
Jiri Slaby's avatar
Jiri Slaby committed
118

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

/*
 * 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
130 131 132 133
	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
134 135

#if 0
Jiri Slaby's avatar
Jiri Slaby committed
136 137 138 139 140 141
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
142

Jiri Slaby's avatar
Jiri Slaby committed
143 144 145 146
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
147 148 149 150 151
#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
152 153 154 155
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
156 157

/* These values written directly to tbpr/rbpr */
Jiri Slaby's avatar
Jiri Slaby committed
158 159 160 161
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
162

Jiri Slaby's avatar
Jiri Slaby committed
163 164 165 166
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
167 168

static void shutdown(struct cyclades_port *);
Jiri Slaby's avatar
Jiri Slaby committed
169
static int startup(struct cyclades_port *);
Linus Torvalds's avatar
Linus Torvalds committed
170 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 *);
extern void console_print(const char *);
#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 223 224 225 226 227 228 229 230 231 232 233 234
	if (!info) {
		printk("Warning: null cyclades_port for (%s) in %s\n", name,
				routine);
		return 1;
	}

	if ((long)info < (long)(&cy_port[0])
	    || (long)(&cy_port[NR_PORTS]) < (long)info) {
		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
235 236
#endif
	return 0;
Jiri Slaby's avatar
Jiri Slaby committed
237
}				/* serial_paranoia_check */
Linus Torvalds's avatar
Linus Torvalds committed
238 239 240 241 242

#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
243 244 245 246 247 248
void SP(char *data)
{
	unsigned long flags;
	local_irq_save(flags);
	console_print(data);
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
249
}
Jiri Slaby's avatar
Jiri Slaby committed
250

Linus Torvalds's avatar
Linus Torvalds committed
251
char scrn[2];
Jiri Slaby's avatar
Jiri Slaby committed
252 253 254 255 256 257 258 259
void CP(char data)
{
	unsigned long flags;
	local_irq_save(flags);
	scrn[0] = data;
	console_print(scrn);
	local_irq_restore(flags);
}				/* CP */
Linus Torvalds's avatar
Linus Torvalds committed
260

Jiri Slaby's avatar
Jiri Slaby committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
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
280 281 282 283 284 285 286
#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
287
u_short write_cy_cmd(volatile u_char * base_addr, u_char cmd)
Linus Torvalds's avatar
Linus Torvalds committed
288
{
Jiri Slaby's avatar
Jiri Slaby committed
289 290
	unsigned long flags;
	volatile int i;
Linus Torvalds's avatar
Linus Torvalds committed
291

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

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

/* 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
316
static void cy_stop(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
317
{
Jiri Slaby's avatar
Jiri Slaby committed
318 319 320 321
	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
322 323

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

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

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

Jiri Slaby's avatar
Jiri Slaby committed
332 333 334 335 336
	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
337

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

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

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

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

Jiri Slaby's avatar
Jiri Slaby committed
354 355 356 357 358
	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
359 360 361 362 363 364 365

/*
 * This routine is used by the interrupt handler to schedule
 * processing in the software interrupt portion of the driver
 * (also known as the "bottom half").  This can be called any
 * number of times for any channel without harm.
 */
Jiri Slaby's avatar
Jiri Slaby committed
366
static inline void cy_sched_event(struct cyclades_port *info, int event)
Linus Torvalds's avatar
Linus Torvalds committed
367
{
Jiri Slaby's avatar
Jiri Slaby committed
368 369 370
	info->event |= 1 << event;	/* remember what kind of event and who */
	schedule_work(&info->tqueue);
}				/* cy_sched_event */
Linus Torvalds's avatar
Linus Torvalds committed
371 372 373 374 375

/* 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
376
static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
377
{
Jiri Slaby's avatar
Jiri Slaby committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	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
395

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

Jiri Slaby's avatar
Jiri Slaby committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 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 446 447 448 449 450 451 452 453 454 455 456 457 458
	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
					   overflowing, we still loose
					   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
459 460
	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
Jiri Slaby's avatar
Jiri Slaby committed
461 462 463
}				/* cy_rxerr_interrupt */

static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
464
{
Jiri Slaby's avatar
Jiri Slaby committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	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
485
/* CP('!'); */
Jiri Slaby's avatar
Jiri Slaby committed
486 487
				cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
			} else {
Linus Torvalds's avatar
Linus Torvalds committed
488
/* CP('@'); */
Jiri Slaby's avatar
Jiri Slaby committed
489 490
				cy_sched_event(info, Cy_EVENT_HANGUP);
			}
Linus Torvalds's avatar
Linus Torvalds committed
491
		}
Jiri Slaby's avatar
Jiri Slaby committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
		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;
					cy_sched_event(info,
						       Cy_EVENT_WRITE_WAKEUP);
				}
			} 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
512 513
		}
	}
Jiri Slaby's avatar
Jiri Slaby committed
514 515 516
	base_addr[CyMEOIR] = 0;
	return IRQ_HANDLED;
}				/* cy_modem_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
517

Jiri Slaby's avatar
Jiri Slaby committed
518
static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
519
{
Jiri Slaby's avatar
Jiri Slaby committed
520 521 522 523 524
	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
525

Jiri Slaby's avatar
Jiri Slaby committed
526 527
	/* determine the channel and change to that context */
	channel = (u_short) (base_addr[CyLICR] >> 2);
Linus Torvalds's avatar
Linus Torvalds committed
528 529

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
530 531 532
	if (channel == DEBUG_PORT) {
		panic("TxInt on debug port!!!");
	}
Linus Torvalds's avatar
Linus Torvalds committed
533 534
#endif

Jiri Slaby's avatar
Jiri Slaby committed
535
	info = &cy_port[channel];
Linus Torvalds's avatar
Linus Torvalds committed
536

Jiri Slaby's avatar
Jiri Slaby committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	/* 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;
	}
	info->last_active = jiffies;
	if (info->tty == 0) {
		base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
		if (info->xmit_cnt < WAKEUP_CHARS) {
			cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
		}
		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
558 559
		base_addr[CyTDR] = outch;
		char_count--;
Jiri Slaby's avatar
Jiri Slaby committed
560
		info->x_char = 0;
Linus Torvalds's avatar
Linus Torvalds committed
561 562
	}

Jiri Slaby's avatar
Jiri Slaby committed
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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	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;
			}
		}
	}

	if (info->xmit_cnt < WAKEUP_CHARS) {
		cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
	}
	base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
}				/* cy_tx_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
636

Jiri Slaby's avatar
Jiri Slaby committed
637
static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
638
{
Jiri Slaby's avatar
Jiri Slaby committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652
	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
653 654

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
655 656 657 658 659 660
	if (channel == DEBUG_PORT) {
		while (char_count--) {
			data = base_addr[CyRDR];
			queueDebugChar(data);
		}
	} else
Linus Torvalds's avatar
Linus Torvalds committed
661
#endif
Jiri Slaby's avatar
Jiri Slaby committed
662 663 664 665 666 667 668 669
		/* 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
670 671

#ifdef CYCLOM_ENABLE_MONITORING
Jiri Slaby's avatar
Jiri Slaby committed
672 673 674 675 676 677 678 679 680 681
		++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
		len = tty_buffer_request_room(tty, char_count);
		while (len--) {
			data = base_addr[CyRDR];
			tty_insert_flip_char(tty, data, TTY_NORMAL);
Linus Torvalds's avatar
Linus Torvalds committed
682
#ifdef CYCLOM_16Y_HACK
Jiri Slaby's avatar
Jiri Slaby committed
683
			udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
684
#endif
Jiri Slaby's avatar
Jiri Slaby committed
685 686 687 688 689 690 691
		}
		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
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

/*
 * This routine is used to handle the "bottom half" processing for the
 * serial driver, known also the "software interrupt" processing.
 * This processing is done at the kernel interrupt level, after the
 * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 * is where time-consuming activities which can not be done in the
 * interrupt driver proper are done; the interrupt driver schedules
 * them using cy_sched_event(), and they get done here.
 *
 * This is done through one level of indirection--the task queue.
 * When a hardware interrupt service routine wants service by the
 * driver's bottom half, it enqueues the appropriate tq_struct (one
 * per port) to the keventd work queue and sets a request flag
 * that the work queue be processed.
 *
 * Although this may seem unwieldy, it gives the system a way to
 * pass an argument (in this case the pointer to the cyclades_port
 * structure) to the bottom half of the driver.  Previous kernels
 * had to poll every port to see if that port needed servicing.
 */
Jiri Slaby's avatar
Jiri Slaby committed
713
static void do_softint(struct work_struct *ugly_api)
Linus Torvalds's avatar
Linus Torvalds committed
714
{
Jiri Slaby's avatar
Jiri Slaby committed
715 716 717
	struct cyclades_port *info =
	    container_of(ugly_api, struct cyclades_port, tqueue);
	struct tty_struct *tty;
Linus Torvalds's avatar
Linus Torvalds committed
718

Jiri Slaby's avatar
Jiri Slaby committed
719 720 721
	tty = info->tty;
	if (!tty)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
722

Jiri Slaby's avatar
Jiri Slaby committed
723 724 725 726 727 728 729 730 731 732 733 734
	if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
		tty_hangup(info->tty);
		wake_up_interruptible(&info->open_wait);
		info->flags &= ~ASYNC_NORMAL_ACTIVE;
	}
	if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
		wake_up_interruptible(&info->open_wait);
	}
	if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
		tty_wakeup(tty);
	}
}				/* do_softint */
Linus Torvalds's avatar
Linus Torvalds committed
735 736 737 738

/* This is called whenever a port becomes active;
   interrupts are enabled and DTR & RTS are turned on.
 */
Jiri Slaby's avatar
Jiri Slaby committed
739
static int startup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
740
{
Jiri Slaby's avatar
Jiri Slaby committed
741 742 743
	unsigned long flags;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
744

Jiri Slaby's avatar
Jiri Slaby committed
745 746 747
	if (info->flags & ASYNC_INITIALIZED) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
748

Jiri Slaby's avatar
Jiri Slaby committed
749 750 751 752 753
	if (!info->type) {
		if (info->tty) {
			set_bit(TTY_IO_ERROR, &info->tty->flags);
		}
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
754
	}
Jiri Slaby's avatar
Jiri Slaby committed
755 756 757 758 759
	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
760 761
	}

Jiri Slaby's avatar
Jiri Slaby committed
762
	config_setup(info);
Linus Torvalds's avatar
Linus Torvalds committed
763

Jiri Slaby's avatar
Jiri Slaby committed
764
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
765 766

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

Jiri Slaby's avatar
Jiri Slaby committed
770 771 772
	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
773

Jiri Slaby's avatar
Jiri Slaby committed
774
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
775 776 777 778 779
	base_addr[CyMSVR1] = CyRTS;
/* CP('S');CP('1'); */
	base_addr[CyMSVR2] = CyDTR;

#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
780 781 782
	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
783 784 785 786 787
#endif

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

Jiri Slaby's avatar
Jiri Slaby committed
788 789
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
790 791 792
	}
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;

Jiri Slaby's avatar
Jiri Slaby committed
793
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
794 795

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
796
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
797
#endif
Jiri Slaby's avatar
Jiri Slaby committed
798 799
	return 0;
}				/* startup */
Linus Torvalds's avatar
Linus Torvalds committed
800

Jiri Slaby's avatar
Jiri Slaby committed
801
void start_xmit(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
802
{
Jiri Slaby's avatar
Jiri Slaby committed
803 804 805
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
806

Jiri Slaby's avatar
Jiri Slaby committed
807 808
	channel = info->line;
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
809 810
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
811 812
	local_irq_restore(flags);
}				/* start_xmit */
Linus Torvalds's avatar
Linus Torvalds committed
813 814 815 816 817

/*
 * 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
818
static void shutdown(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
819
{
Jiri Slaby's avatar
Jiri Slaby committed
820 821 822
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
823

Jiri Slaby's avatar
Jiri Slaby committed
824
	if (!(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds's avatar
Linus Torvalds committed
825
/* CP('$'); */
Jiri Slaby's avatar
Jiri Slaby committed
826 827
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
828

Jiri Slaby's avatar
Jiri Slaby committed
829
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
830 831

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

Jiri Slaby's avatar
Jiri Slaby committed
835 836 837 838 839 840 841 842 843 844
	/* !!! 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
845 846
	}

Jiri Slaby's avatar
Jiri Slaby committed
847
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
848
	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
Jiri Slaby's avatar
Jiri Slaby committed
849
		base_addr[CyMSVR1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
850
/* CP('C');CP('1'); */
Jiri Slaby's avatar
Jiri Slaby committed
851
		base_addr[CyMSVR2] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
852
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
853 854 855
		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
856
#endif
Jiri Slaby's avatar
Jiri Slaby committed
857 858 859 860
	}
	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
861

Jiri Slaby's avatar
Jiri Slaby committed
862 863
	if (info->tty) {
		set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
864 865
	}
	info->flags &= ~ASYNC_INITIALIZED;
Jiri Slaby's avatar
Jiri Slaby committed
866
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
867 868

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
869
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
870
#endif
Jiri Slaby's avatar
Jiri Slaby committed
871
}				/* shutdown */
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875

/*
 * This routine finds or computes the various line characteristics.
 */
Jiri Slaby's avatar
Jiri Slaby committed
876
static void config_setup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
877
{
Jiri Slaby's avatar
Jiri Slaby committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	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
895 896 897 898 899 900 901 902 903
#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
904 905 906 907 908
	if (i & CBAUDEX) {
		if (i == B57600)
			i = 16;
		else if (i == B115200)
			i = 18;
Linus Torvalds's avatar
Linus Torvalds committed
909
#ifdef B78600
Jiri Slaby's avatar
Jiri Slaby committed
910 911
		else if (i == B78600)
			i = 17;
Linus Torvalds's avatar
Linus Torvalds committed
912
#endif
Jiri Slaby's avatar
Jiri Slaby committed
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 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		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
968
	} else {
Jiri Slaby's avatar
Jiri Slaby committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		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
1007
#if 0
Jiri Slaby's avatar
Jiri Slaby committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	/* 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
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

     /***********************************************
	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
1032
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1033

Jiri Slaby's avatar
Jiri Slaby committed
1034 1035
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038

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

Jiri Slaby's avatar
Jiri Slaby committed
1043
	/* tx and rx baud rate */
Linus Torvalds's avatar
Linus Torvalds committed
1044 1045

	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
1046
		need_init_chan = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1047
	if (base_addr[CyTCOR] != info->tco)
Jiri Slaby's avatar
Jiri Slaby committed
1048
		base_addr[CyTCOR] = info->tco;
Linus Torvalds's avatar
Linus Torvalds committed
1049
	if (base_addr[CyTBPR] != info->tbpr)
Jiri Slaby's avatar
Jiri Slaby committed
1050
		base_addr[CyTBPR] = info->tbpr;
Linus Torvalds's avatar
Linus Torvalds committed
1051
	if (base_addr[CyRCOR] != info->rco)
Jiri Slaby's avatar
Jiri Slaby committed
1052
		base_addr[CyRCOR] = info->rco;
Linus Torvalds's avatar
Linus Torvalds committed
1053
	if (base_addr[CyRBPR] != info->rbpr)
Jiri Slaby's avatar
Jiri Slaby committed
1054
		base_addr[CyRBPR] = info->rbpr;
Linus Torvalds's avatar
Linus Torvalds committed
1055 1056 1057 1058

	/* set line characteristics  according configuration */

	if (base_addr[CySCHR1] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1059
		base_addr[CySCHR1] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1060
	if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1061
		base_addr[CySCHR2] = STOP_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1062
	if (base_addr[CySCRL] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1063
		base_addr[CySCRL] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1064
	if (base_addr[CySCRH] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
1065
		base_addr[CySCRH] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
1066
	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
1067
		base_addr[CyCOR1] = info->cor1;
Linus Torvalds's avatar
Linus Torvalds committed
1068
	if (base_addr[CyCOR2] != info->cor2)
Jiri Slaby's avatar
Jiri Slaby committed
1069
		base_addr[CyCOR2] = info->cor2;
Linus Torvalds's avatar
Linus Torvalds committed
1070
	if (base_addr[CyCOR3] != info->cor3)
Jiri Slaby's avatar
Jiri Slaby committed
1071
		base_addr[CyCOR3] = info->cor3;
Linus Torvalds's avatar
Linus Torvalds committed
1072
	if (base_addr[CyCOR4] != info->cor4)
Jiri Slaby's avatar
Jiri Slaby committed
1073
		base_addr[CyCOR4] = info->cor4;
Linus Torvalds's avatar
Linus Torvalds committed
1074
	if (base_addr[CyCOR5] != info->cor5)
Jiri Slaby's avatar
Jiri Slaby committed
1075
		base_addr[CyCOR5] = info->cor5;
Linus Torvalds's avatar
Linus Torvalds committed
1076
	if (base_addr[CyCOR6] != info->cor6)
Jiri Slaby's avatar
Jiri Slaby committed
1077
		base_addr[CyCOR6] = info->cor6;
Linus Torvalds's avatar
Linus Torvalds committed
1078
	if (base_addr[CyCOR7] != info->cor7)
Jiri Slaby's avatar
Jiri Slaby committed
1079
		base_addr[CyCOR7] = info->cor7;
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081

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

Jiri Slaby's avatar
Jiri Slaby committed
1084
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
1085 1086 1087 1088

	/* 2ms default rx timeout */
	ti = info->default_timeout ? info->default_timeout : 0x02;
	if (base_addr[CyRTPRL] != ti)
Jiri Slaby's avatar
Jiri Slaby committed
1089
		base_addr[CyRTPRL] = ti;
Linus Torvalds's avatar
Linus Torvalds committed
1090
	if (base_addr[CyRTPRH] != 0)
Jiri Slaby's avatar
Jiri Slaby committed
1091
		base_addr[CyRTPRH] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1092 1093

	/* Set up RTS here also ????? RGH 141095 */
Jiri Slaby's avatar
Jiri Slaby committed
1094 1095 1096
	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
1097
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1098 1099 1100
		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
1101
#endif
Jiri Slaby's avatar
Jiri Slaby committed
1102 1103 1104
	} else {
		if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
			base_addr[CyMSVR2] = CyDTR;
Linus Torvalds's avatar
Linus Torvalds committed
1105
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1106 1107 1108
		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
1109 1110 1111
#endif
	}

Jiri Slaby's avatar
Jiri Slaby committed
1112 1113
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1114 1115
	}

Jiri Slaby's avatar
Jiri Slaby committed
1116
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1117

Jiri Slaby's avatar
Jiri Slaby committed
1118
}				/* config_setup */
Linus Torvalds's avatar
Linus Torvalds committed
1119

Jiri Slaby's avatar
Jiri Slaby committed
1120
static void cy_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds's avatar
Linus Torvalds committed
1121
{
Jiri Slaby's avatar
Jiri Slaby committed
1122 1123
	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125

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

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

Jiri Slaby's avatar
Jiri Slaby committed
1132 1133
	if (!info->xmit_buf)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1134

Jiri Slaby's avatar
Jiri Slaby committed
1135
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1136
	if (info->xmit_cnt >= PAGE_SIZE - 1) {
Jiri Slaby's avatar
Jiri Slaby committed
1137 1138
		local_irq_restore(flags);
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1139 1140 1141 1142 1143
	}

	info->xmit_buf[info->xmit_head++] = ch;
	info->xmit_head &= PAGE_SIZE - 1;
	info->xmit_cnt++;
Jiri Slaby's avatar
Jiri Slaby committed
1144 1145
	local_irq_restore(flags);
}				/* cy_put_char */
Linus Torvalds's avatar
Linus Torvalds committed
1146

Jiri Slaby's avatar
Jiri Slaby committed
1147
static void cy_flush_chars(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1148
{
Jiri Slaby's avatar
Jiri Slaby committed
1149 1150 1151 1152 1153
	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;

Linus Torvalds's avatar
Linus Torvalds committed
1154
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1155
	printk("cy_flush_chars %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1156 1157
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1158 1159
	if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1160

Jiri Slaby's avatar
Jiri Slaby committed
1161 1162 1163
	if (info->xmit_cnt <= 0 || tty->stopped
	    || tty->hw_stopped || !info->xmit_buf)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1164

Jiri Slaby's avatar
Jiri Slaby committed
1165
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1166

Jiri Slaby's avatar
Jiri Slaby committed
1167
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1168 1169
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
1170 1171
	local_irq_restore(flags);
}				/* cy_flush_chars */
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173 1174 1175 1176 1177 1178

/* 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
1179
static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1180
{
Jiri Slaby's avatar
Jiri Slaby committed
1181 1182 1183
	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
	unsigned long flags;
	int c, total = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185

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

Jiri Slaby's avatar
Jiri Slaby committed
1189 1190 1191
	if (serial_paranoia_check(info, tty->name, "cy_write")) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1192

Jiri Slaby's avatar
Jiri Slaby committed
1193 1194 1195
	if (!info->xmit_buf) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1196

Jiri Slaby's avatar
Jiri Slaby committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	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
1224
{
Jiri Slaby's avatar
Jiri Slaby committed
1225 1226 1227
	struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
1228
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1229
	printk("cy_write_room %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1230 1231
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1232 1233 1234 1235 1236 1237 1238
	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
1239

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