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