1 1.21 thorpej /* $NetBSD: hd64465uart.c,v 1.21 2023/12/20 14:50:03 thorpej Exp $ */ 2 1.1 uch 3 1.1 uch /*- 4 1.1 uch * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc. 5 1.1 uch * All rights reserved. 6 1.1 uch * 7 1.1 uch * Redistribution and use in source and binary forms, with or without 8 1.1 uch * modification, are permitted provided that the following conditions 9 1.1 uch * are met: 10 1.1 uch * 1. Redistributions of source code must retain the above copyright 11 1.1 uch * notice, this list of conditions and the following disclaimer. 12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 uch * notice, this list of conditions and the following disclaimer in the 14 1.1 uch * documentation and/or other materials provided with the distribution. 15 1.1 uch * 16 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 uch * POSSIBILITY OF SUCH DAMAGE. 27 1.1 uch */ 28 1.10 lukem 29 1.10 lukem #include <sys/cdefs.h> 30 1.21 thorpej __KERNEL_RCSID(0, "$NetBSD: hd64465uart.c,v 1.21 2023/12/20 14:50:03 thorpej Exp $"); 31 1.1 uch 32 1.1 uch #include "opt_kgdb.h" 33 1.1 uch 34 1.1 uch #include <sys/param.h> 35 1.1 uch #include <sys/systm.h> 36 1.1 uch #include <sys/reboot.h> 37 1.1 uch #include <sys/device.h> 38 1.2 uch #include <sys/kgdb.h> 39 1.18 dyoung #include <sys/bus.h> 40 1.1 uch 41 1.1 uch #include <sys/termios.h> 42 1.1 uch #include <dev/cons.h> 43 1.1 uch #include <sys/conf.h> 44 1.1 uch 45 1.1 uch #include <machine/intr.h> 46 1.1 uch #include <machine/console.h> 47 1.1 uch 48 1.1 uch #include <dev/ic/comvar.h> 49 1.1 uch #include <dev/ic/comreg.h> 50 1.1 uch 51 1.1 uch #include <machine/debug.h> 52 1.1 uch 53 1.1 uch #include <hpcsh/dev/hd64465/hd64465var.h> 54 1.4 uch #include <hpcsh/dev/hd64465/hd64465intcreg.h> 55 1.1 uch #include <hpcsh/dev/hd64465/hd64465uartvar.h> 56 1.1 uch #include <hpcsh/dev/hd64465/hd64465uartreg.h> 57 1.1 uch 58 1.1 uch STATIC struct hd64465uart_chip { 59 1.1 uch struct hpcsh_bus_space __tag_body; 60 1.1 uch bus_space_tag_t io_tag; 61 1.1 uch int console; 62 1.1 uch } hd64465uart_chip; 63 1.1 uch 64 1.1 uch struct hd64465uart_softc { 65 1.1 uch struct com_softc sc_com; 66 1.1 uch 67 1.1 uch struct hd64465uart_chip *sc_chip; 68 1.1 uch enum hd64465_module_id sc_module_id; 69 1.1 uch }; 70 1.1 uch 71 1.1 uch /* boot console */ 72 1.2 uch void hd64465uartcnprobe(struct consdev *); 73 1.2 uch void hd64465uartcninit(struct consdev *); 74 1.1 uch 75 1.16 cube STATIC int hd64465uart_match(device_t, cfdata_t , void *); 76 1.16 cube STATIC void hd64465uart_attach(device_t, device_t, void *); 77 1.1 uch 78 1.16 cube CFATTACH_DECL_NEW(hd64465uart, sizeof(struct hd64465uart_softc), 79 1.8 thorpej hd64465uart_match, hd64465uart_attach, NULL, NULL); 80 1.1 uch 81 1.1 uch STATIC void hd64465uart_init(void); 82 1.13 uwe STATIC uint8_t hd64465uart_read_1(void *, bus_space_handle_t, bus_size_t); 83 1.1 uch STATIC void hd64465uart_write_1(void *, bus_space_handle_t, bus_size_t, 84 1.13 uwe uint8_t); 85 1.1 uch 86 1.1 uch #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 87 1.1 uch #ifndef COMCN_SPEED 88 1.1 uch #define COMCN_SPEED 19200 89 1.1 uch #endif 90 1.1 uch 91 1.1 uch void 92 1.2 uch hd64465uartcnprobe(struct consdev *cp) 93 1.1 uch { 94 1.5 gehenna extern struct cdevsw com_cdevsw; 95 1.1 uch int maj; 96 1.1 uch 97 1.1 uch /* locate the major number */ 98 1.5 gehenna maj = cdevsw_lookup_major(&com_cdevsw); 99 1.1 uch 100 1.1 uch /* Initialize required fields. */ 101 1.1 uch cp->cn_dev = makedev(maj, 0); 102 1.1 uch cp->cn_pri = CN_NORMAL; 103 1.1 uch } 104 1.1 uch 105 1.1 uch void 106 1.2 uch hd64465uartcninit(struct consdev *cp) 107 1.1 uch { 108 1.1 uch 109 1.1 uch hd64465uart_init(); 110 1.1 uch 111 1.1 uch comcnattach(hd64465uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ, 112 1.9 thorpej COM_TYPE_NORMAL, CONMODE); 113 1.1 uch 114 1.1 uch hd64465uart_chip.console = 1; 115 1.1 uch } 116 1.2 uch 117 1.2 uch #ifdef KGDB 118 1.2 uch int 119 1.19 matt hd64465uart_kgdb_init(void) 120 1.2 uch { 121 1.2 uch 122 1.2 uch if (strcmp(kgdb_devname, "hd64465uart") != 0) 123 1.2 uch return (1); 124 1.2 uch 125 1.3 uch if (hd64465uart_chip.console) 126 1.3 uch return (1); /* can't share with console */ 127 1.3 uch 128 1.2 uch hd64465uart_init(); 129 1.2 uch 130 1.2 uch if (com_kgdb_attach(hd64465uart_chip.io_tag, 0x0, kgdb_rate, 131 1.9 thorpej COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) { 132 1.15 perry printf("%s: KGDB console open failed.\n", __func__); 133 1.2 uch return (1); 134 1.2 uch } 135 1.2 uch 136 1.2 uch return (0); 137 1.2 uch } 138 1.2 uch #endif /* KGDB */ 139 1.1 uch 140 1.1 uch int 141 1.16 cube hd64465uart_match(device_t parent, cfdata_t cf, void *aux) 142 1.1 uch { 143 1.1 uch struct hd64465_attach_args *ha = aux; 144 1.1 uch 145 1.1 uch return (ha->ha_module_id == HD64465_MODULE_UART); 146 1.1 uch } 147 1.1 uch 148 1.1 uch void 149 1.16 cube hd64465uart_attach(device_t parent, device_t self, void *aux) 150 1.1 uch { 151 1.1 uch struct hd64465_attach_args *ha = aux; 152 1.16 cube struct hd64465uart_softc *sc = device_private(self); 153 1.1 uch struct com_softc *csc = &sc->sc_com; 154 1.14 gdamore bus_space_handle_t ioh; 155 1.1 uch 156 1.16 cube csc->sc_dev = self; 157 1.1 uch sc->sc_chip = &hd64465uart_chip; 158 1.1 uch 159 1.1 uch sc->sc_module_id = ha->ha_module_id; 160 1.1 uch 161 1.3 uch if (!sc->sc_chip->console) 162 1.3 uch hd64465uart_init(); 163 1.1 uch 164 1.14 gdamore bus_space_map(sc->sc_chip->io_tag, 0, 8, 0, &ioh); 165 1.20 thorpej com_init_regs(&csc->sc_regs, sc->sc_chip->io_tag, ioh, 0); 166 1.1 uch csc->sc_frequency = COM_FREQ; 167 1.1 uch 168 1.1 uch /* supply clock XXX notyet */ 169 1.1 uch 170 1.1 uch /* sanity check */ 171 1.14 gdamore if (!com_probe_subr(&csc->sc_regs)) { 172 1.16 cube aprint_error(": device problem. don't attach.\n"); 173 1.1 uch 174 1.1 uch /* stop clock XXX notyet */ 175 1.1 uch return; 176 1.1 uch } 177 1.1 uch 178 1.1 uch com_attach_subr(csc); 179 1.1 uch 180 1.1 uch /* register interrupt handler */ 181 1.4 uch hd64465_intr_establish(HD64465_UART, IST_LEVEL, IPL_TTY, 182 1.1 uch comintr, self); 183 1.1 uch } 184 1.1 uch 185 1.1 uch void 186 1.19 matt hd64465uart_init(void) 187 1.1 uch { 188 1.1 uch 189 1.1 uch if (hd64465uart_chip.io_tag) 190 1.1 uch return; 191 1.1 uch 192 1.1 uch hd64465uart_chip.io_tag = bus_space_create( 193 1.1 uch &hd64465uart_chip.__tag_body, "HD64465 UART I/O", 194 1.1 uch 0xb0008000, 0); /* no extent */ 195 1.1 uch 196 1.1 uch /* override bus_space_read_1, bus_space_write_1 */ 197 1.1 uch hd64465uart_chip.io_tag->hbs_r_1 = hd64465uart_read_1; 198 1.1 uch hd64465uart_chip.io_tag->hbs_w_1 = hd64465uart_write_1; 199 1.1 uch } 200 1.1 uch 201 1.13 uwe uint8_t 202 1.1 uch hd64465uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs) 203 1.1 uch { 204 1.1 uch 205 1.13 uwe return *(volatile uint8_t *)(h + (ofs << 1)); 206 1.1 uch } 207 1.1 uch 208 1.1 uch void 209 1.1 uch hd64465uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs, 210 1.13 uwe uint8_t val) 211 1.1 uch { 212 1.1 uch 213 1.13 uwe *(volatile uint8_t *)(h + (ofs << 1)) = val; 214 1.1 uch } 215