pmc.c 50 KB

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