FreeBSD/Linux Kernel Cross Reference
sys/dev/puc/puc.c
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * 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$");
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 #define __RMAN_RESOURCE_VISIBLE /* Shouldn't be there */
85 #include "opt_puc.h"
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 #ifdef __sparc64__
101 #include <sparc64/fhc/fhcreg.h>
102 #endif
103
104 #define PUC_ENTRAILS 1
105 #include <dev/puc/pucvar.h>
106
107 struct puc_device {
108 struct resource_list resources;
109 int port;
110 int regshft;
111 u_int serialfreq;
112 u_int subtype;
113 };
114
115 static void puc_intr(void *arg);
116
117 static int puc_find_free_unit(char *);
118 #ifdef PUC_DEBUG
119 static void puc_print_resource_list(struct resource_list *);
120 #endif
121
122 devclass_t puc_devclass;
123
124 static int
125 puc_port_bar_index(struct puc_softc *sc, int bar)
126 {
127 int i;
128
129 for (i = 0; i < PUC_MAX_BAR; i += 1) {
130 if (!sc->sc_bar_mappings[i].used)
131 break;
132 if (sc->sc_bar_mappings[i].bar == bar)
133 return (i);
134 }
135 sc->sc_bar_mappings[i].bar = bar;
136 sc->sc_bar_mappings[i].used = 1;
137 return (i);
138 }
139
140 static int
141 puc_probe_ilr(struct puc_softc *sc, struct resource *res)
142 {
143 u_char t1, t2;
144 int i;
145
146 switch (sc->sc_desc.ilr_type) {
147 case PUC_ILR_TYPE_DIGI:
148 sc->ilr_st = rman_get_bustag(res);
149 sc->ilr_sh = rman_get_bushandle(res);
150 for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) {
151 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
152 sc->sc_desc.ilr_offset[i]);
153 t1 = ~t1;
154 bus_space_write_1(sc->ilr_st, sc->ilr_sh,
155 sc->sc_desc.ilr_offset[i], t1);
156 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
157 sc->sc_desc.ilr_offset[i]);
158 if (t2 == t1)
159 return (0);
160 }
161 return (1);
162
163 default:
164 break;
165 }
166 return (0);
167 }
168
169 int
170 puc_attach(device_t dev, const struct puc_device_description *desc)
171 {
172 char *typestr;
173 int bidx, childunit, i, irq_setup, ressz, rid, type;
174 struct puc_softc *sc;
175 struct puc_device *pdev;
176 struct resource *res;
177 struct resource_list_entry *rle;
178 bus_space_handle_t bh;
179
180 if (desc == NULL)
181 return (ENXIO);
182
183 sc = (struct puc_softc *)device_get_softc(dev);
184 bzero(sc, sizeof(*sc));
185 sc->sc_desc = *desc;
186
187 #ifdef PUC_DEBUG
188 bootverbose = 1;
189
190 printf("puc: name: %s\n", sc->sc_desc.name);
191 #endif
192
193 rid = 0;
194 #ifdef __sparc64__
195 if (strcmp(device_get_name(device_get_parent(dev)), "fhc") == 0)
196 rid = FHC_UART;
197 #endif
198 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
199 RF_ACTIVE | RF_SHAREABLE);
200 if (!res)
201 return (ENXIO);
202
203 sc->irqres = res;
204 sc->irqrid = rid;
205 #ifdef PUC_FASTINTR
206 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
207 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
208 if (irq_setup == 0)
209 sc->fastintr = INTR_FAST;
210 else
211 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
212 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
213 #else
214 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
215 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
216 #endif
217 if (irq_setup != 0)
218 return (ENXIO);
219
220 rid = 0;
221 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
222 if (i > 0 && rid == sc->sc_desc.ports[i].bar)
223 sc->barmuxed = 1;
224 rid = sc->sc_desc.ports[i].bar;
225 bidx = puc_port_bar_index(sc, rid);
226
227 if (sc->sc_bar_mappings[bidx].res != NULL)
228 continue;
229
230 type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY)
231 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
232
233 res = bus_alloc_resource_any(dev, type, &rid,
234 RF_ACTIVE);
235 if (res == NULL &&
236 sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) {
237 type = (type == SYS_RES_IOPORT)
238 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
239 res = bus_alloc_resource_any(dev, type, &rid,
240 RF_ACTIVE);
241 }
242 if (res == NULL) {
243 device_printf(dev, "could not get resource\n");
244 continue;
245 }
246 sc->sc_bar_mappings[bidx].type = type;
247 sc->sc_bar_mappings[bidx].res = res;
248
249 if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) {
250 sc->ilr_enabled = puc_probe_ilr(sc, res);
251 if (sc->ilr_enabled)
252 device_printf(dev, "ILR enabled\n");
253 else
254 device_printf(dev, "ILR disabled\n");
255 }
256 #ifdef PUC_DEBUG
257 printf("%s rid %d bst %lx, start %lx, end %lx\n",
258 (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
259 (u_long)rman_get_bustag(res), (u_long)rman_get_start(res),
260 (u_long)rman_get_end(res));
261 #endif
262 }
263
264 if (desc->init != NULL) {
265 i = desc->init(sc);
266 if (i != 0)
267 return (i);
268 }
269
270 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
271 rid = sc->sc_desc.ports[i].bar;
272 bidx = puc_port_bar_index(sc, rid);
273 if (sc->sc_bar_mappings[bidx].res == NULL)
274 continue;
275
276 switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) {
277 case PUC_PORT_TYPE_COM:
278 typestr = "sio";
279 break;
280 case PUC_PORT_TYPE_LPT:
281 typestr = "ppc";
282 break;
283 case PUC_PORT_TYPE_UART:
284 typestr = "uart";
285 break;
286 default:
287 continue;
288 }
289 switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) {
290 case PUC_PORT_UART_SAB82532:
291 ressz = 64;
292 break;
293 case PUC_PORT_UART_Z8530:
294 ressz = 2;
295 break;
296 default:
297 ressz = 8;
298 break;
299 }
300 pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
301 M_NOWAIT | M_ZERO);
302 if (!pdev)
303 continue;
304 resource_list_init(&pdev->resources);
305
306 /* First fake up an IRQ resource. */
307 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
308 rman_get_start(sc->irqres), rman_get_end(sc->irqres),
309 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
310 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
311 rle->res = sc->irqres;
312
313 /* Now fake an IOPORT or MEMORY resource */
314 res = sc->sc_bar_mappings[bidx].res;
315 type = sc->sc_bar_mappings[bidx].type;
316 resource_list_add(&pdev->resources, type, 0,
317 rman_get_start(res) + sc->sc_desc.ports[i].offset,
318 rman_get_start(res) + sc->sc_desc.ports[i].offset
319 + ressz - 1, ressz);
320 rle = resource_list_find(&pdev->resources, type, 0);
321
322 if (sc->barmuxed == 0) {
323 rle->res = sc->sc_bar_mappings[bidx].res;
324 } else {
325 rle->res = malloc(sizeof(struct resource), M_DEVBUF,
326 M_WAITOK | M_ZERO);
327 if (rle->res == NULL) {
328 free(pdev, M_DEVBUF);
329 return (ENOMEM);
330 }
331
332 rman_set_start(rle->res, rman_get_start(res) +
333 sc->sc_desc.ports[i].offset);
334 rman_set_end(rle->res, rman_get_start(rle->res) +
335 ressz - 1);
336 rman_set_bustag(rle->res, rman_get_bustag(res));
337 bus_space_subregion(rman_get_bustag(rle->res),
338 rman_get_bushandle(res),
339 sc->sc_desc.ports[i].offset, ressz,
340 &bh);
341 rman_set_bushandle(rle->res, bh);
342 }
343
344 pdev->port = i + 1;
345 pdev->serialfreq = sc->sc_desc.ports[i].serialfreq;
346 pdev->subtype = sc->sc_desc.ports[i].type &
347 PUC_PORT_SUBTYPE_MASK;
348 pdev->regshft = sc->sc_desc.ports[i].regshft;
349
350 childunit = puc_find_free_unit(typestr);
351 if (childunit < 0 && strcmp(typestr, "uart") != 0) {
352 typestr = "uart";
353 childunit = puc_find_free_unit(typestr);
354 }
355 sc->sc_ports[i].dev = device_add_child(dev, typestr,
356 childunit);
357 if (sc->sc_ports[i].dev == NULL) {
358 if (sc->barmuxed) {
359 bus_space_unmap(rman_get_bustag(rle->res),
360 rman_get_bushandle(rle->res), ressz);
361 free(rle->res, M_DEVBUF);
362 free(pdev, M_DEVBUF);
363 }
364 continue;
365 }
366 device_set_ivars(sc->sc_ports[i].dev, pdev);
367 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name);
368 #ifdef PUC_DEBUG
369 printf("puc: type %d, bar %x, offset %x\n",
370 sc->sc_desc.ports[i].type,
371 sc->sc_desc.ports[i].bar,
372 sc->sc_desc.ports[i].offset);
373 puc_print_resource_list(&pdev->resources);
374 #endif
375 device_set_flags(sc->sc_ports[i].dev,
376 sc->sc_desc.ports[i].flags);
377 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
378 if (sc->barmuxed) {
379 bus_space_unmap(rman_get_bustag(rle->res),
380 rman_get_bushandle(rle->res), ressz);
381 free(rle->res, M_DEVBUF);
382 free(pdev, M_DEVBUF);
383 }
384 }
385 }
386
387 #ifdef PUC_DEBUG
388 bootverbose = 0;
389 #endif
390 return (0);
391 }
392
393 static u_int32_t
394 puc_ilr_read(struct puc_softc *sc)
395 {
396 u_int32_t mask;
397 int i;
398
399 mask = 0;
400 switch (sc->sc_desc.ilr_type) {
401 case PUC_ILR_TYPE_DIGI:
402 for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) {
403 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
404 sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff);
405 }
406 break;
407
408 default:
409 mask = 0xffffffff;
410 break;
411 }
412 return (mask);
413 }
414
415 /*
416 * This is an interrupt handler. For boards that can't tell us which
417 * device generated the interrupt it just calls all the registered
418 * handlers sequencially, but for boards that can tell us which
419 * device(s) generated the interrupt it calls only handlers for devices
420 * that actually generated the interrupt.
421 */
422 static void
423 puc_intr(void *arg)
424 {
425 int i;
426 u_int32_t ilr_mask;
427 struct puc_softc *sc;
428
429 sc = (struct puc_softc *)arg;
430 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
431 for (i = 0; i < PUC_MAX_PORTS; i++)
432 if (sc->sc_ports[i].ihand != NULL &&
433 ((ilr_mask >> i) & 0x00000001))
434 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
435 }
436
437 static int
438 puc_find_free_unit(char *name)
439 {
440 devclass_t dc;
441 int start;
442 int unit;
443
444 unit = 0;
445 start = 0;
446 while (resource_int_value(name, unit, "port", &start) == 0 &&
447 start > 0)
448 unit++;
449 dc = devclass_find(name);
450 if (dc == NULL)
451 return (-1);
452 while (devclass_get_device(dc, unit))
453 unit++;
454 #ifdef PUC_DEBUG
455 printf("puc: Using %s%d\n", name, unit);
456 #endif
457 return (unit);
458 }
459
460 #ifdef PUC_DEBUG
461 static void
462 puc_print_resource_list(struct resource_list *rl)
463 {
464 #if 0
465 struct resource_list_entry *rle;
466
467 printf("print_resource_list: rl %p\n", rl);
468 SLIST_FOREACH(rle, rl, link)
469 printf(" type %x, rid %x start %lx end %lx count %lx\n",
470 rle->type, rle->rid, rle->start, rle->end, rle->count);
471 printf("print_resource_list: end.\n");
472 #endif
473 }
474 #endif
475
476 struct resource *
477 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
478 u_long start, u_long end, u_long count, u_int flags)
479 {
480 struct puc_device *pdev;
481 struct resource *retval;
482 struct resource_list *rl;
483 struct resource_list_entry *rle;
484 device_t my_child;
485
486 /*
487 * in the case of a child of child we need to find our immediate child
488 */
489 for (my_child = child; device_get_parent(my_child) != dev;
490 my_child = device_get_parent(my_child));
491
492 pdev = device_get_ivars(my_child);
493 rl = &pdev->resources;
494
495 #ifdef PUC_DEBUG
496 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
497 pdev, type, *rid);
498 puc_print_resource_list(rl);
499 #endif
500 retval = NULL;
501 rle = resource_list_find(rl, type, *rid);
502 if (rle) {
503 #ifdef PUC_DEBUG
504 printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end,
505 rle->count);
506 #endif
507 retval = rle->res;
508 }
509 #ifdef PUC_DEBUG
510 else
511 printf("oops rle is gone\n");
512 #endif
513
514 return (retval);
515 }
516
517 int
518 puc_release_resource(device_t dev, device_t child, int type, int rid,
519 struct resource *res)
520 {
521 return (0);
522 }
523
524 int
525 puc_get_resource(device_t dev, device_t child, int type, int rid,
526 u_long *startp, u_long *countp)
527 {
528 struct puc_device *pdev;
529 struct resource_list *rl;
530 struct resource_list_entry *rle;
531
532 pdev = device_get_ivars(child);
533 rl = &pdev->resources;
534
535 #ifdef PUC_DEBUG
536 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
537 type, rid);
538 puc_print_resource_list(rl);
539 #endif
540 rle = resource_list_find(rl, type, rid);
541 if (rle) {
542 #ifdef PUC_DEBUG
543 printf("found rle %p,", rle);
544 #endif
545 if (startp != NULL)
546 *startp = rle->start;
547 if (countp != NULL)
548 *countp = rle->count;
549 #ifdef PUC_DEBUG
550 printf(" %lx, %lx\n", rle->start, rle->count);
551 #endif
552 return (0);
553 } else
554 printf("oops rle is gone\n");
555 return (ENXIO);
556 }
557
558 int
559 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
560 void (*ihand)(void *), void *arg, void **cookiep)
561 {
562 int i;
563 struct puc_softc *sc;
564
565 sc = (struct puc_softc *)device_get_softc(dev);
566 if ((flags & INTR_FAST) != sc->fastintr)
567 return (ENXIO);
568 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
569 if (sc->sc_ports[i].dev == child) {
570 if (sc->sc_ports[i].ihand != 0)
571 return (ENXIO);
572 sc->sc_ports[i].ihand = ihand;
573 sc->sc_ports[i].ihandarg = arg;
574 *cookiep = arg;
575 return (0);
576 }
577 }
578 return (ENXIO);
579 }
580
581 int
582 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
583 void *cookie)
584 {
585 int i;
586 struct puc_softc *sc;
587
588 sc = (struct puc_softc *)device_get_softc(dev);
589 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
590 if (sc->sc_ports[i].dev == child) {
591 sc->sc_ports[i].ihand = NULL;
592 sc->sc_ports[i].ihandarg = NULL;
593 return (0);
594 }
595 }
596 return (ENXIO);
597 }
598
599 int
600 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
601 {
602 struct puc_device *pdev;
603
604 pdev = device_get_ivars(child);
605 if (pdev == NULL)
606 return (ENOENT);
607
608 switch(index) {
609 case PUC_IVAR_FREQ:
610 *result = pdev->serialfreq;
611 break;
612 case PUC_IVAR_PORT:
613 *result = pdev->port;
614 break;
615 case PUC_IVAR_REGSHFT:
616 *result = pdev->regshft;
617 break;
618 case PUC_IVAR_SUBTYPE:
619 *result = pdev->subtype;
620 break;
621 default:
622 return (ENOENT);
623 }
624 return (0);
625 }
Cache object: ea4f307aefd821c227052b67484955b9
|