#include <machine/machine_routines.h>
#include <pexpert/protos.h>
#include <pexpert/pexpert.h>
struct pe_serial_functions {
void (*uart_init) (void);
void (*uart_set_baud_rate) (int unit, uint32_t baud_rate);
int (*tr0) (void);
void (*td0) (int c);
int (*rr0) (void);
int (*rd0) (void);
};
static struct pe_serial_functions *gPESF;
static int uart_initted = 0;
static unsigned int legacy_uart_enabled = 0;
static boolean_t lpss_uart_supported = 0;
static unsigned int lpss_uart_enabled = 0;
static void lpss_uart_re_init(void);
static boolean_t pcie_uart_enabled = 0;
#define DEFAULT_UART_BAUD_RATE 115200
static unsigned uart_baud_rate = DEFAULT_UART_BAUD_RATE;
#define LEGACY_UART_PORT_ADDR COM1_PORT_ADDR
#define LEGACY_UART_CLOCK 1843200
#define IO_WRITE(r, v) outb(LEGACY_UART_PORT_ADDR + UART_##r, v)
#define IO_READ(r) inb(LEGACY_UART_PORT_ADDR + UART_##r)
enum {
COM1_PORT_ADDR = 0x3f8,
COM2_PORT_ADDR = 0x2f8
};
enum {
UART_RBR = 0,
UART_THR = 0,
UART_DLL = 0,
UART_IER = 1,
UART_DLM = 1,
UART_IIR = 2,
UART_FCR = 2,
UART_LCR = 3,
UART_MCR = 4,
UART_LSR = 5,
UART_MSR = 6,
UART_SCR = 7
};
enum {
UART_LCR_8BITS = 0x03,
UART_LCR_DLAB = 0x80
};
enum {
UART_MCR_DTR = 0x01,
UART_MCR_RTS = 0x02,
UART_MCR_OUT1 = 0x04,
UART_MCR_OUT2 = 0x08,
UART_MCR_LOOP = 0x10
};
enum {
UART_LSR_DR = 0x01,
UART_LSR_OE = 0x02,
UART_LSR_PE = 0x04,
UART_LSR_FE = 0x08,
UART_LSR_THRE = 0x20
};
enum {
UART_CLK_125M_1 = 0x60002,
UART_CLK_125M_2 = 0x80060003,
};
static int
legacy_uart_probe( void )
{
IO_WRITE( SCR, 0x5a );
if (IO_READ(SCR) != 0x5a) {
return 0;
}
IO_WRITE( SCR, 0xa5 );
if (IO_READ(SCR) != 0xa5) {
return 0;
}
return 1;
}
static void
legacy_uart_set_baud_rate( __unused int unit, uint32_t baud_rate )
{
const unsigned char lcr = IO_READ( LCR );
unsigned long div;
if (baud_rate == 0) {
baud_rate = 9600;
}
div = LEGACY_UART_CLOCK / 16 / baud_rate;
IO_WRITE( LCR, lcr | UART_LCR_DLAB );
IO_WRITE( DLM, (unsigned char)(div >> 8));
IO_WRITE( DLL, (unsigned char) div );
IO_WRITE( LCR, lcr & ~UART_LCR_DLAB);
}
static int
legacy_uart_tr0( void )
{
return IO_READ(LSR) & UART_LSR_THRE;
}
static void
legacy_uart_td0( int c )
{
IO_WRITE( THR, c );
}
static void
legacy_uart_init( void )
{
IO_WRITE( MCR, 0 );
IO_WRITE( IER, 0 );
IO_WRITE( FCR, 0 );
IO_WRITE( LCR, UART_LCR_8BITS );
gPESF->uart_set_baud_rate( 0, uart_baud_rate );
IO_WRITE( MCR, UART_MCR_DTR | UART_MCR_RTS );
IO_READ( RBR );
uart_initted = 1;
}
static int
legacy_uart_rr0( void )
{
unsigned char lsr;
lsr = IO_READ( LSR );
if (lsr & (UART_LSR_FE | UART_LSR_PE | UART_LSR_OE)) {
IO_READ( RBR );
return 0;
}
return lsr & UART_LSR_DR;
}
static int
legacy_uart_rd0( void )
{
return IO_READ( RBR );
}
static struct pe_serial_functions legacy_uart_serial_functions = {
.uart_init = legacy_uart_init,
.uart_set_baud_rate = legacy_uart_set_baud_rate,
.tr0 = legacy_uart_tr0,
.td0 = legacy_uart_td0,
.rr0 = legacy_uart_rr0,
.rd0 = legacy_uart_rd0
};
#define MMIO_UART2_BASE_LEGACY 0xFE034000
#define MMIO_UART2_BASE 0xFE036000
#define PCI_UART2 0xFE037000
#define MMIO_WRITE(r, v) ml_phys_write_word(mmio_uart_base + MMIO_UART_##r, v)
#define MMIO_READ(r) ml_phys_read_word(mmio_uart_base + MMIO_UART_##r)
enum {
MMIO_UART_RBR = 0x0,
MMIO_UART_THR = 0x0,
MMIO_UART_DLL = 0x0,
MMIO_UART_IER = 0x4,
MMIO_UART_DLM = 0x4,
MMIO_UART_FCR = 0x8,
MMIO_UART_LCR = 0xc,
MMIO_UART_MCR = 0x10,
MMIO_UART_LSR = 0x14,
MMIO_UART_SCR = 0x1c,
MMIO_UART_CLK = 0x200,
MMIO_UART_RST = 0x204
};
static vm_offset_t mmio_uart_base = 0;
static int
mmio_uart_present( void )
{
MMIO_WRITE( SCR, 0x5a );
if (MMIO_READ(SCR) != 0x5a) {
return 0;
}
MMIO_WRITE( SCR, 0xa5 );
if (MMIO_READ(SCR) != 0xa5) {
return 0;
}
return 1;
}
static int
mmio_uart_probe( void )
{
unsigned new_mmio_uart_base = 0;
if (PE_parse_boot_argn("mmio_uart", &new_mmio_uart_base, sizeof(new_mmio_uart_base))) {
if (new_mmio_uart_base == 0) {
return 0;
}
mmio_uart_base = new_mmio_uart_base;
return 1;
}
mmio_uart_base = MMIO_UART2_BASE;
if (mmio_uart_present()) {
return 1;
}
mmio_uart_base = MMIO_UART2_BASE_LEGACY;
if (mmio_uart_present()) {
return 1;
}
return 0;
}
static void
mmio_uart_set_baud_rate( __unused int unit, __unused uint32_t baud_rate )
{
const unsigned char lcr = MMIO_READ( LCR );
unsigned long div;
if (baud_rate == 0) {
baud_rate = 9600;
}
div = LEGACY_UART_CLOCK / 16 / baud_rate;
MMIO_WRITE( LCR, lcr | UART_LCR_DLAB );
MMIO_WRITE( DLM, (unsigned char)(div >> 8));
MMIO_WRITE( DLL, (unsigned char) div );
MMIO_WRITE( LCR, lcr & ~UART_LCR_DLAB);
}
static int
mmio_uart_tr0( void )
{
return MMIO_READ(LSR) & UART_LSR_THRE;
}
static void
mmio_uart_td0( int c )
{
MMIO_WRITE( THR, c );
}
static void
mmio_uart_init( void )
{
MMIO_WRITE( MCR, 0 );
MMIO_WRITE( IER, 0 );
MMIO_WRITE( FCR, 0 );
MMIO_WRITE( LCR, UART_LCR_8BITS );
if (uart_baud_rate != DEFAULT_UART_BAUD_RATE) {
gPESF->uart_set_baud_rate( 0, uart_baud_rate );
}
MMIO_WRITE( MCR, UART_MCR_DTR | UART_MCR_RTS );
MMIO_READ( RBR );
uart_initted = 1;
}
static int
mmio_uart_rr0( void )
{
unsigned char lsr;
lsr = MMIO_READ( LSR );
if (lsr & (UART_LSR_FE | UART_LSR_PE | UART_LSR_OE)) {
MMIO_READ( RBR );
return 0;
}
return lsr & UART_LSR_DR;
}
void
lpss_uart_enable( boolean_t on_off )
{
unsigned int pmcs_reg;
if (!lpss_uart_supported) {
return;
}
pmcs_reg = ml_phys_read_byte(PCI_UART2 + 0x84);
if (on_off == FALSE) {
pmcs_reg |= 0x03;
lpss_uart_enabled = 0;
} else {
pmcs_reg &= ~(0x03);
}
ml_phys_write_byte(PCI_UART2 + 0x84, pmcs_reg);
pmcs_reg = ml_phys_read_byte(PCI_UART2 + 0x84);
if (on_off == TRUE) {
lpss_uart_re_init();
lpss_uart_enabled = 1;
}
}
static void
lpss_uart_re_init( void )
{
uint32_t register_read;
MMIO_WRITE(RST, 0x7);
register_read = MMIO_READ(RST);
MMIO_WRITE(LCR, UART_LCR_DLAB);
register_read = MMIO_READ(LCR);
MMIO_WRITE(DLL, 1);
register_read = MMIO_READ(DLL);
MMIO_WRITE(DLM, 0);
register_read = MMIO_READ(DLM);
MMIO_WRITE(FCR, 1);
register_read = MMIO_READ(FCR);
MMIO_WRITE(LCR, UART_LCR_8BITS);
register_read = MMIO_READ(LCR);
MMIO_WRITE(MCR, UART_MCR_RTS);
register_read = MMIO_READ(MCR);
MMIO_WRITE(CLK, UART_CLK_125M_1);
register_read = MMIO_READ(CLK);
MMIO_WRITE(CLK, UART_CLK_125M_2);
register_read = MMIO_READ(CLK);
}
static int
mmio_uart_rd0( void )
{
return MMIO_READ( RBR );
}
static struct pe_serial_functions mmio_uart_serial_functions = {
.uart_init = mmio_uart_init,
.uart_set_baud_rate = mmio_uart_set_baud_rate,
.tr0 = mmio_uart_tr0,
.td0 = mmio_uart_td0,
.rr0 = mmio_uart_rr0,
.rd0 = mmio_uart_rd0
};
#define PCIE_MMIO_UART_BASE 0xFE410000
#define PCIE_MMIO_WRITE(r, v) ml_phys_write_byte(pcie_mmio_uart_base + PCIE_MMIO_UART_##r, v)
#define PCIE_MMIO_READ(r) ml_phys_read_byte(pcie_mmio_uart_base + PCIE_MMIO_UART_##r)
enum {
PCIE_MMIO_UART_RBR = 0x0,
PCIE_MMIO_UART_THR = 0x0,
PCIE_MMIO_UART_IER = 0x1,
PCIE_MMIO_UART_FCR = 0x2,
PCIE_MMIO_UART_LCR = 0x4,
PCIE_MMIO_UART_MCR = 0x4,
PCIE_MMIO_UART_LSR = 0x5,
PCIE_MMIO_UART_DLL = 0x8,
PCIE_MMIO_UART_DLM = 0x9,
PCIE_MMIO_UART_SCR = 0x30,
};
static vm_offset_t pcie_mmio_uart_base = 0;
static int
pcie_mmio_uart_present( void )
{
PCIE_MMIO_WRITE( SCR, 0x5a );
if (PCIE_MMIO_READ(SCR) != 0x5a) {
return 0;
}
PCIE_MMIO_WRITE( SCR, 0xa5 );
if (PCIE_MMIO_READ(SCR) != 0xa5) {
return 0;
}
return 1;
}
static int
pcie_mmio_uart_probe( void )
{
unsigned new_pcie_mmio_uart_base = 0;
if (PE_parse_boot_argn("pcie_mmio_uart", &new_pcie_mmio_uart_base, sizeof(new_pcie_mmio_uart_base))) {
if (new_pcie_mmio_uart_base == 0) {
return 0;
}
pcie_mmio_uart_base = new_pcie_mmio_uart_base;
return 1;
}
pcie_mmio_uart_base = PCIE_MMIO_UART_BASE;
if (pcie_mmio_uart_present()) {
return 1;
}
return 0;
}
static void
pcie_mmio_uart_set_baud_rate( __unused int unit, __unused uint32_t baud_rate )
{
const unsigned char lcr = PCIE_MMIO_READ( LCR );
unsigned long div;
if (baud_rate == 0) {
baud_rate = 9600;
}
div = LEGACY_UART_CLOCK / 16 / baud_rate;
PCIE_MMIO_WRITE( LCR, lcr | UART_LCR_DLAB );
PCIE_MMIO_WRITE( DLM, (unsigned char)(div >> 8));
PCIE_MMIO_WRITE( DLL, (unsigned char) div );
PCIE_MMIO_WRITE( LCR, lcr & ~UART_LCR_DLAB);
}
static int
pcie_mmio_uart_tr0( void )
{
return PCIE_MMIO_READ(LSR) & UART_LSR_THRE;
}
static void
pcie_mmio_uart_td0( int c )
{
PCIE_MMIO_WRITE( THR, c );
}
static void
pcie_mmio_uart_init( void )
{
uart_initted = 1;
}
static int
pcie_mmio_uart_rr0( void )
{
unsigned char lsr;
lsr = PCIE_MMIO_READ( LSR );
if (lsr & (UART_LSR_FE | UART_LSR_PE | UART_LSR_OE)) {
PCIE_MMIO_READ( RBR );
return 0;
}
return lsr & UART_LSR_DR;
}
static int
pcie_mmio_uart_rd0( void )
{
return PCIE_MMIO_READ( RBR );
}
static struct pe_serial_functions pcie_mmio_uart_serial_functions = {
.uart_init = pcie_mmio_uart_init,
.uart_set_baud_rate = pcie_mmio_uart_set_baud_rate,
.tr0 = pcie_mmio_uart_tr0,
.td0 = pcie_mmio_uart_td0,
.rr0 = pcie_mmio_uart_rr0,
.rd0 = pcie_mmio_uart_rd0
};
int
serial_init( void )
{
unsigned new_uart_baud_rate = 0;
if (PE_parse_boot_argn("serialbaud", &new_uart_baud_rate, sizeof(new_uart_baud_rate))) {
if (!((LEGACY_UART_CLOCK / 16) % new_uart_baud_rate)) {
uart_baud_rate = new_uart_baud_rate;
}
}
if (mmio_uart_probe()) {
gPESF = &mmio_uart_serial_functions;
gPESF->uart_init();
lpss_uart_supported = 1;
lpss_uart_enabled = 1;
return 1;
} else if (legacy_uart_probe()) {
gPESF = &legacy_uart_serial_functions;
gPESF->uart_init();
legacy_uart_enabled = 1;
return 1;
} else if (pcie_mmio_uart_probe()) {
gPESF = &pcie_mmio_uart_serial_functions;
gPESF->uart_init();
pcie_uart_enabled = 1;
return 1;
} else {
return 0;
}
}
static void
uart_putc(char c)
{
if (uart_initted && (legacy_uart_enabled || lpss_uart_enabled || pcie_uart_enabled)) {
while (!gPESF->tr0()) {
;
}
gPESF->td0(c);
}
}
static int
uart_getc(void)
{
if (uart_initted && (legacy_uart_enabled || lpss_uart_enabled || pcie_uart_enabled)) {
if (!gPESF->rr0()) {
return -1;
}
return gPESF->rd0();
}
return -1;
}
void
serial_putc( char c )
{
uart_putc(c);
}
int
serial_getc( void )
{
return uart_getc();
}