macb_main.c 108 KB

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