summaryrefslogtreecommitdiff
path: root/kernel/drv/oss_sbxfi/sbxfi_hwaccess.c
blob: 4d4dad933c8552a1d26ba9f3f166d27a60220f26 (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
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
/**
*******************************************************************************
Confidential & Proprietary
Private & Confidential
Creative Confidential
*******************************************************************************
*/
/**
*******************************************************************************
Copyright (C) Creative Technology, Ltd., 2007. All rights reserved.
*******************************************************************************
**/
#include "oss_sbxfi_cfg.h"
#include <oss_pci.h>
#include "sbxfi.h"
#include "20k1reg.h"
#include "hwaccess.h"

static const int 
volume_table[MIXER_VOLSTEPS+1] =
{
	0x0000000, 0x000010a, 0x0000110, 0x0000116, 0x000011d, 
	0x0000124, 0x000012a, 0x0000131, 0x0000138, 0x0000140, 
	0x0000147, 0x000014f, 0x0000157, 0x000015f, 0x0000167, 
	0x000016f, 0x0000178, 0x0000180, 0x0000189, 0x0000193, 
	0x000019c, 0x00001a6, 0x00001af, 0x00001b9, 0x00001c4, 
	0x00001ce, 0x00001d9, 0x00001e4, 0x00001ef, 0x00001fb, 
	0x0000207, 0x0000213, 0x000021f, 0x000022c, 0x0000239, 
	0x0000246, 0x0000254, 0x0000262, 0x0000270, 0x000027e, 
	0x000028d, 0x000029c, 0x00002ac, 0x00002bc, 0x00002cc, 
	0x00002dd, 0x00002ee, 0x0000300, 0x0000311, 0x0000324, 
	0x0000336, 0x000034a, 0x000035d, 0x0000371, 0x0000386, 
	0x000039b, 0x00003b0, 0x00003c6, 0x00003dd, 0x00003f4, 
	0x000040c, 0x0000424, 0x000043c, 0x0000456, 0x0000470, 
	0x000048a, 0x00004a5, 0x00004c1, 0x00004dd, 0x00004fa, 
	0x0000518, 0x0000536, 0x0000555, 0x0000575, 0x0000596, 
	0x00005b7, 0x00005d9, 0x00005fc, 0x0000620, 0x0000644, 
	0x000066a, 0x0000690, 0x00006b7, 0x00006df, 0x0000708, 
	0x0000732, 0x000075d, 0x0000789, 0x00007b6, 0x00007e4, 
	0x0000813, 0x0000843, 0x0000874, 0x00008a7, 0x00008da, 
	0x000090f, 0x0000945, 0x000097c, 0x00009b5, 0x00009ef, 
	0x0000a2a, 0x0000a67, 0x0000aa5, 0x0000ae4, 0x0000b25, 
	0x0000b68, 0x0000bac, 0x0000bf1, 0x0000c38, 0x0000c81, 
	0x0000ccc, 0x0000d18, 0x0000d66, 0x0000db6, 0x0000e08, 
	0x0000e5c, 0x0000eb1, 0x0000f09, 0x0000f63, 0x0000fbe, 
	0x000101c, 0x000107c, 0x00010df, 0x0001143, 0x00011aa, 
	0x0001214, 0x000127f, 0x00012ee, 0x000135f, 0x00013d2, 
	0x0001448, 0x00014c1, 0x000153d, 0x00015bc, 0x000163d, 
	0x00016c2, 0x000174a, 0x00017d4, 0x0001863, 0x00018f4, 
	0x0001989, 0x0001a21, 0x0001abd, 0x0001b5c, 0x0001c00
};

unsigned char
DetectAndConfigureHardware (sbxfi_devc_t * devc)
{
  unsigned short wData;

  // Default setting for hendrix card is memory access, so must get IO access port from bar5.
  // bar0 will be converted to IO access in SwitchToXFiCore()
  if (devc->hw_family == HW_UAA)
    {
      // Base IO address is at register lcoation 0x24 (bar5)
      pci_read_config_word (devc->osdev, PCI_BASE_ADDRESS_5, &wData);
      devc->wIOPortBase = wData & 0xFFFC;
    }
  else
    {
      // Get the IO base address
      pci_read_config_word (devc->osdev, PCI_BASE_ADDRESS_0, &wData);
      devc->wIOPortBase = wData & 0xFFFC;
    }

  return TRUE;
}

unsigned char
IsVistaCompatibleHardware (sbxfi_devc_t * devc)
{
  // Check the subsystem id
  if (devc->hw_family == HW_UAA)
    {
      return TRUE;
    }

  return FALSE;
}

void
SwitchToXFiCore (sbxfi_devc_t * devc)
{
  unsigned int bar0, bar1, bar2, bar3, bar4, bar5, irq, clSize, lTimer;

  // program the hardware to X-Fi core.
  // Check whether its hendrix card
  // Save the previous memory/io address

  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_0, &bar0);
  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_1, &bar1);
  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_2, &bar2);
  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_3, &bar3);
  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_4, &bar4);
  pci_read_config_dword (devc->osdev, PCI_BASE_ADDRESS_5, &bar5);

  pci_read_config_dword (devc->osdev, PCI_INTERRUPT_LINE, &irq);
  pci_read_config_dword (devc->osdev, PCI_CFGHDR_CACHESIZE, &clSize);
  pci_read_config_dword (devc->osdev, PCI_CFGHDR_LATENCY, &lTimer);

  cmn_err (CE_CONT, "Switching to xfi core...\n");

  // Switch to XFi core config space with BAR0
  pci_write_config_dword (devc->osdev, 0xA0, 0x87654321);

  // copy Base I/O address from UAA core to X-Fi core
  pci_write_config_dword (devc->osdev, PCI_BASE_ADDRESS_5, bar5);

  // Switch to XFi core config space without BAR0
  pci_write_config_dword (devc->osdev, 0xA0, 0x12345678);

  // copy all other setting from UAA config space to X-Fi config space
  pci_write_config_dword (devc->osdev, PCI_BASE_ADDRESS_1, bar1);
  pci_write_config_dword (devc->osdev, PCI_BASE_ADDRESS_2, bar2);
  pci_write_config_dword (devc->osdev, PCI_BASE_ADDRESS_3, bar3);
  pci_write_config_dword (devc->osdev, PCI_BASE_ADDRESS_4, bar4);

  pci_write_config_dword (devc->osdev, PCI_INTERRUPT_LINE, irq);
  pci_write_config_dword (devc->osdev, PCI_CFGHDR_CACHESIZE, clSize);
  pci_write_config_dword (devc->osdev, PCI_CFGHDR_LATENCY, lTimer);

  pci_write_config_dword (devc->osdev, PCI_CFGHDR_CMDREG, 0x07);

  /*
     NOTE: 
     The steps below is needed to switch the control signals to X-Fi core.

     It needs to access the mode change register which reside in the UAA core BAR0 + 0x00003ffc.
     Since this demo sample is a real-mode DOS program, it will need other services such as XMS to access 
     memory above 1MB.  

     Here is the pseudo code:

     WriteMemory((bar0 + 0x00003ffc),0x43544c58);  // CTLX
     WriteMemory((bar0 + 0x00003ffc),0x43544c2d);  // CTL-
     WriteMemory((bar0 + 0x00003ffc),0x43544c46);  // CTLF
     WriteMemory((bar0 + 0x00003ffc),0x43544c69);  // CTLi  
   */
}


