Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_pm.c
26481 views
// SPDX-License-Identifier: GPL-2.01#include <linux/init.h>2#include <linux/suspend.h>3#include <linux/io.h>4#include <linux/of_address.h>56#include <asm/time.h>7#include <asm/cacheflush.h>8#include <asm/mpc52xx.h>910/* these are defined in mpc52xx_sleep.S, and only used here */11extern void mpc52xx_deep_sleep(void __iomem *sram, void __iomem *sdram_regs,12struct mpc52xx_cdm __iomem *, struct mpc52xx_intr __iomem*);13extern void mpc52xx_ds_sram(void);14extern const long mpc52xx_ds_sram_size;15extern void mpc52xx_ds_cached(void);16extern const long mpc52xx_ds_cached_size;1718static void __iomem *mbar;19static void __iomem *sdram;20static struct mpc52xx_cdm __iomem *cdm;21static struct mpc52xx_intr __iomem *intr;22static struct mpc52xx_gpio_wkup __iomem *gpiow;23static void __iomem *sram;24static int sram_size;2526struct mpc52xx_suspend mpc52xx_suspend;2728static int mpc52xx_pm_valid(suspend_state_t state)29{30switch (state) {31case PM_SUSPEND_STANDBY:32return 1;33default:34return 0;35}36}3738int mpc52xx_set_wakeup_gpio(u8 pin, u8 level)39{40u16 tmp;4142/* enable gpio */43out_8(&gpiow->wkup_gpioe, in_8(&gpiow->wkup_gpioe) | (1 << pin));44/* set as input */45out_8(&gpiow->wkup_ddr, in_8(&gpiow->wkup_ddr) & ~(1 << pin));46/* enable deep sleep interrupt */47out_8(&gpiow->wkup_inten, in_8(&gpiow->wkup_inten) | (1 << pin));48/* low/high level creates wakeup interrupt */49tmp = in_be16(&gpiow->wkup_itype);50tmp &= ~(0x3 << (pin * 2));51tmp |= (!level + 1) << (pin * 2);52out_be16(&gpiow->wkup_itype, tmp);53/* master enable */54out_8(&gpiow->wkup_maste, 1);5556return 0;57}5859int mpc52xx_pm_prepare(void)60{61struct device_node *np;62static const struct of_device_id immr_ids[] = {63{ .compatible = "fsl,mpc5200-immr", },64{ .compatible = "fsl,mpc5200b-immr", },65{ .type = "soc", .compatible = "mpc5200", }, /* lite5200 */66{ .type = "builtin", .compatible = "mpc5200", }, /* efika */67{}68};69struct resource res;7071/* map the whole register space */72np = of_find_matching_node(NULL, immr_ids);7374if (of_address_to_resource(np, 0, &res)) {75pr_err("mpc52xx_pm_prepare(): could not get IMMR address\n");76of_node_put(np);77return -ENOSYS;78}7980mbar = ioremap(res.start, 0xc000); /* we should map whole region including SRAM */8182of_node_put(np);83if (!mbar) {84pr_err("mpc52xx_pm_prepare(): could not map registers\n");85return -ENOSYS;86}87/* these offsets are from mpc5200 users manual */88sdram = mbar + 0x100;89cdm = mbar + 0x200;90intr = mbar + 0x500;91gpiow = mbar + 0xc00;92sram = mbar + 0x8000; /* Those will be handled by the */93sram_size = 0x4000; /* bestcomm driver soon */9495/* call board suspend code, if applicable */96if (mpc52xx_suspend.board_suspend_prepare)97mpc52xx_suspend.board_suspend_prepare(mbar);98else {99printk(KERN_ALERT "%s: %i don't know how to wake up the board\n",100__func__, __LINE__);101goto out_unmap;102}103104return 0;105106out_unmap:107iounmap(mbar);108return -ENOSYS;109}110111112char saved_sram[0x4000];113114int mpc52xx_pm_enter(suspend_state_t state)115{116u32 clk_enables;117u32 msr, hid0;118u32 intr_main_mask;119void __iomem * irq_0x500 = (void __iomem *)CONFIG_KERNEL_START + 0x500;120unsigned long irq_0x500_stop = (unsigned long)irq_0x500 + mpc52xx_ds_cached_size;121char saved_0x500[0x600-0x500];122123if (WARN_ON(mpc52xx_ds_cached_size > sizeof(saved_0x500)))124return -ENOMEM;125126/* disable all interrupts in PIC */127intr_main_mask = in_be32(&intr->main_mask);128out_be32(&intr->main_mask, intr_main_mask | 0x1ffff);129130/* don't let DEC expire any time soon */131mtspr(SPRN_DEC, 0x7fffffff);132133/* save SRAM */134memcpy(saved_sram, sram, sram_size);135136/* copy low level suspend code to sram */137memcpy(sram, mpc52xx_ds_sram, mpc52xx_ds_sram_size);138139out_8(&cdm->ccs_sleep_enable, 1);140out_8(&cdm->osc_sleep_enable, 1);141out_8(&cdm->ccs_qreq_test, 1);142143/* disable all but SDRAM and bestcomm (SRAM) clocks */144clk_enables = in_be32(&cdm->clk_enables);145out_be32(&cdm->clk_enables, clk_enables & 0x00088000);146147/* disable power management */148msr = mfmsr();149mtmsr(msr & ~MSR_POW);150151/* enable sleep mode, disable others */152hid0 = mfspr(SPRN_HID0);153mtspr(SPRN_HID0, (hid0 & ~(HID0_DOZE | HID0_NAP | HID0_DPM)) | HID0_SLEEP);154155/* save original, copy our irq handler, flush from dcache and invalidate icache */156memcpy(saved_0x500, irq_0x500, mpc52xx_ds_cached_size);157memcpy(irq_0x500, mpc52xx_ds_cached, mpc52xx_ds_cached_size);158flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);159160/* call low-level sleep code */161mpc52xx_deep_sleep(sram, sdram, cdm, intr);162163/* restore original irq handler */164memcpy(irq_0x500, saved_0x500, mpc52xx_ds_cached_size);165flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);166167/* restore old power mode */168mtmsr(msr & ~MSR_POW);169mtspr(SPRN_HID0, hid0);170mtmsr(msr);171172out_be32(&cdm->clk_enables, clk_enables);173out_8(&cdm->ccs_sleep_enable, 0);174out_8(&cdm->osc_sleep_enable, 0);175176/* restore SRAM */177memcpy(sram, saved_sram, sram_size);178179/* reenable interrupts in PIC */180out_be32(&intr->main_mask, intr_main_mask);181182return 0;183}184185void mpc52xx_pm_finish(void)186{187/* call board resume code */188if (mpc52xx_suspend.board_resume_finish)189mpc52xx_suspend.board_resume_finish(mbar);190191iounmap(mbar);192}193194static const struct platform_suspend_ops mpc52xx_pm_ops = {195.valid = mpc52xx_pm_valid,196.prepare = mpc52xx_pm_prepare,197.enter = mpc52xx_pm_enter,198.finish = mpc52xx_pm_finish,199};200201int __init mpc52xx_pm_init(void)202{203suspend_set_ops(&mpc52xx_pm_ops);204return 0;205}206207208