mtk-scpsys.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2015 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/regmap.h>
  23. #include <linux/soc/mediatek/infracfg.h>
  24. #include <dt-bindings/power/mt8173-power.h>
  25. #define SPM_VDE_PWR_CON 0x0210
  26. #define SPM_MFG_PWR_CON 0x0214
  27. #define SPM_VEN_PWR_CON 0x0230
  28. #define SPM_ISP_PWR_CON 0x0238
  29. #define SPM_DIS_PWR_CON 0x023c
  30. #define SPM_VEN2_PWR_CON 0x0298
  31. #define SPM_AUDIO_PWR_CON 0x029c
  32. #define SPM_MFG_2D_PWR_CON 0x02c0
  33. #define SPM_MFG_ASYNC_PWR_CON 0x02c4
  34. #define SPM_USB_PWR_CON 0x02cc
  35. #define SPM_PWR_STATUS 0x060c
  36. #define SPM_PWR_STATUS_2ND 0x0610
  37. #define PWR_RST_B_BIT BIT(0)
  38. #define PWR_ISO_BIT BIT(1)
  39. #define PWR_ON_BIT BIT(2)
  40. #define PWR_ON_2ND_BIT BIT(3)
  41. #define PWR_CLK_DIS_BIT BIT(4)
  42. #define PWR_STATUS_DISP BIT(3)
  43. #define PWR_STATUS_MFG BIT(4)
  44. #define PWR_STATUS_ISP BIT(5)
  45. #define PWR_STATUS_VDEC BIT(7)
  46. #define PWR_STATUS_VENC_LT BIT(20)
  47. #define PWR_STATUS_VENC BIT(21)
  48. #define PWR_STATUS_MFG_2D BIT(22)
  49. #define PWR_STATUS_MFG_ASYNC BIT(23)
  50. #define PWR_STATUS_AUDIO BIT(24)
  51. #define PWR_STATUS_USB BIT(25)
  52. enum clk_id {
  53. MT8173_CLK_NONE,
  54. MT8173_CLK_MM,
  55. MT8173_CLK_MFG,
  56. MT8173_CLK_VENC,
  57. MT8173_CLK_VENC_LT,
  58. MT8173_CLK_MAX,
  59. };
  60. #define MAX_CLKS 2
  61. struct scp_domain_data {
  62. const char *name;
  63. u32 sta_mask;
  64. int ctl_offs;
  65. u32 sram_pdn_bits;
  66. u32 sram_pdn_ack_bits;
  67. u32 bus_prot_mask;
  68. enum clk_id clk_id[MAX_CLKS];
  69. bool active_wakeup;
  70. };
  71. static const struct scp_domain_data scp_domain_data[] __initconst = {
  72. [MT8173_POWER_DOMAIN_VDEC] = {
  73. .name = "vdec",
  74. .sta_mask = PWR_STATUS_VDEC,
  75. .ctl_offs = SPM_VDE_PWR_CON,
  76. .sram_pdn_bits = GENMASK(11, 8),
  77. .sram_pdn_ack_bits = GENMASK(12, 12),
  78. .clk_id = {MT8173_CLK_MM},
  79. },
  80. [MT8173_POWER_DOMAIN_VENC] = {
  81. .name = "venc",
  82. .sta_mask = PWR_STATUS_VENC,
  83. .ctl_offs = SPM_VEN_PWR_CON,
  84. .sram_pdn_bits = GENMASK(11, 8),
  85. .sram_pdn_ack_bits = GENMASK(15, 12),
  86. .clk_id = {MT8173_CLK_MM, MT8173_CLK_VENC},
  87. },
  88. [MT8173_POWER_DOMAIN_ISP] = {
  89. .name = "isp",
  90. .sta_mask = PWR_STATUS_ISP,
  91. .ctl_offs = SPM_ISP_PWR_CON,
  92. .sram_pdn_bits = GENMASK(11, 8),
  93. .sram_pdn_ack_bits = GENMASK(13, 12),
  94. .clk_id = {MT8173_CLK_MM},
  95. },
  96. [MT8173_POWER_DOMAIN_MM] = {
  97. .name = "mm",
  98. .sta_mask = PWR_STATUS_DISP,
  99. .ctl_offs = SPM_DIS_PWR_CON,
  100. .sram_pdn_bits = GENMASK(11, 8),
  101. .sram_pdn_ack_bits = GENMASK(12, 12),
  102. .clk_id = {MT8173_CLK_MM},
  103. .bus_prot_mask = MT8173_TOP_AXI_PROT_EN_MM_M0 |
  104. MT8173_TOP_AXI_PROT_EN_MM_M1,
  105. },
  106. [MT8173_POWER_DOMAIN_VENC_LT] = {
  107. .name = "venc_lt",
  108. .sta_mask = PWR_STATUS_VENC_LT,
  109. .ctl_offs = SPM_VEN2_PWR_CON,
  110. .sram_pdn_bits = GENMASK(11, 8),
  111. .sram_pdn_ack_bits = GENMASK(15, 12),
  112. .clk_id = {MT8173_CLK_MM, MT8173_CLK_VENC_LT},
  113. },
  114. [MT8173_POWER_DOMAIN_AUDIO] = {
  115. .name = "audio",
  116. .sta_mask = PWR_STATUS_AUDIO,
  117. .ctl_offs = SPM_AUDIO_PWR_CON,
  118. .sram_pdn_bits = GENMASK(11, 8),
  119. .sram_pdn_ack_bits = GENMASK(15, 12),
  120. .clk_id = {MT8173_CLK_NONE},
  121. },
  122. [MT8173_POWER_DOMAIN_USB] = {
  123. .name = "usb",
  124. .sta_mask = PWR_STATUS_USB,
  125. .ctl_offs = SPM_USB_PWR_CON,
  126. .sram_pdn_bits = GENMASK(11, 8),
  127. .sram_pdn_ack_bits = GENMASK(15, 12),
  128. .clk_id = {MT8173_CLK_NONE},
  129. .active_wakeup = true,
  130. },
  131. [MT8173_POWER_DOMAIN_MFG_ASYNC] = {
  132. .name = "mfg_async",
  133. .sta_mask = PWR_STATUS_MFG_ASYNC,
  134. .ctl_offs = SPM_MFG_ASYNC_PWR_CON,
  135. .sram_pdn_bits = GENMASK(11, 8),
  136. .sram_pdn_ack_bits = 0,
  137. .clk_id = {MT8173_CLK_MFG},
  138. },
  139. [MT8173_POWER_DOMAIN_MFG_2D] = {
  140. .name = "mfg_2d",
  141. .sta_mask = PWR_STATUS_MFG_2D,
  142. .ctl_offs = SPM_MFG_2D_PWR_CON,
  143. .sram_pdn_bits = GENMASK(11, 8),
  144. .sram_pdn_ack_bits = GENMASK(13, 12),
  145. .clk_id = {MT8173_CLK_NONE},
  146. },
  147. [MT8173_POWER_DOMAIN_MFG] = {
  148. .name = "mfg",
  149. .sta_mask = PWR_STATUS_MFG,
  150. .ctl_offs = SPM_MFG_PWR_CON,
  151. .sram_pdn_bits = GENMASK(13, 8),
  152. .sram_pdn_ack_bits = GENMASK(21, 16),
  153. .clk_id = {MT8173_CLK_NONE},
  154. .bus_prot_mask = MT8173_TOP_AXI_PROT_EN_MFG_S |
  155. MT8173_TOP_AXI_PROT_EN_MFG_M0 |
  156. MT8173_TOP_AXI_PROT_EN_MFG_M1 |
  157. MT8173_TOP_AXI_PROT_EN_MFG_SNOOP_OUT,
  158. },
  159. };
  160. #define NUM_DOMAINS ARRAY_SIZE(scp_domain_data)
  161. struct scp;
  162. struct scp_domain {
  163. struct generic_pm_domain genpd;
  164. struct scp *scp;
  165. struct clk *clk[MAX_CLKS];
  166. u32 sta_mask;
  167. void __iomem *ctl_addr;
  168. u32 sram_pdn_bits;
  169. u32 sram_pdn_ack_bits;
  170. u32 bus_prot_mask;
  171. bool active_wakeup;
  172. };
  173. struct scp {
  174. struct scp_domain domains[NUM_DOMAINS];
  175. struct genpd_onecell_data pd_data;
  176. struct device *dev;
  177. void __iomem *base;
  178. struct regmap *infracfg;
  179. };
  180. static int scpsys_domain_is_on(struct scp_domain *scpd)
  181. {
  182. struct scp *scp = scpd->scp;
  183. u32 status = readl(scp->base + SPM_PWR_STATUS) & scpd->sta_mask;
  184. u32 status2 = readl(scp->base + SPM_PWR_STATUS_2ND) & scpd->sta_mask;
  185. /*
  186. * A domain is on when both status bits are set. If only one is set
  187. * return an error. This happens while powering up a domain
  188. */
  189. if (status && status2)
  190. return true;
  191. if (!status && !status2)
  192. return false;
  193. return -EINVAL;
  194. }
  195. static int scpsys_power_on(struct generic_pm_domain *genpd)
  196. {
  197. struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
  198. struct scp *scp = scpd->scp;
  199. unsigned long timeout;
  200. bool expired;
  201. void __iomem *ctl_addr = scpd->ctl_addr;
  202. u32 sram_pdn_ack = scpd->sram_pdn_ack_bits;
  203. u32 val;
  204. int ret;
  205. int i;
  206. for (i = 0; i < MAX_CLKS && scpd->clk[i]; i++) {
  207. ret = clk_prepare_enable(scpd->clk[i]);
  208. if (ret) {
  209. for (--i; i >= 0; i--)
  210. clk_disable_unprepare(scpd->clk[i]);
  211. goto err_clk;
  212. }
  213. }
  214. val = readl(ctl_addr);
  215. val |= PWR_ON_BIT;
  216. writel(val, ctl_addr);
  217. val |= PWR_ON_2ND_BIT;
  218. writel(val, ctl_addr);
  219. /* wait until PWR_ACK = 1 */
  220. timeout = jiffies + HZ;
  221. expired = false;
  222. while (1) {
  223. ret = scpsys_domain_is_on(scpd);
  224. if (ret > 0)
  225. break;
  226. if (expired) {
  227. ret = -ETIMEDOUT;
  228. goto err_pwr_ack;
  229. }
  230. cpu_relax();
  231. if (time_after(jiffies, timeout))
  232. expired = true;
  233. }
  234. val &= ~PWR_CLK_DIS_BIT;
  235. writel(val, ctl_addr);
  236. val &= ~PWR_ISO_BIT;
  237. writel(val, ctl_addr);
  238. val |= PWR_RST_B_BIT;
  239. writel(val, ctl_addr);
  240. val &= ~scpd->sram_pdn_bits;
  241. writel(val, ctl_addr);
  242. /* wait until SRAM_PDN_ACK all 0 */
  243. timeout = jiffies + HZ;
  244. expired = false;
  245. while (sram_pdn_ack && (readl(ctl_addr) & sram_pdn_ack)) {
  246. if (expired) {
  247. ret = -ETIMEDOUT;
  248. goto err_pwr_ack;
  249. }
  250. cpu_relax();
  251. if (time_after(jiffies, timeout))
  252. expired = true;
  253. }
  254. if (scpd->bus_prot_mask) {
  255. ret = mtk_infracfg_clear_bus_protection(scp->infracfg,
  256. scpd->bus_prot_mask);
  257. if (ret)
  258. goto err_pwr_ack;
  259. }
  260. return 0;
  261. err_pwr_ack:
  262. for (i = MAX_CLKS - 1; i >= 0; i--) {
  263. if (scpd->clk[i])
  264. clk_disable_unprepare(scpd->clk[i]);
  265. }
  266. err_clk:
  267. dev_err(scp->dev, "Failed to power on domain %s\n", genpd->name);
  268. return ret;
  269. }
  270. static int scpsys_power_off(struct generic_pm_domain *genpd)
  271. {
  272. struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
  273. struct scp *scp = scpd->scp;
  274. unsigned long timeout;
  275. bool expired;
  276. void __iomem *ctl_addr = scpd->ctl_addr;
  277. u32 pdn_ack = scpd->sram_pdn_ack_bits;
  278. u32 val;
  279. int ret;
  280. int i;
  281. if (scpd->bus_prot_mask) {
  282. ret = mtk_infracfg_set_bus_protection(scp->infracfg,
  283. scpd->bus_prot_mask);
  284. if (ret)
  285. goto out;
  286. }
  287. val = readl(ctl_addr);
  288. val |= scpd->sram_pdn_bits;
  289. writel(val, ctl_addr);
  290. /* wait until SRAM_PDN_ACK all 1 */
  291. timeout = jiffies + HZ;
  292. expired = false;
  293. while (pdn_ack && (readl(ctl_addr) & pdn_ack) != pdn_ack) {
  294. if (expired) {
  295. ret = -ETIMEDOUT;
  296. goto out;
  297. }
  298. cpu_relax();
  299. if (time_after(jiffies, timeout))
  300. expired = true;
  301. }
  302. val |= PWR_ISO_BIT;
  303. writel(val, ctl_addr);
  304. val &= ~PWR_RST_B_BIT;
  305. writel(val, ctl_addr);
  306. val |= PWR_CLK_DIS_BIT;
  307. writel(val, ctl_addr);
  308. val &= ~PWR_ON_BIT;
  309. writel(val, ctl_addr);
  310. val &= ~PWR_ON_2ND_BIT;
  311. writel(val, ctl_addr);
  312. /* wait until PWR_ACK = 0 */
  313. timeout = jiffies + HZ;
  314. expired = false;
  315. while (1) {
  316. ret = scpsys_domain_is_on(scpd);
  317. if (ret == 0)
  318. break;
  319. if (expired) {
  320. ret = -ETIMEDOUT;
  321. goto out;
  322. }
  323. cpu_relax();
  324. if (time_after(jiffies, timeout))
  325. expired = true;
  326. }
  327. for (i = 0; i < MAX_CLKS && scpd->clk[i]; i++)
  328. clk_disable_unprepare(scpd->clk[i]);
  329. return 0;
  330. out:
  331. dev_err(scp->dev, "Failed to power off domain %s\n", genpd->name);
  332. return ret;
  333. }
  334. static bool scpsys_active_wakeup(struct device *dev)
  335. {
  336. struct generic_pm_domain *genpd;
  337. struct scp_domain *scpd;
  338. genpd = pd_to_genpd(dev->pm_domain);
  339. scpd = container_of(genpd, struct scp_domain, genpd);
  340. return scpd->active_wakeup;
  341. }
  342. static int __init scpsys_probe(struct platform_device *pdev)
  343. {
  344. struct genpd_onecell_data *pd_data;
  345. struct resource *res;
  346. int i, j, ret;
  347. struct scp *scp;
  348. struct clk *clk[MT8173_CLK_MAX];
  349. scp = devm_kzalloc(&pdev->dev, sizeof(*scp), GFP_KERNEL);
  350. if (!scp)
  351. return -ENOMEM;
  352. scp->dev = &pdev->dev;
  353. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  354. scp->base = devm_ioremap_resource(&pdev->dev, res);
  355. if (IS_ERR(scp->base))
  356. return PTR_ERR(scp->base);
  357. pd_data = &scp->pd_data;
  358. pd_data->domains = devm_kzalloc(&pdev->dev,
  359. sizeof(*pd_data->domains) * NUM_DOMAINS, GFP_KERNEL);
  360. if (!pd_data->domains)
  361. return -ENOMEM;
  362. clk[MT8173_CLK_MM] = devm_clk_get(&pdev->dev, "mm");
  363. if (IS_ERR(clk[MT8173_CLK_MM]))
  364. return PTR_ERR(clk[MT8173_CLK_MM]);
  365. clk[MT8173_CLK_MFG] = devm_clk_get(&pdev->dev, "mfg");
  366. if (IS_ERR(clk[MT8173_CLK_MFG]))
  367. return PTR_ERR(clk[MT8173_CLK_MFG]);
  368. clk[MT8173_CLK_VENC] = devm_clk_get(&pdev->dev, "venc");
  369. if (IS_ERR(clk[MT8173_CLK_VENC]))
  370. return PTR_ERR(clk[MT8173_CLK_VENC]);
  371. clk[MT8173_CLK_VENC_LT] = devm_clk_get(&pdev->dev, "venc_lt");
  372. if (IS_ERR(clk[MT8173_CLK_VENC_LT]))
  373. return PTR_ERR(clk[MT8173_CLK_VENC_LT]);
  374. scp->infracfg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  375. "infracfg");
  376. if (IS_ERR(scp->infracfg)) {
  377. dev_err(&pdev->dev, "Cannot find infracfg controller: %ld\n",
  378. PTR_ERR(scp->infracfg));
  379. return PTR_ERR(scp->infracfg);
  380. }
  381. pd_data->num_domains = NUM_DOMAINS;
  382. for (i = 0; i < NUM_DOMAINS; i++) {
  383. struct scp_domain *scpd = &scp->domains[i];
  384. struct generic_pm_domain *genpd = &scpd->genpd;
  385. const struct scp_domain_data *data = &scp_domain_data[i];
  386. pd_data->domains[i] = genpd;
  387. scpd->scp = scp;
  388. scpd->sta_mask = data->sta_mask;
  389. scpd->ctl_addr = scp->base + data->ctl_offs;
  390. scpd->sram_pdn_bits = data->sram_pdn_bits;
  391. scpd->sram_pdn_ack_bits = data->sram_pdn_ack_bits;
  392. scpd->bus_prot_mask = data->bus_prot_mask;
  393. scpd->active_wakeup = data->active_wakeup;
  394. for (j = 0; j < MAX_CLKS && data->clk_id[j]; j++)
  395. scpd->clk[j] = clk[data->clk_id[j]];
  396. genpd->name = data->name;
  397. genpd->power_off = scpsys_power_off;
  398. genpd->power_on = scpsys_power_on;
  399. genpd->dev_ops.active_wakeup = scpsys_active_wakeup;
  400. /*
  401. * Initially turn on all domains to make the domains usable
  402. * with !CONFIG_PM and to get the hardware in sync with the
  403. * software. The unused domains will be switched off during
  404. * late_init time.
  405. */
  406. genpd->power_on(genpd);
  407. pm_genpd_init(genpd, NULL, false);
  408. }
  409. /*
  410. * We are not allowed to fail here since there is no way to unregister
  411. * a power domain. Once registered above we have to keep the domains
  412. * valid.
  413. */
  414. ret = pm_genpd_add_subdomain(pd_data->domains[MT8173_POWER_DOMAIN_MFG_ASYNC],
  415. pd_data->domains[MT8173_POWER_DOMAIN_MFG_2D]);
  416. if (ret && IS_ENABLED(CONFIG_PM))
  417. dev_err(&pdev->dev, "Failed to add subdomain: %d\n", ret);
  418. ret = pm_genpd_add_subdomain(pd_data->domains[MT8173_POWER_DOMAIN_MFG_2D],
  419. pd_data->domains[MT8173_POWER_DOMAIN_MFG]);
  420. if (ret && IS_ENABLED(CONFIG_PM))
  421. dev_err(&pdev->dev, "Failed to add subdomain: %d\n", ret);
  422. ret = of_genpd_add_provider_onecell(pdev->dev.of_node, pd_data);
  423. if (ret)
  424. dev_err(&pdev->dev, "Failed to add OF provider: %d\n", ret);
  425. return 0;
  426. }
  427. static const struct of_device_id of_scpsys_match_tbl[] = {
  428. {
  429. .compatible = "mediatek,mt8173-scpsys",
  430. }, {
  431. /* sentinel */
  432. }
  433. };
  434. static struct platform_driver scpsys_drv = {
  435. .driver = {
  436. .name = "mtk-scpsys",
  437. .owner = THIS_MODULE,
  438. .of_match_table = of_match_ptr(of_scpsys_match_tbl),
  439. },
  440. };
  441. module_platform_driver_probe(scpsys_drv, scpsys_probe);