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/mod_devicetable.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/semaphore.h>
27 #include <linux/platform_data/dma-ste-dma40.h>
29 #include <crypto/aes.h>
30 #include <crypto/algapi.h>
31 #include <crypto/ctr.h>
32 #include <crypto/des.h>
33 #include <crypto/scatterwalk.h>
35 #include <linux/platform_data/crypto-ux500.h>
40 #define CRYP_MAX_KEY_SIZE 32
41 #define BYTES_PER_WORD 4
44 static atomic_t session_id
;
46 static struct stedma40_chan_cfg
*mem_to_engine
;
47 static struct stedma40_chan_cfg
*engine_to_mem
;
50 * struct cryp_driver_data - data specific to the driver.
52 * @device_list: A list of registered devices to choose from.
53 * @device_allocation: A semaphore initialized with number of devices.
55 struct cryp_driver_data
{
56 struct klist device_list
;
57 struct semaphore device_allocation
;
61 * struct cryp_ctx - Crypto context
62 * @config: Crypto mode.
63 * @key[CRYP_MAX_KEY_SIZE]: Key.
64 * @keylen: Length of key.
65 * @iv: Pointer to initialization vector.
66 * @indata: Pointer to indata.
67 * @outdata: Pointer to outdata.
68 * @datalen: Length of indata.
69 * @outlen: Length of outdata.
70 * @blocksize: Size of blocks.
71 * @updated: Updated flag.
72 * @dev_ctx: Device dependent context.
73 * @device: Pointer to the device.
76 struct cryp_config config
;
77 u8 key
[CRYP_MAX_KEY_SIZE
];
86 struct cryp_device_context dev_ctx
;
87 struct cryp_device_data
*device
;
91 static struct cryp_driver_data driver_data
;
94 * uint8p_to_uint32_be - 4*uint8 to uint32 big endian
95 * @in: Data to convert.
97 static inline u32
uint8p_to_uint32_be(u8
*in
)
99 u32
*data
= (u32
*)in
;
101 return cpu_to_be32p(data
);
105 * swap_bits_in_byte - mirror the bits in a byte
106 * @b: the byte to be mirrored
108 * The bits are swapped the following way:
109 * Byte b include bits 0-7, nibble 1 (n1) include bits 0-3 and
110 * nibble 2 (n2) bits 4-7.
113 * (The "old" (moved) bit is replaced with a zero)
114 * 1. Move bit 6 and 7, 4 positions to the left.
115 * 2. Move bit 3 and 5, 2 positions to the left.
116 * 3. Move bit 1-4, 1 position to the left.
119 * 1. Move bit 0 and 1, 4 positions to the right.
120 * 2. Move bit 2 and 4, 2 positions to the right.
121 * 3. Move bit 3-6, 1 position to the right.
123 * Combine the two nibbles to a complete and swapped byte.
126 static inline u8
swap_bits_in_byte(u8 b
)
128 #define R_SHIFT_4_MASK 0xc0 /* Bits 6 and 7, right shift 4 */
129 #define R_SHIFT_2_MASK 0x28 /* (After right shift 4) Bits 3 and 5,
131 #define R_SHIFT_1_MASK 0x1e /* (After right shift 2) Bits 1-4,
133 #define L_SHIFT_4_MASK 0x03 /* Bits 0 and 1, left shift 4 */
134 #define L_SHIFT_2_MASK 0x14 /* (After left shift 4) Bits 2 and 4,
136 #define L_SHIFT_1_MASK 0x78 /* (After left shift 1) Bits 3-6,
142 /* Swap most significant nibble */
143 /* Right shift 4, bits 6 and 7 */
144 n1
= ((b
& R_SHIFT_4_MASK
) >> 4) | (b
& ~(R_SHIFT_4_MASK
>> 4));
145 /* Right shift 2, bits 3 and 5 */
146 n1
= ((n1
& R_SHIFT_2_MASK
) >> 2) | (n1
& ~(R_SHIFT_2_MASK
>> 2));
147 /* Right shift 1, bits 1-4 */
148 n1
= (n1
& R_SHIFT_1_MASK
) >> 1;
150 /* Swap least significant nibble */
151 /* Left shift 4, bits 0 and 1 */
152 n2
= ((b
& L_SHIFT_4_MASK
) << 4) | (b
& ~(L_SHIFT_4_MASK
<< 4));
153 /* Left shift 2, bits 2 and 4 */
154 n2
= ((n2
& L_SHIFT_2_MASK
) << 2) | (n2
& ~(L_SHIFT_2_MASK
<< 2));
155 /* Left shift 1, bits 3-6 */
156 n2
= (n2
& L_SHIFT_1_MASK
) << 1;
161 static inline void swap_words_in_key_and_bits_in_byte(const u8
*in
,
168 j
= len
- BYTES_PER_WORD
;
170 for (i
= 0; i
< BYTES_PER_WORD
; i
++) {
171 index
= len
- j
- BYTES_PER_WORD
+ i
;
173 swap_bits_in_byte(in
[index
]);
179 static void add_session_id(struct cryp_ctx
*ctx
)
182 * We never want 0 to be a valid value, since this is the default value
183 * for the software context.
185 if (unlikely(atomic_inc_and_test(&session_id
)))
186 atomic_inc(&session_id
);
188 ctx
->session_id
= atomic_read(&session_id
);
191 static irqreturn_t
cryp_interrupt_handler(int irq
, void *param
)
193 struct cryp_ctx
*ctx
;
195 struct cryp_device_data
*device_data
;
202 /* The device is coming from the one found in hw_crypt_noxts. */
203 device_data
= (struct cryp_device_data
*)param
;
205 ctx
= device_data
->current_ctx
;
212 dev_dbg(ctx
->device
->dev
, "[%s] (len: %d) %s, ", __func__
, ctx
->outlen
,
213 cryp_pending_irq_src(device_data
, CRYP_IRQ_SRC_OUTPUT_FIFO
) ?
216 if (cryp_pending_irq_src(device_data
,
217 CRYP_IRQ_SRC_OUTPUT_FIFO
)) {
218 if (ctx
->outlen
/ ctx
->blocksize
> 0) {
219 count
= ctx
->blocksize
/ 4;
221 readsl(&device_data
->base
->dout
, ctx
->outdata
, count
);
222 ctx
->outdata
+= count
;
223 ctx
->outlen
-= count
;
225 if (ctx
->outlen
== 0) {
226 cryp_disable_irq_src(device_data
,
227 CRYP_IRQ_SRC_OUTPUT_FIFO
);
230 } else if (cryp_pending_irq_src(device_data
,
231 CRYP_IRQ_SRC_INPUT_FIFO
)) {
232 if (ctx
->datalen
/ ctx
->blocksize
> 0) {
233 count
= ctx
->blocksize
/ 4;
235 writesl(&device_data
->base
->din
, ctx
->indata
, count
);
237 ctx
->indata
+= count
;
238 ctx
->datalen
-= count
;
240 if (ctx
->datalen
== 0)
241 cryp_disable_irq_src(device_data
,
242 CRYP_IRQ_SRC_INPUT_FIFO
);
244 if (ctx
->config
.algomode
== CRYP_ALGO_AES_XTS
) {
245 CRYP_PUT_BITS(&device_data
->base
->cr
,
250 cryp_wait_until_done(device_data
);
258 static int mode_is_aes(enum cryp_algo_mode mode
)
260 return CRYP_ALGO_AES_ECB
== mode
||
261 CRYP_ALGO_AES_CBC
== mode
||
262 CRYP_ALGO_AES_CTR
== mode
||
263 CRYP_ALGO_AES_XTS
== mode
;
266 static int cfg_iv(struct cryp_device_data
*device_data
, u32 left
, u32 right
,
267 enum cryp_init_vector_index index
)
269 struct cryp_init_vector_value vector_value
;
271 dev_dbg(device_data
->dev
, "[%s]", __func__
);
273 vector_value
.init_value_left
= left
;
274 vector_value
.init_value_right
= right
;
276 return cryp_configure_init_vector(device_data
,
281 static int cfg_ivs(struct cryp_device_data
*device_data
, struct cryp_ctx
*ctx
)
285 int num_of_regs
= ctx
->blocksize
/ 8;
286 u32 iv
[AES_BLOCK_SIZE
/ 4];
288 dev_dbg(device_data
->dev
, "[%s]", __func__
);
291 * Since we loop on num_of_regs we need to have a check in case
292 * someone provides an incorrect blocksize which would force calling
293 * cfg_iv with i greater than 2 which is an error.
295 if (num_of_regs
> 2) {
296 dev_err(device_data
->dev
, "[%s] Incorrect blocksize %d",
297 __func__
, ctx
->blocksize
);
301 for (i
= 0; i
< ctx
->blocksize
/ 4; i
++)
302 iv
[i
] = uint8p_to_uint32_be(ctx
->iv
+ i
*4);
304 for (i
= 0; i
< num_of_regs
; i
++) {
305 status
= cfg_iv(device_data
, iv
[i
*2], iv
[i
*2+1],
306 (enum cryp_init_vector_index
) i
);
313 static int set_key(struct cryp_device_data
*device_data
,
316 enum cryp_key_reg_index index
)
318 struct cryp_key_value key_value
;
321 dev_dbg(device_data
->dev
, "[%s]", __func__
);
323 key_value
.key_value_left
= left_key
;
324 key_value
.key_value_right
= right_key
;
326 cryp_error
= cryp_configure_key_values(device_data
,
330 dev_err(device_data
->dev
, "[%s]: "
331 "cryp_configure_key_values() failed!", __func__
);
336 static int cfg_keys(struct cryp_ctx
*ctx
)
339 int num_of_regs
= ctx
->keylen
/ 8;
340 u32 swapped_key
[CRYP_MAX_KEY_SIZE
/ 4];
343 dev_dbg(ctx
->device
->dev
, "[%s]", __func__
);
345 if (mode_is_aes(ctx
->config
.algomode
)) {
346 swap_words_in_key_and_bits_in_byte((u8
*)ctx
->key
,
350 for (i
= 0; i
< ctx
->keylen
/ 4; i
++)
351 swapped_key
[i
] = uint8p_to_uint32_be(ctx
->key
+ i
*4);
354 for (i
= 0; i
< num_of_regs
; i
++) {
355 cryp_error
= set_key(ctx
->device
,
356 *(((u32
*)swapped_key
)+i
*2),
357 *(((u32
*)swapped_key
)+i
*2+1),
358 (enum cryp_key_reg_index
) i
);
360 if (cryp_error
!= 0) {
361 dev_err(ctx
->device
->dev
, "[%s]: set_key() failed!",
369 static int cryp_setup_context(struct cryp_ctx
*ctx
,
370 struct cryp_device_data
*device_data
)
372 u32 control_register
= CRYP_CR_DEFAULT
;
375 case CRYP_MODE_INTERRUPT
:
376 writel_relaxed(CRYP_IMSC_DEFAULT
, &device_data
->base
->imsc
);
380 writel_relaxed(CRYP_DMACR_DEFAULT
, &device_data
->base
->dmacr
);
387 if (ctx
->updated
== 0) {
388 cryp_flush_inoutfifo(device_data
);
389 if (cfg_keys(ctx
) != 0) {
390 dev_err(ctx
->device
->dev
, "[%s]: cfg_keys failed!",
396 CRYP_ALGO_AES_ECB
!= ctx
->config
.algomode
&&
397 CRYP_ALGO_DES_ECB
!= ctx
->config
.algomode
&&
398 CRYP_ALGO_TDES_ECB
!= ctx
->config
.algomode
) {
399 if (cfg_ivs(device_data
, ctx
) != 0)
403 cryp_set_configuration(device_data
, &ctx
->config
,
406 } else if (ctx
->updated
== 1 &&
407 ctx
->session_id
!= atomic_read(&session_id
)) {
408 cryp_flush_inoutfifo(device_data
);
409 cryp_restore_device_context(device_data
, &ctx
->dev_ctx
);
412 control_register
= ctx
->dev_ctx
.cr
;
414 control_register
= ctx
->dev_ctx
.cr
;
416 writel(control_register
|
417 (CRYP_CRYPEN_ENABLE
<< CRYP_CR_CRYPEN_POS
),
418 &device_data
->base
->cr
);
423 static int cryp_get_device_data(struct cryp_ctx
*ctx
,
424 struct cryp_device_data
**device_data
)
427 struct klist_iter device_iterator
;
428 struct klist_node
*device_node
;
429 struct cryp_device_data
*local_device_data
= NULL
;
430 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
432 /* Wait until a device is available */
433 ret
= down_interruptible(&driver_data
.device_allocation
);
435 return ret
; /* Interrupted */
437 /* Select a device */
438 klist_iter_init(&driver_data
.device_list
, &device_iterator
);
440 device_node
= klist_next(&device_iterator
);
441 while (device_node
) {
442 local_device_data
= container_of(device_node
,
443 struct cryp_device_data
, list_node
);
444 spin_lock(&local_device_data
->ctx_lock
);
445 /* current_ctx allocates a device, NULL = unallocated */
446 if (local_device_data
->current_ctx
) {
447 device_node
= klist_next(&device_iterator
);
449 local_device_data
->current_ctx
= ctx
;
450 ctx
->device
= local_device_data
;
451 spin_unlock(&local_device_data
->ctx_lock
);
454 spin_unlock(&local_device_data
->ctx_lock
);
456 klist_iter_exit(&device_iterator
);
460 * No free device found.
461 * Since we allocated a device with down_interruptible, this
462 * should not be able to happen.
463 * Number of available devices, which are contained in
464 * device_allocation, is therefore decremented by not doing
465 * an up(device_allocation).
470 *device_data
= local_device_data
;
475 static void cryp_dma_setup_channel(struct cryp_device_data
*device_data
,
478 struct dma_slave_config mem2cryp
= {
479 .direction
= DMA_MEM_TO_DEV
,
480 .dst_addr
= device_data
->phybase
+ CRYP_DMA_TX_FIFO
,
481 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
,
484 struct dma_slave_config cryp2mem
= {
485 .direction
= DMA_DEV_TO_MEM
,
486 .src_addr
= device_data
->phybase
+ CRYP_DMA_RX_FIFO
,
487 .src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
,
491 dma_cap_zero(device_data
->dma
.mask
);
492 dma_cap_set(DMA_SLAVE
, device_data
->dma
.mask
);
494 device_data
->dma
.cfg_mem2cryp
= mem_to_engine
;
495 device_data
->dma
.chan_mem2cryp
=
496 dma_request_channel(device_data
->dma
.mask
,
498 device_data
->dma
.cfg_mem2cryp
);
500 device_data
->dma
.cfg_cryp2mem
= engine_to_mem
;
501 device_data
->dma
.chan_cryp2mem
=
502 dma_request_channel(device_data
->dma
.mask
,
504 device_data
->dma
.cfg_cryp2mem
);
506 dmaengine_slave_config(device_data
->dma
.chan_mem2cryp
, &mem2cryp
);
507 dmaengine_slave_config(device_data
->dma
.chan_cryp2mem
, &cryp2mem
);
509 init_completion(&device_data
->dma
.cryp_dma_complete
);
512 static void cryp_dma_out_callback(void *data
)
514 struct cryp_ctx
*ctx
= (struct cryp_ctx
*) data
;
515 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
517 complete(&ctx
->device
->dma
.cryp_dma_complete
);
520 static int cryp_set_dma_transfer(struct cryp_ctx
*ctx
,
521 struct scatterlist
*sg
,
523 enum dma_data_direction direction
)
525 struct dma_async_tx_descriptor
*desc
;
526 struct dma_chan
*channel
= NULL
;
529 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
531 if (unlikely(!IS_ALIGNED((u32
)sg
, 4))) {
532 dev_err(ctx
->device
->dev
, "[%s]: Data in sg list isn't "
533 "aligned! Addr: 0x%08x", __func__
, (u32
)sg
);
539 channel
= ctx
->device
->dma
.chan_mem2cryp
;
540 ctx
->device
->dma
.sg_src
= sg
;
541 ctx
->device
->dma
.sg_src_len
= dma_map_sg(channel
->device
->dev
,
542 ctx
->device
->dma
.sg_src
,
543 ctx
->device
->dma
.nents_src
,
546 if (!ctx
->device
->dma
.sg_src_len
) {
547 dev_dbg(ctx
->device
->dev
,
548 "[%s]: Could not map the sg list (TO_DEVICE)",
553 dev_dbg(ctx
->device
->dev
, "[%s]: Setting up DMA for buffer "
554 "(TO_DEVICE)", __func__
);
556 desc
= dmaengine_prep_slave_sg(channel
,
557 ctx
->device
->dma
.sg_src
,
558 ctx
->device
->dma
.sg_src_len
,
559 DMA_MEM_TO_DEV
, DMA_CTRL_ACK
);
562 case DMA_FROM_DEVICE
:
563 channel
= ctx
->device
->dma
.chan_cryp2mem
;
564 ctx
->device
->dma
.sg_dst
= sg
;
565 ctx
->device
->dma
.sg_dst_len
= dma_map_sg(channel
->device
->dev
,
566 ctx
->device
->dma
.sg_dst
,
567 ctx
->device
->dma
.nents_dst
,
570 if (!ctx
->device
->dma
.sg_dst_len
) {
571 dev_dbg(ctx
->device
->dev
,
572 "[%s]: Could not map the sg list (FROM_DEVICE)",
577 dev_dbg(ctx
->device
->dev
, "[%s]: Setting up DMA for buffer "
578 "(FROM_DEVICE)", __func__
);
580 desc
= dmaengine_prep_slave_sg(channel
,
581 ctx
->device
->dma
.sg_dst
,
582 ctx
->device
->dma
.sg_dst_len
,
587 desc
->callback
= cryp_dma_out_callback
;
588 desc
->callback_param
= ctx
;
592 dev_dbg(ctx
->device
->dev
, "[%s]: Invalid DMA direction",
597 cookie
= dmaengine_submit(desc
);
598 if (dma_submit_error(cookie
)) {
599 dev_dbg(ctx
->device
->dev
, "[%s]: DMA submission failed\n",
604 dma_async_issue_pending(channel
);
609 static void cryp_dma_done(struct cryp_ctx
*ctx
)
611 struct dma_chan
*chan
;
613 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
615 chan
= ctx
->device
->dma
.chan_mem2cryp
;
616 dmaengine_terminate_all(chan
);
617 dma_unmap_sg(chan
->device
->dev
, ctx
->device
->dma
.sg_src
,
618 ctx
->device
->dma
.sg_src_len
, DMA_TO_DEVICE
);
620 chan
= ctx
->device
->dma
.chan_cryp2mem
;
621 dmaengine_terminate_all(chan
);
622 dma_unmap_sg(chan
->device
->dev
, ctx
->device
->dma
.sg_dst
,
623 ctx
->device
->dma
.sg_dst_len
, DMA_FROM_DEVICE
);
626 static int cryp_dma_write(struct cryp_ctx
*ctx
, struct scatterlist
*sg
,
629 int error
= cryp_set_dma_transfer(ctx
, sg
, len
, DMA_TO_DEVICE
);
630 dev_dbg(ctx
->device
->dev
, "[%s]: ", __func__
);
633 dev_dbg(ctx
->device
->dev
, "[%s]: cryp_set_dma_transfer() "
641 static int cryp_dma_read(struct cryp_ctx
*ctx
, struct scatterlist
*sg
, int len
)
643 int error
= cryp_set_dma_transfer(ctx
, sg
, len
, DMA_FROM_DEVICE
);
645 dev_dbg(ctx
->device
->dev
, "[%s]: cryp_set_dma_transfer() "
653 static void cryp_polling_mode(struct cryp_ctx
*ctx
,
654 struct cryp_device_data
*device_data
)
656 int len
= ctx
->blocksize
/ BYTES_PER_WORD
;
657 int remaining_length
= ctx
->datalen
;
658 u32
*indata
= (u32
*)ctx
->indata
;
659 u32
*outdata
= (u32
*)ctx
->outdata
;
661 while (remaining_length
> 0) {
662 writesl(&device_data
->base
->din
, indata
, len
);
664 remaining_length
-= (len
* BYTES_PER_WORD
);
665 cryp_wait_until_done(device_data
);
667 readsl(&device_data
->base
->dout
, outdata
, len
);
669 cryp_wait_until_done(device_data
);
673 static int cryp_disable_power(struct device
*dev
,
674 struct cryp_device_data
*device_data
,
675 bool save_device_context
)
679 dev_dbg(dev
, "[%s]", __func__
);
681 spin_lock(&device_data
->power_state_spinlock
);
682 if (!device_data
->power_state
)
685 spin_lock(&device_data
->ctx_lock
);
686 if (save_device_context
&& device_data
->current_ctx
) {
687 cryp_save_device_context(device_data
,
688 &device_data
->current_ctx
->dev_ctx
,
690 device_data
->restore_dev_ctx
= true;
692 spin_unlock(&device_data
->ctx_lock
);
694 clk_disable(device_data
->clk
);
695 ret
= regulator_disable(device_data
->pwr_regulator
);
697 dev_err(dev
, "[%s]: "
698 "regulator_disable() failed!",
701 device_data
->power_state
= false;
704 spin_unlock(&device_data
->power_state_spinlock
);
709 static int cryp_enable_power(
711 struct cryp_device_data
*device_data
,
712 bool restore_device_context
)
716 dev_dbg(dev
, "[%s]", __func__
);
718 spin_lock(&device_data
->power_state_spinlock
);
719 if (!device_data
->power_state
) {
720 ret
= regulator_enable(device_data
->pwr_regulator
);
722 dev_err(dev
, "[%s]: regulator_enable() failed!",
727 ret
= clk_enable(device_data
->clk
);
729 dev_err(dev
, "[%s]: clk_enable() failed!",
731 regulator_disable(device_data
->pwr_regulator
);
734 device_data
->power_state
= true;
737 if (device_data
->restore_dev_ctx
) {
738 spin_lock(&device_data
->ctx_lock
);
739 if (restore_device_context
&& device_data
->current_ctx
) {
740 device_data
->restore_dev_ctx
= false;
741 cryp_restore_device_context(device_data
,
742 &device_data
->current_ctx
->dev_ctx
);
744 spin_unlock(&device_data
->ctx_lock
);
747 spin_unlock(&device_data
->power_state_spinlock
);
752 static int hw_crypt_noxts(struct cryp_ctx
*ctx
,
753 struct cryp_device_data
*device_data
)
757 const u8
*indata
= ctx
->indata
;
758 u8
*outdata
= ctx
->outdata
;
759 u32 datalen
= ctx
->datalen
;
760 u32 outlen
= datalen
;
762 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
764 ctx
->outlen
= ctx
->datalen
;
766 if (unlikely(!IS_ALIGNED((u32
)indata
, 4))) {
767 pr_debug(DEV_DBG_NAME
" [%s]: Data isn't aligned! Addr: "
768 "0x%08x", __func__
, (u32
)indata
);
772 ret
= cryp_setup_context(ctx
, device_data
);
777 if (cryp_mode
== CRYP_MODE_INTERRUPT
) {
778 cryp_enable_irq_src(device_data
, CRYP_IRQ_SRC_INPUT_FIFO
|
779 CRYP_IRQ_SRC_OUTPUT_FIFO
);
782 * ctx->outlen is decremented in the cryp_interrupt_handler
783 * function. We had to add cpu_relax() (barrier) to make sure
784 * that gcc didn't optimze away this variable.
786 while (ctx
->outlen
> 0)
788 } else if (cryp_mode
== CRYP_MODE_POLLING
||
789 cryp_mode
== CRYP_MODE_DMA
) {
791 * The reason for having DMA in this if case is that if we are
792 * running cryp_mode = 2, then we separate DMA routines for
793 * handling cipher/plaintext > blocksize, except when
794 * running the normal CRYPTO_ALG_TYPE_CIPHER, then we still use
795 * the polling mode. Overhead of doing DMA setup eats up the
798 cryp_polling_mode(ctx
, device_data
);
800 dev_err(ctx
->device
->dev
, "[%s]: Invalid operation mode!",
806 cryp_save_device_context(device_data
, &ctx
->dev_ctx
, cryp_mode
);
810 ctx
->indata
= indata
;
811 ctx
->outdata
= outdata
;
812 ctx
->datalen
= datalen
;
813 ctx
->outlen
= outlen
;
818 static int get_nents(struct scatterlist
*sg
, int nbytes
)
823 nbytes
-= sg
->length
;
831 static int ablk_dma_crypt(struct ablkcipher_request
*areq
)
833 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
834 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
835 struct cryp_device_data
*device_data
;
837 int bytes_written
= 0;
841 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
843 ctx
->datalen
= areq
->nbytes
;
844 ctx
->outlen
= areq
->nbytes
;
846 ret
= cryp_get_device_data(ctx
, &device_data
);
850 ret
= cryp_setup_context(ctx
, device_data
);
854 /* We have the device now, so store the nents in the dma struct. */
855 ctx
->device
->dma
.nents_src
= get_nents(areq
->src
, ctx
->datalen
);
856 ctx
->device
->dma
.nents_dst
= get_nents(areq
->dst
, ctx
->outlen
);
858 /* Enable DMA in- and output. */
859 cryp_configure_for_dma(device_data
, CRYP_DMA_ENABLE_BOTH_DIRECTIONS
);
861 bytes_written
= cryp_dma_write(ctx
, areq
->src
, ctx
->datalen
);
862 bytes_read
= cryp_dma_read(ctx
, areq
->dst
, bytes_written
);
864 wait_for_completion(&ctx
->device
->dma
.cryp_dma_complete
);
867 cryp_save_device_context(device_data
, &ctx
->dev_ctx
, cryp_mode
);
871 spin_lock(&device_data
->ctx_lock
);
872 device_data
->current_ctx
= NULL
;
874 spin_unlock(&device_data
->ctx_lock
);
877 * The down_interruptible part for this semaphore is called in
878 * cryp_get_device_data.
880 up(&driver_data
.device_allocation
);
882 if (unlikely(bytes_written
!= bytes_read
))
888 static int ablk_crypt(struct ablkcipher_request
*areq
)
890 struct ablkcipher_walk walk
;
891 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
892 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
893 struct cryp_device_data
*device_data
;
894 unsigned long src_paddr
;
895 unsigned long dst_paddr
;
899 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
901 ret
= cryp_get_device_data(ctx
, &device_data
);
905 ablkcipher_walk_init(&walk
, areq
->dst
, areq
->src
, areq
->nbytes
);
906 ret
= ablkcipher_walk_phys(areq
, &walk
);
909 pr_err(DEV_DBG_NAME
"[%s]: ablkcipher_walk_phys() failed!",
914 while ((nbytes
= walk
.nbytes
) > 0) {
916 src_paddr
= (page_to_phys(walk
.src
.page
) + walk
.src
.offset
);
917 ctx
->indata
= phys_to_virt(src_paddr
);
919 dst_paddr
= (page_to_phys(walk
.dst
.page
) + walk
.dst
.offset
);
920 ctx
->outdata
= phys_to_virt(dst_paddr
);
922 ctx
->datalen
= nbytes
- (nbytes
% ctx
->blocksize
);
924 ret
= hw_crypt_noxts(ctx
, device_data
);
928 nbytes
-= ctx
->datalen
;
929 ret
= ablkcipher_walk_done(areq
, &walk
, nbytes
);
933 ablkcipher_walk_complete(&walk
);
936 /* Release the device */
937 spin_lock(&device_data
->ctx_lock
);
938 device_data
->current_ctx
= NULL
;
940 spin_unlock(&device_data
->ctx_lock
);
943 * The down_interruptible part for this semaphore is called in
944 * cryp_get_device_data.
946 up(&driver_data
.device_allocation
);
951 static int aes_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
952 const u8
*key
, unsigned int keylen
)
954 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
955 u32
*flags
= &cipher
->base
.crt_flags
;
957 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
960 case AES_KEYSIZE_128
:
961 ctx
->config
.keysize
= CRYP_KEY_SIZE_128
;
964 case AES_KEYSIZE_192
:
965 ctx
->config
.keysize
= CRYP_KEY_SIZE_192
;
968 case AES_KEYSIZE_256
:
969 ctx
->config
.keysize
= CRYP_KEY_SIZE_256
;
973 pr_err(DEV_DBG_NAME
"[%s]: Unknown keylen!", __func__
);
974 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
978 memcpy(ctx
->key
, key
, keylen
);
979 ctx
->keylen
= keylen
;
986 static int des_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
987 const u8
*key
, unsigned int keylen
)
989 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
990 u32
*flags
= &cipher
->base
.crt_flags
;
991 u32 tmp
[DES_EXPKEY_WORDS
];
994 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
995 if (keylen
!= DES_KEY_SIZE
) {
996 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
997 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_RES_BAD_KEY_LEN",
1002 ret
= des_ekey(tmp
, key
);
1003 if (unlikely(ret
== 0) &&
1004 (*flags
& CRYPTO_TFM_REQ_FORBID_WEAK_KEYS
)) {
1005 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
1006 pr_debug(DEV_DBG_NAME
" [%s]: CRYPTO_TFM_RES_WEAK_KEY",
1011 memcpy(ctx
->key
, key
, keylen
);
1012 ctx
->keylen
= keylen
;
1018 static int des3_ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
1019 const u8
*key
, unsigned int keylen
)
1021 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1025 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1027 flags
= crypto_ablkcipher_get_flags(cipher
);
1028 err
= __des3_verify_key(&flags
, key
);
1029 if (unlikely(err
)) {
1030 crypto_ablkcipher_set_flags(cipher
, flags
);
1034 memcpy(ctx
->key
, key
, keylen
);
1035 ctx
->keylen
= keylen
;
1041 static int cryp_blk_encrypt(struct ablkcipher_request
*areq
)
1043 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1044 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1046 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1048 ctx
->config
.algodir
= CRYP_ALGORITHM_ENCRYPT
;
1051 * DMA does not work for DES due to a hw bug */
1052 if (cryp_mode
== CRYP_MODE_DMA
&& mode_is_aes(ctx
->config
.algomode
))
1053 return ablk_dma_crypt(areq
);
1055 /* For everything except DMA, we run the non DMA version. */
1056 return ablk_crypt(areq
);
1059 static int cryp_blk_decrypt(struct ablkcipher_request
*areq
)
1061 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1062 struct cryp_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1064 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1066 ctx
->config
.algodir
= CRYP_ALGORITHM_DECRYPT
;
1068 /* DMA does not work for DES due to a hw bug */
1069 if (cryp_mode
== CRYP_MODE_DMA
&& mode_is_aes(ctx
->config
.algomode
))
1070 return ablk_dma_crypt(areq
);
1072 /* For everything except DMA, we run the non DMA version. */
1073 return ablk_crypt(areq
);
1076 struct cryp_algo_template
{
1077 enum cryp_algo_mode algomode
;
1078 struct crypto_alg crypto
;
1081 static int cryp_cra_init(struct crypto_tfm
*tfm
)
1083 struct cryp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1084 struct crypto_alg
*alg
= tfm
->__crt_alg
;
1085 struct cryp_algo_template
*cryp_alg
= container_of(alg
,
1086 struct cryp_algo_template
,
1089 ctx
->config
.algomode
= cryp_alg
->algomode
;
1090 ctx
->blocksize
= crypto_tfm_alg_blocksize(tfm
);
1095 static struct cryp_algo_template cryp_algs
[] = {
1097 .algomode
= CRYP_ALGO_AES_ECB
,
1100 .cra_driver_name
= "aes-ux500",
1101 .cra_priority
= 300,
1102 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1104 .cra_blocksize
= AES_BLOCK_SIZE
,
1105 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1107 .cra_type
= &crypto_ablkcipher_type
,
1108 .cra_init
= cryp_cra_init
,
1109 .cra_module
= THIS_MODULE
,
1112 .min_keysize
= AES_MIN_KEY_SIZE
,
1113 .max_keysize
= AES_MAX_KEY_SIZE
,
1114 .setkey
= aes_ablkcipher_setkey
,
1115 .encrypt
= cryp_blk_encrypt
,
1116 .decrypt
= cryp_blk_decrypt
1122 .algomode
= CRYP_ALGO_AES_ECB
,
1124 .cra_name
= "ecb(aes)",
1125 .cra_driver_name
= "ecb-aes-ux500",
1126 .cra_priority
= 300,
1127 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1129 .cra_blocksize
= AES_BLOCK_SIZE
,
1130 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1132 .cra_type
= &crypto_ablkcipher_type
,
1133 .cra_init
= cryp_cra_init
,
1134 .cra_module
= THIS_MODULE
,
1137 .min_keysize
= AES_MIN_KEY_SIZE
,
1138 .max_keysize
= AES_MAX_KEY_SIZE
,
1139 .setkey
= aes_ablkcipher_setkey
,
1140 .encrypt
= cryp_blk_encrypt
,
1141 .decrypt
= cryp_blk_decrypt
,
1147 .algomode
= CRYP_ALGO_AES_CBC
,
1149 .cra_name
= "cbc(aes)",
1150 .cra_driver_name
= "cbc-aes-ux500",
1151 .cra_priority
= 300,
1152 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1154 .cra_blocksize
= AES_BLOCK_SIZE
,
1155 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1157 .cra_type
= &crypto_ablkcipher_type
,
1158 .cra_init
= cryp_cra_init
,
1159 .cra_module
= THIS_MODULE
,
1162 .min_keysize
= AES_MIN_KEY_SIZE
,
1163 .max_keysize
= AES_MAX_KEY_SIZE
,
1164 .setkey
= aes_ablkcipher_setkey
,
1165 .encrypt
= cryp_blk_encrypt
,
1166 .decrypt
= cryp_blk_decrypt
,
1167 .ivsize
= AES_BLOCK_SIZE
,
1173 .algomode
= CRYP_ALGO_AES_CTR
,
1175 .cra_name
= "ctr(aes)",
1176 .cra_driver_name
= "ctr-aes-ux500",
1177 .cra_priority
= 300,
1178 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1180 .cra_blocksize
= AES_BLOCK_SIZE
,
1181 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1183 .cra_type
= &crypto_ablkcipher_type
,
1184 .cra_init
= cryp_cra_init
,
1185 .cra_module
= THIS_MODULE
,
1188 .min_keysize
= AES_MIN_KEY_SIZE
,
1189 .max_keysize
= AES_MAX_KEY_SIZE
,
1190 .setkey
= aes_ablkcipher_setkey
,
1191 .encrypt
= cryp_blk_encrypt
,
1192 .decrypt
= cryp_blk_decrypt
,
1193 .ivsize
= AES_BLOCK_SIZE
,
1199 .algomode
= CRYP_ALGO_DES_ECB
,
1201 .cra_name
= "ecb(des)",
1202 .cra_driver_name
= "ecb-des-ux500",
1203 .cra_priority
= 300,
1204 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1206 .cra_blocksize
= DES_BLOCK_SIZE
,
1207 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1209 .cra_type
= &crypto_ablkcipher_type
,
1210 .cra_init
= cryp_cra_init
,
1211 .cra_module
= THIS_MODULE
,
1214 .min_keysize
= DES_KEY_SIZE
,
1215 .max_keysize
= DES_KEY_SIZE
,
1216 .setkey
= des_ablkcipher_setkey
,
1217 .encrypt
= cryp_blk_encrypt
,
1218 .decrypt
= cryp_blk_decrypt
,
1224 .algomode
= CRYP_ALGO_TDES_ECB
,
1226 .cra_name
= "ecb(des3_ede)",
1227 .cra_driver_name
= "ecb-des3_ede-ux500",
1228 .cra_priority
= 300,
1229 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1231 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1232 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1234 .cra_type
= &crypto_ablkcipher_type
,
1235 .cra_init
= cryp_cra_init
,
1236 .cra_module
= THIS_MODULE
,
1239 .min_keysize
= DES3_EDE_KEY_SIZE
,
1240 .max_keysize
= DES3_EDE_KEY_SIZE
,
1241 .setkey
= des3_ablkcipher_setkey
,
1242 .encrypt
= cryp_blk_encrypt
,
1243 .decrypt
= cryp_blk_decrypt
,
1249 .algomode
= CRYP_ALGO_DES_CBC
,
1251 .cra_name
= "cbc(des)",
1252 .cra_driver_name
= "cbc-des-ux500",
1253 .cra_priority
= 300,
1254 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1256 .cra_blocksize
= DES_BLOCK_SIZE
,
1257 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1259 .cra_type
= &crypto_ablkcipher_type
,
1260 .cra_init
= cryp_cra_init
,
1261 .cra_module
= THIS_MODULE
,
1264 .min_keysize
= DES_KEY_SIZE
,
1265 .max_keysize
= DES_KEY_SIZE
,
1266 .setkey
= des_ablkcipher_setkey
,
1267 .encrypt
= cryp_blk_encrypt
,
1268 .decrypt
= cryp_blk_decrypt
,
1274 .algomode
= CRYP_ALGO_TDES_CBC
,
1276 .cra_name
= "cbc(des3_ede)",
1277 .cra_driver_name
= "cbc-des3_ede-ux500",
1278 .cra_priority
= 300,
1279 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1281 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1282 .cra_ctxsize
= sizeof(struct cryp_ctx
),
1284 .cra_type
= &crypto_ablkcipher_type
,
1285 .cra_init
= cryp_cra_init
,
1286 .cra_module
= THIS_MODULE
,
1289 .min_keysize
= DES3_EDE_KEY_SIZE
,
1290 .max_keysize
= DES3_EDE_KEY_SIZE
,
1291 .setkey
= des3_ablkcipher_setkey
,
1292 .encrypt
= cryp_blk_encrypt
,
1293 .decrypt
= cryp_blk_decrypt
,
1294 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1302 * cryp_algs_register_all -
1304 static int cryp_algs_register_all(void)
1310 pr_debug("[%s]", __func__
);
1312 for (i
= 0; i
< ARRAY_SIZE(cryp_algs
); i
++) {
1313 ret
= crypto_register_alg(&cryp_algs
[i
].crypto
);
1316 pr_err("[%s] alg registration failed",
1317 cryp_algs
[i
].crypto
.cra_driver_name
);
1323 for (i
= 0; i
< count
; i
++)
1324 crypto_unregister_alg(&cryp_algs
[i
].crypto
);
1329 * cryp_algs_unregister_all -
1331 static void cryp_algs_unregister_all(void)
1335 pr_debug(DEV_DBG_NAME
" [%s]", __func__
);
1337 for (i
= 0; i
< ARRAY_SIZE(cryp_algs
); i
++)
1338 crypto_unregister_alg(&cryp_algs
[i
].crypto
);
1341 static int ux500_cryp_probe(struct platform_device
*pdev
)
1344 struct resource
*res
;
1345 struct resource
*res_irq
;
1346 struct cryp_device_data
*device_data
;
1347 struct cryp_protection_config prot
= {
1348 .privilege_access
= CRYP_STATE_ENABLE
1350 struct device
*dev
= &pdev
->dev
;
1352 dev_dbg(dev
, "[%s]", __func__
);
1353 device_data
= devm_kzalloc(dev
, sizeof(*device_data
), GFP_ATOMIC
);
1359 device_data
->dev
= dev
;
1360 device_data
->current_ctx
= NULL
;
1362 /* Grab the DMA configuration from platform data. */
1363 mem_to_engine
= &((struct cryp_platform_data
*)
1364 dev
->platform_data
)->mem_to_engine
;
1365 engine_to_mem
= &((struct cryp_platform_data
*)
1366 dev
->platform_data
)->engine_to_mem
;
1368 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1370 dev_err(dev
, "[%s]: platform_get_resource() failed",
1376 device_data
->phybase
= res
->start
;
1377 device_data
->base
= devm_ioremap_resource(dev
, res
);
1378 if (IS_ERR(device_data
->base
)) {
1379 dev_err(dev
, "[%s]: ioremap failed!", __func__
);
1380 ret
= PTR_ERR(device_data
->base
);
1384 spin_lock_init(&device_data
->ctx_lock
);
1385 spin_lock_init(&device_data
->power_state_spinlock
);
1387 /* Enable power for CRYP hardware block */
1388 device_data
->pwr_regulator
= regulator_get(&pdev
->dev
, "v-ape");
1389 if (IS_ERR(device_data
->pwr_regulator
)) {
1390 dev_err(dev
, "[%s]: could not get cryp regulator", __func__
);
1391 ret
= PTR_ERR(device_data
->pwr_regulator
);
1392 device_data
->pwr_regulator
= NULL
;
1396 /* Enable the clk for CRYP hardware block */
1397 device_data
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1398 if (IS_ERR(device_data
->clk
)) {
1399 dev_err(dev
, "[%s]: clk_get() failed!", __func__
);
1400 ret
= PTR_ERR(device_data
->clk
);
1404 ret
= clk_prepare(device_data
->clk
);
1406 dev_err(dev
, "[%s]: clk_prepare() failed!", __func__
);
1410 /* Enable device power (and clock) */
1411 ret
= cryp_enable_power(device_data
->dev
, device_data
, false);
1413 dev_err(dev
, "[%s]: cryp_enable_power() failed!", __func__
);
1414 goto out_clk_unprepare
;
1417 if (cryp_check(device_data
)) {
1418 dev_err(dev
, "[%s]: cryp_check() failed!", __func__
);
1423 if (cryp_configure_protection(device_data
, &prot
)) {
1424 dev_err(dev
, "[%s]: cryp_configure_protection() failed!",
1430 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1432 dev_err(dev
, "[%s]: IORESOURCE_IRQ unavailable",
1438 ret
= devm_request_irq(&pdev
->dev
, res_irq
->start
,
1439 cryp_interrupt_handler
, 0, "cryp1", device_data
);
1441 dev_err(dev
, "[%s]: Unable to request IRQ", __func__
);
1445 if (cryp_mode
== CRYP_MODE_DMA
)
1446 cryp_dma_setup_channel(device_data
, dev
);
1448 platform_set_drvdata(pdev
, device_data
);
1450 /* Put the new device into the device list... */
1451 klist_add_tail(&device_data
->list_node
, &driver_data
.device_list
);
1453 /* ... and signal that a new device is available. */
1454 up(&driver_data
.device_allocation
);
1456 atomic_set(&session_id
, 1);
1458 ret
= cryp_algs_register_all();
1460 dev_err(dev
, "[%s]: cryp_algs_register_all() failed!",
1465 dev_info(dev
, "successfully registered\n");
1470 cryp_disable_power(device_data
->dev
, device_data
, false);
1473 clk_unprepare(device_data
->clk
);
1476 regulator_put(device_data
->pwr_regulator
);
1482 static int ux500_cryp_remove(struct platform_device
*pdev
)
1484 struct cryp_device_data
*device_data
;
1486 dev_dbg(&pdev
->dev
, "[%s]", __func__
);
1487 device_data
= platform_get_drvdata(pdev
);
1489 dev_err(&pdev
->dev
, "[%s]: platform_get_drvdata() failed!",
1494 /* Try to decrease the number of available devices. */
1495 if (down_trylock(&driver_data
.device_allocation
))
1498 /* Check that the device is free */
1499 spin_lock(&device_data
->ctx_lock
);
1500 /* current_ctx allocates a device, NULL = unallocated */
1501 if (device_data
->current_ctx
) {
1502 /* The device is busy */
1503 spin_unlock(&device_data
->ctx_lock
);
1504 /* Return the device to the pool. */
1505 up(&driver_data
.device_allocation
);
1509 spin_unlock(&device_data
->ctx_lock
);
1511 /* Remove the device from the list */
1512 if (klist_node_attached(&device_data
->list_node
))
1513 klist_remove(&device_data
->list_node
);
1515 /* If this was the last device, remove the services */
1516 if (list_empty(&driver_data
.device_list
.k_list
))
1517 cryp_algs_unregister_all();
1519 if (cryp_disable_power(&pdev
->dev
, device_data
, false))
1520 dev_err(&pdev
->dev
, "[%s]: cryp_disable_power() failed",
1523 clk_unprepare(device_data
->clk
);
1524 regulator_put(device_data
->pwr_regulator
);
1529 static void ux500_cryp_shutdown(struct platform_device
*pdev
)
1531 struct cryp_device_data
*device_data
;
1533 dev_dbg(&pdev
->dev
, "[%s]", __func__
);
1535 device_data
= platform_get_drvdata(pdev
);
1537 dev_err(&pdev
->dev
, "[%s]: platform_get_drvdata() failed!",
1542 /* Check that the device is free */
1543 spin_lock(&device_data
->ctx_lock
);
1544 /* current_ctx allocates a device, NULL = unallocated */
1545 if (!device_data
->current_ctx
) {
1546 if (down_trylock(&driver_data
.device_allocation
))
1547 dev_dbg(&pdev
->dev
, "[%s]: Cryp still in use!"
1548 "Shutting down anyway...", __func__
);
1550 * (Allocate the device)
1551 * Need to set this to non-null (dummy) value,
1552 * to avoid usage if context switching.
1554 device_data
->current_ctx
++;
1556 spin_unlock(&device_data
->ctx_lock
);
1558 /* Remove the device from the list */
1559 if (klist_node_attached(&device_data
->list_node
))
1560 klist_remove(&device_data
->list_node
);
1562 /* If this was the last device, remove the services */
1563 if (list_empty(&driver_data
.device_list
.k_list
))
1564 cryp_algs_unregister_all();
1566 if (cryp_disable_power(&pdev
->dev
, device_data
, false))
1567 dev_err(&pdev
->dev
, "[%s]: cryp_disable_power() failed",
1572 #ifdef CONFIG_PM_SLEEP
1573 static int ux500_cryp_suspend(struct device
*dev
)
1576 struct platform_device
*pdev
= to_platform_device(dev
);
1577 struct cryp_device_data
*device_data
;
1578 struct resource
*res_irq
;
1579 struct cryp_ctx
*temp_ctx
= NULL
;
1581 dev_dbg(dev
, "[%s]", __func__
);
1584 device_data
= platform_get_drvdata(pdev
);
1586 dev_err(dev
, "[%s]: platform_get_drvdata() failed!", __func__
);
1590 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1592 dev_err(dev
, "[%s]: IORESOURCE_IRQ, unavailable", __func__
);
1594 disable_irq(res_irq
->start
);
1596 spin_lock(&device_data
->ctx_lock
);
1597 if (!device_data
->current_ctx
)
1598 device_data
->current_ctx
++;
1599 spin_unlock(&device_data
->ctx_lock
);
1601 if (device_data
->current_ctx
== ++temp_ctx
) {
1602 if (down_interruptible(&driver_data
.device_allocation
))
1603 dev_dbg(dev
, "[%s]: down_interruptible() failed",
1605 ret
= cryp_disable_power(dev
, device_data
, false);
1608 ret
= cryp_disable_power(dev
, device_data
, true);
1611 dev_err(dev
, "[%s]: cryp_disable_power()", __func__
);
1616 static int ux500_cryp_resume(struct device
*dev
)
1619 struct platform_device
*pdev
= to_platform_device(dev
);
1620 struct cryp_device_data
*device_data
;
1621 struct resource
*res_irq
;
1622 struct cryp_ctx
*temp_ctx
= NULL
;
1624 dev_dbg(dev
, "[%s]", __func__
);
1626 device_data
= platform_get_drvdata(pdev
);
1628 dev_err(dev
, "[%s]: platform_get_drvdata() failed!", __func__
);
1632 spin_lock(&device_data
->ctx_lock
);
1633 if (device_data
->current_ctx
== ++temp_ctx
)
1634 device_data
->current_ctx
= NULL
;
1635 spin_unlock(&device_data
->ctx_lock
);
1638 if (!device_data
->current_ctx
)
1639 up(&driver_data
.device_allocation
);
1641 ret
= cryp_enable_power(dev
, device_data
, true);
1644 dev_err(dev
, "[%s]: cryp_enable_power() failed!", __func__
);
1646 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1648 enable_irq(res_irq
->start
);
1655 static SIMPLE_DEV_PM_OPS(ux500_cryp_pm
, ux500_cryp_suspend
, ux500_cryp_resume
);
1657 static const struct of_device_id ux500_cryp_match
[] = {
1658 { .compatible
= "stericsson,ux500-cryp" },
1661 MODULE_DEVICE_TABLE(of
, ux500_cryp_match
);
1663 static struct platform_driver cryp_driver
= {
1664 .probe
= ux500_cryp_probe
,
1665 .remove
= ux500_cryp_remove
,
1666 .shutdown
= ux500_cryp_shutdown
,
1669 .of_match_table
= ux500_cryp_match
,
1670 .pm
= &ux500_cryp_pm
,
1674 static int __init
ux500_cryp_mod_init(void)
1676 pr_debug("[%s] is called!", __func__
);
1677 klist_init(&driver_data
.device_list
, NULL
, NULL
);
1678 /* Initialize the semaphore to 0 devices (locked state) */
1679 sema_init(&driver_data
.device_allocation
, 0);
1680 return platform_driver_register(&cryp_driver
);
1683 static void __exit
ux500_cryp_mod_fini(void)
1685 pr_debug("[%s] is called!", __func__
);
1686 platform_driver_unregister(&cryp_driver
);
1689 module_init(ux500_cryp_mod_init
);
1690 module_exit(ux500_cryp_mod_fini
);
1692 module_param(cryp_mode
, int, 0);
1694 MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 CRYP crypto engine.");
1695 MODULE_ALIAS_CRYPTO("aes-all");
1696 MODULE_ALIAS_CRYPTO("des-all");
1698 MODULE_LICENSE("GPL");