Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - serial.c Serial driver for BF537 |
| 3 | * |
Aubrey Li | 155fd76 | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 4 | * Copyright (c) 2005-2007 Analog Devices Inc. |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 5 | * |
| 6 | * This file is based on |
| 7 | * bf537_serial.c: Serial driver for BlackFin BF537 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 Li | 155fd76 | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 41 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 42 | * MA 02110-1301 USA |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [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> |
| 52 | #include <asm/io.h> |
| 53 | #include "serial.h" |
| 54 | |
| 55 | unsigned long pll_div_fact; |
| 56 | |
| 57 | void 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 | |
| 74 | void serial_setbrg(void) |
| 75 | { |
| 76 | int i; |
| 77 | DECLARE_GLOBAL_DATA_PTR; |
| 78 | |
| 79 | calc_baud(); |
| 80 | |
| 81 | for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) { |
| 82 | if (gd->baudrate == baud_table[i]) |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | /* Enable UART */ |
| 87 | *pUART_GCTL |= UART_GCTL_UCEN; |
| 88 | sync(); |
| 89 | |
| 90 | /* Set DLAB in LCR to Access DLL and DLH */ |
| 91 | ACCESS_LATCH; |
| 92 | sync(); |
| 93 | |
| 94 | *pUART_DLL = hw_baud_table[i].dl_low; |
| 95 | sync(); |
| 96 | *pUART_DLH = hw_baud_table[i].dl_high; |
| 97 | sync(); |
| 98 | |
| 99 | /* Clear DLAB in LCR to Access THR RBR IER */ |
| 100 | ACCESS_PORT_IER; |
| 101 | sync(); |
| 102 | |
| 103 | /* Enable ERBFI and ELSI interrupts |
| 104 | * to poll SIC_ISR register*/ |
| 105 | *pUART_IER = UART_IER_ELSI | UART_IER_ERBFI | UART_IER_ETBEI; |
| 106 | sync(); |
| 107 | |
| 108 | /* Set LCR to Word Lengh 8-bit word select */ |
| 109 | *pUART_LCR = UART_LCR_WLS8; |
| 110 | sync(); |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | int serial_init(void) |
| 116 | { |
| 117 | serial_setbrg(); |
| 118 | return (0); |
| 119 | } |
| 120 | |
| 121 | void serial_putc(const char c) |
| 122 | { |
| 123 | if ((*pUART_LSR) & UART_LSR_TEMT) { |
| 124 | if (c == '\n') |
| 125 | serial_putc('\r'); |
| 126 | |
| 127 | local_put_char(c); |
| 128 | } |
| 129 | |
| 130 | while (!((*pUART_LSR) & UART_LSR_TEMT)) |
| 131 | SYNC_ALL; |
| 132 | |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | int serial_tstc(void) |
| 137 | { |
| 138 | if (*pUART_LSR & UART_LSR_DR) |
| 139 | return 1; |
| 140 | else |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int serial_getc(void) |
| 145 | { |
| 146 | unsigned short uart_lsr_val, uart_rbr_val; |
| 147 | unsigned long isr_val; |
| 148 | int ret; |
| 149 | |
| 150 | /* Poll for RX Interrupt */ |
| 151 | while (!((isr_val = |
| 152 | *(volatile unsigned long *)SIC_ISR) & IRQ_UART_RX_BIT)) ; |
| 153 | asm("csync;"); |
| 154 | |
| 155 | uart_lsr_val = *pUART_LSR; /* Clear status bit */ |
| 156 | uart_rbr_val = *pUART_RBR; /* getc() */ |
| 157 | |
| 158 | if (isr_val & IRQ_UART_ERROR_BIT) { |
| 159 | ret = -1; |
| 160 | } else { |
| 161 | ret = uart_rbr_val & 0xff; |
| 162 | } |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | void serial_puts(const char *s) |
| 168 | { |
| 169 | while (*s) { |
| 170 | serial_putc(*s++); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void local_put_char(char ch) |
| 175 | { |
| 176 | int flags = 0; |
| 177 | unsigned long isr_val; |
| 178 | |
| 179 | save_and_cli(flags); |
| 180 | |
| 181 | /* Poll for TX Interruput */ |
| 182 | while (!((isr_val = *pSIC_ISR) & IRQ_UART_TX_BIT)) ; |
| 183 | asm("csync;"); |
| 184 | |
| 185 | *pUART_THR = ch; /* putc() */ |
| 186 | |
| 187 | if (isr_val & IRQ_UART_ERROR_BIT) { |
| 188 | printf("?"); |
| 189 | } |
| 190 | |
| 191 | restore_flags(flags); |
| 192 | |
| 193 | return; |
| 194 | } |