libahci_platform.c 19 KB

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