1*e4a54b41Snonaka /* $NetBSD: ibm405gp.c,v 1.8 2016/10/19 00:08:42 nonaka Exp $ */
267d6f493Sscw
367d6f493Sscw /*
467d6f493Sscw * Copyright 2001 Wasabi Systems, Inc.
567d6f493Sscw * All rights reserved.
667d6f493Sscw *
767d6f493Sscw * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
867d6f493Sscw *
967d6f493Sscw * Redistribution and use in source and binary forms, with or without
1067d6f493Sscw * modification, are permitted provided that the following conditions
1167d6f493Sscw * are met:
1267d6f493Sscw * 1. Redistributions of source code must retain the above copyright
1367d6f493Sscw * notice, this list of conditions and the following disclaimer.
1467d6f493Sscw * 2. Redistributions in binary form must reproduce the above copyright
1567d6f493Sscw * notice, this list of conditions and the following disclaimer in the
1667d6f493Sscw * documentation and/or other materials provided with the distribution.
1767d6f493Sscw * 3. All advertising materials mentioning features or use of this software
1867d6f493Sscw * must display the following acknowledgement:
1967d6f493Sscw * This product includes software developed for the NetBSD Project by
2067d6f493Sscw * Wasabi Systems, Inc.
2167d6f493Sscw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2267d6f493Sscw * or promote products derived from this software without specific prior
2367d6f493Sscw * written permission.
2467d6f493Sscw *
2567d6f493Sscw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2667d6f493Sscw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2767d6f493Sscw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2867d6f493Sscw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
2967d6f493Sscw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3067d6f493Sscw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3167d6f493Sscw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3267d6f493Sscw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3367d6f493Sscw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3467d6f493Sscw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3567d6f493Sscw * POSSIBILITY OF SUCH DAMAGE.
3667d6f493Sscw */
3767d6f493Sscw
38ed517291Slukem #include <sys/cdefs.h>
39*e4a54b41Snonaka __KERNEL_RCSID(0, "$NetBSD: ibm405gp.c,v 1.8 2016/10/19 00:08:42 nonaka Exp $");
40ed517291Slukem
4167d6f493Sscw #include <sys/param.h>
4267d6f493Sscw #include <sys/device.h>
4367d6f493Sscw #include <sys/systm.h>
44728102e6Sscw #include <sys/extent.h>
451fd2c684Smatt #include <sys/bus.h>
4667d6f493Sscw
4767d6f493Sscw #include <dev/pci/pcivar.h>
4867d6f493Sscw
4967d6f493Sscw #include <powerpc/ibm4xx/ibm405gp.h>
50dd2488a8Smatt #include <powerpc/ibm4xx/pci_machdep.h>
5167d6f493Sscw #include <powerpc/ibm4xx/dev/pcicreg.h>
5267d6f493Sscw
53728102e6Sscw static struct powerpc_bus_space pcicfg_tag = {
54728102e6Sscw _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
55728102e6Sscw IBM405GP_PCIL0_BASE, 0x0, 0x40
56728102e6Sscw };
57728102e6Sscw static char ex_storage[EXTENT_FIXED_STORAGE_SIZE(1)]
58728102e6Sscw __attribute__((aligned(8)));
59728102e6Sscw static bus_space_tag_t pcicfg_iot = &pcicfg_tag;
6067d6f493Sscw static bus_space_handle_t pcicfg_ioh = 0;
6167d6f493Sscw
6267d6f493Sscw #define PCI0_MEM_BASE 0x80000000
6367d6f493Sscw
64dd2488a8Smatt static struct genppc_pci_chipset genppc_ibm4xx_chipset = {
65dd2488a8Smatt .pc_conf_v = NULL,
66dd2488a8Smatt .pc_attach_hook = ibm4xx_pci_attach_hook,
67dd2488a8Smatt .pc_bus_maxdevs = ibm4xx_pci_bus_maxdevs,
68dd2488a8Smatt .pc_make_tag = ibm4xx_pci_make_tag,
69dd2488a8Smatt .pc_conf_read = ibm4xx_pci_conf_read,
70dd2488a8Smatt .pc_conf_write = ibm4xx_pci_conf_write,
71dd2488a8Smatt
72dd2488a8Smatt .pc_intr_v = &genppc_ibm4xx_chipset,
73dd2488a8Smatt .pc_intr_map = genppc_pci_intr_map,
74dd2488a8Smatt .pc_intr_string = genppc_pci_intr_string,
75dd2488a8Smatt .pc_intr_evcnt = genppc_pci_intr_evcnt,
76dd2488a8Smatt .pc_intr_establish = genppc_pci_intr_establish,
77dd2488a8Smatt .pc_intr_disestablish = genppc_pci_intr_disestablish,
78dd2488a8Smatt .pc_intr_setattr = ibm4xx_pci_intr_setattr,
79*e4a54b41Snonaka .pc_intr_type = genppc_pci_intr_type,
80*e4a54b41Snonaka .pc_intr_alloc = genppc_pci_intr_alloc,
81*e4a54b41Snonaka .pc_intr_release = genppc_pci_intr_release,
82*e4a54b41Snonaka .pc_intx_alloc = genppc_pci_intx_alloc,
83dd2488a8Smatt
841f2907adSmatt .pc_msi_v = &genppc_ibm4xx_chipset,
851f2907adSmatt GENPPC_PCI_MSI_INITIALIZER,
861f2907adSmatt
87*e4a54b41Snonaka .pc_msix_v = &genppc_ibm4xx_chipset,
88*e4a54b41Snonaka GENPPC_PCI_MSIX_INITIALIZER,
89*e4a54b41Snonaka
90dd2488a8Smatt .pc_conf_interrupt = ibm4xx_pci_conf_interrupt,
91dd2488a8Smatt .pc_decompose_tag = ibm4xx_pci_decompose_tag,
92dd2488a8Smatt .pc_conf_hook = ibm4xx_pci_conf_hook,
93dd2488a8Smatt };
94dd2488a8Smatt
95dd2488a8Smatt pci_chipset_tag_t
ibm4xx_get_pci_chipset_tag(void)96dd2488a8Smatt ibm4xx_get_pci_chipset_tag(void)
97dd2488a8Smatt {
98dd2488a8Smatt return &genppc_ibm4xx_chipset;
99dd2488a8Smatt }
100dd2488a8Smatt
101dd2488a8Smatt static void
setup_pcicfg_window(void)102dd2488a8Smatt setup_pcicfg_window(void)
10367d6f493Sscw {
10467d6f493Sscw if (pcicfg_ioh)
10567d6f493Sscw return;
106728102e6Sscw if (bus_space_init(&pcicfg_tag,
107728102e6Sscw "pcicfg", ex_storage, sizeof(ex_storage)) ||
108728102e6Sscw bus_space_map(pcicfg_iot, 0, 0x40 , 0, &pcicfg_ioh))
10967d6f493Sscw panic("Cannot map PCI configuration registers");
11067d6f493Sscw }
11167d6f493Sscw
11267d6f493Sscw /*
11367d6f493Sscw * Setup proper Local<->PCI mapping
11467d6f493Sscw * PCI memory window: 256M @ PCI0MEMBASE with direct memory translation
11567d6f493Sscw */
116dd2488a8Smatt void
ibm4xx_setup_pci(void)117dd2488a8Smatt ibm4xx_setup_pci(void)
11867d6f493Sscw {
119dd2488a8Smatt pci_chipset_tag_t pc = &genppc_ibm4xx_chipset;
12067d6f493Sscw pcitag_t tag;
12167d6f493Sscw
12267d6f493Sscw setup_pcicfg_window();
12367d6f493Sscw
12467d6f493Sscw /* Disable all three memory mappers */
12567d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0MA, 0x00000000); /* disabled */
12667d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM1MA, 0x00000000); /* disabled */
12767d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM2MA, 0x00000000); /* disabled */
12867d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM1MS, 0x00000000); /* Can't really disable PTM1. */
12967d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM2MS, 0x00000000); /* disabled */
13067d6f493Sscw
13167d6f493Sscw
13267d6f493Sscw /* Setup memory map #0 */
13367d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0MA, 0xF0000001); /* 256M non-prefetchable, enabled */
13467d6f493Sscw
13567d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0LA, PCI0_MEM_BASE);
13667d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0PCILA, PCI0_MEM_BASE);
13767d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0PCIHA, 0);
13867d6f493Sscw
13967d6f493Sscw /* Configure PCI bridge */
140dd2488a8Smatt tag = pci_make_tag(pc, 0, 0, 0);
141dd2488a8Smatt // x = pci_conf_read(pc, tag, PCI0_CMD); /* Read PCI command register */
142dd2488a8Smatt // pci_conf_write(pc, tag, PCI0_CMD, x | MA | ME); /* enable bus mastering and memory space */
14367d6f493Sscw
14467d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM1MS, 0xF0000001); /* Enable PTM1 */
14567d6f493Sscw bus_space_write_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM1LA, 0);
146dd2488a8Smatt pci_conf_write(pc, tag, PCIC_PTM1BAR, 0); /* Set up proper PCI->Local address base. Always enabled */
147dd2488a8Smatt pci_conf_write(pc, tag, PCIC_PTM2BAR, 0);
14867d6f493Sscw }
14967d6f493Sscw
150dd2488a8Smatt void
ibm4xx_show_pci_map(void)151dd2488a8Smatt ibm4xx_show_pci_map(void)
15267d6f493Sscw {
153dd2488a8Smatt pci_chipset_tag_t pc = &genppc_ibm4xx_chipset;
15467d6f493Sscw paddr_t la, lm, pl, ph;
15567d6f493Sscw pcitag_t tag;
15667d6f493Sscw
15767d6f493Sscw setup_pcicfg_window();
15867d6f493Sscw
15967d6f493Sscw printf("Local -> PCI map\n");
16067d6f493Sscw la = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0LA);
16167d6f493Sscw lm = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0MA);
16267d6f493Sscw pl = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0PCILA);
16367d6f493Sscw ph = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM0PCIHA);
16467d6f493Sscw printf("0: %08lx,%08lx -> %08lx%08lx %sprefetchable, %s\n", la, lm, ph, pl,
16567d6f493Sscw (lm & 2) ? "":"not ",
16667d6f493Sscw (lm & 1) ? "enabled":"disabled");
16767d6f493Sscw la = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM1LA);
16867d6f493Sscw lm = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM1MA);
16967d6f493Sscw pl = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM1PCILA);
17067d6f493Sscw ph = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM1PCIHA);
17167d6f493Sscw printf("1: %08lx,%08lx -> %08lx%08lx %sprefetchable, %s\n", la, lm, ph, pl,
17267d6f493Sscw (lm & 2) ? "":"not ",
17367d6f493Sscw (lm & 1) ? "enabled":"disabled");
17467d6f493Sscw la = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM2LA);
17567d6f493Sscw lm = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM2MA);
17667d6f493Sscw pl = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM2PCILA);
17767d6f493Sscw ph = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PMM2PCIHA);
17867d6f493Sscw printf("2: %08lx,%08lx -> %08lx%08lx %sprefetchable, %s\n", la, lm, ph, pl,
17967d6f493Sscw (lm & 2) ? "":"not ",
18067d6f493Sscw (lm & 1) ? "enabled":"disabled");
18167d6f493Sscw printf("PCI -> Local map\n");
18267d6f493Sscw
183dd2488a8Smatt tag = pci_make_tag(pc, 0, 0, 0);
184dd2488a8Smatt pl = pci_conf_read(pc, tag, PCIC_PTM1BAR);
18567d6f493Sscw la = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM1LA);
18667d6f493Sscw lm = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM1MS);
18767d6f493Sscw printf("1: %08lx -> %08lx,%08lx %s\n", pl, la, lm,
18867d6f493Sscw (lm & 1)?"enabled":"disabled");
189dd2488a8Smatt pl = pci_conf_read(pc, tag, PCIC_PTM2BAR);
19067d6f493Sscw la = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM2LA);
19167d6f493Sscw lm = bus_space_read_4(pcicfg_iot, pcicfg_ioh, PCIL_PTM2MS);
19267d6f493Sscw printf("2: %08lx -> %08lx,%08lx %s\n", pl, la, lm,
19367d6f493Sscw (lm & 1)?"enabled":"disabled");
19467d6f493Sscw }
195