cmd64x.c 23 KB

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