Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * C-Brick Serial Port (and console) driver for SGI Altix machines.
   3 *
   4 * This driver is NOT suitable for talking to the l1-controller for
   5 * anything other than 'console activities' --- please use the l1
   6 * driver for that.
   7 *
   8 *
   9 * Copyright (c) 2004-2006 Silicon Graphics, Inc.  All Rights Reserved.
  10 *
  11 * This program is free software; you can redistribute it and/or modify it
  12 * under the terms of version 2 of the GNU General Public License
  13 * as published by the Free Software Foundation.
  14 *
  15 * This program is distributed in the hope that it would be useful, but
  16 * WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18 *
  19 * Further, this software is distributed without any warranty that it is
  20 * free of the rightful claim of any third person regarding infringement
  21 * or the like.  Any license provided herein, whether implied or
  22 * otherwise, applies only to this software file.  Patent licenses, if
  23 * any, provided herein do not apply to combinations of this program with
  24 * other software, or any other product whatsoever.
  25 *
  26 * You should have received a copy of the GNU General Public
  27 * License along with this program; if not, write the Free Software
  28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  29 *
  30 * Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  31 * Mountain View, CA  94043, or:
  32 *
  33 * http://www.sgi.com
  34 *
  35 * For further information regarding this notice, see:
  36 *
  37 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
  38 */
  39
  40#include <linux/interrupt.h>
  41#include <linux/tty.h>
 
  42#include <linux/serial.h>
  43#include <linux/console.h>
  44#include <linux/module.h>
  45#include <linux/sysrq.h>
  46#include <linux/circ_buf.h>
  47#include <linux/serial_reg.h>
  48#include <linux/delay.h> /* for mdelay */
  49#include <linux/miscdevice.h>
  50#include <linux/serial_core.h>
  51
  52#include <asm/io.h>
  53#include <asm/sn/simulator.h>
  54#include <asm/sn/sn_sal.h>
  55
  56/* number of characters we can transmit to the SAL console at a time */
  57#define SN_SAL_MAX_CHARS 120
  58
  59/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
  60 * avoid losing chars, (always has to be a power of 2) */
  61#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
  62
  63#define SN_SAL_UART_FIFO_DEPTH 16
  64#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
  65
  66/* sn_transmit_chars() calling args */
  67#define TRANSMIT_BUFFERED	0
  68#define TRANSMIT_RAW		1
  69
  70/* To use dynamic numbers only and not use the assigned major and minor,
  71 * define the following.. */
  72				  /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
  73#define USE_DYNAMIC_MINOR 0	/* Don't rely on misc_register dynamic minor */
  74
  75/* Device name we're using */
  76#define DEVICE_NAME "ttySG"
  77#define DEVICE_NAME_DYNAMIC "ttySG0"	/* need full name for misc_register */
  78/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
  79#define DEVICE_MAJOR 204
  80#define DEVICE_MINOR 40
  81
  82#ifdef CONFIG_MAGIC_SYSRQ
  83static char sysrq_serial_str[] = "\eSYS";
  84static char *sysrq_serial_ptr = sysrq_serial_str;
  85static unsigned long sysrq_requested;
  86#endif /* CONFIG_MAGIC_SYSRQ */
  87
  88/*
  89 * Port definition - this kinda drives it all
  90 */
  91struct sn_cons_port {
  92	struct timer_list sc_timer;
  93	struct uart_port sc_port;
  94	struct sn_sal_ops {
  95		int (*sal_puts_raw) (const char *s, int len);
  96		int (*sal_puts) (const char *s, int len);
  97		int (*sal_getc) (void);
  98		int (*sal_input_pending) (void);
  99		void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
 100	} *sc_ops;
 101	unsigned long sc_interrupt_timeout;
 102	int sc_is_asynch;
 103};
 104
 105static struct sn_cons_port sal_console_port;
 106static int sn_process_input;
 107
 108/* Only used if USE_DYNAMIC_MINOR is set to 1 */
 109static struct miscdevice misc;	/* used with misc_register for dynamic */
 110
 111extern void early_sn_setup(void);
 112
 113#undef DEBUG
 114#ifdef DEBUG
 115static int sn_debug_printf(const char *fmt, ...);
 116#define DPRINTF(x...) sn_debug_printf(x)
 117#else
 118#define DPRINTF(x...) do { } while (0)
 119#endif
 120
 121/* Prototypes */
 122static int snt_hw_puts_raw(const char *, int);
 123static int snt_hw_puts_buffered(const char *, int);
 124static int snt_poll_getc(void);
 125static int snt_poll_input_pending(void);
 126static int snt_intr_getc(void);
 127static int snt_intr_input_pending(void);
 128static void sn_transmit_chars(struct sn_cons_port *, int);
 129
 130/* A table for polling:
 131 */
 132static struct sn_sal_ops poll_ops = {
 133	.sal_puts_raw = snt_hw_puts_raw,
 134	.sal_puts = snt_hw_puts_raw,
 135	.sal_getc = snt_poll_getc,
 136	.sal_input_pending = snt_poll_input_pending
 137};
 138
 139/* A table for interrupts enabled */
 140static struct sn_sal_ops intr_ops = {
 141	.sal_puts_raw = snt_hw_puts_raw,
 142	.sal_puts = snt_hw_puts_buffered,
 143	.sal_getc = snt_intr_getc,
 144	.sal_input_pending = snt_intr_input_pending,
 145	.sal_wakeup_transmit = sn_transmit_chars
 146};
 147
 148/* the console does output in two distinctly different ways:
 149 * synchronous (raw) and asynchronous (buffered).  initially, early_printk
 150 * does synchronous output.  any data written goes directly to the SAL
 151 * to be output (incidentally, it is internally buffered by the SAL)
 152 * after interrupts and timers are initialized and available for use,
 153 * the console init code switches to asynchronous output.  this is
 154 * also the earliest opportunity to begin polling for console input.
 155 * after console initialization, console output and tty (serial port)
 156 * output is buffered and sent to the SAL asynchronously (either by
 157 * timer callback or by UART interrupt) */
 158
 159/* routines for running the console in polling mode */
 160
 161/**
 162 * snt_poll_getc - Get a character from the console in polling mode
 163 *
 164 */
 165static int snt_poll_getc(void)
 166{
 167	int ch;
 168
 169	ia64_sn_console_getc(&ch);
 170	return ch;
 171}
 172
 173/**
 174 * snt_poll_input_pending - Check if any input is waiting - polling mode.
 175 *
 176 */
 177static int snt_poll_input_pending(void)
 178{
 179	int status, input;
 180
 181	status = ia64_sn_console_check(&input);
 182	return !status && input;
 183}
 184
 185/* routines for an interrupt driven console (normal) */
 186
 187/**
 188 * snt_intr_getc - Get a character from the console, interrupt mode
 189 *
 190 */
 191static int snt_intr_getc(void)
 192{
 193	return ia64_sn_console_readc();
 194}
 195
 196/**
 197 * snt_intr_input_pending - Check if input is pending, interrupt mode
 198 *
 199 */
 200static int snt_intr_input_pending(void)
 201{
 202	return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
 203}
 204
 205/* these functions are polled and interrupt */
 206
 207/**
 208 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
 209 * @s: String
 210 * @len: Length
 211 *
 212 */
 213static int snt_hw_puts_raw(const char *s, int len)
 214{
 215	/* this will call the PROM and not return until this is done */
 216	return ia64_sn_console_putb(s, len);
 217}
 218
 219/**
 220 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
 221 * @s: String
 222 * @len: Length
 223 *
 224 */
 225static int snt_hw_puts_buffered(const char *s, int len)
 226{
 227	/* queue data to the PROM */
 228	return ia64_sn_console_xmit_chars((char *)s, len);
 229}
 230
 231/* uart interface structs
 232 * These functions are associated with the uart_port that the serial core
 233 * infrastructure calls.
 234 *
 235 * Note: Due to how the console works, many routines are no-ops.
 236 */
 237
 238/**
 239 * snp_type - What type of console are we?
 240 * @port: Port to operate with (we ignore since we only have one port)
 241 *
 242 */
 243static const char *snp_type(struct uart_port *port)
 244{
 245	return ("SGI SN L1");
 246}
 247
 248/**
 249 * snp_tx_empty - Is the transmitter empty?  We pretend we're always empty
 250 * @port: Port to operate on (we ignore since we only have one port)
 251 *
 252 */
 253static unsigned int snp_tx_empty(struct uart_port *port)
 254{
 255	return 1;
 256}
 257
 258/**
 259 * snp_stop_tx - stop the transmitter - no-op for us
 260 * @port: Port to operat eon - we ignore - no-op function
 261 *
 262 */
 263static void snp_stop_tx(struct uart_port *port)
 264{
 265}
 266
 267/**
 268 * snp_release_port - Free i/o and resources for port - no-op for us
 269 * @port: Port to operate on - we ignore - no-op function
 270 *
 271 */
 272static void snp_release_port(struct uart_port *port)
 273{
 274}
 275
 276/**
 277 * snp_enable_ms - Force modem status interrupts on - no-op for us
 278 * @port: Port to operate on - we ignore - no-op function
 279 *
 280 */
 281static void snp_enable_ms(struct uart_port *port)
 282{
 283}
 284
 285/**
 286 * snp_shutdown - shut down the port - free irq and disable - no-op for us
 287 * @port: Port to shut down - we ignore
 288 *
 289 */
 290static void snp_shutdown(struct uart_port *port)
 291{
 292}
 293
 294/**
 295 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
 296 * @port: Port to operate on - we ignore
 297 * @mctrl: Lines to set/unset - we ignore
 298 *
 299 */
 300static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
 301{
 302}
 303
 304/**
 305 * snp_get_mctrl - get contorl line info, we just return a static value
 306 * @port: port to operate on - we only have one port so we ignore this
 307 *
 308 */
 309static unsigned int snp_get_mctrl(struct uart_port *port)
 310{
 311	return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
 312}
 313
 314/**
 315 * snp_stop_rx - Stop the receiver - we ignor ethis
 316 * @port: Port to operate on - we ignore
 317 *
 318 */
 319static void snp_stop_rx(struct uart_port *port)
 320{
 321}
 322
 323/**
 324 * snp_start_tx - Start transmitter
 325 * @port: Port to operate on
 326 *
 327 */
 328static void snp_start_tx(struct uart_port *port)
 329{
 330	if (sal_console_port.sc_ops->sal_wakeup_transmit)
 331		sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
 332							     TRANSMIT_BUFFERED);
 333
 334}
 335
 336/**
 337 * snp_break_ctl - handle breaks - ignored by us
 338 * @port: Port to operate on
 339 * @break_state: Break state
 340 *
 341 */
 342static void snp_break_ctl(struct uart_port *port, int break_state)
 343{
 344}
 345
 346/**
 347 * snp_startup - Start up the serial port - always return 0 (We're always on)
 348 * @port: Port to operate on
 349 *
 350 */
 351static int snp_startup(struct uart_port *port)
 352{
 353	return 0;
 354}
 355
 356/**
 357 * snp_set_termios - set termios stuff - we ignore these
 358 * @port: port to operate on
 359 * @termios: New settings
 360 * @termios: Old
 361 *
 362 */
 363static void
 364snp_set_termios(struct uart_port *port, struct ktermios *termios,
 365		struct ktermios *old)
 366{
 367}
 368
 369/**
 370 * snp_request_port - allocate resources for port - ignored by us
 371 * @port: port to operate on
 372 *
 373 */
 374static int snp_request_port(struct uart_port *port)
 375{
 376	return 0;
 377}
 378
 379/**
 380 * snp_config_port - allocate resources, set up - we ignore,  we're always on
 381 * @port: Port to operate on
 382 * @flags: flags used for port setup
 383 *
 384 */
 385static void snp_config_port(struct uart_port *port, int flags)
 386{
 387}
 388
 389/* Associate the uart functions above - given to serial core */
 390
 391static struct uart_ops sn_console_ops = {
 392	.tx_empty = snp_tx_empty,
 393	.set_mctrl = snp_set_mctrl,
 394	.get_mctrl = snp_get_mctrl,
 395	.stop_tx = snp_stop_tx,
 396	.start_tx = snp_start_tx,
 397	.stop_rx = snp_stop_rx,
 398	.enable_ms = snp_enable_ms,
 399	.break_ctl = snp_break_ctl,
 400	.startup = snp_startup,
 401	.shutdown = snp_shutdown,
 402	.set_termios = snp_set_termios,
 403	.pm = NULL,
 404	.type = snp_type,
 405	.release_port = snp_release_port,
 406	.request_port = snp_request_port,
 407	.config_port = snp_config_port,
 408	.verify_port = NULL,
 409};
 410
 411/* End of uart struct functions and defines */
 412
 413#ifdef DEBUG
 414
 415/**
 416 * sn_debug_printf - close to hardware debugging printf
 417 * @fmt: printf format
 418 *
 419 * This is as "close to the metal" as we can get, used when the driver
 420 * itself may be broken.
 421 *
 422 */
 423static int sn_debug_printf(const char *fmt, ...)
 424{
 425	static char printk_buf[1024];
 426	int printed_len;
 427	va_list args;
 428
 429	va_start(args, fmt);
 430	printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
 431
 432	if (!sal_console_port.sc_ops) {
 433		sal_console_port.sc_ops = &poll_ops;
 434		early_sn_setup();
 435	}
 436	sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
 437
 438	va_end(args);
 439	return printed_len;
 440}
 441#endif				/* DEBUG */
 442
 443/*
 444 * Interrupt handling routines.
 445 */
 446
 447/**
 448 * sn_receive_chars - Grab characters, pass them to tty layer
 449 * @port: Port to operate on
 450 * @flags: irq flags
 451 *
 452 * Note: If we're not registered with the serial core infrastructure yet,
 453 * we don't try to send characters to it...
 454 *
 455 */
 456static void
 457sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
 458{
 
 459	int ch;
 460	struct tty_struct *tty;
 461
 462	if (!port) {
 463		printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
 464		return;
 465	}
 466
 467	if (!port->sc_ops) {
 468		printk(KERN_ERR "sn_receive_chars - port->sc_ops  NULL so can't receieve\n");
 469		return;
 470	}
 471
 472	if (port->sc_port.state) {
 473		/* The serial_core stuffs are initialized, use them */
 474		tty = port->sc_port.state->port.tty;
 475	}
 476	else {
 477		/* Not registered yet - can't pass to tty layer.  */
 478		tty = NULL;
 479	}
 480
 481	while (port->sc_ops->sal_input_pending()) {
 482		ch = port->sc_ops->sal_getc();
 483		if (ch < 0) {
 484			printk(KERN_ERR "sn_console: An error occurred while "
 485			       "obtaining data from the console (0x%0x)\n", ch);
 486			break;
 487		}
 488#ifdef CONFIG_MAGIC_SYSRQ
 489                if (sysrq_requested) {
 490                        unsigned long sysrq_timeout = sysrq_requested + HZ*5;
 491
 492                        sysrq_requested = 0;
 493                        if (ch && time_before(jiffies, sysrq_timeout)) {
 494                                spin_unlock_irqrestore(&port->sc_port.lock, flags);
 495                                handle_sysrq(ch);
 496                                spin_lock_irqsave(&port->sc_port.lock, flags);
 497                                /* ignore actual sysrq command char */
 498                                continue;
 499                        }
 500                }
 501                if (ch == *sysrq_serial_ptr) {
 502                        if (!(*++sysrq_serial_ptr)) {
 503                                sysrq_requested = jiffies;
 504                                sysrq_serial_ptr = sysrq_serial_str;
 505                        }
 506			/*
 507			 * ignore the whole sysrq string except for the
 508			 * leading escape
 509			 */
 510			if (ch != '\e')
 511				continue;
 512                }
 513                else
 514			sysrq_serial_ptr = sysrq_serial_str;
 515#endif /* CONFIG_MAGIC_SYSRQ */
 516
 517		/* record the character to pass up to the tty layer */
 518		if (tty) {
 519			if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
 520				break;
 521		}
 522		port->sc_port.icount.rx++;
 523	}
 524
 525	if (tty)
 526		tty_flip_buffer_push(tty);
 527}
 528
 529/**
 530 * sn_transmit_chars - grab characters from serial core, send off
 531 * @port: Port to operate on
 532 * @raw: Transmit raw or buffered
 533 *
 534 * Note: If we're early, before we're registered with serial core, the
 535 * writes are going through sn_sal_console_write because that's how
 536 * register_console has been set up.  We currently could have asynch
 537 * polls calling this function due to sn_sal_switch_to_asynch but we can
 538 * ignore them until we register with the serial core stuffs.
 539 *
 540 */
 541static void sn_transmit_chars(struct sn_cons_port *port, int raw)
 542{
 543	int xmit_count, tail, head, loops, ii;
 544	int result;
 545	char *start;
 546	struct circ_buf *xmit;
 547
 548	if (!port)
 549		return;
 550
 551	BUG_ON(!port->sc_is_asynch);
 552
 553	if (port->sc_port.state) {
 554		/* We're initialized, using serial core infrastructure */
 555		xmit = &port->sc_port.state->xmit;
 556	} else {
 557		/* Probably sn_sal_switch_to_asynch has been run but serial core isn't
 558		 * initialized yet.  Just return.  Writes are going through
 559		 * sn_sal_console_write (due to register_console) at this time.
 560		 */
 561		return;
 562	}
 563
 564	if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
 565		/* Nothing to do. */
 566		ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
 567		return;
 568	}
 569
 570	head = xmit->head;
 571	tail = xmit->tail;
 572	start = &xmit->buf[tail];
 573
 574	/* twice around gets the tail to the end of the buffer and
 575	 * then to the head, if needed */
 576	loops = (head < tail) ? 2 : 1;
 577
 578	for (ii = 0; ii < loops; ii++) {
 579		xmit_count = (head < tail) ?
 580		    (UART_XMIT_SIZE - tail) : (head - tail);
 581
 582		if (xmit_count > 0) {
 583			if (raw == TRANSMIT_RAW)
 584				result =
 585				    port->sc_ops->sal_puts_raw(start,
 586							       xmit_count);
 587			else
 588				result =
 589				    port->sc_ops->sal_puts(start, xmit_count);
 590#ifdef DEBUG
 591			if (!result)
 592				DPRINTF("`");
 593#endif
 594			if (result > 0) {
 595				xmit_count -= result;
 596				port->sc_port.icount.tx += result;
 597				tail += result;
 598				tail &= UART_XMIT_SIZE - 1;
 599				xmit->tail = tail;
 600				start = &xmit->buf[tail];
 601			}
 602		}
 603	}
 604
 605	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
 606		uart_write_wakeup(&port->sc_port);
 607
 608	if (uart_circ_empty(xmit))
 609		snp_stop_tx(&port->sc_port);	/* no-op for us */
 610}
 611
 612/**
 613 * sn_sal_interrupt - Handle console interrupts
 614 * @irq: irq #, useful for debug statements
 615 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
 616 *
 617 */
 618static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
 619{
 620	struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
 621	unsigned long flags;
 622	int status = ia64_sn_console_intr_status();
 623
 624	if (!port)
 625		return IRQ_NONE;
 626
 627	spin_lock_irqsave(&port->sc_port.lock, flags);
 628	if (status & SAL_CONSOLE_INTR_RECV) {
 629		sn_receive_chars(port, flags);
 630	}
 631	if (status & SAL_CONSOLE_INTR_XMIT) {
 632		sn_transmit_chars(port, TRANSMIT_BUFFERED);
 633	}
 634	spin_unlock_irqrestore(&port->sc_port.lock, flags);
 635	return IRQ_HANDLED;
 636}
 637
 638/**
 639 * sn_sal_timer_poll - this function handles polled console mode
 640 * @data: A pointer to our sn_cons_port (which contains the uart port)
 641 *
 642 * data is the pointer that init_timer will store for us.  This function is
 643 * associated with init_timer to see if there is any console traffic.
 644 * Obviously not used in interrupt mode
 645 *
 646 */
 647static void sn_sal_timer_poll(unsigned long data)
 648{
 649	struct sn_cons_port *port = (struct sn_cons_port *)data;
 650	unsigned long flags;
 651
 652	if (!port)
 653		return;
 654
 655	if (!port->sc_port.irq) {
 656		spin_lock_irqsave(&port->sc_port.lock, flags);
 657		if (sn_process_input)
 658			sn_receive_chars(port, flags);
 659		sn_transmit_chars(port, TRANSMIT_RAW);
 660		spin_unlock_irqrestore(&port->sc_port.lock, flags);
 661		mod_timer(&port->sc_timer,
 662			  jiffies + port->sc_interrupt_timeout);
 663	}
 664}
 665
 666/*
 667 * Boot-time initialization code
 668 */
 669
 670/**
 671 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
 672 * @port: Our sn_cons_port (which contains the uart port)
 673 *
 674 * So this is used by sn_sal_serial_console_init (early on, before we're
 675 * registered with serial core).  It's also used by sn_sal_module_init
 676 * right after we've registered with serial core.  The later only happens
 677 * if we didn't already come through here via sn_sal_serial_console_init.
 678 *
 679 */
 680static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
 681{
 682	unsigned long flags;
 683
 684	if (!port)
 685		return;
 686
 687	DPRINTF("sn_console: about to switch to asynchronous console\n");
 688
 689	/* without early_printk, we may be invoked late enough to race
 690	 * with other cpus doing console IO at this point, however
 691	 * console interrupts will never be enabled */
 692	spin_lock_irqsave(&port->sc_port.lock, flags);
 693
 694	/* early_printk invocation may have done this for us */
 695	if (!port->sc_ops)
 696		port->sc_ops = &poll_ops;
 697
 698	/* we can't turn on the console interrupt (as request_irq
 699	 * calls kmalloc, which isn't set up yet), so we rely on a
 700	 * timer to poll for input and push data from the console
 701	 * buffer.
 702	 */
 703	init_timer(&port->sc_timer);
 704	port->sc_timer.function = sn_sal_timer_poll;
 705	port->sc_timer.data = (unsigned long)port;
 706
 707	if (IS_RUNNING_ON_SIMULATOR())
 708		port->sc_interrupt_timeout = 6;
 709	else {
 710		/* 960cps / 16 char FIFO = 60HZ
 711		 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
 712		port->sc_interrupt_timeout =
 713		    HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
 714	}
 715	mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
 716
 717	port->sc_is_asynch = 1;
 718	spin_unlock_irqrestore(&port->sc_port.lock, flags);
 719}
 720
 721/**
 722 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
 723 * @port: Our sn_cons_port (which contains the uart port)
 724 *
 725 * In sn_sal_module_init, after we're registered with serial core and
 726 * the port is added, this function is called to switch us to interrupt
 727 * mode.  We were previously in asynch/polling mode (using init_timer).
 728 *
 729 * We attempt to switch to interrupt mode here by calling
 730 * request_irq.  If that works out, we enable receive interrupts.
 731 */
 732static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
 733{
 734	unsigned long flags;
 735
 736	if (port) {
 737		DPRINTF("sn_console: switching to interrupt driven console\n");
 738
 739		if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
 740				IRQF_DISABLED | IRQF_SHARED,
 741				"SAL console driver", port) >= 0) {
 742			spin_lock_irqsave(&port->sc_port.lock, flags);
 743			port->sc_port.irq = SGI_UART_VECTOR;
 744			port->sc_ops = &intr_ops;
 
 745
 746			/* turn on receive interrupts */
 747			ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
 748			spin_unlock_irqrestore(&port->sc_port.lock, flags);
 749		}
 750		else {
 751			printk(KERN_INFO
 752			    "sn_console: console proceeding in polled mode\n");
 753		}
 754	}
 755}
 756
 757/*
 758 * Kernel console definitions
 759 */
 760
 761static void sn_sal_console_write(struct console *, const char *, unsigned);
 762static int sn_sal_console_setup(struct console *, char *);
 763static struct uart_driver sal_console_uart;
 764extern struct tty_driver *uart_console_device(struct console *, int *);
 765
 766static struct console sal_console = {
 767	.name = DEVICE_NAME,
 768	.write = sn_sal_console_write,
 769	.device = uart_console_device,
 770	.setup = sn_sal_console_setup,
 771	.index = -1,		/* unspecified */
 772	.data = &sal_console_uart,
 773};
 774
 775#define SAL_CONSOLE	&sal_console
 776
 777static struct uart_driver sal_console_uart = {
 778	.owner = THIS_MODULE,
 779	.driver_name = "sn_console",
 780	.dev_name = DEVICE_NAME,
 781	.major = 0,		/* major/minor set at registration time per USE_DYNAMIC_MINOR */
 782	.minor = 0,
 783	.nr = 1,		/* one port */
 784	.cons = SAL_CONSOLE,
 785};
 786
 787/**
 788 * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
 789 *
 790 * Before this is called, we've been printing kernel messages in a special
 791 * early mode not making use of the serial core infrastructure.  When our
 792 * driver is loaded for real, we register the driver and port with serial
 793 * core and try to enable interrupt driven mode.
 794 *
 795 */
 796static int __init sn_sal_module_init(void)
 797{
 798	int retval;
 799
 800	if (!ia64_platform_is("sn2"))
 801		return 0;
 802
 803	printk(KERN_INFO "sn_console: Console driver init\n");
 804
 805	if (USE_DYNAMIC_MINOR == 1) {
 806		misc.minor = MISC_DYNAMIC_MINOR;
 807		misc.name = DEVICE_NAME_DYNAMIC;
 808		retval = misc_register(&misc);
 809		if (retval != 0) {
 810			printk(KERN_WARNING "Failed to register console "
 811			       "device using misc_register.\n");
 812			return -ENODEV;
 813		}
 814		sal_console_uart.major = MISC_MAJOR;
 815		sal_console_uart.minor = misc.minor;
 816	} else {
 817		sal_console_uart.major = DEVICE_MAJOR;
 818		sal_console_uart.minor = DEVICE_MINOR;
 819	}
 820
 821	/* We register the driver and the port before switching to interrupts
 822	 * or async above so the proper uart structures are populated */
 823
 824	if (uart_register_driver(&sal_console_uart) < 0) {
 825		printk
 826		    ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
 827		     __LINE__);
 828		return -ENODEV;
 829	}
 830
 831	spin_lock_init(&sal_console_port.sc_port.lock);
 832
 833	/* Setup the port struct with the minimum needed */
 834	sal_console_port.sc_port.membase = (char *)1;	/* just needs to be non-zero */
 835	sal_console_port.sc_port.type = PORT_16550A;
 836	sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
 837	sal_console_port.sc_port.ops = &sn_console_ops;
 838	sal_console_port.sc_port.line = 0;
 839
 840	if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
 841		/* error - not sure what I'd do - so I'll do nothing */
 842		printk(KERN_ERR "%s: unable to add port\n", __func__);
 843	}
 844
 845	/* when this driver is compiled in, the console initialization
 846	 * will have already switched us into asynchronous operation
 847	 * before we get here through the module initcalls */
 848	if (!sal_console_port.sc_is_asynch) {
 849		sn_sal_switch_to_asynch(&sal_console_port);
 850	}
 851
 852	/* at this point (module_init) we can try to turn on interrupts */
 853	if (!IS_RUNNING_ON_SIMULATOR()) {
 854		sn_sal_switch_to_interrupts(&sal_console_port);
 855	}
 856	sn_process_input = 1;
 857	return 0;
 858}
 859
 860/**
 861 * sn_sal_module_exit - When we're unloaded, remove the driver/port
 862 *
 863 */
 864static void __exit sn_sal_module_exit(void)
 865{
 866	del_timer_sync(&sal_console_port.sc_timer);
 867	uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
 868	uart_unregister_driver(&sal_console_uart);
 869	misc_deregister(&misc);
 870}
 871
 872module_init(sn_sal_module_init);
 873module_exit(sn_sal_module_exit);
 874
 875/**
 876 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
 877 * @puts_raw : puts function to do the writing
 878 * @s: input string
 879 * @count: length
 880 *
 881 * We need a \r ahead of every \n for direct writes through
 882 * ia64_sn_console_putb (what sal_puts_raw below actually does).
 883 *
 884 */
 885
 886static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
 887			   const char *s, int count)
 888{
 889	const char *s1;
 890
 891	/* Output '\r' before each '\n' */
 892	while ((s1 = memchr(s, '\n', count)) != NULL) {
 893		puts_raw(s, s1 - s);
 894		puts_raw("\r\n", 2);
 895		count -= s1 + 1 - s;
 896		s = s1 + 1;
 897	}
 898	puts_raw(s, count);
 899}
 900
 901/**
 902 * sn_sal_console_write - Print statements before serial core available
 903 * @console: Console to operate on - we ignore since we have just one
 904 * @s: String to send
 905 * @count: length
 906 *
 907 * This is referenced in the console struct.  It is used for early
 908 * console printing before we register with serial core and for things
 909 * such as kdb.  The console_lock must be held when we get here.
 910 *
 911 * This function has some code for trying to print output even if the lock
 912 * is held.  We try to cover the case where a lock holder could have died.
 913 * We don't use this special case code if we're not registered with serial
 914 * core yet.  After we're registered with serial core, the only time this
 915 * function would be used is for high level kernel output like magic sys req,
 916 * kdb, and printk's.
 917 */
 918static void
 919sn_sal_console_write(struct console *co, const char *s, unsigned count)
 920{
 921	unsigned long flags = 0;
 922	struct sn_cons_port *port = &sal_console_port;
 923	static int stole_lock = 0;
 924
 925	BUG_ON(!port->sc_is_asynch);
 926
 927	/* We can't look at the xmit buffer if we're not registered with serial core
 928	 *  yet.  So only do the fancy recovery after registering
 929	 */
 930	if (!port->sc_port.state) {
 931		/* Not yet registered with serial core - simple case */
 932		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 933		return;
 934	}
 935
 936	/* somebody really wants this output, might be an
 937	 * oops, kdb, panic, etc.  make sure they get it. */
 938	if (spin_is_locked(&port->sc_port.lock)) {
 939		int lhead = port->sc_port.state->xmit.head;
 940		int ltail = port->sc_port.state->xmit.tail;
 941		int counter, got_lock = 0;
 942
 943		/*
 944		 * We attempt to determine if someone has died with the
 945		 * lock. We wait ~20 secs after the head and tail ptrs
 946		 * stop moving and assume the lock holder is not functional
 947		 * and plow ahead. If the lock is freed within the time out
 948		 * period we re-get the lock and go ahead normally. We also
 949		 * remember if we have plowed ahead so that we don't have
 950		 * to wait out the time out period again - the asumption
 951		 * is that we will time out again.
 952		 */
 953
 954		for (counter = 0; counter < 150; mdelay(125), counter++) {
 955			if (!spin_is_locked(&port->sc_port.lock)
 956			    || stole_lock) {
 957				if (!stole_lock) {
 958					spin_lock_irqsave(&port->sc_port.lock,
 959							  flags);
 960					got_lock = 1;
 961				}
 962				break;
 963			} else {
 964				/* still locked */
 965				if ((lhead != port->sc_port.state->xmit.head)
 966				    || (ltail !=
 967					port->sc_port.state->xmit.tail)) {
 968					lhead =
 969						port->sc_port.state->xmit.head;
 970					ltail =
 971						port->sc_port.state->xmit.tail;
 972					counter = 0;
 973				}
 974			}
 975		}
 976		/* flush anything in the serial core xmit buffer, raw */
 977		sn_transmit_chars(port, 1);
 978		if (got_lock) {
 979			spin_unlock_irqrestore(&port->sc_port.lock, flags);
 980			stole_lock = 0;
 981		} else {
 982			/* fell thru */
 983			stole_lock = 1;
 984		}
 985		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 986	} else {
 987		stole_lock = 0;
 988		spin_lock_irqsave(&port->sc_port.lock, flags);
 989		sn_transmit_chars(port, 1);
 990		spin_unlock_irqrestore(&port->sc_port.lock, flags);
 991
 992		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 993	}
 994}
 995
 996
 997/**
 998 * sn_sal_console_setup - Set up console for early printing
 999 * @co: Console to work with
1000 * @options: Options to set
1001 *
1002 * Altix console doesn't do anything with baud rates, etc, anyway.
1003 *
1004 * This isn't required since not providing the setup function in the
1005 * console struct is ok.  However, other patches like KDB plop something
1006 * here so providing it is easier.
1007 *
1008 */
1009static int sn_sal_console_setup(struct console *co, char *options)
1010{
1011	return 0;
1012}
1013
1014/**
1015 * sn_sal_console_write_early - simple early output routine
1016 * @co - console struct
1017 * @s - string to print
1018 * @count - count
1019 *
1020 * Simple function to provide early output, before even
1021 * sn_sal_serial_console_init is called.  Referenced in the
1022 * console struct registerd in sn_serial_console_early_setup.
1023 *
1024 */
1025static void __init
1026sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1027{
1028	puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1029}
1030
1031/* Used for very early console printing - again, before
1032 * sn_sal_serial_console_init is run */
1033static struct console sal_console_early __initdata = {
1034	.name = "sn_sal",
1035	.write = sn_sal_console_write_early,
1036	.flags = CON_PRINTBUFFER,
1037	.index = -1,
1038};
1039
1040/**
1041 * sn_serial_console_early_setup - Sets up early console output support
1042 *
1043 * Register a console early on...  This is for output before even
1044 * sn_sal_serial_cosnole_init is called.  This function is called from
1045 * setup.c.  This allows us to do really early polled writes. When
1046 * sn_sal_serial_console_init is called, this console is unregistered
1047 * and a new one registered.
1048 */
1049int __init sn_serial_console_early_setup(void)
1050{
1051	if (!ia64_platform_is("sn2"))
1052		return -1;
1053
1054	sal_console_port.sc_ops = &poll_ops;
1055	spin_lock_init(&sal_console_port.sc_port.lock);
1056	early_sn_setup();	/* Find SAL entry points */
1057	register_console(&sal_console_early);
1058
1059	return 0;
1060}
1061
1062/**
1063 * sn_sal_serial_console_init - Early console output - set up for register
1064 *
1065 * This function is called when regular console init happens.  Because we
1066 * support even earlier console output with sn_serial_console_early_setup
1067 * (called from setup.c directly), this function unregisters the really
1068 * early console.
1069 *
1070 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1071 * it here doesn't hurt anything.
1072 *
1073 */
1074static int __init sn_sal_serial_console_init(void)
1075{
1076	if (ia64_platform_is("sn2")) {
1077		sn_sal_switch_to_asynch(&sal_console_port);
1078		DPRINTF("sn_sal_serial_console_init : register console\n");
1079		register_console(&sal_console);
1080		unregister_console(&sal_console_early);
1081	}
1082	return 0;
1083}
1084
1085console_initcall(sn_sal_serial_console_init);
v4.6
   1/*
   2 * C-Brick Serial Port (and console) driver for SGI Altix machines.
   3 *
   4 * This driver is NOT suitable for talking to the l1-controller for
   5 * anything other than 'console activities' --- please use the l1
   6 * driver for that.
   7 *
   8 *
   9 * Copyright (c) 2004-2006 Silicon Graphics, Inc.  All Rights Reserved.
  10 *
  11 * This program is free software; you can redistribute it and/or modify it
  12 * under the terms of version 2 of the GNU General Public License
  13 * as published by the Free Software Foundation.
  14 *
  15 * This program is distributed in the hope that it would be useful, but
  16 * WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18 *
  19 * Further, this software is distributed without any warranty that it is
  20 * free of the rightful claim of any third person regarding infringement
  21 * or the like.  Any license provided herein, whether implied or
  22 * otherwise, applies only to this software file.  Patent licenses, if
  23 * any, provided herein do not apply to combinations of this program with
  24 * other software, or any other product whatsoever.
  25 *
  26 * You should have received a copy of the GNU General Public
  27 * License along with this program; if not, write the Free Software
  28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  29 *
  30 * Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  31 * Mountain View, CA  94043, or:
  32 *
  33 * http://www.sgi.com
  34 *
  35 * For further information regarding this notice, see:
  36 *
  37 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
  38 */
  39
  40#include <linux/interrupt.h>
  41#include <linux/tty.h>
  42#include <linux/tty_flip.h>
  43#include <linux/serial.h>
  44#include <linux/console.h>
  45#include <linux/init.h>
  46#include <linux/sysrq.h>
  47#include <linux/circ_buf.h>
  48#include <linux/serial_reg.h>
  49#include <linux/delay.h> /* for mdelay */
  50#include <linux/miscdevice.h>
  51#include <linux/serial_core.h>
  52
  53#include <asm/io.h>
  54#include <asm/sn/simulator.h>
  55#include <asm/sn/sn_sal.h>
  56
  57/* number of characters we can transmit to the SAL console at a time */
  58#define SN_SAL_MAX_CHARS 120
  59
  60/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
  61 * avoid losing chars, (always has to be a power of 2) */
  62#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
  63
  64#define SN_SAL_UART_FIFO_DEPTH 16
  65#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
  66
  67/* sn_transmit_chars() calling args */
  68#define TRANSMIT_BUFFERED	0
  69#define TRANSMIT_RAW		1
  70
  71/* To use dynamic numbers only and not use the assigned major and minor,
  72 * define the following.. */
  73				  /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
  74#define USE_DYNAMIC_MINOR 0	/* Don't rely on misc_register dynamic minor */
  75
  76/* Device name we're using */
  77#define DEVICE_NAME "ttySG"
  78#define DEVICE_NAME_DYNAMIC "ttySG0"	/* need full name for misc_register */
  79/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
  80#define DEVICE_MAJOR 204
  81#define DEVICE_MINOR 40
  82
  83#ifdef CONFIG_MAGIC_SYSRQ
  84static char sysrq_serial_str[] = "\eSYS";
  85static char *sysrq_serial_ptr = sysrq_serial_str;
  86static unsigned long sysrq_requested;
  87#endif /* CONFIG_MAGIC_SYSRQ */
  88
  89/*
  90 * Port definition - this kinda drives it all
  91 */
  92struct sn_cons_port {
  93	struct timer_list sc_timer;
  94	struct uart_port sc_port;
  95	struct sn_sal_ops {
  96		int (*sal_puts_raw) (const char *s, int len);
  97		int (*sal_puts) (const char *s, int len);
  98		int (*sal_getc) (void);
  99		int (*sal_input_pending) (void);
 100		void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
 101	} *sc_ops;
 102	unsigned long sc_interrupt_timeout;
 103	int sc_is_asynch;
 104};
 105
 106static struct sn_cons_port sal_console_port;
 107static int sn_process_input;
 108
 109/* Only used if USE_DYNAMIC_MINOR is set to 1 */
 110static struct miscdevice misc;	/* used with misc_register for dynamic */
 111
 112extern void early_sn_setup(void);
 113
 114#undef DEBUG
 115#ifdef DEBUG
 116static int sn_debug_printf(const char *fmt, ...);
 117#define DPRINTF(x...) sn_debug_printf(x)
 118#else
 119#define DPRINTF(x...) do { } while (0)
 120#endif
 121
 122/* Prototypes */
 123static int snt_hw_puts_raw(const char *, int);
 124static int snt_hw_puts_buffered(const char *, int);
 125static int snt_poll_getc(void);
 126static int snt_poll_input_pending(void);
 127static int snt_intr_getc(void);
 128static int snt_intr_input_pending(void);
 129static void sn_transmit_chars(struct sn_cons_port *, int);
 130
 131/* A table for polling:
 132 */
 133static struct sn_sal_ops poll_ops = {
 134	.sal_puts_raw = snt_hw_puts_raw,
 135	.sal_puts = snt_hw_puts_raw,
 136	.sal_getc = snt_poll_getc,
 137	.sal_input_pending = snt_poll_input_pending
 138};
 139
 140/* A table for interrupts enabled */
 141static struct sn_sal_ops intr_ops = {
 142	.sal_puts_raw = snt_hw_puts_raw,
 143	.sal_puts = snt_hw_puts_buffered,
 144	.sal_getc = snt_intr_getc,
 145	.sal_input_pending = snt_intr_input_pending,
 146	.sal_wakeup_transmit = sn_transmit_chars
 147};
 148
 149/* the console does output in two distinctly different ways:
 150 * synchronous (raw) and asynchronous (buffered).  initially, early_printk
 151 * does synchronous output.  any data written goes directly to the SAL
 152 * to be output (incidentally, it is internally buffered by the SAL)
 153 * after interrupts and timers are initialized and available for use,
 154 * the console init code switches to asynchronous output.  this is
 155 * also the earliest opportunity to begin polling for console input.
 156 * after console initialization, console output and tty (serial port)
 157 * output is buffered and sent to the SAL asynchronously (either by
 158 * timer callback or by UART interrupt) */
 159
 160/* routines for running the console in polling mode */
 161
 162/**
 163 * snt_poll_getc - Get a character from the console in polling mode
 164 *
 165 */
 166static int snt_poll_getc(void)
 167{
 168	int ch;
 169
 170	ia64_sn_console_getc(&ch);
 171	return ch;
 172}
 173
 174/**
 175 * snt_poll_input_pending - Check if any input is waiting - polling mode.
 176 *
 177 */
 178static int snt_poll_input_pending(void)
 179{
 180	int status, input;
 181
 182	status = ia64_sn_console_check(&input);
 183	return !status && input;
 184}
 185
 186/* routines for an interrupt driven console (normal) */
 187
 188/**
 189 * snt_intr_getc - Get a character from the console, interrupt mode
 190 *
 191 */
 192static int snt_intr_getc(void)
 193{
 194	return ia64_sn_console_readc();
 195}
 196
 197/**
 198 * snt_intr_input_pending - Check if input is pending, interrupt mode
 199 *
 200 */
 201static int snt_intr_input_pending(void)
 202{
 203	return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
 204}
 205
 206/* these functions are polled and interrupt */
 207
 208/**
 209 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
 210 * @s: String
 211 * @len: Length
 212 *
 213 */
 214static int snt_hw_puts_raw(const char *s, int len)
 215{
 216	/* this will call the PROM and not return until this is done */
 217	return ia64_sn_console_putb(s, len);
 218}
 219
 220/**
 221 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
 222 * @s: String
 223 * @len: Length
 224 *
 225 */
 226static int snt_hw_puts_buffered(const char *s, int len)
 227{
 228	/* queue data to the PROM */
 229	return ia64_sn_console_xmit_chars((char *)s, len);
 230}
 231
 232/* uart interface structs
 233 * These functions are associated with the uart_port that the serial core
 234 * infrastructure calls.
 235 *
 236 * Note: Due to how the console works, many routines are no-ops.
 237 */
 238
 239/**
 240 * snp_type - What type of console are we?
 241 * @port: Port to operate with (we ignore since we only have one port)
 242 *
 243 */
 244static const char *snp_type(struct uart_port *port)
 245{
 246	return ("SGI SN L1");
 247}
 248
 249/**
 250 * snp_tx_empty - Is the transmitter empty?  We pretend we're always empty
 251 * @port: Port to operate on (we ignore since we only have one port)
 252 *
 253 */
 254static unsigned int snp_tx_empty(struct uart_port *port)
 255{
 256	return 1;
 257}
 258
 259/**
 260 * snp_stop_tx - stop the transmitter - no-op for us
 261 * @port: Port to operat eon - we ignore - no-op function
 262 *
 263 */
 264static void snp_stop_tx(struct uart_port *port)
 265{
 266}
 267
 268/**
 269 * snp_release_port - Free i/o and resources for port - no-op for us
 270 * @port: Port to operate on - we ignore - no-op function
 271 *
 272 */
 273static void snp_release_port(struct uart_port *port)
 274{
 275}
 276
 277/**
 
 
 
 
 
 
 
 
 
 278 * snp_shutdown - shut down the port - free irq and disable - no-op for us
 279 * @port: Port to shut down - we ignore
 280 *
 281 */
 282static void snp_shutdown(struct uart_port *port)
 283{
 284}
 285
 286/**
 287 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
 288 * @port: Port to operate on - we ignore
 289 * @mctrl: Lines to set/unset - we ignore
 290 *
 291 */
 292static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
 293{
 294}
 295
 296/**
 297 * snp_get_mctrl - get contorl line info, we just return a static value
 298 * @port: port to operate on - we only have one port so we ignore this
 299 *
 300 */
 301static unsigned int snp_get_mctrl(struct uart_port *port)
 302{
 303	return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
 304}
 305
 306/**
 307 * snp_stop_rx - Stop the receiver - we ignor ethis
 308 * @port: Port to operate on - we ignore
 309 *
 310 */
 311static void snp_stop_rx(struct uart_port *port)
 312{
 313}
 314
 315/**
 316 * snp_start_tx - Start transmitter
 317 * @port: Port to operate on
 318 *
 319 */
 320static void snp_start_tx(struct uart_port *port)
 321{
 322	if (sal_console_port.sc_ops->sal_wakeup_transmit)
 323		sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
 324							     TRANSMIT_BUFFERED);
 325
 326}
 327
 328/**
 329 * snp_break_ctl - handle breaks - ignored by us
 330 * @port: Port to operate on
 331 * @break_state: Break state
 332 *
 333 */
 334static void snp_break_ctl(struct uart_port *port, int break_state)
 335{
 336}
 337
 338/**
 339 * snp_startup - Start up the serial port - always return 0 (We're always on)
 340 * @port: Port to operate on
 341 *
 342 */
 343static int snp_startup(struct uart_port *port)
 344{
 345	return 0;
 346}
 347
 348/**
 349 * snp_set_termios - set termios stuff - we ignore these
 350 * @port: port to operate on
 351 * @termios: New settings
 352 * @termios: Old
 353 *
 354 */
 355static void
 356snp_set_termios(struct uart_port *port, struct ktermios *termios,
 357		struct ktermios *old)
 358{
 359}
 360
 361/**
 362 * snp_request_port - allocate resources for port - ignored by us
 363 * @port: port to operate on
 364 *
 365 */
 366static int snp_request_port(struct uart_port *port)
 367{
 368	return 0;
 369}
 370
 371/**
 372 * snp_config_port - allocate resources, set up - we ignore,  we're always on
 373 * @port: Port to operate on
 374 * @flags: flags used for port setup
 375 *
 376 */
 377static void snp_config_port(struct uart_port *port, int flags)
 378{
 379}
 380
 381/* Associate the uart functions above - given to serial core */
 382
 383static struct uart_ops sn_console_ops = {
 384	.tx_empty = snp_tx_empty,
 385	.set_mctrl = snp_set_mctrl,
 386	.get_mctrl = snp_get_mctrl,
 387	.stop_tx = snp_stop_tx,
 388	.start_tx = snp_start_tx,
 389	.stop_rx = snp_stop_rx,
 
 390	.break_ctl = snp_break_ctl,
 391	.startup = snp_startup,
 392	.shutdown = snp_shutdown,
 393	.set_termios = snp_set_termios,
 394	.pm = NULL,
 395	.type = snp_type,
 396	.release_port = snp_release_port,
 397	.request_port = snp_request_port,
 398	.config_port = snp_config_port,
 399	.verify_port = NULL,
 400};
 401
 402/* End of uart struct functions and defines */
 403
 404#ifdef DEBUG
 405
 406/**
 407 * sn_debug_printf - close to hardware debugging printf
 408 * @fmt: printf format
 409 *
 410 * This is as "close to the metal" as we can get, used when the driver
 411 * itself may be broken.
 412 *
 413 */
 414static int sn_debug_printf(const char *fmt, ...)
 415{
 416	static char printk_buf[1024];
 417	int printed_len;
 418	va_list args;
 419
 420	va_start(args, fmt);
 421	printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
 422
 423	if (!sal_console_port.sc_ops) {
 424		sal_console_port.sc_ops = &poll_ops;
 425		early_sn_setup();
 426	}
 427	sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
 428
 429	va_end(args);
 430	return printed_len;
 431}
 432#endif				/* DEBUG */
 433
 434/*
 435 * Interrupt handling routines.
 436 */
 437
 438/**
 439 * sn_receive_chars - Grab characters, pass them to tty layer
 440 * @port: Port to operate on
 441 * @flags: irq flags
 442 *
 443 * Note: If we're not registered with the serial core infrastructure yet,
 444 * we don't try to send characters to it...
 445 *
 446 */
 447static void
 448sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
 449{
 450	struct tty_port *tport = NULL;
 451	int ch;
 
 452
 453	if (!port) {
 454		printk(KERN_ERR "sn_receive_chars - port NULL so can't receive\n");
 455		return;
 456	}
 457
 458	if (!port->sc_ops) {
 459		printk(KERN_ERR "sn_receive_chars - port->sc_ops  NULL so can't receive\n");
 460		return;
 461	}
 462
 463	if (port->sc_port.state) {
 464		/* The serial_core stuffs are initialized, use them */
 465		tport = &port->sc_port.state->port;
 
 
 
 
 466	}
 467
 468	while (port->sc_ops->sal_input_pending()) {
 469		ch = port->sc_ops->sal_getc();
 470		if (ch < 0) {
 471			printk(KERN_ERR "sn_console: An error occurred while "
 472			       "obtaining data from the console (0x%0x)\n", ch);
 473			break;
 474		}
 475#ifdef CONFIG_MAGIC_SYSRQ
 476                if (sysrq_requested) {
 477                        unsigned long sysrq_timeout = sysrq_requested + HZ*5;
 478
 479                        sysrq_requested = 0;
 480                        if (ch && time_before(jiffies, sysrq_timeout)) {
 481                                spin_unlock_irqrestore(&port->sc_port.lock, flags);
 482                                handle_sysrq(ch);
 483                                spin_lock_irqsave(&port->sc_port.lock, flags);
 484                                /* ignore actual sysrq command char */
 485                                continue;
 486                        }
 487                }
 488                if (ch == *sysrq_serial_ptr) {
 489                        if (!(*++sysrq_serial_ptr)) {
 490                                sysrq_requested = jiffies;
 491                                sysrq_serial_ptr = sysrq_serial_str;
 492                        }
 493			/*
 494			 * ignore the whole sysrq string except for the
 495			 * leading escape
 496			 */
 497			if (ch != '\e')
 498				continue;
 499                }
 500                else
 501			sysrq_serial_ptr = sysrq_serial_str;
 502#endif /* CONFIG_MAGIC_SYSRQ */
 503
 504		/* record the character to pass up to the tty layer */
 505		if (tport) {
 506			if (tty_insert_flip_char(tport, ch, TTY_NORMAL) == 0)
 507				break;
 508		}
 509		port->sc_port.icount.rx++;
 510	}
 511
 512	if (tport)
 513		tty_flip_buffer_push(tport);
 514}
 515
 516/**
 517 * sn_transmit_chars - grab characters from serial core, send off
 518 * @port: Port to operate on
 519 * @raw: Transmit raw or buffered
 520 *
 521 * Note: If we're early, before we're registered with serial core, the
 522 * writes are going through sn_sal_console_write because that's how
 523 * register_console has been set up.  We currently could have asynch
 524 * polls calling this function due to sn_sal_switch_to_asynch but we can
 525 * ignore them until we register with the serial core stuffs.
 526 *
 527 */
 528static void sn_transmit_chars(struct sn_cons_port *port, int raw)
 529{
 530	int xmit_count, tail, head, loops, ii;
 531	int result;
 532	char *start;
 533	struct circ_buf *xmit;
 534
 535	if (!port)
 536		return;
 537
 538	BUG_ON(!port->sc_is_asynch);
 539
 540	if (port->sc_port.state) {
 541		/* We're initialized, using serial core infrastructure */
 542		xmit = &port->sc_port.state->xmit;
 543	} else {
 544		/* Probably sn_sal_switch_to_asynch has been run but serial core isn't
 545		 * initialized yet.  Just return.  Writes are going through
 546		 * sn_sal_console_write (due to register_console) at this time.
 547		 */
 548		return;
 549	}
 550
 551	if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
 552		/* Nothing to do. */
 553		ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
 554		return;
 555	}
 556
 557	head = xmit->head;
 558	tail = xmit->tail;
 559	start = &xmit->buf[tail];
 560
 561	/* twice around gets the tail to the end of the buffer and
 562	 * then to the head, if needed */
 563	loops = (head < tail) ? 2 : 1;
 564
 565	for (ii = 0; ii < loops; ii++) {
 566		xmit_count = (head < tail) ?
 567		    (UART_XMIT_SIZE - tail) : (head - tail);
 568
 569		if (xmit_count > 0) {
 570			if (raw == TRANSMIT_RAW)
 571				result =
 572				    port->sc_ops->sal_puts_raw(start,
 573							       xmit_count);
 574			else
 575				result =
 576				    port->sc_ops->sal_puts(start, xmit_count);
 577#ifdef DEBUG
 578			if (!result)
 579				DPRINTF("`");
 580#endif
 581			if (result > 0) {
 582				xmit_count -= result;
 583				port->sc_port.icount.tx += result;
 584				tail += result;
 585				tail &= UART_XMIT_SIZE - 1;
 586				xmit->tail = tail;
 587				start = &xmit->buf[tail];
 588			}
 589		}
 590	}
 591
 592	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
 593		uart_write_wakeup(&port->sc_port);
 594
 595	if (uart_circ_empty(xmit))
 596		snp_stop_tx(&port->sc_port);	/* no-op for us */
 597}
 598
 599/**
 600 * sn_sal_interrupt - Handle console interrupts
 601 * @irq: irq #, useful for debug statements
 602 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
 603 *
 604 */
 605static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
 606{
 607	struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
 608	unsigned long flags;
 609	int status = ia64_sn_console_intr_status();
 610
 611	if (!port)
 612		return IRQ_NONE;
 613
 614	spin_lock_irqsave(&port->sc_port.lock, flags);
 615	if (status & SAL_CONSOLE_INTR_RECV) {
 616		sn_receive_chars(port, flags);
 617	}
 618	if (status & SAL_CONSOLE_INTR_XMIT) {
 619		sn_transmit_chars(port, TRANSMIT_BUFFERED);
 620	}
 621	spin_unlock_irqrestore(&port->sc_port.lock, flags);
 622	return IRQ_HANDLED;
 623}
 624
 625/**
 626 * sn_sal_timer_poll - this function handles polled console mode
 627 * @data: A pointer to our sn_cons_port (which contains the uart port)
 628 *
 629 * data is the pointer that init_timer will store for us.  This function is
 630 * associated with init_timer to see if there is any console traffic.
 631 * Obviously not used in interrupt mode
 632 *
 633 */
 634static void sn_sal_timer_poll(unsigned long data)
 635{
 636	struct sn_cons_port *port = (struct sn_cons_port *)data;
 637	unsigned long flags;
 638
 639	if (!port)
 640		return;
 641
 642	if (!port->sc_port.irq) {
 643		spin_lock_irqsave(&port->sc_port.lock, flags);
 644		if (sn_process_input)
 645			sn_receive_chars(port, flags);
 646		sn_transmit_chars(port, TRANSMIT_RAW);
 647		spin_unlock_irqrestore(&port->sc_port.lock, flags);
 648		mod_timer(&port->sc_timer,
 649			  jiffies + port->sc_interrupt_timeout);
 650	}
 651}
 652
 653/*
 654 * Boot-time initialization code
 655 */
 656
 657/**
 658 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
 659 * @port: Our sn_cons_port (which contains the uart port)
 660 *
 661 * So this is used by sn_sal_serial_console_init (early on, before we're
 662 * registered with serial core).  It's also used by sn_sal_init
 663 * right after we've registered with serial core.  The later only happens
 664 * if we didn't already come through here via sn_sal_serial_console_init.
 665 *
 666 */
 667static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
 668{
 669	unsigned long flags;
 670
 671	if (!port)
 672		return;
 673
 674	DPRINTF("sn_console: about to switch to asynchronous console\n");
 675
 676	/* without early_printk, we may be invoked late enough to race
 677	 * with other cpus doing console IO at this point, however
 678	 * console interrupts will never be enabled */
 679	spin_lock_irqsave(&port->sc_port.lock, flags);
 680
 681	/* early_printk invocation may have done this for us */
 682	if (!port->sc_ops)
 683		port->sc_ops = &poll_ops;
 684
 685	/* we can't turn on the console interrupt (as request_irq
 686	 * calls kmalloc, which isn't set up yet), so we rely on a
 687	 * timer to poll for input and push data from the console
 688	 * buffer.
 689	 */
 690	init_timer(&port->sc_timer);
 691	port->sc_timer.function = sn_sal_timer_poll;
 692	port->sc_timer.data = (unsigned long)port;
 693
 694	if (IS_RUNNING_ON_SIMULATOR())
 695		port->sc_interrupt_timeout = 6;
 696	else {
 697		/* 960cps / 16 char FIFO = 60HZ
 698		 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
 699		port->sc_interrupt_timeout =
 700		    HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
 701	}
 702	mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
 703
 704	port->sc_is_asynch = 1;
 705	spin_unlock_irqrestore(&port->sc_port.lock, flags);
 706}
 707
 708/**
 709 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
 710 * @port: Our sn_cons_port (which contains the uart port)
 711 *
 712 * In sn_sal_init, after we're registered with serial core and
 713 * the port is added, this function is called to switch us to interrupt
 714 * mode.  We were previously in asynch/polling mode (using init_timer).
 715 *
 716 * We attempt to switch to interrupt mode here by calling
 717 * request_irq.  If that works out, we enable receive interrupts.
 718 */
 719static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
 720{
 721	unsigned long flags;
 722
 723	if (port) {
 724		DPRINTF("sn_console: switching to interrupt driven console\n");
 725
 726		if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
 727				IRQF_SHARED,
 728				"SAL console driver", port) >= 0) {
 729			spin_lock_irqsave(&port->sc_port.lock, flags);
 730			port->sc_port.irq = SGI_UART_VECTOR;
 731			port->sc_ops = &intr_ops;
 732			irq_set_handler(port->sc_port.irq, handle_level_irq);
 733
 734			/* turn on receive interrupts */
 735			ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
 736			spin_unlock_irqrestore(&port->sc_port.lock, flags);
 737		}
 738		else {
 739			printk(KERN_INFO
 740			    "sn_console: console proceeding in polled mode\n");
 741		}
 742	}
 743}
 744
 745/*
 746 * Kernel console definitions
 747 */
 748
 749static void sn_sal_console_write(struct console *, const char *, unsigned);
 750static int sn_sal_console_setup(struct console *, char *);
 751static struct uart_driver sal_console_uart;
 752extern struct tty_driver *uart_console_device(struct console *, int *);
 753
 754static struct console sal_console = {
 755	.name = DEVICE_NAME,
 756	.write = sn_sal_console_write,
 757	.device = uart_console_device,
 758	.setup = sn_sal_console_setup,
 759	.index = -1,		/* unspecified */
 760	.data = &sal_console_uart,
 761};
 762
 763#define SAL_CONSOLE	&sal_console
 764
 765static struct uart_driver sal_console_uart = {
 766	.owner = THIS_MODULE,
 767	.driver_name = "sn_console",
 768	.dev_name = DEVICE_NAME,
 769	.major = 0,		/* major/minor set at registration time per USE_DYNAMIC_MINOR */
 770	.minor = 0,
 771	.nr = 1,		/* one port */
 772	.cons = SAL_CONSOLE,
 773};
 774
 775/**
 776 * sn_sal_init - When the kernel loads us, get us rolling w/ serial core
 777 *
 778 * Before this is called, we've been printing kernel messages in a special
 779 * early mode not making use of the serial core infrastructure.  When our
 780 * driver is loaded for real, we register the driver and port with serial
 781 * core and try to enable interrupt driven mode.
 782 *
 783 */
 784static int __init sn_sal_init(void)
 785{
 786	int retval;
 787
 788	if (!ia64_platform_is("sn2"))
 789		return 0;
 790
 791	printk(KERN_INFO "sn_console: Console driver init\n");
 792
 793	if (USE_DYNAMIC_MINOR == 1) {
 794		misc.minor = MISC_DYNAMIC_MINOR;
 795		misc.name = DEVICE_NAME_DYNAMIC;
 796		retval = misc_register(&misc);
 797		if (retval != 0) {
 798			printk(KERN_WARNING "Failed to register console "
 799			       "device using misc_register.\n");
 800			return -ENODEV;
 801		}
 802		sal_console_uart.major = MISC_MAJOR;
 803		sal_console_uart.minor = misc.minor;
 804	} else {
 805		sal_console_uart.major = DEVICE_MAJOR;
 806		sal_console_uart.minor = DEVICE_MINOR;
 807	}
 808
 809	/* We register the driver and the port before switching to interrupts
 810	 * or async above so the proper uart structures are populated */
 811
 812	if (uart_register_driver(&sal_console_uart) < 0) {
 813		printk
 814		    ("ERROR sn_sal_init failed uart_register_driver, line %d\n",
 815		     __LINE__);
 816		return -ENODEV;
 817	}
 818
 819	spin_lock_init(&sal_console_port.sc_port.lock);
 820
 821	/* Setup the port struct with the minimum needed */
 822	sal_console_port.sc_port.membase = (char *)1;	/* just needs to be non-zero */
 823	sal_console_port.sc_port.type = PORT_16550A;
 824	sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
 825	sal_console_port.sc_port.ops = &sn_console_ops;
 826	sal_console_port.sc_port.line = 0;
 827
 828	if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
 829		/* error - not sure what I'd do - so I'll do nothing */
 830		printk(KERN_ERR "%s: unable to add port\n", __func__);
 831	}
 832
 833	/* when this driver is compiled in, the console initialization
 834	 * will have already switched us into asynchronous operation
 835	 * before we get here through the initcalls */
 836	if (!sal_console_port.sc_is_asynch) {
 837		sn_sal_switch_to_asynch(&sal_console_port);
 838	}
 839
 840	/* at this point (device_init) we can try to turn on interrupts */
 841	if (!IS_RUNNING_ON_SIMULATOR()) {
 842		sn_sal_switch_to_interrupts(&sal_console_port);
 843	}
 844	sn_process_input = 1;
 845	return 0;
 846}
 847device_initcall(sn_sal_init);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 848
 849/**
 850 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
 851 * @puts_raw : puts function to do the writing
 852 * @s: input string
 853 * @count: length
 854 *
 855 * We need a \r ahead of every \n for direct writes through
 856 * ia64_sn_console_putb (what sal_puts_raw below actually does).
 857 *
 858 */
 859
 860static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
 861			   const char *s, int count)
 862{
 863	const char *s1;
 864
 865	/* Output '\r' before each '\n' */
 866	while ((s1 = memchr(s, '\n', count)) != NULL) {
 867		puts_raw(s, s1 - s);
 868		puts_raw("\r\n", 2);
 869		count -= s1 + 1 - s;
 870		s = s1 + 1;
 871	}
 872	puts_raw(s, count);
 873}
 874
 875/**
 876 * sn_sal_console_write - Print statements before serial core available
 877 * @console: Console to operate on - we ignore since we have just one
 878 * @s: String to send
 879 * @count: length
 880 *
 881 * This is referenced in the console struct.  It is used for early
 882 * console printing before we register with serial core and for things
 883 * such as kdb.  The console_lock must be held when we get here.
 884 *
 885 * This function has some code for trying to print output even if the lock
 886 * is held.  We try to cover the case where a lock holder could have died.
 887 * We don't use this special case code if we're not registered with serial
 888 * core yet.  After we're registered with serial core, the only time this
 889 * function would be used is for high level kernel output like magic sys req,
 890 * kdb, and printk's.
 891 */
 892static void
 893sn_sal_console_write(struct console *co, const char *s, unsigned count)
 894{
 895	unsigned long flags = 0;
 896	struct sn_cons_port *port = &sal_console_port;
 897	static int stole_lock = 0;
 898
 899	BUG_ON(!port->sc_is_asynch);
 900
 901	/* We can't look at the xmit buffer if we're not registered with serial core
 902	 *  yet.  So only do the fancy recovery after registering
 903	 */
 904	if (!port->sc_port.state) {
 905		/* Not yet registered with serial core - simple case */
 906		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 907		return;
 908	}
 909
 910	/* somebody really wants this output, might be an
 911	 * oops, kdb, panic, etc.  make sure they get it. */
 912	if (spin_is_locked(&port->sc_port.lock)) {
 913		int lhead = port->sc_port.state->xmit.head;
 914		int ltail = port->sc_port.state->xmit.tail;
 915		int counter, got_lock = 0;
 916
 917		/*
 918		 * We attempt to determine if someone has died with the
 919		 * lock. We wait ~20 secs after the head and tail ptrs
 920		 * stop moving and assume the lock holder is not functional
 921		 * and plow ahead. If the lock is freed within the time out
 922		 * period we re-get the lock and go ahead normally. We also
 923		 * remember if we have plowed ahead so that we don't have
 924		 * to wait out the time out period again - the asumption
 925		 * is that we will time out again.
 926		 */
 927
 928		for (counter = 0; counter < 150; mdelay(125), counter++) {
 929			if (!spin_is_locked(&port->sc_port.lock)
 930			    || stole_lock) {
 931				if (!stole_lock) {
 932					spin_lock_irqsave(&port->sc_port.lock,
 933							  flags);
 934					got_lock = 1;
 935				}
 936				break;
 937			} else {
 938				/* still locked */
 939				if ((lhead != port->sc_port.state->xmit.head)
 940				    || (ltail !=
 941					port->sc_port.state->xmit.tail)) {
 942					lhead =
 943						port->sc_port.state->xmit.head;
 944					ltail =
 945						port->sc_port.state->xmit.tail;
 946					counter = 0;
 947				}
 948			}
 949		}
 950		/* flush anything in the serial core xmit buffer, raw */
 951		sn_transmit_chars(port, 1);
 952		if (got_lock) {
 953			spin_unlock_irqrestore(&port->sc_port.lock, flags);
 954			stole_lock = 0;
 955		} else {
 956			/* fell thru */
 957			stole_lock = 1;
 958		}
 959		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 960	} else {
 961		stole_lock = 0;
 962		spin_lock_irqsave(&port->sc_port.lock, flags);
 963		sn_transmit_chars(port, 1);
 964		spin_unlock_irqrestore(&port->sc_port.lock, flags);
 965
 966		puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
 967	}
 968}
 969
 970
 971/**
 972 * sn_sal_console_setup - Set up console for early printing
 973 * @co: Console to work with
 974 * @options: Options to set
 975 *
 976 * Altix console doesn't do anything with baud rates, etc, anyway.
 977 *
 978 * This isn't required since not providing the setup function in the
 979 * console struct is ok.  However, other patches like KDB plop something
 980 * here so providing it is easier.
 981 *
 982 */
 983static int sn_sal_console_setup(struct console *co, char *options)
 984{
 985	return 0;
 986}
 987
 988/**
 989 * sn_sal_console_write_early - simple early output routine
 990 * @co - console struct
 991 * @s - string to print
 992 * @count - count
 993 *
 994 * Simple function to provide early output, before even
 995 * sn_sal_serial_console_init is called.  Referenced in the
 996 * console struct registerd in sn_serial_console_early_setup.
 997 *
 998 */
 999static void __init
