wdenk | 281e00a | 2004-08-01 22:48:16 +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 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 22 | #if defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) |
| 23 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB) |
| 25 | #include <s3c2400.h> |
| 26 | #elif defined(CONFIG_S3C2410) |
| 27 | #include <s3c2410.h> |
| 28 | #endif |
| 29 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 32 | #ifdef CONFIG_SERIAL1 |
| 33 | #define UART_NR S3C24X0_UART0 |
| 34 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 35 | #elif defined(CONFIG_SERIAL2) |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 36 | # if defined(CONFIG_TRAB) |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 37 | # error "TRAB supports only CONFIG_SERIAL1" |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 38 | # endif |
| 39 | #define UART_NR S3C24X0_UART1 |
| 40 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 41 | #elif defined(CONFIG_SERIAL3) |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 42 | # if defined(CONFIG_TRAB) |
| 43 | # #error "TRAB supports only CONFIG_SERIAL1" |
| 44 | # endif |
| 45 | #define UART_NR S3C24X0_UART2 |
| 46 | |
| 47 | #else |
| 48 | #error "Bad: you didn't configure serial ..." |
| 49 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
| 51 | void serial_setbrg (void) |
| 52 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 53 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | int i; |
| 55 | unsigned int reg = 0; |
| 56 | |
| 57 | /* value is calculated so : (int)(PCLK/16./baudrate) -1 */ |
| 58 | reg = get_PCLK() / (16 * gd->baudrate) - 1; |
| 59 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | /* FIFO enable, Tx/Rx FIFO clear */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 61 | uart->UFCON = 0x07; |
| 62 | uart->UMCON = 0x0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | /* Normal,No parity,1 stop,8 bit */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 64 | uart->ULCON = 0x3; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | /* |
| 66 | * tx=level,rx=edge,disable timeout int.,enable rx error int., |
| 67 | * normal,interrupt or polling |
| 68 | */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 69 | uart->UCON = 0x245; |
| 70 | uart->UBRDIV = reg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | |
| 72 | #ifdef CONFIG_HWFLOW |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 73 | uart->UMCON = 0x1; /* RTS up */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | #endif |
| 75 | for (i = 0; i < 100; i++); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Initialise the serial port with the given baudrate. The settings |
| 80 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 81 | * |
| 82 | */ |
| 83 | int serial_init (void) |
| 84 | { |
| 85 | serial_setbrg (); |
| 86 | |
| 87 | return (0); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 92 | * otherwise. When the function is succesfull, the character read is |
| 93 | * written into its argument c. |
| 94 | */ |
| 95 | int serial_getc (void) |
| 96 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 97 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 98 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 99 | /* wait for character to arrive */ |
| 100 | while (!(uart->UTRSTAT & 0x1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 102 | return uart->URXH & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #ifdef CONFIG_HWFLOW |
| 106 | static int hwflow = 0; /* turned off by default */ |
| 107 | int hwflow_onoff(int on) |
| 108 | { |
| 109 | switch(on) { |
| 110 | case 0: |
| 111 | default: |
| 112 | break; /* return current */ |
| 113 | case 1: |
| 114 | hwflow = 1; /* turn on */ |
| 115 | break; |
| 116 | case -1: |
| 117 | hwflow = 0; /* turn off */ |
| 118 | break; |
| 119 | } |
| 120 | return hwflow; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #ifdef CONFIG_MODEM_SUPPORT |
| 125 | static int be_quiet = 0; |
| 126 | void disable_putc(void) |
| 127 | { |
| 128 | be_quiet = 1; |
| 129 | } |
| 130 | |
| 131 | void enable_putc(void) |
| 132 | { |
| 133 | be_quiet = 0; |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | |
| 138 | /* |
| 139 | * Output a single byte to the serial port. |
| 140 | */ |
| 141 | void serial_putc (const char c) |
| 142 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 143 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 144 | #ifdef CONFIG_MODEM_SUPPORT |
| 145 | if (be_quiet) |
| 146 | return; |
| 147 | #endif |
| 148 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 149 | /* wait for room in the tx FIFO */ |
| 150 | while (!(uart->UTRSTAT & 0x2)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef CONFIG_HWFLOW |
| 153 | /* Wait for CTS up */ |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 154 | while(hwflow && !(uart->UMSTAT & 0x1)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | ; |
| 156 | #endif |
| 157 | |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 158 | uart->UTXH = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 159 | |
| 160 | /* If \n, also do \r */ |
| 161 | if (c == '\n') |
| 162 | serial_putc ('\r'); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Test whether a character is in the RX buffer |
| 167 | */ |
| 168 | int serial_tstc (void) |
| 169 | { |
wdenk | 48b4261 | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 170 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); |
| 171 | |
| 172 | return uart->UTRSTAT & 0x1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
| 176 | serial_puts (const char *s) |
| 177 | { |
| 178 | while (*s) { |
| 179 | serial_putc (*s++); |
| 180 | } |
| 181 | } |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 182 | |
| 183 | #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */ |