libahci_platform.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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_clks - Enable platform clocks
  38. * @hpriv: host private area to store config values
  39. *
  40. * This function enables all the clks found in hpriv->clks, starting at
  41. * index 0. If any clk fails to enable it disables all the clks already
  42. * enabled in reverse order, and then returns an error.
  43. *
  44. * RETURNS:
  45. * 0 on success otherwise a negative error code
  46. */
  47. int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
  48. {
  49. int c, rc;
  50. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
  51. rc = clk_prepare_enable(hpriv->clks[c]);
  52. if (rc)
  53. goto disable_unprepare_clk;
  54. }
  55. return 0;
  56. disable_unprepare_clk:
  57. while (--c >= 0)
  58. clk_disable_unprepare(hpriv->clks[c]);
  59. return rc;
  60. }
  61. EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
  62. /**
  63. * ahci_platform_disable_clks - Disable platform clocks
  64. * @hpriv: host private area to store config values
  65. *
  66. * This function disables all the clks found in hpriv->clks, in reverse
  67. * order of ahci_platform_enable_clks (starting at the end of the array).
  68. */
  69. void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
  70. {
  71. int c;
  72. for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
  73. if (hpriv->clks[c])
  74. clk_disable_unprepare(hpriv->clks[c]);
  75. }
  76. EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
  77. /**
  78. * ahci_platform_enable_resources - Enable platform resources
  79. * @hpriv: host private area to store config values
  80. *
  81. * This function enables all ahci_platform managed resources in the
  82. * following order:
  83. * 1) Regulator
  84. * 2) Clocks (through ahci_platform_enable_clks)
  85. * 3) Phy
  86. *
  87. * If resource enabling fails at any point the previous enabled resources
  88. * are disabled in reverse order.
  89. *
  90. * RETURNS:
  91. * 0 on success otherwise a negative error code
  92. */
  93. int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
  94. {
  95. int rc;
  96. if (hpriv->target_pwr) {
  97. rc = regulator_enable(hpriv->target_pwr);
  98. if (rc)
  99. return rc;
  100. }
  101. rc = ahci_platform_enable_clks(hpriv);
  102. if (rc)
  103. goto disable_regulator;
  104. if (hpriv->phy) {
  105. rc = phy_init(hpriv->phy);
  106. if (rc)
  107. goto disable_clks;
  108. rc = phy_power_on(hpriv->phy);
  109. if (rc) {
  110. phy_exit(hpriv->phy);
  111. goto disable_clks;
  112. }
  113. }
  114. return 0;
  115. disable_clks:
  116. ahci_platform_disable_clks(hpriv);
  117. disable_regulator:
  118. if (hpriv->target_pwr)
  119. regulator_disable(hpriv->target_pwr);
  120. return rc;
  121. }
  122. EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
  123. /**
  124. * ahci_platform_disable_resources - Disable platform resources
  125. * @hpriv: host private area to store config values
  126. *
  127. * This function disables all ahci_platform managed resources in the
  128. * following order:
  129. * 1) Phy
  130. * 2) Clocks (through ahci_platform_disable_clks)
  131. * 3) Regulator
  132. */
  133. void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
  134. {
  135. if (hpriv->phy) {
  136. phy_power_off(hpriv->phy);
  137. phy_exit(hpriv->phy);
  138. }
  139. ahci_platform_disable_clks(hpriv);
  140. if (hpriv->target_pwr)
  141. regulator_disable(hpriv->target_pwr);
  142. }
  143. EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
  144. static void ahci_platform_put_resources(struct device *dev, void *res)
  145. {
  146. struct ahci_host_priv *hpriv = res;
  147. int c;
  148. if (hpriv->got_runtime_pm) {
  149. pm_runtime_put_sync(dev);
  150. pm_runtime_disable(dev);
  151. }
  152. for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
  153. clk_put(hpriv->clks[c]);
  154. }
  155. /**
  156. * ahci_platform_get_resources - Get platform resources
  157. * @pdev: platform device to get resources for
  158. *
  159. * This function allocates an ahci_host_priv struct, and gets the following
  160. * resources, storing a reference to them inside the returned struct:
  161. *
  162. * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
  163. * 2) regulator for controlling the targets power (optional)
  164. * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
  165. * or for non devicetree enabled platforms a single clock
  166. * 4) phy (optional)
  167. *
  168. * RETURNS:
  169. * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
  170. */
  171. struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. struct ahci_host_priv *hpriv;
  175. struct clk *clk;
  176. int i, rc = -ENOMEM;
  177. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  178. return ERR_PTR(-ENOMEM);
  179. hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
  180. GFP_KERNEL);
  181. if (!hpriv)
  182. goto err_out;
  183. devres_add(dev, hpriv);
  184. hpriv->mmio = devm_ioremap_resource(dev,
  185. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  186. if (IS_ERR(hpriv->mmio)) {
  187. dev_err(dev, "no mmio space\n");
  188. rc = PTR_ERR(hpriv->mmio);
  189. goto err_out;
  190. }
  191. hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
  192. if (IS_ERR(hpriv->target_pwr)) {
  193. rc = PTR_ERR(hpriv->target_pwr);
  194. if (rc == -EPROBE_DEFER)
  195. goto err_out;
  196. hpriv->target_pwr = NULL;
  197. }
  198. for (i = 0; i < AHCI_MAX_CLKS; i++) {
  199. /*
  200. * For now we must use clk_get(dev, NULL) for the first clock,
  201. * because some platforms (da850, spear13xx) are not yet
  202. * converted to use devicetree for clocks. For new platforms
  203. * this is equivalent to of_clk_get(dev->of_node, 0).
  204. */
  205. if (i == 0)
  206. clk = clk_get(dev, NULL);
  207. else
  208. clk = of_clk_get(dev->of_node, i);
  209. if (IS_ERR(clk)) {
  210. rc = PTR_ERR(clk);
  211. if (rc == -EPROBE_DEFER)
  212. goto err_out;
  213. break;
  214. }
  215. hpriv->clks[i] = clk;
  216. }
  217. hpriv->phy = devm_phy_get(dev, "sata-phy");
  218. if (IS_ERR(hpriv->phy)) {
  219. rc = PTR_ERR(hpriv->phy);
  220. switch (rc) {
  221. case -ENOSYS:
  222. /* No PHY support. Check if PHY is required. */
  223. if (of_find_property(dev->of_node, "phys", NULL)) {
  224. dev_err(dev, "couldn't get sata-phy: ENOSYS\n");
  225. goto err_out;
  226. }
  227. case -ENODEV:
  228. /* continue normally */
  229. hpriv->phy = NULL;
  230. break;
  231. case -EPROBE_DEFER:
  232. goto err_out;
  233. default:
  234. dev_err(dev, "couldn't get sata-phy\n");
  235. goto err_out;
  236. }
  237. }
  238. pm_runtime_enable(dev);
  239. pm_runtime_get_sync(dev);
  240. hpriv->got_runtime_pm = true;
  241. devres_remove_group(dev, NULL);
  242. return hpriv;
  243. err_out:
  244. devres_release_group(dev, NULL);
  245. return ERR_PTR(rc);
  246. }
  247. EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
  248. /**
  249. * ahci_platform_init_host - Bring up an ahci-platform host
  250. * @pdev: platform device pointer for the host
  251. * @hpriv: ahci-host private data for the host
  252. * @pi_template: template for the ata_port_info to use
  253. * @host_flags: ahci host flags used in ahci_host_priv
  254. * @force_port_map: param passed to ahci_save_initial_config
  255. * @mask_port_map: param passed to ahci_save_initial_config
  256. *
  257. * This function does all the usual steps needed to bring up an
  258. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  259. * must be initialized / enabled before calling this.
  260. *
  261. * RETURNS:
  262. * 0 on success otherwise a negative error code
  263. */
  264. int ahci_platform_init_host(struct platform_device *pdev,
  265. struct ahci_host_priv *hpriv,
  266. const struct ata_port_info *pi_template,
  267. unsigned long host_flags,
  268. unsigned int force_port_map,
  269. unsigned int mask_port_map)
  270. {
  271. struct device *dev = &pdev->dev;
  272. struct ata_port_info pi = *pi_template;
  273. const struct ata_port_info *ppi[] = { &pi, NULL };
  274. struct ata_host *host;
  275. int i, irq, n_ports, rc;
  276. irq = platform_get_irq(pdev, 0);
  277. if (irq <= 0) {
  278. dev_err(dev, "no irq\n");
  279. return -EINVAL;
  280. }
  281. /* prepare host */
  282. pi.private_data = (void *)host_flags;
  283. hpriv->flags |= host_flags;
  284. ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
  285. if (hpriv->cap & HOST_CAP_NCQ)
  286. pi.flags |= ATA_FLAG_NCQ;
  287. if (hpriv->cap & HOST_CAP_PMP)
  288. pi.flags |= ATA_FLAG_PMP;
  289. ahci_set_em_messages(hpriv, &pi);
  290. /* CAP.NP sometimes indicate the index of the last enabled
  291. * port, at other times, that of the last possible port, so
  292. * determining the maximum port number requires looking at
  293. * both CAP.NP and port_map.
  294. */
  295. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  296. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  297. if (!host)
  298. return -ENOMEM;
  299. host->private_data = hpriv;
  300. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  301. host->flags |= ATA_HOST_PARALLEL_SCAN;
  302. else
  303. dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
  304. if (pi.flags & ATA_FLAG_EM)
  305. ahci_reset_em(host);
  306. for (i = 0; i < host->n_ports; i++) {
  307. struct ata_port *ap = host->ports[i];
  308. ata_port_desc(ap, "mmio %pR",
  309. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  310. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  311. /* set enclosure management message type */
  312. if (ap->flags & ATA_FLAG_EM)
  313. ap->em_message_type = hpriv->em_msg_type;
  314. /* disabled/not-implemented port */
  315. if (!(hpriv->port_map & (1 << i)))
  316. ap->ops = &ata_dummy_port_ops;
  317. }
  318. rc = ahci_reset_controller(host);
  319. if (rc)
  320. return rc;
  321. ahci_init_controller(host);
  322. ahci_print_info(host, "platform");
  323. return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  324. &ahci_platform_sht);
  325. }
  326. EXPORT_SYMBOL_GPL(ahci_platform_init_host);
  327. static void ahci_host_stop(struct ata_host *host)
  328. {
  329. struct device *dev = host->dev;
  330. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  331. struct ahci_host_priv *hpriv = host->private_data;
  332. if (pdata && pdata->exit)
  333. pdata->exit(dev);
  334. ahci_platform_disable_resources(hpriv);
  335. }
  336. #ifdef CONFIG_PM_SLEEP
  337. /**
  338. * ahci_platform_suspend_host - Suspend an ahci-platform host
  339. * @dev: device pointer for the host
  340. *
  341. * This function does all the usual steps needed to suspend an
  342. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  343. * must be disabled after calling this.
  344. *
  345. * RETURNS:
  346. * 0 on success otherwise a negative error code
  347. */
  348. int ahci_platform_suspend_host(struct device *dev)
  349. {
  350. struct ata_host *host = dev_get_drvdata(dev);
  351. struct ahci_host_priv *hpriv = host->private_data;
  352. void __iomem *mmio = hpriv->mmio;
  353. u32 ctl;
  354. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  355. dev_err(dev, "firmware update required for suspend/resume\n");
  356. return -EIO;
  357. }
  358. /*
  359. * AHCI spec rev1.1 section 8.3.3:
  360. * Software must disable interrupts prior to requesting a
  361. * transition of the HBA to D3 state.
  362. */
  363. ctl = readl(mmio + HOST_CTL);
  364. ctl &= ~HOST_IRQ_EN;
  365. writel(ctl, mmio + HOST_CTL);
  366. readl(mmio + HOST_CTL); /* flush */
  367. return ata_host_suspend(host, PMSG_SUSPEND);
  368. }
  369. EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
  370. /**
  371. * ahci_platform_resume_host - Resume an ahci-platform host
  372. * @dev: device pointer for the host
  373. *
  374. * This function does all the usual steps needed to resume an ahci-platform
  375. * host, note any necessary resources (ie clks, phy, etc.) must be
  376. * initialized / enabled before calling this.
  377. *
  378. * RETURNS:
  379. * 0 on success otherwise a negative error code
  380. */
  381. int ahci_platform_resume_host(struct device *dev)
  382. {
  383. struct ata_host *host = dev_get_drvdata(dev);
  384. int rc;
  385. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  386. rc = ahci_reset_controller(host);
  387. if (rc)
  388. return rc;
  389. ahci_init_controller(host);
  390. }
  391. ata_host_resume(host);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
  395. /**
  396. * ahci_platform_suspend - Suspend an ahci-platform device
  397. * @dev: the platform device to suspend
  398. *
  399. * This function suspends the host associated with the device, followed by
  400. * disabling all the resources of the device.
  401. *
  402. * RETURNS:
  403. * 0 on success otherwise a negative error code
  404. */
  405. int ahci_platform_suspend(struct device *dev)
  406. {
  407. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  408. struct ata_host *host = dev_get_drvdata(dev);
  409. struct ahci_host_priv *hpriv = host->private_data;
  410. int rc;
  411. rc = ahci_platform_suspend_host(dev);
  412. if (rc)
  413. return rc;
  414. if (pdata && pdata->suspend) {
  415. rc = pdata->suspend(dev);
  416. if (rc)
  417. goto resume_host;
  418. }
  419. ahci_platform_disable_resources(hpriv);
  420. return 0;
  421. resume_host:
  422. ahci_platform_resume_host(dev);
  423. return rc;
  424. }
  425. EXPORT_SYMBOL_GPL(ahci_platform_suspend);
  426. /**
  427. * ahci_platform_resume - Resume an ahci-platform device
  428. * @dev: the platform device to resume
  429. *
  430. * This function enables all the resources of the device followed by
  431. * resuming the host associated with the device.
  432. *
  433. * RETURNS:
  434. * 0 on success otherwise a negative error code
  435. */
  436. int ahci_platform_resume(struct device *dev)
  437. {
  438. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  439. struct ata_host *host = dev_get_drvdata(dev);
  440. struct ahci_host_priv *hpriv = host->private_data;
  441. int rc;
  442. rc = ahci_platform_enable_resources(hpriv);
  443. if (rc)
  444. return rc;
  445. if (pdata && pdata->resume) {
  446. rc = pdata->resume(dev);
  447. if (rc)
  448. goto disable_resources;
  449. }
  450. rc = ahci_platform_resume_host(dev);
  451. if (rc)
  452. goto disable_resources;
  453. /* We resumed so update PM runtime state */
  454. pm_runtime_disable(dev);
  455. pm_runtime_set_active(dev);
  456. pm_runtime_enable(dev);
  457. return 0;
  458. disable_resources:
  459. ahci_platform_disable_resources(hpriv);
  460. return rc;
  461. }
  462. EXPORT_SYMBOL_GPL(ahci_platform_resume);
  463. #endif
  464. MODULE_DESCRIPTION("AHCI SATA platform library");
  465. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  466. MODULE_LICENSE("GPL");