cmd640.c 22 KB

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