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