libahci_platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * AHCI SATA platform library
  3. *
  4. * Copyright 2004-2005 Red Hat, Inc.
  5. * Jeff Garzik <jgarzik@pobox.com>
  6. * Copyright 2010 MontaVista Software, LLC.
  7. * Anton Vorontsov <avorontsov@ru.mvista.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/kernel.h>
  16. #include <linux/gfp.h>
  17. #include <linux/module.h>
  18. #include <linux/pm.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/libata.h>
  23. #include <linux/ahci_platform.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/pm_runtime.h>
  26. #include "ahci.h"
  27. static void ahci_host_stop(struct ata_host *host);
  28. struct ata_port_operations ahci_platform_ops = {
  29. .inherits = &ahci_ops,
  30. .host_stop = ahci_host_stop,
  31. };
  32. EXPORT_SYMBOL_GPL(ahci_platform_ops);
  33. static struct scsi_host_template ahci_platform_sht = {
  34. AHCI_SHT("ahci_platform"),
  35. };
  36. /**
  37. * ahci_platform_enable_phys - Enable PHYs
  38. * @hpriv: host private area to store config values
  39. *
  40. * This function enables all the PHYs found in hpriv->phys, if any.
  41. * If a PHY fails to be enabled, it disables all the PHYs already
  42. * enabled in reverse order and returns an error.
  43. *
  44. * RETURNS:
  45. * 0 on success otherwise a negative error code
  46. */
  47. int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
  48. {
  49. int rc, i;
  50. for (i = 0; i < hpriv->nports; i++) {
  51. if (!hpriv->phys[i])
  52. continue;
  53. rc = phy_init(hpriv->phys[i]);
  54. if (rc)
  55. goto disable_phys;
  56. rc = phy_power_on(hpriv->phys[i]);
  57. if (rc) {
  58. phy_exit(hpriv->phys[i]);
  59. goto disable_phys;
  60. }
  61. }
  62. return 0;
  63. disable_phys:
  64. while (--i >= 0) {
  65. phy_power_off(hpriv->phys[i]);
  66. phy_exit(hpriv->phys[i]);
  67. }
  68. return rc;
  69. }
  70. EXPORT_SYMBOL_GPL(ahci_platform_enable_phys);
  71. /**
  72. * ahci_platform_disable_phys - Disable PHYs
  73. * @hpriv: host private area to store config values
  74. *
  75. * This function disables all PHYs found in hpriv->phys.
  76. */
  77. void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
  78. {
  79. int i;
  80. for (i = 0; i < hpriv->nports; i++) {
  81. if (!hpriv->phys[i])
  82. continue;
  83. phy_power_off(hpriv->phys[i]);
  84. phy_exit(hpriv->phys[i]);
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(ahci_platform_disable_phys);
  88. /**
  89. * ahci_platform_enable_clks - Enable platform clocks
  90. * @hpriv: host private area to store config values
  91. *
  92. * This function enables all the clks found in hpriv->clks, starting at
  93. * index 0. If any clk fails to enable it disables all the clks already
  94. * enabled in reverse order, and then returns an error.
  95. *
  96. * RETURNS:
  97. * 0 on success otherwise a negative error code
  98. */
  99. int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
  100. {
  101. int c, rc;
  102. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
  103. rc = clk_prepare_enable(hpriv->clks[c]);
  104. if (rc)
  105. goto disable_unprepare_clk;
  106. }
  107. return 0;
  108. disable_unprepare_clk:
  109. while (--c >= 0)
  110. clk_disable_unprepare(hpriv->clks[c]);
  111. return rc;
  112. }
  113. EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
  114. /**
  115. * ahci_platform_disable_clks - Disable platform clocks
  116. * @hpriv: host private area to store config values
  117. *
  118. * This function disables all the clks found in hpriv->clks, in reverse
  119. * order of ahci_platform_enable_clks (starting at the end of the array).
  120. */
  121. void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
  122. {
  123. int c;
  124. for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
  125. if (hpriv->clks[c])
  126. clk_disable_unprepare(hpriv->clks[c]);
  127. }
  128. EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
  129. /**
  130. * ahci_platform_enable_resources - Enable platform resources
  131. * @hpriv: host private area to store config values
  132. *
  133. * This function enables all ahci_platform managed resources in the
  134. * following order:
  135. * 1) Regulator
  136. * 2) Clocks (through ahci_platform_enable_clks)
  137. * 3) Phys
  138. *
  139. * If resource enabling fails at any point the previous enabled resources
  140. * are disabled in reverse order.
  141. *
  142. * RETURNS:
  143. * 0 on success otherwise a negative error code
  144. */
  145. int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
  146. {
  147. int rc;
  148. if (hpriv->target_pwr) {
  149. rc = regulator_enable(hpriv->target_pwr);
  150. if (rc)
  151. return rc;
  152. }
  153. rc = ahci_platform_enable_clks(hpriv);
  154. if (rc)
  155. goto disable_regulator;
  156. rc = ahci_platform_enable_phys(hpriv);
  157. if (rc)
  158. goto disable_clks;
  159. return 0;
  160. disable_clks:
  161. ahci_platform_disable_clks(hpriv);
  162. disable_regulator:
  163. if (hpriv->target_pwr)
  164. regulator_disable(hpriv->target_pwr);
  165. return rc;
  166. }
  167. EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
  168. /**
  169. * ahci_platform_disable_resources - Disable platform resources
  170. * @hpriv: host private area to store config values
  171. *
  172. * This function disables all ahci_platform managed resources in the
  173. * following order:
  174. * 1) Phys
  175. * 2) Clocks (through ahci_platform_disable_clks)
  176. * 3) Regulator
  177. */
  178. void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
  179. {
  180. ahci_platform_disable_phys(hpriv);
  181. ahci_platform_disable_clks(hpriv);
  182. if (hpriv->target_pwr)
  183. regulator_disable(hpriv->target_pwr);
  184. }
  185. EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
  186. static void ahci_platform_put_resources(struct device *dev, void *res)
  187. {
  188. struct ahci_host_priv *hpriv = res;
  189. int c;
  190. if (hpriv->got_runtime_pm) {
  191. pm_runtime_put_sync(dev);
  192. pm_runtime_disable(dev);
  193. }
  194. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
  195. clk_put(hpriv->clks[c]);
  196. }
  197. /**
  198. * ahci_platform_get_resources - Get platform resources
  199. * @pdev: platform device to get resources for
  200. *
  201. * This function allocates an ahci_host_priv struct, and gets the following
  202. * resources, storing a reference to them inside the returned struct:
  203. *
  204. * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
  205. * 2) regulator for controlling the targets power (optional)
  206. * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
  207. * or for non devicetree enabled platforms a single clock
  208. * 4) phys (optional)
  209. *
  210. * RETURNS:
  211. * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
  212. */
  213. struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
  214. {
  215. struct device *dev = &pdev->dev;
  216. struct ahci_host_priv *hpriv;
  217. struct clk *clk;
  218. struct device_node *child;
  219. int i, enabled_ports = 0, rc = -ENOMEM;
  220. u32 mask_port_map = 0;
  221. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  222. return ERR_PTR(-ENOMEM);
  223. hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
  224. GFP_KERNEL);
  225. if (!hpriv)
  226. goto err_out;
  227. devres_add(dev, hpriv);
  228. hpriv->mmio = devm_ioremap_resource(dev,
  229. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  230. if (IS_ERR(hpriv->mmio)) {
  231. dev_err(dev, "no mmio space\n");
  232. rc = PTR_ERR(hpriv->mmio);
  233. goto err_out;
  234. }
  235. hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
  236. if (IS_ERR(hpriv->target_pwr)) {
  237. rc = PTR_ERR(hpriv->target_pwr);
  238. if (rc == -EPROBE_DEFER)
  239. goto err_out;
  240. hpriv->target_pwr = NULL;
  241. }
  242. for (i = 0; i < AHCI_MAX_CLKS; i++) {
  243. /*
  244. * For now we must use clk_get(dev, NULL) for the first clock,
  245. * because some platforms (da850, spear13xx) are not yet
  246. * converted to use devicetree for clocks. For new platforms
  247. * this is equivalent to of_clk_get(dev->of_node, 0).
  248. */
  249. if (i == 0)
  250. clk = clk_get(dev, NULL);
  251. else
  252. clk = of_clk_get(dev->of_node, i);
  253. if (IS_ERR(clk)) {
  254. rc = PTR_ERR(clk);
  255. if (rc == -EPROBE_DEFER)
  256. goto err_out;
  257. break;
  258. }
  259. hpriv->clks[i] = clk;
  260. }
  261. hpriv->nports = of_get_child_count(dev->of_node);
  262. if (hpriv->nports) {
  263. hpriv->phys = devm_kzalloc(dev,
  264. hpriv->nports * sizeof(*hpriv->phys),
  265. GFP_KERNEL);
  266. if (!hpriv->phys) {
  267. rc = -ENOMEM;
  268. goto err_out;
  269. }
  270. for_each_child_of_node(dev->of_node, child) {
  271. u32 port;
  272. if (!of_device_is_available(child))
  273. continue;
  274. if (of_property_read_u32(child, "reg", &port)) {
  275. rc = -EINVAL;
  276. goto err_out;
  277. }
  278. if (port >= hpriv->nports) {
  279. dev_warn(dev, "invalid port number %d\n", port);
  280. continue;
  281. }
  282. mask_port_map |= BIT(port);
  283. hpriv->phys[port] = devm_of_phy_get(dev, child, NULL);
  284. if (IS_ERR(hpriv->phys[port])) {
  285. rc = PTR_ERR(hpriv->phys[port]);
  286. dev_err(dev,
  287. "couldn't get PHY in node %s: %d\n",
  288. child->name, rc);
  289. goto err_out;
  290. }
  291. enabled_ports++;
  292. }
  293. if (!enabled_ports) {
  294. dev_warn(dev, "No port enabled\n");
  295. rc = -ENODEV;
  296. goto err_out;
  297. }
  298. if (!hpriv->mask_port_map)
  299. hpriv->mask_port_map = mask_port_map;
  300. } else {
  301. /*
  302. * If no sub-node was found, keep this for device tree
  303. * compatibility
  304. */
  305. struct phy *phy = devm_phy_get(dev, "sata-phy");
  306. if (!IS_ERR(phy)) {
  307. hpriv->phys = devm_kzalloc(dev, sizeof(*hpriv->phys),
  308. GFP_KERNEL);
  309. if (!hpriv->phys) {
  310. rc = -ENOMEM;
  311. goto err_out;
  312. }
  313. hpriv->phys[0] = phy;
  314. hpriv->nports = 1;
  315. } else {
  316. rc = PTR_ERR(phy);
  317. switch (rc) {
  318. case -ENOSYS:
  319. /* No PHY support. Check if PHY is required. */
  320. if (of_find_property(dev->of_node, "phys", NULL)) {
  321. dev_err(dev, "couldn't get sata-phy: ENOSYS\n");
  322. goto err_out;
  323. }
  324. case -ENODEV:
  325. /* continue normally */
  326. hpriv->phys = NULL;
  327. break;
  328. default:
  329. goto err_out;
  330. }
  331. }
  332. }
  333. pm_runtime_enable(dev);
  334. pm_runtime_get_sync(dev);
  335. hpriv->got_runtime_pm = true;
  336. devres_remove_group(dev, NULL);
  337. return hpriv;
  338. err_out:
  339. devres_release_group(dev, NULL);
  340. return ERR_PTR(rc);
  341. }
  342. EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
  343. /**
  344. * ahci_platform_init_host - Bring up an ahci-platform host
  345. * @pdev: platform device pointer for the host
  346. * @hpriv: ahci-host private data for the host
  347. * @pi_template: template for the ata_port_info to use
  348. *
  349. * This function does all the usual steps needed to bring up an
  350. * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
  351. * must be initialized / enabled before calling this.
  352. *
  353. * RETURNS:
  354. * 0 on success otherwise a negative error code
  355. */
  356. int ahci_platform_init_host(struct platform_device *pdev,
  357. struct ahci_host_priv *hpriv,
  358. const struct ata_port_info *pi_template)
  359. {
  360. struct device *dev = &pdev->dev;
  361. struct ata_port_info pi = *pi_template;
  362. const struct ata_port_info *ppi[] = { &pi, NULL };
  363. struct ata_host *host;
  364. int i, irq, n_ports, rc;
  365. irq = platform_get_irq(pdev, 0);
  366. if (irq <= 0) {
  367. dev_err(dev, "no irq\n");
  368. return -EINVAL;
  369. }
  370. /* prepare host */
  371. pi.private_data = (void *)(unsigned long)hpriv->flags;
  372. ahci_save_initial_config(dev, hpriv);
  373. if (hpriv->cap & HOST_CAP_NCQ)
  374. pi.flags |= ATA_FLAG_NCQ;
  375. if (hpriv->cap & HOST_CAP_PMP)
  376. pi.flags |= ATA_FLAG_PMP;
  377. ahci_set_em_messages(hpriv, &pi);
  378. /* CAP.NP sometimes indicate the index of the last enabled
  379. * port, at other times, that of the last possible port, so
  380. * determining the maximum port number requires looking at
  381. * both CAP.NP and port_map.
  382. */
  383. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  384. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  385. if (!host)
  386. return -ENOMEM;
  387. host->private_data = hpriv;
  388. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  389. host->flags |= ATA_HOST_PARALLEL_SCAN;
  390. else
  391. dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
  392. if (pi.flags & ATA_FLAG_EM)
  393. ahci_reset_em(host);
  394. for (i = 0; i < host->n_ports; i++) {
  395. struct ata_port *ap = host->ports[i];
  396. ata_port_desc(ap, "mmio %pR",
  397. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  398. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  399. /* set enclosure management message type */
  400. if (ap->flags & ATA_FLAG_EM)
  401. ap->em_message_type = hpriv->em_msg_type;
  402. /* disabled/not-implemented port */
  403. if (!(hpriv->port_map & (1 << i)))
  404. ap->ops = &ata_dummy_port_ops;
  405. }
  406. if (hpriv->cap & HOST_CAP_64) {
  407. rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
  408. if (rc) {
  409. rc = dma_coerce_mask_and_coherent(dev,
  410. DMA_BIT_MASK(32));
  411. if (rc) {
  412. dev_err(dev, "Failed to enable 64-bit DMA.\n");
  413. return rc;
  414. }
  415. dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
  416. }
  417. }
  418. rc = ahci_reset_controller(host);
  419. if (rc)
  420. return rc;
  421. ahci_init_controller(host);
  422. ahci_print_info(host, "platform");
  423. return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  424. &ahci_platform_sht);
  425. }
  426. EXPORT_SYMBOL_GPL(ahci_platform_init_host);
  427. static void ahci_host_stop(struct ata_host *host)
  428. {
  429. struct device *dev = host->dev;
  430. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  431. struct ahci_host_priv *hpriv = host->private_data;
  432. if (pdata && pdata->exit)
  433. pdata->exit(dev);
  434. ahci_platform_disable_resources(hpriv);
  435. }
  436. #ifdef CONFIG_PM_SLEEP
  437. /**
  438. * ahci_platform_suspend_host - Suspend an ahci-platform host
  439. * @dev: device pointer for the host
  440. *
  441. * This function does all the usual steps needed to suspend an
  442. * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
  443. * must be disabled after calling this.
  444. *
  445. * RETURNS:
  446. * 0 on success otherwise a negative error code
  447. */
  448. int ahci_platform_suspend_host(struct device *dev)
  449. {
  450. struct ata_host *host = dev_get_drvdata(dev);
  451. struct ahci_host_priv *hpriv = host->private_data;
  452. void __iomem *mmio = hpriv->mmio;
  453. u32 ctl;
  454. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  455. dev_err(dev, "firmware update required for suspend/resume\n");
  456. return -EIO;
  457. }
  458. /*
  459. * AHCI spec rev1.1 section 8.3.3:
  460. * Software must disable interrupts prior to requesting a
  461. * transition of the HBA to D3 state.
  462. */
  463. ctl = readl(mmio + HOST_CTL);
  464. ctl &= ~HOST_IRQ_EN;
  465. writel(ctl, mmio + HOST_CTL);
  466. readl(mmio + HOST_CTL); /* flush */
  467. return ata_host_suspend(host, PMSG_SUSPEND);
  468. }
  469. EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
  470. /**
  471. * ahci_platform_resume_host - Resume an ahci-platform host
  472. * @dev: device pointer for the host
  473. *
  474. * This function does all the usual steps needed to resume an ahci-platform
  475. * host, note any necessary resources (ie clks, phys, etc.) must be
  476. * initialized / enabled before calling this.
  477. *
  478. * RETURNS:
  479. * 0 on success otherwise a negative error code
  480. */
  481. int ahci_platform_resume_host(struct device *dev)
  482. {
  483. struct ata_host *host = dev_get_drvdata(dev);
  484. int rc;
  485. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  486. rc = ahci_reset_controller(host);
  487. if (rc)
  488. return rc;
  489. ahci_init_controller(host);
  490. }
  491. ata_host_resume(host);
  492. return 0;
  493. }
  494. EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
  495. /**
  496. * ahci_platform_suspend - Suspend an ahci-platform device
  497. * @dev: the platform device to suspend
  498. *
  499. * This function suspends the host associated with the device, followed by
  500. * disabling all the resources of the device.
  501. *
  502. * RETURNS:
  503. * 0 on success otherwise a negative error code
  504. */
  505. int ahci_platform_suspend(struct device *dev)
  506. {
  507. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  508. struct ata_host *host = dev_get_drvdata(dev);
  509. struct ahci_host_priv *hpriv = host->private_data;
  510. int rc;
  511. rc = ahci_platform_suspend_host(dev);
  512. if (rc)
  513. return rc;
  514. if (pdata && pdata->suspend) {
  515. rc = pdata->suspend(dev);
  516. if (rc)
  517. goto resume_host;
  518. }
  519. ahci_platform_disable_resources(hpriv);
  520. return 0;
  521. resume_host:
  522. ahci_platform_resume_host(dev);
  523. return rc;
  524. }
  525. EXPORT_SYMBOL_GPL(ahci_platform_suspend);
  526. /**
  527. * ahci_platform_resume - Resume an ahci-platform device
  528. * @dev: the platform device to resume
  529. *
  530. * This function enables all the resources of the device followed by
  531. * resuming the host associated with the device.
  532. *
  533. * RETURNS:
  534. * 0 on success otherwise a negative error code
  535. */
  536. int ahci_platform_resume(struct device *dev)
  537. {
  538. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  539. struct ata_host *host = dev_get_drvdata(dev);
  540. struct ahci_host_priv *hpriv = host->private_data;
  541. int rc;
  542. rc = ahci_platform_enable_resources(hpriv);
  543. if (rc)
  544. return rc;
  545. if (pdata && pdata->resume) {
  546. rc = pdata->resume(dev);
  547. if (rc)
  548. goto disable_resources;
  549. }
  550. rc = ahci_platform_resume_host(dev);
  551. if (rc)
  552. goto disable_resources;
  553. /* We resumed so update PM runtime state */
  554. pm_runtime_disable(dev);
  555. pm_runtime_set_active(dev);
  556. pm_runtime_enable(dev);
  557. return 0;
  558. disable_resources:
  559. ahci_platform_disable_resources(hpriv);
  560. return rc;
  561. }
  562. EXPORT_SYMBOL_GPL(ahci_platform_resume);
  563. #endif
  564. MODULE_DESCRIPTION("AHCI SATA platform library");
  565. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  566. MODULE_LICENSE("GPL");