mtk-platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for EIP97 cryptographic accelerator.
  4. *
  5. * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include "mtk-platform.h"
  15. #define MTK_BURST_SIZE_MSK GENMASK(7, 4)
  16. #define MTK_BURST_SIZE(x) ((x) << 4)
  17. #define MTK_DESC_SIZE(x) ((x) << 0)
  18. #define MTK_DESC_OFFSET(x) ((x) << 16)
  19. #define MTK_DESC_FETCH_SIZE(x) ((x) << 0)
  20. #define MTK_DESC_FETCH_THRESH(x) ((x) << 16)
  21. #define MTK_DESC_OVL_IRQ_EN BIT(25)
  22. #define MTK_DESC_ATP_PRESENT BIT(30)
  23. #define MTK_DFSE_IDLE GENMASK(3, 0)
  24. #define MTK_DFSE_THR_CTRL_EN BIT(30)
  25. #define MTK_DFSE_THR_CTRL_RESET BIT(31)
  26. #define MTK_DFSE_RING_ID(x) (((x) >> 12) & GENMASK(3, 0))
  27. #define MTK_DFSE_MIN_DATA(x) ((x) << 0)
  28. #define MTK_DFSE_MAX_DATA(x) ((x) << 8)
  29. #define MTK_DFE_MIN_CTRL(x) ((x) << 16)
  30. #define MTK_DFE_MAX_CTRL(x) ((x) << 24)
  31. #define MTK_IN_BUF_MIN_THRESH(x) ((x) << 8)
  32. #define MTK_IN_BUF_MAX_THRESH(x) ((x) << 12)
  33. #define MTK_OUT_BUF_MIN_THRESH(x) ((x) << 0)
  34. #define MTK_OUT_BUF_MAX_THRESH(x) ((x) << 4)
  35. #define MTK_IN_TBUF_SIZE(x) (((x) >> 4) & GENMASK(3, 0))
  36. #define MTK_IN_DBUF_SIZE(x) (((x) >> 8) & GENMASK(3, 0))
  37. #define MTK_OUT_DBUF_SIZE(x) (((x) >> 16) & GENMASK(3, 0))
  38. #define MTK_CMD_FIFO_SIZE(x) (((x) >> 8) & GENMASK(3, 0))
  39. #define MTK_RES_FIFO_SIZE(x) (((x) >> 12) & GENMASK(3, 0))
  40. #define MTK_PE_TK_LOC_AVL BIT(2)
  41. #define MTK_PE_PROC_HELD BIT(14)
  42. #define MTK_PE_TK_TIMEOUT_EN BIT(22)
  43. #define MTK_PE_INPUT_DMA_ERR BIT(0)
  44. #define MTK_PE_OUTPUT_DMA_ERR BIT(1)
  45. #define MTK_PE_PKT_PORC_ERR BIT(2)
  46. #define MTK_PE_PKT_TIMEOUT BIT(3)
  47. #define MTK_PE_FATAL_ERR BIT(14)
  48. #define MTK_PE_INPUT_DMA_ERR_EN BIT(16)
  49. #define MTK_PE_OUTPUT_DMA_ERR_EN BIT(17)
  50. #define MTK_PE_PKT_PORC_ERR_EN BIT(18)
  51. #define MTK_PE_PKT_TIMEOUT_EN BIT(19)
  52. #define MTK_PE_FATAL_ERR_EN BIT(30)
  53. #define MTK_PE_INT_OUT_EN BIT(31)
  54. #define MTK_HIA_SIGNATURE ((u16)0x35ca)
  55. #define MTK_HIA_DATA_WIDTH(x) (((x) >> 25) & GENMASK(1, 0))
  56. #define MTK_HIA_DMA_LENGTH(x) (((x) >> 20) & GENMASK(4, 0))
  57. #define MTK_CDR_STAT_CLR GENMASK(4, 0)
  58. #define MTK_RDR_STAT_CLR GENMASK(7, 0)
  59. #define MTK_AIC_INT_MSK GENMASK(5, 0)
  60. #define MTK_AIC_VER_MSK (GENMASK(15, 0) | GENMASK(27, 20))
  61. #define MTK_AIC_VER11 0x011036c9
  62. #define MTK_AIC_VER12 0x012036c9
  63. #define MTK_AIC_G_CLR GENMASK(30, 20)
  64. /**
  65. * EIP97 is an integrated security subsystem to accelerate cryptographic
  66. * functions and protocols to offload the host processor.
  67. * Some important hardware modules are briefly introduced below:
  68. *
  69. * Host Interface Adapter(HIA) - the main interface between the host
  70. * system and the hardware subsystem. It is responsible for attaching
  71. * processing engine to the specific host bus interface and provides a
  72. * standardized software view for off loading tasks to the engine.
  73. *
  74. * Command Descriptor Ring Manager(CDR Manager) - keeps track of how many
  75. * CD the host has prepared in the CDR. It monitors the fill level of its
  76. * CD-FIFO and if there's sufficient space for the next block of descriptors,
  77. * then it fires off a DMA request to fetch a block of CDs.
  78. *
  79. * Data fetch engine(DFE) - It is responsible for parsing the CD and
  80. * setting up the required control and packet data DMA transfers from
  81. * system memory to the processing engine.
  82. *
  83. * Result Descriptor Ring Manager(RDR Manager) - same as CDR Manager,
  84. * but target is result descriptors, Moreover, it also handles the RD
  85. * updates under control of the DSE. For each packet data segment
  86. * processed, the DSE triggers the RDR Manager to write the updated RD.
  87. * If triggered to update, the RDR Manager sets up a DMA operation to
  88. * copy the RD from the DSE to the correct location in the RDR.
  89. *
  90. * Data Store Engine(DSE) - It is responsible for parsing the prepared RD
  91. * and setting up the required control and packet data DMA transfers from
  92. * the processing engine to system memory.
  93. *
  94. * Advanced Interrupt Controllers(AICs) - receive interrupt request signals
  95. * from various sources and combine them into one interrupt output.
  96. * The AICs are used by:
  97. * - One for the HIA global and processing engine interrupts.
  98. * - The others for the descriptor ring interrupts.
  99. */
  100. /* Cryptographic engine capabilities */
  101. struct mtk_sys_cap {
  102. /* host interface adapter */
  103. u32 hia_ver;
  104. u32 hia_opt;
  105. /* packet engine */
  106. u32 pkt_eng_opt;
  107. /* global hardware */
  108. u32 hw_opt;
  109. };
  110. static void mtk_desc_ring_link(struct mtk_cryp *cryp, u32 mask)
  111. {
  112. /* Assign rings to DFE/DSE thread and enable it */
  113. writel(MTK_DFSE_THR_CTRL_EN | mask, cryp->base + DFE_THR_CTRL);
  114. writel(MTK_DFSE_THR_CTRL_EN | mask, cryp->base + DSE_THR_CTRL);
  115. }
  116. static void mtk_dfe_dse_buf_setup(struct mtk_cryp *cryp,
  117. struct mtk_sys_cap *cap)
  118. {
  119. u32 width = MTK_HIA_DATA_WIDTH(cap->hia_opt) + 2;
  120. u32 len = MTK_HIA_DMA_LENGTH(cap->hia_opt) - 1;
  121. u32 ipbuf = min((u32)MTK_IN_DBUF_SIZE(cap->hw_opt) + width, len);
  122. u32 opbuf = min((u32)MTK_OUT_DBUF_SIZE(cap->hw_opt) + width, len);
  123. u32 itbuf = min((u32)MTK_IN_TBUF_SIZE(cap->hw_opt) + width, len);
  124. writel(MTK_DFSE_MIN_DATA(ipbuf - 1) |
  125. MTK_DFSE_MAX_DATA(ipbuf) |
  126. MTK_DFE_MIN_CTRL(itbuf - 1) |
  127. MTK_DFE_MAX_CTRL(itbuf),
  128. cryp->base + DFE_CFG);
  129. writel(MTK_DFSE_MIN_DATA(opbuf - 1) |
  130. MTK_DFSE_MAX_DATA(opbuf),
  131. cryp->base + DSE_CFG);
  132. writel(MTK_IN_BUF_MIN_THRESH(ipbuf - 1) |
  133. MTK_IN_BUF_MAX_THRESH(ipbuf),
  134. cryp->base + PE_IN_DBUF_THRESH);
  135. writel(MTK_IN_BUF_MIN_THRESH(itbuf - 1) |
  136. MTK_IN_BUF_MAX_THRESH(itbuf),
  137. cryp->base + PE_IN_TBUF_THRESH);
  138. writel(MTK_OUT_BUF_MIN_THRESH(opbuf - 1) |
  139. MTK_OUT_BUF_MAX_THRESH(opbuf),
  140. cryp->base + PE_OUT_DBUF_THRESH);
  141. writel(0, cryp->base + PE_OUT_TBUF_THRESH);
  142. writel(0, cryp->base + PE_OUT_BUF_CTRL);
  143. }
  144. static int mtk_dfe_dse_state_check(struct mtk_cryp *cryp)
  145. {
  146. int ret = -EINVAL;
  147. u32 val;
  148. /* Check for completion of all DMA transfers */
  149. val = readl(cryp->base + DFE_THR_STAT);
  150. if (MTK_DFSE_RING_ID(val) == MTK_DFSE_IDLE) {
  151. val = readl(cryp->base + DSE_THR_STAT);
  152. if (MTK_DFSE_RING_ID(val) == MTK_DFSE_IDLE)
  153. ret = 0;
  154. }
  155. if (!ret) {
  156. /* Take DFE/DSE thread out of reset */
  157. writel(0, cryp->base + DFE_THR_CTRL);
  158. writel(0, cryp->base + DSE_THR_CTRL);
  159. } else {
  160. return -EBUSY;
  161. }
  162. return 0;
  163. }
  164. static int mtk_dfe_dse_reset(struct mtk_cryp *cryp)
  165. {
  166. int err;
  167. /* Reset DSE/DFE and correct system priorities for all rings. */
  168. writel(MTK_DFSE_THR_CTRL_RESET, cryp->base + DFE_THR_CTRL);
  169. writel(0, cryp->base + DFE_PRIO_0);
  170. writel(0, cryp->base + DFE_PRIO_1);
  171. writel(0, cryp->base + DFE_PRIO_2);
  172. writel(0, cryp->base + DFE_PRIO_3);
  173. writel(MTK_DFSE_THR_CTRL_RESET, cryp->base + DSE_THR_CTRL);
  174. writel(0, cryp->base + DSE_PRIO_0);
  175. writel(0, cryp->base + DSE_PRIO_1);
  176. writel(0, cryp->base + DSE_PRIO_2);
  177. writel(0, cryp->base + DSE_PRIO_3);
  178. err = mtk_dfe_dse_state_check(cryp);
  179. if (err)
  180. return err;
  181. return 0;
  182. }
  183. static void mtk_cmd_desc_ring_setup(struct mtk_cryp *cryp,
  184. int i, struct mtk_sys_cap *cap)
  185. {
  186. /* Full descriptor that fits FIFO minus one */
  187. u32 count =
  188. ((1 << MTK_CMD_FIFO_SIZE(cap->hia_opt)) / MTK_DESC_SZ) - 1;
  189. /* Temporarily disable external triggering */
  190. writel(0, cryp->base + CDR_CFG(i));
  191. /* Clear CDR count */
  192. writel(MTK_CNT_RST, cryp->base + CDR_PREP_COUNT(i));
  193. writel(MTK_CNT_RST, cryp->base + CDR_PROC_COUNT(i));
  194. writel(0, cryp->base + CDR_PREP_PNTR(i));
  195. writel(0, cryp->base + CDR_PROC_PNTR(i));
  196. writel(0, cryp->base + CDR_DMA_CFG(i));
  197. /* Configure CDR host address space */
  198. writel(0, cryp->base + CDR_BASE_ADDR_HI(i));
  199. writel(cryp->ring[i]->cmd_dma, cryp->base + CDR_BASE_ADDR_LO(i));
  200. writel(MTK_DESC_RING_SZ, cryp->base + CDR_RING_SIZE(i));
  201. /* Clear and disable all CDR interrupts */
  202. writel(MTK_CDR_STAT_CLR, cryp->base + CDR_STAT(i));
  203. /*
  204. * Set command descriptor offset and enable additional
  205. * token present in descriptor.
  206. */
  207. writel(MTK_DESC_SIZE(MTK_DESC_SZ) |
  208. MTK_DESC_OFFSET(MTK_DESC_OFF) |
  209. MTK_DESC_ATP_PRESENT,
  210. cryp->base + CDR_DESC_SIZE(i));
  211. writel(MTK_DESC_FETCH_SIZE(count * MTK_DESC_OFF) |
  212. MTK_DESC_FETCH_THRESH(count * MTK_DESC_SZ),
  213. cryp->base + CDR_CFG(i));
  214. }
  215. static void mtk_res_desc_ring_setup(struct mtk_cryp *cryp,
  216. int i, struct mtk_sys_cap *cap)
  217. {
  218. u32 rndup = 2;
  219. u32 count = ((1 << MTK_RES_FIFO_SIZE(cap->hia_opt)) / rndup) - 1;
  220. /* Temporarily disable external triggering */
  221. writel(0, cryp->base + RDR_CFG(i));
  222. /* Clear RDR count */
  223. writel(MTK_CNT_RST, cryp->base + RDR_PREP_COUNT(i));
  224. writel(MTK_CNT_RST, cryp->base + RDR_PROC_COUNT(i));
  225. writel(0, cryp->base + RDR_PREP_PNTR(i));
  226. writel(0, cryp->base + RDR_PROC_PNTR(i));
  227. writel(0, cryp->base + RDR_DMA_CFG(i));
  228. /* Configure RDR host address space */
  229. writel(0, cryp->base + RDR_BASE_ADDR_HI(i));
  230. writel(cryp->ring[i]->res_dma, cryp->base + RDR_BASE_ADDR_LO(i));
  231. writel(MTK_DESC_RING_SZ, cryp->base + RDR_RING_SIZE(i));
  232. writel(MTK_RDR_STAT_CLR, cryp->base + RDR_STAT(i));
  233. /*
  234. * RDR manager generates update interrupts on a per-completed-packet,
  235. * and the rd_proc_thresh_irq interrupt is fired when proc_pkt_count
  236. * for the RDR exceeds the number of packets.
  237. */
  238. writel(MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE,
  239. cryp->base + RDR_THRESH(i));
  240. /*
  241. * Configure a threshold and time-out value for the processed
  242. * result descriptors (or complete packets) that are written to
  243. * the RDR.
  244. */
  245. writel(MTK_DESC_SIZE(MTK_DESC_SZ) | MTK_DESC_OFFSET(MTK_DESC_OFF),
  246. cryp->base + RDR_DESC_SIZE(i));
  247. /*
  248. * Configure HIA fetch size and fetch threshold that are used to
  249. * fetch blocks of multiple descriptors.
  250. */
  251. writel(MTK_DESC_FETCH_SIZE(count * MTK_DESC_OFF) |
  252. MTK_DESC_FETCH_THRESH(count * rndup) |
  253. MTK_DESC_OVL_IRQ_EN,
  254. cryp->base + RDR_CFG(i));
  255. }
  256. static int mtk_packet_engine_setup(struct mtk_cryp *cryp)
  257. {
  258. struct mtk_sys_cap cap;
  259. int i, err;
  260. u32 val;
  261. cap.hia_ver = readl(cryp->base + HIA_VERSION);
  262. cap.hia_opt = readl(cryp->base + HIA_OPTIONS);
  263. cap.hw_opt = readl(cryp->base + EIP97_OPTIONS);
  264. if (!(((u16)cap.hia_ver) == MTK_HIA_SIGNATURE))
  265. return -EINVAL;
  266. /* Configure endianness conversion method for master (DMA) interface */
  267. writel(0, cryp->base + EIP97_MST_CTRL);
  268. /* Set HIA burst size */
  269. val = readl(cryp->base + HIA_MST_CTRL);
  270. val &= ~MTK_BURST_SIZE_MSK;
  271. val |= MTK_BURST_SIZE(5);
  272. writel(val, cryp->base + HIA_MST_CTRL);
  273. err = mtk_dfe_dse_reset(cryp);
  274. if (err) {
  275. dev_err(cryp->dev, "Failed to reset DFE and DSE.\n");
  276. return err;
  277. }
  278. mtk_dfe_dse_buf_setup(cryp, &cap);
  279. /* Enable the 4 rings for the packet engines. */
  280. mtk_desc_ring_link(cryp, 0xf);
  281. for (i = 0; i < MTK_RING_MAX; i++) {
  282. mtk_cmd_desc_ring_setup(cryp, i, &cap);
  283. mtk_res_desc_ring_setup(cryp, i, &cap);
  284. }
  285. writel(MTK_PE_TK_LOC_AVL | MTK_PE_PROC_HELD | MTK_PE_TK_TIMEOUT_EN,
  286. cryp->base + PE_TOKEN_CTRL_STAT);
  287. /* Clear all pending interrupts */
  288. writel(MTK_AIC_G_CLR, cryp->base + AIC_G_ACK);
  289. writel(MTK_PE_INPUT_DMA_ERR | MTK_PE_OUTPUT_DMA_ERR |
  290. MTK_PE_PKT_PORC_ERR | MTK_PE_PKT_TIMEOUT |
  291. MTK_PE_FATAL_ERR | MTK_PE_INPUT_DMA_ERR_EN |
  292. MTK_PE_OUTPUT_DMA_ERR_EN | MTK_PE_PKT_PORC_ERR_EN |
  293. MTK_PE_PKT_TIMEOUT_EN | MTK_PE_FATAL_ERR_EN |
  294. MTK_PE_INT_OUT_EN,
  295. cryp->base + PE_INTERRUPT_CTRL_STAT);
  296. return 0;
  297. }
  298. static int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw)
  299. {
  300. u32 val;
  301. if (hw == MTK_RING_MAX)
  302. val = readl(cryp->base + AIC_G_VERSION);
  303. else
  304. val = readl(cryp->base + AIC_VERSION(hw));
  305. val &= MTK_AIC_VER_MSK;
  306. if (val != MTK_AIC_VER11 && val != MTK_AIC_VER12)
  307. return -ENXIO;
  308. if (hw == MTK_RING_MAX)
  309. val = readl(cryp->base + AIC_G_OPTIONS);
  310. else
  311. val = readl(cryp->base + AIC_OPTIONS(hw));
  312. val &= MTK_AIC_INT_MSK;
  313. if (!val || val > 32)
  314. return -ENXIO;
  315. return 0;
  316. }
  317. static int mtk_aic_init(struct mtk_cryp *cryp, int hw)
  318. {
  319. int err;
  320. err = mtk_aic_cap_check(cryp, hw);
  321. if (err)
  322. return err;
  323. /* Disable all interrupts and set initial configuration */
  324. if (hw == MTK_RING_MAX) {
  325. writel(0, cryp->base + AIC_G_ENABLE_CTRL);
  326. writel(0, cryp->base + AIC_G_POL_CTRL);
  327. writel(0, cryp->base + AIC_G_TYPE_CTRL);
  328. writel(0, cryp->base + AIC_G_ENABLE_SET);
  329. } else {
  330. writel(0, cryp->base + AIC_ENABLE_CTRL(hw));
  331. writel(0, cryp->base + AIC_POL_CTRL(hw));
  332. writel(0, cryp->base + AIC_TYPE_CTRL(hw));
  333. writel(0, cryp->base + AIC_ENABLE_SET(hw));
  334. }
  335. return 0;
  336. }
  337. static int mtk_accelerator_init(struct mtk_cryp *cryp)
  338. {
  339. int i, err;
  340. /* Initialize advanced interrupt controller(AIC) */
  341. for (i = 0; i < MTK_IRQ_NUM; i++) {
  342. err = mtk_aic_init(cryp, i);
  343. if (err) {
  344. dev_err(cryp->dev, "Failed to initialize AIC.\n");
  345. return err;
  346. }
  347. }
  348. /* Initialize packet engine */
  349. err = mtk_packet_engine_setup(cryp);
  350. if (err) {
  351. dev_err(cryp->dev, "Failed to configure packet engine.\n");
  352. return err;
  353. }
  354. return 0;
  355. }
  356. static void mtk_desc_dma_free(struct mtk_cryp *cryp)
  357. {
  358. int i;
  359. for (i = 0; i < MTK_RING_MAX; i++) {
  360. dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  361. cryp->ring[i]->res_base,
  362. cryp->ring[i]->res_dma);
  363. dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  364. cryp->ring[i]->cmd_base,
  365. cryp->ring[i]->cmd_dma);
  366. kfree(cryp->ring[i]);
  367. }
  368. }
  369. static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
  370. {
  371. struct mtk_ring **ring = cryp->ring;
  372. int i;
  373. for (i = 0; i < MTK_RING_MAX; i++) {
  374. ring[i] = kzalloc(sizeof(**ring), GFP_KERNEL);
  375. if (!ring[i])
  376. goto err_cleanup;
  377. ring[i]->cmd_base = dma_alloc_coherent(cryp->dev,
  378. MTK_DESC_RING_SZ,
  379. &ring[i]->cmd_dma,
  380. GFP_KERNEL);
  381. if (!ring[i]->cmd_base)
  382. goto err_cleanup;
  383. ring[i]->res_base = dma_alloc_coherent(cryp->dev,
  384. MTK_DESC_RING_SZ,
  385. &ring[i]->res_dma,
  386. GFP_KERNEL);
  387. if (!ring[i]->res_base)
  388. goto err_cleanup;
  389. ring[i]->cmd_next = ring[i]->cmd_base;
  390. ring[i]->res_next = ring[i]->res_base;
  391. }
  392. return 0;
  393. err_cleanup:
  394. do {
  395. dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  396. ring[i]->res_base, ring[i]->res_dma);
  397. dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  398. ring[i]->cmd_base, ring[i]->cmd_dma);
  399. kfree(ring[i]);
  400. } while (i--);
  401. return -ENOMEM;
  402. }
  403. static int mtk_crypto_probe(struct platform_device *pdev)
  404. {
  405. struct mtk_cryp *cryp;
  406. int i, err;
  407. cryp = devm_kzalloc(&pdev->dev, sizeof(*cryp), GFP_KERNEL);
  408. if (!cryp)
  409. return -ENOMEM;
  410. cryp->base = devm_platform_ioremap_resource(pdev, 0);
  411. if (IS_ERR(cryp->base))
  412. return PTR_ERR(cryp->base);
  413. for (i = 0; i < MTK_IRQ_NUM; i++) {
  414. cryp->irq[i] = platform_get_irq(pdev, i);
  415. if (cryp->irq[i] < 0)
  416. return cryp->irq[i];
  417. }
  418. cryp->clk_cryp = devm_clk_get(&pdev->dev, "cryp");
  419. if (IS_ERR(cryp->clk_cryp))
  420. return -EPROBE_DEFER;
  421. cryp->dev = &pdev->dev;
  422. pm_runtime_enable(cryp->dev);
  423. pm_runtime_get_sync(cryp->dev);
  424. err = clk_prepare_enable(cryp->clk_cryp);
  425. if (err)
  426. goto err_clk_cryp;
  427. /* Allocate four command/result descriptor rings */
  428. err = mtk_desc_ring_alloc(cryp);
  429. if (err) {
  430. dev_err(cryp->dev, "Unable to allocate descriptor rings.\n");
  431. goto err_resource;
  432. }
  433. /* Initialize hardware modules */
  434. err = mtk_accelerator_init(cryp);
  435. if (err) {
  436. dev_err(cryp->dev, "Failed to initialize cryptographic engine.\n");
  437. goto err_engine;
  438. }
  439. err = mtk_cipher_alg_register(cryp);
  440. if (err) {
  441. dev_err(cryp->dev, "Unable to register cipher algorithm.\n");
  442. goto err_cipher;
  443. }
  444. err = mtk_hash_alg_register(cryp);
  445. if (err) {
  446. dev_err(cryp->dev, "Unable to register hash algorithm.\n");
  447. goto err_hash;
  448. }
  449. platform_set_drvdata(pdev, cryp);
  450. return 0;
  451. err_hash:
  452. mtk_cipher_alg_release(cryp);
  453. err_cipher:
  454. mtk_dfe_dse_reset(cryp);
  455. err_engine:
  456. mtk_desc_dma_free(cryp);
  457. err_resource:
  458. clk_disable_unprepare(cryp->clk_cryp);
  459. err_clk_cryp:
  460. pm_runtime_put_sync(cryp->dev);
  461. pm_runtime_disable(cryp->dev);
  462. return err;
  463. }
  464. static int mtk_crypto_remove(struct platform_device *pdev)
  465. {
  466. struct mtk_cryp *cryp = platform_get_drvdata(pdev);
  467. mtk_hash_alg_release(cryp);
  468. mtk_cipher_alg_release(cryp);
  469. mtk_desc_dma_free(cryp);
  470. clk_disable_unprepare(cryp->clk_cryp);
  471. pm_runtime_put_sync(cryp->dev);
  472. pm_runtime_disable(cryp->dev);
  473. platform_set_drvdata(pdev, NULL);
  474. return 0;
  475. }
  476. static const struct of_device_id of_crypto_id[] = {
  477. { .compatible = "mediatek,eip97-crypto" },
  478. {},
  479. };
  480. MODULE_DEVICE_TABLE(of, of_crypto_id);
  481. static struct platform_driver mtk_crypto_driver = {
  482. .probe = mtk_crypto_probe,
  483. .remove = mtk_crypto_remove,
  484. .driver = {
  485. .name = "mtk-crypto",
  486. .of_match_table = of_crypto_id,
  487. },
  488. };
  489. module_platform_driver(mtk_crypto_driver);
  490. MODULE_LICENSE("GPL");
  491. MODULE_AUTHOR("Ryder Lee <ryder.lee@mediatek.com>");
  492. MODULE_DESCRIPTION("Cryptographic accelerator driver for EIP97");