pata_acpi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * ACPI PATA driver
  3. *
  4. * (c) 2007 Red Hat <alan@redhat.com>
  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 <scsi/scsi_host.h>
  14. #include <acpi/acpi_bus.h>
  15. #include <acpi/acnames.h>
  16. #include <acpi/acnamesp.h>
  17. #include <acpi/acparser.h>
  18. #include <acpi/acexcep.h>
  19. #include <acpi/acmacros.h>
  20. #include <acpi/actypes.h>
  21. #include <linux/libata.h>
  22. #include <linux/ata.h>
  23. #define DRV_NAME "pata_acpi"
  24. #define DRV_VERSION "0.2.3"
  25. struct pata_acpi {
  26. struct ata_acpi_gtm gtm;
  27. void *last;
  28. unsigned long mask[2];
  29. };
  30. /**
  31. * pacpi_pre_reset - check for 40/80 pin
  32. * @ap: Port
  33. * @deadline: deadline jiffies for the operation
  34. *
  35. * Perform the PATA port setup we need.
  36. */
  37. static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline)
  38. {
  39. struct ata_port *ap = link->ap;
  40. struct pata_acpi *acpi = ap->private_data;
  41. if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
  42. return -ENODEV;
  43. return ata_std_prereset(link, deadline);
  44. }
  45. /**
  46. * pacpi_cable_detect - cable type detection
  47. * @ap: port to detect
  48. *
  49. * Perform device specific cable detection
  50. */
  51. static int pacpi_cable_detect(struct ata_port *ap)
  52. {
  53. struct pata_acpi *acpi = ap->private_data;
  54. if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA))
  55. return ATA_CBL_PATA80;
  56. else
  57. return ATA_CBL_PATA40;
  58. }
  59. /**
  60. * pacpi_error_handler - Setup and error handler
  61. * @ap: Port to handle
  62. *
  63. * LOCKING:
  64. * None (inherited from caller).
  65. */
  66. static void pacpi_error_handler(struct ata_port *ap)
  67. {
  68. ata_bmdma_drive_eh(ap, pacpi_pre_reset, ata_std_softreset, NULL,
  69. ata_std_postreset);
  70. }
  71. /**
  72. * pacpi_discover_modes - filter non ACPI modes
  73. * @adev: ATA device
  74. * @mask: proposed modes
  75. *
  76. * Try the modes available and see which ones the ACPI method will
  77. * set up sensibly. From this we get a mask of ACPI modes we can use
  78. */
  79. static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
  80. {
  81. struct pata_acpi *acpi = ap->private_data;
  82. struct ata_acpi_gtm probe;
  83. unsigned int xfer_mask;
  84. probe = acpi->gtm;
  85. ata_acpi_gtm(ap, &probe);
  86. xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
  87. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  88. ap->cbl = ATA_CBL_PATA80;
  89. return xfer_mask;
  90. }
  91. /**
  92. * pacpi_mode_filter - mode filter for ACPI
  93. * @adev: device
  94. * @mask: mask of valid modes
  95. *
  96. * Filter the valid mode list according to our own specific rules, in
  97. * this case the list of discovered valid modes obtained by ACPI probing
  98. */
  99. static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
  100. {
  101. struct pata_acpi *acpi = adev->link->ap->private_data;
  102. return ata_pci_default_filter(adev, mask & acpi->mask[adev->devno]);
  103. }
  104. /**
  105. * pacpi_set_piomode - set initial PIO mode data
  106. * @ap: ATA interface
  107. * @adev: ATA device
  108. */
  109. static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
  110. {
  111. int unit = adev->devno;
  112. struct pata_acpi *acpi = ap->private_data;
  113. const struct ata_timing *t;
  114. if (!(acpi->gtm.flags & 0x10))
  115. unit = 0;
  116. /* Now stuff the nS values into the structure */
  117. t = ata_timing_find_mode(adev->pio_mode);
  118. acpi->gtm.drive[unit].pio = t->cycle;
  119. ata_acpi_stm(ap, &acpi->gtm);
  120. /* See what mode we actually got */
  121. ata_acpi_gtm(ap, &acpi->gtm);
  122. }
  123. /**
  124. * pacpi_set_dmamode - set initial DMA mode data
  125. * @ap: ATA interface
  126. * @adev: ATA device
  127. */
  128. static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  129. {
  130. int unit = adev->devno;
  131. struct pata_acpi *acpi = ap->private_data;
  132. const struct ata_timing *t;
  133. if (!(acpi->gtm.flags & 0x10))
  134. unit = 0;
  135. /* Now stuff the nS values into the structure */
  136. t = ata_timing_find_mode(adev->dma_mode);
  137. if (adev->dma_mode >= XFER_UDMA_0) {
  138. acpi->gtm.drive[unit].dma = t->udma;
  139. acpi->gtm.flags |= (1 << (2 * unit));
  140. } else {
  141. acpi->gtm.drive[unit].dma = t->cycle;
  142. acpi->gtm.flags &= ~(1 << (2 * unit));
  143. }
  144. ata_acpi_stm(ap, &acpi->gtm);
  145. /* See what mode we actually got */
  146. ata_acpi_gtm(ap, &acpi->gtm);
  147. }
  148. /**
  149. * pacpi_qc_issue_prot - command issue
  150. * @qc: command pending
  151. *
  152. * Called when the libata layer is about to issue a command. We wrap
  153. * this interface so that we can load the correct ATA timings if
  154. * neccessary.
  155. */
  156. static unsigned int pacpi_qc_issue_prot(struct ata_queued_cmd *qc)
  157. {
  158. struct ata_port *ap = qc->ap;
  159. struct ata_device *adev = qc->dev;
  160. struct pata_acpi *acpi = ap->private_data;
  161. if (acpi->gtm.flags & 0x10)
  162. return ata_qc_issue_prot(qc);
  163. if (adev != acpi->last) {
  164. pacpi_set_piomode(ap, adev);
  165. if (adev->dma_mode)
  166. pacpi_set_dmamode(ap, adev);
  167. acpi->last = adev;
  168. }
  169. return ata_qc_issue_prot(qc);
  170. }
  171. /**
  172. * pacpi_port_start - port setup
  173. * @ap: ATA port being set up
  174. *
  175. * Use the port_start hook to maintain private control structures
  176. */
  177. static int pacpi_port_start(struct ata_port *ap)
  178. {
  179. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  180. struct pata_acpi *acpi;
  181. int ret;
  182. if (ap->acpi_handle == NULL)
  183. return -ENODEV;
  184. acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
  185. if (ap->private_data == NULL)
  186. return -ENOMEM;
  187. acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
  188. acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
  189. ret = ata_sff_port_start(ap);
  190. if (ret < 0)
  191. return ret;
  192. return ret;
  193. }
  194. static struct scsi_host_template pacpi_sht = {
  195. ATA_BMDMA_SHT(DRV_NAME),
  196. };
  197. static const struct ata_port_operations pacpi_ops = {
  198. .set_piomode = pacpi_set_piomode,
  199. .set_dmamode = pacpi_set_dmamode,
  200. .mode_filter = pacpi_mode_filter,
  201. /* Task file is PCI ATA format, use helpers */
  202. .tf_load = ata_tf_load,
  203. .tf_read = ata_tf_read,
  204. .check_status = ata_check_status,
  205. .exec_command = ata_exec_command,
  206. .dev_select = ata_std_dev_select,
  207. .freeze = ata_bmdma_freeze,
  208. .thaw = ata_bmdma_thaw,
  209. .error_handler = pacpi_error_handler,
  210. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  211. .cable_detect = pacpi_cable_detect,
  212. /* BMDMA handling is PCI ATA format, use helpers */
  213. .bmdma_setup = ata_bmdma_setup,
  214. .bmdma_start = ata_bmdma_start,
  215. .bmdma_stop = ata_bmdma_stop,
  216. .bmdma_status = ata_bmdma_status,
  217. .qc_prep = ata_qc_prep,
  218. .qc_issue = pacpi_qc_issue_prot,
  219. .data_xfer = ata_data_xfer,
  220. /* Timeout handling */
  221. .irq_handler = ata_interrupt,
  222. .irq_clear = ata_bmdma_irq_clear,
  223. .irq_on = ata_irq_on,
  224. /* Generic PATA PCI ATA helpers */
  225. .port_start = pacpi_port_start,
  226. };
  227. /**
  228. * pacpi_init_one - Register ACPI ATA PCI device with kernel services
  229. * @pdev: PCI device to register
  230. * @ent: Entry in pacpi_pci_tbl matching with @pdev
  231. *
  232. * Called from kernel PCI layer.
  233. *
  234. * LOCKING:
  235. * Inherited from PCI layer (may sleep).
  236. *
  237. * RETURNS:
  238. * Zero on success, or -ERRNO value.
  239. */
  240. static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  241. {
  242. static const struct ata_port_info info = {
  243. .sht = &pacpi_sht,
  244. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  245. .pio_mask = 0x1f,
  246. .mwdma_mask = 0x07,
  247. .udma_mask = 0x7f,
  248. .port_ops = &pacpi_ops,
  249. };
  250. const struct ata_port_info *ppi[] = { &info, NULL };
  251. return ata_pci_init_one(pdev, ppi);
  252. }
  253. static const struct pci_device_id pacpi_pci_tbl[] = {
  254. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
  255. { } /* terminate list */
  256. };
  257. static struct pci_driver pacpi_pci_driver = {
  258. .name = DRV_NAME,
  259. .id_table = pacpi_pci_tbl,
  260. .probe = pacpi_init_one,
  261. .remove = ata_pci_remove_one,
  262. #ifdef CONFIG_PM
  263. .suspend = ata_pci_device_suspend,
  264. .resume = ata_pci_device_resume,
  265. #endif
  266. };
  267. static int __init pacpi_init(void)
  268. {
  269. return pci_register_driver(&pacpi_pci_driver);
  270. }
  271. static void __exit pacpi_exit(void)
  272. {
  273. pci_unregister_driver(&pacpi_pci_driver);
  274. }
  275. module_init(pacpi_init);
  276. module_exit(pacpi_exit);
  277. MODULE_AUTHOR("Alan Cox");
  278. MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
  279. MODULE_LICENSE("GPL");
  280. MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
  281. MODULE_VERSION(DRV_VERSION);