Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: atmel-sha - add support for latest release of the IP (0x410)

Updates from IP release 0x320 to 0x400:
- add DMA support (previous IP revision use PDC)
- add DMA double input buffer support
- add SHA224 support

Update from IP release 0x400 to 0x410:
- add SHA384 and SHA512 support

Signed-off-by: Nicolas Royer <nicolas@eukrea.com>
Acked-by: Nicolas Ferre <nicolas.ferre@atmel.com>
Acked-by: Eric Bénard <eric@eukrea.com>
Tested-by: Eric Bénard <eric@eukrea.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Nicolas Royer and committed by
Herbert Xu
d4905b38 1f858040

+495 -102
+5 -3
drivers/crypto/Kconfig
··· 361 361 will be called atmel-tdes. 362 362 363 363 config CRYPTO_DEV_ATMEL_SHA 364 - tristate "Support for Atmel SHA1/SHA256 hw accelerator" 364 + tristate "Support for Atmel SHA hw accelerator" 365 365 depends on ARCH_AT91 366 366 select CRYPTO_SHA1 367 367 select CRYPTO_SHA256 368 + select CRYPTO_SHA512 368 369 select CRYPTO_ALGAPI 369 370 help 370 - Some Atmel processors have SHA1/SHA256 hw accelerator. 371 + Some Atmel processors have SHA1/SHA224/SHA256/SHA384/SHA512 372 + hw accelerator. 371 373 Select this if you want to use the Atmel module for 372 - SHA1/SHA256 algorithms. 374 + SHA1/SHA224/SHA256/SHA384/SHA512 algorithms. 373 375 374 376 To compile this driver as a module, choose M here: the module 375 377 will be called atmel-sha.
+6 -1
drivers/crypto/atmel-sha-regs.h
··· 14 14 #define SHA_MR_MODE_MANUAL 0x0 15 15 #define SHA_MR_MODE_AUTO 0x1 16 16 #define SHA_MR_MODE_PDC 0x2 17 - #define SHA_MR_DUALBUFF (1 << 3) 18 17 #define SHA_MR_PROCDLY (1 << 4) 19 18 #define SHA_MR_ALGO_SHA1 (0 << 8) 20 19 #define SHA_MR_ALGO_SHA256 (1 << 8) 20 + #define SHA_MR_ALGO_SHA384 (2 << 8) 21 + #define SHA_MR_ALGO_SHA512 (3 << 8) 22 + #define SHA_MR_ALGO_SHA224 (4 << 8) 23 + #define SHA_MR_DUALBUFF (1 << 16) 21 24 22 25 #define SHA_IER 0x10 23 26 #define SHA_IDR 0x14 ··· 35 32 #define SHA_ISR_URAT_ODR (0x1 << 12) 36 33 #define SHA_ISR_URAT_MR (0x2 << 12) 37 34 #define SHA_ISR_URAT_WO (0x5 << 12) 35 + 36 + #define SHA_HW_VERSION 0xFC 38 37 39 38 #define SHA_TPR 0x108 40 39 #define SHA_TCR 0x10C
+484 -98
drivers/crypto/atmel-sha.c
··· 38 38 #include <crypto/sha.h> 39 39 #include <crypto/hash.h> 40 40 #include <crypto/internal/hash.h> 41 + #include <linux/platform_data/crypto-atmel.h> 41 42 #include "atmel-sha-regs.h" 42 43 43 44 /* SHA flags */ ··· 53 52 #define SHA_FLAGS_FINUP BIT(16) 54 53 #define SHA_FLAGS_SG BIT(17) 55 54 #define SHA_FLAGS_SHA1 BIT(18) 56 - #define SHA_FLAGS_SHA256 BIT(19) 57 - #define SHA_FLAGS_ERROR BIT(20) 58 - #define SHA_FLAGS_PAD BIT(21) 59 - 60 - #define SHA_FLAGS_DUALBUFF BIT(24) 55 + #define SHA_FLAGS_SHA224 BIT(19) 56 + #define SHA_FLAGS_SHA256 BIT(20) 57 + #define SHA_FLAGS_SHA384 BIT(21) 58 + #define SHA_FLAGS_SHA512 BIT(22) 59 + #define SHA_FLAGS_ERROR BIT(23) 60 + #define SHA_FLAGS_PAD BIT(24) 61 61 62 62 #define SHA_OP_UPDATE 1 63 63 #define SHA_OP_FINAL 2 ··· 67 65 68 66 #define ATMEL_SHA_DMA_THRESHOLD 56 69 67 68 + struct atmel_sha_caps { 69 + bool has_dma; 70 + bool has_dualbuff; 71 + bool has_sha224; 72 + bool has_sha_384_512; 73 + }; 70 74 71 75 struct atmel_sha_dev; 72 76 ··· 81 73 unsigned long flags; 82 74 unsigned long op; 83 75 84 - u8 digest[SHA256_DIGEST_SIZE] __aligned(sizeof(u32)); 85 - size_t digcnt; 76 + u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32)); 77 + u64 digcnt[2]; 86 78 size_t bufcnt; 87 79 size_t buflen; 88 80 dma_addr_t dma_addr; ··· 91 83 struct scatterlist *sg; 92 84 unsigned int offset; /* offset in current sg */ 93 85 unsigned int total; /* total request */ 86 + 87 + size_t block_size; 94 88 95 89 u8 buffer[0] __aligned(sizeof(u32)); 96 90 }; ··· 107 97 108 98 }; 109 99 110 - #define ATMEL_SHA_QUEUE_LENGTH 1 100 + #define ATMEL_SHA_QUEUE_LENGTH 50 101 + 102 + struct atmel_sha_dma { 103 + struct dma_chan *chan; 104 + struct dma_slave_config dma_conf; 105 + }; 111 106 112 107 struct atmel_sha_dev { 113 108 struct list_head list; ··· 129 114 unsigned long flags; 130 115 struct crypto_queue queue; 131 116 struct ahash_request *req; 117 + 118 + struct atmel_sha_dma dma_lch_in; 119 + 120 + struct atmel_sha_caps caps; 121 + 122 + u32 hw_version; 132 123 }; 133 124 134 125 struct atmel_sha_drv { ··· 156 135 u32 offset, u32 value) 157 136 { 158 137 writel_relaxed(value, dd->io_base + offset); 159 - } 160 - 161 - static void atmel_sha_dualbuff_test(struct atmel_sha_dev *dd) 162 - { 163 - atmel_sha_write(dd, SHA_MR, SHA_MR_DUALBUFF); 164 - 165 - if (atmel_sha_read(dd, SHA_MR) & SHA_MR_DUALBUFF) 166 - dd->flags |= SHA_FLAGS_DUALBUFF; 167 138 } 168 139 169 140 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx) ··· 189 176 } 190 177 191 178 /* 192 - * The purpose of this padding is to ensure that the padded message 193 - * is a multiple of 512 bits. The bit "1" is appended at the end of 194 - * the message followed by "padlen-1" zero bits. Then a 64 bits block 195 - * equals to the message length in bits is appended. 179 + * The purpose of this padding is to ensure that the padded message is a 180 + * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512). 181 + * The bit "1" is appended at the end of the message followed by 182 + * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or 183 + * 128 bits block (SHA384/SHA512) equals to the message length in bits 184 + * is appended. 196 185 * 197 - * padlen is calculated as followed: 186 + * For SHA1/SHA224/SHA256, padlen is calculated as followed: 198 187 * - if message length < 56 bytes then padlen = 56 - message length 199 188 * - else padlen = 64 + 56 - message length 189 + * 190 + * For SHA384/SHA512, padlen is calculated as followed: 191 + * - if message length < 112 bytes then padlen = 112 - message length 192 + * - else padlen = 128 + 112 - message length 200 193 */ 201 194 static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length) 202 195 { 203 196 unsigned int index, padlen; 204 - u64 bits; 205 - u64 size; 197 + u64 bits[2]; 198 + u64 size[2]; 206 199 207 - bits = (ctx->bufcnt + ctx->digcnt + length) << 3; 208 - size = cpu_to_be64(bits); 200 + size[0] = ctx->digcnt[0]; 201 + size[1] = ctx->digcnt[1]; 209 202 210 - index = ctx->bufcnt & 0x3f; 211 - padlen = (index < 56) ? (56 - index) : ((64+56) - index); 212 - *(ctx->buffer + ctx->bufcnt) = 0x80; 213 - memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); 214 - memcpy(ctx->buffer + ctx->bufcnt + padlen, &size, 8); 215 - ctx->bufcnt += padlen + 8; 216 - ctx->flags |= SHA_FLAGS_PAD; 203 + size[0] += ctx->bufcnt; 204 + if (size[0] < ctx->bufcnt) 205 + size[1]++; 206 + 207 + size[0] += length; 208 + if (size[0] < length) 209 + size[1]++; 210 + 211 + bits[1] = cpu_to_be64(size[0] << 3); 212 + bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61); 213 + 214 + if (ctx->flags & (SHA_FLAGS_SHA384 | SHA_FLAGS_SHA512)) { 215 + index = ctx->bufcnt & 0x7f; 216 + padlen = (index < 112) ? (112 - index) : ((128+112) - index); 217 + *(ctx->buffer + ctx->bufcnt) = 0x80; 218 + memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); 219 + memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16); 220 + ctx->bufcnt += padlen + 16; 221 + ctx->flags |= SHA_FLAGS_PAD; 222 + } else { 223 + index = ctx->bufcnt & 0x3f; 224 + padlen = (index < 56) ? (56 - index) : ((64+56) - index); 225 + *(ctx->buffer + ctx->bufcnt) = 0x80; 226 + memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); 227 + memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8); 228 + ctx->bufcnt += padlen + 8; 229 + ctx->flags |= SHA_FLAGS_PAD; 230 + } 217 231 } 218 232 219 233 static int atmel_sha_init(struct ahash_request *req) ··· 271 231 dev_dbg(dd->dev, "init: digest size: %d\n", 272 232 crypto_ahash_digestsize(tfm)); 273 233 274 - if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE) 234 + switch (crypto_ahash_digestsize(tfm)) { 235 + case SHA1_DIGEST_SIZE: 275 236 ctx->flags |= SHA_FLAGS_SHA1; 276 - else if (crypto_ahash_digestsize(tfm) == SHA256_DIGEST_SIZE) 237 + ctx->block_size = SHA1_BLOCK_SIZE; 238 + break; 239 + case SHA224_DIGEST_SIZE: 240 + ctx->flags |= SHA_FLAGS_SHA224; 241 + ctx->block_size = SHA224_BLOCK_SIZE; 242 + break; 243 + case SHA256_DIGEST_SIZE: 277 244 ctx->flags |= SHA_FLAGS_SHA256; 245 + ctx->block_size = SHA256_BLOCK_SIZE; 246 + break; 247 + case SHA384_DIGEST_SIZE: 248 + ctx->flags |= SHA_FLAGS_SHA384; 249 + ctx->block_size = SHA384_BLOCK_SIZE; 250 + break; 251 + case SHA512_DIGEST_SIZE: 252 + ctx->flags |= SHA_FLAGS_SHA512; 253 + ctx->block_size = SHA512_BLOCK_SIZE; 254 + break; 255 + default: 256 + return -EINVAL; 257 + break; 258 + } 278 259 279 260 ctx->bufcnt = 0; 280 - ctx->digcnt = 0; 261 + ctx->digcnt[0] = 0; 262 + ctx->digcnt[1] = 0; 281 263 ctx->buflen = SHA_BUFFER_LEN; 282 264 283 265 return 0; ··· 311 249 u32 valcr = 0, valmr = SHA_MR_MODE_AUTO; 312 250 313 251 if (likely(dma)) { 314 - atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE); 252 + if (!dd->caps.has_dma) 253 + atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE); 315 254 valmr = SHA_MR_MODE_PDC; 316 - if (dd->flags & SHA_FLAGS_DUALBUFF) 317 - valmr = SHA_MR_DUALBUFF; 255 + if (dd->caps.has_dualbuff) 256 + valmr |= SHA_MR_DUALBUFF; 318 257 } else { 319 258 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 320 259 } 321 260 322 - if (ctx->flags & SHA_FLAGS_SHA256) 261 + if (ctx->flags & SHA_FLAGS_SHA1) 262 + valmr |= SHA_MR_ALGO_SHA1; 263 + else if (ctx->flags & SHA_FLAGS_SHA224) 264 + valmr |= SHA_MR_ALGO_SHA224; 265 + else if (ctx->flags & SHA_FLAGS_SHA256) 323 266 valmr |= SHA_MR_ALGO_SHA256; 267 + else if (ctx->flags & SHA_FLAGS_SHA384) 268 + valmr |= SHA_MR_ALGO_SHA384; 269 + else if (ctx->flags & SHA_FLAGS_SHA512) 270 + valmr |= SHA_MR_ALGO_SHA512; 324 271 325 272 /* Setting CR_FIRST only for the first iteration */ 326 - if (!ctx->digcnt) 273 + if (!(ctx->digcnt[0] || ctx->digcnt[1])) 327 274 valcr = SHA_CR_FIRST; 328 275 329 276 atmel_sha_write(dd, SHA_CR, valcr); ··· 346 275 int count, len32; 347 276 const u32 *buffer = (const u32 *)buf; 348 277 349 - dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n", 350 - ctx->digcnt, length, final); 278 + dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", 279 + ctx->digcnt[1], ctx->digcnt[0], length, final); 351 280 352 281 atmel_sha_write_ctrl(dd, 0); 353 282 354 283 /* should be non-zero before next lines to disable clocks later */ 355 - ctx->digcnt += length; 284 + ctx->digcnt[0] += length; 285 + if (ctx->digcnt[0] < length) 286 + ctx->digcnt[1]++; 356 287 357 288 if (final) 358 289 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ ··· 375 302 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 376 303 int len32; 377 304 378 - dev_dbg(dd->dev, "xmit_pdc: digcnt: %d, length: %d, final: %d\n", 379 - ctx->digcnt, length1, final); 305 + dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", 306 + ctx->digcnt[1], ctx->digcnt[0], length1, final); 380 307 381 308 len32 = DIV_ROUND_UP(length1, sizeof(u32)); 382 309 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS); ··· 390 317 atmel_sha_write_ctrl(dd, 1); 391 318 392 319 /* should be non-zero before next lines to disable clocks later */ 393 - ctx->digcnt += length1; 320 + ctx->digcnt[0] += length1; 321 + if (ctx->digcnt[0] < length1) 322 + ctx->digcnt[1]++; 394 323 395 324 if (final) 396 325 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ ··· 405 330 return -EINPROGRESS; 406 331 } 407 332 333 + static void atmel_sha_dma_callback(void *data) 334 + { 335 + struct atmel_sha_dev *dd = data; 336 + 337 + /* dma_lch_in - completed - wait DATRDY */ 338 + atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 339 + } 340 + 341 + static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, 342 + size_t length1, dma_addr_t dma_addr2, size_t length2, int final) 343 + { 344 + struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 345 + struct dma_async_tx_descriptor *in_desc; 346 + struct scatterlist sg[2]; 347 + 348 + dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", 349 + ctx->digcnt[1], ctx->digcnt[0], length1, final); 350 + 351 + if (ctx->flags & (SHA_FLAGS_SHA1 | SHA_FLAGS_SHA224 | 352 + SHA_FLAGS_SHA256)) { 353 + dd->dma_lch_in.dma_conf.src_maxburst = 16; 354 + dd->dma_lch_in.dma_conf.dst_maxburst = 16; 355 + } else { 356 + dd->dma_lch_in.dma_conf.src_maxburst = 32; 357 + dd->dma_lch_in.dma_conf.dst_maxburst = 32; 358 + } 359 + 360 + dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf); 361 + 362 + if (length2) { 363 + sg_init_table(sg, 2); 364 + sg_dma_address(&sg[0]) = dma_addr1; 365 + sg_dma_len(&sg[0]) = length1; 366 + sg_dma_address(&sg[1]) = dma_addr2; 367 + sg_dma_len(&sg[1]) = length2; 368 + in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2, 369 + DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 370 + } else { 371 + sg_init_table(sg, 1); 372 + sg_dma_address(&sg[0]) = dma_addr1; 373 + sg_dma_len(&sg[0]) = length1; 374 + in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1, 375 + DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 376 + } 377 + if (!in_desc) 378 + return -EINVAL; 379 + 380 + in_desc->callback = atmel_sha_dma_callback; 381 + in_desc->callback_param = dd; 382 + 383 + atmel_sha_write_ctrl(dd, 1); 384 + 385 + /* should be non-zero before next lines to disable clocks later */ 386 + ctx->digcnt[0] += length1; 387 + if (ctx->digcnt[0] < length1) 388 + ctx->digcnt[1]++; 389 + 390 + if (final) 391 + dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ 392 + 393 + dd->flags |= SHA_FLAGS_DMA_ACTIVE; 394 + 395 + /* Start DMA transfer */ 396 + dmaengine_submit(in_desc); 397 + dma_async_issue_pending(dd->dma_lch_in.chan); 398 + 399 + return -EINPROGRESS; 400 + } 401 + 402 + static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, 403 + size_t length1, dma_addr_t dma_addr2, size_t length2, int final) 404 + { 405 + if (dd->caps.has_dma) 406 + return atmel_sha_xmit_dma(dd, dma_addr1, length1, 407 + dma_addr2, length2, final); 408 + else 409 + return atmel_sha_xmit_pdc(dd, dma_addr1, length1, 410 + dma_addr2, length2, final); 411 + } 412 + 408 413 static int atmel_sha_update_cpu(struct atmel_sha_dev *dd) 409 414 { 410 415 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); ··· 492 337 493 338 atmel_sha_append_sg(ctx); 494 339 atmel_sha_fill_padding(ctx, 0); 495 - 496 340 bufcnt = ctx->bufcnt; 497 341 ctx->bufcnt = 0; 498 342 ··· 503 349 size_t length, int final) 504 350 { 505 351 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, 506 - ctx->buflen + SHA1_BLOCK_SIZE, DMA_TO_DEVICE); 352 + ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 507 353 if (dma_mapping_error(dd->dev, ctx->dma_addr)) { 508 354 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen + 509 - SHA1_BLOCK_SIZE); 355 + ctx->block_size); 510 356 return -EINVAL; 511 357 } 512 358 513 359 ctx->flags &= ~SHA_FLAGS_SG; 514 360 515 361 /* next call does not fail... so no unmap in the case of error */ 516 - return atmel_sha_xmit_pdc(dd, ctx->dma_addr, length, 0, 0, final); 362 + return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final); 517 363 } 518 364 519 365 static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd) ··· 526 372 527 373 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; 528 374 529 - dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n", 530 - ctx->bufcnt, ctx->digcnt, final); 375 + dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n", 376 + ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final); 531 377 532 378 if (final) 533 379 atmel_sha_fill_padding(ctx, 0); ··· 554 400 if (ctx->bufcnt || ctx->offset) 555 401 return atmel_sha_update_dma_slow(dd); 556 402 557 - dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n", 558 - ctx->digcnt, ctx->bufcnt, ctx->total); 403 + dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n", 404 + ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total); 559 405 560 406 sg = ctx->sg; 561 407 562 408 if (!IS_ALIGNED(sg->offset, sizeof(u32))) 563 409 return atmel_sha_update_dma_slow(dd); 564 410 565 - if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, SHA1_BLOCK_SIZE)) 566 - /* size is not SHA1_BLOCK_SIZE aligned */ 411 + if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size)) 412 + /* size is not ctx->block_size aligned */ 567 413 return atmel_sha_update_dma_slow(dd); 568 414 569 415 length = min(ctx->total, sg->length); 570 416 571 417 if (sg_is_last(sg)) { 572 418 if (!(ctx->flags & SHA_FLAGS_FINUP)) { 573 - /* not last sg must be SHA1_BLOCK_SIZE aligned */ 574 - tail = length & (SHA1_BLOCK_SIZE - 1); 419 + /* not last sg must be ctx->block_size aligned */ 420 + tail = length & (ctx->block_size - 1); 575 421 length -= tail; 576 - if (length == 0) { 577 - /* offset where to start slow */ 578 - ctx->offset = length; 579 - return atmel_sha_update_dma_slow(dd); 580 - } 581 422 } 582 423 } 583 424 ··· 583 434 584 435 /* Add padding */ 585 436 if (final) { 586 - tail = length & (SHA1_BLOCK_SIZE - 1); 437 + tail = length & (ctx->block_size - 1); 587 438 length -= tail; 588 439 ctx->total += tail; 589 440 ctx->offset = length; /* offset where to start slow */ ··· 594 445 atmel_sha_fill_padding(ctx, length); 595 446 596 447 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, 597 - ctx->buflen + SHA1_BLOCK_SIZE, DMA_TO_DEVICE); 448 + ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 598 449 if (dma_mapping_error(dd->dev, ctx->dma_addr)) { 599 450 dev_err(dd->dev, "dma %u bytes error\n", 600 - ctx->buflen + SHA1_BLOCK_SIZE); 451 + ctx->buflen + ctx->block_size); 601 452 return -EINVAL; 602 453 } 603 454 ··· 605 456 ctx->flags &= ~SHA_FLAGS_SG; 606 457 count = ctx->bufcnt; 607 458 ctx->bufcnt = 0; 608 - return atmel_sha_xmit_pdc(dd, ctx->dma_addr, count, 0, 459 + return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0, 609 460 0, final); 610 461 } else { 611 462 ctx->sg = sg; ··· 619 470 620 471 count = ctx->bufcnt; 621 472 ctx->bufcnt = 0; 622 - return atmel_sha_xmit_pdc(dd, sg_dma_address(ctx->sg), 473 + return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), 623 474 length, ctx->dma_addr, count, final); 624 475 } 625 476 } ··· 632 483 ctx->flags |= SHA_FLAGS_SG; 633 484 634 485 /* next call does not fail... so no unmap in the case of error */ 635 - return atmel_sha_xmit_pdc(dd, sg_dma_address(ctx->sg), length, 0, 486 + return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0, 636 487 0, final); 637 488 } 638 489 ··· 647 498 if (ctx->sg) 648 499 ctx->offset = 0; 649 500 } 650 - if (ctx->flags & SHA_FLAGS_PAD) 501 + if (ctx->flags & SHA_FLAGS_PAD) { 651 502 dma_unmap_single(dd->dev, ctx->dma_addr, 652 - ctx->buflen + SHA1_BLOCK_SIZE, DMA_TO_DEVICE); 503 + ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 504 + } 653 505 } else { 654 506 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen + 655 - SHA1_BLOCK_SIZE, DMA_TO_DEVICE); 507 + ctx->block_size, DMA_TO_DEVICE); 656 508 } 657 509 658 510 return 0; ··· 665 515 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 666 516 int err; 667 517 668 - dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n", 669 - ctx->total, ctx->digcnt, (ctx->flags & SHA_FLAGS_FINUP) != 0); 518 + dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n", 519 + ctx->total, ctx->digcnt[1], ctx->digcnt[0]); 670 520 671 521 if (ctx->flags & SHA_FLAGS_CPU) 672 522 err = atmel_sha_update_cpu(dd); ··· 674 524 err = atmel_sha_update_dma_start(dd); 675 525 676 526 /* wait for dma completion before can take more data */ 677 - dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", 678 - err, ctx->digcnt); 527 + dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n", 528 + err, ctx->digcnt[1], ctx->digcnt[0]); 679 529 680 530 return err; 681 531 } ··· 712 562 u32 *hash = (u32 *)ctx->digest; 713 563 int i; 714 564 715 - if (likely(ctx->flags & SHA_FLAGS_SHA1)) 565 + if (ctx->flags & SHA_FLAGS_SHA1) 716 566 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) 717 567 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 718 - else 568 + else if (ctx->flags & SHA_FLAGS_SHA224) 569 + for (i = 0; i < SHA224_DIGEST_SIZE / sizeof(u32); i++) 570 + hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 571 + else if (ctx->flags & SHA_FLAGS_SHA256) 719 572 for (i = 0; i < SHA256_DIGEST_SIZE / sizeof(u32); i++) 573 + hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 574 + else if (ctx->flags & SHA_FLAGS_SHA384) 575 + for (i = 0; i < SHA384_DIGEST_SIZE / sizeof(u32); i++) 576 + hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 577 + else 578 + for (i = 0; i < SHA512_DIGEST_SIZE / sizeof(u32); i++) 720 579 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 721 580 } 722 581 ··· 736 577 if (!req->result) 737 578 return; 738 579 739 - if (likely(ctx->flags & SHA_FLAGS_SHA1)) 580 + if (ctx->flags & SHA_FLAGS_SHA1) 740 581 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE); 741 - else 582 + else if (ctx->flags & SHA_FLAGS_SHA224) 583 + memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE); 584 + else if (ctx->flags & SHA_FLAGS_SHA256) 742 585 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE); 586 + else if (ctx->flags & SHA_FLAGS_SHA384) 587 + memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE); 588 + else 589 + memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE); 743 590 } 744 591 745 592 static int atmel_sha_finish(struct ahash_request *req) ··· 754 589 struct atmel_sha_dev *dd = ctx->dd; 755 590 int err = 0; 756 591 757 - if (ctx->digcnt) 592 + if (ctx->digcnt[0] || ctx->digcnt[1]) 758 593 atmel_sha_copy_ready_hash(req); 759 594 760 - dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, 761 - ctx->bufcnt); 595 + dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1], 596 + ctx->digcnt[0], ctx->bufcnt); 762 597 763 598 return err; 764 599 } ··· 793 628 { 794 629 clk_prepare_enable(dd->iclk); 795 630 796 - if (SHA_FLAGS_INIT & dd->flags) { 631 + if (!(SHA_FLAGS_INIT & dd->flags)) { 797 632 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST); 798 - atmel_sha_dualbuff_test(dd); 799 633 dd->flags |= SHA_FLAGS_INIT; 800 634 dd->err = 0; 801 635 } 802 636 803 637 return 0; 638 + } 639 + 640 + static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd) 641 + { 642 + return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff; 643 + } 644 + 645 + static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd) 646 + { 647 + atmel_sha_hw_init(dd); 648 + 649 + dd->hw_version = atmel_sha_get_version(dd); 650 + 651 + dev_info(dd->dev, 652 + "version: 0x%x\n", dd->hw_version); 653 + 654 + clk_disable_unprepare(dd->iclk); 804 655 } 805 656 806 657 static int atmel_sha_handle_queue(struct atmel_sha_dev *dd, ··· 863 682 864 683 if (ctx->op == SHA_OP_UPDATE) { 865 684 err = atmel_sha_update_req(dd); 866 - if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP)) { 685 + if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP)) 867 686 /* no final() after finup() */ 868 687 err = atmel_sha_final_req(dd); 869 - } 870 688 } else if (ctx->op == SHA_OP_FINAL) { 871 689 err = atmel_sha_final_req(dd); 872 690 } ··· 988 808 } 989 809 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 990 810 sizeof(struct atmel_sha_reqctx) + 991 - SHA_BUFFER_LEN + SHA256_BLOCK_SIZE); 811 + SHA_BUFFER_LEN + SHA512_BLOCK_SIZE); 992 812 993 813 return 0; 994 814 } ··· 1006 826 tctx->fallback = NULL; 1007 827 } 1008 828 1009 - static struct ahash_alg sha_algs[] = { 829 + static struct ahash_alg sha_1_256_algs[] = { 1010 830 { 1011 831 .init = atmel_sha_init, 1012 832 .update = atmel_sha_update, ··· 1047 867 .cra_blocksize = SHA256_BLOCK_SIZE, 1048 868 .cra_ctxsize = sizeof(struct atmel_sha_ctx), 1049 869 .cra_alignmask = 0, 870 + .cra_module = THIS_MODULE, 871 + .cra_init = atmel_sha_cra_init, 872 + .cra_exit = atmel_sha_cra_exit, 873 + } 874 + } 875 + }, 876 + }; 877 + 878 + static struct ahash_alg sha_224_alg = { 879 + .init = atmel_sha_init, 880 + .update = atmel_sha_update, 881 + .final = atmel_sha_final, 882 + .finup = atmel_sha_finup, 883 + .digest = atmel_sha_digest, 884 + .halg = { 885 + .digestsize = SHA224_DIGEST_SIZE, 886 + .base = { 887 + .cra_name = "sha224", 888 + .cra_driver_name = "atmel-sha224", 889 + .cra_priority = 100, 890 + .cra_flags = CRYPTO_ALG_ASYNC | 891 + CRYPTO_ALG_NEED_FALLBACK, 892 + .cra_blocksize = SHA224_BLOCK_SIZE, 893 + .cra_ctxsize = sizeof(struct atmel_sha_ctx), 894 + .cra_alignmask = 0, 895 + .cra_module = THIS_MODULE, 896 + .cra_init = atmel_sha_cra_init, 897 + .cra_exit = atmel_sha_cra_exit, 898 + } 899 + } 900 + }; 901 + 902 + static struct ahash_alg sha_384_512_algs[] = { 903 + { 904 + .init = atmel_sha_init, 905 + .update = atmel_sha_update, 906 + .final = atmel_sha_final, 907 + .finup = atmel_sha_finup, 908 + .digest = atmel_sha_digest, 909 + .halg = { 910 + .digestsize = SHA384_DIGEST_SIZE, 911 + .base = { 912 + .cra_name = "sha384", 913 + .cra_driver_name = "atmel-sha384", 914 + .cra_priority = 100, 915 + .cra_flags = CRYPTO_ALG_ASYNC | 916 + CRYPTO_ALG_NEED_FALLBACK, 917 + .cra_blocksize = SHA384_BLOCK_SIZE, 918 + .cra_ctxsize = sizeof(struct atmel_sha_ctx), 919 + .cra_alignmask = 0x3, 920 + .cra_module = THIS_MODULE, 921 + .cra_init = atmel_sha_cra_init, 922 + .cra_exit = atmel_sha_cra_exit, 923 + } 924 + } 925 + }, 926 + { 927 + .init = atmel_sha_init, 928 + .update = atmel_sha_update, 929 + .final = atmel_sha_final, 930 + .finup = atmel_sha_finup, 931 + .digest = atmel_sha_digest, 932 + .halg = { 933 + .digestsize = SHA512_DIGEST_SIZE, 934 + .base = { 935 + .cra_name = "sha512", 936 + .cra_driver_name = "atmel-sha512", 937 + .cra_priority = 100, 938 + .cra_flags = CRYPTO_ALG_ASYNC | 939 + CRYPTO_ALG_NEED_FALLBACK, 940 + .cra_blocksize = SHA512_BLOCK_SIZE, 941 + .cra_ctxsize = sizeof(struct atmel_sha_ctx), 942 + .cra_alignmask = 0x3, 1050 943 .cra_module = THIS_MODULE, 1051 944 .cra_init = atmel_sha_cra_init, 1052 945 .cra_exit = atmel_sha_cra_exit, ··· 1194 941 { 1195 942 int i; 1196 943 1197 - for (i = 0; i < ARRAY_SIZE(sha_algs); i++) 1198 - crypto_unregister_ahash(&sha_algs[i]); 944 + for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) 945 + crypto_unregister_ahash(&sha_1_256_algs[i]); 946 + 947 + if (dd->caps.has_sha224) 948 + crypto_unregister_ahash(&sha_224_alg); 949 + 950 + if (dd->caps.has_sha_384_512) { 951 + for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) 952 + crypto_unregister_ahash(&sha_384_512_algs[i]); 953 + } 1199 954 } 1200 955 1201 956 static int atmel_sha_register_algs(struct atmel_sha_dev *dd) 1202 957 { 1203 958 int err, i, j; 1204 959 1205 - for (i = 0; i < ARRAY_SIZE(sha_algs); i++) { 1206 - err = crypto_register_ahash(&sha_algs[i]); 960 + for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) { 961 + err = crypto_register_ahash(&sha_1_256_algs[i]); 1207 962 if (err) 1208 - goto err_sha_algs; 963 + goto err_sha_1_256_algs; 964 + } 965 + 966 + if (dd->caps.has_sha224) { 967 + err = crypto_register_ahash(&sha_224_alg); 968 + if (err) 969 + goto err_sha_224_algs; 970 + } 971 + 972 + if (dd->caps.has_sha_384_512) { 973 + for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) { 974 + err = crypto_register_ahash(&sha_384_512_algs[i]); 975 + if (err) 976 + goto err_sha_384_512_algs; 977 + } 1209 978 } 1210 979 1211 980 return 0; 1212 981 1213 - err_sha_algs: 982 + err_sha_384_512_algs: 1214 983 for (j = 0; j < i; j++) 1215 - crypto_unregister_ahash(&sha_algs[j]); 984 + crypto_unregister_ahash(&sha_384_512_algs[j]); 985 + crypto_unregister_ahash(&sha_224_alg); 986 + err_sha_224_algs: 987 + i = ARRAY_SIZE(sha_1_256_algs); 988 + err_sha_1_256_algs: 989 + for (j = 0; j < i; j++) 990 + crypto_unregister_ahash(&sha_1_256_algs[j]); 1216 991 1217 992 return err; 993 + } 994 + 995 + static bool atmel_sha_filter(struct dma_chan *chan, void *slave) 996 + { 997 + struct at_dma_slave *sl = slave; 998 + 999 + if (sl && sl->dma_dev == chan->device->dev) { 1000 + chan->private = sl; 1001 + return true; 1002 + } else { 1003 + return false; 1004 + } 1005 + } 1006 + 1007 + static int atmel_sha_dma_init(struct atmel_sha_dev *dd, 1008 + struct crypto_platform_data *pdata) 1009 + { 1010 + int err = -ENOMEM; 1011 + dma_cap_mask_t mask_in; 1012 + 1013 + if (pdata && pdata->dma_slave->rxdata.dma_dev) { 1014 + /* Try to grab DMA channel */ 1015 + dma_cap_zero(mask_in); 1016 + dma_cap_set(DMA_SLAVE, mask_in); 1017 + 1018 + dd->dma_lch_in.chan = dma_request_channel(mask_in, 1019 + atmel_sha_filter, &pdata->dma_slave->rxdata); 1020 + 1021 + if (!dd->dma_lch_in.chan) 1022 + return err; 1023 + 1024 + dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV; 1025 + dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base + 1026 + SHA_REG_DIN(0); 1027 + dd->dma_lch_in.dma_conf.src_maxburst = 1; 1028 + dd->dma_lch_in.dma_conf.src_addr_width = 1029 + DMA_SLAVE_BUSWIDTH_4_BYTES; 1030 + dd->dma_lch_in.dma_conf.dst_maxburst = 1; 1031 + dd->dma_lch_in.dma_conf.dst_addr_width = 1032 + DMA_SLAVE_BUSWIDTH_4_BYTES; 1033 + dd->dma_lch_in.dma_conf.device_fc = false; 1034 + 1035 + return 0; 1036 + } 1037 + 1038 + return -ENODEV; 1039 + } 1040 + 1041 + static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd) 1042 + { 1043 + dma_release_channel(dd->dma_lch_in.chan); 1044 + } 1045 + 1046 + static void atmel_sha_get_cap(struct atmel_sha_dev *dd) 1047 + { 1048 + 1049 + dd->caps.has_dma = 0; 1050 + dd->caps.has_dualbuff = 0; 1051 + dd->caps.has_sha224 = 0; 1052 + dd->caps.has_sha_384_512 = 0; 1053 + 1054 + /* keep only major version number */ 1055 + switch (dd->hw_version & 0xff0) { 1056 + case 0x410: 1057 + dd->caps.has_dma = 1; 1058 + dd->caps.has_dualbuff = 1; 1059 + dd->caps.has_sha224 = 1; 1060 + dd->caps.has_sha_384_512 = 1; 1061 + break; 1062 + case 0x400: 1063 + dd->caps.has_dma = 1; 1064 + dd->caps.has_dualbuff = 1; 1065 + dd->caps.has_sha224 = 1; 1066 + break; 1067 + case 0x320: 1068 + break; 1069 + default: 1070 + dev_warn(dd->dev, 1071 + "Unmanaged sha version, set minimum capabilities\n"); 1072 + break; 1073 + } 1218 1074 } 1219 1075 1220 1076 static int atmel_sha_probe(struct platform_device *pdev) 1221 1077 { 1222 1078 struct atmel_sha_dev *sha_dd; 1079 + struct crypto_platform_data *pdata; 1223 1080 struct device *dev = &pdev->dev; 1224 1081 struct resource *sha_res; 1225 1082 unsigned long sha_phys_size; ··· 1381 1018 } 1382 1019 1383 1020 /* Initializing the clock */ 1384 - sha_dd->iclk = clk_get(&pdev->dev, NULL); 1021 + sha_dd->iclk = clk_get(&pdev->dev, "sha_clk"); 1385 1022 if (IS_ERR(sha_dd->iclk)) { 1386 1023 dev_err(dev, "clock intialization failed.\n"); 1387 1024 err = PTR_ERR(sha_dd->iclk); ··· 1393 1030 dev_err(dev, "can't ioremap\n"); 1394 1031 err = -ENOMEM; 1395 1032 goto sha_io_err; 1033 + } 1034 + 1035 + atmel_sha_hw_version_init(sha_dd); 1036 + 1037 + atmel_sha_get_cap(sha_dd); 1038 + 1039 + if (sha_dd->caps.has_dma) { 1040 + pdata = pdev->dev.platform_data; 1041 + if (!pdata) { 1042 + dev_err(&pdev->dev, "platform data not available\n"); 1043 + err = -ENXIO; 1044 + goto err_pdata; 1045 + } 1046 + err = atmel_sha_dma_init(sha_dd, pdata); 1047 + if (err) 1048 + goto err_sha_dma; 1396 1049 } 1397 1050 1398 1051 spin_lock(&atmel_sha.lock); ··· 1427 1048 spin_lock(&atmel_sha.lock); 1428 1049 list_del(&sha_dd->list); 1429 1050 spin_unlock(&atmel_sha.lock); 1051 + if (sha_dd->caps.has_dma) 1052 + atmel_sha_dma_cleanup(sha_dd); 1053 + err_sha_dma: 1054 + err_pdata: 1430 1055 iounmap(sha_dd->io_base); 1431 1056 sha_io_err: 1432 1057 clk_put(sha_dd->iclk); ··· 1461 1078 1462 1079 tasklet_kill(&sha_dd->done_task); 1463 1080 1081 + if (sha_dd->caps.has_dma) 1082 + atmel_sha_dma_cleanup(sha_dd); 1083 + 1464 1084 iounmap(sha_dd->io_base); 1465 1085 1466 1086 clk_put(sha_dd->iclk); ··· 1488 1102 1489 1103 module_platform_driver(atmel_sha_driver); 1490 1104 1491 - MODULE_DESCRIPTION("Atmel SHA1/SHA256 hw acceleration support."); 1105 + MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support."); 1492 1106 MODULE_LICENSE("GPL v2"); 1493 1107 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");