Lines Matching +full:read +full:- +full:cmd
5 * SPDX-License-Identifier: Apache-2.0
20 #include <zephyr/dt-bindings/flash_controller/ospi.h>
169 /* Read operation dummy cycles */
176 /* Table to hold the jedec Read ID given by the octoFlash or the DTS */
187 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_lock_thread()
189 k_sem_take(&dev_data->sem, K_FOREVER); in ospi_lock_thread()
194 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_unlock_thread()
196 k_sem_give(&dev_data->sem); in ospi_unlock_thread()
199 static int ospi_send_cmd(const struct device *dev, OSPI_RegularCmdTypeDef *cmd) in ospi_send_cmd() argument
201 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in ospi_send_cmd()
202 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_send_cmd()
205 LOG_DBG("Instruction 0x%x", cmd->Instruction); in ospi_send_cmd()
207 dev_data->cmd_status = 0; in ospi_send_cmd()
209 hal_ret = HAL_OSPI_Command(&dev_data->hospi, cmd, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in ospi_send_cmd()
212 return -EIO; in ospi_send_cmd()
214 LOG_DBG("CCR 0x%x", dev_cfg->regs->CCR); in ospi_send_cmd()
216 return dev_data->cmd_status; in ospi_send_cmd()
219 static int ospi_read_access(const struct device *dev, OSPI_RegularCmdTypeDef *cmd, in ospi_read_access() argument
222 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_read_access()
225 LOG_DBG("Instruction 0x%x", cmd->Instruction); in ospi_read_access()
227 cmd->NbData = size; in ospi_read_access()
229 dev_data->cmd_status = 0; in ospi_read_access()
231 hal_ret = HAL_OSPI_Command(&dev_data->hospi, cmd, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in ospi_read_access()
234 return -EIO; in ospi_read_access()
238 hal_ret = HAL_OSPI_Receive_DMA(&dev_data->hospi, data); in ospi_read_access()
240 hal_ret = HAL_OSPI_Receive_IT(&dev_data->hospi, data); in ospi_read_access()
243 LOG_ERR("%d: Failed to read data", hal_ret); in ospi_read_access()
244 return -EIO; in ospi_read_access()
247 k_sem_take(&dev_data->sync, K_FOREVER); in ospi_read_access()
249 return dev_data->cmd_status; in ospi_read_access()
252 static int ospi_write_access(const struct device *dev, OSPI_RegularCmdTypeDef *cmd, in ospi_write_access() argument
255 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in ospi_write_access()
256 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_write_access()
259 LOG_DBG("Instruction 0x%x", cmd->Instruction); in ospi_write_access()
261 cmd->NbData = size; in ospi_write_access()
263 dev_data->cmd_status = 0; in ospi_write_access()
265 /* in OPI/STR the 3-byte AddressSize is not supported by the NOR flash */ in ospi_write_access()
266 if ((dev_cfg->data_mode == OSPI_OPI_MODE) && in ospi_write_access()
267 (cmd->AddressSize != HAL_OSPI_ADDRESS_32_BITS)) { in ospi_write_access()
269 return -EIO; in ospi_write_access()
272 hal_ret = HAL_OSPI_Command(&dev_data->hospi, cmd, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in ospi_write_access()
275 return -EIO; in ospi_write_access()
279 hal_ret = HAL_OSPI_Transmit_DMA(&dev_data->hospi, (uint8_t *)data); in ospi_write_access()
281 hal_ret = HAL_OSPI_Transmit_IT(&dev_data->hospi, (uint8_t *)data); in ospi_write_access()
286 return -EIO; in ospi_write_access()
289 k_sem_take(&dev_data->sync, K_FOREVER); in ospi_write_access()
291 return dev_data->cmd_status; in ospi_write_access()
356 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_hal_address_size()
358 if (dev_data->address_width == 4U) { in stm32_ospi_hal_address_size()
367 * Read the JEDEC ID data from the octoFlash at init or DTS
372 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_read_jedec_id()
377 LOG_ERR("Read ID length is wrong (%d)", DT_INST_PROP_LEN(0, jedec_id)); in stm32_ospi_read_jedec_id()
378 return -EIO; in stm32_ospi_read_jedec_id()
381 /* The dev_data->jedec_id if filled from the DTS property */ in stm32_ospi_read_jedec_id()
384 OSPI_RegularCmdTypeDef cmd = ospi_prepare_cmd(OSPI_SPI_MODE, OSPI_STR_TRANSFER); in stm32_ospi_read_jedec_id() local
386 cmd.Instruction = JESD216_CMD_READ_ID; in stm32_ospi_read_jedec_id()
387 cmd.AddressSize = stm32_ospi_hal_address_size(dev); in stm32_ospi_read_jedec_id()
388 cmd.AddressMode = HAL_OSPI_ADDRESS_NONE; in stm32_ospi_read_jedec_id()
389 cmd.NbData = JESD216_READ_ID_LEN; /* 3 bytes in the READ ID */ in stm32_ospi_read_jedec_id()
393 hal_ret = HAL_OSPI_Command(&dev_data->hospi, &cmd, in stm32_ospi_read_jedec_id()
398 return -EIO; in stm32_ospi_read_jedec_id()
402 hal_ret = HAL_OSPI_Receive(&dev_data->hospi, dev_data->jedec_id, in stm32_ospi_read_jedec_id()
405 LOG_ERR("%d: Failed to read data", hal_ret); in stm32_ospi_read_jedec_id()
406 return -EIO; in stm32_ospi_read_jedec_id()
410 dev_data->jedec_id[0], dev_data->jedec_id[1], dev_data->jedec_id[2]); in stm32_ospi_read_jedec_id()
412 dev_data->cmd_status = 0; in stm32_ospi_read_jedec_id()
418 * Read Serial Flash ID :
423 struct flash_stm32_ospi_data *dev_data = dev->data; in ospi_read_jedec_id()
426 memcpy(id, dev_data->jedec_id, JESD216_READ_ID_LEN); in ospi_read_jedec_id()
437 * Read Serial Flash Discovery Parameter from the octoFlash at init :
438 * perform a read access over SPI bus for SDFP (DataMode is already set)
444 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in stm32_ospi_read_sfdp()
445 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_read_sfdp()
447 OSPI_RegularCmdTypeDef cmd = ospi_prepare_cmd(dev_cfg->data_mode, in stm32_ospi_read_sfdp() local
448 dev_cfg->data_rate); in stm32_ospi_read_sfdp()
449 if (dev_cfg->data_mode == OSPI_OPI_MODE) { in stm32_ospi_read_sfdp()
450 cmd.Instruction = JESD216_OCMD_READ_SFDP; in stm32_ospi_read_sfdp()
451 cmd.DummyCycles = 20U; in stm32_ospi_read_sfdp()
452 cmd.AddressSize = HAL_OSPI_ADDRESS_32_BITS; in stm32_ospi_read_sfdp()
454 cmd.Instruction = JESD216_CMD_READ_SFDP; in stm32_ospi_read_sfdp()
455 cmd.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; in stm32_ospi_read_sfdp()
456 cmd.DataMode = HAL_OSPI_DATA_1_LINE; in stm32_ospi_read_sfdp()
457 cmd.AddressMode = HAL_OSPI_ADDRESS_1_LINE; in stm32_ospi_read_sfdp()
458 cmd.DummyCycles = 8U; in stm32_ospi_read_sfdp()
459 cmd.AddressSize = HAL_OSPI_ADDRESS_24_BITS; in stm32_ospi_read_sfdp()
461 cmd.Address = addr; in stm32_ospi_read_sfdp()
462 cmd.NbData = size; in stm32_ospi_read_sfdp()
466 hal_ret = HAL_OSPI_Command(&dev_data->hospi, &cmd, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in stm32_ospi_read_sfdp()
469 return -EIO; in stm32_ospi_read_sfdp()
472 hal_ret = HAL_OSPI_Receive(&dev_data->hospi, (uint8_t *)data, in stm32_ospi_read_sfdp()
475 LOG_ERR("%d: Failed to read data", hal_ret); in stm32_ospi_read_sfdp()
476 return -EIO; in stm32_ospi_read_sfdp()
479 dev_data->cmd_status = 0; in stm32_ospi_read_sfdp()
486 * Read Serial Flash Discovery Parameter :
487 * perform a read access over SPI bus for SDFP (DataMode is already set)
494 /* There is a sfdp-bfp property in the deviceTree : do not read the flash */ in ospi_read_sfdp()
495 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in ospi_read_sfdp()
497 LOG_INF("Read SFDP from DTS property"); in ospi_read_sfdp()
501 return -EIO; in ospi_read_sfdp()
503 /* The dev_cfg->sfdp_bfp if filled from the DTS property */ in ospi_read_sfdp()
504 memcpy(data, dev_cfg->sfdp_bfp + addr, size); in ospi_read_sfdp()
508 LOG_INF("Read SFDP from octoFlash"); in ospi_read_sfdp()
509 /* Get the SFDP from the octoFlash (no sfdp-bfp table in the DeviceTree) */ in ospi_read_sfdp()
515 return -EINVAL; in ospi_read_sfdp()
522 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in ospi_address_is_valid()
523 size_t flash_size = dev_cfg->flash_size; in ospi_address_is_valid()
531 dev_data->cmd_status = 0; in stm32_ospi_wait_auto_polling()
532 if (HAL_OSPI_AutoPolling_IT(&dev_data->hospi, s_config) != HAL_OK) { in stm32_ospi_wait_auto_polling()
534 return -EIO; in stm32_ospi_wait_auto_polling()
537 if (k_sem_take(&dev_data->sync, K_MSEC(timeout_ms)) != 0) { in stm32_ospi_wait_auto_polling()
539 HAL_OSPI_Abort(&dev_data->hospi); in stm32_ospi_wait_auto_polling()
540 k_sem_reset(&dev_data->sync); in stm32_ospi_wait_auto_polling()
541 return -EIO; in stm32_ospi_wait_auto_polling()
547 return dev_data->cmd_status; in stm32_ospi_wait_auto_polling()
558 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in stm32_ospi_mem_erased()
559 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_mem_erased()
560 uint8_t nor_mode = dev_cfg->data_mode; in stm32_ospi_mem_erased()
561 uint8_t nor_rate = dev_cfg->data_rate; in stm32_ospi_mem_erased()
562 OSPI_HandleTypeDef *hospi = &dev_data->hospi; in stm32_ospi_mem_erased()
574 /* force 1-line InstructionMode for any non-OSPI transfer */ in stm32_ospi_mem_erased()
577 /* force 1-line DataMode for any non-OSPI transfer */ in stm32_ospi_mem_erased()
595 return -EIO; in stm32_ospi_mem_erased()
598 /* Start Automatic-Polling mode to wait until the memory is totally erased */ in stm32_ospi_mem_erased()
611 OSPI_HandleTypeDef *hospi = &dev_data->hospi; in stm32_ospi_mem_ready()
623 /* force 1-line InstructionMode for any non-OSPI transfer */ in stm32_ospi_mem_ready()
626 /* force 1-line DataMode for any non-OSPI transfer */ in stm32_ospi_mem_ready()
643 return -EIO; in stm32_ospi_mem_ready()
646 /* Start Automatic-Polling mode to wait until the memory is ready WIP=0 */ in stm32_ospi_mem_ready()
654 OSPI_HandleTypeDef *hospi = &dev_data->hospi; in stm32_ospi_write_enable()
663 /* force 1-line InstructionMode for any non-OSPI transfer */ in stm32_ospi_write_enable()
671 LOG_ERR("OSPI flash write enable cmd failed"); in stm32_ospi_write_enable()
672 return -EIO; in stm32_ospi_write_enable()
685 /* force 1-line DataMode for any non-OSPI transfer */ in stm32_ospi_write_enable()
697 LOG_ERR("OSPI config auto polling cmd failed"); in stm32_ospi_write_enable()
698 return -EIO; in stm32_ospi_write_enable()
729 return -EIO; in stm32_ospi_write_cfg2reg_dummy()
735 return -EIO; in stm32_ospi_write_cfg2reg_dummy()
759 return -EIO; in stm32_ospi_write_cfg2reg_io()
765 return -EIO; in stm32_ospi_write_cfg2reg_io()
771 /* Read Flash configuration register 2 with new single or octal SPI protocol */
791 return -EIO; in stm32_ospi_read_cfg2reg()
796 return -EIO; in stm32_ospi_read_cfg2reg()
805 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in stm32_ospi_config_mem()
806 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_config_mem()
810 if ((dev_cfg->data_mode != OSPI_OPI_MODE) in stm32_ospi_config_mem()
811 && (dev_cfg->data_rate == OSPI_STR_TRANSFER)) { in stm32_ospi_config_mem()
822 return -EIO; in stm32_ospi_config_mem()
826 if (stm32_ospi_write_cfg2reg_dummy(&dev_data->hospi, in stm32_ospi_config_mem()
829 return -EIO; in stm32_ospi_config_mem()
834 return -EIO; in stm32_ospi_config_mem()
839 return -EIO; in stm32_ospi_config_mem()
843 uint8_t mode_enable = ((dev_cfg->data_rate == OSPI_DTR_TRANSFER) in stm32_ospi_config_mem()
846 if (stm32_ospi_write_cfg2reg_io(&dev_data->hospi, in stm32_ospi_config_mem()
849 return -EIO; in stm32_ospi_config_mem()
856 dev_data->hospi.Init.MemoryType = HAL_OSPI_MEMTYPE_MACRONIX; in stm32_ospi_config_mem()
857 dev_data->hospi.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_ENABLE; in stm32_ospi_config_mem()
858 if (HAL_OSPI_Init(&dev_data->hospi) != HAL_OK) { in stm32_ospi_config_mem()
860 return -EIO; in stm32_ospi_config_mem()
863 if (dev_cfg->data_rate == OSPI_STR_TRANSFER) { in stm32_ospi_config_mem()
868 return -EIO; in stm32_ospi_config_mem()
871 if (stm32_ospi_read_cfg2reg(&dev_data->hospi, in stm32_ospi_config_mem()
874 LOG_ERR("OSPI flash config read failed"); in stm32_ospi_config_mem()
875 return -EIO; in stm32_ospi_config_mem()
881 if (dev_cfg->data_rate == OSPI_DTR_TRANSFER) { in stm32_ospi_config_mem()
886 return -EIO; in stm32_ospi_config_mem()
898 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_mem_reset()
901 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in stm32_ospi_mem_reset()
904 gpio_pin_configure_dt(&dev_cfg->reset, GPIO_OUTPUT_ACTIVE); in stm32_ospi_mem_reset()
906 gpio_pin_set_dt(&dev_cfg->reset, 0); in stm32_ospi_mem_reset()
926 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
929 return -EIO; in stm32_ospi_mem_reset()
934 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
937 return -EIO; in stm32_ospi_mem_reset()
945 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
948 return -EIO; in stm32_ospi_mem_reset()
953 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
956 return -EIO; in stm32_ospi_mem_reset()
962 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
965 return -EIO; in stm32_ospi_mem_reset()
970 if (HAL_OSPI_Command(&dev_data->hospi, in stm32_ospi_mem_reset()
973 return -EIO; in stm32_ospi_mem_reset()
977 /* Wait after SWreset CMD, in case SWReset occurred during erase operation */ in stm32_ospi_mem_reset()
988 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in stm32_ospi_set_memorymap()
989 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_set_memorymap()
990 OSPI_RegularCmdTypeDef s_command = ospi_prepare_cmd(dev_cfg->data_mode, dev_cfg->data_rate); in stm32_ospi_set_memorymap()
994 if ((dev_cfg->data_mode == OSPI_SPI_MODE) && in stm32_ospi_set_memorymap()
996 /* OPI mode and 3-bytes address size not supported by memory */ in stm32_ospi_set_memorymap()
998 return -ENOTSUP; in stm32_ospi_set_memorymap()
1001 /* Initialize the read command */ in stm32_ospi_set_memorymap()
1003 s_command.AddressSize = (dev_cfg->data_rate == OSPI_STR_TRANSFER) in stm32_ospi_set_memorymap()
1007 if (dev_cfg->data_mode != OSPI_OPI_MODE) { in stm32_ospi_set_memorymap()
1008 switch (dev_data->read_mode) { in stm32_ospi_set_memorymap()
1035 if (dev_cfg->data_rate == OSPI_DTR_TRANSFER) { in stm32_ospi_set_memorymap()
1041 if (dev_cfg->data_mode == OSPI_OPI_MODE) { in stm32_ospi_set_memorymap()
1046 /* use SFDP:BFP read instruction */ in stm32_ospi_set_memorymap()
1047 s_command.Instruction = dev_data->read_opcode; in stm32_ospi_set_memorymap()
1048 s_command.DummyCycles = dev_data->read_dummy; in stm32_ospi_set_memorymap()
1052 ret = HAL_OSPI_Command(&dev_data->hospi, &s_command, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in stm32_ospi_set_memorymap()
1054 LOG_ERR("%d: Failed to set memory map read cmd", ret); in stm32_ospi_set_memorymap()
1055 return -EIO; in stm32_ospi_set_memorymap()
1062 s_command.Instruction = dev_data->write_opcode; in stm32_ospi_set_memorymap()
1097 ret = HAL_OSPI_Command(&dev_data->hospi, &s_command, HAL_OSPI_TIMEOUT_DEFAULT_VALUE); in stm32_ospi_set_memorymap()
1099 LOG_ERR("%d: Failed to set memory map write cmd", ret); in stm32_ospi_set_memorymap()
1100 return -EIO; in stm32_ospi_set_memorymap()
1103 /* Enable the memory-mapping */ in stm32_ospi_set_memorymap()
1106 ret = HAL_OSPI_MemoryMapped(&dev_data->hospi, &s_MemMappedCfg); in stm32_ospi_set_memorymap()
1109 return -EIO; in stm32_ospi_set_memorymap()
1119 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_is_memorymap()
1121 return ((READ_BIT(dev_data->hospi.Instance->CR, in stm32_ospi_is_memorymap()
1128 struct flash_stm32_ospi_data *dev_data = dev->data; in stm32_ospi_abort()
1131 hal_ret = HAL_OSPI_Abort(&dev_data->hospi); in stm32_ospi_abort()
1134 return -EIO; in stm32_ospi_abort()
1151 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in flash_stm32_ospi_erase()
1152 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_erase()
1161 if (size > dev_cfg->flash_size) { in flash_stm32_ospi_erase()
1162 size = dev_cfg->flash_size; in flash_stm32_ospi_erase()
1168 return -EINVAL; in flash_stm32_ospi_erase()
1171 if (((size % SPI_NOR_SECTOR_SIZE) != 0) && (size < dev_cfg->flash_size)) { in flash_stm32_ospi_erase()
1173 return -ENOTSUP; in flash_stm32_ospi_erase()
1183 LOG_ERR("Failed to abort memory-mapped access before erase"); in flash_stm32_ospi_erase()
1201 dev_cfg->data_mode, dev_cfg->data_rate) != 0) { in flash_stm32_ospi_erase()
1203 ret = -EBUSY; in flash_stm32_ospi_erase()
1207 cmd_erase.InstructionMode = (dev_cfg->data_mode == OSPI_OPI_MODE) in flash_stm32_ospi_erase()
1210 cmd_erase.InstructionDtrMode = (dev_cfg->data_rate == OSPI_DTR_TRANSFER) in flash_stm32_ospi_erase()
1213 cmd_erase.InstructionSize = (dev_cfg->data_mode == OSPI_OPI_MODE) in flash_stm32_ospi_erase()
1220 dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_erase()
1226 if (size == dev_cfg->flash_size) { in flash_stm32_ospi_erase()
1231 cmd_erase.Instruction = (dev_cfg->data_mode == OSPI_OPI_MODE) in flash_stm32_ospi_erase()
1238 size -= dev_cfg->flash_size; in flash_stm32_ospi_erase()
1250 (dev_cfg->data_mode == OSPI_OPI_MODE) in flash_stm32_ospi_erase()
1254 (dev_cfg->data_rate == OSPI_DTR_TRANSFER) in flash_stm32_ospi_erase()
1261 dev_data->erase_types; in flash_stm32_ospi_erase()
1269 if ((etp->exp != 0) in flash_stm32_ospi_erase()
1270 && SPI_NOR_IS_ALIGNED(addr, etp->exp) in flash_stm32_ospi_erase()
1271 && (size >= BIT(etp->exp)) in flash_stm32_ospi_erase()
1273 || (etp->exp > bet->exp))) { in flash_stm32_ospi_erase()
1275 cmd_erase.Instruction = bet->cmd; in flash_stm32_ospi_erase()
1277 /* Use the default sector erase cmd */ in flash_stm32_ospi_erase()
1278 if (dev_cfg->data_mode == OSPI_OPI_MODE) { in flash_stm32_ospi_erase()
1300 addr += BIT(bet->exp); in flash_stm32_ospi_erase()
1301 size -= BIT(bet->exp); in flash_stm32_ospi_erase()
1304 size -= SPI_NOR_SECTOR_SIZE; in flash_stm32_ospi_erase()
1307 ret = stm32_ospi_mem_ready(dev_data, dev_cfg->data_mode, in flash_stm32_ospi_erase()
1308 dev_cfg->data_rate); in flash_stm32_ospi_erase()
1319 /* Function to read the flash with possible OSPI/SPI and STR/DTR */
1328 return -EINVAL; in flash_stm32_ospi_read()
1331 /* Ignore zero size read */ in flash_stm32_ospi_read()
1340 LOG_ERR("READ failed: cannot enable MemoryMap"); in flash_stm32_ospi_read()
1341 return -EIO; in flash_stm32_ospi_read()
1344 /* Now in MemMapped mode : read with memcopy */ in flash_stm32_ospi_read()
1345 LOG_DBG("MemoryMapped Read offset: 0x%lx, len: %zu", in flash_stm32_ospi_read()
1351 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in flash_stm32_ospi_read()
1352 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_read()
1355 OSPI_RegularCmdTypeDef cmd = ospi_prepare_cmd(dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_read() local
1357 if (dev_cfg->data_mode != OSPI_OPI_MODE) { in flash_stm32_ospi_read()
1358 switch (dev_data->read_mode) { in flash_stm32_ospi_read()
1360 cmd.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; in flash_stm32_ospi_read()
1361 cmd.AddressMode = HAL_OSPI_ADDRESS_1_LINE; in flash_stm32_ospi_read()
1362 cmd.DataMode = HAL_OSPI_DATA_2_LINES; in flash_stm32_ospi_read()
1366 cmd.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; in flash_stm32_ospi_read()
1367 cmd.AddressMode = HAL_OSPI_ADDRESS_2_LINES; in flash_stm32_ospi_read()
1368 cmd.DataMode = HAL_OSPI_DATA_2_LINES; in flash_stm32_ospi_read()
1372 cmd.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; in flash_stm32_ospi_read()
1373 cmd.AddressMode = HAL_OSPI_ADDRESS_1_LINE; in flash_stm32_ospi_read()
1374 cmd.DataMode = HAL_OSPI_DATA_4_LINES; in flash_stm32_ospi_read()
1378 cmd.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; in flash_stm32_ospi_read()
1379 cmd.AddressMode = HAL_OSPI_ADDRESS_4_LINES; in flash_stm32_ospi_read()
1380 cmd.DataMode = HAL_OSPI_DATA_4_LINES; in flash_stm32_ospi_read()
1390 cmd.Address = addr; /* AddressSize is 32bits in OPSI mode */ in flash_stm32_ospi_read()
1391 cmd.AddressSize = stm32_ospi_hal_address_size(dev); in flash_stm32_ospi_read()
1392 /* DataSize is set by the read cmd */ in flash_stm32_ospi_read()
1395 if (dev_cfg->data_rate == OSPI_DTR_TRANSFER) { in flash_stm32_ospi_read()
1397 cmd.Instruction = SPI_NOR_OCMD_DTR_RD; in flash_stm32_ospi_read()
1398 cmd.DummyCycles = SPI_NOR_DUMMY_RD_OCTAL_DTR; in flash_stm32_ospi_read()
1401 if (dev_cfg->data_mode == OSPI_OPI_MODE) { in flash_stm32_ospi_read()
1403 cmd.Instruction = SPI_NOR_OCMD_RD; in flash_stm32_ospi_read()
1404 cmd.DummyCycles = SPI_NOR_DUMMY_RD_OCTAL; in flash_stm32_ospi_read()
1406 /* use SFDP:BFP read instruction */ in flash_stm32_ospi_read()
1407 cmd.Instruction = dev_data->read_opcode; in flash_stm32_ospi_read()
1408 cmd.DummyCycles = dev_data->read_dummy; in flash_stm32_ospi_read()
1413 LOG_DBG("OSPI: read %zu data", size); in flash_stm32_ospi_read()
1416 ret = ospi_read_access(dev, &cmd, data, size); in flash_stm32_ospi_read()
1431 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in flash_stm32_ospi_write()
1432 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_write()
1439 return -EINVAL; in flash_stm32_ospi_write()
1454 LOG_ERR("Failed to abort memory-mapped access before write"); in flash_stm32_ospi_write()
1461 OSPI_RegularCmdTypeDef cmd_pp = ospi_prepare_cmd(dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_write()
1464 cmd_pp.Instruction = dev_data->write_opcode; in flash_stm32_ospi_write()
1506 dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_write()
1510 return -EIO; in flash_stm32_ospi_write()
1516 dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_write()
1527 if (((addr + to_write - 1U) / SPI_NOR_PAGE_SIZE) in flash_stm32_ospi_write()
1529 to_write = SPI_NOR_PAGE_SIZE - in flash_stm32_ospi_write()
1540 size -= to_write; in flash_stm32_ospi_write()
1546 dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_write()
1575 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_isr()
1577 HAL_OSPI_IRQHandler(&dev_data->hospi); in flash_stm32_ospi_isr()
1621 dev_data->cmd_status = -EIO; in HAL_OSPI_ErrorCallback()
1623 k_sem_give(&dev_data->sync); in HAL_OSPI_ErrorCallback()
1634 LOG_DBG("Cmd Cplt cb"); in HAL_OSPI_CmdCpltCallback()
1636 k_sem_give(&dev_data->sync); in HAL_OSPI_CmdCpltCallback()
1649 k_sem_give(&dev_data->sync); in HAL_OSPI_RxCpltCallback()
1662 k_sem_give(&dev_data->sync); in HAL_OSPI_TxCpltCallback()
1675 k_sem_give(&dev_data->sync); in HAL_OSPI_StatusMatchCallback()
1688 dev_data->cmd_status = -EIO; in HAL_OSPI_TimeOutCallback()
1690 k_sem_give(&dev_data->sync); in HAL_OSPI_TimeOutCallback()
1698 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_pages_layout()
1700 *layout = &dev_data->layout; in flash_stm32_ospi_pages_layout()
1706 .read = flash_stm32_ospi_read,
1722 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in setup_pages_layout()
1723 struct flash_stm32_ospi_data *data = dev->data; in setup_pages_layout()
1724 const size_t flash_size = dev_cfg->flash_size; in setup_pages_layout()
1725 uint32_t layout_page_size = data->page_size; in setup_pages_layout()
1730 for (size_t i = 0; i < ARRAY_SIZE(data->erase_types); ++i) { in setup_pages_layout()
1731 const struct jesd216_erase_type *etp = &data->erase_types[i]; in setup_pages_layout()
1733 if ((etp->cmd != 0) in setup_pages_layout()
1734 && ((value == 0) || (etp->exp < value))) { in setup_pages_layout()
1735 value = etp->exp; in setup_pages_layout()
1761 data->layout.pages_size = layout_page_size; in setup_pages_layout()
1762 data->layout.pages_count = flash_size / layout_page_size; in setup_pages_layout()
1763 LOG_DBG("layout %u x %u By pages", data->layout.pages_count, in setup_pages_layout()
1764 data->layout.pages_size); in setup_pages_layout()
1788 return -EINVAL; in stm32_ospi_read_status_register()
1796 struct flash_stm32_ospi_data *data = dev->data; in stm32_ospi_write_status_register()
1812 if (data->qer_type == JESD216_DW15_QER_S2B1v1) { in stm32_ospi_write_status_register()
1825 if ((data->qer_type == JESD216_DW15_QER_VAL_S2B1v1) || in stm32_ospi_write_status_register()
1826 (data->qer_type == JESD216_DW15_QER_VAL_S2B1v4) || in stm32_ospi_write_status_register()
1827 (data->qer_type == JESD216_DW15_QER_VAL_S2B1v5)) { in stm32_ospi_write_status_register()
1842 return -EINVAL; in stm32_ospi_write_status_register()
1850 struct flash_stm32_ospi_data *data = dev->data; in stm32_ospi_enable_qe()
1856 switch (data->qer_type) { in stm32_ospi_enable_qe()
1879 return -ENOTSUP; in stm32_ospi_enable_qe()
1917 ret = -EIO; in stm32_ospi_enable_qe()
1926 struct flash_stm32_ospi_data *data = dev->data; in spi_nor_process_bfp_addrbytes()
1930 data->address_width = 4U; in spi_nor_process_bfp_addrbytes()
1932 data->address_width = 3U; in spi_nor_process_bfp_addrbytes()
1974 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in spi_nor_process_bfp()
1975 struct flash_stm32_ospi_data *data = dev->data; in spi_nor_process_bfp()
1976 /* must be kept in data mode order, ignore 1-1-1 (always supported) */ in spi_nor_process_bfp()
1981 struct jesd216_erase_type *etp = data->erase_types; in spi_nor_process_bfp()
1987 if (flash_size != dev_cfg->flash_size) { in spi_nor_process_bfp()
1991 LOG_DBG("%s: %u MiBy flash", dev->name, (uint32_t)(flash_size >> 20)); in spi_nor_process_bfp()
1996 memset(data->erase_types, 0, sizeof(data->erase_types)); in spi_nor_process_bfp()
1997 for (idx = 1U; idx <= ARRAY_SIZE(data->erase_types); ++idx) { in spi_nor_process_bfp()
2000 (uint32_t)BIT(etp->exp), etp->cmd); in spi_nor_process_bfp()
2006 LOG_DBG("Address width: %u Bytes", data->address_width); in spi_nor_process_bfp()
2009 if (data->write_opcode == SPI_NOR_WRITEOC_NONE) { in spi_nor_process_bfp()
2010 switch (dev_cfg->data_mode) { in spi_nor_process_bfp()
2012 data->write_opcode = SPI_NOR_OCMD_PAGE_PRG; in spi_nor_process_bfp()
2015 data->write_opcode = SPI_NOR_CMD_PP_1_4_4; in spi_nor_process_bfp()
2018 data->write_opcode = SPI_NOR_CMD_PP_1_1_2; in spi_nor_process_bfp()
2021 data->write_opcode = SPI_NOR_CMD_PP; in spi_nor_process_bfp()
2026 if (dev_cfg->data_mode != OSPI_OPI_MODE) { in spi_nor_process_bfp()
2027 /* determine supported read modes, begin from the slowest */ in spi_nor_process_bfp()
2028 data->read_mode = JESD216_MODE_111; in spi_nor_process_bfp()
2029 data->read_opcode = SPI_NOR_CMD_READ; in spi_nor_process_bfp()
2030 data->read_dummy = 0U; in spi_nor_process_bfp()
2032 if (dev_cfg->data_mode != OSPI_SPI_MODE) { in spi_nor_process_bfp()
2033 if (dev_cfg->data_mode == OSPI_DUAL_MODE) { in spi_nor_process_bfp()
2047 LOG_DBG("Supports read mode: %d, instr: 0x%X", in spi_nor_process_bfp()
2049 data->read_mode = supported_read_modes[idx]; in spi_nor_process_bfp()
2050 data->read_opcode = read_instr.instr; in spi_nor_process_bfp()
2051 data->read_dummy = in spi_nor_process_bfp()
2056 /* convert 3-Byte opcodes to 4-Byte (if required) */ in spi_nor_process_bfp()
2058 if (data->address_width != 4U) { in spi_nor_process_bfp()
2059 LOG_DBG("4-Byte opcodes require 4-Byte address width"); in spi_nor_process_bfp()
2060 return -ENOTSUP; in spi_nor_process_bfp()
2062 data->read_opcode = spi_nor_convert_read_to_4b(data->read_opcode); in spi_nor_process_bfp()
2063 data->write_opcode = spi_nor_convert_write_to_4b(data->write_opcode); in spi_nor_process_bfp()
2067 if (dev_cfg->data_mode == OSPI_QUAD_MODE) { in spi_nor_process_bfp()
2073 data->qer_type = dw15.qer; in spi_nor_process_bfp()
2076 LOG_DBG("QE requirement mode: %x", data->qer_type); in spi_nor_process_bfp()
2080 return -EIO; in spi_nor_process_bfp()
2087 data->page_size = jesd216_bfp_page_size(php, bfp); in spi_nor_process_bfp()
2089 LOG_DBG("Page size %u bytes", data->page_size); in spi_nor_process_bfp()
2091 LOG_DBG("Using read mode: %d, instr: 0x%X, dummy cycles: %u", in spi_nor_process_bfp()
2092 data->read_mode, data->read_opcode, data->read_dummy); in spi_nor_process_bfp()
2093 LOG_DBG("Using write instr: 0x%X", data->write_opcode); in spi_nor_process_bfp()
2100 const struct flash_stm32_ospi_config *dev_cfg = dev->config; in flash_stm32_ospi_init()
2101 struct flash_stm32_ospi_data *dev_data = dev->data; in flash_stm32_ospi_init()
2107 if ((dev_cfg->data_mode != OSPI_OPI_MODE) in flash_stm32_ospi_init()
2108 && (dev_cfg->data_rate == OSPI_DTR_TRANSFER)) { in flash_stm32_ospi_init()
2111 return -ENOTSUP; in flash_stm32_ospi_init()
2115 ret = pinctrl_apply_state(dev_cfg->pcfg, PINCTRL_STATE_DEFAULT); in flash_stm32_ospi_init()
2123 return -ENODEV; in flash_stm32_ospi_init()
2131 dev_data->hospi.State = HAL_OSPI_STATE_BUSY_MEM_MAPPED; in flash_stm32_ospi_init()
2145 struct dma_config dma_cfg = dev_data->dma.cfg; in flash_stm32_ospi_init()
2148 if (!device_is_ready(dev_data->dma.dev)) { in flash_stm32_ospi_init()
2149 LOG_ERR("%s device not ready", dev_data->dma.dev->name); in flash_stm32_ospi_init()
2150 return -ENODEV; in flash_stm32_ospi_init()
2157 /* Because of the STREAM OFFSET, the DMA channel given here is from 1 - 8 */ in flash_stm32_ospi_init()
2158 ret = dma_config(dev_data->dma.dev, in flash_stm32_ospi_init()
2159 (dev_data->dma.channel + STM32_DMA_STREAM_OFFSET), &dma_cfg); in flash_stm32_ospi_init()
2162 dev_data->dma.channel + STM32_DMA_STREAM_OFFSET); in flash_stm32_ospi_init()
2169 return -EINVAL; in flash_stm32_ospi_init()
2172 int index = find_lsb_set(dma_cfg.source_data_size) - 1; in flash_stm32_ospi_init()
2197 hdma.Instance = __LL_DMA_GET_STREAM_INSTANCE(dev_data->dma.reg, in flash_stm32_ospi_init()
2198 dev_data->dma.channel); in flash_stm32_ospi_init()
2202 hdma.Instance = LL_DMA_GET_CHANNEL_INSTANCE(dev_data->dma.reg, in flash_stm32_ospi_init()
2203 dev_data->dma.channel); in flash_stm32_ospi_init()
2209 hdma.Instance = __LL_DMA_GET_CHANNEL_INSTANCE(dev_data->dma.reg, in flash_stm32_ospi_init()
2210 dev_data->dma.channel); in flash_stm32_ospi_init()
2212 hdma.Instance = __LL_DMA_GET_CHANNEL_INSTANCE(dev_data->dma.reg, in flash_stm32_ospi_init()
2213 dev_data->dma.channel-1); in flash_stm32_ospi_init()
2218 __HAL_LINKDMA(&dev_data->hospi, hdma, hdma); in flash_stm32_ospi_init()
2221 return -EIO; in flash_stm32_ospi_init()
2229 (clock_control_subsys_t) &dev_cfg->pclken) != 0) { in flash_stm32_ospi_init()
2231 return -EIO; in flash_stm32_ospi_init()
2236 (clock_control_subsys_t) &dev_cfg->pclken_ker, in flash_stm32_ospi_init()
2239 return -EIO; in flash_stm32_ospi_init()
2242 (clock_control_subsys_t) &dev_cfg->pclken_ker, in flash_stm32_ospi_init()
2245 return -EIO; in flash_stm32_ospi_init()
2249 (clock_control_subsys_t) &dev_cfg->pclken, in flash_stm32_ospi_init()
2252 return -EIO; in flash_stm32_ospi_init()
2257 (clock_control_subsys_t) &dev_cfg->pclken_mgr) != 0) { in flash_stm32_ospi_init()
2259 return -EIO; in flash_stm32_ospi_init()
2266 if (clk <= dev_cfg->max_frequency) { in flash_stm32_ospi_init()
2274 dev_data->hospi.Init.FifoThreshold = 4; in flash_stm32_ospi_init()
2275 dev_data->hospi.Init.ClockPrescaler = prescaler; in flash_stm32_ospi_init()
2278 dev_data->hospi.Init.DeviceSize = find_lsb_set(dev_cfg->flash_size) - 2; in flash_stm32_ospi_init()
2281 dev_data->hospi.Init.DeviceSize = find_lsb_set(dev_cfg->flash_size) - 1; in flash_stm32_ospi_init()
2283 dev_data->hospi.Init.DualQuad = HAL_OSPI_DUALQUAD_DISABLE; in flash_stm32_ospi_init()
2284 dev_data->hospi.Init.ChipSelectHighTime = 2; in flash_stm32_ospi_init()
2285 dev_data->hospi.Init.FreeRunningClock = HAL_OSPI_FREERUNCLK_DISABLE; in flash_stm32_ospi_init()
2286 dev_data->hospi.Init.ClockMode = HAL_OSPI_CLOCK_MODE_0; in flash_stm32_ospi_init()
2288 dev_data->hospi.Init.WrapSize = HAL_OSPI_WRAP_NOT_SUPPORTED; in flash_stm32_ospi_init()
2291 if (dev_cfg->data_rate == OSPI_DTR_TRANSFER) { in flash_stm32_ospi_init()
2292 dev_data->hospi.Init.MemoryType = HAL_OSPI_MEMTYPE_MACRONIX; in flash_stm32_ospi_init()
2293 dev_data->hospi.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_ENABLE; in flash_stm32_ospi_init()
2295 dev_data->hospi.Init.MemoryType = HAL_OSPI_MEMTYPE_MICRON; in flash_stm32_ospi_init()
2296 dev_data->hospi.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_DISABLE; in flash_stm32_ospi_init()
2298 dev_data->hospi.Init.ChipSelectBoundary = 0; in flash_stm32_ospi_init()
2300 dev_data->hospi.Init.DelayBlockBypass = HAL_OSPI_DELAY_BLOCK_BYPASSED; in flash_stm32_ospi_init()
2302 dev_data->hospi.Init.DelayBlockBypass = HAL_OSPI_DELAY_BLOCK_USED; in flash_stm32_ospi_init()
2305 dev_data->hospi.Init.Refresh = 0; in flash_stm32_ospi_init()
2308 if (HAL_OSPI_Init(&dev_data->hospi) != HAL_OK) { in flash_stm32_ospi_init()
2310 return -EIO; in flash_stm32_ospi_init()
2319 if (dev_data->hospi.Instance == OCTOSPI1) { in flash_stm32_ospi_init()
2327 } else if (dev_data->hospi.Instance == OCTOSPI2) { in flash_stm32_ospi_init()
2339 if (HAL_OSPIM_Config(&dev_data->hospi, &ospi_mgr_cfg, in flash_stm32_ospi_init()
2342 return -EIO; in flash_stm32_ospi_init()
2350 if (HAL_OSPI_DLYB_SetConfig(&dev_data->hospi, &ospi_delay_block_cfg) != HAL_OK) { in flash_stm32_ospi_init()
2352 return -EIO; in flash_stm32_ospi_init()
2362 (void)HAL_XSPI_DLYB_GetClockPeriod(&dev_data->hospi, &xspi_delay_block_cfg); in flash_stm32_ospi_init()
2366 if (HAL_XSPI_DLYB_SetConfig(&dev_data->hospi, &xspi_delay_block_cfg) != HAL_OK) { in flash_stm32_ospi_init()
2368 return -EIO; in flash_stm32_ospi_init()
2376 k_sem_init(&dev_data->sem, 1, 1); in flash_stm32_ospi_init()
2377 k_sem_init(&dev_data->sync, 0, 1); in flash_stm32_ospi_init()
2380 dev_cfg->irq_config(dev); in flash_stm32_ospi_init()
2385 return -EIO; in flash_stm32_ospi_init()
2394 return -EIO; in flash_stm32_ospi_init()
2400 /* Process with the RDID (jedec read ID) instruction at init and fill jedec_id Table */ in flash_stm32_ospi_init()
2403 LOG_ERR("Read ID failed: %d", ret); in flash_stm32_ospi_init()
2410 dev_cfg->data_mode, dev_cfg->data_rate); in flash_stm32_ospi_init()
2411 return -EIO; in flash_stm32_ospi_init()
2416 /* Send the instruction to read the SFDP */ in flash_stm32_ospi_init()
2427 LOG_ERR("SFDP read failed: %d", ret); in flash_stm32_ospi_init()
2435 return -EINVAL; in flash_stm32_ospi_init()
2438 LOG_DBG("%s: SFDP v %u.%u AP %x with %u PH", dev->name, in flash_stm32_ospi_init()
2439 hp->rev_major, hp->rev_minor, hp->access, 1 + hp->nph); in flash_stm32_ospi_init()
2441 const struct jesd216_param_header *php = hp->phdr; in flash_stm32_ospi_init()
2443 MIN(decl_nph, 1 + hp->nph); in flash_stm32_ospi_init()
2449 (php - hp->phdr), id, php->rev_major, php->rev_minor, in flash_stm32_ospi_init()
2450 php->len_dw, jesd216_param_addr(php)); in flash_stm32_ospi_init()
2461 MIN(sizeof(uint32_t) * php->len_dw, sizeof(u2.dw))); in flash_stm32_ospi_init()
2473 if (dev_data->address_width == 4U) { in flash_stm32_ospi_init()
2487 MIN(sizeof(uint32_t) * php->len_dw, sizeof(u2.dw))); in flash_stm32_ospi_init()
2492 struct jesd216_erase_type *etp = &dev_data->erase_types[ei]; in flash_stm32_ospi_init()
2493 const uint8_t cmd = u2.types.type[ei]; in flash_stm32_ospi_init() local
2495 if (cmd == 0xff) { in flash_stm32_ospi_init()
2496 etp->exp = 0; in flash_stm32_ospi_init()
2497 etp->cmd = 0; in flash_stm32_ospi_init()
2499 etp->cmd = cmd; in flash_stm32_ospi_init()
2511 return -ENODEV; in flash_stm32_ospi_init()
2520 return -EINVAL; in flash_stm32_ospi_init()
2522 LOG_DBG("NOR octo-flash in MemoryMapped mode at 0x%lx (0x%x bytes)", in flash_stm32_ospi_init()
2524 dev_cfg->flash_size); in flash_stm32_ospi_init()
2526 LOG_DBG("NOR octo-flash at 0x%lx (0x%x bytes)", in flash_stm32_ospi_init()
2528 dev_cfg->flash_size); in flash_stm32_ospi_init()