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