CTSTATUS
InitHardware (sbxfi_devc_t * devc)
{
  unsigned int gctlorg;
  unsigned int dwIterCount, dwData;


  // kick in auto-init
  gctlorg = HwRead20K1 (devc, GCTL);
  HwWrite20K1 (devc, GCTL, (~0x2 & gctlorg));
  HwWrite20K1 (devc, GCTL, (0x2 | gctlorg));
  osDelayms (1000);
  // poll for AID in GCTL to be set
  dwIterCount = 0x400000;
  do
    {
      dwData = HwRead20K1 (devc, GCTL);
    }
  while (!(dwData & 0x00100000) && --dwIterCount);

  // AID bit is not set when time out, return failure.
  if (!(dwData & 0x00100000))
    return CTSTATUS_ERROR;

  gctlorg = HwRead20K1 (devc, GCTL);
  HwWrite20K1 (devc, GCTL, (0x100aa3 | gctlorg));
  osDelayms (10000);

  HwWrite20K1 (devc, GIE, 0);
  HwWrite20K1 (devc, SRCIP(0), 0);
  osDelayms (30000);

  if (((HwRead20K1 (devc, PLLCTL)) != 0x1480a001)
      && ((HwRead20K1 (devc, PLLCTL)) != 0x1480a731))
    {
      HwWrite20K1 (devc, PLLCTL, 0x1480a001);
    }
  osDelayms (40000);
  dwData = HwRead20K1 (devc, PLLCTL);

  // configure GPIO per the card's family.
  switch (devc->hw_family)
    {
    case HW_055x:
      HwWrite20K1 (devc, GPIOCTL, 0x13fe);
      break;
    case HW_073x:
      HwWrite20K1 (devc, GPIOCTL, 0x00e6);
      break;
    case HW_UAA:
      HwWrite20K1 (devc, GPIOCTL, 0x00c2);
      break;
    case HW_ORIG:
    default:
      HwWrite20K1 (devc, GPIOCTL, 0x01e6);
      break;
    }

  return CTSTATUS_SUCCESS;
}

CTSTATUS
AllocateBuffers (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  int ctStatus = CTSTATUS_SUCCESS;

#if 0
  if (devc->pdwPageTable == NULL)
    ctStatus = CTSTATUS_NOMEMORY;
  else
    {
      // alloc playL buffer
      portc->pdwPlayLBuffer = CONTIG_MALLOC (devc->osdev,
					     portc->dwPlayLBufferSize,
					     MEMLIMIT_32BITS,
					     &portc->dwPlayLPhysAddx, portc->playl_dma_handle);

      if (portc->pdwPlayLBuffer == NULL)
	ctStatus = CTSTATUS_NOMEMORY;
      else
	{
	  // alloc playR buffer
	  portc->pdwPlayRBuffer = CONTIG_MALLOC (devc->osdev,
						 portc->dwPlayRBufferSize,
						 MEMLIMIT_32BITS,
						 &portc->dwPlayLPhysAddx,portc->playr_dma_handle);

	  if (portc->pdwPlayRBuffer == NULL)
	    ctStatus = CTSTATUS_NOMEMORY;
	  else
	    {
	      // alloc recordL buffer
	      portc->pdwRecordLBuffer = CONTIG_MALLOC (devc->osdev,
						       portc->
						       dwRecordLBufferSize,
						       MEMLIMIT_32BITS,
						       &portc->
						       dwRecordLPhysAddx, portc->recl_dma_handle);

	      if (portc->pdwRecordLBuffer == NULL)
		ctStatus = CTSTATUS_NOMEMORY;
	      else
		{
		  // alloc recordR buffer
		  portc->pdwRecordRBuffer = CONTIG_MALLOC (devc->osdev,
							   portc->
							   dwRecordRBufferSize,
							   MEMLIMIT_32BITS,
							   &portc->
							   dwRecordRPhysAddx, portc->recr_dma_handle);
		  if (portc->pdwRecordRBuffer == NULL)
		    ctStatus = CTSTATUS_NOMEMORY;
		}
	    }
	}
    }

  if (ctStatus != CTSTATUS_SUCCESS)
    FreeBuffers (devc, portc);
#endif

  return ctStatus;
}

