cmd640.c 24 KB

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