Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
/*
 *  linux/drivers/char/serial_amba.c
 *
 *  Driver for AMBA serial ports
 *
 *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
 *
 *  Copyright 1999 ARM Limited
 *  Copyright (C) 2000 Deep Blue Solutions Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * This is a generic driver for ARM AMBA-type serial ports.  They
 * have a lot of 16550-like features, but are not register compatable.
 * Note that although they do have CTS, DCD and DSR inputs, they do
 * not have an RI input, nor do they have DTR or RTS outputs.  If
 * required, these have to be supplied via some other means (eg, GPIO)
 * and hooked into this driver.
 *
 * This could very easily become a generic serial driver for dumb UARTs
 * (eg, {82,16x}50, 21285, SA1100).
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/circ_buf.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/sysrq.h>

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

#include <asm/hardware/serial_amba.h>

#define SERIAL_AMBA_NAME	"ttyAM"
#define SERIAL_AMBA_MAJOR	204
#define SERIAL_AMBA_MINOR	16
#define SERIAL_AMBA_NR		2

#define CALLOUT_AMBA_NAME	"cuaam"
#define CALLOUT_AMBA_MAJOR	205
#define CALLOUT_AMBA_MINOR	16
#define CALLOUT_AMBA_NR		SERIAL_AMBA_NR

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define DEBUG 0
#define DEBUG_LEDS 0

#if DEBUG_LEDS
extern int get_leds(void);
extern int set_leds(int);
#endif

/*
 * Access routines for the AMBA UARTs
 */
#define UART_GET_INT_STATUS(p)	IO_READ((p)->uart_base + AMBA_UARTIIR)
#define UART_GET_FR(p)		IO_READ((p)->uart_base + AMBA_UARTFR)
#define UART_GET_CHAR(p)	IO_READ((p)->uart_base + AMBA_UARTDR)
#define UART_PUT_CHAR(p, c)	IO_WRITE((p)->uart_base + AMBA_UARTDR, (c))
#define UART_GET_RSR(p)		IO_READ((p)->uart_base + AMBA_UARTRSR)
#define UART_GET_CR(p)		IO_READ((p)->uart_base + AMBA_UARTCR)
#define UART_PUT_CR(p,c)	IO_WRITE((p)->uart_base + AMBA_UARTCR, (c))
#define UART_GET_LCRL(p)	IO_READ((p)->uart_base + AMBA_UARTLCR_L)
#define UART_PUT_LCRL(p,c)	IO_WRITE((p)->uart_base + AMBA_UARTLCR_L, (c))
#define UART_GET_LCRM(p)	IO_READ((p)->uart_base + AMBA_UARTLCR_M)
#define UART_PUT_LCRM(p,c)	IO_WRITE((p)->uart_base + AMBA_UARTLCR_M, (c))
#define UART_GET_LCRH(p)	IO_READ((p)->uart_base + AMBA_UARTLCR_H)
#define UART_PUT_LCRH(p,c)	IO_WRITE((p)->uart_base + AMBA_UARTLCR_H, (c))
#define UART_RX_DATA(s)		(((s) & AMBA_UARTFR_RXFE) == 0)
#define UART_TX_READY(s)	(((s) & AMBA_UARTFR_TXFF) == 0)
#define UART_TX_EMPTY(p)	((UART_GET_FR(p) & AMBA_UARTFR_TMSK) == 0)

#define AMBA_UARTRSR_ANY	(AMBA_UARTRSR_OE|AMBA_UARTRSR_BE|AMBA_UARTRSR_PE|AMBA_UARTRSR_FE)
#define AMBA_UARTFR_MODEM_ANY	(AMBA_UARTFR_DCD|AMBA_UARTFR_DSR|AMBA_UARTFR_CTS)

/*
 * Things needed by tty driver
 */
static struct tty_driver ambanormal_driver, ambacallout_driver;
static int ambauart_refcount;
static struct tty_struct *ambauart_table[SERIAL_AMBA_NR];
static struct termios *ambauart_termios[SERIAL_AMBA_NR];
static struct termios *ambauart_termios_locked[SERIAL_AMBA_NR];

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

/*
 * Things needed internally to this driver
 */

/*
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
 * lock it in case the copy_from_user blocks while swapping in a page,
 * and some other program tries to do a serial write at the same time.
 * Since the lock will only come under contention when the system is
 * swapping and available memory is low, it makes sense to share one
 * buffer across all the serial ports, since it significantly saves
 * memory if large numbers of serial ports are open.
 */
static u_char *tmp_buf;
static DECLARE_MUTEX(tmp_buf_sem);

#define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)

/* number of characters left in xmit buffer before we ask for more */
#define WAKEUP_CHARS		256
#define AMBA_ISR_PASS_LIMIT	256

#define EVT_WRITE_WAKEUP	0

struct amba_icount {
	__u32	cts;
	__u32	dsr;
	__u32	rng;
	__u32	dcd;
	__u32	rx;
	__u32	tx;
	__u32	frame;
	__u32	overrun;
	__u32	parity;
	__u32	brk;
	__u32	buf_overrun;
};

/*
 * Static information about the port
 */
struct amba_port {
	unsigned int		uart_base;
	unsigned int		irq;
	unsigned int		uartclk;
	unsigned int		fifosize;
	unsigned int		tiocm_support;
	void (*set_mctrl)(struct amba_port *, u_int mctrl);
};	

/*
 * This is the state information which is persistent across opens
 */
struct amba_state {
	struct amba_icount	icount;
	unsigned int		line;
	unsigned int		close_delay;
	unsigned int		closing_wait;
	unsigned int		custom_divisor;
	unsigned int		flags;
	struct termios		normal_termios;
	struct termios		callout_termios;

	int			count;
	struct amba_info	*info;
};

#define AMBA_XMIT_SIZE 1024
/*
 * This is the state information which is only valid when the port is open.
 */
struct amba_info {
	struct amba_port	*port;
	struct amba_state	*state;
	struct tty_struct	*tty;
	unsigned char		x_char;
	unsigned char		old_status;
	unsigned char		read_status_mask;
	unsigned char		ignore_status_mask;
	struct circ_buf		xmit;
	unsigned int		flags;
#ifdef SUPPORT_SYSRQ
	unsigned long		sysrq;
#endif

	unsigned int		event;
	unsigned int		timeout;
	unsigned int		lcr_h;
	unsigned int		mctrl;
	int			blocked_open;
	pid_t			session;
	pid_t			pgrp;

	struct tasklet_struct	tlet;

	wait_queue_head_t	open_wait;
	wait_queue_head_t	close_wait;
	wait_queue_head_t	delta_msr_wait;
};

#ifdef CONFIG_SERIAL_AMBA_CONSOLE
static struct console ambauart_cons;
#endif
static void ambauart_change_speed(struct amba_info *info, struct termios *old_termios);
static void ambauart_wait_until_sent(struct tty_struct *tty, int timeout);

#if 1 //def CONFIG_SERIAL_INTEGRATOR
static void amba_set_mctrl_null(struct amba_port *port, u_int mctrl)
{
}

static struct amba_port amba_ports[SERIAL_AMBA_NR] = {
	{
		uart_base:	IO_ADDRESS(INTEGRATOR_UART0_BASE),
		irq:		IRQ_UARTINT0,
		uartclk:	14745600,
		fifosize:	8,
		set_mctrl:	amba_set_mctrl_null,
	},
	{
		uart_base:	IO_ADDRESS(INTEGRATOR_UART1_BASE),
		irq:		IRQ_UARTINT1,
		uartclk:	14745600,
		fifosize:	8,
		set_mctrl:	amba_set_mctrl_null,
	}
};
#endif

