@@ -580,19 +580,92 @@ static int flash_stm32_qspi_read(const struct device *dev, off_t addr,
580580 return ret ;
581581}
582582
583- static int qspi_wait_until_ready (const struct device * dev )
583+ static int qspi_read_status_register (const struct device * dev , uint8_t reg_num , uint8_t * reg )
584584{
585- uint8_t reg ;
585+ QSPI_CommandTypeDef cmd = {
586+ .InstructionMode = QSPI_INSTRUCTION_1_LINE ,
587+ .DataMode = QSPI_DATA_1_LINE ,
588+ };
589+
590+ switch (reg_num ) {
591+ case 1U :
592+ cmd .Instruction = SPI_NOR_CMD_RDSR ;
593+ break ;
594+ case 2U :
595+ cmd .Instruction = SPI_NOR_CMD_RDSR2 ;
596+ break ;
597+ case 3U :
598+ cmd .Instruction = SPI_NOR_CMD_RDSR3 ;
599+ break ;
600+ default :
601+ return - EINVAL ;
602+ }
603+
604+ return qspi_read_access (dev , & cmd , reg , sizeof (* reg ));
605+ }
606+
607+ static int qspi_write_status_register (const struct device * dev , uint8_t reg_num , uint8_t reg )
608+ {
609+ struct flash_stm32_qspi_data * dev_data = dev -> data ;
610+ size_t size ;
611+ uint8_t regs [4 ] = { 0 };
612+ uint8_t * regs_p ;
586613 int ret ;
587614
588615 QSPI_CommandTypeDef cmd = {
589- .Instruction = SPI_NOR_CMD_RDSR ,
616+ .Instruction = SPI_NOR_CMD_WRSR ,
590617 .InstructionMode = QSPI_INSTRUCTION_1_LINE ,
591618 .DataMode = QSPI_DATA_1_LINE ,
592619 };
593620
621+ if (reg_num == 1U ) {
622+ size = 1U ;
623+ regs [0 ] = reg ;
624+ regs_p = & regs [0 ];
625+ /* 1 byte write clears SR2, write SR2 as well */
626+ if (dev_data -> qer_type == JESD216_DW15_QER_S2B1v1 ) {
627+ ret = qspi_read_status_register (dev , 2 , & regs [1 ]);
628+ if (ret < 0 ) {
629+ return ret ;
630+ }
631+ size = 2U ;
632+ }
633+ } else if (reg_num == 2U ) {
634+ cmd .Instruction = SPI_NOR_CMD_WRSR2 ;
635+ size = 1U ;
636+ regs [1 ] = reg ;
637+ regs_p = & regs [1 ];
638+ /* if SR2 write needs SR1 */
639+ if ((dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v1 ) ||
640+ (dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v4 ) ||
641+ (dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v5 )) {
642+ ret = qspi_read_status_register (dev , 1 , & regs [0 ]);
643+ if (ret < 0 ) {
644+ return ret ;
645+ }
646+ cmd .Instruction = SPI_NOR_CMD_WRSR ;
647+ size = 2U ;
648+ regs_p = & regs [0 ];
649+ }
650+ } else if (reg_num == 3U ) {
651+ cmd .Instruction = SPI_NOR_CMD_WRSR3 ;
652+ size = 1U ;
653+ regs [2 ] = reg ;
654+ regs_p = & regs [2 ];
655+ } else {
656+ return - EINVAL ;
657+ }
658+
659+ return qspi_write_access (dev , & cmd , regs_p , size );
660+ }
661+
662+ static int qspi_wait_until_ready (const struct device * dev )
663+ {
664+ uint8_t reg ;
665+ int ret ;
666+
594667 do {
595- ret = qspi_read_access (dev , & cmd , & reg , sizeof ( reg ) );
668+ ret = qspi_read_status_register (dev , 1 , & reg );
596669 } while (!ret && (reg & SPI_NOR_WIP_BIT ));
597670
598671 return ret ;
@@ -1088,85 +1161,6 @@ static int qspi_program_addr_4b(const struct device *dev, bool write_enable)
10881161 return qspi_send_cmd (dev , & cmd );
10891162}
10901163
1091- static int qspi_read_status_register (const struct device * dev , uint8_t reg_num , uint8_t * reg )
1092- {
1093- QSPI_CommandTypeDef cmd = {
1094- .InstructionMode = QSPI_INSTRUCTION_1_LINE ,
1095- .DataMode = QSPI_DATA_1_LINE ,
1096- };
1097-
1098- switch (reg_num ) {
1099- case 1U :
1100- cmd .Instruction = SPI_NOR_CMD_RDSR ;
1101- break ;
1102- case 2U :
1103- cmd .Instruction = SPI_NOR_CMD_RDSR2 ;
1104- break ;
1105- case 3U :
1106- cmd .Instruction = SPI_NOR_CMD_RDSR3 ;
1107- break ;
1108- default :
1109- return - EINVAL ;
1110- }
1111-
1112- return qspi_read_access (dev , & cmd , reg , sizeof (* reg ));
1113- }
1114-
1115- static int qspi_write_status_register (const struct device * dev , uint8_t reg_num , uint8_t reg )
1116- {
1117- struct flash_stm32_qspi_data * dev_data = dev -> data ;
1118- size_t size ;
1119- uint8_t regs [4 ] = { 0 };
1120- uint8_t * regs_p ;
1121- int ret ;
1122-
1123- QSPI_CommandTypeDef cmd = {
1124- .Instruction = SPI_NOR_CMD_WRSR ,
1125- .InstructionMode = QSPI_INSTRUCTION_1_LINE ,
1126- .DataMode = QSPI_DATA_1_LINE ,
1127- };
1128-
1129- if (reg_num == 1 ) {
1130- size = 1U ;
1131- regs [0 ] = reg ;
1132- regs_p = & regs [0 ];
1133- /* 1 byte write clears SR2, write SR2 as well */
1134- if (dev_data -> qer_type == JESD216_DW15_QER_S2B1v1 ) {
1135- ret = qspi_read_status_register (dev , 2 , & regs [1 ]);
1136- if (ret < 0 ) {
1137- return ret ;
1138- }
1139- size = 2U ;
1140- }
1141- } else if (reg_num == 2 ) {
1142- cmd .Instruction = SPI_NOR_CMD_WRSR2 ;
1143- size = 1U ;
1144- regs [1 ] = reg ;
1145- regs_p = & regs [1 ];
1146- /* if SR2 write needs SR1 */
1147- if ((dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v1 ) ||
1148- (dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v4 ) ||
1149- (dev_data -> qer_type == JESD216_DW15_QER_VAL_S2B1v5 )) {
1150- ret = qspi_read_status_register (dev , 1 , & regs [0 ]);
1151- if (ret < 0 ) {
1152- return ret ;
1153- }
1154- cmd .Instruction = SPI_NOR_CMD_WRSR ;
1155- size = 2U ;
1156- regs_p = & regs [0 ];
1157- }
1158- } else if (reg_num == 3 ) {
1159- cmd .Instruction = SPI_NOR_CMD_WRSR3 ;
1160- size = 1U ;
1161- regs [2 ] = reg ;
1162- regs_p = & regs [2 ];
1163- } else {
1164- return - EINVAL ;
1165- }
1166-
1167- return qspi_write_access (dev , & cmd , regs_p , size );
1168- }
1169-
11701164static int qspi_write_enable (const struct device * dev )
11711165{
11721166 uint8_t reg ;
0 commit comments