ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
esp_eth_phy_jl1101.c
Go to the documentation of this file.
1// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifdef USE_ESP32
16
17#include <string.h>
18#include <stdlib.h>
19#include <sys/cdefs.h>
20#include "esp_log.h"
21#include "esp_eth.h"
22#include "esp_eth_phy_802_3.h"
23#include "freertos/FreeRTOS.h"
24#include "freertos/task.h"
25#include "driver/gpio.h"
26#include "esp_rom_gpio.h"
27#include "esp_rom_sys.h"
28#include "esp_idf_version.h"
29
30#if defined(USE_ARDUINO) || ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
31
32static const char *TAG = "jl1101";
33#define PHY_CHECK(a, str, goto_tag, ...) \
34 do { \
35 if (!(a)) { \
36 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
37 goto goto_tag; \
38 } \
39 } while (0)
40
41/***************Vendor Specific Register***************/
42
47typedef union {
48 struct {
49 uint16_t page_select : 8; /* Select register page, default is 0 */
50 uint16_t reserved : 8; /* Reserved */
51 };
52 uint16_t val;
53} psr_reg_t;
54#define ETH_PHY_PSR_REG_ADDR (0x1F)
55
56typedef struct {
57 esp_eth_phy_t parent;
58 esp_eth_mediator_t *eth;
59 int addr;
60 uint32_t reset_timeout_ms;
61 uint32_t autonego_timeout_ms;
62 eth_link_t link_status;
63 int reset_gpio_num;
64} phy_jl1101_t;
65
66static esp_err_t jl1101_page_select(phy_jl1101_t *jl1101, uint32_t page) {
67 esp_eth_mediator_t *eth = jl1101->eth;
68 psr_reg_t psr = {.page_select = page};
69 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_PSR_REG_ADDR, psr.val) == ESP_OK, "write PSR failed", err);
70 return ESP_OK;
71err:
72 return ESP_FAIL;
73}
74
75static esp_err_t jl1101_update_link_duplex_speed(phy_jl1101_t *jl1101) {
76 esp_eth_mediator_t *eth = jl1101->eth;
77 eth_speed_t speed = ETH_SPEED_10M;
78 eth_duplex_t duplex = ETH_DUPLEX_HALF;
79 bmcr_reg_t bmcr;
80 bmsr_reg_t bmsr;
81 uint32_t peer_pause_ability = false;
82 anlpar_reg_t anlpar;
83 PHY_CHECK(jl1101_page_select(jl1101, 0) == ESP_OK, "select page 0 failed", err);
84 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed",
85 err);
86 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)) == ESP_OK,
87 "read ANLPAR failed", err);
88 eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
89 /* check if link status changed */
90 if (jl1101->link_status != link) {
91 /* when link up, read negotiation result */
92 if (link == ETH_LINK_UP) {
93 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
94 err);
95 if (bmcr.speed_select) {
96 speed = ETH_SPEED_100M;
97 } else {
98 speed = ETH_SPEED_10M;
99 }
100 if (bmcr.duplex_mode) {
101 duplex = ETH_DUPLEX_FULL;
102 } else {
103 duplex = ETH_DUPLEX_HALF;
104 }
105 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *) speed) == ESP_OK, "change speed failed", err);
106 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *) duplex) == ESP_OK, "change duplex failed", err);
107 /* if we're in duplex mode, and peer has the flow control ability */
108 if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
109 peer_pause_ability = 1;
110 } else {
111 peer_pause_ability = 0;
112 }
113 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *) peer_pause_ability) == ESP_OK,
114 "change pause ability failed", err);
115 }
116 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (void *) link) == ESP_OK, "change link failed", err);
117 jl1101->link_status = link;
118 }
119 return ESP_OK;
120err:
121 return ESP_FAIL;
122}
123
124static esp_err_t jl1101_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth) {
125 PHY_CHECK(eth, "can't set mediator to null", err);
126 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
127 jl1101->eth = eth;
128 return ESP_OK;
129err:
130 return ESP_ERR_INVALID_ARG;
131}
132
133static esp_err_t jl1101_get_link(esp_eth_phy_t *phy) {
134 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
135 /* Updata information about link, speed, duplex */
136 PHY_CHECK(jl1101_update_link_duplex_speed(jl1101) == ESP_OK, "update link duplex speed failed", err);
137 return ESP_OK;
138err:
139 return ESP_FAIL;
140}
141
142static esp_err_t jl1101_reset(esp_eth_phy_t *phy) {
143 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
144 jl1101->link_status = ETH_LINK_DOWN;
145 esp_eth_mediator_t *eth = jl1101->eth;
146 bmcr_reg_t bmcr = {.reset = 1};
147 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
148 /* Wait for reset complete */
149 uint32_t to = 0;
150 for (to = 0; to < jl1101->reset_timeout_ms / 50; to++) {
151 vTaskDelay(pdMS_TO_TICKS(50));
152 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
153 err);
154 if (!bmcr.reset) {
155 break;
156 }
157 }
158 PHY_CHECK(to < jl1101->reset_timeout_ms / 50, "reset timeout", err);
159 return ESP_OK;
160err:
161 return ESP_FAIL;
162}
163
164static esp_err_t jl1101_reset_hw(esp_eth_phy_t *phy) {
165 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
166 if (jl1101->reset_gpio_num >= 0) {
167 esp_rom_gpio_pad_select_gpio(jl1101->reset_gpio_num);
168 gpio_set_direction(jl1101->reset_gpio_num, GPIO_MODE_OUTPUT);
169 gpio_set_level(jl1101->reset_gpio_num, 0);
170 esp_rom_delay_us(100); // insert min input assert time
171 gpio_set_level(jl1101->reset_gpio_num, 1);
172 }
173 return ESP_OK;
174}
175
176static esp_err_t jl1101_negotiate(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd, bool *nego_state) {
177 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
178 esp_eth_mediator_t *eth = jl1101->eth;
179 /* in case any link status has changed, let's assume we're in link down status */
180 jl1101->link_status = ETH_LINK_DOWN;
181 /* Restart auto negotiation */
182 bmcr_reg_t bmcr = {
183 .speed_select = 1, /* 100Mbps */
184 .duplex_mode = 1, /* Full Duplex */
185 .en_auto_nego = 1, /* Auto Negotiation */
186 .restart_auto_nego = 1 /* Restart Auto Negotiation */
187 };
188 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
189 /* Wait for auto negotiation complete */
190 bmsr_reg_t bmsr;
191 uint32_t to = 0;
192 for (to = 0; to < jl1101->autonego_timeout_ms / 100; to++) {
193 vTaskDelay(pdMS_TO_TICKS(100));
194 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed",
195 err);
196 if (bmsr.auto_nego_complete) {
197 break;
198 }
199 }
200 /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */
201 if (to >= jl1101->autonego_timeout_ms / 100) {
202 ESP_LOGW(TAG, "auto negotiation timeout");
203 }
204 return ESP_OK;
205err:
206 return ESP_FAIL;
207}
208
209static esp_err_t jl1101_pwrctl(esp_eth_phy_t *phy, bool enable) {
210 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
211 esp_eth_mediator_t *eth = jl1101->eth;
212 bmcr_reg_t bmcr;
213 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
214 err);
215 if (!enable) {
216 /* Enable IEEE Power Down Mode */
217 bmcr.power_down = 1;
218 } else {
219 /* Disable IEEE Power Down Mode */
220 bmcr.power_down = 0;
221 }
222 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
223 if (!enable) {
224 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
225 err);
226 PHY_CHECK(bmcr.power_down == 1, "power down failed", err);
227 } else {
228 /* wait for power up complete */
229 uint32_t to = 0;
230 for (to = 0; to < jl1101->reset_timeout_ms / 10; to++) {
231 vTaskDelay(pdMS_TO_TICKS(10));
232 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
233 err);
234 if (bmcr.power_down == 0) {
235 break;
236 }
237 }
238 PHY_CHECK(to < jl1101->reset_timeout_ms / 10, "power up timeout", err);
239 }
240 return ESP_OK;
241err:
242 return ESP_FAIL;
243}
244
245static esp_err_t jl1101_set_addr(esp_eth_phy_t *phy, uint32_t addr) {
246 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
247 jl1101->addr = addr;
248 return ESP_OK;
249}
250
251static esp_err_t jl1101_get_addr(esp_eth_phy_t *phy, uint32_t *addr) {
252 PHY_CHECK(addr, "addr can't be null", err);
253 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
254 *addr = jl1101->addr;
255 return ESP_OK;
256err:
257 return ESP_ERR_INVALID_ARG;
258}
259
260static esp_err_t jl1101_del(esp_eth_phy_t *phy) {
261 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
262 free(jl1101);
263 return ESP_OK;
264}
265
266static esp_err_t jl1101_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability) {
267 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
268 esp_eth_mediator_t *eth = jl1101->eth;
269 /* Set PAUSE function ability */
270 anar_reg_t anar;
271 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK, "read ANAR failed",
272 err);
273 if (ability) {
274 anar.asymmetric_pause = 1;
275 anar.symmetric_pause = 1;
276 } else {
277 anar.asymmetric_pause = 0;
278 anar.symmetric_pause = 0;
279 }
280 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK, "write ANAR failed", err);
281 return ESP_OK;
282err:
283 return ESP_FAIL;
284}
285
286static esp_err_t jl1101_init(esp_eth_phy_t *phy) {
287 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
288 esp_eth_mediator_t *eth = jl1101->eth;
289 // Detect PHY address
290 if (jl1101->addr == ESP_ETH_PHY_ADDR_AUTO) {
291 PHY_CHECK(esp_eth_phy_802_3_detect_phy_addr(eth, &jl1101->addr) == ESP_OK, "Detect PHY address failed", err);
292 }
293 /* Power on Ethernet PHY */
294 PHY_CHECK(jl1101_pwrctl(phy, true) == ESP_OK, "power control failed", err);
295 /* Reset Ethernet PHY */
296 PHY_CHECK(jl1101_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, jl1101->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK, "read ID1 failed", err);
301 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK, "read ID2 failed", err);
302 PHY_CHECK(id1.oui_msb == 0x937C && id2.oui_lsb == 0x10 && id2.vendor_model == 0x2, "wrong chip ID", err);
303 return ESP_OK;
304err:
305 return ESP_FAIL;
306}
307
308static esp_err_t jl1101_deinit(esp_eth_phy_t *phy) {
309 /* Power off Ethernet PHY */
310 PHY_CHECK(jl1101_pwrctl(phy, false) == ESP_OK, "power control failed", err);
311 return ESP_OK;
312err:
313 return ESP_FAIL;
314}
315
316esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config) {
317 PHY_CHECK(config, "can't set phy config to null", err);
318 phy_jl1101_t *jl1101 = calloc(1, sizeof(phy_jl1101_t));
319 PHY_CHECK(jl1101, "calloc jl1101 failed", err);
320 jl1101->addr = config->phy_addr;
321 jl1101->reset_gpio_num = config->reset_gpio_num;
322 jl1101->reset_timeout_ms = config->reset_timeout_ms;
323 jl1101->link_status = ETH_LINK_DOWN;
324 jl1101->autonego_timeout_ms = config->autonego_timeout_ms;
325 jl1101->parent.reset = jl1101_reset;
326 jl1101->parent.reset_hw = jl1101_reset_hw;
327 jl1101->parent.init = jl1101_init;
328 jl1101->parent.deinit = jl1101_deinit;
329 jl1101->parent.set_mediator = jl1101_set_mediator;
330 jl1101->parent.autonego_ctrl = jl1101_negotiate;
331 jl1101->parent.get_link = jl1101_get_link;
332 jl1101->parent.pwrctl = jl1101_pwrctl;
333 jl1101->parent.get_addr = jl1101_get_addr;
334 jl1101->parent.set_addr = jl1101_set_addr;
335 jl1101->parent.advertise_pause_ability = jl1101_advertise_pause_ability;
336 jl1101->parent.del = jl1101_del;
337
338 return &(jl1101->parent);
339err:
340 return NULL;
341}
342
343#endif /* USE_ARDUINO */
344#endif /* USE_ESP32 */
uint16_t reserved
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
int speed
Definition fan.h:1
mopeka_std_values val[4]