summaryrefslogtreecommitdiff
path: root/drivers/serial/ns921x-serial.c
blob: 7aa8f7f2b103a3bf9751201f0f3a37b86cb72b1f (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
/*
 * drivers/serial/ns921x-serial.c
 *
 * Copyright (C) 2007,2008 by Digi International Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#if defined(CONFIG_SERIAL_NS921X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

#include <linux/clk.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/serial_core.h>
#include <linux/serial.h>

#include <mach/ns921x-serial.h>
#include <mach/gpio.h>

#define HUB_IFS		0x0000
#define HUB_IFS_RXFSRIP		(1 << 25)
#define HUB_IFS_TXFSRIP		(1 << 19)
#define HUB_IFS_MODIP		(1 << 18)
#define HUB_IFS_RXFE		(1 << 13)
#define HUB_IFS_TXFF		(1 << 11)
#define HUB_IFS_TXFE		(1 << 10)

#define HUB_DMARXCTRL	0x0004
#define HUB_DMARXCTRL_DIRECT	(1 << 28)

#define HUB_RXIC	0x000c
#define HUB_RXIC_RXTHRS		(15 << 28)
#define HUB_RXIC_RXFSRIE	(1 << 25)

#define HUB_DMRXSF	0x0010
#define HUB_DMRXSF_BYTE		(7 << 9)
#define HUB_DMRXSF_FULL		(1 << 7)

#define HUB_DMRXDF	0x0014

#define HUB_DMATXCTRL	0x0018
#define HUB_DMATXCTRL_DIRECT	(1 << 28)

#define HUB_TXIC	0x0020
#define HUB_TXIC_TXTHRS		(15 << 28)
#define HUB_TXIC_TXFUFIE	(1 << 26)
#define HUB_TXIC_TXFSRIE	(1 << 25)

#define HUB_DMTXDF	0x0028

#define UART_WC		0x1000
#define UART_WC_RXEN		(1 << 30)
#define UART_WC_TXEN		(1 << 29)
#define UART_WC_RTSEN		(1 << 19)
#define UART_WC_RXFLUSH		(1 << 17)
#define UART_WC_TXFLUSH		(1 << 16)
#define UART_WC_TXFLOW_CHAR	(1 << 11)
#define UART_WC_TXFLOW_SOFT	(1 << 10)
#define UART_WC_TXFLOW_RI	(1 << 9)
#define UART_WC_TXFLOW_DSR	(1 << 8)
#define UART_WC_TXFLOW_DCD	(1 << 7)
#define UART_WC_TXFLOW_CTS	(1 << 6)
#define UART_WC_TXFLOW	(UART_WC_TXFLOW_RI | UART_WC_TXFLOW_DSR | \
			 UART_WC_TXFLOW_DCD | UART_WC_TXFLOW_CTS)

#define UART_IE		0x1004
#define UART_IE_OFLOW		(1 << 19)
#define UART_IE_PARITY		(1 << 18)
#define UART_IE_FRAME		(1 << 17)
#define UART_IE_BREAK		(1 << 16)
#define UART_IE_DSR		(1 <<  7)
#define UART_IE_DCD		(1 <<  6)
#define UART_IE_CTS		(1 <<  5)
#define UART_IE_RI		(1 <<  4)
#define UART_IE_TBC		(1 <<  3)
#define UART_IE_RBC		(1 <<  2)
#define UART_IE_TXIDLE		(1 <<  1)

#define UART_IS		0x1008
#define UART_IS_OFLOW		(1 << 19)
#define UART_IS_PARITY		(1 << 18)
#define UART_IS_FRAME		(1 << 17)
#define UART_IS_BREAK		(1 << 16)
#define UART_IS_DSR		(1 <<  7)
#define UART_IS_DCD		(1 <<  6)
#define UART_IS_CTS		(1 <<  5)
#define UART_IS_RI		(1 <<  4)
#define UART_IS_TBC		(1 <<  3)
#define UART_IS_RBC		(1 <<  2)
#define UART_IS_TXIDLE		(1 <<  1)

#define UIE_RX (UART_IE_OFLOW | UART_IE_PARITY | UART_IE_FRAME | \
		UART_IE_BREAK | UART_IE_RBC)
#define UIS_RX (UART_IS_OFLOW | UART_IS_PARITY | UART_IS_FRAME | \
		UART_IS_BREAK | UART_IS_RBC)
#define UIE_MS (UART_IE_DSR | UART_IE_DCD | UART_IE_CTS | UART_IE_RI)
#define UIS_MS (UART_IS_DSR | UART_IS_DCD | UART_IS_CTS | UART_IS_RI)

#define UART_CGAPCTRL	0x100c
#define UART_CGAPCTRL_EN	(1 << 31)

#define UART_BGAPCTRL	0x1010
#define UART_BGAPCTRL_EN	(1 << 31)

#define UART_RCMC0	0x1014
#define UART_RCMC0_ENABLE	(1 << 31)
#define UART_RCMC0_MASK		(0xff << 16)
#define UART_RCMC0_DATA		(0xff << 0)

#define UART_AWC	0x1030
#define UART_AWC_ENABLE		(1 << 0)

#define UART_BRDL       0x1100	/* DLAB = 1 */

#define UART_UIE	0x1104	/* DLAB = 0 */
#define UART_UIE_ETBEI		(1 << 1)

#define UART_BRDM	0x1104	/* DLAB = 1 */

#define UART_FCR	0x1108
#define UART_FCR_TXCLR		(1 << 2)
#define UART_FCR_RXCLR		(1 << 1)
#define UART_FCR_FIFOEN		(1 << 0)

#define UART_LCR	0x110c
#define UART_LCR_DLAB		(1 << 7)
#define UART_LCR_SBC		(1 << 6)
#define UART_LCR_SPAR		(1 << 5)
#define UART_LCR_EPAR		(1 << 4)
#define UART_LCR_PARITY		(1 << 3)
#define UART_LCR_STOP		(1 << 2)
#define UART_LCR_WLEN		0x0003
#define UART_LCR_WLEN_5		0x0000
#define UART_LCR_WLEN_6		0x0001
#define UART_LCR_WLEN_7		0x0002
#define UART_LCR_WLEN_8		0x0003

#define UART_MCR	0x1110
#define UART_MCR_AFE		(1 << 5)
#define UART_MCR_LOOP		(1 << 4)
#define UART_MCR_RTS		(1 << 1)
#define UART_MCR_DTR		(1 << 0)

#define UART_LSR	0x1114
#define UART_LSR_TEMT		(1 << 6)

#define UART_MSR	0x1118
#define UART_MSR_DCD		(1 << 7)
#define UART_MSR_RI		(1 << 6)
#define UART_MSR_DSR		(1 << 5)
#define UART_MSR_CTS		(1 << 4)
#define UART_MSR_DDCD		(1 << 3)
#define UART_MSR_TERI		(1 << 2)
#define UART_MSR_DDSR		(1 << 1)
#define UART_MSR_DCTS		(1 << 0)

#define UMSR_ANYDELTA		(UART_MSR_DDCD | UART_MSR_TERI | \
		UART_MSR_DDSR | UART_MSR_DCTS)

