serial167.c 63.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>
67
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
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 113

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

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

/*
 * 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
132 133 134 135
	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
136 137

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

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

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

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

static void shutdown(struct cyclades_port *);
Jiri Slaby's avatar
Jiri Slaby committed
171
static int startup(struct cyclades_port *);
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
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

/*
 * 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
186
void my_udelay(long us)
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194 195 196
{
	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
197 198
static inline int serial_paranoia_check(struct cyclades_port *info, char *name,
		const char *routine)
Linus Torvalds's avatar
Linus Torvalds committed
199 200
{
#ifdef SERIAL_PARANOIA_CHECK
Jiri Slaby's avatar
Jiri Slaby committed
201 202 203 204 205 206
	if (!info) {
		printk("Warning: null cyclades_port for (%s) in %s\n", name,
				routine);
		return 1;
	}

207
	if (info < &cy_port[0] || info >= &cy_port[NR_PORTS]) {
Jiri Slaby's avatar
Jiri Slaby committed
208 209 210 211 212 213 214 215 216 217
		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
218 219
#endif
	return 0;
Jiri Slaby's avatar
Jiri Slaby committed
220
}				/* serial_paranoia_check */
Linus Torvalds's avatar
Linus Torvalds committed
221 222 223 224 225

#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
226 227 228 229
void SP(char *data)
{
	unsigned long flags;
	local_irq_save(flags);
Anirban Sinha's avatar
Anirban Sinha committed
230
	printk(KERN_EMERG "%s", data);
Jiri Slaby's avatar
Jiri Slaby committed
231
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
232
}
Jiri Slaby's avatar
Jiri Slaby committed
233

Linus Torvalds's avatar
Linus Torvalds committed
234
char scrn[2];
Jiri Slaby's avatar
Jiri Slaby committed
235 236 237 238 239
void CP(char data)
{
	unsigned long flags;
	local_irq_save(flags);
	scrn[0] = data;
Anirban Sinha's avatar
Anirban Sinha committed
240
	printk(KERN_EMERG "%c", scrn);
Jiri Slaby's avatar
Jiri Slaby committed
241 242
	local_irq_restore(flags);
}				/* CP */
Linus Torvalds's avatar
Linus Torvalds committed
243

Jiri Slaby's avatar
Jiri Slaby committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
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
263 264 265 266 267 268 269
#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
270
u_short write_cy_cmd(volatile u_char * base_addr, u_char cmd)
Linus Torvalds's avatar
Linus Torvalds committed
271
{
Jiri Slaby's avatar
Jiri Slaby committed
272 273
	unsigned long flags;
	volatile int i;
Linus Torvalds's avatar
Linus Torvalds committed
274

Jiri Slaby's avatar
Jiri Slaby committed
275
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
276
	/* Check to see that the previous command has completed */
Jiri Slaby's avatar
Jiri Slaby committed
277 278 279 280 281
	for (i = 0; i < 100; i++) {
		if (base_addr[CyCCR] == 0) {
			break;
		}
		my_udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
282 283
	}
	/* if the CCR never cleared, the previous command
Jiri Slaby's avatar
Jiri Slaby committed
284 285 286 287
	   didn't finish within the "reasonable time" */
	if (i == 10) {
		local_irq_restore(flags);
		return (-1);
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291
	}

	/* Issue the new command */
	base_addr[CyCCR] = cmd;
Jiri Slaby's avatar
Jiri Slaby committed
292 293 294
	local_irq_restore(flags);
	return (0);
}				/* write_cy_cmd */
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298

/* 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
299
static void cy_stop(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
300
{
Alan Cox's avatar
Alan Cox committed
301
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
302 303 304
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
305 306

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

Jiri Slaby's avatar
Jiri Slaby committed
310 311
	if (serial_paranoia_check(info, tty->name, "cy_stop"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
312

Jiri Slaby's avatar
Jiri Slaby committed
313
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
314

Jiri Slaby's avatar
Jiri Slaby committed
315 316 317 318 319
	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
320

Jiri Slaby's avatar
Jiri Slaby committed
321
static void cy_start(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
322
{
Alan Cox's avatar
Alan Cox committed
323
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
324 325 326
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
327 328

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

Jiri Slaby's avatar
Jiri Slaby committed
332 333
	if (serial_paranoia_check(info, tty->name, "cy_start"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
334

Jiri Slaby's avatar
Jiri Slaby committed
335
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
336

Jiri Slaby's avatar
Jiri Slaby committed
337 338 339 340 341
	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
342 343 344 345 346

/* 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
347
static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
348
{
Jiri Slaby's avatar
Jiri Slaby committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	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
366

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

Jiri Slaby's avatar
Jiri Slaby committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	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
404
					   overflowing, we still lose
Jiri Slaby's avatar
Jiri Slaby committed
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
					   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
430 431
	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
Jiri Slaby's avatar
Jiri Slaby committed
432 433 434
}				/* cy_rxerr_interrupt */

