2021-11-04 04:10:19 -04:00
|
|
|
/*
|
2024-02-07 05:24:17 -05:00
|
|
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
2021-11-04 04:10:19 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-04-10 04:24:50 -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"
|
2022-05-13 06:03:56 -04:00
|
|
|
#include "esp_eth_phy_802_3.h"
|
2019-04-10 04:24:50 -04:00
|
|
|
|
|
|
|
static const char *TAG = "ip101";
|
|
|
|
|
2024-02-07 05:24:17 -05:00
|
|
|
#define IP101_PHY_RESET_ASSERTION_TIME_US 10000
|
|
|
|
#define IP101_PHY_POST_RESET_INIT_TIME_MS 10
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
/***************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 {
|
2022-05-13 06:03:56 -04:00
|
|
|
phy_802_3_t phy_802_3;
|
2019-04-10 04:24:50 -04:00
|
|
|
} phy_ip101_t;
|
|
|
|
|
|
|
|
static esp_err_t ip101_page_select(phy_ip101_t *ip101, uint32_t page)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2022-05-13 06:03:56 -04:00
|
|
|
esp_eth_mediator_t *eth = ip101->phy_802_3.eth;
|
2019-04-10 04:24:50 -04:00
|
|
|
pcr_reg_t pcr = {
|
|
|
|
.register_page_select = page
|
|
|
|
};
|
2022-05-13 06:03:56 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, ip101->phy_802_3.addr, ETH_PHY_PCR_REG_ADDR, pcr.val), err, TAG, "write PCR failed");
|
2019-04-10 04:24:50 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 23:27:42 -04:00
|
|
|
static esp_err_t ip101_update_link_duplex_speed(phy_ip101_t *ip101)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2022-05-13 06:03:56 -04:00
|
|
|
esp_eth_mediator_t *eth = ip101->phy_802_3.eth;
|
|
|
|
uint32_t addr = ip101->phy_802_3.addr;
|
2019-09-18 23:27:42 -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;
|
2019-09-18 23:27:42 -04:00
|
|
|
cssr_reg_t cssr;
|
2020-07-20 08:42:52 -04:00
|
|
|
anlpar_reg_t anlpar;
|
2022-05-13 06:03:56 -04:00
|
|
|
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_page_select(ip101, 16), err, TAG, "select page 16 failed");
|
2022-05-13 06:03:56 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, addr, ETH_PHY_CSSR_REG_ADDR, &(cssr.val)), err, TAG, "read CSSR failed");
|
|
|
|
ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)), err, TAG, "read ANLPAR failed");
|
2019-12-23 04:06:02 -05:00
|
|
|
eth_link_t link = cssr.link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
|
2019-09-18 23:27:42 -04:00
|
|
|
/* check if link status changed */
|
2022-05-13 06:03:56 -04:00
|
|
|
if (ip101->phy_802_3.link_status != link) {
|
2019-09-18 23:27:42 -04:00
|
|
|
/* 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 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-11-04 04:10:19 -04:00
|
|
|
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link), err, TAG, "change link failed");
|
2022-05-13 06:03:56 -04:00
|
|
|
ip101->phy_802_3.link_status = link;
|
2019-09-18 23:27:42 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-09-18 23:27:42 -04:00
|
|
|
}
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static esp_err_t ip101_get_link(esp_eth_phy_t *phy)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2022-05-13 06:03:56 -04:00
|
|
|
phy_ip101_t *ip101 = __containerof(esp_eth_phy_into_phy_802_3(phy), phy_ip101_t, phy_802_3);
|
|
|
|
|
2021-11-04 04:10:19 -04:00
|
|
|
/* Update information about link, speed, duplex */
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ip101_update_link_duplex_speed(ip101), err, TAG, "update link duplex speed failed");
|
2019-04-10 04:24:50 -04:00
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|
|
|
|
|
2024-02-07 05:24:17 -05:00
|
|
|
static esp_err_t ip101_reset_hw(esp_eth_phy_t *phy)
|
|
|
|
{
|
|
|
|
phy_802_3_t *phy_802_3 = esp_eth_phy_into_phy_802_3(phy);
|
|
|
|
esp_err_t ret = esp_eth_phy_802_3_reset_hw(phy_802_3, IP101_PHY_RESET_ASSERTION_TIME_US);
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(IP101_PHY_POST_RESET_INIT_TIME_MS));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-13 06:03:56 -04:00
|
|
|
static esp_err_t ip101_init(esp_eth_phy_t *phy)
|
2021-11-04 04:10:19 -04:00
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_OK;
|
2022-05-13 06:03:56 -04:00
|
|
|
phy_802_3_t *phy_802_3 = esp_eth_phy_into_phy_802_3(phy);
|
2021-11-04 04:10:19 -04:00
|
|
|
|
2022-05-13 06:03:56 -04:00
|
|
|
/* Basic PHY init */
|
|
|
|
ESP_GOTO_ON_ERROR(esp_eth_phy_802_3_basic_phy_init(phy_802_3), err, TAG, "failed to init PHY");
|
2021-11-04 04:10:19 -04:00
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
/* Check PHY ID */
|
2022-05-13 06:03:56 -04:00
|
|
|
uint32_t oui;
|
|
|
|
uint8_t model;
|
|
|
|
ESP_GOTO_ON_ERROR(esp_eth_phy_802_3_read_oui(phy_802_3, &oui), err, TAG, "read OUI failed");
|
|
|
|
ESP_GOTO_ON_ERROR(esp_eth_phy_802_3_read_manufac_info(phy_802_3, &model, NULL), err, TAG, "read manufacturer's info failed");
|
|
|
|
ESP_GOTO_ON_FALSE(oui == 0x90C3 && model == 0x5, ESP_FAIL, err, TAG, "wrong chip ID");
|
2019-04-10 04:24:50 -04:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
err:
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config)
|
|
|
|
{
|
2021-04-01 08:00:54 -04:00
|
|
|
esp_eth_phy_t *ret = NULL;
|
2019-04-10 04:24:50 -04:00
|
|
|
phy_ip101_t *ip101 = calloc(1, sizeof(phy_ip101_t));
|
2021-04-01 08:00:54 -04:00
|
|
|
ESP_GOTO_ON_FALSE(ip101, NULL, err, TAG, "calloc ip101 failed");
|
2022-05-13 06:03:56 -04:00
|
|
|
ESP_GOTO_ON_FALSE(esp_eth_phy_802_3_obj_config_init(&ip101->phy_802_3, config) == ESP_OK,
|
|
|
|
NULL, err, TAG, "configuration initialization of PHY 802.3 failed");
|
|
|
|
|
|
|
|
// redefine functions which need to be customized for sake of IP101
|
|
|
|
ip101->phy_802_3.parent.init = ip101_init;
|
|
|
|
ip101->phy_802_3.parent.get_link = ip101_get_link;
|
2024-02-07 05:24:17 -05:00
|
|
|
ip101->phy_802_3.parent.reset_hw = ip101_reset_hw;
|
2019-04-10 04:24:50 -04:00
|
|
|
|
2022-05-13 06:03:56 -04:00
|
|
|
return &ip101->phy_802_3.parent;
|
2019-04-10 04:24:50 -04:00
|
|
|
err:
|
2022-05-13 06:03:56 -04:00
|
|
|
if (ip101 != NULL) {
|
|
|
|
free(ip101);
|
|
|
|
}
|
2021-04-01 08:00:54 -04:00
|
|
|
return ret;
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|