#define DRIVER_NAME "ns921x-serial"
#define NS921X_TTY_NAME "ttyNS"
#define NS921X_TTY_MAJOR 204 /* XXX */
#define NS921X_TTY_MINOR_START 196 /* XXX */

#define NS921X_UART_NR 4

#define FIFOSIZE 16
#define TXTHRESHOLD 1
#define RXTHRESHOLD 5

#define up2unp(up) container_of(up, struct uart_ns921x_port, port)
struct uart_ns921x_port {
	struct uart_port port;
	struct clk *clk;
#define NSUPF_TXIRQPENDING 1
#define NSUPF_SCHEDSTOPTX 2
	unsigned int flags;
	u32 ifs2ack;
	u32 is2ack;
	struct ns921x_uart_data *data;
};

#if defined(CONFIG_DEBUG_LL) && 0
void printch(char);
void printhex2(unsigned);
void printhex4(unsigned);
void printhex8(unsigned);
void printascii(const char *);
#else
#define printch(c) ((void)0)
#define printhex2(u) ((void)0)
#define printhex4(u) ((void)0)
#define printhex8(u) ((void)0)
#define printascii(s) ((void)0)
#endif

static inline u32 uartread32(struct uart_port *port, unsigned int offset)
{
	u32 ret = ioread32(port->membase + offset);

#if defined(DEBUG_UARTRW)
	dev_dbg(port->dev, "read  0x%p -> 0x%08x\n",
			port->membase + offset, ret);
#endif

	return ret;
}

static inline void uartwrite32(struct uart_port *port,
		u32 value, unsigned int offset)
{
#if defined(DEBUG_UARTRW)
	dev_dbg(port->dev, "write 0x%p <- 0x%08x\n",
			port->membase + offset, value);
#endif
	iowrite32(value, port->membase + offset);
}

static inline void uartwrite8(struct uart_port *port, u8 value,
		unsigned int offset)
{
#if defined(DEBUG_UARTRW)
	dev_dbg(port->dev, "write 0x%p <- 0x%02x\n",
			port->membase + offset, value);
#endif
	iowrite8(value, port->membase + offset);
}

/*
 * bits 19 to 31 of HUB_IFS need to be cleard by writing a 1 to it.
 * ns921x_uart_read_ifs and ns921x_uart_clear_ifs help debugging missed clears
 * by tracking the bits set
 */
#define IFS_BITSTOACK 0xfff80000
static inline u32 ns921x_uart_read_ifs(struct uart_ns921x_port *unp)
{
	u32 ret = uartread32(&unp->port, HUB_IFS);
	if (unp->ifs2ack)
		dev_dbg(unp->port.dev, "%s: %s still unacked: %x "
				"(called from %p)\n", __func__, "IFS",
				unp->ifs2ack, __builtin_return_address(0));
	unp->ifs2ack |= ret & IFS_BITSTOACK;

	if (unp->ifs2ack & ~ret)
		dev_dbg(unp->port.dev, "%s: rereading %s doesn't "
				"yield unacked bits (called from %p): "
				"%08x %08x\n", __func__, "IFS",
				__builtin_return_address(0), unp->ifs2ack, ret);

	return ret;
}

static inline void ns921x_uart_clear_ifs(struct uart_ns921x_port *unp,
		unsigned int mask)
{
	if (mask & ~unp->ifs2ack) {
		dev_dbg(unp->port.dev, "%s: unp->%s = %08x, mask = %08x\n",
				__func__, "ifs2ack", unp->ifs2ack, mask);
		BUG();
	}
	BUG_ON(mask & ~IFS_BITSTOACK);
	unp->ifs2ack &= ~mask;
	uartwrite32(&unp->port, mask, HUB_IFS);
}

/*
 * all bits of UART_IS need to be cleard by writing a 1 to it.
 * ns921x_uart_read_is and ns921x_uart_clear_is help debugging missed clears by
 * tracking the bits set
 */
static inline u32 ns921x_uart_read_is(struct uart_ns921x_port *unp)
{
	u32 ret = uartread32(&unp->port, UART_IS);
	if (unp->is2ack)
		dev_dbg(unp->port.dev, "%s: %s still unacked: %x "
				"(called from %p)\n", __func__, "IS",
				unp->is2ack, __builtin_return_address(0));
	unp->is2ack |= ret;

	if (unp->is2ack & ~ret)
		dev_dbg(unp->port.dev, "%s: rereading %s doesn't "
				"yield unacked bits (called from %p): "
				"%08x %08x\n", __func__, "IS",
				__builtin_return_address(0), unp->is2ack, ret);

	return ret;
}

static inline void ns921x_uart_clear_is(struct uart_ns921x_port *unp,
		u32 mask)
{
	if (mask & ~unp->is2ack) {
		dev_dbg(unp->port.dev, "%s: unp->%s = %08x, mask = %08x\n",
				__func__, "is2ack", unp->is2ack, mask);
		BUG();
	}
	unp->is2ack &= ~mask;
	uartwrite32(&unp->port, mask, UART_IS);
}

