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