2015-02-10 04:39:39 +01:00
|
|
|
/*
|
2015-01-23 19:02:24 +01:00
|
|
|
* This file is part of the coreboot project.
|
2015-02-10 04:39:39 +01:00
|
|
|
*
|
2015-01-23 19:02:24 +01:00
|
|
|
* Copyright (C) 2000 Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
|
|
|
|
* Copyright (C) Broadcom Corporation
|
|
|
|
* Copyright (C) 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2015-02-10 04:39:39 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
#include <arch/io.h>
|
|
|
|
#include <boot/coreboot_tables.h>
|
|
|
|
#include <console/console.h> /* for __console definition */
|
|
|
|
#include <console/uart.h>
|
|
|
|
#include <delay.h>
|
|
|
|
#include <soc/ns16550.h>
|
2015-02-10 04:39:39 +01:00
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
#define SYS_NS16550_CLK 100000000
|
|
|
|
#define SYS_NS16550_BAUDRATE 115200
|
|
|
|
#define MODE_X_DIV 16
|
|
|
|
#define SINGLE_CHAR_TIMEOUT (50 * 1000)
|
2015-02-10 04:39:39 +01:00
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static struct ns16550 * const regs =
|
|
|
|
(void *)CONFIG_CONSOLE_SERIAL_UART_ADDRESS;
|
2015-02-10 04:39:39 +01:00
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static int calc_divisor(void)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
|
|
|
/* Compute divisor value. Normally, we should simply return:
|
2015-01-23 19:02:24 +01:00
|
|
|
* ns16550_clk / MODE_X_DIV / baudrate
|
2015-02-10 04:39:39 +01:00
|
|
|
* but we need to round that value by adding 0.5.
|
|
|
|
* Rounding is especially important at high baud rates.
|
|
|
|
*/
|
2015-01-23 19:02:24 +01:00
|
|
|
int div = MODE_X_DIV * SYS_NS16550_BAUDRATE;
|
|
|
|
return (SYS_NS16550_CLK + div / 2) / div;
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static void ns16550_init(void)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
int baud_divisor = calc_divisor();
|
|
|
|
|
2015-04-21 09:28:39 +02:00
|
|
|
while (!(read32(®s->lsr) & UART_LSR_TEMT))
|
2015-01-23 19:02:24 +01:00
|
|
|
;
|
|
|
|
|
2015-04-21 09:28:39 +02:00
|
|
|
write32(®s->ier, 0);
|
|
|
|
write32(®s->lcr, UART_LCR_BKSE | UART_LCR_8N1);
|
|
|
|
write32(®s->dll, 0);
|
|
|
|
write32(®s->dlm, 0);
|
|
|
|
write32(®s->lcr, UART_LCR_8N1);
|
|
|
|
write32(®s->mcr, UART_MCR_DTR | UART_MCR_RTS);
|
2015-01-23 19:02:24 +01:00
|
|
|
/* clear & enable FIFOs */
|
2015-04-21 09:28:39 +02:00
|
|
|
write32(®s->fcr, UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR);
|
|
|
|
write32(®s->lcr, UART_LCR_BKSE | UART_LCR_8N1);
|
|
|
|
write32(®s->dll, baud_divisor & 0xff);
|
|
|
|
write32(®s->dlm, (baud_divisor >> 8) & 0xff);
|
|
|
|
write32(®s->lcr, UART_LCR_8N1);
|
2015-01-23 19:02:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ns16550_tx_byte(unsigned char data)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-04-21 09:28:39 +02:00
|
|
|
while ((read32(®s->lsr) & UART_LSR_THRE) == 0)
|
2015-01-23 19:02:24 +01:00
|
|
|
;
|
2015-04-21 09:28:39 +02:00
|
|
|
write32(®s->thr, data);
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static void ns16550_tx_flush(void)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-04-21 09:28:39 +02:00
|
|
|
while (!(read32(®s->lsr) & UART_LSR_TEMT))
|
2015-01-23 19:02:24 +01:00
|
|
|
;
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static int ns16550_tst_byte(void)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-04-21 09:28:39 +02:00
|
|
|
return (read32(®s->lsr) & UART_LSR_DR) != 0;
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
static unsigned char ns16550_rx_byte(void)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
unsigned long int i = SINGLE_CHAR_TIMEOUT;
|
|
|
|
while (i-- && !ns16550_tst_byte())
|
|
|
|
udelay(1);
|
|
|
|
if (i)
|
2015-04-21 09:28:39 +02:00
|
|
|
return read32(®s->rbr);
|
2015-01-23 19:02:24 +01:00
|
|
|
else
|
|
|
|
return 0x0;
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
void uart_init(int idx)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
ns16550_init();
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
void uart_tx_byte(int idx, unsigned char data)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
ns16550_tx_byte(data);
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
void uart_tx_flush(int idx)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
ns16550_tx_flush();
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
unsigned char uart_rx_byte(int idx)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
return ns16550_rx_byte();
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
#ifndef __PRE_RAM__
|
|
|
|
void uart_fill_lb(void *data)
|
2015-02-10 04:39:39 +01:00
|
|
|
{
|
2015-01-23 19:02:24 +01:00
|
|
|
struct lb_serial serial;
|
|
|
|
serial.type = LB_SERIAL_TYPE_MEMORY_MAPPED;
|
|
|
|
serial.baseaddr = (uintptr_t)regs;
|
|
|
|
serial.baud = default_baudrate();
|
|
|
|
serial.regwidth = 1;
|
|
|
|
lb_add_serial(&serial, data);
|
2015-02-10 04:39:39 +01:00
|
|
|
|
2015-01-23 19:02:24 +01:00
|
|
|
lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, data);
|
2015-02-10 04:39:39 +01:00
|
|
|
}
|
|
|
|
#endif
|