static inline void ns921x_uart_rmw_uartie(struct uart_port *port,
		u32 mask, u32 value)
{
	u32 ie = uartread32(port, UART_IE);

	BUG_ON((value & mask) != value);

	ie = (ie & ~mask) | value;

	uartwrite32(port, ie, UART_IE);
}

static unsigned int ns921x_uart_tx_empty(struct uart_port *port)
{
	struct uart_ns921x_port *unp = up2unp(port);
	/* Should I use UART_IS_TXIDLE? HUB_IFS_TXFE? */

	u32 lsr;

#if defined(NSUPF_TXIRQPENDING)
	if (unp->flags & NSUPF_TXIRQPENDING)
		return 0;
#endif

	lsr = uartread32(&unp->port, UART_LSR);

	if (!(lsr & UART_LSR_TEMT))
		return 0;

	return TIOCSER_TEMT;
}

static void ns921x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	u32 mcr = 0;

	/* RTS signal should be controlled as gpio to allow using AFE */
	if (mctrl & TIOCM_RTS)
		mcr |= UART_MCR_RTS;

	if (mctrl & TIOCM_DTR)
		mcr |= UART_MCR_DTR;

	if (mctrl & TIOCM_LOOP)
		mcr |= UART_MCR_LOOP;

	uartwrite32(port, mcr, UART_MCR);
}


/*
 * Depending on the configuration of the HW flow control, the CTS line is
 * not be passed to the uart, making impossible to read the status of some
 * modem lines through the modem status register.
 * Following function workarounds that by reading the CTS line as gpio.
 */
static unsigned int ns921x_uart_check_msr(struct uart_port *port, u32 irqstat)
{
	struct uart_ns921x_port *unp = up2unp(port);
	u32 status, cts_gpio;

	/* We can read the status of the modem lines through the
	 * modem status register, but we need to add the value of 
	 * CTS line from the gpio value */
	status = uartread32(port, UART_MSR);
	cts_gpio = unp->data->gpios[2];
	status |= (gpio_get_value(cts_gpio) != 0) ? 0 : UART_MSR_CTS;

	/* For event detection on the modem lines, we use the interrupt status
	 * register instead of the delta section of the modem status register.
	 * The interrupt flags are valid in all the cases */
	if ((irqstat & (UART_IS_DSR|UART_IS_DCD|UART_IS_CTS|UART_IS_RI)) && 
	     port->info != NULL) {
		if (irqstat & UART_IS_RI)
			port->icount.rng++;
		if (irqstat & UART_IS_DSR)
			port->icount.dsr++;
		if (irqstat & UART_IS_DCD)
			uart_handle_dcd_change(port, status & UART_MSR_DCD);
		if (irqstat & UART_IS_CTS)
			uart_handle_cts_change(port, status & UART_MSR_CTS);

		wake_up_interruptible(&port->info->delta_msr_wait);
	}

	return status;
}

static unsigned int ns921x_uart_get_mctrl(struct uart_port *port)
{
	unsigned int status;
	unsigned int ret = 0;

	status = ns921x_uart_check_msr(port, 0);

	if (status & UART_MSR_DCD)
		ret |= TIOCM_CAR;
	if (status & UART_MSR_RI)
		ret |= TIOCM_RNG;
	if (status & UART_MSR_DSR)
		ret |= TIOCM_DSR;
	if (status & UART_MSR_CTS) 
		ret |= TIOCM_CTS;

	return ret;
}

static void ns921x_uart_stop_tx_real(struct uart_port *port)
{
	struct uart_ns921x_port *unp = up2unp(port);

	u32 txic = uartread32(&unp->port, HUB_TXIC);

	printch('.');

	ns921x_uart_rmw_uartie(&unp->port, UART_IE_TXIDLE, 0);

	uartwrite32(port, txic & ~HUB_TXIC_TXFSRIE, HUB_TXIC);

#if defined(NSUPF_TXIRQPENDING)
	unp->flags &= ~NSUPF_TXIRQPENDING;
#endif
}

/* called with port->lock taken */
static void ns921x_uart_stop_tx(struct uart_port *port)
{
#if defined(NSUPF_TXIRQPENDING) && defined(NSUPF_SCHEDSTOPTX)
	struct uart_ns921x_port *unp = up2unp(port);

	/* don't stop the port if there is a tx irq pending */
	if (unp->flags & NSUPF_TXIRQPENDING) {
		unp->flags |= NSUPF_SCHEDSTOPTX;
	} else {
		ns921x_uart_stop_tx_real(port);

		unp->flags &= ~NSUPF_SCHEDSTOPTX;
	}
#else
	ns921x_uart_stop_tx_real(port);
#endif
}

/* send out chars in xmit buffer.  This is called with port->lock taken */
static void ns921x_uart_tx_chars(struct uart_ns921x_port *unp,
		unsigned int freebuffers)
{
	struct circ_buf *xmit = &unp->port.info->xmit;
	unsigned long ifs;

	BUG_ON(!freebuffers);

	assert_spin_locked(&unp->port.lock);

	/*
	 * If the FIFO is not empty return at this point, then the
	 * interrupt-handler will recall this function
	 */
	ifs = ns921x_uart_read_ifs(unp);
	if (!(ifs & HUB_IFS_TXFE))
		return;
	
	if (unp->port.x_char) {
		uartwrite8(&unp->port, unp->port.x_char, HUB_DMTXDF);
		unp->port.icount.tx++;
		unp->port.x_char = 0;
		freebuffers--;
#if defined(NSUPF_TXIRQPENDING)
		unp->flags |= NSUPF_TXIRQPENDING;
#endif
	}
	if (uart_circ_empty(xmit) || uart_tx_stopped(&unp->port)) {
		ns921x_uart_stop_tx(&unp->port);
		return;
	}

	printch('T');
	printhex4(uart_circ_chars_pending(xmit));
	while (freebuffers && uart_circ_chars_pending(xmit)) {
		if (uart_circ_chars_pending(xmit) >= 4) {
			u32 fourchars = xmit->buf[xmit->tail];
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
			fourchars |= xmit->buf[xmit->tail] << 8;
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
			fourchars |= xmit->buf[xmit->tail] << 16;
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
			fourchars |= xmit->buf[xmit->tail] << 24;
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);

			uartwrite32(&unp->port, fourchars, HUB_DMTXDF);
			printhex8(cpu_to_be32(fourchars));
			unp->port.icount.tx += 4;
		} else {
			uartwrite8(&unp->port,
					xmit->buf[xmit->tail], HUB_DMTXDF);
			printhex2(xmit->buf[xmit->tail]);
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
			unp->port.icount.tx++;
		}
#if defined(NSUPF_TXIRQPENDING)
		unp->flags |= NSUPF_TXIRQPENDING;
#endif
		--freebuffers;
	}

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&unp->port);

	if (uart_circ_empty(xmit))
		ns921x_uart_stop_tx(&unp->port);
}

