1 // SPDX-License-Identifier: GPL-2.0
3 * amlgoic-core.c - hardware cryptographic offloader for Amlogic GXL SoC
5 * Copyright (C) 2018-2019 Corentin Labbe <clabbe@baylibre.com>
7 * Core file which registers crypto algorithms supported by the hardware.
10 #include <crypto/engine.h>
11 #include <crypto/internal/skcipher.h>
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
23 #include "amlogic-gxl.h"
25 static irqreturn_t
meson_irq_handler(int irq
, void *data
)
27 struct meson_dev
*mc
= (struct meson_dev
*)data
;
31 for (flow
= 0; flow
< MAXFLOW
; flow
++) {
32 if (mc
->irqs
[flow
] == irq
) {
33 p
= readl(mc
->base
+ ((0x04 + flow
) << 2));
35 writel_relaxed(0xF, mc
->base
+ ((0x4 + flow
) << 2));
36 mc
->chanlist
[flow
].status
= 1;
37 complete(&mc
->chanlist
[flow
].complete
);
40 dev_err(mc
->dev
, "%s %d Got irq for flow %d but ctrl is empty\n", __func__
, irq
, flow
);
44 dev_err(mc
->dev
, "%s %d from unknown irq\n", __func__
, irq
);
48 static struct meson_alg_template mc_algs
[] = {
50 .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
51 .blockmode
= MESON_OPMODE_CBC
,
52 .alg
.skcipher
.base
= {
54 .cra_name
= "cbc(aes)",
55 .cra_driver_name
= "cbc-aes-gxl",
57 .cra_blocksize
= AES_BLOCK_SIZE
,
58 .cra_flags
= CRYPTO_ALG_TYPE_SKCIPHER
|
59 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_ALLOCATES_MEMORY
|
60 CRYPTO_ALG_NEED_FALLBACK
,
61 .cra_ctxsize
= sizeof(struct meson_cipher_tfm_ctx
),
62 .cra_module
= THIS_MODULE
,
64 .cra_init
= meson_cipher_init
,
65 .cra_exit
= meson_cipher_exit
,
67 .min_keysize
= AES_MIN_KEY_SIZE
,
68 .max_keysize
= AES_MAX_KEY_SIZE
,
69 .ivsize
= AES_BLOCK_SIZE
,
70 .setkey
= meson_aes_setkey
,
71 .encrypt
= meson_skencrypt
,
72 .decrypt
= meson_skdecrypt
,
75 .do_one_request
= meson_handle_cipher_request
,
79 .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
80 .blockmode
= MESON_OPMODE_ECB
,
81 .alg
.skcipher
.base
= {
83 .cra_name
= "ecb(aes)",
84 .cra_driver_name
= "ecb-aes-gxl",
86 .cra_blocksize
= AES_BLOCK_SIZE
,
87 .cra_flags
= CRYPTO_ALG_TYPE_SKCIPHER
|
88 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_ALLOCATES_MEMORY
|
89 CRYPTO_ALG_NEED_FALLBACK
,
90 .cra_ctxsize
= sizeof(struct meson_cipher_tfm_ctx
),
91 .cra_module
= THIS_MODULE
,
93 .cra_init
= meson_cipher_init
,
94 .cra_exit
= meson_cipher_exit
,
96 .min_keysize
= AES_MIN_KEY_SIZE
,
97 .max_keysize
= AES_MAX_KEY_SIZE
,
98 .setkey
= meson_aes_setkey
,
99 .encrypt
= meson_skencrypt
,
100 .decrypt
= meson_skdecrypt
,
103 .do_one_request
= meson_handle_cipher_request
,
108 static int meson_debugfs_show(struct seq_file
*seq
, void *v
)
110 struct meson_dev
*mc __maybe_unused
= seq
->private;
113 for (i
= 0; i
< MAXFLOW
; i
++)
114 seq_printf(seq
, "Channel %d: nreq %lu\n", i
,
115 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
116 mc
->chanlist
[i
].stat_req
);
121 for (i
= 0; i
< ARRAY_SIZE(mc_algs
); i
++) {
122 switch (mc_algs
[i
].type
) {
123 case CRYPTO_ALG_TYPE_SKCIPHER
:
124 seq_printf(seq
, "%s %s %lu %lu\n",
125 mc_algs
[i
].alg
.skcipher
.base
.base
.cra_driver_name
,
126 mc_algs
[i
].alg
.skcipher
.base
.base
.cra_name
,
127 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
128 mc_algs
[i
].stat_req
, mc_algs
[i
].stat_fb
);
137 DEFINE_SHOW_ATTRIBUTE(meson_debugfs
);
139 static void meson_free_chanlist(struct meson_dev
*mc
, int i
)
142 crypto_engine_exit(mc
->chanlist
[i
].engine
);
143 if (mc
->chanlist
[i
].tl
)
144 dma_free_coherent(mc
->dev
, sizeof(struct meson_desc
) * MAXDESC
,
146 mc
->chanlist
[i
].t_phy
);
152 * Allocate the channel list structure
154 static int meson_allocate_chanlist(struct meson_dev
*mc
)
158 mc
->chanlist
= devm_kcalloc(mc
->dev
, MAXFLOW
,
159 sizeof(struct meson_flow
), GFP_KERNEL
);
163 for (i
= 0; i
< MAXFLOW
; i
++) {
164 init_completion(&mc
->chanlist
[i
].complete
);
166 mc
->chanlist
[i
].engine
= crypto_engine_alloc_init(mc
->dev
, true);
167 if (!mc
->chanlist
[i
].engine
) {
168 dev_err(mc
->dev
, "Cannot allocate engine\n");
173 err
= crypto_engine_start(mc
->chanlist
[i
].engine
);
175 dev_err(mc
->dev
, "Cannot start engine\n");
178 mc
->chanlist
[i
].tl
= dma_alloc_coherent(mc
->dev
,
179 sizeof(struct meson_desc
) * MAXDESC
,
180 &mc
->chanlist
[i
].t_phy
,
182 if (!mc
->chanlist
[i
].tl
) {
189 meson_free_chanlist(mc
, i
);
193 static int meson_register_algs(struct meson_dev
*mc
)
197 for (i
= 0; i
< ARRAY_SIZE(mc_algs
); i
++) {
199 switch (mc_algs
[i
].type
) {
200 case CRYPTO_ALG_TYPE_SKCIPHER
:
201 err
= crypto_engine_register_skcipher(&mc_algs
[i
].alg
.skcipher
);
203 dev_err(mc
->dev
, "Fail to register %s\n",
204 mc_algs
[i
].alg
.skcipher
.base
.base
.cra_name
);
205 mc_algs
[i
].mc
= NULL
;
215 static void meson_unregister_algs(struct meson_dev
*mc
)
219 for (i
= 0; i
< ARRAY_SIZE(mc_algs
); i
++) {
222 switch (mc_algs
[i
].type
) {
223 case CRYPTO_ALG_TYPE_SKCIPHER
:
224 crypto_engine_unregister_skcipher(&mc_algs
[i
].alg
.skcipher
);
230 static int meson_crypto_probe(struct platform_device
*pdev
)
232 struct meson_dev
*mc
;
235 mc
= devm_kzalloc(&pdev
->dev
, sizeof(*mc
), GFP_KERNEL
);
239 mc
->dev
= &pdev
->dev
;
240 platform_set_drvdata(pdev
, mc
);
242 mc
->base
= devm_platform_ioremap_resource(pdev
, 0);
243 if (IS_ERR(mc
->base
))
244 return PTR_ERR(mc
->base
);
246 mc
->busclk
= devm_clk_get(&pdev
->dev
, "blkmv");
247 if (IS_ERR(mc
->busclk
)) {
248 err
= PTR_ERR(mc
->busclk
);
249 dev_err(&pdev
->dev
, "Cannot get core clock err=%d\n", err
);
253 for (i
= 0; i
< MAXFLOW
; i
++) {
254 mc
->irqs
[i
] = platform_get_irq(pdev
, i
);
258 err
= devm_request_irq(&pdev
->dev
, mc
->irqs
[i
], meson_irq_handler
, 0,
261 dev_err(mc
->dev
, "Cannot request IRQ for flow %d\n", i
);
266 err
= clk_prepare_enable(mc
->busclk
);
268 dev_err(&pdev
->dev
, "Cannot prepare_enable busclk\n");
272 err
= meson_allocate_chanlist(mc
);
276 err
= meson_register_algs(mc
);
280 if (IS_ENABLED(CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
)) {
281 struct dentry
*dbgfs_dir
;
283 dbgfs_dir
= debugfs_create_dir("gxl-crypto", NULL
);
284 debugfs_create_file("stats", 0444, dbgfs_dir
, mc
, &meson_debugfs_fops
);
286 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
287 mc
->dbgfs_dir
= dbgfs_dir
;
293 meson_unregister_algs(mc
);
295 meson_free_chanlist(mc
, MAXFLOW
- 1);
296 clk_disable_unprepare(mc
->busclk
);
300 static void meson_crypto_remove(struct platform_device
*pdev
)
302 struct meson_dev
*mc
= platform_get_drvdata(pdev
);
304 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
305 debugfs_remove_recursive(mc
->dbgfs_dir
);
308 meson_unregister_algs(mc
);
310 meson_free_chanlist(mc
, MAXFLOW
- 1);
312 clk_disable_unprepare(mc
->busclk
);
315 static const struct of_device_id meson_crypto_of_match_table
[] = {
316 { .compatible
= "amlogic,gxl-crypto", },
319 MODULE_DEVICE_TABLE(of
, meson_crypto_of_match_table
);
321 static struct platform_driver meson_crypto_driver
= {
322 .probe
= meson_crypto_probe
,
323 .remove
= meson_crypto_remove
,
325 .name
= "gxl-crypto",
326 .of_match_table
= meson_crypto_of_match_table
,
330 module_platform_driver(meson_crypto_driver
);
332 MODULE_DESCRIPTION("Amlogic GXL cryptographic offloader");
333 MODULE_LICENSE("GPL");
334 MODULE_AUTHOR("Corentin Labbe <clabbe@baylibre.com>");