blob: 78ca68e60f97e141dd9fc96806aa8d33bcaafb10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995-2000 Linus Torvalds & author (see below)
3 */
4
5/*
6 *
7 * Version 0.01 Initial version hacked out of ide.c
8 *
9 * Version 0.02 Added support for PIO modes, auto-tune
10 *
11 * Version 0.03 Some cleanups
12 *
13 * Version 0.05 PIO mode cycle timings auto-tune using bus-speed
14 *
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.
19 *
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.
23 *
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +010024 * Version 0.08 Need to force prefetch for CDs and other non-disk
25 * devices. (not sure which devices exactly need
26 * prefetch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * HT-6560B EIDE-controller support
29 * To activate controller support use kernel parameter "ide0=ht6560b".
30 * Use hdparm utility to enable PIO mode support.
31 *
32 * Author: Mikko Ala-Fossi <maf@iki.fi>
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +010033 * Jan Evert van Grootheest <janevert@caiway.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * Try: http://www.maf.iki.fi/~maf/ht6560b/
36 */
37
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +010038#define HT6560B_VERSION "v0.08"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/kernel.h>
43#include <linux/delay.h>
44#include <linux/timer.h>
45#include <linux/mm.h>
46#include <linux/ioport.h>
47#include <linux/blkdev.h>
48#include <linux/hdreg.h>
49#include <linux/ide.h>
50#include <linux/init.h>
51
52#include <asm/io.h>
53
54/* #define DEBUG */ /* remove comments for DEBUG messages */
55
56/*
57 * The special i/o-port that HT-6560B uses to configuration:
58 * bit0 (0x01): "1" selects secondary interface
59 * bit2 (0x04): "1" enables FIFO function
60 * bit5 (0x20): "1" enables prefetched data read function (???)
61 *
62 * The special i/o-port that HT-6560A uses to configuration:
63 * bit0 (0x01): "1" selects secondary interface
64 * bit1 (0x02): "1" enables prefetched data read function
65 * bit2 (0x04): "0" enables multi-master system (?)
66 * bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time (?)
67 */
68#define HT_CONFIG_PORT 0x3e6
69#define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
70/*
71 * FIFO + PREFETCH (both a/b-model)
72 */
73#define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
74/* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
75#define HT_SECONDARY_IF 0x01
76#define HT_PREFETCH_MODE 0x20
77
78/*
79 * ht6560b Timing values:
80 *
81 * I reviewed some assembler source listings of htide drivers and found
82 * out how they setup those cycle time interfacing values, as they at Holtek
83 * call them. IDESETUP.COM that is supplied with the drivers figures out
84 * optimal values and fetches those values to drivers. I found out that
85 * they use IDE_SELECT_REG to fetch timings to the ide board right after
86 * interface switching. After that it was quite easy to add code to
87 * ht6560b.c.
88 *
89 * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
90 * for hda and hdc. But hdb needed higher values to work, so I guess
91 * that sometimes it is necessary to give higher value than IDESETUP
92 * gives. [see cmd640.c for an extreme example of this. -ml]
93 *
94 * Perhaps I should explain something about these timing values:
95 * The higher nibble of value is the Recovery Time (rt) and the lower nibble
96 * of the value is the Active Time (at). Minimum value 2 is the fastest and
97 * the maximum value 15 is the slowest. Default values should be 15 for both.
98 * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
99 * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
100 * similar. If value is too small there will be all sorts of failures.
101 *
102 * Timing byte consists of
103 * High nibble: Recovery Cycle Time (rt)
104 * The valid values range from 2 to 15. The default is 15.
105 *
106 * Low nibble: Active Cycle Time (at)
107 * The valid values range from 2 to 15. The default is 15.
108 *
109 * You can obtain optimized timing values by running Holtek IDESETUP.COM
110 * for DOS. DOS drivers get their timing values from command line, where
111 * the first value is the Recovery Time and the second value is the
112 * Active Time for each drive. Smaller value gives higher speed.
113 * In case of failures you should probably fall back to a higher value.
114 */
115#define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
116#define HT_TIMING_DEFAULT 0xff
117
118/*
119 * This routine handles interface switching for the peculiar hardware design
120 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
121 * The HT-6560B can only enable one IDE port at a time, and requires a
122 * silly sequence (below) whenever we switch between primary and secondary.
123 */
124
125/*
126 * This routine is invoked from ide.c to prepare for access to a given drive.
127 */
128static void ht6560b_selectproc (ide_drive_t *drive)
129{
130 unsigned long flags;
131 static u8 current_select = 0;
132 static u8 current_timing = 0;
133 u8 select, timing;
134
135 local_irq_save(flags);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 select = HT_CONFIG(drive);
138 timing = HT_TIMING(drive);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100139
140 /*
141 * Need to enforce prefetch sometimes because otherwise
142 * it'll hang (hard).
143 */
144 if (drive->media != ide_disk || !drive->present)
145 select |= HT_PREFETCH_MODE;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (select != current_select || timing != current_timing) {
148 current_select = select;
149 current_timing = timing;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100150 (void)inb(HT_CONFIG_PORT);
151 (void)inb(HT_CONFIG_PORT);
152 (void)inb(HT_CONFIG_PORT);
153 (void)inb(HT_CONFIG_PORT);
154 outb(select, HT_CONFIG_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /*
156 * Set timing for this drive:
157 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100158 outb(timing, IDE_SELECT_REG);
159 (void)inb(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#ifdef DEBUG
161 printk("ht6560b: %s: select=%#x timing=%#x\n",
162 drive->name, select, timing);
163#endif
164 }
165 local_irq_restore(flags);
166}
167
168/*
169 * Autodetection and initialization of ht6560b
170 */
171static int __init try_to_init_ht6560b(void)
172{
173 u8 orig_value;
174 int i;
175
176 /* Autodetect ht6560b */
177 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
178 return 0;
179
180 for (i=3;i>0;i--) {
181 outb(0x00, HT_CONFIG_PORT);
182 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
183 outb(orig_value, HT_CONFIG_PORT);
184 return 0;
185 }
186 }
187 outb(0x00, HT_CONFIG_PORT);
188 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
189 outb(orig_value, HT_CONFIG_PORT);
190 return 0;
191 }
192 /*
193 * Ht6560b autodetected
194 */
195 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
196 outb(HT_TIMING_DEFAULT, 0x1f6); /* IDE_SELECT_REG */
197 (void) inb(0x1f7); /* IDE_STATUS_REG */
198
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100199 printk("ht6560b " HT6560B_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ": chipset detected and initialized"
201#ifdef DEBUG
202 " with debug enabled"
203#endif
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100204 "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 );
206 return 1;
207}
208
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200209static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int active_time, recovery_time;
212 int active_cycles, recovery_cycles;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int bus_speed = system_bus_clock();
214
215 if (pio) {
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200216 unsigned int cycle_time;
217
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200218 cycle_time = ide_pio_cycle_time(drive, pio);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
221 * Just like opti621.c we try to calculate the
222 * actual cycle time for recovery and activity
223 * according system bus speed.
224 */
225 active_time = ide_pio_timings[pio].active_time;
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200226 recovery_time = cycle_time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 - active_time
228 - ide_pio_timings[pio].setup_time;
229 /*
230 * Cycle times should be Vesa bus cycles
231 */
232 active_cycles = (active_time * bus_speed + 999) / 1000;
233 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
234 /*
235 * Upper and lower limits
236 */
237 if (active_cycles < 2) active_cycles = 2;
238 if (recovery_cycles < 2) recovery_cycles = 2;
239 if (active_cycles > 15) active_cycles = 15;
240 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
241
242#ifdef DEBUG
243 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);
244#endif
245
246 return (u8)((recovery_cycles << 4) | active_cycles);
247 } else {
248
249#ifdef DEBUG
250 printk("ht6560b: drive %s setting pio=0\n", drive->name);
251#endif
252
253 return HT_TIMING_DEFAULT; /* default setting */
254 }
255}
256
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200257static DEFINE_SPINLOCK(ht6560b_lock);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Enable/Disable so called prefetch mode
261 */
262static void ht_set_prefetch(ide_drive_t *drive, u8 state)
263{
264 unsigned long flags;
265 int t = HT_PREFETCH_MODE << 8;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200266
267 spin_lock_irqsave(&ht6560b_lock, flags);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
270 * Prefetch mode and unmask irq seems to conflict
271 */
272 if (state) {
273 drive->drive_data |= t; /* enable prefetch mode */
274 drive->no_unmask = 1;
275 drive->unmask = 0;
276 } else {
277 drive->drive_data &= ~t; /* disable prefetch mode */
278 drive->no_unmask = 0;
279 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200280
281 spin_unlock_irqrestore(&ht6560b_lock, flags);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#ifdef DEBUG
284 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
285#endif
286}
287
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200288static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 unsigned long flags;
291 u8 timing;
292
293 switch (pio) {
294 case 8: /* set prefetch off */
295 case 9: /* set prefetch on */
296 ht_set_prefetch(drive, pio & 1);
297 return;
298 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 timing = ht_pio2timings(drive, pio);
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200301
302 spin_lock_irqsave(&ht6560b_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 drive->drive_data &= 0xff00;
304 drive->drive_data |= timing;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200305 spin_unlock_irqrestore(&ht6560b_lock, flags);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifdef DEBUG
308 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
309#endif
310}
311
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100312static void __init ht6560b_port_init_devs(ide_hwif_t *hwif)
313{
314 /* Setting default configurations for drives. */
315 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
316
317 if (hwif->channel)
318 t |= (HT_SECONDARY_IF << 8);
319
320 hwif->drives[0].drive_data = t;
321 hwif->drives[1].drive_data = t;
322}
323
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100324int probe_ht6560b = 0;
325
326module_param_named(probe, probe_ht6560b, bool, 0);
327MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
328
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100329static const struct ide_port_info ht6560b_port_info __initdata = {
330 .chipset = ide_ht6560b,
331 .host_flags = IDE_HFLAG_SERIALIZE | /* is this needed? */
332 IDE_HFLAG_NO_DMA |
333 IDE_HFLAG_NO_AUTOTUNE |
334 IDE_HFLAG_ABUSE_PREFETCH,
Jan Evert van Grootheest1a1990f2008-02-19 01:41:26 +0100335 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100336};
337
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100338static int __init ht6560b_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 ide_hwif_t *hwif, *mate;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200341 static u8 idx[4] = { 0, 1, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100343 if (probe_ht6560b == 0)
344 return -ENODEV;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 hwif = &ide_hwifs[0];
347 mate = &ide_hwifs[1];
348
349 if (!request_region(HT_CONFIG_PORT, 1, hwif->name)) {
350 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
351 __FUNCTION__);
352 return -ENODEV;
353 }
354
355 if (!try_to_init_ht6560b()) {
356 printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
357 goto release_region;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 hwif->selectproc = &ht6560b_selectproc;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200361 hwif->set_pio_mode = &ht6560b_set_pio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 mate->selectproc = &ht6560b_selectproc;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200364 mate->set_pio_mode = &ht6560b_set_pio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100366 hwif->port_init_devs = ht6560b_port_init_devs;
367 mate->port_init_devs = ht6560b_port_init_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100369 ide_device_add(idx, &ht6560b_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return 0;
372
373release_region:
374 release_region(HT_CONFIG_PORT, 1);
375 return -ENODEV;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378module_init(ht6560b_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380MODULE_AUTHOR("See Local File");
381MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
382MODULE_LICENSE("GPL");