1000sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1001{
1002	puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1003}
1004
1005/* Used for very early console printing - again, before
1006 * sn_sal_serial_console_init is run */
1007static struct console sal_console_early __initdata = {
1008	.name = "sn_sal",
1009	.write = sn_sal_console_write_early,
1010	.flags = CON_PRINTBUFFER,
1011	.index = -1,
1012};
1013
1014/**
1015 * sn_serial_console_early_setup - Sets up early console output support
1016 *
1017 * Register a console early on...  This is for output before even
1018 * sn_sal_serial_cosnole_init is called.  This function is called from
1019 * setup.c.  This allows us to do really early polled writes. When
1020 * sn_sal_serial_console_init is called, this console is unregistered
1021 * and a new one registered.
1022 */
1023int __init sn_serial_console_early_setup(void)
1024{
1025	if (!ia64_platform_is("sn2"))
1026		return -1;
1027
1028	sal_console_port.sc_ops = &poll_ops;
1029	spin_lock_init(&sal_console_port.sc_port.lock);
1030	early_sn_setup();	/* Find SAL entry points */
1031	register_console(&sal_console_early);
1032
1033	return 0;
1034}
1035
1036/**
1037 * sn_sal_serial_console_init - Early console output - set up for register
1038 *
1039 * This function is called when regular console init happens.  Because we
1040 * support even earlier console output with sn_serial_console_early_setup
1041 * (called from setup.c directly), this function unregisters the really
1042 * early console.
1043 *
1044 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1045 * it here doesn't hurt anything.
1046 *
1047 */
1048static int __init sn_sal_serial_console_init(void)
1049{
1050	if (ia64_platform_is("sn2")) {
1051		sn_sal_switch_to_asynch(&sal_console_port);
1052		DPRINTF("sn_sal_serial_console_init : register console\n");
1053		register_console(&sal_console);
1054		unregister_console(&sal_console_early);
1055	}
1056	return 0;
1057}
1058
1059console_initcall(sn_sal_serial_console_init);