mcpcia.c revision 1.23 1 /* $NetBSD: mcpcia.c,v 1.23 2009/03/14 14:45:53 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1998 by Matthew Jacob
35 * NASA AMES Research Center.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice immediately at the beginning of the file, without modification,
43 * this list of conditions, and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
54 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 /*
64 * MCPCIA mcbus to PCI bus adapter
65 * found on AlphaServer 4100 systems.
66 */
67
68 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
69
70 __KERNEL_RCSID(0, "$NetBSD: mcpcia.c,v 1.23 2009/03/14 14:45:53 dsl Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/device.h>
75 #include <sys/malloc.h>
76
77 #include <machine/autoconf.h>
78 #include <machine/rpb.h>
79 #include <machine/sysarch.h>
80
81 #include <alpha/mcbus/mcbusreg.h>
82 #include <alpha/mcbus/mcbusvar.h>
83 #include <alpha/pci/mcpciareg.h>
84 #include <alpha/pci/mcpciavar.h>
85 #include <alpha/pci/pci_kn300.h>
86
87 #define KV(_addr) ((void *)ALPHA_PHYS_TO_K0SEG((_addr)))
88 #define MCPCIA_SYSBASE(mc) \
89 ((((unsigned long) (mc)->cc_gid) << MCBUS_GID_SHIFT) | \
90 (((unsigned long) (mc)->cc_mid) << MCBUS_MID_SHIFT) | \
91 (MCBUS_IOSPACE))
92
93 #define MCPCIA_PROBE(mid, gid) \
94 badaddr((void *)KV(((((unsigned long) gid) << MCBUS_GID_SHIFT) | \
95 (((unsigned long) mid) << MCBUS_MID_SHIFT) | \
96 (MCBUS_IOSPACE) | MCPCIA_PCI_BRIDGE | _MCPCIA_PCI_REV)), \
97 sizeof(u_int32_t))
98
99 static int mcpciamatch(struct device *, struct cfdata *, void *);
100 static void mcpciaattach(struct device *, struct device *, void *);
101 CFATTACH_DECL(mcpcia, sizeof(struct mcpcia_softc),
102 mcpciamatch, mcpciaattach, NULL, NULL);
103
104 void mcpcia_init0(struct mcpcia_config *, int);
105
106 /*
107 * We have one statically-allocated mcpcia_config structure; this is
108 * the one used for the console (which, coincidentally, is the only
109 * MCPCIA with an EISA adapter attached to it).
110 */
111 struct mcpcia_config mcpcia_console_configuration;
112
113 int mcpcia_bus_get_window(int, int,
114 struct alpha_bus_space_translation *abst);
115
116 static int
117 mcpciamatch(parent, cf, aux)
118 struct device *parent;
119 struct cfdata *cf;
120 void *aux;
121 {
122 struct mcbus_dev_attach_args *ma = aux;
123 if (ma->ma_type == MCBUS_TYPE_PCI)
124 return (1);
125 return (0);
126 }
127
128 static void
129 mcpciaattach(parent, self, aux)
130 struct device *parent;
131 struct device *self;
132 void *aux;
133 {
134 static int first = 1;
135 struct mcbus_dev_attach_args *ma = aux;
136 struct mcpcia_softc *mcp = (struct mcpcia_softc *)self;
137 struct mcpcia_config *ccp;
138 struct pcibus_attach_args pba;
139 u_int32_t ctl;
140
141 /*
142 * Make sure this MCPCIA exists...
143 */
144 if (MCPCIA_PROBE(ma->ma_mid, ma->ma_gid)) {
145 mcp->mcpcia_cc = NULL;
146 printf(" (not present)\n");
147 return;
148 }
149 printf("\n");
150
151 /*
152 * Determine if we're the console's MCPCIA.
153 */
154 if (ma->ma_mid == mcpcia_console_configuration.cc_mid &&
155 ma->ma_gid == mcpcia_console_configuration.cc_gid)
156 ccp = &mcpcia_console_configuration;
157 else {
158 ccp = malloc(sizeof(struct mcpcia_config), M_DEVBUF, M_WAITOK);
159 memset(ccp, 0, sizeof(struct mcpcia_config));
160
161 ccp->cc_mid = ma->ma_mid;
162 ccp->cc_gid = ma->ma_gid;
163 }
164
165 mcp->mcpcia_cc = ccp;
166 ccp->cc_sc = mcp;
167
168 /* This initializes cc_sysbase so we can do register access. */
169 mcpcia_init0(ccp, 1);
170
171 ctl = REGVAL(MCPCIA_PCI_REV(ccp));
172 printf("%s: Horse Revision %d, %s Handed Saddle Revision %d,"
173 " CAP Revision %d\n", mcp->mcpcia_dev.dv_xname, HORSE_REV(ctl),
174 (SADDLE_TYPE(ctl) & 1)? "Right": "Left", SADDLE_REV(ctl),
175 CAP_REV(ctl));
176
177 mcpcia_dma_init(ccp);
178
179 /*
180 * Set up interrupts
181 */
182 pci_kn300_pickintr(ccp, first);
183 first = 0;
184
185 /*
186 * Attach PCI bus
187 */
188 pba.pba_iot = &ccp->cc_iot;
189 pba.pba_memt = &ccp->cc_memt;
190 pba.pba_dmat = /* start with direct, may change... */
191 alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI);
192 pba.pba_dmat64 = NULL;
193 pba.pba_pc = &ccp->cc_pc;
194 pba.pba_bus = 0;
195 pba.pba_bridgetag = NULL;
196 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
197 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
198 (void) config_found_ia(self, "pcibus", &pba, pcibusprint);
199
200 /*
201 * Clear any errors that may have occurred during the probe
202 * sequence.
203 */
204 REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
205 alpha_mb();
206 }
207
208 void
209 mcpcia_init()
210 {
211 struct mcpcia_config *ccp = &mcpcia_console_configuration;
212 int i;
213
214 /*
215 * Look for all of the MCPCIAs on the system. One of them
216 * will have an EISA attached to it. This MCPCIA is the
217 * only one that can be used for the console. Once we find
218 * that one, initialize it.
219 */
220
221 for (i = 0; i < MCPCIA_PER_MCBUS; i++) {
222 ccp->cc_mid = mcbus_mcpcia_probe_order[i];
223 /*
224 * XXX If we ever support more than one MCBUS, we'll
225 * XXX have to probe for them, and map them to unit
226 * XXX numbers.
227 */
228 ccp->cc_gid = MCBUS_GID_FROM_INSTANCE(0);
229 ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
230
231 if (badaddr((void *)ALPHA_PHYS_TO_K0SEG(MCPCIA_PCI_REV(ccp)),
232 sizeof(u_int32_t)))
233 continue;
234
235 if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
236 mcpcia_init0(ccp, 0);
237
238 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 2;
239 alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 3;
240
241 alpha_bus_get_window = mcpcia_bus_get_window;
242 return;
243 }
244 }
245
246 panic("mcpcia_init: unable to find EISA bus");
247 }
248
249 void
250 mcpcia_init0(ccp, mallocsafe)
251 struct mcpcia_config *ccp;
252 int mallocsafe;
253 {
254 u_int32_t ctl;
255
256 if (ccp->cc_initted == 0) {
257 /* don't do these twice since they set up extents */
258 mcpcia_bus_io_init(&ccp->cc_iot, ccp);
259 mcpcia_bus_mem_init(&ccp->cc_memt, ccp);
260 }
261 ccp->cc_mallocsafe = mallocsafe;
262
263 mcpcia_pci_init(&ccp->cc_pc, ccp);
264
265 /*
266 * Establish a precalculated base for convenience's sake.
267 */
268 ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
269
270 /*
271 * Disable interrupts and clear errors prior to probing
272 */
273 REGVAL(MCPCIA_INT_MASK0(ccp)) = 0;
274 REGVAL(MCPCIA_INT_MASK1(ccp)) = 0;
275 REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
276 alpha_mb();
277
278 if (ccp == &mcpcia_console_configuration) {
279 /*
280 * Use this opportunity to also find out the MID and CPU
281 * type of the currently running CPU (that's us, billybob....)
282 */
283 ctl = REGVAL(MCPCIA_WHOAMI(ccp));
284 mcbus_primary.mcbus_cpu_mid = MCBUS_CPU_MID(ctl);
285 if ((MCBUS_CPU_INFO(ctl) & CPU_Fill_Err) == 0 &&
286 mcbus_primary.mcbus_valid == 0) {
287 mcbus_primary.mcbus_bcache =
288 MCBUS_CPU_INFO(ctl) & CPU_BCacheMask;
289 mcbus_primary.mcbus_valid = 1;
290 }
291 alpha_mb();
292 }
293
294 ccp->cc_initted = 1;
295 }
296
297 #ifdef TEST_PROBE_DEATH
298 static void
299 die_heathen_dog(arg)
300 void *arg;
301 {
302 struct mcpcia_config *ccp = arg;
303
304 /* this causes a fatal machine check (0x670) */
305 REGVAL(MCPCIA_CAP_DIAG(ccp)) |= CAP_DIAG_MC_ADRPE;
306 }
307 #endif
308
309 void
310 mcpcia_config_cleanup()
311 {
312 volatile u_int32_t ctl;
313 struct mcpcia_softc *mcp;
314 struct mcpcia_config *ccp;
315 int i;
316 extern struct cfdriver mcpcia_cd;
317
318 /*
319 * Turn on Hard, Soft error interrupts. Maybe i2c too.
320 */
321 for (i = 0; i < mcpcia_cd.cd_ndevs; i++) {
322 if ((mcp = device_lookup_private(&mcpcia_cd, i)) == NULL)
323 continue;
324
325 ccp = mcp->mcpcia_cc;
326 if (ccp == NULL)
327 continue;
328
329 ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
330 ctl |= MCPCIA_GEN_IENABL;
331 REGVAL(MCPCIA_INT_MASK0(ccp)) = ctl;
332 alpha_mb();
333
334 /* force stall while write completes */
335 ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
336 }
337 #ifdef TEST_PROBE_DEATH
338 (void) timeout (die_heathen_dog, &mcpcia_console_configuration,
339 30 * hz);
340 #endif
341 }
342
343 int
344 mcpcia_bus_get_window(type, window, abst)
345 int type, window;
346 struct alpha_bus_space_translation *abst;
347 {
348 struct mcpcia_config *ccp = &mcpcia_console_configuration;
349 bus_space_tag_t st;
350
351 switch (type) {
352 case ALPHA_BUS_TYPE_PCI_IO:
353 st = &ccp->cc_iot;
354 break;
355
356 case ALPHA_BUS_TYPE_PCI_MEM:
357 st = &ccp->cc_memt;
358 break;
359
360 default:
361 panic("mcpcia_bus_get_window");
362 }
363
364 return (alpha_bus_space_get_window(st, window, abst));
365 }
366