summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/altera-cyclone-v/hwlib/src/hwmgr/alt_16550_uart.c
blob: 7ed75c39d3fbcae3569fbc89344cd0bcb0ef269e (plain) (blame)
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
/******************************************************************************
 *
 * Copyright 2013 Altera Corporation. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 ******************************************************************************/

#include <bsp/alt_16550_uart.h>
#include <bsp/alt_clock_manager.h>
#include <bsp/socal/alt_rstmgr.h>
#include <bsp/socal/alt_uart.h>
#include <bsp/socal/hps.h>
#include <bsp/socal/socal.h>

/////

#define ALT_16550_HANDLE_DATA_UART_ENABLED_MSK   (1UL << 31)
#define ALT_16550_HANDLE_DATA_DIVISOR_VALUE_GET(value) (value & 0xffff)

#define ALT_ALTERA_16550_CPR_OFST        (0xF4)
#define ALT_ALTERA_16550_CPR_ADDR(base)  ALT_CAST(void *, (ALT_CAST(char *, (base)) + ALT_ALTERA_16550_CPR_OFST))
#define ALT_ALTERA_16550_CPR_FIFO_MODE_GET(value) (((value) >> 16) & 0xff)
#define ALT_ALTERA_16550_CPR_AFCE_MODE_SET_MSK (1 << 4)

/////

// Remove these macros as part of case:123835.
#define ALT_UART_IER_DLH_VALUE_SET(value) ((value) & 0xff)
#define ALT_UART_IER_DLH_ETBEI_DLH1_SET_MSK ALT_UART_IER_DLH_ETBEI_DLHL_SET_MSK

/////

//
// Helper function which resets the UART and if requested, initializes the UART
// to the default settings. Currently the default settings are:
//  - 8 databits
//  - no parity
//  - 1 stopbit
//  - 57600 baudrate
// The reset routines depends on the hardware implementation of the UART.
//

// This helper is needed because the regular alt_read_word(src) essentially
// resolves to "*(volatile uint32_t *)src". As there is no assignment, this
// could potentially be optimized away. With the helper, the actual register
// read should occur and be returned (and subsequently discarded).
static inline uint32_t alt_read_word_helper(const void * addr)
{
    return alt_read_word(addr);
}

//
// Helper function write the divisor in hardware.
//
static ALT_STATUS_CODE alt_16550_write_divisor_helper(ALT_16550_HANDLE_t * handle,
                                                      uint32_t divisor)
{
    // Validate the divisor parameter.
    if (divisor > 0xffff)
    {
        // This should never happen as it is verified in divisor_set.
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Set LCR::DLAB (Line Control Register :: Divisor Latch Access Bit)
        alt_setbits_word(ALT_UART_LCR_ADDR(handle->location), ALT_UART_LCR_DLAB_SET_MSK);

        // Write DLL (Divisor Latch Low).
        alt_write_word(ALT_UART_RBR_THR_DLL_ADDR(handle->location), ALT_UART_RBR_THR_DLL_VALUE_SET(divisor));

        // Write DLH (Divisor Latch High).
        alt_write_word(ALT_UART_IER_DLH_ADDR(handle->location), ALT_UART_IER_DLH_VALUE_SET(divisor >> 8));

        // Clear LCR::DLAB (Line Control Register :: Divisor Latch Access Bit)
        alt_clrbits_word(ALT_UART_LCR_ADDR(handle->location), ALT_UART_LCR_DLAB_SET_MSK);

        break;

    default:
        return ALT_E_ERROR;
    }

    // Update the enabled state in the handle data.
    if (divisor != 0)
    {
        handle->data |= ALT_16550_HANDLE_DATA_UART_ENABLED_MSK;
    }
    else
    {
        handle->data &= ~ALT_16550_HANDLE_DATA_UART_ENABLED_MSK;
    }

    return ALT_E_SUCCESS;
}

