bandit.c revision 1.25.38.2 1 /* $NetBSD: bandit.c,v 1.25.38.2 2007/05/06 05:11:41 macallan 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.25.38.2 2007/05/06 05:11:41 macallan 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
43 struct bandit_softc {
44 struct device sc_dev;
45 struct genppc_pci_chipset sc_pc;
46 };
47
48 static void bandit_attach(struct device *, struct device *, void *);
49 static int bandit_match(struct device *, struct cfdata *, void *);
50
51 static pcireg_t bandit_conf_read(void *, pcitag_t, int);
52 static void bandit_conf_write(void *, pcitag_t, int, pcireg_t);
53
54 static void bandit_init(struct bandit_softc *);
55
56 CFATTACH_DECL(bandit, sizeof(struct bandit_softc),
57 bandit_match, bandit_attach, NULL, NULL);
58
59 static int
60 bandit_match(struct device *parent, struct cfdata *cf, void *aux)
61 {
62 struct confargs *ca = aux;
63
64 if (strcmp(ca->ca_name, "bandit") == 0 ||
65 strcmp(ca->ca_name, "chaos") == 0)
66 return 1;
67
68 return 0;
69 }
70
71 static void
72 bandit_attach(struct device *parent, struct device *self, void *aux)
73 {
74 struct bandit_softc *sc = (void *)self;
75 pci_chipset_tag_t pc = &sc->sc_pc;
76 struct confargs *ca = aux;
77 struct pcibus_attach_args pba;
78 int len, node = ca->ca_node;
79 u_int32_t reg[2], busrange[2];
80 struct ranges {
81 u_int32_t pci_hi, pci_mid, pci_lo;
82 u_int32_t host;
83 u_int32_t size_hi, size_lo;
84 } ranges[6], *rp = ranges;
85
86 printf("\n");
87
88 /* Bandit address */
89 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
90 return;
91
92 /* PCI bus number */
93 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
94 return;
95
96 macppc_pci_get_chipset_tag(pc);
97 pc->pc_node = node;
98 pc->pc_addr = mapiodev(reg[0] + 0x800000, 4);
99 pc->pc_data = mapiodev(reg[0] + 0xc00000, 8);
100 pc->pc_bus = busrange[0];
101 pc->pc_conf_read = bandit_conf_read;
102 pc->pc_conf_write = bandit_conf_write;
103 pc->pc_memt = (bus_space_tag_t)0;
104
105 /* find i/o tag */
106 len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
107 if (len == -1)
108 return;
109 while (len >= sizeof(ranges[0])) {
110 if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
111 OFW_PCI_PHYS_HI_SPACE_IO)
112 pc->pc_iot = (bus_space_tag_t)rp->host;
113 len -= sizeof(ranges[0]);
114 rp++;
115 }
116
117 bandit_init(sc);
118
119 memset(&pba, 0, sizeof(pba));
120 pba.pba_memt = pc->pc_memt;
121 pba.pba_iot = pc->pc_iot;
122 pba.pba_dmat = &pci_bus_dma_tag;
123 pba.pba_dmat64 = NULL;
124 pba.pba_bus = pc->pc_bus;
125 pba.pba_bridgetag = NULL;
126 pba.pba_pc = pc;
127 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
128
129 config_found_ia(self, "pcibus", &pba, pcibusprint);
130 }
131
132 static pcireg_t
133 bandit_conf_read(void *cookie, pcitag_t tag, int reg)
134 {
135 pci_chipset_tag_t pc = cookie;
136 pcireg_t data;
137 int bus, dev, func, s;
138 u_int32_t x;
139
140 pci_decompose_tag(pc, tag, &bus, &dev, &func);
141
142 /*
143 * bandit's minimum device number of the first bus is 11.
144 * So we behave as if there is no device when dev < 11.
145 */
146 if (func > 7)
147 panic("pci_conf_read: func > 7");
148
149 if (bus == pc->pc_bus) {
150 if (dev < 11)
151 return 0xffffffff;
152 x = (1 << dev) | (func << 8) | reg;
153 } else
154 x = tag | reg | 1;
155
156 s = splhigh();
157
158 out32rb(pc->pc_addr, x);
159 DELAY(10);
160 data = 0xffffffff;
161 if (!badaddr(pc->pc_data, 4))
162 data = in32rb(pc->pc_data);
163 DELAY(10);
164 out32rb(pc->pc_addr, 0);
165 DELAY(10);
166
167 splx(s);
168
169 return data;
170 }
171
172 static void
173 bandit_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
174 {
175 pci_chipset_tag_t pc = cookie;
176 int bus, dev, func, s;
177 u_int32_t x;
178
179 pci_decompose_tag(pc, tag, &bus, &dev, &func);
180
181 if (func > 7)
182 panic("pci_conf_write: func > 7");
183
184 if (bus == pc->pc_bus) {
185 if (dev < 11)
186 panic("pci_conf_write: dev < 11");
187 x = (1 << dev) | (func << 8) | reg;
188 } else
189 x = tag | reg | 1;
190
191 s = splhigh();
192
193 out32rb(pc->pc_addr, x);
194 DELAY(10);
195 out32rb(pc->pc_data, data);
196 DELAY(10);
197 out32rb(pc->pc_addr, 0);
198 DELAY(10);
199
200 splx(s);
201 }
202
203 #define PCI_BANDIT 11
204
205 #define PCI_REG_MODE_SELECT 0x50
206
207 #define PCI_MODE_IO_COHERENT 0x040 /* I/O coherent */
208
209 static void
210 bandit_init(struct bandit_softc *sc)
211 {
212 pci_chipset_tag_t pc = &sc->sc_pc;
213 pcitag_t tag;
214 u_int mode;
215
216 tag = pci_make_tag(pc, pc->pc_bus, PCI_BANDIT, 0);
217 if ((pci_conf_read(pc, tag, PCI_ID_REG) & 0xffff) == 0xffff)
218 return;
219
220 mode = pci_conf_read(pc, tag, PCI_REG_MODE_SELECT);
221
222 if ((mode & PCI_MODE_IO_COHERENT) == 0) {
223 mode |= PCI_MODE_IO_COHERENT;
224 pci_conf_write(pc, tag, PCI_REG_MODE_SELECT, mode);
225 }
226 }
227