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_new = 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. .fast_io = true,
  287. };
  288. static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
  289. &imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base,
  290. &imx_gpc_domains[GPC_PGC_DOMAIN_PU].base,
  291. };
  292. static struct genpd_onecell_data imx_gpc_onecell_data = {
  293. .domains = imx_gpc_onecell_domains,
  294. .num_domains = 2,
  295. };
  296. static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
  297. unsigned int num_domains)
  298. {
  299. struct imx_pm_domain *domain;
  300. int i, ret;
  301. for (i = 0; i < num_domains; i++) {
  302. domain = &imx_gpc_domains[i];
  303. domain->regmap = regmap;
  304. domain->ipg_rate_mhz = 66;
  305. if (i == 1) {
  306. domain->supply = devm_regulator_get(dev, "pu");
  307. if (IS_ERR(domain->supply))
  308. return PTR_ERR(domain->supply);
  309. ret = imx_pgc_get_clocks(dev, domain);
  310. if (ret)
  311. goto clk_err;
  312. domain->base.power_on(&domain->base);
  313. }
  314. }
  315. for (i = 0; i < num_domains; i++)
  316. pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
  317. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  318. ret = of_genpd_add_provider_onecell(dev->of_node,
  319. &imx_gpc_onecell_data);
  320. if (ret)
  321. goto genpd_err;
  322. }
  323. return 0;
  324. genpd_err:
  325. for (i = 0; i < num_domains; i++)
  326. pm_genpd_remove(&imx_gpc_domains[i].base);
  327. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  328. clk_err:
  329. return ret;
  330. }
  331. static int imx_gpc_probe(struct platform_device *pdev)
  332. {
  333. const struct imx_gpc_dt_data *of_id_data = device_get_match_data(&pdev->dev);
  334. struct device_node *pgc_node;
  335. struct regmap *regmap;
  336. void __iomem *base;
  337. int ret;
  338. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  339. /* bail out if DT too old and doesn't provide the necessary info */
  340. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  341. !pgc_node)
  342. return 0;
  343. base = devm_platform_ioremap_resource(pdev, 0);
  344. if (IS_ERR(base))
  345. return PTR_ERR(base);
  346. regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  347. &imx_gpc_regmap_config);
  348. if (IS_ERR(regmap)) {
  349. ret = PTR_ERR(regmap);
  350. dev_err(&pdev->dev, "failed to init regmap: %d\n",
  351. ret);
  352. return ret;
  353. }
  354. /*
  355. * Disable PU power down by runtime PM if ERR009619 is present.
  356. *
  357. * The PRE clock will be paused for several cycles when turning on the
  358. * PU domain LDO from power down state. If PRE is in use at that time,
  359. * the IPU/PRG cannot get the correct display data from the PRE.
  360. *
  361. * This is not a concern when the whole system enters suspend state, so
  362. * it's safe to power down PU in this case.
  363. */
  364. if (of_id_data->err009619_present)
  365. imx_gpc_domains[GPC_PGC_DOMAIN_PU].base.flags |=
  366. GENPD_FLAG_RPM_ALWAYS_ON;
  367. /* Keep DISP always on if ERR006287 is present */
  368. if (of_id_data->err006287_present)
  369. imx_gpc_domains[GPC_PGC_DOMAIN_DISPLAY].base.flags |=
  370. GENPD_FLAG_ALWAYS_ON;
  371. if (!pgc_node) {
  372. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  373. of_id_data->num_domains);
  374. if (ret)
  375. return ret;
  376. } else {
  377. struct imx_pm_domain *domain;
  378. struct platform_device *pd_pdev;
  379. struct clk *ipg_clk;
  380. unsigned int ipg_rate_mhz;
  381. int domain_index;
  382. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  383. if (IS_ERR(ipg_clk))
  384. return PTR_ERR(ipg_clk);
  385. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  386. for_each_child_of_node_scoped(pgc_node, np) {
  387. ret = of_property_read_u32(np, "reg", &domain_index);
  388. if (ret)
  389. return ret;
  390. if (domain_index >= of_id_data->num_domains)
  391. continue;
  392. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  393. domain_index);
  394. if (!pd_pdev)
  395. return -ENOMEM;
  396. ret = platform_device_add_data(pd_pdev,
  397. &imx_gpc_domains[domain_index],
  398. sizeof(imx_gpc_domains[domain_index]));
  399. if (ret) {
  400. platform_device_put(pd_pdev);
  401. return ret;
  402. }
  403. domain = pd_pdev->dev.platform_data;
  404. domain->regmap = regmap;
  405. domain->ipg_rate_mhz = ipg_rate_mhz;
  406. pd_pdev->dev.parent = &pdev->dev;
  407. pd_pdev->dev.of_node = np;
  408. pd_pdev->dev.fwnode = of_fwnode_handle(np);
  409. ret = platform_device_add(pd_pdev);
  410. if (ret) {
  411. platform_device_put(pd_pdev);
  412. return ret;
  413. }
  414. }
  415. }
  416. return 0;
  417. }
  418. static void imx_gpc_remove(struct platform_device *pdev)
  419. {
  420. struct device_node *pgc_node;
  421. int ret;
  422. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  423. /* bail out if DT too old and doesn't provide the necessary info */
  424. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  425. !pgc_node)
  426. return;
  427. /*
  428. * If the old DT binding is used the toplevel driver needs to
  429. * de-register the power domains
  430. */
  431. if (!pgc_node) {
  432. of_genpd_del_provider(pdev->dev.of_node);
  433. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
  434. if (ret) {
  435. dev_err(&pdev->dev, "Failed to remove PU power domain (%pe)\n",
  436. ERR_PTR(ret));
  437. return;
  438. }
  439. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  440. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  441. if (ret) {
  442. dev_err(&pdev->dev, "Failed to remove ARM power domain (%pe)\n",
  443. ERR_PTR(ret));
  444. return;
  445. }
  446. }
  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_new = imx_gpc_remove,
  455. };
  456. builtin_platform_driver(imx_gpc_driver)