gpc.c 13 KB

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