1/*-
2 * Copyright 2003 by Peter Grehan. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/powerpc/powermac/grackle.c 174782 2007-12-19 18:00:50Z marcel $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/kernel.h>
36
37#include <dev/ofw/openfirm.h>
38#include <dev/ofw/ofw_pci.h>
39
40#include <dev/pci/pcivar.h>
41#include <dev/pci/pcireg.h>
42
43#include <machine/bus.h>
44#include <machine/md_var.h>
45#include <machine/nexusvar.h>
46#include <machine/pio.h>
47#include <machine/resource.h>
48
49#include <sys/rman.h>
50
51#include <powerpc/ofw/ofw_pci.h>
52#include <powerpc/powermac/gracklevar.h>
53
54#include <vm/vm.h>
55#include <vm/pmap.h>
56
57#include "pcib_if.h"
58
59int badaddr(void *, size_t); /* XXX */
60
61/*
62 * Device interface.
63 */
64static int grackle_probe(device_t);
65static int grackle_attach(device_t);
66
67/*
68 * Bus interface.
69 */
70static int grackle_read_ivar(device_t, device_t, int,
71 uintptr_t *);
72static struct resource * grackle_alloc_resource(device_t bus,
73 device_t child, int type, int *rid, u_long start,
74 u_long end, u_long count, u_int flags);
75static int grackle_release_resource(device_t bus, device_t child,
76 int type, int rid, struct resource *res);
77static int grackle_activate_resource(device_t bus, device_t child,
78 int type, int rid, struct resource *res);
79static int grackle_deactivate_resource(device_t bus,
80 device_t child, int type, int rid,
81 struct resource *res);
82
83
84/*
85 * pcib interface.
86 */
87static int grackle_maxslots(device_t);
88static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int,
89 u_int, int);
90static void grackle_write_config(device_t, u_int, u_int, u_int,
91 u_int, u_int32_t, int);
92static int grackle_route_interrupt(device_t, device_t, int);
93
94/*
95 * Local routines.
96 */
97static int grackle_enable_config(struct grackle_softc *, u_int,
98 u_int, u_int, u_int);
99static void grackle_disable_config(struct grackle_softc *);
100
101/*
102 * Driver methods.
103 */
104static device_method_t grackle_methods[] = {
105 /* Device interface */
106 DEVMETHOD(device_probe, grackle_probe),
107 DEVMETHOD(device_attach, grackle_attach),
108
109 /* Bus interface */
110 DEVMETHOD(bus_print_child, bus_generic_print_child),
111 DEVMETHOD(bus_read_ivar, grackle_read_ivar),
112 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
113 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
114 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource),
115 DEVMETHOD(bus_release_resource, grackle_release_resource),
116 DEVMETHOD(bus_activate_resource, grackle_activate_resource),
117 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource),
118
119 /* pcib interface */
120 DEVMETHOD(pcib_maxslots, grackle_maxslots),
121 DEVMETHOD(pcib_read_config, grackle_read_config),
122 DEVMETHOD(pcib_write_config, grackle_write_config),
123 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt),
124
125 { 0, 0 }
126};
127
128static driver_t grackle_driver = {
129 "pcib",
130 grackle_methods,
131 sizeof(struct grackle_softc)
132};
133
134static devclass_t grackle_devclass;
135
136DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0);
137
138static int
139grackle_probe(device_t dev)
140{
141 char *type, *compatible;
142
143 type = nexus_get_device_type(dev);
144 compatible = nexus_get_compatible(dev);
145
146 if (type == NULL || compatible == NULL)
147 return (ENXIO);
148
149 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
150 return (ENXIO);
151
152 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
153 return (0);
154}
155
156static int
157grackle_attach(device_t dev)
158{
159 struct grackle_softc *sc;
160 phandle_t node;
161 u_int32_t busrange[2];
162 struct grackle_range *rp, *io, *mem[2];
163 int nmem, i;
164
165 node = nexus_get_node(dev);
166 sc = device_get_softc(dev);
167
168 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
169 return (ENXIO);
170
171 sc->sc_dev = dev;
172 sc->sc_node = node;
173 sc->sc_bus = busrange[0];
174
175 /*
176 * The Grackle PCI config addr/data registers are actually in
177 * PCI space, but since they are needed to actually probe the
178 * PCI bus, use the fact that they are also available directly
179 * on the processor bus and map them
180 */
181 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
182 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
183
184 bzero(sc->sc_range, sizeof(sc->sc_range));
185 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
186 sizeof(sc->sc_range));
187
188 if (sc->sc_nrange == -1) {
189 device_printf(dev, "could not get ranges\n");
190 return (ENXIO);
191 }
192
193 sc->sc_range[6].pci_hi = 0;
194 io = NULL;
195 nmem = 0;
196
197 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
198 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
199 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
200 break;
201 case OFW_PCI_PHYS_HI_SPACE_IO:
202 io = rp;
203 break;
204 case OFW_PCI_PHYS_HI_SPACE_MEM32:
205 mem[nmem] = rp;
206 nmem++;
207 break;
208 case OFW_PCI_PHYS_HI_SPACE_MEM64:
209 break;
210 }
211 }
212
213 if (io == NULL) {
214 device_printf(dev, "can't find io range\n");
215 return (ENXIO);
216 }
217 sc->sc_io_rman.rm_type = RMAN_ARRAY;
218 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports";
219 sc->sc_iostart = io->pci_iospace;
220 if (rman_init(&sc->sc_io_rman) != 0 ||
221 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
222 io->pci_lo + io->size_lo) != 0) {
223 device_printf(dev, "failed to set up io range management\n");
224 return (ENXIO);
225 }
226
227 if (nmem == 0) {
228 device_printf(dev, "can't find mem ranges\n");
229 return (ENXIO);
230 }
231 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
232 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory";
233 if (rman_init(&sc->sc_mem_rman) != 0) {
234 device_printf(dev,
235 "failed to init mem range resources\n");
236 return (ENXIO);
237 }
238 for (i = 0; i < nmem; i++) {
239 if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
240 mem[i]->pci_lo + mem[i]->size_lo) != 0) {
241 device_printf(dev,
242 "failed to set up memory range management\n");
243 return (ENXIO);
244 }
245 }
246
247 /*
248 * Write out the correct PIC interrupt values to config space
249 * of all devices on the bus.
250 */
251 ofw_pci_fixup(dev, sc->sc_bus, sc->sc_node);
252
253 device_add_child(dev, "pci", device_get_unit(dev));
254 return (bus_generic_attach(dev));
255}
256
257static int
258grackle_maxslots(device_t dev)
259{
260
261 return (PCI_SLOTMAX);
262}
263
264static u_int32_t
265grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
266 int width)
267{
268 struct grackle_softc *sc;
269 vm_offset_t caoff;
270 u_int32_t retval = 0xffffffff;
271
272 sc = device_get_softc(dev);
273 caoff = sc->sc_data + (reg & 0x03);
274
275 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
276
277 /*
278 * Config probes to non-existent devices on the
279 * secondary bus generates machine checks. Be sure
280 * to catch these.
281 */
282 if (bus > 0) {
283 if (badaddr((void *)sc->sc_data, 4)) {
284 return (retval);
285 }
286 }
287
288 switch (width) {
289 case 1:
290 retval = (in8rb(caoff));
291 break;
292 case 2:
293 retval = (in16rb(caoff));
294 break;
295 case 4:
296 retval = (in32rb(caoff));
297 break;
298 }
299 }
300 grackle_disable_config(sc);
301
302 return (retval);
303}
304
305static void
306grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
307 u_int reg, u_int32_t val, int width)
308{
309 struct grackle_softc *sc;
310 vm_offset_t caoff;
311
312 sc = device_get_softc(dev);
313 caoff = sc->sc_data + (reg & 0x03);
314
315 if (grackle_enable_config(sc, bus, slot, func, reg)) {
316 switch (width) {
317 case 1:
318 out8rb(caoff, val);
319 (void)in8rb(caoff);
320 break;
321 case 2:
322 out16rb(caoff, val);
323 (void)in16rb(caoff);
324 break;
325 case 4:
326 out32rb(caoff, val);
327 (void)in32rb(caoff);
328 break;
329 }
330 }
331 grackle_disable_config(sc);
332}
333
334static int
335grackle_route_interrupt(device_t bus, device_t dev, int pin)
336{
337
338 return (0);
339}
340
341static int
342grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
343{
344 struct grackle_softc *sc;
345
346 sc = device_get_softc(dev);
347
348 switch (which) {
349 case PCIB_IVAR_DOMAIN:
350 *result = 0;
351 return (0);
352 case PCIB_IVAR_BUS:
353 *result = sc->sc_bus;
354 return (0);
355 }
356
357 return (ENOENT);
358}
359
360static struct resource *
361grackle_alloc_resource(device_t bus, device_t child, int type, int *rid,
362 u_long start, u_long end, u_long count, u_int flags)
363{
364 struct grackle_softc *sc;
365 struct resource *rv;
366 struct rman *rm;
367 int needactivate;
368
369 needactivate = flags & RF_ACTIVE;
370 flags &= ~RF_ACTIVE;
371
372 sc = device_get_softc(bus);
373
374 switch (type) {
375 case SYS_RES_MEMORY:
376 rm = &sc->sc_mem_rman;
377 break;
378
379 case SYS_RES_IOPORT:
380 rm = &sc->sc_io_rman;
381 break;
382
383 case SYS_RES_IRQ:
384 return (bus_alloc_resource(bus, type, rid, start, end, count,
385 flags));
386
387 default:
388 device_printf(bus, "unknown resource request from %s\n",
389 device_get_nameunit(child));
390 return (NULL);
391 }
392
393 rv = rman_reserve_resource(rm, start, end, count, flags, child);
394 if (rv == NULL) {
395 device_printf(bus, "failed to reserve resource for %s\n",
396 device_get_nameunit(child));
397 return (NULL);
398 }
399
400 rman_set_rid(rv, *rid);
401 rman_set_bustag(rv, &bs_le_tag);
402 rman_set_bushandle(rv, rman_get_start(rv));
403
404 if (needactivate) {
405 if (bus_activate_resource(child, type, *rid, rv) != 0) {
406 device_printf(bus,
407 "failed to activate resource for %s\n",
408 device_get_nameunit(child));
409 rman_release_resource(rv);
410 return (NULL);
411 }
412 }
413
414 return (rv);
415}
416
417static int
418grackle_release_resource(device_t bus, device_t child, int type, int rid,
419 struct resource *res)
420{
421 if (rman_get_flags(res) & RF_ACTIVE) {
422 int error = bus_deactivate_resource(child, type, rid, res);
423 if (error)
424 return error;
425 }
426
427 return (rman_release_resource(res));
428}
429
430static int
431grackle_activate_resource(device_t bus, device_t child, int type, int rid,
432 struct resource *res)
433{
434 struct grackle_softc *sc;
435 void *p;
436
437 sc = device_get_softc(bus);
438
439 if (type == SYS_RES_IRQ) {
440 return (bus_activate_resource(bus, type, rid, res));
441 }
442 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
443 vm_offset_t start;
444
445 start = (vm_offset_t)rman_get_start(res);
446 /*
447 * For i/o-ports, convert the start address to the
448 * MPC106 PCI i/o window
449 */
450 if (type == SYS_RES_IOPORT)
451 start += sc->sc_iostart;
452
453 if (bootverbose)
454 printf("grackle mapdev: start %x, len %ld\n", start,
455 rman_get_size(res));
456
457 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
458 if (p == NULL)
459 return (ENOMEM);
460
461 rman_set_virtual(res, p);
462 rman_set_bushandle(res, (u_long)p);
463 }
464
465 return (rman_activate_resource(res));
466}
467
468static int
469grackle_deactivate_resource(device_t bus, device_t child, int type, int rid,
470 struct resource *res)
471{
472 /*
473 * If this is a memory resource, unmap it.
474 */
475 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
476 u_int32_t psize;
477
478 psize = rman_get_size(res);
479 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
480 }
481
482 return (rman_deactivate_resource(res));
483}
484
485
486static int
487grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
488 u_int func, u_int reg)
489{
490 u_int32_t cfgval;
491
492 /*
493 * Unlike UniNorth, the format of the config word is the same
494 * for local (0) and remote busses.
495 */
496 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
497 | GRACKLE_CFG_ENABLE;
498
499 out32rb(sc->sc_addr, cfgval);
500 (void) in32rb(sc->sc_addr);
501
502 return (1);
503}
504
505static void
506grackle_disable_config(struct grackle_softc *sc)
507{
508 /*
509 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
510 * accesses from causing config cycles
511 */
512 out32rb(sc->sc_addr, 0);
513}
514
515/*
516 * Driver to swallow Grackle host bridges from the PCI bus side.
517 */
518static int
519grackle_hb_probe(device_t dev)
520{
521
522 if (pci_get_devid(dev) == 0x00021057) {
523 device_set_desc(dev, "Grackle Host to PCI bridge");
524 device_quiet(dev);
525 return (0);
526 }
527
528 return (ENXIO);
529}
530
531static int
532grackle_hb_attach(device_t dev)
533{
534
535 return (0);
536}
537
538static device_method_t grackle_hb_methods[] = {
539 /* Device interface */
540 DEVMETHOD(device_probe, grackle_hb_probe),
541 DEVMETHOD(device_attach, grackle_hb_attach),
542
543 { 0, 0 }
544};
545
546static driver_t grackle_hb_driver = {
547 "grackle_hb",
548 grackle_hb_methods,
549 1,
550};
551static devclass_t grackle_hb_devclass;
552
553DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);
2 * Copyright 2003 by Peter Grehan. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/powerpc/powermac/grackle.c 174782 2007-12-19 18:00:50Z marcel $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/kernel.h>
36
37#include <dev/ofw/openfirm.h>
38#include <dev/ofw/ofw_pci.h>
39
40#include <dev/pci/pcivar.h>
41#include <dev/pci/pcireg.h>
42
43#include <machine/bus.h>
44#include <machine/md_var.h>
45#include <machine/nexusvar.h>
46#include <machine/pio.h>
47#include <machine/resource.h>
48
49#include <sys/rman.h>
50
51#include <powerpc/ofw/ofw_pci.h>
52#include <powerpc/powermac/gracklevar.h>
53
54#include <vm/vm.h>
55#include <vm/pmap.h>
56
57#include "pcib_if.h"
58
59int badaddr(void *, size_t); /* XXX */
60
61/*
62 * Device interface.
63 */
64static int grackle_probe(device_t);
65static int grackle_attach(device_t);
66
67/*
68 * Bus interface.
69 */
70static int grackle_read_ivar(device_t, device_t, int,
71 uintptr_t *);
72static struct resource * grackle_alloc_resource(device_t bus,
73 device_t child, int type, int *rid, u_long start,
74 u_long end, u_long count, u_int flags);
75static int grackle_release_resource(device_t bus, device_t child,
76 int type, int rid, struct resource *res);
77static int grackle_activate_resource(device_t bus, device_t child,
78 int type, int rid, struct resource *res);
79static int grackle_deactivate_resource(device_t bus,
80 device_t child, int type, int rid,
81 struct resource *res);
82
83
84/*
85 * pcib interface.
86 */
87static int grackle_maxslots(device_t);
88static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int,
89 u_int, int);
90static void grackle_write_config(device_t, u_int, u_int, u_int,
91 u_int, u_int32_t, int);
92static int grackle_route_interrupt(device_t, device_t, int);
93
94/*
95 * Local routines.
96 */
97static int grackle_enable_config(struct grackle_softc *, u_int,
98 u_int, u_int, u_int);
99static void grackle_disable_config(struct grackle_softc *);
100
101/*
102 * Driver methods.
103 */
104static device_method_t grackle_methods[] = {
105 /* Device interface */
106 DEVMETHOD(device_probe, grackle_probe),
107 DEVMETHOD(device_attach, grackle_attach),
108
109 /* Bus interface */
110 DEVMETHOD(bus_print_child, bus_generic_print_child),
111 DEVMETHOD(bus_read_ivar, grackle_read_ivar),
112 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
113 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
114 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource),
115 DEVMETHOD(bus_release_resource, grackle_release_resource),
116 DEVMETHOD(bus_activate_resource, grackle_activate_resource),
117 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource),
118
119 /* pcib interface */
120 DEVMETHOD(pcib_maxslots, grackle_maxslots),
121 DEVMETHOD(pcib_read_config, grackle_read_config),
122 DEVMETHOD(pcib_write_config, grackle_write_config),
123 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt),
124
125 { 0, 0 }
126};
127
128static driver_t grackle_driver = {
129 "pcib",
130 grackle_methods,
131 sizeof(struct grackle_softc)
132};
133
134static devclass_t grackle_devclass;
135
136DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0);
137
138static int
139grackle_probe(device_t dev)
140{
141 char *type, *compatible;
142
143 type = nexus_get_device_type(dev);
144 compatible = nexus_get_compatible(dev);
145
146 if (type == NULL || compatible == NULL)
147 return (ENXIO);
148
149 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
150 return (ENXIO);
151
152 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
153 return (0);
154}
155
156static int
157grackle_attach(device_t dev)
158{
159 struct grackle_softc *sc;
160 phandle_t node;
161 u_int32_t busrange[2];
162 struct grackle_range *rp, *io, *mem[2];
163 int nmem, i;
164
165 node = nexus_get_node(dev);
166 sc = device_get_softc(dev);
167
168 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
169 return (ENXIO);
170
171 sc->sc_dev = dev;
172 sc->sc_node = node;
173 sc->sc_bus = busrange[0];
174
175 /*
176 * The Grackle PCI config addr/data registers are actually in
177 * PCI space, but since they are needed to actually probe the
178 * PCI bus, use the fact that they are also available directly
179 * on the processor bus and map them
180 */
181 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
182 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
183
184 bzero(sc->sc_range, sizeof(sc->sc_range));
185 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
186 sizeof(sc->sc_range));
187
188 if (sc->sc_nrange == -1) {
189 device_printf(dev, "could not get ranges\n");
190 return (ENXIO);
191 }
192
193 sc->sc_range[6].pci_hi = 0;
194 io = NULL;
195 nmem = 0;
196
197 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
198 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
199 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
200 break;
201 case OFW_PCI_PHYS_HI_SPACE_IO:
202 io = rp;
203 break;
204 case OFW_PCI_PHYS_HI_SPACE_MEM32:
205 mem[nmem] = rp;
206 nmem++;
207 break;
208 case OFW_PCI_PHYS_HI_SPACE_MEM64:
209 break;
210 }
211 }
212
213 if (io == NULL) {
214 device_printf(dev, "can't find io range\n");
215 return (ENXIO);
216 }
217 sc->sc_io_rman.rm_type = RMAN_ARRAY;
218 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports";
219 sc->sc_iostart = io->pci_iospace;
220 if (rman_init(&sc->sc_io_rman) != 0 ||
221 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
222 io->pci_lo + io->size_lo) != 0) {
223 device_printf(dev, "failed to set up io range management\n");
224 return (ENXIO);
225 }
226
227 if (nmem == 0) {
228 device_printf(dev, "can't find mem ranges\n");
229 return (ENXIO);
230 }
231 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
232 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory";
233 if (rman_init(&sc->sc_mem_rman) != 0) {
234 device_printf(dev,
235 "failed to init mem range resources\n");
236 return (ENXIO);
237 }
238 for (i = 0; i < nmem; i++) {
239 if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
240 mem[i]->pci_lo + mem[i]->size_lo) != 0) {
241 device_printf(dev,
242 "failed to set up memory range management\n");
243 return (ENXIO);
244 }
245 }
246
247 /*
248 * Write out the correct PIC interrupt values to config space
249 * of all devices on the bus.
250 */
251 ofw_pci_fixup(dev, sc->sc_bus, sc->sc_node);
252
253 device_add_child(dev, "pci", device_get_unit(dev));
254 return (bus_generic_attach(dev));
255}
256
257static int
258grackle_maxslots(device_t dev)
259{
260
261 return (PCI_SLOTMAX);
262}
263
264static u_int32_t
265grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
266 int width)
267{
268 struct grackle_softc *sc;
269 vm_offset_t caoff;
270 u_int32_t retval = 0xffffffff;
271
272 sc = device_get_softc(dev);
273 caoff = sc->sc_data + (reg & 0x03);
274
275 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
276
277 /*
278 * Config probes to non-existent devices on the
279 * secondary bus generates machine checks. Be sure
280 * to catch these.
281 */
282 if (bus > 0) {
283 if (badaddr((void *)sc->sc_data, 4)) {
284 return (retval);
285 }
286 }
287
288 switch (width) {
289 case 1:
290 retval = (in8rb(caoff));
291 break;
292 case 2:
293 retval = (in16rb(caoff));
294 break;
295 case 4:
296 retval = (in32rb(caoff));
297 break;
298 }
299 }
300 grackle_disable_config(sc);
301
302 return (retval);
303}
304
305static void
306grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
307 u_int reg, u_int32_t val, int width)
308{
309 struct grackle_softc *sc;
310 vm_offset_t caoff;
311
312 sc = device_get_softc(dev);
313 caoff = sc->sc_data + (reg & 0x03);
314
315 if (grackle_enable_config(sc, bus, slot, func, reg)) {
316 switch (width) {
317 case 1:
318 out8rb(caoff, val);
319 (void)in8rb(caoff);
320 break;
321 case 2:
322 out16rb(caoff, val);
323 (void)in16rb(caoff);
324 break;
325 case 4:
326 out32rb(caoff, val);
327 (void)in32rb(caoff);
328 break;
329 }
330 }
331 grackle_disable_config(sc);
332}
333
334static int
335grackle_route_interrupt(device_t bus, device_t dev, int pin)
336{
337
338 return (0);
339}
340
341static int
342grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
343{
344 struct grackle_softc *sc;
345
346 sc = device_get_softc(dev);
347
348 switch (which) {
349 case PCIB_IVAR_DOMAIN:
350 *result = 0;
351 return (0);
352 case PCIB_IVAR_BUS:
353 *result = sc->sc_bus;
354 return (0);
355 }
356
357 return (ENOENT);
358}
359
360static struct resource *
361grackle_alloc_resource(device_t bus, device_t child, int type, int *rid,
362 u_long start, u_long end, u_long count, u_int flags)
363{
364 struct grackle_softc *sc;
365 struct resource *rv;
366 struct rman *rm;
367 int needactivate;
368
369 needactivate = flags & RF_ACTIVE;
370 flags &= ~RF_ACTIVE;
371
372 sc = device_get_softc(bus);
373
374 switch (type) {
375 case SYS_RES_MEMORY:
376 rm = &sc->sc_mem_rman;
377 break;
378
379 case SYS_RES_IOPORT:
380 rm = &sc->sc_io_rman;
381 break;
382
383 case SYS_RES_IRQ:
384 return (bus_alloc_resource(bus, type, rid, start, end, count,
385 flags));
386
387 default:
388 device_printf(bus, "unknown resource request from %s\n",
389 device_get_nameunit(child));
390 return (NULL);
391 }
392
393 rv = rman_reserve_resource(rm, start, end, count, flags, child);
394 if (rv == NULL) {
395 device_printf(bus, "failed to reserve resource for %s\n",
396 device_get_nameunit(child));
397 return (NULL);
398 }
399
400 rman_set_rid(rv, *rid);
401 rman_set_bustag(rv, &bs_le_tag);
402 rman_set_bushandle(rv, rman_get_start(rv));
403
404 if (needactivate) {
405 if (bus_activate_resource(child, type, *rid, rv) != 0) {
406 device_printf(bus,
407 "failed to activate resource for %s\n",
408 device_get_nameunit(child));
409 rman_release_resource(rv);
410 return (NULL);
411 }
412 }
413
414 return (rv);
415}
416
417static int
418grackle_release_resource(device_t bus, device_t child, int type, int rid,
419 struct resource *res)
420{
421 if (rman_get_flags(res) & RF_ACTIVE) {
422 int error = bus_deactivate_resource(child, type, rid, res);
423 if (error)
424 return error;
425 }
426
427 return (rman_release_resource(res));
428}
429
430static int
431grackle_activate_resource(device_t bus, device_t child, int type, int rid,
432 struct resource *res)
433{
434 struct grackle_softc *sc;
435 void *p;
436
437 sc = device_get_softc(bus);
438
439 if (type == SYS_RES_IRQ) {
440 return (bus_activate_resource(bus, type, rid, res));
441 }
442 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
443 vm_offset_t start;
444
445 start = (vm_offset_t)rman_get_start(res);
446 /*
447 * For i/o-ports, convert the start address to the
448 * MPC106 PCI i/o window
449 */
450 if (type == SYS_RES_IOPORT)
451 start += sc->sc_iostart;
452
453 if (bootverbose)
454 printf("grackle mapdev: start %x, len %ld\n", start,
455 rman_get_size(res));
456
457 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
458 if (p == NULL)
459 return (ENOMEM);
460
461 rman_set_virtual(res, p);
462 rman_set_bushandle(res, (u_long)p);
463 }
464
465 return (rman_activate_resource(res));
466}
467
468static int
469grackle_deactivate_resource(device_t bus, device_t child, int type, int rid,
470 struct resource *res)
471{
472 /*
473 * If this is a memory resource, unmap it.
474 */
475 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
476 u_int32_t psize;
477
478 psize = rman_get_size(res);
479 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
480 }
481
482 return (rman_deactivate_resource(res));
483}
484
485
486static int
487grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
488 u_int func, u_int reg)
489{
490 u_int32_t cfgval;
491
492 /*
493 * Unlike UniNorth, the format of the config word is the same
494 * for local (0) and remote busses.
495 */
496 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
497 | GRACKLE_CFG_ENABLE;
498
499 out32rb(sc->sc_addr, cfgval);
500 (void) in32rb(sc->sc_addr);
501
502 return (1);
503}
504
505static void
506grackle_disable_config(struct grackle_softc *sc)
507{
508 /*
509 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
510 * accesses from causing config cycles
511 */
512 out32rb(sc->sc_addr, 0);
513}
514
515/*
516 * Driver to swallow Grackle host bridges from the PCI bus side.
517 */
518static int
519grackle_hb_probe(device_t dev)
520{
521
522 if (pci_get_devid(dev) == 0x00021057) {
523 device_set_desc(dev, "Grackle Host to PCI bridge");
524 device_quiet(dev);
525 return (0);
526 }
527
528 return (ENXIO);
529}
530
531static int
532grackle_hb_attach(device_t dev)
533{
534
535 return (0);
536}
537
538static device_method_t grackle_hb_methods[] = {
539 /* Device interface */
540 DEVMETHOD(device_probe, grackle_hb_probe),
541 DEVMETHOD(device_attach, grackle_hb_attach),
542
543 { 0, 0 }
544};
545
546static driver_t grackle_hb_driver = {
547 "grackle_hb",
548 grackle_hb_methods,
549 1,
550};
551static devclass_t grackle_hb_devclass;
552
553DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);