puc.c revision 102751
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 102751 2002-09-01 01:59:38Z jmallett $"); 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 115static int 116puc_port_bar_index(struct puc_softc *sc, int bar) 117{ 118 int i; 119 120 for (i = 0; i < PUC_MAX_BAR; i += 1) { 121 if (!sc->sc_bar_mappings[i].used) 122 break; 123 if (sc->sc_bar_mappings[i].bar == bar) 124 return (i); 125 } 126 sc->sc_bar_mappings[i].bar = bar; 127 sc->sc_bar_mappings[i].used = 1; 128 return (i); 129} 130 131int 132puc_attach(device_t dev, const struct puc_device_description *desc) 133{ 134 char *typestr; 135 int bidx, childunit, i, irq_setup, rid; 136 struct puc_softc *sc; 137 struct puc_device *pdev; 138 struct resource *res; 139 struct resource_list_entry *rle; 140 141 sc = (struct puc_softc *)device_get_softc(dev); 142 bzero(sc, sizeof(*sc)); 143 sc->sc_desc = desc; 144 if (sc->sc_desc == NULL) 145 return (ENXIO); 146 147#ifdef PUC_DEBUG 148 bootverbose = 1; 149 150 printf("puc: name: %s\n", sc->sc_desc->name); 151#endif 152 rid = 0; 153 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 154 RF_ACTIVE | RF_SHAREABLE); 155 if (!res) 156 return (ENXIO); 157 158 sc->irqres = res; 159 sc->irqrid = rid; 160#ifdef PUC_FASTINTR 161 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 162 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 163#else 164 irq_setup = ENXIO; 165#endif 166 if (irq_setup != 0) 167 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 168 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 169 if (irq_setup != 0) 170 return (ENXIO); 171 172 rid = 0; 173 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 174 if (i > 0 && rid == sc->sc_desc->ports[i].bar) 175 sc->barmuxed = 1; 176 rid = sc->sc_desc->ports[i].bar; 177 bidx = puc_port_bar_index(sc, rid); 178 179 if (sc->sc_bar_mappings[bidx].res != NULL) 180 continue; 181 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 182 0ul, ~0ul, 1, RF_ACTIVE); 183 if (res == NULL) { 184 printf("could not get resource\n"); 185 continue; 186 } 187 sc->sc_bar_mappings[bidx].res = res; 188#ifdef PUC_DEBUG 189 printf("port rid %d bst %x, start %x, end %x\n", rid, 190 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 191 (u_int)rman_get_end(res)); 192#endif 193 } 194 195 if (desc->init != NULL) { 196 i = desc->init(sc); 197 if (i != 0) 198 return (i); 199 } 200 201 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 202 rid = sc->sc_desc->ports[i].bar; 203 bidx = puc_port_bar_index(sc, rid); 204 if (sc->sc_bar_mappings[bidx].res == NULL) 205 continue; 206 207 switch (sc->sc_desc->ports[i].type) { 208 case PUC_PORT_TYPE_COM: 209 typestr = "sio"; 210 break; 211 default: 212 continue; 213 } 214 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 215 M_NOWAIT | M_ZERO); 216 if (!pdev) 217 continue; 218 resource_list_init(&pdev->resources); 219 220 /* First fake up an IRQ resource. */ 221 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 222 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 223 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 224 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 225 rle->res = sc->irqres; 226 227 /* Now fake an IOPORT resource */ 228 res = sc->sc_bar_mappings[bidx].res; 229 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0, 230 rman_get_start(res) + sc->sc_desc->ports[i].offset, 231 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 232 8); 233 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0); 234 235 if (sc->barmuxed == 0) { 236 rle->res = sc->sc_bar_mappings[bidx].res; 237 } else { 238 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 239 M_WAITOK | M_ZERO); 240 if (rle->res == NULL) { 241 free(pdev, M_DEVBUF); 242 return (ENOMEM); 243 } 244 245 rle->res->r_start = rman_get_start(res) + 246 sc->sc_desc->ports[i].offset; 247 rle->res->r_end = rle->res->r_start + 8 - 1; 248 rle->res->r_bustag = rman_get_bustag(res); 249 bus_space_subregion(rle->res->r_bustag, 250 rman_get_bushandle(res), 251 sc->sc_desc->ports[i].offset, 8, 252 &rle->res->r_bushandle); 253 } 254 255 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 256 257 childunit = puc_find_free_unit(typestr); 258 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 259 if (sc->sc_ports[i].dev == NULL) { 260 if (sc->barmuxed) { 261 bus_space_unmap(rman_get_bustag(rle->res), 262 rman_get_bushandle(rle->res), 263 8); 264 free(rle->res, M_DEVBUF); 265 free(pdev, M_DEVBUF); 266 } 267 continue; 268 } 269 device_set_ivars(sc->sc_ports[i].dev, pdev); 270 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 271 if (!bootverbose) 272 device_quiet(sc->sc_ports[i].dev); 273#ifdef PUC_DEBUG 274 printf("puc: type %d, bar %x, offset %x\n", 275 sc->sc_desc->ports[i].type, 276 sc->sc_desc->ports[i].bar, 277 sc->sc_desc->ports[i].offset); 278 puc_print_resource_list(&pdev->resources); 279#endif 280 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 281 if (sc->barmuxed) { 282 bus_space_unmap(rman_get_bustag(rle->res), 283 rman_get_bushandle(rle->res), 284 8); 285 free(rle->res, M_DEVBUF); 286 free(pdev, M_DEVBUF); 287 } 288 } 289 } 290 291#ifdef PUC_DEBUG 292 bootverbose = 0; 293#endif 294 return (0); 295} 296 297/* 298 * This is just an brute force interrupt handler. It just calls all the 299 * registered handlers sequencially. 300 * 301 * Later on we should maybe have a different handler for boards that can 302 * tell us which device generated the interrupt. 303 */ 304static void 305puc_intr(void *arg) 306{ 307 int i; 308 struct puc_softc *sc; 309 310printf("puc_intr\n"); 311 sc = (struct puc_softc *)arg; 312 for (i = 0; i < PUC_MAX_PORTS; i++) 313 if (sc->sc_ports[i].ihand != NULL) 314 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 315} 316 317const struct puc_device_description * 318puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 319 uint32_t sprod) 320{ 321 int i; 322 323#define checkreg(val, index) \ 324 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 325 326 for (i = 0; puc_devices[i].name != NULL; i++) { 327 if (checkreg(vend, PUC_REG_VEND) && 328 checkreg(prod, PUC_REG_PROD) && 329 checkreg(svend, PUC_REG_SVEND) && 330 checkreg(sprod, PUC_REG_SPROD)) 331 return (&puc_devices[i]); 332 } 333 334#undef checkreg 335 336 return (NULL); 337} 338static int puc_find_free_unit(char *name) 339{ 340 devclass_t dc; 341 int start; 342 int unit; 343 344 unit = 0; 345 start = 0; 346 while (resource_int_value(name, unit, "port", &start) == 0 && 347 start > 0) 348 unit++; 349 dc = devclass_find(name); 350 if (dc == NULL) 351 return (-1); 352 while (devclass_get_device(dc, unit)) 353 unit++; 354#ifdef PUC_DEBUG 355 printf("puc: Using %s%d\n", name, unit); 356#endif 357 return (unit); 358} 359 360#ifdef PUC_DEBUG 361static void 362puc_print_resource_list(struct resource_list *rl) 363{ 364#if 0 365 struct resource_list_entry *rle; 366 367 printf("print_resource_list: rl %p\n", rl); 368 SLIST_FOREACH(rle, rl, link) 369 printf(" type %x, rid %x start %x end %x count %x\n", 370 rle->type, rle->rid, rle->start, rle->end, rle->count); 371 printf("print_resource_list: end.\n"); 372#endif 373} 374#endif 375 376struct resource * 377puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 378 u_long start, u_long end, u_long count, u_int flags) 379{ 380 struct puc_device *pdev; 381 struct resource *retval; 382 struct resource_list *rl; 383 struct resource_list_entry *rle; 384 385 pdev = device_get_ivars(child); 386 rl = &pdev->resources; 387 388#ifdef PUC_DEBUG 389 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 390 pdev, type, *rid); 391 puc_print_resource_list(rl); 392#endif 393 retval = NULL; 394 rle = resource_list_find(rl, type, *rid); 395 if (rle) { 396 start = rle->start; 397 end = rle->end; 398 count = rle->count; 399#ifdef PUC_DEBUG 400 printf("found rle, %lx, %lx, %lx\n", start, end, count); 401#endif 402 retval = rle->res; 403 } else 404 printf("oops rle is gone\n"); 405 406 return (retval); 407} 408 409int 410puc_release_resource(device_t dev, device_t child, int type, int rid, 411 struct resource *res) 412{ 413 return (0); 414} 415 416int 417puc_get_resource(device_t dev, device_t child, int type, int rid, 418 u_long *startp, u_long *countp) 419{ 420 struct puc_device *pdev; 421 struct resource_list *rl; 422 struct resource_list_entry *rle; 423 424 pdev = device_get_ivars(child); 425 rl = &pdev->resources; 426 427#ifdef PUC_DEBUG 428 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 429 type, rid); 430 puc_print_resource_list(rl); 431#endif 432 rle = resource_list_find(rl, type, rid); 433 if (rle) { 434#ifdef PUC_DEBUG 435 printf("found rle %p,", rle); 436#endif 437 if (startp != NULL) 438 *startp = rle->start; 439 if (countp != NULL) 440 *countp = rle->count; 441#ifdef PUC_DEBUG 442 printf(" %lx, %lx\n", rle->start, rle->count); 443#endif 444 return (0); 445 } else 446 printf("oops rle is gone\n"); 447 return (ENXIO); 448} 449 450int 451puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 452 void (*ihand)(void *), void *arg, void **cookiep) 453{ 454 int i; 455 struct puc_softc *sc; 456 457printf("puc_setup_intr()\n"); 458 sc = (struct puc_softc *)device_get_softc(dev); 459 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 460 if (sc->sc_ports[i].dev == child) { 461 if (sc->sc_ports[i].ihand != 0) 462 return (ENXIO); 463 sc->sc_ports[i].ihand = ihand; 464 sc->sc_ports[i].ihandarg = arg; 465 *cookiep = arg; 466 return (0); 467 } 468 } 469 return (ENXIO); 470} 471 472int 473puc_teardown_intr(device_t dev, device_t child, struct resource *r, 474 void *cookie) 475{ 476 int i; 477 struct puc_softc *sc; 478 479printf("puc_teardown_intr()\n"); 480 sc = (struct puc_softc *)device_get_softc(dev); 481 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 482 if (sc->sc_ports[i].dev == child) { 483 sc->sc_ports[i].ihand = NULL; 484 sc->sc_ports[i].ihandarg = NULL; 485 return (0); 486 } 487 } 488 return (ENXIO); 489} 490 491int 492puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 493{ 494 struct puc_device *pdev; 495 496 pdev = device_get_ivars(child); 497 if (pdev == NULL) 498 return (ENOENT); 499 500 switch(index) { 501 case PUC_IVAR_FREQ: 502 *result = pdev->serialfreq; 503 break; 504 default: 505 return (ENOENT); 506 } 507 return (0); 508} 509 510devclass_t puc_devclass; 511 512