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 | |
| 34 | struct mv_cesa_dev *cesa_dev; |
| 35 | |
| 36 | static void mv_cesa_dequeue_req_unlocked(struct mv_cesa_engine *engine) |
| 37 | { |
| 38 | struct crypto_async_request *req, *backlog; |
| 39 | struct mv_cesa_ctx *ctx; |
| 40 | |
| 41 | spin_lock_bh(&cesa_dev->lock); |
| 42 | backlog = crypto_get_backlog(&cesa_dev->queue); |
| 43 | req = crypto_dequeue_request(&cesa_dev->queue); |
| 44 | engine->req = req; |
| 45 | spin_unlock_bh(&cesa_dev->lock); |
| 46 | |
| 47 | if (!req) |
| 48 | return; |
| 49 | |
| 50 | if (backlog) |
| 51 | backlog->complete(backlog, -EINPROGRESS); |
| 52 | |
| 53 | ctx = crypto_tfm_ctx(req->tfm); |
| 54 | ctx->ops->prepare(req, engine); |
| 55 | ctx->ops->step(req); |
| 56 | } |
| 57 | |
| 58 | static irqreturn_t mv_cesa_int(int irq, void *priv) |
| 59 | { |
| 60 | struct mv_cesa_engine *engine = priv; |
| 61 | struct crypto_async_request *req; |
| 62 | struct mv_cesa_ctx *ctx; |
| 63 | u32 status, mask; |
| 64 | irqreturn_t ret = IRQ_NONE; |
| 65 | |
| 66 | while (true) { |
| 67 | int res; |
| 68 | |
| 69 | mask = mv_cesa_get_int_mask(engine); |
| 70 | status = readl(engine->regs + CESA_SA_INT_STATUS); |
| 71 | |
| 72 | if (!(status & mask)) |
| 73 | break; |
| 74 | |
| 75 | /* |
| 76 | * TODO: avoid clearing the FPGA_INT_STATUS if this not |
| 77 | * relevant on some platforms. |
| 78 | */ |
| 79 | writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS); |
| 80 | writel(~status, engine->regs + CESA_SA_INT_STATUS); |
| 81 | |
| 82 | ret = IRQ_HANDLED; |
| 83 | spin_lock_bh(&engine->lock); |
| 84 | req = engine->req; |
| 85 | spin_unlock_bh(&engine->lock); |
| 86 | if (req) { |
| 87 | ctx = crypto_tfm_ctx(req->tfm); |
| 88 | res = ctx->ops->process(req, status & mask); |
| 89 | if (res != -EINPROGRESS) { |
| 90 | spin_lock_bh(&engine->lock); |
| 91 | engine->req = NULL; |
| 92 | mv_cesa_dequeue_req_unlocked(engine); |
| 93 | spin_unlock_bh(&engine->lock); |
| 94 | ctx->ops->cleanup(req); |
| 95 | local_bh_disable(); |
| 96 | req->complete(req, res); |
| 97 | local_bh_enable(); |
| 98 | } else { |
| 99 | ctx->ops->step(req); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | int mv_cesa_queue_req(struct crypto_async_request *req) |
| 108 | { |
| 109 | int ret; |
| 110 | int i; |
| 111 | |
| 112 | spin_lock_bh(&cesa_dev->lock); |
| 113 | ret = crypto_enqueue_request(&cesa_dev->queue, req); |
| 114 | spin_unlock_bh(&cesa_dev->lock); |
| 115 | |
| 116 | if (ret != -EINPROGRESS) |
| 117 | return ret; |
| 118 | |
| 119 | for (i = 0; i < cesa_dev->caps->nengines; i++) { |
| 120 | spin_lock_bh(&cesa_dev->engines[i].lock); |
| 121 | if (!cesa_dev->engines[i].req) |
| 122 | mv_cesa_dequeue_req_unlocked(&cesa_dev->engines[i]); |
| 123 | spin_unlock_bh(&cesa_dev->engines[i].lock); |
| 124 | } |
| 125 | |
| 126 | return -EINPROGRESS; |
| 127 | } |
| 128 | |
| 129 | static int mv_cesa_add_algs(struct mv_cesa_dev *cesa) |
| 130 | { |
| 131 | int ret; |
| 132 | int i, j; |
| 133 | |
| 134 | for (i = 0; i < cesa->caps->ncipher_algs; i++) { |
| 135 | ret = crypto_register_alg(cesa->caps->cipher_algs[i]); |
| 136 | if (ret) |
| 137 | goto err_unregister_crypto; |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < cesa->caps->nahash_algs; i++) { |
| 141 | ret = crypto_register_ahash(cesa->caps->ahash_algs[i]); |
| 142 | if (ret) |
| 143 | goto err_unregister_ahash; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | |
| 148 | err_unregister_ahash: |
| 149 | for (j = 0; j < i; j++) |
| 150 | crypto_unregister_ahash(cesa->caps->ahash_algs[j]); |
| 151 | i = cesa->caps->ncipher_algs; |
| 152 | |
| 153 | err_unregister_crypto: |
| 154 | for (j = 0; j < i; j++) |
| 155 | crypto_unregister_alg(cesa->caps->cipher_algs[j]); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < cesa->caps->nahash_algs; i++) |
| 165 | crypto_unregister_ahash(cesa->caps->ahash_algs[i]); |
| 166 | |
| 167 | for (i = 0; i < cesa->caps->ncipher_algs; i++) |
| 168 | crypto_unregister_alg(cesa->caps->cipher_algs[i]); |
| 169 | } |
| 170 | |
| 171 | static struct crypto_alg *armada_370_cipher_algs[] = { |
| 172 | &mv_cesa_ecb_aes_alg, |
| 173 | &mv_cesa_cbc_aes_alg, |
| 174 | }; |
| 175 | |
| 176 | static struct ahash_alg *armada_370_ahash_algs[] = { |
| 177 | &mv_sha1_alg, |
| 178 | &mv_ahmac_sha1_alg, |
| 179 | }; |
| 180 | |
| 181 | static const struct mv_cesa_caps armada_370_caps = { |
| 182 | .nengines = 1, |
| 183 | .cipher_algs = armada_370_cipher_algs, |
| 184 | .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs), |
| 185 | .ahash_algs = armada_370_ahash_algs, |
| 186 | .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs), |
| 187 | }; |
| 188 | |
| 189 | static const struct of_device_id mv_cesa_of_match_table[] = { |
| 190 | { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps }, |
| 191 | {} |
| 192 | }; |
| 193 | MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table); |
| 194 | |
| 195 | static int mv_cesa_get_sram(struct platform_device *pdev, int idx) |
| 196 | { |
| 197 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 198 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 199 | const char *res_name = "sram"; |
| 200 | struct resource *res; |
| 201 | |
| 202 | engine->pool = of_get_named_gen_pool(cesa->dev->of_node, |
| 203 | "marvell,crypto-srams", |
| 204 | idx); |
| 205 | if (engine->pool) { |
| 206 | engine->sram = gen_pool_dma_alloc(engine->pool, |
| 207 | cesa->sram_size, |
| 208 | &engine->sram_dma); |
| 209 | if (engine->sram) |
| 210 | return 0; |
| 211 | |
| 212 | engine->pool = NULL; |
| 213 | return -ENOMEM; |
| 214 | } |
| 215 | |
| 216 | if (cesa->caps->nengines > 1) { |
| 217 | if (!idx) |
| 218 | res_name = "sram0"; |
| 219 | else |
| 220 | res_name = "sram1"; |
| 221 | } |
| 222 | |
| 223 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 224 | res_name); |
| 225 | if (!res || resource_size(res) < cesa->sram_size) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | engine->sram = devm_ioremap_resource(cesa->dev, res); |
| 229 | if (IS_ERR(engine->sram)) |
| 230 | return PTR_ERR(engine->sram); |
| 231 | |
| 232 | engine->sram_dma = phys_to_dma(cesa->dev, |
| 233 | (phys_addr_t)res->start); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static void mv_cesa_put_sram(struct platform_device *pdev, int idx) |
| 239 | { |
| 240 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 241 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 242 | |
| 243 | if (!engine->pool) |
| 244 | return; |
| 245 | |
| 246 | gen_pool_free(engine->pool, (unsigned long)engine->sram, |
| 247 | cesa->sram_size); |
| 248 | } |
| 249 | |
| 250 | static int mv_cesa_probe(struct platform_device *pdev) |
| 251 | { |
| 252 | const struct mv_cesa_caps *caps = NULL; |
| 253 | const struct mbus_dram_target_info *dram; |
| 254 | const struct of_device_id *match; |
| 255 | struct device *dev = &pdev->dev; |
| 256 | struct mv_cesa_dev *cesa; |
| 257 | struct mv_cesa_engine *engines; |
| 258 | struct resource *res; |
| 259 | int irq, ret, i; |
| 260 | u32 sram_size; |
| 261 | |
| 262 | if (cesa_dev) { |
| 263 | dev_err(&pdev->dev, "Only one CESA device authorized\n"); |
| 264 | return -EEXIST; |
| 265 | } |
| 266 | |
| 267 | if (!dev->of_node) |
| 268 | return -ENOTSUPP; |
| 269 | |
| 270 | match = of_match_node(mv_cesa_of_match_table, dev->of_node); |
| 271 | if (!match || !match->data) |
| 272 | return -ENOTSUPP; |
| 273 | |
| 274 | caps = match->data; |
| 275 | |
| 276 | cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL); |
| 277 | if (!cesa) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | cesa->caps = caps; |
| 281 | cesa->dev = dev; |
| 282 | |
| 283 | sram_size = CESA_SA_DEFAULT_SRAM_SIZE; |
| 284 | of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size", |
| 285 | &sram_size); |
| 286 | if (sram_size < CESA_SA_MIN_SRAM_SIZE) |
| 287 | sram_size = CESA_SA_MIN_SRAM_SIZE; |
| 288 | |
| 289 | cesa->sram_size = sram_size; |
| 290 | cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines), |
| 291 | GFP_KERNEL); |
| 292 | if (!cesa->engines) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | spin_lock_init(&cesa->lock); |
| 296 | crypto_init_queue(&cesa->queue, 50); |
| 297 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs"); |
| 298 | cesa->regs = devm_ioremap_resource(dev, res); |
| 299 | if (IS_ERR(cesa->regs)) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | dram = mv_mbus_dram_info_nooverlap(); |
| 303 | |
| 304 | platform_set_drvdata(pdev, cesa); |
| 305 | |
| 306 | for (i = 0; i < caps->nengines; i++) { |
| 307 | struct mv_cesa_engine *engine = &cesa->engines[i]; |
| 308 | char res_name[7]; |
| 309 | |
| 310 | engine->id = i; |
| 311 | spin_lock_init(&engine->lock); |
| 312 | |
| 313 | ret = mv_cesa_get_sram(pdev, i); |
| 314 | if (ret) |
| 315 | goto err_cleanup; |
| 316 | |
| 317 | irq = platform_get_irq(pdev, i); |
| 318 | if (irq < 0) { |
| 319 | ret = irq; |
| 320 | goto err_cleanup; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Not all platforms can gate the CESA clocks: do not complain |
| 325 | * if the clock does not exist. |
| 326 | */ |
| 327 | snprintf(res_name, sizeof(res_name), "cesa%d", i); |
| 328 | engine->clk = devm_clk_get(dev, res_name); |
| 329 | if (IS_ERR(engine->clk)) { |
| 330 | engine->clk = devm_clk_get(dev, NULL); |
| 331 | if (IS_ERR(engine->clk)) |
| 332 | engine->clk = NULL; |
| 333 | } |
| 334 | |
| 335 | snprintf(res_name, sizeof(res_name), "cesaz%d", i); |
| 336 | engine->zclk = devm_clk_get(dev, res_name); |
| 337 | if (IS_ERR(engine->zclk)) |
| 338 | engine->zclk = NULL; |
| 339 | |
| 340 | ret = clk_prepare_enable(engine->clk); |
| 341 | if (ret) |
| 342 | goto err_cleanup; |
| 343 | |
| 344 | ret = clk_prepare_enable(engine->zclk); |
| 345 | if (ret) |
| 346 | goto err_cleanup; |
| 347 | |
| 348 | engine->regs = cesa->regs + CESA_ENGINE_OFF(i); |
| 349 | |
| 350 | writel(0, cesa->engines[i].regs + CESA_SA_INT_STATUS); |
| 351 | writel(CESA_SA_CFG_STOP_DIG_ERR, |
| 352 | cesa->engines[i].regs + CESA_SA_CFG); |
| 353 | writel(engine->sram_dma & CESA_SA_SRAM_MSK, |
| 354 | cesa->engines[i].regs + CESA_SA_DESC_P0); |
| 355 | |
| 356 | ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int, |
| 357 | IRQF_ONESHOT, |
| 358 | dev_name(&pdev->dev), |
| 359 | &cesa->engines[i]); |
| 360 | if (ret) |
| 361 | goto err_cleanup; |
| 362 | } |
| 363 | |
| 364 | cesa_dev = cesa; |
| 365 | |
| 366 | ret = mv_cesa_add_algs(cesa); |
| 367 | if (ret) { |
| 368 | cesa_dev = NULL; |
| 369 | goto err_cleanup; |
| 370 | } |
| 371 | |
| 372 | dev_info(dev, "CESA device successfully registered\n"); |
| 373 | |
| 374 | return 0; |
| 375 | |
| 376 | err_cleanup: |
| 377 | for (i = 0; i < caps->nengines; i++) { |
| 378 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 379 | clk_disable_unprepare(cesa->engines[i].clk); |
| 380 | mv_cesa_put_sram(pdev, i); |
| 381 | } |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static int mv_cesa_remove(struct platform_device *pdev) |
| 387 | { |
| 388 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 389 | int i; |
| 390 | |
| 391 | mv_cesa_remove_algs(cesa); |
| 392 | |
| 393 | for (i = 0; i < cesa->caps->nengines; i++) { |
| 394 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 395 | clk_disable_unprepare(cesa->engines[i].clk); |
| 396 | mv_cesa_put_sram(pdev, i); |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static struct platform_driver marvell_cesa = { |
| 403 | .probe = mv_cesa_probe, |
| 404 | .remove = mv_cesa_remove, |
| 405 | .driver = { |
| 406 | .owner = THIS_MODULE, |
| 407 | .name = "marvell-cesa", |
| 408 | .of_match_table = mv_cesa_of_match_table, |
| 409 | }, |
| 410 | }; |
| 411 | module_platform_driver(marvell_cesa); |
| 412 | |
| 413 | MODULE_ALIAS("platform:mv_crypto"); |
| 414 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); |
| 415 | MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>"); |
| 416 | MODULE_DESCRIPTION("Support for Marvell's cryptographic engine"); |
| 417 | MODULE_LICENSE("GPL v2"); |