]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/powerpc/mpc85xx/pci_ocp.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / powerpc / mpc85xx / pci_ocp.c
1 /*-
2  * Copyright 2006-2007 by Juniper Networks.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * 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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ktr.h>
35 #include <sys/sockio.h>
36 #include <sys/mbuf.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/queue.h>
42 #include <sys/bus.h>
43 #include <sys/rman.h>
44 #include <sys/endian.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcib_private.h>
52
53 #include "pcib_if.h"
54
55 #include <machine/resource.h>
56 #include <machine/bus.h>
57 #include <machine/intr_machdep.h>
58 #include <machine/ocpbus.h>
59 #include <machine/spr.h>
60
61 #include <powerpc/mpc85xx/ocpbus.h>
62
63 #define REG_CFG_ADDR    0x0000
64 #define CONFIG_ACCESS_ENABLE    0x80000000
65
66 #define REG_CFG_DATA    0x0004
67 #define REG_INT_ACK     0x0008
68
69 #define REG_POTAR(n)    (0x0c00 + 0x20 * (n))
70 #define REG_POTEAR(n)   (0x0c04 + 0x20 * (n))
71 #define REG_POWBAR(n)   (0x0c08 + 0x20 * (n))
72 #define REG_POWAR(n)    (0x0c10 + 0x20 * (n))
73
74 #define REG_PITAR(n)    (0x0e00 - 0x20 * (n))
75 #define REG_PIWBAR(n)   (0x0e08 - 0x20 * (n))
76 #define REG_PIWBEAR(n)  (0x0e0c - 0x20 * (n))
77 #define REG_PIWAR(n)    (0x0e10 - 0x20 * (n))
78
79 #define DEVFN(b, s, f)  ((b << 16) | (s << 8) | f)
80
81 struct pci_ocp_softc {
82         device_t        sc_dev;
83
84         struct rman     sc_iomem;
85         bus_addr_t      sc_iomem_va;            /* Virtual mapping. */
86         bus_addr_t      sc_iomem_alloc;         /* Next allocation. */
87         struct rman     sc_ioport;
88         bus_addr_t      sc_ioport_va;           /* Virtual mapping. */
89         bus_addr_t      sc_ioport_alloc;        /* Next allocation. */
90
91         struct resource *sc_res;
92         bus_space_handle_t sc_bsh;
93         bus_space_tag_t sc_bst;
94         int             sc_rid;
95
96         int             sc_busnr;
97         int             sc_pcie:1;
98
99         /* Devices that need special attention. */
100         int             sc_devfn_tundra;
101         int             sc_devfn_via_ide;
102 };
103
104 static int pci_ocp_attach(device_t);
105 static int pci_ocp_probe(device_t);
106
107 static struct resource *pci_ocp_alloc_resource(device_t, device_t, int, int *,
108     u_long, u_long, u_long, u_int);
109 static int pci_ocp_read_ivar(device_t, device_t, int, uintptr_t *);
110 static int pci_ocp_release_resource(device_t, device_t, int, int,
111     struct resource *);
112 static int pci_ocp_write_ivar(device_t, device_t, int, uintptr_t);
113
114 static int pci_ocp_maxslots(device_t);
115 static uint32_t pci_ocp_read_config(device_t, u_int, u_int, u_int, u_int, int);
116 static void pci_ocp_write_config(device_t, u_int, u_int, u_int, u_int,
117     uint32_t, int);
118
119 /*
120  * Bus interface definitions.
121  */
122 static device_method_t pci_ocp_methods[] = {
123         /* Device interface */
124         DEVMETHOD(device_probe,         pci_ocp_probe),
125         DEVMETHOD(device_attach,        pci_ocp_attach),
126
127         /* Bus interface */
128         DEVMETHOD(bus_print_child,      bus_generic_print_child),
129         DEVMETHOD(bus_read_ivar,        pci_ocp_read_ivar),
130         DEVMETHOD(bus_write_ivar,       pci_ocp_write_ivar),
131         DEVMETHOD(bus_alloc_resource,   pci_ocp_alloc_resource),
132         DEVMETHOD(bus_release_resource, pci_ocp_release_resource),
133         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
134         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
135         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
136         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
137
138         /* pcib interface */
139         DEVMETHOD(pcib_maxslots,        pci_ocp_maxslots),
140         DEVMETHOD(pcib_read_config,     pci_ocp_read_config),
141         DEVMETHOD(pcib_write_config,    pci_ocp_write_config),
142         DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
143
144         { 0, 0 }
145 };
146
147 static driver_t pci_ocp_driver = {
148         "pcib",
149         pci_ocp_methods,
150         sizeof(struct pci_ocp_softc),
151 };
152
153 devclass_t pcib_devclass;
154
155 DRIVER_MODULE(pcib, ocpbus, pci_ocp_driver, pcib_devclass, 0, 0);
156
157 static uint32_t
158 pci_ocp_cfgread(struct pci_ocp_softc *sc, u_int bus, u_int slot, u_int func,
159     u_int reg, int bytes)
160 {
161         uint32_t addr, data;
162
163         if (bus == sc->sc_busnr)
164                 bus = 0;
165
166         addr = CONFIG_ACCESS_ENABLE;
167         addr |= (bus & 0xff) << 16;
168         addr |= (slot & 0x1f) << 11;
169         addr |= (func & 0x7) << 8;
170         addr |= reg & 0xfc;
171         if (sc->sc_pcie)
172                 addr |= (reg & 0xf00) << 16;
173         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
174
175         switch (bytes) {
176         case 1:
177                 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
178                     REG_CFG_DATA + (reg & 3));
179                 break;
180         case 2:
181                 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
182                     REG_CFG_DATA + (reg & 2)));
183                 break;
184         case 4:
185                 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
186                     REG_CFG_DATA));
187                 break;
188         default:
189                 data = ~0;
190                 break;
191         }
192         return (data);
193 }
194
195 static void
196 pci_ocp_cfgwrite(struct pci_ocp_softc *sc, u_int bus, u_int slot, u_int func,
197     u_int reg, uint32_t data, int bytes)
198 {
199         uint32_t addr;
200
201         if (bus == sc->sc_busnr)
202                 bus = 0;
203
204         addr = CONFIG_ACCESS_ENABLE;
205         addr |= (bus & 0xff) << 16;
206         addr |= (slot & 0x1f) << 11;
207         addr |= (func & 0x7) << 8;
208         addr |= reg & 0xfc;
209         if (sc->sc_pcie)
210                 addr |= (reg & 0xf00) << 16;
211         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
212
213         switch (bytes) {
214         case 1:
215                 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
216                     REG_CFG_DATA + (reg & 3), data);
217                 break;
218         case 2:
219                 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
220                     REG_CFG_DATA + (reg & 2), htole16(data));
221                 break;
222         case 4:
223                 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
224                     REG_CFG_DATA, htole32(data));
225                 break;
226         }
227 }
228
229 #if 0
230 static void
231 dump(struct pci_ocp_softc *sc)
232 {
233         unsigned int i;
234
235 #define RD(o)   bus_space_read_4(sc->sc_bst, sc->sc_bsh, o)
236         for (i = 0; i < 5; i++) {
237                 printf("POTAR%u  =0x%08x\n", i, RD(REG_POTAR(i)));
238                 printf("POTEAR%u =0x%08x\n", i, RD(REG_POTEAR(i)));
239                 printf("POWBAR%u =0x%08x\n", i, RD(REG_POWBAR(i)));
240                 printf("POWAR%u  =0x%08x\n", i, RD(REG_POWAR(i)));
241         }
242         printf("\n");
243         for (i = 1; i < 4; i++) {
244                 printf("PITAR%u  =0x%08x\n", i, RD(REG_PITAR(i)));
245                 printf("PIWBAR%u =0x%08x\n", i, RD(REG_PIWBAR(i)));
246                 printf("PIWBEAR%u=0x%08x\n", i, RD(REG_PIWBEAR(i)));
247                 printf("PIWAR%u  =0x%08x\n", i, RD(REG_PIWAR(i)));
248         }
249         printf("\n");
250 #undef RD
251
252         for (i = 0; i < 0x48; i += 4) {
253                 printf("cfg%02x=0x%08x\n", i, pci_ocp_cfgread(sc, 0, 0, 0,
254                     i, 4));
255         }
256 }
257 #endif
258
259 static int
260 pci_ocp_maxslots(device_t dev)
261 {
262         struct pci_ocp_softc *sc = device_get_softc(dev);
263
264         return ((sc->sc_pcie) ? 0 : 30);
265 }
266
267 static uint32_t
268 pci_ocp_read_config(device_t dev, u_int bus, u_int slot, u_int func,
269     u_int reg, int bytes)
270 {
271         struct pci_ocp_softc *sc = device_get_softc(dev);
272         u_int devfn;
273
274         if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
275                 return (~0);
276         devfn = DEVFN(bus, slot, func);
277         if (devfn == sc->sc_devfn_tundra)
278                 return (~0);
279         if (devfn == sc->sc_devfn_via_ide && reg == PCIR_INTPIN)
280                 return (1);
281         return (pci_ocp_cfgread(sc, bus, slot, func, reg, bytes));
282 }
283
284 static void
285 pci_ocp_write_config(device_t dev, u_int bus, u_int slot, u_int func,
286     u_int reg, uint32_t val, int bytes)
287 {
288         struct pci_ocp_softc *sc = device_get_softc(dev);
289
290         if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
291                 return;
292         pci_ocp_cfgwrite(sc, bus, slot, func, reg, val, bytes);
293 }
294
295 static int
296 pci_ocp_probe(device_t dev)
297 {
298         char buf[128];
299         struct pci_ocp_softc *sc;
300         const char *mpcid, *type;
301         device_t parent;
302         u_long start, size;
303         uintptr_t devtype;
304         uint32_t cfgreg;
305         int error;
306
307         parent = device_get_parent(dev);
308         error = BUS_READ_IVAR(parent, dev, OCPBUS_IVAR_DEVTYPE, &devtype);
309         if (error)
310                 return (error);
311         if (devtype != OCPBUS_DEVTYPE_PCIB)
312                 return (ENXIO);
313
314         sc = device_get_softc(dev);
315
316         sc->sc_rid = 0;
317         sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
318             RF_ACTIVE);
319         if (sc->sc_res == NULL)
320                 return (ENXIO);
321
322         sc->sc_bst = rman_get_bustag(sc->sc_res);
323         sc->sc_bsh = rman_get_bushandle(sc->sc_res);
324         sc->sc_busnr = 0;
325
326         error = ENOENT;
327         cfgreg = pci_ocp_cfgread(sc, 0, 0, 0, PCIR_VENDOR, 2);
328         if (cfgreg != 0x1057 && cfgreg != 0x1957)
329                 goto out;
330         cfgreg = pci_ocp_cfgread(sc, 0, 0, 0, PCIR_DEVICE, 2);
331         switch (cfgreg) {
332         case 0x000a:
333                 mpcid = "8555E";
334                 break;
335         case 0x0012:
336                 mpcid = "8548E";
337                 break;
338         case 0x0013:
339                 mpcid = "8548";
340                 break;
341         /*
342          * Documentation from Freescale is incorrect.
343          * Use right values after documentation is corrected.
344          */
345         case 0x0030:
346                 mpcid = "8544E";
347                 break;
348         case 0x0031:
349                 mpcid = "8544";
350                 break;
351         case 0x0032:
352                 mpcid = "8544";
353                 break;
354         default:
355                 goto out;
356         }
357
358         type = "PCI";
359         cfgreg = pci_ocp_cfgread(sc, 0, 0, 0, PCIR_CAP_PTR, 1);
360         while (cfgreg != 0) {
361                 cfgreg = pci_ocp_cfgread(sc, 0, 0, 0, cfgreg, 2);
362                 switch (cfgreg & 0xff) {
363                 case PCIY_PCIX:         /* PCI-X */
364                         type = "PCI-X";
365                         break;
366                 case PCIY_EXPRESS:      /* PCI Express */
367                         type = "PCI Express";
368                         sc->sc_pcie = 1;
369                         break;
370                 }
371                 cfgreg = (cfgreg >> 8) & 0xff;
372         }
373
374         error = bus_get_resource(dev, SYS_RES_MEMORY, 1, &start, &size);
375         if (error || start == 0 || size == 0)
376                 goto out;
377
378         snprintf(buf, sizeof(buf),
379             "Freescale MPC%s %s host controller", mpcid, type);
380         device_set_desc_copy(dev, buf);
381         error = BUS_PROBE_DEFAULT;
382
383 out:
384         bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
385         return (error);
386 }
387
388 static void
389 pci_ocp_init_via(struct pci_ocp_softc *sc, uint16_t device, int bus,
390     int slot, int fn)
391 {
392
393         if (device == 0x0686) {
394                 pci_ocp_write_config(sc->sc_dev, bus, slot, fn, 0x52, 0x34, 1);
395                 pci_ocp_write_config(sc->sc_dev, bus, slot, fn, 0x77, 0x00, 1);
396                 pci_ocp_write_config(sc->sc_dev, bus, slot, fn, 0x83, 0x98, 1);
397                 pci_ocp_write_config(sc->sc_dev, bus, slot, fn, 0x85, 0x03, 1);
398         } else if (device == 0x0571) {
399                 sc->sc_devfn_via_ide = DEVFN(bus, slot, fn);
400                 pci_ocp_write_config(sc->sc_dev, bus, slot, fn, 0x40, 0x0b, 1);
401         }
402 }
403
404 static int
405 pci_ocp_init_bar(struct pci_ocp_softc *sc, int bus, int slot, int func,
406     int barno)
407 {
408         bus_addr_t *allocp;
409         uint32_t addr, mask, size;
410         int reg, width;
411
412         reg = PCIR_BAR(barno);
413
414         if (DEVFN(bus, slot, func) == sc->sc_devfn_via_ide) {
415                 switch (barno) {
416                 case 0: addr = 0x1f0; break;
417                 case 1: addr = 0x3f4; break;
418                 case 2: addr = 0x170; break;
419                 case 3: addr = 0x374; break;
420                 case 4: addr = 0xcc0; break;
421                 default: return (1);
422                 }
423                 pci_ocp_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
424                 return (1);
425         }
426
427         pci_ocp_write_config(sc->sc_dev, bus, slot, func, reg, ~0, 4);
428         size = pci_ocp_read_config(sc->sc_dev, bus, slot, func, reg, 4);
429         if (size == 0)
430                 return (1);
431         width = ((size & 7) == 4) ? 2 : 1;
432
433         if (size & 1) {         /* I/O port */
434                 allocp = &sc->sc_ioport_alloc;
435                 size &= ~3;
436                 if ((size & 0xffff0000) == 0)
437                         size |= 0xffff0000;
438         } else {                /* memory */
439                 allocp = &sc->sc_iomem_alloc;
440                 size &= ~15;
441         }
442         mask = ~size;
443         size = mask + 1;
444         /* Sanity check (must be a power of 2). */
445         if (size & mask)
446                 return (width);
447
448         addr = (*allocp + mask) & ~mask;
449         *allocp = addr + size;
450
451         if (bootverbose)
452                 printf("PCI %u:%u:%u:%u: reg %x: size=%08x: addr=%08x\n",
453                     device_get_unit(sc->sc_dev), bus, slot, func, reg,
454                     size, addr);
455
456         pci_ocp_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
457         if (width == 2)
458                 pci_ocp_write_config(sc->sc_dev, bus, slot, func, reg + 4,
459                     0, 4);
460         return (width);
461 }
462
463 static u_int
464 pci_ocp_route_int(struct pci_ocp_softc *sc, u_int bus, u_int slot, u_int func,
465     u_int intpin)
466 {
467         u_int devfn, intline;
468
469         devfn = DEVFN(bus, slot, func);
470         if (devfn == sc->sc_devfn_via_ide)
471                 intline = 14;
472         else if (devfn == sc->sc_devfn_via_ide + 1)
473                 intline = 10;
474         else if (devfn == sc->sc_devfn_via_ide + 2)
475                 intline = 10;
476         else {
477                 if (intpin != 0) {
478                         intline = intpin - 1;
479                         intline += (bus != sc->sc_busnr) ? slot : 0;
480                         intline = PIC_IRQ_EXT(intline & 3);
481                 } else
482                         intline = 0xff;
483         }
484
485         if (bootverbose)
486                 printf("PCI %u:%u:%u:%u: intpin %u: intline=%u\n",
487                     device_get_unit(sc->sc_dev), bus, slot, func,
488                     intpin, intline);
489
490         return (intline);
491 }
492
493 static int
494 pci_ocp_init(struct pci_ocp_softc *sc, int bus, int maxslot)
495 {
496         int secbus, slot;
497         int func, maxfunc;
498         int bar, maxbar;
499         uint16_t vendor, device;
500         uint8_t command, hdrtype, class, subclass;
501         uint8_t intline, intpin;
502
503         secbus = bus;
504         for (slot = 0; slot < maxslot; slot++) {
505                 maxfunc = 0;
506                 for (func = 0; func <= maxfunc; func++) {
507                         hdrtype = pci_ocp_read_config(sc->sc_dev, bus, slot,
508                             func, PCIR_HDRTYPE, 1);
509                         if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
510                                 continue;
511
512                         if (func == 0 && (hdrtype & PCIM_MFDEV))
513                                 maxfunc = PCI_FUNCMAX;
514
515                         vendor = pci_ocp_read_config(sc->sc_dev, bus, slot,
516                             func, PCIR_VENDOR, 2);
517                         device = pci_ocp_read_config(sc->sc_dev, bus, slot,
518                             func, PCIR_DEVICE, 2);
519
520                         if (vendor == 0x1957 && device == 0x3fff) {
521                                 sc->sc_devfn_tundra = DEVFN(bus, slot, func);
522                                 continue;
523                         }
524
525                         command = pci_ocp_read_config(sc->sc_dev, bus, slot,
526                             func, PCIR_COMMAND, 1);
527                         command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
528                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
529                             PCIR_COMMAND, command, 1);
530
531                         if (vendor == 0x1106)
532                                 pci_ocp_init_via(sc, device, bus, slot, func);
533
534                         /* Program the base address registers. */
535                         maxbar = (hdrtype & PCIM_HDRTYPE) ? 1 : 6;
536                         bar = 0;
537                         while (bar < maxbar)
538                                 bar += pci_ocp_init_bar(sc, bus, slot, func,
539                                     bar);
540
541                         /* Perform interrupt routing. */
542                         intpin = pci_ocp_read_config(sc->sc_dev, bus, slot,
543                             func, PCIR_INTPIN, 1);
544                         intline = pci_ocp_route_int(sc, bus, slot, func,
545                             intpin);
546                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
547                             PCIR_INTLINE, intline, 1);
548
549                         command |= PCIM_CMD_MEMEN | PCIM_CMD_PORTEN;
550                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
551                             PCIR_COMMAND, command, 1);
552
553                         /*
554                          * Handle PCI-PCI bridges
555                          */
556                         class = pci_ocp_read_config(sc->sc_dev, bus, slot,
557                             func, PCIR_CLASS, 1);
558                         if (class != PCIC_BRIDGE)
559                                 continue;
560                         subclass = pci_ocp_read_config(sc->sc_dev, bus, slot,
561                             func, PCIR_SUBCLASS, 1);
562                         if (subclass != PCIS_BRIDGE_PCI)
563                                 continue;
564
565                         secbus++;
566
567                         /* Program I/O decoder. */
568                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
569                             PCIR_IOBASEL_1, sc->sc_ioport.rm_start >> 8, 1);
570                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
571                             PCIR_IOLIMITL_1, sc->sc_ioport.rm_end >> 8, 1);
572                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
573                             PCIR_IOBASEH_1, sc->sc_ioport.rm_start >> 16, 2);
574                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
575                             PCIR_IOLIMITH_1, sc->sc_ioport.rm_end >> 16, 2);
576
577                         /* Program (non-prefetchable) memory decoder. */
578                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
579                             PCIR_MEMBASE_1, sc->sc_iomem.rm_start >> 16, 2);
580                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
581                             PCIR_MEMLIMIT_1, sc->sc_iomem.rm_end >> 16, 2);
582
583                         /* Program prefetchable memory decoder. */
584                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
585                             PCIR_PMBASEL_1, 0x0010, 2);
586                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
587                             PCIR_PMLIMITL_1, 0x000f, 2);
588                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
589                             PCIR_PMBASEH_1, 0x00000000, 4);
590                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
591                             PCIR_PMLIMITH_1, 0x00000000, 4);
592
593                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
594                             PCIR_PRIBUS_1, bus, 1);
595                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
596                             PCIR_SECBUS_1, secbus, 1);
597                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
598                             PCIR_SUBBUS_1, 0xff, 1);
599
600                         secbus = pci_ocp_init(sc, secbus,
601                             (subclass == PCIS_BRIDGE_PCI) ? 31 : 1);
602
603                         pci_ocp_write_config(sc->sc_dev, bus, slot, func,
604                             PCIR_SUBBUS_1, secbus, 1);
605                 }
606         }
607
608         return (secbus);
609 }
610
611 static void
612 pci_ocp_inbound(struct pci_ocp_softc *sc, int wnd, int tgt, u_long start,
613     u_long size, u_long pci_start)
614 {
615         uint32_t attr, bar, tar;
616
617         KASSERT(wnd > 0, ("%s: inbound window 0 is invalid", __func__));
618
619         switch (tgt) {
620         case OCP85XX_TGTIF_RAM1:
621                 attr = 0xa0f55000 | (ffsl(size) - 2);
622                 break;
623         default:
624                 attr = 0;
625                 break;
626         }
627         tar = start >> 12;
628         bar = pci_start >> 12;
629
630         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PITAR(wnd), tar);
631         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBEAR(wnd), 0);
632         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBAR(wnd), bar);
633         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWAR(wnd), attr);
634 }
635
636 static void
637 pci_ocp_outbound(struct pci_ocp_softc *sc, int wnd, int res, u_long start,
638     u_long size, u_long pci_start)
639 {
640         uint32_t attr, bar, tar;
641
642         switch (res) {
643         case SYS_RES_MEMORY:
644                 attr = 0x80044000 | (ffsl(size) - 2);
645                 break;
646         case SYS_RES_IOPORT:
647                 attr = 0x80088000 | (ffsl(size) - 2);
648                 break;
649         default:
650                 attr = 0x0004401f;
651                 break;
652         }
653         bar = start >> 12;
654         tar = pci_start >> 12;
655
656         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTAR(wnd), tar);
657         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTEAR(wnd), 0);
658         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWBAR(wnd), bar);
659         bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWAR(wnd), attr);
660 }
661
662 static int
663 pci_ocp_iorange(struct pci_ocp_softc *sc, int type, int wnd)
664 {
665         struct rman *rm;
666         u_long start, end, size, alloc;
667         bus_addr_t pci_start, pci_end;
668         bus_addr_t *vap, *allocp;
669         int error;
670
671         error = bus_get_resource(sc->sc_dev, type, 1, &start, &size);
672         if (error)
673                 return (error);
674
675         end = start + size - 1;
676
677         switch (type) {
678         case SYS_RES_IOPORT:
679                 rm = &sc->sc_ioport;
680                 pci_start = 0x0000;
681                 pci_end = 0xffff;
682                 alloc = 0x1000;
683                 vap = &sc->sc_ioport_va;
684                 allocp = &sc->sc_ioport_alloc;
685                 break;
686         case SYS_RES_MEMORY:
687                 rm = &sc->sc_iomem;
688                 pci_start = start;
689                 pci_end = end;
690                 alloc = 0;
691                 vap = &sc->sc_iomem_va;
692                 allocp = &sc->sc_iomem_alloc;
693                 break;
694         default:
695                 return (EINVAL);
696         }
697
698         rm->rm_type = RMAN_ARRAY;
699         rm->rm_start = pci_start;
700         rm->rm_end = pci_end;
701         error = rman_init(rm);
702         if (error)
703                 return (error);
704
705         error = rman_manage_region(rm, pci_start, pci_end);
706         if (error) {
707                 rman_fini(rm);
708                 return (error);
709         }
710
711         *allocp = pci_start + alloc;
712         *vap = (uintptr_t)pmap_mapdev(start, size);
713         if (wnd != -1)
714                 pci_ocp_outbound(sc, wnd, type, start, size, pci_start);
715         return (0);
716 }
717
718 static int
719 pci_ocp_attach(device_t dev)
720 {
721         struct pci_ocp_softc *sc;
722         uint32_t cfgreg;
723         int error, maxslot;
724
725         sc = device_get_softc(dev);
726         sc->sc_dev = dev;
727
728         sc->sc_rid = 0;
729         sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
730             RF_ACTIVE);
731         if (sc->sc_res == NULL) {
732                 device_printf(dev, "could not map I/O memory\n");
733                 return (ENXIO);
734         }
735         sc->sc_bst = rman_get_bustag(sc->sc_res);
736         sc->sc_bsh = rman_get_bushandle(sc->sc_res);
737
738         cfgreg = pci_ocp_cfgread(sc, 0, 0, 0, PCIR_COMMAND, 2);
739         cfgreg |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
740             PCIM_CMD_PORTEN;
741         pci_ocp_cfgwrite(sc, 0, 0, 0, PCIR_COMMAND, cfgreg, 2);
742
743         pci_ocp_outbound(sc, 0, -1, 0, 0, 0);
744         error = pci_ocp_iorange(sc, SYS_RES_MEMORY, 1);
745         error = pci_ocp_iorange(sc, SYS_RES_IOPORT, 2);
746         pci_ocp_outbound(sc, 3, -1, 0, 0, 0);
747         pci_ocp_outbound(sc, 4, -1, 0, 0, 0);
748
749         pci_ocp_inbound(sc, 1, -1, 0, 0, 0);
750         pci_ocp_inbound(sc, 2, -1, 0, 0, 0);
751         pci_ocp_inbound(sc, 3, OCP85XX_TGTIF_RAM1, 0, 2U*1024U*1024U*1024U, 0);
752
753         sc->sc_devfn_tundra = -1;
754         sc->sc_devfn_via_ide = -1;
755
756         maxslot = (sc->sc_pcie) ? 1 : 31;
757         pci_ocp_init(sc, sc->sc_busnr, maxslot);
758
759         device_add_child(dev, "pci", -1);
760         return (bus_generic_attach(dev));
761 }
762
763 static struct resource *
764 pci_ocp_alloc_resource(device_t dev, device_t child, int type, int *rid,
765     u_long start, u_long end, u_long count, u_int flags)
766 {
767         struct pci_ocp_softc *sc = device_get_softc(dev);
768         struct rman *rm;
769         struct resource *res;
770         bus_addr_t va;
771
772         switch (type) {
773         case SYS_RES_IOPORT:
774                 rm = &sc->sc_ioport;
775                 va = sc->sc_ioport_va;
776                 break;
777         case SYS_RES_MEMORY:
778                 rm = &sc->sc_iomem;
779                 va = sc->sc_iomem_va;
780                 break;
781         case SYS_RES_IRQ:
782                 if (start < PIC_IRQ_START) {
783                         device_printf(dev, "%s requested ISA interrupt %lu\n",
784                             device_get_nameunit(child), start);
785                 }
786                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
787                     type, rid, start, end, count, flags));
788         default:
789                 return (NULL);
790         }
791
792         res = rman_reserve_resource(rm, start, end, count, flags, child);
793         if (res == NULL)
794                 return (NULL);
795
796         rman_set_bustag(res, &bs_le_tag);
797         rman_set_bushandle(res, va + rman_get_start(res) - rm->rm_start);
798         return (res);
799 }
800
801 static int
802 pci_ocp_release_resource(device_t dev, device_t child, int type, int rid,
803     struct resource *res)
804 {
805
806         return (rman_release_resource(res));
807 }
808
809 static int
810 pci_ocp_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
811 {
812         struct pci_ocp_softc  *sc = device_get_softc(dev);
813
814         switch (which) {
815         case PCIB_IVAR_BUS:
816                 *result = sc->sc_busnr;
817                 return (0);
818         case PCIB_IVAR_DOMAIN:
819                 *result = device_get_unit(dev);
820                 return (0);
821         }
822         return (ENOENT);
823 }
824
825 static int
826 pci_ocp_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
827 {
828         struct pci_ocp_softc *sc = device_get_softc(dev);
829
830         switch (which) {
831         case PCIB_IVAR_BUS:
832                 sc->sc_busnr = value;
833                 return (0);
834         }
835         return (ENOENT);
836 }