summaryrefslogtreecommitdiff
path: root/target/linux/amazon-2.6/files/drivers/char/ifx_ssc.c
blob: ea01659a94aca56c8361b347edd3b56c061e5e89 (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
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
/**************************************************
 *
 * drivers/ifx/serial/ifx_ssc.c
 *
 * Driver for IFX_SSC serial ports
 *
 * Copyright (C) 2004 Infineon Technologies AG
 * Author Michael Schoenenborn (IFX COM TI BT)
 *
 */
#define IFX_SSC_DRV_VERSION "0.2.1"
/*
 **************************************************
 *
 * This driver was originally based on the INCA-IP driver, but due to 
 * fundamental conceptual drawbacks there has been changed a lot.
 *
 * Based on INCA-IP driver Copyright (c) 2003 Gary Jennejohn <gj@denx.de>
 * Based on the VxWorks drivers Copyright (c) 2002, Infineon Technologies.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

// ### TO DO: general issues:
//	      - power management
//	      - interrupt handling (direct/indirect)
//	      - pin/mux-handling (just overall concept due to project dependency)
//	      - multiple instances capability
//	      - slave functionality

/*
 * Include section 
 */
#ifndef EXPORT_SYMTAB
#define EXPORT_SYMTAB
#endif

#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
//#include <linux/poll.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/version.h>

#include <asm/amazon/amazon.h>
#include <asm/amazon/irq.h>
#include <asm/amazon/ifx_ssc_defines.h>
#include <asm/amazon/ifx_ssc.h>

#ifdef SSC_FRAME_INT_ENABLE
#undef SSC_FRAME_INT_ENABLE
#endif

#define not_yet

#define SPI_VINETIC

/*
 * Deal with CONFIG_MODVERSIONS
 */
#if CONFIG_MODVERSIONS==1
# include <linux/modversions.h>
#endif

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Michael Schoenenborn");
MODULE_DESCRIPTION("IFX SSC driver");
MODULE_SUPPORTED_DEVICE("ifx_ssc");
MODULE_PARM(maj, "i");
MODULE_PARM_DESC(maj, "Major device number");

/* allow the user to set the major device number */
static int maj = 0;


/*
 * This is the per-channel data structure containing pointers, flags
 * and variables for the port. This driver supports a maximum of PORT_CNT.
 * isp is allocated in ifx_ssc_init() based on the chip version.
 */
static struct ifx_ssc_port *isp;

/* prototypes for fops */
static ssize_t ifx_ssc_read(struct file *, char *, size_t, loff_t *);
static ssize_t ifx_ssc_write(struct file *, const char *, size_t, loff_t *);
//static unsigned int ifx_ssc_poll(struct file *, struct poll_table_struct *);
int ifx_ssc_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
int ifx_ssc_open(struct inode *, struct file *);
int ifx_ssc_close(struct inode *, struct file *);

/* other forward declarations */
static unsigned int ifx_ssc_get_kernel_clk(struct ifx_ssc_port *info);
static void ifx_ssc_rx_int(int, void *, struct pt_regs *);
static void ifx_ssc_tx_int(int, void *, struct pt_regs *);
static void ifx_ssc_err_int(int, void *, struct pt_regs *);
#ifdef SSC_FRAME_INT_ENABLE
static void ifx_ssc_frm_int(int, void *, struct pt_regs *);
#endif
static void tx_int(struct ifx_ssc_port *);
static int ifx_ssc1_read_proc(char *, char **, off_t, int, int *, void *);
static void ifx_gpio_init(void);
/************************************************************************
 *  Function declaration
 ************************************************************************/
//interrupt.c
extern unsigned int amazon_get_fpi_hz(void);
extern void disable_amazon_irq(unsigned int irq_nr);
extern void enable_amazon_irq(unsigned int irq_nr);
extern void mask_and_ack_amazon_irq(unsigned int irq_nr);


/*****************************************************************/
typedef struct {
	int (*request)(unsigned int irq, 
		       void (*handler)(int, void *, struct pt_regs *), 
		       unsigned long irqflags,
		       const char * devname, 
		       void *dev_id);
	void (*free)(unsigned int irq, void *dev_id);
	void (*enable)(unsigned int irq);
	void (*disable)(unsigned int irq);
        void (*clear)(unsigned int irq);
} ifx_int_wrapper_t;

static ifx_int_wrapper_t ifx_int_wrapper = {
	request:	request_irq,	// IM action: enable int
	free:		free_irq,	// IM action: disable int
	enable:		enable_amazon_irq,
	disable:	disable_amazon_irq,
        clear:          mask_and_ack_amazon_irq,
	//end:		
};

/* Fops-struct */
static struct file_operations ifx_ssc_fops = {
        owner:		THIS_MODULE,
	read:		ifx_ssc_read,    /* read */
	write:		ifx_ssc_write,   /* write */
//        poll:		ifx_ssc_poll,    /* poll */
        ioctl:		ifx_ssc_ioctl,   /* ioctl */
        open:		ifx_ssc_open,    /* open */
        release:	ifx_ssc_close,   /* release */
};


static inline unsigned int ifx_ssc_get_kernel_clk(struct ifx_ssc_port *info)
{ // ATTENTION: This function assumes that the CLC register is set with the 
  // appropriate value for RMC.
	unsigned int rmc;

	rmc = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_CLC) & 
	       IFX_CLC_RUN_DIVIDER_MASK) >> IFX_CLC_RUN_DIVIDER_OFFSET;
	if (rmc == 0){
		printk("ifx_ssc_get_kernel_clk rmc==0 \n");
		return (0);
	}
	return (amazon_get_fpi_hz() / rmc);
}

#ifndef not_yet
#ifdef IFX_SSC_INT_USE_BH
/*
 * This routine is used by the interrupt handler to schedule
 * processing in the software interrupt portion of the driver
 * (also known as the "bottom half").  This can be called any
 * number of times for any channel without harm.
 */
static inline void
ifx_ssc_sched_event(struct ifx_ssc_port *info, int event)
{
    info->event |= 1 << event; /* remember what kind of event and who */
    queue_task(&info->tqueue, &tq_cyclades); /* it belongs to */
    mark_bh(CYCLADES_BH);                       /* then trigger event */
} /* ifx_ssc_sched_event */


/*
 * This routine is used to handle the "bottom half" processing for the
 * serial driver, known also the "software interrupt" processing.
 * This processing is done at the kernel interrupt level, after the
 * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 * is where time-consuming activities which can not be done in the
 * interrupt driver proper are done; the interrupt driver schedules
 * them using ifx_ssc_sched_event(), and they get done here.
 *
 * This is done through one level of indirection--the task queue.
 * When a hardware interrupt service routine wants service by the
 * driver's bottom half, it enqueues the appropriate tq_struct (one
 * per port) to the tq_cyclades work queue and sets a request flag
 * via mark_bh for processing that queue.  When the time is right,
 * do_ifx_ssc_bh is called (because of the mark_bh) and it requests
 * that the work queue be processed.
 *
 * Although this may seem unwieldy, it gives the system a way to
 * pass an argument (in this case the pointer to the ifx_ssc_port
 * structure) to the bottom half of the driver.  Previous kernels
 * had to poll every port to see if that port needed servicing.
 */
static void
do_ifx_ssc_bh(void)
{
    run_task_queue(&tq_cyclades);
} /* do_ifx_ssc_bh */

static void
do_softint(void *private_)
{
  struct ifx_ssc_port *info = (struct ifx_ssc_port *) private_;

    if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
        wake_up_interruptible(&info->open_wait);
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|
                             ASYNC_CALLOUT_ACTIVE);
    }
    if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
        wake_up_interruptible(&info->open_wait);
    }
    if (test_and_clear_bit(Cy_EVENT_DELTA_WAKEUP, &info->event)) {
	wake_up_interruptible(&info->delta_msr_wait);
    }
    if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
        wake_up_interruptible(&tty->write_wait);
    }
#ifdef Z_WAKE
    if (test_and_clear_bit(Cy_EVENT_SHUTDOWN_WAKEUP, &info->event)) {
        wake_up_interruptible(&info->shutdown_wait);
    }
#endif
} /* do_softint */
#endif /* IFX_SSC_INT_USE_BH */
#endif // not_yet

