Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[pandora-kernel.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/types.h>
43 #include <linux/kernel.h>
44 #include <linux/delay.h>
45 #include <linux/timer.h>
46 #include <linux/mm.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>
52
53 #include <asm/io.h>
54
55 /* #define DEBUG */  /* remove comments for DEBUG messages */
56
57 /*
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  (???)
62  *
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         (?)
68  */
69 #define HT_CONFIG_PORT    0x3e6
70 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
71 /*
72  * FIFO + PREFETCH (both a/b-model)
73  */
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
78
79 /*
80  * ht6560b Timing values:
81  *
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 IDE_SELECT_REG to fetch timings to the ide board right after
87  * interface switching. After that it was quite easy to add code to
88  * ht6560b.c.
89  *
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]
94  *
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.
102  *
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.
106  *
107  *      Low nibble:   Active Cycle Time    (at)
108  *           The valid values range from 2 to 15. The default is 15.
109  *
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.
115  */
116 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
117 #define HT_TIMING_DEFAULT 0xff
118
119 /*
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.
124  */
125
126 /*
127  * This routine is invoked from ide.c to prepare for access to a given drive.
128  */
129 static void ht6560b_selectproc (ide_drive_t *drive)
130 {
131         unsigned long flags;
132         static u8 current_select = 0;
133         static u8 current_timing = 0;
134         u8 select, timing;
135         
136         local_irq_save(flags);
137         
138         select = HT_CONFIG(drive);
139         timing = HT_TIMING(drive);
140         
141         if (select != current_select || timing != current_timing) {
142                 current_select = select;
143                 current_timing = timing;
144                 if (drive->media != ide_disk || !drive->present)
145                         select |= HT_PREFETCH_MODE;
146                 (void)inb(HT_CONFIG_PORT);
147                 (void)inb(HT_CONFIG_PORT);
148                 (void)inb(HT_CONFIG_PORT);
149                 (void)inb(HT_CONFIG_PORT);
150                 outb(select, HT_CONFIG_PORT);
151                 /*
152                  * Set timing for this drive:
153                  */
154                 outb(timing, IDE_SELECT_REG);
155                 (void)inb(IDE_STATUS_REG);
156 #ifdef DEBUG
157                 printk("ht6560b: %s: select=%#x timing=%#x\n",
158                         drive->name, select, timing);
159 #endif
160         }
161         local_irq_restore(flags);
162 }
163
164 /*
165  * Autodetection and initialization of ht6560b
166  */
167 static int __init try_to_init_ht6560b(void)
168 {
169         u8 orig_value;
170         int i;
171         
172         /* Autodetect ht6560b */
173         if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
174                 return 0;
175         
176         for (i=3;i>0;i--) {
177                 outb(0x00, HT_CONFIG_PORT);
178                 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
179                         outb(orig_value, HT_CONFIG_PORT);
180                         return 0;
181                 }
182         }
183         outb(0x00, HT_CONFIG_PORT);
184         if ((~inb(HT_CONFIG_PORT))& 0x3f) {
185                 outb(orig_value, HT_CONFIG_PORT);
186                 return 0;
187         }
188         /*
189          * Ht6560b autodetected
190          */
191         outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
192         outb(HT_TIMING_DEFAULT, 0x1f6);  /* IDE_SELECT_REG */
193         (void) inb(0x1f7);               /* IDE_STATUS_REG */
194         
195         printk("\nht6560b " HT6560B_VERSION
196                ": chipset detected and initialized"
197 #ifdef DEBUG
198                " with debug enabled"
199 #endif
200                 );
201         return 1;
202 }
203
204 static u8 ht_pio2timings(ide_drive_t *drive, u8 pio)
205 {
206         int active_time, recovery_time;
207         int active_cycles, recovery_cycles;
208         ide_pio_data_t d;
209         int bus_speed = system_bus_clock();
210         
211         if (pio) {
212                 pio = ide_get_best_pio_mode(drive, pio, 5, &d);
213                 
214                 /*
215                  *  Just like opti621.c we try to calculate the
216                  *  actual cycle time for recovery and activity
217                  *  according system bus speed.
218                  */
219                 active_time = ide_pio_timings[pio].active_time;
220                 recovery_time = d.cycle_time 
221                         - active_time
222                         - ide_pio_timings[pio].setup_time;
223                 /*
224                  *  Cycle times should be Vesa bus cycles
225                  */
226                 active_cycles   = (active_time   * bus_speed + 999) / 1000;
227                 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
228                 /*
229                  *  Upper and lower limits
230                  */
231                 if (active_cycles   < 2)  active_cycles   = 2;
232                 if (recovery_cycles < 2)  recovery_cycles = 2;
233                 if (active_cycles   > 15) active_cycles   = 15;
234                 if (recovery_cycles > 15) recovery_cycles = 0;  /* 0==16 */
235                 
236 #ifdef DEBUG
237                 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);
238 #endif
239                 
240                 return (u8)((recovery_cycles << 4) | active_cycles);
241         } else {
242                 
243 #ifdef DEBUG
244                 printk("ht6560b: drive %s setting pio=0\n", drive->name);
245 #endif
246                 
247                 return HT_TIMING_DEFAULT;    /* default setting */
248         }
249 }
250
251 /*
252  *  Enable/Disable so called prefetch mode
253  */
254 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
255 {
256         unsigned long flags;
257         int t = HT_PREFETCH_MODE << 8;
258         
259         spin_lock_irqsave(&ide_lock, flags);
260         
261         /*
262          *  Prefetch mode and unmask irq seems to conflict
263          */
264         if (state) {
265                 drive->drive_data |= t;   /* enable prefetch mode */
266                 drive->no_unmask = 1;
267                 drive->unmask = 0;
268         } else {
269                 drive->drive_data &= ~t;  /* disable prefetch mode */
270                 drive->no_unmask = 0;
271         }
272         
273         spin_unlock_irqrestore(&ide_lock, flags);
274         
275 #ifdef DEBUG
276         printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
277 #endif
278 }
279
280 static void tune_ht6560b (ide_drive_t *drive, u8 pio)
281 {
282         unsigned long flags;
283         u8 timing;
284         
285         switch (pio) {
286         case 8:         /* set prefetch off */
287         case 9:         /* set prefetch on */
288                 ht_set_prefetch(drive, pio & 1);
289                 return;
290         }
291         
292         timing = ht_pio2timings(drive, pio);
293         
294         spin_lock_irqsave(&ide_lock, flags);
295         
296         drive->drive_data &= 0xff00;
297         drive->drive_data |= timing;
298         
299         spin_unlock_irqrestore(&ide_lock, flags);
300         
301 #ifdef DEBUG
302         printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
303 #endif
304 }
305
306 /* Can be called directly from ide.c. */
307 int __init ht6560b_init(void)
308 {
309         ide_hwif_t *hwif, *mate;
310         int t;
311
312         hwif = &ide_hwifs[0];
313         mate = &ide_hwifs[1];
314
315         if (!request_region(HT_CONFIG_PORT, 1, hwif->name)) {
316                 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
317                         __FUNCTION__);
318                 return -ENODEV;
319         }
320
321         if (!try_to_init_ht6560b()) {
322                 printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
323                 goto release_region;
324         }
325
326         hwif->chipset = ide_ht6560b;
327         hwif->selectproc = &ht6560b_selectproc;
328         hwif->tuneproc = &tune_ht6560b;
329         hwif->serialized = 1;   /* is this needed? */
330         hwif->mate = mate;
331
332         mate->chipset = ide_ht6560b;
333         mate->selectproc = &ht6560b_selectproc;
334         mate->tuneproc = &tune_ht6560b;
335         mate->serialized = 1;   /* is this needed? */
336         mate->mate = hwif;
337         mate->channel = 1;
338
339         /*
340          * Setting default configurations for drives
341          */
342         t = (HT_CONFIG_DEFAULT << 8);
343         t |= HT_TIMING_DEFAULT;
344         hwif->drives[0].drive_data = t;
345         hwif->drives[1].drive_data = t;
346
347         t |= (HT_SECONDARY_IF << 8);
348         mate->drives[0].drive_data = t;
349         mate->drives[1].drive_data = t;
350
351         probe_hwif_init(hwif);
352         probe_hwif_init(mate);
353
354         create_proc_ide_interfaces();
355
356         return 0;
357
358 release_region:
359         release_region(HT_CONFIG_PORT, 1);
360         return -ENODEV;
361 }
362
363 #ifdef MODULE
364 module_init(ht6560b_init);
365 #endif
366
367 MODULE_AUTHOR("See Local File");
368 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
369 MODULE_LICENSE("GPL");