gpc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2015-2017 Pengutronix, Lucas Stach <kernel@pengutronix.de>
  4. * Copyright 2011-2013 Freescale Semiconductor, Inc.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_domain.h>
  12. #include <linux/property.h>
  13. #include <linux/regmap.h>
  14. #include <linux/regulator/consumer.h>
  15. #define GPC_CNTR 0x000
  16. #define GPC_PGC_CTRL_OFFS 0x0
  17. #define GPC_PGC_PUPSCR_OFFS 0x4
  18. #define GPC_PGC_PDNSCR_OFFS 0x8
  19. #define GPC_PGC_SW2ISO_SHIFT 0x8
  20. #define GPC_PGC_SW_SHIFT 0x0
  21. #define GPC_PGC_PCI_PDN 0x200
  22. #define GPC_PGC_PCI_SR 0x20c
  23. #define GPC_PGC_GPU_PDN 0x260
  24. #define GPC_PGC_GPU_PUPSCR 0x264
  25. #define GPC_PGC_GPU_PDNSCR 0x268
  26. #define GPC_PGC_GPU_SR 0x26c
  27. #define GPC_PGC_DISP_PDN 0x240
  28. #define GPC_PGC_DISP_SR 0x24c
  29. #define GPU_VPU_PUP_REQ BIT(1)
  30. #define GPU_VPU_PDN_REQ BIT(0)
  31. #define GPC_CLK_MAX 7
  32. #define PGC_DOMAIN_FLAG_NO_PD BIT(0)
  33. struct imx_pm_domain {
  34. struct generic_pm_domain base;
  35. struct regmap *regmap;
  36. struct regulator *supply;
  37. struct clk *clk[GPC_CLK_MAX];
  38. int num_clks;
  39. unsigned int reg_offs;
  40. signed char cntr_pdn_bit;
  41. unsigned int ipg_rate_mhz;
  42. };
  43. static inline struct imx_pm_domain *
  44. to_imx_pm_domain(struct generic_pm_domain *genpd)
  45. {
  46. return container_of(genpd, struct imx_pm_domain, base);
  47. }
  48. static int imx6_pm_domain_power_off(struct generic_pm_domain *genpd)
  49. {
  50. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  51. int iso, iso2sw;
  52. u32 val;
  53. /* Read ISO and ISO2SW power down delays */
  54. regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PDNSCR_OFFS, &val);
  55. iso = val & 0x3f;
  56. iso2sw = (val >> 8) & 0x3f;
  57. /* Gate off domain when powered down */
  58. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  59. 0x1, 0x1);
  60. /* Request GPC to power down domain */
  61. val = BIT(pd->cntr_pdn_bit);
  62. regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
  63. /* Wait ISO + ISO2SW IPG clock cycles */
  64. udelay(DIV_ROUND_UP(iso + iso2sw, pd->ipg_rate_mhz));
  65. if (pd->supply)
  66. regulator_disable(pd->supply);
  67. return 0;
  68. }
  69. static int imx6_pm_domain_power_on(struct generic_pm_domain *genpd)
  70. {
  71. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  72. int i, ret;
  73. u32 val, req;
  74. if (pd->supply) {
  75. ret = regulator_enable(pd->supply);
  76. if (ret) {
  77. pr_err("%s: failed to enable regulator: %d\n",
  78. __func__, ret);
  79. return ret;
  80. }
  81. }
  82. /* Enable reset clocks for all devices in the domain */
  83. for (i = 0; i < pd->num_clks; i++)
  84. clk_prepare_enable(pd->clk[i]);
  85. /* Gate off domain when powered down */
  86. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  87. 0x1, 0x1);
  88. /* Request GPC to power up domain */
  89. req = BIT(pd->cntr_pdn_bit + 1);
  90. regmap_update_bits(pd->regmap, GPC_CNTR, req, req);
  91. /* Wait for the PGC to handle the request */
  92. ret = regmap_read_poll_timeout(pd->regmap, GPC_CNTR, val, !(val & req),
  93. 1, 50);
  94. if (ret)
  95. pr_err("powerup request on domain %s timed out\n", genpd->name);
  96. /* Wait for reset to propagate through peripherals */
  97. usleep_range(5, 10);
  98. /* Disable reset clocks for all devices in the domain */
  99. for (i = 0; i < pd->num_clks; i++)
  100. clk_disable_unprepare(pd->clk[i]);
  101. return 0;
  102. }
  103. static int imx_pgc_get_clocks(struct device *dev, struct imx_pm_domain *domain)
  104. {
  105. int i, ret;
  106. for (i = 0; ; i++) {
  107. struct clk *clk = of_clk_get(dev->of_node, i);
  108. if (IS_ERR(clk))
  109. break;
  110. if (i >= GPC_CLK_MAX) {
  111. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  112. ret = -EINVAL;
  113. goto clk_err;
  114. }
  115. domain->clk[i] = clk;
  116. }
  117. domain->num_clks = i;
  118. return 0;
  119. clk_err:
  120. while (i--)
  121. clk_put(domain->clk[i]);
  122. return ret;
  123. }
  124. static void imx_pgc_put_clocks(struct imx_pm_domain *domain)
  125. {
  126. int i;
  127. for (i = domain->num_clks - 1; i >= 0; i--)
  128. clk_put(domain->clk[i]);
  129. }
  130. static int imx_pgc_parse_dt(struct device *dev, struct imx_pm_domain *domain)
  131. {
  132. /* try to get the domain supply regulator */
  133. domain->supply = devm_regulator_get_optional(dev, "power");
  134. if (IS_ERR(domain->supply)) {
  135. if (PTR_ERR(domain->supply) == -ENODEV)
  136. domain->supply = NULL;
  137. else
  138. return PTR_ERR(domain->supply);
  139. }
  140. /* try to get all clocks needed for reset propagation */
  141. return imx_pgc_get_clocks(dev, domain);
  142. }
  143. static int imx_pgc_power_domain_probe(struct platform_device *pdev)
  144. {
  145. struct imx_pm_domain *domain = pdev->dev.platform_data;
  146. struct device *dev = &pdev->dev;
  147. int ret;
  148. /* if this PD is associated with a DT node try to parse it */
  149. if (dev->of_node) {
  150. ret = imx_pgc_parse_dt(dev, domain);
  151. if (ret)
  152. return ret;
  153. }
  154. /* initially power on the domain */
  155. if (domain->base.power_on)
  156. domain->base.power_on(&domain->base);
  157. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  158. pm_genpd_init(&domain->base, NULL, false);
  159. ret = of_genpd_add_provider_simple(dev->of_node, &domain->base);
  160. if (ret)
  161. goto genpd_err;
  162. }
  163. device_link_add(dev, dev->parent, DL_FLAG_AUTOREMOVE_CONSUMER);
  164. return 0;
  165. genpd_err:
  166. pm_genpd_remove(&domain->base);
  167. imx_pgc_put_clocks(domain);
  168. return ret;
  169. }
  170. static void imx_pgc_power_domain_remove(struct platform_device *pdev)
  171. {
  172. struct imx_pm_domain *domain = pdev->dev.platform_data;
  173. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  174. of_genpd_del_provider(pdev->dev.of_node);
  175. pm_genpd_remove(&domain->base);
  176. imx_pgc_put_clocks(domain);
  177. }
  178. }
  179. static const struct platform_device_id imx_pgc_power_domain_id[] = {
  180. { "imx-pgc-power-domain"},
  181. { },
  182. };
  183. static struct platform_driver imx_pgc_power_domain_driver = {
  184. .driver = {
  185. .name = "imx-pgc-pd",
  186. },
  187. .probe = imx_pgc_power_domain_probe,
  188. .remove = imx_pgc_power_domain_remove,
  189. .id_table = imx_pgc_power_domain_id,
  190. };
  191. builtin_platform_driver(imx_pgc_power_domain_driver)
  192. #define GPC_PGC_DOMAIN_ARM 0
  193. #define GPC_PGC_DOMAIN_PU 1
  194. #define GPC_PGC_DOMAIN_DISPLAY 2
  195. #define GPC_PGC_DOMAIN_PCI 3
  196. static struct genpd_power_state imx6_pm_domain_pu_state = {
  197. .power_off_latency_ns = 25000,
  198. .power_on_latency_ns = 2000000,
  199. };
  200. static struct imx_pm_domain imx_gpc_domains[] = {
  201. [GPC_PGC_DOMAIN_ARM] = {
  202. .base = {
  203. .name = "ARM",
  204. .flags = GENPD_FLAG_ALWAYS_ON,
  205. },
  206. },
  207. [GPC_PGC_DOMAIN_PU] = {
  208. .base = {
  209. .name = "PU",
  210. .power_off = imx6_pm_domain_power_off,
  211. .power_on = imx6_pm_domain_power_on,
  212. .states = &imx6_pm_domain_pu_state,
  213. .state_count = 1,
  214. },
  215. .reg_offs = 0x260,
  216. .cntr_pdn_bit = 0,
  217. },
  218. [GPC_PGC_DOMAIN_DISPLAY] = {
  219. .base = {
  220. .name = "DISPLAY",
  221. .power_off = imx6_pm_domain_power_off,
  222. .power_on = imx6_pm_domain_power_on,
  223. },
  224. .reg_offs = 0x240,
  225. .cntr_pdn_bit = 4,
  226. },
  227. [GPC_PGC_DOMAIN_PCI] = {
  228. .base = {
  229. .name = "PCI",
  230. .power_off = imx6_pm_domain_power_off,
  231. .power_on = imx6_pm_domain_power_on,
  232. },
  233. .reg_offs = 0x200,
  234. .cntr_pdn_bit = 6,
  235. },
  236. };
  237. struct imx_gpc_dt_data {
  238. int num_domains;
  239. bool err009619_present;
  240. bool err006287_present;
  241. };
  242. static const struct imx_gpc_dt_data imx6q_dt_data = {
  243. .num_domains = 2,
  244. .err009619_present = false,
  245. .err006287_present = false,
  246. };
  247. static const struct imx_gpc_dt_data imx6qp_dt_data = {
  248. .num_domains = 2,
  249. .err009619_present = true,
  250. .err006287_present = false,
  251. };
  252. static const struct imx_gpc_dt_data imx6sl_dt_data = {
  253. .num_domains = 3,
  254. .err009619_present = false,
  255. .err006287_present = true,
  256. };
  257. static const struct imx_gpc_dt_data imx6sx_dt_data = {
  258. .num_domains = 4,
  259. .err009619_present = false,
  260. .err006287_present = false,
  261. };
  262. static const struct of_device_id imx_gpc_dt_ids[] = {
  263. { .compatible = "fsl,imx6q-gpc", .data = &imx6q_dt_data },
  264. { .compatible = "fsl,imx6qp-gpc", .data = &imx6qp_dt_data },
  265. { .compatible = "fsl,imx6sl-gpc", .data = &imx6sl_dt_data },
  266. { .compatible = "fsl,imx6sx-gpc", .data = &imx6sx_dt_data },
  267. { }
  268. };
  269. static const struct regmap_range yes_ranges[] = {
  270. regmap_reg_range(GPC_CNTR, GPC_CNTR),
  271. regmap_reg_range(GPC_PGC_PCI_PDN, GPC_PGC_PCI_SR),
  272. regmap_reg_range(GPC_PGC_GPU_PDN, GPC_PGC_GPU_SR),
  273. regmap_reg_range(GPC_PGC_DISP_PDN, GPC_PGC_DISP_SR),
  274. };
  275. static const struct regmap_access_table access_table = {
  276. .yes_ranges = yes_ranges,
  277. .n_yes_ranges = ARRAY_SIZE(yes_ranges),
  278. };
  279. static const struct regmap_config imx_gpc_regmap_config = {
  280. .reg_bits = 32,
  281. .val_bits = 32,
  282. .reg_stride = 4,
  283. .rd_table = &access_table,
  284. .wr_table = &access_table,
  285. .max_register = 0x2ac,
  286. };
  287. static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
  288. &imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base,
  289. &imx_gpc_domains[GPC_PGC_DOMAIN_PU].base,
  290. };
  291. static struct genpd_onecell_data imx_gpc_onecell_data = {
  292. .domains = imx_gpc_onecell_domains,
  293. .num_domains = 2,
  294. };
  295. static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
  296. unsigned int num_domains)
  297. {
  298. struct imx_pm_domain *domain;
  299. int i, ret;
  300. for (i = 0; i < num_domains; i++) {
  301. domain = &imx_gpc_domains[i];
  302. domain->regmap = regmap;
  303. domain->ipg_rate_mhz = 66;
  304. if (i == 1) {
  305. domain->supply = devm_regulator_get(dev, "pu");
  306. if (IS_ERR(domain->supply))
  307. return PTR_ERR(domain->supply);
  308. ret = imx_pgc_get_clocks(dev, domain);
  309. if (ret)
  310. goto clk_err;
  311. domain->base.power_on(&domain->base);
  312. }
  313. }
  314. for (i = 0; i < num_domains; i++)
  315. pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
  316. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  317. ret = of_genpd_add_provider_onecell(dev->of_node,
  318. &imx_gpc_onecell_data);
  319. if (ret)
  320. goto genpd_err;
  321. }
  322. return 0;
  323. genpd_err:
  324. for (i = 0; i < num_domains; i++)
  325. pm_genpd_remove(&imx_gpc_domains[i].base);
  326. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  327. clk_err:
  328. return ret;
  329. }
  330. static int imx_gpc_probe(struct platform_device *pdev)
  331. {
  332. const struct imx_gpc_dt_data *of_id_data = device_get_match_data(&pdev->dev);
  333. struct device_node *pgc_node __free(device_node)
  334. = of_get_child_by_name(pdev->dev.of_node, "pgc");
  335. struct regmap *regmap;
  336. void __iomem *base;
  337. int ret;
  338. /* bail out if DT too old and doesn't provide the necessary info */
  339. if (!of_property_present(pdev->dev.of_node, "#power-domain-cells") &&
  340. !pgc_node)
  341. return 0;
  342. base = devm_platform_ioremap_resource(pdev, 0);
  343. if (IS_ERR(base))
  344. return PTR_ERR(base);
  345. regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  346. &imx_gpc_regmap_config);
  347. if (IS_ERR(regmap)) {
  348. ret = PTR_ERR(regmap);
  349. dev_err(&pdev->dev, "failed to init regmap: %d\n",
  350. ret);
  351. return ret;
  352. }
  353. /*
  354. * Disable PU power down by runtime PM if ERR009619 is present.
  355. *
  356. * The PRE clock will be paused for several cycles when turning on the
  357. * PU domain LDO from power down state. If PRE is in use at that time,
  358. * the IPU/PRG cannot get the correct display data from the PRE.
  359. *
  360. * This is not a concern when the whole system enters suspend state, so
  361. * it's safe to power down PU in this case.
  362. */
  363. if (of_id_data->err009619_present)
  364. imx_gpc_domains[GPC_PGC_DOMAIN_PU].base.flags |=
  365. GENPD_FLAG_RPM_ALWAYS_ON;
  366. /* Keep DISP always on if ERR006287 is present */
  367. if (of_id_data->err006287_present)
  368. imx_gpc_domains[GPC_PGC_DOMAIN_DISPLAY].base.flags |=
  369. GENPD_FLAG_ALWAYS_ON;
  370. if (!pgc_node) {
  371. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  372. of_id_data->num_domains);
  373. if (ret)
  374. return ret;
  375. } else {
  376. struct imx_pm_domain *domain;
  377. struct platform_device *pd_pdev;
  378. struct clk *ipg_clk;
  379. unsigned int ipg_rate_mhz;
  380. int domain_index;
  381. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  382. if (IS_ERR(ipg_clk))
  383. return PTR_ERR(ipg_clk);
  384. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  385. for_each_child_of_node_scoped(pgc_node, np) {
  386. ret = of_property_read_u32(np, "reg", &domain_index);
  387. if (ret)
  388. return ret;
  389. if (domain_index >= of_id_data->num_domains)
  390. continue;
  391. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  392. domain_index);
  393. if (!pd_pdev)
  394. return -ENOMEM;
  395. ret = platform_device_add_data(pd_pdev,
  396. &imx_gpc_domains[domain_index],
  397. sizeof(imx_gpc_domains[domain_index]));
  398. if (ret) {
  399. platform_device_put(pd_pdev);
  400. return ret;
  401. }
  402. domain = pd_pdev->dev.platform_data;
  403. domain->regmap = regmap;
  404. domain->ipg_rate_mhz = ipg_rate_mhz;
  405. pd_pdev->dev.parent = &pdev->dev;
  406. pd_pdev->dev.of_node = np;
  407. pd_pdev->dev.fwnode = of_fwnode_handle(np);
  408. ret = platform_device_add(pd_pdev);
  409. if (ret) {
  410. platform_device_put(pd_pdev);
  411. return ret;
  412. }
  413. }
  414. }
  415. return 0;
  416. }
  417. static void imx_gpc_remove(struct platform_device *pdev)
  418. {
  419. struct device_node *pgc_node;
  420. int ret;
  421. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  422. /* bail out if DT too old and doesn't provide the necessary info */
  423. if (!of_property_present(pdev->dev.of_node, "#power-domain-cells") &&
  424. !pgc_node)
  425. return;
  426. /*
  427. * If the old DT binding is used the toplevel driver needs to
  428. * de-register the power domains
  429. */
  430. if (!pgc_node) {
  431. of_genpd_del_provider(pdev->dev.of_node);
  432. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
  433. if (ret) {
  434. dev_err(&pdev->dev, "Failed to remove PU power domain (%pe)\n",
  435. ERR_PTR(ret));
  436. return;
  437. }
  438. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  439. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  440. if (ret) {
  441. dev_err(&pdev->dev, "Failed to remove ARM power domain (%pe)\n",
  442. ERR_PTR(ret));
  443. return;
  444. }
  445. }
  446. of_node_put(pgc_node);
  447. }
  448. static struct platform_driver imx_gpc_driver = {
  449. .driver = {
  450. .name = "imx-gpc",
  451. .of_match_table = imx_gpc_dt_ids,
  452. },
  453. .probe = imx_gpc_probe,
  454. .remove = imx_gpc_remove,
  455. };
  456. builtin_platform_driver(imx_gpc_driver)