1 | /* |
---|
2 | * This file contains the TTY driver for the serial ports on the LEON. |
---|
3 | * |
---|
4 | * This driver uses the termios pseudo driver. |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1989-1999. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * Modified for LEON3 BSP. |
---|
10 | * COPYRIGHT (c) 2011. |
---|
11 | * Aeroflex Gaisler. |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <bsp.h> |
---|
19 | #include <leon.h> |
---|
20 | #include <rtems/libio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <assert.h> |
---|
23 | #include <stdio.h> |
---|
24 | |
---|
25 | int debug_uart_index __attribute__((weak)) = 0; |
---|
26 | static struct apbuart_regs *dbg_uart = NULL; |
---|
27 | |
---|
28 | #ifndef LEON3_QEMU |
---|
29 | /* Before UART driver has registered (or when no UART is available), calls to |
---|
30 | * printk that gets to bsp_out_char() will be filling data into the |
---|
31 | * pre_printk_dbgbuf[] buffer, hopefully the buffer can help debugging the |
---|
32 | * early BSP boot.. At least the last printk() will be caught. |
---|
33 | */ |
---|
34 | static char pre_printk_dbgbuf[32] = {0}; |
---|
35 | static int pre_printk_pos = 0; |
---|
36 | |
---|
37 | /* Initialize the BSP system debug console layer. It will scan AMBA Plu&Play |
---|
38 | * for a debug APBUART and enable RX/TX for that UART. |
---|
39 | */ |
---|
40 | void bsp_debug_uart_init(void) |
---|
41 | { |
---|
42 | int i; |
---|
43 | struct ambapp_dev *adev; |
---|
44 | struct ambapp_apb_info *apb; |
---|
45 | |
---|
46 | /* Update debug_uart_index to index used as debug console. |
---|
47 | * Let user select Debug console by setting debug_uart_index. If the |
---|
48 | * BSP is to provide the default UART (debug_uart_index==0): |
---|
49 | * non-MP: APBUART[0] is debug console |
---|
50 | * MP: LEON CPU index select UART |
---|
51 | */ |
---|
52 | if (debug_uart_index == 0) { |
---|
53 | #if defined(RTEMS_MULTIPROCESSING) |
---|
54 | debug_uart_index = LEON3_Cpu_Index; |
---|
55 | #else |
---|
56 | debug_uart_index = 0; |
---|
57 | #endif |
---|
58 | } else { |
---|
59 | debug_uart_index = debug_uart_index - 1; /* User selected dbg-console */ |
---|
60 | } |
---|
61 | |
---|
62 | /* Find APBUART core for System Debug Console */ |
---|
63 | i = debug_uart_index; |
---|
64 | adev = (void *)ambapp_for_each(&ambapp_plb, (OPTIONS_ALL|OPTIONS_APB_SLVS), |
---|
65 | VENDOR_GAISLER, GAISLER_APBUART, |
---|
66 | ambapp_find_by_idx, (void *)&i); |
---|
67 | if (adev) { |
---|
68 | /* Found a matching debug console, initialize debug uart if present |
---|
69 | * for printk |
---|
70 | */ |
---|
71 | apb = (struct ambapp_apb_info *)adev->devinfo; |
---|
72 | dbg_uart = (struct apbuart_regs *)apb->start; |
---|
73 | dbg_uart->ctrl |= LEON_REG_UART_CTRL_RE | LEON_REG_UART_CTRL_TE; |
---|
74 | dbg_uart->status = 0; |
---|
75 | } |
---|
76 | } |
---|
77 | #else |
---|
78 | static void bsp_out_char(char c); |
---|
79 | |
---|
80 | static void leon3_qemu_debug_uart_init(char c) |
---|
81 | { |
---|
82 | bsp_debug_uart_init(); |
---|
83 | |
---|
84 | BSP_output_char = bsp_out_char; |
---|
85 | |
---|
86 | bsp_out_char(c); |
---|
87 | } |
---|
88 | |
---|
89 | void bsp_debug_uart_init(void) |
---|
90 | { |
---|
91 | dbg_uart = (struct apbuart_regs *)0x80000100; |
---|
92 | dbg_uart->ctrl |= LEON_REG_UART_CTRL_RE | LEON_REG_UART_CTRL_TE; |
---|
93 | dbg_uart->status = 0; |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | /* |
---|
98 | * apbuart_outbyte_polled |
---|
99 | * |
---|
100 | * This routine transmits a character using polling. |
---|
101 | */ |
---|
102 | void apbuart_outbyte_polled( |
---|
103 | struct apbuart_regs *regs, |
---|
104 | unsigned char ch, |
---|
105 | int do_cr_on_newline, |
---|
106 | int wait_sent |
---|
107 | ) |
---|
108 | { |
---|
109 | send: |
---|
110 | while ( (regs->status & LEON_REG_UART_STATUS_THE) == 0 ) { |
---|
111 | /* Lower bus utilization while waiting for UART */ |
---|
112 | asm volatile ("nop"::); asm volatile ("nop"::); |
---|
113 | asm volatile ("nop"::); asm volatile ("nop"::); |
---|
114 | asm volatile ("nop"::); asm volatile ("nop"::); |
---|
115 | asm volatile ("nop"::); asm volatile ("nop"::); |
---|
116 | } |
---|
117 | regs->data = (unsigned int) ch; |
---|
118 | |
---|
119 | if ((ch == '\n') && do_cr_on_newline) { |
---|
120 | ch = '\r'; |
---|
121 | goto send; |
---|
122 | } |
---|
123 | |
---|
124 | /* Wait until the character has been sent? */ |
---|
125 | if (wait_sent) { |
---|
126 | while ((regs->status & LEON_REG_UART_STATUS_THE) == 0) |
---|
127 | ; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /* |
---|
132 | * apbuart_inbyte_nonblocking |
---|
133 | * |
---|
134 | * This routine polls for a character. |
---|
135 | */ |
---|
136 | int apbuart_inbyte_nonblocking(struct apbuart_regs *regs) |
---|
137 | { |
---|
138 | /* Clear errors */ |
---|
139 | if (regs->status & LEON_REG_UART_STATUS_ERR) |
---|
140 | regs->status = ~LEON_REG_UART_STATUS_ERR; |
---|
141 | |
---|
142 | if ((regs->status & LEON_REG_UART_STATUS_DR) == 0) |
---|
143 | return EOF; |
---|
144 | else |
---|
145 | return (int) regs->data; |
---|
146 | } |
---|
147 | |
---|
148 | /* putchar/getchar for printk */ |
---|
149 | static void bsp_out_char(char c) |
---|
150 | { |
---|
151 | #ifndef LEON3_QEMU |
---|
152 | if (dbg_uart == NULL) { |
---|
153 | /* Local debug buffer when UART driver has not registered */ |
---|
154 | pre_printk_dbgbuf[pre_printk_pos++] = c; |
---|
155 | pre_printk_pos = pre_printk_pos & (sizeof(pre_printk_dbgbuf)-1); |
---|
156 | return; |
---|
157 | } |
---|
158 | #endif |
---|
159 | |
---|
160 | apbuart_outbyte_polled(dbg_uart, c, 1, 1); |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | * To support printk |
---|
165 | */ |
---|
166 | |
---|
167 | #include <rtems/bspIo.h> |
---|
168 | |
---|
169 | #ifndef LEON3_QEMU |
---|
170 | BSP_output_char_function_type BSP_output_char = bsp_out_char; |
---|
171 | #else |
---|
172 | BSP_output_char_function_type BSP_output_char = leon3_qemu_debug_uart_init; |
---|
173 | #endif |
---|
174 | |
---|
175 | static int bsp_in_char(void) |
---|
176 | { |
---|
177 | int tmp; |
---|
178 | |
---|
179 | if (dbg_uart == NULL) |
---|
180 | return EOF; |
---|
181 | |
---|
182 | while ((tmp = apbuart_inbyte_nonblocking(dbg_uart)) < 0) |
---|
183 | ; |
---|
184 | return tmp; |
---|
185 | } |
---|
186 | |
---|
187 | BSP_polling_getchar_function_type BSP_poll_char = bsp_in_char; |
---|