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 #include <string.h>
16 #include <stdlib.h>
17 #include <sys/cdefs.h>
18 #include "esp_log.h"
19 #include "esp_check.h"
20 #include "esp_eth.h"
21 #include "eth_phy_regs_struct.h"
22 #include "freertos/FreeRTOS.h"
23 #include "freertos/task.h"
24 #include "driver/gpio.h"
25 #include "esp_rom_gpio.h"
26 #include "esp_rom_sys.h"
27 
28 static const char *TAG = "rtl8201";
29 
30 /***************Vendor Specific Register***************/
31 
32 /**
33  * @brief PSMR(Power Saving Mode Register)
34  *
35  */
36 typedef union {
37     struct {
38         uint16_t reserved : 15;   /* Reserved */
39         uint16_t en_pwr_save : 1; /* Enable power saving mode */
40     };
41     uint16_t val;
42 } psmr_reg_t;
43 #define ETH_PHY_PSMR_REG_ADDR (0x18)
44 
45 /**
46  * @brief PSR(Page Select Register)
47  *
48  */
49 typedef union {
50     struct {
51         uint16_t page_select : 8; /* Select register page, default is 0 */
52         uint16_t reserved : 8;    /* Reserved */
53     };
54     uint16_t val;
55 } psr_reg_t;
56 #define ETH_PHY_PSR_REG_ADDR (0x1F)
57 
58 typedef struct {
59     esp_eth_phy_t parent;
60     esp_eth_mediator_t *eth;
61     int addr;
62     uint32_t reset_timeout_ms;
63     uint32_t autonego_timeout_ms;
64     eth_link_t link_status;
65     int reset_gpio_num;
66 } phy_rtl8201_t;
67 
rtl8201_page_select(phy_rtl8201_t * rtl8201,uint32_t page)68 static esp_err_t rtl8201_page_select(phy_rtl8201_t *rtl8201, uint32_t page)
69 {
70     esp_err_t ret = ESP_OK;
71     esp_eth_mediator_t *eth = rtl8201->eth;
72     psr_reg_t psr = {
73         .page_select = page
74     };
75     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_PSR_REG_ADDR, psr.val), err, TAG, "write PSR failed");
76     return ESP_OK;
77 err:
78     return ret;
79 }
80 
rtl8201_update_link_duplex_speed(phy_rtl8201_t * rtl8201)81 static esp_err_t rtl8201_update_link_duplex_speed(phy_rtl8201_t *rtl8201)
82 {
83     esp_err_t ret = ESP_OK;
84     esp_eth_mediator_t *eth = rtl8201->eth;
85     eth_speed_t speed = ETH_SPEED_10M;
86     eth_duplex_t duplex = ETH_DUPLEX_HALF;
87     bmcr_reg_t bmcr;
88     bmsr_reg_t bmsr;
89     uint32_t peer_pause_ability = false;
90     anlpar_reg_t anlpar;
91     ESP_GOTO_ON_ERROR(rtl8201_page_select(rtl8201, 0), err, TAG, "select page 0 failed");
92     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
93     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)), err, TAG, "read ANLPAR failed");
94     eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
95     /* check if link status changed */
96     if (rtl8201->link_status != link) {
97         /* when link up, read negotiation result */
98         if (link == ETH_LINK_UP) {
99             ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
100             if (bmcr.speed_select) {
101                 speed = ETH_SPEED_100M;
102             } else {
103                 speed = ETH_SPEED_10M;
104             }
105             if (bmcr.duplex_mode) {
106                 duplex = ETH_DUPLEX_FULL;
107             } else {
108                 duplex = ETH_DUPLEX_HALF;
109             }
110             ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *)speed), err, TAG, "change speed failed");
111             ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *)duplex), err, TAG, "change duplex failed");
112             /* if we're in duplex mode, and peer has the flow control ability */
113             if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
114                 peer_pause_ability = 1;
115             } else {
116                 peer_pause_ability = 0;
117             }
118             ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *)peer_pause_ability), err, TAG, "change pause ability failed");
119         }
120         ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link), err, TAG, "change link failed");
121         rtl8201->link_status = link;
122     }
123     return ESP_OK;
124 err:
125     return ret;
126 }
127 
rtl8201_set_mediator(esp_eth_phy_t * phy,esp_eth_mediator_t * eth)128 static esp_err_t rtl8201_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
129 {
130     esp_err_t ret = ESP_OK;
131     ESP_GOTO_ON_FALSE(eth, ESP_ERR_INVALID_ARG, err, TAG, "can't set mediator to null");
132     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
133     rtl8201->eth = eth;
134     return ESP_OK;
135 err:
136     return ret;
137 }
138 
rtl8201_get_link(esp_eth_phy_t * phy)139 static esp_err_t rtl8201_get_link(esp_eth_phy_t *phy)
140 {
141     esp_err_t ret = ESP_OK;
142     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
143     /* Updata information about link, speed, duplex */
144     ESP_GOTO_ON_ERROR(rtl8201_update_link_duplex_speed(rtl8201), err, TAG, "update link duplex speed failed");
145     return ESP_OK;
146 err:
147     return ret;
148 }
149 
rtl8201_reset(esp_eth_phy_t * phy)150 static esp_err_t rtl8201_reset(esp_eth_phy_t *phy)
151 {
152     esp_err_t ret = ESP_OK;
153     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
154     rtl8201->link_status = ETH_LINK_DOWN;
155     esp_eth_mediator_t *eth = rtl8201->eth;
156     bmcr_reg_t bmcr = {.reset = 1};
157     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
158     /* Wait for reset complete */
159     uint32_t to = 0;
160     for (to = 0; to < rtl8201->reset_timeout_ms / 10; to++) {
161         vTaskDelay(pdMS_TO_TICKS(10));
162         ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
163         if (!bmcr.reset) {
164             break;
165         }
166     }
167     ESP_GOTO_ON_FALSE(to < rtl8201->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "reset timeout");
168     return ESP_OK;
169 err:
170     return ret;
171 }
172 
rtl8201_reset_hw(esp_eth_phy_t * phy)173 static esp_err_t rtl8201_reset_hw(esp_eth_phy_t *phy)
174 {
175     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
176     if (rtl8201->reset_gpio_num >= 0) {
177         esp_rom_gpio_pad_select_gpio(rtl8201->reset_gpio_num);
178         gpio_set_direction(rtl8201->reset_gpio_num, GPIO_MODE_OUTPUT);
179         gpio_set_level(rtl8201->reset_gpio_num, 0);
180         esp_rom_delay_us(100); // insert min input assert time
181         gpio_set_level(rtl8201->reset_gpio_num, 1);
182     }
183     return ESP_OK;
184 }
185 
186 /**
187  * @note This function is responsible for restarting a new auto-negotiation,
188  *       the result of negotiation won't be relected to uppler layers.
189  *       Instead, the negotiation result is fetched by linker timer, see `rtl8201_get_link()`
190  */
rtl8201_negotiate(esp_eth_phy_t * phy)191 static esp_err_t rtl8201_negotiate(esp_eth_phy_t *phy)
192 {
193     esp_err_t ret = ESP_OK;
194     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
195     esp_eth_mediator_t *eth = rtl8201->eth;
196     /* in case any link status has changed, let's assume we're in link down status */
197     rtl8201->link_status = ETH_LINK_DOWN;
198     /* Restart auto negotiation */
199     bmcr_reg_t bmcr = {
200         .speed_select = 1,     /* 100Mbps */
201         .duplex_mode = 1,      /* Full Duplex */
202         .en_auto_nego = 1,     /* Auto Negotiation */
203         .restart_auto_nego = 1 /* Restart Auto Negotiation */
204     };
205     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
206     /* Wait for auto negotiation complete */
207     bmsr_reg_t bmsr;
208     uint32_t to = 0;
209     for (to = 0; to < rtl8201->autonego_timeout_ms / 100; to++) {
210         vTaskDelay(pdMS_TO_TICKS(100));
211         ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)), err, TAG, "read BMSR failed");
212         if (bmsr.auto_nego_complete) {
213             break;
214         }
215     }
216     if ((to >= rtl8201->autonego_timeout_ms / 100) && (rtl8201->link_status == ETH_LINK_UP)) {
217         ESP_LOGW(TAG, "auto negotiation timeout");
218     }
219     return ESP_OK;
220 err:
221     return ret;
222 }
223 
rtl8201_pwrctl(esp_eth_phy_t * phy,bool enable)224 static esp_err_t rtl8201_pwrctl(esp_eth_phy_t *phy, bool enable)
225 {
226     esp_err_t ret = ESP_OK;
227     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
228     esp_eth_mediator_t *eth = rtl8201->eth;
229     bmcr_reg_t bmcr;
230     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
231     if (!enable) {
232         /* Enable IEEE Power Down Mode */
233         bmcr.power_down = 1;
234     } else {
235         /* Disable IEEE Power Down Mode */
236         bmcr.power_down = 0;
237     }
238     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
239     if (!enable) {
240         ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
241         ESP_GOTO_ON_FALSE(bmcr.power_down == 1, ESP_FAIL, err, TAG, "power down failed");
242     } else {
243         /* wait for power up complete */
244         uint32_t to = 0;
245         for (to = 0; to < rtl8201->reset_timeout_ms / 10; to++) {
246             vTaskDelay(pdMS_TO_TICKS(10));
247             ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
248             if (bmcr.power_down == 0) {
249                 break;
250             }
251         }
252         ESP_GOTO_ON_FALSE(to < rtl8201->reset_timeout_ms / 10, ESP_FAIL, err, TAG, "power up timeout");
253     }
254     return ESP_OK;
255 err:
256     return ret;
257 }
258 
rtl8201_set_addr(esp_eth_phy_t * phy,uint32_t addr)259 static esp_err_t rtl8201_set_addr(esp_eth_phy_t *phy, uint32_t addr)
260 {
261     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
262     rtl8201->addr = addr;
263     return ESP_OK;
264 }
265 
rtl8201_get_addr(esp_eth_phy_t * phy,uint32_t * addr)266 static esp_err_t rtl8201_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
267 {
268     esp_err_t ret = ESP_OK;
269     ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "addr can't be null");
270     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
271     *addr = rtl8201->addr;
272     return ESP_OK;
273 err:
274     return ret;
275 }
276 
rtl8201_del(esp_eth_phy_t * phy)277 static esp_err_t rtl8201_del(esp_eth_phy_t *phy)
278 {
279     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
280     free(rtl8201);
281     return ESP_OK;
282 }
283 
rtl8201_advertise_pause_ability(esp_eth_phy_t * phy,uint32_t ability)284 static esp_err_t rtl8201_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
285 {
286     esp_err_t ret = ESP_OK;
287     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
288     esp_eth_mediator_t *eth = rtl8201->eth;
289     /* Set PAUSE function ability */
290     anar_reg_t anar;
291     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)), err, TAG, "read ANAR failed");
292     if (ability) {
293         anar.asymmetric_pause = 1;
294         anar.symmetric_pause = 1;
295     } else {
296         anar.asymmetric_pause = 0;
297         anar.symmetric_pause = 0;
298     }
299     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_ANAR_REG_ADDR, anar.val), err, TAG, "write ANAR failed");
300     return ESP_OK;
301 err:
302     return ret;
303 }
304 
rtl8201_loopback(esp_eth_phy_t * phy,bool enable)305 static esp_err_t rtl8201_loopback(esp_eth_phy_t *phy, bool enable)
306 {
307     esp_err_t ret = ESP_OK;
308     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
309     esp_eth_mediator_t *eth = rtl8201->eth;
310     /* Set Loopback function */
311     bmcr_reg_t bmcr;
312     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)), err, TAG, "read BMCR failed");
313     if (enable) {
314         bmcr.en_loopback = 1;
315     } else {
316         bmcr.en_loopback = 0;
317     }
318     ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, rtl8201->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val), err, TAG, "write BMCR failed");
319     return ESP_OK;
320 err:
321     return ret;
322 }
323 
rtl8201_init(esp_eth_phy_t * phy)324 static esp_err_t rtl8201_init(esp_eth_phy_t *phy)
325 {
326     esp_err_t ret = ESP_OK;
327     phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent);
328     esp_eth_mediator_t *eth = rtl8201->eth;
329     // Detect PHY address
330     if (rtl8201->addr == ESP_ETH_PHY_ADDR_AUTO) {
331         ESP_GOTO_ON_ERROR(esp_eth_detect_phy_addr(eth, &rtl8201->addr), err, TAG, "Detect PHY address failed");
332     }
333     /* Power on Ethernet PHY */
334     ESP_GOTO_ON_ERROR(rtl8201_pwrctl(phy, true), err, TAG, "power control failed");
335     /* Reset Ethernet PHY */
336     ESP_GOTO_ON_ERROR(rtl8201_reset(phy), err, TAG, "reset failed");
337     /* Check PHY ID */
338     phyidr1_reg_t id1;
339     phyidr2_reg_t id2;
340     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)), err, TAG, "read ID1 failed");
341     ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)), err, TAG, "read ID2 failed");
342     ESP_GOTO_ON_FALSE(id1.oui_msb == 0x1C && id2.oui_lsb == 0x32 && id2.vendor_model == 0x1, ESP_FAIL, err, TAG, "wrong chip ID");
343     return ESP_OK;
344 err:
345     return ret;
346 }
347 
rtl8201_deinit(esp_eth_phy_t * phy)348 static esp_err_t rtl8201_deinit(esp_eth_phy_t *phy)
349 {
350     esp_err_t ret = ESP_OK;
351     /* Power off Ethernet PHY */
352     ESP_GOTO_ON_ERROR(rtl8201_pwrctl(phy, false), err, TAG, "power control failed");
353     return ESP_OK;
354 err:
355     return ret;
356 }
357 
esp_eth_phy_new_rtl8201(const eth_phy_config_t * config)358 esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config)
359 {
360     esp_eth_phy_t *ret = NULL;
361     ESP_GOTO_ON_FALSE(config, NULL, err, TAG, "can't set phy config to null");
362     phy_rtl8201_t *rtl8201 = calloc(1, sizeof(phy_rtl8201_t));
363     ESP_GOTO_ON_FALSE(rtl8201, NULL, err, TAG, "calloc rtl8201 failed");
364     rtl8201->addr = config->phy_addr;
365     rtl8201->reset_gpio_num = config->reset_gpio_num;
366     rtl8201->reset_timeout_ms = config->reset_timeout_ms;
367     rtl8201->link_status = ETH_LINK_DOWN;
368     rtl8201->autonego_timeout_ms = config->autonego_timeout_ms;
369     rtl8201->parent.reset = rtl8201_reset;
370     rtl8201->parent.reset_hw = rtl8201_reset_hw;
371     rtl8201->parent.init = rtl8201_init;
372     rtl8201->parent.deinit = rtl8201_deinit;
373     rtl8201->parent.set_mediator = rtl8201_set_mediator;
374     rtl8201->parent.negotiate = rtl8201_negotiate;
375     rtl8201->parent.get_link = rtl8201_get_link;
376     rtl8201->parent.pwrctl = rtl8201_pwrctl;
377     rtl8201->parent.get_addr = rtl8201_get_addr;
378     rtl8201->parent.set_addr = rtl8201_set_addr;
379     rtl8201->parent.advertise_pause_ability = rtl8201_advertise_pause_ability;
380     rtl8201->parent.loopback = rtl8201_loopback;
381     rtl8201->parent.del = rtl8201_del;
382 
383     return &(rtl8201->parent);
384 err:
385     return ret;
386 }
387