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