void
FreeBuffers (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
#if 0
  if (portc->pdwRecordLBuffer != NULL)
    {
      CONTIG_FREE (devc->osdev, portc->pdwRecordLBuffer,
		   portc->dwRecordLBufferSize, portc->recl_dma_handle);
      portc->pdwRecordLBuffer = NULL;
    }

  if (portc->pdwRecordRBuffer != NULL)
    {
      CONTIG_FREE (devc->osdev, portc->pdwRecordRBuffer,
		   portc->dwRecordRBufferSize, portc->recr_dma_handle);
      portc->pdwRecordRBuffer = NULL;
    }

  if (portc->pdwPlayLBuffer != NULL)
    {
      CONTIG_FREE (devc->osdev, portc->pdwPlayLBuffer,
		   portc->dwPlayLBufferSize, portc->playl_dma_handle);
      portc->pdwPlayLBuffer = NULL;
    }

  if (portc->pdwPlayRBuffer != NULL)
    {
      CONTIG_FREE (devc->osdev, portc->pdwPlayRBuffer,
		   portc->dwPlayRBufferSize, portc->playr_dma_handle);
      portc->pdwPlayRBuffer = NULL;
    }
#endif
}

void
_SetupSB055xADC (sbxfi_devc_t * devc, unsigned int src, unsigned char mic20db)
{
  unsigned short gpioorg;
  unsigned short gpioval = 0x28;


  // check and set the following GPIO bits accordingly
  //   ADC_Gain       = GPIO2
  //   Mic_Pwr_on     = GPIO7 
  //   Digital_IO_Sel = GPIO8      
  //   Mic_Sw         = GPIO9
  //   Aux/MicLine_Sw = GPIO12
  switch (src)
    {
    case ADC_SRC_MICIN:
      gpioval = 0x28;
      if (mic20db)
	gpioval |= 4;
      break;

    case ADC_SRC_LINEIN:
      gpioval = 0;
      break;

    case ADC_SRC_VIDEO:
      gpioval = 0x100;		// not supported, set to digital
      break;

    case ADC_SRC_AUX:
      gpioval = 0x1000;
      break;

    case ADC_SRC_NONE:
      gpioval = 0x100;		// set to digital
      break;

    default:
      break;
    }

  gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
  gpioorg &= 0xec7b;
  gpioorg |= gpioval;
  HwWrite20K1 (devc, GPIO, gpioorg);

  return;
}

void
_SetupADC (sbxfi_devc_t * devc, unsigned int src, unsigned char mic20db)
{
  unsigned int i = 0;
  unsigned short gpioorg;
  unsigned short input_source;
  unsigned int adcdata = 0;

  input_source = 0x100;		// default to analog
  switch (src)
    {
    case ADC_SRC_MICIN:
      adcdata = 0x1;
      input_source = 0x180;	// set GPIO7 to select Mic
      break;

    case ADC_SRC_LINEIN:
      adcdata = 0x2;
      break;

    case ADC_SRC_VIDEO:
      adcdata = 0x4;
      break;

    case ADC_SRC_AUX:
      adcdata = 0x8;
      break;

    case ADC_SRC_NONE:
      adcdata = 0x0;
      input_source = 0x0;	// set to Digital
      break;

    default:
      break;
    }


  HwWrite20K1PCI (devc, 0xcc, 0x8c);
  HwWrite20K1PCI (devc, 0xcc, 0x0e);
  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    {
      HwWrite20K1PCI (devc, 0xcc, 0xee);
      HwWrite20K1PCI (devc, 0xcc, 0xaa);
    }

  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    return;

  HwWrite20K1PCI (devc, 0xEC, 0x05);	//write to i2c status control

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);
  HwWrite20K1PCI (devc, 0xE4, 0x080e);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);
  HwWrite20K1PCI (devc, 0xE4, 0x0a18);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);

  if (mic20db)
    HwWrite20K1PCI (devc, 0xE4, 0xf71c);
  else
    HwWrite20K1PCI (devc, 0xE4, 0xcf1c);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);

  if (mic20db)
    HwWrite20K1PCI (devc, 0xE4, 0xf71e);
  else
    HwWrite20K1PCI (devc, 0xE4, 0xcf1e);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);
  HwWrite20K1PCI (devc, 0xE4, 0x8628);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x001a0080);
  HwWrite20K1PCI (devc, 0xE4, 0x2a | (adcdata << 0x8));

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }				//i2c ready poll

  gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
  gpioorg &= 0xfe7f;
  gpioorg |= input_source;
  HwWrite20K1 (devc, GPIO, gpioorg);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  if (!((HwRead20K1 (devc, ID0)) & 0x100))
    {
      HwWrite20K1PCI (devc, 0xE0, 0x001a0080);
      HwWrite20K1PCI (devc, 0xE4, 0x2616);
    }

  return;
}

void
InitADC (sbxfi_devc_t * devc, unsigned int src, unsigned char mic20db)
{
  unsigned short wSSID;

  wSSID = devc->wSubsystemID;
  if ((wSSID == 0x0022) || (wSSID == 0x002F))
    {
      // Sb055x card
      _SetupSB055xADC (devc, src, mic20db);
    }
  else
    {
      _SetupADC (devc, src, mic20db);
    }

  return;
}

