cmd64x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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/config.h>
  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. /*
  171. * Registers and masks for easy access by drive index:
  172. */
  173. #if 0
  174. static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
  175. static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
  176. #endif
  177. /*
  178. * This routine writes the prepared setup/active/recovery counts
  179. * for a drive into the cmd646 chipset registers to active them.
  180. */
  181. static void program_drive_counts (ide_drive_t *drive, int setup_count, int active_count, int recovery_count)
  182. {
  183. unsigned long flags;
  184. struct pci_dev *dev = HWIF(drive)->pci_dev;
  185. ide_drive_t *drives = HWIF(drive)->drives;
  186. u8 temp_b;
  187. static const u8 setup_counts[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
  188. static const u8 recovery_counts[] =
  189. {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
  190. static const u8 arttim_regs[2][2] = {
  191. { ARTTIM0, ARTTIM1 },
  192. { ARTTIM23, ARTTIM23 }
  193. };
  194. static const u8 drwtim_regs[2][2] = {
  195. { DRWTIM0, DRWTIM1 },
  196. { DRWTIM2, DRWTIM3 }
  197. };
  198. int channel = (int) HWIF(drive)->channel;
  199. int slave = (drives != drive); /* Is this really the best way to determine this?? */
  200. cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n",
  201. setup_count, active_count, recovery_count, drive->present);
  202. /*
  203. * Set up address setup count registers.
  204. * Primary interface has individual count/timing registers for
  205. * each drive. Secondary interface has one common set of registers,
  206. * for address setup so we merge these timings, using the slowest
  207. * value.
  208. */
  209. if (channel) {
  210. drive->drive_data = setup_count;
  211. setup_count = max(drives[0].drive_data,
  212. drives[1].drive_data);
  213. cmdprintk("Secondary interface, setup_count = %d\n",
  214. setup_count);
  215. }
  216. /*
  217. * Convert values to internal chipset representation
  218. */
  219. setup_count = (setup_count > 5) ? 0xc0 : (int) setup_counts[setup_count];
  220. active_count &= 0xf; /* Remember, max value is 16 */
  221. recovery_count = (int) recovery_counts[recovery_count];
  222. cmdprintk("Final values = %d,%d,%d\n",
  223. setup_count, active_count, recovery_count);
  224. /*
  225. * Now that everything is ready, program the new timings
  226. */
  227. local_irq_save(flags);
  228. /*
  229. * Program the address_setup clocks into ARTTIM reg,
  230. * and then the active/recovery counts into the DRWTIM reg
  231. */
  232. (void) pci_read_config_byte(dev, arttim_regs[channel][slave], &temp_b);
  233. (void) pci_write_config_byte(dev, arttim_regs[channel][slave],
  234. ((u8) setup_count) | (temp_b & 0x3f));
  235. (void) pci_write_config_byte(dev, drwtim_regs[channel][slave],
  236. (u8) ((active_count << 4) | recovery_count));
  237. cmdprintk ("Write %x to %x\n",
  238. ((u8) setup_count) | (temp_b & 0x3f),
  239. arttim_regs[channel][slave]);
  240. cmdprintk ("Write %x to %x\n",
  241. (u8) ((active_count << 4) | recovery_count),
  242. drwtim_regs[channel][slave]);
  243. local_irq_restore(flags);
  244. }
  245. /*
  246. * Attempts to set the interface PIO mode.
  247. * The preferred method of selecting PIO modes (e.g. mode 4) is
  248. * "echo 'piomode:4' > /proc/ide/hdx/settings". Special cases are
  249. * 8: prefetch off, 9: prefetch on, 255: auto-select best mode.
  250. * Called with 255 at boot time.
  251. */
  252. static void cmd64x_tuneproc (ide_drive_t *drive, u8 mode_wanted)
  253. {
  254. int setup_time, active_time, recovery_time;
  255. int clock_time, pio_mode, cycle_time;
  256. u8 recovery_count2, cycle_count;
  257. int setup_count, active_count, recovery_count;
  258. int bus_speed = system_bus_clock();
  259. /*byte b;*/
  260. ide_pio_data_t d;
  261. switch (mode_wanted) {
  262. case 8: /* set prefetch off */
  263. case 9: /* set prefetch on */
  264. mode_wanted &= 1;
  265. /*set_prefetch_mode(index, mode_wanted);*/
  266. cmdprintk("%s: %sabled cmd640 prefetch\n",
  267. drive->name, mode_wanted ? "en" : "dis");
  268. return;
  269. }
  270. mode_wanted = ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
  271. pio_mode = d.pio_mode;
  272. cycle_time = d.cycle_time;
  273. /*
  274. * I copied all this complicated stuff from cmd640.c and made a few
  275. * minor changes. For now I am just going to pray that it is correct.
  276. */
  277. if (pio_mode > 5)
  278. pio_mode = 5;
  279. setup_time = ide_pio_timings[pio_mode].setup_time;
  280. active_time = ide_pio_timings[pio_mode].active_time;
  281. recovery_time = cycle_time - (setup_time + active_time);
  282. clock_time = 1000 / bus_speed;
  283. cycle_count = (cycle_time + clock_time - 1) / clock_time;
  284. setup_count = (setup_time + clock_time - 1) / clock_time;
  285. active_count = (active_time + clock_time - 1) / clock_time;
  286. recovery_count = (recovery_time + clock_time - 1) / clock_time;
  287. recovery_count2 = cycle_count - (setup_count + active_count);
  288. if (recovery_count2 > recovery_count)
  289. recovery_count = recovery_count2;
  290. if (recovery_count > 16) {
  291. active_count += recovery_count - 16;
  292. recovery_count = 16;
  293. }
  294. if (active_count > 16)
  295. active_count = 16; /* maximum allowed by cmd646 */
  296. /*
  297. * In a perfect world, we might set the drive pio mode here
  298. * (using WIN_SETFEATURE) before continuing.
  299. *
  300. * But we do not, because:
  301. * 1) this is the wrong place to do it
  302. * (proper is do_special() in ide.c)
  303. * 2) in practice this is rarely, if ever, necessary
  304. */
  305. program_drive_counts (drive, setup_count, active_count, recovery_count);
  306. cmdprintk("%s: selected cmd646 PIO mode%d : %d (%dns)%s, "
  307. "clocks=%d/%d/%d\n",
  308. drive->name, pio_mode, mode_wanted, cycle_time,
  309. d.overridden ? " (overriding vendor mode)" : "",
  310. setup_count, active_count, recovery_count);
  311. }
  312. static u8 cmd64x_ratemask (ide_drive_t *drive)
  313. {
  314. struct pci_dev *dev = HWIF(drive)->pci_dev;
  315. u8 mode = 0;
  316. switch(dev->device) {
  317. case PCI_DEVICE_ID_CMD_649:
  318. mode = 3;
  319. break;
  320. case PCI_DEVICE_ID_CMD_648:
  321. mode = 2;
  322. break;
  323. case PCI_DEVICE_ID_CMD_643:
  324. return 0;
  325. case PCI_DEVICE_ID_CMD_646:
  326. {
  327. unsigned int class_rev = 0;
  328. pci_read_config_dword(dev,
  329. PCI_CLASS_REVISION, &class_rev);
  330. class_rev &= 0xff;
  331. /*
  332. * UltraDMA only supported on PCI646U and PCI646U2, which
  333. * correspond to revisions 0x03, 0x05 and 0x07 respectively.
  334. * Actually, although the CMD tech support people won't
  335. * tell me the details, the 0x03 revision cannot support
  336. * UDMA correctly without hardware modifications, and even
  337. * then it only works with Quantum disks due to some
  338. * hold time assumptions in the 646U part which are fixed
  339. * in the 646U2.
  340. *
  341. * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
  342. */
  343. switch(class_rev) {
  344. case 0x07:
  345. case 0x05:
  346. return 1;
  347. case 0x03:
  348. case 0x01:
  349. default:
  350. return 0;
  351. }
  352. }
  353. }
  354. if (!eighty_ninty_three(drive))
  355. mode = min(mode, (u8)1);
  356. return mode;
  357. }
  358. static void config_cmd64x_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
  359. {
  360. u8 speed = 0x00;
  361. u8 set_pio = ide_get_best_pio_mode(drive, 4, 5, NULL);
  362. cmd64x_tuneproc(drive, set_pio);
  363. speed = XFER_PIO_0 + set_pio;
  364. if (set_speed)
  365. (void) ide_config_drive_speed(drive, speed);
  366. }
  367. static void config_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
  368. {
  369. config_cmd64x_chipset_for_pio(drive, set_speed);
  370. }
  371. static int cmd64x_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  372. {
  373. ide_hwif_t *hwif = HWIF(drive);
  374. struct pci_dev *dev = hwif->pci_dev;
  375. u8 unit = (drive->select.b.unit & 0x01);
  376. u8 regU = 0, pciU = (hwif->channel) ? UDIDETCR1 : UDIDETCR0;
  377. u8 regD = 0, pciD = (hwif->channel) ? BMIDESR1 : BMIDESR0;
  378. u8 speed = ide_rate_filter(cmd64x_ratemask(drive), xferspeed);
  379. if (speed > XFER_PIO_4) {
  380. (void) pci_read_config_byte(dev, pciD, &regD);
  381. (void) pci_read_config_byte(dev, pciU, &regU);
  382. regD &= ~(unit ? 0x40 : 0x20);
  383. regU &= ~(unit ? 0xCA : 0x35);
  384. (void) pci_write_config_byte(dev, pciD, regD);
  385. (void) pci_write_config_byte(dev, pciU, regU);
  386. (void) pci_read_config_byte(dev, pciD, &regD);
  387. (void) pci_read_config_byte(dev, pciU, &regU);
  388. }
  389. switch(speed) {
  390. case XFER_UDMA_5: regU |= (unit ? 0x0A : 0x05); break;
  391. case XFER_UDMA_4: regU |= (unit ? 0x4A : 0x15); break;
  392. case XFER_UDMA_3: regU |= (unit ? 0x8A : 0x25); break;
  393. case XFER_UDMA_2: regU |= (unit ? 0x42 : 0x11); break;
  394. case XFER_UDMA_1: regU |= (unit ? 0x82 : 0x21); break;
  395. case XFER_UDMA_0: regU |= (unit ? 0xC2 : 0x31); break;
  396. case XFER_MW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
  397. case XFER_MW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
  398. case XFER_MW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
  399. case XFER_SW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
  400. case XFER_SW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
  401. case XFER_SW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
  402. case XFER_PIO_4: cmd64x_tuneproc(drive, 4); break;
  403. case XFER_PIO_3: cmd64x_tuneproc(drive, 3); break;
  404. case XFER_PIO_2: cmd64x_tuneproc(drive, 2); break;
  405. case XFER_PIO_1: cmd64x_tuneproc(drive, 1); break;
  406. case XFER_PIO_0: cmd64x_tuneproc(drive, 0); break;
  407. default:
  408. return 1;
  409. }
  410. if (speed > XFER_PIO_4) {
  411. (void) pci_write_config_byte(dev, pciU, regU);
  412. regD |= (unit ? 0x40 : 0x20);
  413. (void) pci_write_config_byte(dev, pciD, regD);
  414. }
  415. return (ide_config_drive_speed(drive, speed));
  416. }
  417. static int config_chipset_for_dma (ide_drive_t *drive)
  418. {
  419. u8 speed = ide_dma_speed(drive, cmd64x_ratemask(drive));
  420. config_chipset_for_pio(drive, !speed);
  421. if (!speed)
  422. return 0;
  423. if(ide_set_xfer_rate(drive, speed))
  424. return 0;
  425. if (!drive->init_speed)
  426. drive->init_speed = speed;
  427. return ide_dma_enable(drive);
  428. }
  429. static int cmd64x_config_drive_for_dma (ide_drive_t *drive)
  430. {
  431. ide_hwif_t *hwif = HWIF(drive);
  432. struct hd_driveid *id = drive->id;
  433. if ((id != NULL) && ((id->capability & 1) != 0) && drive->autodma) {
  434. if (ide_use_dma(drive)) {
  435. if (config_chipset_for_dma(drive))
  436. return hwif->ide_dma_on(drive);
  437. }
  438. goto fast_ata_pio;
  439. } else if ((id->capability & 8) || (id->field_valid & 2)) {
  440. fast_ata_pio:
  441. config_chipset_for_pio(drive, 1);
  442. return hwif->ide_dma_off_quietly(drive);
  443. }
  444. /* IORDY not supported */
  445. return 0;
  446. }
  447. static int cmd64x_alt_dma_status (struct pci_dev *dev)
  448. {
  449. switch(dev->device) {
  450. case PCI_DEVICE_ID_CMD_648:
  451. case PCI_DEVICE_ID_CMD_649:
  452. return 1;
  453. default:
  454. break;
  455. }
  456. return 0;
  457. }
  458. static int cmd64x_ide_dma_end (ide_drive_t *drive)
  459. {
  460. u8 dma_stat = 0, dma_cmd = 0;
  461. ide_hwif_t *hwif = HWIF(drive);
  462. struct pci_dev *dev = hwif->pci_dev;
  463. drive->waiting_for_dma = 0;
  464. /* read DMA command state */
  465. dma_cmd = hwif->INB(hwif->dma_command);
  466. /* stop DMA */
  467. hwif->OUTB((dma_cmd & ~1), hwif->dma_command);
  468. /* get DMA status */
  469. dma_stat = hwif->INB(hwif->dma_status);
  470. /* clear the INTR & ERROR bits */
  471. hwif->OUTB(dma_stat|6, hwif->dma_status);
  472. if (cmd64x_alt_dma_status(dev)) {
  473. u8 dma_intr = 0;
  474. u8 dma_mask = (hwif->channel) ? ARTTIM23_INTR_CH1 :
  475. CFR_INTR_CH0;
  476. u8 dma_reg = (hwif->channel) ? ARTTIM2 : CFR;
  477. (void) pci_read_config_byte(dev, dma_reg, &dma_intr);
  478. /* clear the INTR bit */
  479. (void) pci_write_config_byte(dev, dma_reg, dma_intr|dma_mask);
  480. }
  481. /* purge DMA mappings */
  482. ide_destroy_dmatable(drive);
  483. /* verify good DMA status */
  484. return (dma_stat & 7) != 4;
  485. }
  486. static int cmd64x_ide_dma_test_irq (ide_drive_t *drive)
  487. {
  488. ide_hwif_t *hwif = HWIF(drive);
  489. struct pci_dev *dev = hwif->pci_dev;
  490. u8 dma_alt_stat = 0, mask = (hwif->channel) ? MRDMODE_INTR_CH1 :
  491. MRDMODE_INTR_CH0;
  492. u8 dma_stat = hwif->INB(hwif->dma_status);
  493. (void) pci_read_config_byte(dev, MRDMODE, &dma_alt_stat);
  494. #ifdef DEBUG
  495. printk("%s: dma_stat: 0x%02x dma_alt_stat: "
  496. "0x%02x mask: 0x%02x\n", drive->name,
  497. dma_stat, dma_alt_stat, mask);
  498. #endif
  499. if (!(dma_alt_stat & mask))
  500. return 0;
  501. /* return 1 if INTR asserted */
  502. if ((dma_stat & 4) == 4)
  503. return 1;
  504. return 0;
  505. }
  506. /*
  507. * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
  508. * event order for DMA transfers.
  509. */
  510. static int cmd646_1_ide_dma_end (ide_drive_t *drive)
  511. {
  512. ide_hwif_t *hwif = HWIF(drive);
  513. u8 dma_stat = 0, dma_cmd = 0;
  514. drive->waiting_for_dma = 0;
  515. /* get DMA status */
  516. dma_stat = hwif->INB(hwif->dma_status);
  517. /* read DMA command state */
  518. dma_cmd = hwif->INB(hwif->dma_command);
  519. /* stop DMA */
  520. hwif->OUTB((dma_cmd & ~1), hwif->dma_command);
  521. /* clear the INTR & ERROR bits */
  522. hwif->OUTB(dma_stat|6, hwif->dma_status);
  523. /* and free any DMA resources */
  524. ide_destroy_dmatable(drive);
  525. /* verify good DMA status */
  526. return (dma_stat & 7) != 4;
  527. }
  528. static unsigned int __devinit init_chipset_cmd64x(struct pci_dev *dev, const char *name)
  529. {
  530. u32 class_rev = 0;
  531. u8 mrdmode = 0;
  532. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  533. class_rev &= 0xff;
  534. #ifdef __i386__
  535. if (dev->resource[PCI_ROM_RESOURCE].start) {
  536. pci_write_config_dword(dev, PCI_ROM_ADDRESS, dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
  537. printk(KERN_INFO "%s: ROM enabled at 0x%08lx\n", name, dev->resource[PCI_ROM_RESOURCE].start);
  538. }
  539. #endif
  540. switch(dev->device) {
  541. case PCI_DEVICE_ID_CMD_643:
  542. break;
  543. case PCI_DEVICE_ID_CMD_646:
  544. printk(KERN_INFO "%s: chipset revision 0x%02X, ", name, class_rev);
  545. switch(class_rev) {
  546. case 0x07:
  547. case 0x05:
  548. printk("UltraDMA Capable");
  549. break;
  550. case 0x03:
  551. printk("MultiWord DMA Force Limited");
  552. break;
  553. case 0x01:
  554. default:
  555. printk("MultiWord DMA Limited, IRQ workaround enabled");
  556. break;
  557. }
  558. printk("\n");
  559. break;
  560. case PCI_DEVICE_ID_CMD_648:
  561. case PCI_DEVICE_ID_CMD_649:
  562. break;
  563. default:
  564. break;
  565. }
  566. /* Set a good latency timer and cache line size value. */
  567. (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  568. /* FIXME: pci_set_master() to ensure a good latency timer value */
  569. /* Setup interrupts. */
  570. (void) pci_read_config_byte(dev, MRDMODE, &mrdmode);
  571. mrdmode &= ~(0x30);
  572. (void) pci_write_config_byte(dev, MRDMODE, mrdmode);
  573. /* Use MEMORY READ LINE for reads.
  574. * NOTE: Although not mentioned in the PCI0646U specs,
  575. * these bits are write only and won't be read
  576. * back as set or not. The PCI0646U2 specs clarify
  577. * this point.
  578. */
  579. (void) pci_write_config_byte(dev, MRDMODE, mrdmode | 0x02);
  580. /* Set reasonable active/recovery/address-setup values. */
  581. (void) pci_write_config_byte(dev, ARTTIM0, 0x40);
  582. (void) pci_write_config_byte(dev, DRWTIM0, 0x3f);
  583. (void) pci_write_config_byte(dev, ARTTIM1, 0x40);
  584. (void) pci_write_config_byte(dev, DRWTIM1, 0x3f);
  585. #ifdef __i386__
  586. (void) pci_write_config_byte(dev, ARTTIM23, 0x1c);
  587. #else
  588. (void) pci_write_config_byte(dev, ARTTIM23, 0x5c);
  589. #endif
  590. (void) pci_write_config_byte(dev, DRWTIM23, 0x3f);
  591. (void) pci_write_config_byte(dev, DRWTIM3, 0x3f);
  592. #ifdef CONFIG_PPC
  593. (void) pci_write_config_byte(dev, UDIDETCR0, 0xf0);
  594. #endif /* CONFIG_PPC */
  595. #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
  596. cmd_devs[n_cmd_devs++] = dev;
  597. if (!cmd64x_proc) {
  598. cmd64x_proc = 1;
  599. ide_pci_create_host_proc("cmd64x", cmd64x_get_info);
  600. }
  601. #endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
  602. return 0;
  603. }
  604. static unsigned int __devinit ata66_cmd64x(ide_hwif_t *hwif)
  605. {
  606. u8 ata66 = 0, mask = (hwif->channel) ? 0x02 : 0x01;
  607. switch(hwif->pci_dev->device) {
  608. case PCI_DEVICE_ID_CMD_643:
  609. case PCI_DEVICE_ID_CMD_646:
  610. return ata66;
  611. default:
  612. break;
  613. }
  614. pci_read_config_byte(hwif->pci_dev, BMIDECSR, &ata66);
  615. return (ata66 & mask) ? 1 : 0;
  616. }
  617. static void __devinit init_hwif_cmd64x(ide_hwif_t *hwif)
  618. {
  619. struct pci_dev *dev = hwif->pci_dev;
  620. unsigned int class_rev;
  621. hwif->autodma = 0;
  622. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  623. class_rev &= 0xff;
  624. hwif->tuneproc = &cmd64x_tuneproc;
  625. hwif->speedproc = &cmd64x_tune_chipset;
  626. if (!hwif->dma_base) {
  627. hwif->drives[0].autotune = 1;
  628. hwif->drives[1].autotune = 1;
  629. return;
  630. }
  631. hwif->atapi_dma = 1;
  632. hwif->ultra_mask = 0x3f;
  633. hwif->mwdma_mask = 0x07;
  634. hwif->swdma_mask = 0x07;
  635. if (dev->device == PCI_DEVICE_ID_CMD_643)
  636. hwif->ultra_mask = 0x80;
  637. if (dev->device == PCI_DEVICE_ID_CMD_646)
  638. hwif->ultra_mask = (class_rev > 0x04) ? 0x07 : 0x80;
  639. if (dev->device == PCI_DEVICE_ID_CMD_648)
  640. hwif->ultra_mask = 0x1f;
  641. hwif->ide_dma_check = &cmd64x_config_drive_for_dma;
  642. if (!(hwif->udma_four))
  643. hwif->udma_four = ata66_cmd64x(hwif);
  644. if (dev->device == PCI_DEVICE_ID_CMD_646) {
  645. hwif->chipset = ide_cmd646;
  646. if (class_rev == 0x01) {
  647. hwif->ide_dma_end = &cmd646_1_ide_dma_end;
  648. } else {
  649. hwif->ide_dma_end = &cmd64x_ide_dma_end;
  650. hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
  651. }
  652. } else {
  653. hwif->ide_dma_end = &cmd64x_ide_dma_end;
  654. hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
  655. }
  656. if (!noautodma)
  657. hwif->autodma = 1;
  658. hwif->drives[0].autodma = hwif->autodma;
  659. hwif->drives[1].autodma = hwif->autodma;
  660. }
  661. static ide_pci_device_t cmd64x_chipsets[] __devinitdata = {
  662. { /* 0 */
  663. .name = "CMD643",
  664. .init_chipset = init_chipset_cmd64x,
  665. .init_hwif = init_hwif_cmd64x,
  666. .channels = 2,
  667. .autodma = AUTODMA,
  668. .bootable = ON_BOARD,
  669. },{ /* 1 */
  670. .name = "CMD646",
  671. .init_chipset = init_chipset_cmd64x,
  672. .init_hwif = init_hwif_cmd64x,
  673. .channels = 2,
  674. .autodma = AUTODMA,
  675. .enablebits = {{0x00,0x00,0x00}, {0x51,0x80,0x80}},
  676. .bootable = ON_BOARD,
  677. },{ /* 2 */
  678. .name = "CMD648",
  679. .init_chipset = init_chipset_cmd64x,
  680. .init_hwif = init_hwif_cmd64x,
  681. .channels = 2,
  682. .autodma = AUTODMA,
  683. .bootable = ON_BOARD,
  684. },{ /* 3 */
  685. .name = "CMD649",
  686. .init_chipset = init_chipset_cmd64x,
  687. .init_hwif = init_hwif_cmd64x,
  688. .channels = 2,
  689. .autodma = AUTODMA,
  690. .bootable = ON_BOARD,
  691. }
  692. };
  693. static int __devinit cmd64x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  694. {
  695. return ide_setup_pci_device(dev, &cmd64x_chipsets[id->driver_data]);
  696. }
  697. static struct pci_device_id cmd64x_pci_tbl[] = {
  698. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  699. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  700. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  701. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
  702. { 0, },
  703. };
  704. MODULE_DEVICE_TABLE(pci, cmd64x_pci_tbl);
  705. static struct pci_driver driver = {
  706. .name = "CMD64x_IDE",
  707. .id_table = cmd64x_pci_tbl,
  708. .probe = cmd64x_init_one,
  709. };
  710. static int cmd64x_ide_init(void)
  711. {
  712. return ide_pci_register_driver(&driver);
  713. }
  714. module_init(cmd64x_ide_init);
  715. MODULE_AUTHOR("Eddie Dost, David Miller, Andre Hedrick");
  716. MODULE_DESCRIPTION("PCI driver module for CMD64x IDE");
  717. MODULE_LICENSE("GPL");