libahci_platform.c 23 KB

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