summaryrefslogtreecommitdiff
path: root/sys/arch/amiga/dev/acafh.c
blob: c5f85e9ec71d795db098d2448961ece7161488e1 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*	$NetBSD: acafh.c,v 1.5 2021/08/07 16:18:41 thorpej Exp $ */

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Radoslaw Kujawa.
 *
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acafh.c,v 1.5 2021/08/07 16:18:41 thorpej Exp $");

/*
 * Individual Computers ACA500 driver. 
 */

#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/types.h>

#include <uvm/uvm.h>

#include <machine/cpu.h>

#include <amiga/amiga/device.h>
#include <amiga/amiga/isr.h>

#include <amiga/dev/zbusvar.h>
#include <amiga/dev/acafhvar.h>
#include <amiga/dev/acafhreg.h>

int	acafh_match(device_t, cfdata_t , void *);
void	acafh_attach(device_t, device_t, void *);
static int acafh_print(void *, const char *);
static uint8_t acafh_reg_read(struct acafh_softc *, uint8_t);
static uint8_t acafh_revision(struct acafh_softc *);

CFATTACH_DECL_NEW(acafh, sizeof(struct acafh_softc),
    acafh_match, acafh_attach, NULL, NULL);

/*
 * Since ACA500 is not an AutoConfig board and current amiga port infrastructure
 * does not have typical obio attachment, we need to hack in the probe procedure
 * into mbattach(). This is supposed to be a temporary solution.
 */
bool
acafh_mbattach_probe(void)
{
	vaddr_t aca_rom_vbase;
	struct bus_space_tag aca_rom_bst;
	bus_space_tag_t aca_rom_t;
	bus_space_handle_t aca_rom_h;
	uint32_t aca_id;
	bool rv;

	rv = false;

#ifdef ACAFH_DEBUG
	aprint_normal("acafh: probing for ACA500\n");
#endif /* ACAFH_DEBUG */

	/* 
	 * Allocate VA to hold one mapped page, which we will use
	 * to access the beginning of ACA500 flash. 
	 */
	aca_rom_vbase = uvm_km_alloc(kernel_map,
	    PAGE_SIZE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);

	/* Create the physical to virtual mapping. */
	pmap_enter(vm_map_pmap(kernel_map), aca_rom_vbase, ACAFH_ROM_BASE,
	    VM_PROT_READ, PMAP_NOCACHE);
	pmap_update(vm_map_pmap(kernel_map));

	aca_rom_bst.base = (bus_addr_t) aca_rom_vbase;
        aca_rom_bst.absm = &amiga_bus_stride_1;
	aca_rom_t = &aca_rom_bst;
	bus_space_map(aca_rom_t, 0, PAGE_SIZE, 0, &aca_rom_h);

	/* Read out the ID. */
	aca_id = bus_space_read_4(aca_rom_t, aca_rom_h, ACAFH_ROM_ID_OFFSET);
#ifdef ACAFH_DEBUG
	aprint_normal("acafh: probe read %x from ACA ROM offset %x\n", aca_id, 
	    ACAFH_ROM_ID_OFFSET);
#endif /* ACAFH_DEBUG */

	if (aca_id == ACAFH_ROM_ID_VALUE)
		rv = true;
	else
		rv = false;

#ifdef ACAFH_DEBUG
	aprint_normal("acafh: clean up after probe\n");
#endif /* ACAFH_DEBUG */

	pmap_remove(vm_map_pmap(kernel_map), aca_rom_vbase, aca_rom_vbase + PAGE_SIZE);
        pmap_update(vm_map_pmap(kernel_map));

	uvm_km_free(kernel_map, aca_rom_vbase, PAGE_SIZE, 
	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);

	return rv;
}

int
acafh_match(device_t parent, cfdata_t cf, void *aux)
{
	if (matchname(aux, "acafh") == 0)
		return(0);
	return(1);
}

