@@ -763,10 +763,15 @@ static int RAMFUNCTION hal_flash_ppb_unlock(uint32_t sector)
763763 FLASH_IO8_WRITE (0 , FLASH_UNLOCK_ADDR2 , AMD_CMD_UNLOCK_ACK );
764764 FLASH_IO8_WRITE (0 , FLASH_UNLOCK_ADDR1 , AMD_CMD_SET_PPB_ENTRY );
765765
766- /* Read PPB status for target sector: DQ0=0 means protected */
767- ppb_status = FLASH_IO8_READ (sector , 0 );
768-
766+ /* Read PPB status for target sector: DQ0=0 means protected.
767+ * On 16-bit bus, must read both chip lanes to check both devices. */
768+ #if FLASH_CFI_WIDTH == 16
769+ ppb_status = FLASH_IO16_READ (sector , 0 );
769770 if ((ppb_status & 0x0101 ) == 0x0101 ) {
771+ #else
772+ ppb_status = FLASH_IO8_READ (sector , 0 );
773+ if ((ppb_status & 0x01 ) == 0x01 ) {
774+ #endif
770775 /* Both chips report unprotected — exit PPB mode and return */
771776 FLASH_IO8_WRITE (0 , 0 , AMD_CMD_SET_PPB_EXIT_BC1 );
772777 FLASH_IO8_WRITE (0 , 0 , AMD_CMD_SET_PPB_EXIT_BC2 );
@@ -792,11 +797,17 @@ static int RAMFUNCTION hal_flash_ppb_unlock(uint32_t sector)
792797 FLASH_IO8_WRITE (0 , 0 , AMD_CMD_PPB_UNLOCK_BC1 ); /* 0x80 */
793798 FLASH_IO8_WRITE (0 , 0 , AMD_CMD_PPB_UNLOCK_BC2 ); /* 0x30 */
794799
795- /* Wait for PPB erase completion — poll for toggle stop */
800+ /* Wait for PPB erase completion — poll for toggle stop.
801+ * On 16-bit bus, read both chip lanes to ensure both complete. */
796802 timeout = 0 ;
797803 do {
804+ #if FLASH_CFI_WIDTH == 16
805+ read1 = FLASH_IO16_READ (0 , 0 );
806+ read2 = FLASH_IO16_READ (0 , 0 );
807+ #else
798808 read1 = FLASH_IO8_READ (0 , 0 );
799809 read2 = FLASH_IO8_READ (0 , 0 );
810+ #endif
800811 if (read1 == read2 )
801812 break ;
802813 ram_udelay (10 );
@@ -828,19 +839,37 @@ static int RAMFUNCTION hal_flash_status_wait(uint32_t sector, uint16_t mask,
828839 uint32_t timeout = 0 ;
829840 uint16_t read1 , read2 ;
830841
842+ /* Replicate 8-bit AMD status mask to both bytes for parallel chips */
843+ #if FLASH_CFI_WIDTH == 16
844+ uint16_t mask16 = (mask << 8 ) | mask ;
845+ uint16_t toggle16 = (AMD_STATUS_TOGGLE << 8 ) | AMD_STATUS_TOGGLE ;
846+ #else
847+ uint16_t mask16 = mask ;
848+ uint16_t toggle16 = AMD_STATUS_TOGGLE ;
849+ #endif
850+
831851 do {
832852 /* detection of completion happens when reading status bits
833853 * DQ6 and DQ2 stop toggling (0x44) */
854+ #if FLASH_CFI_WIDTH == 16
855+ read1 = FLASH_IO16_READ (sector , 0 );
856+ if ((read1 & toggle16 ) == 0 )
857+ read1 = FLASH_IO16_READ (sector , 0 );
858+ read2 = FLASH_IO16_READ (sector , 0 );
859+ if ((read2 & toggle16 ) == 0 )
860+ read2 = FLASH_IO16_READ (sector , 0 );
861+ #else
834862 read1 = FLASH_IO8_READ (sector , 0 );
835- if ((read1 & AMD_STATUS_TOGGLE ) == 0 )
863+ if ((read1 & toggle16 ) == 0 )
836864 read1 = FLASH_IO8_READ (sector , 0 );
837865 read2 = FLASH_IO8_READ (sector , 0 );
838- if ((read2 & AMD_STATUS_TOGGLE ) == 0 )
866+ if ((read2 & toggle16 ) == 0 )
839867 read2 = FLASH_IO8_READ (sector , 0 );
868+ #endif
840869 #ifdef DEBUG_FLASH
841870 wolfBoot_printf ("Wait toggle %x -> %x\n" , read1 , read2 );
842871 #endif
843- if (read1 == read2 && ((read1 & mask ) == mask ))
872+ if (read1 == read2 && ((read1 & mask16 ) == mask16 ))
844873 break ;
845874 ram_udelay (1 );
846875 } while (timeout ++ < timeout_us );
@@ -858,6 +887,14 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
858887{
859888 int ret = 0 ;
860889 uint32_t i , pos , sector , offset , xfer , nwords ;
890+ const uint32_t width_bytes = FLASH_CFI_WIDTH / 8 ;
891+
892+ /* Enforce alignment to flash bus width */
893+ if ((address % width_bytes ) != 0 || (len % width_bytes ) != 0 ) {
894+ wolfBoot_printf ("Flash Write: unaligned addr 0x%x or len %d "
895+ "(need %d-byte alignment)\n" , address , len , width_bytes );
896+ return -1 ;
897+ }
861898
862899 /* adjust for flash base */
863900 if (address >= FLASH_BASE_ADDR )
@@ -1191,21 +1228,19 @@ int hal_dts_fixup(void* dts_addr)
11911228
11921229 /* fixup the memory region - single bank */
11931230 off = fdt_find_devtype (fdt , -1 , "memory" );
1194- if (off != - FDT_ERR_NOTFOUND ) {
1195- /* build addr/size as 64-bit */
1196- uint8_t ranges [sizeof (uint64_t ) * 2 ], * p = ranges ;
1197- * (uint64_t * )p = cpu_to_fdt64 (DDR_ADDRESS );
1198- p += sizeof (uint64_t );
1199- * (uint64_t * )p = cpu_to_fdt64 (DDR_SIZE );
1200- p += sizeof (uint64_t );
1231+ if (off >= 0 ) {
1232+ /* build addr/size as aligned 64-bit values */
1233+ uint64_t ranges [2 ];
1234+ ranges [0 ] = cpu_to_fdt64 (DDR_ADDRESS );
1235+ ranges [1 ] = cpu_to_fdt64 (DDR_SIZE );
12011236 wolfBoot_printf ("FDT: Set memory, start=0x%x, size=0x%x\n" ,
12021237 DDR_ADDRESS , (uint32_t )DDR_SIZE );
1203- fdt_setprop (fdt , off , "reg" , ranges , ( int )( p - ranges ));
1238+ fdt_setprop (fdt , off , "reg" , ranges , sizeof ( ranges ));
12041239 }
12051240
12061241 /* fixup CPU status and release address and enable method */
12071242 off = fdt_find_devtype (fdt , -1 , "cpu" );
1208- while (off != - FDT_ERR_NOTFOUND ) {
1243+ while (off >= 0 ) {
12091244 int core ;
12101245 #ifdef ENABLE_MP
12111246 uint64_t core_spin_table ;
@@ -1237,13 +1272,13 @@ int hal_dts_fixup(void* dts_addr)
12371272
12381273 /* fixup the soc clock */
12391274 off = fdt_find_devtype (fdt , -1 , "soc" );
1240- if (off != - FDT_ERR_NOTFOUND ) {
1275+ if (off >= 0 ) {
12411276 fdt_fixup_val (fdt , off , "soc" , "bus-frequency" , hal_get_plat_clk ());
12421277 }
12431278
12441279 /* fixup the serial clocks */
12451280 off = fdt_find_devtype (fdt , -1 , "serial" );
1246- while (off != - FDT_ERR_NOTFOUND ) {
1281+ while (off >= 0 ) {
12471282 fdt_fixup_val (fdt , off , "serial" , "clock-frequency" , hal_get_bus_clk ());
12481283 off = fdt_find_devtype (fdt , off , "serial" );
12491284 }
0 commit comments