#define maskshiftfac(mask)		((mask) & (-(mask)))
#define im2mask(val, mask)						\
	(((val) * maskshiftfac(mask)) & (mask))

/* called with port->lock taken */
static void ns921x_uart_start_tx(struct uart_port *port)
{
	struct uart_ns921x_port *unp = up2unp(port);

	u32 txic = uartread32(port, HUB_TXIC);

	uartwrite32(port, txic | im2mask(TXTHRESHOLD, HUB_TXIC_TXTHRS) |
			HUB_TXIC_TXFSRIE, HUB_TXIC);
	ns921x_uart_rmw_uartie(&unp->port, UART_IE_TXIDLE, UART_IE_TXIDLE);

#if defined(NSUPF_SCHEDSTOPTX)
	unp->flags &= ~NSUPF_SCHEDSTOPTX;
#endif

	if (
#if defined(NSUPF_TXIRQPENDING)
			!(unp->flags & NSUPF_TXIRQPENDING) &&
#endif
			!(ns921x_uart_read_ifs(unp) & HUB_IFS_TXFF)) {
		unsigned int freebuffers = 1;
		ns921x_uart_tx_chars(up2unp(port), freebuffers);
	}
}

static void ns921x_uart_stop_rx(struct uart_port *port)
{
	dev_vdbg(port->dev, "%s\n", __func__);

	ns921x_uart_rmw_uartie(port, UIE_RX, 0);
}

static void ns921x_uart_enable_ms(struct uart_port *port)
{
	dev_vdbg(port->dev, "%s\n", __func__);

	ns921x_uart_rmw_uartie(port, UIE_MS, UIE_MS);
}

static void ns921x_uart_break_ctl(struct uart_port *port, int break_state)
{
	unsigned long flags;
	u32 lcr;

	dev_vdbg(port->dev, "%s\n", __func__);

	spin_lock_irqsave(&port->lock, flags);

	lcr = uartread32(port, UART_LCR);

	/* XXX: amba_pl011_break_ctl tests for break_state being -1,
	 * Documentation/serial/driver tells to tests for != 0 */
	if (break_state)
		lcr |= UART_LCR_SBC;
	else
		lcr &= ~UART_LCR_SBC;

	uartwrite32(port, lcr, UART_LCR);

	spin_unlock_irqrestore(&port->lock, flags);
}

static void ns921x_uart_rx_char(struct uart_ns921x_port *unp,
		unsigned int ch, u32 is)
{
	unsigned int flag = TTY_NORMAL;

	unp->port.icount.rx++;
#define ISERR	(UART_IS_BREAK | UART_IS_PARITY | UART_IS_FRAME | UART_IS_OFLOW)

	dev_vdbg(unp->port.dev, "%s: IS=%x\n", __func__, is);
	if (unlikely(is & ISERR)) {
		if (is & UART_IS_BREAK) {
			dev_vdbg(unp->port.dev, "break, IS=%x\n", is);
			unp->port.icount.brk++;
			if (uart_handle_break(&unp->port))
				return;
		} else if (is & UART_IS_PARITY)
			unp->port.icount.parity++;
		else if (is & UART_IS_FRAME)
			unp->port.icount.frame++;

		if (is & UART_IS_OFLOW)
			unp->port.icount.overrun++;

		is &= unp->port.read_status_mask;

		if (is & UART_IS_BREAK)
			flag = TTY_BREAK;
		else if (is & UART_IS_PARITY)
			flag = TTY_PARITY;
		else if (is & UART_IS_FRAME)
			flag = TTY_FRAME;
	}

	if (uart_handle_sysrq_char(&unp->port, ch))
		return;

	dev_vdbg(unp->port.dev, "%s: insert %x (IS=%x, ism=%x, flag=%x)\n",
			__func__, ch, is, unp->port.ignore_status_mask, flag);
	uart_insert_char(&unp->port, is, UART_IS_OFLOW, ch, flag);
}

/* This is called with port->lock taken */
static void ns921x_uart_rx_chars(struct uart_ns921x_port *unp,
		u32 ifs, u32 is)
{
	struct tty_struct *tty = unp->port.info->port.tty;

	if (is & (UART_IS_RBC | UIS_RX))
		ns921x_uart_clear_is(unp, is & (UART_IS_RBC | UIS_RX));

	if (ifs & HUB_IFS_RXFE) {
		if (is & (UART_IS_BREAK | UART_IS_OFLOW))
			ns921x_uart_rx_char(unp, 0, is);
	}

	while (!(ifs & HUB_IFS_RXFE)) {
		u32 dmrxsf = uartread32(&unp->port, HUB_DMRXSF);
		u32 dmrxdf = uartread32(&unp->port, HUB_DMRXDF);
		/* XXX: better put this into a macro */
		unsigned int bytes = (dmrxsf & HUB_DMRXSF_BYTE) >> 9;
		int i;

		dev_vdbg(unp->port.dev, "%s: DMRXSF = %x, DMRXDF = %x\n",
				__func__, dmrxsf, dmrxdf);

		BUG_ON(bytes > 4);

		for (i = 0; i < bytes; ++i) {
			unsigned int ch = (dmrxdf >> (8 * i)) & 0xff;

			/*
			 * assume break and errors only apply to the last
			 * character.
			 */
			ns921x_uart_rx_char(unp, ch, i == bytes - 1 ? is : 0);
		}
		ifs = ns921x_uart_read_ifs(unp);
	}
	spin_unlock(&unp->port.lock);
	tty_flip_buffer_push(tty);
	spin_lock(&unp->port.lock);
}

