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) 2003 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., 1600 Amphitheatre Pkwy,
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 <asm/uaccess.h>
50 #include <asm/sn/sn_sal.h>
51 #include <asm/sn/pci/pciio.h>		/* this is needed for get_console_nasid */
52 #include <asm/sn/simulator.h>
53 #include <asm/sn/sn2/sn_private.h>
54 
55 #if defined(CONFIG_SGI_L1_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
56 static char sysrq_serial_str[] = "\eSYS";
57 static char *sysrq_serial_ptr = sysrq_serial_str;
58 static unsigned long sysrq_requested;
59 #endif /* CONFIG_SGI_L1_SERIAL_CONSOLE && CONFIG_MAGIC_SYSRQ */
60 
61 /* minor device number */
62 #define SN_SAL_MINOR 64
63 
64 #define SN_SAL_SUBTYPE	1
65 
66 /* number of characters left in xmit buffer before we ask for more */
67 #define WAKEUP_CHARS 128
68 
69 /* number of characters we can transmit to the SAL console at a time */
70 #define SN_SAL_MAX_CHARS 120
71 
72 #define SN_SAL_EVENT_WRITE_WAKEUP 0
73 
74 #define CONSOLE_RESTART 1
75 
76 
77 /* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
78  * avoid losing chars, (always has to be a power of 2) */
79 #define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
80 
81 #define SN_SAL_UART_FIFO_DEPTH 16
82 #define SN_SAL_UART_FIFO_SPEED_CPS 9600/10
83 
84 /* we don't kmalloc/get_free_page these as we want them available
85  * before either of those are initialized */
86 static char sn_xmit_buff_mem[SN_SAL_BUFFER_SIZE];
87 
88 struct volatile_circ_buf {
89 	char *cb_buf;
90 	int cb_head;
91 	int cb_tail;
92 };
93 
94 static struct volatile_circ_buf xmit = { .cb_buf = sn_xmit_buff_mem };
95 static char sn_tmp_buffer[SN_SAL_BUFFER_SIZE];
96 
97 static struct tty_struct *sn_sal_tty;
98 
99 static struct timer_list sn_sal_timer;
100 static int sn_sal_event; /* event type for task queue */
101 static int sn_sal_refcount;
102 
103 static int sn_sal_is_asynch;
104 static int sn_sal_irq;
105 static spinlock_t sn_sal_lock = SPIN_LOCK_UNLOCKED;
106 static int sn_total_tx_count;
107 static int sn_total_rx_count;
108 
109 static struct tty_struct *sn_sal_table;
110 static struct termios *sn_sal_termios;
111 static struct termios *sn_sal_termios_locked;
112 
113 static void sn_sal_tasklet_action(unsigned long data);
114 static DECLARE_TASKLET(sn_sal_tasklet, sn_sal_tasklet_action, 0);
115 
116 static unsigned long sn_interrupt_timeout;
117 
118 extern u64 master_node_bedrock_address;
119 
120 static int sn_debug_printf(const char *fmt, ...);
121 
122 #undef DEBUG
123 #ifdef DEBUG
124 #define DPRINTF(x...) sn_debug_printf(x)
125 #else
126 #define DPRINTF(x...) do { } while (0)
127 #endif
128 
129 struct sn_sal_ops {
130 	int (*sal_puts)(const char *s, int len);
131 	int (*sal_getc)(void);
132 	int (*sal_input_pending)(void);
133 	void (*sal_wakeup_transmit)(void);
134 };
135 
136 /* This is the pointer used. It is assigned to point to one of
137  * the tables below.
138  */
139 static struct sn_sal_ops *sn_func;
140 
141 /* Prototypes */
142 static int snt_hw_puts(const char *, int);
143 static int snt_poll_getc(void);
144 static int snt_poll_input_pending(void);
145 static int snt_sim_puts(const char *, int);
146 static int snt_sim_getc(void);
147 static int snt_sim_input_pending(void);
148 static int snt_intr_getc(void);
149 static int snt_intr_input_pending(void);
150 static void sn_intr_transmit_chars(void);
151 
152 /* A table for polling */
153 static struct sn_sal_ops poll_ops = {
154 	.sal_puts		= snt_hw_puts,
155 	.sal_getc 		= snt_poll_getc,
156 	.sal_input_pending	= snt_poll_input_pending
157 };
158 
159 /* A table for the simulator */
160 static struct sn_sal_ops sim_ops = {
161 	.sal_puts		= snt_sim_puts,
162 	.sal_getc		= snt_sim_getc,
163 	.sal_input_pending	= snt_sim_input_pending
164 };
165 
166 /* A table for interrupts enabled */
167 static struct sn_sal_ops intr_ops = {
168 	.sal_puts		= snt_hw_puts,
169 	.sal_getc		= snt_intr_getc,
170 	.sal_input_pending	= snt_intr_input_pending,
171 	.sal_wakeup_transmit	= sn_intr_transmit_chars
172 };
173 
174 
175 /* the console does output in two distinctly different ways:
176  * synchronous and asynchronous (buffered).  initally, early_printk
177  * does synchronous output.  any data written goes directly to the SAL
178  * to be output (incidentally, it is internally buffered by the SAL)
179  * after interrupts and timers are initialized and available for use,
180  * the console init code switches to asynchronous output.  this is
181  * also the earliest opportunity to begin polling for console input.
182  * after console initialization, console output and tty (serial port)
183  * output is buffered and sent to the SAL asynchronously (either by
184  * timer callback or by UART interrupt) */
185 
186 
187 /* routines for running the console in polling mode */
188 
189 static int
snt_hw_puts(const char * s,int len)190 snt_hw_puts(const char *s, int len)
191 {
192 	/* looking at the PROM source code, putb calls the flush
193 	 * routine, so if we send characters in FIFO sized chunks, it
194 	 * should go out by the next time the timer gets called */
195 	return ia64_sn_console_putb(s, len);
196 }
197 
198 static int
snt_poll_getc(void)199 snt_poll_getc(void)
200 {
201 	int ch;
202 	ia64_sn_console_getc(&ch);
203 	return ch;
204 }
205 
206 static int
snt_poll_input_pending(void)207 snt_poll_input_pending(void)
208 {
209 	int status, input;
210 
211 	status = ia64_sn_console_check(&input);
212 	return !status && input;
213 }
214 
215 
216 /* routines for running the console on the simulator */
217 
218 static int
snt_sim_puts(const char * str,int count)219 snt_sim_puts(const char *str, int count)
220 {
221 	int counter = count;
222 
223 #ifdef FLAG_DIRECT_CONSOLE_WRITES
224 	/* This is an easy way to pre-pend the output to know whether the output
225 	 * was done via sal or directly */
226 	writeb('[', master_node_bedrock_address + (UART_TX << 3));
227 	writeb('+', master_node_bedrock_address + (UART_TX << 3));
228 	writeb(']', master_node_bedrock_address + (UART_TX << 3));
229 	writeb(' ', master_node_bedrock_address + (UART_TX << 3));
230 #endif /* FLAG_DIRECT_CONSOLE_WRITES */
231 	while (counter > 0) {
232 		writeb(*str, master_node_bedrock_address + (UART_TX << 3));
233 		counter--;
234 		str++;
235 	}
236 
237 	return count;
238 }
239 
240 static int
snt_sim_getc(void)241 snt_sim_getc(void)
242 {
243 	return readb(master_node_bedrock_address + (UART_RX << 3));
244 }
245 
246 static int
snt_sim_input_pending(void)247 snt_sim_input_pending(void)
248 {
249 	return readb(master_node_bedrock_address + (UART_LSR << 3)) & UART_LSR_DR;
250 }
251 
252 
253 /* routines for an interrupt driven console (normal) */
254 
255 static int
snt_intr_getc(void)256 snt_intr_getc(void)
257 {
258 	return ia64_sn_console_readc();
259 }
260 
261 static int
snt_intr_input_pending(void)262 snt_intr_input_pending(void)
263 {
264 	return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
265 }
266 
267 /* The early printk (possible setup) and function call */
268 
269 void
early_printk_sn_sal(const char * s,unsigned count)270 early_printk_sn_sal(const char *s, unsigned count)
271 {
272 #if defined(CONFIG_IA64_EARLY_PRINTK_SGI_SN) || defined(CONFIG_IA64_SGI_SN_SIM)
273 	extern void early_sn_setup(void);
274 #endif
275 
276 	if (!sn_func) {
277 		if (IS_RUNNING_ON_SIMULATOR())
278 			sn_func = &sim_ops;
279 		else
280 			sn_func = &poll_ops;
281 
282 #if defined(CONFIG_IA64_EARLY_PRINTK_SGI_SN) || defined(CONFIG_IA64_SGI_SN_SIM)
283 		early_sn_setup();
284 #endif
285 	}
286 	sn_func->sal_puts(s, count);
287 }
288 
289 /* this is as "close to the metal" as we can get, used when the driver
290  * itself may be broken */
291 static int
sn_debug_printf(const char * fmt,...)292 sn_debug_printf(const char *fmt, ...)
293 {
294 	static char printk_buf[1024];
295 	int printed_len;
296 	va_list args;
297 
298 	va_start(args, fmt);
299 	printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
300 	early_printk_sn_sal(printk_buf, printed_len);
301 	va_end(args);
302 	return printed_len;
303 }
304 
305 /*
306  * Interrupt handling routines.
307  */
308 
309 static void
sn_sal_sched_event(int event)310 sn_sal_sched_event(int event)
311 {
312 	sn_sal_event |= (1 << event);
313 	tasklet_schedule(&sn_sal_tasklet);
314 }
315 
316 /* sn_receive_chars can be called before sn_sal_tty is initialized.  in
317  * that case, its only use is to trigger sysrq and kdb */
318 static void
sn_receive_chars(struct pt_regs * regs,unsigned long * flags)319 sn_receive_chars(struct pt_regs *regs, unsigned long *flags)
320 {
321 	int ch;
322 
323 	while (sn_func->sal_input_pending()) {
324 		ch = sn_func->sal_getc();
325 		if (ch < 0) {
326 			printk(KERN_ERR "sn_serial: An error occured while "
327 			       "obtaining data from the console (0x%0x)\n", ch);
328 			break;
329 		}
330 #if defined(CONFIG_SGI_L1_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
331 		if (sysrq_requested) {
332 			unsigned long sysrq_timeout = sysrq_requested + HZ*5;
333 
334 			sysrq_requested = 0;
335 			if (ch && time_before(jiffies, sysrq_timeout)) {
336 				spin_unlock_irqrestore(&sn_sal_lock, *flags);
337 				handle_sysrq(ch, regs, NULL, NULL);
338 				spin_lock_irqsave(&sn_sal_lock, *flags);
339 				/* don't record this char */
340 				continue;
341 			}
342 		}
343 		if (ch == *sysrq_serial_ptr) {
344 			if (!(*++sysrq_serial_ptr)) {
345 				sysrq_requested = jiffies;
346 				sysrq_serial_ptr = sysrq_serial_str;
347 			}
348 		} else
349 			sysrq_serial_ptr = sysrq_serial_str;
350 #endif /* CONFIG_SGI_L1_SERIAL_CONSOLE && CONFIG_MAGIC_SYSRQ */
351 
352 		/* record the character to pass up to the tty layer */
353 		if (sn_sal_tty) {
354 			*sn_sal_tty->flip.char_buf_ptr = ch;
355 			sn_sal_tty->flip.char_buf_ptr++;
356 			sn_sal_tty->flip.count++;
357 			if (sn_sal_tty->flip.count == TTY_FLIPBUF_SIZE)
358 				break;
359 		}
360 		sn_total_rx_count++;
361 	}
362 
363 	if (sn_sal_tty)
364 		tty_flip_buffer_push((struct tty_struct *)sn_sal_tty);
365 }
366 
367 
368 /* synch_flush_xmit must be called with sn_sal_lock */
369 static void
synch_flush_xmit(void)370 synch_flush_xmit(void)
371 {
372 	int xmit_count, tail, head, loops, ii;
373 	int result;
374 	char *start;
375 
376 	if (xmit.cb_head == xmit.cb_tail)
377 		return; /* Nothing to do. */
378 
379 	head = xmit.cb_head;
380 	tail = xmit.cb_tail;
381 	start = &xmit.cb_buf[tail];
382 
383 	/* twice around gets the tail to the end of the buffer and
384 	 * then to the head, if needed */
385 	loops = (head < tail) ? 2 : 1;
386 
387 	for (ii = 0; ii < loops; ii++) {
388 		xmit_count = (head < tail) ?  (SN_SAL_BUFFER_SIZE - tail) : (head - tail);
389 
390 		if (xmit_count > 0) {
391 			result = sn_func->sal_puts((char *)start, xmit_count);
392 			if (!result)
393 				sn_debug_printf("\n*** synch_flush_xmit failed to flush\n");
394 			if (result > 0) {
395 				xmit_count -= result;
396 				sn_total_tx_count += result;
397 				tail += result;
398 				tail &= SN_SAL_BUFFER_SIZE - 1;
399 				xmit.cb_tail = tail;
400 				start = (char *)&xmit.cb_buf[tail];
401 			}
402 		}
403 	}
404 }
405 
406 /* must be called with a lock protecting the circular buffer and
407  * sn_sal_tty */
408 static void
sn_poll_transmit_chars(void)409 sn_poll_transmit_chars(void)
410 {
411 	int xmit_count, tail, head;
412 	int result;
413 	char *start;
414 
415 	BUG_ON(!sn_sal_is_asynch);
416 
417 	if (xmit.cb_head == xmit.cb_tail ||
418 	    (sn_sal_tty && (sn_sal_tty->stopped || sn_sal_tty->hw_stopped))) {
419 		/* Nothing to do. */
420 		return;
421 	}
422 
423 	head = xmit.cb_head;
424 	tail = xmit.cb_tail;
425 	start = &xmit.cb_buf[tail];
426 
427 	xmit_count = (head < tail) ?  (SN_SAL_BUFFER_SIZE - tail) : (head - tail);
428 
429 	if (xmit_count == 0)
430 		sn_debug_printf("\n*** empty xmit_count\n");
431 
432 	/* use the ops, as we could be on the simulator */
433 	result = sn_func->sal_puts((char *)start, xmit_count);
434 	if (!result)
435 		sn_debug_printf("\n*** error in synchronous sal_puts\n");
436 	/* XXX chadt clean this up */
437 	if (result > 0) {
438 		xmit_count -= result;
439 		sn_total_tx_count += result;
440 		tail += result;
441 		tail &= SN_SAL_BUFFER_SIZE - 1;
442 		xmit.cb_tail = tail;
443 		start = &xmit.cb_buf[tail];
444 	}
445 
446 	/* if there's few enough characters left in the xmit buffer
447 	 * that we could stand for the upper layer to send us some
448 	 * more, ask for it. */
449 	if (sn_sal_tty)
450 		if (CIRC_CNT(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE) < WAKEUP_CHARS)
451 			sn_sal_sched_event(SN_SAL_EVENT_WRITE_WAKEUP);
452 }
453 
454 
455 /* must be called with a lock protecting the circular buffer and
456  * sn_sal_tty */
457 static void
sn_intr_transmit_chars(void)458 sn_intr_transmit_chars(void)
459 {
460 	int xmit_count, tail, head, loops, ii;
461 	int result;
462 	char *start;
463 
464 	BUG_ON(!sn_sal_is_asynch);
465 
466 	if (xmit.cb_head == xmit.cb_tail ||
467 	    (sn_sal_tty && (sn_sal_tty->stopped || sn_sal_tty->hw_stopped))) {
468 		/* Nothing to do. */
469 		return;
470 	}
471 
472 	head = xmit.cb_head;
473 	tail = xmit.cb_tail;
474 	start = &xmit.cb_buf[tail];
475 
476 	/* twice around gets the tail to the end of the buffer and
477 	 * then to the head, if needed */
478 	loops = (head < tail) ? 2 : 1;
479 
480 	for (ii = 0; ii < loops; ii++) {
481 		xmit_count = (head < tail) ?
482 			(SN_SAL_BUFFER_SIZE - tail) : (head - tail);
483 
484 		if (xmit_count > 0) {
485 			result = ia64_sn_console_xmit_chars((char *)start, xmit_count);
486 #ifdef DEBUG
487 			if (!result)
488 				sn_debug_printf("`");
489 #endif
490 			if (result > 0) {
491 				xmit_count -= result;
492 				sn_total_tx_count += result;
493 				tail += result;
494 				tail &= SN_SAL_BUFFER_SIZE - 1;
495 				xmit.cb_tail = tail;
496 				start = &xmit.cb_buf[tail];
497 			}
498 		}
499 	}
500 
501 	/* if there's few enough characters left in the xmit buffer
502 	 * that we could stand for the upper layer to send us some
503 	 * more, ask for it. */
504 	if (sn_sal_tty)
505 		if (CIRC_CNT(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE) < WAKEUP_CHARS)
506 			sn_sal_sched_event(SN_SAL_EVENT_WRITE_WAKEUP);
507 }
508 
509 
510 static void
sn_sal_interrupt(int irq,void * dev_id,struct pt_regs * regs)511 sn_sal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
512 {
513 	/* this call is necessary to pass the interrupt back to the
514 	 * SAL, since it doesn't intercept the UART interrupts
515 	 * itself */
516 	int status = ia64_sn_console_intr_status();
517 	unsigned long flags;
518 
519 	spin_lock_irqsave(&sn_sal_lock, flags);
520 	if (status & SAL_CONSOLE_INTR_RECV)
521 		sn_receive_chars(regs, &flags);
522 	if (status & SAL_CONSOLE_INTR_XMIT)
523 		sn_intr_transmit_chars();
524 	spin_unlock_irqrestore(&sn_sal_lock, flags);
525 }
526 
527 
528 /* returns the console irq if interrupt is successfully registered,
529  * else 0 */
530 static int
sn_sal_connect_interrupt(void)531 sn_sal_connect_interrupt(void)
532 {
533 	cpuid_t intr_cpuid;
534 	unsigned int intr_cpuloc;
535 	nasid_t console_nasid;
536 	unsigned int console_irq;
537 	int result;
538 
539 	/* if it is an old prom, run in poll mode */
540 	if ((sn_sal_rev_major() <= 1) && (sn_sal_rev_minor() <= 3)) {
541 		/* before version 1.06 doesn't work */
542 		printk(KERN_INFO "sn_serial: old prom version %x.%02x"
543 		       " - running in polled mode\n",
544 		       sn_sal_rev_major(), sn_sal_rev_minor());
545 		return 0;
546 	}
547 
548 	console_nasid = ia64_sn_get_console_nasid();
549 	intr_cpuid = NODEPDA(NASID_TO_COMPACT_NODEID(console_nasid))->node_first_cpu;
550 	intr_cpuloc = cpu_physical_id(intr_cpuid);
551 	console_irq = CPU_VECTOR_TO_IRQ(intr_cpuloc, SGI_UART_VECTOR);
552 
553 	result = intr_connect_level(intr_cpuid, SGI_UART_VECTOR, 0 /*not used*/, 0 /*not used*/);
554 	BUG_ON(result != SGI_UART_VECTOR);
555 
556 	result = request_irq(console_irq, sn_sal_interrupt, SA_INTERRUPT,
557 		       			"SAL console driver", &sn_sal_tty);
558 	if (result >= 0)
559 		return console_irq;
560 
561 	printk(KERN_INFO "sn_serial: console proceeding in polled mode\n");
562 	return 0;
563 }
564 
565 static void
sn_sal_tasklet_action(unsigned long data)566 sn_sal_tasklet_action(unsigned long data)
567 {
568 	unsigned long flags;
569 
570 	if (sn_sal_tty) {
571 		spin_lock_irqsave(&sn_sal_lock, flags);
572 		if (sn_sal_tty) {
573 			if (test_and_clear_bit(SN_SAL_EVENT_WRITE_WAKEUP, &sn_sal_event)) {
574 				if ((sn_sal_tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
575 							&& sn_sal_tty->ldisc.write_wakeup)
576 					(sn_sal_tty->ldisc.write_wakeup)((struct tty_struct *)sn_sal_tty);
577 				wake_up_interruptible((wait_queue_head_t *)&sn_sal_tty->write_wait);
578 			}
579 		}
580 		spin_unlock_irqrestore(&sn_sal_lock, flags);
581 	}
582 }
583 
584 
585 /*
586  * This function handles polled mode.
587  */
588 static void
sn_sal_timer_poll(unsigned long dummy)589 sn_sal_timer_poll(unsigned long dummy)
590 {
591 	unsigned long flags;
592 
593 	if (!sn_sal_irq) {
594 		spin_lock_irqsave(&sn_sal_lock, flags);
595 		sn_receive_chars(NULL, &flags);
596 		sn_poll_transmit_chars();
597 		spin_unlock_irqrestore(&sn_sal_lock, flags);
598 		mod_timer(&sn_sal_timer, jiffies + sn_interrupt_timeout);
599 	}
600 }
601 
602 static void
sn_sal_timer_restart(unsigned long dummy)603 sn_sal_timer_restart(unsigned long dummy)
604 {
605 	unsigned long flags;
606 
607 	local_irq_save(flags);
608 	sn_sal_interrupt(0, NULL, NULL);
609 	local_irq_restore(flags);
610 	mod_timer(&sn_sal_timer, jiffies + sn_interrupt_timeout);
611 }
612 
613 /*
614  * User-level console routines
615  */
616 
617 static int
sn_sal_open(struct tty_struct * tty,struct file * filp)618 sn_sal_open(struct tty_struct *tty, struct file *filp)
619 {
620 	unsigned long flags;
621 
622 	DPRINTF("sn_sal_open: sn_sal_tty = %p, tty = %p, filp = %p\n",
623 		sn_sal_tty, tty, filp);
624 
625 	spin_lock_irqsave(&sn_sal_lock, flags);
626 	if (!sn_sal_tty)
627 		sn_sal_tty = tty;
628 	spin_unlock_irqrestore(&sn_sal_lock, flags);
629 
630 	return 0;
631 }
632 
633 
634 /* We're keeping all our resources.  We're keeping interrupts turned
635  * on.  Maybe just let the tty layer finish its stuff...? GMSH
636  */
637 static void
sn_sal_close(struct tty_struct * tty,struct file * filp)638 sn_sal_close(struct tty_struct *tty, struct file * filp)
639 {
640 	if (tty->count == 1) {
641 		unsigned long flags;
642 		tty->closing = 1;
643 		if (tty->driver.flush_buffer)
644 			tty->driver.flush_buffer(tty);
645 		if (tty->ldisc.flush_buffer)
646 			tty->ldisc.flush_buffer(tty);
647 		tty->closing = 0;
648 		spin_lock_irqsave(&sn_sal_lock, flags);
649 		sn_sal_tty = NULL;
650 		spin_unlock_irqrestore(&sn_sal_lock, flags);
651 	}
652 }
653 
654 
655 static int
sn_sal_write(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)656 sn_sal_write(struct tty_struct *tty, int from_user,
657 	     const unsigned char *buf, int count)
658 {
659 	int c, ret = 0;
660 	unsigned long flags;
661 
662 	if (from_user) {
663 		while (1) {
664 			int c1;
665 			c = CIRC_SPACE_TO_END(xmit.cb_head, xmit.cb_tail,
666 					      SN_SAL_BUFFER_SIZE);
667 
668 			if (count < c)
669 				c = count;
670 			if (c <= 0)
671 				break;
672 
673 			c -= copy_from_user(sn_tmp_buffer, buf, c);
674 			if (!c) {
675 				if (!ret)
676 					ret = -EFAULT;
677 				break;
678 			}
679 
680 			/* Turn off interrupts and see if the xmit buffer has
681 			 * moved since the last time we looked.
682 			 */
683 			spin_lock_irqsave(&sn_sal_lock, flags);
684 			c1 = CIRC_SPACE_TO_END(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE);
685 
686 			if (c1 < c)
687 				c = c1;
688 
689 			memcpy(xmit.cb_buf + xmit.cb_head, sn_tmp_buffer, c);
690 			xmit.cb_head = ((xmit.cb_head + c) & (SN_SAL_BUFFER_SIZE - 1));
691 			spin_unlock_irqrestore(&sn_sal_lock, flags);
692 
693 			buf += c;
694 			count -= c;
695 			ret += c;
696 		}
697 	} else {
698 		/* The buffer passed in isn't coming from userland,
699 		 * so cut out the middleman (sn_tmp_buffer).
700 		 */
701 		spin_lock_irqsave(&sn_sal_lock, flags);
702 		while (1) {
703 			c = CIRC_SPACE_TO_END(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE);
704 
705 			if (count < c)
706 				c = count;
707 			if (c <= 0) {
708 				break;
709 			}
710 			memcpy(xmit.cb_buf + xmit.cb_head, buf, c);
711 			xmit.cb_head = ((xmit.cb_head + c) & (SN_SAL_BUFFER_SIZE - 1));
712 			buf += c;
713 			count -= c;
714 			ret += c;
715 		}
716 		spin_unlock_irqrestore(&sn_sal_lock, flags);
717 	}
718 
719 	spin_lock_irqsave(&sn_sal_lock, flags);
720 	if (xmit.cb_head != xmit.cb_tail && !(tty && (tty->stopped || tty->hw_stopped)))
721 		if (sn_func->sal_wakeup_transmit)
722 			sn_func->sal_wakeup_transmit();
723 	spin_unlock_irqrestore(&sn_sal_lock, flags);
724 
725 	return ret;
726 }
727 
728 
729 static void
sn_sal_put_char(struct tty_struct * tty,unsigned char ch)730 sn_sal_put_char(struct tty_struct *tty, unsigned char ch)
731 {
732 	unsigned long flags;
733 
734 	spin_lock_irqsave(&sn_sal_lock, flags);
735 	if (CIRC_SPACE(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE) != 0) {
736 		xmit.cb_buf[xmit.cb_head] = ch;
737 		xmit.cb_head = (xmit.cb_head + 1) & (SN_SAL_BUFFER_SIZE-1);
738 		if ( sn_func->sal_wakeup_transmit )
739 			sn_func->sal_wakeup_transmit();
740 	}
741 	spin_unlock_irqrestore(&sn_sal_lock, flags);
742 }
743 
744 
745 static void
sn_sal_flush_chars(struct tty_struct * tty)746 sn_sal_flush_chars(struct tty_struct *tty)
747 {
748 	unsigned long flags;
749 
750 	spin_lock_irqsave(&sn_sal_lock, flags);
751 	if (CIRC_CNT(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE))
752 		if (sn_func->sal_wakeup_transmit)
753 			sn_func->sal_wakeup_transmit();
754 	spin_unlock_irqrestore(&sn_sal_lock, flags);
755 }
756 
757 
758 static int
sn_sal_write_room(struct tty_struct * tty)759 sn_sal_write_room(struct tty_struct *tty)
760 {
761 	unsigned long flags;
762 	int space;
763 
764 	spin_lock_irqsave(&sn_sal_lock, flags);
765 	space = CIRC_SPACE(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE);
766 	spin_unlock_irqrestore(&sn_sal_lock, flags);
767 	return space;
768 }
769 
770 
771 static int
sn_sal_chars_in_buffer(struct tty_struct * tty)772 sn_sal_chars_in_buffer(struct tty_struct *tty)
773 {
774 	unsigned long flags;
775 	int space;
776 
777 	spin_lock_irqsave(&sn_sal_lock, flags);
778 	space = CIRC_CNT(xmit.cb_head, xmit.cb_tail, SN_SAL_BUFFER_SIZE);
779 	DPRINTF("<%d>", space);
780 	spin_unlock_irqrestore(&sn_sal_lock, flags);
781 	return space;
782 }
783 
784 
785 static void
sn_sal_flush_buffer(struct tty_struct * tty)786 sn_sal_flush_buffer(struct tty_struct *tty)
787 {
788 	unsigned long flags;
789 
790 	/* drop everything */
791 	spin_lock_irqsave(&sn_sal_lock, flags);
792 	xmit.cb_head = xmit.cb_tail = 0;
793 	spin_unlock_irqrestore(&sn_sal_lock, flags);
794 
795 	/* wake up tty level */
796 	wake_up_interruptible(&tty->write_wait);
797 	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
798 		(tty->ldisc.write_wakeup)(tty);
799 }
800 
801 
802 static void
sn_sal_hangup(struct tty_struct * tty)803 sn_sal_hangup(struct tty_struct *tty)
804 {
805 	sn_sal_flush_buffer(tty);
806 }
807 
808 
809 static void
sn_sal_wait_until_sent(struct tty_struct * tty,int timeout)810 sn_sal_wait_until_sent(struct tty_struct *tty, int timeout)
811 {
812 	/* this is SAL's problem */
813 	DPRINTF("<sn_serial: should wait until sent>");
814 }
815 
816 
817 /*
818  * sn_sal_read_proc
819  *
820  * Console /proc interface
821  */
822 
823 static int
sn_sal_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)824 sn_sal_read_proc(char *page, char **start, off_t off, int count,
825 		   int *eof, void *data)
826 {
827 	int len = 0;
828 	off_t	begin = 0;
829 
830 	len += sprintf(page, "sn_serial: nasid:%d irq:%d tx:%d rx:%d\n",
831 		       snia_get_console_nasid(), sn_sal_irq,
832 		       sn_total_tx_count, sn_total_rx_count);
833 	*eof = 1;
834 
835 	if (off >= len+begin)
836 		return 0;
837 	*start = page + (off-begin);
838 
839 	return count < begin+len-off ? count : begin+len-off;
840 }
841 
842 
843 static struct tty_driver sn_sal_driver = {
844 	.magic		= TTY_DRIVER_MAGIC,
845 	.driver_name	= "sn_serial",
846 #if defined(CONFIG_DEVFS_FS)
847 	.name		= "tts/%d",
848 #else
849 	.name		= "ttyS",
850 #endif
851 	.major		= TTY_MAJOR,
852 	.minor_start	= SN_SAL_MINOR,
853 	.num		= 1,
854 	.type		= TTY_DRIVER_TYPE_SERIAL,
855 	.subtype	= SN_SAL_SUBTYPE,
856 	.flags		= TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
857 	.refcount	= &sn_sal_refcount,
858 	.table		= &sn_sal_table,
859 	.termios	= &sn_sal_termios,
860 	.termios_locked = &sn_sal_termios_locked,
861 
862 	.open		= sn_sal_open,
863 	.close		= sn_sal_close,
864 	.write		= sn_sal_write,
865 	.put_char	= sn_sal_put_char,
866 	.flush_chars	= sn_sal_flush_chars,
867 	.write_room	= sn_sal_write_room,
868 	.chars_in_buffer = sn_sal_chars_in_buffer,
869 	.hangup		= sn_sal_hangup,
870 	.wait_until_sent = sn_sal_wait_until_sent,
871 	.read_proc	= sn_sal_read_proc,
872 };
873 
874 /* sn_sal_init wishlist:
875  * - allocate sn_tmp_buffer
876  * - fix up the tty_driver struct
877  * - turn on receive interrupts
878  * - do any termios twiddling once and for all
879  */
880 
881 /*
882  * Boot-time initialization code
883  */
884 
885 static void __init
sn_sal_switch_to_asynch(void)886 sn_sal_switch_to_asynch(void)
887 {
888 	unsigned long flags;
889 
890 	sn_debug_printf("sn_serial: about to switch to asynchronous console\n");
891 
892 	/* without early_printk, we may be invoked late enough to race
893 	 * with other cpus doing console IO at this point, however
894 	 * console interrupts will never be enabled */
895 	spin_lock_irqsave(&sn_sal_lock, flags);
896 
897 	/* early_printk invocation may have done this for us */
898 	if (!sn_func) {
899 		if (IS_RUNNING_ON_SIMULATOR())
900 			sn_func = &sim_ops;
901 		else
902 			sn_func = &poll_ops;
903 	}
904 
905 	/* we can't turn on the console interrupt (as request_irq
906 	 * calls kmalloc, which isn't set up yet), so we rely on a
907 	 * timer to poll for input and push data from the console
908 	 * buffer.
909 	 */
910 	init_timer(&sn_sal_timer);
911 	sn_sal_timer.function = sn_sal_timer_poll;
912 
913 	if (IS_RUNNING_ON_SIMULATOR())
914 		sn_interrupt_timeout = 6;
915 	else {
916 		/* 960cps / 16 char FIFO = 60HZ
917 		 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
918 		sn_interrupt_timeout = HZ * SN_SAL_UART_FIFO_DEPTH
919 					/ SN_SAL_UART_FIFO_SPEED_CPS;
920 	}
921 	mod_timer(&sn_sal_timer, jiffies + sn_interrupt_timeout);
922 
923 	sn_sal_is_asynch = 1;
924 	spin_unlock_irqrestore(&sn_sal_lock, flags);
925 }
926 
927 static void __init
sn_sal_switch_to_interrupts(void)928 sn_sal_switch_to_interrupts(void)
929 {
930 	int irq;
931 
932 	sn_debug_printf("sn_serial: switching to interrupt driven console\n");
933 
934 	irq = sn_sal_connect_interrupt();
935 	if (irq) {
936 		unsigned long flags;
937 		spin_lock_irqsave(&sn_sal_lock, flags);
938 
939 		/* sn_sal_irq is a global variable.  When it's set to
940 		 * a non-zero value, we stop polling for input (since
941 		 * interrupts should now be enabled). */
942 		sn_sal_irq = irq;
943 		sn_func = &intr_ops;
944 
945 		/* turn on receive interrupts */
946 		ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
947 
948 		/* the polling timer is already set up, we just change the
949 		 * frequency.  if we've successfully enabled interrupts (and
950 		 * CONSOLE_RESTART isn't defined) the next timer expiration
951 		 * will be the last, otherwise we continue polling */
952 		if (CONSOLE_RESTART) {
953 			/* kick the console every once in a while in
954 			 * case we miss an interrupt */
955 			sn_interrupt_timeout = 20*HZ;
956 			sn_sal_timer.function = sn_sal_timer_restart;
957 			mod_timer(&sn_sal_timer, jiffies + sn_interrupt_timeout);
958 		}
959 		spin_unlock_irqrestore(&sn_sal_lock, flags);
960 	}
961 }
962 
963 static int __init
sn_sal_module_init(void)964 sn_sal_module_init(void)
965 {
966 	int retval;
967 
968 	printk("sn_serial: sn_sal_module_init\n");
969 
970 	if (!ia64_platform_is("sn2"))
971 		return -ENODEV;
972 
973 	/* when this driver is compiled in, the console initialization
974 	 * will have already switched us into asynchronous operation
975 	 * before we get here through the module initcalls */
976 	if (!sn_sal_is_asynch)
977 		sn_sal_switch_to_asynch();
978 
979 	/* at this point (module_init) we can try to turn on interrupts */
980 	if (!IS_RUNNING_ON_SIMULATOR())
981 	    sn_sal_switch_to_interrupts();
982 
983 	sn_sal_driver.init_termios = tty_std_termios;
984 	sn_sal_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
985 
986 	if ((retval = tty_register_driver(&sn_sal_driver))) {
987 		printk(KERN_ERR "sn_serial: Unable to register tty driver\n");
988 		return retval;
989 	}
990 
991 	tty_register_devfs(&sn_sal_driver, 0, sn_sal_driver.minor_start);
992 
993 	return 0;
994 }
995 
996 
997 static void __exit
sn_sal_module_exit(void)998 sn_sal_module_exit(void)
999 {
1000 	del_timer_sync(&sn_sal_timer);
1001 	tty_unregister_driver(&sn_sal_driver);
1002 }
1003 
1004 module_init(sn_sal_module_init);
1005 module_exit(sn_sal_module_exit);
1006 
1007 /*
1008  * Kernel console definitions
1009  */
1010 
1011 #ifdef CONFIG_SGI_L1_SERIAL_CONSOLE
1012 /*
1013  * Print a string to the SAL console.  The console_lock must be held
1014  * when we get here.
1015  */
1016 static void
sn_sal_console_write(struct console * co,const char * s,unsigned count)1017 sn_sal_console_write(struct console *co, const char *s, unsigned count)
1018 {
1019 	unsigned long flags;
1020 
1021 	if (count > CIRC_SPACE_TO_END(xmit.cb_head, xmit.cb_tail,
1022 				      SN_SAL_BUFFER_SIZE))
1023 		sn_debug_printf("\n*** SN_SAL_BUFFER_SIZE too small, lost chars\n");
1024 
1025 	/* somebody really wants this output, might be an
1026 	 * oops, kdb, panic, etc.  make sure they get it. */
1027 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1028 	if (spin_is_locked(&sn_sal_lock)) {
1029 		synch_flush_xmit();
1030 		sn_func->sal_puts(s, count);
1031 	} else
1032 #endif
1033 	if (in_interrupt()) {
1034 		spin_lock_irqsave(&sn_sal_lock, flags);
1035 		synch_flush_xmit();
1036 		spin_unlock_irqrestore(&sn_sal_lock, flags);
1037 		sn_func->sal_puts(s, count);
1038 	} else {
1039 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1040 		sn_sal_write(NULL, 0, s, count);
1041 #else
1042 		synch_flush_xmit();
1043 		sn_func->sal_puts(s, count);
1044 #endif
1045 	}
1046 }
1047 
1048 static kdev_t
sn_sal_console_device(struct console * c)1049 sn_sal_console_device(struct console *c)
1050 {
1051 	return MKDEV(TTY_MAJOR, 64 + c->index);
1052 }
1053 
1054 static int __init
sn_sal_console_setup(struct console * co,char * options)1055 sn_sal_console_setup(struct console *co, char *options)
1056 {
1057 	return 0;
1058 }
1059 
1060 
1061 static struct console sal_console = {
1062 	.name	= "ttyS",
1063 	.write	= sn_sal_console_write,
1064 	.device	= sn_sal_console_device,
1065 	.setup	= sn_sal_console_setup,
1066 	.index	= -1
1067 };
1068 
1069 void __init
sn_sal_serial_console_init(void)1070 sn_sal_serial_console_init(void)
1071 {
1072 	if (ia64_platform_is("sn2")) {
1073 		sn_sal_switch_to_asynch();
1074 		register_console(&sal_console);
1075 	}
1076 }
1077 
1078 #endif /* CONFIG_SGI_L1_SERIAL_CONSOLE */
1079