wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB) |
| 23 | #include <s3c2400.h> |
| 24 | #elif defined(CONFIG_S3C2410) |
| 25 | #include <s3c2410.h> |
| 26 | #endif |
| 27 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 28 | #ifdef CONFIG_SERIAL1 |
| 29 | #define UART_NR S3C24X0_UART0 |
| 30 | |
| 31 | #elif CONFIG_SERIAL2 |
| 32 | # if defined(CONFIG_TRAB) |
| 33 | # #error "TRAB supports only CONFIG_SERIAL1" |
| 34 | # endif |
| 35 | #define UART_NR S3C24X0_UART1 |
| 36 | |
| 37 | #elif CONFIG_SERIAL3 |
| 38 | # if defined(CONFIG_TRAB) |
| 39 | # #error "TRAB supports only CONFIG_SERIAL1" |
| 40 | # endif |
| 41 | #define UART_NR S3C24X0_UART2 |
| 42 | |
| 43 | #else |
| 44 | #error "Bad: you didn't configure serial ..." |
| 45 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | |
| 47 | void serial_setbrg (void) |
| 48 | { |
| 49 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 50 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | int i; |
| 52 | unsigned int reg = 0; |
| 53 | |
| 54 | /* value is calculated so : (int)(PCLK/16./baudrate) -1 */ |
| 55 | reg = get_PCLK() / (16 * gd->baudrate) - 1; |
| 56 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | /* FIFO enable, Tx/Rx FIFO clear */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 58 | uart->UFCON = 0x07; |
| 59 | uart->UMCON = 0x0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | /* Normal,No parity,1 stop,8 bit */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 61 | uart->ULCON = 0x3; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | /* |
| 63 | * tx=level,rx=edge,disable timeout int.,enable rx error int., |
| 64 | * normal,interrupt or polling |
| 65 | */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 66 | uart->UCON = 0x245; |
| 67 | uart->UBRDIV = reg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | |
| 69 | #ifdef CONFIG_HWFLOW |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 70 | uart->UMCON = 0x1; /* RTS up */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | #endif |
| 72 | for (i = 0; i < 100; i++); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Initialise the serial port with the given baudrate. The settings |
| 77 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 78 | * |
| 79 | */ |
| 80 | int serial_init (void) |
| 81 | { |
| 82 | serial_setbrg (); |
| 83 | |
| 84 | return (0); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 89 | * otherwise. When the function is succesfull, the character read is |
| 90 | * written into its argument c. |
| 91 | */ |
| 92 | int serial_getc (void) |
| 93 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 94 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame^] | 95 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 96 | /* wait for character to arrive */ |
| 97 | while (!(uart->UTRSTAT & 0x1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 99 | return uart->URXH & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | #ifdef CONFIG_HWFLOW |
| 103 | static int hwflow = 0; /* turned off by default */ |
| 104 | int hwflow_onoff(int on) |
| 105 | { |
| 106 | switch(on) { |
| 107 | case 0: |
| 108 | default: |
| 109 | break; /* return current */ |
| 110 | case 1: |
| 111 | hwflow = 1; /* turn on */ |
| 112 | break; |
| 113 | case -1: |
| 114 | hwflow = 0; /* turn off */ |
| 115 | break; |
| 116 | } |
| 117 | return hwflow; |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | #ifdef CONFIG_MODEM_SUPPORT |
| 122 | static int be_quiet = 0; |
| 123 | void disable_putc(void) |
| 124 | { |
| 125 | be_quiet = 1; |
| 126 | } |
| 127 | |
| 128 | void enable_putc(void) |
| 129 | { |
| 130 | be_quiet = 0; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * Output a single byte to the serial port. |
| 137 | */ |
| 138 | void serial_putc (const char c) |
| 139 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 140 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | #ifdef CONFIG_MODEM_SUPPORT |
| 142 | if (be_quiet) |
| 143 | return; |
| 144 | #endif |
| 145 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 146 | /* wait for room in the tx FIFO */ |
| 147 | while (!(uart->UTRSTAT & 0x2)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | |
| 149 | #ifdef CONFIG_HWFLOW |
| 150 | /* Wait for CTS up */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 151 | while(hwflow && !(uart->UMSTAT & 0x1)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | ; |
| 153 | #endif |
| 154 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 155 | uart->UTXH = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 156 | |
| 157 | /* If \n, also do \r */ |
| 158 | if (c == '\n') |
| 159 | serial_putc ('\r'); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Test whether a character is in the RX buffer |
| 164 | */ |
| 165 | int serial_tstc (void) |
| 166 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 167 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
| 168 | |
| 169 | return uart->UTRSTAT & 0x1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
| 173 | serial_puts (const char *s) |
| 174 | { |
| 175 | while (*s) { |
| 176 | serial_putc (*s++); |
| 177 | } |
| 178 | } |