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 | * $Id$ |
---|
18 | */ |
---|
19 | |
---|
20 | #include <bsp.h> |
---|
21 | #include <rtems/libio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <assert.h> |
---|
24 | |
---|
25 | /* |
---|
26 | * Number of uarts on AMBA bus |
---|
27 | */ |
---|
28 | extern int uarts; |
---|
29 | |
---|
30 | static int isinit = 0; |
---|
31 | |
---|
32 | /* Let user override which on-chip APBUART will be debug UART |
---|
33 | * 0 = Default APBUART. On MP system CPU0=APBUART0, CPU1=APBUART1... |
---|
34 | * 1 = APBUART[0] |
---|
35 | * 2 = APBUART[1] |
---|
36 | * 3 = APBUART[2] |
---|
37 | * ... |
---|
38 | */ |
---|
39 | int debug_uart_index __attribute__((weak)) = 0; |
---|
40 | |
---|
41 | /* |
---|
42 | * Scan for UARTS in configuration |
---|
43 | */ |
---|
44 | int scan_uarts(void) |
---|
45 | { |
---|
46 | int i; |
---|
47 | amba_apb_device apbuarts[LEON3_APBUARTS]; |
---|
48 | |
---|
49 | if (isinit == 0) { |
---|
50 | i = 0; |
---|
51 | uarts = 0; |
---|
52 | |
---|
53 | uarts = amba_find_apbslvs( |
---|
54 | &amba_conf, VENDOR_GAISLER, GAISLER_APBUART, apbuarts, LEON3_APBUARTS); |
---|
55 | for(i=0; i<uarts; i++) { |
---|
56 | LEON3_Console_Uart[i] = (volatile LEON3_UART_Regs_Map *)apbuarts[i].start; |
---|
57 | } |
---|
58 | |
---|
59 | /* Update debug_uart_index to index used as debug console. |
---|
60 | * Let user select Debug console by setting debug_uart_index. If the |
---|
61 | * BSP is to provide the default UART (debug_uart_index==0): |
---|
62 | * non-MP: APBUART[0] is debug console |
---|
63 | * MP: LEON CPU index select UART |
---|
64 | */ |
---|
65 | if (debug_uart_index == 0) { |
---|
66 | #if defined(RTEMS_MULTIPROCESSING) |
---|
67 | debug_uart_index = LEON3_Cpu_Index; |
---|
68 | #else |
---|
69 | debug_uart_index = 0; |
---|
70 | #endif |
---|
71 | } else { |
---|
72 | debug_uart_index = debug_uart_index - 1; /* User selected dbg-console */ |
---|
73 | } |
---|
74 | |
---|
75 | /* initialize debug uart if present for printk */ |
---|
76 | if (debug_uart_index < uarts) { |
---|
77 | LEON3_Console_Uart[debug_uart_index]->ctrl |= LEON_REG_UART_CTRL_RE | |
---|
78 | LEON_REG_UART_CTRL_TE; |
---|
79 | LEON3_Console_Uart[debug_uart_index]->status = 0; |
---|
80 | } |
---|
81 | isinit = 1; |
---|
82 | } |
---|
83 | |
---|
84 | return uarts; |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | * console_outbyte_polled |
---|
89 | * |
---|
90 | * This routine transmits a character using polling. |
---|
91 | */ |
---|
92 | void console_outbyte_polled( |
---|
93 | int port, |
---|
94 | unsigned char ch |
---|
95 | ) |
---|
96 | { |
---|
97 | if ((port >= 0) && (port < uarts)) { |
---|
98 | return; |
---|
99 | |
---|
100 | while ( (LEON3_Console_Uart[port]->status & LEON_REG_UART_STATUS_THE) == 0 ); |
---|
101 | LEON3_Console_Uart[port]->data = (unsigned int) ch; |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * console_inbyte_nonblocking |
---|
106 | * |
---|
107 | * This routine polls for a character. |
---|
108 | */ |
---|
109 | int apbuart_inbyte_nonblocking(int port) |
---|
110 | { |
---|
111 | if ((port >= 0) && (port < uarts)) { |
---|
112 | assert( 0 ); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | /* Clear errors */ |
---|
117 | if (LEON3_Console_Uart[port]->status & LEON_REG_UART_STATUS_ERR) |
---|
118 | LEON3_Console_Uart[port]->status = ~LEON_REG_UART_STATUS_ERR; |
---|
119 | |
---|
120 | if ((LEON3_Console_Uart[port]->status & LEON_REG_UART_STATUS_DR) == 0) |
---|
121 | return -1; |
---|
122 | else |
---|
123 | return (int) LEON3_Console_Uart[port]->data; |
---|
124 | } |
---|
125 | |
---|
126 | /* putchar/getchar for printk */ |
---|
127 | static void bsp_out_char(char c) |
---|
128 | { |
---|
129 | console_outbyte_polled(debug_uart_index, c); |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * To support printk |
---|
134 | */ |
---|
135 | |
---|
136 | #include <rtems/bspIo.h> |
---|
137 | |
---|
138 | BSP_output_char_function_type BSP_output_char = bsp_out_char; |
---|
139 | |
---|
140 | static int bsp_in_char(void) |
---|
141 | { |
---|
142 | int tmp; |
---|
143 | |
---|
144 | while ((tmp = apbuart_inbyte_nonblocking(debug_uart_index)) < 0) |
---|
145 | ; |
---|
146 | return tmp; |
---|
147 | } |
---|
148 | |
---|
149 | BSP_polling_getchar_function_type BSP_poll_char = bsp_in_char; |
---|