ahci_platform.c 7.1 KB

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