bandit.c revision 1.23 1 /* $NetBSD: bandit.c,v 1.23 2003/07/15 02:43:33 lukem 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.23 2003/07/15 02:43:33 lukem 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
42 struct bandit_softc {
43 struct device sc_dev;
44 struct pci_bridge sc_pc;
45 };
46
47 void bandit_attach __P((struct device *, struct device *, void *));
48 int bandit_match __P((struct device *, struct cfdata *, void *));
49 int bandit_print __P((void *, const char *));
50
51 pcireg_t bandit_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
52 void bandit_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
53
54 static void bandit_init __P((struct bandit_softc *));
55
56 CFATTACH_DECL(bandit, sizeof(struct bandit_softc),
57 bandit_match, bandit_attach, NULL, NULL);
58
59 int
60 bandit_match(parent, cf, aux)
61 struct device *parent;
62 struct cfdata *cf;
63 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 void
75 bandit_attach(parent, self, aux)
76 struct device *parent, *self;
77 void *aux;
78 {
79 struct bandit_softc *sc = (void *)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 u_int32_t reg[2], busrange[2];
85 struct ranges {
86 u_int32_t pci_hi, pci_mid, pci_lo;
87 u_int32_t host;
88 u_int32_t size_hi, size_lo;
89 } ranges[6], *rp = ranges;
90
91 printf("\n");
92
93 /* Bandit address */
94 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
95 return;
96
97 /* PCI bus number */
98 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
99 return;
100
101 pc->node = node;
102 pc->addr = mapiodev(reg[0] + 0x800000, 4);
103 pc->data = mapiodev(reg[0] + 0xc00000, 8);
104 pc->bus = busrange[0];
105 pc->conf_read = bandit_conf_read;
106 pc->conf_write = bandit_conf_write;
107 pc->memt = (bus_space_tag_t)0;
108
109 /* find i/o tag */
110 len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
111 if (len == -1)
112 return;
113 while (len >= sizeof(ranges[0])) {
114 if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
115 OFW_PCI_PHYS_HI_SPACE_IO)
116 pc->iot = (bus_space_tag_t)rp->host;
117 len -= sizeof(ranges[0]);
118 rp++;
119 }
120
121 bandit_init(sc);
122
123 memset(&pba, 0, sizeof(pba));
124 pba.pba_busname = "pci";
125 pba.pba_memt = pc->memt;
126 pba.pba_iot = pc->iot;
127 pba.pba_dmat = &pci_bus_dma_tag;
128 pba.pba_dmat64 = NULL;
129 pba.pba_bus = pc->bus;
130 pba.pba_bridgetag = NULL;
131 pba.pba_pc = pc;
132 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
133
134 config_found(self, &pba, bandit_print);
135 }
136
137 int
138 bandit_print(aux, pnp)
139 void *aux;
140 const char *pnp;
141 {
142 struct pcibus_attach_args *pa = aux;
143
144 if (pnp)
145 aprint_normal("%s at %s", pa->pba_busname, pnp);
146 aprint_normal(" bus %d", pa->pba_bus);
147 return UNCONF;
148 }
149
150 pcireg_t
151 bandit_conf_read(pc, tag, reg)
152 pci_chipset_tag_t pc;
153 pcitag_t tag;
154 int reg;
155 {
156 pcireg_t data;
157 int bus, dev, func, s;
158 u_int32_t x;
159
160 pci_decompose_tag(pc, tag, &bus, &dev, &func);
161
162 /*
163 * bandit's minimum device number of the first bus is 11.
164 * So we behave as if there is no device when dev < 11.
165 */
166 if (func > 7)
167 panic("pci_conf_read: func > 7");
168
169 if (bus == pc->bus) {
170 if (dev < 11)
171 return 0xffffffff;
172 x = (1 << dev) | (func << 8) | reg;
173 } else
174 x = tag | reg | 1;
175
176 s = splhigh();
177
178 out32rb(pc->addr, x);
179 DELAY(10);
180 data = 0xffffffff;
181 if (!badaddr(pc->data, 4))
182 data = in32rb(pc->data);
183 DELAY(10);
184 out32rb(pc->addr, 0);
185 DELAY(10);
186
187 splx(s);
188
189 return data;
190 }
191
192 void
193 bandit_conf_write(pc, tag, reg, data)
194 pci_chipset_tag_t pc;
195 pcitag_t tag;
196 int reg;
197 pcireg_t data;
198 {
199 int bus, dev, func, s;
200 u_int32_t x;
201
202 pci_decompose_tag(pc, tag, &bus, &dev, &func);
203
204 if (func > 7)
205 panic("pci_conf_write: func > 7");
206
207 if (bus == pc->bus) {
208 if (dev < 11)
209 panic("pci_conf_write: dev < 11");
210 x = (1 << dev) | (func << 8) | reg;
211 } else
212 x = tag | reg | 1;
213
214 s = splhigh();
215
216 out32rb(pc->addr, x);
217 DELAY(10);
218 out32rb(pc->data, data);
219 DELAY(10);
220 out32rb(pc->addr, 0);
221 DELAY(10);
222
223 splx(s);
224 }
225
226 #define PCI_BANDIT 11
227
228 #define PCI_REG_MODE_SELECT 0x50
229
230 #define PCI_MODE_IO_COHERENT 0x040 /* I/O coherent */
231
232 void
233 bandit_init(sc)
234 struct bandit_softc *sc;
235 {
236 pci_chipset_tag_t pc = &sc->sc_pc;
237 pcitag_t tag;
238 u_int mode;
239
240 tag = pci_make_tag(pc, pc->bus, PCI_BANDIT, 0);
241 if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
242 return;
243
244 mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
245
246 if ((mode & PCI_MODE_IO_COHERENT) == 0) {
247 mode |= PCI_MODE_IO_COHERENT;
248 pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
249 }
250 }
251