pata_acpi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * ACPI PATA driver
  3. *
  4. * (c) 2007 Red Hat
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include <linux/init.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/gfp.h>
  14. #include <scsi/scsi_host.h>
  15. #include <acpi/acpi_bus.h>
  16. #include <linux/libata.h>
  17. #include <linux/ata.h>
  18. #define DRV_NAME "pata_acpi"
  19. #define DRV_VERSION "0.2.3"
  20. struct pata_acpi {
  21. struct ata_acpi_gtm gtm;
  22. void *last;
  23. unsigned long mask[2];
  24. };
  25. /**
  26. * pacpi_pre_reset - check for 40/80 pin
  27. * @ap: Port
  28. * @deadline: deadline jiffies for the operation
  29. *
  30. * Perform the PATA port setup we need.
  31. */
  32. static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline)
  33. {
  34. struct ata_port *ap = link->ap;
  35. struct pata_acpi *acpi = ap->private_data;
  36. if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
  37. return -ENODEV;
  38. return ata_sff_prereset(link, deadline);
  39. }
  40. /**
  41. * pacpi_cable_detect - cable type detection
  42. * @ap: port to detect
  43. *
  44. * Perform device specific cable detection
  45. */
  46. static int pacpi_cable_detect(struct ata_port *ap)
  47. {
  48. struct pata_acpi *acpi = ap->private_data;
  49. if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA))
  50. return ATA_CBL_PATA80;
  51. else
  52. return ATA_CBL_PATA40;
  53. }
  54. /**
  55. * pacpi_discover_modes - filter non ACPI modes
  56. * @adev: ATA device
  57. * @mask: proposed modes
  58. *
  59. * Try the modes available and see which ones the ACPI method will
  60. * set up sensibly. From this we get a mask of ACPI modes we can use
  61. */
  62. static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
  63. {
  64. struct pata_acpi *acpi = ap->private_data;
  65. struct ata_acpi_gtm probe;
  66. unsigned int xfer_mask;
  67. probe = acpi->gtm;
  68. ata_acpi_gtm(ap, &probe);
  69. xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
  70. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  71. ap->cbl = ATA_CBL_PATA80;
  72. return xfer_mask;
  73. }
  74. /**
  75. * pacpi_mode_filter - mode filter for ACPI
  76. * @adev: device
  77. * @mask: mask of valid modes
  78. *
  79. * Filter the valid mode list according to our own specific rules, in
  80. * this case the list of discovered valid modes obtained by ACPI probing
  81. */
  82. static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
  83. {
  84. struct pata_acpi *acpi = adev->link->ap->private_data;
  85. return mask & acpi->mask[adev->devno];
  86. }
  87. /**
  88. * pacpi_set_piomode - set initial PIO mode data
  89. * @ap: ATA interface
  90. * @adev: ATA device
  91. */
  92. static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
  93. {
  94. int unit = adev->devno;
  95. struct pata_acpi *acpi = ap->private_data;
  96. const struct ata_timing *t;
  97. if (!(acpi->gtm.flags & 0x10))
  98. unit = 0;
  99. /* Now stuff the nS values into the structure */
  100. t = ata_timing_find_mode(adev->pio_mode);
  101. acpi->gtm.drive[unit].pio = t->cycle;
  102. ata_acpi_stm(ap, &acpi->gtm);
  103. /* See what mode we actually got */
  104. ata_acpi_gtm(ap, &acpi->gtm);
  105. }
  106. /**
  107. * pacpi_set_dmamode - set initial DMA mode data
  108. * @ap: ATA interface
  109. * @adev: ATA device
  110. */
  111. static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  112. {
  113. int unit = adev->devno;
  114. struct pata_acpi *acpi = ap->private_data;
  115. const struct ata_timing *t;
  116. if (!(acpi->gtm.flags & 0x10))
  117. unit = 0;
  118. /* Now stuff the nS values into the structure */
  119. t = ata_timing_find_mode(adev->dma_mode);
  120. if (adev->dma_mode >= XFER_UDMA_0) {
  121. acpi->gtm.drive[unit].dma = t->udma;
  122. acpi->gtm.flags |= (1 << (2 * unit));
  123. } else {
  124. acpi->gtm.drive[unit].dma = t->cycle;
  125. acpi->gtm.flags &= ~(1 << (2 * unit));
  126. }
  127. ata_acpi_stm(ap, &acpi->gtm);
  128. /* See what mode we actually got */
  129. ata_acpi_gtm(ap, &acpi->gtm);
  130. }
  131. /**
  132. * pacpi_qc_issue - command issue
  133. * @qc: command pending
  134. *
  135. * Called when the libata layer is about to issue a command. We wrap
  136. * this interface so that we can load the correct ATA timings if
  137. * necessary.
  138. */
  139. static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc)
  140. {
  141. struct ata_port *ap = qc->ap;
  142. struct ata_device *adev = qc->dev;
  143. struct pata_acpi *acpi = ap->private_data;
  144. if (acpi->gtm.flags & 0x10)
  145. return ata_bmdma_qc_issue(qc);
  146. if (adev != acpi->last) {
  147. pacpi_set_piomode(ap, adev);
  148. if (ata_dma_enabled(adev))
  149. pacpi_set_dmamode(ap, adev);
  150. acpi->last = adev;
  151. }
  152. return ata_bmdma_qc_issue(qc);
  153. }
  154. /**
  155. * pacpi_port_start - port setup
  156. * @ap: ATA port being set up
  157. *
  158. * Use the port_start hook to maintain private control structures
  159. */
  160. static int pacpi_port_start(struct ata_port *ap)
  161. {
  162. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  163. struct pata_acpi *acpi;
  164. if (ap->acpi_handle == NULL)
  165. return -ENODEV;
  166. acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
  167. if (ap->private_data == NULL)
  168. return -ENOMEM;
  169. acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
  170. acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
  171. return ata_bmdma_port_start(ap);
  172. }
  173. static struct scsi_host_template pacpi_sht = {
  174. ATA_BMDMA_SHT(DRV_NAME),
  175. };
  176. static struct ata_port_operations pacpi_ops = {
  177. .inherits = &ata_bmdma_port_ops,
  178. .qc_issue = pacpi_qc_issue,
  179. .cable_detect = pacpi_cable_detect,
  180. .mode_filter = pacpi_mode_filter,
  181. .set_piomode = pacpi_set_piomode,
  182. .set_dmamode = pacpi_set_dmamode,
  183. .prereset = pacpi_pre_reset,
  184. .port_start = pacpi_port_start,
  185. };
  186. /**
  187. * pacpi_init_one - Register ACPI ATA PCI device with kernel services
  188. * @pdev: PCI device to register
  189. * @ent: Entry in pacpi_pci_tbl matching with @pdev
  190. *
  191. * Called from kernel PCI layer.
  192. *
  193. * LOCKING:
  194. * Inherited from PCI layer (may sleep).
  195. *
  196. * RETURNS:
  197. * Zero on success, or -ERRNO value.
  198. */
  199. static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  200. {
  201. static const struct ata_port_info info = {
  202. .flags = ATA_FLAG_SLAVE_POSS,
  203. .pio_mask = ATA_PIO4,
  204. .mwdma_mask = ATA_MWDMA2,
  205. .udma_mask = ATA_UDMA6,
  206. .port_ops = &pacpi_ops,
  207. };
  208. const struct ata_port_info *ppi[] = { &info, NULL };
  209. if (pdev->vendor == PCI_VENDOR_ID_ATI) {
  210. int rc = pcim_enable_device(pdev);
  211. if (rc < 0)
  212. return rc;
  213. pcim_pin_device(pdev);
  214. }
  215. return ata_pci_bmdma_init_one(pdev, ppi, &pacpi_sht, NULL, 0);
  216. }
  217. static const struct pci_device_id pacpi_pci_tbl[] = {
  218. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
  219. { } /* terminate list */
  220. };
  221. static struct pci_driver pacpi_pci_driver = {
  222. .name = DRV_NAME,
  223. .id_table = pacpi_pci_tbl,
  224. .probe = pacpi_init_one,
  225. .remove = ata_pci_remove_one,
  226. #ifdef CONFIG_PM
  227. .suspend = ata_pci_device_suspend,
  228. .resume = ata_pci_device_resume,
  229. #endif
  230. };
  231. static int __init pacpi_init(void)
  232. {
  233. return pci_register_driver(&pacpi_pci_driver);
  234. }
  235. static void __exit pacpi_exit(void)
  236. {
  237. pci_unregister_driver(&pacpi_pci_driver);
  238. }
  239. module_init(pacpi_init);
  240. module_exit(pacpi_exit);
  241. MODULE_AUTHOR("Alan Cox");
  242. MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
  243. MODULE_LICENSE("GPL");
  244. MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
  245. MODULE_VERSION(DRV_VERSION);