pata_pdc202xx_old.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * pata_pdc202xx_old.c - Promise PDC202xx PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. * (C) 2007 Bartlomiej Zolnierkiewicz
  6. *
  7. * Based in part on linux/drivers/ide/pci/pdc202xx_old.c
  8. *
  9. * First cut with LBA48/ATAPI
  10. *
  11. * TODO:
  12. * Channel interlock/reset on both required ?
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/delay.h>
  20. #include <scsi/scsi_host.h>
  21. #include <linux/libata.h>
  22. #define DRV_NAME "pata_pdc202xx_old"
  23. #define DRV_VERSION "0.4.3"
  24. static int pdc2026x_cable_detect(struct ata_port *ap)
  25. {
  26. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  27. u16 cis;
  28. pci_read_config_word(pdev, 0x50, &cis);
  29. if (cis & (1 << (10 + ap->port_no)))
  30. return ATA_CBL_PATA40;
  31. return ATA_CBL_PATA80;
  32. }
  33. /**
  34. * pdc202xx_configure_piomode - set chip PIO timing
  35. * @ap: ATA interface
  36. * @adev: ATA device
  37. * @pio: PIO mode
  38. *
  39. * Called to do the PIO mode setup. Our timing registers are shared
  40. * so a configure_dmamode call will undo any work we do here and vice
  41. * versa
  42. */
  43. static void pdc202xx_configure_piomode(struct ata_port *ap, struct ata_device *adev, int pio)
  44. {
  45. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  46. int port = 0x60 + 8 * ap->port_no + 4 * adev->devno;
  47. static u16 pio_timing[5] = {
  48. 0x0913, 0x050C , 0x0308, 0x0206, 0x0104
  49. };
  50. u8 r_ap, r_bp;
  51. pci_read_config_byte(pdev, port, &r_ap);
  52. pci_read_config_byte(pdev, port + 1, &r_bp);
  53. r_ap &= ~0x3F; /* Preserve ERRDY_EN, SYNC_IN */
  54. r_bp &= ~0x1F;
  55. r_ap |= (pio_timing[pio] >> 8);
  56. r_bp |= (pio_timing[pio] & 0xFF);
  57. if (ata_pio_need_iordy(adev))
  58. r_ap |= 0x20; /* IORDY enable */
  59. if (adev->class == ATA_DEV_ATA)
  60. r_ap |= 0x10; /* FIFO enable */
  61. pci_write_config_byte(pdev, port, r_ap);
  62. pci_write_config_byte(pdev, port + 1, r_bp);
  63. }
  64. /**
  65. * pdc202xx_set_piomode - set initial PIO mode data
  66. * @ap: ATA interface
  67. * @adev: ATA device
  68. *
  69. * Called to do the PIO mode setup. Our timing registers are shared
  70. * but we want to set the PIO timing by default.
  71. */
  72. static void pdc202xx_set_piomode(struct ata_port *ap, struct ata_device *adev)
  73. {
  74. pdc202xx_configure_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  75. }
  76. /**
  77. * pdc202xx_configure_dmamode - set DMA mode in chip
  78. * @ap: ATA interface
  79. * @adev: ATA device
  80. *
  81. * Load DMA cycle times into the chip ready for a DMA transfer
  82. * to occur.
  83. */
  84. static void pdc202xx_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  85. {
  86. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  87. int port = 0x60 + 8 * ap->port_no + 4 * adev->devno;
  88. static u8 udma_timing[6][2] = {
  89. { 0x60, 0x03 }, /* 33 Mhz Clock */
  90. { 0x40, 0x02 },
  91. { 0x20, 0x01 },
  92. { 0x40, 0x02 }, /* 66 Mhz Clock */
  93. { 0x20, 0x01 },
  94. { 0x20, 0x01 }
  95. };
  96. static u8 mdma_timing[3][2] = {
  97. { 0xe0, 0x0f },
  98. { 0x60, 0x04 },
  99. { 0x60, 0x03 },
  100. };
  101. u8 r_bp, r_cp;
  102. pci_read_config_byte(pdev, port + 1, &r_bp);
  103. pci_read_config_byte(pdev, port + 2, &r_cp);
  104. r_bp &= ~0xE0;
  105. r_cp &= ~0x0F;
  106. if (adev->dma_mode >= XFER_UDMA_0) {
  107. int speed = adev->dma_mode - XFER_UDMA_0;
  108. r_bp |= udma_timing[speed][0];
  109. r_cp |= udma_timing[speed][1];
  110. } else {
  111. int speed = adev->dma_mode - XFER_MW_DMA_0;
  112. r_bp |= mdma_timing[speed][0];
  113. r_cp |= mdma_timing[speed][1];
  114. }
  115. pci_write_config_byte(pdev, port + 1, r_bp);
  116. pci_write_config_byte(pdev, port + 2, r_cp);
  117. }
  118. /**
  119. * pdc2026x_bmdma_start - DMA engine begin
  120. * @qc: ATA command
  121. *
  122. * In UDMA3 or higher we have to clock switch for the duration of the
  123. * DMA transfer sequence.
  124. *
  125. * Note: The host lock held by the libata layer protects
  126. * us from two channels both trying to set DMA bits at once
  127. */
  128. static void pdc2026x_bmdma_start(struct ata_queued_cmd *qc)
  129. {
  130. struct ata_port *ap = qc->ap;
  131. struct ata_device *adev = qc->dev;
  132. struct ata_taskfile *tf = &qc->tf;
  133. int sel66 = ap->port_no ? 0x08: 0x02;
  134. void __iomem *master = ap->host->ports[0]->ioaddr.bmdma_addr;
  135. void __iomem *clock = master + 0x11;
  136. void __iomem *atapi_reg = master + 0x20 + (4 * ap->port_no);
  137. u32 len;
  138. /* Check we keep host level locking here */
  139. if (adev->dma_mode >= XFER_UDMA_2)
  140. iowrite8(ioread8(clock) | sel66, clock);
  141. else
  142. iowrite8(ioread8(clock) & ~sel66, clock);
  143. /* The DMA clocks may have been trashed by a reset. FIXME: make conditional
  144. and move to qc_issue ? */
  145. pdc202xx_set_dmamode(ap, qc->dev);
  146. /* Cases the state machine will not complete correctly without help */
  147. if ((tf->flags & ATA_TFLAG_LBA48) || tf->protocol == ATAPI_PROT_DMA) {
  148. len = qc->nbytes / 2;
  149. if (tf->flags & ATA_TFLAG_WRITE)
  150. len |= 0x06000000;
  151. else
  152. len |= 0x05000000;
  153. iowrite32(len, atapi_reg);
  154. }
  155. /* Activate DMA */
  156. ata_bmdma_start(qc);
  157. }
  158. /**
  159. * pdc2026x_bmdma_end - DMA engine stop
  160. * @qc: ATA command
  161. *
  162. * After a DMA completes we need to put the clock back to 33MHz for
  163. * PIO timings.
  164. *
  165. * Note: The host lock held by the libata layer protects
  166. * us from two channels both trying to set DMA bits at once
  167. */
  168. static void pdc2026x_bmdma_stop(struct ata_queued_cmd *qc)
  169. {
  170. struct ata_port *ap = qc->ap;
  171. struct ata_device *adev = qc->dev;
  172. struct ata_taskfile *tf = &qc->tf;
  173. int sel66 = ap->port_no ? 0x08: 0x02;
  174. /* The clock bits are in the same register for both channels */
  175. void __iomem *master = ap->host->ports[0]->ioaddr.bmdma_addr;
  176. void __iomem *clock = master + 0x11;
  177. void __iomem *atapi_reg = master + 0x20 + (4 * ap->port_no);
  178. /* Cases the state machine will not complete correctly */
  179. if (tf->protocol == ATAPI_PROT_DMA || (tf->flags & ATA_TFLAG_LBA48)) {
  180. iowrite32(0, atapi_reg);
  181. iowrite8(ioread8(clock) & ~sel66, clock);
  182. }
  183. /* Flip back to 33Mhz for PIO */
  184. if (adev->dma_mode >= XFER_UDMA_2)
  185. iowrite8(ioread8(clock) & ~sel66, clock);
  186. ata_bmdma_stop(qc);
  187. pdc202xx_set_piomode(ap, adev);
  188. }
  189. /**
  190. * pdc2026x_dev_config - device setup hook
  191. * @adev: newly found device
  192. *
  193. * Perform chip specific early setup. We need to lock the transfer
  194. * sizes to 8bit to avoid making the state engine on the 2026x cards
  195. * barf.
  196. */
  197. static void pdc2026x_dev_config(struct ata_device *adev)
  198. {
  199. adev->max_sectors = 256;
  200. }
  201. static int pdc2026x_port_start(struct ata_port *ap)
  202. {
  203. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  204. if (bmdma) {
  205. /* Enable burst mode */
  206. u8 burst = ioread8(bmdma + 0x1f);
  207. iowrite8(burst | 0x01, bmdma + 0x1f);
  208. }
  209. return ata_sff_port_start(ap);
  210. }
  211. /**
  212. * pdc2026x_check_atapi_dma - Check whether ATAPI DMA can be supported for this command
  213. * @qc: Metadata associated with taskfile to check
  214. *
  215. * Just say no - not supported on older Promise.
  216. *
  217. * LOCKING:
  218. * None (inherited from caller).
  219. *
  220. * RETURNS: 0 when ATAPI DMA can be used
  221. * 1 otherwise
  222. */
  223. static int pdc2026x_check_atapi_dma(struct ata_queued_cmd *qc)
  224. {
  225. return 1;
  226. }
  227. static struct scsi_host_template pdc202xx_sht = {
  228. .module = THIS_MODULE,
  229. .name = DRV_NAME,
  230. .ioctl = ata_scsi_ioctl,
  231. .queuecommand = ata_scsi_queuecmd,
  232. .can_queue = ATA_DEF_QUEUE,
  233. .this_id = ATA_SHT_THIS_ID,
  234. .sg_tablesize = LIBATA_MAX_PRD,
  235. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  236. .emulated = ATA_SHT_EMULATED,
  237. .use_clustering = ATA_SHT_USE_CLUSTERING,
  238. .proc_name = DRV_NAME,
  239. .dma_boundary = ATA_DMA_BOUNDARY,
  240. .slave_configure = ata_scsi_slave_config,
  241. .slave_destroy = ata_scsi_slave_destroy,
  242. .bios_param = ata_std_bios_param,
  243. };
  244. static struct ata_port_operations pdc2024x_port_ops = {
  245. .set_piomode = pdc202xx_set_piomode,
  246. .set_dmamode = pdc202xx_set_dmamode,
  247. .mode_filter = ata_pci_default_filter,
  248. .tf_load = ata_tf_load,
  249. .tf_read = ata_tf_read,
  250. .check_status = ata_check_status,
  251. .exec_command = ata_exec_command,
  252. .dev_select = ata_std_dev_select,
  253. .freeze = ata_bmdma_freeze,
  254. .thaw = ata_bmdma_thaw,
  255. .error_handler = ata_bmdma_error_handler,
  256. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  257. .cable_detect = ata_cable_40wire,
  258. .bmdma_setup = ata_bmdma_setup,
  259. .bmdma_start = ata_bmdma_start,
  260. .bmdma_stop = ata_bmdma_stop,
  261. .bmdma_status = ata_bmdma_status,
  262. .qc_prep = ata_qc_prep,
  263. .qc_issue = ata_qc_issue_prot,
  264. .data_xfer = ata_data_xfer,
  265. .irq_handler = ata_interrupt,
  266. .irq_clear = ata_bmdma_irq_clear,
  267. .irq_on = ata_irq_on,
  268. .port_start = ata_sff_port_start,
  269. };
  270. static struct ata_port_operations pdc2026x_port_ops = {
  271. .set_piomode = pdc202xx_set_piomode,
  272. .set_dmamode = pdc202xx_set_dmamode,
  273. .mode_filter = ata_pci_default_filter,
  274. .tf_load = ata_tf_load,
  275. .tf_read = ata_tf_read,
  276. .check_status = ata_check_status,
  277. .exec_command = ata_exec_command,
  278. .dev_select = ata_std_dev_select,
  279. .dev_config = pdc2026x_dev_config,
  280. .freeze = ata_bmdma_freeze,
  281. .thaw = ata_bmdma_thaw,
  282. .error_handler = ata_bmdma_error_handler,
  283. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  284. .cable_detect = pdc2026x_cable_detect,
  285. .check_atapi_dma= pdc2026x_check_atapi_dma,
  286. .bmdma_setup = ata_bmdma_setup,
  287. .bmdma_start = pdc2026x_bmdma_start,
  288. .bmdma_stop = pdc2026x_bmdma_stop,
  289. .bmdma_status = ata_bmdma_status,
  290. .qc_prep = ata_qc_prep,
  291. .qc_issue = ata_qc_issue_prot,
  292. .data_xfer = ata_data_xfer,
  293. .irq_handler = ata_interrupt,
  294. .irq_clear = ata_bmdma_irq_clear,
  295. .irq_on = ata_irq_on,
  296. .port_start = pdc2026x_port_start,
  297. };
  298. static int pdc202xx_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  299. {
  300. static const struct ata_port_info info[3] = {
  301. {
  302. .sht = &pdc202xx_sht,
  303. .flags = ATA_FLAG_SLAVE_POSS,
  304. .pio_mask = 0x1f,
  305. .mwdma_mask = 0x07,
  306. .udma_mask = ATA_UDMA2,
  307. .port_ops = &pdc2024x_port_ops
  308. },
  309. {
  310. .sht = &pdc202xx_sht,
  311. .flags = ATA_FLAG_SLAVE_POSS,
  312. .pio_mask = 0x1f,
  313. .mwdma_mask = 0x07,
  314. .udma_mask = ATA_UDMA4,
  315. .port_ops = &pdc2026x_port_ops
  316. },
  317. {
  318. .sht = &pdc202xx_sht,
  319. .flags = ATA_FLAG_SLAVE_POSS,
  320. .pio_mask = 0x1f,
  321. .mwdma_mask = 0x07,
  322. .udma_mask = ATA_UDMA5,
  323. .port_ops = &pdc2026x_port_ops
  324. }
  325. };
  326. const struct ata_port_info *ppi[] = { &info[id->driver_data], NULL };
  327. if (dev->device == PCI_DEVICE_ID_PROMISE_20265) {
  328. struct pci_dev *bridge = dev->bus->self;
  329. /* Don't grab anything behind a Promise I2O RAID */
  330. if (bridge && bridge->vendor == PCI_VENDOR_ID_INTEL) {
  331. if (bridge->device == PCI_DEVICE_ID_INTEL_I960)
  332. return -ENODEV;
  333. if (bridge->device == PCI_DEVICE_ID_INTEL_I960RM)
  334. return -ENODEV;
  335. }
  336. }
  337. return ata_pci_init_one(dev, ppi);
  338. }
  339. static const struct pci_device_id pdc202xx[] = {
  340. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20246), 0 },
  341. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20262), 1 },
  342. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20263), 1 },
  343. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20265), 2 },
  344. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20267), 2 },
  345. { },
  346. };
  347. static struct pci_driver pdc202xx_pci_driver = {
  348. .name = DRV_NAME,
  349. .id_table = pdc202xx,
  350. .probe = pdc202xx_init_one,
  351. .remove = ata_pci_remove_one,
  352. #ifdef CONFIG_PM
  353. .suspend = ata_pci_device_suspend,
  354. .resume = ata_pci_device_resume,
  355. #endif
  356. };
  357. static int __init pdc202xx_init(void)
  358. {
  359. return pci_register_driver(&pdc202xx_pci_driver);
  360. }
  361. static void __exit pdc202xx_exit(void)
  362. {
  363. pci_unregister_driver(&pdc202xx_pci_driver);
  364. }
  365. MODULE_AUTHOR("Alan Cox");
  366. MODULE_DESCRIPTION("low-level driver for Promise 2024x and 20262-20267");
  367. MODULE_LICENSE("GPL");
  368. MODULE_DEVICE_TABLE(pci, pdc202xx);
  369. MODULE_VERSION(DRV_VERSION);
  370. module_init(pdc202xx_init);
  371. module_exit(pdc202xx_exit);