Skip to content

Commit 984dc0b

Browse files
committed
drivers: ethernet: stm32: add support of the stm32mp13
Add the support of the stm32mp13 ethernet: - Adapt to HAL API - Add support of a memory region in DT for descriptor and buffers - Add support of PHY with/without crystal 50MHz Signed-off-by: Arnaud Pouliquen <[email protected]> Signed-off-by: Arif Balik <[email protected]>
1 parent 31a066c commit 984dc0b

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

drivers/ethernet/Kconfig.stm32_hal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ choice ETH_STM32_HAL_API_VERSION
2626

2727
config ETH_STM32_HAL_API_V2
2828
bool "Use official STM32Cube HAL driver"
29-
depends on SOC_SERIES_STM32H7X || SOC_SERIES_STM32H5X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32N6X
30-
select USE_STM32_HAL_ETH_EX if SOC_SERIES_STM32N6X
29+
depends on SOC_SERIES_STM32H7X || SOC_SERIES_STM32H5X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32N6X || SOC_SERIES_STM32MP13X
30+
select USE_STM32_HAL_ETH_EX if SOC_SERIES_STM32N6X || SOC_SERIES_STM32MP13X
3131
help
3232
Use the official STM32Cube HAL driver instead of the legacy one.
3333

drivers/ethernet/eth_stm32_hal_common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,18 @@ static const struct eth_stm32_hal_dev_cfg eth0_config = {
431431
#if DT_INST_CLOCKS_HAS_NAME(0, mac_clk_ptp)
432432
.pclken_ptp = {.bus = DT_INST_CLOCKS_CELL_BY_NAME(0, mac_clk_ptp, bus),
433433
.enr = DT_INST_CLOCKS_CELL_BY_NAME(0, mac_clk_ptp, bits)},
434+
#endif
435+
#if DT_INST_CLOCKS_HAS_NAME(0, mac_clk)
436+
.pclken_mac = {.bus = DT_INST_CLOCKS_CELL_BY_NAME(0, mac_clk, bus),
437+
.enr = DT_INST_CLOCKS_CELL_BY_NAME(0, mac_clk, bits)},
438+
#endif
439+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
440+
.clockselection = DT_NODE_HAS_PROP(0, st_ext_phyclk) ? HAL_ETH1_REF_CLK_RCC
441+
: HAL_ETH1_REF_CLK_RX_CLK_PIN,
442+
#endif
443+
#if DT_INST_CLOCKS_HAS_NAME(0, eth_ker)
444+
.pclken_ker = {.bus = DT_INST_CLOCKS_CELL_BY_NAME(0, eth_ker, bus),
445+
.enr = DT_INST_CLOCKS_CELL_BY_NAME(0, eth_ker, bits)},
434446
#endif
435447
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
436448
};

