serial167.c 67.4 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
{
Alan Cox's avatar
Alan Cox committed
318
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
319 320 321
	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
{
Alan Cox's avatar
Alan Cox committed
340
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
341 342 343
	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

/* 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
364
static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
365
{
Jiri Slaby's avatar
Jiri Slaby committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	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
383

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

Jiri Slaby's avatar
Jiri Slaby committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	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
421
					   overflowing, we still lose
Jiri Slaby's avatar
Jiri Slaby committed
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
					   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
447 448
	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
	return IRQ_HANDLED;
Jiri Slaby's avatar
Jiri Slaby committed
449 450 451
}				/* cy_rxerr_interrupt */

static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
452
{
Jiri Slaby's avatar
Jiri Slaby committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	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
473
/* CP('!'); */
474
				wake_up_interruptible(&info->open_wait);
Jiri Slaby's avatar
Jiri Slaby committed
475
			} else {
Linus Torvalds's avatar
Linus Torvalds committed
476
/* CP('@'); */
477 478 479
				tty_hangup(info->tty);
				wake_up_interruptible(&info->open_wait);
				info->flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby's avatar
Jiri Slaby committed
480
			}
Linus Torvalds's avatar
Linus Torvalds committed
481
		}
Jiri Slaby's avatar
Jiri Slaby committed
482 483 484 485 486 487 488
		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;
489
					tty_wakeup(info->tty);
Jiri Slaby's avatar
Jiri Slaby committed
490 491 492 493 494 495 496 497 498 499 500
				}
			} 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
501 502
		}
	}
Jiri Slaby's avatar
Jiri Slaby committed
503 504 505
	base_addr[CyMEOIR] = 0;
	return IRQ_HANDLED;
}				/* cy_modem_interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
506

Jiri Slaby's avatar
Jiri Slaby committed
507
static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
508
{
Jiri Slaby's avatar
Jiri Slaby committed
509 510 511 512 513
	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
514

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

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
519 520 521
	if (channel == DEBUG_PORT) {
		panic("TxInt on debug port!!!");
	}
Linus Torvalds's avatar
Linus Torvalds committed
522 523
#endif

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

Jiri Slaby's avatar
Jiri Slaby committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
	/* 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);
		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
544 545
		base_addr[CyTDR] = outch;
		char_count--;
Jiri Slaby's avatar
Jiri Slaby committed
546
		info->x_char = 0;
Linus Torvalds's avatar
Linus Torvalds committed
547 548
	}

Jiri Slaby's avatar
Jiri Slaby committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	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;
			}
		}
	}

616 617 618
	if (info->xmit_cnt < WAKEUP_CHARS)
		tty_wakeup(info->tty);

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

Jiri Slaby's avatar
Jiri Slaby committed
623
static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
624
{
Jiri Slaby's avatar
Jiri Slaby committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638
	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
639 640

#ifdef CONFIG_REMOTE_DEBUG
Jiri Slaby's avatar
Jiri Slaby committed
641 642 643 644 645 646
	if (channel == DEBUG_PORT) {
		while (char_count--) {
			data = base_addr[CyRDR];
			queueDebugChar(data);
		}
	} else
Linus Torvalds's avatar
Linus Torvalds committed
647
#endif
Jiri Slaby's avatar
Jiri Slaby committed
648 649 650 651 652 653 654 655
		/* 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
656 657

#ifdef CYCLOM_ENABLE_MONITORING
Jiri Slaby's avatar
Jiri Slaby committed
658 659 660 661 662 663 664 665 666 667
		++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
668
#ifdef CYCLOM_16Y_HACK
Jiri Slaby's avatar
Jiri Slaby committed
669
			udelay(10L);
Linus Torvalds's avatar
Linus Torvalds committed
670
#endif
Jiri Slaby's avatar
Jiri Slaby committed
671 672 673 674 675 676 677
		}
		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
678 679 680 681

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

Jiri Slaby's avatar
Jiri Slaby committed
688 689 690
	if (info->flags & ASYNC_INITIALIZED) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
691

Jiri Slaby's avatar
Jiri Slaby committed
692 693 694 695 696
	if (!info->type) {
		if (info->tty) {
			set_bit(TTY_IO_ERROR, &info->tty->flags);
		}
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
697
	}
Jiri Slaby's avatar
Jiri Slaby committed
698 699 700 701 702
	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
703 704
	}

Jiri Slaby's avatar
Jiri Slaby committed
705
	config_setup(info);
Linus Torvalds's avatar
Linus Torvalds committed
706

Jiri Slaby's avatar
Jiri Slaby committed
707
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
708 709

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

Jiri Slaby's avatar
Jiri Slaby committed
713 714 715
	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
716

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

#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
723 724 725
	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
726 727 728 729 730
#endif

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

Jiri Slaby's avatar
Jiri Slaby committed
731 732
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
733 734 735
	}
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;

Jiri Slaby's avatar
Jiri Slaby committed
736
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
737 738

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
739
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
740
#endif
Jiri Slaby's avatar
Jiri Slaby committed
741 742
	return 0;
}				/* startup */
Linus Torvalds's avatar
Linus Torvalds committed
743

