libahci_platform.c 21 KB

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