static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
435
{
Jiri Slaby's avatar
Jiri Slaby committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	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
456
/* CP('!'); */
457
				wake_up_interruptible(&info->open_wait);
Jiri Slaby's avatar
Jiri Slaby committed
458
			} else {
Linus Torvalds's avatar
Linus Torvalds committed
459
/* CP('@'); */
460 461 462
				tty_hangup(info->tty);
				wake_up_interruptible(&info->open_wait);
				info->flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby's avatar
Jiri Slaby committed
463
			}
Linus Torvalds's avatar
Linus Torvalds committed
464
		}
Jiri Slaby's avatar
Jiri Slaby committed
465 466 467 468 469 470 471
		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;
472
					tty_wakeup(info->tty);
Jiri Slaby's avatar
Jiri Slaby committed
473 474 475 476 477 478 479 480 481 482 483
				}
			} 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
484 485
		}
	}
Jiri Slaby's avatar
Jiri Slaby committed
486 487 488
	base_addr[CyMEOIR] = 0;
	return IRQ_HANDLED;
}				/* cy_modem_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
489

Jiri Slaby's avatar
Jiri Slaby committed
490
static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
491
{
Jiri Slaby's avatar
Jiri Slaby committed
492 493 494 495 496
	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
497

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

Jiri Slaby's avatar
Jiri Slaby committed
501 502 503 504 505 506
	/* 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;
	}
507
	info = &cy_port[channel];
Jiri Slaby's avatar
Jiri Slaby committed
508 509 510 511 512 513 514 515 516 517 518 519
	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
520 521
		base_addr[CyTDR] = outch;
		char_count--;
Jiri Slaby's avatar
Jiri Slaby committed
522
		info->x_char = 0;
Linus Torvalds's avatar
Linus Torvalds committed
523 524
	}

Jiri Slaby's avatar
Jiri Slaby committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 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
	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;
			}
		}
	}

592 593 594
	if (info->xmit_cnt < WAKEUP_CHARS)
		tty_wakeup(info->tty);

Jiri Slaby's avatar
Jiri Slaby committed
595 596 597
	base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
}				/* cy_tx_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
598

Jiri Slaby's avatar
Jiri Slaby committed
599
static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
600
{
Jiri Slaby's avatar
Jiri Slaby committed
601 602 603 604 605 606 607 608 609 610 611 612 613
	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;

	/* 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
614

Jiri Slaby's avatar
Jiri Slaby committed
615 616 617 618 619 620 621 622
		/* 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
623 624

#ifdef CYCLOM_ENABLE_MONITORING
Jiri Slaby's avatar
Jiri Slaby committed
625 626 627 628 629 630
		++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
631
		while (char_count--) {
Jiri Slaby's avatar
Jiri Slaby committed
632 633
			data = base_addr[CyRDR];
			tty_insert_flip_char(tty, data, TTY_NORMAL);
Linus Torvalds's avatar
Linus Torvalds committed
634
#ifdef CYCLOM_16Y_HACK
Jiri Slaby's avatar
Jiri Slaby committed
635
			udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
636
#endif
Jiri Slaby's avatar
Jiri Slaby committed
637 638 639 640 641 642 643
		}
		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
644 645 646 647

/* This is called whenever a port becomes active;
   interrupts are enabled and DTR & RTS are turned on.
 */
Jiri Slaby's avatar
Jiri Slaby committed
648
static int startup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
649
{
Jiri Slaby's avatar
Jiri Slaby committed
650 651 652
	unsigned long flags;
	volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
653

Jiri Slaby's avatar
Jiri Slaby committed
654 655 656
	if (info->flags & ASYNC_INITIALIZED) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
657

Jiri Slaby's avatar
Jiri Slaby committed
658 659 660 661 662
	if (!info->type) {
		if (info->tty) {
			set_bit(TTY_IO_ERROR, &info->tty->flags);
		}
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
663
	}
Jiri Slaby's avatar
Jiri Slaby committed
664 665 666 667 668
	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
669 670
	}

Jiri Slaby's avatar
Jiri Slaby committed
671
	config_setup(info);
Linus Torvalds's avatar
Linus Torvalds committed
672

Jiri Slaby's avatar
Jiri Slaby committed
673
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
674 675

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

