3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Author: Igor Lisitsin <igor@emcraft.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * The controllers are configured to loopback mode and several
32 * characters are transmitted.
39 #if CONFIG_POST & CFG_POST_UART
41 #include <asm/processor.h>
44 #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
45 #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
46 #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000500
47 #define UART3_BASE CFG_PERIPHERAL_BASE + 0x00000600
49 #define CR0_MASK 0xdfffffff
50 #define CR0_EXTCLK_ENA 0x00800000
51 #define CR0_UDIV_POS 0
52 #define UDIV_SUBTRACT 0
53 #define UART0_SDR sdr_uart0
54 #define UART1_SDR sdr_uart1
55 #define UART2_SDR sdr_uart2
56 #define UART3_SDR sdr_uart3
57 #define MFREG(a, d) mfsdr(a, d)
58 #define MTREG(a, d) mtsdr(a, d)
76 #define asyncLSRDataReady1 0x01
77 #define asyncLSROverrunError1 0x02
78 #define asyncLSRParityError1 0x04
79 #define asyncLSRFramingError1 0x08
80 #define asyncLSRBreakInterrupt1 0x10
81 #define asyncLSRTxHoldEmpty1 0x20
82 #define asyncLSRTxShiftEmpty1 0x40
83 #define asyncLSRRxFifoError1 0x80
85 DECLARE_GLOBAL_DATA_PTR;
87 #if !defined(CFG_EXT_SERIAL_CLOCK)
88 static void serial_divs (int baudrate, unsigned long *pudiv,
89 unsigned short *pbdiv)
92 unsigned long div; /* total divisor udiv * bdiv */
93 unsigned long umin; /* minimum udiv */
94 unsigned short diff; /* smallest diff */
95 unsigned long udiv; /* best udiv */
96 unsigned short idiff; /* current diff */
97 unsigned short ibdiv; /* current bdiv */
99 unsigned long est; /* current estimate */
101 get_sys_info(&sysinfo);
103 udiv = 32; /* Assume lowest possible serial clk */
104 div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
105 umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
106 diff = 32; /* highest possible */
108 /* i is the test udiv value -- start with the largest
109 * possible (32) to minimize serial clock and constrain
112 for (i = 32; i > umin; i--) {
115 idiff = (est > div) ? (est-div) : (div-est);
118 break; /* can't do better */
119 } else if (idiff < diff) {
120 udiv = i; /* best so far */
121 diff = idiff; /* update lowest diff*/
130 static int uart_post_init (unsigned long dev_base)
136 #ifdef CFG_EXT_SERIAL_CLOCK
141 for (i = 0; i < 3500; i++) {
142 if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
146 MFREG(UART0_SDR, reg);
149 #ifdef CFG_EXT_SERIAL_CLOCK
150 reg |= CR0_EXTCLK_ENA;
152 tmp = gd->baudrate * 16;
153 bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
155 /* For 440, the cpu clock is on divider chain A, UART on divider
156 * chain B ... so cpu clock is irrelevant. Get the "optimized"
157 * values that are subject to the 1/2 opb clock constraint
159 serial_divs (gd->baudrate, &udiv, &bdiv);
162 reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
165 * Configure input clock to baudrate generator for all
166 * available serial ports here
168 MTREG(UART0_SDR, reg);
169 #if defined(UART1_SDR)
170 MTREG(UART1_SDR, reg);
172 #if defined(UART2_SDR)
173 MTREG(UART2_SDR, reg);
175 #if defined(UART3_SDR)
176 MTREG(UART3_SDR, reg);
179 out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
180 out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
181 out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
182 out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
183 out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
184 out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
185 val = in8(dev_base + UART_LSR); /* clear line status */
186 val = in8(dev_base + UART_RBR); /* read receive buffer */
187 out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
188 out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
193 static void uart_post_putc (unsigned long dev_base, char c)
197 out8 (dev_base + UART_THR, c); /* put character out */
199 /* Wait for transfer completion */
200 for (i = 0; i < 3500; i++) {
201 if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
207 static int uart_post_getc (unsigned long dev_base)
211 /* Wait for character available */
212 for (i = 0; i < 3500; i++) {
213 if (in8 (dev_base + UART_LSR) & asyncLSRDataReady1)
217 return 0xff & in8 (dev_base + UART_RBR);
220 static int test_ctlr (unsigned long dev_base, int index)
223 char test_str[] = "*** UART Test String ***\r\n";
226 uart_post_init (dev_base);
228 for (i = 0; i < sizeof (test_str) - 1; i++) {
229 uart_post_putc (dev_base, test_str[i]);
230 if (uart_post_getc (dev_base) != test_str[i])
236 post_log ("uart%d test failed\n", index);
241 int uart_post_test (int flags)
244 static unsigned long base[] = {
245 UART0_BASE, UART1_BASE, UART2_BASE, UART3_BASE
248 for (i = 0; i < sizeof (base) / sizeof (base[0]); i++) {
249 if (test_ctlr (base[i], i))
252 serial_reinit_all ();
257 #endif /* CONFIG_POST & CFG_POST_UART */
258 #endif /* CONFIG_POST */