sata_uli.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * sata_uli.c - ULi Electronics SATA
  4. *
  5. * libata documentation is available via 'make {ps|pdf}docs',
  6. * as Documentation/driver-api/libata.rst
  7. *
  8. * Hardware documentation available under NDA.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/gfp.h>
  13. #include <linux/pci.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/device.h>
  18. #include <scsi/scsi_host.h>
  19. #include <linux/libata.h>
  20. #define DRV_NAME "sata_uli"
  21. #define DRV_VERSION "1.3"
  22. enum {
  23. uli_5289 = 0,
  24. uli_5287 = 1,
  25. uli_5281 = 2,
  26. uli_max_ports = 4,
  27. /* PCI configuration registers */
  28. ULI5287_BASE = 0x90, /* sata0 phy SCR registers */
  29. ULI5287_OFFS = 0x10, /* offset from sata0->sata1 phy regs */
  30. ULI5281_BASE = 0x60, /* sata0 phy SCR registers */
  31. ULI5281_OFFS = 0x60, /* offset from sata0->sata1 phy regs */
  32. };
  33. struct uli_priv {
  34. unsigned int scr_cfg_addr[uli_max_ports];
  35. };
  36. static int uli_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  37. static int uli_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  38. static int uli_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  39. static const struct pci_device_id uli_pci_tbl[] = {
  40. { PCI_VDEVICE(AL, 0x5289), uli_5289 },
  41. { PCI_VDEVICE(AL, 0x5287), uli_5287 },
  42. { PCI_VDEVICE(AL, 0x5281), uli_5281 },
  43. { } /* terminate list */
  44. };
  45. static struct pci_driver uli_pci_driver = {
  46. .name = DRV_NAME,
  47. .id_table = uli_pci_tbl,
  48. .probe = uli_init_one,
  49. .remove = ata_pci_remove_one,
  50. };
  51. static struct scsi_host_template uli_sht = {
  52. ATA_BMDMA_SHT(DRV_NAME),
  53. };
  54. static struct ata_port_operations uli_ops = {
  55. .inherits = &ata_bmdma_port_ops,
  56. .scr_read = uli_scr_read,
  57. .scr_write = uli_scr_write,
  58. .hardreset = ATA_OP_NULL,
  59. };
  60. static const struct ata_port_info uli_port_info = {
  61. .flags = ATA_FLAG_SATA | ATA_FLAG_IGN_SIMPLEX,
  62. .pio_mask = ATA_PIO4,
  63. .udma_mask = ATA_UDMA6,
  64. .port_ops = &uli_ops,
  65. };
  66. MODULE_AUTHOR("Peer Chen");
  67. MODULE_DESCRIPTION("low-level driver for ULi Electronics SATA controller");
  68. MODULE_LICENSE("GPL");
  69. MODULE_DEVICE_TABLE(pci, uli_pci_tbl);
  70. MODULE_VERSION(DRV_VERSION);
  71. static unsigned int get_scr_cfg_addr(struct ata_port *ap, unsigned int sc_reg)
  72. {
  73. struct uli_priv *hpriv = ap->host->private_data;
  74. return hpriv->scr_cfg_addr[ap->port_no] + (4 * sc_reg);
  75. }
  76. static u32 uli_scr_cfg_read(struct ata_link *link, unsigned int sc_reg)
  77. {
  78. struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
  79. unsigned int cfg_addr = get_scr_cfg_addr(link->ap, sc_reg);
  80. u32 val;
  81. pci_read_config_dword(pdev, cfg_addr, &val);
  82. return val;
  83. }
  84. static void uli_scr_cfg_write(struct ata_link *link, unsigned int scr, u32 val)
  85. {
  86. struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
  87. unsigned int cfg_addr = get_scr_cfg_addr(link->ap, scr);
  88. pci_write_config_dword(pdev, cfg_addr, val);
  89. }
  90. static int uli_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  91. {
  92. if (sc_reg > SCR_CONTROL)
  93. return -EINVAL;
  94. *val = uli_scr_cfg_read(link, sc_reg);
  95. return 0;
  96. }
  97. static int uli_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  98. {
  99. if (sc_reg > SCR_CONTROL) //SCR_CONTROL=2, SCR_ERROR=1, SCR_STATUS=0
  100. return -EINVAL;
  101. uli_scr_cfg_write(link, sc_reg, val);
  102. return 0;
  103. }
  104. static int uli_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  105. {
  106. const struct ata_port_info *ppi[] = { &uli_port_info, NULL };
  107. unsigned int board_idx = (unsigned int) ent->driver_data;
  108. struct ata_host *host;
  109. struct uli_priv *hpriv;
  110. void __iomem * const *iomap;
  111. struct ata_ioports *ioaddr;
  112. int n_ports, rc;
  113. ata_print_version_once(&pdev->dev, DRV_VERSION);
  114. rc = pcim_enable_device(pdev);
  115. if (rc)
  116. return rc;
  117. n_ports = 2;
  118. if (board_idx == uli_5287)
  119. n_ports = 4;
  120. /* allocate the host */
  121. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  122. if (!host)
  123. return -ENOMEM;
  124. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  125. if (!hpriv)
  126. return -ENOMEM;
  127. host->private_data = hpriv;
  128. /* the first two ports are standard SFF */
  129. rc = ata_pci_sff_init_host(host);
  130. if (rc)
  131. return rc;
  132. ata_pci_bmdma_init(host);
  133. iomap = host->iomap;
  134. switch (board_idx) {
  135. case uli_5287:
  136. /* If there are four, the last two live right after
  137. * the standard SFF ports.
  138. */
  139. hpriv->scr_cfg_addr[0] = ULI5287_BASE;
  140. hpriv->scr_cfg_addr[1] = ULI5287_BASE + ULI5287_OFFS;
  141. ioaddr = &host->ports[2]->ioaddr;
  142. ioaddr->cmd_addr = iomap[0] + 8;
  143. ioaddr->altstatus_addr =
  144. ioaddr->ctl_addr = (void __iomem *)
  145. ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS) + 4;
  146. ioaddr->bmdma_addr = iomap[4] + 16;
  147. hpriv->scr_cfg_addr[2] = ULI5287_BASE + ULI5287_OFFS*4;
  148. ata_sff_std_ports(ioaddr);
  149. ata_port_desc(host->ports[2],
  150. "cmd 0x%llx ctl 0x%llx bmdma 0x%llx",
  151. (unsigned long long)pci_resource_start(pdev, 0) + 8,
  152. ((unsigned long long)pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS) + 4,
  153. (unsigned long long)pci_resource_start(pdev, 4) + 16);
  154. ioaddr = &host->ports[3]->ioaddr;
  155. ioaddr->cmd_addr = iomap[2] + 8;
  156. ioaddr->altstatus_addr =
  157. ioaddr->ctl_addr = (void __iomem *)
  158. ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS) + 4;
  159. ioaddr->bmdma_addr = iomap[4] + 24;
  160. hpriv->scr_cfg_addr[3] = ULI5287_BASE + ULI5287_OFFS*5;
  161. ata_sff_std_ports(ioaddr);
  162. ata_port_desc(host->ports[2],
  163. "cmd 0x%llx ctl 0x%llx bmdma 0x%llx",
  164. (unsigned long long)pci_resource_start(pdev, 2) + 9,
  165. ((unsigned long long)pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS) + 4,
  166. (unsigned long long)pci_resource_start(pdev, 4) + 24);
  167. break;
  168. case uli_5289:
  169. hpriv->scr_cfg_addr[0] = ULI5287_BASE;
  170. hpriv->scr_cfg_addr[1] = ULI5287_BASE + ULI5287_OFFS;
  171. break;
  172. case uli_5281:
  173. hpriv->scr_cfg_addr[0] = ULI5281_BASE;
  174. hpriv->scr_cfg_addr[1] = ULI5281_BASE + ULI5281_OFFS;
  175. break;
  176. default:
  177. BUG();
  178. break;
  179. }
  180. pci_set_master(pdev);
  181. pci_intx(pdev, 1);
  182. return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  183. IRQF_SHARED, &uli_sht);
  184. }
  185. module_pci_driver(uli_pci_driver);