2021-10-27 17:08:23 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-06-14 21:59:20 -04:00
|
|
|
#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"
|
2020-06-14 21:59:20 -04:00
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "eth_phy_regs_struct.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "driver/gpio.h"
|
2020-07-09 09:49:07 -04:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2020-06-14 21:59:20 -04:00
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static const char *TAG = "ksz80xx";
|
2020-06-14 21:59:20 -04:00
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
#define KSZ8041_MODEL_ID (0x11)
|
|
|
|
#define KSZ8081_MODEL_ID (0x16)
|
2021-10-27 17:08:23 -04:00
|
|
|
#define KSZ8091_MODEL_ID KSZ8081_MODEL_ID
|
2021-03-15 00:50:39 -04:00
|
|
|
|
2020-06-14 21:59:20 -04:00
|
|
|
/***************Vendor Specific Register***************/
|
|
|
|
/**
|
2021-03-15 00:50:39 -04:00
|
|
|
* @brief PC2R(PHY Control 2 Register) for KSZ8041
|
2020-06-14 21:59:20 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
struct {
|
2021-03-12 01:35:42 -05:00
|
|
|
uint32_t dis_data_scr: 1; /* Disable Scrambler */
|
|
|
|
uint32_t en_sqe_test : 1; /* Enable SQE Test */
|
|
|
|
uint32_t op_mode : 3; /* Operation Mode */
|
|
|
|
uint32_t phy_iso : 1; /* PHY Isolate */
|
|
|
|
uint32_t en_flow_ctrl : 1; /* Enable Flow Control */
|
|
|
|
uint32_t auto_nego_comp : 1; /* Auto Negotiation Complete */
|
|
|
|
uint32_t en_jabber : 1; /* Enable Jabber Counter */
|
|
|
|
uint32_t irq_level : 1; /* Interrupt Pin Active Level */
|
|
|
|
uint32_t power_saving : 1; /* Enable Powering Saving */
|
|
|
|
uint32_t force_link : 1; /* Force Link Pass */
|
|
|
|
uint32_t energy_det : 1; /* Presence of Signal on RX+/- Wire Pair */
|
2021-10-27 17:08:23 -04:00
|
|
|
uint32_t pair_swap_dis : 1; /* Disable Auto MDI/MDI-X */
|
2021-03-12 01:35:42 -05:00
|
|
|
uint32_t mdix_select : 1; /* MDI/MDI-X Select */
|
|
|
|
uint32_t hp_mdix : 1; /* HP Auto MDI/MDI-X Mode */
|
2020-06-14 21:59:20 -04:00
|
|
|
};
|
|
|
|
uint32_t val;
|
2021-03-15 00:50:39 -04:00
|
|
|
} ksz8041_pc2r_reg_t;
|
2021-10-27 17:08:23 -04:00
|
|
|
#define KSZ8041_PC2R_REG_ADDR (0x1F)
|
2021-03-15 00:50:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PC1R(PHY Control 1 Register) for KSZ8081
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
struct {
|
|
|
|
uint32_t op_mode : 3; /* Operation Mode Indication */
|
|
|
|
uint32_t phy_iso : 1; /* PHY in Isolate Mode */
|
|
|
|
uint32_t energy_det: 1; /* Signal presence on RX pair */
|
|
|
|
uint32_t mdix_state : 1; /* MDI/MDI-X state */
|
|
|
|
uint32_t reserved_6 : 1; /* Reserved */
|
|
|
|
uint32_t polarity_status : 1; /* Polarity status */
|
|
|
|
uint32_t link_status : 1; /* Link status */
|
|
|
|
uint32_t en_flow_ctrl : 1; /* Flow control */
|
|
|
|
uint32_t reserved_15_10 : 6; /* Reserved */
|
|
|
|
};
|
|
|
|
uint32_t val;
|
|
|
|
} ksz8081_pc1r_reg_t;
|
|
|
|
#define KSZ8081_PC1R_REG_ADDR (0x1E)
|
2020-06-14 21:59:20 -04:00
|
|
|
|
2021-10-27 17:08:23 -04:00
|
|
|
/**
|
|
|
|
* @brief PC2R(PHY Control 2 Register)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
struct {
|
|
|
|
uint32_t dis_data_scr: 1; /* Disable Scrambler */
|
|
|
|
uint32_t enable_sqe_test : 1; /* Enable SQE test. MLX/MNX/RNB only. */
|
|
|
|
uint32_t rem_loopb : 1; /* Enable remote loopback */
|
|
|
|
uint32_t dis_trx : 1; /* Disable Transmitter */
|
|
|
|
uint32_t led_mode : 2; /* LED mode link-activity / link */
|
|
|
|
uint32_t reserved_6 : 1;
|
|
|
|
uint32_t rmii_ref_sel : 1; /* RMII Reference Clock Select */
|
|
|
|
uint32_t en_jabber : 1; /* Enable Jabber Counter */
|
|
|
|
uint32_t irq_level : 1; /* Interrupt Pin Active Level */
|
|
|
|
uint32_t power_saving : 1; /* Enable Powering Saving */
|
|
|
|
uint32_t force_link : 1; /* Force Link Pass */
|
|
|
|
uint32_t reserved_12 : 1;
|
|
|
|
uint32_t pair_swap_dis : 1; /* Disable Auto MDI/MDI-X */
|
|
|
|
uint32_t mdix_select : 1; /* MDI/MDI-X Select */
|
|
|
|
uint32_t hp_mdix : 1; /* HP Auto MDI/MDI-X Mode */
|
|
|
|
};
|
|
|
|
uint32_t val;
|
|
|
|
} ksz8081_pc2r_reg_t;
|
|
|
|
#define KSZ8081_PC2R_REG_ADDR (0x1F)
|
|
|
|
|
|
|
|
|
2020-06-14 21:59:20 -04:00
|
|
|
typedef struct {
|
|
|
|
esp_eth_phy_t parent;
|
|
|
|
esp_eth_mediator_t *eth;
|
2020-11-16 23:48:35 -05:00
|
|
|
int addr;
|
2020-06-14 21:59:20 -04:00
|
|
|
uint32_t reset_timeout_ms;
|
|
|
|
uint32_t autonego_timeout_ms;
|
|
|
|
eth_link_t link_status;
|
|
|
|
int reset_gpio_num;
|
2021-03-15 00:50:39 -04:00
|
|
|
int vendor_model;
|
|
|
|
} phy_ksz80xx_t;
|
2020-06-14 21:59:20 -04:00
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_update_link_duplex_speed(phy_ksz80xx_t *ksz80xx)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
2020-06-14 21:59:20 -04:00
|
|
|
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;
|
|
|
|
anlpar_reg_t anlpar;
|
2020-06-14 21:59:20 -04:00
|
|
|
bmsr_reg_t bmsr;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)), err, TAG, "read ANLPAR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
|
|
|
|
/* check if link status changed */
|
2021-03-15 00:50:39 -04:00
|
|
|
if (ksz80xx->link_status != link) {
|
2020-06-14 21:59:20 -04:00
|
|
|
/* when link up, read negotiation result */
|
|
|
|
if (link == ETH_LINK_UP) {
|
2021-03-15 00:50:39 -04:00
|
|
|
int op_mode = 0;
|
|
|
|
if (ksz80xx->vendor_model == KSZ8041_MODEL_ID) {
|
|
|
|
ksz8041_pc2r_reg_t pc2r;
|
2021-10-27 17:08:23 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, KSZ8041_PC2R_REG_ADDR, &(pc2r.val)), err, TAG, "read PC2R failed");
|
2021-03-15 00:50:39 -04:00
|
|
|
op_mode = pc2r.op_mode;
|
|
|
|
} else if (ksz80xx->vendor_model == KSZ8081_MODEL_ID) {
|
|
|
|
ksz8081_pc1r_reg_t pc1r;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, KSZ8081_PC1R_REG_ADDR, &(pc1r.val)), err, TAG, "read PC1R failed");
|
2021-03-15 00:50:39 -04:00
|
|
|
op_mode = pc1r.op_mode;
|
|
|
|
}
|
|
|
|
switch (op_mode) {
|
2020-06-14 21:59:20 -04:00
|
|
|
case 1: //10Base-T half-duplex
|
|
|
|
speed = ETH_SPEED_10M;
|
|
|
|
duplex = ETH_DUPLEX_HALF;
|
|
|
|
break;
|
|
|
|
case 2: //100Base-TX half-duplex
|
|
|
|
speed = ETH_SPEED_100M;
|
|
|
|
duplex = ETH_DUPLEX_HALF;
|
|
|
|
break;
|
|
|
|
case 5: //10Base-T full-duplex
|
|
|
|
speed = ETH_SPEED_10M;
|
|
|
|
duplex = ETH_DUPLEX_FULL;
|
|
|
|
break;
|
|
|
|
case 6: //100Base-TX full-duplex
|
|
|
|
speed = ETH_SPEED_100M;
|
|
|
|
duplex = ETH_DUPLEX_FULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
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");
|
2020-06-14 21:59:20 -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");
|
2021-03-15 00:50:39 -04:00
|
|
|
ksz80xx->link_status = link;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
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");
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
ksz80xx->eth = eth;
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_get_link(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Update information about link, speed, duplex */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ksz80xx_update_link_duplex_speed(ksz80xx), err, TAG, "update link duplex speed failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_reset(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
ksz80xx->link_status = ETH_LINK_DOWN;
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
2020-06-14 21:59:20 -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, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
/* wait for reset complete */
|
|
|
|
uint32_t to = 0;
|
2021-03-15 00:50:39 -04:00
|
|
|
for (to = 0; to < ksz80xx->reset_timeout_ms / 10; to++) {
|
2020-06-14 21:59:20 -04:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
if (!bmcr.reset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(to < ksz80xx->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "reset timeout");
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_reset_hw(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
if (ksz80xx->reset_gpio_num >= 0) {
|
|
|
|
esp_rom_gpio_pad_select_gpio(ksz80xx->reset_gpio_num);
|
|
|
|
gpio_set_direction(ksz80xx->reset_gpio_num, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_level(ksz80xx->reset_gpio_num, 0);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(100); // insert min input assert time
|
2021-03-15 00:50:39 -04:00
|
|
|
gpio_set_level(ksz80xx->reset_gpio_num, 1);
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-06-17 01:40:26 -04: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 `ksz80xx_get_link()`
|
|
|
|
*/
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_negotiate(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->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 */
|
|
|
|
ksz80xx->link_status = ETH_LINK_DOWN;
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Restart 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, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Wait for auto negotiation complete */
|
|
|
|
bmsr_reg_t bmsr;
|
2020-11-16 23:48:35 -05:00
|
|
|
uint32_t to = 0;
|
2021-05-06 08:16:12 -04:00
|
|
|
for (to = 0; to < ksz80xx->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, ksz80xx->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
|
2021-03-15 00:50:39 -04:00
|
|
|
if (bmsr.auto_nego_complete) {
|
2020-06-14 21:59:20 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 01:40:26 -04:00
|
|
|
if ((to >= ksz80xx->autonego_timeout_ms / 100) && (ksz80xx->link_status == ETH_LINK_UP)) {
|
2020-06-14 21:59:20 -04:00
|
|
|
ESP_LOGW(TAG, "auto negotiation timeout");
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_pwrctl(esp_eth_phy_t *phy, bool enable)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
2020-06-14 21:59:20 -04:00
|
|
|
bmcr_reg_t bmcr;
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
if (!enable) {
|
|
|
|
/* General Power Down Mode */
|
|
|
|
bmcr.power_down = 1;
|
|
|
|
} else {
|
|
|
|
/* Normal operation Mode */
|
|
|
|
bmcr.power_down = 0;
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
if (!enable) {
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->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");
|
2020-06-14 21:59:20 -04:00
|
|
|
} else {
|
2020-07-30 03:23:36 -04:00
|
|
|
/* wait for power up complete */
|
|
|
|
uint32_t to = 0;
|
2021-03-15 00:50:39 -04:00
|
|
|
for (to = 0; to < ksz80xx->reset_timeout_ms / 10; to++) {
|
2020-07-30 03:23:36 -04:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->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 < ksz80xx->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "power up timeout");
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_set_addr(esp_eth_phy_t *phy, uint32_t addr)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
ksz80xx->addr = addr;
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
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");
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
*addr = ksz80xx->addr;
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_del(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
free(ksz80xx);
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
|
2020-07-20 08:42:52 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
2020-07-20 08:42:52 -04:00
|
|
|
/* 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, ksz80xx->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, ksz80xx->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
|
|
|
}
|
|
|
|
|
2021-09-03 11:24:01 -04:00
|
|
|
static esp_err_t ksz80xx_loopback(esp_eth_phy_t *phy, bool enable)
|
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
|
|
|
/* Set Loopback function */
|
|
|
|
bmcr_reg_t bmcr;
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->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, ksz80xx->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_init(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz80xx = __containerof(phy, phy_ksz80xx_t, parent);
|
|
|
|
esp_eth_mediator_t *eth = ksz80xx->eth;
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Power on Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ksz80xx_pwrctl(phy, true), err, TAG, "power control failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Reset Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ksz80xx_reset(phy), err, TAG, "reset failed");
|
2020-06-14 21:59:20 -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, ksz80xx->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)), err, TAG, "read ID1 failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, ksz80xx->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)), err, TAG, "read ID2 failed");
|
|
|
|
ESP_GOTO_ON_FALSE(id1.oui_msb == 0x22 && id2.oui_lsb == 0x5 && id2.vendor_model == ksz80xx->vendor_model, ESP_FAIL, err, TAG, "wrong chip ID");
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 00:50:39 -04:00
|
|
|
static esp_err_t ksz80xx_deinit(esp_eth_phy_t *phy)
|
2020-06-14 21:59:20 -04:00
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2020-06-14 21:59:20 -04:00
|
|
|
/* Power off Ethernet PHY */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ksz80xx_pwrctl(phy, false), err, TAG, "power control failed");
|
2020-06-14 21:59:20 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_eth_phy_t *esp_eth_phy_new_ksz8041(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");
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz8041 = calloc(1, sizeof(phy_ksz80xx_t));
|
2021-10-27 17:08:23 -04:00
|
|
|
ESP_GOTO_ON_FALSE(ksz8041, NULL, err, TAG, "calloc ksz8041 failed");
|
2021-03-15 00:50:39 -04:00
|
|
|
ksz8041->vendor_model = KSZ8041_MODEL_ID;
|
2020-06-14 21:59:20 -04:00
|
|
|
ksz8041->addr = config->phy_addr;
|
|
|
|
ksz8041->reset_gpio_num = config->reset_gpio_num;
|
|
|
|
ksz8041->reset_timeout_ms = config->reset_timeout_ms;
|
|
|
|
ksz8041->link_status = ETH_LINK_DOWN;
|
|
|
|
ksz8041->autonego_timeout_ms = config->autonego_timeout_ms;
|
2021-03-15 00:50:39 -04:00
|
|
|
ksz8041->parent.reset = ksz80xx_reset;
|
|
|
|
ksz8041->parent.reset_hw = ksz80xx_reset_hw;
|
|
|
|
ksz8041->parent.init = ksz80xx_init;
|
|
|
|
ksz8041->parent.deinit = ksz80xx_deinit;
|
|
|
|
ksz8041->parent.set_mediator = ksz80xx_set_mediator;
|
|
|
|
ksz8041->parent.negotiate = ksz80xx_negotiate;
|
|
|
|
ksz8041->parent.get_link = ksz80xx_get_link;
|
|
|
|
ksz8041->parent.pwrctl = ksz80xx_pwrctl;
|
|
|
|
ksz8041->parent.get_addr = ksz80xx_get_addr;
|
|
|
|
ksz8041->parent.set_addr = ksz80xx_set_addr;
|
|
|
|
ksz8041->parent.advertise_pause_ability = ksz80xx_advertise_pause_ability;
|
2021-09-03 11:24:01 -04:00
|
|
|
ksz8041->parent.loopback = ksz80xx_loopback;
|
2021-03-15 00:50:39 -04:00
|
|
|
ksz8041->parent.del = ksz80xx_del;
|
2020-06-14 21:59:20 -04:00
|
|
|
return &(ksz8041->parent);
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2020-06-14 21:59:20 -04:00
|
|
|
}
|
2021-03-15 00:50:39 -04:00
|
|
|
|
|
|
|
esp_eth_phy_t *esp_eth_phy_new_ksz8081(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");
|
2021-03-15 00:50:39 -04:00
|
|
|
phy_ksz80xx_t *ksz8081 = calloc(1, sizeof(phy_ksz80xx_t));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(ksz8081, NULL, err, TAG, "calloc ksz8081 failed");
|
2021-03-15 00:50:39 -04:00
|
|
|
ksz8081->vendor_model = KSZ8081_MODEL_ID;
|
|
|
|
ksz8081->addr = config->phy_addr;
|
|
|
|
ksz8081->reset_gpio_num = config->reset_gpio_num;
|
|
|
|
ksz8081->reset_timeout_ms = config->reset_timeout_ms;
|
|
|
|
ksz8081->link_status = ETH_LINK_DOWN;
|
|
|
|
ksz8081->autonego_timeout_ms = config->autonego_timeout_ms;
|
|
|
|
ksz8081->parent.reset = ksz80xx_reset;
|
|
|
|
ksz8081->parent.reset_hw = ksz80xx_reset_hw;
|
|
|
|
ksz8081->parent.init = ksz80xx_init;
|
|
|
|
ksz8081->parent.deinit = ksz80xx_deinit;
|
|
|
|
ksz8081->parent.set_mediator = ksz80xx_set_mediator;
|
|
|
|
ksz8081->parent.negotiate = ksz80xx_negotiate;
|
|
|
|
ksz8081->parent.get_link = ksz80xx_get_link;
|
|
|
|
ksz8081->parent.pwrctl = ksz80xx_pwrctl;
|
|
|
|
ksz8081->parent.get_addr = ksz80xx_get_addr;
|
|
|
|
ksz8081->parent.set_addr = ksz80xx_set_addr;
|
|
|
|
ksz8081->parent.advertise_pause_ability = ksz80xx_advertise_pause_ability;
|
|
|
|
ksz8081->parent.del = ksz80xx_del;
|
|
|
|
return &(ksz8081->parent);
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2021-03-15 00:50:39 -04:00
|
|
|
}
|