1 /* $NetBSD: com_pioc.c,v 1.7 2002/10/02 03:31:58 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /*- 40 * Copyright (c) 1991 The Regents of the University of California. 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)com.c 7.5 (Berkeley) 5/16/91 72 */ 73 74 #include <sys/param.h> 75 76 __KERNEL_RCSID(0, "$NetBSD: com_pioc.c,v 1.7 2002/10/02 03:31:58 thorpej Exp $"); 77 78 #include <sys/systm.h> 79 #include <sys/tty.h> 80 #include <sys/proc.h> 81 #include <sys/conf.h> 82 #include <sys/kernel.h> 83 #include <sys/device.h> 84 85 #include <machine/intr.h> 86 #include <machine/bus.h> 87 #include <machine/io.h> 88 89 #include <acorn32/mainbus/piocvar.h> 90 #include <dev/ic/comreg.h> 91 #include <dev/ic/comvar.h> 92 93 #include <dev/cons.h> 94 95 #include "locators.h" 96 97 struct com_pioc_softc { 98 struct com_softc sc_com; /* real "com" softc */ 99 void *sc_ih; /* interrupt handler */ 100 }; 101 102 /* Prototypes for functions */ 103 104 cons_decl(com); 105 106 static int com_pioc_probe __P((struct device *, struct cfdata *, void *)); 107 static void com_pioc_attach __P((struct device *, struct device *, void *)); 108 static void com_pioc_cleanup __P((void *)); 109 110 /* device attach structure */ 111 112 CFATTACH_DECL(com_pioc, sizeof(struct com_pioc_softc), 113 com_pioc_probe, com_pioc_attach, NULL, NULL); 114 115 extern bus_space_tag_t comconstag; /* From pioc.c */ 116 117 /* 118 * int com_pioc_probe(struct device *parent, struct cfdata *cf, void *aux) 119 * 120 * Make sure we are trying to attach a com device and then 121 * probe for one. 122 */ 123 124 static int 125 com_pioc_probe(parent, cf, aux) 126 struct device *parent; 127 struct cfdata *cf; 128 void *aux; 129 { 130 bus_space_tag_t iot; 131 bus_space_handle_t ioh; 132 int iobase; 133 int rv = 1; 134 struct pioc_attach_args *pa = aux; 135 136 /* We need an offset */ 137 if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT) 138 return(0); 139 140 iot = pa->pa_iot; 141 iobase = pa->pa_iobase + pa->pa_offset; 142 143 /* if it's in use as console, it's there. */ 144 if (!com_is_console(iot, iobase, 0)) { 145 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) { 146 return 0; 147 } 148 rv = comprobe1(iot, ioh); 149 bus_space_unmap(iot, ioh, COM_NPORTS); 150 } 151 152 if (rv) { 153 pa->pa_iosize = COM_NPORTS; 154 } 155 return (rv); 156 } 157 158 /* 159 * void com_pioc_attach(struct device *parent, struct device *self, void *aux) 160 * 161 * attach the com device 162 */ 163 164 static void 165 com_pioc_attach(parent, self, aux) 166 struct device *parent, *self; 167 void *aux; 168 { 169 struct com_pioc_softc *psc = (void *)self; 170 struct com_softc *sc = &psc->sc_com; 171 u_int iobase; 172 bus_space_tag_t iot; 173 struct pioc_attach_args *pa = aux; 174 int count; 175 176 iot = sc->sc_iot = pa->pa_iot; 177 iobase = sc->sc_iobase = pa->pa_iobase + pa->pa_offset; 178 179 /* 180 printf(" (iot = %p, iobase = 0x%08x) ", iot, iobase); 181 */ 182 if (!com_is_console(iot, iobase, &sc->sc_ioh) 183 && bus_space_map(iot, iobase, COM_NPORTS, 0, &sc->sc_ioh)) 184 panic("comattach: io mapping failed"); 185 186 sc->sc_frequency = COM_FREQ; 187 188 com_attach_subr(sc); 189 190 if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT) { 191 psc->sc_ih = intr_claim(pa->pa_irq, IPL_SERIAL, "com", 192 comintr, sc); 193 } 194 195 /* 196 * Shutdown hook for buggy BIOSs that don't recognize the UART 197 * without a disabled FIFO. 198 */ 199 if (shutdownhook_establish(com_pioc_cleanup, sc) == NULL) 200 panic("%s: could not establish shutdown hook", 201 sc->sc_dev.dv_xname); 202 203 /* 204 * This is a patch for bugged revision 1-4 SMC FDC37C665 205 * I/O controllers. 206 * If there is RX data pending when the FIFO in turned on 207 * the RX register cannot be emptied. 208 * 209 * Solution: 210 * Make sure FIFO is off. 211 * Read pending data / int status etc. 212 */ 213 bus_space_write_1(iot, sc->sc_ioh, com_fifo, 0); 214 for (count = 0; count < 8; ++count) 215 (void)bus_space_read_1(iot, sc->sc_ioh, count); 216 217 } 218 219 /* 220 * void com_pioc_cleanup(void *arg) 221 * 222 * clean up driver 223 */ 224 225 static void 226 com_pioc_cleanup(arg) 227 void *arg; 228 { 229 struct com_softc *sc = arg; 230 231 if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) 232 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_fifo, 0); 233 } 234 235 /* 236 * Console attachment functions 237 */ 238 239 void 240 comcnprobe(cp) 241 struct consdev *cp; 242 { 243 244 #ifdef COMCONSOLE 245 cp->cn_pri = CN_REMOTE; /* Force a serial port console */ 246 #else 247 cp->cn_pri = CN_NORMAL; 248 #endif 249 } 250 251 void 252 comcninit(cp) 253 struct consdev *cp; 254 { 255 int result; 256 257 #ifndef CONMODE 258 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 259 #endif 260 #ifndef CONSPEED 261 #define CONSPEED 38400 262 #endif 263 #ifndef CONADDR 264 #define CONADDR 0x3f8 265 #endif 266 267 result = comcnattach(comconstag, (IO_CONF_BASE + CONADDR), CONSPEED, COM_FREQ, CONMODE); 268 if (result) { 269 printf("initialising serial; got errornr %d\n", result); 270 panic("can't init serial console @%x", CONADDR); 271 }; 272 } 273 274 275 /* End of com_pioc.c */ 276