pdc202xx_old.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2006-2007, 2009 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/pci.h>
  16. #include <linux/init.h>
  17. #include <linux/ide.h>
  18. #include <asm/io.h>
  19. #define DRV_NAME "pdc202xx_old"
  20. #define PDC202XX_DEBUG_DRIVE_INFO 0
  21. static void pdc_old_disable_66MHz_clock(ide_hwif_t *);
  22. static void pdc202xx_set_mode(ide_drive_t *drive, const u8 speed)
  23. {
  24. ide_hwif_t *hwif = drive->hwif;
  25. struct pci_dev *dev = to_pci_dev(hwif->dev);
  26. u8 drive_pci = 0x60 + (drive->dn << 2);
  27. u8 AP = 0, BP = 0, CP = 0;
  28. u8 TA = 0, TB = 0, TC = 0;
  29. #if PDC202XX_DEBUG_DRIVE_INFO
  30. u32 drive_conf = 0;
  31. pci_read_config_dword(dev, drive_pci, &drive_conf);
  32. #endif
  33. /*
  34. * TODO: do this once per channel
  35. */
  36. if (dev->device != PCI_DEVICE_ID_PROMISE_20246)
  37. pdc_old_disable_66MHz_clock(hwif);
  38. pci_read_config_byte(dev, drive_pci, &AP);
  39. pci_read_config_byte(dev, drive_pci + 1, &BP);
  40. pci_read_config_byte(dev, drive_pci + 2, &CP);
  41. switch(speed) {
  42. case XFER_UDMA_5:
  43. case XFER_UDMA_4: TB = 0x20; TC = 0x01; break;
  44. case XFER_UDMA_2: TB = 0x20; TC = 0x01; break;
  45. case XFER_UDMA_3:
  46. case XFER_UDMA_1: TB = 0x40; TC = 0x02; break;
  47. case XFER_UDMA_0:
  48. case XFER_MW_DMA_2: TB = 0x60; TC = 0x03; break;
  49. case XFER_MW_DMA_1: TB = 0x60; TC = 0x04; break;
  50. case XFER_MW_DMA_0: TB = 0xE0; TC = 0x0F; break;
  51. case XFER_PIO_4: TA = 0x01; TB = 0x04; break;
  52. case XFER_PIO_3: TA = 0x02; TB = 0x06; break;
  53. case XFER_PIO_2: TA = 0x03; TB = 0x08; break;
  54. case XFER_PIO_1: TA = 0x05; TB = 0x0C; break;
  55. case XFER_PIO_0:
  56. default: TA = 0x09; TB = 0x13; break;
  57. }
  58. if (speed < XFER_SW_DMA_0) {
  59. /*
  60. * preserve SYNC_INT / ERDDY_EN bits while clearing
  61. * Prefetch_EN / IORDY_EN / PA[3:0] bits of register A
  62. */
  63. AP &= ~0x3f;
  64. if (ide_pio_need_iordy(drive, speed - XFER_PIO_0))
  65. AP |= 0x20; /* set IORDY_EN bit */
  66. if (drive->media == ide_disk)
  67. AP |= 0x10; /* set Prefetch_EN bit */
  68. /* clear PB[4:0] bits of register B */
  69. BP &= ~0x1f;
  70. pci_write_config_byte(dev, drive_pci, AP | TA);
  71. pci_write_config_byte(dev, drive_pci + 1, BP | TB);
  72. } else {
  73. /* clear MB[2:0] bits of register B */
  74. BP &= ~0xe0;
  75. /* clear MC[3:0] bits of register C */
  76. CP &= ~0x0f;
  77. pci_write_config_byte(dev, drive_pci + 1, BP | TB);
  78. pci_write_config_byte(dev, drive_pci + 2, CP | TC);
  79. }
  80. #if PDC202XX_DEBUG_DRIVE_INFO
  81. printk(KERN_DEBUG "%s: %s drive%d 0x%08x ",
  82. drive->name, ide_xfer_verbose(speed),
  83. drive->dn, drive_conf);
  84. pci_read_config_dword(dev, drive_pci, &drive_conf);
  85. printk("0x%08x\n", drive_conf);
  86. #endif
  87. }
  88. static void pdc202xx_set_pio_mode(ide_drive_t *drive, const u8 pio)
  89. {
  90. pdc202xx_set_mode(drive, XFER_PIO_0 + pio);
  91. }
  92. static int pdc202xx_test_irq(ide_hwif_t *hwif)
  93. {
  94. struct pci_dev *dev = to_pci_dev(hwif->dev);
  95. unsigned long high_16 = pci_resource_start(dev, 4);
  96. u8 sc1d = inb(high_16 + 0x1d);
  97. if (hwif->channel) {
  98. /*
  99. * bit 7: error, bit 6: interrupting,
  100. * bit 5: FIFO full, bit 4: FIFO empty
  101. */
  102. return ((sc1d & 0x50) == 0x40) ? 1 : 0;
  103. } else {
  104. /*
  105. * bit 3: error, bit 2: interrupting,
  106. * bit 1: FIFO full, bit 0: FIFO empty
  107. */
  108. return ((sc1d & 0x05) == 0x04) ? 1 : 0;
  109. }
  110. }
  111. static u8 pdc2026x_cable_detect(ide_hwif_t *hwif)
  112. {
  113. struct pci_dev *dev = to_pci_dev(hwif->dev);
  114. u16 CIS, mask = hwif->channel ? (1 << 11) : (1 << 10);
  115. pci_read_config_word(dev, 0x50, &CIS);
  116. return (CIS & mask) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  117. }
  118. /*
  119. * Set the control register to use the 66MHz system
  120. * clock for UDMA 3/4/5 mode operation when necessary.
  121. *
  122. * FIXME: this register is shared by both channels, some locking is needed
  123. *
  124. * It may also be possible to leave the 66MHz clock on
  125. * and readjust the timing parameters.
  126. */
  127. static void pdc_old_enable_66MHz_clock(ide_hwif_t *hwif)
  128. {
  129. unsigned long clock_reg = hwif->extra_base + 0x01;
  130. u8 clock = inb(clock_reg);
  131. outb(clock | (hwif->channel ? 0x08 : 0x02), clock_reg);
  132. }
  133. static void pdc_old_disable_66MHz_clock(ide_hwif_t *hwif)
  134. {
  135. unsigned long clock_reg = hwif->extra_base + 0x01;
  136. u8 clock = inb(clock_reg);
  137. outb(clock & ~(hwif->channel ? 0x08 : 0x02), clock_reg);
  138. }
  139. static void pdc202xx_dma_start(ide_drive_t *drive)
  140. {
  141. if (drive->current_speed > XFER_UDMA_2)
  142. pdc_old_enable_66MHz_clock(drive->hwif);
  143. if (drive->media != ide_disk || (drive->dev_flags & IDE_DFLAG_LBA48)) {
  144. ide_hwif_t *hwif = drive->hwif;
  145. struct request *rq = hwif->rq;
  146. unsigned long high_16 = hwif->extra_base - 16;
  147. unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
  148. u32 word_count = 0;
  149. u8 clock = inb(high_16 + 0x11);
  150. outb(clock | (hwif->channel ? 0x08 : 0x02), high_16 + 0x11);
  151. word_count = (blk_rq_sectors(rq) << 8);
  152. word_count = (rq_data_dir(rq) == READ) ?
  153. word_count | 0x05000000 :
  154. word_count | 0x06000000;
  155. outl(word_count, atapi_reg);
  156. }
  157. ide_dma_start(drive);
  158. }
  159. static int pdc202xx_dma_end(ide_drive_t *drive)
  160. {
  161. if (drive->media != ide_disk || (drive->dev_flags & IDE_DFLAG_LBA48)) {
  162. ide_hwif_t *hwif = drive->hwif;
  163. unsigned long high_16 = hwif->extra_base - 16;
  164. unsigned long atapi_reg = high_16 + (hwif->channel ? 0x24 : 0x20);
  165. u8 clock = 0;
  166. outl(0, atapi_reg); /* zero out extra */
  167. clock = inb(high_16 + 0x11);
  168. outb(clock & ~(hwif->channel ? 0x08:0x02), high_16 + 0x11);
  169. }
  170. if (drive->current_speed > XFER_UDMA_2)
  171. pdc_old_disable_66MHz_clock(drive->hwif);
  172. return ide_dma_end(drive);
  173. }
  174. static int init_chipset_pdc202xx(struct pci_dev *dev)
  175. {
  176. unsigned long dmabase = pci_resource_start(dev, 4);
  177. u8 udma_speed_flag = 0, primary_mode = 0, secondary_mode = 0;
  178. if (dmabase == 0)
  179. goto out;
  180. udma_speed_flag = inb(dmabase | 0x1f);
  181. primary_mode = inb(dmabase | 0x1a);
  182. secondary_mode = inb(dmabase | 0x1b);
  183. printk(KERN_INFO "%s: (U)DMA Burst Bit %sABLED " \
  184. "Primary %s Mode " \
  185. "Secondary %s Mode.\n", pci_name(dev),
  186. (udma_speed_flag & 1) ? "EN" : "DIS",
  187. (primary_mode & 1) ? "MASTER" : "PCI",
  188. (secondary_mode & 1) ? "MASTER" : "PCI" );
  189. if (!(udma_speed_flag & 1)) {
  190. printk(KERN_INFO "%s: FORCING BURST BIT 0x%02x->0x%02x ",
  191. pci_name(dev), udma_speed_flag,
  192. (udma_speed_flag|1));
  193. outb(udma_speed_flag | 1, dmabase | 0x1f);
  194. printk("%sACTIVE\n", (inb(dmabase | 0x1f) & 1) ? "" : "IN");
  195. }
  196. out:
  197. return 0;
  198. }
  199. static void __devinit pdc202ata4_fixup_irq(struct pci_dev *dev,
  200. const char *name)
  201. {
  202. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE) {
  203. u8 irq = 0, irq2 = 0;
  204. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  205. /* 0xbc */
  206. pci_read_config_byte(dev, (PCI_INTERRUPT_LINE)|0x80, &irq2);
  207. if (irq != irq2) {
  208. pci_write_config_byte(dev,
  209. (PCI_INTERRUPT_LINE)|0x80, irq); /* 0xbc */
  210. printk(KERN_INFO "%s %s: PCI config space interrupt "
  211. "mirror fixed\n", name, pci_name(dev));
  212. }
  213. }
  214. }
  215. #define IDE_HFLAGS_PDC202XX \
  216. (IDE_HFLAG_ERROR_STOPS_FIFO | \
  217. IDE_HFLAG_OFF_BOARD)
  218. static const struct ide_port_ops pdc20246_port_ops = {
  219. .set_pio_mode = pdc202xx_set_pio_mode,
  220. .set_dma_mode = pdc202xx_set_mode,
  221. .test_irq = pdc202xx_test_irq,
  222. };
  223. static const struct ide_port_ops pdc2026x_port_ops = {
  224. .set_pio_mode = pdc202xx_set_pio_mode,
  225. .set_dma_mode = pdc202xx_set_mode,
  226. .cable_detect = pdc2026x_cable_detect,
  227. };
  228. static const struct ide_dma_ops pdc2026x_dma_ops = {
  229. .dma_host_set = ide_dma_host_set,
  230. .dma_setup = ide_dma_setup,
  231. .dma_start = pdc202xx_dma_start,
  232. .dma_end = pdc202xx_dma_end,
  233. .dma_test_irq = ide_dma_test_irq,
  234. .dma_lost_irq = ide_dma_lost_irq,
  235. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  236. .dma_sff_read_status = ide_dma_sff_read_status,
  237. };
  238. #define DECLARE_PDC2026X_DEV(udma, sectors) \
  239. { \
  240. .name = DRV_NAME, \
  241. .init_chipset = init_chipset_pdc202xx, \
  242. .port_ops = &pdc2026x_port_ops, \
  243. .dma_ops = &pdc2026x_dma_ops, \
  244. .host_flags = IDE_HFLAGS_PDC202XX, \
  245. .pio_mask = ATA_PIO4, \
  246. .mwdma_mask = ATA_MWDMA2, \
  247. .udma_mask = udma, \
  248. .max_sectors = sectors, \
  249. }
  250. static const struct ide_port_info pdc202xx_chipsets[] __devinitdata = {
  251. { /* 0: PDC20246 */
  252. .name = DRV_NAME,
  253. .init_chipset = init_chipset_pdc202xx,
  254. .port_ops = &pdc20246_port_ops,
  255. .dma_ops = &sff_dma_ops,
  256. .host_flags = IDE_HFLAGS_PDC202XX,
  257. .pio_mask = ATA_PIO4,
  258. .mwdma_mask = ATA_MWDMA2,
  259. .udma_mask = ATA_UDMA2,
  260. },
  261. /* 1: PDC2026{2,3} */
  262. DECLARE_PDC2026X_DEV(ATA_UDMA4, 0),
  263. /* 2: PDC2026{5,7}: UDMA5, limit LBA48 requests to 256 sectors */
  264. DECLARE_PDC2026X_DEV(ATA_UDMA5, 256),
  265. };
  266. /**
  267. * pdc202xx_init_one - called when a PDC202xx is found
  268. * @dev: the pdc202xx device
  269. * @id: the matching pci id
  270. *
  271. * Called when the PCI registration layer (or the IDE initialization)
  272. * finds a device matching our IDE device tables.
  273. */
  274. static int __devinit pdc202xx_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  275. {
  276. const struct ide_port_info *d;
  277. u8 idx = id->driver_data;
  278. d = &pdc202xx_chipsets[idx];
  279. if (idx < 2)
  280. pdc202ata4_fixup_irq(dev, d->name);
  281. if (dev->vendor == PCI_DEVICE_ID_PROMISE_20265) {
  282. struct pci_dev *bridge = dev->bus->self;
  283. if (bridge &&
  284. bridge->vendor == PCI_VENDOR_ID_INTEL &&
  285. (bridge->device == PCI_DEVICE_ID_INTEL_I960 ||
  286. bridge->device == PCI_DEVICE_ID_INTEL_I960RM)) {
  287. printk(KERN_INFO DRV_NAME " %s: skipping Promise "
  288. "PDC20265 attached to I2O RAID controller\n",
  289. pci_name(dev));
  290. return -ENODEV;
  291. }
  292. }
  293. return ide_pci_init_one(dev, d, NULL);
  294. }
  295. static const struct pci_device_id pdc202xx_pci_tbl[] = {
  296. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20246), 0 },
  297. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20262), 1 },
  298. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20263), 1 },
  299. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20265), 2 },
  300. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20267), 2 },
  301. { 0, },
  302. };
  303. MODULE_DEVICE_TABLE(pci, pdc202xx_pci_tbl);
  304. static struct pci_driver pdc202xx_pci_driver = {
  305. .name = "Promise_Old_IDE",
  306. .id_table = pdc202xx_pci_tbl,
  307. .probe = pdc202xx_init_one,
  308. .remove = ide_pci_remove,
  309. .suspend = ide_pci_suspend,
  310. .resume = ide_pci_resume,
  311. };
  312. static int __init pdc202xx_ide_init(void)
  313. {
  314. return ide_pci_register_driver(&pdc202xx_pci_driver);
  315. }
  316. static void __exit pdc202xx_ide_exit(void)
  317. {
  318. pci_unregister_driver(&pdc202xx_pci_driver);
  319. }
  320. module_init(pdc202xx_ide_init);
  321. module_exit(pdc202xx_ide_exit);
  322. MODULE_AUTHOR("Andre Hedrick, Frank Tiernan");
  323. MODULE_DESCRIPTION("PCI driver module for older Promise IDE");
  324. MODULE_LICENSE("GPL");