xref: /netbsd/sys/arch/macppc/pci/bandit.c (revision 6550d01e)
1 /*	$NetBSD: bandit.c,v 1.26 2007/10/17 19:55:34 garbled Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: bandit.c,v 1.26 2007/10/17 19:55:34 garbled Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35 
36 #include <dev/pci/pcivar.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39 
40 #include <machine/autoconf.h>
41 #include <machine/pci_machdep.h>
42 #include <machine/pio.h>
43 
44 struct bandit_softc {
45 	struct device sc_dev;
46 	struct genppc_pci_chipset sc_pc;
47 	struct powerpc_bus_space sc_iot;
48 	struct powerpc_bus_space sc_memt;
49 };
50 
51 static void bandit_attach(struct device *, struct device *, void *);
52 static int bandit_match(struct device *, struct cfdata *, void *);
53 
54 static pcireg_t bandit_conf_read(void *, pcitag_t, int);
55 static void bandit_conf_write(void *, pcitag_t, int, pcireg_t);
56 
57 static void bandit_init(struct bandit_softc *);
58 
59 CFATTACH_DECL(bandit, sizeof(struct bandit_softc),
60     bandit_match, bandit_attach, NULL, NULL);
61 
62 static int
63 bandit_match(struct device *parent, struct cfdata *cf, void *aux)
64 {
65 	struct confargs *ca = aux;
66 
67 	if (strcmp(ca->ca_name, "bandit") == 0 ||
68 	    strcmp(ca->ca_name, "chaos") == 0)
69 		return 1;
70 
71 	return 0;
72 }
73 
74 static void
75 bandit_attach(struct device *parent, struct device *self, void *aux)
76 {
77 	struct bandit_softc *sc = (void *)self;
78 	pci_chipset_tag_t pc = &sc->sc_pc;
79 	struct confargs *ca = aux;
80 	struct pcibus_attach_args pba;
81 	int len, node = ca->ca_node;
82 	uint32_t reg[2], busrange[2];
83 	struct range {
84 		uint32_t pci_hi, pci_mid, pci_lo;
85 		uint32_t host;
86 		uint32_t size_hi, size_lo;
87 	} ranges[6], *rp = ranges;
88 
89 	aprint_normal("\n");
90 
91 	/* Bandit address */
92 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
93 		return;
94 
95 	/* PCI bus number */
96 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
97 		return;
98 
99 	/* find i/o tag */
100 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
101 	if (len == -1)
102 		return;
103 	while (len >= sizeof(ranges[0])) {
104 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
105 		     OFW_PCI_PHYS_HI_SPACE_IO) {
106 			sc->sc_iot.pbs_base = rp->host;
107 			sc->sc_iot.pbs_limit = rp->host + rp->size_lo;
108 			break;
109 		}
110 		len -= sizeof(ranges[0]);
111 		rp++;
112 	}
113 	memset(&sc->sc_iot, 0, sizeof(struct powerpc_bus_space));
114 	memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
115 	sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
116 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
117 	    "bandit io-space") != 0)
118 		panic("Can't init bandit io tag");
119 
120 	sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
121 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
122 	    "bandit mem-space") != 0)
123 		panic("Can't init bandit mem tag");
124 
125 	macppc_pci_get_chipset_tag(pc);
126 	pc->pc_node = node;
127 	pc->pc_iot = &sc->sc_iot;
128 	pc->pc_memt = &sc->sc_memt;
129 	pc->pc_addr = mapiodev(reg[0] + 0x800000, 4);
130 	pc->pc_data = mapiodev(reg[0] + 0xc00000, 8);
131 	pc->pc_bus = busrange[0];
132 	pc->pc_conf_read = bandit_conf_read;
133 	pc->pc_conf_write = bandit_conf_write;
134 
135 	bandit_init(sc);
136 
137 	memset(&pba, 0, sizeof(pba));
138 	pba.pba_memt = pc->pc_memt;
139 	pba.pba_iot = pc->pc_iot;
140 	pba.pba_dmat = &pci_bus_dma_tag;
141 	pba.pba_dmat64 = NULL;
142 	pba.pba_bus = pc->pc_bus;
143 	pba.pba_bridgetag = NULL;
144 	pba.pba_pc = pc;
145 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
146 
147 	config_found_ia(self, "pcibus", &pba, pcibusprint);
148 }
149 
150 static pcireg_t
151 bandit_conf_read(void *cookie, pcitag_t tag, int reg)
152 {
153 	pci_chipset_tag_t pc = cookie;
154 	pcireg_t data;
155 	int bus, dev, func, s;
156 	uint32_t x;
157 
158 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
159 
160 	/*
161 	 * bandit's minimum device number of the first bus is 11.
162 	 * So we behave as if there is no device when dev < 11.
163 	 */
164 	if (func > 7)
165 		panic("pci_conf_read: func > 7");
166 
167 	if (bus == pc->pc_bus) {
168 		if (dev < 11)
169 			return 0xffffffff;
170 		x = (1 << dev) | (func << 8) | reg;
171 	} else
172 		x = tag | reg | 1;
173 
174 	s = splhigh();
175 
176 	out32rb(pc->pc_addr, x);
177 	DELAY(10);
178 	data = 0xffffffff;
179 	if (!badaddr(pc->pc_data, 4))
180 		data = in32rb(pc->pc_data);
181 	DELAY(10);
182 	out32rb(pc->pc_addr, 0);
183 	DELAY(10);
184 
185 	splx(s);
186 
187 	return data;
188 }
189 
190 static void
191 bandit_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
192 {
193 	pci_chipset_tag_t pc = cookie;
194 	int bus, dev, func, s;
195 	u_int32_t x;
196 
197 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
198 
199 	if (func > 7)
200 		panic("pci_conf_write: func > 7");
201 
202 	if (bus == pc->pc_bus) {
203 		if (dev < 11)
204 			panic("pci_conf_write: dev < 11");
205 		x = (1 << dev) | (func << 8) | reg;
206 	} else
207 		x = tag | reg | 1;
208 
209 	s = splhigh();
210 
211 	out32rb(pc->pc_addr, x);
212 	DELAY(10);
213 	out32rb(pc->pc_data, data);
214 	DELAY(10);
215 	out32rb(pc->pc_addr, 0);
216 	DELAY(10);
217 
218 	splx(s);
219 }
220 
221 #define	PCI_BANDIT		11
222 
223 #define	PCI_REG_MODE_SELECT	0x50
224 
225 #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
226 
227 static void
228 bandit_init(struct bandit_softc *sc)
229 {
230 	pci_chipset_tag_t pc = &sc->sc_pc;
231 	pcitag_t tag;
232 	u_int mode;
233 
234 	tag = pci_make_tag(pc, pc->pc_bus, PCI_BANDIT, 0);
235 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
236 		return;
237 
238 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
239 
240 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
241 		mode |= PCI_MODE_IO_COHERENT;
242 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
243 	}
244 }
245