blob: f51ffdc696fd4f753aff688e5e8d0fb8e0e07779 [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 *
Jes Sorensend171e512006-01-17 13:05:12 -05009 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/interrupt.h>
41#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020042#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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
roel kluin1f3ccae2008-10-18 20:27:46 -070065#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300150 * synchronous (raw) and asynchronous (buffered). initially, early_printk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100264static void snp_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
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 */
273static 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 */
282static 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 */
291static 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 */
301static 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 */
310static 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 */
320static void snp_stop_rx(struct uart_port *port)
321{
322}
323
324/**
325 * snp_start_tx - Start transmitter
326 * @port: Port to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *
328 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100329static void snp_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 if (sal_console_port.sc_ops->sal_wakeup_transmit)
332 sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
333 TRANSMIT_BUFFERED);
334
335}
336
337/**
338 * snp_break_ctl - handle breaks - ignored by us
339 * @port: Port to operate on
340 * @break_state: Break state
341 *
342 */
343static void snp_break_ctl(struct uart_port *port, int break_state)
344{
345}
346
347/**
348 * snp_startup - Start up the serial port - always return 0 (We're always on)
349 * @port: Port to operate on
350 *
351 */
352static int snp_startup(struct uart_port *port)
353{
354 return 0;
355}
356
357/**
358 * snp_set_termios - set termios stuff - we ignore these
359 * @port: port to operate on
360 * @termios: New settings
361 * @termios: Old
362 *
363 */
364static void
Alan Cox606d0992006-12-08 02:38:45 -0800365snp_set_termios(struct uart_port *port, struct ktermios *termios,
366 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368}
369
370/**
371 * snp_request_port - allocate resources for port - ignored by us
372 * @port: port to operate on
373 *
374 */
375static int snp_request_port(struct uart_port *port)
376{
377 return 0;
378}
379
380/**
381 * snp_config_port - allocate resources, set up - we ignore, we're always on
382 * @port: Port to operate on
383 * @flags: flags used for port setup
384 *
385 */
386static void snp_config_port(struct uart_port *port, int flags)
387{
388}
389
390/* Associate the uart functions above - given to serial core */
391
392static struct uart_ops sn_console_ops = {
393 .tx_empty = snp_tx_empty,
394 .set_mctrl = snp_set_mctrl,
395 .get_mctrl = snp_get_mctrl,
396 .stop_tx = snp_stop_tx,
397 .start_tx = snp_start_tx,
398 .stop_rx = snp_stop_rx,
399 .enable_ms = snp_enable_ms,
400 .break_ctl = snp_break_ctl,
401 .startup = snp_startup,
402 .shutdown = snp_shutdown,
403 .set_termios = snp_set_termios,
404 .pm = NULL,
405 .type = snp_type,
406 .release_port = snp_release_port,
407 .request_port = snp_request_port,
408 .config_port = snp_config_port,
409 .verify_port = NULL,
410};
411
412/* End of uart struct functions and defines */
413
414#ifdef DEBUG
415
416/**
417 * sn_debug_printf - close to hardware debugging printf
418 * @fmt: printf format
419 *
420 * This is as "close to the metal" as we can get, used when the driver
421 * itself may be broken.
422 *
423 */
424static int sn_debug_printf(const char *fmt, ...)
425{
426 static char printk_buf[1024];
427 int printed_len;
428 va_list args;
429
430 va_start(args, fmt);
431 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
432
433 if (!sal_console_port.sc_ops) {
434 sal_console_port.sc_ops = &poll_ops;
435 early_sn_setup();
436 }
437 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
438
439 va_end(args);
440 return printed_len;
441}
442#endif /* DEBUG */
443
444/*
445 * Interrupt handling routines.
446 */
447
448/**
449 * sn_receive_chars - Grab characters, pass them to tty layer
450 * @port: Port to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @flags: irq flags
452 *
453 * Note: If we're not registered with the serial core infrastructure yet,
454 * we don't try to send characters to it...
455 *
456 */
457static void
David Howells7d12e782006-10-05 14:55:46 +0100458sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100460 struct tty_port *tport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!port) {
Masanari Iida90af6d22012-02-18 19:49:47 +0900464 printk(KERN_ERR "sn_receive_chars - port NULL so can't receive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return;
466 }
467
468 if (!port->sc_ops) {
Masanari Iida90af6d22012-02-18 19:49:47 +0900469 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return;
471 }
472
Alan Coxebd2c8f2009-09-19 13:13:28 -0700473 if (port->sc_port.state) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200474 /* The serial_core stuffs are initialized, use them */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100475 tport = &port->sc_port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
478 while (port->sc_ops->sal_input_pending()) {
479 ch = port->sc_ops->sal_getc();
480 if (ch < 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300481 printk(KERN_ERR "sn_console: An error occurred while "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 "obtaining data from the console (0x%0x)\n", ch);
483 break;
484 }
485#ifdef CONFIG_MAGIC_SYSRQ
486 if (sysrq_requested) {
487 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
488
489 sysrq_requested = 0;
490 if (ch && time_before(jiffies, sysrq_timeout)) {
491 spin_unlock_irqrestore(&port->sc_port.lock, flags);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700492 handle_sysrq(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 spin_lock_irqsave(&port->sc_port.lock, flags);
494 /* ignore actual sysrq command char */
495 continue;
496 }
497 }
498 if (ch == *sysrq_serial_ptr) {
499 if (!(*++sysrq_serial_ptr)) {
500 sysrq_requested = jiffies;
501 sysrq_serial_ptr = sysrq_serial_str;
502 }
503 /*
504 * ignore the whole sysrq string except for the
505 * leading escape
506 */
507 if (ch != '\e')
508 continue;
509 }
510 else
511 sysrq_serial_ptr = sysrq_serial_str;
512#endif /* CONFIG_MAGIC_SYSRQ */
513
514 /* record the character to pass up to the tty layer */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100515 if (tport) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100516 if (tty_insert_flip_char(tport, ch, TTY_NORMAL) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 }
519 port->sc_port.icount.rx++;
520 }
521
Jiri Slaby2e124b42013-01-03 15:53:06 +0100522 if (tport)
523 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/**
527 * sn_transmit_chars - grab characters from serial core, send off
528 * @port: Port to operate on
529 * @raw: Transmit raw or buffered
530 *
531 * Note: If we're early, before we're registered with serial core, the
532 * writes are going through sn_sal_console_write because that's how
533 * register_console has been set up. We currently could have asynch
534 * polls calling this function due to sn_sal_switch_to_asynch but we can
535 * ignore them until we register with the serial core stuffs.
536 *
537 */
538static void sn_transmit_chars(struct sn_cons_port *port, int raw)
539{
540 int xmit_count, tail, head, loops, ii;
541 int result;
542 char *start;
543 struct circ_buf *xmit;
544
545 if (!port)
546 return;
547
548 BUG_ON(!port->sc_is_asynch);
549
Alan Coxebd2c8f2009-09-19 13:13:28 -0700550 if (port->sc_port.state) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200551 /* We're initialized, using serial core infrastructure */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700552 xmit = &port->sc_port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 } else {
554 /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200555 * initialized yet. Just return. Writes are going through
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * sn_sal_console_write (due to register_console) at this time.
557 */
558 return;
559 }
560
561 if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
562 /* Nothing to do. */
Pat Gefre328007b2005-06-23 00:09:54 -0700563 ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565 }
566
567 head = xmit->head;
568 tail = xmit->tail;
569 start = &xmit->buf[tail];
570
571 /* twice around gets the tail to the end of the buffer and
572 * then to the head, if needed */
573 loops = (head < tail) ? 2 : 1;
574
575 for (ii = 0; ii < loops; ii++) {
576 xmit_count = (head < tail) ?
577 (UART_XMIT_SIZE - tail) : (head - tail);
578
579 if (xmit_count > 0) {
580 if (raw == TRANSMIT_RAW)
581 result =
582 port->sc_ops->sal_puts_raw(start,
583 xmit_count);
584 else
585 result =
586 port->sc_ops->sal_puts(start, xmit_count);
587#ifdef DEBUG
588 if (!result)
589 DPRINTF("`");
590#endif
591 if (result > 0) {
592 xmit_count -= result;
593 port->sc_port.icount.tx += result;
594 tail += result;
595 tail &= UART_XMIT_SIZE - 1;
596 xmit->tail = tail;
597 start = &xmit->buf[tail];
598 }
599 }
600 }
601
602 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
603 uart_write_wakeup(&port->sc_port);
604
605 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100606 snp_stop_tx(&port->sc_port); /* no-op for us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/**
610 * sn_sal_interrupt - Handle console interrupts
611 * @irq: irq #, useful for debug statements
612 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
614 */
David Howells7d12e782006-10-05 14:55:46 +0100615static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
618 unsigned long flags;
619 int status = ia64_sn_console_intr_status();
620
621 if (!port)
622 return IRQ_NONE;
623
624 spin_lock_irqsave(&port->sc_port.lock, flags);
625 if (status & SAL_CONSOLE_INTR_RECV) {
David Howells7d12e782006-10-05 14:55:46 +0100626 sn_receive_chars(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 if (status & SAL_CONSOLE_INTR_XMIT) {
629 sn_transmit_chars(port, TRANSMIT_BUFFERED);
630 }
631 spin_unlock_irqrestore(&port->sc_port.lock, flags);
632 return IRQ_HANDLED;
633}
634
635/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * sn_sal_timer_poll - this function handles polled console mode
637 * @data: A pointer to our sn_cons_port (which contains the uart port)
638 *
639 * data is the pointer that init_timer will store for us. This function is
640 * associated with init_timer to see if there is any console traffic.
641 * Obviously not used in interrupt mode
642 *
643 */
644static void sn_sal_timer_poll(unsigned long data)
645{
646 struct sn_cons_port *port = (struct sn_cons_port *)data;
647 unsigned long flags;
648
649 if (!port)
650 return;
651
652 if (!port->sc_port.irq) {
653 spin_lock_irqsave(&port->sc_port.lock, flags);
654 if (sn_process_input)
Tony Luck8c1addb2006-10-06 10:09:41 -0700655 sn_receive_chars(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 sn_transmit_chars(port, TRANSMIT_RAW);
657 spin_unlock_irqrestore(&port->sc_port.lock, flags);
658 mod_timer(&port->sc_timer,
659 jiffies + port->sc_interrupt_timeout);
660 }
661}
662
663/*
664 * Boot-time initialization code
665 */
666
667/**
668 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
669 * @port: Our sn_cons_port (which contains the uart port)
670 *
671 * So this is used by sn_sal_serial_console_init (early on, before we're
672 * registered with serial core). It's also used by sn_sal_module_init
673 * right after we've registered with serial core. The later only happens
674 * if we didn't already come through here via sn_sal_serial_console_init.
675 *
676 */
677static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
678{
679 unsigned long flags;
680
681 if (!port)
682 return;
683
684 DPRINTF("sn_console: about to switch to asynchronous console\n");
685
686 /* without early_printk, we may be invoked late enough to race
687 * with other cpus doing console IO at this point, however
688 * console interrupts will never be enabled */
689 spin_lock_irqsave(&port->sc_port.lock, flags);
690
691 /* early_printk invocation may have done this for us */
692 if (!port->sc_ops)
693 port->sc_ops = &poll_ops;
694
695 /* we can't turn on the console interrupt (as request_irq
696 * calls kmalloc, which isn't set up yet), so we rely on a
697 * timer to poll for input and push data from the console
698 * buffer.
699 */
700 init_timer(&port->sc_timer);
701 port->sc_timer.function = sn_sal_timer_poll;
702 port->sc_timer.data = (unsigned long)port;
703
704 if (IS_RUNNING_ON_SIMULATOR())
705 port->sc_interrupt_timeout = 6;
706 else {
707 /* 960cps / 16 char FIFO = 60HZ
708 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
709 port->sc_interrupt_timeout =
710 HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
711 }
712 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
713
714 port->sc_is_asynch = 1;
715 spin_unlock_irqrestore(&port->sc_port.lock, flags);
716}
717
718/**
719 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
720 * @port: Our sn_cons_port (which contains the uart port)
721 *
722 * In sn_sal_module_init, after we're registered with serial core and
723 * the port is added, this function is called to switch us to interrupt
724 * mode. We were previously in asynch/polling mode (using init_timer).
725 *
726 * We attempt to switch to interrupt mode here by calling
Pat Gefref70c81d2007-03-07 20:41:29 -0800727 * request_irq. If that works out, we enable receive interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
729static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned long flags;
732
Pat Gefref70c81d2007-03-07 20:41:29 -0800733 if (port) {
734 DPRINTF("sn_console: switching to interrupt driven console\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Pat Gefref70c81d2007-03-07 20:41:29 -0800736 if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800737 IRQF_SHARED,
Pat Gefref70c81d2007-03-07 20:41:29 -0800738 "SAL console driver", port) >= 0) {
739 spin_lock_irqsave(&port->sc_port.lock, flags);
740 port->sc_port.irq = SGI_UART_VECTOR;
741 port->sc_ops = &intr_ops;
Dimitri Sivanich48e30fa2012-02-22 11:11:06 -0800742 irq_set_handler(port->sc_port.irq, handle_level_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Pat Gefref70c81d2007-03-07 20:41:29 -0800744 /* turn on receive interrupts */
745 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
746 spin_unlock_irqrestore(&port->sc_port.lock, flags);
747 }
748 else {
749 printk(KERN_INFO
750 "sn_console: console proceeding in polled mode\n");
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
756 * Kernel console definitions
757 */
758
759static void sn_sal_console_write(struct console *, const char *, unsigned);
Jes Sorensen17e0c7c2007-07-15 23:42:02 -0700760static int sn_sal_console_setup(struct console *, char *);
Andreas Schwab434498d2005-05-01 08:59:12 -0700761static struct uart_driver sal_console_uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762extern struct tty_driver *uart_console_device(struct console *, int *);
763
764static struct console sal_console = {
765 .name = DEVICE_NAME,
766 .write = sn_sal_console_write,
767 .device = uart_console_device,
768 .setup = sn_sal_console_setup,
769 .index = -1, /* unspecified */
770 .data = &sal_console_uart,
771};
772
773#define SAL_CONSOLE &sal_console
774
Andreas Schwab434498d2005-05-01 08:59:12 -0700775static struct uart_driver sal_console_uart = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .owner = THIS_MODULE,
777 .driver_name = "sn_console",
778 .dev_name = DEVICE_NAME,
779 .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
780 .minor = 0,
781 .nr = 1, /* one port */
782 .cons = SAL_CONSOLE,
783};
784
785/**
786 * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
787 *
788 * Before this is called, we've been printing kernel messages in a special
789 * early mode not making use of the serial core infrastructure. When our
790 * driver is loaded for real, we register the driver and port with serial
791 * core and try to enable interrupt driven mode.
792 *
793 */
794static int __init sn_sal_module_init(void)
795{
796 int retval;
797
798 if (!ia64_platform_is("sn2"))
Bjorn Helgaasf032f902006-03-03 15:34:34 -0700799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 printk(KERN_INFO "sn_console: Console driver init\n");
802
803 if (USE_DYNAMIC_MINOR == 1) {
804 misc.minor = MISC_DYNAMIC_MINOR;
805 misc.name = DEVICE_NAME_DYNAMIC;
806 retval = misc_register(&misc);
807 if (retval != 0) {
Jes Sorensend171e512006-01-17 13:05:12 -0500808 printk(KERN_WARNING "Failed to register console "
809 "device using misc_register.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -ENODEV;
811 }
812 sal_console_uart.major = MISC_MAJOR;
813 sal_console_uart.minor = misc.minor;
814 } else {
815 sal_console_uart.major = DEVICE_MAJOR;
816 sal_console_uart.minor = DEVICE_MINOR;
817 }
818
819 /* We register the driver and the port before switching to interrupts
820 * or async above so the proper uart structures are populated */
821
822 if (uart_register_driver(&sal_console_uart) < 0) {
823 printk
824 ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
825 __LINE__);
826 return -ENODEV;
827 }
828
829 spin_lock_init(&sal_console_port.sc_port.lock);
830
831 /* Setup the port struct with the minimum needed */
832 sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
833 sal_console_port.sc_port.type = PORT_16550A;
834 sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
835 sal_console_port.sc_port.ops = &sn_console_ops;
836 sal_console_port.sc_port.line = 0;
837
838 if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
839 /* error - not sure what I'd do - so I'll do nothing */
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700840 printk(KERN_ERR "%s: unable to add port\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
843 /* when this driver is compiled in, the console initialization
844 * will have already switched us into asynchronous operation
845 * before we get here through the module initcalls */
846 if (!sal_console_port.sc_is_asynch) {
847 sn_sal_switch_to_asynch(&sal_console_port);
848 }
849
850 /* at this point (module_init) we can try to turn on interrupts */
851 if (!IS_RUNNING_ON_SIMULATOR()) {
852 sn_sal_switch_to_interrupts(&sal_console_port);
853 }
854 sn_process_input = 1;
855 return 0;
856}
857
858/**
859 * sn_sal_module_exit - When we're unloaded, remove the driver/port
860 *
861 */
862static void __exit sn_sal_module_exit(void)
863{
864 del_timer_sync(&sal_console_port.sc_timer);
865 uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
866 uart_unregister_driver(&sal_console_uart);
867 misc_deregister(&misc);
868}
869
870module_init(sn_sal_module_init);
871module_exit(sn_sal_module_exit);
872
873/**
874 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
875 * @puts_raw : puts function to do the writing
876 * @s: input string
877 * @count: length
878 *
879 * We need a \r ahead of every \n for direct writes through
880 * ia64_sn_console_putb (what sal_puts_raw below actually does).
881 *
882 */
883
884static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
885 const char *s, int count)
886{
887 const char *s1;
888
889 /* Output '\r' before each '\n' */
890 while ((s1 = memchr(s, '\n', count)) != NULL) {
891 puts_raw(s, s1 - s);
892 puts_raw("\r\n", 2);
893 count -= s1 + 1 - s;
894 s = s1 + 1;
895 }
896 puts_raw(s, count);
897}
898
899/**
900 * sn_sal_console_write - Print statements before serial core available
901 * @console: Console to operate on - we ignore since we have just one
902 * @s: String to send
903 * @count: length
904 *
905 * This is referenced in the console struct. It is used for early
906 * console printing before we register with serial core and for things
907 * such as kdb. The console_lock must be held when we get here.
908 *
909 * This function has some code for trying to print output even if the lock
910 * is held. We try to cover the case where a lock holder could have died.
911 * We don't use this special case code if we're not registered with serial
912 * core yet. After we're registered with serial core, the only time this
913 * function would be used is for high level kernel output like magic sys req,
914 * kdb, and printk's.
915 */
916static void
917sn_sal_console_write(struct console *co, const char *s, unsigned count)
918{
919 unsigned long flags = 0;
920 struct sn_cons_port *port = &sal_console_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 static int stole_lock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 BUG_ON(!port->sc_is_asynch);
924
925 /* We can't look at the xmit buffer if we're not registered with serial core
926 * yet. So only do the fancy recovery after registering
927 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700928 if (!port->sc_port.state) {
Jes Sorensend171e512006-01-17 13:05:12 -0500929 /* Not yet registered with serial core - simple case */
930 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
931 return;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Jes Sorensend171e512006-01-17 13:05:12 -0500934 /* somebody really wants this output, might be an
935 * oops, kdb, panic, etc. make sure they get it. */
936 if (spin_is_locked(&port->sc_port.lock)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -0700937 int lhead = port->sc_port.state->xmit.head;
938 int ltail = port->sc_port.state->xmit.tail;
Jes Sorensend171e512006-01-17 13:05:12 -0500939 int counter, got_lock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Jes Sorensend171e512006-01-17 13:05:12 -0500941 /*
942 * We attempt to determine if someone has died with the
943 * lock. We wait ~20 secs after the head and tail ptrs
944 * stop moving and assume the lock holder is not functional
945 * and plow ahead. If the lock is freed within the time out
946 * period we re-get the lock and go ahead normally. We also
947 * remember if we have plowed ahead so that we don't have
948 * to wait out the time out period again - the asumption
949 * is that we will time out again.
950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jes Sorensend171e512006-01-17 13:05:12 -0500952 for (counter = 0; counter < 150; mdelay(125), counter++) {
953 if (!spin_is_locked(&port->sc_port.lock)
954 || stole_lock) {
955 if (!stole_lock) {
956 spin_lock_irqsave(&port->sc_port.lock,
957 flags);
958 got_lock = 1;
959 }
960 break;
961 } else {
962 /* still locked */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700963 if ((lhead != port->sc_port.state->xmit.head)
Jes Sorensend171e512006-01-17 13:05:12 -0500964 || (ltail !=
Alan Coxebd2c8f2009-09-19 13:13:28 -0700965 port->sc_port.state->xmit.tail)) {
Jes Sorensend171e512006-01-17 13:05:12 -0500966 lhead =
Alan Coxebd2c8f2009-09-19 13:13:28 -0700967 port->sc_port.state->xmit.head;
Jes Sorensend171e512006-01-17 13:05:12 -0500968 ltail =
Alan Coxebd2c8f2009-09-19 13:13:28 -0700969 port->sc_port.state->xmit.tail;
Jes Sorensend171e512006-01-17 13:05:12 -0500970 counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Jes Sorensend171e512006-01-17 13:05:12 -0500974 /* flush anything in the serial core xmit buffer, raw */
975 sn_transmit_chars(port, 1);
976 if (got_lock) {
977 spin_unlock_irqrestore(&port->sc_port.lock, flags);
978 stole_lock = 0;
979 } else {
980 /* fell thru */
981 stole_lock = 1;
982 }
983 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
984 } else {
985 stole_lock = 0;
986 spin_lock_irqsave(&port->sc_port.lock, flags);
987 sn_transmit_chars(port, 1);
988 spin_unlock_irqrestore(&port->sc_port.lock, flags);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
991 }
992}
993
994
995/**
996 * sn_sal_console_setup - Set up console for early printing
997 * @co: Console to work with
998 * @options: Options to set
999 *
1000 * Altix console doesn't do anything with baud rates, etc, anyway.
1001 *
1002 * This isn't required since not providing the setup function in the
1003 * console struct is ok. However, other patches like KDB plop something
1004 * here so providing it is easier.
1005 *
1006 */
Jes Sorensen17e0c7c2007-07-15 23:42:02 -07001007static int sn_sal_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 return 0;
1010}
1011
1012/**
1013 * sn_sal_console_write_early - simple early output routine
1014 * @co - console struct
1015 * @s - string to print
1016 * @count - count
1017 *
1018 * Simple function to provide early output, before even
1019 * sn_sal_serial_console_init is called. Referenced in the
1020 * console struct registerd in sn_serial_console_early_setup.
1021 *
1022 */
1023static void __init
1024sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1025{
1026 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1027}
1028
1029/* Used for very early console printing - again, before
1030 * sn_sal_serial_console_init is run */
1031static struct console sal_console_early __initdata = {
1032 .name = "sn_sal",
1033 .write = sn_sal_console_write_early,
1034 .flags = CON_PRINTBUFFER,
1035 .index = -1,
1036};
1037
1038/**
1039 * sn_serial_console_early_setup - Sets up early console output support
1040 *
1041 * Register a console early on... This is for output before even
1042 * sn_sal_serial_cosnole_init is called. This function is called from
1043 * setup.c. This allows us to do really early polled writes. When
1044 * sn_sal_serial_console_init is called, this console is unregistered
1045 * and a new one registered.
1046 */
1047int __init sn_serial_console_early_setup(void)
1048{
1049 if (!ia64_platform_is("sn2"))
1050 return -1;
1051
1052 sal_console_port.sc_ops = &poll_ops;
Keith Owens71841b82005-07-30 17:52:00 -07001053 spin_lock_init(&sal_console_port.sc_port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 early_sn_setup(); /* Find SAL entry points */
1055 register_console(&sal_console_early);
1056
1057 return 0;
1058}
1059
1060/**
1061 * sn_sal_serial_console_init - Early console output - set up for register
1062 *
1063 * This function is called when regular console init happens. Because we
1064 * support even earlier console output with sn_serial_console_early_setup
1065 * (called from setup.c directly), this function unregisters the really
1066 * early console.
1067 *
1068 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1069 * it here doesn't hurt anything.
1070 *
1071 */
1072static int __init sn_sal_serial_console_init(void)
1073{
1074 if (ia64_platform_is("sn2")) {
1075 sn_sal_switch_to_asynch(&sal_console_port);
1076 DPRINTF("sn_sal_serial_console_init : register console\n");
1077 register_console(&sal_console);
1078 unregister_console(&sal_console_early);
1079 }
1080 return 0;
1081}
1082
1083console_initcall(sn_sal_serial_console_init);