static struct amba_state amba_state[SERIAL_AMBA_NR];

static void ambauart_enable_rx_interrupt(struct amba_info *info)
{
	unsigned int cr;

	cr = UART_GET_CR(info->port);
	cr |= AMBA_UARTCR_RIE | AMBA_UARTCR_RTIE;
	UART_PUT_CR(info->port, cr);
}

static void ambauart_disable_rx_interrupt(struct amba_info *info)
{
	unsigned int cr;

	cr = UART_GET_CR(info->port);
	cr &= ~(AMBA_UARTCR_RIE | AMBA_UARTCR_RTIE);
	UART_PUT_CR(info->port, cr);
}

static void ambauart_enable_tx_interrupt(struct amba_info *info)
{
	unsigned int cr;

	cr = UART_GET_CR(info->port);
	cr |= AMBA_UARTCR_TIE;
	UART_PUT_CR(info->port, cr);
}

static void ambauart_disable_tx_interrupt(struct amba_info *info)
{
	unsigned int cr;

	cr = UART_GET_CR(info->port);
	cr &= ~AMBA_UARTCR_TIE;
	UART_PUT_CR(info->port, cr);
}

static void ambauart_stop(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

	save_flags(flags); cli();
	ambauart_disable_tx_interrupt(info);
	restore_flags(flags);
}

static void ambauart_start(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

	save_flags(flags); cli();
	if (info->xmit.head != info->xmit.tail
	    && info->xmit.buf)
		ambauart_enable_tx_interrupt(info);
	restore_flags(flags);
}


/*
 * This routine is used by the interrupt handler to schedule
 * processing in the software interrupt portion of the driver.
 */
static void ambauart_event(struct amba_info *info, int event)
{
	info->event |= 1 << event;
	tasklet_schedule(&info->tlet);
}

static void
#ifdef SUPPORT_SYSRQ
ambauart_rx_chars(struct amba_info *info, struct pt_regs *regs)
#else
ambauart_rx_chars(struct amba_info *info)
#endif
{
	struct tty_struct *tty = info->tty;
	unsigned int status, ch, rsr, flg, ignored = 0;
	struct amba_icount *icount = &info->state->icount;
	struct amba_port *port = info->port;

	status = UART_GET_FR(port);
	while (UART_RX_DATA(status)) {
		ch = UART_GET_CHAR(port);

		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
			goto ignore_char;
		icount->rx++;

		flg = TTY_NORMAL;

		/*
		 * Note that the error handling code is
		 * out of the main execution path
		 */
		rsr = UART_GET_RSR(port);
		if (rsr & AMBA_UARTRSR_ANY)
			goto handle_error;
#ifdef SUPPORT_SYSRQ
		if (info->sysrq) {
			if (ch && time_before(jiffies, info->sysrq)) {
				handle_sysrq(ch, regs, NULL, NULL);
				info->sysrq = 0;
				goto ignore_char;
			}
			info->sysrq = 0;
		}
#endif
	error_return:
		*tty->flip.flag_buf_ptr++ = flg;
		*tty->flip.char_buf_ptr++ = ch;
		tty->flip.count++;
	ignore_char:
		status = UART_GET_FR(port);
	}
out:
	tty_flip_buffer_push(tty);
	return;

handle_error:
	if (rsr & AMBA_UARTRSR_BE) {
		rsr &= ~(AMBA_UARTRSR_FE | AMBA_UARTRSR_PE);
		icount->brk++;

#ifdef SUPPORT_SYSRQ
		if (info->state->line == ambauart_cons.index) {
			if (!info->sysrq) {
				info->sysrq = jiffies + HZ*5;
				goto ignore_char;
			}
		}
#endif
	} else if (rsr & AMBA_UARTRSR_PE)
		icount->parity++;
	else if (rsr & AMBA_UARTRSR_FE)
		icount->frame++;
	if (rsr & AMBA_UARTRSR_OE)
		icount->overrun++;

	if (rsr & info->ignore_status_mask) {
		if (++ignored > 100)
			goto out;
		goto ignore_char;
	}
	rsr &= info->read_status_mask;

	if (rsr & AMBA_UARTRSR_BE)
		flg = TTY_BREAK;
	else if (rsr & AMBA_UARTRSR_PE)
		flg = TTY_PARITY;
	else if (rsr & AMBA_UARTRSR_FE)
		flg = TTY_FRAME;

	if (rsr & AMBA_UARTRSR_OE) {
		/*
		 * CHECK: does overrun affect the current character?
		 * ASSUMPTION: it does not.
		 */
		*tty->flip.flag_buf_ptr++ = flg;
		*tty->flip.char_buf_ptr++ = ch;
		tty->flip.count++;
		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
			goto ignore_char;
		ch = 0;
		flg = TTY_OVERRUN;
	}
#ifdef SUPPORT_SYSRQ
	info->sysrq = 0;
#endif
	goto error_return;
}

static void ambauart_tx_chars(struct amba_info *info)
{
	struct amba_port *port = info->port;
	int count;

	if (info->x_char) {
		UART_PUT_CHAR(port, info->x_char);
		info->state->icount.tx++;
		info->x_char = 0;
		return;
	}
	if (info->xmit.head == info->xmit.tail
	    || info->tty->stopped
	    || info->tty->hw_stopped) {
		ambauart_disable_tx_interrupt(info);
		return;
	}

	count = port->fifosize;
	do {
		UART_PUT_CHAR(port, info->xmit.buf[info->xmit.tail]);
		info->xmit.tail = (info->xmit.tail + 1) & (AMBA_XMIT_SIZE - 1);
		info->state->icount.tx++;
		if (info->xmit.head == info->xmit.tail)
			break;
	} while (--count > 0);

	if (CIRC_CNT(info->xmit.head,
		     info->xmit.tail,
		     AMBA_XMIT_SIZE) < WAKEUP_CHARS)
		ambauart_event(info, EVT_WRITE_WAKEUP);

	if (info->xmit.head == info->xmit.tail) {
		ambauart_disable_tx_interrupt(info);
	}
}

static void ambauart_modem_status(struct amba_info *info)
{
	unsigned int status, delta;
	struct amba_icount *icount = &info->state->icount;

	status = UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;

	delta = status ^ info->old_status;
	info->old_status = status;

	if (!delta)
		return;

	if (delta & AMBA_UARTFR_DCD) {
		icount->dcd++;
#ifdef CONFIG_HARD_PPS
		if ((info->flags & ASYNC_HARDPPS_CD) &&
		    (status & AMBA_UARTFR_DCD)
			hardpps();
#endif
		if (info->flags & ASYNC_CHECK_CD) {
			if (status & AMBA_UARTFR_DCD)
				wake_up_interruptible(&info->open_wait);
			else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
				   (info->flags & ASYNC_CALLOUT_NOHUP))) {
				if (info->tty)
					tty_hangup(info->tty);
			}
		}
	}

	if (delta & AMBA_UARTFR_DSR)
		icount->dsr++;

	if (delta & AMBA_UARTFR_CTS) {
		icount->cts++;

		if (info->flags & ASYNC_CTS_FLOW) {
			status &= AMBA_UARTFR_CTS;

			if (info->tty->hw_stopped) {
				if (status) {
					info->tty->hw_stopped = 0;
					ambauart_enable_tx_interrupt(info);
					ambauart_event(info, EVT_WRITE_WAKEUP);
				}
			} else {
				if (!status) {
					info->tty->hw_stopped = 1;
					ambauart_disable_tx_interrupt(info);
				}
			}
		}
	}
	wake_up_interruptible(&info->delta_msr_wait);

}

