emac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include "emac.h"
  11. #include "lwipopts.h"
  12. #include <netif/ethernetif.h>
  13. #define EMAC_PHY_AUTO 0
  14. #define EMAC_PHY_10MBIT 1
  15. #define EMAC_PHY_100MBIT 2
  16. #define MAX_ADDR_LEN 6
  17. struct lpc17xx_emac
  18. {
  19. /* inherit from ethernet device */
  20. struct eth_device parent;
  21. rt_uint8_t phy_mode;
  22. /* interface address info. */
  23. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  24. };
  25. static struct lpc17xx_emac lpc17xx_emac_device;
  26. static struct rt_semaphore sem_lock;
  27. static struct rt_event tx_event;
  28. /* Local Function Prototypes */
  29. static void write_PHY (rt_uint32_t PhyReg, rt_uint32_t Value);
  30. static rt_uint16_t read_PHY (rt_uint8_t PhyReg) ;
  31. void ENET_IRQHandler(void)
  32. {
  33. rt_uint32_t status;
  34. /* enter interrupt */
  35. rt_interrupt_enter();
  36. status = LPC_EMAC->IntStatus;
  37. if (status & INT_RX_DONE)
  38. {
  39. /* Disable EMAC RxDone interrupts. */
  40. LPC_EMAC->IntEnable = INT_TX_DONE;
  41. /* a frame has been received */
  42. eth_device_ready(&(lpc17xx_emac_device.parent));
  43. }
  44. else if (status & INT_TX_DONE)
  45. {
  46. /* set event */
  47. rt_event_send(&tx_event, 0x01);
  48. }
  49. if (status & INT_RX_OVERRUN)
  50. {
  51. rt_kprintf("rx overrun\n");
  52. }
  53. if (status & INT_TX_UNDERRUN)
  54. {
  55. rt_kprintf("tx underrun\n");
  56. }
  57. /* Clear the interrupt. */
  58. LPC_EMAC->IntClear = status;
  59. /* leave interrupt */
  60. rt_interrupt_leave();
  61. }
  62. /* phy write */
  63. static void write_PHY (rt_uint32_t PhyReg, rt_uint32_t Value)
  64. {
  65. unsigned int tout;
  66. LPC_EMAC->MADR = DP83848C_DEF_ADR | PhyReg;
  67. LPC_EMAC->MWTD = Value;
  68. /* Wait utill operation completed */
  69. tout = 0;
  70. for (tout = 0; tout < MII_WR_TOUT; tout++)
  71. {
  72. if ((LPC_EMAC->MIND & MIND_BUSY) == 0)
  73. {
  74. break;
  75. }
  76. }
  77. }
  78. /* phy read */
  79. static rt_uint16_t read_PHY (rt_uint8_t PhyReg)
  80. {
  81. rt_uint32_t tout;
  82. LPC_EMAC->MADR = DP83848C_DEF_ADR | PhyReg;
  83. LPC_EMAC->MCMD = MCMD_READ;
  84. /* Wait until operation completed */
  85. tout = 0;
  86. for (tout = 0; tout < MII_RD_TOUT; tout++)
  87. {
  88. if ((LPC_EMAC->MIND & MIND_BUSY) == 0)
  89. {
  90. break;
  91. }
  92. }
  93. LPC_EMAC->MCMD = 0;
  94. return (LPC_EMAC->MRDD);
  95. }
  96. /* init rx descriptor */
  97. rt_inline void rx_descr_init (void)
  98. {
  99. rt_uint32_t i;
  100. for (i = 0; i < NUM_RX_FRAG; i++)
  101. {
  102. RX_DESC_PACKET(i) = RX_BUF(i);
  103. RX_DESC_CTRL(i) = RCTRL_INT | (ETH_FRAG_SIZE-1);
  104. RX_STAT_INFO(i) = 0;
  105. RX_STAT_HASHCRC(i) = 0;
  106. }
  107. /* Set EMAC Receive Descriptor Registers. */
  108. LPC_EMAC->RxDescriptor = RX_DESC_BASE;
  109. LPC_EMAC->RxStatus = RX_STAT_BASE;
  110. LPC_EMAC->RxDescriptorNumber = NUM_RX_FRAG-1;
  111. /* Rx Descriptors Point to 0 */
  112. LPC_EMAC->RxConsumeIndex = 0;
  113. }
  114. /* init tx descriptor */
  115. rt_inline void tx_descr_init (void)
  116. {
  117. rt_uint32_t i;
  118. for (i = 0; i < NUM_TX_FRAG; i++)
  119. {
  120. TX_DESC_PACKET(i) = TX_BUF(i);
  121. TX_DESC_CTRL(i) = (1ul<<31) | (1ul<<30) | (1ul<<29) | (1ul<<28) | (1ul<<26) | (ETH_FRAG_SIZE-1);
  122. TX_STAT_INFO(i) = 0;
  123. }
  124. /* Set EMAC Transmit Descriptor Registers. */
  125. LPC_EMAC->TxDescriptor = TX_DESC_BASE;
  126. LPC_EMAC->TxStatus = TX_STAT_BASE;
  127. LPC_EMAC->TxDescriptorNumber = NUM_TX_FRAG-1;
  128. /* Tx Descriptors Point to 0 */
  129. LPC_EMAC->TxProduceIndex = 0;
  130. }
  131. static rt_err_t lpc17xx_emac_init(rt_device_t dev)
  132. {
  133. /* Initialize the EMAC ethernet controller. */
  134. rt_uint32_t regv, tout, id1, id2;
  135. /* Power Up the EMAC controller. */
  136. LPC_SC->PCONP |= 0x40000000;
  137. /* Enable P1 Ethernet Pins. */
  138. LPC_PINCON->PINSEL2 = 0x50150105;
  139. LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & ~0x0000000F) | 0x00000005;
  140. /* Reset all EMAC internal modules. */
  141. LPC_EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX |
  142. MAC1_SIM_RES | MAC1_SOFT_RES;
  143. LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES;
  144. /* A short delay after reset. */
  145. for (tout = 100; tout; tout--);
  146. /* Initialize MAC control registers. */
  147. LPC_EMAC->MAC1 = MAC1_PASS_ALL;
  148. LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
  149. LPC_EMAC->MAXF = ETH_MAX_FLEN;
  150. LPC_EMAC->CLRT = CLRT_DEF;
  151. LPC_EMAC->IPGR = IPGR_DEF;
  152. /* PCLK=18MHz, clock select=6, MDC=18/6=3MHz */
  153. /* Enable Reduced MII interface. */
  154. LPC_EMAC->MCFG = MCFG_CLK_DIV20 | MCFG_RES_MII;
  155. for (tout = 100; tout; tout--);
  156. LPC_EMAC->MCFG = MCFG_CLK_DIV20;
  157. /* Enable Reduced MII interface. */
  158. LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM | CR_PASS_RX_FILT;
  159. /* Reset Reduced MII Logic. */
  160. LPC_EMAC->SUPP = SUPP_RES_RMII | SUPP_SPEED;
  161. for (tout = 100; tout; tout--);
  162. LPC_EMAC->SUPP = SUPP_SPEED;
  163. /* Put the PHY in reset mode */
  164. write_PHY (PHY_REG_BMCR, 0x8000);
  165. for (tout = 1000; tout; tout--);
  166. /* Wait for hardware reset to end. */
  167. for (tout = 0; tout < 10000; tout++)
  168. {
  169. regv = read_PHY (PHY_REG_BMCR);
  170. if (!(regv & 0x8000))
  171. {
  172. /* Reset complete */
  173. break;
  174. }
  175. }
  176. if (tout >= 10000)
  177. {
  178. //return -RT_ERROR; /* reset failed */
  179. rt_kprintf("\tPHY Read PHY_REG_BMSR,Reset timeout,tout: %d.\n",tout);
  180. }
  181. /* Check if this is a DP83848C PHY. */
  182. id1 = read_PHY (PHY_REG_IDR1);
  183. id2 = read_PHY (PHY_REG_IDR2);
  184. if (((id1 << 16) | (id2 & 0xFFF0)) != DP83848C_ID)
  185. {
  186. // return -RT_ERROR;
  187. rt_kprintf("\tPHY Read PHY_REG_IDRx,PHY chip isn't DP83848C,Chip ID is %d.\n",((id1 << 16) | (id2 & 0xFFF0)));
  188. }
  189. else
  190. {
  191. /* Configure the PHY device */
  192. /* Configure the PHY device */
  193. switch (lpc17xx_emac_device.phy_mode)
  194. {
  195. case EMAC_PHY_AUTO:
  196. /* Use auto negotiation about the link speed. */
  197. write_PHY (PHY_REG_BMCR, PHY_AUTO_NEG);
  198. /* Wait to complete Auto_Negotiation. */
  199. for (tout = 0; tout < 200000; tout++)
  200. {
  201. regv = read_PHY (PHY_REG_BMSR);
  202. if (regv & 0x0020)
  203. {
  204. /* Auto negotiation Complete. */
  205. break;
  206. }
  207. }
  208. if(tout >= 200000)
  209. {
  210. rt_kprintf("\tPHY Read PHY_REG_BMSR,Auto nego timeout,tout: %d.\n",tout);
  211. }
  212. break;
  213. case EMAC_PHY_10MBIT:
  214. /* Connect at 10MBit */
  215. write_PHY (PHY_REG_BMCR, PHY_FULLD_10M);
  216. break;
  217. case EMAC_PHY_100MBIT:
  218. /* Connect at 100MBit */
  219. write_PHY (PHY_REG_BMCR, PHY_FULLD_100M);
  220. break;
  221. }
  222. }
  223. //if (tout >= 0x100000) return -RT_ERROR; // auto_neg failed
  224. /* Check the link status. */
  225. for (tout = 0; tout < 100; tout++)
  226. {
  227. regv = read_PHY (PHY_REG_STS);
  228. if (regv & 0x0001)
  229. {
  230. /* Link is on. */
  231. break;
  232. }
  233. }
  234. if (tout >= 100)
  235. {
  236. //return -RT_ERROR;
  237. rt_kprintf("\tPHY Read PHY_REG_BMSR,Link on timeout,tout: %d.\n",tout);
  238. }
  239. /* Configure Full/Half Duplex mode. */
  240. if (regv & 0x0004)
  241. {
  242. /* Full duplex is enabled. */
  243. LPC_EMAC->MAC2 |= MAC2_FULL_DUP;
  244. LPC_EMAC->Command |= CR_FULL_DUP;
  245. LPC_EMAC->IPGT = IPGT_FULL_DUP;
  246. }
  247. else
  248. {
  249. /* Half duplex mode. */
  250. LPC_EMAC->IPGT = IPGT_HALF_DUP;
  251. }
  252. /* Configure 100MBit/10MBit mode. */
  253. if (regv & 0x0002)
  254. {
  255. /* 10MBit mode. */
  256. LPC_EMAC->SUPP = 0;
  257. }
  258. else
  259. {
  260. /* 100MBit mode. */
  261. LPC_EMAC->SUPP = SUPP_SPEED;
  262. }
  263. /* Set the Ethernet MAC Address registers */
  264. LPC_EMAC->SA0 = (lpc17xx_emac_device.dev_addr[1]<<8) | lpc17xx_emac_device.dev_addr[0];
  265. LPC_EMAC->SA1 = (lpc17xx_emac_device.dev_addr[3]<<8) | lpc17xx_emac_device.dev_addr[2];
  266. LPC_EMAC->SA2 = (lpc17xx_emac_device.dev_addr[5]<<8) | lpc17xx_emac_device.dev_addr[4];
  267. /* Initialize Tx and Rx DMA Descriptors */
  268. rx_descr_init ();
  269. tx_descr_init ();
  270. /* Receive Broadcast and Perfect Match Packets */
  271. LPC_EMAC->RxFilterCtrl = RFC_BCAST_EN | RFC_PERFECT_EN;
  272. /* Reset all interrupts */
  273. LPC_EMAC->IntClear = 0xFFFF;
  274. /* Enable EMAC interrupts. */
  275. LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE;
  276. /* Enable receive and transmit mode of MAC Ethernet core */
  277. LPC_EMAC->Command |= (CR_RX_EN | CR_TX_EN);
  278. LPC_EMAC->MAC1 |= MAC1_REC_EN;
  279. /* Enable the ENET Interrupt */
  280. NVIC_EnableIRQ(ENET_IRQn);
  281. return RT_EOK;
  282. }
  283. static rt_err_t lpc17xx_emac_open(rt_device_t dev, rt_uint16_t oflag)
  284. {
  285. return RT_EOK;
  286. }
  287. static rt_err_t lpc17xx_emac_close(rt_device_t dev)
  288. {
  289. return RT_EOK;
  290. }
  291. static rt_size_t lpc17xx_emac_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  292. {
  293. rt_set_errno(-RT_ENOSYS);
  294. return 0;
  295. }
  296. static rt_size_t lpc17xx_emac_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  297. {
  298. rt_set_errno(-RT_ENOSYS);
  299. return 0;
  300. }
  301. static rt_err_t lpc17xx_emac_control(rt_device_t dev, int cmd, void *args)
  302. {
  303. switch (cmd)
  304. {
  305. case NIOCTL_GADDR:
  306. /* get mac address */
  307. if (args) rt_memcpy(args, lpc17xx_emac_device.dev_addr, 6);
  308. else return -RT_ERROR;
  309. break;
  310. default :
  311. break;
  312. }
  313. return RT_EOK;
  314. }
  315. /* EtherNet Device Interface */
  316. /* transmit packet. */
  317. rt_err_t lpc17xx_emac_tx( rt_device_t dev, struct pbuf* p)
  318. {
  319. rt_uint32_t Index, IndexNext;
  320. struct pbuf *q;
  321. rt_uint8_t *ptr;
  322. /* calculate next index */
  323. IndexNext = LPC_EMAC->TxProduceIndex + 1;
  324. if(IndexNext > LPC_EMAC->TxDescriptorNumber) IndexNext = 0;
  325. /* check whether block is full */
  326. while (IndexNext == LPC_EMAC->TxConsumeIndex)
  327. {
  328. rt_err_t result;
  329. rt_uint32_t recved;
  330. /* there is no block yet, wait a flag */
  331. result = rt_event_recv(&tx_event, 0x01,
  332. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &recved);
  333. RT_ASSERT(result == RT_EOK);
  334. }
  335. /* lock EMAC device */
  336. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  337. /* get produce index */
  338. Index = LPC_EMAC->TxProduceIndex;
  339. /* calculate next index */
  340. IndexNext = LPC_EMAC->TxProduceIndex + 1;
  341. if(IndexNext > LPC_EMAC->TxDescriptorNumber)
  342. IndexNext = 0;
  343. /* copy data to tx buffer */
  344. q = p;
  345. ptr = (rt_uint8_t*)TX_BUF(Index);
  346. while (q)
  347. {
  348. memcpy(ptr, q->payload, q->len);
  349. ptr += q->len;
  350. q = q->next;
  351. }
  352. TX_DESC_CTRL(Index) &= ~0x7ff;
  353. TX_DESC_CTRL(Index) |= (p->tot_len - 1) & 0x7ff;
  354. /* change index to the next */
  355. LPC_EMAC->TxProduceIndex = IndexNext;
  356. /* unlock EMAC device */
  357. rt_sem_release(&sem_lock);
  358. return RT_EOK;
  359. }
  360. /* reception packet. */
  361. struct pbuf *lpc17xx_emac_rx(rt_device_t dev)
  362. {
  363. struct pbuf* p;
  364. rt_uint32_t size;
  365. rt_uint32_t Index;
  366. /* init p pointer */
  367. p = RT_NULL;
  368. /* lock EMAC device */
  369. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  370. Index = LPC_EMAC->RxConsumeIndex;
  371. if(Index != LPC_EMAC->RxProduceIndex)
  372. {
  373. size = (RX_STAT_INFO(Index) & 0x7ff)+1;
  374. if (size > ETH_FRAG_SIZE) size = ETH_FRAG_SIZE;
  375. /* allocate buffer */
  376. p = pbuf_alloc(PBUF_LINK, size, PBUF_RAM);
  377. if (p != RT_NULL)
  378. {
  379. struct pbuf* q;
  380. rt_uint8_t *ptr;
  381. ptr = (rt_uint8_t*)RX_BUF(Index);
  382. for (q = p; q != RT_NULL; q= q->next)
  383. {
  384. memcpy(q->payload, ptr, q->len);
  385. ptr += q->len;
  386. }
  387. }
  388. /* move Index to the next */
  389. if(++Index > LPC_EMAC->RxDescriptorNumber)
  390. Index = 0;
  391. /* set consume index */
  392. LPC_EMAC->RxConsumeIndex = Index;
  393. }
  394. else
  395. {
  396. /* Enable RxDone interrupt */
  397. LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE;
  398. }
  399. /* unlock EMAC device */
  400. rt_sem_release(&sem_lock);
  401. return p;
  402. }
  403. int lpc17xx_emac_hw_init(void)
  404. {
  405. rt_event_init(&tx_event, "tx_event", RT_IPC_FLAG_FIFO);
  406. rt_sem_init(&sem_lock, "eth_lock", 1, RT_IPC_FLAG_FIFO);
  407. /* set auto negotiation mode */
  408. lpc17xx_emac_device.phy_mode = EMAC_PHY_AUTO;
  409. // OUI 00-60-37 NXP Semiconductors
  410. lpc17xx_emac_device.dev_addr[0] = 0x00;
  411. lpc17xx_emac_device.dev_addr[1] = 0x60;
  412. lpc17xx_emac_device.dev_addr[2] = 0x37;
  413. /* set mac address: (only for test) */
  414. lpc17xx_emac_device.dev_addr[3] = 0x12;
  415. lpc17xx_emac_device.dev_addr[4] = 0x34;
  416. lpc17xx_emac_device.dev_addr[5] = 0x56;
  417. lpc17xx_emac_device.parent.parent.init = lpc17xx_emac_init;
  418. lpc17xx_emac_device.parent.parent.open = lpc17xx_emac_open;
  419. lpc17xx_emac_device.parent.parent.close = lpc17xx_emac_close;
  420. lpc17xx_emac_device.parent.parent.read = lpc17xx_emac_read;
  421. lpc17xx_emac_device.parent.parent.write = lpc17xx_emac_write;
  422. lpc17xx_emac_device.parent.parent.control = lpc17xx_emac_control;
  423. lpc17xx_emac_device.parent.parent.user_data = RT_NULL;
  424. lpc17xx_emac_device.parent.eth_rx = lpc17xx_emac_rx;
  425. lpc17xx_emac_device.parent.eth_tx = lpc17xx_emac_tx;
  426. eth_device_init(&(lpc17xx_emac_device.parent), "e0");
  427. return 0;
  428. }
  429. INIT_DEVICE_EXPORT(lpc17xx_emac_hw_init);