summaryrefslogtreecommitdiffstats
path: root/bsps/powerpc/tqm8xx/console/console.c
blob: 91eeff12f41819c361e102da9102d729d24d870f (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
/*===============================================================*\
| Project: RTEMS TQM8xx BSP                                       |
+-----------------------------------------------------------------+
| This file has been adapted to MPC8xx by                         |
|    Thomas Doerfler <Thomas.Doerfler@embedded-brains.de>         |
|                    Copyright (c) 2008                           |
|                    Embedded Brains GmbH                         |
|                    Obere Lagerstr. 30                           |
|                    D-82178 Puchheim                             |
|                    Germany                                      |
|                    rtems@embedded-brains.de                     |
|                                                                 |
| See the other copyright notice below for the original parts.    |
+-----------------------------------------------------------------+
| The license and distribution terms for this file may be         |
| found in the file LICENSE in this distribution or at            |
|                                                                 |
| http://www.rtems.org/license/LICENSE.                           |
|                                                                 |
+-----------------------------------------------------------------+
| this file contains the console driver                           |
\*===============================================================*/
/* derived from: */
/*
 *  SMC1/2 SCC1..4 raw console serial I/O.
 *  adapted to work with up to 4 SCC and 2 SMC
 *
 *  This driver is an example of `TASK DRIVEN' `POLLING' or `INTERRUPT' I/O.
 *
 *  To run with interrupt-driven I/O, ensure m8xx_smc1_interrupt
 *  is set before calling the initialization routine.
 *
 *  Author:
 *    W. Eric Norum
 *    Saskatchewan Accelerator Laboratory
 *    University of Saskatchewan
 *    Saskatoon, Saskatchewan, CANADA
 *    eric@skatter.usask.ca
 *
 *  COPYRIGHT (c) 1989-1998.
 *  On-Line Applications Research Corporation (OAR).
 *  Copyright assigned to U.S. Government, 1994.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 */

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

#include <rtems.h>
#include <rtems/console.h>
#include <rtems/libio.h>
#include <rtems/termiostypes.h>
#include <rtems/bspIo.h>
#include <rtems/error.h>

#include <bsp.h>
#include <mpc8xx.h>
#include <bsp/irq.h>

/*
 * Interrupt-driven input buffer
 */
#define RXBUFSIZE	16

#define M8xx_SICR_BRG1 (0)
#define M8xx_SICR_BRG2 (1)
#define M8xx_SICR_BRG3 (2)
#define M8xx_SICR_BRG4 (3)

#define M8xx_SICR_SCCRX_MSK(scc) ((  7) << (((scc))*8+3))
#define M8xx_SICR_SCCRX(scc,clk) ((clk) << (((scc))*8+3))

#define M8xx_SICR_SCCTX_MSK(scc) ((  7) << (((scc))*8+0))
#define M8xx_SICR_SCCTX(scc,clk) ((clk) << (((scc))*8+0))

#define M8xx_SIMODE_SMCCS(smc,clk) ((clk) << ((smc)*16+12))
#define M8xx_SIMODE_SMCCS_MSK(smc) M8xx_SIMODE_SMCCS(smc,7)

#define CONS_CHN_CNT 6
#define CONS_CHN_SCC1 0
#define CONS_CHN_SCC2 1
#define CONS_CHN_SCC3 2
#define CONS_CHN_SCC4 3
#define CONS_CHN_SMC1 4
#define CONS_CHN_SMC2 5
#define CONS_CHN_NONE -1

/*
 * possible identifiers for bspopts.h: CONS_SxCy_MODE
 */
#define CONS_MODE_UNUSED -1
#define CONS_MODE_POLLED TERMIOS_POLLED
#define CONS_MODE_IRQ    TERMIOS_IRQ_DRIVEN

#define CHN_IS_SCC(chan) ((chan) < CONS_CHN_SMC1)

#define BRG_CNT 4

#define MAX_IDL_DEFAULT 10
#define DEVICEPREFIX "tty"

/*
 * I/O buffers and pointers to buffer descriptors
 */
#define SCC_RXBD_CNT 4
#define SCC_TXBD_CNT 4
typedef volatile char sccRxBuf_t[SCC_RXBD_CNT][RXBUFSIZE];

/*
 * Interrupt-driven callback
 */
static int m8xx_scc_mode[CONS_CHN_CNT];
static void *sccttyp[CONS_CHN_CNT];
typedef struct m8xx_console_chan_desc_s {
  bool is_scc;                  /* true for SCC */
  struct {
    volatile m8xxSCCparms_t *sccp;
    volatile m8xxSMCparms_t *smcp;
  } parms;
  struct {
    volatile m8xxSCCRegisters_t *sccr;
    volatile m8xxSMCRegisters_t *smcr;
  } regs;
  rtems_vector_number ivec_src;
  int cr_chan_code;
  int brg_used;
  sccRxBuf_t *rxBuf;
} m8xx_console_chan_desc_t;

m8xx_console_chan_desc_t m8xx_console_chan_desc[CONS_CHN_CNT] = {
  /* SCC1 */
  { .is_scc = true,
   .parms = {(m8xxSCCparms_t *)&(m8xx.scc1p),NULL},
   .regs = {&(m8xx.scc1),NULL},
   .ivec_src = BSP_CPM_IRQ_SCC1,
   .cr_chan_code = M8xx_CR_CHAN_SCC1,
   .brg_used = -1},
  /* SCC2 */
  { .is_scc = true,
   .parms = {&(m8xx.scc2p),NULL},
   .regs = {&(m8xx.scc2),NULL},
   .ivec_src = BSP_CPM_IRQ_SCC2,
   .cr_chan_code = M8xx_CR_CHAN_SCC2,
   .brg_used = -1},
  /* SCC3 */
  { .is_scc = true,
   .parms = {&(m8xx.scc3p),NULL},
   .regs = {&(m8xx.scc3),NULL},
   .ivec_src = BSP_CPM_IRQ_SCC3,
   .cr_chan_code = M8xx_CR_CHAN_SCC3,
   .brg_used = -1},
  /* SCC4 */
  { .is_scc = true,
   .parms = {&(m8xx.scc4p),NULL},
   .regs = {&(m8xx.scc4),NULL},
   .ivec_src = BSP_CPM_IRQ_SCC4,
   .cr_chan_code = M8xx_CR_CHAN_SCC4,
   .brg_used = -1},
  /* SMC1 */
  { .is_scc = false,
   .parms = {NULL,&(m8xx.smc1p)},
   .regs = {NULL,&(m8xx.smc1)},
   .ivec_src = BSP_CPM_IRQ_SMC1,
   .cr_chan_code = M8xx_CR_CHAN_SMC1,
   .brg_used = -1},
  /* SMC2 */
  { .is_scc = false,
   .parms = {NULL,&(m8xx.smc2p)},
   .regs = {NULL,&(m8xx.smc2)},
   .ivec_src = BSP_CPM_IRQ_SMC2_OR_PIP,
   .cr_chan_code = M8xx_CR_CHAN_SMC2,
   .brg_used = -1}};

#define CHN_PARAM_GET(chan,param)			\
  (m8xx_console_chan_desc[chan].is_scc			\
   ? m8xx_console_chan_desc[chan].parms.sccp->param	\
   : m8xx_console_chan_desc[chan].parms.smcp->param)

#define CHN_PARAM_SET(chan,param,value)				\
  do {if (m8xx_console_chan_desc[chan].is_scc)			\
      m8xx_console_chan_desc[chan].parms.sccp->param = value;	\
    else							\
      m8xx_console_chan_desc[chan].parms.smcp->param = value;	\
  } while (0)

#define CHN_EVENT_GET(chan)				\
  (m8xx_console_chan_desc[chan].is_scc			\
   ? m8xx_console_chan_desc[chan].regs.sccr->scce	\
   : m8xx_console_chan_desc[chan].regs.smcr->smce)

#define CHN_EVENT_CLR(chan,mask) 				\
  do {								\
    if (m8xx_console_chan_desc[chan].is_scc) 			\
      m8xx_console_chan_desc[chan].regs.sccr->scce = (mask);	\
    else							\
      m8xx_console_chan_desc[chan].regs.smcr->smce = (mask);	\
  }while (0)

#define CHN_MASK_GET(chan)				\
  (m8xx_console_chan_desc[chan].is_scc			\
   ? m8xx_console_chan_desc[chan].regs.sccr->sccm	\
   : m8xx_console_chan_desc[chan].regs.smcr->smcm)

#define CHN_MASK_SET(chan,mask) 				\
  do {								\
    if (m8xx_console_chan_desc[chan].is_scc) 			\
      m8xx_console_chan_desc[chan].regs.sccr->sccm = (mask);	\
    else							\
      m8xx_console_chan_desc[chan].regs.smcr->smcm = (mask);	\
  }while (0)


static volatile m8xxBufferDescriptor_t *sccFrstRxBd[CONS_CHN_CNT];
static volatile m8xxBufferDescriptor_t *sccCurrRxBd[CONS_CHN_CNT];
static volatile m8xxBufferDescriptor_t *sccFrstTxBd[CONS_CHN_CNT];
static volatile m8xxBufferDescriptor_t *sccPrepTxBd[CONS_CHN_CNT];
static volatile m8xxBufferDescriptor_t *sccDequTxBd[CONS_CHN_CNT];

/*
 * Compute baud-rate-generator configuration register value
 */
static uint32_t
sccBRGval (int baud)
{
  int divisor;
  int div16 = 0;

  divisor = ((BSP_bus_frequency / 16) + (baud / 2)) / baud;
  if (divisor > 4096) {
    div16 = 1;
    divisor = (divisor + 8) / 16;
  }
  return M8xx_BRG_EN | M8xx_BRG_EXTC_BRGCLK | ((divisor - 1) << 1) | div16;
}

typedef struct {
  uint32_t reg_content;
  int link_cnt;
}brg_state_t;
brg_state_t scc_brg_state[BRG_CNT];

/*
 * initialize brg_state
 */
static void sccBRGinit(void)
{
  int brg_idx;

  for (brg_idx = 0;brg_idx < BRG_CNT;brg_idx++) {
    scc_brg_state[brg_idx].reg_content = 0;
    scc_brg_state[brg_idx].link_cnt    = 0;
  }
#ifndef MDE360
  /*
   * on ZEM40, init CLK4/5 inputs
   */
  m8xx.papar |=  ((1 << 11) | (1 << 12));
  m8xx.padir &= ~((1 << 11) | (1 << 12));
#endif
}

#if CONS_USE_EXT_CLK
/*
 * input clock frq for CPM clock inputs
 */
static uint32_t clkin_frq[2][4] = {
#ifdef MDE360
  {0,0,0,0},
  {0,0,0,0}
#else
  {0,0,0,1843000},
  {1843000,0,0,0}
#endif
};
#endif

/*
 * allocate, set and connect baud rate generators
 * FIXME: or clock input
 * FIXME: set pin to be clock input
 */

static int sccBRGalloc(int chan,int baud)
{
  rtems_interrupt_level level;
  m8xx_console_chan_desc_t *chan_desc = &(m8xx_console_chan_desc[chan]);
  uint32_t reg_val;
  int old_brg;
  int new_brg = -1;
  int brg_idx;
#if CONS_USE_EXT_CLK
  int clk_group;
  int clk_sel;
#endif

  old_brg = chan_desc->brg_used;
  /* compute brg register contents needed */
  reg_val = sccBRGval(baud);

#if CONS_EXT_CLK
  /* search for clock input with this frq */
  clk_group = ((chan == CONS_CHN_SCC3) ||
	       (chan == CONS_CHN_SCC4) ||
	       (chan == CONS_CHN_SMC2)) ? 1 : 0;

  for (clk_sel = 0, new_brg = -1;
       (clk_sel < 4) && (new_brg < 0);
       clk_sel++) {
    if (baud == (clkin_frq[clk_group][clk_sel] / 16)) {
      new_brg = clk_sel + 4;
    }
  }
#endif

  rtems_interrupt_disable(level);

  if (new_brg < 0) {
    /* search for brg with this settings */
    for (brg_idx = 0;
	 (new_brg < 0) && (brg_idx < BRG_CNT);
	 brg_idx++) {
      if (scc_brg_state[brg_idx].reg_content == reg_val) {
	new_brg = brg_idx;
      }
    }
    /*
     * if not found: check, whether brg currently in use
     * is linked only from our channel
     */
    if ((new_brg < 0) &&
	(old_brg >= 0) &&
	(scc_brg_state[old_brg].link_cnt == 1)) {
      new_brg = old_brg;
    }
    /* if not found: search for unused brg, set it  */
    for (brg_idx = 0;
	 (new_brg < 0) && (brg_idx < BRG_CNT);
	 brg_idx++) {
      if (scc_brg_state[brg_idx].link_cnt == 0) {
	new_brg = brg_idx;
      }
    }
  }

  /* decrease old link count */
  if ((old_brg >= 0) &&
      (old_brg < 4)) {
    scc_brg_state[old_brg].link_cnt--;
  }
  /* increase new brg link count, set brg */
  if ((new_brg >= 0) &&
      (new_brg < 4)) {
    scc_brg_state[new_brg].link_cnt++;
    scc_brg_state[new_brg].reg_content = reg_val;
    (&m8xx.brgc1)[new_brg] = reg_val;
  }
  rtems_interrupt_enable(level);

  /* connect to scc/smc */
  if (new_brg >= 0) {
    m8xx_console_chan_desc[chan].brg_used = new_brg;
    /*
     * Put SCC in NMSI mode, connect SCC to BRG or CLKx
     */
    if (m8xx_console_chan_desc[chan].is_scc) {
      m8xx.sicr = ((m8xx.sicr & ~(M8xx_SICR_SCCRX_MSK(chan) |
				  M8xx_SICR_SCCTX_MSK(chan))) |
		   M8xx_SICR_SCCRX(chan,new_brg)|
		   M8xx_SICR_SCCTX(chan,new_brg));
    }
    else {
      /* connect SMC to BRGx or CLKx... */
      m8xx.simode = ((m8xx.simode & ~(M8xx_SIMODE_SMCCS_MSK(chan - CONS_CHN_SMC1)))|
		     M8xx_SIMODE_SMCCS(chan - CONS_CHN_SMC1,new_brg));
    }
  }
  return (new_brg < 0);
}


/*
 * Hardware-dependent portion of tcsetattr().
 */
static int
sccSetAttributes (int minor, const struct termios *t)
{
  int baud;

  switch (t->c_ospeed) {
  default:	baud = -1;	break;
  case B50:	baud = 50;	break;
  case B75:	baud = 75;	break;
  case B110:	baud = 110;	break;
  case B134:	baud = 134;	break;
  case B150:	baud = 150;	break;
  case B200:	baud = 200;	break;
  case B300:	baud = 300;	break;
  case B600:	baud = 600;	break;
  case B1200:	baud = 1200;	break;
  case B1800:	baud = 1800;	break;
  case B2400:	baud = 2400;	break;
  case B4800:	baud = 4800;	break;
  case B9600:	baud = 9600;	break;
  case B19200:	baud = 19200;	break;
  case B38400:	baud = 38400;	break;
  case B57600:	baud = 57600;	break;
  case B115200:	baud = 115200;	break;
  case B230400:	baud = 230400;	break;
  case B460800:	baud = 460800;	break;
  }
  return sccBRGalloc(minor,baud);
  return 0;
}

/*
 * Interrupt handler
 */
static rtems_isr
sccInterruptHandler (void *arg)
{
  int chan = (int)arg;

  /*
   * Buffer received?
   */
  if (CHN_EVENT_GET(chan) & 0x1) {
    /*
     * clear SCC event flag
     */
    CHN_EVENT_CLR(chan,0x01);
    /*
     * process event
     */
    while ((sccCurrRxBd[chan]->status & M8xx_BD_EMPTY) == 0) {
      if (sccttyp[chan] != NULL) {
	rtems_cache_invalidate_multiple_data_lines((void *)sccCurrRxBd[chan]->buffer,
						   sccCurrRxBd[chan]->length);
	rtems_termios_enqueue_raw_characters (sccttyp[chan],
					      (char *)sccCurrRxBd[chan]->buffer,
					      sccCurrRxBd[chan]->length);
      }
      /*
       * clear status
       */
      sccCurrRxBd[chan]->status =
	(sccCurrRxBd[chan]->status
	 & (M8xx_BD_WRAP | M8xx_BD_INTERRUPT))
	| M8xx_BD_EMPTY;
      /*
       * advance to next BD
       */
      if ((sccCurrRxBd[chan]->status & M8xx_BD_WRAP) != 0) {
	sccCurrRxBd[chan] = sccFrstRxBd[chan];
      }
      else {
	sccCurrRxBd[chan]++;
      }
    }
  }
  /*
   * Buffer transmitted?
   */
  if (CHN_EVENT_GET(chan) & 0x2) {
    /*
     * then clear interrupt event bit
     */
    CHN_EVENT_CLR(chan,0x2);
    /*
     * and signal successful transmit to termios
     */
    /*
     * FIXME: multiple dequeue calls for multiple buffers
     */
    while((sccDequTxBd[chan] != sccPrepTxBd[chan]) &&
	  ((sccDequTxBd[chan]->status & M8xx_BD_READY) == 0)) {
      if (sccttyp[chan] != NULL) {
	rtems_termios_dequeue_characters (sccttyp[chan],
					  sccDequTxBd[chan]->length);
      }
      /*
       * advance to next BD
       */
      if ((sccDequTxBd[chan]->status & M8xx_BD_WRAP) != 0) {
	sccDequTxBd[chan] = sccFrstTxBd[chan];
      }
      else {
	sccDequTxBd[chan]++;
      }
    }
  }
}

static void
mpc8xx_console_irq_on(int chan)
{
    CHN_MASK_SET(chan, 3);	/* Enable TX and RX interrupts */
}

static void
sccInitialize (m8xx_console_chan_desc_t *cd, int chan)
{
  int i;
  /*
   * allocate buffers
   * FIXME: use a cache-line size boundary alloc here
   */
  cd->rxBuf = rtems_cache_aligned_malloc(sizeof(*cd->rxBuf));
  if (cd->rxBuf == NULL) {
    rtems_panic("Cannot allocate console rx buffer\n");
  }

  /*
   * Allocate buffer descriptors
   */
  sccCurrRxBd[chan] =
    sccFrstRxBd[chan] = m8xx_bd_allocate(SCC_RXBD_CNT);
  sccPrepTxBd[chan] =
    sccDequTxBd[chan] =
    sccFrstTxBd[chan] = m8xx_bd_allocate(SCC_TXBD_CNT);
  switch(chan) {
  case CONS_CHN_SCC1:
    /*
     * Configure port A pins to enable TXD1 and RXD1 pins
     * FIXME: add setup for modem control lines....
     */
    m8xx.papar |=  0x03;
    m8xx.padir &= ~0x03;

    /*
     * Configure port C pins to enable RTS1 pins (static active low)
     */
    m8xx.pcpar &= ~0x01;
    m8xx.pcso  &= ~0x01;
    m8xx.pcdir |=  0x01;
    m8xx.pcdat &= ~0x01;
    break;
  case CONS_CHN_SCC2:
    /*
     * Configure port A pins to enable TXD2 and RXD2 pins
     * FIXME: add setup for modem control lines....
     */
    m8xx.papar |=  0x0C;
    m8xx.padir &= ~0x0C;

    /*
     * Configure port C pins to enable RTS2 pins (static active low)
     */
    m8xx.pcpar &= ~0x02;
    m8xx.pcso  &= ~0x02;
    m8xx.pcdir |=  0x02;
    m8xx.pcdat &= ~0x02;
    break;
  case CONS_CHN_SCC3:
    /*
     * Configure port A pins to enable TXD3 and RXD3 pins
     * FIXME: add setup for modem control lines....
     */
    m8xx.papar |=  0x30;
    m8xx.padir &= ~0x30;

    /*
     * Configure port C pins to enable RTS3 (static active low)
     */
    m8xx.pcpar &= ~0x04;
    m8xx.pcso  &= ~0x04;
    m8xx.pcdir |=  0x04;
    m8xx.pcdat &= ~0x04;
    break;
  case CONS_CHN_SCC4:
    /*
     * Configure port A pins to enable TXD4 and RXD4 pins
     * FIXME: add setup for modem control lines....
     */
    m8xx.papar |=  0xC0;
    m8xx.padir &= ~0xC0;

    /*
     * Configure port C pins to enable RTS4 pins (static active low)
     */
    m8xx.pcpar &= ~0x08;
    m8xx.pcso  &= ~0x08;
    m8xx.pcdir |=  0x08;
    m8xx.pcdat &= ~0x08;
    break;
  case CONS_CHN_SMC1:
    /*
     * Configure port B pins to enable SMTXD1 and SMRXD1 pins
     */
    m8xx.pbpar |=  0xC0;
    m8xx.pbdir &= ~0xC0;
    break;
  case CONS_CHN_SMC2:
    /*
     * Configure port B pins to enable SMTXD2 and SMRXD2 pins
     */
    m8xx.pbpar |=  0xC00;
    m8xx.pbdir &= ~0xC00;
    break;
  }
  /*
   * allocate and connect BRG
   */
  sccBRGalloc(chan,9600);


  /*
   * Set up SCCx parameter RAM common to all protocols
   */
  CHN_PARAM_SET(chan,rbase,(char *)sccFrstRxBd[chan] - (char *)&m8xx);
  CHN_PARAM_SET(chan,tbase,(char *)sccFrstTxBd[chan] - (char *)&m8xx);
  CHN_PARAM_SET(chan,rfcr ,M8xx_RFCR_MOT | M8xx_RFCR_DMA_SPACE(0));
  CHN_PARAM_SET(chan,tfcr ,M8xx_TFCR_MOT | M8xx_TFCR_DMA_SPACE(0));
  if (m8xx_scc_mode[chan] != TERMIOS_POLLED)
    CHN_PARAM_SET(chan,mrblr,RXBUFSIZE);
  else
    CHN_PARAM_SET(chan,mrblr,1);

  /*
   * Set up SCCx parameter RAM UART-specific parameters
   */
  CHN_PARAM_SET(chan,un.uart.max_idl ,MAX_IDL_DEFAULT);
  CHN_PARAM_SET(chan,un.uart.brkln   ,0);
  CHN_PARAM_SET(chan,un.uart.brkec   ,0);
  CHN_PARAM_SET(chan,un.uart.brkcr   ,0);
  if (m8xx_console_chan_desc[chan].is_scc) {
    m8xx_console_chan_desc[chan].parms.sccp->un.uart.character[0]=0x8000; /* no char filter */
    m8xx_console_chan_desc[chan].parms.sccp->un.uart.rccm=0x80FF; /* control character mask */
  }

  /*
   * Set up the Receive Buffer Descriptors
   */
  for (i = 0;i < SCC_RXBD_CNT;i++) {
    sccFrstRxBd[chan][i].status = M8xx_BD_EMPTY | M8xx_BD_INTERRUPT;
    if (i == SCC_RXBD_CNT-1) {
      sccFrstRxBd[chan][i].status |= M8xx_BD_WRAP;
    }
    sccFrstRxBd[chan][i].length = 0;
    sccFrstRxBd[chan][i].buffer = (*cd->rxBuf)[i];
  }
  /*
   * Setup the Transmit Buffer Descriptor
   */
  for (i = 0;i < SCC_TXBD_CNT;i++) {
    sccFrstTxBd[chan][i].status = M8xx_BD_INTERRUPT;
    if (i == SCC_TXBD_CNT-1) {
      sccFrstTxBd[chan][i].status |= M8xx_BD_WRAP;
    }
    sccFrstTxBd[chan][i].length = 0;
    sccFrstTxBd[chan][i].buffer = NULL;
  }

  /*
   * Set up SCC general and protocol-specific mode registers
   */
  CHN_EVENT_CLR(chan,~0);	/* Clear any pending events */
  CHN_MASK_SET(chan,0);	        /* Mask all interrupt/event sources */

  if (m8xx_console_chan_desc[chan].is_scc) {
    m8xx_console_chan_desc[chan].regs.sccr->psmr = 0xb000; /* 8N1, CTS flow control */
    m8xx_console_chan_desc[chan].regs.sccr->gsmr_h = 0x00000000;
    m8xx_console_chan_desc[chan].regs.sccr->gsmr_l = 0x00028004; /* UART mode */
  }
  else {
    m8xx_console_chan_desc[chan].regs.smcr->smcmr = 0x4820;
  }
  /*
   * Send "Init parameters" command
   */
  m8xx_cp_execute_cmd(M8xx_CR_OP_INIT_RX_TX
		      | m8xx_console_chan_desc[chan].cr_chan_code);

  /*
   * Enable receiver and transmitter
   */
  if (m8xx_console_chan_desc[chan].is_scc) {
    m8xx_console_chan_desc[chan].regs.sccr->gsmr_l |= 0x00000030;
  }
  else {
    m8xx_console_chan_desc[chan].regs.smcr->smcmr |= 0x0003;
  }

  if (m8xx_scc_mode[chan] != TERMIOS_POLLED) {
    rtems_status_code sc;

    sc = rtems_interrupt_handler_install(
      m8xx_console_chan_desc[chan].ivec_src,
      "SCC",
      RTEMS_INTERRUPT_UNIQUE,
      sccInterruptHandler,
      (void *)chan
    );
    if (sc != RTEMS_SUCCESSFUL) {
      rtems_panic("console: cannot install IRQ handler");
    }
    mpc8xx_console_irq_on(chan);
  }
}

/*
 * polled scc read function
 */
static int
sccPollRead (int minor)
{
  int c = -1;
  int chan = minor;

  while(1) {
    if ((sccCurrRxBd[chan]->status & M8xx_BD_EMPTY) != 0) {
      return -1;
    }

    if (0 == (sccCurrRxBd[chan]->status & (M8xx_BD_OVERRUN
					   | M8xx_BD_PARITY_ERROR
					   | M8xx_BD_FRAMING_ERROR
					   | M8xx_BD_BREAK
					   | M8xx_BD_IDLE))) {
      /* character received and no error detected */
      rtems_cache_invalidate_multiple_data_lines((void *)sccCurrRxBd[chan]->buffer,
						 sccCurrRxBd[chan]->length);
      c = (unsigned)*((char *)sccCurrRxBd[chan]->buffer);
      /*
       * clear status
       */
    }
    sccCurrRxBd[chan]->status =
      (sccCurrRxBd[chan]->status
       & (M8xx_BD_WRAP | M8xx_BD_INTERRUPT))
      | M8xx_BD_EMPTY;
    /*
     * advance to next BD
     */
    if ((sccCurrRxBd[chan]->status & M8xx_BD_WRAP) != 0) {
      sccCurrRxBd[chan] = sccFrstRxBd[chan];
    }
    else {
      sccCurrRxBd[chan]++;
    }
    if (c >= 0) {
      return c;
    }
  }
}


/*
 * Device-dependent write routine
 * Interrupt-driven devices:
 *	Begin transmission of as many characters as possible (minimum is 1).
 * Polling devices:
 *	Transmit all characters.
 */
static ssize_t
sccInterruptWrite (int minor, const char *buf, size_t len)
{
  if (len > 0) {
    int chan = minor;

    if ((sccPrepTxBd[chan]->status & M8xx_BD_READY) == 0) {
      sccPrepTxBd[chan]->buffer = (char *)buf;
      sccPrepTxBd[chan]->length = len;
      rtems_cache_flush_multiple_data_lines((const void *)buf,len);
      /*
       * clear status, set ready bit
       */
      sccPrepTxBd[chan]->status =
        (sccPrepTxBd[chan]->status
         & M8xx_BD_WRAP)
        | M8xx_BD_READY | M8xx_BD_INTERRUPT;
      if ((sccPrepTxBd[chan]->status & M8xx_BD_WRAP) != 0) {
        sccPrepTxBd[chan] = sccFrstTxBd[chan];
      }
      else {
        sccPrepTxBd[chan]++;
      }
    }
  }

  return 0;
}

static ssize_t
sccPollWrite (int minor, const char *buf, size_t len)
{
  static char txBuf[CONS_CHN_CNT][SCC_TXBD_CNT];
  int chan = minor;
  int bd_used;
  size_t retval = len;

  while (len--) {
    while (sccPrepTxBd[chan]->status & M8xx_BD_READY)
      continue;
    bd_used = sccPrepTxBd[chan]-sccFrstTxBd[chan];
    txBuf[chan][bd_used] = *buf++;
      rtems_cache_flush_multiple_data_lines((const void *)&txBuf[chan][bd_used],
					    sizeof(txBuf[chan][bd_used]));
    sccPrepTxBd[chan]->buffer = &(txBuf[chan][bd_used]);
    sccPrepTxBd[chan]->length = 1;
    sccPrepTxBd[chan]->status =
      (sccPrepTxBd[chan]->status
       & M8xx_BD_WRAP)
      | M8xx_BD_READY;
    if ((sccPrepTxBd[chan]->status & M8xx_BD_WRAP) != 0) {
      sccPrepTxBd[chan] = sccFrstTxBd[chan];
    }
    else {
      sccPrepTxBd[chan]++;
    }
  }
  return retval;
}

/*
 * printk basic support
 */
int BSP_output_chan = CONS_CHN_NONE; /* channel used for printk operation */

static void console_debug_putc_onlcr(const char c)
{
  rtems_interrupt_level irq_level;

  if (BSP_output_chan != CONS_CHN_NONE) {
    rtems_interrupt_disable(irq_level);

    sccPollWrite (BSP_output_chan,&c,1);
    rtems_interrupt_enable(irq_level);
  }
}

BSP_output_char_function_type     BSP_output_char = console_debug_putc_onlcr;
BSP_polling_getchar_function_type BSP_poll_char = NULL;


/*
***************
* BOILERPLATE *
***************
*/

struct {
  rtems_device_minor_number minor;
  int driver_mode;
} channel_list[] = {
  {CONS_CHN_SMC1,CONS_SMC1_MODE},
  {CONS_CHN_SMC2,CONS_SMC2_MODE},
  {CONS_CHN_SCC1,CONS_SCC1_MODE},
  {CONS_CHN_SCC2,CONS_SCC2_MODE},
  {CONS_CHN_SCC3,CONS_SCC3_MODE},
  {CONS_CHN_SCC4,CONS_SCC4_MODE}
};


/*
 * Initialize and register the device
 */
rtems_device_driver console_initialize(rtems_device_major_number  major,
				       rtems_device_minor_number  minor,/* ignored */
				       void                      *arg
				       )
{
  rtems_status_code status = RTEMS_SUCCESSFUL;
  int chan,entry,ttynum;
  char tty_name[] = "/dev/tty00";

  /*
   * Set up TERMIOS
   */
  rtems_termios_initialize ();
  /*
   * init BRG allocataion
   */
  sccBRGinit();
  ttynum = 0;
  for (entry = 0;
       (entry < sizeof(channel_list)/sizeof(channel_list[0]))
	 && (status == RTEMS_SUCCESSFUL);
       entry++) {
    if (channel_list[entry].driver_mode != CONS_MODE_UNUSED) {
      /*
       * Do device-specific initialization
       */
      chan = channel_list[entry].minor;
      m8xx_scc_mode[chan] = channel_list[entry].driver_mode;
      sccInitialize (&m8xx_console_chan_desc[chan], chan);

      /*
       * build device name
       */
      tty_name[sizeof(tty_name)-2] = '0'+ttynum;
      ttynum++;
      /*
       * Register the device
       */
      status = rtems_io_register_name (tty_name,
				       major,
				       channel_list[entry].minor);
      if (status != RTEMS_SUCCESSFUL) {
	rtems_fatal_error_occurred (status);
      }
    }
  }
  /*
   * register /dev/console
   */
  status = rtems_io_register_name ("/dev/console",
				   major,
				   CONSOLE_CHN);
  if (status != RTEMS_SUCCESSFUL) {
    rtems_fatal_error_occurred (status);
  }
  /*
   * enable printk support
   */
  BSP_output_chan = PRINTK_CHN;

  return RTEMS_SUCCESSFUL;
}

/*
 * Open the device
 */
rtems_device_driver console_open(
				 rtems_device_major_number major,
				 rtems_device_minor_number minor,
				 void                    * arg
				 )
{
  rtems_status_code status;
  int chan = minor;
  rtems_libio_open_close_args_t *args = (rtems_libio_open_close_args_t *)arg;
  static const rtems_termios_callbacks interruptCallbacks = {
    NULL,		/* firstOpen */
    NULL,		/* lastClose */
    NULL,	        /* pollRead */
    sccInterruptWrite,	/* write */
    sccSetAttributes,	/* setAttributes */
    NULL,		/* stopRemoteTx */
    NULL,		/* startRemoteTx */
    TERMIOS_IRQ_DRIVEN	/* outputUsesInterrupts */
  };
  static const rtems_termios_callbacks pollCallbacks = {
    NULL,		/* firstOpen */
    NULL,		/* lastClose */
    sccPollRead,	/* pollRead */
    sccPollWrite,	/* write */
    sccSetAttributes,	/* setAttributes */
    NULL,		/* stopRemoteTx */
    NULL,		/* startRemoteTx */
    0			/* outputUsesInterrupts */
  };

  if (m8xx_scc_mode[chan] == TERMIOS_IRQ_DRIVEN) {
    status = rtems_termios_open (major, minor, arg, &interruptCallbacks);
    sccttyp[chan] = args->iop->data1;
  }
  else {
    status = rtems_termios_open (major, minor, arg, &pollCallbacks);
    sccttyp[chan] = args->iop->data1;
  }
  return status;
}

/*
 * Close the device
 */
rtems_device_driver console_close(
				  rtems_device_major_number major,
				  rtems_device_minor_number minor,
				  void                    * arg
				  )
{
  rtems_status_code rc;

  rc = rtems_termios_close (arg);
  sccttyp[minor] = NULL;

  return rc;

}

/*
 * Read from the device
 */
rtems_device_driver console_read(
				 rtems_device_major_number major,
				 rtems_device_minor_number minor,
				 void                    * arg
				 )
{
  return rtems_termios_read (arg);
}

/*
 * Write to the device
 */
rtems_device_driver console_write(
				  rtems_device_major_number major,
				  rtems_device_minor_number minor,
				  void                    * arg
				  )
{
  return rtems_termios_write (arg);
}

#if 0
static int scc_io_set_trm_char(rtems_device_minor_number minor,
			       rtems_libio_ioctl_args_t *ioa)
{
  rtems_status_code rc                = RTEMS_SUCCESSFUL;
  con360_io_trm_char_t *trm_char_info = ioa->buffer;

  /*
   * check, that parameter is non-NULL
   */
  if ((rc == RTEMS_SUCCESSFUL) &&
      (trm_char_info == NULL)) {
    rc = RTEMS_INVALID_ADDRESS;
  }
  /*
   * transfer max_idl
   */
  if (rc == RTEMS_SUCCESSFUL) {
    if (trm_char_info->max_idl >= 0x10000) {
      rc = RTEMS_INVALID_NUMBER;
    }
    else if (trm_char_info->max_idl > 0) {
      CHN_PARAM_SET(minor,un.uart.max_idl ,trm_char_info->max_idl);
    }
    else if (trm_char_info->max_idl == 0) {
      CHN_PARAM_SET(minor,un.uart.max_idl ,MAX_IDL_DEFAULT);
    }
  }
  /*
   * transfer characters
   */
  if (rc == RTEMS_SUCCESSFUL) {
    if (trm_char_info->char_cnt > CON8XX_TRM_CHAR_CNT) {
      rc = RTEMS_TOO_MANY;
    }
    else if (trm_char_info->char_cnt >= 0) {
      /*
       * check, whether device is a SCC
       */
      if ((rc == RTEMS_SUCCESSFUL) &&
	  !m8xx_console_chan_desc[minor].is_scc) {
	rc = RTEMS_UNSATISFIED;
      }
      else {
	int idx = 0;
	for(idx = 0;idx < trm_char_info->char_cnt;idx++) {
	  m8xx_console_chan_desc[minor].parms.sccp->un.uart.character[idx] =
	    trm_char_info->character[idx] & 0x00ff;
	}
	if (trm_char_info->char_cnt < CON8XX_TRM_CHAR_CNT) {
	  m8xx_console_chan_desc[minor].parms.sccp
	    ->un.uart.character[trm_char_info->char_cnt] = 0x8000;
	}
      }
    }
  }

  return rc;
}
#endif

/*
 * Handle ioctl request.
 */
rtems_device_driver console_control(
				    rtems_device_major_number major,
				    rtems_device_minor_number minor,
				    void                    * arg
				    )
{
  rtems_libio_ioctl_args_t *ioa=arg;

  switch (ioa->command) {
#if 0
  case CON8XX_IO_SET_TRM_CHAR:
    return scc_io_set_trm_char(minor, ioa);
#endif
  default:
    return rtems_termios_ioctl (arg);
    break;
  }
}