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#include <common.h>
41#include <watchdog.h>
42#include <asm/blackfin.h>
43#include <asm/mach-common/bits/uart.h>
44
45#ifdef CONFIG_UART_CONSOLE
46
47#if defined(UART_LSR) && (CONFIG_UART_CONSOLE != 0)
48# error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
49#endif
50
51#include "serial.h"
52
53#ifdef CONFIG_DEBUG_SERIAL
54uint16_t cached_lsr[256];
55uint16_t cached_rbr[256];
56size_t cache_count;
57
58
59
60
61
62
63static uint16_t uart_lsr_save;
64static uint16_t uart_lsr_read(void)
65{
66 uint16_t lsr = *pUART_LSR;
67 uart_lsr_save |= (lsr & (OE|PE|FE|BI));
68 return lsr | uart_lsr_save;
69}
70
71static void uart_lsr_clear(void)
72{
73 uart_lsr_save = 0;
74 *pUART_LSR |= -1;
75}
76#else
77
78
79
80
81static inline uint16_t uart_lsr_read(void) { return *pUART_LSR; }
82static inline void uart_lsr_clear(void) { *pUART_LSR = -1; }
83#endif
84
85
86void serial_set_baud(uint32_t baud)
87{
88 serial_early_set_baud(baud);
89}
90
91
92
93
94void serial_setbrg(void)
95{
96 DECLARE_GLOBAL_DATA_PTR;
97 serial_set_baud(gd->baudrate);
98}
99
100
101void serial_initialize(void)
102{
103 serial_early_init();
104}
105
106
107int serial_init(void)
108{
109 serial_initialize();
110 serial_setbrg();
111 uart_lsr_clear();
112#ifdef CONFIG_DEBUG_SERIAL
113 cache_count = 0;
114 memset(cached_lsr, 0x00, sizeof(cached_lsr));
115 memset(cached_rbr, 0x00, sizeof(cached_rbr));
116#endif
117 return 0;
118}
119
120void serial_putc(const char c)
121{
122
123 if (c == '\n')
124 serial_putc('\r');
125
126 WATCHDOG_RESET();
127
128
129 while (!(uart_lsr_read() & THRE))
130 continue;
131
132
133 *pUART_THR = c;
134 SSYNC();
135
136 WATCHDOG_RESET();
137}
138
139int serial_tstc(void)
140{
141 WATCHDOG_RESET();
142 return (uart_lsr_read() & DR) ? 1 : 0;
143}
144
145int serial_getc(void)
146{
147 uint16_t uart_rbr_val;
148
149
150 while (!serial_tstc())
151 continue;
152
153
154 uart_rbr_val = *pUART_RBR;
155
156#ifdef CONFIG_DEBUG_SERIAL
157
158 uint16_t uart_lsr_val = uart_lsr_read();
159
160 cached_lsr[cache_count] = uart_lsr_val;
161 cached_rbr[cache_count] = uart_rbr_val;
162 cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
163
164 if (uart_lsr_val & (OE|PE|FE|BI)) {
165 uint16_t dll, dlh;
166 printf("\n[SERIAL ERROR]\n");
167 ACCESS_LATCH();
168 dll = *pUART_DLL;
169 dlh = *pUART_DLH;
170 ACCESS_PORT_IER();
171 printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
172 do {
173 --cache_count;
174 printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count,
175 cached_rbr[cache_count], cached_lsr[cache_count]);
176 } while (cache_count > 0);
177 return -1;
178 }
179#endif
180 uart_lsr_clear();
181
182 return uart_rbr_val;
183}
184
185void serial_puts(const char *s)
186{
187 while (*s)
188 serial_putc(*s++);
189}
190
191#endif
192