pmc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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, value;
  765. unsigned int bit, mask;
  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. mask = 1 << bit;
  772. value = tegra_pmc_readl(request);
  773. value |= mask;
  774. value &= ~IO_DPD_REQ_CODE_MASK;
  775. value |= IO_DPD_REQ_CODE_OFF;
  776. tegra_pmc_writel(value, request);
  777. err = tegra_io_rail_poll(status, mask, 0, 250);
  778. if (err) {
  779. pr_info("tegra_io_rail_poll() failed: %d\n", err);
  780. goto error;
  781. }
  782. tegra_io_rail_unprepare();
  783. error:
  784. mutex_unlock(&pmc->powergates_lock);
  785. return err;
  786. }
  787. EXPORT_SYMBOL(tegra_io_rail_power_on);
  788. int tegra_io_rail_power_off(unsigned int id)
  789. {
  790. unsigned long request, status, value;
  791. unsigned int bit, mask;
  792. int err;
  793. mutex_lock(&pmc->powergates_lock);
  794. err = tegra_io_rail_prepare(id, &request, &status, &bit);
  795. if (err) {
  796. pr_info("tegra_io_rail_prepare() failed: %d\n", err);
  797. goto error;
  798. }
  799. mask = 1 << bit;
  800. value = tegra_pmc_readl(request);
  801. value |= mask;
  802. value &= ~IO_DPD_REQ_CODE_MASK;
  803. value |= IO_DPD_REQ_CODE_ON;
  804. tegra_pmc_writel(value, request);
  805. err = tegra_io_rail_poll(status, mask, mask, 250);
  806. if (err)
  807. goto error;
  808. tegra_io_rail_unprepare();
  809. error:
  810. mutex_unlock(&pmc->powergates_lock);
  811. return err;
  812. }
  813. EXPORT_SYMBOL(tegra_io_rail_power_off);
  814. #ifdef CONFIG_PM_SLEEP
  815. enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
  816. {
  817. return pmc->suspend_mode;
  818. }
  819. void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
  820. {
  821. if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
  822. return;
  823. pmc->suspend_mode = mode;
  824. }
  825. void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  826. {
  827. unsigned long long rate = 0;
  828. u32 value;
  829. switch (mode) {
  830. case TEGRA_SUSPEND_LP1:
  831. rate = 32768;
  832. break;
  833. case TEGRA_SUSPEND_LP2:
  834. rate = clk_get_rate(pmc->clk);
  835. break;
  836. default:
  837. break;
  838. }
  839. if (WARN_ON_ONCE(rate == 0))
  840. rate = 100000000;
  841. if (rate != pmc->rate) {
  842. u64 ticks;
  843. ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
  844. do_div(ticks, USEC_PER_SEC);
  845. tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
  846. ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
  847. do_div(ticks, USEC_PER_SEC);
  848. tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
  849. wmb();
  850. pmc->rate = rate;
  851. }
  852. value = tegra_pmc_readl(PMC_CNTRL);
  853. value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
  854. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  855. tegra_pmc_writel(value, PMC_CNTRL);
  856. }
  857. #endif
  858. static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
  859. {
  860. u32 value, values[2];
  861. if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
  862. } else {
  863. switch (value) {
  864. case 0:
  865. pmc->suspend_mode = TEGRA_SUSPEND_LP0;
  866. break;
  867. case 1:
  868. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  869. break;
  870. case 2:
  871. pmc->suspend_mode = TEGRA_SUSPEND_LP2;
  872. break;
  873. default:
  874. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  875. break;
  876. }
  877. }
  878. pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
  879. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
  880. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  881. pmc->cpu_good_time = value;
  882. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
  883. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  884. pmc->cpu_off_time = value;
  885. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  886. values, ARRAY_SIZE(values)))
  887. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  888. pmc->core_osc_time = values[0];
  889. pmc->core_pmu_time = values[1];
  890. if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
  891. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  892. pmc->core_off_time = value;
  893. pmc->corereq_high = of_property_read_bool(np,
  894. "nvidia,core-power-req-active-high");
  895. pmc->sysclkreq_high = of_property_read_bool(np,
  896. "nvidia,sys-clock-req-active-high");
  897. pmc->combined_req = of_property_read_bool(np,
  898. "nvidia,combined-power-req");
  899. pmc->cpu_pwr_good_en = of_property_read_bool(np,
  900. "nvidia,cpu-pwr-good-en");
  901. if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
  902. ARRAY_SIZE(values)))
  903. if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
  904. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  905. pmc->lp0_vec_phys = values[0];
  906. pmc->lp0_vec_size = values[1];
  907. return 0;
  908. }
  909. static void tegra_pmc_init(struct tegra_pmc *pmc)
  910. {
  911. u32 value;
  912. /* Always enable CPU power request */
  913. value = tegra_pmc_readl(PMC_CNTRL);
  914. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  915. tegra_pmc_writel(value, PMC_CNTRL);
  916. value = tegra_pmc_readl(PMC_CNTRL);
  917. if (pmc->sysclkreq_high)
  918. value &= ~PMC_CNTRL_SYSCLK_POLARITY;
  919. else
  920. value |= PMC_CNTRL_SYSCLK_POLARITY;
  921. /* configure the output polarity while the request is tristated */
  922. tegra_pmc_writel(value, PMC_CNTRL);
  923. /* now enable the request */
  924. value = tegra_pmc_readl(PMC_CNTRL);
  925. value |= PMC_CNTRL_SYSCLK_OE;
  926. tegra_pmc_writel(value, PMC_CNTRL);
  927. }
  928. static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
  929. {
  930. static const char disabled[] = "emergency thermal reset disabled";
  931. u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
  932. struct device *dev = pmc->dev;
  933. struct device_node *np;
  934. u32 value, checksum;
  935. if (!pmc->soc->has_tsense_reset)
  936. return;
  937. np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
  938. if (!np) {
  939. dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
  940. return;
  941. }
  942. if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
  943. dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
  944. goto out;
  945. }
  946. if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
  947. dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
  948. goto out;
  949. }
  950. if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
  951. dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
  952. goto out;
  953. }
  954. if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
  955. dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
  956. goto out;
  957. }
  958. if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
  959. pinmux = 0;
  960. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  961. value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
  962. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  963. value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
  964. (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
  965. tegra_pmc_writel(value, PMC_SCRATCH54);
  966. value = PMC_SCRATCH55_RESET_TEGRA;
  967. value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
  968. value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
  969. value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
  970. /*
  971. * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
  972. * contain the checksum and are currently zero, so they are not added.
  973. */
  974. checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
  975. + ((value >> 24) & 0xff);
  976. checksum &= 0xff;
  977. checksum = 0x100 - checksum;
  978. value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
  979. tegra_pmc_writel(value, PMC_SCRATCH55);
  980. value = tegra_pmc_readl(PMC_SENSOR_CTRL);
  981. value |= PMC_SENSOR_CTRL_ENABLE_RST;
  982. tegra_pmc_writel(value, PMC_SENSOR_CTRL);
  983. dev_info(pmc->dev, "emergency thermal reset enabled\n");
  984. out:
  985. of_node_put(np);
  986. }
  987. static int tegra_pmc_probe(struct platform_device *pdev)
  988. {
  989. void __iomem *base;
  990. struct resource *res;
  991. int err;
  992. /*
  993. * Early initialisation should have configured an initial
  994. * register mapping and setup the soc data pointer. If these
  995. * are not valid then something went badly wrong!
  996. */
  997. if (WARN_ON(!pmc->base || !pmc->soc))
  998. return -ENODEV;
  999. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  1000. if (err < 0)
  1001. return err;
  1002. /* take over the memory region from the early initialization */
  1003. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1004. base = devm_ioremap_resource(&pdev->dev, res);
  1005. if (IS_ERR(base))
  1006. return PTR_ERR(base);
  1007. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  1008. if (IS_ERR(pmc->clk)) {
  1009. err = PTR_ERR(pmc->clk);
  1010. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  1011. return err;
  1012. }
  1013. pmc->dev = &pdev->dev;
  1014. tegra_pmc_init(pmc);
  1015. tegra_pmc_init_tsense_reset(pmc);
  1016. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1017. err = tegra_powergate_debugfs_init();
  1018. if (err < 0)
  1019. return err;
  1020. }
  1021. err = register_restart_handler(&tegra_pmc_restart_handler);
  1022. if (err) {
  1023. debugfs_remove(pmc->debugfs);
  1024. dev_err(&pdev->dev, "unable to register restart handler, %d\n",
  1025. err);
  1026. return err;
  1027. }
  1028. mutex_lock(&pmc->powergates_lock);
  1029. iounmap(pmc->base);
  1030. pmc->base = base;
  1031. mutex_unlock(&pmc->powergates_lock);
  1032. return 0;
  1033. }
  1034. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  1035. static int tegra_pmc_suspend(struct device *dev)
  1036. {
  1037. tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
  1038. return 0;
  1039. }
  1040. static int tegra_pmc_resume(struct device *dev)
  1041. {
  1042. tegra_pmc_writel(0x0, PMC_SCRATCH41);
  1043. return 0;
  1044. }
  1045. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  1046. #endif
  1047. static const char * const tegra20_powergates[] = {
  1048. [TEGRA_POWERGATE_CPU] = "cpu",
  1049. [TEGRA_POWERGATE_3D] = "3d",
  1050. [TEGRA_POWERGATE_VENC] = "venc",
  1051. [TEGRA_POWERGATE_VDEC] = "vdec",
  1052. [TEGRA_POWERGATE_PCIE] = "pcie",
  1053. [TEGRA_POWERGATE_L2] = "l2",
  1054. [TEGRA_POWERGATE_MPE] = "mpe",
  1055. };
  1056. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  1057. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  1058. .powergates = tegra20_powergates,
  1059. .num_cpu_powergates = 0,
  1060. .cpu_powergates = NULL,
  1061. .has_tsense_reset = false,
  1062. .has_gpu_clamps = false,
  1063. };
  1064. static const char * const tegra30_powergates[] = {
  1065. [TEGRA_POWERGATE_CPU] = "cpu0",
  1066. [TEGRA_POWERGATE_3D] = "3d0",
  1067. [TEGRA_POWERGATE_VENC] = "venc",
  1068. [TEGRA_POWERGATE_VDEC] = "vdec",
  1069. [TEGRA_POWERGATE_PCIE] = "pcie",
  1070. [TEGRA_POWERGATE_L2] = "l2",
  1071. [TEGRA_POWERGATE_MPE] = "mpe",
  1072. [TEGRA_POWERGATE_HEG] = "heg",
  1073. [TEGRA_POWERGATE_SATA] = "sata",
  1074. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1075. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1076. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1077. [TEGRA_POWERGATE_CELP] = "celp",
  1078. [TEGRA_POWERGATE_3D1] = "3d1",
  1079. };
  1080. static const u8 tegra30_cpu_powergates[] = {
  1081. TEGRA_POWERGATE_CPU,
  1082. TEGRA_POWERGATE_CPU1,
  1083. TEGRA_POWERGATE_CPU2,
  1084. TEGRA_POWERGATE_CPU3,
  1085. };
  1086. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  1087. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  1088. .powergates = tegra30_powergates,
  1089. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  1090. .cpu_powergates = tegra30_cpu_powergates,
  1091. .has_tsense_reset = true,
  1092. .has_gpu_clamps = false,
  1093. };
  1094. static const char * const tegra114_powergates[] = {
  1095. [TEGRA_POWERGATE_CPU] = "crail",
  1096. [TEGRA_POWERGATE_3D] = "3d",
  1097. [TEGRA_POWERGATE_VENC] = "venc",
  1098. [TEGRA_POWERGATE_VDEC] = "vdec",
  1099. [TEGRA_POWERGATE_MPE] = "mpe",
  1100. [TEGRA_POWERGATE_HEG] = "heg",
  1101. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1102. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1103. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1104. [TEGRA_POWERGATE_CELP] = "celp",
  1105. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1106. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1107. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1108. [TEGRA_POWERGATE_DIS] = "dis",
  1109. [TEGRA_POWERGATE_DISB] = "disb",
  1110. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1111. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1112. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1113. };
  1114. static const u8 tegra114_cpu_powergates[] = {
  1115. TEGRA_POWERGATE_CPU0,
  1116. TEGRA_POWERGATE_CPU1,
  1117. TEGRA_POWERGATE_CPU2,
  1118. TEGRA_POWERGATE_CPU3,
  1119. };
  1120. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  1121. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  1122. .powergates = tegra114_powergates,
  1123. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  1124. .cpu_powergates = tegra114_cpu_powergates,
  1125. .has_tsense_reset = true,
  1126. .has_gpu_clamps = false,
  1127. };
  1128. static const char * const tegra124_powergates[] = {
  1129. [TEGRA_POWERGATE_CPU] = "crail",
  1130. [TEGRA_POWERGATE_3D] = "3d",
  1131. [TEGRA_POWERGATE_VENC] = "venc",
  1132. [TEGRA_POWERGATE_PCIE] = "pcie",
  1133. [TEGRA_POWERGATE_VDEC] = "vdec",
  1134. [TEGRA_POWERGATE_MPE] = "mpe",
  1135. [TEGRA_POWERGATE_HEG] = "heg",
  1136. [TEGRA_POWERGATE_SATA] = "sata",
  1137. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1138. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1139. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1140. [TEGRA_POWERGATE_CELP] = "celp",
  1141. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1142. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1143. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1144. [TEGRA_POWERGATE_SOR] = "sor",
  1145. [TEGRA_POWERGATE_DIS] = "dis",
  1146. [TEGRA_POWERGATE_DISB] = "disb",
  1147. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1148. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1149. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1150. [TEGRA_POWERGATE_VIC] = "vic",
  1151. [TEGRA_POWERGATE_IRAM] = "iram",
  1152. };
  1153. static const u8 tegra124_cpu_powergates[] = {
  1154. TEGRA_POWERGATE_CPU0,
  1155. TEGRA_POWERGATE_CPU1,
  1156. TEGRA_POWERGATE_CPU2,
  1157. TEGRA_POWERGATE_CPU3,
  1158. };
  1159. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  1160. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  1161. .powergates = tegra124_powergates,
  1162. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  1163. .cpu_powergates = tegra124_cpu_powergates,
  1164. .has_tsense_reset = true,
  1165. .has_gpu_clamps = true,
  1166. };
  1167. static const char * const tegra210_powergates[] = {
  1168. [TEGRA_POWERGATE_CPU] = "crail",
  1169. [TEGRA_POWERGATE_3D] = "3d",
  1170. [TEGRA_POWERGATE_VENC] = "venc",
  1171. [TEGRA_POWERGATE_PCIE] = "pcie",
  1172. [TEGRA_POWERGATE_MPE] = "mpe",
  1173. [TEGRA_POWERGATE_SATA] = "sata",
  1174. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1175. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1176. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1177. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1178. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1179. [TEGRA_POWERGATE_SOR] = "sor",
  1180. [TEGRA_POWERGATE_DIS] = "dis",
  1181. [TEGRA_POWERGATE_DISB] = "disb",
  1182. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1183. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1184. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1185. [TEGRA_POWERGATE_VIC] = "vic",
  1186. [TEGRA_POWERGATE_IRAM] = "iram",
  1187. [TEGRA_POWERGATE_NVDEC] = "nvdec",
  1188. [TEGRA_POWERGATE_NVJPG] = "nvjpg",
  1189. [TEGRA_POWERGATE_AUD] = "aud",
  1190. [TEGRA_POWERGATE_DFD] = "dfd",
  1191. [TEGRA_POWERGATE_VE2] = "ve2",
  1192. };
  1193. static const u8 tegra210_cpu_powergates[] = {
  1194. TEGRA_POWERGATE_CPU0,
  1195. TEGRA_POWERGATE_CPU1,
  1196. TEGRA_POWERGATE_CPU2,
  1197. TEGRA_POWERGATE_CPU3,
  1198. };
  1199. static const struct tegra_pmc_soc tegra210_pmc_soc = {
  1200. .num_powergates = ARRAY_SIZE(tegra210_powergates),
  1201. .powergates = tegra210_powergates,
  1202. .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
  1203. .cpu_powergates = tegra210_cpu_powergates,
  1204. .has_tsense_reset = true,
  1205. .has_gpu_clamps = true,
  1206. };
  1207. static const struct of_device_id tegra_pmc_match[] = {
  1208. { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
  1209. { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
  1210. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  1211. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  1212. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  1213. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  1214. { }
  1215. };
  1216. static struct platform_driver tegra_pmc_driver = {
  1217. .driver = {
  1218. .name = "tegra-pmc",
  1219. .suppress_bind_attrs = true,
  1220. .of_match_table = tegra_pmc_match,
  1221. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  1222. .pm = &tegra_pmc_pm_ops,
  1223. #endif
  1224. },
  1225. .probe = tegra_pmc_probe,
  1226. };
  1227. builtin_platform_driver(tegra_pmc_driver);
  1228. /*
  1229. * Early initialization to allow access to registers in the very early boot
  1230. * process.
  1231. */
  1232. static int __init tegra_pmc_early_init(void)
  1233. {
  1234. const struct of_device_id *match;
  1235. struct device_node *np;
  1236. struct resource regs;
  1237. bool invert;
  1238. u32 value;
  1239. mutex_init(&pmc->powergates_lock);
  1240. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  1241. if (!np) {
  1242. /*
  1243. * Fall back to legacy initialization for 32-bit ARM only. All
  1244. * 64-bit ARM device tree files for Tegra are required to have
  1245. * a PMC node.
  1246. *
  1247. * This is for backwards-compatibility with old device trees
  1248. * that didn't contain a PMC node. Note that in this case the
  1249. * SoC data can't be matched and therefore powergating is
  1250. * disabled.
  1251. */
  1252. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  1253. pr_warn("DT node not found, powergating disabled\n");
  1254. regs.start = 0x7000e400;
  1255. regs.end = 0x7000e7ff;
  1256. regs.flags = IORESOURCE_MEM;
  1257. pr_warn("Using memory region %pR\n", &regs);
  1258. } else {
  1259. /*
  1260. * At this point we're not running on Tegra, so play
  1261. * nice with multi-platform kernels.
  1262. */
  1263. return 0;
  1264. }
  1265. } else {
  1266. /*
  1267. * Extract information from the device tree if we've found a
  1268. * matching node.
  1269. */
  1270. if (of_address_to_resource(np, 0, &regs) < 0) {
  1271. pr_err("failed to get PMC registers\n");
  1272. of_node_put(np);
  1273. return -ENXIO;
  1274. }
  1275. }
  1276. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  1277. if (!pmc->base) {
  1278. pr_err("failed to map PMC registers\n");
  1279. of_node_put(np);
  1280. return -ENXIO;
  1281. }
  1282. if (np) {
  1283. pmc->soc = match->data;
  1284. tegra_powergate_init(pmc, np);
  1285. /*
  1286. * Invert the interrupt polarity if a PMC device tree node
  1287. * exists and contains the nvidia,invert-interrupt property.
  1288. */
  1289. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  1290. value = tegra_pmc_readl(PMC_CNTRL);
  1291. if (invert)
  1292. value |= PMC_CNTRL_INTR_POLARITY;
  1293. else
  1294. value &= ~PMC_CNTRL_INTR_POLARITY;
  1295. tegra_pmc_writel(value, PMC_CNTRL);
  1296. of_node_put(np);
  1297. }
  1298. return 0;
  1299. }
  1300. early_initcall(tegra_pmc_early_init);