libahci_platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 -ENODEV:
  222. case -ENOSYS:
  223. /* continue normally */
  224. hpriv->phy = NULL;
  225. break;
  226. case -EPROBE_DEFER:
  227. goto err_out;
  228. default:
  229. dev_err(dev, "couldn't get sata-phy\n");
  230. goto err_out;
  231. }
  232. }
  233. pm_runtime_enable(dev);
  234. pm_runtime_get_sync(dev);
  235. hpriv->got_runtime_pm = true;
  236. devres_remove_group(dev, NULL);
  237. return hpriv;
  238. err_out:
  239. devres_release_group(dev, NULL);
  240. return ERR_PTR(rc);
  241. }
  242. EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
  243. /**
  244. * ahci_platform_init_host - Bring up an ahci-platform host
  245. * @pdev: platform device pointer for the host
  246. * @hpriv: ahci-host private data for the host
  247. * @pi_template: template for the ata_port_info to use
  248. * @host_flags: ahci host flags used in ahci_host_priv
  249. * @force_port_map: param passed to ahci_save_initial_config
  250. * @mask_port_map: param passed to ahci_save_initial_config
  251. *
  252. * This function does all the usual steps needed to bring up an
  253. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  254. * must be initialized / enabled before calling this.
  255. *
  256. * RETURNS:
  257. * 0 on success otherwise a negative error code
  258. */
  259. int ahci_platform_init_host(struct platform_device *pdev,
  260. struct ahci_host_priv *hpriv,
  261. const struct ata_port_info *pi_template,
  262. unsigned long host_flags,
  263. unsigned int force_port_map,
  264. unsigned int mask_port_map)
  265. {
  266. struct device *dev = &pdev->dev;
  267. struct ata_port_info pi = *pi_template;
  268. const struct ata_port_info *ppi[] = { &pi, NULL };
  269. struct ata_host *host;
  270. int i, irq, n_ports, rc;
  271. irq = platform_get_irq(pdev, 0);
  272. if (irq <= 0) {
  273. dev_err(dev, "no irq\n");
  274. return -EINVAL;
  275. }
  276. /* prepare host */
  277. pi.private_data = (void *)host_flags;
  278. hpriv->flags |= host_flags;
  279. ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
  280. if (hpriv->cap & HOST_CAP_NCQ)
  281. pi.flags |= ATA_FLAG_NCQ;
  282. if (hpriv->cap & HOST_CAP_PMP)
  283. pi.flags |= ATA_FLAG_PMP;
  284. ahci_set_em_messages(hpriv, &pi);
  285. /* CAP.NP sometimes indicate the index of the last enabled
  286. * port, at other times, that of the last possible port, so
  287. * determining the maximum port number requires looking at
  288. * both CAP.NP and port_map.
  289. */
  290. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  291. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  292. if (!host)
  293. return -ENOMEM;
  294. host->private_data = hpriv;
  295. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  296. host->flags |= ATA_HOST_PARALLEL_SCAN;
  297. else
  298. dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
  299. if (pi.flags & ATA_FLAG_EM)
  300. ahci_reset_em(host);
  301. for (i = 0; i < host->n_ports; i++) {
  302. struct ata_port *ap = host->ports[i];
  303. ata_port_desc(ap, "mmio %pR",
  304. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  305. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  306. /* set enclosure management message type */
  307. if (ap->flags & ATA_FLAG_EM)
  308. ap->em_message_type = hpriv->em_msg_type;
  309. /* disabled/not-implemented port */
  310. if (!(hpriv->port_map & (1 << i)))
  311. ap->ops = &ata_dummy_port_ops;
  312. }
  313. rc = ahci_reset_controller(host);
  314. if (rc)
  315. return rc;
  316. ahci_init_controller(host);
  317. ahci_print_info(host, "platform");
  318. return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
  319. &ahci_platform_sht);
  320. }
  321. EXPORT_SYMBOL_GPL(ahci_platform_init_host);
  322. static void ahci_host_stop(struct ata_host *host)
  323. {
  324. struct device *dev = host->dev;
  325. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  326. struct ahci_host_priv *hpriv = host->private_data;
  327. if (pdata && pdata->exit)
  328. pdata->exit(dev);
  329. ahci_platform_disable_resources(hpriv);
  330. }
  331. #ifdef CONFIG_PM_SLEEP
  332. /**
  333. * ahci_platform_suspend_host - Suspend an ahci-platform host
  334. * @dev: device pointer for the host
  335. *
  336. * This function does all the usual steps needed to suspend an
  337. * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
  338. * must be disabled after calling this.
  339. *
  340. * RETURNS:
  341. * 0 on success otherwise a negative error code
  342. */
  343. int ahci_platform_suspend_host(struct device *dev)
  344. {
  345. struct ata_host *host = dev_get_drvdata(dev);
  346. struct ahci_host_priv *hpriv = host->private_data;
  347. void __iomem *mmio = hpriv->mmio;
  348. u32 ctl;
  349. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  350. dev_err(dev, "firmware update required for suspend/resume\n");
  351. return -EIO;
  352. }
  353. /*
  354. * AHCI spec rev1.1 section 8.3.3:
  355. * Software must disable interrupts prior to requesting a
  356. * transition of the HBA to D3 state.
  357. */
  358. ctl = readl(mmio + HOST_CTL);
  359. ctl &= ~HOST_IRQ_EN;
  360. writel(ctl, mmio + HOST_CTL);
  361. readl(mmio + HOST_CTL); /* flush */
  362. return ata_host_suspend(host, PMSG_SUSPEND);
  363. }
  364. EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
  365. /**
  366. * ahci_platform_resume_host - Resume an ahci-platform host
  367. * @dev: device pointer for the host
  368. *
  369. * This function does all the usual steps needed to resume an ahci-platform
  370. * host, note any necessary resources (ie clks, phy, etc.) must be
  371. * initialized / enabled before calling this.
  372. *
  373. * RETURNS:
  374. * 0 on success otherwise a negative error code
  375. */
  376. int ahci_platform_resume_host(struct device *dev)
  377. {
  378. struct ata_host *host = dev_get_drvdata(dev);
  379. int rc;
  380. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  381. rc = ahci_reset_controller(host);
  382. if (rc)
  383. return rc;
  384. ahci_init_controller(host);
  385. }
  386. ata_host_resume(host);
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
  390. /**
  391. * ahci_platform_suspend - Suspend an ahci-platform device
  392. * @dev: the platform device to suspend
  393. *
  394. * This function suspends the host associated with the device, followed by
  395. * disabling all the resources of the device.
  396. *
  397. * RETURNS:
  398. * 0 on success otherwise a negative error code
  399. */
  400. int ahci_platform_suspend(struct device *dev)
  401. {
  402. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  403. struct ata_host *host = dev_get_drvdata(dev);
  404. struct ahci_host_priv *hpriv = host->private_data;
  405. int rc;
  406. rc = ahci_platform_suspend_host(dev);
  407. if (rc)
  408. return rc;
  409. if (pdata && pdata->suspend) {
  410. rc = pdata->suspend(dev);
  411. if (rc)
  412. goto resume_host;
  413. }
  414. ahci_platform_disable_resources(hpriv);
  415. return 0;
  416. resume_host:
  417. ahci_platform_resume_host(dev);
  418. return rc;
  419. }
  420. EXPORT_SYMBOL_GPL(ahci_platform_suspend);
  421. /**
  422. * ahci_platform_resume - Resume an ahci-platform device
  423. * @dev: the platform device to resume
  424. *
  425. * This function enables all the resources of the device followed by
  426. * resuming the host associated with the device.
  427. *
  428. * RETURNS:
  429. * 0 on success otherwise a negative error code
  430. */
  431. int ahci_platform_resume(struct device *dev)
  432. {
  433. struct ahci_platform_data *pdata = dev_get_platdata(dev);
  434. struct ata_host *host = dev_get_drvdata(dev);
  435. struct ahci_host_priv *hpriv = host->private_data;
  436. int rc;
  437. rc = ahci_platform_enable_resources(hpriv);
  438. if (rc)
  439. return rc;
  440. if (pdata && pdata->resume) {
  441. rc = pdata->resume(dev);
  442. if (rc)
  443. goto disable_resources;
  444. }
  445. rc = ahci_platform_resume_host(dev);
  446. if (rc)
  447. goto disable_resources;
  448. /* We resumed so update PM runtime state */
  449. pm_runtime_disable(dev);
  450. pm_runtime_set_active(dev);
  451. pm_runtime_enable(dev);
  452. return 0;
  453. disable_resources:
  454. ahci_platform_disable_resources(hpriv);
  455. return rc;
  456. }
  457. EXPORT_SYMBOL_GPL(ahci_platform_resume);
  458. #endif
  459. MODULE_DESCRIPTION("AHCI SATA platform library");
  460. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  461. MODULE_LICENSE("GPL");