pciide.c revision 1.7 1 /* $NetBSD: pciide.c,v 1.7 1998/06/08 06:55:57 thorpej 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 #include <dev/ic/wdcreg.h>
58
59 struct pciide_softc {
60 struct device sc_dev;
61
62 void *sc_pci_ih; /* PCI interrupt handle */
63 int sc_dma_ok; /* bus-master DMA info */
64 bus_space_tag_t sc_dma_iot;
65 bus_space_handle_t sc_dma_ioh;
66
67 struct pciide_channel { /* per-channel data */
68 /* internal bookkeeping */
69 int hw_ok; /* hardware mapped & OK? */
70 struct device *dev; /* 'wdc' dev attached */
71 int compat; /* is it compat? */
72 void *ih; /* compat or pci handle */
73
74 /* used by wdc attachment (read-only after init) */
75 bus_space_tag_t cmd_iot, ctl_iot;
76 bus_space_handle_t cmd_ioh, ctl_ioh;
77
78 /* filled in by wdc attachment (written by wdc attach) */
79 int (*ihand) __P((void *));
80 void *ihandarg;
81 } sc_channels[PCIIDE_NUM_CHANNELS];
82 };
83
84 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
85
86 int pciide_match __P((struct device *, struct cfdata *, void *));
87 void pciide_attach __P((struct device *, struct device *, void *));
88
89 struct cfattach pciide_ca = {
90 sizeof(struct pciide_softc), pciide_match, pciide_attach
91 };
92
93 int pciide_map_channel_compat __P((struct pciide_softc *,
94 struct pci_attach_args *, int));
95 const char *pciide_compat_channel_probe __P((struct pciide_softc *,
96 struct pci_attach_args *, int));
97 int pciide_probe_wdc __P((struct pciide_channel *));
98 int pciide_map_channel_native __P((struct pciide_softc *,
99 struct pci_attach_args *, int));
100 int pciide_print __P((void *, const char *pnp));
101 int pciide_compat_intr __P((void *));
102 int pciide_pci_intr __P((void *));
103
104 #define PCIIDE_PROBE_WDC_DELAY 100 /* 100us each */
105 #define PCIIDE_PROBE_WDC_NDELAY 10000 /* wait up to 1s */
106
107 int
108 pciide_match(parent, match, aux)
109 struct device *parent;
110 struct cfdata *match;
111 void *aux;
112 {
113 struct pci_attach_args *pa = aux;
114
115 /*
116 * Check the ID register to see that it's a PCI IDE controller.
117 * If it is, we assume that we can deal with it; it _should_
118 * work in a standardized way...
119 */
120 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
121 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
122 return (1);
123 }
124
125 return (0);
126 }
127
128 void
129 pciide_attach(parent, self, aux)
130 struct device *parent, *self;
131 void *aux;
132 {
133 struct pci_attach_args *pa = aux;
134 pci_chipset_tag_t pc = pa->pa_pc;
135 struct pciide_softc *sc = (struct pciide_softc *)self;
136 struct pciide_attach_args aa;
137 struct pciide_channel *cp;
138 pcireg_t class, interface, csr;
139 pci_intr_handle_t intrhandle;
140 const char *intrstr;
141 char devinfo[256];
142 int i;
143
144 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
145 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
146
147 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
148 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
149 printf("%s: device disabled (at %s)\n", sc->sc_dev.dv_xname,
150 (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
151 return;
152 }
153
154 class = pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG);
155 interface = PCI_INTERFACE(class);
156
157 /*
158 * Set up PCI interrupt.
159 *
160 * If mapping fails, that's (probably) because there's no pin
161 * set to intr, which is (probably) because it's a compat-only
162 * device (or hard-wired in compatibility-only mode). Native-PCI
163 * channels will complain later if the interrupt was needed.
164 *
165 * If establishment fails, that's (probably) some other problem.
166 */
167 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
168 pa->pa_intrline, &intrhandle) == 0) {
169 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
170 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
171 IPL_BIO, pciide_pci_intr, sc);
172
173 if (sc->sc_pci_ih != NULL) {
174 printf("%s: using %s for native-PCI interrupt\n",
175 sc->sc_dev.dv_xname,
176 intrstr ? intrstr : "unknown interrupt");
177 } else {
178 printf("%s: couldn't establish native-PCI interrupt",
179 sc->sc_dev.dv_xname);
180 if (intrstr != NULL)
181 printf(" at %s", intrstr);
182 printf("\n");
183 }
184 }
185
186 /*
187 * Map DMA registers, if DMA is supported.
188 *
189 * Note that sc_dma_ok is the right variable to test to see if
190 * DMA can * be done. If the interface doesn't support DMA,
191 * sc_dma_ok * will never be non-zero. If the DMA regs couldn't
192 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
193 * non-zero if the interface supports DMA and the registers
194 * could be mapped.
195 *
196 * XXX Note that despite the fact that the Bus Master IDE specs
197 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
198 * XXX space," some controllers (at least the United
199 * XXX Microelectronics UM8886BF) place it in memory space.
200 * XXX eventually, we should probably read the register and check
201 * XXX which type it is. Either that or 'quirk' certain devices.
202 */
203 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
204 sc->sc_dma_ok = (pci_mapreg_map(pa,
205 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
206 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
207 printf("%s: bus-master DMA support present, but unused (%s)\n",
208 sc->sc_dev.dv_xname,
209 sc->sc_dma_ok ? "no driver support" :
210 "couldn't map registers");
211 }
212
213 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
214 cp = &sc->sc_channels[i];
215
216 printf("%s: %s channel %s to %s mode\n",
217 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(i),
218 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
219 "configured" : "wired",
220 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
221 "compatibility");
222
223 if (interface & PCIIDE_INTERFACE_PCI(i))
224 cp->hw_ok = pciide_map_channel_native(sc, pa, i);
225 else
226 cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
227 if (!cp->hw_ok)
228 continue;
229
230 aa.channel = i;
231 aa.cmd_iot = cp->cmd_iot;
232 aa.cmd_ioh = cp->cmd_ioh;
233 aa.ctl_iot = cp->ctl_iot;
234 aa.ctl_ioh = cp->ctl_ioh;
235 aa.ihandp = &cp->ihand;
236 aa.ihandargp = &cp->ihandarg;
237 cp->dev = config_found(self, &aa, pciide_print);
238
239 /*
240 * Note that if the 'wdc' device isn't configured,
241 * the controller's resources are still marked as
242 * being in use. This is a feature.
243 */
244 }
245 }
246
247 int
248 pciide_map_channel_compat(sc, pa, chan)
249 struct pciide_softc *sc;
250 struct pci_attach_args *pa;
251 int chan;
252 {
253 struct pciide_channel *cp = &sc->sc_channels[chan];
254 const char *probe_fail_reason;
255 int rv = 1;
256
257 cp->compat = 1;
258
259 cp->cmd_iot = pa->pa_iot;
260 if (bus_space_map(cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
261 PCIIDE_COMPAT_CMD_SIZE, 0, &cp->cmd_ioh) != 0) {
262 printf("%s: couldn't map %s channel cmd regs\n",
263 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
264 rv = 0;
265 }
266
267 cp->ctl_iot = pa->pa_iot;
268 if (bus_space_map(cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
269 PCIIDE_COMPAT_CTL_SIZE, 0, &cp->ctl_ioh) != 0) {
270 printf("%s: couldn't map %s channel ctl regs\n",
271 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
272 rv = 0;
273 }
274
275 /*
276 * If we weren't able to map the device successfully,
277 * we just give up now. Something else has already
278 * occupied those ports, indicating that the device has
279 * (probably) been completely disabled (by some nonstandard
280 * mechanism).
281 *
282 * XXX If we successfully map some ports, but not others,
283 * XXX it might make sense to unmap the ones that we mapped.
284 */
285 if (rv == 0)
286 goto out;
287
288 /*
289 * If we were able to map the device successfully, try to
290 * make sure that there's a wdc there and that it's
291 * attributable to us.
292 *
293 * If there's not, then we assume that there's the device
294 * has been disabled and that other devices are free to use
295 * its ports.
296 */
297 probe_fail_reason = pciide_compat_channel_probe(sc, pa, chan);
298 if (probe_fail_reason != NULL) {
299 printf("%s: %s channel ignored (%s)\n", sc->sc_dev.dv_xname,
300 PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
301 rv = 0;
302
303 bus_space_unmap(cp->cmd_iot, cp->cmd_ioh,
304 PCIIDE_COMPAT_CMD_SIZE);
305 bus_space_unmap(cp->ctl_iot, cp->ctl_ioh,
306 PCIIDE_COMPAT_CTL_SIZE);
307
308 goto out;
309 }
310
311 /*
312 * If we're here, we were able to map the device successfully
313 * and it really looks like there's a controller there.
314 *
315 * Unless those conditions are true, we don't map the
316 * compatibility interrupt. The spec indicates that if a
317 * channel is configured for compatibility mode and the PCI
318 * device's I/O space is enabled, the channel will be enabled.
319 * Hoewver, some devices seem to be able to disable invididual
320 * compatibility channels (via non-standard mechanisms). If
321 * the channel is disabled, the interrupt line can (probably)
322 * be used by other devices (and may be assigned to other
323 * devices by the BIOS). If we mapped the interrupt we might
324 * conflict with another interrupt assignment.
325 */
326 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_dev, pa,
327 chan, pciide_compat_intr, cp);
328 if (cp->ih == NULL) {
329 printf("%s: no compatibility interrupt for use by %s channel\n",
330 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
331 rv = 0;
332 }
333
334 out:
335 return (rv);
336 }
337
338 const char *
339 pciide_compat_channel_probe(sc, pa, chan)
340 struct pciide_softc *sc;
341 struct pci_attach_args *pa;
342 {
343 pcireg_t csr;
344 const char *failreason = NULL;
345
346 /*
347 * Check to see if something appears to be there.
348 */
349 if (!pciide_probe_wdc(&sc->sc_channels[chan])) {
350 failreason = "not responding; disabled or no drives?";
351 goto out;
352 }
353
354 /*
355 * Now, make sure it's actually attributable to this PCI IDE
356 * channel by trying to access the channel again while the
357 * PCI IDE controller's I/O space is disabled. (If the
358 * channel no longer appears to be there, it belongs to
359 * this controller.) YUCK!
360 */
361 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
362 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
363 csr & ~PCI_COMMAND_IO_ENABLE);
364 if (pciide_probe_wdc(&sc->sc_channels[chan]))
365 failreason = "other hardware responding at addresses";
366 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
367
368 out:
369 return (failreason);
370 }
371
372 int
373 pciide_probe_wdc(cp)
374 struct pciide_channel *cp;
375 {
376 u_int8_t st0, st1;
377 int timeout;
378
379 /*
380 * Sanity check to see if the wdc channel responds at all.
381 * (Modeled on wdc_init_controller() and wdc_reset() in wdc.c.)
382 *
383 * Reset the channel, and make sure that it responds sanely
384 * after it's been reset.
385 */
386
387 /* Reset the channel. */
388 bus_space_write_1(cp->ctl_iot, cp->ctl_ioh, wd_aux_ctlr,
389 WDCTL_RST | WDCTL_IDS);
390 delay(1000);
391 bus_space_write_1(cp->ctl_iot, cp->ctl_ioh, wd_aux_ctlr,
392 WDCTL_IDS);
393 delay(1000);
394 (void)bus_space_read_1(cp->cmd_iot, cp->cmd_ioh, wd_error);
395
396 timeout = 0;
397 while (timeout++ < PCIIDE_PROBE_WDC_NDELAY) {
398 st0 = bus_space_read_1(cp->cmd_iot, cp->cmd_ioh, wd_status);
399 bus_space_write_1(cp->cmd_iot, cp->cmd_ioh, wd_sdh,
400 WDSD_IBM | 0x10);
401 st1 = bus_space_read_1(cp->cmd_iot, cp->cmd_ioh, wd_status);
402
403 if ((st0 & WDCS_BSY) == 0 || (st1 & WDCS_BSY) == 0)
404 return (1);
405
406 delay(PCIIDE_PROBE_WDC_DELAY);
407 }
408 /* timed out; nothing there */
409
410 return (0);
411 }
412
413 int
414 pciide_map_channel_native(sc, pa, chan)
415 struct pciide_softc *sc;
416 struct pci_attach_args *pa;
417 int chan;
418 {
419 struct pciide_channel *cp = &sc->sc_channels[chan];
420 int rv = 1;
421
422 cp->compat = 0;
423
424 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
425 0, &cp->cmd_iot, &cp->cmd_ioh, NULL, NULL) != 0) {
426 printf("%s: couldn't map %s channel cmd regs\n",
427 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
428 rv = 0;
429 }
430
431 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
432 0, &cp->ctl_iot, &cp->ctl_ioh, NULL, NULL) != 0) {
433 printf("%s: couldn't map %s channel ctl regs\n",
434 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
435 rv = 0;
436 }
437
438 if ((cp->ih = sc->sc_pci_ih) == NULL) {
439 printf("%s: no native-PCI interrupt for use by %s channel\n",
440 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(chan));
441 rv = 0;
442 }
443
444 return (rv);
445 }
446
447 int
448 pciide_print(aux, pnp)
449 void *aux;
450 const char *pnp;
451 {
452 struct pciide_attach_args *aa = aux;
453
454 /* only 'wdc's can attach to 'pciide's; easy. */
455 if (pnp)
456 printf("wdc at %s", pnp);
457 printf(" channel %d", aa->channel);
458 return (UNCONF);
459 }
460
461 int
462 pciide_compat_intr(arg)
463 void *arg;
464 {
465 struct pciide_channel *cp = arg;
466
467 #ifdef DIAGNOSTIC
468 /* should only be called for a compat channel */
469 if (cp->compat == 0)
470 panic("pciide compat intr called for non-compat chan %p\n", cp);
471 #endif
472 /* if there's no handler, that probably means no dev attached */
473 if (cp->ihand == NULL)
474 return (0);
475
476 return ((*cp->ihand)(cp->ihandarg));
477 }
478
479 int
480 pciide_pci_intr(arg)
481 void *arg;
482 {
483 struct pciide_softc *sc = arg;
484 struct pciide_channel *cp;
485 int i, rv, crv;
486
487 rv = 0;
488 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
489 cp = &sc->sc_channels[i];
490
491 /* If a compat channel or there's no handler, skip. */
492 if (cp->compat || cp->ihand == NULL)
493 continue;
494
495 crv = ((*cp->ihand)(cp->ihandarg));
496 if (crv == 0)
497 ; /* leave rv alone */
498 else if (crv == 1)
499 rv = 1; /* claim the intr */
500 else if (rv == 0) /* crv should be -1 in this case */
501 rv = crv; /* if we've done no better, take it */
502 }
503 return (rv);
504 }
505