esp_eth_phy_lan8720.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include <string.h>
  15. #include <stdlib.h>
  16. #include <sys/cdefs.h>
  17. #include "esp_log.h"
  18. #include "esp_eth.h"
  19. #include "eth_phy_regs_struct.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "driver/gpio.h"
  23. #include "esp_rom_gpio.h"
  24. #include "esp_rom_sys.h"
  25. static const char *TAG = "lan8720";
  26. #define PHY_CHECK(a, str, goto_tag, ...) \
  27. do \
  28. { \
  29. if (!(a)) \
  30. { \
  31. ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  32. goto goto_tag; \
  33. } \
  34. } while (0)
  35. /***************Vendor Specific Register***************/
  36. /**
  37. * @brief MCSR(Mode Control Status Register)
  38. *
  39. */
  40. typedef union {
  41. struct {
  42. uint32_t reserved1 : 1; /* Reserved */
  43. uint32_t energy_is_on : 1; /* Energy is On */
  44. uint32_t reserved2 : 4; /* Reserved */
  45. uint32_t en_alternate_interrupt : 1; /* Enable Alternate Interrupt Mode */
  46. uint32_t reserved3 : 2; /* Reserved */
  47. uint32_t en_far_loopback : 1; /* Enable Far Loopback Mode */
  48. uint32_t reserved4 : 3; /* Reserved */
  49. uint32_t en_energy_detect_powerdown : 1; /* Enable Energy Detect Power Down */
  50. uint32_t reserved5 : 2; /* Reserved */
  51. };
  52. uint32_t val;
  53. } mcsr_reg_t;
  54. #define ETH_PHY_MCSR_REG_ADDR (0x11)
  55. /**
  56. * @brief SMR(Special Modes Register)
  57. *
  58. */
  59. typedef union {
  60. struct {
  61. uint32_t phy_addr : 5; /* PHY Address */
  62. uint32_t mode : 3; /* Transceiver Mode of Operation */
  63. uint32_t reserved : 8; /* Reserved */
  64. };
  65. uint32_t val;
  66. } smr_reg_t;
  67. #define ETH_PHY_SMR_REG_ADDR (0x12)
  68. /**
  69. * @brief SECR(Symbol Error Counter Register)
  70. *
  71. */
  72. typedef union {
  73. struct {
  74. uint32_t symbol_err_count : 16; /* Symbol Error Counter */
  75. };
  76. uint32_t val;
  77. } secr_reg_t;
  78. #define EHT_PHY_SECR_REG_ADDR (0x1A)
  79. /**
  80. * @brief CSIR(Control Status Indications Register)
  81. *
  82. */
  83. typedef union {
  84. struct {
  85. uint32_t reserved1 : 4; /* Reserved */
  86. uint32_t base10_t_polarity : 1; /* Polarity State of 10Base-T */
  87. uint32_t reserved2 : 6; /* Reserved */
  88. uint32_t dis_sqe : 1; /* Disable SQE test(Heartbeat) */
  89. uint32_t reserved3 : 1; /* Reserved */
  90. uint32_t select_channel : 1; /* Manual channel select:MDI(0) or MDIX(1) */
  91. uint32_t reserved4 : 1; /* Reserved */
  92. uint32_t auto_mdix_ctrl : 1; /* Auto-MDIX Control: EN(0) or DE(1) */
  93. };
  94. uint32_t val;
  95. } scsir_reg_t;
  96. #define ETH_PHY_CSIR_REG_ADDR (0x1B)
  97. /**
  98. * @brief ISR(Interrupt Source Register)
  99. *
  100. */
  101. typedef union {
  102. struct {
  103. uint32_t reserved1 : 1; /* Reserved */
  104. uint32_t auto_nego_page_received : 1; /* Auto-Negotiation Page Received */
  105. uint32_t parallel_detect_fault : 1; /* Parallel Detection Fault */
  106. uint32_t auto_nego_lp_acknowledge : 1; /* Auto-Negotiation LP Acknowledge */
  107. uint32_t link_down : 1; /* Link Down */
  108. uint32_t remote_fault_detect : 1; /* Remote Fault Detect */
  109. uint32_t auto_nego_complete : 1; /* Auto-Negotiation Complete */
  110. uint32_t energy_on_generate : 1; /* ENERYON generated */
  111. uint32_t reserved2 : 8; /* Reserved */
  112. };
  113. uint32_t val;
  114. } isfr_reg_t;
  115. #define ETH_PHY_ISR_REG_ADDR (0x1D)
  116. /**
  117. * @brief IMR(Interrupt Mask Register)
  118. *
  119. */
  120. typedef union {
  121. struct {
  122. uint32_t reserved1 : 1; /* Reserved */
  123. uint32_t auto_nego_page_received : 1; /* Auto-Negotiation Page Received */
  124. uint32_t parallel_detect_fault : 1; /* Parallel Detection Fault */
  125. uint32_t auto_nego_lp_acknowledge : 1; /* Auto-Negotiation LP Acknowledge */
  126. uint32_t link_down : 1; /* Link Down */
  127. uint32_t remote_fault_detect : 1; /* Remote Fault Detect */
  128. uint32_t auto_nego_complete : 1; /* Auto-Negotiation Complete */
  129. uint32_t energy_on_generate : 1; /* ENERGY ON generated */
  130. uint32_t reserved2 : 8; /* Reserved */
  131. };
  132. uint32_t val;
  133. } imr_reg_t;
  134. #define ETH_PHY_IMR_REG_ADDR (0x1E)
  135. /**
  136. * @brief PSCSR(PHY Special Control Status Register)
  137. *
  138. */
  139. typedef union {
  140. struct {
  141. uint32_t reserved1 : 2; /* Reserved */
  142. uint32_t speed_indication : 3; /* Speed Indication */
  143. uint32_t reserved2 : 7; /* Reserved */
  144. uint32_t auto_nego_done : 1; /* Auto Negotiation Done */
  145. uint32_t reserved3 : 3; /* Reserved */
  146. };
  147. uint32_t val;
  148. } pscsr_reg_t;
  149. #define ETH_PHY_PSCSR_REG_ADDR (0x1F)
  150. typedef struct {
  151. esp_eth_phy_t parent;
  152. esp_eth_mediator_t *eth;
  153. int addr;
  154. uint32_t reset_timeout_ms;
  155. uint32_t autonego_timeout_ms;
  156. eth_link_t link_status;
  157. int reset_gpio_num;
  158. } phy_lan8720_t;
  159. static esp_err_t lan8720_update_link_duplex_speed(phy_lan8720_t *lan8720)
  160. {
  161. esp_eth_mediator_t *eth = lan8720->eth;
  162. eth_speed_t speed = ETH_SPEED_10M;
  163. eth_duplex_t duplex = ETH_DUPLEX_HALF;
  164. bmsr_reg_t bmsr;
  165. pscsr_reg_t pscsr;
  166. uint32_t peer_pause_ability = false;
  167. anlpar_reg_t anlpar;
  168. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)) == ESP_OK,
  169. "read ANLPAR failed", err);
  170. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
  171. "read BMSR failed", err);
  172. eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
  173. /* check if link status changed */
  174. if (lan8720->link_status != link) {
  175. /* when link up, read negotiation result */
  176. if (link == ETH_LINK_UP) {
  177. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_PSCSR_REG_ADDR, &(pscsr.val)) == ESP_OK,
  178. "read PSCSR failed", err);
  179. switch (pscsr.speed_indication) {
  180. case 1: //10Base-T half-duplex
  181. speed = ETH_SPEED_10M;
  182. duplex = ETH_DUPLEX_HALF;
  183. break;
  184. case 2: //100Base-TX half-duplex
  185. speed = ETH_SPEED_100M;
  186. duplex = ETH_DUPLEX_HALF;
  187. break;
  188. case 5: //10Base-T full-duplex
  189. speed = ETH_SPEED_10M;
  190. duplex = ETH_DUPLEX_FULL;
  191. break;
  192. case 6: //100Base-TX full-duplex
  193. speed = ETH_SPEED_100M;
  194. duplex = ETH_DUPLEX_FULL;
  195. break;
  196. default:
  197. break;
  198. }
  199. PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *)speed) == ESP_OK,
  200. "change speed failed", err);
  201. PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *)duplex) == ESP_OK,
  202. "change duplex failed", err);
  203. /* if we're in duplex mode, and peer has the flow control ability */
  204. if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
  205. peer_pause_ability = 1;
  206. } else {
  207. peer_pause_ability = 0;
  208. }
  209. PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *)peer_pause_ability) == ESP_OK,
  210. "change pause ability failed", err);
  211. }
  212. PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link) == ESP_OK,
  213. "change link failed", err);
  214. lan8720->link_status = link;
  215. }
  216. return ESP_OK;
  217. err:
  218. return ESP_FAIL;
  219. }
  220. static esp_err_t lan8720_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
  221. {
  222. PHY_CHECK(eth, "can't set mediator to null", err);
  223. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  224. lan8720->eth = eth;
  225. return ESP_OK;
  226. err:
  227. return ESP_ERR_INVALID_ARG;
  228. }
  229. static esp_err_t lan8720_get_link(esp_eth_phy_t *phy)
  230. {
  231. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  232. /* Updata information about link, speed, duplex */
  233. PHY_CHECK(lan8720_update_link_duplex_speed(lan8720) == ESP_OK, "update link duplex speed failed", err);
  234. return ESP_OK;
  235. err:
  236. return ESP_FAIL;
  237. }
  238. static esp_err_t lan8720_reset(esp_eth_phy_t *phy)
  239. {
  240. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  241. lan8720->link_status = ETH_LINK_DOWN;
  242. esp_eth_mediator_t *eth = lan8720->eth;
  243. bmcr_reg_t bmcr = {.reset = 1};
  244. PHY_CHECK(eth->phy_reg_write(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
  245. "write BMCR failed", err);
  246. /* wait for reset complete */
  247. uint32_t to = 0;
  248. for (to = 0; to < lan8720->reset_timeout_ms / 10; to++) {
  249. vTaskDelay(pdMS_TO_TICKS(10));
  250. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
  251. "read BMCR failed", err);
  252. if (!bmcr.reset) {
  253. break;
  254. }
  255. }
  256. PHY_CHECK(to < lan8720->reset_timeout_ms / 10, "reset timeout", err);
  257. return ESP_OK;
  258. err:
  259. return ESP_FAIL;
  260. }
  261. static esp_err_t lan8720_reset_hw(esp_eth_phy_t *phy)
  262. {
  263. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  264. if (lan8720->reset_gpio_num >= 0) {
  265. esp_rom_gpio_pad_select_gpio(lan8720->reset_gpio_num);
  266. gpio_set_direction(lan8720->reset_gpio_num, GPIO_MODE_OUTPUT);
  267. gpio_set_level(lan8720->reset_gpio_num, 0);
  268. esp_rom_delay_us(100); // insert min input assert time
  269. gpio_set_level(lan8720->reset_gpio_num, 1);
  270. }
  271. return ESP_OK;
  272. }
  273. static esp_err_t lan8720_negotiate(esp_eth_phy_t *phy)
  274. {
  275. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  276. esp_eth_mediator_t *eth = lan8720->eth;
  277. /* Restart auto negotiation */
  278. bmcr_reg_t bmcr = {
  279. .speed_select = 1, /* 100Mbps */
  280. .duplex_mode = 1, /* Full Duplex */
  281. .en_auto_nego = 1, /* Auto Negotiation */
  282. .restart_auto_nego = 1 /* Restart Auto Negotiation */
  283. };
  284. PHY_CHECK(eth->phy_reg_write(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
  285. /* Wait for auto negotiation complete */
  286. bmsr_reg_t bmsr;
  287. pscsr_reg_t pscsr;
  288. uint32_t to = 0;
  289. for (to = 0; to < lan8720->autonego_timeout_ms / 10; to++) {
  290. vTaskDelay(pdMS_TO_TICKS(10));
  291. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
  292. "read BMSR failed", err);
  293. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_PSCSR_REG_ADDR, &(pscsr.val)) == ESP_OK,
  294. "read PSCSR failed", err);
  295. if (bmsr.auto_nego_complete && pscsr.auto_nego_done) {
  296. break;
  297. }
  298. }
  299. /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */
  300. if (to >= lan8720->autonego_timeout_ms / 10) {
  301. ESP_LOGW(TAG, "auto negotiation timeout");
  302. }
  303. /* Updata information about link, speed, duplex */
  304. PHY_CHECK(lan8720_update_link_duplex_speed(lan8720) == ESP_OK, "update link duplex speed failed", err);
  305. return ESP_OK;
  306. err:
  307. return ESP_FAIL;
  308. }
  309. static esp_err_t lan8720_pwrctl(esp_eth_phy_t *phy, bool enable)
  310. {
  311. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  312. esp_eth_mediator_t *eth = lan8720->eth;
  313. bmcr_reg_t bmcr;
  314. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
  315. "read BMCR failed", err);
  316. if (!enable) {
  317. /* General Power Down Mode */
  318. bmcr.power_down = 1;
  319. } else {
  320. /* Normal operation Mode */
  321. bmcr.power_down = 0;
  322. }
  323. PHY_CHECK(eth->phy_reg_write(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
  324. "write BMCR failed", err);
  325. if (!enable) {
  326. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
  327. "read BMCR failed", err);
  328. PHY_CHECK(bmcr.power_down == 1, "power down failed", err);
  329. } else {
  330. /* wait for power up complete */
  331. uint32_t to = 0;
  332. for (to = 0; to < lan8720->reset_timeout_ms / 10; to++) {
  333. vTaskDelay(pdMS_TO_TICKS(10));
  334. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
  335. "read BMCR failed", err);
  336. if (bmcr.power_down == 0) {
  337. break;
  338. }
  339. }
  340. PHY_CHECK(to < lan8720->reset_timeout_ms / 10, "power up timeout", err);
  341. }
  342. return ESP_OK;
  343. err:
  344. return ESP_FAIL;
  345. }
  346. static esp_err_t lan8720_set_addr(esp_eth_phy_t *phy, uint32_t addr)
  347. {
  348. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  349. lan8720->addr = addr;
  350. return ESP_OK;
  351. }
  352. static esp_err_t lan8720_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
  353. {
  354. PHY_CHECK(addr, "addr can't be null", err);
  355. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  356. *addr = lan8720->addr;
  357. return ESP_OK;
  358. err:
  359. return ESP_ERR_INVALID_ARG;
  360. }
  361. static esp_err_t lan8720_del(esp_eth_phy_t *phy)
  362. {
  363. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  364. free(lan8720);
  365. return ESP_OK;
  366. }
  367. static esp_err_t lan8720_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
  368. {
  369. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  370. esp_eth_mediator_t *eth = lan8720->eth;
  371. /* Set PAUSE function ability */
  372. anar_reg_t anar;
  373. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK,
  374. "read ANAR failed", err);
  375. if (ability) {
  376. anar.asymmetric_pause = 1;
  377. anar.symmetric_pause = 1;
  378. } else {
  379. anar.asymmetric_pause = 0;
  380. anar.symmetric_pause = 0;
  381. }
  382. PHY_CHECK(eth->phy_reg_write(eth, lan8720->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK,
  383. "write ANAR failed", err);
  384. return ESP_OK;
  385. err:
  386. return ESP_FAIL;
  387. }
  388. static esp_err_t lan8720_init(esp_eth_phy_t *phy)
  389. {
  390. phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent);
  391. esp_eth_mediator_t *eth = lan8720->eth;
  392. // Detect PHY address
  393. if (lan8720->addr == ESP_ETH_PHY_ADDR_AUTO) {
  394. PHY_CHECK(esp_eth_detect_phy_addr(eth, &lan8720->addr) == ESP_OK, "Detect PHY address failed", err);
  395. }
  396. /* Power on Ethernet PHY */
  397. PHY_CHECK(lan8720_pwrctl(phy, true) == ESP_OK, "power control failed", err);
  398. /* Reset Ethernet PHY */
  399. PHY_CHECK(lan8720_reset(phy) == ESP_OK, "reset failed", err);
  400. /* Check PHY ID */
  401. phyidr1_reg_t id1;
  402. phyidr2_reg_t id2;
  403. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK,
  404. "read ID1 failed", err);
  405. PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK,
  406. "read ID2 failed", err);
  407. PHY_CHECK(id1.oui_msb == 0x7 && id2.oui_lsb == 0x30 && id2.vendor_model == 0xF, "wrong chip ID", err);
  408. return ESP_OK;
  409. err:
  410. return ESP_FAIL;
  411. }
  412. static esp_err_t lan8720_deinit(esp_eth_phy_t *phy)
  413. {
  414. /* Power off Ethernet PHY */
  415. PHY_CHECK(lan8720_pwrctl(phy, false) == ESP_OK, "power control failed", err);
  416. return ESP_OK;
  417. err:
  418. return ESP_FAIL;
  419. }
  420. esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config)
  421. {
  422. PHY_CHECK(config, "can't set phy config to null", err);
  423. phy_lan8720_t *lan8720 = calloc(1, sizeof(phy_lan8720_t));
  424. PHY_CHECK(lan8720, "calloc lan8720 failed", err);
  425. lan8720->addr = config->phy_addr;
  426. lan8720->reset_gpio_num = config->reset_gpio_num;
  427. lan8720->reset_timeout_ms = config->reset_timeout_ms;
  428. lan8720->link_status = ETH_LINK_DOWN;
  429. lan8720->autonego_timeout_ms = config->autonego_timeout_ms;
  430. lan8720->parent.reset = lan8720_reset;
  431. lan8720->parent.reset_hw = lan8720_reset_hw;
  432. lan8720->parent.init = lan8720_init;
  433. lan8720->parent.deinit = lan8720_deinit;
  434. lan8720->parent.set_mediator = lan8720_set_mediator;
  435. lan8720->parent.negotiate = lan8720_negotiate;
  436. lan8720->parent.get_link = lan8720_get_link;
  437. lan8720->parent.pwrctl = lan8720_pwrctl;
  438. lan8720->parent.get_addr = lan8720_get_addr;
  439. lan8720->parent.set_addr = lan8720_set_addr;
  440. lan8720->parent.advertise_pause_ability = lan8720_advertise_pause_ability;
  441. lan8720->parent.del = lan8720_del;
  442. return &(lan8720->parent);
  443. err:
  444. return NULL;
  445. }