Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA) |
| 3 | * that can be found on the following platform: Orion, Kirkwood, Armada. This |
| 4 | * driver supports the TDMA engine on platforms on which it is available. |
| 5 | * |
| 6 | * Author: Boris Brezillon <boris.brezillon@free-electrons.com> |
| 7 | * Author: Arnaud Ebalard <arno@natisbad.org> |
| 8 | * |
| 9 | * This work is based on an initial version written by |
| 10 | * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc > |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License version 2 as published |
| 14 | * by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/genalloc.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/kthread.h> |
| 22 | #include <linux/mbus.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_platform.h> |
| 30 | #include <linux/of_irq.h> |
| 31 | |
| 32 | #include "cesa.h" |
| 33 | |
Romain Perier | e26df73 | 2016-06-21 10:08:31 +0200 | [diff] [blame] | 34 | /* Limit of the crypto queue before reaching the backlog */ |
Romain Perier | 47a1f0b | 2016-06-21 10:08:40 +0200 | [diff] [blame^] | 35 | #define CESA_CRYPTO_DEFAULT_MAX_QLEN 128 |
Romain Perier | e26df73 | 2016-06-21 10:08:31 +0200 | [diff] [blame] | 36 | |
Boris BREZILLON | 64c55d4 | 2015-06-18 15:46:27 +0200 | [diff] [blame] | 37 | static int allhwsupport = !IS_ENABLED(CONFIG_CRYPTO_DEV_MV_CESA); |
| 38 | module_param_named(allhwsupport, allhwsupport, int, 0444); |
| 39 | MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the mv_cesa driver)"); |
| 40 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 41 | struct mv_cesa_dev *cesa_dev; |
| 42 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 43 | struct crypto_async_request * |
| 44 | mv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine, |
| 45 | struct crypto_async_request **backlog) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 46 | { |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 47 | struct crypto_async_request *req; |
| 48 | |
| 49 | *backlog = crypto_get_backlog(&engine->queue); |
| 50 | req = crypto_dequeue_request(&engine->queue); |
| 51 | |
| 52 | if (!req) |
| 53 | return NULL; |
| 54 | |
| 55 | return req; |
| 56 | } |
| 57 | |
| 58 | static void mv_cesa_rearm_engine(struct mv_cesa_engine *engine) |
| 59 | { |
| 60 | struct crypto_async_request *req = NULL, *backlog = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 61 | struct mv_cesa_ctx *ctx; |
| 62 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 63 | |
| 64 | spin_lock_bh(&engine->lock); |
| 65 | if (!engine->req) { |
| 66 | req = mv_cesa_dequeue_req_locked(engine, &backlog); |
| 67 | engine->req = req; |
| 68 | } |
| 69 | spin_unlock_bh(&engine->lock); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 70 | |
| 71 | if (!req) |
| 72 | return; |
| 73 | |
| 74 | if (backlog) |
| 75 | backlog->complete(backlog, -EINPROGRESS); |
| 76 | |
| 77 | ctx = crypto_tfm_ctx(req->tfm); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 78 | ctx->ops->step(req); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | static int mv_cesa_std_process(struct mv_cesa_engine *engine, u32 status) |
| 84 | { |
| 85 | struct crypto_async_request *req; |
| 86 | struct mv_cesa_ctx *ctx; |
| 87 | int res; |
| 88 | |
| 89 | req = engine->req; |
| 90 | ctx = crypto_tfm_ctx(req->tfm); |
| 91 | res = ctx->ops->process(req, status); |
| 92 | |
| 93 | if (res == 0) { |
| 94 | ctx->ops->complete(req); |
| 95 | mv_cesa_engine_enqueue_complete_request(engine, req); |
| 96 | } else if (res == -EINPROGRESS) { |
| 97 | ctx->ops->step(req); |
| 98 | } |
| 99 | |
| 100 | return res; |
| 101 | } |
| 102 | |
| 103 | static int mv_cesa_int_process(struct mv_cesa_engine *engine, u32 status) |
| 104 | { |
| 105 | if (engine->chain.first && engine->chain.last) |
| 106 | return mv_cesa_tdma_process(engine, status); |
| 107 | |
| 108 | return mv_cesa_std_process(engine, status); |
| 109 | } |
| 110 | |
| 111 | static inline void |
| 112 | mv_cesa_complete_req(struct mv_cesa_ctx *ctx, struct crypto_async_request *req, |
| 113 | int res) |
| 114 | { |
| 115 | ctx->ops->cleanup(req); |
| 116 | local_bh_disable(); |
| 117 | req->complete(req, res); |
| 118 | local_bh_enable(); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static irqreturn_t mv_cesa_int(int irq, void *priv) |
| 122 | { |
| 123 | struct mv_cesa_engine *engine = priv; |
| 124 | struct crypto_async_request *req; |
| 125 | struct mv_cesa_ctx *ctx; |
| 126 | u32 status, mask; |
| 127 | irqreturn_t ret = IRQ_NONE; |
| 128 | |
| 129 | while (true) { |
| 130 | int res; |
| 131 | |
| 132 | mask = mv_cesa_get_int_mask(engine); |
| 133 | status = readl(engine->regs + CESA_SA_INT_STATUS); |
| 134 | |
| 135 | if (!(status & mask)) |
| 136 | break; |
| 137 | |
| 138 | /* |
| 139 | * TODO: avoid clearing the FPGA_INT_STATUS if this not |
| 140 | * relevant on some platforms. |
| 141 | */ |
| 142 | writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS); |
| 143 | writel(~status, engine->regs + CESA_SA_INT_STATUS); |
| 144 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 145 | /* Process fetched requests */ |
| 146 | res = mv_cesa_int_process(engine, status & mask); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 147 | ret = IRQ_HANDLED; |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 148 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 149 | spin_lock_bh(&engine->lock); |
| 150 | req = engine->req; |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 151 | if (res != -EINPROGRESS) |
| 152 | engine->req = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 153 | spin_unlock_bh(&engine->lock); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 154 | |
| 155 | ctx = crypto_tfm_ctx(req->tfm); |
| 156 | |
| 157 | if (res && res != -EINPROGRESS) |
| 158 | mv_cesa_complete_req(ctx, req, res); |
| 159 | |
| 160 | /* Launch the next pending request */ |
| 161 | mv_cesa_rearm_engine(engine); |
| 162 | |
| 163 | /* Iterate over the complete queue */ |
| 164 | while (true) { |
| 165 | req = mv_cesa_engine_dequeue_complete_request(engine); |
| 166 | if (!req) |
| 167 | break; |
| 168 | |
| 169 | mv_cesa_complete_req(ctx, req, 0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 176 | int mv_cesa_queue_req(struct crypto_async_request *req, |
| 177 | struct mv_cesa_req *creq) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 178 | { |
| 179 | int ret; |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 180 | struct mv_cesa_engine *engine = creq->engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 181 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 182 | spin_lock_bh(&engine->lock); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 183 | if (mv_cesa_req_get_type(creq) == CESA_DMA_REQ) |
| 184 | mv_cesa_tdma_chain(engine, creq); |
| 185 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 186 | ret = crypto_enqueue_request(&engine->queue, req); |
| 187 | spin_unlock_bh(&engine->lock); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 188 | |
| 189 | if (ret != -EINPROGRESS) |
| 190 | return ret; |
| 191 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 192 | mv_cesa_rearm_engine(engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 193 | |
| 194 | return -EINPROGRESS; |
| 195 | } |
| 196 | |
| 197 | static int mv_cesa_add_algs(struct mv_cesa_dev *cesa) |
| 198 | { |
| 199 | int ret; |
| 200 | int i, j; |
| 201 | |
| 202 | for (i = 0; i < cesa->caps->ncipher_algs; i++) { |
| 203 | ret = crypto_register_alg(cesa->caps->cipher_algs[i]); |
| 204 | if (ret) |
| 205 | goto err_unregister_crypto; |
| 206 | } |
| 207 | |
| 208 | for (i = 0; i < cesa->caps->nahash_algs; i++) { |
| 209 | ret = crypto_register_ahash(cesa->caps->ahash_algs[i]); |
| 210 | if (ret) |
| 211 | goto err_unregister_ahash; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | |
| 216 | err_unregister_ahash: |
| 217 | for (j = 0; j < i; j++) |
| 218 | crypto_unregister_ahash(cesa->caps->ahash_algs[j]); |
| 219 | i = cesa->caps->ncipher_algs; |
| 220 | |
| 221 | err_unregister_crypto: |
| 222 | for (j = 0; j < i; j++) |
| 223 | crypto_unregister_alg(cesa->caps->cipher_algs[j]); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < cesa->caps->nahash_algs; i++) |
| 233 | crypto_unregister_ahash(cesa->caps->ahash_algs[i]); |
| 234 | |
| 235 | for (i = 0; i < cesa->caps->ncipher_algs; i++) |
| 236 | crypto_unregister_alg(cesa->caps->cipher_algs[i]); |
| 237 | } |
| 238 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 239 | static struct crypto_alg *orion_cipher_algs[] = { |
| 240 | &mv_cesa_ecb_des_alg, |
| 241 | &mv_cesa_cbc_des_alg, |
| 242 | &mv_cesa_ecb_des3_ede_alg, |
| 243 | &mv_cesa_cbc_des3_ede_alg, |
| 244 | &mv_cesa_ecb_aes_alg, |
| 245 | &mv_cesa_cbc_aes_alg, |
| 246 | }; |
| 247 | |
| 248 | static struct ahash_alg *orion_ahash_algs[] = { |
| 249 | &mv_md5_alg, |
| 250 | &mv_sha1_alg, |
| 251 | &mv_ahmac_md5_alg, |
| 252 | &mv_ahmac_sha1_alg, |
| 253 | }; |
| 254 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 255 | static struct crypto_alg *armada_370_cipher_algs[] = { |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 256 | &mv_cesa_ecb_des_alg, |
| 257 | &mv_cesa_cbc_des_alg, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 258 | &mv_cesa_ecb_des3_ede_alg, |
| 259 | &mv_cesa_cbc_des3_ede_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 260 | &mv_cesa_ecb_aes_alg, |
| 261 | &mv_cesa_cbc_aes_alg, |
| 262 | }; |
| 263 | |
| 264 | static struct ahash_alg *armada_370_ahash_algs[] = { |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 265 | &mv_md5_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 266 | &mv_sha1_alg, |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 267 | &mv_sha256_alg, |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 268 | &mv_ahmac_md5_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 269 | &mv_ahmac_sha1_alg, |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 270 | &mv_ahmac_sha256_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 271 | }; |
| 272 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 273 | static const struct mv_cesa_caps orion_caps = { |
| 274 | .nengines = 1, |
| 275 | .cipher_algs = orion_cipher_algs, |
| 276 | .ncipher_algs = ARRAY_SIZE(orion_cipher_algs), |
| 277 | .ahash_algs = orion_ahash_algs, |
| 278 | .nahash_algs = ARRAY_SIZE(orion_ahash_algs), |
| 279 | .has_tdma = false, |
| 280 | }; |
| 281 | |
Arnaud Ebalard | 7240425 | 2015-06-18 15:46:29 +0200 | [diff] [blame] | 282 | static const struct mv_cesa_caps kirkwood_caps = { |
| 283 | .nengines = 1, |
| 284 | .cipher_algs = orion_cipher_algs, |
| 285 | .ncipher_algs = ARRAY_SIZE(orion_cipher_algs), |
| 286 | .ahash_algs = orion_ahash_algs, |
| 287 | .nahash_algs = ARRAY_SIZE(orion_ahash_algs), |
| 288 | .has_tdma = true, |
| 289 | }; |
| 290 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 291 | static const struct mv_cesa_caps armada_370_caps = { |
| 292 | .nengines = 1, |
| 293 | .cipher_algs = armada_370_cipher_algs, |
| 294 | .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs), |
| 295 | .ahash_algs = armada_370_ahash_algs, |
| 296 | .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs), |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 297 | .has_tdma = true, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 298 | }; |
| 299 | |
Boris BREZILLON | 898c9d5 | 2015-06-18 15:46:26 +0200 | [diff] [blame] | 300 | static const struct mv_cesa_caps armada_xp_caps = { |
| 301 | .nengines = 2, |
| 302 | .cipher_algs = armada_370_cipher_algs, |
| 303 | .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs), |
| 304 | .ahash_algs = armada_370_ahash_algs, |
| 305 | .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs), |
| 306 | .has_tdma = true, |
| 307 | }; |
| 308 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 309 | static const struct of_device_id mv_cesa_of_match_table[] = { |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 310 | { .compatible = "marvell,orion-crypto", .data = &orion_caps }, |
Arnaud Ebalard | 7240425 | 2015-06-18 15:46:29 +0200 | [diff] [blame] | 311 | { .compatible = "marvell,kirkwood-crypto", .data = &kirkwood_caps }, |
| 312 | { .compatible = "marvell,dove-crypto", .data = &kirkwood_caps }, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 313 | { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps }, |
Boris BREZILLON | 898c9d5 | 2015-06-18 15:46:26 +0200 | [diff] [blame] | 314 | { .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps }, |
| 315 | { .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps }, |
| 316 | { .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps }, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 317 | {} |
| 318 | }; |
| 319 | MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table); |
| 320 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 321 | static void |
| 322 | mv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine, |
| 323 | const struct mbus_dram_target_info *dram) |
| 324 | { |
| 325 | void __iomem *iobase = engine->regs; |
| 326 | int i; |
| 327 | |
| 328 | for (i = 0; i < 4; i++) { |
| 329 | writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i)); |
| 330 | writel(0, iobase + CESA_TDMA_WINDOW_BASE(i)); |
| 331 | } |
| 332 | |
| 333 | for (i = 0; i < dram->num_cs; i++) { |
| 334 | const struct mbus_dram_window *cs = dram->cs + i; |
| 335 | |
| 336 | writel(((cs->size - 1) & 0xffff0000) | |
| 337 | (cs->mbus_attr << 8) | |
| 338 | (dram->mbus_dram_target_id << 4) | 1, |
| 339 | iobase + CESA_TDMA_WINDOW_CTRL(i)); |
| 340 | writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i)); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa) |
| 345 | { |
| 346 | struct device *dev = cesa->dev; |
| 347 | struct mv_cesa_dev_dma *dma; |
| 348 | |
| 349 | if (!cesa->caps->has_tdma) |
| 350 | return 0; |
| 351 | |
| 352 | dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL); |
| 353 | if (!dma) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev, |
| 357 | sizeof(struct mv_cesa_tdma_desc), |
| 358 | 16, 0); |
| 359 | if (!dma->tdma_desc_pool) |
| 360 | return -ENOMEM; |
| 361 | |
| 362 | dma->op_pool = dmam_pool_create("cesa_op", dev, |
| 363 | sizeof(struct mv_cesa_op_ctx), 16, 0); |
| 364 | if (!dma->op_pool) |
| 365 | return -ENOMEM; |
| 366 | |
| 367 | dma->cache_pool = dmam_pool_create("cesa_cache", dev, |
| 368 | CESA_MAX_HASH_BLOCK_SIZE, 1, 0); |
| 369 | if (!dma->cache_pool) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0); |
Boris BREZILLON | 8a3978a | 2016-02-05 17:45:48 +0100 | [diff] [blame] | 373 | if (!dma->padding_pool) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 374 | return -ENOMEM; |
| 375 | |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 376 | dma->iv_pool = dmam_pool_create("cesa_iv", dev, 16, 1, 0); |
| 377 | if (!dma->iv_pool) |
| 378 | return -ENOMEM; |
| 379 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 380 | cesa->dma = dma; |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 385 | static int mv_cesa_get_sram(struct platform_device *pdev, int idx) |
| 386 | { |
| 387 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 388 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 389 | const char *res_name = "sram"; |
| 390 | struct resource *res; |
| 391 | |
Vladimir Zapolskiy | abdd4a7 | 2015-06-30 15:00:07 -0700 | [diff] [blame] | 392 | engine->pool = of_gen_pool_get(cesa->dev->of_node, |
| 393 | "marvell,crypto-srams", idx); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 394 | if (engine->pool) { |
| 395 | engine->sram = gen_pool_dma_alloc(engine->pool, |
| 396 | cesa->sram_size, |
| 397 | &engine->sram_dma); |
| 398 | if (engine->sram) |
| 399 | return 0; |
| 400 | |
| 401 | engine->pool = NULL; |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
| 405 | if (cesa->caps->nengines > 1) { |
| 406 | if (!idx) |
| 407 | res_name = "sram0"; |
| 408 | else |
| 409 | res_name = "sram1"; |
| 410 | } |
| 411 | |
| 412 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 413 | res_name); |
| 414 | if (!res || resource_size(res) < cesa->sram_size) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | engine->sram = devm_ioremap_resource(cesa->dev, res); |
| 418 | if (IS_ERR(engine->sram)) |
| 419 | return PTR_ERR(engine->sram); |
| 420 | |
| 421 | engine->sram_dma = phys_to_dma(cesa->dev, |
| 422 | (phys_addr_t)res->start); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static void mv_cesa_put_sram(struct platform_device *pdev, int idx) |
| 428 | { |
| 429 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 430 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 431 | |
| 432 | if (!engine->pool) |
| 433 | return; |
| 434 | |
| 435 | gen_pool_free(engine->pool, (unsigned long)engine->sram, |
| 436 | cesa->sram_size); |
| 437 | } |
| 438 | |
| 439 | static int mv_cesa_probe(struct platform_device *pdev) |
| 440 | { |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 441 | const struct mv_cesa_caps *caps = &orion_caps; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 442 | const struct mbus_dram_target_info *dram; |
| 443 | const struct of_device_id *match; |
| 444 | struct device *dev = &pdev->dev; |
| 445 | struct mv_cesa_dev *cesa; |
| 446 | struct mv_cesa_engine *engines; |
| 447 | struct resource *res; |
| 448 | int irq, ret, i; |
| 449 | u32 sram_size; |
| 450 | |
| 451 | if (cesa_dev) { |
| 452 | dev_err(&pdev->dev, "Only one CESA device authorized\n"); |
| 453 | return -EEXIST; |
| 454 | } |
| 455 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 456 | if (dev->of_node) { |
| 457 | match = of_match_node(mv_cesa_of_match_table, dev->of_node); |
| 458 | if (!match || !match->data) |
| 459 | return -ENOTSUPP; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 460 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 461 | caps = match->data; |
| 462 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 463 | |
Arnaud Ebalard | 7240425 | 2015-06-18 15:46:29 +0200 | [diff] [blame] | 464 | if ((caps == &orion_caps || caps == &kirkwood_caps) && !allhwsupport) |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 465 | return -ENOTSUPP; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 466 | |
| 467 | cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL); |
| 468 | if (!cesa) |
| 469 | return -ENOMEM; |
| 470 | |
| 471 | cesa->caps = caps; |
| 472 | cesa->dev = dev; |
| 473 | |
| 474 | sram_size = CESA_SA_DEFAULT_SRAM_SIZE; |
| 475 | of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size", |
| 476 | &sram_size); |
| 477 | if (sram_size < CESA_SA_MIN_SRAM_SIZE) |
| 478 | sram_size = CESA_SA_MIN_SRAM_SIZE; |
| 479 | |
| 480 | cesa->sram_size = sram_size; |
| 481 | cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines), |
| 482 | GFP_KERNEL); |
| 483 | if (!cesa->engines) |
| 484 | return -ENOMEM; |
| 485 | |
| 486 | spin_lock_init(&cesa->lock); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 487 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 488 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs"); |
| 489 | cesa->regs = devm_ioremap_resource(dev, res); |
| 490 | if (IS_ERR(cesa->regs)) |
Boris BREZILLON | dfe97ad | 2016-03-17 10:47:10 +0100 | [diff] [blame] | 491 | return PTR_ERR(cesa->regs); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 492 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 493 | ret = mv_cesa_dev_dma_init(cesa); |
| 494 | if (ret) |
| 495 | return ret; |
| 496 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 497 | dram = mv_mbus_dram_info_nooverlap(); |
| 498 | |
| 499 | platform_set_drvdata(pdev, cesa); |
| 500 | |
| 501 | for (i = 0; i < caps->nengines; i++) { |
| 502 | struct mv_cesa_engine *engine = &cesa->engines[i]; |
| 503 | char res_name[7]; |
| 504 | |
| 505 | engine->id = i; |
| 506 | spin_lock_init(&engine->lock); |
| 507 | |
| 508 | ret = mv_cesa_get_sram(pdev, i); |
| 509 | if (ret) |
| 510 | goto err_cleanup; |
| 511 | |
| 512 | irq = platform_get_irq(pdev, i); |
| 513 | if (irq < 0) { |
| 514 | ret = irq; |
| 515 | goto err_cleanup; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Not all platforms can gate the CESA clocks: do not complain |
| 520 | * if the clock does not exist. |
| 521 | */ |
| 522 | snprintf(res_name, sizeof(res_name), "cesa%d", i); |
| 523 | engine->clk = devm_clk_get(dev, res_name); |
| 524 | if (IS_ERR(engine->clk)) { |
| 525 | engine->clk = devm_clk_get(dev, NULL); |
| 526 | if (IS_ERR(engine->clk)) |
| 527 | engine->clk = NULL; |
| 528 | } |
| 529 | |
| 530 | snprintf(res_name, sizeof(res_name), "cesaz%d", i); |
| 531 | engine->zclk = devm_clk_get(dev, res_name); |
| 532 | if (IS_ERR(engine->zclk)) |
| 533 | engine->zclk = NULL; |
| 534 | |
| 535 | ret = clk_prepare_enable(engine->clk); |
| 536 | if (ret) |
| 537 | goto err_cleanup; |
| 538 | |
| 539 | ret = clk_prepare_enable(engine->zclk); |
| 540 | if (ret) |
| 541 | goto err_cleanup; |
| 542 | |
| 543 | engine->regs = cesa->regs + CESA_ENGINE_OFF(i); |
| 544 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 545 | if (dram && cesa->caps->has_tdma) |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 546 | mv_cesa_conf_mbus_windows(engine, dram); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 547 | |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 548 | writel(0, engine->regs + CESA_SA_INT_STATUS); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 549 | writel(CESA_SA_CFG_STOP_DIG_ERR, |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 550 | engine->regs + CESA_SA_CFG); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 551 | writel(engine->sram_dma & CESA_SA_SRAM_MSK, |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 552 | engine->regs + CESA_SA_DESC_P0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 553 | |
| 554 | ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int, |
| 555 | IRQF_ONESHOT, |
| 556 | dev_name(&pdev->dev), |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 557 | engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 558 | if (ret) |
| 559 | goto err_cleanup; |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 560 | |
| 561 | crypto_init_queue(&engine->queue, CESA_CRYPTO_DEFAULT_MAX_QLEN); |
| 562 | atomic_set(&engine->load, 0); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 563 | INIT_LIST_HEAD(&engine->complete_queue); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | cesa_dev = cesa; |
| 567 | |
| 568 | ret = mv_cesa_add_algs(cesa); |
| 569 | if (ret) { |
| 570 | cesa_dev = NULL; |
| 571 | goto err_cleanup; |
| 572 | } |
| 573 | |
| 574 | dev_info(dev, "CESA device successfully registered\n"); |
| 575 | |
| 576 | return 0; |
| 577 | |
| 578 | err_cleanup: |
| 579 | for (i = 0; i < caps->nengines; i++) { |
| 580 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 581 | clk_disable_unprepare(cesa->engines[i].clk); |
| 582 | mv_cesa_put_sram(pdev, i); |
| 583 | } |
| 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | static int mv_cesa_remove(struct platform_device *pdev) |
| 589 | { |
| 590 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 591 | int i; |
| 592 | |
| 593 | mv_cesa_remove_algs(cesa); |
| 594 | |
| 595 | for (i = 0; i < cesa->caps->nengines; i++) { |
| 596 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 597 | clk_disable_unprepare(cesa->engines[i].clk); |
| 598 | mv_cesa_put_sram(pdev, i); |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static struct platform_driver marvell_cesa = { |
| 605 | .probe = mv_cesa_probe, |
| 606 | .remove = mv_cesa_remove, |
| 607 | .driver = { |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 608 | .name = "marvell-cesa", |
| 609 | .of_match_table = mv_cesa_of_match_table, |
| 610 | }, |
| 611 | }; |
| 612 | module_platform_driver(marvell_cesa); |
| 613 | |
| 614 | MODULE_ALIAS("platform:mv_crypto"); |
| 615 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); |
| 616 | MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>"); |
| 617 | MODULE_DESCRIPTION("Support for Marvell's cryptographic engine"); |
| 618 | MODULE_LICENSE("GPL v2"); |