cmd640.c 22 KB

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