puc.c revision 109458
198184Sgordon/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 298184Sgordon 398184Sgordon/*- 498184Sgordon * Copyright (c) 2002 JF Hay. All rights reserved. 598184Sgordon * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6151806Syar * 7113676Smtm * Redistribution and use in source and binary forms, with or without 8136224Smtm * modification, are permitted provided that the following conditions 998184Sgordon * are met: 1098184Sgordon * 1. Redistributions of source code must retain the above copyright 1198184Sgordon * notice unmodified, this list of conditions, and the following 1298184Sgordon * disclaimer. 13138889Sbrian * 2. Redistributions in binary form must reproduce the above copyright 14151807Syar * notice, this list of conditions and the following disclaimer in the 15172586Semax * documentation and/or other materials provided with the distribution. 16172761Semax * 17168119Smtm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1898184Sgordon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19172586Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2098184Sgordon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21172586Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22172586Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23172586Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24172586Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25172586Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26172586Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27172586Semax */ 28172586Semax 29172586Semax/* 30172586Semax * Copyright (c) 1996, 1998, 1999 31172586Semax * Christopher G. Demetriou. All rights reserved. 32172586Semax * 33172586Semax * Redistribution and use in source and binary forms, with or without 34172586Semax * modification, are permitted provided that the following conditions 35172586Semax * are met: 36172586Semax * 1. Redistributions of source code must retain the above copyright 37172586Semax * notice, this list of conditions and the following disclaimer. 38172586Semax * 2. Redistributions in binary form must reproduce the above copyright 3998184Sgordon * notice, this list of conditions and the following disclaimer in the 4098184Sgordon * documentation and/or other materials provided with the distribution. 41172586Semax * 3. All advertising materials mentioning features or use of this software 42172586Semax * must display the following acknowledgement: 43172586Semax * This product includes software developed by Christopher G. Demetriou 44172586Semax * for the NetBSD Project. 4598184Sgordon * 4. The name of the author may not be used to endorse or promote products 4698184Sgordon * derived from this software without specific prior written permission 47172586Semax * 4898184Sgordon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 4998184Sgordon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 5098184Sgordon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51172586Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 5298184Sgordon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 53138889Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5498184Sgordon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5598184Sgordon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5698184Sgordon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 57172586Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58172586Semax */ 59172586Semax 60138889Sbrian#include <sys/cdefs.h> 6198184Sgordon__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 109458 2003-01-18 02:54:16Z marcel $"); 62172586Semax 63172586Semax/* 64172761Semax * PCI "universal" communication card device driver, glues com, lpt, 65172586Semax * and similar ports to PCI via bridge chip often much larger than 66172761Semax * the devices being glued. 67172761Semax * 68172761Semax * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 69172761Semax * sys/dev/pci/pciide.c, revision 1.6). 70172761Semax * 71172586Semax * These devices could be (and some times are) described as 72172586Semax * communications/{serial,parallel}, etc. devices with known 73172761Semax * programming interfaces, but those programming interfaces (in 74172586Semax * particular the BAR assignments for devices, etc.) in fact are not 75172586Semax * particularly well defined. 76172586Semax * 77172586Semax * After I/we have seen more of these devices, it may be possible 78172586Semax * to generalize some of these bits. In particular, devices which 79172586Semax * describe themselves as communications/serial/16[45]50, and 80172586Semax * communications/parallel/??? might be attached via direct 81168119Smtm * 'com' and 'lpt' attachments to pci. 82138889Sbrian */ 83163696Savatar 8498184Sgordon#include "opt_puc.h" 8598184Sgordon 86163696Savatar#include <sys/param.h> 8798184Sgordon#include <sys/systm.h> 8898184Sgordon#include <sys/kernel.h> 89172761Semax#include <sys/bus.h> 90172761Semax#include <sys/conf.h> 91172761Semax#include <sys/malloc.h> 92172761Semax 93172761Semax#include <machine/bus.h> 94172761Semax#include <machine/resource.h> 95172761Semax#include <sys/rman.h> 96172761Semax 97172761Semax#include <dev/pci/pcireg.h> 98172761Semax#include <dev/pci/pcivar.h> 99172761Semax 100172761Semax#define PUC_ENTRAILS 1 101172761Semax#include <dev/puc/pucvar.h> 102172761Semax 103172761Semaxstruct puc_device { 104172761Semax struct resource_list resources; 105172761Semax u_int serialfreq; 106172761Semax}; 107172761Semax 108172761Semaxstatic void puc_intr(void *arg); 109172761Semax 110172761Semaxstatic int puc_find_free_unit(char *); 111172761Semax#ifdef PUC_DEBUG 112172761Semaxstatic void puc_print_resource_list(struct resource_list *); 113172761Semax#endif 114172761Semax 115172761Semaxdevclass_t puc_devclass; 11698184Sgordon 117172761Semaxstatic 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, type; 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 if (irq_setup == 0) 166 sc->fastintr = INTR_FAST; 167 else 168 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 169 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 170#else 171 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 172 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 173#endif 174 if (irq_setup != 0) 175 return (ENXIO); 176 177 rid = 0; 178 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 179 if (i > 0 && rid == sc->sc_desc->ports[i].bar) 180 sc->barmuxed = 1; 181 rid = sc->sc_desc->ports[i].bar; 182 bidx = puc_port_bar_index(sc, rid); 183 184 if (sc->sc_bar_mappings[bidx].res != NULL) 185 continue; 186 187 type = (sc->sc_desc->ports[i].flags & PUC_FLAGS_MEMORY) 188 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 189 190 res = bus_alloc_resource(dev, type, &rid, 0ul, ~0ul, 1, 191 RF_ACTIVE); 192 if (res == NULL) { 193 printf("could not get resource\n"); 194 continue; 195 } 196 sc->sc_bar_mappings[bidx].type = type; 197 sc->sc_bar_mappings[bidx].res = res; 198#ifdef PUC_DEBUG 199 printf("%s rid %d bst %x, start %x, end %x\n", 200 (type == SYS_RES_MEMORY) ? "memory" : "port", rid, 201 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 202 (u_int)rman_get_end(res)); 203#endif 204 } 205 206 if (desc->init != NULL) { 207 i = desc->init(sc); 208 if (i != 0) 209 return (i); 210 } 211 212 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 213 rid = sc->sc_desc->ports[i].bar; 214 bidx = puc_port_bar_index(sc, rid); 215 if (sc->sc_bar_mappings[bidx].res == NULL) 216 continue; 217 218 switch (sc->sc_desc->ports[i].type) { 219 case PUC_PORT_TYPE_COM: 220 typestr = "sio"; 221 break; 222 default: 223 continue; 224 } 225 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 226 M_NOWAIT | M_ZERO); 227 if (!pdev) 228 continue; 229 resource_list_init(&pdev->resources); 230 231 /* First fake up an IRQ resource. */ 232 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 233 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 234 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 235 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 236 rle->res = sc->irqres; 237 238 /* Now fake an IOPORT or MEMORY resource */ 239 res = sc->sc_bar_mappings[bidx].res; 240 type = sc->sc_bar_mappings[bidx].type; 241 resource_list_add(&pdev->resources, type, 0, 242 rman_get_start(res) + sc->sc_desc->ports[i].offset, 243 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 244 8); 245 rle = resource_list_find(&pdev->resources, type, 0); 246 247 if (sc->barmuxed == 0) { 248 rle->res = sc->sc_bar_mappings[bidx].res; 249 } else { 250 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 251 M_WAITOK | M_ZERO); 252 if (rle->res == NULL) { 253 free(pdev, M_DEVBUF); 254 return (ENOMEM); 255 } 256 257 rle->res->r_start = rman_get_start(res) + 258 sc->sc_desc->ports[i].offset; 259 rle->res->r_end = rle->res->r_start + 8 - 1; 260 rle->res->r_bustag = rman_get_bustag(res); 261 bus_space_subregion(rle->res->r_bustag, 262 rman_get_bushandle(res), 263 sc->sc_desc->ports[i].offset, 8, 264 &rle->res->r_bushandle); 265 } 266 267 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 268 269 childunit = puc_find_free_unit(typestr); 270 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 271 if (sc->sc_ports[i].dev == NULL) { 272 if (sc->barmuxed) { 273 bus_space_unmap(rman_get_bustag(rle->res), 274 rman_get_bushandle(rle->res), 8); 275 free(rle->res, M_DEVBUF); 276 free(pdev, M_DEVBUF); 277 } 278 continue; 279 } 280 device_set_ivars(sc->sc_ports[i].dev, pdev); 281 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 282 if (!bootverbose) 283 device_quiet(sc->sc_ports[i].dev); 284#ifdef PUC_DEBUG 285 printf("puc: type %d, bar %x, offset %x\n", 286 sc->sc_desc->ports[i].type, 287 sc->sc_desc->ports[i].bar, 288 sc->sc_desc->ports[i].offset); 289 puc_print_resource_list(&pdev->resources); 290#endif 291 device_set_flags(sc->sc_ports[i].dev, 292 sc->sc_desc->ports[i].flags); 293 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 294 if (sc->barmuxed) { 295 bus_space_unmap(rman_get_bustag(rle->res), 296 rman_get_bushandle(rle->res), 297 8); 298 free(rle->res, M_DEVBUF); 299 free(pdev, M_DEVBUF); 300 } 301 } 302 } 303 304#ifdef PUC_DEBUG 305 bootverbose = 0; 306#endif 307 return (0); 308} 309 310/* 311 * This is just a brute force interrupt handler. It just calls all the 312 * registered handlers sequencially. 313 * 314 * Later on we should maybe have a different handler for boards that can 315 * tell us which device generated the interrupt. 316 */ 317static void 318puc_intr(void *arg) 319{ 320 int i; 321 struct puc_softc *sc; 322 323 sc = (struct puc_softc *)arg; 324 for (i = 0; i < PUC_MAX_PORTS; i++) 325 if (sc->sc_ports[i].ihand != NULL) 326 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 327} 328 329const struct puc_device_description * 330puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 331 uint32_t sprod) 332{ 333 int i; 334 335#define checkreg(val, index) \ 336 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 337 338 for (i = 0; puc_devices[i].name != NULL; i++) { 339 if (checkreg(vend, PUC_REG_VEND) && 340 checkreg(prod, PUC_REG_PROD) && 341 checkreg(svend, PUC_REG_SVEND) && 342 checkreg(sprod, PUC_REG_SPROD)) 343 return (&puc_devices[i]); 344 } 345 346#undef checkreg 347 348 return (NULL); 349} 350 351static int 352puc_find_free_unit(char *name) 353{ 354 devclass_t dc; 355 int start; 356 int unit; 357 358 unit = 0; 359 start = 0; 360 while (resource_int_value(name, unit, "port", &start) == 0 && 361 start > 0) 362 unit++; 363 dc = devclass_find(name); 364 if (dc == NULL) 365 return (-1); 366 while (devclass_get_device(dc, unit)) 367 unit++; 368#ifdef PUC_DEBUG 369 printf("puc: Using %s%d\n", name, unit); 370#endif 371 return (unit); 372} 373 374#ifdef PUC_DEBUG 375static void 376puc_print_resource_list(struct resource_list *rl) 377{ 378#if 0 379 struct resource_list_entry *rle; 380 381 printf("print_resource_list: rl %p\n", rl); 382 SLIST_FOREACH(rle, rl, link) 383 printf(" type %x, rid %x start %x end %x count %x\n", 384 rle->type, rle->rid, rle->start, rle->end, rle->count); 385 printf("print_resource_list: end.\n"); 386#endif 387} 388#endif 389 390struct resource * 391puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 392 u_long start, u_long end, u_long count, u_int flags) 393{ 394 struct puc_device *pdev; 395 struct resource *retval; 396 struct resource_list *rl; 397 struct resource_list_entry *rle; 398 399 pdev = device_get_ivars(child); 400 rl = &pdev->resources; 401 402#ifdef PUC_DEBUG 403 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 404 pdev, type, *rid); 405 puc_print_resource_list(rl); 406#endif 407 retval = NULL; 408 rle = resource_list_find(rl, type, *rid); 409 if (rle) { 410 start = rle->start; 411 end = rle->end; 412 count = rle->count; 413#ifdef PUC_DEBUG 414 printf("found rle, %lx, %lx, %lx\n", start, end, count); 415#endif 416 retval = rle->res; 417 } else 418 printf("oops rle is gone\n"); 419 420 return (retval); 421} 422 423int 424puc_release_resource(device_t dev, device_t child, int type, int rid, 425 struct resource *res) 426{ 427 return (0); 428} 429 430int 431puc_get_resource(device_t dev, device_t child, int type, int rid, 432 u_long *startp, u_long *countp) 433{ 434 struct puc_device *pdev; 435 struct resource_list *rl; 436 struct resource_list_entry *rle; 437 438 pdev = device_get_ivars(child); 439 rl = &pdev->resources; 440 441#ifdef PUC_DEBUG 442 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 443 type, rid); 444 puc_print_resource_list(rl); 445#endif 446 rle = resource_list_find(rl, type, rid); 447 if (rle) { 448#ifdef PUC_DEBUG 449 printf("found rle %p,", rle); 450#endif 451 if (startp != NULL) 452 *startp = rle->start; 453 if (countp != NULL) 454 *countp = rle->count; 455#ifdef PUC_DEBUG 456 printf(" %lx, %lx\n", rle->start, rle->count); 457#endif 458 return (0); 459 } else 460 printf("oops rle is gone\n"); 461 return (ENXIO); 462} 463 464int 465puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 466 void (*ihand)(void *), void *arg, void **cookiep) 467{ 468 int i; 469 struct puc_softc *sc; 470 471 sc = (struct puc_softc *)device_get_softc(dev); 472 if ((flags & INTR_FAST) != sc->fastintr) 473 return (ENXIO); 474 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 475 if (sc->sc_ports[i].dev == child) { 476 if (sc->sc_ports[i].ihand != 0) 477 return (ENXIO); 478 sc->sc_ports[i].ihand = ihand; 479 sc->sc_ports[i].ihandarg = arg; 480 *cookiep = arg; 481 return (0); 482 } 483 } 484 return (ENXIO); 485} 486 487int 488puc_teardown_intr(device_t dev, device_t child, struct resource *r, 489 void *cookie) 490{ 491 int i; 492 struct puc_softc *sc; 493 494 sc = (struct puc_softc *)device_get_softc(dev); 495 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 496 if (sc->sc_ports[i].dev == child) { 497 sc->sc_ports[i].ihand = NULL; 498 sc->sc_ports[i].ihandarg = NULL; 499 return (0); 500 } 501 } 502 return (ENXIO); 503} 504 505int 506puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 507{ 508 struct puc_device *pdev; 509 510 pdev = device_get_ivars(child); 511 if (pdev == NULL) 512 return (ENOENT); 513 514 switch(index) { 515 case PUC_IVAR_FREQ: 516 *result = pdev->serialfreq; 517 break; 518 default: 519 return (ENOENT); 520 } 521 return (0); 522} 523