mtk-scpsys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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_MM,
  54. MT8173_CLK_MFG,
  55. MT8173_CLK_NONE,
  56. MT8173_CLK_MAX = MT8173_CLK_NONE,
  57. };
  58. struct scp_domain_data {
  59. const char *name;
  60. u32 sta_mask;
  61. int ctl_offs;
  62. u32 sram_pdn_bits;
  63. u32 sram_pdn_ack_bits;
  64. u32 bus_prot_mask;
  65. enum clk_id clk_id;
  66. };
  67. static const struct scp_domain_data scp_domain_data[] __initconst = {
  68. [MT8173_POWER_DOMAIN_VDEC] = {
  69. .name = "vdec",
  70. .sta_mask = PWR_STATUS_VDEC,
  71. .ctl_offs = SPM_VDE_PWR_CON,
  72. .sram_pdn_bits = GENMASK(11, 8),
  73. .sram_pdn_ack_bits = GENMASK(12, 12),
  74. .clk_id = MT8173_CLK_MM,
  75. },
  76. [MT8173_POWER_DOMAIN_VENC] = {
  77. .name = "venc",
  78. .sta_mask = PWR_STATUS_VENC,
  79. .ctl_offs = SPM_VEN_PWR_CON,
  80. .sram_pdn_bits = GENMASK(11, 8),
  81. .sram_pdn_ack_bits = GENMASK(15, 12),
  82. .clk_id = MT8173_CLK_MM,
  83. },
  84. [MT8173_POWER_DOMAIN_ISP] = {
  85. .name = "isp",
  86. .sta_mask = PWR_STATUS_ISP,
  87. .ctl_offs = SPM_ISP_PWR_CON,
  88. .sram_pdn_bits = GENMASK(11, 8),
  89. .sram_pdn_ack_bits = GENMASK(13, 12),
  90. .clk_id = MT8173_CLK_MM,
  91. },
  92. [MT8173_POWER_DOMAIN_MM] = {
  93. .name = "mm",
  94. .sta_mask = PWR_STATUS_DISP,
  95. .ctl_offs = SPM_DIS_PWR_CON,
  96. .sram_pdn_bits = GENMASK(11, 8),
  97. .sram_pdn_ack_bits = GENMASK(12, 12),
  98. .clk_id = MT8173_CLK_MM,
  99. .bus_prot_mask = MT8173_TOP_AXI_PROT_EN_MM_M0 |
  100. MT8173_TOP_AXI_PROT_EN_MM_M1,
  101. },
  102. [MT8173_POWER_DOMAIN_VENC_LT] = {
  103. .name = "venc_lt",
  104. .sta_mask = PWR_STATUS_VENC_LT,
  105. .ctl_offs = SPM_VEN2_PWR_CON,
  106. .sram_pdn_bits = GENMASK(11, 8),
  107. .sram_pdn_ack_bits = GENMASK(15, 12),
  108. .clk_id = MT8173_CLK_MM,
  109. },
  110. [MT8173_POWER_DOMAIN_AUDIO] = {
  111. .name = "audio",
  112. .sta_mask = PWR_STATUS_AUDIO,
  113. .ctl_offs = SPM_AUDIO_PWR_CON,
  114. .sram_pdn_bits = GENMASK(11, 8),
  115. .sram_pdn_ack_bits = GENMASK(15, 12),
  116. .clk_id = MT8173_CLK_NONE,
  117. },
  118. [MT8173_POWER_DOMAIN_USB] = {
  119. .name = "usb",
  120. .sta_mask = PWR_STATUS_USB,
  121. .ctl_offs = SPM_USB_PWR_CON,
  122. .sram_pdn_bits = GENMASK(11, 8),
  123. .sram_pdn_ack_bits = GENMASK(15, 12),
  124. .clk_id = MT8173_CLK_NONE,
  125. },
  126. [MT8173_POWER_DOMAIN_MFG_ASYNC] = {
  127. .name = "mfg_async",
  128. .sta_mask = PWR_STATUS_MFG_ASYNC,
  129. .ctl_offs = SPM_MFG_ASYNC_PWR_CON,
  130. .sram_pdn_bits = GENMASK(11, 8),
  131. .sram_pdn_ack_bits = 0,
  132. .clk_id = MT8173_CLK_MFG,
  133. },
  134. [MT8173_POWER_DOMAIN_MFG_2D] = {
  135. .name = "mfg_2d",
  136. .sta_mask = PWR_STATUS_MFG_2D,
  137. .ctl_offs = SPM_MFG_2D_PWR_CON,
  138. .sram_pdn_bits = GENMASK(11, 8),
  139. .sram_pdn_ack_bits = GENMASK(13, 12),
  140. .clk_id = MT8173_CLK_NONE,
  141. },
  142. [MT8173_POWER_DOMAIN_MFG] = {
  143. .name = "mfg",
  144. .sta_mask = PWR_STATUS_MFG,
  145. .ctl_offs = SPM_MFG_PWR_CON,
  146. .sram_pdn_bits = GENMASK(13, 8),
  147. .sram_pdn_ack_bits = GENMASK(21, 16),
  148. .clk_id = MT8173_CLK_NONE,
  149. .bus_prot_mask = MT8173_TOP_AXI_PROT_EN_MFG_S |
  150. MT8173_TOP_AXI_PROT_EN_MFG_M0 |
  151. MT8173_TOP_AXI_PROT_EN_MFG_M1 |
  152. MT8173_TOP_AXI_PROT_EN_MFG_SNOOP_OUT,
  153. },
  154. };
  155. #define NUM_DOMAINS ARRAY_SIZE(scp_domain_data)
  156. struct scp;
  157. struct scp_domain {
  158. struct generic_pm_domain genpd;
  159. struct scp *scp;
  160. struct clk *clk;
  161. u32 sta_mask;
  162. void __iomem *ctl_addr;
  163. u32 sram_pdn_bits;
  164. u32 sram_pdn_ack_bits;
  165. u32 bus_prot_mask;
  166. };
  167. struct scp {
  168. struct scp_domain domains[NUM_DOMAINS];
  169. struct genpd_onecell_data pd_data;
  170. struct device *dev;
  171. void __iomem *base;
  172. struct regmap *infracfg;
  173. };
  174. static int scpsys_domain_is_on(struct scp_domain *scpd)
  175. {
  176. struct scp *scp = scpd->scp;
  177. u32 status = readl(scp->base + SPM_PWR_STATUS) & scpd->sta_mask;
  178. u32 status2 = readl(scp->base + SPM_PWR_STATUS_2ND) & scpd->sta_mask;
  179. /*
  180. * A domain is on when both status bits are set. If only one is set
  181. * return an error. This happens while powering up a domain
  182. */
  183. if (status && status2)
  184. return true;
  185. if (!status && !status2)
  186. return false;
  187. return -EINVAL;
  188. }
  189. static int scpsys_power_on(struct generic_pm_domain *genpd)
  190. {
  191. struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
  192. struct scp *scp = scpd->scp;
  193. unsigned long timeout;
  194. bool expired;
  195. void __iomem *ctl_addr = scpd->ctl_addr;
  196. u32 sram_pdn_ack = scpd->sram_pdn_ack_bits;
  197. u32 val;
  198. int ret;
  199. if (scpd->clk) {
  200. ret = clk_prepare_enable(scpd->clk);
  201. if (ret)
  202. goto err_clk;
  203. }
  204. val = readl(ctl_addr);
  205. val |= PWR_ON_BIT;
  206. writel(val, ctl_addr);
  207. val |= PWR_ON_2ND_BIT;
  208. writel(val, ctl_addr);
  209. /* wait until PWR_ACK = 1 */
  210. timeout = jiffies + HZ;
  211. expired = false;
  212. while (1) {
  213. ret = scpsys_domain_is_on(scpd);
  214. if (ret > 0)
  215. break;
  216. if (expired) {
  217. ret = -ETIMEDOUT;
  218. goto err_pwr_ack;
  219. }
  220. cpu_relax();
  221. if (time_after(jiffies, timeout))
  222. expired = true;
  223. }
  224. val &= ~PWR_CLK_DIS_BIT;
  225. writel(val, ctl_addr);
  226. val &= ~PWR_ISO_BIT;
  227. writel(val, ctl_addr);
  228. val |= PWR_RST_B_BIT;
  229. writel(val, ctl_addr);
  230. val &= ~scpd->sram_pdn_bits;
  231. writel(val, ctl_addr);
  232. /* wait until SRAM_PDN_ACK all 0 */
  233. timeout = jiffies + HZ;
  234. expired = false;
  235. while (sram_pdn_ack && (readl(ctl_addr) & sram_pdn_ack)) {
  236. if (expired) {
  237. ret = -ETIMEDOUT;
  238. goto err_pwr_ack;
  239. }
  240. cpu_relax();
  241. if (time_after(jiffies, timeout))
  242. expired = true;
  243. }
  244. if (scpd->bus_prot_mask) {
  245. ret = mtk_infracfg_clear_bus_protection(scp->infracfg,
  246. scpd->bus_prot_mask);
  247. if (ret)
  248. goto err_pwr_ack;
  249. }
  250. return 0;
  251. err_pwr_ack:
  252. clk_disable_unprepare(scpd->clk);
  253. err_clk:
  254. dev_err(scp->dev, "Failed to power on domain %s\n", genpd->name);
  255. return ret;
  256. }
  257. static int scpsys_power_off(struct generic_pm_domain *genpd)
  258. {
  259. struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
  260. struct scp *scp = scpd->scp;
  261. unsigned long timeout;
  262. bool expired;
  263. void __iomem *ctl_addr = scpd->ctl_addr;
  264. u32 pdn_ack = scpd->sram_pdn_ack_bits;
  265. u32 val;
  266. int ret;
  267. if (scpd->bus_prot_mask) {
  268. ret = mtk_infracfg_set_bus_protection(scp->infracfg,
  269. scpd->bus_prot_mask);
  270. if (ret)
  271. goto out;
  272. }
  273. val = readl(ctl_addr);
  274. val |= scpd->sram_pdn_bits;
  275. writel(val, ctl_addr);
  276. /* wait until SRAM_PDN_ACK all 1 */
  277. timeout = jiffies + HZ;
  278. expired = false;
  279. while (pdn_ack && (readl(ctl_addr) & pdn_ack) != pdn_ack) {
  280. if (expired) {
  281. ret = -ETIMEDOUT;
  282. goto out;
  283. }
  284. cpu_relax();
  285. if (time_after(jiffies, timeout))
  286. expired = true;
  287. }
  288. val |= PWR_ISO_BIT;
  289. writel(val, ctl_addr);
  290. val &= ~PWR_RST_B_BIT;
  291. writel(val, ctl_addr);
  292. val |= PWR_CLK_DIS_BIT;
  293. writel(val, ctl_addr);
  294. val &= ~PWR_ON_BIT;
  295. writel(val, ctl_addr);
  296. val &= ~PWR_ON_2ND_BIT;
  297. writel(val, ctl_addr);
  298. /* wait until PWR_ACK = 0 */
  299. timeout = jiffies + HZ;
  300. expired = false;
  301. while (1) {
  302. ret = scpsys_domain_is_on(scpd);
  303. if (ret == 0)
  304. break;
  305. if (expired) {
  306. ret = -ETIMEDOUT;
  307. goto out;
  308. }
  309. cpu_relax();
  310. if (time_after(jiffies, timeout))
  311. expired = true;
  312. }
  313. if (scpd->clk)
  314. clk_disable_unprepare(scpd->clk);
  315. return 0;
  316. out:
  317. dev_err(scp->dev, "Failed to power off domain %s\n", genpd->name);
  318. return ret;
  319. }
  320. static int __init scpsys_probe(struct platform_device *pdev)
  321. {
  322. struct genpd_onecell_data *pd_data;
  323. struct resource *res;
  324. int i, ret;
  325. struct scp *scp;
  326. struct clk *clk[MT8173_CLK_MAX];
  327. scp = devm_kzalloc(&pdev->dev, sizeof(*scp), GFP_KERNEL);
  328. if (!scp)
  329. return -ENOMEM;
  330. scp->dev = &pdev->dev;
  331. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  332. scp->base = devm_ioremap_resource(&pdev->dev, res);
  333. if (IS_ERR(scp->base))
  334. return PTR_ERR(scp->base);
  335. pd_data = &scp->pd_data;
  336. pd_data->domains = devm_kzalloc(&pdev->dev,
  337. sizeof(*pd_data->domains) * NUM_DOMAINS, GFP_KERNEL);
  338. if (!pd_data->domains)
  339. return -ENOMEM;
  340. clk[MT8173_CLK_MM] = devm_clk_get(&pdev->dev, "mm");
  341. if (IS_ERR(clk[MT8173_CLK_MM]))
  342. return PTR_ERR(clk[MT8173_CLK_MM]);
  343. clk[MT8173_CLK_MFG] = devm_clk_get(&pdev->dev, "mfg");
  344. if (IS_ERR(clk[MT8173_CLK_MFG]))
  345. return PTR_ERR(clk[MT8173_CLK_MFG]);
  346. scp->infracfg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  347. "infracfg");
  348. if (IS_ERR(scp->infracfg)) {
  349. dev_err(&pdev->dev, "Cannot find infracfg controller: %ld\n",
  350. PTR_ERR(scp->infracfg));
  351. return PTR_ERR(scp->infracfg);
  352. }
  353. pd_data->num_domains = NUM_DOMAINS;
  354. for (i = 0; i < NUM_DOMAINS; i++) {
  355. struct scp_domain *scpd = &scp->domains[i];
  356. struct generic_pm_domain *genpd = &scpd->genpd;
  357. const struct scp_domain_data *data = &scp_domain_data[i];
  358. pd_data->domains[i] = genpd;
  359. scpd->scp = scp;
  360. scpd->sta_mask = data->sta_mask;
  361. scpd->ctl_addr = scp->base + data->ctl_offs;
  362. scpd->sram_pdn_bits = data->sram_pdn_bits;
  363. scpd->sram_pdn_ack_bits = data->sram_pdn_ack_bits;
  364. scpd->bus_prot_mask = data->bus_prot_mask;
  365. if (data->clk_id != MT8173_CLK_NONE)
  366. scpd->clk = clk[data->clk_id];
  367. genpd->name = data->name;
  368. genpd->power_off = scpsys_power_off;
  369. genpd->power_on = scpsys_power_on;
  370. /*
  371. * Initially turn on all domains to make the domains usable
  372. * with !CONFIG_PM and to get the hardware in sync with the
  373. * software. The unused domains will be switched off during
  374. * late_init time.
  375. */
  376. genpd->power_on(genpd);
  377. pm_genpd_init(genpd, NULL, false);
  378. }
  379. /*
  380. * We are not allowed to fail here since there is no way to unregister
  381. * a power domain. Once registered above we have to keep the domains
  382. * valid.
  383. */
  384. ret = pm_genpd_add_subdomain(pd_data->domains[MT8173_POWER_DOMAIN_MFG_ASYNC],
  385. pd_data->domains[MT8173_POWER_DOMAIN_MFG_2D]);
  386. if (ret && IS_ENABLED(CONFIG_PM))
  387. dev_err(&pdev->dev, "Failed to add subdomain: %d\n", ret);
  388. ret = pm_genpd_add_subdomain(pd_data->domains[MT8173_POWER_DOMAIN_MFG_2D],
  389. pd_data->domains[MT8173_POWER_DOMAIN_MFG]);
  390. if (ret && IS_ENABLED(CONFIG_PM))
  391. dev_err(&pdev->dev, "Failed to add subdomain: %d\n", ret);
  392. ret = of_genpd_add_provider_onecell(pdev->dev.of_node, pd_data);
  393. if (ret)
  394. dev_err(&pdev->dev, "Failed to add OF provider: %d\n", ret);
  395. return 0;
  396. }
  397. static const struct of_device_id of_scpsys_match_tbl[] = {
  398. {
  399. .compatible = "mediatek,mt8173-scpsys",
  400. }, {
  401. /* sentinel */
  402. }
  403. };
  404. static struct platform_driver scpsys_drv = {
  405. .driver = {
  406. .name = "mtk-scpsys",
  407. .owner = THIS_MODULE,
  408. .of_match_table = of_match_ptr(of_scpsys_match_tbl),
  409. },
  410. };
  411. module_platform_driver_probe(scpsys_drv, scpsys_probe);