/
ma35d1_serial.c
1416 lines (1115 loc) · 34.7 KB
/
ma35d1_serial.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
/*
* linux/drivers/tty/serial/ma35d1_serial.c
*
* MA35D1 serial driver
*
*
* Copyright (C) 2018 Nuvoton Technology Corp.
*
* 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.
*
*/
#if defined(CONFIG_SERIAL_MA35D1_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/clk.h>
#include <linux/serial_reg.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/nmi.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/serial.h>
#include <linux/platform_data/dma-ma35d1.h>
#include "ma35d1_serial.h"
#define UART_NR 17
#define UART_RX_BUF_SIZE 4096 //bytes
#define UART_TX_MAX_BUF_SIZE 128 //bytes
// PDMA mode time-out
#define Time_Out_Frame_Count 2
#define Time_Out_Low_Baudrate 115200
unsigned char UART_PDMA_TX_ID[UART_NR] = {
PDMA_UART0_TX , PDMA_UART1_TX , PDMA_UART2_TX ,
PDMA_UART3_TX , PDMA_UART4_TX , PDMA_UART5_TX ,
PDMA_UART6_TX , PDMA_UART7_TX , PDMA_UART8_TX ,
PDMA_UART9_TX , PDMA_UART10_TX, PDMA_UART11_TX,
PDMA_UART12_TX, PDMA_UART13_TX, PDMA_UART14_TX,
PDMA_UART15_TX, PDMA_UART16_TX
};
unsigned char UART_PDMA_RX_ID[UART_NR] = {
PDMA_UART0_RX , PDMA_UART1_RX , PDMA_UART2_RX ,
PDMA_UART3_RX , PDMA_UART4_RX , PDMA_UART5_RX ,
PDMA_UART6_RX , PDMA_UART7_RX , PDMA_UART8_RX ,
PDMA_UART9_RX , PDMA_UART10_RX, PDMA_UART11_RX,
PDMA_UART12_RX, PDMA_UART13_RX, PDMA_UART14_RX,
PDMA_UART15_RX, PDMA_UART16_RX
};
static struct uart_driver ma35d1serial_reg;
struct clk *clk;
struct uart_ma35d1_port {
struct uart_port port;
unsigned short capabilities; /* port capabilities */
unsigned char ier;
unsigned char lcr;
unsigned char mcr;
unsigned char mcr_mask; /* mask of user bits */
unsigned char mcr_force; /* mask of forced bits */
struct serial_rs485 rs485; /* rs485 settings */
struct ma35d1_ip_rx_dma dma_rx;
struct ma35d1_ip_tx_dma dma_tx;
struct ma35d1_mem_alloc src_mem_p;
struct ma35d1_mem_alloc dest_mem_p;
struct ma35d1_dma_done dma_slave_done;
unsigned char PDMA_UARTx_TX;
unsigned char PDMA_UARTx_RX;
struct ma35d1_dma_done dma_Rx_done;
struct ma35d1_dma_done dma_Tx_done;
u64 pdma_rx_vir_addr1;
u64 pdma_rx_vir_addr2;
u64 pdma_rx_phy_addr1;
u64 pdma_rx_phy_addr2;
unsigned int tx_dma_len;
unsigned char uart_pdma_enable_flag;
unsigned char Tx_pdma_busy_flag;
unsigned int pdma_time_out_prescaler;
unsigned int pdma_time_out_count;
unsigned int baud_rate;
unsigned int console_baud_rate;
unsigned int console_line;
unsigned int console_int;
/*
* We provide a per-port pm hook.
*/
void (*pm)(struct uart_port *port, unsigned int state, unsigned int old);
};
static struct uart_ma35d1_port ma35d1serial_ports[UART_NR]={0};
static inline void __stop_tx(struct uart_ma35d1_port *p);
static void ma35d1_prepare_RX_dma(struct uart_ma35d1_port *p);
static void ma35d1_prepare_TX_dma(struct uart_ma35d1_port *p);
static inline struct uart_ma35d1_port *
to_ma35d1_uart_port(struct uart_port *uart)
{
return container_of(uart, struct uart_ma35d1_port, port);
}
static inline unsigned int serial_in(struct uart_ma35d1_port *p, int offset)
{
return(__raw_readl(p->port.membase + offset));
}
static inline void serial_out(struct uart_ma35d1_port *p, int offset, int value)
{
__raw_writel(value, p->port.membase + offset);
}
static void ma35d1_Rx_dma_callback(void *arg)
{
struct ma35d1_dma_done *done = arg;
struct uart_ma35d1_port *p = (struct uart_ma35d1_port *)done->callback_param;
struct ma35d1_ip_rx_dma *pdma_rx = &(p->dma_rx);
struct tty_port *tty_port = &p->port.state->port;
int count;
int copied_count = 0;
if(done->timeout==1)
count = ((p->dest_mem_p.size) - (done->remain +1));
else
count = (p->dest_mem_p.size);
spin_lock(&p->port.lock);
if(p->pdma_rx_phy_addr2 == p->dest_mem_p.phy_addr) {
p->dest_mem_p.phy_addr = p->pdma_rx_phy_addr1;
p->dest_mem_p.vir_addr = p->pdma_rx_vir_addr1;
ma35d1_prepare_RX_dma(p);
//Trigger Rx dma again
serial_out(p, UART_REG_IER, (serial_in(p, UART_REG_IER)|RXPDMAEN));
dma_sync_single_for_cpu(pdma_rx->chan_rx->device->dev, p->pdma_rx_phy_addr2, UART_RX_BUF_SIZE, DMA_FROM_DEVICE);
copied_count = tty_insert_flip_string(tty_port, ((unsigned char *)p->pdma_rx_vir_addr2), count);
}else {
p->dest_mem_p.phy_addr = p->pdma_rx_phy_addr2;
p->dest_mem_p.vir_addr = p->pdma_rx_vir_addr2;
ma35d1_prepare_RX_dma(p);
//Trigger Rx dma again
serial_out(p, UART_REG_IER, (serial_in(p, UART_REG_IER)|RXPDMAEN));
dma_sync_single_for_cpu(pdma_rx->chan_rx->device->dev, p->pdma_rx_phy_addr1, UART_RX_BUF_SIZE, DMA_FROM_DEVICE);
copied_count = tty_insert_flip_string(tty_port, ((unsigned char *)p->pdma_rx_vir_addr1), count);
}
if(copied_count != count) {
dev_err(p->port.dev, "Rx overrun: dropping %d bytes\n", (count - copied_count));
}
p->port.icount.rx +=copied_count;
tty_flip_buffer_push(tty_port);
spin_unlock(&p->port.lock);
return;
}
static void ma35d1_Tx_dma_callback(void *arg)
{
struct ma35d1_dma_done *done = arg;
struct uart_ma35d1_port *p = (struct uart_ma35d1_port *)done->callback_param;
struct circ_buf *xmit = &p->port.state->xmit;
spin_lock(&p->port.lock);
p->port.icount.tx += p->tx_dma_len;
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(&p->port);
if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
p->Tx_pdma_busy_flag = 1;
ma35d1_prepare_TX_dma(p);
// Trigger Tx dma again
serial_out(p, UART_REG_IER, (serial_in(p, UART_REG_IER)| TXPDMAEN));
} else {
p->Tx_pdma_busy_flag = 0;
}
spin_unlock(&p->port.lock);
}
static void set_pdma_flag(struct uart_ma35d1_port *p, int id)
{
p->uart_pdma_enable_flag = 1;
p->PDMA_UARTx_RX = UART_PDMA_RX_ID[id];
p->PDMA_UARTx_TX = UART_PDMA_TX_ID[id];
}
void ma35d1_uart_cal_pdma_time_out(struct uart_ma35d1_port *p, unsigned int baud)
{
unsigned int lcr;
unsigned int pdma_time_out_base = 300000000 * Time_Out_Frame_Count / 256; // 300M*Time_Out_Frame_Count/256
unsigned int time_out_prescaler = 0;
unsigned int bit_length;
unsigned int time_out;
if(baud > Time_Out_Low_Baudrate){
p->pdma_time_out_count = 255 * 16;
p->pdma_time_out_prescaler = 7 * 16;
return;
}
bit_length = 2;//1 start + 1 stop bit
lcr = serial_in(p, UART_REG_LCR);
switch(lcr & 0x3){
case 0:
bit_length += 5;
break;
case 1:
bit_length += 6;
break;
case 2:
bit_length += 7;
break;
case 3:
bit_length += 8;
break;
}
if(lcr & 0x4)
bit_length += 1;
if(lcr & 0x8)//Parity bit
bit_length += 1;
time_out = pdma_time_out_base * bit_length;
time_out = (time_out / baud) + 1;
time_out = time_out * 16;
while(time_out > 65535) // pdma max. time-out count is 65535
{
time_out = time_out / 2;
time_out_prescaler++;
}
if(time_out == 0) time_out = 1;
p->pdma_time_out_count = time_out;
p->pdma_time_out_prescaler = time_out_prescaler;
return;
}
static void ma35d1_prepare_RX_dma(struct uart_ma35d1_port *p)
{
struct ma35d1_dma_config;
struct ma35d1_ip_rx_dma *pdma_rx = &(p->dma_rx);
struct ma35d1_dma_data data;
int ret;
int sg_loop, sg_size, sg_addr;
dma_cookie_t cookie;
serial_out(p, UART_REG_IER, (serial_in(p, UART_REG_IER)&~ RXPDMAEN));
if(p->dest_mem_p.size == 0) {
p->dest_mem_p.size = UART_RX_BUF_SIZE;
p->dest_mem_p.vir_addr = (u64)(kmalloc((UART_RX_BUF_SIZE * 2), GFP_KERNEL));
p->pdma_rx_vir_addr1 = p->dest_mem_p.vir_addr;
p->dest_mem_p.phy_addr = dma_map_single(pdma_rx->chan_rx->device->dev, (void *)p->dest_mem_p.vir_addr, (UART_RX_BUF_SIZE * 2), DMA_FROM_DEVICE);
ret = dma_mapping_error(pdma_rx->chan_rx->device->dev, p->dest_mem_p.phy_addr);
if (ret)
dev_err(p->port.dev, "dest mapping error.\n");
p->pdma_rx_phy_addr1 = p->dest_mem_p.phy_addr;
p->pdma_rx_vir_addr2 = p->pdma_rx_vir_addr1 + UART_RX_BUF_SIZE;
p->pdma_rx_phy_addr2 = p->pdma_rx_phy_addr1 + UART_RX_BUF_SIZE;
}
pdma_rx->slave_config.src_addr = (unsigned int)(p->port.iobase);
pdma_rx->slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
pdma_rx->slave_config.src_maxburst = 1;
pdma_rx->slave_config.direction = DMA_DEV_TO_MEM;
pdma_rx->slave_config.device_fc = false;
pdma_rx->slave_config.slave_id = p->PDMA_UARTx_RX;
data.timeout_prescaler = p->pdma_time_out_prescaler;
data.timeout_counter = p->pdma_time_out_count;
pdma_rx->chan_rx->private = (void *)&data;
dmaengine_slave_config(pdma_rx->chan_rx,&(pdma_rx->slave_config));
sg_loop = 1;
sg_size = p->dest_mem_p.size;
sg_addr = p->dest_mem_p.phy_addr;
sg_init_table(pdma_rx->sgrx, sg_loop);
pdma_rx->sgrx[0].dma_address = sg_addr;
pdma_rx->sgrx[0].dma_length = sg_size;
pdma_rx->rxdesc = dmaengine_prep_slave_sg(pdma_rx->chan_rx, pdma_rx->sgrx, sg_loop, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!pdma_rx->rxdesc) {
dev_err(p->port.dev, "pdma->rxdes==NULL.\n");
return;
}
pdma_rx->rxdesc->callback = ma35d1_Rx_dma_callback;
p->dma_Rx_done.callback_param = p;
p->dma_Rx_done.timeout = 0;
pdma_rx->rxdesc->callback_param = &(p->dma_Rx_done);
cookie = pdma_rx->rxdesc->tx_submit(pdma_rx->rxdesc);
if (dma_submit_error(cookie)) {
dev_err(p->port.dev, "rx dma_submit_error.\n");
return;
}
}
static void ma35d1_prepare_TX_dma(struct uart_ma35d1_port *p)
{
struct ma35d1_dma_config dma_ctx;
struct ma35d1_ip_tx_dma *pdma_tx = &(p->dma_tx);
struct ma35d1_dma_data data;
int ret;
dma_cookie_t cookie;
struct circ_buf *xmit = &p->port.state->xmit;
if(p->src_mem_p.size == 0) {
p->src_mem_p.size = UART_XMIT_SIZE;
p->src_mem_p.vir_addr = (u64)(kmalloc(p->src_mem_p.size, GFP_KERNEL));
p->src_mem_p.phy_addr = dma_map_single(pdma_tx->chan_tx->device->dev, (void *)p->src_mem_p.vir_addr, p->src_mem_p.size, DMA_TO_DEVICE);
ret = dma_mapping_error(pdma_tx->chan_tx->device->dev, p->src_mem_p.phy_addr);
if (ret)
dev_err(p->port.dev, "src mapping error.\n");
}
p->tx_dma_len = uart_circ_chars_pending(xmit);
if (xmit->tail < xmit->head) {
memcpy((unsigned char *)p->src_mem_p.vir_addr, &xmit->buf[xmit->tail], p->tx_dma_len);
} else {
size_t first = UART_XMIT_SIZE - xmit->tail;
size_t second = xmit->head;
memcpy((unsigned char *)p->src_mem_p.vir_addr, &xmit->buf[xmit->tail], first);
if (second)
memcpy((unsigned char *)p->src_mem_p.vir_addr+first, &xmit->buf[0], second);
}
dma_sync_single_for_device(pdma_tx->chan_tx->device->dev, p->src_mem_p.phy_addr, UART_XMIT_SIZE, DMA_TO_DEVICE);
xmit->tail = (xmit->tail + p->tx_dma_len) & (UART_XMIT_SIZE - 1);
serial_out(p, UART_REG_IER, (serial_in(p, UART_REG_IER) &~ TXPDMAEN));
pdma_tx->slave_config.dst_addr = (unsigned int)(p->port.iobase);
pdma_tx->slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
pdma_tx->slave_config.dst_maxburst = 1;
pdma_tx->slave_config.direction = DMA_MEM_TO_DEV;
pdma_tx->slave_config.slave_id = p->PDMA_UARTx_TX;
data.timeout_prescaler = 0;
data.timeout_counter = 0;
pdma_tx->chan_tx->private = (void *)&data;
dmaengine_slave_config(pdma_tx->chan_tx,&(pdma_tx->slave_config));
sg_init_table(pdma_tx->sgtx, 1);
pdma_tx->sgtx[0].dma_address =p->src_mem_p.phy_addr;
pdma_tx->sgtx[0].dma_length = p->tx_dma_len;
dma_ctx.en_sc = 0;
pdma_tx->txdesc = dmaengine_prep_slave_sg(pdma_tx->chan_tx, pdma_tx->sgtx, 1, DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!pdma_tx->txdesc) {
dev_err(p->port.dev, "pdma->txdes==NULL.\n");
return;
}
pdma_tx->txdesc->callback = ma35d1_Tx_dma_callback;
p->dma_Tx_done.callback_param = p;
pdma_tx->txdesc->callback_param = &(p->dma_Tx_done);
cookie = pdma_tx->txdesc->tx_submit(pdma_tx->txdesc);
if (dma_submit_error(cookie)) {
dev_err(p->port.dev, "dma_submit_error.\n");
return;
}
}
static void rs485_start_rx(struct uart_ma35d1_port *port)
{
}
static void rs485_stop_rx(struct uart_ma35d1_port *port)
{
}
static inline void __stop_tx(struct uart_ma35d1_port *p)
{
unsigned int ier;
struct tty_struct *tty = p->port.state->port.tty;
if ((ier = serial_in(p, UART_REG_IER)) & THRE_IEN) {
serial_out(p, UART_REG_IER, ier & ~THRE_IEN);
}
if (p->rs485.flags & SER_RS485_ENABLED)
rs485_start_rx(p);
}
static void ma35d1serial_stop_tx(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
__stop_tx(up);
}
static void transmit_chars(struct uart_ma35d1_port *up);
static void ma35d1serial_start_tx(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned int ier;
struct tty_struct *tty = up->port.state->port.tty;
struct circ_buf *xmit = &up->port.state->xmit;
if (up->rs485.flags & SER_RS485_ENABLED)
rs485_stop_rx(up);
if(up->uart_pdma_enable_flag == 1) {
if(up->Tx_pdma_busy_flag == 1) {
return;
}
if (uart_circ_empty(xmit)) {
__stop_tx(up);
return;
}
up->Tx_pdma_busy_flag = 1;
ma35d1_prepare_TX_dma(up);
serial_out(up, UART_REG_IER, (serial_in(up, UART_REG_IER)|TXPDMAEN));
} else {
struct circ_buf *xmit = &up->port.state->xmit;
ier = serial_in(up, UART_REG_IER);
serial_out(up, UART_REG_IER, ier & ~THRE_IEN);
if( uart_circ_chars_pending(xmit)<(16-((serial_in(up, UART_REG_FSR)>>16)&0x3F)) )
transmit_chars(up);
serial_out(up, UART_REG_IER, ier | THRE_IEN);
}
}
static void ma35d1serial_stop_rx(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
serial_out(up, UART_REG_IER, serial_in(up, UART_REG_IER) & ~RDA_IEN);
}
static void ma35d1serial_enable_ms(struct uart_port *port)
{
}
static int max_count = 0;
static void
receive_chars(struct uart_ma35d1_port *up)
{
unsigned char ch;
unsigned int fsr;
unsigned int isr;
unsigned int dcnt;
char flag;
isr = serial_in(up, UART_REG_ISR);
fsr = serial_in(up, UART_REG_FSR);
while(!(fsr & RX_EMPTY)) {
//fsr = serial_in(up, UART_REG_FSR);
flag = TTY_NORMAL;
up->port.icount.rx++;
if (unlikely(fsr & (BIF | FEF | PEF | RX_OVER_IF))) {
if (fsr & BIF) {
serial_out(up, UART_REG_FSR, BIF);
up->port.icount.brk++;
if (uart_handle_break(&up->port))
continue;
}
if (fsr & FEF) {
serial_out(up, UART_REG_FSR, FEF);
up->port.icount.frame++;
}
if (fsr & PEF) {
serial_out(up, UART_REG_FSR, PEF);
up->port.icount.parity++;
}
if (fsr & RX_OVER_IF) {
serial_out(up, UART_REG_FSR, RX_OVER_IF);
up->port.icount.overrun++;
}
// FIXME: check port->read_status_mask to determin report flags
if (fsr & BIF)
flag = TTY_BREAK;
if (fsr & PEF)
flag = TTY_PARITY;
if (fsr & FEF)
flag = TTY_FRAME;
}
ch = (unsigned char)serial_in(up, UART_REG_RBR);
if (uart_handle_sysrq_char(&up->port, ch))
continue;
uart_insert_char(&up->port, fsr, RX_OVER_IF, ch, flag);
max_count++;
dcnt=(serial_in(up, UART_REG_FSR) >> 8) & 0x3f;
if(max_count > 1023)
{
spin_lock(&up->port.lock);
tty_flip_buffer_push(&up->port.state->port);
spin_unlock(&up->port.lock);
max_count=0;
if((isr & TOUT_IF) && (dcnt == 0))
goto tout_end;
}
if(isr & RDA_IF) {
if(dcnt == 1)
return; // have remaining data, don't reset max_count
}
fsr = serial_in(up, UART_REG_FSR);
}
spin_lock(&up->port.lock);
tty_flip_buffer_push(&up->port.state->port);
spin_unlock(&up->port.lock);
tout_end:
max_count=0;
return;
}
static void transmit_chars(struct uart_ma35d1_port *up)
{
struct circ_buf *xmit = &up->port.state->xmit;
int count = 16 -((serial_in(up, UART_REG_FSR)>>16)&0xF);
if(serial_in(up, UART_REG_FSR) & TX_FULL){
count = 0;
}
if (up->port.x_char) {
while(serial_in(up, UART_REG_FSR) & TX_FULL);
serial_out(up, UART_REG_THR, up->port.x_char);
up->port.icount.tx++;
up->port.x_char = 0;
return;
}
if (uart_tx_stopped(&up->port)) {
ma35d1serial_stop_tx(&up->port);
return;
}
if (uart_circ_empty(xmit)) {
__stop_tx(up);
return;
}
while(count > 0){
while(serial_in(up, UART_REG_FSR) & TX_FULL);
serial_out(up, UART_REG_THR, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
up->port.icount.tx++;
count--;
if (uart_circ_empty(xmit))
break;
}
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(&up->port);
if (uart_circ_empty(xmit))
__stop_tx(up);
}
static unsigned int check_modem_status(struct uart_ma35d1_port *up)
{
unsigned int status = 0;
if (0) {
wake_up_interruptible(&up->port.state->port.delta_msr_wait);
}
return status;
}
static irqreturn_t ma35d1serial_interrupt(int irq, void *dev_id)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)dev_id;
unsigned int isr, fsr;
isr = serial_in(up, UART_REG_ISR);
fsr = serial_in(up, UART_REG_FSR);
if(up->uart_pdma_enable_flag == 1) {
if(fsr & (BIF | FEF | PEF | RX_OVER_IF | HWBUFE_IF | TX_OVER_IF)) {
serial_out(up, UART_REG_FSR, (BIF | FEF | PEF | RX_OVER_IF | TX_OVER_IF));
}
} else {
if (isr & (RDA_IF | TOUT_IF)){
receive_chars(up);
}
check_modem_status(up);
if (isr & THRE_INT)
transmit_chars(up);
if(fsr & (BIF | FEF | PEF | RX_OVER_IF | TX_OVER_IF)) {
serial_out(up, UART_REG_FSR, (BIF | FEF | PEF | RX_OVER_IF | TX_OVER_IF));
}
}
return IRQ_HANDLED;
}
static unsigned int ma35d1serial_tx_empty(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned int fsr;
fsr = serial_in(up, UART_REG_FSR);
return (fsr & (TE_FLAG | TX_EMPTY)) == (TE_FLAG | TX_EMPTY) ? TIOCSER_TEMT : 0;
}
static unsigned int ma35d1serial_get_mctrl(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned int status;
unsigned int ret = 0;
status = serial_in(up, UART_REG_MSR);;
if(!(status & 0x10))
ret |= TIOCM_CTS;
return ret;
}
static void ma35d1serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned int mcr = 0;
unsigned int ier = 0;
if (mctrl & TIOCM_RTS) {
// set RTS high level trigger
mcr = serial_in(up, UART_REG_MCR);
mcr |= 0x200;
mcr &= ~(0x2);
}
if (up->mcr & UART_MCR_AFE) {
// set RTS high level trigger
mcr = serial_in(up, UART_REG_MCR);
mcr |= 0x200;
mcr &= ~(0x2);
// enable CTS/RTS auto-flow control
serial_out(up, UART_REG_IER, (serial_in(up, UART_REG_IER) | (0x3000)));
// Set hardware flow control
up->port.flags |= UPF_HARD_FLOW;
} else {
// disable CTS/RTS auto-flow control
ier = serial_in(up, UART_REG_IER);
ier &= ~(0x3000);
serial_out(up, UART_REG_IER, ier);
//un-set hardware flow control
up->port.flags &= ~UPF_HARD_FLOW;
}
// set CTS high level trigger
serial_out(up, UART_REG_MSR, (serial_in(up, UART_REG_MSR) | (0x100)));
serial_out(up, UART_REG_MCR, mcr);
}
static void ma35d1serial_break_ctl(struct uart_port *port, int break_state)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned long flags;
unsigned int lcr;
spin_lock_irqsave(&up->port.lock, flags);
lcr = serial_in(up, UART_REG_LCR);
if (break_state != 0)
lcr |= BCB; // set break
else
lcr &= ~BCB; // clr break
serial_out(up, UART_REG_LCR, lcr);
spin_unlock_irqrestore(&up->port.lock, flags);
}
static int ma35d1serial_startup(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
struct tty_struct *tty = port->state->port.tty;
int retval;
struct ma35d1_ip_rx_dma *pdma_rx = &(up->dma_rx);
struct ma35d1_ip_tx_dma *pdma_tx = &(up->dma_tx);
dma_cap_mask_t mask;
if(up->uart_pdma_enable_flag == 1) {
dma_cap_zero(mask);
dma_cap_set(DMA_SLAVE, mask);
dma_cap_set(DMA_PRIVATE, mask);
pdma_rx->chan_rx = dma_request_channel(mask, NULL, NULL);
if (!pdma_rx->chan_rx) {
dev_err(up->port.dev, "RX DMA channel request error.\n");
return -1;
}
pdma_rx->chan_rx->private=(void *)1;
pdma_tx->chan_tx = dma_request_channel(mask, NULL, NULL);
if (!pdma_tx->chan_tx) {
dev_err(up->port.dev, "TX DMA channel request error.\n");
return -1;
}
pdma_tx->chan_tx->private=(void *)1;
}
/* Reset FIFO */
serial_out(up, UART_REG_FCR, TFR | RFR /* | RX_DIS */);
/* Clear pending interrupts (not every bit are write 1 clear though...) */
serial_out(up, UART_REG_ISR, 0xFFFFFFFF);
retval = request_irq(port->irq, ma35d1serial_interrupt, 0, tty ? tty->name : "ma35d1_serial", port);
if (retval) {
dev_err(up->port.dev, "request irq failed.\n");
return retval;
}
/*
* Now, initialize the UART
*/
// FIFO trigger level 4 byte // RTS trigger level 8 bytes
serial_out(up, UART_REG_FCR, serial_in(up, UART_REG_FCR) | 0x10 | 0x20000);
serial_out(up, UART_REG_LCR, 0x7); // 8 bit
serial_out(up, UART_REG_TOR, 0x40);
if(up->uart_pdma_enable_flag == 1)
serial_out(up, UART_REG_IER, RLS_IEN | BUFERR_IEN);
else
serial_out(up, UART_REG_IER, RTO_IEN | RDA_IEN | TIME_OUT_EN | BUFERR_IEN);
if(up->uart_pdma_enable_flag == 1) {
up->baud_rate = 0;
}
return 0;
}
static void ma35d1serial_shutdown(struct uart_port *port)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
struct ma35d1_ip_rx_dma *pdma_rx = &(up->dma_rx);
struct ma35d1_ip_tx_dma *pdma_tx = &(up->dma_tx);
if(up->uart_pdma_enable_flag == 1) {
dma_release_channel(pdma_rx->chan_rx);
dma_release_channel(pdma_tx->chan_tx);
if(up->dest_mem_p.size != 0)
{
kfree((void *)up->dest_mem_p.vir_addr);
}
if(up->src_mem_p.size != 0)
{
kfree((void *)up->src_mem_p.vir_addr);
}
up->Tx_pdma_busy_flag = 0;
up->dest_mem_p.size = 0;
up->src_mem_p.size = 0;
}
free_irq(port->irq, port);
/*
* Disable interrupts from this port
*/
serial_out(up, UART_REG_IER, 0);
}
static unsigned int ma35d1serial_get_divisor(struct uart_port *port, unsigned int baud)
{
unsigned int quot;
quot = (port->uartclk / baud) - 2;
return quot;
}
static void
ma35d1serial_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
{
struct uart_ma35d1_port *up = (struct uart_ma35d1_port *)port;
unsigned int lcr = 0;
unsigned long flags;
unsigned int baud, quot;
switch (termios->c_cflag & CSIZE) {
case CS5:
lcr = 0;
break;
case CS6:
lcr |= 1;
break;
case CS7:
lcr |= 2;
break;
default:
case CS8:
lcr |= 3;
break;
}
if (termios->c_cflag & CSTOPB)
lcr |= NSB;
if (termios->c_cflag & PARENB)
lcr |= PBE;
if (!(termios->c_cflag & PARODD))
lcr |= EPE;
if (termios->c_cflag & CMSPAR)
lcr |= SPE;
baud = uart_get_baud_rate(port, termios, old, port->uartclk / 0xffff, port->uartclk / 11);
quot = ma35d1serial_get_divisor(port, baud);
/*
* Ok, we're now changing the port state. Do it with
* interrupts disabled.
*/
spin_lock_irqsave(&up->port.lock, flags);
up->port.read_status_mask = RX_OVER_IF /*| UART_LSR_THRE | UART_LSR_DR*/;
if (termios->c_iflag & INPCK)
up->port.read_status_mask |= FEF | PEF;
if (termios->c_iflag & (BRKINT | PARMRK))
up->port.read_status_mask |= BIF;
/*
* Characteres to ignore
*/
up->port.ignore_status_mask = 0;
if (termios->c_iflag & IGNPAR)
up->port.ignore_status_mask |= FEF | PEF;
if (termios->c_iflag & IGNBRK) {
up->port.ignore_status_mask |= BIF;
/*
* If we're ignoring parity and break indicators,
* ignore overruns too (for real raw support).
*/
if (termios->c_iflag & IGNPAR)
up->port.ignore_status_mask |= RX_OVER_IF;
}
if (termios->c_cflag & CRTSCTS)
up->mcr |= UART_MCR_AFE;
else
up->mcr &= ~UART_MCR_AFE;
ma35d1serial_set_mctrl(&up->port, up->port.mctrl);
serial_out(up, UART_REG_BAUD, quot | 0x30000000);
serial_out(up, UART_REG_LCR, lcr);
spin_unlock_irqrestore(&up->port.lock, flags);
if(up->uart_pdma_enable_flag == 1) {
if(up->baud_rate != baud){
up->baud_rate = baud;
ma35d1_uart_cal_pdma_time_out(up, baud);
ma35d1_prepare_RX_dma(up);
// trigger pdma
serial_out(up, UART_REG_IER, (serial_in(up, UART_REG_IER)|RXPDMAEN));
}
}
}
static void
ma35d1serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
{
struct uart_ma35d1_port *p = (struct uart_ma35d1_port *)port;
if (p->pm)
p->pm(port, state, oldstate);
}
static void ma35d1serial_release_port(struct uart_port *port)
{
struct uart_ma35d1_port *p = (struct uart_ma35d1_port *)port;
struct ma35d1_ip_rx_dma *pdma_rx = &(p->dma_rx);
struct ma35d1_ip_tx_dma *pdma_tx = &(p->dma_tx);
if(p->uart_pdma_enable_flag == 1){
dma_unmap_single(pdma_rx->chan_rx->device->dev, p->dest_mem_p.phy_addr, UART_RX_BUF_SIZE, DMA_FROM_DEVICE);
dma_unmap_single(pdma_tx->chan_tx->device->dev, p->src_mem_p.phy_addr, p->src_mem_p.size, DMA_TO_DEVICE);
}
iounmap(port->membase);
port->membase = NULL;
}
static int ma35d1serial_request_port(struct uart_port *port)
{
return 0;
}
static void ma35d1serial_config_port(struct uart_port *port, int flags)
{
int ret;
/*
* Find the region that we can probe for. This in turn
* tells us whether we can probe for the type of port.
*/
ret = ma35d1serial_request_port(port);
if (ret < 0)
return;
port->type = PORT_MA35D1;
}