1/*	$NetBSD: acafh.c,v 1.7 2023/12/20 00:40:42 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Radoslaw Kujawa.
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#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: acafh.c,v 1.7 2023/12/20 00:40:42 thorpej Exp $");
34
35/*
36 * Individual Computers ACA500 driver.
37 */
38
39#include <sys/param.h>
40#include <sys/device.h>
41#include <sys/socket.h>
42#include <sys/systm.h>
43#include <sys/bus.h>
44#include <sys/types.h>
45
46#include <uvm/uvm.h>
47
48#include <machine/cpu.h>
49
50#include <amiga/amiga/device.h>
51#include <amiga/amiga/isr.h>
52
53#include <amiga/dev/zbusvar.h>
54#include <amiga/dev/acafhvar.h>
55#include <amiga/dev/acafhreg.h>
56
57int	acafh_match(device_t, cfdata_t , void *);
58void	acafh_attach(device_t, device_t, void *);
59static int acafh_print(void *, const char *);
60static uint8_t acafh_reg_read(struct acafh_softc *, uint8_t);
61static uint8_t acafh_revision(struct acafh_softc *);
62
63CFATTACH_DECL_NEW(acafh, sizeof(struct acafh_softc),
64    acafh_match, acafh_attach, NULL, NULL);
65
66/*
67 * Since ACA500 is not an AutoConfig board and current amiga port infrastructure
68 * does not have typical obio attachment, we need to hack in the probe procedure
69 * into mbattach(). This is supposed to be a temporary solution.
70 */
71bool
72acafh_mbattach_probe(void)
73{
74	vaddr_t aca_rom_vbase;
75	struct bus_space_tag aca_rom_bst;
76	bus_space_tag_t aca_rom_t;
77	bus_space_handle_t aca_rom_h;
78	uint32_t aca_id;
79	bool rv;
80
81	rv = false;
82
83#ifdef ACAFH_DEBUG
84	aprint_normal("acafh: probing for ACA500\n");
85#endif /* ACAFH_DEBUG */
86
87	/*
88	 * Allocate VA to hold one mapped page, which we will use
89	 * to access the beginning of ACA500 flash.
90	 */
91	aca_rom_vbase = uvm_km_alloc(kernel_map,
92	    PAGE_SIZE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
93
94	/* Create the physical to virtual mapping. */
95	pmap_enter(vm_map_pmap(kernel_map), aca_rom_vbase, ACAFH_ROM_BASE,
96	    VM_PROT_READ, PMAP_NOCACHE);
97	pmap_update(vm_map_pmap(kernel_map));
98
99	aca_rom_bst.base = (bus_addr_t) aca_rom_vbase;
100        aca_rom_bst.absm = &amiga_bus_stride_1;
101	aca_rom_t = &aca_rom_bst;
102	bus_space_map(aca_rom_t, 0, PAGE_SIZE, 0, &aca_rom_h);
103
104	/* Read out the ID. */
105	aca_id = bus_space_read_4(aca_rom_t, aca_rom_h, ACAFH_ROM_ID_OFFSET);
106#ifdef ACAFH_DEBUG
107	aprint_normal("acafh: probe read %x from ACA ROM offset %x\n", aca_id,
108	    ACAFH_ROM_ID_OFFSET);
109#endif /* ACAFH_DEBUG */
110
111	if (aca_id == ACAFH_ROM_ID_VALUE)
112		rv = true;
113	else
114		rv = false;
115
116#ifdef ACAFH_DEBUG
117	aprint_normal("acafh: clean up after probe\n");
118#endif /* ACAFH_DEBUG */
119
120	pmap_remove(vm_map_pmap(kernel_map), aca_rom_vbase, aca_rom_vbase + PAGE_SIZE);
121        pmap_update(vm_map_pmap(kernel_map));
122
123	uvm_km_free(kernel_map, aca_rom_vbase, PAGE_SIZE,
124	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
125
126	return rv;
127}
128
129int
130acafh_match(device_t parent, cfdata_t cf, void *aux)
131{
132	if (matchname(aux, "acafh") == 0)
133		return(0);
134	return(1);
135}
136
137void
138acafh_attach(device_t parent, device_t self, void *aux)
139{
140	struct acafh_softc *sc;
141	vaddr_t aca_vbase;
142	int i;
143	struct acafhbus_attach_args aaa_wdc;
144	struct acafhbus_attach_args aaa_cp;
145
146	sc = device_private(self);
147	sc->sc_dev = self;
148
149	/*
150	 * Allocate enough kernel memory.
151	 * XXX: we should be sure to prepare enough kva during early init...
152	 */
153	aca_vbase = uvm_km_alloc(kernel_map,
154	    ACAFH_END - ACAFH_BASE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
155
156	if (aca_vbase == 0) {
157		aprint_error_dev(sc->sc_dev,
158		    "failed allocating virtual memory\n");
159		return;
160	}
161
162	/*
163	 * Map the ACA500 registers into kernel virutal space.
164	 */
165	for (i = ACAFH_BASE; i < ACAFH_END; i += PAGE_SIZE)
166		pmap_enter(vm_map_pmap(kernel_map),
167			i - ACAFH_BASE + aca_vbase, i,
168			   VM_PROT_READ | VM_PROT_WRITE, true);
169	pmap_update(vm_map_pmap(kernel_map));
170
171	aca_vbase += ACAFH_FIRST_REG_OFF;
172
173	sc->sc_aca_bst.base = (bus_addr_t) aca_vbase;
174	sc->sc_aca_bst.absm = &amiga_bus_stride_1;
175/*	sc->sc_aca_bst.absm = &amiga_bus_stride_0x4000; */
176	sc->sc_aca_iot = &sc->sc_aca_bst;
177
178	bus_space_map(sc->sc_aca_iot, 0, 0xF, 0, &sc->sc_aca_ioh);
179
180#ifdef ACAFH_DEBUG
181	aprint_normal_dev(sc->sc_dev,
182	    "ACA500 registers mapped to pa %x (va %x)\n",
183	    kvtop((void*)sc->sc_aca_ioh), sc->sc_aca_bst.base);
184	aprint_normal_dev(sc->sc_dev, "AUX intr enable %x\n",
185	    acafh_reg_read(sc, ACAFH_MEMPROBE_AUXIRQ));
186#endif /* ACAFH_DEBUG */
187
188	aprint_normal(": Individual Computers ACA500 (rev %x)\n",
189	    acafh_revision(sc));
190
191	aprint_normal_dev(sc->sc_dev, "CF cards present: ");
192	if (acafh_reg_read(sc, ACAFH_CF_DETECT_BOOT)) {
193		aprint_normal("BOOT ");
194	}
195	if (acafh_reg_read(sc, ACAFH_CF_DETECT_AUX)) {
196		aprint_normal("AUX ");
197	}
198	aprint_normal("\n");
199
200	aaa_wdc.aaa_pbase = (bus_addr_t) GAYLE_IDE_BASE + 2;
201	strcpy(aaa_wdc.aaa_name, "wdc_acafh");
202	config_found(sc->sc_dev, &aaa_wdc, acafh_print, CFARGS_NONE);
203
204	aaa_cp.aaa_pbase = (bus_addr_t) ACAFH_CLOCKPORT_BASE;
205	strcpy(aaa_cp.aaa_name, "gencp_acafh");
206	config_found(sc->sc_dev, &aaa_cp, acafh_print, CFARGS_NONE);
207}
208
209uint8_t
210acafh_cf_intr_status(struct acafh_softc *sc, uint8_t slot)
211{
212	uint8_t status;
213
214	if (slot == 0) {
215		status = acafh_reg_read(sc, ACAFH_CF_IRQ_BOOT);
216	} else {
217		status = acafh_reg_read(sc, ACAFH_CF_IRQ_AUX);
218	}
219
220	return status;
221}
222
223static uint8_t
224acafh_revision(struct acafh_softc *sc)
225{
226	uint8_t revbit0, revbit1, revbit2, revbit3;
227
228	revbit3 = acafh_reg_read(sc, ACAFH_VERSION_BIT3);
229	revbit2 = acafh_reg_read(sc, ACAFH_VERSION_BIT2);
230	revbit1 = acafh_reg_read(sc, ACAFH_VERSION_BIT1);
231	revbit0 = acafh_reg_read(sc, ACAFH_VERSION_BIT0);
232
233	return revbit0 | (revbit1 << 1) | (revbit2 << 2) | (revbit3 << 3);
234}
235
236static uint8_t
237acafh_reg_read(struct acafh_softc *sc, uint8_t reg)
238{
239	uint16_t val;
240
241	val = bus_space_read_2(sc->sc_aca_iot, sc->sc_aca_ioh, reg * 0x4000);
242#ifdef ACAFH_DEBUG
243	aprint_normal_dev(sc->sc_dev, "read out reg %x from %lx (%x), val %x\n",
244	    reg, sc->sc_aca_ioh + reg, (bus_addr_t) kvtop((void*)
245	    ((unsigned) sc->sc_aca_ioh + reg * 0x4000)), val);
246#endif /* ACAFH_DEBUG */
247
248	return (val & ACAFH_MSB_MASK) >> ACAFH_MSB_SHIFT;
249}
250
251static int
252acafh_print(void *aux, const char *w)
253{
254	if (w == NULL)
255		return 0;
256
257	return 0;
258}
259
260