pmc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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/iopoll.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/pm_domain.h>
  35. #include <linux/reboot.h>
  36. #include <linux/reset.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. #include <soc/tegra/common.h>
  41. #include <soc/tegra/fuse.h>
  42. #include <soc/tegra/pmc.h>
  43. #define PMC_CNTRL 0x0
  44. #define PMC_CNTRL_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
  45. #define PMC_CNTRL_SYSCLK_OE (1 << 11) /* system clock enable */
  46. #define PMC_CNTRL_SIDE_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
  47. #define PMC_CNTRL_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
  48. #define PMC_CNTRL_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
  49. #define PMC_CNTRL_INTR_POLARITY (1 << 17) /* inverts INTR polarity */
  50. #define PMC_CNTRL_MAIN_RST (1 << 4)
  51. #define DPD_SAMPLE 0x020
  52. #define DPD_SAMPLE_ENABLE (1 << 0)
  53. #define DPD_SAMPLE_DISABLE (0 << 0)
  54. #define PWRGATE_TOGGLE 0x30
  55. #define PWRGATE_TOGGLE_START (1 << 8)
  56. #define REMOVE_CLAMPING 0x34
  57. #define PWRGATE_STATUS 0x38
  58. #define PMC_SCRATCH0 0x50
  59. #define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)
  60. #define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)
  61. #define PMC_SCRATCH0_MODE_RCM (1 << 1)
  62. #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
  63. PMC_SCRATCH0_MODE_BOOTLOADER | \
  64. PMC_SCRATCH0_MODE_RCM)
  65. #define PMC_CPUPWRGOOD_TIMER 0xc8
  66. #define PMC_CPUPWROFF_TIMER 0xcc
  67. #define PMC_SCRATCH41 0x140
  68. #define PMC_SENSOR_CTRL 0x1b0
  69. #define PMC_SENSOR_CTRL_SCRATCH_WRITE (1 << 2)
  70. #define PMC_SENSOR_CTRL_ENABLE_RST (1 << 1)
  71. #define PMC_RST_STATUS 0x1b4
  72. #define PMC_RST_STATUS_POR 0
  73. #define PMC_RST_STATUS_WATCHDOG 1
  74. #define PMC_RST_STATUS_SENSOR 2
  75. #define PMC_RST_STATUS_SW_MAIN 3
  76. #define PMC_RST_STATUS_LP0 4
  77. #define PMC_RST_STATUS_AOTAG 5
  78. #define IO_DPD_REQ 0x1b8
  79. #define IO_DPD_REQ_CODE_IDLE (0 << 30)
  80. #define IO_DPD_REQ_CODE_OFF (1 << 30)
  81. #define IO_DPD_REQ_CODE_ON (2 << 30)
  82. #define IO_DPD_REQ_CODE_MASK (3 << 30)
  83. #define IO_DPD_STATUS 0x1bc
  84. #define IO_DPD2_REQ 0x1c0
  85. #define IO_DPD2_STATUS 0x1c4
  86. #define SEL_DPD_TIM 0x1c8
  87. #define PMC_SCRATCH54 0x258
  88. #define PMC_SCRATCH54_DATA_SHIFT 8
  89. #define PMC_SCRATCH54_ADDR_SHIFT 0
  90. #define PMC_SCRATCH55 0x25c
  91. #define PMC_SCRATCH55_RESET_TEGRA (1 << 31)
  92. #define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
  93. #define PMC_SCRATCH55_PINMUX_SHIFT 24
  94. #define PMC_SCRATCH55_16BITOP (1 << 15)
  95. #define PMC_SCRATCH55_CHECKSUM_SHIFT 16
  96. #define PMC_SCRATCH55_I2CSLV1_SHIFT 0
  97. #define GPU_RG_CNTRL 0x2d4
  98. struct tegra_powergate {
  99. struct generic_pm_domain genpd;
  100. struct tegra_pmc *pmc;
  101. unsigned int id;
  102. struct clk **clks;
  103. unsigned int num_clks;
  104. struct reset_control **resets;
  105. unsigned int num_resets;
  106. };
  107. struct tegra_pmc_soc {
  108. unsigned int num_powergates;
  109. const char *const *powergates;
  110. unsigned int num_cpu_powergates;
  111. const u8 *cpu_powergates;
  112. bool has_tsense_reset;
  113. bool has_gpu_clamps;
  114. };
  115. /**
  116. * struct tegra_pmc - NVIDIA Tegra PMC
  117. * @dev: pointer to PMC device structure
  118. * @base: pointer to I/O remapped register region
  119. * @clk: pointer to pclk clock
  120. * @soc: pointer to SoC data structure
  121. * @debugfs: pointer to debugfs entry
  122. * @rate: currently configured rate of pclk
  123. * @suspend_mode: lowest suspend mode available
  124. * @cpu_good_time: CPU power good time (in microseconds)
  125. * @cpu_off_time: CPU power off time (in microsecends)
  126. * @core_osc_time: core power good OSC time (in microseconds)
  127. * @core_pmu_time: core power good PMU time (in microseconds)
  128. * @core_off_time: core power off time (in microseconds)
  129. * @corereq_high: core power request is active-high
  130. * @sysclkreq_high: system clock request is active-high
  131. * @combined_req: combined power request for CPU & core
  132. * @cpu_pwr_good_en: CPU power good signal is enabled
  133. * @lp0_vec_phys: physical base address of the LP0 warm boot code
  134. * @lp0_vec_size: size of the LP0 warm boot code
  135. * @powergates_available: Bitmap of available power gates
  136. * @powergates_lock: mutex for power gate register access
  137. */
  138. struct tegra_pmc {
  139. struct device *dev;
  140. void __iomem *base;
  141. struct clk *clk;
  142. struct dentry *debugfs;
  143. const struct tegra_pmc_soc *soc;
  144. unsigned long rate;
  145. enum tegra_suspend_mode suspend_mode;
  146. u32 cpu_good_time;
  147. u32 cpu_off_time;
  148. u32 core_osc_time;
  149. u32 core_pmu_time;
  150. u32 core_off_time;
  151. bool corereq_high;
  152. bool sysclkreq_high;
  153. bool combined_req;
  154. bool cpu_pwr_good_en;
  155. u32 lp0_vec_phys;
  156. u32 lp0_vec_size;
  157. DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
  158. struct mutex powergates_lock;
  159. };
  160. static struct tegra_pmc *pmc = &(struct tegra_pmc) {
  161. .base = NULL,
  162. .suspend_mode = TEGRA_SUSPEND_NONE,
  163. };
  164. static inline struct tegra_powergate *
  165. to_powergate(struct generic_pm_domain *domain)
  166. {
  167. return container_of(domain, struct tegra_powergate, genpd);
  168. }
  169. static u32 tegra_pmc_readl(unsigned long offset)
  170. {
  171. return readl(pmc->base + offset);
  172. }
  173. static void tegra_pmc_writel(u32 value, unsigned long offset)
  174. {
  175. writel(value, pmc->base + offset);
  176. }
  177. static inline bool tegra_powergate_state(int id)
  178. {
  179. if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
  180. return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
  181. else
  182. return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
  183. }
  184. static inline bool tegra_powergate_is_valid(int id)
  185. {
  186. return (pmc->soc && pmc->soc->powergates[id]);
  187. }
  188. static inline bool tegra_powergate_is_available(int id)
  189. {
  190. return test_bit(id, pmc->powergates_available);
  191. }
  192. static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
  193. {
  194. unsigned int i;
  195. if (!pmc || !pmc->soc || !name)
  196. return -EINVAL;
  197. for (i = 0; i < pmc->soc->num_powergates; i++) {
  198. if (!tegra_powergate_is_valid(i))
  199. continue;
  200. if (!strcmp(name, pmc->soc->powergates[i]))
  201. return i;
  202. }
  203. dev_err(pmc->dev, "powergate %s not found\n", name);
  204. return -ENODEV;
  205. }
  206. /**
  207. * tegra_powergate_set() - set the state of a partition
  208. * @id: partition ID
  209. * @new_state: new state of the partition
  210. */
  211. static int tegra_powergate_set(unsigned int id, bool new_state)
  212. {
  213. bool status;
  214. int err;
  215. if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
  216. return -EINVAL;
  217. mutex_lock(&pmc->powergates_lock);
  218. if (tegra_powergate_state(id) == new_state) {
  219. mutex_unlock(&pmc->powergates_lock);
  220. return 0;
  221. }
  222. tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  223. err = readx_poll_timeout(tegra_powergate_state, id, status,
  224. status == new_state, 10, 100000);
  225. mutex_unlock(&pmc->powergates_lock);
  226. return err;
  227. }
  228. static int __tegra_powergate_remove_clamping(unsigned int id)
  229. {
  230. u32 mask;
  231. mutex_lock(&pmc->powergates_lock);
  232. /*
  233. * On Tegra124 and later, the clamps for the GPU are controlled by a
  234. * separate register (with different semantics).
  235. */
  236. if (id == TEGRA_POWERGATE_3D) {
  237. if (pmc->soc->has_gpu_clamps) {
  238. tegra_pmc_writel(0, GPU_RG_CNTRL);
  239. goto out;
  240. }
  241. }
  242. /*
  243. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  244. * swapped relatively to the partition ids
  245. */
  246. if (id == TEGRA_POWERGATE_VDEC)
  247. mask = (1 << TEGRA_POWERGATE_PCIE);
  248. else if (id == TEGRA_POWERGATE_PCIE)
  249. mask = (1 << TEGRA_POWERGATE_VDEC);
  250. else
  251. mask = (1 << id);
  252. tegra_pmc_writel(mask, REMOVE_CLAMPING);
  253. out:
  254. mutex_unlock(&pmc->powergates_lock);
  255. return 0;
  256. }
  257. static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < pg->num_clks; i++)
  261. clk_disable_unprepare(pg->clks[i]);
  262. }
  263. static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
  264. {
  265. unsigned int i;
  266. int err;
  267. for (i = 0; i < pg->num_clks; i++) {
  268. err = clk_prepare_enable(pg->clks[i]);
  269. if (err)
  270. goto out;
  271. }
  272. return 0;
  273. out:
  274. while (i--)
  275. clk_disable_unprepare(pg->clks[i]);
  276. return err;
  277. }
  278. static int tegra_powergate_reset_assert(struct tegra_powergate *pg)
  279. {
  280. unsigned int i;
  281. int err;
  282. for (i = 0; i < pg->num_resets; i++) {
  283. err = reset_control_assert(pg->resets[i]);
  284. if (err)
  285. return err;
  286. }
  287. return 0;
  288. }
  289. static int tegra_powergate_reset_deassert(struct tegra_powergate *pg)
  290. {
  291. unsigned int i;
  292. int err;
  293. for (i = 0; i < pg->num_resets; i++) {
  294. err = reset_control_deassert(pg->resets[i]);
  295. if (err)
  296. return err;
  297. }
  298. return 0;
  299. }
  300. static int tegra_powergate_power_up(struct tegra_powergate *pg,
  301. bool disable_clocks)
  302. {
  303. int err;
  304. err = tegra_powergate_reset_assert(pg);
  305. if (err)
  306. return err;
  307. usleep_range(10, 20);
  308. err = tegra_powergate_set(pg->id, true);
  309. if (err < 0)
  310. return err;
  311. usleep_range(10, 20);
  312. err = tegra_powergate_enable_clocks(pg);
  313. if (err)
  314. goto disable_clks;
  315. usleep_range(10, 20);
  316. err = __tegra_powergate_remove_clamping(pg->id);
  317. if (err)
  318. goto disable_clks;
  319. usleep_range(10, 20);
  320. err = tegra_powergate_reset_deassert(pg);
  321. if (err)
  322. goto powergate_off;
  323. usleep_range(10, 20);
  324. if (disable_clocks)
  325. tegra_powergate_disable_clocks(pg);
  326. return 0;
  327. disable_clks:
  328. tegra_powergate_disable_clocks(pg);
  329. usleep_range(10, 20);
  330. powergate_off:
  331. tegra_powergate_set(pg->id, false);
  332. return err;
  333. }
  334. static int tegra_powergate_power_down(struct tegra_powergate *pg)
  335. {
  336. int err;
  337. err = tegra_powergate_enable_clocks(pg);
  338. if (err)
  339. return err;
  340. usleep_range(10, 20);
  341. err = tegra_powergate_reset_assert(pg);
  342. if (err)
  343. goto disable_clks;
  344. usleep_range(10, 20);
  345. tegra_powergate_disable_clocks(pg);
  346. usleep_range(10, 20);
  347. err = tegra_powergate_set(pg->id, false);
  348. if (err)
  349. goto assert_resets;
  350. return 0;
  351. assert_resets:
  352. tegra_powergate_enable_clocks(pg);
  353. usleep_range(10, 20);
  354. tegra_powergate_reset_deassert(pg);
  355. usleep_range(10, 20);
  356. disable_clks:
  357. tegra_powergate_disable_clocks(pg);
  358. return err;
  359. }
  360. static int tegra_genpd_power_on(struct generic_pm_domain *domain)
  361. {
  362. struct tegra_powergate *pg = to_powergate(domain);
  363. struct tegra_pmc *pmc = pg->pmc;
  364. int err;
  365. err = tegra_powergate_power_up(pg, true);
  366. if (err)
  367. dev_err(pmc->dev, "failed to turn on PM domain %s: %d\n",
  368. pg->genpd.name, err);
  369. return err;
  370. }
  371. static int tegra_genpd_power_off(struct generic_pm_domain *domain)
  372. {
  373. struct tegra_powergate *pg = to_powergate(domain);
  374. struct tegra_pmc *pmc = pg->pmc;
  375. int err;
  376. err = tegra_powergate_power_down(pg);
  377. if (err)
  378. dev_err(pmc->dev, "failed to turn off PM domain %s: %d\n",
  379. pg->genpd.name, err);
  380. return err;
  381. }
  382. /**
  383. * tegra_powergate_power_on() - power on partition
  384. * @id: partition ID
  385. */
  386. int tegra_powergate_power_on(unsigned int id)
  387. {
  388. if (!tegra_powergate_is_available(id))
  389. return -EINVAL;
  390. return tegra_powergate_set(id, true);
  391. }
  392. /**
  393. * tegra_powergate_power_off() - power off partition
  394. * @id: partition ID
  395. */
  396. int tegra_powergate_power_off(unsigned int id)
  397. {
  398. if (!tegra_powergate_is_available(id))
  399. return -EINVAL;
  400. return tegra_powergate_set(id, false);
  401. }
  402. EXPORT_SYMBOL(tegra_powergate_power_off);
  403. /**
  404. * tegra_powergate_is_powered() - check if partition is powered
  405. * @id: partition ID
  406. */
  407. int tegra_powergate_is_powered(unsigned int id)
  408. {
  409. int status;
  410. if (!tegra_powergate_is_valid(id))
  411. return -EINVAL;
  412. mutex_lock(&pmc->powergates_lock);
  413. status = tegra_powergate_state(id);
  414. mutex_unlock(&pmc->powergates_lock);
  415. return status;
  416. }
  417. /**
  418. * tegra_powergate_remove_clamping() - remove power clamps for partition
  419. * @id: partition ID
  420. */
  421. int tegra_powergate_remove_clamping(unsigned int id)
  422. {
  423. if (!tegra_powergate_is_available(id))
  424. return -EINVAL;
  425. return __tegra_powergate_remove_clamping(id);
  426. }
  427. EXPORT_SYMBOL(tegra_powergate_remove_clamping);
  428. /**
  429. * tegra_powergate_sequence_power_up() - power up partition
  430. * @id: partition ID
  431. * @clk: clock for partition
  432. * @rst: reset for partition
  433. *
  434. * Must be called with clk disabled, and returns with clk enabled.
  435. */
  436. int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
  437. struct reset_control *rst)
  438. {
  439. struct tegra_powergate pg;
  440. int err;
  441. if (!tegra_powergate_is_available(id))
  442. return -EINVAL;
  443. pg.id = id;
  444. pg.clks = &clk;
  445. pg.num_clks = 1;
  446. pg.resets = &rst;
  447. pg.num_resets = 1;
  448. err = tegra_powergate_power_up(&pg, false);
  449. if (err)
  450. pr_err("failed to turn on partition %d: %d\n", id, err);
  451. return err;
  452. }
  453. EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
  454. #ifdef CONFIG_SMP
  455. /**
  456. * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
  457. * @cpuid: CPU partition ID
  458. *
  459. * Returns the partition ID corresponding to the CPU partition ID or a
  460. * negative error code on failure.
  461. */
  462. static int tegra_get_cpu_powergate_id(unsigned int cpuid)
  463. {
  464. if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
  465. return pmc->soc->cpu_powergates[cpuid];
  466. return -EINVAL;
  467. }
  468. /**
  469. * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
  470. * @cpuid: CPU partition ID
  471. */
  472. bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
  473. {
  474. int id;
  475. id = tegra_get_cpu_powergate_id(cpuid);
  476. if (id < 0)
  477. return false;
  478. return tegra_powergate_is_powered(id);
  479. }
  480. /**
  481. * tegra_pmc_cpu_power_on() - power on CPU partition
  482. * @cpuid: CPU partition ID
  483. */
  484. int tegra_pmc_cpu_power_on(unsigned int cpuid)
  485. {
  486. int id;
  487. id = tegra_get_cpu_powergate_id(cpuid);
  488. if (id < 0)
  489. return id;
  490. return tegra_powergate_set(id, true);
  491. }
  492. /**
  493. * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
  494. * @cpuid: CPU partition ID
  495. */
  496. int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
  497. {
  498. int id;
  499. id = tegra_get_cpu_powergate_id(cpuid);
  500. if (id < 0)
  501. return id;
  502. return tegra_powergate_remove_clamping(id);
  503. }
  504. #endif /* CONFIG_SMP */
  505. static int tegra_pmc_restart_notify(struct notifier_block *this,
  506. unsigned long action, void *data)
  507. {
  508. const char *cmd = data;
  509. u32 value;
  510. value = tegra_pmc_readl(PMC_SCRATCH0);
  511. value &= ~PMC_SCRATCH0_MODE_MASK;
  512. if (cmd) {
  513. if (strcmp(cmd, "recovery") == 0)
  514. value |= PMC_SCRATCH0_MODE_RECOVERY;
  515. if (strcmp(cmd, "bootloader") == 0)
  516. value |= PMC_SCRATCH0_MODE_BOOTLOADER;
  517. if (strcmp(cmd, "forced-recovery") == 0)
  518. value |= PMC_SCRATCH0_MODE_RCM;
  519. }
  520. tegra_pmc_writel(value, PMC_SCRATCH0);
  521. /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
  522. value = tegra_pmc_readl(PMC_CNTRL);
  523. value |= PMC_CNTRL_MAIN_RST;
  524. tegra_pmc_writel(value, PMC_CNTRL);
  525. return NOTIFY_DONE;
  526. }
  527. static struct notifier_block tegra_pmc_restart_handler = {
  528. .notifier_call = tegra_pmc_restart_notify,
  529. .priority = 128,
  530. };
  531. static int powergate_show(struct seq_file *s, void *data)
  532. {
  533. unsigned int i;
  534. int status;
  535. seq_printf(s, " powergate powered\n");
  536. seq_printf(s, "------------------\n");
  537. for (i = 0; i < pmc->soc->num_powergates; i++) {
  538. status = tegra_powergate_is_powered(i);
  539. if (status < 0)
  540. continue;
  541. seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
  542. status ? "yes" : "no");
  543. }
  544. return 0;
  545. }
  546. static int powergate_open(struct inode *inode, struct file *file)
  547. {
  548. return single_open(file, powergate_show, inode->i_private);
  549. }
  550. static const struct file_operations powergate_fops = {
  551. .open = powergate_open,
  552. .read = seq_read,
  553. .llseek = seq_lseek,
  554. .release = single_release,
  555. };
  556. static int tegra_powergate_debugfs_init(void)
  557. {
  558. pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  559. &powergate_fops);
  560. if (!pmc->debugfs)
  561. return -ENOMEM;
  562. return 0;
  563. }
  564. static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
  565. struct device_node *np)
  566. {
  567. struct clk *clk;
  568. unsigned int i, count;
  569. int err;
  570. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  571. if (count == 0)
  572. return -ENODEV;
  573. pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
  574. if (!pg->clks)
  575. return -ENOMEM;
  576. for (i = 0; i < count; i++) {
  577. pg->clks[i] = of_clk_get(np, i);
  578. if (IS_ERR(pg->clks[i])) {
  579. err = PTR_ERR(pg->clks[i]);
  580. goto err;
  581. }
  582. }
  583. pg->num_clks = count;
  584. return 0;
  585. err:
  586. while (i--)
  587. clk_put(pg->clks[i]);
  588. kfree(pg->clks);
  589. return err;
  590. }
  591. static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
  592. struct device_node *np, bool off)
  593. {
  594. struct reset_control *rst;
  595. unsigned int i, count;
  596. int err;
  597. count = of_count_phandle_with_args(np, "resets", "#reset-cells");
  598. if (count == 0)
  599. return -ENODEV;
  600. pg->resets = kcalloc(count, sizeof(rst), GFP_KERNEL);
  601. if (!pg->resets)
  602. return -ENOMEM;
  603. for (i = 0; i < count; i++) {
  604. pg->resets[i] = of_reset_control_get_by_index(np, i);
  605. if (IS_ERR(pg->resets[i])) {
  606. err = PTR_ERR(pg->resets[i]);
  607. goto error;
  608. }
  609. if (off)
  610. err = reset_control_assert(pg->resets[i]);
  611. else
  612. err = reset_control_deassert(pg->resets[i]);
  613. if (err) {
  614. reset_control_put(pg->resets[i]);
  615. goto error;
  616. }
  617. }
  618. pg->num_resets = count;
  619. return 0;
  620. error:
  621. while (i--)
  622. reset_control_put(pg->resets[i]);
  623. kfree(pg->resets);
  624. return err;
  625. }
  626. static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
  627. {
  628. struct tegra_powergate *pg;
  629. int id, err;
  630. bool off;
  631. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  632. if (!pg)
  633. return;
  634. id = tegra_powergate_lookup(pmc, np->name);
  635. if (id < 0) {
  636. dev_err(pmc->dev, "powergate lookup failed for %s: %d\n",
  637. np->name, id);
  638. goto free_mem;
  639. }
  640. /*
  641. * Clear the bit for this powergate so it cannot be managed
  642. * directly via the legacy APIs for controlling powergates.
  643. */
  644. clear_bit(id, pmc->powergates_available);
  645. pg->id = id;
  646. pg->genpd.name = np->name;
  647. pg->genpd.power_off = tegra_genpd_power_off;
  648. pg->genpd.power_on = tegra_genpd_power_on;
  649. pg->pmc = pmc;
  650. off = !tegra_powergate_is_powered(pg->id);
  651. err = tegra_powergate_of_get_clks(pg, np);
  652. if (err < 0) {
  653. dev_err(pmc->dev, "failed to get clocks for %s: %d\n",
  654. np->name, err);
  655. goto set_available;
  656. }
  657. err = tegra_powergate_of_get_resets(pg, np, off);
  658. if (err < 0) {
  659. dev_err(pmc->dev, "failed to get resets for %s: %d\n",
  660. np->name, err);
  661. goto remove_clks;
  662. }
  663. if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS))
  664. goto power_on_cleanup;
  665. /*
  666. * FIXME: If XHCI is enabled for Tegra, then power-up the XUSB
  667. * host and super-speed partitions. Once the XHCI driver
  668. * manages the partitions itself this code can be removed. Note
  669. * that we don't register these partitions with the genpd core
  670. * to avoid it from powering down the partitions as they appear
  671. * to be unused.
  672. */
  673. if (IS_ENABLED(CONFIG_USB_XHCI_TEGRA) &&
  674. (id == TEGRA_POWERGATE_XUSBA || id == TEGRA_POWERGATE_XUSBC))
  675. goto power_on_cleanup;
  676. pm_genpd_init(&pg->genpd, NULL, off);
  677. err = of_genpd_add_provider_simple(np, &pg->genpd);
  678. if (err < 0) {
  679. dev_err(pmc->dev, "failed to add genpd provider for %s: %d\n",
  680. np->name, err);
  681. goto remove_resets;
  682. }
  683. dev_dbg(pmc->dev, "added power domain %s\n", pg->genpd.name);
  684. return;
  685. power_on_cleanup:
  686. if (off)
  687. WARN_ON(tegra_powergate_power_up(pg, true));
  688. remove_resets:
  689. while (pg->num_resets--)
  690. reset_control_put(pg->resets[pg->num_resets]);
  691. kfree(pg->resets);
  692. remove_clks:
  693. while (pg->num_clks--)
  694. clk_put(pg->clks[pg->num_clks]);
  695. kfree(pg->clks);
  696. set_available:
  697. set_bit(id, pmc->powergates_available);
  698. free_mem:
  699. kfree(pg);
  700. }
  701. static void tegra_powergate_init(struct tegra_pmc *pmc,
  702. struct device_node *parent)
  703. {
  704. struct device_node *np, *child;
  705. unsigned int i;
  706. /* Create a bitmap of the available and valid partitions */
  707. for (i = 0; i < pmc->soc->num_powergates; i++)
  708. if (pmc->soc->powergates[i])
  709. set_bit(i, pmc->powergates_available);
  710. np = of_get_child_by_name(parent, "powergates");
  711. if (!np)
  712. return;
  713. for_each_child_of_node(np, child) {
  714. tegra_powergate_add(pmc, child);
  715. of_node_put(child);
  716. }
  717. of_node_put(np);
  718. }
  719. static int tegra_io_rail_prepare(unsigned int id, unsigned long *request,
  720. unsigned long *status, unsigned int *bit)
  721. {
  722. unsigned long rate, value;
  723. *bit = id % 32;
  724. /*
  725. * There are two sets of 30 bits to select IO rails, but bits 30 and
  726. * 31 are control bits rather than IO rail selection bits.
  727. */
  728. if (id > 63 || *bit == 30 || *bit == 31)
  729. return -EINVAL;
  730. if (id < 32) {
  731. *status = IO_DPD_STATUS;
  732. *request = IO_DPD_REQ;
  733. } else {
  734. *status = IO_DPD2_STATUS;
  735. *request = IO_DPD2_REQ;
  736. }
  737. rate = clk_get_rate(pmc->clk);
  738. tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
  739. /* must be at least 200 ns, in APB (PCLK) clock cycles */
  740. value = DIV_ROUND_UP(1000000000, rate);
  741. value = DIV_ROUND_UP(200, value);
  742. tegra_pmc_writel(value, SEL_DPD_TIM);
  743. return 0;
  744. }
  745. static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
  746. unsigned long val, unsigned long timeout)
  747. {
  748. unsigned long value;
  749. timeout = jiffies + msecs_to_jiffies(timeout);
  750. while (time_after(timeout, jiffies)) {
  751. value = tegra_pmc_readl(offset);
  752. if ((value & mask) == val)
  753. return 0;
  754. usleep_range(250, 1000);
  755. }
  756. return -ETIMEDOUT;
  757. }
  758. static void tegra_io_rail_unprepare(void)
  759. {
  760. tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
  761. }
  762. int tegra_io_rail_power_on(unsigned int id)
  763. {
  764. unsigned long request, status;
  765. unsigned int bit;
  766. int err;
  767. mutex_lock(&pmc->powergates_lock);
  768. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  769. if (err)
  770. goto error;
  771. tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | BIT(bit), request);
  772. err = tegra_io_rail_poll(status, BIT(bit), 0, 250);
  773. if (err) {
  774. pr_info("tegra_io_rail_poll() failed: %d\n", err);
  775. goto error;
  776. }
  777. tegra_io_rail_unprepare();
  778. error:
  779. mutex_unlock(&pmc->powergates_lock);
  780. return err;
  781. }
  782. EXPORT_SYMBOL(tegra_io_rail_power_on);
  783. int tegra_io_rail_power_off(unsigned int id)
  784. {
  785. unsigned long request, status;
  786. unsigned int bit;
  787. int err;
  788. mutex_lock(&pmc->powergates_lock);
  789. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  790. if (err) {
  791. pr_info("tegra_io_rail_prepare() failed: %d\n", err);
  792. goto error;
  793. }
  794. tegra_pmc_writel(IO_DPD_REQ_CODE_ON | BIT(bit), request);
  795. err = tegra_io_rail_poll(status, BIT(bit), BIT(bit), 250);
  796. if (err)
  797. goto error;
  798. tegra_io_rail_unprepare();
  799. error:
  800. mutex_unlock(&pmc->powergates_lock);
  801. return err;
  802. }
  803. EXPORT_SYMBOL(tegra_io_rail_power_off);
  804. #ifdef CONFIG_PM_SLEEP
  805. enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
  806. {
  807. return pmc->suspend_mode;
  808. }
  809. void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
  810. {
  811. if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
  812. return;
  813. pmc->suspend_mode = mode;
  814. }
  815. void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  816. {
  817. unsigned long long rate = 0;
  818. u32 value;
  819. switch (mode) {
  820. case TEGRA_SUSPEND_LP1:
  821. rate = 32768;
  822. break;
  823. case TEGRA_SUSPEND_LP2:
  824. rate = clk_get_rate(pmc->clk);
  825. break;
  826. default:
  827. break;
  828. }
  829. if (WARN_ON_ONCE(rate == 0))
  830. rate = 100000000;
  831. if (rate != pmc->rate) {
  832. u64 ticks;
  833. ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
  834. do_div(ticks, USEC_PER_SEC);
  835. tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
  836. ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
  837. do_div(ticks, USEC_PER_SEC);
  838. tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
  839. wmb();
  840. pmc->rate = rate;
  841. }
  842. value = tegra_pmc_readl(PMC_CNTRL);
  843. value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
  844. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  845. tegra_pmc_writel(value, PMC_CNTRL);
  846. }
  847. #endif
  848. static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
  849. {
  850. u32 value, values[2];
  851. if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
  852. } else {
  853. switch (value) {
  854. case 0:
  855. pmc->suspend_mode = TEGRA_SUSPEND_LP0;
  856. break;
  857. case 1:
  858. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  859. break;
  860. case 2:
  861. pmc->suspend_mode = TEGRA_SUSPEND_LP2;
  862. break;
  863. default:
  864. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  865. break;
  866. }
  867. }
  868. pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
  869. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
  870. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  871. pmc->cpu_good_time = value;
  872. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
  873. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  874. pmc->cpu_off_time = value;
  875. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  876. values, ARRAY_SIZE(values)))
  877. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  878. pmc->core_osc_time = values[0];
  879. pmc->core_pmu_time = values[1];
  880. if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
  881. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  882. pmc->core_off_time = value;
  883. pmc->corereq_high = of_property_read_bool(np,
  884. "nvidia,core-power-req-active-high");
  885. pmc->sysclkreq_high = of_property_read_bool(np,
  886. "nvidia,sys-clock-req-active-high");
  887. pmc->combined_req = of_property_read_bool(np,
  888. "nvidia,combined-power-req");
  889. pmc->cpu_pwr_good_en = of_property_read_bool(np,
  890. "nvidia,cpu-pwr-good-en");
  891. if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
  892. ARRAY_SIZE(values)))
  893. if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
  894. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  895. pmc->lp0_vec_phys = values[0];
  896. pmc->lp0_vec_size = values[1];
  897. return 0;
  898. }
  899. static void tegra_pmc_init(struct tegra_pmc *pmc)
  900. {
  901. u32 value;
  902. /* Always enable CPU power request */
  903. value = tegra_pmc_readl(PMC_CNTRL);
  904. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  905. tegra_pmc_writel(value, PMC_CNTRL);
  906. value = tegra_pmc_readl(PMC_CNTRL);
  907. if (pmc->sysclkreq_high)
  908. value &= ~PMC_CNTRL_SYSCLK_POLARITY;
  909. else
  910. value |= PMC_CNTRL_SYSCLK_POLARITY;
  911. /* configure the output polarity while the request is tristated */
  912. tegra_pmc_writel(value, PMC_CNTRL);
  913. /* now enable the request */
  914. value = tegra_pmc_readl(PMC_CNTRL);
  915. value |= PMC_CNTRL_SYSCLK_OE;
  916. tegra_pmc_writel(value, PMC_CNTRL);
  917. }
  918. static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
  919. {
  920. static const char disabled[] = "emergency thermal reset disabled";
  921. u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
  922. struct device *dev = pmc->dev;
  923. struct device_node *np;
  924. u32 value, checksum;
  925. if (!pmc->soc->has_tsense_reset)
  926. return;
  927. np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
  928. if (!np) {
  929. dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
  930. return;
  931. }
  932. if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
  933. dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
  934. goto out;
  935. }
  936. if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
  937. dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
  938. goto out;
  939. }
  940. if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
  941. dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
  942. goto out;
  943. }
  944. if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
  945. dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
  946. goto out;
  947. }
  948. if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
  949. pinmux = 0;
  950. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  951. value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
  952. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  953. value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
  954. (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
  955. tegra_pmc_writel(value, PMC_SCRATCH54);
  956. value = PMC_SCRATCH55_RESET_TEGRA;
  957. value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
  958. value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
  959. value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
  960. /*
  961. * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
  962. * contain the checksum and are currently zero, so they are not added.
  963. */
  964. checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
  965. + ((value >> 24) & 0xff);
  966. checksum &= 0xff;
  967. checksum = 0x100 - checksum;
  968. value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
  969. tegra_pmc_writel(value, PMC_SCRATCH55);
  970. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  971. value |= PMC_SENSOR_CTRL_ENABLE_RST;
  972. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  973. dev_info(pmc->dev, "emergency thermal reset enabled\n");
  974. out:
  975. of_node_put(np);
  976. }
  977. static int tegra_pmc_probe(struct platform_device *pdev)
  978. {
  979. void __iomem *base;
  980. struct resource *res;
  981. int err;
  982. /*
  983. * Early initialisation should have configured an initial
  984. * register mapping and setup the soc data pointer. If these
  985. * are not valid then something went badly wrong!
  986. */
  987. if (WARN_ON(!pmc->base || !pmc->soc))
  988. return -ENODEV;
  989. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  990. if (err < 0)
  991. return err;
  992. /* take over the memory region from the early initialization */
  993. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  994. base = devm_ioremap_resource(&pdev->dev, res);
  995. if (IS_ERR(base))
  996. return PTR_ERR(base);
  997. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  998. if (IS_ERR(pmc->clk)) {
  999. err = PTR_ERR(pmc->clk);
  1000. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  1001. return err;
  1002. }
  1003. pmc->dev = &pdev->dev;
  1004. tegra_pmc_init(pmc);
  1005. tegra_pmc_init_tsense_reset(pmc);
  1006. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1007. err = tegra_powergate_debugfs_init();
  1008. if (err < 0)
  1009. return err;
  1010. }
  1011. err = register_restart_handler(&tegra_pmc_restart_handler);
  1012. if (err) {
  1013. debugfs_remove(pmc->debugfs);
  1014. dev_err(&pdev->dev, "unable to register restart handler, %d\n",
  1015. err);
  1016. return err;
  1017. }
  1018. mutex_lock(&pmc->powergates_lock);
  1019. iounmap(pmc->base);
  1020. pmc->base = base;
  1021. mutex_unlock(&pmc->powergates_lock);
  1022. return 0;
  1023. }
  1024. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  1025. static int tegra_pmc_suspend(struct device *dev)
  1026. {
  1027. tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
  1028. return 0;
  1029. }
  1030. static int tegra_pmc_resume(struct device *dev)
  1031. {
  1032. tegra_pmc_writel(0x0, PMC_SCRATCH41);
  1033. return 0;
  1034. }
  1035. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  1036. #endif
  1037. static const char * const tegra20_powergates[] = {
  1038. [TEGRA_POWERGATE_CPU] = "cpu",
  1039. [TEGRA_POWERGATE_3D] = "3d",
  1040. [TEGRA_POWERGATE_VENC] = "venc",
  1041. [TEGRA_POWERGATE_VDEC] = "vdec",
  1042. [TEGRA_POWERGATE_PCIE] = "pcie",
  1043. [TEGRA_POWERGATE_L2] = "l2",
  1044. [TEGRA_POWERGATE_MPE] = "mpe",
  1045. };
  1046. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  1047. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  1048. .powergates = tegra20_powergates,
  1049. .num_cpu_powergates = 0,
  1050. .cpu_powergates = NULL,
  1051. .has_tsense_reset = false,
  1052. .has_gpu_clamps = false,
  1053. };
  1054. static const char * const tegra30_powergates[] = {
  1055. [TEGRA_POWERGATE_CPU] = "cpu0",
  1056. [TEGRA_POWERGATE_3D] = "3d0",
  1057. [TEGRA_POWERGATE_VENC] = "venc",
  1058. [TEGRA_POWERGATE_VDEC] = "vdec",
  1059. [TEGRA_POWERGATE_PCIE] = "pcie",
  1060. [TEGRA_POWERGATE_L2] = "l2",
  1061. [TEGRA_POWERGATE_MPE] = "mpe",
  1062. [TEGRA_POWERGATE_HEG] = "heg",
  1063. [TEGRA_POWERGATE_SATA] = "sata",
  1064. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1065. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1066. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1067. [TEGRA_POWERGATE_CELP] = "celp",
  1068. [TEGRA_POWERGATE_3D1] = "3d1",
  1069. };
  1070. static const u8 tegra30_cpu_powergates[] = {
  1071. TEGRA_POWERGATE_CPU,
  1072. TEGRA_POWERGATE_CPU1,
  1073. TEGRA_POWERGATE_CPU2,
  1074. TEGRA_POWERGATE_CPU3,
  1075. };
  1076. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  1077. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  1078. .powergates = tegra30_powergates,
  1079. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  1080. .cpu_powergates = tegra30_cpu_powergates,
  1081. .has_tsense_reset = true,
  1082. .has_gpu_clamps = false,
  1083. };
  1084. static const char * const tegra114_powergates[] = {
  1085. [TEGRA_POWERGATE_CPU] = "crail",
  1086. [TEGRA_POWERGATE_3D] = "3d",
  1087. [TEGRA_POWERGATE_VENC] = "venc",
  1088. [TEGRA_POWERGATE_VDEC] = "vdec",
  1089. [TEGRA_POWERGATE_MPE] = "mpe",
  1090. [TEGRA_POWERGATE_HEG] = "heg",
  1091. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1092. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1093. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1094. [TEGRA_POWERGATE_CELP] = "celp",
  1095. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1096. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1097. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1098. [TEGRA_POWERGATE_DIS] = "dis",
  1099. [TEGRA_POWERGATE_DISB] = "disb",
  1100. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1101. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1102. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1103. };
  1104. static const u8 tegra114_cpu_powergates[] = {
  1105. TEGRA_POWERGATE_CPU0,
  1106. TEGRA_POWERGATE_CPU1,
  1107. TEGRA_POWERGATE_CPU2,
  1108. TEGRA_POWERGATE_CPU3,
  1109. };
  1110. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  1111. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  1112. .powergates = tegra114_powergates,
  1113. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  1114. .cpu_powergates = tegra114_cpu_powergates,
  1115. .has_tsense_reset = true,
  1116. .has_gpu_clamps = false,
  1117. };
  1118. static const char * const tegra124_powergates[] = {
  1119. [TEGRA_POWERGATE_CPU] = "crail",
  1120. [TEGRA_POWERGATE_3D] = "3d",
  1121. [TEGRA_POWERGATE_VENC] = "venc",
  1122. [TEGRA_POWERGATE_PCIE] = "pcie",
  1123. [TEGRA_POWERGATE_VDEC] = "vdec",
  1124. [TEGRA_POWERGATE_MPE] = "mpe",
  1125. [TEGRA_POWERGATE_HEG] = "heg",
  1126. [TEGRA_POWERGATE_SATA] = "sata",
  1127. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1128. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1129. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1130. [TEGRA_POWERGATE_CELP] = "celp",
  1131. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1132. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1133. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1134. [TEGRA_POWERGATE_SOR] = "sor",
  1135. [TEGRA_POWERGATE_DIS] = "dis",
  1136. [TEGRA_POWERGATE_DISB] = "disb",
  1137. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1138. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1139. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1140. [TEGRA_POWERGATE_VIC] = "vic",
  1141. [TEGRA_POWERGATE_IRAM] = "iram",
  1142. };
  1143. static const u8 tegra124_cpu_powergates[] = {
  1144. TEGRA_POWERGATE_CPU0,
  1145. TEGRA_POWERGATE_CPU1,
  1146. TEGRA_POWERGATE_CPU2,
  1147. TEGRA_POWERGATE_CPU3,
  1148. };
  1149. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  1150. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  1151. .powergates = tegra124_powergates,
  1152. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  1153. .cpu_powergates = tegra124_cpu_powergates,
  1154. .has_tsense_reset = true,
  1155. .has_gpu_clamps = true,
  1156. };
  1157. static const char * const tegra210_powergates[] = {
  1158. [TEGRA_POWERGATE_CPU] = "crail",
  1159. [TEGRA_POWERGATE_3D] = "3d",
  1160. [TEGRA_POWERGATE_VENC] = "venc",
  1161. [TEGRA_POWERGATE_PCIE] = "pcie",
  1162. [TEGRA_POWERGATE_MPE] = "mpe",
  1163. [TEGRA_POWERGATE_SATA] = "sata",
  1164. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1165. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1166. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1167. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1168. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1169. [TEGRA_POWERGATE_SOR] = "sor",
  1170. [TEGRA_POWERGATE_DIS] = "dis",
  1171. [TEGRA_POWERGATE_DISB] = "disb",
  1172. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1173. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1174. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1175. [TEGRA_POWERGATE_VIC] = "vic",
  1176. [TEGRA_POWERGATE_IRAM] = "iram",
  1177. [TEGRA_POWERGATE_NVDEC] = "nvdec",
  1178. [TEGRA_POWERGATE_NVJPG] = "nvjpg",
  1179. [TEGRA_POWERGATE_AUD] = "aud",
  1180. [TEGRA_POWERGATE_DFD] = "dfd",
  1181. [TEGRA_POWERGATE_VE2] = "ve2",
  1182. };
  1183. static const u8 tegra210_cpu_powergates[] = {
  1184. TEGRA_POWERGATE_CPU0,
  1185. TEGRA_POWERGATE_CPU1,
  1186. TEGRA_POWERGATE_CPU2,
  1187. TEGRA_POWERGATE_CPU3,
  1188. };
  1189. static const struct tegra_pmc_soc tegra210_pmc_soc = {
  1190. .num_powergates = ARRAY_SIZE(tegra210_powergates),
  1191. .powergates = tegra210_powergates,
  1192. .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
  1193. .cpu_powergates = tegra210_cpu_powergates,
  1194. .has_tsense_reset = true,
  1195. .has_gpu_clamps = true,
  1196. };
  1197. static const struct of_device_id tegra_pmc_match[] = {
  1198. { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
  1199. { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
  1200. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  1201. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  1202. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  1203. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  1204. { }
  1205. };
  1206. static struct platform_driver tegra_pmc_driver = {
  1207. .driver = {
  1208. .name = "tegra-pmc",
  1209. .suppress_bind_attrs = true,
  1210. .of_match_table = tegra_pmc_match,
  1211. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  1212. .pm = &tegra_pmc_pm_ops,
  1213. #endif
  1214. },
  1215. .probe = tegra_pmc_probe,
  1216. };
  1217. builtin_platform_driver(tegra_pmc_driver);
  1218. /*
  1219. * Early initialization to allow access to registers in the very early boot
  1220. * process.
  1221. */
  1222. static int __init tegra_pmc_early_init(void)
  1223. {
  1224. const struct of_device_id *match;
  1225. struct device_node *np;
  1226. struct resource regs;
  1227. bool invert;
  1228. u32 value;
  1229. mutex_init(&pmc->powergates_lock);
  1230. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  1231. if (!np) {
  1232. /*
  1233. * Fall back to legacy initialization for 32-bit ARM only. All
  1234. * 64-bit ARM device tree files for Tegra are required to have
  1235. * a PMC node.
  1236. *
  1237. * This is for backwards-compatibility with old device trees
  1238. * that didn't contain a PMC node. Note that in this case the
  1239. * SoC data can't be matched and therefore powergating is
  1240. * disabled.
  1241. */
  1242. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  1243. pr_warn("DT node not found, powergating disabled\n");
  1244. regs.start = 0x7000e400;
  1245. regs.end = 0x7000e7ff;
  1246. regs.flags = IORESOURCE_MEM;
  1247. pr_warn("Using memory region %pR\n", &regs);
  1248. } else {
  1249. /*
  1250. * At this point we're not running on Tegra, so play
  1251. * nice with multi-platform kernels.
  1252. */
  1253. return 0;
  1254. }
  1255. } else {
  1256. /*
  1257. * Extract information from the device tree if we've found a
  1258. * matching node.
  1259. */
  1260. if (of_address_to_resource(np, 0, &regs) < 0) {
  1261. pr_err("failed to get PMC registers\n");
  1262. of_node_put(np);
  1263. return -ENXIO;
  1264. }
  1265. }
  1266. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  1267. if (!pmc->base) {
  1268. pr_err("failed to map PMC registers\n");
  1269. of_node_put(np);
  1270. return -ENXIO;
  1271. }
  1272. if (np) {
  1273. pmc->soc = match->data;
  1274. tegra_powergate_init(pmc, np);
  1275. /*
  1276. * Invert the interrupt polarity if a PMC device tree node
  1277. * exists and contains the nvidia,invert-interrupt property.
  1278. */
  1279. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  1280. value = tegra_pmc_readl(PMC_CNTRL);
  1281. if (invert)
  1282. value |= PMC_CNTRL_INTR_POLARITY;
  1283. else
  1284. value &= ~PMC_CNTRL_INTR_POLARITY;
  1285. tegra_pmc_writel(value, PMC_CNTRL);
  1286. of_node_put(np);
  1287. }
  1288. return 0;
  1289. }
  1290. early_initcall(tegra_pmc_early_init);