2 * Copyright (C) 1995-2000 Linus Torvalds & author (see below)
7 * Version 0.01 Initial version hacked out of ide.c
9 * Version 0.02 Added support for PIO modes, auto-tune
11 * Version 0.03 Some cleanups
13 * Version 0.05 PIO mode cycle timings auto-tune using bus-speed
15 * Version 0.06 Prefetch mode now defaults no OFF. To set
16 * prefetch mode OFF/ON use "hdparm -p8/-p9".
17 * Unmask irq is disabled when prefetch mode
20 * Version 0.07 Trying to fix CD-ROM detection problem.
21 * "Prefetch" mode bit OFF for ide disks and
22 * ON for anything else.
24 * Version 0.08 Need to force prefetch for CDs and other non-disk
25 * devices. (not sure which devices exactly need
28 * HT-6560B EIDE-controller support
29 * To activate controller support use kernel parameter "ide0=ht6560b".
30 * Use hdparm utility to enable PIO mode support.
32 * Author: Mikko Ala-Fossi <maf@iki.fi>
33 * Jan Evert van Grootheest <janevert@caiway.nl>
35 * Try: http://www.maf.iki.fi/~maf/ht6560b/
38 #define DRV_NAME "ht6560b"
39 #define HT6560B_VERSION "v0.08"
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/delay.h>
45 #include <linux/timer.h>
47 #include <linux/ioport.h>
48 #include <linux/blkdev.h>
49 #include <linux/hdreg.h>
50 #include <linux/ide.h>
51 #include <linux/init.h>
55 /* #define DEBUG */ /* remove comments for DEBUG messages */
58 * The special i/o-port that HT-6560B uses to configuration:
59 * bit0 (0x01): "1" selects secondary interface
60 * bit2 (0x04): "1" enables FIFO function
61 * bit5 (0x20): "1" enables prefetched data read function (???)
63 * The special i/o-port that HT-6560A uses to configuration:
64 * bit0 (0x01): "1" selects secondary interface
65 * bit1 (0x02): "1" enables prefetched data read function
66 * bit2 (0x04): "0" enables multi-master system (?)
67 * bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time (?)
69 #define HT_CONFIG_PORT 0x3e6
70 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
72 * FIFO + PREFETCH (both a/b-model)
74 #define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
75 /* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
76 #define HT_SECONDARY_IF 0x01
77 #define HT_PREFETCH_MODE 0x20
80 * ht6560b Timing values:
82 * I reviewed some assembler source listings of htide drivers and found
83 * out how they setup those cycle time interfacing values, as they at Holtek
84 * call them. IDESETUP.COM that is supplied with the drivers figures out
85 * optimal values and fetches those values to drivers. I found out that
86 * they use Select register to fetch timings to the ide board right after
87 * interface switching. After that it was quite easy to add code to
90 * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
91 * for hda and hdc. But hdb needed higher values to work, so I guess
92 * that sometimes it is necessary to give higher value than IDESETUP
93 * gives. [see cmd640.c for an extreme example of this. -ml]
95 * Perhaps I should explain something about these timing values:
96 * The higher nibble of value is the Recovery Time (rt) and the lower nibble
97 * of the value is the Active Time (at). Minimum value 2 is the fastest and
98 * the maximum value 15 is the slowest. Default values should be 15 for both.
99 * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
100 * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
101 * similar. If value is too small there will be all sorts of failures.
103 * Timing byte consists of
104 * High nibble: Recovery Cycle Time (rt)
105 * The valid values range from 2 to 15. The default is 15.
107 * Low nibble: Active Cycle Time (at)
108 * The valid values range from 2 to 15. The default is 15.
110 * You can obtain optimized timing values by running Holtek IDESETUP.COM
111 * for DOS. DOS drivers get their timing values from command line, where
112 * the first value is the Recovery Time and the second value is the
113 * Active Time for each drive. Smaller value gives higher speed.
114 * In case of failures you should probably fall back to a higher value.
116 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
117 #define HT_TIMING_DEFAULT 0xff
120 * This routine handles interface switching for the peculiar hardware design
121 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
122 * The HT-6560B can only enable one IDE port at a time, and requires a
123 * silly sequence (below) whenever we switch between primary and secondary.
127 * This routine is invoked from ide.c to prepare for access to a given drive.
129 static void ht6560b_selectproc (ide_drive_t *drive)
131 ide_hwif_t *hwif = drive->hwif;
133 static u8 current_select = 0;
134 static u8 current_timing = 0;
137 local_irq_save(flags);
139 select = HT_CONFIG(drive);
140 timing = HT_TIMING(drive);
143 * Need to enforce prefetch sometimes because otherwise
146 if (drive->media != ide_disk || !drive->present)
147 select |= HT_PREFETCH_MODE;
149 if (select != current_select || timing != current_timing) {
150 current_select = select;
151 current_timing = timing;
152 (void)inb(HT_CONFIG_PORT);
153 (void)inb(HT_CONFIG_PORT);
154 (void)inb(HT_CONFIG_PORT);
155 (void)inb(HT_CONFIG_PORT);
156 outb(select, HT_CONFIG_PORT);
158 * Set timing for this drive:
160 outb(timing, hwif->io_ports.device_addr);
161 (void)inb(hwif->io_ports.status_addr);
163 printk("ht6560b: %s: select=%#x timing=%#x\n",
164 drive->name, select, timing);
167 local_irq_restore(flags);
171 * Autodetection and initialization of ht6560b
173 static int __init try_to_init_ht6560b(void)
178 /* Autodetect ht6560b */
179 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
183 outb(0x00, HT_CONFIG_PORT);
184 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
185 outb(orig_value, HT_CONFIG_PORT);
189 outb(0x00, HT_CONFIG_PORT);
190 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
191 outb(orig_value, HT_CONFIG_PORT);
195 * Ht6560b autodetected
197 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
198 outb(HT_TIMING_DEFAULT, 0x1f6); /* Select register */
199 (void)inb(0x1f7); /* Status register */
201 printk("ht6560b " HT6560B_VERSION
202 ": chipset detected and initialized"
204 " with debug enabled"
211 static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
213 int active_time, recovery_time;
214 int active_cycles, recovery_cycles;
215 int bus_speed = ide_vlb_clk ? ide_vlb_clk : system_bus_clock();
218 unsigned int cycle_time;
220 cycle_time = ide_pio_cycle_time(drive, pio);
223 * Just like opti621.c we try to calculate the
224 * actual cycle time for recovery and activity
225 * according system bus speed.
227 active_time = ide_pio_timings[pio].active_time;
228 recovery_time = cycle_time
230 - ide_pio_timings[pio].setup_time;
232 * Cycle times should be Vesa bus cycles
234 active_cycles = (active_time * bus_speed + 999) / 1000;
235 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
237 * Upper and lower limits
239 if (active_cycles < 2) active_cycles = 2;
240 if (recovery_cycles < 2) recovery_cycles = 2;
241 if (active_cycles > 15) active_cycles = 15;
242 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
245 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);
248 return (u8)((recovery_cycles << 4) | active_cycles);
252 printk("ht6560b: drive %s setting pio=0\n", drive->name);
255 return HT_TIMING_DEFAULT; /* default setting */
259 static DEFINE_SPINLOCK(ht6560b_lock);
262 * Enable/Disable so called prefetch mode
264 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
267 int t = HT_PREFETCH_MODE << 8;
269 spin_lock_irqsave(&ht6560b_lock, flags);
272 * Prefetch mode and unmask irq seems to conflict
275 drive->drive_data |= t; /* enable prefetch mode */
276 drive->no_unmask = 1;
279 drive->drive_data &= ~t; /* disable prefetch mode */
280 drive->no_unmask = 0;
283 spin_unlock_irqrestore(&ht6560b_lock, flags);
286 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
290 static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
296 case 8: /* set prefetch off */
297 case 9: /* set prefetch on */
298 ht_set_prefetch(drive, pio & 1);
302 timing = ht_pio2timings(drive, pio);
304 spin_lock_irqsave(&ht6560b_lock, flags);
305 drive->drive_data &= 0xff00;
306 drive->drive_data |= timing;
307 spin_unlock_irqrestore(&ht6560b_lock, flags);
310 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
314 static void __init ht6560b_port_init_devs(ide_hwif_t *hwif)
316 /* Setting default configurations for drives. */
317 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
320 t |= (HT_SECONDARY_IF << 8);
322 hwif->drives[0].drive_data = t;
323 hwif->drives[1].drive_data = t;
326 static int probe_ht6560b;
328 module_param_named(probe, probe_ht6560b, bool, 0);
329 MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
331 static const struct ide_port_ops ht6560b_port_ops = {
332 .port_init_devs = ht6560b_port_init_devs,
333 .set_pio_mode = ht6560b_set_pio_mode,
334 .selectproc = ht6560b_selectproc,
337 static const struct ide_port_info ht6560b_port_info __initdata = {
339 .chipset = ide_ht6560b,
340 .port_ops = &ht6560b_port_ops,
341 .host_flags = IDE_HFLAG_SERIALIZE | /* is this needed? */
343 IDE_HFLAG_ABUSE_PREFETCH,
344 .pio_mask = ATA_PIO4,
347 static int __init ht6560b_init(void)
349 if (probe_ht6560b == 0)
352 if (!request_region(HT_CONFIG_PORT, 1, DRV_NAME)) {
353 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
358 if (!try_to_init_ht6560b()) {
359 printk(KERN_NOTICE "%s: HBA not found\n", __func__);
363 return ide_legacy_device_add(&ht6560b_port_info, 0);
366 release_region(HT_CONFIG_PORT, 1);
370 module_init(ht6560b_init);
372 MODULE_AUTHOR("See Local File");
373 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
374 MODULE_LICENSE("GPL");