libahci_platform.c 23 KB

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