1/*	$NetBSD: scsirom.c,v 1.22 2021/08/07 16:19:07 thorpej 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 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 * SCSI BIOS ROM.
34 * Used to probe the board.
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: scsirom.c,v 1.22 2021/08/07 16:19:07 thorpej Exp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/device.h>
43
44#include <machine/bus.h>
45#include <machine/cpu.h>
46
47#include <arch/x68k/dev/intiovar.h>
48#include <arch/x68k/dev/scsiromvar.h>
49
50struct {
51	paddr_t addr, devaddr;
52	int intr;
53	const char id[7];
54} scsirom_descr[] = {{
55	0x00fc0000, 0x00e96020, 108, "SCSIIN"
56}, {
57	0x00ea0020, 0x00ea0000, 246, "SCSIEX"
58}};
59
60#define SCSIROM_ID	0x24
61
62/*
63 * autoconf stuff
64 */
65static int scsirom_find(device_t, struct intio_attach_args *);
66static int scsirom_match(device_t, cfdata_t, void *);
67static void scsirom_attach(device_t, device_t, void *);
68
69CFATTACH_DECL_NEW(scsirom, sizeof(struct scsirom_softc),
70    scsirom_match, scsirom_attach, NULL, NULL);
71
72static int
73scsirom_find(device_t parent, struct intio_attach_args *ia)
74{
75	bus_space_handle_t ioh;
76	char buf[10];
77	int which;
78	int r = -1;
79
80	if (ia->ia_addr == scsirom_descr[INTERNAL].addr)
81		which = INTERNAL;
82	else if (ia->ia_addr == scsirom_descr[EXTERNAL].addr)
83		which = EXTERNAL;
84	else
85		return -1;
86
87	ia->ia_size = 0x1fe0;
88	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY))
89		return -1;
90
91	if (bus_space_map(ia->ia_bst, ia->ia_addr, ia->ia_size, 0, &ioh) < 0)
92		return -1;
93	if (badaddr((void *)IIOV(ia->ia_addr+SCSIROM_ID))) {
94		bus_space_unmap(ia->ia_bst, ioh, ia->ia_size);
95		return -1;
96	}
97	bus_space_read_region_1(ia->ia_bst, ioh, SCSIROM_ID, buf, 6);
98	if (memcmp(buf, scsirom_descr[which].id, 6) == 0)
99		r = which;
100	bus_space_unmap(ia->ia_bst, ioh, ia->ia_size);
101
102	return r;
103}
104
105static int
106scsirom_match(device_t parent, cfdata_t cf, void *aux)
107{
108	struct intio_attach_args *ia = aux;
109	int r;
110
111	if (strcmp(ia->ia_name, "scsirom") != 0)
112		return 0;
113
114	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT) {
115		ia->ia_addr = scsirom_descr[0].addr;
116		r = scsirom_find(parent, ia);
117		if (r == INTERNAL)
118			return 1;
119		ia->ia_addr = scsirom_descr[1].addr;
120		r = scsirom_find(parent, ia);
121		if (r == EXTERNAL)
122			return 1;
123		return 0;
124	} else if (scsirom_find(parent, ia) >= 0)
125		return 1;
126	else
127		return 0;
128}
129
130static void
131scsirom_attach(device_t parent, device_t self, void *aux)
132{
133	struct scsirom_softc *sc = device_private(self);
134	struct intio_attach_args *ia = aux;
135	int r __diagused;
136	cfdata_t cf;
137
138	sc->sc_addr = ia->ia_addr;
139	sc->sc_which = scsirom_find(parent, ia);
140#ifdef DIAGNOSTIC
141	if (sc->sc_which < 0)
142		panic("SCSIROM curruption??");
143#endif
144	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
145#ifdef DIAGNOSTIC
146	if (r)
147		panic("IO map for SCSIROM corruption??");
148#endif
149
150	ia->ia_addr = scsirom_descr[sc->sc_which].devaddr;
151	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
152		ia->ia_intr = scsirom_descr[sc->sc_which].intr;
153
154	if (sc->sc_which == INTERNAL)
155		aprint_normal(": On-board at %p\n", (void *)ia->ia_addr);
156	else
157		aprint_normal(": External at %p\n", (void *)ia->ia_addr);
158
159	cf = config_search(self, ia,
160	    CFARGS_NONE);
161	if (cf) {
162		config_attach(self, cf, ia, NULL, CFARGS_NONE);
163	} else {
164		aprint_normal_dev(self, "no matching device; ignored.\n");
165	}
166
167	return;
168}
169