pciide.c revision 1.5 1 /* $NetBSD: pciide.c,v 1.5 1998/03/06 19:13:19 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI IDE controller driver.
35 *
36 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
37 * sys/dev/pci/ppb.c, revision 1.16).
38 *
39 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
40 * "Programming Interface for Bus Master IDE Controller, Revision 1.0
41 * 5/16/94" from the PCI SIG.
42 *
43 * XXX Does not yet support DMA (but does map the Bus Master DMA regs).
44 *
45 * XXX Does not support serializing the two channels for broken (at least
46 * XXX according to linux and freebsd) controllers, e.g. CMD PCI0640.
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/device.h>
52
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pciidereg.h>
56 #include <dev/pci/pciidevar.h>
57
58 struct pciide_softc {
59 struct device sc_dev;
60
61 void *sc_pci_ih; /* PCI interrupt handle */
62 int sc_dma_ok; /* bus-master DMA info */
63 bus_space_tag_t sc_dma_iot;
64 bus_space_handle_t sc_dma_ioh;
65
66 struct pciide_channel { /* per-channel data */
67 /* internal bookkeeping */
68 int hw_ok; /* hardware mapped & OK? */
69 struct device *dev; /* 'wdc' dev attached */
70 int compat; /* is it compat? */
71 void *ih; /* compat or pci handle */
72
73 /* used by wdc attachment (read-only after init) */
74 bus_space_tag_t cmd_iot, ctl_iot;
75 bus_space_handle_t cmd_ioh, ctl_ioh;
76
77 /* filled in by wdc attachment (written by wdc attach) */
78 int (*ihand) __P((void *));
79 void *ihandarg;
80 } sc_channels[PCIIDE_NUM_CHANNELS];
81 };
82
83 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
84
85 #ifdef __BROKEN_INDIRECT_CONFIG
86 int pciide_match __P((struct device *, void *, void *));
87 #else
88 int pciide_match __P((struct device *, struct cfdata *, void *));
89 #endif
90 void pciide_attach __P((struct device *, struct device *, void *));
91
92 struct cfattach pciide_ca = {
93 sizeof(struct pciide_softc), pciide_match, pciide_attach
94 };
95
96 int pciide_map_channel_compat __P((struct pciide_softc *,
97 struct pci_attach_args *, int));
98 int pciide_compat_channel_probe __P((struct pciide_softc *,
99 struct pci_attach_args *, int));
100 int pciide_map_channel_native __P((struct pciide_softc *,
101 struct pci_attach_args *, int));
102 int pciide_print __P((void *, const char *pnp));
103 int pciide_compat_intr __P((void *));
104 int pciide_pci_intr __P((void *));
105
106 int
107 pciide_match(parent, match, aux)
108 struct device *parent;
109 #ifdef __BROKEN_INDIRECT_CONFIG
110 void *match;
111 #else
112 struct cfdata *match;
113 #endif
114 void *aux;
115 {
116 struct pci_attach_args *pa = aux;
117
118 /*
119 * Check the ID register to see that it's a PCI IDE controller.
120 * If it is, we assume that we can deal with it; it _should_
121 * work in a standardized way...
122 */
123 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
124 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
125 return (1);
126 }
127
128 return (0);
129 }
130
131 void
132 pciide_attach(parent, self, aux)
133 struct device *parent, *self;
134 void *aux;
135 {
136 struct pci_attach_args *pa = aux;
137 pci_chipset_tag_t pc = pa->pa_pc;
138 struct pciide_softc *sc = (struct pciide_softc *)self;
139 struct pciide_attach_args aa;
140 struct pciide_channel *cp;
141 pcireg_t class, interface, csr;
142 pci_intr_handle_t intrhandle;
143 const char *intrstr;
144 char devinfo[256];
145 int i;
146
147 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
148 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
149
150 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
151 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
152 printf("%s: device disabled (at %s)\n", sc->sc_dev.dv_xname,
153 (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
154 return;
155 }
156
157 class = pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG);
158 interface = PCI_INTERFACE(class);
159
160 /*
161 * Set up PCI interrupt.
162 *
163 * If mapping fails, that's (probably) because there's no pin
164 * set to intr, which is (probably) because it's a compat-only
165 * device (or hard-wired in compatibility-only mode). Native-PCI
166 * channels will complain later if the interrupt was needed.
167 *
168 * If establishment fails, that's (probably) some other problem.
169 */
170 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
171 pa->pa_intrline, &intrhandle) == 0) {
172 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
173 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
174 IPL_BIO, pciide_pci_intr, sc);
175
176 if (sc->sc_pci_ih != NULL) {
177 printf("%s: using %s for native-PCI interrupt\n",
178 sc->sc_dev.dv_xname,
179 intrstr ? intrstr : "unknown interrupt");
180 } else {
181 printf("%s: couldn't establish native-PCI interrupt",
182 sc->sc_dev.dv_xname);
183 if (intrstr != NULL)
184 printf(" at %s", intrstr);
185 printf("\n");
186 }
187 }
188
189 /*
190 * Map DMA registers, if DMA is supported.
191 *
192 * Note that sc_dma_ok is the right variable to test to see if
193 * DMA can * be done. If the interface doesn't support DMA,
194 * sc_dma_ok * will never be non-zero. If the DMA regs couldn't
195 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
196 * non-zero if the interface supports DMA and the registers
197 * could be mapped.
198 *
199 * XXX Note that despite the fact that the Bus Master IDE specs
200 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
201 * XXX space," some controllers (at least the United
202 * XXX Microelectronics UM8886BF) place it in memory space.
203 * XXX eventually, we should probably read the register and check
204 * XXX which type it is. Either that or 'quirk' certain devices.
205 */
206 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
207 sc->sc_dma_ok = (pci_mapreg_map(pa,
208 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
209 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
210 printf("%s: bus-master DMA support present, but unused (%s)\n",
211 sc->sc_dev.dv_xname,
212 sc->sc_dma_ok ? "no driver support" :
213 "couldn't map registers");
214 }
215
216 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
217 cp = &sc->sc_channels[i];
218
219 printf("%s: %s channel %s to %s mode\n",
220 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(i),
221 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
222 "configured" : "wired",
223 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
224 "compatibility");
225
226 if (interface & PCIIDE_INTERFACE_PCI(i))
227 cp->hw_ok = pciide_map_channel_native(sc, pa, i);
228 else
229 cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
230 if (!cp->hw_ok)
231 continue;
232
233 aa.channel = i;
234 aa.cmd_iot = cp->cmd_iot;
235 aa.cmd_ioh = cp->cmd_ioh;
236 aa.ctl_iot = cp->ctl_iot;
237 aa.ctl_ioh = cp->ctl_ioh;
238 aa.ihandp = &cp->ihand;
239 aa.ihandargp = &cp->ihandarg;
240 cp->dev = config_found(self, &aa, pciide_print);
241
242 /*
243 * Note that if the 'wdc' device isn't configured,
244 * the controller's resources are still marked as
245 * being in use. This is a feature.
246 */
247 }
248 }
249
250 int
251 pciide_map_channel_compat(sc, pa, chan)
252 struct pciide_softc *sc;
253 struct pci_attach_args *pa;
254 int chan;
255 {
256 struct pciide_channel *cp = &sc->sc_channels[chan];
257 int rv = 1;
258
259 cp->compat = 1;
260
261 cp->cmd_iot = pa->pa_iot;
262 if (bus_space_map(cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
263 PCIIDE_COMPAT_CMD_SIZE, 0, &cp->cmd_ioh) != 0) {
264 printf("%s: couldn't map %s channel cmd regs\n",
265 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
266 rv = 0;
267 }
268
269 cp->ctl_iot = pa->pa_iot;
270 if (bus_space_map(cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
271 PCIIDE_COMPAT_CTL_SIZE, 0, &cp->ctl_ioh) != 0) {
272 printf("%s: couldn't map %s channel ctl regs\n",
273 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
274 rv = 0;
275 }
276
277 /*
278 * If we weren't able to map the device successfully,
279 * we just give up now. Something else has already
280 * occupied those ports, indicating that the device has
281 * (probably) been completely disabled (by some nonstandard
282 * mechanism).
283 *
284 * XXX If we successfully map some ports, but not others,
285 * XXX it might make sense to unmap the ones that we mapped.
286 */
287 if (rv == 0)
288 goto out;
289
290 /*
291 * If we were able to map the device successfully, try to
292 * make sure that there's a wdc there and that it's
293 * attributable to us.
294 *
295 * If there's not, then we assume that there's the device
296 * has been disabled and that other devices are free to use
297 * its ports.
298 */
299 if (pciide_compat_channel_probe(sc, pa, chan) == 0) {
300 rv = 0;
301
302 bus_space_unmap(cp->cmd_iot, cp->cmd_ioh,
303 PCIIDE_COMPAT_CMD_SIZE);
304 bus_space_unmap(cp->ctl_iot, cp->ctl_ioh,
305 PCIIDE_COMPAT_CTL_SIZE);
306
307 goto out;
308 }
309
310 /*
311 * If we're here, we were able to map the device successfully
312 * and it really looks like there's a controller there.
313 *
314 * Unless those conditions are true, we don't map the
315 * compatibility interrupt. The spec indicates that if a
316 * channel is configured for compatibility mode and the PCI
317 * device's I/O space is enabled, the channel will be enabled.
318 * Hoewver, some devices seem to be able to disable invididual
319 * compatibility channels (via non-standard mechanisms). If
320 * the channel is disabled, the interrupt line can (probably)
321 * be used by other devices (and may be assigned to other
322 * devices by the BIOS). If we mapped the interrupt we might
323 * conflict with another interrupt assignment.
324 */
325 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_dev, pa,
326 chan, pciide_compat_intr, cp);
327 if (cp->ih == NULL) {
328 printf("%s: no compatibility interrupt for use by %s channel\n",
329 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
330 rv = 0;
331 }
332
333 out:
334 return (rv);
335 }
336
337 int
338 pciide_compat_channel_probe(sc, pa, chan)
339 struct pciide_softc *sc;
340 struct pci_attach_args *pa;
341 {
342
343 /*
344 * XXX for now we claim that the devices are always present.
345 */
346 return (1);
347 }
348
349 int
350 pciide_map_channel_native(sc, pa, chan)
351 struct pciide_softc *sc;
352 struct pci_attach_args *pa;
353 int chan;
354 {
355 struct pciide_channel *cp = &sc->sc_channels[chan];
356 int rv = 1;
357
358 cp->compat = 0;
359
360 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
361 0, &cp->cmd_iot, &cp->cmd_ioh, NULL, NULL) != 0) {
362 printf("%s: couldn't map %s channel cmd regs\n",
363 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
364 rv = 0;
365 }
366
367 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
368 0, &cp->ctl_iot, &cp->ctl_ioh, NULL, NULL) != 0) {
369 printf("%s: couldn't map %s channel ctl regs\n",
370 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
371 rv = 0;
372 }
373
374 if ((cp->ih = sc->sc_pci_ih) == NULL) {
375 printf("%s: no native-PCI interrupt for use by %s channel\n",
376 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
377 rv = 0;
378 }
379
380 return (rv);
381 }
382
383 int
384 pciide_print(aux, pnp)
385 void *aux;
386 const char *pnp;
387 {
388 struct pciide_attach_args *aa = aux;
389
390 /* only 'wdc's can attach to 'pciide's; easy. */
391 if (pnp)
392 printf("wdc at %s", pnp);
393 printf(" channel %d", aa->channel);
394 return (UNCONF);
395 }
396
397 int
398 pciide_compat_intr(arg)
399 void *arg;
400 {
401 struct pciide_channel *cp = arg;
402
403 #ifdef DIAGNOSTIC
404 /* should only be called for a compat channel */
405 if (cp->compat == 0)
406 panic("pciide compat intr called for non-compat chan %p\n", cp);
407 #endif
408 /* if there's no handler, that probably means no dev attached */
409 if (cp->ihand == NULL)
410 return (0);
411
412 return ((*cp->ihand)(cp->ihandarg));
413 }
414
415 int
416 pciide_pci_intr(arg)
417 void *arg;
418 {
419 struct pciide_softc *sc = arg;
420 struct pciide_channel *cp;
421 int i, rv, crv;
422
423 rv = 0;
424 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
425 cp = &sc->sc_channels[i];
426
427 /* If a compat channel or there's no handler, skip. */
428 if (cp->compat || cp->ihand == NULL)
429 continue;
430
431 crv = ((*cp->ihand)(cp->ihandarg));
432 if (crv == 0)
433 ; /* leave rv alone */
434 else if (crv == 1)
435 rv = 1; /* claim the intr */
436 else if (rv == 0) /* crv should be -1 in this case */
437 rv = crv; /* if we've done no better, take it */
438 }
439 return (rv);
440 }
441