1 // SPDX-License-Identifier: GPL-2.0
5 * Support for StarFive hardware cryptographic engine.
6 * Copyright (c) 2022 StarFive Technology
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
20 #include "jh7110-cryp.h"
22 #define DRIVER_NAME "starfive-crypto"
24 struct starfive_dev_list {
25 struct list_head dev_list;
26 spinlock_t lock; /* protect dev_list */
29 static struct starfive_dev_list dev_list = {
30 .dev_list = LIST_HEAD_INIT(dev_list.dev_list),
31 .lock = __SPIN_LOCK_UNLOCKED(dev_list.lock),
34 struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx)
36 struct starfive_cryp_dev *cryp = NULL, *tmp;
38 spin_lock_bh(&dev_list.lock);
40 list_for_each_entry(tmp, &dev_list.dev_list, list) {
49 spin_unlock_bh(&dev_list.lock);
54 static int starfive_dma_init(struct starfive_cryp_dev *cryp)
62 dma_cap_set(DMA_SLAVE, mask);
64 cryp->tx = dma_request_chan(cryp->dev, "tx");
66 return dev_err_probe(cryp->dev, PTR_ERR(cryp->tx),
67 "Error requesting tx dma channel.\n");
69 cryp->rx = dma_request_chan(cryp->dev, "rx");
70 if (IS_ERR(cryp->rx)) {
71 dma_release_channel(cryp->tx);
72 return dev_err_probe(cryp->dev, PTR_ERR(cryp->rx),
73 "Error requesting rx dma channel.\n");
76 init_completion(&cryp->tx_comp);
77 init_completion(&cryp->rx_comp);
82 static void starfive_dma_cleanup(struct starfive_cryp_dev *cryp)
84 dma_release_channel(cryp->tx);
85 dma_release_channel(cryp->rx);
88 static int starfive_cryp_probe(struct platform_device *pdev)
90 struct starfive_cryp_dev *cryp;
94 cryp = devm_kzalloc(&pdev->dev, sizeof(*cryp), GFP_KERNEL);
98 platform_set_drvdata(pdev, cryp);
99 cryp->dev = &pdev->dev;
101 cryp->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
102 if (IS_ERR(cryp->base))
103 return dev_err_probe(&pdev->dev, PTR_ERR(cryp->base),
104 "Error remapping memory for platform device\n");
106 cryp->phys_base = res->start;
107 cryp->dma_maxburst = 32;
109 cryp->hclk = devm_clk_get(&pdev->dev, "hclk");
110 if (IS_ERR(cryp->hclk))
111 return dev_err_probe(&pdev->dev, PTR_ERR(cryp->hclk),
112 "Error getting hardware reference clock\n");
114 cryp->ahb = devm_clk_get(&pdev->dev, "ahb");
115 if (IS_ERR(cryp->ahb))
116 return dev_err_probe(&pdev->dev, PTR_ERR(cryp->ahb),
117 "Error getting ahb reference clock\n");
119 cryp->rst = devm_reset_control_get_shared(cryp->dev, NULL);
120 if (IS_ERR(cryp->rst))
121 return dev_err_probe(&pdev->dev, PTR_ERR(cryp->rst),
122 "Error getting hardware reset line\n");
124 clk_prepare_enable(cryp->hclk);
125 clk_prepare_enable(cryp->ahb);
126 reset_control_deassert(cryp->rst);
128 spin_lock(&dev_list.lock);
129 list_add(&cryp->list, &dev_list.dev_list);
130 spin_unlock(&dev_list.lock);
132 ret = starfive_dma_init(cryp);
136 /* Initialize crypto engine */
137 cryp->engine = crypto_engine_alloc_init(&pdev->dev, 1);
143 ret = crypto_engine_start(cryp->engine);
145 goto err_engine_start;
150 crypto_engine_exit(cryp->engine);
152 starfive_dma_cleanup(cryp);
154 spin_lock(&dev_list.lock);
155 list_del(&cryp->list);
156 spin_unlock(&dev_list.lock);
161 static int starfive_cryp_remove(struct platform_device *pdev)
163 struct starfive_cryp_dev *cryp = platform_get_drvdata(pdev);
168 crypto_engine_stop(cryp->engine);
169 crypto_engine_exit(cryp->engine);
171 starfive_dma_cleanup(cryp);
173 spin_lock(&dev_list.lock);
174 list_del(&cryp->list);
175 spin_unlock(&dev_list.lock);
177 clk_disable_unprepare(cryp->hclk);
178 clk_disable_unprepare(cryp->ahb);
179 reset_control_assert(cryp->rst);
184 static const struct of_device_id starfive_dt_ids[] __maybe_unused = {
185 { .compatible = "starfive,jh7110-crypto", .data = NULL},
188 MODULE_DEVICE_TABLE(of, starfive_dt_ids);
190 static struct platform_driver starfive_cryp_driver = {
191 .probe = starfive_cryp_probe,
192 .remove = starfive_cryp_remove,
195 .of_match_table = starfive_dt_ids,
199 module_platform_driver(starfive_cryp_driver);
201 MODULE_LICENSE("GPL");
202 MODULE_DESCRIPTION("StarFive Cryptographic Module");