blob: 908f37b4e0ee140f7dca78f6c6e5c6de2efc3484 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Sergei Shtylyov8ac98ce2007-11-27 21:35:53 +01002 * linux/drivers/ide/pci/siimage.c Version 1.19 Nov 16 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
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020029 * if necessary
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{
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +020060#ifdef CONFIG_BLK_DEV_IDE_SATA
61 switch(pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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();
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +020069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return 0;
71}
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +020072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/**
74 * is_sata - check if hwif is SATA
75 * @hwif: interface to check
76 *
77 * Returns true if this is a SATA controller
78 */
79
80static inline int is_sata(ide_hwif_t *hwif)
81{
82 return pdev_is_sata(hwif->pci_dev);
83}
84
85/**
86 * siimage_selreg - return register base
87 * @hwif: interface
88 * @r: config offset
89 *
90 * Turn a config register offset into the right address in either
91 * PCI space or MMIO space to access the control register in question
92 * Thankfully this is a configuration operation so isnt performance
93 * criticial.
94 */
95
96static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
97{
98 unsigned long base = (unsigned long)hwif->hwif_data;
99 base += 0xA0 + r;
100 if(hwif->mmio)
101 base += (hwif->channel << 6);
102 else
103 base += (hwif->channel << 4);
104 return base;
105}
106
107/**
108 * siimage_seldev - return register base
109 * @hwif: interface
110 * @r: config offset
111 *
112 * Turn a config register offset into the right address in either
113 * PCI space or MMIO space to access the control register in question
114 * including accounting for the unit shift.
115 */
116
117static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
118{
119 ide_hwif_t *hwif = HWIF(drive);
120 unsigned long base = (unsigned long)hwif->hwif_data;
121 base += 0xA0 + r;
122 if(hwif->mmio)
123 base += (hwif->channel << 6);
124 else
125 base += (hwif->channel << 4);
126 base |= drive->select.b.unit << drive->select.b.unit;
127 return base;
128}
129
130/**
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200131 * sil_udma_filter - compute UDMA mask
132 * @drive: IDE device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200134 * Compute the available UDMA speeds for the device on the interface.
135 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * For the CMD680 this depends on the clocking mode (scsc), for the
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200137 * SI3112 SATA controller life is a bit simpler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200139
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200140static u8 sil_pata_udma_filter(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200142 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned long base = (unsigned long) hwif->hwif_data;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200144 u8 mask = 0, scsc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (hwif->mmio)
147 scsc = hwif->INB(base + 0x4A);
148 else
149 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if ((scsc & 0x30) == 0x10) /* 133 */
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200152 mask = ATA_UDMA6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 else if ((scsc & 0x30) == 0x20) /* 2xPCI */
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200154 mask = ATA_UDMA6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 else if ((scsc & 0x30) == 0x00) /* 100 */
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200156 mask = ATA_UDMA5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 else /* Disabled ? */
158 BUG();
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200159
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200160 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200163static u8 sil_sata_udma_filter(ide_drive_t *drive)
164{
165 return strstr(drive->id->model, "Maxtor") ? ATA_UDMA5 : ATA_UDMA6;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200169 * sil_set_pio_mode - set host controller for PIO mode
170 * @drive: drive
171 * @pio: PIO mode number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * Load the timing settings for this device mode into the
174 * controller. If we are in PIO mode 3 or 4 turn on IORDY
175 * monitoring (bit 9). The TF timing is bits 31:16
176 */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200177
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200178static void sil_set_pio_mode(ide_drive_t *drive, u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200180 const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
181 const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ide_hwif_t *hwif = HWIF(drive);
Benjamin Herrenschmidta87a87c2007-10-19 00:30:05 +0200184 ide_drive_t *pair = ide_get_paired_drive(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 u32 speedt = 0;
186 u16 speedp = 0;
187 unsigned long addr = siimage_seldev(drive, 0x04);
188 unsigned long tfaddr = siimage_selreg(hwif, 0x02);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200189 unsigned long base = (unsigned long)hwif->hwif_data;
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200190 u8 tf_pio = pio;
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200191 u8 addr_mask = hwif->channel ? (hwif->mmio ? 0xF4 : 0x84)
192 : (hwif->mmio ? 0xB4 : 0x80);
193 u8 mode = 0;
194 u8 unit = drive->select.b.unit;
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200195
196 /* trim *taskfile* PIO to the slowest of the master/slave */
197 if (pair->present) {
Bartlomiej Zolnierkiewicz21347582007-07-20 01:11:58 +0200198 u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200199
200 if (pair_pio < tf_pio)
201 tf_pio = pair_pio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100203
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200204 /* cheat for now and use the docs */
205 speedp = data_speed[pio];
206 speedt = tf_speed[tf_pio];
207
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100208 if (hwif->mmio) {
209 hwif->OUTW(speedp, addr);
210 hwif->OUTW(speedt, tfaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Now set up IORDY */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200212 if (pio > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
214 else
215 hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200216
217 mode = hwif->INB(base + addr_mask);
218 mode &= ~(unit ? 0x30 : 0x03);
219 mode |= (unit ? 0x10 : 0x01);
220 hwif->OUTB(mode, base + addr_mask);
Sergei Shtylyov075cb652007-02-17 02:40:22 +0100221 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 pci_write_config_word(hwif->pci_dev, addr, speedp);
223 pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
224 pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
225 speedp &= ~0x200;
226 /* Set IORDY for mode 3 or 4 */
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200227 if (pio > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 speedp |= 0x200;
229 pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
Bartlomiej Zolnierkiewiczffe54152007-10-11 23:54:01 +0200230
231 pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
232 mode &= ~(unit ? 0x30 : 0x03);
233 mode |= (unit ? 0x10 : 0x01);
234 pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200239 * sil_set_dma_mode - set host controller for DMA mode
240 * @drive: drive
241 * @speed: DMA mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200243 * Tune the SiI chipset for the desired DMA mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200245
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200246static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
249 u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
250 u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
251
252 ide_hwif_t *hwif = HWIF(drive);
253 u16 ultra = 0, multi = 0;
254 u8 mode = 0, unit = drive->select.b.unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned long base = (unsigned long)hwif->hwif_data;
256 u8 scsc = 0, addr_mask = ((hwif->channel) ?
257 ((hwif->mmio) ? 0xF4 : 0x84) :
258 ((hwif->mmio) ? 0xB4 : 0x80));
259
260 unsigned long ma = siimage_seldev(drive, 0x08);
261 unsigned long ua = siimage_seldev(drive, 0x0C);
262
263 if (hwif->mmio) {
264 scsc = hwif->INB(base + 0x4A);
265 mode = hwif->INB(base + addr_mask);
266 multi = hwif->INW(ma);
267 ultra = hwif->INW(ua);
268 } else {
269 pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
270 pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
271 pci_read_config_word(hwif->pci_dev, ma, &multi);
272 pci_read_config_word(hwif->pci_dev, ua, &ultra);
273 }
274
275 mode &= ~((unit) ? 0x30 : 0x03);
276 ultra &= ~0x3F;
277 scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
278
279 scsc = is_sata(hwif) ? 1 : scsc;
280
Bartlomiej Zolnierkiewicz4db90a12008-01-25 22:17:18 +0100281 if (speed >= XFER_UDMA_0) {
282 multi = dma[2];
283 ultra |= (scsc ? ultra6[speed - XFER_UDMA_0] :
284 ultra5[speed - XFER_UDMA_0]);
285 mode |= (unit ? 0x30 : 0x03);
286 } else {
287 multi = dma[speed - XFER_MW_DMA_0];
288 mode |= (unit ? 0x20 : 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if (hwif->mmio) {
292 hwif->OUTB(mode, base + addr_mask);
293 hwif->OUTW(multi, ma);
294 hwif->OUTW(ultra, ua);
295 } else {
296 pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
297 pci_write_config_word(hwif->pci_dev, ma, multi);
298 pci_write_config_word(hwif->pci_dev, ua, ultra);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* returns 1 if dma irq issued, 0 otherwise */
303static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
304{
305 ide_hwif_t *hwif = HWIF(drive);
306 u8 dma_altstat = 0;
307 unsigned long addr = siimage_selreg(hwif, 1);
308
309 /* return 1 if INTR asserted */
310 if ((hwif->INB(hwif->dma_status) & 4) == 4)
311 return 1;
312
313 /* return 1 if Device INTR asserted */
314 pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
315 if (dma_altstat & 8)
316 return 0; //return 1;
317 return 0;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/**
321 * siimage_mmio_ide_dma_test_irq - check we caused an IRQ
322 * @drive: drive we are testing
323 *
324 * Check if we caused an IDE DMA interrupt. We may also have caused
325 * SATA status interrupts, if so we clean them up and continue.
326 */
327
328static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
329{
330 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unsigned long addr = siimage_selreg(hwif, 0x1);
332
333 if (SATA_ERROR_REG) {
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200334 unsigned long base = (unsigned long)hwif->hwif_data;
335
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100336 u32 ext_stat = readl((void __iomem *)(base + 0x10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 u8 watchdog = 0;
338 if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100339 u32 sata_error = readl((void __iomem *)SATA_ERROR_REG);
340 writel(sata_error, (void __iomem *)SATA_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 watchdog = (sata_error & 0x00680000) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk(KERN_WARNING "%s: sata_error = 0x%08x, "
343 "watchdog = %d, %s\n",
344 drive->name, sata_error, watchdog,
345 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 } else {
348 watchdog = (ext_stat & 0x8000) ? 1 : 0;
349 }
350 ext_stat >>= 16;
351
352 if (!(ext_stat & 0x0404) && !watchdog)
353 return 0;
354 }
355
356 /* return 1 if INTR asserted */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100357 if ((readb((void __iomem *)hwif->dma_status) & 0x04) == 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 1;
359
360 /* return 1 if Device INTR asserted */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100361 if ((readb((void __iomem *)addr) & 8) == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0; //return 1;
363
364 return 0;
365}
366
367/**
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200368 * sil_sata_busproc - bus isolation IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * @drive: drive to isolate/restore
370 * @state: bus state to set
371 *
372 * Used by the SII3112 to handle bus isolation. As this is a
373 * SATA controller the work required is quite limited, we
374 * just have to clean up the statistics
375 */
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200376
377static int sil_sata_busproc(ide_drive_t * drive, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 ide_hwif_t *hwif = HWIF(drive);
380 u32 stat_config = 0;
381 unsigned long addr = siimage_selreg(hwif, 0);
382
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100383 if (hwif->mmio)
384 stat_config = readl((void __iomem *)addr);
385 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
387
388 switch (state) {
389 case BUSSTATE_ON:
390 hwif->drives[0].failures = 0;
391 hwif->drives[1].failures = 0;
392 break;
393 case BUSSTATE_OFF:
394 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
395 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
396 break;
397 case BUSSTATE_TRISTATE:
398 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
399 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
400 break;
401 default:
402 return -EINVAL;
403 }
404 hwif->bus_state = state;
405 return 0;
406}
407
408/**
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200409 * sil_sata_reset_poll - wait for SATA reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * @drive: drive we are resetting
411 *
412 * Poll the SATA phy and see whether it has come back from the dead
413 * yet.
414 */
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200415
416static int sil_sata_reset_poll(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 if (SATA_STATUS_REG) {
419 ide_hwif_t *hwif = HWIF(drive);
420
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100421 /* SATA_STATUS_REG is valid only when in MMIO mode */
422 if ((readl((void __iomem *)SATA_STATUS_REG) & 0x03) != 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100424 hwif->name, readl((void __iomem *)SATA_STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 HWGROUP(drive)->polling = 0;
426 return ide_started;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200429
430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/**
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200434 * sil_sata_pre_reset - reset hook
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * @drive: IDE device being reset
436 *
437 * For the SATA devices we need to handle recalibration/geometry
438 * differently
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200441static void sil_sata_pre_reset(ide_drive_t *drive)
442{
443 if (drive->media == ide_disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 drive->special.b.set_geometry = 0;
445 drive->special.b.recalibrate = 0;
446 }
447}
448
449/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * proc_reports_siimage - add siimage controller to proc
451 * @dev: PCI device
452 * @clocking: SCSC value
453 * @name: controller name
454 *
455 * Report the clocking mode of the controller and add it to
456 * the /proc interface layer
457 */
458
459static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
460{
461 if (!pdev_is_sata(dev)) {
462 printk(KERN_INFO "%s: BASE CLOCK ", name);
463 clocking &= 0x03;
464 switch (clocking) {
465 case 0x03: printk("DISABLED!\n"); break;
466 case 0x02: printk("== 2X PCI\n"); break;
467 case 0x01: printk("== 133\n"); break;
468 case 0x00: printk("== 100\n"); break;
469 }
470 }
471}
472
473/**
474 * setup_mmio_siimage - switch an SI controller into MMIO
475 * @dev: PCI device we are configuring
476 * @name: device name
477 *
478 * Attempt to put the device into mmio mode. There are some slight
479 * complications here with certain systems where the mmio bar isnt
480 * mapped so we have to be sure we can fall back to I/O.
481 */
482
483static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
484{
485 unsigned long bar5 = pci_resource_start(dev, 5);
486 unsigned long barsize = pci_resource_len(dev, 5);
487 u8 tmpbyte = 0;
488 void __iomem *ioaddr;
John W. Linvilled868dd12005-11-10 00:19:14 +0100489 u32 tmp, irq_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * Drop back to PIO if we can't map the mmio. Some
493 * systems seem to get terminally confused in the PCI
494 * spaces.
495 */
496
497 if(!request_mem_region(bar5, barsize, name))
498 {
499 printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
500 return 0;
501 }
502
503 ioaddr = ioremap(bar5, barsize);
504
505 if (ioaddr == NULL)
506 {
507 release_mem_region(bar5, barsize);
508 return 0;
509 }
510
511 pci_set_master(dev);
512 pci_set_drvdata(dev, (void *) ioaddr);
513
514 if (pdev_is_sata(dev)) {
John W. Linvilled868dd12005-11-10 00:19:14 +0100515 /* make sure IDE0/1 interrupts are not masked */
516 irq_mask = (1 << 22) | (1 << 23);
517 tmp = readl(ioaddr + 0x48);
518 if (tmp & irq_mask) {
519 tmp &= ~irq_mask;
520 writel(tmp, ioaddr + 0x48);
521 readl(ioaddr + 0x48); /* flush */
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 writel(0, ioaddr + 0x148);
524 writel(0, ioaddr + 0x1C8);
525 }
526
527 writeb(0, ioaddr + 0xB4);
528 writeb(0, ioaddr + 0xF4);
529 tmpbyte = readb(ioaddr + 0x4A);
530
531 switch(tmpbyte & 0x30) {
532 case 0x00:
533 /* In 100 MHz clocking, try and switch to 133 */
534 writeb(tmpbyte|0x10, ioaddr + 0x4A);
535 break;
536 case 0x10:
537 /* On 133Mhz clocking */
538 break;
539 case 0x20:
540 /* On PCIx2 clocking */
541 break;
542 case 0x30:
543 /* Clocking is disabled */
544 /* 133 clock attempt to force it on */
545 writeb(tmpbyte & ~0x20, ioaddr + 0x4A);
546 break;
547 }
548
549 writeb( 0x72, ioaddr + 0xA1);
550 writew( 0x328A, ioaddr + 0xA2);
551 writel(0x62DD62DD, ioaddr + 0xA4);
552 writel(0x43924392, ioaddr + 0xA8);
553 writel(0x40094009, ioaddr + 0xAC);
554 writeb( 0x72, ioaddr + 0xE1);
555 writew( 0x328A, ioaddr + 0xE2);
556 writel(0x62DD62DD, ioaddr + 0xE4);
557 writel(0x43924392, ioaddr + 0xE8);
558 writel(0x40094009, ioaddr + 0xEC);
559
560 if (pdev_is_sata(dev)) {
561 writel(0xFFFF0000, ioaddr + 0x108);
562 writel(0xFFFF0000, ioaddr + 0x188);
563 writel(0x00680000, ioaddr + 0x148);
564 writel(0x00680000, ioaddr + 0x1C8);
565 }
566
567 tmpbyte = readb(ioaddr + 0x4A);
568
569 proc_reports_siimage(dev, (tmpbyte>>4), name);
570 return 1;
571}
572
573/**
574 * init_chipset_siimage - set up an SI device
575 * @dev: PCI device
576 * @name: device name
577 *
578 * Perform the initial PCI set up for this device. Attempt to switch
579 * to 133MHz clocking if the system isn't already set up to do it.
580 */
581
582static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
583{
Bartlomiej Zolnierkiewiczfc212bb2007-10-19 00:30:08 +0200584 u8 rev = dev->revision, tmpbyte = 0, BA5_EN = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Bartlomiej Zolnierkiewiczfc212bb2007-10-19 00:30:08 +0200586 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, rev ? 1 : 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 pci_read_config_byte(dev, 0x8A, &BA5_EN);
589 if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
590 if (setup_mmio_siimage(dev, name)) {
591 return 0;
592 }
593 }
594
595 pci_write_config_byte(dev, 0x80, 0x00);
596 pci_write_config_byte(dev, 0x84, 0x00);
597 pci_read_config_byte(dev, 0x8A, &tmpbyte);
598 switch(tmpbyte & 0x30) {
599 case 0x00:
600 /* 133 clock attempt to force it on */
601 pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
602 case 0x30:
603 /* if clocking is disabled */
604 /* 133 clock attempt to force it on */
605 pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
606 case 0x10:
607 /* 133 already */
608 break;
609 case 0x20:
610 /* BIOS set PCI x2 clocking */
611 break;
612 }
613
614 pci_read_config_byte(dev, 0x8A, &tmpbyte);
615
616 pci_write_config_byte(dev, 0xA1, 0x72);
617 pci_write_config_word(dev, 0xA2, 0x328A);
618 pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
619 pci_write_config_dword(dev, 0xA8, 0x43924392);
620 pci_write_config_dword(dev, 0xAC, 0x40094009);
621 pci_write_config_byte(dev, 0xB1, 0x72);
622 pci_write_config_word(dev, 0xB2, 0x328A);
623 pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
624 pci_write_config_dword(dev, 0xB8, 0x43924392);
625 pci_write_config_dword(dev, 0xBC, 0x40094009);
626
627 proc_reports_siimage(dev, (tmpbyte>>4), name);
628 return 0;
629}
630
631/**
632 * init_mmio_iops_siimage - set up the iops for MMIO
633 * @hwif: interface to set up
634 *
635 * The basic setup here is fairly simple, we can use standard MMIO
636 * operations. However we do have to set the taskfile register offsets
637 * by hand as there isnt a standard defined layout for them this
638 * time.
639 *
640 * The hardware supports buffered taskfiles and also some rather nice
Alan Cox19c1ef52006-06-28 04:26:59 -0700641 * extended PRD tables. For better SI3112 support use the libata driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643
644static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
645{
646 struct pci_dev *dev = hwif->pci_dev;
647 void *addr = pci_get_drvdata(dev);
648 u8 ch = hwif->channel;
649 hw_regs_t hw;
650 unsigned long base;
651
652 /*
653 * Fill in the basic HWIF bits
654 */
655
656 default_hwif_mmiops(hwif);
657 hwif->hwif_data = addr;
658
659 /*
660 * Now set up the hw. We have to do this ourselves as
Michael Opdenacker59c51592007-05-09 08:57:56 +0200661 * the MMIO layout isnt the same as the standard port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * based I/O
663 */
664
665 memset(&hw, 0, sizeof(hw_regs_t));
666
667 base = (unsigned long)addr;
668 if (ch)
669 base += 0xC0;
670 else
671 base += 0x80;
672
673 /*
674 * The buffered task file doesn't have status/control
675 * so we can't currently use it sanely since we want to
676 * use LBA48 mode.
677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 hw.io_ports[IDE_DATA_OFFSET] = base;
679 hw.io_ports[IDE_ERROR_OFFSET] = base + 1;
680 hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
681 hw.io_ports[IDE_SECTOR_OFFSET] = base + 3;
682 hw.io_ports[IDE_LCYL_OFFSET] = base + 4;
683 hw.io_ports[IDE_HCYL_OFFSET] = base + 5;
684 hw.io_ports[IDE_SELECT_OFFSET] = base + 6;
685 hw.io_ports[IDE_STATUS_OFFSET] = base + 7;
686 hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
687
688 hw.io_ports[IDE_IRQ_OFFSET] = 0;
689
690 if (pdev_is_sata(dev)) {
691 base = (unsigned long)addr;
692 if (ch)
693 base += 0x80;
694 hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
695 hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
696 hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
697 hwif->sata_misc[SATA_MISC_OFFSET] = base + 0x140;
698 hwif->sata_misc[SATA_PHY_OFFSET] = base + 0x144;
699 hwif->sata_misc[SATA_IEN_OFFSET] = base + 0x148;
700 }
701
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200702 memcpy(hwif->io_ports, hw.io_ports, sizeof(hwif->io_ports));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200704 hwif->irq = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200706 hwif->dma_base = (unsigned long)addr + (ch ? 0x08 : 0x00);
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100707
708 hwif->mmio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static int is_dev_seagate_sata(ide_drive_t *drive)
712{
713 const char *s = &drive->id->model[0];
714 unsigned len;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 len = strnlen(s, sizeof(drive->id->model));
717
718 if ((len > 4) && (!memcmp(s, "ST", 2))) {
719 if ((!memcmp(s + len - 2, "AS", 2)) ||
720 (!memcmp(s + len - 3, "ASL", 3))) {
721 printk(KERN_INFO "%s: applying pessimistic Seagate "
722 "errata fix\n", drive->name);
723 return 1;
724 }
725 }
726 return 0;
727}
728
729/**
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100730 * sil_quirkproc - post probe fixups
731 * @drive: drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
733 * Called after drive probe we use this to decide whether the
734 * Seagate fixup must be applied. This used to be in init_iops but
735 * that can occur before we know what drives are present.
736 */
737
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100738static void __devinit sil_quirkproc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100740 ide_hwif_t *hwif = drive->hwif;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Try and raise the rqsize */
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100743 if (!is_sata(hwif) || !is_dev_seagate_sata(drive))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 hwif->rqsize = 128;
745}
746
747/**
748 * init_iops_siimage - set up iops
749 * @hwif: interface to set up
750 *
751 * Do the basic setup for the SIIMAGE hardware interface
752 * and then do the MMIO setup if we can. This is the first
753 * look in we get for setting up the hwif so that we
754 * can get the iops right before using them.
755 */
756
757static void __devinit init_iops_siimage(ide_hwif_t *hwif)
758{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 hwif->hwif_data = NULL;
760
761 /* Pessimal until we finish probing */
762 hwif->rqsize = 15;
763
Bartlomiej Zolnierkiewiczfc212bb2007-10-19 00:30:08 +0200764 if (pci_get_drvdata(hwif->pci_dev) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return;
Bartlomiej Zolnierkiewiczfc212bb2007-10-19 00:30:08 +0200766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 init_mmio_iops_siimage(hwif);
768}
769
770/**
771 * ata66_siimage - check for 80 pin cable
772 * @hwif: interface to check
773 *
774 * Check for the presence of an ATA66 capable cable on the
775 * interface.
776 */
777
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200778static u8 __devinit ata66_siimage(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 unsigned long addr = siimage_selreg(hwif, 0);
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200781 u8 ata66 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200783 if (pci_get_drvdata(hwif->pci_dev) == NULL)
784 pci_read_config_byte(hwif->pci_dev, addr, &ata66);
785 else
786 ata66 = hwif->INB(addr);
787
788 return (ata66 & 0x01) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/**
792 * init_hwif_siimage - set up hwif structs
793 * @hwif: interface to set up
794 *
795 * We do the basic set up of the interface structure. The SIIMAGE
796 * requires several custom handlers so we override the default
797 * ide DMA handlers appropriately
798 */
799
800static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
801{
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200802 u8 sata = is_sata(hwif);
803
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200804 hwif->set_pio_mode = &sil_set_pio_mode;
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200805 hwif->set_dma_mode = &sil_set_dma_mode;
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100806 hwif->quirkproc = &sil_quirkproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200808 if (sata) {
Alan Cox19c1ef52006-06-28 04:26:59 -0700809 static int first = 1;
810
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200811 hwif->busproc = &sil_sata_busproc;
812 hwif->reset_poll = &sil_sata_reset_poll;
813 hwif->pre_reset = &sil_sata_pre_reset;
814 hwif->udma_filter = &sil_sata_udma_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alan Cox19c1ef52006-06-28 04:26:59 -0700816 if (first) {
817 printk(KERN_INFO "siimage: For full SATA support you should use the libata sata_sil module.\n");
818 first = 0;
819 }
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200820 } else
821 hwif->udma_filter = &sil_pata_udma_filter;
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200822
Bartlomiej Zolnierkiewicz328dcbb2007-07-20 01:11:54 +0200823 if (hwif->dma_base == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Bartlomiej Zolnierkiewicz438c4702007-10-20 00:32:31 +0200826 if (sata)
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200827 hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200829 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
830 hwif->cbl = ata66_siimage(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (hwif->mmio) {
833 hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
834 } else {
835 hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839#define DECLARE_SII_DEV(name_str) \
840 { \
841 .name = name_str, \
842 .init_chipset = init_chipset_siimage, \
843 .init_iops = init_iops_siimage, \
844 .init_hwif = init_hwif_siimage, \
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200845 .host_flags = IDE_HFLAG_BOOTABLE, \
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200846 .pio_mask = ATA_PIO4, \
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200847 .mwdma_mask = ATA_MWDMA2, \
848 .udma_mask = ATA_UDMA6, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200851static const struct ide_port_info siimage_chipsets[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* 0 */ DECLARE_SII_DEV("SiI680"),
853 /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
854 /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
855};
856
857/**
858 * siimage_init_one - pci layer discovery entry
859 * @dev: PCI device
860 * @id: ident table entry
861 *
862 * Called by the PCI code when it finds an SI680 or SI3112 controller.
863 * We then use the IDE PCI generic helper to do most of the work.
864 */
865
866static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
867{
868 return ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
869}
870
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200871static const struct pci_device_id siimage_pci_tbl[] = {
872 { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_680), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#ifdef CONFIG_BLK_DEV_IDE_SATA
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200874 { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_3112), 1 },
875 { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_1210SA), 2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#endif
877 { 0, },
878};
879MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
880
881static struct pci_driver driver = {
882 .name = "SiI_IDE",
883 .id_table = siimage_pci_tbl,
884 .probe = siimage_init_one,
885};
886
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100887static int __init siimage_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 return ide_pci_register_driver(&driver);
890}
891
892module_init(siimage_ide_init);
893
894MODULE_AUTHOR("Andre Hedrick, Alan Cox");
895MODULE_DESCRIPTION("PCI driver module for SiI IDE");
896MODULE_LICENSE("GPL");