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