blob: f671096768b6b0bffce7ff5984335bb2b85da4c0 [file] [log] [blame]
Mike Frysinger94bae5c2008-03-30 15:46:13 -04001/*
2 * serial.h - common serial defines for early debug and serial driver.
3 * any functions defined here must be always_inline since
4 * initcode cannot have function calls.
5 *
6 * Copyright (c) 2004-2007 Analog Devices Inc.
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#ifndef __BFIN_CPU_SERIAL_H__
12#define __BFIN_CPU_SERIAL_H__
13
14#include <asm/blackfin.h>
15#include <asm/mach-common/bits/uart.h>
16
Mike Frysinger500f2bb2008-10-11 21:52:17 -040017#ifndef CONFIG_UART_CONSOLE
18# define CONFIG_UART_CONSOLE 0
19#endif
20
Mike Frysinger94bae5c2008-03-30 15:46:13 -040021#ifdef CONFIG_DEBUG_EARLY_SERIAL
22# define BFIN_DEBUG_EARLY_SERIAL 1
23#else
24# define BFIN_DEBUG_EARLY_SERIAL 0
25#endif
26
27#define LOB(x) ((x) & 0xFF)
28#define HIB(x) (((x) >> 8) & 0xFF)
29
30#ifndef UART_LSR
31# if (CONFIG_UART_CONSOLE == 3)
32# define pUART_DLH pUART3_DLH
33# define pUART_DLL pUART3_DLL
34# define pUART_GCTL pUART3_GCTL
35# define pUART_IER pUART3_IER
36# define pUART_IERC pUART3_IER_CLEAR
37# define pUART_LCR pUART3_LCR
38# define pUART_LSR pUART3_LSR
39# define pUART_RBR pUART3_RBR
40# define pUART_THR pUART3_THR
41# define UART_THR UART3_THR
42# define UART_LSR UART3_LSR
43# elif (CONFIG_UART_CONSOLE == 2)
44# define pUART_DLH pUART2_DLH
45# define pUART_DLL pUART2_DLL
46# define pUART_GCTL pUART2_GCTL
47# define pUART_IER pUART2_IER
48# define pUART_IERC pUART2_IER_CLEAR
49# define pUART_LCR pUART2_LCR
50# define pUART_LSR pUART2_LSR
51# define pUART_RBR pUART2_RBR
52# define pUART_THR pUART2_THR
53# define UART_THR UART2_THR
54# define UART_LSR UART2_LSR
55# elif (CONFIG_UART_CONSOLE == 1)
56# define pUART_DLH pUART1_DLH
57# define pUART_DLL pUART1_DLL
58# define pUART_GCTL pUART1_GCTL
59# define pUART_IER pUART1_IER
60# define pUART_IERC pUART1_IER_CLEAR
61# define pUART_LCR pUART1_LCR
62# define pUART_LSR pUART1_LSR
63# define pUART_RBR pUART1_RBR
64# define pUART_THR pUART1_THR
65# define UART_THR UART1_THR
66# define UART_LSR UART1_LSR
67# elif (CONFIG_UART_CONSOLE == 0)
68# define pUART_DLH pUART0_DLH
69# define pUART_DLL pUART0_DLL
70# define pUART_GCTL pUART0_GCTL
71# define pUART_IER pUART0_IER
72# define pUART_IERC pUART0_IER_CLEAR
73# define pUART_LCR pUART0_LCR
74# define pUART_LSR pUART0_LSR
75# define pUART_RBR pUART0_RBR
76# define pUART_THR pUART0_THR
77# define UART_THR UART0_THR
78# define UART_LSR UART0_LSR
79# endif
80#endif
81
82#ifndef __ASSEMBLY__
83
84/* We cannot use get_sclk() in initcode as it is defined elsewhere. */
85#ifdef BFIN_IN_INITCODE
86# define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
87#endif
88
89#ifdef __ADSPBF54x__
90# define ACCESS_LATCH()
91# define ACCESS_PORT_IER()
92# define CLEAR_IER() (*pUART_IERC = 0)
93#else
94# define ACCESS_LATCH() (*pUART_LCR |= DLAB)
95# define ACCESS_PORT_IER() (*pUART_LCR &= ~DLAB)
96# define CLEAR_IER() (*pUART_IER = 0)
97#endif
98
99__attribute__((always_inline))
100static inline void serial_do_portmux(void)
101{
Mike Frysinger07374482008-10-11 21:15:53 -0400102#if defined(__ADSPBF51x__)
103# define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
104 bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~(PORT_x_MUX_##mux_tx##_MASK | PORT_x_MUX_##mux_rx##_MASK)) | PORT_x_MUX_##mux_tx##_FUNC_2 | PORT_x_MUX_##mux_rx##_FUNC_2); \
105 bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
106 switch (CONFIG_UART_CONSOLE) {
107 case 0: DO_MUX(G, 5, 5, 9, 10); break; /* Port G; mux 5; PG9 and PG10 */
108 case 1: DO_MUX(F, 2, 3, 14, 15); break; /* Port H; mux 2/3; PH14 and PH15 */
109 }
110 SSYNC();
111#elif defined(__ADSPBF52x__)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400112# define DO_MUX(port, mux, tx, rx) \
113 bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
114 bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
115 switch (CONFIG_UART_CONSOLE) {
116 case 0: DO_MUX(G, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
117 case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
118 }
119 SSYNC();
120#elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
121# define DO_MUX(func, tx, rx) \
122 bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~(func)); \
123 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | PF##tx | PF##rx);
124 switch (CONFIG_UART_CONSOLE) {
125 case 0: DO_MUX(PFDE, 0, 1); break;
126 case 1: DO_MUX(PFTE, 2, 3); break;
127 }
128 SSYNC();
129#elif defined(__ADSPBF54x__)
130# define DO_MUX(port, tx, rx) \
131 bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~(PORT_x_MUX_##tx##_MASK | PORT_x_MUX_##rx##_MASK)) | PORT_x_MUX_##tx##_FUNC_1 | PORT_x_MUX_##rx##_FUNC_1); \
132 bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
133 switch (CONFIG_UART_CONSOLE) {
134 case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
135 case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
136 case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
137 case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
138 }
139 SSYNC();
140#endif
141}
142
143__attribute__((always_inline))
144static inline void serial_early_init(void)
145{
146 /* handle portmux crap on different Blackfins */
147 serial_do_portmux();
148
149 /* Enable UART */
150 *pUART_GCTL = UCEN;
151
152 /* Set LCR to Word Lengh 8-bit word select */
153 *pUART_LCR = WLS_8;
154
155 SSYNC();
156}
157
158__attribute__((always_inline))
159static inline uint32_t serial_early_get_baud(void)
160{
161 /* If the UART isnt enabled, then we are booting an LDR
162 * from a non-UART source (so like flash) which means
163 * the baud rate here is meaningless.
164 */
165 if ((*pUART_GCTL & UCEN) != UCEN)
166 return 0;
167
168#if (0) /* See comment for serial_reset_baud() in initcode.c */
169 /* Set DLAB in LCR to Access DLL and DLH */
170 ACCESS_LATCH();
171 SSYNC();
172
173 uint8_t dll = *pUART_DLL;
174 uint8_t dlh = *pUART_DLH;
175 uint16_t divisor = (dlh << 8) | dll;
176 uint32_t baud = get_sclk() / (divisor * 16);
177
178 /* Clear DLAB in LCR to Access THR RBR IER */
179 ACCESS_PORT_IER();
180 SSYNC();
181
182 return baud;
183#else
184 return CONFIG_BAUDRATE;
185#endif
186}
187
188__attribute__((always_inline))
189static inline void serial_early_set_baud(uint32_t baud)
190{
Mike Frysingerae2d4672008-04-20 03:11:53 -0400191 /* Translate from baud into divisor in terms of SCLK. The
192 * weird multiplication is to make sure we over sample just
193 * a little rather than under sample the incoming signals.
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400194 */
Mike Frysingerae2d4672008-04-20 03:11:53 -0400195 uint16_t divisor = (get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400196
197 /* Set DLAB in LCR to Access DLL and DLH */
198 ACCESS_LATCH();
199 SSYNC();
200
201 /* Program the divisor to get the baud rate we want */
202 *pUART_DLL = LOB(divisor);
203 *pUART_DLH = HIB(divisor);
204 SSYNC();
205
206 /* Clear DLAB in LCR to Access THR RBR IER */
207 ACCESS_PORT_IER();
208 SSYNC();
209}
210
211#ifndef BFIN_IN_INITCODE
212__attribute__((always_inline))
213static inline void serial_early_puts(const char *s)
214{
215 if (BFIN_DEBUG_EARLY_SERIAL) {
216 serial_puts("Early: ");
217 serial_puts(s);
218 }
219}
220#endif
221
222#else
223
224.macro serial_early_init
225#ifdef CONFIG_DEBUG_EARLY_SERIAL
226 call _serial_initialize;
227#endif
228.endm
229
230.macro serial_early_set_baud
231#ifdef CONFIG_DEBUG_EARLY_SERIAL
232 R0.L = LO(CONFIG_BAUDRATE);
233 R0.H = HI(CONFIG_BAUDRATE);
234 call _serial_set_baud;
235#endif
236.endm
237
238/* Recursively expand calls to _serial_putc for every byte
239 * passed to us. Append a newline when we're all done.
240 */
241.macro _serial_early_putc byte:req morebytes:vararg
242#ifdef CONFIG_DEBUG_EARLY_SERIAL
243 R0 = \byte;
244 call _serial_putc;
245.ifnb \morebytes
246 _serial_early_putc \morebytes
247.else
248.if (\byte != '\n')
249 _serial_early_putc '\n'
250.endif
251.endif
252#endif
253.endm
254
255/* Wrapper around recurisve _serial_early_putc macro which
256 * simply prepends the string "Early: "
257 */
258.macro serial_early_putc byte:req morebytes:vararg
259#ifdef CONFIG_DEBUG_EARLY_SERIAL
260 _serial_early_putc 'E', 'a', 'r', 'l', 'y', ':', ' ', \byte, \morebytes
261#endif
262.endm
263
264/* Since we embed the string right into our .text section, we need
265 * to find its address. We do this by getting our PC and adding 2
266 * bytes (which is the length of the jump instruction). Then we
267 * pass this address to serial_puts().
268 */
269#ifdef CONFIG_DEBUG_EARLY_SERIAL
270# define serial_early_puts(str) \
271 call _get_pc; \
272 jump 1f; \
273 .ascii "Early:"; \
274 .ascii __FILE__; \
275 .ascii ": "; \
276 .ascii str; \
277 .asciz "\n"; \
278 .align 4; \
2791: \
280 R0 += 2; \
281 call _serial_puts;
282#else
283# define serial_early_puts(str)
284#endif
285
286#endif
287
288#endif