blob: 8ac6e3ff64e328ed681bb44ea3e1da10397a1454 [file] [log] [blame]
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01001/*
2 * U-boot - serial.c Serial driver for BF533
3 *
Aubrey Li155fd762007-04-05 18:31:18 +08004 * Copyright (c) 2005-2007 Analog Devices Inc.
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01005 *
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>
Wolfgang Denk8e7b7032006-03-12 02:55:22 +010016 *
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010017 * 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>
Wolfgang Denk8e7b7032006-03-12 02:55:22 +010021 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010022 *
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
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010043 */
44
45#include <common.h>
46#include <asm/irq.h>
47#include <asm/system.h>
48#include <asm/segment.h>
49#include <asm/bitops.h>
50#include <asm/delay.h>
51#include <asm/uaccess.h>
Aubrey Li8440bb12007-03-12 00:25:14 +080052#include <asm/io.h>
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010053#include "bf533_serial.h"
54
Aubrey Li8440bb12007-03-12 00:25:14 +080055DECLARE_GLOBAL_DATA_PTR;
56
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010057unsigned long pll_div_fact;
58
59void calc_baud(void)
60{
61 unsigned char i;
Aubrey.Li3f0606a2007-03-09 13:38:44 +080062 int temp;
63 u_long sclk = get_sclk();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010064
Aubrey.Li3f0606a2007-03-09 13:38:44 +080065 for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) {
66 temp = sclk / (baud_table[i] * 8);
67 if ((temp & 0x1) == 1) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010068 temp++;
69 }
Aubrey.Li3f0606a2007-03-09 13:38:44 +080070 temp = temp / 2;
71 hw_baud_table[i].dl_high = (temp >> 8) & 0xFF;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010072 hw_baud_table[i].dl_low = (temp) & 0xFF;
73 }
74}
75
76void serial_setbrg(void)
77{
78 int i;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010079
80 calc_baud();
81
82 for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) {
83 if (gd->baudrate == baud_table[i])
84 break;
85 }
86
87 /* Enable UART */
88 *pUART_GCTL |= UART_GCTL_UCEN;
Aubrey Li8440bb12007-03-12 00:25:14 +080089 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010090
91 /* Set DLAB in LCR to Access DLL and DLH */
92 ACCESS_LATCH;
Aubrey Li8440bb12007-03-12 00:25:14 +080093 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010094
95 *pUART_DLL = hw_baud_table[i].dl_low;
Aubrey Li8440bb12007-03-12 00:25:14 +080096 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010097 *pUART_DLH = hw_baud_table[i].dl_high;
Aubrey Li8440bb12007-03-12 00:25:14 +080098 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010099
100 /* Clear DLAB in LCR to Access THR RBR IER */
101 ACCESS_PORT_IER;
Aubrey Li8440bb12007-03-12 00:25:14 +0800102 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100103
104 /* Enable ERBFI and ELSI interrupts
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800105 * to poll SIC_ISR register*/
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100106 *pUART_IER = UART_IER_ELSI | UART_IER_ERBFI | UART_IER_ETBEI;
Aubrey Li8440bb12007-03-12 00:25:14 +0800107 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100108
109 /* Set LCR to Word Lengh 8-bit word select */
110 *pUART_LCR = UART_LCR_WLS8;
Aubrey Li8440bb12007-03-12 00:25:14 +0800111 sync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100112
113 return;
114}
115
116int serial_init(void)
117{
118 serial_setbrg();
119 return (0);
120}
121
122void serial_putc(const char c)
123{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800124 if ((*pUART_LSR) & UART_LSR_TEMT) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100125 if (c == '\n')
126 serial_putc('\r');
127
128 local_put_char(c);
129 }
130
131 while (!((*pUART_LSR) & UART_LSR_TEMT))
132 SYNC_ALL;
133
134 return;
135}
136
137int serial_tstc(void)
138{
139 if (*pUART_LSR & UART_LSR_DR)
140 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 */
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800152 while (!((isr_val =
153 *(volatile unsigned long *)SIC_ISR) & IRQ_UART_RX_BIT)) ;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100154 asm("csync;");
155
156 uart_lsr_val = *pUART_LSR; /* Clear status bit */
157 uart_rbr_val = *pUART_RBR; /* getc() */
158
159 if (isr_val & IRQ_UART_ERROR_BIT) {
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800160 ret = -1;
161 } else {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100162 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
180 save_and_cli(flags);
181
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100182 /* Poll for TX Interruput */
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800183 while (!((isr_val = *pSIC_ISR) & IRQ_UART_TX_BIT)) ;
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100184 asm("csync;");
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100185
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800186 *pUART_THR = ch; /* putc() */
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100187
188 if (isr_val & IRQ_UART_ERROR_BIT) {
189 printf("?");
190 }
191
192 restore_flags(flags);
193
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800194 return;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100195}