cmd64x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* $Id: cmd64x.c,v 1.21 2000/01/30 23:23:16
  2. *
  3. * linux/drivers/ide/pci/cmd64x.c Version 1.42 Feb 8, 2007
  4. *
  5. * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
  6. * Note, this driver is not used at all on other systems because
  7. * there the "BIOS" has done all of the following already.
  8. * Due to massive hardware bugs, UltraDMA is only supported
  9. * on the 646U2 and not on the 646U.
  10. *
  11. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  12. * Copyright (C) 1998 David S. Miller (davem@redhat.com)
  13. *
  14. * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
  15. * Copyright (C) 2007 MontaVista Software, Inc. <source@mvista.com>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/hdreg.h>
  22. #include <linux/ide.h>
  23. #include <linux/init.h>
  24. #include <asm/io.h>
  25. #define DISPLAY_CMD64X_TIMINGS
  26. #define CMD_DEBUG 0
  27. #if CMD_DEBUG
  28. #define cmdprintk(x...) printk(x)
  29. #else
  30. #define cmdprintk(x...)
  31. #endif
  32. /*
  33. * CMD64x specific registers definition.
  34. */
  35. #define CFR 0x50
  36. #define CFR_INTR_CH0 0x02
  37. #define CNTRL 0x51
  38. #define CNTRL_DIS_RA0 0x40
  39. #define CNTRL_DIS_RA1 0x80
  40. #define CNTRL_ENA_2ND 0x08
  41. #define CMDTIM 0x52
  42. #define ARTTIM0 0x53
  43. #define DRWTIM0 0x54
  44. #define ARTTIM1 0x55
  45. #define DRWTIM1 0x56
  46. #define ARTTIM23 0x57
  47. #define ARTTIM23_DIS_RA2 0x04
  48. #define ARTTIM23_DIS_RA3 0x08
  49. #define ARTTIM23_INTR_CH1 0x10
  50. #define ARTTIM2 0x57
  51. #define ARTTIM3 0x57
  52. #define DRWTIM23 0x58
  53. #define DRWTIM2 0x58
  54. #define BRST 0x59
  55. #define DRWTIM3 0x5b
  56. #define BMIDECR0 0x70
  57. #define MRDMODE 0x71
  58. #define MRDMODE_INTR_CH0 0x04
  59. #define MRDMODE_INTR_CH1 0x08
  60. #define MRDMODE_BLK_CH0 0x10
  61. #define MRDMODE_BLK_CH1 0x20
  62. #define BMIDESR0 0x72
  63. #define UDIDETCR0 0x73
  64. #define DTPR0 0x74
  65. #define BMIDECR1 0x78
  66. #define BMIDECSR 0x79
  67. #define BMIDESR1 0x7A
  68. #define UDIDETCR1 0x7B
  69. #define DTPR1 0x7C
  70. #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
  71. #include <linux/stat.h>
  72. #include <linux/proc_fs.h>
  73. static u8 cmd64x_proc = 0;
  74. #define CMD_MAX_DEVS 5
  75. static struct pci_dev *cmd_devs[CMD_MAX_DEVS];
  76. static int n_cmd_devs;
  77. static char * print_cmd64x_get_info (char *buf, struct pci_dev *dev, int index)
  78. {
  79. char *p = buf;
  80. u8 reg53 = 0, reg54 = 0, reg55 = 0, reg56 = 0; /* primary */
  81. u8 reg57 = 0, reg58 = 0, reg5b; /* secondary */
  82. u8 reg72 = 0, reg73 = 0; /* primary */
  83. u8 reg7a = 0, reg7b = 0; /* secondary */
  84. u8 reg50 = 0, reg71 = 0; /* extra */
  85. p += sprintf(p, "\nController: %d\n", index);
  86. p += sprintf(p, "CMD%x Chipset.\n", dev->device);
  87. (void) pci_read_config_byte(dev, CFR, &reg50);
  88. (void) pci_read_config_byte(dev, ARTTIM0, &reg53);
  89. (void) pci_read_config_byte(dev, DRWTIM0, &reg54);
  90. (void) pci_read_config_byte(dev, ARTTIM1, &reg55);
  91. (void) pci_read_config_byte(dev, DRWTIM1, &reg56);
  92. (void) pci_read_config_byte(dev, ARTTIM2, &reg57);
  93. (void) pci_read_config_byte(dev, DRWTIM2, &reg58);
  94. (void) pci_read_config_byte(dev, DRWTIM3, &reg5b);
  95. (void) pci_read_config_byte(dev, MRDMODE, &reg71);
  96. (void) pci_read_config_byte(dev, BMIDESR0, &reg72);
  97. (void) pci_read_config_byte(dev, UDIDETCR0, &reg73);
  98. (void) pci_read_config_byte(dev, BMIDESR1, &reg7a);
  99. (void) pci_read_config_byte(dev, UDIDETCR1, &reg7b);
  100. p += sprintf(p, "--------------- Primary Channel "
  101. "---------------- Secondary Channel "
  102. "-------------\n");
  103. p += sprintf(p, " %sabled "
  104. " %sabled\n",
  105. (reg72&0x80)?"dis":" en",
  106. (reg7a&0x80)?"dis":" en");
  107. p += sprintf(p, "--------------- drive0 "
  108. "--------- drive1 -------- drive0 "
  109. "---------- drive1 ------\n");
  110. p += sprintf(p, "DMA enabled: %s %s"
  111. " %s %s\n",
  112. (reg72&0x20)?"yes":"no ", (reg72&0x40)?"yes":"no ",
  113. (reg7a&0x20)?"yes":"no ", (reg7a&0x40)?"yes":"no ");
  114. p += sprintf(p, "DMA Mode: %s(%s) %s(%s)",
  115. (reg72&0x20)?((reg73&0x01)?"UDMA":" DMA"):" PIO",
  116. (reg72&0x20)?(
  117. ((reg73&0x30)==0x30)?(((reg73&0x35)==0x35)?"3":"0"):
  118. ((reg73&0x20)==0x20)?(((reg73&0x25)==0x25)?"3":"1"):
  119. ((reg73&0x10)==0x10)?(((reg73&0x15)==0x15)?"4":"2"):
  120. ((reg73&0x00)==0x00)?(((reg73&0x05)==0x05)?"5":"2"):
  121. "X"):"?",
  122. (reg72&0x40)?((reg73&0x02)?"UDMA":" DMA"):" PIO",
  123. (reg72&0x40)?(
  124. ((reg73&0xC0)==0xC0)?(((reg73&0xC5)==0xC5)?"3":"0"):
  125. ((reg73&0x80)==0x80)?(((reg73&0x85)==0x85)?"3":"1"):
  126. ((reg73&0x40)==0x40)?(((reg73&0x4A)==0x4A)?"4":"2"):
  127. ((reg73&0x00)==0x00)?(((reg73&0x0A)==0x0A)?"5":"2"):
  128. "X"):"?");
  129. p += sprintf(p, " %s(%s) %s(%s)\n",
  130. (reg7a&0x20)?((reg7b&0x01)?"UDMA":" DMA"):" PIO",
  131. (reg7a&0x20)?(
  132. ((reg7b&0x30)==0x30)?(((reg7b&0x35)==0x35)?"3":"0"):
  133. ((reg7b&0x20)==0x20)?(((reg7b&0x25)==0x25)?"3":"1"):
  134. ((reg7b&0x10)==0x10)?(((reg7b&0x15)==0x15)?"4":"2"):
  135. ((reg7b&0x00)==0x00)?(((reg7b&0x05)==0x05)?"5":"2"):
  136. "X"):"?",
  137. (reg7a&0x40)?((reg7b&0x02)?"UDMA":" DMA"):" PIO",
  138. (reg7a&0x40)?(
  139. ((reg7b&0xC0)==0xC0)?(((reg7b&0xC5)==0xC5)?"3":"0"):
  140. ((reg7b&0x80)==0x80)?(((reg7b&0x85)==0x85)?"3":"1"):
  141. ((reg7b&0x40)==0x40)?(((reg7b&0x4A)==0x4A)?"4":"2"):
  142. ((reg7b&0x00)==0x00)?(((reg7b&0x0A)==0x0A)?"5":"2"):
  143. "X"):"?" );
  144. p += sprintf(p, "PIO Mode: %s %s"
  145. " %s %s\n",
  146. "?", "?", "?", "?");
  147. p += sprintf(p, " %s %s\n",
  148. (reg50 & CFR_INTR_CH0) ? "interrupting" : "polling ",
  149. (reg57 & ARTTIM23_INTR_CH1) ? "interrupting" : "polling");
  150. p += sprintf(p, " %s %s\n",
  151. (reg71 & MRDMODE_INTR_CH0) ? "pending" : "clear ",
  152. (reg71 & MRDMODE_INTR_CH1) ? "pending" : "clear");
  153. p += sprintf(p, " %s %s\n",
  154. (reg71 & MRDMODE_BLK_CH0) ? "blocked" : "enabled",
  155. (reg71 & MRDMODE_BLK_CH1) ? "blocked" : "enabled");
  156. return (char *)p;
  157. }
  158. static int cmd64x_get_info (char *buffer, char **addr, off_t offset, int count)
  159. {
  160. char *p = buffer;
  161. int i;
  162. p += sprintf(p, "\n");
  163. for (i = 0; i < n_cmd_devs; i++) {
  164. struct pci_dev *dev = cmd_devs[i];
  165. p = print_cmd64x_get_info(p, dev, i);
  166. }
  167. return p-buffer; /* => must be less than 4k! */
  168. }
  169. #endif /* defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS) */
  170. static u8 quantize_timing(int timing, int quant)
  171. {
  172. return (timing + quant - 1) / quant;
  173. }
  174. /*
  175. * This routine writes the prepared setup/active/recovery counts
  176. * for a drive into the cmd646 chipset registers to active them.
  177. */
  178. static void program_drive_counts (ide_drive_t *drive, int setup_count, int active_count, int recovery_count)
  179. {
  180. unsigned long flags;
  181. struct pci_dev *dev = HWIF(drive)->pci_dev;
  182. ide_drive_t *drives = HWIF(drive)->drives;
  183. u8 temp_b;
  184. static const u8 setup_counts[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
  185. static const u8 recovery_counts[] =
  186. {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
  187. static const u8 arttim_regs[2][2] = {
  188. { ARTTIM0, ARTTIM1 },
  189. { ARTTIM23, ARTTIM23 }
  190. };
  191. static const u8 drwtim_regs[2][2] = {
  192. { DRWTIM0, DRWTIM1 },
  193. { DRWTIM2, DRWTIM3 }
  194. };
  195. int channel = (int) HWIF(drive)->channel;
  196. int slave = (drives != drive); /* Is this really the best way to determine this?? */
  197. cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n",
  198. setup_count, active_count, recovery_count, drive->present);
  199. /*
  200. * Set up address setup count registers.
  201. * Primary interface has individual count/timing registers for
  202. * each drive. Secondary interface has one common set of registers,
  203. * for address setup so we merge these timings, using the slowest
  204. * value.
  205. */
  206. if (channel) {
  207. drive->drive_data = setup_count;
  208. setup_count = max(drives[0].drive_data,
  209. drives[1].drive_data);
  210. cmdprintk("Secondary interface, setup_count = %d\n",
  211. setup_count);
  212. }
  213. /*
  214. * Convert values to internal chipset representation
  215. */
  216. setup_count = (setup_count > 5) ? 0xc0 : (int) setup_counts[setup_count];
  217. active_count &= 0xf; /* Remember, max value is 16 */
  218. recovery_count = (int) recovery_counts[recovery_count];
  219. cmdprintk("Final values = %d,%d,%d\n",
  220. setup_count, active_count, recovery_count);
  221. /*
  222. * Now that everything is ready, program the new timings
  223. */
  224. local_irq_save(flags);
  225. /*
  226. * Program the address_setup clocks into ARTTIM reg,
  227. * and then the active/recovery counts into the DRWTIM reg
  228. */
  229. (void) pci_read_config_byte(dev, arttim_regs[channel][slave], &temp_b);
  230. (void) pci_write_config_byte(dev, arttim_regs[channel][slave],
  231. ((u8) setup_count) | (temp_b & 0x3f));
  232. (void) pci_write_config_byte(dev, drwtim_regs[channel][slave],
  233. (u8) ((active_count << 4) | recovery_count));
  234. cmdprintk ("Write %x to %x\n",
  235. ((u8) setup_count) | (temp_b & 0x3f),
  236. arttim_regs[channel][slave]);
  237. cmdprintk ("Write %x to %x\n",
  238. (u8) ((active_count << 4) | recovery_count),
  239. drwtim_regs[channel][slave]);
  240. local_irq_restore(flags);
  241. }
  242. /*
  243. * This routine selects drive's best PIO mode, calculates setup/active/recovery
  244. * counts, and then writes them into the chipset registers.
  245. */
  246. static u8 cmd64x_tune_pio (ide_drive_t *drive, u8 mode_wanted)
  247. {
  248. int setup_time, active_time, cycle_time;
  249. u8 cycle_count, setup_count, active_count, recovery_count;
  250. u8 pio_mode;
  251. int clock_time = 1000 / system_bus_clock();
  252. ide_pio_data_t pio;
  253. pio_mode = ide_get_best_pio_mode(drive, mode_wanted, 5, &pio);
  254. cycle_time = pio.cycle_time;
  255. setup_time = ide_pio_timings[pio_mode].setup_time;
  256. active_time = ide_pio_timings[pio_mode].active_time;
  257. setup_count = quantize_timing( setup_time, clock_time);
  258. cycle_count = quantize_timing( cycle_time, clock_time);
  259. active_count = quantize_timing(active_time, clock_time);
  260. recovery_count = cycle_count - active_count;
  261. /* program_drive_counts() takes care of zero recovery cycles */
  262. if (recovery_count > 16) {
  263. active_count += recovery_count - 16;
  264. recovery_count = 16;
  265. }
  266. if (active_count > 16)
  267. active_count = 16; /* maximum allowed by cmd64x */
  268. program_drive_counts (drive, setup_count, active_count, recovery_count);
  269. cmdprintk("%s: PIO mode wanted %d, selected %d (%dns)%s, "
  270. "clocks=%d/%d/%d\n",
  271. drive->name, mode_wanted, pio_mode, cycle_time,
  272. pio.overridden ? " (overriding vendor mode)" : "",
  273. setup_count, active_count, recovery_count);
  274. return pio_mode;
  275. }
  276. /*
  277. * Attempts to set drive's PIO mode.
  278. * Special cases are 8: prefetch off, 9: prefetch on (both never worked),
  279. * and 255: auto-select best mode (used at boot time).
  280. */
  281. static void cmd64x_tune_drive (ide_drive_t *drive, u8 pio)
  282. {
  283. /*
  284. * Filter out the prefetch control values
  285. * to prevent PIO5 from being programmed
  286. */
  287. if (pio == 8 || pio == 9)
  288. return;
  289. pio = cmd64x_tune_pio(drive, pio);
  290. (void) ide_config_drive_speed(drive, XFER_PIO_0 + pio);
  291. }
  292. static u8 cmd64x_ratemask (ide_drive_t *drive)
  293. {
  294. struct pci_dev *dev = HWIF(drive)->pci_dev;
  295. u8 mode = 0;
  296. switch(dev->device) {
  297. case PCI_DEVICE_ID_CMD_649:
  298. mode = 3;
  299. break;
  300. case PCI_DEVICE_ID_CMD_648:
  301. mode = 2;
  302. break;
  303. case PCI_DEVICE_ID_CMD_643:
  304. return 0;
  305. case PCI_DEVICE_ID_CMD_646:
  306. {
  307. unsigned int class_rev = 0;
  308. pci_read_config_dword(dev,
  309. PCI_CLASS_REVISION, &class_rev);
  310. class_rev &= 0xff;
  311. /*
  312. * UltraDMA only supported on PCI646U and PCI646U2, which
  313. * correspond to revisions 0x03, 0x05 and 0x07 respectively.
  314. * Actually, although the CMD tech support people won't
  315. * tell me the details, the 0x03 revision cannot support
  316. * UDMA correctly without hardware modifications, and even
  317. * then it only works with Quantum disks due to some
  318. * hold time assumptions in the 646U part which are fixed
  319. * in the 646U2.
  320. *
  321. * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
  322. */
  323. switch(class_rev) {
  324. case 0x07:
  325. case 0x05:
  326. return 1;
  327. case 0x03:
  328. case 0x01:
  329. default:
  330. return 0;
  331. }
  332. }
  333. }
  334. if (!eighty_ninty_three(drive))
  335. mode = min(mode, (u8)1);
  336. return mode;
  337. }
  338. static int cmd64x_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  339. {
  340. ide_hwif_t *hwif = HWIF(drive);
  341. struct pci_dev *dev = hwif->pci_dev;
  342. u8 unit = (drive->select.b.unit & 0x01);
  343. u8 regU = 0, pciU = (hwif->channel) ? UDIDETCR1 : UDIDETCR0;
  344. u8 regD = 0, pciD = (hwif->channel) ? BMIDESR1 : BMIDESR0;
  345. u8 speed = ide_rate_filter(cmd64x_ratemask(drive), xferspeed);
  346. if (speed >= XFER_SW_DMA_0) {
  347. (void) pci_read_config_byte(dev, pciD, &regD);
  348. (void) pci_read_config_byte(dev, pciU, &regU);
  349. regD &= ~(unit ? 0x40 : 0x20);
  350. regU &= ~(unit ? 0xCA : 0x35);
  351. (void) pci_write_config_byte(dev, pciD, regD);
  352. (void) pci_write_config_byte(dev, pciU, regU);
  353. (void) pci_read_config_byte(dev, pciD, &regD);
  354. (void) pci_read_config_byte(dev, pciU, &regU);
  355. }
  356. switch(speed) {
  357. case XFER_UDMA_5: regU |= (unit ? 0x0A : 0x05); break;
  358. case XFER_UDMA_4: regU |= (unit ? 0x4A : 0x15); break;
  359. case XFER_UDMA_3: regU |= (unit ? 0x8A : 0x25); break;
  360. case XFER_UDMA_2: regU |= (unit ? 0x42 : 0x11); break;
  361. case XFER_UDMA_1: regU |= (unit ? 0x82 : 0x21); break;
  362. case XFER_UDMA_0: regU |= (unit ? 0xC2 : 0x31); break;
  363. case XFER_MW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
  364. case XFER_MW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
  365. case XFER_MW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
  366. case XFER_SW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
  367. case XFER_SW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
  368. case XFER_SW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
  369. case XFER_PIO_5:
  370. case XFER_PIO_4:
  371. case XFER_PIO_3:
  372. case XFER_PIO_2:
  373. case XFER_PIO_1:
  374. case XFER_PIO_0:
  375. (void) cmd64x_tune_pio(drive, speed - XFER_PIO_0);
  376. break;
  377. default:
  378. return 1;
  379. }
  380. if (speed >= XFER_SW_DMA_0) {
  381. (void) pci_write_config_byte(dev, pciU, regU);
  382. regD |= (unit ? 0x40 : 0x20);
  383. (void) pci_write_config_byte(dev, pciD, regD);
  384. }
  385. return (ide_config_drive_speed(drive, speed));
  386. }
  387. static int config_chipset_for_dma (ide_drive_t *drive)
  388. {
  389. u8 speed = ide_dma_speed(drive, cmd64x_ratemask(drive));
  390. if (!speed)
  391. return 0;
  392. if (cmd64x_tune_chipset(drive, speed))
  393. return 0;
  394. return ide_dma_enable(drive);
  395. }
  396. static int cmd64x_config_drive_for_dma (ide_drive_t *drive)
  397. {
  398. if (ide_use_dma(drive) && config_chipset_for_dma(drive))
  399. return 0;
  400. if (ide_use_fast_pio(drive))
  401. cmd64x_tune_drive(drive, 255);
  402. return -1;
  403. }
  404. static int cmd64x_alt_dma_status (struct pci_dev *dev)
  405. {
  406. switch(dev->device) {
  407. case PCI_DEVICE_ID_CMD_648:
  408. case PCI_DEVICE_ID_CMD_649:
  409. return 1;
  410. default:
  411. break;
  412. }
  413. return 0;
  414. }
  415. static int cmd64x_ide_dma_end (ide_drive_t *drive)
  416. {
  417. u8 dma_stat = 0, dma_cmd = 0;
  418. ide_hwif_t *hwif = HWIF(drive);
  419. struct pci_dev *dev = hwif->pci_dev;
  420. drive->waiting_for_dma = 0;
  421. /* read DMA command state */
  422. dma_cmd = inb(hwif->dma_command);
  423. /* stop DMA */
  424. outb(dma_cmd & ~1, hwif->dma_command);
  425. /* get DMA status */
  426. dma_stat = inb(hwif->dma_status);
  427. /* clear the INTR & ERROR bits */
  428. outb(dma_stat | 6, hwif->dma_status);
  429. if (cmd64x_alt_dma_status(dev)) {
  430. u8 dma_intr = 0;
  431. u8 dma_mask = (hwif->channel) ? ARTTIM23_INTR_CH1 :
  432. CFR_INTR_CH0;
  433. u8 dma_reg = (hwif->channel) ? ARTTIM2 : CFR;
  434. (void) pci_read_config_byte(dev, dma_reg, &dma_intr);
  435. /* clear the INTR bit */
  436. (void) pci_write_config_byte(dev, dma_reg, dma_intr|dma_mask);
  437. }
  438. /* purge DMA mappings */
  439. ide_destroy_dmatable(drive);
  440. /* verify good DMA status */
  441. return (dma_stat & 7) != 4;
  442. }
  443. static int cmd64x_ide_dma_test_irq (ide_drive_t *drive)
  444. {
  445. ide_hwif_t *hwif = HWIF(drive);
  446. struct pci_dev *dev = hwif->pci_dev;
  447. u8 dma_alt_stat = 0, mask = (hwif->channel) ? MRDMODE_INTR_CH1 :
  448. MRDMODE_INTR_CH0;
  449. u8 dma_stat = inb(hwif->dma_status);
  450. (void) pci_read_config_byte(dev, MRDMODE, &dma_alt_stat);
  451. #ifdef DEBUG
  452. printk("%s: dma_stat: 0x%02x dma_alt_stat: "
  453. "0x%02x mask: 0x%02x\n", drive->name,
  454. dma_stat, dma_alt_stat, mask);
  455. #endif
  456. if (!(dma_alt_stat & mask))
  457. return 0;
  458. /* return 1 if INTR asserted */
  459. if ((dma_stat & 4) == 4)
  460. return 1;
  461. return 0;
  462. }
  463. /*
  464. * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
  465. * event order for DMA transfers.
  466. */
  467. static int cmd646_1_ide_dma_end (ide_drive_t *drive)
  468. {
  469. ide_hwif_t *hwif = HWIF(drive);
  470. u8 dma_stat = 0, dma_cmd = 0;
  471. drive->waiting_for_dma = 0;
  472. /* get DMA status */
  473. dma_stat = inb(hwif->dma_status);
  474. /* read DMA command state */
  475. dma_cmd = inb(hwif->dma_command);
  476. /* stop DMA */
  477. outb(dma_cmd & ~1, hwif->dma_command);
  478. /* clear the INTR & ERROR bits */
  479. outb(dma_stat | 6, hwif->dma_status);
  480. /* and free any DMA resources */
  481. ide_destroy_dmatable(drive);
  482. /* verify good DMA status */
  483. return (dma_stat & 7) != 4;
  484. }
  485. static unsigned int __devinit init_chipset_cmd64x(struct pci_dev *dev, const char *name)
  486. {
  487. u32 class_rev = 0;
  488. u8 mrdmode = 0;
  489. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  490. class_rev &= 0xff;
  491. switch(dev->device) {
  492. case PCI_DEVICE_ID_CMD_643:
  493. break;
  494. case PCI_DEVICE_ID_CMD_646:
  495. printk(KERN_INFO "%s: chipset revision 0x%02X, ", name, class_rev);
  496. switch(class_rev) {
  497. case 0x07:
  498. case 0x05:
  499. printk("UltraDMA Capable");
  500. break;
  501. case 0x03:
  502. printk("MultiWord DMA Force Limited");
  503. break;
  504. case 0x01:
  505. default:
  506. printk("MultiWord DMA Limited, IRQ workaround enabled");
  507. break;
  508. }
  509. printk("\n");
  510. break;
  511. case PCI_DEVICE_ID_CMD_648:
  512. case PCI_DEVICE_ID_CMD_649:
  513. break;
  514. default:
  515. break;
  516. }
  517. /* Set a good latency timer and cache line size value. */
  518. (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  519. /* FIXME: pci_set_master() to ensure a good latency timer value */
  520. /* Setup interrupts. */
  521. (void) pci_read_config_byte(dev, MRDMODE, &mrdmode);
  522. mrdmode &= ~(0x30);
  523. (void) pci_write_config_byte(dev, MRDMODE, mrdmode);
  524. /* Use MEMORY READ LINE for reads.
  525. * NOTE: Although not mentioned in the PCI0646U specs,
  526. * these bits are write only and won't be read
  527. * back as set or not. The PCI0646U2 specs clarify
  528. * this point.
  529. */
  530. (void) pci_write_config_byte(dev, MRDMODE, mrdmode | 0x02);
  531. /* Set reasonable active/recovery/address-setup values. */
  532. (void) pci_write_config_byte(dev, ARTTIM0, 0x40);
  533. (void) pci_write_config_byte(dev, DRWTIM0, 0x3f);
  534. (void) pci_write_config_byte(dev, ARTTIM1, 0x40);
  535. (void) pci_write_config_byte(dev, DRWTIM1, 0x3f);
  536. #ifdef __i386__
  537. (void) pci_write_config_byte(dev, ARTTIM23, 0x1c);
  538. #else
  539. (void) pci_write_config_byte(dev, ARTTIM23, 0x5c);
  540. #endif
  541. (void) pci_write_config_byte(dev, DRWTIM23, 0x3f);
  542. (void) pci_write_config_byte(dev, DRWTIM3, 0x3f);
  543. #ifdef CONFIG_PPC
  544. (void) pci_write_config_byte(dev, UDIDETCR0, 0xf0);
  545. #endif /* CONFIG_PPC */
  546. #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
  547. cmd_devs[n_cmd_devs++] = dev;
  548. if (!cmd64x_proc) {
  549. cmd64x_proc = 1;
  550. ide_pci_create_host_proc("cmd64x", cmd64x_get_info);
  551. }
  552. #endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
  553. return 0;
  554. }
  555. static unsigned int __devinit ata66_cmd64x(ide_hwif_t *hwif)
  556. {
  557. u8 ata66 = 0, mask = (hwif->channel) ? 0x02 : 0x01;
  558. switch(hwif->pci_dev->device) {
  559. case PCI_DEVICE_ID_CMD_643:
  560. case PCI_DEVICE_ID_CMD_646:
  561. return ata66;
  562. default:
  563. break;
  564. }
  565. pci_read_config_byte(hwif->pci_dev, BMIDECSR, &ata66);
  566. return (ata66 & mask) ? 1 : 0;
  567. }
  568. static void __devinit init_hwif_cmd64x(ide_hwif_t *hwif)
  569. {
  570. struct pci_dev *dev = hwif->pci_dev;
  571. unsigned int class_rev;
  572. hwif->autodma = 0;
  573. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  574. class_rev &= 0xff;
  575. hwif->tuneproc = &cmd64x_tune_drive;
  576. hwif->speedproc = &cmd64x_tune_chipset;
  577. hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
  578. if (!hwif->dma_base)
  579. return;
  580. hwif->atapi_dma = 1;
  581. hwif->ultra_mask = 0x3f;
  582. hwif->mwdma_mask = 0x07;
  583. hwif->swdma_mask = 0x07;
  584. if (dev->device == PCI_DEVICE_ID_CMD_643)
  585. hwif->ultra_mask = 0x80;
  586. if (dev->device == PCI_DEVICE_ID_CMD_646)
  587. hwif->ultra_mask = (class_rev > 0x04) ? 0x07 : 0x80;
  588. if (dev->device == PCI_DEVICE_ID_CMD_648)
  589. hwif->ultra_mask = 0x1f;
  590. hwif->ide_dma_check = &cmd64x_config_drive_for_dma;
  591. if (!(hwif->udma_four))
  592. hwif->udma_four = ata66_cmd64x(hwif);
  593. if (dev->device == PCI_DEVICE_ID_CMD_646) {
  594. hwif->chipset = ide_cmd646;
  595. if (class_rev == 0x01) {
  596. hwif->ide_dma_end = &cmd646_1_ide_dma_end;
  597. } else {
  598. hwif->ide_dma_end = &cmd64x_ide_dma_end;
  599. hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
  600. }
  601. } else {
  602. hwif->ide_dma_end = &cmd64x_ide_dma_end;
  603. hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
  604. }
  605. if (!noautodma)
  606. hwif->autodma = 1;
  607. hwif->drives[0].autodma = hwif->autodma;
  608. hwif->drives[1].autodma = hwif->autodma;
  609. }
  610. static ide_pci_device_t cmd64x_chipsets[] __devinitdata = {
  611. { /* 0 */
  612. .name = "CMD643",
  613. .init_chipset = init_chipset_cmd64x,
  614. .init_hwif = init_hwif_cmd64x,
  615. .channels = 2,
  616. .autodma = AUTODMA,
  617. .bootable = ON_BOARD,
  618. },{ /* 1 */
  619. .name = "CMD646",
  620. .init_chipset = init_chipset_cmd64x,
  621. .init_hwif = init_hwif_cmd64x,
  622. .channels = 2,
  623. .autodma = AUTODMA,
  624. .enablebits = {{0x00,0x00,0x00}, {0x51,0x80,0x80}},
  625. .bootable = ON_BOARD,
  626. },{ /* 2 */
  627. .name = "CMD648",
  628. .init_chipset = init_chipset_cmd64x,
  629. .init_hwif = init_hwif_cmd64x,
  630. .channels = 2,
  631. .autodma = AUTODMA,
  632. .bootable = ON_BOARD,
  633. },{ /* 3 */
  634. .name = "CMD649",
  635. .init_chipset = init_chipset_cmd64x,
  636. .init_hwif = init_hwif_cmd64x,
  637. .channels = 2,
  638. .autodma = AUTODMA,
  639. .bootable = ON_BOARD,
  640. }
  641. };
  642. static int __devinit cmd64x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  643. {
  644. return ide_setup_pci_device(dev, &cmd64x_chipsets[id->driver_data]);
  645. }
  646. static struct pci_device_id cmd64x_pci_tbl[] = {
  647. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  648. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  649. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  650. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
  651. { 0, },
  652. };
  653. MODULE_DEVICE_TABLE(pci, cmd64x_pci_tbl);
  654. static struct pci_driver driver = {
  655. .name = "CMD64x_IDE",
  656. .id_table = cmd64x_pci_tbl,
  657. .probe = cmd64x_init_one,
  658. };
  659. static int __init cmd64x_ide_init(void)
  660. {
  661. return ide_pci_register_driver(&driver);
  662. }
  663. module_init(cmd64x_ide_init);
  664. MODULE_AUTHOR("Eddie Dost, David Miller, Andre Hedrick");
  665. MODULE_DESCRIPTION("PCI driver module for CMD64x IDE");
  666. MODULE_LICENSE("GPL");