static void ambauart_int(int irq, void *dev_id, struct pt_regs *regs)
{
	struct amba_info *info = dev_id;
	unsigned int status, pass_counter = 0;

#if DEBUG_LEDS
	// tell the world
	set_leds(get_leds() | RED_LED);
#endif

	status = UART_GET_INT_STATUS(info->port);
	do {
		/*
		 * FIXME: what about clearing the interrupts?
		 */

		if (status & (AMBA_UARTIIR_RTIS | AMBA_UARTIIR_RIS))
#ifdef SUPPORT_SYSRQ
			ambauart_rx_chars(info, regs);
#else
			ambauart_rx_chars(info);
#endif
		if (status & AMBA_UARTIIR_TIS)
			ambauart_tx_chars(info);
		if (status & AMBA_UARTIIR_MIS)
			ambauart_modem_status(info);
		if (pass_counter++ > AMBA_ISR_PASS_LIMIT)
			break;

		status = UART_GET_INT_STATUS(info->port);
	} while (status & (AMBA_UARTIIR_RTIS | AMBA_UARTIIR_RIS | AMBA_UARTIIR_TIS));

#if DEBUG_LEDS
	// tell the world
	set_leds(get_leds() & ~RED_LED);
#endif
}

static void ambauart_tasklet_action(unsigned long data)
{
	struct amba_info *info = (struct amba_info *)data;
	struct tty_struct *tty;

	tty = info->tty;
	if (!tty || !test_and_clear_bit(EVT_WRITE_WAKEUP, &info->event))
		return;

	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
	wake_up_interruptible(&tty->write_wait);
}

static int ambauart_startup(struct amba_info *info)
{
	unsigned long flags;
	unsigned long page;
	int retval = 0;

	page = get_zeroed_page(GFP_KERNEL);
	if (!page)
		return -ENOMEM;

	save_flags(flags); cli();

	if (info->flags & ASYNC_INITIALIZED) {
		free_page(page);
		goto errout;
	}

	if (info->xmit.buf)
		free_page(page);
	else
		info->xmit.buf = (unsigned char *) page;

	/*
	 * Allocate the IRQ
	 */
	retval = request_irq(info->port->irq, ambauart_int, 0, "amba", info);
	if (retval) {
		if (capable(CAP_SYS_ADMIN)) {
			if (info->tty)
				set_bit(TTY_IO_ERROR, &info->tty->flags);
			retval = 0;
		}
		goto errout;
	}

	info->mctrl = 0;
	if (info->tty->termios->c_cflag & CBAUD)
		info->mctrl = TIOCM_RTS | TIOCM_DTR;
	info->port->set_mctrl(info->port, info->mctrl);

	/*
	 * initialise the old status of the modem signals
	 */
	info->old_status = UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;

	/*
	 * Finally, enable interrupts
	 */
	ambauart_enable_rx_interrupt(info);

	if (info->tty)
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
	info->xmit.head = info->xmit.tail = 0;

	/*
	 * Set up the tty->alt_speed kludge
	 */
	if (info->tty) {
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
			info->tty->alt_speed = 57600;
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
			info->tty->alt_speed = 115200;
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
			info->tty->alt_speed = 230400;
		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
			info->tty->alt_speed = 460800;
	}

	/*
	 * and set the speed of the serial port
	 */
	ambauart_change_speed(info, 0);

	info->flags |= ASYNC_INITIALIZED;
	restore_flags(flags);
	return 0;

errout:
	restore_flags(flags);
	return retval;
}

/*
 * This routine will shutdown a serial port; interrupts are disabled, and
 * DTR is dropped if the hangup on close termio flag is on.
 */
static void ambauart_shutdown(struct amba_info *info)
{
	unsigned long flags;

	if (!(info->flags & ASYNC_INITIALIZED))
		return;

	save_flags(flags); cli(); /* Disable interrupts */

	/*
	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
	 * here so the queue might never be woken up
	 */
	wake_up_interruptible(&info->delta_msr_wait);

	/*
	 * Free the IRQ
	 */
	free_irq(info->port->irq, info);

	if (info->xmit.buf) {
		unsigned long pg = (unsigned long) info->xmit.buf;
		info->xmit.buf = NULL;
		free_page(pg);
	}

	/*
	 * disable all interrupts, disable the port
	 */
	UART_PUT_CR(info->port, 0);

	/* disable break condition and fifos */
	UART_PUT_LCRH(info->port, UART_GET_LCRH(info->port) &
		~(AMBA_UARTLCR_H_BRK | AMBA_UARTLCR_H_FEN));

	if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
		info->mctrl &= ~(TIOCM_DTR|TIOCM_RTS);
	info->port->set_mctrl(info->port, info->mctrl);

	/* kill off our tasklet */
	tasklet_kill(&info->tlet);
	if (info->tty)
		set_bit(TTY_IO_ERROR, &info->tty->flags);

	info->flags &= ~ASYNC_INITIALIZED;
	restore_flags(flags);
}

