ht6560b.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * Version 0.01 Initial version hacked out of ide.c
  9. *
  10. * Version 0.02 Added support for PIO modes, auto-tune
  11. *
  12. * Version 0.03 Some cleanups
  13. *
  14. * Version 0.05 PIO mode cycle timings auto-tune using bus-speed
  15. *
  16. * Version 0.06 Prefetch mode now defaults no OFF. To set
  17. * prefetch mode OFF/ON use "hdparm -p8/-p9".
  18. * Unmask irq is disabled when prefetch mode
  19. * is enabled.
  20. *
  21. * Version 0.07 Trying to fix CD-ROM detection problem.
  22. * "Prefetch" mode bit OFF for ide disks and
  23. * ON for anything else.
  24. *
  25. *
  26. * HT-6560B EIDE-controller support
  27. * To activate controller support use kernel parameter "ide0=ht6560b".
  28. * Use hdparm utility to enable PIO mode support.
  29. *
  30. * Author: Mikko Ala-Fossi <maf@iki.fi>
  31. * Jan Evert van Grootheest <janevert@iae.nl>
  32. *
  33. * Try: http://www.maf.iki.fi/~maf/ht6560b/
  34. */
  35. #define HT6560B_VERSION "v0.07"
  36. #undef REALLY_SLOW_IO /* most systems can safely undef this */
  37. #include <linux/module.h>
  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. #include <asm/io.h>
  49. /* #define DEBUG */ /* remove comments for DEBUG messages */
  50. /*
  51. * The special i/o-port that HT-6560B uses to configuration:
  52. * bit0 (0x01): "1" selects secondary interface
  53. * bit2 (0x04): "1" enables FIFO function
  54. * bit5 (0x20): "1" enables prefetched data read function (???)
  55. *
  56. * The special i/o-port that HT-6560A uses to configuration:
  57. * bit0 (0x01): "1" selects secondary interface
  58. * bit1 (0x02): "1" enables prefetched data read function
  59. * bit2 (0x04): "0" enables multi-master system (?)
  60. * bit3 (0x08): "1" 3 cycle time, "0" 2 cycle time (?)
  61. */
  62. #define HT_CONFIG_PORT 0x3e6
  63. #define HT_CONFIG(drivea) (u8)(((drivea)->drive_data & 0xff00) >> 8)
  64. /*
  65. * FIFO + PREFETCH (both a/b-model)
  66. */
  67. #define HT_CONFIG_DEFAULT 0x1c /* no prefetch */
  68. /* #define HT_CONFIG_DEFAULT 0x3c */ /* with prefetch */
  69. #define HT_SECONDARY_IF 0x01
  70. #define HT_PREFETCH_MODE 0x20
  71. /*
  72. * ht6560b Timing values:
  73. *
  74. * I reviewed some assembler source listings of htide drivers and found
  75. * out how they setup those cycle time interfacing values, as they at Holtek
  76. * call them. IDESETUP.COM that is supplied with the drivers figures out
  77. * optimal values and fetches those values to drivers. I found out that
  78. * they use IDE_SELECT_REG to fetch timings to the ide board right after
  79. * interface switching. After that it was quite easy to add code to
  80. * ht6560b.c.
  81. *
  82. * IDESETUP.COM gave me values 0x24, 0x45, 0xaa, 0xff that worked fine
  83. * for hda and hdc. But hdb needed higher values to work, so I guess
  84. * that sometimes it is necessary to give higher value than IDESETUP
  85. * gives. [see cmd640.c for an extreme example of this. -ml]
  86. *
  87. * Perhaps I should explain something about these timing values:
  88. * The higher nibble of value is the Recovery Time (rt) and the lower nibble
  89. * of the value is the Active Time (at). Minimum value 2 is the fastest and
  90. * the maximum value 15 is the slowest. Default values should be 15 for both.
  91. * So 0x24 means 2 for rt and 4 for at. Each of the drives should have
  92. * both values, and IDESETUP gives automatically rt=15 st=15 for CDROMs or
  93. * similar. If value is too small there will be all sorts of failures.
  94. *
  95. * Timing byte consists of
  96. * High nibble: Recovery Cycle Time (rt)
  97. * The valid values range from 2 to 15. The default is 15.
  98. *
  99. * Low nibble: Active Cycle Time (at)
  100. * The valid values range from 2 to 15. The default is 15.
  101. *
  102. * You can obtain optimized timing values by running Holtek IDESETUP.COM
  103. * for DOS. DOS drivers get their timing values from command line, where
  104. * the first value is the Recovery Time and the second value is the
  105. * Active Time for each drive. Smaller value gives higher speed.
  106. * In case of failures you should probably fall back to a higher value.
  107. */
  108. #define HT_TIMING(drivea) (u8)((drivea)->drive_data & 0x00ff)
  109. #define HT_TIMING_DEFAULT 0xff
  110. /*
  111. * This routine handles interface switching for the peculiar hardware design
  112. * on the F.G.I./Holtek HT-6560B VLB IDE interface.
  113. * The HT-6560B can only enable one IDE port at a time, and requires a
  114. * silly sequence (below) whenever we switch between primary and secondary.
  115. */
  116. /*
  117. * This routine is invoked from ide.c to prepare for access to a given drive.
  118. */
  119. static void ht6560b_selectproc (ide_drive_t *drive)
  120. {
  121. unsigned long flags;
  122. static u8 current_select = 0;
  123. static u8 current_timing = 0;
  124. u8 select, timing;
  125. local_irq_save(flags);
  126. select = HT_CONFIG(drive);
  127. timing = HT_TIMING(drive);
  128. if (select != current_select || timing != current_timing) {
  129. current_select = select;
  130. current_timing = timing;
  131. if (drive->media != ide_disk || !drive->present)
  132. select |= HT_PREFETCH_MODE;
  133. (void) HWIF(drive)->INB(HT_CONFIG_PORT);
  134. (void) HWIF(drive)->INB(HT_CONFIG_PORT);
  135. (void) HWIF(drive)->INB(HT_CONFIG_PORT);
  136. (void) HWIF(drive)->INB(HT_CONFIG_PORT);
  137. HWIF(drive)->OUTB(select, HT_CONFIG_PORT);
  138. /*
  139. * Set timing for this drive:
  140. */
  141. HWIF(drive)->OUTB(timing, IDE_SELECT_REG);
  142. (void) HWIF(drive)->INB(IDE_STATUS_REG);
  143. #ifdef DEBUG
  144. printk("ht6560b: %s: select=%#x timing=%#x\n",
  145. drive->name, select, timing);
  146. #endif
  147. }
  148. local_irq_restore(flags);
  149. }
  150. /*
  151. * Autodetection and initialization of ht6560b
  152. */
  153. static int __init try_to_init_ht6560b(void)
  154. {
  155. u8 orig_value;
  156. int i;
  157. /* Autodetect ht6560b */
  158. if ((orig_value = inb(HT_CONFIG_PORT)) == 0xff)
  159. return 0;
  160. for (i=3;i>0;i--) {
  161. outb(0x00, HT_CONFIG_PORT);
  162. if (!( (~inb(HT_CONFIG_PORT)) & 0x3f )) {
  163. outb(orig_value, HT_CONFIG_PORT);
  164. return 0;
  165. }
  166. }
  167. outb(0x00, HT_CONFIG_PORT);
  168. if ((~inb(HT_CONFIG_PORT))& 0x3f) {
  169. outb(orig_value, HT_CONFIG_PORT);
  170. return 0;
  171. }
  172. /*
  173. * Ht6560b autodetected
  174. */
  175. outb(HT_CONFIG_DEFAULT, HT_CONFIG_PORT);
  176. outb(HT_TIMING_DEFAULT, 0x1f6); /* IDE_SELECT_REG */
  177. (void) inb(0x1f7); /* IDE_STATUS_REG */
  178. printk("\nht6560b " HT6560B_VERSION
  179. ": chipset detected and initialized"
  180. #ifdef DEBUG
  181. " with debug enabled"
  182. #endif
  183. );
  184. return 1;
  185. }
  186. static u8 ht_pio2timings(ide_drive_t *drive, u8 pio)
  187. {
  188. int active_time, recovery_time;
  189. int active_cycles, recovery_cycles;
  190. ide_pio_data_t d;
  191. int bus_speed = system_bus_clock();
  192. if (pio) {
  193. pio = ide_get_best_pio_mode(drive, pio, 5, &d);
  194. /*
  195. * Just like opti621.c we try to calculate the
  196. * actual cycle time for recovery and activity
  197. * according system bus speed.
  198. */
  199. active_time = ide_pio_timings[pio].active_time;
  200. recovery_time = d.cycle_time
  201. - active_time
  202. - ide_pio_timings[pio].setup_time;
  203. /*
  204. * Cycle times should be Vesa bus cycles
  205. */
  206. active_cycles = (active_time * bus_speed + 999) / 1000;
  207. recovery_cycles = (recovery_time * bus_speed + 999) / 1000;
  208. /*
  209. * Upper and lower limits
  210. */
  211. if (active_cycles < 2) active_cycles = 2;
  212. if (recovery_cycles < 2) recovery_cycles = 2;
  213. if (active_cycles > 15) active_cycles = 15;
  214. if (recovery_cycles > 15) recovery_cycles = 0; /* 0==16 */
  215. #ifdef DEBUG
  216. 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);
  217. #endif
  218. return (u8)((recovery_cycles << 4) | active_cycles);
  219. } else {
  220. #ifdef DEBUG
  221. printk("ht6560b: drive %s setting pio=0\n", drive->name);
  222. #endif
  223. return HT_TIMING_DEFAULT; /* default setting */
  224. }
  225. }
  226. /*
  227. * Enable/Disable so called prefetch mode
  228. */
  229. static void ht_set_prefetch(ide_drive_t *drive, u8 state)
  230. {
  231. unsigned long flags;
  232. int t = HT_PREFETCH_MODE << 8;
  233. spin_lock_irqsave(&ide_lock, flags);
  234. /*
  235. * Prefetch mode and unmask irq seems to conflict
  236. */
  237. if (state) {
  238. drive->drive_data |= t; /* enable prefetch mode */
  239. drive->no_unmask = 1;
  240. drive->unmask = 0;
  241. } else {
  242. drive->drive_data &= ~t; /* disable prefetch mode */
  243. drive->no_unmask = 0;
  244. }
  245. spin_unlock_irqrestore(&ide_lock, flags);
  246. #ifdef DEBUG
  247. printk("ht6560b: drive %s prefetch mode %sabled\n", drive->name, (state ? "en" : "dis"));
  248. #endif
  249. }
  250. static void tune_ht6560b (ide_drive_t *drive, u8 pio)
  251. {
  252. unsigned long flags;
  253. u8 timing;
  254. switch (pio) {
  255. case 8: /* set prefetch off */
  256. case 9: /* set prefetch on */
  257. ht_set_prefetch(drive, pio & 1);
  258. return;
  259. }
  260. timing = ht_pio2timings(drive, pio);
  261. spin_lock_irqsave(&ide_lock, flags);
  262. drive->drive_data &= 0xff00;
  263. drive->drive_data |= timing;
  264. spin_unlock_irqrestore(&ide_lock, flags);
  265. #ifdef DEBUG
  266. printk("ht6560b: drive %s tuned to pio mode %#x timing=%#x\n", drive->name, pio, timing);
  267. #endif
  268. }
  269. /* Can be called directly from ide.c. */
  270. int __init ht6560b_init(void)
  271. {
  272. ide_hwif_t *hwif, *mate;
  273. int t;
  274. hwif = &ide_hwifs[0];
  275. mate = &ide_hwifs[1];
  276. if (!request_region(HT_CONFIG_PORT, 1, hwif->name)) {
  277. printk(KERN_NOTICE "%s: HT_CONFIG_PORT not found\n",
  278. __FUNCTION__);
  279. return -ENODEV;
  280. }
  281. if (!try_to_init_ht6560b()) {
  282. printk(KERN_NOTICE "%s: HBA not found\n", __FUNCTION__);
  283. goto release_region;
  284. }
  285. hwif->chipset = ide_ht6560b;
  286. hwif->selectproc = &ht6560b_selectproc;
  287. hwif->tuneproc = &tune_ht6560b;
  288. hwif->serialized = 1; /* is this needed? */
  289. hwif->mate = mate;
  290. mate->chipset = ide_ht6560b;
  291. mate->selectproc = &ht6560b_selectproc;
  292. mate->tuneproc = &tune_ht6560b;
  293. mate->serialized = 1; /* is this needed? */
  294. mate->mate = hwif;
  295. mate->channel = 1;
  296. /*
  297. * Setting default configurations for drives
  298. */
  299. t = (HT_CONFIG_DEFAULT << 8);
  300. t |= HT_TIMING_DEFAULT;
  301. hwif->drives[0].drive_data = t;
  302. hwif->drives[1].drive_data = t;
  303. t |= (HT_SECONDARY_IF << 8);
  304. mate->drives[0].drive_data = t;
  305. mate->drives[1].drive_data = t;
  306. probe_hwif_init(hwif);
  307. probe_hwif_init(mate);
  308. create_proc_ide_interfaces();
  309. return 0;
  310. release_region:
  311. release_region(HT_CONFIG_PORT, 1);
  312. return -ENODEV;
  313. }
  314. #ifdef MODULE
  315. module_init(ht6560b_init);
  316. #endif
  317. MODULE_AUTHOR("See Local File");
  318. MODULE_DESCRIPTION("HT-6560B EIDE-controller support");
  319. MODULE_LICENSE("GPL");