ahci_st.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2012 STMicroelectronics Limited
  3. *
  4. * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
  5. * Alexandre Torgue <alexandre.torgue@st.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/export.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <linux/of.h>
  17. #include <linux/ahci_platform.h>
  18. #include <linux/libata.h>
  19. #include <linux/reset.h>
  20. #include <linux/io.h>
  21. #include <linux/dma-mapping.h>
  22. #include "ahci.h"
  23. #define ST_AHCI_OOBR 0xbc
  24. #define ST_AHCI_OOBR_WE BIT(31)
  25. #define ST_AHCI_OOBR_CWMIN_SHIFT 24
  26. #define ST_AHCI_OOBR_CWMAX_SHIFT 16
  27. #define ST_AHCI_OOBR_CIMIN_SHIFT 8
  28. #define ST_AHCI_OOBR_CIMAX_SHIFT 0
  29. struct st_ahci_drv_data {
  30. struct platform_device *ahci;
  31. struct reset_control *pwr;
  32. struct reset_control *sw_rst;
  33. struct reset_control *pwr_rst;
  34. struct ahci_host_priv *hpriv;
  35. };
  36. static void st_ahci_configure_oob(void __iomem *mmio)
  37. {
  38. unsigned long old_val, new_val;
  39. new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
  40. (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
  41. (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
  42. (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
  43. old_val = readl(mmio + ST_AHCI_OOBR);
  44. writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
  45. writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
  46. writel(new_val, mmio + ST_AHCI_OOBR);
  47. }
  48. static int st_ahci_deassert_resets(struct device *dev)
  49. {
  50. struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
  51. int err;
  52. if (drv_data->pwr) {
  53. err = reset_control_deassert(drv_data->pwr);
  54. if (err) {
  55. dev_err(dev, "unable to bring out of pwrdwn\n");
  56. return err;
  57. }
  58. }
  59. st_ahci_configure_oob(drv_data->hpriv->mmio);
  60. if (drv_data->sw_rst) {
  61. err = reset_control_deassert(drv_data->sw_rst);
  62. if (err) {
  63. dev_err(dev, "unable to bring out of sw-rst\n");
  64. return err;
  65. }
  66. }
  67. if (drv_data->pwr_rst) {
  68. err = reset_control_deassert(drv_data->pwr_rst);
  69. if (err) {
  70. dev_err(dev, "unable to bring out of pwr-rst\n");
  71. return err;
  72. }
  73. }
  74. return 0;
  75. }
  76. static void st_ahci_host_stop(struct ata_host *host)
  77. {
  78. struct ahci_host_priv *hpriv = host->private_data;
  79. struct device *dev = host->dev;
  80. struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
  81. int err;
  82. if (drv_data->pwr) {
  83. err = reset_control_assert(drv_data->pwr);
  84. if (err)
  85. dev_err(dev, "unable to pwrdwn\n");
  86. }
  87. ahci_platform_disable_resources(hpriv);
  88. }
  89. static int st_ahci_probe_resets(struct platform_device *pdev)
  90. {
  91. struct st_ahci_drv_data *drv_data = platform_get_drvdata(pdev);
  92. drv_data->pwr = devm_reset_control_get(&pdev->dev, "pwr-dwn");
  93. if (IS_ERR(drv_data->pwr)) {
  94. dev_info(&pdev->dev, "power reset control not defined\n");
  95. drv_data->pwr = NULL;
  96. }
  97. drv_data->sw_rst = devm_reset_control_get(&pdev->dev, "sw-rst");
  98. if (IS_ERR(drv_data->sw_rst)) {
  99. dev_info(&pdev->dev, "soft reset control not defined\n");
  100. drv_data->sw_rst = NULL;
  101. }
  102. drv_data->pwr_rst = devm_reset_control_get(&pdev->dev, "pwr-rst");
  103. if (IS_ERR(drv_data->pwr_rst)) {
  104. dev_dbg(&pdev->dev, "power soft reset control not defined\n");
  105. drv_data->pwr_rst = NULL;
  106. }
  107. return st_ahci_deassert_resets(&pdev->dev);
  108. }
  109. static struct ata_port_operations st_ahci_port_ops = {
  110. .inherits = &ahci_platform_ops,
  111. .host_stop = st_ahci_host_stop,
  112. };
  113. static const struct ata_port_info st_ahci_port_info = {
  114. .flags = AHCI_FLAG_COMMON,
  115. .pio_mask = ATA_PIO4,
  116. .udma_mask = ATA_UDMA6,
  117. .port_ops = &st_ahci_port_ops,
  118. };
  119. static int st_ahci_probe(struct platform_device *pdev)
  120. {
  121. struct st_ahci_drv_data *drv_data;
  122. struct ahci_host_priv *hpriv;
  123. int err;
  124. drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
  125. if (!drv_data)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, drv_data);
  128. hpriv = ahci_platform_get_resources(pdev);
  129. if (IS_ERR(hpriv))
  130. return PTR_ERR(hpriv);
  131. drv_data->hpriv = hpriv;
  132. err = st_ahci_probe_resets(pdev);
  133. if (err)
  134. return err;
  135. err = ahci_platform_enable_resources(hpriv);
  136. if (err)
  137. return err;
  138. err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info, 0, 0, 0);
  139. if (err) {
  140. ahci_platform_disable_resources(hpriv);
  141. return err;
  142. }
  143. return 0;
  144. }
  145. #ifdef CONFIG_PM_SLEEP
  146. static int st_ahci_suspend(struct device *dev)
  147. {
  148. struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
  149. struct ahci_host_priv *hpriv = drv_data->hpriv;
  150. int err;
  151. err = ahci_platform_suspend_host(dev);
  152. if (err)
  153. return err;
  154. if (drv_data->pwr) {
  155. err = reset_control_assert(drv_data->pwr);
  156. if (err) {
  157. dev_err(dev, "unable to pwrdwn");
  158. return err;
  159. }
  160. }
  161. ahci_platform_disable_resources(hpriv);
  162. return 0;
  163. }
  164. static int st_ahci_resume(struct device *dev)
  165. {
  166. struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
  167. struct ahci_host_priv *hpriv = drv_data->hpriv;
  168. int err;
  169. err = ahci_platform_enable_resources(hpriv);
  170. if (err)
  171. return err;
  172. err = st_ahci_deassert_resets(dev);
  173. if (err) {
  174. ahci_platform_disable_resources(hpriv);
  175. return err;
  176. }
  177. return ahci_platform_resume_host(dev);
  178. }
  179. #endif
  180. static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
  181. static struct of_device_id st_ahci_match[] = {
  182. { .compatible = "st,ahci", },
  183. {},
  184. };
  185. MODULE_DEVICE_TABLE(of, st_ahci_match);
  186. static struct platform_driver st_ahci_driver = {
  187. .driver = {
  188. .name = "st_ahci",
  189. .owner = THIS_MODULE,
  190. .pm = &st_ahci_pm_ops,
  191. .of_match_table = of_match_ptr(st_ahci_match),
  192. },
  193. .probe = st_ahci_probe,
  194. .remove = ata_platform_remove_one,
  195. };
  196. module_platform_driver(st_ahci_driver);
  197. MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
  198. MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
  199. MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
  200. MODULE_LICENSE("GPL v2");