libahci_platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * AHCI SATA platform library
  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/interrupt.h>
  20. #include <linux/device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/libata.h>
  23. #include <linux/ahci_platform.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/pm_runtime.h>
  26. #include "ahci.h"
  27. static void ahci_host_stop(struct ata_host *host);
  28. struct ata_port_operations ahci_platform_ops = {
  29. .inherits = &ahci_ops,
  30. .host_stop = ahci_host_stop,
  31. };
  32. EXPORT_SYMBOL_GPL(ahci_platform_ops);
  33. static struct scsi_host_template ahci_platform_sht = {
  34. AHCI_SHT("ahci_platform"),
  35. };
  36. /**
  37. * ahci_platform_enable_clks - Enable platform clocks
  38. * @hpriv: host private area to store config values
  39. *
  40. * This function enables all the clks found in hpriv->clks, starting at
  41. * index 0. If any clk fails to enable it disables all the clks already
  42. * enabled in reverse order, and then returns an error.
  43. *
  44. * RETURNS:
  45. * 0 on success otherwise a negative error code
  46. */
  47. int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
  48. {
  49. int c, rc;
  50. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
  51. rc = clk_prepare_enable(hpriv->clks[c]);
  52. if (rc)
  53. goto disable_unprepare_clk;
  54. }
  55. return 0;
  56. disable_unprepare_clk:
  57. while (--c >= 0)
  58. clk_disable_unprepare(hpriv->clks[c]);
  59. return rc;
  60. }
  61. EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
  62. /**
  63. * ahci_platform_disable_clks - Disable platform clocks
  64. * @hpriv: host private area to store config values
  65. *
  66. * This function disables all the clks found in hpriv->clks, in reverse
  67. * order of ahci_platform_enable_clks (starting at the end of the array).
  68. */
  69. void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
  70. {
  71. int c;
  72. for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
  73. if (hpriv->clks[c])
  74. clk_disable_unprepare(hpriv->clks[c]);
  75. }
  76. EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
  77. /**
  78. * ahci_platform_enable_resources - Enable platform resources
  79. * @hpriv: host private area to store config values
  80. *
  81. * This function enables all ahci_platform managed resources in the
  82. * following order:
  83. * 1) Regulator
  84. * 2) Clocks (through ahci_platform_enable_clks)
  85. * 3) Phy
  86. *
  87. * If resource enabling fails at any point the previous enabled resources
  88. * are disabled in reverse order.
  89. *
  90. * RETURNS:
  91. * 0 on success otherwise a negative error code
  92. */
  93. int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
  94. {
  95. int rc;
  96. if (hpriv->target_pwr) {
  97. rc = regulator_enable(hpriv->target_pwr);
  98. if (rc)
  99. return rc;
  100. }
  101. rc = ahci_platform_enable_clks(hpriv);
  102. if (rc)
  103. goto disable_regulator;
  104. if (hpriv->phy) {
  105. rc = phy_init(hpriv->phy);
  106. if (rc)
  107. goto disable_clks;
  108. rc = phy_power_on(hpriv->phy);
  109. if (rc) {
  110. phy_exit(hpriv->phy);
  111. goto disable_clks;
  112. }
  113. }
  114. return 0;
  115. disable_clks:
  116. ahci_platform_disable_clks(hpriv);
  117. disable_regulator:
  118. if (hpriv->target_pwr)
  119. regulator_disable(hpriv->target_pwr);
  120. return rc;
  121. }
  122. EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
  123. /**
  124. * ahci_platform_disable_resources - Disable platform resources
  125. * @hpriv: host private area to store config values
  126. *
  127. * This function disables all ahci_platform managed resources in the
  128. * following order:
  129. * 1) Phy
  130. * 2) Clocks (through ahci_platform_disable_clks)
  131. * 3) Regulator
  132. */
  133. void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
  134. {
  135. if (hpriv->phy) {
  136. phy_power_off(hpriv->phy);
  137. phy_exit(hpriv->phy);
  138. }
  139. ahci_platform_disable_clks(hpriv);
  140. if (hpriv->target_pwr)
  141. regulator_disable(hpriv->target_pwr);
  142. }
  143. EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
  144. static void ahci_platform_put_resources(struct device *dev, void *res)
  145. {
  146. struct ahci_host_priv *hpriv = res;
  147. int c;
  148. if (hpriv->got_runtime_pm) {
  149. pm_runtime_put_sync(dev);
  150. pm_runtime_disable(dev);
  151. }
  152. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
  153. clk_put(hpriv->clks[c]);
  154. }
  155. /**
  156. * ahci_platform_get_resources - Get platform resources
  157. * @pdev: platform device to get resources for
  158. *
  159. * This function allocates an ahci_host_priv struct, and gets the following
  160. * resources, storing a reference to them inside the returned struct:
  161. *
  162. * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
  163. * 2) regulator for controlling the targets power (optional)
  164. * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
  165. * or for non devicetree enabled platforms a single clock
  166. * 4) phy (optional)
  167. *
  168. * RETURNS:
  169. * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
  170. */
  171. struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. struct ahci_host_priv *hpriv;
  175. struct clk *clk;
  176. int i, rc = -ENOMEM;
  177. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  178. return ERR_PTR(-ENOMEM);
  179. hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
  180. GFP_KERNEL);
  181. if (!hpriv)
  182. goto err_out;
  183. devres_add(dev, hpriv);
  184. hpriv->mmio = devm_ioremap_resource(dev,
  185. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  186. if (IS_ERR(hpriv->mmio)) {
  187. dev_err(dev, "no mmio space\n");
  188. rc = PTR_ERR(hpriv->mmio);
  189. goto err_out;
  190. }
  191. hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
  192. if (IS_ERR(hpriv->target_pwr)) {
  193. rc = PTR_ERR(hpriv->target_pwr);
  194. if (rc == -EPROBE_DEFER)
  195. goto err_out;
  196. hpriv->target_pwr = NULL;
  197. }
  198. for (i = 0; i < AHCI_MAX_CLKS; i++) {
  199. /*
  200. * For now we must use clk_get(dev, NULL) for the first clock,
  201. * because some platforms (da850, spear13xx) are not yet
  202. * converted to use devicetree for clocks. For new platforms
  203. * this is equivalent to of_clk_get(dev->of_node, 0).
  204. */
  205. if (i == 0)
  206. clk = clk_get(dev, NULL);
  207. else
  208. clk = of_clk_get(dev->of_node, i);
  209. if (IS_ERR(clk)) {
  210. rc = PTR_ERR(clk);
  211. if (rc == -EPROBE_DEFER)
  212. goto err_out;
  213. break;
  214. }
  215. hpriv->clks[i] = clk;
  216. }
  217. hpriv->phy = devm_phy_get(dev, "sata-phy");
  218. if (IS_ERR(hpriv->phy)) {
  219. rc = PTR_ERR(hpriv->phy);
  220. switch (rc) {
  221. case -ENODEV:
  222. case -ENOSYS:
  223. /* continue normally */
  224. hpriv->phy = NULL;
  225. break;
  226. case -EPROBE_DEFER:
  227. goto err_out;
  228. default:
  229. dev_err(dev, "couldn't get sata-phy\n");
  230. goto err_out;
  231. }
  232. }
  233. pm_runtime_enable(dev);
  234. pm_runtime_get_sync(dev);
  235. hpriv->got_runtime_pm = true;
  236. devres_remove_group(dev, NULL);
  237. return hpriv;
  238. err_out:
  239. devres_release_group(dev, NULL);
  240. return ERR_PTR(rc);
  241. }
  242. EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
  243. /**
  244. * ahci_platform_init_host - Bring up an ahci-platform host
  245. * @pdev: platform device pointer for the host
  246. * @hpriv: ahci-host private data for the host
  247. * @pi_template: template for the ata_port_info to use
  248. * @force_port_map: param passed to ahci_save_initial_config
  249. * @mask_port_map: param passed to ahci_save_initial_config
  250. *
  251. * This function does all the usual steps needed to bring up an
  252. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  253. * must be initialized / enabled before calling this.
  254. *
  255. * RETURNS:
  256. * 0 on success otherwise a negative error code
  257. */
  258. int ahci_platform_init_host(struct platform_device *pdev,
  259. struct ahci_host_priv *hpriv,
  260. const struct ata_port_info *pi_template,
  261. unsigned int force_port_map,
  262. unsigned int mask_port_map)
  263. {
  264. struct device *dev = &pdev->dev;
  265. struct ata_port_info pi = *pi_template;
  266. const struct ata_port_info *ppi[] = { &pi, NULL };
  267. struct ata_host *host;
  268. int i, irq, n_ports, rc;
  269. irq = platform_get_irq(pdev, 0);
  270. if (irq <= 0) {
  271. dev_err(dev, "no irq\n");
  272. return -EINVAL;
  273. }
  274. /* prepare host */
  275. hpriv->flags |= (unsigned long)pi.private_data;
  276. ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
  277. if (hpriv->cap & HOST_CAP_NCQ)
  278. pi.flags |= ATA_FLAG_NCQ;
  279. if (hpriv->cap & HOST_CAP_PMP)
  280. pi.flags |= ATA_FLAG_PMP;
  281. ahci_set_em_messages(hpriv, &pi);
  282. /* CAP.NP sometimes indicate the index of the last enabled
  283. * port, at other times, that of the last possible port, so
  284. * determining the maximum port number requires looking at
  285. * both CAP.NP and port_map.
  286. */
  287. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  288. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  289. if (!host)
  290. return -ENOMEM;
  291. host->private_data = hpriv;
  292. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  293. host->flags |= ATA_HOST_PARALLEL_SCAN;
  294. else
  295. dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
  296. if (pi.flags & ATA_FLAG_EM)
  297. ahci_reset_em(host);
  298. for (i = 0; i < host->n_ports; i++) {
  299. struct ata_port *ap = host->ports[i];
  300. ata_port_desc(ap, "mmio %pR",
  301. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  302. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  303. /* set enclosure management message type */
  304. if (ap->flags & ATA_FLAG_EM)
  305. ap->em_message_type = hpriv->em_msg_type;
  306. /* disabled/not-implemented port */
  307. if (!(hpriv->port_map & (1 << i)))
  308. ap->ops = &ata_dummy_port_ops;
  309. }
  310. rc = ahci_reset_controller(host);
  311. if (rc)
  312. return rc;
  313. ahci_init_controller(host);
  314. ahci_print_info(host, "platform");
  315. return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  316. &ahci_platform_sht);
  317. }
  318. EXPORT_SYMBOL_GPL(ahci_platform_init_host);
  319. static void ahci_host_stop(struct ata_host *host)
  320. {
  321. struct device *dev = host->dev;
  322. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  323. struct ahci_host_priv *hpriv = host->private_data;
  324. if (pdata && pdata->exit)
  325. pdata->exit(dev);
  326. ahci_platform_disable_resources(hpriv);
  327. }
  328. #ifdef CONFIG_PM_SLEEP
  329. /**
  330. * ahci_platform_suspend_host - Suspend an ahci-platform host
  331. * @dev: device pointer for the host
  332. *
  333. * This function does all the usual steps needed to suspend an
  334. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  335. * must be disabled after calling this.
  336. *
  337. * RETURNS:
  338. * 0 on success otherwise a negative error code
  339. */
  340. int ahci_platform_suspend_host(struct device *dev)
  341. {
  342. struct ata_host *host = dev_get_drvdata(dev);
  343. struct ahci_host_priv *hpriv = host->private_data;
  344. void __iomem *mmio = hpriv->mmio;
  345. u32 ctl;
  346. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  347. dev_err(dev, "firmware update required for suspend/resume\n");
  348. return -EIO;
  349. }
  350. /*
  351. * AHCI spec rev1.1 section 8.3.3:
  352. * Software must disable interrupts prior to requesting a
  353. * transition of the HBA to D3 state.
  354. */
  355. ctl = readl(mmio + HOST_CTL);
  356. ctl &= ~HOST_IRQ_EN;
  357. writel(ctl, mmio + HOST_CTL);
  358. readl(mmio + HOST_CTL); /* flush */
  359. return ata_host_suspend(host, PMSG_SUSPEND);
  360. }
  361. EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
  362. /**
  363. * ahci_platform_resume_host - Resume an ahci-platform host
  364. * @dev: device pointer for the host
  365. *
  366. * This function does all the usual steps needed to resume an ahci-platform
  367. * host, note any necessary resources (ie clks, phy, etc.) must be
  368. * initialized / enabled before calling this.
  369. *
  370. * RETURNS:
  371. * 0 on success otherwise a negative error code
  372. */
  373. int ahci_platform_resume_host(struct device *dev)
  374. {
  375. struct ata_host *host = dev_get_drvdata(dev);
  376. int rc;
  377. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  378. rc = ahci_reset_controller(host);
  379. if (rc)
  380. return rc;
  381. ahci_init_controller(host);
  382. }
  383. ata_host_resume(host);
  384. return 0;
  385. }
  386. EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
  387. /**
  388. * ahci_platform_suspend - Suspend an ahci-platform device
  389. * @dev: the platform device to suspend
  390. *
  391. * This function suspends the host associated with the device, followed by
  392. * disabling all the resources of the device.
  393. *
  394. * RETURNS:
  395. * 0 on success otherwise a negative error code
  396. */
  397. int ahci_platform_suspend(struct device *dev)
  398. {
  399. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  400. struct ata_host *host = dev_get_drvdata(dev);
  401. struct ahci_host_priv *hpriv = host->private_data;
  402. int rc;
  403. rc = ahci_platform_suspend_host(dev);
  404. if (rc)
  405. return rc;
  406. if (pdata && pdata->suspend) {
  407. rc = pdata->suspend(dev);
  408. if (rc)
  409. goto resume_host;
  410. }
  411. ahci_platform_disable_resources(hpriv);
  412. return 0;
  413. resume_host:
  414. ahci_platform_resume_host(dev);
  415. return rc;
  416. }
  417. EXPORT_SYMBOL_GPL(ahci_platform_suspend);
  418. /**
  419. * ahci_platform_resume - Resume an ahci-platform device
  420. * @dev: the platform device to resume
  421. *
  422. * This function enables all the resources of the device followed by
  423. * resuming the host associated with the device.
  424. *
  425. * RETURNS:
  426. * 0 on success otherwise a negative error code
  427. */
  428. int ahci_platform_resume(struct device *dev)
  429. {
  430. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  431. struct ata_host *host = dev_get_drvdata(dev);
  432. struct ahci_host_priv *hpriv = host->private_data;
  433. int rc;
  434. rc = ahci_platform_enable_resources(hpriv);
  435. if (rc)
  436. return rc;
  437. if (pdata && pdata->resume) {
  438. rc = pdata->resume(dev);
  439. if (rc)
  440. goto disable_resources;
  441. }
  442. rc = ahci_platform_resume_host(dev);
  443. if (rc)
  444. goto disable_resources;
  445. /* We resumed so update PM runtime state */
  446. pm_runtime_disable(dev);
  447. pm_runtime_set_active(dev);
  448. pm_runtime_enable(dev);
  449. return 0;
  450. disable_resources:
  451. ahci_platform_disable_resources(hpriv);
  452. return rc;
  453. }
  454. EXPORT_SYMBOL_GPL(ahci_platform_resume);
  455. #endif
  456. MODULE_DESCRIPTION("AHCI SATA platform library");
  457. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  458. MODULE_LICENSE("GPL");