Jiri Slaby's avatar
Jiri Slaby committed
744
void start_xmit(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
745
{
Jiri Slaby's avatar
Jiri Slaby committed
746 747 748
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
749

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

/*
 * 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
761
static void shutdown(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
762
{
Jiri Slaby's avatar
Jiri Slaby committed
763 764 765
	unsigned long flags;
	volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
	int channel;
Linus Torvalds's avatar
Linus Torvalds committed
766

Jiri Slaby's avatar
Jiri Slaby committed
767
	if (!(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds's avatar
Linus Torvalds committed
768
/* CP('$'); */
Jiri Slaby's avatar
Jiri Slaby committed
769 770
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
771

Jiri Slaby's avatar
Jiri Slaby committed
772
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
773 774

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

Jiri Slaby's avatar
Jiri Slaby committed
778 779 780 781 782 783 784 785 786 787
	/* !!! 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
788 789
	}

Jiri Slaby's avatar
Jiri Slaby committed
790
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
791
	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
Jiri Slaby's avatar
Jiri Slaby committed
792
		base_addr[CyMSVR1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
793
/* CP('C');CP('1'); */
Jiri Slaby's avatar
Jiri Slaby committed
794
		base_addr[CyMSVR2] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
795
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
796 797 798
		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
799
#endif
Jiri Slaby's avatar
Jiri Slaby committed
800 801 802 803
	}
	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
804

Jiri Slaby's avatar
Jiri Slaby committed
805 806
	if (info->tty) {
		set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
807 808
	}
	info->flags &= ~ASYNC_INITIALIZED;
Jiri Slaby's avatar
Jiri Slaby committed
809
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
810 811

#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby's avatar
Jiri Slaby committed
812
	printk(" done\n");
Linus Torvalds's avatar
Linus Torvalds committed
813
#endif
Jiri Slaby's avatar
Jiri Slaby committed
814
}				/* shutdown */
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818

/*
 * This routine finds or computes the various line characteristics.
 */
