ht6560b.c 10 KB

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