static void ambauart_change_speed(struct amba_info *info, struct termios *old_termios)
{
	unsigned int lcr_h, baud, quot, cflag, old_cr, bits;
	unsigned long flags;

	if (!info->tty || !info->tty->termios)
		return;

	cflag = info->tty->termios->c_cflag;

#if DEBUG
	printk("ambauart_set_cflag(0x%x) called\n", cflag);
#endif
	/* byte size and parity */
	switch (cflag & CSIZE) {
	case CS5: lcr_h = AMBA_UARTLCR_H_WLEN_5; bits = 7;  break;
	case CS6: lcr_h = AMBA_UARTLCR_H_WLEN_6; bits = 8;  break;
	case CS7: lcr_h = AMBA_UARTLCR_H_WLEN_7; bits = 9;  break;
	default:  lcr_h = AMBA_UARTLCR_H_WLEN_8; bits = 10; break; // CS8
	}
	if (cflag & CSTOPB) {
		lcr_h |= AMBA_UARTLCR_H_STP2;
		bits ++;
	}
	if (cflag & PARENB) {
		lcr_h |= AMBA_UARTLCR_H_PEN;
		bits++;
		if (!(cflag & PARODD))
			lcr_h |= AMBA_UARTLCR_H_EPS;
	}
	if (info->port->fifosize > 1)
		lcr_h |= AMBA_UARTLCR_H_FEN;

	do {
		/* Determine divisor based on baud rate */
		baud = tty_get_baud_rate(info->tty);
		if (!baud)
			baud = 9600;

		if (baud == 38400 &&
		    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
			quot = info->state->custom_divisor;
		else
			quot = (info->port->uartclk / (16 * baud)) - 1;

		if (!quot && old_termios) {
			info->tty->termios->c_cflag &= ~CBAUD;
			info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
			old_termios = NULL;
		}
	} while (quot == 0 && old_termios);

	/* As a last resort, if the quotient is zero, default to 9600 bps */
	if (!quot)
		quot = (info->port->uartclk / (16 * 9600)) - 1;
		
	info->timeout = (info->port->fifosize * HZ * bits * quot) /
			 (info->port->uartclk / 16);
	info->timeout += HZ/50;		/* Add .02 seconds of slop */

	if (cflag & CRTSCTS)
		info->flags |= ASYNC_CTS_FLOW;
	else
		info->flags &= ~ASYNC_CTS_FLOW;
	if (cflag & CLOCAL)
		info->flags &= ~ASYNC_CHECK_CD;
	else
		info->flags |= ASYNC_CHECK_CD;

	/*
	 * Set up parity check flag
	 */
#define RELEVENT_IFLAG(iflag)	((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))

	info->read_status_mask = AMBA_UARTRSR_OE;
	if (I_INPCK(info->tty))
		info->read_status_mask |= AMBA_UARTRSR_FE | AMBA_UARTRSR_PE;
	if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
		info->read_status_mask |= AMBA_UARTRSR_BE;

	/*
	 * Characters to ignore
	 */
	info->ignore_status_mask = 0;
	if (I_IGNPAR(info->tty))
		info->ignore_status_mask |= AMBA_UARTRSR_FE | AMBA_UARTRSR_PE;
	if (I_IGNBRK(info->tty)) {
		info->ignore_status_mask |= AMBA_UARTRSR_BE;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns to (for real raw support).
		 */
		if (I_IGNPAR(info->tty))
			info->ignore_status_mask |= AMBA_UARTRSR_OE;
	}

	/* first, disable everything */
	save_flags(flags); cli();
	old_cr = UART_GET_CR(info->port) &= ~AMBA_UARTCR_MSIE;

	if ((info->flags & ASYNC_HARDPPS_CD) ||
	    (cflag & CRTSCTS) ||
	    !(cflag & CLOCAL))
		old_cr |= AMBA_UARTCR_MSIE;

	UART_PUT_CR(info->port, 0);
	restore_flags(flags);

	/* Set baud rate */
	UART_PUT_LCRM(info->port, ((quot & 0xf00) >> 8));
	UART_PUT_LCRL(info->port, (quot & 0xff));

	/*
	 * ----------v----------v----------v----------v-----
	 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
	 * ----------^----------^----------^----------^-----
	 */
	UART_PUT_LCRH(info->port, lcr_h);
	UART_PUT_CR(info->port, old_cr);
}

static void ambauart_put_char(struct tty_struct *tty, u_char ch)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

	if (!tty || !info->xmit.buf)
		return;

	save_flags(flags); cli();
	if (CIRC_SPACE(info->xmit.head, info->xmit.tail, AMBA_XMIT_SIZE) != 0) {
		info->xmit.buf[info->xmit.head] = ch;
		info->xmit.head = (info->xmit.head + 1) & (AMBA_XMIT_SIZE - 1);
	}
	restore_flags(flags);
}

static void ambauart_flush_chars(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

	if (info->xmit.head == info->xmit.tail
	    || tty->stopped
	    || tty->hw_stopped
	    || !info->xmit.buf)
		return;

	save_flags(flags); cli();
	ambauart_enable_tx_interrupt(info);
	restore_flags(flags);
}

static int ambauart_write(struct tty_struct *tty, int from_user,
			  const u_char * buf, int count)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;
	int c, ret = 0;

	if (!tty || !info->xmit.buf || !tmp_buf)
		return 0;

	save_flags(flags);
	if (from_user) {
		down(&tmp_buf_sem);
		while (1) {
			int c1;
			c = CIRC_SPACE_TO_END(info->xmit.head,
					      info->xmit.tail,
					      AMBA_XMIT_SIZE);
			if (count < c)
				c = count;
			if (c <= 0)
				break;

			c -= copy_from_user(tmp_buf, buf, c);
			if (!c) {
				if (!ret)
					ret = -EFAULT;
				break;
			}
			cli();
			c1 = CIRC_SPACE_TO_END(info->xmit.head,
					       info->xmit.tail,
					       AMBA_XMIT_SIZE);
			if (c1 < c)
				c = c1;
			memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
			info->xmit.head = (info->xmit.head + c) &
					  (AMBA_XMIT_SIZE - 1);
			restore_flags(flags);
			buf += c;
			count -= c;
			ret += c;
		}
		up(&tmp_buf_sem);
	} else {
		cli();
		while (1) {
			c = CIRC_SPACE_TO_END(info->xmit.head,
					      info->xmit.tail,
					      AMBA_XMIT_SIZE);
			if (count < c)
				c = count;
			if (c <= 0)
				break;
			memcpy(info->xmit.buf + info->xmit.head, buf, c);
			info->xmit.head = (info->xmit.head + c) &
					  (AMBA_XMIT_SIZE - 1);
			buf += c;
			count -= c;
			ret += c;
		}
		restore_flags(flags);
	}
	if (info->xmit.head != info->xmit.tail
	    && !tty->stopped
	    && !tty->hw_stopped)
		ambauart_enable_tx_interrupt(info);
	return ret;
}

static int ambauart_write_room(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;

	return CIRC_SPACE(info->xmit.head, info->xmit.tail, AMBA_XMIT_SIZE);
}

static int ambauart_chars_in_buffer(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;

	return CIRC_CNT(info->xmit.head, info->xmit.tail, AMBA_XMIT_SIZE);
}

static void ambauart_flush_buffer(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

#if DEBUG
	printk("ambauart_flush_buffer(%d) called\n",
	       MINOR(tty->device) - tty->driver.minor_start);
#endif
	save_flags(flags); cli();
	info->xmit.head = info->xmit.tail = 0;
	restore_flags(flags);
	wake_up_interruptible(&tty->write_wait);
	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
}

/*
 * This function is used to send a high-priority XON/XOFF character to
 * the device
 */
static void ambauart_send_xchar(struct tty_struct *tty, char ch)
{
	struct amba_info *info = tty->driver_data;

	info->x_char = ch;
	if (ch)
		ambauart_enable_tx_interrupt(info);
}

static void ambauart_throttle(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;

	if (I_IXOFF(tty))
		ambauart_send_xchar(tty, STOP_CHAR(tty));

	if (tty->termios->c_cflag & CRTSCTS) {
		save_flags(flags); cli();
		info->mctrl &= ~TIOCM_RTS;
		info->port->set_mctrl(info->port, info->mctrl);
		restore_flags(flags);
	}
}

static void ambauart_unthrottle(struct tty_struct *tty)
{
	struct amba_info *info = (struct amba_info *) tty->driver_data;
	unsigned long flags;

	if (I_IXOFF(tty)) {
		if (info->x_char)
			info->x_char = 0;
		else
			ambauart_send_xchar(tty, START_CHAR(tty));
	}

	if (tty->termios->c_cflag & CRTSCTS) {
		save_flags(flags); cli();
		info->mctrl |= TIOCM_RTS;
		info->port->set_mctrl(info->port, info->mctrl);
		restore_flags(flags);
	}
}

static int get_serial_info(struct amba_info *info, struct serial_struct *retinfo)
{
	struct amba_state *state = info->state;
	struct amba_port *port = info->port;
	struct serial_struct tmp;

	memset(&tmp, 0, sizeof(tmp));
	tmp.type	   = 0;
	tmp.line	   = state->line;
	tmp.port	   = port->uart_base;
	if (HIGH_BITS_OFFSET)
		tmp.port_high = port->uart_base >> HIGH_BITS_OFFSET;
	tmp.irq		   = port->irq;
	tmp.flags	   = 0;
	tmp.xmit_fifo_size = port->fifosize;
	tmp.baud_base	   = port->uartclk / 16;
	tmp.close_delay	   = state->close_delay;
	tmp.closing_wait   = state->closing_wait;
	tmp.custom_divisor = state->custom_divisor;

	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return 0;
}

