ahci_mtk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * MediaTek AHCI SATA driver
  3. *
  4. * Copyright (c) 2017 MediaTek Inc.
  5. * Author: Ryder Lee <ryder.lee@mediatek.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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/ahci_platform.h>
  17. #include <linux/kernel.h>
  18. #include <linux/libata.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm.h>
  23. #include <linux/regmap.h>
  24. #include <linux/reset.h>
  25. #include "ahci.h"
  26. #define DRV_NAME "ahci-mtk"
  27. #define SYS_CFG 0x14
  28. #define SYS_CFG_SATA_MSK GENMASK(31, 30)
  29. #define SYS_CFG_SATA_EN BIT(31)
  30. struct mtk_ahci_plat {
  31. struct regmap *mode;
  32. struct reset_control *axi_rst;
  33. struct reset_control *sw_rst;
  34. struct reset_control *reg_rst;
  35. };
  36. static const struct ata_port_info ahci_port_info = {
  37. .flags = AHCI_FLAG_COMMON,
  38. .pio_mask = ATA_PIO4,
  39. .udma_mask = ATA_UDMA6,
  40. .port_ops = &ahci_platform_ops,
  41. };
  42. static struct scsi_host_template ahci_platform_sht = {
  43. AHCI_SHT(DRV_NAME),
  44. };
  45. static int mtk_ahci_platform_resets(struct ahci_host_priv *hpriv,
  46. struct device *dev)
  47. {
  48. struct mtk_ahci_plat *plat = hpriv->plat_data;
  49. int err;
  50. /* reset AXI bus and PHY part */
  51. plat->axi_rst = devm_reset_control_get_optional_exclusive(dev, "axi");
  52. if (PTR_ERR(plat->axi_rst) == -EPROBE_DEFER)
  53. return PTR_ERR(plat->axi_rst);
  54. plat->sw_rst = devm_reset_control_get_optional_exclusive(dev, "sw");
  55. if (PTR_ERR(plat->sw_rst) == -EPROBE_DEFER)
  56. return PTR_ERR(plat->sw_rst);
  57. plat->reg_rst = devm_reset_control_get_optional_exclusive(dev, "reg");
  58. if (PTR_ERR(plat->reg_rst) == -EPROBE_DEFER)
  59. return PTR_ERR(plat->reg_rst);
  60. err = reset_control_assert(plat->axi_rst);
  61. if (err) {
  62. dev_err(dev, "failed to assert AXI bus\n");
  63. return err;
  64. }
  65. err = reset_control_assert(plat->sw_rst);
  66. if (err) {
  67. dev_err(dev, "failed to assert PHY digital part\n");
  68. return err;
  69. }
  70. err = reset_control_assert(plat->reg_rst);
  71. if (err) {
  72. dev_err(dev, "failed to assert PHY register part\n");
  73. return err;
  74. }
  75. err = reset_control_deassert(plat->reg_rst);
  76. if (err) {
  77. dev_err(dev, "failed to deassert PHY register part\n");
  78. return err;
  79. }
  80. err = reset_control_deassert(plat->sw_rst);
  81. if (err) {
  82. dev_err(dev, "failed to deassert PHY digital part\n");
  83. return err;
  84. }
  85. err = reset_control_deassert(plat->axi_rst);
  86. if (err) {
  87. dev_err(dev, "failed to deassert AXI bus\n");
  88. return err;
  89. }
  90. return 0;
  91. }
  92. static int mtk_ahci_parse_property(struct ahci_host_priv *hpriv,
  93. struct device *dev)
  94. {
  95. struct mtk_ahci_plat *plat = hpriv->plat_data;
  96. struct device_node *np = dev->of_node;
  97. /* enable SATA function if needed */
  98. if (of_find_property(np, "mediatek,phy-mode", NULL)) {
  99. plat->mode = syscon_regmap_lookup_by_phandle(
  100. np, "mediatek,phy-mode");
  101. if (IS_ERR(plat->mode)) {
  102. dev_err(dev, "missing phy-mode phandle\n");
  103. return PTR_ERR(plat->mode);
  104. }
  105. regmap_update_bits(plat->mode, SYS_CFG, SYS_CFG_SATA_MSK,
  106. SYS_CFG_SATA_EN);
  107. }
  108. of_property_read_u32(np, "ports-implemented", &hpriv->force_port_map);
  109. return 0;
  110. }
  111. static int mtk_ahci_probe(struct platform_device *pdev)
  112. {
  113. struct device *dev = &pdev->dev;
  114. struct mtk_ahci_plat *plat;
  115. struct ahci_host_priv *hpriv;
  116. int err;
  117. plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
  118. if (!plat)
  119. return -ENOMEM;
  120. hpriv = ahci_platform_get_resources(pdev, 0);
  121. if (IS_ERR(hpriv))
  122. return PTR_ERR(hpriv);
  123. hpriv->plat_data = plat;
  124. err = mtk_ahci_parse_property(hpriv, dev);
  125. if (err)
  126. return err;
  127. err = mtk_ahci_platform_resets(hpriv, dev);
  128. if (err)
  129. return err;
  130. err = ahci_platform_enable_resources(hpriv);
  131. if (err)
  132. return err;
  133. err = ahci_platform_init_host(pdev, hpriv, &ahci_port_info,
  134. &ahci_platform_sht);
  135. if (err)
  136. goto disable_resources;
  137. return 0;
  138. disable_resources:
  139. ahci_platform_disable_resources(hpriv);
  140. return err;
  141. }
  142. static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
  143. ahci_platform_resume);
  144. static const struct of_device_id ahci_of_match[] = {
  145. { .compatible = "mediatek,mtk-ahci", },
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(of, ahci_of_match);
  149. static struct platform_driver mtk_ahci_driver = {
  150. .probe = mtk_ahci_probe,
  151. .remove = ata_platform_remove_one,
  152. .driver = {
  153. .name = DRV_NAME,
  154. .of_match_table = ahci_of_match,
  155. .pm = &ahci_pm_ops,
  156. },
  157. };
  158. module_platform_driver(mtk_ahci_driver);
  159. MODULE_DESCRIPTION("MediaTek SATA AHCI Driver");
  160. MODULE_LICENSE("GPL v2");