Lines Matching +full:i2c +full:- +full:command
4 * SPDX-License-Identifier: Apache-2.0
11 #include <zephyr/drivers/i2c.h>
48 struct ataes132a_device_data *data = dev->data; in ataes132a_send_command()
49 const struct ataes132a_device_config *cfg = dev->config; in ataes132a_send_command()
57 LOG_ERR("command too large for command buffer"); in ataes132a_send_command()
58 return -EDOM; in ataes132a_send_command()
61 /* If there is a command in progress, idle wait until it is available. in ataes132a_send_command()
65 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status); in ataes132a_send_command()
69 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status); in ataes132a_send_command()
72 data->command_buffer[0] = count; in ataes132a_send_command()
73 data->command_buffer[1] = opcode; in ataes132a_send_command()
74 data->command_buffer[2] = mode; in ataes132a_send_command()
76 data->command_buffer[i + 3] = params[i]; in ataes132a_send_command()
79 /*Calculate command CRC*/ in ataes132a_send_command()
80 ataes132a_atmel_crc(data->command_buffer, nparams + 3, crc); in ataes132a_send_command()
81 data->command_buffer[nparams + 3] = crc[0]; in ataes132a_send_command()
82 data->command_buffer[nparams + 4] = crc[1]; in ataes132a_send_command()
84 /*Reset i/O address start before sending a command*/ in ataes132a_send_command()
85 write_reg_i2c(&cfg->i2c, ATAES_COMMAND_ADDRR_RESET, 0x0); in ataes132a_send_command()
87 /*Send a command through the command buffer*/ in ataes132a_send_command()
88 i2c_return = burst_write_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR, in ataes132a_send_command()
89 data->command_buffer, count); in ataes132a_send_command()
93 /* Idle-waiting for the command completion*/ in ataes132a_send_command()
96 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status); in ataes132a_send_command()
100 LOG_ERR("incorrect CRC command"); in ataes132a_send_command()
101 return -EINVAL; in ataes132a_send_command()
106 return -EINVAL; in ataes132a_send_command()
110 burst_read_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR, data->command_buffer, 64); in ataes132a_send_command()
112 count = data->command_buffer[0]; in ataes132a_send_command()
115 ataes132a_atmel_crc(data->command_buffer, count - 2, crc); in ataes132a_send_command()
117 LOG_DBG("COMMAND CRC %x%x", data->command_buffer[count - 2], in ataes132a_send_command()
118 data->command_buffer[count - 1]); in ataes132a_send_command()
122 while (crc[0] != data->command_buffer[count - 2] || in ataes132a_send_command()
123 crc[1] != data->command_buffer[count - 1]) { in ataes132a_send_command()
124 if (retry_count > MAX_RETRIES - 1) { in ataes132a_send_command()
127 return -EINVAL; in ataes132a_send_command()
130 burst_read_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR, data->command_buffer, 64); in ataes132a_send_command()
132 count = data->command_buffer[0]; in ataes132a_send_command()
134 ataes132a_atmel_crc(data->command_buffer, count - 2, crc); in ataes132a_send_command()
137 LOG_DBG("COMMAND RETRY %d", retry_count); in ataes132a_send_command()
138 LOG_DBG("COMMAND CRC %x%x", in ataes132a_send_command()
139 data->command_buffer[count - 2], in ataes132a_send_command()
140 data->command_buffer[count - 1]); in ataes132a_send_command()
144 if ((status & ATAES_STATUS_ERR) || data->command_buffer[1] != 0x00) { in ataes132a_send_command()
145 LOG_ERR("command execution error %x", in ataes132a_send_command()
146 data->command_buffer[1]); in ataes132a_send_command()
147 return -EIO; in ataes132a_send_command()
152 for (i = 0; i < count - 3; i++) { in ataes132a_send_command()
153 response[i] = data->command_buffer[i + 1]; in ataes132a_send_command()
156 *nresponse = count - 3; in ataes132a_send_command()
163 struct ataes132a_device_data *ataes132a = dev->data; in ataes132a_init()
164 const struct ataes132a_device_config *cfg = dev->config; in ataes132a_init()
169 if (!device_is_ready(cfg->i2c.bus)) { in ataes132a_init()
171 return -ENODEV; in ataes132a_init()
176 i2c_configure(cfg->i2c.bus, i2c_cfg); in ataes132a_init()
178 k_sem_init(&ataes132a->device_sem, 1, K_SEM_MAX_LIMIT); in ataes132a_init()
193 struct ataes132a_device_data *data = dev->data; in ataes132a_aes_ccm_decrypt()
202 return -EINVAL; in ataes132a_aes_ccm_decrypt()
205 if (!aead_op->pkt) { in ataes132a_aes_ccm_decrypt()
207 return -EINVAL; in ataes132a_aes_ccm_decrypt()
210 in_buf_len = aead_op->pkt->in_len; in ataes132a_aes_ccm_decrypt()
211 expected_out_len = aead_op->pkt->out_len; in ataes132a_aes_ccm_decrypt()
216 return -EINVAL; in ataes132a_aes_ccm_decrypt()
222 return -EINVAL; in ataes132a_aes_ccm_decrypt()
228 return -EINVAL; in ataes132a_aes_ccm_decrypt()
239 if (!aead_op->pkt->in_buf || !aead_op->pkt->out_buf) { in ataes132a_aes_ccm_decrypt()
257 k_sem_take(&data->device_sem, K_FOREVER); in ataes132a_aes_ccm_decrypt()
276 LOG_ERR("nonce command ended with code %d", in ataes132a_aes_ccm_decrypt()
278 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
279 return -EINVAL; in ataes132a_aes_ccm_decrypt()
283 LOG_ERR("nonce command failed with error" in ataes132a_aes_ccm_decrypt()
285 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
286 return -EIO; in ataes132a_aes_ccm_decrypt()
292 * the command will use the current nonce register value. in ataes132a_aes_ccm_decrypt()
304 param_buffer[0] = mac_packet->encryption_key_id; in ataes132a_aes_ccm_decrypt()
305 param_buffer[2] = mac_packet->encryption_mac_count; in ataes132a_aes_ccm_decrypt()
318 if (mac_mode->include_counter) { in ataes132a_aes_ccm_decrypt()
324 if (mac_mode->include_serial) { in ataes132a_aes_ccm_decrypt()
330 if (mac_mode->include_smallzone) { in ataes132a_aes_ccm_decrypt()
340 if (aead_op->tag) { in ataes132a_aes_ccm_decrypt()
341 memcpy(param_buffer + 4, aead_op->tag, 16); in ataes132a_aes_ccm_decrypt()
343 memcpy(param_buffer + 20, aead_op->pkt->in_buf, in_buf_len); in ataes132a_aes_ccm_decrypt()
351 LOG_ERR("decrypt command ended with code %d", return_code); in ataes132a_aes_ccm_decrypt()
352 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
353 return -EINVAL; in ataes132a_aes_ccm_decrypt()
357 LOG_ERR("decrypt command response has invalid" in ataes132a_aes_ccm_decrypt()
359 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
360 return -EINVAL; in ataes132a_aes_ccm_decrypt()
364 LOG_ERR("legacy command failed with error" in ataes132a_aes_ccm_decrypt()
366 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
367 return -param_buffer[0]; in ataes132a_aes_ccm_decrypt()
370 if (expected_out_len != out_len - 1) { in ataes132a_aes_ccm_decrypt()
372 " size %d are different", out_len - 1, in ataes132a_aes_ccm_decrypt()
374 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
375 return -EINVAL; in ataes132a_aes_ccm_decrypt()
378 memcpy(aead_op->pkt->out_buf, param_buffer + 1, out_len - 1); in ataes132a_aes_ccm_decrypt()
380 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_decrypt()
393 struct ataes132a_device_data *data = dev->data; in ataes132a_aes_ccm_encrypt()
408 return -EINVAL; in ataes132a_aes_ccm_encrypt()
411 if (!aead_op->pkt) { in ataes132a_aes_ccm_encrypt()
413 return -EINVAL; in ataes132a_aes_ccm_encrypt()
416 buf_len = aead_op->pkt->in_len; in ataes132a_aes_ccm_encrypt()
421 return -EINVAL; in ataes132a_aes_ccm_encrypt()
426 return -EINVAL; in ataes132a_aes_ccm_encrypt()
437 if (!aead_op->pkt->in_buf || !aead_op->pkt->out_buf) { in ataes132a_aes_ccm_encrypt()
455 k_sem_take(&data->device_sem, K_FOREVER); in ataes132a_aes_ccm_encrypt()
474 LOG_ERR("nonce command ended with code %d", in ataes132a_aes_ccm_encrypt()
476 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
477 return -EINVAL; in ataes132a_aes_ccm_encrypt()
481 LOG_ERR("nonce command failed with error" in ataes132a_aes_ccm_encrypt()
483 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
484 return -EIO; in ataes132a_aes_ccm_encrypt()
489 * the command will use the current nonce register value. in ataes132a_aes_ccm_encrypt()
500 if (mac_mode->include_counter) { in ataes132a_aes_ccm_encrypt()
506 if (mac_mode->include_serial) { in ataes132a_aes_ccm_encrypt()
512 if (mac_mode->include_smallzone) { in ataes132a_aes_ccm_encrypt()
522 memcpy(param_buffer + 2, aead_op->pkt->in_buf, buf_len); in ataes132a_aes_ccm_encrypt()
530 LOG_ERR("encrypt command ended with code %d", return_code); in ataes132a_aes_ccm_encrypt()
531 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
532 return -EINVAL; in ataes132a_aes_ccm_encrypt()
536 LOG_ERR("encrypt command response has invalid" in ataes132a_aes_ccm_encrypt()
538 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
539 return -EINVAL; in ataes132a_aes_ccm_encrypt()
543 LOG_ERR("encrypt command failed with error" in ataes132a_aes_ccm_encrypt()
545 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
546 return -EIO; in ataes132a_aes_ccm_encrypt()
549 if (aead_op->tag) { in ataes132a_aes_ccm_encrypt()
550 memcpy(aead_op->tag, param_buffer + 1, 16); in ataes132a_aes_ccm_encrypt()
553 memcpy(aead_op->pkt->out_buf, param_buffer + 17, out_len - 17U); in ataes132a_aes_ccm_encrypt()
556 if (mac_mode->include_counter) { in ataes132a_aes_ccm_encrypt()
565 LOG_ERR("info command failed with error" in ataes132a_aes_ccm_encrypt()
567 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
568 return -EIO; in ataes132a_aes_ccm_encrypt()
576 k_sem_give(&data->device_sem); in ataes132a_aes_ccm_encrypt()
585 struct ataes132a_device_data *data = dev->data; in ataes132a_aes_ecb_block()
593 return -EINVAL; in ataes132a_aes_ecb_block()
596 buf_len = pkt->in_len; in ataes132a_aes_ecb_block()
599 return -EINVAL; in ataes132a_aes_ecb_block()
607 return -EINVAL; in ataes132a_aes_ecb_block()
612 return -EINVAL; in ataes132a_aes_ecb_block()
627 if (!pkt->in_buf || !pkt->out_buf) { in ataes132a_aes_ecb_block()
631 k_sem_take(&data->device_sem, K_FOREVER); in ataes132a_aes_ecb_block()
636 memcpy(param_buffer + 3, pkt->in_buf, buf_len); in ataes132a_aes_ecb_block()
637 (void)memset(param_buffer + 3 + buf_len, 0x0, 16 - buf_len); in ataes132a_aes_ecb_block()
644 LOG_ERR("legacy command ended with code %d", return_code); in ataes132a_aes_ecb_block()
645 k_sem_give(&data->device_sem); in ataes132a_aes_ecb_block()
646 return -EINVAL; in ataes132a_aes_ecb_block()
650 LOG_ERR("legacy command response has invalid" in ataes132a_aes_ecb_block()
652 k_sem_give(&data->device_sem); in ataes132a_aes_ecb_block()
653 return -EINVAL; in ataes132a_aes_ecb_block()
656 LOG_ERR("legacy command failed with error" in ataes132a_aes_ecb_block()
658 k_sem_give(&data->device_sem); in ataes132a_aes_ecb_block()
659 return -EIO; in ataes132a_aes_ecb_block()
662 memcpy(pkt->out_buf, param_buffer + 1, 16); in ataes132a_aes_ecb_block()
664 k_sem_give(&data->device_sem); in ataes132a_aes_ecb_block()
672 const struct device *dev = ctx->device; in do_ccm_encrypt_mac()
673 struct ataes132a_driver_state *state = ctx->drv_sessn_state; in do_ccm_encrypt_mac()
677 key_id = state->key_id; in do_ccm_encrypt_mac()
679 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id); in do_ccm_encrypt_mac()
689 if (aead_op->pkt->in_len <= 16 && in do_ccm_encrypt_mac()
690 aead_op->pkt->out_buf_max < 16) { in do_ccm_encrypt_mac()
692 return -EINVAL; in do_ccm_encrypt_mac()
695 if (aead_op->pkt->in_len > 16 && in do_ccm_encrypt_mac()
696 aead_op->pkt->out_buf_max < 32) { in do_ccm_encrypt_mac()
698 return -EINVAL; in do_ccm_encrypt_mac()
701 if (aead_op->pkt->in_len <= 16) { in do_ccm_encrypt_mac()
702 aead_op->pkt->out_len = 16; in do_ccm_encrypt_mac()
703 } else if (aead_op->pkt->in_len > 16) { in do_ccm_encrypt_mac()
704 aead_op->pkt->out_len = 32; in do_ccm_encrypt_mac()
707 if (aead_op->ad != NULL || aead_op->ad_len != 0U) { in do_ccm_encrypt_mac()
709 return -EINVAL; in do_ccm_encrypt_mac()
721 const struct device *dev = ctx->device; in do_ccm_decrypt_auth()
722 struct ataes132a_driver_state *state = ctx->drv_sessn_state; in do_ccm_decrypt_auth()
726 key_id = state->key_id; in do_ccm_decrypt_auth()
728 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id); in do_ccm_decrypt_auth()
738 if (aead_op->pkt->in_len <= 16 && in do_ccm_decrypt_auth()
739 aead_op->pkt->out_buf_max < 16) { in do_ccm_decrypt_auth()
741 return -EINVAL; in do_ccm_decrypt_auth()
744 if (aead_op->pkt->in_len > 16 && in do_ccm_decrypt_auth()
745 aead_op->pkt->out_buf_max < 32) { in do_ccm_decrypt_auth()
747 return -EINVAL; in do_ccm_decrypt_auth()
750 aead_op->pkt->ctx = ctx; in do_ccm_decrypt_auth()
752 if (aead_op->ad != NULL || aead_op->ad_len != 0U) { in do_ccm_decrypt_auth()
754 return -EINVAL; in do_ccm_decrypt_auth()
766 const struct device *dev = ctx->device; in do_block()
767 struct ataes132a_driver_state *state = ctx->drv_sessn_state; in do_block()
770 key_id = state->key_id; in do_block()
772 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id); in do_block()
774 if (pkt->out_buf_max < 16) { in do_block()
776 return -EINVAL; in do_block()
779 pkt->out_len = 16; in do_block()
787 struct ataes132a_driver_state *state = session->drv_sessn_state; in ataes132a_session_free()
791 state->in_use = false; in ataes132a_session_free()
801 uint8_t key_id = *((uint8_t *)ctx->key.handle); in ataes132a_session_setup()
802 const struct ataes132a_device_config *cfg = dev->config; in ataes132a_session_setup()
807 return -EINVAL; in ataes132a_session_setup()
810 ctx->mode_params.ccm_info.tag_len != 16U) { in ataes132a_session_setup()
812 return -EINVAL; in ataes132a_session_setup()
815 ctx->mode_params.ccm_info.nonce_len != 12U) { in ataes132a_session_setup()
817 return -EINVAL; in ataes132a_session_setup()
821 read_reg_i2c(&cfg->i2c, ATAES_KEYCFG_REG(key_id), &config); in ataes132a_session_setup()
823 read_reg_i2c(&cfg->i2c, ATAES_CHIPCONFIG_REG, &config); in ataes132a_session_setup()
826 ctx->drv_sessn_state = &ataes132a_state[key_id]; in ataes132a_session_setup()
827 ctx->device = dev; in ataes132a_session_setup()
831 return -EINVAL; in ataes132a_session_setup()
834 /*ATAES132A support I2C polling only*/ in ataes132a_session_setup()
835 if (!(ctx->flags & CAP_SYNC_OPS)) { in ataes132a_session_setup()
837 return -EINVAL; in ataes132a_session_setup()
840 if (ctx->keylen != ATAES132A_AES_KEY_SIZE) { in ataes132a_session_setup()
842 return -EINVAL; in ataes132a_session_setup()
848 ctx->ops.block_crypt_hndlr = do_block; in ataes132a_session_setup()
851 ctx->ops.ccm_crypt_hndlr = do_ccm_encrypt_mac; in ataes132a_session_setup()
855 return -EINVAL; in ataes132a_session_setup()
860 ctx->ops.block_crypt_hndlr = do_block; in ataes132a_session_setup()
863 ctx->ops.ccm_crypt_hndlr = do_ccm_decrypt_auth; in ataes132a_session_setup()
867 return -EINVAL; in ataes132a_session_setup()
871 ctx->ops.cipher_mode = mode; in ataes132a_session_setup()
883 .i2c = I2C_DT_SPEC_INST_GET(0),