pm33xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AM33XX Power Management Routines
  4. *
  5. * Copyright (C) 2012-2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Vaibhav Bedia, Dave Gerlach
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/cpu.h>
  10. #include <linux/err.h>
  11. #include <linux/genalloc.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/nvmem-consumer.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_data/pm33xx.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/rtc.h>
  23. #include <linux/rtc/rtc-omap.h>
  24. #include <linux/sizes.h>
  25. #include <linux/sram.h>
  26. #include <linux/suspend.h>
  27. #include <linux/ti-emif-sram.h>
  28. #include <linux/wkup_m3_ipc.h>
  29. #include <asm/proc-fns.h>
  30. #include <asm/suspend.h>
  31. #include <asm/system_misc.h>
  32. #define AMX3_PM_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
  33. (unsigned long)pm_sram->do_wfi)
  34. #define RTC_SCRATCH_RESUME_REG 0
  35. #define RTC_SCRATCH_MAGIC_REG 1
  36. #define RTC_REG_BOOT_MAGIC 0x8cd0 /* RTC */
  37. #define GIC_INT_SET_PENDING_BASE 0x200
  38. #define AM43XX_GIC_DIST_BASE 0x48241000
  39. static void __iomem *rtc_base_virt;
  40. static struct clk *rtc_fck;
  41. static u32 rtc_magic_val;
  42. static int (*am33xx_do_wfi_sram)(unsigned long unused);
  43. static phys_addr_t am33xx_do_wfi_sram_phys;
  44. static struct gen_pool *sram_pool, *sram_pool_data;
  45. static unsigned long ocmcram_location, ocmcram_location_data;
  46. static struct rtc_device *omap_rtc;
  47. static void __iomem *gic_dist_base;
  48. static struct am33xx_pm_platform_data *pm_ops;
  49. static struct am33xx_pm_sram_addr *pm_sram;
  50. static struct device *pm33xx_dev;
  51. static struct wkup_m3_ipc *m3_ipc;
  52. #ifdef CONFIG_SUSPEND
  53. static int rtc_only_idle;
  54. static int retrigger_irq;
  55. static unsigned long suspend_wfi_flags;
  56. static struct wkup_m3_wakeup_src wakeup_src = {.irq_nr = 0,
  57. .src = "Unknown",
  58. };
  59. static struct wkup_m3_wakeup_src rtc_alarm_wakeup = {
  60. .irq_nr = 108, .src = "RTC Alarm",
  61. };
  62. static struct wkup_m3_wakeup_src rtc_ext_wakeup = {
  63. .irq_nr = 0, .src = "Ext wakeup",
  64. };
  65. #endif
  66. static u32 sram_suspend_address(unsigned long addr)
  67. {
  68. return ((unsigned long)am33xx_do_wfi_sram +
  69. AMX3_PM_SRAM_SYMBOL_OFFSET(addr));
  70. }
  71. static int am33xx_push_sram_idle(void)
  72. {
  73. struct am33xx_pm_ro_sram_data ro_sram_data;
  74. int ret;
  75. u32 table_addr, ro_data_addr;
  76. void *copy_addr;
  77. ro_sram_data.amx3_pm_sram_data_virt = ocmcram_location_data;
  78. ro_sram_data.amx3_pm_sram_data_phys =
  79. gen_pool_virt_to_phys(sram_pool_data, ocmcram_location_data);
  80. ro_sram_data.rtc_base_virt = rtc_base_virt;
  81. /* Save physical address to calculate resume offset during pm init */
  82. am33xx_do_wfi_sram_phys = gen_pool_virt_to_phys(sram_pool,
  83. ocmcram_location);
  84. am33xx_do_wfi_sram = sram_exec_copy(sram_pool, (void *)ocmcram_location,
  85. pm_sram->do_wfi,
  86. *pm_sram->do_wfi_sz);
  87. if (!am33xx_do_wfi_sram) {
  88. dev_err(pm33xx_dev,
  89. "PM: %s: am33xx_do_wfi copy to sram failed\n",
  90. __func__);
  91. return -ENODEV;
  92. }
  93. table_addr =
  94. sram_suspend_address((unsigned long)pm_sram->emif_sram_table);
  95. ret = ti_emif_copy_pm_function_table(sram_pool, (void *)table_addr);
  96. if (ret) {
  97. dev_dbg(pm33xx_dev,
  98. "PM: %s: EMIF function copy failed\n", __func__);
  99. return -EPROBE_DEFER;
  100. }
  101. ro_data_addr =
  102. sram_suspend_address((unsigned long)pm_sram->ro_sram_data);
  103. copy_addr = sram_exec_copy(sram_pool, (void *)ro_data_addr,
  104. &ro_sram_data,
  105. sizeof(ro_sram_data));
  106. if (!copy_addr) {
  107. dev_err(pm33xx_dev,
  108. "PM: %s: ro_sram_data copy to sram failed\n",
  109. __func__);
  110. return -ENODEV;
  111. }
  112. return 0;
  113. }
  114. static int am33xx_do_sram_idle(u32 wfi_flags)
  115. {
  116. if (!m3_ipc || !pm_ops)
  117. return 0;
  118. if (wfi_flags & WFI_FLAG_WAKE_M3)
  119. m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_IDLE);
  120. return pm_ops->cpu_suspend(am33xx_do_wfi_sram, wfi_flags);
  121. }
  122. static int __init am43xx_map_gic(void)
  123. {
  124. gic_dist_base = ioremap(AM43XX_GIC_DIST_BASE, SZ_4K);
  125. if (!gic_dist_base)
  126. return -ENOMEM;
  127. return 0;
  128. }
  129. #ifdef CONFIG_SUSPEND
  130. static struct wkup_m3_wakeup_src rtc_wake_src(void)
  131. {
  132. u32 i;
  133. i = __raw_readl(rtc_base_virt + 0x44) & 0x40;
  134. if (i) {
  135. retrigger_irq = rtc_alarm_wakeup.irq_nr;
  136. return rtc_alarm_wakeup;
  137. }
  138. retrigger_irq = rtc_ext_wakeup.irq_nr;
  139. return rtc_ext_wakeup;
  140. }
  141. static int am33xx_rtc_only_idle(unsigned long wfi_flags)
  142. {
  143. omap_rtc_power_off_program(&omap_rtc->dev);
  144. am33xx_do_wfi_sram(wfi_flags);
  145. return 0;
  146. }
  147. /*
  148. * Note that the RTC module clock must be re-enabled only for rtc+ddr suspend.
  149. * And looks like the module can stay in SYSC_IDLE_SMART_WKUP mode configured
  150. * by the interconnect code just fine for both rtc+ddr suspend and retention
  151. * suspend.
  152. */
  153. static int am33xx_pm_suspend(suspend_state_t suspend_state)
  154. {
  155. int i, ret = 0;
  156. if (suspend_state == PM_SUSPEND_MEM &&
  157. pm_ops->check_off_mode_enable()) {
  158. ret = clk_prepare_enable(rtc_fck);
  159. if (ret) {
  160. dev_err(pm33xx_dev, "Failed to enable clock: %i\n", ret);
  161. return ret;
  162. }
  163. pm_ops->save_context();
  164. suspend_wfi_flags |= WFI_FLAG_RTC_ONLY;
  165. clk_save_context();
  166. ret = pm_ops->soc_suspend(suspend_state, am33xx_rtc_only_idle,
  167. suspend_wfi_flags);
  168. suspend_wfi_flags &= ~WFI_FLAG_RTC_ONLY;
  169. dev_info(pm33xx_dev, "Entering RTC Only mode with DDR in self-refresh\n");
  170. if (!ret) {
  171. clk_restore_context();
  172. pm_ops->restore_context();
  173. m3_ipc->ops->set_rtc_only(m3_ipc);
  174. am33xx_push_sram_idle();
  175. }
  176. } else {
  177. ret = pm_ops->soc_suspend(suspend_state, am33xx_do_wfi_sram,
  178. suspend_wfi_flags);
  179. }
  180. if (ret) {
  181. dev_err(pm33xx_dev, "PM: Kernel suspend failure\n");
  182. } else {
  183. i = m3_ipc->ops->request_pm_status(m3_ipc);
  184. switch (i) {
  185. case 0:
  186. dev_info(pm33xx_dev,
  187. "PM: Successfully put all powerdomains to target state\n");
  188. break;
  189. case 1:
  190. dev_err(pm33xx_dev,
  191. "PM: Could not transition all powerdomains to target state\n");
  192. ret = -1;
  193. break;
  194. default:
  195. dev_err(pm33xx_dev,
  196. "PM: CM3 returned unknown result = %d\n", i);
  197. ret = -1;
  198. }
  199. /* print the wakeup reason */
  200. if (rtc_only_idle) {
  201. wakeup_src = rtc_wake_src();
  202. pr_info("PM: Wakeup source %s\n", wakeup_src.src);
  203. } else {
  204. pr_info("PM: Wakeup source %s\n",
  205. m3_ipc->ops->request_wake_src(m3_ipc));
  206. }
  207. }
  208. if (suspend_state == PM_SUSPEND_MEM && pm_ops->check_off_mode_enable())
  209. clk_disable_unprepare(rtc_fck);
  210. return ret;
  211. }
  212. static int am33xx_pm_enter(suspend_state_t suspend_state)
  213. {
  214. int ret = 0;
  215. switch (suspend_state) {
  216. case PM_SUSPEND_MEM:
  217. case PM_SUSPEND_STANDBY:
  218. ret = am33xx_pm_suspend(suspend_state);
  219. break;
  220. default:
  221. ret = -EINVAL;
  222. }
  223. return ret;
  224. }
  225. static int am33xx_pm_begin(suspend_state_t state)
  226. {
  227. int ret = -EINVAL;
  228. struct nvmem_device *nvmem;
  229. if (state == PM_SUSPEND_MEM && pm_ops->check_off_mode_enable()) {
  230. nvmem = devm_nvmem_device_get(&omap_rtc->dev,
  231. "omap_rtc_scratch0");
  232. if (!IS_ERR(nvmem))
  233. nvmem_device_write(nvmem, RTC_SCRATCH_MAGIC_REG * 4, 4,
  234. (void *)&rtc_magic_val);
  235. rtc_only_idle = 1;
  236. } else {
  237. rtc_only_idle = 0;
  238. }
  239. pm_ops->begin_suspend();
  240. switch (state) {
  241. case PM_SUSPEND_MEM:
  242. ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_DEEPSLEEP);
  243. break;
  244. case PM_SUSPEND_STANDBY:
  245. ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_STANDBY);
  246. break;
  247. }
  248. return ret;
  249. }
  250. static void am33xx_pm_end(void)
  251. {
  252. u32 val = 0;
  253. struct nvmem_device *nvmem;
  254. nvmem = devm_nvmem_device_get(&omap_rtc->dev, "omap_rtc_scratch0");
  255. if (IS_ERR(nvmem))
  256. return;
  257. m3_ipc->ops->finish_low_power(m3_ipc);
  258. if (rtc_only_idle) {
  259. if (retrigger_irq) {
  260. /*
  261. * 32 bits of Interrupt Set-Pending correspond to 32
  262. * 32 interrupts. Compute the bit offset of the
  263. * Interrupt and set that particular bit
  264. * Compute the register offset by dividing interrupt
  265. * number by 32 and mutiplying by 4
  266. */
  267. writel_relaxed(1 << (retrigger_irq & 31),
  268. gic_dist_base + GIC_INT_SET_PENDING_BASE
  269. + retrigger_irq / 32 * 4);
  270. }
  271. nvmem_device_write(nvmem, RTC_SCRATCH_MAGIC_REG * 4, 4,
  272. (void *)&val);
  273. }
  274. rtc_only_idle = 0;
  275. pm_ops->finish_suspend();
  276. }
  277. static int am33xx_pm_valid(suspend_state_t state)
  278. {
  279. switch (state) {
  280. case PM_SUSPEND_STANDBY:
  281. case PM_SUSPEND_MEM:
  282. return 1;
  283. default:
  284. return 0;
  285. }
  286. }
  287. static const struct platform_suspend_ops am33xx_pm_ops = {
  288. .begin = am33xx_pm_begin,
  289. .end = am33xx_pm_end,
  290. .enter = am33xx_pm_enter,
  291. .valid = am33xx_pm_valid,
  292. };
  293. #endif /* CONFIG_SUSPEND */
  294. static void am33xx_pm_set_ipc_ops(void)
  295. {
  296. u32 resume_address;
  297. int temp;
  298. temp = ti_emif_get_mem_type();
  299. if (temp < 0) {
  300. dev_err(pm33xx_dev, "PM: Cannot determine memory type, no PM available\n");
  301. return;
  302. }
  303. m3_ipc->ops->set_mem_type(m3_ipc, temp);
  304. /* Physical resume address to be used by ROM code */
  305. resume_address = am33xx_do_wfi_sram_phys +
  306. *pm_sram->resume_offset + 0x4;
  307. m3_ipc->ops->set_resume_address(m3_ipc, (void *)resume_address);
  308. }
  309. static void am33xx_pm_free_sram(void)
  310. {
  311. gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
  312. gen_pool_free(sram_pool_data, ocmcram_location_data,
  313. sizeof(struct am33xx_pm_ro_sram_data));
  314. }
  315. /*
  316. * Push the minimal suspend-resume code to SRAM
  317. */
  318. static int am33xx_pm_alloc_sram(void)
  319. {
  320. struct device_node *np __free(device_node) =
  321. of_find_compatible_node(NULL, NULL, "ti,omap3-mpu");
  322. if (!np) {
  323. np = of_find_compatible_node(NULL, NULL, "ti,omap4-mpu");
  324. if (!np)
  325. return dev_err_probe(pm33xx_dev, -ENODEV,
  326. "PM: %s: Unable to find device node for mpu\n",
  327. __func__);
  328. }
  329. sram_pool = of_gen_pool_get(np, "pm-sram", 0);
  330. if (!sram_pool)
  331. return dev_err_probe(pm33xx_dev, -ENODEV,
  332. "PM: %s: Unable to get sram pool for ocmcram\n",
  333. __func__);
  334. sram_pool_data = of_gen_pool_get(np, "pm-sram", 1);
  335. if (!sram_pool_data)
  336. return dev_err_probe(pm33xx_dev, -ENODEV,
  337. "PM: %s: Unable to get sram data pool for ocmcram\n",
  338. __func__);
  339. ocmcram_location = gen_pool_alloc(sram_pool, *pm_sram->do_wfi_sz);
  340. if (!ocmcram_location)
  341. return dev_err_probe(pm33xx_dev, -ENOMEM,
  342. "PM: %s: Unable to allocate memory from ocmcram\n",
  343. __func__);
  344. ocmcram_location_data = gen_pool_alloc(sram_pool_data,
  345. sizeof(struct emif_regs_amx3));
  346. if (!ocmcram_location_data) {
  347. gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
  348. return dev_err_probe(pm33xx_dev, -ENOMEM,
  349. "PM: Unable to allocate memory from ocmcram\n");
  350. }
  351. return 0;
  352. }
  353. static int am33xx_pm_rtc_setup(void)
  354. {
  355. struct device_node *np;
  356. unsigned long val = 0;
  357. struct nvmem_device *nvmem;
  358. int error;
  359. np = of_find_node_by_name(NULL, "rtc");
  360. if (of_device_is_available(np)) {
  361. /* RTC interconnect target module clock */
  362. rtc_fck = of_clk_get_by_name(np->parent, "fck");
  363. if (IS_ERR(rtc_fck))
  364. return PTR_ERR(rtc_fck);
  365. rtc_base_virt = of_iomap(np, 0);
  366. if (!rtc_base_virt) {
  367. pr_warn("PM: could not iomap rtc\n");
  368. error = -ENODEV;
  369. goto err_clk_put;
  370. }
  371. omap_rtc = rtc_class_open("rtc0");
  372. if (!omap_rtc) {
  373. pr_warn("PM: rtc0 not available\n");
  374. error = -EPROBE_DEFER;
  375. goto err_iounmap;
  376. }
  377. nvmem = devm_nvmem_device_get(&omap_rtc->dev,
  378. "omap_rtc_scratch0");
  379. if (!IS_ERR(nvmem)) {
  380. nvmem_device_read(nvmem, RTC_SCRATCH_MAGIC_REG * 4,
  381. 4, (void *)&rtc_magic_val);
  382. if ((rtc_magic_val & 0xffff) != RTC_REG_BOOT_MAGIC)
  383. pr_warn("PM: bootloader does not support rtc-only!\n");
  384. nvmem_device_write(nvmem, RTC_SCRATCH_MAGIC_REG * 4,
  385. 4, (void *)&val);
  386. val = pm_sram->resume_address;
  387. nvmem_device_write(nvmem, RTC_SCRATCH_RESUME_REG * 4,
  388. 4, (void *)&val);
  389. }
  390. } else {
  391. pr_warn("PM: no-rtc available, rtc-only mode disabled.\n");
  392. }
  393. return 0;
  394. err_iounmap:
  395. iounmap(rtc_base_virt);
  396. err_clk_put:
  397. clk_put(rtc_fck);
  398. return error;
  399. }
  400. static int am33xx_pm_probe(struct platform_device *pdev)
  401. {
  402. struct device *dev = &pdev->dev;
  403. int ret;
  404. if (!of_machine_is_compatible("ti,am33xx") &&
  405. !of_machine_is_compatible("ti,am43"))
  406. return -ENODEV;
  407. pm_ops = dev->platform_data;
  408. if (!pm_ops) {
  409. dev_err(dev, "PM: Cannot get core PM ops!\n");
  410. return -ENODEV;
  411. }
  412. ret = am43xx_map_gic();
  413. if (ret) {
  414. pr_err("PM: Could not ioremap GIC base\n");
  415. return ret;
  416. }
  417. pm_sram = pm_ops->get_sram_addrs();
  418. if (!pm_sram) {
  419. dev_err(dev, "PM: Cannot get PM asm function addresses!!\n");
  420. return -ENODEV;
  421. }
  422. m3_ipc = wkup_m3_ipc_get();
  423. if (!m3_ipc) {
  424. pr_err("PM: Cannot get wkup_m3_ipc handle\n");
  425. return -EPROBE_DEFER;
  426. }
  427. pm33xx_dev = dev;
  428. ret = am33xx_pm_alloc_sram();
  429. if (ret)
  430. goto err_wkup_m3_ipc_put;
  431. ret = am33xx_pm_rtc_setup();
  432. if (ret)
  433. goto err_free_sram;
  434. ret = am33xx_push_sram_idle();
  435. if (ret)
  436. goto err_unsetup_rtc;
  437. am33xx_pm_set_ipc_ops();
  438. #ifdef CONFIG_SUSPEND
  439. suspend_set_ops(&am33xx_pm_ops);
  440. /*
  441. * For a system suspend we must flush the caches, we want
  442. * the DDR in self-refresh, we want to save the context
  443. * of the EMIF, and we want the wkup_m3 to handle low-power
  444. * transition.
  445. */
  446. suspend_wfi_flags |= WFI_FLAG_FLUSH_CACHE;
  447. suspend_wfi_flags |= WFI_FLAG_SELF_REFRESH;
  448. suspend_wfi_flags |= WFI_FLAG_SAVE_EMIF;
  449. suspend_wfi_flags |= WFI_FLAG_WAKE_M3;
  450. #endif /* CONFIG_SUSPEND */
  451. pm_runtime_enable(dev);
  452. ret = pm_runtime_resume_and_get(dev);
  453. if (ret < 0)
  454. goto err_pm_runtime_disable;
  455. ret = pm_ops->init(am33xx_do_sram_idle);
  456. if (ret) {
  457. dev_err(dev, "Unable to call core pm init!\n");
  458. ret = -ENODEV;
  459. goto err_pm_runtime_put;
  460. }
  461. return 0;
  462. err_pm_runtime_put:
  463. pm_runtime_put_sync(dev);
  464. err_pm_runtime_disable:
  465. pm_runtime_disable(dev);
  466. err_unsetup_rtc:
  467. iounmap(rtc_base_virt);
  468. clk_put(rtc_fck);
  469. err_free_sram:
  470. am33xx_pm_free_sram();
  471. pm33xx_dev = NULL;
  472. err_wkup_m3_ipc_put:
  473. wkup_m3_ipc_put(m3_ipc);
  474. return ret;
  475. }
  476. static void am33xx_pm_remove(struct platform_device *pdev)
  477. {
  478. pm_runtime_put_sync(&pdev->dev);
  479. pm_runtime_disable(&pdev->dev);
  480. if (pm_ops->deinit)
  481. pm_ops->deinit();
  482. suspend_set_ops(NULL);
  483. wkup_m3_ipc_put(m3_ipc);
  484. am33xx_pm_free_sram();
  485. iounmap(rtc_base_virt);
  486. clk_put(rtc_fck);
  487. }
  488. static struct platform_driver am33xx_pm_driver = {
  489. .driver = {
  490. .name = "pm33xx",
  491. },
  492. .probe = am33xx_pm_probe,
  493. .remove_new = am33xx_pm_remove,
  494. };
  495. module_platform_driver(am33xx_pm_driver);
  496. MODULE_ALIAS("platform:pm33xx");
  497. MODULE_LICENSE("GPL v2");
  498. MODULE_DESCRIPTION("am33xx power management driver");