void
ResetDAC (sbxfi_devc_t * devc)
{
  unsigned int i = 0;
  unsigned short gpioorg;


  HwWrite20K1PCI (devc, 0xcc, 0x8c);
  HwWrite20K1PCI (devc, 0xcc, 0x0e);
  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    {
      HwWrite20K1PCI (devc, 0xcc, 0xee);
      HwWrite20K1PCI (devc, 0xcc, 0xaa);
    }
  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    return;

  HwWrite20K1PCI (devc, 0xEC, 0x05);	//write to i2c status control
  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  // To be effective, need to reset the DAC twice.
  for (i = 0; i < 2; i++)
    {
      osDelayms (100000);
      gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
      gpioorg &= 0xfffd;
      HwWrite20K1 (devc, GPIO, gpioorg);
      osDelayms (1000);
      HwWrite20K1 (devc, GPIO, gpioorg | 0x2);
    }				//set gpio

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, 0x8001);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, 0x1002);

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }
}

void
InitDAC (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int i = 0;
  unsigned int wData;
  unsigned short gpioorg;
  unsigned int dwSamplingRate;
  unsigned short wSSID;


  wSSID = devc->wSubsystemID;
  // if SB055x, unmute outputs
  if ((wSSID == 0x0022) || (wSSID == 0x002F))
    {
      gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
      gpioorg &= 0xffbf;	// set GPIO6 to low
      gpioorg |= 2;		// set GPIO1 to high
      HwWrite20K1 (devc, GPIO, gpioorg);

      return;
    }


  dwSamplingRate = portc->rate;

  // Mute outputs
  gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
  gpioorg &= 0xffbf;
  HwWrite20K1 (devc, GPIO, gpioorg);

  ResetDAC (devc);

  HwWrite20K1PCI (devc, 0xcc, 0x8c);
  HwWrite20K1PCI (devc, 0xcc, 0x0e);
  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    {
      HwWrite20K1PCI (devc, 0xcc, 0xee);
      HwWrite20K1PCI (devc, 0xcc, 0xaa);
    }
  if (((HwRead20K1PCI (devc, 0xcc)) & 0xff) != 0xaa)
    return;

  HwWrite20K1PCI (devc, 0xEC, 0x05);	//write to i2c status control

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  if (dwSamplingRate == 48000)
    wData = 0x2400;
  else if (dwSamplingRate == 96000)
    wData = 0x2500;
  else if (dwSamplingRate == 192000)
    wData = 0x2600;
  else
    wData = 0x2400;

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, (wData | 0x6));

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, (wData | 0x9));

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, (wData | 0xc));

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  HwWrite20K1PCI (devc, 0xE0, 0x00180080);
  HwWrite20K1PCI (devc, 0xE4, (wData | 0xf));

  i = 0;
  while (i != 0x800000)
    {
      i = ((HwRead20K1PCI (devc, 0xEC)) & 0x800000);
    }

  // unmute outputs
  gpioorg = (unsigned short) HwRead20K1 (devc, GPIO);
  gpioorg = gpioorg | 0x40;
  HwWrite20K1 (devc, GPIO, gpioorg);
}


void
SetupPlayInputMapper (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
/*
 * TODO: This routine supports only stereo
 */
  unsigned int i;
  unsigned int srcch;
  unsigned int dio1, dio2;
  unsigned int dwSamplingRate;


  srcch = portc->SrcChan;
  dio1 = portc->dwDAChan[0];
  dio2 = portc->dwDAChan[1];
  dwSamplingRate = portc->rate;

  // initialize input mappers
  for (i = 0; i < 0x50; i++)
    HwWrite20K1 (devc, DAOIMAP_START(i), 0);

  if (dwSamplingRate == 48000)
    {
      if (dio1 == 0)
	{
	  HwWrite20K1 (devc, DAOIMAP_START(dio1), 0);
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2),
		       (dio1 << 16) | GetAudioSrcChan (srcch+i));
	  HwWrite20K1 (devc, DAOIMAP_START(dio1),
		       (dio2 << 16) | GetAudioSrcChan (srcch));
	}
      else
	{
	  HwWrite20K1 (devc, DAOIMAP_START(0), 0);
	  HwWrite20K1 (devc, DAOIMAP_START(dio1),
		       (dio2 << 16) | GetAudioSrcChan (srcch));
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2),
		       (0 << 16) | GetAudioSrcChan (srcch+i));
	  HwWrite20K1 (devc, DAOIMAP_START(0), (dio1 << 16) | 0);
	}
    }
  else if (dwSamplingRate == 96000)
    {
      // input mapper.  Input mapper is a circular linked-list
      if (dio1 == 0)
	{
	  HwWrite20K1 (devc, DAOIMAP_START(dio1), 0);
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2),
		       ((dio1 + 2) << 16) | GetAudioSrcChan (srcch+i));
	  HwWrite20K1 (devc, DAOIMAP_START(dio1 + 2),
		       ((dio2 + 2) << 16) | GetAudioSrcChan (srcch + 0x80));
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2 + 2),
		       (dio1 << 16) | GetAudioSrcChan (srcch+i + 0x80));
	  HwWrite20K1 (devc, DAOIMAP_START(dio1),
		       (dio2 << 16) | GetAudioSrcChan (srcch));
	}
      else
	{
	  HwWrite20K1 (devc, DAOIMAP_START(0), 0);
	  HwWrite20K1 (devc, DAOIMAP_START(dio1),
		       (dio2 << 16) | GetAudioSrcChan (srcch));
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2),
		       ((dio1 + 2) << 16) | GetAudioSrcChan (srcch+i));
	  HwWrite20K1 (devc, DAOIMAP_START(dio1 + 2),
		       ((dio2 + 2) << 16) | GetAudioSrcChan (srcch + 0x80));
	  for (i=1;i<portc->channels;i++)
	  HwWrite20K1 (devc, DAOIMAP_START(dio2 + 2),
		       (0 << 16) | GetAudioSrcChan (srcch+i + 0x80));
	  HwWrite20K1 (devc, DAOIMAP_START(0), (dio1 << 16) | 0);
	}
    }
}

