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