blob: 2fb0f29650099c8c5ccb706c96dae78c85944c21 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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>
Jan Evert van Grootheesteb34b2d2008-07-24 22:53:35 +020011 * Jan Evert van Grootheest <j.e.van.grootheest@caiway.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Try: http://www.maf.iki.fi/~maf/ht6560b/
14 */
15
Bartlomiej Zolnierkiewicz2e4ed292008-04-26 17:36:35 +020016#define DRV_NAME "ht6560b"
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +010017#define HT6560B_VERSION "v0.08"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020063 * they use Select register to fetch timings to the ide board right after
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 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 */
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200106static void ht6560b_dev_select(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200108 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 select = HT_CONFIG(drive);
117 timing = HT_TIMING(drive);
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100118
119 /*
120 * Need to enforce prefetch sometimes because otherwise
121 * it'll hang (hard).
122 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200123 if (drive->media != ide_disk ||
124 (drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100125 select |= HT_PREFETCH_MODE;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (select != current_select || timing != current_timing) {
128 current_select = select;
129 current_timing = timing;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100130 (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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
136 * Set timing for this drive:
137 */
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200138 outb(timing, hwif->io_ports.device_addr);
139 (void)inb(hwif->io_ports.status_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#ifdef DEBUG
141 printk("ht6560b: %s: select=%#x timing=%#x\n",
142 drive->name, select, timing);
143#endif
144 }
145 local_irq_restore(flags);
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200146
147 outb(drive->select | ATA_DEVICE_OBS, hwif->io_ports.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/*
151 * Autodetection and initialization of ht6560b
152 */
153static int __init try_to_init_ht6560b(void)
154{
155 u8 orig_value;
156 int i;
157
158 /* Autodetect ht6560b */
159 if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
160 return 0;
161
162 for (i=3;i>0;i--) {
163 outb(0x00, HT_CONFIG_PORT);
164 if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
165 outb(orig_value, HT_CONFIG_PORT);
166 return 0;
167 }
168 }
169 outb(0x00, HT_CONFIG_PORT);
170 if ((~inb(HT_CONFIG_PORT))& 0x3f) {
171 outb(orig_value, HT_CONFIG_PORT);
172 return 0;
173 }
174 /*
175 * Ht6560b autodetected
176 */
177 outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200178 outb(HT_TIMING_DEFAULT, 0x1f6); /* Select register */
179 (void)inb(0x1f7); /* Status register */
180
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100181 printk("ht6560b " HT6560B_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ": chipset detected and initialized"
183#ifdef DEBUG
184 " with debug enabled"
185#endif
Jan Evert van Grootheest0e7d8d482008-02-19 01:41:26 +0100186 "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 );
188 return 1;
189}
190
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200191static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int active_time, recovery_time;
194 int active_cycles, recovery_cycles;
Bartlomiej Zolnierkiewicz30e5ee42008-07-15 21:21:46 +0200195 int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
Bartlomiej Zolnierkiewiczebae41a2008-04-27 15:38:29 +0200196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (pio) {
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200198 unsigned int cycle_time;
Bartlomiej Zolnierkiewiczb32b76f2008-07-16 20:33:37 +0200199 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200200
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200201 cycle_time = ide_pio_cycle_time(drive, pio);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /*
204 * Just like opti621.c we try to calculate the
205 * actual cycle time for recovery and activity
206 * according system bus speed.
207 */
Bartlomiej Zolnierkiewiczb32b76f2008-07-16 20:33:37 +0200208 active_time = t->active;
209 recovery_time = cycle_time - active_time - t->setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
211 * Cycle times should be Vesa bus cycles
212 */
213 active_cycles = (active_time * bus_speed + 999) / 1000;
214 recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
215 /*
216 * Upper and lower limits
217 */
218 if (active_cycles < 2) active_cycles = 2;
219 if (recovery_cycles < 2) recovery_cycles = 2;
220 if (active_cycles > 15) active_cycles = 15;
221 if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
222
223#ifdef DEBUG
224 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);
225#endif
226
227 return (u8)((recovery_cycles << 4) | active_cycles);
228 } else {
229
230#ifdef DEBUG
231 printk("ht6560b: drive %s setting pio=0\n", drive->name);
232#endif
233
234 return HT_TIMING_DEFAULT; /* default setting */
235 }
236}
237
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200238static DEFINE_SPINLOCK(ht6560b_lock);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Enable/Disable so called prefetch mode
242 */
243static void ht_set_prefetch(ide_drive_t *drive, u8 state)
244{
245 unsigned long flags;
246 int t = HT_PREFETCH_MODE << 8;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200247
248 spin_lock_irqsave(&ht6560b_lock, flags);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /*
251 * Prefetch mode and unmask irq seems to conflict
252 */
253 if (state) {
254 drive->drive_data |= t; /* enable prefetch mode */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200255 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
256 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
258 drive->drive_data &= ~t; /* disable prefetch mode */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200259 drive->dev_flags &= ~IDE_DFLAG_NO_UNMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200261
262 spin_unlock_irqrestore(&ht6560b_lock, flags);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#ifdef DEBUG
265 printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
266#endif
267}
268
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200269static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned long flags;
272 u8 timing;
273
274 switch (pio) {
275 case 8: /* set prefetch off */
276 case 9: /* set prefetch on */
277 ht_set_prefetch(drive, pio & 1);
278 return;
279 }
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 timing = ht_pio2timings(drive, pio);
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200282
283 spin_lock_irqsave(&ht6560b_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 drive->drive_data &= 0xff00;
285 drive->drive_data |= timing;
Bartlomiej Zolnierkiewicz69e88d22007-10-20 00:32:35 +0200286 spin_unlock_irqrestore(&ht6560b_lock, flags);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#ifdef DEBUG
289 printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
290#endif
291}
292
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200293static void __init ht6560b_init_dev(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100294{
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200295 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100296 /* Setting default configurations for drives. */
297 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
298
299 if (hwif->channel)
300 t |= (HT_SECONDARY_IF << 8);
301
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200302 drive->drive_data = t;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +0100303}
304
Bartlomiej Zolnierkiewiczef87f8d2008-04-27 15:38:24 +0200305static int probe_ht6560b;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100306
307module_param_named(probe, probe_ht6560b, bool, 0);
308MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
309
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200310static const struct ide_tp_ops ht6560b_tp_ops = {
311 .exec_command = ide_exec_command,
312 .read_status = ide_read_status,
313 .read_altstatus = ide_read_altstatus,
314 .write_devctl = ide_write_devctl,
315
316 .dev_select = ht6560b_dev_select,
317 .tf_load = ide_tf_load,
318 .tf_read = ide_tf_read,
319
320 .input_data = ide_input_data,
321 .output_data = ide_output_data,
322};
323
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200324static const struct ide_port_ops ht6560b_port_ops = {
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +0200325 .init_dev = ht6560b_init_dev,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200326 .set_pio_mode = ht6560b_set_pio_mode,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200327};
328
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100329static const struct ide_port_info ht6560b_port_info __initdata = {
Bartlomiej Zolnierkiewiczd92f1a22008-04-26 22:25:18 +0200330 .name = DRV_NAME,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100331 .chipset = ide_ht6560b,
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200332 .tp_ops = &ht6560b_tp_ops,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200333 .port_ops = &ht6560b_port_ops,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100334 .host_flags = IDE_HFLAG_SERIALIZE | /* is this needed? */
335 IDE_HFLAG_NO_DMA |
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100336 IDE_HFLAG_ABUSE_PREFETCH,
Jan Evert van Grootheest1a1990f2008-02-19 01:41:26 +0100337 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100338};
339
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100340static int __init ht6560b_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100342 if (probe_ht6560b == 0)
343 return -ENODEV;
344
Bartlomiej Zolnierkiewicz2e4ed292008-04-26 17:36:35 +0200345 if (!request_region(HT_CONFIG_PORT, 1, DRV_NAME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200347 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -ENODEV;
349 }
350
351 if (!try_to_init_ht6560b()) {
Harvey Harrisoneb639632008-04-26 22:25:20 +0200352 printk(KERN_NOTICE "%s: HBA not found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto release_region;
354 }
355
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +0200356 return ide_legacy_device_add(&ht6560b_port_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358release_region:
359 release_region(HT_CONFIG_PORT, 1);
360 return -ENODEV;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363module_init(ht6560b_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365MODULE_AUTHOR("See Local File");
366MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
367MODULE_LICENSE("GPL");