blob: 846648290cc0ba14aeae93ef2c5cc2137d5aed06 [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
2 * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch.h>
32#include <asm_macros.S>
33#include <uart_16550.h>
34
35 .globl console_core_init
36 .globl console_core_putc
37 .globl console_core_getc
38
39 /* -----------------------------------------------
40 * int console_core_init(unsigned long base_addr,
41 * unsigned int uart_clk, unsigned int baud_rate)
42 * Function to initialize the console without a
43 * C Runtime to print debug information. This
44 * function will be accessed by console_init and
45 * crash reporting.
46 * In: x0 - console base address
47 * w1 - Uart clock in Hz
48 * w2 - Baud rate
49 * Out: return 1 on success
50 * Clobber list : x1, x2, x3
51 * -----------------------------------------------
52 */
53func console_core_init
54 /* Check the input base address */
55 cbz x0, init_fail
56 /* Check baud rate and uart clock for sanity */
57 cbz w1, init_fail
58 cbz w2, init_fail
59
60 /* Program the baudrate */
61 /* Divisor = Uart clock / (16 * baudrate) */
62 lsl w2, w2, #4
63 udiv w2, w1, w2
64 and w1, w2, #0xff /* w1 = DLL */
65 lsr w2, w2, #8
66 and w2, w2, #0xff /* w2 = DLLM */
67 ldr w3, [x0, #UARTLCR]
68 orr w3, w3, #UARTLCR_DLAB
69 str w3, [x0, #UARTLCR] /* enable DLL, DLLM programming */
70 str w1, [x0, #UARTDLL] /* program DLL */
71 str w2, [x0, #UARTDLLM] /* program DLLM */
72 mov w2, #~UARTLCR_DLAB
73 and w3, w3, w2
74 str w3, [x0, #UARTLCR] /* disable DLL, DLLM programming */
75
76 /* 8n1 */
77 mov w3, #3
78 str w3, [x0, #UARTLCR]
79 /* no interrupt */
80 mov w3, #0
81 str w3, [x0, #UARTIER]
82 /* enable fifo, DMA */
83 mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
84 str w3, [x0, #UARTFCR]
85 /* DTR + RTS */
86 mov w3, #3
87 str w3, [x0, #UARTMCR]
88 mov w0, #1
89init_fail:
90 ret
91endfunc console_core_init
92
93 /* --------------------------------------------------------
94 * int console_core_putc(int c, unsigned int base_addr)
95 * Function to output a character over the console. It
96 * returns the character printed on success or -1 on error.
97 * In : w0 - character to be printed
98 * x1 - console base address
99 * Out : return -1 on error else return character.
100 * Clobber list : x2
101 * --------------------------------------------------------
102 */
103func console_core_putc
104 /* Check the input parameter */
105 cbz x1, putc_error
106
107 /* Prepend '\r' to '\n' */
108 cmp w0, #0xA
109 b.ne 2f
110 /* Check if the transmit FIFO is full */
1111: ldr w2, [x1, #UARTLSR]
112 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
113 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
114 b.ne 1b
115 mov w2, #0xD /* '\r' */
116 str w2, [x1, #UARTTX]
117 ldr w2, [x1, #UARTFCR]
118 orr w2, w2, #UARTFCR_TXCLR
119 str w2, [x1, #UARTFCR]
120
121 /* Check if the transmit FIFO is full */
1222: ldr w2, [x1, #UARTLSR]
123 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
124 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
125 b.ne 2b
126 str w0, [x1, #UARTTX]
127 ldr w2, [x1, #UARTFCR]
128 orr w2, w2, #UARTFCR_TXCLR
129 str w2, [x1, #UARTFCR]
130 ret
131putc_error:
132 mov w0, #-1
133 ret
134endfunc console_core_putc
135
136 /* ---------------------------------------------
137 * int console_core_getc(void)
138 * Function to get a character from the console.
139 * It returns the character grabbed on success
140 * or -1 on error.
141 * In : w0 - console base address
142 * Out : return -1 on error else return character.
143 * Clobber list : x0, x1
144 * ---------------------------------------------
145 */
146func console_core_getc
147 /* Check if the receive FIFO is empty */
1481: ldr w1, [x0, #UARTLSR]
Nishanth Menon7b4b5132017-01-10 09:34:07 -0600149 tbz w1, #UARTLSR_RDR_BIT, 1b
Soby Mathew7abebe82016-08-08 12:38:52 +0100150 ldr w0, [x0, #UARTRX]
151 ret
152getc_error:
153 mov w0, #-1
154 ret
155endfunc console_core_getc