powergate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include "fuse.h"
  32. #define PWRGATE_TOGGLE 0x30
  33. #define PWRGATE_TOGGLE_START (1 << 8)
  34. #define REMOVE_CLAMPING 0x34
  35. #define PWRGATE_STATUS 0x38
  36. static int tegra_num_powerdomains;
  37. static int tegra_num_cpu_domains;
  38. static u8 *tegra_cpu_domains;
  39. static u8 tegra30_cpu_domains[] = {
  40. TEGRA_POWERGATE_CPU0,
  41. TEGRA_POWERGATE_CPU1,
  42. TEGRA_POWERGATE_CPU2,
  43. TEGRA_POWERGATE_CPU3,
  44. };
  45. static DEFINE_SPINLOCK(tegra_powergate_lock);
  46. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  47. static u32 pmc_read(unsigned long reg)
  48. {
  49. return readl(pmc + reg);
  50. }
  51. static void pmc_write(u32 val, unsigned long reg)
  52. {
  53. writel(val, pmc + reg);
  54. }
  55. static int tegra_powergate_set(int id, bool new_state)
  56. {
  57. bool status;
  58. unsigned long flags;
  59. spin_lock_irqsave(&tegra_powergate_lock, flags);
  60. status = pmc_read(PWRGATE_STATUS) & (1 << id);
  61. if (status == new_state) {
  62. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  63. return -EINVAL;
  64. }
  65. pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  66. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  67. return 0;
  68. }
  69. int tegra_powergate_power_on(int id)
  70. {
  71. if (id < 0 || id >= tegra_num_powerdomains)
  72. return -EINVAL;
  73. return tegra_powergate_set(id, true);
  74. }
  75. int tegra_powergate_power_off(int id)
  76. {
  77. if (id < 0 || id >= tegra_num_powerdomains)
  78. return -EINVAL;
  79. return tegra_powergate_set(id, false);
  80. }
  81. int tegra_powergate_is_powered(int id)
  82. {
  83. u32 status;
  84. if (id < 0 || id >= tegra_num_powerdomains)
  85. return -EINVAL;
  86. status = pmc_read(PWRGATE_STATUS) & (1 << id);
  87. return !!status;
  88. }
  89. int tegra_powergate_remove_clamping(int id)
  90. {
  91. u32 mask;
  92. if (id < 0 || id >= tegra_num_powerdomains)
  93. return -EINVAL;
  94. /*
  95. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  96. * swapped relatively to the partition ids
  97. */
  98. if (id == TEGRA_POWERGATE_VDEC)
  99. mask = (1 << TEGRA_POWERGATE_PCIE);
  100. else if (id == TEGRA_POWERGATE_PCIE)
  101. mask = (1 << TEGRA_POWERGATE_VDEC);
  102. else
  103. mask = (1 << id);
  104. pmc_write(mask, REMOVE_CLAMPING);
  105. return 0;
  106. }
  107. /* Must be called with clk disabled, and returns with clk enabled */
  108. int tegra_powergate_sequence_power_up(int id, struct clk *clk)
  109. {
  110. int ret;
  111. tegra_periph_reset_assert(clk);
  112. ret = tegra_powergate_power_on(id);
  113. if (ret)
  114. goto err_power;
  115. ret = clk_enable(clk);
  116. if (ret)
  117. goto err_clk;
  118. udelay(10);
  119. ret = tegra_powergate_remove_clamping(id);
  120. if (ret)
  121. goto err_clamp;
  122. udelay(10);
  123. tegra_periph_reset_deassert(clk);
  124. return 0;
  125. err_clamp:
  126. clk_disable(clk);
  127. err_clk:
  128. tegra_powergate_power_off(id);
  129. err_power:
  130. return ret;
  131. }
  132. int tegra_cpu_powergate_id(int cpuid)
  133. {
  134. if (cpuid > 0 && cpuid < tegra_num_cpu_domains)
  135. return tegra_cpu_domains[cpuid];
  136. return -EINVAL;
  137. }
  138. int __init tegra_powergate_init(void)
  139. {
  140. switch (tegra_chip_id) {
  141. case TEGRA20:
  142. tegra_num_powerdomains = 7;
  143. break;
  144. case TEGRA30:
  145. tegra_num_powerdomains = 14;
  146. tegra_num_cpu_domains = 4;
  147. tegra_cpu_domains = tegra30_cpu_domains;
  148. break;
  149. default:
  150. /* Unknown Tegra variant. Disable powergating */
  151. tegra_num_powerdomains = 0;
  152. break;
  153. }
  154. return 0;
  155. }
  156. #ifdef CONFIG_DEBUG_FS
  157. static const char * const powergate_name[] = {
  158. [TEGRA_POWERGATE_CPU] = "cpu",
  159. [TEGRA_POWERGATE_3D] = "3d",
  160. [TEGRA_POWERGATE_VENC] = "venc",
  161. [TEGRA_POWERGATE_VDEC] = "vdec",
  162. [TEGRA_POWERGATE_PCIE] = "pcie",
  163. [TEGRA_POWERGATE_L2] = "l2",
  164. [TEGRA_POWERGATE_MPE] = "mpe",
  165. };
  166. static int powergate_show(struct seq_file *s, void *data)
  167. {
  168. int i;
  169. seq_printf(s, " powergate powered\n");
  170. seq_printf(s, "------------------\n");
  171. for (i = 0; i < tegra_num_powerdomains; i++)
  172. seq_printf(s, " %9s %7s\n", powergate_name[i],
  173. tegra_powergate_is_powered(i) ? "yes" : "no");
  174. return 0;
  175. }
  176. static int powergate_open(struct inode *inode, struct file *file)
  177. {
  178. return single_open(file, powergate_show, inode->i_private);
  179. }
  180. static const struct file_operations powergate_fops = {
  181. .open = powergate_open,
  182. .read = seq_read,
  183. .llseek = seq_lseek,
  184. .release = single_release,
  185. };
  186. static int __init powergate_debugfs_init(void)
  187. {
  188. struct dentry *d;
  189. int err = -ENOMEM;
  190. d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  191. &powergate_fops);
  192. if (!d)
  193. return -ENOMEM;
  194. return err;
  195. }
  196. late_initcall(powergate_debugfs_init);
  197. #endif