xref: /netbsd-src/sys/arch/amiga/dev/wdc_buddha.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: wdc_buddha.c,v 1.2 2007/08/21 06:51:09 is Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael L. Hitch and Ignatios Souvatzis.
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/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 
45 #include <machine/cpu.h>
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 #include <machine/bswap.h>
49 
50 #include <amiga/amiga/cia.h>
51 #include <amiga/amiga/custom.h>
52 #include <amiga/amiga/device.h>
53 #include <amiga/dev/zbusvar.h>
54 
55 #include <dev/ata/atavar.h>
56 #include <dev/ic/wdcvar.h>
57 
58 #define BUDDHA_MAX_CHANNELS 3
59 
60 struct wdc_buddha_softc {
61 	struct wdc_softc sc_wdcdev;
62 	struct ata_channel *wdc_chanarray[BUDDHA_MAX_CHANNELS];
63 	struct ata_channel channels[BUDDHA_MAX_CHANNELS];
64 	struct bus_space_tag sc_iot;
65 	struct isr sc_isr;
66 	volatile char *ba;
67 };
68 
69 int	wdc_buddha_probe(struct device *, struct cfdata *, void *);
70 void	wdc_buddha_attach(struct device *, struct device *, void *);
71 int	wdc_buddha_intr(void *);
72 
73 CFATTACH_DECL(wdc_buddha, sizeof(struct wdc_buddha_softc),
74     wdc_buddha_probe, wdc_buddha_attach, NULL, NULL);
75 
76 int
77 wdc_buddha_probe(struct device *parent, struct cfdata *cfp, void *aux)
78 {
79 	struct zbus_args *zap;
80 
81 	zap = aux;
82 
83 	if (zap->manid != 4626)
84 		return 0;
85 
86 	if ((zap->prodid != 0) &&	/* Buddha */
87 	    (zap->prodid != 42))	/* Catweasel */
88 		return 0;
89 
90 	return 1;
91 }
92 
93 void
94 wdc_buddha_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct wdc_buddha_softc *sc;
97 	struct zbus_args *zap;
98 	int nchannels;
99 	int ch;
100 
101 	sc = (void *)self;
102 	zap = aux;
103 
104 	sc->ba = zap->va;
105 
106 	sc->sc_iot.base = (bus_addr_t)sc->ba;
107 	sc->sc_iot.absm = &amiga_bus_stride_4swap;
108 
109 	nchannels = 2;
110 	if (zap->prodid == 42) {
111 		printf(": Catweasel Z2\n");
112 		nchannels = 3;
113 	} else if (zap->serno == 0)
114 		printf(": Buddha\n");
115 	else
116 		printf(": Buddha Flash\n");
117 
118 	/* XXX pio mode setting not implemented yet. */
119 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
120 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
121 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
122 	sc->sc_wdcdev.sc_atac.atac_nchannels = nchannels;
123 
124 	wdc_allocate_regs(&sc->sc_wdcdev);
125 
126 	for (ch = 0; ch < nchannels; ch++) {
127 		struct ata_channel *cp;
128 		struct wdc_regs *wdr;
129 		int i;
130 
131 		cp = &sc->channels[ch];
132 		sc->wdc_chanarray[ch] = cp;
133 
134 		cp->ch_channel = ch;
135 		cp->ch_atac = &sc->sc_wdcdev.sc_atac;
136 		cp->ch_queue =
137 		    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
138 		if (cp->ch_queue == NULL) {
139 			printf("%s: can't allocate memory for command queue\n",
140 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
141 			return;
142 		}
143 		cp->ch_ndrive = 2;
144 
145 		/*
146 		 * XXX According to the Buddha docs, we should use a method
147 		 * array that adds 0x40 to the address for byte accesses, to
148 		 * get the slow timing for command accesses, and the 0x00
149 		 * offset for the word (fast) accesses. This will be
150 		 * reconsidered when implementing setting the timing.
151 		 *
152 		 * XXX We also could consider to abuse the 32bit capability, or
153 		 * 32bit accesses to the words (which will read in two words)
154 		 * for better performance.
155 		 *		-is
156 		 */
157 		wdr = CHAN_TO_WDC_REGS(cp);
158 
159 		wdr->cmd_iot = &sc->sc_iot;
160 		if (bus_space_map(wdr->cmd_iot, 0x210+ch*0x80, 8, 0,
161 		    &wdr->cmd_baseioh)) {
162 			printf("%s: couldn't map cmd registers\n",
163 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
164 			return;
165 		}
166 
167 		wdr->ctl_iot = &sc->sc_iot;
168 		if (bus_space_map(wdr->ctl_iot, 0x250+ch*0x80, 2, 0,
169 		    &wdr->ctl_ioh)) {
170 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 8);
171 			printf("%s: couldn't map ctl registers\n",
172 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
173 			return;
174 		}
175 
176 		for (i = 0; i < WDC_NREG; i++) {
177 			if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
178 			    i, i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
179 				printf("%s: couldn't subregion cmd regs\n",
180 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
181 				return;
182 			}
183 		}
184 
185 		wdc_init_shadow_regs(cp);
186 		wdcattach(cp);
187 	}
188 
189 	sc->sc_isr.isr_intr = wdc_buddha_intr;
190 	sc->sc_isr.isr_arg = sc;
191 	sc->sc_isr.isr_ipl = 2;
192 	add_isr (&sc->sc_isr);
193 	sc->ba[0xfc0] = 0;	/* enable interrupts */
194 }
195 
196 int
197 wdc_buddha_intr(void *arg)
198 {
199 	struct wdc_buddha_softc *sc = (struct wdc_buddha_softc *)arg;
200 	int nchannels, i, ret;
201 	volatile char *p;
202 
203 	ret = 0;
204 	nchannels = sc->sc_wdcdev.sc_atac.atac_nchannels;
205 	p = sc->ba;
206 
207 	for (i=0; i<nchannels; i++) {
208 		if (p[0xf00 + 0x40*i] & 0x80) {
209 			wdcintr(&sc->channels[i]);
210 			ret = 1;
211 		}
212 	}
213 
214 	return ret;
215 }
216