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