qd65xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 1996-2001 Linus Torvalds & author (see below)
  3. */
  4. /*
  5. * Version 0.03 Cleaned auto-tune, added probe
  6. * Version 0.04 Added second channel tuning
  7. * Version 0.05 Enhanced tuning ; added qd6500 support
  8. * Version 0.06 Added dos driver's list
  9. * Version 0.07 Second channel bug fix
  10. *
  11. * QDI QD6500/QD6580 EIDE controller fast support
  12. *
  13. * Please set local bus speed using kernel parameter idebus
  14. * for example, "idebus=33" stands for 33Mhz VLbus
  15. * To activate controller support, use "ide0=qd65xx"
  16. * To enable tuning, use "hda=autotune hdb=autotune"
  17. * To enable 2nd channel tuning (qd6580 only), use "hdc=autotune hdd=autotune"
  18. */
  19. /*
  20. * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
  21. * Samuel Thibault <samuel.thibault@fnac.net>
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/timer.h>
  28. #include <linux/mm.h>
  29. #include <linux/ioport.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/hdreg.h>
  32. #include <linux/ide.h>
  33. #include <linux/init.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include "qd65xx.h"
  37. /*
  38. * I/O ports are 0x30-0x31 (and 0x32-0x33 for qd6580)
  39. * or 0xb0-0xb1 (and 0xb2-0xb3 for qd6580)
  40. * -- qd6500 is a single IDE interface
  41. * -- qd6580 is a dual IDE interface
  42. *
  43. * More research on qd6580 being done by willmore@cig.mot.com (David)
  44. * More Information given by Petr Soucek (petr@ryston.cz)
  45. * http://www.ryston.cz/petr/vlb
  46. */
  47. /*
  48. * base: Timer1
  49. *
  50. *
  51. * base+0x01: Config (R/O)
  52. *
  53. * bit 0: ide baseport: 1 = 0x1f0 ; 0 = 0x170 (only useful for qd6500)
  54. * bit 1: qd65xx baseport: 1 = 0xb0 ; 0 = 0x30
  55. * bit 2: ID3: bus speed: 1 = <=33MHz ; 0 = >33MHz
  56. * bit 3: qd6500: 1 = disabled, 0 = enabled
  57. * qd6580: 1
  58. * upper nibble:
  59. * qd6500: 1100
  60. * qd6580: either 1010 or 0101
  61. *
  62. *
  63. * base+0x02: Timer2 (qd6580 only)
  64. *
  65. *
  66. * base+0x03: Control (qd6580 only)
  67. *
  68. * bits 0-3 must always be set 1
  69. * bit 4 must be set 1, but is set 0 by dos driver while measuring vlb clock
  70. * bit 0 : 1 = Only primary port enabled : channel 0 for hda, channel 1 for hdb
  71. * 0 = Primary and Secondary ports enabled : channel 0 for hda & hdb
  72. * channel 1 for hdc & hdd
  73. * bit 1 : 1 = only disks on primary port
  74. * 0 = disks & ATAPI devices on primary port
  75. * bit 2-4 : always 0
  76. * bit 5 : status, but of what ?
  77. * bit 6 : always set 1 by dos driver
  78. * bit 7 : set 1 for non-ATAPI devices on primary port
  79. * (maybe read-ahead and post-write buffer ?)
  80. */
  81. static int timings[4]={-1,-1,-1,-1}; /* stores current timing for each timer */
  82. /*
  83. * qd_select:
  84. *
  85. * This routine is invoked from ide.c to prepare for access to a given drive.
  86. */
  87. static void qd_select (ide_drive_t *drive)
  88. {
  89. u8 index = (( (QD_TIMREG(drive)) & 0x80 ) >> 7) |
  90. (QD_TIMREG(drive) & 0x02);
  91. if (timings[index] != QD_TIMING(drive))
  92. outb(timings[index] = QD_TIMING(drive), QD_TIMREG(drive));
  93. }
  94. /*
  95. * qd6500_compute_timing
  96. *
  97. * computes the timing value where
  98. * lower nibble represents active time, in count of VLB clocks
  99. * upper nibble represents recovery time, in count of VLB clocks
  100. */
  101. static u8 qd6500_compute_timing (ide_hwif_t *hwif, int active_time, int recovery_time)
  102. {
  103. u8 active_cycle,recovery_cycle;
  104. if (system_bus_clock()<=33) {
  105. active_cycle = 9 - IDE_IN(active_time * system_bus_clock() / 1000 + 1, 2, 9);
  106. recovery_cycle = 15 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 0, 15);
  107. } else {
  108. active_cycle = 8 - IDE_IN(active_time * system_bus_clock() / 1000 + 1, 1, 8);
  109. recovery_cycle = 18 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 3, 18);
  110. }
  111. return((recovery_cycle<<4) | 0x08 | active_cycle);
  112. }
  113. /*
  114. * qd6580_compute_timing
  115. *
  116. * idem for qd6580
  117. */
  118. static u8 qd6580_compute_timing (int active_time, int recovery_time)
  119. {
  120. u8 active_cycle = 17 - IDE_IN(active_time * system_bus_clock() / 1000 + 1, 2, 17);
  121. u8 recovery_cycle = 15 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 2, 15);
  122. return((recovery_cycle<<4) | active_cycle);
  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 model[40];
  134. if (!*drive->id->model) return 0;
  135. strncpy(model,drive->id->model,40);
  136. ide_fixstring(model,40,1); /* byte-swap */
  137. for (p = qd65xx_timing ; p->offset != -1 ; p++) {
  138. if (!strncmp(p->model, model+p->offset, 4)) {
  139. printk(KERN_DEBUG "%s: listed !\n", drive->name);
  140. *active_time = p->active;
  141. *recovery_time = p->recovery;
  142. return 1;
  143. }
  144. }
  145. return 0;
  146. }
  147. /*
  148. * qd_timing_ok:
  149. *
  150. * check whether timings don't conflict
  151. */
  152. static int qd_timing_ok (ide_drive_t drives[])
  153. {
  154. return (IDE_IMPLY(drives[0].present && drives[1].present,
  155. IDE_IMPLY(QD_TIMREG(drives) == QD_TIMREG(drives+1),
  156. QD_TIMING(drives) == QD_TIMING(drives+1))));
  157. /* if same timing register, must be same timing */
  158. }
  159. /*
  160. * qd_set_timing:
  161. *
  162. * records the timing, and enables selectproc as needed
  163. */
  164. static void qd_set_timing (ide_drive_t *drive, u8 timing)
  165. {
  166. ide_hwif_t *hwif = HWIF(drive);
  167. drive->drive_data &= 0xff00;
  168. drive->drive_data |= timing;
  169. if (qd_timing_ok(hwif->drives)) {
  170. qd_select(drive); /* selects once */
  171. hwif->selectproc = NULL;
  172. } else
  173. hwif->selectproc = &qd_select;
  174. printk(KERN_DEBUG "%s: %#x\n", drive->name, timing);
  175. }
  176. static void qd6500_set_pio_mode(ide_drive_t *drive, const u8 pio)
  177. {
  178. int active_time = 175;
  179. int recovery_time = 415; /* worst case values from the dos driver */
  180. /*
  181. * FIXME: use "pio" value
  182. */
  183. if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)
  184. && drive->id->tPIO && (drive->id->field_valid & 0x02)
  185. && drive->id->eide_pio >= 240) {
  186. printk(KERN_INFO "%s: PIO mode%d\n", drive->name,
  187. drive->id->tPIO);
  188. active_time = 110;
  189. recovery_time = drive->id->eide_pio - 120;
  190. }
  191. qd_set_timing(drive, qd6500_compute_timing(HWIF(drive), active_time, recovery_time));
  192. }
  193. static void qd6580_set_pio_mode(ide_drive_t *drive, const u8 pio)
  194. {
  195. int base = HWIF(drive)->select_data;
  196. unsigned int cycle_time;
  197. int active_time = 175;
  198. int recovery_time = 415; /* worst case values from the dos driver */
  199. if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)) {
  200. cycle_time = ide_pio_cycle_time(drive, pio);
  201. switch (pio) {
  202. case 0: break;
  203. case 3:
  204. if (cycle_time >= 110) {
  205. active_time = 86;
  206. recovery_time = cycle_time - 102;
  207. } else
  208. printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
  209. break;
  210. case 4:
  211. if (cycle_time >= 69) {
  212. active_time = 70;
  213. recovery_time = cycle_time - 61;
  214. } else
  215. printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
  216. break;
  217. default:
  218. if (cycle_time >= 180) {
  219. active_time = 110;
  220. recovery_time = cycle_time - 120;
  221. } else {
  222. active_time = ide_pio_timings[pio].active_time;
  223. recovery_time = cycle_time - active_time;
  224. }
  225. }
  226. printk(KERN_INFO "%s: PIO mode%d\n", drive->name,pio);
  227. }
  228. if (!HWIF(drive)->channel && drive->media != ide_disk) {
  229. outb(0x5f, QD_CONTROL_PORT);
  230. printk(KERN_WARNING "%s: ATAPI: disabled read-ahead FIFO "
  231. "and post-write buffer on %s.\n",
  232. drive->name, HWIF(drive)->name);
  233. }
  234. qd_set_timing(drive, qd6580_compute_timing(active_time, recovery_time));
  235. }
  236. /*
  237. * qd_testreg
  238. *
  239. * tests if the given port is a register
  240. */
  241. static int __init qd_testreg(int port)
  242. {
  243. unsigned long flags;
  244. u8 savereg, readreg;
  245. local_irq_save(flags);
  246. savereg = inb_p(port);
  247. outb_p(QD_TESTVAL, port); /* safe value */
  248. readreg = inb_p(port);
  249. outb(savereg, port);
  250. local_irq_restore(flags);
  251. if (savereg == QD_TESTVAL) {
  252. printk(KERN_ERR "Outch ! the probe for qd65xx isn't reliable !\n");
  253. printk(KERN_ERR "Please contact maintainers to tell about your hardware\n");
  254. printk(KERN_ERR "Assuming qd65xx is not present.\n");
  255. return 1;
  256. }
  257. return (readreg != QD_TESTVAL);
  258. }
  259. /*
  260. * qd_setup:
  261. *
  262. * called to setup an ata channel : adjusts attributes & links for tuning
  263. */
  264. static void __init qd_setup(ide_hwif_t *hwif, int base, int config)
  265. {
  266. hwif->select_data = base;
  267. hwif->config_data = config;
  268. }
  269. static void __init qd6500_port_init_devs(ide_hwif_t *hwif)
  270. {
  271. u8 base = hwif->select_data, config = QD_CONFIG(hwif);
  272. hwif->drives[0].drive_data = QD6500_DEF_DATA;
  273. hwif->drives[1].drive_data = QD6500_DEF_DATA;
  274. }
  275. static void __init qd6580_port_init_devs(ide_hwif_t *hwif)
  276. {
  277. u16 t1, t2;
  278. u8 base = hwif->select_data, config = QD_CONFIG(hwif);
  279. if (QD_CONTROL(hwif) & QD_CONTR_SEC_DISABLED) {
  280. t1 = QD6580_DEF_DATA;
  281. t2 = QD6580_DEF_DATA2;
  282. } else
  283. t2 = t1 = hwif->channel ? QD6580_DEF_DATA2 : QD6580_DEF_DATA;
  284. hwif->drives[0].drive_data = t1;
  285. hwif->drives[1].drive_data = t2;
  286. }
  287. static const struct ide_port_info qd65xx_port_info __initdata = {
  288. .chipset = ide_qd65xx,
  289. .host_flags = IDE_HFLAG_IO_32BIT |
  290. IDE_HFLAG_NO_DMA |
  291. IDE_HFLAG_NO_AUTOTUNE,
  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. ide_hwif_t *hwif;
  303. u8 config, unit;
  304. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  305. hw_regs_t hw[2];
  306. struct ide_port_info d = qd65xx_port_info;
  307. config = inb(QD_CONFIG_PORT);
  308. if (! ((config & QD_CONFIG_BASEPORT) >> 1 == (base == 0xb0)) )
  309. return -ENODEV;
  310. unit = ! (config & QD_CONFIG_IDE_BASEPORT);
  311. if (unit)
  312. d.host_flags |= IDE_HFLAG_QD_2ND_PORT;
  313. memset(&hw, 0, sizeof(hw));
  314. ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
  315. hw[0].irq = 14;
  316. ide_std_init_ports(&hw[1], 0x170, 0x376);
  317. hw[1].irq = 15;
  318. if ((config & 0xf0) == QD_CONFIG_QD6500) {
  319. if (qd_testreg(base))
  320. return -ENODEV; /* bad register */
  321. /* qd6500 found */
  322. if (config & QD_CONFIG_DISABLED) {
  323. printk(KERN_WARNING "qd6500 is disabled !\n");
  324. return -ENODEV;
  325. }
  326. printk(KERN_NOTICE "qd6500 at %#x\n", base);
  327. printk(KERN_DEBUG "qd6500: config=%#x, ID3=%u\n",
  328. config, QD_ID3);
  329. hwif = ide_find_port_slot(&d);
  330. if (hwif == NULL)
  331. return -ENOENT;
  332. ide_init_port_hw(hwif, &hw[unit]);
  333. qd_setup(hwif, base, config);
  334. hwif->port_init_devs = qd6500_port_init_devs;
  335. hwif->set_pio_mode = &qd6500_set_pio_mode;
  336. idx[unit] = hwif->index;
  337. ide_device_add(idx, &d);
  338. return 1;
  339. }
  340. if (((config & 0xf0) == QD_CONFIG_QD6580_A) ||
  341. ((config & 0xf0) == QD_CONFIG_QD6580_B)) {
  342. u8 control;
  343. if (qd_testreg(base) || qd_testreg(base + 0x02))
  344. return -ENODEV; /* bad registers */
  345. /* qd6580 found */
  346. control = inb(QD_CONTROL_PORT);
  347. printk(KERN_NOTICE "qd6580 at %#x\n", base);
  348. printk(KERN_DEBUG "qd6580: config=%#x, control=%#x, ID3=%u\n",
  349. config, control, QD_ID3);
  350. outb(QD_DEF_CONTR, QD_CONTROL_PORT);
  351. if (control & QD_CONTR_SEC_DISABLED) {
  352. /* secondary disabled */
  353. printk(KERN_INFO "qd6580: single IDE board\n");
  354. hwif = ide_find_port_slot(&d);
  355. if (hwif == NULL)
  356. return -ENOENT;
  357. ide_init_port_hw(hwif, &hw[unit]);
  358. qd_setup(hwif, base, config | (control << 8));
  359. hwif->port_init_devs = qd6580_port_init_devs;
  360. hwif->set_pio_mode = &qd6580_set_pio_mode;
  361. idx[unit] = hwif->index;
  362. ide_device_add(idx, &d);
  363. return 1;
  364. } else {
  365. ide_hwif_t *mate;
  366. /* secondary enabled */
  367. printk(KERN_INFO "qd6580: dual IDE board\n");
  368. hwif = ide_find_port();
  369. if (hwif) {
  370. ide_init_port_hw(hwif, &hw[0]);
  371. qd_setup(hwif, base, config | (control << 8));
  372. hwif->port_init_devs = qd6580_port_init_devs;
  373. hwif->set_pio_mode = qd6580_set_pio_mode;
  374. idx[0] = hwif->index;
  375. }
  376. mate = ide_find_port();
  377. if (mate) {
  378. ide_init_port_hw(mate, &hw[1]);
  379. qd_setup(mate, base, config | (control << 8));
  380. mate->port_init_devs = qd6580_port_init_devs;
  381. mate->set_pio_mode = qd6580_set_pio_mode;
  382. idx[1] = mate->index;
  383. }
  384. ide_device_add(idx, &qd65xx_port_info);
  385. return 0; /* no other qd65xx possible */
  386. }
  387. }
  388. /* no qd65xx found */
  389. return -ENODEV;
  390. }
  391. int probe_qd65xx = 0;
  392. module_param_named(probe, probe_qd65xx, bool, 0);
  393. MODULE_PARM_DESC(probe, "probe for QD65xx chipsets");
  394. static int __init qd65xx_init(void)
  395. {
  396. int rc1, rc2 = -ENODEV;
  397. if (probe_qd65xx == 0)
  398. return -ENODEV;
  399. rc1 = qd_probe(0x30);
  400. if (rc1)
  401. rc2 = qd_probe(0xb0);
  402. if (rc1 < 0 && rc2 < 0)
  403. return -ENODEV;
  404. return 0;
  405. }
  406. module_init(qd65xx_init);
  407. MODULE_AUTHOR("Samuel Thibault");
  408. MODULE_DESCRIPTION("support of qd65xx vlb ide chipset");
  409. MODULE_LICENSE("GPL");