sh_eth.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264
  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-2016 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 = 0xe77f009f,
  473. .tx_check = EESR_TC1 | EESR_FTC,
  474. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  475. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  476. EESR_TDE | EESR_ECI,
  477. .fdr_value = 0x0000070f,
  478. .no_psr = 1,
  479. .apr = 1,
  480. .mpr = 1,
  481. .tpauser = 1,
  482. .hw_swap = 1,
  483. .rpadir = 1,
  484. .rpadir_value = 2 << 16,
  485. .no_trimd = 1,
  486. .no_ade = 1,
  487. .hw_crc = 1,
  488. .tsu = 1,
  489. .shift_rd0 = 1,
  490. };
  491. static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
  492. {
  493. sh_eth_chip_reset(ndev);
  494. sh_eth_select_mii(ndev);
  495. }
  496. /* R8A7740 */
  497. static struct sh_eth_cpu_data r8a7740_data = {
  498. .chip_reset = sh_eth_chip_reset_r8a7740,
  499. .set_duplex = sh_eth_set_duplex,
  500. .set_rate = sh_eth_set_rate_gether,
  501. .register_type = SH_ETH_REG_GIGABIT,
  502. .ecsr_value = ECSR_ICD | ECSR_MPD,
  503. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  504. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  505. .tx_check = EESR_TC1 | EESR_FTC,
  506. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  507. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  508. EESR_TDE | EESR_ECI,
  509. .fdr_value = 0x0000070f,
  510. .apr = 1,
  511. .mpr = 1,
  512. .tpauser = 1,
  513. .bculr = 1,
  514. .hw_swap = 1,
  515. .rpadir = 1,
  516. .rpadir_value = 2 << 16,
  517. .no_trimd = 1,
  518. .no_ade = 1,
  519. .hw_crc = 1,
  520. .tsu = 1,
  521. .select_mii = 1,
  522. .shift_rd0 = 1,
  523. };
  524. /* There is CPU dependent code */
  525. static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
  526. {
  527. struct sh_eth_private *mdp = netdev_priv(ndev);
  528. switch (mdp->speed) {
  529. case 10: /* 10BASE */
  530. sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
  531. break;
  532. case 100:/* 100BASE */
  533. sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
  534. break;
  535. }
  536. }
  537. /* R8A7778/9 */
  538. static struct sh_eth_cpu_data r8a777x_data = {
  539. .set_duplex = sh_eth_set_duplex,
  540. .set_rate = sh_eth_set_rate_r8a777x,
  541. .register_type = SH_ETH_REG_FAST_RCAR,
  542. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  543. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  544. .eesipr_value = 0x01ff009f,
  545. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  546. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  547. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
  548. EESR_ECI,
  549. .fdr_value = 0x00000f0f,
  550. .apr = 1,
  551. .mpr = 1,
  552. .tpauser = 1,
  553. .hw_swap = 1,
  554. };
  555. /* R8A7790/1 */
  556. static struct sh_eth_cpu_data r8a779x_data = {
  557. .set_duplex = sh_eth_set_duplex,
  558. .set_rate = sh_eth_set_rate_r8a777x,
  559. .register_type = SH_ETH_REG_FAST_RCAR,
  560. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  561. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  562. .eesipr_value = 0x01ff009f,
  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. EESR_ECI,
  567. .fdr_value = 0x00000f0f,
  568. .trscer_err_mask = DESC_I_RINT8,
  569. .apr = 1,
  570. .mpr = 1,
  571. .tpauser = 1,
  572. .hw_swap = 1,
  573. .rmiimode = 1,
  574. };
  575. #endif /* CONFIG_OF */
  576. static void sh_eth_set_rate_sh7724(struct net_device *ndev)
  577. {
  578. struct sh_eth_private *mdp = netdev_priv(ndev);
  579. switch (mdp->speed) {
  580. case 10: /* 10BASE */
  581. sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
  582. break;
  583. case 100:/* 100BASE */
  584. sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
  585. break;
  586. }
  587. }
  588. /* SH7724 */
  589. static struct sh_eth_cpu_data sh7724_data = {
  590. .set_duplex = sh_eth_set_duplex,
  591. .set_rate = sh_eth_set_rate_sh7724,
  592. .register_type = SH_ETH_REG_FAST_SH4,
  593. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  594. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  595. .eesipr_value = 0x01ff009f,
  596. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  597. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  598. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
  599. EESR_ECI,
  600. .apr = 1,
  601. .mpr = 1,
  602. .tpauser = 1,
  603. .hw_swap = 1,
  604. .rpadir = 1,
  605. .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
  606. };
  607. static void sh_eth_set_rate_sh7757(struct net_device *ndev)
  608. {
  609. struct sh_eth_private *mdp = netdev_priv(ndev);
  610. switch (mdp->speed) {
  611. case 10: /* 10BASE */
  612. sh_eth_write(ndev, 0, RTRATE);
  613. break;
  614. case 100:/* 100BASE */
  615. sh_eth_write(ndev, 1, RTRATE);
  616. break;
  617. }
  618. }
  619. /* SH7757 */
  620. static struct sh_eth_cpu_data sh7757_data = {
  621. .set_duplex = sh_eth_set_duplex,
  622. .set_rate = sh_eth_set_rate_sh7757,
  623. .register_type = SH_ETH_REG_FAST_SH4,
  624. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  625. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  626. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
  627. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
  628. EESR_ECI,
  629. .irq_flags = IRQF_SHARED,
  630. .apr = 1,
  631. .mpr = 1,
  632. .tpauser = 1,
  633. .hw_swap = 1,
  634. .no_ade = 1,
  635. .rpadir = 1,
  636. .rpadir_value = 2 << 16,
  637. .rtrate = 1,
  638. };
  639. #define SH_GIGA_ETH_BASE 0xfee00000UL
  640. #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
  641. #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
  642. static void sh_eth_chip_reset_giga(struct net_device *ndev)
  643. {
  644. u32 mahr[2], malr[2];
  645. int i;
  646. /* save MAHR and MALR */
  647. for (i = 0; i < 2; i++) {
  648. malr[i] = ioread32((void *)GIGA_MALR(i));
  649. mahr[i] = ioread32((void *)GIGA_MAHR(i));
  650. }
  651. sh_eth_chip_reset(ndev);
  652. /* restore MAHR and MALR */
  653. for (i = 0; i < 2; i++) {
  654. iowrite32(malr[i], (void *)GIGA_MALR(i));
  655. iowrite32(mahr[i], (void *)GIGA_MAHR(i));
  656. }
  657. }
  658. static void sh_eth_set_rate_giga(struct net_device *ndev)
  659. {
  660. struct sh_eth_private *mdp = netdev_priv(ndev);
  661. switch (mdp->speed) {
  662. case 10: /* 10BASE */
  663. sh_eth_write(ndev, 0x00000000, GECMR);
  664. break;
  665. case 100:/* 100BASE */
  666. sh_eth_write(ndev, 0x00000010, GECMR);
  667. break;
  668. case 1000: /* 1000BASE */
  669. sh_eth_write(ndev, 0x00000020, GECMR);
  670. break;
  671. }
  672. }
  673. /* SH7757(GETHERC) */
  674. static struct sh_eth_cpu_data sh7757_data_giga = {
  675. .chip_reset = sh_eth_chip_reset_giga,
  676. .set_duplex = sh_eth_set_duplex,
  677. .set_rate = sh_eth_set_rate_giga,
  678. .register_type = SH_ETH_REG_GIGABIT,
  679. .ecsr_value = ECSR_ICD | ECSR_MPD,
  680. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  681. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  682. .tx_check = EESR_TC1 | EESR_FTC,
  683. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  684. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  685. EESR_TDE | EESR_ECI,
  686. .fdr_value = 0x0000072f,
  687. .irq_flags = IRQF_SHARED,
  688. .apr = 1,
  689. .mpr = 1,
  690. .tpauser = 1,
  691. .bculr = 1,
  692. .hw_swap = 1,
  693. .rpadir = 1,
  694. .rpadir_value = 2 << 16,
  695. .no_trimd = 1,
  696. .no_ade = 1,
  697. .tsu = 1,
  698. };
  699. /* SH7734 */
  700. static struct sh_eth_cpu_data sh7734_data = {
  701. .chip_reset = sh_eth_chip_reset,
  702. .set_duplex = sh_eth_set_duplex,
  703. .set_rate = sh_eth_set_rate_gether,
  704. .register_type = SH_ETH_REG_GIGABIT,
  705. .ecsr_value = ECSR_ICD | ECSR_MPD,
  706. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  707. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003f07ff,
  708. .tx_check = EESR_TC1 | EESR_FTC,
  709. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  710. EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
  711. EESR_TDE | EESR_ECI,
  712. .apr = 1,
  713. .mpr = 1,
  714. .tpauser = 1,
  715. .bculr = 1,
  716. .hw_swap = 1,
  717. .no_trimd = 1,
  718. .no_ade = 1,
  719. .tsu = 1,
  720. .hw_crc = 1,
  721. .select_mii = 1,
  722. .shift_rd0 = 1,
  723. };
  724. /* SH7763 */
  725. static struct sh_eth_cpu_data sh7763_data = {
  726. .chip_reset = sh_eth_chip_reset,
  727. .set_duplex = sh_eth_set_duplex,
  728. .set_rate = sh_eth_set_rate_gether,
  729. .register_type = SH_ETH_REG_GIGABIT,
  730. .ecsr_value = ECSR_ICD | ECSR_MPD,
  731. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  732. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003f07ff,
  733. .tx_check = EESR_TC1 | EESR_FTC,
  734. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
  735. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
  736. EESR_ECI,
  737. .apr = 1,
  738. .mpr = 1,
  739. .tpauser = 1,
  740. .bculr = 1,
  741. .hw_swap = 1,
  742. .no_trimd = 1,
  743. .no_ade = 1,
  744. .tsu = 1,
  745. .irq_flags = IRQF_SHARED,
  746. };
  747. static struct sh_eth_cpu_data sh7619_data = {
  748. .register_type = SH_ETH_REG_FAST_SH3_SH2,
  749. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  750. .apr = 1,
  751. .mpr = 1,
  752. .tpauser = 1,
  753. .hw_swap = 1,
  754. };
  755. static struct sh_eth_cpu_data sh771x_data = {
  756. .register_type = SH_ETH_REG_FAST_SH3_SH2,
  757. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  758. .tsu = 1,
  759. };
  760. static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
  761. {
  762. if (!cd->ecsr_value)
  763. cd->ecsr_value = DEFAULT_ECSR_INIT;
  764. if (!cd->ecsipr_value)
  765. cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
  766. if (!cd->fcftr_value)
  767. cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
  768. DEFAULT_FIFO_F_D_RFD;
  769. if (!cd->fdr_value)
  770. cd->fdr_value = DEFAULT_FDR_INIT;
  771. if (!cd->tx_check)
  772. cd->tx_check = DEFAULT_TX_CHECK;
  773. if (!cd->eesr_err_check)
  774. cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
  775. if (!cd->trscer_err_mask)
  776. cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
  777. }
  778. static int sh_eth_check_reset(struct net_device *ndev)
  779. {
  780. int ret = 0;
  781. int cnt = 100;
  782. while (cnt > 0) {
  783. if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
  784. break;
  785. mdelay(1);
  786. cnt--;
  787. }
  788. if (cnt <= 0) {
  789. netdev_err(ndev, "Device reset failed\n");
  790. ret = -ETIMEDOUT;
  791. }
  792. return ret;
  793. }
  794. static int sh_eth_reset(struct net_device *ndev)
  795. {
  796. struct sh_eth_private *mdp = netdev_priv(ndev);
  797. int ret = 0;
  798. if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
  799. sh_eth_write(ndev, EDSR_ENALL, EDSR);
  800. sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
  801. ret = sh_eth_check_reset(ndev);
  802. if (ret)
  803. return ret;
  804. /* Table Init */
  805. sh_eth_write(ndev, 0x0, TDLAR);
  806. sh_eth_write(ndev, 0x0, TDFAR);
  807. sh_eth_write(ndev, 0x0, TDFXR);
  808. sh_eth_write(ndev, 0x0, TDFFR);
  809. sh_eth_write(ndev, 0x0, RDLAR);
  810. sh_eth_write(ndev, 0x0, RDFAR);
  811. sh_eth_write(ndev, 0x0, RDFXR);
  812. sh_eth_write(ndev, 0x0, RDFFR);
  813. /* Reset HW CRC register */
  814. if (mdp->cd->hw_crc)
  815. sh_eth_write(ndev, 0x0, CSMR);
  816. /* Select MII mode */
  817. if (mdp->cd->select_mii)
  818. sh_eth_select_mii(ndev);
  819. } else {
  820. sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
  821. mdelay(3);
  822. sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
  823. }
  824. return ret;
  825. }
  826. static void sh_eth_set_receive_align(struct sk_buff *skb)
  827. {
  828. uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
  829. if (reserve)
  830. skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
  831. }
  832. /* Program the hardware MAC address from dev->dev_addr. */
  833. static void update_mac_address(struct net_device *ndev)
  834. {
  835. sh_eth_write(ndev,
  836. (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
  837. (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
  838. sh_eth_write(ndev,
  839. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
  840. }
  841. /* Get MAC address from SuperH MAC address register
  842. *
  843. * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
  844. * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
  845. * When you want use this device, you must set MAC address in bootloader.
  846. *
  847. */
  848. static void read_mac_address(struct net_device *ndev, unsigned char *mac)
  849. {
  850. if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
  851. memcpy(ndev->dev_addr, mac, ETH_ALEN);
  852. } else {
  853. u32 mahr = sh_eth_read(ndev, MAHR);
  854. u32 malr = sh_eth_read(ndev, MALR);
  855. ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
  856. ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
  857. ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
  858. ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
  859. ndev->dev_addr[4] = (malr >> 8) & 0xFF;
  860. ndev->dev_addr[5] = (malr >> 0) & 0xFF;
  861. }
  862. }
  863. static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
  864. {
  865. if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
  866. return EDTRR_TRNS_GETHER;
  867. else
  868. return EDTRR_TRNS_ETHER;
  869. }
  870. struct bb_info {
  871. void (*set_gate)(void *addr);
  872. struct mdiobb_ctrl ctrl;
  873. void *addr;
  874. };
  875. static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
  876. {
  877. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  878. u32 pir;
  879. if (bitbang->set_gate)
  880. bitbang->set_gate(bitbang->addr);
  881. pir = ioread32(bitbang->addr);
  882. if (set)
  883. pir |= mask;
  884. else
  885. pir &= ~mask;
  886. iowrite32(pir, bitbang->addr);
  887. }
  888. /* Data I/O pin control */
  889. static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  890. {
  891. sh_mdio_ctrl(ctrl, PIR_MMD, bit);
  892. }
  893. /* Set bit data*/
  894. static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
  895. {
  896. sh_mdio_ctrl(ctrl, PIR_MDO, bit);
  897. }
  898. /* Get bit data*/
  899. static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
  900. {
  901. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  902. if (bitbang->set_gate)
  903. bitbang->set_gate(bitbang->addr);
  904. return (ioread32(bitbang->addr) & PIR_MDI) != 0;
  905. }
  906. /* MDC pin control */
  907. static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  908. {
  909. sh_mdio_ctrl(ctrl, PIR_MDC, bit);
  910. }
  911. /* mdio bus control struct */
  912. static struct mdiobb_ops bb_ops = {
  913. .owner = THIS_MODULE,
  914. .set_mdc = sh_mdc_ctrl,
  915. .set_mdio_dir = sh_mmd_ctrl,
  916. .set_mdio_data = sh_set_mdio,
  917. .get_mdio_data = sh_get_mdio,
  918. };
  919. /* free Tx skb function */
  920. static int sh_eth_tx_free(struct net_device *ndev, bool sent_only)
  921. {
  922. struct sh_eth_private *mdp = netdev_priv(ndev);
  923. struct sh_eth_txdesc *txdesc;
  924. int free_num = 0;
  925. int entry;
  926. bool sent;
  927. for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
  928. entry = mdp->dirty_tx % mdp->num_tx_ring;
  929. txdesc = &mdp->tx_ring[entry];
  930. sent = !(txdesc->status & cpu_to_le32(TD_TACT));
  931. if (sent_only && !sent)
  932. break;
  933. /* TACT bit must be checked before all the following reads */
  934. dma_rmb();
  935. netif_info(mdp, tx_done, ndev,
  936. "tx entry %d status 0x%08x\n",
  937. entry, le32_to_cpu(txdesc->status));
  938. /* Free the original skb. */
  939. if (mdp->tx_skbuff[entry]) {
  940. dma_unmap_single(&ndev->dev, le32_to_cpu(txdesc->addr),
  941. le32_to_cpu(txdesc->len) >> 16,
  942. DMA_TO_DEVICE);
  943. dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
  944. mdp->tx_skbuff[entry] = NULL;
  945. free_num++;
  946. }
  947. txdesc->status = cpu_to_le32(TD_TFP);
  948. if (entry >= mdp->num_tx_ring - 1)
  949. txdesc->status |= cpu_to_le32(TD_TDLE);
  950. if (sent) {
  951. ndev->stats.tx_packets++;
  952. ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
  953. }
  954. }
  955. return free_num;
  956. }
  957. /* free skb and descriptor buffer */
  958. static void sh_eth_ring_free(struct net_device *ndev)
  959. {
  960. struct sh_eth_private *mdp = netdev_priv(ndev);
  961. int ringsize, i;
  962. if (mdp->rx_ring) {
  963. for (i = 0; i < mdp->num_rx_ring; i++) {
  964. if (mdp->rx_skbuff[i]) {
  965. struct sh_eth_rxdesc *rxdesc = &mdp->rx_ring[i];
  966. dma_unmap_single(&ndev->dev,
  967. le32_to_cpu(rxdesc->addr),
  968. ALIGN(mdp->rx_buf_sz, 32),
  969. DMA_FROM_DEVICE);
  970. }
  971. }
  972. ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  973. dma_free_coherent(NULL, ringsize, mdp->rx_ring,
  974. mdp->rx_desc_dma);
  975. mdp->rx_ring = NULL;
  976. }
  977. /* Free Rx skb ringbuffer */
  978. if (mdp->rx_skbuff) {
  979. for (i = 0; i < mdp->num_rx_ring; i++)
  980. dev_kfree_skb(mdp->rx_skbuff[i]);
  981. }
  982. kfree(mdp->rx_skbuff);
  983. mdp->rx_skbuff = NULL;
  984. if (mdp->tx_ring) {
  985. sh_eth_tx_free(ndev, false);
  986. ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  987. dma_free_coherent(NULL, ringsize, mdp->tx_ring,
  988. mdp->tx_desc_dma);
  989. mdp->tx_ring = NULL;
  990. }
  991. /* Free Tx skb ringbuffer */
  992. kfree(mdp->tx_skbuff);
  993. mdp->tx_skbuff = NULL;
  994. }
  995. /* format skb and descriptor buffer */
  996. static void sh_eth_ring_format(struct net_device *ndev)
  997. {
  998. struct sh_eth_private *mdp = netdev_priv(ndev);
  999. int i;
  1000. struct sk_buff *skb;
  1001. struct sh_eth_rxdesc *rxdesc = NULL;
  1002. struct sh_eth_txdesc *txdesc = NULL;
  1003. int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
  1004. int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
  1005. int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
  1006. dma_addr_t dma_addr;
  1007. u32 buf_len;
  1008. mdp->cur_rx = 0;
  1009. mdp->cur_tx = 0;
  1010. mdp->dirty_rx = 0;
  1011. mdp->dirty_tx = 0;
  1012. memset(mdp->rx_ring, 0, rx_ringsize);
  1013. /* build Rx ring buffer */
  1014. for (i = 0; i < mdp->num_rx_ring; i++) {
  1015. /* skb */
  1016. mdp->rx_skbuff[i] = NULL;
  1017. skb = netdev_alloc_skb(ndev, skbuff_size);
  1018. if (skb == NULL)
  1019. break;
  1020. sh_eth_set_receive_align(skb);
  1021. /* The size of the buffer is a multiple of 32 bytes. */
  1022. buf_len = ALIGN(mdp->rx_buf_sz, 32);
  1023. dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len,
  1024. DMA_FROM_DEVICE);
  1025. if (dma_mapping_error(&ndev->dev, dma_addr)) {
  1026. kfree_skb(skb);
  1027. break;
  1028. }
  1029. mdp->rx_skbuff[i] = skb;
  1030. /* RX descriptor */
  1031. rxdesc = &mdp->rx_ring[i];
  1032. rxdesc->len = cpu_to_le32(buf_len << 16);
  1033. rxdesc->addr = cpu_to_le32(dma_addr);
  1034. rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
  1035. /* Rx descriptor address set */
  1036. if (i == 0) {
  1037. sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
  1038. if (sh_eth_is_gether(mdp) ||
  1039. sh_eth_is_rz_fast_ether(mdp))
  1040. sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
  1041. }
  1042. }
  1043. mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
  1044. /* Mark the last entry as wrapping the ring. */
  1045. if (rxdesc)
  1046. rxdesc->status |= cpu_to_le32(RD_RDLE);
  1047. memset(mdp->tx_ring, 0, tx_ringsize);
  1048. /* build Tx ring buffer */
  1049. for (i = 0; i < mdp->num_tx_ring; i++) {
  1050. mdp->tx_skbuff[i] = NULL;
  1051. txdesc = &mdp->tx_ring[i];
  1052. txdesc->status = cpu_to_le32(TD_TFP);
  1053. txdesc->len = cpu_to_le32(0);
  1054. if (i == 0) {
  1055. /* Tx descriptor address set */
  1056. sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
  1057. if (sh_eth_is_gether(mdp) ||
  1058. sh_eth_is_rz_fast_ether(mdp))
  1059. sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
  1060. }
  1061. }
  1062. txdesc->status |= cpu_to_le32(TD_TDLE);
  1063. }
  1064. /* Get skb and descriptor buffer */
  1065. static int sh_eth_ring_init(struct net_device *ndev)
  1066. {
  1067. struct sh_eth_private *mdp = netdev_priv(ndev);
  1068. int rx_ringsize, tx_ringsize;
  1069. /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  1070. * card needs room to do 8 byte alignment, +2 so we can reserve
  1071. * the first 2 bytes, and +16 gets room for the status word from the
  1072. * card.
  1073. */
  1074. mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
  1075. (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
  1076. if (mdp->cd->rpadir)
  1077. mdp->rx_buf_sz += NET_IP_ALIGN;
  1078. /* Allocate RX and TX skb rings */
  1079. mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
  1080. GFP_KERNEL);
  1081. if (!mdp->rx_skbuff)
  1082. return -ENOMEM;
  1083. mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
  1084. GFP_KERNEL);
  1085. if (!mdp->tx_skbuff)
  1086. goto ring_free;
  1087. /* Allocate all Rx descriptors. */
  1088. rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  1089. mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
  1090. GFP_KERNEL);
  1091. if (!mdp->rx_ring)
  1092. goto ring_free;
  1093. mdp->dirty_rx = 0;
  1094. /* Allocate all Tx descriptors. */
  1095. tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  1096. mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
  1097. GFP_KERNEL);
  1098. if (!mdp->tx_ring)
  1099. goto ring_free;
  1100. return 0;
  1101. ring_free:
  1102. /* Free Rx and Tx skb ring buffer and DMA buffer */
  1103. sh_eth_ring_free(ndev);
  1104. return -ENOMEM;
  1105. }
  1106. static int sh_eth_dev_init(struct net_device *ndev)
  1107. {
  1108. struct sh_eth_private *mdp = netdev_priv(ndev);
  1109. int ret;
  1110. /* Soft Reset */
  1111. ret = sh_eth_reset(ndev);
  1112. if (ret)
  1113. return ret;
  1114. if (mdp->cd->rmiimode)
  1115. sh_eth_write(ndev, 0x1, RMIIMODE);
  1116. /* Descriptor format */
  1117. sh_eth_ring_format(ndev);
  1118. if (mdp->cd->rpadir)
  1119. sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
  1120. /* all sh_eth int mask */
  1121. sh_eth_write(ndev, 0, EESIPR);
  1122. #if defined(__LITTLE_ENDIAN)
  1123. if (mdp->cd->hw_swap)
  1124. sh_eth_write(ndev, EDMR_EL, EDMR);
  1125. else
  1126. #endif
  1127. sh_eth_write(ndev, 0, EDMR);
  1128. /* FIFO size set */
  1129. sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
  1130. sh_eth_write(ndev, 0, TFTR);
  1131. /* Frame recv control (enable multiple-packets per rx irq) */
  1132. sh_eth_write(ndev, RMCR_RNC, RMCR);
  1133. sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
  1134. if (mdp->cd->bculr)
  1135. sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
  1136. sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
  1137. if (!mdp->cd->no_trimd)
  1138. sh_eth_write(ndev, 0, TRIMD);
  1139. /* Recv frame limit set register */
  1140. sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  1141. RFLR);
  1142. sh_eth_modify(ndev, EESR, 0, 0);
  1143. mdp->irq_enabled = true;
  1144. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1145. /* PAUSE Prohibition */
  1146. sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
  1147. ECMR_TE | ECMR_RE, ECMR);
  1148. if (mdp->cd->set_rate)
  1149. mdp->cd->set_rate(ndev);
  1150. /* E-MAC Status Register clear */
  1151. sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
  1152. /* E-MAC Interrupt Enable register */
  1153. sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
  1154. /* Set MAC address */
  1155. update_mac_address(ndev);
  1156. /* mask reset */
  1157. if (mdp->cd->apr)
  1158. sh_eth_write(ndev, APR_AP, APR);
  1159. if (mdp->cd->mpr)
  1160. sh_eth_write(ndev, MPR_MP, MPR);
  1161. if (mdp->cd->tpauser)
  1162. sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
  1163. /* Setting the Rx mode will start the Rx process. */
  1164. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1165. return ret;
  1166. }
  1167. static void sh_eth_dev_exit(struct net_device *ndev)
  1168. {
  1169. struct sh_eth_private *mdp = netdev_priv(ndev);
  1170. int i;
  1171. /* Deactivate all TX descriptors, so DMA should stop at next
  1172. * packet boundary if it's currently running
  1173. */
  1174. for (i = 0; i < mdp->num_tx_ring; i++)
  1175. mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
  1176. /* Disable TX FIFO egress to MAC */
  1177. sh_eth_rcv_snd_disable(ndev);
  1178. /* Stop RX DMA at next packet boundary */
  1179. sh_eth_write(ndev, 0, EDRRR);
  1180. /* Aside from TX DMA, we can't tell when the hardware is
  1181. * really stopped, so we need to reset to make sure.
  1182. * Before doing that, wait for long enough to *probably*
  1183. * finish transmitting the last packet and poll stats.
  1184. */
  1185. msleep(2); /* max frame time at 10 Mbps < 1250 us */
  1186. sh_eth_get_stats(ndev);
  1187. sh_eth_reset(ndev);
  1188. /* Set MAC address again */
  1189. update_mac_address(ndev);
  1190. }
  1191. /* Packet receive function */
  1192. static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
  1193. {
  1194. struct sh_eth_private *mdp = netdev_priv(ndev);
  1195. struct sh_eth_rxdesc *rxdesc;
  1196. int entry = mdp->cur_rx % mdp->num_rx_ring;
  1197. int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
  1198. int limit;
  1199. struct sk_buff *skb;
  1200. u32 desc_status;
  1201. int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
  1202. dma_addr_t dma_addr;
  1203. u16 pkt_len;
  1204. u32 buf_len;
  1205. boguscnt = min(boguscnt, *quota);
  1206. limit = boguscnt;
  1207. rxdesc = &mdp->rx_ring[entry];
  1208. while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
  1209. /* RACT bit must be checked before all the following reads */
  1210. dma_rmb();
  1211. desc_status = le32_to_cpu(rxdesc->status);
  1212. pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
  1213. if (--boguscnt < 0)
  1214. break;
  1215. netif_info(mdp, rx_status, ndev,
  1216. "rx entry %d status 0x%08x len %d\n",
  1217. entry, desc_status, pkt_len);
  1218. if (!(desc_status & RDFEND))
  1219. ndev->stats.rx_length_errors++;
  1220. /* In case of almost all GETHER/ETHERs, the Receive Frame State
  1221. * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
  1222. * bit 0. However, in case of the R8A7740 and R7S72100
  1223. * the RFS bits are from bit 25 to bit 16. So, the
  1224. * driver needs right shifting by 16.
  1225. */
  1226. if (mdp->cd->shift_rd0)
  1227. desc_status >>= 16;
  1228. skb = mdp->rx_skbuff[entry];
  1229. if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
  1230. RD_RFS5 | RD_RFS6 | RD_RFS10)) {
  1231. ndev->stats.rx_errors++;
  1232. if (desc_status & RD_RFS1)
  1233. ndev->stats.rx_crc_errors++;
  1234. if (desc_status & RD_RFS2)
  1235. ndev->stats.rx_frame_errors++;
  1236. if (desc_status & RD_RFS3)
  1237. ndev->stats.rx_length_errors++;
  1238. if (desc_status & RD_RFS4)
  1239. ndev->stats.rx_length_errors++;
  1240. if (desc_status & RD_RFS6)
  1241. ndev->stats.rx_missed_errors++;
  1242. if (desc_status & RD_RFS10)
  1243. ndev->stats.rx_over_errors++;
  1244. } else if (skb) {
  1245. dma_addr = le32_to_cpu(rxdesc->addr);
  1246. if (!mdp->cd->hw_swap)
  1247. sh_eth_soft_swap(
  1248. phys_to_virt(ALIGN(dma_addr, 4)),
  1249. pkt_len + 2);
  1250. mdp->rx_skbuff[entry] = NULL;
  1251. if (mdp->cd->rpadir)
  1252. skb_reserve(skb, NET_IP_ALIGN);
  1253. dma_unmap_single(&ndev->dev, dma_addr,
  1254. ALIGN(mdp->rx_buf_sz, 32),
  1255. DMA_FROM_DEVICE);
  1256. skb_put(skb, pkt_len);
  1257. skb->protocol = eth_type_trans(skb, ndev);
  1258. netif_receive_skb(skb);
  1259. ndev->stats.rx_packets++;
  1260. ndev->stats.rx_bytes += pkt_len;
  1261. if (desc_status & RD_RFS8)
  1262. ndev->stats.multicast++;
  1263. }
  1264. entry = (++mdp->cur_rx) % mdp->num_rx_ring;
  1265. rxdesc = &mdp->rx_ring[entry];
  1266. }
  1267. /* Refill the Rx ring buffers. */
  1268. for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
  1269. entry = mdp->dirty_rx % mdp->num_rx_ring;
  1270. rxdesc = &mdp->rx_ring[entry];
  1271. /* The size of the buffer is 32 byte boundary. */
  1272. buf_len = ALIGN(mdp->rx_buf_sz, 32);
  1273. rxdesc->len = cpu_to_le32(buf_len << 16);
  1274. if (mdp->rx_skbuff[entry] == NULL) {
  1275. skb = netdev_alloc_skb(ndev, skbuff_size);
  1276. if (skb == NULL)
  1277. break; /* Better luck next round. */
  1278. sh_eth_set_receive_align(skb);
  1279. dma_addr = dma_map_single(&ndev->dev, skb->data,
  1280. buf_len, DMA_FROM_DEVICE);
  1281. if (dma_mapping_error(&ndev->dev, dma_addr)) {
  1282. kfree_skb(skb);
  1283. break;
  1284. }
  1285. mdp->rx_skbuff[entry] = skb;
  1286. skb_checksum_none_assert(skb);
  1287. rxdesc->addr = cpu_to_le32(dma_addr);
  1288. }
  1289. dma_wmb(); /* RACT bit must be set after all the above writes */
  1290. if (entry >= mdp->num_rx_ring - 1)
  1291. rxdesc->status |=
  1292. cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
  1293. else
  1294. rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
  1295. }
  1296. /* Restart Rx engine if stopped. */
  1297. /* If we don't need to check status, don't. -KDU */
  1298. if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
  1299. /* fix the values for the next receiving if RDE is set */
  1300. if (intr_status & EESR_RDE &&
  1301. mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
  1302. u32 count = (sh_eth_read(ndev, RDFAR) -
  1303. sh_eth_read(ndev, RDLAR)) >> 4;
  1304. mdp->cur_rx = count;
  1305. mdp->dirty_rx = count;
  1306. }
  1307. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1308. }
  1309. *quota -= limit - boguscnt - 1;
  1310. return *quota <= 0;
  1311. }
  1312. static void sh_eth_rcv_snd_disable(struct net_device *ndev)
  1313. {
  1314. /* disable tx and rx */
  1315. sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
  1316. }
  1317. static void sh_eth_rcv_snd_enable(struct net_device *ndev)
  1318. {
  1319. /* enable tx and rx */
  1320. sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
  1321. }
  1322. /* error control function */
  1323. static void sh_eth_error(struct net_device *ndev, u32 intr_status)
  1324. {
  1325. struct sh_eth_private *mdp = netdev_priv(ndev);
  1326. u32 felic_stat;
  1327. u32 link_stat;
  1328. u32 mask;
  1329. if (intr_status & EESR_ECI) {
  1330. felic_stat = sh_eth_read(ndev, ECSR);
  1331. sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
  1332. if (felic_stat & ECSR_ICD)
  1333. ndev->stats.tx_carrier_errors++;
  1334. if (felic_stat & ECSR_LCHNG) {
  1335. /* Link Changed */
  1336. if (mdp->cd->no_psr || mdp->no_ether_link) {
  1337. goto ignore_link;
  1338. } else {
  1339. link_stat = (sh_eth_read(ndev, PSR));
  1340. if (mdp->ether_link_active_low)
  1341. link_stat = ~link_stat;
  1342. }
  1343. if (!(link_stat & PHY_ST_LINK)) {
  1344. sh_eth_rcv_snd_disable(ndev);
  1345. } else {
  1346. /* Link Up */
  1347. sh_eth_modify(ndev, EESIPR, DMAC_M_ECI, 0);
  1348. /* clear int */
  1349. sh_eth_modify(ndev, ECSR, 0, 0);
  1350. sh_eth_modify(ndev, EESIPR, DMAC_M_ECI,
  1351. DMAC_M_ECI);
  1352. /* enable tx and rx */
  1353. sh_eth_rcv_snd_enable(ndev);
  1354. }
  1355. }
  1356. }
  1357. ignore_link:
  1358. if (intr_status & EESR_TWB) {
  1359. /* Unused write back interrupt */
  1360. if (intr_status & EESR_TABT) { /* Transmit Abort int */
  1361. ndev->stats.tx_aborted_errors++;
  1362. netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
  1363. }
  1364. }
  1365. if (intr_status & EESR_RABT) {
  1366. /* Receive Abort int */
  1367. if (intr_status & EESR_RFRMER) {
  1368. /* Receive Frame Overflow int */
  1369. ndev->stats.rx_frame_errors++;
  1370. }
  1371. }
  1372. if (intr_status & EESR_TDE) {
  1373. /* Transmit Descriptor Empty int */
  1374. ndev->stats.tx_fifo_errors++;
  1375. netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
  1376. }
  1377. if (intr_status & EESR_TFE) {
  1378. /* FIFO under flow */
  1379. ndev->stats.tx_fifo_errors++;
  1380. netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
  1381. }
  1382. if (intr_status & EESR_RDE) {
  1383. /* Receive Descriptor Empty int */
  1384. ndev->stats.rx_over_errors++;
  1385. }
  1386. if (intr_status & EESR_RFE) {
  1387. /* Receive FIFO Overflow int */
  1388. ndev->stats.rx_fifo_errors++;
  1389. }
  1390. if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
  1391. /* Address Error */
  1392. ndev->stats.tx_fifo_errors++;
  1393. netif_err(mdp, tx_err, ndev, "Address Error\n");
  1394. }
  1395. mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
  1396. if (mdp->cd->no_ade)
  1397. mask &= ~EESR_ADE;
  1398. if (intr_status & mask) {
  1399. /* Tx error */
  1400. u32 edtrr = sh_eth_read(ndev, EDTRR);
  1401. /* dmesg */
  1402. netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
  1403. intr_status, mdp->cur_tx, mdp->dirty_tx,
  1404. (u32)ndev->state, edtrr);
  1405. /* dirty buffer free */
  1406. sh_eth_tx_free(ndev, true);
  1407. /* SH7712 BUG */
  1408. if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
  1409. /* tx dma start */
  1410. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  1411. }
  1412. /* wakeup */
  1413. netif_wake_queue(ndev);
  1414. }
  1415. }
  1416. static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
  1417. {
  1418. struct net_device *ndev = netdev;
  1419. struct sh_eth_private *mdp = netdev_priv(ndev);
  1420. struct sh_eth_cpu_data *cd = mdp->cd;
  1421. irqreturn_t ret = IRQ_NONE;
  1422. u32 intr_status, intr_enable;
  1423. spin_lock(&mdp->lock);
  1424. /* Get interrupt status */
  1425. intr_status = sh_eth_read(ndev, EESR);
  1426. /* Mask it with the interrupt mask, forcing ECI interrupt to be always
  1427. * enabled since it's the one that comes thru regardless of the mask,
  1428. * and we need to fully handle it in sh_eth_error() in order to quench
  1429. * it as it doesn't get cleared by just writing 1 to the ECI bit...
  1430. */
  1431. intr_enable = sh_eth_read(ndev, EESIPR);
  1432. intr_status &= intr_enable | DMAC_M_ECI;
  1433. if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
  1434. ret = IRQ_HANDLED;
  1435. else
  1436. goto out;
  1437. if (!likely(mdp->irq_enabled)) {
  1438. sh_eth_write(ndev, 0, EESIPR);
  1439. goto out;
  1440. }
  1441. if (intr_status & EESR_RX_CHECK) {
  1442. if (napi_schedule_prep(&mdp->napi)) {
  1443. /* Mask Rx interrupts */
  1444. sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
  1445. EESIPR);
  1446. __napi_schedule(&mdp->napi);
  1447. } else {
  1448. netdev_warn(ndev,
  1449. "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
  1450. intr_status, intr_enable);
  1451. }
  1452. }
  1453. /* Tx Check */
  1454. if (intr_status & cd->tx_check) {
  1455. /* Clear Tx interrupts */
  1456. sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
  1457. sh_eth_tx_free(ndev, true);
  1458. netif_wake_queue(ndev);
  1459. }
  1460. if (intr_status & cd->eesr_err_check) {
  1461. /* Clear error interrupts */
  1462. sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
  1463. sh_eth_error(ndev, intr_status);
  1464. }
  1465. out:
  1466. spin_unlock(&mdp->lock);
  1467. return ret;
  1468. }
  1469. static int sh_eth_poll(struct napi_struct *napi, int budget)
  1470. {
  1471. struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
  1472. napi);
  1473. struct net_device *ndev = napi->dev;
  1474. int quota = budget;
  1475. u32 intr_status;
  1476. for (;;) {
  1477. intr_status = sh_eth_read(ndev, EESR);
  1478. if (!(intr_status & EESR_RX_CHECK))
  1479. break;
  1480. /* Clear Rx interrupts */
  1481. sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
  1482. if (sh_eth_rx(ndev, intr_status, &quota))
  1483. goto out;
  1484. }
  1485. napi_complete(napi);
  1486. /* Reenable Rx interrupts */
  1487. if (mdp->irq_enabled)
  1488. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1489. out:
  1490. return budget - quota;
  1491. }
  1492. /* PHY state control function */
  1493. static void sh_eth_adjust_link(struct net_device *ndev)
  1494. {
  1495. struct sh_eth_private *mdp = netdev_priv(ndev);
  1496. struct phy_device *phydev = ndev->phydev;
  1497. int new_state = 0;
  1498. if (phydev->link) {
  1499. if (phydev->duplex != mdp->duplex) {
  1500. new_state = 1;
  1501. mdp->duplex = phydev->duplex;
  1502. if (mdp->cd->set_duplex)
  1503. mdp->cd->set_duplex(ndev);
  1504. }
  1505. if (phydev->speed != mdp->speed) {
  1506. new_state = 1;
  1507. mdp->speed = phydev->speed;
  1508. if (mdp->cd->set_rate)
  1509. mdp->cd->set_rate(ndev);
  1510. }
  1511. if (!mdp->link) {
  1512. sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
  1513. new_state = 1;
  1514. mdp->link = phydev->link;
  1515. if (mdp->cd->no_psr || mdp->no_ether_link)
  1516. sh_eth_rcv_snd_enable(ndev);
  1517. }
  1518. } else if (mdp->link) {
  1519. new_state = 1;
  1520. mdp->link = 0;
  1521. mdp->speed = 0;
  1522. mdp->duplex = -1;
  1523. if (mdp->cd->no_psr || mdp->no_ether_link)
  1524. sh_eth_rcv_snd_disable(ndev);
  1525. }
  1526. if (new_state && netif_msg_link(mdp))
  1527. phy_print_status(phydev);
  1528. }
  1529. /* PHY init function */
  1530. static int sh_eth_phy_init(struct net_device *ndev)
  1531. {
  1532. struct device_node *np = ndev->dev.parent->of_node;
  1533. struct sh_eth_private *mdp = netdev_priv(ndev);
  1534. struct phy_device *phydev;
  1535. mdp->link = 0;
  1536. mdp->speed = 0;
  1537. mdp->duplex = -1;
  1538. /* Try connect to PHY */
  1539. if (np) {
  1540. struct device_node *pn;
  1541. pn = of_parse_phandle(np, "phy-handle", 0);
  1542. phydev = of_phy_connect(ndev, pn,
  1543. sh_eth_adjust_link, 0,
  1544. mdp->phy_interface);
  1545. of_node_put(pn);
  1546. if (!phydev)
  1547. phydev = ERR_PTR(-ENOENT);
  1548. } else {
  1549. char phy_id[MII_BUS_ID_SIZE + 3];
  1550. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  1551. mdp->mii_bus->id, mdp->phy_id);
  1552. phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
  1553. mdp->phy_interface);
  1554. }
  1555. if (IS_ERR(phydev)) {
  1556. netdev_err(ndev, "failed to connect PHY\n");
  1557. return PTR_ERR(phydev);
  1558. }
  1559. phy_attached_info(phydev);
  1560. return 0;
  1561. }
  1562. /* PHY control start function */
  1563. static int sh_eth_phy_start(struct net_device *ndev)
  1564. {
  1565. int ret;
  1566. ret = sh_eth_phy_init(ndev);
  1567. if (ret)
  1568. return ret;
  1569. phy_start(ndev->phydev);
  1570. return 0;
  1571. }
  1572. static int sh_eth_get_link_ksettings(struct net_device *ndev,
  1573. struct ethtool_link_ksettings *cmd)
  1574. {
  1575. struct sh_eth_private *mdp = netdev_priv(ndev);
  1576. unsigned long flags;
  1577. int ret;
  1578. if (!ndev->phydev)
  1579. return -ENODEV;
  1580. spin_lock_irqsave(&mdp->lock, flags);
  1581. ret = phy_ethtool_ksettings_get(ndev->phydev, cmd);
  1582. spin_unlock_irqrestore(&mdp->lock, flags);
  1583. return ret;
  1584. }
  1585. static int sh_eth_set_link_ksettings(struct net_device *ndev,
  1586. const struct ethtool_link_ksettings *cmd)
  1587. {
  1588. struct sh_eth_private *mdp = netdev_priv(ndev);
  1589. unsigned long flags;
  1590. int ret;
  1591. if (!ndev->phydev)
  1592. return -ENODEV;
  1593. spin_lock_irqsave(&mdp->lock, flags);
  1594. /* disable tx and rx */
  1595. sh_eth_rcv_snd_disable(ndev);
  1596. ret = phy_ethtool_ksettings_set(ndev->phydev, cmd);
  1597. if (ret)
  1598. goto error_exit;
  1599. if (cmd->base.duplex == DUPLEX_FULL)
  1600. mdp->duplex = 1;
  1601. else
  1602. mdp->duplex = 0;
  1603. if (mdp->cd->set_duplex)
  1604. mdp->cd->set_duplex(ndev);
  1605. error_exit:
  1606. mdelay(1);
  1607. /* enable tx and rx */
  1608. sh_eth_rcv_snd_enable(ndev);
  1609. spin_unlock_irqrestore(&mdp->lock, flags);
  1610. return ret;
  1611. }
  1612. /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
  1613. * version must be bumped as well. Just adding registers up to that
  1614. * limit is fine, as long as the existing register indices don't
  1615. * change.
  1616. */
  1617. #define SH_ETH_REG_DUMP_VERSION 1
  1618. #define SH_ETH_REG_DUMP_MAX_REGS 256
  1619. static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
  1620. {
  1621. struct sh_eth_private *mdp = netdev_priv(ndev);
  1622. struct sh_eth_cpu_data *cd = mdp->cd;
  1623. u32 *valid_map;
  1624. size_t len;
  1625. BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
  1626. /* Dump starts with a bitmap that tells ethtool which
  1627. * registers are defined for this chip.
  1628. */
  1629. len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
  1630. if (buf) {
  1631. valid_map = buf;
  1632. buf += len;
  1633. } else {
  1634. valid_map = NULL;
  1635. }
  1636. /* Add a register to the dump, if it has a defined offset.
  1637. * This automatically skips most undefined registers, but for
  1638. * some it is also necessary to check a capability flag in
  1639. * struct sh_eth_cpu_data.
  1640. */
  1641. #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
  1642. #define add_reg_from(reg, read_expr) do { \
  1643. if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
  1644. if (buf) { \
  1645. mark_reg_valid(reg); \
  1646. *buf++ = read_expr; \
  1647. } \
  1648. ++len; \
  1649. } \
  1650. } while (0)
  1651. #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
  1652. #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
  1653. add_reg(EDSR);
  1654. add_reg(EDMR);
  1655. add_reg(EDTRR);
  1656. add_reg(EDRRR);
  1657. add_reg(EESR);
  1658. add_reg(EESIPR);
  1659. add_reg(TDLAR);
  1660. add_reg(TDFAR);
  1661. add_reg(TDFXR);
  1662. add_reg(TDFFR);
  1663. add_reg(RDLAR);
  1664. add_reg(RDFAR);
  1665. add_reg(RDFXR);
  1666. add_reg(RDFFR);
  1667. add_reg(TRSCER);
  1668. add_reg(RMFCR);
  1669. add_reg(TFTR);
  1670. add_reg(FDR);
  1671. add_reg(RMCR);
  1672. add_reg(TFUCR);
  1673. add_reg(RFOCR);
  1674. if (cd->rmiimode)
  1675. add_reg(RMIIMODE);
  1676. add_reg(FCFTR);
  1677. if (cd->rpadir)
  1678. add_reg(RPADIR);
  1679. if (!cd->no_trimd)
  1680. add_reg(TRIMD);
  1681. add_reg(ECMR);
  1682. add_reg(ECSR);
  1683. add_reg(ECSIPR);
  1684. add_reg(PIR);
  1685. if (!cd->no_psr)
  1686. add_reg(PSR);
  1687. add_reg(RDMLR);
  1688. add_reg(RFLR);
  1689. add_reg(IPGR);
  1690. if (cd->apr)
  1691. add_reg(APR);
  1692. if (cd->mpr)
  1693. add_reg(MPR);
  1694. add_reg(RFCR);
  1695. add_reg(RFCF);
  1696. if (cd->tpauser)
  1697. add_reg(TPAUSER);
  1698. add_reg(TPAUSECR);
  1699. add_reg(GECMR);
  1700. if (cd->bculr)
  1701. add_reg(BCULR);
  1702. add_reg(MAHR);
  1703. add_reg(MALR);
  1704. add_reg(TROCR);
  1705. add_reg(CDCR);
  1706. add_reg(LCCR);
  1707. add_reg(CNDCR);
  1708. add_reg(CEFCR);
  1709. add_reg(FRECR);
  1710. add_reg(TSFRCR);
  1711. add_reg(TLFRCR);
  1712. add_reg(CERCR);
  1713. add_reg(CEECR);
  1714. add_reg(MAFCR);
  1715. if (cd->rtrate)
  1716. add_reg(RTRATE);
  1717. if (cd->hw_crc)
  1718. add_reg(CSMR);
  1719. if (cd->select_mii)
  1720. add_reg(RMII_MII);
  1721. add_reg(ARSTR);
  1722. if (cd->tsu) {
  1723. add_tsu_reg(TSU_CTRST);
  1724. add_tsu_reg(TSU_FWEN0);
  1725. add_tsu_reg(TSU_FWEN1);
  1726. add_tsu_reg(TSU_FCM);
  1727. add_tsu_reg(TSU_BSYSL0);
  1728. add_tsu_reg(TSU_BSYSL1);
  1729. add_tsu_reg(TSU_PRISL0);
  1730. add_tsu_reg(TSU_PRISL1);
  1731. add_tsu_reg(TSU_FWSL0);
  1732. add_tsu_reg(TSU_FWSL1);
  1733. add_tsu_reg(TSU_FWSLC);
  1734. add_tsu_reg(TSU_QTAG0);
  1735. add_tsu_reg(TSU_QTAG1);
  1736. add_tsu_reg(TSU_QTAGM0);
  1737. add_tsu_reg(TSU_QTAGM1);
  1738. add_tsu_reg(TSU_FWSR);
  1739. add_tsu_reg(TSU_FWINMK);
  1740. add_tsu_reg(TSU_ADQT0);
  1741. add_tsu_reg(TSU_ADQT1);
  1742. add_tsu_reg(TSU_VTAG0);
  1743. add_tsu_reg(TSU_VTAG1);
  1744. add_tsu_reg(TSU_ADSBSY);
  1745. add_tsu_reg(TSU_TEN);
  1746. add_tsu_reg(TSU_POST1);
  1747. add_tsu_reg(TSU_POST2);
  1748. add_tsu_reg(TSU_POST3);
  1749. add_tsu_reg(TSU_POST4);
  1750. if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
  1751. /* This is the start of a table, not just a single
  1752. * register.
  1753. */
  1754. if (buf) {
  1755. unsigned int i;
  1756. mark_reg_valid(TSU_ADRH0);
  1757. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
  1758. *buf++ = ioread32(
  1759. mdp->tsu_addr +
  1760. mdp->reg_offset[TSU_ADRH0] +
  1761. i * 4);
  1762. }
  1763. len += SH_ETH_TSU_CAM_ENTRIES * 2;
  1764. }
  1765. }
  1766. #undef mark_reg_valid
  1767. #undef add_reg_from
  1768. #undef add_reg
  1769. #undef add_tsu_reg
  1770. return len * 4;
  1771. }
  1772. static int sh_eth_get_regs_len(struct net_device *ndev)
  1773. {
  1774. return __sh_eth_get_regs(ndev, NULL);
  1775. }
  1776. static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
  1777. void *buf)
  1778. {
  1779. struct sh_eth_private *mdp = netdev_priv(ndev);
  1780. regs->version = SH_ETH_REG_DUMP_VERSION;
  1781. pm_runtime_get_sync(&mdp->pdev->dev);
  1782. __sh_eth_get_regs(ndev, buf);
  1783. pm_runtime_put_sync(&mdp->pdev->dev);
  1784. }
  1785. static int sh_eth_nway_reset(struct net_device *ndev)
  1786. {
  1787. struct sh_eth_private *mdp = netdev_priv(ndev);
  1788. unsigned long flags;
  1789. int ret;
  1790. if (!ndev->phydev)
  1791. return -ENODEV;
  1792. spin_lock_irqsave(&mdp->lock, flags);
  1793. ret = phy_start_aneg(ndev->phydev);
  1794. spin_unlock_irqrestore(&mdp->lock, flags);
  1795. return ret;
  1796. }
  1797. static u32 sh_eth_get_msglevel(struct net_device *ndev)
  1798. {
  1799. struct sh_eth_private *mdp = netdev_priv(ndev);
  1800. return mdp->msg_enable;
  1801. }
  1802. static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
  1803. {
  1804. struct sh_eth_private *mdp = netdev_priv(ndev);
  1805. mdp->msg_enable = value;
  1806. }
  1807. static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
  1808. "rx_current", "tx_current",
  1809. "rx_dirty", "tx_dirty",
  1810. };
  1811. #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
  1812. static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
  1813. {
  1814. switch (sset) {
  1815. case ETH_SS_STATS:
  1816. return SH_ETH_STATS_LEN;
  1817. default:
  1818. return -EOPNOTSUPP;
  1819. }
  1820. }
  1821. static void sh_eth_get_ethtool_stats(struct net_device *ndev,
  1822. struct ethtool_stats *stats, u64 *data)
  1823. {
  1824. struct sh_eth_private *mdp = netdev_priv(ndev);
  1825. int i = 0;
  1826. /* device-specific stats */
  1827. data[i++] = mdp->cur_rx;
  1828. data[i++] = mdp->cur_tx;
  1829. data[i++] = mdp->dirty_rx;
  1830. data[i++] = mdp->dirty_tx;
  1831. }
  1832. static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1833. {
  1834. switch (stringset) {
  1835. case ETH_SS_STATS:
  1836. memcpy(data, *sh_eth_gstrings_stats,
  1837. sizeof(sh_eth_gstrings_stats));
  1838. break;
  1839. }
  1840. }
  1841. static void sh_eth_get_ringparam(struct net_device *ndev,
  1842. struct ethtool_ringparam *ring)
  1843. {
  1844. struct sh_eth_private *mdp = netdev_priv(ndev);
  1845. ring->rx_max_pending = RX_RING_MAX;
  1846. ring->tx_max_pending = TX_RING_MAX;
  1847. ring->rx_pending = mdp->num_rx_ring;
  1848. ring->tx_pending = mdp->num_tx_ring;
  1849. }
  1850. static int sh_eth_set_ringparam(struct net_device *ndev,
  1851. struct ethtool_ringparam *ring)
  1852. {
  1853. struct sh_eth_private *mdp = netdev_priv(ndev);
  1854. int ret;
  1855. if (ring->tx_pending > TX_RING_MAX ||
  1856. ring->rx_pending > RX_RING_MAX ||
  1857. ring->tx_pending < TX_RING_MIN ||
  1858. ring->rx_pending < RX_RING_MIN)
  1859. return -EINVAL;
  1860. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  1861. return -EINVAL;
  1862. if (netif_running(ndev)) {
  1863. netif_device_detach(ndev);
  1864. netif_tx_disable(ndev);
  1865. /* Serialise with the interrupt handler and NAPI, then
  1866. * disable interrupts. We have to clear the
  1867. * irq_enabled flag first to ensure that interrupts
  1868. * won't be re-enabled.
  1869. */
  1870. mdp->irq_enabled = false;
  1871. synchronize_irq(ndev->irq);
  1872. napi_synchronize(&mdp->napi);
  1873. sh_eth_write(ndev, 0x0000, EESIPR);
  1874. sh_eth_dev_exit(ndev);
  1875. /* Free all the skbuffs in the Rx queue and the DMA buffers. */
  1876. sh_eth_ring_free(ndev);
  1877. }
  1878. /* Set new parameters */
  1879. mdp->num_rx_ring = ring->rx_pending;
  1880. mdp->num_tx_ring = ring->tx_pending;
  1881. if (netif_running(ndev)) {
  1882. ret = sh_eth_ring_init(ndev);
  1883. if (ret < 0) {
  1884. netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
  1885. __func__);
  1886. return ret;
  1887. }
  1888. ret = sh_eth_dev_init(ndev);
  1889. if (ret < 0) {
  1890. netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
  1891. __func__);
  1892. return ret;
  1893. }
  1894. netif_device_attach(ndev);
  1895. }
  1896. return 0;
  1897. }
  1898. static const struct ethtool_ops sh_eth_ethtool_ops = {
  1899. .get_regs_len = sh_eth_get_regs_len,
  1900. .get_regs = sh_eth_get_regs,
  1901. .nway_reset = sh_eth_nway_reset,
  1902. .get_msglevel = sh_eth_get_msglevel,
  1903. .set_msglevel = sh_eth_set_msglevel,
  1904. .get_link = ethtool_op_get_link,
  1905. .get_strings = sh_eth_get_strings,
  1906. .get_ethtool_stats = sh_eth_get_ethtool_stats,
  1907. .get_sset_count = sh_eth_get_sset_count,
  1908. .get_ringparam = sh_eth_get_ringparam,
  1909. .set_ringparam = sh_eth_set_ringparam,
  1910. .get_link_ksettings = sh_eth_get_link_ksettings,
  1911. .set_link_ksettings = sh_eth_set_link_ksettings,
  1912. };
  1913. /* network device open function */
  1914. static int sh_eth_open(struct net_device *ndev)
  1915. {
  1916. struct sh_eth_private *mdp = netdev_priv(ndev);
  1917. int ret;
  1918. pm_runtime_get_sync(&mdp->pdev->dev);
  1919. napi_enable(&mdp->napi);
  1920. ret = request_irq(ndev->irq, sh_eth_interrupt,
  1921. mdp->cd->irq_flags, ndev->name, ndev);
  1922. if (ret) {
  1923. netdev_err(ndev, "Can not assign IRQ number\n");
  1924. goto out_napi_off;
  1925. }
  1926. /* Descriptor set */
  1927. ret = sh_eth_ring_init(ndev);
  1928. if (ret)
  1929. goto out_free_irq;
  1930. /* device init */
  1931. ret = sh_eth_dev_init(ndev);
  1932. if (ret)
  1933. goto out_free_irq;
  1934. /* PHY control start*/
  1935. ret = sh_eth_phy_start(ndev);
  1936. if (ret)
  1937. goto out_free_irq;
  1938. netif_start_queue(ndev);
  1939. mdp->is_opened = 1;
  1940. return ret;
  1941. out_free_irq:
  1942. free_irq(ndev->irq, ndev);
  1943. out_napi_off:
  1944. napi_disable(&mdp->napi);
  1945. pm_runtime_put_sync(&mdp->pdev->dev);
  1946. return ret;
  1947. }
  1948. /* Timeout function */
  1949. static void sh_eth_tx_timeout(struct net_device *ndev)
  1950. {
  1951. struct sh_eth_private *mdp = netdev_priv(ndev);
  1952. struct sh_eth_rxdesc *rxdesc;
  1953. int i;
  1954. netif_stop_queue(ndev);
  1955. netif_err(mdp, timer, ndev,
  1956. "transmit timed out, status %8.8x, resetting...\n",
  1957. sh_eth_read(ndev, EESR));
  1958. /* tx_errors count up */
  1959. ndev->stats.tx_errors++;
  1960. /* Free all the skbuffs in the Rx queue. */
  1961. for (i = 0; i < mdp->num_rx_ring; i++) {
  1962. rxdesc = &mdp->rx_ring[i];
  1963. rxdesc->status = cpu_to_le32(0);
  1964. rxdesc->addr = cpu_to_le32(0xBADF00D0);
  1965. dev_kfree_skb(mdp->rx_skbuff[i]);
  1966. mdp->rx_skbuff[i] = NULL;
  1967. }
  1968. for (i = 0; i < mdp->num_tx_ring; i++) {
  1969. dev_kfree_skb(mdp->tx_skbuff[i]);
  1970. mdp->tx_skbuff[i] = NULL;
  1971. }
  1972. /* device init */
  1973. sh_eth_dev_init(ndev);
  1974. netif_start_queue(ndev);
  1975. }
  1976. /* Packet transmit function */
  1977. static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1978. {
  1979. struct sh_eth_private *mdp = netdev_priv(ndev);
  1980. struct sh_eth_txdesc *txdesc;
  1981. dma_addr_t dma_addr;
  1982. u32 entry;
  1983. unsigned long flags;
  1984. spin_lock_irqsave(&mdp->lock, flags);
  1985. if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
  1986. if (!sh_eth_tx_free(ndev, true)) {
  1987. netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
  1988. netif_stop_queue(ndev);
  1989. spin_unlock_irqrestore(&mdp->lock, flags);
  1990. return NETDEV_TX_BUSY;
  1991. }
  1992. }
  1993. spin_unlock_irqrestore(&mdp->lock, flags);
  1994. if (skb_put_padto(skb, ETH_ZLEN))
  1995. return NETDEV_TX_OK;
  1996. entry = mdp->cur_tx % mdp->num_tx_ring;
  1997. mdp->tx_skbuff[entry] = skb;
  1998. txdesc = &mdp->tx_ring[entry];
  1999. /* soft swap. */
  2000. if (!mdp->cd->hw_swap)
  2001. sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
  2002. dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len,
  2003. DMA_TO_DEVICE);
  2004. if (dma_mapping_error(&ndev->dev, dma_addr)) {
  2005. kfree_skb(skb);
  2006. return NETDEV_TX_OK;
  2007. }
  2008. txdesc->addr = cpu_to_le32(dma_addr);
  2009. txdesc->len = cpu_to_le32(skb->len << 16);
  2010. dma_wmb(); /* TACT bit must be set after all the above writes */
  2011. if (entry >= mdp->num_tx_ring - 1)
  2012. txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
  2013. else
  2014. txdesc->status |= cpu_to_le32(TD_TACT);
  2015. mdp->cur_tx++;
  2016. if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
  2017. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  2018. return NETDEV_TX_OK;
  2019. }
  2020. /* The statistics registers have write-clear behaviour, which means we
  2021. * will lose any increment between the read and write. We mitigate
  2022. * this by only clearing when we read a non-zero value, so we will
  2023. * never falsely report a total of zero.
  2024. */
  2025. static void
  2026. sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
  2027. {
  2028. u32 delta = sh_eth_read(ndev, reg);
  2029. if (delta) {
  2030. *stat += delta;
  2031. sh_eth_write(ndev, 0, reg);
  2032. }
  2033. }
  2034. static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
  2035. {
  2036. struct sh_eth_private *mdp = netdev_priv(ndev);
  2037. if (sh_eth_is_rz_fast_ether(mdp))
  2038. return &ndev->stats;
  2039. if (!mdp->is_opened)
  2040. return &ndev->stats;
  2041. sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
  2042. sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
  2043. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
  2044. if (sh_eth_is_gether(mdp)) {
  2045. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2046. CERCR);
  2047. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2048. CEECR);
  2049. } else {
  2050. sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
  2051. CNDCR);
  2052. }
  2053. return &ndev->stats;
  2054. }
  2055. /* device close function */
  2056. static int sh_eth_close(struct net_device *ndev)
  2057. {
  2058. struct sh_eth_private *mdp = netdev_priv(ndev);
  2059. netif_stop_queue(ndev);
  2060. /* Serialise with the interrupt handler and NAPI, then disable
  2061. * interrupts. We have to clear the irq_enabled flag first to
  2062. * ensure that interrupts won't be re-enabled.
  2063. */
  2064. mdp->irq_enabled = false;
  2065. synchronize_irq(ndev->irq);
  2066. napi_disable(&mdp->napi);
  2067. sh_eth_write(ndev, 0x0000, EESIPR);
  2068. sh_eth_dev_exit(ndev);
  2069. /* PHY Disconnect */
  2070. if (ndev->phydev) {
  2071. phy_stop(ndev->phydev);
  2072. phy_disconnect(ndev->phydev);
  2073. }
  2074. free_irq(ndev->irq, ndev);
  2075. /* Free all the skbuffs in the Rx queue and the DMA buffer. */
  2076. sh_eth_ring_free(ndev);
  2077. pm_runtime_put_sync(&mdp->pdev->dev);
  2078. mdp->is_opened = 0;
  2079. return 0;
  2080. }
  2081. /* ioctl to device function */
  2082. static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  2083. {
  2084. struct phy_device *phydev = ndev->phydev;
  2085. if (!netif_running(ndev))
  2086. return -EINVAL;
  2087. if (!phydev)
  2088. return -ENODEV;
  2089. return phy_mii_ioctl(phydev, rq, cmd);
  2090. }
  2091. /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
  2092. static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
  2093. int entry)
  2094. {
  2095. return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
  2096. }
  2097. static u32 sh_eth_tsu_get_post_mask(int entry)
  2098. {
  2099. return 0x0f << (28 - ((entry % 8) * 4));
  2100. }
  2101. static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
  2102. {
  2103. return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
  2104. }
  2105. static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
  2106. int entry)
  2107. {
  2108. struct sh_eth_private *mdp = netdev_priv(ndev);
  2109. u32 tmp;
  2110. void *reg_offset;
  2111. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  2112. tmp = ioread32(reg_offset);
  2113. iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
  2114. }
  2115. static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
  2116. int entry)
  2117. {
  2118. struct sh_eth_private *mdp = netdev_priv(ndev);
  2119. u32 post_mask, ref_mask, tmp;
  2120. void *reg_offset;
  2121. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  2122. post_mask = sh_eth_tsu_get_post_mask(entry);
  2123. ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
  2124. tmp = ioread32(reg_offset);
  2125. iowrite32(tmp & ~post_mask, reg_offset);
  2126. /* If other port enables, the function returns "true" */
  2127. return tmp & ref_mask;
  2128. }
  2129. static int sh_eth_tsu_busy(struct net_device *ndev)
  2130. {
  2131. int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
  2132. struct sh_eth_private *mdp = netdev_priv(ndev);
  2133. while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
  2134. udelay(10);
  2135. timeout--;
  2136. if (timeout <= 0) {
  2137. netdev_err(ndev, "%s: timeout\n", __func__);
  2138. return -ETIMEDOUT;
  2139. }
  2140. }
  2141. return 0;
  2142. }
  2143. static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
  2144. const u8 *addr)
  2145. {
  2146. u32 val;
  2147. val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
  2148. iowrite32(val, reg);
  2149. if (sh_eth_tsu_busy(ndev) < 0)
  2150. return -EBUSY;
  2151. val = addr[4] << 8 | addr[5];
  2152. iowrite32(val, reg + 4);
  2153. if (sh_eth_tsu_busy(ndev) < 0)
  2154. return -EBUSY;
  2155. return 0;
  2156. }
  2157. static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
  2158. {
  2159. u32 val;
  2160. val = ioread32(reg);
  2161. addr[0] = (val >> 24) & 0xff;
  2162. addr[1] = (val >> 16) & 0xff;
  2163. addr[2] = (val >> 8) & 0xff;
  2164. addr[3] = val & 0xff;
  2165. val = ioread32(reg + 4);
  2166. addr[4] = (val >> 8) & 0xff;
  2167. addr[5] = val & 0xff;
  2168. }
  2169. static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
  2170. {
  2171. struct sh_eth_private *mdp = netdev_priv(ndev);
  2172. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2173. int i;
  2174. u8 c_addr[ETH_ALEN];
  2175. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  2176. sh_eth_tsu_read_entry(reg_offset, c_addr);
  2177. if (ether_addr_equal(addr, c_addr))
  2178. return i;
  2179. }
  2180. return -ENOENT;
  2181. }
  2182. static int sh_eth_tsu_find_empty(struct net_device *ndev)
  2183. {
  2184. u8 blank[ETH_ALEN];
  2185. int entry;
  2186. memset(blank, 0, sizeof(blank));
  2187. entry = sh_eth_tsu_find_entry(ndev, blank);
  2188. return (entry < 0) ? -ENOMEM : entry;
  2189. }
  2190. static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
  2191. int entry)
  2192. {
  2193. struct sh_eth_private *mdp = netdev_priv(ndev);
  2194. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2195. int ret;
  2196. u8 blank[ETH_ALEN];
  2197. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
  2198. ~(1 << (31 - entry)), TSU_TEN);
  2199. memset(blank, 0, sizeof(blank));
  2200. ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
  2201. if (ret < 0)
  2202. return ret;
  2203. return 0;
  2204. }
  2205. static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
  2206. {
  2207. struct sh_eth_private *mdp = netdev_priv(ndev);
  2208. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2209. int i, ret;
  2210. if (!mdp->cd->tsu)
  2211. return 0;
  2212. i = sh_eth_tsu_find_entry(ndev, addr);
  2213. if (i < 0) {
  2214. /* No entry found, create one */
  2215. i = sh_eth_tsu_find_empty(ndev);
  2216. if (i < 0)
  2217. return -ENOMEM;
  2218. ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
  2219. if (ret < 0)
  2220. return ret;
  2221. /* Enable the entry */
  2222. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
  2223. (1 << (31 - i)), TSU_TEN);
  2224. }
  2225. /* Entry found or created, enable POST */
  2226. sh_eth_tsu_enable_cam_entry_post(ndev, i);
  2227. return 0;
  2228. }
  2229. static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
  2230. {
  2231. struct sh_eth_private *mdp = netdev_priv(ndev);
  2232. int i, ret;
  2233. if (!mdp->cd->tsu)
  2234. return 0;
  2235. i = sh_eth_tsu_find_entry(ndev, addr);
  2236. if (i) {
  2237. /* Entry found */
  2238. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  2239. goto done;
  2240. /* Disable the entry if both ports was disabled */
  2241. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  2242. if (ret < 0)
  2243. return ret;
  2244. }
  2245. done:
  2246. return 0;
  2247. }
  2248. static int sh_eth_tsu_purge_all(struct net_device *ndev)
  2249. {
  2250. struct sh_eth_private *mdp = netdev_priv(ndev);
  2251. int i, ret;
  2252. if (!mdp->cd->tsu)
  2253. return 0;
  2254. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
  2255. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  2256. continue;
  2257. /* Disable the entry if both ports was disabled */
  2258. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  2259. if (ret < 0)
  2260. return ret;
  2261. }
  2262. return 0;
  2263. }
  2264. static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
  2265. {
  2266. struct sh_eth_private *mdp = netdev_priv(ndev);
  2267. u8 addr[ETH_ALEN];
  2268. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  2269. int i;
  2270. if (!mdp->cd->tsu)
  2271. return;
  2272. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  2273. sh_eth_tsu_read_entry(reg_offset, addr);
  2274. if (is_multicast_ether_addr(addr))
  2275. sh_eth_tsu_del_entry(ndev, addr);
  2276. }
  2277. }
  2278. /* Update promiscuous flag and multicast filter */
  2279. static void sh_eth_set_rx_mode(struct net_device *ndev)
  2280. {
  2281. struct sh_eth_private *mdp = netdev_priv(ndev);
  2282. u32 ecmr_bits;
  2283. int mcast_all = 0;
  2284. unsigned long flags;
  2285. spin_lock_irqsave(&mdp->lock, flags);
  2286. /* Initial condition is MCT = 1, PRM = 0.
  2287. * Depending on ndev->flags, set PRM or clear MCT
  2288. */
  2289. ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
  2290. if (mdp->cd->tsu)
  2291. ecmr_bits |= ECMR_MCT;
  2292. if (!(ndev->flags & IFF_MULTICAST)) {
  2293. sh_eth_tsu_purge_mcast(ndev);
  2294. mcast_all = 1;
  2295. }
  2296. if (ndev->flags & IFF_ALLMULTI) {
  2297. sh_eth_tsu_purge_mcast(ndev);
  2298. ecmr_bits &= ~ECMR_MCT;
  2299. mcast_all = 1;
  2300. }
  2301. if (ndev->flags & IFF_PROMISC) {
  2302. sh_eth_tsu_purge_all(ndev);
  2303. ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
  2304. } else if (mdp->cd->tsu) {
  2305. struct netdev_hw_addr *ha;
  2306. netdev_for_each_mc_addr(ha, ndev) {
  2307. if (mcast_all && is_multicast_ether_addr(ha->addr))
  2308. continue;
  2309. if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
  2310. if (!mcast_all) {
  2311. sh_eth_tsu_purge_mcast(ndev);
  2312. ecmr_bits &= ~ECMR_MCT;
  2313. mcast_all = 1;
  2314. }
  2315. }
  2316. }
  2317. }
  2318. /* update the ethernet mode */
  2319. sh_eth_write(ndev, ecmr_bits, ECMR);
  2320. spin_unlock_irqrestore(&mdp->lock, flags);
  2321. }
  2322. static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
  2323. {
  2324. if (!mdp->port)
  2325. return TSU_VTAG0;
  2326. else
  2327. return TSU_VTAG1;
  2328. }
  2329. static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
  2330. __be16 proto, u16 vid)
  2331. {
  2332. struct sh_eth_private *mdp = netdev_priv(ndev);
  2333. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  2334. if (unlikely(!mdp->cd->tsu))
  2335. return -EPERM;
  2336. /* No filtering if vid = 0 */
  2337. if (!vid)
  2338. return 0;
  2339. mdp->vlan_num_ids++;
  2340. /* The controller has one VLAN tag HW filter. So, if the filter is
  2341. * already enabled, the driver disables it and the filte
  2342. */
  2343. if (mdp->vlan_num_ids > 1) {
  2344. /* disable VLAN filter */
  2345. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2346. return 0;
  2347. }
  2348. sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
  2349. vtag_reg_index);
  2350. return 0;
  2351. }
  2352. static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
  2353. __be16 proto, u16 vid)
  2354. {
  2355. struct sh_eth_private *mdp = netdev_priv(ndev);
  2356. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  2357. if (unlikely(!mdp->cd->tsu))
  2358. return -EPERM;
  2359. /* No filtering if vid = 0 */
  2360. if (!vid)
  2361. return 0;
  2362. mdp->vlan_num_ids--;
  2363. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2364. return 0;
  2365. }
  2366. /* SuperH's TSU register init function */
  2367. static void sh_eth_tsu_init(struct sh_eth_private *mdp)
  2368. {
  2369. if (sh_eth_is_rz_fast_ether(mdp)) {
  2370. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2371. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL,
  2372. TSU_FWSLC); /* Enable POST registers */
  2373. return;
  2374. }
  2375. sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
  2376. sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
  2377. sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
  2378. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
  2379. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
  2380. sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
  2381. sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
  2382. sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
  2383. sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
  2384. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
  2385. if (sh_eth_is_gether(mdp)) {
  2386. sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
  2387. sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
  2388. } else {
  2389. sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
  2390. sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
  2391. }
  2392. sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
  2393. sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
  2394. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2395. sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
  2396. sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
  2397. sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
  2398. sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
  2399. }
  2400. /* MDIO bus release function */
  2401. static int sh_mdio_release(struct sh_eth_private *mdp)
  2402. {
  2403. /* unregister mdio bus */
  2404. mdiobus_unregister(mdp->mii_bus);
  2405. /* free bitbang info */
  2406. free_mdio_bitbang(mdp->mii_bus);
  2407. return 0;
  2408. }
  2409. /* MDIO bus init function */
  2410. static int sh_mdio_init(struct sh_eth_private *mdp,
  2411. struct sh_eth_plat_data *pd)
  2412. {
  2413. int ret;
  2414. struct bb_info *bitbang;
  2415. struct platform_device *pdev = mdp->pdev;
  2416. struct device *dev = &mdp->pdev->dev;
  2417. /* create bit control struct for PHY */
  2418. bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
  2419. if (!bitbang)
  2420. return -ENOMEM;
  2421. /* bitbang init */
  2422. bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
  2423. bitbang->set_gate = pd->set_mdio_gate;
  2424. bitbang->ctrl.ops = &bb_ops;
  2425. /* MII controller setting */
  2426. mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  2427. if (!mdp->mii_bus)
  2428. return -ENOMEM;
  2429. /* Hook up MII support for ethtool */
  2430. mdp->mii_bus->name = "sh_mii";
  2431. mdp->mii_bus->parent = dev;
  2432. snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  2433. pdev->name, pdev->id);
  2434. /* register MDIO bus */
  2435. if (dev->of_node) {
  2436. ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
  2437. } else {
  2438. if (pd->phy_irq > 0)
  2439. mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
  2440. ret = mdiobus_register(mdp->mii_bus);
  2441. }
  2442. if (ret)
  2443. goto out_free_bus;
  2444. return 0;
  2445. out_free_bus:
  2446. free_mdio_bitbang(mdp->mii_bus);
  2447. return ret;
  2448. }
  2449. static const u16 *sh_eth_get_register_offset(int register_type)
  2450. {
  2451. const u16 *reg_offset = NULL;
  2452. switch (register_type) {
  2453. case SH_ETH_REG_GIGABIT:
  2454. reg_offset = sh_eth_offset_gigabit;
  2455. break;
  2456. case SH_ETH_REG_FAST_RZ:
  2457. reg_offset = sh_eth_offset_fast_rz;
  2458. break;
  2459. case SH_ETH_REG_FAST_RCAR:
  2460. reg_offset = sh_eth_offset_fast_rcar;
  2461. break;
  2462. case SH_ETH_REG_FAST_SH4:
  2463. reg_offset = sh_eth_offset_fast_sh4;
  2464. break;
  2465. case SH_ETH_REG_FAST_SH3_SH2:
  2466. reg_offset = sh_eth_offset_fast_sh3_sh2;
  2467. break;
  2468. }
  2469. return reg_offset;
  2470. }
  2471. static const struct net_device_ops sh_eth_netdev_ops = {
  2472. .ndo_open = sh_eth_open,
  2473. .ndo_stop = sh_eth_close,
  2474. .ndo_start_xmit = sh_eth_start_xmit,
  2475. .ndo_get_stats = sh_eth_get_stats,
  2476. .ndo_set_rx_mode = sh_eth_set_rx_mode,
  2477. .ndo_tx_timeout = sh_eth_tx_timeout,
  2478. .ndo_do_ioctl = sh_eth_do_ioctl,
  2479. .ndo_validate_addr = eth_validate_addr,
  2480. .ndo_set_mac_address = eth_mac_addr,
  2481. .ndo_change_mtu = eth_change_mtu,
  2482. };
  2483. static const struct net_device_ops sh_eth_netdev_ops_tsu = {
  2484. .ndo_open = sh_eth_open,
  2485. .ndo_stop = sh_eth_close,
  2486. .ndo_start_xmit = sh_eth_start_xmit,
  2487. .ndo_get_stats = sh_eth_get_stats,
  2488. .ndo_set_rx_mode = sh_eth_set_rx_mode,
  2489. .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
  2490. .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
  2491. .ndo_tx_timeout = sh_eth_tx_timeout,
  2492. .ndo_do_ioctl = sh_eth_do_ioctl,
  2493. .ndo_validate_addr = eth_validate_addr,
  2494. .ndo_set_mac_address = eth_mac_addr,
  2495. .ndo_change_mtu = eth_change_mtu,
  2496. };
  2497. #ifdef CONFIG_OF
  2498. static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
  2499. {
  2500. struct device_node *np = dev->of_node;
  2501. struct sh_eth_plat_data *pdata;
  2502. const char *mac_addr;
  2503. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2504. if (!pdata)
  2505. return NULL;
  2506. pdata->phy_interface = of_get_phy_mode(np);
  2507. mac_addr = of_get_mac_address(np);
  2508. if (mac_addr)
  2509. memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
  2510. pdata->no_ether_link =
  2511. of_property_read_bool(np, "renesas,no-ether-link");
  2512. pdata->ether_link_active_low =
  2513. of_property_read_bool(np, "renesas,ether-link-active-low");
  2514. return pdata;
  2515. }
  2516. static const struct of_device_id sh_eth_match_table[] = {
  2517. { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
  2518. { .compatible = "renesas,ether-r8a7743", .data = &r8a779x_data },
  2519. { .compatible = "renesas,ether-r8a7745", .data = &r8a779x_data },
  2520. { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
  2521. { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
  2522. { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
  2523. { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
  2524. { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
  2525. { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
  2526. { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
  2527. { }
  2528. };
  2529. MODULE_DEVICE_TABLE(of, sh_eth_match_table);
  2530. #else
  2531. static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
  2532. {
  2533. return NULL;
  2534. }
  2535. #endif
  2536. static int sh_eth_drv_probe(struct platform_device *pdev)
  2537. {
  2538. struct resource *res;
  2539. struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
  2540. const struct platform_device_id *id = platform_get_device_id(pdev);
  2541. struct sh_eth_private *mdp;
  2542. struct net_device *ndev;
  2543. int ret, devno;
  2544. /* get base addr */
  2545. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2546. ndev = alloc_etherdev(sizeof(struct sh_eth_private));
  2547. if (!ndev)
  2548. return -ENOMEM;
  2549. pm_runtime_enable(&pdev->dev);
  2550. pm_runtime_get_sync(&pdev->dev);
  2551. devno = pdev->id;
  2552. if (devno < 0)
  2553. devno = 0;
  2554. ret = platform_get_irq(pdev, 0);
  2555. if (ret < 0)
  2556. goto out_release;
  2557. ndev->irq = ret;
  2558. SET_NETDEV_DEV(ndev, &pdev->dev);
  2559. mdp = netdev_priv(ndev);
  2560. mdp->num_tx_ring = TX_RING_SIZE;
  2561. mdp->num_rx_ring = RX_RING_SIZE;
  2562. mdp->addr = devm_ioremap_resource(&pdev->dev, res);
  2563. if (IS_ERR(mdp->addr)) {
  2564. ret = PTR_ERR(mdp->addr);
  2565. goto out_release;
  2566. }
  2567. ndev->base_addr = res->start;
  2568. spin_lock_init(&mdp->lock);
  2569. mdp->pdev = pdev;
  2570. if (pdev->dev.of_node)
  2571. pd = sh_eth_parse_dt(&pdev->dev);
  2572. if (!pd) {
  2573. dev_err(&pdev->dev, "no platform data\n");
  2574. ret = -EINVAL;
  2575. goto out_release;
  2576. }
  2577. /* get PHY ID */
  2578. mdp->phy_id = pd->phy;
  2579. mdp->phy_interface = pd->phy_interface;
  2580. mdp->no_ether_link = pd->no_ether_link;
  2581. mdp->ether_link_active_low = pd->ether_link_active_low;
  2582. /* set cpu data */
  2583. if (id)
  2584. mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
  2585. else
  2586. mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
  2587. mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
  2588. if (!mdp->reg_offset) {
  2589. dev_err(&pdev->dev, "Unknown register type (%d)\n",
  2590. mdp->cd->register_type);
  2591. ret = -EINVAL;
  2592. goto out_release;
  2593. }
  2594. sh_eth_set_default_cpu_data(mdp->cd);
  2595. /* set function */
  2596. if (mdp->cd->tsu)
  2597. ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
  2598. else
  2599. ndev->netdev_ops = &sh_eth_netdev_ops;
  2600. ndev->ethtool_ops = &sh_eth_ethtool_ops;
  2601. ndev->watchdog_timeo = TX_TIMEOUT;
  2602. /* debug message level */
  2603. mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
  2604. /* read and set MAC address */
  2605. read_mac_address(ndev, pd->mac_addr);
  2606. if (!is_valid_ether_addr(ndev->dev_addr)) {
  2607. dev_warn(&pdev->dev,
  2608. "no valid MAC address supplied, using a random one.\n");
  2609. eth_hw_addr_random(ndev);
  2610. }
  2611. /* ioremap the TSU registers */
  2612. if (mdp->cd->tsu) {
  2613. struct resource *rtsu;
  2614. rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2615. if (!rtsu) {
  2616. dev_err(&pdev->dev, "no TSU resource\n");
  2617. ret = -ENODEV;
  2618. goto out_release;
  2619. }
  2620. /* We can only request the TSU region for the first port
  2621. * of the two sharing this TSU for the probe to succeed...
  2622. */
  2623. if (devno % 2 == 0 &&
  2624. !devm_request_mem_region(&pdev->dev, rtsu->start,
  2625. resource_size(rtsu),
  2626. dev_name(&pdev->dev))) {
  2627. dev_err(&pdev->dev, "can't request TSU resource.\n");
  2628. ret = -EBUSY;
  2629. goto out_release;
  2630. }
  2631. mdp->tsu_addr = devm_ioremap(&pdev->dev, rtsu->start,
  2632. resource_size(rtsu));
  2633. if (!mdp->tsu_addr) {
  2634. dev_err(&pdev->dev, "TSU region ioremap() failed.\n");
  2635. ret = -ENOMEM;
  2636. goto out_release;
  2637. }
  2638. mdp->port = devno % 2;
  2639. ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
  2640. }
  2641. /* Need to init only the first port of the two sharing a TSU */
  2642. if (devno % 2 == 0) {
  2643. if (mdp->cd->chip_reset)
  2644. mdp->cd->chip_reset(ndev);
  2645. if (mdp->cd->tsu) {
  2646. /* TSU init (Init only)*/
  2647. sh_eth_tsu_init(mdp);
  2648. }
  2649. }
  2650. if (mdp->cd->rmiimode)
  2651. sh_eth_write(ndev, 0x1, RMIIMODE);
  2652. /* MDIO bus init */
  2653. ret = sh_mdio_init(mdp, pd);
  2654. if (ret) {
  2655. dev_err(&pdev->dev, "failed to initialise MDIO\n");
  2656. goto out_release;
  2657. }
  2658. netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
  2659. /* network device register */
  2660. ret = register_netdev(ndev);
  2661. if (ret)
  2662. goto out_napi_del;
  2663. /* print device information */
  2664. netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
  2665. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  2666. pm_runtime_put(&pdev->dev);
  2667. platform_set_drvdata(pdev, ndev);
  2668. return ret;
  2669. out_napi_del:
  2670. netif_napi_del(&mdp->napi);
  2671. sh_mdio_release(mdp);
  2672. out_release:
  2673. /* net_dev free */
  2674. if (ndev)
  2675. free_netdev(ndev);
  2676. pm_runtime_put(&pdev->dev);
  2677. pm_runtime_disable(&pdev->dev);
  2678. return ret;
  2679. }
  2680. static int sh_eth_drv_remove(struct platform_device *pdev)
  2681. {
  2682. struct net_device *ndev = platform_get_drvdata(pdev);
  2683. struct sh_eth_private *mdp = netdev_priv(ndev);
  2684. unregister_netdev(ndev);
  2685. netif_napi_del(&mdp->napi);
  2686. sh_mdio_release(mdp);
  2687. pm_runtime_disable(&pdev->dev);
  2688. free_netdev(ndev);
  2689. return 0;
  2690. }
  2691. #ifdef CONFIG_PM
  2692. #ifdef CONFIG_PM_SLEEP
  2693. static int sh_eth_suspend(struct device *dev)
  2694. {
  2695. struct net_device *ndev = dev_get_drvdata(dev);
  2696. int ret = 0;
  2697. if (netif_running(ndev)) {
  2698. netif_device_detach(ndev);
  2699. ret = sh_eth_close(ndev);
  2700. }
  2701. return ret;
  2702. }
  2703. static int sh_eth_resume(struct device *dev)
  2704. {
  2705. struct net_device *ndev = dev_get_drvdata(dev);
  2706. int ret = 0;
  2707. if (netif_running(ndev)) {
  2708. ret = sh_eth_open(ndev);
  2709. if (ret < 0)
  2710. return ret;
  2711. netif_device_attach(ndev);
  2712. }
  2713. return ret;
  2714. }
  2715. #endif
  2716. static int sh_eth_runtime_nop(struct device *dev)
  2717. {
  2718. /* Runtime PM callback shared between ->runtime_suspend()
  2719. * and ->runtime_resume(). Simply returns success.
  2720. *
  2721. * This driver re-initializes all registers after
  2722. * pm_runtime_get_sync() anyway so there is no need
  2723. * to save and restore registers here.
  2724. */
  2725. return 0;
  2726. }
  2727. static const struct dev_pm_ops sh_eth_dev_pm_ops = {
  2728. SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
  2729. SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
  2730. };
  2731. #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
  2732. #else
  2733. #define SH_ETH_PM_OPS NULL
  2734. #endif
  2735. static struct platform_device_id sh_eth_id_table[] = {
  2736. { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
  2737. { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
  2738. { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
  2739. { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
  2740. { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
  2741. { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
  2742. { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
  2743. { }
  2744. };
  2745. MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
  2746. static struct platform_driver sh_eth_driver = {
  2747. .probe = sh_eth_drv_probe,
  2748. .remove = sh_eth_drv_remove,
  2749. .id_table = sh_eth_id_table,
  2750. .driver = {
  2751. .name = CARDNAME,
  2752. .pm = SH_ETH_PM_OPS,
  2753. .of_match_table = of_match_ptr(sh_eth_match_table),
  2754. },
  2755. };
  2756. module_platform_driver(sh_eth_driver);
  2757. MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
  2758. MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
  2759. MODULE_LICENSE("GPL v2");