puc.c revision 102894
1181847Sjkim/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 2182393Sjkim 3181847Sjkim/*- 4181847Sjkim * Copyright (c) 2002 JF Hay. All rights reserved. 5181847Sjkim * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6181847Sjkim * 7181847Sjkim * Redistribution and use in source and binary forms, with or without 8181847Sjkim * modification, are permitted provided that the following conditions 9182393Sjkim * are met: 10181847Sjkim * 1. Redistributions of source code must retain the above copyright 11181847Sjkim * notice unmodified, this list of conditions, and the following 12181847Sjkim * disclaimer. 13181847Sjkim * 2. Redistributions in binary form must reproduce the above copyright 14181847Sjkim * notice, this list of conditions and the following disclaimer in the 15181847Sjkim * documentation and/or other materials provided with the distribution. 16181847Sjkim * 17181847Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18181847Sjkim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19181847Sjkim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20181847Sjkim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21181847Sjkim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22181847Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23181847Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24181847Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25181847Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26181847Sjkim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27181847Sjkim */ 28181847Sjkim 29181847Sjkim/* 30182393Sjkim * Copyright (c) 1996, 1998, 1999 31181847Sjkim * 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 102894 2002-09-03 11:19:44Z 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#ifdef PUC_FASTINTR 163 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 164 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 165#else 166 irq_setup = ENXIO; 167#endif 168 if (irq_setup != 0) 169 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 170 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 171 if (irq_setup != 0) 172 return (ENXIO); 173 174 rid = 0; 175 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 176 if (i > 0 && rid == sc->sc_desc->ports[i].bar) 177 sc->barmuxed = 1; 178 rid = sc->sc_desc->ports[i].bar; 179 bidx = puc_port_bar_index(sc, rid); 180 181 if (sc->sc_bar_mappings[bidx].res != NULL) 182 continue; 183 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 184 0ul, ~0ul, 1, RF_ACTIVE); 185 if (res == NULL) { 186 printf("could not get resource\n"); 187 continue; 188 } 189 sc->sc_bar_mappings[bidx].res = res; 190#ifdef PUC_DEBUG 191 printf("port rid %d bst %x, start %x, end %x\n", rid, 192 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 193 (u_int)rman_get_end(res)); 194#endif 195 } 196 197 if (desc->init != NULL) { 198 i = desc->init(sc); 199 if (i != 0) 200 return (i); 201 } 202 203 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 204 rid = sc->sc_desc->ports[i].bar; 205 bidx = puc_port_bar_index(sc, rid); 206 if (sc->sc_bar_mappings[bidx].res == NULL) 207 continue; 208 209 switch (sc->sc_desc->ports[i].type) { 210 case PUC_PORT_TYPE_COM: 211 typestr = "sio"; 212 break; 213 default: 214 continue; 215 } 216 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 217 M_NOWAIT | M_ZERO); 218 if (!pdev) 219 continue; 220 resource_list_init(&pdev->resources); 221 222 /* First fake up an IRQ resource. */ 223 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 224 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 225 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 226 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 227 rle->res = sc->irqres; 228 229 /* Now fake an IOPORT resource */ 230 res = sc->sc_bar_mappings[bidx].res; 231 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0, 232 rman_get_start(res) + sc->sc_desc->ports[i].offset, 233 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 234 8); 235 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0); 236 237 if (sc->barmuxed == 0) { 238 rle->res = sc->sc_bar_mappings[bidx].res; 239 } else { 240 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 241 M_WAITOK | M_ZERO); 242 if (rle->res == NULL) { 243 free(pdev, M_DEVBUF); 244 return (ENOMEM); 245 } 246 247 rle->res->r_start = rman_get_start(res) + 248 sc->sc_desc->ports[i].offset; 249 rle->res->r_end = rle->res->r_start + 8 - 1; 250 rle->res->r_bustag = rman_get_bustag(res); 251 bus_space_subregion(rle->res->r_bustag, 252 rman_get_bushandle(res), 253 sc->sc_desc->ports[i].offset, 8, 254 &rle->res->r_bushandle); 255 } 256 257 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 258 259 childunit = puc_find_free_unit(typestr); 260 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 261 if (sc->sc_ports[i].dev == NULL) { 262 if (sc->barmuxed) { 263 bus_space_unmap(rman_get_bustag(rle->res), 264 rman_get_bushandle(rle->res), 265 8); 266 free(rle->res, M_DEVBUF); 267 free(pdev, M_DEVBUF); 268 } 269 continue; 270 } 271 device_set_ivars(sc->sc_ports[i].dev, pdev); 272 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 273 if (!bootverbose) 274 device_quiet(sc->sc_ports[i].dev); 275#ifdef PUC_DEBUG 276 printf("puc: type %d, bar %x, offset %x\n", 277 sc->sc_desc->ports[i].type, 278 sc->sc_desc->ports[i].bar, 279 sc->sc_desc->ports[i].offset); 280 puc_print_resource_list(&pdev->resources); 281#endif 282 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 283 if (sc->barmuxed) { 284 bus_space_unmap(rman_get_bustag(rle->res), 285 rman_get_bushandle(rle->res), 286 8); 287 free(rle->res, M_DEVBUF); 288 free(pdev, M_DEVBUF); 289 } 290 } 291 } 292 293#ifdef PUC_DEBUG 294 bootverbose = 0; 295#endif 296 return (0); 297} 298 299/* 300 * This is just an brute force interrupt handler. It just calls all the 301 * registered handlers sequencially. 302 * 303 * Later on we should maybe have a different handler for boards that can 304 * tell us which device generated the interrupt. 305 */ 306static void 307puc_intr(void *arg) 308{ 309 int i; 310 struct puc_softc *sc; 311 312 sc = (struct puc_softc *)arg; 313 for (i = 0; i < PUC_MAX_PORTS; i++) 314 if (sc->sc_ports[i].ihand != NULL) 315 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 316} 317 318const struct puc_device_description * 319puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 320 uint32_t sprod) 321{ 322 int i; 323 324#define checkreg(val, index) \ 325 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 326 327 for (i = 0; puc_devices[i].name != NULL; i++) { 328 if (checkreg(vend, PUC_REG_VEND) && 329 checkreg(prod, PUC_REG_PROD) && 330 checkreg(svend, PUC_REG_SVEND) && 331 checkreg(sprod, PUC_REG_SPROD)) 332 return (&puc_devices[i]); 333 } 334 335#undef checkreg 336 337 return (NULL); 338} 339 340static int 341puc_find_free_unit(char *name) 342{ 343 devclass_t dc; 344 int start; 345 int unit; 346 347 unit = 0; 348 start = 0; 349 while (resource_int_value(name, unit, "port", &start) == 0 && 350 start > 0) 351 unit++; 352 dc = devclass_find(name); 353 if (dc == NULL) 354 return (-1); 355 while (devclass_get_device(dc, unit)) 356 unit++; 357#ifdef PUC_DEBUG 358 printf("puc: Using %s%d\n", name, unit); 359#endif 360 return (unit); 361} 362 363#ifdef PUC_DEBUG 364static void 365puc_print_resource_list(struct resource_list *rl) 366{ 367#if 0 368 struct resource_list_entry *rle; 369 370 printf("print_resource_list: rl %p\n", rl); 371 SLIST_FOREACH(rle, rl, link) 372 printf(" type %x, rid %x start %x end %x count %x\n", 373 rle->type, rle->rid, rle->start, rle->end, rle->count); 374 printf("print_resource_list: end.\n"); 375#endif 376} 377#endif 378 379struct resource * 380puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 381 u_long start, u_long end, u_long count, u_int flags) 382{ 383 struct puc_device *pdev; 384 struct resource *retval; 385 struct resource_list *rl; 386 struct resource_list_entry *rle; 387 388 pdev = device_get_ivars(child); 389 rl = &pdev->resources; 390 391#ifdef PUC_DEBUG 392 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 393 pdev, type, *rid); 394 puc_print_resource_list(rl); 395#endif 396 retval = NULL; 397 rle = resource_list_find(rl, type, *rid); 398 if (rle) { 399 start = rle->start; 400 end = rle->end; 401 count = rle->count; 402#ifdef PUC_DEBUG 403 printf("found rle, %lx, %lx, %lx\n", start, end, count); 404#endif 405 retval = rle->res; 406 } else 407 printf("oops rle is gone\n"); 408 409 return (retval); 410} 411 412int 413puc_release_resource(device_t dev, device_t child, int type, int rid, 414 struct resource *res) 415{ 416 return (0); 417} 418 419int 420puc_get_resource(device_t dev, device_t child, int type, int rid, 421 u_long *startp, u_long *countp) 422{ 423 struct puc_device *pdev; 424 struct resource_list *rl; 425 struct resource_list_entry *rle; 426 427 pdev = device_get_ivars(child); 428 rl = &pdev->resources; 429 430#ifdef PUC_DEBUG 431 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 432 type, rid); 433 puc_print_resource_list(rl); 434#endif 435 rle = resource_list_find(rl, type, rid); 436 if (rle) { 437#ifdef PUC_DEBUG 438 printf("found rle %p,", rle); 439#endif 440 if (startp != NULL) 441 *startp = rle->start; 442 if (countp != NULL) 443 *countp = rle->count; 444#ifdef PUC_DEBUG 445 printf(" %lx, %lx\n", rle->start, rle->count); 446#endif 447 return (0); 448 } else 449 printf("oops rle is gone\n"); 450 return (ENXIO); 451} 452 453int 454puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 455 void (*ihand)(void *), void *arg, void **cookiep) 456{ 457 int i; 458 struct puc_softc *sc; 459 460printf("puc_setup_intr()\n"); 461 sc = (struct puc_softc *)device_get_softc(dev); 462 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 463 if (sc->sc_ports[i].dev == child) { 464 if (sc->sc_ports[i].ihand != 0) 465 return (ENXIO); 466 sc->sc_ports[i].ihand = ihand; 467 sc->sc_ports[i].ihandarg = arg; 468 *cookiep = arg; 469 return (0); 470 } 471 } 472 return (ENXIO); 473} 474 475int 476puc_teardown_intr(device_t dev, device_t child, struct resource *r, 477 void *cookie) 478{ 479 int i; 480 struct puc_softc *sc; 481 482printf("puc_teardown_intr()\n"); 483 sc = (struct puc_softc *)device_get_softc(dev); 484 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 485 if (sc->sc_ports[i].dev == child) { 486 sc->sc_ports[i].ihand = NULL; 487 sc->sc_ports[i].ihandarg = NULL; 488 return (0); 489 } 490 } 491 return (ENXIO); 492} 493 494int 495puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 496{ 497 struct puc_device *pdev; 498 499 pdev = device_get_ivars(child); 500 if (pdev == NULL) 501 return (ENOENT); 502 503 switch(index) { 504 case PUC_IVAR_FREQ: 505 *result = pdev->serialfreq; 506 break; 507 default: 508 return (ENOENT); 509 } 510 return (0); 511} 512