inline static void
rx_int(struct ifx_ssc_port *info)
{
	int		fifo_fill_lev, bytes_in_buf, i;
	unsigned long	tmp_val;
	unsigned long	*tmp_ptr;
	unsigned int	rx_valid_cnt;
	/* number of words waiting in the RX FIFO */
	fifo_fill_lev = (READ_PERIPHERAL_REGISTER(info->mapbase + 
                                                  IFX_SSC_FSTAT) & 
                         IFX_SSC_FSTAT_RECEIVED_WORDS_MASK) >>
                         IFX_SSC_FSTAT_RECEIVED_WORDS_OFFSET;
        // Note: There are always 32 bits in a fifo-entry except for the last 
        // word of a contigous transfer block and except for not in rx-only 
        // mode and CON.ENBV set. But for this case it should be a convention 
        // in software which helps:
        // In tx or rx/tx mode all transfers from the buffer to the FIFO are 
        // 32-bit wide, except for the last three bytes, which could be a 
        // combination of 16- and 8-bit access.
        // => The whole block is received as 32-bit words as a contigous stream, 
        // even if there was a gap in tx which has the fifo run out of data! 
        // Just the last fifo entry *may* be partially filled (0, 1, 2 or 3 bytes)!

	/* free space in the RX buffer */
	bytes_in_buf = info->rxbuf_end - info->rxbuf_ptr;
        // transfer with 32 bits per entry
	while ((bytes_in_buf >= 4) && (fifo_fill_lev > 0)) {
		tmp_ptr = (unsigned long *)info->rxbuf_ptr;
                *tmp_ptr = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RB);
                info->rxbuf_ptr += 4;
                info->stats.rxBytes += 4;
                fifo_fill_lev --;
                bytes_in_buf -= 4;
	} // while ((bytes_in_buf >= 4) && (fifo_fill_lev > 0))
        // now do the rest as mentioned in STATE.RXBV
        while ((bytes_in_buf > 0) && (fifo_fill_lev > 0)) {
                rx_valid_cnt = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) & 
                                IFX_SSC_STATE_RX_BYTE_VALID_MASK) >> 
                        IFX_SSC_STATE_RX_BYTE_VALID_OFFSET;
		if (rx_valid_cnt == 0) break;
                if (rx_valid_cnt > bytes_in_buf) {
                        // ### TO DO: warning message: not block aligned data, other data 
                        //                             in this entry will be lost
                        rx_valid_cnt = bytes_in_buf;
                }
                tmp_val = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RB);
		
                for (i=0; i<rx_valid_cnt; i++) {		
			*info->rxbuf_ptr = (tmp_val >> ( 8 * (rx_valid_cnt - i-1))) & 0xff;
/*			
                        *info->rxbuf_ptr = tmp_val & 0xff;
                        tmp_val >>= 8;
*/			
			bytes_in_buf--;

			
			info->rxbuf_ptr++;
                }
                info->stats.rxBytes += rx_valid_cnt;
        }  // while ((bytes_in_buf > 0) && (fifo_fill_lev > 0))

        // check if transfer is complete
        if (info->rxbuf_ptr >= info->rxbuf_end) {
                ifx_int_wrapper.disable(info->rxirq);
                /* wakeup any processes waiting in read() */
                wake_up_interruptible(&info->rwait);
                /* and in poll() */
                //wake_up_interruptible(&info->pwait);
        } else if ((info->opts.modeRxTx == IFX_SSC_MODE_RX) && 
		   (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RXCNT) == 0)) {
		// if buffer not filled completely and rx request done initiate new transfer
/*
		if  (info->rxbuf_end - info->rxbuf_ptr < 65536)
*/
		if  (info->rxbuf_end - info->rxbuf_ptr < IFX_SSC_RXREQ_BLOCK_SIZE)
                        WRITE_PERIPHERAL_REGISTER((info->rxbuf_end - info->rxbuf_ptr) << 
						  IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
                else 
			WRITE_PERIPHERAL_REGISTER(IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
	}
} // rx_int

inline static void
tx_int(struct ifx_ssc_port *info)
{
	
	int fifo_space, fill, i;
	fifo_space = ((READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_ID) & 
                       IFX_SSC_PERID_TXFS_MASK) >> IFX_SSC_PERID_TXFS_OFFSET) - 
                ((READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_FSTAT) & 
                  IFX_SSC_FSTAT_TRANSMIT_WORDS_MASK) >> 
                 IFX_SSC_FSTAT_TRANSMIT_WORDS_OFFSET);

	if (fifo_space == 0)
		return;

	fill = info->txbuf_end - info->txbuf_ptr;

	if (fill > fifo_space * 4)
		fill = fifo_space * 4;

	for (i = 0; i < fill / 4; i++) {
                // at first 32 bit access
		WRITE_PERIPHERAL_REGISTER(*(UINT32 *)info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
                info->txbuf_ptr += 4;
	}

        fifo_space -= fill / 4;
	info->stats.txBytes += fill & ~0x3;
        fill &= 0x3;
        if ((fifo_space > 0) & (fill > 1)) {
                // trailing 16 bit access
                WRITE_PERIPHERAL_REGISTER_16(*(UINT16 *)info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
                info->txbuf_ptr += 2;
                info->stats.txBytes += 2;
                fifo_space --;
/* added by bingtao */
		fill -=2;
	}
        if ((fifo_space > 0) & (fill > 0)) {
                // trailing 8 bit access
                WRITE_PERIPHERAL_REGISTER_8(*(UINT8 *)info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
                info->txbuf_ptr ++;
                info->stats.txBytes ++;
/*
                fifo_space --;
*/
	}

        // check if transmission complete
        if (info->txbuf_ptr >= info->txbuf_end) {
                ifx_int_wrapper.disable(info->txirq);
                kfree(info->txbuf);
                info->txbuf = NULL;
                /* wake up any process waiting in poll() */
                //wake_up_interruptible(&info->pwait);
        }	

} // tx_int

static void
ifx_ssc_rx_int(int irq, void *dev_id, struct pt_regs *regs)
{
	struct ifx_ssc_port *info = (struct ifx_ssc_port *)dev_id;
	//WRITE_PERIPHERAL_REGISTER(IFX_SSC_R_BIT, info->mapbase + IFX_SSC_IRN_CR);
        rx_int(info);
}

static void
ifx_ssc_tx_int(int irq, void *dev_id, struct pt_regs *regs)
{
	struct ifx_ssc_port *info = (struct ifx_ssc_port *)dev_id;
	//WRITE_PERIPHERAL_REGISTER(IFX_SSC_T_BIT, info->mapbase + IFX_SSC_IRN_CR);
        tx_int(info);
}

static void
ifx_ssc_err_int(int irq, void *dev_id, struct pt_regs *regs)
{
	struct ifx_ssc_port *info = (struct ifx_ssc_port *)dev_id;
	unsigned int state;
	unsigned int write_back = 0;
	unsigned long flags;


	local_irq_save(flags);
	state = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE); 

	if ((state & IFX_SSC_STATE_RX_UFL) != 0) {
		info->stats.rxUnErr++;
		write_back |= IFX_SSC_WHBSTATE_CLR_RX_UFL_ERROR;
	}
	if ((state & IFX_SSC_STATE_RX_OFL) != 0) {
		info->stats.rxOvErr++;
		write_back |= IFX_SSC_WHBSTATE_CLR_RX_OFL_ERROR;
	}
	if ((state & IFX_SSC_STATE_TX_OFL) != 0) {
		info->stats.txOvErr++;
		write_back |= IFX_SSC_WHBSTATE_CLR_TX_OFL_ERROR;
	}
	if ((state & IFX_SSC_STATE_TX_UFL) != 0) {
		info->stats.txUnErr++;
		write_back |= IFX_SSC_WHBSTATE_CLR_TX_UFL_ERROR;
	}
//	if ((state & IFX_SSC_STATE_ABORT_ERR) != 0) {
//		info->stats.abortErr++;
//		write_back |= IFX_SSC_WHBSTATE_CLR_ABORT_ERROR;
//	}
	if ((state & IFX_SSC_STATE_MODE_ERR) != 0) {
		info->stats.modeErr++;
		write_back |= IFX_SSC_WHBSTATE_CLR_MODE_ERROR;
	}

	if (write_back)
		WRITE_PERIPHERAL_REGISTER(write_back, 
					  info->mapbase + IFX_SSC_WHBSTATE);

	local_irq_restore(flags);
}

#ifdef SSC_FRAME_INT_ENABLE
static void
ifx_ssc_frm_int(int irq, void *dev_id, struct pt_regs *regs)
{
	// ### TO DO: wake up framing wait-queue in conjunction with batch execution
}
#endif

static void
ifx_ssc_abort(struct ifx_ssc_port *info)
{
	unsigned long flags;
	bool enabled;

        local_irq_save(flags);

	// disable all int's
	ifx_int_wrapper.disable(info->rxirq);
	ifx_int_wrapper.disable(info->txirq);
	ifx_int_wrapper.disable(info->errirq);
/*
	ifx_int_wrapper.disable(info->frmirq);
*/
        local_irq_restore(flags);

	// disable SSC (also aborts a receive request!)
	// ### TO DO: Perhaps it's better to abort after the receiption of a 
	// complete word. The disable cuts the transmission immediatly and 
	// releases the chip selects. This could result in unpredictable 
	// behavior of connected external devices!
	enabled = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) 
		   & IFX_SSC_STATE_IS_ENABLED) != 0;
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ENABLE, 
				  info->mapbase + IFX_SSC_WHBSTATE);


	// flush fifos
        WRITE_PERIPHERAL_REGISTER(IFX_SSC_XFCON_FIFO_FLUSH, 
                                  info->mapbase + IFX_SSC_TXFCON);
        WRITE_PERIPHERAL_REGISTER(IFX_SSC_XFCON_FIFO_FLUSH, 
                                  info->mapbase + IFX_SSC_RXFCON);

	// free txbuf
	if (info->txbuf != NULL) {
		kfree(info->txbuf);
		info->txbuf = NULL;
	}

	// wakeup read process
	if (info->rxbuf != NULL)
		wake_up_interruptible(&info->rwait);

	// clear pending int's 
	ifx_int_wrapper.clear(info->rxirq);
	ifx_int_wrapper.clear(info->txirq);
	ifx_int_wrapper.clear(info->errirq);
/*
	ifx_int_wrapper.clear(info->frmirq);
*/

	// clear error flags
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ALL_ERROR,
				  info->mapbase + IFX_SSC_WHBSTATE);

	//printk("IFX SSC%d: Transmission aborted\n", info->port_nr);
	// enable SSC
	if (enabled)
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_SET_ENABLE, 
					  info->mapbase + IFX_SSC_WHBSTATE);

} // ifx_ssc_abort


