ahci_platform.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 int __init ahci_probe(struct platform_device *pdev)
  25. {
  26. struct device *dev = &pdev->dev;
  27. struct ahci_platform_data *pdata = dev->platform_data;
  28. struct ata_port_info pi = {
  29. .flags = AHCI_FLAG_COMMON,
  30. .pio_mask = ATA_PIO4,
  31. .udma_mask = ATA_UDMA6,
  32. .port_ops = &ahci_ops,
  33. };
  34. const struct ata_port_info *ppi[] = { &pi, NULL };
  35. struct ahci_host_priv *hpriv;
  36. struct ata_host *host;
  37. struct resource *mem;
  38. int irq;
  39. int n_ports;
  40. int i;
  41. int rc;
  42. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  43. if (!mem) {
  44. dev_err(dev, "no mmio space\n");
  45. return -EINVAL;
  46. }
  47. irq = platform_get_irq(pdev, 0);
  48. if (irq <= 0) {
  49. dev_err(dev, "no irq\n");
  50. return -EINVAL;
  51. }
  52. if (pdata && pdata->init) {
  53. rc = pdata->init(dev);
  54. if (rc)
  55. return rc;
  56. }
  57. if (pdata && pdata->ata_port_info)
  58. pi = *pdata->ata_port_info;
  59. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  60. if (!hpriv) {
  61. rc = -ENOMEM;
  62. goto err0;
  63. }
  64. hpriv->flags |= (unsigned long)pi.private_data;
  65. hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
  66. if (!hpriv->mmio) {
  67. dev_err(dev, "can't map %pR\n", mem);
  68. rc = -ENOMEM;
  69. goto err0;
  70. }
  71. ahci_save_initial_config(dev, hpriv,
  72. pdata ? pdata->force_port_map : 0,
  73. pdata ? pdata->mask_port_map : 0);
  74. /* prepare host */
  75. if (hpriv->cap & HOST_CAP_NCQ)
  76. pi.flags |= ATA_FLAG_NCQ;
  77. if (hpriv->cap & HOST_CAP_PMP)
  78. pi.flags |= ATA_FLAG_PMP;
  79. ahci_set_em_messages(hpriv, &pi);
  80. /* CAP.NP sometimes indicate the index of the last enabled
  81. * port, at other times, that of the last possible port, so
  82. * determining the maximum port number requires looking at
  83. * both CAP.NP and port_map.
  84. */
  85. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  86. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  87. if (!host) {
  88. rc = -ENOMEM;
  89. goto err0;
  90. }
  91. host->private_data = hpriv;
  92. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  93. host->flags |= ATA_HOST_PARALLEL_SCAN;
  94. else
  95. printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
  96. if (pi.flags & ATA_FLAG_EM)
  97. ahci_reset_em(host);
  98. for (i = 0; i < host->n_ports; i++) {
  99. struct ata_port *ap = host->ports[i];
  100. ata_port_desc(ap, "mmio %pR", mem);
  101. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  102. /* set initial link pm policy */
  103. ap->pm_policy = NOT_AVAILABLE;
  104. /* set enclosure management message type */
  105. if (ap->flags & ATA_FLAG_EM)
  106. ap->em_message_type = hpriv->em_msg_type;
  107. /* disabled/not-implemented port */
  108. if (!(hpriv->port_map & (1 << i)))
  109. ap->ops = &ata_dummy_port_ops;
  110. }
  111. rc = ahci_reset_controller(host);
  112. if (rc)
  113. goto err0;
  114. ahci_init_controller(host);
  115. ahci_print_info(host, "platform");
  116. rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  117. &ahci_sht);
  118. if (rc)
  119. goto err0;
  120. return 0;
  121. err0:
  122. if (pdata && pdata->exit)
  123. pdata->exit(dev);
  124. return rc;
  125. }
  126. static int __devexit ahci_remove(struct platform_device *pdev)
  127. {
  128. struct device *dev = &pdev->dev;
  129. struct ahci_platform_data *pdata = dev->platform_data;
  130. struct ata_host *host = dev_get_drvdata(dev);
  131. ata_host_detach(host);
  132. if (pdata && pdata->exit)
  133. pdata->exit(dev);
  134. return 0;
  135. }
  136. static struct platform_driver ahci_driver = {
  137. .probe = ahci_probe,
  138. .remove = __devexit_p(ahci_remove),
  139. .driver = {
  140. .name = "ahci",
  141. .owner = THIS_MODULE,
  142. },
  143. };
  144. static int __init ahci_init(void)
  145. {
  146. return platform_driver_probe(&ahci_driver, ahci_probe);
  147. }
  148. module_init(ahci_init);
  149. static void __exit ahci_exit(void)
  150. {
  151. platform_driver_unregister(&ahci_driver);
  152. }
  153. module_exit(ahci_exit);
  154. MODULE_DESCRIPTION("AHCI SATA platform driver");
  155. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  156. MODULE_LICENSE("GPL");
  157. MODULE_ALIAS("platform:ahci");