Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 1 | /* |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 2 | * This file is part of the coreboot project. |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 3 | * |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 4 | * Copyright (C) 2000 Rob Taylor, Flying Pig Systems. robt@flyingpig.com. |
| 5 | * Copyright (C) Broadcom Corporation |
| 6 | * Copyright (C) 2015 Google Inc. |
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 21 | */ |
| 22 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 23 | #include <arch/io.h> |
| 24 | #include <boot/coreboot_tables.h> |
| 25 | #include <console/console.h> /* for __console definition */ |
| 26 | #include <console/uart.h> |
| 27 | #include <delay.h> |
| 28 | #include <soc/ns16550.h> |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 29 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 30 | #define SYS_NS16550_CLK 100000000 |
| 31 | #define SYS_NS16550_BAUDRATE 115200 |
| 32 | #define MODE_X_DIV 16 |
| 33 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 34 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 35 | static struct ns16550 * const regs = |
| 36 | (void *)CONFIG_CONSOLE_SERIAL_UART_ADDRESS; |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 37 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 38 | static int calc_divisor(void) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 39 | { |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 40 | /* Compute divisor value. Normally, we should simply return: |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 41 | * ns16550_clk / MODE_X_DIV / baudrate |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 42 | * but we need to round that value by adding 0.5. |
| 43 | * Rounding is especially important at high baud rates. |
| 44 | */ |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 45 | int div = MODE_X_DIV * SYS_NS16550_BAUDRATE; |
| 46 | return (SYS_NS16550_CLK + div / 2) / div; |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 49 | static void ns16550_init(void) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 50 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 51 | int baud_divisor = calc_divisor(); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 52 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 53 | while (!(readl(®s->lsr) & UART_LSR_TEMT)) |
| 54 | ; |
| 55 | |
| 56 | writel(0, ®s->ier); |
| 57 | writel(UART_LCR_BKSE | UART_LCR_8N1, ®s->lcr); |
| 58 | writel(0, ®s->dll); |
| 59 | writel(0, ®s->dlm); |
| 60 | writel(UART_LCR_8N1, ®s->lcr); |
| 61 | writel(UART_MCR_DTR | UART_MCR_RTS, ®s->mcr); |
| 62 | /* clear & enable FIFOs */ |
| 63 | writel(UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR, ®s->fcr); |
| 64 | writel(UART_LCR_BKSE | UART_LCR_8N1, ®s->lcr); |
| 65 | writel(baud_divisor & 0xff, ®s->dll); |
| 66 | writel((baud_divisor >> 8) & 0xff, ®s->dlm); |
| 67 | writel(UART_LCR_8N1, ®s->lcr); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 70 | static void ns16550_tx_byte(unsigned char data) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 71 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 72 | while ((readl(®s->lsr) & UART_LSR_THRE) == 0) |
| 73 | ; |
| 74 | writel(data, ®s->thr); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 77 | static void ns16550_tx_flush(void) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 78 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 79 | while (!(readl(®s->lsr) & UART_LSR_TEMT)) |
| 80 | ; |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 83 | static int ns16550_tst_byte(void) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 84 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 85 | return (readl(®s->lsr) & UART_LSR_DR) != 0; |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 88 | static unsigned char ns16550_rx_byte(void) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 89 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 90 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 91 | while (i-- && !ns16550_tst_byte()) |
| 92 | udelay(1); |
| 93 | if (i) |
| 94 | return readl(®s->rbr); |
| 95 | else |
| 96 | return 0x0; |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 99 | void uart_init(int idx) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 100 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 101 | ns16550_init(); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 104 | void uart_tx_byte(int idx, unsigned char data) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 105 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 106 | ns16550_tx_byte(data); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 109 | void uart_tx_flush(int idx) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 110 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 111 | ns16550_tx_flush(); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 114 | unsigned char uart_rx_byte(int idx) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 115 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 116 | return ns16550_rx_byte(); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 119 | #ifndef __PRE_RAM__ |
| 120 | void uart_fill_lb(void *data) |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 121 | { |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 122 | struct lb_serial serial; |
| 123 | serial.type = LB_SERIAL_TYPE_MEMORY_MAPPED; |
| 124 | serial.baseaddr = (uintptr_t)regs; |
| 125 | serial.baud = default_baudrate(); |
| 126 | serial.regwidth = 1; |
| 127 | lb_add_serial(&serial, data); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 128 | |
Daisuke Nojiri | c047b10 | 2015-01-23 10:02:24 -0800 | [diff] [blame^] | 129 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, data); |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 130 | } |
Daisuke Nojiri | 3a2ac88 | 2015-02-09 19:39:39 -0800 | [diff] [blame] | 131 | #endif |