2 * Copyright (C) ST-Ericsson SA 2010
3 * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
4 * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
5 * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
6 * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
7 * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
8 * Author: Andreas Westin <andreas.westin@stericsson.com> for ST-Ericsson.
9 * License terms: GNU General Public License (GPL) version 2
12 #include <linux/clk.h>
13 #include <linux/completion.h>
14 #include <linux/crypto.h>
15 #include <linux/dmaengine.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/interrupt.h>
20 #include <linux/irqreturn.h>
21 #include <linux/klist.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/semaphore.h>
26 #include <linux/platform_data/dma-ste-dma40.h>
28 #include <crypto/aes.h>
29 #include <crypto/algapi.h>
30 #include <crypto/ctr.h>
31 #include <crypto/des.h>
32 #include <crypto/scatterwalk.h>
34 #include <linux/platform_data/crypto-ux500.h>
39 #define CRYP_MAX_KEY_SIZE 32
40 #define BYTES_PER_WORD 4
43 static atomic_t session_id
;
45 static struct stedma40_chan_cfg
*mem_to_engine
;
46 static struct stedma40_chan_cfg
*engine_to_mem
;
49 * struct cryp_driver_data - data specific to the driver.
51 * @device_list: A list of registered devices to choose from.
52 * @device_allocation: A semaphore initialized with number of devices.
54 struct cryp_driver_data
{
55 struct klist device_list
;
56 struct semaphore device_allocation
;
60 * struct cryp_ctx - Crypto context
61 * @config: Crypto mode.
62 * @key[CRYP_MAX_KEY_SIZE]: Key.
63 * @keylen: Length of key.
64 * @iv: Pointer to initialization vector.
65 * @indata: Pointer to indata.
66 * @outdata: Pointer to outdata.
67 * @datalen: Length of indata.
68 * @outlen: Length of outdata.
69 * @blocksize: Size of blocks.
70 * @updated: Updated flag.
71 * @dev_ctx: Device dependent context.
72 * @device: Pointer to the device.
75 struct cryp_config config
;
76 u8 key
[CRYP_MAX_KEY_SIZE
];
85 struct cryp_device_context dev_ctx
;
86 struct cryp_device_data
*device
;
90 static struct cryp_driver_data driver_data
;
93 * uint8p_to_uint32_be - 4*uint8 to uint32 big endian
94 * @in: Data to convert.
96 static inline u32
uint8p_to_uint32_be(u8
*in
)
98 u32
*data
= (u32
*)in
;
100 return cpu_to_be32p(data
);
104 * swap_bits_in_byte - mirror the bits in a byte
105 * @b: the byte to be mirrored
107 * The bits are swapped the following way:
108 * Byte b include bits 0-7, nibble 1 (n1) include bits 0-3 and
109 * nibble 2 (n2) bits 4-7.
112 * (The "old" (moved) bit is replaced with a zero)
113 * 1. Move bit 6 and 7, 4 positions to the left.
114 * 2. Move bit 3 and 5, 2 positions to the left.
115 * 3. Move bit 1-4, 1 position to the left.
118 * 1. Move bit 0 and 1, 4 positions to the right.
119 * 2. Move bit 2 and 4, 2 positions to the right.
120 * 3. Move bit 3-6, 1 position to the right.
122 * Combine the two nibbles to a complete and swapped byte.
125 static inline u8
swap_bits_in_byte(u8 b
)
127 #define R_SHIFT_4_MASK 0xc0 /* Bits 6 and 7, right shift 4 */
128 #define R_SHIFT_2_MASK 0x28 /* (After right shift 4) Bits 3 and 5,
130 #define R_SHIFT_1_MASK 0x1e /* (After right shift 2) Bits 1-4,
132 #define L_SHIFT_4_MASK 0x03 /* Bits 0 and 1, left shift 4 */
133 #define L_SHIFT_2_MASK 0x14 /* (After left shift 4) Bits 2 and 4,
135 #define L_SHIFT_1_MASK 0x78 /* (After left shift 1) Bits 3-6,
141 /* Swap most significant nibble */
142 /* Right shift 4, bits 6 and 7 */
143 n1
= ((b
& R_SHIFT_4_MASK
) >> 4) | (b
& ~(R_SHIFT_4_MASK
>> 4));
144 /* Right shift 2, bits 3 and 5 */
145 n1
= ((n1
& R_SHIFT_2_MASK
) >> 2) | (n1
& ~(R_SHIFT_2_MASK
>> 2));
146 /* Right shift 1, bits 1-4 */
147 n1
= (n1
& R_SHIFT_1_MASK
) >> 1;
149 /* Swap least significant nibble */
150 /* Left shift 4, bits 0 and 1 */
151 n2
= ((b
& L_SHIFT_4_MASK
) << 4) | (b
& ~(L_SHIFT_4_MASK
<< 4));
152 /* Left shift 2, bits 2 and 4 */
153 n2
= ((n2
& L_SHIFT_2_MASK
) << 2) | (n2
& ~(L_SHIFT_2_MASK
<< 2));
154 /* Left shift 1, bits 3-6 */
155 n2
= (n2
& L_SHIFT_1_MASK
) << 1;
160 static inline void swap_words_in_key_and_bits_in_byte(const u8
*in
,
167 j
= len
- BYTES_PER_WORD
;
169 for (i
= 0; i
< BYTES_PER_WORD
; i
++) {
170 index
= len
- j
- BYTES_PER_WORD
+ i
;
172 swap_bits_in_byte(in
[index
]);
178 static void add_session_id(struct cryp_ctx
*ctx
)
181 * We never want 0 to be a valid value, since this is the default value
182 * for the software context.
184 if (unlikely(atomic_inc_and_test(&session_id
)))
185 atomic_inc(&session_id
);
187 ctx
->session_id
= atomic_read(&session_id
);
190 static irqreturn_t
cryp_interrupt_handler(int irq
, void *param
)
192 struct cryp_ctx
*ctx
;
194 struct cryp_device_data
*device_data
;
201 /* The device is coming from the one found in hw_crypt_noxts. */
202 device_data
= (struct cryp_device_data
*)param
;
204 ctx
= device_data
->current_ctx
;
211 dev_dbg(ctx
->device
->dev
, "[%s] (len: %d) %s, ", __func__
, ctx
->outlen
,
212 cryp_pending_irq_src(device_data
, CRYP_IRQ_SRC_OUTPUT_FIFO
) ?
215 if (cryp_pending_irq_src(device_data
,
216 CRYP_IRQ_SRC_OUTPUT_FIFO
)) {
217 if (ctx
->outlen
/ ctx
->blocksize
> 0) {
218 count
= ctx
->blocksize
/ 4;
220 readsl(&device_data
->base
->dout
, ctx
->outdata
, count
);
221 ctx
->outdata
+= count
;
222 ctx
->outlen
-= count
;
224 if (ctx
->outlen
== 0) {
225 cryp_disable_irq_src(device_data
,
226 CRYP_IRQ_SRC_OUTPUT_FIFO
);
229 } else if (cryp_pending_irq_src(device_data
,
230 CRYP_IRQ_SRC_INPUT_FIFO
)) {
231 if (ctx
->datalen
/ ctx
->blocksize
> 0) {
232 count
= ctx
->blocksize
/ 4;
234 writesl(&device_data
->base
->din
, ctx
->indata
, count
);
236 ctx
->indata
+= count
;
237 ctx
->datalen
-= count
;
239 if (ctx
->datalen
== 0)
240 cryp_disable_irq_src(device_data
,
241 CRYP_IRQ_SRC_INPUT_FIFO
);
243 if (ctx
->config
.algomode
== CRYP_ALGO_AES_XTS
) {
244 CRYP_PUT_BITS(&device_data
->base
->cr
,
249 cryp_wait_until_done(device_data
);
257 static int mode_is_aes(enum cryp_algo_mode mode
)
259 return CRYP_ALGO_AES_ECB
== mode
||
260 CRYP_ALGO_AES_CBC
== mode
||
261 CRYP_ALGO_AES_CTR
== mode
||
262 CRYP_ALGO_AES_XTS
== mode
;
265 static int cfg_iv(struct cryp_device_data
*device_data
, u32 left
, u32 right
,
266 enum cryp_init_vector_index index
)
268 struct cryp_init_vector_value vector_value
;
270 dev_dbg(device_data
->dev
, "[%s]", __func__
);
272 vector_value
.init_value_left
= left
;
273 vector_value
.init_value_right
= right
;
275 return cryp_configure_init_vector(device_data
,
280 static int cfg_ivs(struct cryp_device_data
*device_data
, struct cryp_ctx
*ctx
)
284 int num_of_regs
= ctx
->blocksize
/ 8;
285 u32 iv
[AES_BLOCK_SIZE
/ 4];
287 dev_dbg(device_data
->dev
, "[%s]", __func__
);
290 * Since we loop on num_of_regs we need to have a check in case
291 * someone provides an incorrect blocksize which would force calling
292 * cfg_iv with i greater than 2 which is an error.
294 if (num_of_regs
> 2) {
295 dev_err(device_data
->dev
, "[%s] Incorrect blocksize %d",
296 __func__
, ctx
->blocksize
);
300 for (i
= 0; i
< ctx
->blocksize
/ 4; i
++)
301 iv
[i
] = uint8p_to_uint32_be(ctx
->iv
+ i
*4);
303 for (i
= 0; i
< num_of_regs
; i
++) {
304 status
= cfg_iv(device_data
, iv
[i
*2], iv
[i
*2+1],
305 (enum cryp_init_vector_index
) i
);
312 static int set_key(struct cryp_device_data
*device_data
,
315 enum cryp_key_reg_index index
)
317 struct cryp_key_value key_value
;
320 dev_dbg(device_data
->dev
, "[%s]", __func__
);
322 key_value
.key_value_left
= left_key
;
323 key_value
.key_value_right
= right_key
;
325 cryp_error
= cryp_configure_key_values(device_data
,
329 dev_err(device_data
->dev
, "[%s]: "
330 "cryp_configure_key_values() failed!", __func__
);
335 static int cfg_keys(struct cryp_ctx
*ctx
)
338 int num_of_regs
= ctx
->keylen
/ 8;
339 u32 swapped_key
[CRYP_MAX_KEY_SIZE
/ 4];
342 dev_dbg(ctx
->device
->dev
, "[%s]", __func__
);
344 if (mode_is_aes(ctx
->config
.algomode
)) {
345 swap_words_in_key_and_bits_in_byte((u8
*)ctx
->key
,
349 for (i
= 0; i
< ctx
->keylen
/ 4; i
++)
350 swapped_key
[i
] = uint8p_to_uint32_be(ctx
->key
+ i
*4);
353 for (i
= 0; i
< num_of_regs
; i
++) {
354 cryp_error
= set_key(ctx
->device
,
355 *(((u32
*)swapped_key
)+i
*2),
356 *(((u32
*)swapped_key
)+i
*2+1),
357 (enum cryp_key_reg_index
) i
);
359 if (cryp_error
!= 0) {
360 dev_err(ctx
->device
->dev
, "[%s]: set_key() failed!",
368 static int cryp_setup_context(struct cryp_ctx
*ctx
,
369 struct cryp_device_data
*device_data
)
371 u32 control_register
= CRYP_CR_DEFAULT
;
374 case CRYP_MODE_INTERRUPT
:
375 writel_relaxed(CRYP_IMSC_DEFAULT
, &device_data
->base
->imsc
);
379 writel_relaxed(CRYP_DMACR_DEFAULT
, &device_data
->base
->dmacr
);
386 if (ctx
->updated
== 0) {
387 cryp_flush_inoutfifo(device_data
);
388 if (cfg_keys(ctx
) != 0) {
389 dev_err(ctx
->device
->dev
, "[%s]: cfg_keys failed!",
395 CRYP_ALGO_AES_ECB
!= ctx
->config
.algomode
&&
396 CRYP_ALGO_DES_ECB
!= ctx
->config
.algomode
&&
397 CRYP_ALGO_TDES_ECB
!= ctx
->config
.algomode
) {
398 if (cfg_ivs(device_data
, ctx
) != 0)
402 cryp_set_configuration(device_data
, &ctx
->config
,
405 } else if (ctx
->updated
== 1 &&
406 ctx
->session_id
!= atomic_read(&session_id
)) {
407 cryp_flush_inoutfifo(device_data
);
408 cryp_restore_device_context(device_data
, &ctx
->dev_ctx
);
411 control_register
= ctx
->dev_ctx
.cr
;
413 control_register
= ctx
->dev_ctx
.cr
;
415 writel(control_register
|
416 (CRYP_CRYPEN_ENABLE
<< CRYP_CR_CRYPEN_POS
),
417 &device_data
->base
->cr
);
422 static int cryp_get_device_data(struct cryp_ctx
*ctx
,
423 struct cryp_device_data
**device_data
)
426 struct klist_iter device_iterator
;
427 struct klist_node
*device_node
;
428 struct cryp_device_data
*local_device_data
= NULL
;
429 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
431 /* Wait until a device is available */
432 ret
= down_interruptible(&driver_data
.device_allocation
);
434 return ret
; /* Interrupted */
436 /* Select a device */
437 klist_iter_init(&driver_data
.device_list
, &device_iterator
);
439 device_node
= klist_next(&device_iterator
);
440 while (device_node
) {
441 local_device_data
= container_of(device_node
,
442 struct cryp_device_data
, list_node
);
443 spin_lock(&local_device_data
->ctx_lock
);
444 /* current_ctx allocates a device, NULL = unallocated */
445 if (local_device_data
->current_ctx
) {
446 device_node
= klist_next(&device_iterator
);
448 local_device_data
->current_ctx
= ctx
;
449 ctx
->device
= local_device_data
;
450 spin_unlock(&local_device_data
->ctx_lock
);
453 spin_unlock(&local_device_data
->ctx_lock
);
455 klist_iter_exit(&device_iterator
);
459 * No free device found.
460 * Since we allocated a device with down_interruptible, this
461 * should not be able to happen.
462 * Number of available devices, which are contained in
463 * device_allocation, is therefore decremented by not doing
464 * an up(device_allocation).
469 *device_data
= local_device_data
;
474 static void cryp_dma_setup_channel(struct cryp_device_data
*device_data
,
477 struct dma_slave_config mem2cryp
= {
478 .direction
= DMA_MEM_TO_DEV
,
479 .dst_addr
= device_data
->phybase
+ CRYP_DMA_TX_FIFO
,
480 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
,
483 struct dma_slave_config cryp2mem
= {
484 .direction
= DMA_DEV_TO_MEM
,
485 .src_addr
= device_data
->phybase
+ CRYP_DMA_RX_FIFO
,
486 .src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
,
490 dma_cap_zero(device_data
->dma
.mask
);
491 dma_cap_set(DMA_SLAVE
, device_data
->dma
.mask
);
493 device_data
->dma
.cfg_mem2cryp
= mem_to_engine
;
494 device_data
->dma
.chan_mem2cryp
=
495 dma_request_channel(device_data
->dma
.mask
,
497 device_data
->dma
.cfg_mem2cryp
);
499 device_data
->dma
.cfg_cryp2mem
= engine_to_mem
;
500 device_data
->dma
.chan_cryp2mem
=
501 dma_request_channel(device_data
->dma
.mask
,
503 device_data
->dma
.cfg_cryp2mem
);
505 dmaengine_slave_config(device_data
->dma
.chan_mem2cryp
, &mem2cryp
);
506 dmaengine_slave_config(device_data
->dma
.chan_cryp2mem
, &cryp2mem
);
508 init_completion(&device_data
->dma
.cryp_dma_complete
);
511 static void cryp_dma_out_callback(void *data
)
513 struct cryp_ctx
*ctx
= (struct cryp_ctx
*) data
;
514 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
516 complete(&ctx
->device
->dma
.cryp_dma_complete
);
519 static int cryp_set_dma_transfer(struct cryp_ctx
*ctx
,
520 struct scatterlist
*sg
,
522 enum dma_data_direction direction
)
524 struct dma_async_tx_descriptor
*desc
;
525 struct dma_chan
*channel
= NULL
;
528 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
530 if (unlikely(!IS_ALIGNED((u32
)sg
, 4))) {
531 dev_err(ctx
->device
->dev
, "[%s]: Data in sg list isn't "
532 "aligned! Addr: 0x%08x", __func__
, (u32
)sg
);
538 channel
= ctx
->device
->dma
.chan_mem2cryp
;
539 ctx
->device
->dma
.sg_src
= sg
;
540 ctx
->device
->dma
.sg_src_len
= dma_map_sg(channel
->device
->dev
,
541 ctx
->device
->dma
.sg_src
,
542 ctx
->device
->dma
.nents_src
,
545 if (!ctx
->device
->dma
.sg_src_len
) {
546 dev_dbg(ctx
->device
->dev
,
547 "[%s]: Could not map the sg list (TO_DEVICE)",
552 dev_dbg(ctx
->device
->dev
, "[%s]: Setting up DMA for buffer "
553 "(TO_DEVICE)", __func__
);
555 desc
= dmaengine_prep_slave_sg(channel
,
556 ctx
->device
->dma
.sg_src
,
557 ctx
->device
->dma
.sg_src_len
,
558 direction
, DMA_CTRL_ACK
);
561 case DMA_FROM_DEVICE
:
562 channel
= ctx
->device
->dma
.chan_cryp2mem
;
563 ctx
->device
->dma
.sg_dst
= sg
;
564 ctx
->device
->dma
.sg_dst_len
= dma_map_sg(channel
->device
->dev
,
565 ctx
->device
->dma
.sg_dst
,
566 ctx
->device
->dma
.nents_dst
,
569 if (!ctx
->device
->dma
.sg_dst_len
) {
570 dev_dbg(ctx
->device
->dev
,
571 "[%s]: Could not map the sg list (FROM_DEVICE)",
576 dev_dbg(ctx
->device
->dev
, "[%s]: Setting up DMA for buffer "
577 "(FROM_DEVICE)", __func__
);
579 desc
= dmaengine_prep_slave_sg(channel
,
580 ctx
->device
->dma
.sg_dst
,
581 ctx
->device
->dma
.sg_dst_len
,
586 desc
->callback
= cryp_dma_out_callback
;
587 desc
->callback_param
= ctx
;
591 dev_dbg(ctx
->device
->dev
, "[%s]: Invalid DMA direction",
596 cookie
= dmaengine_submit(desc
);
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((u32
)indata
, 4))) {
760 pr_debug(DEV_DBG_NAME
" [%s]: Data isn't aligned! Addr: "
761 "0x%08x", __func__
, (u32
)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 ablkcipher_request
*areq
)
826 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
827 struct cryp_ctx
*ctx
= crypto_ablkcipher_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
->nbytes
;
837 ctx
->outlen
= areq
->nbytes
;
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 ablkcipher_request
*areq
)
883 struct ablkcipher_walk walk
;
884 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
885 struct cryp_ctx
*ctx
= crypto_ablkcipher_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 ablkcipher_walk_init(&walk
, areq
->dst
, areq
->src
, areq
->nbytes
);
899 ret
= ablkcipher_walk_phys(areq
, &walk
);
902 pr_err(DEV_DBG_NAME
"[%s]: ablkcipher_walk_phys() failed!",
907 while ((nbytes
= walk
.nbytes
) > 0) {
909 src_paddr
= (page_to_phys(walk
.src
.page
) + walk
.src
.offset
);
910 ctx
->indata
= phys_to_virt(src_paddr
);
912 dst_paddr
= (page_to_phys(walk
.dst
.page
) + walk
.dst
.offset
);
913 ctx
->outdata
= phys_to_virt(dst_paddr
);
915 ctx
->datalen
= nbytes
- (nbytes
% ctx
->blocksize
);
917 ret
= hw_crypt_noxts(ctx
, device_data
);
921 nbytes
-= ctx
->datalen
;
922 ret
= ablkcipher_walk_done(areq
, &walk
, nbytes
);
926 ablkcipher_walk_complete(&walk
);
929 /* Release the device */
930 spin_lock(&device_data
->ctx_lock
);
931 device_data
->current_ctx
= NULL
;
933 spin_unlock(&device_data
->ctx_lock
);
936 * The down_interruptible part for this semaphore is called in
937 * cryp_get_device_data.
939 up(&driver_data
.device_allocation
);
944 static int aes_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
945 const u8
*key
, unsigned int keylen
)
947 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
948 u32
*flags
= &cipher
->base
.crt_flags
;
950 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
953 case AES_KEYSIZE_128
:
954 ctx
->config
.keysize
= CRYP_KEY_SIZE_128
;
957 case AES_KEYSIZE_192
:
958 ctx
->config
.keysize
= CRYP_KEY_SIZE_192
;
961 case AES_KEYSIZE_256
:
962 ctx
->config
.keysize
= CRYP_KEY_SIZE_256
;
966 pr_err(DEV_DBG_NAME
"[%s]: Unknown keylen!", __func__
);
967 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
971 memcpy(ctx
->key
, key
, keylen
);
972 ctx
->keylen
= keylen
;
979 static int des_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
980 const u8
*key
, unsigned int keylen
)
982 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
983 u32
*flags
= &cipher
->base
.crt_flags
;
984 u32 tmp
[DES_EXPKEY_WORDS
];
987 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
988 if (keylen
!= DES_KEY_SIZE
) {
989 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
990 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_RES_BAD_KEY_LEN",
995 ret
= des_ekey(tmp
, key
);
996 if (unlikely(ret
== 0) && (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
997 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
998 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_REQ_WEAK_KEY",
1003 memcpy(ctx
->key
, key
, keylen
);
1004 ctx
->keylen
= keylen
;
1010 static int des3_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
1011 const u8
*key
, unsigned int keylen
)
1013 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1014 u32
*flags
= &cipher
->base
.crt_flags
;
1015 const u32
*K
= (const u32
*)key
;
1016 u32 tmp
[DES3_EDE_EXPKEY_WORDS
];
1019 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1020 if (keylen
!= DES3_EDE_KEY_SIZE
) {
1021 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
1022 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_RES_BAD_KEY_LEN",
1027 /* Checking key interdependency for weak key detection. */
1028 if (unlikely(!((K
[0] ^ K
[2]) | (K
[1] ^ K
[3])) ||
1029 !((K
[2] ^ K
[4]) | (K
[3] ^ K
[5]))) &&
1030 (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
1031 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
1032 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_REQ_WEAK_KEY",
1036 for (i
= 0; i
< 3; i
++) {
1037 ret
= des_ekey(tmp
, key
+ i
*DES_KEY_SIZE
);
1038 if (unlikely(ret
== 0) && (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
1039 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
1040 pr_debug(DEV_DBG_NAME
" [%s]: "
1041 "CRYPTO_TFM_REQ_WEAK_KEY", __func__
);
1046 memcpy(ctx
->key
, key
, keylen
);
1047 ctx
->keylen
= keylen
;
1053 static int cryp_blk_encrypt(struct ablkcipher_request
*areq
)
1055 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1056 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1058 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1060 ctx
->config
.algodir
= CRYP_ALGORITHM_ENCRYPT
;
1063 * DMA does not work for DES due to a hw bug */
1064 if (cryp_mode
== CRYP_MODE_DMA
&& mode_is_aes(ctx
->config
.algomode
))
1065 return ablk_dma_crypt(areq
);
1067 /* For everything except DMA, we run the non DMA version. */
1068 return ablk_crypt(areq
);
1071 static int cryp_blk_decrypt(struct ablkcipher_request
*areq
)
1073 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1074 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1076 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1078 ctx
->config
.algodir
= CRYP_ALGORITHM_DECRYPT
;
1080 /* DMA does not work for DES due to a hw bug */
1081 if (cryp_mode
== CRYP_MODE_DMA
&& mode_is_aes(ctx
->config
.algomode
))
1082 return ablk_dma_crypt(areq
);
1084 /* For everything except DMA, we run the non DMA version. */
1085 return ablk_crypt(areq
);
1088 struct cryp_algo_template
{
1089 enum cryp_algo_mode algomode
;
1090 struct crypto_alg crypto
;
1093 static int cryp_cra_init(struct crypto_tfm
*tfm
)
1095 struct cryp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1096 struct crypto_alg
*alg
= tfm
->__crt_alg
;
1097 struct cryp_algo_template
*cryp_alg
= container_of(alg
,
1098 struct cryp_algo_template
,
1101 ctx
->config
.algomode
= cryp_alg
->algomode
;
1102 ctx
->blocksize
= crypto_tfm_alg_blocksize(tfm
);
1107 static struct cryp_algo_template cryp_algs
[] = {
1109 .algomode
= CRYP_ALGO_AES_ECB
,
1112 .cra_driver_name
= "aes-ux500",
1113 .cra_priority
= 300,
1114 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1116 .cra_blocksize
= AES_BLOCK_SIZE
,
1117 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1119 .cra_type
= &crypto_ablkcipher_type
,
1120 .cra_init
= cryp_cra_init
,
1121 .cra_module
= THIS_MODULE
,
1124 .min_keysize
= AES_MIN_KEY_SIZE
,
1125 .max_keysize
= AES_MAX_KEY_SIZE
,
1126 .setkey
= aes_ablkcipher_setkey
,
1127 .encrypt
= cryp_blk_encrypt
,
1128 .decrypt
= cryp_blk_decrypt
1134 .algomode
= CRYP_ALGO_AES_ECB
,
1136 .cra_name
= "ecb(aes)",
1137 .cra_driver_name
= "ecb-aes-ux500",
1138 .cra_priority
= 300,
1139 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1141 .cra_blocksize
= AES_BLOCK_SIZE
,
1142 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1144 .cra_type
= &crypto_ablkcipher_type
,
1145 .cra_init
= cryp_cra_init
,
1146 .cra_module
= THIS_MODULE
,
1149 .min_keysize
= AES_MIN_KEY_SIZE
,
1150 .max_keysize
= AES_MAX_KEY_SIZE
,
1151 .setkey
= aes_ablkcipher_setkey
,
1152 .encrypt
= cryp_blk_encrypt
,
1153 .decrypt
= cryp_blk_decrypt
,
1159 .algomode
= CRYP_ALGO_AES_CBC
,
1161 .cra_name
= "cbc(aes)",
1162 .cra_driver_name
= "cbc-aes-ux500",
1163 .cra_priority
= 300,
1164 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1166 .cra_blocksize
= AES_BLOCK_SIZE
,
1167 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1169 .cra_type
= &crypto_ablkcipher_type
,
1170 .cra_init
= cryp_cra_init
,
1171 .cra_module
= THIS_MODULE
,
1174 .min_keysize
= AES_MIN_KEY_SIZE
,
1175 .max_keysize
= AES_MAX_KEY_SIZE
,
1176 .setkey
= aes_ablkcipher_setkey
,
1177 .encrypt
= cryp_blk_encrypt
,
1178 .decrypt
= cryp_blk_decrypt
,
1179 .ivsize
= AES_BLOCK_SIZE
,
1185 .algomode
= CRYP_ALGO_AES_CTR
,
1187 .cra_name
= "ctr(aes)",
1188 .cra_driver_name
= "ctr-aes-ux500",
1189 .cra_priority
= 300,
1190 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1192 .cra_blocksize
= AES_BLOCK_SIZE
,
1193 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1195 .cra_type
= &crypto_ablkcipher_type
,
1196 .cra_init
= cryp_cra_init
,
1197 .cra_module
= THIS_MODULE
,
1200 .min_keysize
= AES_MIN_KEY_SIZE
,
1201 .max_keysize
= AES_MAX_KEY_SIZE
,
1202 .setkey
= aes_ablkcipher_setkey
,
1203 .encrypt
= cryp_blk_encrypt
,
1204 .decrypt
= cryp_blk_decrypt
,
1205 .ivsize
= AES_BLOCK_SIZE
,
1211 .algomode
= CRYP_ALGO_DES_ECB
,
1214 .cra_driver_name
= "des-ux500",
1215 .cra_priority
= 300,
1216 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1218 .cra_blocksize
= DES_BLOCK_SIZE
,
1219 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1221 .cra_type
= &crypto_ablkcipher_type
,
1222 .cra_init
= cryp_cra_init
,
1223 .cra_module
= THIS_MODULE
,
1226 .min_keysize
= DES_KEY_SIZE
,
1227 .max_keysize
= DES_KEY_SIZE
,
1228 .setkey
= des_ablkcipher_setkey
,
1229 .encrypt
= cryp_blk_encrypt
,
1230 .decrypt
= cryp_blk_decrypt
1237 .algomode
= CRYP_ALGO_TDES_ECB
,
1239 .cra_name
= "des3_ede",
1240 .cra_driver_name
= "des3_ede-ux500",
1241 .cra_priority
= 300,
1242 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1244 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1245 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1247 .cra_type
= &crypto_ablkcipher_type
,
1248 .cra_init
= cryp_cra_init
,
1249 .cra_module
= THIS_MODULE
,
1252 .min_keysize
= DES3_EDE_KEY_SIZE
,
1253 .max_keysize
= DES3_EDE_KEY_SIZE
,
1254 .setkey
= des_ablkcipher_setkey
,
1255 .encrypt
= cryp_blk_encrypt
,
1256 .decrypt
= cryp_blk_decrypt
1262 .algomode
= CRYP_ALGO_DES_ECB
,
1264 .cra_name
= "ecb(des)",
1265 .cra_driver_name
= "ecb-des-ux500",
1266 .cra_priority
= 300,
1267 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1269 .cra_blocksize
= DES_BLOCK_SIZE
,
1270 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1272 .cra_type
= &crypto_ablkcipher_type
,
1273 .cra_init
= cryp_cra_init
,
1274 .cra_module
= THIS_MODULE
,
1277 .min_keysize
= DES_KEY_SIZE
,
1278 .max_keysize
= DES_KEY_SIZE
,
1279 .setkey
= des_ablkcipher_setkey
,
1280 .encrypt
= cryp_blk_encrypt
,
1281 .decrypt
= cryp_blk_decrypt
,
1287 .algomode
= CRYP_ALGO_TDES_ECB
,
1289 .cra_name
= "ecb(des3_ede)",
1290 .cra_driver_name
= "ecb-des3_ede-ux500",
1291 .cra_priority
= 300,
1292 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1294 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1295 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1297 .cra_type
= &crypto_ablkcipher_type
,
1298 .cra_init
= cryp_cra_init
,
1299 .cra_module
= THIS_MODULE
,
1302 .min_keysize
= DES3_EDE_KEY_SIZE
,
1303 .max_keysize
= DES3_EDE_KEY_SIZE
,
1304 .setkey
= des3_ablkcipher_setkey
,
1305 .encrypt
= cryp_blk_encrypt
,
1306 .decrypt
= cryp_blk_decrypt
,
1312 .algomode
= CRYP_ALGO_DES_CBC
,
1314 .cra_name
= "cbc(des)",
1315 .cra_driver_name
= "cbc-des-ux500",
1316 .cra_priority
= 300,
1317 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1319 .cra_blocksize
= DES_BLOCK_SIZE
,
1320 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1322 .cra_type
= &crypto_ablkcipher_type
,
1323 .cra_init
= cryp_cra_init
,
1324 .cra_module
= THIS_MODULE
,
1327 .min_keysize
= DES_KEY_SIZE
,
1328 .max_keysize
= DES_KEY_SIZE
,
1329 .setkey
= des_ablkcipher_setkey
,
1330 .encrypt
= cryp_blk_encrypt
,
1331 .decrypt
= cryp_blk_decrypt
,
1337 .algomode
= CRYP_ALGO_TDES_CBC
,
1339 .cra_name
= "cbc(des3_ede)",
1340 .cra_driver_name
= "cbc-des3_ede-ux500",
1341 .cra_priority
= 300,
1342 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1344 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1345 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1347 .cra_type
= &crypto_ablkcipher_type
,
1348 .cra_init
= cryp_cra_init
,
1349 .cra_module
= THIS_MODULE
,
1352 .min_keysize
= DES3_EDE_KEY_SIZE
,
1353 .max_keysize
= DES3_EDE_KEY_SIZE
,
1354 .setkey
= des3_ablkcipher_setkey
,
1355 .encrypt
= cryp_blk_encrypt
,
1356 .decrypt
= cryp_blk_decrypt
,
1357 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1365 * cryp_algs_register_all -
1367 static int cryp_algs_register_all(void)
1373 pr_debug("[%s]", __func__
);
1375 for (i
= 0; i
< ARRAY_SIZE(cryp_algs
); i
++) {
1376 ret
= crypto_register_alg(&cryp_algs
[i
].crypto
);
1379 pr_err("[%s] alg registration failed",
1380 cryp_algs
[i
].crypto
.cra_driver_name
);
1386 for (i
= 0; i
< count
; i
++)
1387 crypto_unregister_alg(&cryp_algs
[i
].crypto
);
1392 * cryp_algs_unregister_all -
1394 static void cryp_algs_unregister_all(void)
1398 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1400 for (i
= 0; i
< ARRAY_SIZE(cryp_algs
); i
++)
1401 crypto_unregister_alg(&cryp_algs
[i
].crypto
);
1404 static int ux500_cryp_probe(struct platform_device
*pdev
)
1408 struct resource
*res
= NULL
;
1409 struct resource
*res_irq
= NULL
;
1410 struct cryp_device_data
*device_data
;
1411 struct cryp_protection_config prot
= {
1412 .privilege_access
= CRYP_STATE_ENABLE
1414 struct device
*dev
= &pdev
->dev
;
1416 dev_dbg(dev
, "[%s]", __func__
);
1417 device_data
= devm_kzalloc(dev
, sizeof(*device_data
), GFP_ATOMIC
);
1419 dev_err(dev
, "[%s]: kzalloc() failed!", __func__
);
1424 device_data
->dev
= dev
;
1425 device_data
->current_ctx
= NULL
;
1427 /* Grab the DMA configuration from platform data. */
1428 mem_to_engine
= &((struct cryp_platform_data
*)
1429 dev
->platform_data
)->mem_to_engine
;
1430 engine_to_mem
= &((struct cryp_platform_data
*)
1431 dev
->platform_data
)->engine_to_mem
;
1433 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1435 dev_err(dev
, "[%s]: platform_get_resource() failed",
1441 device_data
->phybase
= res
->start
;
1442 device_data
->base
= devm_ioremap_resource(dev
, res
);
1443 if (IS_ERR(device_data
->base
)) {
1444 dev_err(dev
, "[%s]: ioremap failed!", __func__
);
1445 ret
= PTR_ERR(device_data
->base
);
1449 spin_lock_init(&device_data
->ctx_lock
);
1450 spin_lock_init(&device_data
->power_state_spinlock
);
1452 /* Enable power for CRYP hardware block */
1453 device_data
->pwr_regulator
= regulator_get(&pdev
->dev
, "v-ape");
1454 if (IS_ERR(device_data
->pwr_regulator
)) {
1455 dev_err(dev
, "[%s]: could not get cryp regulator", __func__
);
1456 ret
= PTR_ERR(device_data
->pwr_regulator
);
1457 device_data
->pwr_regulator
= NULL
;
1461 /* Enable the clk for CRYP hardware block */
1462 device_data
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1463 if (IS_ERR(device_data
->clk
)) {
1464 dev_err(dev
, "[%s]: clk_get() failed!", __func__
);
1465 ret
= PTR_ERR(device_data
->clk
);
1469 ret
= clk_prepare(device_data
->clk
);
1471 dev_err(dev
, "[%s]: clk_prepare() failed!", __func__
);
1475 /* Enable device power (and clock) */
1476 ret
= cryp_enable_power(device_data
->dev
, device_data
, false);
1478 dev_err(dev
, "[%s]: cryp_enable_power() failed!", __func__
);
1479 goto out_clk_unprepare
;
1482 cryp_error
= cryp_check(device_data
);
1483 if (cryp_error
!= 0) {
1484 dev_err(dev
, "[%s]: cryp_init() failed!", __func__
);
1489 cryp_error
= cryp_configure_protection(device_data
, &prot
);
1490 if (cryp_error
!= 0) {
1491 dev_err(dev
, "[%s]: cryp_configure_protection() failed!",
1497 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1499 dev_err(dev
, "[%s]: IORESOURCE_IRQ unavailable",
1505 ret
= devm_request_irq(&pdev
->dev
, res_irq
->start
,
1506 cryp_interrupt_handler
, 0, "cryp1", device_data
);
1508 dev_err(dev
, "[%s]: Unable to request IRQ", __func__
);
1512 if (cryp_mode
== CRYP_MODE_DMA
)
1513 cryp_dma_setup_channel(device_data
, dev
);
1515 platform_set_drvdata(pdev
, device_data
);
1517 /* Put the new device into the device list... */
1518 klist_add_tail(&device_data
->list_node
, &driver_data
.device_list
);
1520 /* ... and signal that a new device is available. */
1521 up(&driver_data
.device_allocation
);
1523 atomic_set(&session_id
, 1);
1525 ret
= cryp_algs_register_all();
1527 dev_err(dev
, "[%s]: cryp_algs_register_all() failed!",
1532 dev_info(dev
, "successfully registered\n");
1537 cryp_disable_power(device_data
->dev
, device_data
, false);
1540 clk_unprepare(device_data
->clk
);
1543 regulator_put(device_data
->pwr_regulator
);
1549 static int ux500_cryp_remove(struct platform_device
*pdev
)
1551 struct cryp_device_data
*device_data
;
1553 dev_dbg(&pdev
->dev
, "[%s]", __func__
);
1554 device_data
= platform_get_drvdata(pdev
);
1556 dev_err(&pdev
->dev
, "[%s]: platform_get_drvdata() failed!",
1561 /* Try to decrease the number of available devices. */
1562 if (down_trylock(&driver_data
.device_allocation
))
1565 /* Check that the device is free */
1566 spin_lock(&device_data
->ctx_lock
);
1567 /* current_ctx allocates a device, NULL = unallocated */
1568 if (device_data
->current_ctx
) {
1569 /* The device is busy */
1570 spin_unlock(&device_data
->ctx_lock
);
1571 /* Return the device to the pool. */
1572 up(&driver_data
.device_allocation
);
1576 spin_unlock(&device_data
->ctx_lock
);
1578 /* Remove the device from the list */
1579 if (klist_node_attached(&device_data
->list_node
))
1580 klist_remove(&device_data
->list_node
);
1582 /* If this was the last device, remove the services */
1583 if (list_empty(&driver_data
.device_list
.k_list
))
1584 cryp_algs_unregister_all();
1586 if (cryp_disable_power(&pdev
->dev
, device_data
, false))
1587 dev_err(&pdev
->dev
, "[%s]: cryp_disable_power() failed",
1590 clk_unprepare(device_data
->clk
);
1591 regulator_put(device_data
->pwr_regulator
);
1596 static void ux500_cryp_shutdown(struct platform_device
*pdev
)
1598 struct cryp_device_data
*device_data
;
1600 dev_dbg(&pdev
->dev
, "[%s]", __func__
);
1602 device_data
= platform_get_drvdata(pdev
);
1604 dev_err(&pdev
->dev
, "[%s]: platform_get_drvdata() failed!",
1609 /* Check that the device is free */
1610 spin_lock(&device_data
->ctx_lock
);
1611 /* current_ctx allocates a device, NULL = unallocated */
1612 if (!device_data
->current_ctx
) {
1613 if (down_trylock(&driver_data
.device_allocation
))
1614 dev_dbg(&pdev
->dev
, "[%s]: Cryp still in use!"
1615 "Shutting down anyway...", __func__
);
1617 * (Allocate the device)
1618 * Need to set this to non-null (dummy) value,
1619 * to avoid usage if context switching.
1621 device_data
->current_ctx
++;
1623 spin_unlock(&device_data
->ctx_lock
);
1625 /* Remove the device from the list */
1626 if (klist_node_attached(&device_data
->list_node
))
1627 klist_remove(&device_data
->list_node
);
1629 /* If this was the last device, remove the services */
1630 if (list_empty(&driver_data
.device_list
.k_list
))
1631 cryp_algs_unregister_all();
1633 if (cryp_disable_power(&pdev
->dev
, device_data
, false))
1634 dev_err(&pdev
->dev
, "[%s]: cryp_disable_power() failed",
1639 #ifdef CONFIG_PM_SLEEP
1640 static int ux500_cryp_suspend(struct device
*dev
)
1643 struct platform_device
*pdev
= to_platform_device(dev
);
1644 struct cryp_device_data
*device_data
;
1645 struct resource
*res_irq
;
1646 struct cryp_ctx
*temp_ctx
= NULL
;
1648 dev_dbg(dev
, "[%s]", __func__
);
1651 device_data
= platform_get_drvdata(pdev
);
1653 dev_err(dev
, "[%s]: platform_get_drvdata() failed!", __func__
);
1657 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1659 dev_err(dev
, "[%s]: IORESOURCE_IRQ, unavailable", __func__
);
1661 disable_irq(res_irq
->start
);
1663 spin_lock(&device_data
->ctx_lock
);
1664 if (!device_data
->current_ctx
)
1665 device_data
->current_ctx
++;
1666 spin_unlock(&device_data
->ctx_lock
);
1668 if (device_data
->current_ctx
== ++temp_ctx
) {
1669 if (down_interruptible(&driver_data
.device_allocation
))
1670 dev_dbg(dev
, "[%s]: down_interruptible() failed",
1672 ret
= cryp_disable_power(dev
, device_data
, false);
1675 ret
= cryp_disable_power(dev
, device_data
, true);
1678 dev_err(dev
, "[%s]: cryp_disable_power()", __func__
);
1683 static int ux500_cryp_resume(struct device
*dev
)
1686 struct platform_device
*pdev
= to_platform_device(dev
);
1687 struct cryp_device_data
*device_data
;
1688 struct resource
*res_irq
;
1689 struct cryp_ctx
*temp_ctx
= NULL
;
1691 dev_dbg(dev
, "[%s]", __func__
);
1693 device_data
= platform_get_drvdata(pdev
);
1695 dev_err(dev
, "[%s]: platform_get_drvdata() failed!", __func__
);
1699 spin_lock(&device_data
->ctx_lock
);
1700 if (device_data
->current_ctx
== ++temp_ctx
)
1701 device_data
->current_ctx
= NULL
;
1702 spin_unlock(&device_data
->ctx_lock
);
1705 if (!device_data
->current_ctx
)
1706 up(&driver_data
.device_allocation
);
1708 ret
= cryp_enable_power(dev
, device_data
, true);
1711 dev_err(dev
, "[%s]: cryp_enable_power() failed!", __func__
);
1713 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1715 enable_irq(res_irq
->start
);
1722 static SIMPLE_DEV_PM_OPS(ux500_cryp_pm
, ux500_cryp_suspend
, ux500_cryp_resume
);
1724 static const struct of_device_id ux500_cryp_match
[] = {
1725 { .compatible
= "stericsson,ux500-cryp" },
1728 MODULE_DEVICE_TABLE(of
, ux500_cryp_match
);
1730 static struct platform_driver cryp_driver
= {
1731 .probe
= ux500_cryp_probe
,
1732 .remove
= ux500_cryp_remove
,
1733 .shutdown
= ux500_cryp_shutdown
,
1736 .of_match_table
= ux500_cryp_match
,
1737 .pm
= &ux500_cryp_pm
,
1741 static int __init
ux500_cryp_mod_init(void)
1743 pr_debug("[%s] is called!", __func__
);
1744 klist_init(&driver_data
.device_list
, NULL
, NULL
);
1745 /* Initialize the semaphore to 0 devices (locked state) */
1746 sema_init(&driver_data
.device_allocation
, 0);
1747 return platform_driver_register(&cryp_driver
);
1750 static void __exit
ux500_cryp_mod_fini(void)
1752 pr_debug("[%s] is called!", __func__
);
1753 platform_driver_unregister(&cryp_driver
);
1756 module_init(ux500_cryp_mod_init
);
1757 module_exit(ux500_cryp_mod_fini
);
1759 module_param(cryp_mode
, int, 0);
1761 MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 CRYP crypto engine.");
1762 MODULE_ALIAS_CRYPTO("aes-all");
1763 MODULE_ALIAS_CRYPTO("des-all");
1765 MODULE_LICENSE("GPL");