qd65xx.c 13 KB

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