mtk-smi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2016 MediaTek Inc.
  4. * Author: Yong Wu <[email protected]>
  5. */
  6. #include <linux/arm-smccc.h>
  7. #include <linux/clk.h>
  8. #include <linux/component.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/soc/mediatek/mtk_sip_svc.h>
  19. #include <soc/mediatek/smi.h>
  20. #include <dt-bindings/memory/mt2701-larb-port.h>
  21. #include <dt-bindings/memory/mtk-memory-port.h>
  22. /* SMI COMMON */
  23. #define SMI_L1LEN 0x100
  24. #define SMI_L1_ARB 0x200
  25. #define SMI_BUS_SEL 0x220
  26. #define SMI_BUS_LARB_SHIFT(larbid) ((larbid) << 1)
  27. /* All are MMU0 defaultly. Only specialize mmu1 here. */
  28. #define F_MMU1_LARB(larbid) (0x1 << SMI_BUS_LARB_SHIFT(larbid))
  29. #define SMI_READ_FIFO_TH 0x230
  30. #define SMI_M4U_TH 0x234
  31. #define SMI_FIFO_TH1 0x238
  32. #define SMI_FIFO_TH2 0x23c
  33. #define SMI_DCM 0x300
  34. #define SMI_DUMMY 0x444
  35. /* SMI LARB */
  36. #define SMI_LARB_SLP_CON 0xc
  37. #define SLP_PROT_EN BIT(0)
  38. #define SLP_PROT_RDY BIT(16)
  39. #define SMI_LARB_CMD_THRT_CON 0x24
  40. #define SMI_LARB_THRT_RD_NU_LMT_MSK GENMASK(7, 4)
  41. #define SMI_LARB_THRT_RD_NU_LMT (5 << 4)
  42. #define SMI_LARB_SW_FLAG 0x40
  43. #define SMI_LARB_SW_FLAG_1 0x1
  44. #define SMI_LARB_OSTDL_PORT 0x200
  45. #define SMI_LARB_OSTDL_PORTx(id) (SMI_LARB_OSTDL_PORT + (((id) & 0x1f) << 2))
  46. /* Below are about mmu enable registers, they are different in SoCs */
  47. /* gen1: mt2701 */
  48. #define REG_SMI_SECUR_CON_BASE 0x5c0
  49. /* every register control 8 port, register offset 0x4 */
  50. #define REG_SMI_SECUR_CON_OFFSET(id) (((id) >> 3) << 2)
  51. #define REG_SMI_SECUR_CON_ADDR(id) \
  52. (REG_SMI_SECUR_CON_BASE + REG_SMI_SECUR_CON_OFFSET(id))
  53. /*
  54. * every port have 4 bit to control, bit[port + 3] control virtual or physical,
  55. * bit[port + 2 : port + 1] control the domain, bit[port] control the security
  56. * or non-security.
  57. */
  58. #define SMI_SECUR_CON_VAL_MSK(id) (~(0xf << (((id) & 0x7) << 2)))
  59. #define SMI_SECUR_CON_VAL_VIRT(id) BIT((((id) & 0x7) << 2) + 3)
  60. /* mt2701 domain should be set to 3 */
  61. #define SMI_SECUR_CON_VAL_DOMAIN(id) (0x3 << ((((id) & 0x7) << 2) + 1))
  62. /* gen2: */
  63. /* mt8167 */
  64. #define MT8167_SMI_LARB_MMU_EN 0xfc0
  65. /* mt8173 */
  66. #define MT8173_SMI_LARB_MMU_EN 0xf00
  67. /* general */
  68. #define SMI_LARB_NONSEC_CON(id) (0x380 + ((id) * 4))
  69. #define F_MMU_EN BIT(0)
  70. #define BANK_SEL(id) ({ \
  71. u32 _id = (id) & 0x3; \
  72. (_id << 8 | _id << 10 | _id << 12 | _id << 14); \
  73. })
  74. #define SMI_COMMON_INIT_REGS_NR 6
  75. #define SMI_LARB_PORT_NR_MAX 32
  76. #define MTK_SMI_FLAG_THRT_UPDATE BIT(0)
  77. #define MTK_SMI_FLAG_SW_FLAG BIT(1)
  78. #define MTK_SMI_FLAG_SLEEP_CTL BIT(2)
  79. #define MTK_SMI_FLAG_CFG_PORT_SEC_CTL BIT(3)
  80. #define MTK_SMI_CAPS(flags, _x) (!!((flags) & (_x)))
  81. struct mtk_smi_reg_pair {
  82. unsigned int offset;
  83. u32 value;
  84. };
  85. enum mtk_smi_type {
  86. MTK_SMI_GEN1,
  87. MTK_SMI_GEN2, /* gen2 smi common */
  88. MTK_SMI_GEN2_SUB_COMM, /* gen2 smi sub common */
  89. };
  90. /* larbs: Require apb/smi clocks while gals is optional. */
  91. static const char * const mtk_smi_larb_clks[] = {"apb", "smi", "gals"};
  92. #define MTK_SMI_LARB_REQ_CLK_NR 2
  93. #define MTK_SMI_LARB_OPT_CLK_NR 1
  94. /*
  95. * common: Require these four clocks in has_gals case. Otherwise, only apb/smi are required.
  96. * sub common: Require apb/smi/gals0 clocks in has_gals case. Otherwise, only apb/smi are required.
  97. */
  98. static const char * const mtk_smi_common_clks[] = {"apb", "smi", "gals0", "gals1"};
  99. #define MTK_SMI_CLK_NR_MAX ARRAY_SIZE(mtk_smi_common_clks)
  100. #define MTK_SMI_COM_REQ_CLK_NR 2
  101. #define MTK_SMI_COM_GALS_REQ_CLK_NR MTK_SMI_CLK_NR_MAX
  102. #define MTK_SMI_SUB_COM_GALS_REQ_CLK_NR 3
  103. struct mtk_smi_common_plat {
  104. enum mtk_smi_type type;
  105. bool has_gals;
  106. u32 bus_sel; /* Balance some larbs to enter mmu0 or mmu1 */
  107. const struct mtk_smi_reg_pair *init;
  108. };
  109. struct mtk_smi_larb_gen {
  110. int port_in_larb[MTK_LARB_NR_MAX + 1];
  111. int (*config_port)(struct device *dev);
  112. unsigned int larb_direct_to_common_mask;
  113. unsigned int flags_general;
  114. const u8 (*ostd)[SMI_LARB_PORT_NR_MAX];
  115. };
  116. struct mtk_smi {
  117. struct device *dev;
  118. unsigned int clk_num;
  119. struct clk_bulk_data clks[MTK_SMI_CLK_NR_MAX];
  120. struct clk *clk_async; /*only needed by mt2701*/
  121. union {
  122. void __iomem *smi_ao_base; /* only for gen1 */
  123. void __iomem *base; /* only for gen2 */
  124. };
  125. struct device *smi_common_dev; /* for sub common */
  126. const struct mtk_smi_common_plat *plat;
  127. };
  128. struct mtk_smi_larb { /* larb: local arbiter */
  129. struct mtk_smi smi;
  130. void __iomem *base;
  131. struct device *smi_common_dev; /* common or sub-common dev */
  132. const struct mtk_smi_larb_gen *larb_gen;
  133. int larbid;
  134. u32 *mmu;
  135. unsigned char *bank;
  136. };
  137. static int
  138. mtk_smi_larb_bind(struct device *dev, struct device *master, void *data)
  139. {
  140. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  141. struct mtk_smi_larb_iommu *larb_mmu = data;
  142. unsigned int i;
  143. for (i = 0; i < MTK_LARB_NR_MAX; i++) {
  144. if (dev == larb_mmu[i].dev) {
  145. larb->larbid = i;
  146. larb->mmu = &larb_mmu[i].mmu;
  147. larb->bank = larb_mmu[i].bank;
  148. return 0;
  149. }
  150. }
  151. return -ENODEV;
  152. }
  153. static void
  154. mtk_smi_larb_unbind(struct device *dev, struct device *master, void *data)
  155. {
  156. /* Do nothing as the iommu is always enabled. */
  157. }
  158. static const struct component_ops mtk_smi_larb_component_ops = {
  159. .bind = mtk_smi_larb_bind,
  160. .unbind = mtk_smi_larb_unbind,
  161. };
  162. static int mtk_smi_larb_config_port_gen1(struct device *dev)
  163. {
  164. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  165. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  166. struct mtk_smi *common = dev_get_drvdata(larb->smi_common_dev);
  167. int i, m4u_port_id, larb_port_num;
  168. u32 sec_con_val, reg_val;
  169. m4u_port_id = larb_gen->port_in_larb[larb->larbid];
  170. larb_port_num = larb_gen->port_in_larb[larb->larbid + 1]
  171. - larb_gen->port_in_larb[larb->larbid];
  172. for (i = 0; i < larb_port_num; i++, m4u_port_id++) {
  173. if (*larb->mmu & BIT(i)) {
  174. /* bit[port + 3] controls the virtual or physical */
  175. sec_con_val = SMI_SECUR_CON_VAL_VIRT(m4u_port_id);
  176. } else {
  177. /* do not need to enable m4u for this port */
  178. continue;
  179. }
  180. reg_val = readl(common->smi_ao_base
  181. + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
  182. reg_val &= SMI_SECUR_CON_VAL_MSK(m4u_port_id);
  183. reg_val |= sec_con_val;
  184. reg_val |= SMI_SECUR_CON_VAL_DOMAIN(m4u_port_id);
  185. writel(reg_val,
  186. common->smi_ao_base
  187. + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
  188. }
  189. return 0;
  190. }
  191. static int mtk_smi_larb_config_port_mt8167(struct device *dev)
  192. {
  193. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  194. writel(*larb->mmu, larb->base + MT8167_SMI_LARB_MMU_EN);
  195. return 0;
  196. }
  197. static int mtk_smi_larb_config_port_mt8173(struct device *dev)
  198. {
  199. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  200. writel(*larb->mmu, larb->base + MT8173_SMI_LARB_MMU_EN);
  201. return 0;
  202. }
  203. static int mtk_smi_larb_config_port_gen2_general(struct device *dev)
  204. {
  205. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  206. u32 reg, flags_general = larb->larb_gen->flags_general;
  207. const u8 *larbostd = larb->larb_gen->ostd ? larb->larb_gen->ostd[larb->larbid] : NULL;
  208. struct arm_smccc_res res;
  209. int i;
  210. if (BIT(larb->larbid) & larb->larb_gen->larb_direct_to_common_mask)
  211. return 0;
  212. if (MTK_SMI_CAPS(flags_general, MTK_SMI_FLAG_THRT_UPDATE)) {
  213. reg = readl_relaxed(larb->base + SMI_LARB_CMD_THRT_CON);
  214. reg &= ~SMI_LARB_THRT_RD_NU_LMT_MSK;
  215. reg |= SMI_LARB_THRT_RD_NU_LMT;
  216. writel_relaxed(reg, larb->base + SMI_LARB_CMD_THRT_CON);
  217. }
  218. if (MTK_SMI_CAPS(flags_general, MTK_SMI_FLAG_SW_FLAG))
  219. writel_relaxed(SMI_LARB_SW_FLAG_1, larb->base + SMI_LARB_SW_FLAG);
  220. for (i = 0; i < SMI_LARB_PORT_NR_MAX && larbostd && !!larbostd[i]; i++)
  221. writel_relaxed(larbostd[i], larb->base + SMI_LARB_OSTDL_PORTx(i));
  222. /*
  223. * When mmu_en bits are in security world, the bank_sel still is in the
  224. * LARB_NONSEC_CON below. And the mmu_en bits of LARB_NONSEC_CON have no
  225. * effect in this case.
  226. */
  227. if (MTK_SMI_CAPS(flags_general, MTK_SMI_FLAG_CFG_PORT_SEC_CTL)) {
  228. arm_smccc_smc(MTK_SIP_KERNEL_IOMMU_CONTROL, IOMMU_ATF_CMD_CONFIG_SMI_LARB,
  229. larb->larbid, *larb->mmu, 0, 0, 0, 0, &res);
  230. if (res.a0 != 0) {
  231. dev_err(dev, "Enable iommu fail, ret %ld\n", res.a0);
  232. return -EINVAL;
  233. }
  234. }
  235. for_each_set_bit(i, (unsigned long *)larb->mmu, 32) {
  236. reg = readl_relaxed(larb->base + SMI_LARB_NONSEC_CON(i));
  237. reg |= F_MMU_EN;
  238. reg |= BANK_SEL(larb->bank[i]);
  239. writel(reg, larb->base + SMI_LARB_NONSEC_CON(i));
  240. }
  241. return 0;
  242. }
  243. static const u8 mtk_smi_larb_mt8188_ostd[][SMI_LARB_PORT_NR_MAX] = {
  244. [0] = {0x02, 0x18, 0x22, 0x22, 0x01, 0x02, 0x0a,},
  245. [1] = {0x12, 0x02, 0x14, 0x14, 0x01, 0x18, 0x0a,},
  246. [2] = {0x12, 0x12, 0x12, 0x12, 0x0a,},
  247. [3] = {0x12, 0x12, 0x12, 0x12, 0x28, 0x28, 0x0a,},
  248. [4] = {0x06, 0x01, 0x17, 0x06, 0x0a, 0x07, 0x07,},
  249. [5] = {0x02, 0x01, 0x04, 0x02, 0x06, 0x01, 0x06, 0x0a,},
  250. [6] = {0x06, 0x01, 0x06, 0x0a,},
  251. [7] = {0x0c, 0x0c, 0x12,},
  252. [8] = {0x0c, 0x01, 0x0a, 0x05, 0x02, 0x03, 0x01, 0x01, 0x14, 0x14,
  253. 0x0a, 0x14, 0x1e, 0x01, 0x0c, 0x0a, 0x05, 0x02, 0x02, 0x05,
  254. 0x03, 0x01, 0x1e, 0x01, 0x05,},
  255. [9] = {0x1e, 0x01, 0x0a, 0x0a, 0x01, 0x01, 0x03, 0x1e, 0x1e, 0x10,
  256. 0x07, 0x01, 0x0a, 0x06, 0x03, 0x03, 0x0e, 0x01, 0x04, 0x28,},
  257. [10] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  258. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  259. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  260. [11] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  261. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  262. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  263. [12] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  264. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  265. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  266. [13] = {0x07, 0x02, 0x04, 0x02, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  267. 0x07, 0x02, 0x04, 0x02, 0x05, 0x05,},
  268. [14] = {0x02, 0x02, 0x0c, 0x0c, 0x0c, 0x0c, 0x01, 0x01, 0x02, 0x02,
  269. 0x02, 0x02, 0x0c, 0x0c, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  270. 0x02, 0x02, 0x01, 0x01,},
  271. [15] = {0x0c, 0x0c, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x0c, 0x0c,
  272. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02,
  273. 0x0c, 0x01, 0x01,},
  274. [16] = {0x28, 0x28, 0x03, 0x01, 0x01, 0x03, 0x14, 0x14, 0x0a, 0x0d,
  275. 0x03, 0x05, 0x0e, 0x01, 0x01, 0x05, 0x06, 0x0d, 0x01,},
  276. [17] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  277. 0x12, 0x02, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  278. [18] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  279. 0x12, 0x02, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  280. [19] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  281. [20] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  282. [21] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  283. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  284. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  285. [22] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,
  286. 0x01,},
  287. [23] = {0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x18, 0x01, 0x01,},
  288. [24] = {0x12, 0x06, 0x12, 0x06,},
  289. [25] = {0x01},
  290. };
  291. static const u8 mtk_smi_larb_mt8195_ostd[][SMI_LARB_PORT_NR_MAX] = {
  292. [0] = {0x0a, 0xc, 0x22, 0x22, 0x01, 0x0a,}, /* larb0 */
  293. [1] = {0x0a, 0xc, 0x22, 0x22, 0x01, 0x0a,}, /* larb1 */
  294. [2] = {0x12, 0x12, 0x12, 0x12, 0x0a,}, /* ... */
  295. [3] = {0x12, 0x12, 0x12, 0x12, 0x28, 0x28, 0x0a,},
  296. [4] = {0x06, 0x01, 0x17, 0x06, 0x0a,},
  297. [5] = {0x06, 0x01, 0x17, 0x06, 0x06, 0x01, 0x06, 0x0a,},
  298. [6] = {0x06, 0x01, 0x06, 0x0a,},
  299. [7] = {0x0c, 0x0c, 0x12,},
  300. [8] = {0x0c, 0x0c, 0x12,},
  301. [9] = {0x0a, 0x08, 0x04, 0x06, 0x01, 0x01, 0x10, 0x18, 0x11, 0x0a,
  302. 0x08, 0x04, 0x11, 0x06, 0x02, 0x06, 0x01, 0x11, 0x11, 0x06,},
  303. [10] = {0x18, 0x08, 0x01, 0x01, 0x20, 0x12, 0x18, 0x06, 0x05, 0x10,
  304. 0x08, 0x08, 0x10, 0x08, 0x08, 0x18, 0x0c, 0x09, 0x0b, 0x0d,
  305. 0x0d, 0x06, 0x10, 0x10,},
  306. [11] = {0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x01, 0x01, 0x01, 0x01,},
  307. [12] = {0x09, 0x09, 0x05, 0x05, 0x0c, 0x18, 0x02, 0x02, 0x04, 0x02,},
  308. [13] = {0x02, 0x02, 0x12, 0x12, 0x02, 0x02, 0x02, 0x02, 0x08, 0x01,},
  309. [14] = {0x12, 0x12, 0x02, 0x02, 0x02, 0x02, 0x16, 0x01, 0x16, 0x01,
  310. 0x01, 0x02, 0x02, 0x08, 0x02,},
  311. [15] = {},
  312. [16] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  313. 0x12, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  314. [17] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  315. [18] = {0x12, 0x06, 0x12, 0x06,},
  316. [19] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  317. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  318. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  319. [20] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  320. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  321. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  322. [21] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,},
  323. [22] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,},
  324. [23] = {0x18, 0x01,},
  325. [24] = {0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01,
  326. 0x01, 0x01,},
  327. [25] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  328. 0x02, 0x01,},
  329. [26] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  330. 0x02, 0x01,},
  331. [27] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  332. 0x02, 0x01,},
  333. [28] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  334. };
  335. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2701 = {
  336. .port_in_larb = {
  337. LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
  338. LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
  339. },
  340. .config_port = mtk_smi_larb_config_port_gen1,
  341. };
  342. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2712 = {
  343. .config_port = mtk_smi_larb_config_port_gen2_general,
  344. .larb_direct_to_common_mask = BIT(8) | BIT(9), /* bdpsys */
  345. };
  346. static const struct mtk_smi_larb_gen mtk_smi_larb_mt6779 = {
  347. .config_port = mtk_smi_larb_config_port_gen2_general,
  348. .larb_direct_to_common_mask =
  349. BIT(4) | BIT(6) | BIT(11) | BIT(12) | BIT(13),
  350. /* DUMMY | IPU0 | IPU1 | CCU | MDLA */
  351. };
  352. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8167 = {
  353. /* mt8167 do not need the port in larb */
  354. .config_port = mtk_smi_larb_config_port_mt8167,
  355. };
  356. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8173 = {
  357. /* mt8173 do not need the port in larb */
  358. .config_port = mtk_smi_larb_config_port_mt8173,
  359. };
  360. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8183 = {
  361. .config_port = mtk_smi_larb_config_port_gen2_general,
  362. .larb_direct_to_common_mask = BIT(2) | BIT(3) | BIT(7),
  363. /* IPU0 | IPU1 | CCU */
  364. };
  365. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8186 = {
  366. .config_port = mtk_smi_larb_config_port_gen2_general,
  367. .flags_general = MTK_SMI_FLAG_SLEEP_CTL,
  368. };
  369. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8188 = {
  370. .config_port = mtk_smi_larb_config_port_gen2_general,
  371. .flags_general = MTK_SMI_FLAG_THRT_UPDATE | MTK_SMI_FLAG_SW_FLAG |
  372. MTK_SMI_FLAG_SLEEP_CTL | MTK_SMI_FLAG_CFG_PORT_SEC_CTL,
  373. .ostd = mtk_smi_larb_mt8188_ostd,
  374. };
  375. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8192 = {
  376. .config_port = mtk_smi_larb_config_port_gen2_general,
  377. };
  378. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8195 = {
  379. .config_port = mtk_smi_larb_config_port_gen2_general,
  380. .flags_general = MTK_SMI_FLAG_THRT_UPDATE | MTK_SMI_FLAG_SW_FLAG |
  381. MTK_SMI_FLAG_SLEEP_CTL,
  382. .ostd = mtk_smi_larb_mt8195_ostd,
  383. };
  384. static const struct of_device_id mtk_smi_larb_of_ids[] = {
  385. {.compatible = "mediatek,mt2701-smi-larb", .data = &mtk_smi_larb_mt2701},
  386. {.compatible = "mediatek,mt2712-smi-larb", .data = &mtk_smi_larb_mt2712},
  387. {.compatible = "mediatek,mt6779-smi-larb", .data = &mtk_smi_larb_mt6779},
  388. {.compatible = "mediatek,mt6795-smi-larb", .data = &mtk_smi_larb_mt8173},
  389. {.compatible = "mediatek,mt8167-smi-larb", .data = &mtk_smi_larb_mt8167},
  390. {.compatible = "mediatek,mt8173-smi-larb", .data = &mtk_smi_larb_mt8173},
  391. {.compatible = "mediatek,mt8183-smi-larb", .data = &mtk_smi_larb_mt8183},
  392. {.compatible = "mediatek,mt8186-smi-larb", .data = &mtk_smi_larb_mt8186},
  393. {.compatible = "mediatek,mt8188-smi-larb", .data = &mtk_smi_larb_mt8188},
  394. {.compatible = "mediatek,mt8192-smi-larb", .data = &mtk_smi_larb_mt8192},
  395. {.compatible = "mediatek,mt8195-smi-larb", .data = &mtk_smi_larb_mt8195},
  396. {}
  397. };
  398. static int mtk_smi_larb_sleep_ctrl_enable(struct mtk_smi_larb *larb)
  399. {
  400. int ret;
  401. u32 tmp;
  402. writel_relaxed(SLP_PROT_EN, larb->base + SMI_LARB_SLP_CON);
  403. ret = readl_poll_timeout_atomic(larb->base + SMI_LARB_SLP_CON,
  404. tmp, !!(tmp & SLP_PROT_RDY), 10, 1000);
  405. if (ret) {
  406. /* TODO: Reset this larb if it fails here. */
  407. dev_err(larb->smi.dev, "sleep ctrl is not ready(0x%x).\n", tmp);
  408. }
  409. return ret;
  410. }
  411. static void mtk_smi_larb_sleep_ctrl_disable(struct mtk_smi_larb *larb)
  412. {
  413. writel_relaxed(0, larb->base + SMI_LARB_SLP_CON);
  414. }
  415. static int mtk_smi_device_link_common(struct device *dev, struct device **com_dev)
  416. {
  417. struct platform_device *smi_com_pdev;
  418. struct device_node *smi_com_node;
  419. struct device *smi_com_dev;
  420. struct device_link *link;
  421. smi_com_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0);
  422. if (!smi_com_node)
  423. return -EINVAL;
  424. smi_com_pdev = of_find_device_by_node(smi_com_node);
  425. of_node_put(smi_com_node);
  426. if (smi_com_pdev) {
  427. /* smi common is the supplier, Make sure it is ready before */
  428. if (!platform_get_drvdata(smi_com_pdev)) {
  429. put_device(&smi_com_pdev->dev);
  430. return -EPROBE_DEFER;
  431. }
  432. smi_com_dev = &smi_com_pdev->dev;
  433. link = device_link_add(dev, smi_com_dev,
  434. DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
  435. if (!link) {
  436. dev_err(dev, "Unable to link smi-common dev\n");
  437. put_device(&smi_com_pdev->dev);
  438. return -ENODEV;
  439. }
  440. *com_dev = smi_com_dev;
  441. } else {
  442. dev_err(dev, "Failed to get the smi_common device\n");
  443. return -EINVAL;
  444. }
  445. return 0;
  446. }
  447. static int mtk_smi_dts_clk_init(struct device *dev, struct mtk_smi *smi,
  448. const char * const clks[],
  449. unsigned int clk_nr_required,
  450. unsigned int clk_nr_optional)
  451. {
  452. int i, ret;
  453. for (i = 0; i < clk_nr_required; i++)
  454. smi->clks[i].id = clks[i];
  455. ret = devm_clk_bulk_get(dev, clk_nr_required, smi->clks);
  456. if (ret)
  457. return ret;
  458. for (i = clk_nr_required; i < clk_nr_required + clk_nr_optional; i++)
  459. smi->clks[i].id = clks[i];
  460. ret = devm_clk_bulk_get_optional(dev, clk_nr_optional,
  461. smi->clks + clk_nr_required);
  462. smi->clk_num = clk_nr_required + clk_nr_optional;
  463. return ret;
  464. }
  465. static int mtk_smi_larb_probe(struct platform_device *pdev)
  466. {
  467. struct mtk_smi_larb *larb;
  468. struct device *dev = &pdev->dev;
  469. int ret;
  470. larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL);
  471. if (!larb)
  472. return -ENOMEM;
  473. larb->larb_gen = of_device_get_match_data(dev);
  474. larb->base = devm_platform_ioremap_resource(pdev, 0);
  475. if (IS_ERR(larb->base))
  476. return PTR_ERR(larb->base);
  477. ret = mtk_smi_dts_clk_init(dev, &larb->smi, mtk_smi_larb_clks,
  478. MTK_SMI_LARB_REQ_CLK_NR, MTK_SMI_LARB_OPT_CLK_NR);
  479. if (ret)
  480. return ret;
  481. larb->smi.dev = dev;
  482. ret = mtk_smi_device_link_common(dev, &larb->smi_common_dev);
  483. if (ret < 0)
  484. return ret;
  485. pm_runtime_enable(dev);
  486. platform_set_drvdata(pdev, larb);
  487. ret = component_add(dev, &mtk_smi_larb_component_ops);
  488. if (ret)
  489. goto err_pm_disable;
  490. return 0;
  491. err_pm_disable:
  492. pm_runtime_disable(dev);
  493. device_link_remove(dev, larb->smi_common_dev);
  494. return ret;
  495. }
  496. static int mtk_smi_larb_remove(struct platform_device *pdev)
  497. {
  498. struct mtk_smi_larb *larb = platform_get_drvdata(pdev);
  499. device_link_remove(&pdev->dev, larb->smi_common_dev);
  500. pm_runtime_disable(&pdev->dev);
  501. component_del(&pdev->dev, &mtk_smi_larb_component_ops);
  502. return 0;
  503. }
  504. static int __maybe_unused mtk_smi_larb_resume(struct device *dev)
  505. {
  506. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  507. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  508. int ret;
  509. ret = clk_bulk_prepare_enable(larb->smi.clk_num, larb->smi.clks);
  510. if (ret)
  511. return ret;
  512. if (MTK_SMI_CAPS(larb->larb_gen->flags_general, MTK_SMI_FLAG_SLEEP_CTL))
  513. mtk_smi_larb_sleep_ctrl_disable(larb);
  514. /* Configure the basic setting for this larb */
  515. return larb_gen->config_port(dev);
  516. }
  517. static int __maybe_unused mtk_smi_larb_suspend(struct device *dev)
  518. {
  519. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  520. int ret;
  521. if (MTK_SMI_CAPS(larb->larb_gen->flags_general, MTK_SMI_FLAG_SLEEP_CTL)) {
  522. ret = mtk_smi_larb_sleep_ctrl_enable(larb);
  523. if (ret)
  524. return ret;
  525. }
  526. clk_bulk_disable_unprepare(larb->smi.clk_num, larb->smi.clks);
  527. return 0;
  528. }
  529. static const struct dev_pm_ops smi_larb_pm_ops = {
  530. SET_RUNTIME_PM_OPS(mtk_smi_larb_suspend, mtk_smi_larb_resume, NULL)
  531. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  532. pm_runtime_force_resume)
  533. };
  534. static struct platform_driver mtk_smi_larb_driver = {
  535. .probe = mtk_smi_larb_probe,
  536. .remove = mtk_smi_larb_remove,
  537. .driver = {
  538. .name = "mtk-smi-larb",
  539. .of_match_table = mtk_smi_larb_of_ids,
  540. .pm = &smi_larb_pm_ops,
  541. }
  542. };
  543. static const struct mtk_smi_reg_pair mtk_smi_common_mt6795_init[SMI_COMMON_INIT_REGS_NR] = {
  544. {SMI_L1_ARB, 0x1b},
  545. {SMI_M4U_TH, 0xce810c85},
  546. {SMI_FIFO_TH1, 0x43214c8},
  547. {SMI_READ_FIFO_TH, 0x191f},
  548. };
  549. static const struct mtk_smi_reg_pair mtk_smi_common_mt8195_init[SMI_COMMON_INIT_REGS_NR] = {
  550. {SMI_L1LEN, 0xb},
  551. {SMI_M4U_TH, 0xe100e10},
  552. {SMI_FIFO_TH1, 0x506090a},
  553. {SMI_FIFO_TH2, 0x506090a},
  554. {SMI_DCM, 0x4f1},
  555. {SMI_DUMMY, 0x1},
  556. };
  557. static const struct mtk_smi_common_plat mtk_smi_common_gen1 = {
  558. .type = MTK_SMI_GEN1,
  559. };
  560. static const struct mtk_smi_common_plat mtk_smi_common_gen2 = {
  561. .type = MTK_SMI_GEN2,
  562. };
  563. static const struct mtk_smi_common_plat mtk_smi_common_mt6779 = {
  564. .type = MTK_SMI_GEN2,
  565. .has_gals = true,
  566. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(4) |
  567. F_MMU1_LARB(5) | F_MMU1_LARB(6) | F_MMU1_LARB(7),
  568. };
  569. static const struct mtk_smi_common_plat mtk_smi_common_mt6795 = {
  570. .type = MTK_SMI_GEN2,
  571. .bus_sel = F_MMU1_LARB(0),
  572. .init = mtk_smi_common_mt6795_init,
  573. };
  574. static const struct mtk_smi_common_plat mtk_smi_common_mt8183 = {
  575. .type = MTK_SMI_GEN2,
  576. .has_gals = true,
  577. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(5) |
  578. F_MMU1_LARB(7),
  579. };
  580. static const struct mtk_smi_common_plat mtk_smi_common_mt8186 = {
  581. .type = MTK_SMI_GEN2,
  582. .has_gals = true,
  583. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(4) | F_MMU1_LARB(7),
  584. };
  585. static const struct mtk_smi_common_plat mtk_smi_common_mt8188_vdo = {
  586. .type = MTK_SMI_GEN2,
  587. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(5) | F_MMU1_LARB(7),
  588. .init = mtk_smi_common_mt8195_init,
  589. };
  590. static const struct mtk_smi_common_plat mtk_smi_common_mt8188_vpp = {
  591. .type = MTK_SMI_GEN2,
  592. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(7),
  593. .init = mtk_smi_common_mt8195_init,
  594. };
  595. static const struct mtk_smi_common_plat mtk_smi_common_mt8192 = {
  596. .type = MTK_SMI_GEN2,
  597. .has_gals = true,
  598. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(5) |
  599. F_MMU1_LARB(6),
  600. };
  601. static const struct mtk_smi_common_plat mtk_smi_common_mt8195_vdo = {
  602. .type = MTK_SMI_GEN2,
  603. .has_gals = true,
  604. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(3) | F_MMU1_LARB(5) |
  605. F_MMU1_LARB(7),
  606. .init = mtk_smi_common_mt8195_init,
  607. };
  608. static const struct mtk_smi_common_plat mtk_smi_common_mt8195_vpp = {
  609. .type = MTK_SMI_GEN2,
  610. .has_gals = true,
  611. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(7),
  612. .init = mtk_smi_common_mt8195_init,
  613. };
  614. static const struct mtk_smi_common_plat mtk_smi_sub_common_mt8195 = {
  615. .type = MTK_SMI_GEN2_SUB_COMM,
  616. .has_gals = true,
  617. };
  618. static const struct of_device_id mtk_smi_common_of_ids[] = {
  619. {.compatible = "mediatek,mt2701-smi-common", .data = &mtk_smi_common_gen1},
  620. {.compatible = "mediatek,mt2712-smi-common", .data = &mtk_smi_common_gen2},
  621. {.compatible = "mediatek,mt6779-smi-common", .data = &mtk_smi_common_mt6779},
  622. {.compatible = "mediatek,mt6795-smi-common", .data = &mtk_smi_common_mt6795},
  623. {.compatible = "mediatek,mt8167-smi-common", .data = &mtk_smi_common_gen2},
  624. {.compatible = "mediatek,mt8173-smi-common", .data = &mtk_smi_common_gen2},
  625. {.compatible = "mediatek,mt8183-smi-common", .data = &mtk_smi_common_mt8183},
  626. {.compatible = "mediatek,mt8186-smi-common", .data = &mtk_smi_common_mt8186},
  627. {.compatible = "mediatek,mt8188-smi-common-vdo", .data = &mtk_smi_common_mt8188_vdo},
  628. {.compatible = "mediatek,mt8188-smi-common-vpp", .data = &mtk_smi_common_mt8188_vpp},
  629. {.compatible = "mediatek,mt8192-smi-common", .data = &mtk_smi_common_mt8192},
  630. {.compatible = "mediatek,mt8195-smi-common-vdo", .data = &mtk_smi_common_mt8195_vdo},
  631. {.compatible = "mediatek,mt8195-smi-common-vpp", .data = &mtk_smi_common_mt8195_vpp},
  632. {.compatible = "mediatek,mt8195-smi-sub-common", .data = &mtk_smi_sub_common_mt8195},
  633. {}
  634. };
  635. static int mtk_smi_common_probe(struct platform_device *pdev)
  636. {
  637. struct device *dev = &pdev->dev;
  638. struct mtk_smi *common;
  639. int ret, clk_required = MTK_SMI_COM_REQ_CLK_NR;
  640. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  641. if (!common)
  642. return -ENOMEM;
  643. common->dev = dev;
  644. common->plat = of_device_get_match_data(dev);
  645. if (common->plat->has_gals) {
  646. if (common->plat->type == MTK_SMI_GEN2)
  647. clk_required = MTK_SMI_COM_GALS_REQ_CLK_NR;
  648. else if (common->plat->type == MTK_SMI_GEN2_SUB_COMM)
  649. clk_required = MTK_SMI_SUB_COM_GALS_REQ_CLK_NR;
  650. }
  651. ret = mtk_smi_dts_clk_init(dev, common, mtk_smi_common_clks, clk_required, 0);
  652. if (ret)
  653. return ret;
  654. /*
  655. * for mtk smi gen 1, we need to get the ao(always on) base to config
  656. * m4u port, and we need to enable the aync clock for transform the smi
  657. * clock into emi clock domain, but for mtk smi gen2, there's no smi ao
  658. * base.
  659. */
  660. if (common->plat->type == MTK_SMI_GEN1) {
  661. common->smi_ao_base = devm_platform_ioremap_resource(pdev, 0);
  662. if (IS_ERR(common->smi_ao_base))
  663. return PTR_ERR(common->smi_ao_base);
  664. common->clk_async = devm_clk_get(dev, "async");
  665. if (IS_ERR(common->clk_async))
  666. return PTR_ERR(common->clk_async);
  667. ret = clk_prepare_enable(common->clk_async);
  668. if (ret)
  669. return ret;
  670. } else {
  671. common->base = devm_platform_ioremap_resource(pdev, 0);
  672. if (IS_ERR(common->base))
  673. return PTR_ERR(common->base);
  674. }
  675. /* link its smi-common if this is smi-sub-common */
  676. if (common->plat->type == MTK_SMI_GEN2_SUB_COMM) {
  677. ret = mtk_smi_device_link_common(dev, &common->smi_common_dev);
  678. if (ret < 0)
  679. return ret;
  680. }
  681. pm_runtime_enable(dev);
  682. platform_set_drvdata(pdev, common);
  683. return 0;
  684. }
  685. static int mtk_smi_common_remove(struct platform_device *pdev)
  686. {
  687. struct mtk_smi *common = dev_get_drvdata(&pdev->dev);
  688. if (common->plat->type == MTK_SMI_GEN2_SUB_COMM)
  689. device_link_remove(&pdev->dev, common->smi_common_dev);
  690. pm_runtime_disable(&pdev->dev);
  691. return 0;
  692. }
  693. static int __maybe_unused mtk_smi_common_resume(struct device *dev)
  694. {
  695. struct mtk_smi *common = dev_get_drvdata(dev);
  696. const struct mtk_smi_reg_pair *init = common->plat->init;
  697. u32 bus_sel = common->plat->bus_sel; /* default is 0 */
  698. int ret, i;
  699. ret = clk_bulk_prepare_enable(common->clk_num, common->clks);
  700. if (ret)
  701. return ret;
  702. if (common->plat->type != MTK_SMI_GEN2)
  703. return 0;
  704. for (i = 0; i < SMI_COMMON_INIT_REGS_NR && init && init[i].offset; i++)
  705. writel_relaxed(init[i].value, common->base + init[i].offset);
  706. writel(bus_sel, common->base + SMI_BUS_SEL);
  707. return 0;
  708. }
  709. static int __maybe_unused mtk_smi_common_suspend(struct device *dev)
  710. {
  711. struct mtk_smi *common = dev_get_drvdata(dev);
  712. clk_bulk_disable_unprepare(common->clk_num, common->clks);
  713. return 0;
  714. }
  715. static const struct dev_pm_ops smi_common_pm_ops = {
  716. SET_RUNTIME_PM_OPS(mtk_smi_common_suspend, mtk_smi_common_resume, NULL)
  717. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  718. pm_runtime_force_resume)
  719. };
  720. static struct platform_driver mtk_smi_common_driver = {
  721. .probe = mtk_smi_common_probe,
  722. .remove = mtk_smi_common_remove,
  723. .driver = {
  724. .name = "mtk-smi-common",
  725. .of_match_table = mtk_smi_common_of_ids,
  726. .pm = &smi_common_pm_ops,
  727. }
  728. };
  729. static struct platform_driver * const smidrivers[] = {
  730. &mtk_smi_common_driver,
  731. &mtk_smi_larb_driver,
  732. };
  733. static int __init mtk_smi_init(void)
  734. {
  735. return platform_register_drivers(smidrivers, ARRAY_SIZE(smidrivers));
  736. }
  737. module_init(mtk_smi_init);
  738. static void __exit mtk_smi_exit(void)
  739. {
  740. platform_unregister_drivers(smidrivers, ARRAY_SIZE(smidrivers));
  741. }
  742. module_exit(mtk_smi_exit);
  743. MODULE_DESCRIPTION("MediaTek SMI driver");
  744. MODULE_LICENSE("GPL v2");