ARM: tegra12: set CPU rate to 2.2GHz for sku 0x87
[linux-3.10.git] / arch / arm / mach-tegra / powergate.c
index 0c5760c..2778e8c 100644 (file)
@@ -1,7 +1,8 @@
 /*
- * drivers/powergate/tegra-powergate.c
+ * arch/arm/mach-tegra/powergate.c
  *
  * Copyright (c) 2010 Google, Inc
+ * Copyright (c) 2011 - 2014, NVIDIA CORPORATION.  All rights reserved.
  *
  * Author:
  *     Colin Cross <ccross@google.com>
  */
 
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/clk.h>
+#include <linux/clk/tegra.h>
+#include <linux/string.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/seq_file.h>
 #include <linux/spinlock.h>
 #include <linux/clk/tegra.h>
-
-#include <mach/powergate.h>
+#include <linux/tegra-powergate.h>
+#include <linux/tegra-soc.h>
+#include <trace/events/power.h>
+#include <asm/atomic.h>
 
 #include "clock.h"
-#include "fuse.h"
 #include "iomap.h"
+#include "powergate-priv.h"
+#include "common.h"
 
-#define PWRGATE_TOGGLE         0x30
-#define PWRGATE_TOGGLE_START   (1 << 8)
-
-#define REMOVE_CLAMPING                0x34
+static struct powergate_ops *pg_ops;
 
-#define PWRGATE_STATUS         0x38
-
-static int tegra_num_powerdomains;
-static int tegra_num_cpu_domains;
-static u8 *tegra_cpu_domains;
-static u8 tegra30_cpu_domains[] = {
-       TEGRA_POWERGATE_CPU0,
-       TEGRA_POWERGATE_CPU1,
-       TEGRA_POWERGATE_CPU2,
-       TEGRA_POWERGATE_CPU3,
-};
+static spinlock_t *tegra_get_powergate_lock(void)
+{
+       if (pg_ops && pg_ops->get_powergate_lock)
+               return pg_ops->get_powergate_lock();
+       else
+               WARN_ON_ONCE("This SOC does not export powergate lock");
 
-#define MC_CLIENT_HOTRESET_CTRL        0x200
-#define MC_CLIENT_HOTRESET_STAT        0x204
-
-enum mc_client {
-       MC_CLIENT_AFI   = 0,
-       MC_CLIENT_AVPC  = 1,
-       MC_CLIENT_DC    = 2,
-       MC_CLIENT_DCB   = 3,
-       MC_CLIENT_EPP   = 4,
-       MC_CLIENT_G2    = 5,
-       MC_CLIENT_HC    = 6,
-       MC_CLIENT_HDA   = 7,
-       MC_CLIENT_ISP   = 8,
-       MC_CLIENT_MPCORE        = 9,
-       MC_CLIENT_MPCORELP      = 10,
-       MC_CLIENT_MPE   = 11,
-       MC_CLIENT_NV    = 12,
-       MC_CLIENT_NV2   = 13,
-       MC_CLIENT_PPCS  = 14,
-       MC_CLIENT_SATA  = 15,
-       MC_CLIENT_VDE   = 16,
-       MC_CLIENT_VI    = 17,
-       MC_CLIENT_LAST  = -1,
-};
+       return NULL;
+}
 
