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