pmc.c 25 KB

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