wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 3 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | 7ac1610 | 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> |
Mike Frysinger | f96c042 | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 22 | #include <linux/compiler.h> |
kevin.morfitt@fearnside-systems.co.uk | e0d8131 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 23 | #include <asm/arch/s3c24x0_cpu.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 27 | #ifdef CONFIG_SERIAL1 |
| 28 | #define UART_NR S3C24X0_UART0 |
| 29 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 30 | #elif defined(CONFIG_SERIAL2) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 31 | #define UART_NR S3C24X0_UART1 |
| 32 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 33 | #elif defined(CONFIG_SERIAL3) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 34 | #define UART_NR S3C24X0_UART2 |
| 35 | |
| 36 | #else |
| 37 | #error "Bad: you didn't configure serial ..." |
| 38 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 40 | #include <asm/io.h> |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 41 | #include <serial.h> |
| 42 | |
| 43 | /* Multi serial device functions */ |
| 44 | #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 45 | int s3serial##port##_init(void) \ |
| 46 | { \ |
| 47 | return serial_init_dev(port); \ |
| 48 | } \ |
| 49 | void s3serial##port##_setbrg(void) \ |
| 50 | { \ |
| 51 | serial_setbrg_dev(port); \ |
| 52 | } \ |
| 53 | int s3serial##port##_getc(void) \ |
| 54 | { \ |
| 55 | return serial_getc_dev(port); \ |
| 56 | } \ |
| 57 | int s3serial##port##_tstc(void) \ |
| 58 | { \ |
| 59 | return serial_tstc_dev(port); \ |
| 60 | } \ |
| 61 | void s3serial##port##_putc(const char c) \ |
| 62 | { \ |
| 63 | serial_putc_dev(port, c); \ |
| 64 | } \ |
| 65 | void s3serial##port##_puts(const char *s) \ |
| 66 | { \ |
| 67 | serial_puts_dev(port, s); \ |
| 68 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 69 | |
Marek Vasut | 5bcdf24 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 70 | #define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \ |
| 71 | .name = __name, \ |
| 72 | .start = s3serial##port##_init, \ |
| 73 | .stop = NULL, \ |
| 74 | .setbrg = s3serial##port##_setbrg, \ |
| 75 | .getc = s3serial##port##_getc, \ |
| 76 | .tstc = s3serial##port##_tstc, \ |
| 77 | .putc = s3serial##port##_putc, \ |
| 78 | .puts = s3serial##port##_puts, \ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 79 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 80 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 81 | #ifdef CONFIG_HWFLOW |
| 82 | static int hwflow; |
| 83 | #endif |
| 84 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 85 | void _serial_setbrg(const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 87 | struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | unsigned int reg = 0; |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 89 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | |
| 91 | /* value is calculated so : (int)(PCLK/16./baudrate) -1 */ |
| 92 | reg = get_PCLK() / (16 * gd->baudrate) - 1; |
| 93 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 94 | writel(reg, &uart->ubrdiv); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 95 | for (i = 0; i < 100; i++) |
| 96 | /* Delay */ ; |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 97 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 98 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 99 | static inline void serial_setbrg_dev(unsigned int dev_index) |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 100 | { |
| 101 | _serial_setbrg(dev_index); |
| 102 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 103 | |
| 104 | /* Initialise the serial port. The settings are always 8 data bits, no parity, |
| 105 | * 1 stop bit, no start bits. |
| 106 | */ |
| 107 | static int serial_init_dev(const int dev_index) |
| 108 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 109 | struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index); |
| 110 | |
| 111 | #ifdef CONFIG_HWFLOW |
| 112 | hwflow = 0; /* turned off by default */ |
| 113 | #endif |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 114 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 115 | /* FIFO enable, Tx/Rx FIFO clear */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 116 | writel(0x07, &uart->ufcon); |
| 117 | writel(0x0, &uart->umcon); |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 118 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | /* Normal,No parity,1 stop,8 bit */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 120 | writel(0x3, &uart->ulcon); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | /* |
| 122 | * tx=level,rx=edge,disable timeout int.,enable rx error int., |
| 123 | * normal,interrupt or polling |
| 124 | */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 125 | writel(0x245, &uart->ucon); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | |
| 127 | #ifdef CONFIG_HWFLOW |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 128 | writel(0x1, &uart->umcon); /* rts up */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | #endif |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 130 | |
| 131 | /* FIXME: This is sooooooooooooooooooo ugly */ |
| 132 | #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2) |
| 133 | /* we need auto hw flow control on the gsm and gps port */ |
| 134 | if (dev_index == 0 || dev_index == 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 135 | writel(0x10, &uart->umcon); |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 136 | #endif |
| 137 | _serial_setbrg(dev_index); |
| 138 | |
| 139 | return (0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 144 | * otherwise. When the function is succesfull, the character read is |
| 145 | * written into its argument c. |
| 146 | */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 147 | int _serial_getc(const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 149 | struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 150 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 151 | while (!(readl(&uart->utrstat) & 0x1)) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 152 | /* wait for character to arrive */ ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 153 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 154 | return readb(&uart->urxh) & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 156 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 157 | static inline int serial_getc_dev(unsigned int dev_index) |
| 158 | { |
| 159 | return _serial_getc(dev_index); |
| 160 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | |
| 162 | #ifdef CONFIG_HWFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 163 | int hwflow_onoff(int on) |
| 164 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 165 | switch (on) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | case 0: |
| 167 | default: |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 168 | break; /* return current */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | case 1: |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 170 | hwflow = 1; /* turn on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | break; |
| 172 | case -1: |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 173 | hwflow = 0; /* turn off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | return hwflow; |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #ifdef CONFIG_MODEM_SUPPORT |
| 181 | static int be_quiet = 0; |
| 182 | void disable_putc(void) |
| 183 | { |
| 184 | be_quiet = 1; |
| 185 | } |
| 186 | |
| 187 | void enable_putc(void) |
| 188 | { |
| 189 | be_quiet = 0; |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | |
| 194 | /* |
| 195 | * Output a single byte to the serial port. |
| 196 | */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 197 | void _serial_putc(const char c, const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 199 | struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | #ifdef CONFIG_MODEM_SUPPORT |
| 201 | if (be_quiet) |
| 202 | return; |
| 203 | #endif |
| 204 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 205 | while (!(readl(&uart->utrstat) & 0x2)) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 206 | /* wait for room in the tx FIFO */ ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 207 | |
| 208 | #ifdef CONFIG_HWFLOW |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 209 | while (hwflow && !(readl(&uart->umstat) & 0x1)) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 210 | /* Wait for CTS up */ ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 213 | writeb(c, &uart->utxh); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | |
| 215 | /* If \n, also do \r */ |
| 216 | if (c == '\n') |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 217 | serial_putc('\r'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 218 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 219 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 220 | static inline void serial_putc_dev(unsigned int dev_index, const char c) |
| 221 | { |
| 222 | _serial_putc(c, dev_index); |
| 223 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Test whether a character is in the RX buffer |
| 227 | */ |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 228 | int _serial_tstc(const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 230 | struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 231 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 232 | return readl(&uart->utrstat) & 0x1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 234 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 235 | static inline int serial_tstc_dev(unsigned int dev_index) |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 236 | { |
| 237 | return _serial_tstc(dev_index); |
| 238 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 239 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 240 | void _serial_puts(const char *s, const int dev_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | { |
| 242 | while (*s) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 243 | _serial_putc(*s++, dev_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 246 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 247 | static inline void serial_puts_dev(int dev_index, const char *s) |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 248 | { |
| 249 | _serial_puts(s, dev_index); |
| 250 | } |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 251 | |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 252 | DECLARE_S3C_SERIAL_FUNCTIONS(0); |
| 253 | struct serial_device s3c24xx_serial0_device = |
Mike Frysinger | 1ef72f6 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 254 | INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0"); |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 255 | DECLARE_S3C_SERIAL_FUNCTIONS(1); |
| 256 | struct serial_device s3c24xx_serial1_device = |
Mike Frysinger | 1ef72f6 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 257 | INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1"); |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 258 | DECLARE_S3C_SERIAL_FUNCTIONS(2); |
| 259 | struct serial_device s3c24xx_serial2_device = |
Mike Frysinger | 1ef72f6 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 260 | INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2"); |
Mike Frysinger | f96c042 | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 261 | |
| 262 | __weak struct serial_device *default_serial_console(void) |
| 263 | { |
| 264 | #if defined(CONFIG_SERIAL1) |
| 265 | return &s3c24xx_serial0_device; |
| 266 | #elif defined(CONFIG_SERIAL2) |
| 267 | return &s3c24xx_serial1_device; |
| 268 | #elif defined(CONFIG_SERIAL3) |
| 269 | return &s3c24xx_serial2_device; |
| 270 | #else |
| 271 | #error "CONFIG_SERIAL? missing." |
| 272 | #endif |
| 273 | } |
Marek Vasut | b3e48aa | 2012-09-12 16:01:16 +0200 | [diff] [blame] | 274 | |
| 275 | void s3c24xx_serial_initialize(void) |
| 276 | { |
| 277 | serial_register(&s3c24xx_serial0_device); |
| 278 | serial_register(&s3c24xx_serial1_device); |
| 279 | serial_register(&s3c24xx_serial2_device); |
| 280 | } |