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