pdc202xx_old.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2006-2007 MontaVista Software, Inc.
  4. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  5. *
  6. * Portions Copyright (C) 1999 Promise Technology, Inc.
  7. * Author: Frank Tiernan (frankt@promise.com)
  8. * Released under terms of General Public License
  9. */
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/hdreg.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/ide.h>
  19. #include <asm/io.h>
  20. #define DRV_NAME "pdc202xx_old"
  21. #define PDC202XX_DEBUG_DRIVE_INFO 0
  22. static const char *pdc_quirk_drives[] = {
  23. "QUANTUM FIREBALLlct08 08",
  24. "QUANTUM FIREBALLP KA6.4",
  25. "QUANTUM FIREBALLP KA9.1",
  26. "QUANTUM FIREBALLP LM20.4",
  27. "QUANTUM FIREBALLP KX13.6",
  28. "QUANTUM FIREBALLP KX20.5",
  29. "QUANTUM FIREBALLP KX27.3",
  30. "QUANTUM FIREBALLP LM20.5",
  31. NULL
  32. };
  33. static void pdc_old_disable_66MHz_clock(ide_hwif_t *);
  34. static void pdc202xx_set_mode(ide_drive_t *drive, const u8 speed)
  35. {
  36. ide_hwif_t *hwif = HWIF(drive);
  37. struct pci_dev *dev = to_pci_dev(hwif->dev);
  38. u8 drive_pci = 0x60 + (drive->dn << 2);
  39. u8 AP = 0, BP = 0, CP = 0;
  40. u8 TA = 0, TB = 0, TC = 0;
  41. #if PDC202XX_DEBUG_DRIVE_INFO
  42. u32 drive_conf = 0;
  43. pci_read_config_dword(dev, drive_pci, &drive_conf);
  44. #endif
  45. /*
  46. * TODO: do this once per channel
  47. */
  48. if (dev->device != PCI_DEVICE_ID_PROMISE_20246)
  49. pdc_old_disable_66MHz_clock(hwif);
  50. pci_read_config_byte(dev, drive_pci, &AP);
  51. pci_read_config_byte(dev, drive_pci + 1, &BP);
  52. pci_read_config_byte(dev, drive_pci + 2, &CP);
  53. switch(speed) {
  54. case XFER_UDMA_5:
  55. case XFER_UDMA_4: TB = 0x20; TC = 0x01; break;
  56. case XFER_UDMA_2: TB = 0x20; TC = 0x01; break;
  57. case XFER_UDMA_3:
  58. case XFER_UDMA_1: TB = 0x40; TC = 0x02; break;
  59. case XFER_UDMA_0:
  60. case XFER_MW_DMA_2: TB = 0x60; TC = 0x03; break;
  61. case XFER_MW_DMA_1: TB = 0x60; TC = 0x04; break;
  62. case XFER_MW_DMA_0: TB = 0xE0; TC = 0x0F; break;
  63. case XFER_PIO_4: TA = 0x01; TB = 0x04; break;
  64. case XFER_PIO_3: TA = 0x02; TB = 0x06; break;
  65. case XFER_PIO_2: TA = 0x03; TB = 0x08; break;
  66. case XFER_PIO_1: TA = 0x05; TB = 0x0C; break;
  67. case XFER_PIO_0:
  68. default: TA = 0x09; TB = 0x13; break;
  69. }
  70. if (speed < XFER_SW_DMA_0) {
  71. /*
  72. * preserve SYNC_INT / ERDDY_EN bits while clearing
  73. * Prefetch_EN / IORDY_EN / PA[3:0] bits of register A
  74. */
  75. AP &= ~0x3f;
  76. if (drive->id->capability & 4)
  77. AP |= 0x20; /* set IORDY_EN bit */
  78. if (drive->media == ide_disk)
  79. AP |= 0x10; /* set Prefetch_EN bit */
  80. /* clear PB[4:0] bits of register B */
  81. BP &= ~0x1f;
  82. pci_write_config_byte(dev, drive_pci, AP | TA);
  83. pci_write_config_byte(dev, drive_pci + 1, BP | TB);
  84. } else {
  85. /* clear MB[2:0] bits of register B */
  86. BP &= ~0xe0;
  87. /* clear MC[3:0] bits of register C */
  88. CP &= ~0x0f;
  89. pci_write_config_byte(dev, drive_pci + 1, BP | TB);
  90. pci_write_config_byte(dev, drive_pci + 2, CP | TC);
  91. }
  92. #if PDC202XX_DEBUG_DRIVE_INFO
  93. printk(KERN_DEBUG "%s: %s drive%d 0x%08x ",
  94. drive->name, ide_xfer_verbose(speed),
  95. drive->dn, drive_conf);
  96. pci_read_config_dword(dev, drive_pci, &drive_conf);
  97. printk("0x%08x\n", drive_conf);
  98. #endif
  99. }
  100. static void pdc202xx_set_pio_mode(ide_drive_t *drive, const u8 pio)
  101. {
  102. pdc202xx_set_mode(drive, XFER_PIO_0 + pio);
  103. }
  104. static u8 pdc2026x_cable_detect(ide_hwif_t *hwif)
  105. {
  106. struct pci_dev *dev = to_pci_dev(hwif->dev);
  107. u16 CIS, mask = hwif->channel ? (1 << 11) : (1 << 10);
  108. pci_read_config_word(dev, 0x50, &CIS);
  109. return (CIS & mask) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  110. }
  111. /*
  112. * Set the control register to use the 66MHz system
  113. * clock for UDMA 3/4/5 mode operation when necessary.
  114. *
  115. * FIXME: this register is shared by both channels, some locking is needed
  116. *
  117. * It may also be possible to leave the 66MHz clock on
  118. * and readjust the timing parameters.
  119. */
  120. static void pdc_old_enable_66MHz_clock(ide_hwif_t *hwif)
  121. {
  122. unsigned long clock_reg = hwif->extra_base + 0x01;
  123. u8 clock = inb(clock_reg);
  124. outb(clock | (hwif->channel ? 0x08 : 0x02), clock_reg);
  125. }
  126. static void pdc_old_disable_66MHz_clock(ide_hwif_t *hwif)
  127. {
  128. unsigned long clock_reg = hwif->extra_base + 0x01;
  129. u8 clock = inb(clock_reg);
  130. outb(clock & ~(hwif->channel ? 0x08 : 0x02), clock_reg);
  131. }
  132. static void pdc202xx_quirkproc(ide_drive_t *drive)
  133. {
  134. const char **list, *model = drive->id->model;
  135. for (list = pdc_quirk_drives; *list != NULL; list++)
  136. if (strstr(model, *list) != NULL) {
  137. drive->quirk_list = 2;
  138. return;
  139. }
  140. drive->quirk_list = 0;
  141. }
  142. static void pdc202xx_dma_start(ide_drive_t *drive)
  143. {
  144. if (drive->current_speed > XFER_UDMA_2)
  145. pdc_old_enable_66MHz_clock(drive->hwif);
  146. if (drive->media != ide_disk || drive->addressing == 1) {
  147. struct request *rq = HWGROUP(drive)->rq;
  148. ide_hwif_t *hwif = HWIF(drive);
  149. unsigned long high_16 = hwif->extra_base - 16;
  150. unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
  151. u32 word_count = 0;
  152. u8 clock = inb(high_16 + 0x11);
  153. outb(clock | (hwif->channel ? 0x08 : 0x02), high_16 + 0x11);
  154. word_count = (rq->nr_sectors << 8);
  155. word_count = (rq_data_dir(rq) == READ) ?
  156. word_count | 0x05000000 :
  157. word_count | 0x06000000;
  158. outl(word_count, atapi_reg);
  159. }
  160. ide_dma_start(drive);
  161. }
  162. static int pdc202xx_dma_end(ide_drive_t *drive)
  163. {
  164. if (drive->media != ide_disk || drive->addressing == 1) {
  165. ide_hwif_t *hwif = HWIF(drive);
  166. unsigned long high_16 = hwif->extra_base - 16;
  167. unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
  168. u8 clock = 0;
  169. outl(0, atapi_reg); /* zero out extra */
  170. clock = inb(high_16 + 0x11);
  171. outb(clock & ~(hwif->channel ? 0x08:0x02), high_16 + 0x11);
  172. }
  173. if (drive->current_speed > XFER_UDMA_2)
  174. pdc_old_disable_66MHz_clock(drive->hwif);
  175. return __ide_dma_end(drive);
  176. }
  177. static int pdc202xx_dma_test_irq(ide_drive_t *drive)
  178. {
  179. ide_hwif_t *hwif = HWIF(drive);
  180. unsigned long high_16 = hwif->extra_base - 16;
  181. u8 dma_stat = inb(hwif->dma_base + ATA_DMA_STATUS);
  182. u8 sc1d = inb(high_16 + 0x001d);
  183. if (hwif->channel) {
  184. /* bit7: Error, bit6: Interrupting, bit5: FIFO Full, bit4: FIFO Empty */
  185. if ((sc1d & 0x50) == 0x50)
  186. goto somebody_else;
  187. else if ((sc1d & 0x40) == 0x40)
  188. return (dma_stat & 4) == 4;
  189. } else {
  190. /* bit3: Error, bit2: Interrupting, bit1: FIFO Full, bit0: FIFO Empty */
  191. if ((sc1d & 0x05) == 0x05)
  192. goto somebody_else;
  193. else if ((sc1d & 0x04) == 0x04)
  194. return (dma_stat & 4) == 4;
  195. }
  196. somebody_else:
  197. return (dma_stat & 4) == 4; /* return 1 if INTR asserted */
  198. }
  199. static void pdc202xx_reset_host (ide_hwif_t *hwif)
  200. {
  201. unsigned long high_16 = hwif->extra_base - 16;
  202. u8 udma_speed_flag = inb(high_16 | 0x001f);
  203. outb(udma_speed_flag | 0x10, high_16 | 0x001f);
  204. mdelay(100);
  205. outb(udma_speed_flag & ~0x10, high_16 | 0x001f);
  206. mdelay(2000); /* 2 seconds ?! */
  207. printk(KERN_WARNING "PDC202XX: %s channel reset.\n",
  208. hwif->channel ? "Secondary" : "Primary");
  209. }
  210. static void pdc202xx_reset (ide_drive_t *drive)
  211. {
  212. ide_hwif_t *hwif = HWIF(drive);
  213. ide_hwif_t *mate = hwif->mate;
  214. pdc202xx_reset_host(hwif);
  215. pdc202xx_reset_host(mate);
  216. ide_set_max_pio(drive);
  217. }
  218. static void pdc202xx_dma_lost_irq(ide_drive_t *drive)
  219. {
  220. pdc202xx_reset(drive);
  221. ide_dma_lost_irq(drive);
  222. }
  223. static void pdc202xx_dma_timeout(ide_drive_t *drive)
  224. {
  225. pdc202xx_reset(drive);
  226. ide_dma_timeout(drive);
  227. }
  228. static unsigned int __devinit init_chipset_pdc202xx(struct pci_dev *dev)
  229. {
  230. unsigned long dmabase = pci_resource_start(dev, 4);
  231. u8 udma_speed_flag = 0, primary_mode = 0, secondary_mode = 0;
  232. if (dmabase == 0)
  233. goto out;
  234. udma_speed_flag = inb(dmabase | 0x1f);
  235. primary_mode = inb(dmabase | 0x1a);
  236. secondary_mode = inb(dmabase | 0x1b);
  237. printk(KERN_INFO "%s: (U)DMA Burst Bit %sABLED " \
  238. "Primary %s Mode " \
  239. "Secondary %s Mode.\n", pci_name(dev),
  240. (udma_speed_flag & 1) ? "EN" : "DIS",
  241. (primary_mode & 1) ? "MASTER" : "PCI",
  242. (secondary_mode & 1) ? "MASTER" : "PCI" );
  243. if (!(udma_speed_flag & 1)) {
  244. printk(KERN_INFO "%s: FORCING BURST BIT 0x%02x->0x%02x ",
  245. pci_name(dev), udma_speed_flag,
  246. (udma_speed_flag|1));
  247. outb(udma_speed_flag | 1, dmabase | 0x1f);
  248. printk("%sACTIVE\n", (inb(dmabase | 0x1f) & 1) ? "" : "IN");
  249. }
  250. out:
  251. return dev->irq;
  252. }
  253. static void __devinit pdc202ata4_fixup_irq(struct pci_dev *dev,
  254. const char *name)
  255. {
  256. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE) {
  257. u8 irq = 0, irq2 = 0;
  258. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  259. /* 0xbc */
  260. pci_read_config_byte(dev, (PCI_INTERRUPT_LINE)|0x80, &irq2);
  261. if (irq != irq2) {
  262. pci_write_config_byte(dev,
  263. (PCI_INTERRUPT_LINE)|0x80, irq); /* 0xbc */
  264. printk(KERN_INFO "%s %s: PCI config space interrupt "
  265. "mirror fixed\n", name, pci_name(dev));
  266. }
  267. }
  268. }
  269. #define IDE_HFLAGS_PDC202XX \
  270. (IDE_HFLAG_ERROR_STOPS_FIFO | \
  271. IDE_HFLAG_OFF_BOARD)
  272. static const struct ide_port_ops pdc20246_port_ops = {
  273. .set_pio_mode = pdc202xx_set_pio_mode,
  274. .set_dma_mode = pdc202xx_set_mode,
  275. .quirkproc = pdc202xx_quirkproc,
  276. };
  277. static const struct ide_port_ops pdc2026x_port_ops = {
  278. .set_pio_mode = pdc202xx_set_pio_mode,
  279. .set_dma_mode = pdc202xx_set_mode,
  280. .quirkproc = pdc202xx_quirkproc,
  281. .resetproc = pdc202xx_reset,
  282. .cable_detect = pdc2026x_cable_detect,
  283. };
  284. static const struct ide_dma_ops pdc20246_dma_ops = {
  285. .dma_host_set = ide_dma_host_set,
  286. .dma_setup = ide_dma_setup,
  287. .dma_exec_cmd = ide_dma_exec_cmd,
  288. .dma_start = ide_dma_start,
  289. .dma_end = __ide_dma_end,
  290. .dma_test_irq = pdc202xx_dma_test_irq,
  291. .dma_lost_irq = pdc202xx_dma_lost_irq,
  292. .dma_timeout = pdc202xx_dma_timeout,
  293. };
  294. static const struct ide_dma_ops pdc2026x_dma_ops = {
  295. .dma_host_set = ide_dma_host_set,
  296. .dma_setup = ide_dma_setup,
  297. .dma_exec_cmd = ide_dma_exec_cmd,
  298. .dma_start = pdc202xx_dma_start,
  299. .dma_end = pdc202xx_dma_end,
  300. .dma_test_irq = pdc202xx_dma_test_irq,
  301. .dma_lost_irq = pdc202xx_dma_lost_irq,
  302. .dma_timeout = pdc202xx_dma_timeout,
  303. };
  304. #define DECLARE_PDC2026X_DEV(udma, extra_flags) \
  305. { \
  306. .name = DRV_NAME, \
  307. .init_chipset = init_chipset_pdc202xx, \
  308. .port_ops = &pdc2026x_port_ops, \
  309. .dma_ops = &pdc2026x_dma_ops, \
  310. .host_flags = IDE_HFLAGS_PDC202XX | extra_flags, \
  311. .pio_mask = ATA_PIO4, \
  312. .mwdma_mask = ATA_MWDMA2, \
  313. .udma_mask = udma, \
  314. }
  315. static const struct ide_port_info pdc202xx_chipsets[] __devinitdata = {
  316. { /* 0: PDC20246 */
  317. .name = DRV_NAME,
  318. .init_chipset = init_chipset_pdc202xx,
  319. .port_ops = &pdc20246_port_ops,
  320. .dma_ops = &pdc20246_dma_ops,
  321. .host_flags = IDE_HFLAGS_PDC202XX,
  322. .pio_mask = ATA_PIO4,
  323. .mwdma_mask = ATA_MWDMA2,
  324. .udma_mask = ATA_UDMA2,
  325. },
  326. /* 1: PDC2026{2,3} */
  327. DECLARE_PDC2026X_DEV(ATA_UDMA4, 0),
  328. /* 2: PDC2026{5,7} */
  329. DECLARE_PDC2026X_DEV(ATA_UDMA5, IDE_HFLAG_RQSIZE_256),
  330. };
  331. /**
  332. * pdc202xx_init_one - called when a PDC202xx is found
  333. * @dev: the pdc202xx device
  334. * @id: the matching pci id
  335. *
  336. * Called when the PCI registration layer (or the IDE initialization)
  337. * finds a device matching our IDE device tables.
  338. */
  339. static int __devinit pdc202xx_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  340. {
  341. const struct ide_port_info *d;
  342. u8 idx = id->driver_data;
  343. d = &pdc202xx_chipsets[idx];
  344. if (idx < 2)
  345. pdc202ata4_fixup_irq(dev, d->name);
  346. if (dev->vendor == PCI_DEVICE_ID_PROMISE_20265) {
  347. struct pci_dev *bridge = dev->bus->self;
  348. if (bridge &&
  349. bridge->vendor == PCI_VENDOR_ID_INTEL &&
  350. (bridge->device == PCI_DEVICE_ID_INTEL_I960 ||
  351. bridge->device == PCI_DEVICE_ID_INTEL_I960RM)) {
  352. printk(KERN_INFO DRV_NAME " %s: skipping Promise "
  353. "PDC20265 attached to I2O RAID controller\n",
  354. pci_name(dev));
  355. return -ENODEV;
  356. }
  357. }
  358. return ide_pci_init_one(dev, d, NULL);
  359. }
  360. static const struct pci_device_id pdc202xx_pci_tbl[] = {
  361. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20246), 0 },
  362. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20262), 1 },
  363. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20263), 1 },
  364. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20265), 2 },
  365. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20267), 2 },
  366. { 0, },
  367. };
  368. MODULE_DEVICE_TABLE(pci, pdc202xx_pci_tbl);
  369. static struct pci_driver driver = {
  370. .name = "Promise_Old_IDE",
  371. .id_table = pdc202xx_pci_tbl,
  372. .probe = pdc202xx_init_one,
  373. .remove = ide_pci_remove,
  374. };
  375. static int __init pdc202xx_ide_init(void)
  376. {
  377. return ide_pci_register_driver(&driver);
  378. }
  379. static void __exit pdc202xx_ide_exit(void)
  380. {
  381. pci_unregister_driver(&driver);
  382. }
  383. module_init(pdc202xx_ide_init);
  384. module_exit(pdc202xx_ide_exit);
  385. MODULE_AUTHOR("Andre Hedrick, Frank Tiernan");
  386. MODULE_DESCRIPTION("PCI driver module for older Promise IDE");
  387. MODULE_LICENSE("GPL");