ofwpci.c revision 1.1.6.1 1 1.1.6.1 joerg /* $NetBSD: ofwpci.c,v 1.1.6.1 2007/10/26 15:43:05 joerg Exp $ */
2 1.1.6.1 joerg
3 1.1.6.1 joerg /*-
4 1.1.6.1 joerg * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.6.1 joerg * All rights reserved.
6 1.1.6.1 joerg *
7 1.1.6.1 joerg * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.1 joerg * by Tim Rightnour
9 1.1.6.1 joerg *
10 1.1.6.1 joerg * Redistribution and use in source and binary forms, with or without
11 1.1.6.1 joerg * modification, are permitted provided that the following conditions
12 1.1.6.1 joerg * are met:
13 1.1.6.1 joerg * 1. Redistributions of source code must retain the above copyright
14 1.1.6.1 joerg * notice, this list of conditions and the following disclaimer.
15 1.1.6.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.1 joerg * notice, this list of conditions and the following disclaimer in the
17 1.1.6.1 joerg * documentation and/or other materials provided with the distribution.
18 1.1.6.1 joerg * 3. All advertising materials mentioning features or use of this software
19 1.1.6.1 joerg * must display the following acknowledgement:
20 1.1.6.1 joerg * This product includes software developed by the NetBSD
21 1.1.6.1 joerg * Foundation, Inc. and its contributors.
22 1.1.6.1 joerg * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.6.1 joerg * contributors may be used to endorse or promote products derived
24 1.1.6.1 joerg * from this software without specific prior written permission.
25 1.1.6.1 joerg *
26 1.1.6.1 joerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.6.1 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.6.1 joerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.6.1 joerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.6.1 joerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.6.1 joerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.6.1 joerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.6.1 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.6.1 joerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.6.1 joerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.6.1 joerg * POSSIBILITY OF SUCH DAMAGE.
37 1.1.6.1 joerg */
38 1.1.6.1 joerg
39 1.1.6.1 joerg #include <sys/cdefs.h>
40 1.1.6.1 joerg __KERNEL_RCSID(0, "$NetBSD: ofwpci.c,v 1.1.6.1 2007/10/26 15:43:05 joerg Exp $");
41 1.1.6.1 joerg
42 1.1.6.1 joerg #include <sys/param.h>
43 1.1.6.1 joerg #include <sys/device.h>
44 1.1.6.1 joerg #include <sys/malloc.h>
45 1.1.6.1 joerg #include <sys/systm.h>
46 1.1.6.1 joerg
47 1.1.6.1 joerg #include <dev/pci/pcivar.h>
48 1.1.6.1 joerg #include <dev/ofw/openfirm.h>
49 1.1.6.1 joerg #include <dev/ofw/ofw_pci.h>
50 1.1.6.1 joerg
51 1.1.6.1 joerg #include <machine/autoconf.h>
52 1.1.6.1 joerg #include <machine/pio.h>
53 1.1.6.1 joerg
54 1.1.6.1 joerg struct ofwpci_softc {
55 1.1.6.1 joerg struct device sc_dev;
56 1.1.6.1 joerg struct genppc_pci_chipset sc_pc;
57 1.1.6.1 joerg struct powerpc_bus_space sc_iot;
58 1.1.6.1 joerg struct powerpc_bus_space sc_memt;
59 1.1.6.1 joerg };
60 1.1.6.1 joerg
61 1.1.6.1 joerg static void ofwpci_attach(struct device *, struct device *, void *);
62 1.1.6.1 joerg static int ofwpci_match(struct device *, struct cfdata *, void *);
63 1.1.6.1 joerg
64 1.1.6.1 joerg CFATTACH_DECL(ofwpci, sizeof(struct ofwpci_softc),
65 1.1.6.1 joerg ofwpci_match, ofwpci_attach, NULL, NULL);
66 1.1.6.1 joerg
67 1.1.6.1 joerg extern struct genppc_pci_chipset *genppc_pct;
68 1.1.6.1 joerg
69 1.1.6.1 joerg static void
70 1.1.6.1 joerg ofwpci_get_chipset_tag(pci_chipset_tag_t pc)
71 1.1.6.1 joerg {
72 1.1.6.1 joerg pc->pc_conf_v = (void *)pc;
73 1.1.6.1 joerg
74 1.1.6.1 joerg pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
75 1.1.6.1 joerg pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
76 1.1.6.1 joerg pc->pc_make_tag = genppc_pci_indirect_make_tag;
77 1.1.6.1 joerg pc->pc_conf_read = genppc_pci_indirect_conf_read;
78 1.1.6.1 joerg pc->pc_conf_write = genppc_pci_indirect_conf_write;
79 1.1.6.1 joerg
80 1.1.6.1 joerg pc->pc_intr_v = (void *)pc;
81 1.1.6.1 joerg
82 1.1.6.1 joerg pc->pc_intr_map = genofw_pci_intr_map;
83 1.1.6.1 joerg pc->pc_intr_string = genppc_pci_intr_string;
84 1.1.6.1 joerg pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
85 1.1.6.1 joerg pc->pc_intr_establish = genppc_pci_intr_establish;
86 1.1.6.1 joerg pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
87 1.1.6.1 joerg
88 1.1.6.1 joerg pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
89 1.1.6.1 joerg pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
90 1.1.6.1 joerg pc->pc_conf_hook = genofw_pci_conf_hook;
91 1.1.6.1 joerg
92 1.1.6.1 joerg pc->pc_addr = 0;
93 1.1.6.1 joerg pc->pc_data = 0;
94 1.1.6.1 joerg pc->pc_bus = 0;
95 1.1.6.1 joerg pc->pc_node = 0;
96 1.1.6.1 joerg pc->pc_memt = 0;
97 1.1.6.1 joerg pc->pc_iot = 0;
98 1.1.6.1 joerg }
99 1.1.6.1 joerg
100 1.1.6.1 joerg static int
101 1.1.6.1 joerg ofwpci_match(struct device *parent, struct cfdata *cf, void *aux)
102 1.1.6.1 joerg {
103 1.1.6.1 joerg struct confargs *ca = aux;
104 1.1.6.1 joerg char name[32];
105 1.1.6.1 joerg
106 1.1.6.1 joerg if (strcmp(ca->ca_name, "pci") != 0)
107 1.1.6.1 joerg return 0;
108 1.1.6.1 joerg
109 1.1.6.1 joerg memset(name, 0, sizeof(name));
110 1.1.6.1 joerg OF_getprop(ca->ca_node, "device-type", name, sizeof(name));
111 1.1.6.1 joerg if (strcmp(name, "pci") != 0)
112 1.1.6.1 joerg return 0;
113 1.1.6.1 joerg
114 1.1.6.1 joerg return 1;
115 1.1.6.1 joerg }
116 1.1.6.1 joerg
117 1.1.6.1 joerg static void
118 1.1.6.1 joerg ofwpci_attach(struct device *parent, struct device *self, void *aux)
119 1.1.6.1 joerg {
120 1.1.6.1 joerg struct ofwpci_softc *sc = (void *)self;
121 1.1.6.1 joerg pci_chipset_tag_t pc = &sc->sc_pc;
122 1.1.6.1 joerg struct confargs *ca = aux;
123 1.1.6.1 joerg struct pcibus_attach_args pba;
124 1.1.6.1 joerg struct genppc_pci_chipset_businfo *pbi;
125 1.1.6.1 joerg int node = ca->ca_node;
126 1.1.6.1 joerg uint32_t reg[2], busrange[2];
127 1.1.6.1 joerg #if 0
128 1.1.6.1 joerg struct ranges {
129 1.1.6.1 joerg uint32_t pci_hi, pci_mid, pci_lo;
130 1.1.6.1 joerg uint32_t host;
131 1.1.6.1 joerg uint32_t size_hi, size_lo;
132 1.1.6.1 joerg } ranges[6], *rp = ranges;
133 1.1.6.1 joerg #endif
134 1.1.6.1 joerg
135 1.1.6.1 joerg aprint_normal("\n");
136 1.1.6.1 joerg
137 1.1.6.1 joerg if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
138 1.1.6.1 joerg return;
139 1.1.6.1 joerg
140 1.1.6.1 joerg /* PCI bus number */
141 1.1.6.1 joerg if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
142 1.1.6.1 joerg return;
143 1.1.6.1 joerg
144 1.1.6.1 joerg /* bus space map the io ranges */
145 1.1.6.1 joerg sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
146 1.1.6.1 joerg sc->sc_iot.pbs_base = 0x00000000;
147 1.1.6.1 joerg if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
148 1.1.6.1 joerg "ofwpci io-space") != 0)
149 1.1.6.1 joerg panic("Can't init ofwpci io tag");
150 1.1.6.1 joerg
151 1.1.6.1 joerg sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
152 1.1.6.1 joerg sc->sc_memt.pbs_base = 0x00000000;
153 1.1.6.1 joerg if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
154 1.1.6.1 joerg "ofwpci mem-space") != 0)
155 1.1.6.1 joerg panic("Can't init ofwpci mem tag");
156 1.1.6.1 joerg
157 1.1.6.1 joerg ofwpci_get_chipset_tag(pc);
158 1.1.6.1 joerg pc->pc_node = node;
159 1.1.6.1 joerg pc->pc_addr = mapiodev(reg[0] + 0xcf8, 4);
160 1.1.6.1 joerg pc->pc_data = mapiodev(reg[0] + 0xcfc, 4);
161 1.1.6.1 joerg pc->pc_bus = busrange[0];
162 1.1.6.1 joerg pc->pc_iot = &sc->sc_iot;
163 1.1.6.1 joerg pc->pc_memt = &sc->sc_memt;
164 1.1.6.1 joerg
165 1.1.6.1 joerg pbi = malloc(sizeof(struct genppc_pci_chipset_businfo),
166 1.1.6.1 joerg M_DEVBUF, M_NOWAIT);
167 1.1.6.1 joerg KASSERT(pbi != NULL);
168 1.1.6.1 joerg pbi->pbi_properties = prop_dictionary_create();
169 1.1.6.1 joerg KASSERT(pbi->pbi_properties != NULL);
170 1.1.6.1 joerg SIMPLEQ_INIT(&pc->pc_pbi);
171 1.1.6.1 joerg SIMPLEQ_INSERT_TAIL(&pc->pc_pbi, pbi, next);
172 1.1.6.1 joerg
173 1.1.6.1 joerg genofw_setup_pciintr_map(pbi, pc->pc_node);
174 1.1.6.1 joerg
175 1.1.6.1 joerg memset(&pba, 0, sizeof(pba));
176 1.1.6.1 joerg pba.pba_memt = pc->pc_memt;
177 1.1.6.1 joerg pba.pba_iot = pc->pc_iot;
178 1.1.6.1 joerg pba.pba_dmat = &pci_bus_dma_tag;
179 1.1.6.1 joerg pba.pba_dmat64 = NULL;
180 1.1.6.1 joerg pba.pba_bus = pc->pc_bus;
181 1.1.6.1 joerg pba.pba_bridgetag = NULL;
182 1.1.6.1 joerg pba.pba_pc = pc;
183 1.1.6.1 joerg pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
184 1.1.6.1 joerg
185 1.1.6.1 joerg config_found_ia(self, "pcibus", &pba, pcibusprint);
186 1.1.6.1 joerg }
187