xref: /netbsd-src/sys/arch/macppc/pci/bandit.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: bandit.c,v 1.17 2001/09/14 21:04:58 nathanw 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/param.h>
30 #include <sys/device.h>
31 #include <sys/systm.h>
32 
33 #include <dev/pci/pcivar.h>
34 #include <dev/ofw/openfirm.h>
35 #include <dev/ofw/ofw_pci.h>
36 
37 #include <machine/autoconf.h>
38 
39 struct bandit_softc {
40 	struct device sc_dev;
41 	struct pci_bridge sc_pc;
42 };
43 
44 void bandit_attach __P((struct device *, struct device *, void *));
45 int bandit_match __P((struct device *, struct cfdata *, void *));
46 int bandit_print __P((void *, const char *));
47 
48 pcireg_t bandit_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
49 void bandit_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
50 
51 static void bandit_init __P((struct bandit_softc *));
52 
53 struct cfattach bandit_ca = {
54 	sizeof(struct bandit_softc), bandit_match, bandit_attach
55 };
56 
57 int
58 bandit_match(parent, cf, aux)
59 	struct device *parent;
60 	struct cfdata *cf;
61 	void *aux;
62 {
63 	struct confargs *ca = aux;
64 
65 	if (strcmp(ca->ca_name, "bandit") == 0 ||
66 	    strcmp(ca->ca_name, "chaos") == 0)
67 		return 1;
68 
69 	return 0;
70 }
71 
72 void
73 bandit_attach(parent, self, aux)
74 	struct device *parent, *self;
75 	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 	u_int32_t reg[2], busrange[2];
83 	struct ranges {
84 		u_int32_t pci_hi, pci_mid, pci_lo;
85 		u_int32_t host;
86 		u_int32_t size_hi, size_lo;
87 	} ranges[6], *rp = ranges;
88 
89 	printf("\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 	pc->node = node;
100 	pc->addr = mapiodev(reg[0] + 0x800000, 4);
101 	pc->data = mapiodev(reg[0] + 0xc00000, 8);
102 	pc->bus = busrange[0];
103 	pc->conf_read = bandit_conf_read;
104 	pc->conf_write = bandit_conf_write;
105 	pc->memt = (bus_space_tag_t)0;
106 
107 	/* find i/o tag */
108 	len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
109 	if (len == -1)
110 		return;
111 	while (len >= sizeof(ranges[0])) {
112 		if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
113 		     OFW_PCI_PHYS_HI_SPACE_IO)
114 			pc->iot = (bus_space_tag_t)rp->host;
115 		len -= sizeof(ranges[0]);
116 		rp++;
117 	}
118 
119 	bandit_init(sc);
120 
121 	memset(&pba, 0, sizeof(pba));
122 	pba.pba_busname = "pci";
123 	pba.pba_memt = pc->memt;
124 	pba.pba_iot = pc->iot;
125 	pba.pba_dmat = &pci_bus_dma_tag;
126 	pba.pba_bus = pc->bus;
127 	pba.pba_pc = pc;
128 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
129 
130 	config_found(self, &pba, bandit_print);
131 }
132 
133 int
134 bandit_print(aux, pnp)
135 	void *aux;
136 	const char *pnp;
137 {
138 	struct pcibus_attach_args *pa = aux;
139 
140 	if (pnp)
141 		printf("%s at %s", pa->pba_busname, pnp);
142 	printf(" bus %d", pa->pba_bus);
143 	return UNCONF;
144 }
145 
146 pcireg_t
147 bandit_conf_read(pc, tag, reg)
148 	pci_chipset_tag_t pc;
149 	pcitag_t tag;
150 	int reg;
151 {
152 	pcireg_t data;
153 	int bus, dev, func, s;
154 	u_int32_t x;
155 
156 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
157 
158 	/*
159 	 * bandit's minimum device number of the first bus is 11.
160 	 * So we behave as if there is no device when dev < 11.
161 	 */
162 	if (func > 7)
163 		panic("pci_conf_read: func > 7");
164 
165 	if (bus == pc->bus) {
166 		if (dev < 11)
167 			return 0xffffffff;
168 		x = (1 << dev) | (func << 8) | reg;
169 	} else
170 		x = tag | reg | 1;
171 
172 	s = splhigh();
173 
174 	out32rb(pc->addr, x);
175 	DELAY(10);
176 	data = 0xffffffff;
177 	if (!badaddr(pc->data, 4))
178 		data = in32rb(pc->data);
179 	DELAY(10);
180 	out32rb(pc->addr, 0);
181 	DELAY(10);
182 
183 	splx(s);
184 
185 	return data;
186 }
187 
188 void
189 bandit_conf_write(pc, tag, reg, data)
190 	pci_chipset_tag_t pc;
191 	pcitag_t tag;
192 	int reg;
193 	pcireg_t data;
194 {
195 	int bus, dev, func, s;
196 	u_int32_t x;
197 
198 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
199 
200 	if (func > 7)
201 		panic("pci_conf_write: func > 7");
202 
203 	if (bus == pc->bus) {
204 		if (dev < 11)
205 			panic("pci_conf_write: dev < 11");
206 		x = (1 << dev) | (func << 8) | reg;
207 	} else
208 		x = tag | reg | 1;
209 
210 	s = splhigh();
211 
212 	out32rb(pc->addr, x);
213 	DELAY(10);
214 	out32rb(pc->data, data);
215 	DELAY(10);
216 	out32rb(pc->addr, 0);
217 	DELAY(10);
218 
219 	splx(s);
220 }
221 
222 #define	PCI_BANDIT		11
223 
224 #define	PCI_REG_MODE_SELECT	0x50
225 
226 #define	PCI_MODE_IO_COHERENT	0x040	/* I/O coherent */
227 
228 void
229 bandit_init(sc)
230 	struct bandit_softc *sc;
231 {
232 	pci_chipset_tag_t pc = &sc->sc_pc;
233 	pcitag_t tag;
234 	u_int mode;
235 
236 	tag = pci_make_tag(pc, pc->bus, PCI_BANDIT, 0);
237 	if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
238 		return;
239 
240 	mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
241 
242 	if ((mode & PCI_MODE_IO_COHERENT) == 0) {
243 		mode |= PCI_MODE_IO_COHERENT;
244 		pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
245 	}
246 }
247