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 #ifndef CONFIG_UART_CONSOLE
18 # define CONFIG_UART_CONSOLE 0
21 #ifdef CONFIG_DEBUG_EARLY_SERIAL
22 # define BFIN_DEBUG_EARLY_SERIAL 1
24 # define BFIN_DEBUG_EARLY_SERIAL 0
27 #define LOB(x) ((x) & 0xFF)
28 #define HIB(x) (((x) >> 8) & 0xFF)
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
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)
90 # define ACCESS_LATCH()
91 # define ACCESS_PORT_IER()
92 # define CLEAR_IER() (*pUART_IERC = 0)
94 # define ACCESS_LATCH() (*pUART_LCR |= DLAB)
95 # define ACCESS_PORT_IER() (*pUART_LCR &= ~DLAB)
96 # define CLEAR_IER() (*pUART_IER = 0)
99 __attribute__((always_inline))
100 static inline void serial_do_portmux(void)
102 #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 */
111 #elif defined(__ADSPBF52x__)
112 # 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 */
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;
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 */
143 __attribute__((always_inline))
144 static inline void serial_early_init(void)
146 /* handle portmux crap on different Blackfins */
149 /* always enable UART -- avoids anomalies 05000309 and 05000350 */
152 /* Set LCR to Word Lengh 8-bit word select */
158 __attribute__((always_inline))
159 static inline void serial_early_put_div(uint16_t divisor)
161 /* Set DLAB in LCR to Access DLL and DLH */
165 /* Program the divisor to get the baud rate we want */
166 *pUART_DLL = LOB(divisor);
167 *pUART_DLH = HIB(divisor);
170 /* Clear DLAB in LCR to Access THR RBR IER */
175 __attribute__((always_inline))
176 static inline uint16_t serial_early_get_div(void)
178 /* Set DLAB in LCR to Access DLL and DLH */
182 uint8_t dll = *pUART_DLL;
183 uint8_t dlh = *pUART_DLH;
184 uint16_t divisor = (dlh << 8) | dll;
186 /* Clear DLAB in LCR to Access THR RBR IER */
193 __attribute__((always_inline))
194 static inline void serial_early_set_baud(uint32_t baud)
196 /* Translate from baud into divisor in terms of SCLK. The
197 * weird multiplication is to make sure we over sample just
198 * a little rather than under sample the incoming signals.
200 serial_early_put_div((get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
203 #ifndef BFIN_IN_INITCODE
204 __attribute__((always_inline))
205 static inline void serial_early_puts(const char *s)
207 if (BFIN_DEBUG_EARLY_SERIAL) {
208 serial_puts("Early: ");
216 .macro serial_early_init
217 #ifdef CONFIG_DEBUG_EARLY_SERIAL
218 call _serial_initialize;
222 .macro serial_early_set_baud
223 #ifdef CONFIG_DEBUG_EARLY_SERIAL
224 R0.L = LO(CONFIG_BAUDRATE);
225 R0.H = HI(CONFIG_BAUDRATE);
226 call _serial_set_baud;
230 /* Since we embed the string right into our .text section, we need
231 * to find its address. We do this by getting our PC and adding 2
232 * bytes (which is the length of the jump instruction). Then we
233 * pass this address to serial_puts().
235 #ifdef CONFIG_DEBUG_EARLY_SERIAL
236 # define serial_early_puts(str) \
249 # define serial_early_puts(str)