/*
 * This routine is called whenever a port is opened.  It enforces
 * exclusive opening of a port and enables interrupts, etc.
 */
int
ifx_ssc_open(struct inode *inode, struct file * filp)
{
	struct ifx_ssc_port  *info;
	int line;
	int from_kernel = 0;

	if ((inode == (struct inode *)0) || (inode == (struct inode *)1)) {
		from_kernel = 1;
		line = (int)inode;
	}
	else {
		line = MINOR(filp->f_dentry->d_inode->i_rdev);
		filp->f_op = &ifx_ssc_fops;
	}

	/* don't open more minor devices than we can support */
	if (line < 0 || line >= PORT_CNT)
		return -ENXIO;

	info = &isp[line];

	/* exclusive open */
	if (info->port_is_open != 0)
		return -EBUSY;
	info->port_is_open++;

	ifx_int_wrapper.disable(info->rxirq);
	ifx_int_wrapper.disable(info->txirq);
	ifx_int_wrapper.disable(info->errirq);
/*
	ifx_int_wrapper.disable(info->frmirq);
*/

	/* Flush and enable TX/RX FIFO */
	WRITE_PERIPHERAL_REGISTER((IFX_SSC_DEF_TXFIFO_FL << 
                                   IFX_SSC_XFCON_ITL_OFFSET) | 
				  IFX_SSC_XFCON_FIFO_FLUSH   |
                                  IFX_SSC_XFCON_FIFO_ENABLE, 
                                  info->mapbase + IFX_SSC_TXFCON);
        WRITE_PERIPHERAL_REGISTER((IFX_SSC_DEF_RXFIFO_FL << 
                                   IFX_SSC_XFCON_ITL_OFFSET) |
				  IFX_SSC_XFCON_FIFO_FLUSH   | 
                                  IFX_SSC_XFCON_FIFO_ENABLE, 
                                  info->mapbase + IFX_SSC_RXFCON);


	/* logically flush the software FIFOs */
	info->rxbuf_ptr = 0;
	info->txbuf_ptr = 0;

	/* clear all error bits */
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ALL_ERROR,
				  info->mapbase + IFX_SSC_WHBSTATE);

	// clear pending interrupts
	ifx_int_wrapper.clear(info->rxirq);
	ifx_int_wrapper.clear(info->txirq);
	ifx_int_wrapper.clear(info->errirq);
/*
	ifx_int_wrapper.clear(info->frmirq);
*/

	// enable SSC
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_SET_ENABLE, 
				  info->mapbase + IFX_SSC_WHBSTATE);

	MOD_INC_USE_COUNT;

	return 0;
} /* ifx_ssc_open */
EXPORT_SYMBOL(ifx_ssc_open);

/*
 * This routine is called when a particular device is closed.
 */
int
ifx_ssc_close(struct inode *inode, struct file *filp)
{
	struct ifx_ssc_port *info;
        int idx;

	if ((inode == (struct inode *)0) || (inode == (struct inode *)1))
		idx = (int)inode;
	else
		idx = MINOR(filp->f_dentry->d_inode->i_rdev);

	if (idx < 0 || idx >= PORT_CNT)
		return -ENXIO;

	info = &isp[idx];
        if (!info)
                return -ENXIO;

	// disable SSC
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ENABLE, 
				  info->mapbase + IFX_SSC_WHBSTATE);

	// call abort function to disable int's, flush fifos...
	ifx_ssc_abort(info);

	info->port_is_open --;
	MOD_DEC_USE_COUNT;

	return 0;
} /* ifx_ssc_close */
EXPORT_SYMBOL(ifx_ssc_close);

/* added by bingtao */
/* helper routine to handle reads from the kernel or user-space */
/* info->rxbuf : never kfree and contains valid data */
/* should be points to NULL after copying data !!! */
static ssize_t
ifx_ssc_read_helper_poll(struct ifx_ssc_port *info, char *buf, size_t len,
                    int from_kernel)
{
        ssize_t         ret_val;
	unsigned long   flags;

        if (info->opts.modeRxTx == IFX_SSC_MODE_TX) 
                return -EFAULT;
        local_irq_save(flags);
        info->rxbuf_ptr = info->rxbuf;
        info->rxbuf_end = info->rxbuf + len;
        local_irq_restore(flags);
/* Vinetic driver always works in IFX_SSC_MODE_RXTX */ 
/* TXRX in poll mode */
	while (info->rxbuf_ptr < info->rxbuf_end){
/* This is the key point, if you don't check this condition 
   kfree (NULL) will happen 
   because tx only need write into FIFO, it's much fast than rx
   So when rx still waiting , tx already finish and release buf	
*/	
		if (info->txbuf_ptr < info->txbuf_end) {
			tx_int(info);
		}
		
		rx_int(info);
        };

        ret_val = info->rxbuf_ptr - info->rxbuf; 
	return (ret_val);
} // ifx_ssc_read_helper_poll

