ahci_platform.c 4.3 KB

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