Home | History | Annotate | Line # | Download | only in boot
      1  1.5  tsutsui /*	$NetBSD: ns16550.c,v 1.5 2008/04/29 15:24:50 tsutsui Exp $	*/
      2  1.1      cdi 
      3  1.1      cdi /*-
      4  1.5  tsutsui  * Copyright (c) 2008 Izumi Tsutsui.  All rights reserved.
      5  1.1      cdi  *
      6  1.1      cdi  * Redistribution and use in source and binary forms, with or without
      7  1.1      cdi  * modification, are permitted provided that the following conditions
      8  1.1      cdi  * are met:
      9  1.1      cdi  * 1. Redistributions of source code must retain the above copyright
     10  1.1      cdi  *    notice, this list of conditions and the following disclaimer.
     11  1.1      cdi  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1      cdi  *    notice, this list of conditions and the following disclaimer in the
     13  1.1      cdi  *    documentation and/or other materials provided with the distribution.
     14  1.1      cdi  *
     15  1.1      cdi  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1      cdi  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1      cdi  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1      cdi  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1      cdi  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1      cdi  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1      cdi  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1      cdi  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1      cdi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1      cdi  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1      cdi  */
     26  1.1      cdi 
     27  1.1      cdi #ifdef CONS_SERIAL
     28  1.1      cdi 
     29  1.1      cdi #include <lib/libsa/stand.h>
     30  1.4  tsutsui #include <lib/libkern/libkern.h>
     31  1.4  tsutsui 
     32  1.5  tsutsui #include <dev/ic/comreg.h>
     33  1.5  tsutsui 
     34  1.4  tsutsui #include <machine/cpu.h>
     35  1.4  tsutsui 
     36  1.1      cdi #include "boot.h"
     37  1.1      cdi #include "ns16550.h"
     38  1.1      cdi 
     39  1.5  tsutsui #define CSR_READ(base, reg)		(*(volatile uint8_t *)((base) + (reg)))
     40  1.5  tsutsui #define CSR_WRITE(base, reg, val)					\
     41  1.5  tsutsui 	do {								\
     42  1.5  tsutsui 		*(volatile uint8_t *)((base) + (reg)) = (val);		\
     43  1.5  tsutsui 	} while (/* CONSTCOND */ 0)
     44  1.1      cdi 
     45  1.5  tsutsui void *
     46  1.5  tsutsui com_init(int addr, int speed)
     47  1.1      cdi {
     48  1.5  tsutsui 	uint8_t *com_port;
     49  1.1      cdi 
     50  1.4  tsutsui 	com_port = (void *)MIPS_PHYS_TO_KSEG1(COM_BASE + addr);
     51  1.1      cdi 
     52  1.5  tsutsui 	CSR_WRITE(com_port, com_lctl, LCR_DLAB);
     53  1.1      cdi 	speed = comspeed(speed);
     54  1.5  tsutsui 	CSR_WRITE(com_port, com_dlbl, speed);
     55  1.5  tsutsui 	CSR_WRITE(com_port, com_dlbh, speed >> 8);
     56  1.1      cdi 
     57  1.5  tsutsui 	CSR_WRITE(com_port, com_lctl, LCR_PNONE | LCR_8BITS);
     58  1.5  tsutsui 	CSR_WRITE(com_port, com_mcr, MCR_RTS | MCR_DTR);
     59  1.5  tsutsui 	CSR_WRITE(com_port, com_fifo,
     60  1.5  tsutsui 	    FIFO_XMT_RST | FIFO_RCV_RST | FIFO_ENABLE);
     61  1.5  tsutsui 	CSR_WRITE(com_port, com_ier, 0);
     62  1.1      cdi 
     63  1.2  tsutsui 	return com_port;
     64  1.1      cdi }
     65  1.1      cdi 
     66  1.1      cdi void
     67  1.5  tsutsui com_putc(void *dev, int c)
     68  1.1      cdi {
     69  1.5  tsutsui 	volatile uint8_t *com_port = dev;
     70  1.1      cdi 
     71  1.5  tsutsui 	while ((CSR_READ(com_port, com_lsr) & LSR_TXRDY) == 0)
     72  1.1      cdi 		;
     73  1.5  tsutsui 
     74  1.5  tsutsui 	CSR_WRITE(com_port, com_data, c);
     75  1.1      cdi }
     76  1.1      cdi 
     77  1.1      cdi int
     78  1.5  tsutsui com_getc(void *dev)
     79  1.1      cdi {
     80  1.5  tsutsui 	volatile uint8_t *com_port = dev;
     81  1.1      cdi 
     82  1.5  tsutsui 	while ((CSR_READ(com_port, com_lsr) & LSR_RXRDY) == 0)
     83  1.1      cdi 		;
     84  1.5  tsutsui 
     85  1.5  tsutsui 	return CSR_READ(com_port, com_data);
     86  1.1      cdi }
     87  1.1      cdi 
     88  1.1      cdi int
     89  1.5  tsutsui com_scankbd(void *dev)
     90  1.1      cdi {
     91  1.5  tsutsui 	volatile uint8_t *com_port = dev;
     92  1.1      cdi 
     93  1.5  tsutsui 	if ((CSR_READ(com_port, com_lsr) & LSR_RXRDY) == 0)
     94  1.1      cdi 		return -1;
     95  1.1      cdi 
     96  1.5  tsutsui 	return CSR_READ(com_port, com_data);
     97  1.1      cdi }
     98  1.1      cdi #endif /* CONS_SERIAL */
     99