cmd640.c 24 KB

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