blob: a398fd5f84a68676606cd93bd9aea68d506ac4ec [file] [log] [blame]
Aubrey Li65458982007-03-20 18:16:24 +08001/*
2 * U-boot - serial.c Serial driver for BF561
3 *
Aubrey Li155fd762007-04-05 18:31:18 +08004 * Copyright (c) 2005-2007 Analog Devices Inc.
Aubrey Li65458982007-03-20 18:16:24 +08005 *
6 * This file is based on
7 * bf533_serial.c: Serial driver for BlackFin BF533 DSP internal UART.
8 * Copyright (c) 2003 Bas Vermeulen <bas@buyways.nl>,
9 * BuyWays B.V. (www.buyways.nl)
10 *
11 * Based heavily on blkfinserial.c
12 * blkfinserial.c: Serial driver for BlackFin DSP internal USRTs.
13 * Copyright(c) 2003 Metrowerks <mwaddel@metrowerks.com>
14 * Copyright(c) 2001 Tony Z. Kou <tonyko@arcturusnetworks.com>
15 * Copyright(c) 2001-2002 Arcturus Networks Inc. <www.arcturusnetworks.com>
16 *
17 * Based on code from 68328 version serial driver imlpementation which was:
18 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
19 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
20 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
21 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
22 *
23 * (C) Copyright 2000-2004
24 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
25 *
26 * See file CREDITS for list of people who contributed to this
27 * project.
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License as
31 * published by the Free Software Foundation; either version 2 of
32 * the License, or (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
Aubrey Li155fd762007-04-05 18:31:18 +080041 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
42 * MA 02110-1301 USA
Aubrey Li65458982007-03-20 18:16:24 +080043 */
44
45#include <common.h>
Aubrey Li65458982007-03-20 18:16:24 +080046#include <asm/system.h>
Aubrey Li65458982007-03-20 18:16:24 +080047#include <asm/bitops.h>
48#include <asm/delay.h>
Aubrey Li65458982007-03-20 18:16:24 +080049#include "serial.h"
50#include <asm/io.h>
Mike Frysingerd4d77302008-02-04 19:26:55 -050051#include <asm/mach-common/bits/uart.h>
Aubrey Li65458982007-03-20 18:16:24 +080052
Wolfgang Denk1218abf2007-09-15 20:48:41 +020053DECLARE_GLOBAL_DATA_PTR;
54
Aubrey Li65458982007-03-20 18:16:24 +080055unsigned long pll_div_fact;
56
57void calc_baud(void)
58{
59 unsigned char i;
60 int temp;
61 u_long sclk = get_sclk();
62
63 for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) {
64 temp = sclk / (baud_table[i] * 8);
65 if ((temp & 0x1) == 1) {
66 temp++;
67 }
68 temp = temp / 2;
69 hw_baud_table[i].dl_high = (temp >> 8) & 0xFF;
70 hw_baud_table[i].dl_low = (temp) & 0xFF;
71 }
72}
73
74void serial_setbrg(void)
75{
76 int i;
Aubrey Li65458982007-03-20 18:16:24 +080077
78 calc_baud();
79
80 for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) {
81 if (gd->baudrate == baud_table[i])
82 break;
83 }
84
85 /* Enable UART */
Mike Frysingerd4d77302008-02-04 19:26:55 -050086 *pUART_GCTL |= UCEN;
87 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +080088
89 /* Set DLAB in LCR to Access DLL and DLH */
90 ACCESS_LATCH;
Mike Frysingerd4d77302008-02-04 19:26:55 -050091 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +080092
93 *pUART_DLL = hw_baud_table[i].dl_low;
Mike Frysingerd4d77302008-02-04 19:26:55 -050094 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +080095 *pUART_DLH = hw_baud_table[i].dl_high;
Mike Frysingerd4d77302008-02-04 19:26:55 -050096 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +080097
98 /* Clear DLAB in LCR to Access THR RBR IER */
99 ACCESS_PORT_IER;
Mike Frysingerd4d77302008-02-04 19:26:55 -0500100 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +0800101
102 /*
103 * Enable ERBFI and ELSI interrupts
104 * to poll SIC_ISR register
105 */
Mike Frysingerd4d77302008-02-04 19:26:55 -0500106 *pUART_IER = ELSI | ERBFI | ETBEI;
107 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +0800108
109 /* Set LCR to Word Lengh 8-bit word select */
Mike Frysingerd4d77302008-02-04 19:26:55 -0500110 *pUART_LCR = WLS_8;
111 SSYNC();
Aubrey Li65458982007-03-20 18:16:24 +0800112
113 return;
114}
115
116int serial_init(void)
117{
118 serial_setbrg();
119 return (0);
120}
121
122void serial_putc(const char c)
123{
Mike Frysingerd4d77302008-02-04 19:26:55 -0500124 if ((*pUART_LSR) & TEMT) {
Aubrey Li65458982007-03-20 18:16:24 +0800125 if (c == '\n')
126 serial_putc('\r');
127
128 local_put_char(c);
129 }
130
Mike Frysingerd4d77302008-02-04 19:26:55 -0500131 while (!((*pUART_LSR) & TEMT))
Aubrey Li65458982007-03-20 18:16:24 +0800132 SYNC_ALL;
133
134 return;
135}
136
137int serial_tstc(void)
138{
Mike Frysingerd4d77302008-02-04 19:26:55 -0500139 if (*pUART_LSR & DR)
Aubrey Li65458982007-03-20 18:16:24 +0800140 return 1;
141 else
142 return 0;
143}
144
145int serial_getc(void)
146{
147 unsigned short uart_lsr_val, uart_rbr_val;
148 unsigned long isr_val;
149 int ret;
150
151 /* Poll for RX Interrupt */
Mike Frysingerd4d77302008-02-04 19:26:55 -0500152 while (!serial_tstc())
153 continue;
Aubrey Li65458982007-03-20 18:16:24 +0800154 asm("csync;");
155
156 uart_lsr_val = *pUART_LSR; /* Clear status bit */
157 uart_rbr_val = *pUART_RBR; /* getc() */
158
Mike Frysingerd4d77302008-02-04 19:26:55 -0500159 if (uart_lsr_val & (OE|PE|FE|BI)) {
Aubrey Li65458982007-03-20 18:16:24 +0800160 ret = -1;
161 } else {
162 ret = uart_rbr_val & 0xff;
163 }
164
165 return ret;
166}
167
168void serial_puts(const char *s)
169{
170 while (*s) {
171 serial_putc(*s++);
172 }
173}
174
175static void local_put_char(char ch)
176{
177 int flags = 0;
178 unsigned long isr_val;
179
Aubrey Li65458982007-03-20 18:16:24 +0800180 /* Poll for TX Interruput */
Mike Frysingerd4d77302008-02-04 19:26:55 -0500181 while (!(*pUART_LSR & THRE))
182 continue;
Aubrey Li65458982007-03-20 18:16:24 +0800183 asm("csync;");
184
185 *pUART_THR = ch; /* putc() */
186
Aubrey Li65458982007-03-20 18:16:24 +0800187 return;
188}