ofwpci.c revision 1.18 1 1.18 thorpej /* $NetBSD: ofwpci.c,v 1.18 2020/11/21 17:48:26 thorpej Exp $ */
2 1.2 garbled
3 1.2 garbled /*-
4 1.2 garbled * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.2 garbled * All rights reserved.
6 1.2 garbled *
7 1.2 garbled * This code is derived from software contributed to The NetBSD Foundation
8 1.2 garbled * by Tim Rightnour
9 1.2 garbled *
10 1.2 garbled * Redistribution and use in source and binary forms, with or without
11 1.2 garbled * modification, are permitted provided that the following conditions
12 1.2 garbled * are met:
13 1.2 garbled * 1. Redistributions of source code must retain the above copyright
14 1.2 garbled * notice, this list of conditions and the following disclaimer.
15 1.2 garbled * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 garbled * notice, this list of conditions and the following disclaimer in the
17 1.2 garbled * documentation and/or other materials provided with the distribution.
18 1.2 garbled *
19 1.2 garbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 garbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 garbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 garbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 garbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 garbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 garbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 garbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 garbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 garbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 garbled * POSSIBILITY OF SUCH DAMAGE.
30 1.2 garbled */
31 1.2 garbled
32 1.2 garbled #include <sys/cdefs.h>
33 1.18 thorpej __KERNEL_RCSID(0, "$NetBSD: ofwpci.c,v 1.18 2020/11/21 17:48:26 thorpej Exp $");
34 1.5 garbled
35 1.5 garbled #include "opt_pci.h"
36 1.2 garbled
37 1.2 garbled #include <sys/param.h>
38 1.2 garbled #include <sys/device.h>
39 1.18 thorpej #include <sys/kmem.h>
40 1.2 garbled #include <sys/systm.h>
41 1.2 garbled
42 1.2 garbled #include <dev/pci/pcivar.h>
43 1.5 garbled #include <dev/pci/pciconf.h>
44 1.2 garbled #include <dev/ofw/openfirm.h>
45 1.2 garbled #include <dev/ofw/ofw_pci.h>
46 1.2 garbled
47 1.2 garbled #include <machine/autoconf.h>
48 1.5 garbled #include <machine/isa_machdep.h>
49 1.2 garbled #include <machine/pio.h>
50 1.2 garbled
51 1.2 garbled struct ofwpci_softc {
52 1.10 matt device_t sc_dev;
53 1.2 garbled struct genppc_pci_chipset sc_pc;
54 1.2 garbled struct powerpc_bus_space sc_iot;
55 1.2 garbled struct powerpc_bus_space sc_memt;
56 1.2 garbled };
57 1.2 garbled
58 1.10 matt static void ofwpci_attach(device_t, device_t, void *);
59 1.10 matt static int ofwpci_match(device_t, cfdata_t, void *);
60 1.2 garbled
61 1.10 matt CFATTACH_DECL_NEW(ofwpci, sizeof(struct ofwpci_softc),
62 1.2 garbled ofwpci_match, ofwpci_attach, NULL, NULL);
63 1.2 garbled
64 1.2 garbled extern struct genppc_pci_chipset *genppc_pct;
65 1.6 garbled extern struct model_data modeldata;
66 1.2 garbled
67 1.2 garbled static void
68 1.2 garbled ofwpci_get_chipset_tag(pci_chipset_tag_t pc)
69 1.2 garbled {
70 1.2 garbled pc->pc_conf_v = (void *)pc;
71 1.2 garbled
72 1.5 garbled pc->pc_attach_hook = genppc_pci_ofmethod_attach_hook;
73 1.2 garbled pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
74 1.5 garbled pc->pc_make_tag = genppc_pci_ofmethod_make_tag;
75 1.5 garbled pc->pc_conf_read = genppc_pci_ofmethod_conf_read;
76 1.5 garbled pc->pc_conf_write = genppc_pci_ofmethod_conf_write;
77 1.2 garbled
78 1.2 garbled pc->pc_intr_v = (void *)pc;
79 1.2 garbled
80 1.2 garbled pc->pc_intr_map = genofw_pci_intr_map;
81 1.2 garbled pc->pc_intr_string = genppc_pci_intr_string;
82 1.2 garbled pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
83 1.2 garbled pc->pc_intr_establish = genppc_pci_intr_establish;
84 1.2 garbled pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
85 1.13 nonaka pc->pc_intr_setattr = genppc_pci_intr_setattr;
86 1.13 nonaka pc->pc_intr_type = genppc_pci_intr_type;
87 1.13 nonaka pc->pc_intr_alloc = genppc_pci_intr_alloc;
88 1.13 nonaka pc->pc_intr_release = genppc_pci_intr_release;
89 1.13 nonaka pc->pc_intx_alloc = genppc_pci_intx_alloc;
90 1.13 nonaka
91 1.13 nonaka pc->pc_msi_v = (void *)pc;
92 1.13 nonaka genppc_pci_chipset_msi_init(pc);
93 1.13 nonaka
94 1.13 nonaka pc->pc_msix_v = (void *)pc;
95 1.13 nonaka genppc_pci_chipset_msix_init(pc);
96 1.2 garbled
97 1.2 garbled pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
98 1.5 garbled pc->pc_decompose_tag = genppc_pci_ofmethod_decompose_tag;
99 1.2 garbled pc->pc_conf_hook = genofw_pci_conf_hook;
100 1.2 garbled
101 1.2 garbled pc->pc_addr = 0;
102 1.2 garbled pc->pc_data = 0;
103 1.2 garbled pc->pc_bus = 0;
104 1.2 garbled pc->pc_node = 0;
105 1.2 garbled pc->pc_memt = 0;
106 1.2 garbled pc->pc_iot = 0;
107 1.5 garbled pc->pc_ihandle = 0;
108 1.2 garbled }
109 1.2 garbled
110 1.2 garbled static int
111 1.10 matt ofwpci_match(device_t parent, cfdata_t cf, void *aux)
112 1.2 garbled {
113 1.2 garbled struct confargs *ca = aux;
114 1.2 garbled char name[32];
115 1.2 garbled
116 1.2 garbled if (strcmp(ca->ca_name, "pci") != 0)
117 1.2 garbled return 0;
118 1.2 garbled
119 1.2 garbled memset(name, 0, sizeof(name));
120 1.3 garbled OF_getprop(ca->ca_node, "device_type", name, sizeof(name));
121 1.2 garbled if (strcmp(name, "pci") != 0)
122 1.2 garbled return 0;
123 1.2 garbled
124 1.2 garbled return 1;
125 1.2 garbled }
126 1.2 garbled
127 1.2 garbled static void
128 1.10 matt ofwpci_attach(device_t parent, device_t self, void *aux)
129 1.2 garbled {
130 1.10 matt struct ofwpci_softc *sc = device_private(self);
131 1.2 garbled pci_chipset_tag_t pc = &sc->sc_pc;
132 1.2 garbled struct confargs *ca = aux;
133 1.2 garbled struct pcibus_attach_args pba;
134 1.2 garbled struct genppc_pci_chipset_businfo *pbi;
135 1.2 garbled int node = ca->ca_node;
136 1.14 christos int i;
137 1.5 garbled uint32_t busrange[2];
138 1.5 garbled char buf[64];
139 1.2 garbled
140 1.2 garbled aprint_normal("\n");
141 1.2 garbled
142 1.10 matt sc->sc_dev = self;
143 1.10 matt
144 1.2 garbled /* PCI bus number */
145 1.2 garbled if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
146 1.2 garbled return;
147 1.2 garbled
148 1.2 garbled /* bus space map the io ranges */
149 1.2 garbled sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
150 1.2 garbled sc->sc_iot.pbs_base = 0x00000000;
151 1.2 garbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
152 1.2 garbled "ofwpci io-space") != 0)
153 1.2 garbled panic("Can't init ofwpci io tag");
154 1.2 garbled
155 1.2 garbled sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
156 1.2 garbled sc->sc_memt.pbs_base = 0x00000000;
157 1.2 garbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
158 1.2 garbled "ofwpci mem-space") != 0)
159 1.2 garbled panic("Can't init ofwpci mem tag");
160 1.2 garbled
161 1.12 matt aprint_debug("io base=0x%"PRIxPTR" offset=0x%"PRIxPTR" limit=0x%"PRIxPTR"\n",
162 1.5 garbled sc->sc_iot.pbs_base, sc->sc_iot.pbs_offset, sc->sc_iot.pbs_limit);
163 1.5 garbled
164 1.12 matt aprint_debug("mem base=0x%"PRIxPTR" offset=0x%"PRIxPTR" limit=0x%"PRIxPTR"\n",
165 1.5 garbled sc->sc_memt.pbs_base, sc->sc_memt.pbs_offset,
166 1.5 garbled sc->sc_memt.pbs_limit);
167 1.5 garbled
168 1.5 garbled /* are we the primary pci bus? */
169 1.5 garbled if (of_find_firstchild_byname(OF_finddevice("/"), "pci") == node) {
170 1.5 garbled int isa_node;
171 1.5 garbled
172 1.5 garbled /* yes we are, now do we have an ISA child? */
173 1.5 garbled isa_node = of_find_firstchild_byname(node, "isa");
174 1.5 garbled if (isa_node != -1) {
175 1.5 garbled /* isa == pci */
176 1.5 garbled genppc_isa_io_space_tag = sc->sc_iot;
177 1.5 garbled genppc_isa_mem_space_tag = sc->sc_memt;
178 1.5 garbled map_isa_ioregs();
179 1.5 garbled init_ofppc_interrupt();
180 1.5 garbled ofppc_init_comcons(isa_node);
181 1.5 garbled }
182 1.5 garbled }
183 1.5 garbled
184 1.2 garbled ofwpci_get_chipset_tag(pc);
185 1.5 garbled
186 1.2 garbled pc->pc_node = node;
187 1.5 garbled i = OF_package_to_path(node, buf, sizeof(buf)-5);
188 1.5 garbled if (i <= 0)
189 1.5 garbled panic("Can't determine path for pci node %d", node);
190 1.5 garbled buf[i] = '\0';
191 1.5 garbled pc->pc_ihandle = OF_open(buf);
192 1.5 garbled if (pc->pc_ihandle < 0)
193 1.5 garbled panic("Can't open device %s", buf);
194 1.2 garbled pc->pc_bus = busrange[0];
195 1.2 garbled pc->pc_iot = &sc->sc_iot;
196 1.2 garbled pc->pc_memt = &sc->sc_memt;
197 1.2 garbled
198 1.18 thorpej pbi = kmem_alloc(sizeof(struct genppc_pci_chipset_businfo), KM_SLEEP);
199 1.2 garbled pbi->pbi_properties = prop_dictionary_create();
200 1.2 garbled KASSERT(pbi->pbi_properties != NULL);
201 1.2 garbled SIMPLEQ_INIT(&pc->pc_pbi);
202 1.2 garbled SIMPLEQ_INSERT_TAIL(&pc->pc_pbi, pbi, next);
203 1.2 garbled
204 1.4 garbled genofw_setup_pciintr_map((void *)pc, pbi, pc->pc_node);
205 1.5 garbled #ifdef PCI_NETBSD_CONFIGURE
206 1.17 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
207 1.17 thorpej
208 1.17 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
209 1.6 garbled modeldata.pciiodata[device_unit(self)].start,
210 1.17 thorpej (modeldata.pciiodata[device_unit(self)].limit -
211 1.17 thorpej modeldata.pciiodata[device_unit(self)].start) + 1);
212 1.17 thorpej
213 1.17 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
214 1.17 thorpej sc->sc_memt.pbs_base,
215 1.17 thorpej (sc->sc_memt.pbs_limit - sc->sc_memt.pbs_base) + 1);
216 1.5 garbled
217 1.17 thorpej if (pci_configure_bus(pc, pcires, 0, CACHELINESIZE))
218 1.5 garbled aprint_error("pci_configure_bus() failed\n");
219 1.5 garbled
220 1.17 thorpej pciconf_resource_fini(pcires);
221 1.5 garbled #endif /* PCI_NETBSD_CONFIGURE */
222 1.2 garbled memset(&pba, 0, sizeof(pba));
223 1.2 garbled pba.pba_memt = pc->pc_memt;
224 1.2 garbled pba.pba_iot = pc->pc_iot;
225 1.2 garbled pba.pba_dmat = &pci_bus_dma_tag;
226 1.2 garbled pba.pba_dmat64 = NULL;
227 1.2 garbled pba.pba_bus = pc->pc_bus;
228 1.2 garbled pba.pba_bridgetag = NULL;
229 1.2 garbled pba.pba_pc = pc;
230 1.9 dyoung pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
231 1.2 garbled config_found_ia(self, "pcibus", &pba, pcibusprint);
232 1.2 garbled }
233