|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <string.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <sys/cdefs.h> |
| 9 | +#include "esp_log.h" |
| 10 | +#include "esp_eth.h" |
| 11 | +#include "eth_phy_802_3_regs.h" |
| 12 | +#include "esp_eth_enc28j60.h" |
| 13 | +#include "freertos/FreeRTOS.h" |
| 14 | +#include "freertos/task.h" |
| 15 | +#include "driver/gpio.h" |
| 16 | + |
| 17 | +static const char *TAG = "enc28j60"; |
| 18 | +#define PHY_CHECK(a, str, goto_tag, ...) \ |
| 19 | + do { \ |
| 20 | + if (!(a)) { \ |
| 21 | + ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 22 | + goto goto_tag; \ |
| 23 | + } \ |
| 24 | + } while (0) |
| 25 | + |
| 26 | +/***************Vendor Specific Register***************/ |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief PHCON2(PHY Control Register 2) |
| 30 | + * |
| 31 | + */ |
| 32 | +typedef union { |
| 33 | + struct { |
| 34 | + uint32_t reserved_7_0 : 8; // Reserved |
| 35 | + uint32_t pdpxmd : 1; // PHY Duplex Mode bit |
| 36 | + uint32_t reserved_10_9: 2; // Reserved |
| 37 | + uint32_t ppwrsv: 1; // PHY Power-Down bit |
| 38 | + uint32_t reserved_13_12: 2; // Reserved |
| 39 | + uint32_t ploopbk: 1; // PHY Loopback bit |
| 40 | + uint32_t prst: 1; // PHY Software Reset bit |
| 41 | + }; |
| 42 | + uint32_t val; |
| 43 | +} phcon1_reg_t; |
| 44 | +#define ETH_PHY_PHCON1_REG_ADDR (0x00) |
| 45 | + |
| 46 | +/** |
| 47 | + * @brief PHCON2(PHY Control Register 2) |
| 48 | + * |
| 49 | + */ |
| 50 | +typedef union { |
| 51 | + struct { |
| 52 | + uint32_t reserved_7_0 : 8; // Reserved |
| 53 | + uint32_t hdldis : 1; // Half-Duplex Loopback Disable |
| 54 | + uint32_t reserved_9: 1; // Reserved |
| 55 | + uint32_t jabber: 1; // Disable Jabber Correction |
| 56 | + uint32_t reserved_12_11: 2; // Reserved |
| 57 | + uint32_t txdis: 1; // Disable Twist-Pair Transmitter |
| 58 | + uint32_t frclnk: 1; // Force Linkup |
| 59 | + uint32_t reserved_15: 1; //Reserved |
| 60 | + }; |
| 61 | + uint32_t val; |
| 62 | +} phcon2_reg_t; |
| 63 | +#define ETH_PHY_PHCON2_REG_ADDR (0x10) |
| 64 | + |
| 65 | +/** |
| 66 | + * @brief PHSTAT2(PHY Status Register 2) |
| 67 | + * |
| 68 | + */ |
| 69 | +typedef union { |
| 70 | + struct { |
| 71 | + uint32_t reserved_4_0 : 5; // Reserved |
| 72 | + uint32_t plrity : 1; // Polarity Status |
| 73 | + uint32_t reserved_8_6 : 3; // Reserved |
| 74 | + uint32_t dpxstat : 1; // PHY Duplex Status |
| 75 | + uint32_t lstat : 1; // PHY Link Status (non-latching) |
| 76 | + uint32_t colstat : 1; // PHY Collision Status |
| 77 | + uint32_t rxstat : 1; // PHY Receive Status |
| 78 | + uint32_t txstat : 1; // PHY Transmit Status |
| 79 | + uint32_t reserved_15_14 : 2; // Reserved |
| 80 | + }; |
| 81 | + uint32_t val; |
| 82 | +} phstat2_reg_t; |
| 83 | +#define ETH_PHY_PHSTAT2_REG_ADDR (0x11) |
| 84 | + |
| 85 | +typedef struct { |
| 86 | + esp_eth_phy_t parent; |
| 87 | + esp_eth_mediator_t *eth; |
| 88 | + uint32_t addr; |
| 89 | + uint32_t reset_timeout_ms; |
| 90 | + eth_link_t link_status; |
| 91 | + int reset_gpio_num; |
| 92 | +} phy_enc28j60_t; |
| 93 | + |
| 94 | +static esp_err_t enc28j60_update_link_duplex_speed(phy_enc28j60_t *enc28j60) { |
| 95 | + esp_eth_mediator_t *eth = enc28j60->eth; |
| 96 | + eth_speed_t speed = ETH_SPEED_10M; // enc28j60 speed is fixed to 10Mbps |
| 97 | + eth_duplex_t duplex = ETH_DUPLEX_HALF; |
| 98 | + phstat2_reg_t phstat; |
| 99 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_PHSTAT2_REG_ADDR, &(phstat.val)) == ESP_OK, |
| 100 | + "read PHSTAT2 failed", err); |
| 101 | + eth_link_t link = phstat.lstat ? ETH_LINK_UP : ETH_LINK_DOWN; |
| 102 | + /* check if link status changed */ |
| 103 | + if (enc28j60->link_status != link) { |
| 104 | + /* when link up, read result */ |
| 105 | + if (link == ETH_LINK_UP) { |
| 106 | + if (phstat.dpxstat) { |
| 107 | + duplex = ETH_DUPLEX_FULL; |
| 108 | + } else { |
| 109 | + duplex = ETH_DUPLEX_HALF; |
| 110 | + } |
| 111 | + PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *)speed) == ESP_OK, |
| 112 | + "change speed failed", err); |
| 113 | + PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *)duplex) == ESP_OK, |
| 114 | + "change duplex failed", err); |
| 115 | + } |
| 116 | + PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link) == ESP_OK, |
| 117 | + "change link failed", err); |
| 118 | + enc28j60->link_status = link; |
| 119 | + } |
| 120 | + return ESP_OK; |
| 121 | + err: |
| 122 | + return ESP_FAIL; |
| 123 | +} |
| 124 | + |
| 125 | +static esp_err_t enc28j60_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth) { |
| 126 | + PHY_CHECK(eth, "can't set mediator for enc28j60 to null", err); |
| 127 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 128 | + enc28j60->eth = eth; |
| 129 | + return ESP_OK; |
| 130 | + err: |
| 131 | + return ESP_ERR_INVALID_ARG; |
| 132 | +} |
| 133 | + |
| 134 | +static esp_err_t enc28j60_get_link(esp_eth_phy_t *phy) { |
| 135 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 136 | + /* Updata information about link, speed, duplex */ |
| 137 | + PHY_CHECK(enc28j60_update_link_duplex_speed(enc28j60) == ESP_OK, "update link duplex speed failed", err); |
| 138 | + return ESP_OK; |
| 139 | + err: |
| 140 | + return ESP_FAIL; |
| 141 | +} |
| 142 | + |
| 143 | +static esp_err_t enc28j60_reset(esp_eth_phy_t *phy) { |
| 144 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 145 | + enc28j60->link_status = ETH_LINK_DOWN; |
| 146 | + esp_eth_mediator_t *eth = enc28j60->eth; |
| 147 | + bmcr_reg_t bmcr = {.reset = 1}; |
| 148 | + PHY_CHECK(eth->phy_reg_write(eth, enc28j60->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, |
| 149 | + "write BMCR failed", err); |
| 150 | + /* Wait for reset complete */ |
| 151 | + uint32_t to = 0; |
| 152 | + for (to = 0; to < enc28j60->reset_timeout_ms / 10; to++) { |
| 153 | + vTaskDelay(pdMS_TO_TICKS(10)); |
| 154 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, |
| 155 | + "read BMCR failed", err); |
| 156 | + if (!bmcr.reset) { |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + PHY_CHECK(to < enc28j60->reset_timeout_ms / 10, "PHY reset timeout", err); |
| 161 | + return ESP_OK; |
| 162 | + err: |
| 163 | + return ESP_FAIL; |
| 164 | +} |
| 165 | + |
| 166 | +static esp_err_t enc28j60_reset_hw(esp_eth_phy_t *phy) { |
| 167 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 168 | + // set reset_gpio_num minus zero can skip hardware reset phy chip |
| 169 | + if (enc28j60->reset_gpio_num >= 0) { |
| 170 | + gpio_reset_pin(enc28j60->reset_gpio_num); |
| 171 | + gpio_set_direction(enc28j60->reset_gpio_num, GPIO_MODE_OUTPUT); |
| 172 | + gpio_set_level(enc28j60->reset_gpio_num, 0); |
| 173 | + gpio_set_level(enc28j60->reset_gpio_num, 1); |
| 174 | + } |
| 175 | + return ESP_OK; |
| 176 | +} |
| 177 | + |
| 178 | +static esp_err_t enc28j60_autonego_ctrl(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd, bool *autoneg_en_stat) { |
| 179 | + /** |
| 180 | + * ENC28J60 does not support automatic duplex negotiation. |
| 181 | + * If it is connected to an automatic duplex negotiation enabled network switch, |
| 182 | + * ENC28J60 will be detected as a half-duplex device. |
| 183 | + * To communicate in Full-Duplex mode, ENC28J60 and the remote node |
| 184 | + * must be manually configured for full-duplex operation. |
| 185 | + */ |
| 186 | + |
| 187 | + switch (cmd) |
| 188 | + { |
| 189 | + case ESP_ETH_PHY_AUTONEGO_RESTART: |
| 190 | + /* Fallthrough */ |
| 191 | + case ESP_ETH_PHY_AUTONEGO_EN: |
| 192 | + return ESP_ERR_NOT_SUPPORTED; |
| 193 | + case ESP_ETH_PHY_AUTONEGO_DIS: |
| 194 | + /* Fallthrough */ |
| 195 | + case ESP_ETH_PHY_AUTONEGO_G_STAT: |
| 196 | + *autoneg_en_stat = false; |
| 197 | + break; |
| 198 | + default: |
| 199 | + return ESP_ERR_INVALID_ARG; |
| 200 | + } |
| 201 | + return ESP_OK; |
| 202 | +} |
| 203 | + |
| 204 | +esp_err_t enc28j60_set_speed(esp_eth_phy_t *phy, eth_speed_t speed) { |
| 205 | + /* ENC28J60 supports only 10Mbps */ |
| 206 | + if (speed == ETH_SPEED_10M) { |
| 207 | + return ESP_OK; |
| 208 | + } |
| 209 | + return ESP_ERR_NOT_SUPPORTED; |
| 210 | +} |
| 211 | + |
| 212 | +esp_err_t enc28j60_set_duplex(esp_eth_phy_t *phy, eth_duplex_t duplex) { |
| 213 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 214 | + esp_eth_mediator_t *eth = enc28j60->eth; |
| 215 | + phcon1_reg_t phcon1; |
| 216 | + |
| 217 | + /* Since the link is going to be reconfigured, consider it down to be status updated once the driver re-started */ |
| 218 | + enc28j60->link_status = ETH_LINK_DOWN; |
| 219 | + |
| 220 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, 0, &phcon1.val) == ESP_OK, |
| 221 | + "read PHCON1 failed", err); |
| 222 | + switch (duplex) { |
| 223 | + case ETH_DUPLEX_HALF: |
| 224 | + phcon1.pdpxmd = 0; |
| 225 | + break; |
| 226 | + case ETH_DUPLEX_FULL: |
| 227 | + phcon1.pdpxmd = 1; |
| 228 | + break; |
| 229 | + default: |
| 230 | + PHY_CHECK(false, "unknown duplex", err); |
| 231 | + break; |
| 232 | + } |
| 233 | + |
| 234 | + PHY_CHECK(eth->phy_reg_write(eth, enc28j60->addr, 0, phcon1.val) == ESP_OK, |
| 235 | + "write PHCON1 failed", err); |
| 236 | + |
| 237 | + return ESP_OK; |
| 238 | + err: |
| 239 | + return ESP_FAIL; |
| 240 | +} |
| 241 | + |
| 242 | +static esp_err_t enc28j60_pwrctl(esp_eth_phy_t *phy, bool enable) { |
| 243 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 244 | + esp_eth_mediator_t *eth = enc28j60->eth; |
| 245 | + bmcr_reg_t bmcr; |
| 246 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, |
| 247 | + "read BMCR failed", err); |
| 248 | + if (!enable) { |
| 249 | + /* Enable IEEE Power Down Mode */ |
| 250 | + bmcr.power_down = 1; |
| 251 | + } else { |
| 252 | + /* Disable IEEE Power Down Mode */ |
| 253 | + bmcr.power_down = 0; |
| 254 | + } |
| 255 | + PHY_CHECK(eth->phy_reg_write(eth, enc28j60->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, |
| 256 | + "write BMCR failed", err); |
| 257 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, |
| 258 | + "read BMCR failed", err); |
| 259 | + if (!enable) { |
| 260 | + PHY_CHECK(bmcr.power_down == 1, "power down failed", err); |
| 261 | + } else { |
| 262 | + PHY_CHECK(bmcr.power_down == 0, "power up failed", err); |
| 263 | + } |
| 264 | + return ESP_OK; |
| 265 | + err: |
| 266 | + return ESP_FAIL; |
| 267 | +} |
| 268 | + |
| 269 | +static esp_err_t enc28j60_set_addr(esp_eth_phy_t *phy, uint32_t addr) { |
| 270 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 271 | + enc28j60->addr = addr; |
| 272 | + return ESP_OK; |
| 273 | +} |
| 274 | + |
| 275 | +static esp_err_t enc28j60_get_addr(esp_eth_phy_t *phy, uint32_t *addr) { |
| 276 | + PHY_CHECK(addr, "addr can't be null", err); |
| 277 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 278 | + *addr = enc28j60->addr; |
| 279 | + return ESP_OK; |
| 280 | + err: |
| 281 | + return ESP_ERR_INVALID_ARG; |
| 282 | +} |
| 283 | + |
| 284 | +static esp_err_t enc28j60_del(esp_eth_phy_t *phy) { |
| 285 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 286 | + free(enc28j60); |
| 287 | + return ESP_OK; |
| 288 | +} |
| 289 | + |
| 290 | +static esp_err_t enc28j60_init(esp_eth_phy_t *phy) { |
| 291 | + phy_enc28j60_t *enc28j60 = __containerof(phy, phy_enc28j60_t, parent); |
| 292 | + esp_eth_mediator_t *eth = enc28j60->eth; |
| 293 | + /* Power on Ethernet PHY */ |
| 294 | + PHY_CHECK(enc28j60_pwrctl(phy, true) == ESP_OK, "power control failed", err); |
| 295 | + /* Reset Ethernet PHY */ |
| 296 | + PHY_CHECK(enc28j60_reset(phy) == ESP_OK, "reset failed", err); |
| 297 | + /* Check PHY ID */ |
| 298 | + phyidr1_reg_t id1; |
| 299 | + phyidr2_reg_t id2; |
| 300 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK, |
| 301 | + "read ID1 failed", err); |
| 302 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK, |
| 303 | + "read ID2 failed", err); |
| 304 | + PHY_CHECK(id1.oui_msb == 0x0083 && id2.oui_lsb == 0x05 && id2.vendor_model == 0x00, |
| 305 | + "wrong chip ID", err); |
| 306 | + /* Disable half duplex loopback */ |
| 307 | + phcon2_reg_t phcon2; |
| 308 | + PHY_CHECK(eth->phy_reg_read(eth, enc28j60->addr, ETH_PHY_PHCON2_REG_ADDR, &(phcon2.val)) == ESP_OK, |
| 309 | + "read PHCON2 failed", err); |
| 310 | + phcon2.hdldis = 1; |
| 311 | + PHY_CHECK(eth->phy_reg_write(eth, enc28j60->addr, ETH_PHY_PHCON2_REG_ADDR, phcon2.val) == ESP_OK, |
| 312 | + "write PHCON2 failed", err); |
| 313 | + return ESP_OK; |
| 314 | + err: |
| 315 | + return ESP_FAIL; |
| 316 | +} |
| 317 | + |
| 318 | +static esp_err_t enc28j60_deinit(esp_eth_phy_t *phy) { |
| 319 | + /* Power off Ethernet PHY */ |
| 320 | + PHY_CHECK(enc28j60_pwrctl(phy, false) == ESP_OK, "power off Ethernet PHY failed", err); |
| 321 | + return ESP_OK; |
| 322 | + err: |
| 323 | + return ESP_FAIL; |
| 324 | +} |
| 325 | + |
| 326 | +esp_eth_phy_t *esp_eth_phy_new_enc28j60(const eth_phy_config_t *config) { |
| 327 | + PHY_CHECK(config, "can't set phy config to null", err); |
| 328 | + phy_enc28j60_t *enc28j60 = calloc(1, sizeof(phy_enc28j60_t)); |
| 329 | + PHY_CHECK(enc28j60, "calloc enc28j60 failed", err); |
| 330 | + enc28j60->addr = config->phy_addr; // although PHY addr is meaningless to ENC28J60 |
| 331 | + enc28j60->reset_timeout_ms = config->reset_timeout_ms; |
| 332 | + enc28j60->reset_gpio_num = config->reset_gpio_num; |
| 333 | + enc28j60->link_status = ETH_LINK_DOWN; |
| 334 | + enc28j60->parent.reset = enc28j60_reset; |
| 335 | + enc28j60->parent.reset_hw = enc28j60_reset_hw; |
| 336 | + enc28j60->parent.init = enc28j60_init; |
| 337 | + enc28j60->parent.deinit = enc28j60_deinit; |
| 338 | + enc28j60->parent.set_mediator = enc28j60_set_mediator; |
| 339 | + enc28j60->parent.autonego_ctrl = enc28j60_autonego_ctrl; |
| 340 | + enc28j60->parent.get_link = enc28j60_get_link; |
| 341 | + enc28j60->parent.pwrctl = enc28j60_pwrctl; |
| 342 | + enc28j60->parent.get_addr = enc28j60_get_addr; |
| 343 | + enc28j60->parent.set_addr = enc28j60_set_addr; |
| 344 | + enc28j60->parent.set_speed = enc28j60_set_speed; |
| 345 | + enc28j60->parent.set_duplex = enc28j60_set_duplex; |
| 346 | + enc28j60->parent.del = enc28j60_del; |
| 347 | + return &(enc28j60->parent); |
| 348 | + err: |
| 349 | + return NULL; |
| 350 | +} |
0 commit comments