macb_main.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219
  1. /*
  2. * Cadence MACB/GEM Ethernet Controller driver
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/crc32.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/circ_buf.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_data/macb.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/phy.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_mdio.h>
  34. #include <linux/of_net.h>
  35. #include <linux/ip.h>
  36. #include <linux/udp.h>
  37. #include <linux/tcp.h>
  38. #include "macb.h"
  39. #define MACB_RX_BUFFER_SIZE 128
  40. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  41. #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
  42. #define MIN_RX_RING_SIZE 64
  43. #define MAX_RX_RING_SIZE 8192
  44. #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  45. * (bp)->rx_ring_size)
  46. #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
  47. #define MIN_TX_RING_SIZE 64
  48. #define MAX_TX_RING_SIZE 4096
  49. #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  50. * (bp)->tx_ring_size)
  51. /* level of occupied TX descriptors under which we wake up TX process */
  52. #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
  53. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  54. | MACB_BIT(ISR_ROVR))
  55. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  56. | MACB_BIT(ISR_RLE) \
  57. | MACB_BIT(TXERR))
  58. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  59. /* Max length of transmit frame must be a multiple of 8 bytes */
  60. #define MACB_TX_LEN_ALIGN 8
  61. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  62. #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  63. #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
  64. #define MACB_NETIF_LSO NETIF_F_TSO
  65. #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  66. #define MACB_WOL_ENABLED (0x1 << 1)
  67. /* Graceful stop timeouts in us. We should allow up to
  68. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  69. */
  70. #define MACB_HALT_TIMEOUT 1230
  71. /* DMA buffer descriptor might be different size
  72. * depends on hardware configuration:
  73. *
  74. * 1. dma address width 32 bits:
  75. * word 1: 32 bit address of Data Buffer
  76. * word 2: control
  77. *
  78. * 2. dma address width 64 bits:
  79. * word 1: 32 bit address of Data Buffer
  80. * word 2: control
  81. * word 3: upper 32 bit address of Data Buffer
  82. * word 4: unused
  83. *
  84. * 3. dma address width 32 bits with hardware timestamping:
  85. * word 1: 32 bit address of Data Buffer
  86. * word 2: control
  87. * word 3: timestamp word 1
  88. * word 4: timestamp word 2
  89. *
  90. * 4. dma address width 64 bits with hardware timestamping:
  91. * word 1: 32 bit address of Data Buffer
  92. * word 2: control
  93. * word 3: upper 32 bit address of Data Buffer
  94. * word 4: unused
  95. * word 5: timestamp word 1
  96. * word 6: timestamp word 2
  97. */
  98. static unsigned int macb_dma_desc_get_size(struct macb *bp)
  99. {
  100. #ifdef MACB_EXT_DESC
  101. unsigned int desc_size;
  102. switch (bp->hw_dma_cap) {
  103. case HW_DMA_CAP_64B:
  104. desc_size = sizeof(struct macb_dma_desc)
  105. + sizeof(struct macb_dma_desc_64);
  106. break;
  107. case HW_DMA_CAP_PTP:
  108. desc_size = sizeof(struct macb_dma_desc)
  109. + sizeof(struct macb_dma_desc_ptp);
  110. break;
  111. case HW_DMA_CAP_64B_PTP:
  112. desc_size = sizeof(struct macb_dma_desc)
  113. + sizeof(struct macb_dma_desc_64)
  114. + sizeof(struct macb_dma_desc_ptp);
  115. break;
  116. default:
  117. desc_size = sizeof(struct macb_dma_desc);
  118. }
  119. return desc_size;
  120. #endif
  121. return sizeof(struct macb_dma_desc);
  122. }
  123. static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
  124. {
  125. #ifdef MACB_EXT_DESC
  126. switch (bp->hw_dma_cap) {
  127. case HW_DMA_CAP_64B:
  128. case HW_DMA_CAP_PTP:
  129. desc_idx <<= 1;
  130. break;
  131. case HW_DMA_CAP_64B_PTP:
  132. desc_idx *= 3;
  133. break;
  134. default:
  135. break;
  136. }
  137. #endif
  138. return desc_idx;
  139. }
  140. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  141. static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
  142. {
  143. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  144. return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
  145. return NULL;
  146. }
  147. #endif
  148. /* Ring buffer accessors */
  149. static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
  150. {
  151. return index & (bp->tx_ring_size - 1);
  152. }
  153. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  154. unsigned int index)
  155. {
  156. index = macb_tx_ring_wrap(queue->bp, index);
  157. index = macb_adj_dma_desc_idx(queue->bp, index);
  158. return &queue->tx_ring[index];
  159. }
  160. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  161. unsigned int index)
  162. {
  163. return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
  164. }
  165. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  166. {
  167. dma_addr_t offset;
  168. offset = macb_tx_ring_wrap(queue->bp, index) *
  169. macb_dma_desc_get_size(queue->bp);
  170. return queue->tx_ring_dma + offset;
  171. }
  172. static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
  173. {
  174. return index & (bp->rx_ring_size - 1);
  175. }
  176. static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
  177. {
  178. index = macb_rx_ring_wrap(queue->bp, index);
  179. index = macb_adj_dma_desc_idx(queue->bp, index);
  180. return &queue->rx_ring[index];
  181. }
  182. static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
  183. {
  184. return queue->rx_buffers + queue->bp->rx_buffer_size *
  185. macb_rx_ring_wrap(queue->bp, index);
  186. }
  187. /* I/O accessors */
  188. static u32 hw_readl_native(struct macb *bp, int offset)
  189. {
  190. return __raw_readl(bp->regs + offset);
  191. }
  192. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  193. {
  194. __raw_writel(value, bp->regs + offset);
  195. }
  196. static u32 hw_readl(struct macb *bp, int offset)
  197. {
  198. return readl_relaxed(bp->regs + offset);
  199. }
  200. static void hw_writel(struct macb *bp, int offset, u32 value)
  201. {
  202. writel_relaxed(value, bp->regs + offset);
  203. }
  204. /* Find the CPU endianness by using the loopback bit of NCR register. When the
  205. * CPU is in big endian we need to program swapped mode for management
  206. * descriptor access.
  207. */
  208. static bool hw_is_native_io(void __iomem *addr)
  209. {
  210. u32 value = MACB_BIT(LLB);
  211. __raw_writel(value, addr + MACB_NCR);
  212. value = __raw_readl(addr + MACB_NCR);
  213. /* Write 0 back to disable everything */
  214. __raw_writel(0, addr + MACB_NCR);
  215. return value == MACB_BIT(LLB);
  216. }
  217. static bool hw_is_gem(void __iomem *addr, bool native_io)
  218. {
  219. u32 id;
  220. if (native_io)
  221. id = __raw_readl(addr + MACB_MID);
  222. else
  223. id = readl_relaxed(addr + MACB_MID);
  224. return MACB_BFEXT(IDNUM, id) >= 0x2;
  225. }
  226. static void macb_set_hwaddr(struct macb *bp)
  227. {
  228. u32 bottom;
  229. u16 top;
  230. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  231. macb_or_gem_writel(bp, SA1B, bottom);
  232. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  233. macb_or_gem_writel(bp, SA1T, top);
  234. /* Clear unused address register sets */
  235. macb_or_gem_writel(bp, SA2B, 0);
  236. macb_or_gem_writel(bp, SA2T, 0);
  237. macb_or_gem_writel(bp, SA3B, 0);
  238. macb_or_gem_writel(bp, SA3T, 0);
  239. macb_or_gem_writel(bp, SA4B, 0);
  240. macb_or_gem_writel(bp, SA4T, 0);
  241. }
  242. static void macb_get_hwaddr(struct macb *bp)
  243. {
  244. struct macb_platform_data *pdata;
  245. u32 bottom;
  246. u16 top;
  247. u8 addr[6];
  248. int i;
  249. pdata = dev_get_platdata(&bp->pdev->dev);
  250. /* Check all 4 address register for valid address */
  251. for (i = 0; i < 4; i++) {
  252. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  253. top = macb_or_gem_readl(bp, SA1T + i * 8);
  254. if (pdata && pdata->rev_eth_addr) {
  255. addr[5] = bottom & 0xff;
  256. addr[4] = (bottom >> 8) & 0xff;
  257. addr[3] = (bottom >> 16) & 0xff;
  258. addr[2] = (bottom >> 24) & 0xff;
  259. addr[1] = top & 0xff;
  260. addr[0] = (top & 0xff00) >> 8;
  261. } else {
  262. addr[0] = bottom & 0xff;
  263. addr[1] = (bottom >> 8) & 0xff;
  264. addr[2] = (bottom >> 16) & 0xff;
  265. addr[3] = (bottom >> 24) & 0xff;
  266. addr[4] = top & 0xff;
  267. addr[5] = (top >> 8) & 0xff;
  268. }
  269. if (is_valid_ether_addr(addr)) {
  270. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  271. return;
  272. }
  273. }
  274. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  275. eth_hw_addr_random(bp->dev);
  276. }
  277. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  278. {
  279. struct macb *bp = bus->priv;
  280. int value;
  281. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  282. | MACB_BF(RW, MACB_MAN_READ)
  283. | MACB_BF(PHYA, mii_id)
  284. | MACB_BF(REGA, regnum)
  285. | MACB_BF(CODE, MACB_MAN_CODE)));
  286. /* wait for end of transfer */
  287. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  288. cpu_relax();
  289. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  290. return value;
  291. }
  292. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  293. u16 value)
  294. {
  295. struct macb *bp = bus->priv;
  296. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  297. | MACB_BF(RW, MACB_MAN_WRITE)
  298. | MACB_BF(PHYA, mii_id)
  299. | MACB_BF(REGA, regnum)
  300. | MACB_BF(CODE, MACB_MAN_CODE)
  301. | MACB_BF(DATA, value)));
  302. /* wait for end of transfer */
  303. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  304. cpu_relax();
  305. return 0;
  306. }
  307. /**
  308. * macb_set_tx_clk() - Set a clock to a new frequency
  309. * @clk Pointer to the clock to change
  310. * @rate New frequency in Hz
  311. * @dev Pointer to the struct net_device
  312. */
  313. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  314. {
  315. long ferr, rate, rate_rounded;
  316. if (!clk)
  317. return;
  318. switch (speed) {
  319. case SPEED_10:
  320. rate = 2500000;
  321. break;
  322. case SPEED_100:
  323. rate = 25000000;
  324. break;
  325. case SPEED_1000:
  326. rate = 125000000;
  327. break;
  328. default:
  329. return;
  330. }
  331. rate_rounded = clk_round_rate(clk, rate);
  332. if (rate_rounded < 0)
  333. return;
  334. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  335. * is not satisfied.
  336. */
  337. ferr = abs(rate_rounded - rate);
  338. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  339. if (ferr > 5)
  340. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  341. rate);
  342. if (clk_set_rate(clk, rate_rounded))
  343. netdev_err(dev, "adjusting tx_clk failed.\n");
  344. }
  345. static void macb_handle_link_change(struct net_device *dev)
  346. {
  347. struct macb *bp = netdev_priv(dev);
  348. struct phy_device *phydev = dev->phydev;
  349. unsigned long flags;
  350. int status_change = 0;
  351. spin_lock_irqsave(&bp->lock, flags);
  352. if (phydev->link) {
  353. if ((bp->speed != phydev->speed) ||
  354. (bp->duplex != phydev->duplex)) {
  355. u32 reg;
  356. reg = macb_readl(bp, NCFGR);
  357. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  358. if (macb_is_gem(bp))
  359. reg &= ~GEM_BIT(GBE);
  360. if (phydev->duplex)
  361. reg |= MACB_BIT(FD);
  362. if (phydev->speed == SPEED_100)
  363. reg |= MACB_BIT(SPD);
  364. if (phydev->speed == SPEED_1000 &&
  365. bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  366. reg |= GEM_BIT(GBE);
  367. macb_or_gem_writel(bp, NCFGR, reg);
  368. bp->speed = phydev->speed;
  369. bp->duplex = phydev->duplex;
  370. status_change = 1;
  371. }
  372. }
  373. if (phydev->link != bp->link) {
  374. if (!phydev->link) {
  375. bp->speed = 0;
  376. bp->duplex = -1;
  377. }
  378. bp->link = phydev->link;
  379. status_change = 1;
  380. }
  381. spin_unlock_irqrestore(&bp->lock, flags);
  382. if (status_change) {
  383. if (phydev->link) {
  384. /* Update the TX clock rate if and only if the link is
  385. * up and there has been a link change.
  386. */
  387. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  388. netif_carrier_on(dev);
  389. netdev_info(dev, "link up (%d/%s)\n",
  390. phydev->speed,
  391. phydev->duplex == DUPLEX_FULL ?
  392. "Full" : "Half");
  393. } else {
  394. netif_carrier_off(dev);
  395. netdev_info(dev, "link down\n");
  396. }
  397. }
  398. }
  399. /* based on au1000_eth. c*/
  400. static int macb_mii_probe(struct net_device *dev)
  401. {
  402. struct macb *bp = netdev_priv(dev);
  403. struct macb_platform_data *pdata;
  404. struct phy_device *phydev;
  405. struct device_node *np;
  406. int phy_irq, ret, i;
  407. pdata = dev_get_platdata(&bp->pdev->dev);
  408. np = bp->pdev->dev.of_node;
  409. ret = 0;
  410. if (np) {
  411. if (of_phy_is_fixed_link(np)) {
  412. bp->phy_node = of_node_get(np);
  413. } else {
  414. bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
  415. /* fallback to standard phy registration if no
  416. * phy-handle was found nor any phy found during
  417. * dt phy registration
  418. */
  419. if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
  420. for (i = 0; i < PHY_MAX_ADDR; i++) {
  421. struct phy_device *phydev;
  422. phydev = mdiobus_scan(bp->mii_bus, i);
  423. if (IS_ERR(phydev) &&
  424. PTR_ERR(phydev) != -ENODEV) {
  425. ret = PTR_ERR(phydev);
  426. break;
  427. }
  428. }
  429. if (ret)
  430. return -ENODEV;
  431. }
  432. }
  433. }
  434. if (bp->phy_node) {
  435. phydev = of_phy_connect(dev, bp->phy_node,
  436. &macb_handle_link_change, 0,
  437. bp->phy_interface);
  438. if (!phydev)
  439. return -ENODEV;
  440. } else {
  441. phydev = phy_find_first(bp->mii_bus);
  442. if (!phydev) {
  443. netdev_err(dev, "no PHY found\n");
  444. return -ENXIO;
  445. }
  446. if (pdata) {
  447. if (gpio_is_valid(pdata->phy_irq_pin)) {
  448. ret = devm_gpio_request(&bp->pdev->dev,
  449. pdata->phy_irq_pin, "phy int");
  450. if (!ret) {
  451. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  452. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  453. }
  454. } else {
  455. phydev->irq = PHY_POLL;
  456. }
  457. }
  458. /* attach the mac to the phy */
  459. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  460. bp->phy_interface);
  461. if (ret) {
  462. netdev_err(dev, "Could not attach to PHY\n");
  463. return ret;
  464. }
  465. }
  466. /* mask with MAC supported features */
  467. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  468. phy_set_max_speed(phydev, SPEED_1000);
  469. else
  470. phy_set_max_speed(phydev, SPEED_100);
  471. if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
  472. phy_remove_link_mode(phydev,
  473. ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
  474. bp->link = 0;
  475. bp->speed = 0;
  476. bp->duplex = -1;
  477. return 0;
  478. }
  479. static int macb_mii_init(struct macb *bp)
  480. {
  481. struct macb_platform_data *pdata;
  482. struct device_node *np;
  483. int err = -ENXIO;
  484. /* Enable management port */
  485. macb_writel(bp, NCR, MACB_BIT(MPE));
  486. bp->mii_bus = mdiobus_alloc();
  487. if (!bp->mii_bus) {
  488. err = -ENOMEM;
  489. goto err_out;
  490. }
  491. bp->mii_bus->name = "MACB_mii_bus";
  492. bp->mii_bus->read = &macb_mdio_read;
  493. bp->mii_bus->write = &macb_mdio_write;
  494. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  495. bp->pdev->name, bp->pdev->id);
  496. bp->mii_bus->priv = bp;
  497. bp->mii_bus->parent = &bp->pdev->dev;
  498. pdata = dev_get_platdata(&bp->pdev->dev);
  499. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  500. np = bp->pdev->dev.of_node;
  501. if (np && of_phy_is_fixed_link(np)) {
  502. if (of_phy_register_fixed_link(np) < 0) {
  503. dev_err(&bp->pdev->dev,
  504. "broken fixed-link specification %pOF\n", np);
  505. goto err_out_free_mdiobus;
  506. }
  507. err = mdiobus_register(bp->mii_bus);
  508. } else {
  509. if (pdata)
  510. bp->mii_bus->phy_mask = pdata->phy_mask;
  511. err = of_mdiobus_register(bp->mii_bus, np);
  512. }
  513. if (err)
  514. goto err_out_free_fixed_link;
  515. err = macb_mii_probe(bp->dev);
  516. if (err)
  517. goto err_out_unregister_bus;
  518. return 0;
  519. err_out_unregister_bus:
  520. mdiobus_unregister(bp->mii_bus);
  521. err_out_free_fixed_link:
  522. if (np && of_phy_is_fixed_link(np))
  523. of_phy_deregister_fixed_link(np);
  524. err_out_free_mdiobus:
  525. of_node_put(bp->phy_node);
  526. mdiobus_free(bp->mii_bus);
  527. err_out:
  528. return err;
  529. }
  530. static void macb_update_stats(struct macb *bp)
  531. {
  532. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  533. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  534. int offset = MACB_PFR;
  535. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  536. for (; p < end; p++, offset += 4)
  537. *p += bp->macb_reg_readl(bp, offset);
  538. }
  539. static int macb_halt_tx(struct macb *bp)
  540. {
  541. unsigned long halt_time, timeout;
  542. u32 status;
  543. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  544. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  545. do {
  546. halt_time = jiffies;
  547. status = macb_readl(bp, TSR);
  548. if (!(status & MACB_BIT(TGO)))
  549. return 0;
  550. udelay(250);
  551. } while (time_before(halt_time, timeout));
  552. return -ETIMEDOUT;
  553. }
  554. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  555. {
  556. if (tx_skb->mapping) {
  557. if (tx_skb->mapped_as_page)
  558. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  559. tx_skb->size, DMA_TO_DEVICE);
  560. else
  561. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  562. tx_skb->size, DMA_TO_DEVICE);
  563. tx_skb->mapping = 0;
  564. }
  565. if (tx_skb->skb) {
  566. dev_kfree_skb_any(tx_skb->skb);
  567. tx_skb->skb = NULL;
  568. }
  569. }
  570. static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
  571. {
  572. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  573. struct macb_dma_desc_64 *desc_64;
  574. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  575. desc_64 = macb_64b_desc(bp, desc);
  576. desc_64->addrh = upper_32_bits(addr);
  577. }
  578. #endif
  579. desc->addr = lower_32_bits(addr);
  580. }
  581. static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
  582. {
  583. dma_addr_t addr = 0;
  584. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  585. struct macb_dma_desc_64 *desc_64;
  586. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  587. desc_64 = macb_64b_desc(bp, desc);
  588. addr = ((u64)(desc_64->addrh) << 32);
  589. }
  590. #endif
  591. addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  592. return addr;
  593. }
  594. static void macb_tx_error_task(struct work_struct *work)
  595. {
  596. struct macb_queue *queue = container_of(work, struct macb_queue,
  597. tx_error_task);
  598. struct macb *bp = queue->bp;
  599. struct macb_tx_skb *tx_skb;
  600. struct macb_dma_desc *desc;
  601. struct sk_buff *skb;
  602. unsigned int tail;
  603. unsigned long flags;
  604. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  605. (unsigned int)(queue - bp->queues),
  606. queue->tx_tail, queue->tx_head);
  607. /* Prevent the queue IRQ handlers from running: each of them may call
  608. * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
  609. * As explained below, we have to halt the transmission before updating
  610. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  611. * network engine about the macb/gem being halted.
  612. */
  613. spin_lock_irqsave(&bp->lock, flags);
  614. /* Make sure nobody is trying to queue up new packets */
  615. netif_tx_stop_all_queues(bp->dev);
  616. /* Stop transmission now
  617. * (in case we have just queued new packets)
  618. * macb/gem must be halted to write TBQP register
  619. */
  620. if (macb_halt_tx(bp))
  621. /* Just complain for now, reinitializing TX path can be good */
  622. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  623. /* Treat frames in TX queue including the ones that caused the error.
  624. * Free transmit buffers in upper layer.
  625. */
  626. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  627. u32 ctrl;
  628. desc = macb_tx_desc(queue, tail);
  629. ctrl = desc->ctrl;
  630. tx_skb = macb_tx_skb(queue, tail);
  631. skb = tx_skb->skb;
  632. if (ctrl & MACB_BIT(TX_USED)) {
  633. /* skb is set for the last buffer of the frame */
  634. while (!skb) {
  635. macb_tx_unmap(bp, tx_skb);
  636. tail++;
  637. tx_skb = macb_tx_skb(queue, tail);
  638. skb = tx_skb->skb;
  639. }
  640. /* ctrl still refers to the first buffer descriptor
  641. * since it's the only one written back by the hardware
  642. */
  643. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  644. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  645. macb_tx_ring_wrap(bp, tail),
  646. skb->data);
  647. bp->dev->stats.tx_packets++;
  648. queue->stats.tx_packets++;
  649. bp->dev->stats.tx_bytes += skb->len;
  650. queue->stats.tx_bytes += skb->len;
  651. }
  652. } else {
  653. /* "Buffers exhausted mid-frame" errors may only happen
  654. * if the driver is buggy, so complain loudly about
  655. * those. Statistics are updated by hardware.
  656. */
  657. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  658. netdev_err(bp->dev,
  659. "BUG: TX buffers exhausted mid-frame\n");
  660. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  661. }
  662. macb_tx_unmap(bp, tx_skb);
  663. }
  664. /* Set end of TX queue */
  665. desc = macb_tx_desc(queue, 0);
  666. macb_set_addr(bp, desc, 0);
  667. desc->ctrl = MACB_BIT(TX_USED);
  668. /* Make descriptor updates visible to hardware */
  669. wmb();
  670. /* Reinitialize the TX desc queue */
  671. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  672. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  673. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  674. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  675. #endif
  676. /* Make TX ring reflect state of hardware */
  677. queue->tx_head = 0;
  678. queue->tx_tail = 0;
  679. /* Housework before enabling TX IRQ */
  680. macb_writel(bp, TSR, macb_readl(bp, TSR));
  681. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  682. /* Now we are ready to start transmission again */
  683. netif_tx_start_all_queues(bp->dev);
  684. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  685. spin_unlock_irqrestore(&bp->lock, flags);
  686. }
  687. static void macb_tx_interrupt(struct macb_queue *queue)
  688. {
  689. unsigned int tail;
  690. unsigned int head;
  691. u32 status;
  692. struct macb *bp = queue->bp;
  693. u16 queue_index = queue - bp->queues;
  694. status = macb_readl(bp, TSR);
  695. macb_writel(bp, TSR, status);
  696. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  697. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  698. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  699. (unsigned long)status);
  700. head = queue->tx_head;
  701. for (tail = queue->tx_tail; tail != head; tail++) {
  702. struct macb_tx_skb *tx_skb;
  703. struct sk_buff *skb;
  704. struct macb_dma_desc *desc;
  705. u32 ctrl;
  706. desc = macb_tx_desc(queue, tail);
  707. /* Make hw descriptor updates visible to CPU */
  708. rmb();
  709. ctrl = desc->ctrl;
  710. /* TX_USED bit is only set by hardware on the very first buffer
  711. * descriptor of the transmitted frame.
  712. */
  713. if (!(ctrl & MACB_BIT(TX_USED)))
  714. break;
  715. /* Process all buffers of the current transmitted frame */
  716. for (;; tail++) {
  717. tx_skb = macb_tx_skb(queue, tail);
  718. skb = tx_skb->skb;
  719. /* First, update TX stats if needed */
  720. if (skb) {
  721. if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
  722. /* skb now belongs to timestamp buffer
  723. * and will be removed later
  724. */
  725. tx_skb->skb = NULL;
  726. }
  727. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  728. macb_tx_ring_wrap(bp, tail),
  729. skb->data);
  730. bp->dev->stats.tx_packets++;
  731. queue->stats.tx_packets++;
  732. bp->dev->stats.tx_bytes += skb->len;
  733. queue->stats.tx_bytes += skb->len;
  734. }
  735. /* Now we can safely release resources */
  736. macb_tx_unmap(bp, tx_skb);
  737. /* skb is set only for the last buffer of the frame.
  738. * WARNING: at this point skb has been freed by
  739. * macb_tx_unmap().
  740. */
  741. if (skb)
  742. break;
  743. }
  744. }
  745. queue->tx_tail = tail;
  746. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  747. CIRC_CNT(queue->tx_head, queue->tx_tail,
  748. bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
  749. netif_wake_subqueue(bp->dev, queue_index);
  750. }
  751. static void gem_rx_refill(struct macb_queue *queue)
  752. {
  753. unsigned int entry;
  754. struct sk_buff *skb;
  755. dma_addr_t paddr;
  756. struct macb *bp = queue->bp;
  757. struct macb_dma_desc *desc;
  758. while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
  759. bp->rx_ring_size) > 0) {
  760. entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
  761. /* Make hw descriptor updates visible to CPU */
  762. rmb();
  763. queue->rx_prepared_head++;
  764. desc = macb_rx_desc(queue, entry);
  765. if (!queue->rx_skbuff[entry]) {
  766. /* allocate sk_buff for this free entry in ring */
  767. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  768. if (unlikely(!skb)) {
  769. netdev_err(bp->dev,
  770. "Unable to allocate sk_buff\n");
  771. break;
  772. }
  773. /* now fill corresponding descriptor entry */
  774. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  775. bp->rx_buffer_size,
  776. DMA_FROM_DEVICE);
  777. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  778. dev_kfree_skb(skb);
  779. break;
  780. }
  781. queue->rx_skbuff[entry] = skb;
  782. if (entry == bp->rx_ring_size - 1)
  783. paddr |= MACB_BIT(RX_WRAP);
  784. macb_set_addr(bp, desc, paddr);
  785. desc->ctrl = 0;
  786. /* properly align Ethernet header */
  787. skb_reserve(skb, NET_IP_ALIGN);
  788. } else {
  789. desc->addr &= ~MACB_BIT(RX_USED);
  790. desc->ctrl = 0;
  791. }
  792. }
  793. /* Make descriptor updates visible to hardware */
  794. wmb();
  795. netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
  796. queue, queue->rx_prepared_head, queue->rx_tail);
  797. }
  798. /* Mark DMA descriptors from begin up to and not including end as unused */
  799. static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
  800. unsigned int end)
  801. {
  802. unsigned int frag;
  803. for (frag = begin; frag != end; frag++) {
  804. struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
  805. desc->addr &= ~MACB_BIT(RX_USED);
  806. }
  807. /* Make descriptor updates visible to hardware */
  808. wmb();
  809. /* When this happens, the hardware stats registers for
  810. * whatever caused this is updated, so we don't have to record
  811. * anything.
  812. */
  813. }
  814. static int gem_rx(struct macb_queue *queue, int budget)
  815. {
  816. struct macb *bp = queue->bp;
  817. unsigned int len;
  818. unsigned int entry;
  819. struct sk_buff *skb;
  820. struct macb_dma_desc *desc;
  821. int count = 0;
  822. while (count < budget) {
  823. u32 ctrl;
  824. dma_addr_t addr;
  825. bool rxused;
  826. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  827. desc = macb_rx_desc(queue, entry);
  828. /* Make hw descriptor updates visible to CPU */
  829. rmb();
  830. rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
  831. addr = macb_get_addr(bp, desc);
  832. ctrl = desc->ctrl;
  833. if (!rxused)
  834. break;
  835. queue->rx_tail++;
  836. count++;
  837. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  838. netdev_err(bp->dev,
  839. "not whole frame pointed by descriptor\n");
  840. bp->dev->stats.rx_dropped++;
  841. queue->stats.rx_dropped++;
  842. break;
  843. }
  844. skb = queue->rx_skbuff[entry];
  845. if (unlikely(!skb)) {
  846. netdev_err(bp->dev,
  847. "inconsistent Rx descriptor chain\n");
  848. bp->dev->stats.rx_dropped++;
  849. queue->stats.rx_dropped++;
  850. break;
  851. }
  852. /* now everything is ready for receiving packet */
  853. queue->rx_skbuff[entry] = NULL;
  854. len = ctrl & bp->rx_frm_len_mask;
  855. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  856. skb_put(skb, len);
  857. dma_unmap_single(&bp->pdev->dev, addr,
  858. bp->rx_buffer_size, DMA_FROM_DEVICE);
  859. skb->protocol = eth_type_trans(skb, bp->dev);
  860. skb_checksum_none_assert(skb);
  861. if (bp->dev->features & NETIF_F_RXCSUM &&
  862. !(bp->dev->flags & IFF_PROMISC) &&
  863. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  864. skb->ip_summed = CHECKSUM_UNNECESSARY;
  865. bp->dev->stats.rx_packets++;
  866. queue->stats.rx_packets++;
  867. bp->dev->stats.rx_bytes += skb->len;
  868. queue->stats.rx_bytes += skb->len;
  869. gem_ptp_do_rxstamp(bp, skb, desc);
  870. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  871. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  872. skb->len, skb->csum);
  873. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  874. skb_mac_header(skb), 16, true);
  875. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  876. skb->data, 32, true);
  877. #endif
  878. netif_receive_skb(skb);
  879. }
  880. gem_rx_refill(queue);
  881. return count;
  882. }
  883. static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
  884. unsigned int last_frag)
  885. {
  886. unsigned int len;
  887. unsigned int frag;
  888. unsigned int offset;
  889. struct sk_buff *skb;
  890. struct macb_dma_desc *desc;
  891. struct macb *bp = queue->bp;
  892. desc = macb_rx_desc(queue, last_frag);
  893. len = desc->ctrl & bp->rx_frm_len_mask;
  894. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  895. macb_rx_ring_wrap(bp, first_frag),
  896. macb_rx_ring_wrap(bp, last_frag), len);
  897. /* The ethernet header starts NET_IP_ALIGN bytes into the
  898. * first buffer. Since the header is 14 bytes, this makes the
  899. * payload word-aligned.
  900. *
  901. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  902. * the two padding bytes into the skb so that we avoid hitting
  903. * the slowpath in memcpy(), and pull them off afterwards.
  904. */
  905. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  906. if (!skb) {
  907. bp->dev->stats.rx_dropped++;
  908. for (frag = first_frag; ; frag++) {
  909. desc = macb_rx_desc(queue, frag);
  910. desc->addr &= ~MACB_BIT(RX_USED);
  911. if (frag == last_frag)
  912. break;
  913. }
  914. /* Make descriptor updates visible to hardware */
  915. wmb();
  916. return 1;
  917. }
  918. offset = 0;
  919. len += NET_IP_ALIGN;
  920. skb_checksum_none_assert(skb);
  921. skb_put(skb, len);
  922. for (frag = first_frag; ; frag++) {
  923. unsigned int frag_len = bp->rx_buffer_size;
  924. if (offset + frag_len > len) {
  925. if (unlikely(frag != last_frag)) {
  926. dev_kfree_skb_any(skb);
  927. return -1;
  928. }
  929. frag_len = len - offset;
  930. }
  931. skb_copy_to_linear_data_offset(skb, offset,
  932. macb_rx_buffer(queue, frag),
  933. frag_len);
  934. offset += bp->rx_buffer_size;
  935. desc = macb_rx_desc(queue, frag);
  936. desc->addr &= ~MACB_BIT(RX_USED);
  937. if (frag == last_frag)
  938. break;
  939. }
  940. /* Make descriptor updates visible to hardware */
  941. wmb();
  942. __skb_pull(skb, NET_IP_ALIGN);
  943. skb->protocol = eth_type_trans(skb, bp->dev);
  944. bp->dev->stats.rx_packets++;
  945. bp->dev->stats.rx_bytes += skb->len;
  946. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  947. skb->len, skb->csum);
  948. netif_receive_skb(skb);
  949. return 0;
  950. }
  951. static inline void macb_init_rx_ring(struct macb_queue *queue)
  952. {
  953. struct macb *bp = queue->bp;
  954. dma_addr_t addr;
  955. struct macb_dma_desc *desc = NULL;
  956. int i;
  957. addr = queue->rx_buffers_dma;
  958. for (i = 0; i < bp->rx_ring_size; i++) {
  959. desc = macb_rx_desc(queue, i);
  960. macb_set_addr(bp, desc, addr);
  961. desc->ctrl = 0;
  962. addr += bp->rx_buffer_size;
  963. }
  964. desc->addr |= MACB_BIT(RX_WRAP);
  965. queue->rx_tail = 0;
  966. }
  967. static int macb_rx(struct macb_queue *queue, int budget)
  968. {
  969. struct macb *bp = queue->bp;
  970. bool reset_rx_queue = false;
  971. int received = 0;
  972. unsigned int tail;
  973. int first_frag = -1;
  974. for (tail = queue->rx_tail; budget > 0; tail++) {
  975. struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
  976. u32 ctrl;
  977. /* Make hw descriptor updates visible to CPU */
  978. rmb();
  979. ctrl = desc->ctrl;
  980. if (!(desc->addr & MACB_BIT(RX_USED)))
  981. break;
  982. if (ctrl & MACB_BIT(RX_SOF)) {
  983. if (first_frag != -1)
  984. discard_partial_frame(queue, first_frag, tail);
  985. first_frag = tail;
  986. }
  987. if (ctrl & MACB_BIT(RX_EOF)) {
  988. int dropped;
  989. if (unlikely(first_frag == -1)) {
  990. reset_rx_queue = true;
  991. continue;
  992. }
  993. dropped = macb_rx_frame(queue, first_frag, tail);
  994. first_frag = -1;
  995. if (unlikely(dropped < 0)) {
  996. reset_rx_queue = true;
  997. continue;
  998. }
  999. if (!dropped) {
  1000. received++;
  1001. budget--;
  1002. }
  1003. }
  1004. }
  1005. if (unlikely(reset_rx_queue)) {
  1006. unsigned long flags;
  1007. u32 ctrl;
  1008. netdev_err(bp->dev, "RX queue corruption: reset it\n");
  1009. spin_lock_irqsave(&bp->lock, flags);
  1010. ctrl = macb_readl(bp, NCR);
  1011. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1012. macb_init_rx_ring(queue);
  1013. queue_writel(queue, RBQP, queue->rx_ring_dma);
  1014. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1015. spin_unlock_irqrestore(&bp->lock, flags);
  1016. return received;
  1017. }
  1018. if (first_frag != -1)
  1019. queue->rx_tail = first_frag;
  1020. else
  1021. queue->rx_tail = tail;
  1022. return received;
  1023. }
  1024. static int macb_poll(struct napi_struct *napi, int budget)
  1025. {
  1026. struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
  1027. struct macb *bp = queue->bp;
  1028. int work_done;
  1029. u32 status;
  1030. status = macb_readl(bp, RSR);
  1031. macb_writel(bp, RSR, status);
  1032. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  1033. (unsigned long)status, budget);
  1034. work_done = bp->macbgem_ops.mog_rx(queue, budget);
  1035. if (work_done < budget) {
  1036. napi_complete_done(napi, work_done);
  1037. /* Packets received while interrupts were disabled */
  1038. status = macb_readl(bp, RSR);
  1039. if (status) {
  1040. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1041. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1042. napi_reschedule(napi);
  1043. } else {
  1044. queue_writel(queue, IER, MACB_RX_INT_FLAGS);
  1045. }
  1046. }
  1047. /* TODO: Handle errors */
  1048. return work_done;
  1049. }
  1050. static void macb_hresp_error_task(unsigned long data)
  1051. {
  1052. struct macb *bp = (struct macb *)data;
  1053. struct net_device *dev = bp->dev;
  1054. struct macb_queue *queue = bp->queues;
  1055. unsigned int q;
  1056. u32 ctrl;
  1057. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1058. queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
  1059. MACB_TX_INT_FLAGS |
  1060. MACB_BIT(HRESP));
  1061. }
  1062. ctrl = macb_readl(bp, NCR);
  1063. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  1064. macb_writel(bp, NCR, ctrl);
  1065. netif_tx_stop_all_queues(dev);
  1066. netif_carrier_off(dev);
  1067. bp->macbgem_ops.mog_init_rings(bp);
  1068. /* Initialize TX and RX buffers */
  1069. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1070. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  1071. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1072. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1073. queue_writel(queue, RBQPH,
  1074. upper_32_bits(queue->rx_ring_dma));
  1075. #endif
  1076. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1077. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1078. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1079. queue_writel(queue, TBQPH,
  1080. upper_32_bits(queue->tx_ring_dma));
  1081. #endif
  1082. /* Enable interrupts */
  1083. queue_writel(queue, IER,
  1084. MACB_RX_INT_FLAGS |
  1085. MACB_TX_INT_FLAGS |
  1086. MACB_BIT(HRESP));
  1087. }
  1088. ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
  1089. macb_writel(bp, NCR, ctrl);
  1090. netif_carrier_on(dev);
  1091. netif_tx_start_all_queues(dev);
  1092. }
  1093. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  1094. {
  1095. struct macb_queue *queue = dev_id;
  1096. struct macb *bp = queue->bp;
  1097. struct net_device *dev = bp->dev;
  1098. u32 status, ctrl;
  1099. status = queue_readl(queue, ISR);
  1100. if (unlikely(!status))
  1101. return IRQ_NONE;
  1102. spin_lock(&bp->lock);
  1103. while (status) {
  1104. /* close possible race with dev_close */
  1105. if (unlikely(!netif_running(dev))) {
  1106. queue_writel(queue, IDR, -1);
  1107. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1108. queue_writel(queue, ISR, -1);
  1109. break;
  1110. }
  1111. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  1112. (unsigned int)(queue - bp->queues),
  1113. (unsigned long)status);
  1114. if (status & MACB_RX_INT_FLAGS) {
  1115. /* There's no point taking any more interrupts
  1116. * until we have processed the buffers. The
  1117. * scheduling call may fail if the poll routine
  1118. * is already scheduled, so disable interrupts
  1119. * now.
  1120. */
  1121. queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
  1122. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1123. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1124. if (napi_schedule_prep(&queue->napi)) {
  1125. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  1126. __napi_schedule(&queue->napi);
  1127. }
  1128. }
  1129. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  1130. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  1131. schedule_work(&queue->tx_error_task);
  1132. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1133. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  1134. break;
  1135. }
  1136. if (status & MACB_BIT(TCOMP))
  1137. macb_tx_interrupt(queue);
  1138. /* Link change detection isn't possible with RMII, so we'll
  1139. * add that if/when we get our hands on a full-blown MII PHY.
  1140. */
  1141. /* There is a hardware issue under heavy load where DMA can
  1142. * stop, this causes endless "used buffer descriptor read"
  1143. * interrupts but it can be cleared by re-enabling RX. See
  1144. * the at91 manual, section 41.3.1 or the Zynq manual
  1145. * section 16.7.4 for details.
  1146. */
  1147. if (status & MACB_BIT(RXUBR)) {
  1148. ctrl = macb_readl(bp, NCR);
  1149. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1150. wmb();
  1151. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1152. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1153. queue_writel(queue, ISR, MACB_BIT(RXUBR));
  1154. }
  1155. if (status & MACB_BIT(ISR_ROVR)) {
  1156. /* We missed at least one packet */
  1157. if (macb_is_gem(bp))
  1158. bp->hw_stats.gem.rx_overruns++;
  1159. else
  1160. bp->hw_stats.macb.rx_overruns++;
  1161. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1162. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  1163. }
  1164. if (status & MACB_BIT(HRESP)) {
  1165. tasklet_schedule(&bp->hresp_err_tasklet);
  1166. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  1167. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1168. queue_writel(queue, ISR, MACB_BIT(HRESP));
  1169. }
  1170. status = queue_readl(queue, ISR);
  1171. }
  1172. spin_unlock(&bp->lock);
  1173. return IRQ_HANDLED;
  1174. }
  1175. #ifdef CONFIG_NET_POLL_CONTROLLER
  1176. /* Polling receive - used by netconsole and other diagnostic tools
  1177. * to allow network i/o with interrupts disabled.
  1178. */
  1179. static void macb_poll_controller(struct net_device *dev)
  1180. {
  1181. struct macb *bp = netdev_priv(dev);
  1182. struct macb_queue *queue;
  1183. unsigned long flags;
  1184. unsigned int q;
  1185. local_irq_save(flags);
  1186. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1187. macb_interrupt(dev->irq, queue);
  1188. local_irq_restore(flags);
  1189. }
  1190. #endif
  1191. static unsigned int macb_tx_map(struct macb *bp,
  1192. struct macb_queue *queue,
  1193. struct sk_buff *skb,
  1194. unsigned int hdrlen)
  1195. {
  1196. dma_addr_t mapping;
  1197. unsigned int len, entry, i, tx_head = queue->tx_head;
  1198. struct macb_tx_skb *tx_skb = NULL;
  1199. struct macb_dma_desc *desc;
  1200. unsigned int offset, size, count = 0;
  1201. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  1202. unsigned int eof = 1, mss_mfs = 0;
  1203. u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
  1204. /* LSO */
  1205. if (skb_shinfo(skb)->gso_size != 0) {
  1206. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1207. /* UDP - UFO */
  1208. lso_ctrl = MACB_LSO_UFO_ENABLE;
  1209. else
  1210. /* TCP - TSO */
  1211. lso_ctrl = MACB_LSO_TSO_ENABLE;
  1212. }
  1213. /* First, map non-paged data */
  1214. len = skb_headlen(skb);
  1215. /* first buffer length */
  1216. size = hdrlen;
  1217. offset = 0;
  1218. while (len) {
  1219. entry = macb_tx_ring_wrap(bp, tx_head);
  1220. tx_skb = &queue->tx_skb[entry];
  1221. mapping = dma_map_single(&bp->pdev->dev,
  1222. skb->data + offset,
  1223. size, DMA_TO_DEVICE);
  1224. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1225. goto dma_error;
  1226. /* Save info to properly release resources */
  1227. tx_skb->skb = NULL;
  1228. tx_skb->mapping = mapping;
  1229. tx_skb->size = size;
  1230. tx_skb->mapped_as_page = false;
  1231. len -= size;
  1232. offset += size;
  1233. count++;
  1234. tx_head++;
  1235. size = min(len, bp->max_tx_length);
  1236. }
  1237. /* Then, map paged data from fragments */
  1238. for (f = 0; f < nr_frags; f++) {
  1239. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1240. len = skb_frag_size(frag);
  1241. offset = 0;
  1242. while (len) {
  1243. size = min(len, bp->max_tx_length);
  1244. entry = macb_tx_ring_wrap(bp, tx_head);
  1245. tx_skb = &queue->tx_skb[entry];
  1246. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  1247. offset, size, DMA_TO_DEVICE);
  1248. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1249. goto dma_error;
  1250. /* Save info to properly release resources */
  1251. tx_skb->skb = NULL;
  1252. tx_skb->mapping = mapping;
  1253. tx_skb->size = size;
  1254. tx_skb->mapped_as_page = true;
  1255. len -= size;
  1256. offset += size;
  1257. count++;
  1258. tx_head++;
  1259. }
  1260. }
  1261. /* Should never happen */
  1262. if (unlikely(!tx_skb)) {
  1263. netdev_err(bp->dev, "BUG! empty skb!\n");
  1264. return 0;
  1265. }
  1266. /* This is the last buffer of the frame: save socket buffer */
  1267. tx_skb->skb = skb;
  1268. /* Update TX ring: update buffer descriptors in reverse order
  1269. * to avoid race condition
  1270. */
  1271. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1272. * to set the end of TX queue
  1273. */
  1274. i = tx_head;
  1275. entry = macb_tx_ring_wrap(bp, i);
  1276. ctrl = MACB_BIT(TX_USED);
  1277. desc = macb_tx_desc(queue, entry);
  1278. desc->ctrl = ctrl;
  1279. if (lso_ctrl) {
  1280. if (lso_ctrl == MACB_LSO_UFO_ENABLE)
  1281. /* include header and FCS in value given to h/w */
  1282. mss_mfs = skb_shinfo(skb)->gso_size +
  1283. skb_transport_offset(skb) +
  1284. ETH_FCS_LEN;
  1285. else /* TSO */ {
  1286. mss_mfs = skb_shinfo(skb)->gso_size;
  1287. /* TCP Sequence Number Source Select
  1288. * can be set only for TSO
  1289. */
  1290. seq_ctrl = 0;
  1291. }
  1292. }
  1293. do {
  1294. i--;
  1295. entry = macb_tx_ring_wrap(bp, i);
  1296. tx_skb = &queue->tx_skb[entry];
  1297. desc = macb_tx_desc(queue, entry);
  1298. ctrl = (u32)tx_skb->size;
  1299. if (eof) {
  1300. ctrl |= MACB_BIT(TX_LAST);
  1301. eof = 0;
  1302. }
  1303. if (unlikely(entry == (bp->tx_ring_size - 1)))
  1304. ctrl |= MACB_BIT(TX_WRAP);
  1305. /* First descriptor is header descriptor */
  1306. if (i == queue->tx_head) {
  1307. ctrl |= MACB_BF(TX_LSO, lso_ctrl);
  1308. ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
  1309. if ((bp->dev->features & NETIF_F_HW_CSUM) &&
  1310. skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
  1311. ctrl |= MACB_BIT(TX_NOCRC);
  1312. } else
  1313. /* Only set MSS/MFS on payload descriptors
  1314. * (second or later descriptor)
  1315. */
  1316. ctrl |= MACB_BF(MSS_MFS, mss_mfs);
  1317. /* Set TX buffer descriptor */
  1318. macb_set_addr(bp, desc, tx_skb->mapping);
  1319. /* desc->addr must be visible to hardware before clearing
  1320. * 'TX_USED' bit in desc->ctrl.
  1321. */
  1322. wmb();
  1323. desc->ctrl = ctrl;
  1324. } while (i != queue->tx_head);
  1325. queue->tx_head = tx_head;
  1326. return count;
  1327. dma_error:
  1328. netdev_err(bp->dev, "TX DMA map failed\n");
  1329. for (i = queue->tx_head; i != tx_head; i++) {
  1330. tx_skb = macb_tx_skb(queue, i);
  1331. macb_tx_unmap(bp, tx_skb);
  1332. }
  1333. return 0;
  1334. }
  1335. static netdev_features_t macb_features_check(struct sk_buff *skb,
  1336. struct net_device *dev,
  1337. netdev_features_t features)
  1338. {
  1339. unsigned int nr_frags, f;
  1340. unsigned int hdrlen;
  1341. /* Validate LSO compatibility */
  1342. /* there is only one buffer */
  1343. if (!skb_is_nonlinear(skb))
  1344. return features;
  1345. /* length of header */
  1346. hdrlen = skb_transport_offset(skb);
  1347. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  1348. hdrlen += tcp_hdrlen(skb);
  1349. /* For LSO:
  1350. * When software supplies two or more payload buffers all payload buffers
  1351. * apart from the last must be a multiple of 8 bytes in size.
  1352. */
  1353. if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
  1354. return features & ~MACB_NETIF_LSO;
  1355. nr_frags = skb_shinfo(skb)->nr_frags;
  1356. /* No need to check last fragment */
  1357. nr_frags--;
  1358. for (f = 0; f < nr_frags; f++) {
  1359. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1360. if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
  1361. return features & ~MACB_NETIF_LSO;
  1362. }
  1363. return features;
  1364. }
  1365. static inline int macb_clear_csum(struct sk_buff *skb)
  1366. {
  1367. /* no change for packets without checksum offloading */
  1368. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1369. return 0;
  1370. /* make sure we can modify the header */
  1371. if (unlikely(skb_cow_head(skb, 0)))
  1372. return -1;
  1373. /* initialize checksum field
  1374. * This is required - at least for Zynq, which otherwise calculates
  1375. * wrong UDP header checksums for UDP packets with UDP data len <=2
  1376. */
  1377. *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
  1378. return 0;
  1379. }
  1380. static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
  1381. {
  1382. bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
  1383. int padlen = ETH_ZLEN - (*skb)->len;
  1384. int headroom = skb_headroom(*skb);
  1385. int tailroom = skb_tailroom(*skb);
  1386. struct sk_buff *nskb;
  1387. u32 fcs;
  1388. if (!(ndev->features & NETIF_F_HW_CSUM) ||
  1389. !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
  1390. skb_shinfo(*skb)->gso_size) /* Not available for GSO */
  1391. return 0;
  1392. if (padlen <= 0) {
  1393. /* FCS could be appeded to tailroom. */
  1394. if (tailroom >= ETH_FCS_LEN)
  1395. goto add_fcs;
  1396. /* FCS could be appeded by moving data to headroom. */
  1397. else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
  1398. padlen = 0;
  1399. /* No room for FCS, need to reallocate skb. */
  1400. else
  1401. padlen = ETH_FCS_LEN - tailroom;
  1402. } else {
  1403. /* Add room for FCS. */
  1404. padlen += ETH_FCS_LEN;
  1405. }
  1406. if (!cloned && headroom + tailroom >= padlen) {
  1407. (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
  1408. skb_set_tail_pointer(*skb, (*skb)->len);
  1409. } else {
  1410. nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
  1411. if (!nskb)
  1412. return -ENOMEM;
  1413. dev_kfree_skb_any(*skb);
  1414. *skb = nskb;
  1415. }
  1416. if (padlen) {
  1417. if (padlen >= ETH_FCS_LEN)
  1418. skb_put_zero(*skb, padlen - ETH_FCS_LEN);
  1419. else
  1420. skb_trim(*skb, ETH_FCS_LEN - padlen);
  1421. }
  1422. add_fcs:
  1423. /* set FCS to packet */
  1424. fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
  1425. fcs = ~fcs;
  1426. skb_put_u8(*skb, fcs & 0xff);
  1427. skb_put_u8(*skb, (fcs >> 8) & 0xff);
  1428. skb_put_u8(*skb, (fcs >> 16) & 0xff);
  1429. skb_put_u8(*skb, (fcs >> 24) & 0xff);
  1430. return 0;
  1431. }
  1432. static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1433. {
  1434. u16 queue_index = skb_get_queue_mapping(skb);
  1435. struct macb *bp = netdev_priv(dev);
  1436. struct macb_queue *queue = &bp->queues[queue_index];
  1437. unsigned long flags;
  1438. unsigned int desc_cnt, nr_frags, frag_size, f;
  1439. unsigned int hdrlen;
  1440. bool is_lso, is_udp = 0;
  1441. netdev_tx_t ret = NETDEV_TX_OK;
  1442. if (macb_clear_csum(skb)) {
  1443. dev_kfree_skb_any(skb);
  1444. return ret;
  1445. }
  1446. if (macb_pad_and_fcs(&skb, dev)) {
  1447. dev_kfree_skb_any(skb);
  1448. return ret;
  1449. }
  1450. is_lso = (skb_shinfo(skb)->gso_size != 0);
  1451. if (is_lso) {
  1452. is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
  1453. /* length of headers */
  1454. if (is_udp)
  1455. /* only queue eth + ip headers separately for UDP */
  1456. hdrlen = skb_transport_offset(skb);
  1457. else
  1458. hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1459. if (skb_headlen(skb) < hdrlen) {
  1460. netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
  1461. /* if this is required, would need to copy to single buffer */
  1462. return NETDEV_TX_BUSY;
  1463. }
  1464. } else
  1465. hdrlen = min(skb_headlen(skb), bp->max_tx_length);
  1466. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1467. netdev_vdbg(bp->dev,
  1468. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1469. queue_index, skb->len, skb->head, skb->data,
  1470. skb_tail_pointer(skb), skb_end_pointer(skb));
  1471. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1472. skb->data, 16, true);
  1473. #endif
  1474. /* Count how many TX buffer descriptors are needed to send this
  1475. * socket buffer: skb fragments of jumbo frames may need to be
  1476. * split into many buffer descriptors.
  1477. */
  1478. if (is_lso && (skb_headlen(skb) > hdrlen))
  1479. /* extra header descriptor if also payload in first buffer */
  1480. desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
  1481. else
  1482. desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1483. nr_frags = skb_shinfo(skb)->nr_frags;
  1484. for (f = 0; f < nr_frags; f++) {
  1485. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1486. desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1487. }
  1488. spin_lock_irqsave(&bp->lock, flags);
  1489. /* This is a hard error, log it. */
  1490. if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
  1491. bp->tx_ring_size) < desc_cnt) {
  1492. netif_stop_subqueue(dev, queue_index);
  1493. spin_unlock_irqrestore(&bp->lock, flags);
  1494. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1495. queue->tx_head, queue->tx_tail);
  1496. return NETDEV_TX_BUSY;
  1497. }
  1498. /* Map socket buffer for DMA transfer */
  1499. if (!macb_tx_map(bp, queue, skb, hdrlen)) {
  1500. dev_kfree_skb_any(skb);
  1501. goto unlock;
  1502. }
  1503. /* Make newly initialized descriptor visible to hardware */
  1504. wmb();
  1505. skb_tx_timestamp(skb);
  1506. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1507. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
  1508. netif_stop_subqueue(dev, queue_index);
  1509. unlock:
  1510. spin_unlock_irqrestore(&bp->lock, flags);
  1511. return ret;
  1512. }
  1513. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1514. {
  1515. if (!macb_is_gem(bp)) {
  1516. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1517. } else {
  1518. bp->rx_buffer_size = size;
  1519. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1520. netdev_dbg(bp->dev,
  1521. "RX buffer must be multiple of %d bytes, expanding\n",
  1522. RX_BUFFER_MULTIPLE);
  1523. bp->rx_buffer_size =
  1524. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1525. }
  1526. }
  1527. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
  1528. bp->dev->mtu, bp->rx_buffer_size);
  1529. }
  1530. static void gem_free_rx_buffers(struct macb *bp)
  1531. {
  1532. struct sk_buff *skb;
  1533. struct macb_dma_desc *desc;
  1534. struct macb_queue *queue;
  1535. dma_addr_t addr;
  1536. unsigned int q;
  1537. int i;
  1538. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1539. if (!queue->rx_skbuff)
  1540. continue;
  1541. for (i = 0; i < bp->rx_ring_size; i++) {
  1542. skb = queue->rx_skbuff[i];
  1543. if (!skb)
  1544. continue;
  1545. desc = macb_rx_desc(queue, i);
  1546. addr = macb_get_addr(bp, desc);
  1547. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1548. DMA_FROM_DEVICE);
  1549. dev_kfree_skb_any(skb);
  1550. skb = NULL;
  1551. }
  1552. kfree(queue->rx_skbuff);
  1553. queue->rx_skbuff = NULL;
  1554. }
  1555. }
  1556. static void macb_free_rx_buffers(struct macb *bp)
  1557. {
  1558. struct macb_queue *queue = &bp->queues[0];
  1559. if (queue->rx_buffers) {
  1560. dma_free_coherent(&bp->pdev->dev,
  1561. bp->rx_ring_size * bp->rx_buffer_size,
  1562. queue->rx_buffers, queue->rx_buffers_dma);
  1563. queue->rx_buffers = NULL;
  1564. }
  1565. }
  1566. static void macb_free_consistent(struct macb *bp)
  1567. {
  1568. struct macb_queue *queue;
  1569. unsigned int q;
  1570. int size;
  1571. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1572. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1573. kfree(queue->tx_skb);
  1574. queue->tx_skb = NULL;
  1575. if (queue->tx_ring) {
  1576. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  1577. dma_free_coherent(&bp->pdev->dev, size,
  1578. queue->tx_ring, queue->tx_ring_dma);
  1579. queue->tx_ring = NULL;
  1580. }
  1581. if (queue->rx_ring) {
  1582. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  1583. dma_free_coherent(&bp->pdev->dev, size,
  1584. queue->rx_ring, queue->rx_ring_dma);
  1585. queue->rx_ring = NULL;
  1586. }
  1587. }
  1588. }
  1589. static int gem_alloc_rx_buffers(struct macb *bp)
  1590. {
  1591. struct macb_queue *queue;
  1592. unsigned int q;
  1593. int size;
  1594. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1595. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  1596. queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1597. if (!queue->rx_skbuff)
  1598. return -ENOMEM;
  1599. else
  1600. netdev_dbg(bp->dev,
  1601. "Allocated %d RX struct sk_buff entries at %p\n",
  1602. bp->rx_ring_size, queue->rx_skbuff);
  1603. }
  1604. return 0;
  1605. }
  1606. static int macb_alloc_rx_buffers(struct macb *bp)
  1607. {
  1608. struct macb_queue *queue = &bp->queues[0];
  1609. int size;
  1610. size = bp->rx_ring_size * bp->rx_buffer_size;
  1611. queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1612. &queue->rx_buffers_dma, GFP_KERNEL);
  1613. if (!queue->rx_buffers)
  1614. return -ENOMEM;
  1615. netdev_dbg(bp->dev,
  1616. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1617. size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
  1618. return 0;
  1619. }
  1620. static int macb_alloc_consistent(struct macb *bp)
  1621. {
  1622. struct macb_queue *queue;
  1623. unsigned int q;
  1624. int size;
  1625. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1626. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  1627. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1628. &queue->tx_ring_dma,
  1629. GFP_KERNEL);
  1630. if (!queue->tx_ring)
  1631. goto out_err;
  1632. netdev_dbg(bp->dev,
  1633. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1634. q, size, (unsigned long)queue->tx_ring_dma,
  1635. queue->tx_ring);
  1636. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  1637. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1638. if (!queue->tx_skb)
  1639. goto out_err;
  1640. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  1641. queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1642. &queue->rx_ring_dma, GFP_KERNEL);
  1643. if (!queue->rx_ring)
  1644. goto out_err;
  1645. netdev_dbg(bp->dev,
  1646. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1647. size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
  1648. }
  1649. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1650. goto out_err;
  1651. return 0;
  1652. out_err:
  1653. macb_free_consistent(bp);
  1654. return -ENOMEM;
  1655. }
  1656. static void gem_init_rings(struct macb *bp)
  1657. {
  1658. struct macb_queue *queue;
  1659. struct macb_dma_desc *desc = NULL;
  1660. unsigned int q;
  1661. int i;
  1662. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1663. for (i = 0; i < bp->tx_ring_size; i++) {
  1664. desc = macb_tx_desc(queue, i);
  1665. macb_set_addr(bp, desc, 0);
  1666. desc->ctrl = MACB_BIT(TX_USED);
  1667. }
  1668. desc->ctrl |= MACB_BIT(TX_WRAP);
  1669. queue->tx_head = 0;
  1670. queue->tx_tail = 0;
  1671. queue->rx_tail = 0;
  1672. queue->rx_prepared_head = 0;
  1673. gem_rx_refill(queue);
  1674. }
  1675. }
  1676. static void macb_init_rings(struct macb *bp)
  1677. {
  1678. int i;
  1679. struct macb_dma_desc *desc = NULL;
  1680. macb_init_rx_ring(&bp->queues[0]);
  1681. for (i = 0; i < bp->tx_ring_size; i++) {
  1682. desc = macb_tx_desc(&bp->queues[0], i);
  1683. macb_set_addr(bp, desc, 0);
  1684. desc->ctrl = MACB_BIT(TX_USED);
  1685. }
  1686. bp->queues[0].tx_head = 0;
  1687. bp->queues[0].tx_tail = 0;
  1688. desc->ctrl |= MACB_BIT(TX_WRAP);
  1689. }
  1690. static void macb_reset_hw(struct macb *bp)
  1691. {
  1692. struct macb_queue *queue;
  1693. unsigned int q;
  1694. u32 ctrl = macb_readl(bp, NCR);
  1695. /* Disable RX and TX (XXX: Should we halt the transmission
  1696. * more gracefully?)
  1697. */
  1698. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  1699. /* Clear the stats registers (XXX: Update stats first?) */
  1700. ctrl |= MACB_BIT(CLRSTAT);
  1701. macb_writel(bp, NCR, ctrl);
  1702. /* Clear all status flags */
  1703. macb_writel(bp, TSR, -1);
  1704. macb_writel(bp, RSR, -1);
  1705. /* Disable all interrupts */
  1706. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1707. queue_writel(queue, IDR, -1);
  1708. queue_readl(queue, ISR);
  1709. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1710. queue_writel(queue, ISR, -1);
  1711. }
  1712. }
  1713. static u32 gem_mdc_clk_div(struct macb *bp)
  1714. {
  1715. u32 config;
  1716. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1717. if (pclk_hz <= 20000000)
  1718. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1719. else if (pclk_hz <= 40000000)
  1720. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1721. else if (pclk_hz <= 80000000)
  1722. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1723. else if (pclk_hz <= 120000000)
  1724. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1725. else if (pclk_hz <= 160000000)
  1726. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1727. else
  1728. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1729. return config;
  1730. }
  1731. static u32 macb_mdc_clk_div(struct macb *bp)
  1732. {
  1733. u32 config;
  1734. unsigned long pclk_hz;
  1735. if (macb_is_gem(bp))
  1736. return gem_mdc_clk_div(bp);
  1737. pclk_hz = clk_get_rate(bp->pclk);
  1738. if (pclk_hz <= 20000000)
  1739. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1740. else if (pclk_hz <= 40000000)
  1741. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1742. else if (pclk_hz <= 80000000)
  1743. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1744. else
  1745. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1746. return config;
  1747. }
  1748. /* Get the DMA bus width field of the network configuration register that we
  1749. * should program. We find the width from decoding the design configuration
  1750. * register to find the maximum supported data bus width.
  1751. */
  1752. static u32 macb_dbw(struct macb *bp)
  1753. {
  1754. if (!macb_is_gem(bp))
  1755. return 0;
  1756. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1757. case 4:
  1758. return GEM_BF(DBW, GEM_DBW128);
  1759. case 2:
  1760. return GEM_BF(DBW, GEM_DBW64);
  1761. case 1:
  1762. default:
  1763. return GEM_BF(DBW, GEM_DBW32);
  1764. }
  1765. }
  1766. /* Configure the receive DMA engine
  1767. * - use the correct receive buffer size
  1768. * - set best burst length for DMA operations
  1769. * (if not supported by FIFO, it will fallback to default)
  1770. * - set both rx/tx packet buffers to full memory size
  1771. * These are configurable parameters for GEM.
  1772. */
  1773. static void macb_configure_dma(struct macb *bp)
  1774. {
  1775. struct macb_queue *queue;
  1776. u32 buffer_size;
  1777. unsigned int q;
  1778. u32 dmacfg;
  1779. buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
  1780. if (macb_is_gem(bp)) {
  1781. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1782. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1783. if (q)
  1784. queue_writel(queue, RBQS, buffer_size);
  1785. else
  1786. dmacfg |= GEM_BF(RXBS, buffer_size);
  1787. }
  1788. if (bp->dma_burst_length)
  1789. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1790. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1791. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1792. if (bp->native_io)
  1793. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1794. else
  1795. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1796. if (bp->dev->features & NETIF_F_HW_CSUM)
  1797. dmacfg |= GEM_BIT(TXCOEN);
  1798. else
  1799. dmacfg &= ~GEM_BIT(TXCOEN);
  1800. dmacfg &= ~GEM_BIT(ADDR64);
  1801. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1802. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1803. dmacfg |= GEM_BIT(ADDR64);
  1804. #endif
  1805. #ifdef CONFIG_MACB_USE_HWSTAMP
  1806. if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
  1807. dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
  1808. #endif
  1809. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1810. dmacfg);
  1811. gem_writel(bp, DMACFG, dmacfg);
  1812. }
  1813. }
  1814. static void macb_init_hw(struct macb *bp)
  1815. {
  1816. struct macb_queue *queue;
  1817. unsigned int q;
  1818. u32 config;
  1819. macb_reset_hw(bp);
  1820. macb_set_hwaddr(bp);
  1821. config = macb_mdc_clk_div(bp);
  1822. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  1823. config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  1824. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1825. config |= MACB_BIT(PAE); /* PAuse Enable */
  1826. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1827. if (bp->caps & MACB_CAPS_JUMBO)
  1828. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  1829. else
  1830. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1831. if (bp->dev->flags & IFF_PROMISC)
  1832. config |= MACB_BIT(CAF); /* Copy All Frames */
  1833. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1834. config |= GEM_BIT(RXCOEN);
  1835. if (!(bp->dev->flags & IFF_BROADCAST))
  1836. config |= MACB_BIT(NBC); /* No BroadCast */
  1837. config |= macb_dbw(bp);
  1838. macb_writel(bp, NCFGR, config);
  1839. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  1840. gem_writel(bp, JML, bp->jumbo_max_len);
  1841. bp->speed = SPEED_10;
  1842. bp->duplex = DUPLEX_HALF;
  1843. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  1844. if (bp->caps & MACB_CAPS_JUMBO)
  1845. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  1846. macb_configure_dma(bp);
  1847. /* Initialize TX and RX buffers */
  1848. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1849. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  1850. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1851. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1852. queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
  1853. #endif
  1854. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1855. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1856. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1857. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  1858. #endif
  1859. /* Enable interrupts */
  1860. queue_writel(queue, IER,
  1861. MACB_RX_INT_FLAGS |
  1862. MACB_TX_INT_FLAGS |
  1863. MACB_BIT(HRESP));
  1864. }
  1865. /* Enable TX and RX */
  1866. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
  1867. }
  1868. /* The hash address register is 64 bits long and takes up two
  1869. * locations in the memory map. The least significant bits are stored
  1870. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1871. *
  1872. * The unicast hash enable and the multicast hash enable bits in the
  1873. * network configuration register enable the reception of hash matched
  1874. * frames. The destination address is reduced to a 6 bit index into
  1875. * the 64 bit hash register using the following hash function. The
  1876. * hash function is an exclusive or of every sixth bit of the
  1877. * destination address.
  1878. *
  1879. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1880. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1881. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1882. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1883. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1884. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1885. *
  1886. * da[0] represents the least significant bit of the first byte
  1887. * received, that is, the multicast/unicast indicator, and da[47]
  1888. * represents the most significant bit of the last byte received. If
  1889. * the hash index, hi[n], points to a bit that is set in the hash
  1890. * register then the frame will be matched according to whether the
  1891. * frame is multicast or unicast. A multicast match will be signalled
  1892. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1893. * index points to a bit set in the hash register. A unicast match
  1894. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1895. * and the hash index points to a bit set in the hash register. To
  1896. * receive all multicast frames, the hash register should be set with
  1897. * all ones and the multicast hash enable bit should be set in the
  1898. * network configuration register.
  1899. */
  1900. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1901. {
  1902. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1903. return 1;
  1904. return 0;
  1905. }
  1906. /* Return the hash index value for the specified address. */
  1907. static int hash_get_index(__u8 *addr)
  1908. {
  1909. int i, j, bitval;
  1910. int hash_index = 0;
  1911. for (j = 0; j < 6; j++) {
  1912. for (i = 0, bitval = 0; i < 8; i++)
  1913. bitval ^= hash_bit_value(i * 6 + j, addr);
  1914. hash_index |= (bitval << j);
  1915. }
  1916. return hash_index;
  1917. }
  1918. /* Add multicast addresses to the internal multicast-hash table. */
  1919. static void macb_sethashtable(struct net_device *dev)
  1920. {
  1921. struct netdev_hw_addr *ha;
  1922. unsigned long mc_filter[2];
  1923. unsigned int bitnr;
  1924. struct macb *bp = netdev_priv(dev);
  1925. mc_filter[0] = 0;
  1926. mc_filter[1] = 0;
  1927. netdev_for_each_mc_addr(ha, dev) {
  1928. bitnr = hash_get_index(ha->addr);
  1929. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1930. }
  1931. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1932. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1933. }
  1934. /* Enable/Disable promiscuous and multicast modes. */
  1935. static void macb_set_rx_mode(struct net_device *dev)
  1936. {
  1937. unsigned long cfg;
  1938. struct macb *bp = netdev_priv(dev);
  1939. cfg = macb_readl(bp, NCFGR);
  1940. if (dev->flags & IFF_PROMISC) {
  1941. /* Enable promiscuous mode */
  1942. cfg |= MACB_BIT(CAF);
  1943. /* Disable RX checksum offload */
  1944. if (macb_is_gem(bp))
  1945. cfg &= ~GEM_BIT(RXCOEN);
  1946. } else {
  1947. /* Disable promiscuous mode */
  1948. cfg &= ~MACB_BIT(CAF);
  1949. /* Enable RX checksum offload only if requested */
  1950. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1951. cfg |= GEM_BIT(RXCOEN);
  1952. }
  1953. if (dev->flags & IFF_ALLMULTI) {
  1954. /* Enable all multicast mode */
  1955. macb_or_gem_writel(bp, HRB, -1);
  1956. macb_or_gem_writel(bp, HRT, -1);
  1957. cfg |= MACB_BIT(NCFGR_MTI);
  1958. } else if (!netdev_mc_empty(dev)) {
  1959. /* Enable specific multicasts */
  1960. macb_sethashtable(dev);
  1961. cfg |= MACB_BIT(NCFGR_MTI);
  1962. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1963. /* Disable all multicast mode */
  1964. macb_or_gem_writel(bp, HRB, 0);
  1965. macb_or_gem_writel(bp, HRT, 0);
  1966. cfg &= ~MACB_BIT(NCFGR_MTI);
  1967. }
  1968. macb_writel(bp, NCFGR, cfg);
  1969. }
  1970. static int macb_open(struct net_device *dev)
  1971. {
  1972. struct macb *bp = netdev_priv(dev);
  1973. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1974. struct macb_queue *queue;
  1975. unsigned int q;
  1976. int err;
  1977. netdev_dbg(bp->dev, "open\n");
  1978. /* carrier starts down */
  1979. netif_carrier_off(dev);
  1980. /* if the phy is not yet register, retry later*/
  1981. if (!dev->phydev)
  1982. return -EAGAIN;
  1983. /* RX buffers initialization */
  1984. macb_init_rx_buffer_size(bp, bufsz);
  1985. err = macb_alloc_consistent(bp);
  1986. if (err) {
  1987. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1988. err);
  1989. return err;
  1990. }
  1991. bp->macbgem_ops.mog_init_rings(bp);
  1992. macb_init_hw(bp);
  1993. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1994. napi_enable(&queue->napi);
  1995. /* schedule a link state check */
  1996. phy_start(dev->phydev);
  1997. netif_tx_start_all_queues(dev);
  1998. if (bp->ptp_info)
  1999. bp->ptp_info->ptp_init(dev);
  2000. return 0;
  2001. }
  2002. static int macb_close(struct net_device *dev)
  2003. {
  2004. struct macb *bp = netdev_priv(dev);
  2005. struct macb_queue *queue;
  2006. unsigned long flags;
  2007. unsigned int q;
  2008. netif_tx_stop_all_queues(dev);
  2009. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  2010. napi_disable(&queue->napi);
  2011. if (dev->phydev)
  2012. phy_stop(dev->phydev);
  2013. spin_lock_irqsave(&bp->lock, flags);
  2014. macb_reset_hw(bp);
  2015. netif_carrier_off(dev);
  2016. spin_unlock_irqrestore(&bp->lock, flags);
  2017. macb_free_consistent(bp);
  2018. if (bp->ptp_info)
  2019. bp->ptp_info->ptp_remove(dev);
  2020. return 0;
  2021. }
  2022. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  2023. {
  2024. if (netif_running(dev))
  2025. return -EBUSY;
  2026. dev->mtu = new_mtu;
  2027. return 0;
  2028. }
  2029. static void gem_update_stats(struct macb *bp)
  2030. {
  2031. struct macb_queue *queue;
  2032. unsigned int i, q, idx;
  2033. unsigned long *stat;
  2034. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  2035. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  2036. u32 offset = gem_statistics[i].offset;
  2037. u64 val = bp->macb_reg_readl(bp, offset);
  2038. bp->ethtool_stats[i] += val;
  2039. *p += val;
  2040. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  2041. /* Add GEM_OCTTXH, GEM_OCTRXH */
  2042. val = bp->macb_reg_readl(bp, offset + 4);
  2043. bp->ethtool_stats[i] += ((u64)val) << 32;
  2044. *(++p) += val;
  2045. }
  2046. }
  2047. idx = GEM_STATS_LEN;
  2048. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  2049. for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
  2050. bp->ethtool_stats[idx++] = *stat;
  2051. }
  2052. static struct net_device_stats *gem_get_stats(struct macb *bp)
  2053. {
  2054. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2055. struct net_device_stats *nstat = &bp->dev->stats;
  2056. gem_update_stats(bp);
  2057. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  2058. hwstat->rx_alignment_errors +
  2059. hwstat->rx_resource_errors +
  2060. hwstat->rx_overruns +
  2061. hwstat->rx_oversize_frames +
  2062. hwstat->rx_jabbers +
  2063. hwstat->rx_undersized_frames +
  2064. hwstat->rx_length_field_frame_errors);
  2065. nstat->tx_errors = (hwstat->tx_late_collisions +
  2066. hwstat->tx_excessive_collisions +
  2067. hwstat->tx_underrun +
  2068. hwstat->tx_carrier_sense_errors);
  2069. nstat->multicast = hwstat->rx_multicast_frames;
  2070. nstat->collisions = (hwstat->tx_single_collision_frames +
  2071. hwstat->tx_multiple_collision_frames +
  2072. hwstat->tx_excessive_collisions);
  2073. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  2074. hwstat->rx_jabbers +
  2075. hwstat->rx_undersized_frames +
  2076. hwstat->rx_length_field_frame_errors);
  2077. nstat->rx_over_errors = hwstat->rx_resource_errors;
  2078. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  2079. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  2080. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2081. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  2082. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  2083. nstat->tx_fifo_errors = hwstat->tx_underrun;
  2084. return nstat;
  2085. }
  2086. static void gem_get_ethtool_stats(struct net_device *dev,
  2087. struct ethtool_stats *stats, u64 *data)
  2088. {
  2089. struct macb *bp;
  2090. bp = netdev_priv(dev);
  2091. gem_update_stats(bp);
  2092. memcpy(data, &bp->ethtool_stats, sizeof(u64)
  2093. * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
  2094. }
  2095. static int gem_get_sset_count(struct net_device *dev, int sset)
  2096. {
  2097. struct macb *bp = netdev_priv(dev);
  2098. switch (sset) {
  2099. case ETH_SS_STATS:
  2100. return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
  2101. default:
  2102. return -EOPNOTSUPP;
  2103. }
  2104. }
  2105. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  2106. {
  2107. char stat_string[ETH_GSTRING_LEN];
  2108. struct macb *bp = netdev_priv(dev);
  2109. struct macb_queue *queue;
  2110. unsigned int i;
  2111. unsigned int q;
  2112. switch (sset) {
  2113. case ETH_SS_STATS:
  2114. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  2115. memcpy(p, gem_statistics[i].stat_string,
  2116. ETH_GSTRING_LEN);
  2117. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2118. for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
  2119. snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
  2120. q, queue_statistics[i].stat_string);
  2121. memcpy(p, stat_string, ETH_GSTRING_LEN);
  2122. }
  2123. }
  2124. break;
  2125. }
  2126. }
  2127. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  2128. {
  2129. struct macb *bp = netdev_priv(dev);
  2130. struct net_device_stats *nstat = &bp->dev->stats;
  2131. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2132. if (macb_is_gem(bp))
  2133. return gem_get_stats(bp);
  2134. /* read stats from hardware */
  2135. macb_update_stats(bp);
  2136. /* Convert HW stats into netdevice stats */
  2137. nstat->rx_errors = (hwstat->rx_fcs_errors +
  2138. hwstat->rx_align_errors +
  2139. hwstat->rx_resource_errors +
  2140. hwstat->rx_overruns +
  2141. hwstat->rx_oversize_pkts +
  2142. hwstat->rx_jabbers +
  2143. hwstat->rx_undersize_pkts +
  2144. hwstat->rx_length_mismatch);
  2145. nstat->tx_errors = (hwstat->tx_late_cols +
  2146. hwstat->tx_excessive_cols +
  2147. hwstat->tx_underruns +
  2148. hwstat->tx_carrier_errors +
  2149. hwstat->sqe_test_errors);
  2150. nstat->collisions = (hwstat->tx_single_cols +
  2151. hwstat->tx_multiple_cols +
  2152. hwstat->tx_excessive_cols);
  2153. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  2154. hwstat->rx_jabbers +
  2155. hwstat->rx_undersize_pkts +
  2156. hwstat->rx_length_mismatch);
  2157. nstat->rx_over_errors = hwstat->rx_resource_errors +
  2158. hwstat->rx_overruns;
  2159. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  2160. nstat->rx_frame_errors = hwstat->rx_align_errors;
  2161. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2162. /* XXX: What does "missed" mean? */
  2163. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  2164. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  2165. nstat->tx_fifo_errors = hwstat->tx_underruns;
  2166. /* Don't know about heartbeat or window errors... */
  2167. return nstat;
  2168. }
  2169. static int macb_get_regs_len(struct net_device *netdev)
  2170. {
  2171. return MACB_GREGS_NBR * sizeof(u32);
  2172. }
  2173. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2174. void *p)
  2175. {
  2176. struct macb *bp = netdev_priv(dev);
  2177. unsigned int tail, head;
  2178. u32 *regs_buff = p;
  2179. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  2180. | MACB_GREGS_VERSION;
  2181. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  2182. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  2183. regs_buff[0] = macb_readl(bp, NCR);
  2184. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  2185. regs_buff[2] = macb_readl(bp, NSR);
  2186. regs_buff[3] = macb_readl(bp, TSR);
  2187. regs_buff[4] = macb_readl(bp, RBQP);
  2188. regs_buff[5] = macb_readl(bp, TBQP);
  2189. regs_buff[6] = macb_readl(bp, RSR);
  2190. regs_buff[7] = macb_readl(bp, IMR);
  2191. regs_buff[8] = tail;
  2192. regs_buff[9] = head;
  2193. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  2194. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  2195. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  2196. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  2197. if (macb_is_gem(bp))
  2198. regs_buff[13] = gem_readl(bp, DMACFG);
  2199. }
  2200. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2201. {
  2202. struct macb *bp = netdev_priv(netdev);
  2203. wol->supported = 0;
  2204. wol->wolopts = 0;
  2205. if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
  2206. wol->supported = WAKE_MAGIC;
  2207. if (bp->wol & MACB_WOL_ENABLED)
  2208. wol->wolopts |= WAKE_MAGIC;
  2209. }
  2210. }
  2211. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2212. {
  2213. struct macb *bp = netdev_priv(netdev);
  2214. if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
  2215. (wol->wolopts & ~WAKE_MAGIC))
  2216. return -EOPNOTSUPP;
  2217. if (wol->wolopts & WAKE_MAGIC)
  2218. bp->wol |= MACB_WOL_ENABLED;
  2219. else
  2220. bp->wol &= ~MACB_WOL_ENABLED;
  2221. device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
  2222. return 0;
  2223. }
  2224. static void macb_get_ringparam(struct net_device *netdev,
  2225. struct ethtool_ringparam *ring)
  2226. {
  2227. struct macb *bp = netdev_priv(netdev);
  2228. ring->rx_max_pending = MAX_RX_RING_SIZE;
  2229. ring->tx_max_pending = MAX_TX_RING_SIZE;
  2230. ring->rx_pending = bp->rx_ring_size;
  2231. ring->tx_pending = bp->tx_ring_size;
  2232. }
  2233. static int macb_set_ringparam(struct net_device *netdev,
  2234. struct ethtool_ringparam *ring)
  2235. {
  2236. struct macb *bp = netdev_priv(netdev);
  2237. u32 new_rx_size, new_tx_size;
  2238. unsigned int reset = 0;
  2239. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2240. return -EINVAL;
  2241. new_rx_size = clamp_t(u32, ring->rx_pending,
  2242. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  2243. new_rx_size = roundup_pow_of_two(new_rx_size);
  2244. new_tx_size = clamp_t(u32, ring->tx_pending,
  2245. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  2246. new_tx_size = roundup_pow_of_two(new_tx_size);
  2247. if ((new_tx_size == bp->tx_ring_size) &&
  2248. (new_rx_size == bp->rx_ring_size)) {
  2249. /* nothing to do */
  2250. return 0;
  2251. }
  2252. if (netif_running(bp->dev)) {
  2253. reset = 1;
  2254. macb_close(bp->dev);
  2255. }
  2256. bp->rx_ring_size = new_rx_size;
  2257. bp->tx_ring_size = new_tx_size;
  2258. if (reset)
  2259. macb_open(bp->dev);
  2260. return 0;
  2261. }
  2262. #ifdef CONFIG_MACB_USE_HWSTAMP
  2263. static unsigned int gem_get_tsu_rate(struct macb *bp)
  2264. {
  2265. struct clk *tsu_clk;
  2266. unsigned int tsu_rate;
  2267. tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
  2268. if (!IS_ERR(tsu_clk))
  2269. tsu_rate = clk_get_rate(tsu_clk);
  2270. /* try pclk instead */
  2271. else if (!IS_ERR(bp->pclk)) {
  2272. tsu_clk = bp->pclk;
  2273. tsu_rate = clk_get_rate(tsu_clk);
  2274. } else
  2275. return -ENOTSUPP;
  2276. return tsu_rate;
  2277. }
  2278. static s32 gem_get_ptp_max_adj(void)
  2279. {
  2280. return 64000000;
  2281. }
  2282. static int gem_get_ts_info(struct net_device *dev,
  2283. struct ethtool_ts_info *info)
  2284. {
  2285. struct macb *bp = netdev_priv(dev);
  2286. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
  2287. ethtool_op_get_ts_info(dev, info);
  2288. return 0;
  2289. }
  2290. info->so_timestamping =
  2291. SOF_TIMESTAMPING_TX_SOFTWARE |
  2292. SOF_TIMESTAMPING_RX_SOFTWARE |
  2293. SOF_TIMESTAMPING_SOFTWARE |
  2294. SOF_TIMESTAMPING_TX_HARDWARE |
  2295. SOF_TIMESTAMPING_RX_HARDWARE |
  2296. SOF_TIMESTAMPING_RAW_HARDWARE;
  2297. info->tx_types =
  2298. (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
  2299. (1 << HWTSTAMP_TX_OFF) |
  2300. (1 << HWTSTAMP_TX_ON);
  2301. info->rx_filters =
  2302. (1 << HWTSTAMP_FILTER_NONE) |
  2303. (1 << HWTSTAMP_FILTER_ALL);
  2304. info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
  2305. return 0;
  2306. }
  2307. static struct macb_ptp_info gem_ptp_info = {
  2308. .ptp_init = gem_ptp_init,
  2309. .ptp_remove = gem_ptp_remove,
  2310. .get_ptp_max_adj = gem_get_ptp_max_adj,
  2311. .get_tsu_rate = gem_get_tsu_rate,
  2312. .get_ts_info = gem_get_ts_info,
  2313. .get_hwtst = gem_get_hwtst,
  2314. .set_hwtst = gem_set_hwtst,
  2315. };
  2316. #endif
  2317. static int macb_get_ts_info(struct net_device *netdev,
  2318. struct ethtool_ts_info *info)
  2319. {
  2320. struct macb *bp = netdev_priv(netdev);
  2321. if (bp->ptp_info)
  2322. return bp->ptp_info->get_ts_info(netdev, info);
  2323. return ethtool_op_get_ts_info(netdev, info);
  2324. }
  2325. static void gem_enable_flow_filters(struct macb *bp, bool enable)
  2326. {
  2327. struct ethtool_rx_fs_item *item;
  2328. u32 t2_scr;
  2329. int num_t2_scr;
  2330. num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
  2331. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2332. struct ethtool_rx_flow_spec *fs = &item->fs;
  2333. struct ethtool_tcpip4_spec *tp4sp_m;
  2334. if (fs->location >= num_t2_scr)
  2335. continue;
  2336. t2_scr = gem_readl_n(bp, SCRT2, fs->location);
  2337. /* enable/disable screener regs for the flow entry */
  2338. t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
  2339. /* only enable fields with no masking */
  2340. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2341. if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
  2342. t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
  2343. else
  2344. t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
  2345. if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
  2346. t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
  2347. else
  2348. t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
  2349. if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
  2350. t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
  2351. else
  2352. t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
  2353. gem_writel_n(bp, SCRT2, fs->location, t2_scr);
  2354. }
  2355. }
  2356. static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
  2357. {
  2358. struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
  2359. uint16_t index = fs->location;
  2360. u32 w0, w1, t2_scr;
  2361. bool cmp_a = false;
  2362. bool cmp_b = false;
  2363. bool cmp_c = false;
  2364. tp4sp_v = &(fs->h_u.tcp_ip4_spec);
  2365. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2366. /* ignore field if any masking set */
  2367. if (tp4sp_m->ip4src == 0xFFFFFFFF) {
  2368. /* 1st compare reg - IP source address */
  2369. w0 = 0;
  2370. w1 = 0;
  2371. w0 = tp4sp_v->ip4src;
  2372. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2373. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2374. w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
  2375. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
  2376. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
  2377. cmp_a = true;
  2378. }
  2379. /* ignore field if any masking set */
  2380. if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
  2381. /* 2nd compare reg - IP destination address */
  2382. w0 = 0;
  2383. w1 = 0;
  2384. w0 = tp4sp_v->ip4dst;
  2385. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2386. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2387. w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
  2388. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
  2389. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
  2390. cmp_b = true;
  2391. }
  2392. /* ignore both port fields if masking set in both */
  2393. if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
  2394. /* 3rd compare reg - source port, destination port */
  2395. w0 = 0;
  2396. w1 = 0;
  2397. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
  2398. if (tp4sp_m->psrc == tp4sp_m->pdst) {
  2399. w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
  2400. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2401. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2402. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2403. } else {
  2404. /* only one port definition */
  2405. w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
  2406. w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
  2407. if (tp4sp_m->psrc == 0xFFFF) { /* src port */
  2408. w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
  2409. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2410. } else { /* dst port */
  2411. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2412. w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
  2413. }
  2414. }
  2415. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
  2416. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
  2417. cmp_c = true;
  2418. }
  2419. t2_scr = 0;
  2420. t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
  2421. t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
  2422. if (cmp_a)
  2423. t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
  2424. if (cmp_b)
  2425. t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
  2426. if (cmp_c)
  2427. t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
  2428. gem_writel_n(bp, SCRT2, index, t2_scr);
  2429. }
  2430. static int gem_add_flow_filter(struct net_device *netdev,
  2431. struct ethtool_rxnfc *cmd)
  2432. {
  2433. struct macb *bp = netdev_priv(netdev);
  2434. struct ethtool_rx_flow_spec *fs = &cmd->fs;
  2435. struct ethtool_rx_fs_item *item, *newfs;
  2436. unsigned long flags;
  2437. int ret = -EINVAL;
  2438. bool added = false;
  2439. newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
  2440. if (newfs == NULL)
  2441. return -ENOMEM;
  2442. memcpy(&newfs->fs, fs, sizeof(newfs->fs));
  2443. netdev_dbg(netdev,
  2444. "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2445. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2446. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2447. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2448. htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
  2449. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2450. /* find correct place to add in list */
  2451. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2452. if (item->fs.location > newfs->fs.location) {
  2453. list_add_tail(&newfs->list, &item->list);
  2454. added = true;
  2455. break;
  2456. } else if (item->fs.location == fs->location) {
  2457. netdev_err(netdev, "Rule not added: location %d not free!\n",
  2458. fs->location);
  2459. ret = -EBUSY;
  2460. goto err;
  2461. }
  2462. }
  2463. if (!added)
  2464. list_add_tail(&newfs->list, &bp->rx_fs_list.list);
  2465. gem_prog_cmp_regs(bp, fs);
  2466. bp->rx_fs_list.count++;
  2467. /* enable filtering if NTUPLE on */
  2468. if (netdev->features & NETIF_F_NTUPLE)
  2469. gem_enable_flow_filters(bp, 1);
  2470. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2471. return 0;
  2472. err:
  2473. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2474. kfree(newfs);
  2475. return ret;
  2476. }
  2477. static int gem_del_flow_filter(struct net_device *netdev,
  2478. struct ethtool_rxnfc *cmd)
  2479. {
  2480. struct macb *bp = netdev_priv(netdev);
  2481. struct ethtool_rx_fs_item *item;
  2482. struct ethtool_rx_flow_spec *fs;
  2483. unsigned long flags;
  2484. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2485. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2486. if (item->fs.location == cmd->fs.location) {
  2487. /* disable screener regs for the flow entry */
  2488. fs = &(item->fs);
  2489. netdev_dbg(netdev,
  2490. "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2491. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2492. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2493. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2494. htons(fs->h_u.tcp_ip4_spec.psrc),
  2495. htons(fs->h_u.tcp_ip4_spec.pdst));
  2496. gem_writel_n(bp, SCRT2, fs->location, 0);
  2497. list_del(&item->list);
  2498. bp->rx_fs_list.count--;
  2499. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2500. kfree(item);
  2501. return 0;
  2502. }
  2503. }
  2504. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2505. return -EINVAL;
  2506. }
  2507. static int gem_get_flow_entry(struct net_device *netdev,
  2508. struct ethtool_rxnfc *cmd)
  2509. {
  2510. struct macb *bp = netdev_priv(netdev);
  2511. struct ethtool_rx_fs_item *item;
  2512. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2513. if (item->fs.location == cmd->fs.location) {
  2514. memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
  2515. return 0;
  2516. }
  2517. }
  2518. return -EINVAL;
  2519. }
  2520. static int gem_get_all_flow_entries(struct net_device *netdev,
  2521. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  2522. {
  2523. struct macb *bp = netdev_priv(netdev);
  2524. struct ethtool_rx_fs_item *item;
  2525. uint32_t cnt = 0;
  2526. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2527. if (cnt == cmd->rule_cnt)
  2528. return -EMSGSIZE;
  2529. rule_locs[cnt] = item->fs.location;
  2530. cnt++;
  2531. }
  2532. cmd->data = bp->max_tuples;
  2533. cmd->rule_cnt = cnt;
  2534. return 0;
  2535. }
  2536. static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  2537. u32 *rule_locs)
  2538. {
  2539. struct macb *bp = netdev_priv(netdev);
  2540. int ret = 0;
  2541. switch (cmd->cmd) {
  2542. case ETHTOOL_GRXRINGS:
  2543. cmd->data = bp->num_queues;
  2544. break;
  2545. case ETHTOOL_GRXCLSRLCNT:
  2546. cmd->rule_cnt = bp->rx_fs_list.count;
  2547. break;
  2548. case ETHTOOL_GRXCLSRULE:
  2549. ret = gem_get_flow_entry(netdev, cmd);
  2550. break;
  2551. case ETHTOOL_GRXCLSRLALL:
  2552. ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
  2553. break;
  2554. default:
  2555. netdev_err(netdev,
  2556. "Command parameter %d is not supported\n", cmd->cmd);
  2557. ret = -EOPNOTSUPP;
  2558. }
  2559. return ret;
  2560. }
  2561. static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  2562. {
  2563. struct macb *bp = netdev_priv(netdev);
  2564. int ret;
  2565. switch (cmd->cmd) {
  2566. case ETHTOOL_SRXCLSRLINS:
  2567. if ((cmd->fs.location >= bp->max_tuples)
  2568. || (cmd->fs.ring_cookie >= bp->num_queues)) {
  2569. ret = -EINVAL;
  2570. break;
  2571. }
  2572. ret = gem_add_flow_filter(netdev, cmd);
  2573. break;
  2574. case ETHTOOL_SRXCLSRLDEL:
  2575. ret = gem_del_flow_filter(netdev, cmd);
  2576. break;
  2577. default:
  2578. netdev_err(netdev,
  2579. "Command parameter %d is not supported\n", cmd->cmd);
  2580. ret = -EOPNOTSUPP;
  2581. }
  2582. return ret;
  2583. }
  2584. static const struct ethtool_ops macb_ethtool_ops = {
  2585. .get_regs_len = macb_get_regs_len,
  2586. .get_regs = macb_get_regs,
  2587. .get_link = ethtool_op_get_link,
  2588. .get_ts_info = ethtool_op_get_ts_info,
  2589. .get_wol = macb_get_wol,
  2590. .set_wol = macb_set_wol,
  2591. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2592. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2593. .get_ringparam = macb_get_ringparam,
  2594. .set_ringparam = macb_set_ringparam,
  2595. };
  2596. static const struct ethtool_ops gem_ethtool_ops = {
  2597. .get_regs_len = macb_get_regs_len,
  2598. .get_regs = macb_get_regs,
  2599. .get_link = ethtool_op_get_link,
  2600. .get_ts_info = macb_get_ts_info,
  2601. .get_ethtool_stats = gem_get_ethtool_stats,
  2602. .get_strings = gem_get_ethtool_strings,
  2603. .get_sset_count = gem_get_sset_count,
  2604. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2605. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2606. .get_ringparam = macb_get_ringparam,
  2607. .set_ringparam = macb_set_ringparam,
  2608. .get_rxnfc = gem_get_rxnfc,
  2609. .set_rxnfc = gem_set_rxnfc,
  2610. };
  2611. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2612. {
  2613. struct phy_device *phydev = dev->phydev;
  2614. struct macb *bp = netdev_priv(dev);
  2615. if (!netif_running(dev))
  2616. return -EINVAL;
  2617. if (!phydev)
  2618. return -ENODEV;
  2619. if (!bp->ptp_info)
  2620. return phy_mii_ioctl(phydev, rq, cmd);
  2621. switch (cmd) {
  2622. case SIOCSHWTSTAMP:
  2623. return bp->ptp_info->set_hwtst(dev, rq, cmd);
  2624. case SIOCGHWTSTAMP:
  2625. return bp->ptp_info->get_hwtst(dev, rq);
  2626. default:
  2627. return phy_mii_ioctl(phydev, rq, cmd);
  2628. }
  2629. }
  2630. static int macb_set_features(struct net_device *netdev,
  2631. netdev_features_t features)
  2632. {
  2633. struct macb *bp = netdev_priv(netdev);
  2634. netdev_features_t changed = features ^ netdev->features;
  2635. /* TX checksum offload */
  2636. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  2637. u32 dmacfg;
  2638. dmacfg = gem_readl(bp, DMACFG);
  2639. if (features & NETIF_F_HW_CSUM)
  2640. dmacfg |= GEM_BIT(TXCOEN);
  2641. else
  2642. dmacfg &= ~GEM_BIT(TXCOEN);
  2643. gem_writel(bp, DMACFG, dmacfg);
  2644. }
  2645. /* RX checksum offload */
  2646. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  2647. u32 netcfg;
  2648. netcfg = gem_readl(bp, NCFGR);
  2649. if (features & NETIF_F_RXCSUM &&
  2650. !(netdev->flags & IFF_PROMISC))
  2651. netcfg |= GEM_BIT(RXCOEN);
  2652. else
  2653. netcfg &= ~GEM_BIT(RXCOEN);
  2654. gem_writel(bp, NCFGR, netcfg);
  2655. }
  2656. /* RX Flow Filters */
  2657. if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
  2658. bool turn_on = features & NETIF_F_NTUPLE;
  2659. gem_enable_flow_filters(bp, turn_on);
  2660. }
  2661. return 0;
  2662. }
  2663. static const struct net_device_ops macb_netdev_ops = {
  2664. .ndo_open = macb_open,
  2665. .ndo_stop = macb_close,
  2666. .ndo_start_xmit = macb_start_xmit,
  2667. .ndo_set_rx_mode = macb_set_rx_mode,
  2668. .ndo_get_stats = macb_get_stats,
  2669. .ndo_do_ioctl = macb_ioctl,
  2670. .ndo_validate_addr = eth_validate_addr,
  2671. .ndo_change_mtu = macb_change_mtu,
  2672. .ndo_set_mac_address = eth_mac_addr,
  2673. #ifdef CONFIG_NET_POLL_CONTROLLER
  2674. .ndo_poll_controller = macb_poll_controller,
  2675. #endif
  2676. .ndo_set_features = macb_set_features,
  2677. .ndo_features_check = macb_features_check,
  2678. };
  2679. /* Configure peripheral capabilities according to device tree
  2680. * and integration options used
  2681. */
  2682. static void macb_configure_caps(struct macb *bp,
  2683. const struct macb_config *dt_conf)
  2684. {
  2685. u32 dcfg;
  2686. if (dt_conf)
  2687. bp->caps = dt_conf->caps;
  2688. if (hw_is_gem(bp->regs, bp->native_io)) {
  2689. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  2690. dcfg = gem_readl(bp, DCFG1);
  2691. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  2692. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  2693. dcfg = gem_readl(bp, DCFG2);
  2694. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  2695. bp->caps |= MACB_CAPS_FIFO_MODE;
  2696. #ifdef CONFIG_MACB_USE_HWSTAMP
  2697. if (gem_has_ptp(bp)) {
  2698. if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
  2699. pr_err("GEM doesn't support hardware ptp.\n");
  2700. else {
  2701. bp->hw_dma_cap |= HW_DMA_CAP_PTP;
  2702. bp->ptp_info = &gem_ptp_info;
  2703. }
  2704. }
  2705. #endif
  2706. }
  2707. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  2708. }
  2709. static void macb_probe_queues(void __iomem *mem,
  2710. bool native_io,
  2711. unsigned int *queue_mask,
  2712. unsigned int *num_queues)
  2713. {
  2714. unsigned int hw_q;
  2715. *queue_mask = 0x1;
  2716. *num_queues = 1;
  2717. /* is it macb or gem ?
  2718. *
  2719. * We need to read directly from the hardware here because
  2720. * we are early in the probe process and don't have the
  2721. * MACB_CAPS_MACB_IS_GEM flag positioned
  2722. */
  2723. if (!hw_is_gem(mem, native_io))
  2724. return;
  2725. /* bit 0 is never set but queue 0 always exists */
  2726. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  2727. *queue_mask |= 0x1;
  2728. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  2729. if (*queue_mask & (1 << hw_q))
  2730. (*num_queues)++;
  2731. }
  2732. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  2733. struct clk **hclk, struct clk **tx_clk,
  2734. struct clk **rx_clk)
  2735. {
  2736. struct macb_platform_data *pdata;
  2737. int err;
  2738. pdata = dev_get_platdata(&pdev->dev);
  2739. if (pdata) {
  2740. *pclk = pdata->pclk;
  2741. *hclk = pdata->hclk;
  2742. } else {
  2743. *pclk = devm_clk_get(&pdev->dev, "pclk");
  2744. *hclk = devm_clk_get(&pdev->dev, "hclk");
  2745. }
  2746. if (IS_ERR(*pclk)) {
  2747. err = PTR_ERR(*pclk);
  2748. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  2749. return err;
  2750. }
  2751. if (IS_ERR(*hclk)) {
  2752. err = PTR_ERR(*hclk);
  2753. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  2754. return err;
  2755. }
  2756. *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  2757. if (IS_ERR(*tx_clk))
  2758. *tx_clk = NULL;
  2759. *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
  2760. if (IS_ERR(*rx_clk))
  2761. *rx_clk = NULL;
  2762. err = clk_prepare_enable(*pclk);
  2763. if (err) {
  2764. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2765. return err;
  2766. }
  2767. err = clk_prepare_enable(*hclk);
  2768. if (err) {
  2769. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  2770. goto err_disable_pclk;
  2771. }
  2772. err = clk_prepare_enable(*tx_clk);
  2773. if (err) {
  2774. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  2775. goto err_disable_hclk;
  2776. }
  2777. err = clk_prepare_enable(*rx_clk);
  2778. if (err) {
  2779. dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
  2780. goto err_disable_txclk;
  2781. }
  2782. return 0;
  2783. err_disable_txclk:
  2784. clk_disable_unprepare(*tx_clk);
  2785. err_disable_hclk:
  2786. clk_disable_unprepare(*hclk);
  2787. err_disable_pclk:
  2788. clk_disable_unprepare(*pclk);
  2789. return err;
  2790. }
  2791. static int macb_init(struct platform_device *pdev)
  2792. {
  2793. struct net_device *dev = platform_get_drvdata(pdev);
  2794. unsigned int hw_q, q;
  2795. struct macb *bp = netdev_priv(dev);
  2796. struct macb_queue *queue;
  2797. int err;
  2798. u32 val, reg;
  2799. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  2800. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  2801. /* set the queue register mapping once for all: queue0 has a special
  2802. * register mapping but we don't want to test the queue index then
  2803. * compute the corresponding register offset at run time.
  2804. */
  2805. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  2806. if (!(bp->queue_mask & (1 << hw_q)))
  2807. continue;
  2808. queue = &bp->queues[q];
  2809. queue->bp = bp;
  2810. netif_napi_add(dev, &queue->napi, macb_poll, 64);
  2811. if (hw_q) {
  2812. queue->ISR = GEM_ISR(hw_q - 1);
  2813. queue->IER = GEM_IER(hw_q - 1);
  2814. queue->IDR = GEM_IDR(hw_q - 1);
  2815. queue->IMR = GEM_IMR(hw_q - 1);
  2816. queue->TBQP = GEM_TBQP(hw_q - 1);
  2817. queue->RBQP = GEM_RBQP(hw_q - 1);
  2818. queue->RBQS = GEM_RBQS(hw_q - 1);
  2819. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2820. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2821. queue->TBQPH = GEM_TBQPH(hw_q - 1);
  2822. queue->RBQPH = GEM_RBQPH(hw_q - 1);
  2823. }
  2824. #endif
  2825. } else {
  2826. /* queue0 uses legacy registers */
  2827. queue->ISR = MACB_ISR;
  2828. queue->IER = MACB_IER;
  2829. queue->IDR = MACB_IDR;
  2830. queue->IMR = MACB_IMR;
  2831. queue->TBQP = MACB_TBQP;
  2832. queue->RBQP = MACB_RBQP;
  2833. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2834. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2835. queue->TBQPH = MACB_TBQPH;
  2836. queue->RBQPH = MACB_RBQPH;
  2837. }
  2838. #endif
  2839. }
  2840. /* get irq: here we use the linux queue index, not the hardware
  2841. * queue index. the queue irq definitions in the device tree
  2842. * must remove the optional gaps that could exist in the
  2843. * hardware queue mask.
  2844. */
  2845. queue->irq = platform_get_irq(pdev, q);
  2846. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  2847. IRQF_SHARED, dev->name, queue);
  2848. if (err) {
  2849. dev_err(&pdev->dev,
  2850. "Unable to request IRQ %d (error %d)\n",
  2851. queue->irq, err);
  2852. return err;
  2853. }
  2854. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  2855. q++;
  2856. }
  2857. dev->netdev_ops = &macb_netdev_ops;
  2858. /* setup appropriated routines according to adapter type */
  2859. if (macb_is_gem(bp)) {
  2860. bp->max_tx_length = GEM_MAX_TX_LEN;
  2861. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  2862. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  2863. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  2864. bp->macbgem_ops.mog_rx = gem_rx;
  2865. dev->ethtool_ops = &gem_ethtool_ops;
  2866. } else {
  2867. bp->max_tx_length = MACB_MAX_TX_LEN;
  2868. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  2869. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  2870. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  2871. bp->macbgem_ops.mog_rx = macb_rx;
  2872. dev->ethtool_ops = &macb_ethtool_ops;
  2873. }
  2874. /* Set features */
  2875. dev->hw_features = NETIF_F_SG;
  2876. /* Check LSO capability */
  2877. if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  2878. dev->hw_features |= MACB_NETIF_LSO;
  2879. /* Checksum offload is only available on gem with packet buffer */
  2880. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  2881. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  2882. if (bp->caps & MACB_CAPS_SG_DISABLED)
  2883. dev->hw_features &= ~NETIF_F_SG;
  2884. dev->features = dev->hw_features;
  2885. /* Check RX Flow Filters support.
  2886. * Max Rx flows set by availability of screeners & compare regs:
  2887. * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
  2888. */
  2889. reg = gem_readl(bp, DCFG8);
  2890. bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
  2891. GEM_BFEXT(T2SCR, reg));
  2892. if (bp->max_tuples > 0) {
  2893. /* also needs one ethtype match to check IPv4 */
  2894. if (GEM_BFEXT(SCR2ETH, reg) > 0) {
  2895. /* program this reg now */
  2896. reg = 0;
  2897. reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
  2898. gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
  2899. /* Filtering is supported in hw but don't enable it in kernel now */
  2900. dev->hw_features |= NETIF_F_NTUPLE;
  2901. /* init Rx flow definitions */
  2902. INIT_LIST_HEAD(&bp->rx_fs_list.list);
  2903. bp->rx_fs_list.count = 0;
  2904. spin_lock_init(&bp->rx_fs_lock);
  2905. } else
  2906. bp->max_tuples = 0;
  2907. }
  2908. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  2909. val = 0;
  2910. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  2911. val = GEM_BIT(RGMII);
  2912. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  2913. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2914. val = MACB_BIT(RMII);
  2915. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2916. val = MACB_BIT(MII);
  2917. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  2918. val |= MACB_BIT(CLKEN);
  2919. macb_or_gem_writel(bp, USRIO, val);
  2920. }
  2921. /* Set MII management clock divider */
  2922. val = macb_mdc_clk_div(bp);
  2923. val |= macb_dbw(bp);
  2924. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2925. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  2926. macb_writel(bp, NCFGR, val);
  2927. return 0;
  2928. }
  2929. #if defined(CONFIG_OF)
  2930. /* 1518 rounded up */
  2931. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  2932. /* max number of receive buffers */
  2933. #define AT91ETHER_MAX_RX_DESCR 9
  2934. /* Initialize and start the Receiver and Transmit subsystems */
  2935. static int at91ether_start(struct net_device *dev)
  2936. {
  2937. struct macb *lp = netdev_priv(dev);
  2938. struct macb_queue *q = &lp->queues[0];
  2939. struct macb_dma_desc *desc;
  2940. dma_addr_t addr;
  2941. u32 ctl;
  2942. int i;
  2943. q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  2944. (AT91ETHER_MAX_RX_DESCR *
  2945. macb_dma_desc_get_size(lp)),
  2946. &q->rx_ring_dma, GFP_KERNEL);
  2947. if (!q->rx_ring)
  2948. return -ENOMEM;
  2949. q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  2950. AT91ETHER_MAX_RX_DESCR *
  2951. AT91ETHER_MAX_RBUFF_SZ,
  2952. &q->rx_buffers_dma, GFP_KERNEL);
  2953. if (!q->rx_buffers) {
  2954. dma_free_coherent(&lp->pdev->dev,
  2955. AT91ETHER_MAX_RX_DESCR *
  2956. macb_dma_desc_get_size(lp),
  2957. q->rx_ring, q->rx_ring_dma);
  2958. q->rx_ring = NULL;
  2959. return -ENOMEM;
  2960. }
  2961. addr = q->rx_buffers_dma;
  2962. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  2963. desc = macb_rx_desc(q, i);
  2964. macb_set_addr(lp, desc, addr);
  2965. desc->ctrl = 0;
  2966. addr += AT91ETHER_MAX_RBUFF_SZ;
  2967. }
  2968. /* Set the Wrap bit on the last descriptor */
  2969. desc->addr |= MACB_BIT(RX_WRAP);
  2970. /* Reset buffer index */
  2971. q->rx_tail = 0;
  2972. /* Program address of descriptor list in Rx Buffer Queue register */
  2973. macb_writel(lp, RBQP, q->rx_ring_dma);
  2974. /* Enable Receive and Transmit */
  2975. ctl = macb_readl(lp, NCR);
  2976. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  2977. return 0;
  2978. }
  2979. /* Open the ethernet interface */
  2980. static int at91ether_open(struct net_device *dev)
  2981. {
  2982. struct macb *lp = netdev_priv(dev);
  2983. u32 ctl;
  2984. int ret;
  2985. /* Clear internal statistics */
  2986. ctl = macb_readl(lp, NCR);
  2987. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  2988. macb_set_hwaddr(lp);
  2989. ret = at91ether_start(dev);
  2990. if (ret)
  2991. return ret;
  2992. /* Enable MAC interrupts */
  2993. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  2994. MACB_BIT(RXUBR) |
  2995. MACB_BIT(ISR_TUND) |
  2996. MACB_BIT(ISR_RLE) |
  2997. MACB_BIT(TCOMP) |
  2998. MACB_BIT(ISR_ROVR) |
  2999. MACB_BIT(HRESP));
  3000. /* schedule a link state check */
  3001. phy_start(dev->phydev);
  3002. netif_start_queue(dev);
  3003. return 0;
  3004. }
  3005. /* Close the interface */
  3006. static int at91ether_close(struct net_device *dev)
  3007. {
  3008. struct macb *lp = netdev_priv(dev);
  3009. struct macb_queue *q = &lp->queues[0];
  3010. u32 ctl;
  3011. /* Disable Receiver and Transmitter */
  3012. ctl = macb_readl(lp, NCR);
  3013. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  3014. /* Disable MAC interrupts */
  3015. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  3016. MACB_BIT(RXUBR) |
  3017. MACB_BIT(ISR_TUND) |
  3018. MACB_BIT(ISR_RLE) |
  3019. MACB_BIT(TCOMP) |
  3020. MACB_BIT(ISR_ROVR) |
  3021. MACB_BIT(HRESP));
  3022. netif_stop_queue(dev);
  3023. dma_free_coherent(&lp->pdev->dev,
  3024. AT91ETHER_MAX_RX_DESCR *
  3025. macb_dma_desc_get_size(lp),
  3026. q->rx_ring, q->rx_ring_dma);
  3027. q->rx_ring = NULL;
  3028. dma_free_coherent(&lp->pdev->dev,
  3029. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  3030. q->rx_buffers, q->rx_buffers_dma);
  3031. q->rx_buffers = NULL;
  3032. return 0;
  3033. }
  3034. /* Transmit packet */
  3035. static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
  3036. struct net_device *dev)
  3037. {
  3038. struct macb *lp = netdev_priv(dev);
  3039. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  3040. netif_stop_queue(dev);
  3041. /* Store packet information (to free when Tx completed) */
  3042. lp->skb = skb;
  3043. lp->skb_length = skb->len;
  3044. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  3045. DMA_TO_DEVICE);
  3046. if (dma_mapping_error(NULL, lp->skb_physaddr)) {
  3047. dev_kfree_skb_any(skb);
  3048. dev->stats.tx_dropped++;
  3049. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  3050. return NETDEV_TX_OK;
  3051. }
  3052. /* Set address of the data in the Transmit Address register */
  3053. macb_writel(lp, TAR, lp->skb_physaddr);
  3054. /* Set length of the packet in the Transmit Control register */
  3055. macb_writel(lp, TCR, skb->len);
  3056. } else {
  3057. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  3058. return NETDEV_TX_BUSY;
  3059. }
  3060. return NETDEV_TX_OK;
  3061. }
  3062. /* Extract received frame from buffer descriptors and sent to upper layers.
  3063. * (Called from interrupt context)
  3064. */
  3065. static void at91ether_rx(struct net_device *dev)
  3066. {
  3067. struct macb *lp = netdev_priv(dev);
  3068. struct macb_queue *q = &lp->queues[0];
  3069. struct macb_dma_desc *desc;
  3070. unsigned char *p_recv;
  3071. struct sk_buff *skb;
  3072. unsigned int pktlen;
  3073. desc = macb_rx_desc(q, q->rx_tail);
  3074. while (desc->addr & MACB_BIT(RX_USED)) {
  3075. p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  3076. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  3077. skb = netdev_alloc_skb(dev, pktlen + 2);
  3078. if (skb) {
  3079. skb_reserve(skb, 2);
  3080. skb_put_data(skb, p_recv, pktlen);
  3081. skb->protocol = eth_type_trans(skb, dev);
  3082. dev->stats.rx_packets++;
  3083. dev->stats.rx_bytes += pktlen;
  3084. netif_rx(skb);
  3085. } else {
  3086. dev->stats.rx_dropped++;
  3087. }
  3088. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  3089. dev->stats.multicast++;
  3090. /* reset ownership bit */
  3091. desc->addr &= ~MACB_BIT(RX_USED);
  3092. /* wrap after last buffer */
  3093. if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  3094. q->rx_tail = 0;
  3095. else
  3096. q->rx_tail++;
  3097. desc = macb_rx_desc(q, q->rx_tail);
  3098. }
  3099. }
  3100. /* MAC interrupt handler */
  3101. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  3102. {
  3103. struct net_device *dev = dev_id;
  3104. struct macb *lp = netdev_priv(dev);
  3105. u32 intstatus, ctl;
  3106. /* MAC Interrupt Status register indicates what interrupts are pending.
  3107. * It is automatically cleared once read.
  3108. */
  3109. intstatus = macb_readl(lp, ISR);
  3110. /* Receive complete */
  3111. if (intstatus & MACB_BIT(RCOMP))
  3112. at91ether_rx(dev);
  3113. /* Transmit complete */
  3114. if (intstatus & MACB_BIT(TCOMP)) {
  3115. /* The TCOM bit is set even if the transmission failed */
  3116. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  3117. dev->stats.tx_errors++;
  3118. if (lp->skb) {
  3119. dev_kfree_skb_irq(lp->skb);
  3120. lp->skb = NULL;
  3121. dma_unmap_single(NULL, lp->skb_physaddr,
  3122. lp->skb_length, DMA_TO_DEVICE);
  3123. dev->stats.tx_packets++;
  3124. dev->stats.tx_bytes += lp->skb_length;
  3125. }
  3126. netif_wake_queue(dev);
  3127. }
  3128. /* Work-around for EMAC Errata section 41.3.1 */
  3129. if (intstatus & MACB_BIT(RXUBR)) {
  3130. ctl = macb_readl(lp, NCR);
  3131. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  3132. wmb();
  3133. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  3134. }
  3135. if (intstatus & MACB_BIT(ISR_ROVR))
  3136. netdev_err(dev, "ROVR error\n");
  3137. return IRQ_HANDLED;
  3138. }
  3139. #ifdef CONFIG_NET_POLL_CONTROLLER
  3140. static void at91ether_poll_controller(struct net_device *dev)
  3141. {
  3142. unsigned long flags;
  3143. local_irq_save(flags);
  3144. at91ether_interrupt(dev->irq, dev);
  3145. local_irq_restore(flags);
  3146. }
  3147. #endif
  3148. static const struct net_device_ops at91ether_netdev_ops = {
  3149. .ndo_open = at91ether_open,
  3150. .ndo_stop = at91ether_close,
  3151. .ndo_start_xmit = at91ether_start_xmit,
  3152. .ndo_get_stats = macb_get_stats,
  3153. .ndo_set_rx_mode = macb_set_rx_mode,
  3154. .ndo_set_mac_address = eth_mac_addr,
  3155. .ndo_do_ioctl = macb_ioctl,
  3156. .ndo_validate_addr = eth_validate_addr,
  3157. #ifdef CONFIG_NET_POLL_CONTROLLER
  3158. .ndo_poll_controller = at91ether_poll_controller,
  3159. #endif
  3160. };
  3161. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  3162. struct clk **hclk, struct clk **tx_clk,
  3163. struct clk **rx_clk)
  3164. {
  3165. int err;
  3166. *hclk = NULL;
  3167. *tx_clk = NULL;
  3168. *rx_clk = NULL;
  3169. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  3170. if (IS_ERR(*pclk))
  3171. return PTR_ERR(*pclk);
  3172. err = clk_prepare_enable(*pclk);
  3173. if (err) {
  3174. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  3175. return err;
  3176. }
  3177. return 0;
  3178. }
  3179. static int at91ether_init(struct platform_device *pdev)
  3180. {
  3181. struct net_device *dev = platform_get_drvdata(pdev);
  3182. struct macb *bp = netdev_priv(dev);
  3183. int err;
  3184. u32 reg;
  3185. bp->queues[0].bp = bp;
  3186. dev->netdev_ops = &at91ether_netdev_ops;
  3187. dev->ethtool_ops = &macb_ethtool_ops;
  3188. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  3189. 0, dev->name, dev);
  3190. if (err)
  3191. return err;
  3192. macb_writel(bp, NCR, 0);
  3193. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  3194. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  3195. reg |= MACB_BIT(RM9200_RMII);
  3196. macb_writel(bp, NCFGR, reg);
  3197. return 0;
  3198. }
  3199. static const struct macb_config at91sam9260_config = {
  3200. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3201. .clk_init = macb_clk_init,
  3202. .init = macb_init,
  3203. };
  3204. static const struct macb_config sama5d3macb_config = {
  3205. .caps = MACB_CAPS_SG_DISABLED
  3206. | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3207. .clk_init = macb_clk_init,
  3208. .init = macb_init,
  3209. };
  3210. static const struct macb_config pc302gem_config = {
  3211. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  3212. .dma_burst_length = 16,
  3213. .clk_init = macb_clk_init,
  3214. .init = macb_init,
  3215. };
  3216. static const struct macb_config sama5d2_config = {
  3217. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3218. .dma_burst_length = 16,
  3219. .clk_init = macb_clk_init,
  3220. .init = macb_init,
  3221. };
  3222. static const struct macb_config sama5d3_config = {
  3223. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
  3224. | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  3225. .dma_burst_length = 16,
  3226. .clk_init = macb_clk_init,
  3227. .init = macb_init,
  3228. .jumbo_max_len = 10240,
  3229. };
  3230. static const struct macb_config sama5d4_config = {
  3231. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3232. .dma_burst_length = 4,
  3233. .clk_init = macb_clk_init,
  3234. .init = macb_init,
  3235. };
  3236. static const struct macb_config emac_config = {
  3237. .clk_init = at91ether_clk_init,
  3238. .init = at91ether_init,
  3239. };
  3240. static const struct macb_config np4_config = {
  3241. .caps = MACB_CAPS_USRIO_DISABLED,
  3242. .clk_init = macb_clk_init,
  3243. .init = macb_init,
  3244. };
  3245. static const struct macb_config zynqmp_config = {
  3246. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3247. MACB_CAPS_JUMBO |
  3248. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
  3249. .dma_burst_length = 16,
  3250. .clk_init = macb_clk_init,
  3251. .init = macb_init,
  3252. .jumbo_max_len = 10240,
  3253. };
  3254. static const struct macb_config zynq_config = {
  3255. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  3256. .dma_burst_length = 16,
  3257. .clk_init = macb_clk_init,
  3258. .init = macb_init,
  3259. };
  3260. static const struct of_device_id macb_dt_ids[] = {
  3261. { .compatible = "cdns,at32ap7000-macb" },
  3262. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  3263. { .compatible = "cdns,macb" },
  3264. { .compatible = "cdns,np4-macb", .data = &np4_config },
  3265. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  3266. { .compatible = "cdns,gem", .data = &pc302gem_config },
  3267. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  3268. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  3269. { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
  3270. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  3271. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  3272. { .compatible = "cdns,emac", .data = &emac_config },
  3273. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  3274. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  3275. { /* sentinel */ }
  3276. };
  3277. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  3278. #endif /* CONFIG_OF */
  3279. static const struct macb_config default_gem_config = {
  3280. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3281. MACB_CAPS_JUMBO |
  3282. MACB_CAPS_GEM_HAS_PTP,
  3283. .dma_burst_length = 16,
  3284. .clk_init = macb_clk_init,
  3285. .init = macb_init,
  3286. .jumbo_max_len = 10240,
  3287. };
  3288. static int macb_probe(struct platform_device *pdev)
  3289. {
  3290. const struct macb_config *macb_config = &default_gem_config;
  3291. int (*clk_init)(struct platform_device *, struct clk **,
  3292. struct clk **, struct clk **, struct clk **)
  3293. = macb_config->clk_init;
  3294. int (*init)(struct platform_device *) = macb_config->init;
  3295. struct device_node *np = pdev->dev.of_node;
  3296. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  3297. unsigned int queue_mask, num_queues;
  3298. struct macb_platform_data *pdata;
  3299. bool native_io;
  3300. struct phy_device *phydev;
  3301. struct net_device *dev;
  3302. struct resource *regs;
  3303. void __iomem *mem;
  3304. const char *mac;
  3305. struct macb *bp;
  3306. int err, val;
  3307. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3308. mem = devm_ioremap_resource(&pdev->dev, regs);
  3309. if (IS_ERR(mem))
  3310. return PTR_ERR(mem);
  3311. if (np) {
  3312. const struct of_device_id *match;
  3313. match = of_match_node(macb_dt_ids, np);
  3314. if (match && match->data) {
  3315. macb_config = match->data;
  3316. clk_init = macb_config->clk_init;
  3317. init = macb_config->init;
  3318. }
  3319. }
  3320. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
  3321. if (err)
  3322. return err;
  3323. native_io = hw_is_native_io(mem);
  3324. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  3325. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  3326. if (!dev) {
  3327. err = -ENOMEM;
  3328. goto err_disable_clocks;
  3329. }
  3330. dev->base_addr = regs->start;
  3331. SET_NETDEV_DEV(dev, &pdev->dev);
  3332. bp = netdev_priv(dev);
  3333. bp->pdev = pdev;
  3334. bp->dev = dev;
  3335. bp->regs = mem;
  3336. bp->native_io = native_io;
  3337. if (native_io) {
  3338. bp->macb_reg_readl = hw_readl_native;
  3339. bp->macb_reg_writel = hw_writel_native;
  3340. } else {
  3341. bp->macb_reg_readl = hw_readl;
  3342. bp->macb_reg_writel = hw_writel;
  3343. }
  3344. bp->num_queues = num_queues;
  3345. bp->queue_mask = queue_mask;
  3346. if (macb_config)
  3347. bp->dma_burst_length = macb_config->dma_burst_length;
  3348. bp->pclk = pclk;
  3349. bp->hclk = hclk;
  3350. bp->tx_clk = tx_clk;
  3351. bp->rx_clk = rx_clk;
  3352. if (macb_config)
  3353. bp->jumbo_max_len = macb_config->jumbo_max_len;
  3354. bp->wol = 0;
  3355. if (of_get_property(np, "magic-packet", NULL))
  3356. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  3357. device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  3358. spin_lock_init(&bp->lock);
  3359. /* setup capabilities */
  3360. macb_configure_caps(bp, macb_config);
  3361. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3362. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  3363. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  3364. bp->hw_dma_cap |= HW_DMA_CAP_64B;
  3365. }
  3366. #endif
  3367. platform_set_drvdata(pdev, dev);
  3368. dev->irq = platform_get_irq(pdev, 0);
  3369. if (dev->irq < 0) {
  3370. err = dev->irq;
  3371. goto err_out_free_netdev;
  3372. }
  3373. /* MTU range: 68 - 1500 or 10240 */
  3374. dev->min_mtu = GEM_MTU_MIN_SIZE;
  3375. if (bp->caps & MACB_CAPS_JUMBO)
  3376. dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  3377. else
  3378. dev->max_mtu = ETH_DATA_LEN;
  3379. if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
  3380. val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
  3381. if (val)
  3382. bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
  3383. macb_dma_desc_get_size(bp);
  3384. val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
  3385. if (val)
  3386. bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
  3387. macb_dma_desc_get_size(bp);
  3388. }
  3389. mac = of_get_mac_address(np);
  3390. if (mac) {
  3391. ether_addr_copy(bp->dev->dev_addr, mac);
  3392. } else {
  3393. err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
  3394. if (err) {
  3395. if (err == -EPROBE_DEFER)
  3396. goto err_out_free_netdev;
  3397. macb_get_hwaddr(bp);
  3398. }
  3399. }
  3400. err = of_get_phy_mode(np);
  3401. if (err < 0) {
  3402. pdata = dev_get_platdata(&pdev->dev);
  3403. if (pdata && pdata->is_rmii)
  3404. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  3405. else
  3406. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  3407. } else {
  3408. bp->phy_interface = err;
  3409. }
  3410. /* IP specific init */
  3411. err = init(pdev);
  3412. if (err)
  3413. goto err_out_free_netdev;
  3414. err = macb_mii_init(bp);
  3415. if (err)
  3416. goto err_out_free_netdev;
  3417. phydev = dev->phydev;
  3418. netif_carrier_off(dev);
  3419. err = register_netdev(dev);
  3420. if (err) {
  3421. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  3422. goto err_out_unregister_mdio;
  3423. }
  3424. tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
  3425. (unsigned long)bp);
  3426. phy_attached_info(phydev);
  3427. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  3428. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  3429. dev->base_addr, dev->irq, dev->dev_addr);
  3430. return 0;
  3431. err_out_unregister_mdio:
  3432. phy_disconnect(dev->phydev);
  3433. mdiobus_unregister(bp->mii_bus);
  3434. of_node_put(bp->phy_node);
  3435. if (np && of_phy_is_fixed_link(np))
  3436. of_phy_deregister_fixed_link(np);
  3437. mdiobus_free(bp->mii_bus);
  3438. err_out_free_netdev:
  3439. free_netdev(dev);
  3440. err_disable_clocks:
  3441. clk_disable_unprepare(tx_clk);
  3442. clk_disable_unprepare(hclk);
  3443. clk_disable_unprepare(pclk);
  3444. clk_disable_unprepare(rx_clk);
  3445. return err;
  3446. }
  3447. static int macb_remove(struct platform_device *pdev)
  3448. {
  3449. struct net_device *dev;
  3450. struct macb *bp;
  3451. struct device_node *np = pdev->dev.of_node;
  3452. dev = platform_get_drvdata(pdev);
  3453. if (dev) {
  3454. bp = netdev_priv(dev);
  3455. if (dev->phydev)
  3456. phy_disconnect(dev->phydev);
  3457. mdiobus_unregister(bp->mii_bus);
  3458. if (np && of_phy_is_fixed_link(np))
  3459. of_phy_deregister_fixed_link(np);
  3460. dev->phydev = NULL;
  3461. mdiobus_free(bp->mii_bus);
  3462. unregister_netdev(dev);
  3463. clk_disable_unprepare(bp->tx_clk);
  3464. clk_disable_unprepare(bp->hclk);
  3465. clk_disable_unprepare(bp->pclk);
  3466. clk_disable_unprepare(bp->rx_clk);
  3467. of_node_put(bp->phy_node);
  3468. free_netdev(dev);
  3469. }
  3470. return 0;
  3471. }
  3472. static int __maybe_unused macb_suspend(struct device *dev)
  3473. {
  3474. struct platform_device *pdev = to_platform_device(dev);
  3475. struct net_device *netdev = platform_get_drvdata(pdev);
  3476. struct macb *bp = netdev_priv(netdev);
  3477. netif_carrier_off(netdev);
  3478. netif_device_detach(netdev);
  3479. if (bp->wol & MACB_WOL_ENABLED) {
  3480. macb_writel(bp, IER, MACB_BIT(WOL));
  3481. macb_writel(bp, WOL, MACB_BIT(MAG));
  3482. enable_irq_wake(bp->queues[0].irq);
  3483. } else {
  3484. clk_disable_unprepare(bp->tx_clk);
  3485. clk_disable_unprepare(bp->hclk);
  3486. clk_disable_unprepare(bp->pclk);
  3487. clk_disable_unprepare(bp->rx_clk);
  3488. }
  3489. return 0;
  3490. }
  3491. static int __maybe_unused macb_resume(struct device *dev)
  3492. {
  3493. struct platform_device *pdev = to_platform_device(dev);
  3494. struct net_device *netdev = platform_get_drvdata(pdev);
  3495. struct macb *bp = netdev_priv(netdev);
  3496. if (bp->wol & MACB_WOL_ENABLED) {
  3497. macb_writel(bp, IDR, MACB_BIT(WOL));
  3498. macb_writel(bp, WOL, 0);
  3499. disable_irq_wake(bp->queues[0].irq);
  3500. } else {
  3501. clk_prepare_enable(bp->pclk);
  3502. clk_prepare_enable(bp->hclk);
  3503. clk_prepare_enable(bp->tx_clk);
  3504. clk_prepare_enable(bp->rx_clk);
  3505. }
  3506. netif_device_attach(netdev);
  3507. return 0;
  3508. }
  3509. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  3510. static struct platform_driver macb_driver = {
  3511. .probe = macb_probe,
  3512. .remove = macb_remove,
  3513. .driver = {
  3514. .name = "macb",
  3515. .of_match_table = of_match_ptr(macb_dt_ids),
  3516. .pm = &macb_pm_ops,
  3517. },
  3518. };
  3519. module_platform_driver(macb_driver);
  3520. MODULE_LICENSE("GPL");
  3521. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  3522. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  3523. MODULE_ALIAS("platform:macb");