Home | History | Annotate | Line # | Download | only in hd64461
      1 /*	$NetBSD: hd64461uart.c,v 1.32 2023/12/20 14:50:02 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: hd64461uart.c,v 1.32 2023/12/20 14:50:02 thorpej Exp $");
     31 
     32 #include "opt_kgdb.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/reboot.h>
     37 #include <sys/device.h>
     38 #include <sys/kgdb.h>
     39 
     40 #include <sys/termios.h>
     41 #include <dev/cons.h>
     42 #include <sys/conf.h>
     43 #include <sys/bus.h>
     44 
     45 #include <machine/intr.h>
     46 #include <machine/console.h>
     47 #include <machine/platid.h>
     48 #include <machine/platid_mask.h>
     49 
     50 #include <dev/ic/comvar.h>
     51 #include <dev/ic/comreg.h>
     52 
     53 #include <machine/debug.h>
     54 
     55 #include <hpcsh/dev/hd64461/hd64461var.h>
     56 #include <hpcsh/dev/hd64461/hd64461reg.h>
     57 #include <hpcsh/dev/hd64461/hd64461intcreg.h>
     58 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
     59 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
     60 
     61 STATIC struct hd64461uart_chip {
     62 	struct hpcsh_bus_space __tag_body;
     63 	bus_space_tag_t io_tag;
     64 	int console;
     65 } hd64461uart_chip;
     66 
     67 struct hd64461uart_softc {
     68 	struct com_softc sc_com;
     69 
     70 	struct hd64461uart_chip *sc_chip;
     71 	enum hd64461_module_id sc_module_id;
     72 };
     73 
     74 /* boot console */
     75 void hd64461uartcnprobe(struct consdev *);
     76 void hd64461uartcninit(struct consdev *);
     77 
     78 STATIC int hd64461uart_match(device_t, cfdata_t , void *);
     79 STATIC void hd64461uart_attach(device_t, device_t, void *);
     80 
     81 CFATTACH_DECL_NEW(hd64461uart, sizeof(struct hd64461uart_softc),
     82     hd64461uart_match, hd64461uart_attach, NULL, NULL);
     83 
     84 STATIC void hd64461uart_init(void);
     85 
     86 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
     87 #ifndef COMCN_SPEED
     88 #define COMCN_SPEED	19200
     89 #endif
     90 
     91 static void
     92 hd64461uart_init_regs(struct com_regs *regs, bus_space_tag_t tag,
     93 		      bus_space_handle_t hdl, bus_addr_t addr)
     94 {
     95 
     96 	com_init_regs_stride(regs, tag, hdl, addr, 1);
     97 }
     98 
     99 void
    100 hd64461uartcnprobe(struct consdev *cp)
    101 {
    102 	extern const struct cdevsw com_cdevsw;
    103 	int maj;
    104 
    105 	/* locate the major number */
    106 	maj = cdevsw_lookup_major(&com_cdevsw);
    107 
    108 	/* Initialize required fields. */
    109 	cp->cn_dev = makedev(maj, 0);
    110 	cp->cn_pri = CN_NORMAL;
    111 }
    112 
    113 void
    114 hd64461uartcninit(struct consdev *cp)
    115 {
    116 	struct com_regs regs;
    117 
    118 	hd64461uart_init();
    119 
    120 	hd64461uart_init_regs(&regs, hd64461uart_chip.io_tag, 0x0, 0x0);
    121 	comcnattach1(&regs, COMCN_SPEED, COM_FREQ, COM_TYPE_NORMAL, CONMODE);
    122 
    123 	hd64461uart_chip.console = 1;
    124 	/* Don't stop to suply AFECK */
    125 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    126 		use_afeck = 1;
    127 }
    128 
    129 #ifdef KGDB
    130 int
    131 hd64461uart_kgdb_init(void)
    132 {
    133 	struct com_regs regs;
    134 
    135 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
    136 		return 1;
    137 
    138 	if (hd64461uart_chip.console)
    139 		return 1;	/* can't share with console */
    140 
    141 	hd64461uart_init();
    142 
    143 	hd64461uart_init_regs(&regs, hd64461uart_chip.io_tag, 0x0, 0x0);
    144 	if (com_kgdb_attach1(&regs,
    145 	    kgdb_rate, COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
    146 		printf("%s: KGDB console open failed.\n", __func__);
    147 		return 1;
    148 	}
    149 
    150 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    151 		use_afeck = 1;
    152 	return 0;
    153 }
    154 #endif /* KGDB */
    155 
    156 STATIC int
    157 hd64461uart_match(device_t parent, cfdata_t cf, void *aux)
    158 {
    159 	struct hd64461_attach_args *ha = aux;
    160 
    161 	return ha->ha_module_id == HD64461_MODULE_UART;
    162 }
    163 
    164 STATIC void
    165 hd64461uart_attach(device_t parent, device_t self, void *aux)
    166 {
    167 	struct hd64461_attach_args *ha = aux;
    168 	struct hd64461uart_softc *sc = device_private(self);
    169 	struct com_softc *csc = &sc->sc_com;
    170 	uint16_t r16, or16;
    171 	bus_space_handle_t ioh;
    172 
    173 	csc->sc_dev = self;
    174 	sc->sc_chip = &hd64461uart_chip;
    175 
    176 	sc->sc_module_id = ha->ha_module_id;
    177 
    178 	if (!sc->sc_chip->console)
    179 		hd64461uart_init();
    180 
    181 	bus_space_map(sc->sc_chip->io_tag, 0x0, 8, 0, &ioh);
    182 	csc->sc_frequency = COM_FREQ;
    183 	hd64461uart_init_regs(&csc->sc_regs, sc->sc_chip->io_tag, ioh, 0x0);
    184 
    185 	/* switch port to UART */
    186 
    187 	/* supply clock */
    188 	r16 = or16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
    189 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
    190 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    191 		r16 &= ~(HD64461_SYSSTBCR_SAFECKE_IST |
    192 		    HD64461_SYSSTBCR_SAFECKE_OST);
    193 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
    194 
    195 	/* sanity check */
    196 	if (!com_probe_subr(&csc->sc_regs)) {
    197 		aprint_error(": device problem. don't attach.\n");
    198 
    199 		/* restore old clock */
    200 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, or16);
    201 		return;
    202 	}
    203 
    204 	com_attach_subr(csc);
    205 
    206 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
    207 	    comintr, csc);
    208 }
    209 
    210 STATIC void
    211 hd64461uart_init(void)
    212 {
    213 
    214 	if (hd64461uart_chip.io_tag)
    215 		return;
    216 
    217 	hd64461uart_chip.io_tag = bus_space_create(
    218 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
    219 		HD64461_UART_REGBASE, 0); /* no extent */
    220 }
    221