xref: /netbsd-src/sys/arch/alpha/pci/mcpcia.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: mcpcia.c,v 1.29 2012/02/06 02:14:14 matt 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.29 2012/02/06 02:14:14 matt 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(uint32_t))
98 
99 static int	mcpciamatch(device_t, cfdata_t, void *);
100 static void	mcpciaattach(device_t, device_t, void *);
101 
102 CFATTACH_DECL_NEW(mcpcia, sizeof(struct mcpcia_softc),
103     mcpciamatch, mcpciaattach, NULL, NULL);
104 
105 void	mcpcia_init0(struct mcpcia_config *, int);
106 
107 /*
108  * We have one statically-allocated mcpcia_config structure; this is
109  * the one used for the console (which, coincidentally, is the only
110  * MCPCIA with an EISA adapter attached to it).
111  */
112 struct mcpcia_config mcpcia_console_configuration;
113 
114 int	mcpcia_bus_get_window(int, int,
115 	    struct alpha_bus_space_translation *abst);
116 
117 static int
118 mcpciamatch(device_t parent, cfdata_t cf, void *aux)
119 {
120 	struct mcbus_dev_attach_args *ma = aux;
121 	if (ma->ma_type == MCBUS_TYPE_PCI)
122 		return (1);
123 	return (0);
124 }
125 
126 static void
127 mcpciaattach(device_t parent, device_t self, void *aux)
128 {
129 	static int first = 1;
130 	struct mcbus_dev_attach_args *ma = aux;
131 	struct mcpcia_softc *mcp = device_private(self);
132 	struct mcpcia_config *ccp;
133 	struct pcibus_attach_args pba;
134 	uint32_t ctl;
135 
136 	/*
137 	 * Make sure this MCPCIA exists...
138 	 */
139 	if (MCPCIA_PROBE(ma->ma_mid, ma->ma_gid)) {
140 		mcp->mcpcia_cc = NULL;
141 		printf(" (not present)\n");
142 		return;
143 	}
144 	printf("\n");
145 
146 	/*
147 	 * Determine if we're the console's MCPCIA.
148 	 */
149 	if (ma->ma_mid == mcpcia_console_configuration.cc_mid &&
150 	    ma->ma_gid == mcpcia_console_configuration.cc_gid)
151 		ccp = &mcpcia_console_configuration;
152 	else {
153 		ccp = malloc(sizeof(struct mcpcia_config), M_DEVBUF, M_WAITOK);
154 		memset(ccp, 0, sizeof(struct mcpcia_config));
155 
156 		ccp->cc_mid = ma->ma_mid;
157 		ccp->cc_gid = ma->ma_gid;
158 	}
159 
160 	mcp->mcpcia_dev = self;
161 	mcp->mcpcia_cc = ccp;
162 	ccp->cc_sc = mcp;
163 
164 	/* This initializes cc_sysbase so we can do register access. */
165 	mcpcia_init0(ccp, 1);
166 
167 	ctl = REGVAL(MCPCIA_PCI_REV(ccp));
168 	aprint_normal_dev(self,
169 	    "Horse Revision %d, %s Handed Saddle Revision %d,"
170 	    " CAP Revision %d\n", HORSE_REV(ctl),
171 	    (SADDLE_TYPE(ctl) & 1)? "Right": "Left", SADDLE_REV(ctl),
172 	    CAP_REV(ctl));
173 
174 	mcpcia_dma_init(ccp);
175 
176 	/*
177 	 * Set up interrupts
178 	 */
179 	pci_kn300_pickintr(ccp, first);
180 	first = 0;
181 
182 	/*
183 	 * Attach PCI bus
184 	 */
185 	pba.pba_iot = &ccp->cc_iot;
186 	pba.pba_memt = &ccp->cc_memt;
187 	pba.pba_dmat =	/* start with direct, may change... */
188 	    alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI);
189 	pba.pba_dmat64 = NULL;
190 	pba.pba_pc = &ccp->cc_pc;
191 	pba.pba_bus = 0;
192 	pba.pba_bridgetag = NULL;
193 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
194 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
195 	(void) config_found_ia(self, "pcibus", &pba, pcibusprint);
196 
197 	/*
198 	 * Clear any errors that may have occurred during the probe
199 	 * sequence.
200 	 */
201 	REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
202 	alpha_mb();
203 }
204 
205 void
206 mcpcia_init(void)
207 {
208 	struct mcpcia_config *ccp = &mcpcia_console_configuration;
209 	int i;
210 
211 	/*
212 	 * Look for all of the MCPCIAs on the system.  One of them
213 	 * will have an EISA attached to it.  This MCPCIA is the
214 	 * only one that can be used for the console.  Once we find
215 	 * that one, initialize it.
216 	 */
217 
218 	for (i = 0; i < MCPCIA_PER_MCBUS; i++) {
219 		ccp->cc_mid = mcbus_mcpcia_probe_order[i];
220 		/*
221 		 * XXX If we ever support more than one MCBUS, we'll
222 		 * XXX have to probe for them, and map them to unit
223 		 * XXX numbers.
224 		 */
225 		ccp->cc_gid = MCBUS_GID_FROM_INSTANCE(0);
226 		ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
227 
228 		if (badaddr((void *)ALPHA_PHYS_TO_K0SEG(MCPCIA_PCI_REV(ccp)),
229 		    sizeof(uint32_t)))
230 			continue;
231 
232 		if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
233 			mcpcia_init0(ccp, 0);
234 
235 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 2;
236 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 3;
237 
238 			alpha_bus_get_window = mcpcia_bus_get_window;
239 			return;
240 		}
241 	}
242 
243 	panic("mcpcia_init: unable to find EISA bus");
244 }
245 
246 void
247 mcpcia_init0(struct mcpcia_config *ccp, int mallocsafe)
248 {
249 	uint32_t ctl;
250 
251 	if (ccp->cc_initted == 0) {
252 		/* don't do these twice since they set up extents */
253 		mcpcia_bus_io_init(&ccp->cc_iot, ccp);
254 		mcpcia_bus_mem_init(&ccp->cc_memt, ccp);
255 	}
256 	ccp->cc_mallocsafe = mallocsafe;
257 
258 	mcpcia_pci_init(&ccp->cc_pc, ccp);
259 
260 	/*
261 	 * Establish a precalculated base for convenience's sake.
262 	 */
263 	ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
264 
265 	/*
266  	 * Disable interrupts and clear errors prior to probing
267 	 */
268 	REGVAL(MCPCIA_INT_MASK0(ccp)) = 0;
269 	REGVAL(MCPCIA_INT_MASK1(ccp)) = 0;
270 	REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
271 	alpha_mb();
272 
273 	if (ccp == &mcpcia_console_configuration) {
274 		/*
275 		 * Use this opportunity to also find out the MID and CPU
276 		 * type of the currently running CPU (that's us, billybob....)
277 		 */
278 		ctl = REGVAL(MCPCIA_WHOAMI(ccp));
279 		mcbus_primary.mcbus_cpu_mid = MCBUS_CPU_MID(ctl);
280 		if ((MCBUS_CPU_INFO(ctl) & CPU_Fill_Err) == 0 &&
281 		    mcbus_primary.mcbus_valid == 0) {
282 			mcbus_primary.mcbus_bcache =
283 			    MCBUS_CPU_INFO(ctl) & CPU_BCacheMask;
284 			mcbus_primary.mcbus_valid = 1;
285 		}
286 		alpha_mb();
287 	}
288 
289 	ccp->cc_initted = 1;
290 }
291 
292 #ifdef TEST_PROBE_DEATH
293 static void
294 die_heathen_dog(void *arg)
295 {
296 	struct mcpcia_config *ccp = arg;
297 
298 	/* this causes a fatal machine check (0x670) */
299 	REGVAL(MCPCIA_CAP_DIAG(ccp)) |= CAP_DIAG_MC_ADRPE;
300 }
301 #endif
302 
303 void
304 mcpcia_config_cleanup(void)
305 {
306 	volatile uint32_t ctl;
307 	struct mcpcia_softc *mcp;
308 	struct mcpcia_config *ccp;
309 	int i;
310 	extern struct cfdriver mcpcia_cd;
311 
312 	/*
313 	 * Turn on Hard, Soft error interrupts. Maybe i2c too.
314 	 */
315 	for (i = 0; i < mcpcia_cd.cd_ndevs; i++) {
316 		if ((mcp = device_lookup_private(&mcpcia_cd, i)) == NULL)
317 			continue;
318 
319 		ccp = mcp->mcpcia_cc;
320 		if (ccp == NULL)
321 			continue;
322 
323 		ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
324 		ctl |= MCPCIA_GEN_IENABL;
325 		REGVAL(MCPCIA_INT_MASK0(ccp)) = ctl;
326 		alpha_mb();
327 
328 		/* force stall while write completes */
329 		ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
330 	}
331 #ifdef TEST_PROBE_DEATH
332 	(void) timeout (die_heathen_dog, &mcpcia_console_configuration,
333 	    30 * hz);
334 #endif
335 }
336 
337 int
338 mcpcia_bus_get_window(int type, int window, struct alpha_bus_space_translation *abst)
339 {
340 	struct mcpcia_config *ccp = &mcpcia_console_configuration;
341 	bus_space_tag_t st;
342 
343 	switch (type) {
344 	case ALPHA_BUS_TYPE_PCI_IO:
345 		st = &ccp->cc_iot;
346 		break;
347 
348 	case ALPHA_BUS_TYPE_PCI_MEM:
349 		st = &ccp->cc_memt;
350 		break;
351 
352 	default:
353 		panic("mcpcia_bus_get_window");
354 	}
355 
356 	return (alpha_bus_space_get_window(st, window, abst));
357 }
358