ahci_st.c 5.7 KB

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