2 * Crypto acceleration support for Rockchip RK3288
4 * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
6 * Author: Zain Wang <zain.wang@rock-chips.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * Some ideas are from marvell-cesa.c and s5p-sss.c driver.
15 #include "rk3288_crypto.h"
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/crypto.h>
21 #include <linux/reset.h>
23 static int rk_crypto_enable_clk(struct rk_crypto_info
*dev
)
27 err
= clk_prepare_enable(dev
->sclk
);
29 dev_err(dev
->dev
, "[%s:%d], Couldn't enable clock sclk\n",
33 err
= clk_prepare_enable(dev
->aclk
);
35 dev_err(dev
->dev
, "[%s:%d], Couldn't enable clock aclk\n",
39 err
= clk_prepare_enable(dev
->hclk
);
41 dev_err(dev
->dev
, "[%s:%d], Couldn't enable clock hclk\n",
45 err
= clk_prepare_enable(dev
->dmaclk
);
47 dev_err(dev
->dev
, "[%s:%d], Couldn't enable clock dmaclk\n",
53 clk_disable_unprepare(dev
->hclk
);
55 clk_disable_unprepare(dev
->aclk
);
57 clk_disable_unprepare(dev
->sclk
);
62 static void rk_crypto_disable_clk(struct rk_crypto_info
*dev
)
64 clk_disable_unprepare(dev
->dmaclk
);
65 clk_disable_unprepare(dev
->hclk
);
66 clk_disable_unprepare(dev
->aclk
);
67 clk_disable_unprepare(dev
->sclk
);
70 static int check_alignment(struct scatterlist
*sg_src
,
71 struct scatterlist
*sg_dst
,
76 in
= IS_ALIGNED((uint32_t)sg_src
->offset
, 4) &&
77 IS_ALIGNED((uint32_t)sg_src
->length
, align_mask
);
80 out
= IS_ALIGNED((uint32_t)sg_dst
->offset
, 4) &&
81 IS_ALIGNED((uint32_t)sg_dst
->length
, align_mask
);
84 return (align
&& (sg_src
->length
== sg_dst
->length
));
87 static int rk_load_data(struct rk_crypto_info
*dev
,
88 struct scatterlist
*sg_src
,
89 struct scatterlist
*sg_dst
)
93 dev
->aligned
= dev
->aligned
?
94 check_alignment(sg_src
, sg_dst
, dev
->align_size
) :
97 count
= min(dev
->left_bytes
, sg_src
->length
);
98 dev
->left_bytes
-= count
;
100 if (!dma_map_sg(dev
->dev
, sg_src
, 1, DMA_TO_DEVICE
)) {
101 dev_err(dev
->dev
, "[%s:%d] dma_map_sg(src) error\n",
105 dev
->addr_in
= sg_dma_address(sg_src
);
108 if (!dma_map_sg(dev
->dev
, sg_dst
, 1, DMA_FROM_DEVICE
)) {
110 "[%s:%d] dma_map_sg(dst) error\n",
112 dma_unmap_sg(dev
->dev
, sg_src
, 1,
116 dev
->addr_out
= sg_dma_address(sg_dst
);
119 count
= (dev
->left_bytes
> PAGE_SIZE
) ?
120 PAGE_SIZE
: dev
->left_bytes
;
122 if (!sg_pcopy_to_buffer(dev
->first
, dev
->nents
,
123 dev
->addr_vir
, count
,
124 dev
->total
- dev
->left_bytes
)) {
125 dev_err(dev
->dev
, "[%s:%d] pcopy err\n",
129 dev
->left_bytes
-= count
;
130 sg_init_one(&dev
->sg_tmp
, dev
->addr_vir
, count
);
131 if (!dma_map_sg(dev
->dev
, &dev
->sg_tmp
, 1, DMA_TO_DEVICE
)) {
132 dev_err(dev
->dev
, "[%s:%d] dma_map_sg(sg_tmp) error\n",
136 dev
->addr_in
= sg_dma_address(&dev
->sg_tmp
);
139 if (!dma_map_sg(dev
->dev
, &dev
->sg_tmp
, 1,
142 "[%s:%d] dma_map_sg(sg_tmp) error\n",
144 dma_unmap_sg(dev
->dev
, &dev
->sg_tmp
, 1,
148 dev
->addr_out
= sg_dma_address(&dev
->sg_tmp
);
155 static void rk_unload_data(struct rk_crypto_info
*dev
)
157 struct scatterlist
*sg_in
, *sg_out
;
159 sg_in
= dev
->aligned
? dev
->sg_src
: &dev
->sg_tmp
;
160 dma_unmap_sg(dev
->dev
, sg_in
, 1, DMA_TO_DEVICE
);
163 sg_out
= dev
->aligned
? dev
->sg_dst
: &dev
->sg_tmp
;
164 dma_unmap_sg(dev
->dev
, sg_out
, 1, DMA_FROM_DEVICE
);
168 static irqreturn_t
rk_crypto_irq_handle(int irq
, void *dev_id
)
170 struct rk_crypto_info
*dev
= platform_get_drvdata(dev_id
);
171 u32 interrupt_status
;
174 spin_lock(&dev
->lock
);
175 interrupt_status
= CRYPTO_READ(dev
, RK_CRYPTO_INTSTS
);
176 CRYPTO_WRITE(dev
, RK_CRYPTO_INTSTS
, interrupt_status
);
177 if (interrupt_status
& 0x0a) {
178 dev_warn(dev
->dev
, "DMA Error\n");
180 } else if (interrupt_status
& 0x05) {
181 err
= dev
->update(dev
);
184 dev
->complete(dev
, err
);
185 spin_unlock(&dev
->lock
);
189 static void rk_crypto_tasklet_cb(unsigned long data
)
191 struct rk_crypto_info
*dev
= (struct rk_crypto_info
*)data
;
192 struct crypto_async_request
*async_req
, *backlog
;
196 spin_lock_irqsave(&dev
->lock
, flags
);
197 backlog
= crypto_get_backlog(&dev
->queue
);
198 async_req
= crypto_dequeue_request(&dev
->queue
);
199 spin_unlock_irqrestore(&dev
->lock
, flags
);
201 dev_err(dev
->dev
, "async_req is NULL !!\n");
205 backlog
->complete(backlog
, -EINPROGRESS
);
209 if (crypto_tfm_alg_type(async_req
->tfm
) == CRYPTO_ALG_TYPE_ABLKCIPHER
)
210 dev
->ablk_req
= ablkcipher_request_cast(async_req
);
212 dev
->ahash_req
= ahash_request_cast(async_req
);
213 err
= dev
->start(dev
);
215 dev
->complete(dev
, err
);
218 static struct rk_crypto_tmp
*rk_cipher_algs
[] = {
223 &rk_ecb_des3_ede_alg
,
224 &rk_cbc_des3_ede_alg
,
230 static int rk_crypto_register(struct rk_crypto_info
*crypto_info
)
235 for (i
= 0; i
< ARRAY_SIZE(rk_cipher_algs
); i
++) {
236 rk_cipher_algs
[i
]->dev
= crypto_info
;
237 if (rk_cipher_algs
[i
]->type
== ALG_TYPE_CIPHER
)
238 err
= crypto_register_alg(
239 &rk_cipher_algs
[i
]->alg
.crypto
);
241 err
= crypto_register_ahash(
242 &rk_cipher_algs
[i
]->alg
.hash
);
244 goto err_cipher_algs
;
249 for (k
= 0; k
< i
; k
++) {
250 if (rk_cipher_algs
[i
]->type
== ALG_TYPE_CIPHER
)
251 crypto_unregister_alg(&rk_cipher_algs
[k
]->alg
.crypto
);
253 crypto_unregister_ahash(&rk_cipher_algs
[i
]->alg
.hash
);
258 static void rk_crypto_unregister(void)
262 for (i
= 0; i
< ARRAY_SIZE(rk_cipher_algs
); i
++) {
263 if (rk_cipher_algs
[i
]->type
== ALG_TYPE_CIPHER
)
264 crypto_unregister_alg(&rk_cipher_algs
[i
]->alg
.crypto
);
266 crypto_unregister_ahash(&rk_cipher_algs
[i
]->alg
.hash
);
270 static void rk_crypto_action(void *data
)
272 struct rk_crypto_info
*crypto_info
= data
;
274 reset_control_assert(crypto_info
->rst
);
277 static const struct of_device_id crypto_of_id_table
[] = {
278 { .compatible
= "rockchip,rk3288-crypto" },
281 MODULE_DEVICE_TABLE(of
, crypto_of_id_table
);
283 static int rk_crypto_probe(struct platform_device
*pdev
)
285 struct resource
*res
;
286 struct device
*dev
= &pdev
->dev
;
287 struct rk_crypto_info
*crypto_info
;
290 crypto_info
= devm_kzalloc(&pdev
->dev
,
291 sizeof(*crypto_info
), GFP_KERNEL
);
297 crypto_info
->rst
= devm_reset_control_get(dev
, "crypto-rst");
298 if (IS_ERR(crypto_info
->rst
)) {
299 err
= PTR_ERR(crypto_info
->rst
);
303 reset_control_assert(crypto_info
->rst
);
304 usleep_range(10, 20);
305 reset_control_deassert(crypto_info
->rst
);
307 err
= devm_add_action_or_reset(dev
, rk_crypto_action
, crypto_info
);
311 spin_lock_init(&crypto_info
->lock
);
313 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
314 crypto_info
->reg
= devm_ioremap_resource(&pdev
->dev
, res
);
315 if (IS_ERR(crypto_info
->reg
)) {
316 err
= PTR_ERR(crypto_info
->reg
);
320 crypto_info
->aclk
= devm_clk_get(&pdev
->dev
, "aclk");
321 if (IS_ERR(crypto_info
->aclk
)) {
322 err
= PTR_ERR(crypto_info
->aclk
);
326 crypto_info
->hclk
= devm_clk_get(&pdev
->dev
, "hclk");
327 if (IS_ERR(crypto_info
->hclk
)) {
328 err
= PTR_ERR(crypto_info
->hclk
);
332 crypto_info
->sclk
= devm_clk_get(&pdev
->dev
, "sclk");
333 if (IS_ERR(crypto_info
->sclk
)) {
334 err
= PTR_ERR(crypto_info
->sclk
);
338 crypto_info
->dmaclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
339 if (IS_ERR(crypto_info
->dmaclk
)) {
340 err
= PTR_ERR(crypto_info
->dmaclk
);
344 crypto_info
->irq
= platform_get_irq(pdev
, 0);
345 if (crypto_info
->irq
< 0) {
346 dev_warn(crypto_info
->dev
,
347 "control Interrupt is not available.\n");
348 err
= crypto_info
->irq
;
352 err
= devm_request_irq(&pdev
->dev
, crypto_info
->irq
,
353 rk_crypto_irq_handle
, IRQF_SHARED
,
357 dev_err(crypto_info
->dev
, "irq request failed.\n");
361 crypto_info
->dev
= &pdev
->dev
;
362 platform_set_drvdata(pdev
, crypto_info
);
364 tasklet_init(&crypto_info
->crypto_tasklet
,
365 rk_crypto_tasklet_cb
, (unsigned long)crypto_info
);
366 crypto_init_queue(&crypto_info
->queue
, 50);
368 crypto_info
->enable_clk
= rk_crypto_enable_clk
;
369 crypto_info
->disable_clk
= rk_crypto_disable_clk
;
370 crypto_info
->load_data
= rk_load_data
;
371 crypto_info
->unload_data
= rk_unload_data
;
373 err
= rk_crypto_register(crypto_info
);
375 dev_err(dev
, "err in register alg");
376 goto err_register_alg
;
379 dev_info(dev
, "Crypto Accelerator successfully registered\n");
383 tasklet_kill(&crypto_info
->crypto_tasklet
);
388 static int rk_crypto_remove(struct platform_device
*pdev
)
390 struct rk_crypto_info
*crypto_tmp
= platform_get_drvdata(pdev
);
392 rk_crypto_unregister();
393 tasklet_kill(&crypto_tmp
->crypto_tasklet
);
397 static struct platform_driver crypto_driver
= {
398 .probe
= rk_crypto_probe
,
399 .remove
= rk_crypto_remove
,
401 .name
= "rk3288-crypto",
402 .of_match_table
= crypto_of_id_table
,
406 module_platform_driver(crypto_driver
);
408 MODULE_AUTHOR("Zain Wang <zain.wang@rock-chips.com>");
409 MODULE_DESCRIPTION("Support for Rockchip's cryptographic engine");
410 MODULE_LICENSE("GPL");