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