pmc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * drivers/soc/tegra/pmc.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/clk/tegra.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/export.h>
  26. #include <linux/init.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/reboot.h>
  32. #include <linux/reset.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/spinlock.h>
  35. #include <soc/tegra/common.h>
  36. #include <soc/tegra/fuse.h>
  37. #include <soc/tegra/pmc.h>
  38. #define PMC_CNTRL 0x0
  39. #define PMC_CNTRL_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
  40. #define PMC_CNTRL_SYSCLK_OE (1 << 11) /* system clock enable */
  41. #define PMC_CNTRL_SIDE_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
  42. #define PMC_CNTRL_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
  43. #define PMC_CNTRL_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
  44. #define PMC_CNTRL_INTR_POLARITY (1 << 17) /* inverts INTR polarity */
  45. #define DPD_SAMPLE 0x020
  46. #define DPD_SAMPLE_ENABLE (1 << 0)
  47. #define DPD_SAMPLE_DISABLE (0 << 0)
  48. #define PWRGATE_TOGGLE 0x30
  49. #define PWRGATE_TOGGLE_START (1 << 8)
  50. #define REMOVE_CLAMPING 0x34
  51. #define PWRGATE_STATUS 0x38
  52. #define PMC_SCRATCH0 0x50
  53. #define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)
  54. #define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)
  55. #define PMC_SCRATCH0_MODE_RCM (1 << 1)
  56. #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
  57. PMC_SCRATCH0_MODE_BOOTLOADER | \
  58. PMC_SCRATCH0_MODE_RCM)
  59. #define PMC_CPUPWRGOOD_TIMER 0xc8
  60. #define PMC_CPUPWROFF_TIMER 0xcc
  61. #define PMC_SCRATCH41 0x140
  62. #define IO_DPD_REQ 0x1b8
  63. #define IO_DPD_REQ_CODE_IDLE (0 << 30)
  64. #define IO_DPD_REQ_CODE_OFF (1 << 30)
  65. #define IO_DPD_REQ_CODE_ON (2 << 30)
  66. #define IO_DPD_REQ_CODE_MASK (3 << 30)
  67. #define IO_DPD_STATUS 0x1bc
  68. #define IO_DPD2_REQ 0x1c0
  69. #define IO_DPD2_STATUS 0x1c4
  70. #define SEL_DPD_TIM 0x1c8
  71. #define GPU_RG_CNTRL 0x2d4
  72. struct tegra_pmc_soc {
  73. unsigned int num_powergates;
  74. const char *const *powergates;
  75. unsigned int num_cpu_powergates;
  76. const u8 *cpu_powergates;
  77. };
  78. /**
  79. * struct tegra_pmc - NVIDIA Tegra PMC
  80. * @base: pointer to I/O remapped register region
  81. * @clk: pointer to pclk clock
  82. * @rate: currently configured rate of pclk
  83. * @suspend_mode: lowest suspend mode available
  84. * @cpu_good_time: CPU power good time (in microseconds)
  85. * @cpu_off_time: CPU power off time (in microsecends)
  86. * @core_osc_time: core power good OSC time (in microseconds)
  87. * @core_pmu_time: core power good PMU time (in microseconds)
  88. * @core_off_time: core power off time (in microseconds)
  89. * @corereq_high: core power request is active-high
  90. * @sysclkreq_high: system clock request is active-high
  91. * @combined_req: combined power request for CPU & core
  92. * @cpu_pwr_good_en: CPU power good signal is enabled
  93. * @lp0_vec_phys: physical base address of the LP0 warm boot code
  94. * @lp0_vec_size: size of the LP0 warm boot code
  95. * @powergates_lock: mutex for power gate register access
  96. */
  97. struct tegra_pmc {
  98. void __iomem *base;
  99. struct clk *clk;
  100. const struct tegra_pmc_soc *soc;
  101. unsigned long rate;
  102. enum tegra_suspend_mode suspend_mode;
  103. u32 cpu_good_time;
  104. u32 cpu_off_time;
  105. u32 core_osc_time;
  106. u32 core_pmu_time;
  107. u32 core_off_time;
  108. bool corereq_high;
  109. bool sysclkreq_high;
  110. bool combined_req;
  111. bool cpu_pwr_good_en;
  112. u32 lp0_vec_phys;
  113. u32 lp0_vec_size;
  114. struct mutex powergates_lock;
  115. };
  116. static struct tegra_pmc *pmc = &(struct tegra_pmc) {
  117. .base = NULL,
  118. .suspend_mode = TEGRA_SUSPEND_NONE,
  119. };
  120. static u32 tegra_pmc_readl(unsigned long offset)
  121. {
  122. return readl(pmc->base + offset);
  123. }
  124. static void tegra_pmc_writel(u32 value, unsigned long offset)
  125. {
  126. writel(value, pmc->base + offset);
  127. }
  128. /**
  129. * tegra_powergate_set() - set the state of a partition
  130. * @id: partition ID
  131. * @new_state: new state of the partition
  132. */
  133. static int tegra_powergate_set(int id, bool new_state)
  134. {
  135. bool status;
  136. mutex_lock(&pmc->powergates_lock);
  137. status = tegra_pmc_readl(PWRGATE_STATUS) & (1 << id);
  138. if (status == new_state) {
  139. mutex_unlock(&pmc->powergates_lock);
  140. return 0;
  141. }
  142. tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  143. mutex_unlock(&pmc->powergates_lock);
  144. return 0;
  145. }
  146. /**
  147. * tegra_powergate_power_on() - power on partition
  148. * @id: partition ID
  149. */
  150. int tegra_powergate_power_on(int id)
  151. {
  152. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  153. return -EINVAL;
  154. return tegra_powergate_set(id, true);
  155. }
  156. /**
  157. * tegra_powergate_power_off() - power off partition
  158. * @id: partition ID
  159. */
  160. int tegra_powergate_power_off(int id)
  161. {
  162. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  163. return -EINVAL;
  164. return tegra_powergate_set(id, false);
  165. }
  166. EXPORT_SYMBOL(tegra_powergate_power_off);
  167. /**
  168. * tegra_powergate_is_powered() - check if partition is powered
  169. * @id: partition ID
  170. */
  171. int tegra_powergate_is_powered(int id)
  172. {
  173. u32 status;
  174. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  175. return -EINVAL;
  176. status = tegra_pmc_readl(PWRGATE_STATUS) & (1 << id);
  177. return !!status;
  178. }
  179. /**
  180. * tegra_powergate_remove_clamping() - remove power clamps for partition
  181. * @id: partition ID
  182. */
  183. int tegra_powergate_remove_clamping(int id)
  184. {
  185. u32 mask;
  186. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  187. return -EINVAL;
  188. /*
  189. * The Tegra124 GPU has a separate register (with different semantics)
  190. * to remove clamps.
  191. */
  192. if (tegra_get_chip_id() == TEGRA124) {
  193. if (id == TEGRA_POWERGATE_3D) {
  194. tegra_pmc_writel(0, GPU_RG_CNTRL);
  195. return 0;
  196. }
  197. }
  198. /*
  199. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  200. * swapped relatively to the partition ids
  201. */
  202. if (id == TEGRA_POWERGATE_VDEC)
  203. mask = (1 << TEGRA_POWERGATE_PCIE);
  204. else if (id == TEGRA_POWERGATE_PCIE)
  205. mask = (1 << TEGRA_POWERGATE_VDEC);
  206. else
  207. mask = (1 << id);
  208. tegra_pmc_writel(mask, REMOVE_CLAMPING);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(tegra_powergate_remove_clamping);
  212. /**
  213. * tegra_powergate_sequence_power_up() - power up partition
  214. * @id: partition ID
  215. * @clk: clock for partition
  216. * @rst: reset for partition
  217. *
  218. * Must be called with clk disabled, and returns with clk enabled.
  219. */
  220. int tegra_powergate_sequence_power_up(int id, struct clk *clk,
  221. struct reset_control *rst)
  222. {
  223. int ret;
  224. reset_control_assert(rst);
  225. ret = tegra_powergate_power_on(id);
  226. if (ret)
  227. goto err_power;
  228. ret = clk_prepare_enable(clk);
  229. if (ret)
  230. goto err_clk;
  231. usleep_range(10, 20);
  232. ret = tegra_powergate_remove_clamping(id);
  233. if (ret)
  234. goto err_clamp;
  235. usleep_range(10, 20);
  236. reset_control_deassert(rst);
  237. return 0;
  238. err_clamp:
  239. clk_disable_unprepare(clk);
  240. err_clk:
  241. tegra_powergate_power_off(id);
  242. err_power:
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
  246. #ifdef CONFIG_SMP
  247. /**
  248. * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
  249. * @cpuid: CPU partition ID
  250. *
  251. * Returns the partition ID corresponding to the CPU partition ID or a
  252. * negative error code on failure.
  253. */
  254. static int tegra_get_cpu_powergate_id(int cpuid)
  255. {
  256. if (pmc->soc && cpuid > 0 && cpuid < pmc->soc->num_cpu_powergates)
  257. return pmc->soc->cpu_powergates[cpuid];
  258. return -EINVAL;
  259. }
  260. /**
  261. * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
  262. * @cpuid: CPU partition ID
  263. */
  264. bool tegra_pmc_cpu_is_powered(int cpuid)
  265. {
  266. int id;
  267. id = tegra_get_cpu_powergate_id(cpuid);
  268. if (id < 0)
  269. return false;
  270. return tegra_powergate_is_powered(id);
  271. }
  272. /**
  273. * tegra_pmc_cpu_power_on() - power on CPU partition
  274. * @cpuid: CPU partition ID
  275. */
  276. int tegra_pmc_cpu_power_on(int cpuid)
  277. {
  278. int id;
  279. id = tegra_get_cpu_powergate_id(cpuid);
  280. if (id < 0)
  281. return id;
  282. return tegra_powergate_set(id, true);
  283. }
  284. /**
  285. * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
  286. * @cpuid: CPU partition ID
  287. */
  288. int tegra_pmc_cpu_remove_clamping(int cpuid)
  289. {
  290. int id;
  291. id = tegra_get_cpu_powergate_id(cpuid);
  292. if (id < 0)
  293. return id;
  294. return tegra_powergate_remove_clamping(id);
  295. }
  296. #endif /* CONFIG_SMP */
  297. /**
  298. * tegra_pmc_restart() - reboot the system
  299. * @mode: which mode to reboot in
  300. * @cmd: reboot command
  301. */
  302. void tegra_pmc_restart(enum reboot_mode mode, const char *cmd)
  303. {
  304. u32 value;
  305. value = tegra_pmc_readl(PMC_SCRATCH0);
  306. value &= ~PMC_SCRATCH0_MODE_MASK;
  307. if (cmd) {
  308. if (strcmp(cmd, "recovery") == 0)
  309. value |= PMC_SCRATCH0_MODE_RECOVERY;
  310. if (strcmp(cmd, "bootloader") == 0)
  311. value |= PMC_SCRATCH0_MODE_BOOTLOADER;
  312. if (strcmp(cmd, "forced-recovery") == 0)
  313. value |= PMC_SCRATCH0_MODE_RCM;
  314. }
  315. tegra_pmc_writel(value, PMC_SCRATCH0);
  316. value = tegra_pmc_readl(0);
  317. value |= 0x10;
  318. tegra_pmc_writel(value, 0);
  319. }
  320. static int powergate_show(struct seq_file *s, void *data)
  321. {
  322. unsigned int i;
  323. seq_printf(s, " powergate powered\n");
  324. seq_printf(s, "------------------\n");
  325. for (i = 0; i < pmc->soc->num_powergates; i++) {
  326. if (!pmc->soc->powergates[i])
  327. continue;
  328. seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
  329. tegra_powergate_is_powered(i) ? "yes" : "no");
  330. }
  331. return 0;
  332. }
  333. static int powergate_open(struct inode *inode, struct file *file)
  334. {
  335. return single_open(file, powergate_show, inode->i_private);
  336. }
  337. static const struct file_operations powergate_fops = {
  338. .open = powergate_open,
  339. .read = seq_read,
  340. .llseek = seq_lseek,
  341. .release = single_release,
  342. };
  343. static int tegra_powergate_debugfs_init(void)
  344. {
  345. struct dentry *d;
  346. d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  347. &powergate_fops);
  348. if (!d)
  349. return -ENOMEM;
  350. return 0;
  351. }
  352. static int tegra_io_rail_prepare(int id, unsigned long *request,
  353. unsigned long *status, unsigned int *bit)
  354. {
  355. unsigned long rate, value;
  356. struct clk *clk;
  357. *bit = id % 32;
  358. /*
  359. * There are two sets of 30 bits to select IO rails, but bits 30 and
  360. * 31 are control bits rather than IO rail selection bits.
  361. */
  362. if (id > 63 || *bit == 30 || *bit == 31)
  363. return -EINVAL;
  364. if (id < 32) {
  365. *status = IO_DPD_STATUS;
  366. *request = IO_DPD_REQ;
  367. } else {
  368. *status = IO_DPD2_STATUS;
  369. *request = IO_DPD2_REQ;
  370. }
  371. clk = clk_get_sys(NULL, "pclk");
  372. if (IS_ERR(clk))
  373. return PTR_ERR(clk);
  374. rate = clk_get_rate(clk);
  375. clk_put(clk);
  376. tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
  377. /* must be at least 200 ns, in APB (PCLK) clock cycles */
  378. value = DIV_ROUND_UP(1000000000, rate);
  379. value = DIV_ROUND_UP(200, value);
  380. tegra_pmc_writel(value, SEL_DPD_TIM);
  381. return 0;
  382. }
  383. static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
  384. unsigned long val, unsigned long timeout)
  385. {
  386. unsigned long value;
  387. timeout = jiffies + msecs_to_jiffies(timeout);
  388. while (time_after(timeout, jiffies)) {
  389. value = tegra_pmc_readl(offset);
  390. if ((value & mask) == val)
  391. return 0;
  392. usleep_range(250, 1000);
  393. }
  394. return -ETIMEDOUT;
  395. }
  396. static void tegra_io_rail_unprepare(void)
  397. {
  398. tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
  399. }
  400. int tegra_io_rail_power_on(int id)
  401. {
  402. unsigned long request, status, value;
  403. unsigned int bit, mask;
  404. int err;
  405. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  406. if (err < 0)
  407. return err;
  408. mask = 1 << bit;
  409. value = tegra_pmc_readl(request);
  410. value |= mask;
  411. value &= ~IO_DPD_REQ_CODE_MASK;
  412. value |= IO_DPD_REQ_CODE_OFF;
  413. tegra_pmc_writel(value, request);
  414. err = tegra_io_rail_poll(status, mask, 0, 250);
  415. if (err < 0)
  416. return err;
  417. tegra_io_rail_unprepare();
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(tegra_io_rail_power_on);
  421. int tegra_io_rail_power_off(int id)
  422. {
  423. unsigned long request, status, value;
  424. unsigned int bit, mask;
  425. int err;
  426. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  427. if (err < 0)
  428. return err;
  429. mask = 1 << bit;
  430. value = tegra_pmc_readl(request);
  431. value |= mask;
  432. value &= ~IO_DPD_REQ_CODE_MASK;
  433. value |= IO_DPD_REQ_CODE_ON;
  434. tegra_pmc_writel(value, request);
  435. err = tegra_io_rail_poll(status, mask, mask, 250);
  436. if (err < 0)
  437. return err;
  438. tegra_io_rail_unprepare();
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(tegra_io_rail_power_off);
  442. #ifdef CONFIG_PM_SLEEP
  443. enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
  444. {
  445. return pmc->suspend_mode;
  446. }
  447. void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
  448. {
  449. if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
  450. return;
  451. pmc->suspend_mode = mode;
  452. }
  453. void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  454. {
  455. unsigned long long rate = 0;
  456. u32 value;
  457. switch (mode) {
  458. case TEGRA_SUSPEND_LP1:
  459. rate = 32768;
  460. break;
  461. case TEGRA_SUSPEND_LP2:
  462. rate = clk_get_rate(pmc->clk);
  463. break;
  464. default:
  465. break;
  466. }
  467. if (WARN_ON_ONCE(rate == 0))
  468. rate = 100000000;
  469. if (rate != pmc->rate) {
  470. u64 ticks;
  471. ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
  472. do_div(ticks, USEC_PER_SEC);
  473. tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
  474. ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
  475. do_div(ticks, USEC_PER_SEC);
  476. tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
  477. wmb();
  478. pmc->rate = rate;
  479. }
  480. value = tegra_pmc_readl(PMC_CNTRL);
  481. value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
  482. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  483. tegra_pmc_writel(value, PMC_CNTRL);
  484. }
  485. #endif
  486. static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
  487. {
  488. u32 value, values[2];
  489. if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
  490. } else {
  491. switch (value) {
  492. case 0:
  493. pmc->suspend_mode = TEGRA_SUSPEND_LP0;
  494. break;
  495. case 1:
  496. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  497. break;
  498. case 2:
  499. pmc->suspend_mode = TEGRA_SUSPEND_LP2;
  500. break;
  501. default:
  502. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  503. break;
  504. }
  505. }
  506. pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
  507. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
  508. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  509. pmc->cpu_good_time = value;
  510. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
  511. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  512. pmc->cpu_off_time = value;
  513. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  514. values, ARRAY_SIZE(values)))
  515. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  516. pmc->core_osc_time = values[0];
  517. pmc->core_pmu_time = values[1];
  518. if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
  519. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  520. pmc->core_off_time = value;
  521. pmc->corereq_high = of_property_read_bool(np,
  522. "nvidia,core-power-req-active-high");
  523. pmc->sysclkreq_high = of_property_read_bool(np,
  524. "nvidia,sys-clock-req-active-high");
  525. pmc->combined_req = of_property_read_bool(np,
  526. "nvidia,combined-power-req");
  527. pmc->cpu_pwr_good_en = of_property_read_bool(np,
  528. "nvidia,cpu-pwr-good-en");
  529. if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
  530. ARRAY_SIZE(values)))
  531. if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
  532. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  533. pmc->lp0_vec_phys = values[0];
  534. pmc->lp0_vec_size = values[1];
  535. return 0;
  536. }
  537. static void tegra_pmc_init(struct tegra_pmc *pmc)
  538. {
  539. u32 value;
  540. /* Always enable CPU power request */
  541. value = tegra_pmc_readl(PMC_CNTRL);
  542. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  543. tegra_pmc_writel(value, PMC_CNTRL);
  544. value = tegra_pmc_readl(PMC_CNTRL);
  545. if (pmc->sysclkreq_high)
  546. value &= ~PMC_CNTRL_SYSCLK_POLARITY;
  547. else
  548. value |= PMC_CNTRL_SYSCLK_POLARITY;
  549. /* configure the output polarity while the request is tristated */
  550. tegra_pmc_writel(value, PMC_CNTRL);
  551. /* now enable the request */
  552. value = tegra_pmc_readl(PMC_CNTRL);
  553. value |= PMC_CNTRL_SYSCLK_OE;
  554. tegra_pmc_writel(value, PMC_CNTRL);
  555. }
  556. static int tegra_pmc_probe(struct platform_device *pdev)
  557. {
  558. void __iomem *base = pmc->base;
  559. struct resource *res;
  560. int err;
  561. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  562. if (err < 0)
  563. return err;
  564. /* take over the memory region from the early initialization */
  565. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  566. pmc->base = devm_ioremap_resource(&pdev->dev, res);
  567. if (IS_ERR(pmc->base))
  568. return PTR_ERR(pmc->base);
  569. iounmap(base);
  570. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  571. if (IS_ERR(pmc->clk)) {
  572. err = PTR_ERR(pmc->clk);
  573. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  574. return err;
  575. }
  576. tegra_pmc_init(pmc);
  577. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  578. err = tegra_powergate_debugfs_init();
  579. if (err < 0)
  580. return err;
  581. }
  582. return 0;
  583. }
  584. #ifdef CONFIG_PM_SLEEP
  585. static int tegra_pmc_suspend(struct device *dev)
  586. {
  587. tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
  588. return 0;
  589. }
  590. static int tegra_pmc_resume(struct device *dev)
  591. {
  592. tegra_pmc_writel(0x0, PMC_SCRATCH41);
  593. return 0;
  594. }
  595. #endif
  596. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  597. static const char * const tegra20_powergates[] = {
  598. [TEGRA_POWERGATE_CPU] = "cpu",
  599. [TEGRA_POWERGATE_3D] = "3d",
  600. [TEGRA_POWERGATE_VENC] = "venc",
  601. [TEGRA_POWERGATE_VDEC] = "vdec",
  602. [TEGRA_POWERGATE_PCIE] = "pcie",
  603. [TEGRA_POWERGATE_L2] = "l2",
  604. [TEGRA_POWERGATE_MPE] = "mpe",
  605. };
  606. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  607. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  608. .powergates = tegra20_powergates,
  609. .num_cpu_powergates = 0,
  610. .cpu_powergates = NULL,
  611. };
  612. static const char * const tegra30_powergates[] = {
  613. [TEGRA_POWERGATE_CPU] = "cpu0",
  614. [TEGRA_POWERGATE_3D] = "3d0",
  615. [TEGRA_POWERGATE_VENC] = "venc",
  616. [TEGRA_POWERGATE_VDEC] = "vdec",
  617. [TEGRA_POWERGATE_PCIE] = "pcie",
  618. [TEGRA_POWERGATE_L2] = "l2",
  619. [TEGRA_POWERGATE_MPE] = "mpe",
  620. [TEGRA_POWERGATE_HEG] = "heg",
  621. [TEGRA_POWERGATE_SATA] = "sata",
  622. [TEGRA_POWERGATE_CPU1] = "cpu1",
  623. [TEGRA_POWERGATE_CPU2] = "cpu2",
  624. [TEGRA_POWERGATE_CPU3] = "cpu3",
  625. [TEGRA_POWERGATE_CELP] = "celp",
  626. [TEGRA_POWERGATE_3D1] = "3d1",
  627. };
  628. static const u8 tegra30_cpu_powergates[] = {
  629. TEGRA_POWERGATE_CPU,
  630. TEGRA_POWERGATE_CPU1,
  631. TEGRA_POWERGATE_CPU2,
  632. TEGRA_POWERGATE_CPU3,
  633. };
  634. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  635. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  636. .powergates = tegra30_powergates,
  637. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  638. .cpu_powergates = tegra30_cpu_powergates,
  639. };
  640. static const char * const tegra114_powergates[] = {
  641. [TEGRA_POWERGATE_CPU] = "crail",
  642. [TEGRA_POWERGATE_3D] = "3d",
  643. [TEGRA_POWERGATE_VENC] = "venc",
  644. [TEGRA_POWERGATE_VDEC] = "vdec",
  645. [TEGRA_POWERGATE_MPE] = "mpe",
  646. [TEGRA_POWERGATE_HEG] = "heg",
  647. [TEGRA_POWERGATE_CPU1] = "cpu1",
  648. [TEGRA_POWERGATE_CPU2] = "cpu2",
  649. [TEGRA_POWERGATE_CPU3] = "cpu3",
  650. [TEGRA_POWERGATE_CELP] = "celp",
  651. [TEGRA_POWERGATE_CPU0] = "cpu0",
  652. [TEGRA_POWERGATE_C0NC] = "c0nc",
  653. [TEGRA_POWERGATE_C1NC] = "c1nc",
  654. [TEGRA_POWERGATE_DIS] = "dis",
  655. [TEGRA_POWERGATE_DISB] = "disb",
  656. [TEGRA_POWERGATE_XUSBA] = "xusba",
  657. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  658. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  659. };
  660. static const u8 tegra114_cpu_powergates[] = {
  661. TEGRA_POWERGATE_CPU0,
  662. TEGRA_POWERGATE_CPU1,
  663. TEGRA_POWERGATE_CPU2,
  664. TEGRA_POWERGATE_CPU3,
  665. };
  666. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  667. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  668. .powergates = tegra114_powergates,
  669. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  670. .cpu_powergates = tegra114_cpu_powergates,
  671. };
  672. static const char * const tegra124_powergates[] = {
  673. [TEGRA_POWERGATE_CPU] = "crail",
  674. [TEGRA_POWERGATE_3D] = "3d",
  675. [TEGRA_POWERGATE_VENC] = "venc",
  676. [TEGRA_POWERGATE_PCIE] = "pcie",
  677. [TEGRA_POWERGATE_VDEC] = "vdec",
  678. [TEGRA_POWERGATE_L2] = "l2",
  679. [TEGRA_POWERGATE_MPE] = "mpe",
  680. [TEGRA_POWERGATE_HEG] = "heg",
  681. [TEGRA_POWERGATE_SATA] = "sata",
  682. [TEGRA_POWERGATE_CPU1] = "cpu1",
  683. [TEGRA_POWERGATE_CPU2] = "cpu2",
  684. [TEGRA_POWERGATE_CPU3] = "cpu3",
  685. [TEGRA_POWERGATE_CELP] = "celp",
  686. [TEGRA_POWERGATE_CPU0] = "cpu0",
  687. [TEGRA_POWERGATE_C0NC] = "c0nc",
  688. [TEGRA_POWERGATE_C1NC] = "c1nc",
  689. [TEGRA_POWERGATE_SOR] = "sor",
  690. [TEGRA_POWERGATE_DIS] = "dis",
  691. [TEGRA_POWERGATE_DISB] = "disb",
  692. [TEGRA_POWERGATE_XUSBA] = "xusba",
  693. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  694. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  695. [TEGRA_POWERGATE_VIC] = "vic",
  696. [TEGRA_POWERGATE_IRAM] = "iram",
  697. };
  698. static const u8 tegra124_cpu_powergates[] = {
  699. TEGRA_POWERGATE_CPU0,
  700. TEGRA_POWERGATE_CPU1,
  701. TEGRA_POWERGATE_CPU2,
  702. TEGRA_POWERGATE_CPU3,
  703. };
  704. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  705. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  706. .powergates = tegra124_powergates,
  707. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  708. .cpu_powergates = tegra124_cpu_powergates,
  709. };
  710. static const struct of_device_id tegra_pmc_match[] = {
  711. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  712. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  713. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  714. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  715. { }
  716. };
  717. static struct platform_driver tegra_pmc_driver = {
  718. .driver = {
  719. .name = "tegra-pmc",
  720. .suppress_bind_attrs = true,
  721. .of_match_table = tegra_pmc_match,
  722. .pm = &tegra_pmc_pm_ops,
  723. },
  724. .probe = tegra_pmc_probe,
  725. };
  726. module_platform_driver(tegra_pmc_driver);
  727. /*
  728. * Early initialization to allow access to registers in the very early boot
  729. * process.
  730. */
  731. static int __init tegra_pmc_early_init(void)
  732. {
  733. const struct of_device_id *match;
  734. struct device_node *np;
  735. struct resource regs;
  736. bool invert;
  737. u32 value;
  738. if (!soc_is_tegra())
  739. return 0;
  740. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  741. if (!np) {
  742. pr_warn("PMC device node not found, disabling powergating\n");
  743. regs.start = 0x7000e400;
  744. regs.end = 0x7000e7ff;
  745. regs.flags = IORESOURCE_MEM;
  746. pr_warn("Using memory region %pR\n", &regs);
  747. } else {
  748. pmc->soc = match->data;
  749. }
  750. if (of_address_to_resource(np, 0, &regs) < 0) {
  751. pr_err("failed to get PMC registers\n");
  752. return -ENXIO;
  753. }
  754. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  755. if (!pmc->base) {
  756. pr_err("failed to map PMC registers\n");
  757. return -ENXIO;
  758. }
  759. mutex_init(&pmc->powergates_lock);
  760. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  761. value = tegra_pmc_readl(PMC_CNTRL);
  762. if (invert)
  763. value |= PMC_CNTRL_INTR_POLARITY;
  764. else
  765. value &= ~PMC_CNTRL_INTR_POLARITY;
  766. tegra_pmc_writel(value, PMC_CNTRL);
  767. return 0;
  768. }
  769. early_initcall(tegra_pmc_early_init);