1/*	$NetBSD: wdc_acafh.c,v 1.7 2023/12/20 00:40:42 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2000, 2003, 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 * This code is derived from software contributed to The NetBSD Foundation
11 * by Michael L. Hitch.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * ACA500 CF (IDE) controller driver.
37 *
38 * The hardware emulates original A600/A1200 Gayle interface. However, it has
39 * two channels, second channel placed instead of ctl registers (so software
40 * reset of the bus is not possible, duh). There are no slave devices.
41 */
42
43#include <sys/cdefs.h>
44__KERNEL_RCSID(0, "$NetBSD: wdc_acafh.c,v 1.7 2023/12/20 00:40:42 thorpej Exp $");
45
46#include <sys/types.h>
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/device.h>
50#include <sys/bus.h>
51
52#include <machine/cpu.h>
53#include <machine/intr.h>
54#include <sys/bswap.h>
55
56#include <amiga/amiga/cia.h>
57#include <amiga/amiga/custom.h>
58#include <amiga/amiga/device.h>
59#include <amiga/amiga/gayle.h>
60#include <amiga/dev/zbusvar.h>
61#include <amiga/dev/acafhvar.h>
62#include <amiga/dev/acafhreg.h>
63
64#include <dev/ata/atavar.h>
65#include <dev/ic/wdcvar.h>
66
67#define WDC_ACAFH_SLOTS 2
68
69struct wdc_acafh_slot {
70	struct ata_channel	channel;
71	struct wdc_regs		wdr;
72};
73
74struct wdc_acafh_softc {
75	struct wdc_softc	sc_wdcdev;
76	struct ata_channel	*sc_chanlist[WDC_ACAFH_SLOTS];
77	struct wdc_acafh_slot	sc_slots[WDC_ACAFH_SLOTS];
78
79	struct isr		sc_isr;
80
81	struct bus_space_tag	cmd_iot;
82
83	struct acafh_softc	*aca_sc;
84};
85
86int		wdc_acafh_match(device_t, cfdata_t, void *);
87void		wdc_acafh_attach(device_t, device_t, void *);
88int		wdc_acafh_intr(void *);
89static void	wdc_acafh_attach_channel(struct wdc_acafh_softc *, int);
90static void	wdc_acafh_map_channel(struct wdc_acafh_softc *, int);
91
92CFATTACH_DECL_NEW(wdc_acafh, sizeof(struct wdc_acafh_softc),
93    wdc_acafh_match, wdc_acafh_attach, NULL, NULL);
94
95int
96wdc_acafh_match(device_t parent, cfdata_t cfp, void *aux)
97{
98	struct acafhbus_attach_args *aap = aux;
99
100	if (strcmp(aap->aaa_name, "wdc_acafh") != 0)
101		return(0);
102	return 1;
103}
104
105void
106wdc_acafh_attach(device_t parent, device_t self, void *aux)
107{
108	struct wdc_acafh_softc *sc = device_private(self);
109	int i;
110
111	aprint_normal(": ACA500 CompactFlash interface\n");
112	sc->aca_sc = device_private(parent);
113
114	gayle_init();
115
116	/* XXX: take addr from attach args? */
117	sc->cmd_iot.base = (u_long) ztwomap(GAYLE_IDE_BASE + 2);
118	sc->cmd_iot.absm = &amiga_bus_stride_4swap;
119
120	sc->sc_wdcdev.sc_atac.atac_dev = self;
121	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
122	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
123	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
124	sc->sc_wdcdev.sc_atac.atac_nchannels = WDC_ACAFH_SLOTS;
125	sc->sc_wdcdev.wdc_maxdrives = 2;
126	sc->sc_wdcdev.cap = WDC_CAPABILITY_NO_AUXCTL;
127
128	wdc_allocate_regs(&sc->sc_wdcdev);
129	for (i = 0; i < WDC_ACAFH_SLOTS; i++) {
130		wdc_acafh_attach_channel(sc, i);
131	}
132
133	sc->sc_isr.isr_intr = wdc_acafh_intr;
134	sc->sc_isr.isr_arg = sc;
135	sc->sc_isr.isr_ipl = 2;
136	add_isr (&sc->sc_isr);
137
138	gayle_intr_enable_set(GAYLE_INT_IDE);
139
140}
141
142void
143wdc_acafh_attach_channel(struct wdc_acafh_softc *sc, int chnum)
144{
145#ifdef WDC_ACAFH_DEBUG
146	device_t self;
147
148	self = sc->sc_wdcdev.sc_atac.atac_dev;
149#endif /* WDC_ACAFH_DEBUG */
150
151	sc->sc_chanlist[chnum] = &sc->sc_slots[chnum].channel;
152	memset(&sc->sc_slots[chnum],0,sizeof(struct wdc_acafh_slot));
153	sc->sc_slots[chnum].channel.ch_channel = chnum;
154	sc->sc_slots[chnum].channel.ch_atac = &sc->sc_wdcdev.sc_atac;
155
156	wdc_acafh_map_channel(sc, chnum);
157
158	wdc_init_shadow_regs(CHAN_TO_WDC_REGS(&sc->sc_slots[chnum].channel));
159	wdcattach(&sc->sc_slots[chnum].channel);
160
161#ifdef WDC_ACAFH_DEBUG
162	aprint_normal_dev(self, "done init for channel %d\n", chnum);
163#endif /* WDC_ACAFH_DEBUG */
164}
165
166void
167wdc_acafh_map_channel(struct wdc_acafh_softc *sc, int chnum)
168{
169	struct wdc_regs *wdr;
170	bus_addr_t off;
171	device_t self;
172	int i;
173
174	self = sc->sc_wdcdev.sc_atac.atac_dev;
175
176	wdr = CHAN_TO_WDC_REGS(&sc->sc_slots[chnum].channel);
177	wdr->cmd_iot = &sc->cmd_iot;
178
179	if (chnum == 0)
180		off = 0;
181	else
182		off = 0x400;
183
184	if (bus_space_map(wdr->cmd_iot, off, 0x40, 0,
185			  &wdr->cmd_baseioh)) {
186		aprint_error_dev(self, "couldn't map regs\n");
187		return;
188	}
189
190	for (i = 0; i < WDC_NREG; i++) {
191		if (bus_space_subregion(wdr->cmd_iot,
192		    wdr->cmd_baseioh, i, i == 0 ? 4 : 1,
193		    &wdr->cmd_iohs[i]) != 0) {
194
195			bus_space_unmap(wdr->cmd_iot,
196			    wdr->cmd_baseioh, 0x40);
197			aprint_error_dev(self, "couldn't map regs\n");
198			return;
199		}
200	}
201
202}
203
204int
205wdc_acafh_intr(void *arg)
206{
207	struct wdc_acafh_softc *sc;
208	uint8_t intreq;
209	int ret;
210
211	sc = (struct wdc_acafh_softc *) arg;
212	intreq = gayle_intr_status();
213	ret = 0;
214
215	if (intreq & GAYLE_INT_IDE) {
216		if (acafh_cf_intr_status(sc->aca_sc, 1) == 1) {
217			ret = wdcintr(&sc->sc_slots[1].channel);
218		}
219		if (acafh_cf_intr_status(sc->aca_sc, 0) == 1) {
220			ret = wdcintr(&sc->sc_slots[0].channel);
221		}
222		gayle_intr_ack(0x7C | (intreq & GAYLE_INT_IDEACK));
223	}
224
225	return ret;
226}
227
228