2 * linux/drivers/ide/sis5513.c Version 0.11 June 9, 2000
4 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
5 * May be copied or modified under the terms of the GNU General Public License
7 * Thanks to SIS Taiwan for direct support and hardware.
8 * Tested and designed on the SiS620/5513 chipset.
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/timer.h>
17 #include <linux/ioport.h>
18 #include <linux/blkdev.h>
19 #include <linux/hdreg.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/init.h>
24 #include <linux/ide.h>
29 #include "ide_modes.h"
31 #define DISPLAY_SIS_TIMINGS
32 #define SIS5513_DEBUG_DRIVE_INFO 0
34 static struct pci_dev
*host_dev
= NULL
;
36 #define SIS5513_FLAG_ATA_00 0x00000000
37 #define SIS5513_FLAG_ATA_16 0x00000001
38 #define SIS5513_FLAG_ATA_33 0x00000002
39 #define SIS5513_FLAG_ATA_66 0x00000004
40 #define SIS5513_FLAG_LATENCY 0x00000010
44 unsigned short host_id
;
46 } SiSHostChipInfo
[] = {
47 { "SiS530", PCI_DEVICE_ID_SI_530
, SIS5513_FLAG_ATA_66
, },
48 { "SiS540", PCI_DEVICE_ID_SI_540
, SIS5513_FLAG_ATA_66
, },
49 { "SiS620", PCI_DEVICE_ID_SI_620
, SIS5513_FLAG_ATA_66
|SIS5513_FLAG_LATENCY
, },
50 { "SiS630", PCI_DEVICE_ID_SI_630
, SIS5513_FLAG_ATA_66
|SIS5513_FLAG_LATENCY
, },
51 { "SiS5591", PCI_DEVICE_ID_SI_5591
, SIS5513_FLAG_ATA_33
, },
52 { "SiS5597", PCI_DEVICE_ID_SI_5597
, SIS5513_FLAG_ATA_33
, },
53 { "SiS5600", PCI_DEVICE_ID_SI_5600
, SIS5513_FLAG_ATA_33
, },
54 { "SiS5511", PCI_DEVICE_ID_SI_5511
, SIS5513_FLAG_ATA_16
, },
59 static struct _pio_mode_mapping
{
63 } pio_mode_mapping
[] = {
71 static struct _dma_mode_mapping
{
75 } dma_mode_mapping
[] = {
81 static struct _udma_mode_mapping
{
84 } udma_mode_mapping
[] = {
93 static __inline__
char * find_udma_mode (byte cycle_time
)
97 for (n
= 0; n
<= 4; n
++)
98 if (udma_mode_mapping
[n
].cycle_time
<= cycle_time
)
99 return udma_mode_mapping
[n
].udma_mode
;
100 return udma_mode_mapping
[4].udma_mode
;
104 #if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
105 #include <linux/stat.h>
106 #include <linux/proc_fs.h>
108 static int sis_get_info(char *, char **, off_t
, int);
109 extern int (*sis_display_info
)(char *, char **, off_t
, int); /* ide-proc.c */
110 static struct pci_dev
*bmide_dev
;
112 static char *cable_type
[] = {
117 static char *recovery_time
[] ={
118 "12 PCICLK", "1 PCICLK",
119 "2 PCICLK", "3 PCICLK",
120 "4 PCICLK", "5 PCICLCK",
121 "6 PCICLK", "7 PCICLCK",
122 "8 PCICLK", "9 PCICLCK",
123 "10 PCICLK", "11 PCICLK",
124 "13 PCICLK", "14 PCICLK",
125 "15 PCICLK", "15 PCICLK"
128 static char * cycle_time
[] = {
129 "Undefined", "2 CLCK",
135 static char * active_time
[] = {
136 "8 PCICLK", "1 PCICLCK",
137 "2 PCICLK", "2 PCICLK",
138 "4 PCICLK", "5 PCICLK",
139 "6 PCICLK", "12 PCICLK"
142 static int sis_get_info (char *buffer
, char **addr
, off_t offset
, int count
)
149 p
+= sprintf(p
, "--------------- Primary Channel ---------------- Secondary Channel -------------\n");
150 rc
= pci_read_config_byte(bmide_dev
, 0x4a, ®
);
151 p
+= sprintf(p
, "Channel Status: %s \t \t \t \t %s \n",
152 (reg
& 0x02) ? "On" : "Off",
153 (reg
& 0x04) ? "On" : "Off");
155 rc
= pci_read_config_byte(bmide_dev
, 0x09, ®
);
156 p
+= sprintf(p
, "Operation Mode: %s \t \t \t %s \n",
157 (reg
& 0x01) ? "Native" : "Compatible",
158 (reg
& 0x04) ? "Native" : "Compatible");
160 rc
= pci_read_config_byte(bmide_dev
, 0x48, ®
);
161 p
+= sprintf(p
, "Cable Type: %s \t \t \t %s\n",
162 (reg
& 0x10) ? cable_type
[1] : cable_type
[0],
163 (reg
& 0x20) ? cable_type
[1] : cable_type
[0]);
165 rc
= pci_read_config_word(bmide_dev
, 0x4c, ®2
);
166 rc
= pci_read_config_word(bmide_dev
, 0x4e, ®3
);
167 p
+= sprintf(p
, "Prefetch Count: %d \t \t \t \t %d\n",
170 rc
= pci_read_config_byte(bmide_dev
, 0x4b, ®
);
171 p
+= sprintf(p
, "Drive 0: Postwrite %s \t \t Postwrite %s\n",
172 (reg
& 0x10) ? "Enabled" : "Disabled",
173 (reg
& 0x40) ? "Enabled" : "Disabled");
174 p
+= sprintf(p
, " Prefetch %s \t \t Prefetch %s\n",
175 (reg
& 0x01) ? "Enabled" : "Disabled",
176 (reg
& 0x04) ? "Enabled" : "Disabled");
178 rc
= pci_read_config_byte(bmide_dev
, 0x41, ®
);
179 rc
= pci_read_config_byte(bmide_dev
, 0x45, ®1
);
180 p
+= sprintf(p
, " UDMA %s \t \t \t UDMA %s\n",
181 (reg
& 0x80) ? "Enabled" : "Disabled",
182 (reg1
& 0x80) ? "Enabled" : "Disabled");
183 p
+= sprintf(p
, " UDMA Cycle Time %s \t UDMA Cycle Time %s\n",
184 cycle_time
[(reg
& 0x70) >> 4], cycle_time
[(reg1
& 0x70) >> 4]);
185 p
+= sprintf(p
, " Data Active Time %s \t Data Active Time %s\n",
186 active_time
[(reg
& 0x07)], active_time
[(reg1
&0x07)] );
188 rc
= pci_read_config_byte(bmide_dev
, 0x40, ®
);
189 rc
= pci_read_config_byte(bmide_dev
, 0x44, ®1
);
190 p
+= sprintf(p
, " Data Recovery Time %s \t Data Recovery Time %s\n",
191 recovery_time
[(reg
& 0x0f)], recovery_time
[(reg1
& 0x0f)]);
194 rc
= pci_read_config_byte(bmide_dev
, 0x4b, ®
);
195 p
+= sprintf(p
, "Drive 1: Postwrite %s \t \t Postwrite %s\n",
196 (reg
& 0x20) ? "Enabled" : "Disabled",
197 (reg
& 0x80) ? "Enabled" : "Disabled");
198 p
+= sprintf(p
, " Prefetch %s \t \t Prefetch %s\n",
199 (reg
& 0x02) ? "Enabled" : "Disabled",
200 (reg
& 0x08) ? "Enabled" : "Disabled");
202 rc
= pci_read_config_byte(bmide_dev
, 0x43, ®
);
203 rc
= pci_read_config_byte(bmide_dev
, 0x47, ®1
);
204 p
+= sprintf(p
, " UDMA %s \t \t \t UDMA %s\n",
205 (reg
& 0x80) ? "Enabled" : "Disabled",
206 (reg1
& 0x80) ? "Enabled" : "Disabled");
207 p
+= sprintf(p
, " UDMA Cycle Time %s \t UDMA Cycle Time %s\n",
208 cycle_time
[(reg
& 0x70) >> 4], cycle_time
[(reg1
& 0x70) >> 4]);
209 p
+= sprintf(p
, " Data Active Time %s \t Data Active Time %s\n",
210 active_time
[(reg
& 0x07)], active_time
[(reg1
&0x07)] );
212 rc
= pci_read_config_byte(bmide_dev
, 0x42, ®
);
213 rc
= pci_read_config_byte(bmide_dev
, 0x46, ®1
);
214 p
+= sprintf(p
, " Data Recovery Time %s \t Data Recovery Time %s\n",
215 recovery_time
[(reg
& 0x0f)], recovery_time
[(reg1
& 0x0f)]);
218 #endif /* defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS) */
221 extern char *ide_xfer_verbose (byte xfer_rate
);
223 static void config_drive_art_rwp (ide_drive_t
*drive
)
225 ide_hwif_t
*hwif
= HWIF(drive
);
226 struct pci_dev
*dev
= hwif
->pci_dev
;
229 byte rw_prefetch
= (0x11 << drive
->dn
);
231 pci_read_config_byte(dev
, 0x4b, ®4bh
);
232 if (drive
->media
!= ide_disk
)
235 if ((reg4bh
& rw_prefetch
) != rw_prefetch
)
236 pci_write_config_byte(dev
, 0x4b, reg4bh
|rw_prefetch
);
239 static void config_art_rwp_pio (ide_drive_t
*drive
, byte pio
)
241 ide_hwif_t
*hwif
= HWIF(drive
);
242 struct pci_dev
*dev
= hwif
->pci_dev
;
244 byte timing
, drive_pci
, test1
, test2
;
246 unsigned short eide_pio_timing
[6] = {600, 390, 240, 180, 120, 90};
247 unsigned short xfer_pio
= drive
->id
->eide_pio_modes
;
249 config_drive_art_rwp(drive
);
250 pio
= ide_get_best_pio_mode(drive
, 255, pio
, NULL
);
255 if (drive
->id
->eide_pio_iordy
> 0) {
258 drive
->id
->eide_pio_iordy
>eide_pio_timing
[xfer_pio
];
261 xfer_pio
= (drive
->id
->eide_pio_modes
& 4) ? 0x05 :
262 (drive
->id
->eide_pio_modes
& 2) ? 0x04 :
263 (drive
->id
->eide_pio_modes
& 1) ? 0x03 : xfer_pio
;
266 timing
= (xfer_pio
>= pio
) ? xfer_pio
: pio
;
269 * Mode 0 Mode 1 Mode 2 Mode 3 Mode 4
270 * Active time 8T (240ns) 6T (180ns) 4T (120ns) 3T (90ns) 3T (90ns)
271 * 0x41 2:0 bits 000 110 100 011 011
272 * Recovery time 12T (360ns) 7T (210ns) 4T (120ns) 3T (90ns) 1T (30ns)
273 * 0x40 3:0 bits 0000 0111 0100 0011 0001
274 * Cycle time 20T (600ns) 13T (390ns) 8T (240ns) 6T (180ns) 4T (120ns)
278 case 0: drive_pci
= 0x40; break;
279 case 1: drive_pci
= 0x42; break;
280 case 2: drive_pci
= 0x44; break;
281 case 3: drive_pci
= 0x46; break;
285 pci_read_config_byte(dev
, drive_pci
, &test1
);
286 pci_read_config_byte(dev
, drive_pci
|0x01, &test2
);
289 * Do a blanket clear of active and recovery timings.
296 case 4: test1
|= 0x01; test2
|= 0x03; break;
297 case 3: test1
|= 0x03; test2
|= 0x03; break;
298 case 2: test1
|= 0x04; test2
|= 0x04; break;
299 case 1: test1
|= 0x07; test2
|= 0x06; break;
303 pci_write_config_byte(dev
, drive_pci
, test1
);
304 pci_write_config_byte(dev
, drive_pci
|0x01, test2
);
307 static int config_chipset_for_pio (ide_drive_t
*drive
, byte pio
)
313 case 4: speed
= XFER_PIO_4
; break;
314 case 3: speed
= XFER_PIO_3
; break;
315 case 2: speed
= XFER_PIO_2
; break;
316 case 1: speed
= XFER_PIO_1
; break;
317 default: speed
= XFER_PIO_0
; break;
320 config_art_rwp_pio(drive
, pio
);
321 drive
->current_speed
= speed
;
322 err
= ide_config_drive_speed(drive
, speed
);
326 static int sis5513_tune_chipset (ide_drive_t
*drive
, byte speed
)
328 ide_hwif_t
*hwif
= HWIF(drive
);
329 struct pci_dev
*dev
= hwif
->pci_dev
;
331 byte drive_pci
, test1
, test2
;
332 byte unmask
, four_two
, mask
= 0;
335 switch(host_dev
->device
) {
336 case PCI_DEVICE_ID_SI_530
:
337 case PCI_DEVICE_ID_SI_540
:
338 case PCI_DEVICE_ID_SI_620
:
339 case PCI_DEVICE_ID_SI_630
:
354 case 0: drive_pci
= 0x40;break;
355 case 1: drive_pci
= 0x42;break;
356 case 2: drive_pci
= 0x44;break;
357 case 3: drive_pci
= 0x46;break;
358 default: return ide_dma_off
;
361 pci_read_config_byte(dev
, drive_pci
, &test1
);
362 pci_read_config_byte(dev
, drive_pci
|0x01, &test2
);
364 if ((speed
<= XFER_MW_DMA_2
) && (test2
& 0x80)) {
365 pci_write_config_byte(dev
, drive_pci
|0x01, test2
& ~0x80);
366 pci_read_config_byte(dev
, drive_pci
|0x01, &test2
);
368 pci_write_config_byte(dev
, drive_pci
|0x01, test2
& ~unmask
);
372 #ifdef CONFIG_BLK_DEV_IDEDMA
373 case XFER_UDMA_5
: /* can not do ultra mode 5 yet */
374 case XFER_UDMA_4
: mask
= 0x90; break;
375 case XFER_UDMA_3
: mask
= 0xA0; break;
376 case XFER_UDMA_2
: mask
= (four_two
) ? 0xB0 : 0xA0; break;
377 case XFER_UDMA_1
: mask
= (four_two
) ? 0xD0 : 0xC0; break;
378 case XFER_UDMA_0
: mask
= unmask
; break;
384 case XFER_SW_DMA_0
: break;
385 #endif /* CONFIG_BLK_DEV_IDEDMA */
386 case XFER_PIO_4
: return((int) config_chipset_for_pio(drive
, 4));
387 case XFER_PIO_3
: return((int) config_chipset_for_pio(drive
, 3));
388 case XFER_PIO_2
: return((int) config_chipset_for_pio(drive
, 2));
389 case XFER_PIO_1
: return((int) config_chipset_for_pio(drive
, 1));
391 default: return((int) config_chipset_for_pio(drive
, 0));
394 if (speed
> XFER_MW_DMA_2
)
395 pci_write_config_byte(dev
, drive_pci
|0x01, test2
|mask
);
397 drive
->current_speed
= speed
;
398 return ((int) ide_config_drive_speed(drive
, speed
));
401 static void sis5513_tune_drive (ide_drive_t
*drive
, byte pio
)
403 (void) config_chipset_for_pio(drive
, pio
);
406 #ifdef CONFIG_BLK_DEV_IDEDMA
408 * ((id->hw_config & 0x4000|0x2000) && (HWIF(drive)->udma_four))
410 static int config_chipset_for_dma (ide_drive_t
*drive
, byte ultra
)
412 struct hd_driveid
*id
= drive
->id
;
413 ide_hwif_t
*hwif
= HWIF(drive
);
415 byte four_two
= 0, speed
= 0;
418 byte unit
= (drive
->select
.b
.unit
& 0x01);
419 byte udma_66
= eighty_ninty_three(drive
);
422 switch(host_dev
->device
) {
423 case PCI_DEVICE_ID_SI_530
:
424 case PCI_DEVICE_ID_SI_540
:
425 case PCI_DEVICE_ID_SI_620
:
426 case PCI_DEVICE_ID_SI_630
:
427 four_two
= 0x01; break;
429 four_two
= 0x00; break;
433 if ((id
->dma_ultra
& 0x0010) && (ultra
) && (udma_66
) && (four_two
))
435 else if ((id
->dma_ultra
& 0x0008) && (ultra
) && (udma_66
) && (four_two
))
437 else if ((id
->dma_ultra
& 0x0004) && (ultra
))
439 else if ((id
->dma_ultra
& 0x0002) && (ultra
))
441 else if ((id
->dma_ultra
& 0x0001) && (ultra
))
443 else if (id
->dma_mword
& 0x0004)
444 speed
= XFER_MW_DMA_2
;
445 else if (id
->dma_mword
& 0x0002)
446 speed
= XFER_MW_DMA_1
;
447 else if (id
->dma_mword
& 0x0001)
448 speed
= XFER_MW_DMA_0
;
449 else if (id
->dma_1word
& 0x0004)
450 speed
= XFER_SW_DMA_2
;
451 else if (id
->dma_1word
& 0x0002)
452 speed
= XFER_SW_DMA_1
;
453 else if (id
->dma_1word
& 0x0001)
454 speed
= XFER_SW_DMA_0
;
456 return ((int) ide_dma_off_quietly
);
458 outb(inb(hwif
->dma_base
+2)|(1<<(5+unit
)), hwif
->dma_base
+2);
460 err
= sis5513_tune_chipset(drive
, speed
);
462 #if SIS5513_DEBUG_DRIVE_INFO
463 printk("%s: %s drive%d\n", drive
->name
, ide_xfer_verbose(speed
), drive
->dn
);
464 #endif /* SIS5513_DEBUG_DRIVE_INFO */
466 return ((int) ((id
->dma_ultra
>> 11) & 3) ? ide_dma_on
:
467 ((id
->dma_ultra
>> 8) & 7) ? ide_dma_on
:
468 ((id
->dma_mword
>> 8) & 7) ? ide_dma_on
:
469 ((id
->dma_1word
>> 8) & 7) ? ide_dma_on
:
470 ide_dma_off_quietly
);
473 static int config_drive_xfer_rate (ide_drive_t
*drive
)
475 struct hd_driveid
*id
= drive
->id
;
476 ide_dma_action_t dma_func
= ide_dma_off_quietly
;
478 if (id
&& (id
->capability
& 1) && HWIF(drive
)->autodma
) {
479 /* Consult the list of known "bad" drives */
480 if (ide_dmaproc(ide_dma_bad_drive
, drive
)) {
481 dma_func
= ide_dma_off
;
484 dma_func
= ide_dma_off_quietly
;
485 if (id
->field_valid
& 4) {
486 if (id
->dma_ultra
& 0x001F) {
487 /* Force if Capable UltraDMA */
488 dma_func
= config_chipset_for_dma(drive
, 1);
489 if ((id
->field_valid
& 2) &&
490 (dma_func
!= ide_dma_on
))
493 } else if (id
->field_valid
& 2) {
495 if ((id
->dma_mword
& 0x0007) ||
496 (id
->dma_1word
& 0x0007)) {
497 /* Force if Capable regular DMA modes */
498 dma_func
= config_chipset_for_dma(drive
, 0);
499 if (dma_func
!= ide_dma_on
)
502 } else if ((ide_dmaproc(ide_dma_good_drive
, drive
)) &&
503 (id
->eide_dma_time
> 150)) {
504 /* Consult the list of known "good" drives */
505 dma_func
= config_chipset_for_dma(drive
, 0);
506 if (dma_func
!= ide_dma_on
)
511 } else if ((id
->capability
& 8) || (id
->field_valid
& 2)) {
513 dma_func
= ide_dma_off_quietly
;
515 (void) config_chipset_for_pio(drive
, 5);
518 return HWIF(drive
)->dmaproc(dma_func
, drive
);
522 * sis5513_dmaproc() initiates/aborts (U)DMA read/write operations on a drive.
524 int sis5513_dmaproc (ide_dma_action_t func
, ide_drive_t
*drive
)
528 config_drive_art_rwp(drive
);
529 config_art_rwp_pio(drive
, 5);
530 return config_drive_xfer_rate(drive
);
534 return ide_dmaproc(func
, drive
); /* use standard DMA stuff */
536 #endif /* CONFIG_BLK_DEV_IDEDMA */
538 unsigned int __init
pci_init_sis5513 (struct pci_dev
*dev
, const char *name
)
540 struct pci_dev
*host
;
544 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &latency
);
546 for (i
= 0; i
< ARRAY_SIZE (SiSHostChipInfo
) && !host_dev
; i
++) {
547 host
= pci_find_device (PCI_VENDOR_ID_SI
,
548 SiSHostChipInfo
[i
].host_id
,
554 printk(SiSHostChipInfo
[i
].name
);
556 if (SiSHostChipInfo
[i
].flags
& SIS5513_FLAG_LATENCY
) {
558 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0x10);
565 pci_read_config_byte(dev
, 0x52, ®52h
);
566 if (!(reg52h
& 0x04)) {
567 /* set IDE controller to operate in Compabitility mode only */
568 pci_write_config_byte(dev
, 0x52, reg52h
|0x04);
570 #if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
574 sis_display_info
= &sis_get_info
;
576 #endif /* defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS) */
581 unsigned int __init
ata66_sis5513 (ide_hwif_t
*hwif
)
583 byte reg48h
= 0, ata66
= 0;
584 byte mask
= hwif
->channel
? 0x20 : 0x10;
585 pci_read_config_byte(hwif
->pci_dev
, 0x48, ®48h
);
588 switch(host_dev
->device
) {
589 case PCI_DEVICE_ID_SI_530
:
590 case PCI_DEVICE_ID_SI_540
:
591 case PCI_DEVICE_ID_SI_620
:
592 case PCI_DEVICE_ID_SI_630
:
593 ata66
= (reg48h
& mask
) ? 0 : 1;
601 void __init
ide_init_sis5513 (ide_hwif_t
*hwif
)
604 hwif
->irq
= hwif
->channel
? 15 : 14;
606 hwif
->tuneproc
= &sis5513_tune_drive
;
607 hwif
->speedproc
= &sis5513_tune_chipset
;
609 if (!(hwif
->dma_base
))
613 switch(host_dev
->device
) {
614 #ifdef CONFIG_BLK_DEV_IDEDMA
615 case PCI_DEVICE_ID_SI_530
:
616 case PCI_DEVICE_ID_SI_540
:
617 case PCI_DEVICE_ID_SI_620
:
618 case PCI_DEVICE_ID_SI_630
:
619 case PCI_DEVICE_ID_SI_5600
:
620 case PCI_DEVICE_ID_SI_5597
:
621 case PCI_DEVICE_ID_SI_5591
:
623 hwif
->dmaproc
= &sis5513_dmaproc
;
625 #endif /* CONFIG_BLK_DEV_IDEDMA */