blob: ce39148f83f68446bb4a10f419c322cc301fe641 [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))
Mike Frysinger84451302008-12-10 12:33:54 -0500159static inline void serial_early_put_div(uint16_t divisor)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400160{
Mike Frysinger84451302008-12-10 12:33:54 -0500161 /* Set DLAB in LCR to Access DLL and DLH */
162 ACCESS_LATCH();
163 SSYNC();
164
165 /* Program the divisor to get the baud rate we want */
166 *pUART_DLL = LOB(divisor);
167 *pUART_DLH = HIB(divisor);
168 SSYNC();
169
170 /* Clear DLAB in LCR to Access THR RBR IER */
171 ACCESS_PORT_IER();
172 SSYNC();
173}
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400174
Mike Frysinger84451302008-12-10 12:33:54 -0500175__attribute__((always_inline))
176static inline uint16_t serial_early_get_div(void)
177{
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400178 /* Set DLAB in LCR to Access DLL and DLH */
179 ACCESS_LATCH();
180 SSYNC();
181
182 uint8_t dll = *pUART_DLL;
183 uint8_t dlh = *pUART_DLH;
184 uint16_t divisor = (dlh << 8) | dll;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400185
186 /* Clear DLAB in LCR to Access THR RBR IER */
187 ACCESS_PORT_IER();
188 SSYNC();
189
Mike Frysinger84451302008-12-10 12:33:54 -0500190 return divisor;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400191}
192
193__attribute__((always_inline))
194static inline void serial_early_set_baud(uint32_t baud)
195{
Mike Frysingerae2d4672008-04-20 03:11:53 -0400196 /* 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.
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400199 */
Mike Frysinger84451302008-12-10 12:33:54 -0500200 serial_early_put_div((get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400201}
202
203#ifndef BFIN_IN_INITCODE
204__attribute__((always_inline))
205static inline void serial_early_puts(const char *s)
206{
207 if (BFIN_DEBUG_EARLY_SERIAL) {
208 serial_puts("Early: ");
209 serial_puts(s);
210 }
211}
212#endif
213
214#else
215
216.macro serial_early_init
217#ifdef CONFIG_DEBUG_EARLY_SERIAL
218 call _serial_initialize;
219#endif
220.endm
221
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;
227#endif
228.endm
229
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400230/* 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().
234 */
235#ifdef CONFIG_DEBUG_EARLY_SERIAL
236# define serial_early_puts(str) \
237 call _get_pc; \
238 jump 1f; \
239 .ascii "Early:"; \
240 .ascii __FILE__; \
241 .ascii ": "; \
242 .ascii str; \
243 .asciz "\n"; \
244 .align 4; \
2451: \
246 R0 += 2; \
247 call _serial_puts;
248#else
249# define serial_early_puts(str)
250#endif
251
252#endif
253
254#endif