mtk-pmic-wrap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 (1 << 13)
  51. #define PWRAP_MAN_CMD_OP_CSH (0x0 << 8)
  52. #define PWRAP_MAN_CMD_OP_CSL (0x1 << 8)
  53. #define PWRAP_MAN_CMD_OP_CK (0x2 << 8)
  54. #define PWRAP_MAN_CMD_OP_OUTS (0x8 << 8)
  55. #define PWRAP_MAN_CMD_OP_OUTD (0x9 << 8)
  56. #define PWRAP_MAN_CMD_OP_OUTQ (0xa << 8)
  57. /* macro for slave device wrapper registers */
  58. #define PWRAP_DEW_BASE 0xbc00
  59. #define PWRAP_DEW_EVENT_OUT_EN (PWRAP_DEW_BASE + 0x0)
  60. #define PWRAP_DEW_DIO_EN (PWRAP_DEW_BASE + 0x2)
  61. #define PWRAP_DEW_EVENT_SRC_EN (PWRAP_DEW_BASE + 0x4)
  62. #define PWRAP_DEW_EVENT_SRC (PWRAP_DEW_BASE + 0x6)
  63. #define PWRAP_DEW_EVENT_FLAG (PWRAP_DEW_BASE + 0x8)
  64. #define PWRAP_DEW_READ_TEST (PWRAP_DEW_BASE + 0xa)
  65. #define PWRAP_DEW_WRITE_TEST (PWRAP_DEW_BASE + 0xc)
  66. #define PWRAP_DEW_CRC_EN (PWRAP_DEW_BASE + 0xe)
  67. #define PWRAP_DEW_CRC_VAL (PWRAP_DEW_BASE + 0x10)
  68. #define PWRAP_DEW_MON_GRP_SEL (PWRAP_DEW_BASE + 0x12)
  69. #define PWRAP_DEW_MON_FLAG_SEL (PWRAP_DEW_BASE + 0x14)
  70. #define PWRAP_DEW_EVENT_TEST (PWRAP_DEW_BASE + 0x16)
  71. #define PWRAP_DEW_CIPHER_KEY_SEL (PWRAP_DEW_BASE + 0x18)
  72. #define PWRAP_DEW_CIPHER_IV_SEL (PWRAP_DEW_BASE + 0x1a)
  73. #define PWRAP_DEW_CIPHER_LOAD (PWRAP_DEW_BASE + 0x1c)
  74. #define PWRAP_DEW_CIPHER_START (PWRAP_DEW_BASE + 0x1e)
  75. #define PWRAP_DEW_CIPHER_RDY (PWRAP_DEW_BASE + 0x20)
  76. #define PWRAP_DEW_CIPHER_MODE (PWRAP_DEW_BASE + 0x22)
  77. #define PWRAP_DEW_CIPHER_SWRST (PWRAP_DEW_BASE + 0x24)
  78. #define PWRAP_MT8173_DEW_CIPHER_IV0 (PWRAP_DEW_BASE + 0x26)
  79. #define PWRAP_MT8173_DEW_CIPHER_IV1 (PWRAP_DEW_BASE + 0x28)
  80. #define PWRAP_MT8173_DEW_CIPHER_IV2 (PWRAP_DEW_BASE + 0x2a)
  81. #define PWRAP_MT8173_DEW_CIPHER_IV3 (PWRAP_DEW_BASE + 0x2c)
  82. #define PWRAP_MT8173_DEW_CIPHER_IV4 (PWRAP_DEW_BASE + 0x2e)
  83. #define PWRAP_MT8173_DEW_CIPHER_IV5 (PWRAP_DEW_BASE + 0x30)
  84. enum pwrap_regs {
  85. PWRAP_MUX_SEL,
  86. PWRAP_WRAP_EN,
  87. PWRAP_DIO_EN,
  88. PWRAP_SIDLY,
  89. PWRAP_CSHEXT_WRITE,
  90. PWRAP_CSHEXT_READ,
  91. PWRAP_CSLEXT_START,
  92. PWRAP_CSLEXT_END,
  93. PWRAP_STAUPD_PRD,
  94. PWRAP_STAUPD_GRPEN,
  95. PWRAP_STAUPD_MAN_TRIG,
  96. PWRAP_STAUPD_STA,
  97. PWRAP_WRAP_STA,
  98. PWRAP_HARB_INIT,
  99. PWRAP_HARB_HPRIO,
  100. PWRAP_HIPRIO_ARB_EN,
  101. PWRAP_HARB_STA0,
  102. PWRAP_HARB_STA1,
  103. PWRAP_MAN_EN,
  104. PWRAP_MAN_CMD,
  105. PWRAP_MAN_RDATA,
  106. PWRAP_MAN_VLDCLR,
  107. PWRAP_WACS0_EN,
  108. PWRAP_INIT_DONE0,
  109. PWRAP_WACS0_CMD,
  110. PWRAP_WACS0_RDATA,
  111. PWRAP_WACS0_VLDCLR,
  112. PWRAP_WACS1_EN,
  113. PWRAP_INIT_DONE1,
  114. PWRAP_WACS1_CMD,
  115. PWRAP_WACS1_RDATA,
  116. PWRAP_WACS1_VLDCLR,
  117. PWRAP_WACS2_EN,
  118. PWRAP_INIT_DONE2,
  119. PWRAP_WACS2_CMD,
  120. PWRAP_WACS2_RDATA,
  121. PWRAP_WACS2_VLDCLR,
  122. PWRAP_INT_EN,
  123. PWRAP_INT_FLG_RAW,
  124. PWRAP_INT_FLG,
  125. PWRAP_INT_CLR,
  126. PWRAP_SIG_ADR,
  127. PWRAP_SIG_MODE,
  128. PWRAP_SIG_VALUE,
  129. PWRAP_SIG_ERRVAL,
  130. PWRAP_CRC_EN,
  131. PWRAP_TIMER_EN,
  132. PWRAP_TIMER_STA,
  133. PWRAP_WDT_UNIT,
  134. PWRAP_WDT_SRC_EN,
  135. PWRAP_WDT_FLG,
  136. PWRAP_DEBUG_INT_SEL,
  137. PWRAP_CIPHER_KEY_SEL,
  138. PWRAP_CIPHER_IV_SEL,
  139. PWRAP_CIPHER_RDY,
  140. PWRAP_CIPHER_MODE,
  141. PWRAP_CIPHER_SWRST,
  142. PWRAP_DCM_EN,
  143. PWRAP_DCM_DBC_PRD,
  144. /* MT8135 only regs */
  145. PWRAP_CSHEXT,
  146. PWRAP_EVENT_IN_EN,
  147. PWRAP_EVENT_DST_EN,
  148. PWRAP_RRARB_INIT,
  149. PWRAP_RRARB_EN,
  150. PWRAP_RRARB_STA0,
  151. PWRAP_RRARB_STA1,
  152. PWRAP_EVENT_STA,
  153. PWRAP_EVENT_STACLR,
  154. PWRAP_CIPHER_LOAD,
  155. PWRAP_CIPHER_START,
  156. /* MT8173 only regs */
  157. PWRAP_RDDMY,
  158. PWRAP_SI_CK_CON,
  159. PWRAP_DVFS_ADR0,
  160. PWRAP_DVFS_WDATA0,
  161. PWRAP_DVFS_ADR1,
  162. PWRAP_DVFS_WDATA1,
  163. PWRAP_DVFS_ADR2,
  164. PWRAP_DVFS_WDATA2,
  165. PWRAP_DVFS_ADR3,
  166. PWRAP_DVFS_WDATA3,
  167. PWRAP_DVFS_ADR4,
  168. PWRAP_DVFS_WDATA4,
  169. PWRAP_DVFS_ADR5,
  170. PWRAP_DVFS_WDATA5,
  171. PWRAP_DVFS_ADR6,
  172. PWRAP_DVFS_WDATA6,
  173. PWRAP_DVFS_ADR7,
  174. PWRAP_DVFS_WDATA7,
  175. PWRAP_SPMINF_STA,
  176. PWRAP_CIPHER_EN,
  177. };
  178. static int mt8173_regs[] = {
  179. [PWRAP_MUX_SEL] = 0x0,
  180. [PWRAP_WRAP_EN] = 0x4,
  181. [PWRAP_DIO_EN] = 0x8,
  182. [PWRAP_SIDLY] = 0xc,
  183. [PWRAP_RDDMY] = 0x10,
  184. [PWRAP_SI_CK_CON] = 0x14,
  185. [PWRAP_CSHEXT_WRITE] = 0x18,
  186. [PWRAP_CSHEXT_READ] = 0x1c,
  187. [PWRAP_CSLEXT_START] = 0x20,
  188. [PWRAP_CSLEXT_END] = 0x24,
  189. [PWRAP_STAUPD_PRD] = 0x28,
  190. [PWRAP_STAUPD_GRPEN] = 0x2c,
  191. [PWRAP_STAUPD_MAN_TRIG] = 0x40,
  192. [PWRAP_STAUPD_STA] = 0x44,
  193. [PWRAP_WRAP_STA] = 0x48,
  194. [PWRAP_HARB_INIT] = 0x4c,
  195. [PWRAP_HARB_HPRIO] = 0x50,
  196. [PWRAP_HIPRIO_ARB_EN] = 0x54,
  197. [PWRAP_HARB_STA0] = 0x58,
  198. [PWRAP_HARB_STA1] = 0x5c,
  199. [PWRAP_MAN_EN] = 0x60,
  200. [PWRAP_MAN_CMD] = 0x64,
  201. [PWRAP_MAN_RDATA] = 0x68,
  202. [PWRAP_MAN_VLDCLR] = 0x6c,
  203. [PWRAP_WACS0_EN] = 0x70,
  204. [PWRAP_INIT_DONE0] = 0x74,
  205. [PWRAP_WACS0_CMD] = 0x78,
  206. [PWRAP_WACS0_RDATA] = 0x7c,
  207. [PWRAP_WACS0_VLDCLR] = 0x80,
  208. [PWRAP_WACS1_EN] = 0x84,
  209. [PWRAP_INIT_DONE1] = 0x88,
  210. [PWRAP_WACS1_CMD] = 0x8c,
  211. [PWRAP_WACS1_RDATA] = 0x90,
  212. [PWRAP_WACS1_VLDCLR] = 0x94,
  213. [PWRAP_WACS2_EN] = 0x98,
  214. [PWRAP_INIT_DONE2] = 0x9c,
  215. [PWRAP_WACS2_CMD] = 0xa0,
  216. [PWRAP_WACS2_RDATA] = 0xa4,
  217. [PWRAP_WACS2_VLDCLR] = 0xa8,
  218. [PWRAP_INT_EN] = 0xac,
  219. [PWRAP_INT_FLG_RAW] = 0xb0,
  220. [PWRAP_INT_FLG] = 0xb4,
  221. [PWRAP_INT_CLR] = 0xb8,
  222. [PWRAP_SIG_ADR] = 0xbc,
  223. [PWRAP_SIG_MODE] = 0xc0,
  224. [PWRAP_SIG_VALUE] = 0xc4,
  225. [PWRAP_SIG_ERRVAL] = 0xc8,
  226. [PWRAP_CRC_EN] = 0xcc,
  227. [PWRAP_TIMER_EN] = 0xd0,
  228. [PWRAP_TIMER_STA] = 0xd4,
  229. [PWRAP_WDT_UNIT] = 0xd8,
  230. [PWRAP_WDT_SRC_EN] = 0xdc,
  231. [PWRAP_WDT_FLG] = 0xe0,
  232. [PWRAP_DEBUG_INT_SEL] = 0xe4,
  233. [PWRAP_DVFS_ADR0] = 0xe8,
  234. [PWRAP_DVFS_WDATA0] = 0xec,
  235. [PWRAP_DVFS_ADR1] = 0xf0,
  236. [PWRAP_DVFS_WDATA1] = 0xf4,
  237. [PWRAP_DVFS_ADR2] = 0xf8,
  238. [PWRAP_DVFS_WDATA2] = 0xfc,
  239. [PWRAP_DVFS_ADR3] = 0x100,
  240. [PWRAP_DVFS_WDATA3] = 0x104,
  241. [PWRAP_DVFS_ADR4] = 0x108,
  242. [PWRAP_DVFS_WDATA4] = 0x10c,
  243. [PWRAP_DVFS_ADR5] = 0x110,
  244. [PWRAP_DVFS_WDATA5] = 0x114,
  245. [PWRAP_DVFS_ADR6] = 0x118,
  246. [PWRAP_DVFS_WDATA6] = 0x11c,
  247. [PWRAP_DVFS_ADR7] = 0x120,
  248. [PWRAP_DVFS_WDATA7] = 0x124,
  249. [PWRAP_SPMINF_STA] = 0x128,
  250. [PWRAP_CIPHER_KEY_SEL] = 0x12c,
  251. [PWRAP_CIPHER_IV_SEL] = 0x130,
  252. [PWRAP_CIPHER_EN] = 0x134,
  253. [PWRAP_CIPHER_RDY] = 0x138,
  254. [PWRAP_CIPHER_MODE] = 0x13c,
  255. [PWRAP_CIPHER_SWRST] = 0x140,
  256. [PWRAP_DCM_EN] = 0x144,
  257. [PWRAP_DCM_DBC_PRD] = 0x148,
  258. };
  259. static int mt8135_regs[] = {
  260. [PWRAP_MUX_SEL] = 0x0,
  261. [PWRAP_WRAP_EN] = 0x4,
  262. [PWRAP_DIO_EN] = 0x8,
  263. [PWRAP_SIDLY] = 0xc,
  264. [PWRAP_CSHEXT] = 0x10,
  265. [PWRAP_CSHEXT_WRITE] = 0x14,
  266. [PWRAP_CSHEXT_READ] = 0x18,
  267. [PWRAP_CSLEXT_START] = 0x1c,
  268. [PWRAP_CSLEXT_END] = 0x20,
  269. [PWRAP_STAUPD_PRD] = 0x24,
  270. [PWRAP_STAUPD_GRPEN] = 0x28,
  271. [PWRAP_STAUPD_MAN_TRIG] = 0x2c,
  272. [PWRAP_STAUPD_STA] = 0x30,
  273. [PWRAP_EVENT_IN_EN] = 0x34,
  274. [PWRAP_EVENT_DST_EN] = 0x38,
  275. [PWRAP_WRAP_STA] = 0x3c,
  276. [PWRAP_RRARB_INIT] = 0x40,
  277. [PWRAP_RRARB_EN] = 0x44,
  278. [PWRAP_RRARB_STA0] = 0x48,
  279. [PWRAP_RRARB_STA1] = 0x4c,
  280. [PWRAP_HARB_INIT] = 0x50,
  281. [PWRAP_HARB_HPRIO] = 0x54,
  282. [PWRAP_HIPRIO_ARB_EN] = 0x58,
  283. [PWRAP_HARB_STA0] = 0x5c,
  284. [PWRAP_HARB_STA1] = 0x60,
  285. [PWRAP_MAN_EN] = 0x64,
  286. [PWRAP_MAN_CMD] = 0x68,
  287. [PWRAP_MAN_RDATA] = 0x6c,
  288. [PWRAP_MAN_VLDCLR] = 0x70,
  289. [PWRAP_WACS0_EN] = 0x74,
  290. [PWRAP_INIT_DONE0] = 0x78,
  291. [PWRAP_WACS0_CMD] = 0x7c,
  292. [PWRAP_WACS0_RDATA] = 0x80,
  293. [PWRAP_WACS0_VLDCLR] = 0x84,
  294. [PWRAP_WACS1_EN] = 0x88,
  295. [PWRAP_INIT_DONE1] = 0x8c,
  296. [PWRAP_WACS1_CMD] = 0x90,
  297. [PWRAP_WACS1_RDATA] = 0x94,
  298. [PWRAP_WACS1_VLDCLR] = 0x98,
  299. [PWRAP_WACS2_EN] = 0x9c,
  300. [PWRAP_INIT_DONE2] = 0xa0,
  301. [PWRAP_WACS2_CMD] = 0xa4,
  302. [PWRAP_WACS2_RDATA] = 0xa8,
  303. [PWRAP_WACS2_VLDCLR] = 0xac,
  304. [PWRAP_INT_EN] = 0xb0,
  305. [PWRAP_INT_FLG_RAW] = 0xb4,
  306. [PWRAP_INT_FLG] = 0xb8,
  307. [PWRAP_INT_CLR] = 0xbc,
  308. [PWRAP_SIG_ADR] = 0xc0,
  309. [PWRAP_SIG_MODE] = 0xc4,
  310. [PWRAP_SIG_VALUE] = 0xc8,
  311. [PWRAP_SIG_ERRVAL] = 0xcc,
  312. [PWRAP_CRC_EN] = 0xd0,
  313. [PWRAP_EVENT_STA] = 0xd4,
  314. [PWRAP_EVENT_STACLR] = 0xd8,
  315. [PWRAP_TIMER_EN] = 0xdc,
  316. [PWRAP_TIMER_STA] = 0xe0,
  317. [PWRAP_WDT_UNIT] = 0xe4,
  318. [PWRAP_WDT_SRC_EN] = 0xe8,
  319. [PWRAP_WDT_FLG] = 0xec,
  320. [PWRAP_DEBUG_INT_SEL] = 0xf0,
  321. [PWRAP_CIPHER_KEY_SEL] = 0x134,
  322. [PWRAP_CIPHER_IV_SEL] = 0x138,
  323. [PWRAP_CIPHER_LOAD] = 0x13c,
  324. [PWRAP_CIPHER_START] = 0x140,
  325. [PWRAP_CIPHER_RDY] = 0x144,
  326. [PWRAP_CIPHER_MODE] = 0x148,
  327. [PWRAP_CIPHER_SWRST] = 0x14c,
  328. [PWRAP_DCM_EN] = 0x15c,
  329. [PWRAP_DCM_DBC_PRD] = 0x160,
  330. };
  331. enum pwrap_type {
  332. PWRAP_MT8135,
  333. PWRAP_MT8173,
  334. };
  335. struct pmic_wrapper_type {
  336. int *regs;
  337. enum pwrap_type type;
  338. u32 arb_en_all;
  339. };
  340. static struct pmic_wrapper_type pwrap_mt8135 = {
  341. .regs = mt8135_regs,
  342. .type = PWRAP_MT8135,
  343. .arb_en_all = 0x1ff,
  344. };
  345. static struct pmic_wrapper_type pwrap_mt8173 = {
  346. .regs = mt8173_regs,
  347. .type = PWRAP_MT8173,
  348. .arb_en_all = 0x3f,
  349. };
  350. struct pmic_wrapper {
  351. struct device *dev;
  352. void __iomem *base;
  353. struct regmap *regmap;
  354. int *regs;
  355. enum pwrap_type type;
  356. u32 arb_en_all;
  357. struct clk *clk_spi;
  358. struct clk *clk_wrap;
  359. struct reset_control *rstc;
  360. struct reset_control *rstc_bridge;
  361. void __iomem *bridge_base;
  362. };
  363. static inline int pwrap_is_mt8135(struct pmic_wrapper *wrp)
  364. {
  365. return wrp->type == PWRAP_MT8135;
  366. }
  367. static inline int pwrap_is_mt8173(struct pmic_wrapper *wrp)
  368. {
  369. return wrp->type == PWRAP_MT8173;
  370. }
  371. static u32 pwrap_readl(struct pmic_wrapper *wrp, enum pwrap_regs reg)
  372. {
  373. return readl(wrp->base + wrp->regs[reg]);
  374. }
  375. static void pwrap_writel(struct pmic_wrapper *wrp, u32 val, enum pwrap_regs reg)
  376. {
  377. writel(val, wrp->base + wrp->regs[reg]);
  378. }
  379. static bool pwrap_is_fsm_idle(struct pmic_wrapper *wrp)
  380. {
  381. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  382. return PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_IDLE;
  383. }
  384. static bool pwrap_is_fsm_vldclr(struct pmic_wrapper *wrp)
  385. {
  386. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  387. return PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_WFVLDCLR;
  388. }
  389. static bool pwrap_is_sync_idle(struct pmic_wrapper *wrp)
  390. {
  391. return pwrap_readl(wrp, PWRAP_WACS2_RDATA) & PWRAP_STATE_SYNC_IDLE0;
  392. }
  393. static bool pwrap_is_fsm_idle_and_sync_idle(struct pmic_wrapper *wrp)
  394. {
  395. u32 val = pwrap_readl(wrp, PWRAP_WACS2_RDATA);
  396. return (PWRAP_GET_WACS_FSM(val) == PWRAP_WACS_FSM_IDLE) &&
  397. (val & PWRAP_STATE_SYNC_IDLE0);
  398. }
  399. static int pwrap_wait_for_state(struct pmic_wrapper *wrp,
  400. bool (*fp)(struct pmic_wrapper *))
  401. {
  402. unsigned long timeout;
  403. timeout = jiffies + usecs_to_jiffies(255);
  404. do {
  405. if (time_after(jiffies, timeout))
  406. return fp(wrp) ? 0 : -ETIMEDOUT;
  407. if (fp(wrp))
  408. return 0;
  409. } while (1);
  410. }
  411. static int pwrap_write(struct pmic_wrapper *wrp, u32 adr, u32 wdata)
  412. {
  413. int ret;
  414. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle);
  415. if (ret)
  416. return ret;
  417. pwrap_writel(wrp, (1 << 31) | ((adr >> 1) << 16) | wdata,
  418. PWRAP_WACS2_CMD);
  419. return 0;
  420. }
  421. static int pwrap_read(struct pmic_wrapper *wrp, u32 adr, u32 *rdata)
  422. {
  423. int ret;
  424. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle);
  425. if (ret)
  426. return ret;
  427. pwrap_writel(wrp, (adr >> 1) << 16, PWRAP_WACS2_CMD);
  428. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_vldclr);
  429. if (ret)
  430. return ret;
  431. *rdata = PWRAP_GET_WACS_RDATA(pwrap_readl(wrp, PWRAP_WACS2_RDATA));
  432. pwrap_writel(wrp, 1, PWRAP_WACS2_VLDCLR);
  433. return 0;
  434. }
  435. static int pwrap_regmap_read(void *context, u32 adr, u32 *rdata)
  436. {
  437. return pwrap_read(context, adr, rdata);
  438. }
  439. static int pwrap_regmap_write(void *context, u32 adr, u32 wdata)
  440. {
  441. return pwrap_write(context, adr, wdata);
  442. }
  443. static int pwrap_reset_spislave(struct pmic_wrapper *wrp)
  444. {
  445. int ret, i;
  446. pwrap_writel(wrp, 0, PWRAP_HIPRIO_ARB_EN);
  447. pwrap_writel(wrp, 0, PWRAP_WRAP_EN);
  448. pwrap_writel(wrp, 1, PWRAP_MUX_SEL);
  449. pwrap_writel(wrp, 1, PWRAP_MAN_EN);
  450. pwrap_writel(wrp, 0, PWRAP_DIO_EN);
  451. pwrap_writel(wrp, PWRAP_MAN_CMD_SPI_WRITE | PWRAP_MAN_CMD_OP_CSL,
  452. PWRAP_MAN_CMD);
  453. pwrap_writel(wrp, PWRAP_MAN_CMD_SPI_WRITE | PWRAP_MAN_CMD_OP_OUTS,
  454. PWRAP_MAN_CMD);
  455. pwrap_writel(wrp, PWRAP_MAN_CMD_SPI_WRITE | PWRAP_MAN_CMD_OP_CSH,
  456. PWRAP_MAN_CMD);
  457. for (i = 0; i < 4; i++)
  458. pwrap_writel(wrp, PWRAP_MAN_CMD_SPI_WRITE | PWRAP_MAN_CMD_OP_OUTS,
  459. PWRAP_MAN_CMD);
  460. ret = pwrap_wait_for_state(wrp, pwrap_is_sync_idle);
  461. if (ret) {
  462. dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
  463. return ret;
  464. }
  465. pwrap_writel(wrp, 0, PWRAP_MAN_EN);
  466. pwrap_writel(wrp, 0, PWRAP_MUX_SEL);
  467. return 0;
  468. }
  469. /*
  470. * pwrap_init_sidly - configure serial input delay
  471. *
  472. * This configures the serial input delay. We can configure 0, 2, 4 or 6ns
  473. * delay. Do a read test with all possible values and chose the best delay.
  474. */
  475. static int pwrap_init_sidly(struct pmic_wrapper *wrp)
  476. {
  477. u32 rdata;
  478. u32 i;
  479. u32 pass = 0;
  480. signed char dly[16] = {
  481. -1, 0, 1, 0, 2, -1, 1, 1, 3, -1, -1, -1, 3, -1, 2, 1
  482. };
  483. for (i = 0; i < 4; i++) {
  484. pwrap_writel(wrp, i, PWRAP_SIDLY);
  485. pwrap_read(wrp, PWRAP_DEW_READ_TEST, &rdata);
  486. if (rdata == PWRAP_DEW_READ_TEST_VAL) {
  487. dev_dbg(wrp->dev, "[Read Test] pass, SIDLY=%x\n", i);
  488. pass |= 1 << i;
  489. }
  490. }
  491. if (dly[pass] < 0) {
  492. dev_err(wrp->dev, "sidly pass range 0x%x not continuous\n",
  493. pass);
  494. return -EIO;
  495. }
  496. pwrap_writel(wrp, dly[pass], PWRAP_SIDLY);
  497. return 0;
  498. }
  499. static int pwrap_init_reg_clock(struct pmic_wrapper *wrp)
  500. {
  501. if (pwrap_is_mt8135(wrp)) {
  502. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT);
  503. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_WRITE);
  504. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT_READ);
  505. pwrap_writel(wrp, 0x0, PWRAP_CSLEXT_START);
  506. pwrap_writel(wrp, 0x0, PWRAP_CSLEXT_END);
  507. } else {
  508. pwrap_writel(wrp, 0x0, PWRAP_CSHEXT_WRITE);
  509. pwrap_writel(wrp, 0x4, PWRAP_CSHEXT_READ);
  510. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_START);
  511. pwrap_writel(wrp, 0x2, PWRAP_CSLEXT_END);
  512. }
  513. return 0;
  514. }
  515. static bool pwrap_is_cipher_ready(struct pmic_wrapper *wrp)
  516. {
  517. return pwrap_readl(wrp, PWRAP_CIPHER_RDY) & 1;
  518. }
  519. static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
  520. {
  521. u32 rdata;
  522. int ret;
  523. ret = pwrap_read(wrp, PWRAP_DEW_CIPHER_RDY, &rdata);
  524. if (ret)
  525. return 0;
  526. return rdata == 1;
  527. }
  528. static int pwrap_init_cipher(struct pmic_wrapper *wrp)
  529. {
  530. int ret;
  531. u32 rdata;
  532. pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
  533. pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
  534. pwrap_writel(wrp, 0x1, PWRAP_CIPHER_KEY_SEL);
  535. pwrap_writel(wrp, 0x2, PWRAP_CIPHER_IV_SEL);
  536. if (pwrap_is_mt8135(wrp)) {
  537. pwrap_writel(wrp, 1, PWRAP_CIPHER_LOAD);
  538. pwrap_writel(wrp, 1, PWRAP_CIPHER_START);
  539. } else {
  540. pwrap_writel(wrp, 1, PWRAP_CIPHER_EN);
  541. }
  542. /* Config cipher mode @PMIC */
  543. pwrap_write(wrp, PWRAP_DEW_CIPHER_SWRST, 0x1);
  544. pwrap_write(wrp, PWRAP_DEW_CIPHER_SWRST, 0x0);
  545. pwrap_write(wrp, PWRAP_DEW_CIPHER_KEY_SEL, 0x1);
  546. pwrap_write(wrp, PWRAP_DEW_CIPHER_IV_SEL, 0x2);
  547. pwrap_write(wrp, PWRAP_DEW_CIPHER_LOAD, 0x1);
  548. pwrap_write(wrp, PWRAP_DEW_CIPHER_START, 0x1);
  549. /* wait for cipher data ready@AP */
  550. ret = pwrap_wait_for_state(wrp, pwrap_is_cipher_ready);
  551. if (ret) {
  552. dev_err(wrp->dev, "cipher data ready@AP fail, ret=%d\n", ret);
  553. return ret;
  554. }
  555. /* wait for cipher data ready@PMIC */
  556. ret = pwrap_wait_for_state(wrp, pwrap_is_pmic_cipher_ready);
  557. if (ret) {
  558. dev_err(wrp->dev, "timeout waiting for cipher data ready@PMIC\n");
  559. return ret;
  560. }
  561. /* wait for cipher mode idle */
  562. pwrap_write(wrp, PWRAP_DEW_CIPHER_MODE, 0x1);
  563. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle_and_sync_idle);
  564. if (ret) {
  565. dev_err(wrp->dev, "cipher mode idle fail, ret=%d\n", ret);
  566. return ret;
  567. }
  568. pwrap_writel(wrp, 1, PWRAP_CIPHER_MODE);
  569. /* Write Test */
  570. if (pwrap_write(wrp, PWRAP_DEW_WRITE_TEST, PWRAP_DEW_WRITE_TEST_VAL) ||
  571. pwrap_read(wrp, PWRAP_DEW_WRITE_TEST, &rdata) ||
  572. (rdata != PWRAP_DEW_WRITE_TEST_VAL)) {
  573. dev_err(wrp->dev, "rdata=0x%04X\n", rdata);
  574. return -EFAULT;
  575. }
  576. return 0;
  577. }
  578. static int pwrap_init(struct pmic_wrapper *wrp)
  579. {
  580. int ret;
  581. u32 rdata;
  582. reset_control_reset(wrp->rstc);
  583. if (wrp->rstc_bridge)
  584. reset_control_reset(wrp->rstc_bridge);
  585. if (pwrap_is_mt8173(wrp)) {
  586. /* Enable DCM */
  587. pwrap_writel(wrp, 3, PWRAP_DCM_EN);
  588. pwrap_writel(wrp, 0, PWRAP_DCM_DBC_PRD);
  589. }
  590. /* Reset SPI slave */
  591. ret = pwrap_reset_spislave(wrp);
  592. if (ret)
  593. return ret;
  594. pwrap_writel(wrp, 1, PWRAP_WRAP_EN);
  595. pwrap_writel(wrp, wrp->arb_en_all, PWRAP_HIPRIO_ARB_EN);
  596. pwrap_writel(wrp, 1, PWRAP_WACS2_EN);
  597. ret = pwrap_init_reg_clock(wrp);
  598. if (ret)
  599. return ret;
  600. /* Setup serial input delay */
  601. ret = pwrap_init_sidly(wrp);
  602. if (ret)
  603. return ret;
  604. /* Enable dual IO mode */
  605. pwrap_write(wrp, PWRAP_DEW_DIO_EN, 1);
  606. /* Check IDLE & INIT_DONE in advance */
  607. ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle_and_sync_idle);
  608. if (ret) {
  609. dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
  610. return ret;
  611. }
  612. pwrap_writel(wrp, 1, PWRAP_DIO_EN);
  613. /* Read Test */
  614. pwrap_read(wrp, PWRAP_DEW_READ_TEST, &rdata);
  615. if (rdata != PWRAP_DEW_READ_TEST_VAL) {
  616. dev_err(wrp->dev, "Read test failed after switch to DIO mode: 0x%04x != 0x%04x\n",
  617. PWRAP_DEW_READ_TEST_VAL, rdata);
  618. return -EFAULT;
  619. }
  620. /* Enable encryption */
  621. ret = pwrap_init_cipher(wrp);
  622. if (ret)
  623. return ret;
  624. /* Signature checking - using CRC */
  625. if (pwrap_write(wrp, PWRAP_DEW_CRC_EN, 0x1))
  626. return -EFAULT;
  627. pwrap_writel(wrp, 0x1, PWRAP_CRC_EN);
  628. pwrap_writel(wrp, 0x0, PWRAP_SIG_MODE);
  629. pwrap_writel(wrp, PWRAP_DEW_CRC_VAL, PWRAP_SIG_ADR);
  630. pwrap_writel(wrp, wrp->arb_en_all, PWRAP_HIPRIO_ARB_EN);
  631. if (pwrap_is_mt8135(wrp))
  632. pwrap_writel(wrp, 0x7, PWRAP_RRARB_EN);
  633. pwrap_writel(wrp, 0x1, PWRAP_WACS0_EN);
  634. pwrap_writel(wrp, 0x1, PWRAP_WACS1_EN);
  635. pwrap_writel(wrp, 0x1, PWRAP_WACS2_EN);
  636. pwrap_writel(wrp, 0x5, PWRAP_STAUPD_PRD);
  637. pwrap_writel(wrp, 0xff, PWRAP_STAUPD_GRPEN);
  638. if (pwrap_is_mt8135(wrp)) {
  639. /* enable pwrap events and pwrap bridge in AP side */
  640. pwrap_writel(wrp, 0x1, PWRAP_EVENT_IN_EN);
  641. pwrap_writel(wrp, 0xffff, PWRAP_EVENT_DST_EN);
  642. writel(0x7f, wrp->bridge_base + PWRAP_MT8135_BRIDGE_IORD_ARB_EN);
  643. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WACS3_EN);
  644. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WACS4_EN);
  645. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WDT_UNIT);
  646. writel(0xffff, wrp->bridge_base + PWRAP_MT8135_BRIDGE_WDT_SRC_EN);
  647. writel(0x1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_TIMER_EN);
  648. writel(0x7ff, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INT_EN);
  649. /* enable PMIC event out and sources */
  650. if (pwrap_write(wrp, PWRAP_DEW_EVENT_OUT_EN, 0x1) ||
  651. pwrap_write(wrp, PWRAP_DEW_EVENT_SRC_EN, 0xffff)) {
  652. dev_err(wrp->dev, "enable dewrap fail\n");
  653. return -EFAULT;
  654. }
  655. } else {
  656. /* PMIC_DEWRAP enables */
  657. if (pwrap_write(wrp, PWRAP_DEW_EVENT_OUT_EN, 0x1) ||
  658. pwrap_write(wrp, PWRAP_DEW_EVENT_SRC_EN, 0xffff)) {
  659. dev_err(wrp->dev, "enable dewrap fail\n");
  660. return -EFAULT;
  661. }
  662. }
  663. /* Setup the init done registers */
  664. pwrap_writel(wrp, 1, PWRAP_INIT_DONE2);
  665. pwrap_writel(wrp, 1, PWRAP_INIT_DONE0);
  666. pwrap_writel(wrp, 1, PWRAP_INIT_DONE1);
  667. if (pwrap_is_mt8135(wrp)) {
  668. writel(1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INIT_DONE3);
  669. writel(1, wrp->bridge_base + PWRAP_MT8135_BRIDGE_INIT_DONE4);
  670. }
  671. return 0;
  672. }
  673. static irqreturn_t pwrap_interrupt(int irqno, void *dev_id)
  674. {
  675. u32 rdata;
  676. struct pmic_wrapper *wrp = dev_id;
  677. rdata = pwrap_readl(wrp, PWRAP_INT_FLG);
  678. dev_err(wrp->dev, "unexpected interrupt int=0x%x\n", rdata);
  679. pwrap_writel(wrp, 0xffffffff, PWRAP_INT_CLR);
  680. return IRQ_HANDLED;
  681. }
  682. static const struct regmap_config pwrap_regmap_config = {
  683. .reg_bits = 16,
  684. .val_bits = 16,
  685. .reg_stride = 2,
  686. .reg_read = pwrap_regmap_read,
  687. .reg_write = pwrap_regmap_write,
  688. .max_register = 0xffff,
  689. };
  690. static struct of_device_id of_pwrap_match_tbl[] = {
  691. {
  692. .compatible = "mediatek,mt8135-pwrap",
  693. .data = &pwrap_mt8135,
  694. }, {
  695. .compatible = "mediatek,mt8173-pwrap",
  696. .data = &pwrap_mt8173,
  697. }, {
  698. /* sentinel */
  699. }
  700. };
  701. MODULE_DEVICE_TABLE(of, of_pwrap_match_tbl);
  702. static int pwrap_probe(struct platform_device *pdev)
  703. {
  704. int ret, irq;
  705. struct pmic_wrapper *wrp;
  706. struct device_node *np = pdev->dev.of_node;
  707. const struct of_device_id *of_id =
  708. of_match_device(of_pwrap_match_tbl, &pdev->dev);
  709. const struct pmic_wrapper_type *type;
  710. struct resource *res;
  711. wrp = devm_kzalloc(&pdev->dev, sizeof(*wrp), GFP_KERNEL);
  712. if (!wrp)
  713. return -ENOMEM;
  714. platform_set_drvdata(pdev, wrp);
  715. type = of_id->data;
  716. wrp->regs = type->regs;
  717. wrp->type = type->type;
  718. wrp->arb_en_all = type->arb_en_all;
  719. wrp->dev = &pdev->dev;
  720. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwrap");
  721. wrp->base = devm_ioremap_resource(wrp->dev, res);
  722. if (IS_ERR(wrp->base))
  723. return PTR_ERR(wrp->base);
  724. wrp->rstc = devm_reset_control_get(wrp->dev, "pwrap");
  725. if (IS_ERR(wrp->rstc)) {
  726. ret = PTR_ERR(wrp->rstc);
  727. dev_dbg(wrp->dev, "cannot get pwrap reset: %d\n", ret);
  728. return ret;
  729. }
  730. if (pwrap_is_mt8135(wrp)) {
  731. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  732. "pwrap-bridge");
  733. wrp->bridge_base = devm_ioremap_resource(wrp->dev, res);
  734. if (IS_ERR(wrp->bridge_base))
  735. return PTR_ERR(wrp->bridge_base);
  736. wrp->rstc_bridge = devm_reset_control_get(wrp->dev, "pwrap-bridge");
  737. if (IS_ERR(wrp->rstc_bridge)) {
  738. ret = PTR_ERR(wrp->rstc_bridge);
  739. dev_dbg(wrp->dev, "cannot get pwrap-bridge reset: %d\n", ret);
  740. return ret;
  741. }
  742. }
  743. wrp->clk_spi = devm_clk_get(wrp->dev, "spi");
  744. if (IS_ERR(wrp->clk_spi)) {
  745. dev_dbg(wrp->dev, "failed to get clock: %ld\n", PTR_ERR(wrp->clk_spi));
  746. return PTR_ERR(wrp->clk_spi);
  747. }
  748. wrp->clk_wrap = devm_clk_get(wrp->dev, "wrap");
  749. if (IS_ERR(wrp->clk_wrap)) {
  750. dev_dbg(wrp->dev, "failed to get clock: %ld\n", PTR_ERR(wrp->clk_wrap));
  751. return PTR_ERR(wrp->clk_wrap);
  752. }
  753. ret = clk_prepare_enable(wrp->clk_spi);
  754. if (ret)
  755. return ret;
  756. ret = clk_prepare_enable(wrp->clk_wrap);
  757. if (ret)
  758. goto err_out1;
  759. /* Enable internal dynamic clock */
  760. pwrap_writel(wrp, 1, PWRAP_DCM_EN);
  761. pwrap_writel(wrp, 0, PWRAP_DCM_DBC_PRD);
  762. /*
  763. * The PMIC could already be initialized by the bootloader.
  764. * Skip initialization here in this case.
  765. */
  766. if (!pwrap_readl(wrp, PWRAP_INIT_DONE2)) {
  767. ret = pwrap_init(wrp);
  768. if (ret) {
  769. dev_dbg(wrp->dev, "init failed with %d\n", ret);
  770. goto err_out2;
  771. }
  772. }
  773. if (!(pwrap_readl(wrp, PWRAP_WACS2_RDATA) & PWRAP_STATE_INIT_DONE0)) {
  774. dev_dbg(wrp->dev, "initialization isn't finished\n");
  775. return -ENODEV;
  776. }
  777. /* Initialize watchdog, may not be done by the bootloader */
  778. pwrap_writel(wrp, 0xf, PWRAP_WDT_UNIT);
  779. pwrap_writel(wrp, 0xffffffff, PWRAP_WDT_SRC_EN);
  780. pwrap_writel(wrp, 0x1, PWRAP_TIMER_EN);
  781. pwrap_writel(wrp, ~((1 << 31) | (1 << 1)), PWRAP_INT_EN);
  782. irq = platform_get_irq(pdev, 0);
  783. ret = devm_request_irq(wrp->dev, irq, pwrap_interrupt, IRQF_TRIGGER_HIGH,
  784. "mt-pmic-pwrap", wrp);
  785. if (ret)
  786. goto err_out2;
  787. wrp->regmap = devm_regmap_init(wrp->dev, NULL, wrp, &pwrap_regmap_config);
  788. if (IS_ERR(wrp->regmap))
  789. return PTR_ERR(wrp->regmap);
  790. ret = of_platform_populate(np, NULL, NULL, wrp->dev);
  791. if (ret) {
  792. dev_dbg(wrp->dev, "failed to create child devices at %s\n",
  793. np->full_name);
  794. goto err_out2;
  795. }
  796. return 0;
  797. err_out2:
  798. clk_disable_unprepare(wrp->clk_wrap);
  799. err_out1:
  800. clk_disable_unprepare(wrp->clk_spi);
  801. return ret;
  802. }
  803. static struct platform_driver pwrap_drv = {
  804. .driver = {
  805. .name = "mt-pmic-pwrap",
  806. .of_match_table = of_match_ptr(of_pwrap_match_tbl),
  807. },
  808. .probe = pwrap_probe,
  809. };
  810. module_platform_driver(pwrap_drv);
  811. MODULE_AUTHOR("Flora Fu, MediaTek");
  812. MODULE_DESCRIPTION("MediaTek MT8135 PMIC Wrapper Driver");
  813. MODULE_LICENSE("GPL v2");