void
acafh_attach(device_t parent, device_t self, void *aux)
{
	struct acafh_softc *sc;
	vaddr_t aca_vbase;
	int i;
	struct acafhbus_attach_args aaa_wdc;
	struct acafhbus_attach_args aaa_cp;

	sc = device_private(self);
	sc->sc_dev = self;

	/* 
	 * Allocate enough kernel memory.
	 * XXX: we should be sure to prepare enough kva during early init... 
	 */
	aca_vbase = uvm_km_alloc(kernel_map,
	    ACAFH_END - ACAFH_BASE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);

	if (aca_vbase == 0) {
		aprint_error_dev(sc->sc_dev, 
		    "failed allocating virtual memory\n");
		return;
	}

	/* 
	 * Map the ACA500 registers into kernel virutal space.
	 */
	for (i = ACAFH_BASE; i < ACAFH_END; i += PAGE_SIZE)
		pmap_enter(vm_map_pmap(kernel_map),
			i - ACAFH_BASE + aca_vbase, i,
			   VM_PROT_READ | VM_PROT_WRITE, true);
		pmap_update(vm_map_pmap(kernel_map));

	aca_vbase += ACAFH_FIRST_REG_OFF;

	sc->sc_aca_bst.base = (bus_addr_t) aca_vbase;
	sc->sc_aca_bst.absm = &amiga_bus_stride_1;
/*	sc->sc_aca_bst.absm = &amiga_bus_stride_0x4000; */
	sc->sc_aca_iot = &sc->sc_aca_bst;

	bus_space_map(sc->sc_aca_iot, 0, 0xF, 0, &sc->sc_aca_ioh);

#ifdef ACAFH_DEBUG
	aprint_normal_dev(sc->sc_dev, 
	    "ACA500 registers mapped to pa %x (va %x)\n",
	    kvtop((void*)sc->sc_aca_ioh), sc->sc_aca_bst.base);
	aprint_normal_dev(sc->sc_dev, "AUX intr enable %x\n", 
	    acafh_reg_read(sc, ACAFH_MEMPROBE_AUXIRQ));
#endif /* ACAFH_DEBUG */

	aprint_normal(": Individual Computers ACA500 (rev %x)\n",
	    acafh_revision(sc));

	aprint_normal_dev(sc->sc_dev, "CF cards present: ");
	if (acafh_reg_read(sc, ACAFH_CF_DETECT_BOOT)) {
		aprint_normal("BOOT ");	
	}
	if (acafh_reg_read(sc, ACAFH_CF_DETECT_AUX)) {
		aprint_normal("AUX ");	
	}
	aprint_normal("\n");

	aaa_wdc.aaa_pbase = (bus_addr_t) GAYLE_IDE_BASE + 2;
	strcpy(aaa_wdc.aaa_name, "wdc_acafh");
	config_found(sc->sc_dev, &aaa_wdc, acafh_print, CFARGS_NONE);

	aaa_cp.aaa_pbase = (bus_addr_t) ACAFH_CLOCKPORT_BASE;
	strcpy(aaa_cp.aaa_name, "gencp_acafh");
	config_found(sc->sc_dev, &aaa_cp, acafh_print, CFARGS_NONE);
}

uint8_t
acafh_cf_intr_status(struct acafh_softc *sc, uint8_t slot) 
{
	uint8_t status;

	if (slot == 0) {
		status = acafh_reg_read(sc, ACAFH_CF_IRQ_BOOT);
	} else {
		status = acafh_reg_read(sc, ACAFH_CF_IRQ_AUX);
	}

	return status;
}

static uint8_t
acafh_revision(struct acafh_softc *sc)
{
	uint8_t revbit0, revbit1, revbit2, revbit3;

	revbit3 = acafh_reg_read(sc, ACAFH_VERSION_BIT3);
	revbit2 = acafh_reg_read(sc, ACAFH_VERSION_BIT2);
	revbit1 = acafh_reg_read(sc, ACAFH_VERSION_BIT1);
	revbit0 = acafh_reg_read(sc, ACAFH_VERSION_BIT0);

	return revbit0 | (revbit1 << 1) | (revbit2 << 2) | (revbit3 << 3);
}

static uint8_t
acafh_reg_read(struct acafh_softc *sc, uint8_t reg)
{
	uint16_t val;

	val = bus_space_read_2(sc->sc_aca_iot, sc->sc_aca_ioh, reg * 0x4000);
#ifdef ACAFH_DEBUG
	aprint_normal_dev(sc->sc_dev, "read out reg %x from %lx (%x), val %x\n",
	    reg, sc->sc_aca_ioh + reg, (bus_addr_t) kvtop((void*) 
	    ((unsigned) sc->sc_aca_ioh + reg * 0x4000)), val);
#endif /* ACAFH_DEBUG */

	return (val & ACAFH_MSB_MASK) >> ACAFH_MSB_SHIFT;
}

static int
acafh_print(void *aux, const char *w)
{
	if (w == NULL)
		return 0;

	return 0;
}