Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / legacy / ht6560b.c
bloba77fb249d5cf8fa006bc77590929e6ec77c7504e
1 /*
2 * linux/drivers/ide/legacy/ht6560b.c Version 0.07 Feb 1, 2000
4 * Copyright (C) 1995-2000 Linus Torvalds & author (see below)
5 */
7 /*
9 * Version 0.01 Initial version hacked out of ide.c
11 * Version 0.02 Added support for PIO modes, auto-tune
13 * Version 0.03 Some cleanups
15 * Version 0.05 PIO mode cycle timings auto-tune using bus-speed
17 * Version 0.06 Prefetch mode now defaults no OFF. To set
18 * prefetch mode OFF/ON use "hdparm -p8/-p9".
19 * Unmask irq is disabled when prefetch mode
20 * is enabled.
22 * Version 0.07 Trying to fix CD-ROM detection problem.
23 * "Prefetch" mode bit OFF for ide disks and
24 * ON for anything else.
27 * HT-6560B EIDE-controller support
28 * To activate controller support use kernel parameter "ide0=ht6560b".
29 * Use hdparm utility to enable PIO mode support.
31 * Author: Mikko Ala-Fossi <maf@iki.fi>
32 * Jan Evert van Grootheest <janevert@iae.nl>
34 * Try: http://www.maf.iki.fi/~maf/ht6560b/
37 #define HT6560B_VERSION "v0.07"
39 #undef REALLY_SLOW_IO /* most systems can safely undef this */
41 #include <linux/module.h>
42 #include <linux/config.h>
43 #include <linux/types.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/mm.h>
48 #include <linux/ioport.h>
49 #include <linux/blkdev.h>
50 #include <linux/hdreg.h>
51 #include <linux/ide.h>
52 #include <linux/init.h>
54 #include <asm/io.h>
56 /* #define DEBUG */ /* remove comments for DEBUG messages */
59 * The special i/o-port that HT-6560B uses to configuration:
60 * bit0 (0x01): "1" selects secondary interface
61 * bit2 (0x04): "1" enables FIFO function
62 * bit5 (0x20): "1" enables prefetched data read function (???)
64 * The special i/o-port that HT-6560A uses to configuration:
65 * bit0 (0x01): "1" selects secondary interface
66 * bit1 (0x02): "1" enables prefetched data read function
67 * bit2 (0x04): "0" enables multi-master system (?)
68 * bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time (?)
70 #define HT_CONFIG_PORT 0x3e6
71 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
73 * FIFO + PREFETCH (both a/b-model)
75 #define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
76 /* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
77 #define HT_SECONDARY_IF 0x01
78 #define HT_PREFETCH_MODE 0x20
81 * ht6560b Timing values:
83 * I reviewed some assembler source listings of htide drivers and found
84 * out how they setup those cycle time interfacing values, as they at Holtek
85 * call them. IDESETUP.COM that is supplied with the drivers figures out
86 * optimal values and fetches those values to drivers. I found out that
87 * they use IDE_SELECT_REG to fetch timings to the ide board right after
88 * interface switching. After that it was quite easy to add code to
89 * ht6560b.c.
91 * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
92 * for hda and hdc. But hdb needed higher values to work, so I guess
93 * that sometimes it is necessary to give higher value than IDESETUP
94 * gives. [see cmd640.c for an extreme example of this. -ml]
96 * Perhaps I should explain something about these timing values:
97 * The higher nibble of value is the Recovery Time (rt) and the lower nibble
98 * of the value is the Active Time (at). Minimum value 2 is the fastest and
99 * the maximum value 15 is the slowest. Default values should be 15 for both.
100 * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
101 * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
102 * similar. If value is too small there will be all sorts of failures.
104 * Timing byte consists of
105 * High nibble: Recovery Cycle Time (rt)
106 * The valid values range from 2 to 15. The default is 15.
108 * Low nibble: Active Cycle Time (at)
109 * The valid values range from 2 to 15. The default is 15.
111 * You can obtain optimized timing values by running Holtek IDESETUP.COM
112 * for DOS. DOS drivers get their timing values from command line, where
113 * the first value is the Recovery Time and the second value is the
114 * Active Time for each drive. Smaller value gives higher speed.
115 * In case of failures you should probably fall back to a higher value.
117 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
118 #define HT_TIMING_DEFAULT 0xff
121 * This routine handles interface switching for the peculiar hardware design
122 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
123 * The HT-6560B can only enable one IDE port at a time, and requires a
124 * silly sequence (below) whenever we switch between primary and secondary.
128 * This routine is invoked from ide.c to prepare for access to a given drive.
130 static void ht6560b_selectproc (ide_drive_t *drive)
132 unsigned long flags;
133 static u8 current_select = 0;
134 static u8 current_timing = 0;
135 u8 select, timing;
137 local_irq_save(flags);
139 select = HT_CONFIG(drive);
140 timing = HT_TIMING(drive);
142 if (select != current_select || timing != current_timing) {
143 current_select = select;
144 current_timing = timing;
145 if (drive->media != ide_disk || !drive->present)
146 select |= HT_PREFETCH_MODE;
147 (void) HWIF(drive)->INB(HT_CONFIG_PORT);
148 (void) HWIF(drive)->INB(HT_CONFIG_PORT);
149 (void) HWIF(drive)->INB(HT_CONFIG_PORT);
150 (void) HWIF(drive)->INB(HT_CONFIG_PORT);
151 HWIF(drive)->OUTB(select, HT_CONFIG_PORT);
153 * Set timing for this drive:
155 HWIF(drive)->OUTB(timing, IDE_SELECT_REG);
156 (void) HWIF(drive)->INB(IDE_STATUS_REG);
157 #ifdef DEBUG
158 printk("ht6560b: %s: select=%#x timing=%#x\n",
159 drive->name, select, timing);
160 #endif
162 local_irq_restore(flags);
166 * Autodetection and initialization of ht6560b
168 static int __init try_to_init_ht6560b(void)
170 u8 orig_value;
171 int i;
173 /* Autodetect ht6560b */
174 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
175 return 0;
177 for (i=3;i>0;i--) {
178 outb(0x00, HT_CONFIG_PORT);
179 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
180 outb(orig_value, HT_CONFIG_PORT);
181 return 0;
184 outb(0x00, HT_CONFIG_PORT);
185 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
186 outb(orig_value, HT_CONFIG_PORT);
187 return 0;
190 * Ht6560b autodetected
192 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
193 outb(HT_TIMING_DEFAULT, 0x1f6); /* IDE_SELECT_REG */
194 (void) inb(0x1f7); /* IDE_STATUS_REG */
196 printk("\nht6560b " HT6560B_VERSION
197 ": chipset detected and initialized"
198 #ifdef DEBUG
199 " with debug enabled"
200 #endif
202 return 1;
205 static u8 ht_pio2timings(ide_drive_t *drive, u8 pio)
207 int active_time, recovery_time;
208 int active_cycles, recovery_cycles;
209 ide_pio_data_t d;
210 int bus_speed = system_bus_clock();
212 if (pio) {
213 pio = ide_get_best_pio_mode(drive, pio, 5, &d);
216 * Just like opti621.c we try to calculate the
217 * actual cycle time for recovery and activity
218 * according system bus speed.
220 active_time = ide_pio_timings[pio].active_time;
221 recovery_time = d.cycle_time
222 - active_time
223 - ide_pio_timings[pio].setup_time;
225 * Cycle times should be Vesa bus cycles
227 active_cycles = (active_time * bus_speed + 999) / 1000;
228 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
230 * Upper and lower limits
232 if (active_cycles < 2) active_cycles = 2;
233 if (recovery_cycles < 2) recovery_cycles = 2;
234 if (active_cycles > 15) active_cycles = 15;
235 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
237 #ifdef DEBUG
238 printk("ht6560b: drive %s setting pio=%d recovery=%d (%dns) active=%d (%dns)\n", drive->name, pio, recovery_cycles, recovery_time, active_cycles, active_time);
239 #endif
241 return (u8)((recovery_cycles << 4) | active_cycles);
242 } else {
244 #ifdef DEBUG
245 printk("ht6560b: drive %s setting pio=0\n", drive->name);
246 #endif
248 return HT_TIMING_DEFAULT; /* default setting */
253 * Enable/Disable so called prefetch mode
255 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
257 unsigned long flags;
258 int t = HT_PREFETCH_MODE << 8;
260 spin_lock_irqsave(&ide_lock, flags);
263 * Prefetch mode and unmask irq seems to conflict
265 if (state) {
266 drive->drive_data |= t; /* enable prefetch mode */
267 drive->no_unmask = 1;
268 drive->unmask = 0;
269 } else {
270 drive->drive_data &= ~t; /* disable prefetch mode */
271 drive->no_unmask = 0;
274 spin_unlock_irqrestore(&ide_lock, flags);
276 #ifdef DEBUG
277 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
278 #endif
281 static void tune_ht6560b (ide_drive_t *drive, u8 pio)
283 unsigned long flags;
284 u8 timing;
286 switch (pio) {
287 case 8: /* set prefetch off */
288 case 9: /* set prefetch on */
289 ht_set_prefetch(drive, pio & 1);
290 return;
293 timing = ht_pio2timings(drive, pio);
295 spin_lock_irqsave(&ide_lock, flags);
297 drive->drive_data &= 0xff00;
298 drive->drive_data |= timing;
300 spin_unlock_irqrestore(&ide_lock, flags);
302 #ifdef DEBUG
303 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
304 #endif
307 /* Can be called directly from ide.c. */
308 int __init ht6560b_init(void)
310 ide_hwif_t *hwif, *mate;
311 int t;
313 hwif = &ide_hwifs[0];
314 mate = &ide_hwifs[1];
316 if (!request_region(HT_CONFIG_PORT, 1, hwif->name)) {
317 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
318 __FUNCTION__);
319 return -ENODEV;
322 if (!try_to_init_ht6560b()) {
323 printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
324 goto release_region;
327 hwif->chipset = ide_ht6560b;
328 hwif->selectproc = &ht6560b_selectproc;
329 hwif->tuneproc = &tune_ht6560b;
330 hwif->serialized = 1; /* is this needed? */
331 hwif->mate = mate;
333 mate->chipset = ide_ht6560b;
334 mate->selectproc = &ht6560b_selectproc;
335 mate->tuneproc = &tune_ht6560b;
336 mate->serialized = 1; /* is this needed? */
337 mate->mate = hwif;
338 mate->channel = 1;
341 * Setting default configurations for drives
343 t = (HT_CONFIG_DEFAULT << 8);
344 t |= HT_TIMING_DEFAULT;
345 hwif->drives[0].drive_data = t;
346 hwif->drives[1].drive_data = t;
348 t |= (HT_SECONDARY_IF << 8);
349 mate->drives[0].drive_data = t;
350 mate->drives[1].drive_data = t;
352 probe_hwif_init(hwif);
353 probe_hwif_init(mate);
355 create_proc_ide_interfaces();
357 return 0;
359 release_region:
360 release_region(HT_CONFIG_PORT, 1);
361 return -ENODEV;
364 #ifdef MODULE
365 module_init(ht6560b_init);
366 #endif
368 MODULE_AUTHOR("See Local File");
369 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
370 MODULE_LICENSE("GPL");