void
SetupPlayFormat (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int i2sorg;
  unsigned int dio1;
  unsigned int dwSamplingRate;


  dio1 = portc->dwDAChan[0];
  dwSamplingRate = portc->rate;

  // Read I2S CTL.  Keep original value.
  i2sorg = HwRead20K1 (devc, I2SCTL);

#if 1
  i2sorg = i2sorg | 0x04040404; // All I2S outputs enabled
#else
  // setup I2S value to program
  switch (dio1)
    {
    case I2SA_L:
      i2sorg = i2sorg | 0x4;
      break;
    case I2SB_L:
      i2sorg = i2sorg | 0x400;
      break;
    case I2SC_L:
      i2sorg = i2sorg | 0x40000;
      break;
    case I2SD_L:
      i2sorg = i2sorg | 0x4000000;
      break;
    default:
      i2sorg = i2sorg | 0x4;
      break;
    }
#endif

  // Program I2S with proper sample rate and enable the correct I2S channel.
  i2sorg &= 0xfffffffc;
  if (dwSamplingRate == 96000)
    {
      i2sorg = i2sorg | 2;
      HwWrite20K1 (devc, I2SCTL, i2sorg);
    }
  else
    {
      i2sorg = i2sorg | 1;
      HwWrite20K1 (devc, I2SCTL, i2sorg);
    }
}

void
SetupPlayMixer (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  int i;
  unsigned int fixed_pitch;
  unsigned int srcArchn, srcArchnC;
  unsigned int srcPrchn, srcPrchnC;
  unsigned int srcArchn2, srcArchnC2;
  unsigned int srcch;
  unsigned int dwSamplingRate;
  unsigned int dwYData;

  srcch = portc->SrcChan;
  dwSamplingRate = portc->rate;

  // NOTE: Y-Data is a 14-bit immediate floating-point constant multiplier.
  // Adjust the Y-Data to control the multiplier.
  // This can be used to control the level of the signal.
  // dwYData = 0x1c00; // Original level used by Creative's driver.
  dwYData = volume_table[portc->vol_left];

  srcArchn = GetAudioSrcChan (srcch);
  srcArchnC = GetAudioSrcChan (srcch + 0x80);	// conjugate channel for srcch
  srcPrchn = GetParamPitchChan (srcch);
  srcPrchnC = GetParamPitchChan (srcch + 0x80);

  // since input is same as output, pitch is 1.0
  // convert to fixed-point 8.24 format, shift left 24 bit.
  fixed_pitch = 1;
  fixed_pitch = fixed_pitch << 24;

  // write the pitch to param ring of the corresponsing SRC pitch slot
  HwWrite20K1 (devc, PRING_LO_HI_START(srcPrchn), fixed_pitch);
  HwWrite20K1 (devc, PRING_LO_HI_START(srcPrchnC), fixed_pitch);

  WriteAMOP (devc, srcArchn, dwYData, srcArchn, 0);
  if (dwSamplingRate == 96000)
    {
      WriteAMOP (devc, srcArchnC, dwYData, srcArchnC, 0);
    }

  // Handle subsequent channels

  for (i=1;i<portc->channels;i++)
  {
  	  dwYData = volume_table[(i&1) ? portc->vol_right : portc->vol_left];

	  // Since we will use 1st SRC ch as pitch master, 
	  // we do not need to program the pitch for SRC ch2
	
	  srcArchn2 = GetAudioSrcChan (srcch+i);
	  srcArchnC2 = GetAudioSrcChan (srcch+i + 0x80);	// conjugate channel for srcch+i
	
	  WriteAMOP (devc, srcArchn2, dwYData, srcArchn2, 0);
	  if (dwSamplingRate == 96000)
	    {
	      WriteAMOP (devc, srcArchnC2, dwYData, srcArchnC2, 0);
	    }
  }
}

void
SetupAndStartPlaySRC (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int Sa, Ladr, Ca, Ctl = 0x44c;
  unsigned int srcch;
  unsigned int dwSamplingRate;
  int count;
  int i;

  srcch = portc->SrcChan;
  dwSamplingRate = portc->rate;

  count = audio_engines[portc->dev]->dmap_out->bytes_in_use;

  //  start addx: 1st entry in page table.
  //  Note: this must match with pagetable entry 
  Sa = portc->pgtable_index * 4096;
  Ladr = Sa + count;
  Ca = Sa + 0x100;
  if (dwSamplingRate == 48000)
    Ctl = 0x44c;		// Set the Pitch Master for stereo.
  else if ((dwSamplingRate == 96000))
    Ctl = 0x45c;		// Set the Pitch Master for stereo.

  Ctl |= (portc->channels-1)*SRCCTL_ILSZ; /* Number of interleaved channels to follow */

  // Program SRC for channel 1, enable interrupts and interleaved channels
  WriteSRC (devc, Ca, 0, Sa, Ladr, 0x100, Ctl, srcch);

  Ladr = Sa + count;
  Ca = Sa + 0x100;

  for (i=1;i<portc->channels;i++)
  {
	  if (dwSamplingRate == 48000)
	    Ctl = 0x4c;			// slave
	  else if ((dwSamplingRate == 96000))
	    Ctl = 0x5c;			// slave
  	  Ctl |= (portc->channels-i-1)*SRCCTL_ILSZ;
	
	  // Program SRC for channel 2
	  WriteSRC (devc, Ca, 0, Sa, Ladr, 0x100, Ctl, srcch+i);
  }

  //_dumpRegisters (devc, portc);
}

void
StopPlay (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int srcch;
  unsigned int dwData;
  int i;

  srcch = portc->SrcChan;

  //WriteSRC(devc, 0, 0, 0, 0, 0, 0, srcch);
  //WriteSRC(devc, 0, 0, 0, 0, 0, 0, srcch2);

  dwData = HwRead20K1 (devc, SRCCTL(srcch));
  dwData &= 0xfffffff0;
  dwData |= 0;
  dwData &= ~SRCCTL_IE; /* Interrupt disable */
  HwWrite20K1 (devc, SRCCTL(srcch), dwData);

  for (i=1;i<portc->channels;i++)
  {
	  dwData = HwRead20K1 (devc, SRCCTL(srcch+i));
	  dwData &= 0xfffffff0;
	  dwData |= 0;
	  HwWrite20K1 (devc, SRCCTL(srcch+i), dwData);
  }
}


