Merge commit 'upstream/master'
[pandora-kernel.git] / drivers / ide / legacy / ht6560b.c
1 /*
2  *  Copyright (C) 1995-2000  Linus Torvalds & author (see below)
3  */
4
5 /*
6  *  HT-6560B EIDE-controller support
7  *  To activate controller support use kernel parameter "ide0=ht6560b".
8  *  Use hdparm utility to enable PIO mode support.
9  *
10  *  Author:    Mikko Ala-Fossi            <maf@iki.fi>
11  *             Jan Evert van Grootheest   <j.e.van.grootheest@caiway.nl>
12  *
13  *  Try:  http://www.maf.iki.fi/~maf/ht6560b/
14  */
15
16 #define DRV_NAME        "ht6560b"
17 #define HT6560B_VERSION "v0.08"
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/ioport.h>
26 #include <linux/blkdev.h>
27 #include <linux/hdreg.h>
28 #include <linux/ide.h>
29 #include <linux/init.h>
30
31 #include <asm/io.h>
32
33 /* #define DEBUG */  /* remove comments for DEBUG messages */
34
35 /*
36  * The special i/o-port that HT-6560B uses to configuration:
37  *    bit0 (0x01): "1" selects secondary interface
38  *    bit2 (0x04): "1" enables FIFO function
39  *    bit5 (0x20): "1" enables prefetched data read function  (???)
40  *
41  * The special i/o-port that HT-6560A uses to configuration:
42  *    bit0 (0x01): "1" selects secondary interface
43  *    bit1 (0x02): "1" enables prefetched data read function
44  *    bit2 (0x04): "0" enables multi-master system            (?)
45  *    bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time         (?)
46  */
47 #define HT_CONFIG_PORT    0x3e6
48 #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
49 /*
50  * FIFO + PREFETCH (both a/b-model)
51  */
52 #define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
53 /* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
54 #define HT_SECONDARY_IF   0x01
55 #define HT_PREFETCH_MODE  0x20
56
57 /*
58  * ht6560b Timing values:
59  *
60  * I reviewed some assembler source listings of htide drivers and found
61  * out how they setup those cycle time interfacing values, as they at Holtek
62  * call them. IDESETUP.COM that is supplied with the drivers figures out
63  * optimal values and fetches those values to drivers. I found out that
64  * they use Select register to fetch timings to the ide board right after
65  * interface switching. After that it was quite easy to add code to
66  * ht6560b.c.
67  *
68  * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
69  * for hda and hdc. But hdb needed higher values to work, so I guess
70  * that sometimes it is necessary to give higher value than IDESETUP
71  * gives.   [see cmd640.c for an extreme example of this. -ml]
72  *
73  * Perhaps I should explain something about these timing values:
74  * The higher nibble of value is the Recovery Time  (rt) and the lower nibble
75  * of the value is the Active Time  (at). Minimum value 2 is the fastest and
76  * the maximum value 15 is the slowest. Default values should be 15 for both.
77  * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
78  * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
79  * similar. If value is too small there will be all sorts of failures.
80  *
81  * Timing byte consists of
82  *      High nibble:  Recovery Cycle Time  (rt)
83  *           The valid values range from 2 to 15. The default is 15.
84  *
85  *      Low nibble:   Active Cycle Time    (at)
86  *           The valid values range from 2 to 15. The default is 15.
87  *
88  * You can obtain optimized timing values by running Holtek IDESETUP.COM
89  * for DOS. DOS drivers get their timing values from command line, where
90  * the first value is the Recovery Time and the second value is the
91  * Active Time for each drive. Smaller value gives higher speed.
92  * In case of failures you should probably fall back to a higher value.
93  */
94 #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
95 #define HT_TIMING_DEFAULT 0xff
96
97 /*
98  * This routine handles interface switching for the peculiar hardware design
99  * on the F.G.I./Holtek HT-6560B VLB IDE interface.
100  * The HT-6560B can only enable one IDE port at a time, and requires a
101  * silly sequence (below) whenever we switch between primary and secondary.
102  */
103
104 /*
105  * This routine is invoked from ide.c to prepare for access to a given drive.
106  */
107 static void ht6560b_selectproc (ide_drive_t *drive)
108 {
109         ide_hwif_t *hwif = drive->hwif;
110         unsigned long flags;
111         static u8 current_select = 0;
112         static u8 current_timing = 0;
113         u8 select, timing;
114         
115         local_irq_save(flags);
116
117         select = HT_CONFIG(drive);
118         timing = HT_TIMING(drive);
119
120         /*
121          * Need to enforce prefetch sometimes because otherwise
122          * it'll hang (hard).
123          */
124         if (drive->media != ide_disk || !drive->present)
125                 select |= HT_PREFETCH_MODE;
126
127         if (select != current_select || timing != current_timing) {
128                 current_select = select;
129                 current_timing = timing;
130                 (void)inb(HT_CONFIG_PORT);
131                 (void)inb(HT_CONFIG_PORT);
132                 (void)inb(HT_CONFIG_PORT);
133                 (void)inb(HT_CONFIG_PORT);
134                 outb(select, HT_CONFIG_PORT);
135                 /*
136                  * Set timing for this drive:
137                  */
138                 outb(timing, hwif->io_ports.device_addr);
139                 (void)inb(hwif->io_ports.status_addr);
140 #ifdef DEBUG
141                 printk("ht6560b: %s: select=%#x timing=%#x\n",
142                         drive->name, select, timing);
143 #endif
144         }
145         local_irq_restore(flags);
146 }
147
148 /*
149  * Autodetection and initialization of ht6560b
150  */
151 static int __init try_to_init_ht6560b(void)
152 {
153         u8 orig_value;
154         int i;
155         
156         /* Autodetect ht6560b */
157         if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
158                 return 0;
159         
160         for (i=3;i>0;i--) {
161                 outb(0x00, HT_CONFIG_PORT);
162                 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
163                         outb(orig_value, HT_CONFIG_PORT);
164                         return 0;
165                 }
166         }
167         outb(0x00, HT_CONFIG_PORT);
168         if ((~inb(HT_CONFIG_PORT))& 0x3f) {
169                 outb(orig_value, HT_CONFIG_PORT);
170                 return 0;
171         }
172         /*
173          * Ht6560b autodetected
174          */
175         outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
176         outb(HT_TIMING_DEFAULT, 0x1f6); /* Select register */
177         (void)inb(0x1f7);               /* Status register */
178
179         printk("ht6560b " HT6560B_VERSION
180                ": chipset detected and initialized"
181 #ifdef DEBUG
182                " with debug enabled"
183 #endif
184                "\n"
185                 );
186         return 1;
187 }
188
189 static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
190 {
191         int active_time, recovery_time;
192         int active_cycles, recovery_cycles;
193         int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
194
195         if (pio) {
196                 unsigned int cycle_time;
197                 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
198
199                 cycle_time = ide_pio_cycle_time(drive, pio);
200
201                 /*
202                  *  Just like opti621.c we try to calculate the
203                  *  actual cycle time for recovery and activity
204                  *  according system bus speed.
205                  */
206                 active_time = t->active;
207                 recovery_time = cycle_time - active_time - t->setup;
208                 /*
209                  *  Cycle times should be Vesa bus cycles
210                  */
211                 active_cycles   = (active_time   * bus_speed + 999) / 1000;
212                 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
213                 /*
214                  *  Upper and lower limits
215                  */
216                 if (active_cycles   < 2)  active_cycles   = 2;
217                 if (recovery_cycles < 2)  recovery_cycles = 2;
218                 if (active_cycles   > 15) active_cycles   = 15;
219                 if (recovery_cycles > 15) recovery_cycles = 0;  /* 0==16 */
220                 
221 #ifdef DEBUG
222                 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);
223 #endif
224                 
225                 return (u8)((recovery_cycles << 4) | active_cycles);
226         } else {
227                 
228 #ifdef DEBUG
229                 printk("ht6560b: drive %s setting pio=0\n", drive->name);
230 #endif
231                 
232                 return HT_TIMING_DEFAULT;    /* default setting */
233         }
234 }
235
236 static DEFINE_SPINLOCK(ht6560b_lock);
237
238 /*
239  *  Enable/Disable so called prefetch mode
240  */
241 static void ht_set_prefetch(ide_drive_t *drive, u8 state)
242 {
243         unsigned long flags;
244         int t = HT_PREFETCH_MODE << 8;
245
246         spin_lock_irqsave(&ht6560b_lock, flags);
247
248         /*
249          *  Prefetch mode and unmask irq seems to conflict
250          */
251         if (state) {
252                 drive->drive_data |= t;   /* enable prefetch mode */
253                 drive->no_unmask = 1;
254                 drive->unmask = 0;
255         } else {
256                 drive->drive_data &= ~t;  /* disable prefetch mode */
257                 drive->no_unmask = 0;
258         }
259
260         spin_unlock_irqrestore(&ht6560b_lock, flags);
261
262 #ifdef DEBUG
263         printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
264 #endif
265 }
266
267 static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
268 {
269         unsigned long flags;
270         u8 timing;
271         
272         switch (pio) {
273         case 8:         /* set prefetch off */
274         case 9:         /* set prefetch on */
275                 ht_set_prefetch(drive, pio & 1);
276                 return;
277         }
278
279         timing = ht_pio2timings(drive, pio);
280
281         spin_lock_irqsave(&ht6560b_lock, flags);
282         drive->drive_data &= 0xff00;
283         drive->drive_data |= timing;
284         spin_unlock_irqrestore(&ht6560b_lock, flags);
285
286 #ifdef DEBUG
287         printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
288 #endif
289 }
290
291 static void __init ht6560b_init_dev(ide_drive_t *drive)
292 {
293         ide_hwif_t *hwif = drive->hwif;
294         /* Setting default configurations for drives. */
295         int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
296
297         if (hwif->channel)
298                 t |= (HT_SECONDARY_IF << 8);
299
300         drive->drive_data = t;
301 }
302
303 static int probe_ht6560b;
304
305 module_param_named(probe, probe_ht6560b, bool, 0);
306 MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
307
308 static const struct ide_port_ops ht6560b_port_ops = {
309         .init_dev               = ht6560b_init_dev,
310         .set_pio_mode           = ht6560b_set_pio_mode,
311         .selectproc             = ht6560b_selectproc,
312 };
313
314 static const struct ide_port_info ht6560b_port_info __initdata = {
315         .name                   = DRV_NAME,
316         .chipset                = ide_ht6560b,
317         .port_ops               = &ht6560b_port_ops,
318         .host_flags             = IDE_HFLAG_SERIALIZE | /* is this needed? */
319                                   IDE_HFLAG_NO_DMA |
320                                   IDE_HFLAG_ABUSE_PREFETCH,
321         .pio_mask               = ATA_PIO4,
322 };
323
324 static int __init ht6560b_init(void)
325 {
326         if (probe_ht6560b == 0)
327                 return -ENODEV;
328
329         if (!request_region(HT_CONFIG_PORT, 1, DRV_NAME)) {
330                 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
331                         __func__);
332                 return -ENODEV;
333         }
334
335         if (!try_to_init_ht6560b()) {
336                 printk(KERN_NOTICE "%s: HBA not found\n", __func__);
337                 goto release_region;
338         }
339
340         return ide_legacy_device_add(&ht6560b_port_info, 0);
341
342 release_region:
343         release_region(HT_CONFIG_PORT, 1);
344         return -ENODEV;
345 }
346
347 module_init(ht6560b_init);
348
349 MODULE_AUTHOR("See Local File");
350 MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
351 MODULE_LICENSE("GPL");