ht6560b.c 9.7 KB

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