patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / pci / siimage.c
1 /*
2  * linux/drivers/ide/pci/siimage.c              Version 1.07    Nov 30, 2003
3  *
4  * Copyright (C) 2001-2002      Andre Hedrick <andre@linux-ide.org>
5  * Copyright (C) 2003           Red Hat <alan@redhat.com>
6  *
7  *  May be copied or modified under the terms of the GNU General Public License
8  *
9  *  Documentation available under NDA only
10  *
11  *
12  *  FAQ Items:
13  *      If you are using Marvell SATA-IDE adapters with Maxtor drives
14  *      ensure the system is set up for ATA100/UDMA5 not UDMA6.
15  *
16  *      If you are using WD drives with SATA bridges you must set the
17  *      drive to "Single". "Master" will hang
18  *
19  *      If you have strange problems with nVidia chipset systems please
20  *      see the SI support documentation and update your system BIOS
21  *      if neccessary
22  */
23
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/hdreg.h>
29 #include <linux/ide.h>
30 #include <linux/init.h>
31
32 #include <asm/io.h>
33
34 #undef SIIMAGE_VIRTUAL_DMAPIO
35 #undef SIIMAGE_LARGE_DMA
36
37 /**
38  *      pdev_is_sata            -       check if device is SATA
39  *      @pdev:  PCI device to check
40  *      
41  *      Returns true if this is a SATA controller
42  */
43  
44 static int pdev_is_sata(struct pci_dev *pdev)
45 {
46         switch(pdev->device)
47         {
48                 case PCI_DEVICE_ID_SII_3112:
49                 case PCI_DEVICE_ID_SII_1210SA:
50                         return 1;
51                 case PCI_DEVICE_ID_SII_680:
52                         return 0;
53         }
54         BUG();
55         return 0;
56 }
57  
58 /**
59  *      is_sata                 -       check if hwif is SATA
60  *      @hwif:  interface to check
61  *      
62  *      Returns true if this is a SATA controller
63  */
64  
65 static inline int is_sata(ide_hwif_t *hwif)
66 {
67         return pdev_is_sata(hwif->pci_dev);
68 }
69
70 /**
71  *      siimage_selreg          -       return register base
72  *      @hwif: interface
73  *      @r: config offset
74  *
75  *      Turn a config register offset into the right address in either
76  *      PCI space or MMIO space to access the control register in question
77  *      Thankfully this is a configuration operation so isnt performance
78  *      criticial. 
79  */
80  
81 static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
82 {
83         unsigned long base = (unsigned long)hwif->hwif_data;
84         base += 0xA0 + r;
85         if(hwif->mmio)
86                 base += (hwif->channel << 6);
87         else
88                 base += (hwif->channel << 4);
89         return base;
90 }
91         
92 /**
93  *      siimage_seldev          -       return register base
94  *      @hwif: interface
95  *      @r: config offset
96  *
97  *      Turn a config register offset into the right address in either
98  *      PCI space or MMIO space to access the control register in question
99  *      including accounting for the unit shift.
100  */
101  
102 static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
103 {
104         ide_hwif_t *hwif        = HWIF(drive);
105         unsigned long base = (unsigned long)hwif->hwif_data;
106         base += 0xA0 + r;
107         if(hwif->mmio)
108                 base += (hwif->channel << 6);
109         else
110                 base += (hwif->channel << 4);
111         base |= drive->select.b.unit << drive->select.b.unit;
112         return base;
113 }
114
115 /**
116  *      siimage_ratemask        -       Compute available modes
117  *      @drive: IDE drive
118  *
119  *      Compute the available speeds for the devices on the interface.
120  *      For the CMD680 this depends on the clocking mode (scsc), for the
121  *      SI3312 SATA controller life is a bit simpler. Enforce UDMA33
122  *      as a limit if there is no 80pin cable present.
123  */
124  
125 static byte siimage_ratemask (ide_drive_t *drive)
126 {
127         ide_hwif_t *hwif        = HWIF(drive);
128         u8 mode = 0, scsc = 0;
129         unsigned long base = (unsigned long) hwif->hwif_data;
130
131         if (hwif->mmio)
132                 scsc = hwif->INB(base + 0x4A);
133         else
134                 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
135
136         if(is_sata(hwif))
137         {
138                 if(strstr(drive->id->model, "Maxtor"))
139                         return 3;
140                 return 4;
141         }
142         
143         if ((scsc & 0x30) == 0x10)      /* 133 */
144                 mode = 4;
145         else if ((scsc & 0x30) == 0x20) /* 2xPCI */
146                 mode = 4;
147         else if ((scsc & 0x30) == 0x00) /* 100 */
148                 mode = 3;
149         else    /* Disabled ? */
150                 BUG();
151
152         if (!eighty_ninty_three(drive))
153                 mode = min(mode, (u8)1);
154         return mode;
155 }
156
157 /**
158  *      siimage_taskfile_timing -       turn timing data to a mode
159  *      @hwif: interface to query
160  *
161  *      Read the timing data for the interface and return the 
162  *      mode that is being used.
163  */
164  
165 static byte siimage_taskfile_timing (ide_hwif_t *hwif)
166 {
167         u16 timing      = 0x328a;
168         unsigned long addr = siimage_selreg(hwif, 2);
169
170         if (hwif->mmio)
171                 timing = hwif->INW(addr);
172         else
173                 pci_read_config_word(hwif->pci_dev, addr, &timing);
174
175         switch (timing) {
176                 case 0x10c1:    return 4;
177                 case 0x10c3:    return 3;
178                 case 0x1104:
179                 case 0x1281:    return 2;
180                 case 0x2283:    return 1;
181                 case 0x328a:
182                 default:        return 0;
183         }
184 }
185
186 /**
187  *      simmage_tuneproc        -       tune a drive
188  *      @drive: drive to tune
189  *      @mode_wanted: the target operating mode
190  *
191  *      Load the timing settings for this device mode into the
192  *      controller. If we are in PIO mode 3 or 4 turn on IORDY
193  *      monitoring (bit 9). The TF timing is bits 31:16
194  */
195  
196 static void siimage_tuneproc (ide_drive_t *drive, byte mode_wanted)
197 {
198         ide_hwif_t *hwif        = HWIF(drive);
199         u32 speedt              = 0;
200         u16 speedp              = 0;
201         unsigned long addr      = siimage_seldev(drive, 0x04);
202         unsigned long tfaddr    = siimage_selreg(hwif, 0x02);
203         
204         /* cheat for now and use the docs */
205         switch(mode_wanted) {
206                 case 4: 
207                         speedp = 0x10c1; 
208                         speedt = 0x10c1;
209                         break;
210                 case 3: 
211                         speedp = 0x10C3; 
212                         speedt = 0x10C3;
213                         break;
214                 case 2: 
215                         speedp = 0x1104; 
216                         speedt = 0x1281;
217                         break;
218                 case 1:         
219                         speedp = 0x2283; 
220                         speedt = 0x1281;
221                         break;
222                 case 0:
223                 default:
224                         speedp = 0x328A; 
225                         speedt = 0x328A;
226                         break;
227         }
228         if (hwif->mmio)
229         {
230                 hwif->OUTW(speedt, addr);
231                 hwif->OUTW(speedp, tfaddr);
232                 /* Now set up IORDY */
233                 if(mode_wanted == 3 || mode_wanted == 4)
234                         hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
235                 else
236                         hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
237         }
238         else
239         {
240                 pci_write_config_word(hwif->pci_dev, addr, speedp);
241                 pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
242                 pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
243                 speedp &= ~0x200;
244                 /* Set IORDY for mode 3 or 4 */
245                 if(mode_wanted == 3 || mode_wanted == 4)
246                         speedp |= 0x200;
247                 pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
248         }
249 }
250
251 /**
252  *      config_siimage_chipset_for_pio  -       set drive timings
253  *      @drive: drive to tune
254  *      @speed we want
255  *
256  *      Compute the best pio mode we can for a given device. Also honour
257  *      the timings for the driver when dealing with mixed devices. Some
258  *      of this is ugly but its all wrapped up here
259  *
260  *      The SI680 can also do VDMA - we need to start using that
261  *
262  *      FIXME: we use the BIOS channel timings to avoid driving the task
263  *      files too fast at the disk. We need to compute the master/slave
264  *      drive PIO mode properly so that we can up the speed on a hotplug
265  *      system.
266  */
267  
268 static void config_siimage_chipset_for_pio (ide_drive_t *drive, byte set_speed)
269 {
270         u8 channel_timings      = siimage_taskfile_timing(HWIF(drive));
271         u8 speed = 0, set_pio   = ide_get_best_pio_mode(drive, 4, 5, NULL);
272
273         /* WARNING PIO timing mess is going to happen b/w devices, argh */
274         if ((channel_timings != set_pio) && (set_pio > channel_timings))
275                 set_pio = channel_timings;
276
277         siimage_tuneproc(drive, set_pio);
278         speed = XFER_PIO_0 + set_pio;
279         if (set_speed)
280                 (void) ide_config_drive_speed(drive, speed);
281 }
282
283 static void config_chipset_for_pio (ide_drive_t *drive, byte set_speed)
284 {
285         config_siimage_chipset_for_pio(drive, set_speed);
286 }
287
288 /**
289  *      siimage_tune_chipset    -       set controller timings
290  *      @drive: Drive to set up
291  *      @xferspeed: speed we want to achieve
292  *
293  *      Tune the SII chipset for the desired mode. If we can't achieve
294  *      the desired mode then tune for a lower one, but ultimately
295  *      make the thing work.
296  */
297  
298 static int siimage_tune_chipset (ide_drive_t *drive, byte xferspeed)
299 {
300         u8 ultra6[]             = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
301         u8 ultra5[]             = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
302         u16 dma[]               = { 0x2208, 0x10C2, 0x10C1 };
303
304         ide_hwif_t *hwif        = HWIF(drive);
305         u16 ultra = 0, multi    = 0;
306         u8 mode = 0, unit       = drive->select.b.unit;
307         u8 speed                = ide_rate_filter(siimage_ratemask(drive), xferspeed);
308         unsigned long base      = (unsigned long)hwif->hwif_data;
309         u8 scsc = 0, addr_mask  = ((hwif->channel) ?
310                                     ((hwif->mmio) ? 0xF4 : 0x84) :
311                                     ((hwif->mmio) ? 0xB4 : 0x80));
312                                     
313         unsigned long ma        = siimage_seldev(drive, 0x08);
314         unsigned long ua        = siimage_seldev(drive, 0x0C);
315
316         if (hwif->mmio) {
317                 scsc = hwif->INB(base + 0x4A);
318                 mode = hwif->INB(base + addr_mask);
319                 multi = hwif->INW(ma);
320                 ultra = hwif->INW(ua);
321         } else {
322                 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
323                 pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
324                 pci_read_config_word(hwif->pci_dev, ma, &multi);
325                 pci_read_config_word(hwif->pci_dev, ua, &ultra);
326         }
327
328         mode &= ~((unit) ? 0x30 : 0x03);
329         ultra &= ~0x3F;
330         scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
331
332         scsc = is_sata(hwif) ? 1 : scsc;
333
334         switch(speed) {
335                 case XFER_PIO_4:
336                 case XFER_PIO_3:
337                 case XFER_PIO_2:
338                 case XFER_PIO_1:
339                 case XFER_PIO_0:
340                         siimage_tuneproc(drive, (speed - XFER_PIO_0));
341                         mode |= ((unit) ? 0x10 : 0x01);
342                         break;
343                 case XFER_MW_DMA_2:
344                 case XFER_MW_DMA_1:
345                 case XFER_MW_DMA_0:
346                         multi = dma[speed - XFER_MW_DMA_0];
347                         mode |= ((unit) ? 0x20 : 0x02);
348                         config_siimage_chipset_for_pio(drive, 0);
349                         break;
350                 case XFER_UDMA_6:
351                 case XFER_UDMA_5:
352                 case XFER_UDMA_4:
353                 case XFER_UDMA_3:
354                 case XFER_UDMA_2:
355                 case XFER_UDMA_1:
356                 case XFER_UDMA_0:
357                         multi = dma[2];
358                         ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
359                                            (ultra5[speed - XFER_UDMA_0]));
360                         mode |= ((unit) ? 0x30 : 0x03);
361                         config_siimage_chipset_for_pio(drive, 0);
362                         break;
363                 default:
364                         return 1;
365         }
366
367         if (hwif->mmio) {
368                 hwif->OUTB(mode, base + addr_mask);
369                 hwif->OUTW(multi, ma);
370                 hwif->OUTW(ultra, ua);
371         } else {
372                 pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
373                 pci_write_config_word(hwif->pci_dev, ma, multi);
374                 pci_write_config_word(hwif->pci_dev, ua, ultra);
375         }
376         return (ide_config_drive_speed(drive, speed));
377 }
378
379 /**
380  *      config_chipset_for_dma  -       configure for DMA
381  *      @drive: drive to configure
382  *
383  *      Called by the IDE layer when it wants the timings set up.
384  *      For the CMD680 we also need to set up the PIO timings and
385  *      enable DMA.
386  */
387  
388 static int config_chipset_for_dma (ide_drive_t *drive)
389 {
390         u8 speed        = ide_dma_speed(drive, siimage_ratemask(drive));
391
392         config_chipset_for_pio(drive, !speed);
393
394         if (!speed)
395                 return 0;
396
397         if (ide_set_xfer_rate(drive, speed))
398                 return 0;
399
400         if (!drive->init_speed)
401                 drive->init_speed = speed;
402
403         return ide_dma_enable(drive);
404 }
405
406 /**
407  *      siimage_configure_drive_for_dma -       set up for DMA transfers
408  *      @drive: drive we are going to set up
409  *
410  *      Set up the drive for DMA, tune the controller and drive as 
411  *      required. If the drive isn't suitable for DMA or we hit
412  *      other problems then we will drop down to PIO and set up
413  *      PIO appropriately
414  */
415  
416 static int siimage_config_drive_for_dma (ide_drive_t *drive)
417 {
418         ide_hwif_t *hwif        = HWIF(drive);
419         struct hd_driveid *id   = drive->id;
420
421         if ((id->capability & 1) != 0 && drive->autodma) {
422                 /* Consult the list of known "bad" drives */
423                 if (__ide_dma_bad_drive(drive))
424                         goto fast_ata_pio;
425
426                 if ((id->field_valid & 4) && siimage_ratemask(drive)) {
427                         if (id->dma_ultra & hwif->ultra_mask) {
428                                 /* Force if Capable UltraDMA */
429                                 int dma = config_chipset_for_dma(drive);
430                                 if ((id->field_valid & 2) && !dma)
431                                         goto try_dma_modes;
432                         }
433                 } else if (id->field_valid & 2) {
434 try_dma_modes:
435                         if ((id->dma_mword & hwif->mwdma_mask) ||
436                             (id->dma_1word & hwif->swdma_mask)) {
437                                 /* Force if Capable regular DMA modes */
438                                 if (!config_chipset_for_dma(drive))
439                                         goto no_dma_set;
440                         }
441                 } else if (__ide_dma_good_drive(drive) &&
442                            (id->eide_dma_time < 150)) {
443                         /* Consult the list of known "good" drives */
444                         if (!config_chipset_for_dma(drive))
445                                 goto no_dma_set;
446                 } else {
447                         goto fast_ata_pio;
448                 }
449                 return hwif->ide_dma_on(drive);
450         } else if ((id->capability & 8) || (id->field_valid & 2)) {
451 fast_ata_pio:
452 no_dma_set:
453                 config_chipset_for_pio(drive, 1);
454                 return hwif->ide_dma_off_quietly(drive);
455         }
456         /* IORDY not supported */
457         return 0;
458 }
459
460 /* returns 1 if dma irq issued, 0 otherwise */
461 static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
462 {
463         ide_hwif_t *hwif        = HWIF(drive);
464         u8 dma_altstat          = 0;
465         unsigned long addr      = siimage_selreg(hwif, 1);
466
467         /* return 1 if INTR asserted */
468         if ((hwif->INB(hwif->dma_status) & 4) == 4)
469                 return 1;
470
471         /* return 1 if Device INTR asserted */
472         pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
473         if (dma_altstat & 8)
474                 return 0;       //return 1;
475         return 0;
476 }
477
478 #if 0
479 /**
480  *      siimage_mmio_ide_dma_count      -       DMA bytes done
481  *      @drive
482  *
483  *      If we are doing VDMA the CMD680 requires a little bit
484  *      of more careful handling and we have to read the counts
485  *      off ourselves. For non VDMA life is normal.
486  */
487  
488 static int siimage_mmio_ide_dma_count (ide_drive_t *drive)
489 {
490 #ifdef SIIMAGE_VIRTUAL_DMAPIO
491         struct request *rq      = HWGROUP(drive)->rq;
492         ide_hwif_t *hwif        = HWIF(drive);
493         u32 count               = (rq->nr_sectors * SECTOR_SIZE);
494         u32 rcount              = 0;
495         unsigned long addr      = siimage_selreg(hwif, 0x1C);
496
497         hwif->OUTL(count, addr);
498         rcount = hwif->INL(addr);
499
500         printk("\n%s: count = %d, rcount = %d, nr_sectors = %lu\n",
501                 drive->name, count, rcount, rq->nr_sectors);
502
503 #endif /* SIIMAGE_VIRTUAL_DMAPIO */
504         return __ide_dma_count(drive);
505 }
506 #endif
507
508 /**
509  *      siimage_mmio_ide_dma_test_irq   -       check we caused an IRQ
510  *      @drive: drive we are testing
511  *
512  *      Check if we caused an IDE DMA interrupt. We may also have caused
513  *      SATA status interrupts, if so we clean them up and continue.
514  */
515  
516 static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
517 {
518         ide_hwif_t *hwif        = HWIF(drive);
519         unsigned long base      = (unsigned long)hwif->hwif_data;
520         unsigned long addr      = siimage_selreg(hwif, 0x1);
521
522         if (SATA_ERROR_REG) {
523                 u32 ext_stat = hwif->INL(base + 0x10);
524                 u8 watchdog = 0;
525                 if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
526                         u32 sata_error = hwif->INL(SATA_ERROR_REG);
527                         hwif->OUTL(sata_error, SATA_ERROR_REG);
528                         watchdog = (sata_error & 0x00680000) ? 1 : 0;
529 #if 1
530                         printk(KERN_WARNING "%s: sata_error = 0x%08x, "
531                                 "watchdog = %d, %s\n",
532                                 drive->name, sata_error, watchdog,
533                                 __FUNCTION__);
534 #endif
535
536                 } else {
537                         watchdog = (ext_stat & 0x8000) ? 1 : 0;
538                 }
539                 ext_stat >>= 16;
540
541                 if (!(ext_stat & 0x0404) && !watchdog)
542                         return 0;
543         }
544
545         /* return 1 if INTR asserted */
546         if ((hwif->INB(hwif->dma_status) & 0x04) == 0x04)
547                 return 1;
548
549         /* return 1 if Device INTR asserted */
550         if ((hwif->INB(addr) & 8) == 8)
551                 return 0;       //return 1;
552
553         return 0;
554 }
555
556 static int siimage_mmio_ide_dma_verbose (ide_drive_t *drive)
557 {
558         int temp = __ide_dma_verbose(drive);
559         return temp;
560 }
561
562 /**
563  *      siimage_busproc         -       bus isolation ioctl
564  *      @drive: drive to isolate/restore
565  *      @state: bus state to set
566  *
567  *      Used by the SII3112 to handle bus isolation. As this is a 
568  *      SATA controller the work required is quite limited, we 
569  *      just have to clean up the statistics
570  */
571  
572 static int siimage_busproc (ide_drive_t * drive, int state)
573 {
574         ide_hwif_t *hwif        = HWIF(drive);
575         u32 stat_config         = 0;
576         unsigned long addr      = siimage_selreg(hwif, 0);
577
578         if (hwif->mmio) {
579                 stat_config = hwif->INL(addr);
580         } else
581                 pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
582
583         switch (state) {
584                 case BUSSTATE_ON:
585                         hwif->drives[0].failures = 0;
586                         hwif->drives[1].failures = 0;
587                         break;
588                 case BUSSTATE_OFF:
589                         hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
590                         hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
591                         break;
592                 case BUSSTATE_TRISTATE:
593                         hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
594                         hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
595                         break;
596                 default:
597                         return -EINVAL;
598         }
599         hwif->bus_state = state;
600         return 0;
601 }
602
603 /**
604  *      siimage_reset_poll      -       wait for sata reset
605  *      @drive: drive we are resetting
606  *
607  *      Poll the SATA phy and see whether it has come back from the dead
608  *      yet.
609  */
610  
611 static int siimage_reset_poll (ide_drive_t *drive)
612 {
613         if (SATA_STATUS_REG) {
614                 ide_hwif_t *hwif        = HWIF(drive);
615
616                 if ((hwif->INL(SATA_STATUS_REG) & 0x03) != 0x03) {
617                         printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
618                                 hwif->name, hwif->INL(SATA_STATUS_REG));
619                         HWGROUP(drive)->poll_timeout = 0;
620                         return ide_started;
621                 }
622                 return 0;
623         } else {
624                 return 0;
625         }
626 }
627
628 /**
629  *      siimage_pre_reset       -       reset hook
630  *      @drive: IDE device being reset
631  *
632  *      For the SATA devices we need to handle recalibration/geometry
633  *      differently
634  */
635  
636 static void siimage_pre_reset (ide_drive_t *drive)
637 {
638         if (drive->media != ide_disk)
639                 return;
640
641         if (is_sata(HWIF(drive)))
642         {
643                 drive->special.b.set_geometry = 0;
644                 drive->special.b.recalibrate = 0;
645         }
646 }
647
648 /**
649  *      siimage_reset   -       reset a device on an siimage controller
650  *      @drive: drive to reset
651  *
652  *      Perform a controller level reset fo the device. For
653  *      SATA we must also check the PHY.
654  */
655  
656 static void siimage_reset (ide_drive_t *drive)
657 {
658         ide_hwif_t *hwif        = HWIF(drive);
659         u8 reset                = 0;
660         unsigned long addr      = siimage_selreg(hwif, 0);
661
662         if (hwif->mmio) {
663                 reset = hwif->INB(addr);
664                 hwif->OUTB((reset|0x03), addr);
665                 /* FIXME:posting */
666                 udelay(25);
667                 hwif->OUTB(reset, addr);
668                 (void) hwif->INB(addr);
669         } else {
670                 pci_read_config_byte(hwif->pci_dev, addr, &reset);
671                 pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
672                 udelay(25);
673                 pci_write_config_byte(hwif->pci_dev, addr, reset);
674                 pci_read_config_byte(hwif->pci_dev, addr, &reset);
675         }
676
677         if (SATA_STATUS_REG) {
678                 u32 sata_stat = hwif->INL(SATA_STATUS_REG);
679                 printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
680                         hwif->name, sata_stat, __FUNCTION__);
681                 if (!(sata_stat)) {
682                         printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
683                                 hwif->name, sata_stat);
684                         drive->failures++;
685                 }
686         }
687
688 }
689
690 /**
691  *      proc_reports_siimage            -       add siimage controller to proc
692  *      @dev: PCI device
693  *      @clocking: SCSC value
694  *      @name: controller name
695  *
696  *      Report the clocking mode of the controller and add it to
697  *      the /proc interface layer
698  */
699  
700 static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
701 {
702         if (!pdev_is_sata(dev)) {
703                 printk(KERN_INFO "%s: BASE CLOCK ", name);
704                 clocking &= 0x03;
705                 switch (clocking) {
706                         case 0x03: printk("DISABLED!\n"); break;
707                         case 0x02: printk("== 2X PCI\n"); break;
708                         case 0x01: printk("== 133\n"); break;
709                         case 0x00: printk("== 100\n"); break;
710                 }
711         }
712 }
713
714 /**
715  *      setup_mmio_siimage      -       switch an SI controller into MMIO
716  *      @dev: PCI device we are configuring
717  *      @name: device name
718  *
719  *      Attempt to put the device into mmio mode. There are some slight
720  *      complications here with certain systems where the mmio bar isnt
721  *      mapped so we have to be sure we can fall back to I/O.
722  */
723  
724 static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
725 {
726         unsigned long bar5      = pci_resource_start(dev, 5);
727         unsigned long barsize   = pci_resource_len(dev, 5);
728         u8 tmpbyte      = 0;
729         unsigned long addr;
730         void *ioaddr;
731
732         /*
733          *      Drop back to PIO if we can't map the mmio. Some
734          *      systems seem to get terminally confused in the PCI
735          *      spaces.
736          */
737          
738         if(!request_mem_region(bar5, barsize, name))
739         {
740                 printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
741                 return 0;
742         }
743                 
744         ioaddr = ioremap(bar5, barsize);
745
746         if (ioaddr == NULL)
747         {
748                 release_mem_region(bar5, barsize);
749                 return 0;
750         }
751
752         pci_set_master(dev);
753         pci_set_drvdata(dev, ioaddr);
754         addr = (unsigned long) ioaddr;
755
756         if (pdev_is_sata(dev)) {
757                 writel(0, addr + 0x148);
758                 writel(0, addr + 0x1C8);
759         }
760
761         writeb(0, addr + 0xB4);
762         writeb(0, addr + 0xF4);
763         tmpbyte = readb(addr + 0x4A);
764
765         switch(tmpbyte & 0x30) {
766                 case 0x00:
767                         /* In 100 MHz clocking, try and switch to 133 */
768                         writeb(tmpbyte|0x10, addr + 0x4A);
769                         break;
770                 case 0x10:
771                         /* On 133Mhz clocking */
772                         break;
773                 case 0x20:
774                         /* On PCIx2 clocking */
775                         break;
776                 case 0x30:
777                         /* Clocking is disabled */
778                         /* 133 clock attempt to force it on */
779                         writeb(tmpbyte & ~0x20, addr + 0x4A);
780                         break;
781         }
782         
783         writeb(      0x72, addr + 0xA1);
784         writew(    0x328A, addr + 0xA2);
785         writel(0x62DD62DD, addr + 0xA4);
786         writel(0x43924392, addr + 0xA8);
787         writel(0x40094009, addr + 0xAC);
788         writeb(      0x72, addr + 0xE1);
789         writew(    0x328A, addr + 0xE2);
790         writel(0x62DD62DD, addr + 0xE4);
791         writel(0x43924392, addr + 0xE8);
792         writel(0x40094009, addr + 0xEC);
793
794         if (pdev_is_sata(dev)) {
795                 writel(0xFFFF0000, addr + 0x108);
796                 writel(0xFFFF0000, addr + 0x188);
797                 writel(0x00680000, addr + 0x148);
798                 writel(0x00680000, addr + 0x1C8);
799         }
800
801         tmpbyte = readb(addr + 0x4A);
802
803         proc_reports_siimage(dev, (tmpbyte>>4), name);
804         return 1;
805 }
806
807 /**
808  *      init_chipset_siimage    -       set up an SI device
809  *      @dev: PCI device
810  *      @name: device name
811  *
812  *      Perform the initial PCI set up for this device. Attempt to switch
813  *      to 133MHz clocking if the system isn't already set up to do it.
814  */
815
816 static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
817 {
818         u32 class_rev   = 0;
819         u8 tmpbyte      = 0;
820         u8 BA5_EN       = 0;
821
822         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
823         class_rev &= 0xff;
824         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255); 
825
826         pci_read_config_byte(dev, 0x8A, &BA5_EN);
827         if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
828                 if (setup_mmio_siimage(dev, name)) {
829                         return 0;
830                 }
831         }
832
833         pci_write_config_byte(dev, 0x80, 0x00);
834         pci_write_config_byte(dev, 0x84, 0x00);
835         pci_read_config_byte(dev, 0x8A, &tmpbyte);
836         switch(tmpbyte & 0x30) {
837                 case 0x00:
838                         /* 133 clock attempt to force it on */
839                         pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
840                 case 0x30:
841                         /* if clocking is disabled */
842                         /* 133 clock attempt to force it on */
843                         pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
844                 case 0x10:
845                         /* 133 already */
846                         break;
847                 case 0x20:
848                         /* BIOS set PCI x2 clocking */
849                         break;
850         }
851
852         pci_read_config_byte(dev,   0x8A, &tmpbyte);
853
854         pci_write_config_byte(dev,  0xA1, 0x72);
855         pci_write_config_word(dev,  0xA2, 0x328A);
856         pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
857         pci_write_config_dword(dev, 0xA8, 0x43924392);
858         pci_write_config_dword(dev, 0xAC, 0x40094009);
859         pci_write_config_byte(dev,  0xB1, 0x72);
860         pci_write_config_word(dev,  0xB2, 0x328A);
861         pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
862         pci_write_config_dword(dev, 0xB8, 0x43924392);
863         pci_write_config_dword(dev, 0xBC, 0x40094009);
864
865         proc_reports_siimage(dev, (tmpbyte>>4), name);
866         return 0;
867 }
868
869 /**
870  *      init_mmio_iops_siimage  -       set up the iops for MMIO
871  *      @hwif: interface to set up
872  *
873  *      The basic setup here is fairly simple, we can use standard MMIO
874  *      operations. However we do have to set the taskfile register offsets
875  *      by hand as there isnt a standard defined layout for them this
876  *      time.
877  *
878  *      The hardware supports buffered taskfiles and also some rather nice
879  *      extended PRD tables. Unfortunately right now we don't.
880  */
881
882 static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
883 {
884         struct pci_dev *dev     = hwif->pci_dev;
885         void *addr              = pci_get_drvdata(dev);
886         u8 ch                   = hwif->channel;
887         hw_regs_t               hw;
888         unsigned long           base;
889
890         /*
891          *      Fill in the basic HWIF bits
892          */
893
894         default_hwif_mmiops(hwif);
895         hwif->hwif_data                 = addr;
896
897         /*
898          *      Now set up the hw. We have to do this ourselves as
899          *      the MMIO layout isnt the same as the the standard port
900          *      based I/O
901          */
902          
903         memset(&hw, 0, sizeof(hw_regs_t));
904         hw.priv                         = addr;
905
906         base                            = (unsigned long)addr;
907         if(ch)
908                 base += 0xC0;
909         else
910                 base += 0x80;
911
912         /*
913          *      The buffered task file doesn't have status/control
914          *      so we can't currently use it sanely since we want to
915          *      use LBA48 mode.
916          */     
917 //      base += 0x10;
918 //      hwif->no_lba48 = 1;
919
920         hw.io_ports[IDE_DATA_OFFSET]    = base;
921         hw.io_ports[IDE_ERROR_OFFSET]   = base + 1;
922         hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
923         hw.io_ports[IDE_SECTOR_OFFSET]  = base + 3;
924         hw.io_ports[IDE_LCYL_OFFSET]    = base + 4;
925         hw.io_ports[IDE_HCYL_OFFSET]    = base + 5;
926         hw.io_ports[IDE_SELECT_OFFSET]  = base + 6;
927         hw.io_ports[IDE_STATUS_OFFSET]  = base + 7;
928         hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
929
930         hw.io_ports[IDE_IRQ_OFFSET]     = 0;
931
932         if (pdev_is_sata(dev)) {
933                 base = (unsigned long) addr;
934                 if(ch)
935                         base += 0x80;
936                 hw.sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
937                 hw.sata_scr[SATA_ERROR_OFFSET]  = base + 0x108;
938                 hw.sata_scr[SATA_CONTROL_OFFSET]= base + 0x100;
939                 hw.sata_misc[SATA_MISC_OFFSET]  = base + 0x140;
940                 hw.sata_misc[SATA_PHY_OFFSET]   = base + 0x144;
941                 hw.sata_misc[SATA_IEN_OFFSET]   = base + 0x148;
942         }
943
944         hw.irq                          = hwif->pci_dev->irq;
945
946         memcpy(&hwif->hw, &hw, sizeof(hw));
947         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
948
949         if (is_sata(hwif)) {
950                 memcpy(hwif->sata_scr, hwif->hw.sata_scr, sizeof(hwif->hw.sata_scr));
951                 memcpy(hwif->sata_misc, hwif->hw.sata_misc, sizeof(hwif->hw.sata_misc));
952         }
953
954         hwif->irq                       = hw.irq;
955
956         base = (unsigned long) addr;
957
958 #ifdef SIIMAGE_LARGE_DMA
959 /* Watch the brackets - even Ken and Dennis get some language design wrong */
960         hwif->dma_base                  = base + (ch ? 0x18 : 0x10);
961         hwif->dma_base2                 = base + (ch ? 0x08 : 0x00);
962         hwif->dma_prdtable              = hwif->dma_base2 + 4;
963 #else /* ! SIIMAGE_LARGE_DMA */
964         hwif->dma_base                  = base + (ch ? 0x08 : 0x00);
965         hwif->dma_base2                 = base + (ch ? 0x18 : 0x10);
966 #endif /* SIIMAGE_LARGE_DMA */
967         hwif->mmio                      = 2;
968 }
969
970 static int is_dev_seagate_sata(ide_drive_t *drive)
971 {
972         const char *s = &drive->id->model[0];
973         unsigned len;
974
975         if (!drive->present)
976                 return 0;
977
978         len = strnlen(s, sizeof(drive->id->model));
979
980         if ((len > 4) && (!memcmp(s, "ST", 2))) {
981                 if ((!memcmp(s + len - 2, "AS", 2)) ||
982                     (!memcmp(s + len - 3, "ASL", 3))) {
983                         printk(KERN_INFO "%s: applying pessimistic Seagate "
984                                          "errata fix\n", drive->name);
985                         return 1;
986                 }
987         }
988         return 0;
989 }
990
991 /**
992  *      init_iops_siimage       -       set up iops
993  *      @hwif: interface to set up
994  *
995  *      Do the basic setup for the SIIMAGE hardware interface
996  *      and then do the MMIO setup if we can. This is the first
997  *      look in we get for setting up the hwif so that we
998  *      can get the iops right before using them.
999  */
1000
1001 static void __devinit init_iops_siimage(ide_hwif_t *hwif)
1002 {
1003         struct pci_dev *dev     = hwif->pci_dev;
1004         u32 class_rev           = 0;
1005
1006         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1007         class_rev &= 0xff;
1008         
1009         hwif->hwif_data = 0;
1010
1011         hwif->rqsize = 128;
1012         if (is_sata(hwif) && is_dev_seagate_sata(&hwif->drives[0]))
1013                 hwif->rqsize = 15;
1014
1015         if (pci_get_drvdata(dev) == NULL)
1016                 return;
1017         init_mmio_iops_siimage(hwif);
1018 }
1019
1020 /**
1021  *      ata66_siimage   -       check for 80 pin cable
1022  *      @hwif: interface to check
1023  *
1024  *      Check for the presence of an ATA66 capable cable on the
1025  *      interface.
1026  */
1027
1028 static unsigned int __devinit ata66_siimage(ide_hwif_t *hwif)
1029 {
1030         unsigned long addr = siimage_selreg(hwif, 0);
1031         if (pci_get_drvdata(hwif->pci_dev) == NULL) {
1032                 u8 ata66 = 0;
1033                 pci_read_config_byte(hwif->pci_dev, addr, &ata66);
1034                 return (ata66 & 0x01) ? 1 : 0;
1035         }
1036
1037         return (hwif->INB(addr) & 0x01) ? 1 : 0;
1038 }
1039
1040 /**
1041  *      init_hwif_siimage       -       set up hwif structs
1042  *      @hwif: interface to set up
1043  *
1044  *      We do the basic set up of the interface structure. The SIIMAGE
1045  *      requires several custom handlers so we override the default
1046  *      ide DMA handlers appropriately
1047  */
1048
1049 static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
1050 {
1051         hwif->autodma = 0;
1052         
1053         hwif->resetproc = &siimage_reset;
1054         hwif->speedproc = &siimage_tune_chipset;
1055         hwif->tuneproc  = &siimage_tuneproc;
1056         hwif->reset_poll = &siimage_reset_poll;
1057         hwif->pre_reset = &siimage_pre_reset;
1058
1059         if(is_sata(hwif))
1060                 hwif->busproc   = &siimage_busproc;
1061
1062         if (!hwif->dma_base) {
1063                 hwif->drives[0].autotune = 1;
1064                 hwif->drives[1].autotune = 1;
1065                 return;
1066         }
1067
1068         hwif->ultra_mask = 0x7f;
1069         hwif->mwdma_mask = 0x07;
1070         hwif->swdma_mask = 0x07;
1071
1072         if (!is_sata(hwif))
1073                 hwif->atapi_dma = 1;
1074
1075         hwif->ide_dma_check = &siimage_config_drive_for_dma;
1076         if (!(hwif->udma_four))
1077                 hwif->udma_four = ata66_siimage(hwif);
1078
1079         if (hwif->mmio) {
1080                 hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
1081                 hwif->ide_dma_verbose = &siimage_mmio_ide_dma_verbose;
1082         } else {
1083                 hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
1084         }
1085         
1086         /*
1087          *      The BIOS often doesn't set up DMA on this controller
1088          *      so we always do it.
1089          */
1090
1091         hwif->autodma = 1;
1092         hwif->drives[0].autodma = hwif->autodma;
1093         hwif->drives[1].autodma = hwif->autodma;
1094 }
1095
1096 #define DECLARE_SII_DEV(name_str)                       \
1097         {                                               \
1098                 .name           = name_str,             \
1099                 .init_chipset   = init_chipset_siimage, \
1100                 .init_iops      = init_iops_siimage,    \
1101                 .init_hwif      = init_hwif_siimage,    \
1102                 .channels       = 2,                    \
1103                 .autodma        = AUTODMA,              \
1104                 .bootable       = ON_BOARD,             \
1105         }
1106
1107 static ide_pci_device_t siimage_chipsets[] __devinitdata = {
1108         /* 0 */ DECLARE_SII_DEV("SiI680"),
1109         /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
1110         /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
1111 };
1112
1113 /**
1114  *      siimage_init_one        -       pci layer discovery entry
1115  *      @dev: PCI device
1116  *      @id: ident table entry
1117  *
1118  *      Called by the PCI code when it finds an SI680 or SI3112 controller.
1119  *      We then use the IDE PCI generic helper to do most of the work.
1120  */
1121  
1122 static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1123 {
1124         ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
1125         return 0;
1126 }
1127
1128 static struct pci_device_id siimage_pci_tbl[] = {
1129         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1130         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1131         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1132         { 0, },
1133 };
1134 MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
1135
1136 static struct pci_driver driver = {
1137         .name           = "SiI IDE",
1138         .id_table       = siimage_pci_tbl,
1139         .probe          = siimage_init_one,
1140 };
1141
1142 static int siimage_ide_init(void)
1143 {
1144         return ide_pci_register_driver(&driver);
1145 }
1146
1147 module_init(siimage_ide_init);
1148
1149 MODULE_AUTHOR("Andre Hedrick, Alan Cox");
1150 MODULE_DESCRIPTION("PCI driver module for SiI IDE");
1151 MODULE_LICENSE("GPL");