puc.c revision 102895
1/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 2 3/*- 4 * Copyright (c) 2002 JF Hay. All rights reserved. 5 * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * Copyright (c) 1996, 1998, 1999 31 * Christopher G. Demetriou. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by Christopher G. Demetriou 44 * for the NetBSD Project. 45 * 4. The name of the author may not be used to endorse or promote products 46 * derived from this software without specific prior written permission 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60#include <sys/cdefs.h> 61__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 102895 2002-09-03 11:22:13Z phk $"); 62 63/* 64 * PCI "universal" communication card device driver, glues com, lpt, 65 * and similar ports to PCI via bridge chip often much larger than 66 * the devices being glued. 67 * 68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 69 * sys/dev/pci/pciide.c, revision 1.6). 70 * 71 * These devices could be (and some times are) described as 72 * communications/{serial,parallel}, etc. devices with known 73 * programming interfaces, but those programming interfaces (in 74 * particular the BAR assignments for devices, etc.) in fact are not 75 * particularly well defined. 76 * 77 * After I/we have seen more of these devices, it may be possible 78 * to generalize some of these bits. In particular, devices which 79 * describe themselves as communications/serial/16[45]50, and 80 * communications/parallel/??? might be attached via direct 81 * 'com' and 'lpt' attachments to pci. 82 */ 83 84#include "opt_puc.h" 85 86#include <sys/param.h> 87#include <sys/systm.h> 88#include <sys/kernel.h> 89#include <sys/bus.h> 90#include <sys/conf.h> 91#include <sys/malloc.h> 92 93#include <machine/bus.h> 94#include <machine/resource.h> 95#include <sys/rman.h> 96 97#include <dev/pci/pcireg.h> 98#include <dev/pci/pcivar.h> 99 100#define PUC_ENTRAILS 1 101#include <dev/puc/pucvar.h> 102 103struct puc_device { 104 struct resource_list resources; 105 u_int serialfreq; 106}; 107 108static void puc_intr(void *arg); 109 110static int puc_find_free_unit(char *); 111#ifdef PUC_DEBUG 112static void puc_print_resource_list(struct resource_list *); 113#endif 114 115devclass_t puc_devclass; 116 117static int 118puc_port_bar_index(struct puc_softc *sc, int bar) 119{ 120 int i; 121 122 for (i = 0; i < PUC_MAX_BAR; i += 1) { 123 if (!sc->sc_bar_mappings[i].used) 124 break; 125 if (sc->sc_bar_mappings[i].bar == bar) 126 return (i); 127 } 128 sc->sc_bar_mappings[i].bar = bar; 129 sc->sc_bar_mappings[i].used = 1; 130 return (i); 131} 132 133int 134puc_attach(device_t dev, const struct puc_device_description *desc) 135{ 136 char *typestr; 137 int bidx, childunit, i, irq_setup, rid; 138 struct puc_softc *sc; 139 struct puc_device *pdev; 140 struct resource *res; 141 struct resource_list_entry *rle; 142 143 sc = (struct puc_softc *)device_get_softc(dev); 144 bzero(sc, sizeof(*sc)); 145 sc->sc_desc = desc; 146 if (sc->sc_desc == NULL) 147 return (ENXIO); 148 149#ifdef PUC_DEBUG 150 bootverbose = 1; 151 152 printf("puc: name: %s\n", sc->sc_desc->name); 153#endif 154 rid = 0; 155 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 156 RF_ACTIVE | RF_SHAREABLE); 157 if (!res) 158 return (ENXIO); 159 160 sc->irqres = res; 161 sc->irqrid = rid; 162 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 163 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 164 if (irq_setup != 0) 165 return (ENXIO); 166 167 rid = 0; 168 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 169 if (i > 0 && rid == sc->sc_desc->ports[i].bar) 170 sc->barmuxed = 1; 171 rid = sc->sc_desc->ports[i].bar; 172 bidx = puc_port_bar_index(sc, rid); 173 174 if (sc->sc_bar_mappings[bidx].res != NULL) 175 continue; 176 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 177 0ul, ~0ul, 1, RF_ACTIVE); 178 if (res == NULL) { 179 printf("could not get resource\n"); 180 continue; 181 } 182 sc->sc_bar_mappings[bidx].res = res; 183#ifdef PUC_DEBUG 184 printf("port rid %d bst %x, start %x, end %x\n", rid, 185 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 186 (u_int)rman_get_end(res)); 187#endif 188 } 189 190 if (desc->init != NULL) { 191 i = desc->init(sc); 192 if (i != 0) 193 return (i); 194 } 195 196 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 197 rid = sc->sc_desc->ports[i].bar; 198 bidx = puc_port_bar_index(sc, rid); 199 if (sc->sc_bar_mappings[bidx].res == NULL) 200 continue; 201 202 switch (sc->sc_desc->ports[i].type) { 203 case PUC_PORT_TYPE_COM: 204 typestr = "sio"; 205 break; 206 default: 207 continue; 208 } 209 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 210 M_NOWAIT | M_ZERO); 211 if (!pdev) 212 continue; 213 resource_list_init(&pdev->resources); 214 215 /* First fake up an IRQ resource. */ 216 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 217 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 218 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 219 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 220 rle->res = sc->irqres; 221 222 /* Now fake an IOPORT resource */ 223 res = sc->sc_bar_mappings[bidx].res; 224 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0, 225 rman_get_start(res) + sc->sc_desc->ports[i].offset, 226 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 227 8); 228 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0); 229 230 if (sc->barmuxed == 0) { 231 rle->res = sc->sc_bar_mappings[bidx].res; 232 } else { 233 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 234 M_WAITOK | M_ZERO); 235 if (rle->res == NULL) { 236 free(pdev, M_DEVBUF); 237 return (ENOMEM); 238 } 239 240 rle->res->r_start = rman_get_start(res) + 241 sc->sc_desc->ports[i].offset; 242 rle->res->r_end = rle->res->r_start + 8 - 1; 243 rle->res->r_bustag = rman_get_bustag(res); 244 bus_space_subregion(rle->res->r_bustag, 245 rman_get_bushandle(res), 246 sc->sc_desc->ports[i].offset, 8, 247 &rle->res->r_bushandle); 248 } 249 250 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 251 252 childunit = puc_find_free_unit(typestr); 253 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 254 if (sc->sc_ports[i].dev == NULL) { 255 if (sc->barmuxed) { 256 bus_space_unmap(rman_get_bustag(rle->res), 257 rman_get_bushandle(rle->res), 258 8); 259 free(rle->res, M_DEVBUF); 260 free(pdev, M_DEVBUF); 261 } 262 continue; 263 } 264 device_set_ivars(sc->sc_ports[i].dev, pdev); 265 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 266 if (!bootverbose) 267 device_quiet(sc->sc_ports[i].dev); 268#ifdef PUC_DEBUG 269 printf("puc: type %d, bar %x, offset %x\n", 270 sc->sc_desc->ports[i].type, 271 sc->sc_desc->ports[i].bar, 272 sc->sc_desc->ports[i].offset); 273 puc_print_resource_list(&pdev->resources); 274#endif 275 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 276 if (sc->barmuxed) { 277 bus_space_unmap(rman_get_bustag(rle->res), 278 rman_get_bushandle(rle->res), 279 8); 280 free(rle->res, M_DEVBUF); 281 free(pdev, M_DEVBUF); 282 } 283 } 284 } 285 286#ifdef PUC_DEBUG 287 bootverbose = 0; 288#endif 289 return (0); 290} 291 292/* 293 * This is just an brute force interrupt handler. It just calls all the 294 * registered handlers sequencially. 295 * 296 * Later on we should maybe have a different handler for boards that can 297 * tell us which device generated the interrupt. 298 */ 299static void 300puc_intr(void *arg) 301{ 302 int i; 303 struct puc_softc *sc; 304 305 sc = (struct puc_softc *)arg; 306 for (i = 0; i < PUC_MAX_PORTS; i++) 307 if (sc->sc_ports[i].ihand != NULL) 308 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 309} 310 311const struct puc_device_description * 312puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 313 uint32_t sprod) 314{ 315 int i; 316 317#define checkreg(val, index) \ 318 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 319 320 for (i = 0; puc_devices[i].name != NULL; i++) { 321 if (checkreg(vend, PUC_REG_VEND) && 322 checkreg(prod, PUC_REG_PROD) && 323 checkreg(svend, PUC_REG_SVEND) && 324 checkreg(sprod, PUC_REG_SPROD)) 325 return (&puc_devices[i]); 326 } 327 328#undef checkreg 329 330 return (NULL); 331} 332 333static int 334puc_find_free_unit(char *name) 335{ 336 devclass_t dc; 337 int start; 338 int unit; 339 340 unit = 0; 341 start = 0; 342 while (resource_int_value(name, unit, "port", &start) == 0 && 343 start > 0) 344 unit++; 345 dc = devclass_find(name); 346 if (dc == NULL) 347 return (-1); 348 while (devclass_get_device(dc, unit)) 349 unit++; 350#ifdef PUC_DEBUG 351 printf("puc: Using %s%d\n", name, unit); 352#endif 353 return (unit); 354} 355 356#ifdef PUC_DEBUG 357static void 358puc_print_resource_list(struct resource_list *rl) 359{ 360#if 0 361 struct resource_list_entry *rle; 362 363 printf("print_resource_list: rl %p\n", rl); 364 SLIST_FOREACH(rle, rl, link) 365 printf(" type %x, rid %x start %x end %x count %x\n", 366 rle->type, rle->rid, rle->start, rle->end, rle->count); 367 printf("print_resource_list: end.\n"); 368#endif 369} 370#endif 371 372struct resource * 373puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 374 u_long start, u_long end, u_long count, u_int flags) 375{ 376 struct puc_device *pdev; 377 struct resource *retval; 378 struct resource_list *rl; 379 struct resource_list_entry *rle; 380 381 pdev = device_get_ivars(child); 382 rl = &pdev->resources; 383 384#ifdef PUC_DEBUG 385 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 386 pdev, type, *rid); 387 puc_print_resource_list(rl); 388#endif 389 retval = NULL; 390 rle = resource_list_find(rl, type, *rid); 391 if (rle) { 392 start = rle->start; 393 end = rle->end; 394 count = rle->count; 395#ifdef PUC_DEBUG 396 printf("found rle, %lx, %lx, %lx\n", start, end, count); 397#endif 398 retval = rle->res; 399 } else 400 printf("oops rle is gone\n"); 401 402 return (retval); 403} 404 405int 406puc_release_resource(device_t dev, device_t child, int type, int rid, 407 struct resource *res) 408{ 409 return (0); 410} 411 412int 413puc_get_resource(device_t dev, device_t child, int type, int rid, 414 u_long *startp, u_long *countp) 415{ 416 struct puc_device *pdev; 417 struct resource_list *rl; 418 struct resource_list_entry *rle; 419 420 pdev = device_get_ivars(child); 421 rl = &pdev->resources; 422 423#ifdef PUC_DEBUG 424 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 425 type, rid); 426 puc_print_resource_list(rl); 427#endif 428 rle = resource_list_find(rl, type, rid); 429 if (rle) { 430#ifdef PUC_DEBUG 431 printf("found rle %p,", rle); 432#endif 433 if (startp != NULL) 434 *startp = rle->start; 435 if (countp != NULL) 436 *countp = rle->count; 437#ifdef PUC_DEBUG 438 printf(" %lx, %lx\n", rle->start, rle->count); 439#endif 440 return (0); 441 } else 442 printf("oops rle is gone\n"); 443 return (ENXIO); 444} 445 446int 447puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 448 void (*ihand)(void *), void *arg, void **cookiep) 449{ 450 int i; 451 struct puc_softc *sc; 452 453 if (flags & INTR_FAST) 454 return (ENXIO); 455 sc = (struct puc_softc *)device_get_softc(dev); 456 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 457 if (sc->sc_ports[i].dev == child) { 458 if (sc->sc_ports[i].ihand != 0) 459 return (ENXIO); 460 sc->sc_ports[i].ihand = ihand; 461 sc->sc_ports[i].ihandarg = arg; 462 *cookiep = arg; 463 return (0); 464 } 465 } 466 return (ENXIO); 467} 468 469int 470puc_teardown_intr(device_t dev, device_t child, struct resource *r, 471 void *cookie) 472{ 473 int i; 474 struct puc_softc *sc; 475 476 sc = (struct puc_softc *)device_get_softc(dev); 477 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 478 if (sc->sc_ports[i].dev == child) { 479 sc->sc_ports[i].ihand = NULL; 480 sc->sc_ports[i].ihandarg = NULL; 481 return (0); 482 } 483 } 484 return (ENXIO); 485} 486 487int 488puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 489{ 490 struct puc_device *pdev; 491 492 pdev = device_get_ivars(child); 493 if (pdev == NULL) 494 return (ENOENT); 495 496 switch(index) { 497 case PUC_IVAR_FREQ: 498 *result = pdev->serialfreq; 499 break; 500 default: 501 return (ENOENT); 502 } 503 return (0); 504} 505