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