meson-ee-pwrc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019 BayLibre, SAS
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. */
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/regmap.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of_device.h>
  13. #include <linux/reset-controller.h>
  14. #include <linux/reset.h>
  15. #include <linux/clk.h>
  16. #include <dt-bindings/power/meson-g12a-power.h>
  17. #include <dt-bindings/power/meson-sm1-power.h>
  18. /* AO Offsets */
  19. #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
  20. #define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
  21. /* HHI Offsets */
  22. #define HHI_MEM_PD_REG0 (0x40 << 2)
  23. #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
  24. #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
  25. #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
  26. #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
  27. #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
  28. #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
  29. #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
  30. #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
  31. struct meson_ee_pwrc;
  32. struct meson_ee_pwrc_domain;
  33. struct meson_ee_pwrc_mem_domain {
  34. unsigned int reg;
  35. unsigned int mask;
  36. };
  37. struct meson_ee_pwrc_top_domain {
  38. unsigned int sleep_reg;
  39. unsigned int sleep_mask;
  40. unsigned int iso_reg;
  41. unsigned int iso_mask;
  42. };
  43. struct meson_ee_pwrc_domain_desc {
  44. char *name;
  45. unsigned int reset_names_count;
  46. unsigned int clk_names_count;
  47. struct meson_ee_pwrc_top_domain *top_pd;
  48. unsigned int mem_pd_count;
  49. struct meson_ee_pwrc_mem_domain *mem_pd;
  50. bool (*get_power)(struct meson_ee_pwrc_domain *pwrc_domain);
  51. };
  52. struct meson_ee_pwrc_domain_data {
  53. unsigned int count;
  54. struct meson_ee_pwrc_domain_desc *domains;
  55. };
  56. /* TOP Power Domains */
  57. static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu = {
  58. .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
  59. .sleep_mask = BIT(8),
  60. .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
  61. .iso_mask = BIT(9),
  62. };
  63. #define SM1_EE_PD(__bit) \
  64. { \
  65. .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
  66. .sleep_mask = BIT(__bit), \
  67. .iso_reg = AO_RTI_GEN_PWR_ISO0, \
  68. .iso_mask = BIT(__bit), \
  69. }
  70. static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
  71. static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
  72. static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
  73. static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
  74. static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
  75. /* Memory PD Domains */
  76. #define VPU_MEMPD(__reg) \
  77. { __reg, GENMASK(1, 0) }, \
  78. { __reg, GENMASK(3, 2) }, \
  79. { __reg, GENMASK(5, 4) }, \
  80. { __reg, GENMASK(7, 6) }, \
  81. { __reg, GENMASK(9, 8) }, \
  82. { __reg, GENMASK(11, 10) }, \
  83. { __reg, GENMASK(13, 12) }, \
  84. { __reg, GENMASK(15, 14) }, \
  85. { __reg, GENMASK(17, 16) }, \
  86. { __reg, GENMASK(19, 18) }, \
  87. { __reg, GENMASK(21, 20) }, \
  88. { __reg, GENMASK(23, 22) }, \
  89. { __reg, GENMASK(25, 24) }, \
  90. { __reg, GENMASK(27, 26) }, \
  91. { __reg, GENMASK(29, 28) }, \
  92. { __reg, GENMASK(31, 30) }
  93. #define VPU_HHI_MEMPD(__reg) \
  94. { __reg, BIT(8) }, \
  95. { __reg, BIT(9) }, \
  96. { __reg, BIT(10) }, \
  97. { __reg, BIT(11) }, \
  98. { __reg, BIT(12) }, \
  99. { __reg, BIT(13) }, \
  100. { __reg, BIT(14) }, \
  101. { __reg, BIT(15) }
  102. static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
  103. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  104. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  105. VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
  106. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  107. };
  108. static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
  109. { HHI_MEM_PD_REG0, GENMASK(3, 2) },
  110. };
  111. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
  112. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  113. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  114. VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
  115. VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
  116. { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
  117. { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
  118. { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
  119. { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
  120. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  121. };
  122. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
  123. { HHI_NANOQ_MEM_PD_REG0, 0xff },
  124. { HHI_NANOQ_MEM_PD_REG1, 0xff },
  125. };
  126. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
  127. { HHI_MEM_PD_REG0, GENMASK(31, 30) },
  128. };
  129. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
  130. { HHI_MEM_PD_REG0, GENMASK(29, 26) },
  131. };
  132. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
  133. { HHI_MEM_PD_REG0, GENMASK(25, 18) },
  134. };
  135. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
  136. { HHI_MEM_PD_REG0, GENMASK(5, 4) },
  137. { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
  138. { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
  139. { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
  140. { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
  141. { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
  142. { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
  143. { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
  144. { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
  145. { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
  146. { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
  147. { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
  148. { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
  149. };
  150. #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
  151. { \
  152. .name = __name, \
  153. .reset_names_count = __resets, \
  154. .clk_names_count = __clks, \
  155. .top_pd = __top_pd, \
  156. .mem_pd_count = ARRAY_SIZE(__mem), \
  157. .mem_pd = __mem, \
  158. .get_power = __get_power, \
  159. }
  160. #define TOP_PD(__name, __top_pd, __mem, __get_power) \
  161. { \
  162. .name = __name, \
  163. .top_pd = __top_pd, \
  164. .mem_pd_count = ARRAY_SIZE(__mem), \
  165. .mem_pd = __mem, \
  166. .get_power = __get_power, \
  167. }
  168. #define MEM_PD(__name, __mem) \
  169. TOP_PD(__name, NULL, __mem, NULL)
  170. static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain *pwrc_domain);
  171. static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
  172. [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &g12a_pwrc_vpu, g12a_pwrc_mem_vpu,
  173. pwrc_ee_get_power, 11, 2),
  174. [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
  175. };
  176. static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
  177. [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
  178. pwrc_ee_get_power, 11, 2),
  179. [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
  180. pwrc_ee_get_power),
  181. [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
  182. pwrc_ee_get_power),
  183. [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
  184. pwrc_ee_get_power),
  185. [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
  186. pwrc_ee_get_power),
  187. [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
  188. [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
  189. };
  190. struct meson_ee_pwrc_domain {
  191. struct generic_pm_domain base;
  192. bool enabled;
  193. struct meson_ee_pwrc *pwrc;
  194. struct meson_ee_pwrc_domain_desc desc;
  195. struct clk_bulk_data *clks;
  196. int num_clks;
  197. struct reset_control *rstc;
  198. int num_rstc;
  199. };
  200. struct meson_ee_pwrc {
  201. struct regmap *regmap_ao;
  202. struct regmap *regmap_hhi;
  203. struct meson_ee_pwrc_domain *domains;
  204. struct genpd_onecell_data xlate;
  205. };
  206. static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain *pwrc_domain)
  207. {
  208. u32 reg;
  209. regmap_read(pwrc_domain->pwrc->regmap_ao,
  210. pwrc_domain->desc.top_pd->sleep_reg, &reg);
  211. return (reg & pwrc_domain->desc.top_pd->sleep_mask);
  212. }
  213. static int meson_ee_pwrc_off(struct generic_pm_domain *domain)
  214. {
  215. struct meson_ee_pwrc_domain *pwrc_domain =
  216. container_of(domain, struct meson_ee_pwrc_domain, base);
  217. int i;
  218. if (pwrc_domain->desc.top_pd)
  219. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  220. pwrc_domain->desc.top_pd->sleep_reg,
  221. pwrc_domain->desc.top_pd->sleep_mask,
  222. pwrc_domain->desc.top_pd->sleep_mask);
  223. udelay(20);
  224. for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
  225. regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
  226. pwrc_domain->desc.mem_pd[i].reg,
  227. pwrc_domain->desc.mem_pd[i].mask,
  228. pwrc_domain->desc.mem_pd[i].mask);
  229. udelay(20);
  230. if (pwrc_domain->desc.top_pd)
  231. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  232. pwrc_domain->desc.top_pd->iso_reg,
  233. pwrc_domain->desc.top_pd->iso_mask,
  234. pwrc_domain->desc.top_pd->iso_mask);
  235. if (pwrc_domain->num_clks) {
  236. msleep(20);
  237. clk_bulk_disable_unprepare(pwrc_domain->num_clks,
  238. pwrc_domain->clks);
  239. }
  240. return 0;
  241. }
  242. static int meson_ee_pwrc_on(struct generic_pm_domain *domain)
  243. {
  244. struct meson_ee_pwrc_domain *pwrc_domain =
  245. container_of(domain, struct meson_ee_pwrc_domain, base);
  246. int i, ret;
  247. if (pwrc_domain->desc.top_pd)
  248. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  249. pwrc_domain->desc.top_pd->sleep_reg,
  250. pwrc_domain->desc.top_pd->sleep_mask, 0);
  251. udelay(20);
  252. for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
  253. regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
  254. pwrc_domain->desc.mem_pd[i].reg,
  255. pwrc_domain->desc.mem_pd[i].mask, 0);
  256. udelay(20);
  257. ret = reset_control_assert(pwrc_domain->rstc);
  258. if (ret)
  259. return ret;
  260. if (pwrc_domain->desc.top_pd)
  261. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  262. pwrc_domain->desc.top_pd->iso_reg,
  263. pwrc_domain->desc.top_pd->iso_mask, 0);
  264. ret = reset_control_deassert(pwrc_domain->rstc);
  265. if (ret)
  266. return ret;
  267. return clk_bulk_prepare_enable(pwrc_domain->num_clks,
  268. pwrc_domain->clks);
  269. }
  270. static int meson_ee_pwrc_init_domain(struct platform_device *pdev,
  271. struct meson_ee_pwrc *pwrc,
  272. struct meson_ee_pwrc_domain *dom)
  273. {
  274. int ret;
  275. dom->pwrc = pwrc;
  276. dom->num_rstc = dom->desc.reset_names_count;
  277. dom->num_clks = dom->desc.clk_names_count;
  278. if (dom->num_rstc) {
  279. int count = reset_control_get_count(&pdev->dev);
  280. if (count != dom->num_rstc)
  281. dev_warn(&pdev->dev, "Invalid resets count %d for domain %s\n",
  282. count, dom->desc.name);
  283. dom->rstc = devm_reset_control_array_get(&pdev->dev, false,
  284. false);
  285. if (IS_ERR(dom->rstc))
  286. return PTR_ERR(dom->rstc);
  287. }
  288. if (dom->num_clks) {
  289. int ret = devm_clk_bulk_get_all(&pdev->dev, &dom->clks);
  290. if (ret < 0)
  291. return ret;
  292. if (dom->num_clks != ret) {
  293. dev_warn(&pdev->dev, "Invalid clocks count %d for domain %s\n",
  294. ret, dom->desc.name);
  295. dom->num_clks = ret;
  296. }
  297. }
  298. dom->base.name = dom->desc.name;
  299. dom->base.power_on = meson_ee_pwrc_on;
  300. dom->base.power_off = meson_ee_pwrc_off;
  301. /*
  302. * TOFIX: This is a special case for the VPU power domain, which can
  303. * be enabled previously by the bootloader. In this case the VPU
  304. * pipeline may be functional but no driver maybe never attach
  305. * to this power domain, and if the domain is disabled it could
  306. * cause system errors. This is why the pm_domain_always_on_gov
  307. * is used here.
  308. * For the same reason, the clocks should be enabled in case
  309. * we need to power the domain off, otherwise the internal clocks
  310. * prepare/enable counters won't be in sync.
  311. */
  312. if (dom->num_clks && dom->desc.get_power && !dom->desc.get_power(dom)) {
  313. ret = clk_bulk_prepare_enable(dom->num_clks, dom->clks);
  314. if (ret)
  315. return ret;
  316. ret = pm_genpd_init(&dom->base, &pm_domain_always_on_gov,
  317. false);
  318. if (ret)
  319. return ret;
  320. } else {
  321. ret = pm_genpd_init(&dom->base, NULL,
  322. (dom->desc.get_power ?
  323. dom->desc.get_power(dom) : true));
  324. if (ret)
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. static int meson_ee_pwrc_probe(struct platform_device *pdev)
  330. {
  331. const struct meson_ee_pwrc_domain_data *match;
  332. struct regmap *regmap_ao, *regmap_hhi;
  333. struct meson_ee_pwrc *pwrc;
  334. int i, ret;
  335. match = of_device_get_match_data(&pdev->dev);
  336. if (!match) {
  337. dev_err(&pdev->dev, "failed to get match data\n");
  338. return -ENODEV;
  339. }
  340. pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
  341. if (!pwrc)
  342. return -ENOMEM;
  343. pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
  344. sizeof(*pwrc->xlate.domains),
  345. GFP_KERNEL);
  346. if (!pwrc->xlate.domains)
  347. return -ENOMEM;
  348. pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
  349. sizeof(*pwrc->domains), GFP_KERNEL);
  350. if (!pwrc->domains)
  351. return -ENOMEM;
  352. pwrc->xlate.num_domains = match->count;
  353. regmap_hhi = syscon_node_to_regmap(of_get_parent(pdev->dev.of_node));
  354. if (IS_ERR(regmap_hhi)) {
  355. dev_err(&pdev->dev, "failed to get HHI regmap\n");
  356. return PTR_ERR(regmap_hhi);
  357. }
  358. regmap_ao = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  359. "amlogic,ao-sysctrl");
  360. if (IS_ERR(regmap_ao)) {
  361. dev_err(&pdev->dev, "failed to get AO regmap\n");
  362. return PTR_ERR(regmap_ao);
  363. }
  364. pwrc->regmap_ao = regmap_ao;
  365. pwrc->regmap_hhi = regmap_hhi;
  366. platform_set_drvdata(pdev, pwrc);
  367. for (i = 0 ; i < match->count ; ++i) {
  368. struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
  369. memcpy(&dom->desc, &match->domains[i], sizeof(dom->desc));
  370. ret = meson_ee_pwrc_init_domain(pdev, pwrc, dom);
  371. if (ret)
  372. return ret;
  373. pwrc->xlate.domains[i] = &dom->base;
  374. }
  375. return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
  376. }
  377. static void meson_ee_pwrc_shutdown(struct platform_device *pdev)
  378. {
  379. struct meson_ee_pwrc *pwrc = platform_get_drvdata(pdev);
  380. int i;
  381. for (i = 0 ; i < pwrc->xlate.num_domains ; ++i) {
  382. struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
  383. if (dom->desc.get_power && !dom->desc.get_power(dom))
  384. meson_ee_pwrc_off(&dom->base);
  385. }
  386. }
  387. static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
  388. .count = ARRAY_SIZE(g12a_pwrc_domains),
  389. .domains = g12a_pwrc_domains,
  390. };
  391. static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
  392. .count = ARRAY_SIZE(sm1_pwrc_domains),
  393. .domains = sm1_pwrc_domains,
  394. };
  395. static const struct of_device_id meson_ee_pwrc_match_table[] = {
  396. {
  397. .compatible = "amlogic,meson-g12a-pwrc",
  398. .data = &meson_ee_g12a_pwrc_data,
  399. },
  400. {
  401. .compatible = "amlogic,meson-sm1-pwrc",
  402. .data = &meson_ee_sm1_pwrc_data,
  403. },
  404. { /* sentinel */ }
  405. };
  406. static struct platform_driver meson_ee_pwrc_driver = {
  407. .probe = meson_ee_pwrc_probe,
  408. .shutdown = meson_ee_pwrc_shutdown,
  409. .driver = {
  410. .name = "meson_ee_pwrc",
  411. .of_match_table = meson_ee_pwrc_match_table,
  412. },
  413. };
  414. builtin_platform_driver(meson_ee_pwrc_driver);