pata_acpi.c 6.6 KB

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