blob: c526c70d65a4c1d8d53ccdebc10d997d70a87515 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +02002 * linux/drivers/ide/pci/siimage.c Version 1.16 Jul 13 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
Sergei Shtylyov075cb652007-02-17 02:40:22 +01006 * Copyright (C) 2007 MontaVista Software, Inc.
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +02007 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * May be copied or modified under the terms of the GNU General Public License
10 *
Jeff Garzikbf4c7962005-11-18 22:55:47 +010011 * Documentation for CMD680:
12 * http://gkernel.sourceforge.net/specs/sii/sii-0680a-v1.31.pdf.bz2
13 *
14 * Documentation for SiI 3112:
15 * http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
16 *
17 * Errata and other documentation only available under NDA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 *
20 * FAQ Items:
21 * If you are using Marvell SATA-IDE adapters with Maxtor drives
22 * ensure the system is set up for ATA100/UDMA5 not UDMA6.
23 *
24 * If you are using WD drives with SATA bridges you must set the
25 * drive to "Single". "Master" will hang
26 *
27 * If you have strange problems with nVidia chipset systems please
28 * see the SI support documentation and update your system BIOS
29 * if neccessary
Alan Cox8693d3e2007-03-03 17:48:54 +010030 *
31 * The Dell DRAC4 has some interesting features including effectively hot
32 * unplugging/replugging the virtual CD interface when the DRAC is reset.
33 * This often causes drivers/ide/siimage to panic but is ok with the rather
34 * smarter code in libata.
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +020035 *
36 * TODO:
37 * - IORDY fixes
38 * - VDMA support
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/module.h>
43#include <linux/pci.h>
44#include <linux/delay.h>
45#include <linux/hdreg.h>
46#include <linux/ide.h>
47#include <linux/init.h>
48
49#include <asm/io.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/**
52 * pdev_is_sata - check if device is SATA
53 * @pdev: PCI device to check
54 *
55 * Returns true if this is a SATA controller
56 */
57
58static int pdev_is_sata(struct pci_dev *pdev)
59{
60 switch(pdev->device)
61 {
62 case PCI_DEVICE_ID_SII_3112:
63 case PCI_DEVICE_ID_SII_1210SA:
64 return 1;
65 case PCI_DEVICE_ID_SII_680:
66 return 0;
67 }
68 BUG();
69 return 0;
70}
71
72/**
73 * is_sata - check if hwif is SATA
74 * @hwif: interface to check
75 *
76 * Returns true if this is a SATA controller
77 */
78
79static inline int is_sata(ide_hwif_t *hwif)
80{
81 return pdev_is_sata(hwif->pci_dev);
82}
83
84/**
85 * siimage_selreg - return register base
86 * @hwif: interface
87 * @r: config offset
88 *
89 * Turn a config register offset into the right address in either
90 * PCI space or MMIO space to access the control register in question
91 * Thankfully this is a configuration operation so isnt performance
92 * criticial.
93 */
94
95static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
96{
97 unsigned long base = (unsigned long)hwif->hwif_data;
98 base += 0xA0 + r;
99 if(hwif->mmio)
100 base += (hwif->channel << 6);
101 else
102 base += (hwif->channel << 4);
103 return base;
104}
105
106/**
107 * siimage_seldev - return register base
108 * @hwif: interface
109 * @r: config offset
110 *
111 * Turn a config register offset into the right address in either
112 * PCI space or MMIO space to access the control register in question
113 * including accounting for the unit shift.
114 */
115
116static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
117{
118 ide_hwif_t *hwif = HWIF(drive);
119 unsigned long base = (unsigned long)hwif->hwif_data;
120 base += 0xA0 + r;
121 if(hwif->mmio)
122 base += (hwif->channel << 6);
123 else
124 base += (hwif->channel << 4);
125 base |= drive->select.b.unit << drive->select.b.unit;
126 return base;
127}
128
129/**
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200130 * sil_udma_filter - compute UDMA mask
131 * @drive: IDE device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200133 * Compute the available UDMA speeds for the device on the interface.
134 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * For the CMD680 this depends on the clocking mode (scsc), for the
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200136 * SI3112 SATA controller life is a bit simpler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200138
139static u8 sil_udma_filter(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200141 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long base = (unsigned long) hwif->hwif_data;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200143 u8 mask = 0, scsc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (hwif->mmio)
146 scsc = hwif->INB(base + 0x4A);
147 else
148 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
149
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200150 if (is_sata(hwif)) {
151 mask = strstr(drive->id->model, "Maxtor") ? 0x3f : 0x7f;
152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if ((scsc & 0x30) == 0x10) /* 133 */
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200156 mask = 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 else if ((scsc & 0x30) == 0x20) /* 2xPCI */
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200158 mask = 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 else if ((scsc & 0x30) == 0x00) /* 100 */
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200160 mask = 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else /* Disabled ? */
162 BUG();
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200163out:
164 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/**
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200168 * sil_tune_pio - tune a drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * @drive: drive to tune
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200170 * @pio: the desired PIO mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Load the timing settings for this device mode into the
173 * controller. If we are in PIO mode 3 or 4 turn on IORDY
174 * monitoring (bit 9). The TF timing is bits 31:16
175 */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200176
177static void sil_tune_pio(ide_drive_t *drive, u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200179 const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
180 const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200183 ide_drive_t *pair = &hwif->drives[drive->dn ^ 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u32 speedt = 0;
185 u16 speedp = 0;
186 unsigned long addr = siimage_seldev(drive, 0x04);
187 unsigned long tfaddr = siimage_selreg(hwif, 0x02);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200188 unsigned long base = (unsigned long)hwif->hwif_data;
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200189 u8 tf_pio = pio;
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200190 u8 addr_mask = hwif->channel ? (hwif->mmio ? 0xF4 : 0x84)
191 : (hwif->mmio ? 0xB4 : 0x80);
192 u8 mode = 0;
193 u8 unit = drive->select.b.unit;
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200194
195 /* trim *taskfile* PIO to the slowest of the master/slave */
196 if (pair->present) {
Bartlomiej Zolnierkiewicz21347582007-07-20 01:11:58 +0200197 u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200198
199 if (pair_pio < tf_pio)
200 tf_pio = pair_pio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100202
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200203 /* cheat for now and use the docs */
204 speedp = data_speed[pio];
205 speedt = tf_speed[tf_pio];
206
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100207 if (hwif->mmio) {
208 hwif->OUTW(speedp, addr);
209 hwif->OUTW(speedt, tfaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Now set up IORDY */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200211 if (pio > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
213 else
214 hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200215
216 mode = hwif->INB(base + addr_mask);
217 mode &= ~(unit ? 0x30 : 0x03);
218 mode |= (unit ? 0x10 : 0x01);
219 hwif->OUTB(mode, base + addr_mask);
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100220 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 pci_write_config_word(hwif->pci_dev, addr, speedp);
222 pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
223 pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
224 speedp &= ~0x200;
225 /* Set IORDY for mode 3 or 4 */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200226 if (pio > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 speedp |= 0x200;
228 pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200229
230 pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
231 mode &= ~(unit ? 0x30 : 0x03);
232 mode |= (unit ? 0x10 : 0x01);
233 pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235}
236
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200237static void sil_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200239 sil_tune_pio(drive, pio);
240 (void)ide_config_drive_speed(drive, XFER_PIO_0 + pio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
244 * siimage_tune_chipset - set controller timings
245 * @drive: Drive to set up
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200246 * @speed: speed we want to achieve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200248 * Tune the SII chipset for the desired mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200250
251static int siimage_tune_chipset(ide_drive_t *drive, const u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
254 u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
255 u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
256
257 ide_hwif_t *hwif = HWIF(drive);
258 u16 ultra = 0, multi = 0;
259 u8 mode = 0, unit = drive->select.b.unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned long base = (unsigned long)hwif->hwif_data;
261 u8 scsc = 0, addr_mask = ((hwif->channel) ?
262 ((hwif->mmio) ? 0xF4 : 0x84) :
263 ((hwif->mmio) ? 0xB4 : 0x80));
264
265 unsigned long ma = siimage_seldev(drive, 0x08);
266 unsigned long ua = siimage_seldev(drive, 0x0C);
267
268 if (hwif->mmio) {
269 scsc = hwif->INB(base + 0x4A);
270 mode = hwif->INB(base + addr_mask);
271 multi = hwif->INW(ma);
272 ultra = hwif->INW(ua);
273 } else {
274 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
275 pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
276 pci_read_config_word(hwif->pci_dev, ma, &multi);
277 pci_read_config_word(hwif->pci_dev, ua, &ultra);
278 }
279
280 mode &= ~((unit) ? 0x30 : 0x03);
281 ultra &= ~0x3F;
282 scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
283
284 scsc = is_sata(hwif) ? 1 : scsc;
285
286 switch(speed) {
287 case XFER_PIO_4:
288 case XFER_PIO_3:
289 case XFER_PIO_2:
290 case XFER_PIO_1:
291 case XFER_PIO_0:
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200292 sil_tune_pio(drive, speed - XFER_PIO_0);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200293 return ide_config_drive_speed(drive, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case XFER_MW_DMA_2:
295 case XFER_MW_DMA_1:
296 case XFER_MW_DMA_0:
297 multi = dma[speed - XFER_MW_DMA_0];
298 mode |= ((unit) ? 0x20 : 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300 case XFER_UDMA_6:
301 case XFER_UDMA_5:
302 case XFER_UDMA_4:
303 case XFER_UDMA_3:
304 case XFER_UDMA_2:
305 case XFER_UDMA_1:
306 case XFER_UDMA_0:
307 multi = dma[2];
308 ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
309 (ultra5[speed - XFER_UDMA_0]));
310 mode |= ((unit) ? 0x30 : 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 default:
313 return 1;
314 }
315
316 if (hwif->mmio) {
317 hwif->OUTB(mode, base + addr_mask);
318 hwif->OUTW(multi, ma);
319 hwif->OUTW(ultra, ua);
320 } else {
321 pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
322 pci_write_config_word(hwif->pci_dev, ma, multi);
323 pci_write_config_word(hwif->pci_dev, ua, ultra);
324 }
325 return (ide_config_drive_speed(drive, speed));
326}
327
328/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * siimage_configure_drive_for_dma - set up for DMA transfers
330 * @drive: drive we are going to set up
331 *
332 * Set up the drive for DMA, tune the controller and drive as
333 * required. If the drive isn't suitable for DMA or we hit
334 * other problems then we will drop down to PIO and set up
335 * PIO appropriately
336 */
337
338static int siimage_config_drive_for_dma (ide_drive_t *drive)
339{
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200340 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Bartlomiej Zolnierkiewiczd8f44692007-02-17 02:40:25 +0100343 if (ide_use_fast_pio(drive))
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200344 ide_set_max_pio(drive);
Bartlomiej Zolnierkiewiczd8f44692007-02-17 02:40:25 +0100345
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100346 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/* returns 1 if dma irq issued, 0 otherwise */
350static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
351{
352 ide_hwif_t *hwif = HWIF(drive);
353 u8 dma_altstat = 0;
354 unsigned long addr = siimage_selreg(hwif, 1);
355
356 /* return 1 if INTR asserted */
357 if ((hwif->INB(hwif->dma_status) & 4) == 4)
358 return 1;
359
360 /* return 1 if Device INTR asserted */
361 pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
362 if (dma_altstat & 8)
363 return 0; //return 1;
364 return 0;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * siimage_mmio_ide_dma_test_irq - check we caused an IRQ
369 * @drive: drive we are testing
370 *
371 * Check if we caused an IDE DMA interrupt. We may also have caused
372 * SATA status interrupts, if so we clean them up and continue.
373 */
374
375static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
376{
377 ide_hwif_t *hwif = HWIF(drive);
378 unsigned long base = (unsigned long)hwif->hwif_data;
379 unsigned long addr = siimage_selreg(hwif, 0x1);
380
381 if (SATA_ERROR_REG) {
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100382 u32 ext_stat = readl((void __iomem *)(base + 0x10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 u8 watchdog = 0;
384 if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100385 u32 sata_error = readl((void __iomem *)SATA_ERROR_REG);
386 writel(sata_error, (void __iomem *)SATA_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 watchdog = (sata_error & 0x00680000) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 printk(KERN_WARNING "%s: sata_error = 0x%08x, "
389 "watchdog = %d, %s\n",
390 drive->name, sata_error, watchdog,
391 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 } else {
394 watchdog = (ext_stat & 0x8000) ? 1 : 0;
395 }
396 ext_stat >>= 16;
397
398 if (!(ext_stat & 0x0404) && !watchdog)
399 return 0;
400 }
401
402 /* return 1 if INTR asserted */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100403 if ((readb((void __iomem *)hwif->dma_status) & 0x04) == 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 1;
405
406 /* return 1 if Device INTR asserted */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100407 if ((readb((void __iomem *)addr) & 8) == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0; //return 1;
409
410 return 0;
411}
412
413/**
414 * siimage_busproc - bus isolation ioctl
415 * @drive: drive to isolate/restore
416 * @state: bus state to set
417 *
418 * Used by the SII3112 to handle bus isolation. As this is a
419 * SATA controller the work required is quite limited, we
420 * just have to clean up the statistics
421 */
422
423static int siimage_busproc (ide_drive_t * drive, int state)
424{
425 ide_hwif_t *hwif = HWIF(drive);
426 u32 stat_config = 0;
427 unsigned long addr = siimage_selreg(hwif, 0);
428
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100429 if (hwif->mmio)
430 stat_config = readl((void __iomem *)addr);
431 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
433
434 switch (state) {
435 case BUSSTATE_ON:
436 hwif->drives[0].failures = 0;
437 hwif->drives[1].failures = 0;
438 break;
439 case BUSSTATE_OFF:
440 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
441 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
442 break;
443 case BUSSTATE_TRISTATE:
444 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
445 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
446 break;
447 default:
448 return -EINVAL;
449 }
450 hwif->bus_state = state;
451 return 0;
452}
453
454/**
455 * siimage_reset_poll - wait for sata reset
456 * @drive: drive we are resetting
457 *
458 * Poll the SATA phy and see whether it has come back from the dead
459 * yet.
460 */
461
462static int siimage_reset_poll (ide_drive_t *drive)
463{
464 if (SATA_STATUS_REG) {
465 ide_hwif_t *hwif = HWIF(drive);
466
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100467 /* SATA_STATUS_REG is valid only when in MMIO mode */
468 if ((readl((void __iomem *)SATA_STATUS_REG) & 0x03) != 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100470 hwif->name, readl((void __iomem *)SATA_STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 HWGROUP(drive)->polling = 0;
472 return ide_started;
473 }
474 return 0;
475 } else {
476 return 0;
477 }
478}
479
480/**
481 * siimage_pre_reset - reset hook
482 * @drive: IDE device being reset
483 *
484 * For the SATA devices we need to handle recalibration/geometry
485 * differently
486 */
487
488static void siimage_pre_reset (ide_drive_t *drive)
489{
490 if (drive->media != ide_disk)
491 return;
492
493 if (is_sata(HWIF(drive)))
494 {
495 drive->special.b.set_geometry = 0;
496 drive->special.b.recalibrate = 0;
497 }
498}
499
500/**
501 * siimage_reset - reset a device on an siimage controller
502 * @drive: drive to reset
503 *
504 * Perform a controller level reset fo the device. For
505 * SATA we must also check the PHY.
506 */
507
508static void siimage_reset (ide_drive_t *drive)
509{
510 ide_hwif_t *hwif = HWIF(drive);
511 u8 reset = 0;
512 unsigned long addr = siimage_selreg(hwif, 0);
513
514 if (hwif->mmio) {
515 reset = hwif->INB(addr);
516 hwif->OUTB((reset|0x03), addr);
517 /* FIXME:posting */
518 udelay(25);
519 hwif->OUTB(reset, addr);
520 (void) hwif->INB(addr);
521 } else {
522 pci_read_config_byte(hwif->pci_dev, addr, &reset);
523 pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
524 udelay(25);
525 pci_write_config_byte(hwif->pci_dev, addr, reset);
526 pci_read_config_byte(hwif->pci_dev, addr, &reset);
527 }
528
529 if (SATA_STATUS_REG) {
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100530 /* SATA_STATUS_REG is valid only when in MMIO mode */
531 u32 sata_stat = readl((void __iomem *)SATA_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
533 hwif->name, sata_stat, __FUNCTION__);
534 if (!(sata_stat)) {
535 printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
536 hwif->name, sata_stat);
537 drive->failures++;
538 }
539 }
540
541}
542
543/**
544 * proc_reports_siimage - add siimage controller to proc
545 * @dev: PCI device
546 * @clocking: SCSC value
547 * @name: controller name
548 *
549 * Report the clocking mode of the controller and add it to
550 * the /proc interface layer
551 */
552
553static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
554{
555 if (!pdev_is_sata(dev)) {
556 printk(KERN_INFO "%s: BASE CLOCK ", name);
557 clocking &= 0x03;
558 switch (clocking) {
559 case 0x03: printk("DISABLED!\n"); break;
560 case 0x02: printk("== 2X PCI\n"); break;
561 case 0x01: printk("== 133\n"); break;
562 case 0x00: printk("== 100\n"); break;
563 }
564 }
565}
566
567/**
568 * setup_mmio_siimage - switch an SI controller into MMIO
569 * @dev: PCI device we are configuring
570 * @name: device name
571 *
572 * Attempt to put the device into mmio mode. There are some slight
573 * complications here with certain systems where the mmio bar isnt
574 * mapped so we have to be sure we can fall back to I/O.
575 */
576
577static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
578{
579 unsigned long bar5 = pci_resource_start(dev, 5);
580 unsigned long barsize = pci_resource_len(dev, 5);
581 u8 tmpbyte = 0;
582 void __iomem *ioaddr;
John W. Linvilled868dd12005-11-10 00:19:14 +0100583 u32 tmp, irq_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /*
586 * Drop back to PIO if we can't map the mmio. Some
587 * systems seem to get terminally confused in the PCI
588 * spaces.
589 */
590
591 if(!request_mem_region(bar5, barsize, name))
592 {
593 printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
594 return 0;
595 }
596
597 ioaddr = ioremap(bar5, barsize);
598
599 if (ioaddr == NULL)
600 {
601 release_mem_region(bar5, barsize);
602 return 0;
603 }
604
605 pci_set_master(dev);
606 pci_set_drvdata(dev, (void *) ioaddr);
607
608 if (pdev_is_sata(dev)) {
John W. Linvilled868dd12005-11-10 00:19:14 +0100609 /* make sure IDE0/1 interrupts are not masked */
610 irq_mask = (1 << 22) | (1 << 23);
611 tmp = readl(ioaddr + 0x48);
612 if (tmp & irq_mask) {
613 tmp &= ~irq_mask;
614 writel(tmp, ioaddr + 0x48);
615 readl(ioaddr + 0x48); /* flush */
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 writel(0, ioaddr + 0x148);
618 writel(0, ioaddr + 0x1C8);
619 }
620
621 writeb(0, ioaddr + 0xB4);
622 writeb(0, ioaddr + 0xF4);
623 tmpbyte = readb(ioaddr + 0x4A);
624
625 switch(tmpbyte & 0x30) {
626 case 0x00:
627 /* In 100 MHz clocking, try and switch to 133 */
628 writeb(tmpbyte|0x10, ioaddr + 0x4A);
629 break;
630 case 0x10:
631 /* On 133Mhz clocking */
632 break;
633 case 0x20:
634 /* On PCIx2 clocking */
635 break;
636 case 0x30:
637 /* Clocking is disabled */
638 /* 133 clock attempt to force it on */
639 writeb(tmpbyte & ~0x20, ioaddr + 0x4A);
640 break;
641 }
642
643 writeb( 0x72, ioaddr + 0xA1);
644 writew( 0x328A, ioaddr + 0xA2);
645 writel(0x62DD62DD, ioaddr + 0xA4);
646 writel(0x43924392, ioaddr + 0xA8);
647 writel(0x40094009, ioaddr + 0xAC);
648 writeb( 0x72, ioaddr + 0xE1);
649 writew( 0x328A, ioaddr + 0xE2);
650 writel(0x62DD62DD, ioaddr + 0xE4);
651 writel(0x43924392, ioaddr + 0xE8);
652 writel(0x40094009, ioaddr + 0xEC);
653
654 if (pdev_is_sata(dev)) {
655 writel(0xFFFF0000, ioaddr + 0x108);
656 writel(0xFFFF0000, ioaddr + 0x188);
657 writel(0x00680000, ioaddr + 0x148);
658 writel(0x00680000, ioaddr + 0x1C8);
659 }
660
661 tmpbyte = readb(ioaddr + 0x4A);
662
663 proc_reports_siimage(dev, (tmpbyte>>4), name);
664 return 1;
665}
666
667/**
668 * init_chipset_siimage - set up an SI device
669 * @dev: PCI device
670 * @name: device name
671 *
672 * Perform the initial PCI set up for this device. Attempt to switch
673 * to 133MHz clocking if the system isn't already set up to do it.
674 */
675
676static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
677{
678 u32 class_rev = 0;
679 u8 tmpbyte = 0;
680 u8 BA5_EN = 0;
681
682 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
683 class_rev &= 0xff;
684 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
685
686 pci_read_config_byte(dev, 0x8A, &BA5_EN);
687 if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
688 if (setup_mmio_siimage(dev, name)) {
689 return 0;
690 }
691 }
692
693 pci_write_config_byte(dev, 0x80, 0x00);
694 pci_write_config_byte(dev, 0x84, 0x00);
695 pci_read_config_byte(dev, 0x8A, &tmpbyte);
696 switch(tmpbyte & 0x30) {
697 case 0x00:
698 /* 133 clock attempt to force it on */
699 pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
700 case 0x30:
701 /* if clocking is disabled */
702 /* 133 clock attempt to force it on */
703 pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
704 case 0x10:
705 /* 133 already */
706 break;
707 case 0x20:
708 /* BIOS set PCI x2 clocking */
709 break;
710 }
711
712 pci_read_config_byte(dev, 0x8A, &tmpbyte);
713
714 pci_write_config_byte(dev, 0xA1, 0x72);
715 pci_write_config_word(dev, 0xA2, 0x328A);
716 pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
717 pci_write_config_dword(dev, 0xA8, 0x43924392);
718 pci_write_config_dword(dev, 0xAC, 0x40094009);
719 pci_write_config_byte(dev, 0xB1, 0x72);
720 pci_write_config_word(dev, 0xB2, 0x328A);
721 pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
722 pci_write_config_dword(dev, 0xB8, 0x43924392);
723 pci_write_config_dword(dev, 0xBC, 0x40094009);
724
725 proc_reports_siimage(dev, (tmpbyte>>4), name);
726 return 0;
727}
728
729/**
730 * init_mmio_iops_siimage - set up the iops for MMIO
731 * @hwif: interface to set up
732 *
733 * The basic setup here is fairly simple, we can use standard MMIO
734 * operations. However we do have to set the taskfile register offsets
735 * by hand as there isnt a standard defined layout for them this
736 * time.
737 *
738 * The hardware supports buffered taskfiles and also some rather nice
Alan Cox19c1ef52006-06-28 04:26:59 -0700739 * extended PRD tables. For better SI3112 support use the libata driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741
742static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
743{
744 struct pci_dev *dev = hwif->pci_dev;
745 void *addr = pci_get_drvdata(dev);
746 u8 ch = hwif->channel;
747 hw_regs_t hw;
748 unsigned long base;
749
750 /*
751 * Fill in the basic HWIF bits
752 */
753
754 default_hwif_mmiops(hwif);
755 hwif->hwif_data = addr;
756
757 /*
758 * Now set up the hw. We have to do this ourselves as
Michael Opdenacker59c51592007-05-09 08:57:56 +0200759 * the MMIO layout isnt the same as the standard port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * based I/O
761 */
762
763 memset(&hw, 0, sizeof(hw_regs_t));
764
765 base = (unsigned long)addr;
766 if (ch)
767 base += 0xC0;
768 else
769 base += 0x80;
770
771 /*
772 * The buffered task file doesn't have status/control
773 * so we can't currently use it sanely since we want to
774 * use LBA48 mode.
775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 hw.io_ports[IDE_DATA_OFFSET] = base;
777 hw.io_ports[IDE_ERROR_OFFSET] = base + 1;
778 hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
779 hw.io_ports[IDE_SECTOR_OFFSET] = base + 3;
780 hw.io_ports[IDE_LCYL_OFFSET] = base + 4;
781 hw.io_ports[IDE_HCYL_OFFSET] = base + 5;
782 hw.io_ports[IDE_SELECT_OFFSET] = base + 6;
783 hw.io_ports[IDE_STATUS_OFFSET] = base + 7;
784 hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
785
786 hw.io_ports[IDE_IRQ_OFFSET] = 0;
787
788 if (pdev_is_sata(dev)) {
789 base = (unsigned long)addr;
790 if (ch)
791 base += 0x80;
792 hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
793 hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
794 hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
795 hwif->sata_misc[SATA_MISC_OFFSET] = base + 0x140;
796 hwif->sata_misc[SATA_PHY_OFFSET] = base + 0x144;
797 hwif->sata_misc[SATA_IEN_OFFSET] = base + 0x148;
798 }
799
800 hw.irq = hwif->pci_dev->irq;
801
802 memcpy(&hwif->hw, &hw, sizeof(hw));
803 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
804
805 hwif->irq = hw.irq;
806
807 base = (unsigned long) addr;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 hwif->dma_base = base + (ch ? 0x08 : 0x00);
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100810
811 hwif->mmio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814static int is_dev_seagate_sata(ide_drive_t *drive)
815{
816 const char *s = &drive->id->model[0];
817 unsigned len;
818
819 if (!drive->present)
820 return 0;
821
822 len = strnlen(s, sizeof(drive->id->model));
823
824 if ((len > 4) && (!memcmp(s, "ST", 2))) {
825 if ((!memcmp(s + len - 2, "AS", 2)) ||
826 (!memcmp(s + len - 3, "ASL", 3))) {
827 printk(KERN_INFO "%s: applying pessimistic Seagate "
828 "errata fix\n", drive->name);
829 return 1;
830 }
831 }
832 return 0;
833}
834
835/**
836 * siimage_fixup - post probe fixups
837 * @hwif: interface to fix up
838 *
839 * Called after drive probe we use this to decide whether the
840 * Seagate fixup must be applied. This used to be in init_iops but
841 * that can occur before we know what drives are present.
842 */
843
844static void __devinit siimage_fixup(ide_hwif_t *hwif)
845{
846 /* Try and raise the rqsize */
847 if (!is_sata(hwif) || !is_dev_seagate_sata(&hwif->drives[0]))
848 hwif->rqsize = 128;
849}
850
851/**
852 * init_iops_siimage - set up iops
853 * @hwif: interface to set up
854 *
855 * Do the basic setup for the SIIMAGE hardware interface
856 * and then do the MMIO setup if we can. This is the first
857 * look in we get for setting up the hwif so that we
858 * can get the iops right before using them.
859 */
860
861static void __devinit init_iops_siimage(ide_hwif_t *hwif)
862{
863 struct pci_dev *dev = hwif->pci_dev;
864 u32 class_rev = 0;
865
866 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
867 class_rev &= 0xff;
868
869 hwif->hwif_data = NULL;
870
871 /* Pessimal until we finish probing */
872 hwif->rqsize = 15;
873
874 if (pci_get_drvdata(dev) == NULL)
875 return;
876 init_mmio_iops_siimage(hwif);
877}
878
879/**
880 * ata66_siimage - check for 80 pin cable
881 * @hwif: interface to check
882 *
883 * Check for the presence of an ATA66 capable cable on the
884 * interface.
885 */
886
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200887static u8 __devinit ata66_siimage(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 unsigned long addr = siimage_selreg(hwif, 0);
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200890 u8 ata66 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200892 if (pci_get_drvdata(hwif->pci_dev) == NULL)
893 pci_read_config_byte(hwif->pci_dev, addr, &ata66);
894 else
895 ata66 = hwif->INB(addr);
896
897 return (ata66 & 0x01) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/**
901 * init_hwif_siimage - set up hwif structs
902 * @hwif: interface to set up
903 *
904 * We do the basic set up of the interface structure. The SIIMAGE
905 * requires several custom handlers so we override the default
906 * ide DMA handlers appropriately
907 */
908
909static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
910{
911 hwif->autodma = 0;
912
913 hwif->resetproc = &siimage_reset;
914 hwif->speedproc = &siimage_tune_chipset;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200915 hwif->set_pio_mode = &sil_set_pio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 hwif->reset_poll = &siimage_reset_poll;
917 hwif->pre_reset = &siimage_pre_reset;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200918 hwif->udma_filter = &sil_udma_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Alan Cox19c1ef52006-06-28 04:26:59 -0700920 if(is_sata(hwif)) {
921 static int first = 1;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 hwif->busproc = &siimage_busproc;
924
Alan Cox19c1ef52006-06-28 04:26:59 -0700925 if (first) {
926 printk(KERN_INFO "siimage: For full SATA support you should use the libata sata_sil module.\n");
927 first = 0;
928 }
929 }
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200930
931 hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
932
933 if (hwif->dma_base == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 hwif->ultra_mask = 0x7f;
937 hwif->mwdma_mask = 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (!is_sata(hwif))
940 hwif->atapi_dma = 1;
941
942 hwif->ide_dma_check = &siimage_config_drive_for_dma;
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200943
944 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
945 hwif->cbl = ata66_siimage(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (hwif->mmio) {
948 hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
949 } else {
950 hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
951 }
952
953 /*
954 * The BIOS often doesn't set up DMA on this controller
955 * so we always do it.
956 */
957
958 hwif->autodma = 1;
959 hwif->drives[0].autodma = hwif->autodma;
960 hwif->drives[1].autodma = hwif->autodma;
961}
962
963#define DECLARE_SII_DEV(name_str) \
964 { \
965 .name = name_str, \
966 .init_chipset = init_chipset_siimage, \
967 .init_iops = init_iops_siimage, \
968 .init_hwif = init_hwif_siimage, \
969 .fixup = siimage_fixup, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .autodma = AUTODMA, \
971 .bootable = ON_BOARD, \
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200972 .pio_mask = ATA_PIO4, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
975static ide_pci_device_t siimage_chipsets[] __devinitdata = {
976 /* 0 */ DECLARE_SII_DEV("SiI680"),
977 /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
978 /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
979};
980
981/**
982 * siimage_init_one - pci layer discovery entry
983 * @dev: PCI device
984 * @id: ident table entry
985 *
986 * Called by the PCI code when it finds an SI680 or SI3112 controller.
987 * We then use the IDE PCI generic helper to do most of the work.
988 */
989
990static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
991{
992 return ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
993}
994
995static struct pci_device_id siimage_pci_tbl[] = {
Alan Cox28a2a3f2006-09-11 14:45:07 +0100996 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997#ifdef CONFIG_BLK_DEV_IDE_SATA
Alan Cox28a2a3f2006-09-11 14:45:07 +0100998 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
999 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#endif
1001 { 0, },
1002};
1003MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
1004
1005static struct pci_driver driver = {
1006 .name = "SiI_IDE",
1007 .id_table = siimage_pci_tbl,
1008 .probe = siimage_init_one,
1009};
1010
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +01001011static int __init siimage_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 return ide_pci_register_driver(&driver);
1014}
1015
1016module_init(siimage_ide_init);
1017
1018MODULE_AUTHOR("Andre Hedrick, Alan Cox");
1019MODULE_DESCRIPTION("PCI driver module for SiI IDE");
1020MODULE_LICENSE("GPL");