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