pmc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. #define pr_fmt(fmt) "tegra-pmc: " fmt
  20. #include <linux/kernel.h>
  21. #include <linux/clk.h>
  22. #include <linux/clk/tegra.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/export.h>
  27. #include <linux/init.h>
  28. #include <linux/io.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/reboot.h>
  33. #include <linux/reset.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/spinlock.h>
  36. #include <soc/tegra/common.h>
  37. #include <soc/tegra/fuse.h>
  38. #include <soc/tegra/pmc.h>
  39. #define PMC_CNTRL 0x0
  40. #define PMC_CNTRL_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
  41. #define PMC_CNTRL_SYSCLK_OE (1 << 11) /* system clock enable */
  42. #define PMC_CNTRL_SIDE_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
  43. #define PMC_CNTRL_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
  44. #define PMC_CNTRL_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
  45. #define PMC_CNTRL_INTR_POLARITY (1 << 17) /* inverts INTR polarity */
  46. #define DPD_SAMPLE 0x020
  47. #define DPD_SAMPLE_ENABLE (1 << 0)
  48. #define DPD_SAMPLE_DISABLE (0 << 0)
  49. #define PWRGATE_TOGGLE 0x30
  50. #define PWRGATE_TOGGLE_START (1 << 8)
  51. #define REMOVE_CLAMPING 0x34
  52. #define PWRGATE_STATUS 0x38
  53. #define PMC_SCRATCH0 0x50
  54. #define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)
  55. #define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)
  56. #define PMC_SCRATCH0_MODE_RCM (1 << 1)
  57. #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
  58. PMC_SCRATCH0_MODE_BOOTLOADER | \
  59. PMC_SCRATCH0_MODE_RCM)
  60. #define PMC_CPUPWRGOOD_TIMER 0xc8
  61. #define PMC_CPUPWROFF_TIMER 0xcc
  62. #define PMC_SCRATCH41 0x140
  63. #define PMC_SENSOR_CTRL 0x1b0
  64. #define PMC_SENSOR_CTRL_SCRATCH_WRITE (1 << 2)
  65. #define PMC_SENSOR_CTRL_ENABLE_RST (1 << 1)
  66. #define IO_DPD_REQ 0x1b8
  67. #define IO_DPD_REQ_CODE_IDLE (0 << 30)
  68. #define IO_DPD_REQ_CODE_OFF (1 << 30)
  69. #define IO_DPD_REQ_CODE_ON (2 << 30)
  70. #define IO_DPD_REQ_CODE_MASK (3 << 30)
  71. #define IO_DPD_STATUS 0x1bc
  72. #define IO_DPD2_REQ 0x1c0
  73. #define IO_DPD2_STATUS 0x1c4
  74. #define SEL_DPD_TIM 0x1c8
  75. #define PMC_SCRATCH54 0x258
  76. #define PMC_SCRATCH54_DATA_SHIFT 8
  77. #define PMC_SCRATCH54_ADDR_SHIFT 0
  78. #define PMC_SCRATCH55 0x25c
  79. #define PMC_SCRATCH55_RESET_TEGRA (1 << 31)
  80. #define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
  81. #define PMC_SCRATCH55_PINMUX_SHIFT 24
  82. #define PMC_SCRATCH55_16BITOP (1 << 15)
  83. #define PMC_SCRATCH55_CHECKSUM_SHIFT 16
  84. #define PMC_SCRATCH55_I2CSLV1_SHIFT 0
  85. #define GPU_RG_CNTRL 0x2d4
  86. struct tegra_pmc_soc {
  87. unsigned int num_powergates;
  88. const char *const *powergates;
  89. unsigned int num_cpu_powergates;
  90. const u8 *cpu_powergates;
  91. bool has_tsense_reset;
  92. bool has_gpu_clamps;
  93. };
  94. /**
  95. * struct tegra_pmc - NVIDIA Tegra PMC
  96. * @base: pointer to I/O remapped register region
  97. * @clk: pointer to pclk clock
  98. * @rate: currently configured rate of pclk
  99. * @suspend_mode: lowest suspend mode available
  100. * @cpu_good_time: CPU power good time (in microseconds)
  101. * @cpu_off_time: CPU power off time (in microsecends)
  102. * @core_osc_time: core power good OSC time (in microseconds)
  103. * @core_pmu_time: core power good PMU time (in microseconds)
  104. * @core_off_time: core power off time (in microseconds)
  105. * @corereq_high: core power request is active-high
  106. * @sysclkreq_high: system clock request is active-high
  107. * @combined_req: combined power request for CPU & core
  108. * @cpu_pwr_good_en: CPU power good signal is enabled
  109. * @lp0_vec_phys: physical base address of the LP0 warm boot code
  110. * @lp0_vec_size: size of the LP0 warm boot code
  111. * @powergates_lock: mutex for power gate register access
  112. */
  113. struct tegra_pmc {
  114. struct device *dev;
  115. void __iomem *base;
  116. struct clk *clk;
  117. const struct tegra_pmc_soc *soc;
  118. unsigned long rate;
  119. enum tegra_suspend_mode suspend_mode;
  120. u32 cpu_good_time;
  121. u32 cpu_off_time;
  122. u32 core_osc_time;
  123. u32 core_pmu_time;
  124. u32 core_off_time;
  125. bool corereq_high;
  126. bool sysclkreq_high;
  127. bool combined_req;
  128. bool cpu_pwr_good_en;
  129. u32 lp0_vec_phys;
  130. u32 lp0_vec_size;
  131. struct mutex powergates_lock;
  132. };
  133. static struct tegra_pmc *pmc = &(struct tegra_pmc) {
  134. .base = NULL,
  135. .suspend_mode = TEGRA_SUSPEND_NONE,
  136. };
  137. static u32 tegra_pmc_readl(unsigned long offset)
  138. {
  139. return readl(pmc->base + offset);
  140. }
  141. static void tegra_pmc_writel(u32 value, unsigned long offset)
  142. {
  143. writel(value, pmc->base + offset);
  144. }
  145. /**
  146. * tegra_powergate_set() - set the state of a partition
  147. * @id: partition ID
  148. * @new_state: new state of the partition
  149. */
  150. static int tegra_powergate_set(int id, bool new_state)
  151. {
  152. bool status;
  153. mutex_lock(&pmc->powergates_lock);
  154. status = tegra_pmc_readl(PWRGATE_STATUS) & (1 << id);
  155. if (status == new_state) {
  156. mutex_unlock(&pmc->powergates_lock);
  157. return 0;
  158. }
  159. tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  160. mutex_unlock(&pmc->powergates_lock);
  161. return 0;
  162. }
  163. /**
  164. * tegra_powergate_power_on() - power on partition
  165. * @id: partition ID
  166. */
  167. int tegra_powergate_power_on(int id)
  168. {
  169. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  170. return -EINVAL;
  171. return tegra_powergate_set(id, true);
  172. }
  173. /**
  174. * tegra_powergate_power_off() - power off partition
  175. * @id: partition ID
  176. */
  177. int tegra_powergate_power_off(int id)
  178. {
  179. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  180. return -EINVAL;
  181. return tegra_powergate_set(id, false);
  182. }
  183. EXPORT_SYMBOL(tegra_powergate_power_off);
  184. /**
  185. * tegra_powergate_is_powered() - check if partition is powered
  186. * @id: partition ID
  187. */
  188. int tegra_powergate_is_powered(int id)
  189. {
  190. u32 status;
  191. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  192. return -EINVAL;
  193. status = tegra_pmc_readl(PWRGATE_STATUS) & (1 << id);
  194. return !!status;
  195. }
  196. /**
  197. * tegra_powergate_remove_clamping() - remove power clamps for partition
  198. * @id: partition ID
  199. */
  200. int tegra_powergate_remove_clamping(int id)
  201. {
  202. u32 mask;
  203. if (!pmc->soc || id < 0 || id >= pmc->soc->num_powergates)
  204. return -EINVAL;
  205. /*
  206. * On Tegra124 and later, the clamps for the GPU are controlled by a
  207. * separate register (with different semantics).
  208. */
  209. if (id == TEGRA_POWERGATE_3D) {
  210. if (pmc->soc->has_gpu_clamps) {
  211. tegra_pmc_writel(0, GPU_RG_CNTRL);
  212. return 0;
  213. }
  214. }
  215. /*
  216. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  217. * swapped relatively to the partition ids
  218. */
  219. if (id == TEGRA_POWERGATE_VDEC)
  220. mask = (1 << TEGRA_POWERGATE_PCIE);
  221. else if (id == TEGRA_POWERGATE_PCIE)
  222. mask = (1 << TEGRA_POWERGATE_VDEC);
  223. else
  224. mask = (1 << id);
  225. tegra_pmc_writel(mask, REMOVE_CLAMPING);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(tegra_powergate_remove_clamping);
  229. /**
  230. * tegra_powergate_sequence_power_up() - power up partition
  231. * @id: partition ID
  232. * @clk: clock for partition
  233. * @rst: reset for partition
  234. *
  235. * Must be called with clk disabled, and returns with clk enabled.
  236. */
  237. int tegra_powergate_sequence_power_up(int id, struct clk *clk,
  238. struct reset_control *rst)
  239. {
  240. int ret;
  241. reset_control_assert(rst);
  242. ret = tegra_powergate_power_on(id);
  243. if (ret)
  244. goto err_power;
  245. ret = clk_prepare_enable(clk);
  246. if (ret)
  247. goto err_clk;
  248. usleep_range(10, 20);
  249. ret = tegra_powergate_remove_clamping(id);
  250. if (ret)
  251. goto err_clamp;
  252. usleep_range(10, 20);
  253. reset_control_deassert(rst);
  254. return 0;
  255. err_clamp:
  256. clk_disable_unprepare(clk);
  257. err_clk:
  258. tegra_powergate_power_off(id);
  259. err_power:
  260. return ret;
  261. }
  262. EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
  263. #ifdef CONFIG_SMP
  264. /**
  265. * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
  266. * @cpuid: CPU partition ID
  267. *
  268. * Returns the partition ID corresponding to the CPU partition ID or a
  269. * negative error code on failure.
  270. */
  271. static int tegra_get_cpu_powergate_id(int cpuid)
  272. {
  273. if (pmc->soc && cpuid > 0 && cpuid < pmc->soc->num_cpu_powergates)
  274. return pmc->soc->cpu_powergates[cpuid];
  275. return -EINVAL;
  276. }
  277. /**
  278. * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
  279. * @cpuid: CPU partition ID
  280. */
  281. bool tegra_pmc_cpu_is_powered(int cpuid)
  282. {
  283. int id;
  284. id = tegra_get_cpu_powergate_id(cpuid);
  285. if (id < 0)
  286. return false;
  287. return tegra_powergate_is_powered(id);
  288. }
  289. /**
  290. * tegra_pmc_cpu_power_on() - power on CPU partition
  291. * @cpuid: CPU partition ID
  292. */
  293. int tegra_pmc_cpu_power_on(int cpuid)
  294. {
  295. int id;
  296. id = tegra_get_cpu_powergate_id(cpuid);
  297. if (id < 0)
  298. return id;
  299. return tegra_powergate_set(id, true);
  300. }
  301. /**
  302. * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
  303. * @cpuid: CPU partition ID
  304. */
  305. int tegra_pmc_cpu_remove_clamping(int cpuid)
  306. {
  307. int id;
  308. id = tegra_get_cpu_powergate_id(cpuid);
  309. if (id < 0)
  310. return id;
  311. return tegra_powergate_remove_clamping(id);
  312. }
  313. #endif /* CONFIG_SMP */
  314. static int tegra_pmc_restart_notify(struct notifier_block *this,
  315. unsigned long action, void *data)
  316. {
  317. const char *cmd = data;
  318. u32 value;
  319. value = tegra_pmc_readl(PMC_SCRATCH0);
  320. value &= ~PMC_SCRATCH0_MODE_MASK;
  321. if (cmd) {
  322. if (strcmp(cmd, "recovery") == 0)
  323. value |= PMC_SCRATCH0_MODE_RECOVERY;
  324. if (strcmp(cmd, "bootloader") == 0)
  325. value |= PMC_SCRATCH0_MODE_BOOTLOADER;
  326. if (strcmp(cmd, "forced-recovery") == 0)
  327. value |= PMC_SCRATCH0_MODE_RCM;
  328. }
  329. tegra_pmc_writel(value, PMC_SCRATCH0);
  330. value = tegra_pmc_readl(0);
  331. value |= 0x10;
  332. tegra_pmc_writel(value, 0);
  333. return NOTIFY_DONE;
  334. }
  335. static struct notifier_block tegra_pmc_restart_handler = {
  336. .notifier_call = tegra_pmc_restart_notify,
  337. .priority = 128,
  338. };
  339. static int powergate_show(struct seq_file *s, void *data)
  340. {
  341. unsigned int i;
  342. seq_printf(s, " powergate powered\n");
  343. seq_printf(s, "------------------\n");
  344. for (i = 0; i < pmc->soc->num_powergates; i++) {
  345. if (!pmc->soc->powergates[i])
  346. continue;
  347. seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
  348. tegra_powergate_is_powered(i) ? "yes" : "no");
  349. }
  350. return 0;
  351. }
  352. static int powergate_open(struct inode *inode, struct file *file)
  353. {
  354. return single_open(file, powergate_show, inode->i_private);
  355. }
  356. static const struct file_operations powergate_fops = {
  357. .open = powergate_open,
  358. .read = seq_read,
  359. .llseek = seq_lseek,
  360. .release = single_release,
  361. };
  362. static int tegra_powergate_debugfs_init(void)
  363. {
  364. struct dentry *d;
  365. d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  366. &powergate_fops);
  367. if (!d)
  368. return -ENOMEM;
  369. return 0;
  370. }
  371. static int tegra_io_rail_prepare(int id, unsigned long *request,
  372. unsigned long *status, unsigned int *bit)
  373. {
  374. unsigned long rate, value;
  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. rate = clk_get_rate(pmc->clk);
  390. tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
  391. /* must be at least 200 ns, in APB (PCLK) clock cycles */
  392. value = DIV_ROUND_UP(1000000000, rate);
  393. value = DIV_ROUND_UP(200, value);
  394. tegra_pmc_writel(value, SEL_DPD_TIM);
  395. return 0;
  396. }
  397. static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
  398. unsigned long val, unsigned long timeout)
  399. {
  400. unsigned long value;
  401. timeout = jiffies + msecs_to_jiffies(timeout);
  402. while (time_after(timeout, jiffies)) {
  403. value = tegra_pmc_readl(offset);
  404. if ((value & mask) == val)
  405. return 0;
  406. usleep_range(250, 1000);
  407. }
  408. return -ETIMEDOUT;
  409. }
  410. static void tegra_io_rail_unprepare(void)
  411. {
  412. tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
  413. }
  414. int tegra_io_rail_power_on(int id)
  415. {
  416. unsigned long request, status, value;
  417. unsigned int bit, mask;
  418. int err;
  419. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  420. if (err < 0)
  421. return err;
  422. mask = 1 << bit;
  423. value = tegra_pmc_readl(request);
  424. value |= mask;
  425. value &= ~IO_DPD_REQ_CODE_MASK;
  426. value |= IO_DPD_REQ_CODE_OFF;
  427. tegra_pmc_writel(value, request);
  428. err = tegra_io_rail_poll(status, mask, 0, 250);
  429. if (err < 0) {
  430. pr_info("tegra_io_rail_poll() failed: %d\n", err);
  431. return err;
  432. }
  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. pr_info("tegra_io_rail_prepare() failed: %d\n", err);
  445. return err;
  446. }
  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. return;
  583. np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
  584. if (!np) {
  585. dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
  586. return;
  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. }
  633. static int tegra_pmc_probe(struct platform_device *pdev)
  634. {
  635. void __iomem *base = pmc->base;
  636. struct resource *res;
  637. int err;
  638. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  639. if (err < 0)
  640. return err;
  641. /* take over the memory region from the early initialization */
  642. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  643. pmc->base = devm_ioremap_resource(&pdev->dev, res);
  644. if (IS_ERR(pmc->base))
  645. return PTR_ERR(pmc->base);
  646. iounmap(base);
  647. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  648. if (IS_ERR(pmc->clk)) {
  649. err = PTR_ERR(pmc->clk);
  650. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  651. return err;
  652. }
  653. pmc->dev = &pdev->dev;
  654. tegra_pmc_init(pmc);
  655. tegra_pmc_init_tsense_reset(pmc);
  656. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  657. err = tegra_powergate_debugfs_init();
  658. if (err < 0)
  659. return err;
  660. }
  661. err = register_restart_handler(&tegra_pmc_restart_handler);
  662. if (err) {
  663. dev_err(&pdev->dev, "unable to register restart handler, %d\n",
  664. err);
  665. return err;
  666. }
  667. return 0;
  668. }
  669. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  670. static int tegra_pmc_suspend(struct device *dev)
  671. {
  672. tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
  673. return 0;
  674. }
  675. static int tegra_pmc_resume(struct device *dev)
  676. {
  677. tegra_pmc_writel(0x0, PMC_SCRATCH41);
  678. return 0;
  679. }
  680. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  681. #endif
  682. static const char * const tegra20_powergates[] = {
  683. [TEGRA_POWERGATE_CPU] = "cpu",
  684. [TEGRA_POWERGATE_3D] = "3d",
  685. [TEGRA_POWERGATE_VENC] = "venc",
  686. [TEGRA_POWERGATE_VDEC] = "vdec",
  687. [TEGRA_POWERGATE_PCIE] = "pcie",
  688. [TEGRA_POWERGATE_L2] = "l2",
  689. [TEGRA_POWERGATE_MPE] = "mpe",
  690. };
  691. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  692. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  693. .powergates = tegra20_powergates,
  694. .num_cpu_powergates = 0,
  695. .cpu_powergates = NULL,
  696. .has_tsense_reset = false,
  697. .has_gpu_clamps = false,
  698. };
  699. static const char * const tegra30_powergates[] = {
  700. [TEGRA_POWERGATE_CPU] = "cpu0",
  701. [TEGRA_POWERGATE_3D] = "3d0",
  702. [TEGRA_POWERGATE_VENC] = "venc",
  703. [TEGRA_POWERGATE_VDEC] = "vdec",
  704. [TEGRA_POWERGATE_PCIE] = "pcie",
  705. [TEGRA_POWERGATE_L2] = "l2",
  706. [TEGRA_POWERGATE_MPE] = "mpe",
  707. [TEGRA_POWERGATE_HEG] = "heg",
  708. [TEGRA_POWERGATE_SATA] = "sata",
  709. [TEGRA_POWERGATE_CPU1] = "cpu1",
  710. [TEGRA_POWERGATE_CPU2] = "cpu2",
  711. [TEGRA_POWERGATE_CPU3] = "cpu3",
  712. [TEGRA_POWERGATE_CELP] = "celp",
  713. [TEGRA_POWERGATE_3D1] = "3d1",
  714. };
  715. static const u8 tegra30_cpu_powergates[] = {
  716. TEGRA_POWERGATE_CPU,
  717. TEGRA_POWERGATE_CPU1,
  718. TEGRA_POWERGATE_CPU2,
  719. TEGRA_POWERGATE_CPU3,
  720. };
  721. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  722. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  723. .powergates = tegra30_powergates,
  724. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  725. .cpu_powergates = tegra30_cpu_powergates,
  726. .has_tsense_reset = true,
  727. .has_gpu_clamps = false,
  728. };
  729. static const char * const tegra114_powergates[] = {
  730. [TEGRA_POWERGATE_CPU] = "crail",
  731. [TEGRA_POWERGATE_3D] = "3d",
  732. [TEGRA_POWERGATE_VENC] = "venc",
  733. [TEGRA_POWERGATE_VDEC] = "vdec",
  734. [TEGRA_POWERGATE_MPE] = "mpe",
  735. [TEGRA_POWERGATE_HEG] = "heg",
  736. [TEGRA_POWERGATE_CPU1] = "cpu1",
  737. [TEGRA_POWERGATE_CPU2] = "cpu2",
  738. [TEGRA_POWERGATE_CPU3] = "cpu3",
  739. [TEGRA_POWERGATE_CELP] = "celp",
  740. [TEGRA_POWERGATE_CPU0] = "cpu0",
  741. [TEGRA_POWERGATE_C0NC] = "c0nc",
  742. [TEGRA_POWERGATE_C1NC] = "c1nc",
  743. [TEGRA_POWERGATE_DIS] = "dis",
  744. [TEGRA_POWERGATE_DISB] = "disb",
  745. [TEGRA_POWERGATE_XUSBA] = "xusba",
  746. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  747. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  748. };
  749. static const u8 tegra114_cpu_powergates[] = {
  750. TEGRA_POWERGATE_CPU0,
  751. TEGRA_POWERGATE_CPU1,
  752. TEGRA_POWERGATE_CPU2,
  753. TEGRA_POWERGATE_CPU3,
  754. };
  755. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  756. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  757. .powergates = tegra114_powergates,
  758. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  759. .cpu_powergates = tegra114_cpu_powergates,
  760. .has_tsense_reset = true,
  761. .has_gpu_clamps = false,
  762. };
  763. static const char * const tegra124_powergates[] = {
  764. [TEGRA_POWERGATE_CPU] = "crail",
  765. [TEGRA_POWERGATE_3D] = "3d",
  766. [TEGRA_POWERGATE_VENC] = "venc",
  767. [TEGRA_POWERGATE_PCIE] = "pcie",
  768. [TEGRA_POWERGATE_VDEC] = "vdec",
  769. [TEGRA_POWERGATE_L2] = "l2",
  770. [TEGRA_POWERGATE_MPE] = "mpe",
  771. [TEGRA_POWERGATE_HEG] = "heg",
  772. [TEGRA_POWERGATE_SATA] = "sata",
  773. [TEGRA_POWERGATE_CPU1] = "cpu1",
  774. [TEGRA_POWERGATE_CPU2] = "cpu2",
  775. [TEGRA_POWERGATE_CPU3] = "cpu3",
  776. [TEGRA_POWERGATE_CELP] = "celp",
  777. [TEGRA_POWERGATE_CPU0] = "cpu0",
  778. [TEGRA_POWERGATE_C0NC] = "c0nc",
  779. [TEGRA_POWERGATE_C1NC] = "c1nc",
  780. [TEGRA_POWERGATE_SOR] = "sor",
  781. [TEGRA_POWERGATE_DIS] = "dis",
  782. [TEGRA_POWERGATE_DISB] = "disb",
  783. [TEGRA_POWERGATE_XUSBA] = "xusba",
  784. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  785. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  786. [TEGRA_POWERGATE_VIC] = "vic",
  787. [TEGRA_POWERGATE_IRAM] = "iram",
  788. };
  789. static const u8 tegra124_cpu_powergates[] = {
  790. TEGRA_POWERGATE_CPU0,
  791. TEGRA_POWERGATE_CPU1,
  792. TEGRA_POWERGATE_CPU2,
  793. TEGRA_POWERGATE_CPU3,
  794. };
  795. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  796. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  797. .powergates = tegra124_powergates,
  798. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  799. .cpu_powergates = tegra124_cpu_powergates,
  800. .has_tsense_reset = true,
  801. .has_gpu_clamps = true,
  802. };
  803. static const char * const tegra210_powergates[] = {
  804. [TEGRA_POWERGATE_CPU] = "crail",
  805. [TEGRA_POWERGATE_3D] = "3d",
  806. [TEGRA_POWERGATE_VENC] = "venc",
  807. [TEGRA_POWERGATE_PCIE] = "pcie",
  808. [TEGRA_POWERGATE_L2] = "l2",
  809. [TEGRA_POWERGATE_MPE] = "mpe",
  810. [TEGRA_POWERGATE_HEG] = "heg",
  811. [TEGRA_POWERGATE_SATA] = "sata",
  812. [TEGRA_POWERGATE_CPU1] = "cpu1",
  813. [TEGRA_POWERGATE_CPU2] = "cpu2",
  814. [TEGRA_POWERGATE_CPU3] = "cpu3",
  815. [TEGRA_POWERGATE_CELP] = "celp",
  816. [TEGRA_POWERGATE_CPU0] = "cpu0",
  817. [TEGRA_POWERGATE_C0NC] = "c0nc",
  818. [TEGRA_POWERGATE_C1NC] = "c1nc",
  819. [TEGRA_POWERGATE_SOR] = "sor",
  820. [TEGRA_POWERGATE_DIS] = "dis",
  821. [TEGRA_POWERGATE_DISB] = "disb",
  822. [TEGRA_POWERGATE_XUSBA] = "xusba",
  823. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  824. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  825. [TEGRA_POWERGATE_VIC] = "vic",
  826. [TEGRA_POWERGATE_IRAM] = "iram",
  827. [TEGRA_POWERGATE_NVDEC] = "nvdec",
  828. [TEGRA_POWERGATE_NVJPG] = "nvjpg",
  829. [TEGRA_POWERGATE_AUD] = "aud",
  830. [TEGRA_POWERGATE_DFD] = "dfd",
  831. [TEGRA_POWERGATE_VE2] = "ve2",
  832. };
  833. static const u8 tegra210_cpu_powergates[] = {
  834. TEGRA_POWERGATE_CPU0,
  835. TEGRA_POWERGATE_CPU1,
  836. TEGRA_POWERGATE_CPU2,
  837. TEGRA_POWERGATE_CPU3,
  838. };
  839. static const struct tegra_pmc_soc tegra210_pmc_soc = {
  840. .num_powergates = ARRAY_SIZE(tegra210_powergates),
  841. .powergates = tegra210_powergates,
  842. .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
  843. .cpu_powergates = tegra210_cpu_powergates,
  844. .has_tsense_reset = true,
  845. .has_gpu_clamps = true,
  846. };
  847. static const struct of_device_id tegra_pmc_match[] = {
  848. { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
  849. { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
  850. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  851. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  852. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  853. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  854. { }
  855. };
  856. static struct platform_driver tegra_pmc_driver = {
  857. .driver = {
  858. .name = "tegra-pmc",
  859. .suppress_bind_attrs = true,
  860. .of_match_table = tegra_pmc_match,
  861. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  862. .pm = &tegra_pmc_pm_ops,
  863. #endif
  864. },
  865. .probe = tegra_pmc_probe,
  866. };
  867. builtin_platform_driver(tegra_pmc_driver);
  868. /*
  869. * Early initialization to allow access to registers in the very early boot
  870. * process.
  871. */
  872. static int __init tegra_pmc_early_init(void)
  873. {
  874. const struct of_device_id *match;
  875. struct device_node *np;
  876. struct resource regs;
  877. bool invert;
  878. u32 value;
  879. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  880. if (!np) {
  881. /*
  882. * Fall back to legacy initialization for 32-bit ARM only. All
  883. * 64-bit ARM device tree files for Tegra are required to have
  884. * a PMC node.
  885. *
  886. * This is for backwards-compatibility with old device trees
  887. * that didn't contain a PMC node. Note that in this case the
  888. * SoC data can't be matched and therefore powergating is
  889. * disabled.
  890. */
  891. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  892. pr_warn("DT node not found, powergating disabled\n");
  893. regs.start = 0x7000e400;
  894. regs.end = 0x7000e7ff;
  895. regs.flags = IORESOURCE_MEM;
  896. pr_warn("Using memory region %pR\n", &regs);
  897. } else {
  898. /*
  899. * At this point we're not running on Tegra, so play
  900. * nice with multi-platform kernels.
  901. */
  902. return 0;
  903. }
  904. } else {
  905. /*
  906. * Extract information from the device tree if we've found a
  907. * matching node.
  908. */
  909. if (of_address_to_resource(np, 0, &regs) < 0) {
  910. pr_err("failed to get PMC registers\n");
  911. return -ENXIO;
  912. }
  913. pmc->soc = match->data;
  914. }
  915. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  916. if (!pmc->base) {
  917. pr_err("failed to map PMC registers\n");
  918. return -ENXIO;
  919. }
  920. mutex_init(&pmc->powergates_lock);
  921. /*
  922. * Invert the interrupt polarity if a PMC device tree node exists and
  923. * contains the nvidia,invert-interrupt property.
  924. */
  925. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  926. value = tegra_pmc_readl(PMC_CNTRL);
  927. if (invert)
  928. value |= PMC_CNTRL_INTR_POLARITY;
  929. else
  930. value &= ~PMC_CNTRL_INTR_POLARITY;
  931. tegra_pmc_writel(value, PMC_CNTRL);
  932. return 0;
  933. }
  934. early_initcall(tegra_pmc_early_init);