emac.c 12 KB

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