ht6560b.c 10 KB

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