static int set_serial_info(struct amba_info *info,
			   struct serial_struct *newinfo)
{
	struct serial_struct new_serial;
	struct amba_state *state, old_state;
	struct amba_port *port;
	unsigned long new_port;
	unsigned int i, change_irq, change_port;
	int retval = 0;

	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
		return -EFAULT;

	state = info->state;
	old_state = *state;
	port = info->port;

	new_port = new_serial.port;
	if (HIGH_BITS_OFFSET)
		new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;

	change_irq  = new_serial.irq != port->irq;
	change_port = new_port != port->uart_base;

	if (!capable(CAP_SYS_ADMIN)) {
		if (change_irq || change_port ||
		    (new_serial.baud_base != port->uartclk / 16) ||
		    (new_serial.close_delay != state->close_delay) ||
		    (new_serial.xmit_fifo_size != port->fifosize) ||
		    ((new_serial.flags & ~ASYNC_USR_MASK) !=
		     (state->flags & ~ASYNC_USR_MASK)))
			return -EPERM;
		state->flags = ((state->flags & ~ASYNC_USR_MASK) |
				(new_serial.flags & ASYNC_USR_MASK));
		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
			       (new_serial.flags & ASYNC_USR_MASK));
		state->custom_divisor = new_serial.custom_divisor;
		goto check_and_exit;
	}

	if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
	    (new_serial.baud_base < 9600))
		return -EINVAL;

	if (new_serial.type && change_port) {
		for (i = 0; i < SERIAL_AMBA_NR; i++)
			if ((port != amba_ports + i) &&
			    amba_ports[i].uart_base != new_port)
				return -EADDRINUSE;
	}

	if ((change_port || change_irq) && (state->count > 1))
		return -EBUSY;

	/*
	 * OK, past this point, all the error checking has been done.
	 * At this point, we start making changes.....
	 */
	port->uartclk = new_serial.baud_base * 16;
	state->flags = ((state->flags & ~ASYNC_FLAGS) |
			(new_serial.flags & ASYNC_FLAGS));
	info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
		       (info->flags & ASYNC_INTERNAL_FLAGS));
	state->custom_divisor = new_serial.custom_divisor;
	state->close_delay = new_serial.close_delay * HZ / 100;
	state->closing_wait = new_serial.closing_wait * HZ / 100;
	info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
	port->fifosize = new_serial.xmit_fifo_size;

	if (change_port || change_irq) {
		/*
		 * We need to shutdown the serial port at the old
		 * port/irq combination.
		 */
		ambauart_shutdown(info);
		port->irq = new_serial.irq;
		port->uart_base = new_port;
	}

check_and_exit:
	if (!port->uart_base)
		return 0;
	if (info->flags & ASYNC_INITIALIZED) {
		if ((old_state.flags & ASYNC_SPD_MASK) !=
		    (state->flags & ASYNC_SPD_MASK) ||
		    (old_state.custom_divisor != state->custom_divisor)) {
			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
				info->tty->alt_speed = 57600;
			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
				info->tty->alt_speed = 115200;
			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
				info->tty->alt_speed = 230400;
			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
				info->tty->alt_speed = 460800;
			ambauart_change_speed(info, NULL);
		}
	} else
		retval = ambauart_startup(info);
	return retval;
}


/*
 * get_lsr_info - get line status register info
 */
static int get_lsr_info(struct amba_info *info, unsigned int *value)
{
	unsigned int result, status;
	unsigned long flags;

	save_flags(flags); cli();
	status = UART_GET_FR(info->port);
	restore_flags(flags);
	result = status & AMBA_UARTFR_BUSY ? TIOCSER_TEMT : 0;

	/*
	 * If we're about to load something into the transmit
	 * register, we'll pretend the transmitter isn't empty to
	 * avoid a race condition (depending on when the transmit
	 * interrupt happens).
	 */
	if (info->x_char ||
	    ((CIRC_CNT(info->xmit.head, info->xmit.tail,
		       AMBA_XMIT_SIZE) > 0) &&
	     !info->tty->stopped && !info->tty->hw_stopped))
		result &= TIOCSER_TEMT;
	
	return put_user(result, value);
}

static int get_modem_info(struct amba_info *info, unsigned int *value)
{
	unsigned int result = info->mctrl;
	unsigned int status;

	status = UART_GET_FR(info->port);
	if (status & AMBA_UARTFR_DCD)
		result |= TIOCM_CAR;
	if (status & AMBA_UARTFR_DSR)
		result |= TIOCM_DSR;
	if (status & AMBA_UARTFR_CTS)
		result |= TIOCM_CTS;

	return put_user(result, value);
}

static int set_modem_info(struct amba_info *info, unsigned int cmd,
			  unsigned int *value)
{
	unsigned int arg, old;
	unsigned long flags;

	if (get_user(arg, value))
		return -EFAULT;

	old = info->mctrl;
	switch (cmd) {
	case TIOCMBIS:
		info->mctrl |= arg;
		break;

	case TIOCMBIC:
		info->mctrl &= ~arg;
		break;

	case TIOCMSET:
		info->mctrl = arg;
		break;

	default:
		return -EINVAL;
	}
	save_flags(flags); cli();
	if (old != info->mctrl)
		info->port->set_mctrl(info->port, info->mctrl);
	restore_flags(flags);
	return 0;
}

static void ambauart_break_ctl(struct tty_struct *tty, int break_state)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;
	unsigned int lcr_h;

	save_flags(flags); cli();
	lcr_h = UART_GET_LCRH(info->port);
	if (break_state == -1)
		lcr_h |= AMBA_UARTLCR_H_BRK;
	else
		lcr_h &= ~AMBA_UARTLCR_H_BRK;
	UART_PUT_LCRH(info->port, lcr_h);
	restore_flags(flags);
}

static int ambauart_ioctl(struct tty_struct *tty, struct file *file,
			   unsigned int cmd, unsigned long arg)
{
	struct amba_info *info = tty->driver_data;
	struct amba_icount cprev, cnow;
	struct serial_icounter_struct icount;
	unsigned long flags;

	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
		if (tty->flags & (1 << TTY_IO_ERROR))
			return -EIO;
	}

	switch (cmd) {
		case TIOCMGET:
			return get_modem_info(info, (unsigned int *)arg);
		case TIOCMBIS:
		case TIOCMBIC:
		case TIOCMSET:
			return set_modem_info(info, cmd, (unsigned int *)arg);
		case TIOCGSERIAL:
			return get_serial_info(info,
					       (struct serial_struct *)arg);
		case TIOCSSERIAL:
			return set_serial_info(info,
					       (struct serial_struct *)arg);
		case TIOCSERGETLSR: /* Get line status register */
			return get_lsr_info(info, (unsigned int *)arg);
		/*
		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
		 * - mask passed in arg for lines of interest
		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
		 * Caller should use TIOCGICOUNT to see which one it was
		 */
		case TIOCMIWAIT:
			save_flags(flags); cli();
			/* note the counters on entry */
			cprev = info->state->icount;
			/* Force modem status interrupts on */
			UART_PUT_CR(info->port, UART_GET_CR(info->port) | AMBA_UARTCR_MSIE);
			restore_flags(flags);
			while (1) {
				interruptible_sleep_on(&info->delta_msr_wait);
				/* see if a signal did it */
				if (signal_pending(current))
					return -ERESTARTSYS;
				save_flags(flags); cli();
				cnow = info->state->icount; /* atomic copy */
				restore_flags(flags);
				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
					return -EIO; /* no change => error */
				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
					return 0;
				}
				cprev = cnow;
			}
			/* NOTREACHED */

		/*
		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
		 * Return: write counters to the user passed counter struct
		 * NB: both 1->0 and 0->1 transitions are counted except for
		 *     RI where only 0->1 is counted.
		 */
		case TIOCGICOUNT:
			save_flags(flags); cli();
			cnow = info->state->icount;
			restore_flags(flags);
			icount.cts = cnow.cts;
			icount.dsr = cnow.dsr;
			icount.rng = cnow.rng;
			icount.dcd = cnow.dcd;
			icount.rx  = cnow.rx;
			icount.tx  = cnow.tx;
			icount.frame = cnow.frame;
			icount.overrun = cnow.overrun;
			icount.parity = cnow.parity;
			icount.brk = cnow.brk;
			icount.buf_overrun = cnow.buf_overrun;

			return copy_to_user((void *)arg, &icount, sizeof(icount))
					? -EFAULT : 0;

		default:
			return -ENOIOCTLCMD;
	}
	return 0;
}

