ahci_platform.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. enum ahci_type {
  25. AHCI, /* standard platform ahci */
  26. IMX53_AHCI, /* ahci on i.mx53 */
  27. };
  28. static struct platform_device_id ahci_devtype[] = {
  29. {
  30. .name = "ahci",
  31. .driver_data = AHCI,
  32. }, {
  33. .name = "imx53-ahci",
  34. .driver_data = IMX53_AHCI,
  35. }, {
  36. /* sentinel */
  37. }
  38. };
  39. MODULE_DEVICE_TABLE(platform, ahci_devtype);
  40. static const struct ata_port_info ahci_port_info[] = {
  41. /* by features */
  42. [AHCI] = {
  43. .flags = AHCI_FLAG_COMMON,
  44. .pio_mask = ATA_PIO4,
  45. .udma_mask = ATA_UDMA6,
  46. .port_ops = &ahci_ops,
  47. },
  48. [IMX53_AHCI] = {
  49. .flags = AHCI_FLAG_COMMON,
  50. .pio_mask = ATA_PIO4,
  51. .udma_mask = ATA_UDMA6,
  52. .port_ops = &ahci_pmp_retry_srst_ops,
  53. },
  54. };
  55. static struct scsi_host_template ahci_platform_sht = {
  56. AHCI_SHT("ahci_platform"),
  57. };
  58. static int __init ahci_probe(struct platform_device *pdev)
  59. {
  60. struct device *dev = &pdev->dev;
  61. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  62. const struct platform_device_id *id = platform_get_device_id(pdev);
  63. struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
  64. const struct ata_port_info *ppi[] = { &pi, NULL };
  65. struct ahci_host_priv *hpriv;
  66. struct ata_host *host;
  67. struct resource *mem;
  68. int irq;
  69. int n_ports;
  70. int i;
  71. int rc;
  72. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  73. if (!mem) {
  74. dev_err(dev, "no mmio space\n");
  75. return -EINVAL;
  76. }
  77. irq = platform_get_irq(pdev, 0);
  78. if (irq <= 0) {
  79. dev_err(dev, "no irq\n");
  80. return -EINVAL;
  81. }
  82. if (pdata && pdata->ata_port_info)
  83. pi = *pdata->ata_port_info;
  84. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  85. if (!hpriv) {
  86. dev_err(dev, "can't alloc ahci_host_priv\n");
  87. return -ENOMEM;
  88. }
  89. hpriv->flags |= (unsigned long)pi.private_data;
  90. hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
  91. if (!hpriv->mmio) {
  92. dev_err(dev, "can't map %pR\n", mem);
  93. return -ENOMEM;
  94. }
  95. /*
  96. * Some platforms might need to prepare for mmio region access,
  97. * which could be done in the following init call. So, the mmio
  98. * region shouldn't be accessed before init (if provided) has
  99. * returned successfully.
  100. */
  101. if (pdata && pdata->init) {
  102. rc = pdata->init(dev, hpriv->mmio);
  103. if (rc)
  104. return rc;
  105. }
  106. ahci_save_initial_config(dev, hpriv,
  107. pdata ? pdata->force_port_map : 0,
  108. pdata ? pdata->mask_port_map : 0);
  109. /* prepare host */
  110. if (hpriv->cap & HOST_CAP_NCQ)
  111. pi.flags |= ATA_FLAG_NCQ;
  112. if (hpriv->cap & HOST_CAP_PMP)
  113. pi.flags |= ATA_FLAG_PMP;
  114. ahci_set_em_messages(hpriv, &pi);
  115. /* CAP.NP sometimes indicate the index of the last enabled
  116. * port, at other times, that of the last possible port, so
  117. * determining the maximum port number requires looking at
  118. * both CAP.NP and port_map.
  119. */
  120. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  121. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  122. if (!host) {
  123. rc = -ENOMEM;
  124. goto err0;
  125. }
  126. host->private_data = hpriv;
  127. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  128. host->flags |= ATA_HOST_PARALLEL_SCAN;
  129. else
  130. printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
  131. if (pi.flags & ATA_FLAG_EM)
  132. ahci_reset_em(host);
  133. for (i = 0; i < host->n_ports; i++) {
  134. struct ata_port *ap = host->ports[i];
  135. ata_port_desc(ap, "mmio %pR", mem);
  136. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  137. /* set enclosure management message type */
  138. if (ap->flags & ATA_FLAG_EM)
  139. ap->em_message_type = hpriv->em_msg_type;
  140. /* disabled/not-implemented port */
  141. if (!(hpriv->port_map & (1 << i)))
  142. ap->ops = &ata_dummy_port_ops;
  143. }
  144. rc = ahci_reset_controller(host);
  145. if (rc)
  146. goto err0;
  147. ahci_init_controller(host);
  148. ahci_print_info(host, "platform");
  149. rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  150. &ahci_platform_sht);
  151. if (rc)
  152. goto err0;
  153. return 0;
  154. err0:
  155. if (pdata && pdata->exit)
  156. pdata->exit(dev);
  157. return rc;
  158. }
  159. static int __devexit ahci_remove(struct platform_device *pdev)
  160. {
  161. struct device *dev = &pdev->dev;
  162. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  163. struct ata_host *host = dev_get_drvdata(dev);
  164. ata_host_detach(host);
  165. if (pdata && pdata->exit)
  166. pdata->exit(dev);
  167. return 0;
  168. }
  169. #ifdef CONFIG_PM
  170. static int ahci_suspend(struct device *dev)
  171. {
  172. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  173. struct ata_host *host = dev_get_drvdata(dev);
  174. struct ahci_host_priv *hpriv = host->private_data;
  175. void __iomem *mmio = hpriv->mmio;
  176. u32 ctl;
  177. int rc;
  178. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  179. dev_err(dev, "firmware update required for suspend/resume\n");
  180. return -EIO;
  181. }
  182. /*
  183. * AHCI spec rev1.1 section 8.3.3:
  184. * Software must disable interrupts prior to requesting a
  185. * transition of the HBA to D3 state.
  186. */
  187. ctl = readl(mmio + HOST_CTL);
  188. ctl &= ~HOST_IRQ_EN;
  189. writel(ctl, mmio + HOST_CTL);
  190. readl(mmio + HOST_CTL); /* flush */
  191. rc = ata_host_suspend(host, PMSG_SUSPEND);
  192. if (rc)
  193. return rc;
  194. if (pdata && pdata->suspend)
  195. return pdata->suspend(dev);
  196. return 0;
  197. }
  198. static int ahci_resume(struct device *dev)
  199. {
  200. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  201. struct ata_host *host = dev_get_drvdata(dev);
  202. int rc;
  203. if (pdata && pdata->resume) {
  204. rc = pdata->resume(dev);
  205. if (rc)
  206. return rc;
  207. }
  208. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  209. rc = ahci_reset_controller(host);
  210. if (rc)
  211. return rc;
  212. ahci_init_controller(host);
  213. }
  214. ata_host_resume(host);
  215. return 0;
  216. }
  217. static struct dev_pm_ops ahci_pm_ops = {
  218. .suspend = &ahci_suspend,
  219. .resume = &ahci_resume,
  220. };
  221. #endif
  222. static const struct of_device_id ahci_of_match[] = {
  223. { .compatible = "calxeda,hb-ahci", },
  224. {},
  225. };
  226. MODULE_DEVICE_TABLE(of, ahci_of_match);
  227. static struct platform_driver ahci_driver = {
  228. .remove = __devexit_p(ahci_remove),
  229. .driver = {
  230. .name = "ahci",
  231. .owner = THIS_MODULE,
  232. .of_match_table = ahci_of_match,
  233. #ifdef CONFIG_PM
  234. .pm = &ahci_pm_ops,
  235. #endif
  236. },
  237. .id_table = ahci_devtype,
  238. };
  239. static int __init ahci_init(void)
  240. {
  241. return platform_driver_probe(&ahci_driver, ahci_probe);
  242. }
  243. module_init(ahci_init);
  244. static void __exit ahci_exit(void)
  245. {
  246. platform_driver_unregister(&ahci_driver);
  247. }
  248. module_exit(ahci_exit);
  249. MODULE_DESCRIPTION("AHCI SATA platform driver");
  250. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  251. MODULE_LICENSE("GPL");
  252. MODULE_ALIAS("platform:ahci");