xref: /netbsd-src/sys/arch/macppc/pci/bandit.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: bandit.c,v 1.33 2021/04/24 23:36:41 thorpej 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.33 2021/04/24 23:36:41 thorpej 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 	device_t sc_dev;
46 	struct genppc_pci_chipset sc_pc;
47 	struct powerpc_bus_space sc_iot;
48 	struct powerpc_bus_space sc_memt;
49 	boolean_t sc_is_chaos;
50 };
51 
52 static void bandit_attach(device_t, device_t, void *);
53 static int bandit_match(device_t, cfdata_t, void *);
54 
55 static pcireg_t bandit_conf_read(void *, pcitag_t, int);
56 static void bandit_conf_write(void *, pcitag_t, int, pcireg_t);
57 static void chaos_conf_write(void *, pcitag_t, int, pcireg_t);
58 
59 static void bandit_init(struct bandit_softc *);
60 
61 CFATTACH_DECL_NEW(bandit, sizeof(struct bandit_softc),
62     bandit_match, bandit_attach, NULL, NULL);
63 
64 static int
65 bandit_match(device_t parent, cfdata_t cf, void *aux)
66 {
67 	struct confargs *ca = aux;
68 
69 	if (strcmp(ca->ca_name, "bandit") == 0 ||
70 	    strcmp(ca->ca_name, "chaos") == 0)
71 		return 1;
72 
73 	return 0;
74 }
75 
76 static void
77 bandit_attach(device_t parent, device_t self, void *aux)
78 {
79 	struct bandit_softc *sc = device_private(self);
80 	pci_chipset_tag_t pc = &sc->sc_pc;
81 	struct confargs *ca = aux;
82 	struct pcibus_attach_args pba;
83 	int len, node = ca->ca_node;
84 	uint32_t reg[2], busrange[2];
85 	struct range {
86 		uint32_t pci_hi, pci_mid, pci_lo;
87 		uint32_t host;
88 		uint32_t size_hi, size_lo;
89 	} ranges[6], *rp = ranges;
90 
91 	aprint_normal("\n");
92 	sc->sc_dev = self;
93 
94 	sc->sc_is_chaos = (strcmp(ca->ca_name, "chaos") == 0);
95 
96 	/* Bandit address */
97 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
98 		return;
99 
100 	/* PCI bus number */
101 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
102 		return;
103 
104 	/* find i/o tag */
105 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
106 	if (len == -1)
107 		return;
108 	while (len >= sizeof(ranges[0])) {
109 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
110 		     OFW_PCI_PHYS_HI_SPACE_IO) {
111 			sc->sc_iot.pbs_base = rp->host;
112 			sc->sc_iot.pbs_limit = rp->host + rp->size_lo;
113 			break;
114 		}
115 		len -= sizeof(ranges[0]);
116 		rp++;
117 	}
118 	memset(&sc->sc_iot, 0, sizeof(struct powerpc_bus_space));
119 	memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
120 	sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE;
121 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot,
122 	    "bandit io-space") != 0)
123 		panic("Can't init bandit io tag");
124 
125 	sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
126 	if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt,
127 	    "bandit mem-space") != 0)
128 		panic("Can't init bandit mem tag");
129 
130 	macppc_pci_get_chipset_tag(pc);
131 	pc->pc_node = node;
132 	pc->pc_iot = &sc->sc_iot;
133 	pc->pc_memt = &sc->sc_memt;
134 	pc->pc_addr = mapiodev(reg[0] + 0x800000, 4, false);
135 	pc->pc_data = mapiodev(reg[0] + 0xc00000, 8, false);
136 	pc->pc_bus = busrange[0];
137 	pc->pc_conf_read = bandit_conf_read;
138 	if (sc->sc_is_chaos) {
139 		pc->pc_conf_write = chaos_conf_write;
140 	} else
141 		pc->pc_conf_write = bandit_conf_write;
142 
143 	bandit_init(sc);
144 
145 	memset(&pba, 0, sizeof(pba));
146 	pba.pba_memt = pc->pc_memt;
147 	pba.pba_iot = pc->pc_iot;
148 	pba.pba_dmat = &pci_bus_dma_tag;
149 	pba.pba_dmat64 = NULL;
150 	pba.pba_bus = pc->pc_bus;
151 	pba.pba_bridgetag = NULL;
152 	pba.pba_pc = pc;
153 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
154 
155 	config_found(self, &pba, pcibusprint,
156 	    CFARG_DEVHANDLE, devhandle_from_of(node),
157 	    CFARG_EOL);
158 }
159 
160 static pcireg_t
161 bandit_conf_read(void *cookie, pcitag_t tag, int reg)
162 {
163 	pci_chipset_tag_t pc = cookie;
164 	pcireg_t data;
165 	int bus, dev, func, s;
166 	uint32_t x;
167 
168 	if ((unsigned int)reg >= PCI_CONF_SIZE)
169 		return (pcireg_t) -1;
170 
171 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
172 
173 	/*
174 	 * bandit's minimum device number of the first bus is 11.
175 	 * So we behave as if there is no device when dev < 11.
176 	 */
177 	if (func > 7)
178 		panic("pci_conf_read: func > 7");
179 
180 	if (bus == pc->pc_bus) {
181 		if (dev < 11)
182 			return 0xffffffff;
183 		x = (1 << dev) | (func << 8) | reg;
184 	} else
185 		x = tag | reg | 1;
186 
187 	s = splhigh();
188 
189 	out32rb(pc->pc_addr, x);
190 	DELAY(10);
191 	data = 0xffffffff;
192 	if (!badaddr(pc->pc_data, 4))
193 		data = in32rb(pc->pc_data);
194 	DELAY(10);
195 	out32rb(pc->pc_addr, 0);
196 	DELAY(10);
197 
198 	splx(s);
199 
200 	return data;
201 }
202 
203 static void
204 bandit_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
205 {
206 	pci_chipset_tag_t pc = cookie;
207 	int bus, dev, func, s;
208 	u_int32_t x;
209 
210 	if ((unsigned int)reg >= PCI_CONF_SIZE)
211 		return;
212 
213 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
214 
215 	if (func > 7)
216 		panic("pci_conf_write: func > 7");
217 
218 	if (bus == pc->pc_bus) {
219 		if (dev < 11)
220 			panic("pci_conf_write: dev < 11");
221 		x = (1 << dev) | (func << 8) | reg;
222 	} else
223 		x = tag | reg | 1;
224 
225 	s = splhigh();
226 
227 	out32rb(pc->pc_addr, x);
228 	DELAY(10);
229 	out32rb(pc->pc_data, data);
230 	DELAY(10);
231 	out32rb(pc->pc_addr, 0);
232 	DELAY(10);
233 
234 	splx(s);
235 }
236 
237 /*
238  * XXX
239  * /chaos really hates writes to config space, so we just don't do them
240  */
241 static void
242 chaos_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
243 {
244 }
245 
246 #define	PCI_BANDIT		11
247 
248 #define	PCI_REG_MODE_SELECT	0x50
249 
250 #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
251 
252 static void
253 bandit_init(struct bandit_softc *sc)
254 {
255 	pci_chipset_tag_t pc = &sc->sc_pc;
256 	pcitag_t tag;
257 	u_int mode;
258 
259 	tag = pci_make_tag(pc, pc->pc_bus, PCI_BANDIT, 0);
260 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
261 		return;
262 
263 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
264 
265 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
266 		mode |= PCI_MODE_IO_COHERENT;
267 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
268 	}
269 }
270