1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
5 * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
6 * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
7 * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
8 * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
9 * Author: Andreas Westin <andreas.westin@stericsson.com> for ST-Ericsson.
12 #include <linux/clk.h>
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dmaengine.h>
17 #include <linux/err.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
21 #include <linux/irqreturn.h>
22 #include <linux/kernel.h>
23 #include <linux/klist.h>
24 #include <linux/module.h>
25 #include <linux/mod_devicetable.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/semaphore.h>
29 #include <linux/platform_data/dma-ste-dma40.h>
31 #include <crypto/aes.h>
32 #include <crypto/ctr.h>
33 #include <crypto/internal/des.h>
34 #include <crypto/internal/skcipher.h>
35 #include <crypto/scatterwalk.h>
37 #include <linux/platform_data/crypto-ux500.h>
42 #define CRYP_MAX_KEY_SIZE 32
43 #define BYTES_PER_WORD 4
46 static atomic_t session_id;
48 static struct stedma40_chan_cfg *mem_to_engine;
49 static struct stedma40_chan_cfg *engine_to_mem;
52 * struct cryp_driver_data - data specific to the driver.
54 * @device_list: A list of registered devices to choose from.
55 * @device_allocation: A semaphore initialized with number of devices.
57 struct cryp_driver_data {
58 struct klist device_list;
59 struct semaphore device_allocation;
63 * struct cryp_ctx - Crypto context
64 * @config: Crypto mode.
65 * @key[CRYP_MAX_KEY_SIZE]: Key.
66 * @keylen: Length of key.
67 * @iv: Pointer to initialization vector.
68 * @indata: Pointer to indata.
69 * @outdata: Pointer to outdata.
70 * @datalen: Length of indata.
71 * @outlen: Length of outdata.
72 * @blocksize: Size of blocks.
73 * @updated: Updated flag.
74 * @dev_ctx: Device dependent context.
75 * @device: Pointer to the device.
78 struct cryp_config config;
79 u8 key[CRYP_MAX_KEY_SIZE];
88 struct cryp_device_context dev_ctx;
89 struct cryp_device_data *device;
93 static struct cryp_driver_data driver_data;
96 * swap_bits_in_byte - mirror the bits in a byte
97 * @b: the byte to be mirrored
99 * The bits are swapped the following way:
100 * Byte b include bits 0-7, nibble 1 (n1) include bits 0-3 and
101 * nibble 2 (n2) bits 4-7.
104 * (The "old" (moved) bit is replaced with a zero)
105 * 1. Move bit 6 and 7, 4 positions to the left.
106 * 2. Move bit 3 and 5, 2 positions to the left.
107 * 3. Move bit 1-4, 1 position to the left.
110 * 1. Move bit 0 and 1, 4 positions to the right.
111 * 2. Move bit 2 and 4, 2 positions to the right.
112 * 3. Move bit 3-6, 1 position to the right.
114 * Combine the two nibbles to a complete and swapped byte.
117 static inline u8 swap_bits_in_byte(u8 b)
119 #define R_SHIFT_4_MASK 0xc0 /* Bits 6 and 7, right shift 4 */
120 #define R_SHIFT_2_MASK 0x28 /* (After right shift 4) Bits 3 and 5,
122 #define R_SHIFT_1_MASK 0x1e /* (After right shift 2) Bits 1-4,
124 #define L_SHIFT_4_MASK 0x03 /* Bits 0 and 1, left shift 4 */
125 #define L_SHIFT_2_MASK 0x14 /* (After left shift 4) Bits 2 and 4,
127 #define L_SHIFT_1_MASK 0x78 /* (After left shift 1) Bits 3-6,
133 /* Swap most significant nibble */
134 /* Right shift 4, bits 6 and 7 */
135 n1 = ((b & R_SHIFT_4_MASK) >> 4) | (b & ~(R_SHIFT_4_MASK >> 4));
136 /* Right shift 2, bits 3 and 5 */
137 n1 = ((n1 & R_SHIFT_2_MASK) >> 2) | (n1 & ~(R_SHIFT_2_MASK >> 2));
138 /* Right shift 1, bits 1-4 */
139 n1 = (n1 & R_SHIFT_1_MASK) >> 1;
141 /* Swap least significant nibble */
142 /* Left shift 4, bits 0 and 1 */
143 n2 = ((b & L_SHIFT_4_MASK) << 4) | (b & ~(L_SHIFT_4_MASK << 4));
144 /* Left shift 2, bits 2 and 4 */
145 n2 = ((n2 & L_SHIFT_2_MASK) << 2) | (n2 & ~(L_SHIFT_2_MASK << 2));
146 /* Left shift 1, bits 3-6 */
147 n2 = (n2 & L_SHIFT_1_MASK) << 1;
152 static inline void swap_words_in_key_and_bits_in_byte(const u8 *in,
159 j = len - BYTES_PER_WORD;
161 for (i = 0; i < BYTES_PER_WORD; i++) {
162 index = len - j - BYTES_PER_WORD + i;
164 swap_bits_in_byte(in[index]);
170 static void add_session_id(struct cryp_ctx *ctx)
173 * We never want 0 to be a valid value, since this is the default value
174 * for the software context.
176 if (unlikely(atomic_inc_and_test(&session_id)))
177 atomic_inc(&session_id);
179 ctx->session_id = atomic_read(&session_id);
182 static irqreturn_t cryp_interrupt_handler(int irq, void *param)
184 struct cryp_ctx *ctx;
186 struct cryp_device_data *device_data;
193 /* The device is coming from the one found in hw_crypt_noxts. */
194 device_data = (struct cryp_device_data *)param;
196 ctx = device_data->current_ctx;
203 dev_dbg(ctx->device->dev, "[%s] (len: %d) %s, ", __func__, ctx->outlen,
204 cryp_pending_irq_src(device_data, CRYP_IRQ_SRC_OUTPUT_FIFO) ?
207 if (cryp_pending_irq_src(device_data,
208 CRYP_IRQ_SRC_OUTPUT_FIFO)) {
209 if (ctx->outlen / ctx->blocksize > 0) {
210 count = ctx->blocksize / 4;
212 readsl(&device_data->base->dout, ctx->outdata, count);
213 ctx->outdata += count;
214 ctx->outlen -= count;
216 if (ctx->outlen == 0) {
217 cryp_disable_irq_src(device_data,
218 CRYP_IRQ_SRC_OUTPUT_FIFO);
221 } else if (cryp_pending_irq_src(device_data,
222 CRYP_IRQ_SRC_INPUT_FIFO)) {
223 if (ctx->datalen / ctx->blocksize > 0) {
224 count = ctx->blocksize / 4;
226 writesl(&device_data->base->din, ctx->indata, count);
228 ctx->indata += count;
229 ctx->datalen -= count;
231 if (ctx->datalen == 0)
232 cryp_disable_irq_src(device_data,
233 CRYP_IRQ_SRC_INPUT_FIFO);
235 if (ctx->config.algomode == CRYP_ALGO_AES_XTS) {
236 CRYP_PUT_BITS(&device_data->base->cr,
241 cryp_wait_until_done(device_data);
249 static int mode_is_aes(enum cryp_algo_mode mode)
251 return CRYP_ALGO_AES_ECB == mode ||
252 CRYP_ALGO_AES_CBC == mode ||
253 CRYP_ALGO_AES_CTR == mode ||
254 CRYP_ALGO_AES_XTS == mode;
257 static int cfg_iv(struct cryp_device_data *device_data, u32 left, u32 right,
258 enum cryp_init_vector_index index)
260 struct cryp_init_vector_value vector_value;
262 dev_dbg(device_data->dev, "[%s]", __func__);
264 vector_value.init_value_left = left;
265 vector_value.init_value_right = right;
267 return cryp_configure_init_vector(device_data,
272 static int cfg_ivs(struct cryp_device_data *device_data, struct cryp_ctx *ctx)
276 int num_of_regs = ctx->blocksize / 8;
277 __be32 *civ = (__be32 *)ctx->iv;
278 u32 iv[AES_BLOCK_SIZE / 4];
280 dev_dbg(device_data->dev, "[%s]", __func__);
283 * Since we loop on num_of_regs we need to have a check in case
284 * someone provides an incorrect blocksize which would force calling
285 * cfg_iv with i greater than 2 which is an error.
287 if (num_of_regs > 2) {
288 dev_err(device_data->dev, "[%s] Incorrect blocksize %d",
289 __func__, ctx->blocksize);
293 for (i = 0; i < ctx->blocksize / 4; i++)
294 iv[i] = be32_to_cpup(civ + i);
296 for (i = 0; i < num_of_regs; i++) {
297 status = cfg_iv(device_data, iv[i*2], iv[i*2+1],
298 (enum cryp_init_vector_index) i);
305 static int set_key(struct cryp_device_data *device_data,
308 enum cryp_key_reg_index index)
310 struct cryp_key_value key_value;
313 dev_dbg(device_data->dev, "[%s]", __func__);
315 key_value.key_value_left = left_key;
316 key_value.key_value_right = right_key;
318 cryp_error = cryp_configure_key_values(device_data,
322 dev_err(device_data->dev, "[%s]: "
323 "cryp_configure_key_values() failed!", __func__);
328 static int cfg_keys(struct cryp_ctx *ctx)
331 int num_of_regs = ctx->keylen / 8;
332 u32 swapped_key[CRYP_MAX_KEY_SIZE / 4];
333 __be32 *ckey = (__be32 *)ctx->key;
336 dev_dbg(ctx->device->dev, "[%s]", __func__);
338 if (mode_is_aes(ctx->config.algomode)) {
339 swap_words_in_key_and_bits_in_byte((u8 *)ckey,
343 for (i = 0; i < ctx->keylen / 4; i++)
344 swapped_key[i] = be32_to_cpup(ckey + i);
347 for (i = 0; i < num_of_regs; i++) {
348 cryp_error = set_key(ctx->device,
350 swapped_key[i * 2 + 1],
351 (enum cryp_key_reg_index) i);
353 if (cryp_error != 0) {
354 dev_err(ctx->device->dev, "[%s]: set_key() failed!",
362 static int cryp_setup_context(struct cryp_ctx *ctx,
363 struct cryp_device_data *device_data)
365 u32 control_register = CRYP_CR_DEFAULT;
368 case CRYP_MODE_INTERRUPT:
369 writel_relaxed(CRYP_IMSC_DEFAULT, &device_data->base->imsc);
373 writel_relaxed(CRYP_DMACR_DEFAULT, &device_data->base->dmacr);
380 if (ctx->updated == 0) {
381 cryp_flush_inoutfifo(device_data);
382 if (cfg_keys(ctx) != 0) {
383 dev_err(ctx->device->dev, "[%s]: cfg_keys failed!",
389 CRYP_ALGO_AES_ECB != ctx->config.algomode &&
390 CRYP_ALGO_DES_ECB != ctx->config.algomode &&
391 CRYP_ALGO_TDES_ECB != ctx->config.algomode) {
392 if (cfg_ivs(device_data, ctx) != 0)
396 cryp_set_configuration(device_data, &ctx->config,
399 } else if (ctx->updated == 1 &&
400 ctx->session_id != atomic_read(&session_id)) {
401 cryp_flush_inoutfifo(device_data);
402 cryp_restore_device_context(device_data, &ctx->dev_ctx);
405 control_register = ctx->dev_ctx.cr;
407 control_register = ctx->dev_ctx.cr;
409 writel(control_register |
410 (CRYP_CRYPEN_ENABLE << CRYP_CR_CRYPEN_POS),
411 &device_data->base->cr);
416 static int cryp_get_device_data(struct cryp_ctx *ctx,
417 struct cryp_device_data **device_data)
420 struct klist_iter device_iterator;
421 struct klist_node *device_node;
422 struct cryp_device_data *local_device_data = NULL;
423 pr_debug(DEV_DBG_NAME " [%s]", __func__);
425 /* Wait until a device is available */
426 ret = down_interruptible(&driver_data.device_allocation);
428 return ret; /* Interrupted */
430 /* Select a device */
431 klist_iter_init(&driver_data.device_list, &device_iterator);
433 device_node = klist_next(&device_iterator);
434 while (device_node) {
435 local_device_data = container_of(device_node,
436 struct cryp_device_data, list_node);
437 spin_lock(&local_device_data->ctx_lock);
438 /* current_ctx allocates a device, NULL = unallocated */
439 if (local_device_data->current_ctx) {
440 device_node = klist_next(&device_iterator);
442 local_device_data->current_ctx = ctx;
443 ctx->device = local_device_data;
444 spin_unlock(&local_device_data->ctx_lock);
447 spin_unlock(&local_device_data->ctx_lock);
449 klist_iter_exit(&device_iterator);
453 * No free device found.
454 * Since we allocated a device with down_interruptible, this
455 * should not be able to happen.
456 * Number of available devices, which are contained in
457 * device_allocation, is therefore decremented by not doing
458 * an up(device_allocation).
463 *device_data = local_device_data;
468 static void cryp_dma_setup_channel(struct cryp_device_data *device_data,
471 struct dma_slave_config mem2cryp = {
472 .direction = DMA_MEM_TO_DEV,
473 .dst_addr = device_data->phybase + CRYP_DMA_TX_FIFO,
474 .dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
477 struct dma_slave_config cryp2mem = {
478 .direction = DMA_DEV_TO_MEM,
479 .src_addr = device_data->phybase + CRYP_DMA_RX_FIFO,
480 .src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
484 dma_cap_zero(device_data->dma.mask);
485 dma_cap_set(DMA_SLAVE, device_data->dma.mask);
487 device_data->dma.cfg_mem2cryp = mem_to_engine;
488 device_data->dma.chan_mem2cryp =
489 dma_request_channel(device_data->dma.mask,
491 device_data->dma.cfg_mem2cryp);
493 device_data->dma.cfg_cryp2mem = engine_to_mem;
494 device_data->dma.chan_cryp2mem =
495 dma_request_channel(device_data->dma.mask,
497 device_data->dma.cfg_cryp2mem);
499 dmaengine_slave_config(device_data->dma.chan_mem2cryp, &mem2cryp);
500 dmaengine_slave_config(device_data->dma.chan_cryp2mem, &cryp2mem);
502 init_completion(&device_data->dma.cryp_dma_complete);
505 static void cryp_dma_out_callback(void *data)
507 struct cryp_ctx *ctx = (struct cryp_ctx *) data;
508 dev_dbg(ctx->device->dev, "[%s]: ", __func__);
510 complete(&ctx->device->dma.cryp_dma_complete);
513 static int cryp_set_dma_transfer(struct cryp_ctx *ctx,
514 struct scatterlist *sg,
516 enum dma_data_direction direction)
518 struct dma_async_tx_descriptor *desc;
519 struct dma_chan *channel = NULL;
522 dev_dbg(ctx->device->dev, "[%s]: ", __func__);
524 if (unlikely(!IS_ALIGNED((unsigned long)sg, 4))) {
525 dev_err(ctx->device->dev, "[%s]: Data in sg list isn't "
526 "aligned! Addr: 0x%08lx", __func__, (unsigned long)sg);
532 channel = ctx->device->dma.chan_mem2cryp;
533 ctx->device->dma.sg_src = sg;
534 ctx->device->dma.sg_src_len = dma_map_sg(channel->device->dev,
535 ctx->device->dma.sg_src,
536 ctx->device->dma.nents_src,
539 if (!ctx->device->dma.sg_src_len) {
540 dev_dbg(ctx->device->dev,
541 "[%s]: Could not map the sg list (TO_DEVICE)",
546 dev_dbg(ctx->device->dev, "[%s]: Setting up DMA for buffer "
547 "(TO_DEVICE)", __func__);
549 desc = dmaengine_prep_slave_sg(channel,
550 ctx->device->dma.sg_src,
551 ctx->device->dma.sg_src_len,
552 DMA_MEM_TO_DEV, DMA_CTRL_ACK);
555 case DMA_FROM_DEVICE:
556 channel = ctx->device->dma.chan_cryp2mem;
557 ctx->device->dma.sg_dst = sg;
558 ctx->device->dma.sg_dst_len = dma_map_sg(channel->device->dev,
559 ctx->device->dma.sg_dst,
560 ctx->device->dma.nents_dst,
563 if (!ctx->device->dma.sg_dst_len) {
564 dev_dbg(ctx->device->dev,
565 "[%s]: Could not map the sg list (FROM_DEVICE)",
570 dev_dbg(ctx->device->dev, "[%s]: Setting up DMA for buffer "
571 "(FROM_DEVICE)", __func__);
573 desc = dmaengine_prep_slave_sg(channel,
574 ctx->device->dma.sg_dst,
575 ctx->device->dma.sg_dst_len,
580 desc->callback = cryp_dma_out_callback;
581 desc->callback_param = ctx;
585 dev_dbg(ctx->device->dev, "[%s]: Invalid DMA direction",
590 cookie = dmaengine_submit(desc);
591 if (dma_submit_error(cookie)) {
592 dev_dbg(ctx->device->dev, "[%s]: DMA submission failed\n",
597 dma_async_issue_pending(channel);
602 static void cryp_dma_done(struct cryp_ctx *ctx)
604 struct dma_chan *chan;
606 dev_dbg(ctx->device->dev, "[%s]: ", __func__);
608 chan = ctx->device->dma.chan_mem2cryp;
609 dmaengine_terminate_all(chan);
610 dma_unmap_sg(chan->device->dev, ctx->device->dma.sg_src,
611 ctx->device->dma.sg_src_len, DMA_TO_DEVICE);
613 chan = ctx->device->dma.chan_cryp2mem;
614 dmaengine_terminate_all(chan);
615 dma_unmap_sg(chan->device->dev, ctx->device->dma.sg_dst,
616 ctx->device->dma.sg_dst_len, DMA_FROM_DEVICE);
619 static int cryp_dma_write(struct cryp_ctx *ctx, struct scatterlist *sg,
622 int error = cryp_set_dma_transfer(ctx, sg, len, DMA_TO_DEVICE);
623 dev_dbg(ctx->device->dev, "[%s]: ", __func__);
626 dev_dbg(ctx->device->dev, "[%s]: cryp_set_dma_transfer() "
634 static int cryp_dma_read(struct cryp_ctx *ctx, struct scatterlist *sg, int len)
636 int error = cryp_set_dma_transfer(ctx, sg, len, DMA_FROM_DEVICE);
638 dev_dbg(ctx->device->dev, "[%s]: cryp_set_dma_transfer() "
646 static void cryp_polling_mode(struct cryp_ctx *ctx,
647 struct cryp_device_data *device_data)
649 int len = ctx->blocksize / BYTES_PER_WORD;
650 int remaining_length = ctx->datalen;
651 u32 *indata = (u32 *)ctx->indata;
652 u32 *outdata = (u32 *)ctx->outdata;
654 while (remaining_length > 0) {
655 writesl(&device_data->base->din, indata, len);
657 remaining_length -= (len * BYTES_PER_WORD);
658 cryp_wait_until_done(device_data);
660 readsl(&device_data->base->dout, outdata, len);
662 cryp_wait_until_done(device_data);
666 static int cryp_disable_power(struct device *dev,
667 struct cryp_device_data *device_data,
668 bool save_device_context)
672 dev_dbg(dev, "[%s]", __func__);
674 spin_lock(&device_data->power_state_spinlock);
675 if (!device_data->power_state)
678 spin_lock(&device_data->ctx_lock);
679 if (save_device_context && device_data->current_ctx) {
680 cryp_save_device_context(device_data,
681 &device_data->current_ctx->dev_ctx,
683 device_data->restore_dev_ctx = true;
685 spin_unlock(&device_data->ctx_lock);
687 clk_disable(device_data->clk);
688 ret = regulator_disable(device_data->pwr_regulator);
690 dev_err(dev, "[%s]: "
691 "regulator_disable() failed!",
694 device_data->power_state = false;
697 spin_unlock(&device_data->power_state_spinlock);
702 static int cryp_enable_power(
704 struct cryp_device_data *device_data,
705 bool restore_device_context)
709 dev_dbg(dev, "[%s]", __func__);
711 spin_lock(&device_data->power_state_spinlock);
712 if (!device_data->power_state) {
713 ret = regulator_enable(device_data->pwr_regulator);
715 dev_err(dev, "[%s]: regulator_enable() failed!",
720 ret = clk_enable(device_data->clk);
722 dev_err(dev, "[%s]: clk_enable() failed!",
724 regulator_disable(device_data->pwr_regulator);
727 device_data->power_state = true;
730 if (device_data->restore_dev_ctx) {
731 spin_lock(&device_data->ctx_lock);
732 if (restore_device_context && device_data->current_ctx) {
733 device_data->restore_dev_ctx = false;
734 cryp_restore_device_context(device_data,
735 &device_data->current_ctx->dev_ctx);
737 spin_unlock(&device_data->ctx_lock);
740 spin_unlock(&device_data->power_state_spinlock);
745 static int hw_crypt_noxts(struct cryp_ctx *ctx,
746 struct cryp_device_data *device_data)
750 const u8 *indata = ctx->indata;
751 u8 *outdata = ctx->outdata;
752 u32 datalen = ctx->datalen;
753 u32 outlen = datalen;
755 pr_debug(DEV_DBG_NAME " [%s]", __func__);
757 ctx->outlen = ctx->datalen;
759 if (unlikely(!IS_ALIGNED((unsigned long)indata, 4))) {
760 pr_debug(DEV_DBG_NAME " [%s]: Data isn't aligned! Addr: "
761 "0x%08lx", __func__, (unsigned long)indata);
765 ret = cryp_setup_context(ctx, device_data);
770 if (cryp_mode == CRYP_MODE_INTERRUPT) {
771 cryp_enable_irq_src(device_data, CRYP_IRQ_SRC_INPUT_FIFO |
772 CRYP_IRQ_SRC_OUTPUT_FIFO);
775 * ctx->outlen is decremented in the cryp_interrupt_handler
776 * function. We had to add cpu_relax() (barrier) to make sure
777 * that gcc didn't optimze away this variable.
779 while (ctx->outlen > 0)
781 } else if (cryp_mode == CRYP_MODE_POLLING ||
782 cryp_mode == CRYP_MODE_DMA) {
784 * The reason for having DMA in this if case is that if we are
785 * running cryp_mode = 2, then we separate DMA routines for
786 * handling cipher/plaintext > blocksize, except when
787 * running the normal CRYPTO_ALG_TYPE_CIPHER, then we still use
788 * the polling mode. Overhead of doing DMA setup eats up the
791 cryp_polling_mode(ctx, device_data);
793 dev_err(ctx->device->dev, "[%s]: Invalid operation mode!",
799 cryp_save_device_context(device_data, &ctx->dev_ctx, cryp_mode);
803 ctx->indata = indata;
804 ctx->outdata = outdata;
805 ctx->datalen = datalen;
806 ctx->outlen = outlen;
811 static int get_nents(struct scatterlist *sg, int nbytes)
816 nbytes -= sg->length;
824 static int ablk_dma_crypt(struct skcipher_request *areq)
826 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
827 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
828 struct cryp_device_data *device_data;
830 int bytes_written = 0;
834 pr_debug(DEV_DBG_NAME " [%s]", __func__);
836 ctx->datalen = areq->cryptlen;
837 ctx->outlen = areq->cryptlen;
839 ret = cryp_get_device_data(ctx, &device_data);
843 ret = cryp_setup_context(ctx, device_data);
847 /* We have the device now, so store the nents in the dma struct. */
848 ctx->device->dma.nents_src = get_nents(areq->src, ctx->datalen);
849 ctx->device->dma.nents_dst = get_nents(areq->dst, ctx->outlen);
851 /* Enable DMA in- and output. */
852 cryp_configure_for_dma(device_data, CRYP_DMA_ENABLE_BOTH_DIRECTIONS);
854 bytes_written = cryp_dma_write(ctx, areq->src, ctx->datalen);
855 bytes_read = cryp_dma_read(ctx, areq->dst, bytes_written);
857 wait_for_completion(&ctx->device->dma.cryp_dma_complete);
860 cryp_save_device_context(device_data, &ctx->dev_ctx, cryp_mode);
864 spin_lock(&device_data->ctx_lock);
865 device_data->current_ctx = NULL;
867 spin_unlock(&device_data->ctx_lock);
870 * The down_interruptible part for this semaphore is called in
871 * cryp_get_device_data.
873 up(&driver_data.device_allocation);
875 if (unlikely(bytes_written != bytes_read))
881 static int ablk_crypt(struct skcipher_request *areq)
883 struct skcipher_walk walk;
884 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
885 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
886 struct cryp_device_data *device_data;
887 unsigned long src_paddr;
888 unsigned long dst_paddr;
892 pr_debug(DEV_DBG_NAME " [%s]", __func__);
894 ret = cryp_get_device_data(ctx, &device_data);
898 ret = skcipher_walk_async(&walk, areq);
901 pr_err(DEV_DBG_NAME "[%s]: skcipher_walk_async() failed!",
906 while ((nbytes = walk.nbytes) > 0) {
908 src_paddr = (page_to_phys(walk.src.phys.page) + walk.src.phys.offset);
909 ctx->indata = phys_to_virt(src_paddr);
911 dst_paddr = (page_to_phys(walk.dst.phys.page) + walk.dst.phys.offset);
912 ctx->outdata = phys_to_virt(dst_paddr);
914 ctx->datalen = nbytes - (nbytes % ctx->blocksize);
916 ret = hw_crypt_noxts(ctx, device_data);
920 nbytes -= ctx->datalen;
921 ret = skcipher_walk_done(&walk, nbytes);
927 /* Release the device */
928 spin_lock(&device_data->ctx_lock);
929 device_data->current_ctx = NULL;
931 spin_unlock(&device_data->ctx_lock);
934 * The down_interruptible part for this semaphore is called in
935 * cryp_get_device_data.
937 up(&driver_data.device_allocation);
942 static int aes_skcipher_setkey(struct crypto_skcipher *cipher,
943 const u8 *key, unsigned int keylen)
945 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
947 pr_debug(DEV_DBG_NAME " [%s]", __func__);
950 case AES_KEYSIZE_128:
951 ctx->config.keysize = CRYP_KEY_SIZE_128;
954 case AES_KEYSIZE_192:
955 ctx->config.keysize = CRYP_KEY_SIZE_192;
958 case AES_KEYSIZE_256:
959 ctx->config.keysize = CRYP_KEY_SIZE_256;
963 pr_err(DEV_DBG_NAME "[%s]: Unknown keylen!", __func__);
967 memcpy(ctx->key, key, keylen);
968 ctx->keylen = keylen;
975 static int des_skcipher_setkey(struct crypto_skcipher *cipher,
976 const u8 *key, unsigned int keylen)
978 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
981 pr_debug(DEV_DBG_NAME " [%s]", __func__);
983 err = verify_skcipher_des_key(cipher, key);
987 memcpy(ctx->key, key, keylen);
988 ctx->keylen = keylen;
994 static int des3_skcipher_setkey(struct crypto_skcipher *cipher,
995 const u8 *key, unsigned int keylen)
997 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
1000 pr_debug(DEV_DBG_NAME " [%s]", __func__);
1002 err = verify_skcipher_des3_key(cipher, key);
1006 memcpy(ctx->key, key, keylen);
1007 ctx->keylen = keylen;
1013 static int cryp_blk_encrypt(struct skcipher_request *areq)
1015 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
1016 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
1018 pr_debug(DEV_DBG_NAME " [%s]", __func__);
1020 ctx->config.algodir = CRYP_ALGORITHM_ENCRYPT;
1023 * DMA does not work for DES due to a hw bug */
1024 if (cryp_mode == CRYP_MODE_DMA && mode_is_aes(ctx->config.algomode))
1025 return ablk_dma_crypt(areq);
1027 /* For everything except DMA, we run the non DMA version. */
1028 return ablk_crypt(areq);
1031 static int cryp_blk_decrypt(struct skcipher_request *areq)
1033 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
1034 struct cryp_ctx *ctx = crypto_skcipher_ctx(cipher);
1036 pr_debug(DEV_DBG_NAME " [%s]", __func__);
1038 ctx->config.algodir = CRYP_ALGORITHM_DECRYPT;
1040 /* DMA does not work for DES due to a hw bug */
1041 if (cryp_mode == CRYP_MODE_DMA && mode_is_aes(ctx->config.algomode))
1042 return ablk_dma_crypt(areq);
1044 /* For everything except DMA, we run the non DMA version. */
1045 return ablk_crypt(areq);
1048 struct cryp_algo_template {
1049 enum cryp_algo_mode algomode;
1050 struct skcipher_alg skcipher;
1053 static int cryp_init_tfm(struct crypto_skcipher *tfm)
1055 struct cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
1056 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1057 struct cryp_algo_template *cryp_alg = container_of(alg,
1058 struct cryp_algo_template,
1061 ctx->config.algomode = cryp_alg->algomode;
1062 ctx->blocksize = crypto_skcipher_blocksize(tfm);
1067 static struct cryp_algo_template cryp_algs[] = {
1069 .algomode = CRYP_ALGO_AES_ECB,
1071 .base.cra_name = "ecb(aes)",
1072 .base.cra_driver_name = "ecb-aes-ux500",
1073 .base.cra_priority = 300,
1074 .base.cra_flags = CRYPTO_ALG_ASYNC,
1075 .base.cra_blocksize = AES_BLOCK_SIZE,
1076 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1077 .base.cra_alignmask = 3,
1078 .base.cra_module = THIS_MODULE,
1080 .min_keysize = AES_MIN_KEY_SIZE,
1081 .max_keysize = AES_MAX_KEY_SIZE,
1082 .setkey = aes_skcipher_setkey,
1083 .encrypt = cryp_blk_encrypt,
1084 .decrypt = cryp_blk_decrypt,
1085 .init = cryp_init_tfm,
1089 .algomode = CRYP_ALGO_AES_CBC,
1091 .base.cra_name = "cbc(aes)",
1092 .base.cra_driver_name = "cbc-aes-ux500",
1093 .base.cra_priority = 300,
1094 .base.cra_flags = CRYPTO_ALG_ASYNC,
1095 .base.cra_blocksize = AES_BLOCK_SIZE,
1096 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1097 .base.cra_alignmask = 3,
1098 .base.cra_module = THIS_MODULE,
1100 .min_keysize = AES_MIN_KEY_SIZE,
1101 .max_keysize = AES_MAX_KEY_SIZE,
1102 .setkey = aes_skcipher_setkey,
1103 .encrypt = cryp_blk_encrypt,
1104 .decrypt = cryp_blk_decrypt,
1105 .init = cryp_init_tfm,
1106 .ivsize = AES_BLOCK_SIZE,
1110 .algomode = CRYP_ALGO_AES_CTR,
1112 .base.cra_name = "ctr(aes)",
1113 .base.cra_driver_name = "ctr-aes-ux500",
1114 .base.cra_priority = 300,
1115 .base.cra_flags = CRYPTO_ALG_ASYNC,
1116 .base.cra_blocksize = 1,
1117 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1118 .base.cra_alignmask = 3,
1119 .base.cra_module = THIS_MODULE,
1121 .min_keysize = AES_MIN_KEY_SIZE,
1122 .max_keysize = AES_MAX_KEY_SIZE,
1123 .setkey = aes_skcipher_setkey,
1124 .encrypt = cryp_blk_encrypt,
1125 .decrypt = cryp_blk_decrypt,
1126 .init = cryp_init_tfm,
1127 .ivsize = AES_BLOCK_SIZE,
1128 .chunksize = AES_BLOCK_SIZE,
1132 .algomode = CRYP_ALGO_DES_ECB,
1134 .base.cra_name = "ecb(des)",
1135 .base.cra_driver_name = "ecb-des-ux500",
1136 .base.cra_priority = 300,
1137 .base.cra_flags = CRYPTO_ALG_ASYNC,
1138 .base.cra_blocksize = DES_BLOCK_SIZE,
1139 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1140 .base.cra_alignmask = 3,
1141 .base.cra_module = THIS_MODULE,
1143 .min_keysize = DES_KEY_SIZE,
1144 .max_keysize = DES_KEY_SIZE,
1145 .setkey = des_skcipher_setkey,
1146 .encrypt = cryp_blk_encrypt,
1147 .decrypt = cryp_blk_decrypt,
1148 .init = cryp_init_tfm,
1152 .algomode = CRYP_ALGO_TDES_ECB,
1154 .base.cra_name = "ecb(des3_ede)",
1155 .base.cra_driver_name = "ecb-des3_ede-ux500",
1156 .base.cra_priority = 300,
1157 .base.cra_flags = CRYPTO_ALG_ASYNC,
1158 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1159 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1160 .base.cra_alignmask = 3,
1161 .base.cra_module = THIS_MODULE,
1163 .min_keysize = DES3_EDE_KEY_SIZE,
1164 .max_keysize = DES3_EDE_KEY_SIZE,
1165 .setkey = des3_skcipher_setkey,
1166 .encrypt = cryp_blk_encrypt,
1167 .decrypt = cryp_blk_decrypt,
1168 .init = cryp_init_tfm,
1172 .algomode = CRYP_ALGO_DES_CBC,
1174 .base.cra_name = "cbc(des)",
1175 .base.cra_driver_name = "cbc-des-ux500",
1176 .base.cra_priority = 300,
1177 .base.cra_flags = CRYPTO_ALG_ASYNC,
1178 .base.cra_blocksize = DES_BLOCK_SIZE,
1179 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1180 .base.cra_alignmask = 3,
1181 .base.cra_module = THIS_MODULE,
1183 .min_keysize = DES_KEY_SIZE,
1184 .max_keysize = DES_KEY_SIZE,
1185 .setkey = des_skcipher_setkey,
1186 .encrypt = cryp_blk_encrypt,
1187 .decrypt = cryp_blk_decrypt,
1188 .ivsize = DES_BLOCK_SIZE,
1189 .init = cryp_init_tfm,
1193 .algomode = CRYP_ALGO_TDES_CBC,
1195 .base.cra_name = "cbc(des3_ede)",
1196 .base.cra_driver_name = "cbc-des3_ede-ux500",
1197 .base.cra_priority = 300,
1198 .base.cra_flags = CRYPTO_ALG_ASYNC,
1199 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1200 .base.cra_ctxsize = sizeof(struct cryp_ctx),
1201 .base.cra_alignmask = 3,
1202 .base.cra_module = THIS_MODULE,
1204 .min_keysize = DES3_EDE_KEY_SIZE,
1205 .max_keysize = DES3_EDE_KEY_SIZE,
1206 .setkey = des3_skcipher_setkey,
1207 .encrypt = cryp_blk_encrypt,
1208 .decrypt = cryp_blk_decrypt,
1209 .ivsize = DES3_EDE_BLOCK_SIZE,
1210 .init = cryp_init_tfm,
1216 * cryp_algs_register_all -
1218 static int cryp_algs_register_all(void)
1224 pr_debug("[%s]", __func__);
1226 for (i = 0; i < ARRAY_SIZE(cryp_algs); i++) {
1227 ret = crypto_register_skcipher(&cryp_algs[i].skcipher);
1230 pr_err("[%s] alg registration failed",
1231 cryp_algs[i].skcipher.base.cra_driver_name);
1237 for (i = 0; i < count; i++)
1238 crypto_unregister_skcipher(&cryp_algs[i].skcipher);
1243 * cryp_algs_unregister_all -
1245 static void cryp_algs_unregister_all(void)
1249 pr_debug(DEV_DBG_NAME " [%s]", __func__);
1251 for (i = 0; i < ARRAY_SIZE(cryp_algs); i++)
1252 crypto_unregister_skcipher(&cryp_algs[i].skcipher);
1255 static int ux500_cryp_probe(struct platform_device *pdev)
1258 struct resource *res;
1259 struct resource *res_irq;
1260 struct cryp_device_data *device_data;
1261 struct cryp_protection_config prot = {
1262 .privilege_access = CRYP_STATE_ENABLE
1264 struct device *dev = &pdev->dev;
1266 dev_dbg(dev, "[%s]", __func__);
1267 device_data = devm_kzalloc(dev, sizeof(*device_data), GFP_ATOMIC);
1273 device_data->dev = dev;
1274 device_data->current_ctx = NULL;
1276 /* Grab the DMA configuration from platform data. */
1277 mem_to_engine = &((struct cryp_platform_data *)
1278 dev->platform_data)->mem_to_engine;
1279 engine_to_mem = &((struct cryp_platform_data *)
1280 dev->platform_data)->engine_to_mem;
1282 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1284 dev_err(dev, "[%s]: platform_get_resource() failed",
1290 device_data->phybase = res->start;
1291 device_data->base = devm_ioremap_resource(dev, res);
1292 if (IS_ERR(device_data->base)) {
1293 dev_err(dev, "[%s]: ioremap failed!", __func__);
1294 ret = PTR_ERR(device_data->base);
1298 spin_lock_init(&device_data->ctx_lock);
1299 spin_lock_init(&device_data->power_state_spinlock);
1301 /* Enable power for CRYP hardware block */
1302 device_data->pwr_regulator = regulator_get(&pdev->dev, "v-ape");
1303 if (IS_ERR(device_data->pwr_regulator)) {
1304 dev_err(dev, "[%s]: could not get cryp regulator", __func__);
1305 ret = PTR_ERR(device_data->pwr_regulator);
1306 device_data->pwr_regulator = NULL;
1310 /* Enable the clk for CRYP hardware block */
1311 device_data->clk = devm_clk_get(&pdev->dev, NULL);
1312 if (IS_ERR(device_data->clk)) {
1313 dev_err(dev, "[%s]: clk_get() failed!", __func__);
1314 ret = PTR_ERR(device_data->clk);
1318 ret = clk_prepare(device_data->clk);
1320 dev_err(dev, "[%s]: clk_prepare() failed!", __func__);
1324 /* Enable device power (and clock) */
1325 ret = cryp_enable_power(device_data->dev, device_data, false);
1327 dev_err(dev, "[%s]: cryp_enable_power() failed!", __func__);
1328 goto out_clk_unprepare;
1331 if (cryp_check(device_data)) {
1332 dev_err(dev, "[%s]: cryp_check() failed!", __func__);
1337 if (cryp_configure_protection(device_data, &prot)) {
1338 dev_err(dev, "[%s]: cryp_configure_protection() failed!",
1344 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1346 dev_err(dev, "[%s]: IORESOURCE_IRQ unavailable",
1352 ret = devm_request_irq(&pdev->dev, res_irq->start,
1353 cryp_interrupt_handler, 0, "cryp1", device_data);
1355 dev_err(dev, "[%s]: Unable to request IRQ", __func__);
1359 if (cryp_mode == CRYP_MODE_DMA)
1360 cryp_dma_setup_channel(device_data, dev);
1362 platform_set_drvdata(pdev, device_data);
1364 /* Put the new device into the device list... */
1365 klist_add_tail(&device_data->list_node, &driver_data.device_list);
1367 /* ... and signal that a new device is available. */
1368 up(&driver_data.device_allocation);
1370 atomic_set(&session_id, 1);
1372 ret = cryp_algs_register_all();
1374 dev_err(dev, "[%s]: cryp_algs_register_all() failed!",
1379 dev_info(dev, "successfully registered\n");
1384 cryp_disable_power(device_data->dev, device_data, false);
1387 clk_unprepare(device_data->clk);
1390 regulator_put(device_data->pwr_regulator);
1396 static int ux500_cryp_remove(struct platform_device *pdev)
1398 struct cryp_device_data *device_data;
1400 dev_dbg(&pdev->dev, "[%s]", __func__);
1401 device_data = platform_get_drvdata(pdev);
1403 dev_err(&pdev->dev, "[%s]: platform_get_drvdata() failed!",
1408 /* Try to decrease the number of available devices. */
1409 if (down_trylock(&driver_data.device_allocation))
1412 /* Check that the device is free */
1413 spin_lock(&device_data->ctx_lock);
1414 /* current_ctx allocates a device, NULL = unallocated */
1415 if (device_data->current_ctx) {
1416 /* The device is busy */
1417 spin_unlock(&device_data->ctx_lock);
1418 /* Return the device to the pool. */
1419 up(&driver_data.device_allocation);
1423 spin_unlock(&device_data->ctx_lock);
1425 /* Remove the device from the list */
1426 if (klist_node_attached(&device_data->list_node))
1427 klist_remove(&device_data->list_node);
1429 /* If this was the last device, remove the services */
1430 if (list_empty(&driver_data.device_list.k_list))
1431 cryp_algs_unregister_all();
1433 if (cryp_disable_power(&pdev->dev, device_data, false))
1434 dev_err(&pdev->dev, "[%s]: cryp_disable_power() failed",
1437 clk_unprepare(device_data->clk);
1438 regulator_put(device_data->pwr_regulator);
1443 static void ux500_cryp_shutdown(struct platform_device *pdev)
1445 struct cryp_device_data *device_data;
1447 dev_dbg(&pdev->dev, "[%s]", __func__);
1449 device_data = platform_get_drvdata(pdev);
1451 dev_err(&pdev->dev, "[%s]: platform_get_drvdata() failed!",
1456 /* Check that the device is free */
1457 spin_lock(&device_data->ctx_lock);
1458 /* current_ctx allocates a device, NULL = unallocated */
1459 if (!device_data->current_ctx) {
1460 if (down_trylock(&driver_data.device_allocation))
1461 dev_dbg(&pdev->dev, "[%s]: Cryp still in use!"
1462 "Shutting down anyway...", __func__);
1464 * (Allocate the device)
1465 * Need to set this to non-null (dummy) value,
1466 * to avoid usage if context switching.
1468 device_data->current_ctx++;
1470 spin_unlock(&device_data->ctx_lock);
1472 /* Remove the device from the list */
1473 if (klist_node_attached(&device_data->list_node))
1474 klist_remove(&device_data->list_node);
1476 /* If this was the last device, remove the services */
1477 if (list_empty(&driver_data.device_list.k_list))
1478 cryp_algs_unregister_all();
1480 if (cryp_disable_power(&pdev->dev, device_data, false))
1481 dev_err(&pdev->dev, "[%s]: cryp_disable_power() failed",
1486 #ifdef CONFIG_PM_SLEEP
1487 static int ux500_cryp_suspend(struct device *dev)
1490 struct platform_device *pdev = to_platform_device(dev);
1491 struct cryp_device_data *device_data;
1492 struct resource *res_irq;
1493 struct cryp_ctx *temp_ctx = NULL;
1495 dev_dbg(dev, "[%s]", __func__);
1498 device_data = platform_get_drvdata(pdev);
1500 dev_err(dev, "[%s]: platform_get_drvdata() failed!", __func__);
1504 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1506 dev_err(dev, "[%s]: IORESOURCE_IRQ, unavailable", __func__);
1508 disable_irq(res_irq->start);
1510 spin_lock(&device_data->ctx_lock);
1511 if (!device_data->current_ctx)
1512 device_data->current_ctx++;
1513 spin_unlock(&device_data->ctx_lock);
1515 if (device_data->current_ctx == ++temp_ctx) {
1516 if (down_interruptible(&driver_data.device_allocation))
1517 dev_dbg(dev, "[%s]: down_interruptible() failed",
1519 ret = cryp_disable_power(dev, device_data, false);
1522 ret = cryp_disable_power(dev, device_data, true);
1525 dev_err(dev, "[%s]: cryp_disable_power()", __func__);
1530 static int ux500_cryp_resume(struct device *dev)
1533 struct platform_device *pdev = to_platform_device(dev);
1534 struct cryp_device_data *device_data;
1535 struct resource *res_irq;
1536 struct cryp_ctx *temp_ctx = NULL;
1538 dev_dbg(dev, "[%s]", __func__);
1540 device_data = platform_get_drvdata(pdev);
1542 dev_err(dev, "[%s]: platform_get_drvdata() failed!", __func__);
1546 spin_lock(&device_data->ctx_lock);
1547 if (device_data->current_ctx == ++temp_ctx)
1548 device_data->current_ctx = NULL;
1549 spin_unlock(&device_data->ctx_lock);
1552 if (!device_data->current_ctx)
1553 up(&driver_data.device_allocation);
1555 ret = cryp_enable_power(dev, device_data, true);
1558 dev_err(dev, "[%s]: cryp_enable_power() failed!", __func__);
1560 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1562 enable_irq(res_irq->start);
1569 static SIMPLE_DEV_PM_OPS(ux500_cryp_pm, ux500_cryp_suspend, ux500_cryp_resume);
1571 static const struct of_device_id ux500_cryp_match[] = {
1572 { .compatible = "stericsson,ux500-cryp" },
1575 MODULE_DEVICE_TABLE(of, ux500_cryp_match);
1577 static struct platform_driver cryp_driver = {
1578 .probe = ux500_cryp_probe,
1579 .remove = ux500_cryp_remove,
1580 .shutdown = ux500_cryp_shutdown,
1583 .of_match_table = ux500_cryp_match,
1584 .pm = &ux500_cryp_pm,
1588 static int __init ux500_cryp_mod_init(void)
1590 pr_debug("[%s] is called!", __func__);
1591 klist_init(&driver_data.device_list, NULL, NULL);
1592 /* Initialize the semaphore to 0 devices (locked state) */
1593 sema_init(&driver_data.device_allocation, 0);
1594 return platform_driver_register(&cryp_driver);
1597 static void __exit ux500_cryp_mod_fini(void)
1599 pr_debug("[%s] is called!", __func__);
1600 platform_driver_unregister(&cryp_driver);
1603 module_init(ux500_cryp_mod_init);
1604 module_exit(ux500_cryp_mod_fini);
1606 module_param(cryp_mode, int, 0);
1608 MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 CRYP crypto engine.");
1609 MODULE_ALIAS_CRYPTO("aes-all");
1610 MODULE_ALIAS_CRYPTO("des-all");
1612 MODULE_LICENSE("GPL");