1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2015 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Contribution by Nitin Bhaskar(nitin.bhaskar.27.09@gmail.com)
18 #include "ethernet_api.h"
22 #include "mbed_interface.h"
23 #include "toolchain.h"
24 #include "mbed_error.h"
28 #define NEW_ETH_BUFFER 0
32 #define NUM_RX_FRAG 4 // Number of Rx Fragments (== packets)
33 #define NUM_TX_FRAG 3 // Number of Tx Fragments (== packets)
35 #define ETH_MAX_FLEN 1536 // Maximum Ethernet Frame Size
36 #define ETH_FRAG_SIZE ETH_MAX_FLEN // Packet Fragment size (same as packet length)
40 // Memfree calculation:
41 // (16 * 1024) - ((2 * 4 * NUM_RX) + (2 * 4 * NUM_RX) + (0x300 * NUM_RX) +
42 // (2 * 4 * NUM_TX) + (1 * 4 * NUM_TX) + (0x300 * NUM_TX)) = 8556
43 /* EMAC Memory Buffer configuration for 16K Ethernet RAM. */
44 #define NUM_RX_FRAG 4 /* Num.of RX Fragments 4*1536= 6.0kB */
45 #define NUM_TX_FRAG 3 /* Num.of TX Fragments 3*1536= 4.6kB */
46 //#define ETH_FRAG_SIZE 1536 /* Packet Fragment size 1536 Bytes */
48 //#define ETH_MAX_FLEN 1536 /* Max. Ethernet Frame Size */
49 #define ETH_FRAG_SIZE 0x300 /* Packet Fragment size 1536/2 Bytes */
50 #define ETH_MAX_FLEN 0x300 /* Max. Ethernet Frame Size */
52 const int ethernet_MTU_SIZE = 0x300;
56 #define ETHERNET_ADDR_SIZE 6
58 /* Descriptors Fields bits */
59 #define TRDES_OWN_BIT (1U<<31) /* Own bit in RDES0 & TDES0 */
60 #define RX_END_RING (1<<15) /* Receive End of Ring bit in RDES1 */
61 #define RX_NXTDESC_FLAG (1<<14) /* Second Address Chained bit in RDES1 */
62 #define TX_LAST_SEGM (1<<29) /* Last Segment bit in TDES0 */
63 #define TX_FIRST_SEGM (1<<28) /* First Segment bit in TDES0 */
64 #define TX_END_RING (1<<21) /* Transmit End of Ring bit in TDES0 */
65 #define TX_NXTDESC_FLAG (1<<20) /* Second Address Chained bit in TDES0 */
67 PACKED struct RX_DESC_TypeDef { /* RX Descriptor struct */
70 unsigned int BufAddr1;
71 unsigned int NextDescAddr;
73 typedef struct RX_DESC_TypeDef RX_DESC_TypeDef;
75 PACKED struct TX_DESC_TypeDef { /* TX Descriptor struct */
78 unsigned int BufAddr1;
79 unsigned int NextDescAddr;
81 typedef struct TX_DESC_TypeDef TX_DESC_TypeDef;
83 /* ETHMODE RMII SELECT */
84 #define RMII_SELECT 0x04
85 /* define to tell PHY about write operation */
86 #define MII_WRITE (1 << 1)
87 /* define to tell PHY about read operation */
88 #define MII_READ (0 << 1)
89 /* define to enable duplex mode */
90 #define MAC_DUPLEX_MODE (1 << 11)
92 /* MAC_FRAME_FILTER register bit defines */
93 #define MAC_FRAME_FILTER_PR (1 << 0) /* Promiscuous Mode */
94 #define MAC_FRAME_FILTER_RA (1UL << 31) /* Receive all */
96 /* MAC_CONFIG register bit defines */
97 #define MAC_CONFIG_RE (1 << 2) /* Receiver enable */
98 #define MAC_CONFIG_TE (1 << 3) /* Transmitter Enable */
100 /* DMA_OP_MODE register bit defines */
101 #define DMA_OP_MODE_SSR (1 << 1) /* Start/stop receive */
102 #define DMA_OP_MODE_SST (1 << 13) /* Start/Stop Transmission Command */
104 /* DMA_INT_EN register bit defines */
105 #define DMA_INT_EN_TIE (1 << 0) /* Transmit interrupt enable */
106 #define DMA_INT_EN_TSE (1 << 1) /* Transmit stopped enable */
107 #define DMA_INT_EN_TUE (1 << 2) /* Transmit buffer unavailable enable */
108 #define DMA_INT_EN_TJE (1 << 3) /* Transmit jabber timeout enable */
109 #define DMA_INT_EN_OVE (1 << 4) /* Overflow interrupt enable */
110 #define DMA_INT_EN_UNE (1 << 5) /* Underflow interrupt enable */
111 #define DMA_INT_EN_RIE (1 << 6) /* Receive interrupt enable */
112 #define DMA_INT_EN_RUE (1 << 7) /* Receive buffer unavailable enable */
113 #define DMA_INT_EN_RSE (1 << 8) /* Received stopped enable */
114 #define DMA_INT_EN_RWE (1 << 9) /* Receive watchdog timeout enable */
115 #define DMA_INT_EN_ETE (1 << 10) /* Early transmit interrupt enable */
116 #define DMA_INT_EN_FBE (1 << 13) /* Fatal bus error enable */
117 #define DMA_INT_EN_ERE (1 << 14) /* Early receive interrupt enable */
118 #define DMA_INT_EN_AIE (1 << 15) /* Abnormal interrupt summary enable */
119 #define DMA_INT_EN_NIE (1 << 16) /* Normal interrupt summary enable */
123 /* PHY Support Register */
124 #define SUPP_SPEED 0x00004000 /* Reduced MII Logic Current Speed */
125 //#define SUPP_RES_RMII 0x00000800 /* Reset Reduced MII Logic */
126 #define SUPP_RES_RMII 0x00000000 /* Reset Reduced MII Logic */
128 /* MII Management Command Register */
129 #define MCMD_READ 0x00000001 /* MII Read */
130 #define MCMD_SCAN 0x00000002 /* MII Scan continuously */
132 #define MII_WR_TOUT 0x00050000 /* MII Write timeout count */
133 #define MII_RD_TOUT 0x00050000 /* MII Read timeout count */
135 /* MII Management Address Register */
136 #define MADR_REG_ADR 0x0000001F /* MII Register Address Mask */
137 #define MADR_PHY_ADR 0x00001F00 /* PHY Address Mask */
139 /* MII Management Indicators Register */
140 #define MIND_BUSY 0x00000001 /* MII is Busy */
141 #define MIND_SCAN 0x00000002 /* MII Scanning in Progress */
142 #define MIND_NOT_VAL 0x00000004 /* MII Read Data not valid */
143 #define MIND_MII_LINK_FAIL 0x00000008 /* MII Link Failed */
145 /* DP83848C PHY Registers */
146 #define PHY_REG_BMCR 0x00 /* Basic Mode Control Register */
147 #define PHY_REG_BMSR 0x01 /* Basic Mode Status Register */
148 #define PHY_REG_IDR1 0x02 /* PHY Identifier 1 */
149 #define PHY_REG_IDR2 0x03 /* PHY Identifier 2 */
150 #define PHY_REG_ANAR 0x04 /* Auto-Negotiation Advertisement */
151 #define PHY_REG_ANLPAR 0x05 /* Auto-Neg. Link Partner Abitily */
152 #define PHY_REG_ANER 0x06 /* Auto-Neg. Expansion Register */
153 #define PHY_REG_ANNPTR 0x07 /* Auto-Neg. Next Page TX */
155 /* PHY Extended Registers */
156 #define PHY_REG_STS 0x10 /* Status Register */
157 #define PHY_REG_MICR 0x11 /* MII Interrupt Control Register */
158 #define PHY_REG_MISR 0x12 /* MII Interrupt Status Register */
159 #define PHY_REG_FCSCR 0x14 /* False Carrier Sense Counter */
160 #define PHY_REG_RECR 0x15 /* Receive Error Counter */
161 #define PHY_REG_PCSR 0x16 /* PCS Sublayer Config. and Status */
162 #define PHY_REG_RBR 0x17 /* RMII and Bypass Register */
163 #define PHY_REG_LEDCR 0x18 /* LED Direct Control Register */
164 #define PHY_REG_PHYCR 0x19 /* PHY Control Register */
165 #define PHY_REG_10BTSCR 0x1A /* 10Base-T Status/Control Register */
166 #define PHY_REG_CDCTRL1 0x1B /* CD Test Control and BIST Extens. */
167 #define PHY_REG_EDCR 0x1D /* Energy Detect Control Register */
169 #define PHY_REG_SCSR 0x1F /* PHY Special Control/Status Register */
171 #define PHY_FULLD_100M 0x2100 /* Full Duplex 100Mbit */
172 #define PHY_HALFD_100M 0x2000 /* Half Duplex 100Mbit */
173 #define PHY_FULLD_10M 0x0100 /* Full Duplex 10Mbit */
174 #define PHY_HALFD_10M 0x0000 /* Half Duplex 10MBit */
175 #define PHY_AUTO_NEG 0x1000 /* Select Auto Negotiation */
177 #define DP83848C_DEF_ADR 0x01 /* Default PHY device address */
178 #define DP83848C_ID 0x20005C90 /* PHY Identifier - DP83848C */
180 #define LAN8720_ID 0x0007C0F0 /* PHY Identifier - LAN8720 */
182 #define PHY_STS_LINK 0x0001 /* PHY Status Link Mask */
183 #define PHY_STS_SPEED 0x0002 /* PHY Status Speed Mask */
184 #define PHY_STS_DUPLEX 0x0004 /* PHY Status Duplex Mask */
186 #define PHY_BMCR_RESET 0x8000 /* PHY Reset */
188 #define PHY_BMSR_LINK 0x0004 /* PHY BMSR Link valid */
190 #define PHY_SCSR_100MBIT 0x0008 /* Speed: 1=100 MBit, 0=10Mbit */
191 #define PHY_SCSR_DUPLEX 0x0010 /* PHY Duplex Mask */
193 static int phy_read(unsigned int PhyReg);
194 static int phy_write(unsigned int PhyReg, unsigned short Data);
196 static void txdscr_init(void);
197 static void rxdscr_init(void);
199 #if defined (__ICCARM__)
201 #elif defined(TOOLCHAIN_GCC_CR)
202 # define AHBSRAM1 __attribute__((section(".data.$RamPeriph32")))
204 # define AHBSRAM1 __attribute__((section("AHBSRAM1"),aligned))
207 AHBSRAM1 volatile uint8_t rxbuf[NUM_RX_FRAG][ETH_FRAG_SIZE];
208 AHBSRAM1 volatile uint8_t txbuf[NUM_TX_FRAG][ETH_FRAG_SIZE];
209 AHBSRAM1 volatile RX_DESC_TypeDef rxdesc[NUM_RX_FRAG];
210 AHBSRAM1 volatile TX_DESC_TypeDef txdesc[NUM_TX_FRAG];
213 #define min(x, y) (((x)<(y))?(x):(y))
216 static uint32_t phy_id = 0;
217 static uint32_t TxDescIndex = 0;
218 static uint32_t RxDescIndex = 0;
219 static uint32_t RxOffset = 0;
221 /*----------------------------------------------------------------------------
222 Ethernet Device initialize
223 *----------------------------------------------------------------------------*/
227 char mac[ETHERNET_ADDR_SIZE];
229 pin_function(PC_0, (SCU_MODE_INACT | FUNC3)); /* Enable ENET RX CLK */
230 pin_function(P1_19, (SCU_MODE_INACT | FUNC0)); /* Enable ENET TX CLK */
232 /* Ethernet pinmuxing */
233 pin_function(P2_0, SCU_PINIO_FAST | FUNC7); /* ENET_MDC */
234 pin_function(P1_17, SCU_PINIO_FAST | FUNC3); /* ENET_MDIO */
235 pin_function(P1_18, SCU_PINIO_FAST | FUNC3); /* ENET_TXD0 */
236 pin_function(P1_20, SCU_PINIO_FAST | FUNC3); /* ENET_TXD1 */
237 pin_function(P1_19, SCU_PINIO_FAST | FUNC0); /* ENET_REF */
238 pin_function(P0_1, SCU_PINIO_FAST | FUNC6); /* ENET_TX_EN */
239 pin_function(P1_15, SCU_PINIO_FAST | FUNC3); /* ENET_RXD0 */
240 pin_function(P0_0, SCU_PINIO_FAST | FUNC2); /* ENET_RXD1 */
241 pin_function(P1_16, SCU_PINIO_FAST | FUNC3); /* ENET_CRS */
242 pin_function(PC_9, SCU_PINIO_FAST | FUNC3); /* ENET_RX_ER */
243 pin_function(P1_16, SCU_PINIO_FAST | FUNC7); /* ENET_RXDV */
245 LPC_CREG->CREG6 |= RMII_SELECT;
247 /* perform RGU soft reset */
248 LPC_RGU->RESET_CTRL0 = 1 << 22;
249 LPC_RGU->RESET_CTRL0 = 0;
251 /* Wait until reset is performed */
253 if (LPC_RGU->RESET_ACTIVE_STATUS0 & (1 << 22))
257 /* Reset MAC DMA Controller */
258 LPC_ETHERNET->DMA_BUS_MODE |= 0x01;
259 while(LPC_ETHERNET->DMA_BUS_MODE & 0x01);
261 phy_write(PHY_REG_BMCR, PHY_BMCR_RESET); /* perform PHY reset */
263 for(tout = 0x20000; ; tout--) { /* Wait for hardware reset to end. */
264 regv = phy_read(PHY_REG_BMCR);
265 if(regv < 0 || tout == 0) {
266 return -1; /* Error */
268 if(!(regv & PHY_BMCR_RESET)) {
269 break; /* Reset complete. */
273 phy_id = (phy_read(PHY_REG_IDR1) << 16);
274 phy_id |= (phy_read(PHY_REG_IDR2) & 0XFFF0);
276 if (phy_id != DP83848C_ID && phy_id != LAN8720_ID) {
277 error("Unknown Ethernet PHY (%x)", (unsigned int)phy_id);
280 ethernet_set_link(-1, 0);
282 /* Set the Ethernet MAC Address registers */
283 ethernet_address(mac);
284 LPC_ETHERNET->MAC_ADDR0_HIGH = (mac[5] << 8) | mac[4];
285 LPC_ETHERNET->MAC_ADDR0_LOW = (mac[3] << 24) | (mac[2] << 16) | (mac[1] << 8) | mac[0];
287 txdscr_init(); /* initialize DMA TX Descriptor */
288 rxdscr_init(); /* initialize DMA RX Descriptor */
290 /* Configure Filter */
291 LPC_ETHERNET->MAC_FRAME_FILTER = MAC_FRAME_FILTER_PR | MAC_FRAME_FILTER_RA;
293 /* Enable Receiver and Transmitter */
294 LPC_ETHERNET->MAC_CONFIG |= (MAC_CONFIG_RE | MAC_CONFIG_TE);
296 //LPC_ETHERNET->DMA_INT_EN = DMA_INT_EN_NIE | DMA_INT_EN_RIE | DMA_INT_EN_TJE; /* Enable EMAC interrupts. */
298 /* Start Transmission & Receive processes */
299 LPC_ETHERNET->DMA_OP_MODE |= (DMA_OP_MODE_SST | DMA_OP_MODE_SSR);
304 /*----------------------------------------------------------------------------
305 Ethernet Device Uninitialize
306 *----------------------------------------------------------------------------*/
311 /*----------------------------------------------------------------------------
313 *----------------------------------------------------------------------------*/
314 int ethernet_write(const char *data, int slen)
316 if (slen > ETH_FRAG_SIZE)
319 txdesc[TxDescIndex].Ctrl = slen;
320 memcpy((void *)txdesc[TxDescIndex].BufAddr1, data, slen);
324 /*----------------------------------------------------------------------------
326 *----------------------------------------------------------------------------*/
329 int s = txdesc[TxDescIndex].Ctrl;
330 txdesc[TxDescIndex].Status |= TRDES_OWN_BIT;
331 LPC_ETHERNET->DMA_TRANS_POLL_DEMAND = 1; // Wake Up the DMA if it's in Suspended Mode
333 if (TxDescIndex == NUM_TX_FRAG)
339 /*----------------------------------------------------------------------------
341 *----------------------------------------------------------------------------*/
342 int ethernet_receive()
345 for (i = RxDescIndex;; i++) {
346 if (rxdesc[i].Status & TRDES_OWN_BIT)
347 return (slen - RxOffset);
349 slen += (rxdesc[i].Status >> 16) & 0x03FFF;
355 /*----------------------------------------------------------------------------
357 *----------------------------------------------------------------------------*/
358 int ethernet_read(char *data, int dlen)
361 uint32_t *pSrc = (uint32_t *)rxdesc[RxDescIndex].BufAddr1;
362 copylen = (rxdesc[RxDescIndex].Status >> 16) & 0x03FFF;
363 if (rxdesc[RxDescIndex].Status & TRDES_OWN_BIT || (dlen + RxOffset) > copylen)
366 if ((dlen + RxOffset) == copylen) {
367 memcpy(&pSrc[RxOffset], data, copylen);
368 rxdesc[RxDescIndex].Status = TRDES_OWN_BIT;
371 if (RxDescIndex == NUM_RX_FRAG)
373 } else if ((dlen + RxOffset) < copylen) {
375 memcpy(&pSrc[RxOffset], data, copylen);
381 int ethernet_link(void)
384 if (phy_id == DP83848C_ID) {
385 return (phy_read(PHY_REG_STS) & PHY_STS_LINK);
386 } else { // LAN8720_ID
387 return (phy_read(PHY_REG_BMSR) & PHY_BMSR_LINK);
391 static int phy_write(unsigned int PhyReg, unsigned short Data)
393 unsigned int timeOut;
395 while(LPC_ETHERNET->MAC_MII_ADDR & MIND_BUSY);
396 LPC_ETHERNET->MAC_MII_ADDR = (DP83848C_DEF_ADR<<11) | (PhyReg<<6) | MII_WRITE;
397 LPC_ETHERNET->MAC_MII_DATA = Data;
398 LPC_ETHERNET->MAC_MII_ADDR |= MIND_BUSY; // Start PHY Write Cycle
400 /* Wait utill operation completed */
401 for (timeOut = 0; timeOut < MII_WR_TOUT; timeOut++) {
402 if ((LPC_ETHERNET->MAC_MII_ADDR & MIND_BUSY) == 0) {
410 static int phy_read(unsigned int PhyReg)
412 unsigned int timeOut;
414 while(LPC_ETHERNET->MAC_MII_ADDR & MIND_BUSY);
415 LPC_ETHERNET->MAC_MII_ADDR = (DP83848C_DEF_ADR<<11) | (PhyReg<<6) | MII_READ;
416 LPC_ETHERNET->MAC_MII_ADDR |= MIND_BUSY;
418 for(timeOut = 0; timeOut < MII_RD_TOUT; timeOut++) { /* Wait until operation completed */
419 if((LPC_ETHERNET->MAC_MII_ADDR & MIND_BUSY) == 0) {
420 return LPC_ETHERNET->MAC_MII_DATA; /* Return a 16-bit value. */
427 static void txdscr_init()
431 for(i = 0; i < NUM_TX_FRAG; i++) {
432 txdesc[i].Status = TX_LAST_SEGM | TX_FIRST_SEGM;;
434 txdesc[i].BufAddr1 = (uint32_t)&txbuf[i];
435 if (i == (NUM_RX_FRAG - 1)) {
436 txdesc[i].Status |= TX_END_RING;
440 LPC_ETHERNET->DMA_TRANS_DES_ADDR = (uint32_t)txdesc; /* Set EMAC Transmit Descriptor Registers. */
444 static void rxdscr_init()
448 for(i = 0; i < NUM_RX_FRAG; i++) {
449 rxdesc[i].Status = TRDES_OWN_BIT;
450 rxdesc[i].Ctrl = ETH_FRAG_SIZE;
451 rxdesc[i].BufAddr1 = (uint32_t)&rxbuf[i];
452 if (i == (NUM_RX_FRAG - 1)) {
453 rxdesc[i].Ctrl |= RX_END_RING;
457 LPC_ETHERNET->DMA_REC_DES_ADDR = (uint32_t)rxdesc; /* Set EMAC Receive Descriptor Registers. */
460 void ethernet_address(char *mac)
462 mbed_mac_address(mac);
465 void ethernet_set_link(int speed, int duplex)
467 volatile unsigned short phy_data;
470 if((speed < 0) || (speed > 1)) {
472 phy_data = PHY_AUTO_NEG;
476 phy_data = (((unsigned short) speed << 13) |
477 ((unsigned short) duplex << 8));
480 phy_write(PHY_REG_BMCR, phy_data);
482 for(tout = 100; tout; tout--) {
483 __NOP(); /* A short delay */
489 phy_data = phy_read(PHY_REG_STS);
491 if(phy_data & PHY_STS_DUPLEX) {
492 /* Full duplex is enabled. */
493 LPC_ETHERNET->MAC_CONFIG |= MAC_DUPLEX_MODE;
495 LPC_ETHERNET->MAC_CONFIG &= ~MAC_DUPLEX_MODE;
498 if(phy_data & PHY_STS_SPEED) {
499 LPC_ETHERNET->MAC_CONFIG &= ~SUPP_SPEED;
501 LPC_ETHERNET->MAC_CONFIG |= SUPP_SPEED;
507 for(tout = 100; tout; tout--) {
508 phy_data = phy_read(PHY_REG_BMSR);
509 if (phy_data & PHY_STS_DUPLEX)
513 if (phy_data & PHY_STS_DUPLEX) {
514 /* Full duplex is enabled. */
515 LPC_ETHERNET->MAC_CONFIG |= MAC_DUPLEX_MODE;
517 LPC_ETHERNET->MAC_CONFIG &= ~MAC_DUPLEX_MODE;
520 if(phy_data & PHY_STS_SPEED) {
521 LPC_ETHERNET->MAC_CONFIG &= ~SUPP_SPEED;
523 LPC_ETHERNET->MAC_CONFIG |= SUPP_SPEED;