pmc.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/soc/tegra/pmc.c
  4. *
  5. * Copyright (c) 2010 Google, Inc
  6. * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  7. *
  8. * Author:
  9. * Colin Cross <ccross@google.com>
  10. */
  11. #define pr_fmt(fmt) "tegra-pmc: " fmt
  12. #include <linux/arm-smccc.h>
  13. #include <linux/clk.h>
  14. #include <linux/clk/tegra.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/export.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/irq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_clk.h>
  27. #include <linux/of.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/pinctrl/pinconf-generic.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinctrl.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. #include <dt-bindings/interrupt-controller/arm-gic.h>
  44. #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
  45. #include <dt-bindings/gpio/tegra186-gpio.h>
  46. #include <dt-bindings/gpio/tegra194-gpio.h>
  47. #define PMC_CNTRL 0x0
  48. #define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
  49. #define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
  50. #define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
  51. #define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
  52. #define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
  53. #define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
  54. #define PMC_CNTRL_MAIN_RST BIT(4)
  55. #define DPD_SAMPLE 0x020
  56. #define DPD_SAMPLE_ENABLE BIT(0)
  57. #define DPD_SAMPLE_DISABLE (0 << 0)
  58. #define PWRGATE_TOGGLE 0x30
  59. #define PWRGATE_TOGGLE_START BIT(8)
  60. #define REMOVE_CLAMPING 0x34
  61. #define PWRGATE_STATUS 0x38
  62. #define PMC_IMPL_E_33V_PWR 0x40
  63. #define PMC_PWR_DET 0x48
  64. #define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
  65. #define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
  66. #define PMC_SCRATCH0_MODE_RCM BIT(1)
  67. #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
  68. PMC_SCRATCH0_MODE_BOOTLOADER | \
  69. PMC_SCRATCH0_MODE_RCM)
  70. #define PMC_CPUPWRGOOD_TIMER 0xc8
  71. #define PMC_CPUPWROFF_TIMER 0xcc
  72. #define PMC_PWR_DET_VALUE 0xe4
  73. #define PMC_SCRATCH41 0x140
  74. #define PMC_SENSOR_CTRL 0x1b0
  75. #define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
  76. #define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
  77. #define PMC_RST_STATUS_POR 0
  78. #define PMC_RST_STATUS_WATCHDOG 1
  79. #define PMC_RST_STATUS_SENSOR 2
  80. #define PMC_RST_STATUS_SW_MAIN 3
  81. #define PMC_RST_STATUS_LP0 4
  82. #define PMC_RST_STATUS_AOTAG 5
  83. #define IO_DPD_REQ 0x1b8
  84. #define IO_DPD_REQ_CODE_IDLE (0U << 30)
  85. #define IO_DPD_REQ_CODE_OFF (1U << 30)
  86. #define IO_DPD_REQ_CODE_ON (2U << 30)
  87. #define IO_DPD_REQ_CODE_MASK (3U << 30)
  88. #define IO_DPD_STATUS 0x1bc
  89. #define IO_DPD2_REQ 0x1c0
  90. #define IO_DPD2_STATUS 0x1c4
  91. #define SEL_DPD_TIM 0x1c8
  92. #define PMC_SCRATCH54 0x258
  93. #define PMC_SCRATCH54_DATA_SHIFT 8
  94. #define PMC_SCRATCH54_ADDR_SHIFT 0
  95. #define PMC_SCRATCH55 0x25c
  96. #define PMC_SCRATCH55_RESET_TEGRA BIT(31)
  97. #define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
  98. #define PMC_SCRATCH55_PINMUX_SHIFT 24
  99. #define PMC_SCRATCH55_16BITOP BIT(15)
  100. #define PMC_SCRATCH55_CHECKSUM_SHIFT 16
  101. #define PMC_SCRATCH55_I2CSLV1_SHIFT 0
  102. #define GPU_RG_CNTRL 0x2d4
  103. /* Tegra186 and later */
  104. #define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
  105. #define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
  106. #define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2))
  107. #define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2))
  108. #define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2))
  109. #define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2))
  110. #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
  111. #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
  112. #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
  113. #define WAKE_AOWAKE_CTRL 0x4f4
  114. #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
  115. /* for secure PMC */
  116. #define TEGRA_SMC_PMC 0xc2fffe00
  117. #define TEGRA_SMC_PMC_READ 0xaa
  118. #define TEGRA_SMC_PMC_WRITE 0xbb
  119. struct tegra_powergate {
  120. struct generic_pm_domain genpd;
  121. struct tegra_pmc *pmc;
  122. unsigned int id;
  123. struct clk **clks;
  124. unsigned int num_clks;
  125. struct reset_control *reset;
  126. };
  127. struct tegra_io_pad_soc {
  128. enum tegra_io_pad id;
  129. unsigned int dpd;
  130. unsigned int voltage;
  131. const char *name;
  132. };
  133. struct tegra_pmc_regs {
  134. unsigned int scratch0;
  135. unsigned int dpd_req;
  136. unsigned int dpd_status;
  137. unsigned int dpd2_req;
  138. unsigned int dpd2_status;
  139. unsigned int rst_status;
  140. unsigned int rst_source_shift;
  141. unsigned int rst_source_mask;
  142. unsigned int rst_level_shift;
  143. unsigned int rst_level_mask;
  144. };
  145. struct tegra_wake_event {
  146. const char *name;
  147. unsigned int id;
  148. unsigned int irq;
  149. struct {
  150. unsigned int instance;
  151. unsigned int pin;
  152. } gpio;
  153. };
  154. #define TEGRA_WAKE_IRQ(_name, _id, _irq) \
  155. { \
  156. .name = _name, \
  157. .id = _id, \
  158. .irq = _irq, \
  159. .gpio = { \
  160. .instance = UINT_MAX, \
  161. .pin = UINT_MAX, \
  162. }, \
  163. }
  164. #define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin) \
  165. { \
  166. .name = _name, \
  167. .id = _id, \
  168. .irq = 0, \
  169. .gpio = { \
  170. .instance = _instance, \
  171. .pin = _pin, \
  172. }, \
  173. }
  174. struct tegra_pmc_soc {
  175. unsigned int num_powergates;
  176. const char *const *powergates;
  177. unsigned int num_cpu_powergates;
  178. const u8 *cpu_powergates;
  179. bool has_tsense_reset;
  180. bool has_gpu_clamps;
  181. bool needs_mbist_war;
  182. bool has_impl_33v_pwr;
  183. bool maybe_tz_only;
  184. const struct tegra_io_pad_soc *io_pads;
  185. unsigned int num_io_pads;
  186. const struct pinctrl_pin_desc *pin_descs;
  187. unsigned int num_pin_descs;
  188. const struct tegra_pmc_regs *regs;
  189. void (*init)(struct tegra_pmc *pmc);
  190. void (*setup_irq_polarity)(struct tegra_pmc *pmc,
  191. struct device_node *np,
  192. bool invert);
  193. const char * const *reset_sources;
  194. unsigned int num_reset_sources;
  195. const char * const *reset_levels;
  196. unsigned int num_reset_levels;
  197. /*
  198. * These describe events that can wake the system from sleep (i.e.
  199. * LP0 or SC7). Wakeup from other sleep states (such as LP1 or LP2)
  200. * are dealt with in the LIC.
  201. */
  202. const struct tegra_wake_event *wake_events;
  203. unsigned int num_wake_events;
  204. };
  205. static const char * const tegra186_reset_sources[] = {
  206. "SYS_RESET",
  207. "AOWDT",
  208. "MCCPLEXWDT",
  209. "BPMPWDT",
  210. "SCEWDT",
  211. "SPEWDT",
  212. "APEWDT",
  213. "BCCPLEXWDT",
  214. "SENSOR",
  215. "AOTAG",
  216. "VFSENSOR",
  217. "SWREST",
  218. "SC7",
  219. "HSM",
  220. "CORESIGHT"
  221. };
  222. static const char * const tegra186_reset_levels[] = {
  223. "L0", "L1", "L2", "WARM"
  224. };
  225. static const char * const tegra30_reset_sources[] = {
  226. "POWER_ON_RESET",
  227. "WATCHDOG",
  228. "SENSOR",
  229. "SW_MAIN",
  230. "LP0"
  231. };
  232. static const char * const tegra210_reset_sources[] = {
  233. "POWER_ON_RESET",
  234. "WATCHDOG",
  235. "SENSOR",
  236. "SW_MAIN",
  237. "LP0",
  238. "AOTAG"
  239. };
  240. /**
  241. * struct tegra_pmc - NVIDIA Tegra PMC
  242. * @dev: pointer to PMC device structure
  243. * @base: pointer to I/O remapped register region
  244. * @wake: pointer to I/O remapped region for WAKE registers
  245. * @aotag: pointer to I/O remapped region for AOTAG registers
  246. * @scratch: pointer to I/O remapped region for scratch registers
  247. * @clk: pointer to pclk clock
  248. * @soc: pointer to SoC data structure
  249. * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
  250. * @debugfs: pointer to debugfs entry
  251. * @rate: currently configured rate of pclk
  252. * @suspend_mode: lowest suspend mode available
  253. * @cpu_good_time: CPU power good time (in microseconds)
  254. * @cpu_off_time: CPU power off time (in microsecends)
  255. * @core_osc_time: core power good OSC time (in microseconds)
  256. * @core_pmu_time: core power good PMU time (in microseconds)
  257. * @core_off_time: core power off time (in microseconds)
  258. * @corereq_high: core power request is active-high
  259. * @sysclkreq_high: system clock request is active-high
  260. * @combined_req: combined power request for CPU & core
  261. * @cpu_pwr_good_en: CPU power good signal is enabled
  262. * @lp0_vec_phys: physical base address of the LP0 warm boot code
  263. * @lp0_vec_size: size of the LP0 warm boot code
  264. * @powergates_available: Bitmap of available power gates
  265. * @powergates_lock: mutex for power gate register access
  266. * @pctl_dev: pin controller exposed by the PMC
  267. * @domain: IRQ domain provided by the PMC
  268. * @irq: chip implementation for the IRQ domain
  269. */
  270. struct tegra_pmc {
  271. struct device *dev;
  272. void __iomem *base;
  273. void __iomem *wake;
  274. void __iomem *aotag;
  275. void __iomem *scratch;
  276. struct clk *clk;
  277. struct dentry *debugfs;
  278. const struct tegra_pmc_soc *soc;
  279. bool tz_only;
  280. unsigned long rate;
  281. enum tegra_suspend_mode suspend_mode;
  282. u32 cpu_good_time;
  283. u32 cpu_off_time;
  284. u32 core_osc_time;
  285. u32 core_pmu_time;
  286. u32 core_off_time;
  287. bool corereq_high;
  288. bool sysclkreq_high;
  289. bool combined_req;
  290. bool cpu_pwr_good_en;
  291. u32 lp0_vec_phys;
  292. u32 lp0_vec_size;
  293. DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
  294. struct mutex powergates_lock;
  295. struct pinctrl_dev *pctl_dev;
  296. struct irq_domain *domain;
  297. struct irq_chip irq;
  298. };
  299. static struct tegra_pmc *pmc = &(struct tegra_pmc) {
  300. .base = NULL,
  301. .suspend_mode = TEGRA_SUSPEND_NONE,
  302. };
  303. static inline struct tegra_powergate *
  304. to_powergate(struct generic_pm_domain *domain)
  305. {
  306. return container_of(domain, struct tegra_powergate, genpd);
  307. }
  308. static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset)
  309. {
  310. struct arm_smccc_res res;
  311. if (pmc->tz_only) {
  312. arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0,
  313. 0, 0, 0, &res);
  314. if (res.a0) {
  315. if (pmc->dev)
  316. dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
  317. __func__, res.a0);
  318. else
  319. pr_warn("%s(): SMC failed: %lu\n", __func__,
  320. res.a0);
  321. }
  322. return res.a1;
  323. }
  324. return readl(pmc->base + offset);
  325. }
  326. static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value,
  327. unsigned long offset)
  328. {
  329. struct arm_smccc_res res;
  330. if (pmc->tz_only) {
  331. arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset,
  332. value, 0, 0, 0, 0, &res);
  333. if (res.a0) {
  334. if (pmc->dev)
  335. dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
  336. __func__, res.a0);
  337. else
  338. pr_warn("%s(): SMC failed: %lu\n", __func__,
  339. res.a0);
  340. }
  341. } else {
  342. writel(value, pmc->base + offset);
  343. }
  344. }
  345. static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset)
  346. {
  347. if (pmc->tz_only)
  348. return tegra_pmc_readl(pmc, offset);
  349. return readl(pmc->scratch + offset);
  350. }
  351. static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value,
  352. unsigned long offset)
  353. {
  354. if (pmc->tz_only)
  355. tegra_pmc_writel(pmc, value, offset);
  356. else
  357. writel(value, pmc->scratch + offset);
  358. }
  359. /*
  360. * TODO Figure out a way to call this with the struct tegra_pmc * passed in.
  361. * This currently doesn't work because readx_poll_timeout() can only operate
  362. * on functions that take a single argument.
  363. */
  364. static inline bool tegra_powergate_state(int id)
  365. {
  366. if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
  367. return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0;
  368. else
  369. return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0;
  370. }
  371. static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id)
  372. {
  373. return (pmc->soc && pmc->soc->powergates[id]);
  374. }
  375. static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id)
  376. {
  377. return test_bit(id, pmc->powergates_available);
  378. }
  379. static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
  380. {
  381. unsigned int i;
  382. if (!pmc || !pmc->soc || !name)
  383. return -EINVAL;
  384. for (i = 0; i < pmc->soc->num_powergates; i++) {
  385. if (!tegra_powergate_is_valid(pmc, i))
  386. continue;
  387. if (!strcmp(name, pmc->soc->powergates[i]))
  388. return i;
  389. }
  390. return -ENODEV;
  391. }
  392. /**
  393. * tegra_powergate_set() - set the state of a partition
  394. * @pmc: power management controller
  395. * @id: partition ID
  396. * @new_state: new state of the partition
  397. */
  398. static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
  399. bool new_state)
  400. {
  401. bool status;
  402. int err;
  403. if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
  404. return -EINVAL;
  405. mutex_lock(&pmc->powergates_lock);
  406. if (tegra_powergate_state(id) == new_state) {
  407. mutex_unlock(&pmc->powergates_lock);
  408. return 0;
  409. }
  410. tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
  411. err = readx_poll_timeout(tegra_powergate_state, id, status,
  412. status == new_state, 10, 100000);
  413. mutex_unlock(&pmc->powergates_lock);
  414. return err;
  415. }
  416. static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
  417. unsigned int id)
  418. {
  419. u32 mask;
  420. mutex_lock(&pmc->powergates_lock);
  421. /*
  422. * On Tegra124 and later, the clamps for the GPU are controlled by a
  423. * separate register (with different semantics).
  424. */
  425. if (id == TEGRA_POWERGATE_3D) {
  426. if (pmc->soc->has_gpu_clamps) {
  427. tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL);
  428. goto out;
  429. }
  430. }
  431. /*
  432. * Tegra 2 has a bug where PCIE and VDE clamping masks are
  433. * swapped relatively to the partition ids
  434. */
  435. if (id == TEGRA_POWERGATE_VDEC)
  436. mask = (1 << TEGRA_POWERGATE_PCIE);
  437. else if (id == TEGRA_POWERGATE_PCIE)
  438. mask = (1 << TEGRA_POWERGATE_VDEC);
  439. else
  440. mask = (1 << id);
  441. tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
  442. out:
  443. mutex_unlock(&pmc->powergates_lock);
  444. return 0;
  445. }
  446. static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
  447. {
  448. unsigned int i;
  449. for (i = 0; i < pg->num_clks; i++)
  450. clk_disable_unprepare(pg->clks[i]);
  451. }
  452. static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
  453. {
  454. unsigned int i;
  455. int err;
  456. for (i = 0; i < pg->num_clks; i++) {
  457. err = clk_prepare_enable(pg->clks[i]);
  458. if (err)
  459. goto out;
  460. }
  461. return 0;
  462. out:
  463. while (i--)
  464. clk_disable_unprepare(pg->clks[i]);
  465. return err;
  466. }
  467. int __weak tegra210_clk_handle_mbist_war(unsigned int id)
  468. {
  469. return 0;
  470. }
  471. static int tegra_powergate_power_up(struct tegra_powergate *pg,
  472. bool disable_clocks)
  473. {
  474. int err;
  475. err = reset_control_assert(pg->reset);
  476. if (err)
  477. return err;
  478. usleep_range(10, 20);
  479. err = tegra_powergate_set(pg->pmc, pg->id, true);
  480. if (err < 0)
  481. return err;
  482. usleep_range(10, 20);
  483. err = tegra_powergate_enable_clocks(pg);
  484. if (err)
  485. goto disable_clks;
  486. usleep_range(10, 20);
  487. err = __tegra_powergate_remove_clamping(pg->pmc, pg->id);
  488. if (err)
  489. goto disable_clks;
  490. usleep_range(10, 20);
  491. err = reset_control_deassert(pg->reset);
  492. if (err)
  493. goto powergate_off;
  494. usleep_range(10, 20);
  495. if (pg->pmc->soc->needs_mbist_war)
  496. err = tegra210_clk_handle_mbist_war(pg->id);
  497. if (err)
  498. goto disable_clks;
  499. if (disable_clocks)
  500. tegra_powergate_disable_clocks(pg);
  501. return 0;
  502. disable_clks:
  503. tegra_powergate_disable_clocks(pg);
  504. usleep_range(10, 20);
  505. powergate_off:
  506. tegra_powergate_set(pg->pmc, pg->id, false);
  507. return err;
  508. }
  509. static int tegra_powergate_power_down(struct tegra_powergate *pg)
  510. {
  511. int err;
  512. err = tegra_powergate_enable_clocks(pg);
  513. if (err)
  514. return err;
  515. usleep_range(10, 20);
  516. err = reset_control_assert(pg->reset);
  517. if (err)
  518. goto disable_clks;
  519. usleep_range(10, 20);
  520. tegra_powergate_disable_clocks(pg);
  521. usleep_range(10, 20);
  522. err = tegra_powergate_set(pg->pmc, pg->id, false);
  523. if (err)
  524. goto assert_resets;
  525. return 0;
  526. assert_resets:
  527. tegra_powergate_enable_clocks(pg);
  528. usleep_range(10, 20);
  529. reset_control_deassert(pg->reset);
  530. usleep_range(10, 20);
  531. disable_clks:
  532. tegra_powergate_disable_clocks(pg);
  533. return err;
  534. }
  535. static int tegra_genpd_power_on(struct generic_pm_domain *domain)
  536. {
  537. struct tegra_powergate *pg = to_powergate(domain);
  538. struct device *dev = pg->pmc->dev;
  539. int err;
  540. err = tegra_powergate_power_up(pg, true);
  541. if (err) {
  542. dev_err(dev, "failed to turn on PM domain %s: %d\n",
  543. pg->genpd.name, err);
  544. goto out;
  545. }
  546. reset_control_release(pg->reset);
  547. out:
  548. return err;
  549. }
  550. static int tegra_genpd_power_off(struct generic_pm_domain *domain)
  551. {
  552. struct tegra_powergate *pg = to_powergate(domain);
  553. struct device *dev = pg->pmc->dev;
  554. int err;
  555. err = reset_control_acquire(pg->reset);
  556. if (err < 0) {
  557. pr_err("failed to acquire resets: %d\n", err);
  558. return err;
  559. }
  560. err = tegra_powergate_power_down(pg);
  561. if (err) {
  562. dev_err(dev, "failed to turn off PM domain %s: %d\n",
  563. pg->genpd.name, err);
  564. reset_control_release(pg->reset);
  565. }
  566. return err;
  567. }
  568. /**
  569. * tegra_powergate_power_on() - power on partition
  570. * @id: partition ID
  571. */
  572. int tegra_powergate_power_on(unsigned int id)
  573. {
  574. if (!tegra_powergate_is_available(pmc, id))
  575. return -EINVAL;
  576. return tegra_powergate_set(pmc, id, true);
  577. }
  578. EXPORT_SYMBOL(tegra_powergate_power_on);
  579. /**
  580. * tegra_powergate_power_off() - power off partition
  581. * @id: partition ID
  582. */
  583. int tegra_powergate_power_off(unsigned int id)
  584. {
  585. if (!tegra_powergate_is_available(pmc, id))
  586. return -EINVAL;
  587. return tegra_powergate_set(pmc, id, false);
  588. }
  589. EXPORT_SYMBOL(tegra_powergate_power_off);
  590. /**
  591. * tegra_powergate_is_powered() - check if partition is powered
  592. * @pmc: power management controller
  593. * @id: partition ID
  594. */
  595. static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id)
  596. {
  597. if (!tegra_powergate_is_valid(pmc, id))
  598. return -EINVAL;
  599. return tegra_powergate_state(id);
  600. }
  601. /**
  602. * tegra_powergate_remove_clamping() - remove power clamps for partition
  603. * @id: partition ID
  604. */
  605. int tegra_powergate_remove_clamping(unsigned int id)
  606. {
  607. if (!tegra_powergate_is_available(pmc, id))
  608. return -EINVAL;
  609. return __tegra_powergate_remove_clamping(pmc, id);
  610. }
  611. EXPORT_SYMBOL(tegra_powergate_remove_clamping);
  612. /**
  613. * tegra_powergate_sequence_power_up() - power up partition
  614. * @id: partition ID
  615. * @clk: clock for partition
  616. * @rst: reset for partition
  617. *
  618. * Must be called with clk disabled, and returns with clk enabled.
  619. */
  620. int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
  621. struct reset_control *rst)
  622. {
  623. struct tegra_powergate *pg;
  624. int err;
  625. if (!tegra_powergate_is_available(pmc, id))
  626. return -EINVAL;
  627. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  628. if (!pg)
  629. return -ENOMEM;
  630. pg->id = id;
  631. pg->clks = &clk;
  632. pg->num_clks = 1;
  633. pg->reset = rst;
  634. pg->pmc = pmc;
  635. err = tegra_powergate_power_up(pg, false);
  636. if (err)
  637. dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id,
  638. err);
  639. kfree(pg);
  640. return err;
  641. }
  642. EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
  643. /**
  644. * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
  645. * @pmc: power management controller
  646. * @cpuid: CPU partition ID
  647. *
  648. * Returns the partition ID corresponding to the CPU partition ID or a
  649. * negative error code on failure.
  650. */
  651. static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc,
  652. unsigned int cpuid)
  653. {
  654. if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
  655. return pmc->soc->cpu_powergates[cpuid];
  656. return -EINVAL;
  657. }
  658. /**
  659. * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
  660. * @cpuid: CPU partition ID
  661. */
  662. bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
  663. {
  664. int id;
  665. id = tegra_get_cpu_powergate_id(pmc, cpuid);
  666. if (id < 0)
  667. return false;
  668. return tegra_powergate_is_powered(pmc, id);
  669. }
  670. /**
  671. * tegra_pmc_cpu_power_on() - power on CPU partition
  672. * @cpuid: CPU partition ID
  673. */
  674. int tegra_pmc_cpu_power_on(unsigned int cpuid)
  675. {
  676. int id;
  677. id = tegra_get_cpu_powergate_id(pmc, cpuid);
  678. if (id < 0)
  679. return id;
  680. return tegra_powergate_set(pmc, id, true);
  681. }
  682. /**
  683. * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
  684. * @cpuid: CPU partition ID
  685. */
  686. int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
  687. {
  688. int id;
  689. id = tegra_get_cpu_powergate_id(pmc, cpuid);
  690. if (id < 0)
  691. return id;
  692. return tegra_powergate_remove_clamping(id);
  693. }
  694. static int tegra_pmc_restart_notify(struct notifier_block *this,
  695. unsigned long action, void *data)
  696. {
  697. const char *cmd = data;
  698. u32 value;
  699. value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0);
  700. value &= ~PMC_SCRATCH0_MODE_MASK;
  701. if (cmd) {
  702. if (strcmp(cmd, "recovery") == 0)
  703. value |= PMC_SCRATCH0_MODE_RECOVERY;
  704. if (strcmp(cmd, "bootloader") == 0)
  705. value |= PMC_SCRATCH0_MODE_BOOTLOADER;
  706. if (strcmp(cmd, "forced-recovery") == 0)
  707. value |= PMC_SCRATCH0_MODE_RCM;
  708. }
  709. tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0);
  710. /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
  711. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  712. value |= PMC_CNTRL_MAIN_RST;
  713. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  714. return NOTIFY_DONE;
  715. }
  716. static struct notifier_block tegra_pmc_restart_handler = {
  717. .notifier_call = tegra_pmc_restart_notify,
  718. .priority = 128,
  719. };
  720. static int powergate_show(struct seq_file *s, void *data)
  721. {
  722. unsigned int i;
  723. int status;
  724. seq_printf(s, " powergate powered\n");
  725. seq_printf(s, "------------------\n");
  726. for (i = 0; i < pmc->soc->num_powergates; i++) {
  727. status = tegra_powergate_is_powered(pmc, i);
  728. if (status < 0)
  729. continue;
  730. seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
  731. status ? "yes" : "no");
  732. }
  733. return 0;
  734. }
  735. DEFINE_SHOW_ATTRIBUTE(powergate);
  736. static int tegra_powergate_debugfs_init(void)
  737. {
  738. pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
  739. &powergate_fops);
  740. if (!pmc->debugfs)
  741. return -ENOMEM;
  742. return 0;
  743. }
  744. static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
  745. struct device_node *np)
  746. {
  747. struct clk *clk;
  748. unsigned int i, count;
  749. int err;
  750. count = of_clk_get_parent_count(np);
  751. if (count == 0)
  752. return -ENODEV;
  753. pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
  754. if (!pg->clks)
  755. return -ENOMEM;
  756. for (i = 0; i < count; i++) {
  757. pg->clks[i] = of_clk_get(np, i);
  758. if (IS_ERR(pg->clks[i])) {
  759. err = PTR_ERR(pg->clks[i]);
  760. goto err;
  761. }
  762. }
  763. pg->num_clks = count;
  764. return 0;
  765. err:
  766. while (i--)
  767. clk_put(pg->clks[i]);
  768. kfree(pg->clks);
  769. return err;
  770. }
  771. static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
  772. struct device_node *np, bool off)
  773. {
  774. struct device *dev = pg->pmc->dev;
  775. int err;
  776. pg->reset = of_reset_control_array_get_exclusive_released(np);
  777. if (IS_ERR(pg->reset)) {
  778. err = PTR_ERR(pg->reset);
  779. dev_err(dev, "failed to get device resets: %d\n", err);
  780. return err;
  781. }
  782. err = reset_control_acquire(pg->reset);
  783. if (err < 0) {
  784. pr_err("failed to acquire resets: %d\n", err);
  785. goto out;
  786. }
  787. if (off) {
  788. err = reset_control_assert(pg->reset);
  789. } else {
  790. err = reset_control_deassert(pg->reset);
  791. if (err < 0)
  792. goto out;
  793. reset_control_release(pg->reset);
  794. }
  795. out:
  796. if (err) {
  797. reset_control_release(pg->reset);
  798. reset_control_put(pg->reset);
  799. }
  800. return err;
  801. }
  802. static int tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
  803. {
  804. struct device *dev = pmc->dev;
  805. struct tegra_powergate *pg;
  806. int id, err = 0;
  807. bool off;
  808. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  809. if (!pg)
  810. return -ENOMEM;
  811. id = tegra_powergate_lookup(pmc, np->name);
  812. if (id < 0) {
  813. dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id);
  814. err = -ENODEV;
  815. goto free_mem;
  816. }
  817. /*
  818. * Clear the bit for this powergate so it cannot be managed
  819. * directly via the legacy APIs for controlling powergates.
  820. */
  821. clear_bit(id, pmc->powergates_available);
  822. pg->id = id;
  823. pg->genpd.name = np->name;
  824. pg->genpd.power_off = tegra_genpd_power_off;
  825. pg->genpd.power_on = tegra_genpd_power_on;
  826. pg->pmc = pmc;
  827. off = !tegra_powergate_is_powered(pmc, pg->id);
  828. err = tegra_powergate_of_get_clks(pg, np);
  829. if (err < 0) {
  830. dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err);
  831. goto set_available;
  832. }
  833. err = tegra_powergate_of_get_resets(pg, np, off);
  834. if (err < 0) {
  835. dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err);
  836. goto remove_clks;
  837. }
  838. if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  839. if (off)
  840. WARN_ON(tegra_powergate_power_up(pg, true));
  841. goto remove_resets;
  842. }
  843. err = pm_genpd_init(&pg->genpd, NULL, off);
  844. if (err < 0) {
  845. dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np,
  846. err);
  847. goto remove_resets;
  848. }
  849. err = of_genpd_add_provider_simple(np, &pg->genpd);
  850. if (err < 0) {
  851. dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n",
  852. np, err);
  853. goto remove_genpd;
  854. }
  855. dev_dbg(dev, "added PM domain %s\n", pg->genpd.name);
  856. return 0;
  857. remove_genpd:
  858. pm_genpd_remove(&pg->genpd);
  859. remove_resets:
  860. reset_control_put(pg->reset);
  861. remove_clks:
  862. while (pg->num_clks--)
  863. clk_put(pg->clks[pg->num_clks]);
  864. kfree(pg->clks);
  865. set_available:
  866. set_bit(id, pmc->powergates_available);
  867. free_mem:
  868. kfree(pg);
  869. return err;
  870. }
  871. static int tegra_powergate_init(struct tegra_pmc *pmc,
  872. struct device_node *parent)
  873. {
  874. struct device_node *np, *child;
  875. int err = 0;
  876. np = of_get_child_by_name(parent, "powergates");
  877. if (!np)
  878. return 0;
  879. for_each_child_of_node(np, child) {
  880. err = tegra_powergate_add(pmc, child);
  881. if (err < 0) {
  882. of_node_put(child);
  883. break;
  884. }
  885. }
  886. of_node_put(np);
  887. return err;
  888. }
  889. static void tegra_powergate_remove(struct generic_pm_domain *genpd)
  890. {
  891. struct tegra_powergate *pg = to_powergate(genpd);
  892. reset_control_put(pg->reset);
  893. while (pg->num_clks--)
  894. clk_put(pg->clks[pg->num_clks]);
  895. kfree(pg->clks);
  896. set_bit(pg->id, pmc->powergates_available);
  897. kfree(pg);
  898. }
  899. static void tegra_powergate_remove_all(struct device_node *parent)
  900. {
  901. struct generic_pm_domain *genpd;
  902. struct device_node *np, *child;
  903. np = of_get_child_by_name(parent, "powergates");
  904. if (!np)
  905. return;
  906. for_each_child_of_node(np, child) {
  907. of_genpd_del_provider(child);
  908. genpd = of_genpd_remove_last(child);
  909. if (IS_ERR(genpd))
  910. continue;
  911. tegra_powergate_remove(genpd);
  912. }
  913. of_node_put(np);
  914. }
  915. static const struct tegra_io_pad_soc *
  916. tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
  917. {
  918. unsigned int i;
  919. for (i = 0; i < pmc->soc->num_io_pads; i++)
  920. if (pmc->soc->io_pads[i].id == id)
  921. return &pmc->soc->io_pads[i];
  922. return NULL;
  923. }
  924. static int tegra_io_pad_get_dpd_register_bit(struct tegra_pmc *pmc,
  925. enum tegra_io_pad id,
  926. unsigned long *request,
  927. unsigned long *status,
  928. u32 *mask)
  929. {
  930. const struct tegra_io_pad_soc *pad;
  931. pad = tegra_io_pad_find(pmc, id);
  932. if (!pad) {
  933. dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
  934. return -ENOENT;
  935. }
  936. if (pad->dpd == UINT_MAX)
  937. return -ENOTSUPP;
  938. *mask = BIT(pad->dpd % 32);
  939. if (pad->dpd < 32) {
  940. *status = pmc->soc->regs->dpd_status;
  941. *request = pmc->soc->regs->dpd_req;
  942. } else {
  943. *status = pmc->soc->regs->dpd2_status;
  944. *request = pmc->soc->regs->dpd2_req;
  945. }
  946. return 0;
  947. }
  948. static int tegra_io_pad_prepare(struct tegra_pmc *pmc, enum tegra_io_pad id,
  949. unsigned long *request, unsigned long *status,
  950. u32 *mask)
  951. {
  952. unsigned long rate, value;
  953. int err;
  954. err = tegra_io_pad_get_dpd_register_bit(pmc, id, request, status, mask);
  955. if (err)
  956. return err;
  957. if (pmc->clk) {
  958. rate = clk_get_rate(pmc->clk);
  959. if (!rate) {
  960. dev_err(pmc->dev, "failed to get clock rate\n");
  961. return -ENODEV;
  962. }
  963. tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE);
  964. /* must be at least 200 ns, in APB (PCLK) clock cycles */
  965. value = DIV_ROUND_UP(1000000000, rate);
  966. value = DIV_ROUND_UP(200, value);
  967. tegra_pmc_writel(pmc, value, SEL_DPD_TIM);
  968. }
  969. return 0;
  970. }
  971. static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset,
  972. u32 mask, u32 val, unsigned long timeout)
  973. {
  974. u32 value;
  975. timeout = jiffies + msecs_to_jiffies(timeout);
  976. while (time_after(timeout, jiffies)) {
  977. value = tegra_pmc_readl(pmc, offset);
  978. if ((value & mask) == val)
  979. return 0;
  980. usleep_range(250, 1000);
  981. }
  982. return -ETIMEDOUT;
  983. }
  984. static void tegra_io_pad_unprepare(struct tegra_pmc *pmc)
  985. {
  986. if (pmc->clk)
  987. tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE);
  988. }
  989. /**
  990. * tegra_io_pad_power_enable() - enable power to I/O pad
  991. * @id: Tegra I/O pad ID for which to enable power
  992. *
  993. * Returns: 0 on success or a negative error code on failure.
  994. */
  995. int tegra_io_pad_power_enable(enum tegra_io_pad id)
  996. {
  997. unsigned long request, status;
  998. u32 mask;
  999. int err;
  1000. mutex_lock(&pmc->powergates_lock);
  1001. err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
  1002. if (err < 0) {
  1003. dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
  1004. goto unlock;
  1005. }
  1006. tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request);
  1007. err = tegra_io_pad_poll(pmc, status, mask, 0, 250);
  1008. if (err < 0) {
  1009. dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err);
  1010. goto unlock;
  1011. }
  1012. tegra_io_pad_unprepare(pmc);
  1013. unlock:
  1014. mutex_unlock(&pmc->powergates_lock);
  1015. return err;
  1016. }
  1017. EXPORT_SYMBOL(tegra_io_pad_power_enable);
  1018. /**
  1019. * tegra_io_pad_power_disable() - disable power to I/O pad
  1020. * @id: Tegra I/O pad ID for which to disable power
  1021. *
  1022. * Returns: 0 on success or a negative error code on failure.
  1023. */
  1024. int tegra_io_pad_power_disable(enum tegra_io_pad id)
  1025. {
  1026. unsigned long request, status;
  1027. u32 mask;
  1028. int err;
  1029. mutex_lock(&pmc->powergates_lock);
  1030. err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
  1031. if (err < 0) {
  1032. dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
  1033. goto unlock;
  1034. }
  1035. tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request);
  1036. err = tegra_io_pad_poll(pmc, status, mask, mask, 250);
  1037. if (err < 0) {
  1038. dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err);
  1039. goto unlock;
  1040. }
  1041. tegra_io_pad_unprepare(pmc);
  1042. unlock:
  1043. mutex_unlock(&pmc->powergates_lock);
  1044. return err;
  1045. }
  1046. EXPORT_SYMBOL(tegra_io_pad_power_disable);
  1047. static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id)
  1048. {
  1049. unsigned long request, status;
  1050. u32 mask, value;
  1051. int err;
  1052. err = tegra_io_pad_get_dpd_register_bit(pmc, id, &request, &status,
  1053. &mask);
  1054. if (err)
  1055. return err;
  1056. value = tegra_pmc_readl(pmc, status);
  1057. return !(value & mask);
  1058. }
  1059. static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id,
  1060. int voltage)
  1061. {
  1062. const struct tegra_io_pad_soc *pad;
  1063. u32 value;
  1064. pad = tegra_io_pad_find(pmc, id);
  1065. if (!pad)
  1066. return -ENOENT;
  1067. if (pad->voltage == UINT_MAX)
  1068. return -ENOTSUPP;
  1069. mutex_lock(&pmc->powergates_lock);
  1070. if (pmc->soc->has_impl_33v_pwr) {
  1071. value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
  1072. if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
  1073. value &= ~BIT(pad->voltage);
  1074. else
  1075. value |= BIT(pad->voltage);
  1076. tegra_pmc_writel(pmc, value, PMC_IMPL_E_33V_PWR);
  1077. } else {
  1078. /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
  1079. value = tegra_pmc_readl(pmc, PMC_PWR_DET);
  1080. value |= BIT(pad->voltage);
  1081. tegra_pmc_writel(pmc, value, PMC_PWR_DET);
  1082. /* update I/O voltage */
  1083. value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
  1084. if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
  1085. value &= ~BIT(pad->voltage);
  1086. else
  1087. value |= BIT(pad->voltage);
  1088. tegra_pmc_writel(pmc, value, PMC_PWR_DET_VALUE);
  1089. }
  1090. mutex_unlock(&pmc->powergates_lock);
  1091. usleep_range(100, 250);
  1092. return 0;
  1093. }
  1094. static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id)
  1095. {
  1096. const struct tegra_io_pad_soc *pad;
  1097. u32 value;
  1098. pad = tegra_io_pad_find(pmc, id);
  1099. if (!pad)
  1100. return -ENOENT;
  1101. if (pad->voltage == UINT_MAX)
  1102. return -ENOTSUPP;
  1103. if (pmc->soc->has_impl_33v_pwr)
  1104. value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
  1105. else
  1106. value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
  1107. if ((value & BIT(pad->voltage)) == 0)
  1108. return TEGRA_IO_PAD_VOLTAGE_1V8;
  1109. return TEGRA_IO_PAD_VOLTAGE_3V3;
  1110. }
  1111. /**
  1112. * tegra_io_rail_power_on() - enable power to I/O rail
  1113. * @id: Tegra I/O pad ID for which to enable power
  1114. *
  1115. * See also: tegra_io_pad_power_enable()
  1116. */
  1117. int tegra_io_rail_power_on(unsigned int id)
  1118. {
  1119. return tegra_io_pad_power_enable(id);
  1120. }
  1121. EXPORT_SYMBOL(tegra_io_rail_power_on);
  1122. /**
  1123. * tegra_io_rail_power_off() - disable power to I/O rail
  1124. * @id: Tegra I/O pad ID for which to disable power
  1125. *
  1126. * See also: tegra_io_pad_power_disable()
  1127. */
  1128. int tegra_io_rail_power_off(unsigned int id)
  1129. {
  1130. return tegra_io_pad_power_disable(id);
  1131. }
  1132. EXPORT_SYMBOL(tegra_io_rail_power_off);
  1133. #ifdef CONFIG_PM_SLEEP
  1134. enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
  1135. {
  1136. return pmc->suspend_mode;
  1137. }
  1138. void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
  1139. {
  1140. if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
  1141. return;
  1142. pmc->suspend_mode = mode;
  1143. }
  1144. void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  1145. {
  1146. unsigned long long rate = 0;
  1147. u32 value;
  1148. switch (mode) {
  1149. case TEGRA_SUSPEND_LP1:
  1150. rate = 32768;
  1151. break;
  1152. case TEGRA_SUSPEND_LP2:
  1153. rate = clk_get_rate(pmc->clk);
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. if (WARN_ON_ONCE(rate == 0))
  1159. rate = 100000000;
  1160. if (rate != pmc->rate) {
  1161. u64 ticks;
  1162. ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
  1163. do_div(ticks, USEC_PER_SEC);
  1164. tegra_pmc_writel(pmc, ticks, PMC_CPUPWRGOOD_TIMER);
  1165. ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
  1166. do_div(ticks, USEC_PER_SEC);
  1167. tegra_pmc_writel(pmc, ticks, PMC_CPUPWROFF_TIMER);
  1168. wmb();
  1169. pmc->rate = rate;
  1170. }
  1171. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  1172. value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
  1173. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  1174. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  1175. }
  1176. #endif
  1177. static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
  1178. {
  1179. u32 value, values[2];
  1180. if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
  1181. } else {
  1182. switch (value) {
  1183. case 0:
  1184. pmc->suspend_mode = TEGRA_SUSPEND_LP0;
  1185. break;
  1186. case 1:
  1187. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  1188. break;
  1189. case 2:
  1190. pmc->suspend_mode = TEGRA_SUSPEND_LP2;
  1191. break;
  1192. default:
  1193. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  1194. break;
  1195. }
  1196. }
  1197. pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
  1198. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
  1199. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  1200. pmc->cpu_good_time = value;
  1201. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
  1202. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  1203. pmc->cpu_off_time = value;
  1204. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  1205. values, ARRAY_SIZE(values)))
  1206. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  1207. pmc->core_osc_time = values[0];
  1208. pmc->core_pmu_time = values[1];
  1209. if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
  1210. pmc->suspend_mode = TEGRA_SUSPEND_NONE;
  1211. pmc->core_off_time = value;
  1212. pmc->corereq_high = of_property_read_bool(np,
  1213. "nvidia,core-power-req-active-high");
  1214. pmc->sysclkreq_high = of_property_read_bool(np,
  1215. "nvidia,sys-clock-req-active-high");
  1216. pmc->combined_req = of_property_read_bool(np,
  1217. "nvidia,combined-power-req");
  1218. pmc->cpu_pwr_good_en = of_property_read_bool(np,
  1219. "nvidia,cpu-pwr-good-en");
  1220. if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
  1221. ARRAY_SIZE(values)))
  1222. if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
  1223. pmc->suspend_mode = TEGRA_SUSPEND_LP1;
  1224. pmc->lp0_vec_phys = values[0];
  1225. pmc->lp0_vec_size = values[1];
  1226. return 0;
  1227. }
  1228. static void tegra_pmc_init(struct tegra_pmc *pmc)
  1229. {
  1230. if (pmc->soc->init)
  1231. pmc->soc->init(pmc);
  1232. }
  1233. static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
  1234. {
  1235. static const char disabled[] = "emergency thermal reset disabled";
  1236. u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
  1237. struct device *dev = pmc->dev;
  1238. struct device_node *np;
  1239. u32 value, checksum;
  1240. if (!pmc->soc->has_tsense_reset)
  1241. return;
  1242. np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
  1243. if (!np) {
  1244. dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
  1245. return;
  1246. }
  1247. if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
  1248. dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
  1249. goto out;
  1250. }
  1251. if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
  1252. dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
  1253. goto out;
  1254. }
  1255. if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
  1256. dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
  1257. goto out;
  1258. }
  1259. if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
  1260. dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
  1261. goto out;
  1262. }
  1263. if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
  1264. pinmux = 0;
  1265. value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
  1266. value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
  1267. tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
  1268. value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
  1269. (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
  1270. tegra_pmc_writel(pmc, value, PMC_SCRATCH54);
  1271. value = PMC_SCRATCH55_RESET_TEGRA;
  1272. value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
  1273. value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
  1274. value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
  1275. /*
  1276. * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
  1277. * contain the checksum and are currently zero, so they are not added.
  1278. */
  1279. checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
  1280. + ((value >> 24) & 0xff);
  1281. checksum &= 0xff;
  1282. checksum = 0x100 - checksum;
  1283. value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
  1284. tegra_pmc_writel(pmc, value, PMC_SCRATCH55);
  1285. value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
  1286. value |= PMC_SENSOR_CTRL_ENABLE_RST;
  1287. tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
  1288. dev_info(pmc->dev, "emergency thermal reset enabled\n");
  1289. out:
  1290. of_node_put(np);
  1291. }
  1292. static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
  1293. {
  1294. struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
  1295. return pmc->soc->num_io_pads;
  1296. }
  1297. static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl,
  1298. unsigned int group)
  1299. {
  1300. struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl);
  1301. return pmc->soc->io_pads[group].name;
  1302. }
  1303. static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
  1304. unsigned int group,
  1305. const unsigned int **pins,
  1306. unsigned int *num_pins)
  1307. {
  1308. struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
  1309. *pins = &pmc->soc->io_pads[group].id;
  1310. *num_pins = 1;
  1311. return 0;
  1312. }
  1313. static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
  1314. .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
  1315. .get_group_name = tegra_io_pad_pinctrl_get_group_name,
  1316. .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
  1317. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  1318. .dt_free_map = pinconf_generic_dt_free_map,
  1319. };
  1320. static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
  1321. unsigned int pin, unsigned long *config)
  1322. {
  1323. enum pin_config_param param = pinconf_to_config_param(*config);
  1324. struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
  1325. const struct tegra_io_pad_soc *pad;
  1326. int ret;
  1327. u32 arg;
  1328. pad = tegra_io_pad_find(pmc, pin);
  1329. if (!pad)
  1330. return -EINVAL;
  1331. switch (param) {
  1332. case PIN_CONFIG_POWER_SOURCE:
  1333. ret = tegra_io_pad_get_voltage(pmc, pad->id);
  1334. if (ret < 0)
  1335. return ret;
  1336. arg = ret;
  1337. break;
  1338. case PIN_CONFIG_LOW_POWER_MODE:
  1339. ret = tegra_io_pad_is_powered(pmc, pad->id);
  1340. if (ret < 0)
  1341. return ret;
  1342. arg = !ret;
  1343. break;
  1344. default:
  1345. return -EINVAL;
  1346. }
  1347. *config = pinconf_to_config_packed(param, arg);
  1348. return 0;
  1349. }
  1350. static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
  1351. unsigned int pin, unsigned long *configs,
  1352. unsigned int num_configs)
  1353. {
  1354. struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
  1355. const struct tegra_io_pad_soc *pad;
  1356. enum pin_config_param param;
  1357. unsigned int i;
  1358. int err;
  1359. u32 arg;
  1360. pad = tegra_io_pad_find(pmc, pin);
  1361. if (!pad)
  1362. return -EINVAL;
  1363. for (i = 0; i < num_configs; ++i) {
  1364. param = pinconf_to_config_param(configs[i]);
  1365. arg = pinconf_to_config_argument(configs[i]);
  1366. switch (param) {
  1367. case PIN_CONFIG_LOW_POWER_MODE:
  1368. if (arg)
  1369. err = tegra_io_pad_power_disable(pad->id);
  1370. else
  1371. err = tegra_io_pad_power_enable(pad->id);
  1372. if (err)
  1373. return err;
  1374. break;
  1375. case PIN_CONFIG_POWER_SOURCE:
  1376. if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
  1377. arg != TEGRA_IO_PAD_VOLTAGE_3V3)
  1378. return -EINVAL;
  1379. err = tegra_io_pad_set_voltage(pmc, pad->id, arg);
  1380. if (err)
  1381. return err;
  1382. break;
  1383. default:
  1384. return -EINVAL;
  1385. }
  1386. }
  1387. return 0;
  1388. }
  1389. static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
  1390. .pin_config_get = tegra_io_pad_pinconf_get,
  1391. .pin_config_set = tegra_io_pad_pinconf_set,
  1392. .is_generic = true,
  1393. };
  1394. static struct pinctrl_desc tegra_pmc_pctl_desc = {
  1395. .pctlops = &tegra_io_pad_pinctrl_ops,
  1396. .confops = &tegra_io_pad_pinconf_ops,
  1397. };
  1398. static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
  1399. {
  1400. int err;
  1401. if (!pmc->soc->num_pin_descs)
  1402. return 0;
  1403. tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
  1404. tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
  1405. tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
  1406. pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
  1407. pmc);
  1408. if (IS_ERR(pmc->pctl_dev)) {
  1409. err = PTR_ERR(pmc->pctl_dev);
  1410. dev_err(pmc->dev, "failed to register pin controller: %d\n",
  1411. err);
  1412. return err;
  1413. }
  1414. return 0;
  1415. }
  1416. static ssize_t reset_reason_show(struct device *dev,
  1417. struct device_attribute *attr, char *buf)
  1418. {
  1419. u32 value;
  1420. value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
  1421. value &= pmc->soc->regs->rst_source_mask;
  1422. value >>= pmc->soc->regs->rst_source_shift;
  1423. if (WARN_ON(value >= pmc->soc->num_reset_sources))
  1424. return sprintf(buf, "%s\n", "UNKNOWN");
  1425. return sprintf(buf, "%s\n", pmc->soc->reset_sources[value]);
  1426. }
  1427. static DEVICE_ATTR_RO(reset_reason);
  1428. static ssize_t reset_level_show(struct device *dev,
  1429. struct device_attribute *attr, char *buf)
  1430. {
  1431. u32 value;
  1432. value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
  1433. value &= pmc->soc->regs->rst_level_mask;
  1434. value >>= pmc->soc->regs->rst_level_shift;
  1435. if (WARN_ON(value >= pmc->soc->num_reset_levels))
  1436. return sprintf(buf, "%s\n", "UNKNOWN");
  1437. return sprintf(buf, "%s\n", pmc->soc->reset_levels[value]);
  1438. }
  1439. static DEVICE_ATTR_RO(reset_level);
  1440. static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
  1441. {
  1442. struct device *dev = pmc->dev;
  1443. int err = 0;
  1444. if (pmc->soc->reset_sources) {
  1445. err = device_create_file(dev, &dev_attr_reset_reason);
  1446. if (err < 0)
  1447. dev_warn(dev,
  1448. "failed to create attr \"reset_reason\": %d\n",
  1449. err);
  1450. }
  1451. if (pmc->soc->reset_levels) {
  1452. err = device_create_file(dev, &dev_attr_reset_level);
  1453. if (err < 0)
  1454. dev_warn(dev,
  1455. "failed to create attr \"reset_level\": %d\n",
  1456. err);
  1457. }
  1458. }
  1459. static int tegra_pmc_irq_translate(struct irq_domain *domain,
  1460. struct irq_fwspec *fwspec,
  1461. unsigned long *hwirq,
  1462. unsigned int *type)
  1463. {
  1464. if (WARN_ON(fwspec->param_count < 2))
  1465. return -EINVAL;
  1466. *hwirq = fwspec->param[0];
  1467. *type = fwspec->param[1];
  1468. return 0;
  1469. }
  1470. static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
  1471. unsigned int num_irqs, void *data)
  1472. {
  1473. struct tegra_pmc *pmc = domain->host_data;
  1474. const struct tegra_pmc_soc *soc = pmc->soc;
  1475. struct irq_fwspec *fwspec = data;
  1476. unsigned int i;
  1477. int err = 0;
  1478. if (WARN_ON(num_irqs > 1))
  1479. return -EINVAL;
  1480. for (i = 0; i < soc->num_wake_events; i++) {
  1481. const struct tegra_wake_event *event = &soc->wake_events[i];
  1482. if (fwspec->param_count == 2) {
  1483. struct irq_fwspec spec;
  1484. if (event->id != fwspec->param[0])
  1485. continue;
  1486. err = irq_domain_set_hwirq_and_chip(domain, virq,
  1487. event->id,
  1488. &pmc->irq, pmc);
  1489. if (err < 0)
  1490. break;
  1491. spec.fwnode = &pmc->dev->of_node->fwnode;
  1492. spec.param_count = 3;
  1493. spec.param[0] = GIC_SPI;
  1494. spec.param[1] = event->irq;
  1495. spec.param[2] = fwspec->param[1];
  1496. err = irq_domain_alloc_irqs_parent(domain, virq,
  1497. num_irqs, &spec);
  1498. break;
  1499. }
  1500. if (fwspec->param_count == 3) {
  1501. if (event->gpio.instance != fwspec->param[0] ||
  1502. event->gpio.pin != fwspec->param[1])
  1503. continue;
  1504. err = irq_domain_set_hwirq_and_chip(domain, virq,
  1505. event->id,
  1506. &pmc->irq, pmc);
  1507. /*
  1508. * GPIOs don't have an equivalent interrupt in the
  1509. * parent controller (GIC). However some code, such
  1510. * as the one in irq_get_irqchip_state(), require a
  1511. * valid IRQ chip to be set. Make sure that's the
  1512. * case by passing NULL here, which will install a
  1513. * dummy IRQ chip for the interrupt in the parent
  1514. * domain.
  1515. */
  1516. if (domain->parent)
  1517. irq_domain_set_hwirq_and_chip(domain->parent,
  1518. virq, 0, NULL,
  1519. NULL);
  1520. break;
  1521. }
  1522. }
  1523. /*
  1524. * For interrupts that don't have associated wake events, assign a
  1525. * dummy hardware IRQ number. This is used in the ->irq_set_type()
  1526. * and ->irq_set_wake() callbacks to return early for these IRQs.
  1527. */
  1528. if (i == soc->num_wake_events) {
  1529. err = irq_domain_set_hwirq_and_chip(domain, virq, ULONG_MAX,
  1530. &pmc->irq, pmc);
  1531. /*
  1532. * Interrupts without a wake event don't have a corresponding
  1533. * interrupt in the parent controller (GIC). Pass NULL for the
  1534. * chip here, which causes a dummy IRQ chip to be installed
  1535. * for the interrupt in the parent domain, to make this
  1536. * explicit.
  1537. */
  1538. if (domain->parent)
  1539. irq_domain_set_hwirq_and_chip(domain->parent, virq, 0,
  1540. NULL, NULL);
  1541. }
  1542. return err;
  1543. }
  1544. static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
  1545. .translate = tegra_pmc_irq_translate,
  1546. .alloc = tegra_pmc_irq_alloc,
  1547. };
  1548. static int tegra_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
  1549. {
  1550. struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
  1551. unsigned int offset, bit;
  1552. u32 value;
  1553. /* nothing to do if there's no associated wake event */
  1554. if (WARN_ON(data->hwirq == ULONG_MAX))
  1555. return 0;
  1556. offset = data->hwirq / 32;
  1557. bit = data->hwirq % 32;
  1558. /* clear wake status */
  1559. writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq));
  1560. /* route wake to tier 2 */
  1561. value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
  1562. if (!on)
  1563. value &= ~(1 << bit);
  1564. else
  1565. value |= 1 << bit;
  1566. writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
  1567. /* enable wakeup event */
  1568. writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq));
  1569. return 0;
  1570. }
  1571. static int tegra_pmc_irq_set_type(struct irq_data *data, unsigned int type)
  1572. {
  1573. struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
  1574. u32 value;
  1575. /* nothing to do if there's no associated wake event */
  1576. if (data->hwirq == ULONG_MAX)
  1577. return 0;
  1578. value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
  1579. switch (type) {
  1580. case IRQ_TYPE_EDGE_RISING:
  1581. case IRQ_TYPE_LEVEL_HIGH:
  1582. value |= WAKE_AOWAKE_CNTRL_LEVEL;
  1583. break;
  1584. case IRQ_TYPE_EDGE_FALLING:
  1585. case IRQ_TYPE_LEVEL_LOW:
  1586. value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
  1587. break;
  1588. case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
  1589. value ^= WAKE_AOWAKE_CNTRL_LEVEL;
  1590. break;
  1591. default:
  1592. return -EINVAL;
  1593. }
  1594. writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
  1595. return 0;
  1596. }
  1597. static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
  1598. {
  1599. struct irq_domain *parent = NULL;
  1600. struct device_node *np;
  1601. np = of_irq_find_parent(pmc->dev->of_node);
  1602. if (np) {
  1603. parent = irq_find_host(np);
  1604. of_node_put(np);
  1605. }
  1606. if (!parent)
  1607. return 0;
  1608. pmc->irq.name = dev_name(pmc->dev);
  1609. pmc->irq.irq_mask = irq_chip_mask_parent;
  1610. pmc->irq.irq_unmask = irq_chip_unmask_parent;
  1611. pmc->irq.irq_eoi = irq_chip_eoi_parent;
  1612. pmc->irq.irq_set_affinity = irq_chip_set_affinity_parent;
  1613. pmc->irq.irq_set_type = tegra_pmc_irq_set_type;
  1614. pmc->irq.irq_set_wake = tegra_pmc_irq_set_wake;
  1615. pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node,
  1616. &tegra_pmc_irq_domain_ops, pmc);
  1617. if (!pmc->domain) {
  1618. dev_err(pmc->dev, "failed to allocate domain\n");
  1619. return -ENOMEM;
  1620. }
  1621. return 0;
  1622. }
  1623. static int tegra_pmc_probe(struct platform_device *pdev)
  1624. {
  1625. void __iomem *base;
  1626. struct resource *res;
  1627. int err;
  1628. /*
  1629. * Early initialisation should have configured an initial
  1630. * register mapping and setup the soc data pointer. If these
  1631. * are not valid then something went badly wrong!
  1632. */
  1633. if (WARN_ON(!pmc->base || !pmc->soc))
  1634. return -ENODEV;
  1635. err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
  1636. if (err < 0)
  1637. return err;
  1638. /* take over the memory region from the early initialization */
  1639. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1640. base = devm_ioremap_resource(&pdev->dev, res);
  1641. if (IS_ERR(base))
  1642. return PTR_ERR(base);
  1643. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
  1644. if (res) {
  1645. pmc->wake = devm_ioremap_resource(&pdev->dev, res);
  1646. if (IS_ERR(pmc->wake))
  1647. return PTR_ERR(pmc->wake);
  1648. } else {
  1649. pmc->wake = base;
  1650. }
  1651. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
  1652. if (res) {
  1653. pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
  1654. if (IS_ERR(pmc->aotag))
  1655. return PTR_ERR(pmc->aotag);
  1656. } else {
  1657. pmc->aotag = base;
  1658. }
  1659. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
  1660. if (res) {
  1661. pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
  1662. if (IS_ERR(pmc->scratch))
  1663. return PTR_ERR(pmc->scratch);
  1664. } else {
  1665. pmc->scratch = base;
  1666. }
  1667. pmc->clk = devm_clk_get(&pdev->dev, "pclk");
  1668. if (IS_ERR(pmc->clk)) {
  1669. err = PTR_ERR(pmc->clk);
  1670. if (err != -ENOENT) {
  1671. dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
  1672. return err;
  1673. }
  1674. pmc->clk = NULL;
  1675. }
  1676. pmc->dev = &pdev->dev;
  1677. tegra_pmc_init(pmc);
  1678. tegra_pmc_init_tsense_reset(pmc);
  1679. tegra_pmc_reset_sysfs_init(pmc);
  1680. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1681. err = tegra_powergate_debugfs_init();
  1682. if (err < 0)
  1683. goto cleanup_sysfs;
  1684. }
  1685. err = register_restart_handler(&tegra_pmc_restart_handler);
  1686. if (err) {
  1687. dev_err(&pdev->dev, "unable to register restart handler, %d\n",
  1688. err);
  1689. goto cleanup_debugfs;
  1690. }
  1691. err = tegra_pmc_pinctrl_init(pmc);
  1692. if (err)
  1693. goto cleanup_restart_handler;
  1694. err = tegra_powergate_init(pmc, pdev->dev.of_node);
  1695. if (err < 0)
  1696. goto cleanup_powergates;
  1697. err = tegra_pmc_irq_init(pmc);
  1698. if (err < 0)
  1699. goto cleanup_powergates;
  1700. mutex_lock(&pmc->powergates_lock);
  1701. iounmap(pmc->base);
  1702. pmc->base = base;
  1703. mutex_unlock(&pmc->powergates_lock);
  1704. platform_set_drvdata(pdev, pmc);
  1705. return 0;
  1706. cleanup_powergates:
  1707. tegra_powergate_remove_all(pdev->dev.of_node);
  1708. cleanup_restart_handler:
  1709. unregister_restart_handler(&tegra_pmc_restart_handler);
  1710. cleanup_debugfs:
  1711. debugfs_remove(pmc->debugfs);
  1712. cleanup_sysfs:
  1713. device_remove_file(&pdev->dev, &dev_attr_reset_reason);
  1714. device_remove_file(&pdev->dev, &dev_attr_reset_level);
  1715. return err;
  1716. }
  1717. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  1718. static int tegra_pmc_suspend(struct device *dev)
  1719. {
  1720. struct tegra_pmc *pmc = dev_get_drvdata(dev);
  1721. tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
  1722. return 0;
  1723. }
  1724. static int tegra_pmc_resume(struct device *dev)
  1725. {
  1726. struct tegra_pmc *pmc = dev_get_drvdata(dev);
  1727. tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
  1728. return 0;
  1729. }
  1730. static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
  1731. #endif
  1732. static const char * const tegra20_powergates[] = {
  1733. [TEGRA_POWERGATE_CPU] = "cpu",
  1734. [TEGRA_POWERGATE_3D] = "3d",
  1735. [TEGRA_POWERGATE_VENC] = "venc",
  1736. [TEGRA_POWERGATE_VDEC] = "vdec",
  1737. [TEGRA_POWERGATE_PCIE] = "pcie",
  1738. [TEGRA_POWERGATE_L2] = "l2",
  1739. [TEGRA_POWERGATE_MPE] = "mpe",
  1740. };
  1741. static const struct tegra_pmc_regs tegra20_pmc_regs = {
  1742. .scratch0 = 0x50,
  1743. .dpd_req = 0x1b8,
  1744. .dpd_status = 0x1bc,
  1745. .dpd2_req = 0x1c0,
  1746. .dpd2_status = 0x1c4,
  1747. .rst_status = 0x1b4,
  1748. .rst_source_shift = 0x0,
  1749. .rst_source_mask = 0x7,
  1750. .rst_level_shift = 0x0,
  1751. .rst_level_mask = 0x0,
  1752. };
  1753. static void tegra20_pmc_init(struct tegra_pmc *pmc)
  1754. {
  1755. u32 value;
  1756. /* Always enable CPU power request */
  1757. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  1758. value |= PMC_CNTRL_CPU_PWRREQ_OE;
  1759. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  1760. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  1761. if (pmc->sysclkreq_high)
  1762. value &= ~PMC_CNTRL_SYSCLK_POLARITY;
  1763. else
  1764. value |= PMC_CNTRL_SYSCLK_POLARITY;
  1765. /* configure the output polarity while the request is tristated */
  1766. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  1767. /* now enable the request */
  1768. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  1769. value |= PMC_CNTRL_SYSCLK_OE;
  1770. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  1771. }
  1772. static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
  1773. struct device_node *np,
  1774. bool invert)
  1775. {
  1776. u32 value;
  1777. value = tegra_pmc_readl(pmc, PMC_CNTRL);
  1778. if (invert)
  1779. value |= PMC_CNTRL_INTR_POLARITY;
  1780. else
  1781. value &= ~PMC_CNTRL_INTR_POLARITY;
  1782. tegra_pmc_writel(pmc, value, PMC_CNTRL);
  1783. }
  1784. static const struct tegra_pmc_soc tegra20_pmc_soc = {
  1785. .num_powergates = ARRAY_SIZE(tegra20_powergates),
  1786. .powergates = tegra20_powergates,
  1787. .num_cpu_powergates = 0,
  1788. .cpu_powergates = NULL,
  1789. .has_tsense_reset = false,
  1790. .has_gpu_clamps = false,
  1791. .needs_mbist_war = false,
  1792. .has_impl_33v_pwr = false,
  1793. .maybe_tz_only = false,
  1794. .num_io_pads = 0,
  1795. .io_pads = NULL,
  1796. .num_pin_descs = 0,
  1797. .pin_descs = NULL,
  1798. .regs = &tegra20_pmc_regs,
  1799. .init = tegra20_pmc_init,
  1800. .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
  1801. .reset_sources = NULL,
  1802. .num_reset_sources = 0,
  1803. .reset_levels = NULL,
  1804. .num_reset_levels = 0,
  1805. };
  1806. static const char * const tegra30_powergates[] = {
  1807. [TEGRA_POWERGATE_CPU] = "cpu0",
  1808. [TEGRA_POWERGATE_3D] = "3d0",
  1809. [TEGRA_POWERGATE_VENC] = "venc",
  1810. [TEGRA_POWERGATE_VDEC] = "vdec",
  1811. [TEGRA_POWERGATE_PCIE] = "pcie",
  1812. [TEGRA_POWERGATE_L2] = "l2",
  1813. [TEGRA_POWERGATE_MPE] = "mpe",
  1814. [TEGRA_POWERGATE_HEG] = "heg",
  1815. [TEGRA_POWERGATE_SATA] = "sata",
  1816. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1817. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1818. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1819. [TEGRA_POWERGATE_CELP] = "celp",
  1820. [TEGRA_POWERGATE_3D1] = "3d1",
  1821. };
  1822. static const u8 tegra30_cpu_powergates[] = {
  1823. TEGRA_POWERGATE_CPU,
  1824. TEGRA_POWERGATE_CPU1,
  1825. TEGRA_POWERGATE_CPU2,
  1826. TEGRA_POWERGATE_CPU3,
  1827. };
  1828. static const struct tegra_pmc_soc tegra30_pmc_soc = {
  1829. .num_powergates = ARRAY_SIZE(tegra30_powergates),
  1830. .powergates = tegra30_powergates,
  1831. .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
  1832. .cpu_powergates = tegra30_cpu_powergates,
  1833. .has_tsense_reset = true,
  1834. .has_gpu_clamps = false,
  1835. .needs_mbist_war = false,
  1836. .has_impl_33v_pwr = false,
  1837. .maybe_tz_only = false,
  1838. .num_io_pads = 0,
  1839. .io_pads = NULL,
  1840. .num_pin_descs = 0,
  1841. .pin_descs = NULL,
  1842. .regs = &tegra20_pmc_regs,
  1843. .init = tegra20_pmc_init,
  1844. .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
  1845. .reset_sources = tegra30_reset_sources,
  1846. .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
  1847. .reset_levels = NULL,
  1848. .num_reset_levels = 0,
  1849. };
  1850. static const char * const tegra114_powergates[] = {
  1851. [TEGRA_POWERGATE_CPU] = "crail",
  1852. [TEGRA_POWERGATE_3D] = "3d",
  1853. [TEGRA_POWERGATE_VENC] = "venc",
  1854. [TEGRA_POWERGATE_VDEC] = "vdec",
  1855. [TEGRA_POWERGATE_MPE] = "mpe",
  1856. [TEGRA_POWERGATE_HEG] = "heg",
  1857. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1858. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1859. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1860. [TEGRA_POWERGATE_CELP] = "celp",
  1861. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1862. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1863. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1864. [TEGRA_POWERGATE_DIS] = "dis",
  1865. [TEGRA_POWERGATE_DISB] = "disb",
  1866. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1867. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1868. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1869. };
  1870. static const u8 tegra114_cpu_powergates[] = {
  1871. TEGRA_POWERGATE_CPU0,
  1872. TEGRA_POWERGATE_CPU1,
  1873. TEGRA_POWERGATE_CPU2,
  1874. TEGRA_POWERGATE_CPU3,
  1875. };
  1876. static const struct tegra_pmc_soc tegra114_pmc_soc = {
  1877. .num_powergates = ARRAY_SIZE(tegra114_powergates),
  1878. .powergates = tegra114_powergates,
  1879. .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
  1880. .cpu_powergates = tegra114_cpu_powergates,
  1881. .has_tsense_reset = true,
  1882. .has_gpu_clamps = false,
  1883. .needs_mbist_war = false,
  1884. .has_impl_33v_pwr = false,
  1885. .maybe_tz_only = false,
  1886. .num_io_pads = 0,
  1887. .io_pads = NULL,
  1888. .num_pin_descs = 0,
  1889. .pin_descs = NULL,
  1890. .regs = &tegra20_pmc_regs,
  1891. .init = tegra20_pmc_init,
  1892. .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
  1893. .reset_sources = tegra30_reset_sources,
  1894. .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
  1895. .reset_levels = NULL,
  1896. .num_reset_levels = 0,
  1897. };
  1898. static const char * const tegra124_powergates[] = {
  1899. [TEGRA_POWERGATE_CPU] = "crail",
  1900. [TEGRA_POWERGATE_3D] = "3d",
  1901. [TEGRA_POWERGATE_VENC] = "venc",
  1902. [TEGRA_POWERGATE_PCIE] = "pcie",
  1903. [TEGRA_POWERGATE_VDEC] = "vdec",
  1904. [TEGRA_POWERGATE_MPE] = "mpe",
  1905. [TEGRA_POWERGATE_HEG] = "heg",
  1906. [TEGRA_POWERGATE_SATA] = "sata",
  1907. [TEGRA_POWERGATE_CPU1] = "cpu1",
  1908. [TEGRA_POWERGATE_CPU2] = "cpu2",
  1909. [TEGRA_POWERGATE_CPU3] = "cpu3",
  1910. [TEGRA_POWERGATE_CELP] = "celp",
  1911. [TEGRA_POWERGATE_CPU0] = "cpu0",
  1912. [TEGRA_POWERGATE_C0NC] = "c0nc",
  1913. [TEGRA_POWERGATE_C1NC] = "c1nc",
  1914. [TEGRA_POWERGATE_SOR] = "sor",
  1915. [TEGRA_POWERGATE_DIS] = "dis",
  1916. [TEGRA_POWERGATE_DISB] = "disb",
  1917. [TEGRA_POWERGATE_XUSBA] = "xusba",
  1918. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  1919. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  1920. [TEGRA_POWERGATE_VIC] = "vic",
  1921. [TEGRA_POWERGATE_IRAM] = "iram",
  1922. };
  1923. static const u8 tegra124_cpu_powergates[] = {
  1924. TEGRA_POWERGATE_CPU0,
  1925. TEGRA_POWERGATE_CPU1,
  1926. TEGRA_POWERGATE_CPU2,
  1927. TEGRA_POWERGATE_CPU3,
  1928. };
  1929. #define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
  1930. ((struct tegra_io_pad_soc) { \
  1931. .id = (_id), \
  1932. .dpd = (_dpd), \
  1933. .voltage = (_voltage), \
  1934. .name = (_name), \
  1935. })
  1936. #define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
  1937. ((struct pinctrl_pin_desc) { \
  1938. .number = (_id), \
  1939. .name = (_name) \
  1940. })
  1941. #define TEGRA124_IO_PAD_TABLE(_pad) \
  1942. /* .id .dpd .voltage .name */ \
  1943. _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
  1944. _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
  1945. _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
  1946. _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
  1947. _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
  1948. _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
  1949. _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
  1950. _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
  1951. _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
  1952. _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
  1953. _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
  1954. _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
  1955. _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
  1956. _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
  1957. _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
  1958. _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
  1959. _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
  1960. _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
  1961. _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
  1962. _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
  1963. _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
  1964. _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
  1965. _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
  1966. _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
  1967. _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
  1968. _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
  1969. _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
  1970. _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
  1971. _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
  1972. _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
  1973. static const struct tegra_io_pad_soc tegra124_io_pads[] = {
  1974. TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
  1975. };
  1976. static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
  1977. TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
  1978. };
  1979. static const struct tegra_pmc_soc tegra124_pmc_soc = {
  1980. .num_powergates = ARRAY_SIZE(tegra124_powergates),
  1981. .powergates = tegra124_powergates,
  1982. .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
  1983. .cpu_powergates = tegra124_cpu_powergates,
  1984. .has_tsense_reset = true,
  1985. .has_gpu_clamps = true,
  1986. .needs_mbist_war = false,
  1987. .has_impl_33v_pwr = false,
  1988. .maybe_tz_only = false,
  1989. .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
  1990. .io_pads = tegra124_io_pads,
  1991. .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
  1992. .pin_descs = tegra124_pin_descs,
  1993. .regs = &tegra20_pmc_regs,
  1994. .init = tegra20_pmc_init,
  1995. .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
  1996. .reset_sources = tegra30_reset_sources,
  1997. .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
  1998. .reset_levels = NULL,
  1999. .num_reset_levels = 0,
  2000. };
  2001. static const char * const tegra210_powergates[] = {
  2002. [TEGRA_POWERGATE_CPU] = "crail",
  2003. [TEGRA_POWERGATE_3D] = "3d",
  2004. [TEGRA_POWERGATE_VENC] = "venc",
  2005. [TEGRA_POWERGATE_PCIE] = "pcie",
  2006. [TEGRA_POWERGATE_MPE] = "mpe",
  2007. [TEGRA_POWERGATE_SATA] = "sata",
  2008. [TEGRA_POWERGATE_CPU1] = "cpu1",
  2009. [TEGRA_POWERGATE_CPU2] = "cpu2",
  2010. [TEGRA_POWERGATE_CPU3] = "cpu3",
  2011. [TEGRA_POWERGATE_CPU0] = "cpu0",
  2012. [TEGRA_POWERGATE_C0NC] = "c0nc",
  2013. [TEGRA_POWERGATE_SOR] = "sor",
  2014. [TEGRA_POWERGATE_DIS] = "dis",
  2015. [TEGRA_POWERGATE_DISB] = "disb",
  2016. [TEGRA_POWERGATE_XUSBA] = "xusba",
  2017. [TEGRA_POWERGATE_XUSBB] = "xusbb",
  2018. [TEGRA_POWERGATE_XUSBC] = "xusbc",
  2019. [TEGRA_POWERGATE_VIC] = "vic",
  2020. [TEGRA_POWERGATE_IRAM] = "iram",
  2021. [TEGRA_POWERGATE_NVDEC] = "nvdec",
  2022. [TEGRA_POWERGATE_NVJPG] = "nvjpg",
  2023. [TEGRA_POWERGATE_AUD] = "aud",
  2024. [TEGRA_POWERGATE_DFD] = "dfd",
  2025. [TEGRA_POWERGATE_VE2] = "ve2",
  2026. };
  2027. static const u8 tegra210_cpu_powergates[] = {
  2028. TEGRA_POWERGATE_CPU0,
  2029. TEGRA_POWERGATE_CPU1,
  2030. TEGRA_POWERGATE_CPU2,
  2031. TEGRA_POWERGATE_CPU3,
  2032. };
  2033. #define TEGRA210_IO_PAD_TABLE(_pad) \
  2034. /* .id .dpd .voltage .name */ \
  2035. _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
  2036. _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
  2037. _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
  2038. _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
  2039. _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
  2040. _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
  2041. _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
  2042. _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
  2043. _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
  2044. _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
  2045. _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
  2046. _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
  2047. _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
  2048. _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
  2049. _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
  2050. _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
  2051. _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
  2052. _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
  2053. _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
  2054. _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
  2055. _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
  2056. _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
  2057. _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
  2058. _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
  2059. _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
  2060. _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
  2061. _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
  2062. _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
  2063. _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
  2064. _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
  2065. _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
  2066. _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
  2067. _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
  2068. _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
  2069. _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
  2070. _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
  2071. _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
  2072. _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
  2073. static const struct tegra_io_pad_soc tegra210_io_pads[] = {
  2074. TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
  2075. };
  2076. static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
  2077. TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
  2078. };
  2079. static const struct tegra_pmc_soc tegra210_pmc_soc = {
  2080. .num_powergates = ARRAY_SIZE(tegra210_powergates),
  2081. .powergates = tegra210_powergates,
  2082. .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
  2083. .cpu_powergates = tegra210_cpu_powergates,
  2084. .has_tsense_reset = true,
  2085. .has_gpu_clamps = true,
  2086. .needs_mbist_war = true,
  2087. .has_impl_33v_pwr = false,
  2088. .maybe_tz_only = true,
  2089. .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
  2090. .io_pads = tegra210_io_pads,
  2091. .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
  2092. .pin_descs = tegra210_pin_descs,
  2093. .regs = &tegra20_pmc_regs,
  2094. .init = tegra20_pmc_init,
  2095. .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
  2096. .reset_sources = tegra210_reset_sources,
  2097. .num_reset_sources = ARRAY_SIZE(tegra210_reset_sources),
  2098. .reset_levels = NULL,
  2099. .num_reset_levels = 0,
  2100. };
  2101. #define TEGRA186_IO_PAD_TABLE(_pad) \
  2102. /* .id .dpd .voltage .name */ \
  2103. _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
  2104. _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
  2105. _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
  2106. _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
  2107. _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
  2108. _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
  2109. _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
  2110. _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
  2111. _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
  2112. _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
  2113. _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
  2114. _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
  2115. _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
  2116. _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
  2117. _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
  2118. _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
  2119. _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
  2120. _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
  2121. _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
  2122. _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
  2123. _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
  2124. _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
  2125. _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
  2126. _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
  2127. _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
  2128. _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
  2129. _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
  2130. _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
  2131. _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
  2132. _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
  2133. _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
  2134. _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
  2135. _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
  2136. _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
  2137. _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
  2138. _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
  2139. _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
  2140. _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
  2141. static const struct tegra_io_pad_soc tegra186_io_pads[] = {
  2142. TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
  2143. };
  2144. static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
  2145. TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
  2146. };
  2147. static const struct tegra_pmc_regs tegra186_pmc_regs = {
  2148. .scratch0 = 0x2000,
  2149. .dpd_req = 0x74,
  2150. .dpd_status = 0x78,
  2151. .dpd2_req = 0x7c,
  2152. .dpd2_status = 0x80,
  2153. .rst_status = 0x70,
  2154. .rst_source_shift = 0x2,
  2155. .rst_source_mask = 0x3C,
  2156. .rst_level_shift = 0x0,
  2157. .rst_level_mask = 0x3,
  2158. };
  2159. static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
  2160. struct device_node *np,
  2161. bool invert)
  2162. {
  2163. struct resource regs;
  2164. void __iomem *wake;
  2165. u32 value;
  2166. int index;
  2167. index = of_property_match_string(np, "reg-names", "wake");
  2168. if (index < 0) {
  2169. dev_err(pmc->dev, "failed to find PMC wake registers\n");
  2170. return;
  2171. }
  2172. of_address_to_resource(np, index, &regs);
  2173. wake = ioremap_nocache(regs.start, resource_size(&regs));
  2174. if (!wake) {
  2175. dev_err(pmc->dev, "failed to map PMC wake registers\n");
  2176. return;
  2177. }
  2178. value = readl(wake + WAKE_AOWAKE_CTRL);
  2179. if (invert)
  2180. value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
  2181. else
  2182. value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
  2183. writel(value, wake + WAKE_AOWAKE_CTRL);
  2184. iounmap(wake);
  2185. }
  2186. static const struct tegra_wake_event tegra186_wake_events[] = {
  2187. TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
  2188. TEGRA_WAKE_IRQ("rtc", 73, 10),
  2189. };
  2190. static const struct tegra_pmc_soc tegra186_pmc_soc = {
  2191. .num_powergates = 0,
  2192. .powergates = NULL,
  2193. .num_cpu_powergates = 0,
  2194. .cpu_powergates = NULL,
  2195. .has_tsense_reset = false,
  2196. .has_gpu_clamps = false,
  2197. .needs_mbist_war = false,
  2198. .has_impl_33v_pwr = true,
  2199. .maybe_tz_only = false,
  2200. .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
  2201. .io_pads = tegra186_io_pads,
  2202. .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
  2203. .pin_descs = tegra186_pin_descs,
  2204. .regs = &tegra186_pmc_regs,
  2205. .init = NULL,
  2206. .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
  2207. .reset_sources = tegra186_reset_sources,
  2208. .num_reset_sources = ARRAY_SIZE(tegra186_reset_sources),
  2209. .reset_levels = tegra186_reset_levels,
  2210. .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
  2211. .num_wake_events = ARRAY_SIZE(tegra186_wake_events),
  2212. .wake_events = tegra186_wake_events,
  2213. };
  2214. static const struct tegra_io_pad_soc tegra194_io_pads[] = {
  2215. { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
  2216. { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
  2217. { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
  2218. { .id = TEGRA_IO_PAD_PEX_CLK_BIAS, .dpd = 4, .voltage = UINT_MAX },
  2219. { .id = TEGRA_IO_PAD_PEX_CLK3, .dpd = 5, .voltage = UINT_MAX },
  2220. { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
  2221. { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 7, .voltage = UINT_MAX },
  2222. { .id = TEGRA_IO_PAD_EQOS, .dpd = 8, .voltage = UINT_MAX },
  2223. { .id = TEGRA_IO_PAD_PEX_CLK2_BIAS, .dpd = 9, .voltage = UINT_MAX },
  2224. { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 10, .voltage = UINT_MAX },
  2225. { .id = TEGRA_IO_PAD_DAP3, .dpd = 11, .voltage = UINT_MAX },
  2226. { .id = TEGRA_IO_PAD_DAP5, .dpd = 12, .voltage = UINT_MAX },
  2227. { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = UINT_MAX },
  2228. { .id = TEGRA_IO_PAD_PWR_CTL, .dpd = 15, .voltage = UINT_MAX },
  2229. { .id = TEGRA_IO_PAD_SOC_GPIO53, .dpd = 16, .voltage = UINT_MAX },
  2230. { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = UINT_MAX },
  2231. { .id = TEGRA_IO_PAD_GP_PWM2, .dpd = 18, .voltage = UINT_MAX },
  2232. { .id = TEGRA_IO_PAD_GP_PWM3, .dpd = 19, .voltage = UINT_MAX },
  2233. { .id = TEGRA_IO_PAD_SOC_GPIO12, .dpd = 20, .voltage = UINT_MAX },
  2234. { .id = TEGRA_IO_PAD_SOC_GPIO13, .dpd = 21, .voltage = UINT_MAX },
  2235. { .id = TEGRA_IO_PAD_SOC_GPIO10, .dpd = 22, .voltage = UINT_MAX },
  2236. { .id = TEGRA_IO_PAD_UART4, .dpd = 23, .voltage = UINT_MAX },
  2237. { .id = TEGRA_IO_PAD_UART5, .dpd = 24, .voltage = UINT_MAX },
  2238. { .id = TEGRA_IO_PAD_DBG, .dpd = 25, .voltage = UINT_MAX },
  2239. { .id = TEGRA_IO_PAD_HDMI_DP3, .dpd = 26, .voltage = UINT_MAX },
  2240. { .id = TEGRA_IO_PAD_HDMI_DP2, .dpd = 27, .voltage = UINT_MAX },
  2241. { .id = TEGRA_IO_PAD_HDMI_DP0, .dpd = 28, .voltage = UINT_MAX },
  2242. { .id = TEGRA_IO_PAD_HDMI_DP1, .dpd = 29, .voltage = UINT_MAX },
  2243. { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = 32, .voltage = UINT_MAX },
  2244. { .id = TEGRA_IO_PAD_PEX_CTL2, .dpd = 33, .voltage = UINT_MAX },
  2245. { .id = TEGRA_IO_PAD_PEX_L0_RST_N, .dpd = 34, .voltage = UINT_MAX },
  2246. { .id = TEGRA_IO_PAD_PEX_L1_RST_N, .dpd = 35, .voltage = UINT_MAX },
  2247. { .id = TEGRA_IO_PAD_SDMMC4, .dpd = 36, .voltage = UINT_MAX },
  2248. { .id = TEGRA_IO_PAD_PEX_L5_RST_N, .dpd = 37, .voltage = UINT_MAX },
  2249. { .id = TEGRA_IO_PAD_CSIC, .dpd = 43, .voltage = UINT_MAX },
  2250. { .id = TEGRA_IO_PAD_CSID, .dpd = 44, .voltage = UINT_MAX },
  2251. { .id = TEGRA_IO_PAD_CSIE, .dpd = 45, .voltage = UINT_MAX },
  2252. { .id = TEGRA_IO_PAD_CSIF, .dpd = 46, .voltage = UINT_MAX },
  2253. { .id = TEGRA_IO_PAD_SPI, .dpd = 47, .voltage = UINT_MAX },
  2254. { .id = TEGRA_IO_PAD_UFS, .dpd = 49, .voltage = UINT_MAX },
  2255. { .id = TEGRA_IO_PAD_CSIG, .dpd = 50, .voltage = UINT_MAX },
  2256. { .id = TEGRA_IO_PAD_CSIH, .dpd = 51, .voltage = UINT_MAX },
  2257. { .id = TEGRA_IO_PAD_EDP, .dpd = 53, .voltage = UINT_MAX },
  2258. { .id = TEGRA_IO_PAD_SDMMC1_HV, .dpd = 55, .voltage = UINT_MAX },
  2259. { .id = TEGRA_IO_PAD_SDMMC3_HV, .dpd = 56, .voltage = UINT_MAX },
  2260. { .id = TEGRA_IO_PAD_CONN, .dpd = 60, .voltage = UINT_MAX },
  2261. { .id = TEGRA_IO_PAD_AUDIO_HV, .dpd = 61, .voltage = UINT_MAX },
  2262. };
  2263. static const struct tegra_wake_event tegra194_wake_events[] = {
  2264. TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
  2265. TEGRA_WAKE_IRQ("rtc", 73, 10),
  2266. };
  2267. static const struct tegra_pmc_soc tegra194_pmc_soc = {
  2268. .num_powergates = 0,
  2269. .powergates = NULL,
  2270. .num_cpu_powergates = 0,
  2271. .cpu_powergates = NULL,
  2272. .has_tsense_reset = false,
  2273. .has_gpu_clamps = false,
  2274. .needs_mbist_war = false,
  2275. .has_impl_33v_pwr = false,
  2276. .maybe_tz_only = false,
  2277. .num_io_pads = ARRAY_SIZE(tegra194_io_pads),
  2278. .io_pads = tegra194_io_pads,
  2279. .regs = &tegra186_pmc_regs,
  2280. .init = NULL,
  2281. .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
  2282. .num_wake_events = ARRAY_SIZE(tegra194_wake_events),
  2283. .wake_events = tegra194_wake_events,
  2284. };
  2285. static const struct of_device_id tegra_pmc_match[] = {
  2286. { .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
  2287. { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
  2288. { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
  2289. { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
  2290. { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
  2291. { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
  2292. { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
  2293. { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
  2294. { }
  2295. };
  2296. static struct platform_driver tegra_pmc_driver = {
  2297. .driver = {
  2298. .name = "tegra-pmc",
  2299. .suppress_bind_attrs = true,
  2300. .of_match_table = tegra_pmc_match,
  2301. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
  2302. .pm = &tegra_pmc_pm_ops,
  2303. #endif
  2304. },
  2305. .probe = tegra_pmc_probe,
  2306. };
  2307. builtin_platform_driver(tegra_pmc_driver);
  2308. static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
  2309. {
  2310. u32 value, saved;
  2311. saved = readl(pmc->base + pmc->soc->regs->scratch0);
  2312. value = saved ^ 0xffffffff;
  2313. if (value == 0xffffffff)
  2314. value = 0xdeadbeef;
  2315. /* write pattern and read it back */
  2316. writel(value, pmc->base + pmc->soc->regs->scratch0);
  2317. value = readl(pmc->base + pmc->soc->regs->scratch0);
  2318. /* if we read all-zeroes, access is restricted to TZ only */
  2319. if (value == 0) {
  2320. pr_info("access to PMC is restricted to TZ\n");
  2321. return true;
  2322. }
  2323. /* restore original value */
  2324. writel(saved, pmc->base + pmc->soc->regs->scratch0);
  2325. return false;
  2326. }
  2327. /*
  2328. * Early initialization to allow access to registers in the very early boot
  2329. * process.
  2330. */
  2331. static int __init tegra_pmc_early_init(void)
  2332. {
  2333. const struct of_device_id *match;
  2334. struct device_node *np;
  2335. struct resource regs;
  2336. unsigned int i;
  2337. bool invert;
  2338. mutex_init(&pmc->powergates_lock);
  2339. np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
  2340. if (!np) {
  2341. /*
  2342. * Fall back to legacy initialization for 32-bit ARM only. All
  2343. * 64-bit ARM device tree files for Tegra are required to have
  2344. * a PMC node.
  2345. *
  2346. * This is for backwards-compatibility with old device trees
  2347. * that didn't contain a PMC node. Note that in this case the
  2348. * SoC data can't be matched and therefore powergating is
  2349. * disabled.
  2350. */
  2351. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  2352. pr_warn("DT node not found, powergating disabled\n");
  2353. regs.start = 0x7000e400;
  2354. regs.end = 0x7000e7ff;
  2355. regs.flags = IORESOURCE_MEM;
  2356. pr_warn("Using memory region %pR\n", &regs);
  2357. } else {
  2358. /*
  2359. * At this point we're not running on Tegra, so play
  2360. * nice with multi-platform kernels.
  2361. */
  2362. return 0;
  2363. }
  2364. } else {
  2365. /*
  2366. * Extract information from the device tree if we've found a
  2367. * matching node.
  2368. */
  2369. if (of_address_to_resource(np, 0, &regs) < 0) {
  2370. pr_err("failed to get PMC registers\n");
  2371. of_node_put(np);
  2372. return -ENXIO;
  2373. }
  2374. }
  2375. pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
  2376. if (!pmc->base) {
  2377. pr_err("failed to map PMC registers\n");
  2378. of_node_put(np);
  2379. return -ENXIO;
  2380. }
  2381. if (np) {
  2382. pmc->soc = match->data;
  2383. if (pmc->soc->maybe_tz_only)
  2384. pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
  2385. /* Create a bitmap of the available and valid partitions */
  2386. for (i = 0; i < pmc->soc->num_powergates; i++)
  2387. if (pmc->soc->powergates[i])
  2388. set_bit(i, pmc->powergates_available);
  2389. /*
  2390. * Invert the interrupt polarity if a PMC device tree node
  2391. * exists and contains the nvidia,invert-interrupt property.
  2392. */
  2393. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  2394. pmc->soc->setup_irq_polarity(pmc, np, invert);
  2395. of_node_put(np);
  2396. }
  2397. return 0;
  2398. }
  2399. early_initcall(tegra_pmc_early_init);