2021-11-04 09:10:19 +01:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
2019-04-10 16:24:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
#include "esp_log.h"
|
2021-04-01 20:00:54 +08:00
|
|
|
#include "esp_check.h"
|
2022-05-27 09:06:42 +02:00
|
|
|
#include "esp_eth_driver.h"
|
2019-04-10 16:24:50 +08:00
|
|
|
#include "eth_phy_regs_struct.h"
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
#include "freertos/task.h"
|
2019-11-14 12:03:14 +08:00
|
|
|
#include "driver/gpio.h"
|
2020-06-19 12:00:58 +08:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-21 13:07:34 +08:00
|
|
|
#include "esp_rom_sys.h"
|
2019-04-10 16:24:50 +08:00
|
|
|
|
|
|
|
|
static const char *TAG = "ip101";
|
|
|
|
|
|
|
|
|
|
/***************Vendor Specific Register***************/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief PCR(Page Control Register)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
typedef union {
|
|
|
|
|
struct {
|
|
|
|
|
uint32_t register_page_select : 5; /* Select register page, default is 16 */
|
|
|
|
|
uint32_t reserved : 11; /* Reserved */
|
|
|
|
|
};
|
|
|
|
|
uint32_t val;
|
|
|
|
|
} pcr_reg_t;
|
|
|
|
|
#define ETH_PHY_PCR_REG_ADDR (0x14)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief ISR(Interrupt Status Register), Page 16
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
typedef union {
|
|
|
|
|
struct {
|
|
|
|
|
uint32_t link_changed : 1; /* Flag to indicate link status change interrupt */
|
|
|
|
|
uint32_t duplex_changed : 1; /* Flag to indicate duplex change interrupt */
|
|
|
|
|
uint32_t speed_changed : 1; /* Flag to indicate speed change interrupt */
|
|
|
|
|
uint32_t intr_status : 1; /* Flag to indicate interrupt status */
|
|
|
|
|
uint32_t reserved1 : 4; /* Reserved */
|
|
|
|
|
uint32_t link_mask : 1; /* Mask link change interrupt */
|
|
|
|
|
uint32_t duplex_mask : 1; /* Mask duplex change interrupt */
|
|
|
|
|
uint32_t speed_mask : 1; /* Mask speed change interrupt */
|
|
|
|
|
uint32_t all_mask : 1; /* Mask all interrupt */
|
|
|
|
|
uint32_t reserved2 : 3; /* Reserved */
|
|
|
|
|
uint32_t use_intr_pin : 1; /* Set high to use INTR and INTR_32 as an interrupt pin */
|
|
|
|
|
};
|
|
|
|
|
uint32_t val;
|
|
|
|
|
} isr_reg_t;
|
|
|
|
|
#define ETH_PHY_ISR_REG_ADDR (0x11)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief PHY MDI/MDIX Control and Specific Status Register, Page 16
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
typedef union {
|
|
|
|
|
struct {
|
|
|
|
|
uint32_t op_mode : 3; /* Operation Mode Idicator */
|
|
|
|
|
uint32_t force_mdix : 1; /* Force the MDIX channel to be selected */
|
|
|
|
|
uint32_t reserved1 : 4; /* Reserved */
|
|
|
|
|
uint32_t link_up : 1; /* Indicate the link status is OK or FAIL */
|
|
|
|
|
uint32_t reserved2 : 7; /* Reserved */
|
|
|
|
|
};
|
|
|
|
|
uint32_t val;
|
|
|
|
|
} cssr_reg_t;
|
|
|
|
|
#define ETH_PHY_CSSR_REG_ADDR (0x1E)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief PSCR(PHY Specific Control Register), Page 1
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
typedef union {
|
|
|
|
|
struct {
|
|
|
|
|
uint32_t reserved1 : 7; /* Reserved */
|
|
|
|
|
uint32_t force_link_100 : 1; /* Force Link 100 */
|
|
|
|
|
uint32_t force_link_10 : 1; /* Force Link 10 */
|
|
|
|
|
uint32_t reserved2 : 7; /* Reserved */
|
|
|
|
|
};
|
|
|
|
|
uint32_t val;
|
|
|
|
|
} pscr_reg_t;
|
|
|
|
|
#define ETH_PHY_PSCR_REG_ADDR (0x11)
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
esp_eth_phy_t parent;
|
|
|
|
|
esp_eth_mediator_t *eth;
|
2020-11-17 12:48:35 +08:00
|
|
|
int addr;
|
2019-04-10 16:24:50 +08:00
|
|
|
uint32_t reset_timeout_ms;
|
|
|
|
|
uint32_t autonego_timeout_ms;
|
|
|
|
|
eth_link_t link_status;
|
2019-11-14 12:03:14 +08:00
|
|
|
int reset_gpio_num;
|
2019-04-10 16:24:50 +08:00
|
|
|
} phy_ip101_t;
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_page_select(phy_ip101_t *ip101, uint32_t page)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
pcr_reg_t pcr = {
|
|
|
|
|
.register_page_select = page
|
|
|
|
|
};
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_PCR_REG_ADDR, pcr.val), err, TAG, "write PCR failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-19 11:27:42 +08:00
|
|
|
static esp_err_t ip101_update_link_duplex_speed(phy_ip101_t *ip101)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-09-19 11:27:42 +08:00
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
eth_speed_t speed = ETH_SPEED_10M;
|
|
|
|
|
eth_duplex_t duplex = ETH_DUPLEX_HALF;
|
2020-07-20 20:42:52 +08:00
|
|
|
uint32_t peer_pause_ability = false;
|
2019-09-19 11:27:42 +08:00
|
|
|
cssr_reg_t cssr;
|
2020-07-20 20:42:52 +08:00
|
|
|
anlpar_reg_t anlpar;
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_page_select(ip101, 16), err, TAG, "select page 16 failed");
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_CSSR_REG_ADDR, &(cssr.val)), err, TAG, "read CSSR failed");
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)), err, TAG, "read ANLPAR failed");
|
2019-12-23 17:06:02 +08:00
|
|
|
eth_link_t link = cssr.link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
|
2019-09-19 11:27:42 +08:00
|
|
|
/* check if link status changed */
|
|
|
|
|
if (ip101->link_status != link) {
|
|
|
|
|
/* when link up, read negotiation result */
|
|
|
|
|
if (link == ETH_LINK_UP) {
|
|
|
|
|
switch (cssr.op_mode) {
|
|
|
|
|
case 1: //10M Half
|
|
|
|
|
speed = ETH_SPEED_10M;
|
|
|
|
|
duplex = ETH_DUPLEX_HALF;
|
|
|
|
|
break;
|
|
|
|
|
case 2: //100M Half
|
|
|
|
|
speed = ETH_SPEED_100M;
|
|
|
|
|
duplex = ETH_DUPLEX_HALF;
|
|
|
|
|
break;
|
|
|
|
|
case 5: //10M Full
|
|
|
|
|
speed = ETH_SPEED_10M;
|
|
|
|
|
duplex = ETH_DUPLEX_FULL;
|
|
|
|
|
break;
|
|
|
|
|
case 6: //100M Full
|
|
|
|
|
speed = ETH_SPEED_100M;
|
|
|
|
|
duplex = ETH_DUPLEX_FULL;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-01 20:00:54 +08: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 20:42:52 +08: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 20:00:54 +08: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-19 11:27:42 +08:00
|
|
|
}
|
2021-11-04 09:10:19 +01:00
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link), err, TAG, "change link failed");
|
2019-09-19 11:27:42 +08:00
|
|
|
ip101->link_status = link;
|
|
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-09-19 11:27:42 +08:00
|
|
|
}
|
|
|
|
|
|
2019-04-10 16:24:50 +08:00
|
|
|
static esp_err_t ip101_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08: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-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
ip101->eth = eth;
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_get_link(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
2021-11-04 09:10:19 +01:00
|
|
|
/* Update information about link, speed, duplex */
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_update_link_duplex_speed(ip101), err, TAG, "update link duplex speed failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_reset(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
2019-12-03 15:37:34 +08:00
|
|
|
ip101->link_status = ETH_LINK_DOWN;
|
2019-04-10 16:24:50 +08:00
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
bmcr_reg_t bmcr = {.reset = 1};
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
/* wait for reset complete */
|
2019-09-19 11:27:42 +08:00
|
|
|
uint32_t to = 0;
|
2019-04-10 16:24:50 +08:00
|
|
|
for (to = 0; to < ip101->reset_timeout_ms / 10; to++) {
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
if (!bmcr.reset) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_FALSE(to < ip101->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "reset timeout");
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-14 12:03:14 +08:00
|
|
|
static esp_err_t ip101_reset_hw(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
if (ip101->reset_gpio_num >= 0) {
|
2020-06-19 12:00:58 +08:00
|
|
|
esp_rom_gpio_pad_select_gpio(ip101->reset_gpio_num);
|
2019-11-14 12:03:14 +08:00
|
|
|
gpio_set_direction(ip101->reset_gpio_num, GPIO_MODE_OUTPUT);
|
|
|
|
|
gpio_set_level(ip101->reset_gpio_num, 0);
|
2020-07-21 13:07:34 +08:00
|
|
|
esp_rom_delay_us(100); // insert min input assert time
|
2019-11-14 12:03:14 +08:00
|
|
|
gpio_set_level(ip101->reset_gpio_num, 1);
|
|
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 13:40:26 +08:00
|
|
|
/**
|
|
|
|
|
* @note This function is responsible for restarting a new auto-negotiation,
|
|
|
|
|
* the result of negotiation won't be relected to uppler layers.
|
|
|
|
|
* Instead, the negotiation result is fetched by linker timer, see `ip101_get_link()`
|
|
|
|
|
*/
|
2021-11-04 09:10:19 +01:00
|
|
|
static esp_err_t ip101_autonego_ctrl(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd, bool *autonego_en_stat)
|
2019-04-10 16:24:50 +08:00
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
2021-11-04 09:10:19 +01:00
|
|
|
|
|
|
|
|
bmcr_reg_t bmcr;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
case ESP_ETH_PHY_AUTONEGO_RESTART:
|
|
|
|
|
ESP_GOTO_ON_FALSE(bmcr.en_auto_nego, ESP_ERR_INVALID_STATE, err, TAG, "auto negotiation is disabled");
|
|
|
|
|
/* in case any link status has changed, let's assume we're in link down status */
|
|
|
|
|
ip101->link_status = ETH_LINK_DOWN;
|
|
|
|
|
|
|
|
|
|
bmcr.restart_auto_nego = 1; /* Restart Auto Negotiation */
|
|
|
|
|
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
/* Wait for auto negotiation complete */
|
|
|
|
|
bmsr_reg_t bmsr;
|
|
|
|
|
uint32_t to = 0;
|
|
|
|
|
for (to = 0; to < ip101->autonego_timeout_ms / 100; to++) {
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
|
|
|
|
if (bmsr.auto_nego_complete) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
2021-11-04 09:10:19 +01:00
|
|
|
if ((to >= ip101->autonego_timeout_ms / 100) && (ip101->link_status == ETH_LINK_UP)) {
|
|
|
|
|
ESP_LOGW(TAG, "auto negotiation timeout");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ESP_ETH_PHY_AUTONEGO_DIS:
|
|
|
|
|
if (bmcr.en_auto_nego == 1) {
|
|
|
|
|
bmcr.en_auto_nego = 0; /* Disable Auto Negotiation */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
/* read configuration back */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
ESP_GOTO_ON_FALSE(bmcr.en_auto_nego == 0, ESP_FAIL, err, TAG, "disable auto-negotiation failed");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ESP_ETH_PHY_AUTONEGO_EN:
|
|
|
|
|
if (bmcr.en_auto_nego == 0) {
|
|
|
|
|
bmcr.en_auto_nego = 1; /* Enable Auto Negotiation */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
/* read configuration back */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
ESP_GOTO_ON_FALSE(bmcr.en_auto_nego == 1, ESP_FAIL, err, TAG, "enable auto-negotiation failed");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ESP_ETH_PHY_AUTONEGO_G_STAT:
|
|
|
|
|
/* do nothing autonego_en_stat is set at the function end */
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
2021-11-04 09:10:19 +01:00
|
|
|
|
|
|
|
|
*autonego_en_stat = bmcr.en_auto_nego;
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_pwrctl(esp_eth_phy_t *phy, bool enable)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
bmcr_reg_t bmcr;
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2019-04-10 16:24:50 +08: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 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
if (!enable) {
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->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-04-10 16:24:50 +08:00
|
|
|
} else {
|
2020-07-30 15:23:36 +08:00
|
|
|
/* wait for power up complete */
|
|
|
|
|
uint32_t to = 0;
|
|
|
|
|
for (to = 0; to < ip101->reset_timeout_ms / 10; to++) {
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2020-07-30 15:23:36 +08:00
|
|
|
if (bmcr.power_down == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_FALSE(to < ip101->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "power up timeout");
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_set_addr(esp_eth_phy_t *phy, uint32_t addr)
|
|
|
|
|
{
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
ip101->addr = addr;
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "addr can't be null");
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
*addr = ip101->addr;
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_del(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
2019-11-26 17:48:38 +08:00
|
|
|
free(ip101);
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 20:42:52 +08:00
|
|
|
static esp_err_t ip101_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2020-07-20 20:42:52 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
/* Set PAUSE function ability */
|
|
|
|
|
anar_reg_t anar;
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)), err, TAG, "read ANAR failed");
|
2020-07-20 20:42:52 +08:00
|
|
|
if (ability) {
|
|
|
|
|
anar.asymmetric_pause = 1;
|
|
|
|
|
anar.symmetric_pause = 1;
|
|
|
|
|
} else {
|
|
|
|
|
anar.asymmetric_pause = 0;
|
|
|
|
|
anar.symmetric_pause = 0;
|
|
|
|
|
}
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val), err, TAG, "write ANAR failed");
|
2020-07-20 20:42:52 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2020-07-20 20:42:52 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:24:01 +02:00
|
|
|
static esp_err_t ip101_loopback(esp_eth_phy_t *phy, bool enable)
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
/* Set Loopback function */
|
|
|
|
|
bmcr_reg_t bmcr;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
if (enable) {
|
|
|
|
|
bmcr.en_loopback = 1;
|
|
|
|
|
} else {
|
|
|
|
|
bmcr.en_loopback = 0;
|
|
|
|
|
}
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-04 09:10:19 +01:00
|
|
|
static esp_err_t ip101_set_speed(esp_eth_phy_t *phy, eth_speed_t speed)
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
|
|
|
|
|
if (ip101->link_status == ETH_LINK_UP) {
|
|
|
|
|
/* Since the link is going to be reconfigured, consider it down for a while */
|
|
|
|
|
ip101->link_status = ETH_LINK_DOWN;
|
|
|
|
|
/* Indicate to upper stream apps the link is cosidered down */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)ip101->link_status), err, TAG, "change link failed");
|
|
|
|
|
}
|
|
|
|
|
/* Set speed */
|
|
|
|
|
bmcr_reg_t bmcr;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
bmcr.speed_select = speed;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_set_duplex(esp_eth_phy_t *phy, eth_duplex_t duplex)
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
|
|
|
|
|
|
|
|
|
if (ip101->link_status == ETH_LINK_UP) {
|
|
|
|
|
/* Since the link is going to be reconfigured, consider it down for a while */
|
|
|
|
|
ip101->link_status = ETH_LINK_DOWN;
|
|
|
|
|
/* Indicate to upper stream apps the link is cosidered down */
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)ip101->link_status), err, TAG, "change link failed");
|
|
|
|
|
}
|
|
|
|
|
/* Set duplex mode */
|
|
|
|
|
bmcr_reg_t bmcr;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
|
|
|
|
bmcr.duplex_mode = duplex;
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 16:24:50 +08:00
|
|
|
static esp_err_t ip101_init(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent);
|
|
|
|
|
esp_eth_mediator_t *eth = ip101->eth;
|
2019-12-23 17:06:02 +08:00
|
|
|
// Detect PHY address
|
|
|
|
|
if (ip101->addr == ESP_ETH_PHY_ADDR_AUTO) {
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(esp_eth_detect_phy_addr(eth, &ip101->addr), err, TAG, "Detect PHY address failed");
|
2019-12-23 17:06:02 +08:00
|
|
|
}
|
2019-04-10 16:24:50 +08:00
|
|
|
/* Power on Ethernet PHY */
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_pwrctl(phy, true), err, TAG, "power control failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
/* Reset Ethernet PHY */
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_reset(phy), err, TAG, "reset failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
/* Check PHY ID */
|
|
|
|
|
phyidr1_reg_t id1;
|
|
|
|
|
phyidr2_reg_t id2;
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)), err, TAG, "read ID1 failed");
|
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)), err, TAG, "read ID2 failed");
|
|
|
|
|
ESP_GOTO_ON_FALSE(id1.oui_msb == 0x243 && id2.oui_lsb == 0x3 && id2.vendor_model == 0x5, ESP_FAIL, err, TAG, "wrong chip ID");
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static esp_err_t ip101_deinit(esp_eth_phy_t *phy)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_err_t ret = ESP_OK;
|
2019-04-10 16:24:50 +08:00
|
|
|
/* Power off Ethernet PHY */
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_pwrctl(phy, false), err, TAG, "power control failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
return ESP_OK;
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config)
|
|
|
|
|
{
|
2021-04-01 20:00:54 +08:00
|
|
|
esp_eth_phy_t *ret = NULL;
|
|
|
|
|
ESP_GOTO_ON_FALSE(config, NULL, err, TAG, "can't set phy config to null");
|
2019-04-10 16:24:50 +08:00
|
|
|
phy_ip101_t *ip101 = calloc(1, sizeof(phy_ip101_t));
|
2021-04-01 20:00:54 +08:00
|
|
|
ESP_GOTO_ON_FALSE(ip101, NULL, err, TAG, "calloc ip101 failed");
|
2019-04-10 16:24:50 +08:00
|
|
|
ip101->addr = config->phy_addr;
|
|
|
|
|
ip101->reset_timeout_ms = config->reset_timeout_ms;
|
2019-11-14 12:03:14 +08:00
|
|
|
ip101->reset_gpio_num = config->reset_gpio_num;
|
2019-04-10 16:24:50 +08:00
|
|
|
ip101->link_status = ETH_LINK_DOWN;
|
|
|
|
|
ip101->autonego_timeout_ms = config->autonego_timeout_ms;
|
|
|
|
|
ip101->parent.reset = ip101_reset;
|
2019-11-14 12:03:14 +08:00
|
|
|
ip101->parent.reset_hw = ip101_reset_hw;
|
2019-04-10 16:24:50 +08:00
|
|
|
ip101->parent.init = ip101_init;
|
|
|
|
|
ip101->parent.deinit = ip101_deinit;
|
|
|
|
|
ip101->parent.set_mediator = ip101_set_mediator;
|
2021-11-04 09:10:19 +01:00
|
|
|
ip101->parent.autonego_ctrl = ip101_autonego_ctrl;
|
2019-04-10 16:24:50 +08:00
|
|
|
ip101->parent.get_link = ip101_get_link;
|
|
|
|
|
ip101->parent.pwrctl = ip101_pwrctl;
|
|
|
|
|
ip101->parent.get_addr = ip101_get_addr;
|
|
|
|
|
ip101->parent.set_addr = ip101_set_addr;
|
2020-07-20 20:42:52 +08:00
|
|
|
ip101->parent.advertise_pause_ability = ip101_advertise_pause_ability;
|
2021-09-03 17:24:01 +02:00
|
|
|
ip101->parent.loopback = ip101_loopback;
|
2021-11-04 09:10:19 +01:00
|
|
|
ip101->parent.set_speed = ip101_set_speed;
|
|
|
|
|
ip101->parent.set_duplex = ip101_set_duplex;
|
2019-04-10 16:24:50 +08:00
|
|
|
ip101->parent.del = ip101_del;
|
|
|
|
|
|
|
|
|
|
return &(ip101->parent);
|
|
|
|
|
err:
|
2021-04-01 20:00:54 +08:00
|
|
|
return ret;
|
2019-04-10 16:24:50 +08:00
|
|
|
}
|