cmd640.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. #define CMD640_PREFETCH_MASKS 1
  100. //#define CMD640_DUMP_REGS
  101. #include <linux/types.h>
  102. #include <linux/kernel.h>
  103. #include <linux/delay.h>
  104. #include <linux/timer.h>
  105. #include <linux/mm.h>
  106. #include <linux/ioport.h>
  107. #include <linux/blkdev.h>
  108. #include <linux/hdreg.h>
  109. #include <linux/ide.h>
  110. #include <linux/init.h>
  111. #include <asm/io.h>
  112. /*
  113. * This flag is set in ide.c by the parameter: ide0=cmd640_vlb
  114. */
  115. int cmd640_vlb = 0;
  116. /*
  117. * CMD640 specific registers definition.
  118. */
  119. #define VID 0x00
  120. #define DID 0x02
  121. #define PCMD 0x04
  122. #define PCMD_ENA 0x01
  123. #define PSTTS 0x06
  124. #define REVID 0x08
  125. #define PROGIF 0x09
  126. #define SUBCL 0x0a
  127. #define BASCL 0x0b
  128. #define BaseA0 0x10
  129. #define BaseA1 0x14
  130. #define BaseA2 0x18
  131. #define BaseA3 0x1c
  132. #define INTLINE 0x3c
  133. #define INPINE 0x3d
  134. #define CFR 0x50
  135. #define CFR_DEVREV 0x03
  136. #define CFR_IDE01INTR 0x04
  137. #define CFR_DEVID 0x18
  138. #define CFR_AT_VESA_078h 0x20
  139. #define CFR_DSA1 0x40
  140. #define CFR_DSA0 0x80
  141. #define CNTRL 0x51
  142. #define CNTRL_DIS_RA0 0x40
  143. #define CNTRL_DIS_RA1 0x80
  144. #define CNTRL_ENA_2ND 0x08
  145. #define CMDTIM 0x52
  146. #define ARTTIM0 0x53
  147. #define DRWTIM0 0x54
  148. #define ARTTIM1 0x55
  149. #define DRWTIM1 0x56
  150. #define ARTTIM23 0x57
  151. #define ARTTIM23_DIS_RA2 0x04
  152. #define ARTTIM23_DIS_RA3 0x08
  153. #define DRWTIM23 0x58
  154. #define BRST 0x59
  155. /*
  156. * Registers and masks for easy access by drive index:
  157. */
  158. static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
  159. static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
  160. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  161. static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
  162. static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
  163. /*
  164. * Current cmd640 timing values for each drive.
  165. * The defaults for each are the slowest possible timings.
  166. */
  167. static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
  168. static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
  169. static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
  170. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  171. /*
  172. * These are initialized to point at the devices we control
  173. */
  174. static ide_hwif_t *cmd_hwif0, *cmd_hwif1;
  175. static ide_drive_t *cmd_drives[4];
  176. /*
  177. * Interface to access cmd640x registers
  178. */
  179. static unsigned int cmd640_key;
  180. static void (*__put_cmd640_reg)(u16 reg, u8 val);
  181. static u8 (*__get_cmd640_reg)(u16 reg);
  182. /*
  183. * This is read from the CFR reg, and is used in several places.
  184. */
  185. static unsigned int cmd640_chip_version;
  186. /*
  187. * The CMD640x chip does not support DWORD config write cycles, but some
  188. * of the BIOSes use them to implement the config services.
  189. * Therefore, we must use direct IO instead.
  190. */
  191. /* PCI method 1 access */
  192. static void put_cmd640_reg_pci1 (u16 reg, u8 val)
  193. {
  194. outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
  195. outb_p(val, (reg & 3) | 0xcfc);
  196. }
  197. static u8 get_cmd640_reg_pci1 (u16 reg)
  198. {
  199. outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
  200. return inb_p((reg & 3) | 0xcfc);
  201. }
  202. /* PCI method 2 access (from CMD datasheet) */
  203. static void put_cmd640_reg_pci2 (u16 reg, u8 val)
  204. {
  205. outb_p(0x10, 0xcf8);
  206. outb_p(val, cmd640_key + reg);
  207. outb_p(0, 0xcf8);
  208. }
  209. static u8 get_cmd640_reg_pci2 (u16 reg)
  210. {
  211. u8 b;
  212. outb_p(0x10, 0xcf8);
  213. b = inb_p(cmd640_key + reg);
  214. outb_p(0, 0xcf8);
  215. return b;
  216. }
  217. /* VLB access */
  218. static void put_cmd640_reg_vlb (u16 reg, u8 val)
  219. {
  220. outb_p(reg, cmd640_key);
  221. outb_p(val, cmd640_key + 4);
  222. }
  223. static u8 get_cmd640_reg_vlb (u16 reg)
  224. {
  225. outb_p(reg, cmd640_key);
  226. return inb_p(cmd640_key + 4);
  227. }
  228. static u8 get_cmd640_reg(u16 reg)
  229. {
  230. u8 b;
  231. unsigned long flags;
  232. spin_lock_irqsave(&ide_lock, flags);
  233. b = __get_cmd640_reg(reg);
  234. spin_unlock_irqrestore(&ide_lock, flags);
  235. return b;
  236. }
  237. static void put_cmd640_reg(u16 reg, u8 val)
  238. {
  239. unsigned long flags;
  240. spin_lock_irqsave(&ide_lock, flags);
  241. __put_cmd640_reg(reg,val);
  242. spin_unlock_irqrestore(&ide_lock, flags);
  243. }
  244. static int __init match_pci_cmd640_device (void)
  245. {
  246. const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
  247. unsigned int i;
  248. for (i = 0; i < 4; i++) {
  249. if (get_cmd640_reg(i) != ven_dev[i])
  250. return 0;
  251. }
  252. #ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
  253. if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
  254. printk("ide: cmd640 on PCI disabled by BIOS\n");
  255. return 0;
  256. }
  257. #endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
  258. return 1; /* success */
  259. }
  260. /*
  261. * Probe for CMD640x -- pci method 1
  262. */
  263. static int __init probe_for_cmd640_pci1 (void)
  264. {
  265. __get_cmd640_reg = get_cmd640_reg_pci1;
  266. __put_cmd640_reg = put_cmd640_reg_pci1;
  267. for (cmd640_key = 0x80000000;
  268. cmd640_key <= 0x8000f800;
  269. cmd640_key += 0x800) {
  270. if (match_pci_cmd640_device())
  271. return 1; /* success */
  272. }
  273. return 0;
  274. }
  275. /*
  276. * Probe for CMD640x -- pci method 2
  277. */
  278. static int __init probe_for_cmd640_pci2 (void)
  279. {
  280. __get_cmd640_reg = get_cmd640_reg_pci2;
  281. __put_cmd640_reg = put_cmd640_reg_pci2;
  282. for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
  283. if (match_pci_cmd640_device())
  284. return 1; /* success */
  285. }
  286. return 0;
  287. }
  288. /*
  289. * Probe for CMD640x -- vlb
  290. */
  291. static int __init probe_for_cmd640_vlb (void)
  292. {
  293. u8 b;
  294. __get_cmd640_reg = get_cmd640_reg_vlb;
  295. __put_cmd640_reg = put_cmd640_reg_vlb;
  296. cmd640_key = 0x178;
  297. b = get_cmd640_reg(CFR);
  298. if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
  299. cmd640_key = 0x78;
  300. b = get_cmd640_reg(CFR);
  301. if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
  302. return 0;
  303. }
  304. return 1; /* success */
  305. }
  306. /*
  307. * Returns 1 if an IDE interface/drive exists at 0x170,
  308. * Returns 0 otherwise.
  309. */
  310. static int __init secondary_port_responding (void)
  311. {
  312. unsigned long flags;
  313. spin_lock_irqsave(&ide_lock, flags);
  314. outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */
  315. udelay(100);
  316. if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
  317. outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
  318. udelay(100);
  319. if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
  320. spin_unlock_irqrestore(&ide_lock, flags);
  321. return 0; /* nothing responded */
  322. }
  323. }
  324. spin_unlock_irqrestore(&ide_lock, flags);
  325. return 1; /* success */
  326. }
  327. #ifdef CMD640_DUMP_REGS
  328. /*
  329. * Dump out all cmd640 registers. May be called from ide.c
  330. */
  331. static void cmd640_dump_regs (void)
  332. {
  333. unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
  334. /* Dump current state of chip registers */
  335. printk("ide: cmd640 internal register dump:");
  336. for (; reg <= 0x59; reg++) {
  337. if (!(reg & 0x0f))
  338. printk("\n%04x:", reg);
  339. printk(" %02x", get_cmd640_reg(reg));
  340. }
  341. printk("\n");
  342. }
  343. #endif
  344. /*
  345. * Check whether prefetch is on for a drive,
  346. * and initialize the unmask flags for safe operation.
  347. */
  348. static void __init check_prefetch (unsigned int index)
  349. {
  350. ide_drive_t *drive = cmd_drives[index];
  351. u8 b = get_cmd640_reg(prefetch_regs[index]);
  352. if (b & prefetch_masks[index]) { /* is prefetch off? */
  353. drive->no_unmask = 0;
  354. drive->no_io_32bit = 1;
  355. drive->io_32bit = 0;
  356. } else {
  357. #if CMD640_PREFETCH_MASKS
  358. drive->no_unmask = 1;
  359. drive->unmask = 0;
  360. #endif
  361. drive->no_io_32bit = 0;
  362. }
  363. }
  364. /*
  365. * Figure out which devices we control
  366. */
  367. static void __init setup_device_ptrs (void)
  368. {
  369. unsigned int i;
  370. cmd_hwif0 = &ide_hwifs[0]; /* default, if not found below */
  371. cmd_hwif1 = &ide_hwifs[1]; /* default, if not found below */
  372. for (i = 0; i < MAX_HWIFS; i++) {
  373. ide_hwif_t *hwif = &ide_hwifs[i];
  374. if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced) {
  375. if (hwif->io_ports[IDE_DATA_OFFSET] == 0x1f0)
  376. cmd_hwif0 = hwif;
  377. else if (hwif->io_ports[IDE_DATA_OFFSET] == 0x170)
  378. cmd_hwif1 = hwif;
  379. }
  380. }
  381. cmd_drives[0] = &cmd_hwif0->drives[0];
  382. cmd_drives[1] = &cmd_hwif0->drives[1];
  383. cmd_drives[2] = &cmd_hwif1->drives[0];
  384. cmd_drives[3] = &cmd_hwif1->drives[1];
  385. }
  386. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  387. /*
  388. * Sets prefetch mode for a drive.
  389. */
  390. static void set_prefetch_mode (unsigned int index, int mode)
  391. {
  392. ide_drive_t *drive = cmd_drives[index];
  393. int reg = prefetch_regs[index];
  394. u8 b;
  395. unsigned long flags;
  396. spin_lock_irqsave(&ide_lock, flags);
  397. b = __get_cmd640_reg(reg);
  398. if (mode) { /* want prefetch on? */
  399. #if CMD640_PREFETCH_MASKS
  400. drive->no_unmask = 1;
  401. drive->unmask = 0;
  402. #endif
  403. drive->no_io_32bit = 0;
  404. b &= ~prefetch_masks[index]; /* enable prefetch */
  405. } else {
  406. drive->no_unmask = 0;
  407. drive->no_io_32bit = 1;
  408. drive->io_32bit = 0;
  409. b |= prefetch_masks[index]; /* disable prefetch */
  410. }
  411. __put_cmd640_reg(reg, b);
  412. spin_unlock_irqrestore(&ide_lock, flags);
  413. }
  414. /*
  415. * Dump out current drive clocks settings
  416. */
  417. static void display_clocks (unsigned int index)
  418. {
  419. u8 active_count, recovery_count;
  420. active_count = active_counts[index];
  421. if (active_count == 1)
  422. ++active_count;
  423. recovery_count = recovery_counts[index];
  424. if (active_count > 3 && recovery_count == 1)
  425. ++recovery_count;
  426. if (cmd640_chip_version > 1)
  427. recovery_count += 1; /* cmd640b uses (count + 1)*/
  428. printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
  429. }
  430. /*
  431. * Pack active and recovery counts into single byte representation
  432. * used by controller
  433. */
  434. static inline u8 pack_nibbles (u8 upper, u8 lower)
  435. {
  436. return ((upper & 0x0f) << 4) | (lower & 0x0f);
  437. }
  438. /*
  439. * This routine retrieves the initial drive timings from the chipset.
  440. */
  441. static void __init retrieve_drive_counts (unsigned int index)
  442. {
  443. u8 b;
  444. /*
  445. * Get the internal setup timing, and convert to clock count
  446. */
  447. b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
  448. switch (b) {
  449. case 0x00: b = 4; break;
  450. case 0x80: b = 3; break;
  451. case 0x40: b = 2; break;
  452. default: b = 5; break;
  453. }
  454. setup_counts[index] = b;
  455. /*
  456. * Get the active/recovery counts
  457. */
  458. b = get_cmd640_reg(drwtim_regs[index]);
  459. active_counts[index] = (b >> 4) ? (b >> 4) : 0x10;
  460. recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
  461. }
  462. /*
  463. * This routine writes the prepared setup/active/recovery counts
  464. * for a drive into the cmd640 chipset registers to active them.
  465. */
  466. static void program_drive_counts (unsigned int index)
  467. {
  468. unsigned long flags;
  469. u8 setup_count = setup_counts[index];
  470. u8 active_count = active_counts[index];
  471. u8 recovery_count = recovery_counts[index];
  472. /*
  473. * Set up address setup count and drive read/write timing registers.
  474. * Primary interface has individual count/timing registers for
  475. * each drive. Secondary interface has one common set of registers,
  476. * so we merge the timings, using the slowest value for each timing.
  477. */
  478. if (index > 1) {
  479. unsigned int mate;
  480. if (cmd_drives[mate = index ^ 1]->present) {
  481. if (setup_count < setup_counts[mate])
  482. setup_count = setup_counts[mate];
  483. if (active_count < active_counts[mate])
  484. active_count = active_counts[mate];
  485. if (recovery_count < recovery_counts[mate])
  486. recovery_count = recovery_counts[mate];
  487. }
  488. }
  489. /*
  490. * Convert setup_count to internal chipset representation
  491. */
  492. switch (setup_count) {
  493. case 4: setup_count = 0x00; break;
  494. case 3: setup_count = 0x80; break;
  495. case 1:
  496. case 2: setup_count = 0x40; break;
  497. default: setup_count = 0xc0; /* case 5 */
  498. }
  499. /*
  500. * Now that everything is ready, program the new timings
  501. */
  502. spin_lock_irqsave(&ide_lock, flags);
  503. /*
  504. * Program the address_setup clocks into ARTTIM reg,
  505. * and then the active/recovery counts into the DRWTIM reg
  506. * (this converts counts of 16 into counts of zero -- okay).
  507. */
  508. setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
  509. __put_cmd640_reg(arttim_regs[index], setup_count);
  510. __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
  511. spin_unlock_irqrestore(&ide_lock, flags);
  512. }
  513. /*
  514. * Set a specific pio_mode for a drive
  515. */
  516. static void cmd640_set_mode (unsigned int index, u8 pio_mode, unsigned int cycle_time)
  517. {
  518. int setup_time, active_time, recovery_time, clock_time;
  519. u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
  520. int bus_speed = system_bus_clock();
  521. if (pio_mode > 5)
  522. pio_mode = 5;
  523. setup_time = ide_pio_timings[pio_mode].setup_time;
  524. active_time = ide_pio_timings[pio_mode].active_time;
  525. recovery_time = cycle_time - (setup_time + active_time);
  526. clock_time = 1000 / bus_speed;
  527. cycle_count = (cycle_time + clock_time - 1) / clock_time;
  528. setup_count = (setup_time + clock_time - 1) / clock_time;
  529. active_count = (active_time + clock_time - 1) / clock_time;
  530. if (active_count < 2)
  531. active_count = 2; /* minimum allowed by cmd640 */
  532. recovery_count = (recovery_time + clock_time - 1) / clock_time;
  533. recovery_count2 = cycle_count - (setup_count + active_count);
  534. if (recovery_count2 > recovery_count)
  535. recovery_count = recovery_count2;
  536. if (recovery_count < 2)
  537. recovery_count = 2; /* minimum allowed by cmd640 */
  538. if (recovery_count > 17) {
  539. active_count += recovery_count - 17;
  540. recovery_count = 17;
  541. }
  542. if (active_count > 16)
  543. active_count = 16; /* maximum allowed by cmd640 */
  544. if (cmd640_chip_version > 1)
  545. recovery_count -= 1; /* cmd640b uses (count + 1)*/
  546. if (recovery_count > 16)
  547. recovery_count = 16; /* maximum allowed by cmd640 */
  548. setup_counts[index] = setup_count;
  549. active_counts[index] = active_count;
  550. recovery_counts[index] = recovery_count;
  551. /*
  552. * In a perfect world, we might set the drive pio mode here
  553. * (using WIN_SETFEATURE) before continuing.
  554. *
  555. * But we do not, because:
  556. * 1) this is the wrong place to do it (proper is do_special() in ide.c)
  557. * 2) in practice this is rarely, if ever, necessary
  558. */
  559. program_drive_counts (index);
  560. }
  561. static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
  562. {
  563. unsigned int index = 0, cycle_time;
  564. u8 b;
  565. while (drive != cmd_drives[index]) {
  566. if (++index > 3) {
  567. printk(KERN_ERR "%s: bad news in %s\n",
  568. drive->name, __FUNCTION__);
  569. return;
  570. }
  571. }
  572. switch (pio) {
  573. case 6: /* set fast-devsel off */
  574. case 7: /* set fast-devsel on */
  575. b = get_cmd640_reg(CNTRL) & ~0x27;
  576. if (pio & 1)
  577. b |= 0x27;
  578. put_cmd640_reg(CNTRL, b);
  579. printk("%s: %sabled cmd640 fast host timing (devsel)\n", drive->name, (pio & 1) ? "en" : "dis");
  580. return;
  581. case 8: /* set prefetch off */
  582. case 9: /* set prefetch on */
  583. set_prefetch_mode(index, pio & 1);
  584. printk("%s: %sabled cmd640 prefetch\n", drive->name, (pio & 1) ? "en" : "dis");
  585. return;
  586. }
  587. cycle_time = ide_pio_cycle_time(drive, pio);
  588. cmd640_set_mode(index, pio, cycle_time);
  589. printk("%s: selected cmd640 PIO mode%d (%dns)",
  590. drive->name, pio, cycle_time);
  591. display_clocks(index);
  592. }
  593. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  594. static int pci_conf1(void)
  595. {
  596. u32 tmp;
  597. unsigned long flags;
  598. spin_lock_irqsave(&ide_lock, flags);
  599. outb(0x01, 0xCFB);
  600. tmp = inl(0xCF8);
  601. outl(0x80000000, 0xCF8);
  602. if (inl(0xCF8) == 0x80000000) {
  603. outl(tmp, 0xCF8);
  604. spin_unlock_irqrestore(&ide_lock, flags);
  605. return 1;
  606. }
  607. outl(tmp, 0xCF8);
  608. spin_unlock_irqrestore(&ide_lock, flags);
  609. return 0;
  610. }
  611. static int pci_conf2(void)
  612. {
  613. unsigned long flags;
  614. spin_lock_irqsave(&ide_lock, flags);
  615. outb(0x00, 0xCFB);
  616. outb(0x00, 0xCF8);
  617. outb(0x00, 0xCFA);
  618. if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
  619. spin_unlock_irqrestore(&ide_lock, flags);
  620. return 1;
  621. }
  622. spin_unlock_irqrestore(&ide_lock, flags);
  623. return 0;
  624. }
  625. /*
  626. * Probe for a cmd640 chipset, and initialize it if found. Called from ide.c
  627. */
  628. int __init ide_probe_for_cmd640x (void)
  629. {
  630. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  631. int second_port_toggled = 0;
  632. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  633. int second_port_cmd640 = 0;
  634. const char *bus_type, *port2;
  635. unsigned int index;
  636. u8 b, cfr;
  637. if (cmd640_vlb && probe_for_cmd640_vlb()) {
  638. bus_type = "VLB";
  639. } else {
  640. cmd640_vlb = 0;
  641. /* Find out what kind of PCI probing is supported otherwise
  642. Justin Gibbs will sulk.. */
  643. if (pci_conf1() && probe_for_cmd640_pci1())
  644. bus_type = "PCI (type1)";
  645. else if (pci_conf2() && probe_for_cmd640_pci2())
  646. bus_type = "PCI (type2)";
  647. else
  648. return 0;
  649. }
  650. /*
  651. * Undocumented magic (there is no 0x5b reg in specs)
  652. */
  653. put_cmd640_reg(0x5b, 0xbd);
  654. if (get_cmd640_reg(0x5b) != 0xbd) {
  655. printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
  656. return 0;
  657. }
  658. put_cmd640_reg(0x5b, 0);
  659. #ifdef CMD640_DUMP_REGS
  660. cmd640_dump_regs();
  661. #endif
  662. /*
  663. * Documented magic begins here
  664. */
  665. cfr = get_cmd640_reg(CFR);
  666. cmd640_chip_version = cfr & CFR_DEVREV;
  667. if (cmd640_chip_version == 0) {
  668. printk ("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
  669. return 0;
  670. }
  671. /*
  672. * Initialize data for primary port
  673. */
  674. setup_device_ptrs ();
  675. printk("%s: buggy cmd640%c interface on %s, config=0x%02x\n",
  676. cmd_hwif0->name, 'a' + cmd640_chip_version - 1, bus_type, cfr);
  677. cmd_hwif0->chipset = ide_cmd640;
  678. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  679. cmd_hwif0->host_flags = IDE_HFLAG_ABUSE_PREFETCH |
  680. IDE_HFLAG_ABUSE_FAST_DEVSEL;
  681. cmd_hwif0->pio_mask = ATA_PIO5;
  682. cmd_hwif0->set_pio_mode = &cmd640_set_pio_mode;
  683. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  684. /*
  685. * Ensure compatibility by always using the slowest timings
  686. * for access to the drive's command register block,
  687. * and reset the prefetch burstsize to default (512 bytes).
  688. *
  689. * Maybe we need a way to NOT do these on *some* systems?
  690. */
  691. put_cmd640_reg(CMDTIM, 0);
  692. put_cmd640_reg(BRST, 0x40);
  693. /*
  694. * Try to enable the secondary interface, if not already enabled
  695. */
  696. if (cmd_hwif1->noprobe) {
  697. port2 = "not probed";
  698. } else {
  699. b = get_cmd640_reg(CNTRL);
  700. if (secondary_port_responding()) {
  701. if ((b & CNTRL_ENA_2ND)) {
  702. second_port_cmd640 = 1;
  703. port2 = "okay";
  704. } else if (cmd640_vlb) {
  705. second_port_cmd640 = 1;
  706. port2 = "alive";
  707. } else
  708. port2 = "not cmd640";
  709. } else {
  710. put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
  711. if (secondary_port_responding()) {
  712. second_port_cmd640 = 1;
  713. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  714. second_port_toggled = 1;
  715. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  716. port2 = "enabled";
  717. } else {
  718. put_cmd640_reg(CNTRL, b); /* restore original setting */
  719. port2 = "not responding";
  720. }
  721. }
  722. }
  723. /*
  724. * Initialize data for secondary cmd640 port, if enabled
  725. */
  726. if (second_port_cmd640) {
  727. cmd_hwif0->serialized = 1;
  728. cmd_hwif1->serialized = 1;
  729. cmd_hwif1->chipset = ide_cmd640;
  730. cmd_hwif0->mate = cmd_hwif1;
  731. cmd_hwif1->mate = cmd_hwif0;
  732. cmd_hwif1->channel = 1;
  733. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  734. cmd_hwif1->host_flags = IDE_HFLAG_ABUSE_PREFETCH |
  735. IDE_HFLAG_ABUSE_FAST_DEVSEL;
  736. cmd_hwif1->pio_mask = ATA_PIO5;
  737. cmd_hwif1->set_pio_mode = &cmd640_set_pio_mode;
  738. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  739. }
  740. printk(KERN_INFO "%s: %sserialized, secondary interface %s\n", cmd_hwif1->name,
  741. cmd_hwif0->serialized ? "" : "not ", port2);
  742. /*
  743. * Establish initial timings/prefetch for all drives.
  744. * Do not unnecessarily disturb any prior BIOS setup of these.
  745. */
  746. for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
  747. ide_drive_t *drive = cmd_drives[index];
  748. #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
  749. if (drive->autotune || ((index > 1) && second_port_toggled)) {
  750. /*
  751. * Reset timing to the slowest speed and turn off prefetch.
  752. * This way, the drive identify code has a better chance.
  753. */
  754. setup_counts [index] = 4; /* max possible */
  755. active_counts [index] = 16; /* max possible */
  756. recovery_counts [index] = 16; /* max possible */
  757. program_drive_counts (index);
  758. set_prefetch_mode (index, 0);
  759. printk("cmd640: drive%d timings/prefetch cleared\n", index);
  760. } else {
  761. /*
  762. * Record timings/prefetch without changing them.
  763. * This preserves any prior BIOS setup.
  764. */
  765. retrieve_drive_counts (index);
  766. check_prefetch (index);
  767. printk("cmd640: drive%d timings/prefetch(%s) preserved",
  768. index, drive->no_io_32bit ? "off" : "on");
  769. display_clocks(index);
  770. }
  771. #else
  772. /*
  773. * Set the drive unmask flags to match the prefetch setting
  774. */
  775. check_prefetch (index);
  776. printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
  777. index, drive->no_io_32bit ? "off" : "on");
  778. #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
  779. }
  780. #ifdef CMD640_DUMP_REGS
  781. cmd640_dump_regs();
  782. #endif
  783. return 1;
  784. }