gpc.c 13 KB

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