Home | History | Annotate | Line # | Download | only in common
cons_zs.c revision 1.1
      1 /*	$NetBSD: cons_zs.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 Izumi Tsutsui.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 
     33 #include <lib/libsa/stand.h>
     34 #include <lib/libkern/libkern.h>
     35 
     36 #include <dev/ic/z8530reg.h>
     37 
     38 #include <machine/sbd.h>
     39 
     40 #include "console.h"
     41 
     42 
     43 struct zs zs;
     44 
     45 static void zs_init(void);
     46 static int zs_cngetc(void);
     47 static int zs_cnscan(void);
     48 static void zs_cnputc(int, int, int);
     49 
     50 #define	ZS_CONSDEFSPEED	9600
     51 
     52 void
     53 zs_set_addr(uint32_t csr, uint32_t data, int clock)
     54 {
     55 
     56 	zs.csr  = (volatile uint8_t *)csr;
     57 	zs.data = (volatile uint8_t *)data;
     58 	zs.clock = clock;
     59 
     60 	cons.init = zs_init;
     61 	cons.getc = zs_cngetc;
     62 	cons.scan = zs_cnscan;
     63 	cons.putc = zs_cnputc;
     64 }
     65 
     66 #define	ZS_WRITE_REG(zs, reg, val)	\
     67 	do {				\
     68 		*zs.csr = reg;		\
     69 		*zs.csr = val;		\
     70 	} while (/* CONSTCOND */ 0)
     71 
     72 static void
     73 zs_init(void)
     74 {
     75 
     76 	ZS_WRITE_REG(zs,  9, 0);
     77 	ZS_WRITE_REG(zs,  9, ZSWR9_HARD_RESET);
     78 
     79 	ZS_WRITE_REG(zs,  4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP);
     80 	ZS_WRITE_REG(zs, 10, 0x00);
     81 	ZS_WRITE_REG(zs,  3, ZSWR3_RX_8);
     82 	ZS_WRITE_REG(zs,  5, ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS);
     83 
     84 	ZS_WRITE_REG(zs,  6, 0x00);
     85 	ZS_WRITE_REG(zs,  7, 0x00);
     86 
     87 	ZS_WRITE_REG(zs, 14, ZSWR14_BAUD_FROM_PCLK);
     88 	ZS_WRITE_REG(zs, 11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD);
     89 	ZS_WRITE_REG(zs, 12, BPS_TO_TCONST(zs.clock / 16, ZS_CONSDEFSPEED));
     90 	ZS_WRITE_REG(zs, 13, 0);
     91 
     92 	ZS_WRITE_REG(zs, 14, ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA);
     93 	ZS_WRITE_REG(zs, 15, 0x00);
     94 
     95 	*zs.csr = ZSWR0_RESET_STATUS;
     96 	*zs.csr = ZSWR0_RESET_STATUS;
     97 
     98 	ZS_WRITE_REG(zs,  3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
     99 	ZS_WRITE_REG(zs,  5,
    100 	    ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS | ZSWR5_TX_ENABLE);
    101 }
    102 
    103 static int
    104 zs_cngetc(void)
    105 {
    106 	int csr, data;
    107 
    108 	do {
    109 		csr = *zs.csr;
    110 	} while ((csr & ZSRR0_RX_READY) == 0);
    111 
    112 	data = *zs.data;
    113 
    114 	return data;
    115 }
    116 
    117 int
    118 zs_cnscan(void)
    119 {
    120 	int csr, data;
    121 
    122 	csr = *zs.csr;
    123 	if ((csr & ZSRR0_RX_READY) == 0)
    124 		return -1;
    125 
    126 	data = *zs.data;
    127 
    128 	return data;
    129 }
    130 
    131 static void
    132 zs_cnputc(int x, int y, int c)
    133 {
    134 	int csr;
    135 
    136 	do {
    137 		csr = *zs.csr;
    138 	} while ((csr & ZSRR0_TX_READY) == 0);
    139 
    140 	*zs.data = c;
    141 }
    142