Initial import from FreeBSD RELENG_4:
[games.git] / sys / bus / pci / ide_pci.c
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/pci/ide_pci.c,v 1.42 2000/01/29 16:59:53 peter Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/buf.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39
40 #include <i386/isa/wdreg.h>
41 #ifdef PC98
42 #include <pc98/pc98/pc98.h>
43 #else
44 #include <i386/isa/isa.h>
45 #endif
46 #include <i386/isa/isa_device.h>
47
48 #include <pci/pcivar.h>
49 #include <pci/pcireg.h>
50 #include <pci/ide_pcireg.h>
51
52 #ifndef MIN
53 #define MIN(a,b) (((a)<(b))?(a):(b))
54 #endif
55
56 #define PROMISE_ULTRA33 0x4d33105a
57 #define CMD640B_PCI_ID  0x06401095
58
59 struct ide_pci_cookie;  /* structs vendor_fns, ide_pci_cookie are recursive */
60
61 struct vendor_fns {
62         int (*vendor_dmainit)   /* initialize DMA controller and drive */
63              (struct    ide_pci_cookie *cookie,
64               struct    wdparams *wp, 
65               int       (*wdcmd)(int, void *),
66               void      *);
67         
68              void (*vendor_status) /* prints off DMA timing info */
69              (struct    ide_pci_cookie *cookie);
70 };
71
72 /*
73  * XXX the fact that this list keeps all kinds of info on PCI controllers
74  * is pretty grotty-- much of this should be replaced by a proper integration
75  * of PCI probes into the wd driver.
76  * XXX if we're going to support native-PCI controllers, we also need to
77  * keep the address of the IDE control block register, which is something wd.c
78  * needs to know, which is why this info is in the wrong place.
79  */
80
81 struct ide_pci_cookie {
82         LIST_ENTRY(ide_pci_cookie) le;
83         int             iobase_wd;
84         int             ctlr;   /* controller 0/1 on PCI IDE interface */
85         int             unit;
86         int             iobase_bm; /* SFF-8038 control registers */
87         int             altiobase_wd;
88         pcici_t         tag;
89         pcidi_t         type;
90         struct          ide_pci_prd *prd;
91         struct          vendor_fns vs;
92 };
93
94 struct ide_pci_softc {
95         LIST_HEAD(, ide_pci_cookie) cookies;
96 };
97
98 static int
99 generic_dmainit(struct  ide_pci_cookie *cookie, 
100         struct  wdparams *wp, 
101         int     (*wdcmd)(int, void *),
102         void    *wdinfo);
103
104 static void
105 generic_status(struct ide_pci_cookie *cookie);
106
107 static int
108 sis_5591_dmainit(struct ide_pci_cookie *cookie, 
109         struct  wdparams *wp, 
110         int     (*wdcmd)(int, void *),
111         void    *wdinfo);
112
113 static void
114 sis_5591_status(struct ide_pci_cookie *cookie);
115
116 static void
117 via_571_status(struct ide_pci_cookie *cookie);
118
119 static int
120 via_571_dmainit(struct  ide_pci_cookie *cookie, 
121         struct  wdparams *wp, 
122         int     (*wdcmd)(int, void *),
123         void    *wdinfo);
124
125 static void
126 acer_status(struct ide_pci_cookie *cookie);
127
128 static int 
129 acer_dmainit(struct ide_pci_cookie *cookie,
130         struct  wdparams *wp,
131         int     (*wdcmd)(int, void *),
132         void    *wdinfo);
133
134 static void
135 intel_piix_dump_drive(char      *ctlr,
136         int     sitre,
137         int     is_piix4,
138         int     word40,
139         int     word44,
140         int     word48,
141         int     word4a,
142         int     drive);
143
144 static void
145 intel_piix_status(struct ide_pci_cookie *cookie);
146 static int
147 intel_piix_dmainit(struct       ide_pci_cookie *cookie, 
148         struct  wdparams *wp, 
149         int             (*wdcmd)(int, void *),
150         void            *wdinfo);
151
152 static struct ide_pci_cookie *
153 mkcookie(int            iobase_wd, 
154          int            ctlr,
155          int            unit, 
156          int            iobase_bm, 
157          pcici_t        tag, 
158          pcidi_t        type, 
159          struct         vendor_fns *vp,
160          int            altiobase_wd);
161
162
163
164 static void ide_pci_attach(pcici_t tag, int unit);
165 static void *ide_pci_candma(int, int, int);
166 static int ide_pci_dmainit(void *, 
167         struct wdparams *, 
168         int (*)(int, void *),
169         void *);
170
171 static int ide_pci_dmaverify(void *, char *, u_long, int);
172 static int ide_pci_dmasetup(void *, char *, u_long, int);
173 static void ide_pci_dmastart(void *);
174 static int ide_pci_dmadone(void *);
175 static int ide_pci_status(void *);
176 static int ide_pci_iobase(void *xcp);
177 static int ide_pci_altiobase(void *xcp);
178
179 static struct ide_pci_softc softc;
180
181 static int ide_pci_softc_cookies_initted = 0;
182
183 extern struct isa_driver wdcdriver;
184
185 /*
186  * PRD_ALLOC_SIZE should be something that will not be allocated across a 64k
187  * boundary.
188  * PRD_MAX_SEGS is defined to be the maximum number of segments required for
189  * a transfer on an IDE drive, for an xfer that is linear in virtual memory.
190  * PRD_BUF_SIZE is the size of the buffer needed for a PRD table.
191  */
192 #define PRD_ALLOC_SIZE          PAGE_SIZE
193 #define PRD_MAX_SEGS            ((256 * 512 / PAGE_SIZE) + 1)
194 #define PRD_BUF_SIZE            PRD_MAX_SEGS * 8
195
196 static void *prdbuf = 0;
197 static void *prdbuf_next = 0;
198
199 /* 
200  * Hardware specific IDE controller code.  All vendor-specific code
201  * for handling IDE timing and other chipset peculiarities should be
202  * encapsulated here.
203  */
204
205 /* helper funcs */
206
207 /*
208  * nnn_mode() return the highest valid mode, or -1 if the mode class is
209  * not supported
210  */
211
212 static __inline int
213 pio_mode(struct wdparams *wp)
214 {
215         if ((wp->wdp_atavalid & 2) == 2) {
216                 if ((wp->wdp_eidepiomodes & 2) == 2) return 4;
217                 if ((wp->wdp_eidepiomodes & 1) == 1) return 3;
218         }
219         return -1;
220 }
221
222 #if 0
223 static __inline int
224 dma_mode(struct wdparams *wp)
225 {
226         /* XXX not quite sure how to verify validity on this field */
227 }
228 #endif
229
230 static __inline int
231 mwdma_mode(struct wdparams *wp)
232 {
233         /* 
234          * XXX technically, using wdp_atavalid to test for validity of
235          * this field is not quite correct
236          */
237         if ((wp->wdp_atavalid & 2) == 2) {
238                 if ((wp->wdp_dmamword & 4) == 4) return 2;
239                 if ((wp->wdp_dmamword & 2) == 2) return 1;
240                 if ((wp->wdp_dmamword & 1) == 1) return 0;
241         }
242         return -1;
243 }
244
245 static __inline int
246 udma_mode(struct wdparams *wp)
247 {
248         if ((wp->wdp_atavalid & 4) == 4) {
249                 if ((wp->wdp_udmamode & 4) == 4) return 2;
250                 if ((wp->wdp_udmamode & 2) == 2) return 1;
251                 if ((wp->wdp_udmamode & 1) == 1) return 0;
252         }
253         return -1;
254 }
255
256
257 /* Generic busmastering PCI-IDE */
258
259 static int
260 generic_dmainit(struct ide_pci_cookie *cookie, 
261                 struct wdparams *wp, 
262                 int(*wdcmd)(int, void *),
263                 void *wdinfo)
264 {
265         /*
266          * punt on the whole timing issue by looking for either a
267          * drive programmed for both PIO4 and mDMA2 (which use similar
268          * timing) or a drive in an UltraDMA mode (hopefully all
269          * controllers have separate timing for UDMA).  one hopes that if
270          * the drive's DMA mode has been configured by the BIOS, the
271          * controller's has also.
272          *
273          * XXX there are examples where this approach is now known to be
274          * broken, at least on systems based on Intel chipsets.
275          */
276
277         if ((pio_mode(wp) >= 4 && mwdma_mode(wp) >= 2) || 
278             (udma_mode(wp) >= 2)) {
279                 printf("ide_pci: generic_dmainit %04x:%d: warning, IDE controller timing not set\n",
280                         cookie->iobase_wd,
281                         cookie->unit);
282                 /* If we're here, then this controller is most likely not set 
283                    for UDMA, even if the drive may be. Make the drive wise
284                    up. */  
285
286                 if(!wdcmd(WDDMA_MDMA2, wdinfo)) 
287                         printf("generic_dmainit: could not set multiword DMA mode!\n");
288                 return 1;
289         }       
290 #ifdef IDE_PCI_DEBUG
291         printf("pio_mode: %d, mwdma_mode(wp): %d, udma_mode(wp): %d\n",
292                 pio_mode(wp), mwdma_mode(wp), udma_mode(wp));
293 #endif
294         return 0;
295 }
296
297 static void
298 generic_status(struct ide_pci_cookie *cookie)
299 {
300         printf("generic_status: no PCI IDE timing info available\n");
301 }
302
303 static struct vendor_fns vs_generic = 
304
305         generic_dmainit, 
306         generic_status
307 };
308
309 /* VIA Technologies "82C571" PCI-IDE controller core */
310
311 static void
312 via_571_status(struct ide_pci_cookie *cookie)
313 {
314         int iobase_wd;
315         int ctlr, unit;
316         int iobase_bm;
317         pcici_t tag;
318         pcidi_t type;
319         u_long word40[5];
320         int i, unitno;
321
322         iobase_wd = cookie->iobase_wd;
323         unit = cookie->unit;
324         ctlr = cookie->ctlr;
325         iobase_bm = cookie->iobase_bm;
326         tag = cookie->tag;
327         type = cookie->type;
328
329         unitno = ctlr * 2 + unit;
330
331         for (i=0; i<5; i++) {
332                 word40[i] = pci_conf_read(tag, i * 4 + 0x40);
333         }
334
335         if (ctlr == 0)
336                 printf("via_571_status: Primary IDE prefetch/postwrite %s/%s\n",
337                        word40[0] & 0x8000 ? "enabled" : "disabled",
338                        word40[0] & 0x4000 ? "enabled" : "disabled");
339         else
340                 printf("via_571_status: Secondary IDE prefetch/postwrite %s/%s\n",
341                        word40[0] & 0x2000 ? "enabled" : "disabled",
342                        word40[0] & 0x1000 ? "enabled" : "disabled");
343
344         printf("via_571_status: busmaster status read retry %s\n",
345                (word40[1] & 0x08) ? "enabled" : "disabled");
346
347     
348         printf("via_571_status: %s drive %d data setup=%d active=%d recovery=%d\n",
349                unitno < 2 ? "primary" : "secondary", 
350                unitno & 1,
351                ((u_int)(word40[3] >> ((3 - unitno) * 2)) & 3) + 1,
352                ((u_int)(word40[2] >> (((3 - unitno) * 8) + 4)) & 0x0f) + 1,
353                ((u_int)(word40[2] >> ((3 - unitno) * 8)) & 0x0f) + 1);
354     
355         if (ctlr == 0)
356                 printf("via_571_status: primary ctrl active=%d recovery=%d\n",
357                        ((u_int)(word40[3] >> 28) & 0x0f) + 1,
358                        ((u_int)(word40[2] >> 24) & 0x0f) + 1);
359         else
360                 printf("via_571_status: secondary ctrl active=%d recovery=%d\n",
361                        ((u_int)(word40[3] >> 20) & 0x0f) + 1,
362                        ((u_int)(word40[2] >> 16) & 0x0f) + 1);
363
364         /* UltraDMA dump */
365         {
366                 int foo;
367
368                 foo = word40[4] >> ((3 - unitno) * 8);
369                 printf("via_571_status: %s drive %d udma method=%d enable=%d PIOmode=%d cycle=%d\n",
370                        i < 2 ? "primary" : "secondary", 
371                        i & 1,
372                        (foo >> 7) & 1,
373                        (foo >> 6) & 1,
374                        (foo >> 5) & 1,
375                        (foo & 3) + 2);
376         }
377 }
378
379 /*
380  * XXX timing values set here are only good for 30/33MHz buses; should deal
381  * with slower ones too (BTW: you overclock-- you lose)
382  */
383
384 static int
385 via_571_dmainit(struct ide_pci_cookie *cookie, 
386                 struct wdparams *wp, 
387                 int(*wdcmd)(int, void *),
388                 void *wdinfo)
389 {
390         u_long pci_revision;
391         int unitno;
392
393         pci_revision = pci_conf_read(cookie->tag, PCI_CLASS_REG) & 
394                 PCI_REVISION_MASK;
395
396         unitno = cookie->ctlr * 2 + cookie->unit;
397
398         /* If it's a UDMA drive on a '590, set it up */
399         /* 
400          * XXX the revision number we check for is of dubious validity.
401          * it's extracted from the AMD 645 datasheet.
402          */
403         if (pci_revision >= 1 && udma_mode(wp) >= 2) {
404                 unsigned int word50, mask, new;
405                 word50 = pci_conf_read(cookie->tag, 0x50);
406
407                 /* UDMA enable by SET FEATURES, DMA cycles, cycle time 2T */
408                 mask = 0xe3000000 >> (unitno * 8);
409                 new = 0x40000000 >> (unitno * 8);
410
411                 word50 &= ~mask;
412                 word50 |= new;
413
414                 pci_conf_write(cookie->tag, 0x50, word50);
415
416                 /*
417                  * With the '590, drive configuration should come *after* the
418                  * controller configuration, to make sure the controller sees 
419                  * the SET FEATURES command and does the right thing.
420                  */
421                 /* Set UDMA mode 2 on drive */
422                 if (bootverbose)
423                         printf("via_571_dmainit: setting ultra DMA mode 2\n");
424                 if (!wdcmd(WDDMA_UDMA2, wdinfo)) {
425                         printf("via_571_dmainit: setting DMA mode failed\n");
426                         return 0;
427                 }
428
429                 if (bootverbose)
430                         via_571_status(cookie);
431                 return 1;
432
433         }
434
435         /* otherwise, try and program it for MW DMA mode 2 */
436         else if (mwdma_mode(wp) >= 2 && pio_mode(wp) >= 4) {
437                 u_long workword;
438
439                 /* Set multiword DMA mode 2 on drive */
440                 if (bootverbose)
441                         printf("via_571_dmainit: setting multiword DMA mode 2\n");
442                 if (!wdcmd(WDDMA_MDMA2, wdinfo)) {
443                         printf("via_571_dmainit: setting DMA mode failed\n");
444                         return 0;
445                 }
446
447                 /* Configure the controller appropriately for MWDMA mode 2 */
448
449                 workword = pci_conf_read(cookie->tag, 0x40);
450
451                 /* 
452                  * enable prefetch/postwrite-- XXX may cause problems
453                  * with CD-ROMs? 
454                  */
455                 workword |= 0xc000 >> (cookie->ctlr * 2);
456
457                 /* FIFO configurations-- equal split, threshold 1/2 */
458                 workword &= 0x90ffffff;
459                 workword |= 0x2a000000;
460
461                 pci_conf_write(cookie->tag, 0x40, workword);
462
463                 workword = pci_conf_read(cookie->tag, 0x44);
464
465                 /* enable status read retry */
466                 workword |= 8;
467
468                 /* enable FIFO flush on interrupt and end of sector */
469                 workword &= 0xff0cffff;
470                 workword |= 0x00f00000;
471                 pci_conf_write(cookie->tag, 0x44, workword);
472
473                 workword = pci_conf_read(cookie->tag, 0x48);
474                 /* set Mode2 timing */
475                 workword &= ~(0xff000000 >> (unitno * 8));
476                 workword |= 0x31000000 >> (unitno * 8);
477                 pci_conf_write(cookie->tag, 0x48, workword);
478
479                 /* set sector size */
480                 pci_conf_write(cookie->tag, cookie->ctlr ? 0x68 : 0x60, 0x200);
481
482                 if (bootverbose)
483                         via_571_status(cookie);
484
485                 return 1;
486
487         }
488         return 0;
489 }
490
491
492 static struct vendor_fns vs_via_571 = 
493
494         via_571_dmainit, 
495         via_571_status
496 };
497
498 /* Cyrix Cx5530 Courtesy of Whistle Communications */
499
500 /*
501  * Verify that controller can handle a dma request for cp.  Should
502  * not affect any hardware or driver state.
503  * Special version for 5530 that allows only transfers on 16 byte boundaries.(!)
504  * (Yes the Cyrix 5530 can only UDMA to cache-line boundaries.(bleh!))
505  * Luckily nearly all disk IO is to kernel bufers which are page alligned.
506  * They may fix this in some other version of the chip, but it's in the latest
507  * at this time (Jan 1999).
508  */
509 static int
510 cyrix_5530_dmaverify(void *xcp, char *vaddr, u_long count, int dir)
511 {
512         int badfu;
513
514         /*
515          * check for nonaligned or odd-length Stuff
516          */
517         badfu = ((unsigned int)vaddr & 0xf) || (count & 0xf);
518 #ifdef DIAGNOSTIC
519         if (badfu) {
520                 printf("ide_pci: dmaverify odd vaddr or length, ");
521                 printf("vaddr = %p length = %08lx\n", (void *)vaddr, count);
522         }
523 #endif
524         return (!badfu);
525 }
526
527 /*
528  * XXX Unit number handling  may be broken in the Cx5530 modules.
529  * It has only been checked with a single drive.
530  * 12MByte/Sec transfer rates were seen with Quantum Fireball drives
531  * with negligable CPU usage.
532  */
533 static void
534 cyrix_5530_status(struct ide_pci_cookie *cookie)
535 {
536         int iobase_wd;
537         int ctlr, unit;
538         int iobase_bm;
539         pcici_t tag;
540         pcidi_t type;
541         u_long  PIO_config;
542         u_long  DMA_config;
543         int unitno;
544
545         iobase_wd = cookie->iobase_wd;
546         unit = cookie->unit;
547         ctlr = cookie->ctlr;
548         iobase_bm = cookie->iobase_bm;
549         tag = cookie->tag;
550         type = cookie->type;
551
552         unitno = ctlr * 2 + unit;
553
554         PIO_config = inl(iobase_bm + (unit * 0x10) + 0x20);
555         DMA_config = inl(iobase_bm + (unit * 0x10) + 0x24);
556
557
558         printf("cyrix_5530_status: %s:%u IDE PIO cfg: 0x%08lx\n",
559                (ctlr ? "Secondary" : "Primary"), unit, PIO_config);
560         printf("cyrix_5530_status: %s:%u IDE DMA cfg: 0x%08lx\n",
561                (ctlr ? "Secondary" : "Primary"), unit, DMA_config);
562 }
563
564 /*
565  * XXX timing values set here are only good for 30/33MHz buses; should deal
566  * with slower ones too (BTW: you overclock-- you lose)
567  */
568
569 static int
570 cyrix_5530_dmainit(struct ide_pci_cookie *cookie, 
571                 struct wdparams *wp, 
572                 int(*wdcmd)(int, void *),
573                 void *wdinfo)
574 {
575         u_long  pci_revision;
576         int     unitno;
577         int     iobase_bm;
578         int     unit;
579         int drivemode;
580         int mode;
581         int regval;
582
583         /*cookie->unit = 0; */  /* XXX */
584         unit = cookie->unit;
585         pci_revision = pci_conf_read(cookie->tag, PCI_CLASS_REG) & 
586                 PCI_REVISION_MASK;
587
588         unitno = cookie->ctlr * 2 + unit;
589         iobase_bm = cookie->iobase_bm;
590
591         printf("Setting using 0x%x\n", iobase_bm);
592         if ((cookie->ctlr == 0) && (unit == 0)) /* XXX */
593                 outb(iobase_bm + (unit * 0x10) + BMISTA_PORT, 0xe6);
594         /* If it's a UDMA drive on a '5530, set it up */
595         /* 
596          * depending on what the drive can do,
597          * set the correct modes,
598          */
599         printf("wd%d: mw=0x%x, pio=0x%x, pcirev=0x%lx, udma=0x%x\n",
600                 unitno,
601                 mwdma_mode(wp), pio_mode(wp),
602                 pci_revision, udma_mode(wp));
603         if (/* pci_revision >= 1 && */ udma_mode(wp) >= 0) {
604                 switch(udma_mode(wp)) {
605                 case 0:
606                         mode = 0;
607                         drivemode = WDDMA_UDMA0;
608                         regval = 0x00921250;
609                         break;
610                 default: /* newer modes not supported */
611                 case 2:
612 #if 0
613 /*
614  * XXX The 5530 can do mode 2 but if you do use it, it will block all 
615  * access to the PCI bus (and thus the ISA bus, PICs, PIT, etc. etc.) until the
616  * transfer is complete. Mode 2 swamps the 5530 so much it can't really cope
617  * with any other operations. Therefore, use mode 1 for drives that can
618  * do mode 2 (or more). (FALL THROUGH)
619  */
620
621
622                         mode = 2;
623                         drivemode = WDDMA_UDMA2;
624                         regval = 0x00911030;
625                         break;
626 #endif
627                 case 1:
628                         mode = 1;
629                         drivemode = WDDMA_UDMA1;
630                         regval = 0x00911140;
631                         break;
632                 }
633                 /*
634                  * With the Cx5530, drive configuration
635                  * should come *after* the controller configuration,
636                  * to make sure the controller sees 
637                  * the command and does the right thing.
638                  */
639                 /* Set UDMA mode on drive */
640                 if (bootverbose)
641                         printf("cyrix_5530_dmainit: set UDMA mode %d\n", mode);
642                 outl(iobase_bm+0x24 + (unit * 16), regval);
643                 if (!wdcmd(drivemode, wdinfo)) {
644                         printf("cyrix_5530_dmainit: setting DMA mode failed\n");
645                         return 0;
646                 }
647
648                 if (bootverbose)
649                         cyrix_5530_status(cookie);
650                 return 1;
651
652         }
653
654         /* otherwise, try and program it for MW DMA mode 2 */
655         else if (mwdma_mode(wp) >= 0 && pio_mode(wp) >= 4) {
656
657                 switch(mwdma_mode(wp)) {
658                 case 0:
659                         mode = 0;
660                         drivemode = WDDMA_MDMA0;
661                         regval = 0x00017771;
662                         break;
663                 case 1:
664                         mode = 1;
665                         drivemode = WDDMA_MDMA1;
666                         regval = 0x00012121;
667                         break;
668                 default: /* newer modes not supported */
669                 case 2:
670                         mode = 2;
671                         drivemode = WDDMA_MDMA2;
672                         regval = 0x00002020;
673
674                         break;
675                 }
676                 /* Set multiword DMA mode 2 on drive */
677                 if (bootverbose)
678                         printf("cyrix_5530_dmainit: multiword DMA mode %d\n",
679                                                         mode);
680                 if (!wdcmd(drivemode, wdinfo)) {
681                         printf("cyrix_5530_dmainit: setting DMA mode failed\n");
682                         return 0;
683                 }
684
685                 /* Configure the controller appropriately for MWDMA mode */
686
687                 outl(iobase_bm + 0x24 + (unit * 16), regval);
688
689                 if (bootverbose)
690                         cyrix_5530_status(cookie);
691
692                 return 1;
693
694         }
695         /*
696          * Always set the PIO mode values.
697          */
698         switch(pio_mode(wp)) {
699         case 0:
700                 mode = 0;
701                 drivemode = WDDMA_MDMA0;
702                 regval = 0x0000E132;
703                 break;
704         case 1:
705                 mode = 1;
706                 drivemode = WDDMA_MDMA1;
707                 regval = 0x00001812;
708                 break;
709         case 2:
710                 mode = 2;
711                 drivemode = WDDMA_MDMA1;
712                 regval = 0x00024020;
713                 break;
714         case 3:
715                 mode = 3;
716                 drivemode = WDDMA_MDMA1;
717                 regval = 0x00032010;
718                 break;
719         default: /* newer modes not supported */
720         case 4:
721                 mode = 4;
722                 drivemode = WDDMA_MDMA2;
723                 regval = 0x00040010;
724
725                 break;
726         }
727         outl(iobase_bm + 0x20 + (unit * 16), regval);
728         printf("cyrix_5530_dmainit: setting PIO mode %d\n", mode);
729         return 0;
730 }
731
732
733 static struct vendor_fns vs_cyrix_5530 = 
734
735         cyrix_5530_dmainit, 
736         cyrix_5530_status
737 };
738
739
740 static void
741 promise_status(struct ide_pci_cookie *cookie)
742 {
743     pcici_t tag;
744     int i;
745     u_int32_t port0_command, port0_altstatus;
746     u_int32_t port1_command, port1_altstatus;
747     u_int32_t dma_block;
748
749     u_int32_t lat_and_interrupt;
750     u_int32_t drivetiming;
751     int pa, pb, mb, mc;
752
753     tag = cookie->tag;
754     port0_command = pci_conf_read(tag, 0x10);
755     port0_altstatus = pci_conf_read(tag, 0x14);
756     port1_command = pci_conf_read(tag, 0x18);
757     port1_altstatus = pci_conf_read(tag, 0x1c);
758
759     dma_block = pci_conf_read(tag, 0x20);
760     lat_and_interrupt = pci_conf_read(tag, 0x3c);
761
762     printf("promise_status: port0: 0x%lx, port0_alt: 0x%lx, port1: 0x%lx, port1_alt: 0x%lx\n",
763         (u_long)port0_command, (u_long)port0_altstatus, (u_long)port1_command,
764         (u_long)port1_altstatus);
765     printf(
766         "promise_status: dma control blk address: 0x%lx, int: %d, irq: %d\n",
767         (u_long)dma_block, (u_int)(lat_and_interrupt >> 8) & 0xff,
768         (u_int)lat_and_interrupt & 0xff);
769
770     for(i=0;i<4;i+=2) {
771                 drivetiming = pci_conf_read(tag, 0x60 + i * 4);
772                 printf("drivebits%d-%d: %b\n", i, i+1, drivetiming,
773                         "\020\05Prefetch\06Iordy\07Errdy\010Sync\025DmaW\026DmaR");
774         pa = drivetiming & 0xf;
775         pb = (drivetiming >> 8) & 0x1f;
776         mb = (drivetiming >> 13) & 0x7;
777         mc = (drivetiming >> 16) & 0xf;
778         printf("drivetiming%d: pa: 0x%x, pb: 0x%x, mb: 0x%x, mc: 0x%x\n",
779                 i, pa, pb, mb, mc);
780
781         drivetiming = pci_conf_read(tag, 0x60 + (i + 1) * 4);
782         pa = drivetiming & 0xf;
783         pb = (drivetiming >> 8) & 0x1f;
784         mb = (drivetiming >> 13) & 0x7;
785         mc = (drivetiming >> 16) & 0xf;
786         printf("drivetiming%d: pa: 0x%x, pb: 0x%x, mb: 0x%x, mc: 0x%x\n",
787                 i + 1, pa, pb, mb, mc);
788     }
789 }
790
791 static struct vendor_fns vs_promise = 
792
793     generic_dmainit, 
794     promise_status
795 };
796
797 /* Intel PIIX, PIIX3, and PIIX4 IDE controller subfunctions */
798 static void
799 intel_piix_dump_drive(char *ctlr,
800                       int sitre,
801                       int is_piix4,
802                       int word40,
803                       int word44,
804                       int word48,
805                       int word4a,
806                       int drive)
807 {
808         char *ms;
809
810         if (!sitre)
811                 ms = "master/slave";
812         else if (drive == 0)
813                 ms = "master";
814         else
815                 ms = "slave";
816
817         printf("intel_piix_status: %s %s sample = %d, %s recovery = %d\n", 
818                ctlr, 
819                ms, 
820                5 - ((sitre && drive) ?
821                     ((word44 >> 2) & 3) :
822                     ((word40 >> 12) & 3)),
823                ms,
824                4 - ((sitre && drive) ?
825                     ((word44 >> 0) & 3) :
826                     ((word40 >> 8) & 3)));
827
828         word40 >>= (drive * 4);
829         printf("intel_piix_status: %s %s fastDMAonly %s, pre/post %s,\n\
830 intel_piix_status:  IORDY sampling %s,\n\
831 intel_piix_status:  fast PIO %s%s\n", 
832                ctlr,
833                (drive == 0) ? "master" : "slave",
834                (word40 & 8) ? "enabled" : "disabled",
835                (word40 & 4) ? "enabled" : "disabled",
836                (word40 & 2) ? "enabled" : "disabled",
837                (word40 & 1) ? "enabled" : "disabled",
838                ((word40 & 9) == 9) ? " (overridden by fastDMAonly)" : "" );
839
840         if (is_piix4)
841                 printf("intel_piix_status: UltraDMA %s, CT/RP = %d/%d\n",
842                        word48 ? "enabled": "disabled",
843                        4 - (word4a & 3),
844                        6 - (word4a & 3));
845 }
846
847 static void
848 intel_piix_status(struct ide_pci_cookie *cookie)
849 {
850         int iobase_wd;
851         int unit;
852         int iobase_bm;
853         pcici_t tag;
854         pcidi_t type;
855         int ctlr;
856         u_long word40, word44, word48;
857         int sitre, is_piix4;
858
859         iobase_wd = cookie->iobase_wd;
860         unit = cookie->unit;
861         iobase_bm = cookie->iobase_bm;
862         tag = cookie->tag;
863         type = cookie->type;
864         ctlr = cookie->ctlr;
865
866         word40 = pci_conf_read(tag, 0x40);
867         word44 = pci_conf_read(tag, 0x44);
868         word48 = pci_conf_read(tag, 0x48);
869
870         /* 
871          * XXX will not be right for the *next* generation of upward-compatible
872          * intel IDE controllers...
873          */
874         is_piix4 = pci_conf_read(tag, PCI_CLASS_REG) == 0x71118086;
875
876         sitre = word40 & 0x4000;
877
878         switch (ctlr * 2 + unit) {
879         case 0:
880                 intel_piix_dump_drive("primary", 
881                                       sitre, 
882                                       is_piix4,
883                                       word40 & 0xffff, 
884                                       word44 & 0x0f, 
885                                       word48,
886                                       word48 >> 16,
887                                       0);
888                 break;
889         case 1:
890                 intel_piix_dump_drive("primary", 
891                                       sitre, 
892                                       is_piix4,
893                                       word40 & 0xffff, 
894                                       word44 & 0x0f, 
895                                       word48 >> 1,
896                                       word48 >> 20,
897                                       1);
898                 break;
899         case 2:
900                 intel_piix_dump_drive("secondary", 
901                                       sitre, 
902                                       is_piix4,
903                                       (word40 >> 16) & 0xffff, 
904                                       (word44 >> 4) & 0x0f,
905                                       word48 >> 2,
906                                       word48 >> 24,
907                                       0);
908                 break;
909         case 3:
910                 intel_piix_dump_drive("secondary", 
911                                       sitre, 
912                                       is_piix4,
913                                       (word40 >> 16) & 0xffff, 
914                                       (word44 >> 4) & 0x0f,
915                                       word48 >> 3,
916                                       word48 >> 28,
917                                       1);
918                 break;
919         default:
920                 printf("intel_piix_status: bad drive or controller number\n");
921         }
922 }
923
924 /*
925  * XXX timing values set hereare only good for 30/33MHz buses; should deal
926  * with slower ones too (BTW: you overclock-- you lose)
927  */
928
929 static int
930 intel_piix_dmainit(struct ide_pci_cookie *cookie, 
931                    struct wdparams *wp, 
932                    int(*wdcmd)(int, void *),
933                    void *wdinfo)
934 {
935
936         /* If it's a UDMA drive and a PIIX4, set it up */
937         if (cookie->type == 0x71118086 && udma_mode(wp) >= 2) {
938                 /* Set UDMA mode 2 on controller */
939                 int unitno, mask, new;
940
941                 if (bootverbose)
942                         printf("intel_piix_dmainit: setting ultra DMA mode 2\n");
943
944                 if (!wdcmd(WDDMA_UDMA2, wdinfo)) {
945                         printf("intel_piix_dmainit: setting DMA mode failed\n");
946                         return 0;
947                 }
948
949                 unitno = cookie->ctlr * 2 + cookie->unit;
950
951                 mask = (1 << unitno) + (3 << (16 + unitno * 4));
952                 new  = (1 << unitno) + (2 << (16 + unitno * 4));
953
954                 pci_conf_write(cookie->tag, 0x48, 
955                         (pci_conf_read(cookie->tag, 0x48) & ~mask) | new);
956
957                 if (bootverbose)
958                         intel_piix_status(cookie);
959                 return 1;
960         }
961         /* 
962          * if it's an 82371FB, which can't do independent programming of
963          * drive timing, we punt; we're not going to fuss with trying to
964          * coordinate timing modes between drives.  if this is you, get a
965          * new motherboard.  or contribute patches :)
966          *
967          * we do now at least see if the modes set are OK to use.  this should
968          * satisfy the majority of people, with mwdma mode2 drives.
969          */
970         else if (cookie->type == 0x12308086)
971         {
972                 u_long word40;
973
974                 /* can drive do PIO 4 and MW DMA 2? */
975                 if (!(mwdma_mode(wp) >= 2 && pio_mode(wp) >= 4)) 
976                         return 0;
977
978                 word40 = pci_conf_read(cookie->tag, 0x40);
979                 word40 >>= cookie->ctlr * 16;
980
981                 /* Check for timing config usable for DMA on controller */
982                 if (!((word40 & 0x3300) == 0x2300 && 
983                       ((word40 >> (cookie->unit * 4)) & 1) == 1))
984                         return 0;
985
986                 /* Set multiword DMA mode 2 on drive */
987                 if (bootverbose)
988                         printf("intel_piix_dmainit: setting multiword DMA mode 2\n");
989                 if (!wdcmd(WDDMA_MDMA2, wdinfo)) {
990                         printf("intel_piix_dmainit: setting DMA mode failed\n");
991                         return 0;
992                 }
993                 return 1;
994         }
995
996         /* otherwise, treat it as a PIIX3 and program it for MW DMA mode 2 */
997         else if (mwdma_mode(wp) >= 2 && pio_mode(wp) >= 4) {
998                 u_long mask40, mask44, new40, new44;
999
1000                 /* 
1001                  * If SITRE is not set, set it and copy the
1002                  * appropriate bits into the secondary registers.  Do
1003                  * both controllers at once.
1004                  */
1005                 if (((pci_conf_read(cookie->tag, 0x40) >> (16 * cookie->ctlr)) 
1006                      & 0x4000) == 0) {
1007                         unsigned int word40, word44;
1008
1009                         word40 = pci_conf_read(cookie->tag, 0x40);
1010
1011                         /* copy bits to secondary register */
1012                         word44 = pci_conf_read(cookie->tag, 0x44);
1013                         /*
1014                          * I've got a Biostar motherboard with Award
1015                          * BIOS that sets SITRE and secondary timing
1016                          * on one controller but not the other.
1017                          * Bizarre.
1018                          */
1019                         if ((word40 & 0x4000) == 0) {
1020                                 word44 &= ~0xf;
1021                                 word44 |= ((word40 & 0x3000) >> 10) | 
1022                                         ((word40 & 0x0300) >> 8);
1023                         }
1024                         if ((word40 & 0x40000000) == 0) {
1025                                 word44 &= ~0xf0;
1026                                 word44 |= ((word40 & 0x30000000) >> 22) | 
1027                                         ((word40 & 0x03000000) >> 20);
1028                         }
1029                         /* set SITRE */
1030                         word40 |= 0x40004000;
1031
1032                         pci_conf_write(cookie->tag, 0x40, word40);
1033                         pci_conf_write(cookie->tag, 0x44, word44);
1034                 }
1035
1036                 /* Set multiword DMA mode 2 on drive */
1037                 if (bootverbose)
1038                         printf("intel_piix_dmainit: setting multiword DMA mode 2\n");
1039
1040                 if (!wdcmd(WDDMA_MDMA2, wdinfo)) {
1041                         printf("intel_piix_dmainit: setting DMA mode failed\n");
1042                         return 0;
1043                 }
1044
1045                 /* 
1046                  * backward compatible hardware leaves us with such
1047                  * twisted masses of software (aka twiddle the
1048                  * extremely weird register layout on a PIIX3, setting
1049                  * PIO mode 4 and MWDMA mode 2)
1050                  */
1051                 if (cookie->unit == 0) {
1052                         mask40 = 0x330f;
1053                         new40 = 0x2307;
1054                         mask44 = 0;
1055                         new44 = 0;
1056                 } else {
1057                         mask40 = 0x00f0;
1058                         new40 = 0x0070;
1059                         mask44 = 0x000f;
1060                         new44 = 0x000b;
1061                 }
1062
1063                 if (cookie->ctlr) {
1064                         mask40 <<= 16;
1065                         new40 <<= 16;
1066                         mask44 <<= 4;
1067                         new44 <<= 4;
1068                 }
1069
1070                 pci_conf_write(cookie->tag, 0x40, 
1071                                (pci_conf_read(cookie->tag, 0x40) & ~mask40) | new40);
1072                 pci_conf_write(cookie->tag, 0x44, 
1073                                (pci_conf_read(cookie->tag, 0x44) & ~mask44) | new44);
1074
1075                 if (bootverbose)
1076                         intel_piix_status(cookie);
1077                 return 1;
1078         }
1079         return 0;
1080 }
1081
1082 static struct vendor_fns vs_intel_piix = 
1083
1084         intel_piix_dmainit, 
1085         intel_piix_status
1086 };
1087
1088
1089 static void
1090 acer_status(struct ide_pci_cookie *cookie) {
1091         /* XXX does not do anything right now */
1092 }
1093
1094 static int
1095 acer_dmainit(struct ide_pci_cookie *cookie,
1096                    struct wdparams *wp,
1097                    int(*wdcmd)(int, void *),
1098                    void *wdinfo)
1099 {
1100         /* Acer Aladdin DMA setup code. UDMA looks to be sinfully easy to set
1101            on this thing - just one register. */
1102         
1103         u_long word54 = pci_conf_read(cookie->tag, 0x54);
1104         
1105         /* Set the default Acer FIFO settings (0x55 = 13-word depth and
1106            slave operation mode 1) */
1107         
1108         word54 |= 0x5555;
1109         
1110         /* Is this drive UDMA? Set it up if so... */
1111         if(udma_mode(wp) >= 2) {
1112                 /* This is really easy to do. Just write 0xa (enable 
1113                    UDMA mode with 2T timing) into the word at the right
1114                    places. */
1115                 word54 |= (0xA << (16 + (cookie->ctlr * 8) + (cookie->unit * 4)));
1116                 
1117                 /* Now set the drive for UDMA2. */
1118                 if(!wdcmd(WDDMA_UDMA2, wdinfo)) {
1119                         printf("acer_dmainit: could not set UDMA2 mode on wdc%d:%d!\n", cookie->ctlr, cookie->unit);
1120                         return 0;
1121                 }
1122                 
1123                 /* Write the new config into the registers. I'm not 
1124                    sure if I'm doing this in the right order. */
1125                 
1126                 pci_conf_write(cookie->tag, 0x54, word54);
1127                 
1128         } else if(mwdma_mode(wp) >= 2 && pio_mode(wp) >=4) {
1129         
1130         
1131                 /* Otherwise, we're already set for regular DMA. */
1132
1133                 if(!wdcmd(WDDMA_MDMA2, wdinfo)) {
1134                         printf("acer_dmainit: could not set MWDMA2 mode on wdc%d:%d!\n", 
1135                              cookie->ctlr, cookie->unit);
1136                         return 0;
1137                 }
1138                 return 1;
1139         }
1140         
1141         return 0;
1142 }
1143  
1144 static struct vendor_fns vs_acer = 
1145 {
1146         acer_dmainit,
1147         acer_status
1148 };
1149         
1150 /* SiS 5591 */
1151
1152 static void
1153 sis_5591_status(struct ide_pci_cookie *cookie)
1154 {
1155         int iobase_wd;
1156         int ctlr, unit;
1157         int iobase_bm;
1158         pcici_t tag;
1159         pcidi_t type;
1160         u_int word40[5];
1161         int i, unitno;
1162         int DRTC, DATC;
1163         int val;
1164
1165         iobase_wd = cookie->iobase_wd;
1166         unit = cookie->unit;
1167         ctlr = cookie->ctlr;
1168         iobase_bm = cookie->iobase_bm;
1169         tag = cookie->tag;
1170         type = cookie->type;
1171
1172         unitno = ctlr * 2 + unit;
1173
1174         for (i=0; i<5; i++) {
1175                 word40[i] = pci_conf_read(tag, i * 4 + 0x40);
1176         }
1177
1178         DRTC = word40[ctlr] >> (16 * unit);
1179         DATC = word40[ctlr] >> (8 + 16*unit);
1180
1181         if (unitno == 0) {
1182                 if ((word40[4] & 0x80000) == 0) {
1183                         val = word40[2] & 0xf;
1184                         if (val == 0)
1185                                 val = 12;
1186                         else if (val > 11)
1187                                 val++;
1188                         printf ("SiS 5591 status: CRTC %d PCICLK, ", val);
1189                         val = (word40[2] >> 8) & 0x7;
1190                         if (val == 0)
1191                                 val = 8 ;
1192                         else if (val > 6)
1193                                 val = 12;
1194                         printf ("CATC %d PCICLK, applies to all IDE devices\n", val);
1195                 } else {
1196                         printf ("SiS 5591 status: CRTC and CATC timings are per device, taken from DRTC and DATC\n");
1197                 }
1198                 printf ("SiS 5591 status: burst cycles %s, fast post write control %s\n",
1199                         ((word40[2] >> 16) & 0x80) ? "enabled" : "disabled",
1200                         ((word40[2] >> 16) & 0x20) ? "enabled" : "disabled");
1201
1202         }
1203         val = DRTC & 0xf;
1204         if (val == 0)
1205                 val = 12;
1206         else if (val > 11)
1207                 val++;
1208         printf ("SiS 5591 status: %s drive %d DRTC %d PCICLK,",
1209                 unitno < 2 ? "primary" : "secondary", 
1210                 unitno & 1,
1211                 val);
1212         val = DATC & 0x7;
1213         if (val == 0)
1214                 val = 8 ;
1215         else if (val > 6)
1216                 val = 12;
1217         printf (" DATC %d PCICLK\n", val);
1218         printf ("SiS 5591 status: %s drive %d Ultra DMA %s",
1219                 unitno < 2 ? "primary" : "secondary", 
1220                 unitno & 1,
1221                 (DATC & 0x80) ? "disabled\n" : "enabled");
1222         if ((DATC & 0x80) == 0)
1223                 printf (", %d PCICLK data out\n", ((DATC >> 5) & 0x3) + 1);
1224         printf ("SiS 5591 status: %s drive %d postwrite %s, prefetch %s prefetch count is %d\n",
1225                 unitno < 2 ? "primary" : "secondary", 
1226                 unitno & 1,
1227                 ((word40[2] >> (28 + unitno)) & 1) ? "enabled" : "disabled",
1228                 ((word40[2] >> (24 + unitno)) & 1) ? "enabled" : "disabled",
1229                 (word40[3] >> (16 * ctlr)) & 0xffff);
1230         printf ("SiS 5591 status: %s drive %d has%s been configured for DMA\n",
1231                 unitno < 2 ? "primary" : "secondary", 
1232                 unitno & 1,
1233                 (inb(iobase_bm + BMISTA_PORT) & ((unit == 0) ? BMISTA_DMA0CAP : BMISTA_DMA1CAP)) ?
1234                 " " : " not");
1235 }
1236
1237 static int
1238 sis_5591_dmainit(struct ide_pci_cookie *cookie, 
1239                 struct wdparams *wp, 
1240                 int(*wdcmd)(int, void *),
1241                 void *wdinfo)
1242 {
1243         int r;
1244         unsigned int workword, new, mask;
1245         int ctlr, unit;
1246         int iobase_bm;
1247         pcici_t tag;
1248         int unitno;
1249
1250         unit = cookie->unit;
1251         ctlr = cookie->ctlr;
1252         iobase_bm = cookie->iobase_bm;
1253         tag = cookie->tag;
1254
1255         unitno = ctlr * 2 + unit;
1256
1257         if (udma_mode(wp) >= 2) {
1258                 workword = pci_conf_read(tag, ctlr * 4 + 0x40);
1259
1260                 /* These settings are a little arbitrary.  They're taken from my
1261                  * system, where the BIOS has already set the values, but where 
1262                  * we don't detect that we're initialized because the
1263                  * BMISTA_DMA?CAP values aren't set by the BIOS.
1264                  * 0x8000 turns on UDMA
1265                  * 0x2000 sets UDMA cycle time to 2 PCI clocks for data out
1266                  * 0x0300 sets DATC to 3 PCI clocks
1267                  * 0x0001 sets DRTC to 1 PCI clock
1268                  */
1269                 if (unit) {
1270                         mask = 0x0000ffff;
1271                         new  = 0xa3010000;
1272                 } else {
1273                         mask = 0xffff0000;
1274                         new  = 0x0000a301;
1275                 }
1276
1277                 workword &= mask;
1278                 workword |= new;
1279
1280                 pci_conf_write(tag, ctlr * 4 + 0x40, workword);
1281
1282                 outb(iobase_bm + BMISTA_PORT,
1283                      (inb(iobase_bm + BMISTA_PORT) | ((unit == 0) ? BMISTA_DMA0CAP : BMISTA_DMA1CAP)));
1284
1285                 if (bootverbose)
1286                         printf("SiS 5591 dmainit: %s drive %d setting ultra DMA mode 2\n",
1287                                unitno < 2 ? "primary" : "secondary", 
1288                                unitno & 1);
1289                 r = wdcmd(WDDMA_UDMA2, wdinfo);
1290                 if (!r) {
1291                         printf("SiS 5591 dmainit: %s drive %d setting DMA mode failed\n",
1292                                unitno < 2 ? "primary" : "secondary", 
1293                                unitno & 1);
1294                         return 0;
1295                 }
1296
1297                 if (bootverbose)
1298                         sis_5591_status(cookie);
1299
1300                 return 1;
1301
1302         }
1303
1304         /* otherwise, try and program it for MW DMA mode 2 */
1305         else if (mwdma_mode(wp) >= 2 && pio_mode(wp) >= 4) {
1306                 workword = pci_conf_read(tag, ctlr * 4 + 0x40);
1307
1308                 /* These settings are a little arbitrary.  They're taken from my
1309                  * system, where the BIOS has already set the values, but where 
1310                  * we don't detect that we're initialized because the
1311                  * BMISTA_DMA?CAP values aren't set by the BIOS.
1312                  * 0x0300 sets DATC to 3 PCI clocks
1313                  * 0x0001 sets DRTC to 1 PCI clock
1314                  */
1315                 if (unit) {
1316                         mask = 0x0000ffff;
1317                         new  = 0x03010000;
1318                 } else {
1319                         mask = 0xffff0000;
1320                         new  = 0x00000301;
1321                 }
1322
1323                 workword &= mask;
1324                 workword |= new;
1325
1326                 pci_conf_write(tag, ctlr * 4 + 0x40, workword);
1327
1328                 outb(iobase_bm + BMISTA_PORT,
1329                      (inb(iobase_bm + BMISTA_PORT) | ((unit == 0) ? BMISTA_DMA0CAP : BMISTA_DMA1CAP)));
1330
1331                 /* Set multiword DMA mode 2 on drive */
1332                 if (bootverbose)
1333                         printf("SiS 5591 dmainit: %s drive %d setting multiword DMA mode 2\n",
1334                                unitno < 2 ? "primary" : "secondary", 
1335                                unitno & 1);
1336                 r = wdcmd(WDDMA_MDMA2, wdinfo);
1337                 if (!r) {
1338                         printf("SiS 5591 dmainit: %s drive %d setting DMA mode failed\n",
1339                                unitno < 2 ? "primary" : "secondary", 
1340                                unitno & 1);
1341                         return 0;
1342                 }
1343
1344                 if (bootverbose)
1345                         sis_5591_status(cookie);
1346
1347                 return 1;
1348
1349         }
1350         return 0;
1351 }
1352
1353 static struct vendor_fns vs_sis_5591 = 
1354
1355         sis_5591_dmainit, 
1356         sis_5591_status
1357 };
1358
1359 /* Generic SFF-8038i code-- all code below here, except for PCI probes,
1360  * more or less conforms to the SFF-8038i spec as extended for PCI.
1361  * There should be no code that goes beyond that feature set below.
1362  */
1363
1364 /* XXX mkcookie is overloaded with too many parameters */
1365
1366 static struct ide_pci_cookie *
1367 mkcookie(int iobase_wd, 
1368         int ctlr,
1369         int unit, 
1370         int iobase_bm, 
1371         pcici_t tag, 
1372         pcidi_t type, 
1373         struct vendor_fns *vp,
1374         int altiobase_wd)
1375 {
1376         struct ide_pci_cookie *cp;
1377
1378         cp = malloc(sizeof *cp, M_DEVBUF, M_NOWAIT);
1379         if (!cp) return 0;
1380
1381         cp->iobase_wd = iobase_wd;
1382         cp->ctlr = ctlr;
1383         cp->unit = unit;
1384         cp->tag = tag;
1385         cp->type = type;
1386         cp->iobase_bm = iobase_bm;
1387         cp->altiobase_wd = altiobase_wd;
1388         bcopy(vp, &cp->vs, sizeof(struct vendor_fns));
1389
1390         if (!prdbuf) {
1391                 prdbuf = malloc(PRD_ALLOC_SIZE, M_DEVBUF, M_NOWAIT);
1392                 if (!prdbuf) {
1393                         FREE(cp, M_DEVBUF);
1394                         return 0;
1395                 }
1396                 if (((int)prdbuf >> PAGE_SHIFT) ^
1397                     (((int)prdbuf + PRD_ALLOC_SIZE - 1) >> PAGE_SHIFT)) {
1398                         printf("ide_pci: prdbuf straddles page boundary, no DMA\n");
1399                         FREE(cp, M_DEVBUF);
1400                         FREE(prdbuf, M_DEVBUF);
1401                         return 0;
1402                 }
1403
1404                 prdbuf_next = prdbuf;
1405         }
1406         if (((char *)prdbuf_next + PRD_BUF_SIZE) > 
1407             ((char *)prdbuf + PRD_ALLOC_SIZE)) {
1408                 printf("ide_pci: mkcookie %04x:%d: no more space for PRDs, no DMA\n",
1409                        iobase_wd, unit);
1410                 FREE(cp, M_DEVBUF);
1411                 return 0;
1412         }
1413
1414         cp->prd = prdbuf_next;
1415         (char *)prdbuf_next += PRD_BUF_SIZE;
1416
1417         LIST_INSERT_HEAD(&softc.cookies, cp, le);
1418         return cp;
1419 }
1420
1421 static const char *
1422 ide_pci_probe(pcici_t tag, pcidi_t type)
1423 {
1424         u_long data;
1425
1426         data = pci_conf_read(tag, PCI_CLASS_REG);
1427
1428         if ((data & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE &&
1429             ((data & PCI_SUBCLASS_MASK) == 0x00010000 ||
1430             ((data & PCI_SUBCLASS_MASK) == 0x00040000))) {
1431                 if (type == 0x71118086)
1432                         return ("Intel PIIX4 Bus-master IDE controller");
1433                 if (type == 0x70108086)
1434                         return ("Intel PIIX3 Bus-master IDE controller");
1435                 if (type == 0x12308086)
1436                         return ("Intel PIIX Bus-master IDE controller");
1437                 if (type == PROMISE_ULTRA33)
1438                         return ("Promise Ultra/33 IDE controller");
1439                 if (type == 0x05711106)
1440                       return ("VIA 82C586x (Apollo) Bus-master IDE controller");
1441                 if (type == 0x01021078)
1442                         return ("Cyrix 5530 Bus-master IDE controller");
1443                 if (type == 0x522910b9)
1444                         return ("Acer Aladdin IV/V (M5229) Bus-master IDE controller");
1445                 if (type == 0x55131039)
1446                         return ("SiS 5591 Bus-master IDE Controller");
1447                 if (type == CMD640B_PCI_ID)
1448                         return "CMD 640B IDE controller";
1449                 if (data & 0x8000)
1450                         return ("PCI IDE controller (busmaster capable)");
1451                 else
1452                         return ("PCI IDE controller (not busmaster capable)");
1453         };
1454         return ((char*)0);
1455 }
1456
1457 static void
1458 ide_pci_attach(pcici_t tag, int unit)
1459 {
1460         u_long class = 0, cmd;
1461         int bmista_1, bmista_2;
1462         int iobase_wd_1, iobase_wd_2, iobase_bm_1, iobase_bm_2;
1463         int altiobase_wd_1, altiobase_wd_2;
1464         struct vendor_fns *vp;
1465         pcidi_t type;
1466         struct ide_pci_cookie *cookie;
1467         int ctlridx;
1468
1469         ctlridx = unit * 2;
1470
1471         /* set up vendor-specific stuff */
1472         type = pci_conf_read(tag, PCI_ID_REG);
1473
1474         if (type == CMD640B_PCI_ID) {
1475                 wdc_pci(Q_CMD640B);
1476                 return;
1477         }
1478
1479         if (type != PROMISE_ULTRA33) {
1480         /* is it busmaster capable?  bail if not */
1481                 class = pci_conf_read(tag, PCI_CLASS_REG);
1482                 if (!(class & 0x8000)) {
1483                         return;
1484                 }
1485
1486         /* is it enabled and is busmastering turned on? */
1487                 cmd = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
1488                 if ((cmd & 5) != 5) {
1489                         return;
1490                 }
1491         }
1492
1493         switch (type) {
1494         case 0x71118086:
1495         case 0x70108086:
1496         case 0x12308086:
1497                 /* Intel PIIX, PIIX3, PIIX4 */
1498                 vp = &vs_intel_piix;
1499                 break;
1500
1501         case 0x5711106:
1502                 /* VIA Apollo chipset family */
1503                 vp = &vs_via_571;
1504                 break;
1505
1506         case PROMISE_ULTRA33:
1507                 /* Promise controllers */
1508                 vp = &vs_promise;
1509                 break;
1510
1511         case 0x01021078: /* cyrix 5530 */
1512                 printf("cyrix 5530\n");
1513                 vp = &vs_cyrix_5530;
1514                 break;
1515         case 0x522910B9: /* Acer Aladdin IV/V (M5229) */
1516                 vp = &vs_acer;
1517                 break;
1518         case 0x55131039: /* SiS 5591 */
1519                 vp = &vs_sis_5591;
1520                 break;
1521         default:
1522                 /* everybody else */
1523                 vp = &vs_generic;
1524                 break;
1525         }
1526
1527         if (type != PROMISE_ULTRA33) {
1528                 if ((class & 0x100) == 0) {
1529                         iobase_wd_1 = IO_WD1;
1530                         altiobase_wd_1 = iobase_wd_1 + wd_altsts;
1531                 } else {
1532                         iobase_wd_1 = pci_conf_read(tag, 0x10) & 0xfffc;
1533                         altiobase_wd_1 = pci_conf_read(tag, 0x14) & 0xfffc;
1534                 }
1535
1536                 if ((class & 0x400) == 0) {
1537                         iobase_wd_2 = IO_WD2;
1538                         altiobase_wd_2 = iobase_wd_2 + wd_altsts;
1539                 } else {
1540                         iobase_wd_2 = pci_conf_read(tag, 0x18) & 0xfffc;
1541                         altiobase_wd_2 = pci_conf_read(tag, 0x1c) & 0xfffc;
1542                 }
1543         } else {
1544                 iobase_wd_1 = pci_conf_read(tag, 0x10) & 0xfffc;
1545                 altiobase_wd_1 = pci_conf_read(tag, 0x14) & 0xfffc;
1546                 iobase_wd_2 = pci_conf_read(tag, 0x18) & 0xfffc;
1547                 altiobase_wd_2 = pci_conf_read(tag, 0x1c) & 0xfffc;
1548         }
1549
1550         iobase_bm_1 = pci_conf_read(tag, 0x20) & 0xfffc;
1551         if (iobase_bm_1 == 0) {
1552                 printf("ide_pci: BIOS has not configured busmaster"
1553                         "I/O address,\n ide_pci:  giving up\n");
1554                 return;
1555         }
1556         iobase_bm_2 = iobase_bm_1 + SFF8038_CTLR_1;
1557
1558         wddma[unit].wdd_candma = ide_pci_candma;
1559         wddma[unit].wdd_dmainit = ide_pci_dmainit;
1560         if (type == 0x01021078 /*CYRIX_5530*/)
1561                 wddma[unit].wdd_dmaverify = cyrix_5530_dmaverify;
1562         else
1563                 wddma[unit].wdd_dmaverify = ide_pci_dmaverify;
1564         wddma[unit].wdd_dmaprep = ide_pci_dmasetup;
1565         wddma[unit].wdd_dmastart = ide_pci_dmastart;
1566         wddma[unit].wdd_dmadone = ide_pci_dmadone;
1567         wddma[unit].wdd_dmastatus = ide_pci_status;
1568         wddma[unit].wdd_iobase = ide_pci_iobase;
1569         wddma[unit].wdd_altiobase = ide_pci_altiobase;
1570
1571         bmista_1 = inb(iobase_bm_1 + BMISTA_PORT);
1572         bmista_2 = inb(iobase_bm_2 + BMISTA_PORT);
1573                 
1574         if (!ide_pci_softc_cookies_initted) {
1575                 LIST_INIT(&softc.cookies);
1576                 ide_pci_softc_cookies_initted = 1;
1577         }
1578
1579         if (iobase_wd_1 != 0) {
1580                 cookie = mkcookie(iobase_wd_1,
1581                                   ctlridx, 
1582                                   0, 
1583                                   iobase_bm_1, 
1584                                   tag, 
1585                                   type, 
1586                                   vp,
1587                                   altiobase_wd_1);
1588                 if (bootverbose)
1589                         vp->vendor_status(cookie);
1590                 cookie = mkcookie(iobase_wd_1,
1591                                   ctlridx,
1592                                   1,
1593                                   iobase_bm_1, 
1594                                   tag,
1595                                   type,
1596                                   vp,
1597                                   altiobase_wd_1);
1598                 if (bootverbose) {
1599                         vp->vendor_status(cookie);
1600
1601         bmista_1 = inb(iobase_bm_1 + BMISTA_PORT);
1602         bmista_2 = inb(iobase_bm_2 + BMISTA_PORT);
1603                         printf("ide_pci: busmaster 0 status: %02x from port: %08x\n", 
1604                                bmista_1, iobase_bm_1+BMISTA_PORT);
1605
1606                         if (bmista_1 & BMISTA_DMA0CAP)
1607                                 printf("ide_pci: ide0:0 has been configured for DMA by BIOS\n");
1608                         if (bmista_1 & BMISTA_DMA1CAP)
1609                                 printf("ide_pci: ide0:1 has been configured for DMA by BIOS\n");
1610                 }
1611         }
1612
1613         if (iobase_wd_2 != 0) {
1614                 cookie = mkcookie(iobase_wd_2,
1615                                   ctlridx + 1,
1616                                   0, 
1617                                   iobase_bm_2,
1618                                   tag,
1619                                   type, 
1620                                   vp,
1621                                   altiobase_wd_2);
1622                 if (bootverbose)
1623                         vp->vendor_status(cookie);
1624                 cookie = mkcookie(iobase_wd_2,
1625                                   ctlridx + 1,
1626                                   1, 
1627                                   iobase_bm_2, 
1628                                   tag, 
1629                                   type,
1630                                   vp,
1631                                   altiobase_wd_2);
1632                 if (bootverbose) {
1633                         vp->vendor_status(cookie);
1634
1635         bmista_1 = inb(iobase_bm_1 + BMISTA_PORT);
1636         bmista_2 = inb(iobase_bm_2 + BMISTA_PORT);
1637                         printf("ide_pci: busmaster 1 status: %02x from port: %08x\n",
1638                                bmista_2, iobase_bm_2+BMISTA_PORT);
1639
1640                         if (bmista_2 & BMISTA_DMA0CAP)
1641                                 printf("ide_pci: ide1:0 has been configured for DMA by BIOS\n");
1642                         if (bmista_2 & BMISTA_DMA1CAP)
1643                                 printf("ide_pci: ide1:1 has been configured for DMA by BIOS\n");
1644                 }
1645         }
1646 }
1647
1648 static u_long ide_pci_count;
1649
1650 static struct pci_device ide_pci_device = {
1651         "ide_pci",
1652         ide_pci_probe,
1653         ide_pci_attach,
1654         &ide_pci_count,
1655         0
1656 };
1657
1658 COMPAT_PCI_DRIVER(ide_pci, ide_pci_device);
1659
1660 /*
1661  * Return a cookie if we may be able to do DMA on the specified
1662  * (iobase_wd, ctlr, unit).
1663  */
1664 static void *
1665 ide_pci_candma(int iobase_wd, int ctlr, int unit)
1666 {
1667         struct ide_pci_cookie *cp;
1668
1669         cp = softc.cookies.lh_first;
1670         while(cp) {
1671                 if (cp->ctlr == ctlr && cp->unit == unit &&
1672                         ((iobase_wd == 0) || (cp->iobase_wd == iobase_wd)))
1673                         break;
1674                 cp = cp->le.le_next;
1675         }
1676
1677         return cp;
1678 }
1679
1680 /*
1681  * Initialize controller and drive for DMA operation, including timing modes.
1682  * Uses data passed from the wd driver and a callback function to initialize
1683  * timing modes on the drive.
1684  */
1685 static int
1686 ide_pci_dmainit(void *cookie,
1687                 struct wdparams *wp,
1688                 int(*wdcmd)(int, void *),
1689                 void *wdinfo)
1690 {
1691         struct ide_pci_cookie *cp = cookie;
1692         /* 
1693          * If the controller status indicates that DMA is configured already,
1694          * we flounce happily away
1695          */
1696         if (inb(cp->iobase_bm + BMISTA_PORT) & 
1697             ((cp->unit == 0) ? BMISTA_DMA0CAP : BMISTA_DMA1CAP))
1698                 return 1;
1699     
1700         /* We take a stab at it with device-dependent code */
1701         return(cp->vs.vendor_dmainit(cp, wp, wdcmd, wdinfo));
1702 }
1703
1704 /*
1705  * Verify that controller can handle a dma request for cp.  Should
1706  * not affect any hardware or driver state.
1707  */
1708 static int
1709 ide_pci_dmaverify(void *xcp, char *vaddr, u_long count, int dir)
1710 {
1711         int badfu;
1712
1713         /*
1714          * check for nonaligned or odd-length Stuff
1715          */
1716         badfu = ((unsigned int)vaddr & 1) || (count & 1);
1717 #ifdef DIAGNOSTIC
1718         if (badfu) {
1719                 printf("ide_pci: dmaverify odd vaddr or length, ");
1720                 printf("vaddr = %p length = %08lx\n", (void *)vaddr, count);
1721         }
1722 #endif
1723         return (!badfu);
1724 }
1725
1726 /*
1727  * Set up DMA for cp.  It is the responsibility of the caller
1728  * to ensure that the controller is idle before this routine
1729  * is called.
1730  */
1731 static int
1732 ide_pci_dmasetup(void *xcp, char *vaddr, u_long vcount, int dir)
1733 {
1734         struct ide_pci_cookie *cp = xcp;
1735         struct ide_pci_prd *prd;
1736         int i;
1737         u_long firstpage;
1738         u_long prd_base, prd_count;
1739         u_long nbase, ncount, nend;
1740         int iobase_bm;
1741         u_long count;
1742
1743         prd = cp->prd;
1744
1745         count = vcount;
1746
1747         i = 0;
1748
1749         iobase_bm = cp->iobase_bm;
1750
1751         if (count == 0) {
1752                 printf("ide_pci: dmasetup 0-length transfer, ");
1753                 printf("vaddr = %p length = %08lx\n", (void *)vaddr, count);
1754                 return 1;
1755         }
1756
1757         /* Generate first PRD entry, which may be non-aligned. */
1758
1759         firstpage = PAGE_SIZE - ((uintptr_t)vaddr & PAGE_MASK);
1760
1761         prd_base = vtophys(vaddr);
1762         prd_count = MIN(count,  firstpage);
1763
1764         vaddr += prd_count;
1765         count -= prd_count;
1766
1767         /*
1768          * Step through virtual pages.
1769          * Note that it is not worth trying to coalesce pages that are 
1770          * next to each other physically, and some DMA engines (e.g.
1771          * Cyrix Cx5530) actually blow up if you do.
1772          */
1773         while (count) {
1774                 nbase = vtophys(vaddr);
1775                 ncount = MIN(count, PAGE_SIZE);
1776                 nend = nbase + ncount;
1777
1778                 prd[i].prd_base = prd_base;
1779                 prd[i].prd_count = (prd_count & 0xffff);
1780                 i++;
1781                 if (i >= PRD_MAX_SEGS) {
1782                         printf("wd82371: too many segments in PRD table\n");
1783                         return 1;
1784                 }
1785                 prd_base = nbase;
1786                 prd_count = ncount;
1787                 vaddr += ncount;
1788                 count -= ncount;
1789         }
1790
1791         /* Write last PRD entry. */
1792         prd[i].prd_base = prd_base;
1793         prd[i].prd_count = (prd_count & 0xffff) | PRD_EOT_BIT;
1794
1795         /* Set up PRD base register */
1796         outl(iobase_bm + BMIDTP_PORT, vtophys(prd));
1797
1798         /* Set direction of transfer */
1799         outb(iobase_bm + BMICOM_PORT, (dir == B_READ) ? BMICOM_READ_WRITE : 0);
1800
1801         /* Clear interrupt and error bits */
1802         outb(iobase_bm + BMISTA_PORT,
1803              (inb(iobase_bm + BMISTA_PORT) 
1804               | (BMISTA_INTERRUPT | BMISTA_DMA_ERROR)));
1805
1806         return 0;
1807 }               
1808
1809 static void
1810 ide_pci_dmastart(void *xcp)
1811 {
1812         struct ide_pci_cookie *cp = xcp;
1813         int iobase_bm;
1814
1815         iobase_bm = cp->iobase_bm;
1816
1817         outb(iobase_bm + BMICOM_PORT,
1818              inb(iobase_bm + BMICOM_PORT) | BMICOM_STOP_START);
1819
1820 }
1821
1822 static int
1823 ide_pci_dmadone(void *xcp)
1824 {
1825         struct ide_pci_cookie *cp = xcp;
1826         int iobase_bm, status;
1827
1828         status = ide_pci_status(xcp);
1829         iobase_bm = cp->iobase_bm;
1830
1831         outb(iobase_bm + BMICOM_PORT,
1832              inb(iobase_bm + BMICOM_PORT) & ~BMICOM_STOP_START);
1833
1834         return status;
1835 }
1836
1837 static int
1838 ide_pci_status(void *xcp)
1839 {
1840         int iobase_bm, status, bmista;
1841
1842         status = 0;
1843         iobase_bm = ((struct ide_pci_cookie *)xcp)->iobase_bm;
1844
1845         bmista = inb(iobase_bm + BMISTA_PORT);
1846
1847         if (bmista & BMISTA_INTERRUPT)
1848                 status |= WDDS_INTERRUPT;
1849         if (bmista & BMISTA_DMA_ERROR)
1850                 status |= WDDS_ERROR;
1851         if (bmista & BMISTA_DMA_ACTIVE)
1852                 status |= WDDS_ACTIVE;
1853         return status;
1854 }
1855
1856 static int
1857 ide_pci_altiobase(void *xcp)
1858 {
1859         struct ide_pci_cookie *cp = xcp;
1860         if (cp == 0) {
1861                 return 0;
1862         } else {
1863                 return cp->altiobase_wd;
1864         }
1865 }
1866
1867 static int
1868 ide_pci_iobase(void *xcp)
1869 {
1870         struct ide_pci_cookie *cp = xcp;
1871         if (cp == 0) {
1872                 return 0;
1873         } else {
1874                 return cp->iobase_wd;
1875         }
1876 }