Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - serial.c Serial driver for BF533 |
| 3 | * |
Aubrey Li | 314d22f | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 4 | * Copyright (c) 2005-2007 Analog Devices Inc. |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 5 | * |
| 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 Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 16 | * |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 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> |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 21 | * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 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 Li | 314d22f | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 41 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 42 | * MA 02110-1301 USA |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | #include <common.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 46 | #include <asm/system.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 47 | #include <asm/bitops.h> |
| 48 | #include <asm/delay.h> |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 49 | #include <asm/io.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 50 | #include "bf533_serial.h" |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 51 | #include <asm/mach-common/bits/uart.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 52 | |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 55 | unsigned long pll_div_fact; |
| 56 | |
| 57 | void calc_baud(void) |
| 58 | { |
| 59 | unsigned char i; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 60 | int temp; |
| 61 | u_long sclk = get_sclk(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 62 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 63 | for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) { |
| 64 | temp = sclk / (baud_table[i] * 8); |
| 65 | if ((temp & 0x1) == 1) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 66 | temp++; |
| 67 | } |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 68 | temp = temp / 2; |
| 69 | hw_baud_table[i].dl_high = (temp >> 8) & 0xFF; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 70 | hw_baud_table[i].dl_low = (temp) & 0xFF; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void serial_setbrg(void) |
| 75 | { |
| 76 | int i; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 77 | |
| 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 Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 86 | *pUART_GCTL |= UCEN; |
| 87 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 88 | |
| 89 | /* Set DLAB in LCR to Access DLL and DLH */ |
| 90 | ACCESS_LATCH; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 91 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 92 | |
| 93 | *pUART_DLL = hw_baud_table[i].dl_low; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 94 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 95 | *pUART_DLH = hw_baud_table[i].dl_high; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 96 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 97 | |
| 98 | /* Clear DLAB in LCR to Access THR RBR IER */ |
| 99 | ACCESS_PORT_IER; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 100 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 101 | |
| 102 | /* Enable ERBFI and ELSI interrupts |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 103 | * to poll SIC_ISR register*/ |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 104 | *pUART_IER = ELSI | ERBFI | ETBEI; |
| 105 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 106 | |
| 107 | /* Set LCR to Word Lengh 8-bit word select */ |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 108 | *pUART_LCR = WLS_8; |
| 109 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 110 | |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | int serial_init(void) |
| 115 | { |
| 116 | serial_setbrg(); |
| 117 | return (0); |
| 118 | } |
| 119 | |
| 120 | void serial_putc(const char c) |
| 121 | { |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 122 | if ((*pUART_LSR) & TEMT) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 123 | if (c == '\n') |
| 124 | serial_putc('\r'); |
| 125 | |
| 126 | local_put_char(c); |
| 127 | } |
| 128 | |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 129 | while (!((*pUART_LSR) & TEMT)) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 130 | SYNC_ALL; |
| 131 | |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | int serial_tstc(void) |
| 136 | { |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 137 | if (*pUART_LSR & DR) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 138 | return 1; |
| 139 | else |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int serial_getc(void) |
| 144 | { |
| 145 | unsigned short uart_lsr_val, uart_rbr_val; |
| 146 | unsigned long isr_val; |
| 147 | int ret; |
| 148 | |
| 149 | /* Poll for RX Interrupt */ |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 150 | while (!serial_tstc()) |
| 151 | continue; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 152 | asm("csync;"); |
| 153 | |
| 154 | uart_lsr_val = *pUART_LSR; /* Clear status bit */ |
| 155 | uart_rbr_val = *pUART_RBR; /* getc() */ |
| 156 | |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 157 | if (uart_lsr_val & (OE|PE|FE|BI)) { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 158 | ret = -1; |
| 159 | } else { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 160 | ret = uart_rbr_val & 0xff; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | void serial_puts(const char *s) |
| 167 | { |
| 168 | while (*s) { |
| 169 | serial_putc(*s++); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void local_put_char(char ch) |
| 174 | { |
| 175 | int flags = 0; |
| 176 | unsigned long isr_val; |
| 177 | |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 178 | /* Poll for TX Interruput */ |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame^] | 179 | while (!(*pUART_LSR & THRE)) |
| 180 | continue; |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 181 | asm("csync;"); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 182 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 183 | *pUART_THR = ch; /* putc() */ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 184 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 185 | return; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 186 | } |