summaryrefslogtreecommitdiff
path: root/sys/arch/x68k/dev/scsirom.c
blob: a31b1a3a06608e82fcabcc958eea8514af352c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*	$NetBSD: scsirom.c,v 1.22 2021/08/07 16:19:07 thorpej Exp $	*/

/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Minoura Makoto.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * SCSI BIOS ROM.
 * Used to probe the board.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: scsirom.c,v 1.22 2021/08/07 16:19:07 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>
#include <machine/cpu.h>

#include <arch/x68k/dev/intiovar.h>
#include <arch/x68k/dev/scsiromvar.h>

struct {
	paddr_t addr, devaddr;
	int intr;
	const char id[7];
} scsirom_descr[] = {{
	0x00fc0000, 0x00e96020, 108, "SCSIIN"
}, {
	0x00ea0020, 0x00ea0000, 246, "SCSIEX"
}};

#define SCSIROM_ID	0x24

/*
 * autoconf stuff
 */
static int scsirom_find(device_t, struct intio_attach_args *);
static int scsirom_match(device_t, cfdata_t, void *);
static void scsirom_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(scsirom, sizeof(struct scsirom_softc),
    scsirom_match, scsirom_attach, NULL, NULL);

static int
scsirom_find(device_t parent, struct intio_attach_args *ia)
{
	bus_space_handle_t ioh;
	char buf[10];
	int which;
	int r = -1;

	if (ia->ia_addr == scsirom_descr[INTERNAL].addr)
		which = INTERNAL;
	else if (ia->ia_addr == scsirom_descr[EXTERNAL].addr)
		which = EXTERNAL;
	else
		return -1;

	ia->ia_size = 0x1fe0;
	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY))
		return -1;

	if (bus_space_map(ia->ia_bst, ia->ia_addr, ia->ia_size, 0, &ioh) < 0)
		return -1;
	if (badaddr((void *)IIOV(ia->ia_addr+SCSIROM_ID))) {
		bus_space_unmap(ia->ia_bst, ioh, ia->ia_size);
		return -1;
	}
	bus_space_read_region_1(ia->ia_bst, ioh, SCSIROM_ID, buf, 6);
	if (memcmp(buf, scsirom_descr[which].id, 6) == 0)
		r = which;
	bus_space_unmap(ia->ia_bst, ioh, ia->ia_size);

	return r;
}

static int
scsirom_match(device_t parent, cfdata_t cf, void *aux)
{
	struct intio_attach_args *ia = aux;
	int r;

	if (strcmp(ia->ia_name, "scsirom") != 0)
		return 0;

	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT) {
		ia->ia_addr = scsirom_descr[0].addr;
		r = scsirom_find(parent, ia);
		if (r == INTERNAL)
			return 1;
		ia->ia_addr = scsirom_descr[1].addr;
		r = scsirom_find(parent, ia);
		if (r == EXTERNAL)
			return 1;
		return 0;
	} else if (scsirom_find(parent, ia) >= 0)
		return 1;
	else
		return 0;
}

static void
scsirom_attach(device_t parent, device_t self, void *aux)
{
	struct scsirom_softc *sc = device_private(self);
	struct intio_attach_args *ia = aux;
	int r __diagused;
	cfdata_t cf;

	sc->sc_addr = ia->ia_addr;
	sc->sc_which = scsirom_find(parent, ia);
#ifdef DIAGNOSTIC
	if (sc->sc_which < 0)
		panic("SCSIROM curruption??");
#endif
	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
#ifdef DIAGNOSTIC
	if (r)
		panic("IO map for SCSIROM corruption??");
#endif

	ia->ia_addr = scsirom_descr[sc->sc_which].devaddr;
	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
		ia->ia_intr = scsirom_descr[sc->sc_which].intr;

	if (sc->sc_which == INTERNAL)
		aprint_normal(": On-board at %p\n", (void *)ia->ia_addr);
	else
		aprint_normal(": External at %p\n", (void *)ia->ia_addr);

	cf = config_search(self, ia,
	    CFARGS_NONE);
	if (cf) {
		config_attach(self, cf, ia, NULL, CFARGS_NONE);
	} else {
		aprint_normal_dev(self, "no matching device; ignored.\n");
	}

	return;
}