Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ide / legacy / ht6560b.c
blob28873ce78cb512eea029e8c387734f518a9df403
1 /*
2 * Copyright (C) 1995-2000 Linus Torvalds & author (see below)
3 */
5 /*
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
18 * is enabled.
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 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
25 =======
26 * Version 0.08 Need to force prefetch for CDs and other non-disk
27 * devices. (not sure which devices exactly need
28 * prefetch)
29 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
31 * HT-6560B EIDE-controller support
32 * To activate controller support use kernel parameter "ide0=ht6560b".
33 * Use hdparm utility to enable PIO mode support.
35 * Author: Mikko Ala-Fossi <maf@iki.fi>
36 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
37 * Jan Evert van Grootheest <janevert@iae.nl>
38 =======
39 * Jan Evert van Grootheest <janevert@caiway.nl>
40 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
42 * Try: http://www.maf.iki.fi/~maf/ht6560b/
45 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
46 #define HT6560B_VERSION "v0.07"
47 =======
48 #define HT6560B_VERSION "v0.08"
49 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
51 #include <linux/module.h>
52 #include <linux/types.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/timer.h>
56 #include <linux/mm.h>
57 #include <linux/ioport.h>
58 #include <linux/blkdev.h>
59 #include <linux/hdreg.h>
60 #include <linux/ide.h>
61 #include <linux/init.h>
63 #include <asm/io.h>
65 /* #define DEBUG */ /* remove comments for DEBUG messages */
68 * The special i/o-port that HT-6560B uses to configuration:
69 * bit0 (0x01): "1" selects secondary interface
70 * bit2 (0x04): "1" enables FIFO function
71 * bit5 (0x20): "1" enables prefetched data read function (???)
73 * The special i/o-port that HT-6560A uses to configuration:
74 * bit0 (0x01): "1" selects secondary interface
75 * bit1 (0x02): "1" enables prefetched data read function
76 * bit2 (0x04): "0" enables multi-master system (?)
77 * bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time (?)
79 #define HT_CONFIG_PORT 0x3e6
80 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
82 * FIFO + PREFETCH (both a/b-model)
84 #define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
85 /* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
86 #define HT_SECONDARY_IF 0x01
87 #define HT_PREFETCH_MODE 0x20
90 * ht6560b Timing values:
92 * I reviewed some assembler source listings of htide drivers and found
93 * out how they setup those cycle time interfacing values, as they at Holtek
94 * call them. IDESETUP.COM that is supplied with the drivers figures out
95 * optimal values and fetches those values to drivers. I found out that
96 * they use IDE_SELECT_REG to fetch timings to the ide board right after
97 * interface switching. After that it was quite easy to add code to
98 * ht6560b.c.
100 * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
101 * for hda and hdc. But hdb needed higher values to work, so I guess
102 * that sometimes it is necessary to give higher value than IDESETUP
103 * gives. [see cmd640.c for an extreme example of this. -ml]
105 * Perhaps I should explain something about these timing values:
106 * The higher nibble of value is the Recovery Time (rt) and the lower nibble
107 * of the value is the Active Time (at). Minimum value 2 is the fastest and
108 * the maximum value 15 is the slowest. Default values should be 15 for both.
109 * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
110 * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
111 * similar. If value is too small there will be all sorts of failures.
113 * Timing byte consists of
114 * High nibble: Recovery Cycle Time (rt)
115 * The valid values range from 2 to 15. The default is 15.
117 * Low nibble: Active Cycle Time (at)
118 * The valid values range from 2 to 15. The default is 15.
120 * You can obtain optimized timing values by running Holtek IDESETUP.COM
121 * for DOS. DOS drivers get their timing values from command line, where
122 * the first value is the Recovery Time and the second value is the
123 * Active Time for each drive. Smaller value gives higher speed.
124 * In case of failures you should probably fall back to a higher value.
126 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
127 #define HT_TIMING_DEFAULT 0xff
130 * This routine handles interface switching for the peculiar hardware design
131 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
132 * The HT-6560B can only enable one IDE port at a time, and requires a
133 * silly sequence (below) whenever we switch between primary and secondary.
137 * This routine is invoked from ide.c to prepare for access to a given drive.
139 static void ht6560b_selectproc (ide_drive_t *drive)
141 unsigned long flags;
142 static u8 current_select = 0;
143 static u8 current_timing = 0;
144 u8 select, timing;
146 local_irq_save(flags);
147 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
149 =======
151 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
152 select = HT_CONFIG(drive);
153 timing = HT_TIMING(drive);
154 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
156 =======
159 * Need to enforce prefetch sometimes because otherwise
160 * it'll hang (hard).
162 if (drive->media != ide_disk || !drive->present)
163 select |= HT_PREFETCH_MODE;
165 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
166 if (select != current_select || timing != current_timing) {
167 current_select = select;
168 current_timing = timing;
169 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
170 if (drive->media != ide_disk || !drive->present)
171 select |= HT_PREFETCH_MODE;
172 =======
173 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
174 (void)inb(HT_CONFIG_PORT);
175 (void)inb(HT_CONFIG_PORT);
176 (void)inb(HT_CONFIG_PORT);
177 (void)inb(HT_CONFIG_PORT);
178 outb(select, HT_CONFIG_PORT);
180 * Set timing for this drive:
182 outb(timing, IDE_SELECT_REG);
183 (void)inb(IDE_STATUS_REG);
184 #ifdef DEBUG
185 printk("ht6560b: %s: select=%#x timing=%#x\n",
186 drive->name, select, timing);
187 #endif
189 local_irq_restore(flags);
193 * Autodetection and initialization of ht6560b
195 static int __init try_to_init_ht6560b(void)
197 u8 orig_value;
198 int i;
200 /* Autodetect ht6560b */
201 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
202 return 0;
204 for (i=3;i>0;i--) {
205 outb(0x00, HT_CONFIG_PORT);
206 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
207 outb(orig_value, HT_CONFIG_PORT);
208 return 0;
211 outb(0x00, HT_CONFIG_PORT);
212 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
213 outb(orig_value, HT_CONFIG_PORT);
214 return 0;
217 * Ht6560b autodetected
219 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
220 outb(HT_TIMING_DEFAULT, 0x1f6); /* IDE_SELECT_REG */
221 (void) inb(0x1f7); /* IDE_STATUS_REG */
223 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
224 printk("\nht6560b " HT6560B_VERSION
225 =======
226 printk("ht6560b " HT6560B_VERSION
227 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
228 ": chipset detected and initialized"
229 #ifdef DEBUG
230 " with debug enabled"
231 #endif
232 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
233 =======
234 "\n"
235 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
237 return 1;
240 static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
242 int active_time, recovery_time;
243 int active_cycles, recovery_cycles;
244 int bus_speed = system_bus_clock();
246 if (pio) {
247 unsigned int cycle_time;
249 cycle_time = ide_pio_cycle_time(drive, pio);
252 * Just like opti621.c we try to calculate the
253 * actual cycle time for recovery and activity
254 * according system bus speed.
256 active_time = ide_pio_timings[pio].active_time;
257 recovery_time = cycle_time
258 - active_time
259 - ide_pio_timings[pio].setup_time;
261 * Cycle times should be Vesa bus cycles
263 active_cycles = (active_time * bus_speed + 999) / 1000;
264 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
266 * Upper and lower limits
268 if (active_cycles < 2) active_cycles = 2;
269 if (recovery_cycles < 2) recovery_cycles = 2;
270 if (active_cycles > 15) active_cycles = 15;
271 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
273 #ifdef DEBUG
274 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);
275 #endif
277 return (u8)((recovery_cycles << 4) | active_cycles);
278 } else {
280 #ifdef DEBUG
281 printk("ht6560b: drive %s setting pio=0\n", drive->name);
282 #endif
284 return HT_TIMING_DEFAULT; /* default setting */
288 static DEFINE_SPINLOCK(ht6560b_lock);
291 * Enable/Disable so called prefetch mode
293 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
295 unsigned long flags;
296 int t = HT_PREFETCH_MODE << 8;
298 spin_lock_irqsave(&ht6560b_lock, flags);
301 * Prefetch mode and unmask irq seems to conflict
303 if (state) {
304 drive->drive_data |= t; /* enable prefetch mode */
305 drive->no_unmask = 1;
306 drive->unmask = 0;
307 } else {
308 drive->drive_data &= ~t; /* disable prefetch mode */
309 drive->no_unmask = 0;
312 spin_unlock_irqrestore(&ht6560b_lock, flags);
314 #ifdef DEBUG
315 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
316 #endif
319 static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
321 unsigned long flags;
322 u8 timing;
324 switch (pio) {
325 case 8: /* set prefetch off */
326 case 9: /* set prefetch on */
327 ht_set_prefetch(drive, pio & 1);
328 return;
331 timing = ht_pio2timings(drive, pio);
333 spin_lock_irqsave(&ht6560b_lock, flags);
334 drive->drive_data &= 0xff00;
335 drive->drive_data |= timing;
336 spin_unlock_irqrestore(&ht6560b_lock, flags);
338 #ifdef DEBUG
339 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
340 #endif
343 static void __init ht6560b_port_init_devs(ide_hwif_t *hwif)
345 /* Setting default configurations for drives. */
346 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
348 if (hwif->channel)
349 t |= (HT_SECONDARY_IF << 8);
351 hwif->drives[0].drive_data = t;
352 hwif->drives[1].drive_data = t;
355 int probe_ht6560b = 0;
357 module_param_named(probe, probe_ht6560b, bool, 0);
358 MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
360 static const struct ide_port_info ht6560b_port_info __initdata = {
361 .chipset = ide_ht6560b,
362 .host_flags = IDE_HFLAG_SERIALIZE | /* is this needed? */
363 IDE_HFLAG_NO_DMA |
364 IDE_HFLAG_NO_AUTOTUNE |
365 IDE_HFLAG_ABUSE_PREFETCH,
366 <<<<<<< HEAD:drivers/ide/legacy/ht6560b.c
367 .pio_mask = ATA_PIO5,
368 =======
369 .pio_mask = ATA_PIO4,
370 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/legacy/ht6560b.c
373 static int __init ht6560b_init(void)
375 ide_hwif_t *hwif, *mate;
376 static u8 idx[4] = { 0, 1, 0xff, 0xff };
378 if (probe_ht6560b == 0)
379 return -ENODEV;
381 hwif = &ide_hwifs[0];
382 mate = &ide_hwifs[1];
384 if (!request_region(HT_CONFIG_PORT, 1, hwif->name)) {
385 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
386 __FUNCTION__);
387 return -ENODEV;
390 if (!try_to_init_ht6560b()) {
391 printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
392 goto release_region;
395 hwif->selectproc = &ht6560b_selectproc;
396 hwif->set_pio_mode = &ht6560b_set_pio_mode;
398 mate->selectproc = &ht6560b_selectproc;
399 mate->set_pio_mode = &ht6560b_set_pio_mode;
401 hwif->port_init_devs = ht6560b_port_init_devs;
402 mate->port_init_devs = ht6560b_port_init_devs;
404 ide_device_add(idx, &ht6560b_port_info);
406 return 0;
408 release_region:
409 release_region(HT_CONFIG_PORT, 1);
410 return -ENODEV;
413 module_init(ht6560b_init);
415 MODULE_AUTHOR("See Local File");
416 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
417 MODULE_LICENSE("GPL");