static irqreturn_t ns921x_uart_int(int irq, void *dev_id)
{
	struct uart_ns921x_port *unp = dev_id;
	u32 ifs, uninitialized_var(is);

	spin_lock(&unp->port.lock);

	ifs = ns921x_uart_read_ifs(unp);

	if (ifs & HUB_IFS_TXFSRIP) {
		ns921x_uart_clear_ifs(unp, HUB_IFS_TXFSRIP);

		/* don't clear NSUPF_TXIRQPENDING, we still wait for TXdone */

		ns921x_uart_tx_chars(unp, FIFOSIZE - TXTHRESHOLD - 1);
	}

	if ((ifs & HUB_IFS_RXFSRIP) || (ifs & HUB_IFS_MODIP))
		is = ns921x_uart_read_is(unp);

	if (ifs & HUB_IFS_RXFSRIP) {
		ns921x_uart_clear_ifs(unp, HUB_IFS_RXFSRIP);

		ns921x_uart_rx_chars(unp, ifs, is);
	}

	if (ifs & HUB_IFS_MODIP) {
		dev_vdbg(unp->port.dev, "%s: IS=%x\n", __func__, is);

		/* test for HUB_IFS_RXFSRIP not being set to only fetch
		 * characters once.
		 */
		if (is & UIS_RX && !(ifs & HUB_IFS_RXFSRIP))
			ns921x_uart_rx_chars(unp, ifs, is);

		if (is & UIS_MS)
			ns921x_uart_clear_is(unp, is & UIS_MS);

		/* XXX: call this only after an MS irq? */
		ns921x_uart_check_msr(&unp->port, is);

		if (is & UART_IS_TXIDLE) {
			unsigned int freebuffers = TXTHRESHOLD;
#if defined(NSUPF_TXIRQPENDING)
			unp->flags &= ~NSUPF_TXIRQPENDING;
#endif

			ns921x_uart_clear_is(unp, UART_IS_TXIDLE);

			/* The fifo might not be empty.  The following can
			 * happen:
			 * - irq for HUB_IFS_TXFSRIP serviced without
			 *   UART_IS_TXIDLE
			 * - before new data is written the fifo becomes empty
			 *   and UART_IS_TXIDLE is pending
			 * - UART_IS_TXIDLE is serviced with fifo still having
			 *   some data from servicing UART_IS_TXIDLE.
			 *
			 * So we only write TXTHRESHOLD buffers.  Only if the
			 * fifo is reported to be empty and we didn't wrote
			 * something above, the full fifo is used.
			 */
			if ((ifs & (HUB_IFS_TXFE | HUB_IFS_TXFSRIP)) ==
					HUB_IFS_TXFE)
				freebuffers = FIFOSIZE;

			ns921x_uart_tx_chars(unp, freebuffers);
		}

		if (unp->is2ack)
			dev_dbg(unp->port.dev,
					"%s: unacked flags in UART_IS: %x %x\n",
					__func__, unp->is2ack, is);
	}

	if (unp->ifs2ack)
		dev_dbg(unp->port.dev, "%s: unacked flags in HUB_IFS: %x\n",
				__func__, unp->ifs2ack);

#if defined(NSUPF_SCHEDSTOPTX) && defined(NSUPF_TXIRQPENDING)
	if ((unp->flags & (NSUPF_TXIRQPENDING | NSUPF_SCHEDSTOPTX)) ==
			NSUPF_SCHEDSTOPTX) {
		ns921x_uart_stop_tx_real(&unp->port);
		unp->flags &= ~NSUPF_SCHEDSTOPTX;
	}
#endif

	spin_unlock(&unp->port.lock);

	return IRQ_HANDLED;
}

static int ns921x_uart_startup(struct uart_port *port)
{
	struct uart_ns921x_port *unp = up2unp(port);
	int ret;
	u32 rxic;
	u32 wc;

	unp->flags = 0;

	ret = clk_enable(unp->clk);
	if (ret) {
		dev_dbg(port->dev, "%s: err_clkenable", __func__);
		goto err_clkenable;
	}

	unp->port.uartclk = clk_get_rate(unp->clk);

	ret = request_irq(unp->port.irq, ns921x_uart_int, 0, DRIVER_NAME, unp);
	if (ret) {
		dev_dbg(port->dev, "%s: err_request_irq", __func__);

		clk_disable(unp->clk);
err_clkenable:

		return ret;
	}

	ns921x_uart_rmw_uartie(port, UIE_RX, UIE_RX);

	rxic = uartread32(port, HUB_RXIC);
	uartwrite32(port, rxic | im2mask(RXTHRESHOLD, HUB_RXIC_RXTHRS)
			| HUB_RXIC_RXFSRIE, HUB_RXIC);

	wc = UART_WC_RXEN | UART_WC_TXEN;
	if (unp->data->rtsen)
		wc |= UART_WC_RTSEN;
	uartwrite32(port, wc, UART_WC);
	uartwrite32(&unp->port, UART_FCR_FIFOEN, UART_FCR);

	return 0;
}

static void ns921x_uart_shutdown(struct uart_port *port)
{
	struct uart_ns921x_port *unp = up2unp(port);

	/* wait up to 10ms for the fifo to empty */
	unsigned long expire = jiffies + msecs_to_jiffies(10);

	printch('\\');

	while (!ns921x_uart_tx_empty(port)) {
		msleep(1);
		if (time_after(jiffies, expire))
			break;
	}

	ns921x_uart_rmw_uartie(port, -1, 0);
	uartwrite32(port, 0, UART_WC);

	ns921x_uart_break_ctl(port, 0);

	free_irq(port->irq, unp);

	clk_disable(unp->clk);
}

