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> |
| 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 Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 52 | #include <asm/io.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 53 | #include "bf533_serial.h" |
| 54 | |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
| 56 | |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 57 | unsigned long pll_div_fact; |
| 58 | |
| 59 | void calc_baud(void) |
| 60 | { |
| 61 | unsigned char i; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 62 | int temp; |
| 63 | u_long sclk = get_sclk(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 64 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 65 | for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) { |
| 66 | temp = sclk / (baud_table[i] * 8); |
| 67 | if ((temp & 0x1) == 1) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 68 | temp++; |
| 69 | } |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 70 | temp = temp / 2; |
| 71 | hw_baud_table[i].dl_high = (temp >> 8) & 0xFF; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 72 | hw_baud_table[i].dl_low = (temp) & 0xFF; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void serial_setbrg(void) |
| 77 | { |
| 78 | int i; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 79 | DECLARE_GLOBAL_DATA_PTR; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 80 | |
| 81 | calc_baud(); |
| 82 | |
| 83 | for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) { |
| 84 | if (gd->baudrate == baud_table[i]) |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | /* Enable UART */ |
| 89 | *pUART_GCTL |= UART_GCTL_UCEN; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 90 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 91 | |
| 92 | /* Set DLAB in LCR to Access DLL and DLH */ |
| 93 | ACCESS_LATCH; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 94 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 95 | |
| 96 | *pUART_DLL = hw_baud_table[i].dl_low; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 97 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 98 | *pUART_DLH = hw_baud_table[i].dl_high; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 99 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 100 | |
| 101 | /* Clear DLAB in LCR to Access THR RBR IER */ |
| 102 | ACCESS_PORT_IER; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 103 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 104 | |
| 105 | /* Enable ERBFI and ELSI interrupts |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 106 | * to poll SIC_ISR register*/ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 107 | *pUART_IER = UART_IER_ELSI | UART_IER_ERBFI | UART_IER_ETBEI; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 108 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 109 | |
| 110 | /* Set LCR to Word Lengh 8-bit word select */ |
| 111 | *pUART_LCR = UART_LCR_WLS8; |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 112 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 113 | |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | int serial_init(void) |
| 118 | { |
| 119 | serial_setbrg(); |
| 120 | return (0); |
| 121 | } |
| 122 | |
| 123 | void serial_putc(const char c) |
| 124 | { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 125 | if ((*pUART_LSR) & UART_LSR_TEMT) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 126 | if (c == '\n') |
| 127 | serial_putc('\r'); |
| 128 | |
| 129 | local_put_char(c); |
| 130 | } |
| 131 | |
| 132 | while (!((*pUART_LSR) & UART_LSR_TEMT)) |
| 133 | SYNC_ALL; |
| 134 | |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | int serial_tstc(void) |
| 139 | { |
| 140 | if (*pUART_LSR & UART_LSR_DR) |
| 141 | return 1; |
| 142 | else |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int serial_getc(void) |
| 147 | { |
| 148 | unsigned short uart_lsr_val, uart_rbr_val; |
| 149 | unsigned long isr_val; |
| 150 | int ret; |
| 151 | |
| 152 | /* Poll for RX Interrupt */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 153 | while (!((isr_val = |
| 154 | *(volatile unsigned long *)SIC_ISR) & IRQ_UART_RX_BIT)) ; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 155 | asm("csync;"); |
| 156 | |
| 157 | uart_lsr_val = *pUART_LSR; /* Clear status bit */ |
| 158 | uart_rbr_val = *pUART_RBR; /* getc() */ |
| 159 | |
| 160 | if (isr_val & IRQ_UART_ERROR_BIT) { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 161 | ret = -1; |
| 162 | } else { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 163 | ret = uart_rbr_val & 0xff; |
| 164 | } |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | void serial_puts(const char *s) |
| 170 | { |
| 171 | while (*s) { |
| 172 | serial_putc(*s++); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static void local_put_char(char ch) |
| 177 | { |
| 178 | int flags = 0; |
| 179 | unsigned long isr_val; |
| 180 | |
| 181 | save_and_cli(flags); |
| 182 | |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 183 | /* Poll for TX Interruput */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 184 | while (!((isr_val = *pSIC_ISR) & IRQ_UART_TX_BIT)) ; |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 185 | asm("csync;"); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 186 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 187 | *pUART_THR = ch; /* putc() */ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 188 | |
| 189 | if (isr_val & IRQ_UART_ERROR_BIT) { |
| 190 | printf("?"); |
| 191 | } |
| 192 | |
| 193 | restore_flags(flags); |
| 194 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 195 | return; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 196 | } |