staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / tty / serial / sn_console.c
blob283493358a62d7fdf47d462f8432b42579161309
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * C-Brick Serial Port (and console) driver for SGI Altix machines.
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.
10 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All Rights Reserved.
12 * Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
13 * Mountain View, CA 94043, or:
15 * http://www.sgi.com
17 * For further information regarding this notice, see:
19 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
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>
35 #include <asm/io.h>
36 #include <asm/sn/simulator.h>
37 #include <asm/sn/sn_sal.h>
39 /* number of characters we can transmit to the SAL console at a time */
40 #define SN_SAL_MAX_CHARS 120
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))
46 #define SN_SAL_UART_FIFO_DEPTH 16
47 #define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
49 /* sn_transmit_chars() calling args */
50 #define TRANSMIT_BUFFERED 0
51 #define TRANSMIT_RAW 1
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 */
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
65 #ifdef CONFIG_MAGIC_SYSRQ
66 static char sysrq_serial_str[] = "\eSYS";
67 static char *sysrq_serial_ptr = sysrq_serial_str;
68 static unsigned long sysrq_requested;
69 #endif /* CONFIG_MAGIC_SYSRQ */
72 * Port definition - this kinda drives it all
74 struct 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;
88 static struct sn_cons_port sal_console_port;
89 static int sn_process_input;
91 /* Only used if USE_DYNAMIC_MINOR is set to 1 */
92 static struct miscdevice misc; /* used with misc_register for dynamic */
94 extern void early_sn_setup(void);
96 #undef DEBUG
97 #ifdef DEBUG
98 static 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
104 /* Prototypes */
105 static int snt_hw_puts_raw(const char *, int);
106 static int snt_hw_puts_buffered(const char *, int);
107 static int snt_poll_getc(void);
108 static int snt_poll_input_pending(void);
109 static int snt_intr_getc(void);
110 static int snt_intr_input_pending(void);
111 static void sn_transmit_chars(struct sn_cons_port *, int);
113 /* A table for polling:
115 static 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
122 /* A table for interrupts enabled */
123 static 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
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) */
142 /* routines for running the console in polling mode */
145 * snt_poll_getc - Get a character from the console in polling mode
148 static int snt_poll_getc(void)
150 int ch;
152 ia64_sn_console_getc(&ch);
153 return ch;
157 * snt_poll_input_pending - Check if any input is waiting - polling mode.
160 static int snt_poll_input_pending(void)
162 int status, input;
164 status = ia64_sn_console_check(&input);
165 return !status && input;
168 /* routines for an interrupt driven console (normal) */
171 * snt_intr_getc - Get a character from the console, interrupt mode
174 static int snt_intr_getc(void)
176 return ia64_sn_console_readc();
180 * snt_intr_input_pending - Check if input is pending, interrupt mode
183 static int snt_intr_input_pending(void)
185 return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
188 /* these functions are polled and interrupt */
191 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
192 * @s: String
193 * @len: Length
196 static int snt_hw_puts_raw(const char *s, int len)
198 /* this will call the PROM and not return until this is done */
199 return ia64_sn_console_putb(s, len);
203 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
204 * @s: String
205 * @len: Length
208 static int snt_hw_puts_buffered(const char *s, int len)
210 /* queue data to the PROM */
211 return ia64_sn_console_xmit_chars((char *)s, len);
214 /* uart interface structs
215 * These functions are associated with the uart_port that the serial core
216 * infrastructure calls.
218 * Note: Due to how the console works, many routines are no-ops.
222 * snp_type - What type of console are we?
223 * @port: Port to operate with (we ignore since we only have one port)
226 static const char *snp_type(struct uart_port *port)
228 return ("SGI SN L1");
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)
236 static unsigned int snp_tx_empty(struct uart_port *port)
238 return 1;
242 * snp_stop_tx - stop the transmitter - no-op for us
243 * @port: Port to operat eon - we ignore - no-op function
246 static void snp_stop_tx(struct uart_port *port)
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
255 static void snp_release_port(struct uart_port *port)
260 * snp_shutdown - shut down the port - free irq and disable - no-op for us
261 * @port: Port to shut down - we ignore
264 static void snp_shutdown(struct uart_port *port)
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
274 static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
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
283 static unsigned int snp_get_mctrl(struct uart_port *port)
285 return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
289 * snp_stop_rx - Stop the receiver - we ignor ethis
290 * @port: Port to operate on - we ignore
293 static void snp_stop_rx(struct uart_port *port)
298 * snp_start_tx - Start transmitter
299 * @port: Port to operate on
302 static void snp_start_tx(struct uart_port *port)
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);
311 * snp_break_ctl - handle breaks - ignored by us
312 * @port: Port to operate on
313 * @break_state: Break state
316 static void snp_break_ctl(struct uart_port *port, int break_state)
321 * snp_startup - Start up the serial port - always return 0 (We're always on)
322 * @port: Port to operate on
325 static int snp_startup(struct uart_port *port)
327 return 0;
331 * snp_set_termios - set termios stuff - we ignore these
332 * @port: port to operate on
333 * @termios: New settings
334 * @termios: Old
337 static void
338 snp_set_termios(struct uart_port *port, struct ktermios *termios,
339 struct ktermios *old)
344 * snp_request_port - allocate resources for port - ignored by us
345 * @port: port to operate on
348 static int snp_request_port(struct uart_port *port)
350 return 0;
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
359 static void snp_config_port(struct uart_port *port, int flags)
363 /* Associate the uart functions above - given to serial core */
365 static 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,
384 /* End of uart struct functions and defines */
386 #ifdef DEBUG
389 * sn_debug_printf - close to hardware debugging printf
390 * @fmt: printf format
392 * This is as "close to the metal" as we can get, used when the driver
393 * itself may be broken.
396 static int sn_debug_printf(const char *fmt, ...)
398 static char printk_buf[1024];
399 int printed_len;
400 va_list args;
402 va_start(args, fmt);
403 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
405 if (!sal_console_port.sc_ops) {
406 sal_console_port.sc_ops = &poll_ops;
407 early_sn_setup();
409 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
411 va_end(args);
412 return printed_len;
414 #endif /* DEBUG */
417 * Interrupt handling routines.
421 * sn_receive_chars - Grab characters, pass them to tty layer
422 * @port: Port to operate on
423 * @flags: irq flags
425 * Note: If we're not registered with the serial core infrastructure yet,
426 * we don't try to send characters to it...
429 static void
430 sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
432 struct tty_port *tport = NULL;
433 int ch;
435 if (!port) {
436 printk(KERN_ERR "sn_receive_chars - port NULL so can't receive\n");
437 return;
440 if (!port->sc_ops) {
441 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receive\n");
442 return;
445 if (port->sc_port.state) {
446 /* The serial_core stuffs are initialized, use them */
447 tport = &port->sc_port.state->port;
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;
457 #ifdef CONFIG_MAGIC_SYSRQ
458 if (sysrq_requested) {
459 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
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;
470 if (ch == *sysrq_serial_ptr) {
471 if (!(*++sysrq_serial_ptr)) {
472 sysrq_requested = jiffies;
473 sysrq_serial_ptr = sysrq_serial_str;
476 * ignore the whole sysrq string except for the
477 * leading escape
479 if (ch != '\e')
480 continue;
482 else
483 sysrq_serial_ptr = sysrq_serial_str;
484 #endif /* CONFIG_MAGIC_SYSRQ */
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;
491 port->sc_port.icount.rx++;
494 if (tport)
495 tty_flip_buffer_push(tport);
499 * sn_transmit_chars - grab characters from serial core, send off
500 * @port: Port to operate on
501 * @raw: Transmit raw or buffered
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.
510 static void sn_transmit_chars(struct sn_cons_port *port, int raw)
512 int xmit_count, tail, head, loops, ii;
513 int result;
514 char *start;
515 struct circ_buf *xmit;
517 if (!port)
518 return;
520 BUG_ON(!port->sc_is_asynch);
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.
530 return;
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;
539 head = xmit->head;
540 tail = xmit->tail;
541 start = &xmit->buf[tail];
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;
547 for (ii = 0; ii < loops; ii++) {
548 xmit_count = (head < tail) ?
549 (UART_XMIT_SIZE - tail) : (head - tail);
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];
574 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
575 uart_write_wakeup(&port->sc_port);
577 if (uart_circ_empty(xmit))
578 snp_stop_tx(&port->sc_port); /* no-op for us */
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)
587 static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
589 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
590 unsigned long flags;
591 int status = ia64_sn_console_intr_status();
593 if (!port)
594 return IRQ_NONE;
596 spin_lock_irqsave(&port->sc_port.lock, flags);
597 if (status & SAL_CONSOLE_INTR_RECV) {
598 sn_receive_chars(port, flags);
600 if (status & SAL_CONSOLE_INTR_XMIT) {
601 sn_transmit_chars(port, TRANSMIT_BUFFERED);
603 spin_unlock_irqrestore(&port->sc_port.lock, flags);
604 return IRQ_HANDLED;
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)
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
616 static void sn_sal_timer_poll(struct timer_list *t)
618 struct sn_cons_port *port = from_timer(port, t, sc_timer);
619 unsigned long flags;
621 if (!port)
622 return;
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);
636 * Boot-time initialization code
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)
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.
649 static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
651 unsigned long flags;
653 if (!port)
654 return;
656 DPRINTF("sn_console: about to switch to asynchronous console\n");
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);
663 /* early_printk invocation may have done this for us */
664 if (!port->sc_ops)
665 port->sc_ops = &poll_ops;
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.
672 timer_setup(&port->sc_timer, sn_sal_timer_poll, 0);
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;
682 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
684 port->sc_is_asynch = 1;
685 spin_unlock_irqrestore(&port->sc_port.lock, flags);
689 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
690 * @port: Our sn_cons_port (which contains the uart port)
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).
696 * We attempt to switch to interrupt mode here by calling
697 * request_irq. If that works out, we enable receive interrupts.
699 static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
701 unsigned long flags;
703 if (port) {
704 DPRINTF("sn_console: switching to interrupt driven console\n");
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);
714 /* turn on receive interrupts */
715 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
716 spin_unlock_irqrestore(&port->sc_port.lock, flags);
718 else {
719 printk(KERN_INFO
720 "sn_console: console proceeding in polled mode\n");
726 * Kernel console definitions
729 static void sn_sal_console_write(struct console *, const char *, unsigned);
730 static int sn_sal_console_setup(struct console *, char *);
731 static struct uart_driver sal_console_uart;
732 extern struct tty_driver *uart_console_device(struct console *, int *);
734 static 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,
743 #define SAL_CONSOLE &sal_console
745 static 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,
756 * sn_sal_init - When the kernel loads us, get us rolling w/ serial core
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.
764 static int __init sn_sal_init(void)
766 int retval;
768 if (!ia64_platform_is("sn2"))
769 return 0;
771 printk(KERN_INFO "sn_console: Console driver init\n");
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;
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;
789 /* We register the driver and the port before switching to interrupts
790 * or async above so the proper uart structures are populated */
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;
799 spin_lock_init(&sal_console_port.sc_port.lock);
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;
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__);
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);
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);
824 sn_process_input = 1;
825 return 0;
827 device_initcall(sn_sal_init);
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
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).
840 static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
841 const char *s, int count)
843 const char *s1;
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;
852 puts_raw(s, count);
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
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.
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.
872 static void
873 sn_sal_console_write(struct console *co, const char *s, unsigned count)
875 unsigned long flags = 0;
876 struct sn_cons_port *port = &sal_console_port;
877 static int stole_lock = 0;
879 BUG_ON(!port->sc_is_asynch);
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
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;
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;
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.
908 for (counter = 0; counter < 150; mdelay(125), counter++) {
909 if (stole_lock)
910 break;
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;
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;
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);
943 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
949 * sn_sal_console_setup - Set up console for early printing
950 * @co: Console to work with
951 * @options: Options to set
953 * Altix console doesn't do anything with baud rates, etc, anyway.
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.
960 static int sn_sal_console_setup(struct console *co, char *options)
962 return 0;
966 * sn_sal_console_write_early - simple early output routine
967 * @co - console struct
968 * @s - string to print
969 * @count - count
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.
976 static void __init
977 sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
979 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
982 /* Used for very early console printing - again, before
983 * sn_sal_serial_console_init is run */
984 static struct console sal_console_early __initdata = {
985 .name = "sn_sal",
986 .write = sn_sal_console_write_early,
987 .flags = CON_PRINTBUFFER,
988 .index = -1,
992 * sn_serial_console_early_setup - Sets up early console output support
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.
1000 int __init sn_serial_console_early_setup(void)
1002 if (!ia64_platform_is("sn2"))
1003 return -1;
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);
1010 return 0;
1014 * sn_sal_serial_console_init - Early console output - set up for register
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.
1021 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1022 * it here doesn't hurt anything.
1025 static int __init sn_sal_serial_console_init(void)
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);
1033 return 0;
1036 console_initcall(sn_sal_serial_console_init);