static void ambauart_set_termios(struct tty_struct *tty, struct termios *old_termios)
{
	struct amba_info *info = tty->driver_data;
	unsigned long flags;
	unsigned int cflag = tty->termios->c_cflag;

	if ((cflag ^ old_termios->c_cflag) == 0 &&
	    RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
		return;

	ambauart_change_speed(info, old_termios);

	/* Handle transition to B0 status */
	if ((old_termios->c_cflag & CBAUD) &&
	    !(cflag & CBAUD)) {
		save_flags(flags); cli();
		info->mctrl &= ~(TIOCM_RTS | TIOCM_DTR);
		info->port->set_mctrl(info->port, info->mctrl);
		restore_flags(flags);
	}

	/* Handle transition away from B0 status */
	if (!(old_termios->c_cflag & CBAUD) &&
	    (cflag & CBAUD)) {
		save_flags(flags); cli();
		info->mctrl |= TIOCM_DTR;
		if (!(cflag & CRTSCTS) ||
		    !test_bit(TTY_THROTTLED, &tty->flags))
			info->mctrl |= TIOCM_RTS;
		info->port->set_mctrl(info->port, info->mctrl);
		restore_flags(flags);
	}

	/* Handle turning off CRTSCTS */
	if ((old_termios->c_cflag & CRTSCTS) &&
	    !(cflag & CRTSCTS)) {
		tty->hw_stopped = 0;
		ambauart_start(tty);
	}

#if 0
	/*
	 * No need to wake up processes in open wait, since they
	 * sample the CLOCAL flag once, and don't recheck it.
	 * XXX  It's not clear whether the current behavior is correct
	 * or not.  Hence, this may change.....
	 */
	if (!(old_termios->c_cflag & CLOCAL) &&
	    (tty->termios->c_cflag & CLOCAL))
		wake_up_interruptible(&info->open_wait);
#endif
}

static void ambauart_close(struct tty_struct *tty, struct file *filp)
{
	struct amba_info *info = tty->driver_data;
	struct amba_state *state;
	unsigned long flags;

	if (!info)
		return;

	state = info->state;

#if DEBUG
	printk("ambauart_close() called\n");
#endif

	save_flags(flags); cli();

	if (tty_hung_up_p(filp)) {
		MOD_DEC_USE_COUNT;
		restore_flags(flags);
		return;
	}

	if ((tty->count == 1) && (state->count != 1)) {
		/*
		 * Uh, oh.  tty->count is 1, which means that the tty
		 * structure will be freed.  state->count should always
		 * be one in these conditions.  If it's greater than
		 * one, we've got real problems, since it means the
		 * serial port won't be shutdown.
		 */
		printk("ambauart_close: bad serial port count; tty->count is 1, "
		       "state->count is %d\n", state->count);
		state->count = 1;
	}
	if (--state->count < 0) {
		printk("rs_close: bad serial port count for %s%d: %d\n",
		       tty->driver.name, info->state->line, state->count);
		state->count = 0;
	}
	if (state->count) {
		MOD_DEC_USE_COUNT;
		restore_flags(flags);
		return;
	}
	info->flags |= ASYNC_CLOSING;
	restore_flags(flags);
	/*
	 * Save the termios structure, since this port may have
	 * separate termios for callout and dialin.
	 */
	if (info->flags & ASYNC_NORMAL_ACTIVE)
		info->state->normal_termios = *tty->termios;
	if (info->flags & ASYNC_CALLOUT_ACTIVE)
		info->state->callout_termios = *tty->termios;
	/*
	 * Now we wait for the transmit buffer to clear; and we notify
	 * the line discipline to only process XON/XOFF characters.
	 */
	tty->closing = 1;
	if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
		tty_wait_until_sent(tty, info->state->closing_wait);
	/*
	 * At this point, we stop accepting input.  To do this, we
	 * disable the receive line status interrupts.
	 */
	if (info->flags & ASYNC_INITIALIZED) {
		ambauart_disable_rx_interrupt(info);
		/*
		 * Before we drop DTR, make sure the UART transmitter
		 * has completely drained; this is especially
		 * important if there is a transmit FIFO!
		 */
		ambauart_wait_until_sent(tty, info->timeout);
	}
	ambauart_shutdown(info);
	if (tty->driver.flush_buffer)
		tty->driver.flush_buffer(tty);
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	tty->closing = 0;
	info->event = 0;
	info->tty = NULL;
	if (info->blocked_open) {
		if (info->state->close_delay) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(info->state->close_delay);
		}
		wake_up_interruptible(&info->open_wait);
	}
	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
			 ASYNC_CLOSING);
	wake_up_interruptible(&info->close_wait);
	MOD_DEC_USE_COUNT;
}

static void ambauart_wait_until_sent(struct tty_struct *tty, int timeout)
{
	struct amba_info *info = (struct amba_info *) tty->driver_data;
	unsigned long char_time, expire;
	unsigned int status;

	if (info->port->fifosize == 0)
		return;

	/*
	 * Set the check interval to be 1/5 of the estimated time to
	 * send a single character, and make it at least 1.  The check
	 * interval should also be less than the timeout.
	 *
	 * Note: we have to use pretty tight timings here to satisfy
	 * the NIST-PCTS.
	 */
	char_time = (info->timeout - HZ/50) / info->port->fifosize;
	char_time = char_time / 5;
	if (char_time == 0)
		char_time = 1;
	if (timeout && timeout < char_time)
		char_time = timeout;
	/*
	 * If the transmitter hasn't cleared in twice the approximate
	 * amount of time to send the entire FIFO, it probably won't
	 * ever clear.  This assumes the UART isn't doing flow
	 * control, which is currently the case.  Hence, if it ever
	 * takes longer than info->timeout, this is probably due to a
	 * UART bug of some kind.  So, we clamp the timeout parameter at
	 * 2*info->timeout.
	 */
	if (!timeout || timeout > 2 * info->timeout)
		timeout = 2 * info->timeout;

	expire = jiffies + timeout;
#if DEBUG
	printk("ambauart_wait_until_sent(%d), jiff=%lu, expire=%lu...\n",
	       MINOR(tty->device) - tty->driver.minor_start, jiffies,
	       expire);
#endif
	while (UART_GET_FR(info->port) & AMBA_UARTFR_BUSY) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(char_time);
		if (signal_pending(current))
			break;
		if (timeout && time_after(jiffies, expire))
			break;
		status = UART_GET_FR(info->port);
	}
	set_current_state(TASK_RUNNING);
}

