2 * Copyright (C) 2008 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/module.h>
33 #include <sys/kernel.h>
35 #include <dev/ofw/openfirm.h>
36 #include <dev/ofw/ofw_pci.h>
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
41 #include <machine/bus.h>
42 #include <machine/md_var.h>
43 #include <machine/pio.h>
44 #include <machine/resource.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <powerpc/powermac/cpchtvar.h>
60 #include <isa/isavar.h>
63 static MALLOC_DEFINE(M_CPCHT, "cpcht", "CPC HT device information");
68 static int cpcht_probe(device_t);
69 static int cpcht_attach(device_t);
70 static ofw_bus_get_devinfo_t cpcht_get_devinfo;
73 static device_method_t cpcht_methods[] = {
74 /* Device interface */
75 DEVMETHOD(device_probe, cpcht_probe),
76 DEVMETHOD(device_attach, cpcht_attach),
79 DEVMETHOD(bus_print_child, bus_generic_print_child),
80 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
81 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
82 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
83 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
84 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
85 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
87 /* ofw_bus interface */
88 DEVMETHOD(ofw_bus_get_devinfo, cpcht_get_devinfo),
89 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
90 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
91 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
92 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
93 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
98 static driver_t cpcht_driver = {
104 static devclass_t cpcht_devclass;
106 DRIVER_MODULE(cpcht, nexus, cpcht_driver, cpcht_devclass, 0, 0);
109 cpcht_probe(device_t dev)
111 const char *type, *compatible;
113 type = ofw_bus_get_type(dev);
114 compatible = ofw_bus_get_compat(dev);
116 if (type == NULL || compatible == NULL)
119 if (strcmp(type, "ht") != 0)
122 if (strcmp(compatible, "u3-ht") == 0) {
123 device_set_desc(dev, "IBM CPC925 HyperTransport Tunnel");
125 } else if (strcmp(compatible,"u4-ht") == 0) {
126 device_set_desc(dev, "IBM CPC945 HyperTransport Tunnel");
134 cpcht_attach(device_t dev)
136 phandle_t root, child;
138 struct ofw_bus_devinfo *dinfo;
141 root = ofw_bus_get_node(dev);
143 if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
146 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
147 dinfo = malloc(sizeof(*dinfo), M_CPCHT, M_WAITOK | M_ZERO);
149 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
150 free(dinfo, M_CPCHT);
153 cdev = device_add_child(dev, NULL, -1);
155 device_printf(dev, "<%s>: device_add_child failed\n",
157 ofw_bus_gen_destroy_devinfo(dinfo);
158 free(dinfo, M_CPCHT);
161 device_set_ivars(cdev, dinfo);
164 return (bus_generic_attach(dev));
167 static const struct ofw_bus_devinfo *
168 cpcht_get_devinfo(device_t dev, device_t child)
170 return (device_get_ivars(child));
176 * CPC ISA Device interface.
178 static int cpcisa_probe(device_t);
183 static device_method_t cpcisa_methods[] = {
184 /* Device interface */
185 DEVMETHOD(device_probe, cpcisa_probe),
186 DEVMETHOD(device_attach, isab_attach),
189 DEVMETHOD(bus_print_child, bus_generic_print_child),
190 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
191 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
192 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
193 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
194 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
195 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
200 static driver_t cpcisa_driver = {
206 DRIVER_MODULE(cpcisa, cpcht, cpcisa_driver, isab_devclass, 0, 0);
209 cpcisa_probe(device_t dev)
213 type = ofw_bus_get_type(dev);
218 if (strcmp(type, "isa") != 0)
221 device_set_desc(dev, "HyperTransport-ISA bridge");
229 * CPC PCI Device interface.
231 static int cpcpci_probe(device_t);
232 static int cpcpci_attach(device_t);
237 static int cpcpci_read_ivar(device_t, device_t, int,
239 static struct resource * cpcpci_alloc_resource(device_t bus,
240 device_t child, int type, int *rid, u_long start,
241 u_long end, u_long count, u_int flags);
242 static int cpcpci_activate_resource(device_t bus, device_t child,
243 int type, int rid, struct resource *res);
248 static int cpcpci_maxslots(device_t);
249 static u_int32_t cpcpci_read_config(device_t, u_int, u_int, u_int,
251 static void cpcpci_write_config(device_t, u_int, u_int, u_int,
252 u_int, u_int32_t, int);
253 static int cpcpci_route_interrupt(device_t, device_t, int);
259 static phandle_t cpcpci_get_node(device_t bus, device_t child);
264 static device_method_t cpcpci_methods[] = {
265 /* Device interface */
266 DEVMETHOD(device_probe, cpcpci_probe),
267 DEVMETHOD(device_attach, cpcpci_attach),
270 DEVMETHOD(bus_print_child, bus_generic_print_child),
271 DEVMETHOD(bus_read_ivar, cpcpci_read_ivar),
272 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
273 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
274 DEVMETHOD(bus_alloc_resource, cpcpci_alloc_resource),
275 DEVMETHOD(bus_activate_resource, cpcpci_activate_resource),
278 DEVMETHOD(pcib_maxslots, cpcpci_maxslots),
279 DEVMETHOD(pcib_read_config, cpcpci_read_config),
280 DEVMETHOD(pcib_write_config, cpcpci_write_config),
281 DEVMETHOD(pcib_route_interrupt, cpcpci_route_interrupt),
283 /* ofw_bus interface */
284 DEVMETHOD(ofw_bus_get_node, cpcpci_get_node),
288 static driver_t cpcpci_driver = {
291 sizeof(struct cpcpci_softc)
294 static devclass_t cpcpci_devclass;
296 DRIVER_MODULE(cpcpci, cpcht, cpcpci_driver, cpcpci_devclass, 0, 0);
299 cpcpci_probe(device_t dev)
303 type = ofw_bus_get_type(dev);
308 if (strcmp(type, "pci") != 0)
311 device_set_desc(dev, "HyperTransport-PCI bridge");
317 cpcpci_attach(device_t dev)
319 struct cpcpci_softc *sc;
321 u_int32_t reg[2], busrange[2], config_base;
322 struct cpcpci_range *rp, *io, *mem[2];
323 struct cpcpci_range fakeio;
326 node = ofw_bus_get_node(dev);
327 sc = device_get_softc(dev);
329 if (OF_getprop(OF_parent(node), "reg", reg, sizeof(reg)) < 8)
332 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
337 sc->sc_bus = busrange[0];
338 config_base = reg[1];
340 config_base += 0x01000000UL + (sc->sc_bus << 16);
341 sc->sc_data = (vm_offset_t)pmap_mapdev(config_base, PAGE_SIZE << 4);
343 bzero(sc->sc_range, sizeof(sc->sc_range));
344 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
345 sizeof(sc->sc_range));
347 if (sc->sc_nrange == -1) {
348 device_printf(dev, "could not get ranges\n");
352 sc->sc_range[6].pci_hi = 0;
356 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
357 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
358 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
360 case OFW_PCI_PHYS_HI_SPACE_IO:
363 case OFW_PCI_PHYS_HI_SPACE_MEM32:
367 case OFW_PCI_PHYS_HI_SPACE_MEM64:
374 * On at least some machines, the I/O port range is
375 * not exported in the OF device tree. So hardcode it.
379 fakeio.pci_lo = reg[1];
380 fakeio.size_lo = 0x00400000;
382 fakeio.pci_lo += 0x02000000UL + (sc->sc_bus << 14);
385 sc->sc_io_rman.rm_type = RMAN_ARRAY;
386 sc->sc_io_rman.rm_descr = "CPC 9xx PCI I/O Ports";
387 sc->sc_iostart = io->host_lo;
388 if (rman_init(&sc->sc_io_rman) != 0 ||
389 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
390 io->pci_lo + io->size_lo - 1) != 0) {
391 device_printf(dev, "failed to set up io range management\n");
396 device_printf(dev, "can't find mem ranges\n");
399 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
400 sc->sc_mem_rman.rm_descr = "CPC 9xx PCI Memory";
401 if (rman_init(&sc->sc_mem_rman) != 0) {
403 "failed to init mem range resources\n");
406 for (i = 0; i < nmem; i++) {
407 if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
408 mem[i]->pci_lo + mem[i]->size_lo - 1) != 0) {
410 "failed to set up memory range management\n");
415 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
417 device_add_child(dev, "pci", device_get_unit(dev));
419 return (bus_generic_attach(dev));
423 cpcpci_maxslots(device_t dev)
426 return (PCI_SLOTMAX);
430 cpcpci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
433 struct cpcpci_softc *sc;
436 sc = device_get_softc(dev);
437 caoff = sc->sc_data +
438 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
442 return (in8rb(caoff));
445 return (in16rb(caoff));
448 return (in32rb(caoff));
456 cpcpci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
457 u_int reg, u_int32_t val, int width)
459 struct cpcpci_softc *sc;
462 sc = device_get_softc(dev);
463 caoff = sc->sc_data +
464 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
480 cpcpci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
482 struct cpcpci_softc *sc;
484 sc = device_get_softc(dev);
487 case PCIB_IVAR_DOMAIN:
488 *result = device_get_unit(dev);
491 *result = sc->sc_bus;
498 static struct resource *
499 cpcpci_alloc_resource(device_t bus, device_t child, int type, int *rid,
500 u_long start, u_long end, u_long count, u_int flags)
502 struct cpcpci_softc *sc;
507 needactivate = flags & RF_ACTIVE;
510 sc = device_get_softc(bus);
514 rm = &sc->sc_mem_rman;
518 rm = &sc->sc_io_rman;
524 return (bus_alloc_resource(bus, type, rid, start, end, count,
528 device_printf(bus, "unknown resource request from %s\n",
529 device_get_nameunit(child));
533 rv = rman_reserve_resource(rm, start, end, count, flags, child);
535 device_printf(bus, "failed to reserve resource for %s\n",
536 device_get_nameunit(child));
540 rman_set_rid(rv, *rid);
543 if (bus_activate_resource(child, type, *rid, rv) != 0) {
545 "failed to activate resource for %s\n",
546 device_get_nameunit(child));
547 rman_release_resource(rv);
556 cpcpci_activate_resource(device_t bus, device_t child, int type, int rid,
557 struct resource *res)
560 struct cpcpci_softc *sc;
562 sc = device_get_softc(bus);
564 if (type == SYS_RES_IRQ)
565 return (bus_activate_resource(bus, type, rid, res));
567 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
570 start = (vm_offset_t)rman_get_start(res);
572 * For i/o-ports, convert the start address to the
575 if (type == SYS_RES_IOPORT)
576 start += sc->sc_iostart;
579 printf("cpcpci mapdev: start %x, len %ld\n", start,
582 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
585 rman_set_virtual(res, p);
586 rman_set_bustag(res, &bs_le_tag);
587 rman_set_bushandle(res, (u_long)p);
590 return (rman_activate_resource(res));
594 cpcpci_get_node(device_t bus, device_t dev)
596 struct cpcpci_softc *sc;
598 sc = device_get_softc(bus);
599 /* We only have one child, the PCI bus, which needs our own node. */
600 return (sc->sc_node);
604 cpcpci_route_interrupt(device_t bus, device_t dev, int pin)
606 struct cpcpci_softc *sc;
607 struct ofw_pci_register reg;
608 uint32_t pintr, mintr;
609 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
611 sc = device_get_softc(bus);
613 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®,
614 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr), maskbuf))
617 /* Maybe it's a real interrupt, not an intpin */
621 device_printf(bus, "could not route pin %d for device %d.%d\n",
622 pin, pci_get_slot(dev), pci_get_function(dev));
623 return (PCI_INVALID_IRQ);