mtk-pmic-wrap.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Flora Fu, MediaTek
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #define PWRAP_MT8135_BRIDGE_IORD_ARB_EN 0x4
  24. #define PWRAP_MT8135_BRIDGE_WACS3_EN 0x10
  25. #define PWRAP_MT8135_BRIDGE_INIT_DONE3 0x14
  26. #define PWRAP_MT8135_BRIDGE_WACS4_EN 0x24
  27. #define PWRAP_MT8135_BRIDGE_INIT_DONE4 0x28
  28. #define PWRAP_MT8135_BRIDGE_INT_EN 0x38
  29. #define PWRAP_MT8135_BRIDGE_TIMER_EN 0x48
  30. #define PWRAP_MT8135_BRIDGE_WDT_UNIT 0x50
  31. #define PWRAP_MT8135_BRIDGE_WDT_SRC_EN 0x54
  32. /* macro for wrapper status */
  33. #define PWRAP_GET_WACS_RDATA(x) (((x) >> 0) & 0x0000ffff)
  34. #define PWRAP_GET_WACS_FSM(x) (((x) >> 16) & 0x00000007)
  35. #define PWRAP_GET_WACS_REQ(x) (((x) >> 19) & 0x00000001)
  36. #define PWRAP_STATE_SYNC_IDLE0 (1 << 20)
  37. #define PWRAP_STATE_INIT_DONE0 (1 << 21)
  38. /* macro for WACS FSM */
  39. #define PWRAP_WACS_FSM_IDLE 0x00
  40. #define PWRAP_WACS_FSM_REQ 0x02
  41. #define PWRAP_WACS_FSM_WFDLE 0x04
  42. #define PWRAP_WACS_FSM_WFVLDCLR 0x06
  43. #define PWRAP_WACS_INIT_DONE 0x01
  44. #define PWRAP_WACS_WACS_SYNC_IDLE 0x01
  45. #define PWRAP_WACS_SYNC_BUSY 0x00
  46. /* macro for device wrapper default value */
  47. #define PWRAP_DEW_READ_TEST_VAL 0x5aa5
  48. #define PWRAP_DEW_WRITE_TEST_VAL 0xa55a
  49. /* macro for manual command */
  50. #define PWRAP_MAN_CMD_SPI_WRITE_NEW (1 << 14)
  51. #define PWRAP_MAN_CMD_SPI_WRITE (1 << 13)
  52. #define PWRAP_MAN_CMD_OP_CSH (0x0 << 8)
  53. #define PWRAP_MAN_CMD_OP_CSL (0x1 << 8)
  54. #define PWRAP_MAN_CMD_OP_CK (0x2 << 8)
  55. #define PWRAP_MAN_CMD_OP_OUTS (0x8 << 8)
  56. #define PWRAP_MAN_CMD_OP_OUTD (0x9 << 8)
  57. #define PWRAP_MAN_CMD_OP_OUTQ (0xa << 8)
  58. /* macro for Watch Dog Timer Source */
  59. #define PWRAP_WDT_SRC_EN_STAUPD_TRIG (1 << 25)
  60. #define PWRAP_WDT_SRC_EN_HARB_STAUPD_DLE (1 << 20)
  61. #define PWRAP_WDT_SRC_EN_HARB_STAUPD_ALE (1 << 6)
  62. #define PWRAP_WDT_SRC_MASK_ALL 0xffffffff
  63. #define PWRAP_WDT_SRC_MASK_NO_STAUPD ~(PWRAP_WDT_SRC_EN_STAUPD_TRIG | \
  64. PWRAP_WDT_SRC_EN_HARB_STAUPD_DLE | \
  65. PWRAP_WDT_SRC_EN_HARB_STAUPD_ALE)
  66. /* defines for slave device wrapper registers */
  67. enum dew_regs {
  68. PWRAP_DEW_BASE,
  69. PWRAP_DEW_DIO_EN,
  70. PWRAP_DEW_READ_TEST,
  71. PWRAP_DEW_WRITE_TEST,
  72. PWRAP_DEW_CRC_EN,
  73. PWRAP_DEW_CRC_VAL,
  74. PWRAP_DEW_MON_GRP_SEL,
  75. PWRAP_DEW_CIPHER_KEY_SEL,
  76. PWRAP_DEW_CIPHER_IV_SEL,
  77. PWRAP_DEW_CIPHER_RDY,
  78. PWRAP_DEW_CIPHER_MODE,
  79. PWRAP_DEW_CIPHER_SWRST,
  80. /* MT6397 only regs */
  81. PWRAP_DEW_EVENT_OUT_EN,
  82. PWRAP_DEW_EVENT_SRC_EN,
  83. PWRAP_DEW_EVENT_SRC,
  84. PWRAP_DEW_EVENT_FLAG,
  85. PWRAP_DEW_MON_FLAG_SEL,
  86. PWRAP_DEW_EVENT_TEST,
  87. PWRAP_DEW_CIPHER_LOAD,
  88. PWRAP_DEW_CIPHER_START,
  89. /* MT6323 only regs */
  90. PWRAP_DEW_CIPHER_EN,
  91. PWRAP_DEW_RDDMY_NO,
  92. };
  93. static const u32 mt6323_regs[] = {
  94. [PWRAP_DEW_BASE] = 0x0000,
  95. [PWRAP_DEW_DIO_EN] = 0x018a,
  96. [PWRAP_DEW_READ_TEST] = 0x018c,
  97. [PWRAP_DEW_WRITE_TEST] = 0x018e,
  98. [PWRAP_DEW_CRC_EN] = 0x0192,
  99. [PWRAP_DEW_CRC_VAL] = 0x0194,
  100. [PWRAP_DEW_MON_GRP_SEL] = 0x0196,
  101. [PWRAP_DEW_CIPHER_KEY_SEL] = 0x0198,
  102. [PWRAP_DEW_CIPHER_IV_SEL] = 0x019a,
  103. [PWRAP_DEW_CIPHER_EN] = 0x019c,
  104. [PWRAP_DEW_CIPHER_RDY] = 0x019e,
  105. [PWRAP_DEW_CIPHER_MODE] = 0x01a0,
  106. [PWRAP_DEW_CIPHER_SWRST] = 0x01a2,
  107. [PWRAP_DEW_RDDMY_NO] = 0x01a4,
  108. };
  109. static const u32 mt6397_regs[] = {
  110. [PWRAP_DEW_BASE] = 0xbc00,
  111. [PWRAP_DEW_EVENT_OUT_EN] = 0xbc00,
  112. [PWRAP_DEW_DIO_EN] = 0xbc02,
  113. [PWRAP_DEW_EVENT_SRC_EN] = 0xbc04,
  114. [PWRAP_DEW_EVENT_SRC] = 0xbc06,
  115. [PWRAP_DEW_EVENT_FLAG] = 0xbc08,
  116. [PWRAP_DEW_READ_TEST] = 0xbc0a,
  117. [PWRAP_DEW_WRITE_TEST] = 0xbc0c,
  118. [PWRAP_DEW_CRC_EN] = 0xbc0e,
  119. [PWRAP_DEW_CRC_VAL] = 0xbc10,
  120. [PWRAP_DEW_MON_GRP_SEL] = 0xbc12,
  121. [PWRAP_DEW_MON_FLAG_SEL] = 0xbc14,
  122. [PWRAP_DEW_EVENT_TEST] = 0xbc16,
  123. [PWRAP_DEW_CIPHER_KEY_SEL] = 0xbc18,
  124. [PWRAP_DEW_CIPHER_IV_SEL] = 0xbc1a,
  125. [PWRAP_DEW_CIPHER_LOAD] = 0xbc1c,
  126. [PWRAP_DEW_CIPHER_START] = 0xbc1e,
  127. [PWRAP_DEW_CIPHER_RDY] = 0xbc20,
  128. [PWRAP_DEW_CIPHER_MODE] = 0xbc22,
  129. [PWRAP_DEW_CIPHER_SWRST] = 0xbc24,
  130. };
  131. enum pwrap_regs {
  132. PWRAP_MUX_SEL,
  133. PWRAP_WRAP_EN,
  134. PWRAP_DIO_EN,
  135. PWRAP_SIDLY,
  136. PWRAP_CSHEXT_WRITE,
  137. PWRAP_CSHEXT_READ,
  138. PWRAP_CSLEXT_START,
  139. PWRAP_CSLEXT_END,
  140. PWRAP_STAUPD_PRD,
  141. PWRAP_STAUPD_GRPEN,
  142. PWRAP_STAUPD_MAN_TRIG,
  143. PWRAP_STAUPD_STA,
  144. PWRAP_WRAP_STA,
  145. PWRAP_HARB_INIT,
  146. PWRAP_HARB_HPRIO,
  147. PWRAP_HIPRIO_ARB_EN,
  148. PWRAP_HARB_STA0,
  149. PWRAP_HARB_STA1,
  150. PWRAP_MAN_EN,
  151. PWRAP_MAN_CMD,
  152. PWRAP_MAN_RDATA,
  153. PWRAP_MAN_VLDCLR,
  154. PWRAP_WACS0_EN,
  155. PWRAP_INIT_DONE0,
  156. PWRAP_WACS0_CMD,
  157. PWRAP_WACS0_RDATA,
  158. PWRAP_WACS0_VLDCLR,
  159. PWRAP_WACS1_EN,
  160. PWRAP_INIT_DONE1,
  161. PWRAP_WACS1_CMD,
  162. PWRAP_WACS1_RDATA,
  163. PWRAP_WACS1_VLDCLR,
  164. PWRAP_WACS2_EN,
  165. PWRAP_INIT_DONE2,
  166. PWRAP_WACS2_CMD,
  167. PWRAP_WACS2_RDATA,
  168. PWRAP_WACS2_VLDCLR,
  169. PWRAP_INT_EN,
  170. PWRAP_INT_FLG_RAW,
  171. PWRAP_INT_FLG,
  172. PWRAP_INT_CLR,
  173. PWRAP_SIG_ADR,
  174. PWRAP_SIG_MODE,
  175. PWRAP_SIG_VALUE,
  176. PWRAP_SIG_ERRVAL,
  177. PWRAP_CRC_EN,
  178. PWRAP_TIMER_EN,
  179. PWRAP_TIMER_STA,
  180. PWRAP_WDT_UNIT,
  181. PWRAP_WDT_SRC_EN,
  182. PWRAP_WDT_FLG,
  183. PWRAP_DEBUG_INT_SEL,
  184. PWRAP_CIPHER_KEY_SEL,
  185. PWRAP_CIPHER_IV_SEL,
  186. PWRAP_CIPHER_RDY,
  187. PWRAP_CIPHER_MODE,
  188. PWRAP_CIPHER_SWRST,
  189. PWRAP_DCM_EN,
  190. PWRAP_DCM_DBC_PRD,
  191. /* MT2701 only regs */
  192. PWRAP_ADC_CMD_ADDR,
  193. PWRAP_PWRAP_ADC_CMD,
  194. PWRAP_ADC_RDY_ADDR,
  195. PWRAP_ADC_RDATA_ADDR1,
  196. PWRAP_ADC_RDATA_ADDR2,
  197. /* MT8135 only regs */
  198. PWRAP_CSHEXT,
  199. PWRAP_EVENT_IN_EN,
  200. PWRAP_EVENT_DST_EN,
  201. PWRAP_RRARB_INIT,
  202. PWRAP_RRARB_EN,
  203. PWRAP_RRARB_STA0,
  204. PWRAP_RRARB_STA1,
  205. PWRAP_EVENT_STA,
  206. PWRAP_EVENT_STACLR,
  207. PWRAP_CIPHER_LOAD,
  208. PWRAP_CIPHER_START,
  209. /* MT8173 only regs */
  210. PWRAP_RDDMY,
  211. PWRAP_SI_CK_CON,
  212. PWRAP_DVFS_ADR0,
  213. PWRAP_DVFS_WDATA0,
  214. PWRAP_DVFS_ADR1,
  215. PWRAP_DVFS_WDATA1,
  216. PWRAP_DVFS_ADR2,
  217. PWRAP_DVFS_WDATA2,
  218. PWRAP_DVFS_ADR3,
  219. PWRAP_DVFS_WDATA3,
  220. PWRAP_DVFS_ADR4,
  221. PWRAP_DVFS_WDATA4,
  222. PWRAP_DVFS_ADR5,
  223. PWRAP_DVFS_WDATA5,
  224. PWRAP_DVFS_ADR6,
  225. PWRAP_DVFS_WDATA6,
  226. PWRAP_DVFS_ADR7,
  227. PWRAP_DVFS_WDATA7,
  228. PWRAP_SPMINF_STA,
  229. PWRAP_CIPHER_EN,
  230. };
  231. static int mt2701_regs[] = {
  232. [PWRAP_MUX_SEL] = 0x0,
  233. [PWRAP_WRAP_EN] = 0x4,
  234. [PWRAP_DIO_EN] = 0x8,
  235. [PWRAP_SIDLY] = 0xc,
  236. [PWRAP_RDDMY] = 0x18,
  237. [PWRAP_SI_CK_CON] = 0x1c,
  238. [PWRAP_CSHEXT_WRITE] = 0x20,
  239. [PWRAP_CSHEXT_READ] = 0x24,
  240. [PWRAP_CSLEXT_START] = 0x28,
  241. [PWRAP_CSLEXT_END] = 0x2c,
  242. [PWRAP_STAUPD_PRD] = 0x30,
  243. [PWRAP_STAUPD_GRPEN] = 0x34,
  244. [PWRAP_STAUPD_MAN_TRIG] = 0x38,
  245. [PWRAP_STAUPD_STA] = 0x3c,
  246. [PWRAP_WRAP_STA] = 0x44,
  247. [PWRAP_HARB_INIT] = 0x48,
  248. [PWRAP_HARB_HPRIO] = 0x4c,
  249. [PWRAP_HIPRIO_ARB_EN] = 0x50,
  250. [PWRAP_HARB_STA0] = 0x54,
  251. [PWRAP_HARB_STA1] = 0x58,
  252. [PWRAP_MAN_EN] = 0x5c,
  253. [PWRAP_MAN_CMD] = 0x60,
  254. [PWRAP_MAN_RDATA] = 0x64,
  255. [PWRAP_MAN_VLDCLR] = 0x68,
  256. [PWRAP_WACS0_EN] = 0x6c,
  257. [PWRAP_INIT_DONE0] = 0x70,
  258. [PWRAP_WACS0_CMD] = 0x74,
  259. [PWRAP_WACS0_RDATA] = 0x78,
  260. [PWRAP_WACS0_VLDCLR] = 0x7c,
  261. [PWRAP_WACS1_EN] = 0x80,
  262. [PWRAP_INIT_DONE1] = 0x84,
  263. [PWRAP_WACS1_CMD] = 0x88,
  264. [PWRAP_WACS1_RDATA] = 0x8c,
  265. [PWRAP_WACS1_VLDCLR] = 0x90,
  266. [PWRAP_WACS2_EN] = 0x94,
  267. [PWRAP_INIT_DONE2] = 0x98,
  268. [PWRAP_WACS2_CMD] = 0x9c,
  269. [PWRAP_WACS2_RDATA] = 0xa0,
  270. [PWRAP_WACS2_VLDCLR] = 0xa4,
  271. [PWRAP_INT_EN] = 0xa8,
  272. [PWRAP_INT_FLG_RAW] = 0xac,
  273. [PWRAP_INT_FLG] = 0xb0,
  274. [PWRAP_INT_CLR] = 0xb4,
  275. [PWRAP_SIG_ADR] = 0xb8,
  276. [PWRAP_SIG_MODE] = 0xbc,
  277. [PWRAP_SIG_VALUE] = 0xc0,
  278. [PWRAP_SIG_ERRVAL] = 0xc4,
  279. [PWRAP_CRC_EN] = 0xc8,
  280. [PWRAP_TIMER_EN] = 0xcc,
  281. [PWRAP_TIMER_STA] = 0xd0,
  282. [PWRAP_WDT_UNIT] = 0xd4,
  283. [PWRAP_WDT_SRC_EN] = 0xd8,
  284. [PWRAP_WDT_FLG] = 0xdc,
  285. [PWRAP_DEBUG_INT_SEL] = 0xe0,
  286. [PWRAP_DVFS_ADR0] = 0xe4,
  287. [PWRAP_DVFS_WDATA0] = 0xe8,
  288. [PWRAP_DVFS_ADR1] = 0xec,
  289. [PWRAP_DVFS_WDATA1] = 0xf0,
  290. [PWRAP_DVFS_ADR2] = 0xf4,
  291. [PWRAP_DVFS_WDATA2] = 0xf8,
  292. [PWRAP_DVFS_ADR3] = 0xfc,
  293. [PWRAP_DVFS_WDATA3] = 0x100,
  294. [PWRAP_DVFS_ADR4] = 0x104,
  295. [PWRAP_DVFS_WDATA4] = 0x108,
  296. [PWRAP_DVFS_ADR5] = 0x10c,
  297. [PWRAP_DVFS_WDATA5] = 0x110,
  298. [PWRAP_DVFS_ADR6] = 0x114,
  299. [PWRAP_DVFS_WDATA6] = 0x118,
  300. [PWRAP_DVFS_ADR7] = 0x11c,
  301. [PWRAP_DVFS_WDATA7] = 0x120,
  302. [PWRAP_CIPHER_KEY_SEL] = 0x124,
  303. [PWRAP_CIPHER_IV_SEL] = 0x128,
  304. [PWRAP_CIPHER_EN] = 0x12c,
  305. [PWRAP_CIPHER_RDY] = 0x130,
  306. [PWRAP_CIPHER_MODE] = 0x134,
  307. [PWRAP_CIPHER_SWRST] = 0x138,
  308. [PWRAP_DCM_EN] = 0x13c,
  309. [PWRAP_DCM_DBC_PRD] = 0x140,
  310. [PWRAP_ADC_CMD_ADDR] = 0x144,
  311. [PWRAP_PWRAP_ADC_CMD] = 0x148,
  312. [PWRAP_ADC_RDY_ADDR] = 0x14c,
  313. [PWRAP_ADC_RDATA_ADDR1] = 0x150,
  314. [PWRAP_ADC_RDATA_ADDR2] = 0x154,
  315. };
  316. static int mt8173_regs[] = {
  317. [PWRAP_MUX_SEL] = 0x0,
  318. [PWRAP_WRAP_EN] = 0x4,
  319. [PWRAP_DIO_EN] = 0x8,
  320. [PWRAP_SIDLY] = 0xc,
  321. [PWRAP_RDDMY] = 0x10,
  322. [PWRAP_SI_CK_CON] = 0x14,
  323. [PWRAP_CSHEXT_WRITE] = 0x18,
  324. [PWRAP_CSHEXT_READ] = 0x1c,
  325. [PWRAP_CSLEXT_START] = 0x20,
  326. [PWRAP_CSLEXT_END] = 0x24,
  327. [PWRAP_STAUPD_PRD] = 0x28,
  328. [PWRAP_STAUPD_GRPEN] = 0x2c,
  329. [PWRAP_STAUPD_MAN_TRIG] = 0x40,
  330. [PWRAP_STAUPD_STA] = 0x44,
  331. [PWRAP_WRAP_STA] = 0x48,
  332. [PWRAP_HARB_INIT] = 0x4c,
  333. [PWRAP_HARB_HPRIO] = 0x50,
  334. [PWRAP_HIPRIO_ARB_EN] = 0x54,
  335. [PWRAP_HARB_STA0] = 0x58,
  336. [PWRAP_HARB_STA1] = 0x5c,
  337. [PWRAP_MAN_EN] = 0x60,
  338. [PWRAP_MAN_CMD] = 0x64,
  339. [PWRAP_MAN_RDATA] = 0x68,
  340. [PWRAP_MAN_VLDCLR] = 0x6c,
  341. [PWRAP_WACS0_EN] = 0x70,
  342. [PWRAP_INIT_DONE0] = 0x74,
  343. [PWRAP_WACS0_CMD] = 0x78,
  344. [PWRAP_WACS0_RDATA] = 0x7c,
  345. [PWRAP_WACS0_VLDCLR] = 0x80,
  346. [PWRAP_WACS1_EN] = 0x84,
  347. [PWRAP_INIT_DONE1] = 0x88,
  348. [PWRAP_WACS1_CMD] = 0x8c,
  349. [PWRAP_WACS1_RDATA] = 0x90,
  350. [PWRAP_WACS1_VLDCLR] = 0x94,
  351. [PWRAP_WACS2_EN] = 0x98,
  352. [PWRAP_INIT_DONE2] = 0x9c,
  353. [PWRAP_WACS2_CMD] = 0xa0,
  354. [PWRAP_WACS2_RDATA] = 0xa4,
  355. [PWRAP_WACS2_VLDCLR] = 0xa8,
  356. [PWRAP_INT_EN] = 0xac,
  357. [PWRAP_INT_FLG_RAW] = 0xb0,
  358. [PWRAP_INT_FLG] = 0xb4,
  359. [PWRAP_INT_CLR] = 0xb8,
  360. [PWRAP_SIG_ADR] = 0xbc,
  361. [PWRAP_SIG_MODE] = 0xc0,
  362. [PWRAP_SIG_VALUE] = 0xc4,
  363. [PWRAP_SIG_ERRVAL] = 0xc8,
  364. [PWRAP_CRC_EN] = 0xcc,
  365. [PWRAP_TIMER_EN] = 0xd0,
  366. [PWRAP_TIMER_STA] = 0xd4,
  367. [PWRAP_WDT_UNIT] = 0xd8,
  368. [PWRAP_WDT_SRC_EN] = 0xdc,
  369. [PWRAP_WDT_FLG] = 0xe0,
  370. [PWRAP_DEBUG_INT_SEL] = 0xe4,
  371. [PWRAP_DVFS_ADR0] = 0xe8,
  372. [PWRAP_DVFS_WDATA0] = 0xec,
  373. [PWRAP_DVFS_ADR1] = 0xf0,
  374. [PWRAP_DVFS_WDATA1] = 0xf4,
  375. [PWRAP_DVFS_ADR2] = 0xf8,
  376. [PWRAP_DVFS_WDATA2] = 0xfc,
  377. [PWRAP_DVFS_ADR3] = 0x100,
  378. [PWRAP_DVFS_WDATA3] = 0x104,
  379. [PWRAP_DVFS_ADR4] = 0x108,
  380. [PWRAP_DVFS_WDATA4] = 0x10c,
  381. [PWRAP_DVFS_ADR5] = 0x110,
  382. [PWRAP_DVFS_WDATA5] = 0x114,
  383. [PWRAP_DVFS_ADR6] = 0x118,
  384. [PWRAP_DVFS_WDATA6] = 0x11c,
  385. [PWRAP_DVFS_ADR7] = 0x120,
  386. [PWRAP_DVFS_WDATA7] = 0x124,
  387. [PWRAP_SPMINF_STA] = 0x128,
  388. [PWRAP_CIPHER_KEY_SEL] = 0x12c,
  389. [PWRAP_CIPHER_IV_SEL] = 0x130,
  390. [PWRAP_CIPHER_EN] = 0x134,
  391. [PWRAP_CIPHER_RDY] = 0x138,
  392. [PWRAP_CIPHER_MODE] = 0x13c,
  393. [PWRAP_CIPHER_SWRST] = 0x140,
  394. [PWRAP_DCM_EN] = 0x144,
  395. [PWRAP_DCM_DBC_PRD] = 0x148,
  396. };
  397. static int mt8135_regs[] = {
  398. [PWRAP_MUX_SEL] = 0x0,
  399. [PWRAP_WRAP_EN] = 0x4,
  400. [PWRAP_DIO_EN] = 0x8,
  401. [PWRAP_SIDLY] = 0xc,
  402. [PWRAP_CSHEXT] = 0x10,
  403. [PWRAP_CSHEXT_WRITE] = 0x14,
  404. [PWRAP_CSHEXT_READ] = 0x18,
  405. [PWRAP_CSLEXT_START] = 0x1c,
  406. [PWRAP_CSLEXT_END] = 0x20,
  407. [PWRAP_STAUPD_PRD] = 0x24,
  408. [PWRAP_STAUPD_GRPEN] = 0x28,
  409. [PWRAP_STAUPD_MAN_TRIG] = 0x2c,
  410. [PWRAP_STAUPD_STA] = 0x30,
  411. [PWRAP_EVENT_IN_EN] = 0x34,
  412. [PWRAP_EVENT_DST_EN] = 0x38,
  413. [PWRAP_WRAP_STA] = 0x3c,
  414. [PWRAP_RRARB_INIT] = 0x40,
  415. [PWRAP_RRARB_EN] = 0x44,
  416. [PWRAP_RRARB_STA0] = 0x48,
  417. [PWRAP_RRARB_STA1] = 0x4c,
  418. [PWRAP_HARB_INIT] = 0x50,
  419. [PWRAP_HARB_HPRIO] = 0x54,
  420. [PWRAP_HIPRIO_ARB_EN] = 0x58,
  421. [PWRAP_HARB_STA0] = 0x5c,
  422. [PWRAP_HARB_STA1] = 0x60,
  423. [PWRAP_MAN_EN] = 0x64,
  424. [PWRAP_MAN_CMD] = 0x68,
  425. [PWRAP_MAN_RDATA] = 0x6c,
  426. [PWRAP_MAN_VLDCLR] = 0x70,
  427. [PWRAP_WACS0_EN] = 0x74,
  428. [PWRAP_INIT_DONE0] = 0x78,
  429. [PWRAP_WACS0_CMD] = 0x7c,
  430. [PWRAP_WACS0_RDATA] = 0x80,
  431. [PWRAP_WACS0_VLDCLR] = 0x84,
  432. [PWRAP_WACS1_EN] = 0x88,
  433. [PWRAP_INIT_DONE1] = 0x8c,
  434. [PWRAP_WACS1_CMD] = 0x90,
  435. [PWRAP_WACS1_RDATA] = 0x94,
  436. [PWRAP_WACS1_VLDCLR] = 0x98,
  437. [PWRAP_WACS2_EN] = 0x9c,
  438. [PWRAP_INIT_DONE2] = 0xa0,
  439. [PWRAP_WACS2_CMD] = 0xa4,
  440. [PWRAP_WACS2_RDATA] = 0xa8,
  441. [PWRAP_WACS2_VLDCLR] = 0xac,
  442. [PWRAP_INT_EN] = 0xb0,
  443. [PWRAP_INT_FLG_RAW] = 0xb4,
  444. [PWRAP_INT_FLG] = 0xb8,
  445. [PWRAP_INT_CLR] = 0xbc,
  446. [PWRAP_SIG_ADR] = 0xc0,
  447. [PWRAP_SIG_MODE] = 0xc4,
  448. [PWRAP_SIG_VALUE] = 0xc8,
  449. [PWRAP_SIG_ERRVAL] = 0xcc,
  450. [PWRAP_CRC_EN] = 0xd0,
  451. [PWRAP_EVENT_STA] = 0xd4,
  452. [PWRAP_EVENT_STACLR] = 0xd8,
  453. [PWRAP_TIMER_EN] = 0xdc,
  454. [PWRAP_TIMER_STA] = 0xe0,
  455. [PWRAP_WDT_UNIT] = 0xe4,
  456. [PWRAP_WDT_SRC_EN] = 0xe8,
  457. [PWRAP_WDT_FLG] = 0xec,
  458. [PWRAP_DEBUG_INT_SEL] = 0xf0,
  459. [PWRAP_CIPHER_KEY_SEL] = 0x134,
  460. [PWRAP_CIPHER_IV_SEL] = 0x138,
  461. [PWRAP_CIPHER_LOAD] = 0x13c,
  462. [PWRAP_CIPHER_START] = 0x140,
  463. [PWRAP_CIPHER_RDY] = 0x144,
  464. [PWRAP_CIPHER_MODE] = 0x148,
  465. [PWRAP_CIPHER_SWRST] = 0x14c,
  466. [PWRAP_DCM_EN] = 0x15c,
  467. [PWRAP_DCM_DBC_PRD] = 0x160,
  468. };
  469. enum pmic_type {
  470. PMIC_MT6323,
  471. PMIC_MT6397,
  472. };
  473. enum pwrap_type {
  474. PWRAP_MT2701,
  475. PWRAP_MT8135,
  476. PWRAP_MT8173,
  477. };
  478. struct pwrap_slv_type {
  479. const u32 *dew_regs;
  480. enum pmic_type type;
  481. };
  482. struct pmic_wrapper {
  483. struct device *dev;
  484. void __iomem *base;
  485. struct regmap *regmap;
  486. const struct pmic_wrapper_type *master;
  487. const struct pwrap_slv_type *slave;
  488. struct clk *clk_spi;
  489. struct clk *clk_wrap;
  490. struct reset_control *rstc;
  491. struct reset_control *rstc_bridge;
  492. void __iomem *bridge_base;
  493. };
  494. struct pmic_wrapper_type {
  495. int *regs;
  496. enum pwrap_type type;
  497. u32 arb_en_all;
  498. u32 int_en_all;
  499. u32 spi_w;
  500. u32 wdt_src;
  501. unsigned int has_bridge:1;
  502. int (*init_reg_clock)(struct pmic_wrapper *wrp);
  503. int (*init_soc_specific)(struct pmic_wrapper *wrp);
  504. };
  505. static u32 pwrap_readl(struct pmic_wrapper *wrp, enum pwrap_regs reg)
  506. {
  507. return readl(wrp->base + wrp->master->regs[reg]);
  508. }
  509. static void pwrap_writel(struct pmic_wrapper *wrp, u32 val, enum pwrap_regs reg)
  510. {
  511. writel(val, wrp->base + wrp->master->regs[reg]);
  512. }
  513. static bool pwrap_is_fsm_idle(struct pmic_wrapper *wrp)
  514. {
  515. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  516. return PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_IDLE;
  517. }
  518. static bool pwrap_is_fsm_vldclr(struct pmic_wrapper *wrp)
  519. {
  520. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  521. return PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_WFVLDCLR;
  522. }
  523. /*
  524. * Timeout issue sometimes caused by the last read command
  525. * failed because pmic wrap could not got the FSM_VLDCLR
  526. * in time after finishing WACS2_CMD. It made state machine
  527. * still on FSM_VLDCLR and timeout next time.
  528. * Check the status of FSM and clear the vldclr to recovery the
  529. * error.
  530. */
  531. static inline void pwrap_leave_fsm_vldclr(struct pmic_wrapper *wrp)
  532. {
  533. if (pwrap_is_fsm_vldclr(wrp))
  534. pwrap_writel(wrp, 1, PWRAP_WACS2_VLDCLR);
  535. }
  536. static bool pwrap_is_sync_idle(struct pmic_wrapper *wrp)
  537. {
  538. return pwrap_readl(wrp, PWRAP_WACS2_RDATA) & PWRAP_STATE_SYNC_IDLE0;
  539. }
  540. static bool pwrap_is_fsm_idle_and_sync_idle(struct pmic_wrapper *wrp)
  541. {
  542. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  543. return (PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_IDLE) &&
  544. (val & PWRAP_STATE_SYNC_IDLE0);
  545. }
  546. static int pwrap_wait_for_state(struct pmic_wrapper *wrp,
  547. bool (*fp)(struct pmic_wrapper *))
  548. {
  549. unsigned long timeout;
  550. timeout = jiffies + usecs_to_jiffies(10000);
  551. do {
  552. if (time_after(jiffies, timeout))
  553. return fp(wrp) ? 0 : -ETIMEDOUT;
  554. if (fp(wrp))
  555. return 0;
  556. } while (1);
  557. }
  558. static int pwrap_write(struct pmic_wrapper *wrp, u32 adr, u32 wdata)
  559. {
  560. int ret;
  561. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle);
  562. if (ret) {
  563. pwrap_leave_fsm_vldclr(wrp);
  564. return ret;
  565. }
  566. pwrap_writel(wrp, (1 << 31) | ((adr >> 1) << 16) | wdata,
  567. PWRAP_WACS2_CMD);
  568. return 0;
  569. }
  570. static int pwrap_read(struct pmic_wrapper *wrp, u32 adr, u32 *rdata)
  571. {
  572. int ret;
  573. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle);
  574. if (ret) {
  575. pwrap_leave_fsm_vldclr(wrp);
  576. return ret;
  577. }
  578. pwrap_writel(wrp, (adr >> 1) << 16, PWRAP_WACS2_CMD);
  579. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_vldclr);
  580. if (ret)
  581. return ret;
  582. *rdata = PWRAP_GET_WACS_RDATA(pwrap_readl(wrp, PWRAP_WACS2_RDATA));
  583. pwrap_writel(wrp, 1, PWRAP_WACS2_VLDCLR);
  584. return 0;
  585. }
  586. static int pwrap_regmap_read(void *context, u32 adr, u32 *rdata)
  587. {
  588. return pwrap_read(context, adr, rdata);
  589. }
  590. static int pwrap_regmap_write(void *context, u32 adr, u32 wdata)
  591. {
  592. return pwrap_write(context, adr, wdata);
  593. }
  594. static int pwrap_reset_spislave(struct pmic_wrapper *wrp)
  595. {
  596. int ret, i;
  597. pwrap_writel(wrp, 0, PWRAP_HIPRIO_ARB_EN);
  598. pwrap_writel(wrp, 0, PWRAP_WRAP_EN);
  599. pwrap_writel(wrp, 1, PWRAP_MUX_SEL);
  600. pwrap_writel(wrp, 1, PWRAP_MAN_EN);
  601. pwrap_writel(wrp, 0, PWRAP_DIO_EN);
  602. pwrap_writel(wrp, wrp->master->spi_w | PWRAP_MAN_CMD_OP_CSL,
  603. PWRAP_MAN_CMD);
  604. pwrap_writel(wrp, wrp->master->spi_w | PWRAP_MAN_CMD_OP_OUTS,
  605. PWRAP_MAN_CMD);
  606. pwrap_writel(wrp, wrp->master->spi_w | PWRAP_MAN_CMD_OP_CSH,
  607. PWRAP_MAN_CMD);
  608. for (i = 0; i < 4; i++)
  609. pwrap_writel(wrp, wrp->master->spi_w | PWRAP_MAN_CMD_OP_OUTS,
  610. PWRAP_MAN_CMD);
  611. ret = pwrap_wait_for_state(wrp, pwrap_is_sync_idle);
  612. if (ret) {
  613. dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
  614. return ret;
  615. }
  616. pwrap_writel(wrp, 0, PWRAP_MAN_EN);
  617. pwrap_writel(wrp, 0, PWRAP_MUX_SEL);
  618. return 0;
  619. }
  620. /*
  621. * pwrap_init_sidly - configure serial input delay
  622. *
  623. * This configures the serial input delay. We can configure 0, 2, 4 or 6ns
  624. * delay. Do a read test with all possible values and chose the best delay.
  625. */
  626. static int pwrap_init_sidly(struct pmic_wrapper *wrp)
  627. {
  628. u32 rdata;
  629. u32 i;
  630. u32 pass = 0;
  631. signed char dly[16] = {
  632. -1, 0, 1, 0, 2, -1, 1, 1, 3, -1, -1, -1, 3, -1, 2, 1
  633. };
  634. for (i = 0; i < 4; i++) {
  635. pwrap_writel(wrp, i, PWRAP_SIDLY);
  636. pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_READ_TEST],
  637. &rdata);
  638. if (rdata == PWRAP_DEW_READ_TEST_VAL) {
  639. dev_dbg(wrp->dev, "[Read Test] pass, SIDLY=%x\n", i);
  640. pass |= 1 << i;
  641. }
  642. }
  643. if (dly[pass] < 0) {
  644. dev_err(wrp->dev, "sidly pass range 0x%x not continuous\n",
  645. pass);
  646. return -EIO;
  647. }
  648. pwrap_writel(wrp, dly[pass], PWRAP_SIDLY);
  649. return 0;
  650. }
  651. static int pwrap_mt8135_init_reg_clock(struct pmic_wrapper *wrp)
  652. {
  653. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT);
  654. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_WRITE);
  655. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT_READ);
  656. pwrap_writel(wrp, 0x0, PWRAP_CSLEXT_START);
  657. pwrap_writel(wrp, 0x0, PWRAP_CSLEXT_END);
  658. return 0;
  659. }
  660. static int pwrap_mt8173_init_reg_clock(struct pmic_wrapper *wrp)
  661. {
  662. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_WRITE);
  663. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT_READ);
  664. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_START);
  665. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_END);
  666. return 0;
  667. }
  668. static int pwrap_mt2701_init_reg_clock(struct pmic_wrapper *wrp)
  669. {
  670. switch (wrp->slave->type) {
  671. case PMIC_MT6397:
  672. pwrap_writel(wrp, 0xc, PWRAP_RDDMY);
  673. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT_WRITE);
  674. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_READ);
  675. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_START);
  676. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_END);
  677. break;
  678. case PMIC_MT6323:
  679. pwrap_writel(wrp, 0x8, PWRAP_RDDMY);
  680. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_RDDMY_NO],
  681. 0x8);
  682. pwrap_writel(wrp, 0x5, PWRAP_CSHEXT_WRITE);
  683. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_READ);
  684. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_START);
  685. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_END);
  686. break;
  687. }
  688. return 0;
  689. }
  690. static bool pwrap_is_cipher_ready(struct pmic_wrapper *wrp)
  691. {
  692. return pwrap_readl(wrp, PWRAP_CIPHER_RDY) & 1;
  693. }
  694. static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
  695. {
  696. u32 rdata;
  697. int ret;
  698. ret = pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_RDY],
  699. &rdata);
  700. if (ret)
  701. return 0;
  702. return rdata == 1;
  703. }
  704. static int pwrap_init_cipher(struct pmic_wrapper *wrp)
  705. {
  706. int ret;
  707. u32 rdata;
  708. pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
  709. pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
  710. pwrap_writel(wrp, 0x1, PWRAP_CIPHER_KEY_SEL);
  711. pwrap_writel(wrp, 0x2, PWRAP_CIPHER_IV_SEL);
  712. switch (wrp->master->type) {
  713. case PWRAP_MT8135:
  714. pwrap_writel(wrp, 1, PWRAP_CIPHER_LOAD);
  715. pwrap_writel(wrp, 1, PWRAP_CIPHER_START);
  716. break;
  717. case PWRAP_MT2701:
  718. case PWRAP_MT8173:
  719. pwrap_writel(wrp, 1, PWRAP_CIPHER_EN);
  720. break;
  721. }
  722. /* Config cipher mode @PMIC */
  723. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_SWRST], 0x1);
  724. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_SWRST], 0x0);
  725. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_KEY_SEL], 0x1);
  726. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_IV_SEL], 0x2);
  727. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_LOAD], 0x1);
  728. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_START], 0x1);
  729. switch (wrp->slave->type) {
  730. case PMIC_MT6397:
  731. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_LOAD],
  732. 0x1);
  733. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_START],
  734. 0x1);
  735. break;
  736. case PMIC_MT6323:
  737. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_EN],
  738. 0x1);
  739. break;
  740. }
  741. /* wait for cipher data ready@AP */
  742. ret = pwrap_wait_for_state(wrp, pwrap_is_cipher_ready);
  743. if (ret) {
  744. dev_err(wrp->dev, "cipher data ready@AP fail, ret=%d\n", ret);
  745. return ret;
  746. }
  747. /* wait for cipher data ready@PMIC */
  748. ret = pwrap_wait_for_state(wrp, pwrap_is_pmic_cipher_ready);
  749. if (ret) {
  750. dev_err(wrp->dev, "timeout waiting for cipher data ready@PMIC\n");
  751. return ret;
  752. }
  753. /* wait for cipher mode idle */
  754. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CIPHER_MODE], 0x1);
  755. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle_and_sync_idle);
  756. if (ret) {
  757. dev_err(wrp->dev, "cipher mode idle fail, ret=%d\n", ret);
  758. return ret;
  759. }
  760. pwrap_writel(wrp, 1, PWRAP_CIPHER_MODE);
  761. /* Write Test */
  762. if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_WRITE_TEST],
  763. PWRAP_DEW_WRITE_TEST_VAL) ||
  764. pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_WRITE_TEST],
  765. &rdata) ||
  766. (rdata != PWRAP_DEW_WRITE_TEST_VAL)) {
  767. dev_err(wrp->dev, "rdata=0x%04X\n", rdata);
  768. return -EFAULT;
  769. }
  770. return 0;
  771. }
  772. static int pwrap_mt8135_init_soc_specific(struct pmic_wrapper *wrp)
  773. {
  774. /* enable pwrap events and pwrap bridge in AP side */
  775. pwrap_writel(wrp, 0x1, PWRAP_EVENT_IN_EN);
  776. pwrap_writel(wrp, 0xffff, PWRAP_EVENT_DST_EN);
  777. writel(0x7f, wrp->bridge_base + PWRAP_MT8135_BRIDGE_IORD_ARB_EN);
  778. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WACS3_EN);
  779. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WACS4_EN);
  780. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WDT_UNIT);
  781. writel(0xffff, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WDT_SRC_EN);
  782. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_TIMER_EN);
  783. writel(0x7ff, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INT_EN);
  784. /* enable PMIC event out and sources */
  785. if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_EVENT_OUT_EN],
  786. 0x1) ||
  787. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_EVENT_SRC_EN],
  788. 0xffff)) {
  789. dev_err(wrp->dev, "enable dewrap fail\n");
  790. return -EFAULT;
  791. }
  792. return 0;
  793. }
  794. static int pwrap_mt8173_init_soc_specific(struct pmic_wrapper *wrp)
  795. {
  796. /* PMIC_DEWRAP enables */
  797. if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_EVENT_OUT_EN],
  798. 0x1) ||
  799. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_EVENT_SRC_EN],
  800. 0xffff)) {
  801. dev_err(wrp->dev, "enable dewrap fail\n");
  802. return -EFAULT;
  803. }
  804. return 0;
  805. }
  806. static int pwrap_mt2701_init_soc_specific(struct pmic_wrapper *wrp)
  807. {
  808. /* GPS_INTF initialization */
  809. switch (wrp->slave->type) {
  810. case PMIC_MT6323:
  811. pwrap_writel(wrp, 0x076c, PWRAP_ADC_CMD_ADDR);
  812. pwrap_writel(wrp, 0x8000, PWRAP_PWRAP_ADC_CMD);
  813. pwrap_writel(wrp, 0x072c, PWRAP_ADC_RDY_ADDR);
  814. pwrap_writel(wrp, 0x072e, PWRAP_ADC_RDATA_ADDR1);
  815. pwrap_writel(wrp, 0x0730, PWRAP_ADC_RDATA_ADDR2);
  816. break;
  817. default:
  818. break;
  819. }
  820. return 0;
  821. }
  822. static int pwrap_init(struct pmic_wrapper *wrp)
  823. {
  824. int ret;
  825. u32 rdata;
  826. reset_control_reset(wrp->rstc);
  827. if (wrp->rstc_bridge)
  828. reset_control_reset(wrp->rstc_bridge);
  829. if (wrp->master->type == PWRAP_MT8173) {
  830. /* Enable DCM */
  831. pwrap_writel(wrp, 3, PWRAP_DCM_EN);
  832. pwrap_writel(wrp, 0, PWRAP_DCM_DBC_PRD);
  833. }
  834. /* Reset SPI slave */
  835. ret = pwrap_reset_spislave(wrp);
  836. if (ret)
  837. return ret;
  838. pwrap_writel(wrp, 1, PWRAP_WRAP_EN);
  839. pwrap_writel(wrp, wrp->master->arb_en_all, PWRAP_HIPRIO_ARB_EN);
  840. pwrap_writel(wrp, 1, PWRAP_WACS2_EN);
  841. ret = wrp->master->init_reg_clock(wrp);
  842. if (ret)
  843. return ret;
  844. /* Setup serial input delay */
  845. ret = pwrap_init_sidly(wrp);
  846. if (ret)
  847. return ret;
  848. /* Enable dual IO mode */
  849. pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_DIO_EN], 1);
  850. /* Check IDLE & INIT_DONE in advance */
  851. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle_and_sync_idle);
  852. if (ret) {
  853. dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
  854. return ret;
  855. }
  856. pwrap_writel(wrp, 1, PWRAP_DIO_EN);
  857. /* Read Test */
  858. pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_READ_TEST], &rdata);
  859. if (rdata != PWRAP_DEW_READ_TEST_VAL) {
  860. dev_err(wrp->dev, "Read test failed after switch to DIO mode: 0x%04x != 0x%04x\n",
  861. PWRAP_DEW_READ_TEST_VAL, rdata);
  862. return -EFAULT;
  863. }
  864. /* Enable encryption */
  865. ret = pwrap_init_cipher(wrp);
  866. if (ret)
  867. return ret;
  868. /* Signature checking - using CRC */
  869. if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CRC_EN], 0x1))
  870. return -EFAULT;
  871. pwrap_writel(wrp, 0x1, PWRAP_CRC_EN);
  872. pwrap_writel(wrp, 0x0, PWRAP_SIG_MODE);
  873. pwrap_writel(wrp, wrp->slave->dew_regs[PWRAP_DEW_CRC_VAL],
  874. PWRAP_SIG_ADR);
  875. pwrap_writel(wrp, wrp->master->arb_en_all, PWRAP_HIPRIO_ARB_EN);
  876. if (wrp->master->type == PWRAP_MT8135)
  877. pwrap_writel(wrp, 0x7, PWRAP_RRARB_EN);
  878. pwrap_writel(wrp, 0x1, PWRAP_WACS0_EN);
  879. pwrap_writel(wrp, 0x1, PWRAP_WACS1_EN);
  880. pwrap_writel(wrp, 0x1, PWRAP_WACS2_EN);
  881. pwrap_writel(wrp, 0x5, PWRAP_STAUPD_PRD);
  882. pwrap_writel(wrp, 0xff, PWRAP_STAUPD_GRPEN);
  883. if (wrp->master->init_soc_specific) {
  884. ret = wrp->master->init_soc_specific(wrp);
  885. if (ret)
  886. return ret;
  887. }
  888. /* Setup the init done registers */
  889. pwrap_writel(wrp, 1, PWRAP_INIT_DONE2);
  890. pwrap_writel(wrp, 1, PWRAP_INIT_DONE0);
  891. pwrap_writel(wrp, 1, PWRAP_INIT_DONE1);
  892. if (wrp->master->has_bridge) {
  893. writel(1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INIT_DONE3);
  894. writel(1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INIT_DONE4);
  895. }
  896. return 0;
  897. }
  898. static irqreturn_t pwrap_interrupt(int irqno, void *dev_id)
  899. {
  900. u32 rdata;
  901. struct pmic_wrapper *wrp = dev_id;
  902. rdata = pwrap_readl(wrp, PWRAP_INT_FLG);
  903. dev_err(wrp->dev, "unexpected interrupt int=0x%x\n", rdata);
  904. pwrap_writel(wrp, 0xffffffff, PWRAP_INT_CLR);
  905. return IRQ_HANDLED;
  906. }
  907. static const struct regmap_config pwrap_regmap_config = {
  908. .reg_bits = 16,
  909. .val_bits = 16,
  910. .reg_stride = 2,
  911. .reg_read = pwrap_regmap_read,
  912. .reg_write = pwrap_regmap_write,
  913. .max_register = 0xffff,
  914. };
  915. static const struct pwrap_slv_type pmic_mt6323 = {
  916. .dew_regs = mt6323_regs,
  917. .type = PMIC_MT6323,
  918. };
  919. static const struct pwrap_slv_type pmic_mt6397 = {
  920. .dew_regs = mt6397_regs,
  921. .type = PMIC_MT6397,
  922. };
  923. static const struct of_device_id of_slave_match_tbl[] = {
  924. {
  925. .compatible = "mediatek,mt6323",
  926. .data = &pmic_mt6323,
  927. }, {
  928. .compatible = "mediatek,mt6397",
  929. .data = &pmic_mt6397,
  930. }, {
  931. /* sentinel */
  932. }
  933. };
  934. MODULE_DEVICE_TABLE(of, of_slave_match_tbl);
  935. static const struct pmic_wrapper_type pwrap_mt2701 = {
  936. .regs = mt2701_regs,
  937. .type = PWRAP_MT2701,
  938. .arb_en_all = 0x3f,
  939. .int_en_all = ~(u32)(BIT(31) | BIT(2)),
  940. .spi_w = PWRAP_MAN_CMD_SPI_WRITE_NEW,
  941. .wdt_src = PWRAP_WDT_SRC_MASK_ALL,
  942. .has_bridge = 0,
  943. .init_reg_clock = pwrap_mt2701_init_reg_clock,
  944. .init_soc_specific = pwrap_mt2701_init_soc_specific,
  945. };
  946. static struct pmic_wrapper_type pwrap_mt8135 = {
  947. .regs = mt8135_regs,
  948. .type = PWRAP_MT8135,
  949. .arb_en_all = 0x1ff,
  950. .int_en_all = ~(u32)(BIT(31) | BIT(1)),
  951. .spi_w = PWRAP_MAN_CMD_SPI_WRITE,
  952. .wdt_src = PWRAP_WDT_SRC_MASK_ALL,
  953. .has_bridge = 1,
  954. .init_reg_clock = pwrap_mt8135_init_reg_clock,
  955. .init_soc_specific = pwrap_mt8135_init_soc_specific,
  956. };
  957. static struct pmic_wrapper_type pwrap_mt8173 = {
  958. .regs = mt8173_regs,
  959. .type = PWRAP_MT8173,
  960. .arb_en_all = 0x3f,
  961. .int_en_all = ~(u32)(BIT(31) | BIT(1)),
  962. .spi_w = PWRAP_MAN_CMD_SPI_WRITE,
  963. .wdt_src = PWRAP_WDT_SRC_MASK_NO_STAUPD,
  964. .has_bridge = 0,
  965. .init_reg_clock = pwrap_mt8173_init_reg_clock,
  966. .init_soc_specific = pwrap_mt8173_init_soc_specific,
  967. };
  968. static struct of_device_id of_pwrap_match_tbl[] = {
  969. {
  970. .compatible = "mediatek,mt2701-pwrap",
  971. .data = &pwrap_mt2701,
  972. }, {
  973. .compatible = "mediatek,mt8135-pwrap",
  974. .data = &pwrap_mt8135,
  975. }, {
  976. .compatible = "mediatek,mt8173-pwrap",
  977. .data = &pwrap_mt8173,
  978. }, {
  979. /* sentinel */
  980. }
  981. };
  982. MODULE_DEVICE_TABLE(of, of_pwrap_match_tbl);
  983. static int pwrap_probe(struct platform_device *pdev)
  984. {
  985. int ret, irq;
  986. struct pmic_wrapper *wrp;
  987. struct device_node *np = pdev->dev.of_node;
  988. const struct of_device_id *of_id =
  989. of_match_device(of_pwrap_match_tbl, &pdev->dev);
  990. const struct of_device_id *of_slave_id = NULL;
  991. struct resource *res;
  992. if (pdev->dev.of_node->child)
  993. of_slave_id = of_match_node(of_slave_match_tbl,
  994. pdev->dev.of_node->child);
  995. if (!of_slave_id) {
  996. dev_dbg(&pdev->dev, "slave pmic should be defined in dts\n");
  997. return -EINVAL;
  998. }
  999. wrp = devm_kzalloc(&pdev->dev, sizeof(*wrp), GFP_KERNEL);
  1000. if (!wrp)
  1001. return -ENOMEM;
  1002. platform_set_drvdata(pdev, wrp);
  1003. wrp->master = of_id->data;
  1004. wrp->slave = of_slave_id->data;
  1005. wrp->dev = &pdev->dev;
  1006. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwrap");
  1007. wrp->base = devm_ioremap_resource(wrp->dev, res);
  1008. if (IS_ERR(wrp->base))
  1009. return PTR_ERR(wrp->base);
  1010. wrp->rstc = devm_reset_control_get(wrp->dev, "pwrap");
  1011. if (IS_ERR(wrp->rstc)) {
  1012. ret = PTR_ERR(wrp->rstc);
  1013. dev_dbg(wrp->dev, "cannot get pwrap reset: %d\n", ret);
  1014. return ret;
  1015. }
  1016. if (wrp->master->has_bridge) {
  1017. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1018. "pwrap-bridge");
  1019. wrp->bridge_base = devm_ioremap_resource(wrp->dev, res);
  1020. if (IS_ERR(wrp->bridge_base))
  1021. return PTR_ERR(wrp->bridge_base);
  1022. wrp->rstc_bridge = devm_reset_control_get(wrp->dev, "pwrap-bridge");
  1023. if (IS_ERR(wrp->rstc_bridge)) {
  1024. ret = PTR_ERR(wrp->rstc_bridge);
  1025. dev_dbg(wrp->dev, "cannot get pwrap-bridge reset: %d\n", ret);
  1026. return ret;
  1027. }
  1028. }
  1029. wrp->clk_spi = devm_clk_get(wrp->dev, "spi");
  1030. if (IS_ERR(wrp->clk_spi)) {
  1031. dev_dbg(wrp->dev, "failed to get clock: %ld\n", PTR_ERR(wrp->clk_spi));
  1032. return PTR_ERR(wrp->clk_spi);
  1033. }
  1034. wrp->clk_wrap = devm_clk_get(wrp->dev, "wrap");
  1035. if (IS_ERR(wrp->clk_wrap)) {
  1036. dev_dbg(wrp->dev, "failed to get clock: %ld\n", PTR_ERR(wrp->clk_wrap));
  1037. return PTR_ERR(wrp->clk_wrap);
  1038. }
  1039. ret = clk_prepare_enable(wrp->clk_spi);
  1040. if (ret)
  1041. return ret;
  1042. ret = clk_prepare_enable(wrp->clk_wrap);
  1043. if (ret)
  1044. goto err_out1;
  1045. /* Enable internal dynamic clock */
  1046. pwrap_writel(wrp, 1, PWRAP_DCM_EN);
  1047. pwrap_writel(wrp, 0, PWRAP_DCM_DBC_PRD);
  1048. /*
  1049. * The PMIC could already be initialized by the bootloader.
  1050. * Skip initialization here in this case.
  1051. */
  1052. if (!pwrap_readl(wrp, PWRAP_INIT_DONE2)) {
  1053. ret = pwrap_init(wrp);
  1054. if (ret) {
  1055. dev_dbg(wrp->dev, "init failed with %d\n", ret);
  1056. goto err_out2;
  1057. }
  1058. }
  1059. if (!(pwrap_readl(wrp, PWRAP_WACS2_RDATA) & PWRAP_STATE_INIT_DONE0)) {
  1060. dev_dbg(wrp->dev, "initialization isn't finished\n");
  1061. return -ENODEV;
  1062. }
  1063. /* Initialize watchdog, may not be done by the bootloader */
  1064. pwrap_writel(wrp, 0xf, PWRAP_WDT_UNIT);
  1065. /*
  1066. * Since STAUPD was not used on mt8173 platform,
  1067. * so STAUPD of WDT_SRC which should be turned off
  1068. */
  1069. pwrap_writel(wrp, wrp->master->wdt_src, PWRAP_WDT_SRC_EN);
  1070. pwrap_writel(wrp, 0x1, PWRAP_TIMER_EN);
  1071. pwrap_writel(wrp, wrp->master->int_en_all, PWRAP_INT_EN);
  1072. irq = platform_get_irq(pdev, 0);
  1073. ret = devm_request_irq(wrp->dev, irq, pwrap_interrupt, IRQF_TRIGGER_HIGH,
  1074. "mt-pmic-pwrap", wrp);
  1075. if (ret)
  1076. goto err_out2;
  1077. wrp->regmap = devm_regmap_init(wrp->dev, NULL, wrp, &pwrap_regmap_config);
  1078. if (IS_ERR(wrp->regmap))
  1079. return PTR_ERR(wrp->regmap);
  1080. ret = of_platform_populate(np, NULL, NULL, wrp->dev);
  1081. if (ret) {
  1082. dev_dbg(wrp->dev, "failed to create child devices at %s\n",
  1083. np->full_name);
  1084. goto err_out2;
  1085. }
  1086. return 0;
  1087. err_out2:
  1088. clk_disable_unprepare(wrp->clk_wrap);
  1089. err_out1:
  1090. clk_disable_unprepare(wrp->clk_spi);
  1091. return ret;
  1092. }
  1093. static struct platform_driver pwrap_drv = {
  1094. .driver = {
  1095. .name = "mt-pmic-pwrap",
  1096. .of_match_table = of_match_ptr(of_pwrap_match_tbl),
  1097. },
  1098. .probe = pwrap_probe,
  1099. };
  1100. module_platform_driver(pwrap_drv);
  1101. MODULE_AUTHOR("Flora Fu, MediaTek");
  1102. MODULE_DESCRIPTION("MediaTek MT8135 PMIC Wrapper Driver");
  1103. MODULE_LICENSE("GPL v2");