void
StopPlaySRC (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
#ifndef INTERNAL_LOOPBACK
  StopPlay (devc, portc);
#endif
}


//======================== RECORD ==========================



void
SetupRecordInputMapper (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int srcch, srcch2;


  srcch = portc->SrcChan;
  srcch2 = portc->SrcChan+1;

  // Internal loopback means loop play channels to record
#ifdef INTERNAL_LOOPBACK
  {
    unsigned int playch1, playch2;

    playch1 = portc->dwPlayLSrcChan;
    playch2 = portc->dwPlayRSrcChan;
    if (srcch == 0)
      {
	HwWrite20K1 (devc, SRCIMAP(0), 0);
	HwWrite20K1 (devc, SRCIMAP(srcch2),
		     srcch2 << 24 | (0x80 +
				     srcch) << 16 |
		     GetAudioSrcChan (playch2));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch),
		     (0x80 + srcch) << 24 | (srcch2 +
					      0x80) << 16 |
		     GetAudioSrcChan (playch1 + 0x80));
	HwWrite20K1 (devc, SRCIMAP(0x81 + srcch2),
		     (0x80 +
		      srcch2) << 24 | 0 << 16 | GetAudioSrcChan (playch2 +
								 0x80));
	HwWrite20K1 (devc, SRCIMAP(srcch),
		     srcch << 24 | srcch2 << 16 | GetAudioSrcChan (playch1));
      }
    else
      {
	HwWrite20K1 (devc, SRCIMAP(0), 0);
	HwWrite20K1 (devc, SRCIMAP(srcch),
		     srcch << 24 | srcch2 << 16 | GetAudioSrcChan (playch1));
	HwWrite20K1 (devc, SRCIMAP(srcch2),
		     srcch2 << 24 | (0x80 +
				     srcch) << 16 |
		     GetAudioSrcChan (playch2));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch),
		     (0x80 + srcch) << 24 | (srcch2 +
					      0x80) << 16 |
		     GetAudioSrcChan (playch1 + 0x80));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch2),
		     (0x80 +
		      srcch2) << 24 | 0 << 16 | GetAudioSrcChan (playch2 +
								 0x80));
	HwWrite20K1 (devc, SRCIMAP(0),
		     (0 << 24) | (srcch << 16) | 0x0);
      }
  }
#else
  {
    if (srcch == 0)
      {
	HwWrite20K1 (devc, SRCIMAP(0), 0);
	HwWrite20K1 (devc, SRCIMAP(srcch2),
		     srcch2 << 24 | (0x80 +
				     srcch) << 16 |
		     GetAudioSumChan (srcch2));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch),
		     (0x80 + srcch) << 24 | (srcch2 +
					      0x80) << 16 |
		     GetAudioSumChan (srcch + 0x80));
	HwWrite20K1 (devc, SRCIMAP(0x81 + srcch2),
		     (0x80 +
		      srcch2) << 24 | 0 << 16 | GetAudioSumChan (srcch2 +
								 0x80));
	HwWrite20K1 (devc, SRCIMAP(srcch),
		     srcch << 24 | srcch2 << 16 | GetAudioSumChan (srcch));
      }
    else
      {
	HwWrite20K1 (devc, SRCIMAP(0), 0);
	HwWrite20K1 (devc, SRCIMAP(srcch),
		     srcch << 24 | srcch2 << 16 | GetAudioSumChan (srcch));
	HwWrite20K1 (devc, SRCIMAP(srcch2),
		     srcch2 << 24 | (0x80 +
				     srcch) << 16 |
		     GetAudioSumChan (srcch2));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch),
		     (0x80 + srcch) << 24 | (srcch2 +
					      0x80) << 16 |
		     GetAudioSumChan (srcch + 0x80));
	HwWrite20K1 (devc, SRCIMAP(0x80 + srcch2),
		     (0x80 +
		      srcch2) << 24 | 0 << 16 | GetAudioSumChan (srcch2 +
								 0x80));
	HwWrite20K1 (devc, SRCIMAP(0),
		     (0 << 24) | (srcch << 16) | 0x0);
      }
  }
#endif
}


void
_SetupInputToOutputMonitoring (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int i;
  unsigned int dio1, dio2;
  unsigned int srcch, srcch2;


  srcch = portc->SrcChan;
  srcch2 = portc->SrcChan+1;

  dio1 = portc->dwDAChan[0];
  dio2 = portc->dwDAChan[1];

  // initialize input mappers
  for (i = 0; i < 0x50; i++)
    HwWrite20K1 (devc, DAOIMAP_START(i), 0);

  HwWrite20K1 (devc, DAOIMAP_START(dio1), 0);
  HwWrite20K1 (devc, DAOIMAP_START(dio2),
	       ((dio1 + 2) << 16) | GetAudioSumChan (srcch2));
  HwWrite20K1 (devc, DAOIMAP_START(dio1 + 2),
	       ((dio2 + 2) << 16) | GetAudioSumChan (srcch + 0x80));
  HwWrite20K1 (devc, DAOIMAP_START(dio2 + 2),
	       (dio1 << 16) | GetAudioSumChan (srcch2 + 0x80));
  HwWrite20K1 (devc, DAOIMAP_START(dio1),
	       (dio2 << 16) | GetAudioSumChan (srcch));
}

