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