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