1 /* $NetBSD: amps.c,v 1.16 2009/03/14 21:04:01 dsl 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Card driver and probe and attach functions to use generic 16550 com 32 * driver for the Atomwide multiport serial podule 33 */ 34 35 /* 36 * Thanks to Martin Coulson, Atomwide, for providing the hardware 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: amps.c,v 1.16 2009/03/14 21:04:01 dsl Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/ioctl.h> 45 #include <sys/select.h> 46 #include <sys/tty.h> 47 #include <sys/proc.h> 48 #include <sys/user.h> 49 #include <sys/conf.h> 50 #include <sys/file.h> 51 #include <sys/uio.h> 52 #include <sys/kernel.h> 53 #include <sys/syslog.h> 54 #include <sys/types.h> 55 #include <sys/device.h> 56 57 #include <machine/intr.h> 58 #include <machine/io.h> 59 #include <machine/bus.h> 60 #include <acorn32/podulebus/podulebus.h> 61 #include <acorn32/podulebus/ampsreg.h> 62 #include <dev/ic/comreg.h> 63 #include <dev/ic/comvar.h> 64 #include <dev/podulebus/podules.h> 65 66 #include "locators.h" 67 68 /* 69 * Atomwide Mulit-port serial podule device. 70 * 71 * This probes and attaches the top level multi-port serial device to the 72 * podulebus. It then configures the child com devices. 73 */ 74 75 /* 76 * Atomwide Multi-port serial card softc structure. 77 * 78 * Contains the device node, podule information and global information 79 * required by the driver such as the card version and the interrupt mask. 80 */ 81 82 struct amps_softc { 83 struct device sc_dev; /* device node */ 84 podule_t *sc_podule; /* Our podule info */ 85 int sc_podule_number; /* Our podule number */ 86 bus_space_tag_t sc_iot; /* Bus tag */ 87 }; 88 89 int amps_probe(struct device *, struct cfdata *, void *); 90 void amps_attach(struct device *, struct device *, void *); 91 92 CFATTACH_DECL(amps, sizeof(struct amps_softc), 93 amps_probe, amps_attach, NULL, NULL); 94 95 int amps_print(void *, const char *); 96 void amps_shutdown(void *); 97 98 /* 99 * Attach arguments for child devices. 100 * Pass the podule details, the parent softc and the channel 101 */ 102 103 struct amps_attach_args { 104 bus_space_tag_t aa_iot; /* bus space tag */ 105 unsigned int aa_base; /* base address for port */ 106 int aa_port; /* serial port number */ 107 podulebus_intr_handle_t aa_irq; /* IRQ number */ 108 }; 109 110 /* 111 * Define prototypes for custom bus space functions. 112 */ 113 114 /* Print function used during child config */ 115 116 int 117 amps_print(void *aux, const char *name) 118 { 119 struct amps_attach_args *aa = aux; 120 121 if (!name) 122 aprint_normal(", port %d", aa->aa_port); 123 124 return(QUIET); 125 } 126 127 /* 128 * Card probe function 129 * 130 * Just match the manufacturer and podule ID's 131 */ 132 133 int 134 amps_probe(struct device *parent, struct cfdata *cf, void *aux) 135 { 136 struct podule_attach_args *pa = (void *)aux; 137 138 return (pa->pa_product == PODULE_ATOMWIDE_SERIAL); 139 } 140 141 /* 142 * Card attach function 143 * 144 * Identify the card version and configure any children. 145 * Install a shutdown handler to kill interrupts on shutdown 146 */ 147 148 void 149 amps_attach(struct device *parent, struct device *self, void *aux) 150 { 151 struct amps_softc *sc = (void *)self; 152 struct podule_attach_args *pa = (void *)aux; 153 struct amps_attach_args aa; 154 155 /* Note the podule number and validate */ 156 157 if (pa->pa_podule_number == -1) 158 panic("Podule has disappeared !"); 159 160 sc->sc_podule_number = pa->pa_podule_number; 161 sc->sc_podule = pa->pa_podule; 162 podules[sc->sc_podule_number].attached = 1; 163 164 sc->sc_iot = pa->pa_iot; 165 166 /* Install a clean up handler to make sure IRQ's are disabled */ 167 /* if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL) 168 panic("%s: Cannot install shutdown handler", self->dv_xname);*/ 169 170 /* Set the interrupt info for this podule */ 171 172 /* sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/ /* XXX */ 173 /* sc->sc_podule->irq_mask = ;*/ 174 175 printf("\n"); 176 177 /* Configure the children */ 178 179 aa.aa_iot = sc->sc_iot; 180 aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET; 181 aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING; 182 aa.aa_irq = pa->pa_podule->interrupt; 183 for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) { 184 aa.aa_base -= AMPS_PORT_SPACING; 185 config_found(self, &aa, amps_print); 186 } 187 } 188 189 /* 190 * Card shutdown function 191 * 192 * Called via do_shutdown_hooks() during kernel shutdown. 193 * Clear the cards's interrupt mask to stop any podule interrupts. 194 */ 195 196 /*void 197 amps_shutdown(void *arg) 198 { 199 }*/ 200 201 /* 202 * Atomwide Multi-Port Serial probe and attach code for the com device. 203 * 204 * This provides a different pair of probe and attach functions 205 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card. 206 */ 207 208 struct com_amps_softc { 209 struct com_softc sc_com; /* real "com" softc */ 210 void *sc_ih; /* interrupt handler */ 211 struct evcnt sc_intrcnt; /* interrupt count */ 212 }; 213 214 /* Prototypes for functions */ 215 216 static int com_amps_probe (device_t, cfdata_t , void *); 217 static void com_amps_attach (device_t, device_t, void *); 218 219 /* device attach structure */ 220 221 CFATTACH_DECL_NEW(com_amps, sizeof(struct com_amps_softc), 222 com_amps_probe, com_amps_attach, NULL, NULL); 223 224 /* 225 * Controller probe function 226 * 227 * Map all the required I/O space for this channel, make sure interrupts 228 * are disabled and probe the bus. 229 */ 230 231 int 232 com_amps_probe(device_t parent, cfdata_t cf, void *aux) 233 { 234 bus_space_tag_t iot; 235 bus_space_handle_t ioh; 236 int iobase; 237 int rv = 1; 238 struct amps_attach_args *aa = aux; 239 240 iot = aa->aa_iot; 241 iobase = aa->aa_base; 242 243 /* if it's in use as console, it's there. */ 244 if (!com_is_console(iot, iobase, 0)) { 245 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) { 246 return 0; 247 } 248 /* 249 * We don't use the generic comprobe1() function as it 250 * is not good enough to identify which ports are present. 251 * 252 * Instead test for the presence of the scratch register 253 */ 254 255 bus_space_write_1(iot, ioh, com_scratch, 0x55); 256 bus_space_write_1(iot, ioh, com_ier, 0); 257 (void)bus_space_read_1(iot, ioh, com_data); 258 if (bus_space_read_1(iot, ioh, com_scratch) != 0x55) 259 rv = 0; 260 bus_space_write_1(iot, ioh, com_scratch, 0xaa); 261 bus_space_write_1(iot, ioh, com_ier, 0); 262 (void)bus_space_read_1(iot, ioh, com_data); 263 if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa) 264 rv = 0; 265 266 bus_space_unmap(iot, ioh, COM_NPORTS); 267 } 268 return (rv); 269 } 270 271 /* 272 * Controller attach function 273 * 274 * Map all the required I/O space for this port and attach the driver 275 * The generic attach will probe and attach any device. 276 * Install an interrupt handler and we are ready to rock. 277 */ 278 279 void 280 com_amps_attach(device_t parent, device_t self, void *aux) 281 { 282 struct com_amps_softc *asc = device_private(self); 283 struct com_softc *sc = &asc->sc_com; 284 u_int iobase; 285 bus_space_tag_t iot; 286 bus_space_handle_t ioh; 287 struct amps_attach_args *aa = aux; 288 289 sc->sc_dev = self; 290 iot = aa->aa_iot; 291 iobase = aa->aa_base; 292 293 if (!com_is_console(iot, iobase, &ioh) 294 && bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) 295 panic("comattach: io mapping failed"); 296 COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase); 297 298 sc->sc_frequency = AMPS_FREQ; 299 com_attach_subr(sc); 300 301 evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 302 device_xname(self), "intr"); 303 asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr, 304 sc, &asc->sc_intrcnt); 305 } 306