gpc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 6
  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, sw, sw2iso;
  72. u32 val;
  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. /* Read ISO and ISO2SW power up delays */
  88. regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PUPSCR_OFFS, &val);
  89. sw = val & 0x3f;
  90. sw2iso = (val >> 8) & 0x3f;
  91. /* Request GPC to power up domain */
  92. val = BIT(pd->cntr_pdn_bit + 1);
  93. regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
  94. /* Wait ISO + ISO2SW IPG clock cycles */
  95. udelay(DIV_ROUND_UP(sw + sw2iso, pd->ipg_rate_mhz));
  96. /* Disable reset clocks for all devices in the domain */
  97. for (i = 0; i < pd->num_clks; i++)
  98. clk_disable_unprepare(pd->clk[i]);
  99. return 0;
  100. }
  101. static int imx_pgc_get_clocks(struct device *dev, struct imx_pm_domain *domain)
  102. {
  103. int i, ret;
  104. for (i = 0; ; i++) {
  105. struct clk *clk = of_clk_get(dev->of_node, i);
  106. if (IS_ERR(clk))
  107. break;
  108. if (i >= GPC_CLK_MAX) {
  109. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  110. ret = -EINVAL;
  111. goto clk_err;
  112. }
  113. domain->clk[i] = clk;
  114. }
  115. domain->num_clks = i;
  116. return 0;
  117. clk_err:
  118. while (i--)
  119. clk_put(domain->clk[i]);
  120. return ret;
  121. }
  122. static void imx_pgc_put_clocks(struct imx_pm_domain *domain)
  123. {
  124. int i;
  125. for (i = domain->num_clks - 1; i >= 0; i--)
  126. clk_put(domain->clk[i]);
  127. }
  128. static int imx_pgc_parse_dt(struct device *dev, struct imx_pm_domain *domain)
  129. {
  130. /* try to get the domain supply regulator */
  131. domain->supply = devm_regulator_get_optional(dev, "power");
  132. if (IS_ERR(domain->supply)) {
  133. if (PTR_ERR(domain->supply) == -ENODEV)
  134. domain->supply = NULL;
  135. else
  136. return PTR_ERR(domain->supply);
  137. }
  138. /* try to get all clocks needed for reset propagation */
  139. return imx_pgc_get_clocks(dev, domain);
  140. }
  141. static int imx_pgc_power_domain_probe(struct platform_device *pdev)
  142. {
  143. struct imx_pm_domain *domain = pdev->dev.platform_data;
  144. struct device *dev = &pdev->dev;
  145. int ret;
  146. /* if this PD is associated with a DT node try to parse it */
  147. if (dev->of_node) {
  148. ret = imx_pgc_parse_dt(dev, domain);
  149. if (ret)
  150. return ret;
  151. }
  152. /* initially power on the domain */
  153. if (domain->base.power_on)
  154. domain->base.power_on(&domain->base);
  155. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  156. pm_genpd_init(&domain->base, NULL, false);
  157. ret = of_genpd_add_provider_simple(dev->of_node, &domain->base);
  158. if (ret)
  159. goto genpd_err;
  160. }
  161. device_link_add(dev, dev->parent, DL_FLAG_AUTOREMOVE_CONSUMER);
  162. return 0;
  163. genpd_err:
  164. pm_genpd_remove(&domain->base);
  165. imx_pgc_put_clocks(domain);
  166. return ret;
  167. }
  168. static int imx_pgc_power_domain_remove(struct platform_device *pdev)
  169. {
  170. struct imx_pm_domain *domain = pdev->dev.platform_data;
  171. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  172. of_genpd_del_provider(pdev->dev.of_node);
  173. pm_genpd_remove(&domain->base);
  174. imx_pgc_put_clocks(domain);
  175. }
  176. return 0;
  177. }
  178. static const struct platform_device_id imx_pgc_power_domain_id[] = {
  179. { "imx-pgc-power-domain"},
  180. { },
  181. };
  182. static struct platform_driver imx_pgc_power_domain_driver = {
  183. .driver = {
  184. .name = "imx-pgc-pd",
  185. },
  186. .probe = imx_pgc_power_domain_probe,
  187. .remove = imx_pgc_power_domain_remove,
  188. .id_table = imx_pgc_power_domain_id,
  189. };
  190. builtin_platform_driver(imx_pgc_power_domain_driver)
  191. #define GPC_PGC_DOMAIN_ARM 0
  192. #define GPC_PGC_DOMAIN_PU 1
  193. #define GPC_PGC_DOMAIN_DISPLAY 2
  194. #define GPC_PGC_DOMAIN_PCI 3
  195. static struct genpd_power_state imx6_pm_domain_pu_state = {
  196. .power_off_latency_ns = 25000,
  197. .power_on_latency_ns = 2000000,
  198. };
  199. static struct imx_pm_domain imx_gpc_domains[] = {
  200. [GPC_PGC_DOMAIN_ARM] {
  201. .base = {
  202. .name = "ARM",
  203. .flags = GENPD_FLAG_ALWAYS_ON,
  204. },
  205. },
  206. [GPC_PGC_DOMAIN_PU] {
  207. .base = {
  208. .name = "PU",
  209. .power_off = imx6_pm_domain_power_off,
  210. .power_on = imx6_pm_domain_power_on,
  211. .states = &imx6_pm_domain_pu_state,
  212. .state_count = 1,
  213. },
  214. .reg_offs = 0x260,
  215. .cntr_pdn_bit = 0,
  216. },
  217. [GPC_PGC_DOMAIN_DISPLAY] {
  218. .base = {
  219. .name = "DISPLAY",
  220. .power_off = imx6_pm_domain_power_off,
  221. .power_on = imx6_pm_domain_power_on,
  222. },
  223. .reg_offs = 0x240,
  224. .cntr_pdn_bit = 4,
  225. },
  226. [GPC_PGC_DOMAIN_PCI] {
  227. .base = {
  228. .name = "PCI",
  229. .power_off = imx6_pm_domain_power_off,
  230. .power_on = imx6_pm_domain_power_on,
  231. },
  232. .reg_offs = 0x200,
  233. .cntr_pdn_bit = 6,
  234. },
  235. };
  236. struct imx_gpc_dt_data {
  237. int num_domains;
  238. bool err009619_present;
  239. bool err006287_present;
  240. };
  241. static const struct imx_gpc_dt_data imx6q_dt_data = {
  242. .num_domains = 2,
  243. .err009619_present = false,
  244. .err006287_present = false,
  245. };
  246. static const struct imx_gpc_dt_data imx6qp_dt_data = {
  247. .num_domains = 2,
  248. .err009619_present = true,
  249. .err006287_present = false,
  250. };
  251. static const struct imx_gpc_dt_data imx6sl_dt_data = {
  252. .num_domains = 3,
  253. .err009619_present = false,
  254. .err006287_present = true,
  255. };
  256. static const struct imx_gpc_dt_data imx6sx_dt_data = {
  257. .num_domains = 4,
  258. .err009619_present = false,
  259. .err006287_present = false,
  260. };
  261. static const struct of_device_id imx_gpc_dt_ids[] = {
  262. { .compatible = "fsl,imx6q-gpc", .data = &imx6q_dt_data },
  263. { .compatible = "fsl,imx6qp-gpc", .data = &imx6qp_dt_data },
  264. { .compatible = "fsl,imx6sl-gpc", .data = &imx6sl_dt_data },
  265. { .compatible = "fsl,imx6sx-gpc", .data = &imx6sx_dt_data },
  266. { }
  267. };
  268. static const struct regmap_range yes_ranges[] = {
  269. regmap_reg_range(GPC_CNTR, GPC_CNTR),
  270. regmap_reg_range(GPC_PGC_PCI_PDN, GPC_PGC_PCI_SR),
  271. regmap_reg_range(GPC_PGC_GPU_PDN, GPC_PGC_GPU_SR),
  272. regmap_reg_range(GPC_PGC_DISP_PDN, GPC_PGC_DISP_SR),
  273. };
  274. static const struct regmap_access_table access_table = {
  275. .yes_ranges = yes_ranges,
  276. .n_yes_ranges = ARRAY_SIZE(yes_ranges),
  277. };
  278. static const struct regmap_config imx_gpc_regmap_config = {
  279. .reg_bits = 32,
  280. .val_bits = 32,
  281. .reg_stride = 4,
  282. .rd_table = &access_table,
  283. .wr_table = &access_table,
  284. .max_register = 0x2ac,
  285. };
  286. static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
  287. &imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base,
  288. &imx_gpc_domains[GPC_PGC_DOMAIN_PU].base,
  289. };
  290. static struct genpd_onecell_data imx_gpc_onecell_data = {
  291. .domains = imx_gpc_onecell_domains,
  292. .num_domains = 2,
  293. };
  294. static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
  295. unsigned int num_domains)
  296. {
  297. struct imx_pm_domain *domain;
  298. int i, ret;
  299. for (i = 0; i < num_domains; i++) {
  300. domain = &imx_gpc_domains[i];
  301. domain->regmap = regmap;
  302. domain->ipg_rate_mhz = 66;
  303. if (i == 1) {
  304. domain->supply = devm_regulator_get(dev, "pu");
  305. if (IS_ERR(domain->supply))
  306. return PTR_ERR(domain->supply);
  307. ret = imx_pgc_get_clocks(dev, domain);
  308. if (ret)
  309. goto clk_err;
  310. domain->base.power_on(&domain->base);
  311. }
  312. }
  313. for (i = 0; i < num_domains; i++)
  314. pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
  315. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  316. ret = of_genpd_add_provider_onecell(dev->of_node,
  317. &imx_gpc_onecell_data);
  318. if (ret)
  319. goto genpd_err;
  320. }
  321. return 0;
  322. genpd_err:
  323. for (i = 0; i < num_domains; i++)
  324. pm_genpd_remove(&imx_gpc_domains[i].base);
  325. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  326. clk_err:
  327. return ret;
  328. }
  329. static int imx_gpc_probe(struct platform_device *pdev)
  330. {
  331. const struct of_device_id *of_id =
  332. of_match_device(imx_gpc_dt_ids, &pdev->dev);
  333. const struct imx_gpc_dt_data *of_id_data = of_id->data;
  334. struct device_node *pgc_node;
  335. struct regmap *regmap;
  336. struct resource *res;
  337. void __iomem *base;
  338. int ret;
  339. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  340. /* bail out if DT too old and doesn't provide the necessary info */
  341. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  342. !pgc_node)
  343. return 0;
  344. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  345. base = devm_ioremap_resource(&pdev->dev, res);
  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. /* Disable PU power down in normal operation if ERR009619 is present */
  357. if (of_id_data->err009619_present)
  358. imx_gpc_domains[GPC_PGC_DOMAIN_PU].base.flags |=
  359. GENPD_FLAG_ALWAYS_ON;
  360. /* Keep DISP always on if ERR006287 is present */
  361. if (of_id_data->err006287_present)
  362. imx_gpc_domains[GPC_PGC_DOMAIN_DISPLAY].base.flags |=
  363. GENPD_FLAG_ALWAYS_ON;
  364. if (!pgc_node) {
  365. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  366. of_id_data->num_domains);
  367. if (ret)
  368. return ret;
  369. } else {
  370. struct imx_pm_domain *domain;
  371. struct platform_device *pd_pdev;
  372. struct device_node *np;
  373. struct clk *ipg_clk;
  374. unsigned int ipg_rate_mhz;
  375. int domain_index;
  376. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  377. if (IS_ERR(ipg_clk))
  378. return PTR_ERR(ipg_clk);
  379. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  380. for_each_child_of_node(pgc_node, np) {
  381. ret = of_property_read_u32(np, "reg", &domain_index);
  382. if (ret) {
  383. of_node_put(np);
  384. return ret;
  385. }
  386. if (domain_index >= of_id_data->num_domains)
  387. continue;
  388. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  389. domain_index);
  390. if (!pd_pdev) {
  391. of_node_put(np);
  392. return -ENOMEM;
  393. }
  394. ret = platform_device_add_data(pd_pdev,
  395. &imx_gpc_domains[domain_index],
  396. sizeof(imx_gpc_domains[domain_index]));
  397. if (ret) {
  398. platform_device_put(pd_pdev);
  399. of_node_put(np);
  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. ret = platform_device_add(pd_pdev);
  408. if (ret) {
  409. platform_device_put(pd_pdev);
  410. of_node_put(np);
  411. return ret;
  412. }
  413. }
  414. }
  415. return 0;
  416. }
  417. static int 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_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  424. !pgc_node)
  425. return 0;
  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. return ret;
  435. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  436. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  437. if (ret)
  438. return ret;
  439. }
  440. return 0;
  441. }
  442. static struct platform_driver imx_gpc_driver = {
  443. .driver = {
  444. .name = "imx-gpc",
  445. .of_match_table = imx_gpc_dt_ids,
  446. },
  447. .probe = imx_gpc_probe,
  448. .remove = imx_gpc_remove,
  449. };
  450. builtin_platform_driver(imx_gpc_driver)