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