gpc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_domain.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/consumer.h>
  14. #define GPC_CNTR 0x000
  15. #define GPC_PGC_CTRL_OFFS 0x0
  16. #define GPC_PGC_PUPSCR_OFFS 0x4
  17. #define GPC_PGC_PDNSCR_OFFS 0x8
  18. #define GPC_PGC_SW2ISO_SHIFT 0x8
  19. #define GPC_PGC_SW_SHIFT 0x0
  20. #define GPC_PGC_PCI_PDN 0x200
  21. #define GPC_PGC_PCI_SR 0x20c
  22. #define GPC_PGC_GPU_PDN 0x260
  23. #define GPC_PGC_GPU_PUPSCR 0x264
  24. #define GPC_PGC_GPU_PDNSCR 0x268
  25. #define GPC_PGC_GPU_SR 0x26c
  26. #define GPC_PGC_DISP_PDN 0x240
  27. #define GPC_PGC_DISP_SR 0x24c
  28. #define GPU_VPU_PUP_REQ BIT(1)
  29. #define GPU_VPU_PDN_REQ BIT(0)
  30. #define GPC_CLK_MAX 7
  31. #define PGC_DOMAIN_FLAG_NO_PD BIT(0)
  32. struct imx_pm_domain {
  33. struct generic_pm_domain base;
  34. struct regmap *regmap;
  35. struct regulator *supply;
  36. struct clk *clk[GPC_CLK_MAX];
  37. int num_clks;
  38. unsigned int reg_offs;
  39. signed char cntr_pdn_bit;
  40. unsigned int ipg_rate_mhz;
  41. };
  42. static inline struct imx_pm_domain *
  43. to_imx_pm_domain(struct generic_pm_domain *genpd)
  44. {
  45. return container_of(genpd, struct imx_pm_domain, base);
  46. }
  47. static int imx6_pm_domain_power_off(struct generic_pm_domain *genpd)
  48. {
  49. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  50. int iso, iso2sw;
  51. u32 val;
  52. /* Read ISO and ISO2SW power down delays */
  53. regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PDNSCR_OFFS, &val);
  54. iso = val & 0x3f;
  55. iso2sw = (val >> 8) & 0x3f;
  56. /* Gate off domain when powered down */
  57. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  58. 0x1, 0x1);
  59. /* Request GPC to power down domain */
  60. val = BIT(pd->cntr_pdn_bit);
  61. regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
  62. /* Wait ISO + ISO2SW IPG clock cycles */
  63. udelay(DIV_ROUND_UP(iso + iso2sw, pd->ipg_rate_mhz));
  64. if (pd->supply)
  65. regulator_disable(pd->supply);
  66. return 0;
  67. }
  68. static int imx6_pm_domain_power_on(struct generic_pm_domain *genpd)
  69. {
  70. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  71. int i, ret;
  72. u32 val, req;
  73. if (pd->supply) {
  74. ret = regulator_enable(pd->supply);
  75. if (ret) {
  76. pr_err("%s: failed to enable regulator: %d\n",
  77. __func__, ret);
  78. return ret;
  79. }
  80. }
  81. /* Enable reset clocks for all devices in the domain */
  82. for (i = 0; i < pd->num_clks; i++)
  83. clk_prepare_enable(pd->clk[i]);
  84. /* Gate off domain when powered down */
  85. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  86. 0x1, 0x1);
  87. /* Request GPC to power up domain */
  88. req = BIT(pd->cntr_pdn_bit + 1);
  89. regmap_update_bits(pd->regmap, GPC_CNTR, req, req);
  90. /* Wait for the PGC to handle the request */
  91. ret = regmap_read_poll_timeout(pd->regmap, GPC_CNTR, val, !(val & req),
  92. 1, 50);
  93. if (ret)
  94. pr_err("powerup request on domain %s timed out\n", genpd->name);
  95. /* Wait for reset to propagate through peripherals */
  96. usleep_range(5, 10);
  97. /* Disable reset clocks for all devices in the domain */
  98. for (i = 0; i < pd->num_clks; i++)
  99. clk_disable_unprepare(pd->clk[i]);
  100. return 0;
  101. }
  102. static int imx_pgc_get_clocks(struct device *dev, struct imx_pm_domain *domain)
  103. {
  104. int i, ret;
  105. for (i = 0; ; i++) {
  106. struct clk *clk = of_clk_get(dev->of_node, i);
  107. if (IS_ERR(clk))
  108. break;
  109. if (i >= GPC_CLK_MAX) {
  110. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  111. ret = -EINVAL;
  112. goto clk_err;
  113. }
  114. domain->clk[i] = clk;
  115. }
  116. domain->num_clks = i;
  117. return 0;
  118. clk_err:
  119. while (i--)
  120. clk_put(domain->clk[i]);
  121. return ret;
  122. }
  123. static void imx_pgc_put_clocks(struct imx_pm_domain *domain)
  124. {
  125. int i;
  126. for (i = domain->num_clks - 1; i >= 0; i--)
  127. clk_put(domain->clk[i]);
  128. }
  129. static int imx_pgc_parse_dt(struct device *dev, struct imx_pm_domain *domain)
  130. {
  131. /* try to get the domain supply regulator */
  132. domain->supply = devm_regulator_get_optional(dev, "power");
  133. if (IS_ERR(domain->supply)) {
  134. if (PTR_ERR(domain->supply) == -ENODEV)
  135. domain->supply = NULL;
  136. else
  137. return PTR_ERR(domain->supply);
  138. }
  139. /* try to get all clocks needed for reset propagation */
  140. return imx_pgc_get_clocks(dev, domain);
  141. }
  142. static int imx_pgc_power_domain_probe(struct platform_device *pdev)
  143. {
  144. struct imx_pm_domain *domain = pdev->dev.platform_data;
  145. struct device *dev = &pdev->dev;
  146. int ret;
  147. /* if this PD is associated with a DT node try to parse it */
  148. if (dev->of_node) {
  149. ret = imx_pgc_parse_dt(dev, domain);
  150. if (ret)
  151. return ret;
  152. }
  153. /* initially power on the domain */
  154. if (domain->base.power_on)
  155. domain->base.power_on(&domain->base);
  156. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  157. pm_genpd_init(&domain->base, NULL, false);
  158. ret = of_genpd_add_provider_simple(dev->of_node, &domain->base);
  159. if (ret)
  160. goto genpd_err;
  161. }
  162. device_link_add(dev, dev->parent, DL_FLAG_AUTOREMOVE_CONSUMER);
  163. return 0;
  164. genpd_err:
  165. pm_genpd_remove(&domain->base);
  166. imx_pgc_put_clocks(domain);
  167. return ret;
  168. }
  169. static int imx_pgc_power_domain_remove(struct platform_device *pdev)
  170. {
  171. struct imx_pm_domain *domain = pdev->dev.platform_data;
  172. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  173. of_genpd_del_provider(pdev->dev.of_node);
  174. pm_genpd_remove(&domain->base);
  175. imx_pgc_put_clocks(domain);
  176. }
  177. return 0;
  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. .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 of_device_id *of_id =
  334. of_match_device(imx_gpc_dt_ids, &pdev->dev);
  335. const struct imx_gpc_dt_data *of_id_data = of_id->data;
  336. struct device_node *pgc_node;
  337. struct regmap *regmap;
  338. void __iomem *base;
  339. int ret;
  340. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  341. /* bail out if DT too old and doesn't provide the necessary info */
  342. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  343. !pgc_node)
  344. return 0;
  345. base = devm_platform_ioremap_resource(pdev, 0);
  346. if (IS_ERR(base))
  347. return PTR_ERR(base);
  348. regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  349. &imx_gpc_regmap_config);
  350. if (IS_ERR(regmap)) {
  351. ret = PTR_ERR(regmap);
  352. dev_err(&pdev->dev, "failed to init regmap: %d\n",
  353. ret);
  354. return ret;
  355. }
  356. /*
  357. * Disable PU power down by runtime PM if ERR009619 is present.
  358. *
  359. * The PRE clock will be paused for several cycles when turning on the
  360. * PU domain LDO from power down state. If PRE is in use at that time,
  361. * the IPU/PRG cannot get the correct display data from the PRE.
  362. *
  363. * This is not a concern when the whole system enters suspend state, so
  364. * it's safe to power down PU in this case.
  365. */
  366. if (of_id_data->err009619_present)
  367. imx_gpc_domains[GPC_PGC_DOMAIN_PU].base.flags |=
  368. GENPD_FLAG_RPM_ALWAYS_ON;
  369. /* Keep DISP always on if ERR006287 is present */
  370. if (of_id_data->err006287_present)
  371. imx_gpc_domains[GPC_PGC_DOMAIN_DISPLAY].base.flags |=
  372. GENPD_FLAG_ALWAYS_ON;
  373. if (!pgc_node) {
  374. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  375. of_id_data->num_domains);
  376. if (ret)
  377. return ret;
  378. } else {
  379. struct imx_pm_domain *domain;
  380. struct platform_device *pd_pdev;
  381. struct device_node *np;
  382. struct clk *ipg_clk;
  383. unsigned int ipg_rate_mhz;
  384. int domain_index;
  385. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  386. if (IS_ERR(ipg_clk))
  387. return PTR_ERR(ipg_clk);
  388. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  389. for_each_child_of_node(pgc_node, np) {
  390. ret = of_property_read_u32(np, "reg", &domain_index);
  391. if (ret) {
  392. of_node_put(np);
  393. return ret;
  394. }
  395. if (domain_index >= of_id_data->num_domains)
  396. continue;
  397. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  398. domain_index);
  399. if (!pd_pdev) {
  400. of_node_put(np);
  401. return -ENOMEM;
  402. }
  403. ret = platform_device_add_data(pd_pdev,
  404. &imx_gpc_domains[domain_index],
  405. sizeof(imx_gpc_domains[domain_index]));
  406. if (ret) {
  407. platform_device_put(pd_pdev);
  408. of_node_put(np);
  409. return ret;
  410. }
  411. domain = pd_pdev->dev.platform_data;
  412. domain->regmap = regmap;
  413. domain->ipg_rate_mhz = ipg_rate_mhz;
  414. pd_pdev->dev.parent = &pdev->dev;
  415. pd_pdev->dev.of_node = np;
  416. ret = platform_device_add(pd_pdev);
  417. if (ret) {
  418. platform_device_put(pd_pdev);
  419. of_node_put(np);
  420. return ret;
  421. }
  422. }
  423. }
  424. return 0;
  425. }
  426. static int imx_gpc_remove(struct platform_device *pdev)
  427. {
  428. struct device_node *pgc_node;
  429. int ret;
  430. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  431. /* bail out if DT too old and doesn't provide the necessary info */
  432. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  433. !pgc_node)
  434. return 0;
  435. /*
  436. * If the old DT binding is used the toplevel driver needs to
  437. * de-register the power domains
  438. */
  439. if (!pgc_node) {
  440. of_genpd_del_provider(pdev->dev.of_node);
  441. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
  442. if (ret)
  443. return ret;
  444. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  445. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  446. if (ret)
  447. return ret;
  448. }
  449. return 0;
  450. }
  451. static struct platform_driver imx_gpc_driver = {
  452. .driver = {
  453. .name = "imx-gpc",
  454. .of_match_table = imx_gpc_dt_ids,
  455. },
  456. .probe = imx_gpc_probe,
  457. .remove = imx_gpc_remove,
  458. };
  459. builtin_platform_driver(imx_gpc_driver)