libahci_platform.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 <linux/of_platform.h>
  27. #include <linux/reset.h>
  28. #include "ahci.h"
  29. static void ahci_host_stop(struct ata_host *host);
  30. struct ata_port_operations ahci_platform_ops = {
  31. .inherits = &ahci_ops,
  32. .host_stop = ahci_host_stop,
  33. };
  34. EXPORT_SYMBOL_GPL(ahci_platform_ops);
  35. /**
  36. * ahci_platform_enable_phys - Enable PHYs
  37. * @hpriv: host private area to store config values
  38. *
  39. * This function enables all the PHYs found in hpriv->phys, if any.
  40. * If a PHY fails to be enabled, it disables all the PHYs already
  41. * enabled in reverse order and returns an error.
  42. *
  43. * RETURNS:
  44. * 0 on success otherwise a negative error code
  45. */
  46. static int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
  47. {
  48. int rc, i;
  49. for (i = 0; i < hpriv->nports; i++) {
  50. rc = phy_init(hpriv->phys[i]);
  51. if (rc)
  52. goto disable_phys;
  53. rc = phy_power_on(hpriv->phys[i]);
  54. if (rc) {
  55. phy_exit(hpriv->phys[i]);
  56. goto disable_phys;
  57. }
  58. }
  59. return 0;
  60. disable_phys:
  61. while (--i >= 0) {
  62. phy_power_off(hpriv->phys[i]);
  63. phy_exit(hpriv->phys[i]);
  64. }
  65. return rc;
  66. }
  67. /**
  68. * ahci_platform_disable_phys - Disable PHYs
  69. * @hpriv: host private area to store config values
  70. *
  71. * This function disables all PHYs found in hpriv->phys.
  72. */
  73. static void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
  74. {
  75. int i;
  76. for (i = 0; i < hpriv->nports; i++) {
  77. phy_power_off(hpriv->phys[i]);
  78. phy_exit(hpriv->phys[i]);
  79. }
  80. }
  81. /**
  82. * ahci_platform_enable_clks - Enable platform clocks
  83. * @hpriv: host private area to store config values
  84. *
  85. * This function enables all the clks found in hpriv->clks, starting at
  86. * index 0. If any clk fails to enable it disables all the clks already
  87. * enabled in reverse order, and then returns an error.
  88. *
  89. * RETURNS:
  90. * 0 on success otherwise a negative error code
  91. */
  92. int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
  93. {
  94. int c, rc;
  95. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
  96. rc = clk_prepare_enable(hpriv->clks[c]);
  97. if (rc)
  98. goto disable_unprepare_clk;
  99. }
  100. return 0;
  101. disable_unprepare_clk:
  102. while (--c >= 0)
  103. clk_disable_unprepare(hpriv->clks[c]);
  104. return rc;
  105. }
  106. EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
  107. /**
  108. * ahci_platform_disable_clks - Disable platform clocks
  109. * @hpriv: host private area to store config values
  110. *
  111. * This function disables all the clks found in hpriv->clks, in reverse
  112. * order of ahci_platform_enable_clks (starting at the end of the array).
  113. */
  114. void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
  115. {
  116. int c;
  117. for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
  118. if (hpriv->clks[c])
  119. clk_disable_unprepare(hpriv->clks[c]);
  120. }
  121. EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
  122. /**
  123. * ahci_platform_enable_regulators - Enable regulators
  124. * @hpriv: host private area to store config values
  125. *
  126. * This function enables all the regulators found in
  127. * hpriv->target_pwrs, if any. If a regulator fails to be enabled, it
  128. * disables all the regulators already enabled in reverse order and
  129. * returns an error.
  130. *
  131. * RETURNS:
  132. * 0 on success otherwise a negative error code
  133. */
  134. int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv)
  135. {
  136. int rc, i;
  137. for (i = 0; i < hpriv->nports; i++) {
  138. if (!hpriv->target_pwrs[i])
  139. continue;
  140. rc = regulator_enable(hpriv->target_pwrs[i]);
  141. if (rc)
  142. goto disable_target_pwrs;
  143. }
  144. return 0;
  145. disable_target_pwrs:
  146. while (--i >= 0)
  147. if (hpriv->target_pwrs[i])
  148. regulator_disable(hpriv->target_pwrs[i]);
  149. return rc;
  150. }
  151. EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators);
  152. /**
  153. * ahci_platform_disable_regulators - Disable regulators
  154. * @hpriv: host private area to store config values
  155. *
  156. * This function disables all regulators found in hpriv->target_pwrs.
  157. */
  158. void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv)
  159. {
  160. int i;
  161. for (i = 0; i < hpriv->nports; i++) {
  162. if (!hpriv->target_pwrs[i])
  163. continue;
  164. regulator_disable(hpriv->target_pwrs[i]);
  165. }
  166. }
  167. EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators);
  168. /**
  169. * ahci_platform_enable_resources - Enable platform resources
  170. * @hpriv: host private area to store config values
  171. *
  172. * This function enables all ahci_platform managed resources in the
  173. * following order:
  174. * 1) Regulator
  175. * 2) Clocks (through ahci_platform_enable_clks)
  176. * 3) Resets
  177. * 4) Phys
  178. *
  179. * If resource enabling fails at any point the previous enabled resources
  180. * are disabled in reverse order.
  181. *
  182. * RETURNS:
  183. * 0 on success otherwise a negative error code
  184. */
  185. int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
  186. {
  187. int rc;
  188. rc = ahci_platform_enable_regulators(hpriv);
  189. if (rc)
  190. return rc;
  191. rc = ahci_platform_enable_clks(hpriv);
  192. if (rc)
  193. goto disable_regulator;
  194. rc = reset_control_deassert(hpriv->rsts);
  195. if (rc)
  196. goto disable_clks;
  197. rc = ahci_platform_enable_phys(hpriv);
  198. if (rc)
  199. goto disable_resets;
  200. return 0;
  201. disable_resets:
  202. reset_control_assert(hpriv->rsts);
  203. disable_clks:
  204. ahci_platform_disable_clks(hpriv);
  205. disable_regulator:
  206. ahci_platform_disable_regulators(hpriv);
  207. return rc;
  208. }
  209. EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
  210. /**
  211. * ahci_platform_disable_resources - Disable platform resources
  212. * @hpriv: host private area to store config values
  213. *
  214. * This function disables all ahci_platform managed resources in the
  215. * following order:
  216. * 1) Phys
  217. * 2) Resets
  218. * 3) Clocks (through ahci_platform_disable_clks)
  219. * 4) Regulator
  220. */
  221. void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
  222. {
  223. ahci_platform_disable_phys(hpriv);
  224. reset_control_assert(hpriv->rsts);
  225. ahci_platform_disable_clks(hpriv);
  226. ahci_platform_disable_regulators(hpriv);
  227. }
  228. EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
  229. static void ahci_platform_put_resources(struct device *dev, void *res)
  230. {
  231. struct ahci_host_priv *hpriv = res;
  232. int c;
  233. if (hpriv->got_runtime_pm) {
  234. pm_runtime_put_sync(dev);
  235. pm_runtime_disable(dev);
  236. }
  237. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
  238. clk_put(hpriv->clks[c]);
  239. /*
  240. * The regulators are tied to child node device and not to the
  241. * SATA device itself. So we can't use devm for automatically
  242. * releasing them. We have to do it manually here.
  243. */
  244. for (c = 0; c < hpriv->nports; c++)
  245. if (hpriv->target_pwrs && hpriv->target_pwrs[c])
  246. regulator_put(hpriv->target_pwrs[c]);
  247. kfree(hpriv->target_pwrs);
  248. }
  249. static int ahci_platform_get_phy(struct ahci_host_priv *hpriv, u32 port,
  250. struct device *dev, struct device_node *node)
  251. {
  252. int rc;
  253. hpriv->phys[port] = devm_of_phy_get(dev, node, NULL);
  254. if (!IS_ERR(hpriv->phys[port]))
  255. return 0;
  256. rc = PTR_ERR(hpriv->phys[port]);
  257. switch (rc) {
  258. case -ENOSYS:
  259. /* No PHY support. Check if PHY is required. */
  260. if (of_find_property(node, "phys", NULL)) {
  261. dev_err(dev,
  262. "couldn't get PHY in node %s: ENOSYS\n",
  263. node->name);
  264. break;
  265. }
  266. /* fall through */
  267. case -ENODEV:
  268. /* continue normally */
  269. hpriv->phys[port] = NULL;
  270. rc = 0;
  271. break;
  272. default:
  273. dev_err(dev,
  274. "couldn't get PHY in node %s: %d\n",
  275. node->name, rc);
  276. break;
  277. }
  278. return rc;
  279. }
  280. static int ahci_platform_get_regulator(struct ahci_host_priv *hpriv, u32 port,
  281. struct device *dev)
  282. {
  283. struct regulator *target_pwr;
  284. int rc = 0;
  285. target_pwr = regulator_get_optional(dev, "target");
  286. if (!IS_ERR(target_pwr))
  287. hpriv->target_pwrs[port] = target_pwr;
  288. else
  289. rc = PTR_ERR(target_pwr);
  290. return rc;
  291. }
  292. /**
  293. * ahci_platform_get_resources - Get platform resources
  294. * @pdev: platform device to get resources for
  295. * @flags: bitmap representing the resource to get
  296. *
  297. * This function allocates an ahci_host_priv struct, and gets the following
  298. * resources, storing a reference to them inside the returned struct:
  299. *
  300. * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
  301. * 2) regulator for controlling the targets power (optional)
  302. * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
  303. * or for non devicetree enabled platforms a single clock
  304. * 4) resets, if flags has AHCI_PLATFORM_GET_RESETS (optional)
  305. * 5) phys (optional)
  306. *
  307. * RETURNS:
  308. * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
  309. */
  310. struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
  311. unsigned int flags)
  312. {
  313. struct device *dev = &pdev->dev;
  314. struct ahci_host_priv *hpriv;
  315. struct clk *clk;
  316. struct device_node *child;
  317. int i, enabled_ports = 0, rc = -ENOMEM, child_nodes;
  318. u32 mask_port_map = 0;
  319. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  320. return ERR_PTR(-ENOMEM);
  321. hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
  322. GFP_KERNEL);
  323. if (!hpriv)
  324. goto err_out;
  325. devres_add(dev, hpriv);
  326. hpriv->mmio = devm_ioremap_resource(dev,
  327. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  328. if (IS_ERR(hpriv->mmio)) {
  329. dev_err(dev, "no mmio space\n");
  330. rc = PTR_ERR(hpriv->mmio);
  331. goto err_out;
  332. }
  333. for (i = 0; i < AHCI_MAX_CLKS; i++) {
  334. /*
  335. * For now we must use clk_get(dev, NULL) for the first clock,
  336. * because some platforms (da850, spear13xx) are not yet
  337. * converted to use devicetree for clocks. For new platforms
  338. * this is equivalent to of_clk_get(dev->of_node, 0).
  339. */
  340. if (i == 0)
  341. clk = clk_get(dev, NULL);
  342. else
  343. clk = of_clk_get(dev->of_node, i);
  344. if (IS_ERR(clk)) {
  345. rc = PTR_ERR(clk);
  346. if (rc == -EPROBE_DEFER)
  347. goto err_out;
  348. break;
  349. }
  350. hpriv->clks[i] = clk;
  351. }
  352. if (flags & AHCI_PLATFORM_GET_RESETS) {
  353. hpriv->rsts = devm_reset_control_array_get_optional_shared(dev);
  354. if (IS_ERR(hpriv->rsts)) {
  355. rc = PTR_ERR(hpriv->rsts);
  356. goto err_out;
  357. }
  358. }
  359. hpriv->nports = child_nodes = of_get_child_count(dev->of_node);
  360. /*
  361. * If no sub-node was found, we still need to set nports to
  362. * one in order to be able to use the
  363. * ahci_platform_[en|dis]able_[phys|regulators] functions.
  364. */
  365. if (!child_nodes)
  366. hpriv->nports = 1;
  367. hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL);
  368. if (!hpriv->phys) {
  369. rc = -ENOMEM;
  370. goto err_out;
  371. }
  372. /*
  373. * We cannot use devm_ here, since ahci_platform_put_resources() uses
  374. * target_pwrs after devm_ have freed memory
  375. */
  376. hpriv->target_pwrs = kcalloc(hpriv->nports, sizeof(*hpriv->target_pwrs), GFP_KERNEL);
  377. if (!hpriv->target_pwrs) {
  378. rc = -ENOMEM;
  379. goto err_out;
  380. }
  381. if (child_nodes) {
  382. for_each_child_of_node(dev->of_node, child) {
  383. u32 port;
  384. struct platform_device *port_dev __maybe_unused;
  385. if (!of_device_is_available(child))
  386. continue;
  387. if (of_property_read_u32(child, "reg", &port)) {
  388. rc = -EINVAL;
  389. goto err_out;
  390. }
  391. if (port >= hpriv->nports) {
  392. dev_warn(dev, "invalid port number %d\n", port);
  393. continue;
  394. }
  395. mask_port_map |= BIT(port);
  396. #ifdef CONFIG_OF_ADDRESS
  397. of_platform_device_create(child, NULL, NULL);
  398. port_dev = of_find_device_by_node(child);
  399. if (port_dev) {
  400. rc = ahci_platform_get_regulator(hpriv, port,
  401. &port_dev->dev);
  402. if (rc == -EPROBE_DEFER)
  403. goto err_out;
  404. }
  405. #endif
  406. rc = ahci_platform_get_phy(hpriv, port, dev, child);
  407. if (rc)
  408. goto err_out;
  409. enabled_ports++;
  410. }
  411. if (!enabled_ports) {
  412. dev_warn(dev, "No port enabled\n");
  413. rc = -ENODEV;
  414. goto err_out;
  415. }
  416. if (!hpriv->mask_port_map)
  417. hpriv->mask_port_map = mask_port_map;
  418. } else {
  419. /*
  420. * If no sub-node was found, keep this for device tree
  421. * compatibility
  422. */
  423. rc = ahci_platform_get_phy(hpriv, 0, dev, dev->of_node);
  424. if (rc)
  425. goto err_out;
  426. rc = ahci_platform_get_regulator(hpriv, 0, dev);
  427. if (rc == -EPROBE_DEFER)
  428. goto err_out;
  429. }
  430. pm_runtime_enable(dev);
  431. pm_runtime_get_sync(dev);
  432. hpriv->got_runtime_pm = true;
  433. devres_remove_group(dev, NULL);
  434. return hpriv;
  435. err_out:
  436. devres_release_group(dev, NULL);
  437. return ERR_PTR(rc);
  438. }
  439. EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
  440. /**
  441. * ahci_platform_init_host - Bring up an ahci-platform host
  442. * @pdev: platform device pointer for the host
  443. * @hpriv: ahci-host private data for the host
  444. * @pi_template: template for the ata_port_info to use
  445. * @sht: scsi_host_template to use when registering
  446. *
  447. * This function does all the usual steps needed to bring up an
  448. * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
  449. * must be initialized / enabled before calling this.
  450. *
  451. * RETURNS:
  452. * 0 on success otherwise a negative error code
  453. */
  454. int ahci_platform_init_host(struct platform_device *pdev,
  455. struct ahci_host_priv *hpriv,
  456. const struct ata_port_info *pi_template,
  457. struct scsi_host_template *sht)
  458. {
  459. struct device *dev = &pdev->dev;
  460. struct ata_port_info pi = *pi_template;
  461. const struct ata_port_info *ppi[] = { &pi, NULL };
  462. struct ata_host *host;
  463. int i, irq, n_ports, rc;
  464. irq = platform_get_irq(pdev, 0);
  465. if (irq <= 0) {
  466. if (irq != -EPROBE_DEFER)
  467. dev_err(dev, "no irq\n");
  468. return irq;
  469. }
  470. hpriv->irq = irq;
  471. /* prepare host */
  472. pi.private_data = (void *)(unsigned long)hpriv->flags;
  473. ahci_save_initial_config(dev, hpriv);
  474. if (hpriv->cap & HOST_CAP_NCQ)
  475. pi.flags |= ATA_FLAG_NCQ;
  476. if (hpriv->cap & HOST_CAP_PMP)
  477. pi.flags |= ATA_FLAG_PMP;
  478. ahci_set_em_messages(hpriv, &pi);
  479. /* CAP.NP sometimes indicate the index of the last enabled
  480. * port, at other times, that of the last possible port, so
  481. * determining the maximum port number requires looking at
  482. * both CAP.NP and port_map.
  483. */
  484. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  485. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  486. if (!host)
  487. return -ENOMEM;
  488. host->private_data = hpriv;
  489. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  490. host->flags |= ATA_HOST_PARALLEL_SCAN;
  491. else
  492. dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
  493. if (pi.flags & ATA_FLAG_EM)
  494. ahci_reset_em(host);
  495. for (i = 0; i < host->n_ports; i++) {
  496. struct ata_port *ap = host->ports[i];
  497. ata_port_desc(ap, "mmio %pR",
  498. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  499. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  500. /* set enclosure management message type */
  501. if (ap->flags & ATA_FLAG_EM)
  502. ap->em_message_type = hpriv->em_msg_type;
  503. /* disabled/not-implemented port */
  504. if (!(hpriv->port_map & (1 << i)))
  505. ap->ops = &ata_dummy_port_ops;
  506. }
  507. if (hpriv->cap & HOST_CAP_64) {
  508. rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
  509. if (rc) {
  510. rc = dma_coerce_mask_and_coherent(dev,
  511. DMA_BIT_MASK(32));
  512. if (rc) {
  513. dev_err(dev, "Failed to enable 64-bit DMA.\n");
  514. return rc;
  515. }
  516. dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
  517. }
  518. }
  519. rc = ahci_reset_controller(host);
  520. if (rc)
  521. return rc;
  522. ahci_init_controller(host);
  523. ahci_print_info(host, "platform");
  524. return ahci_host_activate(host, sht);
  525. }
  526. EXPORT_SYMBOL_GPL(ahci_platform_init_host);
  527. static void ahci_host_stop(struct ata_host *host)
  528. {
  529. struct ahci_host_priv *hpriv = host->private_data;
  530. ahci_platform_disable_resources(hpriv);
  531. }
  532. /**
  533. * ahci_platform_shutdown - Disable interrupts and stop DMA for host ports
  534. * @pdev: platform device pointer for the host
  535. *
  536. * This function is called during system shutdown and performs the minimal
  537. * deconfiguration required to ensure that an ahci_platform host cannot
  538. * corrupt or otherwise interfere with a new kernel being started with kexec.
  539. */
  540. void ahci_platform_shutdown(struct platform_device *pdev)
  541. {
  542. struct ata_host *host = platform_get_drvdata(pdev);
  543. struct ahci_host_priv *hpriv = host->private_data;
  544. void __iomem *mmio = hpriv->mmio;
  545. int i;
  546. for (i = 0; i < host->n_ports; i++) {
  547. struct ata_port *ap = host->ports[i];
  548. /* Disable port interrupts */
  549. if (ap->ops->freeze)
  550. ap->ops->freeze(ap);
  551. /* Stop the port DMA engines */
  552. if (ap->ops->port_stop)
  553. ap->ops->port_stop(ap);
  554. }
  555. /* Disable and clear host interrupts */
  556. writel(readl(mmio + HOST_CTL) & ~HOST_IRQ_EN, mmio + HOST_CTL);
  557. readl(mmio + HOST_CTL); /* flush */
  558. writel(GENMASK(host->n_ports, 0), mmio + HOST_IRQ_STAT);
  559. }
  560. EXPORT_SYMBOL_GPL(ahci_platform_shutdown);
  561. #ifdef CONFIG_PM_SLEEP
  562. /**
  563. * ahci_platform_suspend_host - Suspend an ahci-platform host
  564. * @dev: device pointer for the host
  565. *
  566. * This function does all the usual steps needed to suspend an
  567. * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
  568. * must be disabled after calling this.
  569. *
  570. * RETURNS:
  571. * 0 on success otherwise a negative error code
  572. */
  573. int ahci_platform_suspend_host(struct device *dev)
  574. {
  575. struct ata_host *host = dev_get_drvdata(dev);
  576. struct ahci_host_priv *hpriv = host->private_data;
  577. void __iomem *mmio = hpriv->mmio;
  578. u32 ctl;
  579. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  580. dev_err(dev, "firmware update required for suspend/resume\n");
  581. return -EIO;
  582. }
  583. /*
  584. * AHCI spec rev1.1 section 8.3.3:
  585. * Software must disable interrupts prior to requesting a
  586. * transition of the HBA to D3 state.
  587. */
  588. ctl = readl(mmio + HOST_CTL);
  589. ctl &= ~HOST_IRQ_EN;
  590. writel(ctl, mmio + HOST_CTL);
  591. readl(mmio + HOST_CTL); /* flush */
  592. return ata_host_suspend(host, PMSG_SUSPEND);
  593. }
  594. EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
  595. /**
  596. * ahci_platform_resume_host - Resume an ahci-platform host
  597. * @dev: device pointer for the host
  598. *
  599. * This function does all the usual steps needed to resume an ahci-platform
  600. * host, note any necessary resources (ie clks, phys, etc.) must be
  601. * initialized / enabled before calling this.
  602. *
  603. * RETURNS:
  604. * 0 on success otherwise a negative error code
  605. */
  606. int ahci_platform_resume_host(struct device *dev)
  607. {
  608. struct ata_host *host = dev_get_drvdata(dev);
  609. int rc;
  610. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  611. rc = ahci_reset_controller(host);
  612. if (rc)
  613. return rc;
  614. ahci_init_controller(host);
  615. }
  616. ata_host_resume(host);
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
  620. /**
  621. * ahci_platform_suspend - Suspend an ahci-platform device
  622. * @dev: the platform device to suspend
  623. *
  624. * This function suspends the host associated with the device, followed by
  625. * disabling all the resources of the device.
  626. *
  627. * RETURNS:
  628. * 0 on success otherwise a negative error code
  629. */
  630. int ahci_platform_suspend(struct device *dev)
  631. {
  632. struct ata_host *host = dev_get_drvdata(dev);
  633. struct ahci_host_priv *hpriv = host->private_data;
  634. int rc;
  635. rc = ahci_platform_suspend_host(dev);
  636. if (rc)
  637. return rc;
  638. ahci_platform_disable_resources(hpriv);
  639. return 0;
  640. }
  641. EXPORT_SYMBOL_GPL(ahci_platform_suspend);
  642. /**
  643. * ahci_platform_resume - Resume an ahci-platform device
  644. * @dev: the platform device to resume
  645. *
  646. * This function enables all the resources of the device followed by
  647. * resuming the host associated with the device.
  648. *
  649. * RETURNS:
  650. * 0 on success otherwise a negative error code
  651. */
  652. int ahci_platform_resume(struct device *dev)
  653. {
  654. struct ata_host *host = dev_get_drvdata(dev);
  655. struct ahci_host_priv *hpriv = host->private_data;
  656. int rc;
  657. rc = ahci_platform_enable_resources(hpriv);
  658. if (rc)
  659. return rc;
  660. rc = ahci_platform_resume_host(dev);
  661. if (rc)
  662. goto disable_resources;
  663. /* We resumed so update PM runtime state */
  664. pm_runtime_disable(dev);
  665. pm_runtime_set_active(dev);
  666. pm_runtime_enable(dev);
  667. return 0;
  668. disable_resources:
  669. ahci_platform_disable_resources(hpriv);
  670. return rc;
  671. }
  672. EXPORT_SYMBOL_GPL(ahci_platform_resume);
  673. #endif
  674. MODULE_DESCRIPTION("AHCI SATA platform library");
  675. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  676. MODULE_LICENSE("GPL");