/drivers/ide/cmd640.c

http://github.com/mirrors/linux · C · 848 lines · 522 code · 107 blank · 219 comment · 81 complexity · ff07e1c6c5b749dadb4e9c893d1ec337 MD5 · raw file

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