cmd640.c 24 KB

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