Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2003 Eric Biederman |
| 5 | * Copyright (C) 2006-2010 coresystems GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <arch/io.h> |
Aaron Durbin | 4f7cf3a | 2015-07-30 16:47:18 -0500 | [diff] [blame] | 18 | #include <boot/coreboot_tables.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 19 | #include <console/uart.h> |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 20 | #include <device/device.h> |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 21 | #include <delay.h> |
Aaron Durbin | 4f7cf3a | 2015-07-30 16:47:18 -0500 | [diff] [blame] | 22 | #include <rules.h> |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Kyösti Mälkki | bea6bf0 | 2014-01-30 15:45:16 +0200 | [diff] [blame] | 24 | #include "uart8250reg.h" |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 25 | |
| 26 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
| 27 | |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 28 | /* Expected character delay at 1200bps is 9ms for a working UART |
| 29 | * and no flow-control. Assume UART as stuck if shift register |
| 30 | * or FIFO takes more than 50ms per character to appear empty. |
| 31 | */ |
| 32 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
| 33 | #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT) |
| 34 | |
Naveen Krishna Chatradhi | a73408d | 2015-07-08 14:23:06 +0530 | [diff] [blame] | 35 | #if IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM_32) |
| 36 | static uint8_t uart8250_read(void *base, uint8_t reg) |
| 37 | { |
| 38 | return read32(base + 4 * reg) & 0xff; |
| 39 | } |
| 40 | |
| 41 | static void uart8250_write(void *base, uint8_t reg, uint8_t data) |
| 42 | { |
| 43 | write32(base + 4 * reg, data); |
| 44 | } |
| 45 | #else |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 46 | static uint8_t uart8250_read(void *base, uint8_t reg) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 47 | { |
Kyösti Mälkki | 8517f94 | 2015-03-12 11:33:14 +0200 | [diff] [blame] | 48 | return read8(base + reg); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 51 | static void uart8250_write(void *base, uint8_t reg, uint8_t data) |
| 52 | { |
Kyösti Mälkki | 8517f94 | 2015-03-12 11:33:14 +0200 | [diff] [blame] | 53 | write8(base + reg, data); |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 54 | } |
Naveen Krishna Chatradhi | a73408d | 2015-07-08 14:23:06 +0530 | [diff] [blame] | 55 | #endif |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 56 | |
| 57 | static int uart8250_mem_can_tx_byte(void *base) |
| 58 | { |
| 59 | return uart8250_read(base, UART8250_LSR) & UART8250_LSR_THRE; |
| 60 | } |
| 61 | |
| 62 | static void uart8250_mem_tx_byte(void *base, unsigned char data) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 63 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 64 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 65 | while(i-- && !uart8250_mem_can_tx_byte(base)) |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 66 | udelay(1); |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 67 | uart8250_write(base, UART8250_TBR, data); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 70 | static void uart8250_mem_tx_flush(void *base) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 71 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 72 | unsigned long int i = FIFO_TIMEOUT; |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 73 | while(i-- && !(uart8250_read(base, UART8250_LSR) & UART8250_LSR_TEMT)) |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 74 | udelay(1); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 77 | static int uart8250_mem_can_rx_byte(void *base) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 78 | { |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 79 | return uart8250_read(base, UART8250_LSR) & UART8250_LSR_DR; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 82 | static unsigned char uart8250_mem_rx_byte(void *base) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 83 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 84 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 85 | while(i-- && !uart8250_mem_can_rx_byte(base)) |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 86 | udelay(1); |
| 87 | if (i) |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 88 | return uart8250_read(base, UART8250_RBR); |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 89 | else |
| 90 | return 0x0; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 93 | static void uart8250_mem_init(void *base, unsigned divisor) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 94 | { |
| 95 | /* Disable interrupts */ |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 96 | uart8250_write(base, UART8250_IER, 0x0); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 97 | /* Enable FIFOs */ |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 98 | uart8250_write(base, UART8250_FCR, UART8250_FCR_FIFO_EN); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 99 | |
| 100 | /* Assert DTR and RTS so the other end is happy */ |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 101 | uart8250_write(base, UART8250_MCR, UART8250_MCR_DTR | UART8250_MCR_RTS); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 102 | |
| 103 | /* DLAB on */ |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 104 | uart8250_write(base, UART8250_LCR, UART8250_LCR_DLAB | CONFIG_TTYS0_LCS); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 105 | |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 106 | uart8250_write(base, UART8250_DLL, divisor & 0xFF); |
| 107 | uart8250_write(base, UART8250_DLM, (divisor >> 8) & 0xFF); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 108 | |
| 109 | /* Set to 3 for 8N1 */ |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 110 | uart8250_write(base, UART8250_LCR, CONFIG_TTYS0_LCS); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 113 | void uart_init(int idx) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 114 | { |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 115 | void *base = uart_platform_baseptr(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 116 | if (!base) |
| 117 | return; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 118 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 119 | unsigned int div; |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 120 | div = uart_baudrate_divisor(default_baudrate(), uart_platform_refclk(), 16); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 121 | uart8250_mem_init(base, div); |
| 122 | } |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 123 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 124 | void uart_tx_byte(int idx, unsigned char data) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 125 | { |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 126 | void *base = uart_platform_baseptr(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 127 | if (!base) |
| 128 | return; |
| 129 | uart8250_mem_tx_byte(base, data); |
| 130 | } |
| 131 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 132 | unsigned char uart_rx_byte(int idx) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 133 | { |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 134 | void *base = uart_platform_baseptr(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 135 | if (!base) |
| 136 | return 0xff; |
| 137 | return uart8250_mem_rx_byte(base); |
| 138 | } |
| 139 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 140 | void uart_tx_flush(int idx) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 141 | { |
Kyösti Mälkki | 164ac0a | 2014-12-13 16:39:40 +0200 | [diff] [blame] | 142 | void *base = uart_platform_baseptr(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 143 | if (!base) |
| 144 | return; |
| 145 | uart8250_mem_tx_flush(base); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 146 | } |
Aaron Durbin | 4f7cf3a | 2015-07-30 16:47:18 -0500 | [diff] [blame] | 147 | |
| 148 | #if ENV_RAMSTAGE |
| 149 | void uart_fill_lb(void *data) |
| 150 | { |
| 151 | struct lb_serial serial; |
| 152 | serial.type = LB_SERIAL_TYPE_MEMORY_MAPPED; |
| 153 | serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE); |
| 154 | serial.baud = default_baudrate(); |
| 155 | if (IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM_32)) |
| 156 | serial.regwidth = sizeof(uint32_t); |
| 157 | else |
| 158 | serial.regwidth = sizeof(uint8_t); |
Lee Leahy | f92a98c | 2016-05-04 11:59:19 -0700 | [diff] [blame^] | 159 | if (IS_ENABLED(CONFIG_UART_USE_REFCLK_AS_INPUT_CLOCK)) |
| 160 | serial.input_hertz = uart_platform_refclk(); |
| 161 | else |
| 162 | serial.input_hertz = 0; |
| 163 | serial.uart_pci_addr = CONFIG_UART_PCI_ADDR; |
Aaron Durbin | 4f7cf3a | 2015-07-30 16:47:18 -0500 | [diff] [blame] | 164 | lb_add_serial(&serial, data); |
| 165 | |
| 166 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, data); |
| 167 | } |
| 168 | #endif |