puc.c revision 102929
146283Sdfr/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 246283Sdfr 346283Sdfr/*- 446283Sdfr * Copyright (c) 2002 JF Hay. All rights reserved. 546283Sdfr * Copyright (c) 2000 M. Warner Losh. All rights reserved. 646283Sdfr * 746283Sdfr * Redistribution and use in source and binary forms, with or without 846283Sdfr * modification, are permitted provided that the following conditions 946283Sdfr * are met: 1046283Sdfr * 1. Redistributions of source code must retain the above copyright 1146283Sdfr * notice unmodified, this list of conditions, and the following 1246283Sdfr * disclaimer. 1346283Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1446283Sdfr * notice, this list of conditions and the following disclaimer in the 1546283Sdfr * documentation and/or other materials provided with the distribution. 1646283Sdfr * 1746283Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1846283Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1946283Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2046283Sdfr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2146283Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2246283Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2346283Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2446283Sdfr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2546283Sdfr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2646283Sdfr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2746283Sdfr */ 2846283Sdfr 2946283Sdfr/* 3046283Sdfr * Copyright (c) 1996, 1998, 1999 3146283Sdfr * Christopher G. Demetriou. All rights reserved. 3246283Sdfr * 3346283Sdfr * Redistribution and use in source and binary forms, with or without 3446283Sdfr * modification, are permitted provided that the following conditions 3546283Sdfr * are met: 3646283Sdfr * 1. Redistributions of source code must retain the above copyright 3746283Sdfr * notice, this list of conditions and the following disclaimer. 3846283Sdfr * 2. Redistributions in binary form must reproduce the above copyright 3946283Sdfr * notice, this list of conditions and the following disclaimer in the 4046283Sdfr * documentation and/or other materials provided with the distribution. 4146283Sdfr * 3. All advertising materials mentioning features or use of this software 4246283Sdfr * must display the following acknowledgement: 4346283Sdfr * This product includes software developed by Christopher G. Demetriou 4446283Sdfr * for the NetBSD Project. 4546283Sdfr * 4. The name of the author may not be used to endorse or promote products 4646283Sdfr * derived from this software without specific prior written permission 4746283Sdfr * 4846283Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 4946283Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 5046283Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 5146283Sdfr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 5246283Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5346283Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5446283Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5546283Sdfr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5646283Sdfr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 5746283Sdfr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5846283Sdfr */ 5946283Sdfr 6046283Sdfr#include <sys/cdefs.h> 6146283Sdfr__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 102929 2002-09-04 15:29:04Z phk $"); 6246283Sdfr 6346283Sdfr/* 6446283Sdfr * PCI "universal" communication card device driver, glues com, lpt, 6546283Sdfr * and similar ports to PCI via bridge chip often much larger than 6646283Sdfr * the devices being glued. 6746283Sdfr * 6846283Sdfr * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 6946283Sdfr * sys/dev/pci/pciide.c, revision 1.6). 7046283Sdfr * 7146283Sdfr * These devices could be (and some times are) described as 7246283Sdfr * communications/{serial,parallel}, etc. devices with known 7346283Sdfr * programming interfaces, but those programming interfaces (in 7446283Sdfr * particular the BAR assignments for devices, etc.) in fact are not 7546283Sdfr * particularly well defined. 7646283Sdfr * 7746283Sdfr * After I/we have seen more of these devices, it may be possible 7846283Sdfr * to generalize some of these bits. In particular, devices which 7946283Sdfr * describe themselves as communications/serial/16[45]50, and 8046283Sdfr * communications/parallel/??? might be attached via direct 8146283Sdfr * 'com' and 'lpt' attachments to pci. 8246283Sdfr */ 8346283Sdfr 8446283Sdfr#include "opt_puc.h" 8546283Sdfr 8646283Sdfr#include <sys/param.h> 8746283Sdfr#include <sys/systm.h> 8846283Sdfr#include <sys/kernel.h> 8946283Sdfr#include <sys/bus.h> 9046283Sdfr#include <sys/conf.h> 9146283Sdfr#include <sys/malloc.h> 9246283Sdfr 9346283Sdfr#include <machine/bus.h> 9446283Sdfr#include <machine/resource.h> 9546283Sdfr#include <sys/rman.h> 9646283Sdfr 9746283Sdfr#include <dev/pci/pcireg.h> 9846283Sdfr#include <dev/pci/pcivar.h> 9946283Sdfr 10046283Sdfr#define PUC_ENTRAILS 1 10146283Sdfr#include <dev/puc/pucvar.h> 10246283Sdfr 10346283Sdfrstruct puc_device { 10446283Sdfr struct resource_list resources; 10546283Sdfr u_int serialfreq; 10646283Sdfr}; 10746283Sdfr 10846283Sdfrstatic void puc_intr(void *arg); 10946283Sdfr 11046283Sdfrstatic int puc_find_free_unit(char *); 11146283Sdfr#ifdef PUC_DEBUG 11246283Sdfrstatic void puc_print_resource_list(struct resource_list *); 11346283Sdfr#endif 11446283Sdfr 11546283Sdfrdevclass_t puc_devclass; 11646283Sdfr 11746283Sdfrstatic int 11846283Sdfrpuc_port_bar_index(struct puc_softc *sc, int bar) 11946283Sdfr{ 12046283Sdfr int i; 12146283Sdfr 12246283Sdfr for (i = 0; i < PUC_MAX_BAR; i += 1) { 12346283Sdfr if (!sc->sc_bar_mappings[i].used) 12446283Sdfr break; 12546283Sdfr if (sc->sc_bar_mappings[i].bar == bar) 12646283Sdfr return (i); 12746283Sdfr } 12846283Sdfr sc->sc_bar_mappings[i].bar = bar; 12946283Sdfr sc->sc_bar_mappings[i].used = 1; 13046283Sdfr return (i); 13146283Sdfr} 13246283Sdfr 13346283Sdfrint 13446283Sdfrpuc_attach(device_t dev, const struct puc_device_description *desc) 13546283Sdfr{ 13646283Sdfr char *typestr; 13746283Sdfr 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 if (irq_setup == 0) 166 sc->fastintr = 1; 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 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 187 0ul, ~0ul, 1, RF_ACTIVE); 188 if (res == NULL) { 189 printf("could not get resource\n"); 190 continue; 191 } 192 sc->sc_bar_mappings[bidx].res = res; 193#ifdef PUC_DEBUG 194 printf("port rid %d bst %x, start %x, end %x\n", rid, 195 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 196 (u_int)rman_get_end(res)); 197#endif 198 } 199 200 if (desc->init != NULL) { 201 i = desc->init(sc); 202 if (i != 0) 203 return (i); 204 } 205 206 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 207 rid = sc->sc_desc->ports[i].bar; 208 bidx = puc_port_bar_index(sc, rid); 209 if (sc->sc_bar_mappings[bidx].res == NULL) 210 continue; 211 212 switch (sc->sc_desc->ports[i].type) { 213 case PUC_PORT_TYPE_COM: 214 typestr = "sio"; 215 break; 216 default: 217 continue; 218 } 219 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 220 M_NOWAIT | M_ZERO); 221 if (!pdev) 222 continue; 223 resource_list_init(&pdev->resources); 224 225 /* First fake up an IRQ resource. */ 226 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 227 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 228 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 229 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 230 rle->res = sc->irqres; 231 232 /* Now fake an IOPORT resource */ 233 res = sc->sc_bar_mappings[bidx].res; 234 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0, 235 rman_get_start(res) + sc->sc_desc->ports[i].offset, 236 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 237 8); 238 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0); 239 240 if (sc->barmuxed == 0) { 241 rle->res = sc->sc_bar_mappings[bidx].res; 242 } else { 243 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 244 M_WAITOK | M_ZERO); 245 if (rle->res == NULL) { 246 free(pdev, M_DEVBUF); 247 return (ENOMEM); 248 } 249 250 rle->res->r_start = rman_get_start(res) + 251 sc->sc_desc->ports[i].offset; 252 rle->res->r_end = rle->res->r_start + 8 - 1; 253 rle->res->r_bustag = rman_get_bustag(res); 254 bus_space_subregion(rle->res->r_bustag, 255 rman_get_bushandle(res), 256 sc->sc_desc->ports[i].offset, 8, 257 &rle->res->r_bushandle); 258 } 259 260 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 261 262 childunit = puc_find_free_unit(typestr); 263 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 264 if (sc->sc_ports[i].dev == NULL) { 265 if (sc->barmuxed) { 266 bus_space_unmap(rman_get_bustag(rle->res), 267 rman_get_bushandle(rle->res), 268 8); 269 free(rle->res, M_DEVBUF); 270 free(pdev, M_DEVBUF); 271 } 272 continue; 273 } 274 device_set_ivars(sc->sc_ports[i].dev, pdev); 275 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 276 if (!bootverbose) 277 device_quiet(sc->sc_ports[i].dev); 278#ifdef PUC_DEBUG 279 printf("puc: type %d, bar %x, offset %x\n", 280 sc->sc_desc->ports[i].type, 281 sc->sc_desc->ports[i].bar, 282 sc->sc_desc->ports[i].offset); 283 puc_print_resource_list(&pdev->resources); 284#endif 285 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 286 if (sc->barmuxed) { 287 bus_space_unmap(rman_get_bustag(rle->res), 288 rman_get_bushandle(rle->res), 289 8); 290 free(rle->res, M_DEVBUF); 291 free(pdev, M_DEVBUF); 292 } 293 } 294 } 295 296#ifdef PUC_DEBUG 297 bootverbose = 0; 298#endif 299 return (0); 300} 301 302/* 303 * This is just an brute force interrupt handler. It just calls all the 304 * registered handlers sequencially. 305 * 306 * Later on we should maybe have a different handler for boards that can 307 * tell us which device generated the interrupt. 308 */ 309static void 310puc_intr(void *arg) 311{ 312 int i; 313 struct puc_softc *sc; 314 315 sc = (struct puc_softc *)arg; 316 for (i = 0; i < PUC_MAX_PORTS; i++) 317 if (sc->sc_ports[i].ihand != NULL) 318 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 319} 320 321const struct puc_device_description * 322puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 323 uint32_t sprod) 324{ 325 int i; 326 327#define checkreg(val, index) \ 328 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 329 330 for (i = 0; puc_devices[i].name != NULL; i++) { 331 if (checkreg(vend, PUC_REG_VEND) && 332 checkreg(prod, PUC_REG_PROD) && 333 checkreg(svend, PUC_REG_SVEND) && 334 checkreg(sprod, PUC_REG_SPROD)) 335 return (&puc_devices[i]); 336 } 337 338#undef checkreg 339 340 return (NULL); 341} 342 343static int 344puc_find_free_unit(char *name) 345{ 346 devclass_t dc; 347 int start; 348 int unit; 349 350 unit = 0; 351 start = 0; 352 while (resource_int_value(name, unit, "port", &start) == 0 && 353 start > 0) 354 unit++; 355 dc = devclass_find(name); 356 if (dc == NULL) 357 return (-1); 358 while (devclass_get_device(dc, unit)) 359 unit++; 360#ifdef PUC_DEBUG 361 printf("puc: Using %s%d\n", name, unit); 362#endif 363 return (unit); 364} 365 366#ifdef PUC_DEBUG 367static void 368puc_print_resource_list(struct resource_list *rl) 369{ 370#if 0 371 struct resource_list_entry *rle; 372 373 printf("print_resource_list: rl %p\n", rl); 374 SLIST_FOREACH(rle, rl, link) 375 printf(" type %x, rid %x start %x end %x count %x\n", 376 rle->type, rle->rid, rle->start, rle->end, rle->count); 377 printf("print_resource_list: end.\n"); 378#endif 379} 380#endif 381 382struct resource * 383puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 384 u_long start, u_long end, u_long count, u_int flags) 385{ 386 struct puc_device *pdev; 387 struct resource *retval; 388 struct resource_list *rl; 389 struct resource_list_entry *rle; 390 391 pdev = device_get_ivars(child); 392 rl = &pdev->resources; 393 394#ifdef PUC_DEBUG 395 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 396 pdev, type, *rid); 397 puc_print_resource_list(rl); 398#endif 399 retval = NULL; 400 rle = resource_list_find(rl, type, *rid); 401 if (rle) { 402 start = rle->start; 403 end = rle->end; 404 count = rle->count; 405#ifdef PUC_DEBUG 406 printf("found rle, %lx, %lx, %lx\n", start, end, count); 407#endif 408 retval = rle->res; 409 } else 410 printf("oops rle is gone\n"); 411 412 return (retval); 413} 414 415int 416puc_release_resource(device_t dev, device_t child, int type, int rid, 417 struct resource *res) 418{ 419 return (0); 420} 421 422int 423puc_get_resource(device_t dev, device_t child, int type, int rid, 424 u_long *startp, u_long *countp) 425{ 426 struct puc_device *pdev; 427 struct resource_list *rl; 428 struct resource_list_entry *rle; 429 430 pdev = device_get_ivars(child); 431 rl = &pdev->resources; 432 433#ifdef PUC_DEBUG 434 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 435 type, rid); 436 puc_print_resource_list(rl); 437#endif 438 rle = resource_list_find(rl, type, rid); 439 if (rle) { 440#ifdef PUC_DEBUG 441 printf("found rle %p,", rle); 442#endif 443 if (startp != NULL) 444 *startp = rle->start; 445 if (countp != NULL) 446 *countp = rle->count; 447#ifdef PUC_DEBUG 448 printf(" %lx, %lx\n", rle->start, rle->count); 449#endif 450 return (0); 451 } else 452 printf("oops rle is gone\n"); 453 return (ENXIO); 454} 455 456int 457puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 458 void (*ihand)(void *), void *arg, void **cookiep) 459{ 460 int i; 461 struct puc_softc *sc; 462 463 sc = (struct puc_softc *)device_get_softc(dev); 464 if ((flags & INTR_FAST) && !sc->fastintr) 465 return (ENXIO); 466 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 467 if (sc->sc_ports[i].dev == child) { 468 if (sc->sc_ports[i].ihand != 0) 469 return (ENXIO); 470 sc->sc_ports[i].ihand = ihand; 471 sc->sc_ports[i].ihandarg = arg; 472 *cookiep = arg; 473 return (0); 474 } 475 } 476 return (ENXIO); 477} 478 479int 480puc_teardown_intr(device_t dev, device_t child, struct resource *r, 481 void *cookie) 482{ 483 int i; 484 struct puc_softc *sc; 485 486 sc = (struct puc_softc *)device_get_softc(dev); 487 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 488 if (sc->sc_ports[i].dev == child) { 489 sc->sc_ports[i].ihand = NULL; 490 sc->sc_ports[i].ihandarg = NULL; 491 return (0); 492 } 493 } 494 return (ENXIO); 495} 496 497int 498puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 499{ 500 struct puc_device *pdev; 501 502 pdev = device_get_ivars(child); 503 if (pdev == NULL) 504 return (ENOENT); 505 506 switch(index) { 507 case PUC_IVAR_FREQ: 508 *result = pdev->serialfreq; 509 break; 510 default: 511 return (ENOENT); 512 } 513 return (0); 514} 515