Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [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> |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright 2008 |
| 6 | * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Minkyu Kang | a6f1730 | 2012-10-25 22:15:33 +0000 | [diff] [blame] | 25 | #include <linux/compiler.h> |
| 26 | #include <serial.h> |
Minkyu Kang | 47e801b | 2009-11-04 16:07:59 +0900 | [diff] [blame] | 27 | #include <asm/arch/s3c6400.h> |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 28 | |
John Rigby | 2956532 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_SERIAL1 |
| 32 | #define UART_NR S3C64XX_UART0 |
| 33 | |
| 34 | #elif defined(CONFIG_SERIAL2) |
| 35 | #define UART_NR S3C64XX_UART1 |
| 36 | |
| 37 | #elif defined(CONFIG_SERIAL3) |
| 38 | #define UART_NR S3C64XX_UART2 |
| 39 | |
| 40 | #else |
| 41 | #error "Bad: you didn't configure serial ..." |
| 42 | #endif |
| 43 | |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 44 | /* |
| 45 | * The coefficient, used to calculate the baudrate on S3C6400 UARTs is |
| 46 | * calculated as |
| 47 | * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT |
| 48 | * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1, |
| 49 | * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants: |
| 50 | */ |
| 51 | static const int udivslot[] = { |
| 52 | 0, |
| 53 | 0x0080, |
| 54 | 0x0808, |
| 55 | 0x0888, |
| 56 | 0x2222, |
| 57 | 0x4924, |
| 58 | 0x4a52, |
| 59 | 0x54aa, |
| 60 | 0x5555, |
| 61 | 0xd555, |
| 62 | 0xd5d5, |
| 63 | 0xddd5, |
| 64 | 0xdddd, |
| 65 | 0xdfdd, |
| 66 | 0xdfdf, |
| 67 | 0xffdf, |
| 68 | }; |
| 69 | |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 70 | static void s3c64xx_serial_setbrg(void) |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 71 | { |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 72 | s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR); |
| 73 | u32 pclk = get_PCLK(); |
| 74 | u32 baudrate = gd->baudrate; |
| 75 | int i; |
| 76 | |
| 77 | i = (pclk / baudrate) % 16; |
| 78 | |
| 79 | uart->UBRDIV = pclk / baudrate / 16 - 1; |
| 80 | uart->UDIVSLOT = udivslot[i]; |
| 81 | |
| 82 | for (i = 0; i < 100; i++) |
| 83 | barrier(); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Initialise the serial port with the given baudrate. The settings |
| 88 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 89 | */ |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 90 | static int s3c64xx_serial_init(void) |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 91 | { |
| 92 | s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR); |
| 93 | |
| 94 | /* reset and enable FIFOs, set triggers to the maximum */ |
| 95 | uart->UFCON = 0xff; |
| 96 | uart->UMCON = 0; |
| 97 | /* 8N1 */ |
| 98 | uart->ULCON = 3; |
| 99 | /* No interrupts, no DMA, pure polling */ |
| 100 | uart->UCON = 5; |
| 101 | |
| 102 | serial_setbrg(); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 109 | * otherwise. When the function is succesfull, the character read is |
| 110 | * written into its argument c. |
| 111 | */ |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 112 | static int s3c64xx_serial_getc(void) |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 113 | { |
| 114 | s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR); |
| 115 | |
| 116 | /* wait for character to arrive */ |
| 117 | while (!(uart->UTRSTAT & 0x1)); |
| 118 | |
| 119 | return uart->URXH & 0xff; |
| 120 | } |
| 121 | |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 122 | #ifdef CONFIG_MODEM_SUPPORT |
| 123 | static int be_quiet; |
| 124 | void disable_putc(void) |
| 125 | { |
| 126 | be_quiet = 1; |
| 127 | } |
| 128 | |
| 129 | void enable_putc(void) |
| 130 | { |
| 131 | be_quiet = 0; |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | |
| 136 | /* |
| 137 | * Output a single byte to the serial port. |
| 138 | */ |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 139 | static void s3c64xx_serial_putc(const char c) |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 140 | { |
| 141 | s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR); |
| 142 | |
| 143 | #ifdef CONFIG_MODEM_SUPPORT |
| 144 | if (be_quiet) |
| 145 | return; |
| 146 | #endif |
| 147 | |
| 148 | /* wait for room in the tx FIFO */ |
| 149 | while (!(uart->UTRSTAT & 0x2)); |
| 150 | |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 151 | uart->UTXH = c; |
| 152 | |
| 153 | /* If \n, also do \r */ |
| 154 | if (c == '\n') |
| 155 | serial_putc('\r'); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Test whether a character is in the RX buffer |
| 160 | */ |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 161 | static int s3c64xx_serial_tstc(void) |
Guennadi Liakhovetski | 2fb28dc | 2008-08-31 00:39:47 +0200 | [diff] [blame] | 162 | { |
| 163 | s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR); |
| 164 | |
| 165 | return uart->UTRSTAT & 0x1; |
| 166 | } |
| 167 | |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 168 | static struct serial_device s3c64xx_serial_drv = { |
| 169 | .name = "s3c64xx_serial", |
| 170 | .start = s3c64xx_serial_init, |
| 171 | .stop = NULL, |
| 172 | .setbrg = s3c64xx_serial_setbrg, |
| 173 | .putc = s3c64xx_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 174 | .puts = default_serial_puts, |
Marek Vasut | cc61c31 | 2012-09-13 16:53:49 +0200 | [diff] [blame] | 175 | .getc = s3c64xx_serial_getc, |
| 176 | .tstc = s3c64xx_serial_tstc, |
| 177 | }; |
| 178 | |
| 179 | void s3c64xx_serial_initialize(void) |
| 180 | { |
| 181 | serial_register(&s3c64xx_serial_drv); |
| 182 | } |
| 183 | |
| 184 | __weak struct serial_device *default_serial_console(void) |
| 185 | { |
| 186 | return &s3c64xx_serial_drv; |
| 187 | } |