blob: ec40c266ac58a1b2bf2294221206f4edeec153ea [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{
98#ifdef __ADSPBF52x__
99# define DO_MUX(port, mux, tx, rx) \
100 bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
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, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
104 case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
105 }
106 SSYNC();
107#elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
108# define DO_MUX(func, tx, rx) \
109 bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~(func)); \
110 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | PF##tx | PF##rx);
111 switch (CONFIG_UART_CONSOLE) {
112 case 0: DO_MUX(PFDE, 0, 1); break;
113 case 1: DO_MUX(PFTE, 2, 3); break;
114 }
115 SSYNC();
116#elif defined(__ADSPBF54x__)
117# define DO_MUX(port, tx, rx) \
118 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); \
119 bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
120 switch (CONFIG_UART_CONSOLE) {
121 case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
122 case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
123 case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
124 case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
125 }
126 SSYNC();
127#endif
128}
129
130__attribute__((always_inline))
131static inline void serial_early_init(void)
132{
133 /* handle portmux crap on different Blackfins */
134 serial_do_portmux();
135
136 /* Enable UART */
137 *pUART_GCTL = UCEN;
138
139 /* Set LCR to Word Lengh 8-bit word select */
140 *pUART_LCR = WLS_8;
141
142 SSYNC();
143}
144
145__attribute__((always_inline))
146static inline uint32_t serial_early_get_baud(void)
147{
148 /* If the UART isnt enabled, then we are booting an LDR
149 * from a non-UART source (so like flash) which means
150 * the baud rate here is meaningless.
151 */
152 if ((*pUART_GCTL & UCEN) != UCEN)
153 return 0;
154
155#if (0) /* See comment for serial_reset_baud() in initcode.c */
156 /* Set DLAB in LCR to Access DLL and DLH */
157 ACCESS_LATCH();
158 SSYNC();
159
160 uint8_t dll = *pUART_DLL;
161 uint8_t dlh = *pUART_DLH;
162 uint16_t divisor = (dlh << 8) | dll;
163 uint32_t baud = get_sclk() / (divisor * 16);
164
165 /* Clear DLAB in LCR to Access THR RBR IER */
166 ACCESS_PORT_IER();
167 SSYNC();
168
169 return baud;
170#else
171 return CONFIG_BAUDRATE;
172#endif
173}
174
175__attribute__((always_inline))
176static inline void serial_early_set_baud(uint32_t baud)
177{
Mike Frysinger25cd33d2008-04-20 03:11:53 -0400178 /* Translate from baud into divisor in terms of SCLK. The
179 * weird multiplication is to make sure we over sample just
180 * a little rather than under sample the incoming signals.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400181 */
Mike Frysinger25cd33d2008-04-20 03:11:53 -0400182 uint16_t divisor = (get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400183
184 /* Set DLAB in LCR to Access DLL and DLH */
185 ACCESS_LATCH();
186 SSYNC();
187
188 /* Program the divisor to get the baud rate we want */
189 *pUART_DLL = LOB(divisor);
190 *pUART_DLH = HIB(divisor);
191 SSYNC();
192
193 /* Clear DLAB in LCR to Access THR RBR IER */
194 ACCESS_PORT_IER();
195 SSYNC();
196}
197
198#ifndef BFIN_IN_INITCODE
199__attribute__((always_inline))
200static inline void serial_early_puts(const char *s)
201{
202 if (BFIN_DEBUG_EARLY_SERIAL) {
203 serial_puts("Early: ");
204 serial_puts(s);
205 }
206}
207#endif
208
209#else
210
211.macro serial_early_init
212#ifdef CONFIG_DEBUG_EARLY_SERIAL
213 call _serial_initialize;
214#endif
215.endm
216
217.macro serial_early_set_baud
218#ifdef CONFIG_DEBUG_EARLY_SERIAL
219 R0.L = LO(CONFIG_BAUDRATE);
220 R0.H = HI(CONFIG_BAUDRATE);
221 call _serial_set_baud;
222#endif
223.endm
224
225/* Recursively expand calls to _serial_putc for every byte
226 * passed to us. Append a newline when we're all done.
227 */
228.macro _serial_early_putc byte:req morebytes:vararg
229#ifdef CONFIG_DEBUG_EARLY_SERIAL
230 R0 = \byte;
231 call _serial_putc;
232.ifnb \morebytes
233 _serial_early_putc \morebytes
234.else
235.if (\byte != '\n')
236 _serial_early_putc '\n'
237.endif
238.endif
239#endif
240.endm
241
242/* Wrapper around recurisve _serial_early_putc macro which
243 * simply prepends the string "Early: "
244 */
245.macro serial_early_putc byte:req morebytes:vararg
246#ifdef CONFIG_DEBUG_EARLY_SERIAL
247 _serial_early_putc 'E', 'a', 'r', 'l', 'y', ':', ' ', \byte, \morebytes
248#endif
249.endm
250
251/* Since we embed the string right into our .text section, we need
252 * to find its address. We do this by getting our PC and adding 2
253 * bytes (which is the length of the jump instruction). Then we
254 * pass this address to serial_puts().
255 */
256#ifdef CONFIG_DEBUG_EARLY_SERIAL
257# define serial_early_puts(str) \
258 call _get_pc; \
259 jump 1f; \
260 .ascii "Early:"; \
261 .ascii __FILE__; \
262 .ascii ": "; \
263 .ascii str; \
264 .asciz "\n"; \
265 .align 4; \
2661: \
267 R0 += 2; \
268 call _serial_puts;
269#else
270# define serial_early_puts(str)
271#endif
272
273#endif
274
275#endif