/* helper routine to handle reads from the kernel or user-space */
/* info->rx_buf : never kfree and contains valid data */
/* should be points to NULL after copying data !!! */
static ssize_t
ifx_ssc_read_helper(struct ifx_ssc_port *info, char *buf, size_t len,
                    int from_kernel)
{
        ssize_t         ret_val;
	unsigned long   flags;
        DECLARE_WAITQUEUE(wait, current);

        if (info->opts.modeRxTx == IFX_SSC_MODE_TX) 
                return -EFAULT;
        local_irq_save(flags);
        info->rxbuf_ptr = info->rxbuf;
        info->rxbuf_end = info->rxbuf + len;
        if (info->opts.modeRxTx == IFX_SSC_MODE_RXTX) {
                if ((info->txbuf == NULL) ||
                    (info->txbuf != info->txbuf_ptr) ||
                    (info->txbuf_end != len + info->txbuf)) {
                        local_irq_restore(flags);
                        printk("IFX SSC - %s: write must be called before calling "
                               "read in combined RX/TX!\n", __FUNCTION__);
                        return -EFAULT;
                }
		local_irq_restore(flags);
		/* should enable tx, right?*/
		tx_int(info);
		if (info->txbuf_ptr < info->txbuf_end){
			ifx_int_wrapper.enable(info->txirq);
		}
                
		ifx_int_wrapper.enable(info->rxirq);
        } else { // rx mode
		local_irq_restore(flags);
                if (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RXCNT) &
                    IFX_SSC_RXCNT_TODO_MASK)
                        return -EBUSY;
		ifx_int_wrapper.enable(info->rxirq);
		// rx request limited to ' bytes
/*
                if (len < 65536)
*/
                if (len < IFX_SSC_RXREQ_BLOCK_SIZE)
                        WRITE_PERIPHERAL_REGISTER(len << IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
                else 
			WRITE_PERIPHERAL_REGISTER(IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
        }

        __add_wait_queue(&info->rwait, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
        // wakeup done in rx_int
 
        do {
		local_irq_save(flags);
                if (info->rxbuf_ptr >= info->rxbuf_end)
                        break;
		local_irq_restore(flags);

//                if (filp->f_flags & O_NONBLOCK)
//                {
//                        N = -EAGAIN;
//                        goto out;
//                }
                if (signal_pending(current)) {
                        ret_val = -ERESTARTSYS;
                        goto out;
                }
                schedule();
        } while (1);

        ret_val = info->rxbuf_ptr - info->rxbuf; // should be equal to len
	local_irq_restore(flags);

  out:
        current->state = TASK_RUNNING;
        __remove_wait_queue(&info->rwait, &wait);
	return (ret_val);
} // ifx_ssc_read_helper


#if 0
/* helper routine to handle reads from the kernel or user-space */
/* appropriate in interrupt context */
static ssize_t
ifx_ssc_read_helper(struct ifx_ssc_port *info, char *buf, size_t len,
                    int from_kernel)
{
        ssize_t         ret_val;
	unsigned long   flags;
       	DECLARE_WAITQUEUE(wait, current);
	
        if (info->opts.modeRxTx == IFX_SSC_MODE_TX) 
                return -EFAULT;
        local_irq_save(flags);
        info->rxbuf_ptr = info->rxbuf;
        info->rxbuf_end = info->rxbuf + len;
        if (info->opts.modeRxTx == IFX_SSC_MODE_RXTX) {
                if ((info->txbuf == NULL) ||
                    (info->txbuf != info->txbuf_ptr) ||
                    (info->txbuf_end != len + info->txbuf)) {
                        local_irq_restore(flags);
                        printk("IFX SSC - %s: write must be called before calling "
                               "read in combined RX/TX!\n", __FUNCTION__);
                        return -EFAULT;
                }
		local_irq_restore(flags);
		/* should enable tx, right?*/
		tx_int(info);
		if (!in_irq()){
			if (info->txbuf_ptr < info->txbuf_end){
				ifx_int_wrapper.enable(info->txirq);
			}
			ifx_int_wrapper.enable(info->rxirq);
		}
        } else { // rx mode
		local_irq_restore(flags);
                if (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RXCNT) &
                    IFX_SSC_RXCNT_TODO_MASK)
                        return -EBUSY;
		if (!in_irq()){
			ifx_int_wrapper.enable(info->rxirq);
		}

                if (len < IFX_SSC_RXREQ_BLOCK_SIZE)
                        WRITE_PERIPHERAL_REGISTER(len << IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
                else 
			WRITE_PERIPHERAL_REGISTER(IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, 
                                                  info->mapbase + IFX_SSC_RXREQ);
        }
	if (in_irq()){
		do {
			rx_int(info);
			if (info->opts.modeRxTx == IFX_SSC_MODE_RXTX) {
				tx_int(info);
			}
			
                	if (info->rxbuf_ptr >= info->rxbuf_end)
                        	break;
        	} while (1);
		ret_val = info->rxbuf_ptr - info->rxbuf;
	}else{
        	__add_wait_queue(&info->rwait, &wait);
        	set_current_state(TASK_INTERRUPTIBLE);
        	// wakeup done in rx_int
 	
        	do {
			local_irq_save(flags);
                	if (info->rxbuf_ptr >= info->rxbuf_end)
                        	break;
			local_irq_restore(flags);

                	if (signal_pending(current)) {
                        	ret_val = -ERESTARTSYS;
                        	goto out;
                	}
                	schedule();
        	} while (1);

        	ret_val = info->rxbuf_ptr - info->rxbuf; // should be equal to len
		local_irq_restore(flags);

  out:
        	current->state = TASK_RUNNING;
        	__remove_wait_queue(&info->rwait, &wait);
	}
	return (ret_val);
} // ifx_ssc_read_helper
#endif

/* helper routine to handle writes to the kernel or user-space */
/* info->txbuf has two cases:
 *	1) return value < 0 (-EFAULT), not touched at all
 *	2) kfree and points to NULL in interrupt routine (but maybe later )
 */
static ssize_t
ifx_ssc_write_helper(struct ifx_ssc_port *info, const char *buf,
		size_t len, int from_kernel)
{
        // check if in tx or tx/rx mode
        if (info->opts.modeRxTx == IFX_SSC_MODE_RX)
                return -EFAULT;

        info->txbuf_ptr = info->txbuf;
        info->txbuf_end = len + info->txbuf;
	/* start the transmission (not in rx/tx, see read helper) */
        if (info->opts.modeRxTx == IFX_SSC_MODE_TX) {
		tx_int(info);
		if (info->txbuf_ptr < info->txbuf_end){
			ifx_int_wrapper.enable(info->txirq);
		}
        }
	//local_irq_restore(flags);
        return len;
}

/*
 * kernel interfaces for read and write.
 * The caller must set port to: n for SSC<m> with n=m-1 (e.g. n=0 for SSC1)
 */
ssize_t
ifx_ssc_kread(int port, char *kbuf, size_t len)
{
	struct ifx_ssc_port *info;
        ssize_t ret_val;

	if (port < 0 || port >= PORT_CNT)
		return -ENXIO;

        if (len == 0)
                return 0;

	info = &isp[port];

        // check if reception in progress
        if (info->rxbuf != NULL){
		printk("SSC device busy\n");
                return -EBUSY;
	}

	info->rxbuf = kbuf;
        if (info->rxbuf == NULL){
		printk("SSC device error\n");
                return -EINVAL;
	}

/* changed by bingtao */
	/* change by TaiCheng */
	//if (!in_irq()){
	if (0){
		ret_val = ifx_ssc_read_helper(info, kbuf, len, 1);
	}else{
		ret_val = ifx_ssc_read_helper_poll(info, kbuf, len, 1);
	};		
	info->rxbuf = NULL;

        // ### TO DO: perhaps warn if ret_val != len
	ifx_int_wrapper.disable(info->rxirq);

        return (ret_val);
} // ifx_ssc_kread
EXPORT_SYMBOL(ifx_ssc_kread);

ssize_t
ifx_ssc_kwrite(int port, const char *kbuf, size_t len)
{
	struct ifx_ssc_port *info;
	ssize_t ret_val;

	if (port < 0 || port >= PORT_CNT)
		return -ENXIO;

        if (len == 0)
                return 0;

	info = &isp[port];

        // check if transmission in progress
        if (info->txbuf != NULL)
                return -EBUSY;
        info->txbuf = (char *)kbuf;

	ret_val = ifx_ssc_write_helper(info, info->txbuf, len, 1);
	if (ret_val < 0){
		info->txbuf = NULL;
	}
	return ret_val;
}
EXPORT_SYMBOL(ifx_ssc_kwrite);


/* 
 * user interfaces to read and write
 */
static ssize_t
ifx_ssc_read(struct file *filp, char *ubuf, size_t len, loff_t *off)
{
        ssize_t ret_val;
        int idx;
	struct ifx_ssc_port *info;

/*
        if (len == 0)
                return (0);
*/
        idx = MINOR(filp->f_dentry->d_inode->i_rdev);
	info = &isp[idx];

        // check if reception in progress
        if (info->rxbuf != NULL)
                return -EBUSY;

	info->rxbuf = kmalloc(len+ 3, GFP_KERNEL);
        if (info->rxbuf == NULL)
                return -ENOMEM;

	ret_val = ifx_ssc_read_helper(info, info->rxbuf, len, 0);
	// ### TO DO: perhaps warn if ret_val != len
        if (copy_to_user((void*)ubuf, info->rxbuf, ret_val) != 0)
                ret_val = -EFAULT;

	ifx_int_wrapper.disable(info->rxirq);

        kfree(info->rxbuf);
	info->rxbuf = NULL;
        return (ret_val);
} // ifx_ssc_read

/*
 * As many bytes as we have free space for are copied from the user
 * into txbuf and the actual byte count is returned. The transmission is
 * always kicked off by calling the appropriate TX routine.
 */
static ssize_t
ifx_ssc_write(struct file *filp, const char *ubuf, size_t len, loff_t *off)
{
        int idx;
	struct ifx_ssc_port *info;
	int ret_val;

        if (len == 0)
                return (0);

        idx = MINOR(filp->f_dentry->d_inode->i_rdev);
	info = &isp[idx];

        // check if transmission in progress
        if (info->txbuf != NULL)
                return -EBUSY;

	info->txbuf = kmalloc(len+ 3, GFP_KERNEL);
        if (info->txbuf == NULL)
                return -ENOMEM;

	ret_val = copy_from_user(info->txbuf, ubuf, len);
	if (ret_val == 0)
		ret_val = ifx_ssc_write_helper(info, info->txbuf, len, 0);
	else
		ret_val = -EFAULT;
	if (ret_val < 0) {
                kfree(info->txbuf); // otherwise will be done in ISR
		info->txbuf = NULL;
	}
	return (ret_val);
} /* ifx_ssc_write */


/*
 * ------------------------------------------------------------
 * ifx_ssc_ioctl() and friends
 * ------------------------------------------------------------
 */

/*-----------------------------------------------------------------------------
 FUNC-NAME  : ifx_ssc_frm_status_get
 LONG-NAME  : framing status get
 PURPOSE    : Get the actual status of the framing.

 PARAMETER  : *info	pointer to the port-specific structure ifx_ssc_port.

 RESULT     : pointer to a structure ifx_ssc_frm_status which holds busy and 
	      count values.

 REMARKS    : Returns a register value independent of framing is enabled or 
	      not! Changes structure inside of info, so the return value isn't 
	      needed at all, but could be used for simple access.
-----------------------------------------------------------------------------*/
static struct ifx_ssc_frm_status *
ifx_ssc_frm_status_get(struct ifx_ssc_port *info)
{
	unsigned long tmp;
	
	tmp = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_SFSTAT);
	info->frm_status.DataBusy = (tmp & IFX_SSC_SFSTAT_IN_DATA) > 0;
	info->frm_status.PauseBusy = (tmp & IFX_SSC_SFSTAT_IN_PAUSE) > 0;
	info->frm_status.DataCount = (tmp & IFX_SSC_SFSTAT_DATA_COUNT_MASK) 
		>> IFX_SSC_SFSTAT_DATA_COUNT_OFFSET;
	info->frm_status.PauseCount = (tmp & IFX_SSC_SFSTAT_PAUSE_COUNT_MASK) 
		>> IFX_SSC_SFSTAT_PAUSE_COUNT_OFFSET;
	tmp = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_SFCON);
	info->frm_status.EnIntAfterData = 
		(tmp & IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE) > 0;
	info->frm_status.EnIntAfterPause = 
		(tmp & IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE) > 0;
	return (&info->frm_status);
} // ifx_ssc_frm_status_get


/*-----------------------------------------------------------------------------
 FUNC-NAME  : ifx_ssc_frm_control_get
 LONG-NAME  : framing control get
 PURPOSE    : Get the actual control values of the framing.

 PARAMETER  : *info	pointer to the port-specific structure ifx_ssc_port.

 RESULT     : pointer to a structure ifx_ssc_frm_opts which holds control bits  
	      and count reload values.

 REMARKS    : Changes structure inside of info, so the return value isn't 
	      needed at all, but could be used for simple access.
-----------------------------------------------------------------------------*/
static struct ifx_ssc_frm_opts *
ifx_ssc_frm_control_get(struct ifx_ssc_port *info)
{
	unsigned long tmp;
	
	tmp = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_SFCON);
	info->frm_opts.FrameEnable = (tmp & IFX_SSC_SFCON_SF_ENABLE) > 0;
	info->frm_opts.DataLength = (tmp & IFX_SSC_SFCON_DATA_LENGTH_MASK) 
		>> IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
	info->frm_opts.PauseLength = (tmp & IFX_SSC_SFCON_PAUSE_LENGTH_MASK) 
		>> IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
	info->frm_opts.IdleData = (tmp & IFX_SSC_SFCON_PAUSE_DATA_MASK) 
		>> IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
	info->frm_opts.IdleClock = (tmp & IFX_SSC_SFCON_PAUSE_CLOCK_MASK) 
		>> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
	info->frm_opts.StopAfterPause = 
		(tmp & IFX_SSC_SFCON_STOP_AFTER_PAUSE) > 0;
	return (&info->frm_opts);
} // ifx_ssc_frm_control_get


