cmd640.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * Copyright (C) 1995-1996 Linus Torvalds & authors (see below)
  3. */
  4. /*
  5. * Original authors: abramov@cecmow.enet.dec.com (Igor Abramov)
  6. * mlord@pobox.com (Mark Lord)
  7. *
  8. * See linux/MAINTAINERS for address of current maintainer.
  9. *
  10. * This file provides support for the advanced features and bugs
  11. * of IDE interfaces using the CMD Technologies 0640 IDE interface chip.
  12. *
  13. * These chips are basically fucked by design, and getting this driver
  14. * to work on every motherboard design that uses this screwed chip seems
  15. * bloody well impossible. However, we're still trying.
  16. *
  17. * Version 0.97 worked for everybody.
  18. *
  19. * User feedback is essential. Many thanks to the beta test team:
  20. *
  21. * A.Hartgers@stud.tue.nl, JZDQC@CUNYVM.CUNY.edu, abramov@cecmow.enet.dec.com,
  22. * bardj@utopia.ppp.sn.no, bart@gaga.tue.nl, bbol001@cs.auckland.ac.nz,
  23. * chrisc@dbass.demon.co.uk, dalecki@namu26.Num.Math.Uni-Goettingen.de,
  24. * derekn@vw.ece.cmu.edu, florian@btp2x3.phy.uni-bayreuth.de,
  25. * flynn@dei.unipd.it, gadio@netvision.net.il, godzilla@futuris.net,
  26. * j@pobox.com, jkemp1@mises.uni-paderborn.de, jtoppe@hiwaay.net,
  27. * kerouac@ssnet.com, meskes@informatik.rwth-aachen.de, hzoli@cs.elte.hu,
  28. * peter@udgaard.isgtec.com, phil@tazenda.demon.co.uk, roadcapw@cfw.com,
  29. * s0033las@sun10.vsz.bme.hu, schaffer@tam.cornell.edu, sjd@slip.net,
  30. * steve@ei.org, ulrpeg@bigcomm.gun.de, ism@tardis.ed.ac.uk, mack@cray.com
  31. * liug@mama.indstate.edu, and others.
  32. *
  33. * Version 0.01 Initial version, hacked out of ide.c,
  34. * and #include'd rather than compiled separately.
  35. * This will get cleaned up in a subsequent release.
  36. *
  37. * Version 0.02 Fixes for vlb initialization code, enable prefetch
  38. * for versions 'B' and 'C' of chip by default,
  39. * some code cleanup.
  40. *
  41. * Version 0.03 Added reset of secondary interface,
  42. * and black list for devices which are not compatible
  43. * with prefetch mode. Separate function for setting
  44. * prefetch is added, possibly it will be called some
  45. * day from ioctl processing code.
  46. *
  47. * Version 0.04 Now configs/compiles separate from ide.c
  48. *
  49. * Version 0.05 Major rewrite of interface timing code.
  50. * Added new function cmd640_set_mode to set PIO mode
  51. * from ioctl call. New drives added to black list.
  52. *
  53. * Version 0.06 More code cleanup. Prefetch is enabled only for
  54. * detected hard drives, not included in prefetch
  55. * black list.
  56. *
  57. * Version 0.07 Changed to more conservative drive tuning policy.
  58. * Unknown drives, which report PIO < 4 are set to
  59. * (reported_PIO - 1) if it is supported, or to PIO0.
  60. * List of known drives extended by info provided by
  61. * CMD at their ftp site.
  62. *
  63. * Version 0.08 Added autotune/noautotune support.
  64. *
  65. * Version 0.09 Try to be smarter about 2nd port enabling.
  66. * Version 0.10 Be nice and don't reset 2nd port.
  67. * Version 0.11 Try to handle more weird situations.
  68. *
  69. * Version 0.12 Lots of bug fixes from Laszlo Peter
  70. * irq unmasking disabled for reliability.
  71. * try to be even smarter about the second port.
  72. * tidy up source code formatting.
  73. * Version 0.13 permit irq unmasking again.
  74. * Version 0.90 massive code cleanup, some bugs fixed.
  75. * defaults all drives to PIO mode0, prefetch off.
  76. * autotune is OFF by default, with compile time flag.
  77. * prefetch can be turned OFF/ON using "hdparm -p8/-p9"
  78. * (requires hdparm-3.1 or newer)
  79. * Version 0.91 first release to linux-kernel list.
  80. * Version 0.92 move initial reg dump to separate callable function
  81. * change "readahead" to "prefetch" to avoid confusion
  82. * Version 0.95 respect original BIOS timings unless autotuning.
  83. * tons of code cleanup and rearrangement.
  84. * added CONFIG_BLK_DEV_CMD640_ENHANCED option
  85. * prevent use of unmask when prefetch is on
  86. * Version 0.96 prevent use of io_32bit when prefetch is off
  87. * Version 0.97 fix VLB secondary interface for sjd@slip.net
  88. * other minor tune-ups: 0.96 was very good.
  89. * Version 0.98 ignore PCI version when disabled by BIOS
  90. * Version 0.99 display setup/active/recovery clocks with PIO mode
  91. * Version 1.00 Mmm.. cannot depend on PCMD_ENA in all systems
  92. * Version 1.01 slow/fast devsel can be selected with "hdparm -p6/-p7"
  93. * ("fast" is necessary for 32bit I/O in some systems)
  94. * Version 1.02 fix bug that resulted in slow "setup times"
  95. * (patch courtesy of Zoltan Hidvegi)
  96. */
  97. #define CMD640_PREFETCH_MASKS 1
  98. /*#define CMD640_DUMP_REGS */
  99. #include <linux/types.h>
  100. #include <linux/kernel.h>
  101. #include <linux/delay.h>
  102. #include <linux/hdreg.h>
  103. #include <linux/ide.h>
  104. #include <linux/init.h>
  105. #include <asm/io.h>
  106. /*
  107. * This flag is set in ide.c by the parameter: ide0=cmd640_vlb
  108. */
  109. int cmd640_vlb;
  110. /*
  111. * CMD640 specific registers definition.
  112. */
  113. #define VID 0x00
  114. #define DID 0x02
  115. #define PCMD 0x04
  116. #define PCMD_ENA 0x01
  117. #define PSTTS 0x06
  118. #define REVID 0x08
  119. #define PROGIF 0x09
  120. #define SUBCL 0x0a
  121. #define BASCL 0x0b
  122. #define BaseA0 0x10
  123. #define BaseA1 0x14
  124. #define BaseA2 0x18
  125. #define BaseA3 0x1c
  126. #define INTLINE 0x3c
  127. #define INPINE 0x3d
  128. #define CFR 0x50
  129. #define CFR_DEVREV 0x03
  130. #define CFR_IDE01INTR 0x04
  131. #define CFR_DEVID 0x18
  132. #define CFR_AT_VESA_078h 0x20
  133. #define CFR_DSA1 0x40
  134. #define CFR_DSA0 0x80
  135. #define CNTRL 0x51
  136. #define CNTRL_DIS_RA0 0x40
  137. #define CNTRL_DIS_RA1 0x80
  138. #define CNTRL_ENA_2ND 0x08
  139. #define CMDTIM 0x52
  140. #define ARTTIM0 0x53
  141. #define DRWTIM0 0x54
  142. #define ARTTIM1 0x55
  143. #define DRWTIM1 0x56
  144. #define ARTTIM23 0x57
  145. #define ARTTIM23_DIS_RA2 0x04
  146. #define ARTTIM23_DIS_RA3 0x08
  147. #define DRWTIM23 0x58
  148. #define BRST 0x59
  149. /*
  150. * Registers and masks for easy access by drive index:
  151. */
  152. static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
  153. static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
  154. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  155. static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
  156. static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
  157. /*
  158. * Current cmd640 timing values for each drive.
  159. * The defaults for each are the slowest possible timings.
  160. */
  161. static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
  162. static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
  163. static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
  164. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  165. static DEFINE_SPINLOCK(cmd640_lock);
  166. /*
  167. * These are initialized to point at the devices we control
  168. */
  169. static ide_hwif_t *cmd_hwif0, *cmd_hwif1;
  170. /*
  171. * Interface to access cmd640x registers
  172. */
  173. static unsigned int cmd640_key;
  174. static void (*__put_cmd640_reg)(u16 reg, u8 val);
  175. static u8 (*__get_cmd640_reg)(u16 reg);
  176. /*
  177. * This is read from the CFR reg, and is used in several places.
  178. */
  179. static unsigned int cmd640_chip_version;
  180. /*
  181. * The CMD640x chip does not support DWORD config write cycles, but some
  182. * of the BIOSes use them to implement the config services.
  183. * Therefore, we must use direct IO instead.
  184. */
  185. /* PCI method 1 access */
  186. static void put_cmd640_reg_pci1(u16 reg, u8 val)
  187. {
  188. outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
  189. outb_p(val, (reg & 3) | 0xcfc);
  190. }
  191. static u8 get_cmd640_reg_pci1(u16 reg)
  192. {
  193. outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
  194. return inb_p((reg & 3) | 0xcfc);
  195. }
  196. /* PCI method 2 access (from CMD datasheet) */
  197. static void put_cmd640_reg_pci2(u16 reg, u8 val)
  198. {
  199. outb_p(0x10, 0xcf8);
  200. outb_p(val, cmd640_key + reg);
  201. outb_p(0, 0xcf8);
  202. }
  203. static u8 get_cmd640_reg_pci2(u16 reg)
  204. {
  205. u8 b;
  206. outb_p(0x10, 0xcf8);
  207. b = inb_p(cmd640_key + reg);
  208. outb_p(0, 0xcf8);
  209. return b;
  210. }
  211. /* VLB access */
  212. static void put_cmd640_reg_vlb(u16 reg, u8 val)
  213. {
  214. outb_p(reg, cmd640_key);
  215. outb_p(val, cmd640_key + 4);
  216. }
  217. static u8 get_cmd640_reg_vlb(u16 reg)
  218. {
  219. outb_p(reg, cmd640_key);
  220. return inb_p(cmd640_key + 4);
  221. }
  222. static u8 get_cmd640_reg(u16 reg)
  223. {
  224. unsigned long flags;
  225. u8 b;
  226. spin_lock_irqsave(&cmd640_lock, flags);
  227. b = __get_cmd640_reg(reg);
  228. spin_unlock_irqrestore(&cmd640_lock, flags);
  229. return b;
  230. }
  231. static void put_cmd640_reg(u16 reg, u8 val)
  232. {
  233. unsigned long flags;
  234. spin_lock_irqsave(&cmd640_lock, flags);
  235. __put_cmd640_reg(reg, val);
  236. spin_unlock_irqrestore(&cmd640_lock, flags);
  237. }
  238. static int __init match_pci_cmd640_device(void)
  239. {
  240. const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
  241. unsigned int i;
  242. for (i = 0; i < 4; i++) {
  243. if (get_cmd640_reg(i) != ven_dev[i])
  244. return 0;
  245. }
  246. #ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
  247. if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
  248. printk("ide: cmd640 on PCI disabled by BIOS\n");
  249. return 0;
  250. }
  251. #endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
  252. return 1; /* success */
  253. }
  254. /*
  255. * Probe for CMD640x -- pci method 1
  256. */
  257. static int __init probe_for_cmd640_pci1(void)
  258. {
  259. __get_cmd640_reg = get_cmd640_reg_pci1;
  260. __put_cmd640_reg = put_cmd640_reg_pci1;
  261. for (cmd640_key = 0x80000000;
  262. cmd640_key <= 0x8000f800;
  263. cmd640_key += 0x800) {
  264. if (match_pci_cmd640_device())
  265. return 1; /* success */
  266. }
  267. return 0;
  268. }
  269. /*
  270. * Probe for CMD640x -- pci method 2
  271. */
  272. static int __init probe_for_cmd640_pci2(void)
  273. {
  274. __get_cmd640_reg = get_cmd640_reg_pci2;
  275. __put_cmd640_reg = put_cmd640_reg_pci2;
  276. for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
  277. if (match_pci_cmd640_device())
  278. return 1; /* success */
  279. }
  280. return 0;
  281. }
  282. /*
  283. * Probe for CMD640x -- vlb
  284. */
  285. static int __init probe_for_cmd640_vlb(void)
  286. {
  287. u8 b;
  288. __get_cmd640_reg = get_cmd640_reg_vlb;
  289. __put_cmd640_reg = put_cmd640_reg_vlb;
  290. cmd640_key = 0x178;
  291. b = get_cmd640_reg(CFR);
  292. if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
  293. cmd640_key = 0x78;
  294. b = get_cmd640_reg(CFR);
  295. if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
  296. return 0;
  297. }
  298. return 1; /* success */
  299. }
  300. /*
  301. * Returns 1 if an IDE interface/drive exists at 0x170,
  302. * Returns 0 otherwise.
  303. */
  304. static int __init secondary_port_responding(void)
  305. {
  306. unsigned long flags;
  307. spin_lock_irqsave(&cmd640_lock, flags);
  308. outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */
  309. udelay(100);
  310. if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
  311. outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
  312. udelay(100);
  313. if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
  314. spin_unlock_irqrestore(&cmd640_lock, flags);
  315. return 0; /* nothing responded */
  316. }
  317. }
  318. spin_unlock_irqrestore(&cmd640_lock, flags);
  319. return 1; /* success */
  320. }
  321. #ifdef CMD640_DUMP_REGS
  322. /*
  323. * Dump out all cmd640 registers. May be called from ide.c
  324. */
  325. static void cmd640_dump_regs(void)
  326. {
  327. unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
  328. /* Dump current state of chip registers */
  329. printk("ide: cmd640 internal register dump:");
  330. for (; reg <= 0x59; reg++) {
  331. if (!(reg & 0x0f))
  332. printk("\n%04x:", reg);
  333. printk(" %02x", get_cmd640_reg(reg));
  334. }
  335. printk("\n");
  336. }
  337. #endif
  338. /*
  339. * Check whether prefetch is on for a drive,
  340. * and initialize the unmask flags for safe operation.
  341. */
  342. static void __init check_prefetch(ide_drive_t *drive, unsigned int index)
  343. {
  344. u8 b = get_cmd640_reg(prefetch_regs[index]);
  345. if (b & prefetch_masks[index]) { /* is prefetch off? */
  346. drive->no_unmask = 0;
  347. drive->no_io_32bit = 1;
  348. drive->io_32bit = 0;
  349. } else {
  350. #if CMD640_PREFETCH_MASKS
  351. drive->no_unmask = 1;
  352. drive->unmask = 0;
  353. #endif
  354. drive->no_io_32bit = 0;
  355. }
  356. }
  357. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  358. /*
  359. * Sets prefetch mode for a drive.
  360. */
  361. static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode)
  362. {
  363. unsigned long flags;
  364. int reg = prefetch_regs[index];
  365. u8 b;
  366. spin_lock_irqsave(&cmd640_lock, flags);
  367. b = __get_cmd640_reg(reg);
  368. if (mode) { /* want prefetch on? */
  369. #if CMD640_PREFETCH_MASKS
  370. drive->no_unmask = 1;
  371. drive->unmask = 0;
  372. #endif
  373. drive->no_io_32bit = 0;
  374. b &= ~prefetch_masks[index]; /* enable prefetch */
  375. } else {
  376. drive->no_unmask = 0;
  377. drive->no_io_32bit = 1;
  378. drive->io_32bit = 0;
  379. b |= prefetch_masks[index]; /* disable prefetch */
  380. }
  381. __put_cmd640_reg(reg, b);
  382. spin_unlock_irqrestore(&cmd640_lock, flags);
  383. }
  384. /*
  385. * Dump out current drive clocks settings
  386. */
  387. static void display_clocks(unsigned int index)
  388. {
  389. u8 active_count, recovery_count;
  390. active_count = active_counts[index];
  391. if (active_count == 1)
  392. ++active_count;
  393. recovery_count = recovery_counts[index];
  394. if (active_count > 3 && recovery_count == 1)
  395. ++recovery_count;
  396. if (cmd640_chip_version > 1)
  397. recovery_count += 1; /* cmd640b uses (count + 1)*/
  398. printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
  399. }
  400. /*
  401. * Pack active and recovery counts into single byte representation
  402. * used by controller
  403. */
  404. static inline u8 pack_nibbles(u8 upper, u8 lower)
  405. {
  406. return ((upper & 0x0f) << 4) | (lower & 0x0f);
  407. }
  408. /*
  409. * This routine retrieves the initial drive timings from the chipset.
  410. */
  411. static void __init retrieve_drive_counts(unsigned int index)
  412. {
  413. u8 b;
  414. /*
  415. * Get the internal setup timing, and convert to clock count
  416. */
  417. b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
  418. switch (b) {
  419. case 0x00: b = 4; break;
  420. case 0x80: b = 3; break;
  421. case 0x40: b = 2; break;
  422. default: b = 5; break;
  423. }
  424. setup_counts[index] = b;
  425. /*
  426. * Get the active/recovery counts
  427. */
  428. b = get_cmd640_reg(drwtim_regs[index]);
  429. active_counts[index] = (b >> 4) ? (b >> 4) : 0x10;
  430. recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
  431. }
  432. /*
  433. * This routine writes the prepared setup/active/recovery counts
  434. * for a drive into the cmd640 chipset registers to active them.
  435. */
  436. static void program_drive_counts(ide_drive_t *drive, unsigned int index)
  437. {
  438. unsigned long flags;
  439. u8 setup_count = setup_counts[index];
  440. u8 active_count = active_counts[index];
  441. u8 recovery_count = recovery_counts[index];
  442. /*
  443. * Set up address setup count and drive read/write timing registers.
  444. * Primary interface has individual count/timing registers for
  445. * each drive. Secondary interface has one common set of registers,
  446. * so we merge the timings, using the slowest value for each timing.
  447. */
  448. if (index > 1) {
  449. ide_hwif_t *hwif = drive->hwif;
  450. ide_drive_t *peer = &hwif->drives[!drive->select.b.unit];
  451. unsigned int mate = index ^ 1;
  452. if (peer->present) {
  453. if (setup_count < setup_counts[mate])
  454. setup_count = setup_counts[mate];
  455. if (active_count < active_counts[mate])
  456. active_count = active_counts[mate];
  457. if (recovery_count < recovery_counts[mate])
  458. recovery_count = recovery_counts[mate];
  459. }
  460. }
  461. /*
  462. * Convert setup_count to internal chipset representation
  463. */
  464. switch (setup_count) {
  465. case 4: setup_count = 0x00; break;
  466. case 3: setup_count = 0x80; break;
  467. case 1:
  468. case 2: setup_count = 0x40; break;
  469. default: setup_count = 0xc0; /* case 5 */
  470. }
  471. /*
  472. * Now that everything is ready, program the new timings
  473. */
  474. spin_lock_irqsave(&cmd640_lock, flags);
  475. /*
  476. * Program the address_setup clocks into ARTTIM reg,
  477. * and then the active/recovery counts into the DRWTIM reg
  478. * (this converts counts of 16 into counts of zero -- okay).
  479. */
  480. setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
  481. __put_cmd640_reg(arttim_regs[index], setup_count);
  482. __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
  483. spin_unlock_irqrestore(&cmd640_lock, flags);
  484. }
  485. /*
  486. * Set a specific pio_mode for a drive
  487. */
  488. static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
  489. u8 pio_mode, unsigned int cycle_time)
  490. {
  491. int setup_time, active_time, recovery_time, clock_time;
  492. u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
  493. int bus_speed = system_bus_clock();
  494. if (pio_mode > 5)
  495. pio_mode = 5;
  496. setup_time = ide_pio_timings[pio_mode].setup_time;
  497. active_time = ide_pio_timings[pio_mode].active_time;
  498. recovery_time = cycle_time - (setup_time + active_time);
  499. clock_time = 1000 / bus_speed;
  500. cycle_count = DIV_ROUND_UP(cycle_time, clock_time);
  501. setup_count = DIV_ROUND_UP(setup_time, clock_time);
  502. active_count = DIV_ROUND_UP(active_time, clock_time);
  503. if (active_count < 2)
  504. active_count = 2; /* minimum allowed by cmd640 */
  505. recovery_count = DIV_ROUND_UP(recovery_time, clock_time);
  506. recovery_count2 = cycle_count - (setup_count + active_count);
  507. if (recovery_count2 > recovery_count)
  508. recovery_count = recovery_count2;
  509. if (recovery_count < 2)
  510. recovery_count = 2; /* minimum allowed by cmd640 */
  511. if (recovery_count > 17) {
  512. active_count += recovery_count - 17;
  513. recovery_count = 17;
  514. }
  515. if (active_count > 16)
  516. active_count = 16; /* maximum allowed by cmd640 */
  517. if (cmd640_chip_version > 1)
  518. recovery_count -= 1; /* cmd640b uses (count + 1)*/
  519. if (recovery_count > 16)
  520. recovery_count = 16; /* maximum allowed by cmd640 */
  521. setup_counts[index] = setup_count;
  522. active_counts[index] = active_count;
  523. recovery_counts[index] = recovery_count;
  524. /*
  525. * In a perfect world, we might set the drive pio mode here
  526. * (using WIN_SETFEATURE) before continuing.
  527. *
  528. * But we do not, because:
  529. * 1) this is the wrong place to do it (proper is do_special() in ide.c)
  530. * 2) in practice this is rarely, if ever, necessary
  531. */
  532. program_drive_counts(drive, index);
  533. }
  534. static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
  535. {
  536. unsigned int index = 0, cycle_time;
  537. u8 b;
  538. switch (pio) {
  539. case 6: /* set fast-devsel off */
  540. case 7: /* set fast-devsel on */
  541. b = get_cmd640_reg(CNTRL) & ~0x27;
  542. if (pio & 1)
  543. b |= 0x27;
  544. put_cmd640_reg(CNTRL, b);
  545. printk("%s: %sabled cmd640 fast host timing (devsel)\n",
  546. drive->name, (pio & 1) ? "en" : "dis");
  547. return;
  548. case 8: /* set prefetch off */
  549. case 9: /* set prefetch on */
  550. set_prefetch_mode(drive, index, pio & 1);
  551. printk("%s: %sabled cmd640 prefetch\n",
  552. drive->name, (pio & 1) ? "en" : "dis");
  553. return;
  554. }
  555. cycle_time = ide_pio_cycle_time(drive, pio);
  556. cmd640_set_mode(drive, index, pio, cycle_time);
  557. printk("%s: selected cmd640 PIO mode%d (%dns)",
  558. drive->name, pio, cycle_time);
  559. display_clocks(index);
  560. }
  561. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  562. static int pci_conf1(void)
  563. {
  564. unsigned long flags;
  565. u32 tmp;
  566. spin_lock_irqsave(&cmd640_lock, flags);
  567. outb(0x01, 0xCFB);
  568. tmp = inl(0xCF8);
  569. outl(0x80000000, 0xCF8);
  570. if (inl(0xCF8) == 0x80000000) {
  571. outl(tmp, 0xCF8);
  572. spin_unlock_irqrestore(&cmd640_lock, flags);
  573. return 1;
  574. }
  575. outl(tmp, 0xCF8);
  576. spin_unlock_irqrestore(&cmd640_lock, flags);
  577. return 0;
  578. }
  579. static int pci_conf2(void)
  580. {
  581. unsigned long flags;
  582. spin_lock_irqsave(&cmd640_lock, flags);
  583. outb(0x00, 0xCFB);
  584. outb(0x00, 0xCF8);
  585. outb(0x00, 0xCFA);
  586. if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
  587. spin_unlock_irqrestore(&cmd640_lock, flags);
  588. return 1;
  589. }
  590. spin_unlock_irqrestore(&cmd640_lock, flags);
  591. return 0;
  592. }
  593. static const struct ide_port_info cmd640_port_info __initdata = {
  594. .chipset = ide_cmd640,
  595. .host_flags = IDE_HFLAG_SERIALIZE |
  596. IDE_HFLAG_NO_DMA |
  597. IDE_HFLAG_NO_AUTOTUNE |
  598. IDE_HFLAG_ABUSE_PREFETCH |
  599. IDE_HFLAG_ABUSE_FAST_DEVSEL,
  600. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  601. .pio_mask = ATA_PIO5,
  602. #endif
  603. };
  604. /*
  605. * Probe for a cmd640 chipset, and initialize it if found.
  606. */
  607. static int __init cmd640x_init(void)
  608. {
  609. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  610. int second_port_toggled = 0;
  611. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  612. int second_port_cmd640 = 0;
  613. const char *bus_type, *port2;
  614. unsigned int index;
  615. u8 b, cfr;
  616. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  617. hw_regs_t hw[2];
  618. if (cmd640_vlb && probe_for_cmd640_vlb()) {
  619. bus_type = "VLB";
  620. } else {
  621. cmd640_vlb = 0;
  622. /* Find out what kind of PCI probing is supported otherwise
  623. Justin Gibbs will sulk.. */
  624. if (pci_conf1() && probe_for_cmd640_pci1())
  625. bus_type = "PCI (type1)";
  626. else if (pci_conf2() && probe_for_cmd640_pci2())
  627. bus_type = "PCI (type2)";
  628. else
  629. return 0;
  630. }
  631. /*
  632. * Undocumented magic (there is no 0x5b reg in specs)
  633. */
  634. put_cmd640_reg(0x5b, 0xbd);
  635. if (get_cmd640_reg(0x5b) != 0xbd) {
  636. printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
  637. return 0;
  638. }
  639. put_cmd640_reg(0x5b, 0);
  640. #ifdef CMD640_DUMP_REGS
  641. cmd640_dump_regs();
  642. #endif
  643. /*
  644. * Documented magic begins here
  645. */
  646. cfr = get_cmd640_reg(CFR);
  647. cmd640_chip_version = cfr & CFR_DEVREV;
  648. if (cmd640_chip_version == 0) {
  649. printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
  650. return 0;
  651. }
  652. memset(&hw, 0, sizeof(hw));
  653. ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
  654. hw[0].irq = 14;
  655. ide_std_init_ports(&hw[1], 0x170, 0x376);
  656. hw[1].irq = 15;
  657. printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x"
  658. "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr);
  659. cmd_hwif0 = ide_find_port();
  660. /*
  661. * Initialize data for primary port
  662. */
  663. if (cmd_hwif0) {
  664. ide_init_port_hw(cmd_hwif0, &hw[0]);
  665. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  666. cmd_hwif0->set_pio_mode = &cmd640_set_pio_mode;
  667. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  668. idx[0] = cmd_hwif0->index;
  669. }
  670. /*
  671. * Ensure compatibility by always using the slowest timings
  672. * for access to the drive's command register block,
  673. * and reset the prefetch burstsize to default (512 bytes).
  674. *
  675. * Maybe we need a way to NOT do these on *some* systems?
  676. */
  677. put_cmd640_reg(CMDTIM, 0);
  678. put_cmd640_reg(BRST, 0x40);
  679. cmd_hwif1 = ide_find_port();
  680. /*
  681. * Try to enable the secondary interface, if not already enabled
  682. */
  683. if (cmd_hwif1 &&
  684. cmd_hwif1->drives[0].noprobe && cmd_hwif1->drives[1].noprobe) {
  685. port2 = "not probed";
  686. } else {
  687. b = get_cmd640_reg(CNTRL);
  688. if (secondary_port_responding()) {
  689. if ((b & CNTRL_ENA_2ND)) {
  690. second_port_cmd640 = 1;
  691. port2 = "okay";
  692. } else if (cmd640_vlb) {
  693. second_port_cmd640 = 1;
  694. port2 = "alive";
  695. } else
  696. port2 = "not cmd640";
  697. } else {
  698. put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
  699. if (secondary_port_responding()) {
  700. second_port_cmd640 = 1;
  701. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  702. second_port_toggled = 1;
  703. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  704. port2 = "enabled";
  705. } else {
  706. put_cmd640_reg(CNTRL, b); /* restore original setting */
  707. port2 = "not responding";
  708. }
  709. }
  710. }
  711. /*
  712. * Initialize data for secondary cmd640 port, if enabled
  713. */
  714. if (second_port_cmd640 && cmd_hwif1) {
  715. ide_init_port_hw(cmd_hwif1, &hw[1]);
  716. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  717. cmd_hwif1->set_pio_mode = &cmd640_set_pio_mode;
  718. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  719. idx[1] = cmd_hwif1->index;
  720. }
  721. printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
  722. second_port_cmd640 ? "" : "not ", port2);
  723. /*
  724. * Establish initial timings/prefetch for all drives.
  725. * Do not unnecessarily disturb any prior BIOS setup of these.
  726. */
  727. for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
  728. ide_drive_t *drive;
  729. if (index > 1) {
  730. if (cmd_hwif1 == NULL)
  731. continue;
  732. drive = &cmd_hwif1->drives[index & 1];
  733. } else {
  734. if (cmd_hwif0 == NULL)
  735. continue;
  736. drive = &cmd_hwif0->drives[index & 1];
  737. }
  738. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  739. if (drive->autotune || ((index > 1) && second_port_toggled)) {
  740. /*
  741. * Reset timing to the slowest speed and turn off
  742. * prefetch. This way, the drive identify code has
  743. * a better chance.
  744. */
  745. setup_counts [index] = 4; /* max possible */
  746. active_counts [index] = 16; /* max possible */
  747. recovery_counts [index] = 16; /* max possible */
  748. program_drive_counts(drive, index);
  749. set_prefetch_mode(drive, index, 0);
  750. printk("cmd640: drive%d timings/prefetch cleared\n", index);
  751. } else {
  752. /*
  753. * Record timings/prefetch without changing them.
  754. * This preserves any prior BIOS setup.
  755. */
  756. retrieve_drive_counts (index);
  757. check_prefetch(drive, index);
  758. printk("cmd640: drive%d timings/prefetch(%s) preserved",
  759. index, drive->no_io_32bit ? "off" : "on");
  760. display_clocks(index);
  761. }
  762. #else
  763. /*
  764. * Set the drive unmask flags to match the prefetch setting
  765. */
  766. check_prefetch(drive, index);
  767. printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
  768. index, drive->no_io_32bit ? "off" : "on");
  769. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  770. }
  771. #ifdef CMD640_DUMP_REGS
  772. cmd640_dump_regs();
  773. #endif
  774. ide_device_add(idx, &cmd640_port_info);
  775. return 1;
  776. }
  777. module_param_named(probe_vlb, cmd640_vlb, bool, 0);
  778. MODULE_PARM_DESC(probe_vlb, "probe for VLB version of CMD640 chipset");
  779. module_init(cmd640x_init);
  780. MODULE_LICENSE("GPL");