-#define MAX_CLK_EN_NUM                 4
+int tegra_powergate_set(int id, bool new_state)
+{
+       bool status;
+       unsigned long flags;
+       spinlock_t *lock;
+       u32 reg;
 
-static DEFINE_SPINLOCK(tegra_powergate_lock);
+       /* 10us timeout for toggle operation if it takes affect*/
+       int toggle_timeout = 10;
 
-#define MAX_HOTRESET_CLIENT_NUM                3
+       /* 100 * 10 = 1000us timeout for toggle command to take affect in case
+          of contention with h/w initiated CPU power gating */
+       int contention_timeout = 100;
 
-struct partition_clk_info {
-       const char *clk_name;
-       bool only_reset;
-       /* true if clk is only used in assert/deassert reset and not while enable-den*/
-       struct clk *clk_ptr;
-};
+       if (tegra_cpu_is_asim())
+               return 0;
 
-struct powergate_partition {
-       const char *name;
-       enum mc_client hot_reset_clients[MAX_HOTRESET_CLIENT_NUM];
-       struct partition_clk_info clk_info[MAX_CLK_EN_NUM];
-};
+       lock = tegra_get_powergate_lock();
 
-static struct powergate_partition powergate_partition_info[] = {
-       [TEGRA_POWERGATE_CPU]   = { "cpu0",     {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_L2]    = { "l2",       {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_3D]    = { "3d0",
-                                               {MC_CLIENT_NV, MC_CLIENT_LAST},
-                                               {{"3d", false} }, },
-       [TEGRA_POWERGATE_PCIE]  = { "pcie",
-                                               {MC_CLIENT_AFI, MC_CLIENT_LAST},
-                                               {{"afi", false},
-                                               {"pcie", false},
-                                               {"pciex", true} }, },
-       [TEGRA_POWERGATE_VDEC]  = { "vde",
-                                               {MC_CLIENT_VDE, MC_CLIENT_LAST},
-                                               {{"vde", false} }, },
-       [TEGRA_POWERGATE_MPE]   = { "mpe",
-                                               {MC_CLIENT_MPE, MC_CLIENT_LAST},
-                                               {{"mpe", false} }, },
-       [TEGRA_POWERGATE_VENC]  = { "ve",
-                                               {MC_CLIENT_ISP, MC_CLIENT_VI, MC_CLIENT_LAST},
-                                               {{"isp", false}, {"vi", false},
-                                               {"csi", false} }, },
-       [TEGRA_POWERGATE_CPU1]  = { "cpu1",     {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_CPU2]  = { "cpu2",     {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_CPU3]  = { "cpu3",     {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_A9LP]  = { "a9lp",     {MC_CLIENT_LAST}, },
-       [TEGRA_POWERGATE_SATA]  = { "sata",     {MC_CLIENT_SATA, MC_CLIENT_LAST},
-                                               {{"sata", false},
-                                               {"sata_cold", true} }, },
-       [TEGRA_POWERGATE_3D1]   = { "3d1",
-                                               {MC_CLIENT_NV2, MC_CLIENT_LAST},
-                                               {{"3d2", false} }, },
-       [TEGRA_POWERGATE_HEG]   = { "heg",
-                                               {MC_CLIENT_G2, MC_CLIENT_EPP, MC_CLIENT_HC},
-                                               {{"2d", false}, {"epp", false},
-                                               {"host1x", false},
-                                               {"3d", true} }, },
-};
+       spin_lock_irqsave(lock, flags);
 
-static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
+       status = !!(pmc_read(PWRGATE_STATUS) & (1 << id));
 
-static u32 pmc_read(unsigned long reg)
-{
-       return readl(pmc + reg);
-}
+       if (status == new_state) {
+               spin_unlock_irqrestore(lock, flags);
+               return 0;
+       }
 
-static void pmc_write(u32 val, unsigned long reg)
-{
-       writel(val, pmc + reg);
-}
+       if (TEGRA_IS_CPU_POWERGATE_ID(id)) {
+               /* CPU ungated in s/w only during boot/resume with outer
+                  waiting loop and no contention from other CPUs */
+               pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
+               spin_unlock_irqrestore(lock, flags);
+               return 0;
+       }
 
-#if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
-static void __iomem *mc = IO_ADDRESS(TEGRA_MC_BASE);
+#if !defined(CONFIG_ARCH_TEGRA_3x_SOC)
+       /* Wait if PMC is already processing some other power gating request */
+       do {
+               udelay(1);
+               reg = pmc_read(PWRGATE_TOGGLE);
+               contention_timeout--;
+       } while ((contention_timeout > 0) && (reg & PWRGATE_TOGGLE_START));
+
+       if (contention_timeout <= 0)
+               pr_err(" Timed out waiting for PMC to submit \
+                               new power gate request \n");
+       contention_timeout = 100;
+#endif
 
-static u32 mc_read(unsigned long reg)
-{
-       return readl(mc + reg);
-}
+       /* Submit power gate request */
+       pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
 
-static void mc_write(u32 val, unsigned long reg)
-{
-       writel(val, mc + reg);
-}
+#if !defined(CONFIG_ARCH_TEGRA_3x_SOC)
+       /* Wait while PMC accepts the request */
+       do {
+               udelay(1);
+               reg = pmc_read(PWRGATE_TOGGLE);
+               contention_timeout--;
+       } while ((contention_timeout > 0) && (reg & PWRGATE_TOGGLE_START));
+
+       if (contention_timeout <= 0)
+               pr_err(" Timed out waiting for PMC to accept \
+                               new power gate request \n");
+       contention_timeout = 100;
+#endif
 
-static void mc_flush(int id)
-{
-       u32 idx, rst_ctrl, rst_stat;
-       enum mc_client mcClientBit;
-       unsigned long flags;
+       /* Check power gate status */
+       do {
+               do {
+                       udelay(1);
+                       status = !!(pmc_read(PWRGATE_STATUS) & (1 << id));
 
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+                       toggle_timeout--;
+               } while ((status != new_state) && (toggle_timeout > 0));
 
-       for (idx = 0; idx < MAX_HOTRESET_CLIENT_NUM; idx++) {
-               mcClientBit = powergate_partition_info[id].hot_reset_clients[idx];
-               if (mcClientBit == MC_CLIENT_LAST)
-                       break;
+               contention_timeout--;
+       } while ((status != new_state) && (contention_timeout > 0));
 
-               spin_lock_irqsave(&tegra_powergate_lock, flags);
+       spin_unlock_irqrestore(lock, flags);
 
-               rst_ctrl = mc_read(MC_CLIENT_HOTRESET_CTRL);
-               rst_ctrl |= (1 << mcClientBit);
-               mc_write(rst_ctrl, MC_CLIENT_HOTRESET_CTRL);
+       if (status != new_state) {
+               WARN(1, "Could not set powergate %d to %d", id, new_state);
+               return -EBUSY;
+       }
 
-               spin_unlock_irqrestore(&tegra_powergate_lock, flags);
+       trace_power_domain_target(tegra_powergate_get_name(id), new_state,
+                       raw_smp_processor_id());
 
-               do {
-                       udelay(10);
-                       rst_stat = mc_read(MC_CLIENT_HOTRESET_STAT);
-               } while (!(rst_stat & (1 << mcClientBit)));
-       }
+       return 0;
 }
 
-static void mc_flush_done(int id)
+int is_partition_clk_disabled(struct powergate_partition_info *pg_info)
 {
-       u32 idx, rst_ctrl;
-       enum mc_client mcClientBit;
-       unsigned long flags;
+       u32 idx;
+       struct clk *clk;
+       struct partition_clk_info *clk_info;
+       int ret = 0;
 
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
+               clk_info = &pg_info->clk_info[idx];
+               clk = clk_info->clk_ptr;
 
-       for (idx = 0; idx < MAX_HOTRESET_CLIENT_NUM; idx++) {
-               mcClientBit = powergate_partition_info[id].hot_reset_clients[idx];
-               if (mcClientBit == MC_CLIENT_LAST)
+               if (!clk)
                        break;
 
-               spin_lock_irqsave(&tegra_powergate_lock, flags);
-
-               rst_ctrl = mc_read(MC_CLIENT_HOTRESET_CTRL);
-               rst_ctrl &= ~(1 << mcClientBit);
-               mc_write(rst_ctrl, MC_CLIENT_HOTRESET_CTRL);
-
-               spin_unlock_irqrestore(&tegra_powergate_lock, flags);
+               if (clk_info->clk_type != RST_ONLY) {
+                       if (tegra_is_clk_enabled(clk)) {
+                               ret = -1;
+                               break;
+                       }
+               }
        }
 
-       wmb();
+       return ret;
 }
-#else
-static void mc_flush(int id) {}
-static void mc_flush_done(int id) {}
-#endif
 
-static int tegra_powergate_set(int id, bool new_state)
+int powergate_module(int id)
 {
-       bool status;
-       unsigned long flags;
-
-       spin_lock_irqsave(&tegra_powergate_lock, flags);
-
-       status = !!(pmc_read(PWRGATE_STATUS) & (1 << id));
-
-       if (status == new_state) {
-               spin_unlock_irqrestore(&tegra_powergate_lock, flags);
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
                return -EINVAL;
        }
 
-       pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
-
-       spin_unlock_irqrestore(&tegra_powergate_lock, flags);
-
-       return 0;
-}
-
-static int unpowergate_module(int id)
-{
-       if (id < 0 || id >= tegra_num_powerdomains)
+       if (id < 0 || id >= pg_ops->num_powerdomains)
                return -EINVAL;
-       return tegra_powergate_set(id, true);
-}
 
-static int powergate_module(int id)
-{
-       if (id < 0 || id >= tegra_num_powerdomains)
-               return -EINVAL;
+       tegra_powergate_mc_flush(id);
 
-       mc_flush(id);
        return tegra_powergate_set(id, false);
 }
 
-int tegra_powergate_is_powered(int id)
+int unpowergate_module(int id)
 {
-       u32 status;
-
-       if (id < 0 || id >= tegra_num_powerdomains)
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
                return -EINVAL;
+       }
 
-       status = pmc_read(PWRGATE_STATUS) & (1 << id);
-       return !!status;
-}
-
-int tegra_powergate_remove_clamping(int id)
-{
-       u32 mask;
-       if (id < 0 || id >= tegra_num_powerdomains)
+       if (id < 0 || id >= pg_ops->num_powerdomains)
                return -EINVAL;
 
-       /*
-        * PCIE and VDE clamping masks are swapped with respect to their
-        * partition ids
-        */
-       if (id ==  TEGRA_POWERGATE_VDEC)
-               mask = (1 << TEGRA_POWERGATE_PCIE);
-       else if (id == TEGRA_POWERGATE_PCIE)
-               mask = (1 << TEGRA_POWERGATE_VDEC);
-       else
-               mask = (1 << id);
-
-       pmc_write(mask, REMOVE_CLAMPING);
-
-       return 0;
-}
-
-static void get_clk_info(int id)
-{
-       int idx;
-
-       for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               if (!powergate_partition_info[id].clk_info[idx].clk_name)
-                       break;
-               powergate_partition_info[id].
-                               clk_info[idx].clk_ptr =
-                                       tegra_get_clock_by_name(
-                       powergate_partition_info[id].clk_info[idx].clk_name);
-       }
+       return tegra_powergate_set(id, true);
 }
 
-static int partition_clk_enable(int id)
+int partition_clk_enable(struct powergate_partition_info *pg_info)
 {
        int ret;
        u32 idx;
        struct clk *clk;
-
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       struct partition_clk_info *clk_info;
 
        for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               clk = powergate_partition_info[id].clk_info[idx].clk_ptr;
+               clk_info = &pg_info->clk_info[idx];
+               clk = clk_info->clk_ptr;
                if (!clk)
                        break;
 
-               if (!powergate_partition_info[id].clk_info[idx].only_reset) {
-                       ret = clk_prepare_enable(clk);
+               if (clk_info->clk_type != RST_ONLY) {
+                       ret = tegra_clk_prepare_enable(clk);
                        if (ret)
                                goto err_clk_en;
                }
@@ -322,296 +221,453 @@ static int partition_clk_enable(int id)
        return 0;
 
 err_clk_en:
-       WARN(1, "Could not enable clk %s", clk->name);
+       WARN(1, "Could not enable clk %s, error %d", clk->name, ret);
        while (idx--) {
-               if (!powergate_partition_info[id].clk_info[idx].only_reset) {
-                       clk = powergate_partition_info[id].
-                                               clk_info[idx].clk_ptr;
-                       clk_disable_unprepare(clk);
-               }
+               clk_info = &pg_info->clk_info[idx];
+               if (clk_info->clk_type != RST_ONLY)
+                       tegra_clk_disable_unprepare(clk_info->clk_ptr);
        }
 
        return ret;
 }
 
-static int is_partition_clk_disabled(int id)
+void partition_clk_disable(struct powergate_partition_info *pg_info)
 {
        u32 idx;
        struct clk *clk;
-       int ret = 0;
-
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       struct partition_clk_info *clk_info;
 
        for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               clk = powergate_partition_info[id].clk_info[idx].clk_ptr;
+               clk_info = &pg_info->clk_info[idx];
+               clk = clk_info->clk_ptr;
+
                if (!clk)
                        break;
 
-               if (!powergate_partition_info[id].clk_info[idx].only_reset) {
-                       if (tegra_is_clk_enabled(clk)) {
-                               ret = -1;
-                               break;
-                       }
-               }
+               if (clk_info->clk_type != RST_ONLY)
+                       tegra_clk_disable_unprepare(clk);
        }
-
-       return ret;
 }
 
-static void partition_clk_disable(int id)
+void get_clk_info(struct powergate_partition_info *pg_info)
 {
-       u32 idx;
-       struct clk *clk;
-
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       int idx;
 
        for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               clk = powergate_partition_info[id].clk_info[idx].clk_ptr;
-               if (!clk)
+               if (!pg_info->clk_info[idx].clk_name)
                        break;
 
-               if (!powergate_partition_info[id].clk_info[idx].only_reset)
-                       clk_disable_unprepare(clk);
+               pg_info->clk_info[idx].clk_ptr = tegra_get_clock_by_name(
+                       pg_info->clk_info[idx].clk_name);
        }
 }
 
-static void powergate_partition_assert_reset(int id)
+void powergate_partition_assert_reset(struct powergate_partition_info *pg_info)
 {
        u32 idx;
-
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       struct clk *clk_ptr;
+       struct partition_clk_info *clk_info;
 
        for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               if (!powergate_partition_info[id].clk_info[idx].clk_ptr)
+               clk_info = &pg_info->clk_info[idx];
+               clk_ptr = clk_info->clk_ptr;
+
+               if (!clk_ptr)
                        break;
-               tegra_periph_reset_assert(
-                       powergate_partition_info[id].
-                                               clk_info[idx].clk_ptr);
-               }
+
+               if (clk_info->clk_type != CLK_ONLY)
+                       tegra_periph_reset_assert(clk_ptr);
+       }
 }
 
-static void powergate_partition_deassert_reset(int id)
+void powergate_partition_deassert_reset(struct powergate_partition_info *pg_info)
 {
        u32 idx;
-
-       BUG_ON(id < 0 || id >= tegra_num_powerdomains);
+       struct clk *clk_ptr;
+       struct partition_clk_info *clk_info;
 
        for (idx = 0; idx < MAX_CLK_EN_NUM; idx++) {
-               if (!powergate_partition_info[id].clk_info[idx].clk_ptr)
+               clk_info = &pg_info->clk_info[idx];
+               clk_ptr = clk_info->clk_ptr;
+
+               if (!clk_ptr)
                        break;
-               tegra_periph_reset_deassert(
-                       powergate_partition_info[id].clk_info[idx].clk_ptr);
+
+               if (clk_info->clk_type != CLK_ONLY)
+                       tegra_periph_reset_deassert(clk_ptr);
        }
 }
 
-/* Must be called with clk disabled, and returns with clk disabled */
-static int tegra_powergate_reset_module(int id)
+int tegra_powergate_reset_module(struct powergate_partition_info *pg_info)
 {
        int ret;
 
-       powergate_partition_assert_reset(id);
+       powergate_partition_assert_reset(pg_info);
 
        udelay(10);
 
-       ret = partition_clk_enable(id);
+       ret = partition_clk_enable(pg_info);
        if (ret)
                return ret;
 
        udelay(10);
 
-       powergate_partition_deassert_reset(id);
+       powergate_partition_deassert_reset(pg_info);
 
-       partition_clk_disable(id);
+       partition_clk_disable(pg_info);
 
        return 0;
 }
 
-/*
- * Must be called with clk disabled, and returns with clk disabled
- * Drivers should enable clks for partition. Unpowergates only the
- * partition.
- */
-int tegra_unpowergate_partition(int id)
+bool tegra_powergate_check_clamping(int id)
 {
-       int ret;
-
-       /* If first clk_ptr is null, fill clk info for the partition */
-       if (!powergate_partition_info[id].clk_info[0].clk_ptr)
-               get_clk_info(id);
-
-       if (tegra_powergate_is_powered(id))
-               return tegra_powergate_reset_module(id);
+       if (!pg_ops || !pg_ops->powergate_check_clamping) {
+               pr_info("This SOC can't check clamping status\n");
+               return -EINVAL;
+       }
 
-       ret = unpowergate_module(id);
-       if (ret)
-               goto err_power;
+       if (id < 0 || id >= pg_ops->num_powerdomains)
+               return -EINVAL;
 
-       powergate_partition_assert_reset(id);
+       return pg_ops->powergate_check_clamping(id);
+}
 
-       /* Un-Powergating fails if all clks are not enabled */
-       ret = partition_clk_enable(id);
-       if (ret)
-               goto err_clk_on;
+int tegra_powergate_remove_clamping(int id)
+{
+       u32 mask;
+       int contention_timeout = 100;
 
-       udelay(10);
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
 
-       ret = tegra_powergate_remove_clamping(id);
-       if (ret)
-               goto err_clamp;
+       if (id < 0 || id >= pg_ops->num_powerdomains)
+               return -EINVAL;
 
-       udelay(10);
-       powergate_partition_deassert_reset(id);
+       /*
+        * PCIE and VDE clamping masks are swapped with respect to their
+        * partition ids
+        */
+       if (id ==  TEGRA_POWERGATE_VDEC)
+               mask = (1 << TEGRA_POWERGATE_PCIE);
+       else if (id == TEGRA_POWERGATE_PCIE)
+               mask = (1 << TEGRA_POWERGATE_VDEC);
+       else
+               mask = (1 << id);
 
-       mc_flush_done(id);
+       pmc_write(mask, REMOVE_CLAMPING);
+       /* Wait until clamp is removed */
+       do {
+               udelay(1);
+               contention_timeout--;
+       } while ((contention_timeout > 0)
+                       && (pmc_read(PWRGATE_CLAMP_STATUS) & BIT(id)));
 
-       /* Disable all clks enabled earlier. Drivers should enable clks */
-       partition_clk_disable(id);
+       WARN(contention_timeout <= 0, "Couldn't remove clamping");
 
        return 0;
+}
 
-err_clamp:
-       partition_clk_disable(id);
-err_clk_on:
-       powergate_module(id);
-err_power:
-       WARN(1, "Could not Un-Powergate %d", id);
-       return ret;
+static inline bool tegra_powergate_check_skip_list(int id)
+{
+       return pg_ops->powergate_skip ?
+               pg_ops->powergate_skip(id) : false;
 }
 
-/*
- * Must be called with clk disabled, and returns with clk enabled
- * Unpowergates the partition and enables all required clks.
- */
-int tegra_unpowergate_partition_with_clk_on(int id)
+/* EXTERNALY VISIBLE APIS */
+
+bool tegra_powergate_is_powered(int id)
 {
-       int ret = 0;
+       u32 status;
 
-#ifndef CONFIG_ARCH_TEGRA_2x_SOC
-       /* Restrict this functions use to few partitions */
-       BUG_ON(id != TEGRA_POWERGATE_SATA && id != TEGRA_POWERGATE_PCIE);
-#else
-       /* Restrict this functions use to few partitions */
-       BUG_ON(id != TEGRA_POWERGATE_PCIE);
-#endif
+       if (!pg_ops) {
+               pr_debug("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
 
-       ret = tegra_unpowergate_partition(id);
-       if (ret)
-               goto err_unpowergating;
+       if (id < 0 || id >= pg_ops->num_powerdomains)
+               return -EINVAL;
 
-       /* Enable clks for the partition */
-       ret = partition_clk_enable(id);
-       if (ret)
-               goto err_unpowergate_clk;
+       if (pg_ops->powergate_is_powered)
+               return pg_ops->powergate_is_powered(id);
+       else
+               status = pmc_read(PWRGATE_STATUS) & (1 << id);
 
-       return ret;
+       status = pmc_read(PWRGATE_STATUS) & (1 << id);
 
-err_unpowergate_clk:
-       tegra_powergate_partition(id);
-       WARN(1, "Could not Un-Powergate %d, err in enabling clk", id);
-err_unpowergating:
-       WARN(1, "Could not Un-Powergate %d", id);
-       return ret;
+       return !!status;
 }
+EXPORT_SYMBOL(tegra_powergate_is_powered);
+
+int tegra_cpu_powergate_id(int cpuid)
+{
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (cpuid < 0 || cpuid >= pg_ops->num_cpu_domains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (pg_ops->cpu_domains)
+               return pg_ops->cpu_domains[cpuid];
+       else
+               WARN_ON_ONCE("This SOC does not support CPU powergate\n");
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL(tegra_cpu_powergate_id);
 
-/*
- * Must be called with clk disabled. Powergates the partition only
- */
 int tegra_powergate_partition(int id)
 {
-       int ret;
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
 
-       /* If first clk_ptr is null, fill clk info for the partition */
-       if (powergate_partition_info[id].clk_info[0].clk_ptr)
-               get_clk_info(id);
-       powergate_partition_assert_reset(id);
+       if (tegra_powergate_check_skip_list(id))
+               printk_once("%s: %s is in powergate skip list\n", __func__,
+                       tegra_powergate_get_name(id));
 
-       /* Powergating is done only if refcnt of all clks is 0 */
-       ret = is_partition_clk_disabled(id);
-       if (ret)
-               goto err_clk_off;
+       if (pg_ops->powergate_partition)
+               return pg_ops->powergate_partition(id);
+       else
+               WARN_ON_ONCE("This SOC doesn't support powergating");
 
-       ret = powergate_module(id);
-       if (ret)
-               goto err_power_off;
+       return -EINVAL;
+}
+EXPORT_SYMBOL(tegra_powergate_partition);
 
-       return 0;
+int tegra_unpowergate_partition(int id)
+{
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
 
-err_power_off:
-       WARN(1, "Could not Powergate Partition %d", id);
-err_clk_off:
-       WARN(1, "Could not Powergate Partition %d, all clks not disabled", id);
-       return ret;
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (tegra_powergate_check_skip_list(id))
+               printk_once("%s: %s is in powergate skip list\n", __func__,
+                       tegra_powergate_get_name(id));
+
+       if (pg_ops->unpowergate_partition)
+               return pg_ops->unpowergate_partition(id);
+       else
+               WARN_ON_ONCE("This SOC doesn't support un-powergating");
+
+       return -EINVAL;
 }
+EXPORT_SYMBOL(tegra_unpowergate_partition);
 
 int tegra_powergate_partition_with_clk_off(int id)
 {
-       int ret = 0;
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
 
-#ifndef CONFIG_ARCH_TEGRA_2x_SOC
-       /* Restrict functions use to selected partitions */
-       BUG_ON(id != TEGRA_POWERGATE_PCIE && id != TEGRA_POWERGATE_SATA &&
-               id != TEGRA_POWERGATE_3D && id != TEGRA_POWERGATE_3D1 &&
-               id != TEGRA_POWERGATE_MPE);
-#else
-       /* Restrict functions use to selected partitions */
-       BUG_ON(id != TEGRA_POWERGATE_PCIE && id != TEGRA_POWERGATE_MPE &&
-               id != TEGRA_POWERGATE_3D);
-#endif
-       /* Disable clks for the partition */
-       partition_clk_disable(id);
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
 
-       ret = is_partition_clk_disabled(id);
-       if (ret)
-               goto err_powergate_clk;
+       if (tegra_powergate_check_skip_list(id))
+               printk_once("%s: %s is in powergate skip list\n", __func__,
+                       tegra_powergate_get_name(id));
 
-       ret = tegra_powergate_partition(id);
-       if (ret)
-               goto err_powergating;
+       if (pg_ops->powergate_partition_with_clk_off)
+               return pg_ops->powergate_partition_with_clk_off(id);
+       else
+               WARN_ON_ONCE("This SOC doesn't support powergating with clk off");
 
-       return ret;
+       return -EINVAL;
+}
+EXPORT_SYMBOL(tegra_powergate_partition_with_clk_off);
 
-err_powergate_clk:
-       WARN(1, "Could not Powergate Partition %d, all clks not disabled", id);
-err_powergating:
-       partition_clk_enable(id);
-       WARN(1, "Could not Powergate Partition %d", id);
-       return ret;
+int tegra_unpowergate_partition_with_clk_on(int id)
+{
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (tegra_powergate_check_skip_list(id))
+               printk_once("%s: %s is in powergate skip list\n", __func__,
+                       tegra_powergate_get_name(id));
+
+       if (pg_ops->unpowergate_partition_with_clk_on)
+               return pg_ops->unpowergate_partition_with_clk_on(id);
+       else
+               WARN_ON_ONCE("This SOC doesn't support power un-gating with clk on");
+
+       return -EINVAL;
 }
+EXPORT_SYMBOL(tegra_unpowergate_partition_with_clk_on);
 
-int tegra_cpu_powergate_id(int cpuid)
+int tegra_powergate_mc_enable(int id)
 {
-       if (cpuid > 0 && cpuid < tegra_num_cpu_domains)
-               return tegra_cpu_domains[cpuid];
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (pg_ops->powergate_mc_enable)
+               return pg_ops->powergate_mc_enable(id);
+       else
+               WARN_ON_ONCE("This SOC does not support powergate mc enable");
 
        return -EINVAL;
 }
+EXPORT_SYMBOL(tegra_powergate_mc_enable);
 
-int __init tegra_powergate_init(void)
+int tegra_powergate_mc_disable(int id)
 {
-       switch (tegra_chip_id) {
-       case TEGRA20:
-               tegra_num_powerdomains = 7;
-               break;
-       case TEGRA30:
-               tegra_num_powerdomains = 14;
-               tegra_num_cpu_domains = 4;
-               tegra_cpu_domains = tegra30_cpu_domains;
-               break;
-       default:
-               /* Unknown Tegra variant. Disable powergating */
-               tegra_num_powerdomains = 0;
-               break;
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
        }
 
-       return 0;
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (pg_ops->powergate_mc_disable)
+               return pg_ops->powergate_mc_disable(id);
+       else
+               WARN_ON_ONCE("This SOC does not support powergate mc disable");
+
+       return -EINVAL;
 }
+EXPORT_SYMBOL(tegra_powergate_mc_disable);
+
+int tegra_powergate_mc_flush(int id)
+{
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (pg_ops->powergate_mc_flush)
+               return pg_ops->powergate_mc_flush(id);
+       else
+               WARN_ON_ONCE("This SOC does not support powergate mc flush");
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL(tegra_powergate_mc_flush);
+
+int tegra_powergate_mc_flush_done(int id)
+{
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("%s: invalid powergate id\n", __func__);
+               return -EINVAL;
+       }
+
+       if (pg_ops->powergate_mc_flush_done)
+               return pg_ops->powergate_mc_flush_done(id);
+       else
+               WARN_ON_ONCE("This SOC does not support powergate mc flush done");
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL(tegra_powergate_mc_flush_done);
 
 const char *tegra_powergate_get_name(int id)
 {
-       if (id < 0 || id >= tegra_num_powerdomains)
+       if (!pg_ops) {
+               pr_info("This SOC doesn't support powergating\n");
+               return NULL;
+       }
+
+       if (id < 0 || id >= pg_ops->num_powerdomains) {
+               pr_info("invalid powergate id\n");
                return "invalid";
+       }
+
+       if (pg_ops->get_powergate_domain_name)
+               return pg_ops->get_powergate_domain_name(id);
+       else
+               WARN_ON_ONCE("This SOC does not support CPU powergate");
+
+       return "invalid";
+}
+EXPORT_SYMBOL(tegra_powergate_get_name);
+
+int tegra_powergate_init_refcount(void)
+{
+       if ((!pg_ops) || (!pg_ops->powergate_init_refcount))
+               return 0;
+
+       return pg_ops->powergate_init_refcount();
+}
+
+int __init tegra_powergate_init(void)
+{
+       switch (tegra_get_chip_id()) {
+               case TEGRA_CHIPID_TEGRA2:
+                       pg_ops = tegra2_powergate_init_chip_support();
+                       break;
+
+               case TEGRA_CHIPID_TEGRA3:
+                       pg_ops = tegra3_powergate_init_chip_support();
+                       break;
+
+               case TEGRA_CHIPID_TEGRA11:
+                       pg_ops = tegra11x_powergate_init_chip_support();
+                       break;
+
+               case TEGRA_CHIPID_TEGRA14:
+                       pg_ops = tegra14x_powergate_init_chip_support();
+                       break;
+
+               case TEGRA_CHIPID_TEGRA12:
+                       pg_ops = tegra12x_powergate_init_chip_support();
+                       break;
+
+               default:
+                       pg_ops = NULL;
+                       pr_info("%s: Unknown Tegra variant. Disabling powergate\n", __func__);
+                       break;
+       }
 
-       return powergate_partition_info[id].name;
+       tegra_powergate_init_refcount();
+
+       pr_info("%s: DONE\n", __func__);
+
+       return (pg_ops ? 0 : -EINVAL);
 }
 
 #ifdef CONFIG_DEBUG_FS
@@ -619,13 +675,28 @@ const char *tegra_powergate_get_name(int id)
 static int powergate_show(struct seq_file *s, void *data)
 {
        int i;
+       const char *name;
+       bool is_pg_skip;
+
+       if (!pg_ops) {
+               seq_printf(s, "This SOC doesn't support powergating\n");
+               return -EINVAL;
+       }
 
        seq_printf(s, " powergate powered\n");
        seq_printf(s, "------------------\n");
 
-       for (i = 0; i < tegra_num_powerdomains; i++)
-               seq_printf(s, " %9s %7s\n", powergate_partition_info[i].name,
-                       tegra_powergate_is_powered(i) ? "yes" : "no");
+       for (i = 0; i < pg_ops->num_powerdomains; i++) {
+               name = tegra_powergate_get_name(i);
+               if (name) {
+                       is_pg_skip = tegra_powergate_check_skip_list(i);
+                       seq_printf(s, " %9s %7s\n", name,
+                               (is_pg_skip ? "skip" : \
+                               (tegra_powergate_is_powered(i) ? \
+                               "yes" : "no")));
+               }
+       }
+
        return 0;
 }
 
@@ -645,12 +716,10 @@ int __init tegra_powergate_debugfs_init(void)
 {
        struct dentry *d;
 
-       if (powergate_name) {
-               d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
-                       &powergate_fops);
-               if (!d)
-                       return -ENOMEM;
-       }
+       d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
+               &powergate_fops);
+       if (!d)
+               return -ENOMEM;
 
        return 0;
 }