/*-----------------------------------------------------------------------------
 FUNC-NAME  : ifx_ssc_frm_control_set
 LONG-NAME  : framing control set
 PURPOSE    : Set the actual control values of the framing.

 PARAMETER  : *info	pointer to the port-specific structure ifx_ssc_port.

 RESULT     : pointer to a structure ifx_ssc_frm_opts which holds control bits  
	      and count reload values.

 REMARKS    : 
-----------------------------------------------------------------------------*/
static int
ifx_ssc_frm_control_set(struct ifx_ssc_port *info)
{
	unsigned long tmp;

	// check parameters
	if ((info->frm_opts.DataLength > IFX_SSC_SFCON_DATA_LENGTH_MAX) || 
	    (info->frm_opts.DataLength < 1) ||
	    (info->frm_opts.PauseLength > IFX_SSC_SFCON_PAUSE_LENGTH_MAX) || 
	    (info->frm_opts.PauseLength < 1) ||
	    ((info->frm_opts.IdleData & ~(IFX_SSC_SFCON_PAUSE_DATA_MASK >> 
					  IFX_SSC_SFCON_PAUSE_DATA_OFFSET)) != 0 ) || 
	    ((info->frm_opts.IdleClock & ~(IFX_SSC_SFCON_PAUSE_CLOCK_MASK >> 
					   IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET)) != 0 ))
		return -EINVAL;

	// read interrupt bits (they're not changed here)
	tmp = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_SFCON) &
		(IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE | 
		 IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE);

	// set all values with respect to it's bit position (for data and pause 
	// length set N-1)
	tmp = (info->frm_opts.DataLength - 1) << IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
	tmp |= (info->frm_opts.PauseLength - 1) << IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
	tmp |= info->frm_opts.IdleData << IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
	tmp |= info->frm_opts.IdleClock << IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
	tmp |= info->frm_opts.FrameEnable * IFX_SSC_SFCON_SF_ENABLE;
	tmp |= info->frm_opts.StopAfterPause * IFX_SSC_SFCON_STOP_AFTER_PAUSE;

	WRITE_PERIPHERAL_REGISTER(tmp, info->mapbase + IFX_SSC_SFCON);

	return 0;
} // ifx_ssc_frm_control_set


/*-----------------------------------------------------------------------------
 FUNC-NAME  : ifx_ssc_rxtx_mode_set
 LONG-NAME  : rxtx mode set
 PURPOSE    : Set the transmission mode.

 PARAMETER  : *info	pointer to the port-specific structure ifx_ssc_port.

 RESULT     : Returns error code

 REMARKS    : Assumes that SSC not used (SSC disabled, device not opened yet 
	      or just closed) 
-----------------------------------------------------------------------------*/
static int
ifx_ssc_rxtx_mode_set(struct ifx_ssc_port *info, unsigned int val)
{
	unsigned long tmp;

	// check parameters
	if (!(info) || (val & ~(IFX_SSC_MODE_MASK)))
		return -EINVAL;
	/*check BUSY and RXCNT*/	
	if (  READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_BUSY
	    ||READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_RXCNT) & IFX_SSC_RXCNT_TODO_MASK)
		return -EBUSY;
	// modify 
	tmp = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_CON) &
		~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF)) | (val);
	WRITE_PERIPHERAL_REGISTER(tmp, info->mapbase + IFX_SSC_CON);
	info->opts.modeRxTx = val;
/*	
	printk(KERN_DEBUG "IFX SSC%d: Setting mode to %s%s\n", 
	       info->port_nr,
	       ((val & IFX_SSC_CON_RX_OFF) == 0) ? "rx ":"", 
	       ((val & IFX_SSC_CON_TX_OFF) == 0) ? "tx":"");
*/
	return 0;
} // ifx_ssc_rxtx_mode_set

void ifx_gpio_init(void) 
{
	u32	temp;
/* set gpio pin p0.10(SPI_DIN) p0.11(SPI_DOUT) p0.12(SPI_CLK) p0.13(SPI_CS2) direction */
	temp = *(AMAZON_GPIO_P0_DIR) ;
      	temp &= 0xFFFFFBFF;
      	temp |= 0x3800;
	*(AMAZON_GPIO_P0_DIR) = temp;
/* set port 0 alternate select register 0 */
	temp = *(AMAZON_GPIO_P0_ALTSEL0) ;
	temp &= 0xFFFFC3FF;
	temp |= 0x00001c00;
	*(AMAZON_GPIO_P0_ALTSEL0) = temp;

/* set port 0 alternate select register 1 */
 	temp = *(AMAZON_GPIO_P0_ALTSEL1) ;
	temp &= 0xFFFFC3FF;
	temp |= 0x00002000;
	*(AMAZON_GPIO_P0_ALTSEL1) = temp;

/* set port 0 open drain mode register */
	temp = *(AMAZON_GPIO_P0_OD);
	temp |= 0x00003800;	/* set output pin normal mode */
	*(AMAZON_GPIO_P0_OD)= temp;
}

/*
 * This routine intializes the SSC appropriately depending
 * on slave/master and full-/half-duplex mode.
 * It assumes that the SSC is disabled and the fifo's and buffers 
 * are flushes later on.
 */
static int
ifx_ssc_sethwopts(struct ifx_ssc_port *info)
{
	unsigned long flags, bits;
	struct ifx_ssc_hwopts *opts = &info->opts;

	/* sanity checks */
	if ((opts->dataWidth < IFX_SSC_MIN_DATA_WIDTH) ||
	    (opts->dataWidth > IFX_SSC_MAX_DATA_WIDTH)) {
		printk("%s: sanity check failed\n", __FUNCTION__);
	    	return -EINVAL;
	}
        bits = (opts->dataWidth - 1) << IFX_SSC_CON_DATA_WIDTH_OFFSET;
	bits |= IFX_SSC_CON_ENABLE_BYTE_VALID;
//	if (opts->abortErrDetect)
//		bits |= IFX_SSC_CON_ABORT_ERR_CHECK;
	if (opts->rxOvErrDetect)
		bits |= IFX_SSC_CON_RX_OFL_CHECK;
	if (opts->rxUndErrDetect)
		bits |= IFX_SSC_CON_RX_UFL_CHECK;
	if (opts->txOvErrDetect)
		bits |= IFX_SSC_CON_TX_OFL_CHECK;
	if (opts->txUndErrDetect)
		bits |= IFX_SSC_CON_TX_UFL_CHECK;
	if (opts->loopBack)
		bits |= IFX_SSC_CON_LOOPBACK_MODE;
	if (opts->echoMode)
		bits |= IFX_SSC_CON_ECHO_MODE_ON;
	if (opts->headingControl)
		bits |= IFX_SSC_CON_MSB_FIRST;
	if (opts->clockPhase)
		bits |= IFX_SSC_CON_LATCH_THEN_SHIFT;
	if (opts->clockPolarity)
		bits |= IFX_SSC_CON_CLOCK_FALL;
	switch (opts->modeRxTx) {
	case IFX_SSC_MODE_TX:
		bits |= IFX_SSC_CON_RX_OFF;
		break;
	case IFX_SSC_MODE_RX:
		bits |= IFX_SSC_CON_TX_OFF;
		break;
	} // switch (opts->modeRxT)
	local_irq_save(flags);
        WRITE_PERIPHERAL_REGISTER(bits, info->mapbase + IFX_SSC_CON);
        WRITE_PERIPHERAL_REGISTER((info->opts.gpoCs << IFX_SSC_GPOCON_ISCSB0_POS) | 
				  (info->opts.gpoInv << IFX_SSC_GPOCON_INVOUT0_POS), 
				  info->mapbase + IFX_SSC_GPOCON);
	//master mode
	if (opts->masterSelect){
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_SET_MASTER_SELECT,info->mapbase + IFX_SSC_WHBSTATE);
	}else{
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_MASTER_SELECT,info->mapbase + IFX_SSC_WHBSTATE);
	}
	// init serial framing
        WRITE_PERIPHERAL_REGISTER(0, info->mapbase + IFX_SSC_SFCON);
	/* set up the port pins */
        //check for general requirements to switch (external) pad/pin characteristics
	ifx_gpio_init();
	local_irq_restore(flags);

	return 0;
} // ifx_ssc_sethwopts

static int
ifx_ssc_set_baud(struct ifx_ssc_port *info, unsigned int baud)
{
	unsigned int ifx_ssc_clock;
	unsigned int br;
	unsigned long flags;
	bool enabled;

	ifx_ssc_clock = ifx_ssc_get_kernel_clk(info);
	if (ifx_ssc_clock ==0)
		return -EINVAL;

	local_irq_save(flags);
	/* have to disable the SSC to set the baudrate */
	enabled = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) 
		   & IFX_SSC_STATE_IS_ENABLED) != 0;
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ENABLE, 
				  info->mapbase + IFX_SSC_WHBSTATE);

	// compute divider
	br = ((ifx_ssc_clock >> 1)/baud) - 1;
	asm("SYNC"); 
	if (br > 0xffff || 
	    ((br == 0) && 
	     ((READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) & 
	       IFX_SSC_STATE_IS_MASTER) == 0))){
		local_irq_restore(flags);
		printk("%s: illegal baudrate %u\n", __FUNCTION__, baud);
		return -EINVAL;
	}
	WRITE_PERIPHERAL_REGISTER(br, info->mapbase + IFX_SSC_BR);
	if (enabled)
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_SET_ENABLE, 
					  info->mapbase + IFX_SSC_WHBSTATE);

	local_irq_restore(flags);
	return 0;
} // ifx_ssc_set_baud

