Path: blob/master/arch/powerpc/platforms/83xx/suspend.c
10819 views
/*1* MPC83xx suspend support2*3* Author: Scott Wood <[email protected]>4*5* Copyright (c) 2006-2007 Freescale Semiconductor, Inc.6*7* This program is free software; you can redistribute it and/or modify it8* under the terms of the GNU General Public License version 2 as published9* by the Free Software Foundation.10*/1112#include <linux/init.h>13#include <linux/pm.h>14#include <linux/types.h>15#include <linux/ioport.h>16#include <linux/interrupt.h>17#include <linux/wait.h>18#include <linux/kthread.h>19#include <linux/freezer.h>20#include <linux/suspend.h>21#include <linux/fsl_devices.h>22#include <linux/of_platform.h>2324#include <asm/reg.h>25#include <asm/io.h>26#include <asm/time.h>27#include <asm/mpc6xx.h>2829#include <sysdev/fsl_soc.h>3031#define PMCCR1_NEXT_STATE 0x0C /* Next state for power management */32#define PMCCR1_NEXT_STATE_SHIFT 233#define PMCCR1_CURR_STATE 0x03 /* Current state for power management*/34#define IMMR_SYSCR_OFFSET 0x10035#define IMMR_RCW_OFFSET 0x90036#define RCW_PCI_HOST 0x800000003738void mpc83xx_enter_deep_sleep(phys_addr_t immrbase);3940struct mpc83xx_pmc {41u32 config;42#define PMCCR_DLPEN 2 /* DDR SDRAM low power enable */43#define PMCCR_SLPEN 1 /* System low power enable */4445u32 event;46u32 mask;47/* All but PMCI are deep-sleep only */48#define PMCER_GPIO 0x10049#define PMCER_PCI 0x08050#define PMCER_USB 0x04051#define PMCER_ETSEC1 0x02052#define PMCER_ETSEC2 0x01053#define PMCER_TIMER 0x00854#define PMCER_INT1 0x00455#define PMCER_INT2 0x00256#define PMCER_PMCI 0x00157#define PMCER_ALL 0x1FF5859/* deep-sleep only */60u32 config1;61#define PMCCR1_USE_STATE 0x8000000062#define PMCCR1_PME_EN 0x0000008063#define PMCCR1_ASSERT_PME 0x0000004064#define PMCCR1_POWER_OFF 0x000000206566/* deep-sleep only */67u32 config2;68};6970struct mpc83xx_rcw {71u32 rcwlr;72u32 rcwhr;73};7475struct mpc83xx_clock {76u32 spmr;77u32 occr;78u32 sccr;79};8081struct mpc83xx_syscr {82__be32 sgprl;83__be32 sgprh;84__be32 spridr;85__be32 :32;86__be32 spcr;87__be32 sicrl;88__be32 sicrh;89};9091struct mpc83xx_saved {92u32 sicrl;93u32 sicrh;94u32 sccr;95};9697struct pmc_type {98int has_deep_sleep;99};100101static struct platform_device *pmc_dev;102static int has_deep_sleep, deep_sleeping;103static int pmc_irq;104static struct mpc83xx_pmc __iomem *pmc_regs;105static struct mpc83xx_clock __iomem *clock_regs;106static struct mpc83xx_syscr __iomem *syscr_regs;107static struct mpc83xx_saved saved_regs;108static int is_pci_agent, wake_from_pci;109static phys_addr_t immrbase;110static int pci_pm_state;111static DECLARE_WAIT_QUEUE_HEAD(agent_wq);112113int fsl_deep_sleep(void)114{115return deep_sleeping;116}117EXPORT_SYMBOL(fsl_deep_sleep);118119static int mpc83xx_change_state(void)120{121u32 curr_state;122u32 reg_cfg1 = in_be32(&pmc_regs->config1);123124if (is_pci_agent) {125pci_pm_state = (reg_cfg1 & PMCCR1_NEXT_STATE) >>126PMCCR1_NEXT_STATE_SHIFT;127curr_state = reg_cfg1 & PMCCR1_CURR_STATE;128129if (curr_state != pci_pm_state) {130reg_cfg1 &= ~PMCCR1_CURR_STATE;131reg_cfg1 |= pci_pm_state;132out_be32(&pmc_regs->config1, reg_cfg1);133134wake_up(&agent_wq);135return 1;136}137}138139return 0;140}141142static irqreturn_t pmc_irq_handler(int irq, void *dev_id)143{144u32 event = in_be32(&pmc_regs->event);145int ret = IRQ_NONE;146147if (mpc83xx_change_state())148ret = IRQ_HANDLED;149150if (event) {151out_be32(&pmc_regs->event, event);152ret = IRQ_HANDLED;153}154155return ret;156}157158static void mpc83xx_suspend_restore_regs(void)159{160out_be32(&syscr_regs->sicrl, saved_regs.sicrl);161out_be32(&syscr_regs->sicrh, saved_regs.sicrh);162out_be32(&clock_regs->sccr, saved_regs.sccr);163}164165static void mpc83xx_suspend_save_regs(void)166{167saved_regs.sicrl = in_be32(&syscr_regs->sicrl);168saved_regs.sicrh = in_be32(&syscr_regs->sicrh);169saved_regs.sccr = in_be32(&clock_regs->sccr);170}171172static int mpc83xx_suspend_enter(suspend_state_t state)173{174int ret = -EAGAIN;175176/* Don't go to sleep if there's a race where pci_pm_state changes177* between the agent thread checking it and the PM code disabling178* interrupts.179*/180if (wake_from_pci) {181if (pci_pm_state != (deep_sleeping ? 3 : 2))182goto out;183184out_be32(&pmc_regs->config1,185in_be32(&pmc_regs->config1) | PMCCR1_PME_EN);186}187188/* Put the system into low-power mode and the RAM189* into self-refresh mode once the core goes to190* sleep.191*/192193out_be32(&pmc_regs->config, PMCCR_SLPEN | PMCCR_DLPEN);194195/* If it has deep sleep (i.e. it's an 831x or compatible),196* disable power to the core upon entering sleep mode. This will197* require going through the boot firmware upon a wakeup event.198*/199200if (deep_sleeping) {201mpc83xx_suspend_save_regs();202203out_be32(&pmc_regs->mask, PMCER_ALL);204205out_be32(&pmc_regs->config1,206in_be32(&pmc_regs->config1) | PMCCR1_POWER_OFF);207208enable_kernel_fp();209210mpc83xx_enter_deep_sleep(immrbase);211212out_be32(&pmc_regs->config1,213in_be32(&pmc_regs->config1) & ~PMCCR1_POWER_OFF);214215out_be32(&pmc_regs->mask, PMCER_PMCI);216217mpc83xx_suspend_restore_regs();218} else {219out_be32(&pmc_regs->mask, PMCER_PMCI);220221mpc6xx_enter_standby();222}223224ret = 0;225226out:227out_be32(&pmc_regs->config1,228in_be32(&pmc_regs->config1) & ~PMCCR1_PME_EN);229230return ret;231}232233static void mpc83xx_suspend_end(void)234{235deep_sleeping = 0;236}237238static int mpc83xx_suspend_valid(suspend_state_t state)239{240return state == PM_SUSPEND_STANDBY || state == PM_SUSPEND_MEM;241}242243static int mpc83xx_suspend_begin(suspend_state_t state)244{245switch (state) {246case PM_SUSPEND_STANDBY:247deep_sleeping = 0;248return 0;249250case PM_SUSPEND_MEM:251if (has_deep_sleep)252deep_sleeping = 1;253254return 0;255256default:257return -EINVAL;258}259}260261static int agent_thread_fn(void *data)262{263while (1) {264wait_event_interruptible(agent_wq, pci_pm_state >= 2);265try_to_freeze();266267if (signal_pending(current) || pci_pm_state < 2)268continue;269270/* With a preemptible kernel (or SMP), this could race with271* a userspace-driven suspend request. It's probably best272* to avoid mixing the two with such a configuration (or273* else fix it by adding a mutex to state_store that we can274* synchronize with).275*/276277wake_from_pci = 1;278279pm_suspend(pci_pm_state == 3 ? PM_SUSPEND_MEM :280PM_SUSPEND_STANDBY);281282wake_from_pci = 0;283}284285return 0;286}287288static void mpc83xx_set_agent(void)289{290out_be32(&pmc_regs->config1, PMCCR1_USE_STATE);291out_be32(&pmc_regs->mask, PMCER_PMCI);292293kthread_run(agent_thread_fn, NULL, "PCI power mgt");294}295296static int mpc83xx_is_pci_agent(void)297{298struct mpc83xx_rcw __iomem *rcw_regs;299int ret;300301rcw_regs = ioremap(get_immrbase() + IMMR_RCW_OFFSET,302sizeof(struct mpc83xx_rcw));303304if (!rcw_regs)305return -ENOMEM;306307ret = !(in_be32(&rcw_regs->rcwhr) & RCW_PCI_HOST);308309iounmap(rcw_regs);310return ret;311}312313static const struct platform_suspend_ops mpc83xx_suspend_ops = {314.valid = mpc83xx_suspend_valid,315.begin = mpc83xx_suspend_begin,316.enter = mpc83xx_suspend_enter,317.end = mpc83xx_suspend_end,318};319320static struct of_device_id pmc_match[];321static int pmc_probe(struct platform_device *ofdev)322{323const struct of_device_id *match;324struct device_node *np = ofdev->dev.of_node;325struct resource res;326struct pmc_type *type;327int ret = 0;328329match = of_match_device(pmc_match, &ofdev->dev);330if (!match)331return -EINVAL;332333type = match->data;334335if (!of_device_is_available(np))336return -ENODEV;337338has_deep_sleep = type->has_deep_sleep;339immrbase = get_immrbase();340pmc_dev = ofdev;341342is_pci_agent = mpc83xx_is_pci_agent();343if (is_pci_agent < 0)344return is_pci_agent;345346ret = of_address_to_resource(np, 0, &res);347if (ret)348return -ENODEV;349350pmc_irq = irq_of_parse_and_map(np, 0);351if (pmc_irq != NO_IRQ) {352ret = request_irq(pmc_irq, pmc_irq_handler, IRQF_SHARED,353"pmc", ofdev);354355if (ret)356return -EBUSY;357}358359pmc_regs = ioremap(res.start, sizeof(struct mpc83xx_pmc));360361if (!pmc_regs) {362ret = -ENOMEM;363goto out;364}365366ret = of_address_to_resource(np, 1, &res);367if (ret) {368ret = -ENODEV;369goto out_pmc;370}371372clock_regs = ioremap(res.start, sizeof(struct mpc83xx_pmc));373374if (!clock_regs) {375ret = -ENOMEM;376goto out_pmc;377}378379if (has_deep_sleep) {380syscr_regs = ioremap(immrbase + IMMR_SYSCR_OFFSET,381sizeof(*syscr_regs));382if (!syscr_regs) {383ret = -ENOMEM;384goto out_syscr;385}386}387388if (is_pci_agent)389mpc83xx_set_agent();390391suspend_set_ops(&mpc83xx_suspend_ops);392return 0;393394out_syscr:395iounmap(clock_regs);396out_pmc:397iounmap(pmc_regs);398out:399if (pmc_irq != NO_IRQ)400free_irq(pmc_irq, ofdev);401402return ret;403}404405static int pmc_remove(struct platform_device *ofdev)406{407return -EPERM;408};409410static struct pmc_type pmc_types[] = {411{412.has_deep_sleep = 1,413},414{415.has_deep_sleep = 0,416}417};418419static struct of_device_id pmc_match[] = {420{421.compatible = "fsl,mpc8313-pmc",422.data = &pmc_types[0],423},424{425.compatible = "fsl,mpc8349-pmc",426.data = &pmc_types[1],427},428{}429};430431static struct platform_driver pmc_driver = {432.driver = {433.name = "mpc83xx-pmc",434.owner = THIS_MODULE,435.of_match_table = pmc_match,436},437.probe = pmc_probe,438.remove = pmc_remove439};440441static int pmc_init(void)442{443return platform_driver_register(&pmc_driver);444}445446module_init(pmc_init);447448449