//
// Helper function to reset the UART.
//
static ALT_STATUS_CODE alt_16550_reset_helper(ALT_16550_HANDLE_t * handle, bool enable_init)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Write SRR::UR (Shadow Reset Register :: UART Reset)
        alt_write_word(ALT_UART_SRR_ADDR(handle->location), ALT_UART_SRR_UR_SET_MSK);

        // Read the MSR to work around case:119085.
        alt_read_word_helper(ALT_UART_MSR_ADDR(handle->location));
        break;

    case ALT_16550_DEVICE_ALTERA_16550_UART:
        alt_16550_write_divisor_helper(handle, 0); // Disable UART
        alt_16550_int_disable_all(handle);         // Disable interrupts
        alt_16550_fifo_disable(handle);            // Disable FIFOs
        alt_write_word(ALT_UART_MCR_ADDR(handle->location), 0); // 0 -> MCR (AFCE, LP, OUT2, OUT1, RTS, DTR)
        break;

    default:
        return ALT_E_ERROR;
    }

    // If we are initializing (as opposed to just uninitializing)
    if (enable_init)
    {
        ALT_STATUS_CODE status;

        // Set bit IER::PTIME (Interrupt Enable Register :: Programmable THRE Mode Enable)
        alt_setbits_word(ALT_UART_IER_DLH_ADDR(handle->location), ALT_UART_IER_DLH_PTIME_DLH7_SET_MSK);

        // Set the line configuration to use 8-N-1.
        status = alt_16550_line_config_set(handle, ALT_16550_DATABITS_8,
                                                   ALT_16550_PARITY_DISABLE,
                                                   ALT_16550_STOPBITS_1);
        if (status != ALT_E_SUCCESS)
        {
            return status;
        }

        uint32_t divisor = ALT_16550_HANDLE_DATA_DIVISOR_VALUE_GET(handle->data);
        if (divisor == 0)
        {
            // Set the default baudrate to 57600.
            status = alt_16550_baudrate_set(handle, ALT_16550_BAUDRATE_57600);
            if (status != ALT_E_SUCCESS)
            {
                return status;
            }
        }
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_init(ALT_16550_DEVICE_t device,
                               void * location,
                               alt_freq_t clock_freq,
                               ALT_16550_HANDLE_t * handle)
{
    handle->device = device;
    handle->data   = 0;
    handle->fcr    = 0;

    switch (device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // The ALT_CLK_L4_SP is required for all SoCFPGA UARTs. Check that it's enabled.
        if (alt_clk_is_enabled(ALT_CLK_L4_SP) != ALT_E_TRUE)
        {
            return ALT_E_BAD_CLK;
        }
        else
        {
            ALT_STATUS_CODE status;
            status = alt_clk_freq_get(ALT_CLK_L4_SP, &handle->clock_freq);
            if (status != ALT_E_SUCCESS)
            {
                return status;
            }

            if (device == ALT_16550_DEVICE_SOCFPGA_UART0)
            {
                handle->location = ALT_UART0_ADDR;

                // Bring UART0 out of reset.
                alt_clrbits_word(ALT_RSTMGR_PERMODRST_ADDR, ALT_RSTMGR_PERMODRST_UART0_SET_MSK);
            }
            else // device == ALT_16550_DEVICE_SOCFPGA_UART1
            {
                handle->location = ALT_UART1_ADDR;

                // Bring UART1 out of reset.
                alt_clrbits_word(ALT_RSTMGR_PERMODRST_ADDR, ALT_RSTMGR_PERMODRST_UART1_SET_MSK);
            } 

            // Verify the UCR (UART Component Version)
            uint32_t ucr = alt_read_word(ALT_UART_UCV_ADDR(handle->location));
            if (ucr != ALT_UART_UCV_UART_COMPONENT_VER_RESET)
            {
                return ALT_E_ERROR;
            }
        }
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        handle->location   = location;
        handle->clock_freq = clock_freq;
        break;
    default:
        return ALT_E_BAD_ARG;
    }

    return alt_16550_reset_helper(handle, true);
}

ALT_STATUS_CODE alt_16550_uninit(ALT_16550_HANDLE_t * handle)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
        alt_setbits_word(ALT_RSTMGR_PERMODRST_ADDR, ALT_RSTMGR_PERMODRST_UART0_SET_MSK);
        return ALT_E_SUCCESS;
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        alt_setbits_word(ALT_RSTMGR_PERMODRST_ADDR, ALT_RSTMGR_PERMODRST_UART1_SET_MSK);
        return ALT_E_SUCCESS;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
    default:
        return alt_16550_reset_helper(handle, false);
    }
}

