stm32cube: stm32h7: use transmit timestamp status flag.

Read timestamp only when status flag is set in Tx descriptor.
Fire transmit timestamp callbacks only when there is a valid
non-zero timestamp.

(cherry picked from commit d466dc8421ee0c6592bb5682aa93a671bc948107)

Original-Co-authored-by: Naeem Bacha <naeem.bacha@audinate.com>
Original-Signed-off-by: Chamira Perera <chamira.perera@audinate.com>
GitOrigin-RevId: d466dc8421ee0c6592bb5682aa93a671bc948107
Change-Id: Ic216bb5d72be4570721d79b0d550fc4d790cb194
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/hal_stm32/+/4720447
Commit-Queue: Yuval Peress <peress@google.com>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Reviewed-by: Yuval Peress <peress@google.com>
Tested-by: Yuval Peress <peress@google.com>
diff --git a/stm32cube/stm32h7xx/README b/stm32cube/stm32h7xx/README
index 98e55a1..6e90026 100644
--- a/stm32cube/stm32h7xx/README
+++ b/stm32cube/stm32h7xx/README
@@ -68,4 +68,12 @@
       drivers/include/stm32h7xx_hal_def.h
      ST Internal Reference: 136825
 
+   *Fix to use transmit timestamp when it is valid
+    Read timestamp only when status flag is set in Tx descriptor.
+    Fire transmit timestamp callbacks only when there is a valid
+    non-zero timestamp.
+    Impacted files:
+     drivers/src/stm32h7xx_hal_eth.c
+    ST Internal Reference: 157129
+
    See release_note.html from STM32Cube
diff --git a/stm32cube/stm32h7xx/drivers/src/stm32h7xx_hal_eth.c b/stm32cube/stm32h7xx/drivers/src/stm32h7xx_hal_eth.c
index 4850fb5..7a7f800 100644
--- a/stm32cube/stm32h7xx/drivers/src/stm32h7xx_hal_eth.c
+++ b/stm32cube/stm32h7xx/drivers/src/stm32h7xx_hal_eth.c
@@ -1432,17 +1432,29 @@
         /* Disable Ptp transmission */
         CLEAR_BIT(heth->Init.TxDesc[idx].DESC3, (0x40000000U));
 
-        /* Get timestamp low */
-        timestamp->TimeStampLow = heth->Init.TxDesc[idx].DESC0;
-        /* Get timestamp high */
-        timestamp->TimeStampHigh = heth->Init.TxDesc[idx].DESC1;
+        if ((heth->Init.TxDesc[idx].DESC3 & ETH_DMATXNDESCWBF_LD )
+          && (heth->Init.TxDesc[idx].DESC3 & ETH_DMATXNDESCWBF_TTSS))
+        {
+          /* Get timestamp low */
+          timestamp->TimeStampLow = heth->Init.TxDesc[idx].DESC0;
+          /* Get timestamp high */
+          timestamp->TimeStampHigh = heth->Init.TxDesc[idx].DESC1;
+        }
+        else
+        {
+          timestamp->TimeStampHigh = timestamp->TimeStampLow = UINT32_MAX;
+        }
+
 #endif /* HAL_ETH_USE_PTP */
 
 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
         /*Call registered callbacks*/
 #ifdef HAL_ETH_USE_PTP
         /* Handle Ptp  */
-        heth->txPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
+        if (timestamp->TimeStampHigh != UINT32_MAX && timestamp->TimeStampLow != UINT32_MAX)
+        {
+          heth->txPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
+        }
 #endif  /* HAL_ETH_USE_PTP */
         /* Release the packet.  */
         heth->txFreeCallback(dmatxdesclist->PacketAddress[idx]);
@@ -1450,7 +1462,10 @@
         /* Call callbacks */
 #ifdef HAL_ETH_USE_PTP
         /* Handle Ptp  */
-        HAL_ETH_TxPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
+        if (timestamp->TimeStampHigh != UINT32_MAX && timestamp->TimeStampLow != UINT32_MAX)
+        {
+          HAL_ETH_TxPtpCallback(dmatxdesclist->PacketAddress[idx], timestamp);
+        }
 #endif  /* HAL_ETH_USE_PTP */
         /* Release the packet.  */
         HAL_ETH_TxFreeCallback(dmatxdesclist->PacketAddress[idx]);