void
SetupRecordMixer (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int fixed_pitch;
  unsigned int srcPrchn1, srcPrchnC1;
  unsigned int srcch, srcch2, srcchnC1, srcchnC2;
  unsigned int dwYData;
  unsigned short i, inch1, inch2;


  srcch = portc->SrcChan;
  srcch2 = portc->SrcChan+1;

  // NOTE: Y-Data is a 14-bit immediate floating-point constant multiplier.
  // Adjust the Y-Data to control the multiplier.
  // This can be used to control the level of the signal.
  dwYData = 0x1c00;

  srcchnC1 = srcch + 0x80;
  srcchnC2 = srcch2 + 0x80;

  srcPrchn1 = GetParamPitchChan (srcch);
  srcPrchnC1 = GetParamPitchChan (srcch + 0x80);

  // since input is 2x of output, pitch is 2.0
  // convert to fixed-point 8.24 format, shift left 24 bit.
  fixed_pitch = 2;
  fixed_pitch = fixed_pitch << 24;

  // write the pitch to param ring of the corresponsing SRC pitch slot
  HwWrite20K1 (devc, PRING_LO_HI_START(srcPrchn1), fixed_pitch);
  HwWrite20K1 (devc, PRING_LO_HI_START(srcPrchnC1), fixed_pitch);

  inch1 = 0x1b5;		// I2S-In3 L
  inch2 = 0x1bd;		// I2S-In3 R
  // program all I2S-In3 slots
  for (i = 0; i < 8; i++)
    {
      if (i <= 3)
	{
	  WriteAMOP (devc, inch1 + (i * 0x200), dwYData, inch1 + (i * 0x200),
		     (0x80000000 + srcch));
	  WriteAMOP (devc, inch2 + (i * 0x200), dwYData, inch2 + (i * 0x200),
		     (0x80000000 + srcch2));
	}
      else
	{
	  WriteAMOP (devc, inch1 + (i * 0x200), dwYData, inch1 + (i * 0x200),
		     (0x80000000 + srcchnC1));
	  WriteAMOP (devc, inch2 + (i * 0x200), dwYData, inch2 + (i * 0x200),
		     (0x80000000 + srcchnC2));
	}
    }

  // enable physical input I2S_in3 to I2S-Out0 monitoring
  _SetupInputToOutputMonitoring (devc, portc);
}

void
SetupRecordFormat (sbxfi_devc_t * devc)
{
  unsigned int i2sorg;

  i2sorg = HwRead20K1 (devc, I2SCTL);

  // enable I2S-D input
  i2sorg |= 0x90000000;
  HwWrite20K1 (devc, I2SCTL, i2sorg);
}

void
SetupAndStartRecordSRC (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int Sa, Ladr, Ca, Ctl = 0x64d;
  int count;
  unsigned int srcch, srcch2;
  unsigned int dwSamplingRate;


  srcch = portc->SrcChan;
  srcch2 = portc->SrcChan+1;
  dwSamplingRate = portc->rate;

  count = audio_engines[portc->dev]->dmap_in->bytes_in_use;

  // convert the num samples to bytes count

  // hardcoded values:  
  //  start addx: 4th entry in page table.
  Sa = portc->pgtable_index * 4096;
  Ladr = Sa + count;
  Ca = Sa + 0x80;
  if (dwSamplingRate == 48000)
    Ctl = 0x64d;		// record must start with RUN state!.
  else if ((dwSamplingRate == 96000))
    Ctl = 0x65d;

  Ctl |= SRCCTL_ILSZ;	// Interleaved stereo

  WriteSRC (devc, Ca, 0, Sa, Ladr, 0x100, Ctl, srcch);

  Ladr = Sa + count;
  Ca = Sa + 0x80;
  if (dwSamplingRate == 48000)
    Ctl = 0x24d;
  else if ((dwSamplingRate == 96000))
    Ctl = 0x25d;

  WriteSRC (devc, Ca, 0, Sa, Ladr, 0x80, Ctl, srcch2);

  // Enable SRC input from Audio Ring
  HwWrite20K1 (devc, SRCMCTL, 0x1);

//    _dumpRegisters(devc);
}

void
StopRecordSRC (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int srcch, srcch2;
  unsigned int dwData;
  unsigned int i;

  srcch = portc->SrcChan;
  srcch2 = portc->SrcChan+1;

  //WriteSRC(devc, 0, 0, 0, 0, 0, 0, srcch);
  //WriteSRC(devc, 0, 0, 0, 0, 0, 0, srcch2);

  dwData = HwRead20K1 (devc, SRCCTL(srcch));
  dwData &= 0xfffffff0;
  dwData |= 0;
  HwWrite20K1 (devc, SRCCTL(srcch), dwData);

  dwData = HwRead20K1 (devc, SRCCTL(srcch2));
  dwData &= 0xfffffff0;
  dwData |= 0;
  HwWrite20K1 (devc, SRCCTL(srcch2), dwData);

#ifdef INTERNAL_LOOPBACK
  StopPlay (devc, portc);
#endif

  // Disable SRC inputs from Audio Ring
  HwWrite20K1 (devc, SRCMCTL, 0x0);

  for (i = 0; i < 0x50; i++)
    HwWrite20K1 (devc, DAOIMAP_START(i), 0);
}

//========================

unsigned int
HwRead20K1PCI (sbxfi_devc_t * devc, unsigned int dwReg)
{
  unsigned int dwVal;
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  osOutportd (devc, (IOADDR) devc->wIOPortBase + 0x10, dwReg);
  dwVal = osInportd (devc, (IOADDR) (devc->wIOPortBase + 0x14));
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);

  return dwVal;
}

unsigned int
HwRead20K1 (sbxfi_devc_t * devc, unsigned int dwReg)
{
  unsigned int dwVal;

  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  osOutportd (devc, (IOADDR) devc->wIOPortBase + 0x0, dwReg);
  dwVal = osInportd (devc, (IOADDR) (devc->wIOPortBase + 0x4));
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);

  return dwVal;
}

