No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / ofppc / pci / pegasospci.c
blob0ff733fca34f72867b8a8e3a9ce0774269acf928
1 /* $NetBSD: pegasospci.c,v 1.13 2008/01/28 18:24:21 garbled Exp $ */
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pegasospci.c,v 1.13 2008/01/28 18:24:21 garbled Exp $");
35 #include "opt_pci.h"
37 #include <sys/param.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/extent.h>
41 #include <sys/systm.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pciconf.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_pci.h>
48 #include <machine/autoconf.h>
49 #include <machine/pio.h>
50 #include <machine/isa_machdep.h>
51 #include <machine/pegasosreg.h>
53 struct pegasospci_softc {
54 struct device sc_dev;
55 struct genppc_pci_chipset sc_pc;
56 struct powerpc_bus_space sc_iot;
57 struct powerpc_bus_space sc_memt;
60 static void pegasospci_attach(struct device *, struct device *, void *);
61 static int pegasospci_match(struct device *, struct cfdata *, void *);
62 static pcireg_t pegasospci_indirect_conf_read(void *, pcitag_t, int);
63 static void pegasospci_indirect_conf_write(void *, pcitag_t, int, pcireg_t);
65 CFATTACH_DECL(pegasospci, sizeof(struct pegasospci_softc),
66 pegasospci_match, pegasospci_attach, NULL, NULL);
68 extern struct genppc_pci_chipset *genppc_pct;
69 static u_int32_t *pciagp_magic;
71 static void
72 pegasospci_get_chipset_tag(pci_chipset_tag_t pc)
74 pc->pc_conf_v = (void *)pc;
76 pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
77 pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
78 pc->pc_make_tag = genppc_pci_indirect_make_tag;
79 pc->pc_conf_read = genppc_pci_indirect_conf_read;
80 pc->pc_conf_write = genppc_pci_indirect_conf_write;
82 pc->pc_intr_v = (void *)pc;
84 pc->pc_intr_map = genofw_pci_intr_map;
85 pc->pc_intr_string = genppc_pci_intr_string;
86 pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
87 pc->pc_intr_establish = genppc_pci_intr_establish;
88 pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
90 pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
91 pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
92 pc->pc_conf_hook = genofw_pci_conf_hook;
94 pc->pc_addr = 0;
95 pc->pc_data = 0;
96 pc->pc_bus = 0;
97 pc->pc_node = 0;
98 pc->pc_ihandle = 0;
99 pc->pc_memt = 0;
100 pc->pc_iot = 0;
103 static int
104 pegasospci_match(struct device *parent, struct cfdata *cf, void *aux)
106 struct confargs *ca = aux;
107 char name[32];
109 if (strcmp(ca->ca_name, "pci") != 0)
110 return 0;
112 memset(name, 0, sizeof(name));
113 OF_getprop(ca->ca_node, "device_type", name, sizeof(name));
114 if (strcmp(name, "pci") != 0)
115 return 0;
117 if (strcmp(model_name, "Pegasos2") != 0)
118 return 0;
120 return 10;
123 static void
124 pegasospci_attach(struct device *parent, struct device *self, void *aux)
126 struct pegasospci_softc *sc = (void *)self;
127 pci_chipset_tag_t pc = &sc->sc_pc;
128 struct confargs *ca = aux;
129 struct pcibus_attach_args pba;
130 struct genppc_pci_chipset_businfo *pbi;
131 int isprim = 0, node = ca->ca_node;
132 uint32_t reg[2], busrange[2];
133 #ifdef PCI_NETBSD_CONFIGURE
134 struct extent *ioext, *memext;
135 #endif
137 aprint_normal("\n");
139 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
140 return;
142 /* PCI bus number */
143 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
144 return;
146 /* bus space map the io ranges */
147 sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
148 sc->sc_iot.pbs_base = 0x00000000;
149 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
150 "pegasospci io-space") != 0)
151 panic("Can't init pegasospci io tag");
153 sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
154 sc->sc_memt.pbs_base = 0x00000000;
155 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
156 "pegasospci mem-space") != 0)
157 panic("Can't init pegasospci mem tag");
159 /* are we the primary pci bus? */
160 if (of_find_firstchild_byname(OF_finddevice("/"), "pci") == node) {
161 int isa_node;
163 isprim++;
164 /* yes we are, now do we have an ISA child? */
165 isa_node = of_find_firstchild_byname(node, "isa");
166 if (isa_node != -1) {
167 /* The Pegasos is very simple. isa == pci */
168 genppc_isa_io_space_tag = sc->sc_iot;
169 genppc_isa_mem_space_tag = sc->sc_memt;
170 map_isa_ioregs();
171 init_ofppc_interrupt();
172 ofppc_init_comcons(isa_node);
176 pegasospci_get_chipset_tag(pc);
178 pc->pc_node = node;
179 pc->pc_bus = busrange[0];
180 pc->pc_iot = &sc->sc_iot;
181 pc->pc_memt = &sc->sc_memt;
183 /* XXX Warning, the mapiodevs will go away if this region is later
184 * bus space initted or mapped
186 if (isprim) {
187 /* Pegasos2: primary PCI host (33MHz) @ 0x80000000 */
188 pc->pc_addr = mapiodev(PEGASOS2_PCI0_ADDR, 4);
189 pc->pc_data = mapiodev(PEGASOS2_PCI0_DATA, 4);
190 } else {
191 /* Pegasos2: second PCI host (66MHz) @ 0xc0000000 */
192 pc->pc_addr = mapiodev(PEGASOS2_PCI1_ADDR, 4);
193 pc->pc_data = mapiodev(PEGASOS2_PCI1_DATA, 4);
194 pciagp_magic = mapiodev(PEGASOS2_AGP_MAGIC, 8);
195 pc->pc_conf_read = pegasospci_indirect_conf_read;
196 pc->pc_conf_write = pegasospci_indirect_conf_write;
199 pbi = malloc(sizeof(struct genppc_pci_chipset_businfo),
200 M_DEVBUF, M_NOWAIT);
201 KASSERT(pbi != NULL);
202 pbi->pbi_properties = prop_dictionary_create();
203 KASSERT(pbi->pbi_properties != NULL);
204 SIMPLEQ_INIT(&pc->pc_pbi);
205 SIMPLEQ_INSERT_TAIL(&pc->pc_pbi, pbi, next);
207 genofw_setup_pciintr_map((void *)pc, pbi, pc->pc_node);
209 #ifdef PCI_NETBSD_CONFIGURE
210 if (isprim) {
211 ioext = extent_create("pciio", 0x00001400, 0x0000ffff,
212 M_DEVBUF, NULL, 0, EX_NOWAIT);
213 memext = extent_create("pcimem", sc->sc_memt.pbs_base,
214 sc->sc_memt.pbs_limit-1, M_DEVBUF, NULL, 0, EX_NOWAIT);
216 if (pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINESIZE))
217 printf("pci_configure_bus() failed\n");
219 extent_destroy(ioext);
220 extent_destroy(memext);
222 #endif /* PCI_NETBSD_CONFIGURE */
224 memset(&pba, 0, sizeof(pba));
225 pba.pba_memt = pc->pc_memt;
226 pba.pba_iot = pc->pc_iot;
227 pba.pba_dmat = &pci_bus_dma_tag;
228 pba.pba_dmat64 = NULL;
229 pba.pba_bus = pc->pc_bus;
230 pba.pba_bridgetag = NULL;
231 pba.pba_pc = pc;
232 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
234 config_found_ia(self, "pcibus", &pba, pcibusprint);
237 static pcireg_t
238 pegasospci_indirect_conf_read(void *cookie, pcitag_t tag, int reg)
240 pci_chipset_tag_t pc = cookie;
241 pcireg_t data;
242 int s;
244 s = splhigh();
246 out32rb(pciagp_magic, PEGASOS2_AGP_MAGIC_COOKIE);
247 out32rb(pc->pc_addr, tag | reg);
248 data = in32rb(pc->pc_data);
249 out32rb(pc->pc_addr, 0);
250 out32rb(pciagp_magic + 1, PEGASOS2_AGP_MAGIC_COOKIE);
252 splx(s);
254 return data;
257 static void
258 pegasospci_indirect_conf_write(void *cookie, pcitag_t tag, int reg,
259 pcireg_t data)
261 pci_chipset_tag_t pc = cookie;
262 int s;
264 s = splhigh();
266 out32rb(pciagp_magic, PEGASOS2_AGP_MAGIC_COOKIE);
267 out32rb(pc->pc_addr, tag | reg);
268 out32rb(pc->pc_data, data);
269 out32rb(pc->pc_addr, 0);
270 out32rb(pciagp_magic + 1, PEGASOS2_AGP_MAGIC_COOKIE);
272 splx(s);