Home | History | Annotate | Line # | Download | only in common
      1  1.2   andvar /*	$NetBSD: iris_zs.c,v 1.2 2024/05/03 21:38:15 andvar Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2018 Naruaki Etomi
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsutsui  * modification, are permitted provided that the following conditions
      9  1.1  tsutsui  * are met:
     10  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsutsui  *
     16  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  tsutsui  */
     27  1.1  tsutsui 
     28  1.1  tsutsui /*-
     29  1.1  tsutsui  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
     30  1.1  tsutsui  * All rights reserved.
     31  1.1  tsutsui  *
     32  1.1  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
     33  1.1  tsutsui  * by Gordon W. Ross and Wayne Knowles
     34  1.1  tsutsui  *
     35  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     36  1.1  tsutsui  * modification, are permitted provided that the following conditions
     37  1.1  tsutsui  * are met:
     38  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     39  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     40  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     41  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     42  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     43  1.1  tsutsui  *
     44  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     45  1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     46  1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     47  1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     48  1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     49  1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     50  1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     51  1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     52  1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     53  1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     54  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     55  1.1  tsutsui  */
     56  1.1  tsutsui 
     57  1.1  tsutsui /*
     58  1.1  tsutsui  * Silicon Graphics "IRIS" series MIPS processors machine bootloader.
     59  1.1  tsutsui  * Zilog Z8530 Dual UART driver.
     60  1.1  tsutsui  * Most of the following was adapted from /sys/arch/sgimips/dev/zs.c.
     61  1.1  tsutsui  *	NetBSD: zs.c,v 1.39 2015/02/18 16:47:58 macallan Exp
     62  1.1  tsutsui  */
     63  1.1  tsutsui 
     64  1.1  tsutsui #include <lib/libsa/stand.h>
     65  1.1  tsutsui #include <lib/libkern/libkern.h>
     66  1.1  tsutsui 
     67  1.1  tsutsui #include <dev/ic/z8530reg.h>
     68  1.1  tsutsui 
     69  1.1  tsutsui #include <mips/cpuregs.h>
     70  1.1  tsutsui #include <machine/cpu.h>
     71  1.1  tsutsui 
     72  1.1  tsutsui #include "iris_machdep.h"
     73  1.1  tsutsui #include "iris_zs.h"
     74  1.1  tsutsui 
     75  1.1  tsutsui #define ZSCLOCK		3672000	 /* PCLK pin input clock rate */
     76  1.1  tsutsui #define ZS_DELAY()	DELAY(3)
     77  1.1  tsutsui #define ZS_DEFSPEED	9600
     78  1.1  tsutsui 
     79  1.1  tsutsui static void zs_write(void *, uint8_t);
     80  1.1  tsutsui static void zs_write_reg(void *, uint8_t, uint8_t);
     81  1.1  tsutsui static void zs_reset(void *);
     82  1.1  tsutsui static struct zschan *zs_get_chan_addr(int zs_unit, int channel);
     83  1.1  tsutsui int	zs_getc(void *);
     84  1.1  tsutsui void	zs_putc(void *, int);
     85  1.1  tsutsui int	zs_scan(void *);
     86  1.1  tsutsui 
     87  1.1  tsutsui static int cons_port;
     88  1.1  tsutsui 
     89  1.1  tsutsui static void
     90  1.1  tsutsui zs_write(void *dev, uint8_t val)
     91  1.1  tsutsui {
     92  1.1  tsutsui 	struct zschan *zc = dev;
     93  1.1  tsutsui 
     94  1.1  tsutsui 	zc->zc_csr = val;
     95  1.1  tsutsui 	ZS_DELAY();
     96  1.1  tsutsui }
     97  1.1  tsutsui 
     98  1.1  tsutsui static void
     99  1.1  tsutsui zs_write_reg(void *dev, uint8_t reg, uint8_t val)
    100  1.1  tsutsui {
    101  1.1  tsutsui 
    102  1.1  tsutsui 	zs_write(dev, reg);
    103  1.1  tsutsui 	zs_write(dev, val);
    104  1.1  tsutsui }
    105  1.1  tsutsui 
    106  1.1  tsutsui static void
    107  1.1  tsutsui zs_reset(void *dev)
    108  1.1  tsutsui {
    109  1.1  tsutsui 
    110  1.1  tsutsui 	/* clear errors */
    111  1.1  tsutsui 	zs_write_reg(dev,  9, 0);
    112  1.1  tsutsui 	/* hardware reset */
    113  1.1  tsutsui 	zs_write_reg(dev,  9, ZSWR9_HARD_RESET);
    114  1.1  tsutsui 	DELAY(1000);
    115  1.1  tsutsui 
    116  1.2   andvar 	/* disable all interrupts */
    117  1.1  tsutsui 	zs_write_reg(dev,  1, 0);
    118  1.1  tsutsui 
    119  1.1  tsutsui 	/* set TX/RX misc parameters and modes */
    120  1.1  tsutsui 	zs_write_reg(dev,  4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP);
    121  1.1  tsutsui 	zs_write_reg(dev, 10, ZSWR10_NRZ);
    122  1.1  tsutsui 	zs_write_reg(dev,  3, ZSWR3_RX_8);
    123  1.1  tsutsui 	zs_write_reg(dev,  5, ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS);
    124  1.1  tsutsui 
    125  1.1  tsutsui 	/* sync registers unused */
    126  1.1  tsutsui 	zs_write_reg(dev,  6, 0);
    127  1.1  tsutsui 	zs_write_reg(dev,  7, 0);
    128  1.1  tsutsui 
    129  1.1  tsutsui 	/* set clock mode */
    130  1.1  tsutsui 	zs_write_reg(dev, 11,
    131  1.1  tsutsui 	    ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD | ZSWR11_TRXC_OUT_ENA);
    132  1.1  tsutsui 
    133  1.1  tsutsui 	/* set baud rate constant */
    134  1.1  tsutsui 	zs_write_reg(dev, 12, BPS_TO_TCONST(ZSCLOCK / 16, ZS_DEFSPEED));
    135  1.1  tsutsui 	zs_write_reg(dev, 13, 0);
    136  1.1  tsutsui 
    137  1.1  tsutsui 	/* enable baud rate generator */
    138  1.1  tsutsui 	zs_write_reg(dev, 14, ZSWR14_BAUD_ENA);
    139  1.1  tsutsui 
    140  1.1  tsutsui 	/* disable all external interrupts */
    141  1.1  tsutsui 	zs_write_reg(dev, 15, 0);
    142  1.1  tsutsui 
    143  1.1  tsutsui 	/* reset external status twice (see src/sys/dev/ic/z8530sc.c) */
    144  1.1  tsutsui 	zs_write(dev, ZSWR0_RESET_STATUS);
    145  1.1  tsutsui 	zs_write(dev, ZSWR0_RESET_STATUS);
    146  1.1  tsutsui 
    147  1.1  tsutsui 	/* enable TX and RX */
    148  1.1  tsutsui 	zs_write_reg(dev,  3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
    149  1.1  tsutsui 	zs_write_reg(dev,  5,
    150  1.1  tsutsui 	    ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS | ZSWR5_TX_ENABLE);
    151  1.1  tsutsui }
    152  1.1  tsutsui 
    153  1.1  tsutsui static struct zschan *
    154  1.1  tsutsui zs_get_chan_addr(int zs_unit, int channel)
    155  1.1  tsutsui {
    156  1.1  tsutsui 	struct zsdevice *addr;
    157  1.1  tsutsui 	struct zschan *zc;
    158  1.1  tsutsui 
    159  1.1  tsutsui 	addr = (struct zsdevice *)MIPS_PHYS_TO_KSEG1(ZS_ADDR);
    160  1.1  tsutsui 
    161  1.1  tsutsui 	zc = &addr->zs_chan_b;
    162  1.1  tsutsui 
    163  1.1  tsutsui 	return zc;
    164  1.1  tsutsui }
    165  1.1  tsutsui 
    166  1.1  tsutsui void *
    167  1.1  tsutsui zs_init(int addr, int speed)
    168  1.1  tsutsui {
    169  1.1  tsutsui 	struct zschan *zs;
    170  1.1  tsutsui 	cons_port = 0;
    171  1.1  tsutsui 
    172  1.1  tsutsui 	zs = zs_get_chan_addr(1, cons_port);
    173  1.1  tsutsui 
    174  1.1  tsutsui 	zs_reset(zs);
    175  1.1  tsutsui 
    176  1.1  tsutsui 	return zs;
    177  1.1  tsutsui }
    178  1.1  tsutsui 
    179  1.1  tsutsui void
    180  1.1  tsutsui zscnputc(void *dev, int c)
    181  1.1  tsutsui {
    182  1.1  tsutsui 	struct zschan *zs;
    183  1.1  tsutsui 
    184  1.1  tsutsui 	zs = zs_get_chan_addr(1, cons_port);
    185  1.1  tsutsui 
    186  1.1  tsutsui 	zs_putc(zs, c);
    187  1.1  tsutsui }
    188  1.1  tsutsui 
    189  1.1  tsutsui void
    190  1.1  tsutsui zs_putc(void *arg, int c)
    191  1.1  tsutsui {
    192  1.1  tsutsui 	register volatile struct zschan *zc = arg;
    193  1.1  tsutsui 	register int rr0;
    194  1.1  tsutsui 
    195  1.1  tsutsui 	/* Wait for transmitter to become ready. */
    196  1.1  tsutsui 	do {
    197  1.1  tsutsui 		rr0 = zc->zc_csr;
    198  1.1  tsutsui 		ZS_DELAY();
    199  1.1  tsutsui 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    200  1.1  tsutsui 
    201  1.1  tsutsui 	zc->zc_data = c;
    202  1.1  tsutsui 	ZS_DELAY();
    203  1.1  tsutsui }
    204  1.1  tsutsui 
    205  1.1  tsutsui int
    206  1.1  tsutsui zscngetc(void *dev)
    207  1.1  tsutsui {
    208  1.1  tsutsui 	struct zschan *zs;
    209  1.1  tsutsui 
    210  1.1  tsutsui 	zs = zs_get_chan_addr(1, cons_port);
    211  1.1  tsutsui 
    212  1.1  tsutsui 	return zs_getc(zs);
    213  1.1  tsutsui }
    214  1.1  tsutsui 
    215  1.1  tsutsui int
    216  1.1  tsutsui zs_getc(void *arg)
    217  1.1  tsutsui {
    218  1.1  tsutsui 	struct zschan *zc = arg;
    219  1.1  tsutsui 	int c, rr0;
    220  1.1  tsutsui 
    221  1.1  tsutsui 	/* Wait for a character to arrive. */
    222  1.1  tsutsui 	do {
    223  1.1  tsutsui 		rr0 = zc->zc_csr;
    224  1.1  tsutsui 		ZS_DELAY();
    225  1.1  tsutsui 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    226  1.1  tsutsui 
    227  1.1  tsutsui 	c = zc->zc_data;
    228  1.1  tsutsui 	ZS_DELAY();
    229  1.1  tsutsui 
    230  1.1  tsutsui 	return c;
    231  1.1  tsutsui }
    232  1.1  tsutsui 
    233  1.1  tsutsui int
    234  1.1  tsutsui zscnscanc(void *dev)
    235  1.1  tsutsui {
    236  1.1  tsutsui 	struct zschan *zs;
    237  1.1  tsutsui 
    238  1.1  tsutsui 	zs = zs_get_chan_addr(1, cons_port);
    239  1.1  tsutsui 
    240  1.1  tsutsui 	return zs_scan(zs);
    241  1.1  tsutsui }
    242  1.1  tsutsui 
    243  1.1  tsutsui int
    244  1.1  tsutsui zs_scan(void *arg)
    245  1.1  tsutsui {
    246  1.1  tsutsui 	struct zschan *zc = arg;
    247  1.1  tsutsui 	int c, rr0;
    248  1.1  tsutsui 
    249  1.1  tsutsui 	/* Wait for a character to arrive. */
    250  1.1  tsutsui 	rr0 = zc->zc_csr;
    251  1.1  tsutsui 	ZS_DELAY();
    252  1.1  tsutsui 
    253  1.1  tsutsui 	if ((rr0 & ZSRR0_RX_READY) == 0) {
    254  1.1  tsutsui 		return -1;
    255  1.1  tsutsui 	}
    256  1.1  tsutsui 
    257  1.1  tsutsui 	c = zc->zc_data;
    258  1.1  tsutsui 	ZS_DELAY();
    259  1.1  tsutsui 
    260  1.1  tsutsui 	return c;
    261  1.1  tsutsui }
    262