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