puc.c revision 102893
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 102893 2002-09-03 11:18:35Z 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 312printf("puc_intr\n"); 313 sc = (struct puc_softc *)arg; 314 for (i = 0; i < PUC_MAX_PORTS; i++) 315 if (sc->sc_ports[i].ihand != NULL) 316 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 317} 318 319const struct puc_device_description * 320puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 321 uint32_t sprod) 322{ 323 int i; 324 325#define checkreg(val, index) \ 326 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 327 328 for (i = 0; puc_devices[i].name != NULL; i++) { 329 if (checkreg(vend, PUC_REG_VEND) && 330 checkreg(prod, PUC_REG_PROD) && 331 checkreg(svend, PUC_REG_SVEND) && 332 checkreg(sprod, PUC_REG_SPROD)) 333 return (&puc_devices[i]); 334 } 335 336#undef checkreg 337 338 return (NULL); 339} 340static int puc_find_free_unit(char *name) 341{ 342 devclass_t dc; 343 int start; 344 int unit; 345 346 unit = 0; 347 start = 0; 348 while (resource_int_value(name, unit, "port", &start) == 0 && 349 start > 0) 350 unit++; 351 dc = devclass_find(name); 352 if (dc == NULL) 353 return (-1); 354 while (devclass_get_device(dc, unit)) 355 unit++; 356#ifdef PUC_DEBUG 357 printf("puc: Using %s%d\n", name, unit); 358#endif 359 return (unit); 360} 361 362#ifdef PUC_DEBUG 363static void 364puc_print_resource_list(struct resource_list *rl) 365{ 366#if 0 367 struct resource_list_entry *rle; 368 369 printf("print_resource_list: rl %p\n", rl); 370 SLIST_FOREACH(rle, rl, link) 371 printf(" type %x, rid %x start %x end %x count %x\n", 372 rle->type, rle->rid, rle->start, rle->end, rle->count); 373 printf("print_resource_list: end.\n"); 374#endif 375} 376#endif 377 378struct resource * 379puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 380 u_long start, u_long end, u_long count, u_int flags) 381{ 382 struct puc_device *pdev; 383 struct resource *retval; 384 struct resource_list *rl; 385 struct resource_list_entry *rle; 386 387 pdev = device_get_ivars(child); 388 rl = &pdev->resources; 389 390#ifdef PUC_DEBUG 391 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 392 pdev, type, *rid); 393 puc_print_resource_list(rl); 394#endif 395 retval = NULL; 396 rle = resource_list_find(rl, type, *rid); 397 if (rle) { 398 start = rle->start; 399 end = rle->end; 400 count = rle->count; 401#ifdef PUC_DEBUG 402 printf("found rle, %lx, %lx, %lx\n", start, end, count); 403#endif 404 retval = rle->res; 405 } else 406 printf("oops rle is gone\n"); 407 408 return (retval); 409} 410 411int 412puc_release_resource(device_t dev, device_t child, int type, int rid, 413 struct resource *res) 414{ 415 return (0); 416} 417 418int 419puc_get_resource(device_t dev, device_t child, int type, int rid, 420 u_long *startp, u_long *countp) 421{ 422 struct puc_device *pdev; 423 struct resource_list *rl; 424 struct resource_list_entry *rle; 425 426 pdev = device_get_ivars(child); 427 rl = &pdev->resources; 428 429#ifdef PUC_DEBUG 430 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 431 type, rid); 432 puc_print_resource_list(rl); 433#endif 434 rle = resource_list_find(rl, type, rid); 435 if (rle) { 436#ifdef PUC_DEBUG 437 printf("found rle %p,", rle); 438#endif 439 if (startp != NULL) 440 *startp = rle->start; 441 if (countp != NULL) 442 *countp = rle->count; 443#ifdef PUC_DEBUG 444 printf(" %lx, %lx\n", rle->start, rle->count); 445#endif 446 return (0); 447 } else 448 printf("oops rle is gone\n"); 449 return (ENXIO); 450} 451 452int 453puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 454 void (*ihand)(void *), void *arg, void **cookiep) 455{ 456 int i; 457 struct puc_softc *sc; 458 459printf("puc_setup_intr()\n"); 460 sc = (struct puc_softc *)device_get_softc(dev); 461 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 462 if (sc->sc_ports[i].dev == child) { 463 if (sc->sc_ports[i].ihand != 0) 464 return (ENXIO); 465 sc->sc_ports[i].ihand = ihand; 466 sc->sc_ports[i].ihandarg = arg; 467 *cookiep = arg; 468 return (0); 469 } 470 } 471 return (ENXIO); 472} 473 474int 475puc_teardown_intr(device_t dev, device_t child, struct resource *r, 476 void *cookie) 477{ 478 int i; 479 struct puc_softc *sc; 480 481printf("puc_teardown_intr()\n"); 482 sc = (struct puc_softc *)device_get_softc(dev); 483 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 484 if (sc->sc_ports[i].dev == child) { 485 sc->sc_ports[i].ihand = NULL; 486 sc->sc_ports[i].ihandarg = NULL; 487 return (0); 488 } 489 } 490 return (ENXIO); 491} 492 493int 494puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 495{ 496 struct puc_device *pdev; 497 498 pdev = device_get_ivars(child); 499 if (pdev == NULL) 500 return (ENOENT); 501 502 switch(index) { 503 case PUC_IVAR_FREQ: 504 *result = pdev->serialfreq; 505 break; 506 default: 507 return (ENOENT); 508 } 509 return (0); 510} 511