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