ahci_platform.c 8.1 KB

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