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