ht6560b.c 9.7 KB

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