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