static void ns921x_uart_set_termios(struct uart_port *port,
		struct ktermios *termios, struct ktermios *old)
{
	unsigned long flags;
	u32 cval = 0;

	unsigned int baud, quot;
	u32 saved_wc;

	switch (termios->c_cflag & CSIZE) {
	case CS5:
		cval = UART_LCR_WLEN_5;
		break;
	case CS6:
		cval = UART_LCR_WLEN_6;
		break;
	case CS7:
		cval = UART_LCR_WLEN_7;
		break;
	default:
	case CS8:
		cval = UART_LCR_WLEN_8;
		break;
	}

	if (termios->c_cflag & CSTOPB)
		cval |= UART_LCR_STOP;

	if (termios->c_cflag & PARENB) {
		cval |= UART_LCR_PARITY;

		if (!(termios->c_cflag & PARODD))
			cval |= UART_LCR_EPAR;
#ifdef CMSPAR
		/* stick parity:
		 *   - MARK parity requires flags PARENB | CMSPAR | PARODD
		 *     and in the controller the EPS and PEN bitfields
		 *     must be set to 0
		 *   - SPACE parity requires flags PARENB | CMSPAR | ~PARODD
		 *     and in the controller the EPS and PEN bitfields
		 *     must be set to 1 (these are already set by the
		 *     code above this, so there is nothing to do here)
		 *   In any case SP bitfield must be set to 1
		 */
		if(termios->c_cflag & CMSPAR) {
			cval |= UART_LCR_SPAR;	/* set stick parity */
			if (termios->c_cflag & PARODD) {
				cval &= ~UART_LCR_EPAR;
				cval &= ~UART_LCR_PARITY;
			}
		}
#endif
	}

	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
	quot = uart_get_divisor(port, baud);

	spin_lock_irqsave(&port->lock, flags);

	uart_update_timeout(port, termios->c_cflag, baud);

	port->read_status_mask = UART_IS_OFLOW;
	if (termios->c_iflag & INPCK)
		port->read_status_mask |= UART_IS_FRAME | UART_IS_PARITY;
	if (termios->c_iflag & (BRKINT | PARMRK))
		port->read_status_mask |= UART_IS_BREAK;

	port->ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		port->ignore_status_mask |= UART_IS_FRAME | UART_IS_PARITY;
	if (termios->c_iflag & IGNBRK) {
		port->ignore_status_mask |= UART_IS_BREAK;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			port->ignore_status_mask |= UART_IS_OFLOW;
	}

	/* disable everything, prepare fifo flushing */
	saved_wc = uartread32(port, UART_WC);
	uartwrite32(port, UART_WC_RXFLUSH | UART_WC_TXFLUSH, UART_WC);

	if (UART_ENABLE_MS(port, termios->c_cflag))
		ns921x_uart_enable_ms(port);

	/* set character gap period */
	uartwrite32(port, UART_CGAPCTRL_EN | (10 * (port->uartclk / baud) - 1),
			UART_CGAPCTRL);

	/* set buffer gap period */
	uartwrite32(port, UART_BGAPCTRL_EN | (640 * (port->uartclk / baud) - 1),
			UART_BGAPCTRL);

	/* prepare to access baud rate registers */
	uartwrite32(port, UART_LCR_DLAB, UART_LCR);

	/* set baud rate */
	uartwrite32(port, quot & 0xff, UART_BRDL);
	uartwrite32(port, (quot >> 8) & 0xff, UART_BRDM);

	udelay(1);
	uartwrite32(port, cval, UART_LCR);

	/* flush fifos and restore state */
	uartwrite32(port, saved_wc, UART_WC);
	uartwrite32(port, UART_UIE_ETBEI, UART_UIE);

	spin_unlock_irqrestore(&port->lock, flags);
}

static const char *ns921x_uart_type(struct uart_port *port)
{
	return port->type == PORT_NS921X ? "NS921X" : NULL;
}

static void ns921x_uart_release_port(struct uart_port *port)
{
	/* XXX: release_mem_region is marked as Compatibility cruft ??? */
	release_mem_region(port->mapbase, 0x8000);
}

static int ns921x_uart_request_port(struct uart_port *port)
{
	return request_mem_region(port->mapbase,
			0x8000, DRIVER_NAME) ? 0 : -EBUSY;
}

static void ns921x_uart_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE) {
		port->type = PORT_NS921X;
		ns921x_uart_request_port(port);
	}
}

static int ns921x_uart_verify_port(struct uart_port *port,
		struct serial_struct *ser)
{
	int ret = 0;
	if (ser->type != PORT_UNKNOWN && ser->type != PORT_NS921X)
		ret = -EINVAL;

	if (ser->irq < 0 || ser->irq >= NR_IRQS)
		ret = -EINVAL;

	if (ser->baud_base < 9600)
		ret = -EINVAL;

	return ret;
}

static struct uart_ops ns921x_uart_pops = {
	.tx_empty = ns921x_uart_tx_empty,
	.set_mctrl = ns921x_uart_set_mctrl,
	.get_mctrl = ns921x_uart_get_mctrl,
	.stop_tx = ns921x_uart_stop_tx,
	.start_tx = ns921x_uart_start_tx,
	.stop_rx = ns921x_uart_stop_rx,
	.enable_ms = ns921x_uart_enable_ms,
	.break_ctl = ns921x_uart_break_ctl,
	.startup = ns921x_uart_startup,
	.shutdown = ns921x_uart_shutdown,
	.set_termios = ns921x_uart_set_termios,
	/* .pm = ns921x_uart_pm, */
	/* .set_wake = ns921x_uart_set_wake, */
	.type = ns921x_uart_type,
	.release_port = ns921x_uart_release_port,
	.request_port = ns921x_uart_request_port,
	.config_port = ns921x_uart_config_port,
	.verify_port = ns921x_uart_verify_port,
	/* .ioctl */
};

static struct uart_ns921x_port *ns921x_uart_ports[NS921X_UART_NR];

