1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/interrupt.h>
23#include <linux/tty.h>
24#include <linux/tty_flip.h>
25#include <linux/serial.h>
26#include <linux/console.h>
27#include <linux/init.h>
28#include <linux/sysrq.h>
29#include <linux/circ_buf.h>
30#include <linux/serial_reg.h>
31#include <linux/delay.h>
32#include <linux/miscdevice.h>
33#include <linux/serial_core.h>
34
35#include <asm/io.h>
36#include <asm/sn/simulator.h>
37#include <asm/sn/sn_sal.h>
38
39
40#define SN_SAL_MAX_CHARS 120
41
42
43
44#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
45
46#define SN_SAL_UART_FIFO_DEPTH 16
47#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
48
49
50#define TRANSMIT_BUFFERED 0
51#define TRANSMIT_RAW 1
52
53
54
55
56#define USE_DYNAMIC_MINOR 0
57
58
59#define DEVICE_NAME "ttySG"
60#define DEVICE_NAME_DYNAMIC "ttySG0"
61
62#define DEVICE_MAJOR 204
63#define DEVICE_MINOR 40
64
65#ifdef CONFIG_MAGIC_SYSRQ
66static char sysrq_serial_str[] = "\eSYS";
67static char *sysrq_serial_ptr = sysrq_serial_str;
68static unsigned long sysrq_requested;
69#endif
70
71
72
73
74struct sn_cons_port {
75 struct timer_list sc_timer;
76 struct uart_port sc_port;
77 struct sn_sal_ops {
78 int (*sal_puts_raw) (const char *s, int len);
79 int (*sal_puts) (const char *s, int len);
80 int (*sal_getc) (void);
81 int (*sal_input_pending) (void);
82 void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
83 } *sc_ops;
84 unsigned long sc_interrupt_timeout;
85 int sc_is_asynch;
86};
87
88static struct sn_cons_port sal_console_port;
89static int sn_process_input;
90
91
92static struct miscdevice misc;
93
94extern void early_sn_setup(void);
95
96#undef DEBUG
97#ifdef DEBUG
98static int sn_debug_printf(const char *fmt, ...);
99#define DPRINTF(x...) sn_debug_printf(x)
100#else
101#define DPRINTF(x...) do { } while (0)
102#endif
103
104
105static int snt_hw_puts_raw(const char *, int);
106static int snt_hw_puts_buffered(const char *, int);
107static int snt_poll_getc(void);
108static int snt_poll_input_pending(void);
109static int snt_intr_getc(void);
110static int snt_intr_input_pending(void);
111static void sn_transmit_chars(struct sn_cons_port *, int);
112
113
114
115static struct sn_sal_ops poll_ops = {
116 .sal_puts_raw = snt_hw_puts_raw,
117 .sal_puts = snt_hw_puts_raw,
118 .sal_getc = snt_poll_getc,
119 .sal_input_pending = snt_poll_input_pending
120};
121
122
123static struct sn_sal_ops intr_ops = {
124 .sal_puts_raw = snt_hw_puts_raw,
125 .sal_puts = snt_hw_puts_buffered,
126 .sal_getc = snt_intr_getc,
127 .sal_input_pending = snt_intr_input_pending,
128 .sal_wakeup_transmit = sn_transmit_chars
129};
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148static int snt_poll_getc(void)
149{
150 int ch;
151
152 ia64_sn_console_getc(&ch);
153 return ch;
154}
155
156
157
158
159
160static int snt_poll_input_pending(void)
161{
162 int status, input;
163
164 status = ia64_sn_console_check(&input);
165 return !status && input;
166}
167
168
169
170
171
172
173
174static int snt_intr_getc(void)
175{
176 return ia64_sn_console_readc();
177}
178
179
180
181
182
183static int snt_intr_input_pending(void)
184{
185 return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
186}
187
188
189
190
191
192
193
194
195
196static int snt_hw_puts_raw(const char *s, int len)
197{
198
199 return ia64_sn_console_putb(s, len);
200}
201
202
203
204
205
206
207
208static int snt_hw_puts_buffered(const char *s, int len)
209{
210
211 return ia64_sn_console_xmit_chars((char *)s, len);
212}
213
214
215
216
217
218
219
220
221
222
223
224
225
226static const char *snp_type(struct uart_port *port)
227{
228 return ("SGI SN L1");
229}
230
231
232
233
234
235
236static unsigned int snp_tx_empty(struct uart_port *port)
237{
238 return 1;
239}
240
241
242
243
244
245
246static void snp_stop_tx(struct uart_port *port)
247{
248}
249
250
251
252
253
254
255static void snp_release_port(struct uart_port *port)
256{
257}
258
259
260
261
262
263
264static void snp_shutdown(struct uart_port *port)
265{
266}
267
268
269
270
271
272
273
274static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
275{
276}
277
278
279
280
281
282
283static unsigned int snp_get_mctrl(struct uart_port *port)
284{
285 return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
286}
287
288
289
290
291
292
293static void snp_stop_rx(struct uart_port *port)
294{
295}
296
297
298
299
300
301
302static void snp_start_tx(struct uart_port *port)
303{
304 if (sal_console_port.sc_ops->sal_wakeup_transmit)
305 sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
306 TRANSMIT_BUFFERED);
307
308}
309
310
311
312
313
314
315
316static void snp_break_ctl(struct uart_port *port, int break_state)
317{
318}
319
320
321
322
323
324
325static int snp_startup(struct uart_port *port)
326{
327 return 0;
328}
329
330
331
332
333
334
335
336
337static void
338snp_set_termios(struct uart_port *port, struct ktermios *termios,
339 struct ktermios *old)
340{
341}
342
343
344
345
346
347
348static int snp_request_port(struct uart_port *port)
349{
350 return 0;
351}
352
353
354
355
356
357
358
359static void snp_config_port(struct uart_port *port, int flags)
360{
361}
362
363
364
365static const struct uart_ops sn_console_ops = {
366 .tx_empty = snp_tx_empty,
367 .set_mctrl = snp_set_mctrl,
368 .get_mctrl = snp_get_mctrl,
369 .stop_tx = snp_stop_tx,
370 .start_tx = snp_start_tx,
371 .stop_rx = snp_stop_rx,
372 .break_ctl = snp_break_ctl,
373 .startup = snp_startup,
374 .shutdown = snp_shutdown,
375 .set_termios = snp_set_termios,
376 .pm = NULL,
377 .type = snp_type,
378 .release_port = snp_release_port,
379 .request_port = snp_request_port,
380 .config_port = snp_config_port,
381 .verify_port = NULL,
382};
383
384
385
386#ifdef DEBUG
387
388
389
390
391
392
393
394
395
396static int sn_debug_printf(const char *fmt, ...)
397{
398 static char printk_buf[1024];
399 int printed_len;
400 va_list args;
401
402 va_start(args, fmt);
403 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
404
405 if (!sal_console_port.sc_ops) {
406 sal_console_port.sc_ops = &poll_ops;
407 early_sn_setup();
408 }
409 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
410
411 va_end(args);
412 return printed_len;
413}
414#endif
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429static void
430sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
431{
432 struct tty_port *tport = NULL;
433 int ch;
434
435 if (!port) {
436 printk(KERN_ERR "sn_receive_chars - port NULL so can't receive\n");
437 return;
438 }
439
440 if (!port->sc_ops) {
441 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receive\n");
442 return;
443 }
444
445 if (port->sc_port.state) {
446
447 tport = &port->sc_port.state->port;
448 }
449
450 while (port->sc_ops->sal_input_pending()) {
451 ch = port->sc_ops->sal_getc();
452 if (ch < 0) {
453 printk(KERN_ERR "sn_console: An error occurred while "
454 "obtaining data from the console (0x%0x)\n", ch);
455 break;
456 }
457#ifdef CONFIG_MAGIC_SYSRQ
458 if (sysrq_requested) {
459 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
460
461 sysrq_requested = 0;
462 if (ch && time_before(jiffies, sysrq_timeout)) {
463 spin_unlock_irqrestore(&port->sc_port.lock, flags);
464 handle_sysrq(ch);
465 spin_lock_irqsave(&port->sc_port.lock, flags);
466
467 continue;
468 }
469 }
470 if (ch == *sysrq_serial_ptr) {
471 if (!(*++sysrq_serial_ptr)) {
472 sysrq_requested = jiffies;
473 sysrq_serial_ptr = sysrq_serial_str;
474 }
475
476
477
478
479 if (ch != '\e')
480 continue;
481 }
482 else
483 sysrq_serial_ptr = sysrq_serial_str;
484#endif
485
486
487 if (tport) {
488 if (tty_insert_flip_char(tport, ch, TTY_NORMAL) == 0)
489 break;
490 }
491 port->sc_port.icount.rx++;
492 }
493
494 if (tport)
495 tty_flip_buffer_push(tport);
496}
497
498
499
500
501
502
503
504
505
506
507
508
509
510static void sn_transmit_chars(struct sn_cons_port *port, int raw)
511{
512 int xmit_count, tail, head, loops, ii;
513 int result;
514 char *start;
515 struct circ_buf *xmit;
516
517 if (!port)
518 return;
519
520 BUG_ON(!port->sc_is_asynch);
521
522 if (port->sc_port.state) {
523
524 xmit = &port->sc_port.state->xmit;
525 } else {
526
527
528
529
530 return;
531 }
532
533 if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
534
535 ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
536 return;
537 }
538
539 head = xmit->head;
540 tail = xmit->tail;
541 start = &xmit->buf[tail];
542
543
544
545 loops = (head < tail) ? 2 : 1;
546
547 for (ii = 0; ii < loops; ii++) {
548 xmit_count = (head < tail) ?
549 (UART_XMIT_SIZE - tail) : (head - tail);
550
551 if (xmit_count > 0) {
552 if (raw == TRANSMIT_RAW)
553 result =
554 port->sc_ops->sal_puts_raw(start,
555 xmit_count);
556 else
557 result =
558 port->sc_ops->sal_puts(start, xmit_count);
559#ifdef DEBUG
560 if (!result)
561 DPRINTF("`");
562#endif
563 if (result > 0) {
564 xmit_count -= result;
565 port->sc_port.icount.tx += result;
566 tail += result;
567 tail &= UART_XMIT_SIZE - 1;
568 xmit->tail = tail;
569 start = &xmit->buf[tail];
570 }
571 }
572 }
573
574 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
575 uart_write_wakeup(&port->sc_port);
576
577 if (uart_circ_empty(xmit))
578 snp_stop_tx(&port->sc_port);
579}
580
581
582
583
584
585
586
587static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
588{
589 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
590 unsigned long flags;
591 int status = ia64_sn_console_intr_status();
592
593 if (!port)
594 return IRQ_NONE;
595
596 spin_lock_irqsave(&port->sc_port.lock, flags);
597 if (status & SAL_CONSOLE_INTR_RECV) {
598 sn_receive_chars(port, flags);
599 }
600 if (status & SAL_CONSOLE_INTR_XMIT) {
601 sn_transmit_chars(port, TRANSMIT_BUFFERED);
602 }
603 spin_unlock_irqrestore(&port->sc_port.lock, flags);
604 return IRQ_HANDLED;
605}
606
607
608
609
610
611
612
613
614
615
616static void sn_sal_timer_poll(struct timer_list *t)
617{
618 struct sn_cons_port *port = from_timer(port, t, sc_timer);
619 unsigned long flags;
620
621 if (!port)
622 return;
623
624 if (!port->sc_port.irq) {
625 spin_lock_irqsave(&port->sc_port.lock, flags);
626 if (sn_process_input)
627 sn_receive_chars(port, flags);
628 sn_transmit_chars(port, TRANSMIT_RAW);
629 spin_unlock_irqrestore(&port->sc_port.lock, flags);
630 mod_timer(&port->sc_timer,
631 jiffies + port->sc_interrupt_timeout);
632 }
633}
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
650{
651 unsigned long flags;
652
653 if (!port)
654 return;
655
656 DPRINTF("sn_console: about to switch to asynchronous console\n");
657
658
659
660
661 spin_lock_irqsave(&port->sc_port.lock, flags);
662
663
664 if (!port->sc_ops)
665 port->sc_ops = &poll_ops;
666
667
668
669
670
671
672 timer_setup(&port->sc_timer, sn_sal_timer_poll, 0);
673
674 if (IS_RUNNING_ON_SIMULATOR())
675 port->sc_interrupt_timeout = 6;
676 else {
677
678
679 port->sc_interrupt_timeout =
680 HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
681 }
682 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
683
684 port->sc_is_asynch = 1;
685 spin_unlock_irqrestore(&port->sc_port.lock, flags);
686}
687
688
689
690
691
692
693
694
695
696
697
698
699static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
700{
701 unsigned long flags;
702
703 if (port) {
704 DPRINTF("sn_console: switching to interrupt driven console\n");
705
706 if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
707 IRQF_SHARED,
708 "SAL console driver", port) >= 0) {
709 spin_lock_irqsave(&port->sc_port.lock, flags);
710 port->sc_port.irq = SGI_UART_VECTOR;
711 port->sc_ops = &intr_ops;
712 irq_set_handler(port->sc_port.irq, handle_level_irq);
713
714
715 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
716 spin_unlock_irqrestore(&port->sc_port.lock, flags);
717 }
718 else {
719 printk(KERN_INFO
720 "sn_console: console proceeding in polled mode\n");
721 }
722 }
723}
724
725
726
727
728
729static void sn_sal_console_write(struct console *, const char *, unsigned);
730static int sn_sal_console_setup(struct console *, char *);
731static struct uart_driver sal_console_uart;
732extern struct tty_driver *uart_console_device(struct console *, int *);
733
734static struct console sal_console = {
735 .name = DEVICE_NAME,
736 .write = sn_sal_console_write,
737 .device = uart_console_device,
738 .setup = sn_sal_console_setup,
739 .index = -1,
740 .data = &sal_console_uart,
741};
742
743#define SAL_CONSOLE &sal_console
744
745static struct uart_driver sal_console_uart = {
746 .owner = THIS_MODULE,
747 .driver_name = "sn_console",
748 .dev_name = DEVICE_NAME,
749 .major = 0,
750 .minor = 0,
751 .nr = 1,
752 .cons = SAL_CONSOLE,
753};
754
755
756
757
758
759
760
761
762
763
764static int __init sn_sal_init(void)
765{
766 int retval;
767
768 if (!ia64_platform_is("sn2"))
769 return 0;
770
771 printk(KERN_INFO "sn_console: Console driver init\n");
772
773 if (USE_DYNAMIC_MINOR == 1) {
774 misc.minor = MISC_DYNAMIC_MINOR;
775 misc.name = DEVICE_NAME_DYNAMIC;
776 retval = misc_register(&misc);
777 if (retval != 0) {
778 printk(KERN_WARNING "Failed to register console "
779 "device using misc_register.\n");
780 return -ENODEV;
781 }
782 sal_console_uart.major = MISC_MAJOR;
783 sal_console_uart.minor = misc.minor;
784 } else {
785 sal_console_uart.major = DEVICE_MAJOR;
786 sal_console_uart.minor = DEVICE_MINOR;
787 }
788
789
790
791
792 if (uart_register_driver(&sal_console_uart) < 0) {
793 printk
794 ("ERROR sn_sal_init failed uart_register_driver, line %d\n",
795 __LINE__);
796 return -ENODEV;
797 }
798
799 spin_lock_init(&sal_console_port.sc_port.lock);
800
801
802 sal_console_port.sc_port.membase = (char *)1;
803 sal_console_port.sc_port.type = PORT_16550A;
804 sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
805 sal_console_port.sc_port.ops = &sn_console_ops;
806 sal_console_port.sc_port.line = 0;
807
808 if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
809
810 printk(KERN_ERR "%s: unable to add port\n", __func__);
811 }
812
813
814
815
816 if (!sal_console_port.sc_is_asynch) {
817 sn_sal_switch_to_asynch(&sal_console_port);
818 }
819
820
821 if (!IS_RUNNING_ON_SIMULATOR()) {
822 sn_sal_switch_to_interrupts(&sal_console_port);
823 }
824 sn_process_input = 1;
825 return 0;
826}
827device_initcall(sn_sal_init);
828
829
830
831
832
833
834
835
836
837
838
839
840static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
841 const char *s, int count)
842{
843 const char *s1;
844
845
846 while ((s1 = memchr(s, '\n', count)) != NULL) {
847 puts_raw(s, s1 - s);
848 puts_raw("\r\n", 2);
849 count -= s1 + 1 - s;
850 s = s1 + 1;
851 }
852 puts_raw(s, count);
853}
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872static void
873sn_sal_console_write(struct console *co, const char *s, unsigned count)
874{
875 unsigned long flags = 0;
876 struct sn_cons_port *port = &sal_console_port;
877 static int stole_lock = 0;
878
879 BUG_ON(!port->sc_is_asynch);
880
881
882
883
884 if (!port->sc_port.state) {
885
886 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
887 return;
888 }
889
890
891
892 if (!spin_trylock_irqsave(&port->sc_port.lock, flags)) {
893 int lhead = port->sc_port.state->xmit.head;
894 int ltail = port->sc_port.state->xmit.tail;
895 int counter, got_lock = 0;
896
897
898
899
900
901
902
903
904
905
906
907
908 for (counter = 0; counter < 150; mdelay(125), counter++) {
909 if (stole_lock)
910 break;
911
912 if (spin_trylock_irqsave(&port->sc_port.lock, flags)) {
913 got_lock = 1;
914 break;
915 } else {
916
917 if ((lhead != port->sc_port.state->xmit.head)
918 || (ltail !=
919 port->sc_port.state->xmit.tail)) {
920 lhead =
921 port->sc_port.state->xmit.head;
922 ltail =
923 port->sc_port.state->xmit.tail;
924 counter = 0;
925 }
926 }
927 }
928
929 sn_transmit_chars(port, 1);
930 if (got_lock) {
931 spin_unlock_irqrestore(&port->sc_port.lock, flags);
932 stole_lock = 0;
933 } else {
934
935 stole_lock = 1;
936 }
937 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
938 } else {
939 stole_lock = 0;
940 sn_transmit_chars(port, 1);
941 spin_unlock_irqrestore(&port->sc_port.lock, flags);
942
943 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
944 }
945}
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960static int sn_sal_console_setup(struct console *co, char *options)
961{
962 return 0;
963}
964
965
966
967
968
969
970
971
972
973
974
975
976static void __init
977sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
978{
979 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
980}
981
982
983
984static struct console sal_console_early __initdata = {
985 .name = "sn_sal",
986 .write = sn_sal_console_write_early,
987 .flags = CON_PRINTBUFFER,
988 .index = -1,
989};
990
991
992
993
994
995
996
997
998
999
1000int __init sn_serial_console_early_setup(void)
1001{
1002 if (!ia64_platform_is("sn2"))
1003 return -1;
1004
1005 sal_console_port.sc_ops = &poll_ops;
1006 spin_lock_init(&sal_console_port.sc_port.lock);
1007 early_sn_setup();
1008 register_console(&sal_console_early);
1009
1010 return 0;
1011}
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025static int __init sn_sal_serial_console_init(void)
1026{
1027 if (ia64_platform_is("sn2")) {
1028 sn_sal_switch_to_asynch(&sal_console_port);
1029 DPRINTF("sn_sal_serial_console_init : register console\n");
1030 register_console(&sal_console);
1031 unregister_console(&sal_console_early);
1032 }
1033 return 0;
1034}
1035
1036console_initcall(sn_sal_serial_console_init);
1037