2 * linux/drivers/block/alim15x3.c Version 0.06 Sept. 3, 1999
4 * Copyright (C) 1998-99 Michel Aubry, Maintainer
5 * Copyright (C) 1998-99 Andrzej Krzysztofowicz, Maintainer
7 * Copyright (C) 1998-99 Andre Hedrick (andre@suse.com)
8 * May be copied or modified under the terms of the GNU General Public License
10 * (U)DMA capable version of ali 1533/1543(C), 1535(D)
12 * version: 1.0 beta2 (Sep. 2, 1999)
13 * e-mail your problems to cjtsai@ali.com.tw
15 **********************************************************************
16 * 9/7/99 --Parts from the above author are included and need to be
17 * converted into standard interface, once I finish the thought.
20 #include <linux/config.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/pci.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/ide.h>
30 #include "ide_modes.h"
32 #define DISPLAY_ALI_TIMINGS
34 #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
35 #include <linux/stat.h>
36 #include <linux/proc_fs.h>
38 static int ali_get_info(char *buffer
, char **addr
, off_t offset
, int count
, int dummy
);
39 extern int (*ali_display_info
)(char *, char **, off_t
, int, int); /* ide-proc.c */
40 struct pci_dev
*bmide_dev
;
59 char *channel_status
[8] = {
70 static int ali_get_info(char *buffer
, char **addr
, off_t offset
, int count
, int dummy
)
72 byte reg53h
, reg5xh
, reg5yh
, reg5xh1
, reg5yh1
;
80 pci_read_config_byte(bmide_dev
, 0x08, &rev
);
81 if (rev
>= 0xc1) /* M1543C or newer */
86 /* first fetch bibma: */
87 pci_read_config_dword(bmide_dev
, 0x20, &bibma
);
88 bibma
= (bibma
& 0xfff0) ;
90 * at that point bibma+0x2 et bibma+0xa are byte
91 * registers to investigate:
93 c0
= inb((unsigned short)bibma
+ 0x02);
94 c1
= inb((unsigned short)bibma
+ 0x0a);
97 "\n Ali M15x3 Chipset.\n");
99 " ------------------\n");
100 pci_read_config_byte(bmide_dev
, 0x78, ®53h
);
101 p
+= sprintf(p
, "PCI Clock: %d.\n", reg53h
);
103 pci_read_config_byte(bmide_dev
, 0x53, ®53h
);
105 "CD_ROM FIFO:%s, CD_ROM DMA:%s\n",
106 (reg53h
& 0x02) ? "Yes" : "No ",
107 (reg53h
& 0x01) ? "Yes" : "No " );
108 pci_read_config_byte(bmide_dev
, 0x74, ®53h
);
110 "FIFO Status: contains %d Words, runs%s%s\n\n",
112 (reg53h
& 0x40) ? " OVERWR" : "",
113 (reg53h
& 0x80) ? " OVERRD." : "." );
116 "-------------------primary channel-------------------secondary channel---------\n\n");
118 pci_read_config_byte(bmide_dev
, 0x09, ®53h
);
120 "channel status: %s %s\n",
121 (reg53h
& 0x20) ? "On " : "Off",
122 (reg53h
& 0x10) ? "On " : "Off" );
125 "both channels togth: %s %s\n",
126 (c0
&0x80) ? "No " : "Yes",
127 (c1
&0x80) ? "No " : "Yes" );
129 pci_read_config_byte(bmide_dev
, 0x76, ®53h
);
131 "Channel state: %s %s\n",
132 channel_status
[reg53h
& 0x07],
133 channel_status
[(reg53h
& 0x70) >> 4] );
135 pci_read_config_byte(bmide_dev
, 0x58, ®5xh
);
136 pci_read_config_byte(bmide_dev
, 0x5c, ®5yh
);
138 "Add. Setup Timing: %dT %dT\n",
139 (reg5xh
& 0x07) ? (reg5xh
& 0x07) : 8,
140 (reg5yh
& 0x07) ? (reg5yh
& 0x07) : 8 );
142 pci_read_config_byte(bmide_dev
, 0x59, ®5xh
);
143 pci_read_config_byte(bmide_dev
, 0x5d, ®5yh
);
145 "Command Act. Count: %dT %dT\n"
146 "Command Rec. Count: %dT %dT\n\n",
147 (reg5xh
& 0x70) ? ((reg5xh
& 0x70) >> 4) : 8,
148 (reg5yh
& 0x70) ? ((reg5yh
& 0x70) >> 4) : 8,
149 (reg5xh
& 0x0f) ? (reg5xh
& 0x0f) : 16,
150 (reg5yh
& 0x0f) ? (reg5yh
& 0x0f) : 16 );
153 "----------------drive0-----------drive1------------drive0-----------drive1------\n\n");
155 "DMA enabled: %s %s %s %s\n",
156 (c0
&0x20) ? "Yes" : "No ",
157 (c0
&0x40) ? "Yes" : "No ",
158 (c1
&0x20) ? "Yes" : "No ",
159 (c1
&0x40) ? "Yes" : "No " );
161 pci_read_config_byte(bmide_dev
, 0x54, ®5xh
);
162 pci_read_config_byte(bmide_dev
, 0x55, ®5yh
);
163 q
= "FIFO threshold: %2d Words %2d Words %2d Words %2d Words\n";
165 if ((rev
== 0x20) && (pci_read_config_byte(bmide_dev
, 0x4f, &tmp
), (tmp
&= 0x20))) {
166 p
+= sprintf(p
, q
, 8, 8, 8, 8);
169 (reg5xh
& 0x03) + 12,
170 ((reg5xh
& 0x30)>>4) + 12,
171 (reg5yh
& 0x03) + 12,
172 ((reg5yh
& 0x30)>>4) + 12 );
176 (tmp
= (reg5xh
& 0x03)) ? (tmp
<< 3) : 4,
177 (tmp
= ((reg5xh
& 0x30)>>4)) ? (tmp
<< 3) : 4,
178 (tmp
= (reg5yh
& 0x03)) ? (tmp
<< 3) : 4,
179 (tmp
= ((reg5yh
& 0x30)>>4)) ? (tmp
<< 3) : 4 );
184 "FIFO threshold: %2d Words %2d Words %2d Words %2d Words\n",
185 (reg5xh
& 0x03) + 12,
186 ((reg5xh
& 0x30)>>4) + 12,
187 (reg5yh
& 0x03) + 12,
188 ((reg5yh
& 0x30)>>4) + 12 );
192 "FIFO mode: %s %s %s %s\n",
193 fifo
[((reg5xh
& 0x0c) >> 2)],
194 fifo
[((reg5xh
& 0xc0) >> 6)],
195 fifo
[((reg5yh
& 0x0c) >> 2)],
196 fifo
[((reg5yh
& 0xc0) >> 6)] );
198 pci_read_config_byte(bmide_dev
, 0x5a, ®5xh
);
199 pci_read_config_byte(bmide_dev
, 0x5b, ®5xh1
);
200 pci_read_config_byte(bmide_dev
, 0x5e, ®5yh
);
201 pci_read_config_byte(bmide_dev
, 0x5f, ®5yh1
);
204 "------------------drive0-----------drive1------------drive0-----------drive1------\n")*/
205 "Dt RW act. Cnt %2dT %2dT %2dT %2dT\n"
206 "Dt RW rec. Cnt %2dT %2dT %2dT %2dT\n\n",
207 (reg5xh
& 0x70) ? ((reg5xh
& 0x70) >> 4) : 8,
208 (reg5xh1
& 0x70) ? ((reg5xh1
& 0x70) >> 4) : 8,
209 (reg5yh
& 0x70) ? ((reg5yh
& 0x70) >> 4) : 8,
210 (reg5yh1
& 0x70) ? ((reg5yh1
& 0x70) >> 4) : 8,
211 (reg5xh
& 0x0f) ? (reg5xh
& 0x0f) : 16,
212 (reg5xh1
& 0x0f) ? (reg5xh1
& 0x0f) : 16,
213 (reg5yh
& 0x0f) ? (reg5yh
& 0x0f) : 16,
214 (reg5yh1
& 0x0f) ? (reg5yh1
& 0x0f) : 16 );
217 "-----------------------------------UDMA Timings--------------------------------\n\n");
219 pci_read_config_byte(bmide_dev
, 0x56, ®5xh
);
220 pci_read_config_byte(bmide_dev
, 0x57, ®5yh
);
222 "UDMA: %s %s %s %s\n"
223 "UDMA timings: %s %s %s %s\n\n",
224 (reg5xh
& 0x08) ? "OK" : "No",
225 (reg5xh
& 0x80) ? "OK" : "No",
226 (reg5yh
& 0x08) ? "OK" : "No",
227 (reg5yh
& 0x80) ? "OK" : "No",
228 udmaT
[(reg5xh
& 0x07)],
229 udmaT
[(reg5xh
& 0x70) >> 4],
230 udmaT
[reg5yh
& 0x07],
231 udmaT
[(reg5yh
& 0x70) >> 4] );
233 return p
-buffer
; /* => must be less than 4k! */
235 #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
237 static byte m5229_revision
= 0;
238 static byte chip_is_1543c_e
= 0;
239 static byte cable_80_pin
[2] = { 0, 0 };
242 static struct pci_dev
*isa_dev
;
244 static void ali15x3_tune_drive (ide_drive_t
*drive
, byte pio
)
247 ide_hwif_t
*hwif
= HWIF(drive
);
248 struct pci_dev
*dev
= hwif
->pci_dev
;
249 int s_time
, a_time
, c_time
;
250 byte s_clc
, a_clc
, r_clc
;
252 int bus_speed
= ide_system_bus_speed();
253 int port
= hwif
->index
? 0x5c : 0x58;
255 pio
= ide_get_best_pio_mode(drive
, pio
, 5, &d
);
256 s_time
= ide_pio_timings
[pio
].setup_time
;
257 a_time
= ide_pio_timings
[pio
].active_time
;
258 if ((s_clc
= (s_time
* bus_speed
+ 999) / 1000) >= 8)
260 if ((a_clc
= (a_time
* bus_speed
+ 999) / 1000) >= 8)
262 c_time
= ide_pio_timings
[pio
].cycle_time
;
265 if ((r_clc
= ((c_time
- s_time
- a_time
) * bus_speed
+ 999) / 1000) >= 16)
269 if (!(r_clc
= (c_time
* bus_speed
+ 999) / 1000 - a_clc
- s_clc
)) {
277 pci_write_config_byte(dev
, port
, s_clc
);
278 pci_write_config_byte(dev
, port
+drive
->select
.b
.unit
+2, (a_clc
<< 4) | r_clc
);
279 restore_flags(flags
);
283 * { 70, 165, 365 }, PIO Mode 0
284 * { 50, 125, 208 }, PIO Mode 1
285 * { 30, 100, 110 }, PIO Mode 2
286 * { 30, 80, 70 }, PIO Mode 3 with IORDY
287 * { 25, 70, 25 }, PIO Mode 4 with IORDY ns
288 * { 20, 50, 30 } PIO Mode 5 with IORDY (nonstandard)
293 static __inline__
unsigned char dma2_bits_to_command(unsigned char bits
)
296 return XFER_MW_DMA_2
;
298 return XFER_MW_DMA_1
;
299 return XFER_MW_DMA_0
;
302 static __inline__
unsigned char udma2_bits_to_command(unsigned char bits
)
315 static __inline__
int wait_for_ready(ide_drive_t
*drive
)
317 int timeout
= 20000; /* (old value: 100) */
323 * printk("STAT(%2x) ", stat);
325 if (!(stat
& BUSY_STAT
)) {
326 if ((stat
& READY_STAT
) || (stat
& ERR_STAT
)) {
335 if ((stat
& ERR_STAT
) || timeout
<= 0)
340 static void ali15x3_do_setfeature(ide_drive_t
*drive
, byte command
)
348 /* save old selected device */
349 old_select
= IN_BYTE(IDE_SELECT_REG
);
351 OUT_BYTE(drive
->select
.all
, IDE_SELECT_REG
);
353 OUT_BYTE(SETFEATURES_XFER
, IDE_FEATURE_REG
);
355 OUT_BYTE(command
, IDE_NSECTOR_REG
);
357 if(wait_for_ready(drive
)) /* "wait " */
361 OUT_BYTE(WIN_SETFEATURES
, IDE_COMMAND_REG
);
363 (void) wait_for_ready(drive
);
367 * restore to old "selected device"
369 OUT_BYTE(old_select
, IDE_SELECT_REG
);
370 restore_flags(flags
);
373 static void ali15x3_dma2_enable(ide_drive_t
*drive
, unsigned long dma_base
)
375 byte unit
= (drive
->select
.b
.unit
& 0x01);
376 byte bits
= (drive
->id
->dma_mword
| drive
->id
->dma_1word
) & 0x07;
378 ide_hwif_t
*hwif
= HWIF(drive
);
380 int m5229_udma_setting_index
= hwif
->channel
? 0x57 : 0x56;
382 ali15x3_do_setfeature(drive
, dma2_bits_to_command(bits
));
385 * clear "ultra enable" bit
387 pci_read_config_byte(hwif
->pci_dev
, m5229_udma_setting_index
, &tmpbyte
);
395 pci_write_config_byte(hwif
->pci_dev
, m5229_udma_setting_index
, tmpbyte
);
396 restore_flags(flags
);
397 drive
->id
->dma_ultra
= 0x00;
402 outb(inb(dma_base
+2)|(1<<(5+unit
)), dma_base
+2);
403 printk("ALI15X3: MultiWord DMA enabled\n");
406 static void ali15x3_udma_enable(ide_drive_t
*drive
, unsigned long dma_base
)
408 byte unit
= (drive
->select
.b
.unit
& 0x01);
409 byte bits
= drive
->id
->dma_ultra
& 0x1f;
411 ide_hwif_t
*hwif
= HWIF(drive
);
413 unsigned char udma_mode
= 0;
414 int m5229_udma_setting_index
= hwif
->channel
? 0x57 : 0x56;
418 * 00011000, disk: ultra66
420 if (m5229_revision
< 0xc2) {
422 * controller: ultra33
426 * 00000100, use ultra33, mode 2
428 drive
->id
->dma_ultra
&= ~0xFF00;
429 drive
->id
->dma_ultra
|= 0x0004;
432 * controller: ultra66
434 * Try to detect word93 bit13 and
435 * 80-pin cable (from host view)
437 if (!((drive
->id
->word93
& 0x2000) &&
438 cable_80_pin
[hwif
->channel
])) {
441 * 00000100, use ultra33, mode 2
443 drive
->id
->dma_ultra
&= ~0xFF00;
444 drive
->id
->dma_ultra
|= 0x0004;
450 * set feature regardless
452 ali15x3_do_setfeature(drive
, udma_mode
= udma2_bits_to_command(bits
));
453 udma_mode
&= 0x0f; /* get UDMA mode */
456 * Enable DMA and UltraDMA
458 outb(inb(dma_base
+2)|(1<<(5+unit
)), dma_base
+2);
462 pci_read_config_byte(hwif
->pci_dev
, m5229_udma_setting_index
, &tmpbyte
);
464 * clear bit0~3 or bit 4~7
466 tmpbyte
&= (0x0f << ((1-unit
) << 2));
468 * enable ultra dma and set timing
470 tmpbyte
|= ((0x08 | (4-udma_mode
)) << (unit
<< 2));
476 pci_write_config_byte(hwif
->pci_dev
, m5229_udma_setting_index
, tmpbyte
);
477 restore_flags(flags
);
479 if (udma_mode
>= 3) {
483 pci_read_config_byte(hwif
->pci_dev
, 0x4b, &tmpbyte
);
487 pci_write_config_byte(hwif
->pci_dev
, 0x4b, tmpbyte
);
488 restore_flags(flags
);
491 printk("ALI15X3: Ultra DMA enabled\n");
494 static int ali15x3_dma_onoff(ide_drive_t
*drive
, int enable
)
497 ide_hwif_t
*hwif
= HWIF(drive
);
498 unsigned long dma_base
= hwif
->dma_base
;
499 struct hd_driveid
*id
= drive
->id
;
501 if ((id
->field_valid
& 0x0004) &&
502 (id
->dma_ultra
& 0x001f)) {
504 * 1543C_E, in ultra mode, WDC "harddisk"
505 * will cause "CRC" errors (even if no CRC problem),
506 * so we try to use "DMA" here
508 if (m5229_revision
<= 0x20) {
510 * Normal MultiWord DMA modes.
512 ali15x3_dma2_enable(drive
, dma_base
);
513 } else if ((m5229_revision
< 0xC2) &&
514 ((drive
->media
!=ide_disk
) ||
516 strstr(id
->model
, "WDC ")))) {
518 * Normal MultiWord DMA modes.
520 ali15x3_dma2_enable(drive
, dma_base
);
523 * m5229_revision >= 0xC2 for UltraDMA modes.
525 ali15x3_udma_enable(drive
, dma_base
);
529 * Normal MultiWord DMA modes.
531 ali15x3_dma2_enable(drive
, dma_base
);
535 drive
->using_dma
= enable
; /* on, off */
539 static int ali15x3_config_drive_for_dma(ide_drive_t
*drive
)
541 struct hd_driveid
*id
= drive
->id
;
542 ide_hwif_t
*hwif
= HWIF(drive
);
544 if ((m5229_revision
<=0x20) && (drive
->media
!=ide_disk
))
545 return hwif
->dmaproc(ide_dma_off_quietly
, drive
);
547 * Even if the drive is not _currently_ in a DMA
548 * mode, we succeed, and we'll enable it manually
549 * below in alim15x3_dma_onoff
551 if ((id
!= NULL
) && (id
->capability
& 1) && hwif
->autodma
) {
552 if (id
->field_valid
& 0x0004) {
553 if (id
->dma_ultra
& 0x001F)
554 return hwif
->dmaproc(ide_dma_on
, drive
);
556 if (id
->field_valid
& 0x0002) {
557 if ((id
->dma_mword
& 0x0007) || (id
->dma_1word
& 0x0007))
558 return hwif
->dmaproc(ide_dma_on
, drive
);
561 return hwif
->dmaproc(ide_dma_off_quietly
, drive
);
564 static int ali15x3_dmaproc (ide_dma_action_t func
, ide_drive_t
*drive
)
568 return ali15x3_config_drive_for_dma(drive
);
571 case ide_dma_off_quietly
:
572 return ali15x3_dma_onoff(drive
, (func
== ide_dma_on
));
574 if ((m5229_revision
< 0xC2) && (drive
->media
!= ide_disk
))
575 return 1; /* try PIO instead of DMA */
581 return ide_dmaproc(func
, drive
); /* use standard DMA stuff */
584 unsigned int __init
pci_init_ali15x3 (struct pci_dev
*dev
, const char *name
)
587 unsigned long fixdma_base
= dev
->resource
[4].start
;
590 pci_read_config_byte(dev
, PCI_REVISION_ID
, &m5229_revision
);
592 for (isa
= pci_devices
; isa
; isa
=isa
->next
) {
594 * look for ISA bridge
596 if (isa
->vendor
== PCI_VENDOR_ID_AL
&&
597 isa
->device
== PCI_DEVICE_ID_AL_M1533
) {
603 if (!fixdma_base
|| fixdma_base
== PCI_BASE_ADDRESS_IO_MASK
) {
609 * enable DMA capable bit, and "not" simplex only
611 outb(inb(fixdma_base
+2) & 0x60, fixdma_base
+2);
613 if (inb(fixdma_base
+2) & 0x80)
614 printk("%s: simplex device: DMA will fail!!\n", name
);
618 * FIXME !!! This detection needs to be in "ata66_ali15x3()"
619 * below as a standard detection return.
622 if (m5229_revision
>= 0xC2) {
625 * 1543C-B?, 1535, 1535D, 1553
626 * Note 1: not all "motherboard" support this detection
627 * Note 2: if no udma 66 device, the detection may "error".
628 * but in this case, we will not set the device to
629 * ultra 66, the detection result is not important
635 * enable "Cable Detection", m5229, 0x4b, bit3
637 pci_read_config_byte(dev
, 0x4b, &tmpbyte
);
638 pci_write_config_byte(dev
, 0x4b, tmpbyte
| 0x08);
641 * set south-bridge's enable bit, m1533, 0x79
643 pci_read_config_byte(isa_dev
, 0x79, &tmpbyte
);
644 if (m5229_revision
== 0xC2) {
646 * 1543C-B0 (m1533, 0x79, bit 2)
648 pci_write_config_byte(isa_dev
, 0x79, tmpbyte
| 0x04);
649 } else if (m5229_revision
== 0xC3) {
651 * 1553/1535 (m1533, 0x79, bit 1)
653 pci_write_config_byte(isa_dev
, 0x79, tmpbyte
| 0x02);
655 restore_flags(flags
);
657 * Ultra66 cable detection (from Host View)
658 * m5229, 0x4a, bit0: primary, bit1: secondary 80 pin
660 pci_read_config_byte(dev
, 0x4a, &tmpbyte
);
662 * 0x4a, bit0 is 0 => primary channel
663 * has 80-pin (from host view)
665 if (!(tmpbyte
& 0x01))
668 * 0x4a, bit1 is 0 => secondary channel
669 * has 80-pin (from host view)
671 if (!(tmpbyte
& 0x02))
676 * revision 0x20 (1543-E, 1543-F)
677 * revision 0xC0, 0xC1 (1543C-C, 1543C-D, 1543C-E)
678 * clear CD-ROM DMA write bit, m5229, 0x4b, bit 7
680 pci_read_config_byte(dev
, 0x4b, &tmpbyte
);
686 pci_write_config_byte(dev
, 0x4b, tmpbyte
& 0x7F);
687 restore_flags(flags
);
690 * check m1533, 0x5e, bit 1~4 == 1001 => & 00011110 = 00010010
692 pci_read_config_byte(isa_dev
, 0x5e, &tmpbyte
);
693 chip_is_1543c_e
= ((tmpbyte
& 0x1e) == 0x12) ? 1: 0;
699 unsigned int __init
ata66_ali15x3 (ide_hwif_t
*hwif
)
703 * {0x4a,0x01,0x01}, {0x4a,0x02,0x02}
708 void __init
ide_init_ali15x3 (ide_hwif_t
*hwif
)
711 byte irq_routing_table
[] = { -1, 9, 3, 10, 4, 5, 7, 6,
712 1, 11, 0, 12, 0, 14, 0, 15 };
714 hwif
->irq
= hwif
->channel
? 15 : 14;
718 * read IDE interface control
720 pci_read_config_byte(isa_dev
, 0x58, &ideic
);
723 ideic
= ideic
& 0x03;
725 /* get IRQ for IDE Controller */
726 if ((hwif
->channel
&& ideic
== 0x03) ||
727 (!hwif
->channel
&& !ideic
)) {
729 * get SIRQ1 routing table
731 pci_read_config_byte(isa_dev
, 0x44, &inmir
);
732 inmir
= inmir
& 0x0f;
733 hwif
->irq
= irq_routing_table
[inmir
];
734 } else if (hwif
->channel
&& !(ideic
& 0x01)) {
736 * get SIRQ2 routing table
738 pci_read_config_byte(isa_dev
, 0x75, &inmir
);
739 inmir
= inmir
& 0x0f;
740 hwif
->irq
= irq_routing_table
[inmir
];
744 hwif
->tuneproc
= &ali15x3_tune_drive
;
745 if ((hwif
->dma_base
) && (m5229_revision
>= 0xC1)) {
747 * M1543C or newer for DMAing
749 hwif
->dmaproc
= &ali15x3_dmaproc
;
753 hwif
->drives
[0].autotune
= 1;
754 hwif
->drives
[1].autotune
= 1;
757 #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
759 bmide_dev
= hwif
->pci_dev
;
760 ali_display_info
= &ali_get_info
;
761 #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
766 void ide_dmacapable_ali15x3 (ide_hwif_t
*hwif
, unsigned long dmabase
)
768 if ((dmabase
) && (m5229_revision
< 0x20)) {
771 ide_setup_dma(hwif
, dmabase
, 8);