wdc_mb.c revision 1.23
1/*	$NetBSD: wdc_mb.c,v 1.23 2004/08/14 15:08:04 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Onno van der Linden.
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: wdc_mb.c,v 1.23 2004/08/14 15:08:04 thorpej Exp $");
41
42#include <sys/types.h>
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/device.h>
47
48#include <machine/bswap.h>
49#include <machine/cpu.h>
50#include <machine/bus.h>
51#include <machine/iomap.h>
52#include <machine/mfp.h>
53#include <machine/dma.h>
54
55#include <dev/ata/atavar.h>
56#include <dev/ic/wdcvar.h>
57
58#include <m68k/asm_single.h>
59
60#include <atari/dev/ym2149reg.h>
61#include <atari/atari/device.h>
62
63/*
64 * XXX This code currently doesn't even try to allow 32-bit data port use.
65 */
66static int	claim_hw __P((void *, int));
67static void	free_hw __P((void *));
68static void	read_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
69				bus_size_t, u_int16_t *, bus_size_t));
70static void	write_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
71				bus_size_t, const u_int16_t *, bus_size_t));
72
73struct wdc_mb_softc {
74	struct wdc_softc sc_wdcdev;
75	struct	ata_channel *sc_chanlist[1];
76	struct  ata_channel sc_channel;
77	struct	ata_queue sc_chqueue;
78	struct	wdc_regs sc_wdc_regs;
79	void	*sc_ih;
80};
81
82int	wdc_mb_probe	__P((struct device *, struct cfdata *, void *));
83void	wdc_mb_attach	__P((struct device *, struct device *, void *));
84
85CFATTACH_DECL(wdc_mb, sizeof(struct wdc_mb_softc),
86    wdc_mb_probe, wdc_mb_attach, NULL, NULL);
87
88int
89wdc_mb_probe(parent, cfp, aux)
90	struct device *parent;
91	struct cfdata *cfp;
92	void *aux;
93{
94	static int	wdc_matched = 0;
95	struct ata_channel ch;
96	struct wdc_softc wdc;
97	struct wdc_regs wdr;
98	int	result = 0;
99	u_char	sv_ierb;
100
101	if ((machineid & ATARI_TT) || strcmp("wdc", aux) || wdc_matched)
102		return 0;
103	if (!atari_realconfig)
104		return 0;
105
106	memset(&wdc, 0, sizeof(wdc));
107	memset(&ch, 0, sizeof(ch));
108	ch.ch_wdc = &wdc;
109	wdc.regs = &wdr;
110
111	wdr.cmd_iot = wdr.ctl_iot = mb_alloc_bus_space_tag();
112	if (wdr.cmd_iot == NULL)
113		return 0;
114	wdr.cmd_iot->stride = 2;
115	wdr.cmd_iot->wo_1   = 1;
116
117	if (bus_space_map(wdr.cmd_iot, 0xfff00000, 0x40, 0, &wdr.cmd_baseioh))
118		return 0;
119	if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, 0x38, 1,
120	    &wdr.ctl_ioh))
121		return 0;
122
123	/*
124	 * Make sure IDE interrupts are disabled during probing.
125	 */
126	sv_ierb = MFP->mf_ierb;
127	MFP->mf_ierb &= ~IB_DINT;
128
129	/*
130	 * Make sure that IDE is turned on on the Falcon.
131	 */
132	if (machineid & ATARI_FALCON)
133		ym2149_ser2(0);
134
135	result = wdcprobe(&ch);
136
137	MFP->mf_ierb = sv_ierb;
138
139	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, 0x40);
140	mb_free_bus_space_tag(wdr.cmd_iot);
141
142	if (result)
143		wdc_matched = 1;
144	return (result);
145}
146
147void
148wdc_mb_attach(parent, self, aux)
149	struct device *parent, *self;
150	void *aux;
151{
152	struct wdc_mb_softc *sc = (void *)self;
153	struct wdc_regs *wdr;
154
155	printf("\n");
156
157	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
158	wdr->cmd_iot = wdr->ctl_iot =
159	    mb_alloc_bus_space_tag();
160	wdr->cmd_iot->stride = 2;
161	wdr->cmd_iot->wo_1   = 1;
162	wdr->cmd_iot->abs_rms_2 = read_multi_2_swap;
163	wdr->cmd_iot->abs_wms_2 = write_multi_2_swap;
164	if (bus_space_map(wdr->cmd_iot, 0xfff00000, 0x40, 0,
165			  &wdr->cmd_baseioh)) {
166		printf("%s: couldn't map registers\n",
167		    sc->sc_wdcdev.sc_dev.dv_xname);
168		return;
169	}
170	if (bus_space_subregion(wdr->cmd_iot,
171	    wdr->cmd_baseioh, 0x38, 1, &wdr->ctl_ioh))
172		return;
173
174	/*
175	 * Play a nasty trick here. Normally we only manipulate the
176	 * interrupt *mask*. However to defeat wd_get_parms(), we
177	 * disable the interrupts here using the *enable* register.
178	 */
179	MFP->mf_ierb &= ~IB_DINT;
180
181	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 |
182	    WDC_CAPABILITY_ATA_NOSTREAM;
183	sc->sc_wdcdev.PIO_cap = 0;
184	sc->sc_wdcdev.claim_hw = &claim_hw;
185	sc->sc_wdcdev.free_hw  = &free_hw;
186	sc->sc_chanlist[0] = &sc->sc_channel;
187	sc->sc_wdcdev.channels = sc->sc_chanlist;
188	sc->sc_wdcdev.nchannels = 1;
189	sc->sc_channel.ch_channel = 0;
190	sc->sc_channel.ch_wdc = &sc->sc_wdcdev;
191	sc->sc_channel.ch_queue = &sc->sc_chqueue;
192
193	/*
194	 * Setup & enable disk related interrupts.
195	 */
196	MFP->mf_ierb |= IB_DINT;
197	MFP->mf_iprb  = (u_int8_t)~IB_DINT;
198	MFP->mf_imrb |= IB_DINT;
199
200	wdcattach(&sc->sc_channel);
201}
202
203/*
204 * Hardware locking
205 */
206static int	wd_lock;
207
208static int
209claim_hw(softc, maysleep)
210void *softc;
211int  maysleep;
212{
213	if (wd_lock != DMA_LOCK_GRANT) {
214		if (wd_lock == DMA_LOCK_REQ) {
215			/*
216			 * ST_DMA access is being claimed.
217			 */
218			return 0;
219		}
220		if (!st_dmagrab((dma_farg)wdcintr,
221		    (dma_farg)(maysleep ? NULL : wdcrestart), softc,
222		    &wd_lock, 1))
223			return 0;
224	}
225	return 1;
226}
227
228static void
229free_hw(softc)
230void *softc;
231{
232	/*
233	 * Flush pending interrupts before giving-up lock
234	 */
235	MFP->mf_iprb = (u_int8_t)~IB_DINT;
236
237	/*
238	 * Only free the lock on a Falcon. On the Hades, keep it.
239	 */
240/*	if (machineid & ATARI_FALCON) */
241		st_dmafree(softc, &wd_lock);
242}
243
244/*
245 * XXX
246 * This piece of uglyness is caused by the fact that the byte lanes of
247 * the data-register are swapped on the atari. This works OK for an IDE
248 * disk, but turns into a nightmare when used on atapi devices.
249 */
250#define calc_addr(base, off, stride, wm)	\
251	((u_long)(base) + ((off) << (stride)) + (wm))
252
253static void
254read_multi_2_swap(t, h, o, a, c)
255	bus_space_tag_t		t;
256	bus_space_handle_t	h;
257	bus_size_t		o, c;
258	u_int16_t		*a;
259{
260	u_int16_t	*ba;
261
262	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
263	for (; c; a++, c--)
264		*a = bswap16(*ba);
265}
266
267static void
268write_multi_2_swap(t, h, o, a, c)
269	bus_space_tag_t		t;
270	bus_space_handle_t	h;
271	bus_size_t		o, c;
272	const u_int16_t		*a;
273{
274	u_int16_t	*ba;
275
276	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
277	for (; c; a++, c--)
278		*ba = bswap16(*a);
279}
280