mtk-smi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2015-2016 MediaTek Inc.
  3. * Author: Yong Wu <yong.wu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/component.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <soc/mediatek/smi.h>
  24. #define SMI_LARB_MMU_EN 0xf00
  25. struct mtk_smi {
  26. struct device *dev;
  27. struct clk *clk_apb, *clk_smi;
  28. };
  29. struct mtk_smi_larb { /* larb: local arbiter */
  30. struct mtk_smi smi;
  31. void __iomem *base;
  32. struct device *smi_common_dev;
  33. u32 *mmu;
  34. };
  35. static int mtk_smi_enable(const struct mtk_smi *smi)
  36. {
  37. int ret;
  38. ret = pm_runtime_get_sync(smi->dev);
  39. if (ret < 0)
  40. return ret;
  41. ret = clk_prepare_enable(smi->clk_apb);
  42. if (ret)
  43. goto err_put_pm;
  44. ret = clk_prepare_enable(smi->clk_smi);
  45. if (ret)
  46. goto err_disable_apb;
  47. return 0;
  48. err_disable_apb:
  49. clk_disable_unprepare(smi->clk_apb);
  50. err_put_pm:
  51. pm_runtime_put_sync(smi->dev);
  52. return ret;
  53. }
  54. static void mtk_smi_disable(const struct mtk_smi *smi)
  55. {
  56. clk_disable_unprepare(smi->clk_smi);
  57. clk_disable_unprepare(smi->clk_apb);
  58. pm_runtime_put_sync(smi->dev);
  59. }
  60. int mtk_smi_larb_get(struct device *larbdev)
  61. {
  62. struct mtk_smi_larb *larb = dev_get_drvdata(larbdev);
  63. struct mtk_smi *common = dev_get_drvdata(larb->smi_common_dev);
  64. int ret;
  65. /* Enable the smi-common's power and clocks */
  66. ret = mtk_smi_enable(common);
  67. if (ret)
  68. return ret;
  69. /* Enable the larb's power and clocks */
  70. ret = mtk_smi_enable(&larb->smi);
  71. if (ret) {
  72. mtk_smi_disable(common);
  73. return ret;
  74. }
  75. /* Configure the iommu info for this larb */
  76. writel(*larb->mmu, larb->base + SMI_LARB_MMU_EN);
  77. return 0;
  78. }
  79. void mtk_smi_larb_put(struct device *larbdev)
  80. {
  81. struct mtk_smi_larb *larb = dev_get_drvdata(larbdev);
  82. struct mtk_smi *common = dev_get_drvdata(larb->smi_common_dev);
  83. /*
  84. * Don't de-configure the iommu info for this larb since there may be
  85. * several modules in this larb.
  86. * The iommu info will be reset after power off.
  87. */
  88. mtk_smi_disable(&larb->smi);
  89. mtk_smi_disable(common);
  90. }
  91. static int
  92. mtk_smi_larb_bind(struct device *dev, struct device *master, void *data)
  93. {
  94. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  95. struct mtk_smi_iommu *smi_iommu = data;
  96. unsigned int i;
  97. for (i = 0; i < smi_iommu->larb_nr; i++) {
  98. if (dev == smi_iommu->larb_imu[i].dev) {
  99. /* The 'mmu' may be updated in iommu-attach/detach. */
  100. larb->mmu = &smi_iommu->larb_imu[i].mmu;
  101. return 0;
  102. }
  103. }
  104. return -ENODEV;
  105. }
  106. static void
  107. mtk_smi_larb_unbind(struct device *dev, struct device *master, void *data)
  108. {
  109. /* Do nothing as the iommu is always enabled. */
  110. }
  111. static const struct component_ops mtk_smi_larb_component_ops = {
  112. .bind = mtk_smi_larb_bind,
  113. .unbind = mtk_smi_larb_unbind,
  114. };
  115. static int mtk_smi_larb_probe(struct platform_device *pdev)
  116. {
  117. struct mtk_smi_larb *larb;
  118. struct resource *res;
  119. struct device *dev = &pdev->dev;
  120. struct device_node *smi_node;
  121. struct platform_device *smi_pdev;
  122. if (!dev->pm_domain)
  123. return -EPROBE_DEFER;
  124. larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL);
  125. if (!larb)
  126. return -ENOMEM;
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. larb->base = devm_ioremap_resource(dev, res);
  129. if (IS_ERR(larb->base))
  130. return PTR_ERR(larb->base);
  131. larb->smi.clk_apb = devm_clk_get(dev, "apb");
  132. if (IS_ERR(larb->smi.clk_apb))
  133. return PTR_ERR(larb->smi.clk_apb);
  134. larb->smi.clk_smi = devm_clk_get(dev, "smi");
  135. if (IS_ERR(larb->smi.clk_smi))
  136. return PTR_ERR(larb->smi.clk_smi);
  137. larb->smi.dev = dev;
  138. smi_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0);
  139. if (!smi_node)
  140. return -EINVAL;
  141. smi_pdev = of_find_device_by_node(smi_node);
  142. of_node_put(smi_node);
  143. if (smi_pdev) {
  144. larb->smi_common_dev = &smi_pdev->dev;
  145. } else {
  146. dev_err(dev, "Failed to get the smi_common device\n");
  147. return -EINVAL;
  148. }
  149. pm_runtime_enable(dev);
  150. platform_set_drvdata(pdev, larb);
  151. return component_add(dev, &mtk_smi_larb_component_ops);
  152. }
  153. static int mtk_smi_larb_remove(struct platform_device *pdev)
  154. {
  155. pm_runtime_disable(&pdev->dev);
  156. component_del(&pdev->dev, &mtk_smi_larb_component_ops);
  157. return 0;
  158. }
  159. static const struct of_device_id mtk_smi_larb_of_ids[] = {
  160. { .compatible = "mediatek,mt8173-smi-larb",},
  161. {}
  162. };
  163. static struct platform_driver mtk_smi_larb_driver = {
  164. .probe = mtk_smi_larb_probe,
  165. .remove = mtk_smi_larb_remove,
  166. .driver = {
  167. .name = "mtk-smi-larb",
  168. .of_match_table = mtk_smi_larb_of_ids,
  169. }
  170. };
  171. static int mtk_smi_common_probe(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. struct mtk_smi *common;
  175. if (!dev->pm_domain)
  176. return -EPROBE_DEFER;
  177. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  178. if (!common)
  179. return -ENOMEM;
  180. common->dev = dev;
  181. common->clk_apb = devm_clk_get(dev, "apb");
  182. if (IS_ERR(common->clk_apb))
  183. return PTR_ERR(common->clk_apb);
  184. common->clk_smi = devm_clk_get(dev, "smi");
  185. if (IS_ERR(common->clk_smi))
  186. return PTR_ERR(common->clk_smi);
  187. pm_runtime_enable(dev);
  188. platform_set_drvdata(pdev, common);
  189. return 0;
  190. }
  191. static int mtk_smi_common_remove(struct platform_device *pdev)
  192. {
  193. pm_runtime_disable(&pdev->dev);
  194. return 0;
  195. }
  196. static const struct of_device_id mtk_smi_common_of_ids[] = {
  197. { .compatible = "mediatek,mt8173-smi-common", },
  198. {}
  199. };
  200. static struct platform_driver mtk_smi_common_driver = {
  201. .probe = mtk_smi_common_probe,
  202. .remove = mtk_smi_common_remove,
  203. .driver = {
  204. .name = "mtk-smi-common",
  205. .of_match_table = mtk_smi_common_of_ids,
  206. }
  207. };
  208. static int __init mtk_smi_init(void)
  209. {
  210. int ret;
  211. ret = platform_driver_register(&mtk_smi_common_driver);
  212. if (ret != 0) {
  213. pr_err("Failed to register SMI driver\n");
  214. return ret;
  215. }
  216. ret = platform_driver_register(&mtk_smi_larb_driver);
  217. if (ret != 0) {
  218. pr_err("Failed to register SMI-LARB driver\n");
  219. goto err_unreg_smi;
  220. }
  221. return ret;
  222. err_unreg_smi:
  223. platform_driver_unregister(&mtk_smi_common_driver);
  224. return ret;
  225. }
  226. subsys_initcall(mtk_smi_init);