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