drivers/ethernet/eth_stm32_hal_priv.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <zephyr/drivers/clock_control.h>
1111
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
12+
#include <zephyr/linker/devicetree_regions.h>
1213
#include <zephyr/kernel.h>
1314
#include <zephyr/net/ethernet.h>
1415
#include <zephyr/net/phy.h>
@@ -36,6 +37,11 @@ extern const struct device *eth_stm32_phy_dev;
3637
#elif defined(CONFIG_SOC_SERIES_STM32H7X)
3738
#define __eth_stm32_desc __attribute__((section(".eth_stm32_desc")))
3839
#define __eth_stm32_buf __attribute__((section(".eth_stm32_buf")))
40+
#elif defined(CONFIG_SOC_SERIES_STM32MP13X)
41+
#define ETH_DMA_REGION DT_PHANDLE(DT_NODELABEL(mac), memory_regions)
42+
#define ETH_DMA_LINKER_REGION_NAME LINKER_DT_NODE_REGION_NAME(ETH_DMA_REGION)
43+
#define __eth_stm32_desc ALIGN_32BYTES(__attribute__((__section__(ETH_DMA_LINKER_REGION_NAME))))
44+
#define __eth_stm32_buf ALIGN_32BYTES(__attribute__((__section__(ETH_DMA_LINKER_REGION_NAME))))
3945
#elif defined(CONFIG_NOCACHE_MEMORY)
4046
#define __eth_stm32_desc __nocache __aligned(4)
4147
#define __eth_stm32_buf __nocache __aligned(4)
@@ -102,6 +108,15 @@ extern ETH_DMADescTypeDef dma_tx_desc_tab[ETH_TXBUFNB];
102108
struct eth_stm32_hal_dev_cfg {
103109
void (*config_func)(void);
104110
struct stm32_pclken pclken;
111+
#if DT_INST_CLOCKS_HAS_NAME(0, mac_clk)
112+
struct stm32_pclken pclken_mac;
113+
#endif
114+
#if DT_INST_CLOCKS_HAS_NAME(0, eth_ker)
115+
struct stm32_pclken pclken_ker;
116+
#endif
117+
#if defined(CONFIG_SOC_SERIES_STM32MP13X)
118+
ETH_ClkSrcTypeDef clockselection;
119+
#endif /* CONFIG_SOC_SERIES_STM32MP13X */
105120
struct stm32_pclken pclken_rx;
106121
struct stm32_pclken pclken_tx;
107122
#if DT_INST_CLOCKS_HAS_NAME(0, mac_clk_ptp)

drivers/ethernet/eth_stm32_hal_v2.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/sys/util.h>
1111
#include <zephyr/sys/__assert.h>
1212
#include <ethernet/eth_stats.h>
13+
#include <zephyr/linker/devicetree_regions.h>
1314

1415
#include <errno.h>
1516
#include <stdbool.h>
@@ -41,7 +42,11 @@ static struct eth_stm32_tx_context dma_tx_context[ETH_TX_DESC_CNT];
4142
/* Pointer to an array of ETH_STM32_RX_BUF_SIZE uint8_t's */
4243
typedef uint8_t (*RxBufferPtr)[ETH_STM32_RX_BUF_SIZE];
4344

45+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
46+
void HAL_ETH_RxAllocateCallback(ETH_HandleTypeDef *heth, uint8_t **buf)
47+
#else
4448
void HAL_ETH_RxAllocateCallback(uint8_t **buf)
49+
#endif
4550
{
4651
for (size_t i = 0; i < ETH_RXBUFNB; ++i) {
4752
if (!dma_rx_buffer_header[i].used) {
@@ -56,7 +61,12 @@ void HAL_ETH_RxAllocateCallback(uint8_t **buf)
5661
}
5762

5863
/* called by HAL_ETH_ReadData() */
64+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
65+
void HAL_ETH_RxLinkCallback(ETH_HandleTypeDef *heth, void **pStart, void **pEnd,
66+
uint8_t *buff, uint16_t Length)
67+
#else
5968
void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
69+
#endif
6070
{
6171
/* buff points to the begin on one of the rx buffers,
6272
* so we can compute the index of the given buffer
@@ -81,7 +91,11 @@ void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t
8191
}
8292

8393
/* Called by HAL_ETH_ReleaseTxPacket */
94+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
95+
void HAL_ETH_TxFreeCallback(ETH_HandleTypeDef *heth, uint32_t *buff)
96+
#else
8497
void HAL_ETH_TxFreeCallback(uint32_t *buff)
98+
#endif
8599
{
86100
__ASSERT_NO_MSG(buff != NULL);
87101

@@ -279,7 +293,11 @@ int eth_stm32_tx(const struct device *dev, struct net_pkt *pkt)
279293
HAL_ETH_ReleaseTxPacket(heth);
280294
} else {
281295
/* We need to release the tx context and its buffers */
296+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
297+
HAL_ETH_TxFreeCallback(heth, (uint32_t *)ctx);
298+
#else
282299
HAL_ETH_TxFreeCallback((uint32_t *)ctx);
300+
#endif
283301
}
284302

285303
k_mutex_unlock(&dev_data->tx_mutex);
@@ -454,6 +472,9 @@ void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
454472
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet)
455473
dev_data->stats.error_details.rx_crc_errors = heth->Instance->MMCRCRCEPR;
456474
dev_data->stats.error_details.rx_align_errors = heth->Instance->MMCRAEPR;
475+
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
476+
dev_data->stats.error_details.rx_crc_errors = heth->Instance->MMCRXCRCEPR;
477+
dev_data->stats.error_details.rx_align_errors = heth->Instance->MMCRXAEPR;
457478
#else
458479
dev_data->stats.error_details.rx_crc_errors = heth->Instance->MMCRFCECR;
459480
dev_data->stats.error_details.rx_align_errors = heth->Instance->MMCRFAECR;
@@ -467,6 +488,9 @@ int eth_stm32_hal_init(const struct device *dev)
467488
struct eth_stm32_hal_dev_data *dev_data = dev->data;
468489
ETH_HandleTypeDef *heth = &dev_data->heth;
469490
HAL_StatusTypeDef hal_ret = HAL_OK;
491+
__maybe_unused const struct eth_stm32_hal_dev_cfg *cfg = dev->config;
492+
__maybe_unused uint8_t *desc_uncached_addr;
493+
__maybe_unused int ret;
470494

471495
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32n6_ethernet)
472496
for (int ch = 0; ch < ETH_DMA_CH_CNT; ch++) {
@@ -479,6 +503,37 @@ int eth_stm32_hal_init(const struct device *dev)
479503
#endif
480504
heth->Init.RxBuffLen = ETH_STM32_RX_BUF_SIZE;
481505

506+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32mp13_ethernet)
507+
/* Map memory region for DMA descriptor and buffer as non cacheable */
508+
k_mem_map_phys_bare(&desc_uncached_addr,
509+
DT_REG_ADDR(ETH_DMA_REGION),
510+
DT_REG_SIZE(ETH_DMA_REGION),
511+
K_MEM_PERM_RW | K_MEM_DIRECT_MAP | K_MEM_CACHE_NONE |
512+
K_MEM_ARM_NORMAL_NC);
513+
514+
heth->Init.ClockSelection = cfg->clockselection;
515+
#endif
516+
517+
#if DT_INST_CLOCKS_HAS_NAME(0, eth_ker)
518+
/* Turn on DCMIPP peripheral clock */
519+
ret = clock_control_configure(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
520+
(clock_control_subsys_t)&cfg->pclken_ker,
521+
NULL);
522+
if (ret < 0) {
523+
LOG_ERR("Failed to configure ETH kernel clock. Error %d", ret);
524+
return ret;
525+
}
526+
#endif
527+
528+
#if DT_INST_CLOCKS_HAS_NAME(0, mac_clk)
529+
ret = clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
530+
(clock_control_subsys_t)&cfg->pclken_mac);
531+
if (ret < 0) {
532+
LOG_ERR("Failed to configure mac clock. Error %d", ret);
533+
return ret;
534+
}
535+
#endif
536+
482537
hal_ret = HAL_ETH_Init(heth);
483538
if (hal_ret == HAL_TIMEOUT) {
484539
/* HAL Init time out. This could be linked to */

0 commit comments

Comments
 (0)