11 #include "spi_device_regs.h"
13 #define DIF_SPI_DEVICE_TPM_FIFO_DEPTH 16
16 kDifSpiDeviceEFlashLen =
17 SPI_DEVICE_PARAM_SRAM_READ_BUFFER_DEPTH *
sizeof(uint32_t),
18 kDifSpiDeviceMailboxLen =
19 SPI_DEVICE_PARAM_SRAM_MAILBOX_DEPTH *
sizeof(uint32_t),
20 kDifSpiDeviceSfdpLen = SPI_DEVICE_PARAM_SRAM_SFDP_DEPTH *
sizeof(uint32_t),
21 kDifSpiDevicePayloadLen =
22 SPI_DEVICE_PARAM_SRAM_PAYLOAD_DEPTH *
sizeof(uint32_t),
23 kDifSpiDeviceTpmWriteFifoLen =
24 SPI_DEVICE_PARAM_SRAM_TPM_WR_FIFO_DEPTH *
sizeof(uint32_t),
28 kDifSpiDeviceEFlashOffset =
29 SPI_DEVICE_PARAM_SRAM_READ_BUFFER_OFFSET *
sizeof(uint32_t),
30 kDifSpiDeviceMailboxOffset =
31 SPI_DEVICE_PARAM_SRAM_MAILBOX_OFFSET *
sizeof(uint32_t),
32 kDifSpiDeviceSfdpOffset =
33 SPI_DEVICE_PARAM_SRAM_SFDP_OFFSET *
sizeof(uint32_t),
34 kDifSpiDevicePayloadOffset =
35 SPI_DEVICE_PARAM_SRAM_PAYLOAD_OFFSET *
sizeof(uint32_t),
36 kDifSpiDeviceTpmWriteFifoOffset =
37 SPI_DEVICE_PARAM_SRAM_TPM_WR_FIFO_OFFSET *
sizeof(uint32_t),
44 static inline uint32_t build_control_word(
60 static inline uint32_t extract_mode_from_config(
62 switch (config.device_mode) {
64 return SPI_DEVICE_CONTROL_MODE_VALUE_DISABLED;
66 return SPI_DEVICE_CONTROL_MODE_VALUE_FLASHMODE;
68 return SPI_DEVICE_CONTROL_MODE_VALUE_PASSTHROUGH;
89 uint32_t device_mode = extract_mode_from_config(config);
90 if (device_mode == -1u) {
94 uint32_t device_config = build_control_word(config);
95 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET,
99 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET);
103 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET,
116 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET);
118 if (mode != SPI_DEVICE_CONTROL_MODE_VALUE_FLASHMODE &&
119 mode != SPI_DEVICE_CONTROL_MODE_VALUE_PASSTHROUGH) {
124 SPI_DEVICE_CONTROL_MODE_VALUE_PASSTHROUGH);
127 SPI_DEVICE_CONTROL_MODE_VALUE_FLASHMODE);
129 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET,
136 if (spi == NULL || csb == NULL) {
140 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_STATUS_REG_OFFSET);
150 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_MAILBOX_ADDR_REG_OFFSET,
153 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET);
155 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET, cfg_reg);
164 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET);
166 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET, cfg_reg);
172 if (spi == NULL || is_enabled == NULL || address == NULL) {
176 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_CFG_REG_OFFSET);
177 bool mailbox_enabled =
181 SPI_DEVICE_MAILBOX_ADDR_REG_OFFSET);
190 uint32_t cfg_reg = 0;
198 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_ADDR_MODE_REG_OFFSET,
205 if (spi == NULL || addr_4b == NULL) {
209 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_ADDR_MODE_REG_OFFSET);
224 uint32_t device_mode = extract_mode_from_config(spi->
config);
225 if (device_mode == -1u) {
232 control, SPI_DEVICE_CONTROL_FLASH_STATUS_FIFO_CLR_BIT,
true);
233 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET,
241 if (spi == NULL ||
id == NULL) {
245 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_JEDEC_CC_REG_OFFSET);
247 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_JEDEC_ID_REG_OFFSET);
248 id->num_continuation_code =
250 id->continuation_code =
252 id->manufacturer_id =
265 id.num_continuation_code);
267 id.continuation_code);
272 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_JEDEC_CC_REG_OFFSET,
274 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_JEDEC_ID_REG_OFFSET,
293 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_INTERCEPT_EN_REG_OFFSET,
300 if (spi == NULL || address == NULL) {
304 SPI_DEVICE_LAST_READ_ADDR_REG_OFFSET);
310 if (spi == NULL || address > SPI_DEVICE_READ_THRESHOLD_THRESHOLD_MASK) {
313 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_READ_THRESHOLD_REG_OFFSET,
323 uint32_t device_mode = extract_mode_from_config(spi->
config);
324 if (device_mode == -1u) {
331 control, SPI_DEVICE_CONTROL_FLASH_READ_BUFFER_CLR_BIT,
true);
332 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_CONTROL_REG_OFFSET,
341 if (spi == NULL || slot >= SPI_DEVICE_PARAM_NUM_CMD_INFO ||
345 ptrdiff_t reg_offset =
346 SPI_DEVICE_CMD_INFO_0_REG_OFFSET + slot *
sizeof(uint32_t);
347 uint32_t reg_val = 0;
353 uint32_t address_mode;
354 switch (command_info.address_type) {
356 address_mode = SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDRDISABLED;
359 address_mode = SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDRCFG;
362 address_mode = SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDR3B;
365 address_mode = SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDR4B;
371 (1u + SPI_DEVICE_CMD_INFO_0_DUMMY_SIZE_0_MASK)) {
397 uint32_t read_pipeline_mode;
401 SPI_DEVICE_CMD_INFO_0_READ_PIPELINE_MODE_0_VALUE_ZERO_STAGES;
405 SPI_DEVICE_CMD_INFO_0_READ_PIPELINE_MODE_0_VALUE_TWO_STAGES_HALF_CYCLE;
409 SPI_DEVICE_CMD_INFO_0_READ_PIPELINE_MODE_0_VALUE_TWO_STAGES_FULL_CYCLE;
428 reg_val, SPI_DEVICE_CMD_INFO_0_OPCODE_0_FIELD, command_info.
opcode);
430 reg_val, SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_FIELD, address_mode);
436 SPI_DEVICE_CMD_INFO_0_DUMMY_SIZE_0_FIELD,
439 reg_val, SPI_DEVICE_CMD_INFO_0_DUMMY_EN_0_BIT,
true);
442 reg_val, SPI_DEVICE_CMD_INFO_0_PAYLOAD_EN_0_FIELD, payload_en);
447 SPI_DEVICE_CMD_INFO_0_PAYLOAD_SWAP_EN_0_BIT,
450 reg_val, SPI_DEVICE_CMD_INFO_0_READ_PIPELINE_MODE_0_FIELD,
459 mmio_region_write32(spi->
dev.
base_addr, reg_offset, reg_val);
466 if (spi == NULL || enabled == NULL || command_info == NULL ||
467 slot >= SPI_DEVICE_PARAM_NUM_CMD_INFO) {
470 ptrdiff_t reg_offset =
471 SPI_DEVICE_CMD_INFO_0_REG_OFFSET + slot *
sizeof(uint32_t);
472 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
474 dif_spi_device_flash_address_type_t address_type;
475 uint32_t reg_val_address_mode =
477 switch (reg_val_address_mode) {
478 case SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDRDISABLED:
481 case SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDRCFG:
484 case SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDR3B:
487 case SPI_DEVICE_CMD_INFO_0_ADDR_MODE_0_VALUE_ADDR4B:
491 address_type = kDifSpiDeviceFlashAddrCount;
495 uint32_t dummy_cycles = 0;
498 reg_val, SPI_DEVICE_CMD_INFO_0_DUMMY_SIZE_0_FIELD);
503 uint32_t payload_en =
505 bool payload_dir_to_host =
508 switch (payload_en) {
513 if (payload_dir_to_host) {
520 if (!payload_dir_to_host) {
539 reg_val, SPI_DEVICE_CMD_INFO_0_OPCODE_0_FIELD),
540 .address_type = address_type,
541 .dummy_cycles = (uint8_t)dummy_cycles,
542 .payload_io_type = payload_io_type,
544 reg_val, SPI_DEVICE_CMD_INFO_0_PAYLOAD_SWAP_EN_0_BIT),
545 .payload_dir_to_host = payload_dir_to_host,
547 reg_val, SPI_DEVICE_CMD_INFO_0_PAYLOAD_SWAP_EN_0_BIT),
575 ptrdiff_t reg_offset) {
584 mmio_region_write32(spi->
dev.
base_addr, reg_offset, reg_val);
590 return write_special_cmd_info(spi, enable, opcode,
591 SPI_DEVICE_CMD_INFO_EN4B_REG_OFFSET);
596 return write_special_cmd_info(spi, enable, opcode,
597 SPI_DEVICE_CMD_INFO_EX4B_REG_OFFSET);
602 return write_special_cmd_info(spi, enable, opcode,
603 SPI_DEVICE_CMD_INFO_WREN_REG_OFFSET);
608 return write_special_cmd_info(spi, enable, opcode,
609 SPI_DEVICE_CMD_INFO_WRDI_REG_OFFSET);
614 uint32_t replacement) {
618 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_ADDR_SWAP_MASK_REG_OFFSET,
620 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_ADDR_SWAP_DATA_REG_OFFSET,
627 uint32_t replacement) {
632 SPI_DEVICE_PAYLOAD_SWAP_MASK_REG_OFFSET, mask);
634 SPI_DEVICE_PAYLOAD_SWAP_DATA_REG_OFFSET, replacement);
640 if (spi == NULL || occupancy == NULL) {
643 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr,
644 SPI_DEVICE_UPLOAD_STATUS_REG_OFFSET);
646 reg_val, SPI_DEVICE_UPLOAD_STATUS_CMDFIFO_DEPTH_FIELD);
652 if (spi == NULL || occupancy == NULL) {
655 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr,
656 SPI_DEVICE_UPLOAD_STATUS_REG_OFFSET);
658 reg_val, SPI_DEVICE_UPLOAD_STATUS_ADDRFIFO_DEPTH_FIELD);
664 if (spi == NULL || occupancy == NULL || start_offset == NULL) {
667 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr,
668 SPI_DEVICE_UPLOAD_STATUS2_REG_OFFSET);
670 reg_val, SPI_DEVICE_UPLOAD_STATUS2_PAYLOAD_DEPTH_FIELD);
672 reg_val, SPI_DEVICE_UPLOAD_STATUS2_PAYLOAD_START_IDX_FIELD);
679 if (spi == NULL || command == NULL) {
682 uint32_t upload_status = mmio_region_read32(
683 spi->
dev.
base_addr, SPI_DEVICE_UPLOAD_STATUS_REG_OFFSET);
685 SPI_DEVICE_UPLOAD_STATUS_CMDFIFO_NOTEMPTY_BIT)) {
688 uint32_t cmd_item = mmio_region_read32(spi->
dev.
base_addr,
689 SPI_DEVICE_UPLOAD_CMDFIFO_REG_OFFSET);
691 cmd_item, SPI_DEVICE_UPLOAD_CMDFIFO_DATA_FIELD);
697 if (spi == NULL || address == NULL) {
700 uint32_t upload_status = mmio_region_read32(
701 spi->
dev.
base_addr, SPI_DEVICE_UPLOAD_STATUS_REG_OFFSET);
703 SPI_DEVICE_UPLOAD_STATUS_ADDRFIFO_NOTEMPTY_BIT)) {
707 SPI_DEVICE_UPLOAD_ADDRFIFO_REG_OFFSET);
713 ptrdiff_t buffer_offset;
716 static dif_result_t dif_spi_device_get_flash_buffer_info(
717 dif_spi_device_flash_buffer_type_t buffer_type,
719 switch (buffer_type) {
721 info->buffer_len = kDifSpiDeviceEFlashLen;
722 info->buffer_offset = kDifSpiDeviceEFlashOffset;
725 info->buffer_len = kDifSpiDeviceMailboxLen;
726 info->buffer_offset = kDifSpiDeviceMailboxOffset;
729 info->buffer_len = kDifSpiDeviceSfdpLen;
730 info->buffer_offset = kDifSpiDeviceSfdpOffset;
741 if (spi == NULL || buf == NULL) {
745 .buffer_len = kDifSpiDevicePayloadLen,
746 .buffer_offset = kDifSpiDevicePayloadOffset,
748 if (offset >= (info.buffer_offset + (ptrdiff_t)info.buffer_len) ||
749 length > (info.buffer_offset + (ptrdiff_t)info.buffer_len -
750 (ptrdiff_t)offset)) {
753 ptrdiff_t offset_from_base = SPI_DEVICE_INGRESS_BUFFER_REG_OFFSET +
754 info.buffer_offset + (ptrdiff_t)offset;
762 dif_spi_device_flash_buffer_type_t buffer_type, uint32_t offset,
763 size_t length,
const uint8_t *buf) {
764 if (spi == NULL || buf == NULL) {
769 dif_spi_device_get_flash_buffer_info(buffer_type, &info);
773 if (offset >= (info.buffer_offset + (ptrdiff_t)info.buffer_len) ||
774 length > (info.buffer_offset + (ptrdiff_t)info.buffer_len -
775 (ptrdiff_t)offset)) {
778 ptrdiff_t offset_from_base = SPI_DEVICE_EGRESS_BUFFER_REG_OFFSET +
779 info.buffer_offset + (ptrdiff_t)offset;
787 if (spi == NULL || enabled == NULL) {
790 ptrdiff_t reg_offset =
791 SPI_DEVICE_CMD_FILTER_0_REG_OFFSET + (command >> 5) *
sizeof(uint32_t);
792 uint32_t command_index = command & 0x1fu;
793 uint32_t reg_value = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
809 ptrdiff_t reg_offset =
810 SPI_DEVICE_CMD_FILTER_0_REG_OFFSET + (command >> 5) *
sizeof(uint32_t);
811 uint32_t command_index = command & 0x1fu;
812 uint32_t reg_value = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
814 mmio_region_write32(spi->
dev.
base_addr, reg_offset, reg_value);
824 for (
int i = 0; i < SPI_DEVICE_CMD_FILTER_MULTIREG_COUNT; i++) {
825 ptrdiff_t reg_offset =
826 SPI_DEVICE_CMD_FILTER_0_REG_OFFSET + i * (ptrdiff_t)
sizeof(uint32_t);
827 mmio_region_write32(spi->
dev.
base_addr, reg_offset, reg_value);
836 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr,
837 SPI_DEVICE_FLASH_STATUS_REG_OFFSET);
842 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_FLASH_STATUS_REG_OFFSET,
852 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_FLASH_STATUS_REG_OFFSET,
859 if (spi == NULL || value == NULL) {
863 SPI_DEVICE_FLASH_STATUS_REG_OFFSET);
869 if (spi == NULL || caps == NULL) {
873 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_TPM_CAP_REG_OFFSET);
879 reg_val, SPI_DEVICE_TPM_CAP_MAX_WR_SIZE_FIELD);
881 reg_val, SPI_DEVICE_TPM_CAP_MAX_RD_SIZE_FIELD);
916 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_TPM_CFG_REG_OFFSET,
923 if (spi == NULL ||
status == NULL) {
927 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_TPM_STATUS_REG_OFFSET);
940 if (spi == NULL || locality >= SPI_DEVICE_PARAM_NUM_LOCALITY) {
945 ptrdiff_t reg_offset = SPI_DEVICE_TPM_ACCESS_0_REG_OFFSET + (locality & 0xfc);
946 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
947 switch (locality & 0x03) {
950 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_0_FIELD, value);
954 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_1_FIELD, value);
958 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_2_FIELD, value);
962 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_3_FIELD, value);
967 mmio_region_write32(spi->
dev.
base_addr, reg_offset, reg_val);
974 if (spi == NULL || locality >= SPI_DEVICE_PARAM_NUM_LOCALITY ||
980 ptrdiff_t reg_offset = SPI_DEVICE_TPM_ACCESS_0_REG_OFFSET + (locality & 0xfc);
981 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
982 switch (locality & 0x03) {
985 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_0_FIELD);
989 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_1_FIELD);
993 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_2_FIELD);
997 reg_val, SPI_DEVICE_TPM_ACCESS_0_ACCESS_3_FIELD);
1014 uint32_t value, ptrdiff_t reg_offset) {
1018 mmio_region_write32(spi->
dev.
base_addr, reg_offset, value);
1031 uint32_t *value, ptrdiff_t reg_offset) {
1032 if (spi == NULL || value == NULL) {
1035 *value = mmio_region_read32(spi->
dev.
base_addr, reg_offset);
1041 return tpm_reg_write32(spi, value, SPI_DEVICE_TPM_STS_REG_OFFSET);
1046 return tpm_reg_read32(spi, value, SPI_DEVICE_TPM_STS_REG_OFFSET);
1051 return tpm_reg_write32(spi, value, SPI_DEVICE_TPM_INTF_CAPABILITY_REG_OFFSET);
1056 return tpm_reg_read32(spi, value, SPI_DEVICE_TPM_INTF_CAPABILITY_REG_OFFSET);
1061 return tpm_reg_write32(spi, value, SPI_DEVICE_TPM_INT_ENABLE_REG_OFFSET);
1066 return tpm_reg_read32(spi, value, SPI_DEVICE_TPM_INT_ENABLE_REG_OFFSET);
1071 return tpm_reg_write32(spi, value, SPI_DEVICE_TPM_INT_VECTOR_REG_OFFSET);
1076 return tpm_reg_read32(spi, value, SPI_DEVICE_TPM_INT_VECTOR_REG_OFFSET);
1081 return tpm_reg_write32(spi, value, SPI_DEVICE_TPM_INT_STATUS_REG_OFFSET);
1086 return tpm_reg_read32(spi, value, SPI_DEVICE_TPM_INT_STATUS_REG_OFFSET);
1099 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_TPM_DID_VID_REG_OFFSET,
1101 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_TPM_RID_REG_OFFSET,
1108 if (spi == NULL || value == NULL) {
1112 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_TPM_DID_VID_REG_OFFSET);
1114 mmio_region_read32(spi->
dev.
base_addr, SPI_DEVICE_TPM_RID_REG_OFFSET);
1116 did_vid, SPI_DEVICE_TPM_DID_VID_VID_FIELD);
1118 did_vid, SPI_DEVICE_TPM_DID_VID_DID_FIELD);
1126 uint32_t *address) {
1127 if (spi == NULL || command == NULL || address == NULL) {
1130 uint32_t reg_val = mmio_region_read32(spi->
dev.
base_addr,
1131 SPI_DEVICE_TPM_CMD_ADDR_REG_OFFSET);
1133 SPI_DEVICE_TPM_CMD_ADDR_CMD_FIELD);
1139 size_t length, uint8_t *buf) {
1140 if (spi == NULL || buf == NULL) {
1145 uint8_t offset = length & 0x3;
1146 uint32_t rdfifo_wdata;
1151 if (DIF_SPI_DEVICE_TPM_FIFO_DEPTH *
sizeof(uint32_t) < length) {
1154 for (
int i = 0; i < length; i += 4) {
1155 if (i + 4 > length) {
1158 for (
int j = 0; j <= offset; j++) {
1159 rdfifo_wdata |= (uint32_t)(buf[i + j]) << (8 * j);
1163 rdfifo_wdata = *((uint32_t *)buf + (i >> 2));
1165 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_TPM_READ_FIFO_REG_OFFSET,
1172 size_t length, uint8_t *buf) {
1173 if (spi == NULL || buf == NULL) {
1176 const uint32_t kOffset = 0;
1178 .buffer_len = kDifSpiDeviceTpmWriteFifoLen,
1179 .buffer_offset = kDifSpiDeviceTpmWriteFifoOffset,
1181 if (kOffset >= (kInfo.buffer_offset + (ptrdiff_t)kInfo.buffer_len) ||
1182 length > (kInfo.buffer_offset + (ptrdiff_t)kInfo.buffer_len -
1183 (ptrdiff_t)kOffset)) {
1186 ptrdiff_t offset_from_base = SPI_DEVICE_INGRESS_BUFFER_REG_OFFSET +
1187 kInfo.buffer_offset + (ptrdiff_t)kOffset;
1198 mmio_region_write32(spi->
dev.
base_addr, SPI_DEVICE_TPM_STATUS_REG_OFFSET, 0);