static int
ifx_ssc_hwinit(struct ifx_ssc_port *info)
{
	unsigned long flags;
	bool enabled;

	/* have to disable the SSC */
	enabled = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) 
		   & IFX_SSC_STATE_IS_ENABLED) != 0;
	WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ENABLE, 
				  info->mapbase + IFX_SSC_WHBSTATE);

	if (ifx_ssc_sethwopts(info) < 0)
	{
		printk("%s: setting the hardware options failed\n",
                       __FUNCTION__);
		return -EINVAL;
	}

	if (ifx_ssc_set_baud(info, info->baud) < 0) {
		printk("%s: setting the baud rate failed\n", __FUNCTION__);
		return -EINVAL;
	}
        local_irq_save(flags);
        /* TX FIFO */
        WRITE_PERIPHERAL_REGISTER((IFX_SSC_DEF_TXFIFO_FL << 
                                   IFX_SSC_XFCON_ITL_OFFSET) | 
                                  IFX_SSC_XFCON_FIFO_ENABLE, 
                                  info->mapbase + IFX_SSC_TXFCON);
        /* RX FIFO */
        WRITE_PERIPHERAL_REGISTER((IFX_SSC_DEF_RXFIFO_FL << 
                                   IFX_SSC_XFCON_ITL_OFFSET) | 
                                  IFX_SSC_XFCON_FIFO_ENABLE, 
                                  info->mapbase + IFX_SSC_RXFCON);
        local_irq_restore(flags);
	if (enabled)
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_SET_ENABLE, 
					  info->mapbase + IFX_SSC_WHBSTATE);
	return 0;
} // ifx_ssc_hwinit

/*-----------------------------------------------------------------------------
 FUNC-NAME  : ifx_ssc_batch_exec
 LONG-NAME  : 
 PURPOSE    : 

 PARAMETER  : *info	pointer to the port-specific structure ifx_ssc_port.

 RESULT     : Returns error code

 REMARKS    : 
-----------------------------------------------------------------------------*/
static int
ifx_ssc_batch_exec(struct ifx_ssc_port *info, struct ifx_ssc_batch_list *batch_anchor)
{
	// ### TO DO: implement user space batch execution
	// first, copy the whole linked list from user to kernel space
	// save some hardware options
	// execute list
	// restore hardware options if selected
	return -EFAULT;
} // ifx_ssc_batch_exec


/*
 * This routine allows the driver to implement device-
 * specific ioctl's.  If the ioctl number passed in cmd is
 * not recognized by the driver, it should return ENOIOCTLCMD.
 */
int
ifx_ssc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
              unsigned long data)
{
	struct ifx_ssc_port *info;
	int line, ret_val = 0;
	unsigned long flags;
	unsigned long tmp;
	int from_kernel = 0;

	if ((inode == (struct inode *)0) || (inode == (struct inode *)1))
	{
		from_kernel = 1;
		line = (int)inode;
	}
	else
		line = MINOR(filp->f_dentry->d_inode->i_rdev);

	/* don't use more minor devices than we can support */
	if (line < 0 || line >= PORT_CNT)
		return -ENXIO;

	info = &isp[line];

	switch (cmd) {
	case IFX_SSC_STATS_READ:
		/* data must be a pointer to a struct ifx_ssc_statistics */
		if (from_kernel)
			memcpy((void *)data, (void *)&info->stats,
			       sizeof(struct ifx_ssc_statistics));
		else
			if (copy_to_user((void *)data,
					 (void *)&info->stats,
					 sizeof(struct ifx_ssc_statistics)))
				ret_val = -EFAULT;
		break;
	case IFX_SSC_STATS_RESET:
		/* just resets the statistics counters */
		memset((void *)&info->stats, 0, sizeof(struct ifx_ssc_statistics));
		break;
        case IFX_SSC_BAUD_SET:
                /* if the buffers are not empty then the port is */
                /* busy and we shouldn't change things on-the-fly! */
                if (!info->txbuf || !info->rxbuf || 
		    (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) 
		     & IFX_SSC_STATE_BUSY)) {
                        ret_val = -EBUSY;
                        break;
                }
                /* misuse flags */
                if (from_kernel)
                        flags = *((unsigned long *)data);
                else
                        if (copy_from_user((void *)&flags,
                                           (void *)data, sizeof(flags)))
                        {
                                ret_val = -EFAULT;
                                break;
                        }
                if (flags == 0)
                {
                        ret_val = -EINVAL;
                        break;
                }
                if (ifx_ssc_set_baud(info, flags) < 0)
                {
                        ret_val = -EINVAL;
                        break;
                }
                info->baud = flags;
                break;
        case IFX_SSC_BAUD_GET:
                if (from_kernel)
                        *((unsigned int *)data) = info->baud;
                else
                        if (copy_to_user((void *)data,
                                         (void *)&info->baud,
                                         sizeof(unsigned long)))
                                ret_val = -EFAULT;
                break;
	case IFX_SSC_RXTX_MODE_SET:
                if (from_kernel)
                        tmp = *((unsigned long *)data);
                else
                        if (copy_from_user((void *)&tmp,
                                           (void *)data, sizeof(tmp))) {
                                ret_val = -EFAULT;
                                break;
                        }
		ret_val = ifx_ssc_rxtx_mode_set(info, tmp);
		break;
	case IFX_SSC_RXTX_MODE_GET:
		tmp = READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_CON) &
			(~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF));
                if (from_kernel)
                        *((unsigned int *)data) = tmp;
                else
                        if (copy_to_user((void *)data,
                                         (void *)&tmp,
                                         sizeof(tmp)))
                                ret_val = -EFAULT;
		break;

	case IFX_SSC_ABORT:
		ifx_ssc_abort(info);
		break;

	case IFX_SSC_GPO_OUT_SET:
                if (from_kernel)
                        tmp = *((unsigned long *)data);
                else
                        if (copy_from_user((void *)&tmp,
                                           (void *)data, sizeof(tmp))) {
                                ret_val = -EFAULT;
                                break;
                        }
		if (tmp > IFX_SSC_MAX_GPO_OUT)
			ret_val = -EINVAL;
		else
			WRITE_PERIPHERAL_REGISTER
				(1<<(tmp + IFX_SSC_WHBGPOSTAT_SETOUT0_POS), 
				 info->mapbase + IFX_SSC_WHBGPOSTAT);
		break;
	case IFX_SSC_GPO_OUT_CLR:
                if (from_kernel)
                        tmp = *((unsigned long *)data);
                else
                        if (copy_from_user((void *)&tmp,
                                           (void *)data, sizeof(tmp))) {
                                ret_val = -EFAULT;
                                break;
                        }
		if (tmp > IFX_SSC_MAX_GPO_OUT)
			ret_val = -EINVAL;
		else {
			WRITE_PERIPHERAL_REGISTER
				(1<<(tmp + IFX_SSC_WHBGPOSTAT_CLROUT0_POS), 
				 info->mapbase + IFX_SSC_WHBGPOSTAT);
		}
		break;
	case IFX_SSC_GPO_OUT_GET:
		tmp = READ_PERIPHERAL_REGISTER
			(info->mapbase + IFX_SSC_GPOSTAT);
                if (from_kernel)
                        *((unsigned int *)data) = tmp;
                else
                        if (copy_to_user((void *)data,
                                         (void *)&tmp,
                                         sizeof(tmp)))
                                ret_val = -EFAULT;
		break;
	case IFX_SSC_FRM_STATUS_GET:
		ifx_ssc_frm_status_get(info);
		if (from_kernel)
			memcpy((void *)data, (void *)&info->frm_status,
			       sizeof(struct ifx_ssc_frm_status));
		else
			if (copy_to_user((void *)data,
					 (void *)&info->frm_status,
					 sizeof(struct ifx_ssc_frm_status)))
				ret_val = -EFAULT;
		break;
	case IFX_SSC_FRM_CONTROL_GET:
		ifx_ssc_frm_control_get(info);
		if (from_kernel)
			memcpy((void *)data, (void *)&info->frm_opts,
			       sizeof(struct ifx_ssc_frm_opts));
		else
			if (copy_to_user((void *)data,
					 (void *)&info->frm_opts,
					 sizeof(struct ifx_ssc_frm_opts)))
				ret_val = -EFAULT;
		break;
	case IFX_SSC_FRM_CONTROL_SET:
		if (from_kernel)
			memcpy((void *)&info->frm_opts, (void *)data, 
			       sizeof(struct ifx_ssc_frm_opts));
		else
			if (copy_to_user((void *)&info->frm_opts,
					 (void *)data,
					 sizeof(struct ifx_ssc_frm_opts))){
				ret_val = -EFAULT;
				break;
			}
		ret_val = ifx_ssc_frm_control_set(info);
		break;
        case IFX_SSC_HWOPTS_SET:
		/* data must be a pointer to a struct ifx_ssc_hwopts */
                /* if the buffers are not empty then the port is */
                /* busy and we shouldn't change things on-the-fly! */
                if (!info->txbuf || !info->rxbuf || 
		    (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) 
		     & IFX_SSC_STATE_BUSY)) {
                        ret_val = -EBUSY;
                        break;
                }
                if (from_kernel)
                        memcpy((void *)&info->opts, (void *)data,
                               sizeof(struct ifx_ssc_hwopts));
                else
                        if (copy_from_user((void *)&info->opts,
                                           (void *)data,
                                           sizeof(struct ifx_ssc_hwopts)))
                        {
                                ret_val = -EFAULT;
                                break;
                        }
                if (ifx_ssc_hwinit(info) < 0)
                {
                        ret_val = -EIO;
                }
                break;
        case IFX_SSC_HWOPTS_GET:
		/* data must be a pointer to a struct ifx_ssc_hwopts */
                if (from_kernel)
                        memcpy((void *)data, (void *)&info->opts,
                               sizeof(struct ifx_ssc_hwopts));
                else
                        if (copy_to_user((void *)data,
                                         (void *)&info->opts,
                                         sizeof(struct ifx_ssc_hwopts)))
                                ret_val = -EFAULT;
                break;
        default:
                ret_val = -ENOIOCTLCMD;
	}

	return ret_val;
} /* ifx_ssc_ioctl */
EXPORT_SYMBOL(ifx_ssc_ioctl);