static void ambauart_hangup(struct tty_struct *tty)
{
	struct amba_info *info = tty->driver_data;
	struct amba_state *state = info->state;

	ambauart_flush_buffer(tty);
	if (info->flags & ASYNC_CLOSING)
		return;
	ambauart_shutdown(info);
	info->event = 0;
	state->count = 0;
	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
	info->tty = NULL;
	wake_up_interruptible(&info->open_wait);
}

static int block_til_ready(struct tty_struct *tty, struct file *filp,
			   struct amba_info *info)
{
	DECLARE_WAITQUEUE(wait, current);
	struct amba_state *state = info->state;
	unsigned long flags;
	int do_clocal = 0, extra_count = 0, retval;

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
	if (tty_hung_up_p(filp) ||
	    (info->flags & ASYNC_CLOSING)) {
		if (info->flags & ASYNC_CLOSING)
			interruptible_sleep_on(&info->close_wait);
		return (info->flags & ASYNC_HUP_NOTIFY) ?
			-EAGAIN : -ERESTARTSYS;
	}

	/*
	 * If this is a callout device, then just make sure the normal
	 * device isn't being used.
	 */
	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
		if (info->flags & ASYNC_NORMAL_ACTIVE)
			return -EBUSY;
		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
		    (info->session != current->session))
			return -EBUSY;
		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
		    (info->pgrp != current->pgrp))
			return -EBUSY;
		info->flags |= ASYNC_CALLOUT_ACTIVE;
		return 0;
	}

	/*
	 * If non-blocking mode is set, or the port is not enabled,
	 * then make the check up front and then exit.
	 */
	if ((filp->f_flags & O_NONBLOCK) ||
	    (tty->flags & (1 << TTY_IO_ERROR))) {
		if (info->flags & ASYNC_CALLOUT_ACTIVE)
			return -EBUSY;
		info->flags |= ASYNC_NORMAL_ACTIVE;
		return 0;
	}

	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
		if (state->normal_termios.c_cflag & CLOCAL)
			do_clocal = 1;
	} else {
		if (tty->termios->c_cflag & CLOCAL)
			do_clocal = 1;
	}

	/*
	 * Block waiting for the carrier detect and the line to become
	 * free (i.e., not in use by the callout).  While we are in
	 * this loop, state->count is dropped by one, so that
	 * rs_close() knows when to free things.  We restore it upon
	 * exit, either normal or abnormal.
	 */
	retval = 0;
	add_wait_queue(&info->open_wait, &wait);
	save_flags(flags); cli();
	if (!tty_hung_up_p(filp)) {
		extra_count = 1;
		state->count--;
	}
	restore_flags(flags);
	info->blocked_open++;
	while (1) {
		save_flags(flags); cli();
		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
		    (tty->termios->c_cflag & CBAUD)) {
			info->mctrl = TIOCM_DTR | TIOCM_RTS;
			info->port->set_mctrl(info->port, info->mctrl);
		}
		restore_flags(flags);
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
		    !(info->flags & ASYNC_INITIALIZED)) {
			if (info->flags & ASYNC_HUP_NOTIFY)
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;
			break;
		}
		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
		    !(info->flags & ASYNC_CLOSING) &&
		    (do_clocal || (UART_GET_FR(info->port) & AMBA_UARTFR_DCD)))
			break;
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&info->open_wait, &wait);
	if (extra_count)
		state->count++;
	info->blocked_open--;
	if (retval)
		return retval;
	info->flags |= ASYNC_NORMAL_ACTIVE;
	return 0;
}

static struct amba_info *ambauart_get(int line)
{
	struct amba_info *info;
	struct amba_state *state = amba_state + line;

	state->count++;
	if (state->info)
		return state->info;
	info = kmalloc(sizeof(struct amba_info), GFP_KERNEL);
	if (info) {
		memset(info, 0, sizeof(struct amba_info));
		init_waitqueue_head(&info->open_wait);
		init_waitqueue_head(&info->close_wait);
		init_waitqueue_head(&info->delta_msr_wait);
		info->flags = state->flags;
		info->state = state;
		info->port  = amba_ports + line;
		tasklet_init(&info->tlet, ambauart_tasklet_action,
			     (unsigned long)info);
	}
	if (state->info) {
		kfree(info);
		return state->info;
	}
	state->info = info;
	return info;
}

static int ambauart_open(struct tty_struct *tty, struct file *filp)
{
	struct amba_info *info;
	int retval, line = MINOR(tty->device) - tty->driver.minor_start;

#if DEBUG
	printk("ambauart_open(%d) called\n", line);
#endif

	// is this a line that we've got?
	MOD_INC_USE_COUNT;
	if (line >= SERIAL_AMBA_NR) {
		MOD_DEC_USE_COUNT;
		return -ENODEV;
	}

	info = ambauart_get(line);
	if (!info)
		return -ENOMEM;

	tty->driver_data = info;
	info->tty = tty;
	info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;

	/*
	 * Make sure we have the temporary buffer allocated
	 */
	if (!tmp_buf) {
		unsigned long page = get_zeroed_page(GFP_KERNEL);
		if (tmp_buf)
			free_page(page);
		else if (!page) {
			MOD_DEC_USE_COUNT;
			return -ENOMEM;
		}
		tmp_buf = (u_char *)page;
	}

	/*
	 * If the port is in the middle of closing, bail out now.
	 */
	if (tty_hung_up_p(filp) ||
	    (info->flags & ASYNC_CLOSING)) {
		if (info->flags & ASYNC_CLOSING)
			interruptible_sleep_on(&info->close_wait);
		MOD_DEC_USE_COUNT;
		return -EAGAIN;
	}

	/*
	 * Start up the serial port
	 */
	retval = ambauart_startup(info);
	if (retval) {
		MOD_DEC_USE_COUNT;
		return retval;
	}

	retval = block_til_ready(tty, filp, info);
	if (retval) {
		MOD_DEC_USE_COUNT;
		return retval;
	}

	if ((info->state->count == 1) &&
	    (info->flags & ASYNC_SPLIT_TERMIOS)) {
		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
			*tty->termios = info->state->normal_termios;
		else
			*tty->termios = info->state->callout_termios;
	}
#ifdef CONFIG_SERIAL_AMBA_CONSOLE
	if (ambauart_cons.cflag && ambauart_cons.index == line) {
		tty->termios->c_cflag = ambauart_cons.cflag;
		ambauart_cons.cflag = 0;
	}
#endif
	ambauart_change_speed(info, NULL);
	info->session = current->session;
	info->pgrp = current->pgrp;
	return 0;
}

