1/*	$NetBSD: wdc_obio.c,v 1.32 2022/08/20 20:02:22 tsutsui Exp $ */
2
3/*
4 * Copyright (c) 2002 Takeshi Shibagaki  All rights reserved.
5 *
6 * mac68k OBIO-IDE attachment created by Takeshi Shibagaki
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Charles M. Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.32 2022/08/20 20:02:22 tsutsui Exp $");
36
37#include <sys/types.h>
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/device.h>
41#include <sys/kernel.h>
42#include <sys/kmem.h>
43#include <sys/callout.h>
44
45#include <machine/bus.h>
46#include <machine/intr.h>
47#include <machine/cpu.h>
48#include <machine/viareg.h>
49
50#include <mac68k/obio/obiovar.h>
51
52#include <dev/ata/atavar.h>
53#include <dev/ic/wdcvar.h>
54
55#define	WDC_OBIO_REG_NPORTS	0x40
56#define	WDC_OBIO_AUXREG_OFFSET	0x38
57#define	WDC_OBIO_AUXREG_NPORTS	1
58#define	WDC_OBIO_ISR_OFFSET	0x101
59#define	WDC_OBIO_ISR_NPORTS	1
60
61static u_long	IDEBase = 0x50f1a000;
62
63/*
64 * XXX This code currently doesn't even try to allow 32-bit data port use.
65 */
66
67struct wdc_obio_softc {
68	struct  wdc_softc sc_wdcdev;
69	struct  ata_channel *sc_chanlist[1];
70	struct  ata_channel sc_channel;
71	struct	wdc_regs sc_wdc_regs;
72	void    *sc_ih;
73};
74
75int	wdc_obio_match(device_t, cfdata_t, void *);
76void	wdc_obio_attach(device_t, device_t, void *);
77void	wdc_obio_intr(void *);
78
79CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
80    wdc_obio_match, wdc_obio_attach, NULL, NULL);
81
82static bus_space_tag_t		wdc_obio_isr_tag;
83static bus_space_handle_t	wdc_obio_isr_hdl;
84static struct ata_channel	*ch_sc;
85
86int
87wdc_obio_match(device_t parent, cfdata_t match, void *aux)
88{
89	struct obio_attach_args *oa = (struct obio_attach_args *) aux;
90	struct wdc_regs *wdr;
91	int i, result = 0;
92
93	wdr = kmem_alloc(sizeof(*wdr), KM_SLEEP);
94
95	switch (current_mac_model->machineid) {
96	case MACH_MACPB150:
97	case MACH_MACPB190:
98	case MACH_MACPB190CS:
99	case MACH_MACP580:
100	case MACH_MACQ630:
101		wdr->cmd_iot = wdr->ctl_iot = oa->oa_tag;
102
103		if (bus_space_map(wdr->cmd_iot, IDEBase, WDC_OBIO_REG_NPORTS,
104		    0, &wdr->cmd_baseioh))
105			goto out;
106
107		mac68k_bus_space_handle_swapped(wdr->cmd_iot,
108		    &wdr->cmd_baseioh);
109
110		for (i = 0; i < WDC_NREG; i++) {
111			if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
112			    i * 4, 4, &wdr->cmd_iohs[i]) != 0)
113				goto unmap;
114		}
115		wdc_init_shadow_regs(wdr);
116
117		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
118		    WDC_OBIO_AUXREG_OFFSET, WDC_OBIO_AUXREG_NPORTS,
119		    &wdr->ctl_ioh))
120			goto unmap;
121
122		result = wdcprobe(wdr);
123
124 unmap:
125		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
126		    WDC_OBIO_REG_NPORTS);
127	}
128 out:
129	kmem_free(wdr, sizeof(*wdr));
130	return result;
131}
132
133void
134wdc_obio_attach(device_t parent, device_t self, void *aux)
135{
136	struct wdc_obio_softc *sc = device_private(self);
137	struct wdc_regs *wdr;
138	struct obio_attach_args *oa = aux;
139	struct ata_channel *chp = &sc->sc_channel;
140	int i;
141
142	sc->sc_wdcdev.sc_atac.atac_dev = self;
143	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
144
145	oa->oa_addr = IDEBase;
146	wdr->cmd_iot = wdr->ctl_iot = oa->oa_tag;
147
148	if (bus_space_map(wdr->cmd_iot, oa->oa_addr, WDC_OBIO_REG_NPORTS, 0,
149	    &wdr->cmd_baseioh)) {
150		aprint_error(": couldn't map registers\n");
151		return;
152	}
153
154	mac68k_bus_space_handle_swapped(wdr->cmd_iot, &wdr->cmd_baseioh);
155
156	for (i = 0; i < WDC_NREG; i++) {
157		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
158		    i * 4, 4, &wdr->cmd_iohs[i]) != 0) {
159			aprint_error(
160			    ": unable to subregion control register\n");
161			goto err;
162		}
163	}
164
165	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
166	    WDC_OBIO_AUXREG_OFFSET, WDC_OBIO_AUXREG_NPORTS, &wdr->ctl_ioh)) {
167		aprint_error(": unable to subregion aux register\n");
168		goto err;
169	}
170
171	wdc_obio_isr_tag = oa->oa_tag;
172
173	if (bus_space_map(wdc_obio_isr_tag, oa->oa_addr + WDC_OBIO_ISR_OFFSET,
174	    WDC_OBIO_ISR_NPORTS, 0, &wdc_obio_isr_hdl)) {
175		aprint_error(": couldn't map intr status register\n");
176		goto err;
177	}
178
179	switch (current_mac_model->machineid) {
180	case MACH_MACP580:
181	case MACH_MACQ630:
182		/*
183		 * Quadra/Performa IDE generates pseudo Nubus intr at slot F
184		 */
185		aprint_normal(" (Quadra/Performa series IDE interface)");
186
187		add_nubus_intr(0xf, wdc_obio_intr, sc);
188
189		break;
190	case MACH_MACPB150:
191	case MACH_MACPB190:
192	case MACH_MACPB190CS:
193		/*
194		 * PowerBook IDE generates pseudo NuBus intr at slot C
195		 */
196		aprint_normal(" (PowerBook series IDE interface)");
197
198		add_nubus_intr(0xc, wdc_obio_intr, sc);
199
200		break;
201	}
202
203	ch_sc = chp;
204	if ((device_cfdata(self)->cf_flags & ATAC_CAP_NOIRQ) != 0)
205		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
206	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
207	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
208	sc->sc_chanlist[0] = chp;
209	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
210	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
211	sc->sc_wdcdev.wdc_maxdrives = 2;
212	chp->ch_channel = 0;
213	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
214
215	wdc_init_shadow_regs(wdr);
216
217	aprint_normal("\n");
218
219	wdcattach(chp);
220	return;
221
222 err:
223	bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_REG_NPORTS);
224}
225
226void
227wdc_obio_intr(void *arg)
228{
229	uint8_t status;
230
231	status = bus_space_read_1(wdc_obio_isr_tag, wdc_obio_isr_hdl, 0);
232	if ((status & 0x20) != 0) {
233		wdcintr(ch_sc);
234		bus_space_write_1(wdc_obio_isr_tag, wdc_obio_isr_hdl, 0,
235		    status & ~0x20);
236	}
237}
238