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.
6 * Copyright (c) 2004-2007 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
11 #ifndef __BFIN_CPU_SERIAL_H__
12 #define __BFIN_CPU_SERIAL_H__
14 #include <asm/blackfin.h>
15 #include <asm/mach-common/bits/uart.h>
17 #ifdef CONFIG_DEBUG_EARLY_SERIAL
18 # define BFIN_DEBUG_EARLY_SERIAL 1
20 # define BFIN_DEBUG_EARLY_SERIAL 0
23 #define LOB(x) ((x) & 0xFF)
24 #define HIB(x) (((x) >> 8) & 0xFF)
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
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)
86 # define ACCESS_LATCH()
87 # define ACCESS_PORT_IER()
88 # define CLEAR_IER() (*pUART_IERC = 0)
90 # define ACCESS_LATCH() (*pUART_LCR |= DLAB)
91 # define ACCESS_PORT_IER() (*pUART_LCR &= ~DLAB)
92 # define CLEAR_IER() (*pUART_IER = 0)
95 __attribute__((always_inline))
96 static inline void serial_do_portmux(void)
98 #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 */
107 #elif defined(__ADSPBF52x__)
108 # 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 */
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;
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 */
139 __attribute__((always_inline))
140 static inline void serial_early_init(void)
142 /* handle portmux crap on different Blackfins */
148 /* Set LCR to Word Lengh 8-bit word select */
154 __attribute__((always_inline))
155 static inline uint32_t serial_early_get_baud(void)
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.
161 if ((*pUART_GCTL & UCEN) != UCEN)
164 #if (0) /* See comment for serial_reset_baud() in initcode.c */
165 /* Set DLAB in LCR to Access DLL and DLH */
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);
174 /* Clear DLAB in LCR to Access THR RBR IER */
180 return CONFIG_BAUDRATE;
184 __attribute__((always_inline))
185 static inline void serial_early_set_baud(uint32_t baud)
187 /* 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.
191 uint16_t divisor = (get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230;
193 /* Set DLAB in LCR to Access DLL and DLH */
197 /* Program the divisor to get the baud rate we want */
198 *pUART_DLL = LOB(divisor);
199 *pUART_DLH = HIB(divisor);
202 /* Clear DLAB in LCR to Access THR RBR IER */
207 #ifndef BFIN_IN_INITCODE
208 __attribute__((always_inline))
209 static inline void serial_early_puts(const char *s)
211 if (BFIN_DEBUG_EARLY_SERIAL) {
212 serial_puts("Early: ");
220 .macro serial_early_init
221 #ifdef CONFIG_DEBUG_EARLY_SERIAL
222 call _serial_initialize;
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;
234 /* Recursively expand calls to _serial_putc for every byte
235 * passed to us. Append a newline when we're all done.
237 .macro _serial_early_putc byte:req morebytes:vararg
238 #ifdef CONFIG_DEBUG_EARLY_SERIAL
242 _serial_early_putc \morebytes
245 _serial_early_putc '\n'
251 /* Wrapper around recurisve _serial_early_putc macro which
252 * simply prepends the string "Early: "
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
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().
265 #ifdef CONFIG_DEBUG_EARLY_SERIAL
266 # define serial_early_puts(str) \
279 # define serial_early_puts(str)