cmd640.c 24 KB

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