1 1.45 riastrad /* $NetBSD: pcmcom.c,v 1.45 2023/05/10 00:12:12 riastradh Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.17 mycroft * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by RedBack Networks, Inc. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.1 thorpej /* 33 1.1 thorpej * Device driver for multi-port PCMCIA serial cards, written by 34 1.1 thorpej * Jason R. Thorpe for RedBack Networks, Inc. 35 1.1 thorpej * 36 1.1 thorpej * Most of these cards are simply multiple UARTs sharing a single interrupt 37 1.1 thorpej * line, and rely on the fact that PCMCIA level-triggered interrupts can 38 1.1 thorpej * be shared. There are no special interrupt registers on them, as there 39 1.1 thorpej * are on most ISA multi-port serial cards. 40 1.1 thorpej * 41 1.1 thorpej * If there are other cards that have interrupt registers, they should not 42 1.1 thorpej * be glued into this driver. Rather, separate drivers should be written 43 1.1 thorpej * for those devices, as we have in the ISA multi-port serial card case. 44 1.1 thorpej */ 45 1.7 lukem 46 1.7 lukem #include <sys/cdefs.h> 47 1.45 riastrad __KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.45 2023/05/10 00:12:12 riastradh Exp $"); 48 1.1 thorpej 49 1.1 thorpej #include <sys/param.h> 50 1.1 thorpej #include <sys/systm.h> 51 1.22 perry #include <sys/device.h> 52 1.1 thorpej #include <sys/termios.h> 53 1.1 thorpej 54 1.31 ad #include <sys/bus.h> 55 1.31 ad #include <sys/intr.h> 56 1.1 thorpej 57 1.1 thorpej #include <dev/ic/comreg.h> 58 1.22 perry #include <dev/ic/comvar.h> 59 1.1 thorpej 60 1.1 thorpej #include <dev/pcmcia/pcmciavar.h> 61 1.1 thorpej #include <dev/pcmcia/pcmciareg.h> 62 1.1 thorpej #include <dev/pcmcia/pcmciadevs.h> 63 1.1 thorpej 64 1.1 thorpej #include "com.h" 65 1.1 thorpej #include "pcmcom.h" 66 1.1 thorpej 67 1.1 thorpej #include "locators.h" 68 1.1 thorpej 69 1.1 thorpej struct pcmcom_softc { 70 1.39 dyoung device_t sc_dev; /* generic device glue */ 71 1.1 thorpej 72 1.1 thorpej struct pcmcia_function *sc_pf; /* our PCMCIA function */ 73 1.1 thorpej void *sc_ih; /* interrupt handle */ 74 1.1 thorpej int sc_enabled_count; /* enabled count */ 75 1.1 thorpej 76 1.19 mycroft #define NSLAVES 8 77 1.37 cegger device_t sc_slaves[NSLAVES]; /* slave info */ 78 1.1 thorpej int sc_nslaves; /* slave count */ 79 1.19 mycroft 80 1.19 mycroft int sc_state; 81 1.19 mycroft #define PCMCOM_ATTACHED 3 82 1.1 thorpej }; 83 1.1 thorpej 84 1.1 thorpej struct pcmcom_attach_args { 85 1.1 thorpej bus_space_tag_t pca_iot; /* I/O tag */ 86 1.1 thorpej bus_space_handle_t pca_ioh; /* I/O handle */ 87 1.1 thorpej int pca_slave; /* slave # */ 88 1.1 thorpej }; 89 1.1 thorpej 90 1.37 cegger int pcmcom_match(device_t, cfdata_t, void *); 91 1.21 perry int pcmcom_validate_config(struct pcmcia_config_entry *); 92 1.37 cegger void pcmcom_attach(device_t, device_t, void *); 93 1.37 cegger int pcmcom_detach(device_t, int); 94 1.38 dyoung void pcmcom_childdet(device_t, device_t); 95 1.1 thorpej 96 1.40 chs CFATTACH_DECL_NEW(pcmcom, sizeof(struct pcmcom_softc), 97 1.38 dyoung pcmcom_match, pcmcom_attach, pcmcom_detach, NULL); 98 1.1 thorpej 99 1.19 mycroft const struct pcmcia_product pcmcom_products[] = { 100 1.19 mycroft { PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232, 101 1.19 mycroft PCMCIA_CIS_INVALID }, 102 1.29 christos #if 0 /* does not work */ 103 1.29 christos { PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232_A, 104 1.29 christos PCMCIA_CIS_INVALID }, 105 1.29 christos #endif 106 1.1 thorpej }; 107 1.38 dyoung const size_t pcmcom_nproducts = __arraycount(pcmcom_products); 108 1.1 thorpej 109 1.21 perry int pcmcom_print(void *, const char *); 110 1.1 thorpej 111 1.21 perry int pcmcom_enable(struct pcmcom_softc *); 112 1.21 perry void pcmcom_disable(struct pcmcom_softc *); 113 1.1 thorpej 114 1.21 perry int pcmcom_intr(void *); 115 1.1 thorpej 116 1.1 thorpej int 117 1.38 dyoung pcmcom_match(device_t parent, cfdata_t cf, void *aux) 118 1.1 thorpej { 119 1.1 thorpej struct pcmcia_attach_args *pa = aux; 120 1.1 thorpej 121 1.17 mycroft if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts, 122 1.17 mycroft sizeof(pcmcom_products[0]), NULL)) 123 1.17 mycroft return (2); /* beat com_pcmcia */ 124 1.1 thorpej return (0); 125 1.1 thorpej } 126 1.1 thorpej 127 1.19 mycroft int 128 1.35 dsl pcmcom_validate_config(struct pcmcia_config_entry *cfe) 129 1.19 mycroft { 130 1.19 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO || 131 1.19 mycroft cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES) 132 1.19 mycroft return (EINVAL); 133 1.19 mycroft return (0); 134 1.19 mycroft } 135 1.19 mycroft 136 1.1 thorpej void 137 1.37 cegger pcmcom_attach(device_t parent, device_t self, void *aux) 138 1.1 thorpej { 139 1.38 dyoung struct pcmcom_softc *sc = device_private(self); 140 1.1 thorpej struct pcmcia_attach_args *pa = aux; 141 1.1 thorpej struct pcmcia_config_entry *cfe; 142 1.19 mycroft int slave; 143 1.19 mycroft int error; 144 1.23 drochner int locs[PCMCOMCF_NLOCS]; 145 1.1 thorpej 146 1.39 dyoung sc->sc_dev = self; 147 1.39 dyoung 148 1.1 thorpej sc->sc_pf = pa->pf; 149 1.1 thorpej 150 1.19 mycroft error = pcmcia_function_configure(pa->pf, pcmcom_validate_config); 151 1.19 mycroft if (error) { 152 1.38 dyoung aprint_error_dev(self, "configure failed, error=%d\n", error); 153 1.1 thorpej return; 154 1.1 thorpej } 155 1.1 thorpej 156 1.19 mycroft cfe = pa->pf->cfe; 157 1.19 mycroft sc->sc_nslaves = cfe->num_iospace; 158 1.1 thorpej 159 1.19 mycroft error = pcmcom_enable(sc); 160 1.19 mycroft if (error) 161 1.19 mycroft goto fail; 162 1.1 thorpej 163 1.1 thorpej /* Attach the children. */ 164 1.1 thorpej for (slave = 0; slave < sc->sc_nslaves; slave++) { 165 1.19 mycroft struct pcmcom_attach_args pca; 166 1.1 thorpej 167 1.33 cegger printf("%s: slave %d\n", device_xname(self), slave); 168 1.1 thorpej 169 1.19 mycroft pca.pca_iot = cfe->iospace[slave].handle.iot; 170 1.19 mycroft pca.pca_ioh = cfe->iospace[slave].handle.ioh; 171 1.19 mycroft pca.pca_slave = slave; 172 1.1 thorpej 173 1.23 drochner locs[PCMCOMCF_SLAVE] = slave; 174 1.20 drochner 175 1.42 thorpej sc->sc_slaves[slave] = 176 1.42 thorpej config_found(sc->sc_dev, &pca, pcmcom_print, 177 1.43 thorpej CFARGS(.submatch = config_stdsubmatch, 178 1.43 thorpej .locators = locs)); 179 1.1 thorpej } 180 1.1 thorpej 181 1.19 mycroft pcmcom_disable(sc); 182 1.19 mycroft sc->sc_state = PCMCOM_ATTACHED; 183 1.19 mycroft return; 184 1.1 thorpej 185 1.19 mycroft fail: 186 1.19 mycroft pcmcia_function_unconfigure(pa->pf); 187 1.1 thorpej } 188 1.1 thorpej 189 1.38 dyoung void 190 1.38 dyoung pcmcom_childdet(device_t self, device_t child) 191 1.38 dyoung { 192 1.38 dyoung struct pcmcom_softc *sc = device_private(self); 193 1.38 dyoung int slave; 194 1.38 dyoung 195 1.38 dyoung for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) { 196 1.38 dyoung if (sc->sc_slaves[slave] == child) 197 1.38 dyoung sc->sc_slaves[slave] = NULL; 198 1.38 dyoung } 199 1.38 dyoung } 200 1.38 dyoung 201 1.1 thorpej int 202 1.37 cegger pcmcom_detach(device_t self, int flags) 203 1.3 thorpej { 204 1.38 dyoung struct pcmcom_softc *sc = device_private(self); 205 1.45 riastrad int error; 206 1.45 riastrad 207 1.45 riastrad error = config_detach_children(self, flags); 208 1.45 riastrad if (error) 209 1.45 riastrad return error; 210 1.3 thorpej 211 1.19 mycroft if (sc->sc_state != PCMCOM_ATTACHED) 212 1.45 riastrad return 0; 213 1.19 mycroft pcmcia_function_unconfigure(sc->sc_pf); 214 1.45 riastrad return 0; 215 1.3 thorpej } 216 1.3 thorpej 217 1.3 thorpej int 218 1.35 dsl pcmcom_print(void *aux, const char *pnp) 219 1.1 thorpej { 220 1.1 thorpej struct pcmcom_attach_args *pca = aux; 221 1.1 thorpej 222 1.1 thorpej /* only com's can attach to pcmcom's; easy... */ 223 1.1 thorpej if (pnp) 224 1.14 thorpej aprint_normal("com at %s", pnp); 225 1.1 thorpej 226 1.14 thorpej aprint_normal(" slave %d", pca->pca_slave); 227 1.1 thorpej 228 1.1 thorpej return (UNCONF); 229 1.1 thorpej } 230 1.1 thorpej 231 1.1 thorpej int 232 1.35 dsl pcmcom_intr(void *arg) 233 1.1 thorpej { 234 1.1 thorpej #if NCOM > 0 235 1.1 thorpej struct pcmcom_softc *sc = arg; 236 1.1 thorpej int i, rval = 0; 237 1.1 thorpej 238 1.1 thorpej if (sc->sc_enabled_count == 0) 239 1.1 thorpej return (0); 240 1.1 thorpej 241 1.1 thorpej for (i = 0; i < sc->sc_nslaves; i++) { 242 1.19 mycroft if (sc->sc_slaves[i]) 243 1.19 mycroft rval |= comintr(sc->sc_slaves[i]); 244 1.1 thorpej } 245 1.1 thorpej 246 1.1 thorpej return (rval); 247 1.1 thorpej #else 248 1.1 thorpej return (0); 249 1.1 thorpej #endif /* NCOM > 0 */ 250 1.1 thorpej } 251 1.1 thorpej 252 1.1 thorpej int 253 1.35 dsl pcmcom_enable(struct pcmcom_softc *sc) 254 1.1 thorpej { 255 1.16 mycroft int error; 256 1.1 thorpej 257 1.16 mycroft if (sc->sc_enabled_count++ != 0) 258 1.1 thorpej return (0); 259 1.1 thorpej 260 1.1 thorpej /* Establish the interrupt. */ 261 1.1 thorpej sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL, 262 1.1 thorpej pcmcom_intr, sc); 263 1.19 mycroft if (!sc->sc_ih) 264 1.19 mycroft return (EIO); 265 1.1 thorpej 266 1.16 mycroft error = pcmcia_function_enable(sc->sc_pf); 267 1.19 mycroft if (error) { 268 1.16 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 269 1.19 mycroft sc->sc_ih = 0; 270 1.19 mycroft } 271 1.16 mycroft 272 1.16 mycroft return (error); 273 1.1 thorpej } 274 1.1 thorpej 275 1.1 thorpej void 276 1.35 dsl pcmcom_disable(struct pcmcom_softc *sc) 277 1.1 thorpej { 278 1.1 thorpej 279 1.16 mycroft if (--sc->sc_enabled_count != 0) 280 1.1 thorpej return; 281 1.1 thorpej 282 1.1 thorpej pcmcia_function_disable(sc->sc_pf); 283 1.1 thorpej pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 284 1.19 mycroft sc->sc_ih = 0; 285 1.1 thorpej } 286 1.1 thorpej 287 1.1 thorpej /****** Here begins the com attachment code. ******/ 288 1.1 thorpej 289 1.1 thorpej #if NCOM_PCMCOM > 0 290 1.32 cube int com_pcmcom_match(device_t, cfdata_t , void *); 291 1.32 cube void com_pcmcom_attach(device_t, device_t, void *); 292 1.1 thorpej 293 1.1 thorpej /* No pcmcom-specific goo in the softc; it's all in the parent. */ 294 1.40 chs CFATTACH_DECL_NEW(com_pcmcom, sizeof(struct com_softc), 295 1.38 dyoung com_pcmcom_match, com_pcmcom_attach, com_detach, NULL); 296 1.1 thorpej 297 1.21 perry int com_pcmcom_enable(struct com_softc *); 298 1.21 perry void com_pcmcom_disable(struct com_softc *); 299 1.1 thorpej 300 1.1 thorpej int 301 1.32 cube com_pcmcom_match(device_t parent, cfdata_t cf, void *aux) 302 1.1 thorpej { 303 1.1 thorpej 304 1.1 thorpej /* Device is always present. */ 305 1.1 thorpej return (1); 306 1.1 thorpej } 307 1.1 thorpej 308 1.1 thorpej void 309 1.32 cube com_pcmcom_attach(device_t parent, device_t self, void *aux) 310 1.1 thorpej { 311 1.32 cube struct com_softc *sc = device_private(self); 312 1.1 thorpej struct pcmcom_attach_args *pca = aux; 313 1.1 thorpej 314 1.32 cube sc->sc_dev = self; 315 1.41 thorpej com_init_regs(&sc->sc_regs, pca->pca_iot, pca->pca_ioh, -1); 316 1.1 thorpej sc->enabled = 1; 317 1.1 thorpej 318 1.1 thorpej sc->sc_frequency = COM_FREQ; 319 1.1 thorpej 320 1.1 thorpej sc->enable = com_pcmcom_enable; 321 1.1 thorpej sc->disable = com_pcmcom_disable; 322 1.1 thorpej 323 1.1 thorpej com_attach_subr(sc); 324 1.1 thorpej 325 1.1 thorpej sc->enabled = 0; 326 1.1 thorpej } 327 1.1 thorpej 328 1.1 thorpej int 329 1.32 cube com_pcmcom_enable(struct com_softc *sc) 330 1.1 thorpej { 331 1.38 dyoung return pcmcom_enable(device_private(device_parent(sc->sc_dev))); 332 1.1 thorpej } 333 1.1 thorpej 334 1.1 thorpej void 335 1.35 dsl com_pcmcom_disable(struct com_softc *sc) 336 1.1 thorpej { 337 1.1 thorpej 338 1.32 cube pcmcom_disable(device_private(device_parent(sc->sc_dev))); 339 1.1 thorpej } 340 1.1 thorpej #endif /* NCOM_PCMCOM > 0 */ 341