2 * linux/drivers/block/hpt34x.c Version 0.29 Feb. 10, 2000
4 * Copyright (C) 1998-2000 Andre Hedrick (andre@suse.com)
5 * May be copied or modified under the terms of the GNU General Public License
8 * 00:12.0 Unknown mass storage controller:
9 * Triones Technologies, Inc.
10 * Unknown device 0003 (rev 01)
12 * hde: UDMA 2 (0x0000 0x0002) (0x0000 0x0010)
13 * hdf: UDMA 2 (0x0002 0x0012) (0x0010 0x0030)
14 * hde: DMA 2 (0x0000 0x0002) (0x0000 0x0010)
15 * hdf: DMA 2 (0x0002 0x0012) (0x0010 0x0030)
16 * hdg: DMA 1 (0x0012 0x0052) (0x0030 0x0070)
17 * hdh: DMA 1 (0x0052 0x0252) (0x0070 0x00f0)
21 * Since there are two cards that report almost identically,
22 * the only discernable difference is the values reported in pcicmd.
23 * Booting-BIOS card or HPT363 :: pcicmd == 0x07
24 * Non-bootable card or HPT343 :: pcicmd == 0x05
27 #include <linux/config.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/timer.h>
33 #include <linux/ioport.h>
34 #include <linux/blkdev.h>
35 #include <linux/hdreg.h>
36 #include <linux/interrupt.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/ide.h>
43 #include "ide_modes.h"
46 #define SPLIT_BYTE(B,H,L) ((H)=(B>>4), (L)=(B-((B>>4)<<4)))
49 #define HPT343_DEBUG_DRIVE_INFO 1
51 #define DISPLAY_HPT34X_TIMINGS
52 #if defined(DISPLAY_HPT34X_TIMINGS) && defined(CONFIG_PROC_FS)
53 #include <linux/stat.h>
54 #include <linux/proc_fs.h>
56 static int hpt34x_get_info(char *, char **, off_t
, int);
57 extern int (*hpt34x_display_info
)(char *, char **, off_t
, int); /* ide-proc.c */
58 extern char *ide_media_verbose(ide_drive_t
*);
59 static struct pci_dev
*bmide_dev
;
61 static int hpt34x_get_info (char *buffer
, char **addr
, off_t offset
, int count
)
64 u32 bibma
= bmide_dev
->resource
[4].start
;
68 * at that point bibma+0x2 et bibma+0xa are byte registers
71 c0
= inb_p((unsigned short)bibma
+ 0x02);
72 c1
= inb_p((unsigned short)bibma
+ 0x0a);
74 p
+= sprintf(p
, "\n HPT34X Chipset.\n");
75 p
+= sprintf(p
, "--------------- Primary Channel ---------------- Secondary Channel -------------\n");
76 p
+= sprintf(p
, " %sabled %sabled\n",
77 (c0
&0x80) ? "dis" : " en",
78 (c1
&0x80) ? "dis" : " en");
79 p
+= sprintf(p
, "--------------- drive0 --------- drive1 -------- drive0 ---------- drive1 ------\n");
80 p
+= sprintf(p
, "DMA enabled: %s %s %s %s\n",
81 (c0
&0x20) ? "yes" : "no ", (c0
&0x40) ? "yes" : "no ",
82 (c1
&0x20) ? "yes" : "no ", (c1
&0x40) ? "yes" : "no " );
84 p
+= sprintf(p
, "UDMA\n");
85 p
+= sprintf(p
, "DMA\n");
86 p
+= sprintf(p
, "PIO\n");
88 return p
-buffer
; /* => must be less than 4k! */
90 #endif /* defined(DISPLAY_HPT34X_TIMINGS) && defined(CONFIG_PROC_FS) */
94 extern char *ide_xfer_verbose (byte xfer_rate
);
96 static void hpt34x_clear_chipset (ide_drive_t
*drive
)
98 int drive_number
= ((HWIF(drive
)->channel
? 2 : 0) + (drive
->select
.b
.unit
& 0x01));
99 unsigned int reg1
= 0, tmp1
= 0;
100 unsigned int reg2
= 0, tmp2
= 0;
102 pci_read_config_dword(HWIF(drive
)->pci_dev
, 0x44, ®1
);
103 pci_read_config_dword(HWIF(drive
)->pci_dev
, 0x48, ®2
);
104 tmp1
= ((0x00 << (3*drive_number
)) | (reg1
& ~(7 << (3*drive_number
))));
105 tmp2
= (reg2
& ~(0x11 << drive_number
));
106 pci_write_config_dword(HWIF(drive
)->pci_dev
, 0x44, tmp1
);
107 pci_write_config_dword(HWIF(drive
)->pci_dev
, 0x48, tmp2
);
110 static int hpt34x_tune_chipset (ide_drive_t
*drive
, byte speed
)
113 byte hi_speed
, lo_speed
;
114 int drive_number
= ((HWIF(drive
)->channel
? 2 : 0) + (drive
->select
.b
.unit
& 0x01));
115 unsigned int reg1
= 0, tmp1
= 0;
116 unsigned int reg2
= 0, tmp2
= 0;
118 SPLIT_BYTE(speed
, hi_speed
, lo_speed
);
121 hi_speed
= (hi_speed
& 4) ? 0x01 : 0x10;
127 pci_read_config_dword(HWIF(drive
)->pci_dev
, 0x44, ®1
);
128 pci_read_config_dword(HWIF(drive
)->pci_dev
, 0x48, ®2
);
129 tmp1
= ((lo_speed
<< (3*drive_number
)) | (reg1
& ~(7 << (3*drive_number
))));
130 tmp2
= ((hi_speed
<< drive_number
) | reg2
);
131 err
= ide_config_drive_speed(drive
, speed
);
132 pci_write_config_dword(HWIF(drive
)->pci_dev
, 0x44, tmp1
);
133 pci_write_config_dword(HWIF(drive
)->pci_dev
, 0x48, tmp2
);
135 #if HPT343_DEBUG_DRIVE_INFO
136 printk("%s: %s drive%d (0x%04x 0x%04x) (0x%04x 0x%04x)" \
137 " (0x%02x 0x%02x) 0x%04x\n",
138 drive
->name
, ide_xfer_verbose(speed
),
139 drive_number
, reg1
, tmp1
, reg2
, tmp2
,
140 hi_speed
, lo_speed
, err
);
141 #endif /* HPT343_DEBUG_DRIVE_INFO */
147 * This allows the configuration of ide_pci chipset registers
148 * for cards that learn about the drive's UDMA, DMA, PIO capabilities
149 * after the drive is reported by the OS. Initally for designed for
150 * HPT343 UDMA chipset by HighPoint|Triones Technologies, Inc.
152 static int config_chipset_for_dma (ide_drive_t
*drive
, byte ultra
)
154 struct hd_driveid
*id
= drive
->id
;
157 if (drive
->media
!= ide_disk
)
158 return ((int) ide_dma_off_quietly
);
160 hpt34x_clear_chipset(drive
);
162 if ((id
->dma_ultra
& 0x0010) && ultra
) {
164 } else if ((id
->dma_ultra
& 0x0008) && ultra
) {
166 } else if ((id
->dma_ultra
& 0x0004) && ultra
) {
168 } else if ((id
->dma_ultra
& 0x0002) && ultra
) {
170 } else if ((id
->dma_ultra
& 0x0001) && ultra
) {
172 } else if (id
->dma_mword
& 0x0004) {
173 speed
= XFER_MW_DMA_2
;
174 } else if (id
->dma_mword
& 0x0002) {
175 speed
= XFER_MW_DMA_1
;
176 } else if (id
->dma_mword
& 0x0001) {
177 speed
= XFER_MW_DMA_0
;
178 } else if (id
->dma_1word
& 0x0004) {
179 speed
= XFER_SW_DMA_2
;
180 } else if (id
->dma_1word
& 0x0002) {
181 speed
= XFER_SW_DMA_1
;
182 } else if (id
->dma_1word
& 0x0001) {
183 speed
= XFER_SW_DMA_0
;
185 return ((int) ide_dma_off_quietly
);
188 (void) hpt34x_tune_chipset(drive
, speed
);
190 return ((int) ((id
->dma_ultra
>> 11) & 3) ? ide_dma_off
:
191 ((id
->dma_ultra
>> 8) & 7) ? ide_dma_on
:
192 ((id
->dma_mword
>> 8) & 7) ? ide_dma_on
:
193 ((id
->dma_1word
>> 8) & 7) ? ide_dma_on
:
194 ide_dma_off_quietly
);
197 static void config_chipset_for_pio (ide_drive_t
*drive
)
199 unsigned short eide_pio_timing
[6] = {960, 480, 240, 180, 120, 90};
200 unsigned short xfer_pio
= drive
->id
->eide_pio_modes
;
202 byte timing
, speed
, pio
;
204 pio
= ide_get_best_pio_mode(drive
, 255, 5, NULL
);
209 if (drive
->id
->eide_pio_iordy
> 0) {
212 drive
->id
->eide_pio_iordy
>eide_pio_timing
[xfer_pio
];
215 xfer_pio
= (drive
->id
->eide_pio_modes
& 4) ? 0x05 :
216 (drive
->id
->eide_pio_modes
& 2) ? 0x04 :
217 (drive
->id
->eide_pio_modes
& 1) ? 0x03 : xfer_pio
;
220 timing
= (xfer_pio
>= pio
) ? xfer_pio
: pio
;
223 case 4: speed
= XFER_PIO_4
;break;
224 case 3: speed
= XFER_PIO_3
;break;
225 case 2: speed
= XFER_PIO_2
;break;
226 case 1: speed
= XFER_PIO_1
;break;
228 speed
= (!drive
->id
->tPIO
) ? XFER_PIO_0
: XFER_PIO_SLOW
;
231 (void) hpt34x_tune_chipset(drive
, speed
);
234 static void hpt34x_tune_drive (ide_drive_t
*drive
, byte pio
)
239 case 4: speed
= XFER_PIO_4
;break;
240 case 3: speed
= XFER_PIO_3
;break;
241 case 2: speed
= XFER_PIO_2
;break;
242 case 1: speed
= XFER_PIO_1
;break;
243 default: speed
= XFER_PIO_0
;break;
245 hpt34x_clear_chipset(drive
);
246 (void) hpt34x_tune_chipset(drive
, speed
);
249 static int config_drive_xfer_rate (ide_drive_t
*drive
)
251 struct hd_driveid
*id
= drive
->id
;
252 ide_dma_action_t dma_func
= ide_dma_on
;
254 if (id
&& (id
->capability
& 1) && HWIF(drive
)->autodma
) {
255 /* Consult the list of known "bad" drives */
256 if (ide_dmaproc(ide_dma_bad_drive
, drive
)) {
257 dma_func
= ide_dma_off
;
260 dma_func
= ide_dma_off_quietly
;
261 if (id
->field_valid
& 4) {
262 if (id
->dma_ultra
& 0x0007) {
263 /* Force if Capable UltraDMA */
264 dma_func
= config_chipset_for_dma(drive
, 1);
265 if ((id
->field_valid
& 2) &&
266 (dma_func
!= ide_dma_on
))
269 } else if (id
->field_valid
& 2) {
271 if ((id
->dma_mword
& 0x0007) ||
272 (id
->dma_1word
& 0x0007)) {
273 /* Force if Capable regular DMA modes */
274 dma_func
= config_chipset_for_dma(drive
, 0);
275 if (dma_func
!= ide_dma_on
)
278 } else if (ide_dmaproc(ide_dma_good_drive
, drive
)) {
279 if (id
->eide_dma_time
> 150) {
282 /* Consult the list of known "good" drives */
283 dma_func
= config_chipset_for_dma(drive
, 0);
284 if (dma_func
!= ide_dma_on
)
289 } else if ((id
->capability
& 8) || (id
->field_valid
& 2)) {
291 dma_func
= ide_dma_off_quietly
;
293 config_chipset_for_pio(drive
);
296 #ifndef CONFIG_HPT34X_AUTODMA
297 if (dma_func
== ide_dma_on
)
298 dma_func
= ide_dma_off
;
299 #endif /* CONFIG_HPT34X_AUTODMA */
301 return HWIF(drive
)->dmaproc(dma_func
, drive
);
305 * hpt34x_dmaproc() initiates/aborts (U)DMA read/write operations on a drive.
307 * This is specific to the HPT343 UDMA bios-less chipset
308 * and HPT345 UDMA bios chipset (stamped HPT363)
309 * by HighPoint|Triones Technologies, Inc.
312 int hpt34x_dmaproc (ide_dma_action_t func
, ide_drive_t
*drive
)
314 ide_hwif_t
*hwif
= HWIF(drive
);
315 unsigned long dma_base
= hwif
->dma_base
;
316 unsigned int count
, reading
= 0;
321 return config_drive_xfer_rate(drive
);
325 if (!(count
= ide_build_dmatable(drive
, func
)))
326 return 1; /* try PIO instead of DMA */
327 outl(hwif
->dmatable_dma
, dma_base
+ 4); /* PRD table */
329 outb(reading
, dma_base
); /* specify r/w */
330 outb(inb(dma_base
+2)|6, dma_base
+2); /* clear INTR & ERROR flags */
331 drive
->waiting_for_dma
= 1;
332 if (drive
->media
!= ide_disk
)
334 ide_set_handler(drive
, &ide_dma_intr
, WAIT_CMD
, NULL
); /* issue cmd to drive */
335 OUT_BYTE((reading
== 9) ? WIN_READDMA
: WIN_WRITEDMA
, IDE_COMMAND_REG
);
337 case ide_dma_end
: /* returns 1 on error, 0 otherwise */
338 drive
->waiting_for_dma
= 0;
339 outb(inb(dma_base
)&~1, dma_base
); /* stop DMA */
340 dma_stat
= inb(dma_base
+2); /* get DMA status */
341 outb(dma_stat
|6, dma_base
+2); /* clear the INTR & ERROR bits */
342 ide_destroy_dmatable(drive
); /* purge DMA mappings */
343 return (dma_stat
& 7) != 4; /* verify good DMA status */
347 return ide_dmaproc(func
, drive
); /* use standard DMA stuff */
351 * If the BIOS does not set the IO base addaress to XX00, 343 will fail.
353 #define HPT34X_PCI_INIT_REG 0x80
355 unsigned int __init
pci_init_hpt34x (struct pci_dev
*dev
, const char *name
)
358 unsigned long hpt34xIoBase
= dev
->resource
[4].start
;
362 __save_flags(flags
); /* local CPU only */
363 __cli(); /* local CPU only */
365 pci_write_config_byte(dev
, HPT34X_PCI_INIT_REG
, 0x00);
366 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
368 if (cmd
& PCI_COMMAND_MEMORY
) {
369 if (dev
->resource
[PCI_ROM_RESOURCE
].start
) {
370 pci_write_config_byte(dev
, PCI_ROM_ADDRESS
, dev
->resource
[PCI_ROM_RESOURCE
].start
| PCI_ROM_ADDRESS_ENABLE
);
371 printk(KERN_INFO
"HPT345: ROM enabled at 0x%08lx\n", dev
->resource
[PCI_ROM_RESOURCE
].start
);
373 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0xF0);
375 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0x20);
378 pci_write_config_word(dev
, PCI_COMMAND
, cmd
& ~PCI_COMMAND_IO
);
379 dev
->resource
[0].start
= (hpt34xIoBase
+ 0x20);
380 dev
->resource
[1].start
= (hpt34xIoBase
+ 0x34);
381 dev
->resource
[2].start
= (hpt34xIoBase
+ 0x28);
382 dev
->resource
[3].start
= (hpt34xIoBase
+ 0x3c);
384 dev
->resource
[i
].flags
|= PCI_BASE_ADDRESS_SPACE_IO
;
386 * Since 20-23 can be assigned and are R/W, we correct them.
388 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_0
, dev
->resource
[0].start
);
389 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_1
, dev
->resource
[1].start
);
390 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_2
, dev
->resource
[2].start
);
391 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_3
, dev
->resource
[3].start
);
392 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
394 __restore_flags(flags
); /* local CPU only */
396 #if defined(DISPLAY_HPT34X_TIMINGS) && defined(CONFIG_PROC_FS)
399 hpt34x_display_info
= &hpt34x_get_info
;
400 #endif /* DISPLAY_HPT34X_TIMINGS && CONFIG_PROC_FS */
405 void __init
ide_init_hpt34x (ide_hwif_t
*hwif
)
407 hwif
->tuneproc
= &hpt34x_tune_drive
;
408 if (hwif
->dma_base
) {
409 unsigned short pcicmd
= 0;
411 pci_read_config_word(hwif
->pci_dev
, PCI_COMMAND
, &pcicmd
);
412 hwif
->autodma
= (pcicmd
& PCI_COMMAND_MEMORY
) ? 1 : 0;
413 hwif
->dmaproc
= &hpt34x_dmaproc
;
415 hwif
->drives
[0].autotune
= 1;
416 hwif
->drives
[1].autotune
= 1;