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