Jiri Slaby's avatar
Jiri Slaby committed
679 680 681
	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
682

Jiri Slaby's avatar
Jiri Slaby committed
683
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687 688
	base_addr[CyMSVR1] = CyRTS;
/* CP('S');CP('1'); */
	base_addr[CyMSVR2] = CyDTR;

#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
689 690 691
	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
692 693 694 695 696
#endif

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

Jiri Slaby's avatar
Jiri Slaby committed
697 698
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
699 700 701
	}
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;

Jiri Slaby's avatar
Jiri Slaby committed
702
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
703 704

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
705
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
706
#endif
Jiri Slaby's avatar
Jiri Slaby committed
707 708
	return 0;
}				/* startup */
Linus Torvalds's avatar
Linus Torvalds committed
709

Jiri Slaby's avatar
Jiri Slaby committed
710
void start_xmit(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
711
{
Jiri Slaby's avatar
Jiri Slaby committed
712 713 714
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
715

Jiri Slaby's avatar
Jiri Slaby committed
716 717
	channel = info->line;
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
718 719
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
720 721
	local_irq_restore(flags);
}				/* start_xmit */
Linus Torvalds's avatar
Linus Torvalds committed
722 723 724 725 726

/*
 * 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
727
static void shutdown(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
728
{
Jiri Slaby's avatar
Jiri Slaby committed
729 730 731
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
732

Jiri Slaby's avatar
Jiri Slaby committed
733
	if (!(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds's avatar
Linus Torvalds committed
734
/* CP('$'); */
Jiri Slaby's avatar
Jiri Slaby committed
735 736
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
737

Jiri Slaby's avatar
Jiri Slaby committed
738
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
739 740

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

Jiri Slaby's avatar
Jiri Slaby committed
744 745 746 747 748 749 750 751 752 753
	/* !!! 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
754 755
	}

Jiri Slaby's avatar
Jiri Slaby committed
756
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
757
	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
Jiri Slaby's avatar
Jiri Slaby committed
758
		base_addr[CyMSVR1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
759
/* CP('C');CP('1'); */
Jiri Slaby's avatar
Jiri Slaby committed
760
		base_addr[CyMSVR2] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
761
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
762 763 764
		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
765
#endif
Jiri Slaby's avatar
Jiri Slaby committed
766 767 768 769
	}
	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
770

Jiri Slaby's avatar
Jiri Slaby committed
771 772
	if (info->tty) {
		set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
773 774
	}
	info->flags &= ~ASYNC_INITIALIZED;
Jiri Slaby's avatar
Jiri Slaby committed
775
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
776 777

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
778
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
779
#endif
Jiri Slaby's avatar
Jiri Slaby committed
780
}				/* shutdown */
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784

/*
 * This routine finds or computes the various line characteristics.
 */
