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