qd65xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1996-2001 Linus Torvalds & author (see below)
  4. */
  5. /*
  6. * Version 0.03 Cleaned auto-tune, added probe
  7. * Version 0.04 Added second channel tuning
  8. * Version 0.05 Enhanced tuning ; added qd6500 support
  9. * Version 0.06 Added dos driver's list
  10. * Version 0.07 Second channel bug fix
  11. *
  12. * QDI QD6500/QD6580 EIDE controller fast support
  13. *
  14. * To activate controller support, use "ide0=qd65xx"
  15. */
  16. /*
  17. * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
  18. * Samuel Thibault <samuel.thibault@ens-lyon.org>
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/ioport.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/ide.h>
  29. #include <linux/init.h>
  30. #include <asm/io.h>
  31. #define DRV_NAME "qd65xx"
  32. #include "qd65xx.h"
  33. /*
  34. * I/O ports are 0x30-0x31 (and 0x32-0x33 for qd6580)
  35. * or 0xb0-0xb1 (and 0xb2-0xb3 for qd6580)
  36. * -- qd6500 is a single IDE interface
  37. * -- qd6580 is a dual IDE interface
  38. *
  39. * More research on qd6580 being done by willmore@cig.mot.com (David)
  40. * More Information given by Petr Soucek (petr@ryston.cz)
  41. * http://www.ryston.cz/petr/vlb
  42. */
  43. /*
  44. * base: Timer1
  45. *
  46. *
  47. * base+0x01: Config (R/O)
  48. *
  49. * bit 0: ide baseport: 1 = 0x1f0 ; 0 = 0x170 (only useful for qd6500)
  50. * bit 1: qd65xx baseport: 1 = 0xb0 ; 0 = 0x30
  51. * bit 2: ID3: bus speed: 1 = <=33MHz ; 0 = >33MHz
  52. * bit 3: qd6500: 1 = disabled, 0 = enabled
  53. * qd6580: 1
  54. * upper nibble:
  55. * qd6500: 1100
  56. * qd6580: either 1010 or 0101
  57. *
  58. *
  59. * base+0x02: Timer2 (qd6580 only)
  60. *
  61. *
  62. * base+0x03: Control (qd6580 only)
  63. *
  64. * bits 0-3 must always be set 1
  65. * bit 4 must be set 1, but is set 0 by dos driver while measuring vlb clock
  66. * bit 0 : 1 = Only primary port enabled : channel 0 for hda, channel 1 for hdb
  67. * 0 = Primary and Secondary ports enabled : channel 0 for hda & hdb
  68. * channel 1 for hdc & hdd
  69. * bit 1 : 1 = only disks on primary port
  70. * 0 = disks & ATAPI devices on primary port
  71. * bit 2-4 : always 0
  72. * bit 5 : status, but of what ?
  73. * bit 6 : always set 1 by dos driver
  74. * bit 7 : set 1 for non-ATAPI devices on primary port
  75. * (maybe read-ahead and post-write buffer ?)
  76. */
  77. static int timings[4]={-1,-1,-1,-1}; /* stores current timing for each timer */
  78. /*
  79. * qd65xx_select:
  80. *
  81. * This routine is invoked to prepare for access to a given drive.
  82. */
  83. static void qd65xx_dev_select(ide_drive_t *drive)
  84. {
  85. u8 index = (( (QD_TIMREG(drive)) & 0x80 ) >> 7) |
  86. (QD_TIMREG(drive) & 0x02);
  87. if (timings[index] != QD_TIMING(drive))
  88. outb(timings[index] = QD_TIMING(drive), QD_TIMREG(drive));
  89. outb(drive->select | ATA_DEVICE_OBS, drive->hwif->io_ports.device_addr);
  90. }
  91. /*
  92. * qd6500_compute_timing
  93. *
  94. * computes the timing value where
  95. * lower nibble represents active time, in count of VLB clocks
  96. * upper nibble represents recovery time, in count of VLB clocks
  97. */
  98. static u8 qd6500_compute_timing (ide_hwif_t *hwif, int active_time, int recovery_time)
  99. {
  100. int clk = ide_vlb_clk ? ide_vlb_clk : 50;
  101. u8 act_cyc, rec_cyc;
  102. if (clk <= 33) {
  103. act_cyc = 9 - IDE_IN(active_time * clk / 1000 + 1, 2, 9);
  104. rec_cyc = 15 - IDE_IN(recovery_time * clk / 1000 + 1, 0, 15);
  105. } else {
  106. act_cyc = 8 - IDE_IN(active_time * clk / 1000 + 1, 1, 8);
  107. rec_cyc = 18 - IDE_IN(recovery_time * clk / 1000 + 1, 3, 18);
  108. }
  109. return (rec_cyc << 4) | 0x08 | act_cyc;
  110. }
  111. /*
  112. * qd6580_compute_timing
  113. *
  114. * idem for qd6580
  115. */
  116. static u8 qd6580_compute_timing (int active_time, int recovery_time)
  117. {
  118. int clk = ide_vlb_clk ? ide_vlb_clk : 50;
  119. u8 act_cyc, rec_cyc;
  120. act_cyc = 17 - IDE_IN(active_time * clk / 1000 + 1, 2, 17);
  121. rec_cyc = 15 - IDE_IN(recovery_time * clk / 1000 + 1, 2, 15);
  122. return (rec_cyc << 4) | act_cyc;
  123. }
  124. /*
  125. * qd_find_disk_type
  126. *
  127. * tries to find timing from dos driver's table
  128. */
  129. static int qd_find_disk_type (ide_drive_t *drive,
  130. int *active_time, int *recovery_time)
  131. {
  132. struct qd65xx_timing_s *p;
  133. char *m = (char *)&drive->id[ATA_ID_PROD];
  134. char model[ATA_ID_PROD_LEN];
  135. if (*m == 0)
  136. return 0;
  137. strncpy(model, m, ATA_ID_PROD_LEN);
  138. ide_fixstring(model, ATA_ID_PROD_LEN, 1); /* byte-swap */
  139. for (p = qd65xx_timing ; p->offset != -1 ; p++) {
  140. if (!strncmp(p->model, model+p->offset, 4)) {
  141. printk(KERN_DEBUG "%s: listed !\n", drive->name);
  142. *active_time = p->active;
  143. *recovery_time = p->recovery;
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. /*
  150. * qd_set_timing:
  151. *
  152. * records the timing
  153. */
  154. static void qd_set_timing (ide_drive_t *drive, u8 timing)
  155. {
  156. unsigned long data = (unsigned long)ide_get_drivedata(drive);
  157. data &= 0xff00;
  158. data |= timing;
  159. ide_set_drivedata(drive, (void *)data);
  160. printk(KERN_DEBUG "%s: %#x\n", drive->name, timing);
  161. }
  162. static void qd6500_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  163. {
  164. u16 *id = drive->id;
  165. int active_time = 175;
  166. int recovery_time = 415; /* worst case values from the dos driver */
  167. /* FIXME: use drive->pio_mode value */
  168. if (!qd_find_disk_type(drive, &active_time, &recovery_time) &&
  169. (id[ATA_ID_OLD_PIO_MODES] & 0xff) && (id[ATA_ID_FIELD_VALID] & 2) &&
  170. id[ATA_ID_EIDE_PIO] >= 240) {
  171. printk(KERN_INFO "%s: PIO mode%d\n", drive->name,
  172. id[ATA_ID_OLD_PIO_MODES] & 0xff);
  173. active_time = 110;
  174. recovery_time = drive->id[ATA_ID_EIDE_PIO] - 120;
  175. }
  176. qd_set_timing(drive, qd6500_compute_timing(drive->hwif,
  177. active_time, recovery_time));
  178. }
  179. static void qd6580_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  180. {
  181. const u8 pio = drive->pio_mode - XFER_PIO_0;
  182. struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
  183. unsigned int cycle_time;
  184. int active_time = 175;
  185. int recovery_time = 415; /* worst case values from the dos driver */
  186. u8 base = (hwif->config_data & 0xff00) >> 8;
  187. if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)) {
  188. cycle_time = ide_pio_cycle_time(drive, pio);
  189. switch (pio) {
  190. case 0: break;
  191. case 3:
  192. if (cycle_time >= 110) {
  193. active_time = 86;
  194. recovery_time = cycle_time - 102;
  195. } else
  196. printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
  197. break;
  198. case 4:
  199. if (cycle_time >= 69) {
  200. active_time = 70;
  201. recovery_time = cycle_time - 61;
  202. } else
  203. printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
  204. break;
  205. default:
  206. if (cycle_time >= 180) {
  207. active_time = 110;
  208. recovery_time = cycle_time - 120;
  209. } else {
  210. active_time = t->active;
  211. recovery_time = cycle_time - active_time;
  212. }
  213. }
  214. printk(KERN_INFO "%s: PIO mode%d\n", drive->name,pio);
  215. }
  216. if (!hwif->channel && drive->media != ide_disk) {
  217. outb(0x5f, QD_CONTROL_PORT);
  218. printk(KERN_WARNING "%s: ATAPI: disabled read-ahead FIFO "
  219. "and post-write buffer on %s.\n",
  220. drive->name, hwif->name);
  221. }
  222. qd_set_timing(drive, qd6580_compute_timing(active_time, recovery_time));
  223. }
  224. /*
  225. * qd_testreg
  226. *
  227. * tests if the given port is a register
  228. */
  229. static int __init qd_testreg(int port)
  230. {
  231. unsigned long flags;
  232. u8 savereg, readreg;
  233. local_irq_save(flags);
  234. savereg = inb_p(port);
  235. outb_p(QD_TESTVAL, port); /* safe value */
  236. readreg = inb_p(port);
  237. outb(savereg, port);
  238. local_irq_restore(flags);
  239. if (savereg == QD_TESTVAL) {
  240. printk(KERN_ERR "Outch ! the probe for qd65xx isn't reliable !\n");
  241. printk(KERN_ERR "Please contact maintainers to tell about your hardware\n");
  242. printk(KERN_ERR "Assuming qd65xx is not present.\n");
  243. return 1;
  244. }
  245. return (readreg != QD_TESTVAL);
  246. }
  247. static void __init qd6500_init_dev(ide_drive_t *drive)
  248. {
  249. ide_hwif_t *hwif = drive->hwif;
  250. u8 base = (hwif->config_data & 0xff00) >> 8;
  251. u8 config = QD_CONFIG(hwif);
  252. ide_set_drivedata(drive, (void *)QD6500_DEF_DATA);
  253. }
  254. static void __init qd6580_init_dev(ide_drive_t *drive)
  255. {
  256. ide_hwif_t *hwif = drive->hwif;
  257. unsigned long t1, t2;
  258. u8 base = (hwif->config_data & 0xff00) >> 8;
  259. u8 config = QD_CONFIG(hwif);
  260. if (hwif->host_flags & IDE_HFLAG_SINGLE) {
  261. t1 = QD6580_DEF_DATA;
  262. t2 = QD6580_DEF_DATA2;
  263. } else
  264. t2 = t1 = hwif->channel ? QD6580_DEF_DATA2 : QD6580_DEF_DATA;
  265. ide_set_drivedata(drive, (void *)((drive->dn & 1) ? t2 : t1));
  266. }
  267. static const struct ide_tp_ops qd65xx_tp_ops = {
  268. .exec_command = ide_exec_command,
  269. .read_status = ide_read_status,
  270. .read_altstatus = ide_read_altstatus,
  271. .write_devctl = ide_write_devctl,
  272. .dev_select = qd65xx_dev_select,
  273. .tf_load = ide_tf_load,
  274. .tf_read = ide_tf_read,
  275. .input_data = ide_input_data,
  276. .output_data = ide_output_data,
  277. };
  278. static const struct ide_port_ops qd6500_port_ops = {
  279. .init_dev = qd6500_init_dev,
  280. .set_pio_mode = qd6500_set_pio_mode,
  281. };
  282. static const struct ide_port_ops qd6580_port_ops = {
  283. .init_dev = qd6580_init_dev,
  284. .set_pio_mode = qd6580_set_pio_mode,
  285. };
  286. static const struct ide_port_info qd65xx_port_info __initconst = {
  287. .name = DRV_NAME,
  288. .tp_ops = &qd65xx_tp_ops,
  289. .chipset = ide_qd65xx,
  290. .host_flags = IDE_HFLAG_IO_32BIT |
  291. IDE_HFLAG_NO_DMA,
  292. .pio_mask = ATA_PIO4,
  293. };
  294. /*
  295. * qd_probe:
  296. *
  297. * looks at the specified baseport, and if qd found, registers & initialises it
  298. * return 1 if another qd may be probed
  299. */
  300. static int __init qd_probe(int base)
  301. {
  302. int rc;
  303. u8 config, unit, control;
  304. struct ide_port_info d = qd65xx_port_info;
  305. config = inb(QD_CONFIG_PORT);
  306. if (! ((config & QD_CONFIG_BASEPORT) >> 1 == (base == 0xb0)) )
  307. return -ENODEV;
  308. unit = ! (config & QD_CONFIG_IDE_BASEPORT);
  309. if (unit)
  310. d.host_flags |= IDE_HFLAG_QD_2ND_PORT;
  311. switch (config & 0xf0) {
  312. case QD_CONFIG_QD6500:
  313. if (qd_testreg(base))
  314. return -ENODEV; /* bad register */
  315. if (config & QD_CONFIG_DISABLED) {
  316. printk(KERN_WARNING "qd6500 is disabled !\n");
  317. return -ENODEV;
  318. }
  319. printk(KERN_NOTICE "qd6500 at %#x\n", base);
  320. printk(KERN_DEBUG "qd6500: config=%#x, ID3=%u\n",
  321. config, QD_ID3);
  322. d.port_ops = &qd6500_port_ops;
  323. d.host_flags |= IDE_HFLAG_SINGLE;
  324. break;
  325. case QD_CONFIG_QD6580_A:
  326. case QD_CONFIG_QD6580_B:
  327. if (qd_testreg(base) || qd_testreg(base + 0x02))
  328. return -ENODEV; /* bad registers */
  329. control = inb(QD_CONTROL_PORT);
  330. printk(KERN_NOTICE "qd6580 at %#x\n", base);
  331. printk(KERN_DEBUG "qd6580: config=%#x, control=%#x, ID3=%u\n",
  332. config, control, QD_ID3);
  333. outb(QD_DEF_CONTR, QD_CONTROL_PORT);
  334. d.port_ops = &qd6580_port_ops;
  335. if (control & QD_CONTR_SEC_DISABLED)
  336. d.host_flags |= IDE_HFLAG_SINGLE;
  337. printk(KERN_INFO "qd6580: %s IDE board\n",
  338. (control & QD_CONTR_SEC_DISABLED) ? "single" : "dual");
  339. break;
  340. default:
  341. return -ENODEV;
  342. }
  343. rc = ide_legacy_device_add(&d, (base << 8) | config);
  344. if (d.host_flags & IDE_HFLAG_SINGLE)
  345. return (rc == 0) ? 1 : rc;
  346. return rc;
  347. }
  348. static bool probe_qd65xx;
  349. module_param_named(probe, probe_qd65xx, bool, 0);
  350. MODULE_PARM_DESC(probe, "probe for QD65xx chipsets");
  351. static int __init qd65xx_init(void)
  352. {
  353. int rc1, rc2 = -ENODEV;
  354. if (probe_qd65xx == 0)
  355. return -ENODEV;
  356. rc1 = qd_probe(0x30);
  357. if (rc1)
  358. rc2 = qd_probe(0xb0);
  359. if (rc1 < 0 && rc2 < 0)
  360. return -ENODEV;
  361. return 0;
  362. }
  363. module_init(qd65xx_init);
  364. MODULE_AUTHOR("Samuel Thibault");
  365. MODULE_DESCRIPTION("support of qd65xx vlb ide chipset");
  366. MODULE_LICENSE("GPL");