pata_jmicron.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * pata_jmicron.c - JMicron ATA driver for non AHCI mode. This drives the
  4. * PATA port of the controller. The SATA ports are
  5. * driven by AHCI in the usual configuration although
  6. * this driver can handle other setups if we need it.
  7. *
  8. * (c) 2006 Red Hat
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <linux/libata.h>
  18. #include <linux/ata.h>
  19. #define DRV_NAME "pata_jmicron"
  20. #define DRV_VERSION "0.1.5"
  21. typedef enum {
  22. PORT_PATA0 = 0,
  23. PORT_PATA1 = 1,
  24. PORT_SATA = 2,
  25. } port_type;
  26. /**
  27. * jmicron_pre_reset - check for 40/80 pin
  28. * @link: ATA link
  29. * @deadline: deadline jiffies for the operation
  30. *
  31. * Perform the PATA port setup we need.
  32. *
  33. * On the Jmicron 361/363 there is a single PATA port that can be mapped
  34. * either as primary or secondary (or neither). We don't do any policy
  35. * and setup here. We assume that has been done by init_one and the
  36. * BIOS.
  37. */
  38. static int jmicron_pre_reset(struct ata_link *link, unsigned long deadline)
  39. {
  40. struct ata_port *ap = link->ap;
  41. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  42. u32 control;
  43. u32 control5;
  44. int port_mask = 1<< (4 * ap->port_no);
  45. int port = ap->port_no;
  46. port_type port_map[2];
  47. /* Check if our port is enabled */
  48. pci_read_config_dword(pdev, 0x40, &control);
  49. if ((control & port_mask) == 0)
  50. return -ENOENT;
  51. /* There are two basic mappings. One has the two SATA ports merged
  52. as master/slave and the secondary as PATA, the other has only the
  53. SATA port mapped */
  54. if (control & (1 << 23)) {
  55. port_map[0] = PORT_SATA;
  56. port_map[1] = PORT_PATA0;
  57. } else {
  58. port_map[0] = PORT_SATA;
  59. port_map[1] = PORT_SATA;
  60. }
  61. /* The 365/366 may have this bit set to map the second PATA port
  62. as the internal primary channel */
  63. pci_read_config_dword(pdev, 0x80, &control5);
  64. if (control5 & (1<<24))
  65. port_map[0] = PORT_PATA1;
  66. /* The two ports may then be logically swapped by the firmware */
  67. if (control & (1 << 22))
  68. port = port ^ 1;
  69. /*
  70. * Now we know which physical port we are talking about we can
  71. * actually do our cable checking etc. Thankfully we don't need
  72. * to do the plumbing for other cases.
  73. */
  74. switch (port_map[port]) {
  75. case PORT_PATA0:
  76. if ((control & (1 << 5)) == 0)
  77. return -ENOENT;
  78. if (control & (1 << 3)) /* 40/80 pin primary */
  79. ap->cbl = ATA_CBL_PATA40;
  80. else
  81. ap->cbl = ATA_CBL_PATA80;
  82. break;
  83. case PORT_PATA1:
  84. /* Bit 21 is set if the port is enabled */
  85. if ((control5 & (1 << 21)) == 0)
  86. return -ENOENT;
  87. if (control5 & (1 << 19)) /* 40/80 pin secondary */
  88. ap->cbl = ATA_CBL_PATA40;
  89. else
  90. ap->cbl = ATA_CBL_PATA80;
  91. break;
  92. case PORT_SATA:
  93. ap->cbl = ATA_CBL_SATA;
  94. break;
  95. }
  96. return ata_sff_prereset(link, deadline);
  97. }
  98. /* No PIO or DMA methods needed for this device */
  99. static struct scsi_host_template jmicron_sht = {
  100. ATA_BMDMA_SHT(DRV_NAME),
  101. };
  102. static struct ata_port_operations jmicron_ops = {
  103. .inherits = &ata_bmdma_port_ops,
  104. .prereset = jmicron_pre_reset,
  105. };
  106. /**
  107. * jmicron_init_one - Register Jmicron ATA PCI device with kernel services
  108. * @pdev: PCI device to register
  109. * @ent: Entry in jmicron_pci_tbl matching with @pdev
  110. *
  111. * Called from kernel PCI layer.
  112. *
  113. * LOCKING:
  114. * Inherited from PCI layer (may sleep).
  115. *
  116. * RETURNS:
  117. * Zero on success, or -ERRNO value.
  118. */
  119. static int jmicron_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  120. {
  121. static const struct ata_port_info info = {
  122. .flags = ATA_FLAG_SLAVE_POSS,
  123. .pio_mask = ATA_PIO4,
  124. .mwdma_mask = ATA_MWDMA2,
  125. .udma_mask = ATA_UDMA5,
  126. .port_ops = &jmicron_ops,
  127. };
  128. const struct ata_port_info *ppi[] = { &info, NULL };
  129. return ata_pci_bmdma_init_one(pdev, ppi, &jmicron_sht, NULL, 0);
  130. }
  131. static const struct pci_device_id jmicron_pci_tbl[] = {
  132. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  133. PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 0 },
  134. { } /* terminate list */
  135. };
  136. static struct pci_driver jmicron_pci_driver = {
  137. .name = DRV_NAME,
  138. .id_table = jmicron_pci_tbl,
  139. .probe = jmicron_init_one,
  140. .remove = ata_pci_remove_one,
  141. #ifdef CONFIG_PM_SLEEP
  142. .suspend = ata_pci_device_suspend,
  143. .resume = ata_pci_device_resume,
  144. #endif
  145. };
  146. module_pci_driver(jmicron_pci_driver);
  147. MODULE_AUTHOR("Alan Cox");
  148. MODULE_DESCRIPTION("SCSI low-level driver for Jmicron PATA ports");
  149. MODULE_LICENSE("GPL");
  150. MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
  151. MODULE_VERSION(DRV_VERSION);