Jiri Slaby's avatar
Jiri Slaby committed
819
static void config_setup(struct cyclades_port *info)
Linus Torvalds's avatar
Linus Torvalds committed
820
{
Jiri Slaby's avatar
Jiri Slaby committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	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
838 839 840 841 842 843 844 845 846
#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
847 848 849 850 851
	if (i & CBAUDEX) {
		if (i == B57600)
			i = 16;
		else if (i == B115200)
			i = 18;
Linus Torvalds's avatar
Linus Torvalds committed
852
#ifdef B78600
Jiri Slaby's avatar
Jiri Slaby committed
853 854
		else if (i == B78600)
			i = 17;
Linus Torvalds's avatar
Linus Torvalds committed
855
#endif
Jiri Slaby's avatar
Jiri Slaby committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
		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
911
	} else {
Jiri Slaby's avatar
Jiri Slaby committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
		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
950
#if 0
Jiri Slaby's avatar
Jiri Slaby committed
951 952 953 954 955 956 957 958 959 960 961 962 963
	/* 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
964 965 966 967 968 969 970 971 972 973 974

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

Jiri Slaby's avatar
Jiri Slaby committed
977 978
	local_irq_save(flags);
	base_addr[CyCAR] = (u_char) channel;
Linus Torvalds's avatar
Linus Torvalds committed
979 980 981

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

Jiri Slaby's avatar
Jiri Slaby committed
986
	/* tx and rx baud rate */
Linus Torvalds's avatar
Linus Torvalds committed
987 988

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

	/* set line characteristics  according configuration */

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

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

Jiri Slaby's avatar
Jiri Slaby committed
1027
	base_addr[CyCAR] = (u_char) channel;	/* !!! Is this needed? */
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029 1030 1031

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

	/* Set up RTS here also ????? RGH 141095 */
Jiri Slaby's avatar
Jiri Slaby committed
1037 1038 1039
	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
1040
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1041 1042 1043
		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
1044
#endif
Jiri Slaby's avatar
Jiri Slaby committed
1045 1046 1047
	} else {
		if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
			base_addr[CyMSVR2] = CyDTR;
Linus Torvalds's avatar
Linus Torvalds committed
1048
#ifdef SERIAL_DEBUG_DTR
Jiri Slaby's avatar
Jiri Slaby committed
1049 1050 1051
		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
1052 1053 1054
#endif
	}

Jiri Slaby's avatar
Jiri Slaby committed
1055 1056
	if (info->tty) {
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058
	}

Jiri Slaby's avatar
Jiri Slaby committed
1059
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1060

Jiri Slaby's avatar
Jiri Slaby committed
1061
}				/* config_setup */
Linus Torvalds's avatar
Linus Torvalds committed
1062

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

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

Jiri Slaby's avatar
Jiri Slaby committed
1072
	if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1073
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1074

Jiri Slaby's avatar
Jiri Slaby committed
1075
	if (!info->xmit_buf)
1076
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1077

Jiri Slaby's avatar
Jiri Slaby committed
1078
	local_irq_save(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1079
	if (info->xmit_cnt >= PAGE_SIZE - 1) {
Jiri Slaby's avatar
Jiri Slaby committed
1080
		local_irq_restore(flags);
1081
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1082 1083 1084 1085 1086
	}

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

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

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

Jiri Slaby's avatar
Jiri Slaby committed
1102 1103
	if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1104

Jiri Slaby's avatar
Jiri Slaby committed
1105 1106 1107
	if (info->xmit_cnt <= 0 || tty->stopped
	    || tty->hw_stopped || !info->xmit_buf)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1108

Jiri Slaby's avatar
Jiri Slaby committed
1109
	channel = info->line;
Linus Torvalds's avatar
Linus Torvalds committed
1110

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

/* 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
1123
static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1124
{
Alan Cox's avatar
Alan Cox committed
1125
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1126 1127
	unsigned long flags;
	int c, total = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129

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

Jiri Slaby's avatar
Jiri Slaby committed
1133 1134 1135
	if (serial_paranoia_check(info, tty->name, "cy_write")) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1136

Jiri Slaby's avatar
Jiri Slaby committed
1137 1138 1139
	if (!info->xmit_buf) {
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1140

Jiri Slaby's avatar
Jiri Slaby committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	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
1168
{
Alan Cox's avatar
Alan Cox committed
1169
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1170 1171
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
1172
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1173
	printk("cy_write_room %s\n", tty->name);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1174 1175
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1176 1177 1178 1179 1180 1181 1182
	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
1183

Jiri Slaby's avatar
Jiri Slaby committed
1184
static int cy_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1185
{
Alan Cox's avatar
Alan Cox committed
1186
	struct cyclades_port *info = tty->driver_data;
Jiri Slaby's avatar
Jiri Slaby committed
1187

Linus Torvalds's avatar
Linus Torvalds committed
1188
#ifdef SERIAL_DEBUG_IO
Jiri Slaby's avatar
Jiri Slaby committed
1189
	printk("cy_chars_in_buffer %s %d\n", tty->name, info->xmit_cnt);	/* */
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191
#endif

Jiri Slaby's avatar
Jiri Slaby committed
1192 1193
	if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed