sh_eth.c 82 KB

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