mtk-smi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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/iopoll.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <soc/mediatek/smi.h>
  28. #include <dt-bindings/memory/mt2701-larb-port.h>
  29. /* mt8173 */
  30. #define SMI_LARB_MMU_EN 0xf00
  31. #define SMI_LARB_MMU_EN_MT8167 0xfc0
  32. /* mt2701 */
  33. #define REG_SMI_SECUR_CON_BASE 0x5c0
  34. /* every register control 8 port, register offset 0x4 */
  35. #define REG_SMI_SECUR_CON_OFFSET(id) (((id) >> 3) << 2)
  36. #define REG_SMI_SECUR_CON_ADDR(id) \
  37. (REG_SMI_SECUR_CON_BASE + REG_SMI_SECUR_CON_OFFSET(id))
  38. /*
  39. * every port have 4 bit to control, bit[port + 3] control virtual or physical,
  40. * bit[port + 2 : port + 1] control the domain, bit[port] control the security
  41. * or non-security.
  42. */
  43. #define SMI_SECUR_CON_VAL_MSK(id) (~(0xf << (((id) & 0x7) << 2)))
  44. #define SMI_SECUR_CON_VAL_VIRT(id) BIT((((id) & 0x7) << 2) + 3)
  45. /* mt2701 domain should be set to 3 */
  46. #define SMI_SECUR_CON_VAL_DOMAIN(id) (0x3 << ((((id) & 0x7) << 2) + 1))
  47. /* mt2712 */
  48. #define SMI_LARB_NONSEC_CON(id) (0x380 + ((id) * 4))
  49. #define F_MMU_EN BIT(0)
  50. #define SMI_LARB_SLP_CON 0x00c
  51. #define SLP_PROT_EN BIT(0)
  52. #define SLP_PROT_RDY BIT(16)
  53. /* mt8168 */
  54. #define MMSYS_HW_DCM_1ST_DIS_SET0 0x124
  55. #define MMSYS_HW_DCM_1ST_DIS_CLR0 0x128
  56. #define DCM_1ST_DIS0_SMI_COMM0 BIT(24)
  57. #define DCM_1ST_DIS0_SMI_COMM1 BIT(25)
  58. /* SMI COMMON */
  59. #define SMI_BUS_SEL 0x220
  60. #define SMI_BUS_LARB_SHIFT(larbid) ((larbid) << 1)
  61. /* All are MMU0 defaultly. Only specialize mmu1 here. */
  62. #define F_MMU1_LARB(larbid) (0x1 << SMI_BUS_LARB_SHIFT(larbid))
  63. #if !IS_ENABLED(CONFIG_MTK_SMI_EXT)
  64. enum mtk_smi_gen {
  65. MTK_SMI_GEN1,
  66. MTK_SMI_GEN2
  67. };
  68. struct mtk_smi_common_plat {
  69. enum mtk_smi_gen gen;
  70. /* Adjust some larbs to mmu1 to balance the bandwidth */
  71. unsigned int bus_sel;
  72. bool support_dcm;
  73. void (*smi_comm_dcm_ctrl)(struct regmap *mmcfg_regmap, bool dis);
  74. };
  75. struct mtk_smi_larb_gen {
  76. bool need_larbid;
  77. int port_in_larb[MTK_LARB_NR_MAX + 1];
  78. void (*config_port)(struct device *);
  79. void (*larb_sleep_ctrl)(struct device *dev, bool toslp);
  80. };
  81. struct mtk_smi {
  82. struct device *dev;
  83. struct clk *clk_apb, *clk_smi;
  84. struct clk *clk_gals0, *clk_gals1;
  85. struct clk *clk_async; /*only needed by mt2701*/
  86. void __iomem *smi_ao_base; /* only for gen1 */
  87. void __iomem *base; /* only for gen2 */
  88. const struct mtk_smi_common_plat *plat;
  89. struct regmap *mmsyscfg_regmap;
  90. };
  91. struct mtk_smi_larb { /* larb: local arbiter */
  92. struct mtk_smi smi;
  93. void __iomem *base;
  94. struct device *smi_common_dev;
  95. const struct mtk_smi_larb_gen *larb_gen;
  96. int larbid;
  97. u32 *mmu;
  98. };
  99. static int mtk_smi_clk_enable(const struct mtk_smi *smi)
  100. {
  101. int ret;
  102. ret = clk_prepare_enable(smi->clk_apb);
  103. if (ret)
  104. return ret;
  105. ret = clk_prepare_enable(smi->clk_smi);
  106. if (ret)
  107. goto err_disable_apb;
  108. ret = clk_prepare_enable(smi->clk_gals0);
  109. if (ret)
  110. goto err_disable_smi;
  111. ret = clk_prepare_enable(smi->clk_gals1);
  112. if (ret)
  113. goto err_disable_gals0;
  114. return 0;
  115. err_disable_gals0:
  116. clk_disable_unprepare(smi->clk_gals0);
  117. err_disable_smi:
  118. clk_disable_unprepare(smi->clk_smi);
  119. err_disable_apb:
  120. clk_disable_unprepare(smi->clk_apb);
  121. return ret;
  122. }
  123. static void mtk_smi_clk_disable(const struct mtk_smi *smi)
  124. {
  125. clk_disable_unprepare(smi->clk_gals1);
  126. clk_disable_unprepare(smi->clk_gals0);
  127. clk_disable_unprepare(smi->clk_smi);
  128. clk_disable_unprepare(smi->clk_apb);
  129. }
  130. static int
  131. mtk_smi_larb_bind(struct device *dev, struct device *master, void *data)
  132. {
  133. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  134. struct mtk_smi_iommu *smi_iommu = data;
  135. unsigned int i;
  136. if (larb->larb_gen->need_larbid) {
  137. larb->mmu = &smi_iommu->larb_imu[larb->larbid].mmu;
  138. return 0;
  139. }
  140. /*
  141. * If there is no larbid property, Loop to find the corresponding
  142. * iommu information.
  143. */
  144. for (i = 0; i < smi_iommu->larb_nr; i++) {
  145. if (dev == smi_iommu->larb_imu[i].dev) {
  146. /* The 'mmu' may be updated in iommu-attach/detach. */
  147. larb->mmu = &smi_iommu->larb_imu[i].mmu;
  148. return 0;
  149. }
  150. }
  151. return -ENODEV;
  152. }
  153. static void mtk_smi_larb_config_port_gen2_general(struct device *dev)
  154. {
  155. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  156. u32 reg;
  157. int i;
  158. for_each_set_bit(i, (unsigned long *)larb->mmu, 32) {
  159. reg = readl_relaxed(larb->base + SMI_LARB_NONSEC_CON(i));
  160. reg |= F_MMU_EN;
  161. writel(reg, larb->base + SMI_LARB_NONSEC_CON(i));
  162. }
  163. }
  164. static void mtk_smi_larb_config_port_mt2712(struct device *dev)
  165. {
  166. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  167. /*
  168. * larb 8/9 is the bdpsys larb, the iommu_en is enabled defaultly.
  169. * Don't need to set it again.
  170. */
  171. if (larb->larbid == 8 || larb->larbid == 9)
  172. return;
  173. mtk_smi_larb_config_port_gen2_general(dev);
  174. }
  175. static void mtk_smi_larb_config_port_mt8173(struct device *dev)
  176. {
  177. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  178. writel(*larb->mmu, larb->base + SMI_LARB_MMU_EN);
  179. }
  180. static void mtk_smi_larb_config_port_mt8167(struct device *dev)
  181. {
  182. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  183. writel(*larb->mmu, larb->base + SMI_LARB_MMU_EN_MT8167);
  184. }
  185. static void mtk_smi_larb_config_port_gen1(struct device *dev)
  186. {
  187. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  188. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  189. struct mtk_smi *common = dev_get_drvdata(larb->smi_common_dev);
  190. int i, m4u_port_id, larb_port_num;
  191. u32 sec_con_val, reg_val;
  192. m4u_port_id = larb_gen->port_in_larb[larb->larbid];
  193. larb_port_num = larb_gen->port_in_larb[larb->larbid + 1]
  194. - larb_gen->port_in_larb[larb->larbid];
  195. for (i = 0; i < larb_port_num; i++, m4u_port_id++) {
  196. if (*larb->mmu & BIT(i)) {
  197. /* bit[port + 3] controls the virtual or physical */
  198. sec_con_val = SMI_SECUR_CON_VAL_VIRT(m4u_port_id);
  199. } else {
  200. /* do not need to enable m4u for this port */
  201. continue;
  202. }
  203. reg_val = readl(common->smi_ao_base
  204. + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
  205. reg_val &= SMI_SECUR_CON_VAL_MSK(m4u_port_id);
  206. reg_val |= sec_con_val;
  207. reg_val |= SMI_SECUR_CON_VAL_DOMAIN(m4u_port_id);
  208. writel(reg_val,
  209. common->smi_ao_base
  210. + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
  211. }
  212. }
  213. static void mtk_smi_larb_sleep_ctrl_mt8168(struct device *dev, bool toslp)
  214. {
  215. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  216. void __iomem *base = larb->base;
  217. u32 tmp;
  218. /* larb4 should be use a general way. */
  219. if (larb->larbid == 4)
  220. return;
  221. if (toslp) {
  222. writel_relaxed(SLP_PROT_EN, base + SMI_LARB_SLP_CON);
  223. if (readl_poll_timeout_atomic(base + SMI_LARB_SLP_CON,
  224. tmp, !!(tmp & SLP_PROT_RDY), 10, 10000))
  225. dev_warn(dev, "larb sleep con not ready(%d)\n", tmp);
  226. } else
  227. writel_relaxed(0, base + SMI_LARB_SLP_CON);
  228. }
  229. static void
  230. mtk_smi_larb_unbind(struct device *dev, struct device *master, void *data)
  231. {
  232. /* Do nothing as the iommu is always enabled. */
  233. }
  234. static const struct component_ops mtk_smi_larb_component_ops = {
  235. .bind = mtk_smi_larb_bind,
  236. .unbind = mtk_smi_larb_unbind,
  237. };
  238. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8173 = {
  239. /* mt8173 do not need the port in larb */
  240. .config_port = mtk_smi_larb_config_port_mt8173,
  241. };
  242. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8167 = {
  243. .config_port = mtk_smi_larb_config_port_mt8167,
  244. };
  245. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2701 = {
  246. .need_larbid = true,
  247. .port_in_larb = {
  248. LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
  249. LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
  250. },
  251. .config_port = mtk_smi_larb_config_port_gen1,
  252. };
  253. static const struct mtk_smi_larb_gen mtk_smi_larb_mt2712 = {
  254. .need_larbid = true,
  255. .config_port = mtk_smi_larb_config_port_mt2712,
  256. };
  257. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8183 = {
  258. .config_port = mtk_smi_larb_config_port_gen2_general,
  259. };
  260. static const struct mtk_smi_larb_gen mtk_smi_larb_mt8168 = {
  261. .config_port = mtk_smi_larb_config_port_gen2_general,
  262. .larb_sleep_ctrl = mtk_smi_larb_sleep_ctrl_mt8168,
  263. };
  264. static const struct of_device_id mtk_smi_larb_of_ids[] = {
  265. {
  266. .compatible = "mediatek,mt8173-smi-larb",
  267. .data = &mtk_smi_larb_mt8173
  268. },
  269. {
  270. .compatible = "mediatek,mt8167-smi-larb",
  271. .data = &mtk_smi_larb_mt8167
  272. },
  273. {
  274. .compatible = "mediatek,mt2701-smi-larb",
  275. .data = &mtk_smi_larb_mt2701
  276. },
  277. {
  278. .compatible = "mediatek,mt2712-smi-larb",
  279. .data = &mtk_smi_larb_mt2712
  280. },
  281. {
  282. .compatible = "mediatek,mt8183-smi-larb",
  283. .data = &mtk_smi_larb_mt8183
  284. },
  285. {
  286. .compatible = "mediatek,mt8168-smi-larb",
  287. .data = &mtk_smi_larb_mt8168
  288. },
  289. {}
  290. };
  291. static int mtk_smi_larb_probe(struct platform_device *pdev)
  292. {
  293. struct mtk_smi_larb *larb;
  294. struct resource *res;
  295. struct device *dev = &pdev->dev;
  296. struct device_node *smi_node;
  297. struct platform_device *smi_pdev;
  298. int err;
  299. larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL);
  300. if (!larb)
  301. return -ENOMEM;
  302. larb->larb_gen = of_device_get_match_data(dev);
  303. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  304. larb->base = devm_ioremap_resource(dev, res);
  305. if (IS_ERR(larb->base))
  306. return PTR_ERR(larb->base);
  307. larb->smi.clk_apb = devm_clk_get(dev, "apb");
  308. if (IS_ERR(larb->smi.clk_apb))
  309. return PTR_ERR(larb->smi.clk_apb);
  310. larb->smi.clk_smi = devm_clk_get(dev, "smi");
  311. if (IS_ERR(larb->smi.clk_smi))
  312. return PTR_ERR(larb->smi.clk_smi);
  313. larb->smi.clk_gals0 = devm_clk_get(dev, "gals");
  314. if (PTR_ERR(larb->smi.clk_gals0) == -ENOENT)
  315. larb->smi.clk_gals0 = NULL;
  316. else if (IS_ERR(larb->smi.clk_gals0))
  317. return PTR_ERR(larb->smi.clk_gals0);
  318. larb->smi.dev = dev;
  319. if (larb->larb_gen->need_larbid) {
  320. err = of_property_read_u32(dev->of_node, "mediatek,larb-id",
  321. &larb->larbid);
  322. if (err) {
  323. dev_err(dev, "missing larbid property\n");
  324. return err;
  325. }
  326. }
  327. smi_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0);
  328. if (!smi_node)
  329. return -EINVAL;
  330. smi_pdev = of_find_device_by_node(smi_node);
  331. of_node_put(smi_node);
  332. if (smi_pdev) {
  333. if (!platform_get_drvdata(smi_pdev))
  334. return -EPROBE_DEFER;
  335. larb->smi_common_dev = &smi_pdev->dev;
  336. } else {
  337. dev_err(dev, "Failed to get the smi_common device\n");
  338. return -EINVAL;
  339. }
  340. pm_runtime_enable(dev);
  341. platform_set_drvdata(pdev, larb);
  342. return component_add(dev, &mtk_smi_larb_component_ops);
  343. }
  344. static int mtk_smi_larb_remove(struct platform_device *pdev)
  345. {
  346. pm_runtime_disable(&pdev->dev);
  347. component_del(&pdev->dev, &mtk_smi_larb_component_ops);
  348. return 0;
  349. }
  350. static int __maybe_unused mtk_smi_larb_resume(struct device *dev)
  351. {
  352. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  353. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  354. int ret;
  355. /* Power on smi-common. */
  356. ret = pm_runtime_get_sync(larb->smi_common_dev);
  357. if (ret < 0) {
  358. dev_err(dev, "smi-common pm get failed(%d).\n", ret);
  359. return ret;
  360. }
  361. ret = mtk_smi_clk_enable(&larb->smi);
  362. if (ret < 0) {
  363. dev_err(dev, "larb clk enable failed(%d).\n", ret);
  364. pm_runtime_put_sync(larb->smi_common_dev);
  365. return ret;
  366. }
  367. if (larb_gen->larb_sleep_ctrl)
  368. larb_gen->larb_sleep_ctrl(dev, false);
  369. /* Configure the basic setting for this larb */
  370. larb_gen->config_port(dev);
  371. return 0;
  372. }
  373. static int __maybe_unused mtk_smi_larb_suspend(struct device *dev)
  374. {
  375. struct mtk_smi_larb *larb = dev_get_drvdata(dev);
  376. const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
  377. if (larb_gen->larb_sleep_ctrl)
  378. larb_gen->larb_sleep_ctrl(dev, true);
  379. mtk_smi_clk_disable(&larb->smi);
  380. pm_runtime_put_sync(larb->smi_common_dev);
  381. return 0;
  382. }
  383. static const struct dev_pm_ops smi_larb_pm_ops = {
  384. SET_RUNTIME_PM_OPS(mtk_smi_larb_suspend, mtk_smi_larb_resume, NULL)
  385. };
  386. static struct platform_driver mtk_smi_larb_driver = {
  387. .probe = mtk_smi_larb_probe,
  388. .remove = mtk_smi_larb_remove,
  389. .driver = {
  390. .name = "mtk-smi-larb",
  391. .of_match_table = mtk_smi_larb_of_ids,
  392. .pm = &smi_larb_pm_ops,
  393. }
  394. };
  395. static void mtk_smi_comm_dcm_ctrl_mt8168(struct regmap *mmcfg_regmap,
  396. bool disable)
  397. {
  398. if (disable) {
  399. /* disable dcm for smi comm 0 & smi comm 1 */
  400. regmap_write(mmcfg_regmap, MMSYS_HW_DCM_1ST_DIS_SET0,
  401. (DCM_1ST_DIS0_SMI_COMM0 | DCM_1ST_DIS0_SMI_COMM1));
  402. } else {
  403. /* enable dcm for smi comm 0 & smi comm 1 */
  404. regmap_write(mmcfg_regmap, MMSYS_HW_DCM_1ST_DIS_CLR0,
  405. (DCM_1ST_DIS0_SMI_COMM0 | DCM_1ST_DIS0_SMI_COMM1));
  406. }
  407. }
  408. static const struct mtk_smi_common_plat mtk_smi_common_gen1 = {
  409. .gen = MTK_SMI_GEN1,
  410. };
  411. static const struct mtk_smi_common_plat mtk_smi_common_gen2 = {
  412. .gen = MTK_SMI_GEN2,
  413. };
  414. static const struct mtk_smi_common_plat mtk_smi_common_mt8183 = {
  415. .gen = MTK_SMI_GEN2,
  416. .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(3) | F_MMU1_LARB(4) |
  417. F_MMU1_LARB(7),
  418. };
  419. static const struct mtk_smi_common_plat mtk_smi_common_mt8168 = {
  420. .gen = MTK_SMI_GEN2,
  421. .support_dcm = true,
  422. .smi_comm_dcm_ctrl = mtk_smi_comm_dcm_ctrl_mt8168,
  423. };
  424. static const struct of_device_id mtk_smi_common_of_ids[] = {
  425. {
  426. .compatible = "mediatek,mt8173-smi-common",
  427. .data = &mtk_smi_common_gen2,
  428. },
  429. {
  430. .compatible = "mediatek,mt8167-smi-common",
  431. .data = &mtk_smi_common_gen2,
  432. },
  433. {
  434. .compatible = "mediatek,mt2701-smi-common",
  435. .data = &mtk_smi_common_gen1,
  436. },
  437. {
  438. .compatible = "mediatek,mt2712-smi-common",
  439. .data = &mtk_smi_common_gen2,
  440. },
  441. {
  442. .compatible = "mediatek,mt8168-smi-common",
  443. .data = &mtk_smi_common_mt8168,
  444. },
  445. {
  446. .compatible = "mediatek,mt8183-smi-common",
  447. .data = &mtk_smi_common_mt8183,
  448. },
  449. {}
  450. };
  451. #ifdef CONFIG_MACH_MT8167
  452. static struct mtk_smi *gmtk_common_dev;
  453. #endif
  454. static int mtk_smi_common_probe(struct platform_device *pdev)
  455. {
  456. struct device *dev = &pdev->dev;
  457. struct mtk_smi *common;
  458. struct resource *res;
  459. int ret;
  460. common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
  461. if (!common)
  462. return -ENOMEM;
  463. common->dev = dev;
  464. common->plat = of_device_get_match_data(dev);
  465. common->clk_apb = devm_clk_get(dev, "apb");
  466. if (IS_ERR(common->clk_apb))
  467. return PTR_ERR(common->clk_apb);
  468. common->clk_smi = devm_clk_get(dev, "smi");
  469. if (IS_ERR(common->clk_smi))
  470. return PTR_ERR(common->clk_smi);
  471. common->clk_gals0 = devm_clk_get(dev, "gals0");
  472. if (PTR_ERR(common->clk_gals0) == -ENOENT)
  473. common->clk_gals0 = NULL;
  474. else if (IS_ERR(common->clk_gals0))
  475. return PTR_ERR(common->clk_gals0);
  476. common->clk_gals1 = devm_clk_get(dev, "gals1");
  477. if (PTR_ERR(common->clk_gals1) == -ENOENT)
  478. common->clk_gals1 = NULL;
  479. else if (IS_ERR(common->clk_gals1))
  480. return PTR_ERR(common->clk_gals1);
  481. /*
  482. * for mtk smi gen 1, we need to get the ao(always on) base to config
  483. * m4u port, and we need to enable the aync clock for transform the smi
  484. * clock into emi clock domain, but for mtk smi gen2, there's no smi ao
  485. * base.
  486. */
  487. if (common->plat->gen == MTK_SMI_GEN1) {
  488. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  489. common->smi_ao_base = devm_ioremap_resource(dev, res);
  490. if (IS_ERR(common->smi_ao_base))
  491. return PTR_ERR(common->smi_ao_base);
  492. common->clk_async = devm_clk_get(dev, "async");
  493. if (IS_ERR(common->clk_async))
  494. return PTR_ERR(common->clk_async);
  495. ret = clk_prepare_enable(common->clk_async);
  496. if (ret)
  497. return ret;
  498. } else {
  499. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  500. common->base = devm_ioremap_resource(dev, res);
  501. if (IS_ERR(common->base))
  502. return PTR_ERR(common->base);
  503. }
  504. pm_runtime_enable(dev);
  505. platform_set_drvdata(pdev, common);
  506. #ifdef CONFIG_MACH_MT8167
  507. /*
  508. * Without pm_runtime_get_sync(dev), the disp power domain
  509. * would be turn off after pm_runtime_enable, meanwhile disp
  510. * hw are still access register, this would cause system
  511. * abnormal.
  512. *
  513. * If we do not call pm_runtime_get_sync, then system would hang
  514. * in larb0's power domain attach, power domain SA and DE are
  515. * still checking that. We would like to bypass this first and
  516. * don't block the software flow.
  517. */
  518. pm_runtime_get_sync(dev);
  519. gmtk_common_dev = common;
  520. #endif
  521. if (common->plat->support_dcm) {
  522. struct device_node *mmsyscfg_node = NULL;
  523. mmsyscfg_node = of_parse_phandle(dev->of_node,
  524. "mediatek,mmsys", 0);
  525. if (!mmsyscfg_node)
  526. return -ENODEV;
  527. common->mmsyscfg_regmap = syscon_node_to_regmap(mmsyscfg_node);
  528. if (IS_ERR(common->mmsyscfg_regmap))
  529. return PTR_ERR(common->mmsyscfg_regmap);
  530. of_node_put(mmsyscfg_node);
  531. }
  532. return 0;
  533. }
  534. static int mtk_smi_common_remove(struct platform_device *pdev)
  535. {
  536. pm_runtime_disable(&pdev->dev);
  537. return 0;
  538. }
  539. static int __maybe_unused mtk_smi_common_resume(struct device *dev)
  540. {
  541. struct mtk_smi *common = dev_get_drvdata(dev);
  542. unsigned int bus_sel = common->plat->bus_sel;
  543. int ret;
  544. ret = mtk_smi_clk_enable(common);
  545. if (ret)
  546. return ret;
  547. if (common->plat->gen == MTK_SMI_GEN2 && bus_sel)
  548. writel(bus_sel, common->base + SMI_BUS_SEL);
  549. if (common->plat->support_dcm)
  550. common->plat->smi_comm_dcm_ctrl(common->mmsyscfg_regmap, true);
  551. return 0;
  552. }
  553. static int __maybe_unused mtk_smi_common_suspend(struct device *dev)
  554. {
  555. struct mtk_smi *common = dev_get_drvdata(dev);
  556. mtk_smi_clk_disable(common);
  557. return 0;
  558. }
  559. static const struct dev_pm_ops smi_common_pm_ops = {
  560. SET_RUNTIME_PM_OPS(mtk_smi_common_suspend, mtk_smi_common_resume, NULL)
  561. };
  562. static struct platform_driver mtk_smi_common_driver = {
  563. .probe = mtk_smi_common_probe,
  564. .remove = mtk_smi_common_remove,
  565. .driver = {
  566. .name = "mtk-smi-common",
  567. .of_match_table = mtk_smi_common_of_ids,
  568. .pm = &smi_common_pm_ops,
  569. }
  570. };
  571. #ifdef CONFIG_MACH_MT8167
  572. /* put the disp power domain that we got in smi probe */
  573. static int __init mtk_smi_init_late(void)
  574. {
  575. pm_runtime_put_sync(gmtk_common_dev->dev);
  576. return 0;
  577. }
  578. #endif
  579. #else /* IS_ENABLED(CONFIG_MTK_SMI_EXT) */
  580. #include <linux/of_address.h>
  581. static u32 nr_larbs, nr_dev;
  582. static struct mtk_smi_dev **smi_dev;
  583. s32 mtk_smi_clk_enable(struct mtk_smi_dev *smi)
  584. {
  585. s32 i, j, ret = 0;
  586. if (!smi) {
  587. pr_info("No such device or address\n");
  588. return -ENXIO;
  589. } else if (!smi->dev || !smi->clks) {
  590. pr_info("SMI%u no such device or address\n", smi->id);
  591. return -ENXIO;
  592. }
  593. for (i = 1; i < smi->nr_clks; i++) { /* without MTCMOS */
  594. ret = clk_prepare_enable(smi->clks[i]);
  595. if (ret) {
  596. dev_info(smi->dev, "SMI%u CLK%d enable failed:%d\n",
  597. smi->id, i, ret);
  598. for (j = i - 1; j > 0; j--)
  599. clk_disable_unprepare(smi->clks[j]);
  600. return ret;
  601. }
  602. }
  603. atomic_inc(&(smi->clk_cnts));
  604. return ret;
  605. }
  606. EXPORT_SYMBOL_GPL(mtk_smi_clk_enable);
  607. void mtk_smi_clk_disable(struct mtk_smi_dev *smi)
  608. {
  609. s32 i;
  610. if (!smi)
  611. pr_info("No such device or address\n");
  612. else if (!smi->dev || !smi->clks)
  613. pr_info("SMI%u no such device or address\n", smi->id);
  614. else {
  615. atomic_dec(&(smi->clk_cnts));
  616. for (i = smi->nr_clks - 1; i > 0; i--)
  617. clk_disable_unprepare(smi->clks[i]);
  618. }
  619. }
  620. EXPORT_SYMBOL_GPL(mtk_smi_clk_disable);
  621. struct mtk_smi_dev *mtk_smi_dev_get(const u32 id)
  622. {
  623. if (id > nr_dev)
  624. pr_info("Invalid id: %u, nr_dev=%u\n", id, nr_dev);
  625. else if (!smi_dev[id])
  626. pr_info("SMI%u no such device or address\n", id);
  627. else
  628. return smi_dev[id];
  629. return NULL;
  630. }
  631. EXPORT_SYMBOL_GPL(mtk_smi_dev_get);
  632. s32 mtk_smi_conf_set(const struct mtk_smi_dev *smi, const u32 scen_id)
  633. {
  634. u32 cnts, i;
  635. if (!smi) {
  636. pr_info("No such device or address\n");
  637. return -ENXIO;
  638. } else if (!smi->dev) {
  639. pr_info("SMI%u no such device or address\n", smi->id);
  640. return -ENXIO;
  641. }
  642. cnts = atomic_read(&(smi->clk_cnts));
  643. if (cnts <= 0) {
  644. dev_dbg(smi->dev, "SMI%u without MTCMOS: %d\n", smi->id, cnts);
  645. return cnts;
  646. }
  647. for (i = 0; i < smi->nr_conf_pairs; i++) /* conf */
  648. writel(smi->conf_pairs[i].val,
  649. smi->base + smi->conf_pairs[i].off);
  650. for (i = 0; i < smi->nr_scen_pairs; i++) /* scen */
  651. writel(smi->scen_pairs[scen_id][i].val,
  652. smi->base + smi->scen_pairs[scen_id][i].off);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(mtk_smi_conf_set);
  656. static s32 mtk_smi_clks_get(struct mtk_smi_dev *smi)
  657. {
  658. struct property *prop;
  659. const char *name, *clk_names = "clock-names";
  660. s32 i = 0, ret;
  661. if (!smi) {
  662. pr_info("No such device or address\n");
  663. return -ENXIO;
  664. } else if (!smi->dev) {
  665. pr_info("SMI%u no such device or address\n", smi->id);
  666. return -ENXIO;
  667. }
  668. ret = of_property_count_strings(smi->dev->of_node, clk_names);
  669. if (ret < 0)
  670. return ret;
  671. smi->nr_clks = (u32)ret;
  672. smi->clks = devm_kcalloc(smi->dev, smi->nr_clks, sizeof(*smi->clks),
  673. GFP_KERNEL);
  674. if (!smi->clks)
  675. return -ENOMEM;
  676. of_property_for_each_string(smi->dev->of_node, clk_names, prop, name) {
  677. smi->clks[i] = devm_clk_get(smi->dev, name);
  678. if (IS_ERR(smi->clks[i])) {
  679. dev_info(smi->dev, "SMI%u CLK%d:%s get failed\n",
  680. smi->id, i, name);
  681. break;
  682. }
  683. dev_info(smi->dev, "SMI%u CLK%d:%s\n", smi->id, i, name);
  684. i += 1;
  685. }
  686. if (i < smi->nr_clks)
  687. return PTR_ERR(smi->clks[i]);
  688. atomic_set(&(smi->clk_cnts), 0);
  689. return 0;
  690. }
  691. static int mtk_smi_dev_probe(struct platform_device *pdev, const u32 id)
  692. {
  693. struct resource *res;
  694. void __iomem *base;
  695. if (id > nr_dev) {
  696. dev_dbg(&pdev->dev,
  697. "Invalid id:%u, nr_dev=%u\n", id, nr_dev);
  698. return -EINVAL;
  699. }
  700. smi_dev[id] =
  701. devm_kzalloc(&pdev->dev, sizeof(*smi_dev[id]), GFP_KERNEL);
  702. if (!smi_dev[id])
  703. return -ENOMEM;
  704. smi_dev[id]->id = id;
  705. smi_dev[id]->dev = &pdev->dev;
  706. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  707. base = devm_ioremap_resource(smi_dev[id]->dev, res);
  708. if (IS_ERR(base)) {
  709. dev_info(&pdev->dev, "SMI%u base:%p read failed\n",
  710. id, base);
  711. return PTR_ERR(base);
  712. }
  713. smi_dev[id]->base = base;
  714. if (of_address_to_resource(smi_dev[id]->dev->of_node, 0, res))
  715. return -EINVAL;
  716. dev_info(&pdev->dev,
  717. "SMI%u base: VA=%p, PA=%pa\n", id, base, &res->start);
  718. platform_set_drvdata(pdev, smi_dev[id]);
  719. return mtk_smi_clks_get(smi_dev[id]);
  720. }
  721. static int mtk_smi_larb_probe(struct platform_device *pdev)
  722. {
  723. u32 id = 0;
  724. s32 ret;
  725. if (!pdev) {
  726. pr_notice("platform_device missed\n");
  727. return -ENODEV;
  728. }
  729. ret = of_property_read_u32(pdev->dev.of_node, "mediatek,smi-id", &id);
  730. if (ret) {
  731. dev_info(&pdev->dev, "LARB read failed:%d\n", ret);
  732. return ret;
  733. }
  734. return mtk_smi_dev_probe(pdev, id);
  735. }
  736. static int mtk_smi_common_probe(struct platform_device *pdev)
  737. {
  738. u32 id = 0, cnt = 0;
  739. s32 ret;
  740. if (!pdev) {
  741. pr_notice("platform_device missed\n");
  742. return -ENODEV;
  743. }
  744. ret = of_property_read_u32(pdev->dev.of_node, "mediatek,smi-id", &id);
  745. if (ret) {
  746. dev_info(&pdev->dev, "COMMON read failed:%d\n", ret);
  747. return ret;
  748. }
  749. nr_larbs = id;
  750. ret = of_property_read_u32(pdev->dev.of_node, "mediatek,smi-cnt", &cnt);
  751. if (ret)
  752. dev_dbg(&pdev->dev, "COMMON%u read all:%d failed:%d\n",
  753. nr_larbs, cnt, ret);
  754. if (!smi_dev) {
  755. nr_dev = cnt ? cnt : (id + 1);
  756. smi_dev = devm_kcalloc(
  757. &pdev->dev, nr_dev, sizeof(*smi_dev), GFP_KERNEL);
  758. dev_info(&pdev->dev, "COMMON%u nr_dev:%u\n", id, nr_dev);
  759. }
  760. if (!smi_dev)
  761. return -ENOMEM;
  762. return mtk_smi_dev_probe(pdev, id);
  763. }
  764. static const struct of_device_id mtk_smi_larb_of_ids[] = {
  765. {
  766. .compatible = "mediatek,smi_larb",
  767. },
  768. {}
  769. };
  770. static const struct of_device_id mtk_smi_common_of_ids[] = {
  771. {
  772. .compatible = "mediatek,smi_common",
  773. },
  774. {
  775. .compatible = "mediatek,smi_sub_common",
  776. },
  777. {}
  778. };
  779. static struct platform_driver mtk_smi_larb_driver = {
  780. .probe = mtk_smi_larb_probe,
  781. .driver = {
  782. .name = "mtk-smi-larb",
  783. .of_match_table = mtk_smi_larb_of_ids,
  784. }
  785. };
  786. static struct platform_driver mtk_smi_common_driver = {
  787. .probe = mtk_smi_common_probe,
  788. .driver = {
  789. .name = "mtk-smi-common",
  790. .of_match_table = mtk_smi_common_of_ids,
  791. }
  792. };
  793. #endif /* IS_ENABLED(CONFIG_MTK_SMI_EXT) */
  794. static int __init mtk_smi_init(void)
  795. {
  796. int ret;
  797. ret = platform_driver_register(&mtk_smi_common_driver);
  798. if (ret != 0) {
  799. pr_err("Failed to register SMI driver\n");
  800. return ret;
  801. }
  802. ret = platform_driver_register(&mtk_smi_larb_driver);
  803. if (ret != 0) {
  804. pr_err("Failed to register SMI-LARB driver\n");
  805. goto err_unreg_smi;
  806. }
  807. #if IS_ENABLED(CONFIG_MTK_SMI_EXT)
  808. ret = smi_register();
  809. if (ret) {
  810. pr_notice("Failed to register SMI_EXT driver\n");
  811. platform_driver_unregister(&mtk_smi_larb_driver);
  812. goto err_unreg_smi;
  813. }
  814. #endif
  815. return ret;
  816. err_unreg_smi:
  817. platform_driver_unregister(&mtk_smi_common_driver);
  818. return ret;
  819. }
  820. #if !IS_ENABLED(CONFIG_MTK_SMI_EXT)
  821. module_init(mtk_smi_init);
  822. #ifdef CONFIG_MACH_MT8167
  823. late_initcall(mtk_smi_init_late);
  824. #endif
  825. #else
  826. arch_initcall_sync(mtk_smi_init);
  827. #endif
  828. MODULE_DESCRIPTION("MediaTek SMI driver");
  829. MODULE_LICENSE("GPL v2");