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 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 32 | #ifdef CONFIG_SERIAL1 |
| 33 | #define UART_NR S3C24X0_UART0 |
| 34 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 35 | #elif defined(CONFIG_SERIAL2) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 36 | # if defined(CONFIG_TRAB) |
wdenk | f6a6ac1 | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 37 | # error "TRAB supports only CONFIG_SERIAL1" |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 38 | # endif |
| 39 | #define UART_NR S3C24X0_UART1 |
| 40 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 41 | #elif defined(CONFIG_SERIAL3) |
wdenk | 7539dea | 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 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 51 | #if defined(CONFIG_SERIAL_MULTI) |
| 52 | #include <serial.h> |
| 53 | |
| 54 | /* Multi serial device functions */ |
| 55 | #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \ |
| 56 | int s3serial##port##_init (void) {\ |
| 57 | return serial_init_dev(port);}\ |
| 58 | void s3serial##port##_setbrg (void) {\ |
| 59 | serial_setbrg_dev(port);}\ |
| 60 | int s3serial##port##_getc (void) {\ |
| 61 | return serial_getc_dev(port);}\ |
| 62 | int s3serial##port##_tstc (void) {\ |
| 63 | return serial_tstc_dev(port);}\ |
| 64 | void s3serial##port##_putc (const char c) {\ |
| 65 | serial_putc_dev(port, c);}\ |
| 66 | void s3serial##port##_puts (const char *s) {\ |
| 67 | serial_puts_dev(port, s);} |
| 68 | |
| 69 | #define INIT_S3C_SERIAL_STRUCTURE(port,name,bus) {\ |
| 70 | name,\ |
| 71 | bus,\ |
| 72 | s3serial##port##_init,\ |
| 73 | s3serial##port##_setbrg,\ |
| 74 | s3serial##port##_getc,\ |
| 75 | s3serial##port##_tstc,\ |
| 76 | s3serial##port##_putc,\ |
| 77 | s3serial##port##_puts, } |
| 78 | |
| 79 | #endif /* CONFIG_SERIAL_MULTI */ |
| 80 | |
| 81 | void _serial_setbrg(const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 82 | { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 83 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | unsigned int reg = 0; |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 85 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | |
| 87 | /* value is calculated so : (int)(PCLK/16./baudrate) -1 */ |
| 88 | reg = get_PCLK() / (16 * gd->baudrate) - 1; |
| 89 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 90 | uart->UBRDIV = reg; |
| 91 | for (i = 0; i < 100; i++); |
| 92 | } |
| 93 | #if defined(CONFIG_SERIAL_MULTI) |
| 94 | static inline void |
| 95 | serial_setbrg_dev(unsigned int dev_index) |
| 96 | { |
| 97 | _serial_setbrg(dev_index); |
| 98 | } |
| 99 | #else |
| 100 | void serial_setbrg(void) |
| 101 | { |
| 102 | _serial_setbrg(UART_NR); |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | |
| 107 | /* Initialise the serial port. The settings are always 8 data bits, no parity, |
| 108 | * 1 stop bit, no start bits. |
| 109 | */ |
| 110 | static int serial_init_dev(const int dev_index) |
| 111 | { |
| 112 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index); |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 113 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | /* FIFO enable, Tx/Rx FIFO clear */ |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 115 | uart->UFCON = 0x07; |
| 116 | uart->UMCON = 0x0; |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 117 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | /* Normal,No parity,1 stop,8 bit */ |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 119 | uart->ULCON = 0x3; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | /* |
| 121 | * tx=level,rx=edge,disable timeout int.,enable rx error int., |
| 122 | * normal,interrupt or polling |
| 123 | */ |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 124 | uart->UCON = 0x245; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | |
| 126 | #ifdef CONFIG_HWFLOW |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 127 | uart->UMCON = 0x1; /* RTS up */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | #endif |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 129 | |
| 130 | /* FIXME: This is sooooooooooooooooooo ugly */ |
| 131 | #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2) |
| 132 | /* we need auto hw flow control on the gsm and gps port */ |
| 133 | if (dev_index == 0 || dev_index == 1) |
| 134 | uart->UMCON = 0x10; |
| 135 | #endif |
| 136 | _serial_setbrg(dev_index); |
| 137 | |
| 138 | return (0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 141 | #if !defined(CONFIG_SERIAL_MULTI) |
| 142 | /* Initialise the serial port. The settings are always 8 data bits, no parity, |
| 143 | * 1 stop bit, no start bits. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 144 | */ |
| 145 | int serial_init (void) |
| 146 | { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 147 | return serial_init_dev(UART_NR); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 149 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 153 | * otherwise. When the function is succesfull, the character read is |
| 154 | * written into its argument c. |
| 155 | */ |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 156 | int _serial_getc (const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 157 | { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 158 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 160 | /* wait for character to arrive */ |
| 161 | while (!(uart->UTRSTAT & 0x1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 162 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 163 | return uart->URXH & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 165 | #if defined(CONFIG_SERIAL_MULTI) |
| 166 | static inline int serial_getc_dev(unsigned int dev_index) |
| 167 | { |
| 168 | return _serial_getc(dev_index); |
| 169 | } |
| 170 | #else |
| 171 | int serial_getc (void) |
| 172 | { |
| 173 | return _serial_getc(UART_NR); |
| 174 | } |
| 175 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | |
| 177 | #ifdef CONFIG_HWFLOW |
| 178 | static int hwflow = 0; /* turned off by default */ |
| 179 | int hwflow_onoff(int on) |
| 180 | { |
| 181 | switch(on) { |
| 182 | case 0: |
| 183 | default: |
| 184 | break; /* return current */ |
| 185 | case 1: |
| 186 | hwflow = 1; /* turn on */ |
| 187 | break; |
| 188 | case -1: |
| 189 | hwflow = 0; /* turn off */ |
| 190 | break; |
| 191 | } |
| 192 | return hwflow; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #ifdef CONFIG_MODEM_SUPPORT |
| 197 | static int be_quiet = 0; |
| 198 | void disable_putc(void) |
| 199 | { |
| 200 | be_quiet = 1; |
| 201 | } |
| 202 | |
| 203 | void enable_putc(void) |
| 204 | { |
| 205 | be_quiet = 0; |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * Output a single byte to the serial port. |
| 212 | */ |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 213 | void _serial_putc (const char c, const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 215 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | #ifdef CONFIG_MODEM_SUPPORT |
| 217 | if (be_quiet) |
| 218 | return; |
| 219 | #endif |
| 220 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 221 | /* wait for room in the tx FIFO */ |
| 222 | while (!(uart->UTRSTAT & 0x2)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 223 | |
| 224 | #ifdef CONFIG_HWFLOW |
| 225 | /* Wait for CTS up */ |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 226 | while(hwflow && !(uart->UMSTAT & 0x1)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 227 | ; |
| 228 | #endif |
| 229 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 230 | uart->UTXH = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | |
| 232 | /* If \n, also do \r */ |
| 233 | if (c == '\n') |
| 234 | serial_putc ('\r'); |
| 235 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 236 | #if defined(CONFIG_SERIAL_MULTI) |
| 237 | static inline void serial_putc_dev(unsigned int dev_index, const char c) |
| 238 | { |
| 239 | _serial_putc(c, dev_index); |
| 240 | } |
| 241 | #else |
| 242 | void serial_putc(const char c) |
| 243 | { |
| 244 | _serial_putc(c, UART_NR); |
| 245 | } |
| 246 | #endif |
| 247 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * Test whether a character is in the RX buffer |
| 251 | */ |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 252 | int _serial_tstc(const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 254 | S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 255 | |
| 256 | return uart->UTRSTAT & 0x1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 258 | #if defined(CONFIG_SERIAL_MULTI) |
| 259 | static inline int |
| 260 | serial_tstc_dev(unsigned int dev_index) |
| 261 | { |
| 262 | return _serial_tstc(dev_index); |
| 263 | } |
| 264 | #else |
| 265 | int serial_tstc(void) |
| 266 | { |
| 267 | return _serial_tstc(UART_NR); |
| 268 | } |
| 269 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 270 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 271 | void _serial_puts(const char *s, const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | { |
| 273 | while (*s) { |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 274 | _serial_putc (*s++, dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 277 | #if defined(CONFIG_SERIAL_MULTI) |
| 278 | static inline void |
| 279 | serial_puts_dev(int dev_index, const char *s) |
| 280 | { |
| 281 | _serial_puts(s, dev_index); |
| 282 | } |
| 283 | #else |
| 284 | void |
| 285 | serial_puts (const char *s) |
| 286 | { |
| 287 | _serial_puts(s, UART_NR); |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | #if defined(CONFIG_SERIAL_MULTI) |
| 292 | DECLARE_S3C_SERIAL_FUNCTIONS(0); |
| 293 | struct serial_device s3c24xx_serial0_device = |
| 294 | INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1"); |
| 295 | DECLARE_S3C_SERIAL_FUNCTIONS(1); |
| 296 | struct serial_device s3c24xx_serial1_device = |
| 297 | INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2"); |
| 298 | DECLARE_S3C_SERIAL_FUNCTIONS(2); |
| 299 | struct serial_device s3c24xx_serial2_device = |
| 300 | INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3"); |
| 301 | |
| 302 | #endif /* CONFIG_SERIAL_MULTI */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 303 | |
| 304 | #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */ |