drv_emac.c 14 KB

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