forked from demsey/stsci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stsci.c
1418 lines (1229 loc) · 34.6 KB
/
stsci.c
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
/*
* drivers/serial/stsci.c
* Open ST40 SmartCard Interface (SCI) driver
*/
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
#define STM23
#else
#define STM22
#endif
#include <linux/module.h>
#include <linux/init.h>
#ifdef STM22
#include "bitrev.h"
#include <linux/stpio.h>
#else
#include <linux/bitrev.h>
#include <linux/stm/pio.h>
#include <linux/stm/sysconf.h>
#endif
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/kfifo.h>
#include <asm/io.h>
#include <asm/clock.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>
#include "stsci.h"
#include "scif.h"
struct sci_port;
static void sci_socket_interrupt(struct stpio_pin *pin, void *dev);
static void scg_enable_clock( struct uart_port *port );
static void scg_disable_clock( struct uart_port *port );
static void syscfg_enable_scg_clock(struct uart_port *port);
static int sci_set_baud (struct uart_port *port, int baud);
static int sci_request_irq(struct uart_port *port);
static void sci_free_irq(struct uart_port *port);
static int sci_pios_init(struct uart_port *port);
static int sci_pios_free(struct uart_port *port);
static void sci_transmit_chars(struct uart_port *port);
static int sci_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg );
static int sci_ioctl_reset(struct uart_port *port);
#ifdef STM22
static inline void sci_enable(struct uart_port *);
#else
static inline void sci_enable(struct work_struct *);
#endif
static char *sci_hexdump(int m, unsigned char *buf, size_t n);
#define UART_TXD 0
#define UART_RXD 1
#define SC_EXTCLKIN 2
#define SC_CLK_OUT 3
#define UART_CTS 4
#define SC_COND_VCC 5
#define UART_DIR 6
#define SC_DETECT 7
#define to_sci_port(x) container_of((x), struct sci_port, port)
#define CONFIG_TIMEOUT 50 //Timeout for wait for uart configuration
#define RECEIVE_TIMEOUT 500 //Receive timeout (in ms) after receiving last char
struct sci_pio
{
const char* name;
unsigned char port;
unsigned char pin;
unsigned char dir;
struct stpio_pin* pio;
void (*handler)(struct stpio_pin *pin, void *dev);
};
struct cfg_bits
{
const unsigned int addr;
const unsigned long mask;
const unsigned int group;
const unsigned int reg;
#ifdef STM22
#else
struct sysconf_field* field;
#endif
};
#ifdef STM22
#else
struct sci_syscfg
{
struct cfg_bits autovcc;
struct cfg_bits scdetpol;
};
#endif
static struct sci_syscfg sci_syscfgs = {
.autovcc = {
.addr = SYS_CFG7,
.mask = SC_COND_VCC_EN,
.group = SYS_CFG,
.reg = 7,
},
.scdetpol = {
.addr = SYS_CFG7,
.mask = SC_DETECT_POL,
.group = SYS_CFG,
.reg = 7,
},
};
struct sci_port
{
struct uart_port port;
struct sci_pio* pios; /* PIO pin settings for driver */
unsigned char pios_nr;
int break_flag;
int dma_enabled;
unsigned int baud;
unsigned int ctrl;
struct cfg_bits scgclkval;
struct cfg_bits scgclken;
struct cfg_bits clksrc;
#ifdef STM22
struct work_struct work;
#else
struct delayed_work work;
#endif
wait_queue_head_t initwq;
struct kfifo* sendq;
spinlock_t* sendq_lock;
struct kfifo* receq;
spinlock_t* receq_lock;
struct kfifo* flagq;
unsigned char configured;
unsigned char iscard;
unsigned char isatr;
unsigned char ts;
unsigned char atr_timeout_cnt;
};
static struct sci_pio sci_pios0[] = {
[0] = {
.name = "UART0_TxD",
.port = 0,
.pin = 0,
.dir = STPIO_ALT_BIDIR,
},
[1] = {
.name = "UART0_RxD",
.port = 0,
.pin = 1,
.dir = STPIO_IN,
},
[3] = {
.name = "SC_CLK_OUT",
.port = 0,
.pin = 3,
.dir = STPIO_ALT_OUT,
},
[4] = {
.name = "UART0_CTS",
.port = 0,
.pin = 4,
.dir = STPIO_OUT,
},
[5] = {
.name = "SC_COND_VCC",
.port = 0,
.pin = 5,
.dir = STPIO_OUT,
},
[7] = {
.name = "SC_DETECT",
.port = 0,
.pin = 7,
.dir = STPIO_IN,
.handler= sci_socket_interrupt,
},
};
static struct sci_pio sci_pios1[] = {
[0] = {
.name = "UART0_TxD",
.port = 1,
.pin = 0,
.dir = STPIO_ALT_BIDIR,
},
[1] = {
.name = "UART0_RxD",
.port = 1,
.pin = 1,
.dir = STPIO_IN,
},
[3] = {
.name = "SC_CLK_OUT",
.port = 1,
.pin = 3,
.dir = STPIO_ALT_OUT,
},
[4] = {
.name = "UART0_CTS",
.port = 1,
.pin = 4,
.dir = STPIO_OUT,
},
[5] = {
.name = "SC_COND_VCC",
.port = 1,
.pin = 5,
.dir = STPIO_OUT,
},
[7] = {
.name = "SC_DETECT",
.port = 1,
.pin = 7,
.dir = STPIO_IN,
.handler= sci_socket_interrupt,
},
};
/*---- Inline function definitions ---------------------------*/
/* Some simple utility functions to enable and disable interrupts.
* Note that these need to be called with interrupts disabled.
*/
static inline void sci_disable_tx_interrupts(struct uart_port *port)
{
unsigned long intenable;
/* Clear TE (Transmitter empty) interrupt enable in INTEN */
intenable = asc_in(port, INTEN);
// intenable &= ~ASC_INTEN_THE;
intenable &= ~ASC_INTEN_TE;
asc_out(port, INTEN, intenable);
}
static inline void sci_enable_tx_interrupts(struct uart_port *port)
{
unsigned long intenable;
/* Set TE (Transmitter empty) interrupt enable in INTEN */
intenable = asc_in(port, INTEN);
// intenable |= ASC_INTEN_THE;
intenable |= ASC_INTEN_TE;
asc_out(port, INTEN, intenable);
}
static inline void sci_disable_rx_interrupts(struct uart_port *port)
{
unsigned long intenable;
/* Clear RBE (Receive Buffer Full Interrupt Enable) bit in INTEN */
intenable = asc_in(port, INTEN);
intenable &= ~ASC_INTEN_RBE;
// intenable &= ~ASC_INTEN_RHF;
asc_out(port, INTEN, intenable);
}
static inline void sci_enable_rx_interrupts(struct uart_port *port)
{
unsigned long intenable;
/* Set RBE (Receive Buffer Full Interrupt Enable) bit in INTEN */
intenable = asc_in(port, INTEN);
intenable |= ASC_INTEN_RBE;
// intenable |= ASC_INTEN_RHF;
asc_out(port, INTEN, intenable);
}
/*----------------------------------------------------------------------*/
/*
* UART Functions
*/
static unsigned int sci_tx_empty(struct uart_port *port)
{
unsigned long status;
status = asc_in(port, STA);
if (status & ASC_STA_TE)
return TIOCSER_TEMT;
return 0;
}
static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
/* This routine is used for seting signals of: DTR, DCD, CTS/RTS
* We use ASC's hardware for CTS/RTS, so don't need any for that.
* Some boards have DTR and DCD implemented using PIO pins,
* code to do this should be hooked in here.
*/
}
static unsigned int sci_get_mctrl(struct uart_port *port)
{
/* This routine is used for geting signals of: DTR, DCD, DSR, RI,
and CTS/RTS */
return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
}
/*
* There are probably characters waiting to be transmitted.
* Start doing so.
* The port lock is held and interrupts are disabled.
*/
static void sci_start_tx(struct uart_port *port)
{
printk(KERN_INFO "STSCI start_tx.\n");
sci_transmit_chars(port);
}
/*
* Transmit stop - interrupts disabled on entry
*/
static void sci_stop_tx(struct uart_port *port)
{
sci_disable_tx_interrupts(port);
printk(KERN_INFO "STSCI stop_tx.\n");
}
/*
* Receive stop - interrupts still enabled on entry
*/
static void sci_stop_rx(struct uart_port *port)
{
scg_disable_clock(port);
sci_disable_rx_interrupts(port);
printk(KERN_INFO "STSCI stop_rx.\n");
}
/*
* Force modem status interrupts on - no-op for us
*/
static void sci_enable_ms(struct uart_port *port)
{
/* Nothing here yet .. */
}
/*
* Handle breaks - ignored by us
*/
static void sci_break_ctl(struct uart_port *port, int break_state)
{
/* Nothing here yet .. */
}
#ifdef STM22
static void sci_reset(struct uart_port *port)
{
#else
static void sci_reset(struct work_struct *work)
{
struct sci_port *sciport = container_of(work, struct sci_port, work.work);
struct uart_port *port = &sciport->port;
#endif
printk(KERN_DEBUG "STSCI[%d] reset\n", port->line);
sci_ioctl_reset(port);
scg_enable_clock(port);
}
/*
* Enable port for reception.
* port_sem held and interrupts disabled
*/
static int sci_startup(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
printk(KERN_INFO "STSCI startup.\n");
// sci_pios_init(port);
// sci_request_irq(port);
// sci_transmit_chars(port);
asc_out(port, CTL, asc_in(port,CTL) & ~ASC_CTL_RUN);
asc_out(port, CTL, asc_in(port,CTL) & ~ASC_CTL_RXENABLE);
asc_out(port, INTEN, asc_in(port,INTEN) & ~ASC_INTEN_TOI);
sci_enable_rx_interrupts(port);
spin_lock_init(sciport->sendq_lock);
sciport->sendq = kfifo_alloc(256, GFP_KERNEL, sciport->sendq_lock);
spin_lock_init(sciport->receq_lock);
sciport->receq = kfifo_alloc(256, GFP_KERNEL, sciport->receq_lock);
sciport->flagq = kfifo_alloc(256, GFP_KERNEL, sciport->receq_lock);
sciport->configured = 0;
sciport->isatr = 0;
sciport->ts = 0;
syscfg_enable_scg_clock(port);
cancel_delayed_work(&sciport->work);
#ifdef STM22
INIT_WORK(&sciport->work, (void (*)(void *))sci_reset, port);
#else
INIT_DELAYED_WORK(&sciport->work, (work_func_t)sci_reset);
#endif
schedule_delayed_work(&sciport->work,msecs_to_jiffies(CONFIG_TIMEOUT));
wake_up_interruptible(&sciport->initwq);
return 0;
}
static void sci_shutdown(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
asc_out (port, CTL, asc_in(port, CTL) & ~ASC_CTL_RUN);
sci_disable_tx_interrupts(port);
asc_out(port, INTEN, asc_in(port,INTEN) & ~ASC_INTEN_TOI);
sci_disable_rx_interrupts(port);
scg_disable_clock(port);
// sci_pios_free(port);
// sci_free_irq(port);
sciport->configured = 0;
kfifo_free(sciport->sendq);
kfifo_free(sciport->receq);
kfifo_free(sciport->flagq);
printk(KERN_INFO "STSCI shutdown.\n");
}
static void sci_release_port(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
release_mem_region(port->mapbase, 0x100+1);
iounmap(port->membase);
#ifdef STM22
#else
if (sciport->clksrc.field)
sysconf_release(sciport->clksrc.field);
#endif
}
#ifdef STM22
#else
static void sci_configure_field(struct cfg_bits* f )
{
int lsb = ffs(f->mask)-1;
int msb = 32-ffs(bitrev32(f->mask));
f->field = sysconf_claim(f->group,f->reg,lsb,msb,"stsci");
}
#endif
static int sci_request_port(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
if (!request_mem_region(port->mapbase, 0x100+1, "stsci"))
return -EBUSY;
#ifdef STM22
#else
sci_configure_field(&sciport->clksrc);
#endif
return 0;
}
/* Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set */
/* Set type field if successful */
static void sci_config_port(struct uart_port *port, int flags)
{
struct sci_port *sciport = to_sci_port(port);
sci_request_port(port);
if (!port->membase)
port->membase = ioremap(port->mapbase, 0x100+1);
if (!port->membase)
return;
sciport->configured = 0;
syscfg_enable_scg_clock(port);
init_waitqueue_head(&sciport->initwq);
sci_pios_init(port);
sci_request_irq(port);
port->type = PORT_SCI;
port->fifosize = FIFO_SIZE;
printk(KERN_INFO "STSCI config.\n");
}
#ifdef STM22
static void sci_set_termios(struct uart_port *port,
struct termios *termios, struct termios *old)
#else
static void sci_set_termios(struct uart_port *port,
struct ktermios *termios, struct ktermios *old)
#endif
{
struct sci_port *sciport = to_sci_port(port);
unsigned int ctrl_val;
unsigned long flags;
unsigned int baud;
printk(KERN_INFO "STSCI set_termios.\n");
spin_lock_irqsave(&port->lock, flags);
baud = uart_get_baud_rate(port, termios, old, 0,
port->uartclk/16);
/* wait for end of current transmission */
// while (!sci_tx_empty(port)){};
/* read control register */
ctrl_val = asc_in (port, CTL);
asc_out (port, CTL, ctrl_val & ~ASC_CTL_RUN);
/* reset fifo rx & tx */
asc_out (port, TXRESET, 1);
asc_out (port, RXRESET, 1);
/* set default control configuration */
ctrl_val = sciport->ctrl;
/* set speed and baud generator mode */
ctrl_val |= sci_set_baud (port, sciport->baud);
uart_update_timeout(port, termios->c_cflag, baud);
asc_out (port, GUARDTIME, 2); //sci
asc_out (port, TIMEOUT, 254); //sci
asc_out (port, RETRIES, 3); //sci
/* write final value and enable port */
asc_out (port, CTL, ctrl_val);
sciport->configured = 1;
wake_up_interruptible(&sciport->initwq);
spin_unlock_irqrestore(&port->lock, flags);
}
static const char *sci_type(struct uart_port *port)
{
return "sci";
}
static int
sci_verify_port(struct uart_port *port, struct serial_struct *ser)
{
/* No user changeable parameters */
return -EINVAL;
}
/*----------------------------------------------------------------------*/
static struct uart_ops sci_uart_ops = {
.tx_empty = sci_tx_empty,
.set_mctrl = sci_set_mctrl,
.get_mctrl = sci_get_mctrl,
.start_tx = sci_start_tx,
.stop_tx = sci_stop_tx,
.stop_rx = sci_stop_rx,
.enable_ms = sci_enable_ms,
.break_ctl = sci_break_ctl,
.startup = sci_startup,
.shutdown = sci_shutdown,
.set_termios = sci_set_termios,
.type = sci_type,
.release_port = sci_release_port,
.request_port = sci_request_port,
.config_port = sci_config_port,
.verify_port = sci_verify_port,
.ioctl = sci_ioctl,
};
struct sci_port sci_ports[SCI_NPORTS] = {
/* UART0 */
{
.port = {
.mapbase = 0x18030000,
.iotype = SERIAL_IO_MEM,
.irq = 123,
.ops = &sci_uart_ops,
.flags = ASYNC_BOOT_AUTOCONF,
.fifosize = FIFO_SIZE,
.line = 0,
},
.pios = sci_pios0,
.pios_nr = ARRAY_SIZE(sci_pios0),
.baud = 9600,
.ctrl = (ASC_CTL_MODE_8BIT_PAR |
ASC_CTL_STOP_1_HALFBIT |
ASC_CTL_PARITYODD |
ASC_CTL_SCENABLE |
ASC_CTL_RXENABLE |
ASC_CTL_FIFOENABLE |
ASC_CTL_RUN),
.scgclkval = {
.addr = SCG0 + 0x00, /* SCI clock 0 divider value */
.mask = SCI_CLK_VAL_MASK,
},
.scgclken = {
.addr = SCG0 + 0x04, /* SCI clock 0 control register */
.mask = SCI_CLK_CTRL_EN,
},
.clksrc = {
.addr = SYS_CFG7,
.mask = PIO0_SCCLK_NOT_CLK_DSS,
.group = SYS_CFG,
.reg = 7,
},
},
/* UART1 */
{
.port = {
.mapbase = 0x18031000,
.iotype = SERIAL_IO_MEM,
.irq = 122,
.ops = &sci_uart_ops,
.flags = ASYNC_BOOT_AUTOCONF,
.fifosize = FIFO_SIZE,
.line = 1,
},
.pios = sci_pios1,
.pios_nr = ARRAY_SIZE(sci_pios1),
.baud = 9600,
.ctrl = (ASC_CTL_MODE_8BIT_PAR |
ASC_CTL_STOP_1_HALFBIT |
ASC_CTL_PARITYODD |
ASC_CTL_SCENABLE |
ASC_CTL_RXENABLE |
ASC_CTL_FIFOENABLE |
ASC_CTL_RUN),
.scgclkval = {
.addr = SCG1 + 0x00, /* SCI clock 1 divider value */
.mask = SCI_CLK_VAL_MASK,
},
.scgclken = {
.addr = SCG1 + 0x04, /* SCI clock 1 control register */
.mask = SCI_CLK_CTRL_EN,
},
.clksrc = {
.addr = SYS_CFG7, /* COMMs configuration register */
.mask = PIO1_SCCLK_NOT_CLK_DSS,
.group = SYS_CFG,
.reg = 7,
},
},
};
static struct uart_driver sci_uart_driver = {
.owner = THIS_MODULE,
.driver_name = "sci",
// .devfs_name = "sci/",
.dev_name = "sci",
.major = SCI_MAJOR,
.minor = SCI_MINOR_START,
.nr = SCI_NPORTS,
};
static void sci_flush_tty(struct uart_port *port)
{
struct tty_struct *tty = port->info->tty;
tty_flip_buffer_push(tty);
}
static void do_atr(void *private)
{
struct sci_port *sciport = to_sci_port(private);
struct uart_port *port = private;
unsigned char *rb, *fb;
int l = kfifo_len(sciport->receq);
tty_prepare_flip_string_flags(port->info->tty, &rb, (char **)&fb, l);
kfifo_get(sciport->receq, rb, l);
kfifo_get(sciport->flagq, fb, l);
printk(KERN_NOTICE "ATR: %s (%d)\n", sci_hexdump(1,rb,l), l );
sciport->isatr = 1;
sci_flush_tty(port);
}
static inline void sci_disable(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
// struct sci_pio *pio = &(sciport->pios[SC_COND_VCC]);
// stpio_set_pin(pio->pio, 0); //FIXME
scg_disable_clock(port);
cancel_delayed_work(&sciport->work);
}
#ifdef STM22
static inline void sci_enable(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
#else
static inline void sci_enable(struct work_struct *work)
{
struct sci_port *sciport = container_of(work, struct sci_port, work.work);
struct uart_port *port = &sciport->port;
#endif
// struct sci_pio *pio = &(sciport->pios[SC_COND_VCC]);
// stpio_set_pin(pio->pio, 1); //FIXME
if (!sciport->configured)
return;
/* wait for the configure uart */
// wait_event_interruptible_timeout(sciport->initwq,
// (sciport->configured), msecs_to_jiffies(CONFIG_TIMEOUT));
printk(KERN_DEBUG "enable: configured: %d\n",sciport->configured);
scg_enable_clock(port);
}
static void sci_socket_interrupt(struct stpio_pin *pin, void *dev)
{
struct uart_port *port = dev;
struct sci_port *sciport = to_sci_port(dev);
struct sci_pio *pio = &(sciport->pios[SC_COND_VCC]);
sciport->iscard = stpio_get_pin(pin);
sciport->isatr = 0;
sciport->ts = 0;
stpio_disable_irq(pin);
stpio_enable_irq(pin, sciport->iscard);
if ( sciport->iscard == IRQ_TEST_LEVEL ){
printk(KERN_DEBUG "Card[%d] removed\n", port->line);
sci_disable(port);
stpio_set_pin(pio->pio, 1);
} else {
printk(KERN_DEBUG "Card[%d] inserted\n", port->line);
stpio_set_pin(pio->pio, 0);
#ifdef STM22
INIT_WORK(&sciport->work, (void (*)(void *))sci_enable, port);
#else
INIT_DELAYED_WORK(&sciport->work, (work_func_t)sci_enable);
#endif
schedule_delayed_work(&sciport->work, 0);
}
}
static inline unsigned sci_hw_txroom(struct uart_port* port)
{
unsigned long status;
status = asc_in(port, STA);
if (status & ASC_STA_THE) {
return FIFO_SIZE/2;
} else if (! (status & ASC_STA_TF)) {
return 1;
} else {
return 0;
}
}
/*
* Start transmitting chars.
* This is called from both interrupt and task level.
* Either way interrupts are disabled.
*/
static void sci_transmit_chars(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
struct circ_buf *xmit = &port->info->xmit;
int txroom;
unsigned char c;
unsigned char ol;
txroom = sci_hw_txroom(port);
if ((txroom != 0) && port->x_char) {
c = port->x_char;
port->x_char = 0;
asc_out (port, TXBUF, c);
port->icount.tx++;
printk(KERN_DEBUG "Transmitted 0x%02x\n", (unsigned char)c);
txroom = sci_hw_txroom(port);
}
while (txroom > 0) {
if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
break;
}
do {
ol = c = xmit->buf[xmit->tail];
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
if (sciport->ts != TS_DIRECT)
c = bitrev8((unsigned char)c)^0xff;
// printk(KERN_DEBUG "Transmitted 0x%02x (0x%02x), status: 0x%03x\n", (unsigned char)c, (unsigned char)ol, (unsigned char)asc_in(port, STA) );
asc_out (port, TXBUF, c);
kfifo_put(sciport->sendq, (unsigned char *)&ol, sizeof(ol));
port->icount.tx++;
txroom--;
} while ((txroom > 0) && (!uart_circ_empty(xmit)));
txroom = sci_hw_txroom(port);
}
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
uart_write_wakeup(port);
}
if (port->x_char || (!uart_circ_empty(xmit))) {
sci_enable_tx_interrupts(port);
} else {
int l = kfifo_len(sciport->sendq);
unsigned char sb[l];
kfifo_get(sciport->sendq, sb, l); //fill sb buffer
kfifo_put(sciport->sendq, sb, l); //put back sb buffer to fifo
printk(KERN_INFO "APDU: %s (%d)\n",sci_hexdump(1,sb,l),l);
sci_disable_tx_interrupts(port);
}
}
#ifdef STM22
static inline void sci_receive_chars(struct uart_port *port,
struct pt_regs *regs)
#else
static inline void sci_receive_chars(struct uart_port *port)
#endif
{
int count;
struct tty_struct *tty = port->info->tty;
struct sci_port *sciport = to_sci_port(port);
int copied=0;
volatile unsigned long status;
unsigned long c = 0;
unsigned long b;
char flag;
int overrun;
while (1) {
status = asc_in(port, STA);
if (status & ASC_STA_RHF) {
count = FIFO_SIZE / 2;
} else if (status & ASC_STA_RBF) {
count = 1;
} else {
count = 0;
}
/* Check for overrun before reading any data from the
* RX FIFO, as this clears the overflow error condition. */
overrun = status & ASC_STA_OE;
/* Don't copy more bytes than there are room for in the buffer */
count = tty_buffer_request_room(tty, count);
/* If for any reason we can't copy more data, we're done! */
if (count == 0)
break;
for ( ; count != 0; count--) {
status = asc_in(port, STA);
c = asc_in(port, RXBUF);
flag = TTY_NORMAL;
port->icount.rx++;
if (unlikely(!sciport->ts)){
unsigned long ctl = asc_in(port, CTL);
if ( c & ASC_RXBUF_PE){
asc_out(port, CTL, ctl ^ ASC_CTL_PARITYODD);
break;
}
printk(KERN_NOTICE "Parity %s\n", ctl & ASC_CTL_PARITYODD?"ODD":"EVEN");
sciport->ts = (c & 0xff)==TS_DIRECT?(c & 0xff):bitrev8(c)^0xff;
printk(KERN_NOTICE "Convention TS 0x%02x\n", sciport->ts);
}
sciport->atr_timeout_cnt = unlikely(!sciport->isatr)?15:2;
if ( ~(asc_in(port,INTEN) & ASC_INTEN_TOI) )
asc_out(port, INTEN, asc_in(port,INTEN) | ASC_INTEN_TOI);
b = (sciport->ts == TS_DIRECT)?c:bitrev8((unsigned char)c)^0xff;
// printk(KERN_DEBUG "Recieved 0x%02x (0x%02x) count: %d status: 0x%03x\n", (unsigned char)c, (unsigned char)b, count, (unsigned int)status);
if (unlikely(c & ASC_RXBUF_FE)) {
if (c == ASC_RXBUF_FE) {
port->icount.brk++;
if (uart_handle_break(port))
continue;
flag = TTY_BREAK;
} else {
port->icount.frame++;
flag = TTY_FRAME;
}
} else if (unlikely(c & ASC_RXBUF_PE)) {
port->icount.parity++;
flag = TTY_PARITY;
}
#ifdef STM22
if (uart_handle_sysrq_char(port, b, regs))
#else
if (uart_handle_sysrq_char(port, b))
#endif
continue;
// tty_insert_flip_char(tty, b & 0xff, flag);
kfifo_put(sciport->receq, (unsigned char *)&b, 1);
kfifo_put(sciport->flagq, (unsigned char *)&flag, 1);
}
if (overrun) {
unsigned char ov[] = {0,TTY_OVERRUN};
port->icount.overrun++;
// tty_insert_flip_char(tty, 0, TTY_OVERRUN);
kfifo_put(sciport->receq, ov+0, 1);
kfifo_put(sciport->flagq, ov+1, 1);
}
copied=1;
}
if (copied ) {
/* Tell the rest of the system the news. New characters! */
// sci_flush_tty(port);
}
}
static inline void sci_timeout(struct uart_port *port)
{
struct sci_port *sciport = to_sci_port(port);
// printk(KERN_DEBUG "Receive soft-timeout nr %d\n", (unsigned int)sciport->atr_timeout_cnt);
asc_out (port, TIMEOUT, 254); //sci
if (--sciport->atr_timeout_cnt == 0 ){
asc_out (port, TIMEOUT, 254);
asc_out(port, INTEN, asc_in(port,INTEN) & ~ASC_INTEN_TOI);
if (!sciport->isatr){
do_atr(port);
} else {
unsigned char *rb, *fb;
int l = kfifo_len(sciport->sendq);
unsigned char db[l]; //drop buffer
kfifo_get(sciport->sendq, db, l); //drop send fifo
kfifo_get(sciport->receq, db, l); //drop echoed part of receive fifo
kfifo_get(sciport->flagq, db, l); //drop echoed part of receive fifo
l = kfifo_len(sciport->receq);
tty_prepare_flip_string_flags(port->info->tty, &rb, (char **)&fb, l);
kfifo_get(sciport->receq, rb, l);
kfifo_get(sciport->flagq, fb, l);
printk(KERN_INFO "SW: %s (%d)\n", sci_hexdump(1,rb,l), l );
// printk(KERN_INFO "SW: %s (%d)\n", sci_hexdump(1,fb,l), l );
sci_flush_tty(port);
}
// wake_up_interruptible(&sciport->initwq);
return;
}
}
#ifdef STM22
static irqreturn_t sci_interrupt(int irq, void *ptr, struct pt_regs *regs)
#else
static irqreturn_t sci_interrupt(int irq, void *ptr)
#endif
{
struct uart_port *port = ptr;
// struct sci_port *sciport = to_sci_port(port);
unsigned long status;
unsigned long inten;
spin_lock(&port->lock);
status = asc_in (port, STA);
inten = asc_in(port, INTEN);
// if ((status & ASC_STA_TNE) && (inten & ASC_INTEN_TNE) ||
// (status & ASC_STA_TOI) && (inten & ASC_INTEN_TOI) ){
if ( status & ASC_STA_TOI ){
sci_timeout(port);
}
// if (status & ASC_STA_RBF) {
if (status & (ASC_STA_RBF | ASC_STA_RHF)) {
/* Receive FIFO not empty */
#ifdef STM22
sci_receive_chars(port, regs);
#else
sci_receive_chars(port);
#endif
// if (!asc_in(port, INTEN) | (ASC_INTEN_TNE | ASC_INTEN_TOI))
// if (!sciport->isatr){
// printk(KERN_DEBUG "Enable timeout interrupt\n");
// asc_out(port, INTEN, asc_in(port, INTEN) | (ASC_INTEN_TNE | ASC_INTEN_TOI) );
// }
}