macb_main.c 91 KB

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