sh_eth.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* SuperH Ethernet device driver
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
  6. * Copyright (C) 2008-2014 Renesas Solutions Corp.
  7. * Copyright (C) 2013-2017 Cogent Embedded, Inc.
  8. * Copyright (C) 2014 Codethink Limited
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/delay.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mdio-bitbang.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_net.h>
  24. #include <linux/phy.h>
  25. #include <linux/cache.h>
  26. #include <linux/io.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/slab.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/sh_eth.h>
  32. #include <linux/of_mdio.h>
  33. #include "sh_eth.h"
  34. #define SH_ETH_DEF_MSG_ENABLE \
  35. (NETIF_MSG_LINK | \
  36. NETIF_MSG_TIMER | \
  37. NETIF_MSG_RX_ERR| \
  38. NETIF_MSG_TX_ERR)
  39. #define SH_ETH_OFFSET_INVALID ((u16)~0)
  40. #define SH_ETH_OFFSET_DEFAULTS \
  41. [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
  42. static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
  43. SH_ETH_OFFSET_DEFAULTS,
  44. [EDSR] = 0x0000,
  45. [EDMR] = 0x0400,
  46. [EDTRR] = 0x0408,
  47. [EDRRR] = 0x0410,
  48. [EESR] = 0x0428,
  49. [EESIPR] = 0x0430,
  50. [TDLAR] = 0x0010,
  51. [TDFAR] = 0x0014,
  52. [TDFXR] = 0x0018,
  53. [TDFFR] = 0x001c,
  54. [RDLAR] = 0x0030,
  55. [RDFAR] = 0x0034,
  56. [RDFXR] = 0x0038,
  57. [RDFFR] = 0x003c,
  58. [TRSCER] = 0x0438,
  59. [RMFCR] = 0x0440,
  60. [TFTR] = 0x0448,
  61. [FDR] = 0x0450,
  62. [RMCR] = 0x0458,
  63. [RPADIR] = 0x0460,
  64. [FCFTR] = 0x0468,
  65. [CSMR] = 0x04E4,
  66. [ECMR] = 0x0500,
  67. [ECSR] = 0x0510,
  68. [ECSIPR] = 0x0518,
  69. [PIR] = 0x0520,
  70. [PSR] = 0x0528,
  71. [PIPR] = 0x052c,
  72. [RFLR] = 0x0508,
  73. [APR] = 0x0554,
  74. [MPR] = 0x0558,
  75. [PFTCR] = 0x055c,
  76. [PFRCR] = 0x0560,
  77. [TPAUSER] = 0x0564,
  78. [GECMR] = 0x05b0,
  79. [BCULR] = 0x05b4,
  80. [MAHR] = 0x05c0,
  81. [MALR] = 0x05c8,
  82. [TROCR] = 0x0700,
  83. [CDCR] = 0x0708,
  84. [LCCR] = 0x0710,
  85. [CEFCR] = 0x0740,
  86. [FRECR] = 0x0748,
  87. [TSFRCR] = 0x0750,
  88. [TLFRCR] = 0x0758,
  89. [RFCR] = 0x0760,
  90. [CERCR] = 0x0768,
  91. [CEECR] = 0x0770,
  92. [MAFCR] = 0x0778,
  93. [RMII_MII] = 0x0790,
  94. [ARSTR] = 0x0000,
  95. [TSU_CTRST] = 0x0004,
  96. [TSU_FWEN0] = 0x0010,
  97. [TSU_FWEN1] = 0x0014,
  98. [TSU_FCM] = 0x0018,
  99. [TSU_BSYSL0] = 0x0020,
  100. [TSU_BSYSL1] = 0x0024,
  101. [TSU_PRISL0] = 0x0028,
  102. [TSU_PRISL1] = 0x002c,
  103. [TSU_FWSL0] = 0x0030,
  104. [TSU_FWSL1] = 0x0034,
  105. [TSU_FWSLC] = 0x0038,
  106. [TSU_QTAGM0] = 0x0040,
  107. [TSU_QTAGM1] = 0x0044,
  108. [TSU_FWSR] = 0x0050,
  109. [TSU_FWINMK] = 0x0054,
  110. [TSU_ADQT0] = 0x0048,
  111. [TSU_ADQT1] = 0x004c,
  112. [TSU_VTAG0] = 0x0058,
  113. [TSU_VTAG1] = 0x005c,
  114. [TSU_ADSBSY] = 0x0060,
  115. [TSU_TEN] = 0x0064,
  116. [TSU_POST1] = 0x0070,
  117. [TSU_POST2] = 0x0074,
  118. [TSU_POST3] = 0x0078,
  119. [TSU_POST4] = 0x007c,
  120. [TSU_ADRH0] = 0x0100,
  121. [TXNLCR0] = 0x0080,
  122. [TXALCR0] = 0x0084,
  123. [RXNLCR0] = 0x0088,
  124. [RXALCR0] = 0x008c,
  125. [FWNLCR0] = 0x0090,
  126. [FWALCR0] = 0x0094,
  127. [TXNLCR1] = 0x00a0,
  128. [TXALCR1] = 0x00a4,
  129. [RXNLCR1] = 0x00a8,
  130. [RXALCR1] = 0x00ac,
  131. [FWNLCR1] = 0x00b0,
  132. [FWALCR1] = 0x00b4,
  133. };
  134. static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
  135. SH_ETH_OFFSET_DEFAULTS,
  136. [EDSR] = 0x0000,
  137. [EDMR] = 0x0400,
  138. [EDTRR] = 0x0408,
  139. [EDRRR] = 0x0410,
  140. [EESR] = 0x0428,
  141. [EESIPR] = 0x0430,
  142. [TDLAR] = 0x0010,
  143. [TDFAR] = 0x0014,
  144. [TDFXR] = 0x0018,
  145. [TDFFR] = 0x001c,
  146. [RDLAR] = 0x0030,
  147. [RDFAR] = 0x0034,
  148. [RDFXR] = 0x0038,
  149. [RDFFR] = 0x003c,
  150. [TRSCER] = 0x0438,
  151. [RMFCR] = 0x0440,
  152. [TFTR] = 0x0448,
  153. [FDR] = 0x0450,
  154. [RMCR] = 0x0458,
  155. [RPADIR] = 0x0460,
  156. [FCFTR] = 0x0468,
  157. [CSMR] = 0x04E4,
  158. [ECMR] = 0x0500,
  159. [RFLR] = 0x0508,
  160. [ECSR] = 0x0510,
  161. [ECSIPR] = 0x0518,
  162. [PIR] = 0x0520,
  163. [APR] = 0x0554,
  164. [MPR] = 0x0558,
  165. [PFTCR] = 0x055c,
  166. [PFRCR] = 0x0560,
  167. [TPAUSER] = 0x0564,
  168. [MAHR] = 0x05c0,
  169. [MALR] = 0x05c8,
  170. [CEFCR] = 0x0740,
  171. [FRECR] = 0x0748,
  172. [TSFRCR] = 0x0750,
  173. [TLFRCR] = 0x0758,
  174. [RFCR] = 0x0760,
  175. [MAFCR] = 0x0778,
  176. [ARSTR] = 0x0000,
  177. [TSU_CTRST] = 0x0004,
  178. [TSU_FWSLC] = 0x0038,
  179. [TSU_VTAG0] = 0x0058,
  180. [TSU_ADSBSY] = 0x0060,
  181. [TSU_TEN] = 0x0064,
  182. [TSU_POST1] = 0x0070,
  183. [TSU_POST2] = 0x0074,
  184. [TSU_POST3] = 0x0078,
  185. [TSU_POST4] = 0x007c,
  186. [TSU_ADRH0] = 0x0100,
  187. [TXNLCR0] = 0x0080,
  188. [TXALCR0] = 0x0084,
  189. [RXNLCR0] = 0x0088,
  190. [RXALCR0] = 0x008C,
  191. };
  192. static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
  193. SH_ETH_OFFSET_DEFAULTS,
  194. [ECMR] = 0x0300,
  195. [RFLR] = 0x0308,
  196. [ECSR] = 0x0310,
  197. [ECSIPR] = 0x0318,
  198. [PIR] = 0x0320,
  199. [PSR] = 0x0328,
  200. [RDMLR] = 0x0340,
  201. [IPGR] = 0x0350,
  202. [APR] = 0x0354,
  203. [MPR] = 0x0358,
  204. [RFCF] = 0x0360,
  205. [TPAUSER] = 0x0364,
  206. [TPAUSECR] = 0x0368,
  207. [MAHR] = 0x03c0,
  208. [MALR] = 0x03c8,
  209. [TROCR] = 0x03d0,
  210. [CDCR] = 0x03d4,
  211. [LCCR] = 0x03d8,
  212. [CNDCR] = 0x03dc,
  213. [CEFCR] = 0x03e4,
  214. [FRECR] = 0x03e8,
  215. [TSFRCR] = 0x03ec,
  216. [TLFRCR] = 0x03f0,
  217. [RFCR] = 0x03f4,
  218. [MAFCR] = 0x03f8,
  219. [EDMR] = 0x0200,
  220. [EDTRR] = 0x0208,
  221. [EDRRR] = 0x0210,
  222. [TDLAR] = 0x0218,
  223. [RDLAR] = 0x0220,
  224. [EESR] = 0x0228,
  225. [EESIPR] = 0x0230,
  226. [TRSCER] = 0x0238,
  227. [RMFCR] = 0x0240,
  228. [TFTR] = 0x0248,
  229. [FDR] = 0x0250,
  230. [RMCR] = 0x0258,
  231. [TFUCR] = 0x0264,
  232. [RFOCR] = 0x0268,
  233. [RMIIMODE] = 0x026c,
  234. [FCFTR] = 0x0270,
  235. [TRIMD] = 0x027c,
  236. };
  237. static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
  238. SH_ETH_OFFSET_DEFAULTS,
  239. [ECMR] = 0x0100,
  240. [RFLR] = 0x0108,
  241. [ECSR] = 0x0110,
  242. [ECSIPR] = 0x0118,
  243. [PIR] = 0x0120,
  244. [PSR] = 0x0128,
  245. [RDMLR] = 0x0140,
  246. [IPGR] = 0x0150,
  247. [APR] = 0x0154,
  248. [MPR] = 0x0158,
  249. [TPAUSER] = 0x0164,
  250. [RFCF] = 0x0160,
  251. [TPAUSECR] = 0x0168,
  252. [BCFRR] = 0x016c,
  253. [MAHR] = 0x01c0,
  254. [MALR] = 0x01c8,
  255. [TROCR] = 0x01d0,
  256. [CDCR] = 0x01d4,
  257. [LCCR] = 0x01d8,
  258. [CNDCR] = 0x01dc,
  259. [CEFCR] = 0x01e4,
  260. [FRECR] = 0x01e8,
  261. [TSFRCR] = 0x01ec,
  262. [TLFRCR] = 0x01f0,
  263. [RFCR] = 0x01f4,
  264. [MAFCR] = 0x01f8,
  265. [RTRATE] = 0x01fc,
  266. [EDMR] = 0x0000,
  267. [EDTRR] = 0x0008,
  268. [EDRRR] = 0x0010,
  269. [TDLAR] = 0x0018,
  270. [RDLAR] = 0x0020,
  271. [EESR] = 0x0028,
  272. [EESIPR] = 0x0030,
  273. [TRSCER] = 0x0038,
  274. [RMFCR] = 0x0040,
  275. [TFTR] = 0x0048,
  276. [FDR] = 0x0050,
  277. [RMCR] = 0x0058,
  278. [TFUCR] = 0x0064,
  279. [RFOCR] = 0x0068,
  280. [FCFTR] = 0x0070,
  281. [RPADIR] = 0x0078,
  282. [TRIMD] = 0x007c,
  283. [RBWAR] = 0x00c8,
  284. [RDFAR] = 0x00cc,
  285. [TBRAR] = 0x00d4,
  286. [TDFAR] = 0x00d8,
  287. };
  288. static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
  289. SH_ETH_OFFSET_DEFAULTS,
  290. [EDMR] = 0x0000,
  291. [EDTRR] = 0x0004,
  292. [EDRRR] = 0x0008,
  293. [TDLAR] = 0x000c,
  294. [RDLAR] = 0x0010,
  295. [EESR] = 0x0014,
  296. [EESIPR] = 0x0018,
  297. [TRSCER] = 0x001c,
  298. [RMFCR] = 0x0020,
  299. [TFTR] = 0x0024,
  300. [FDR] = 0x0028,
  301. [RMCR] = 0x002c,
  302. [EDOCR] = 0x0030,
  303. [FCFTR] = 0x0034,
  304. [RPADIR] = 0x0038,
  305. [TRIMD] = 0x003c,
  306. [RBWAR] = 0x0040,
  307. [RDFAR] = 0x0044,
  308. [TBRAR] = 0x004c,
  309. [TDFAR] = 0x0050,
  310. [ECMR] = 0x0160,
  311. [ECSR] = 0x0164,
  312. [ECSIPR] = 0x0168,
  313. [PIR] = 0x016c,
  314. [MAHR] = 0x0170,
  315. [MALR] = 0x0174,
  316. [RFLR] = 0x0178,
  317. [PSR] = 0x017c,
  318. [TROCR] = 0x0180,
  319. [CDCR] = 0x0184,
  320. [LCCR] = 0x0188,
  321. [CNDCR] = 0x018c,
  322. [CEFCR] = 0x0194,
  323. [FRECR] = 0x0198,
  324. [TSFRCR] = 0x019c,
  325. [TLFRCR] = 0x01a0,
  326. [RFCR] = 0x01a4,
  327. [MAFCR] = 0x01a8,
  328. [IPGR] = 0x01b4,
  329. [APR] = 0x01b8,
  330. [MPR] = 0x01bc,
  331. [TPAUSER] = 0x01c4,
  332. [BCFR] = 0x01cc,
  333. [ARSTR] = 0x0000,
  334. [TSU_CTRST] = 0x0004,
  335. [TSU_FWEN0] = 0x0010,
  336. [TSU_FWEN1] = 0x0014,
  337. [TSU_FCM] = 0x0018,
  338. [TSU_BSYSL0] = 0x0020,
  339. [TSU_BSYSL1] = 0x0024,
  340. [TSU_PRISL0] = 0x0028,
  341. [TSU_PRISL1] = 0x002c,
  342. [TSU_FWSL0] = 0x0030,
  343. [TSU_FWSL1] = 0x0034,
  344. [TSU_FWSLC] = 0x0038,
  345. [TSU_QTAGM0] = 0x0040,
  346. [TSU_QTAGM1] = 0x0044,
  347. [TSU_ADQT0] = 0x0048,
  348. [TSU_ADQT1] = 0x004c,
  349. [TSU_FWSR] = 0x0050,
  350. [TSU_FWINMK] = 0x0054,
  351. [TSU_ADSBSY] = 0x0060,
  352. [TSU_TEN] = 0x0064,
  353. [TSU_POST1] = 0x0070,
  354. [TSU_POST2] = 0x0074,
  355. [TSU_POST3] = 0x0078,
  356. [TSU_POST4] = 0x007c,
  357. [TXNLCR0] = 0x0080,
  358. [TXALCR0] = 0x0084,
  359. [RXNLCR0] = 0x0088,
  360. [RXALCR0] = 0x008c,
  361. [FWNLCR0] = 0x0090,
  362. [FWALCR0] = 0x0094,
  363. [TXNLCR1] = 0x00a0,
  364. [TXALCR1] = 0x00a4,
  365. [RXNLCR1] = 0x00a8,
  366. [RXALCR1] = 0x00ac,
  367. [FWNLCR1] = 0x00b0,
  368. [FWALCR1] = 0x00b4,
  369. [TSU_ADRH0] = 0x0100,
  370. };
  371. static void sh_eth_rcv_snd_disable(struct net_device *ndev);
  372. static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
  373. static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
  374. {
  375. struct sh_eth_private *mdp = netdev_priv(ndev);
  376. u16 offset = mdp->reg_offset[enum_index];
  377. if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
  378. return;
  379. iowrite32(data, mdp->addr + offset);
  380. }
  381. static u32 sh_eth_read(struct net_device *ndev, int enum_index)
  382. {
  383. struct sh_eth_private *mdp = netdev_priv(ndev);
  384. u16 offset = mdp->reg_offset[enum_index];
  385. if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
  386. return ~0U;
  387. return ioread32(mdp->addr + offset);
  388. }
  389. static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
  390. u32 set)
  391. {
  392. sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
  393. enum_index);
  394. }
  395. static u16 sh_eth_tsu_get_offset(struct sh_eth_private *mdp, int enum_index)
  396. {
  397. return mdp->reg_offset[enum_index];
  398. }
  399. static void sh_eth_tsu_write(struct sh_eth_private *mdp, u32 data,
  400. int enum_index)
  401. {
  402. u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
  403. if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
  404. return;
  405. iowrite32(data, mdp->tsu_addr + offset);
  406. }
  407. static u32 sh_eth_tsu_read(struct sh_eth_private *mdp, int enum_index)
  408. {
  409. u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
  410. if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
  411. return ~0U;
  412. return ioread32(mdp->tsu_addr + offset);
  413. }
  414. static void sh_eth_soft_swap(char *src, int len)
  415. {
  416. #ifdef __LITTLE_ENDIAN
  417. u32 *p = (u32 *)src;
  418. u32 *maxp = p + DIV_ROUND_UP(len, sizeof(u32));
  419. for (; p < maxp; p++)
  420. *p = swab32(*p);
  421. #endif
  422. }
  423. static void sh_eth_select_mii(struct net_device *ndev)
  424. {
  425. struct sh_eth_private *mdp = netdev_priv(ndev);
  426. u32 value;
  427. switch (mdp->phy_interface) {
  428. case PHY_INTERFACE_MODE_RGMII ... PHY_INTERFACE_MODE_RGMII_TXID:
  429. value = 0x3;
  430. break;
  431. case PHY_INTERFACE_MODE_GMII:
  432. value = 0x2;
  433. break;
  434. case PHY_INTERFACE_MODE_MII:
  435. value = 0x1;
  436. break;
  437. case PHY_INTERFACE_MODE_RMII:
  438. value = 0x0;
  439. break;
  440. default:
  441. netdev_warn(ndev,
  442. "PHY interface mode was not setup. Set to MII.\n");
  443. value = 0x1;
  444. break;
  445. }
  446. sh_eth_write(ndev, value, RMII_MII);
  447. }
  448. static void sh_eth_set_duplex(struct net_device *ndev)
  449. {
  450. struct sh_eth_private *mdp = netdev_priv(ndev);
  451. sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
  452. }
  453. static void sh_eth_chip_reset(struct net_device *ndev)
  454. {
  455. struct sh_eth_private *mdp = netdev_priv(ndev);
  456. /* reset device */
  457. sh_eth_tsu_write(mdp, ARSTR_ARST, ARSTR);
  458. mdelay(1);
  459. }
  460. static int sh_eth_soft_reset(struct net_device *ndev)
  461. {
  462. sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
  463. mdelay(3);
  464. sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
  465. return 0;
  466. }
  467. static int sh_eth_check_soft_reset(struct net_device *ndev)
  468. {
  469. int cnt;
  470. for (cnt = 100; cnt > 0; cnt--) {
  471. if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
  472. return 0;
  473. mdelay(1);
  474. }
  475. netdev_err(ndev, "Device reset failed\n");
  476. return -ETIMEDOUT;
  477. }
  478. static int sh_eth_soft_reset_gether(struct net_device *ndev)
  479. {
  480. struct sh_eth_private *mdp = netdev_priv(ndev);
  481. int ret;
  482. sh_eth_write(ndev, EDSR_ENALL, EDSR);
  483. sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
  484. ret = sh_eth_check_soft_reset(ndev);
  485. if (ret)
  486. return ret;
  487. /* Table Init */
  488. sh_eth_write(ndev, 0, TDLAR);
  489. sh_eth_write(ndev, 0, TDFAR);
  490. sh_eth_write(ndev, 0, TDFXR);
  491. sh_eth_write(ndev, 0, TDFFR);
  492. sh_eth_write(ndev, 0, RDLAR);
  493. sh_eth_write(ndev, 0, RDFAR);
  494. sh_eth_write(ndev, 0, RDFXR);
  495. sh_eth_write(ndev, 0, RDFFR);
  496. /* Reset HW CRC register */
  497. if (mdp->cd->hw_checksum)
  498. sh_eth_write(ndev, 0, CSMR);
  499. /* Select MII mode */
  500. if (mdp->cd->select_mii)
  501. sh_eth_select_mii(ndev);
  502. return ret;
  503. }
  504. static void sh_eth_set_rate_gether(struct net_device *ndev)
  505. {
  506. struct sh_eth_private *mdp = netdev_priv(ndev);
  507. switch (mdp->speed) {
  508. case 10: /* 10BASE */
  509. sh_eth_write(ndev, GECMR_10, GECMR);
  510. break;
  511. case 100:/* 100BASE */
  512. sh_eth_write(ndev, GECMR_100, GECMR);
  513. break;
  514. case 1000: /* 1000BASE */
  515. sh_eth_write(ndev, GECMR_1000, GECMR);
  516. break;
  517. }
  518. }
  519. #ifdef CONFIG_OF
  520. /* R7S72100 */
  521. static struct sh_eth_cpu_data r7s72100_data = {
  522. .soft_reset = sh_eth_soft_reset_gether,
  523. .chip_reset = sh_eth_chip_reset,
  524. .set_duplex = sh_eth_set_duplex,
  525. .register_type = SH_ETH_REG_FAST_RZ,
  526. .edtrr_trns = EDTRR_TRNS_GETHER,
  527. .ecsr_value = ECSR_ICD,
  528. .ecsipr_value = ECSIPR_ICDIP,
  529. .eesipr_value = EESIPR_TWB1IP | EESIPR_TWBIP | EESIPR_TC1IP |
  530. EESIPR_TABTIP | EESIPR_RABTIP | EESIPR_RFCOFIP |
  531. EESIPR_ECIIP |
  532. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  533. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  534. EESIPR_RMAFIP | EESIPR_RRFIP |
  535. EESIPR_RTLFIP | EESIPR_RTSFIP |
  536. EESIPR_PREIP | EESIPR_CERFIP,
  537. .tx_check = EESR_TC1 | EESR_FTC,
  538. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  539. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  540. EESR_TDE,
  541. .fdr_value = 0x0000070f,
  542. .no_psr = 1,
  543. .apr = 1,
  544. .mpr = 1,
  545. .tpauser = 1,
  546. .hw_swap = 1,
  547. .rpadir = 1,
  548. .no_trimd = 1,
  549. .no_ade = 1,
  550. .xdfar_rw = 1,
  551. .hw_checksum = 1,
  552. .tsu = 1,
  553. .no_tx_cntrs = 1,
  554. };
  555. static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
  556. {
  557. sh_eth_chip_reset(ndev);
  558. sh_eth_select_mii(ndev);
  559. }
  560. /* R8A7740 */
  561. static struct sh_eth_cpu_data r8a7740_data = {
  562. .soft_reset = sh_eth_soft_reset_gether,
  563. .chip_reset = sh_eth_chip_reset_r8a7740,
  564. .set_duplex = sh_eth_set_duplex,
  565. .set_rate = sh_eth_set_rate_gether,
  566. .register_type = SH_ETH_REG_GIGABIT,
  567. .edtrr_trns = EDTRR_TRNS_GETHER,
  568. .ecsr_value = ECSR_ICD | ECSR_MPD,
  569. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  570. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  571. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  572. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  573. 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
  574. EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
  575. EESIPR_CEEFIP | EESIPR_CELFIP |
  576. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  577. EESIPR_PREIP | EESIPR_CERFIP,
  578. .tx_check = EESR_TC1 | EESR_FTC,
  579. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  580. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  581. EESR_TDE,
  582. .fdr_value = 0x0000070f,
  583. .apr = 1,
  584. .mpr = 1,
  585. .tpauser = 1,
  586. .bculr = 1,
  587. .hw_swap = 1,
  588. .rpadir = 1,
  589. .no_trimd = 1,
  590. .no_ade = 1,
  591. .xdfar_rw = 1,
  592. .hw_checksum = 1,
  593. .tsu = 1,
  594. .select_mii = 1,
  595. .magic = 1,
  596. .cexcr = 1,
  597. };
  598. /* There is CPU dependent code */
  599. static void sh_eth_set_rate_rcar(struct net_device *ndev)
  600. {
  601. struct sh_eth_private *mdp = netdev_priv(ndev);
  602. switch (mdp->speed) {
  603. case 10: /* 10BASE */
  604. sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
  605. break;
  606. case 100:/* 100BASE */
  607. sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
  608. break;
  609. }
  610. }
  611. /* R-Car Gen1 */
  612. static struct sh_eth_cpu_data rcar_gen1_data = {
  613. .soft_reset = sh_eth_soft_reset,
  614. .set_duplex = sh_eth_set_duplex,
  615. .set_rate = sh_eth_set_rate_rcar,
  616. .register_type = SH_ETH_REG_FAST_RCAR,
  617. .edtrr_trns = EDTRR_TRNS_ETHER,
  618. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  619. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  620. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
  621. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  622. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  623. EESIPR_RMAFIP | EESIPR_RRFIP |
  624. EESIPR_RTLFIP | EESIPR_RTSFIP |
  625. EESIPR_PREIP | EESIPR_CERFIP,
  626. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
  627. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  628. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  629. .fdr_value = 0x00000f0f,
  630. .apr = 1,
  631. .mpr = 1,
  632. .tpauser = 1,
  633. .hw_swap = 1,
  634. .no_xdfar = 1,
  635. };
  636. /* R-Car Gen2 and RZ/G1 */
  637. static struct sh_eth_cpu_data rcar_gen2_data = {
  638. .soft_reset = sh_eth_soft_reset,
  639. .set_duplex = sh_eth_set_duplex,
  640. .set_rate = sh_eth_set_rate_rcar,
  641. .register_type = SH_ETH_REG_FAST_RCAR,
  642. .edtrr_trns = EDTRR_TRNS_ETHER,
  643. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
  644. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
  645. ECSIPR_MPDIP,
  646. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
  647. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  648. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  649. EESIPR_RMAFIP | EESIPR_RRFIP |
  650. EESIPR_RTLFIP | EESIPR_RTSFIP |
  651. EESIPR_PREIP | EESIPR_CERFIP,
  652. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
  653. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  654. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  655. .fdr_value = 0x00000f0f,
  656. .trscer_err_mask = DESC_I_RINT8,
  657. .apr = 1,
  658. .mpr = 1,
  659. .tpauser = 1,
  660. .hw_swap = 1,
  661. .no_xdfar = 1,
  662. .rmiimode = 1,
  663. .magic = 1,
  664. };
  665. /* R8A77980 */
  666. static struct sh_eth_cpu_data r8a77980_data = {
  667. .soft_reset = sh_eth_soft_reset_gether,
  668. .set_duplex = sh_eth_set_duplex,
  669. .set_rate = sh_eth_set_rate_gether,
  670. .register_type = SH_ETH_REG_GIGABIT,
  671. .edtrr_trns = EDTRR_TRNS_GETHER,
  672. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
  673. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
  674. ECSIPR_MPDIP,
  675. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  676. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  677. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  678. EESIPR_RMAFIP | EESIPR_RRFIP |
  679. EESIPR_RTLFIP | EESIPR_RTSFIP |
  680. EESIPR_PREIP | EESIPR_CERFIP,
  681. .tx_check = EESR_FTC | EESR_CD | EESR_TRO,
  682. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  683. EESR_RFE | EESR_RDE | EESR_RFRMER |
  684. EESR_TFE | EESR_TDE | EESR_ECI,
  685. .fdr_value = 0x0000070f,
  686. .apr = 1,
  687. .mpr = 1,
  688. .tpauser = 1,
  689. .bculr = 1,
  690. .hw_swap = 1,
  691. .nbst = 1,
  692. .rpadir = 1,
  693. .no_trimd = 1,
  694. .no_ade = 1,
  695. .xdfar_rw = 1,
  696. .hw_checksum = 1,
  697. .select_mii = 1,
  698. .magic = 1,
  699. .cexcr = 1,
  700. };
  701. /* R7S9210 */
  702. static struct sh_eth_cpu_data r7s9210_data = {
  703. .soft_reset = sh_eth_soft_reset,
  704. .set_duplex = sh_eth_set_duplex,
  705. .set_rate = sh_eth_set_rate_rcar,
  706. .register_type = SH_ETH_REG_FAST_SH4,
  707. .edtrr_trns = EDTRR_TRNS_ETHER,
  708. .ecsr_value = ECSR_ICD,
  709. .ecsipr_value = ECSIPR_ICDIP,
  710. .eesipr_value = EESIPR_TWBIP | EESIPR_TABTIP | EESIPR_RABTIP |
  711. EESIPR_RFCOFIP | EESIPR_ECIIP | EESIPR_FTCIP |
  712. EESIPR_TDEIP | EESIPR_TFUFIP | EESIPR_FRIP |
  713. EESIPR_RDEIP | EESIPR_RFOFIP | EESIPR_CNDIP |
  714. EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
  715. EESIPR_RMAFIP | EESIPR_RRFIP | EESIPR_RTLFIP |
  716. EESIPR_RTSFIP | EESIPR_PREIP | EESIPR_CERFIP,
  717. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
  718. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  719. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  720. .fdr_value = 0x0000070f,
  721. .apr = 1,
  722. .mpr = 1,
  723. .tpauser = 1,
  724. .hw_swap = 1,
  725. .rpadir = 1,
  726. .no_ade = 1,
  727. .xdfar_rw = 1,
  728. };
  729. #endif /* CONFIG_OF */
  730. static void sh_eth_set_rate_sh7724(struct net_device *ndev)
  731. {
  732. struct sh_eth_private *mdp = netdev_priv(ndev);
  733. switch (mdp->speed) {
  734. case 10: /* 10BASE */
  735. sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
  736. break;
  737. case 100:/* 100BASE */
  738. sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
  739. break;
  740. }
  741. }
  742. /* SH7724 */
  743. static struct sh_eth_cpu_data sh7724_data = {
  744. .soft_reset = sh_eth_soft_reset,
  745. .set_duplex = sh_eth_set_duplex,
  746. .set_rate = sh_eth_set_rate_sh7724,
  747. .register_type = SH_ETH_REG_FAST_SH4,
  748. .edtrr_trns = EDTRR_TRNS_ETHER,
  749. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  750. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  751. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
  752. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  753. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  754. EESIPR_RMAFIP | EESIPR_RRFIP |
  755. EESIPR_RTLFIP | EESIPR_RTSFIP |
  756. EESIPR_PREIP | EESIPR_CERFIP,
  757. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
  758. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  759. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  760. .apr = 1,
  761. .mpr = 1,
  762. .tpauser = 1,
  763. .hw_swap = 1,
  764. .rpadir = 1,
  765. };
  766. static void sh_eth_set_rate_sh7757(struct net_device *ndev)
  767. {
  768. struct sh_eth_private *mdp = netdev_priv(ndev);
  769. switch (mdp->speed) {
  770. case 10: /* 10BASE */
  771. sh_eth_write(ndev, 0, RTRATE);
  772. break;
  773. case 100:/* 100BASE */
  774. sh_eth_write(ndev, 1, RTRATE);
  775. break;
  776. }
  777. }
  778. /* SH7757 */
  779. static struct sh_eth_cpu_data sh7757_data = {
  780. .soft_reset = sh_eth_soft_reset,
  781. .set_duplex = sh_eth_set_duplex,
  782. .set_rate = sh_eth_set_rate_sh7757,
  783. .register_type = SH_ETH_REG_FAST_SH4,
  784. .edtrr_trns = EDTRR_TRNS_ETHER,
  785. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  786. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  787. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  788. 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
  789. EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
  790. EESIPR_CEEFIP | EESIPR_CELFIP |
  791. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  792. EESIPR_PREIP | EESIPR_CERFIP,
  793. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
  794. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  795. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  796. .irq_flags = IRQF_SHARED,
  797. .apr = 1,
  798. .mpr = 1,
  799. .tpauser = 1,
  800. .hw_swap = 1,
  801. .no_ade = 1,
  802. .rpadir = 1,
  803. .rtrate = 1,
  804. .dual_port = 1,
  805. };
  806. #define SH_GIGA_ETH_BASE 0xfee00000UL
  807. #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
  808. #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
  809. static void sh_eth_chip_reset_giga(struct net_device *ndev)
  810. {
  811. u32 mahr[2], malr[2];
  812. int i;
  813. /* save MAHR and MALR */
  814. for (i = 0; i < 2; i++) {
  815. malr[i] = ioread32((void *)GIGA_MALR(i));
  816. mahr[i] = ioread32((void *)GIGA_MAHR(i));
  817. }
  818. sh_eth_chip_reset(ndev);
  819. /* restore MAHR and MALR */
  820. for (i = 0; i < 2; i++) {
  821. iowrite32(malr[i], (void *)GIGA_MALR(i));
  822. iowrite32(mahr[i], (void *)GIGA_MAHR(i));
  823. }
  824. }
  825. static void sh_eth_set_rate_giga(struct net_device *ndev)
  826. {
  827. struct sh_eth_private *mdp = netdev_priv(ndev);
  828. switch (mdp->speed) {
  829. case 10: /* 10BASE */
  830. sh_eth_write(ndev, 0x00000000, GECMR);
  831. break;
  832. case 100:/* 100BASE */
  833. sh_eth_write(ndev, 0x00000010, GECMR);
  834. break;
  835. case 1000: /* 1000BASE */
  836. sh_eth_write(ndev, 0x00000020, GECMR);
  837. break;
  838. }
  839. }
  840. /* SH7757(GETHERC) */
  841. static struct sh_eth_cpu_data sh7757_data_giga = {
  842. .soft_reset = sh_eth_soft_reset_gether,
  843. .chip_reset = sh_eth_chip_reset_giga,
  844. .set_duplex = sh_eth_set_duplex,
  845. .set_rate = sh_eth_set_rate_giga,
  846. .register_type = SH_ETH_REG_GIGABIT,
  847. .edtrr_trns = EDTRR_TRNS_GETHER,
  848. .ecsr_value = ECSR_ICD | ECSR_MPD,
  849. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  850. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  851. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  852. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  853. 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
  854. EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
  855. EESIPR_CEEFIP | EESIPR_CELFIP |
  856. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  857. EESIPR_PREIP | EESIPR_CERFIP,
  858. .tx_check = EESR_TC1 | EESR_FTC,
  859. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  860. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  861. EESR_TDE,
  862. .fdr_value = 0x0000072f,
  863. .irq_flags = IRQF_SHARED,
  864. .apr = 1,
  865. .mpr = 1,
  866. .tpauser = 1,
  867. .bculr = 1,
  868. .hw_swap = 1,
  869. .rpadir = 1,
  870. .no_trimd = 1,
  871. .no_ade = 1,
  872. .xdfar_rw = 1,
  873. .tsu = 1,
  874. .cexcr = 1,
  875. .dual_port = 1,
  876. };
  877. /* SH7734 */
  878. static struct sh_eth_cpu_data sh7734_data = {
  879. .soft_reset = sh_eth_soft_reset_gether,
  880. .chip_reset = sh_eth_chip_reset,
  881. .set_duplex = sh_eth_set_duplex,
  882. .set_rate = sh_eth_set_rate_gether,
  883. .register_type = SH_ETH_REG_GIGABIT,
  884. .edtrr_trns = EDTRR_TRNS_GETHER,
  885. .ecsr_value = ECSR_ICD | ECSR_MPD,
  886. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  887. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  888. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  889. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  890. EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
  891. EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
  892. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  893. EESIPR_PREIP | EESIPR_CERFIP,
  894. .tx_check = EESR_TC1 | EESR_FTC,
  895. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  896. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  897. EESR_TDE,
  898. .apr = 1,
  899. .mpr = 1,
  900. .tpauser = 1,
  901. .bculr = 1,
  902. .hw_swap = 1,
  903. .no_trimd = 1,
  904. .no_ade = 1,
  905. .xdfar_rw = 1,
  906. .tsu = 1,
  907. .hw_checksum = 1,
  908. .select_mii = 1,
  909. .magic = 1,
  910. .cexcr = 1,
  911. };
  912. /* SH7763 */
  913. static struct sh_eth_cpu_data sh7763_data = {
  914. .soft_reset = sh_eth_soft_reset_gether,
  915. .chip_reset = sh_eth_chip_reset,
  916. .set_duplex = sh_eth_set_duplex,
  917. .set_rate = sh_eth_set_rate_gether,
  918. .register_type = SH_ETH_REG_GIGABIT,
  919. .edtrr_trns = EDTRR_TRNS_GETHER,
  920. .ecsr_value = ECSR_ICD | ECSR_MPD,
  921. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  922. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  923. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  924. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  925. EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
  926. EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
  927. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  928. EESIPR_PREIP | EESIPR_CERFIP,
  929. .tx_check = EESR_TC1 | EESR_FTC,
  930. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  931. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
  932. .apr = 1,
  933. .mpr = 1,
  934. .tpauser = 1,
  935. .bculr = 1,
  936. .hw_swap = 1,
  937. .no_trimd = 1,
  938. .no_ade = 1,
  939. .xdfar_rw = 1,
  940. .tsu = 1,
  941. .irq_flags = IRQF_SHARED,
  942. .magic = 1,
  943. .cexcr = 1,
  944. .dual_port = 1,
  945. };
  946. static struct sh_eth_cpu_data sh7619_data = {
  947. .soft_reset = sh_eth_soft_reset,
  948. .register_type = SH_ETH_REG_FAST_SH3_SH2,
  949. .edtrr_trns = EDTRR_TRNS_ETHER,
  950. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  951. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  952. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  953. 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
  954. EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
  955. EESIPR_CEEFIP | EESIPR_CELFIP |
  956. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  957. EESIPR_PREIP | EESIPR_CERFIP,
  958. .apr = 1,
  959. .mpr = 1,
  960. .tpauser = 1,
  961. .hw_swap = 1,
  962. };
  963. static struct sh_eth_cpu_data sh771x_data = {
  964. .soft_reset = sh_eth_soft_reset,
  965. .register_type = SH_ETH_REG_FAST_SH3_SH2,
  966. .edtrr_trns = EDTRR_TRNS_ETHER,
  967. .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
  968. EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
  969. EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
  970. 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
  971. EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
  972. EESIPR_CEEFIP | EESIPR_CELFIP |
  973. EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
  974. EESIPR_PREIP | EESIPR_CERFIP,
  975. .tsu = 1,
  976. .dual_port = 1,
  977. };
  978. static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
  979. {
  980. if (!cd->ecsr_value)
  981. cd->ecsr_value = DEFAULT_ECSR_INIT;
  982. if (!cd->ecsipr_value)
  983. cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
  984. if (!cd->fcftr_value)
  985. cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
  986. DEFAULT_FIFO_F_D_RFD;
  987. if (!cd->fdr_value)
  988. cd->fdr_value = DEFAULT_FDR_INIT;
  989. if (!cd->tx_check)
  990. cd->tx_check = DEFAULT_TX_CHECK;
  991. if (!cd->eesr_err_check)
  992. cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
  993. if (!cd->trscer_err_mask)
  994. cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
  995. }
  996. static void sh_eth_set_receive_align(struct sk_buff *skb)
  997. {
  998. uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
  999. if (reserve)
  1000. skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
  1001. }
  1002. /* Program the hardware MAC address from dev->dev_addr. */
  1003. static void update_mac_address(struct net_device *ndev)
  1004. {
  1005. sh_eth_write(ndev,
  1006. (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
  1007. (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
  1008. sh_eth_write(ndev,
  1009. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
  1010. }
  1011. /* Get MAC address from SuperH MAC address register
  1012. *
  1013. * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
  1014. * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
  1015. * When you want use this device, you must set MAC address in bootloader.
  1016. *
  1017. */
  1018. static void read_mac_address(struct net_device *ndev, unsigned char *mac)
  1019. {
  1020. if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
  1021. memcpy(ndev->dev_addr, mac, ETH_ALEN);
  1022. } else {
  1023. u32 mahr = sh_eth_read(ndev, MAHR);
  1024. u32 malr = sh_eth_read(ndev, MALR);
  1025. ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
  1026. ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
  1027. ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
  1028. ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
  1029. ndev->dev_addr[4] = (malr >> 8) & 0xFF;
  1030. ndev->dev_addr[5] = (malr >> 0) & 0xFF;
  1031. }
  1032. }
  1033. struct bb_info {
  1034. void (*set_gate)(void *addr);
  1035. struct mdiobb_ctrl ctrl;
  1036. void *addr;
  1037. };
  1038. static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
  1039. {
  1040. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  1041. u32 pir;
  1042. if (bitbang->set_gate)
  1043. bitbang->set_gate(bitbang->addr);
  1044. pir = ioread32(bitbang->addr);
  1045. if (set)
  1046. pir |= mask;
  1047. else
  1048. pir &= ~mask;
  1049. iowrite32(pir, bitbang->addr);
  1050. }
  1051. /* Data I/O pin control */
  1052. static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  1053. {
  1054. sh_mdio_ctrl(ctrl, PIR_MMD, bit);
  1055. }
  1056. /* Set bit data*/
  1057. static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
  1058. {
  1059. sh_mdio_ctrl(ctrl, PIR_MDO, bit);
  1060. }
  1061. /* Get bit data*/
  1062. static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
  1063. {
  1064. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  1065. if (bitbang->set_gate)
  1066. bitbang->set_gate(bitbang->addr);
  1067. return (ioread32(bitbang->addr) & PIR_MDI) != 0;
  1068. }
  1069. /* MDC pin control */
  1070. static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  1071. {
  1072. sh_mdio_ctrl(ctrl, PIR_MDC, bit);
  1073. }
  1074. /* mdio bus control struct */
  1075. static struct mdiobb_ops bb_ops = {
  1076. .owner = THIS_MODULE,
  1077. .set_mdc = sh_mdc_ctrl,
  1078. .set_mdio_dir = sh_mmd_ctrl,
  1079. .set_mdio_data = sh_set_mdio,
  1080. .get_mdio_data = sh_get_mdio,
  1081. };
  1082. /* free Tx skb function */
  1083. static int sh_eth_tx_free(struct net_device *ndev, bool sent_only)
  1084. {
  1085. struct sh_eth_private *mdp = netdev_priv(ndev);
  1086. struct sh_eth_txdesc *txdesc;
  1087. int free_num = 0;
  1088. int entry;
  1089. bool sent;
  1090. for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
  1091. entry = mdp->dirty_tx % mdp->num_tx_ring;
  1092. txdesc = &mdp->tx_ring[entry];
  1093. sent = !(txdesc->status & cpu_to_le32(TD_TACT));
  1094. if (sent_only && !sent)
  1095. break;
  1096. /* TACT bit must be checked before all the following reads */
  1097. dma_rmb();
  1098. netif_info(mdp, tx_done, ndev,
  1099. "tx entry %d status 0x%08x\n",
  1100. entry, le32_to_cpu(txdesc->status));
  1101. /* Free the original skb. */
  1102. if (mdp->tx_skbuff[entry]) {
  1103. dma_unmap_single(&mdp->pdev->dev,
  1104. le32_to_cpu(txdesc->addr),
  1105. le32_to_cpu(txdesc->len) >> 16,
  1106. DMA_TO_DEVICE);
  1107. dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
  1108. mdp->tx_skbuff[entry] = NULL;
  1109. free_num++;
  1110. }
  1111. txdesc->status = cpu_to_le32(TD_TFP);
  1112. if (entry >= mdp->num_tx_ring - 1)
  1113. txdesc->status |= cpu_to_le32(TD_TDLE);
  1114. if (sent) {
  1115. ndev->stats.tx_packets++;
  1116. ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
  1117. }
  1118. }
  1119. return free_num;
  1120. }
  1121. /* free skb and descriptor buffer */
  1122. static void sh_eth_ring_free(struct net_device *ndev)
  1123. {
  1124. struct sh_eth_private *mdp = netdev_priv(ndev);
  1125. int ringsize, i;
  1126. if (mdp->rx_ring) {
  1127. for (i = 0; i < mdp->num_rx_ring; i++) {
  1128. if (mdp->rx_skbuff[i]) {
  1129. struct sh_eth_rxdesc *rxdesc = &mdp->rx_ring[i];
  1130. dma_unmap_single(&mdp->pdev->dev,
  1131. le32_to_cpu(rxdesc->addr),
  1132. ALIGN(mdp->rx_buf_sz, 32),
  1133. DMA_FROM_DEVICE);
  1134. }
  1135. }
  1136. ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  1137. dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->rx_ring,
  1138. mdp->rx_desc_dma);
  1139. mdp->rx_ring = NULL;
  1140. }
  1141. /* Free Rx skb ringbuffer */
  1142. if (mdp->rx_skbuff) {
  1143. for (i = 0; i < mdp->num_rx_ring; i++)
  1144. dev_kfree_skb(mdp->rx_skbuff[i]);
  1145. }
  1146. kfree(mdp->rx_skbuff);
  1147. mdp->rx_skbuff = NULL;
  1148. if (mdp->tx_ring) {
  1149. sh_eth_tx_free(ndev, false);
  1150. ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  1151. dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->tx_ring,
  1152. mdp->tx_desc_dma);
  1153. mdp->tx_ring = NULL;
  1154. }
  1155. /* Free Tx skb ringbuffer */
  1156. kfree(mdp->tx_skbuff);
  1157. mdp->tx_skbuff = NULL;
  1158. }
  1159. /* format skb and descriptor buffer */
  1160. static void sh_eth_ring_format(struct net_device *ndev)
  1161. {
  1162. struct sh_eth_private *mdp = netdev_priv(ndev);
  1163. int i;
  1164. struct sk_buff *skb;
  1165. struct sh_eth_rxdesc *rxdesc = NULL;
  1166. struct sh_eth_txdesc *txdesc = NULL;
  1167. int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
  1168. int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
  1169. int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
  1170. dma_addr_t dma_addr;
  1171. u32 buf_len;
  1172. mdp->cur_rx = 0;
  1173. mdp->cur_tx = 0;
  1174. mdp->dirty_rx = 0;
  1175. mdp->dirty_tx = 0;
  1176. memset(mdp->rx_ring, 0, rx_ringsize);
  1177. /* build Rx ring buffer */
  1178. for (i = 0; i < mdp->num_rx_ring; i++) {
  1179. /* skb */
  1180. mdp->rx_skbuff[i] = NULL;
  1181. skb = netdev_alloc_skb(ndev, skbuff_size);
  1182. if (skb == NULL)
  1183. break;
  1184. sh_eth_set_receive_align(skb);
  1185. /* The size of the buffer is a multiple of 32 bytes. */
  1186. buf_len = ALIGN(mdp->rx_buf_sz, 32);
  1187. dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, buf_len,
  1188. DMA_FROM_DEVICE);
  1189. if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
  1190. kfree_skb(skb);
  1191. break;
  1192. }
  1193. mdp->rx_skbuff[i] = skb;
  1194. /* RX descriptor */
  1195. rxdesc = &mdp->rx_ring[i];
  1196. rxdesc->len = cpu_to_le32(buf_len << 16);
  1197. rxdesc->addr = cpu_to_le32(dma_addr);
  1198. rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
  1199. /* Rx descriptor address set */
  1200. if (i == 0) {
  1201. sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
  1202. if (mdp->cd->xdfar_rw)
  1203. sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
  1204. }
  1205. }
  1206. mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
  1207. /* Mark the last entry as wrapping the ring. */
  1208. if (rxdesc)
  1209. rxdesc->status |= cpu_to_le32(RD_RDLE);
  1210. memset(mdp->tx_ring, 0, tx_ringsize);
  1211. /* build Tx ring buffer */
  1212. for (i = 0; i < mdp->num_tx_ring; i++) {
  1213. mdp->tx_skbuff[i] = NULL;
  1214. txdesc = &mdp->tx_ring[i];
  1215. txdesc->status = cpu_to_le32(TD_TFP);
  1216. txdesc->len = cpu_to_le32(0);
  1217. if (i == 0) {
  1218. /* Tx descriptor address set */
  1219. sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
  1220. if (mdp->cd->xdfar_rw)
  1221. sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
  1222. }
  1223. }
  1224. txdesc->status |= cpu_to_le32(TD_TDLE);
  1225. }
  1226. /* Get skb and descriptor buffer */
  1227. static int sh_eth_ring_init(struct net_device *ndev)
  1228. {
  1229. struct sh_eth_private *mdp = netdev_priv(ndev);
  1230. int rx_ringsize, tx_ringsize;
  1231. /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  1232. * card needs room to do 8 byte alignment, +2 so we can reserve
  1233. * the first 2 bytes, and +16 gets room for the status word from the
  1234. * card.
  1235. */
  1236. mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
  1237. (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
  1238. if (mdp->cd->rpadir)
  1239. mdp->rx_buf_sz += NET_IP_ALIGN;
  1240. /* Allocate RX and TX skb rings */
  1241. mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
  1242. GFP_KERNEL);
  1243. if (!mdp->rx_skbuff)
  1244. return -ENOMEM;
  1245. mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
  1246. GFP_KERNEL);
  1247. if (!mdp->tx_skbuff)
  1248. goto ring_free;
  1249. /* Allocate all Rx descriptors. */
  1250. rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  1251. mdp->rx_ring = dma_alloc_coherent(&mdp->pdev->dev, rx_ringsize,
  1252. &mdp->rx_desc_dma, GFP_KERNEL);
  1253. if (!mdp->rx_ring)
  1254. goto ring_free;
  1255. mdp->dirty_rx = 0;
  1256. /* Allocate all Tx descriptors. */
  1257. tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  1258. mdp->tx_ring = dma_alloc_coherent(&mdp->pdev->dev, tx_ringsize,
  1259. &mdp->tx_desc_dma, GFP_KERNEL);
  1260. if (!mdp->tx_ring)
  1261. goto ring_free;
  1262. return 0;
  1263. ring_free:
  1264. /* Free Rx and Tx skb ring buffer and DMA buffer */
  1265. sh_eth_ring_free(ndev);
  1266. return -ENOMEM;
  1267. }
  1268. static int sh_eth_dev_init(struct net_device *ndev)
  1269. {
  1270. struct sh_eth_private *mdp = netdev_priv(ndev);
  1271. int ret;
  1272. /* Soft Reset */
  1273. ret = mdp->cd->soft_reset(ndev);
  1274. if (ret)
  1275. return ret;
  1276. if (mdp->cd->rmiimode)
  1277. sh_eth_write(ndev, 0x1, RMIIMODE);
  1278. /* Descriptor format */
  1279. sh_eth_ring_format(ndev);
  1280. if (mdp->cd->rpadir)
  1281. sh_eth_write(ndev, NET_IP_ALIGN << 16, RPADIR);
  1282. /* all sh_eth int mask */
  1283. sh_eth_write(ndev, 0, EESIPR);
  1284. #if defined(__LITTLE_ENDIAN)
  1285. if (mdp->cd->hw_swap)
  1286. sh_eth_write(ndev, EDMR_EL, EDMR);
  1287. else
  1288. #endif
  1289. sh_eth_write(ndev, 0, EDMR);
  1290. /* FIFO size set */
  1291. sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
  1292. sh_eth_write(ndev, 0, TFTR);
  1293. /* Frame recv control (enable multiple-packets per rx irq) */
  1294. sh_eth_write(ndev, RMCR_RNC, RMCR);
  1295. sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
  1296. /* DMA transfer burst mode */
  1297. if (mdp->cd->nbst)
  1298. sh_eth_modify(ndev, EDMR, EDMR_NBST, EDMR_NBST);
  1299. /* Burst cycle count upper-limit */
  1300. if (mdp->cd->bculr)
  1301. sh_eth_write(ndev, 0x800, BCULR);
  1302. sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
  1303. if (!mdp->cd->no_trimd)
  1304. sh_eth_write(ndev, 0, TRIMD);
  1305. /* Recv frame limit set register */
  1306. sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  1307. RFLR);
  1308. sh_eth_modify(ndev, EESR, 0, 0);
  1309. mdp->irq_enabled = true;
  1310. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1311. /* PAUSE Prohibition */
  1312. sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
  1313. ECMR_TE | ECMR_RE, ECMR);
  1314. if (mdp->cd->set_rate)
  1315. mdp->cd->set_rate(ndev);
  1316. /* E-MAC Status Register clear */
  1317. sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
  1318. /* E-MAC Interrupt Enable register */
  1319. sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
  1320. /* Set MAC address */
  1321. update_mac_address(ndev);
  1322. /* mask reset */
  1323. if (mdp->cd->apr)
  1324. sh_eth_write(ndev, 1, APR);
  1325. if (mdp->cd->mpr)
  1326. sh_eth_write(ndev, 1, MPR);
  1327. if (mdp->cd->tpauser)
  1328. sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
  1329. /* Setting the Rx mode will start the Rx process. */
  1330. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1331. return ret;
  1332. }
  1333. static void sh_eth_dev_exit(struct net_device *ndev)
  1334. {
  1335. struct sh_eth_private *mdp = netdev_priv(ndev);
  1336. int i;
  1337. /* Deactivate all TX descriptors, so DMA should stop at next
  1338. * packet boundary if it's currently running
  1339. */
  1340. for (i = 0; i < mdp->num_tx_ring; i++)
  1341. mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
  1342. /* Disable TX FIFO egress to MAC */
  1343. sh_eth_rcv_snd_disable(ndev);
  1344. /* Stop RX DMA at next packet boundary */
  1345. sh_eth_write(ndev, 0, EDRRR);
  1346. /* Aside from TX DMA, we can't tell when the hardware is
  1347. * really stopped, so we need to reset to make sure.
  1348. * Before doing that, wait for long enough to *probably*
  1349. * finish transmitting the last packet and poll stats.
  1350. */
  1351. msleep(2); /* max frame time at 10 Mbps < 1250 us */
  1352. sh_eth_get_stats(ndev);
  1353. mdp->cd->soft_reset(ndev);
  1354. /* Set the RMII mode again if required */
  1355. if (mdp->cd->rmiimode)
  1356. sh_eth_write(ndev, 0x1, RMIIMODE);
  1357. /* Set MAC address again */
  1358. update_mac_address(ndev);
  1359. }
  1360. /* Packet receive function */
  1361. static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
  1362. {
  1363. struct sh_eth_private *mdp = netdev_priv(ndev);
  1364. struct sh_eth_rxdesc *rxdesc;
  1365. int entry = mdp->cur_rx % mdp->num_rx_ring;
  1366. int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
  1367. int limit;
  1368. struct sk_buff *skb;
  1369. u32 desc_status;
  1370. int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
  1371. dma_addr_t dma_addr;
  1372. u16 pkt_len;
  1373. u32 buf_len;
  1374. boguscnt = min(boguscnt, *quota);
  1375. limit = boguscnt;
  1376. rxdesc = &mdp->rx_ring[entry];
  1377. while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
  1378. /* RACT bit must be checked before all the following reads */
  1379. dma_rmb();
  1380. desc_status = le32_to_cpu(rxdesc->status);
  1381. pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
  1382. if (--boguscnt < 0)
  1383. break;
  1384. netif_info(mdp, rx_status, ndev,
  1385. "rx entry %d status 0x%08x len %d\n",
  1386. entry, desc_status, pkt_len);
  1387. if (!(desc_status & RDFEND))
  1388. ndev->stats.rx_length_errors++;
  1389. /* In case of almost all GETHER/ETHERs, the Receive Frame State
  1390. * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
  1391. * bit 0. However, in case of the R8A7740 and R7S72100
  1392. * the RFS bits are from bit 25 to bit 16. So, the
  1393. * driver needs right shifting by 16.
  1394. */
  1395. if (mdp->cd->hw_checksum)
  1396. desc_status >>= 16;
  1397. skb = mdp->rx_skbuff[entry];
  1398. if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
  1399. RD_RFS5 | RD_RFS6 | RD_RFS10)) {
  1400. ndev->stats.rx_errors++;
  1401. if (desc_status & RD_RFS1)
  1402. ndev->stats.rx_crc_errors++;
  1403. if (desc_status & RD_RFS2)
  1404. ndev->stats.rx_frame_errors++;
  1405. if (desc_status & RD_RFS3)
  1406. ndev->stats.rx_length_errors++;
  1407. if (desc_status & RD_RFS4)
  1408. ndev->stats.rx_length_errors++;
  1409. if (desc_status & RD_RFS6)
  1410. ndev->stats.rx_missed_errors++;
  1411. if (desc_status & RD_RFS10)
  1412. ndev->stats.rx_over_errors++;
  1413. } else if (skb) {
  1414. dma_addr = le32_to_cpu(rxdesc->addr);
  1415. if (!mdp->cd->hw_swap)
  1416. sh_eth_soft_swap(
  1417. phys_to_virt(ALIGN(dma_addr, 4)),
  1418. pkt_len + 2);
  1419. mdp->rx_skbuff[entry] = NULL;
  1420. if (mdp->cd->rpadir)
  1421. skb_reserve(skb, NET_IP_ALIGN);
  1422. dma_unmap_single(&mdp->pdev->dev, dma_addr,
  1423. ALIGN(mdp->rx_buf_sz, 32),
  1424. DMA_FROM_DEVICE);
  1425. skb_put(skb, pkt_len);
  1426. skb->protocol = eth_type_trans(skb, ndev);
  1427. netif_receive_skb(skb);
  1428. ndev->stats.rx_packets++;
  1429. ndev->stats.rx_bytes += pkt_len;
  1430. if (desc_status & RD_RFS8)
  1431. ndev->stats.multicast++;
  1432. }
  1433. entry = (++mdp->cur_rx) % mdp->num_rx_ring;
  1434. rxdesc = &mdp->rx_ring[entry];
  1435. }
  1436. /* Refill the Rx ring buffers. */
  1437. for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
  1438. entry = mdp->dirty_rx % mdp->num_rx_ring;
  1439. rxdesc = &mdp->rx_ring[entry];
  1440. /* The size of the buffer is 32 byte boundary. */
  1441. buf_len = ALIGN(mdp->rx_buf_sz, 32);
  1442. rxdesc->len = cpu_to_le32(buf_len << 16);
  1443. if (mdp->rx_skbuff[entry] == NULL) {
  1444. skb = netdev_alloc_skb(ndev, skbuff_size);
  1445. if (skb == NULL)
  1446. break; /* Better luck next round. */
  1447. sh_eth_set_receive_align(skb);
  1448. dma_addr = dma_map_single(&mdp->pdev->dev, skb->data,
  1449. buf_len, DMA_FROM_DEVICE);
  1450. if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
  1451. kfree_skb(skb);
  1452. break;
  1453. }
  1454. mdp->rx_skbuff[entry] = skb;
  1455. skb_checksum_none_assert(skb);
  1456. rxdesc->addr = cpu_to_le32(dma_addr);
  1457. }
  1458. dma_wmb(); /* RACT bit must be set after all the above writes */
  1459. if (entry >= mdp->num_rx_ring - 1)
  1460. rxdesc->status |=
  1461. cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
  1462. else
  1463. rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
  1464. }
  1465. /* Restart Rx engine if stopped. */
  1466. /* If we don't need to check status, don't. -KDU */
  1467. if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
  1468. /* fix the values for the next receiving if RDE is set */
  1469. if (intr_status & EESR_RDE && !mdp->cd->no_xdfar) {
  1470. u32 count = (sh_eth_read(ndev, RDFAR) -
  1471. sh_eth_read(ndev, RDLAR)) >> 4;
  1472. mdp->cur_rx = count;
  1473. mdp->dirty_rx = count;
  1474. }
  1475. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1476. }
  1477. *quota -= limit - boguscnt - 1;
  1478. return *quota <= 0;
  1479. }
  1480. static void sh_eth_rcv_snd_disable(struct net_device *ndev)
  1481. {
  1482. /* disable tx and rx */
  1483. sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
  1484. }
  1485. static void sh_eth_rcv_snd_enable(struct net_device *ndev)
  1486. {
  1487. /* enable tx and rx */
  1488. sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
  1489. }
  1490. /* E-MAC interrupt handler */
  1491. static void sh_eth_emac_interrupt(struct net_device *ndev)
  1492. {
  1493. struct sh_eth_private *mdp = netdev_priv(ndev);
  1494. u32 felic_stat;
  1495. u32 link_stat;
  1496. felic_stat = sh_eth_read(ndev, ECSR) & sh_eth_read(ndev, ECSIPR);
  1497. sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
  1498. if (felic_stat & ECSR_ICD)
  1499. ndev->stats.tx_carrier_errors++;
  1500. if (felic_stat & ECSR_MPD)
  1501. pm_wakeup_event(&mdp->pdev->dev, 0);
  1502. if (felic_stat & ECSR_LCHNG) {
  1503. /* Link Changed */
  1504. if (mdp->cd->no_psr || mdp->no_ether_link)
  1505. return;
  1506. link_stat = sh_eth_read(ndev, PSR);
  1507. if (mdp->ether_link_active_low)
  1508. link_stat = ~link_stat;
  1509. if (!(link_stat & PHY_ST_LINK)) {
  1510. sh_eth_rcv_snd_disable(ndev);
  1511. } else {
  1512. /* Link Up */
  1513. sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, 0);
  1514. /* clear int */
  1515. sh_eth_modify(ndev, ECSR, 0, 0);
  1516. sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, EESIPR_ECIIP);
  1517. /* enable tx and rx */
  1518. sh_eth_rcv_snd_enable(ndev);
  1519. }
  1520. }
  1521. }
  1522. /* error control function */
  1523. static void sh_eth_error(struct net_device *ndev, u32 intr_status)
  1524. {
  1525. struct sh_eth_private *mdp = netdev_priv(ndev);
  1526. u32 mask;
  1527. if (intr_status & EESR_TWB) {
  1528. /* Unused write back interrupt */
  1529. if (intr_status & EESR_TABT) { /* Transmit Abort int */
  1530. ndev->stats.tx_aborted_errors++;
  1531. netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
  1532. }
  1533. }
  1534. if (intr_status & EESR_RABT) {
  1535. /* Receive Abort int */
  1536. if (intr_status & EESR_RFRMER) {
  1537. /* Receive Frame Overflow int */
  1538. ndev->stats.rx_frame_errors++;
  1539. }
  1540. }
  1541. if (intr_status & EESR_TDE) {
  1542. /* Transmit Descriptor Empty int */
  1543. ndev->stats.tx_fifo_errors++;
  1544. netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
  1545. }
  1546. if (intr_status & EESR_TFE) {
  1547. /* FIFO under flow */
  1548. ndev->stats.tx_fifo_errors++;
  1549. netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
  1550. }
  1551. if (intr_status & EESR_RDE) {
  1552. /* Receive Descriptor Empty int */
  1553. ndev->stats.rx_over_errors++;
  1554. }
  1555. if (intr_status & EESR_RFE) {
  1556. /* Receive FIFO Overflow int */
  1557. ndev->stats.rx_fifo_errors++;
  1558. }
  1559. if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
  1560. /* Address Error */
  1561. ndev->stats.tx_fifo_errors++;
  1562. netif_err(mdp, tx_err, ndev, "Address Error\n");
  1563. }
  1564. mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
  1565. if (mdp->cd->no_ade)
  1566. mask &= ~EESR_ADE;
  1567. if (intr_status & mask) {
  1568. /* Tx error */
  1569. u32 edtrr = sh_eth_read(ndev, EDTRR);
  1570. /* dmesg */
  1571. netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
  1572. intr_status, mdp->cur_tx, mdp->dirty_tx,
  1573. (u32)ndev->state, edtrr);
  1574. /* dirty buffer free */
  1575. sh_eth_tx_free(ndev, true);
  1576. /* SH7712 BUG */
  1577. if (edtrr ^ mdp->cd->edtrr_trns) {
  1578. /* tx dma start */
  1579. sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
  1580. }
  1581. /* wakeup */
  1582. netif_wake_queue(ndev);
  1583. }
  1584. }
  1585. static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
  1586. {
  1587. struct net_device *ndev = netdev;
  1588. struct sh_eth_private *mdp = netdev_priv(ndev);
  1589. struct sh_eth_cpu_data *cd = mdp->cd;
  1590. irqreturn_t ret = IRQ_NONE;
  1591. u32 intr_status, intr_enable;
  1592. spin_lock(&mdp->lock);
  1593. /* Get interrupt status */
  1594. intr_status = sh_eth_read(ndev, EESR);
  1595. /* Mask it with the interrupt mask, forcing ECI interrupt to be always
  1596. * enabled since it's the one that comes thru regardless of the mask,
  1597. * and we need to fully handle it in sh_eth_emac_interrupt() in order
  1598. * to quench it as it doesn't get cleared by just writing 1 to the ECI
  1599. * bit...
  1600. */
  1601. intr_enable = sh_eth_read(ndev, EESIPR);
  1602. intr_status &= intr_enable | EESIPR_ECIIP;
  1603. if (intr_status & (EESR_RX_CHECK | cd->tx_check | EESR_ECI |
  1604. cd->eesr_err_check))
  1605. ret = IRQ_HANDLED;
  1606. else
  1607. goto out;
  1608. if (unlikely(!mdp->irq_enabled)) {
  1609. sh_eth_write(ndev, 0, EESIPR);
  1610. goto out;
  1611. }
  1612. if (intr_status & EESR_RX_CHECK) {
  1613. if (napi_schedule_prep(&mdp->napi)) {
  1614. /* Mask Rx interrupts */
  1615. sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
  1616. EESIPR);
  1617. __napi_schedule(&mdp->napi);
  1618. } else {
  1619. netdev_warn(ndev,
  1620. "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
  1621. intr_status, intr_enable);
  1622. }
  1623. }
  1624. /* Tx Check */
  1625. if (intr_status & cd->tx_check) {
  1626. /* Clear Tx interrupts */
  1627. sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
  1628. sh_eth_tx_free(ndev, true);
  1629. netif_wake_queue(ndev);
  1630. }
  1631. /* E-MAC interrupt */
  1632. if (intr_status & EESR_ECI)
  1633. sh_eth_emac_interrupt(ndev);
  1634. if (intr_status & cd->eesr_err_check) {
  1635. /* Clear error interrupts */
  1636. sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
  1637. sh_eth_error(ndev, intr_status);
  1638. }
  1639. out:
  1640. spin_unlock(&mdp->lock);
  1641. return ret;
  1642. }
  1643. static int sh_eth_poll(struct napi_struct *napi, int budget)
  1644. {
  1645. struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
  1646. napi);
  1647. struct net_device *ndev = napi->dev;
  1648. int quota = budget;
  1649. u32 intr_status;
  1650. for (;;) {
  1651. intr_status = sh_eth_read(ndev, EESR);
  1652. if (!(intr_status & EESR_RX_CHECK))
  1653. break;
  1654. /* Clear Rx interrupts */
  1655. sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
  1656. if (sh_eth_rx(ndev, intr_status, &quota))
  1657. goto out;
  1658. }
  1659. napi_complete(napi);
  1660. /* Reenable Rx interrupts */
  1661. if (mdp->irq_enabled)
  1662. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1663. out:
  1664. return budget - quota;
  1665. }
  1666. /* PHY state control function */
  1667. static void sh_eth_adjust_link(struct net_device *ndev)
  1668. {
  1669. struct sh_eth_private *mdp = netdev_priv(ndev);
  1670. struct phy_device *phydev = ndev->phydev;
  1671. unsigned long flags;
  1672. int new_state = 0;
  1673. spin_lock_irqsave(&mdp->lock, flags);
  1674. /* Disable TX and RX right over here, if E-MAC change is ignored */
  1675. if (mdp->cd->no_psr || mdp->no_ether_link)
  1676. sh_eth_rcv_snd_disable(ndev);
  1677. if (phydev->link) {
  1678. if (phydev->duplex != mdp->duplex) {
  1679. new_state = 1;
  1680. mdp->duplex = phydev->duplex;
  1681. if (mdp->cd->set_duplex)
  1682. mdp->cd->set_duplex(ndev);
  1683. }
  1684. if (phydev->speed != mdp->speed) {
  1685. new_state = 1;
  1686. mdp->speed = phydev->speed;
  1687. if (mdp->cd->set_rate)
  1688. mdp->cd->set_rate(ndev);
  1689. }
  1690. if (!mdp->link) {
  1691. sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
  1692. new_state = 1;
  1693. mdp->link = phydev->link;
  1694. }
  1695. } else if (mdp->link) {
  1696. new_state = 1;
  1697. mdp->link = 0;
  1698. mdp->speed = 0;
  1699. mdp->duplex = -1;
  1700. }
  1701. /* Enable TX and RX right over here, if E-MAC change is ignored */
  1702. if ((mdp->cd->no_psr || mdp->no_ether_link) && phydev->link)
  1703. sh_eth_rcv_snd_enable(ndev);
  1704. mmiowb();
  1705. spin_unlock_irqrestore(&mdp->lock, flags);
  1706. if (new_state && netif_msg_link(mdp))
  1707. phy_print_status(phydev);
  1708. }
  1709. /* PHY init function */
  1710. static int sh_eth_phy_init(struct net_device *ndev)
  1711. {
  1712. struct device_node *np = ndev->dev.parent->of_node;
  1713. struct sh_eth_private *mdp = netdev_priv(ndev);
  1714. struct phy_device *phydev;
  1715. mdp->link = 0;
  1716. mdp->speed = 0;
  1717. mdp->duplex = -1;
  1718. /* Try connect to PHY */
  1719. if (np) {
  1720. struct device_node *pn;
  1721. pn = of_parse_phandle(np, "phy-handle", 0);
  1722. phydev = of_phy_connect(ndev, pn,
  1723. sh_eth_adjust_link, 0,
  1724. mdp->phy_interface);
  1725. of_node_put(pn);
  1726. if (!phydev)
  1727. phydev = ERR_PTR(-ENOENT);
  1728. } else {
  1729. char phy_id[MII_BUS_ID_SIZE + 3];
  1730. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  1731. mdp->mii_bus->id, mdp->phy_id);
  1732. phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
  1733. mdp->phy_interface);
  1734. }
  1735. if (IS_ERR(phydev)) {
  1736. netdev_err(ndev, "failed to connect PHY\n");
  1737. return PTR_ERR(phydev);
  1738. }
  1739. /* mask with MAC supported features */
  1740. if (mdp->cd->register_type != SH_ETH_REG_GIGABIT) {
  1741. int err = phy_set_max_speed(phydev, SPEED_100);
  1742. if (err) {
  1743. netdev_err(ndev, "failed to limit PHY to 100 Mbit/s\n");
  1744. phy_disconnect(phydev);
  1745. return err;
  1746. }
  1747. }
  1748. phy_attached_info(phydev);
  1749. return 0;
  1750. }
  1751. /* PHY control start function */
  1752. static int sh_eth_phy_start(struct net_device *ndev)
  1753. {
  1754. int ret;
  1755. ret = sh_eth_phy_init(ndev);
  1756. if (ret)
  1757. return ret;
  1758. phy_start(ndev->phydev);
  1759. return 0;
  1760. }
  1761. /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
  1762. * version must be bumped as well. Just adding registers up to that
  1763. * limit is fine, as long as the existing register indices don't
  1764. * change.
  1765. */
  1766. #define SH_ETH_REG_DUMP_VERSION 1
  1767. #define SH_ETH_REG_DUMP_MAX_REGS 256
  1768. static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
  1769. {
  1770. struct sh_eth_private *mdp = netdev_priv(ndev);
  1771. struct sh_eth_cpu_data *cd = mdp->cd;
  1772. u32 *valid_map;
  1773. size_t len;
  1774. BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
  1775. /* Dump starts with a bitmap that tells ethtool which
  1776. * registers are defined for this chip.
  1777. */
  1778. len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
  1779. if (buf) {
  1780. valid_map = buf;
  1781. buf += len;
  1782. } else {
  1783. valid_map = NULL;
  1784. }
  1785. /* Add a register to the dump, if it has a defined offset.
  1786. * This automatically skips most undefined registers, but for
  1787. * some it is also necessary to check a capability flag in
  1788. * struct sh_eth_cpu_data.
  1789. */
  1790. #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
  1791. #define add_reg_from(reg, read_expr) do { \
  1792. if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
  1793. if (buf) { \
  1794. mark_reg_valid(reg); \
  1795. *buf++ = read_expr; \
  1796. } \
  1797. ++len; \
  1798. } \
  1799. } while (0)
  1800. #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
  1801. #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
  1802. add_reg(EDSR);
  1803. add_reg(EDMR);
  1804. add_reg(EDTRR);
  1805. add_reg(EDRRR);
  1806. add_reg(EESR);
  1807. add_reg(EESIPR);
  1808. add_reg(TDLAR);
  1809. add_reg(TDFAR);
  1810. add_reg(TDFXR);
  1811. add_reg(TDFFR);
  1812. add_reg(RDLAR);
  1813. add_reg(RDFAR);
  1814. add_reg(RDFXR);
  1815. add_reg(RDFFR);
  1816. add_reg(TRSCER);
  1817. add_reg(RMFCR);
  1818. add_reg(TFTR);
  1819. add_reg(FDR);
  1820. add_reg(RMCR);
  1821. add_reg(TFUCR);
  1822. add_reg(RFOCR);
  1823. if (cd->rmiimode)
  1824. add_reg(RMIIMODE);
  1825. add_reg(FCFTR);
  1826. if (cd->rpadir)
  1827. add_reg(RPADIR);
  1828. if (!cd->no_trimd)
  1829. add_reg(TRIMD);
  1830. add_reg(ECMR);
  1831. add_reg(ECSR);
  1832. add_reg(ECSIPR);
  1833. add_reg(PIR);
  1834. if (!cd->no_psr)
  1835. add_reg(PSR);
  1836. add_reg(RDMLR);
  1837. add_reg(RFLR);
  1838. add_reg(IPGR);
  1839. if (cd->apr)
  1840. add_reg(APR);
  1841. if (cd->mpr)
  1842. add_reg(MPR);
  1843. add_reg(RFCR);
  1844. add_reg(RFCF);
  1845. if (cd->tpauser)
  1846. add_reg(TPAUSER);
  1847. add_reg(TPAUSECR);
  1848. add_reg(GECMR);
  1849. if (cd->bculr)
  1850. add_reg(BCULR);
  1851. add_reg(MAHR);
  1852. add_reg(MALR);
  1853. add_reg(TROCR);
  1854. add_reg(CDCR);
  1855. add_reg(LCCR);
  1856. add_reg(CNDCR);
  1857. add_reg(CEFCR);
  1858. add_reg(FRECR);
  1859. add_reg(TSFRCR);
  1860. add_reg(TLFRCR);
  1861. add_reg(CERCR);
  1862. add_reg(CEECR);
  1863. add_reg(MAFCR);
  1864. if (cd->rtrate)
  1865. add_reg(RTRATE);
  1866. if (cd->hw_checksum)
  1867. add_reg(CSMR);
  1868. if (cd->select_mii)
  1869. add_reg(RMII_MII);
  1870. if (cd->tsu) {
  1871. add_tsu_reg(ARSTR);
  1872. add_tsu_reg(TSU_CTRST);
  1873. if (cd->dual_port) {
  1874. add_tsu_reg(TSU_FWEN0);
  1875. add_tsu_reg(TSU_FWEN1);
  1876. add_tsu_reg(TSU_FCM);
  1877. add_tsu_reg(TSU_BSYSL0);
  1878. add_tsu_reg(TSU_BSYSL1);
  1879. add_tsu_reg(TSU_PRISL0);
  1880. add_tsu_reg(TSU_PRISL1);
  1881. add_tsu_reg(TSU_FWSL0);
  1882. add_tsu_reg(TSU_FWSL1);
  1883. }
  1884. add_tsu_reg(TSU_FWSLC);
  1885. if (cd->dual_port) {
  1886. add_tsu_reg(TSU_QTAGM0);
  1887. add_tsu_reg(TSU_QTAGM1);
  1888. add_tsu_reg(TSU_FWSR);
  1889. add_tsu_reg(TSU_FWINMK);
  1890. add_tsu_reg(TSU_ADQT0);
  1891. add_tsu_reg(TSU_ADQT1);
  1892. add_tsu_reg(TSU_VTAG0);
  1893. add_tsu_reg(TSU_VTAG1);
  1894. }
  1895. add_tsu_reg(TSU_ADSBSY);
  1896. add_tsu_reg(TSU_TEN);
  1897. add_tsu_reg(TSU_POST1);
  1898. add_tsu_reg(TSU_POST2);
  1899. add_tsu_reg(TSU_POST3);
  1900. add_tsu_reg(TSU_POST4);
  1901. /* This is the start of a table, not just a single register. */
  1902. if (buf) {
  1903. unsigned int i;
  1904. mark_reg_valid(TSU_ADRH0);
  1905. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
  1906. *buf++ = ioread32(mdp->tsu_addr +
  1907. mdp->reg_offset[TSU_ADRH0] +
  1908. i * 4);
  1909. }
  1910. len += SH_ETH_TSU_CAM_ENTRIES * 2;
  1911. }
  1912. #undef mark_reg_valid
  1913. #undef add_reg_from
  1914. #undef add_reg
  1915. #undef add_tsu_reg
  1916. return len * 4;
  1917. }
  1918. static int sh_eth_get_regs_len(struct net_device *ndev)
  1919. {
  1920. return __sh_eth_get_regs(ndev, NULL);
  1921. }
  1922. static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
  1923. void *buf)
  1924. {
  1925. struct sh_eth_private *mdp = netdev_priv(ndev);
  1926. regs->version = SH_ETH_REG_DUMP_VERSION;
  1927. pm_runtime_get_sync(&mdp->pdev->dev);
  1928. __sh_eth_get_regs(ndev, buf);
  1929. pm_runtime_put_sync(&mdp->pdev->dev);
  1930. }
  1931. static u32 sh_eth_get_msglevel(struct net_device *ndev)
  1932. {
  1933. struct sh_eth_private *mdp = netdev_priv(ndev);
  1934. return mdp->msg_enable;
  1935. }
  1936. static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
  1937. {
  1938. struct sh_eth_private *mdp = netdev_priv(ndev);
  1939. mdp->msg_enable = value;
  1940. }
  1941. static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
  1942. "rx_current", "tx_current",
  1943. "rx_dirty", "tx_dirty",
  1944. };
  1945. #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
  1946. static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
  1947. {
  1948. switch (sset) {
  1949. case ETH_SS_STATS:
  1950. return SH_ETH_STATS_LEN;
  1951. default:
  1952. return -EOPNOTSUPP;
  1953. }
  1954. }
  1955. static void sh_eth_get_ethtool_stats(struct net_device *ndev,
  1956. struct ethtool_stats *stats, u64 *data)
  1957. {
  1958. struct sh_eth_private *mdp = netdev_priv(ndev);
  1959. int i = 0;
  1960. /* device-specific stats */
  1961. data[i++] = mdp->cur_rx;
  1962. data[i++] = mdp->cur_tx;
  1963. data[i++] = mdp->dirty_rx;
  1964. data[i++] = mdp->dirty_tx;
  1965. }
  1966. static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1967. {
  1968. switch (stringset) {
  1969. case ETH_SS_STATS:
  1970. memcpy(data, *sh_eth_gstrings_stats,
  1971. sizeof(sh_eth_gstrings_stats));
  1972. break;
  1973. }
  1974. }
  1975. static void sh_eth_get_ringparam(struct net_device *ndev,
  1976. struct ethtool_ringparam *ring)
  1977. {
  1978. struct sh_eth_private *mdp = netdev_priv(ndev);
  1979. ring->rx_max_pending = RX_RING_MAX;
  1980. ring->tx_max_pending = TX_RING_MAX;
  1981. ring->rx_pending = mdp->num_rx_ring;
  1982. ring->tx_pending = mdp->num_tx_ring;
  1983. }
  1984. static int sh_eth_set_ringparam(struct net_device *ndev,
  1985. struct ethtool_ringparam *ring)
  1986. {
  1987. struct sh_eth_private *mdp = netdev_priv(ndev);
  1988. int ret;
  1989. if (ring->tx_pending > TX_RING_MAX ||
  1990. ring->rx_pending > RX_RING_MAX ||
  1991. ring->tx_pending < TX_RING_MIN ||
  1992. ring->rx_pending < RX_RING_MIN)
  1993. return -EINVAL;
  1994. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  1995. return -EINVAL;
  1996. if (netif_running(ndev)) {
  1997. netif_device_detach(ndev);
  1998. netif_tx_disable(ndev);
  1999. /* Serialise with the interrupt handler and NAPI, then
  2000. * disable interrupts. We have to clear the
  2001. * irq_enabled flag first to ensure that interrupts
  2002. * won't be re-enabled.
  2003. */
  2004. mdp->irq_enabled = false;
  2005. synchronize_irq(ndev->irq);
  2006. napi_synchronize(&mdp->napi);
  2007. sh_eth_write(ndev, 0x0000, EESIPR);
  2008. sh_eth_dev_exit(ndev);
  2009. /* Free all the skbuffs in the Rx queue and the DMA buffers. */
  2010. sh_eth_ring_free(ndev);
  2011. }
  2012. /* Set new parameters */
  2013. mdp->num_rx_ring = ring->rx_pending;
  2014. mdp->num_tx_ring = ring->tx_pending;
  2015. if (netif_running(ndev)) {
  2016. ret = sh_eth_ring_init(ndev);
  2017. if (ret < 0) {
  2018. netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
  2019. __func__);
  2020. return ret;
  2021. }
  2022. ret = sh_eth_dev_init(ndev);
  2023. if (ret < 0) {
  2024. netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
  2025. __func__);
  2026. return ret;
  2027. }
  2028. netif_device_attach(ndev);
  2029. }
  2030. return 0;
  2031. }
  2032. static void sh_eth_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2033. {
  2034. struct sh_eth_private *mdp = netdev_priv(ndev);
  2035. wol->supported = 0;
  2036. wol->wolopts = 0;
  2037. if (mdp->cd->magic) {
  2038. wol->supported = WAKE_MAGIC;
  2039. wol->wolopts = mdp->wol_enabled ? WAKE_MAGIC : 0;
  2040. }
  2041. }
  2042. static int sh_eth_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2043. {
  2044. struct sh_eth_private *mdp = netdev_priv(ndev);
  2045. if (!mdp->cd->magic || wol->wolopts & ~WAKE_MAGIC)
  2046. return -EOPNOTSUPP;
  2047. mdp->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
  2048. device_set_wakeup_enable(&mdp->pdev->dev, mdp->wol_enabled);
  2049. return 0;
  2050. }
  2051. static const struct ethtool_ops sh_eth_ethtool_ops = {
  2052. .get_regs_len = sh_eth_get_regs_len,
  2053. .get_regs = sh_eth_get_regs,
  2054. .nway_reset = phy_ethtool_nway_reset,
  2055. .get_msglevel = sh_eth_get_msglevel,
  2056. .set_msglevel = sh_eth_set_msglevel,
  2057. .get_link = ethtool_op_get_link,
  2058. .get_strings = sh_eth_get_strings,
  2059. .get_ethtool_stats = sh_eth_get_ethtool_stats,
  2060. .get_sset_count = sh_eth_get_sset_count,
  2061. .get_ringparam = sh_eth_get_ringparam,
  2062. .set_ringparam = sh_eth_set_ringparam,
  2063. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2064. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2065. .get_wol = sh_eth_get_wol,
  2066. .set_wol = sh_eth_set_wol,
  2067. };
  2068. /* network device open function */
  2069. static int sh_eth_open(struct net_device *ndev)
  2070. {
  2071. struct sh_eth_private *mdp = netdev_priv(ndev);
  2072. int ret;
  2073. pm_runtime_get_sync(&mdp->pdev->dev);
  2074. napi_enable(&mdp->napi);
  2075. ret = request_irq(ndev->irq, sh_eth_interrupt,
  2076. mdp->cd->irq_flags, ndev->name, ndev);
  2077. if (ret) {
  2078. netdev_err(ndev, "Can not assign IRQ number\n");
  2079. goto out_napi_off;
  2080. }
  2081. /* Descriptor set */
  2082. ret = sh_eth_ring_init(ndev);
  2083. if (ret)
  2084. goto out_free_irq;
  2085. /* device init */
  2086. ret = sh_eth_dev_init(ndev);
  2087. if (ret)
  2088. goto out_free_irq;
  2089. /* PHY control start*/
  2090. ret = sh_eth_phy_start(ndev);
  2091. if (ret)
  2092. goto out_free_irq;
  2093. netif_start_queue(ndev);
  2094. mdp->is_opened = 1;
  2095. return ret;
  2096. out_free_irq:
  2097. free_irq(ndev->irq, ndev);
  2098. out_napi_off:
  2099. napi_disable(&mdp->napi);
  2100. pm_runtime_put_sync(&mdp->pdev->dev);
  2101. return ret;
  2102. }
  2103. /* Timeout function */
  2104. static void sh_eth_tx_timeout(struct net_device *ndev)
  2105. {
  2106. struct sh_eth_private *mdp = netdev_priv(ndev);
  2107. struct sh_eth_rxdesc *rxdesc;
  2108. int i;
  2109. netif_stop_queue(ndev);
  2110. netif_err(mdp, timer, ndev,
  2111. "transmit timed out, status %8.8x, resetting...\n",
  2112. sh_eth_read(ndev, EESR));
  2113. /* tx_errors count up */
  2114. ndev->stats.tx_errors++;
  2115. /* Free all the skbuffs in the Rx queue. */
  2116. for (i = 0; i < mdp->num_rx_ring; i++) {
  2117. rxdesc = &mdp->rx_ring[i];
  2118. rxdesc->status = cpu_to_le32(0);
  2119. rxdesc->addr = cpu_to_le32(0xBADF00D0);
  2120. dev_kfree_skb(mdp->rx_skbuff[i]);
  2121. mdp->rx_skbuff[i] = NULL;
  2122. }
  2123. for (i = 0; i < mdp->num_tx_ring; i++) {
  2124. dev_kfree_skb(mdp->tx_skbuff[i]);
  2125. mdp->tx_skbuff[i] = NULL;
  2126. }
  2127. /* device init */
  2128. sh_eth_dev_init(ndev);
  2129. netif_start_queue(ndev);
  2130. }
  2131. /* Packet transmit function */
  2132. static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  2133. {
  2134. struct sh_eth_private *mdp = netdev_priv(ndev);
  2135. struct sh_eth_txdesc *txdesc;
  2136. dma_addr_t dma_addr;
  2137. u32 entry;
  2138. unsigned long flags;
  2139. spin_lock_irqsave(&mdp->lock, flags);
  2140. if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
  2141. if (!sh_eth_tx_free(ndev, true)) {
  2142. netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
  2143. netif_stop_queue(ndev);
  2144. spin_unlock_irqrestore(&mdp->lock, flags);
  2145. return NETDEV_TX_BUSY;
  2146. }
  2147. }
  2148. spin_unlock_irqrestore(&mdp->lock, flags);
  2149. if (skb_put_padto(skb, ETH_ZLEN))
  2150. return NETDEV_TX_OK;
  2151. entry = mdp->cur_tx % mdp->num_tx_ring;
  2152. mdp->tx_skbuff[entry] = skb;
  2153. txdesc = &mdp->tx_ring[entry];
  2154. /* soft swap. */
  2155. if (!mdp->cd->hw_swap)
  2156. sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
  2157. dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, skb->len,
  2158. DMA_TO_DEVICE);
  2159. if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
  2160. kfree_skb(skb);
  2161. return NETDEV_TX_OK;
  2162. }
  2163. txdesc->addr = cpu_to_le32(dma_addr);
  2164. txdesc->len = cpu_to_le32(skb->len << 16);
  2165. dma_wmb(); /* TACT bit must be set after all the above writes */
  2166. if (entry >= mdp->num_tx_ring - 1)
  2167. txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
  2168. else
  2169. txdesc->status |= cpu_to_le32(TD_TACT);
  2170. mdp->cur_tx++;
  2171. if (!(sh_eth_read(ndev, EDTRR) & mdp->cd->edtrr_trns))
  2172. sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
  2173. return NETDEV_TX_OK;
  2174. }
  2175. /* The statistics registers have write-clear behaviour, which means we
  2176. * will lose any increment between the read and write. We mitigate
  2177. * this by only clearing when we read a non-zero value, so we will
  2178. * never falsely report a total of zero.
  2179. */
  2180. static void
  2181. sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
  2182. {
  2183. u32 delta = sh_eth_read(ndev, reg);
  2184. if (delta) {
  2185. *stat += delta;
  2186. sh_eth_write(ndev, 0, reg);
  2187. }
  2188. }
  2189. static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
  2190. {
  2191. struct sh_eth_private *mdp = netdev_priv(ndev);
  2192. if (mdp->cd->no_tx_cntrs)
  2193. return &ndev->stats;
  2194. if (!mdp->is_opened)
  2195. return &ndev->stats;
  2196. sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
  2197. sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
  2198. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
  2199. if (mdp->cd->cexcr) {
  2200. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2201. CERCR);
  2202. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2203. CEECR);
  2204. } else {
  2205. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2206. CNDCR);
  2207. }
  2208. return &ndev->stats;
  2209. }
  2210. /* device close function */
  2211. static int sh_eth_close(struct net_device *ndev)
  2212. {
  2213. struct sh_eth_private *mdp = netdev_priv(ndev);
  2214. netif_stop_queue(ndev);
  2215. /* Serialise with the interrupt handler and NAPI, then disable
  2216. * interrupts. We have to clear the irq_enabled flag first to
  2217. * ensure that interrupts won't be re-enabled.
  2218. */
  2219. mdp->irq_enabled = false;
  2220. synchronize_irq(ndev->irq);
  2221. napi_disable(&mdp->napi);
  2222. sh_eth_write(ndev, 0x0000, EESIPR);
  2223. sh_eth_dev_exit(ndev);
  2224. /* PHY Disconnect */
  2225. if (ndev->phydev) {
  2226. phy_stop(ndev->phydev);
  2227. phy_disconnect(ndev->phydev);
  2228. }
  2229. free_irq(ndev->irq, ndev);
  2230. /* Free all the skbuffs in the Rx queue and the DMA buffer. */
  2231. sh_eth_ring_free(ndev);
  2232. pm_runtime_put_sync(&mdp->pdev->dev);
  2233. mdp->is_opened = 0;
  2234. return 0;
  2235. }
  2236. /* ioctl to device function */
  2237. static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  2238. {
  2239. struct phy_device *phydev = ndev->phydev;
  2240. if (!netif_running(ndev))
  2241. return -EINVAL;
  2242. if (!phydev)
  2243. return -ENODEV;
  2244. return phy_mii_ioctl(phydev, rq, cmd);
  2245. }
  2246. static int sh_eth_change_mtu(struct net_device *ndev, int new_mtu)
  2247. {
  2248. if (netif_running(ndev))
  2249. return -EBUSY;
  2250. ndev->mtu = new_mtu;
  2251. netdev_update_features(ndev);
  2252. return 0;
  2253. }
  2254. /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
  2255. static u32 sh_eth_tsu_get_post_mask(int entry)
  2256. {
  2257. return 0x0f << (28 - ((entry % 8) * 4));
  2258. }
  2259. static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
  2260. {
  2261. return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
  2262. }
  2263. static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
  2264. int entry)
  2265. {
  2266. struct sh_eth_private *mdp = netdev_priv(ndev);
  2267. int reg = TSU_POST1 + entry / 8;
  2268. u32 tmp;
  2269. tmp = sh_eth_tsu_read(mdp, reg);
  2270. sh_eth_tsu_write(mdp, tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg);
  2271. }
  2272. static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
  2273. int entry)
  2274. {
  2275. struct sh_eth_private *mdp = netdev_priv(ndev);
  2276. int reg = TSU_POST1 + entry / 8;
  2277. u32 post_mask, ref_mask, tmp;
  2278. post_mask = sh_eth_tsu_get_post_mask(entry);
  2279. ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
  2280. tmp = sh_eth_tsu_read(mdp, reg);
  2281. sh_eth_tsu_write(mdp, tmp & ~post_mask, reg);
  2282. /* If other port enables, the function returns "true" */
  2283. return tmp & ref_mask;
  2284. }
  2285. static int sh_eth_tsu_busy(struct net_device *ndev)
  2286. {
  2287. int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
  2288. struct sh_eth_private *mdp = netdev_priv(ndev);
  2289. while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
  2290. udelay(10);
  2291. timeout--;
  2292. if (timeout <= 0) {
  2293. netdev_err(ndev, "%s: timeout\n", __func__);
  2294. return -ETIMEDOUT;
  2295. }
  2296. }
  2297. return 0;
  2298. }
  2299. static int sh_eth_tsu_write_entry(struct net_device *ndev, u16 offset,
  2300. const u8 *addr)
  2301. {
  2302. struct sh_eth_private *mdp = netdev_priv(ndev);
  2303. u32 val;
  2304. val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
  2305. iowrite32(val, mdp->tsu_addr + offset);
  2306. if (sh_eth_tsu_busy(ndev) < 0)
  2307. return -EBUSY;
  2308. val = addr[4] << 8 | addr[5];
  2309. iowrite32(val, mdp->tsu_addr + offset + 4);
  2310. if (sh_eth_tsu_busy(ndev) < 0)
  2311. return -EBUSY;
  2312. return 0;
  2313. }
  2314. static void sh_eth_tsu_read_entry(struct net_device *ndev, u16 offset, u8 *addr)
  2315. {
  2316. struct sh_eth_private *mdp = netdev_priv(ndev);
  2317. u32 val;
  2318. val = ioread32(mdp->tsu_addr + offset);
  2319. addr[0] = (val >> 24) & 0xff;
  2320. addr[1] = (val >> 16) & 0xff;
  2321. addr[2] = (val >> 8) & 0xff;
  2322. addr[3] = val & 0xff;
  2323. val = ioread32(mdp->tsu_addr + offset + 4);
  2324. addr[4] = (val >> 8) & 0xff;
  2325. addr[5] = val & 0xff;
  2326. }
  2327. static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
  2328. {
  2329. struct sh_eth_private *mdp = netdev_priv(ndev);
  2330. u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2331. int i;
  2332. u8 c_addr[ETH_ALEN];
  2333. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  2334. sh_eth_tsu_read_entry(ndev, reg_offset, c_addr);
  2335. if (ether_addr_equal(addr, c_addr))
  2336. return i;
  2337. }
  2338. return -ENOENT;
  2339. }
  2340. static int sh_eth_tsu_find_empty(struct net_device *ndev)
  2341. {
  2342. u8 blank[ETH_ALEN];
  2343. int entry;
  2344. memset(blank, 0, sizeof(blank));
  2345. entry = sh_eth_tsu_find_entry(ndev, blank);
  2346. return (entry < 0) ? -ENOMEM : entry;
  2347. }
  2348. static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
  2349. int entry)
  2350. {
  2351. struct sh_eth_private *mdp = netdev_priv(ndev);
  2352. u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2353. int ret;
  2354. u8 blank[ETH_ALEN];
  2355. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
  2356. ~(1 << (31 - entry)), TSU_TEN);
  2357. memset(blank, 0, sizeof(blank));
  2358. ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
  2359. if (ret < 0)
  2360. return ret;
  2361. return 0;
  2362. }
  2363. static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
  2364. {
  2365. struct sh_eth_private *mdp = netdev_priv(ndev);
  2366. u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2367. int i, ret;
  2368. if (!mdp->cd->tsu)
  2369. return 0;
  2370. i = sh_eth_tsu_find_entry(ndev, addr);
  2371. if (i < 0) {
  2372. /* No entry found, create one */
  2373. i = sh_eth_tsu_find_empty(ndev);
  2374. if (i < 0)
  2375. return -ENOMEM;
  2376. ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
  2377. if (ret < 0)
  2378. return ret;
  2379. /* Enable the entry */
  2380. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
  2381. (1 << (31 - i)), TSU_TEN);
  2382. }
  2383. /* Entry found or created, enable POST */
  2384. sh_eth_tsu_enable_cam_entry_post(ndev, i);
  2385. return 0;
  2386. }
  2387. static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
  2388. {
  2389. struct sh_eth_private *mdp = netdev_priv(ndev);
  2390. int i, ret;
  2391. if (!mdp->cd->tsu)
  2392. return 0;
  2393. i = sh_eth_tsu_find_entry(ndev, addr);
  2394. if (i) {
  2395. /* Entry found */
  2396. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  2397. goto done;
  2398. /* Disable the entry if both ports was disabled */
  2399. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  2400. if (ret < 0)
  2401. return ret;
  2402. }
  2403. done:
  2404. return 0;
  2405. }
  2406. static int sh_eth_tsu_purge_all(struct net_device *ndev)
  2407. {
  2408. struct sh_eth_private *mdp = netdev_priv(ndev);
  2409. int i, ret;
  2410. if (!mdp->cd->tsu)
  2411. return 0;
  2412. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
  2413. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  2414. continue;
  2415. /* Disable the entry if both ports was disabled */
  2416. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  2417. if (ret < 0)
  2418. return ret;
  2419. }
  2420. return 0;
  2421. }
  2422. static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
  2423. {
  2424. struct sh_eth_private *mdp = netdev_priv(ndev);
  2425. u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2426. u8 addr[ETH_ALEN];
  2427. int i;
  2428. if (!mdp->cd->tsu)
  2429. return;
  2430. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  2431. sh_eth_tsu_read_entry(ndev, reg_offset, addr);
  2432. if (is_multicast_ether_addr(addr))
  2433. sh_eth_tsu_del_entry(ndev, addr);
  2434. }
  2435. }
  2436. /* Update promiscuous flag and multicast filter */
  2437. static void sh_eth_set_rx_mode(struct net_device *ndev)
  2438. {
  2439. struct sh_eth_private *mdp = netdev_priv(ndev);
  2440. u32 ecmr_bits;
  2441. int mcast_all = 0;
  2442. unsigned long flags;
  2443. spin_lock_irqsave(&mdp->lock, flags);
  2444. /* Initial condition is MCT = 1, PRM = 0.
  2445. * Depending on ndev->flags, set PRM or clear MCT
  2446. */
  2447. ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
  2448. if (mdp->cd->tsu)
  2449. ecmr_bits |= ECMR_MCT;
  2450. if (!(ndev->flags & IFF_MULTICAST)) {
  2451. sh_eth_tsu_purge_mcast(ndev);
  2452. mcast_all = 1;
  2453. }
  2454. if (ndev->flags & IFF_ALLMULTI) {
  2455. sh_eth_tsu_purge_mcast(ndev);
  2456. ecmr_bits &= ~ECMR_MCT;
  2457. mcast_all = 1;
  2458. }
  2459. if (ndev->flags & IFF_PROMISC) {
  2460. sh_eth_tsu_purge_all(ndev);
  2461. ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
  2462. } else if (mdp->cd->tsu) {
  2463. struct netdev_hw_addr *ha;
  2464. netdev_for_each_mc_addr(ha, ndev) {
  2465. if (mcast_all && is_multicast_ether_addr(ha->addr))
  2466. continue;
  2467. if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
  2468. if (!mcast_all) {
  2469. sh_eth_tsu_purge_mcast(ndev);
  2470. ecmr_bits &= ~ECMR_MCT;
  2471. mcast_all = 1;
  2472. }
  2473. }
  2474. }
  2475. }
  2476. /* update the ethernet mode */
  2477. sh_eth_write(ndev, ecmr_bits, ECMR);
  2478. spin_unlock_irqrestore(&mdp->lock, flags);
  2479. }
  2480. static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
  2481. {
  2482. if (!mdp->port)
  2483. return TSU_VTAG0;
  2484. else
  2485. return TSU_VTAG1;
  2486. }
  2487. static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
  2488. __be16 proto, u16 vid)
  2489. {
  2490. struct sh_eth_private *mdp = netdev_priv(ndev);
  2491. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  2492. if (unlikely(!mdp->cd->tsu))
  2493. return -EPERM;
  2494. /* No filtering if vid = 0 */
  2495. if (!vid)
  2496. return 0;
  2497. mdp->vlan_num_ids++;
  2498. /* The controller has one VLAN tag HW filter. So, if the filter is
  2499. * already enabled, the driver disables it and the filte
  2500. */
  2501. if (mdp->vlan_num_ids > 1) {
  2502. /* disable VLAN filter */
  2503. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2504. return 0;
  2505. }
  2506. sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
  2507. vtag_reg_index);
  2508. return 0;
  2509. }
  2510. static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
  2511. __be16 proto, u16 vid)
  2512. {
  2513. struct sh_eth_private *mdp = netdev_priv(ndev);
  2514. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  2515. if (unlikely(!mdp->cd->tsu))
  2516. return -EPERM;
  2517. /* No filtering if vid = 0 */
  2518. if (!vid)
  2519. return 0;
  2520. mdp->vlan_num_ids--;
  2521. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2522. return 0;
  2523. }
  2524. /* SuperH's TSU register init function */
  2525. static void sh_eth_tsu_init(struct sh_eth_private *mdp)
  2526. {
  2527. if (!mdp->cd->dual_port) {
  2528. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2529. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL,
  2530. TSU_FWSLC); /* Enable POST registers */
  2531. return;
  2532. }
  2533. sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
  2534. sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
  2535. sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
  2536. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
  2537. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
  2538. sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
  2539. sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
  2540. sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
  2541. sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
  2542. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
  2543. sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
  2544. sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
  2545. sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
  2546. sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
  2547. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2548. sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
  2549. sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
  2550. sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
  2551. sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
  2552. }
  2553. /* MDIO bus release function */
  2554. static int sh_mdio_release(struct sh_eth_private *mdp)
  2555. {
  2556. /* unregister mdio bus */
  2557. mdiobus_unregister(mdp->mii_bus);
  2558. /* free bitbang info */
  2559. free_mdio_bitbang(mdp->mii_bus);
  2560. return 0;
  2561. }
  2562. /* MDIO bus init function */
  2563. static int sh_mdio_init(struct sh_eth_private *mdp,
  2564. struct sh_eth_plat_data *pd)
  2565. {
  2566. int ret;
  2567. struct bb_info *bitbang;
  2568. struct platform_device *pdev = mdp->pdev;
  2569. struct device *dev = &mdp->pdev->dev;
  2570. /* create bit control struct for PHY */
  2571. bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
  2572. if (!bitbang)
  2573. return -ENOMEM;
  2574. /* bitbang init */
  2575. bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
  2576. bitbang->set_gate = pd->set_mdio_gate;
  2577. bitbang->ctrl.ops = &bb_ops;
  2578. /* MII controller setting */
  2579. mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  2580. if (!mdp->mii_bus)
  2581. return -ENOMEM;
  2582. /* Hook up MII support for ethtool */
  2583. mdp->mii_bus->name = "sh_mii";
  2584. mdp->mii_bus->parent = dev;
  2585. snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  2586. pdev->name, pdev->id);
  2587. /* register MDIO bus */
  2588. if (pd->phy_irq > 0)
  2589. mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
  2590. ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
  2591. if (ret)
  2592. goto out_free_bus;
  2593. return 0;
  2594. out_free_bus:
  2595. free_mdio_bitbang(mdp->mii_bus);
  2596. return ret;
  2597. }
  2598. static const u16 *sh_eth_get_register_offset(int register_type)
  2599. {
  2600. const u16 *reg_offset = NULL;
  2601. switch (register_type) {
  2602. case SH_ETH_REG_GIGABIT:
  2603. reg_offset = sh_eth_offset_gigabit;
  2604. break;
  2605. case SH_ETH_REG_FAST_RZ:
  2606. reg_offset = sh_eth_offset_fast_rz;
  2607. break;
  2608. case SH_ETH_REG_FAST_RCAR:
  2609. reg_offset = sh_eth_offset_fast_rcar;
  2610. break;
  2611. case SH_ETH_REG_FAST_SH4:
  2612. reg_offset = sh_eth_offset_fast_sh4;
  2613. break;
  2614. case SH_ETH_REG_FAST_SH3_SH2:
  2615. reg_offset = sh_eth_offset_fast_sh3_sh2;
  2616. break;
  2617. }
  2618. return reg_offset;
  2619. }
  2620. static const struct net_device_ops sh_eth_netdev_ops = {
  2621. .ndo_open = sh_eth_open,
  2622. .ndo_stop = sh_eth_close,
  2623. .ndo_start_xmit = sh_eth_start_xmit,
  2624. .ndo_get_stats = sh_eth_get_stats,
  2625. .ndo_set_rx_mode = sh_eth_set_rx_mode,
  2626. .ndo_tx_timeout = sh_eth_tx_timeout,
  2627. .ndo_do_ioctl = sh_eth_do_ioctl,
  2628. .ndo_change_mtu = sh_eth_change_mtu,
  2629. .ndo_validate_addr = eth_validate_addr,
  2630. .ndo_set_mac_address = eth_mac_addr,
  2631. };
  2632. static const struct net_device_ops sh_eth_netdev_ops_tsu = {
  2633. .ndo_open = sh_eth_open,
  2634. .ndo_stop = sh_eth_close,
  2635. .ndo_start_xmit = sh_eth_start_xmit,
  2636. .ndo_get_stats = sh_eth_get_stats,
  2637. .ndo_set_rx_mode = sh_eth_set_rx_mode,
  2638. .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
  2639. .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
  2640. .ndo_tx_timeout = sh_eth_tx_timeout,
  2641. .ndo_do_ioctl = sh_eth_do_ioctl,
  2642. .ndo_change_mtu = sh_eth_change_mtu,
  2643. .ndo_validate_addr = eth_validate_addr,
  2644. .ndo_set_mac_address = eth_mac_addr,
  2645. };
  2646. #ifdef CONFIG_OF
  2647. static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
  2648. {
  2649. struct device_node *np = dev->of_node;
  2650. struct sh_eth_plat_data *pdata;
  2651. const char *mac_addr;
  2652. int ret;
  2653. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2654. if (!pdata)
  2655. return NULL;
  2656. ret = of_get_phy_mode(np);
  2657. if (ret < 0)
  2658. return NULL;
  2659. pdata->phy_interface = ret;
  2660. mac_addr = of_get_mac_address(np);
  2661. if (mac_addr)
  2662. memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
  2663. pdata->no_ether_link =
  2664. of_property_read_bool(np, "renesas,no-ether-link");
  2665. pdata->ether_link_active_low =
  2666. of_property_read_bool(np, "renesas,ether-link-active-low");
  2667. return pdata;
  2668. }
  2669. static const struct of_device_id sh_eth_match_table[] = {
  2670. { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
  2671. { .compatible = "renesas,ether-r8a7743", .data = &rcar_gen2_data },
  2672. { .compatible = "renesas,ether-r8a7745", .data = &rcar_gen2_data },
  2673. { .compatible = "renesas,ether-r8a7778", .data = &rcar_gen1_data },
  2674. { .compatible = "renesas,ether-r8a7779", .data = &rcar_gen1_data },
  2675. { .compatible = "renesas,ether-r8a7790", .data = &rcar_gen2_data },
  2676. { .compatible = "renesas,ether-r8a7791", .data = &rcar_gen2_data },
  2677. { .compatible = "renesas,ether-r8a7793", .data = &rcar_gen2_data },
  2678. { .compatible = "renesas,ether-r8a7794", .data = &rcar_gen2_data },
  2679. { .compatible = "renesas,gether-r8a77980", .data = &r8a77980_data },
  2680. { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
  2681. { .compatible = "renesas,ether-r7s9210", .data = &r7s9210_data },
  2682. { .compatible = "renesas,rcar-gen1-ether", .data = &rcar_gen1_data },
  2683. { .compatible = "renesas,rcar-gen2-ether", .data = &rcar_gen2_data },
  2684. { }
  2685. };
  2686. MODULE_DEVICE_TABLE(of, sh_eth_match_table);
  2687. #else
  2688. static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
  2689. {
  2690. return NULL;
  2691. }
  2692. #endif
  2693. static int sh_eth_drv_probe(struct platform_device *pdev)
  2694. {
  2695. struct resource *res;
  2696. struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
  2697. const struct platform_device_id *id = platform_get_device_id(pdev);
  2698. struct sh_eth_private *mdp;
  2699. struct net_device *ndev;
  2700. int ret;
  2701. /* get base addr */
  2702. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2703. ndev = alloc_etherdev(sizeof(struct sh_eth_private));
  2704. if (!ndev)
  2705. return -ENOMEM;
  2706. pm_runtime_enable(&pdev->dev);
  2707. pm_runtime_get_sync(&pdev->dev);
  2708. ret = platform_get_irq(pdev, 0);
  2709. if (ret < 0)
  2710. goto out_release;
  2711. ndev->irq = ret;
  2712. SET_NETDEV_DEV(ndev, &pdev->dev);
  2713. mdp = netdev_priv(ndev);
  2714. mdp->num_tx_ring = TX_RING_SIZE;
  2715. mdp->num_rx_ring = RX_RING_SIZE;
  2716. mdp->addr = devm_ioremap_resource(&pdev->dev, res);
  2717. if (IS_ERR(mdp->addr)) {
  2718. ret = PTR_ERR(mdp->addr);
  2719. goto out_release;
  2720. }
  2721. ndev->base_addr = res->start;
  2722. spin_lock_init(&mdp->lock);
  2723. mdp->pdev = pdev;
  2724. if (pdev->dev.of_node)
  2725. pd = sh_eth_parse_dt(&pdev->dev);
  2726. if (!pd) {
  2727. dev_err(&pdev->dev, "no platform data\n");
  2728. ret = -EINVAL;
  2729. goto out_release;
  2730. }
  2731. /* get PHY ID */
  2732. mdp->phy_id = pd->phy;
  2733. mdp->phy_interface = pd->phy_interface;
  2734. mdp->no_ether_link = pd->no_ether_link;
  2735. mdp->ether_link_active_low = pd->ether_link_active_low;
  2736. /* set cpu data */
  2737. if (id)
  2738. mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
  2739. else
  2740. mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
  2741. mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
  2742. if (!mdp->reg_offset) {
  2743. dev_err(&pdev->dev, "Unknown register type (%d)\n",
  2744. mdp->cd->register_type);
  2745. ret = -EINVAL;
  2746. goto out_release;
  2747. }
  2748. sh_eth_set_default_cpu_data(mdp->cd);
  2749. /* User's manual states max MTU should be 2048 but due to the
  2750. * alignment calculations in sh_eth_ring_init() the practical
  2751. * MTU is a bit less. Maybe this can be optimized some more.
  2752. */
  2753. ndev->max_mtu = 2000 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  2754. ndev->min_mtu = ETH_MIN_MTU;
  2755. /* set function */
  2756. if (mdp->cd->tsu)
  2757. ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
  2758. else
  2759. ndev->netdev_ops = &sh_eth_netdev_ops;
  2760. ndev->ethtool_ops = &sh_eth_ethtool_ops;
  2761. ndev->watchdog_timeo = TX_TIMEOUT;
  2762. /* debug message level */
  2763. mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
  2764. /* read and set MAC address */
  2765. read_mac_address(ndev, pd->mac_addr);
  2766. if (!is_valid_ether_addr(ndev->dev_addr)) {
  2767. dev_warn(&pdev->dev,
  2768. "no valid MAC address supplied, using a random one.\n");
  2769. eth_hw_addr_random(ndev);
  2770. }
  2771. if (mdp->cd->tsu) {
  2772. int port = pdev->id < 0 ? 0 : pdev->id % 2;
  2773. struct resource *rtsu;
  2774. rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2775. if (!rtsu) {
  2776. dev_err(&pdev->dev, "no TSU resource\n");
  2777. ret = -ENODEV;
  2778. goto out_release;
  2779. }
  2780. /* We can only request the TSU region for the first port
  2781. * of the two sharing this TSU for the probe to succeed...
  2782. */
  2783. if (port == 0 &&
  2784. !devm_request_mem_region(&pdev->dev, rtsu->start,
  2785. resource_size(rtsu),
  2786. dev_name(&pdev->dev))) {
  2787. dev_err(&pdev->dev, "can't request TSU resource.\n");
  2788. ret = -EBUSY;
  2789. goto out_release;
  2790. }
  2791. /* ioremap the TSU registers */
  2792. mdp->tsu_addr = devm_ioremap(&pdev->dev, rtsu->start,
  2793. resource_size(rtsu));
  2794. if (!mdp->tsu_addr) {
  2795. dev_err(&pdev->dev, "TSU region ioremap() failed.\n");
  2796. ret = -ENOMEM;
  2797. goto out_release;
  2798. }
  2799. mdp->port = port;
  2800. ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
  2801. /* Need to init only the first port of the two sharing a TSU */
  2802. if (port == 0) {
  2803. if (mdp->cd->chip_reset)
  2804. mdp->cd->chip_reset(ndev);
  2805. /* TSU init (Init only)*/
  2806. sh_eth_tsu_init(mdp);
  2807. }
  2808. }
  2809. if (mdp->cd->rmiimode)
  2810. sh_eth_write(ndev, 0x1, RMIIMODE);
  2811. /* MDIO bus init */
  2812. ret = sh_mdio_init(mdp, pd);
  2813. if (ret) {
  2814. if (ret != -EPROBE_DEFER)
  2815. dev_err(&pdev->dev, "MDIO init failed: %d\n", ret);
  2816. goto out_release;
  2817. }
  2818. netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
  2819. /* network device register */
  2820. ret = register_netdev(ndev);
  2821. if (ret)
  2822. goto out_napi_del;
  2823. if (mdp->cd->magic)
  2824. device_set_wakeup_capable(&pdev->dev, 1);
  2825. /* print device information */
  2826. netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
  2827. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  2828. pm_runtime_put(&pdev->dev);
  2829. platform_set_drvdata(pdev, ndev);
  2830. return ret;
  2831. out_napi_del:
  2832. netif_napi_del(&mdp->napi);
  2833. sh_mdio_release(mdp);
  2834. out_release:
  2835. /* net_dev free */
  2836. free_netdev(ndev);
  2837. pm_runtime_put(&pdev->dev);
  2838. pm_runtime_disable(&pdev->dev);
  2839. return ret;
  2840. }
  2841. static int sh_eth_drv_remove(struct platform_device *pdev)
  2842. {
  2843. struct net_device *ndev = platform_get_drvdata(pdev);
  2844. struct sh_eth_private *mdp = netdev_priv(ndev);
  2845. unregister_netdev(ndev);
  2846. netif_napi_del(&mdp->napi);
  2847. sh_mdio_release(mdp);
  2848. pm_runtime_disable(&pdev->dev);
  2849. free_netdev(ndev);
  2850. return 0;
  2851. }
  2852. #ifdef CONFIG_PM
  2853. #ifdef CONFIG_PM_SLEEP
  2854. static int sh_eth_wol_setup(struct net_device *ndev)
  2855. {
  2856. struct sh_eth_private *mdp = netdev_priv(ndev);
  2857. /* Only allow ECI interrupts */
  2858. synchronize_irq(ndev->irq);
  2859. napi_disable(&mdp->napi);
  2860. sh_eth_write(ndev, EESIPR_ECIIP, EESIPR);
  2861. /* Enable MagicPacket */
  2862. sh_eth_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
  2863. return enable_irq_wake(ndev->irq);
  2864. }
  2865. static int sh_eth_wol_restore(struct net_device *ndev)
  2866. {
  2867. struct sh_eth_private *mdp = netdev_priv(ndev);
  2868. int ret;
  2869. napi_enable(&mdp->napi);
  2870. /* Disable MagicPacket */
  2871. sh_eth_modify(ndev, ECMR, ECMR_MPDE, 0);
  2872. /* The device needs to be reset to restore MagicPacket logic
  2873. * for next wakeup. If we close and open the device it will
  2874. * both be reset and all registers restored. This is what
  2875. * happens during suspend and resume without WoL enabled.
  2876. */
  2877. ret = sh_eth_close(ndev);
  2878. if (ret < 0)
  2879. return ret;
  2880. ret = sh_eth_open(ndev);
  2881. if (ret < 0)
  2882. return ret;
  2883. return disable_irq_wake(ndev->irq);
  2884. }
  2885. static int sh_eth_suspend(struct device *dev)
  2886. {
  2887. struct net_device *ndev = dev_get_drvdata(dev);
  2888. struct sh_eth_private *mdp = netdev_priv(ndev);
  2889. int ret = 0;
  2890. if (!netif_running(ndev))
  2891. return 0;
  2892. netif_device_detach(ndev);
  2893. if (mdp->wol_enabled)
  2894. ret = sh_eth_wol_setup(ndev);
  2895. else
  2896. ret = sh_eth_close(ndev);
  2897. return ret;
  2898. }
  2899. static int sh_eth_resume(struct device *dev)
  2900. {
  2901. struct net_device *ndev = dev_get_drvdata(dev);
  2902. struct sh_eth_private *mdp = netdev_priv(ndev);
  2903. int ret = 0;
  2904. if (!netif_running(ndev))
  2905. return 0;
  2906. if (mdp->wol_enabled)
  2907. ret = sh_eth_wol_restore(ndev);
  2908. else
  2909. ret = sh_eth_open(ndev);
  2910. if (ret < 0)
  2911. return ret;
  2912. netif_device_attach(ndev);
  2913. return ret;
  2914. }
  2915. #endif
  2916. static int sh_eth_runtime_nop(struct device *dev)
  2917. {
  2918. /* Runtime PM callback shared between ->runtime_suspend()
  2919. * and ->runtime_resume(). Simply returns success.
  2920. *
  2921. * This driver re-initializes all registers after
  2922. * pm_runtime_get_sync() anyway so there is no need
  2923. * to save and restore registers here.
  2924. */
  2925. return 0;
  2926. }
  2927. static const struct dev_pm_ops sh_eth_dev_pm_ops = {
  2928. SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
  2929. SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
  2930. };
  2931. #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
  2932. #else
  2933. #define SH_ETH_PM_OPS NULL
  2934. #endif
  2935. static const struct platform_device_id sh_eth_id_table[] = {
  2936. { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
  2937. { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
  2938. { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
  2939. { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
  2940. { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
  2941. { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
  2942. { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
  2943. { }
  2944. };
  2945. MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
  2946. static struct platform_driver sh_eth_driver = {
  2947. .probe = sh_eth_drv_probe,
  2948. .remove = sh_eth_drv_remove,
  2949. .id_table = sh_eth_id_table,
  2950. .driver = {
  2951. .name = CARDNAME,
  2952. .pm = SH_ETH_PM_OPS,
  2953. .of_match_table = of_match_ptr(sh_eth_match_table),
  2954. },
  2955. };
  2956. module_platform_driver(sh_eth_driver);
  2957. MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
  2958. MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
  2959. MODULE_LICENSE("GPL v2");