int __init ambauart_init(void)
{
	int i;

	ambanormal_driver.magic = TTY_DRIVER_MAGIC;
	ambanormal_driver.driver_name = "serial_amba";
	ambanormal_driver.name = SERIAL_AMBA_NAME;
	ambanormal_driver.major = SERIAL_AMBA_MAJOR;
	ambanormal_driver.minor_start = SERIAL_AMBA_MINOR;
	ambanormal_driver.num = SERIAL_AMBA_NR;
	ambanormal_driver.type = TTY_DRIVER_TYPE_SERIAL;
	ambanormal_driver.subtype = SERIAL_TYPE_NORMAL;
	ambanormal_driver.init_termios = tty_std_termios;
	ambanormal_driver.init_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
	ambanormal_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
	ambanormal_driver.refcount = &ambauart_refcount;
	ambanormal_driver.table = ambauart_table;
	ambanormal_driver.termios = ambauart_termios;
	ambanormal_driver.termios_locked = ambauart_termios_locked;

	ambanormal_driver.open = ambauart_open;
	ambanormal_driver.close = ambauart_close;
	ambanormal_driver.write = ambauart_write;
	ambanormal_driver.put_char = ambauart_put_char;
	ambanormal_driver.flush_chars = ambauart_flush_chars;
	ambanormal_driver.write_room = ambauart_write_room;
	ambanormal_driver.chars_in_buffer = ambauart_chars_in_buffer;
	ambanormal_driver.flush_buffer	= ambauart_flush_buffer;
	ambanormal_driver.ioctl = ambauart_ioctl;
	ambanormal_driver.throttle = ambauart_throttle;
	ambanormal_driver.unthrottle = ambauart_unthrottle;
	ambanormal_driver.send_xchar = ambauart_send_xchar;
	ambanormal_driver.set_termios = ambauart_set_termios;
	ambanormal_driver.stop = ambauart_stop;
	ambanormal_driver.start = ambauart_start;
	ambanormal_driver.hangup = ambauart_hangup;
	ambanormal_driver.break_ctl = ambauart_break_ctl;
	ambanormal_driver.wait_until_sent = ambauart_wait_until_sent;
	ambanormal_driver.read_proc = NULL;

	/*
	 * The callout device is just like the normal device except for
	 * the major number and the subtype code.
	 */
	ambacallout_driver = ambanormal_driver;
	ambacallout_driver.name = CALLOUT_AMBA_NAME;
	ambacallout_driver.major = CALLOUT_AMBA_MAJOR;
	ambacallout_driver.subtype = SERIAL_TYPE_CALLOUT;
	ambacallout_driver.read_proc = NULL;
	ambacallout_driver.proc_entry = NULL;

	if (tty_register_driver(&ambanormal_driver))
		panic("Couldn't register AMBA serial driver\n");
	if (tty_register_driver(&ambacallout_driver))
		panic("Couldn't register AMBA callout driver\n");

	for (i = 0; i < SERIAL_AMBA_NR; i++) {
		struct amba_state *state = amba_state + i;
		state->line		= i;
		state->close_delay	= 5 * HZ / 10;
		state->closing_wait	= 30 * HZ;
		state->callout_termios	= ambacallout_driver.init_termios;
		state->normal_termios	= ambanormal_driver.init_termios;
	}

	return 0;
}

__initcall(ambauart_init);

#ifdef CONFIG_SERIAL_AMBA_CONSOLE
/************** console driver *****************/

/*
 * This code is currently never used; console->read is never called.
 * Therefore, although we have an implementation, we don't use it.
 * FIXME: the "const char *s" should be fixed to "char *s" some day.
 * (when the definition in include/linux/console.h is also fixed)
 */
#ifdef used_and_not_const_char_pointer
static int ambauart_console_read(struct console *co, const char *s, u_int count)
{
	struct amba_port *port = &amba_ports[co->index];
	unsigned int status;
	char *w;
	int c;
#if DEBUG
	printk("ambauart_console_read() called\n");
#endif

	c = 0;
	w = s;
	while (c < count) {
		status = UART_GET_FR(port);
		if (UART_RX_DATA(status)) {
			*w++ = UART_GET_CHAR(port);
			c++;
		} else {
			// nothing more to get, return
			return c;
		}
	}
	// return the count
	return c;
}
#endif

/*
 *	Print a string to the serial port trying not to disturb
 *	any possible real use of the port...
 *
 *	The console_lock must be held when we get here.
 */
static void ambauart_console_write(struct console *co, const char *s, u_int count)
{
	struct amba_port *port = &amba_ports[co->index];
	unsigned int status, old_cr;
	int i;

	/*
	 *	First save the CR then disable the interrupts
	 */
	old_cr = UART_GET_CR(port);
	UART_PUT_CR(port, AMBA_UARTCR_UARTEN);

	/*
	 *	Now, do each character
	 */
	for (i = 0; i < count; i++) {
		do {
			status = UART_GET_FR(port);
		} while (!UART_TX_READY(status));
		UART_PUT_CHAR(port, s[i]);
		if (s[i] == '\n') {
			do {
				status = UART_GET_FR(port);
			} while (!UART_TX_READY(status));
			UART_PUT_CHAR(port, '\r');
		}
	}

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the TCR
	 */
	do {
		status = UART_GET_FR(port);
	} while (status & AMBA_UARTFR_BUSY);
	UART_PUT_CR(port, old_cr);
}

/*
 *	Receive character from the serial port
 */
static int ambauart_console_wait_key(struct console *co)
{
	struct amba_port *port = &amba_ports[co->index];
	unsigned int status;
	int c;

	do {
		status = UART_GET_FR(port);
	} while (!UART_RX_DATA(status));
	c = UART_GET_CHAR(port);
	return c;
}

static kdev_t ambauart_console_device(struct console *c)
{
	return MKDEV(SERIAL_AMBA_MAJOR, SERIAL_AMBA_MINOR + c->index);
}

static int __init ambauart_console_setup(struct console *co, char *options)
{
	struct amba_port *port;
	int baud = 38400;
	int bits = 8;
	int parity = 'n';
	u_int cflag = CREAD | HUPCL | CLOCAL;
	u_int lcr_h, quot;

	if (co->index >= SERIAL_AMBA_NR)
		co->index = 0;

	port = &amba_ports[co->index];

	if (options) {
		char *s = options;
		baud = simple_strtoul(s, NULL, 10);
		while (*s >= '0' && *s <= '9')
			s++;
		if (*s) parity = *s++;
		if (*s) bits = *s - '0';
	}

	/*
	 *    Now construct a cflag setting.
	 */
	switch (baud) {
	case 1200:	cflag |= B1200;			break;
	case 2400:	cflag |= B2400;			break;
	case 4800:	cflag |= B4800;			break;
	default:	cflag |= B9600;   baud = 9600;	break;
	case 19200:	cflag |= B19200;		break;
	case 38400:	cflag |= B38400;		break;
	case 57600:	cflag |= B57600;		break;
	case 115200:	cflag |= B115200;		break;
	}
	switch (bits) {
	case 7:	  cflag |= CS7;	lcr_h = AMBA_UARTLCR_H_WLEN_7;	break;
	default:  cflag |= CS8;	lcr_h = AMBA_UARTLCR_H_WLEN_8;	break;
	}
	switch (parity) {
	case 'o':
	case 'O': cflag |= PARODD; lcr_h |= AMBA_UARTLCR_H_PEN;	break;
	case 'e':
	case 'E': cflag |= PARENB; lcr_h |= AMBA_UARTLCR_H_PEN |
					    AMBA_UARTLCR_H_EPS; break;
	}

	co->cflag = cflag;

	if (port->fifosize > 1)
		lcr_h |= AMBA_UARTLCR_H_FEN;

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

	UART_PUT_LCRL(port, (quot & 0xff));
	UART_PUT_LCRM(port, (quot >> 8));
	UART_PUT_LCRH(port, lcr_h);

	/* we will enable the port as we need it */
	UART_PUT_CR(port, 0);

	return 0;
}

static struct console ambauart_cons =
{
	name:		SERIAL_AMBA_NAME,
	write:		ambauart_console_write,
#ifdef used_and_not_const_char_pointer
	read:		ambauart_console_read,
#endif
	device:		ambauart_console_device,
	wait_key:	ambauart_console_wait_key,
	setup:		ambauart_console_setup,
	flags:		CON_PRINTBUFFER,
	index:		-1,
};

void __init ambauart_console_init(void)
{
	register_console(&ambauart_cons);
}

#endif /* CONFIG_SERIAL_AMBA_CONSOLE */