123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644 |
- /*
- * Copyright (c) 2006-2023, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Email: opensource_embedded@phytium.com.cn
- *
- * Change Logs:
- * Date Author Notes
- * 2022-07-07 liuzhihong first commit
- * 2023-07-14 liuzhihong support RT-Smart
- * 2023-12-01 liuzhihong support initialisation of multiple mac
- */
- #include"rtconfig.h"
- #ifdef BSP_USING_ETH
- #include "board.h"
- #define LOG_TAG "xmac_drv"
- #include "drv_log.h"
- #include "mm_aspace.h"
- #ifdef RT_USING_SMART
- #include "ioremap.h"
- #endif
- #include "eth_board.h"
- #include "drv_xmac.h"
- #define FXMAC_BD_TO_INDEX(ringptr, bdptr) \
- (((uintptr)bdptr - (uintptr)(ringptr)->base_bd_addr) / (ringptr)->separation)
- static void FXmacInitOnError(FXmacOs *instance_p);
- static void FXmacSetupIsr(FXmacOs *instance_p);
- static FXmacOs fxmac_os_instace[FXMAC_NUM] =
- {
- [FXMAC0_ID] =
- {
- .config = (0),
- .hwaddr = {0x98, 0x0e, 0x24, 0x00, 0x11, 0x0},
- },
- [FXMAC1_ID] =
- {
- .config = (0),
- .hwaddr = {0x98, 0x0e, 0x24, 0x00, 0x11, 0x1},
- },
- [FXMAC2_ID] =
- {
- .config = (0),
- .hwaddr = {0x98, 0x0e, 0x24, 0x00, 0x11, 0x2},
- },
- [FXMAC3_ID] =
- {
- .config = (0),
- .hwaddr = {0x98, 0x0e, 0x24, 0x00, 0x11, 0x3},
- },
- };
- int isr_calling_flg = 0;
- /* queue */
- void FXmacQueueInit(PqQueue *q)
- {
- FASSERT(q != NULL);
- q->head = q->tail = q->len = 0;
- }
- int FXmacPqEnqueue(PqQueue *q, void *p)
- {
- if (q->len == PQ_QUEUE_SIZE)
- {
- return -1;
- }
- q->data[q->head] = (uintptr)p;
- q->head = (q->head + 1) % PQ_QUEUE_SIZE;
- q->len++;
- return 0;
- }
- void *FXmacPqDequeue(PqQueue *q)
- {
- int ptail;
- if (q->len == 0)
- {
- return NULL;
- }
- ptail = q->tail;
- q->tail = (q->tail + 1) % PQ_QUEUE_SIZE;
- q->len--;
- return (void *)q->data[ptail];
- }
- int FXmacPqQlength(PqQueue *q)
- {
- return q->len;
- }
- /* dma */
- /**
- * @name: IsTxSpaceAvailable
- * @msg: Get the number of free BDs in the Bdrings
- * @param {ethernetif} *ethernetif_p
- * @return {*}
- */
- static u32 IsTxSpaceAvailable(FXmacOs *instance_p)
- {
- FXmacBdRing *txring;
- u32 freecnt;
- FASSERT(instance_p != NULL);
- txring = &(FXMAC_GET_TXRING(instance_p->instance));
- /* tx space is available as long as there are valid BD's */
- freecnt = FXMAC_BD_RING_GET_FREE_CNT(txring);
- return freecnt;
- }
- /**
- * @name: FXmacProcessSentBds
- * @msg: Free up memory space of pbuf on the send queue
- * @return {*}
- * @param {ethernetif} *ethernetif_p
- * @param {FXmacBdRing} *txring
- */
- void FXmacProcessSentBds(FXmacOs *instance_p, FXmacBdRing *txring)
- {
- FXmacBd *txbdset;
- FXmacBd *curbdpntr;
- u32 n_bds;
- FError status;
- u32 n_pbufs_freed;
- u32 bdindex;
- struct pbuf *p;
- u32 *temp;
- while (1)
- {
- /* obtain processed BD's */
- n_bds = FXmacBdRingFromHwTx(txring, FXMAX_TX_PBUFS_LENGTH, &txbdset);
- if (n_bds == 0)
- {
- return;
- }
- /* free the processed BD's */
- n_pbufs_freed = n_bds;
- curbdpntr = txbdset;
- while (n_pbufs_freed > 0)
- {
- bdindex = FXMAC_BD_TO_INDEX(txring, curbdpntr);
- temp = (u32 *)curbdpntr;
- *temp = 0; /* Word 0 */
- temp++;
- if (bdindex == (FXMAX_TX_PBUFS_LENGTH - 1))
- {
- *temp = 0xC0000000; /* Word 1 ,used/Wrap – marks last descriptor in transmit buffer descriptor list.*/
- }
- else
- {
- *temp = 0x80000000; /* Word 1 , Used – must be zero for GEM to read data to the transmit buffer.*/
- }
- p = (struct pbuf *)instance_p->buffer.tx_pbufs_storage[bdindex];
- if (p != NULL)
- {
- pbuf_free(p);
- }
- instance_p->buffer.tx_pbufs_storage[bdindex] = (uintptr)NULL;
- curbdpntr = FXMAC_BD_RING_NEXT(txring, curbdpntr);
- n_pbufs_freed--;
- }
- status = FXmacBdRingFree(txring, n_bds, txbdset);
- if (status != FT_SUCCESS)
- {
- LOG_I("Failure while freeing in Tx Done ISR.");
- }
- }
- return;
- }
- FError FXmacSgsend(FXmacOs *instance_p, struct pbuf *p)
- {
- struct pbuf *q;
- u32 n_pbufs;
- FXmacBd *txbdset, *txbd, *last_txbd = NULL;
- FXmacBd *temp_txbd;
- FError status;
- FXmacBdRing *txring;
- u32 bdindex;
- uintptr tx_payload ;
- u32 max_fr_size;
- txring = &(FXMAC_GET_TXRING(instance_p->instance));
- /* first count the number of pbufs */
- for (q = p, n_pbufs = 0; q != NULL; q = q->next)
- {
- n_pbufs++;
- }
- /* obtain as many BD's */
- status = FXmacBdRingAlloc(txring, n_pbufs, &txbdset);
- if (status != FT_SUCCESS)
- {
- LOG_I("sgsend: Error allocating TxBD.");
- return ERR_GENERAL;
- }
- for (q = p, txbd = txbdset; q != NULL; q = q->next)
- {
- bdindex = FXMAC_BD_TO_INDEX(txring, txbd);
- if (instance_p->buffer.tx_pbufs_storage[bdindex])
- {
- LOG_I("txbd %p, txring->base_bd_addr %p", txbd, txring->base_bd_addr);
- LOG_I("PBUFS not available bdindex is %d ", bdindex);
- LOG_I("instance_p->buffer.tx_pbufs_storage[bdindex] %p ", instance_p->buffer.tx_pbufs_storage[bdindex]);
- return ERR_GENERAL;
- }
- /* Send the data from the pbuf to the interface, one pbuf at a
- time. The size of the data in each pbuf is kept in the ->len
- variable. */
- tx_payload = (uintptr)q->payload;
- #ifdef RT_USING_SMART
- tx_payload += PV_OFFSET;
- #endif
- FXMAC_BD_SET_ADDRESS_TX(txbd, (uintptr)tx_payload);
- if (instance_p->config & FXMAC_OS_CONFIG_JUMBO)
- {
- max_fr_size = FXMAC_MAX_FRAME_SIZE_JUMBO;
- }
- else
- {
- max_fr_size = FXMAC_MAX_FRAME_SIZE;
- }
- if (q->len > max_fr_size)
- {
- FXMAC_BD_SET_LENGTH(txbd, max_fr_size & 0x3FFF);
- }
- else
- {
- FXMAC_BD_SET_LENGTH(txbd, q->len & 0x3FFF);
- }
- instance_p->buffer.tx_pbufs_storage[bdindex] = (uintptr)q;
- pbuf_ref(q);
- last_txbd = txbd;
- FXMAC_BD_CLEAR_LAST(txbd);
- txbd = FXMAC_BD_RING_NEXT(txring, txbd);
- }
- FXMAC_BD_SET_LAST(last_txbd);
- /* For fragmented packets, remember the 1st BD allocated for the 1st
- packet fragment. The used bit for this BD should be cleared at the end
- after clearing out used bits for other fragments. For packets without
- just remember the allocated BD. */
- temp_txbd = txbdset;
- txbd = txbdset;
- txbd = FXMAC_BD_RING_NEXT(txring, txbd);
- q = p->next;
- for (; q != NULL; q = q->next)
- {
- FXMAC_BD_CLEAR_TX_USED(txbd);
- txbd = FXMAC_BD_RING_NEXT(txring, txbd);
- }
- FXMAC_BD_CLEAR_TX_USED(temp_txbd);
- status = FXmacBdRingToHw(txring, n_pbufs, txbdset);
- if (status != FT_SUCCESS)
- {
- LOG_I("sgsend: Error submitting TxBD.");
- return ERR_GENERAL;
- }
- /* Start transmit */
- FXMAC_WRITEREG32((instance_p->instance).config.base_address,
- FXMAC_NWCTRL_OFFSET,
- (FXMAC_READREG32(instance_p->instance.config.base_address,
- FXMAC_NWCTRL_OFFSET) |
- FXMAC_NWCTRL_STARTTX_MASK));
- return status;
- }
- void SetupRxBds(FXmacOs *instance_p, FXmacBdRing *rxring)
- {
- FXmacBd *rxbd;
- FError status;
- struct pbuf *p;
- u32 freebds;
- u32 bdindex;
- u32 *temp;
- uintptr_t pl_paddr;
- freebds = FXMAC_BD_RING_GET_FREE_CNT(rxring);
- while (freebds > 0)
- {
- freebds--;
- if (instance_p->config & FXMAC_OS_CONFIG_JUMBO)
- {
- p = pbuf_alloc(PBUF_RAW, FXMAC_MAX_FRAME_SIZE_JUMBO, PBUF_RAM);
- }
- else
- {
- p = pbuf_alloc(PBUF_RAW, FXMAC_MAX_FRAME_SIZE, PBUF_RAM);
- }
- if (!p)
- {
- #if LINK_STATS
- lwip_stats.link.memerr++;
- lwip_stats.link.drop++;
- #endif
- LOG_I("Unable to alloc pbuf in recv_handler.");
- return;
- }
- status = FXmacBdRingAlloc(rxring, 1, &rxbd);
- if (status != FT_SUCCESS)
- {
- LOG_I("SetupRxBds: Error allocating RxBD.");
- pbuf_free(p);
- return;
- }
- status = FXmacBdRingToHw(rxring, 1, rxbd);
- if (status != FT_SUCCESS)
- {
- LOG_I("Error committing RxBD to hardware: ");
- if (status == FXMAC_ERR_SG_LIST)
- {
- LOG_I("XST_DMA_SG_LIST_ERROR: this function was called out of sequence with FXmacBdRingAlloc().");
- }
- else
- {
- LOG_I("Set of BDs was rejected because the first BD did not have its start-of-packet bit set, or the last BD did not have its end-of-packet bit set, or any one of the BD set has 0 as length value.");
- }
- pbuf_free(p);
- FXmacBdRingUnAlloc(rxring, 1, rxbd);
- return;
- }
- bdindex = FXMAC_BD_TO_INDEX(rxring, rxbd);
- temp = (u32 *)rxbd;
- if (bdindex == (FXMAX_RX_PBUFS_LENGTH - 1))
- {
- *temp = 0x00000002;
- }
- else
- {
- *temp = 0;
- }
- temp++;
- *temp = 0;
- pl_paddr = (uintptr)p->payload;
- #ifdef RT_USING_SMART
- pl_paddr += PV_OFFSET;
- #endif
- FXMAC_BD_SET_ADDRESS_RX(rxbd, (uintptr)pl_paddr);
- instance_p->buffer.rx_pbufs_storage[bdindex] = (uintptr)p;
- }
- }
- void FXmacRecvSemaphoreHandler(void *arg)
- {
- FXmacOs *instance_p;
- rt_err_t result;
- if (RT_NULL == arg)
- {
- LOG_E("Args is NULL");
- return;
- }
- instance_p = (FXmacOs *)arg;
- result = eth_device_ready(&(instance_p->parent));
- if (result != RT_EOK)
- {
- LOG_I("RxCpltCallback err = %d", result);
- }
- }
- void FXmacRecvHandler(void *arg)
- {
- struct pbuf *p;
- FXmacBd *rxbdset, *curbdptr;
- FXmacBdRing *rxring;
- volatile u32 bd_processed;
- u32 rx_bytes, k;
- u32 bdindex;
- u32 regval;
- FXmacOs *instance_p;
- FASSERT(arg != NULL);
- instance_p = (FXmacOs *)arg;
- rxring = &FXMAC_GET_RXRING(instance_p->instance);
- /* If Reception done interrupt is asserted, call RX call back function
- to handle the processed BDs and then raise the according flag.*/
- regval = FXMAC_READREG32(instance_p->instance.config.base_address, FXMAC_RXSR_OFFSET);
- FXMAC_WRITEREG32(instance_p->instance.config.base_address, FXMAC_RXSR_OFFSET, regval);
- while (1)
- {
- bd_processed = FXmacBdRingFromHwRx(rxring, FXMAX_RX_PBUFS_LENGTH, &rxbdset);
- if (bd_processed <= 0)
- {
- break;
- }
- for (k = 0, curbdptr = rxbdset; k < bd_processed; k++)
- {
- bdindex = FXMAC_BD_TO_INDEX(rxring, curbdptr);
- p = (struct pbuf *)instance_p->buffer.rx_pbufs_storage[bdindex];
- /*
- * Adjust the buffer size to the actual number of bytes received.
- */
- if (instance_p->config & FXMAC_OS_CONFIG_JUMBO)
- {
- rx_bytes = FXMAC_GET_RX_FRAME_SIZE(curbdptr);
- }
- else
- {
- rx_bytes = FXMAC_BD_GET_LENGTH(curbdptr);
- }
- pbuf_realloc(p, rx_bytes);
- /* Invalidate RX frame before queuing to handle
- * L1 cache prefetch conditions on any architecture.
- */
- // FCacheDCacheInvalidateRange((uintptr)p->payload, rx_bytes);
- /* store it in the receive queue,
- * where it'll be processed by a different handler
- */
- if (FXmacPqEnqueue(&instance_p->recv_q, (void *)p) < 0)
- {
- #if LINK_STATS
- lwip_stats.link.memerr++;
- lwip_stats.link.drop++;
- #endif
- pbuf_free(p);
- }
- instance_p->buffer.rx_pbufs_storage[bdindex] = (uintptr)NULL;
- curbdptr = FXMAC_BD_RING_NEXT(rxring, curbdptr);
- }
- /* free up the BD's */
- FXmacBdRingFree(rxring, bd_processed, rxbdset);
- SetupRxBds(instance_p, rxring);
- }
- return;
- }
- void CleanDmaTxdescs(FXmacOs *instance_p)
- {
- FXmacBd bdtemplate;
- FXmacBdRing *txringptr;
- txringptr = &FXMAC_GET_TXRING((instance_p->instance));
- FXMAC_BD_CLEAR(&bdtemplate);
- FXMAC_BD_SET_STATUS(&bdtemplate, FXMAC_TXBUF_USED_MASK);
- FXmacBdRingCreate(txringptr, (uintptr)instance_p->buffer.tx_bdspace,
- (uintptr)instance_p->buffer.tx_bdspace, BD_ALIGNMENT,
- sizeof(instance_p->buffer.tx_bdspace));
- FXmacBdRingClone(txringptr, &bdtemplate, FXMAC_SEND);
- }
- FError FXmacInitDma(FXmacOs *instance_p)
- {
- FXmacBd bdtemplate;
- FXmacBdRing *rxringptr, *txringptr;
- FXmacBd *rxbd;
- struct pbuf *p;
- FError status;
- int i;
- u32 bdindex;
- u32 *temp;
- uintptr rx_paddr;
- uintptr tx_paddr;
- uintptr pl_paddr;
- /*
- * The BDs need to be allocated in uncached memory. Hence the 1 MB
- * address range allocated for Bd_Space is made uncached
- * by setting appropriate attributes in the translation table.
- * The Bd_Space is aligned to 1MB and has a size of 1 MB. This ensures
- * a reserved uncached area used only for BDs.
- */
- rxringptr = &FXMAC_GET_RXRING(instance_p->instance);
- txringptr = &FXMAC_GET_TXRING(instance_p->instance);
- LOG_I("rxringptr: 0x%08x", rxringptr);
- LOG_I("txringptr: 0x%08x", txringptr);
- LOG_I("rx_bdspace: %p ", instance_p->buffer.rx_bdspace);
- LOG_I("tx_bdspace: %p ", instance_p->buffer.tx_bdspace);
- /* Setup RxBD space. */
- FXMAC_BD_CLEAR(&bdtemplate);
- rx_paddr = (uintptr)instance_p->buffer.rx_bdspace;
- #ifdef RT_USING_SMART
- rx_paddr += PV_OFFSET;
- #endif
- /* Create the RxBD ring */
- status = FXmacBdRingCreate(rxringptr, (uintptr)rx_paddr,
- (uintptr)instance_p->buffer.rx_bdspace, BD_ALIGNMENT,
- FXMAX_RX_PBUFS_LENGTH);
- if (status != FT_SUCCESS)
- {
- LOG_I("Error setting up RxBD space.");
- return ERR_IF;
- }
- status = FXmacBdRingClone(rxringptr, &bdtemplate, FXMAC_RECV);
- if (status != FT_SUCCESS)
- {
- LOG_I("Error initializing RxBD space.");
- return ERR_IF;
- }
- FXMAC_BD_CLEAR(&bdtemplate);
- FXMAC_BD_SET_STATUS(&bdtemplate, FXMAC_TXBUF_USED_MASK);
- tx_paddr = (uintptr)instance_p->buffer.tx_bdspace;
- #ifdef RT_USING_SMART
- tx_paddr += PV_OFFSET;
- #endif
- /* Create the TxBD ring */
- status = FXmacBdRingCreate(txringptr, (uintptr)tx_paddr,
- (uintptr)instance_p->buffer.tx_bdspace, BD_ALIGNMENT,
- FXMAX_TX_PBUFS_LENGTH);
- if (status != FT_SUCCESS)
- {
- return ERR_IF;
- }
- /* We reuse the bd template, as the same one will work for both rx and tx. */
- status = FXmacBdRingClone(txringptr, &bdtemplate, FXMAC_SEND);
- if (status != FT_SUCCESS)
- {
- return ERR_IF;
- }
- /*
- * Allocate RX descriptors, 1 RxBD at a time.
- */
- for (i = 0; i < FXMAX_RX_PBUFS_LENGTH; i++)
- {
- if (instance_p->config & FXMAC_OS_CONFIG_JUMBO)
- {
- p = pbuf_alloc(PBUF_RAW, FXMAC_MAX_FRAME_SIZE_JUMBO, PBUF_RAM);
- }
- else
- {
- p = pbuf_alloc(PBUF_RAW, FXMAC_MAX_FRAME_SIZE, PBUF_RAM);
- }
- if (!p)
- {
- #if LINK_STATS
- lwip_stats.link.memerr++;
- lwip_stats.link.drop++;
- #endif
- LOG_E("Unable to alloc pbuf in InitDma.");
- return ERR_IF;
- }
- status = FXmacBdRingAlloc(rxringptr, 1, &rxbd);
- if (status != FT_SUCCESS)
- {
- LOG_E("InitDma: Error allocating RxBD.");
- pbuf_free(p);
- return ERR_IF;
- }
- /* Enqueue to HW */
- status = FXmacBdRingToHw(rxringptr, 1, rxbd);
- if (status != FT_SUCCESS)
- {
- LOG_E("Error: committing RxBD to HW.");
- pbuf_free(p);
- FXmacBdRingUnAlloc(rxringptr, 1, rxbd);
- return ERR_IF;
- }
- bdindex = FXMAC_BD_TO_INDEX(rxringptr, rxbd);
- temp = (u32 *)rxbd;
- *temp = 0;
- if (bdindex == (FXMAX_RX_PBUFS_LENGTH - 1))
- {
- *temp = 0x00000002;
- }
- temp++;
- *temp = 0;
- pl_paddr = (uintptr)p->payload;
- #ifdef RT_USING_SMART
- pl_paddr += PV_OFFSET;
- #endif
- FXMAC_BD_SET_ADDRESS_RX(rxbd, (uintptr)pl_paddr);
- instance_p->buffer.rx_pbufs_storage[bdindex] = (uintptr)p;
- }
- FXmacSetQueuePtr(&(instance_p->instance), instance_p->instance.tx_bd_queue.bdring.phys_base_addr, 0, (u16)FXMAC_SEND);
- FXmacSetQueuePtr(&(instance_p->instance), instance_p->instance.rx_bd_queue.bdring.phys_base_addr, 0, (u16)FXMAC_RECV);
- return 0;
- }
- static void FreeOnlyTxPbufs(FXmacOs *instance_p)
- {
- u32 index;
- struct pbuf *p;
- for (index = 0; index < (FXMAX_TX_PBUFS_LENGTH); index++)
- {
- if (instance_p->buffer.tx_pbufs_storage[index] != 0)
- {
- p = (struct pbuf *)instance_p->buffer.tx_pbufs_storage[index];
- pbuf_free(p);
- instance_p->buffer.tx_pbufs_storage[index] = (uintptr)NULL;
- }
- instance_p->buffer.tx_pbufs_storage[index] = (uintptr)0;
- }
- }
- static void FreeOnlyRxPbufs(FXmacOs *instance_p)
- {
- u32 index;
- struct pbuf *p;
- for (index = 0; index < (FXMAX_RX_PBUFS_LENGTH); index++)
- {
- if (instance_p->buffer.rx_pbufs_storage[index] != 0)
- {
- p = (struct pbuf *)instance_p->buffer.rx_pbufs_storage[index];
- pbuf_free(p);
- instance_p->buffer.rx_pbufs_storage[index] = (uintptr)0;
- }
- }
- }
- static void FreeTxRxPbufs(FXmacOs *instance_p)
- {
- u32 rx_queue_len;
- struct pbuf *p;
- /* first :free PqQueue data */
- rx_queue_len = FXmacPqQlength(&instance_p->recv_q);
- while (rx_queue_len)
- {
- /* return one packet from receive q */
- p = (struct pbuf *)FXmacPqDequeue(&instance_p->recv_q);
- pbuf_free(p);
- LOG_E("Delete queue %p", p);
- rx_queue_len--;
- }
- FreeOnlyTxPbufs(instance_p);
- FreeOnlyRxPbufs(instance_p);
- }
- /* interrupt */
- static void FXmacHandleDmaTxError(FXmacOs *instance_p)
- {
- s32_t status = FT_SUCCESS;
- u32 dmacrreg;
- FreeTxRxPbufs(instance_p);
- status = FXmacCfgInitialize(&instance_p->instance, &instance_p->instance.config);
- if (status != FT_SUCCESS)
- {
- LOG_E("In %s:EmacPs Configuration Failed....", __func__);
- }
- /* initialize the mac */
- FXmacInitOnError(instance_p); /* need to set mac filter address */
- dmacrreg = FXMAC_READREG32(instance_p->instance.config.base_address, FXMAC_DMACR_OFFSET);
- dmacrreg = dmacrreg | (FXMAC_DMACR_ORCE_DISCARD_ON_ERR_MASK); /* force_discard_on_err */
- FXMAC_WRITEREG32(instance_p->instance.config.base_address, FXMAC_DMACR_OFFSET, dmacrreg);
- FXmacSetupIsr(instance_p);
- FXmacInitDma(instance_p);
- FXmacStart(&instance_p->instance);
- }
- void FXmacHandleTxErrors(FXmacOs *instance_p)
- {
- u32 netctrlreg;
- netctrlreg = FXMAC_READREG32(instance_p->instance.config.base_address,
- FXMAC_NWCTRL_OFFSET);
- netctrlreg = netctrlreg & (~FXMAC_NWCTRL_TXEN_MASK);
- FXMAC_WRITEREG32(instance_p->instance.config.base_address,
- FXMAC_NWCTRL_OFFSET, netctrlreg);
- FreeOnlyTxPbufs(instance_p);
- CleanDmaTxdescs(instance_p);
- netctrlreg = FXMAC_READREG32(instance_p->instance.config.base_address, FXMAC_NWCTRL_OFFSET);
- netctrlreg = netctrlreg | (FXMAC_NWCTRL_TXEN_MASK);
- FXMAC_WRITEREG32(instance_p->instance.config.base_address, FXMAC_NWCTRL_OFFSET, netctrlreg);
- }
- void FXmacErrorHandler(void *arg, u8 direction, u32 error_word)
- {
- FXmacBdRing *rxring;
- FXmacBdRing *txring;
- FXmacOs *instance_p;
- instance_p = (FXmacOs *)arg;
- rxring = &FXMAC_GET_RXRING(instance_p->instance);
- txring = &FXMAC_GET_TXRING(instance_p->instance);
- if (error_word != 0)
- {
- switch (direction)
- {
- case FXMAC_RECV:
- if (error_word & FXMAC_RXSR_HRESPNOK_MASK)
- {
- LOG_I("Receive DMA error.");
- FXmacHandleDmaTxError(instance_p);
- }
- if (error_word & FXMAC_RXSR_RXOVR_MASK)
- {
- LOG_I("Receive over run.");
- FXmacRecvHandler(instance_p);
- SetupRxBds(instance_p, rxring);
- }
- if (error_word & FXMAC_RXSR_BUFFNA_MASK)
- {
- LOG_I("Receive buffer not available.");
- FXmacRecvHandler(arg);
- SetupRxBds(instance_p, rxring);
- }
- break;
- case FXMAC_SEND:
- if (error_word & FXMAC_TXSR_HRESPNOK_MASK)
- {
- LOG_I("Transmit DMA error.");
- FXmacHandleDmaTxError(instance_p);
- }
- if (error_word & FXMAC_TXSR_URUN_MASK)
- {
- LOG_I("Transmit under run.");
- FXmacHandleTxErrors(instance_p);
- }
- if (error_word & FXMAC_TXSR_BUFEXH_MASK)
- {
- LOG_I("Transmit buffer exhausted.");
- FXmacHandleTxErrors(instance_p);
- }
- if (error_word & FXMAC_TXSR_RXOVR_MASK)
- {
- LOG_I("Transmit retry excessed limits.");
- FXmacHandleTxErrors(instance_p);
- }
- if (error_word & FXMAC_TXSR_FRAMERX_MASK)
- {
- LOG_I("Transmit collision.");
- FXmacProcessSentBds(instance_p, txring);
- }
- break;
- }
- }
- }
- void FXmacLinkChange(void *arg)
- {
- u32 ctrl;
- u32 link, link_status;
- FXmac *xmac_p;
- FXmacOs *instance_p;
- instance_p = (FXmacOs *)arg;
- xmac_p = &instance_p->instance;
- if (xmac_p->config.interface == FXMAC_PHY_INTERFACE_MODE_SGMII)
- {
- LOG_I("xmac_p->config.base_address is %p", xmac_p->config.base_address);
- ctrl = FXMAC_READREG32(xmac_p->config.base_address, FXMAC_PCS_AN_LP_OFFSET);
- link = (ctrl & FXMAC_PCS_LINK_PARTNER_NEXT_PAGE_STATUS) >> 15;
- switch (link)
- {
- case 0:
- LOG_I("Link status is down");
- link_status = FXMAC_LINKDOWN;
- break;
- case 1:
- LOG_I("Link status is up");
- link_status = FXMAC_LINKUP;
- break;
- default:
- LOG_E("Link status is error 0x%x ", link);
- return;
- }
- if (link_status == FXMAC_LINKUP)
- {
- if (link_status != xmac_p->link_status)
- {
- xmac_p->link_status = FXMAC_NEGOTIATING;
- LOG_I("Need NEGOTIATING.");
- }
- }
- else
- {
- xmac_p->link_status = FXMAC_LINKDOWN;
- }
- }
- }
- /* phy */
- /**
- * @name: FXmacPhyLinkDetect
- * @msg: Get current link status
- * @note:
- * @param {FXmac} *fxmac_p
- * @param {u32} phy_addr
- * @return {*} 1 is link up , 0 is link down
- */
- static u32 FXmacPhyLinkDetect(FXmac *xmac_p, u32 phy_addr)
- {
- u16 status;
- /* Read Phy Status register twice to get the confirmation of the current
- * link status.
- */
- FXmacPhyRead(xmac_p, phy_addr, PHY_STATUS_REG_OFFSET, &status);
- if (status & PHY_STAT_LINK_STATUS)
- {
- return 1;
- }
- return 0;
- }
- static u32 FXmacPhyAutonegStatus(FXmac *xmac_p, u32 phy_addr)
- {
- u16 status;
- /* Read Phy Status register twice to get the confirmation of the current
- * link status.
- */
- FXmacPhyRead(xmac_p, phy_addr, PHY_STATUS_REG_OFFSET, &status);
- if (status & PHY_STATUS_AUTONEGOTIATE_COMPLETE)
- {
- return 1;
- }
- return 0;
- }
- enum lwip_port_link_status FXmacLwipPortLinkDetect(FXmacOs *instance_p)
- {
- u32 phy_link_status;
- FXmac *xmac_p = &instance_p->instance;
- if (xmac_p->is_ready != (u32)FT_COMPONENT_IS_READY)
- {
- return ETH_LINK_UNDEFINED;
- }
- phy_link_status = FXmacPhyLinkDetect(xmac_p, xmac_p->phy_address);
- if ((xmac_p->link_status == FXMAC_LINKUP) && (!phy_link_status))
- {
- xmac_p->link_status = FXMAC_LINKDOWN;
- }
- switch (xmac_p->link_status)
- {
- case FXMAC_LINKUP:
- return ETH_LINK_UP;
- case FXMAC_LINKDOWN:
- xmac_p->link_status = FXMAC_NEGOTIATING;
- LOG_D("Ethernet Link down.");
- return ETH_LINK_DOWN;
- case FXMAC_NEGOTIATING:
- if ((phy_link_status == FXMAC_LINKUP) && FXmacPhyAutonegStatus(xmac_p, xmac_p->phy_address))
- {
- err_t phy_ret;
- phy_ret = FXmacPhyInit(xmac_p, xmac_p->config.speed, xmac_p->config.duplex, xmac_p->config.auto_neg, XMAC_PHY_RESET_DISABLE);
- if (phy_ret != FT_SUCCESS)
- {
- LOG_E("FXmacPhyInit is error.");
- return ETH_LINK_DOWN;
- }
- FXmacSelectClk(xmac_p);
- FXmacInitInterface(xmac_p);
- /* Initiate Phy setup to get link speed */
- xmac_p->link_status = FXMAC_LINKUP;
- LOG_D("Ethernet Link up.");
- return ETH_LINK_UP;
- }
- return ETH_LINK_DOWN;
- default:
- return ETH_LINK_DOWN;
- }
- }
- enum lwip_port_link_status FXmacPhyReconnect(FXmacOs *instance_p)
- {
- FXmac *xmac_p;
- xmac_p = &instance_p->instance;
- if (xmac_p->config.interface == FXMAC_PHY_INTERFACE_MODE_SGMII)
- {
- rt_hw_interrupt_mask(xmac_p->config.queue_irq_num[0]);
- if (xmac_p->link_status == FXMAC_NEGOTIATING)
- {
- /* auto negotiation again*/
- err_t phy_ret;
- phy_ret = FXmacPhyInit(xmac_p, xmac_p->config.speed, xmac_p->config.duplex, xmac_p->config.auto_neg, XMAC_PHY_RESET_DISABLE);
- if (phy_ret != FT_SUCCESS)
- {
- LOG_I("FXmacPhyInit is error.");
- rt_hw_interrupt_umask(xmac_p->config.queue_irq_num[0]);
- return ETH_LINK_DOWN;
- }
- FXmacSelectClk(xmac_p);
- FXmacInitInterface(xmac_p);
- xmac_p->link_status = FXMAC_LINKUP;
- }
- rt_hw_interrupt_umask(xmac_p->config.queue_irq_num[0]);
- switch (xmac_p->link_status)
- {
- case FXMAC_LINKDOWN:
- return ETH_LINK_DOWN;
- case FXMAC_LINKUP:
- return ETH_LINK_UP;
- default:
- return ETH_LINK_DOWN;
- }
- }
- else if ((xmac_p->config.interface == FXMAC_PHY_INTERFACE_MODE_RMII) || (xmac_p->config.interface == FXMAC_PHY_INTERFACE_MODE_RGMII))
- {
- return FXmacLwipPortLinkDetect(instance_p);
- }
- else
- {
- switch (xmac_p->link_status)
- {
- case FXMAC_LINKDOWN:
- return ETH_LINK_DOWN;
- case FXMAC_LINKUP:
- return ETH_LINK_UP;
- default:
- return ETH_LINK_DOWN;
- }
- }
- }
- static void FxmacOsIntrHandler(s32 vector, void *args)
- {
- isr_calling_flg++;
- FXmacIntrHandler(vector, args);
- isr_calling_flg--;
- }
- static void FXmacSetupIsr(FXmacOs *instance_p)
- {
- u32 cpu_id;
- GetCpuId(&cpu_id);
- /* Setup callbacks */
- FXmacSetHandler(&instance_p->instance, FXMAC_HANDLER_DMARECV, FXmacRecvSemaphoreHandler, instance_p);
- FXmacSetHandler(&instance_p->instance, FXMAC_HANDLER_ERROR, FXmacErrorHandler, instance_p);
- FXmacSetHandler(&instance_p->instance, FXMAC_HANDLER_LINKCHANGE, FXmacLinkChange, instance_p);
- rt_hw_interrupt_install(instance_p->instance.config.queue_irq_num[0], FxmacOsIntrHandler, &instance_p->instance, "fxmac");
- rt_hw_interrupt_umask(instance_p->instance.config.queue_irq_num[0]);
- }
- /* init fxmac instance */
- static void FXmacInitOnError(FXmacOs *instance_p)
- {
- FXmac *xmac_p;
- u32 status = FT_SUCCESS;
- xmac_p = &instance_p->instance;
- /* set mac address */
- status = FXmacSetMacAddress(xmac_p, (void *)(instance_p->hwaddr), 1);
- if (status != FT_SUCCESS)
- {
- LOG_E("In %s:Emac Mac Address set failed...", __func__);
- }
- }
- /* step 1: initialize instance */
- /* step 2: depend on config set some options : JUMBO / IGMP */
- /* step 3: FXmacSelectClk */
- /* step 4: FXmacInitInterface */
- /* step 5: initialize phy */
- /* step 6: initialize dma */
- /* step 7: initialize interrupt */
- /* step 8: start mac */
- FError FXmacOsInit(FXmacOs *instance_p)
- {
- FXmacConfig mac_config;
- FXmacConfig *mac_config_p;
- FXmacPhyInterface interface = FXMAC_PHY_INTERFACE_MODE_SGMII;
- FXmac *xmac_p;
- u32 dmacrreg;
- FError status;
- FASSERT(instance_p != NULL);
- FASSERT(instance_p->mac_config.instance_id < FXMAC_NUM);
- xmac_p = &instance_p->instance;
- LOG_I("instance_id IS %d", instance_p->mac_config.instance_id);
- mac_config_p = FXmacLookupConfig(instance_p->mac_config.instance_id);
- if (mac_config_p == NULL)
- {
- LOG_E("FXmacLookupConfig is error , instance_id is %d", instance_p->mac_config.instance_id);
- return FREERTOS_XMAC_INIT_ERROR;
- }
- #ifdef RT_USING_SMART
- mac_config_p->base_address = (uintptr)rt_ioremap((void *)mac_config_p->base_address, 0x2000);
- #endif
- mac_config = *mac_config_p;
- switch (instance_p->mac_config.interface)
- {
- case FXMAC_OS_INTERFACE_SGMII:
- interface = FXMAC_PHY_INTERFACE_MODE_SGMII;
- LOG_I("SGMII select.");
- break;
- case FXMAC_OS_INTERFACE_RMII:
- interface = FXMAC_PHY_INTERFACE_MODE_RMII;
- LOG_I("RMII select.");
- break;
- case FXMAC_OS_INTERFACE_RGMII:
- LOG_I("RGMII select.");
- interface = FXMAC_PHY_INTERFACE_MODE_RGMII;
- break;
- default:
- LOG_E("Update interface is error , interface is %d", instance_p->mac_config.instance_id);
- return FREERTOS_XMAC_INIT_ERROR;
- }
- mac_config.interface = interface;
- if (instance_p->mac_config.autonegotiation)
- {
- mac_config.auto_neg = 1;
- }
- else
- {
- mac_config.auto_neg = 0;
- }
- switch (instance_p->mac_config.phy_speed)
- {
- case FXMAC_PHY_SPEED_10M:
- mac_config.speed = FXMAC_SPEED_10;
- break;
- case FXMAC_PHY_SPEED_100M:
- mac_config.speed = FXMAC_SPEED_100;
- break;
- case FXMAC_PHY_SPEED_1000M:
- mac_config.speed = FXMAC_SPEED_1000;
- break;
- default:
- LOG_E("Setting speed is not valid , speed is %d", instance_p->mac_config.phy_speed);
- return FREERTOS_XMAC_INIT_ERROR;
- }
- switch (instance_p->mac_config.phy_duplex)
- {
- case FXMAC_PHY_HALF_DUPLEX:
- mac_config.duplex = 0;
- break;
- case FXMAC_PHY_FULL_DUPLEX:
- mac_config.duplex = 1;
- break;
- }
- status = FXmacCfgInitialize(xmac_p, &mac_config);
- if (status != FT_SUCCESS)
- {
- LOG_E("In %s:EmacPs Configuration Failed....", __func__);
- }
- if (instance_p->config & FXMAC_OS_CONFIG_JUMBO)
- {
- FXmacSetOptions(xmac_p, FXMAC_JUMBO_ENABLE_OPTION, 0);
- }
- if (instance_p->config & FXMAC_OS_CONFIG_MULTICAST_ADDRESS_FILITER)
- {
- FXmacSetOptions(xmac_p, FXMAC_MULTICAST_OPTION, 0);
- }
- /* enable copy all frames */
- if (instance_p->config & FXMAC_OS_CONFIG_COPY_ALL_FRAMES)
- {
- FXmacSetOptions(xmac_p, FXMAC_PROMISC_OPTION, 0);
- }
- status = FXmacSetMacAddress(xmac_p, (void *)(instance_p->hwaddr), 0);
- if (status != FT_SUCCESS)
- {
- LOG_E("In %s:Emac Mac Address set failed...", __func__);
- }
- /* close fcs check */
- if (instance_p->config & FXMAC_OS_CONFIG_CLOSE_FCS_CHECK)
- {
- FXmacSetOptions(xmac_p, FXMAC_FCS_STRIP_OPTION, 0);
- }
- /* initialize phy */
- status = FXmacPhyInit(xmac_p, xmac_p->config.speed, xmac_p->config.duplex, xmac_p->config.auto_neg, XMAC_PHY_RESET_ENABLE);
- if (status != FT_SUCCESS)
- {
- LOG_W("FXmacPhyInit is error.");
- }
- FXmacSelectClk(xmac_p);
- FXmacInitInterface(xmac_p);
- /* initialize dma */
- dmacrreg = FXMAC_READREG32(xmac_p->config.base_address, FXMAC_DMACR_OFFSET);
- dmacrreg &= (~(FXMAC_DMACR_BLENGTH_MASK));
- dmacrreg |= (FXMAC_DMACR_INCR16_AHB_AXI_BURST); /* Attempt to use bursts of up to 16. */
- FXMAC_WRITEREG32(xmac_p->config.base_address, FXMAC_DMACR_OFFSET, dmacrreg);
- FXmacInitDma(instance_p);
- /* initialize interrupt */
- FXmacSetupIsr(instance_p);
- return FT_SUCCESS;
- }
- /**
- * @name: FXmacOsRx
- * @msg: struct pbuf *FXmacOsRx(FXmacOs *instance_p)
- * @return {*}
- * @note:
- * @param {FXmacOs} *instance_p
- */
- struct pbuf *FXmacOsRx(FXmacOs *instance_p)
- {
- FASSERT(instance_p != NULL);
- struct pbuf *p;
- /* see if there is data to process */
- if (FXmacPqQlength(&instance_p->recv_q) == 0)
- {
- return NULL;
- }
- /* return one packet from receive q */
- p = (struct pbuf *)FXmacPqDequeue(&instance_p->recv_q);
- return p;
- }
- static FError FXmacOsOutput(FXmacOs *instance_p, struct pbuf *p)
- {
- FError status;
- status = FXmacSgsend(instance_p, p);
- if (status != FT_SUCCESS)
- {
- #if LINK_STATS
- lwip_stats.link.drop++;
- #endif
- }
- #if LINK_STATS
- lwip_stats.link.xmit++;
- #endif /* LINK_STATS */
- return status;
- }
- FError FXmacOsTx(FXmacOs *instance_p, void *tx_buf)
- {
- FXmacBdRing *txring;
- FError ret;
- u32 n_pbufs;
- struct pbuf *p;
- struct pbuf *q;
- FASSERT(instance_p != NULL);
- if (tx_buf == NULL)
- {
- LOG_E("tx_buf is null.");
- return FREERTOS_XMAC_PARAM_ERROR;
- }
- p = tx_buf;
- txring = &(FXMAC_GET_TXRING(instance_p->instance));
- for (q = p, n_pbufs = 0; q != NULL; q = q->next)
- {
- n_pbufs++;
- }
- /* check if space is available to send */
- if (txring->free_cnt < n_pbufs)
- {
- FXmacProcessSentBds(instance_p, txring);
- }
- if (IsTxSpaceAvailable(instance_p))
- {
- FXmacOsOutput(instance_p, p);
- ret = FT_SUCCESS;
- }
- else
- {
- #if LINK_STATS
- lwip_stats.link.drop++;
- #endif
- LOG_E("Pack dropped, no space.");
- ret = FREERTOS_XMAC_NO_VALID_SPACE;
- }
- return ret;
- }
- FXmacOs *FXmacOsGetInstancePointer(FXmacOsControl *config_p)
- {
- FXmacOs *instance_p;
- FASSERT(config_p != NULL);
- FASSERT(config_p->instance_id < FXMAC_NUM);
- FASSERT_MSG(config_p->interface < FXMAC_OS_INTERFACE_LENGTH, "config_p->interface %d is over %d", config_p->interface, FXMAC_OS_INTERFACE_LENGTH);
- FASSERT_MSG(config_p->autonegotiation <= 1, "config_p->autonegotiation %d is over 1", config_p->autonegotiation);
- FASSERT_MSG(config_p->phy_speed <= FXMAC_PHY_SPEED_1000M, "config_p->phy_speed %d is over 1000", config_p->phy_speed);
- FASSERT_MSG(config_p->phy_duplex <= FXMAC_PHY_FULL_DUPLEX, "config_p->phy_duplex %d is over FXMAC_PHY_FULL_DUPLEX", config_p->phy_duplex);
- instance_p = &fxmac_os_instace[config_p->instance_id];
- memcpy(&instance_p->mac_config, config_p, sizeof(FXmacOsControl));
- return instance_p;
- }
- void FXmacOsStart(FXmacOs *instance_p)
- {
- FASSERT(instance_p != NULL);
- /* start mac */
- instance_p->instance.mask &= (~FXMAC_IXR_TXCOMPL_MASK);
- FXmacStart(&instance_p->instance);
- }
- static rt_err_t rt_xmac_init(rt_device_t dev)
- {
- struct eth_device *pXmacParent;
- FXmacOs *pOsMac;
- FError ret;
- pXmacParent = rt_container_of(dev, struct eth_device, parent);
- if (NULL == pXmacParent)
- {
- return -RT_ENOMEM;
- }
- pOsMac = rt_container_of(pXmacParent, FXmacOs, parent);
- if (NULL == pOsMac)
- {
- return -RT_ENOMEM;
- }
- ret = FXmacOsInit(pOsMac);
- if (ret != FT_SUCCESS)
- {
- LOG_E("FXmacOsInit is error\r\n");
- return -RT_ERROR;
- }
- rt_kprintf("FXMAC OS Init Success!\n");
- return RT_EOK;
- }
- static rt_err_t rt_xmac_open(rt_device_t dev, rt_uint16_t oflag)
- {
- LOG_D("xmac open");
- return RT_EOK;
- }
- static rt_err_t rt_xmac_close(rt_device_t dev)
- {
- LOG_D("xmac close");
- return RT_EOK;
- }
- static rt_ssize_t rt_xmac_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
- {
- LOG_D("xmac read");
- rt_set_errno(-RT_ENOSYS);
- return 0;
- }
- static rt_ssize_t rt_xmac_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
- {
- LOG_D("xmac write");
- rt_set_errno(-RT_ENOSYS);
- return 0;
- }
- static rt_err_t rt_xmac_control(rt_device_t dev, int cmd, void *args)
- {
- FXmacOs *pOsMac;
- struct eth_device *pXmacParent;
- pXmacParent = rt_container_of(dev, struct eth_device, parent);
- if (NULL == pXmacParent)
- {
- return -RT_ENOMEM;
- }
- pOsMac = rt_container_of(pXmacParent, FXmacOs, parent);
- if (NULL == pOsMac)
- {
- return -RT_ENOMEM;
- }
- switch (cmd)
- {
- case NIOCTL_GADDR:
- /* get mac address */
- if (args)
- {
- rt_memcpy(args, pOsMac->hwaddr, 6);
- }
- else
- {
- return -RT_ERROR;
- }
- break;
- default:
- break;
- }
- return RT_EOK;
- }
- rt_err_t rt_xmac_tx(rt_device_t dev, struct pbuf *p)
- {
- FXmacOs *pOsMac;
- struct eth_device *pXmacParent;
- FError ret;
- pXmacParent = rt_container_of(dev, struct eth_device, parent);
- if (NULL == pXmacParent)
- {
- return -RT_ENOMEM;
- }
- pOsMac = rt_container_of(pXmacParent, FXmacOs, parent);
- if (NULL == pOsMac)
- {
- return -RT_ENOMEM;
- }
- #if RT_LWIP_ETH_PAD_SIZE
- pbuf_header(p, -RT_LWIP_ETH_PAD_SIZE); /* reclaim the padding word */
- #endif
- ret = FXmacOsTx(pOsMac, p);
- #if RT_LWIP_ETH_PAD_SIZE
- pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
- #endif
- if (ret != FT_SUCCESS)
- {
- return ERR_MEM;
- }
- return RT_EOK;
- }
- struct pbuf *rt_xmac_rx(rt_device_t dev)
- {
- FXmacOs *pOsMac;
- struct eth_device *pXmacParent;
- struct pbuf *p;
- rt_base_t level;
- pXmacParent = rt_container_of(dev, struct eth_device, parent);
- if (NULL == pXmacParent)
- {
- return RT_NULL;
- }
- pOsMac = rt_container_of(pXmacParent, FXmacOs, parent);
- if (NULL == pOsMac)
- {
- return RT_NULL;
- }
- level = rt_hw_interrupt_disable();
- FXmacRecvHandler(pOsMac);
- p = FXmacOsRx(pOsMac);
- rt_hw_interrupt_enable(level);
- return p;
- }
- enum lwip_port_link_status eth_link_detect(FXmacOs *instance_p)
- {
- if (instance_p->instance.is_ready != FT_COMPONENT_IS_READY)
- {
- return ETH_LINK_UNDEFINED;
- }
- return FXmacPhyReconnect(instance_p);
- }
- static void ethernet_link_thread(void *Args)
- {
- if (RT_NULL == Args)
- {
- return;
- }
- FXmacOs *pOsMac;
- pOsMac = (FXmacOs *)Args;
- while (1)
- {
- /* Call eth_link_detect() every 500ms to detect Ethernet link
- * change.
- */
- switch (eth_link_detect(pOsMac))
- {
- case ETH_LINK_UP:
- if (pOsMac->is_link_up == 0)
- {
- LOG_I("netif flag is link_up\n");
- pOsMac->is_link_up = 1;
- eth_device_linkchange(&pOsMac->parent, RT_TRUE);
- }
- break;
- case ETH_LINK_DOWN:
- default:
- if (pOsMac->is_link_up == 1)
- {
- LOG_I("netif flag is link_down\n");
- pOsMac->is_link_up = 0;
- eth_device_linkchange(&pOsMac->parent, RT_FALSE);
- }
- break;
- }
- rt_thread_mdelay(500);
- }
- }
- #ifdef RT_USING_DEVICE_OPS
- const struct rt_device_ops net_ops=
- {
- rt_xmac_init,
- rt_xmac_open,
- rt_xmac_close,
- rt_xmac_read,
- rt_xmac_write,
- rt_xmac_control
- };
- #endif
- static int rt_hw_xmac_init(FXmacOs *pOsMac, const char *name, const char *link_thread_name)
- {
- rt_err_t state = RT_EOK;
- #ifdef RT_USING_DEVICE_OPS
- pOsMac->parent.parent.ops= &net_ops;
- #else
- pOsMac->parent.parent.init = rt_xmac_init;
- pOsMac->parent.parent.open = rt_xmac_open;
- pOsMac->parent.parent.close = rt_xmac_close;
- pOsMac->parent.parent.read = rt_xmac_read;
- pOsMac->parent.parent.write = rt_xmac_write;
- pOsMac->parent.parent.control = rt_xmac_control;
- #endif
- pOsMac->parent.parent.user_data = RT_NULL;
- pOsMac->parent.eth_rx = rt_xmac_rx;
- pOsMac->parent.eth_tx = rt_xmac_tx;
- /* register eth device */
- state = eth_device_init(&(pOsMac->parent), name);
- if (RT_EOK != state)
- {
- LOG_E("xmac device init faild: %d", state);
- return -RT_ERROR;
- }
- rt_kprintf("Xmac %s Initiailized!\n", name);
- state = rt_thread_init(&pOsMac->_link_thread,
- link_thread_name,
- ethernet_link_thread,
- pOsMac,
- &pOsMac->_link_thread_stack[0],
- sizeof(pOsMac->_link_thread_stack),
- 12, 2);
- if (RT_EOK == state)
- {
- rt_thread_startup(&pOsMac->_link_thread);
- }
- else
- {
- LOG_E("rt_thread_init is error");
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- static int rt_hw_xmac_eth_init(void)
- {
- rt_err_t state = RT_EOK;
- FXmacOsControl os_config;
- FXmacOs *pOsMac;
- #if defined(MAC_NUM0)
- /* os_config initialize,need to be set manually here */
- os_config.instance_id = MAC_NUM0_CONTROLLER;
- os_config.interface = MAC_NUM0_MII_INTERFACE;
- os_config.autonegotiation = 1; /* 1 is autonegotiation ,0 is manually set */
- os_config.phy_speed = FXMAC_PHY_SPEED_1000M; /* FXMAC_PHY_SPEED_XXX */
- os_config.phy_duplex = FXMAC_PHY_FULL_DUPLEX; /* FXMAC_PHY_XXX_DUPLEX */
- pOsMac = FXmacOsGetInstancePointer(&os_config);
- if (pOsMac == NULL)
- {
- LOG_E("FXmacOsGetInstancePointer is error\r\n");
- return -RT_ERROR;
- }
- const char *os_drv_xmac0_name = "e0";
- const char *e0_thread_name = "e0_link_detect";
- state = rt_hw_xmac_init(pOsMac, os_drv_xmac0_name, e0_thread_name);
- extern void set_if(const char *netif_name, const char *ip_addr, const char *gw_addr, const char *nm_addr);
- rt_kprintf("Set netif %s ip addr!\n", os_drv_xmac0_name);
- set_if(os_drv_xmac0_name, "192.168.4.10", "192.168.4.1", "255.255.255.0");
- if (RT_EOK != state)
- {
- goto __exit;
- }
- #endif
- #if defined(MAC_NUM1)
- os_config.instance_id = MAC_NUM1_CONTROLLER;
- os_config.interface = MAC_NUM1_MII_INTERFACE;
- os_config.autonegotiation = 1; /* 1 is autonegotiation ,0 is manually set */
- os_config.phy_speed = FXMAC_PHY_SPEED_1000M; /* FXMAC_PHY_SPEED_XXX */
- os_config.phy_duplex = FXMAC_PHY_FULL_DUPLEX; /* FXMAC_PHY_XXX_DUPLEX */
- pOsMac = FXmacOsGetInstancePointer(&os_config);
- if (pOsMac == NULL)
- {
- LOG_E("FXmacOsGetInstancePointer is error\r\n");
- return -RT_ERROR;
- }
- const char *os_drv_xmac1_name = "e1";
- const char *e1_thread_name = "e1_link_detect";
- state = rt_hw_xmac_init(pOsMac, os_drv_xmac1_name, e1_thread_name);
- rt_kprintf("Set Xmac %s ip addr!\n", os_drv_xmac1_name);
- set_if(os_drv_xmac1_name, "192.168.4.11", "192.168.4.1", "255.255.255.0");
- if (RT_EOK != state)
- {
- goto __exit;
- }
- #endif
- #if defined(MAC_NUM0)
- rt_kprintf("Start Xmac NUM0 \n");
- FXmacOsStart(&fxmac_os_instace[MAC_NUM0_CONTROLLER]);
- #endif
- #if defined(MAC_NUM1)
- rt_kprintf("Start Xmac NUM1 \n");
- FXmacOsStart(&fxmac_os_instace[MAC_NUM1_CONTROLLER]);
- #endif
- __exit:
- return state;
- }
- INIT_DEVICE_EXPORT(rt_hw_xmac_eth_init);
- #endif
|