2019-06-25 07:36:56 -04:00
|
|
|
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include "esp_log.h"
|
2021-04-01 08:00:54 -04:00
|
|
|
#include "esp_check.h"
|
2019-06-25 07:36:56 -04:00
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "eth_phy_regs_struct.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2019-11-13 23:03:14 -05:00
|
|
|
#include "driver/gpio.h"
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2019-06-25 07:36:56 -04:00
|
|
|
|
2021-04-01 08:00:54 -04:00
|
|
|
static const char *TAG = "dm9051.phy";
|
2019-06-25 07:36:56 -04:00
|
|
|
|
|
|
|
/***************Vendor Specific Register***************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DSCR(DAVICOM Specified Configuration Register)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
struct {
|
|
|
|
uint32_t reserved1 : 1; /* Reserved */
|
|
|
|
uint32_t sleep : 1; /* Set 1 to enable PHY into sleep mode */
|
|
|
|
uint32_t mfpsc : 1; /* MII frame preamble suppression control bit */
|
|
|
|
uint32_t smrst : 1; /* Set 1 to reset all state machines of PHY */
|
|
|
|
uint32_t rpdctr_en : 1; /* Set 1 to enable automatic reduced power down */
|
|
|
|
uint32_t reserved2 : 2; /* Reserved */
|
|
|
|
uint32_t flink100 : 1; /* Force Good Link in 100Mbps */
|
|
|
|
uint32_t reserved3 : 2; /* Reserved */
|
|
|
|
uint32_t tx_fx : 1; /* 100BASE-TX or FX Mode Control */
|
|
|
|
uint32_t reserved4 : 1; /* Reserved */
|
|
|
|
uint32_t bp_adpok : 1; /* BYPASS ADPOK */
|
|
|
|
uint32_t bp_align : 1; /* Bypass Symbol Alignment Function */
|
|
|
|
uint32_t bp_scr : 1; /* Bypass Scrambler/Descrambler Function */
|
|
|
|
uint32_t bp_4b5b : 1; /* Bypass 4B5B Encoding and 5B4B Decoding */
|
|
|
|
};
|
|
|
|
uint32_t val;
|
|
|
|
} dscr_reg_t;
|
|
|
|
#define ETH_PHY_DSCR_REG_ADDR (0x10)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DSCSR(DAVICOM Specified Configuration and Status Register)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
struct {
|
|
|
|
uint32_t anmb : 4; /* Auto-Negotiation Monitor Bits */
|
|
|
|
uint32_t phy_addr : 5; /* PHY Address */
|
|
|
|
uint32_t reserved : 3; /* Reserved */
|
|
|
|
uint32_t hdx10 : 1; /* 10M Half-Duplex Operation Mode */
|
|
|
|
uint32_t fdx10 : 1; /* 10M Full-Duplex Operation Mode */
|
|
|
|
uint32_t hdx100 : 1; /* 100M Half-Duplex Operation Mode */
|
|
|
|
uint32_t fdx100 : 1; /* 100M Full-Duplex Operation Mode */
|
|
|
|
};
|
|
|
|
uint32_t val;
|
|
|
|
} dscsr_reg_t;
|
|
|
|
#define ETH_PHY_DSCSR_REG_ADDR (0x11)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
esp_eth_phy_t parent;
|
|
|
|
esp_eth_mediator_t *eth;
|
2020-11-16 23:48:35 -05:00
|
|
|
int addr;
|
2019-06-25 07:36:56 -04:00
|
|
|
uint32_t reset_timeout_ms;
|
|
|
|
uint32_t autonego_timeout_ms;
|
|
|
|
eth_link_t link_status;
|
2019-11-13 23:03:14 -05:00
|
|
|
int reset_gpio_num;
|
2019-06-25 07:36:56 -04:00
|
|
|
} phy_dm9051_t;
|
|
|
|
|
2019-09-18 23:27:42 -04:00
|
|
|
static esp_err_t dm9051_update_link_duplex_speed(phy_dm9051_t *dm9051)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-09-18 23:27:42 -04:00
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
|
|
|
eth_speed_t speed = ETH_SPEED_10M;
|
|
|
|
eth_duplex_t duplex = ETH_DUPLEX_HALF;
|
2020-07-20 08:42:52 -04:00
|
|
|
uint32_t peer_pause_ability = false;
|
2019-09-18 23:27:42 -04:00
|
|
|
bmsr_reg_t bmsr;
|
|
|
|
dscsr_reg_t dscsr;
|
2020-07-20 08:42:52 -04:00
|
|
|
anlpar_reg_t anlpar;
|
2020-05-08 09:44:30 -04:00
|
|
|
// BMSR is a latch low register
|
|
|
|
// after power up, the first latched value must be 0, which means down
|
|
|
|
// to speed up power up link speed, double read this register as a workaround
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)), err, TAG, "read ANLPAR failed");
|
2019-09-18 23:27:42 -04:00
|
|
|
eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
|
|
|
|
/* check if link status changed */
|
|
|
|
if (dm9051->link_status != link) {
|
|
|
|
/* when link up, read negotiation result */
|
|
|
|
if (link == ETH_LINK_UP) {
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_DSCSR_REG_ADDR, &(dscsr.val)), err, TAG, "read DSCSR failed");
|
2019-09-18 23:27:42 -04:00
|
|
|
if (dscsr.fdx100 || dscsr.hdx100) {
|
|
|
|
speed = ETH_SPEED_100M;
|
|
|
|
} else {
|
|
|
|
speed = ETH_SPEED_10M;
|
|
|
|
}
|
|
|
|
if (dscsr.fdx100 || dscsr.fdx10) {
|
|
|
|
duplex = ETH_DUPLEX_FULL;
|
|
|
|
} else {
|
|
|
|
duplex = ETH_DUPLEX_HALF;
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *)speed), err, TAG, "change speed failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *)duplex), err, TAG, "change duplex failed");
|
2020-07-20 08:42:52 -04:00
|
|
|
/* if we're in duplex mode, and peer has the flow control ability */
|
|
|
|
if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
|
|
|
|
peer_pause_ability = 1;
|
|
|
|
} else {
|
|
|
|
peer_pause_ability = 0;
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *)peer_pause_ability), err, TAG, "change pause ability failed");
|
2019-09-18 23:27:42 -04:00
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link), err, TAG, "change link failed");
|
2019-09-18 23:27:42 -04:00
|
|
|
dm9051->link_status = link;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-09-18 23:27:42 -04:00
|
|
|
}
|
|
|
|
|
2019-06-25 07:36:56 -04:00
|
|
|
static esp_err_t dm9051_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
ESP_GOTO_ON_FALSE(eth, ESP_ERR_INVALID_ARG, err, TAG, "can't set mediator to null");
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
dm9051->eth = eth;
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_get_link(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
2019-09-18 23:27:42 -04:00
|
|
|
/* Updata information about link, speed, duplex */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(dm9051_update_link_duplex_speed(dm9051), err, TAG, "update link duplex speed failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_reset(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
2019-12-03 02:37:34 -05:00
|
|
|
dm9051->link_status = ETH_LINK_DOWN;
|
2019-06-25 07:36:56 -04:00
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
|
|
|
dscr_reg_t dscr;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_DSCR_REG_ADDR, &(dscr.val)), err, TAG, "read DSCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
dscr.smrst = 1;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, dm9051->addr, ETH_PHY_DSCR_REG_ADDR, dscr.val), err, TAG, "write DSCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
bmcr_reg_t bmcr = {.reset = 1};
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Wait for reset complete */
|
|
|
|
uint32_t to = 0;
|
|
|
|
for (to = 0; to < dm9051->reset_timeout_ms / 10; to++) {
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_DSCR_REG_ADDR, &(dscr.val)), err, TAG, "read DSCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
if (!bmcr.reset && !dscr.smrst) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(to < dm9051->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "PHY reset timeout");
|
2019-06-25 07:36:56 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
2019-11-13 23:03:14 -05:00
|
|
|
static esp_err_t dm9051_reset_hw(esp_eth_phy_t *phy)
|
|
|
|
{
|
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
// set reset_gpio_num minus zero can skip hardware reset phy chip
|
|
|
|
if (dm9051->reset_gpio_num >= 0) {
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_select_gpio(dm9051->reset_gpio_num);
|
2019-11-13 23:03:14 -05:00
|
|
|
gpio_set_direction(dm9051->reset_gpio_num, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_level(dm9051->reset_gpio_num, 0);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(100); // insert min input assert time
|
2019-11-13 23:03:14 -05:00
|
|
|
gpio_set_level(dm9051->reset_gpio_num, 1);
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-25 07:36:56 -04:00
|
|
|
static esp_err_t dm9051_negotiate(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
2021-05-06 08:16:12 -04:00
|
|
|
/* in case any link status has changed, let's assume we're in link down status */
|
|
|
|
dm9051->link_status = ETH_LINK_DOWN;
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Start auto negotiation */
|
|
|
|
bmcr_reg_t bmcr = {
|
|
|
|
.speed_select = 1, /* 100Mbps */
|
|
|
|
.duplex_mode = 1, /* Full Duplex */
|
|
|
|
.en_auto_nego = 1, /* Auto Negotiation */
|
|
|
|
.restart_auto_nego = 1 /* Restart Auto Negotiation */
|
|
|
|
};
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Wait for auto negotiation complete */
|
|
|
|
bmsr_reg_t bmsr;
|
|
|
|
dscsr_reg_t dscsr;
|
|
|
|
uint32_t to = 0;
|
2021-05-06 08:16:12 -04:00
|
|
|
for (to = 0; to < dm9051->autonego_timeout_ms / 100; to++) {
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_DSCSR_REG_ADDR, &(dscsr.val)), err, TAG, "read DSCSR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
if (bmsr.auto_nego_complete && dscsr.anmb & 0x08) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 08:16:12 -04:00
|
|
|
if (to >= dm9051->autonego_timeout_ms / 100) {
|
2019-06-25 07:36:56 -04:00
|
|
|
ESP_LOGW(TAG, "Ethernet PHY auto negotiation timeout");
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_pwrctl(esp_eth_phy_t *phy, bool enable)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
|
|
|
bmcr_reg_t bmcr;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
if (!enable) {
|
|
|
|
/* Enable IEEE Power Down Mode */
|
|
|
|
bmcr.power_down = 1;
|
|
|
|
} else {
|
|
|
|
/* Disable IEEE Power Down Mode */
|
|
|
|
bmcr.power_down = 0;
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
if (!enable) {
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
ESP_GOTO_ON_FALSE(bmcr.power_down == 1, ESP_FAIL, err, TAG, "power down failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
} else {
|
2020-07-30 03:23:36 -04:00
|
|
|
/* wait for power up complete */
|
|
|
|
uint32_t to = 0;
|
|
|
|
for (to = 0; to < dm9051->reset_timeout_ms / 10; to++) {
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2020-07-30 03:23:36 -04:00
|
|
|
if (bmcr.power_down == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(to < dm9051->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "power up timeout");
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_set_addr(esp_eth_phy_t *phy, uint32_t addr)
|
|
|
|
{
|
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
dm9051->addr = addr;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "addr can't be null");
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
*addr = dm9051->addr;
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_del(esp_eth_phy_t *phy)
|
|
|
|
{
|
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
2019-11-26 04:48:38 -05:00
|
|
|
free(dm9051);
|
2019-06-25 07:36:56 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:42:52 -04:00
|
|
|
static esp_err_t dm9051_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2020-07-20 08:42:52 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
|
|
|
/* Set PAUSE function ability */
|
|
|
|
anar_reg_t anar;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)), err, TAG, "read ANAR failed");
|
2020-07-20 08:42:52 -04:00
|
|
|
if (ability) {
|
|
|
|
anar.asymmetric_pause = 1;
|
|
|
|
anar.symmetric_pause = 1;
|
|
|
|
} else {
|
|
|
|
anar.asymmetric_pause = 0;
|
|
|
|
anar.symmetric_pause = 0;
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, dm9051->addr, ETH_PHY_ANAR_REG_ADDR, anar.val), err, TAG, "write ANAR failed");
|
2020-07-20 08:42:52 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-07-20 08:42:52 -04:00
|
|
|
}
|
|
|
|
|
2019-06-25 07:36:56 -04:00
|
|
|
static esp_err_t dm9051_init(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = dm9051->eth;
|
2019-12-23 04:06:02 -05:00
|
|
|
// Detect PHY address
|
|
|
|
if (dm9051->addr == ESP_ETH_PHY_ADDR_AUTO) {
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(esp_eth_detect_phy_addr(eth, &dm9051->addr), err, TAG, "Detect PHY address failed");
|
2019-12-23 04:06:02 -05:00
|
|
|
}
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Power on Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(dm9051_pwrctl(phy, true), err, TAG, "power control failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Reset Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(dm9051_reset(phy), err, TAG, "reset failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Check PHY ID */
|
|
|
|
phyidr1_reg_t id1;
|
|
|
|
phyidr2_reg_t id2;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)), err, TAG, "read ID1 failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)), err, TAG, "read ID2 failed");
|
|
|
|
ESP_GOTO_ON_FALSE(id1.oui_msb == 0x0181 && id2.oui_lsb == 0x2E && id2.vendor_model == 0x0A, ESP_FAIL, err, TAG, "wrong chip ID");
|
2019-06-25 07:36:56 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t dm9051_deinit(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-06-25 07:36:56 -04:00
|
|
|
/* Power off Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(dm9051_pwrctl(phy, false), err, TAG, "power control failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_eth_phy_t *esp_eth_phy_new_dm9051(const eth_phy_config_t *config)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_eth_phy_t *ret = NULL;
|
|
|
|
ESP_GOTO_ON_FALSE(config, NULL, err, TAG, "can't set phy config to null");
|
2019-06-25 07:36:56 -04:00
|
|
|
phy_dm9051_t *dm9051 = calloc(1, sizeof(phy_dm9051_t));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(dm9051, NULL, err, TAG, "calloc dm9051 failed");
|
2019-06-25 07:36:56 -04:00
|
|
|
dm9051->addr = config->phy_addr;
|
|
|
|
dm9051->reset_timeout_ms = config->reset_timeout_ms;
|
2019-11-13 23:03:14 -05:00
|
|
|
dm9051->reset_gpio_num = config->reset_gpio_num;
|
2019-06-25 07:36:56 -04:00
|
|
|
dm9051->link_status = ETH_LINK_DOWN;
|
|
|
|
dm9051->autonego_timeout_ms = config->autonego_timeout_ms;
|
|
|
|
dm9051->parent.reset = dm9051_reset;
|
2019-11-13 23:03:14 -05:00
|
|
|
dm9051->parent.reset_hw = dm9051_reset_hw;
|
2019-06-25 07:36:56 -04:00
|
|
|
dm9051->parent.init = dm9051_init;
|
|
|
|
dm9051->parent.deinit = dm9051_deinit;
|
|
|
|
dm9051->parent.set_mediator = dm9051_set_mediator;
|
|
|
|
dm9051->parent.negotiate = dm9051_negotiate;
|
|
|
|
dm9051->parent.get_link = dm9051_get_link;
|
|
|
|
dm9051->parent.pwrctl = dm9051_pwrctl;
|
|
|
|
dm9051->parent.get_addr = dm9051_get_addr;
|
|
|
|
dm9051->parent.set_addr = dm9051_set_addr;
|
2020-07-20 08:42:52 -04:00
|
|
|
dm9051->parent.advertise_pause_ability = dm9051_advertise_pause_ability;
|
2019-06-25 07:36:56 -04:00
|
|
|
dm9051->parent.del = dm9051_del;
|
|
|
|
return &(dm9051->parent);
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-06-25 07:36:56 -04:00
|
|
|
}
|