mtk-devapc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 MediaTek Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/iopoll.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_address.h>
  13. #define VIO_MOD_TO_REG_IND(m) ((m) / 32)
  14. #define VIO_MOD_TO_REG_OFF(m) ((m) % 32)
  15. struct mtk_devapc_vio_dbgs {
  16. union {
  17. u32 vio_dbg0;
  18. struct {
  19. u32 mstid:16;
  20. u32 dmnid:6;
  21. u32 vio_w:1;
  22. u32 vio_r:1;
  23. u32 addr_h:4;
  24. u32 resv:4;
  25. } dbg0_bits;
  26. };
  27. u32 vio_dbg1;
  28. };
  29. struct mtk_devapc_regs_ofs {
  30. /* reg offset */
  31. u32 vio_mask_offset;
  32. u32 vio_sta_offset;
  33. u32 vio_dbg0_offset;
  34. u32 vio_dbg1_offset;
  35. u32 apc_con_offset;
  36. u32 vio_shift_sta_offset;
  37. u32 vio_shift_sel_offset;
  38. u32 vio_shift_con_offset;
  39. };
  40. struct mtk_devapc_data {
  41. /* numbers of violation index */
  42. u32 vio_idx_num;
  43. const struct mtk_devapc_regs_ofs *regs_ofs;
  44. };
  45. struct mtk_devapc_context {
  46. struct device *dev;
  47. void __iomem *infra_base;
  48. struct clk *infra_clk;
  49. const struct mtk_devapc_data *data;
  50. };
  51. static void clear_vio_status(struct mtk_devapc_context *ctx)
  52. {
  53. void __iomem *reg;
  54. int i;
  55. reg = ctx->infra_base + ctx->data->regs_ofs->vio_sta_offset;
  56. for (i = 0; i < VIO_MOD_TO_REG_IND(ctx->data->vio_idx_num) - 1; i++)
  57. writel(GENMASK(31, 0), reg + 4 * i);
  58. writel(GENMASK(VIO_MOD_TO_REG_OFF(ctx->data->vio_idx_num) - 1, 0),
  59. reg + 4 * i);
  60. }
  61. static void mask_module_irq(struct mtk_devapc_context *ctx, bool mask)
  62. {
  63. void __iomem *reg;
  64. u32 val;
  65. int i;
  66. reg = ctx->infra_base + ctx->data->regs_ofs->vio_mask_offset;
  67. if (mask)
  68. val = GENMASK(31, 0);
  69. else
  70. val = 0;
  71. for (i = 0; i < VIO_MOD_TO_REG_IND(ctx->data->vio_idx_num) - 1; i++)
  72. writel(val, reg + 4 * i);
  73. val = readl(reg + 4 * i);
  74. if (mask)
  75. val |= GENMASK(VIO_MOD_TO_REG_OFF(ctx->data->vio_idx_num) - 1,
  76. 0);
  77. else
  78. val &= ~GENMASK(VIO_MOD_TO_REG_OFF(ctx->data->vio_idx_num) - 1,
  79. 0);
  80. writel(val, reg + 4 * i);
  81. }
  82. #define PHY_DEVAPC_TIMEOUT 0x10000
  83. /*
  84. * devapc_sync_vio_dbg - do "shift" mechansim" to get full violation information.
  85. * shift mechanism is depends on devapc hardware design.
  86. * Mediatek devapc set multiple slaves as a group.
  87. * When violation is triggered, violation info is kept
  88. * inside devapc hardware.
  89. * Driver should do shift mechansim to sync full violation
  90. * info to VIO_DBGs registers.
  91. *
  92. */
  93. static int devapc_sync_vio_dbg(struct mtk_devapc_context *ctx)
  94. {
  95. void __iomem *pd_vio_shift_sta_reg;
  96. void __iomem *pd_vio_shift_sel_reg;
  97. void __iomem *pd_vio_shift_con_reg;
  98. int min_shift_group;
  99. int ret;
  100. u32 val;
  101. pd_vio_shift_sta_reg = ctx->infra_base +
  102. ctx->data->regs_ofs->vio_shift_sta_offset;
  103. pd_vio_shift_sel_reg = ctx->infra_base +
  104. ctx->data->regs_ofs->vio_shift_sel_offset;
  105. pd_vio_shift_con_reg = ctx->infra_base +
  106. ctx->data->regs_ofs->vio_shift_con_offset;
  107. /* Find the minimum shift group which has violation */
  108. val = readl(pd_vio_shift_sta_reg);
  109. if (!val)
  110. return false;
  111. min_shift_group = __ffs(val);
  112. /* Assign the group to sync */
  113. writel(0x1 << min_shift_group, pd_vio_shift_sel_reg);
  114. /* Start syncing */
  115. writel(0x1, pd_vio_shift_con_reg);
  116. ret = readl_poll_timeout(pd_vio_shift_con_reg, val, val == 0x3, 0,
  117. PHY_DEVAPC_TIMEOUT);
  118. if (ret) {
  119. dev_err(ctx->dev, "%s: Shift violation info failed\n", __func__);
  120. return false;
  121. }
  122. /* Stop syncing */
  123. writel(0x0, pd_vio_shift_con_reg);
  124. /* Write clear */
  125. writel(0x1 << min_shift_group, pd_vio_shift_sta_reg);
  126. return true;
  127. }
  128. /*
  129. * devapc_extract_vio_dbg - extract full violation information after doing
  130. * shift mechanism.
  131. */
  132. static void devapc_extract_vio_dbg(struct mtk_devapc_context *ctx)
  133. {
  134. struct mtk_devapc_vio_dbgs vio_dbgs;
  135. void __iomem *vio_dbg0_reg;
  136. void __iomem *vio_dbg1_reg;
  137. vio_dbg0_reg = ctx->infra_base + ctx->data->regs_ofs->vio_dbg0_offset;
  138. vio_dbg1_reg = ctx->infra_base + ctx->data->regs_ofs->vio_dbg1_offset;
  139. vio_dbgs.vio_dbg0 = readl(vio_dbg0_reg);
  140. vio_dbgs.vio_dbg1 = readl(vio_dbg1_reg);
  141. /* Print violation information */
  142. if (vio_dbgs.dbg0_bits.vio_w)
  143. dev_info(ctx->dev, "Write Violation\n");
  144. else if (vio_dbgs.dbg0_bits.vio_r)
  145. dev_info(ctx->dev, "Read Violation\n");
  146. dev_info(ctx->dev, "Bus ID:0x%x, Dom ID:0x%x, Vio Addr:0x%x\n",
  147. vio_dbgs.dbg0_bits.mstid, vio_dbgs.dbg0_bits.dmnid,
  148. vio_dbgs.vio_dbg1);
  149. }
  150. /*
  151. * devapc_violation_irq - the devapc Interrupt Service Routine (ISR) will dump
  152. * violation information including which master violates
  153. * access slave.
  154. */
  155. static irqreturn_t devapc_violation_irq(int irq_number, void *data)
  156. {
  157. struct mtk_devapc_context *ctx = data;
  158. while (devapc_sync_vio_dbg(ctx))
  159. devapc_extract_vio_dbg(ctx);
  160. clear_vio_status(ctx);
  161. return IRQ_HANDLED;
  162. }
  163. /*
  164. * start_devapc - unmask slave's irq to start receiving devapc violation.
  165. */
  166. static void start_devapc(struct mtk_devapc_context *ctx)
  167. {
  168. writel(BIT(31), ctx->infra_base + ctx->data->regs_ofs->apc_con_offset);
  169. mask_module_irq(ctx, false);
  170. }
  171. /*
  172. * stop_devapc - mask slave's irq to stop service.
  173. */
  174. static void stop_devapc(struct mtk_devapc_context *ctx)
  175. {
  176. mask_module_irq(ctx, true);
  177. writel(BIT(2), ctx->infra_base + ctx->data->regs_ofs->apc_con_offset);
  178. }
  179. static const struct mtk_devapc_regs_ofs devapc_regs_ofs_mt6779 = {
  180. .vio_mask_offset = 0x0,
  181. .vio_sta_offset = 0x400,
  182. .vio_dbg0_offset = 0x900,
  183. .vio_dbg1_offset = 0x904,
  184. .apc_con_offset = 0xF00,
  185. .vio_shift_sta_offset = 0xF10,
  186. .vio_shift_sel_offset = 0xF14,
  187. .vio_shift_con_offset = 0xF20,
  188. };
  189. static const struct mtk_devapc_data devapc_mt6779 = {
  190. .vio_idx_num = 511,
  191. .regs_ofs = &devapc_regs_ofs_mt6779,
  192. };
  193. static const struct mtk_devapc_data devapc_mt8186 = {
  194. .vio_idx_num = 519,
  195. .regs_ofs = &devapc_regs_ofs_mt6779,
  196. };
  197. static const struct of_device_id mtk_devapc_dt_match[] = {
  198. {
  199. .compatible = "mediatek,mt6779-devapc",
  200. .data = &devapc_mt6779,
  201. }, {
  202. .compatible = "mediatek,mt8186-devapc",
  203. .data = &devapc_mt8186,
  204. }, {
  205. },
  206. };
  207. MODULE_DEVICE_TABLE(of, mtk_devapc_dt_match);
  208. static int mtk_devapc_probe(struct platform_device *pdev)
  209. {
  210. struct device_node *node = pdev->dev.of_node;
  211. struct mtk_devapc_context *ctx;
  212. u32 devapc_irq;
  213. int ret;
  214. if (IS_ERR(node))
  215. return -ENODEV;
  216. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  217. if (!ctx)
  218. return -ENOMEM;
  219. ctx->data = of_device_get_match_data(&pdev->dev);
  220. ctx->dev = &pdev->dev;
  221. ctx->infra_base = of_iomap(node, 0);
  222. if (!ctx->infra_base)
  223. return -EINVAL;
  224. devapc_irq = irq_of_parse_and_map(node, 0);
  225. if (!devapc_irq)
  226. return -EINVAL;
  227. ctx->infra_clk = devm_clk_get(&pdev->dev, "devapc-infra-clock");
  228. if (IS_ERR(ctx->infra_clk))
  229. return -EINVAL;
  230. if (clk_prepare_enable(ctx->infra_clk))
  231. return -EINVAL;
  232. ret = devm_request_irq(&pdev->dev, devapc_irq, devapc_violation_irq,
  233. IRQF_TRIGGER_NONE, "devapc", ctx);
  234. if (ret) {
  235. clk_disable_unprepare(ctx->infra_clk);
  236. return ret;
  237. }
  238. platform_set_drvdata(pdev, ctx);
  239. start_devapc(ctx);
  240. return 0;
  241. }
  242. static int mtk_devapc_remove(struct platform_device *pdev)
  243. {
  244. struct mtk_devapc_context *ctx = platform_get_drvdata(pdev);
  245. stop_devapc(ctx);
  246. clk_disable_unprepare(ctx->infra_clk);
  247. return 0;
  248. }
  249. static struct platform_driver mtk_devapc_driver = {
  250. .probe = mtk_devapc_probe,
  251. .remove = mtk_devapc_remove,
  252. .driver = {
  253. .name = "mtk-devapc",
  254. .of_match_table = mtk_devapc_dt_match,
  255. },
  256. };
  257. module_platform_driver(mtk_devapc_driver);
  258. MODULE_DESCRIPTION("Mediatek Device APC Driver");
  259. MODULE_AUTHOR("Neal Liu <[email protected]>");
  260. MODULE_LICENSE("GPL");