Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Samsung Electronics |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 16 | #include <arch/io.h> |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 17 | #include <boot/coreboot_tables.h> |
Julius Werner | 80af442 | 2014-10-20 13:18:56 -0700 | [diff] [blame] | 18 | #include <console/uart.h> |
| 19 | #include <soc/clk.h> |
| 20 | #include <soc/cpu.h> |
| 21 | #include <soc/periph.h> |
| 22 | #include <soc/uart.h> |
| 23 | #include <types.h> |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 24 | |
| 25 | #define RX_FIFO_COUNT_MASK 0xff |
| 26 | #define RX_FIFO_FULL_MASK (1 << 8) |
| 27 | #define TX_FIFO_FULL_MASK (1 << 24) |
| 28 | |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * The coefficient, used to calculate the baudrate on S5P UARTs is |
| 32 | * calculated as |
| 33 | * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT |
Martin Roth | 1fc2ba5 | 2014-12-07 14:59:11 -0700 | [diff] [blame] | 34 | * however, section 31.6.11 of the datasheet doesn't recommend using 1 for 1, |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 35 | * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants: |
| 36 | */ |
| 37 | static const int udivslot[] = { |
| 38 | 0, |
| 39 | 0x0080, |
| 40 | 0x0808, |
| 41 | 0x0888, |
| 42 | 0x2222, |
| 43 | 0x4924, |
| 44 | 0x4a52, |
| 45 | 0x54aa, |
| 46 | 0x5555, |
| 47 | 0xd555, |
| 48 | 0xd5d5, |
| 49 | 0xddd5, |
| 50 | 0xdddd, |
| 51 | 0xdfdd, |
| 52 | 0xdfdf, |
| 53 | 0xffdf, |
| 54 | }; |
| 55 | |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 56 | static void serial_setbrg_dev(struct s5p_uart *uart) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 57 | { |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 58 | u32 uclk; |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 59 | u32 val; |
| 60 | |
| 61 | // All UARTs share the same clock. |
| 62 | uclk = clock_get_periph_rate(PERIPH_ID_UART3); |
Kyösti Mälkki | c5332e3 | 2014-02-19 08:58:12 +0200 | [diff] [blame] | 63 | val = uclk / default_baudrate(); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 64 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 65 | write32(&uart->ubrdiv, val / 16 - 1); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * FIXME(dhendrix): the original uart.h had a "br_rest" value which |
| 69 | * does not seem relevant to the exynos5420... not entirely sure |
| 70 | * where/if we need to worry about it here |
| 71 | */ |
| 72 | #if 0 |
| 73 | if (s5p_uart_divslot()) |
| 74 | writel(udivslot[val % 16], &uart->rest.slot); |
| 75 | else |
| 76 | writeb(val % 16, &uart->rest.value); |
| 77 | #endif |
| 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 | */ |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 84 | static void exynos5_init_dev(struct s5p_uart *uart) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 85 | { |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 86 | /* enable FIFOs */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 87 | write32(&uart->ufcon, 0x1); |
| 88 | write32(&uart->umcon, 0); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 89 | /* 8N1 */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 90 | write32(&uart->ulcon, 0x3); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 91 | /* No interrupts, no DMA, pure polling */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 92 | write32(&uart->ucon, 0x245); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 93 | |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 94 | serial_setbrg_dev(uart); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 97 | static int exynos5_uart_err_check(struct s5p_uart *uart, int op) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 98 | { |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 99 | unsigned int mask; |
| 100 | |
| 101 | /* |
| 102 | * UERSTAT |
| 103 | * Break Detect [3] |
| 104 | * Frame Err [2] : receive operation |
| 105 | * Parity Err [1] : receive operation |
| 106 | * Overrun Err [0] : receive operation |
| 107 | */ |
| 108 | if (op) |
| 109 | mask = 0x8; |
| 110 | else |
| 111 | mask = 0xf; |
| 112 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 113 | return read32(&uart->uerstat) & mask; |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Read a single byte from the serial port. Returns 1 on success, 0 |
Martin Roth | 1fc2ba5 | 2014-12-07 14:59:11 -0700 | [diff] [blame] | 118 | * otherwise. When the function is successful, the character read is |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 119 | * written into its argument c. |
| 120 | */ |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 121 | static unsigned char exynos5_uart_rx_byte(struct s5p_uart *uart) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 122 | { |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 123 | /* wait for character to arrive */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 124 | while (!(read32(&uart->ufstat) & (RX_FIFO_COUNT_MASK | |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 125 | RX_FIFO_FULL_MASK))) { |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 126 | if (exynos5_uart_err_check(uart, 0)) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 130 | return read8(&uart->urxh) & 0xff; |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Output a single byte to the serial port. |
| 135 | */ |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 136 | static void exynos5_uart_tx_byte(struct s5p_uart *uart, unsigned char data) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 137 | { |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 138 | /* wait for room in the tx FIFO */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 139 | while ((read32(&uart->ufstat) & TX_FIFO_FULL_MASK)) { |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 140 | if (exynos5_uart_err_check(uart, 1)) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 144 | write8(&uart->utxh, data); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ronald G. Minnich | 2adb297 | 2014-10-16 10:53:48 +0000 | [diff] [blame] | 147 | uintptr_t uart_platform_base(int idx) |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 148 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 149 | if (idx < 4) |
| 150 | return 0x12c00000 + idx * 0x10000; |
| 151 | else |
| 152 | return 0; |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 155 | void uart_init(int idx) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 156 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 157 | struct s5p_uart *uart = uart_platform_baseptr(idx); |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 158 | exynos5_init_dev(uart); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 161 | unsigned char uart_rx_byte(int idx) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 162 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 163 | struct s5p_uart *uart = uart_platform_baseptr(idx); |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 164 | return exynos5_uart_rx_byte(uart); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 167 | void uart_tx_byte(int idx, unsigned char data) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 168 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 169 | struct s5p_uart *uart = uart_platform_baseptr(idx); |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 170 | exynos5_uart_tx_byte(uart, data); |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 173 | void uart_tx_flush(int idx) |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 174 | { |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 175 | /* Exynos5250 implements this too. */ |
Gabe Black | 607c0b6 | 2013-05-16 05:45:57 -0700 | [diff] [blame] | 176 | } |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 177 | |
| 178 | #ifndef __PRE_RAM__ |
| 179 | void uart_fill_lb(void *data) |
| 180 | { |
| 181 | struct lb_serial serial; |
| 182 | serial.type = LB_SERIAL_TYPE_MEMORY_MAPPED; |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 183 | serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE); |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 184 | serial.baud = default_baudrate(); |
Vadim Bendebury | 9dccf1c | 2015-01-09 16:54:19 -0800 | [diff] [blame] | 185 | serial.regwidth = 1; |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 186 | lb_add_serial(&serial, data); |
| 187 | |
| 188 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, data); |
| 189 | } |
| 190 | #endif |