///* the poll routine */
//static unsigned int
//ifx_ssc_poll(struct file *filp, struct poll_table_struct *pts)
//{
//        int unit = MINOR(filp->f_dentry->d_inode->i_rdev);
//	struct ifx_ssc_port *info;
//        unsigned int mask = 0; 
//	int spc;
//
//	info = &isp[unit];
//
//        /* add event to the wait queues */
//        /* DO NOT FORGET TO DO A WAKEUP ON THESE !!!! */
//        poll_wait(filp, &info->pwait, pts);
//
//	/* are there bytes in the RX SW-FIFO? */
//        if (info->rxrp != info->rxwp)
//                mask |= POLLIN | POLLRDNORM;
//
//	/* free space in the TX SW-FIFO */
//	spc = info->txrp - info->txwp - 1;
//	if (spc < 0)
//		spc += TX_BUFSIZE;
//#ifdef IFX_SSC_USEDMA
//	/* writing always works, except in the DMA case when all descriptors */
//	/* are used up */
//	if (unit == 1 && info->dma_freecnt == 0)
//		spc = 0;
//#endif
//	if (spc > 0)
//		mask |= POLLOUT | POLLWRNORM;
//
//        return (mask);
//}

static int
ifx_ssc1_read_proc(char *page, char **start, off_t offset, int count, int *eof, void *data)
{
	int off = 0;
	unsigned long flags;

	/* don't want any interrupts here */
	save_flags(flags);
	cli();


	/* print statistics */
	off += sprintf(page+off, "Statistics for Infineon Synchronous Serial Controller SSC1\n");
	off += sprintf(page+off, "RX overflow errors %d\n", isp[0].stats.rxOvErr);
	off += sprintf(page+off, "RX underflow errors %d\n", isp[0].stats.rxUnErr);
	off += sprintf(page+off, "TX overflow errors %d\n", isp[0].stats.txOvErr);
	off += sprintf(page+off, "TX underflow errors %d\n", isp[0].stats.txUnErr);
	off += sprintf(page+off, "Abort errors %d\n", isp[0].stats.abortErr);
	off += sprintf(page+off, "Mode errors %d\n", isp[0].stats.modeErr);
	off += sprintf(page+off, "RX Bytes %d\n", isp[0].stats.rxBytes);
	off += sprintf(page+off, "TX Bytes %d\n", isp[0].stats.txBytes);

	restore_flags (flags); /* XXXXX */
	*eof = 1;
	return (off);
}


/*
 * This routine prints out the appropriate serial driver version number
 */
static inline void
show_version(void)
{
#if 0
    printk("Infineon Technologies Synchronous Serial Controller (SSC) driver\n"
           "  version %s - built %s %s\n", IFX_SSC_DRV_VERSION, __DATE__, __TIME__);
#endif	 
} /* show_version */


/*
 * Due to the fact that a port can be dynamically switched between slave
 * and master mode using an IOCTL the hardware is not initialized here,
 * but in ifx_ssc_hwinit() as a result of an IOCTL.
 */
int __init
ifx_ssc_init(void)
{
	struct ifx_ssc_port  *info;
	int i, nbytes;
	unsigned long flags;
	int ret_val;

	// ### TO DO: dynamic port count evaluation due to pin multiplexing

	ret_val = -ENOMEM;
	nbytes = PORT_CNT * sizeof(struct ifx_ssc_port);
	isp = (struct ifx_ssc_port *)kmalloc(nbytes, GFP_KERNEL);
	if (isp == NULL)
	{
		printk("%s: no memory for isp\n", __FUNCTION__);
		return (ret_val);
	}
	memset(isp, 0, nbytes);

	show_version();

	/* register the device */
	ret_val = -ENXIO;
/*
	i = maj;
*/
	if ((i = register_chrdev(maj, "ssc", &ifx_ssc_fops)) < 0)
	{
		printk("Unable to register major %d for the Infineon SSC\n", maj);
		if (maj == 0){
			goto errout;
		}
		else{
			maj = 0;
			if ((i = register_chrdev(maj, "ssc", &ifx_ssc_fops)) < 0)
			{
				printk("Unable to register major %d for the Infineon SSC\n", maj);
				goto errout;
			}
		}
	}
	if (maj == 0) maj = i;
	//printk("registered major %d for Infineon SSC\n", maj);

	/* set default values in ifx_ssc_port */
	for (i = 0; i < PORT_CNT; i++) {
		info = &isp[i];
                info->port_nr = i;
		/* default values for the HwOpts */
		info->opts.AbortErrDetect = IFX_SSC_DEF_ABRT_ERR_DETECT;
		info->opts.rxOvErrDetect = IFX_SSC_DEF_RO_ERR_DETECT;
		info->opts.rxUndErrDetect = IFX_SSC_DEF_RU_ERR_DETECT;
		info->opts.txOvErrDetect = IFX_SSC_DEF_TO_ERR_DETECT;
		info->opts.txUndErrDetect = IFX_SSC_DEF_TU_ERR_DETECT;
		info->opts.loopBack = IFX_SSC_DEF_LOOP_BACK;
		info->opts.echoMode = IFX_SSC_DEF_ECHO_MODE;
		info->opts.idleValue = IFX_SSC_DEF_IDLE_DATA;
		info->opts.clockPolarity = IFX_SSC_DEF_CLOCK_POLARITY;
		info->opts.clockPhase = IFX_SSC_DEF_CLOCK_PHASE;
		info->opts.headingControl = IFX_SSC_DEF_HEADING_CONTROL;
		info->opts.dataWidth = IFX_SSC_DEF_DATA_WIDTH;
		info->opts.modeRxTx = IFX_SSC_DEF_MODE_RXTX;
		info->opts.gpoCs = IFX_SSC_DEF_GPO_CS;
		info->opts.gpoInv = IFX_SSC_DEF_GPO_INV;
		info->opts.masterSelect = IFX_SSC_DEF_MASTERSLAVE;
		info->baud = IFX_SSC_DEF_BAUDRATE;
		info->rxbuf = NULL;
		info->txbuf = NULL;
		/* values specific to SSC1 */
		if (i == 0) {
			info->mapbase = AMAZON_SSC_BASE_ADD_0;
			// ### TO DO: power management

			// setting interrupt vectors
			info->txirq = IFX_SSC_TIR;
			info->rxirq = IFX_SSC_RIR;
			info->errirq = IFX_SSC_EIR;
/*
			info->frmirq = IFX_SSC_FIR;
*/
		}
		/* activate SSC */
		/* CLC.DISS = 0 */
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_DEF_RMC << IFX_CLC_RUN_DIVIDER_OFFSET, info->mapbase + IFX_SSC_CLC);

// ### TO DO: multiple instances

		init_waitqueue_head(&info->rwait);
		//init_waitqueue_head(&info->pwait);

		local_irq_save(flags);

		// init serial framing register
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_DEF_SFCON, info->mapbase + IFX_SSC_SFCON);

		/* try to get the interrupts */
		// ### TO DO: interrupt handling with multiple instances
		ret_val = ifx_int_wrapper.request(info->txirq, ifx_ssc_tx_int, 
						  0, "ifx_ssc_tx", info);
		if (ret_val){
			printk("%s: unable to get irq %d\n", __FUNCTION__,
					info->txirq);
			local_irq_restore(flags);
			goto errout;
		}
		ret_val = ifx_int_wrapper.request(info->rxirq, ifx_ssc_rx_int, 
						  0, "ifx_ssc_rx", info);
		if (ret_val){
			printk("%s: unable to get irq %d\n", __FUNCTION__,
					info->rxirq);
			local_irq_restore(flags);
			goto irqerr;
		}
		ret_val = ifx_int_wrapper.request(info->errirq, ifx_ssc_err_int, 
						  0, "ifx_ssc_err", info);
		if (ret_val){
			printk("%s: unable to get irq %d\n", __FUNCTION__,
					info->errirq);
			local_irq_restore(flags);
			goto irqerr;
		}
