pdc202xx_old.c 10 KB

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