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