powergate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * drivers/powergate/tegra-powergate.c
  3. *
  4. * Copyright (c) 2010 Google, Inc
  5. *
  6. * Author:
  7. * Colin Cross <ccross@google.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/clk.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/spinlock.h>
  28. #include <mach/clk.h>
  29. #include <mach/iomap.h>
  30. #include <mach/powergate.h>
  31. #define PWRGATE_TOGGLE 0x30
  32. #define PWRGATE_TOGGLE_START (1 << 8)
  33. #define REMOVE_CLAMPING 0x34
  34. #define PWRGATE_STATUS 0x38
  35. static DEFINE_SPINLOCK(tegra_powergate_lock);
  36. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  37. static u32 pmc_read(unsigned long reg)
  38. {
  39. return readl(pmc + reg);
  40. }
  41. static void pmc_write(u32 val, unsigned long reg)
  42. {
  43. writel(val, pmc + reg);
  44. }
  45. static int tegra_powergate_set(int id, bool new_state)
  46. {
  47. bool status;
  48. unsigned long flags;
  49. spin_lock_irqsave(&tegra_powergate_lock, flags);
  50. status = pmc_read(PWRGATE_STATUS) & (1 << id);
  51. if (status == new_state) {
  52. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  53. return -EINVAL;
  54. }
  55. pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  56. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  57. return 0;
  58. }
  59. int tegra_powergate_power_on(int id)
  60. {
  61. if (id < 0 || id >= TEGRA_NUM_POWERGATE)
  62. return -EINVAL;
  63. return tegra_powergate_set(id, true);
  64. }
  65. int tegra_powergate_power_off(int id)
  66. {
  67. if (id < 0 || id >= TEGRA_NUM_POWERGATE)
  68. return -EINVAL;
  69. return tegra_powergate_set(id, false);
  70. }
  71. bool tegra_powergate_is_powered(int id)
  72. {
  73. u32 status;
  74. if (id < 0 || id >= TEGRA_NUM_POWERGATE)
  75. return -EINVAL;
  76. status = pmc_read(PWRGATE_STATUS) & (1 << id);
  77. return !!status;
  78. }
  79. int tegra_powergate_remove_clamping(int id)
  80. {
  81. u32 mask;
  82. if (id < 0 || id >= TEGRA_NUM_POWERGATE)
  83. return -EINVAL;
  84. /*
  85. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  86. * swapped relatively to the partition ids
  87. */
  88. if (id == TEGRA_POWERGATE_VDEC)
  89. mask = (1 << TEGRA_POWERGATE_PCIE);
  90. else if (id == TEGRA_POWERGATE_PCIE)
  91. mask = (1 << TEGRA_POWERGATE_VDEC);
  92. else
  93. mask = (1 << id);
  94. pmc_write(mask, REMOVE_CLAMPING);
  95. return 0;
  96. }
  97. /* Must be called with clk disabled, and returns with clk enabled */
  98. int tegra_powergate_sequence_power_up(int id, struct clk *clk)
  99. {
  100. int ret;
  101. tegra_periph_reset_assert(clk);
  102. ret = tegra_powergate_power_on(id);
  103. if (ret)
  104. goto err_power;
  105. ret = clk_enable(clk);
  106. if (ret)
  107. goto err_clk;
  108. udelay(10);
  109. ret = tegra_powergate_remove_clamping(id);
  110. if (ret)
  111. goto err_clamp;
  112. udelay(10);
  113. tegra_periph_reset_deassert(clk);
  114. return 0;
  115. err_clamp:
  116. clk_disable(clk);
  117. err_clk:
  118. tegra_powergate_power_off(id);
  119. err_power:
  120. return ret;
  121. }
  122. #ifdef CONFIG_DEBUG_FS
  123. static const char * const powergate_name[] = {
  124. [TEGRA_POWERGATE_CPU] = "cpu",
  125. [TEGRA_POWERGATE_3D] = "3d",
  126. [TEGRA_POWERGATE_VENC] = "venc",
  127. [TEGRA_POWERGATE_VDEC] = "vdec",
  128. [TEGRA_POWERGATE_PCIE] = "pcie",
  129. [TEGRA_POWERGATE_L2] = "l2",
  130. [TEGRA_POWERGATE_MPE] = "mpe",
  131. };
  132. static int powergate_show(struct seq_file *s, void *data)
  133. {
  134. int i;
  135. seq_printf(s, " powergate powered\n");
  136. seq_printf(s, "------------------\n");
  137. for (i = 0; i < TEGRA_NUM_POWERGATE; i++)
  138. seq_printf(s, " %9s %7s\n", powergate_name[i],
  139. tegra_powergate_is_powered(i) ? "yes" : "no");
  140. return 0;
  141. }
  142. static int powergate_open(struct inode *inode, struct file *file)
  143. {
  144. return single_open(file, powergate_show, inode->i_private);
  145. }
  146. static const struct file_operations powergate_fops = {
  147. .open = powergate_open,
  148. .read = seq_read,
  149. .llseek = seq_lseek,
  150. .release = single_release,
  151. };
  152. static int __init powergate_debugfs_init(void)
  153. {
  154. struct dentry *d;
  155. int err = -ENOMEM;
  156. d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  157. &powergate_fops);
  158. if (!d)
  159. return -ENOMEM;
  160. return err;
  161. }
  162. late_initcall(powergate_debugfs_init);
  163. #endif