1*c7fb772bSthorpej /* $NetBSD: ofwpci.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $ */
2d974db0aSgarbled
3d974db0aSgarbled /*-
4d974db0aSgarbled * Copyright (c) 2007 The NetBSD Foundation, Inc.
5d974db0aSgarbled * All rights reserved.
6d974db0aSgarbled *
7d974db0aSgarbled * This code is derived from software contributed to The NetBSD Foundation
8d974db0aSgarbled * by Tim Rightnour
9d974db0aSgarbled *
10d974db0aSgarbled * Redistribution and use in source and binary forms, with or without
11d974db0aSgarbled * modification, are permitted provided that the following conditions
12d974db0aSgarbled * are met:
13d974db0aSgarbled * 1. Redistributions of source code must retain the above copyright
14d974db0aSgarbled * notice, this list of conditions and the following disclaimer.
15d974db0aSgarbled * 2. Redistributions in binary form must reproduce the above copyright
16d974db0aSgarbled * notice, this list of conditions and the following disclaimer in the
17d974db0aSgarbled * documentation and/or other materials provided with the distribution.
18d974db0aSgarbled *
19d974db0aSgarbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d974db0aSgarbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d974db0aSgarbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d974db0aSgarbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d974db0aSgarbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d974db0aSgarbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d974db0aSgarbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d974db0aSgarbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d974db0aSgarbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d974db0aSgarbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d974db0aSgarbled * POSSIBILITY OF SUCH DAMAGE.
30d974db0aSgarbled */
31d974db0aSgarbled
32d974db0aSgarbled #include <sys/cdefs.h>
33*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: ofwpci.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $");
3480a83a2bSgarbled
3580a83a2bSgarbled #include "opt_pci.h"
36d974db0aSgarbled
37d974db0aSgarbled #include <sys/param.h>
38d974db0aSgarbled #include <sys/device.h>
39192b9947Sthorpej #include <sys/kmem.h>
40d974db0aSgarbled #include <sys/systm.h>
41d974db0aSgarbled
42d974db0aSgarbled #include <dev/pci/pcivar.h>
4380a83a2bSgarbled #include <dev/pci/pciconf.h>
44d974db0aSgarbled #include <dev/ofw/openfirm.h>
45d974db0aSgarbled #include <dev/ofw/ofw_pci.h>
46d974db0aSgarbled
47d974db0aSgarbled #include <machine/autoconf.h>
4880a83a2bSgarbled #include <machine/isa_machdep.h>
49d974db0aSgarbled #include <machine/pio.h>
50d974db0aSgarbled
51d974db0aSgarbled struct ofwpci_softc {
5205b09539Smatt device_t sc_dev;
53d974db0aSgarbled struct genppc_pci_chipset sc_pc;
54d974db0aSgarbled struct powerpc_bus_space sc_iot;
55d974db0aSgarbled struct powerpc_bus_space sc_memt;
56d974db0aSgarbled };
57d974db0aSgarbled
5805b09539Smatt static void ofwpci_attach(device_t, device_t, void *);
5905b09539Smatt static int ofwpci_match(device_t, cfdata_t, void *);
60d974db0aSgarbled
6105b09539Smatt CFATTACH_DECL_NEW(ofwpci, sizeof(struct ofwpci_softc),
62d974db0aSgarbled ofwpci_match, ofwpci_attach, NULL, NULL);
63d974db0aSgarbled
64d974db0aSgarbled extern struct genppc_pci_chipset *genppc_pct;
6555804a6fSgarbled extern struct model_data modeldata;
66d974db0aSgarbled
67d974db0aSgarbled static void
ofwpci_get_chipset_tag(pci_chipset_tag_t pc)68d974db0aSgarbled ofwpci_get_chipset_tag(pci_chipset_tag_t pc)
69d974db0aSgarbled {
70d974db0aSgarbled pc->pc_conf_v = (void *)pc;
71d974db0aSgarbled
7280a83a2bSgarbled pc->pc_attach_hook = genppc_pci_ofmethod_attach_hook;
73d974db0aSgarbled pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
7480a83a2bSgarbled pc->pc_make_tag = genppc_pci_ofmethod_make_tag;
7580a83a2bSgarbled pc->pc_conf_read = genppc_pci_ofmethod_conf_read;
7680a83a2bSgarbled pc->pc_conf_write = genppc_pci_ofmethod_conf_write;
77d974db0aSgarbled
78d974db0aSgarbled pc->pc_intr_v = (void *)pc;
79d974db0aSgarbled
80d974db0aSgarbled pc->pc_intr_map = genofw_pci_intr_map;
81d974db0aSgarbled pc->pc_intr_string = genppc_pci_intr_string;
82d974db0aSgarbled pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
83d974db0aSgarbled pc->pc_intr_establish = genppc_pci_intr_establish;
84d974db0aSgarbled pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
85e4a54b41Snonaka pc->pc_intr_setattr = genppc_pci_intr_setattr;
86e4a54b41Snonaka pc->pc_intr_type = genppc_pci_intr_type;
87e4a54b41Snonaka pc->pc_intr_alloc = genppc_pci_intr_alloc;
88e4a54b41Snonaka pc->pc_intr_release = genppc_pci_intr_release;
89e4a54b41Snonaka pc->pc_intx_alloc = genppc_pci_intx_alloc;
90e4a54b41Snonaka
91e4a54b41Snonaka pc->pc_msi_v = (void *)pc;
92e4a54b41Snonaka genppc_pci_chipset_msi_init(pc);
93e4a54b41Snonaka
94e4a54b41Snonaka pc->pc_msix_v = (void *)pc;
95e4a54b41Snonaka genppc_pci_chipset_msix_init(pc);
96d974db0aSgarbled
97d974db0aSgarbled pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
9880a83a2bSgarbled pc->pc_decompose_tag = genppc_pci_ofmethod_decompose_tag;
99d974db0aSgarbled pc->pc_conf_hook = genofw_pci_conf_hook;
100d974db0aSgarbled
101d974db0aSgarbled pc->pc_addr = 0;
102d974db0aSgarbled pc->pc_data = 0;
103d974db0aSgarbled pc->pc_bus = 0;
104d974db0aSgarbled pc->pc_node = 0;
105d974db0aSgarbled pc->pc_memt = 0;
106d974db0aSgarbled pc->pc_iot = 0;
10780a83a2bSgarbled pc->pc_ihandle = 0;
108d974db0aSgarbled }
109d974db0aSgarbled
110d974db0aSgarbled static int
ofwpci_match(device_t parent,cfdata_t cf,void * aux)11105b09539Smatt ofwpci_match(device_t parent, cfdata_t cf, void *aux)
112d974db0aSgarbled {
113d974db0aSgarbled struct confargs *ca = aux;
114d974db0aSgarbled char name[32];
115d974db0aSgarbled
116d974db0aSgarbled if (strcmp(ca->ca_name, "pci") != 0)
117d974db0aSgarbled return 0;
118d974db0aSgarbled
119d974db0aSgarbled memset(name, 0, sizeof(name));
1205c24ff82Sgarbled OF_getprop(ca->ca_node, "device_type", name, sizeof(name));
121d974db0aSgarbled if (strcmp(name, "pci") != 0)
122d974db0aSgarbled return 0;
123d974db0aSgarbled
124d974db0aSgarbled return 1;
125d974db0aSgarbled }
126d974db0aSgarbled
127d974db0aSgarbled static void
ofwpci_attach(device_t parent,device_t self,void * aux)12805b09539Smatt ofwpci_attach(device_t parent, device_t self, void *aux)
129d974db0aSgarbled {
13005b09539Smatt struct ofwpci_softc *sc = device_private(self);
131d974db0aSgarbled pci_chipset_tag_t pc = &sc->sc_pc;
132d974db0aSgarbled struct confargs *ca = aux;
133d974db0aSgarbled struct pcibus_attach_args pba;
134d974db0aSgarbled struct genppc_pci_chipset_businfo *pbi;
135d974db0aSgarbled int node = ca->ca_node;
136edd2c342Schristos int i;
13780a83a2bSgarbled uint32_t busrange[2];
13880a83a2bSgarbled char buf[64];
139d974db0aSgarbled
140d974db0aSgarbled aprint_normal("\n");
141d974db0aSgarbled
14205b09539Smatt sc->sc_dev = self;
14305b09539Smatt
144d974db0aSgarbled /* PCI bus number */
145d974db0aSgarbled if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
146d974db0aSgarbled return;
147d974db0aSgarbled
148d974db0aSgarbled /* bus space map the io ranges */
149d974db0aSgarbled sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
150d974db0aSgarbled sc->sc_iot.pbs_base = 0x00000000;
151d974db0aSgarbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
152d974db0aSgarbled "ofwpci io-space") != 0)
153d974db0aSgarbled panic("Can't init ofwpci io tag");
154d974db0aSgarbled
155d974db0aSgarbled sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
156d974db0aSgarbled sc->sc_memt.pbs_base = 0x00000000;
157d974db0aSgarbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
158d974db0aSgarbled "ofwpci mem-space") != 0)
159d974db0aSgarbled panic("Can't init ofwpci mem tag");
160d974db0aSgarbled
1611c34cc42Smatt aprint_debug("io base=0x%"PRIxPTR" offset=0x%"PRIxPTR" limit=0x%"PRIxPTR"\n",
16280a83a2bSgarbled sc->sc_iot.pbs_base, sc->sc_iot.pbs_offset, sc->sc_iot.pbs_limit);
16380a83a2bSgarbled
1641c34cc42Smatt aprint_debug("mem base=0x%"PRIxPTR" offset=0x%"PRIxPTR" limit=0x%"PRIxPTR"\n",
16580a83a2bSgarbled sc->sc_memt.pbs_base, sc->sc_memt.pbs_offset,
16680a83a2bSgarbled sc->sc_memt.pbs_limit);
16780a83a2bSgarbled
16880a83a2bSgarbled /* are we the primary pci bus? */
16980a83a2bSgarbled if (of_find_firstchild_byname(OF_finddevice("/"), "pci") == node) {
17080a83a2bSgarbled int isa_node;
17180a83a2bSgarbled
17280a83a2bSgarbled /* yes we are, now do we have an ISA child? */
17380a83a2bSgarbled isa_node = of_find_firstchild_byname(node, "isa");
17480a83a2bSgarbled if (isa_node != -1) {
17580a83a2bSgarbled /* isa == pci */
17680a83a2bSgarbled genppc_isa_io_space_tag = sc->sc_iot;
17780a83a2bSgarbled genppc_isa_mem_space_tag = sc->sc_memt;
17880a83a2bSgarbled map_isa_ioregs();
17980a83a2bSgarbled init_ofppc_interrupt();
18080a83a2bSgarbled ofppc_init_comcons(isa_node);
18180a83a2bSgarbled }
18280a83a2bSgarbled }
18380a83a2bSgarbled
184d974db0aSgarbled ofwpci_get_chipset_tag(pc);
18580a83a2bSgarbled
186d974db0aSgarbled pc->pc_node = node;
18780a83a2bSgarbled i = OF_package_to_path(node, buf, sizeof(buf)-5);
18880a83a2bSgarbled if (i <= 0)
18980a83a2bSgarbled panic("Can't determine path for pci node %d", node);
19080a83a2bSgarbled buf[i] = '\0';
19180a83a2bSgarbled pc->pc_ihandle = OF_open(buf);
19280a83a2bSgarbled if (pc->pc_ihandle < 0)
19380a83a2bSgarbled panic("Can't open device %s", buf);
194d974db0aSgarbled pc->pc_bus = busrange[0];
195d974db0aSgarbled pc->pc_iot = &sc->sc_iot;
196d974db0aSgarbled pc->pc_memt = &sc->sc_memt;
197d974db0aSgarbled
198192b9947Sthorpej pbi = kmem_alloc(sizeof(struct genppc_pci_chipset_businfo), KM_SLEEP);
199d974db0aSgarbled pbi->pbi_properties = prop_dictionary_create();
200d974db0aSgarbled KASSERT(pbi->pbi_properties != NULL);
201d974db0aSgarbled SIMPLEQ_INIT(&pc->pc_pbi);
202d974db0aSgarbled SIMPLEQ_INSERT_TAIL(&pc->pc_pbi, pbi, next);
203d974db0aSgarbled
2049e99088cSgarbled genofw_setup_pciintr_map((void *)pc, pbi, pc->pc_node);
20580a83a2bSgarbled #ifdef PCI_NETBSD_CONFIGURE
206ca8ce3aeSthorpej struct pciconf_resources *pcires = pciconf_resource_init();
20780a83a2bSgarbled
208ca8ce3aeSthorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
209ca8ce3aeSthorpej modeldata.pciiodata[device_unit(self)].start,
210ca8ce3aeSthorpej (modeldata.pciiodata[device_unit(self)].limit -
211ca8ce3aeSthorpej modeldata.pciiodata[device_unit(self)].start) + 1);
212ca8ce3aeSthorpej
213ca8ce3aeSthorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
214ca8ce3aeSthorpej sc->sc_memt.pbs_base,
215ca8ce3aeSthorpej (sc->sc_memt.pbs_limit - sc->sc_memt.pbs_base) + 1);
216ca8ce3aeSthorpej
217ca8ce3aeSthorpej if (pci_configure_bus(pc, pcires, 0, CACHELINESIZE))
21880a83a2bSgarbled aprint_error("pci_configure_bus() failed\n");
21980a83a2bSgarbled
220ca8ce3aeSthorpej pciconf_resource_fini(pcires);
22180a83a2bSgarbled #endif /* PCI_NETBSD_CONFIGURE */
222d974db0aSgarbled memset(&pba, 0, sizeof(pba));
223d974db0aSgarbled pba.pba_memt = pc->pc_memt;
224d974db0aSgarbled pba.pba_iot = pc->pc_iot;
225d974db0aSgarbled pba.pba_dmat = &pci_bus_dma_tag;
226d974db0aSgarbled pba.pba_dmat64 = NULL;
227d974db0aSgarbled pba.pba_bus = pc->pc_bus;
228d974db0aSgarbled pba.pba_bridgetag = NULL;
229d974db0aSgarbled pba.pba_pc = pc;
230a6b2b839Sdyoung pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
231d1083ec1Sthorpej config_found(self, &pba, pcibusprint,
232*c7fb772bSthorpej CFARGS(.devhandle = device_handle(self)));
233d974db0aSgarbled }
234