ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / ide / legacy / ht6560b.c
1 /*
2  *  linux/drivers/ide/legacy/ht6560b.c          Version 0.07    Feb  1, 2000
3  *
4  *  Copyright (C) 1995-2000  Linus Torvalds & author (see below)
5  */
6
7 /*
8  *
9  *  Version 0.01        Initial version hacked out of ide.c
10  *
11  *  Version 0.02        Added support for PIO modes, auto-tune
12  *
13  *  Version 0.03        Some cleanups
14  *
15  *  Version 0.05        PIO mode cycle timings auto-tune using bus-speed
16  *
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.
21  *
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.
25  *
26  *
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.
30  *
31  *  Author:    Mikko Ala-Fossi            <maf@iki.fi>
32  *             Jan Evert van Grootheest   <janevert@iae.nl>
33  *
34  *  Try:  http://www.maf.iki.fi/~maf/ht6560b/
35  */
36
37 #define HT6560B_VERSION "v0.07"
38
39 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
40
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>
53
54 #include <asm/io.h>
55
56 /* #define DEBUG */  /* remove comments for DEBUG messages */
57
58 /*
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  (???)
63  *
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         (?)
69  */
70 #define HT_CONFIG_PORT    0x3e6
71 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
72 /*
73  * FIFO + PREFETCH (both a/b-model)
74  */
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
79
80 /*
81  * ht6560b Timing values:
82  *
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.
90  *
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]
95  *
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.
103  *
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.
107  *
108  *      Low nibble:   Active Cycle Time    (at)
109  *           The valid values range from 2 to 15. The default is 15.
110  *
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.
116  */
117 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
118 #define HT_TIMING_DEFAULT 0xff
119
120 /*
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.
125  */
126
127 /*
128  * This routine is invoked from ide.c to prepare for access to a given drive.
129  */
130 static void ht6560b_selectproc (ide_drive_t *drive)
131 {
132         unsigned long flags;
133         static u8 current_select = 0;
134         static u8 current_timing = 0;
135         u8 select, timing;
136         
137         local_irq_save(flags);
138         
139         select = HT_CONFIG(drive);
140         timing = HT_TIMING(drive);
141         
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);
152                 /*
153                  * Set timing for this drive:
154                  */
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
161         }
162         local_irq_restore(flags);
163 }
164
165 /*
166  * Autodetection and initialization of ht6560b
167  */
168 static int __init try_to_init_ht6560b(void)
169 {
170         u8 orig_value;
171         int i;
172         
173         /* Autodetect ht6560b */
174         if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
175                 return 0;
176         
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;
182                 }
183         }
184         outb(0x00, HT_CONFIG_PORT);
185         if ((~inb(HT_CONFIG_PORT))& 0x3f) {
186                 outb(orig_value, HT_CONFIG_PORT);
187                 return 0;
188         }
189         /*
190          * Ht6560b autodetected
191          */
192         outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
193         outb(HT_TIMING_DEFAULT, 0x1f6);  /* IDE_SELECT_REG */
194         (void) inb(0x1f7);               /* IDE_STATUS_REG */
195         
196         printk("\nht6560b " HT6560B_VERSION
197                ": chipset detected and initialized"
198 #ifdef DEBUG
199                " with debug enabled"
200 #endif
201                 );
202         return 1;
203 }
204
205 static u8 ht_pio2timings(ide_drive_t *drive, u8 pio)
206 {
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();
211         
212         if (pio) {
213                 pio = ide_get_best_pio_mode(drive, pio, 5, &d);
214                 
215                 /*
216                  *  Just like opti621.c we try to calculate the
217                  *  actual cycle time for recovery and activity
218                  *  according system bus speed.
219                  */
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;
224                 /*
225                  *  Cycle times should be Vesa bus cycles
226                  */
227                 active_cycles   = (active_time   * bus_speed + 999) / 1000;
228                 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
229                 /*
230                  *  Upper and lower limits
231                  */
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 */
236                 
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
240                 
241                 return (u8)((recovery_cycles << 4) | active_cycles);
242         } else {
243                 
244 #ifdef DEBUG
245                 printk("ht6560b: drive %s setting pio=0\n", drive->name);
246 #endif
247                 
248                 return HT_TIMING_DEFAULT;    /* default setting */
249         }
250 }
251
252 /*
253  *  Enable/Disable so called prefetch mode
254  */
255 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
256 {
257         unsigned long flags;
258         int t = HT_PREFETCH_MODE << 8;
259         
260         spin_lock_irqsave(&ide_lock, flags);
261         
262         /*
263          *  Prefetch mode and unmask irq seems to conflict
264          */
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;
272         }
273         
274         spin_unlock_irqrestore(&ide_lock, flags);
275         
276 #ifdef DEBUG
277         printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
278 #endif
279 }
280
281 static void tune_ht6560b (ide_drive_t *drive, u8 pio)
282 {
283         unsigned long flags;
284         u8 timing;
285         
286         switch (pio) {
287         case 8:         /* set prefetch off */
288         case 9:         /* set prefetch on */
289                 ht_set_prefetch(drive, pio & 1);
290                 return;
291         }
292         
293         timing = ht_pio2timings(drive, pio);
294         
295         spin_lock_irqsave(&ide_lock, flags);
296         
297         drive->drive_data &= 0xff00;
298         drive->drive_data |= timing;
299         
300         spin_unlock_irqrestore(&ide_lock, flags);
301         
302 #ifdef DEBUG
303         printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
304 #endif
305 }
306
307 /* Can be called directly from ide.c. */
308 int __init ht6560b_init(void)
309 {
310         ide_hwif_t *hwif, *mate;
311         int t;
312
313         hwif = &ide_hwifs[0];
314         mate = &ide_hwifs[1];
315
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;
320         }
321
322         if (!try_to_init_ht6560b()) {
323                 printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
324                 goto release_region;
325         }
326
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;
332
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;
339
340         /*
341          * Setting default configurations for drives
342          */
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;
347
348         t |= (HT_SECONDARY_IF << 8);
349         mate->drives[0].drive_data = t;
350         mate->drives[1].drive_data = t;
351
352         probe_hwif_init(hwif);
353         probe_hwif_init(mate);
354
355         return 0;
356
357 release_region:
358         release_region(HT_CONFIG_PORT, 1);
359         return -ENODEV;
360 }
361
362 #ifdef MODULE
363 module_init(ht6560b_init);
364 #endif
365
366 MODULE_AUTHOR("See Local File");
367 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
368 MODULE_LICENSE("GPL");