Skip to content

Commit

Permalink
sam: mdio: Fix Transfer Timeout at Initialization
Browse files Browse the repository at this point in the history
Initialize the MDIO peripheral clock (normally done during GMAC
initialization) before trying any MDIO transfers, preventing startup
errors.

Signed-off-by: Nick Kraus <[email protected]>
  • Loading branch information
nick-kraus authored and carlescufi committed Nov 10, 2023
1 parent a5d8550 commit 5bd1888
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions drivers/mdio/mdio_sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <zephyr/drivers/clock_control/atmel_sam_pmc.h>
#include <zephyr/drivers/mdio.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/net/mdio.h>
Expand All @@ -33,6 +34,9 @@ struct mdio_sam_dev_data {
struct mdio_sam_dev_config {
Gmac * const regs;
const struct pinctrl_dev_config *pcfg;
#ifdef CONFIG_SOC_FAMILY_SAM
const struct atmel_sam_pmc_config clock_cfg;
#endif
};

static int mdio_transfer(const struct device *dev, uint8_t prtad, uint8_t regad,
Expand Down Expand Up @@ -140,6 +144,15 @@ static int mdio_sam_initialize(const struct device *dev)

k_sem_init(&data->sem, 1, 1);

#ifdef CONFIG_SOC_FAMILY_SAM
/* Enable GMAC module's clock */
(void) clock_control_on(SAM_DT_PMC_CONTROLLER, (clock_control_subsys_t) &cfg->clock_cfg);
#else
/* Enable MCLK clock on GMAC */
MCLK->AHBMASK.reg |= MCLK_AHBMASK_GMAC;
*MCLK_GMAC |= MCLK_GMAC_MASK;
#endif

retval = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);

return retval;
Expand All @@ -154,10 +167,16 @@ static const struct mdio_driver_api mdio_sam_driver_api = {
.bus_disable = mdio_sam_bus_disable,
};

#define MDIO_SAM_CLOCK(n) \
COND_CODE_1(CONFIG_SOC_FAMILY_SAM, \
(.clock_cfg = SAM_DT_INST_CLOCK_PMC_CFG(n),), () \
)

#define MDIO_SAM_CONFIG(n) \
static const struct mdio_sam_dev_config mdio_sam_dev_config_##n = { \
.regs = (Gmac *)DT_INST_REG_ADDR(n), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
MDIO_SAM_CLOCK(n) \
};

#define MDIO_SAM_DEVICE(n) \
Expand Down
1 change: 1 addition & 0 deletions dts/arm/atmel/sam4e.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
mdio: mdio@40034000 {
compatible = "atmel,sam-mdio";
reg = <0x40034000 0x4000>;
clocks = <&pmc PMC_TYPE_PERIPHERAL 44>;
status = "disabled";
#address-cells = <1>;
#size-cells = <0>;
Expand Down
1 change: 1 addition & 0 deletions dts/arm/atmel/same70.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
mdio: mdio@40050000 {
compatible = "atmel,sam-mdio";
reg = <0x40050000 0x4000>;
clocks = <&pmc PMC_TYPE_PERIPHERAL 39>;
status = "disabled";
#address-cells = <1>;
#size-cells = <0>;
Expand Down
4 changes: 4 additions & 0 deletions dts/bindings/mdio/atmel,sam-mdio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ compatible: "atmel,sam-mdio"
include:
- name: mdio-controller.yaml
- name: pinctrl-device.yaml

properties:
clocks:
type: phandle-array

0 comments on commit 5bd1888

Please sign in to comment.