#if defined(CONFIG_SERIAL_NS921X_CONSOLE)

static void ns921x_uart_console_putchar(struct uart_port *port, int ch)
{
	struct uart_ns921x_port *unp = up2unp(port);

	while (ns921x_uart_read_ifs(unp) & HUB_IFS_TXFF)
		barrier();

	uartwrite8(&unp->port, ch, HUB_DMTXDF);
}

/* called with console_sem hold.  irqs locally disabled */
static void ns921x_uart_console_write(struct console *co,
		const char *s, unsigned int count)
{
	struct uart_ns921x_port *unp = ns921x_uart_ports[co->index];
	u32 saved_txic, saved_wc, saved_uie;
	u32 new_wc;

	BUG_ON(!irqs_disabled());

	saved_txic = uartread32(&unp->port, HUB_TXIC);
	saved_wc = uartread32(&unp->port, UART_WC);
	saved_uie = uartread32(&unp->port, UART_UIE);

	new_wc = saved_wc | UART_WC_RXEN | UART_WC_TXEN;
	new_wc &= ~(UART_WC_RXFLUSH | UART_WC_TXFLUSH);

	/* XXX: assert baud, bits, parity etc. to be correct. */

	uartwrite32(&unp->port, 0, HUB_TXIC);
	uartwrite32(&unp->port, UART_FCR_FIFOEN, UART_FCR);
	uartwrite32(&unp->port, UART_UIE_ETBEI, UART_UIE);
	uartwrite32(&unp->port, new_wc, UART_WC);

	uart_console_write(&unp->port, s, count, ns921x_uart_console_putchar);

	/* wait for HUB fifo to become empty */
	while (!(ns921x_uart_read_ifs(unp) & HUB_IFS_TXFE))
		barrier();

	while (!(uartread32(&unp->port, UART_LSR) & UART_LSR_TEMT))
		barrier();

	uartwrite32(&unp->port, saved_wc, UART_WC);
	uartwrite32(&unp->port, saved_uie, UART_UIE);
	uartwrite32(&unp->port, saved_txic, HUB_TXIC);
}

static void __init ns921x_uart_console_get_options(struct uart_ns921x_port *unp,
		int *baud, int *parity, int *bits, int *flow)
{
	if (uartread32(&unp->port, UART_WC) & UART_WC_TXEN) {
		u32 cval = uartread32(&unp->port, UART_LCR);
		unsigned int quot = 0;

		uartwrite32(&unp->port, UART_LCR_DLAB, UART_LCR);

		/* XXX: write barrier */

		quot = (uartread32(&unp->port, UART_BRDM) & 0xff) << 8;
		quot |= uartread32(&unp->port, UART_BRDL) & 0xff;

		uartwrite32(&unp->port, cval, UART_LCR);

		*baud = unp->port.uartclk / (16 * quot);

		*parity = 'n';
		if (cval & UART_LCR_PARITY) {
			if (cval & UART_LCR_EPAR)
				*parity = 'e';
			else
				*parity = 'o';
		}
		if (cval & UART_LCR_SPAR) {
			if( (cval & UART_LCR_PARITY) &&
			    (cval & UART_LCR_EPAR) )
				*parity = 'm';
			else
				*parity = 's';
		}

		switch (cval & UART_LCR_WLEN) {
		case UART_LCR_WLEN_5:
			*bits = 5;
			break;
		case UART_LCR_WLEN_6:
			*bits = 6;
			break;
		case UART_LCR_WLEN_7:
			*bits = 7;
			break;
		case UART_LCR_WLEN_8:
			*bits = 8;
			break;
		}

		/* XXX: *flow = ... */
	}
}

static int __init ns921x_uart_console_setup(struct console *co, char *options)
{
	struct uart_ns921x_port *unp;
	int baud = 38400;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';
	int ret;

	if (co->index >= NS921X_UART_NR)
		co->index = 0;
	unp = ns921x_uart_ports[co->index];
	if (!unp)
		return -ENODEV;

	ret = clk_enable(unp->clk);
	if (ret)
		return ret;

	unp->port.uartclk = clk_get_rate(unp->clk);

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);
	else
		ns921x_uart_console_get_options(unp,
				&baud, &parity, &bits, &flow);

	ret = uart_set_options(&unp->port, co, baud, parity, bits, flow);

	/* disable the clock only in the error path, because
	 * otherwise it would need to be enabled and disabled in console_write
	 * where it must not sleep.  But as clk_disable does a gpio_free it
	 * might.
	 */
	if (ret)
		clk_disable(unp->clk);

	return ret;
}

static struct uart_driver ns921x_uart_reg;
static struct console ns921x_uart_console = {
	.name = NS921X_TTY_NAME,
	.write = ns921x_uart_console_write,
	/* .read */
	.device = uart_console_device,
	/* .unblank */
	.setup = ns921x_uart_console_setup,
	/* .early_setup */
	.flags = CON_PRINTBUFFER,
	.index = -1,
	/* .cflag */
	.data = &ns921x_uart_reg,
	/* .next */
};

#define NS921X_UART_CONSOLE (&ns921x_uart_console)
#else
#define NS921X_UART_CONSOLE NULL
#endif

static struct uart_driver ns921x_uart_reg = {
	.owner = THIS_MODULE,
	.driver_name = DRIVER_NAME,
	.dev_name = NS921X_TTY_NAME,
	.major = NS921X_TTY_MAJOR,
	.minor = NS921X_TTY_MINOR_START,
	.nr = NS921X_UART_NR,
	.cons = NS921X_UART_CONSOLE,
};

