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 | |
| 30 | static inline struct s5pc1xx_uart *s5pc1xx_get_base_uart(int dev_index) |
| 31 | { |
| 32 | u32 offset = dev_index * sizeof(struct s5pc1xx_uart); |
| 33 | |
| 34 | if (cpu_is_s5pc100()) |
| 35 | return (struct s5pc1xx_uart *)(S5PC100_UART_BASE + offset); |
| 36 | else |
| 37 | return (struct s5pc1xx_uart *)(S5PC110_UART_BASE + offset); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * The coefficient, used to calculate the baudrate on S5PC1XX UARTs is |
| 42 | * calculated as |
| 43 | * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT |
| 44 | * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1, |
| 45 | * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants: |
| 46 | */ |
| 47 | static const int udivslot[] = { |
| 48 | 0, |
| 49 | 0x0080, |
| 50 | 0x0808, |
| 51 | 0x0888, |
| 52 | 0x2222, |
| 53 | 0x4924, |
| 54 | 0x4a52, |
| 55 | 0x54aa, |
| 56 | 0x5555, |
| 57 | 0xd555, |
| 58 | 0xd5d5, |
| 59 | 0xddd5, |
| 60 | 0xdddd, |
| 61 | 0xdfdd, |
| 62 | 0xdfdf, |
| 63 | 0xffdf, |
| 64 | }; |
| 65 | |
| 66 | void serial_setbrg_dev(const int dev_index) |
| 67 | { |
| 68 | DECLARE_GLOBAL_DATA_PTR; |
| 69 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 70 | u32 pclk = get_pclk(); |
| 71 | u32 baudrate = gd->baudrate; |
| 72 | u32 val; |
| 73 | |
| 74 | val = pclk / baudrate; |
| 75 | |
| 76 | writel(val / 16 - 1, &uart->ubrdiv); |
| 77 | writel(udivslot[val % 16], &uart->udivslot); |
| 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 | { |
| 86 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 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 | |
| 101 | static int serial_err_check(const int dev_index) |
| 102 | { |
| 103 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 104 | |
| 105 | if (readl(&uart->uerstat) & 0xf) |
| 106 | return 1; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 113 | * otherwise. When the function is succesfull, the character read is |
| 114 | * written into its argument c. |
| 115 | */ |
| 116 | int serial_getc_dev(const int dev_index) |
| 117 | { |
| 118 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 119 | |
| 120 | /* wait for character to arrive */ |
| 121 | while (!(readl(&uart->utrstat) & 0x1)) { |
| 122 | if (serial_err_check(dev_index)) |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | return (int)(readl(&uart->urxh) & 0xff); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Output a single byte to the serial port. |
| 131 | */ |
| 132 | void serial_putc_dev(const char c, const int dev_index) |
| 133 | { |
| 134 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 135 | |
| 136 | /* wait for room in the tx FIFO */ |
| 137 | while (!(readl(&uart->utrstat) & 0x2)) { |
| 138 | if (serial_err_check(dev_index)) |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | writel(c, &uart->utxh); |
| 143 | |
| 144 | /* If \n, also do \r */ |
| 145 | if (c == '\n') |
| 146 | serial_putc('\r'); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Test whether a character is in the RX buffer |
| 151 | */ |
| 152 | int serial_tstc_dev(const int dev_index) |
| 153 | { |
| 154 | struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index); |
| 155 | |
| 156 | return (int)(readl(&uart->utrstat) & 0x1); |
| 157 | } |
| 158 | |
| 159 | void serial_puts_dev(const char *s, const int dev_index) |
| 160 | { |
| 161 | while (*s) |
| 162 | serial_putc_dev(*s++, dev_index); |
| 163 | } |
| 164 | |
| 165 | /* Multi serial device functions */ |
| 166 | #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \ |
| 167 | int s5p_serial##port##_init(void) { return serial_init_dev(port); } \ |
| 168 | void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \ |
| 169 | int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \ |
| 170 | int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \ |
| 171 | void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \ |
| 172 | void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); } |
| 173 | |
| 174 | #define INIT_S5P_SERIAL_STRUCTURE(port, name, bus) { \ |
| 175 | name, \ |
| 176 | bus, \ |
| 177 | s5p_serial##port##_init, \ |
| 178 | s5p_serial##port##_setbrg, \ |
| 179 | s5p_serial##port##_getc, \ |
| 180 | s5p_serial##port##_tstc, \ |
| 181 | s5p_serial##port##_putc, \ |
| 182 | s5p_serial##port##_puts, } |
| 183 | |
| 184 | DECLARE_S5P_SERIAL_FUNCTIONS(0); |
| 185 | struct serial_device s5pc1xx_serial0_device = |
| 186 | INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0", "S5PUART0"); |
| 187 | DECLARE_S5P_SERIAL_FUNCTIONS(1); |
| 188 | struct serial_device s5pc1xx_serial1_device = |
| 189 | INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1", "S5PUART1"); |
| 190 | DECLARE_S5P_SERIAL_FUNCTIONS(2); |
| 191 | struct serial_device s5pc1xx_serial2_device = |
| 192 | INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2", "S5PUART2"); |
| 193 | DECLARE_S5P_SERIAL_FUNCTIONS(3); |
| 194 | struct serial_device s5pc1xx_serial3_device = |
| 195 | INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3", "S5PUART3"); |