ahci_platform.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * AHCI SATA platform driver
  3. *
  4. * Copyright 2004-2005 Red Hat, Inc.
  5. * Jeff Garzik <jgarzik@pobox.com>
  6. * Copyright 2010 MontaVista Software, LLC.
  7. * Anton Vorontsov <avorontsov@ru.mvista.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/gfp.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/libata.h>
  22. #include <linux/ahci_platform.h>
  23. #include "ahci.h"
  24. static struct scsi_host_template ahci_platform_sht = {
  25. AHCI_SHT("ahci_platform"),
  26. };
  27. static int __init ahci_probe(struct platform_device *pdev)
  28. {
  29. struct device *dev = &pdev->dev;
  30. struct ahci_platform_data *pdata = dev->platform_data;
  31. struct ata_port_info pi = {
  32. .flags = AHCI_FLAG_COMMON,
  33. .pio_mask = ATA_PIO4,
  34. .udma_mask = ATA_UDMA6,
  35. .port_ops = &ahci_ops,
  36. };
  37. const struct ata_port_info *ppi[] = { &pi, NULL };
  38. struct ahci_host_priv *hpriv;
  39. struct ata_host *host;
  40. struct resource *mem;
  41. int irq;
  42. int n_ports;
  43. int i;
  44. int rc;
  45. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  46. if (!mem) {
  47. dev_err(dev, "no mmio space\n");
  48. return -EINVAL;
  49. }
  50. irq = platform_get_irq(pdev, 0);
  51. if (irq <= 0) {
  52. dev_err(dev, "no irq\n");
  53. return -EINVAL;
  54. }
  55. if (pdata && pdata->ata_port_info)
  56. pi = *pdata->ata_port_info;
  57. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  58. if (!hpriv) {
  59. dev_err(dev, "can't alloc ahci_host_priv\n");
  60. return -ENOMEM;
  61. }
  62. hpriv->flags |= (unsigned long)pi.private_data;
  63. hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
  64. if (!hpriv->mmio) {
  65. dev_err(dev, "can't map %pR\n", mem);
  66. return -ENOMEM;
  67. }
  68. /*
  69. * Some platforms might need to prepare for mmio region access,
  70. * which could be done in the following init call. So, the mmio
  71. * region shouldn't be accessed before init (if provided) has
  72. * returned successfully.
  73. */
  74. if (pdata && pdata->init) {
  75. rc = pdata->init(dev, hpriv->mmio);
  76. if (rc)
  77. return rc;
  78. }
  79. ahci_save_initial_config(dev, hpriv,
  80. pdata ? pdata->force_port_map : 0,
  81. pdata ? pdata->mask_port_map : 0);
  82. /* prepare host */
  83. if (hpriv->cap & HOST_CAP_NCQ)
  84. pi.flags |= ATA_FLAG_NCQ;
  85. if (hpriv->cap & HOST_CAP_PMP)
  86. pi.flags |= ATA_FLAG_PMP;
  87. ahci_set_em_messages(hpriv, &pi);
  88. /* CAP.NP sometimes indicate the index of the last enabled
  89. * port, at other times, that of the last possible port, so
  90. * determining the maximum port number requires looking at
  91. * both CAP.NP and port_map.
  92. */
  93. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  94. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  95. if (!host) {
  96. rc = -ENOMEM;
  97. goto err0;
  98. }
  99. host->private_data = hpriv;
  100. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  101. host->flags |= ATA_HOST_PARALLEL_SCAN;
  102. else
  103. printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
  104. if (pi.flags & ATA_FLAG_EM)
  105. ahci_reset_em(host);
  106. for (i = 0; i < host->n_ports; i++) {
  107. struct ata_port *ap = host->ports[i];
  108. ata_port_desc(ap, "mmio %pR", mem);
  109. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  110. /* set enclosure management message type */
  111. if (ap->flags & ATA_FLAG_EM)
  112. ap->em_message_type = hpriv->em_msg_type;
  113. /* disabled/not-implemented port */
  114. if (!(hpriv->port_map & (1 << i)))
  115. ap->ops = &ata_dummy_port_ops;
  116. }
  117. rc = ahci_reset_controller(host);
  118. if (rc)
  119. goto err0;
  120. ahci_init_controller(host);
  121. ahci_print_info(host, "platform");
  122. rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  123. &ahci_platform_sht);
  124. if (rc)
  125. goto err0;
  126. return 0;
  127. err0:
  128. if (pdata && pdata->exit)
  129. pdata->exit(dev);
  130. return rc;
  131. }
  132. static int __devexit ahci_remove(struct platform_device *pdev)
  133. {
  134. struct device *dev = &pdev->dev;
  135. struct ahci_platform_data *pdata = dev->platform_data;
  136. struct ata_host *host = dev_get_drvdata(dev);
  137. ata_host_detach(host);
  138. if (pdata && pdata->exit)
  139. pdata->exit(dev);
  140. return 0;
  141. }
  142. static struct platform_driver ahci_driver = {
  143. .remove = __devexit_p(ahci_remove),
  144. .driver = {
  145. .name = "ahci",
  146. .owner = THIS_MODULE,
  147. },
  148. };
  149. static int __init ahci_init(void)
  150. {
  151. return platform_driver_probe(&ahci_driver, ahci_probe);
  152. }
  153. module_init(ahci_init);
  154. static void __exit ahci_exit(void)
  155. {
  156. platform_driver_unregister(&ahci_driver);
  157. }
  158. module_exit(ahci_exit);
  159. MODULE_DESCRIPTION("AHCI SATA platform driver");
  160. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  161. MODULE_LICENSE("GPL");
  162. MODULE_ALIAS("platform:ahci");