mtk-smi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2016 MediaTek Inc.
  4. * Author: Yong Wu <yong.wu@mediatek.com>
  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_mt6893_ostd[][SMI_LARB_PORT_NR_MAX] = {
  244. [0] = {0x2, 0x6, 0x2, 0x2, 0x2, 0x28, 0x18, 0x18, 0x1, 0x1, 0x1, 0x8,
  245. 0x8, 0x1, 0x3f},
  246. [1] = {0x2, 0x6, 0x2, 0x2, 0x2, 0x28, 0x18, 0x18, 0x1, 0x1, 0x1, 0x8,
  247. 0x8, 0x1, 0x3f},
  248. [2] = {0x5, 0x5, 0x5, 0x5, 0x1, 0x3f},
  249. [3] = {0x5, 0x5, 0x5, 0x5, 0x1, 0x3f},
  250. [4] = {0x28, 0x19, 0xb, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x4, 0x1},
  251. [5] = {0x1, 0x1, 0x4, 0x1, 0x1, 0x1, 0x1, 0x16},
  252. [6] = {},
  253. [7] = {0x1, 0x4, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x4, 0x4, 0x1,
  254. 0x4, 0x1, 0xa, 0x6, 0x1, 0xa, 0x6, 0x1, 0x1, 0x1, 0x1, 0x5,
  255. 0x3, 0x3, 0x4},
  256. [8] = {0x1, 0x4, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x4, 0x4, 0x1,
  257. 0x4, 0x1, 0xa, 0x6, 0x1, 0xa, 0x6, 0x1, 0x1, 0x1, 0x1, 0x5,
  258. 0x3, 0x3, 0x4},
  259. [9] = {0x9, 0x7, 0xf, 0x8, 0x1, 0x8, 0x9, 0x3, 0x3, 0x6, 0x7, 0x4,
  260. 0x9, 0x3, 0x4, 0xe, 0x1, 0x7, 0x8, 0x7, 0x7, 0x1, 0x6, 0x2,
  261. 0xf, 0x8, 0x1, 0x1, 0x1},
  262. [10] = {},
  263. [11] = {0x9, 0x7, 0xf, 0x8, 0x1, 0x8, 0x9, 0x3, 0x3, 0x6, 0x7, 0x4,
  264. 0x9, 0x3, 0x4, 0xe, 0x1, 0x7, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  265. 0x1, 0x1, 0x1, 0x1, 0x1},
  266. [12] = {},
  267. [13] = {0x2, 0xc, 0xc, 0xe, 0x6, 0x6, 0x6, 0x6, 0x6, 0x12, 0x6, 0x1},
  268. [14] = {0x2, 0xc, 0xc, 0x28, 0x12, 0x6},
  269. [15] = {0x28, 0x1, 0x2, 0x28, 0x1},
  270. [16] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x2, 0x14, 0x14, 0x4, 0x4, 0x4, 0x2,
  271. 0x4, 0x2, 0x8, 0x4, 0x4},
  272. [17] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x2, 0x14, 0x14, 0x4, 0x4, 0x4, 0x2,
  273. 0x4, 0x2, 0x8, 0x4, 0x4},
  274. [18] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x2, 0x14, 0x14, 0x4, 0x4, 0x4, 0x2,
  275. 0x4, 0x2, 0x8, 0x4, 0x4},
  276. [19] = {0x2, 0x2, 0x4, 0x2},
  277. [20] = {0x9, 0x9, 0x5, 0x5, 0x1, 0x1},
  278. };
  279. static const u8 mtk_smi_larb_mt8186_ostd[][SMI_LARB_PORT_NR_MAX] = {
  280. [0] = {0x2, 0x1, 0x8, 0x1,},
  281. [1] = {0x1, 0x3, 0x1, 0x1,},
  282. [2] = {0x6, 0x1, 0x4, 0x1,},
  283. [3] = {},
  284. [4] = {0xf, 0x1, 0x5, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  285. 0x1, 0x1, 0x1,},
  286. [5] = {},
  287. [6] = {},
  288. [7] = {0x1, 0x3, 0x1, 0x1, 0x1, 0x3, 0x2, 0xd, 0x7, 0x5, 0x3,
  289. 0x1, 0x5,},
  290. [8] = {0x1, 0x2, 0x2,},
  291. [9] = {0x9, 0x7, 0xf, 0x8, 0x1, 0x8, 0x9, 0x3, 0x3, 0xb, 0x7, 0x4,
  292. 0x9, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  293. 0x1, 0x1, 0x1, 0x1, 0x1,},
  294. [10] = {},
  295. [11] = {0x9, 0x7, 0xf, 0x8, 0x1, 0x8, 0x9, 0x3, 0x3, 0xb, 0x7, 0x4,
  296. 0x9, 0x1, 0x1, 0x1, 0x1, 0x1, 0x8, 0x7, 0x7, 0x1, 0x6, 0x2,
  297. 0xf, 0x8, 0x1, 0x1, 0x1,},
  298. [12] = {},
  299. [13] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x6, 0x6, 0x6, 0x1, 0x1, 0x1,},
  300. [14] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1,},
  301. [15] = {},
  302. [16] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x1, 0x14, 0x1, 0x4, 0x4, 0x4,
  303. 0x2, 0x4, 0x2, 0x8, 0x4, 0x4,},
  304. [17] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x1, 0x14, 0x1, 0x4, 0x4, 0x4,
  305. 0x2, 0x4, 0x2, 0x8, 0x4, 0x4,},
  306. [18] = {},
  307. [19] = {0x1, 0x1, 0x1, 0x1,},
  308. [20] = {0x2, 0x2, 0x2, 0x2, 0x1, 0x1,},
  309. };
  310. static const u8 mtk_smi_larb_mt8188_ostd[][SMI_LARB_PORT_NR_MAX] = {
  311. [0] = {0x02, 0x18, 0x22, 0x22, 0x01, 0x02, 0x0a,},
  312. [1] = {0x12, 0x02, 0x14, 0x14, 0x01, 0x18, 0x0a,},
  313. [2] = {0x12, 0x12, 0x12, 0x12, 0x0a,},
  314. [3] = {0x12, 0x12, 0x12, 0x12, 0x28, 0x28, 0x0a,},
  315. [4] = {0x06, 0x01, 0x17, 0x06, 0x0a, 0x07, 0x07,},
  316. [5] = {0x02, 0x01, 0x04, 0x02, 0x06, 0x01, 0x06, 0x0a,},
  317. [6] = {0x06, 0x01, 0x06, 0x0a,},
  318. [7] = {0x0c, 0x0c, 0x12,},
  319. [8] = {0x0c, 0x01, 0x0a, 0x05, 0x02, 0x03, 0x01, 0x01, 0x14, 0x14,
  320. 0x0a, 0x14, 0x1e, 0x01, 0x0c, 0x0a, 0x05, 0x02, 0x02, 0x05,
  321. 0x03, 0x01, 0x1e, 0x01, 0x05,},
  322. [9] = {0x1e, 0x01, 0x0a, 0x0a, 0x01, 0x01, 0x03, 0x1e, 0x1e, 0x10,
  323. 0x07, 0x01, 0x0a, 0x06, 0x03, 0x03, 0x0e, 0x01, 0x04, 0x28,},
  324. [10] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  325. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  326. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  327. [11] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  328. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  329. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  330. [12] = {0x03, 0x20, 0x01, 0x20, 0x01, 0x01, 0x14, 0x0a, 0x0a, 0x0c,
  331. 0x0a, 0x05, 0x02, 0x03, 0x02, 0x14, 0x0a, 0x0a, 0x14, 0x14,
  332. 0x14, 0x01, 0x01, 0x14, 0x1e, 0x01, 0x05, 0x03, 0x02, 0x28,},
  333. [13] = {0x07, 0x02, 0x04, 0x02, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  334. 0x07, 0x02, 0x04, 0x02, 0x05, 0x05,},
  335. [14] = {0x02, 0x02, 0x0c, 0x0c, 0x0c, 0x0c, 0x01, 0x01, 0x02, 0x02,
  336. 0x02, 0x02, 0x0c, 0x0c, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  337. 0x02, 0x02, 0x01, 0x01,},
  338. [15] = {0x0c, 0x0c, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x0c, 0x0c,
  339. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02,
  340. 0x0c, 0x01, 0x01,},
  341. [16] = {0x28, 0x28, 0x03, 0x01, 0x01, 0x03, 0x14, 0x14, 0x0a, 0x0d,
  342. 0x03, 0x05, 0x0e, 0x01, 0x01, 0x05, 0x06, 0x0d, 0x01,},
  343. [17] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  344. 0x12, 0x02, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  345. [18] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  346. 0x12, 0x02, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  347. [19] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  348. [20] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  349. [21] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  350. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  351. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  352. [22] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,
  353. 0x01,},
  354. [23] = {0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x18, 0x01, 0x01,},
  355. [24] = {0x12, 0x06, 0x12, 0x06,},
  356. [25] = {0x01},
  357. };
  358. static const u8 mtk_smi_larb_mt8192_ostd[][SMI_LARB_PORT_NR_MAX] = {
  359. [0] = {0x2, 0x2, 0x28, 0xa, 0xc, 0x28,},
  360. [1] = {0x2, 0x2, 0x18, 0x18, 0x18, 0xa, 0xc, 0x28,},
  361. [2] = {0x5, 0x5, 0x5, 0x5, 0x1,},
  362. [3] = {},
  363. [4] = {0x28, 0x19, 0xb, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x4, 0x1,},
  364. [5] = {0x1, 0x1, 0x4, 0x1, 0x1, 0x1, 0x1, 0x16,},
  365. [6] = {},
  366. [7] = {0x1, 0x3, 0x2, 0x1, 0x1, 0x5, 0x2, 0x12, 0x13, 0x4, 0x4, 0x1,
  367. 0x4, 0x2, 0x1,},
  368. [8] = {},
  369. [9] = {0xa, 0x7, 0xf, 0x8, 0x1, 0x8, 0x9, 0x3, 0x3, 0x6, 0x7, 0x4,
  370. 0xa, 0x3, 0x4, 0xe, 0x1, 0x7, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  371. 0x1, 0x1, 0x1, 0x1, 0x1,},
  372. [10] = {},
  373. [11] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  374. 0x1, 0x1, 0x1, 0xe, 0x1, 0x7, 0x8, 0x7, 0x7, 0x1, 0x6, 0x2,
  375. 0xf, 0x8, 0x1, 0x1, 0x1,},
  376. [12] = {},
  377. [13] = {0x2, 0xc, 0xc, 0xe, 0x6, 0x6, 0x6, 0x6, 0x6, 0x12, 0x6, 0x28,
  378. 0x2, 0xc, 0xc, 0x28, 0x12, 0x6,},
  379. [14] = {},
  380. [15] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x4, 0x28, 0x14, 0x4, 0x4, 0x4, 0x2,
  381. 0x4, 0x2, 0x8, 0x4, 0x4,},
  382. [16] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x4, 0x28, 0x14, 0x4, 0x4, 0x4, 0x2,
  383. 0x4, 0x2, 0x8, 0x4, 0x4,},
  384. [17] = {0x28, 0x14, 0x2, 0xc, 0x18, 0x4, 0x28, 0x14, 0x4, 0x4, 0x4, 0x2,
  385. 0x4, 0x2, 0x8, 0x4, 0x4,},
  386. [18] = {0x2, 0x2, 0x4, 0x2,},
  387. [19] = {0x9, 0x9, 0x5, 0x5, 0x1, 0x1,},
  388. };
  389. static const u8 mtk_smi_larb_mt8195_ostd[][SMI_LARB_PORT_NR_MAX] = {
  390. [0] = {0x0a, 0xc, 0x22, 0x22, 0x01, 0x0a,}, /* larb0 */
  391. [1] = {0x0a, 0xc, 0x22, 0x22, 0x01, 0x0a,}, /* larb1 */
  392. [2] = {0x12, 0x12, 0x12, 0x12, 0x0a,}, /* ... */
  393. [3] = {0x12, 0x12, 0x12, 0x12, 0x28, 0x28, 0x0a,},
  394. [4] = {0x06, 0x01, 0x17, 0x06, 0x0a,},
  395. [5] = {0x06, 0x01, 0x17, 0x06, 0x06, 0x01, 0x06, 0x0a,},
  396. [6] = {0x06, 0x01, 0x06, 0x0a,},
  397. [7] = {0x0c, 0x0c, 0x12,},
  398. [8] = {0x0c, 0x0c, 0x12,},
  399. [9] = {0x0a, 0x08, 0x04, 0x06, 0x01, 0x01, 0x10, 0x18, 0x11, 0x0a,
  400. 0x08, 0x04, 0x11, 0x06, 0x02, 0x06, 0x01, 0x11, 0x11, 0x06,},
  401. [10] = {0x18, 0x08, 0x01, 0x01, 0x20, 0x12, 0x18, 0x06, 0x05, 0x10,
  402. 0x08, 0x08, 0x10, 0x08, 0x08, 0x18, 0x0c, 0x09, 0x0b, 0x0d,
  403. 0x0d, 0x06, 0x10, 0x10,},
  404. [11] = {0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x01, 0x01, 0x01, 0x01,},
  405. [12] = {0x09, 0x09, 0x05, 0x05, 0x0c, 0x18, 0x02, 0x02, 0x04, 0x02,},
  406. [13] = {0x02, 0x02, 0x12, 0x12, 0x02, 0x02, 0x02, 0x02, 0x08, 0x01,},
  407. [14] = {0x12, 0x12, 0x02, 0x02, 0x02, 0x02, 0x16, 0x01, 0x16, 0x01,
  408. 0x01, 0x02, 0x02, 0x08, 0x02,},
  409. [15] = {},
  410. [16] = {0x28, 0x02, 0x02, 0x12, 0x02, 0x12, 0x10, 0x02, 0x02, 0x0a,
  411. 0x12, 0x02, 0x0a, 0x16, 0x02, 0x04,},
  412. [17] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  413. [18] = {0x12, 0x06, 0x12, 0x06,},
  414. [19] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  415. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  416. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  417. [20] = {0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x01,
  418. 0x01, 0x01, 0x04, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x0a, 0x06,
  419. 0x01, 0x01, 0x05, 0x03, 0x03, 0x04, 0x01,},
  420. [21] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,},
  421. [22] = {0x28, 0x19, 0x0c, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,},
  422. [23] = {0x18, 0x01,},
  423. [24] = {0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01,
  424. 0x01, 0x01,},
  425. [25] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  426. 0x02, 0x01,},
  427. [26] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  428. 0x02, 0x01,},
  429. [27] = {0x02, 0x02, 0x02, 0x28, 0x16, 0x02, 0x02, 0x02, 0x12, 0x16,
  430. 0x02, 0x01,},
  431. [28] = {0x1a, 0x0e, 0x0a, 0x0a, 0x0c, 0x0e, 0x10,},
  432. };
  433. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2701 = {
  434. .port_in_larb = {
  435. LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
  436. LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
  437. },
  438. .config_port = mtk_smi_larb_config_port_gen1,
  439. };
  440. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2712 = {
  441. .config_port = mtk_smi_larb_config_port_gen2_general,
  442. .larb_direct_to_common_mask = BIT(8) | BIT(9), /* bdpsys */
  443. };
  444. static const struct mtk_smi_larb_gen mtk_smi_larb_mt6779 = {
  445. .config_port = mtk_smi_larb_config_port_gen2_general,
  446. .larb_direct_to_common_mask =
  447. BIT(4) | BIT(6) | BIT(11) | BIT(12) | BIT(13),
  448. /* DUMMY | IPU0 | IPU1 | CCU | MDLA */
  449. };
  450. static const struct mtk_smi_larb_gen mtk_smi_larb_mt6893 = {
  451. .config_port = mtk_smi_larb_config_port_gen2_general,
  452. .flags_general = MTK_SMI_FLAG_THRT_UPDATE | MTK_SMI_FLAG_SW_FLAG,
  453. .ostd = mtk_smi_larb_mt6893_ostd,
  454. };
  455. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8167 = {
  456. /* mt8167 do not need the port in larb */
  457. .config_port = mtk_smi_larb_config_port_mt8167,
  458. };
  459. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8173 = {
  460. /* mt8173 do not need the port in larb */
  461. .config_port = mtk_smi_larb_config_port_mt8173,
  462. };
  463. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8183 = {
  464. .config_port = mtk_smi_larb_config_port_gen2_general,
  465. .larb_direct_to_common_mask = BIT(2) | BIT(3) | BIT(7),
  466. /* IPU0 | IPU1 | CCU */
  467. };
  468. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8186 = {
  469. .config_port = mtk_smi_larb_config_port_gen2_general,
  470. .flags_general = MTK_SMI_FLAG_SLEEP_CTL,
  471. .ostd = mtk_smi_larb_mt8186_ostd,
  472. };
  473. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8188 = {
  474. .config_port = mtk_smi_larb_config_port_gen2_general,
  475. .flags_general = MTK_SMI_FLAG_THRT_UPDATE | MTK_SMI_FLAG_SW_FLAG |
  476. MTK_SMI_FLAG_SLEEP_CTL | MTK_SMI_FLAG_CFG_PORT_SEC_CTL,
  477. .ostd = mtk_smi_larb_mt8188_ostd,
  478. };
  479. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8192 = {
  480. .config_port = mtk_smi_larb_config_port_gen2_general,
  481. .ostd = mtk_smi_larb_mt8192_ostd,
  482. };
  483. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8195 = {
  484. .config_port = mtk_smi_larb_config_port_gen2_general,
  485. .flags_general = MTK_SMI_FLAG_THRT_UPDATE | MTK_SMI_FLAG_SW_FLAG |
  486. MTK_SMI_FLAG_SLEEP_CTL,
  487. .ostd = mtk_smi_larb_mt8195_ostd,
  488. };
  489. static const struct of_device_id mtk_smi_larb_of_ids[] = {
  490. {.compatible = "mediatek,mt2701-smi-larb", .data = &mtk_smi_larb_mt2701},
  491. {.compatible = "mediatek,mt2712-smi-larb", .data = &mtk_smi_larb_mt2712},
  492. {.compatible = "mediatek,mt6779-smi-larb", .data = &mtk_smi_larb_mt6779},
  493. {.compatible = "mediatek,mt6795-smi-larb", .data = &mtk_smi_larb_mt8173},
  494. {.compatible = "mediatek,mt6893-smi-larb", .data = &mtk_smi_larb_mt6893},
  495. {.compatible = "mediatek,mt8167-smi-larb", .data = &mtk_smi_larb_mt8167},
  496. {.compatible = "mediatek,mt8173-smi-larb", .data = &mtk_smi_larb_mt8173},
  497. {.compatible = "mediatek,mt8183-smi-larb", .data = &mtk_smi_larb_mt8183},
  498. {.compatible = "mediatek,mt8186-smi-larb", .data = &mtk_smi_larb_mt8186},
  499. {.compatible = "mediatek,mt8188-smi-larb", .data = &mtk_smi_larb_mt8188},
  500. {.compatible = "mediatek,mt8192-smi-larb", .data = &mtk_smi_larb_mt8192},
  501. {.compatible = "mediatek,mt8195-smi-larb", .data = &mtk_smi_larb_mt8195},
  502. {}
  503. };
  504. MODULE_DEVICE_TABLE(of, mtk_smi_larb_of_ids);
  505. static int mtk_smi_larb_sleep_ctrl_enable(struct mtk_smi_larb *larb)
  506. {
  507. int ret;
  508. u32 tmp;
  509. writel_relaxed(SLP_PROT_EN, larb->base + SMI_LARB_SLP_CON);
  510. ret = readl_poll_timeout_atomic(larb->base + SMI_LARB_SLP_CON,
  511. tmp, !!(tmp & SLP_PROT_RDY), 10, 1000);
  512. if (ret) {
  513. /* TODO: Reset this larb if it fails here. */
  514. dev_err(larb->smi.dev, "sleep ctrl is not ready(0x%x).\n", tmp);
  515. }
  516. return ret;
  517. }
  518. static void mtk_smi_larb_sleep_ctrl_disable(struct mtk_smi_larb *larb)
  519. {
  520. writel_relaxed(0, larb->base + SMI_LARB_SLP_CON);
  521. }
  522. static int mtk_smi_device_link_common(struct device *dev, struct device **com_dev)
  523. {
  524. struct platform_device *smi_com_pdev;
  525. struct device_node *smi_com_node;
  526. struct device *smi_com_dev;
  527. struct device_link *link;
  528. smi_com_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0);
  529. if (!smi_com_node)
  530. return -EINVAL;
  531. smi_com_pdev = of_find_device_by_node(smi_com_node);
  532. of_node_put(smi_com_node);
  533. if (!smi_com_pdev) {
  534. dev_err(dev, "Failed to get the smi_common device\n");
  535. return -EINVAL;
  536. }
  537. /* smi common is the supplier, Make sure it is ready before */
  538. if (!platform_get_drvdata(smi_com_pdev)) {
  539. put_device(&smi_com_pdev->dev);
  540. return -EPROBE_DEFER;
  541. }
  542. smi_com_dev = &smi_com_pdev->dev;
  543. link = device_link_add(dev, smi_com_dev,
  544. DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
  545. if (!link) {
  546. dev_err(dev, "Unable to link smi-common dev\n");
  547. put_device(&smi_com_pdev->dev);
  548. return -ENODEV;
  549. }
  550. *com_dev = smi_com_dev;
  551. return 0;
  552. }
  553. static int mtk_smi_dts_clk_init(struct device *dev, struct mtk_smi *smi,
  554. const char * const clks[],
  555. unsigned int clk_nr_required,
  556. unsigned int clk_nr_optional)
  557. {
  558. int i, ret;
  559. for (i = 0; i < clk_nr_required; i++)
  560. smi->clks[i].id = clks[i];
  561. ret = devm_clk_bulk_get(dev, clk_nr_required, smi->clks);
  562. if (ret)
  563. return ret;
  564. for (i = clk_nr_required; i < clk_nr_required + clk_nr_optional; i++)
  565. smi->clks[i].id = clks[i];
  566. ret = devm_clk_bulk_get_optional(dev, clk_nr_optional,
  567. smi->clks + clk_nr_required);
  568. smi->clk_num = clk_nr_required + clk_nr_optional;
  569. return ret;
  570. }
  571. static int mtk_smi_larb_probe(struct platform_device *pdev)
  572. {
  573. struct mtk_smi_larb *larb;
  574. struct device *dev = &pdev->dev;
  575. int ret;
  576. larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL);
  577. if (!larb)
  578. return -ENOMEM;
  579. larb->larb_gen = of_device_get_match_data(dev);
  580. larb->base = devm_platform_ioremap_resource(pdev, 0);
  581. if (IS_ERR(larb->base))
  582. return PTR_ERR(larb->base);
  583. ret = mtk_smi_dts_clk_init(dev, &larb->smi, mtk_smi_larb_clks,
  584. MTK_SMI_LARB_REQ_CLK_NR, MTK_SMI_LARB_OPT_CLK_NR);
  585. if (ret)
  586. return ret;
  587. larb->smi.dev = dev;
  588. ret = mtk_smi_device_link_common(dev, &larb->smi_common_dev);
  589. if (ret < 0)
  590. return ret;
  591. pm_runtime_enable(dev);
  592. platform_set_drvdata(pdev, larb);
  593. ret = component_add(dev, &mtk_smi_larb_component_ops);
  594. if (ret)
  595. goto err_pm_disable;
  596. return 0;
  597. err_pm_disable:
  598. pm_runtime_disable(dev);
  599. device_link_remove(dev, larb->smi_common_dev);
  600. put_device(larb->smi_common_dev);
  601. return ret;
  602. }
  603. static void mtk_smi_larb_remove(struct platform_device *pdev)
  604. {
  605. struct mtk_smi_larb *larb = platform_get_drvdata(pdev);
  606. device_link_remove(&pdev->dev, larb->smi_common_dev);
  607. pm_runtime_disable(&pdev->dev);
  608. component_del(&pdev->dev, &mtk_smi_larb_component_ops);
  609. put_device(larb->smi_common_dev);
  610. }
  611. static int __maybe_unused mtk_smi_larb_resume(struct device *dev)
  612. {
  613. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  614. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  615. int ret;
  616. ret = clk_bulk_prepare_enable(larb->smi.clk_num, larb->smi.clks);
  617. if (ret)
  618. return ret;
  619. if (MTK_SMI_CAPS(larb->larb_gen->flags_general, MTK_SMI_FLAG_SLEEP_CTL))
  620. mtk_smi_larb_sleep_ctrl_disable(larb);
  621. /* Configure the basic setting for this larb */
  622. return larb_gen->config_port(dev);
  623. }
  624. static int __maybe_unused mtk_smi_larb_suspend(struct device *dev)
  625. {
  626. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  627. int ret;
  628. if (MTK_SMI_CAPS(larb->larb_gen->flags_general, MTK_SMI_FLAG_SLEEP_CTL)) {
  629. ret = mtk_smi_larb_sleep_ctrl_enable(larb);
  630. if (ret)
  631. return ret;
  632. }
  633. clk_bulk_disable_unprepare(larb->smi.clk_num, larb->smi.clks);
  634. return 0;
  635. }
  636. static const struct dev_pm_ops smi_larb_pm_ops = {
  637. SET_RUNTIME_PM_OPS(mtk_smi_larb_suspend, mtk_smi_larb_resume, NULL)
  638. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  639. pm_runtime_force_resume)
  640. };
  641. static struct platform_driver mtk_smi_larb_driver = {
  642. .probe = mtk_smi_larb_probe,
  643. .remove = mtk_smi_larb_remove,
  644. .driver = {
  645. .name = "mtk-smi-larb",
  646. .of_match_table = mtk_smi_larb_of_ids,
  647. .pm = &smi_larb_pm_ops,
  648. }
  649. };
  650. static const struct mtk_smi_reg_pair mtk_smi_common_mt6795_init[SMI_COMMON_INIT_REGS_NR] = {
  651. {SMI_L1_ARB, 0x1b},
  652. {SMI_M4U_TH, 0xce810c85},
  653. {SMI_FIFO_TH1, 0x43214c8},
  654. {SMI_READ_FIFO_TH, 0x191f},
  655. };
  656. static const struct mtk_smi_reg_pair mtk_smi_common_mt8195_init[SMI_COMMON_INIT_REGS_NR] = {
  657. {SMI_L1LEN, 0xb},
  658. {SMI_M4U_TH, 0xe100e10},
  659. {SMI_FIFO_TH1, 0x506090a},
  660. {SMI_FIFO_TH2, 0x506090a},
  661. {SMI_DCM, 0x4f1},
  662. {SMI_DUMMY, 0x1},
  663. };
  664. static const struct mtk_smi_common_plat mtk_smi_common_gen1 = {
  665. .type = MTK_SMI_GEN1,
  666. };
  667. static const struct mtk_smi_common_plat mtk_smi_common_gen2 = {
  668. .type = MTK_SMI_GEN2,
  669. };
  670. static const struct mtk_smi_common_plat mtk_smi_common_mt6779 = {
  671. .type = MTK_SMI_GEN2,
  672. .has_gals = true,
  673. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(4) |
  674. F_MMU1_LARB(5) | F_MMU1_LARB(6) | F_MMU1_LARB(7),
  675. };
  676. static const struct mtk_smi_common_plat mtk_smi_common_mt6795 = {
  677. .type = MTK_SMI_GEN2,
  678. .bus_sel = F_MMU1_LARB(0),
  679. .init = mtk_smi_common_mt6795_init,
  680. };
  681. static const struct mtk_smi_common_plat mtk_smi_common_mt6893 = {
  682. .type = MTK_SMI_GEN2,
  683. .has_gals = true,
  684. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(4) |
  685. F_MMU1_LARB(5) | F_MMU1_LARB(7),
  686. };
  687. static const struct mtk_smi_common_plat mtk_smi_common_mt8183 = {
  688. .type = MTK_SMI_GEN2,
  689. .has_gals = true,
  690. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(5) |
  691. F_MMU1_LARB(7),
  692. };
  693. static const struct mtk_smi_common_plat mtk_smi_common_mt8186 = {
  694. .type = MTK_SMI_GEN2,
  695. .has_gals = true,
  696. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(4) | F_MMU1_LARB(7),
  697. };
  698. static const struct mtk_smi_common_plat mtk_smi_common_mt8188_vdo = {
  699. .type = MTK_SMI_GEN2,
  700. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(5) | F_MMU1_LARB(7),
  701. .init = mtk_smi_common_mt8195_init,
  702. };
  703. static const struct mtk_smi_common_plat mtk_smi_common_mt8188_vpp = {
  704. .type = MTK_SMI_GEN2,
  705. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(7),
  706. .init = mtk_smi_common_mt8195_init,
  707. };
  708. static const struct mtk_smi_common_plat mtk_smi_common_mt8192 = {
  709. .type = MTK_SMI_GEN2,
  710. .has_gals = true,
  711. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(5) |
  712. F_MMU1_LARB(6),
  713. };
  714. static const struct mtk_smi_common_plat mtk_smi_common_mt8195_vdo = {
  715. .type = MTK_SMI_GEN2,
  716. .has_gals = true,
  717. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(3) | F_MMU1_LARB(5) |
  718. F_MMU1_LARB(7),
  719. .init = mtk_smi_common_mt8195_init,
  720. };
  721. static const struct mtk_smi_common_plat mtk_smi_common_mt8195_vpp = {
  722. .type = MTK_SMI_GEN2,
  723. .has_gals = true,
  724. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(7),
  725. .init = mtk_smi_common_mt8195_init,
  726. };
  727. static const struct mtk_smi_common_plat mtk_smi_sub_common_mt8195 = {
  728. .type = MTK_SMI_GEN2_SUB_COMM,
  729. .has_gals = true,
  730. };
  731. static const struct mtk_smi_common_plat mtk_smi_common_mt8365 = {
  732. .type = MTK_SMI_GEN2,
  733. .bus_sel = F_MMU1_LARB(2) | F_MMU1_LARB(4),
  734. };
  735. static const struct of_device_id mtk_smi_common_of_ids[] = {
  736. {.compatible = "mediatek,mt2701-smi-common", .data = &mtk_smi_common_gen1},
  737. {.compatible = "mediatek,mt2712-smi-common", .data = &mtk_smi_common_gen2},
  738. {.compatible = "mediatek,mt6779-smi-common", .data = &mtk_smi_common_mt6779},
  739. {.compatible = "mediatek,mt6795-smi-common", .data = &mtk_smi_common_mt6795},
  740. {.compatible = "mediatek,mt6893-smi-common", .data = &mtk_smi_common_mt6893},
  741. {.compatible = "mediatek,mt8167-smi-common", .data = &mtk_smi_common_gen2},
  742. {.compatible = "mediatek,mt8173-smi-common", .data = &mtk_smi_common_gen2},
  743. {.compatible = "mediatek,mt8183-smi-common", .data = &mtk_smi_common_mt8183},
  744. {.compatible = "mediatek,mt8186-smi-common", .data = &mtk_smi_common_mt8186},
  745. {.compatible = "mediatek,mt8188-smi-common-vdo", .data = &mtk_smi_common_mt8188_vdo},
  746. {.compatible = "mediatek,mt8188-smi-common-vpp", .data = &mtk_smi_common_mt8188_vpp},
  747. {.compatible = "mediatek,mt8192-smi-common", .data = &mtk_smi_common_mt8192},
  748. {.compatible = "mediatek,mt8195-smi-common-vdo", .data = &mtk_smi_common_mt8195_vdo},
  749. {.compatible = "mediatek,mt8195-smi-common-vpp", .data = &mtk_smi_common_mt8195_vpp},
  750. {.compatible = "mediatek,mt8195-smi-sub-common", .data = &mtk_smi_sub_common_mt8195},
  751. {.compatible = "mediatek,mt8365-smi-common", .data = &mtk_smi_common_mt8365},
  752. {}
  753. };
  754. MODULE_DEVICE_TABLE(of, mtk_smi_common_of_ids);
  755. static int mtk_smi_common_probe(struct platform_device *pdev)
  756. {
  757. struct device *dev = &pdev->dev;
  758. struct mtk_smi *common;
  759. int ret, clk_required = MTK_SMI_COM_REQ_CLK_NR;
  760. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  761. if (!common)
  762. return -ENOMEM;
  763. common->dev = dev;
  764. common->plat = of_device_get_match_data(dev);
  765. if (common->plat->has_gals) {
  766. if (common->plat->type == MTK_SMI_GEN2)
  767. clk_required = MTK_SMI_COM_GALS_REQ_CLK_NR;
  768. else if (common->plat->type == MTK_SMI_GEN2_SUB_COMM)
  769. clk_required = MTK_SMI_SUB_COM_GALS_REQ_CLK_NR;
  770. }
  771. ret = mtk_smi_dts_clk_init(dev, common, mtk_smi_common_clks, clk_required, 0);
  772. if (ret)
  773. return ret;
  774. /*
  775. * for mtk smi gen 1, we need to get the ao(always on) base to config
  776. * m4u port, and we need to enable the aync clock for transform the smi
  777. * clock into emi clock domain, but for mtk smi gen2, there's no smi ao
  778. * base.
  779. */
  780. if (common->plat->type == MTK_SMI_GEN1) {
  781. common->smi_ao_base = devm_platform_ioremap_resource(pdev, 0);
  782. if (IS_ERR(common->smi_ao_base))
  783. return PTR_ERR(common->smi_ao_base);
  784. common->clk_async = devm_clk_get_enabled(dev, "async");
  785. if (IS_ERR(common->clk_async))
  786. return PTR_ERR(common->clk_async);
  787. } else {
  788. common->base = devm_platform_ioremap_resource(pdev, 0);
  789. if (IS_ERR(common->base))
  790. return PTR_ERR(common->base);
  791. }
  792. /* link its smi-common if this is smi-sub-common */
  793. if (common->plat->type == MTK_SMI_GEN2_SUB_COMM) {
  794. ret = mtk_smi_device_link_common(dev, &common->smi_common_dev);
  795. if (ret < 0)
  796. return ret;
  797. }
  798. pm_runtime_enable(dev);
  799. platform_set_drvdata(pdev, common);
  800. return 0;
  801. }
  802. static void mtk_smi_common_remove(struct platform_device *pdev)
  803. {
  804. struct mtk_smi *common = dev_get_drvdata(&pdev->dev);
  805. if (common->plat->type == MTK_SMI_GEN2_SUB_COMM)
  806. device_link_remove(&pdev->dev, common->smi_common_dev);
  807. pm_runtime_disable(&pdev->dev);
  808. put_device(common->smi_common_dev);
  809. }
  810. static int __maybe_unused mtk_smi_common_resume(struct device *dev)
  811. {
  812. struct mtk_smi *common = dev_get_drvdata(dev);
  813. const struct mtk_smi_reg_pair *init = common->plat->init;
  814. u32 bus_sel = common->plat->bus_sel; /* default is 0 */
  815. int ret, i;
  816. ret = clk_bulk_prepare_enable(common->clk_num, common->clks);
  817. if (ret)
  818. return ret;
  819. if (common->plat->type != MTK_SMI_GEN2)
  820. return 0;
  821. for (i = 0; i < SMI_COMMON_INIT_REGS_NR && init && init[i].offset; i++)
  822. writel_relaxed(init[i].value, common->base + init[i].offset);
  823. writel(bus_sel, common->base + SMI_BUS_SEL);
  824. return 0;
  825. }
  826. static int __maybe_unused mtk_smi_common_suspend(struct device *dev)
  827. {
  828. struct mtk_smi *common = dev_get_drvdata(dev);
  829. clk_bulk_disable_unprepare(common->clk_num, common->clks);
  830. return 0;
  831. }
  832. static const struct dev_pm_ops smi_common_pm_ops = {
  833. SET_RUNTIME_PM_OPS(mtk_smi_common_suspend, mtk_smi_common_resume, NULL)
  834. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  835. pm_runtime_force_resume)
  836. };
  837. static struct platform_driver mtk_smi_common_driver = {
  838. .probe = mtk_smi_common_probe,
  839. .remove = mtk_smi_common_remove,
  840. .driver = {
  841. .name = "mtk-smi-common",
  842. .of_match_table = mtk_smi_common_of_ids,
  843. .pm = &smi_common_pm_ops,
  844. }
  845. };
  846. static struct platform_driver * const smidrivers[] = {
  847. &mtk_smi_common_driver,
  848. &mtk_smi_larb_driver,
  849. };
  850. static int __init mtk_smi_init(void)
  851. {
  852. return platform_register_drivers(smidrivers, ARRAY_SIZE(smidrivers));
  853. }
  854. module_init(mtk_smi_init);
  855. static void __exit mtk_smi_exit(void)
  856. {
  857. platform_unregister_drivers(smidrivers, ARRAY_SIZE(smidrivers));
  858. }
  859. module_exit(mtk_smi_exit);
  860. MODULE_DESCRIPTION("MediaTek SMI driver");
  861. MODULE_LICENSE("GPL v2");