libahci_platform.c 24 KB

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