powergate.c 5.8 KB

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