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 #define PUC_ENTRAILS 1
101 #include <dev/puc/pucvar.h>
102
103 struct puc_device {
104 struct resource_list resources;
105 int port;
106 int regshft;
107 u_int serialfreq;
108 u_int subtype;
109 };
110
111 static void puc_intr(void *arg);
112
113 static int puc_find_free_unit(char *);
114 #ifdef PUC_DEBUG
115 static void puc_print_resource_list(struct resource_list *);
116 #endif
117
118 devclass_t puc_devclass;
119
120 static int
121 puc_port_bar_index(struct puc_softc *sc, int bar)
122 {
123 int i;
124
125 for (i = 0; i < PUC_MAX_BAR; i += 1) {
126 if (!sc->sc_bar_mappings[i].used)
127 break;
128 if (sc->sc_bar_mappings[i].bar == bar)
129 return (i);
130 }
131 if (i == PUC_MAX_BAR) {
132 printf("%s: out of bars!\n", __func__);
133 return (-1);
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 rid = 0;
193 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
194 RF_ACTIVE | RF_SHAREABLE);
195 if (!res)
196 return (ENXIO);
197
198 sc->irqres = res;
199 sc->irqrid = rid;
200 #ifdef PUC_FASTINTR
201 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
202 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
203 if (irq_setup == 0)
204 sc->fastintr = INTR_FAST;
205 else
206 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
207 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
208 #else
209 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
210 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
211 #endif
212 if (irq_setup != 0)
213 return (ENXIO);
214
215 rid = 0;
216 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
217 if (i > 0 && rid == sc->sc_desc.ports[i].bar)
218 sc->barmuxed = 1;
219 rid = sc->sc_desc.ports[i].bar;
220 bidx = puc_port_bar_index(sc, rid);
221
222 if (bidx < 0 || sc->sc_bar_mappings[bidx].res != NULL)
223 continue;
224
225 type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY)
226 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
227
228 res = bus_alloc_resource_any(dev, type, &rid,
229 RF_ACTIVE);
230 if (res == NULL &&
231 sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) {
232 type = (type == SYS_RES_IOPORT)
233 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
234 res = bus_alloc_resource_any(dev, type, &rid,
235 RF_ACTIVE);
236 }
237 if (res == NULL) {
238 device_printf(dev, "could not get resource\n");
239 continue;
240 }
241 sc->sc_bar_mappings[bidx].type = type;
242 sc->sc_bar_mappings[bidx].res = res;
243
244 if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) {
245 sc->ilr_enabled = puc_probe_ilr(sc, res);
246 if (sc->ilr_enabled)
247 device_printf(dev, "ILR enabled\n");
248 else
249 device_printf(dev, "ILR disabled\n");
250 }
251 #ifdef PUC_DEBUG
252 printf("%s rid %d bst %lx, start %lx, end %lx\n",
253 (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
254 (u_long)rman_get_bustag(res), (u_long)rman_get_start(res),
255 (u_long)rman_get_end(res));
256 #endif
257 }
258
259 if (desc->init != NULL) {
260 i = desc->init(sc);
261 if (i != 0)
262 return (i);
263 }
264
265 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
266 rid = sc->sc_desc.ports[i].bar;
267 bidx = puc_port_bar_index(sc, rid);
268 if (bidx < 0 || sc->sc_bar_mappings[bidx].res == NULL)
269 continue;
270
271 switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) {
272 case PUC_PORT_TYPE_COM:
273 typestr = "sio";
274 break;
275 case PUC_PORT_TYPE_LPT:
276 typestr = "ppc";
277 break;
278 case PUC_PORT_TYPE_UART:
279 typestr = "uart";
280 break;
281 default:
282 continue;
283 }
284 switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) {
285 case PUC_PORT_UART_SAB82532:
286 ressz = 64;
287 break;
288 case PUC_PORT_UART_Z8530:
289 ressz = 2;
290 break;
291 default:
292 ressz = 8;
293 break;
294 }
295 pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
296 M_NOWAIT | M_ZERO);
297 if (!pdev)
298 continue;
299 resource_list_init(&pdev->resources);
300
301 /* First fake up an IRQ resource. */
302 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
303 rman_get_start(sc->irqres), rman_get_end(sc->irqres),
304 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
305 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
306 rle->res = sc->irqres;
307
308 /* Now fake an IOPORT or MEMORY resource */
309 res = sc->sc_bar_mappings[bidx].res;
310 type = sc->sc_bar_mappings[bidx].type;
311 resource_list_add(&pdev->resources, type, 0,
312 rman_get_start(res) + sc->sc_desc.ports[i].offset,
313 rman_get_start(res) + sc->sc_desc.ports[i].offset
314 + ressz - 1, ressz);
315 rle = resource_list_find(&pdev->resources, type, 0);
316
317 if (sc->barmuxed == 0) {
318 rle->res = sc->sc_bar_mappings[bidx].res;
319 } else {
320 rle->res = malloc(sizeof(struct resource), M_DEVBUF,
321 M_WAITOK | M_ZERO);
322 if (rle->res == NULL) {
323 free(pdev, M_DEVBUF);
324 return (ENOMEM);
325 }
326
327 rman_set_start(rle->res, rman_get_start(res) +
328 sc->sc_desc.ports[i].offset);
329 rman_set_end(rle->res, rman_get_start(rle->res) +
330 ressz - 1);
331 rman_set_bustag(rle->res, rman_get_bustag(res));
332 bus_space_subregion(rman_get_bustag(rle->res),
333 rman_get_bushandle(res),
334 sc->sc_desc.ports[i].offset, ressz,
335 &bh);
336 rman_set_bushandle(rle->res, bh);
337 }
338
339 pdev->port = i + 1;
340 pdev->serialfreq = sc->sc_desc.ports[i].serialfreq;
341 pdev->subtype = sc->sc_desc.ports[i].type &
342 PUC_PORT_SUBTYPE_MASK;
343 pdev->regshft = sc->sc_desc.ports[i].regshft;
344
345 childunit = puc_find_free_unit(typestr);
346 if (childunit < 0 && strcmp(typestr, "uart") != 0) {
347 typestr = "uart";
348 childunit = puc_find_free_unit(typestr);
349 }
350 sc->sc_ports[i].dev = device_add_child(dev, typestr,
351 childunit);
352 if (sc->sc_ports[i].dev == NULL) {
353 if (sc->barmuxed) {
354 bus_space_unmap(rman_get_bustag(rle->res),
355 rman_get_bushandle(rle->res), ressz);
356 free(rle->res, M_DEVBUF);
357 free(pdev, M_DEVBUF);
358 }
359 continue;
360 }
361 device_set_ivars(sc->sc_ports[i].dev, pdev);
362 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name);
363 #ifdef PUC_DEBUG
364 printf("puc: type %d, bar %x, offset %x\n",
365 sc->sc_desc.ports[i].type,
366 sc->sc_desc.ports[i].bar,
367 sc->sc_desc.ports[i].offset);
368 puc_print_resource_list(&pdev->resources);
369 #endif
370 device_set_flags(sc->sc_ports[i].dev,
371 sc->sc_desc.ports[i].flags);
372 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
373 if (sc->barmuxed) {
374 bus_space_unmap(rman_get_bustag(rle->res),
375 rman_get_bushandle(rle->res), ressz);
376 free(rle->res, M_DEVBUF);
377 free(pdev, M_DEVBUF);
378 }
379 }
380 }
381
382 #ifdef PUC_DEBUG
383 bootverbose = 0;
384 #endif
385 return (0);
386 }
387
388 static u_int32_t
389 puc_ilr_read(struct puc_softc *sc)
390 {
391 u_int32_t mask;
392 int i;
393
394 mask = 0;
395 switch (sc->sc_desc.ilr_type) {
396 case PUC_ILR_TYPE_DIGI:
397 for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) {
398 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
399 sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff);
400 }
401 break;
402
403 default:
404 mask = 0xffffffff;
405 break;
406 }
407 return (mask);
408 }
409
410 /*
411 * This is an interrupt handler. For boards that can't tell us which
412 * device generated the interrupt it just calls all the registered
413 * handlers sequencially, but for boards that can tell us which
414 * device(s) generated the interrupt it calls only handlers for devices
415 * that actually generated the interrupt.
416 */
417 static void
418 puc_intr(void *arg)
419 {
420 int i;
421 u_int32_t ilr_mask;
422 struct puc_softc *sc;
423
424 sc = (struct puc_softc *)arg;
425 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
426 for (i = 0; i < PUC_MAX_PORTS; i++)
427 if (sc->sc_ports[i].ihand != NULL &&
428 ((ilr_mask >> i) & 0x00000001))
429 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
430 }
431
432 static int
433 puc_find_free_unit(char *name)
434 {
435 devclass_t dc;
436 int start;
437 int unit;
438
439 unit = 0;
440 start = 0;
441 while (resource_int_value(name, unit, "port", &start) == 0 &&
442 start > 0)
443 unit++;
444 dc = devclass_find(name);
445 if (dc == NULL)
446 return (-1);
447 while (devclass_get_device(dc, unit))
448 unit++;
449 #ifdef PUC_DEBUG
450 printf("puc: Using %s%d\n", name, unit);
451 #endif
452 return (unit);
453 }
454
455 #ifdef PUC_DEBUG
456 static void
457 puc_print_resource_list(struct resource_list *rl)
458 {
459 #if 0
460 struct resource_list_entry *rle;
461
462 printf("print_resource_list: rl %p\n", rl);
463 SLIST_FOREACH(rle, rl, link)
464 printf(" type %x, rid %x start %lx end %lx count %lx\n",
465 rle->type, rle->rid, rle->start, rle->end, rle->count);
466 printf("print_resource_list: end.\n");
467 #endif
468 }
469 #endif
470
471 struct resource *
472 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
473 u_long start, u_long end, u_long count, u_int flags)
474 {
475 struct puc_device *pdev;
476 struct resource *retval;
477 struct resource_list *rl;
478 struct resource_list_entry *rle;
479 device_t my_child;
480
481 /*
482 * in the case of a child of child we need to find our immediate child
483 */
484 for (my_child = child; device_get_parent(my_child) != dev;
485 my_child = device_get_parent(my_child));
486
487 pdev = device_get_ivars(my_child);
488 rl = &pdev->resources;
489
490 #ifdef PUC_DEBUG
491 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
492 pdev, type, *rid);
493 puc_print_resource_list(rl);
494 #endif
495 retval = NULL;
496 rle = resource_list_find(rl, type, *rid);
497 if (rle) {
498 #ifdef PUC_DEBUG
499 printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end,
500 rle->count);
501 #endif
502 retval = rle->res;
503 }
504 #ifdef PUC_DEBUG
505 else
506 printf("oops rle is gone\n");
507 #endif
508
509 return (retval);
510 }
511
512 int
513 puc_release_resource(device_t dev, device_t child, int type, int rid,
514 struct resource *res)
515 {
516 return (0);
517 }
518
519 int
520 puc_get_resource(device_t dev, device_t child, int type, int rid,
521 u_long *startp, u_long *countp)
522 {
523 struct puc_device *pdev;
524 struct resource_list *rl;
525 struct resource_list_entry *rle;
526
527 pdev = device_get_ivars(child);
528 rl = &pdev->resources;
529
530 #ifdef PUC_DEBUG
531 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
532 type, rid);
533 puc_print_resource_list(rl);
534 #endif
535 rle = resource_list_find(rl, type, rid);
536 if (rle) {
537 #ifdef PUC_DEBUG
538 printf("found rle %p,", rle);
539 #endif
540 if (startp != NULL)
541 *startp = rle->start;
542 if (countp != NULL)
543 *countp = rle->count;
544 #ifdef PUC_DEBUG
545 printf(" %lx, %lx\n", rle->start, rle->count);
546 #endif
547 return (0);
548 } else
549 printf("oops rle is gone\n");
550 return (ENXIO);
551 }
552
553 int
554 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
555 void (*ihand)(void *), void *arg, void **cookiep)
556 {
557 int i;
558 struct puc_softc *sc;
559
560 sc = (struct puc_softc *)device_get_softc(dev);
561 if ((flags & INTR_FAST) != sc->fastintr)
562 return (ENXIO);
563 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
564 if (sc->sc_ports[i].dev == child) {
565 if (sc->sc_ports[i].ihand != 0)
566 return (ENXIO);
567 sc->sc_ports[i].ihand = ihand;
568 sc->sc_ports[i].ihandarg = arg;
569 *cookiep = arg;
570 return (0);
571 }
572 }
573 return (ENXIO);
574 }
575
576 int
577 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
578 void *cookie)
579 {
580 int i;
581 struct puc_softc *sc;
582
583 sc = (struct puc_softc *)device_get_softc(dev);
584 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
585 if (sc->sc_ports[i].dev == child) {
586 sc->sc_ports[i].ihand = NULL;
587 sc->sc_ports[i].ihandarg = NULL;
588 return (0);
589 }
590 }
591 return (ENXIO);
592 }
593
594 int
595 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
596 {
597 struct puc_device *pdev;
598
599 pdev = device_get_ivars(child);
600 if (pdev == NULL)
601 return (ENOENT);
602
603 switch(index) {
604 case PUC_IVAR_FREQ:
605 *result = pdev->serialfreq;
606 break;
607 case PUC_IVAR_PORT:
608 *result = pdev->port;
609 break;
610 case PUC_IVAR_REGSHFT:
611 *result = pdev->regshft;
612 break;
613 case PUC_IVAR_SUBTYPE:
614 *result = pdev->subtype;
615 break;
616 default:
617 return (ENOENT);
618 }
619 return (0);
620 }
Cache object: 193f53366f47530528adb6af7203360b
|