1 /* $NetBSD: amps.c,v 1.11 2005/12/11 12:16:05 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Mark Brinicombe of Causality Limited. 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 * Card driver and probe and attach functions to use generic 16550 com 39 * driver for the Atomwide multiport serial podule 40 */ 41 42 /* 43 * Thanks to Martin Coulson, Atomwide, for providing the hardware 44 */ 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: amps.c,v 1.11 2005/12/11 12:16:05 christos Exp $"); 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/ioctl.h> 52 #include <sys/select.h> 53 #include <sys/tty.h> 54 #include <sys/proc.h> 55 #include <sys/user.h> 56 #include <sys/conf.h> 57 #include <sys/file.h> 58 #include <sys/uio.h> 59 #include <sys/kernel.h> 60 #include <sys/syslog.h> 61 #include <sys/types.h> 62 #include <sys/device.h> 63 64 #include <machine/intr.h> 65 #include <machine/io.h> 66 #include <machine/bus.h> 67 #include <acorn32/podulebus/podulebus.h> 68 #include <acorn32/podulebus/ampsreg.h> 69 #include <dev/ic/comreg.h> 70 #include <dev/ic/comvar.h> 71 #include <dev/podulebus/podules.h> 72 73 #include "locators.h" 74 75 /* 76 * Atomwide Mulit-port serial podule device. 77 * 78 * This probes and attaches the top level multi-port serial device to the 79 * podulebus. It then configures the child com devices. 80 */ 81 82 /* 83 * Atomwide Multi-port serial card softc structure. 84 * 85 * Contains the device node, podule information and global information 86 * required by the driver such as the card version and the interrupt mask. 87 */ 88 89 struct amps_softc { 90 struct device sc_dev; /* device node */ 91 podule_t *sc_podule; /* Our podule info */ 92 int sc_podule_number; /* Our podule number */ 93 bus_space_tag_t sc_iot; /* Bus tag */ 94 }; 95 96 int amps_probe(struct device *, struct cfdata *, void *); 97 void amps_attach(struct device *, struct device *, void *); 98 99 CFATTACH_DECL(amps, sizeof(struct amps_softc), 100 amps_probe, amps_attach, NULL, NULL); 101 102 int amps_print(void *, const char *); 103 void amps_shutdown(void *); 104 105 /* 106 * Attach arguments for child devices. 107 * Pass the podule details, the parent softc and the channel 108 */ 109 110 struct amps_attach_args { 111 bus_space_tag_t aa_iot; /* bus space tag */ 112 unsigned int aa_base; /* base address for port */ 113 int aa_port; /* serial port number */ 114 podulebus_intr_handle_t aa_irq; /* IRQ number */ 115 }; 116 117 /* 118 * Define prototypes for custom bus space functions. 119 */ 120 121 /* Print function used during child config */ 122 123 int 124 amps_print(aux, name) 125 void *aux; 126 const char *name; 127 { 128 struct amps_attach_args *aa = aux; 129 130 if (!name) 131 aprint_normal(", port %d", aa->aa_port); 132 133 return(QUIET); 134 } 135 136 /* 137 * Card probe function 138 * 139 * Just match the manufacturer and podule ID's 140 */ 141 142 int 143 amps_probe(parent, cf, aux) 144 struct device *parent; 145 struct cfdata *cf; 146 void *aux; 147 { 148 struct podule_attach_args *pa = (void *)aux; 149 150 return (pa->pa_product == PODULE_ATOMWIDE_SERIAL); 151 } 152 153 /* 154 * Card attach function 155 * 156 * Identify the card version and configure any children. 157 * Install a shutdown handler to kill interrupts on shutdown 158 */ 159 160 void 161 amps_attach(parent, self, aux) 162 struct device *parent, *self; 163 void *aux; 164 { 165 struct amps_softc *sc = (void *)self; 166 struct podule_attach_args *pa = (void *)aux; 167 struct amps_attach_args aa; 168 169 /* Note the podule number and validate */ 170 171 if (pa->pa_podule_number == -1) 172 panic("Podule has disappeared !"); 173 174 sc->sc_podule_number = pa->pa_podule_number; 175 sc->sc_podule = pa->pa_podule; 176 podules[sc->sc_podule_number].attached = 1; 177 178 sc->sc_iot = pa->pa_iot; 179 180 /* Install a clean up handler to make sure IRQ's are disabled */ 181 /* if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL) 182 panic("%s: Cannot install shutdown handler", self->dv_xname);*/ 183 184 /* Set the interrupt info for this podule */ 185 186 /* sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/ /* XXX */ 187 /* sc->sc_podule->irq_mask = ;*/ 188 189 printf("\n"); 190 191 /* Configure the children */ 192 193 aa.aa_iot = sc->sc_iot; 194 aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET; 195 aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING; 196 aa.aa_irq = pa->pa_podule->interrupt; 197 for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) { 198 aa.aa_base -= AMPS_PORT_SPACING; 199 config_found(self, &aa, amps_print); 200 } 201 } 202 203 /* 204 * Card shutdown function 205 * 206 * Called via do_shutdown_hooks() during kernel shutdown. 207 * Clear the cards's interrupt mask to stop any podule interrupts. 208 */ 209 210 /*void 211 amps_shutdown(arg) 212 void *arg; 213 { 214 }*/ 215 216 /* 217 * Atomwide Multi-Port Serial probe and attach code for the com device. 218 * 219 * This provides a different pair of probe and attach functions 220 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card. 221 */ 222 223 struct com_amps_softc { 224 struct com_softc sc_com; /* real "com" softc */ 225 void *sc_ih; /* interrupt handler */ 226 struct evcnt sc_intrcnt; /* interrupt count */ 227 }; 228 229 /* Prototypes for functions */ 230 231 static int com_amps_probe __P((struct device *, struct cfdata *, void *)); 232 static void com_amps_attach __P((struct device *, struct device *, void *)); 233 234 /* device attach structure */ 235 236 CFATTACH_DECL(com_amps, sizeof(struct com_amps_softc), 237 com_amps_probe, com_amps_attach, NULL, NULL); 238 239 /* 240 * Controller probe function 241 * 242 * Map all the required I/O space for this channel, make sure interrupts 243 * are disabled and probe the bus. 244 */ 245 246 int 247 com_amps_probe(parent, cf, aux) 248 struct device *parent; 249 struct cfdata *cf; 250 void *aux; 251 { 252 bus_space_tag_t iot; 253 bus_space_handle_t ioh; 254 int iobase; 255 int rv = 1; 256 struct amps_attach_args *aa = aux; 257 258 iot = aa->aa_iot; 259 iobase = aa->aa_base; 260 261 /* if it's in use as console, it's there. */ 262 if (!com_is_console(iot, iobase, 0)) { 263 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) { 264 return 0; 265 } 266 /* 267 * We don't use the generic comprobe1() function as it 268 * is not good enough to identify which ports are present. 269 * 270 * Instead test for the presence of the scratch register 271 */ 272 273 bus_space_write_1(iot, ioh, com_scratch, 0x55); 274 bus_space_write_1(iot, ioh, com_ier, 0); 275 (void)bus_space_read_1(iot, ioh, com_data); 276 if (bus_space_read_1(iot, ioh, com_scratch) != 0x55) 277 rv = 0; 278 bus_space_write_1(iot, ioh, com_scratch, 0xaa); 279 bus_space_write_1(iot, ioh, com_ier, 0); 280 (void)bus_space_read_1(iot, ioh, com_data); 281 if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa) 282 rv = 0; 283 284 bus_space_unmap(iot, ioh, COM_NPORTS); 285 } 286 return (rv); 287 } 288 289 /* 290 * Controller attach function 291 * 292 * Map all the required I/O space for this port and attach the driver 293 * The generic attach will probe and attach any device. 294 * Install an interrupt handler and we are ready to rock. 295 */ 296 297 void 298 com_amps_attach(parent, self, aux) 299 struct device *parent, *self; 300 void *aux; 301 { 302 struct com_amps_softc *asc = (void *)self; 303 struct com_softc *sc = &asc->sc_com; 304 u_int iobase; 305 bus_space_tag_t iot; 306 struct amps_attach_args *aa = aux; 307 308 iot = sc->sc_iot = aa->aa_iot; 309 iobase = sc->sc_iobase = aa->aa_base; 310 311 if (!com_is_console(iot, iobase, &sc->sc_ioh) 312 && bus_space_map(iot, iobase, COM_NPORTS, 0, &sc->sc_ioh)) 313 panic("comattach: io mapping failed"); 314 315 sc->sc_frequency = AMPS_FREQ; 316 com_attach_subr(sc); 317 318 evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 319 self->dv_xname, "intr"); 320 asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr, 321 sc, &asc->sc_intrcnt); 322 } 323