powergate.c 6.8 KB

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