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