static __devinit int ns921x_uart_pdrv_probe(struct platform_device *pdev)
{
	int ret;
	struct uart_ns921x_port *unp;
	struct resource *mem;
	void __iomem *base;
	int line, irq;

	line = pdev->id;
	if (line < 0 || line >= ARRAY_SIZE(ns921x_uart_ports)) {
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "%s: err_line\n", __func__);
		goto err_line;
	}

	if (ns921x_uart_ports[line] != NULL) {
		ret = -EBUSY;
		dev_dbg(&pdev->dev, "%s: err_line\n", __func__);
		goto err_line;
	}

	unp = kzalloc(sizeof(struct uart_ns921x_port), GFP_KERNEL);
	ns921x_uart_ports[line] = unp;

	if (unp == NULL) {
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "%s: err_alloc\n", __func__);
		goto err_alloc;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		ret = -ENOENT;
		dev_info(&pdev->dev, "%s: err_get_irq\n", __func__);
		goto err_get_irq;
	}

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "%s: err_get_mem\n", __func__);
		goto err_get_mem;
	}

	base = ioremap(mem->start, 0x8000);
	if (!base) {
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "%s: err_ioremap\n", __func__);
		goto err_ioremap;
	}
	dev_dbg(&pdev->dev, "%s: membase = %p, unp = %p\n",
			__func__, base, unp);

	unp->clk = clk_get(&pdev->dev, DRIVER_NAME);
	if (IS_ERR(unp->clk)) {
		ret = PTR_ERR(unp->clk);
		dev_dbg(&pdev->dev, "%s: err_clk_get -> %d\n", __func__, ret);
		goto err_clk_get;
	}

	unp->data = pdev->dev.platform_data;
	unp->port.dev = &pdev->dev;
	unp->port.mapbase = mem->start;
	unp->port.membase = base;
	unp->port.iotype = UPIO_MEM;
	unp->port.irq = irq;
	unp->port.fifosize = 64; /* XXX */
	unp->port.ops = &ns921x_uart_pops;
	unp->port.flags = UPF_BOOT_AUTOCONF;
	unp->port.line = line;

	/* no DMA */
	uartwrite32(&unp->port, HUB_DMARXCTRL_DIRECT, HUB_DMARXCTRL);
	uartwrite32(&unp->port, HUB_DMATXCTRL_DIRECT, HUB_DMATXCTRL);

	ret = uart_add_one_port(&ns921x_uart_reg, &unp->port);
	if (ret) {

		dev_dbg(&pdev->dev, "%s: err_uart_add1port -> %d\n",
				__func__, ret);

		clk_put(unp->clk);
err_clk_get:

		iounmap(base);
err_ioremap:
err_get_mem:
err_get_irq:

		kfree(unp);
		ns921x_uart_ports[line] = NULL;
err_alloc:

err_line:

		return ret;
	}

	/* Make the device wakeup capable, but disabled by default */
	device_init_wakeup(&pdev->dev, 1);
	device_set_wakeup_enable(&pdev->dev, 0);

	platform_set_drvdata(pdev, unp);

	return 0;
}

static __devexit int ns921x_uart_pdrv_remove(struct platform_device *pdev)
{
	int line;
	struct uart_ns921x_port *unp = platform_get_drvdata(pdev);

	dev_dbg(&pdev->dev, "%s\n", __func__);

	line = unp->port.line;

	uart_remove_one_port(&ns921x_uart_reg, &unp->port);
	clk_put(unp->clk);
	iounmap(unp->port.membase);
	kfree(unp);
	ns921x_uart_ports[line] = NULL;

	return 0;
}

#ifdef CONFIG_PM
static int ns921x_uart_pdrv_suspend(struct platform_device *pdev,
		pm_message_t state)
{
	struct uart_ns921x_port *unp = platform_get_drvdata(pdev);
	int ret;

	dev_dbg(&pdev->dev, "%s\n", __func__);

	if (device_may_wakeup(&pdev->dev)) {
		ret = enable_irq_wake(unp->port.irq);
		if (ret)
			dev_dbg(&pdev->dev, "%s: err_enable_irq_wake -> %d\n",
					__func__, ret);

		else {
			uartwrite32(&unp->port, UART_RCMC0_ENABLE |
					UART_RCMC0_MASK, UART_RCMC0);
			/* Write a 0 and then enable to clear previous interrupts */
			uartwrite32(&unp->port, 0x0, UART_AWC);
			uartwrite32(&unp->port, UART_AWC_ENABLE, UART_AWC);
		}
	} else
		ret = uart_suspend_port(&ns921x_uart_reg, &unp->port);

	return ret;
}

static int ns921x_uart_pdrv_resume(struct platform_device *pdev)
{
	struct uart_ns921x_port *unp = platform_get_drvdata(pdev);
	int ret;

	dev_dbg(&pdev->dev, "%s\n", __func__);

	if (device_may_wakeup(&pdev->dev)) {
		uartwrite32(&unp->port, 0x0, UART_RCMC0);
		ret = disable_irq_wake(unp->port.irq);
	}
	else
		ret = uart_resume_port(&ns921x_uart_reg, &unp->port);

	return ret;
}
#else
#define ns921x_uart_pdrv_suspend	NULL
#define ns921x_uart_pdrv_resume		NULL
#endif

static struct platform_driver ns921x_uart_pdriver = {
	.probe = ns921x_uart_pdrv_probe,
	.remove = __devexit_p(ns921x_uart_pdrv_remove),
	.suspend = ns921x_uart_pdrv_suspend,
	.resume = ns921x_uart_pdrv_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
};

static int __init ns921x_uart_init(void)
{
	int ret;

	ret = uart_register_driver(&ns921x_uart_reg);
	if (ret) {
		pr_debug("%s: err_uart_register_driver\n", __func__);
		goto err_uart_register_driver;
	}

	ret = platform_driver_register(&ns921x_uart_pdriver);

	if (ret) {
		pr_debug("%s: err_pdrv_register\n", __func__);

		uart_unregister_driver(&ns921x_uart_reg);
err_uart_register_driver:

		return ret;
	}
	pr_info("Digi NS921x UART driver\n");

	return 0;
}

static void __exit ns921x_uart_exit(void)
{
	platform_driver_unregister(&ns921x_uart_pdriver);
	uart_unregister_driver(&ns921x_uart_reg);
}

module_init(ns921x_uart_init);
module_exit(ns921x_uart_exit);

MODULE_AUTHOR("Uwe Kleine-Koenig");
MODULE_DESCRIPTION("Digi NS921x UART driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRIVER_NAME);