ht6560b.c 10 KB

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