1/*	$NetBSD: xel.c,v 1.17 2008/12/31 09:50:21 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * TSR Xellent30 driver.
34 * Detect Xellent30, and reserve its I/O area.
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: xel.c,v 1.17 2008/12/31 09:50:21 isaki Exp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/device.h>
44
45#include <machine/cpu.h>
46#include <machine/bus.h>
47
48#include <arch/x68k/dev/intiovar.h>
49
50static paddr_t xel_addr(device_t, cfdata_t, struct intio_attach_args *);
51static int xel_probe(paddr_t);
52static int xel_match(device_t, cfdata_t, void *);
53static void xel_attach(device_t, device_t, void *);
54
55struct xel_softc {
56	bus_space_tag_t sc_bst;
57	bus_space_handle_t sc_bh;
58};
59
60CFATTACH_DECL_NEW(xel, sizeof(struct xel_softc),
61    xel_match, xel_attach, NULL, NULL);
62
63static paddr_t xel_addrs[] = { 0xec0000, 0xec4000, 0xec8000, 0xecc000 };
64
65#define XEL_MODE_RAM_LOWER	0x00
66#define XEL_MODE_RAM_HIGHER	0x01
67#define XEL_MODE_UNMAP_RAM	0x00
68#define XEL_MODE_MAP_RAM	0x02
69#define XEL_MODE_MPU_000	0x00
70#define XEL_MODE_MPU_030	0x04
71
72#define XEL_RAM_ADDR_LOWER	0xbc0000
73#define XEL_RAM_ADDR_HIGHER	0xfc0000
74
75
76static paddr_t
77xel_addr(device_t parent, cfdata_t match, struct intio_attach_args *ia)
78{
79	paddr_t addr = 0;
80
81	if (match->cf_addr == INTIOCF_ADDR_DEFAULT) {
82		int i;
83
84		for (i = 0; i < sizeof(xel_addrs)/sizeof(xel_addrs[0]); i++) {
85			if (xel_probe(xel_addrs[i])) {
86				addr = xel_addrs[i];
87				break;
88			}
89		}
90	} else {
91		if (xel_probe((paddr_t) match->cf_addr))
92			addr = (paddr_t) match->cf_addr;
93	}
94
95	if (addr) {
96		/* found! */
97		ia->ia_addr = (int) addr;
98		ia->ia_size = 0x4000;
99		if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY)
100		    < 0)
101			return 0;
102		else
103			return addr;
104	}
105
106	return 0;
107}
108
109extern int *nofault;
110
111static int
112xel_probe(paddr_t addr)
113{
114	u_int32_t b1, b2;
115	volatile u_int16_t *start = (volatile u_int16_t *)IIOV(addr);
116	label_t	faultbuf;
117	volatile u_int32_t *sram = (volatile u_int32_t *)IIOV(XEL_RAM_ADDR_HIGHER);
118
119	if (badaddr(start))
120		return 0;
121
122	nofault = (int *) &faultbuf;
123	if (setjmp(&faultbuf)) {
124		nofault = NULL;
125		return 0;
126	}
127
128	b1 = sram[0];
129	b2 = sram[1];
130	/* Try to map the Xellent local memory. */
131	start[0] = XEL_MODE_RAM_HIGHER | XEL_MODE_MAP_RAM | XEL_MODE_MPU_030;
132
133#if 0
134	/* the contents should be deferent. */
135	if (b1 == sram[0] && b2 == sram[1]) {
136		nofault = (int *) 0;
137		return 0;
138	}
139#else
140	/* Try to write to the local memory. */
141	sram[0] = 0x55555555;
142	sram[1] = 0xaaaaaaaa;
143	if (sram[0] != 0x55555555 || sram[1] != 0xaaaaaaaa) {
144		sram[0] = b1;
145		sram[1] = b2;
146		nofault = (int *) 0;
147		return 0;
148	}
149	sram[0] = 0xaaaaaaaa;
150	sram[1] = 0x55555555;
151	if (sram[0] != 0xaaaaaaaa || sram[1] != 0x55555555) {
152		sram[0] = b1;
153		sram[1] = b2;
154		nofault = (int *) 0;
155		return 0;
156	}
157	sram[0] = b1;
158	sram[1] = b2;
159#endif
160
161	/* Unmap. */
162	start[0] = XEL_MODE_UNMAP_RAM | XEL_MODE_MPU_030;
163
164	nofault = NULL;
165	return 1;
166}
167
168static int
169xel_match(device_t parent, cfdata_t match, void *aux)
170{
171	struct intio_attach_args *ia = aux;
172
173	if (strcmp(ia->ia_name, "xel") != 0)
174		return 0;
175
176	if (xel_addr(parent, match, ia)) {
177#ifdef DIAGNOSTIC
178		if (cputype != CPU_68030)
179			panic("Non-030 Xellent???");
180#endif
181		return 1;
182	}
183	return 0;
184}
185
186static void
187xel_attach(device_t parent, device_t self, void *aux)
188{
189	struct xel_softc *sc = device_private(self);
190	struct intio_attach_args *ia = aux;
191	cfdata_t cf = device_cfdata(self);
192	paddr_t addr;
193	int r __diagused;
194
195	addr = xel_addr(parent, cf, aux);
196	sc->sc_bst = ia->ia_bst;
197	ia->ia_addr = (int) addr;
198	ia->ia_size = 0x4000;
199	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
200#ifdef DIAGNOSTIC
201	if (r)
202		panic("IO map for Xellent30 corruption??");
203#endif
204	aprint_normal(": Xellent30 MPU Accelerator.\n");
205
206	return;
207}
208