mtk-scpsys.c 13 KB

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