pmsu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Power Management Service Unit(PMSU) support for Armada 370/XP platforms.
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Yehuda Yitschak <yehuday@marvell.com>
  7. * Gregory Clement <gregory.clement@free-electrons.com>
  8. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * The Armada 370 and Armada XP SOCs have a power management service
  15. * unit which is responsible for powering down and waking up CPUs and
  16. * other SOC units
  17. */
  18. #define pr_fmt(fmt) "mvebu-pmsu: " fmt
  19. #include <linux/clk.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mbus.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_opp.h>
  30. #include <linux/resource.h>
  31. #include <linux/slab.h>
  32. #include <linux/smp.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/cp15.h>
  35. #include <asm/smp_scu.h>
  36. #include <asm/smp_plat.h>
  37. #include <asm/suspend.h>
  38. #include <asm/tlbflush.h>
  39. #include "common.h"
  40. #include "armada-370-xp.h"
  41. #define PMSU_BASE_OFFSET 0x100
  42. #define PMSU_REG_SIZE 0x1000
  43. /* PMSU MP registers */
  44. #define PMSU_CONTROL_AND_CONFIG(cpu) ((cpu * 0x100) + 0x104)
  45. #define PMSU_CONTROL_AND_CONFIG_DFS_REQ BIT(18)
  46. #define PMSU_CONTROL_AND_CONFIG_PWDDN_REQ BIT(16)
  47. #define PMSU_CONTROL_AND_CONFIG_L2_PWDDN BIT(20)
  48. #define PMSU_CPU_POWER_DOWN_CONTROL(cpu) ((cpu * 0x100) + 0x108)
  49. #define PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP BIT(0)
  50. #define PMSU_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x10c)
  51. #define PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT BIT(16)
  52. #define PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT BIT(17)
  53. #define PMSU_STATUS_AND_MASK_IRQ_WAKEUP BIT(20)
  54. #define PMSU_STATUS_AND_MASK_FIQ_WAKEUP BIT(21)
  55. #define PMSU_STATUS_AND_MASK_DBG_WAKEUP BIT(22)
  56. #define PMSU_STATUS_AND_MASK_IRQ_MASK BIT(24)
  57. #define PMSU_STATUS_AND_MASK_FIQ_MASK BIT(25)
  58. #define PMSU_EVENT_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x120)
  59. #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE BIT(1)
  60. #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK BIT(17)
  61. #define PMSU_BOOT_ADDR_REDIRECT_OFFSET(cpu) ((cpu * 0x100) + 0x124)
  62. /* PMSU fabric registers */
  63. #define L2C_NFABRIC_PM_CTL 0x4
  64. #define L2C_NFABRIC_PM_CTL_PWR_DOWN BIT(20)
  65. /* PMSU delay registers */
  66. #define PMSU_POWERDOWN_DELAY 0xF04
  67. #define PMSU_POWERDOWN_DELAY_PMU BIT(1)
  68. #define PMSU_POWERDOWN_DELAY_MASK 0xFFFE
  69. #define PMSU_DFLT_ARMADA38X_DELAY 0x64
  70. /* CA9 MPcore SoC Control registers */
  71. #define MPCORE_RESET_CTL 0x64
  72. #define MPCORE_RESET_CTL_L2 BIT(0)
  73. #define MPCORE_RESET_CTL_DEBUG BIT(16)
  74. #define SRAM_PHYS_BASE 0xFFFF0000
  75. #define BOOTROM_BASE 0xFFF00000
  76. #define BOOTROM_SIZE 0x100000
  77. #define ARMADA_370_CRYPT0_ENG_TARGET 0x9
  78. #define ARMADA_370_CRYPT0_ENG_ATTR 0x1
  79. extern void ll_disable_coherency(void);
  80. extern void ll_enable_coherency(void);
  81. extern void armada_370_xp_cpu_resume(void);
  82. extern void armada_38x_cpu_resume(void);
  83. static phys_addr_t pmsu_mp_phys_base;
  84. static void __iomem *pmsu_mp_base;
  85. static void *mvebu_cpu_resume;
  86. static struct of_device_id of_pmsu_table[] = {
  87. { .compatible = "marvell,armada-370-pmsu", },
  88. { .compatible = "marvell,armada-370-xp-pmsu", },
  89. { .compatible = "marvell,armada-380-pmsu", },
  90. { /* end of list */ },
  91. };
  92. void mvebu_pmsu_set_cpu_boot_addr(int hw_cpu, void *boot_addr)
  93. {
  94. writel(virt_to_phys(boot_addr), pmsu_mp_base +
  95. PMSU_BOOT_ADDR_REDIRECT_OFFSET(hw_cpu));
  96. }
  97. extern unsigned char mvebu_boot_wa_start;
  98. extern unsigned char mvebu_boot_wa_end;
  99. /*
  100. * This function sets up the boot address workaround needed for SMP
  101. * boot on Armada 375 Z1 and cpuidle on Armada 370. It unmaps the
  102. * BootROM Mbus window, and instead remaps a crypto SRAM into which a
  103. * custom piece of code is copied to replace the problematic BootROM.
  104. */
  105. int mvebu_setup_boot_addr_wa(unsigned int crypto_eng_target,
  106. unsigned int crypto_eng_attribute,
  107. phys_addr_t resume_addr_reg)
  108. {
  109. void __iomem *sram_virt_base;
  110. u32 code_len = &mvebu_boot_wa_end - &mvebu_boot_wa_start;
  111. mvebu_mbus_del_window(BOOTROM_BASE, BOOTROM_SIZE);
  112. mvebu_mbus_add_window_by_id(crypto_eng_target, crypto_eng_attribute,
  113. SRAM_PHYS_BASE, SZ_64K);
  114. sram_virt_base = ioremap(SRAM_PHYS_BASE, SZ_64K);
  115. if (!sram_virt_base) {
  116. pr_err("Unable to map SRAM to setup the boot address WA\n");
  117. return -ENOMEM;
  118. }
  119. memcpy(sram_virt_base, &mvebu_boot_wa_start, code_len);
  120. /*
  121. * The last word of the code copied in SRAM must contain the
  122. * physical base address of the PMSU register. We
  123. * intentionally store this address in the native endianness
  124. * of the system.
  125. */
  126. __raw_writel((unsigned long)resume_addr_reg,
  127. sram_virt_base + code_len - 4);
  128. iounmap(sram_virt_base);
  129. return 0;
  130. }
  131. static int __init mvebu_v7_pmsu_init(void)
  132. {
  133. struct device_node *np;
  134. struct resource res;
  135. int ret = 0;
  136. np = of_find_matching_node(NULL, of_pmsu_table);
  137. if (!np)
  138. return 0;
  139. pr_info("Initializing Power Management Service Unit\n");
  140. if (of_address_to_resource(np, 0, &res)) {
  141. pr_err("unable to get resource\n");
  142. ret = -ENOENT;
  143. goto out;
  144. }
  145. if (of_device_is_compatible(np, "marvell,armada-370-xp-pmsu")) {
  146. pr_warn(FW_WARN "deprecated pmsu binding\n");
  147. res.start = res.start - PMSU_BASE_OFFSET;
  148. res.end = res.start + PMSU_REG_SIZE - 1;
  149. }
  150. if (!request_mem_region(res.start, resource_size(&res),
  151. np->full_name)) {
  152. pr_err("unable to request region\n");
  153. ret = -EBUSY;
  154. goto out;
  155. }
  156. pmsu_mp_phys_base = res.start;
  157. pmsu_mp_base = ioremap(res.start, resource_size(&res));
  158. if (!pmsu_mp_base) {
  159. pr_err("unable to map registers\n");
  160. release_mem_region(res.start, resource_size(&res));
  161. ret = -ENOMEM;
  162. goto out;
  163. }
  164. out:
  165. of_node_put(np);
  166. return ret;
  167. }
  168. static void mvebu_v7_pmsu_enable_l2_powerdown_onidle(void)
  169. {
  170. u32 reg;
  171. if (pmsu_mp_base == NULL)
  172. return;
  173. /* Enable L2 & Fabric powerdown in Deep-Idle mode - Fabric */
  174. reg = readl(pmsu_mp_base + L2C_NFABRIC_PM_CTL);
  175. reg |= L2C_NFABRIC_PM_CTL_PWR_DOWN;
  176. writel(reg, pmsu_mp_base + L2C_NFABRIC_PM_CTL);
  177. }
  178. enum pmsu_idle_prepare_flags {
  179. PMSU_PREPARE_NORMAL = 0,
  180. PMSU_PREPARE_DEEP_IDLE = BIT(0),
  181. PMSU_PREPARE_SNOOP_DISABLE = BIT(1),
  182. };
  183. /* No locking is needed because we only access per-CPU registers */
  184. static int mvebu_v7_pmsu_idle_prepare(unsigned long flags)
  185. {
  186. unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
  187. u32 reg;
  188. if (pmsu_mp_base == NULL)
  189. return -EINVAL;
  190. /*
  191. * Adjust the PMSU configuration to wait for WFI signal, enable
  192. * IRQ and FIQ as wakeup events, set wait for snoop queue empty
  193. * indication and mask IRQ and FIQ from CPU
  194. */
  195. reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
  196. reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
  197. PMSU_STATUS_AND_MASK_IRQ_WAKEUP |
  198. PMSU_STATUS_AND_MASK_FIQ_WAKEUP |
  199. PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT |
  200. PMSU_STATUS_AND_MASK_IRQ_MASK |
  201. PMSU_STATUS_AND_MASK_FIQ_MASK;
  202. writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
  203. reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
  204. /* ask HW to power down the L2 Cache if needed */
  205. if (flags & PMSU_PREPARE_DEEP_IDLE)
  206. reg |= PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
  207. /* request power down */
  208. reg |= PMSU_CONTROL_AND_CONFIG_PWDDN_REQ;
  209. writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
  210. if (flags & PMSU_PREPARE_SNOOP_DISABLE) {
  211. /* Disable snoop disable by HW - SW is taking care of it */
  212. reg = readl(pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
  213. reg |= PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP;
  214. writel(reg, pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
  215. }
  216. return 0;
  217. }
  218. int armada_370_xp_pmsu_idle_enter(unsigned long deepidle)
  219. {
  220. unsigned long flags = PMSU_PREPARE_SNOOP_DISABLE;
  221. int ret;
  222. if (deepidle)
  223. flags |= PMSU_PREPARE_DEEP_IDLE;
  224. ret = mvebu_v7_pmsu_idle_prepare(flags);
  225. if (ret)
  226. return ret;
  227. v7_exit_coherency_flush(all);
  228. ll_disable_coherency();
  229. dsb();
  230. wfi();
  231. /* If we are here, wfi failed. As processors run out of
  232. * coherency for some time, tlbs might be stale, so flush them
  233. */
  234. local_flush_tlb_all();
  235. ll_enable_coherency();
  236. /* Test the CR_C bit and set it if it was cleared */
  237. asm volatile(
  238. "mrc p15, 0, r0, c1, c0, 0 \n\t"
  239. "tst r0, #(1 << 2) \n\t"
  240. "orreq r0, r0, #(1 << 2) \n\t"
  241. "mcreq p15, 0, r0, c1, c0, 0 \n\t"
  242. "isb "
  243. : : : "r0");
  244. pr_debug("Failed to suspend the system\n");
  245. return 0;
  246. }
  247. static int armada_370_xp_cpu_suspend(unsigned long deepidle)
  248. {
  249. return cpu_suspend(deepidle, armada_370_xp_pmsu_idle_enter);
  250. }
  251. static int armada_38x_do_cpu_suspend(unsigned long deepidle)
  252. {
  253. unsigned long flags = 0;
  254. if (deepidle)
  255. flags |= PMSU_PREPARE_DEEP_IDLE;
  256. mvebu_v7_pmsu_idle_prepare(flags);
  257. /*
  258. * Already flushed cache, but do it again as the outer cache
  259. * functions dirty the cache with spinlocks
  260. */
  261. v7_exit_coherency_flush(louis);
  262. scu_power_mode(mvebu_get_scu_base(), SCU_PM_POWEROFF);
  263. cpu_do_idle();
  264. return 1;
  265. }
  266. static int armada_38x_cpu_suspend(unsigned long deepidle)
  267. {
  268. return cpu_suspend(false, armada_38x_do_cpu_suspend);
  269. }
  270. /* No locking is needed because we only access per-CPU registers */
  271. void mvebu_v7_pmsu_idle_exit(void)
  272. {
  273. unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
  274. u32 reg;
  275. if (pmsu_mp_base == NULL)
  276. return;
  277. /* cancel ask HW to power down the L2 Cache if possible */
  278. reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
  279. reg &= ~PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
  280. writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
  281. /* cancel Enable wakeup events and mask interrupts */
  282. reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
  283. reg &= ~(PMSU_STATUS_AND_MASK_IRQ_WAKEUP | PMSU_STATUS_AND_MASK_FIQ_WAKEUP);
  284. reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
  285. reg &= ~PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT;
  286. reg &= ~(PMSU_STATUS_AND_MASK_IRQ_MASK | PMSU_STATUS_AND_MASK_FIQ_MASK);
  287. writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
  288. }
  289. static int mvebu_v7_cpu_pm_notify(struct notifier_block *self,
  290. unsigned long action, void *hcpu)
  291. {
  292. if (action == CPU_PM_ENTER) {
  293. unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
  294. mvebu_pmsu_set_cpu_boot_addr(hw_cpu, mvebu_cpu_resume);
  295. } else if (action == CPU_PM_EXIT) {
  296. mvebu_v7_pmsu_idle_exit();
  297. }
  298. return NOTIFY_OK;
  299. }
  300. static struct notifier_block mvebu_v7_cpu_pm_notifier = {
  301. .notifier_call = mvebu_v7_cpu_pm_notify,
  302. };
  303. static struct platform_device mvebu_v7_cpuidle_device;
  304. static __init int armada_370_cpuidle_init(void)
  305. {
  306. struct device_node *np;
  307. phys_addr_t redirect_reg;
  308. np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
  309. if (!np)
  310. return -ENODEV;
  311. of_node_put(np);
  312. /*
  313. * On Armada 370, there is "a slow exit process from the deep
  314. * idle state due to heavy L1/L2 cache cleanup operations
  315. * performed by the BootROM software". To avoid this, we
  316. * replace the restart code of the bootrom by a a simple jump
  317. * to the boot address. Then the code located at this boot
  318. * address will take care of the initialization.
  319. */
  320. redirect_reg = pmsu_mp_phys_base + PMSU_BOOT_ADDR_REDIRECT_OFFSET(0);
  321. mvebu_setup_boot_addr_wa(ARMADA_370_CRYPT0_ENG_TARGET,
  322. ARMADA_370_CRYPT0_ENG_ATTR,
  323. redirect_reg);
  324. mvebu_cpu_resume = armada_370_xp_cpu_resume;
  325. mvebu_v7_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
  326. mvebu_v7_cpuidle_device.name = "cpuidle-armada-370";
  327. return 0;
  328. }
  329. static __init int armada_38x_cpuidle_init(void)
  330. {
  331. struct device_node *np;
  332. void __iomem *mpsoc_base;
  333. u32 reg;
  334. pr_warn("CPU idle is currently broken on Armada 38x: disabling");
  335. return 0;
  336. np = of_find_compatible_node(NULL, NULL,
  337. "marvell,armada-380-coherency-fabric");
  338. if (!np)
  339. return -ENODEV;
  340. of_node_put(np);
  341. np = of_find_compatible_node(NULL, NULL,
  342. "marvell,armada-380-mpcore-soc-ctrl");
  343. if (!np)
  344. return -ENODEV;
  345. mpsoc_base = of_iomap(np, 0);
  346. BUG_ON(!mpsoc_base);
  347. of_node_put(np);
  348. /* Set up reset mask when powering down the cpus */
  349. reg = readl(mpsoc_base + MPCORE_RESET_CTL);
  350. reg |= MPCORE_RESET_CTL_L2;
  351. reg |= MPCORE_RESET_CTL_DEBUG;
  352. writel(reg, mpsoc_base + MPCORE_RESET_CTL);
  353. iounmap(mpsoc_base);
  354. /* Set up delay */
  355. reg = readl(pmsu_mp_base + PMSU_POWERDOWN_DELAY);
  356. reg &= ~PMSU_POWERDOWN_DELAY_MASK;
  357. reg |= PMSU_DFLT_ARMADA38X_DELAY;
  358. reg |= PMSU_POWERDOWN_DELAY_PMU;
  359. writel(reg, pmsu_mp_base + PMSU_POWERDOWN_DELAY);
  360. mvebu_cpu_resume = armada_38x_cpu_resume;
  361. mvebu_v7_cpuidle_device.dev.platform_data = armada_38x_cpu_suspend;
  362. mvebu_v7_cpuidle_device.name = "cpuidle-armada-38x";
  363. return 0;
  364. }
  365. static __init int armada_xp_cpuidle_init(void)
  366. {
  367. struct device_node *np;
  368. np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
  369. if (!np)
  370. return -ENODEV;
  371. of_node_put(np);
  372. mvebu_cpu_resume = armada_370_xp_cpu_resume;
  373. mvebu_v7_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
  374. mvebu_v7_cpuidle_device.name = "cpuidle-armada-xp";
  375. return 0;
  376. }
  377. static int __init mvebu_v7_cpu_pm_init(void)
  378. {
  379. struct device_node *np;
  380. int ret;
  381. np = of_find_matching_node(NULL, of_pmsu_table);
  382. if (!np)
  383. return 0;
  384. of_node_put(np);
  385. /*
  386. * Currently the CPU idle support for Armada 38x is broken, as
  387. * the CPU hotplug uses some of the CPU idle functions it is
  388. * broken too, so let's disable it
  389. */
  390. if (of_machine_is_compatible("marvell,armada380")) {
  391. cpu_hotplug_disable();
  392. pr_warn("CPU hotplug support is currently broken on Armada 38x: disabling");
  393. }
  394. if (of_machine_is_compatible("marvell,armadaxp"))
  395. ret = armada_xp_cpuidle_init();
  396. else if (of_machine_is_compatible("marvell,armada370"))
  397. ret = armada_370_cpuidle_init();
  398. else if (of_machine_is_compatible("marvell,armada380"))
  399. ret = armada_38x_cpuidle_init();
  400. else
  401. return 0;
  402. if (ret)
  403. return ret;
  404. mvebu_v7_pmsu_enable_l2_powerdown_onidle();
  405. if (mvebu_v7_cpuidle_device.name)
  406. platform_device_register(&mvebu_v7_cpuidle_device);
  407. cpu_pm_register_notifier(&mvebu_v7_cpu_pm_notifier);
  408. return 0;
  409. }
  410. arch_initcall(mvebu_v7_cpu_pm_init);
  411. early_initcall(mvebu_v7_pmsu_init);
  412. static void mvebu_pmsu_dfs_request_local(void *data)
  413. {
  414. u32 reg;
  415. u32 cpu = smp_processor_id();
  416. unsigned long flags;
  417. local_irq_save(flags);
  418. /* Prepare to enter idle */
  419. reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
  420. reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
  421. PMSU_STATUS_AND_MASK_IRQ_MASK |
  422. PMSU_STATUS_AND_MASK_FIQ_MASK;
  423. writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
  424. /* Request the DFS transition */
  425. reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
  426. reg |= PMSU_CONTROL_AND_CONFIG_DFS_REQ;
  427. writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
  428. /* The fact of entering idle will trigger the DFS transition */
  429. wfi();
  430. /*
  431. * We're back from idle, the DFS transition has completed,
  432. * clear the idle wait indication.
  433. */
  434. reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
  435. reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
  436. writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
  437. local_irq_restore(flags);
  438. }
  439. int mvebu_pmsu_dfs_request(int cpu)
  440. {
  441. unsigned long timeout;
  442. int hwcpu = cpu_logical_map(cpu);
  443. u32 reg;
  444. /* Clear any previous DFS DONE event */
  445. reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  446. reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE;
  447. writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  448. /* Mask the DFS done interrupt, since we are going to poll */
  449. reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  450. reg |= PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
  451. writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  452. /* Trigger the DFS on the appropriate CPU */
  453. smp_call_function_single(cpu, mvebu_pmsu_dfs_request_local,
  454. NULL, false);
  455. /* Poll until the DFS done event is generated */
  456. timeout = jiffies + HZ;
  457. while (time_before(jiffies, timeout)) {
  458. reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  459. if (reg & PMSU_EVENT_STATUS_AND_MASK_DFS_DONE)
  460. break;
  461. udelay(10);
  462. }
  463. if (time_after(jiffies, timeout))
  464. return -ETIME;
  465. /* Restore the DFS mask to its original state */
  466. reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  467. reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
  468. writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
  469. return 0;
  470. }
  471. static int __init armada_xp_pmsu_cpufreq_init(void)
  472. {
  473. struct device_node *np;
  474. struct resource res;
  475. int ret, cpu;
  476. if (!of_machine_is_compatible("marvell,armadaxp"))
  477. return 0;
  478. /*
  479. * In order to have proper cpufreq handling, we need to ensure
  480. * that the Device Tree description of the CPU clock includes
  481. * the definition of the PMU DFS registers. If not, we do not
  482. * register the clock notifier and the cpufreq driver. This
  483. * piece of code is only for compatibility with old Device
  484. * Trees.
  485. */
  486. np = of_find_compatible_node(NULL, NULL, "marvell,armada-xp-cpu-clock");
  487. if (!np)
  488. return 0;
  489. ret = of_address_to_resource(np, 1, &res);
  490. if (ret) {
  491. pr_warn(FW_WARN "not enabling cpufreq, deprecated armada-xp-cpu-clock binding\n");
  492. of_node_put(np);
  493. return 0;
  494. }
  495. of_node_put(np);
  496. /*
  497. * For each CPU, this loop registers the operating points
  498. * supported (which are the nominal CPU frequency and half of
  499. * it), and registers the clock notifier that will take care
  500. * of doing the PMSU part of a frequency transition.
  501. */
  502. for_each_possible_cpu(cpu) {
  503. struct device *cpu_dev;
  504. struct clk *clk;
  505. int ret;
  506. cpu_dev = get_cpu_device(cpu);
  507. if (!cpu_dev) {
  508. pr_err("Cannot get CPU %d\n", cpu);
  509. continue;
  510. }
  511. clk = clk_get(cpu_dev, 0);
  512. if (IS_ERR(clk)) {
  513. pr_err("Cannot get clock for CPU %d\n", cpu);
  514. return PTR_ERR(clk);
  515. }
  516. /*
  517. * In case of a failure of dev_pm_opp_add(), we don't
  518. * bother with cleaning up the registered OPP (there's
  519. * no function to do so), and simply cancel the
  520. * registration of the cpufreq device.
  521. */
  522. ret = dev_pm_opp_add(cpu_dev, clk_get_rate(clk), 0);
  523. if (ret) {
  524. clk_put(clk);
  525. return ret;
  526. }
  527. ret = dev_pm_opp_add(cpu_dev, clk_get_rate(clk) / 2, 0);
  528. if (ret) {
  529. clk_put(clk);
  530. return ret;
  531. }
  532. }
  533. platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
  534. return 0;
  535. }
  536. device_initcall(armada_xp_pmsu_cpufreq_init);