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