Jiri Slaby's avatar
Jiri Slaby committed
785
static void config_setup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
786
{
Jiri Slaby's avatar
Jiri Slaby committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
	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
804 805 806 807 808 809 810 811 812
#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
813 814 815 816 817
	if (i & CBAUDEX) {
		if (i == B57600)
			i = 16;
		else if (i == B115200)
			i = 18;
Linus Torvalds's avatar
Linus Torvalds committed
818
#ifdef B78600
Jiri Slaby's avatar
Jiri Slaby committed
819 820
		else if (i == B78600)
			i = 17;
Linus Torvalds's avatar
Linus Torvalds committed
821
#endif
Jiri Slaby's avatar
Jiri Slaby committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 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
		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
877
	} else {
Jiri Slaby's avatar
Jiri Slaby committed
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 907 908 909 910 911 912 913 914 915
		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
916
#if 0
Jiri Slaby's avatar
Jiri Slaby committed
917 918 919 920 921 922 923 924 925 926 927 928 929
	/* 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
930 931 932 933 934 935 936 937 938 939 940

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

Jiri Slaby's avatar
Jiri Slaby committed
943 944
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947

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

Jiri Slaby's avatar
Jiri Slaby committed
952
	/* tx and rx baud rate */
Linus Torvalds's avatar
Linus Torvalds committed
953 954

	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
955
		need_init_chan = 1;
Linus Torvalds's avatar
Linus Torvalds committed
956
	if (base_addr[CyTCOR] != info->tco)
Jiri Slaby's avatar
Jiri Slaby committed
957
		base_addr[CyTCOR] = info->tco;
Linus Torvalds's avatar
Linus Torvalds committed
958
	if (base_addr[CyTBPR] != info->tbpr)
Jiri Slaby's avatar
Jiri Slaby committed
959
		base_addr[CyTBPR] = info->tbpr;
Linus Torvalds's avatar
Linus Torvalds committed
960
	if (base_addr[CyRCOR] != info->rco)
Jiri Slaby's avatar
Jiri Slaby committed
961
		base_addr[CyRCOR] = info->rco;
Linus Torvalds's avatar
Linus Torvalds committed
962
	if (base_addr[CyRBPR] != info->rbpr)
Jiri Slaby's avatar
Jiri Slaby committed
963
		base_addr[CyRBPR] = info->rbpr;
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966 967

	/* set line characteristics  according configuration */

	if (base_addr[CySCHR1] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
968
		base_addr[CySCHR1] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
969
	if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
970
		base_addr[CySCHR2] = STOP_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
971
	if (base_addr[CySCRL] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
972
		base_addr[CySCRL] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
973
	if (base_addr[CySCRH] != START_CHAR(info->tty))
Jiri Slaby's avatar
Jiri Slaby committed
974
		base_addr[CySCRH] = START_CHAR(info->tty);
Linus Torvalds's avatar
Linus Torvalds committed
975
	if (base_addr[CyCOR1] != info->cor1)
Jiri Slaby's avatar
Jiri Slaby committed
976
		base_addr[CyCOR1] = info->cor1;
Linus Torvalds's avatar
Linus Torvalds committed
977
	if (base_addr[CyCOR2] != info->cor2)
Jiri Slaby's avatar
Jiri Slaby committed
978
		base_addr[CyCOR2] = info->cor2;
Linus Torvalds's avatar
Linus Torvalds committed
979
	if (base_addr[CyCOR3] != info->cor3)
Jiri Slaby's avatar
Jiri Slaby committed
980
		base_addr[CyCOR3] = info->cor3;
Linus Torvalds's avatar
Linus Torvalds committed
981
	if (base_addr[CyCOR4] != info->cor4)
Jiri Slaby's avatar
Jiri Slaby committed
982
		base_addr[CyCOR4] = info->cor4;
Linus Torvalds's avatar
Linus Torvalds committed
983
	if (base_addr[CyCOR5] != info->cor5)
Jiri Slaby's avatar
Jiri Slaby committed
984
		base_addr[CyCOR5] = info->cor5;
Linus Torvalds's avatar
Linus Torvalds committed
985
	if (base_addr[CyCOR6] != info->cor6)
Jiri Slaby's avatar
Jiri Slaby committed
986
		base_addr[CyCOR6] = info->cor6;
Linus Torvalds's avatar
Linus Torvalds committed
987
	if (base_addr[CyCOR7] != info->cor7)
Jiri Slaby's avatar
Jiri Slaby committed
988
		base_addr[CyCOR7] = info->cor7;
Linus Torvalds's avatar
Linus Torvalds committed
989 990

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

Jiri Slaby's avatar
Jiri Slaby committed
993
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
994 995 996 997

	/* 2ms default rx timeout */
	ti = info->default_timeout ? info->default_timeout : 0x02;
	if (base_addr[CyRTPRL] != ti)
Jiri Slaby's avatar
Jiri Slaby committed
998
		base_addr[CyRTPRL] = ti;
Linus Torvalds's avatar
Linus Torvalds committed
999
	if (base_addr[CyRTPRH] != 0)
Jiri Slaby's avatar
Jiri Slaby committed
1000
		base_addr[CyRTPRH] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002

	/* Set up RTS here also ????? RGH 141095 */
Jiri Slaby's avatar
Jiri Slaby committed
1003 1004 1005
	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
1006
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1007 1008 1009
		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
1010
#endif
Jiri Slaby's avatar
Jiri Slaby committed
1011 1012 1013
	} else {
		if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
			base_addr[CyMSVR2] = CyDTR;
Linus Torvalds's avatar
Linus Torvalds committed
1014
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1015 1016 1017
		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
1018 1019 1020
#endif
	}

Jiri Slaby's avatar
Jiri Slaby committed
1021 1022
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024
	}

Jiri Slaby's avatar
Jiri Slaby committed
1025
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1026

Jiri Slaby's avatar
Jiri Slaby committed
1027
}				/* config_setup */
Linus Torvalds's avatar
Linus Torvalds committed
1028

1029
static int cy_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds's avatar
Linus Torvalds committed
1030
{
Alan Cox's avatar
Alan Cox committed
1031
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1032
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1033 1034

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

Jiri Slaby's avatar
Jiri Slaby committed
1038
	if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1039
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1040

Jiri Slaby's avatar
Jiri Slaby committed
1041
	if (!info->xmit_buf)
1042
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1043

Jiri Slaby's avatar
Jiri Slaby committed
1044
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1045
	if (info->xmit_cnt >= PAGE_SIZE - 1) {
Jiri Slaby's avatar
Jiri Slaby committed
1046
		local_irq_restore(flags);
1047
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1048 1049 1050 1051 1052
	}

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

Jiri Slaby's avatar
Jiri Slaby committed
1057
static void cy_flush_chars(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1058
{
Alan Cox's avatar
Alan Cox committed
1059
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1060 1061 1062 1063
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;

Linus Torvalds's avatar
Linus Torvalds committed
1064
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1065
	printk("cy_flush_chars %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067
#endif

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

Jiri Slaby's avatar
Jiri Slaby committed
1071 1072 1073
	if (info->xmit_cnt <= 0 || tty->stopped
	    || tty->hw_stopped || !info->xmit_buf)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1074

Jiri Slaby's avatar
Jiri Slaby committed
1075
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1076

Jiri Slaby's avatar
Jiri Slaby committed
1077
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079
	base_addr[CyCAR] = channel;
	base_addr[CyIER] |= CyTxMpty;
Jiri Slaby's avatar
Jiri Slaby committed
1080 1081
	local_irq_restore(flags);
}				/* cy_flush_chars */
Linus Torvalds's avatar
Linus Torvalds committed
1082 1083 1084 1085 1086 1087 1088

/* 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
1089
static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1090
{
Alan Cox's avatar
Alan Cox committed
1091
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1092 1093
	unsigned long flags;
	int c, total = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1094 1095

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

Jiri Slaby's avatar
Jiri Slaby committed
1099 1100 1101
	if (serial_paranoia_check(info, tty->name, "cy_write")) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1102

Jiri Slaby's avatar
Jiri Slaby committed
1103 1104 1105
	if (!info->xmit_buf) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1106

Jiri Slaby's avatar
Jiri Slaby committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	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
1134
{
Alan Cox's avatar
Alan Cox committed
1135
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1136 1137
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
1138
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1139
	printk("cy_write_room %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1140 1141
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1142 1143 1144 1145 1146 1147 1148
	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
1149

Jiri Slaby's avatar
Jiri Slaby committed
1150
static int cy_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1151
{
Alan Cox's avatar
Alan Cox committed
1152
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1153

Linus Torvalds's avatar
Linus Torvalds committed
1154
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1155
	printk("cy_chars_in_buffer %s %d\n", tty->name, info->xmit_cnt);	/* */
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_chars_in_buffer"))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1160

Jiri Slaby's avatar
Jiri Slaby committed
1161 1162
	return info->xmit_cnt;
}				/* cy_chars_in_buffer */
Linus Torvalds's avatar
Linus Torvalds committed
1163

Jiri Slaby's avatar
Jiri Slaby committed
1164
static void cy_flush_buffer(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1165
{
Alan Cox's avatar
Alan Cox committed
1166
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1167 1168
	unsigned long flags;

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

Jiri Slaby's avatar
Jiri Slaby committed
1173 1174 1175
	if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
		return;
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1176
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Jiri Slaby's avatar
Jiri Slaby committed
1177 1178 1179
	local_irq_restore(flags);
	tty_wakeup(tty);
}				/* cy_flush_buffer */
Linus Torvalds's avatar
Linus Torvalds committed
1180 1181 1182 1183 1184

/* This routine is called by the upper-layer tty layer to signal
   that incoming characters should be throttled or that the
   throttle should be released.
 */
Jiri Slaby's avatar
Jiri Slaby committed
1185
static void cy_throttle(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1186
{
Alan Cox's avatar
Alan Cox committed
1187
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1188 1189 1190
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
1191 1192

#ifdef SERIAL_DEBUG_THROTTLE
Jiri Slaby's avatar
Jiri Slaby committed
1193 1194 1195 1196 1197
	char buf[64];

	printk("throttle %s: %d....\n", tty_name(tty, buf),
	       tty->ldisc.chars_in_buffer(tty));
	printk("cy_throttle %s\n", tty->name);
Linus Torvalds's avatar
Linus Torvalds committed
1198 1199
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1200 1201 1202
	if (serial_paranoia_check(info, tty->name, "cy_nthrottle")) {
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1203

Jiri Slaby's avatar
Jiri Slaby committed
1204 1205 1206 1207
	if (I_IXOFF(tty)) {
		info->x_char = STOP_CHAR(tty);
		/* Should use the "Send Special Character" feature!!! */
	}
Linus Torvalds's avatar
Linus Torvalds committed
1208

Jiri Slaby's avatar
Jiri Slaby committed
1209
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1210