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