ALT_STATUS_CODE alt_16550_reset(ALT_16550_HANDLE_t * handle)
{
    return alt_16550_reset_helper(handle, true);
}

ALT_STATUS_CODE alt_16550_enable(ALT_16550_HANDLE_t * handle)
{
    // Write the divisor cached in the handle data to the divisor registers.
    // This will effectively enable the UART.
    return alt_16550_write_divisor_helper(handle,
                                          ALT_16550_HANDLE_DATA_DIVISOR_VALUE_GET(handle->data));
}

ALT_STATUS_CODE alt_16550_disable(ALT_16550_HANDLE_t * handle)
{
    // Write 0 to the divisor the divisor registers. This will effectively
    // disable the UART.
    return alt_16550_write_divisor_helper(handle, 0);
}

ALT_STATUS_CODE alt_16550_read(ALT_16550_HANDLE_t * handle,
                               char * item)
{
    // Verify that the UART is enabled
    if (!(handle->data & ALT_16550_HANDLE_DATA_UART_ENABLED_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify that the FIFO is disabled
    if (handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK)
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Read the RBR (Receive Buffer Register) into *item.
        *item = ALT_UART_RBR_THR_DLL_VALUE_GET(alt_read_word(ALT_UART_RBR_THR_DLL_ADDR(handle->location)));
        break;
    default:
        return ALT_E_ERROR;
    }
    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_write(ALT_16550_HANDLE_t * handle,
                                char item)
{
    // Verify that the UART is enabled
    if (!(handle->data & ALT_16550_HANDLE_DATA_UART_ENABLED_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify that the FIFO is disabled
    if (handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK)
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Write the buffer into the THR (Transmit Holding Register)
        alt_write_word(ALT_UART_RBR_THR_DLL_ADDR(handle->location), item);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

/////

ALT_STATUS_CODE alt_16550_fifo_enable(ALT_16550_HANDLE_t * handle)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Set FCR::FIFOE (FIFO Control Register :: FIFO Enable) bit.
        handle->fcr |= ALT_UART_FCR_FIFOE_SET_MSK;
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr);
        break;
    default:
        return ALT_E_ERROR;
    }

    // No need to reset / clear the FIFOs. This is done automatically when
    // FCR::FIFOE is changed.
    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_disable(ALT_16550_HANDLE_t * handle)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Clear FCR::FIFOE (FIFO Control Register :: FIFO Enable) bit.
        handle->fcr &= ~ALT_UART_FCR_FIFOE_SET_MSK;
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_read(ALT_16550_HANDLE_t * handle,
                                    char * buffer,
                                    size_t count)
{
    // Verify that the UART is enabled
    if (!(handle->data & ALT_16550_HANDLE_DATA_UART_ENABLED_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Read the RBR (Receive Buffer Register) into the buffer
        for (size_t i = 0; i < count; ++i)
        {
            buffer[i] = ALT_UART_RBR_THR_DLL_VALUE_GET(alt_read_word(ALT_UART_RBR_THR_DLL_ADDR(handle->location)));
        }
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,
                                     const char * buffer,
                                     size_t count)
{
    // Verify that the UART is enabled
    if (!(handle->data & ALT_16550_HANDLE_DATA_UART_ENABLED_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Write the buffer into the THR (Transmit Holding Register)
        for (size_t i = 0; i < count; ++i)
        {
            alt_write_word(ALT_UART_RBR_THR_DLL_ADDR(handle->location), buffer[i]);
        }
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_clear_rx(ALT_16550_HANDLE_t * handle)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Write SRR::RFR (Shadow Reset Register :: Receiver FIFO Reset) bit.
        alt_write_word(ALT_UART_SRR_ADDR(handle->location), ALT_UART_SRR_RFR_SET_MSK);
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Write FCR::RFIFOR (FIFO Control Register :: Receiver FIFO Reset) bit.
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr | ALT_UART_FCR_RFIFOR_SET_MSK);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_clear_tx(ALT_16550_HANDLE_t * handle)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Write SRR::XFR (Shadow Reset Register :: Xmitter FIFO Reset) bit.
        alt_write_word(ALT_UART_SRR_ADDR(handle->location), ALT_UART_SRR_XFR_SET_MSK);
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Write FCR::XFIFOR (FIFO Control Register :: Xmitter FIFO Reset) bit.
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr | ALT_UART_FCR_XFIFOR_SET_MSK);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_clear_all(ALT_16550_HANDLE_t * handle)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Write SRR::(RFR | XFR)
        //   (Shadow Reset Register :: (Receiver FIFO Reset | Xmitter FIFO Reset)) bits.
        alt_write_word(ALT_UART_SRR_ADDR(handle->location),
                       ALT_UART_SRR_RFR_SET_MSK | ALT_UART_SRR_XFR_SET_MSK);
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Write FCR::(RFIFOR |XFIFOR)
        //   (FIFO Control Register :: (Receiver FIFO Reset | Xmitter FIFO Reset)) bits.
        alt_write_word(ALT_UART_FCR_ADDR(handle->location),
                       handle->fcr | ALT_UART_FCR_RFIFOR_SET_MSK | ALT_UART_FCR_XFIFOR_SET_MSK);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_size_get_rx(ALT_16550_HANDLE_t * handle,
                                           uint32_t * size)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Read the CPR::FIFO_Mod (Component Parameter Register :: FIFO Mode).
        // The FIFO size is 16x this value.
        *size = ALT_UART_CPR_FIFO_MOD_GET(alt_read_word(ALT_UART_CPR_ADDR(handle->location))) << 4;
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Altera 16550 Compatible Soft UARTs have a configurable size and is
        // stored in the CPR::FIFO_Mode (Component Parameter Register :: FIFO Depth).
        *size = ALT_ALTERA_16550_CPR_FIFO_MODE_GET(alt_read_word(ALT_ALTERA_16550_CPR_ADDR(handle->location))) << 4;
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_size_get_tx(ALT_16550_HANDLE_t * handle,
                                           uint32_t * size)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Read the CPR::FIFO_Mod (Component Parameter Register :: FIFO Mode).
        // The FIFO size is 16x this value.
        *size = ALT_UART_CPR_FIFO_MOD_GET(alt_read_word(ALT_UART_CPR_ADDR(handle->location))) << 4;
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Altera 16550 Compatible Soft UARTs have a configurable size and is
        // stored in the CPR::FIFO_Mode (Component Parameter Register :: FIFO Depth).
        // The FIFO size is 16x this value.
        *size = ALT_ALTERA_16550_CPR_FIFO_MODE_GET(alt_read_word(ALT_ALTERA_16550_CPR_ADDR(handle->location))) << 4;
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_level_get_rx(ALT_16550_HANDLE_t * handle,
                                            uint32_t * level)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Read RFL (Receive FIFO Level).
        *level = alt_read_word(ALT_UART_RFL_ADDR(handle->location));
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // RFL not implemented. Return 0.
        *level = 0;
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_level_get_tx(ALT_16550_HANDLE_t * handle,
                                            uint32_t * level)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
        // Read TFL (Transmit FIFO Level).
        *level = alt_read_word(ALT_UART_TFL_ADDR(handle->location));
        break;
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // TFL not implemented. Return 0.
        *level = 0;
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_trigger_set_rx(ALT_16550_HANDLE_t * handle,
                                              ALT_16550_FIFO_TRIGGER_RX_t trigger)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify triggering parameter
    switch (trigger)
    {
    case ALT_16550_FIFO_TRIGGER_RX_ANY:
    case ALT_16550_FIFO_TRIGGER_RX_QUARTER_FULL:
    case ALT_16550_FIFO_TRIGGER_RX_HALF_FULL:
    case ALT_16550_FIFO_TRIGGER_RX_ALMOST_FULL:
        break;
    default:
        return ALT_E_BAD_ARG;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Update FCR::RT (FIFO Control Register :: Receiver Trigger)
        handle->fcr &= ~ALT_UART_FCR_RT_SET_MSK;
        handle->fcr |= ALT_UART_FCR_RT_SET(trigger);
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_fifo_trigger_set_tx(ALT_16550_HANDLE_t * handle,
                                              ALT_16550_FIFO_TRIGGER_TX_t trigger)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    // Verify triggering parameter
    switch (trigger)
    {
    case ALT_16550_FIFO_TRIGGER_TX_EMPTY:
    case ALT_16550_FIFO_TRIGGER_TX_ALMOST_EMPTY:
    case ALT_16550_FIFO_TRIGGER_TX_QUARTER_FULL:
    case ALT_16550_FIFO_TRIGGER_TX_HALF_FULL:
        break;
    default:
        return ALT_E_BAD_ARG;
    }

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Update FCR::TET (FIFO Control Register :: Transmit Empty Trigger)
        handle->fcr &= ~ALT_UART_FCR_TET_SET_MSK;
        handle->fcr |= ALT_UART_FCR_TET_SET(trigger);
        alt_write_word(ALT_UART_FCR_ADDR(handle->location), handle->fcr);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

/////

ALT_STATUS_CODE alt_16550_baudrate_get(ALT_16550_HANDLE_t * handle,
                                       uint32_t * baudrate)
{
    // Query the divisor cached in the handle data
    uint32_t divisor = ALT_16550_HANDLE_DATA_DIVISOR_VALUE_GET(handle->data);

    // The divisor should never be zero. It is set to allow for a baud of 57600
    // on initialization and a valid value is checked at
    // alt_16550_divisor_set(). We do not check for users altering the data in
    // the handle structure.

    // Formula for calculating the baudrate:
    //    baudrate = clock / (16 * divisor)

    *baudrate = (handle->clock_freq >> 4) / divisor;

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_baudrate_set(ALT_16550_HANDLE_t * handle,
                                       uint32_t baudrate)
{
    if (baudrate == 0)
    {
        return ALT_E_ARG_RANGE;
    }

    // Formula for calculating the divisor:
    //    baudrate = clock / (16 * divisor)
    // => baudrate * 16 * divisor = clock
    // => divisor = clock / (baudrate * 16)
    // => divisor = (clock / 16) / baudrate

    // Add half of the denominator to address rounding errors.
    uint32_t divisor = ((handle->clock_freq + (8 * baudrate)) / (16 * baudrate));

    // Check for divisor range is in alt_16550_divisor_set().
    return alt_16550_divisor_set(handle, divisor);
}

ALT_STATUS_CODE alt_16550_divisor_get(ALT_16550_HANDLE_t * handle,
                                      uint32_t * divisor)
{
    // Just read the divisor portion of the handle data.
    *divisor = ALT_16550_HANDLE_DATA_DIVISOR_VALUE_GET(handle->data);

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_divisor_set(ALT_16550_HANDLE_t * handle,
                                      uint32_t divisor)
{
    // Verify divisor value is in range.
    if ((divisor > 0xffff) || (divisor == 0))
    {
        return ALT_E_ARG_RANGE;
    }

    // Set the divisor portion of the handle data.
    handle->data &= ~(0xffff);
    handle->data |= divisor;

    // Even if the UART is enabled, don't do anything. It is documented that
    // the change will take effect when the UART move to the enabled state.

    return ALT_E_SUCCESS;
}

/////

static ALT_STATUS_CODE alt_16550_ier_mask_set_helper(ALT_16550_HANDLE_t * handle, uint32_t setmask)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Set bit in IER (Interrupt Enable Register)
        alt_setbits_word(ALT_UART_IER_DLH_ADDR(handle->location), setmask);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

static ALT_STATUS_CODE alt_16550_ier_mask_clr_helper(ALT_16550_HANDLE_t * handle, uint32_t setmask)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Clear bit in IER (Interrupt Enable Register)
        alt_clrbits_word(ALT_UART_IER_DLH_ADDR(handle->location), setmask);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_int_enable_rx(ALT_16550_HANDLE_t * handle)
{
    // Set the IER::ERBFI (Interrupt Enable Register :: Enable Receive Buffer Full Interrupt) bit.
    return alt_16550_ier_mask_set_helper(handle, ALT_UART_IER_DLH_ERBFI_DLH0_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_disable_rx(ALT_16550_HANDLE_t * handle)
{
    // Clear the IER::ERBFI (Interrupt Enable Register :: Enable Receive Buffer Full Interrupt) bit.
    return alt_16550_ier_mask_clr_helper(handle, ALT_UART_IER_DLH_ERBFI_DLH0_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_enable_tx(ALT_16550_HANDLE_t * handle)
{
    // Set the IER::ETBEI (Interrupt Enable Register :: Enable Transmit Buffer Empty Interrupt) bit.
    return alt_16550_ier_mask_set_helper(handle, ALT_UART_IER_DLH_ETBEI_DLH1_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_disable_tx(ALT_16550_HANDLE_t * handle)
{
    // Clear the IER::ETBEI (Interrupt Enable Register :: Enable Transmit Buffer Empty Interrupt) bit.
    return alt_16550_ier_mask_clr_helper(handle, ALT_UART_IER_DLH_ETBEI_DLH1_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_enable_line(ALT_16550_HANDLE_t * handle)
{
    // Set the IER::ELSI (Interrupt Enable Register :: Enable Line Status Interrupt) bit.
    return alt_16550_ier_mask_set_helper(handle, ALT_UART_IER_DLH_ELSI_DHL2_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_disable_line(ALT_16550_HANDLE_t * handle)
{
    // Clear the IER::ELSI (Interrupt Enable Register :: Enable Line Status Interrupt) bit.
    return alt_16550_ier_mask_clr_helper(handle, ALT_UART_IER_DLH_ELSI_DHL2_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_enable_modem(ALT_16550_HANDLE_t * handle)
{
    // Set the IER::EDSSI (Interrupt Enable Register :: Enable Modem Status Interrupt) bit.
    return alt_16550_ier_mask_set_helper(handle, ALT_UART_IER_DLH_EDSSI_DHL3_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_disable_modem(ALT_16550_HANDLE_t * handle)
{
    // Clear the IER::EDSSI (Interrupt Enable Register :: Enable Modem Status Interrupt) bit.
    return alt_16550_ier_mask_clr_helper(handle, ALT_UART_IER_DLH_EDSSI_DHL3_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_disable_all(ALT_16550_HANDLE_t * handle)
{
    // Clear the IER::(ERBFI | ETBEI | ELSI | EDSSI)
    //   (Interrupt Enable Register :: (Enable Receive Buffer Full Interrupt   |
    //                                  Enable Transmit Buffer Empty Interrupt |
    //                                  Enable Line Status Interrupt           |
    //                                  Enable Modem Status Interrupt)) bits
    return alt_16550_ier_mask_clr_helper(handle, ALT_UART_IER_DLH_ERBFI_DLH0_SET_MSK |
                                                 ALT_UART_IER_DLH_ETBEI_DLH1_SET_MSK |
                                                 ALT_UART_IER_DLH_ELSI_DHL2_SET_MSK  |
                                                 ALT_UART_IER_DLH_EDSSI_DHL3_SET_MSK);
}

ALT_STATUS_CODE alt_16550_int_status_get(ALT_16550_HANDLE_t * handle,
                                         ALT_16550_INT_STATUS_t * status)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Read IIR::IID (Interrupt Identity Register :: Interrupt ID)
        *status = (ALT_16550_INT_STATUS_t) ALT_UART_IIR_ID_GET(alt_read_word(ALT_UART_IIR_ADDR(handle->location)));
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

/////

ALT_STATUS_CODE alt_16550_line_config_set(ALT_16550_HANDLE_t * handle,
                                          ALT_16550_DATABITS_t databits,
                                          ALT_16550_PARITY_t parity,
                                          ALT_16550_STOPBITS_t stopbits)
{
    // Validate the databits parameter.
    switch (databits)
    {
    case ALT_16550_DATABITS_5:
    case ALT_16550_DATABITS_6:
    case ALT_16550_DATABITS_7:
    case ALT_16550_DATABITS_8:
        break;
    default:
        return ALT_E_ERROR;
    }

    // Validate the parity parameter.
    switch (parity)
    {
    case ALT_16550_PARITY_DISABLE:
    case ALT_16550_PARITY_ODD:
    case ALT_16550_PARITY_EVEN:
        break;
    default:
        return ALT_E_ERROR;
    }

    // Validate the stopbits parameter.
    switch (stopbits)
    {
    case ALT_16550_STOPBITS_1:
    case ALT_16550_STOPBITS_2:
        break;
    default:
        return ALT_E_ERROR;
    }

    // LCR (Line Control Register) cache.
    uint32_t lcr = 0;

    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:

        // Configure the number of databits
        lcr |= ALT_UART_LCR_DLS_SET(databits);

        // Configure the number of stopbits
        lcr |= ALT_UART_LCR_STOP_SET(stopbits);

        // Configure the parity
        if (parity != ALT_16550_PARITY_DISABLE)
        {
            // Enable parity in LCR
            lcr |= ALT_UART_LCR_PEN_SET_MSK;

            if (parity == ALT_16550_PARITY_EVEN)
            {
                // Enable even parity in LCR; otherwise it's odd parity.
                lcr |= ALT_UART_LCR_EPS_SET_MSK;
            }
        }

        // Update LCR (Line Control Register)
        alt_replbits_word(ALT_UART_LCR_ADDR(handle->location), 
                          ALT_UART_LCR_DLS_SET_MSK
                        | ALT_UART_LCR_STOP_SET_MSK
                        | ALT_UART_LCR_PEN_SET_MSK
                        | ALT_UART_LCR_EPS_SET_MSK,
                        lcr);

        break;

    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_line_break_enable(ALT_16550_HANDLE_t * handle)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Set the LCR::Break (Line Control Register :: Break) bit.
        alt_setbits_word(ALT_UART_LCR_ADDR(handle->location), ALT_UART_LCR_BREAK_SET_MSK);
        break;

    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_line_break_disable(ALT_16550_HANDLE_t * handle)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Clear the LCR::Break (Line Control Register :: Break) bit.
        alt_clrbits_word(ALT_UART_LCR_ADDR(handle->location), ALT_UART_LCR_BREAK_SET_MSK);
        break;

    default:
        return ALT_E_ERROR;
    }


    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_line_status_get(ALT_16550_HANDLE_t * handle,
                                          uint32_t * status)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Read the LSR (Line Status Register).
        *status = alt_read_word(ALT_UART_LSR_ADDR(handle->location));
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

/////

static ALT_STATUS_CODE alt_16550_mcr_mask_set_helper(ALT_16550_HANDLE_t * handle,
                                                     uint32_t setmask)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Set the bit in MCR (Modem Control Register).
        alt_setbits_word(ALT_UART_MCR_ADDR(handle->location), setmask);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

static ALT_STATUS_CODE alt_16550_mcr_mask_clr_helper(ALT_16550_HANDLE_t * handle, uint32_t setmask)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Clear the bit in MCR (Modem Control Register).
        alt_clrbits_word(ALT_UART_MCR_ADDR(handle->location), setmask);
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}

ALT_STATUS_CODE alt_16550_flowcontrol_enable(ALT_16550_HANDLE_t * handle)
{
    // Verify that the FIFO is enabled
    if (!(handle->fcr & ALT_UART_FCR_FIFOE_SET_MSK))
    {
        return ALT_E_ERROR;
    }

    // For the Altera 16550 Compatible Soft UART, check that Hardware Flowcontrol is enabled.
    if (handle->device == ALT_16550_DEVICE_ALTERA_16550_UART)
    {
        // Read the CPR::AFCE_Mode (Component Parameter Register :: Auto Flow Control mode) bit.
        uint32_t cpr = alt_read_word(ALT_ALTERA_16550_CPR_ADDR(handle->location));
        if (!(ALT_ALTERA_16550_CPR_AFCE_MODE_SET_MSK & cpr))
        {
            return ALT_E_ERROR;
        }
    }

    // Set MCR::AFCE (Modem Control Register :: Automatic FlowControl Enable) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_AFCE_SET_MSK);
}

ALT_STATUS_CODE alt_16550_flowcontrol_disable(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::AFCE (Modem Control Register :: Automatic FlowControl Enable) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_AFCE_SET_MSK);
}

ALT_STATUS_CODE alt_16550_loopback_enable(ALT_16550_HANDLE_t * handle)
{
    // Loopback is not implemented in the Altera 16550 Compatible Soft UART.
    if (handle->device == ALT_16550_DEVICE_ALTERA_16550_UART)
    {
        return ALT_E_ERROR;
    }

    // Set MCR::Loopback (Modem Control Register :: Loopback) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_LOOPBACK_SET_MSK);
}

ALT_STATUS_CODE alt_16550_loopback_disable(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::Loopback (Modem Control Register :: Loopback) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_LOOPBACK_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_enable_out1(ALT_16550_HANDLE_t * handle)
{
    // Set MCR::Out1 (Modem Control Register :: Out1) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_OUT1_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_disable_out1(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::Out1 (Modem Control Register :: Out1) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_OUT1_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_enable_out2(ALT_16550_HANDLE_t * handle)
{
    // Set MCR::Out2 (Modem Control Register :: Out2) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_OUT2_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_disable_out2(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::Out2 (Modem Control Register :: Out2) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_OUT2_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_enable_rts(ALT_16550_HANDLE_t * handle)
{
    // Set MCR::RTS (Modem Control Register :: Request To Send) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_RTS_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_disable_rts(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::RTS (Modem Control Register :: Request To Send) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_RTS_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_enable_dtr(ALT_16550_HANDLE_t * handle)
{
    // Set MCR::DTR (Modem Control Register :: Data Terminal Ready) bit.
    return alt_16550_mcr_mask_set_helper(handle, ALT_UART_MCR_DTR_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_disable_dtr(ALT_16550_HANDLE_t * handle)
{
    // Clear MCR::DTR (Modem Control Register :: Data Terminal Ready) bit.
    return alt_16550_mcr_mask_clr_helper(handle, ALT_UART_MCR_DTR_SET_MSK);
}

ALT_STATUS_CODE alt_16550_modem_status_get(ALT_16550_HANDLE_t * handle,
                                          uint32_t * status)
{
    switch (handle->device)
    {
    case ALT_16550_DEVICE_SOCFPGA_UART0:
    case ALT_16550_DEVICE_SOCFPGA_UART1:
    case ALT_16550_DEVICE_ALTERA_16550_UART:
        // Read the MSR (Modem Status Register).
        *status = alt_read_word(ALT_UART_MSR_ADDR(handle->location));
        break;
    default:
        return ALT_E_ERROR;
    }

    return ALT_E_SUCCESS;
}