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