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