pmc.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. static int tegra_pmc_restart_notify(struct notifier_block *this,
  314. unsigned long action, void *data)
  315. {
  316. const char *cmd = data;
  317. u32 value;
  318. value = tegra_pmc_readl(PMC_SCRATCH0);
  319. value &= ~PMC_SCRATCH0_MODE_MASK;
  320. if (cmd) {
  321. if (strcmp(cmd, "recovery") == 0)
  322. value |= PMC_SCRATCH0_MODE_RECOVERY;
  323. if (strcmp(cmd, "bootloader") == 0)
  324. value |= PMC_SCRATCH0_MODE_BOOTLOADER;
  325. if (strcmp(cmd, "forced-recovery") == 0)
  326. value |= PMC_SCRATCH0_MODE_RCM;
  327. }
  328. tegra_pmc_writel(value, PMC_SCRATCH0);
  329. value = tegra_pmc_readl(0);
  330. value |= 0x10;
  331. tegra_pmc_writel(value, 0);
  332. return NOTIFY_DONE;
  333. }
  334. static struct notifier_block tegra_pmc_restart_handler = {
  335. .notifier_call = tegra_pmc_restart_notify,
  336. .priority = 128,
  337. };
  338. static int powergate_show(struct seq_file *s, void *data)
  339. {
  340. unsigned int i;
  341. seq_printf(s, " powergate powered\n");
  342. seq_printf(s, "------------------\n");
  343. for (i = 0; i < pmc->soc->num_powergates; i++) {
  344. if (!pmc->soc->powergates[i])
  345. continue;
  346. seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
  347. tegra_powergate_is_powered(i) ? "yes" : "no");
  348. }
  349. return 0;
  350. }
  351. static int powergate_open(struct inode *inode, struct file *file)
  352. {
  353. return single_open(file, powergate_show, inode->i_private);
  354. }
  355. static const struct file_operations powergate_fops = {
  356. .open = powergate_open,
  357. .read = seq_read,
  358. .llseek = seq_lseek,
  359. .release = single_release,
  360. };
  361. static int tegra_powergate_debugfs_init(void)
  362. {
  363. struct dentry *d;
  364. d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  365. &powergate_fops);
  366. if (!d)
  367. return -ENOMEM;
  368. return 0;
  369. }
  370. static int tegra_io_rail_prepare(int id, unsigned long *request,
  371. unsigned long *status, unsigned int *bit)
  372. {
  373. unsigned long rate, value;
  374. struct clk *clk;
  375. *bit = id % 32;
  376. /*
  377. * There are two sets of 30 bits to select IO rails, but bits 30 and
  378. * 31 are control bits rather than IO rail selection bits.
  379. */
  380. if (id > 63 || *bit == 30 || *bit == 31)
  381. return -EINVAL;
  382. if (id < 32) {
  383. *status = IO_DPD_STATUS;
  384. *request = IO_DPD_REQ;
  385. } else {
  386. *status = IO_DPD2_STATUS;
  387. *request = IO_DPD2_REQ;
  388. }
  389. clk = clk_get_sys(NULL, "pclk");
  390. if (IS_ERR(clk))
  391. return PTR_ERR(clk);
  392. rate = clk_get_rate(clk);
  393. clk_put(clk);
  394. tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
  395. /* must be at least 200 ns, in APB (PCLK) clock cycles */
  396. value = DIV_ROUND_UP(1000000000, rate);
  397. value = DIV_ROUND_UP(200, value);
  398. tegra_pmc_writel(value, SEL_DPD_TIM);
  399. return 0;
  400. }
  401. static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
  402. unsigned long val, unsigned long timeout)
  403. {
  404. unsigned long value;
  405. timeout = jiffies + msecs_to_jiffies(timeout);
  406. while (time_after(timeout, jiffies)) {
  407. value = tegra_pmc_readl(offset);
  408. if ((value & mask) == val)
  409. return 0;
  410. usleep_range(250, 1000);
  411. }
  412. return -ETIMEDOUT;
  413. }
  414. static void tegra_io_rail_unprepare(void)
  415. {
  416. tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
  417. }
  418. int tegra_io_rail_power_on(int id)
  419. {
  420. unsigned long request, status, value;
  421. unsigned int bit, mask;
  422. int err;
  423. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  424. if (err < 0)
  425. return err;
  426. mask = 1 << bit;
  427. value = tegra_pmc_readl(request);
  428. value |= mask;
  429. value &= ~IO_DPD_REQ_CODE_MASK;
  430. value |= IO_DPD_REQ_CODE_OFF;
  431. tegra_pmc_writel(value, request);
  432. err = tegra_io_rail_poll(status, mask, 0, 250);
  433. if (err < 0)
  434. return err;
  435. tegra_io_rail_unprepare();
  436. return 0;
  437. }
  438. EXPORT_SYMBOL(tegra_io_rail_power_on);
  439. int tegra_io_rail_power_off(int id)
  440. {
  441. unsigned long request, status, value;
  442. unsigned int bit, mask;
  443. int err;
  444. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  445. if (err < 0)
  446. return err;
  447. mask = 1 << bit;
  448. value = tegra_pmc_readl(request);
  449. value |= mask;
  450. value &= ~IO_DPD_REQ_CODE_MASK;
  451. value |= IO_DPD_REQ_CODE_ON;
  452. tegra_pmc_writel(value, request);
  453. err = tegra_io_rail_poll(status, mask, mask, 250);
  454. if (err < 0)
  455. return err;
  456. tegra_io_rail_unprepare();
  457. return 0;
  458. }
  459. EXPORT_SYMBOL(tegra_io_rail_power_off);
  460. #ifdef CONFIG_PM_SLEEP
  461. enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
  462. {
  463. return pmc->suspend_mode;
  464. }
  465. void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
  466. {
  467. if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
  468. return;
  469. pmc->suspend_mode = mode;
  470. }
  471. void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  472. {
  473. unsigned long long rate = 0;
  474. u32 value;
  475. switch (mode) {
  476. case TEGRA_SUSPEND_LP1:
  477. rate = 32768;
  478. break;
  479. case TEGRA_SUSPEND_LP2:
  480. rate = clk_get_rate(pmc->clk);
  481. break;
  482. default:
  483. break;
  484. }
  485. if (WARN_ON_ONCE(rate == 0))
  486. rate = 100000000;
  487. if (rate != pmc->rate) {
  488. u64 ticks;
  489. ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
  490. do_div(ticks, USEC_PER_SEC);
  491. tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
  492. ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
  493. do_div(ticks, USEC_PER_SEC);
  494. tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
  495. wmb();
  496. pmc->rate = rate;
  497. }
  498. value = tegra_pmc_readl(PMC_CNTRL);
  499. value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
  500. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  501. tegra_pmc_writel(value, PMC_CNTRL);
  502. }
  503. #endif
  504. static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
  505. {
  506. u32 value, values[2];
  507. if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
  508. } else {
  509. switch (value) {
  510. case 0:
  511. pmc->suspend_mode = TEGRA_SUSPEND_LP0;
  512. break;
  513. case 1:
  514. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  515. break;
  516. case 2:
  517. pmc->suspend_mode = TEGRA_SUSPEND_LP2;
  518. break;
  519. default:
  520. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  521. break;
  522. }
  523. }
  524. pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
  525. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
  526. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  527. pmc->cpu_good_time = value;
  528. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
  529. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  530. pmc->cpu_off_time = value;
  531. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  532. values, ARRAY_SIZE(values)))
  533. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  534. pmc->core_osc_time = values[0];
  535. pmc->core_pmu_time = values[1];
  536. if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
  537. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  538. pmc->core_off_time = value;
  539. pmc->corereq_high = of_property_read_bool(np,
  540. "nvidia,core-power-req-active-high");
  541. pmc->sysclkreq_high = of_property_read_bool(np,
  542. "nvidia,sys-clock-req-active-high");
  543. pmc->combined_req = of_property_read_bool(np,
  544. "nvidia,combined-power-req");
  545. pmc->cpu_pwr_good_en = of_property_read_bool(np,
  546. "nvidia,cpu-pwr-good-en");
  547. if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
  548. ARRAY_SIZE(values)))
  549. if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
  550. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  551. pmc->lp0_vec_phys = values[0];
  552. pmc->lp0_vec_size = values[1];
  553. return 0;
  554. }
  555. static void tegra_pmc_init(struct tegra_pmc *pmc)
  556. {
  557. u32 value;
  558. /* Always enable CPU power request */
  559. value = tegra_pmc_readl(PMC_CNTRL);
  560. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  561. tegra_pmc_writel(value, PMC_CNTRL);
  562. value = tegra_pmc_readl(PMC_CNTRL);
  563. if (pmc->sysclkreq_high)
  564. value &= ~PMC_CNTRL_SYSCLK_POLARITY;
  565. else
  566. value |= PMC_CNTRL_SYSCLK_POLARITY;
  567. /* configure the output polarity while the request is tristated */
  568. tegra_pmc_writel(value, PMC_CNTRL);
  569. /* now enable the request */
  570. value = tegra_pmc_readl(PMC_CNTRL);
  571. value |= PMC_CNTRL_SYSCLK_OE;
  572. tegra_pmc_writel(value, PMC_CNTRL);
  573. }
  574. void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
  575. {
  576. static const char disabled[] = "emergency thermal reset disabled";
  577. u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
  578. struct device *dev = pmc->dev;
  579. struct device_node *np;
  580. u32 value, checksum;
  581. if (!pmc->soc->has_tsense_reset)
  582. goto out;
  583. np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
  584. if (!np) {
  585. dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
  586. goto out;
  587. }
  588. if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
  589. dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
  590. goto out;
  591. }
  592. if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
  593. dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
  594. goto out;
  595. }
  596. if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
  597. dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
  598. goto out;
  599. }
  600. if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
  601. dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
  602. goto out;
  603. }
  604. if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
  605. pinmux = 0;
  606. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  607. value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
  608. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  609. value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
  610. (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
  611. tegra_pmc_writel(value, PMC_SCRATCH54);
  612. value = PMC_SCRATCH55_RESET_TEGRA;
  613. value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
  614. value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
  615. value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
  616. /*
  617. * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
  618. * contain the checksum and are currently zero, so they are not added.
  619. */
  620. checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
  621. + ((value >> 24) & 0xff);
  622. checksum &= 0xff;
  623. checksum = 0x100 - checksum;
  624. value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
  625. tegra_pmc_writel(value, PMC_SCRATCH55);
  626. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  627. value |= PMC_SENSOR_CTRL_ENABLE_RST;
  628. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  629. dev_info(pmc->dev, "emergency thermal reset enabled\n");
  630. out:
  631. of_node_put(np);
  632. return;
  633. }
  634. static int tegra_pmc_probe(struct platform_device *pdev)
  635. {
  636. void __iomem *base = pmc->base;
  637. struct resource *res;
  638. int err;
  639. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  640. if (err < 0)
  641. return err;
  642. /* take over the memory region from the early initialization */
  643. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  644. pmc->base = devm_ioremap_resource(&pdev->dev, res);
  645. if (IS_ERR(pmc->base))
  646. return PTR_ERR(pmc->base);
  647. iounmap(base);
  648. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  649. if (IS_ERR(pmc->clk)) {
  650. err = PTR_ERR(pmc->clk);
  651. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  652. return err;
  653. }
  654. pmc->dev = &pdev->dev;
  655. tegra_pmc_init(pmc);
  656. tegra_pmc_init_tsense_reset(pmc);
  657. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  658. err = tegra_powergate_debugfs_init();
  659. if (err < 0)
  660. return err;
  661. }
  662. err = register_restart_handler(&tegra_pmc_restart_handler);
  663. if (err) {
  664. dev_err(&pdev->dev, "unable to register restart handler, %d\n",
  665. err);
  666. return err;
  667. }
  668. return 0;
  669. }
  670. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  671. static int tegra_pmc_suspend(struct device *dev)
  672. {
  673. tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
  674. return 0;
  675. }
  676. static int tegra_pmc_resume(struct device *dev)
  677. {
  678. tegra_pmc_writel(0x0, PMC_SCRATCH41);
  679. return 0;
  680. }
  681. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  682. #endif
  683. static const char * const tegra20_powergates[] = {
  684. [TEGRA_POWERGATE_CPU] = "cpu",
  685. [TEGRA_POWERGATE_3D] = "3d",
  686. [TEGRA_POWERGATE_VENC] = "venc",
  687. [TEGRA_POWERGATE_VDEC] = "vdec",
  688. [TEGRA_POWERGATE_PCIE] = "pcie",
  689. [TEGRA_POWERGATE_L2] = "l2",
  690. [TEGRA_POWERGATE_MPE] = "mpe",
  691. };
  692. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  693. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  694. .powergates = tegra20_powergates,
  695. .num_cpu_powergates = 0,
  696. .cpu_powergates = NULL,
  697. .has_tsense_reset = false,
  698. .has_gpu_clamps = false,
  699. };
  700. static const char * const tegra30_powergates[] = {
  701. [TEGRA_POWERGATE_CPU] = "cpu0",
  702. [TEGRA_POWERGATE_3D] = "3d0",
  703. [TEGRA_POWERGATE_VENC] = "venc",
  704. [TEGRA_POWERGATE_VDEC] = "vdec",
  705. [TEGRA_POWERGATE_PCIE] = "pcie",
  706. [TEGRA_POWERGATE_L2] = "l2",
  707. [TEGRA_POWERGATE_MPE] = "mpe",
  708. [TEGRA_POWERGATE_HEG] = "heg",
  709. [TEGRA_POWERGATE_SATA] = "sata",
  710. [TEGRA_POWERGATE_CPU1] = "cpu1",
  711. [TEGRA_POWERGATE_CPU2] = "cpu2",
  712. [TEGRA_POWERGATE_CPU3] = "cpu3",
  713. [TEGRA_POWERGATE_CELP] = "celp",
  714. [TEGRA_POWERGATE_3D1] = "3d1",
  715. };
  716. static const u8 tegra30_cpu_powergates[] = {
  717. TEGRA_POWERGATE_CPU,
  718. TEGRA_POWERGATE_CPU1,
  719. TEGRA_POWERGATE_CPU2,
  720. TEGRA_POWERGATE_CPU3,
  721. };
  722. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  723. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  724. .powergates = tegra30_powergates,
  725. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  726. .cpu_powergates = tegra30_cpu_powergates,
  727. .has_tsense_reset = true,
  728. .has_gpu_clamps = false,
  729. };
  730. static const char * const tegra114_powergates[] = {
  731. [TEGRA_POWERGATE_CPU] = "crail",
  732. [TEGRA_POWERGATE_3D] = "3d",
  733. [TEGRA_POWERGATE_VENC] = "venc",
  734. [TEGRA_POWERGATE_VDEC] = "vdec",
  735. [TEGRA_POWERGATE_MPE] = "mpe",
  736. [TEGRA_POWERGATE_HEG] = "heg",
  737. [TEGRA_POWERGATE_CPU1] = "cpu1",
  738. [TEGRA_POWERGATE_CPU2] = "cpu2",
  739. [TEGRA_POWERGATE_CPU3] = "cpu3",
  740. [TEGRA_POWERGATE_CELP] = "celp",
  741. [TEGRA_POWERGATE_CPU0] = "cpu0",
  742. [TEGRA_POWERGATE_C0NC] = "c0nc",
  743. [TEGRA_POWERGATE_C1NC] = "c1nc",
  744. [TEGRA_POWERGATE_DIS] = "dis",
  745. [TEGRA_POWERGATE_DISB] = "disb",
  746. [TEGRA_POWERGATE_XUSBA] = "xusba",
  747. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  748. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  749. };
  750. static const u8 tegra114_cpu_powergates[] = {
  751. TEGRA_POWERGATE_CPU0,
  752. TEGRA_POWERGATE_CPU1,
  753. TEGRA_POWERGATE_CPU2,
  754. TEGRA_POWERGATE_CPU3,
  755. };
  756. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  757. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  758. .powergates = tegra114_powergates,
  759. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  760. .cpu_powergates = tegra114_cpu_powergates,
  761. .has_tsense_reset = true,
  762. .has_gpu_clamps = false,
  763. };
  764. static const char * const tegra124_powergates[] = {
  765. [TEGRA_POWERGATE_CPU] = "crail",
  766. [TEGRA_POWERGATE_3D] = "3d",
  767. [TEGRA_POWERGATE_VENC] = "venc",
  768. [TEGRA_POWERGATE_PCIE] = "pcie",
  769. [TEGRA_POWERGATE_VDEC] = "vdec",
  770. [TEGRA_POWERGATE_L2] = "l2",
  771. [TEGRA_POWERGATE_MPE] = "mpe",
  772. [TEGRA_POWERGATE_HEG] = "heg",
  773. [TEGRA_POWERGATE_SATA] = "sata",
  774. [TEGRA_POWERGATE_CPU1] = "cpu1",
  775. [TEGRA_POWERGATE_CPU2] = "cpu2",
  776. [TEGRA_POWERGATE_CPU3] = "cpu3",
  777. [TEGRA_POWERGATE_CELP] = "celp",
  778. [TEGRA_POWERGATE_CPU0] = "cpu0",
  779. [TEGRA_POWERGATE_C0NC] = "c0nc",
  780. [TEGRA_POWERGATE_C1NC] = "c1nc",
  781. [TEGRA_POWERGATE_SOR] = "sor",
  782. [TEGRA_POWERGATE_DIS] = "dis",
  783. [TEGRA_POWERGATE_DISB] = "disb",
  784. [TEGRA_POWERGATE_XUSBA] = "xusba",
  785. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  786. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  787. [TEGRA_POWERGATE_VIC] = "vic",
  788. [TEGRA_POWERGATE_IRAM] = "iram",
  789. };
  790. static const u8 tegra124_cpu_powergates[] = {
  791. TEGRA_POWERGATE_CPU0,
  792. TEGRA_POWERGATE_CPU1,
  793. TEGRA_POWERGATE_CPU2,
  794. TEGRA_POWERGATE_CPU3,
  795. };
  796. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  797. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  798. .powergates = tegra124_powergates,
  799. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  800. .cpu_powergates = tegra124_cpu_powergates,
  801. .has_tsense_reset = true,
  802. .has_gpu_clamps = true,
  803. };
  804. static const struct of_device_id tegra_pmc_match[] = {
  805. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  806. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  807. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  808. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  809. { }
  810. };
  811. static struct platform_driver tegra_pmc_driver = {
  812. .driver = {
  813. .name = "tegra-pmc",
  814. .suppress_bind_attrs = true,
  815. .of_match_table = tegra_pmc_match,
  816. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  817. .pm = &tegra_pmc_pm_ops,
  818. #endif
  819. },
  820. .probe = tegra_pmc_probe,
  821. };
  822. builtin_platform_driver(tegra_pmc_driver);
  823. /*
  824. * Early initialization to allow access to registers in the very early boot
  825. * process.
  826. */
  827. static int __init tegra_pmc_early_init(void)
  828. {
  829. const struct of_device_id *match;
  830. struct device_node *np;
  831. struct resource regs;
  832. bool invert;
  833. u32 value;
  834. if (!soc_is_tegra())
  835. return 0;
  836. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  837. if (!np) {
  838. pr_warn("PMC device node not found, disabling powergating\n");
  839. regs.start = 0x7000e400;
  840. regs.end = 0x7000e7ff;
  841. regs.flags = IORESOURCE_MEM;
  842. pr_warn("Using memory region %pR\n", &regs);
  843. } else {
  844. pmc->soc = match->data;
  845. }
  846. if (of_address_to_resource(np, 0, &regs) < 0) {
  847. pr_err("failed to get PMC registers\n");
  848. return -ENXIO;
  849. }
  850. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  851. if (!pmc->base) {
  852. pr_err("failed to map PMC registers\n");
  853. return -ENXIO;
  854. }
  855. mutex_init(&pmc->powergates_lock);
  856. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  857. value = tegra_pmc_readl(PMC_CNTRL);
  858. if (invert)
  859. value |= PMC_CNTRL_INTR_POLARITY;
  860. else
  861. value &= ~PMC_CNTRL_INTR_POLARITY;
  862. tegra_pmc_writel(value, PMC_CNTRL);
  863. return 0;
  864. }
  865. early_initcall(tegra_pmc_early_init);