blob: 691e4c344cf84c72be40ebd0d5ae53231fceb47b [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Yong Wucc8bbe12016-02-23 01:20:49 +08002/*
3 * Copyright (c) 2015-2016 MediaTek Inc.
4 * Author: Yong Wu <yong.wu@mediatek.com>
Yong Wucc8bbe12016-02-23 01:20:49 +08005 */
6#include <linux/clk.h>
7#include <linux/component.h>
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/io.h>
Yong Wu4f608d32017-08-21 19:00:21 +080011#include <linux/module.h>
Yong Wucc8bbe12016-02-23 01:20:49 +080012#include <linux/of.h>
13#include <linux/of_platform.h>
14#include <linux/platform_device.h>
15#include <linux/pm_runtime.h>
16#include <soc/mediatek/smi.h>
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080017#include <dt-bindings/memory/mt2701-larb-port.h>
Yong Wucc8bbe12016-02-23 01:20:49 +080018
Yong Wue6dec922017-08-21 19:00:16 +080019/* mt8173 */
Yong Wucc8bbe12016-02-23 01:20:49 +080020#define SMI_LARB_MMU_EN 0xf00
Yong Wue6dec922017-08-21 19:00:16 +080021
Fabien Parenta8529f32020-09-06 20:09:38 +020022/* mt8167 */
23#define MT8167_SMI_LARB_MMU_EN 0xfc0
24
Yong Wue6dec922017-08-21 19:00:16 +080025/* mt2701 */
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080026#define REG_SMI_SECUR_CON_BASE 0x5c0
27
28/* every register control 8 port, register offset 0x4 */
29#define REG_SMI_SECUR_CON_OFFSET(id) (((id) >> 3) << 2)
30#define REG_SMI_SECUR_CON_ADDR(id) \
31 (REG_SMI_SECUR_CON_BASE + REG_SMI_SECUR_CON_OFFSET(id))
32
33/*
34 * every port have 4 bit to control, bit[port + 3] control virtual or physical,
35 * bit[port + 2 : port + 1] control the domain, bit[port] control the security
36 * or non-security.
37 */
38#define SMI_SECUR_CON_VAL_MSK(id) (~(0xf << (((id) & 0x7) << 2)))
39#define SMI_SECUR_CON_VAL_VIRT(id) BIT((((id) & 0x7) << 2) + 3)
40/* mt2701 domain should be set to 3 */
41#define SMI_SECUR_CON_VAL_DOMAIN(id) (0x3 << ((((id) & 0x7) << 2) + 1))
42
Yong Wue6dec922017-08-21 19:00:16 +080043/* mt2712 */
44#define SMI_LARB_NONSEC_CON(id) (0x380 + ((id) * 4))
45#define F_MMU_EN BIT(0)
46
Yong Wu567e58c2019-08-24 11:02:05 +080047/* SMI COMMON */
48#define SMI_BUS_SEL 0x220
49#define SMI_BUS_LARB_SHIFT(larbid) ((larbid) << 1)
50/* All are MMU0 defaultly. Only specialize mmu1 here. */
51#define F_MMU1_LARB(larbid) (0x1 << SMI_BUS_LARB_SHIFT(larbid))
52
Yong Wu42d42c72019-08-24 11:01:49 +080053enum mtk_smi_gen {
54 MTK_SMI_GEN1,
55 MTK_SMI_GEN2
56};
57
58struct mtk_smi_common_plat {
59 enum mtk_smi_gen gen;
Yong Wu64fea742019-08-24 11:02:01 +080060 bool has_gals;
Yong Wu567e58c2019-08-24 11:02:05 +080061 u32 bus_sel; /* Balance some larbs to enter mmu0 or mmu1 */
Yong Wu42d42c72019-08-24 11:01:49 +080062};
63
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080064struct mtk_smi_larb_gen {
65 int port_in_larb[MTK_LARB_NR_MAX + 1];
Krzysztof Kozlowski3aa5a6c2020-07-24 09:40:28 +020066 void (*config_port)(struct device *dev);
Yong Wu2e9b0902019-08-24 11:01:48 +080067 unsigned int larb_direct_to_common_mask;
Yong Wu64fea742019-08-24 11:02:01 +080068 bool has_gals;
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080069};
Yong Wucc8bbe12016-02-23 01:20:49 +080070
71struct mtk_smi {
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080072 struct device *dev;
73 struct clk *clk_apb, *clk_smi;
Yong Wu64fea742019-08-24 11:02:01 +080074 struct clk *clk_gals0, *clk_gals1;
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080075 struct clk *clk_async; /*only needed by mt2701*/
Yong Wu567e58c2019-08-24 11:02:05 +080076 union {
77 void __iomem *smi_ao_base; /* only for gen1 */
78 void __iomem *base; /* only for gen2 */
79 };
Yong Wu42d42c72019-08-24 11:01:49 +080080 const struct mtk_smi_common_plat *plat;
Yong Wucc8bbe12016-02-23 01:20:49 +080081};
82
83struct mtk_smi_larb { /* larb: local arbiter */
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +080084 struct mtk_smi smi;
85 void __iomem *base;
86 struct device *smi_common_dev;
87 const struct mtk_smi_larb_gen *larb_gen;
88 int larbid;
89 u32 *mmu;
90};
91
Yong Wu4f0a1a12019-08-24 11:02:04 +080092static int mtk_smi_clk_enable(const struct mtk_smi *smi)
Yong Wucc8bbe12016-02-23 01:20:49 +080093{
94 int ret;
95
Yong Wucc8bbe12016-02-23 01:20:49 +080096 ret = clk_prepare_enable(smi->clk_apb);
97 if (ret)
Yong Wu4f0a1a12019-08-24 11:02:04 +080098 return ret;
Yong Wucc8bbe12016-02-23 01:20:49 +080099
100 ret = clk_prepare_enable(smi->clk_smi);
101 if (ret)
102 goto err_disable_apb;
103
Yong Wu64fea742019-08-24 11:02:01 +0800104 ret = clk_prepare_enable(smi->clk_gals0);
105 if (ret)
106 goto err_disable_smi;
107
108 ret = clk_prepare_enable(smi->clk_gals1);
109 if (ret)
110 goto err_disable_gals0;
111
Yong Wucc8bbe12016-02-23 01:20:49 +0800112 return 0;
113
Yong Wu64fea742019-08-24 11:02:01 +0800114err_disable_gals0:
115 clk_disable_unprepare(smi->clk_gals0);
116err_disable_smi:
117 clk_disable_unprepare(smi->clk_smi);
Yong Wucc8bbe12016-02-23 01:20:49 +0800118err_disable_apb:
119 clk_disable_unprepare(smi->clk_apb);
Yong Wucc8bbe12016-02-23 01:20:49 +0800120 return ret;
121}
122
Yong Wu4f0a1a12019-08-24 11:02:04 +0800123static void mtk_smi_clk_disable(const struct mtk_smi *smi)
Yong Wucc8bbe12016-02-23 01:20:49 +0800124{
Yong Wu64fea742019-08-24 11:02:01 +0800125 clk_disable_unprepare(smi->clk_gals1);
126 clk_disable_unprepare(smi->clk_gals0);
Yong Wucc8bbe12016-02-23 01:20:49 +0800127 clk_disable_unprepare(smi->clk_smi);
128 clk_disable_unprepare(smi->clk_apb);
Yong Wucc8bbe12016-02-23 01:20:49 +0800129}
130
131int mtk_smi_larb_get(struct device *larbdev)
132{
Yong Wu4f0a1a12019-08-24 11:02:04 +0800133 int ret = pm_runtime_get_sync(larbdev);
Yong Wucc8bbe12016-02-23 01:20:49 +0800134
Yong Wu4f0a1a12019-08-24 11:02:04 +0800135 return (ret < 0) ? ret : 0;
Yong Wucc8bbe12016-02-23 01:20:49 +0800136}
Philipp Zabelcb1b5df2016-04-27 10:48:00 +0200137EXPORT_SYMBOL_GPL(mtk_smi_larb_get);
Yong Wucc8bbe12016-02-23 01:20:49 +0800138
139void mtk_smi_larb_put(struct device *larbdev)
140{
Yong Wu4f0a1a12019-08-24 11:02:04 +0800141 pm_runtime_put_sync(larbdev);
Yong Wucc8bbe12016-02-23 01:20:49 +0800142}
Philipp Zabelcb1b5df2016-04-27 10:48:00 +0200143EXPORT_SYMBOL_GPL(mtk_smi_larb_put);
Yong Wucc8bbe12016-02-23 01:20:49 +0800144
145static int
146mtk_smi_larb_bind(struct device *dev, struct device *master, void *data)
147{
148 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
Yong Wu1ee9feb2019-08-24 11:02:08 +0800149 struct mtk_smi_larb_iommu *larb_mmu = data;
Yong Wucc8bbe12016-02-23 01:20:49 +0800150 unsigned int i;
151
Yong Wuec2da072019-08-24 11:02:07 +0800152 for (i = 0; i < MTK_LARB_NR_MAX; i++) {
Yong Wu1ee9feb2019-08-24 11:02:08 +0800153 if (dev == larb_mmu[i].dev) {
Yong Wuec2da072019-08-24 11:02:07 +0800154 larb->larbid = i;
Yong Wu1ee9feb2019-08-24 11:02:08 +0800155 larb->mmu = &larb_mmu[i].mmu;
Yong Wucc8bbe12016-02-23 01:20:49 +0800156 return 0;
157 }
158 }
159 return -ENODEV;
160}
161
Yong Wu2e9b0902019-08-24 11:01:48 +0800162static void mtk_smi_larb_config_port_gen2_general(struct device *dev)
Yong Wue6dec922017-08-21 19:00:16 +0800163{
164 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
165 u32 reg;
166 int i;
167
Yong Wu2e9b0902019-08-24 11:01:48 +0800168 if (BIT(larb->larbid) & larb->larb_gen->larb_direct_to_common_mask)
Yong Wue6dec922017-08-21 19:00:16 +0800169 return;
170
171 for_each_set_bit(i, (unsigned long *)larb->mmu, 32) {
172 reg = readl_relaxed(larb->base + SMI_LARB_NONSEC_CON(i));
173 reg |= F_MMU_EN;
174 writel(reg, larb->base + SMI_LARB_NONSEC_CON(i));
175 }
176}
177
178static void mtk_smi_larb_config_port_mt8173(struct device *dev)
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800179{
180 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
181
182 writel(*larb->mmu, larb->base + SMI_LARB_MMU_EN);
183}
184
Fabien Parenta8529f32020-09-06 20:09:38 +0200185static void mtk_smi_larb_config_port_mt8167(struct device *dev)
186{
187 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
188
189 writel(*larb->mmu, larb->base + MT8167_SMI_LARB_MMU_EN);
190}
191
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800192static void mtk_smi_larb_config_port_gen1(struct device *dev)
193{
194 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
195 const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
196 struct mtk_smi *common = dev_get_drvdata(larb->smi_common_dev);
197 int i, m4u_port_id, larb_port_num;
198 u32 sec_con_val, reg_val;
199
200 m4u_port_id = larb_gen->port_in_larb[larb->larbid];
201 larb_port_num = larb_gen->port_in_larb[larb->larbid + 1]
202 - larb_gen->port_in_larb[larb->larbid];
203
204 for (i = 0; i < larb_port_num; i++, m4u_port_id++) {
205 if (*larb->mmu & BIT(i)) {
206 /* bit[port + 3] controls the virtual or physical */
207 sec_con_val = SMI_SECUR_CON_VAL_VIRT(m4u_port_id);
208 } else {
209 /* do not need to enable m4u for this port */
210 continue;
211 }
212 reg_val = readl(common->smi_ao_base
213 + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
214 reg_val &= SMI_SECUR_CON_VAL_MSK(m4u_port_id);
215 reg_val |= sec_con_val;
216 reg_val |= SMI_SECUR_CON_VAL_DOMAIN(m4u_port_id);
217 writel(reg_val,
218 common->smi_ao_base
219 + REG_SMI_SECUR_CON_ADDR(m4u_port_id));
220 }
221}
222
Yong Wucc8bbe12016-02-23 01:20:49 +0800223static void
224mtk_smi_larb_unbind(struct device *dev, struct device *master, void *data)
225{
226 /* Do nothing as the iommu is always enabled. */
227}
228
229static const struct component_ops mtk_smi_larb_component_ops = {
230 .bind = mtk_smi_larb_bind,
231 .unbind = mtk_smi_larb_unbind,
232};
233
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800234static const struct mtk_smi_larb_gen mtk_smi_larb_mt8173 = {
235 /* mt8173 do not need the port in larb */
Yong Wue6dec922017-08-21 19:00:16 +0800236 .config_port = mtk_smi_larb_config_port_mt8173,
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800237};
238
Fabien Parenta8529f32020-09-06 20:09:38 +0200239static const struct mtk_smi_larb_gen mtk_smi_larb_mt8167 = {
240 /* mt8167 do not need the port in larb */
241 .config_port = mtk_smi_larb_config_port_mt8167,
242};
243
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800244static const struct mtk_smi_larb_gen mtk_smi_larb_mt2701 = {
245 .port_in_larb = {
246 LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
247 LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
248 },
249 .config_port = mtk_smi_larb_config_port_gen1,
250};
251
Yong Wue6dec922017-08-21 19:00:16 +0800252static const struct mtk_smi_larb_gen mtk_smi_larb_mt2712 = {
Yong Wu2e9b0902019-08-24 11:01:48 +0800253 .config_port = mtk_smi_larb_config_port_gen2_general,
254 .larb_direct_to_common_mask = BIT(8) | BIT(9), /* bdpsys */
Yong Wue6dec922017-08-21 19:00:16 +0800255};
256
Ming-Fan Chenfc492f32020-01-08 14:41:30 +0800257static const struct mtk_smi_larb_gen mtk_smi_larb_mt6779 = {
258 .config_port = mtk_smi_larb_config_port_gen2_general,
259 .larb_direct_to_common_mask =
260 BIT(4) | BIT(6) | BIT(11) | BIT(12) | BIT(13),
261 /* DUMMY | IPU0 | IPU1 | CCU | MDLA */
262};
263
Yong Wu907ba6a2019-08-24 11:02:02 +0800264static const struct mtk_smi_larb_gen mtk_smi_larb_mt8183 = {
265 .has_gals = true,
266 .config_port = mtk_smi_larb_config_port_gen2_general,
267 .larb_direct_to_common_mask = BIT(2) | BIT(3) | BIT(7),
268 /* IPU0 | IPU1 | CCU */
269};
270
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800271static const struct of_device_id mtk_smi_larb_of_ids[] = {
272 {
Fabien Parenta8529f32020-09-06 20:09:38 +0200273 .compatible = "mediatek,mt8167-smi-larb",
274 .data = &mtk_smi_larb_mt8167
275 },
276 {
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800277 .compatible = "mediatek,mt8173-smi-larb",
278 .data = &mtk_smi_larb_mt8173
279 },
280 {
281 .compatible = "mediatek,mt2701-smi-larb",
282 .data = &mtk_smi_larb_mt2701
283 },
Yong Wue6dec922017-08-21 19:00:16 +0800284 {
285 .compatible = "mediatek,mt2712-smi-larb",
286 .data = &mtk_smi_larb_mt2712
287 },
Yong Wu907ba6a2019-08-24 11:02:02 +0800288 {
Ming-Fan Chenfc492f32020-01-08 14:41:30 +0800289 .compatible = "mediatek,mt6779-smi-larb",
290 .data = &mtk_smi_larb_mt6779
291 },
292 {
Yong Wu907ba6a2019-08-24 11:02:02 +0800293 .compatible = "mediatek,mt8183-smi-larb",
294 .data = &mtk_smi_larb_mt8183
295 },
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800296 {}
297};
298
Yong Wucc8bbe12016-02-23 01:20:49 +0800299static int mtk_smi_larb_probe(struct platform_device *pdev)
300{
301 struct mtk_smi_larb *larb;
302 struct resource *res;
303 struct device *dev = &pdev->dev;
304 struct device_node *smi_node;
305 struct platform_device *smi_pdev;
306
Yong Wucc8bbe12016-02-23 01:20:49 +0800307 larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL);
308 if (!larb)
309 return -ENOMEM;
310
Honghui Zhang75487862017-08-04 09:32:25 +0800311 larb->larb_gen = of_device_get_match_data(dev);
Yong Wucc8bbe12016-02-23 01:20:49 +0800312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 larb->base = devm_ioremap_resource(dev, res);
314 if (IS_ERR(larb->base))
315 return PTR_ERR(larb->base);
316
317 larb->smi.clk_apb = devm_clk_get(dev, "apb");
318 if (IS_ERR(larb->smi.clk_apb))
319 return PTR_ERR(larb->smi.clk_apb);
320
321 larb->smi.clk_smi = devm_clk_get(dev, "smi");
322 if (IS_ERR(larb->smi.clk_smi))
323 return PTR_ERR(larb->smi.clk_smi);
Yong Wu64fea742019-08-24 11:02:01 +0800324
325 if (larb->larb_gen->has_gals) {
326 /* The larbs may still haven't gals even if the SoC support.*/
327 larb->smi.clk_gals0 = devm_clk_get(dev, "gals");
328 if (PTR_ERR(larb->smi.clk_gals0) == -ENOENT)
329 larb->smi.clk_gals0 = NULL;
330 else if (IS_ERR(larb->smi.clk_gals0))
331 return PTR_ERR(larb->smi.clk_gals0);
332 }
Yong Wucc8bbe12016-02-23 01:20:49 +0800333 larb->smi.dev = dev;
334
335 smi_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0);
336 if (!smi_node)
337 return -EINVAL;
338
339 smi_pdev = of_find_device_by_node(smi_node);
340 of_node_put(smi_node);
341 if (smi_pdev) {
Yong Wu4f608d32017-08-21 19:00:21 +0800342 if (!platform_get_drvdata(smi_pdev))
343 return -EPROBE_DEFER;
Yong Wucc8bbe12016-02-23 01:20:49 +0800344 larb->smi_common_dev = &smi_pdev->dev;
345 } else {
346 dev_err(dev, "Failed to get the smi_common device\n");
347 return -EINVAL;
348 }
349
350 pm_runtime_enable(dev);
351 platform_set_drvdata(pdev, larb);
352 return component_add(dev, &mtk_smi_larb_component_ops);
353}
354
355static int mtk_smi_larb_remove(struct platform_device *pdev)
356{
357 pm_runtime_disable(&pdev->dev);
358 component_del(&pdev->dev, &mtk_smi_larb_component_ops);
359 return 0;
360}
361
Yong Wu4f0a1a12019-08-24 11:02:04 +0800362static int __maybe_unused mtk_smi_larb_resume(struct device *dev)
363{
364 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
365 const struct mtk_smi_larb_gen *larb_gen = larb->larb_gen;
366 int ret;
367
368 /* Power on smi-common. */
369 ret = pm_runtime_get_sync(larb->smi_common_dev);
370 if (ret < 0) {
371 dev_err(dev, "Failed to pm get for smi-common(%d).\n", ret);
372 return ret;
373 }
374
375 ret = mtk_smi_clk_enable(&larb->smi);
376 if (ret < 0) {
377 dev_err(dev, "Failed to enable clock(%d).\n", ret);
378 pm_runtime_put_sync(larb->smi_common_dev);
379 return ret;
380 }
381
382 /* Configure the basic setting for this larb */
383 larb_gen->config_port(dev);
384
385 return 0;
386}
387
388static int __maybe_unused mtk_smi_larb_suspend(struct device *dev)
389{
390 struct mtk_smi_larb *larb = dev_get_drvdata(dev);
391
392 mtk_smi_clk_disable(&larb->smi);
393 pm_runtime_put_sync(larb->smi_common_dev);
394 return 0;
395}
396
397static const struct dev_pm_ops smi_larb_pm_ops = {
398 SET_RUNTIME_PM_OPS(mtk_smi_larb_suspend, mtk_smi_larb_resume, NULL)
Yong Wufb030822019-10-09 19:59:33 +0800399 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
400 pm_runtime_force_resume)
Yong Wu4f0a1a12019-08-24 11:02:04 +0800401};
402
Yong Wucc8bbe12016-02-23 01:20:49 +0800403static struct platform_driver mtk_smi_larb_driver = {
404 .probe = mtk_smi_larb_probe,
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800405 .remove = mtk_smi_larb_remove,
Yong Wucc8bbe12016-02-23 01:20:49 +0800406 .driver = {
407 .name = "mtk-smi-larb",
408 .of_match_table = mtk_smi_larb_of_ids,
Yong Wu4f0a1a12019-08-24 11:02:04 +0800409 .pm = &smi_larb_pm_ops,
Yong Wucc8bbe12016-02-23 01:20:49 +0800410 }
411};
412
Yong Wu42d42c72019-08-24 11:01:49 +0800413static const struct mtk_smi_common_plat mtk_smi_common_gen1 = {
414 .gen = MTK_SMI_GEN1,
415};
416
417static const struct mtk_smi_common_plat mtk_smi_common_gen2 = {
418 .gen = MTK_SMI_GEN2,
419};
420
Ming-Fan Chenfc492f32020-01-08 14:41:30 +0800421static const struct mtk_smi_common_plat mtk_smi_common_mt6779 = {
422 .gen = MTK_SMI_GEN2,
423 .has_gals = true,
424 .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(4) |
425 F_MMU1_LARB(5) | F_MMU1_LARB(6) | F_MMU1_LARB(7),
426};
427
Yong Wu907ba6a2019-08-24 11:02:02 +0800428static const struct mtk_smi_common_plat mtk_smi_common_mt8183 = {
429 .gen = MTK_SMI_GEN2,
430 .has_gals = true,
Yong Wu567e58c2019-08-24 11:02:05 +0800431 .bus_sel = F_MMU1_LARB(1) | F_MMU1_LARB(2) | F_MMU1_LARB(5) |
432 F_MMU1_LARB(7),
Yong Wu907ba6a2019-08-24 11:02:02 +0800433};
434
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800435static const struct of_device_id mtk_smi_common_of_ids[] = {
436 {
437 .compatible = "mediatek,mt8173-smi-common",
Yong Wu42d42c72019-08-24 11:01:49 +0800438 .data = &mtk_smi_common_gen2,
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800439 },
440 {
Fabien Parenta8529f32020-09-06 20:09:38 +0200441 .compatible = "mediatek,mt8167-smi-common",
442 .data = &mtk_smi_common_gen2,
443 },
444 {
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800445 .compatible = "mediatek,mt2701-smi-common",
Yong Wu42d42c72019-08-24 11:01:49 +0800446 .data = &mtk_smi_common_gen1,
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800447 },
Yong Wue6dec922017-08-21 19:00:16 +0800448 {
449 .compatible = "mediatek,mt2712-smi-common",
Yong Wu42d42c72019-08-24 11:01:49 +0800450 .data = &mtk_smi_common_gen2,
Yong Wue6dec922017-08-21 19:00:16 +0800451 },
Yong Wu907ba6a2019-08-24 11:02:02 +0800452 {
Ming-Fan Chenfc492f32020-01-08 14:41:30 +0800453 .compatible = "mediatek,mt6779-smi-common",
454 .data = &mtk_smi_common_mt6779,
455 },
456 {
Yong Wu907ba6a2019-08-24 11:02:02 +0800457 .compatible = "mediatek,mt8183-smi-common",
458 .data = &mtk_smi_common_mt8183,
459 },
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800460 {}
461};
462
Yong Wucc8bbe12016-02-23 01:20:49 +0800463static int mtk_smi_common_probe(struct platform_device *pdev)
464{
465 struct device *dev = &pdev->dev;
466 struct mtk_smi *common;
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800467 struct resource *res;
Arvind Yadav46cc8152017-08-10 10:47:32 +0530468 int ret;
Yong Wucc8bbe12016-02-23 01:20:49 +0800469
Yong Wucc8bbe12016-02-23 01:20:49 +0800470 common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
471 if (!common)
472 return -ENOMEM;
473 common->dev = dev;
Yong Wu42d42c72019-08-24 11:01:49 +0800474 common->plat = of_device_get_match_data(dev);
Yong Wucc8bbe12016-02-23 01:20:49 +0800475
476 common->clk_apb = devm_clk_get(dev, "apb");
477 if (IS_ERR(common->clk_apb))
478 return PTR_ERR(common->clk_apb);
479
480 common->clk_smi = devm_clk_get(dev, "smi");
481 if (IS_ERR(common->clk_smi))
482 return PTR_ERR(common->clk_smi);
483
Yong Wu64fea742019-08-24 11:02:01 +0800484 if (common->plat->has_gals) {
485 common->clk_gals0 = devm_clk_get(dev, "gals0");
486 if (IS_ERR(common->clk_gals0))
487 return PTR_ERR(common->clk_gals0);
488
489 common->clk_gals1 = devm_clk_get(dev, "gals1");
490 if (IS_ERR(common->clk_gals1))
491 return PTR_ERR(common->clk_gals1);
492 }
493
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800494 /*
495 * for mtk smi gen 1, we need to get the ao(always on) base to config
496 * m4u port, and we need to enable the aync clock for transform the smi
497 * clock into emi clock domain, but for mtk smi gen2, there's no smi ao
498 * base.
499 */
Yong Wu42d42c72019-08-24 11:01:49 +0800500 if (common->plat->gen == MTK_SMI_GEN1) {
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800501 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 common->smi_ao_base = devm_ioremap_resource(dev, res);
503 if (IS_ERR(common->smi_ao_base))
504 return PTR_ERR(common->smi_ao_base);
505
506 common->clk_async = devm_clk_get(dev, "async");
507 if (IS_ERR(common->clk_async))
508 return PTR_ERR(common->clk_async);
509
Arvind Yadav46cc8152017-08-10 10:47:32 +0530510 ret = clk_prepare_enable(common->clk_async);
511 if (ret)
512 return ret;
Yong Wu567e58c2019-08-24 11:02:05 +0800513 } else {
514 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
515 common->base = devm_ioremap_resource(dev, res);
516 if (IS_ERR(common->base))
517 return PTR_ERR(common->base);
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800518 }
Yong Wucc8bbe12016-02-23 01:20:49 +0800519 pm_runtime_enable(dev);
520 platform_set_drvdata(pdev, common);
521 return 0;
522}
523
524static int mtk_smi_common_remove(struct platform_device *pdev)
525{
526 pm_runtime_disable(&pdev->dev);
527 return 0;
528}
529
Yong Wu4f0a1a12019-08-24 11:02:04 +0800530static int __maybe_unused mtk_smi_common_resume(struct device *dev)
531{
532 struct mtk_smi *common = dev_get_drvdata(dev);
Yong Wu567e58c2019-08-24 11:02:05 +0800533 u32 bus_sel = common->plat->bus_sel;
Yong Wu4f0a1a12019-08-24 11:02:04 +0800534 int ret;
535
536 ret = mtk_smi_clk_enable(common);
537 if (ret) {
538 dev_err(common->dev, "Failed to enable clock(%d).\n", ret);
539 return ret;
540 }
Yong Wu567e58c2019-08-24 11:02:05 +0800541
542 if (common->plat->gen == MTK_SMI_GEN2 && bus_sel)
543 writel(bus_sel, common->base + SMI_BUS_SEL);
Yong Wu4f0a1a12019-08-24 11:02:04 +0800544 return 0;
545}
546
547static int __maybe_unused mtk_smi_common_suspend(struct device *dev)
548{
549 struct mtk_smi *common = dev_get_drvdata(dev);
550
551 mtk_smi_clk_disable(common);
552 return 0;
553}
554
555static const struct dev_pm_ops smi_common_pm_ops = {
556 SET_RUNTIME_PM_OPS(mtk_smi_common_suspend, mtk_smi_common_resume, NULL)
Yong Wufb030822019-10-09 19:59:33 +0800557 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
558 pm_runtime_force_resume)
Yong Wu4f0a1a12019-08-24 11:02:04 +0800559};
560
Yong Wucc8bbe12016-02-23 01:20:49 +0800561static struct platform_driver mtk_smi_common_driver = {
562 .probe = mtk_smi_common_probe,
563 .remove = mtk_smi_common_remove,
564 .driver = {
565 .name = "mtk-smi-common",
566 .of_match_table = mtk_smi_common_of_ids,
Yong Wu4f0a1a12019-08-24 11:02:04 +0800567 .pm = &smi_common_pm_ops,
Yong Wucc8bbe12016-02-23 01:20:49 +0800568 }
569};
570
571static int __init mtk_smi_init(void)
572{
573 int ret;
574
575 ret = platform_driver_register(&mtk_smi_common_driver);
576 if (ret != 0) {
577 pr_err("Failed to register SMI driver\n");
578 return ret;
579 }
580
581 ret = platform_driver_register(&mtk_smi_larb_driver);
582 if (ret != 0) {
583 pr_err("Failed to register SMI-LARB driver\n");
584 goto err_unreg_smi;
585 }
586 return ret;
587
588err_unreg_smi:
589 platform_driver_unregister(&mtk_smi_common_driver);
590 return ret;
591}
Honghui Zhang3c8f4ad2016-06-08 17:50:59 +0800592
Yong Wu4f608d32017-08-21 19:00:21 +0800593module_init(mtk_smi_init);