sata_uli.c 6.6 KB

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