xref: /netbsd-src/sys/arch/x68k/dev/xel.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: xel.c,v 1.13 2007/03/11 08:09:26 isaki Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Minoura Makoto.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * TSR Xellent30 driver.
41  * Detect Xellent30, and reserve its I/O area.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: xel.c,v 1.13 2007/03/11 08:09:26 isaki Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/device.h>
51 
52 #include <machine/cpu.h>
53 #include <machine/bus.h>
54 
55 #include <arch/x68k/dev/intiovar.h>
56 
57 static paddr_t xel_addr(struct device *, struct cfdata *,
58 	struct intio_attach_args *);
59 static int xel_probe(paddr_t);
60 static int xel_match(struct device *, struct cfdata *, void *);
61 static void xel_attach(struct device *, struct device *, void *);
62 
63 struct xel_softc {
64 	struct device dev;
65 
66 	bus_space_tag_t sc_bst;
67 	bus_space_handle_t sc_bh;
68 };
69 
70 CFATTACH_DECL(xel, sizeof(struct xel_softc),
71     xel_match, xel_attach, NULL, NULL);
72 
73 static paddr_t xel_addrs[] = { 0xec0000, 0xec4000, 0xec8000, 0xecc000 };
74 
75 #define XEL_MODE_RAM_LOWER	0x00
76 #define XEL_MODE_RAM_HIGHER	0x01
77 #define XEL_MODE_UNMAP_RAM	0x00
78 #define XEL_MODE_MAP_RAM	0x02
79 #define XEL_MODE_MPU_000	0x00
80 #define XEL_MODE_MPU_030	0x04
81 
82 #define XEL_RAM_ADDR_LOWER	0xbc0000
83 #define XEL_RAM_ADDR_HIGHER	0xfc0000
84 
85 
86 static paddr_t
87 xel_addr(struct device *parent, struct cfdata *match,
88     struct intio_attach_args *ia)
89 {
90 	paddr_t addr = 0;
91 
92 	if (match->cf_addr == INTIOCF_ADDR_DEFAULT) {
93 		int i;
94 
95 		for (i = 0; i < sizeof(xel_addrs)/sizeof(xel_addrs[0]); i++) {
96 			if (xel_probe(xel_addrs[i])) {
97 				addr = xel_addrs[i];
98 				break;
99 			}
100 		}
101 	} else {
102 		if (xel_probe((paddr_t) match->cf_addr))
103 			addr = (paddr_t) match->cf_addr;
104 	}
105 
106 	if (addr) {
107 		/* found! */
108 		ia->ia_addr = (int) addr;
109 		ia->ia_size = 0x4000;
110 		if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY)
111 		    < 0)
112 			return 0;
113 		else
114 			return addr;
115 	}
116 
117 	return 0;
118 }
119 
120 extern int *nofault;
121 
122 static int
123 xel_probe(paddr_t addr)
124 {
125 	u_int32_t b1, b2;
126 	volatile u_int16_t *start = (volatile void *)INTIO_ADDR(addr);
127 	label_t	faultbuf;
128 	volatile u_int32_t *sram = (volatile void *)INTIO_ADDR(XEL_RAM_ADDR_HIGHER);
129 
130 	if (badaddr(start))
131 		return 0;
132 
133 	nofault = (int *) &faultbuf;
134 	if (setjmp(&faultbuf)) {
135 		nofault = NULL;
136 		return 0;
137 	}
138 
139 	b1 = sram[0];
140 	b2 = sram[1];
141 	/* Try to map the Xellent local memory. */
142 	start[0] = XEL_MODE_RAM_HIGHER | XEL_MODE_MAP_RAM | XEL_MODE_MPU_030;
143 
144 #if 0
145 	/* the contents should be deferent. */
146 	if (b1 == sram[0] && b2 == sram[1]) {
147 		nofault = (int *) 0;
148 		return 0;
149 	}
150 #else
151 	/* Try to write to the local memory. */
152 	sram[0] = 0x55555555;
153 	sram[1] = 0xaaaaaaaa;
154 	if (sram[0] != 0x55555555 || sram[1] != 0xaaaaaaaa) {
155 		sram[0] = b1;
156 		sram[1] = b2;
157 		nofault = (int *) 0;
158 		return 0;
159 	}
160 	sram[0] = 0xaaaaaaaa;
161 	sram[1] = 0x55555555;
162 	if (sram[0] != 0xaaaaaaaa || sram[1] != 0x55555555) {
163 		sram[0] = b1;
164 		sram[1] = b2;
165 		nofault = (int *) 0;
166 		return 0;
167 	}
168 	sram[0] = b1;
169 	sram[1] = b2;
170 #endif
171 
172 	/* Unmap. */
173 	start[0] = XEL_MODE_UNMAP_RAM | XEL_MODE_MPU_030;
174 
175 	nofault = NULL;
176 	return 1;
177 }
178 
179 static int
180 xel_match(struct device *parent, struct cfdata *match, void *aux)
181 {
182 	struct intio_attach_args *ia = aux;
183 
184 	if (strcmp(ia->ia_name, "xel") != 0)
185 		return 0;
186 
187 	if (xel_addr(parent, match, ia)) {
188 #ifdef DIAGNOSTIC
189 		if (cputype != CPU_68030)
190 			panic("Non-030 Xellent???");
191 #endif
192 		return 1;
193 	}
194 	return 0;
195 }
196 
197 static void
198 xel_attach(struct device *parent, struct device *self, void *aux)
199 {
200 	struct xel_softc *sc = (void *)self;
201 	struct intio_attach_args *ia = aux;
202 	struct cfdata *cf = device_cfdata(self);
203 	paddr_t addr;
204 	int r;
205 
206 	addr = xel_addr(parent, cf, aux);
207 	sc->sc_bst = ia->ia_bst;
208 	ia->ia_addr = (int) addr;
209 	ia->ia_size = 0x4000;
210 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
211 #ifdef DIAGNOSTIC
212 	if (r)
213 		panic("IO map for Xellent30 corruption??");
214 #endif
215 	printf(": Xellent30 MPU Accelerator.\n");
216 
217 	return;
218 }
219