void
HwWrite20K1PCI (sbxfi_devc_t * devc, unsigned int dwReg, unsigned int dwData)
{
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  osOutportd (devc, (IOADDR) devc->wIOPortBase + 0x10, dwReg);
  osOutportd (devc, (IOADDR) (devc->wIOPortBase + 0x14), dwData);
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);
}

void
HwWrite20K1 (sbxfi_devc_t * devc, unsigned int dwReg, unsigned int dwData)
{
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  osOutportd (devc, (IOADDR) devc->wIOPortBase + 0x0, dwReg);
  osOutportd (devc, (IOADDR) (devc->wIOPortBase + 0x4), dwData);
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);
}

void
WriteSRC
  (sbxfi_devc_t * devc,
   unsigned int srcca,
   unsigned int srccf,
   unsigned int srcsa, unsigned int srcla, unsigned int srcccr, unsigned int srcctl, unsigned int chn)
{
  HwWrite20K1 (devc, SRCCA(chn), srcca);	// Current Address
  HwWrite20K1 (devc, SRCCF(chn), srccf);	// Current Fraction
  HwWrite20K1 (devc, SRCSA(chn), srcsa);	// START address
  HwWrite20K1 (devc, SRCLA(chn), srcla);	// LOOP address
  HwWrite20K1 (devc, SRCCCR(chn), srcccr);	// Cache control
  HwWrite20K1 (devc, SRCCTL(chn), srcctl);	// SRCCTL
}

#define CRM_TIMESLOT_ALLOC_BLOCK_SIZE   16
#define CRM_PTS_PITCH                   6
#define CRM_PARAM_SRC_OFFSET            0x60

unsigned int
GetParamPitchChan (unsigned int i)
{
  int interpChanID =
    (((int) i * CRM_TIMESLOT_ALLOC_BLOCK_SIZE) + CRM_PTS_PITCH) -
    CRM_PARAM_SRC_OFFSET;
  if (interpChanID < 0)
    {
      interpChanID += 4096;
    }
  return (unsigned int) interpChanID;
}

unsigned int
GetAudioSrcChan (unsigned int srcchn)
{
  //  SRC channel is in Audio Ring slot 1, after every 16 slot.
  return (unsigned int) ((srcchn << 4) + 0x1);
}

unsigned int
GetAudioSumChan (unsigned int chn)
{
  //  SUM channel is in Audio Ring slot 0xc, after every 16 slot.
  return (unsigned int) ((chn << 4) + 0xc);
}

void
WriteAMOP
  (sbxfi_devc_t * devc,
   unsigned int xdata, unsigned int ydata, unsigned int chn, unsigned int hidata)
{
  HwWrite20K1 (devc, AMOP_START(chn), ((((unsigned int) ydata) << 18) | xdata << 4 | 1));	// Audio mixer, y-immediate
  HwWrite20K1 (devc, AMOP_START(chn) + 4, hidata);	//  Audio mixer.
}

void
_dumpRegisters (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  _dumpGlobal (devc);
  _dumpSRCs (devc, portc);
}

void
_dumpSRCs (sbxfi_devc_t * devc, sbxfi_portc_t * portc)
{
  unsigned int chn;

  chn = portc->SrcChan;
  cmn_err (CE_CONT,
	   "SRC chn=%lx, CA=%lx, CF=%lx, SA=%lx, LA=%lx, CCR=%lx, CTL=%lx\n",
	   chn, HwRead20K1 (devc, SRCCA(chn)),
	   HwRead20K1 (devc, SRCCF(chn)), HwRead20K1 (devc,
									 SRCSA(chn)),
	   HwRead20K1 (devc, SRCLA(chn)), HwRead20K1 (devc,
									 SRCCCR(chn)),
	   HwRead20K1 (devc, SRCCTL(chn)));

  chn = portc->SrcChan+1;
  cmn_err (CE_CONT,
	   "SRC chn=%lx, CA=%lx, CF=%lx, SA=%lx, LA=%lx, CCR=%lx, CTL=%lx\n",
	   chn, HwRead20K1 (devc, SRCCA(chn)),
	   HwRead20K1 (devc, SRCCF(chn)), HwRead20K1 (devc,
									 SRCSA(chn)),
	   HwRead20K1 (devc, SRCLA(chn)), HwRead20K1 (devc,
									 SRCCCR(chn)),
	   HwRead20K1 (devc, SRCCTL(chn)));
}


void
_dumpGlobal (sbxfi_devc_t * devc)
{
  unsigned int i;

  cmn_err (CE_CONT,
	   "GCTL=%lx, PLLCTL=%lx, GPIOCTL=%lx, GPIO=%lx, I2SCTL=%lx\n",
	   HwRead20K1 (devc, GCTL), HwRead20K1 (devc, PLLCTL),
	   HwRead20K1 (devc, GPIOCTL), HwRead20K1 (devc, GPIO),
	   HwRead20K1 (devc, I2SCTL));
#if 1
  cmn_err (CE_CONT, "DAOIMAP....\n");
  for (i = 0; i < 0x50; i++)
    {
      cmn_err (CE_CONT, "%02lx: %lx  ", i,
	       HwRead20K1 (devc, DAOIMAP_START(i)));
      if (((i + 1) % 8) == 0)
	cmn_err (CE_CONT, "\n");
    }
#endif
#if 0
  cmn_err (CE_CONT, "PageTable PhysAddx=%lx\n", HwRead20K1 (devc, PTPALX));
  for (i = 0; i < 10; i++)
    {
      cmn_err (CE_CONT, "Entry[%lx]=%lx\n", i, devc->pdwPageTable[i]);
    }
#endif
}