blob: 8af0dc88fd988c25fa9b5256fbcf927caf1158d5 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_sis.c - SiS ATA driver
3 *
Alan Coxab771632008-10-27 15:09:10 +00004 * (C) 2005 Red Hat
Bartlomiej Zolnierkiewicz750c7132009-12-03 20:32:13 +01005 * (C) 2007,2009 Bartlomiej Zolnierkiewicz
Jeff Garzik669a5db2006-08-29 18:12:40 -04006 *
7 * Based upon linux/drivers/ide/pci/sis5513.c
8 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
9 * Copyright (C) 2002 Lionel Bouton <Lionel.Bouton@inet6.fr>, Maintainer
10 * Copyright (C) 2003 Vojtech Pavlik <vojtech@suse.cz>
11 * SiS Taiwan : for direct support and hardware.
12 * Daniela Engert : for initial ATA100 advices and numerous others.
13 * John Fremlin, Manfred Spraul, Dave Morgan, Peter Kjellerstedt :
14 * for checking code correctness, providing patches.
15 * Original tests and design on the SiS620 chipset.
16 * ATA100 tests and design on the SiS735 chipset.
17 * ATA16/33 support from specs
18 * ATA133 support for SiS961/962 by L.C. Chang <lcchang@sis.com.tw>
19 *
20 *
21 * TODO
22 * Check MWDMA on drives that don't support MWDMA speed pio cycles ?
23 * More Testing
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/init.h>
30#include <linux/blkdev.h>
31#include <linux/delay.h>
32#include <linux/device.h>
33#include <scsi/scsi_host.h>
34#include <linux/libata.h>
35#include <linux/ata.h>
Alan4bb64fb2007-02-16 01:40:04 -080036#include "sis.h"
Jeff Garzik669a5db2006-08-29 18:12:40 -040037
38#define DRV_NAME "pata_sis"
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +020039#define DRV_VERSION "0.5.2"
Jeff Garzik669a5db2006-08-29 18:12:40 -040040
41struct sis_chipset {
Tejun Heo1626aeb2007-05-04 12:43:58 +020042 u16 device; /* PCI host ID */
43 const struct ata_port_info *info; /* Info block */
Jeff Garzik669a5db2006-08-29 18:12:40 -040044 /* Probably add family, cable detect type etc here to clean
45 up code later */
46};
47
Jakub W. Jozwicki J7dcbc1f2007-01-09 09:01:19 +090048struct sis_laptop {
49 u16 device;
50 u16 subvendor;
51 u16 subdevice;
52};
53
54static const struct sis_laptop sis_laptop[] = {
55 /* devid, subvendor, subdev */
56 { 0x5513, 0x1043, 0x1107 }, /* ASUS A6K */
Alan Cox4f2d47c2007-08-22 22:56:43 +010057 { 0x5513, 0x1734, 0x105F }, /* FSC Amilo A1630 */
Gabriel C1f71d062007-11-15 13:14:00 +090058 { 0x5513, 0x1071, 0x8640 }, /* EasyNote K5305 */
Jakub W. Jozwicki J7dcbc1f2007-01-09 09:01:19 +090059 /* end marker */
60 { 0, }
61};
62
63static int sis_short_ata40(struct pci_dev *dev)
64{
65 const struct sis_laptop *lap = &sis_laptop[0];
66
67 while (lap->device) {
68 if (lap->device == dev->device &&
69 lap->subvendor == dev->subsystem_vendor &&
70 lap->subdevice == dev->subsystem_device)
71 return 1;
72 lap++;
73 }
74
75 return 0;
76}
77
Jeff Garzik669a5db2006-08-29 18:12:40 -040078/**
Alan Coxdd668d12007-05-21 15:00:53 +010079 * sis_old_port_base - return PCI configuration base for dev
Jeff Garzik669a5db2006-08-29 18:12:40 -040080 * @adev: device
81 *
82 * Returns the base of the PCI configuration registers for this port
83 * number.
84 */
85
Alan Coxdd668d12007-05-21 15:00:53 +010086static int sis_old_port_base(struct ata_device *adev)
Jeff Garzik669a5db2006-08-29 18:12:40 -040087{
Tejun Heo9af5c9c2007-08-06 18:36:22 +090088 return 0x40 + (4 * adev->link->ap->port_no) + (2 * adev->devno);
Jeff Garzik669a5db2006-08-29 18:12:40 -040089}
90
91/**
Alan Cox2e413f52007-03-07 16:54:24 +000092 * sis_133_cable_detect - check for 40/80 pin
Jeff Garzik669a5db2006-08-29 18:12:40 -040093 * @ap: Port
Tejun Heod4b2bab2007-02-02 16:50:52 +090094 * @deadline: deadline jiffies for the operation
Jeff Garzik669a5db2006-08-29 18:12:40 -040095 *
96 * Perform cable detection for the later UDMA133 capable
97 * SiS chipset.
98 */
99
Alan Cox2e413f52007-03-07 16:54:24 +0000100static int sis_133_cable_detect(struct ata_port *ap)
101{
102 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
103 u16 tmp;
104
105 /* The top bit of this register is the cable detect bit */
106 pci_read_config_word(pdev, 0x50 + 2 * ap->port_no, &tmp);
107 if ((tmp & 0x8000) && !sis_short_ata40(pdev))
108 return ATA_CBL_PATA40;
109 return ATA_CBL_PATA80;
110}
111
112/**
113 * sis_66_cable_detect - check for 40/80 pin
114 * @ap: Port
115 *
116 * Perform cable detection on the UDMA66, UDMA100 and early UDMA133
117 * SiS IDE controllers.
118 */
119
120static int sis_66_cable_detect(struct ata_port *ap)
121{
122 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
123 u8 tmp;
124
125 /* Older chips keep cable detect in bits 4/5 of reg 0x48 */
126 pci_read_config_byte(pdev, 0x48, &tmp);
127 tmp >>= ap->port_no;
128 if ((tmp & 0x10) && !sis_short_ata40(pdev))
129 return ATA_CBL_PATA40;
130 return ATA_CBL_PATA80;
131}
132
133
134/**
135 * sis_pre_reset - probe begin
Tejun Heocc0680a2007-08-06 18:36:23 +0900136 * @link: ATA link
Tejun Heod4b2bab2007-02-02 16:50:52 +0900137 * @deadline: deadline jiffies for the operation
Alan Cox2e413f52007-03-07 16:54:24 +0000138 *
139 * Set up cable type and use generic probe init
140 */
141
Tejun Heocc0680a2007-08-06 18:36:23 +0900142static int sis_pre_reset(struct ata_link *link, unsigned long deadline)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400143{
144 static const struct pci_bits sis_enable_bits[] = {
145 { 0x4aU, 1U, 0x02UL, 0x02UL }, /* port 0 */
146 { 0x4aU, 1U, 0x04UL, 0x04UL }, /* port 1 */
147 };
Jeff Garzik85cd7252006-08-31 00:03:49 -0400148
Tejun Heocc0680a2007-08-06 18:36:23 +0900149 struct ata_port *ap = link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400150 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400151
Alan Coxc9619222006-09-26 17:53:38 +0100152 if (!pci_test_config_bits(pdev, &sis_enable_bits[ap->port_no]))
153 return -ENOENT;
Tejun Heod4b2bab2007-02-02 16:50:52 +0900154
Alan Cox15ce0942007-05-25 20:50:24 +0100155 /* Clear the FIFO settings. We can't enable the FIFO until
156 we know we are poking at a disk */
157 pci_write_config_byte(pdev, 0x4B, 0);
Tejun Heo9363c382008-04-07 22:47:16 +0900158 return ata_sff_prereset(link, deadline);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400159}
160
Alan Cox2e413f52007-03-07 16:54:24 +0000161
Jeff Garzik669a5db2006-08-29 18:12:40 -0400162/**
Jeff Garzik669a5db2006-08-29 18:12:40 -0400163 * sis_set_fifo - Set RWP fifo bits for this device
164 * @ap: Port
165 * @adev: Device
166 *
167 * SIS chipsets implement prefetch/postwrite bits for each device
168 * on both channels. This functionality is not ATAPI compatible and
169 * must be configured according to the class of device present
170 */
171
172static void sis_set_fifo(struct ata_port *ap, struct ata_device *adev)
173{
174 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
175 u8 fifoctrl;
176 u8 mask = 0x11;
177
178 mask <<= (2 * ap->port_no);
179 mask <<= adev->devno;
180
181 /* This holds various bits including the FIFO control */
182 pci_read_config_byte(pdev, 0x4B, &fifoctrl);
183 fifoctrl &= ~mask;
184
185 /* Enable for ATA (disk) only */
186 if (adev->class == ATA_DEV_ATA)
187 fifoctrl |= mask;
188 pci_write_config_byte(pdev, 0x4B, fifoctrl);
189}
190
191/**
192 * sis_old_set_piomode - Initialize host controller PATA PIO timings
193 * @ap: Port whose timings we are configuring
194 * @adev: Device we are configuring for.
195 *
196 * Set PIO mode for device, in host controller PCI config space. This
197 * function handles PIO set up for all chips that are pre ATA100 and
198 * also early ATA100 devices.
199 *
200 * LOCKING:
201 * None (inherited from caller).
202 */
203
204static void sis_old_set_piomode (struct ata_port *ap, struct ata_device *adev)
205{
206 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxdd668d12007-05-21 15:00:53 +0100207 int port = sis_old_port_base(adev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400208 u8 t1, t2;
209 int speed = adev->pio_mode - XFER_PIO_0;
210
211 const u8 active[] = { 0x00, 0x07, 0x04, 0x03, 0x01 };
212 const u8 recovery[] = { 0x00, 0x06, 0x04, 0x03, 0x03 };
213
214 sis_set_fifo(ap, adev);
215
216 pci_read_config_byte(pdev, port, &t1);
217 pci_read_config_byte(pdev, port + 1, &t2);
218
219 t1 &= ~0x0F; /* Clear active/recovery timings */
220 t2 &= ~0x07;
221
222 t1 |= active[speed];
223 t2 |= recovery[speed];
224
225 pci_write_config_byte(pdev, port, t1);
226 pci_write_config_byte(pdev, port + 1, t2);
227}
228
229/**
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200230 * sis_100_set_piomode - Initialize host controller PATA PIO timings
Jeff Garzik669a5db2006-08-29 18:12:40 -0400231 * @ap: Port whose timings we are configuring
232 * @adev: Device we are configuring for.
233 *
234 * Set PIO mode for device, in host controller PCI config space. This
235 * function handles PIO set up for ATA100 devices and early ATA133.
236 *
237 * LOCKING:
238 * None (inherited from caller).
239 */
240
241static void sis_100_set_piomode (struct ata_port *ap, struct ata_device *adev)
242{
243 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxdd668d12007-05-21 15:00:53 +0100244 int port = sis_old_port_base(adev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400245 int speed = adev->pio_mode - XFER_PIO_0;
246
247 const u8 actrec[] = { 0x00, 0x67, 0x44, 0x33, 0x31 };
248
249 sis_set_fifo(ap, adev);
250
251 pci_write_config_byte(pdev, port, actrec[speed]);
252}
253
254/**
Alan Coxf20941f2009-11-30 13:22:49 +0000255 * sis_133_do_piomode - Initialize host controller PATA PIO/DMA timings
Jeff Garzik669a5db2006-08-29 18:12:40 -0400256 * @ap: Port whose timings we are configuring
257 * @adev: Device we are configuring for.
258 *
259 * Set PIO mode for device, in host controller PCI config space. This
Alan Coxf20941f2009-11-30 13:22:49 +0000260 * function handles PIO set up for the later ATA133 devices. The same
261 * timings are used for MWDMA.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400262 *
263 * LOCKING:
264 * None (inherited from caller).
265 */
266
Alan Coxf20941f2009-11-30 13:22:49 +0000267static void sis_133_do_piomode(struct ata_port *ap, struct ata_device *adev,
268 int speed)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400269{
270 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
271 int port = 0x40;
272 u32 t1;
273 u32 reg54;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400274
275 const u32 timing133[] = {
276 0x28269000, /* Recovery << 24 | Act << 16 | Ini << 12 */
277 0x0C266000,
278 0x04263000,
279 0x0C0A3000,
280 0x05093000
281 };
282 const u32 timing100[] = {
283 0x1E1C6000, /* Recovery << 24 | Act << 16 | Ini << 12 */
284 0x091C4000,
285 0x031C2000,
286 0x09072000,
287 0x04062000
288 };
289
290 sis_set_fifo(ap, adev);
291
292 /* If bit 14 is set then the registers are mapped at 0x70 not 0x40 */
293 pci_read_config_dword(pdev, 0x54, &reg54);
294 if (reg54 & 0x40000000)
295 port = 0x70;
296 port += 8 * ap->port_no + 4 * adev->devno;
297
298 pci_read_config_dword(pdev, port, &t1);
299 t1 &= 0xC0C00FFF; /* Mask out timing */
300
301 if (t1 & 0x08) /* 100 or 133 ? */
302 t1 |= timing133[speed];
303 else
304 t1 |= timing100[speed];
305 pci_write_config_byte(pdev, port, t1);
306}
307
308/**
Alan Coxf20941f2009-11-30 13:22:49 +0000309 * sis_133_set_piomode - Initialize host controller PATA PIO timings
310 * @ap: Port whose timings we are configuring
311 * @adev: Device we are configuring for.
312 *
313 * Set PIO mode for device, in host controller PCI config space. This
314 * function handles PIO set up for the later ATA133 devices.
315 *
316 * LOCKING:
317 * None (inherited from caller).
318 */
319
320static void sis_133_set_piomode (struct ata_port *ap, struct ata_device *adev)
321{
322
323 sis_133_do_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
324}
325
326/**
327 * mwdma_clip_to_pio - clip MWDMA mode
328 * @adev: device
329 *
330 * As the SiS shared MWDMA and PIO timings we must program the equivalent
331 * PIO timing for the MWDMA mode but we must not program one higher than
332 * the permitted PIO timing of the device.
333 */
334
335static int mwdma_clip_to_pio(struct ata_device *adev)
336{
337 const int mwdma_to_pio[3] = {
338 XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
339 };
340 return min(mwdma_to_pio[adev->dma_mode - XFER_MW_DMA_0],
341 adev->pio_mode - XFER_PIO_0);
342}
343
344/**
Jeff Garzik669a5db2006-08-29 18:12:40 -0400345 * sis_old_set_dmamode - Initialize host controller PATA DMA timings
346 * @ap: Port whose timings we are configuring
347 * @adev: Device to program
348 *
349 * Set UDMA/MWDMA mode for device, in host controller PCI config space.
350 * Handles pre UDMA and UDMA33 devices. Supports MWDMA as well unlike
351 * the old ide/pci driver.
352 *
353 * LOCKING:
354 * None (inherited from caller).
355 */
356
357static void sis_old_set_dmamode (struct ata_port *ap, struct ata_device *adev)
358{
359 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
360 int speed = adev->dma_mode - XFER_MW_DMA_0;
Alan Coxdd668d12007-05-21 15:00:53 +0100361 int drive_pci = sis_old_port_base(adev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400362 u16 timing;
363
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200364 const u16 mwdma_bits[] = { 0x008, 0x302, 0x301 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400365 const u16 udma_bits[] = { 0xE000, 0xC000, 0xA000 };
366
367 pci_read_config_word(pdev, drive_pci, &timing);
368
369 if (adev->dma_mode < XFER_UDMA_0) {
370 /* bits 3-0 hold recovery timing bits 8-10 active timing and
Joe Perches1967b7f2008-02-03 17:08:11 +0200371 the higher bits are dependant on the device */
Alan Coxf20941f2009-11-30 13:22:49 +0000372 speed = mwdma_clip_to_pio(adev);
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200373 timing &= ~0x870F;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400374 timing |= mwdma_bits[speed];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400375 } else {
376 /* Bit 15 is UDMA on/off, bit 13-14 are cycle time */
377 speed = adev->dma_mode - XFER_UDMA_0;
378 timing &= ~0x6000;
379 timing |= udma_bits[speed];
380 }
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200381 pci_write_config_word(pdev, drive_pci, timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400382}
383
384/**
385 * sis_66_set_dmamode - Initialize host controller PATA DMA timings
386 * @ap: Port whose timings we are configuring
387 * @adev: Device to program
388 *
389 * Set UDMA/MWDMA mode for device, in host controller PCI config space.
390 * Handles UDMA66 and early UDMA100 devices. Supports MWDMA as well unlike
391 * the old ide/pci driver.
392 *
393 * LOCKING:
394 * None (inherited from caller).
395 */
396
397static void sis_66_set_dmamode (struct ata_port *ap, struct ata_device *adev)
398{
399 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
400 int speed = adev->dma_mode - XFER_MW_DMA_0;
Alan Coxdd668d12007-05-21 15:00:53 +0100401 int drive_pci = sis_old_port_base(adev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400402 u16 timing;
403
Tejun Heoedeb6142007-09-21 16:29:05 +0900404 /* MWDMA 0-2 and UDMA 0-5 */
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200405 const u16 mwdma_bits[] = { 0x008, 0x302, 0x301 };
Tejun Heoedeb6142007-09-21 16:29:05 +0900406 const u16 udma_bits[] = { 0xF000, 0xD000, 0xB000, 0xA000, 0x9000, 0x8000 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400407
408 pci_read_config_word(pdev, drive_pci, &timing);
409
410 if (adev->dma_mode < XFER_UDMA_0) {
411 /* bits 3-0 hold recovery timing bits 8-10 active timing and
Joe Perches1967b7f2008-02-03 17:08:11 +0200412 the higher bits are dependant on the device, bit 15 udma */
Alan Coxf20941f2009-11-30 13:22:49 +0000413 speed = mwdma_clip_to_pio(adev);
Alan Coxdd668d12007-05-21 15:00:53 +0100414 timing &= ~0x870F;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400415 timing |= mwdma_bits[speed];
416 } else {
417 /* Bit 15 is UDMA on/off, bit 12-14 are cycle time */
418 speed = adev->dma_mode - XFER_UDMA_0;
Alan Coxdd668d12007-05-21 15:00:53 +0100419 timing &= ~0xF000;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400420 timing |= udma_bits[speed];
421 }
422 pci_write_config_word(pdev, drive_pci, timing);
423}
424
425/**
426 * sis_100_set_dmamode - Initialize host controller PATA DMA timings
427 * @ap: Port whose timings we are configuring
428 * @adev: Device to program
429 *
430 * Set UDMA/MWDMA mode for device, in host controller PCI config space.
Alan Coxf20941f2009-11-30 13:22:49 +0000431 * Handles later UDMA100 devices.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400432 *
433 * LOCKING:
434 * None (inherited from caller).
435 */
436
437static void sis_100_set_dmamode (struct ata_port *ap, struct ata_device *adev)
438{
439 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
440 int speed = adev->dma_mode - XFER_MW_DMA_0;
Alan Coxdd668d12007-05-21 15:00:53 +0100441 int drive_pci = sis_old_port_base(adev);
Alan Coxf20941f2009-11-30 13:22:49 +0000442 u16 timing;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400443
Alan Coxf20941f2009-11-30 13:22:49 +0000444 const u16 udma_bits[] = {
445 0x8B00, 0x8700, 0x8500, 0x8300, 0x8200, 0x8100};
446 const u8 mwdma_bits[] = { 0x08, 0x32, 0x31 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400447
Alan Coxf20941f2009-11-30 13:22:49 +0000448 pci_read_config_word(pdev, drive_pci, &timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400449
450 if (adev->dma_mode < XFER_UDMA_0) {
Alan Coxf20941f2009-11-30 13:22:49 +0000451 speed = mwdma_clip_to_pio(adev);
452 timing &= ~0x80FF;
453 timing |= mwdma_bits[speed];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400454 } else {
Alan Coxdd668d12007-05-21 15:00:53 +0100455 /* Bit 7 is UDMA on/off, bit 0-3 are cycle time */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400456 speed = adev->dma_mode - XFER_UDMA_0;
Alan Coxf20941f2009-11-30 13:22:49 +0000457 timing &= ~0x8F00;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400458 timing |= udma_bits[speed];
459 }
Alan Coxf20941f2009-11-30 13:22:49 +0000460 pci_write_config_word(pdev, drive_pci, timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400461}
462
463/**
464 * sis_133_early_set_dmamode - Initialize host controller PATA DMA timings
465 * @ap: Port whose timings we are configuring
466 * @adev: Device to program
467 *
468 * Set UDMA/MWDMA mode for device, in host controller PCI config space.
Bartlomiej Zolnierkiewicz4761c062007-07-31 22:02:41 +0200469 * Handles early SiS 961 bridges.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400470 *
471 * LOCKING:
472 * None (inherited from caller).
473 */
474
475static void sis_133_early_set_dmamode (struct ata_port *ap, struct ata_device *adev)
476{
477 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
478 int speed = adev->dma_mode - XFER_MW_DMA_0;
Alan Coxdd668d12007-05-21 15:00:53 +0100479 int drive_pci = sis_old_port_base(adev);
Alan Coxf20941f2009-11-30 13:22:49 +0000480 u16 timing;
481 /* Bits 15-12 are timing */
482 static const u16 udma_bits[] = {
483 0x8F00, 0x8A00, 0x8700, 0x8500, 0x8300, 0x8200, 0x8100
484 };
485 static const u8 mwdma_bits[] = { 0x08, 0x32, 0x31 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400486
Alan Coxf20941f2009-11-30 13:22:49 +0000487 pci_read_config_word(pdev, drive_pci, &timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400488
489 if (adev->dma_mode < XFER_UDMA_0) {
Alan Coxf20941f2009-11-30 13:22:49 +0000490 speed = mwdma_clip_to_pio(adev);
491 timing &= ~0x80FF;
492 timing = mwdma_bits[speed];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400493 } else {
Alan Coxdd668d12007-05-21 15:00:53 +0100494 /* Bit 7 is UDMA on/off, bit 0-3 are cycle time */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400495 speed = adev->dma_mode - XFER_UDMA_0;
Alan Coxf20941f2009-11-30 13:22:49 +0000496 timing &= ~0x8F00;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400497 timing |= udma_bits[speed];
498 }
Alan Coxf20941f2009-11-30 13:22:49 +0000499 pci_write_config_word(pdev, drive_pci, timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400500}
501
502/**
503 * sis_133_set_dmamode - Initialize host controller PATA DMA timings
504 * @ap: Port whose timings we are configuring
505 * @adev: Device to program
506 *
507 * Set UDMA/MWDMA mode for device, in host controller PCI config space.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400508 *
509 * LOCKING:
510 * None (inherited from caller).
511 */
512
513static void sis_133_set_dmamode (struct ata_port *ap, struct ata_device *adev)
514{
515 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
516 int speed = adev->dma_mode - XFER_MW_DMA_0;
517 int port = 0x40;
518 u32 t1;
519 u32 reg54;
520
521 /* bits 4- cycle time 8 - cvs time */
Alan Cox2e413f52007-03-07 16:54:24 +0000522 static const u32 timing_u100[] = { 0x6B0, 0x470, 0x350, 0x140, 0x120, 0x110, 0x000 };
523 static const u32 timing_u133[] = { 0x9F0, 0x6A0, 0x470, 0x250, 0x230, 0x220, 0x210 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400524
525 /* If bit 14 is set then the registers are mapped at 0x70 not 0x40 */
526 pci_read_config_dword(pdev, 0x54, &reg54);
527 if (reg54 & 0x40000000)
528 port = 0x70;
529 port += (8 * ap->port_no) + (4 * adev->devno);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400530 pci_read_config_dword(pdev, port, &t1);
531
532 if (adev->dma_mode < XFER_UDMA_0) {
Alan Coxf20941f2009-11-30 13:22:49 +0000533 speed = mwdma_clip_to_pio(adev);
534 sis_133_do_piomode(ap, adev, speed);
535 t1 &= ~4; /* UDMA off */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400536 } else {
537 speed = adev->dma_mode - XFER_UDMA_0;
538 /* if & 8 no UDMA133 - need info for ... */
539 t1 &= ~0x00000FF0;
540 t1 |= 0x00000004;
541 if (t1 & 0x08)
542 t1 |= timing_u133[speed];
543 else
544 t1 |= timing_u100[speed];
545 }
546 pci_write_config_dword(pdev, port, t1);
547}
548
549static struct scsi_host_template sis_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900550 ATA_BMDMA_SHT(DRV_NAME),
Jeff Garzik669a5db2006-08-29 18:12:40 -0400551};
552
Tejun Heo029cfd62008-03-25 12:22:49 +0900553static struct ata_port_operations sis_133_for_sata_ops = {
554 .inherits = &ata_bmdma_port_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400555 .set_piomode = sis_133_set_piomode,
556 .set_dmamode = sis_133_set_dmamode,
Tejun Heo029cfd62008-03-25 12:22:49 +0900557 .cable_detect = sis_133_cable_detect,
558};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400559
Tejun Heo029cfd62008-03-25 12:22:49 +0900560static struct ata_port_operations sis_base_ops = {
561 .inherits = &ata_bmdma_port_ops,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900562 .prereset = sis_pre_reset,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400563};
564
Tejun Heo029cfd62008-03-25 12:22:49 +0900565static struct ata_port_operations sis_133_ops = {
566 .inherits = &sis_base_ops,
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200567 .set_piomode = sis_133_set_piomode,
568 .set_dmamode = sis_133_set_dmamode,
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200569 .cable_detect = sis_133_cable_detect,
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200570};
571
Tejun Heo029cfd62008-03-25 12:22:49 +0900572static struct ata_port_operations sis_133_early_ops = {
573 .inherits = &sis_base_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400574 .set_piomode = sis_100_set_piomode,
575 .set_dmamode = sis_133_early_set_dmamode,
Alan Cox2e413f52007-03-07 16:54:24 +0000576 .cable_detect = sis_66_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400577};
578
Tejun Heo029cfd62008-03-25 12:22:49 +0900579static struct ata_port_operations sis_100_ops = {
580 .inherits = &sis_base_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400581 .set_piomode = sis_100_set_piomode,
582 .set_dmamode = sis_100_set_dmamode,
Alan Cox2e413f52007-03-07 16:54:24 +0000583 .cable_detect = sis_66_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400584};
585
Tejun Heo029cfd62008-03-25 12:22:49 +0900586static struct ata_port_operations sis_66_ops = {
587 .inherits = &sis_base_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400588 .set_piomode = sis_old_set_piomode,
589 .set_dmamode = sis_66_set_dmamode,
Alan Cox2e413f52007-03-07 16:54:24 +0000590 .cable_detect = sis_66_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400591};
592
Tejun Heo029cfd62008-03-25 12:22:49 +0900593static struct ata_port_operations sis_old_ops = {
594 .inherits = &sis_base_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400595 .set_piomode = sis_old_set_piomode,
596 .set_dmamode = sis_old_set_dmamode,
Alan Cox2e413f52007-03-07 16:54:24 +0000597 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400598};
599
Tejun Heo1626aeb2007-05-04 12:43:58 +0200600static const struct ata_port_info sis_info = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400601 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100602 .pio_mask = ATA_PIO4,
603 .mwdma_mask = ATA_MWDMA2,
604 /* No UDMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400605 .port_ops = &sis_old_ops,
606};
Tejun Heo1626aeb2007-05-04 12:43:58 +0200607static const struct ata_port_info sis_info33 = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400608 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100609 .pio_mask = ATA_PIO4,
610 .mwdma_mask = ATA_MWDMA2,
611 .udma_mask = ATA_UDMA2,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400612 .port_ops = &sis_old_ops,
613};
Tejun Heo1626aeb2007-05-04 12:43:58 +0200614static const struct ata_port_info sis_info66 = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400615 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100616 .pio_mask = ATA_PIO4,
617 /* No MWDMA */
618 .udma_mask = ATA_UDMA4,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400619 .port_ops = &sis_66_ops,
620};
Tejun Heo1626aeb2007-05-04 12:43:58 +0200621static const struct ata_port_info sis_info100 = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400622 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100623 .pio_mask = ATA_PIO4,
624 /* No MWDMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400625 .udma_mask = ATA_UDMA5,
626 .port_ops = &sis_100_ops,
627};
Tejun Heo1626aeb2007-05-04 12:43:58 +0200628static const struct ata_port_info sis_info100_early = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400629 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100630 .pio_mask = ATA_PIO4,
631 /* No MWDMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400632 .udma_mask = ATA_UDMA5,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400633 .port_ops = &sis_66_ops,
634};
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200635static const struct ata_port_info sis_info133 = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400636 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100637 .pio_mask = ATA_PIO4,
638 /* No MWDMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400639 .udma_mask = ATA_UDMA6,
640 .port_ops = &sis_133_ops,
641};
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200642const struct ata_port_info sis_info133_for_sata = {
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200643 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100644 .pio_mask = ATA_PIO4,
645 /* No MWDMA */
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200646 .udma_mask = ATA_UDMA6,
647 .port_ops = &sis_133_for_sata_ops,
648};
Tejun Heo1626aeb2007-05-04 12:43:58 +0200649static const struct ata_port_info sis_info133_early = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400650 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100651 .pio_mask = ATA_PIO4,
652 /* No MWDMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400653 .udma_mask = ATA_UDMA6,
654 .port_ops = &sis_133_early_ops,
655};
656
Alan9b14dec2007-01-08 16:11:07 +0000657/* Privately shared with the SiS180 SATA driver, not for use elsewhere */
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200658EXPORT_SYMBOL_GPL(sis_info133_for_sata);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400659
660static void sis_fixup(struct pci_dev *pdev, struct sis_chipset *sis)
661{
662 u16 regw;
663 u8 reg;
664
665 if (sis->info == &sis_info133) {
666 pci_read_config_word(pdev, 0x50, &regw);
667 if (regw & 0x08)
668 pci_write_config_word(pdev, 0x50, regw & ~0x08);
669 pci_read_config_word(pdev, 0x52, &regw);
670 if (regw & 0x08)
671 pci_write_config_word(pdev, 0x52, regw & ~0x08);
672 return;
673 }
674
675 if (sis->info == &sis_info133_early || sis->info == &sis_info100) {
676 /* Fix up latency */
677 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);
678 /* Set compatibility bit */
679 pci_read_config_byte(pdev, 0x49, &reg);
680 if (!(reg & 0x01))
681 pci_write_config_byte(pdev, 0x49, reg | 0x01);
682 return;
683 }
684
685 if (sis->info == &sis_info66 || sis->info == &sis_info100_early) {
686 /* Fix up latency */
687 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);
688 /* Set compatibility bit */
689 pci_read_config_byte(pdev, 0x52, &reg);
690 if (!(reg & 0x04))
691 pci_write_config_byte(pdev, 0x52, reg | 0x04);
692 return;
693 }
694
695 if (sis->info == &sis_info33) {
696 pci_read_config_byte(pdev, PCI_CLASS_PROG, &reg);
697 if (( reg & 0x0F ) != 0x00)
698 pci_write_config_byte(pdev, PCI_CLASS_PROG, reg & 0xF0);
699 /* Fall through to ATA16 fixup below */
700 }
701
702 if (sis->info == &sis_info || sis->info == &sis_info33) {
703 /* force per drive recovery and active timings
704 needed on ATA_33 and below chips */
705 pci_read_config_byte(pdev, 0x52, &reg);
706 if (!(reg & 0x08))
707 pci_write_config_byte(pdev, 0x52, reg|0x08);
708 return;
709 }
710
711 BUG();
712}
713
714/**
715 * sis_init_one - Register SiS ATA PCI device with kernel services
716 * @pdev: PCI device to register
717 * @ent: Entry in sis_pci_tbl matching with @pdev
718 *
719 * Called from kernel PCI layer. We probe for combined mode (sigh),
720 * and then hand over control to libata, for it to do the rest.
721 *
722 * LOCKING:
723 * Inherited from PCI layer (may sleep).
724 *
725 * RETURNS:
726 * Zero on success, or -ERRNO value.
727 */
728
729static int sis_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
730{
731 static int printed_version;
Tejun Heo887125e2008-03-25 12:22:49 +0900732 const struct ata_port_info *ppi[] = { NULL, NULL };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400733 struct pci_dev *host = NULL;
734 struct sis_chipset *chipset = NULL;
Alan Coxf3769e92007-04-19 11:09:52 +0100735 struct sis_chipset *sets;
Tejun Heof08048e2008-03-25 12:22:47 +0900736 int rc;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400737
738 static struct sis_chipset sis_chipsets[] = {
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500739
Alan Coxaf323a22006-09-12 17:15:12 +0100740 { 0x0968, &sis_info133 },
741 { 0x0966, &sis_info133 },
742 { 0x0965, &sis_info133 },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400743 { 0x0745, &sis_info100 },
744 { 0x0735, &sis_info100 },
745 { 0x0733, &sis_info100 },
746 { 0x0635, &sis_info100 },
747 { 0x0633, &sis_info100 },
748
749 { 0x0730, &sis_info100_early }, /* 100 with ATA 66 layout */
750 { 0x0550, &sis_info100_early }, /* 100 with ATA 66 layout */
751
752 { 0x0640, &sis_info66 },
753 { 0x0630, &sis_info66 },
754 { 0x0620, &sis_info66 },
755 { 0x0540, &sis_info66 },
756 { 0x0530, &sis_info66 },
757
758 { 0x5600, &sis_info33 },
759 { 0x5598, &sis_info33 },
760 { 0x5597, &sis_info33 },
761 { 0x5591, &sis_info33 },
762 { 0x5582, &sis_info33 },
763 { 0x5581, &sis_info33 },
764
765 { 0x5596, &sis_info },
766 { 0x5571, &sis_info },
767 { 0x5517, &sis_info },
768 { 0x5511, &sis_info },
769
770 {0}
771 };
772 static struct sis_chipset sis133_early = {
773 0x0, &sis_info133_early
774 };
775 static struct sis_chipset sis133 = {
776 0x0, &sis_info133
777 };
778 static struct sis_chipset sis100_early = {
779 0x0, &sis_info100_early
780 };
781 static struct sis_chipset sis100 = {
782 0x0, &sis_info100
783 };
784
785 if (!printed_version++)
786 dev_printk(KERN_DEBUG, &pdev->dev,
787 "version " DRV_VERSION "\n");
788
Tejun Heof08048e2008-03-25 12:22:47 +0900789 rc = pcim_enable_device(pdev);
790 if (rc)
791 return rc;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400792
Tejun Heof08048e2008-03-25 12:22:47 +0900793 /* We have to find the bridge first */
Alan Coxf3769e92007-04-19 11:09:52 +0100794 for (sets = &sis_chipsets[0]; sets->device; sets++) {
795 host = pci_get_device(PCI_VENDOR_ID_SI, sets->device, NULL);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400796 if (host != NULL) {
Alan Coxf3769e92007-04-19 11:09:52 +0100797 chipset = sets; /* Match found */
798 if (sets->device == 0x630) { /* SIS630 */
Auke Kok44c10132007-06-08 15:46:36 -0700799 if (host->revision >= 0x30) /* 630 ET */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400800 chipset = &sis100_early;
801 }
802 break;
803 }
804 }
805
806 /* Look for concealed bridges */
Alan Coxf3769e92007-04-19 11:09:52 +0100807 if (chipset == NULL) {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400808 /* Second check */
809 u32 idemisc;
810 u16 trueid;
811
812 /* Disable ID masking and register remapping then
813 see what the real ID is */
814
815 pci_read_config_dword(pdev, 0x54, &idemisc);
816 pci_write_config_dword(pdev, 0x54, idemisc & 0x7fffffff);
817 pci_read_config_word(pdev, PCI_DEVICE_ID, &trueid);
818 pci_write_config_dword(pdev, 0x54, idemisc);
819
820 switch(trueid) {
821 case 0x5518: /* SIS 962/963 */
822 chipset = &sis133;
823 if ((idemisc & 0x40000000) == 0) {
824 pci_write_config_dword(pdev, 0x54, idemisc | 0x40000000);
825 printk(KERN_INFO "SIS5513: Switching to 5513 register mapping\n");
826 }
827 break;
828 case 0x0180: /* SIS 965/965L */
829 chipset = &sis133;
830 break;
831 case 0x1180: /* SIS 966/966L */
832 chipset = &sis133;
833 break;
834 }
835 }
836
837 /* Further check */
838 if (chipset == NULL) {
839 struct pci_dev *lpc_bridge;
840 u16 trueid;
841 u8 prefctl;
842 u8 idecfg;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400843
844 /* Try the second unmasking technique */
845 pci_read_config_byte(pdev, 0x4a, &idecfg);
846 pci_write_config_byte(pdev, 0x4a, idecfg | 0x10);
847 pci_read_config_word(pdev, PCI_DEVICE_ID, &trueid);
848 pci_write_config_byte(pdev, 0x4a, idecfg);
849
850 switch(trueid) {
851 case 0x5517:
852 lpc_bridge = pci_get_slot(pdev->bus, 0x10); /* Bus 0 Dev 2 Fn 0 */
853 if (lpc_bridge == NULL)
854 break;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400855 pci_read_config_byte(pdev, 0x49, &prefctl);
856 pci_dev_put(lpc_bridge);
857
Auke Kok44c10132007-06-08 15:46:36 -0700858 if (lpc_bridge->revision == 0x10 && (prefctl & 0x80)) {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400859 chipset = &sis133_early;
860 break;
861 }
862 chipset = &sis100;
863 break;
864 }
865 }
866 pci_dev_put(host);
867
868 /* No chipset info, no support */
869 if (chipset == NULL)
870 return -ENODEV;
871
Tejun Heo887125e2008-03-25 12:22:49 +0900872 ppi[0] = chipset->info;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400873
874 sis_fixup(pdev, chipset);
875
Tejun Heo9363c382008-04-07 22:47:16 +0900876 return ata_pci_sff_init_one(pdev, ppi, &sis_sht, chipset);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400877}
878
Bartlomiej Zolnierkiewicz750c7132009-12-03 20:32:13 +0100879#ifdef CONFIG_PM
880static int sis_reinit_one(struct pci_dev *pdev)
881{
882 struct ata_host *host = dev_get_drvdata(&pdev->dev);
883 int rc;
884
885 rc = ata_pci_device_do_resume(pdev);
886 if (rc)
887 return rc;
888
889 sis_fixup(pdev, host->private_data);
890
891 ata_host_resume(host);
892 return 0;
893}
894#endif
895
Jeff Garzik669a5db2006-08-29 18:12:40 -0400896static const struct pci_device_id sis_pci_tbl[] = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400897 { PCI_VDEVICE(SI, 0x5513), }, /* SiS 5513 */
898 { PCI_VDEVICE(SI, 0x5518), }, /* SiS 5518 */
Uwe Kozioleka3cabb22007-06-14 23:40:43 +0200899 { PCI_VDEVICE(SI, 0x1180), }, /* SiS 1180 */
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400900
Jeff Garzik669a5db2006-08-29 18:12:40 -0400901 { }
902};
903
904static struct pci_driver sis_pci_driver = {
905 .name = DRV_NAME,
906 .id_table = sis_pci_tbl,
907 .probe = sis_init_one,
908 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900909#ifdef CONFIG_PM
Alan62d64ae2006-11-27 16:27:20 +0000910 .suspend = ata_pci_device_suspend,
Bartlomiej Zolnierkiewicz750c7132009-12-03 20:32:13 +0100911 .resume = sis_reinit_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900912#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400913};
914
915static int __init sis_init(void)
916{
917 return pci_register_driver(&sis_pci_driver);
918}
919
920static void __exit sis_exit(void)
921{
922 pci_unregister_driver(&sis_pci_driver);
923}
924
Jeff Garzik669a5db2006-08-29 18:12:40 -0400925module_init(sis_init);
926module_exit(sis_exit);
927
928MODULE_AUTHOR("Alan Cox");
929MODULE_DESCRIPTION("SCSI low-level driver for SiS ATA");
930MODULE_LICENSE("GPL");
931MODULE_DEVICE_TABLE(pci, sis_pci_tbl);
932MODULE_VERSION(DRV_VERSION);
933