blob: 7262b3ad45c6f263ed7faf2413f127b5378582e2 [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 Grootheesteb34b2d2008-07-24 22:53:35 +020033 * Jan Evert van Grootheest <j.e.van.grootheest@caiway.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * Try: http://www.maf.iki.fi/~maf/ht6560b/
36 */
37
Bartlomiej Zolnierkiewicz2e4ed292008-04-26 17:36:35 +020038#define DRV_NAME "ht6560b"
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +010039#define HT6560B_VERSION "v0.08"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020086 * they use Select register to fetch timings to the ide board right after
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * 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 */
129static void ht6560b_selectproc (ide_drive_t *drive)
130{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200131 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long flags;
133 static u8 current_select = 0;
134 static u8 current_timing = 0;
135 u8 select, timing;
136
137 local_irq_save(flags);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 select = HT_CONFIG(drive);
140 timing = HT_TIMING(drive);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100141
142 /*
143 * Need to enforce prefetch sometimes because otherwise
144 * it'll hang (hard).
145 */
146 if (drive->media != ide_disk || !drive->present)
147 select |= HT_PREFETCH_MODE;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (select != current_select || timing != current_timing) {
150 current_select = select;
151 current_timing = timing;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100152 (void)inb(HT_CONFIG_PORT);
153 (void)inb(HT_CONFIG_PORT);
154 (void)inb(HT_CONFIG_PORT);
155 (void)inb(HT_CONFIG_PORT);
156 outb(select, HT_CONFIG_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * Set timing for this drive:
159 */
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200160 outb(timing, hwif->io_ports.device_addr);
161 (void)inb(hwif->io_ports.status_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#ifdef DEBUG
163 printk("ht6560b: %s: select=%#x timing=%#x\n",
164 drive->name, select, timing);
165#endif
166 }
167 local_irq_restore(flags);
168}
169
170/*
171 * Autodetection and initialization of ht6560b
172 */
173static int __init try_to_init_ht6560b(void)
174{
175 u8 orig_value;
176 int i;
177
178 /* Autodetect ht6560b */
179 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
180 return 0;
181
182 for (i=3;i>0;i--) {
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 outb(0x00, HT_CONFIG_PORT);
190 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
191 outb(orig_value, HT_CONFIG_PORT);
192 return 0;
193 }
194 /*
195 * Ht6560b autodetected
196 */
197 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200198 outb(HT_TIMING_DEFAULT, 0x1f6); /* Select register */
199 (void)inb(0x1f7); /* Status register */
200
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100201 printk("ht6560b " HT6560B_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 ": chipset detected and initialized"
203#ifdef DEBUG
204 " with debug enabled"
205#endif
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100206 "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 );
208 return 1;
209}
210
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200211static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int active_time, recovery_time;
214 int active_cycles, recovery_cycles;
Bartlomiej Zolnierkiewicz30e5ee42008-07-15 21:21:46 +0200215 int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
Bartlomiej Zolnierkiewiczebae41a2008-04-27 15:38:29 +0200216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (pio) {
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200218 unsigned int cycle_time;
Bartlomiej Zolnierkiewiczb32b76f2008-07-16 20:33:37 +0200219 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200220
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200221 cycle_time = ide_pio_cycle_time(drive, pio);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
224 * Just like opti621.c we try to calculate the
225 * actual cycle time for recovery and activity
226 * according system bus speed.
227 */
Bartlomiej Zolnierkiewiczb32b76f2008-07-16 20:33:37 +0200228 active_time = t->active;
229 recovery_time = cycle_time - active_time - t->setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Cycle times should be Vesa bus cycles
232 */
233 active_cycles = (active_time * bus_speed + 999) / 1000;
234 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
235 /*
236 * Upper and lower limits
237 */
238 if (active_cycles < 2) active_cycles = 2;
239 if (recovery_cycles < 2) recovery_cycles = 2;
240 if (active_cycles > 15) active_cycles = 15;
241 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
242
243#ifdef DEBUG
244 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);
245#endif
246
247 return (u8)((recovery_cycles << 4) | active_cycles);
248 } else {
249
250#ifdef DEBUG
251 printk("ht6560b: drive %s setting pio=0\n", drive->name);
252#endif
253
254 return HT_TIMING_DEFAULT; /* default setting */
255 }
256}
257
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200258static DEFINE_SPINLOCK(ht6560b_lock);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * Enable/Disable so called prefetch mode
262 */
263static void ht_set_prefetch(ide_drive_t *drive, u8 state)
264{
265 unsigned long flags;
266 int t = HT_PREFETCH_MODE << 8;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200267
268 spin_lock_irqsave(&ht6560b_lock, flags);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /*
271 * Prefetch mode and unmask irq seems to conflict
272 */
273 if (state) {
274 drive->drive_data |= t; /* enable prefetch mode */
275 drive->no_unmask = 1;
276 drive->unmask = 0;
277 } else {
278 drive->drive_data &= ~t; /* disable prefetch mode */
279 drive->no_unmask = 0;
280 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200281
282 spin_unlock_irqrestore(&ht6560b_lock, flags);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#ifdef DEBUG
285 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
286#endif
287}
288
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200289static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long flags;
292 u8 timing;
293
294 switch (pio) {
295 case 8: /* set prefetch off */
296 case 9: /* set prefetch on */
297 ht_set_prefetch(drive, pio & 1);
298 return;
299 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 timing = ht_pio2timings(drive, pio);
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200302
303 spin_lock_irqsave(&ht6560b_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 drive->drive_data &= 0xff00;
305 drive->drive_data |= timing;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200306 spin_unlock_irqrestore(&ht6560b_lock, flags);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#ifdef DEBUG
309 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
310#endif
311}
312
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200313static void __init ht6560b_init_dev(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100314{
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200315 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100316 /* Setting default configurations for drives. */
317 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
318
319 if (hwif->channel)
320 t |= (HT_SECONDARY_IF << 8);
321
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200322 drive->drive_data = t;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100323}
324
Bartlomiej Zolnierkiewiczef87f8d2008-04-27 15:38:24 +0200325static int probe_ht6560b;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100326
327module_param_named(probe, probe_ht6560b, bool, 0);
328MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
329
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200330static const struct ide_port_ops ht6560b_port_ops = {
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200331 .init_dev = ht6560b_init_dev,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200332 .set_pio_mode = ht6560b_set_pio_mode,
333 .selectproc = ht6560b_selectproc,
334};
335
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100336static const struct ide_port_info ht6560b_port_info __initdata = {
Bartlomiej Zolnierkiewiczd92f1a22008-04-26 22:25:18 +0200337 .name = DRV_NAME,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100338 .chipset = ide_ht6560b,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200339 .port_ops = &ht6560b_port_ops,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100340 .host_flags = IDE_HFLAG_SERIALIZE | /* is this needed? */
341 IDE_HFLAG_NO_DMA |
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100342 IDE_HFLAG_ABUSE_PREFETCH,
Jan Evert van Grootheest1a1990f2008-02-19 01:41:26 +0100343 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100344};
345
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100346static int __init ht6560b_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100348 if (probe_ht6560b == 0)
349 return -ENODEV;
350
Bartlomiej Zolnierkiewicz2e4ed292008-04-26 17:36:35 +0200351 if (!request_region(HT_CONFIG_PORT, 1, DRV_NAME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200353 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -ENODEV;
355 }
356
357 if (!try_to_init_ht6560b()) {
Harvey Harrisoneb639632008-04-26 22:25:20 +0200358 printk(KERN_NOTICE "%s: HBA not found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto release_region;
360 }
361
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +0200362 return ide_legacy_device_add(&ht6560b_port_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364release_region:
365 release_region(HT_CONFIG_PORT, 1);
366 return -ENODEV;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369module_init(ht6560b_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371MODULE_AUTHOR("See Local File");
372MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
373MODULE_LICENSE("GPL");