libahci_platform.c 20 KB

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