Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 SAMSUNG Electronics |
| 3 | * Minkyu Kang <mk7.kang@samsung.com> |
| 4 | * Heungjun Kim <riverful.kim@samsung.com> |
| 5 | * |
| 6 | * based on drivers/serial/s3c64xx.c |
| 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> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/arch/uart.h> |
| 27 | #include <asm/arch/clk.h> |
| 28 | #include <serial.h> |
| 29 | |
John Rigby | 2956532 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 32 | static inline struct s5p_uart *s5p_get_base_uart(int dev_index) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 33 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 34 | u32 offset = dev_index * sizeof(struct s5p_uart); |
Minkyu Kang | d93d0f0 | 2010-08-13 16:07:35 +0900 | [diff] [blame] | 35 | return (struct s5p_uart *)(samsung_get_base_uart() + offset); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 39 | * The coefficient, used to calculate the baudrate on S5P UARTs is |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 40 | * calculated as |
| 41 | * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT |
| 42 | * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1, |
| 43 | * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants: |
| 44 | */ |
| 45 | static const int udivslot[] = { |
| 46 | 0, |
| 47 | 0x0080, |
| 48 | 0x0808, |
| 49 | 0x0888, |
| 50 | 0x2222, |
| 51 | 0x4924, |
| 52 | 0x4a52, |
| 53 | 0x54aa, |
| 54 | 0x5555, |
| 55 | 0xd555, |
| 56 | 0xd5d5, |
| 57 | 0xddd5, |
| 58 | 0xdddd, |
| 59 | 0xdfdd, |
| 60 | 0xdfdf, |
| 61 | 0xffdf, |
| 62 | }; |
| 63 | |
| 64 | void serial_setbrg_dev(const int dev_index) |
| 65 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 66 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | f70409a | 2010-08-24 15:51:55 +0900 | [diff] [blame] | 67 | u32 uclk = get_uart_clk(dev_index); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 68 | u32 baudrate = gd->baudrate; |
| 69 | u32 val; |
| 70 | |
Minkyu Kang | f70409a | 2010-08-24 15:51:55 +0900 | [diff] [blame] | 71 | val = uclk / baudrate; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 72 | |
| 73 | writel(val / 16 - 1, &uart->ubrdiv); |
Minkyu Kang | 1628cfc | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 74 | |
Minkyu Kang | e0617c6 | 2011-01-24 14:43:25 +0900 | [diff] [blame] | 75 | if (s5p_uart_divslot()) |
Minkyu Kang | 1628cfc | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 76 | writew(udivslot[val % 16], &uart->rest.slot); |
| 77 | else |
| 78 | writeb(val % 16, &uart->rest.value); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Initialise the serial port with the given baudrate. The settings |
| 83 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 84 | */ |
| 85 | int serial_init_dev(const int dev_index) |
| 86 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 87 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 88 | |
| 89 | /* reset and enable FIFOs, set triggers to the maximum */ |
| 90 | writel(0, &uart->ufcon); |
| 91 | writel(0, &uart->umcon); |
| 92 | /* 8N1 */ |
| 93 | writel(0x3, &uart->ulcon); |
| 94 | /* No interrupts, no DMA, pure polling */ |
| 95 | writel(0x245, &uart->ucon); |
| 96 | |
| 97 | serial_setbrg_dev(dev_index); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 102 | static int serial_err_check(const int dev_index, int op) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 103 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 104 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 105 | unsigned int mask; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 106 | |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 107 | /* |
| 108 | * UERSTAT |
| 109 | * Break Detect [3] |
| 110 | * Frame Err [2] : receive operation |
| 111 | * Parity Err [1] : receive operation |
| 112 | * Overrun Err [0] : receive operation |
| 113 | */ |
| 114 | if (op) |
| 115 | mask = 0x8; |
| 116 | else |
| 117 | mask = 0xf; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 118 | |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 119 | return readl(&uart->uerstat) & mask; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 124 | * otherwise. When the function is succesfull, the character read is |
| 125 | * written into its argument c. |
| 126 | */ |
| 127 | int serial_getc_dev(const int dev_index) |
| 128 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 129 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 130 | |
| 131 | /* wait for character to arrive */ |
| 132 | while (!(readl(&uart->utrstat) & 0x1)) { |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 133 | if (serial_err_check(dev_index, 0)) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
Minkyu Kang | 1a4106d | 2010-07-06 20:08:29 +0900 | [diff] [blame] | 137 | return (int)(readb(&uart->urxh) & 0xff); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Output a single byte to the serial port. |
| 142 | */ |
| 143 | void serial_putc_dev(const char c, const int dev_index) |
| 144 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 145 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 146 | |
| 147 | /* wait for room in the tx FIFO */ |
| 148 | while (!(readl(&uart->utrstat) & 0x2)) { |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 149 | if (serial_err_check(dev_index, 1)) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
Minkyu Kang | 1a4106d | 2010-07-06 20:08:29 +0900 | [diff] [blame] | 153 | writeb(c, &uart->utxh); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 154 | |
| 155 | /* If \n, also do \r */ |
| 156 | if (c == '\n') |
| 157 | serial_putc('\r'); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Test whether a character is in the RX buffer |
| 162 | */ |
| 163 | int serial_tstc_dev(const int dev_index) |
| 164 | { |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 165 | struct s5p_uart *const uart = s5p_get_base_uart(dev_index); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 166 | |
| 167 | return (int)(readl(&uart->utrstat) & 0x1); |
| 168 | } |
| 169 | |
| 170 | void serial_puts_dev(const char *s, const int dev_index) |
| 171 | { |
| 172 | while (*s) |
| 173 | serial_putc_dev(*s++, dev_index); |
| 174 | } |
| 175 | |
| 176 | /* Multi serial device functions */ |
| 177 | #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \ |
| 178 | int s5p_serial##port##_init(void) { return serial_init_dev(port); } \ |
| 179 | void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \ |
| 180 | int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \ |
| 181 | int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \ |
| 182 | void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \ |
| 183 | void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); } |
| 184 | |
| 185 | #define INIT_S5P_SERIAL_STRUCTURE(port, name, bus) { \ |
| 186 | name, \ |
| 187 | bus, \ |
| 188 | s5p_serial##port##_init, \ |
Anatolij Gustschin | fbb0030 | 2010-04-24 19:27:04 +0200 | [diff] [blame] | 189 | NULL, \ |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 190 | s5p_serial##port##_setbrg, \ |
| 191 | s5p_serial##port##_getc, \ |
| 192 | s5p_serial##port##_tstc, \ |
| 193 | s5p_serial##port##_putc, \ |
| 194 | s5p_serial##port##_puts, } |
| 195 | |
| 196 | DECLARE_S5P_SERIAL_FUNCTIONS(0); |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 197 | struct serial_device s5p_serial0_device = |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 198 | INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0", "S5PUART0"); |
| 199 | DECLARE_S5P_SERIAL_FUNCTIONS(1); |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 200 | struct serial_device s5p_serial1_device = |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 201 | INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1", "S5PUART1"); |
| 202 | DECLARE_S5P_SERIAL_FUNCTIONS(2); |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 203 | struct serial_device s5p_serial2_device = |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 204 | INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2", "S5PUART2"); |
| 205 | DECLARE_S5P_SERIAL_FUNCTIONS(3); |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 206 | struct serial_device s5p_serial3_device = |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 207 | INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3", "S5PUART3"); |