/*
		ret_val = ifx_int_wrapper.request(info->frmirq, ifx_ssc_frm_int, 
						  0, "ifx_ssc_frm", info);
		if (ret_val){
			printk("%s: unable to get irq %d\n", __FUNCTION__,
					info->frmirq);
			local_irq_restore(flags);
			goto irqerr;
		}

*/
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_DEF_IRNEN, info->mapbase + IFX_SSC_IRN_EN);

		local_irq_restore(flags);
	} // for (i = 0; i < PORT_CNT; i++)

	/* init the SSCs with default values */
	for (i = 0; i < PORT_CNT; i++)
	{
		info = &isp[i];
		if (ifx_ssc_hwinit(info) < 0)
		{
			printk("%s: hardware init failed for port %d\n",
				__FUNCTION__, i);
			goto irqerr;
		}
	}

	/* register /proc read handler */
	// ### TO DO: multiple instances
	/* for SSC1, which is always present */
	create_proc_read_entry("driver/ssc1", 0, NULL, ifx_ssc1_read_proc, NULL);
	return 0;

irqerr:
	// ### TO DO: multiple instances
	ifx_int_wrapper.free(isp[0].txirq,&isp[0]);
	ifx_int_wrapper.free(isp[0].rxirq,&isp[0]);
	ifx_int_wrapper.free(isp[0].errirq,&isp[0]);
/*
	ifx_int_wrapper.free(isp[0].frmirq, &isp[0]);
*/
errout:
	/* free up any allocated memory in the error case */
	kfree(isp);
	return (ret_val);
} /* ifx_ssc_init */


void
ifx_ssc_cleanup_module(void)
{
	int i;

	/* free up any allocated memory */
	for (i = 0; i < PORT_CNT; i++)
	{
		/* disable the SSC */
		WRITE_PERIPHERAL_REGISTER(IFX_SSC_WHBSTATE_CLR_ENABLE,isp[i].mapbase + IFX_SSC_WHBSTATE);
		/* free the interrupts */
		ifx_int_wrapper.free(isp[i].txirq, &isp[i]);
		ifx_int_wrapper.free(isp[i].rxirq, &isp[i]);
		ifx_int_wrapper.free(isp[i].errirq, &isp[i]);
/*
		ifx_int_wrapper.free(isp[i].frmirq, &isp[i]);

		if (isp[i].rxbuf != NULL)
			kfree(isp[i].rxbuf);
		if (isp[i].txbuf != NULL)
			kfree(isp[i].txbuf);
*/
	}
	kfree(isp);
	/* unregister the device */
	if (unregister_chrdev(maj, "ssc"))
	{
		printk("Unable to unregister major %d for the SSC\n", maj);
	}
	/* delete /proc read handler */
	remove_proc_entry("driver/ssc1", NULL);
	remove_proc_entry("driver/ssc2", NULL);
} /* ifx_ssc_cleanup_module */

module_exit(ifx_ssc_cleanup_module);

/* Module entry-points */
module_init(ifx_ssc_init);

#ifndef MODULE
static int __init
ifx_ssc_set_maj(char *str)
{
	maj = simple_strtol(str, NULL, 0);
	return 1;
}
__setup("ssc_maj=", ifx_ssc_set_maj);
#endif /* !MODULE */

#define AMAZON_SSC_EMSG(fmt,arg...) printk("%s: "fmt,__FUNCTION__, ##arg)
/* Brief:	chip select enable
 */
inline int amazon_ssc_cs_low(u32 pin)
{
	int ret=0;
	if ((ret=ifx_ssc_ioctl((struct inode *)0, NULL,IFX_SSC_GPO_OUT_CLR, (unsigned long)&pin))){
		AMAZON_SSC_EMSG("clear CS %d fails\n",pin);
	}
	wmb();
	return ret;
}
EXPORT_SYMBOL(amazon_ssc_cs_low);
/* Brief:	chip select disable
 */
inline int amazon_ssc_cs_high(u32 pin)
{
	int ret=0;
	if ((ret=ifx_ssc_ioctl((struct inode *)0, NULL,IFX_SSC_GPO_OUT_SET, (unsigned long)&pin))){
		AMAZON_SSC_EMSG("set CS %d fails\n", pin);
	}
	wmb();
	return ret;
}
EXPORT_SYMBOL(amazon_ssc_cs_high);
/* Brief:	one SSC session
 * Parameter:	
 *	tx_buf
 *	tx_len
 *	rx_buf
 *	rx_len
 *	session_mode: IFX_SSC_MODE_RXTX or IFX_SSC_MODE_TX
 * Return:	>=0 number of bytes received (if rx_buf != 0) or transmitted
 *		<0 error code
 * Description:
 *	0. copy data to internal buffer 
 *	1. Write command
 *	2a. If SSC_SESSION_MODE_TXONLY, read tx_len data
 *	2b. If not Read back (tx_len + rx_len) data
 *	3. copy internal buffer to rx buf if necessary
 */
static int ssc_session(char * tx_buf, u32 tx_len, char * rx_buf, u32 rx_len)
{
	int ret=0;

	char * ssc_tx_buf=NULL;
	char * ssc_rx_buf=NULL;

//	volatile char ssc_tx_buf[128]={0};
//	volatile char ssc_rx_buf[128]={0};

	int eff_size=0;
	u8 mode=0;
	
	if (tx_buf == NULL && tx_len ==0 && rx_buf == NULL && rx_len == 0){
		AMAZON_SSC_EMSG("invalid parameters\n");
		ret=-EINVAL;
		goto ssc_session_exit;
	}else if (tx_buf == NULL || tx_len == 0){
		if (rx_buf != NULL && rx_len != 0){
			mode = IFX_SSC_MODE_RX;
		}else{
			AMAZON_SSC_EMSG("invalid parameters\n");
			ret=-EINVAL;
			goto ssc_session_exit;
		}
	}else if (rx_buf == NULL || rx_len ==0){
		if (tx_buf != NULL && tx_len != 0){
			mode = IFX_SSC_MODE_TX;
		}else{
			AMAZON_SSC_EMSG("invalid parameters\n");
			ret=-EINVAL;
			goto ssc_session_exit;
		}
	}else{
		mode = IFX_SSC_MODE_RXTX;
	}
	
	if (mode == IFX_SSC_MODE_RXTX){
		eff_size = tx_len + rx_len;
	}else if (mode == IFX_SSC_MODE_RX){
		eff_size = rx_len;
	}else{
		eff_size = tx_len;
	}

	//4 bytes alignment,  required by driver
	/* change by TaiCheng */
	//if (in_irq()){
	if (1){
		ssc_tx_buf = (char*) kmalloc(sizeof(char) *  ((eff_size + 3) & (~3)), GFP_ATOMIC);
		ssc_rx_buf = (char*) kmalloc(sizeof(char) *  ((eff_size + 3) & (~3)), GFP_ATOMIC);
	}else{
		ssc_tx_buf = (char*) kmalloc(sizeof(char) *  ((eff_size + 3) & (~3)), GFP_KERNEL);
		ssc_rx_buf = (char*) kmalloc(sizeof(char) *  ((eff_size + 3) & (~3)), GFP_KERNEL);
	}
	if (ssc_tx_buf == NULL || ssc_rx_buf == NULL){
		AMAZON_SSC_EMSG("no memory for size of %d\n", eff_size);
		ret = -ENOMEM;
		goto ssc_session_exit;
	}
	memset((void*)ssc_tx_buf, 0, eff_size);
	memset((void*)ssc_rx_buf, 0, eff_size);

	if (tx_len>0){
		memcpy(ssc_tx_buf, tx_buf, tx_len);
	}
	
	ret=ifx_ssc_kwrite(0, ssc_tx_buf, eff_size);

	if (ret > 0) {
		ssc_tx_buf = NULL; //should be freed by ifx_ssc_kwrite
	}
		
	if (  ret != eff_size ){
		AMAZON_SSC_EMSG("ifx_ssc_write return %d\n",ret); 
		goto ssc_session_exit;
	}
	ret=ifx_ssc_kread(0, ssc_rx_buf,eff_size);
	if (  ret != eff_size ){
		AMAZON_SSC_EMSG("ifx_ssc_read return %d\n",ret); 
		goto ssc_session_exit;
	}
	
	memcpy(rx_buf, ssc_rx_buf+tx_len, rx_len);
	
	if (mode == IFX_SSC_MODE_TX) {
		ret = tx_len;
	}else{
		ret = rx_len;
	}
ssc_session_exit:

	if (ssc_tx_buf != NULL) kfree(ssc_tx_buf);
	if (ssc_rx_buf != NULL) kfree(ssc_rx_buf);

	if (ret<0) {
		printk("ssc session fails\n");
	}
	return ret;
}
/* Brief:	TX-RX session
 * Parameter:	
 *	tx_buf
 *	tx_len
 *	rx_buf
 *	rx_len
 * Return:	>=0 number of bytes received
 *		<0 error code
 * Description: 
 *	1. TX session
 *	2. RX session
 */
int amazon_ssc_txrx(char * tx_buf, u32 tx_len, char * rx_buf, u32 rx_len)
{
	return ssc_session(tx_buf,tx_len,rx_buf,rx_len);
}
EXPORT_SYMBOL(amazon_ssc_txrx);
/* Brief:	TX only session
 * Parameter:
 *	tx_buf
 *	tx_len
 * Return:	>=0 number of bytes transmitted
 *		<0 error code
 */
int amazon_ssc_tx(char * tx_buf, u32 tx_len)
{
	return ssc_session(tx_buf,tx_len,NULL,0);
}
EXPORT_SYMBOL(amazon_ssc_tx);
/* Brief:	RX only session
 * Parameter:
 *	rx_buf
 *	rx_len
 * Return:	>=0 number of bytes received
 *		<0 error code
 */
int amazon_ssc_rx(char * rx_buf, u32 rx_len)
{
	return ssc_session(NULL,0,rx_buf,rx_len);
}
EXPORT_SYMBOL(amazon_ssc_rx);