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