sh_eth.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721
  1. /*
  2. * SuperH Ethernet device driver
  3. *
  4. * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
  5. * Copyright (C) 2008-2013 Renesas Solutions Corp.
  6. * Copyright (C) 2013 Cogent Embedded, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * The full GNU General Public License is included in this distribution in
  21. * the file called "COPYING".
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mdio-bitbang.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/phy.h>
  35. #include <linux/cache.h>
  36. #include <linux/io.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/slab.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/clk.h>
  42. #include <linux/sh_eth.h>
  43. #include "sh_eth.h"
  44. #define SH_ETH_DEF_MSG_ENABLE \
  45. (NETIF_MSG_LINK | \
  46. NETIF_MSG_TIMER | \
  47. NETIF_MSG_RX_ERR| \
  48. NETIF_MSG_TX_ERR)
  49. static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
  50. [EDSR] = 0x0000,
  51. [EDMR] = 0x0400,
  52. [EDTRR] = 0x0408,
  53. [EDRRR] = 0x0410,
  54. [EESR] = 0x0428,
  55. [EESIPR] = 0x0430,
  56. [TDLAR] = 0x0010,
  57. [TDFAR] = 0x0014,
  58. [TDFXR] = 0x0018,
  59. [TDFFR] = 0x001c,
  60. [RDLAR] = 0x0030,
  61. [RDFAR] = 0x0034,
  62. [RDFXR] = 0x0038,
  63. [RDFFR] = 0x003c,
  64. [TRSCER] = 0x0438,
  65. [RMFCR] = 0x0440,
  66. [TFTR] = 0x0448,
  67. [FDR] = 0x0450,
  68. [RMCR] = 0x0458,
  69. [RPADIR] = 0x0460,
  70. [FCFTR] = 0x0468,
  71. [CSMR] = 0x04E4,
  72. [ECMR] = 0x0500,
  73. [ECSR] = 0x0510,
  74. [ECSIPR] = 0x0518,
  75. [PIR] = 0x0520,
  76. [PSR] = 0x0528,
  77. [PIPR] = 0x052c,
  78. [RFLR] = 0x0508,
  79. [APR] = 0x0554,
  80. [MPR] = 0x0558,
  81. [PFTCR] = 0x055c,
  82. [PFRCR] = 0x0560,
  83. [TPAUSER] = 0x0564,
  84. [GECMR] = 0x05b0,
  85. [BCULR] = 0x05b4,
  86. [MAHR] = 0x05c0,
  87. [MALR] = 0x05c8,
  88. [TROCR] = 0x0700,
  89. [CDCR] = 0x0708,
  90. [LCCR] = 0x0710,
  91. [CEFCR] = 0x0740,
  92. [FRECR] = 0x0748,
  93. [TSFRCR] = 0x0750,
  94. [TLFRCR] = 0x0758,
  95. [RFCR] = 0x0760,
  96. [CERCR] = 0x0768,
  97. [CEECR] = 0x0770,
  98. [MAFCR] = 0x0778,
  99. [RMII_MII] = 0x0790,
  100. [ARSTR] = 0x0000,
  101. [TSU_CTRST] = 0x0004,
  102. [TSU_FWEN0] = 0x0010,
  103. [TSU_FWEN1] = 0x0014,
  104. [TSU_FCM] = 0x0018,
  105. [TSU_BSYSL0] = 0x0020,
  106. [TSU_BSYSL1] = 0x0024,
  107. [TSU_PRISL0] = 0x0028,
  108. [TSU_PRISL1] = 0x002c,
  109. [TSU_FWSL0] = 0x0030,
  110. [TSU_FWSL1] = 0x0034,
  111. [TSU_FWSLC] = 0x0038,
  112. [TSU_QTAG0] = 0x0040,
  113. [TSU_QTAG1] = 0x0044,
  114. [TSU_FWSR] = 0x0050,
  115. [TSU_FWINMK] = 0x0054,
  116. [TSU_ADQT0] = 0x0048,
  117. [TSU_ADQT1] = 0x004c,
  118. [TSU_VTAG0] = 0x0058,
  119. [TSU_VTAG1] = 0x005c,
  120. [TSU_ADSBSY] = 0x0060,
  121. [TSU_TEN] = 0x0064,
  122. [TSU_POST1] = 0x0070,
  123. [TSU_POST2] = 0x0074,
  124. [TSU_POST3] = 0x0078,
  125. [TSU_POST4] = 0x007c,
  126. [TSU_ADRH0] = 0x0100,
  127. [TSU_ADRL0] = 0x0104,
  128. [TSU_ADRH31] = 0x01f8,
  129. [TSU_ADRL31] = 0x01fc,
  130. [TXNLCR0] = 0x0080,
  131. [TXALCR0] = 0x0084,
  132. [RXNLCR0] = 0x0088,
  133. [RXALCR0] = 0x008c,
  134. [FWNLCR0] = 0x0090,
  135. [FWALCR0] = 0x0094,
  136. [TXNLCR1] = 0x00a0,
  137. [TXALCR1] = 0x00a0,
  138. [RXNLCR1] = 0x00a8,
  139. [RXALCR1] = 0x00ac,
  140. [FWNLCR1] = 0x00b0,
  141. [FWALCR1] = 0x00b4,
  142. };
  143. static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
  144. [ECMR] = 0x0300,
  145. [RFLR] = 0x0308,
  146. [ECSR] = 0x0310,
  147. [ECSIPR] = 0x0318,
  148. [PIR] = 0x0320,
  149. [PSR] = 0x0328,
  150. [RDMLR] = 0x0340,
  151. [IPGR] = 0x0350,
  152. [APR] = 0x0354,
  153. [MPR] = 0x0358,
  154. [RFCF] = 0x0360,
  155. [TPAUSER] = 0x0364,
  156. [TPAUSECR] = 0x0368,
  157. [MAHR] = 0x03c0,
  158. [MALR] = 0x03c8,
  159. [TROCR] = 0x03d0,
  160. [CDCR] = 0x03d4,
  161. [LCCR] = 0x03d8,
  162. [CNDCR] = 0x03dc,
  163. [CEFCR] = 0x03e4,
  164. [FRECR] = 0x03e8,
  165. [TSFRCR] = 0x03ec,
  166. [TLFRCR] = 0x03f0,
  167. [RFCR] = 0x03f4,
  168. [MAFCR] = 0x03f8,
  169. [EDMR] = 0x0200,
  170. [EDTRR] = 0x0208,
  171. [EDRRR] = 0x0210,
  172. [TDLAR] = 0x0218,
  173. [RDLAR] = 0x0220,
  174. [EESR] = 0x0228,
  175. [EESIPR] = 0x0230,
  176. [TRSCER] = 0x0238,
  177. [RMFCR] = 0x0240,
  178. [TFTR] = 0x0248,
  179. [FDR] = 0x0250,
  180. [RMCR] = 0x0258,
  181. [TFUCR] = 0x0264,
  182. [RFOCR] = 0x0268,
  183. [FCFTR] = 0x0270,
  184. [TRIMD] = 0x027c,
  185. };
  186. static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
  187. [ECMR] = 0x0100,
  188. [RFLR] = 0x0108,
  189. [ECSR] = 0x0110,
  190. [ECSIPR] = 0x0118,
  191. [PIR] = 0x0120,
  192. [PSR] = 0x0128,
  193. [RDMLR] = 0x0140,
  194. [IPGR] = 0x0150,
  195. [APR] = 0x0154,
  196. [MPR] = 0x0158,
  197. [TPAUSER] = 0x0164,
  198. [RFCF] = 0x0160,
  199. [TPAUSECR] = 0x0168,
  200. [BCFRR] = 0x016c,
  201. [MAHR] = 0x01c0,
  202. [MALR] = 0x01c8,
  203. [TROCR] = 0x01d0,
  204. [CDCR] = 0x01d4,
  205. [LCCR] = 0x01d8,
  206. [CNDCR] = 0x01dc,
  207. [CEFCR] = 0x01e4,
  208. [FRECR] = 0x01e8,
  209. [TSFRCR] = 0x01ec,
  210. [TLFRCR] = 0x01f0,
  211. [RFCR] = 0x01f4,
  212. [MAFCR] = 0x01f8,
  213. [RTRATE] = 0x01fc,
  214. [EDMR] = 0x0000,
  215. [EDTRR] = 0x0008,
  216. [EDRRR] = 0x0010,
  217. [TDLAR] = 0x0018,
  218. [RDLAR] = 0x0020,
  219. [EESR] = 0x0028,
  220. [EESIPR] = 0x0030,
  221. [TRSCER] = 0x0038,
  222. [RMFCR] = 0x0040,
  223. [TFTR] = 0x0048,
  224. [FDR] = 0x0050,
  225. [RMCR] = 0x0058,
  226. [TFUCR] = 0x0064,
  227. [RFOCR] = 0x0068,
  228. [FCFTR] = 0x0070,
  229. [RPADIR] = 0x0078,
  230. [TRIMD] = 0x007c,
  231. [RBWAR] = 0x00c8,
  232. [RDFAR] = 0x00cc,
  233. [TBRAR] = 0x00d4,
  234. [TDFAR] = 0x00d8,
  235. };
  236. static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
  237. [ECMR] = 0x0160,
  238. [ECSR] = 0x0164,
  239. [ECSIPR] = 0x0168,
  240. [PIR] = 0x016c,
  241. [MAHR] = 0x0170,
  242. [MALR] = 0x0174,
  243. [RFLR] = 0x0178,
  244. [PSR] = 0x017c,
  245. [TROCR] = 0x0180,
  246. [CDCR] = 0x0184,
  247. [LCCR] = 0x0188,
  248. [CNDCR] = 0x018c,
  249. [CEFCR] = 0x0194,
  250. [FRECR] = 0x0198,
  251. [TSFRCR] = 0x019c,
  252. [TLFRCR] = 0x01a0,
  253. [RFCR] = 0x01a4,
  254. [MAFCR] = 0x01a8,
  255. [IPGR] = 0x01b4,
  256. [APR] = 0x01b8,
  257. [MPR] = 0x01bc,
  258. [TPAUSER] = 0x01c4,
  259. [BCFR] = 0x01cc,
  260. [ARSTR] = 0x0000,
  261. [TSU_CTRST] = 0x0004,
  262. [TSU_FWEN0] = 0x0010,
  263. [TSU_FWEN1] = 0x0014,
  264. [TSU_FCM] = 0x0018,
  265. [TSU_BSYSL0] = 0x0020,
  266. [TSU_BSYSL1] = 0x0024,
  267. [TSU_PRISL0] = 0x0028,
  268. [TSU_PRISL1] = 0x002c,
  269. [TSU_FWSL0] = 0x0030,
  270. [TSU_FWSL1] = 0x0034,
  271. [TSU_FWSLC] = 0x0038,
  272. [TSU_QTAGM0] = 0x0040,
  273. [TSU_QTAGM1] = 0x0044,
  274. [TSU_ADQT0] = 0x0048,
  275. [TSU_ADQT1] = 0x004c,
  276. [TSU_FWSR] = 0x0050,
  277. [TSU_FWINMK] = 0x0054,
  278. [TSU_ADSBSY] = 0x0060,
  279. [TSU_TEN] = 0x0064,
  280. [TSU_POST1] = 0x0070,
  281. [TSU_POST2] = 0x0074,
  282. [TSU_POST3] = 0x0078,
  283. [TSU_POST4] = 0x007c,
  284. [TXNLCR0] = 0x0080,
  285. [TXALCR0] = 0x0084,
  286. [RXNLCR0] = 0x0088,
  287. [RXALCR0] = 0x008c,
  288. [FWNLCR0] = 0x0090,
  289. [FWALCR0] = 0x0094,
  290. [TXNLCR1] = 0x00a0,
  291. [TXALCR1] = 0x00a0,
  292. [RXNLCR1] = 0x00a8,
  293. [RXALCR1] = 0x00ac,
  294. [FWNLCR1] = 0x00b0,
  295. [FWALCR1] = 0x00b4,
  296. [TSU_ADRH0] = 0x0100,
  297. [TSU_ADRL0] = 0x0104,
  298. [TSU_ADRL31] = 0x01fc,
  299. };
  300. static int sh_eth_is_gether(struct sh_eth_private *mdp)
  301. {
  302. if (mdp->reg_offset == sh_eth_offset_gigabit)
  303. return 1;
  304. else
  305. return 0;
  306. }
  307. static void __maybe_unused sh_eth_select_mii(struct net_device *ndev)
  308. {
  309. u32 value = 0x0;
  310. struct sh_eth_private *mdp = netdev_priv(ndev);
  311. switch (mdp->phy_interface) {
  312. case PHY_INTERFACE_MODE_GMII:
  313. value = 0x2;
  314. break;
  315. case PHY_INTERFACE_MODE_MII:
  316. value = 0x1;
  317. break;
  318. case PHY_INTERFACE_MODE_RMII:
  319. value = 0x0;
  320. break;
  321. default:
  322. pr_warn("PHY interface mode was not setup. Set to MII.\n");
  323. value = 0x1;
  324. break;
  325. }
  326. sh_eth_write(ndev, value, RMII_MII);
  327. }
  328. static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev)
  329. {
  330. struct sh_eth_private *mdp = netdev_priv(ndev);
  331. if (mdp->duplex) /* Full */
  332. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
  333. else /* Half */
  334. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
  335. }
  336. /* There is CPU dependent code */
  337. #if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
  338. static void sh_eth_set_rate(struct net_device *ndev)
  339. {
  340. struct sh_eth_private *mdp = netdev_priv(ndev);
  341. switch (mdp->speed) {
  342. case 10: /* 10BASE */
  343. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
  344. break;
  345. case 100:/* 100BASE */
  346. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. /* R8A7778/9 */
  353. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  354. .set_duplex = sh_eth_set_duplex,
  355. .set_rate = sh_eth_set_rate,
  356. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  357. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  358. .eesipr_value = 0x01ff009f,
  359. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  360. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  361. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  362. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  363. .apr = 1,
  364. .mpr = 1,
  365. .tpauser = 1,
  366. .hw_swap = 1,
  367. };
  368. #elif defined(CONFIG_CPU_SUBTYPE_SH7724)
  369. static void sh_eth_set_rate(struct net_device *ndev)
  370. {
  371. struct sh_eth_private *mdp = netdev_priv(ndev);
  372. switch (mdp->speed) {
  373. case 10: /* 10BASE */
  374. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
  375. break;
  376. case 100:/* 100BASE */
  377. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. /* SH7724 */
  384. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  385. .set_duplex = sh_eth_set_duplex,
  386. .set_rate = sh_eth_set_rate,
  387. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  388. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  389. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
  390. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  391. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  392. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  393. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  394. .apr = 1,
  395. .mpr = 1,
  396. .tpauser = 1,
  397. .hw_swap = 1,
  398. .rpadir = 1,
  399. .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
  400. };
  401. #elif defined(CONFIG_CPU_SUBTYPE_SH7757)
  402. #define SH_ETH_HAS_BOTH_MODULES 1
  403. static void sh_eth_set_rate(struct net_device *ndev)
  404. {
  405. struct sh_eth_private *mdp = netdev_priv(ndev);
  406. switch (mdp->speed) {
  407. case 10: /* 10BASE */
  408. sh_eth_write(ndev, 0, RTRATE);
  409. break;
  410. case 100:/* 100BASE */
  411. sh_eth_write(ndev, 1, RTRATE);
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. /* SH7757 */
  418. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  419. .set_duplex = sh_eth_set_duplex,
  420. .set_rate = sh_eth_set_rate,
  421. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  422. .rmcr_value = 0x00000001,
  423. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  424. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  425. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  426. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  427. .irq_flags = IRQF_SHARED,
  428. .apr = 1,
  429. .mpr = 1,
  430. .tpauser = 1,
  431. .hw_swap = 1,
  432. .no_ade = 1,
  433. .rpadir = 1,
  434. .rpadir_value = 2 << 16,
  435. };
  436. #define SH_GIGA_ETH_BASE 0xfee00000
  437. #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
  438. #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
  439. static void sh_eth_chip_reset_giga(struct net_device *ndev)
  440. {
  441. int i;
  442. unsigned long mahr[2], malr[2];
  443. /* save MAHR and MALR */
  444. for (i = 0; i < 2; i++) {
  445. malr[i] = ioread32((void *)GIGA_MALR(i));
  446. mahr[i] = ioread32((void *)GIGA_MAHR(i));
  447. }
  448. /* reset device */
  449. iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
  450. mdelay(1);
  451. /* restore MAHR and MALR */
  452. for (i = 0; i < 2; i++) {
  453. iowrite32(malr[i], (void *)GIGA_MALR(i));
  454. iowrite32(mahr[i], (void *)GIGA_MAHR(i));
  455. }
  456. }
  457. static void sh_eth_set_rate_giga(struct net_device *ndev)
  458. {
  459. struct sh_eth_private *mdp = netdev_priv(ndev);
  460. switch (mdp->speed) {
  461. case 10: /* 10BASE */
  462. sh_eth_write(ndev, 0x00000000, GECMR);
  463. break;
  464. case 100:/* 100BASE */
  465. sh_eth_write(ndev, 0x00000010, GECMR);
  466. break;
  467. case 1000: /* 1000BASE */
  468. sh_eth_write(ndev, 0x00000020, GECMR);
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. /* SH7757(GETHERC) */
  475. static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
  476. .chip_reset = sh_eth_chip_reset_giga,
  477. .set_duplex = sh_eth_set_duplex,
  478. .set_rate = sh_eth_set_rate_giga,
  479. .ecsr_value = ECSR_ICD | ECSR_MPD,
  480. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  481. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  482. .tx_check = EESR_TC1 | EESR_FTC,
  483. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  484. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  485. EESR_ECI,
  486. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  487. EESR_TFE,
  488. .fdr_value = 0x0000072f,
  489. .rmcr_value = 0x00000001,
  490. .irq_flags = IRQF_SHARED,
  491. .apr = 1,
  492. .mpr = 1,
  493. .tpauser = 1,
  494. .bculr = 1,
  495. .hw_swap = 1,
  496. .rpadir = 1,
  497. .rpadir_value = 2 << 16,
  498. .no_trimd = 1,
  499. .no_ade = 1,
  500. .tsu = 1,
  501. };
  502. static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
  503. {
  504. if (sh_eth_is_gether(mdp))
  505. return &sh_eth_my_cpu_data_giga;
  506. else
  507. return &sh_eth_my_cpu_data;
  508. }
  509. #elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
  510. static void sh_eth_chip_reset(struct net_device *ndev)
  511. {
  512. struct sh_eth_private *mdp = netdev_priv(ndev);
  513. /* reset device */
  514. sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
  515. mdelay(1);
  516. }
  517. static void sh_eth_set_rate(struct net_device *ndev)
  518. {
  519. struct sh_eth_private *mdp = netdev_priv(ndev);
  520. switch (mdp->speed) {
  521. case 10: /* 10BASE */
  522. sh_eth_write(ndev, GECMR_10, GECMR);
  523. break;
  524. case 100:/* 100BASE */
  525. sh_eth_write(ndev, GECMR_100, GECMR);
  526. break;
  527. case 1000: /* 1000BASE */
  528. sh_eth_write(ndev, GECMR_1000, GECMR);
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. /* sh7763 */
  535. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  536. .chip_reset = sh_eth_chip_reset,
  537. .set_duplex = sh_eth_set_duplex,
  538. .set_rate = sh_eth_set_rate,
  539. .ecsr_value = ECSR_ICD | ECSR_MPD,
  540. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  541. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  542. .tx_check = EESR_TC1 | EESR_FTC,
  543. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  544. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  545. EESR_ECI,
  546. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  547. EESR_TFE,
  548. .apr = 1,
  549. .mpr = 1,
  550. .tpauser = 1,
  551. .bculr = 1,
  552. .hw_swap = 1,
  553. .no_trimd = 1,
  554. .no_ade = 1,
  555. .tsu = 1,
  556. #if defined(CONFIG_CPU_SUBTYPE_SH7734)
  557. .hw_crc = 1,
  558. .select_mii = 1,
  559. #else
  560. .irq_flags = IRQF_SHARED,
  561. #endif
  562. };
  563. #endif
  564. static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
  565. {
  566. struct sh_eth_private *mdp = netdev_priv(ndev);
  567. /* reset device */
  568. sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
  569. mdelay(1);
  570. sh_eth_select_mii(ndev);
  571. }
  572. static void sh_eth_set_rate_gether(struct net_device *ndev)
  573. {
  574. struct sh_eth_private *mdp = netdev_priv(ndev);
  575. switch (mdp->speed) {
  576. case 10: /* 10BASE */
  577. sh_eth_write(ndev, GECMR_10, GECMR);
  578. break;
  579. case 100:/* 100BASE */
  580. sh_eth_write(ndev, GECMR_100, GECMR);
  581. break;
  582. case 1000: /* 1000BASE */
  583. sh_eth_write(ndev, GECMR_1000, GECMR);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. /* R8A7740 */
  590. static struct sh_eth_cpu_data r8a7740_data = {
  591. .chip_reset = sh_eth_chip_reset_r8a7740,
  592. .set_duplex = sh_eth_set_duplex,
  593. .set_rate = sh_eth_set_rate_gether,
  594. .ecsr_value = ECSR_ICD | ECSR_MPD,
  595. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  596. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  597. .tx_check = EESR_TC1 | EESR_FTC,
  598. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  599. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  600. EESR_ECI,
  601. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  602. EESR_TFE,
  603. .apr = 1,
  604. .mpr = 1,
  605. .tpauser = 1,
  606. .bculr = 1,
  607. .hw_swap = 1,
  608. .no_trimd = 1,
  609. .no_ade = 1,
  610. .tsu = 1,
  611. .select_mii = 1,
  612. };
  613. static struct sh_eth_cpu_data sh7619_data = {
  614. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  615. .apr = 1,
  616. .mpr = 1,
  617. .tpauser = 1,
  618. .hw_swap = 1,
  619. };
  620. static struct sh_eth_cpu_data sh771x_data = {
  621. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  622. .tsu = 1,
  623. };
  624. static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
  625. {
  626. if (!cd->ecsr_value)
  627. cd->ecsr_value = DEFAULT_ECSR_INIT;
  628. if (!cd->ecsipr_value)
  629. cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
  630. if (!cd->fcftr_value)
  631. cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
  632. DEFAULT_FIFO_F_D_RFD;
  633. if (!cd->fdr_value)
  634. cd->fdr_value = DEFAULT_FDR_INIT;
  635. if (!cd->rmcr_value)
  636. cd->rmcr_value = DEFAULT_RMCR_VALUE;
  637. if (!cd->tx_check)
  638. cd->tx_check = DEFAULT_TX_CHECK;
  639. if (!cd->eesr_err_check)
  640. cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
  641. if (!cd->tx_error_check)
  642. cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
  643. }
  644. static int sh_eth_check_reset(struct net_device *ndev)
  645. {
  646. int ret = 0;
  647. int cnt = 100;
  648. while (cnt > 0) {
  649. if (!(sh_eth_read(ndev, EDMR) & 0x3))
  650. break;
  651. mdelay(1);
  652. cnt--;
  653. }
  654. if (cnt < 0) {
  655. pr_err("Device reset fail\n");
  656. ret = -ETIMEDOUT;
  657. }
  658. return ret;
  659. }
  660. static int sh_eth_reset(struct net_device *ndev)
  661. {
  662. struct sh_eth_private *mdp = netdev_priv(ndev);
  663. int ret = 0;
  664. if (sh_eth_is_gether(mdp)) {
  665. sh_eth_write(ndev, EDSR_ENALL, EDSR);
  666. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
  667. EDMR);
  668. ret = sh_eth_check_reset(ndev);
  669. if (ret)
  670. goto out;
  671. /* Table Init */
  672. sh_eth_write(ndev, 0x0, TDLAR);
  673. sh_eth_write(ndev, 0x0, TDFAR);
  674. sh_eth_write(ndev, 0x0, TDFXR);
  675. sh_eth_write(ndev, 0x0, TDFFR);
  676. sh_eth_write(ndev, 0x0, RDLAR);
  677. sh_eth_write(ndev, 0x0, RDFAR);
  678. sh_eth_write(ndev, 0x0, RDFXR);
  679. sh_eth_write(ndev, 0x0, RDFFR);
  680. /* Reset HW CRC register */
  681. if (mdp->cd->hw_crc)
  682. sh_eth_write(ndev, 0x0, CSMR);
  683. /* Select MII mode */
  684. if (mdp->cd->select_mii)
  685. sh_eth_select_mii(ndev);
  686. } else {
  687. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
  688. EDMR);
  689. mdelay(3);
  690. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
  691. EDMR);
  692. }
  693. out:
  694. return ret;
  695. }
  696. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  697. static void sh_eth_set_receive_align(struct sk_buff *skb)
  698. {
  699. int reserve;
  700. reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
  701. if (reserve)
  702. skb_reserve(skb, reserve);
  703. }
  704. #else
  705. static void sh_eth_set_receive_align(struct sk_buff *skb)
  706. {
  707. skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
  708. }
  709. #endif
  710. /* CPU <-> EDMAC endian convert */
  711. static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
  712. {
  713. switch (mdp->edmac_endian) {
  714. case EDMAC_LITTLE_ENDIAN:
  715. return cpu_to_le32(x);
  716. case EDMAC_BIG_ENDIAN:
  717. return cpu_to_be32(x);
  718. }
  719. return x;
  720. }
  721. static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
  722. {
  723. switch (mdp->edmac_endian) {
  724. case EDMAC_LITTLE_ENDIAN:
  725. return le32_to_cpu(x);
  726. case EDMAC_BIG_ENDIAN:
  727. return be32_to_cpu(x);
  728. }
  729. return x;
  730. }
  731. /*
  732. * Program the hardware MAC address from dev->dev_addr.
  733. */
  734. static void update_mac_address(struct net_device *ndev)
  735. {
  736. sh_eth_write(ndev,
  737. (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
  738. (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
  739. sh_eth_write(ndev,
  740. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
  741. }
  742. /*
  743. * Get MAC address from SuperH MAC address register
  744. *
  745. * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
  746. * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
  747. * When you want use this device, you must set MAC address in bootloader.
  748. *
  749. */
  750. static void read_mac_address(struct net_device *ndev, unsigned char *mac)
  751. {
  752. if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
  753. memcpy(ndev->dev_addr, mac, 6);
  754. } else {
  755. ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
  756. ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
  757. ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
  758. ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
  759. ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
  760. ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
  761. }
  762. }
  763. static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
  764. {
  765. if (sh_eth_is_gether(mdp))
  766. return EDTRR_TRNS_GETHER;
  767. else
  768. return EDTRR_TRNS_ETHER;
  769. }
  770. struct bb_info {
  771. void (*set_gate)(void *addr);
  772. struct mdiobb_ctrl ctrl;
  773. void *addr;
  774. u32 mmd_msk;/* MMD */
  775. u32 mdo_msk;
  776. u32 mdi_msk;
  777. u32 mdc_msk;
  778. };
  779. /* PHY bit set */
  780. static void bb_set(void *addr, u32 msk)
  781. {
  782. iowrite32(ioread32(addr) | msk, addr);
  783. }
  784. /* PHY bit clear */
  785. static void bb_clr(void *addr, u32 msk)
  786. {
  787. iowrite32((ioread32(addr) & ~msk), addr);
  788. }
  789. /* PHY bit read */
  790. static int bb_read(void *addr, u32 msk)
  791. {
  792. return (ioread32(addr) & msk) != 0;
  793. }
  794. /* Data I/O pin control */
  795. static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  796. {
  797. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  798. if (bitbang->set_gate)
  799. bitbang->set_gate(bitbang->addr);
  800. if (bit)
  801. bb_set(bitbang->addr, bitbang->mmd_msk);
  802. else
  803. bb_clr(bitbang->addr, bitbang->mmd_msk);
  804. }
  805. /* Set bit data*/
  806. static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
  807. {
  808. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  809. if (bitbang->set_gate)
  810. bitbang->set_gate(bitbang->addr);
  811. if (bit)
  812. bb_set(bitbang->addr, bitbang->mdo_msk);
  813. else
  814. bb_clr(bitbang->addr, bitbang->mdo_msk);
  815. }
  816. /* Get bit data*/
  817. static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
  818. {
  819. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  820. if (bitbang->set_gate)
  821. bitbang->set_gate(bitbang->addr);
  822. return bb_read(bitbang->addr, bitbang->mdi_msk);
  823. }
  824. /* MDC pin control */
  825. static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  826. {
  827. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  828. if (bitbang->set_gate)
  829. bitbang->set_gate(bitbang->addr);
  830. if (bit)
  831. bb_set(bitbang->addr, bitbang->mdc_msk);
  832. else
  833. bb_clr(bitbang->addr, bitbang->mdc_msk);
  834. }
  835. /* mdio bus control struct */
  836. static struct mdiobb_ops bb_ops = {
  837. .owner = THIS_MODULE,
  838. .set_mdc = sh_mdc_ctrl,
  839. .set_mdio_dir = sh_mmd_ctrl,
  840. .set_mdio_data = sh_set_mdio,
  841. .get_mdio_data = sh_get_mdio,
  842. };
  843. /* free skb and descriptor buffer */
  844. static void sh_eth_ring_free(struct net_device *ndev)
  845. {
  846. struct sh_eth_private *mdp = netdev_priv(ndev);
  847. int i;
  848. /* Free Rx skb ringbuffer */
  849. if (mdp->rx_skbuff) {
  850. for (i = 0; i < mdp->num_rx_ring; i++) {
  851. if (mdp->rx_skbuff[i])
  852. dev_kfree_skb(mdp->rx_skbuff[i]);
  853. }
  854. }
  855. kfree(mdp->rx_skbuff);
  856. mdp->rx_skbuff = NULL;
  857. /* Free Tx skb ringbuffer */
  858. if (mdp->tx_skbuff) {
  859. for (i = 0; i < mdp->num_tx_ring; i++) {
  860. if (mdp->tx_skbuff[i])
  861. dev_kfree_skb(mdp->tx_skbuff[i]);
  862. }
  863. }
  864. kfree(mdp->tx_skbuff);
  865. mdp->tx_skbuff = NULL;
  866. }
  867. /* format skb and descriptor buffer */
  868. static void sh_eth_ring_format(struct net_device *ndev)
  869. {
  870. struct sh_eth_private *mdp = netdev_priv(ndev);
  871. int i;
  872. struct sk_buff *skb;
  873. struct sh_eth_rxdesc *rxdesc = NULL;
  874. struct sh_eth_txdesc *txdesc = NULL;
  875. int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
  876. int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
  877. mdp->cur_rx = mdp->cur_tx = 0;
  878. mdp->dirty_rx = mdp->dirty_tx = 0;
  879. memset(mdp->rx_ring, 0, rx_ringsize);
  880. /* build Rx ring buffer */
  881. for (i = 0; i < mdp->num_rx_ring; i++) {
  882. /* skb */
  883. mdp->rx_skbuff[i] = NULL;
  884. skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
  885. mdp->rx_skbuff[i] = skb;
  886. if (skb == NULL)
  887. break;
  888. dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
  889. DMA_FROM_DEVICE);
  890. sh_eth_set_receive_align(skb);
  891. /* RX descriptor */
  892. rxdesc = &mdp->rx_ring[i];
  893. rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
  894. rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
  895. /* The size of the buffer is 16 byte boundary. */
  896. rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
  897. /* Rx descriptor address set */
  898. if (i == 0) {
  899. sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
  900. if (sh_eth_is_gether(mdp))
  901. sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
  902. }
  903. }
  904. mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
  905. /* Mark the last entry as wrapping the ring. */
  906. rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
  907. memset(mdp->tx_ring, 0, tx_ringsize);
  908. /* build Tx ring buffer */
  909. for (i = 0; i < mdp->num_tx_ring; i++) {
  910. mdp->tx_skbuff[i] = NULL;
  911. txdesc = &mdp->tx_ring[i];
  912. txdesc->status = cpu_to_edmac(mdp, TD_TFP);
  913. txdesc->buffer_length = 0;
  914. if (i == 0) {
  915. /* Tx descriptor address set */
  916. sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
  917. if (sh_eth_is_gether(mdp))
  918. sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
  919. }
  920. }
  921. txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
  922. }
  923. /* Get skb and descriptor buffer */
  924. static int sh_eth_ring_init(struct net_device *ndev)
  925. {
  926. struct sh_eth_private *mdp = netdev_priv(ndev);
  927. int rx_ringsize, tx_ringsize, ret = 0;
  928. /*
  929. * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  930. * card needs room to do 8 byte alignment, +2 so we can reserve
  931. * the first 2 bytes, and +16 gets room for the status word from the
  932. * card.
  933. */
  934. mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
  935. (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
  936. if (mdp->cd->rpadir)
  937. mdp->rx_buf_sz += NET_IP_ALIGN;
  938. /* Allocate RX and TX skb rings */
  939. mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
  940. sizeof(*mdp->rx_skbuff), GFP_KERNEL);
  941. if (!mdp->rx_skbuff) {
  942. ret = -ENOMEM;
  943. return ret;
  944. }
  945. mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
  946. sizeof(*mdp->tx_skbuff), GFP_KERNEL);
  947. if (!mdp->tx_skbuff) {
  948. ret = -ENOMEM;
  949. goto skb_ring_free;
  950. }
  951. /* Allocate all Rx descriptors. */
  952. rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  953. mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
  954. GFP_KERNEL);
  955. if (!mdp->rx_ring) {
  956. ret = -ENOMEM;
  957. goto desc_ring_free;
  958. }
  959. mdp->dirty_rx = 0;
  960. /* Allocate all Tx descriptors. */
  961. tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  962. mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
  963. GFP_KERNEL);
  964. if (!mdp->tx_ring) {
  965. ret = -ENOMEM;
  966. goto desc_ring_free;
  967. }
  968. return ret;
  969. desc_ring_free:
  970. /* free DMA buffer */
  971. dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
  972. skb_ring_free:
  973. /* Free Rx and Tx skb ring buffer */
  974. sh_eth_ring_free(ndev);
  975. mdp->tx_ring = NULL;
  976. mdp->rx_ring = NULL;
  977. return ret;
  978. }
  979. static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
  980. {
  981. int ringsize;
  982. if (mdp->rx_ring) {
  983. ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  984. dma_free_coherent(NULL, ringsize, mdp->rx_ring,
  985. mdp->rx_desc_dma);
  986. mdp->rx_ring = NULL;
  987. }
  988. if (mdp->tx_ring) {
  989. ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  990. dma_free_coherent(NULL, ringsize, mdp->tx_ring,
  991. mdp->tx_desc_dma);
  992. mdp->tx_ring = NULL;
  993. }
  994. }
  995. static int sh_eth_dev_init(struct net_device *ndev, bool start)
  996. {
  997. int ret = 0;
  998. struct sh_eth_private *mdp = netdev_priv(ndev);
  999. u32 val;
  1000. /* Soft Reset */
  1001. ret = sh_eth_reset(ndev);
  1002. if (ret)
  1003. goto out;
  1004. /* Descriptor format */
  1005. sh_eth_ring_format(ndev);
  1006. if (mdp->cd->rpadir)
  1007. sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
  1008. /* all sh_eth int mask */
  1009. sh_eth_write(ndev, 0, EESIPR);
  1010. #if defined(__LITTLE_ENDIAN)
  1011. if (mdp->cd->hw_swap)
  1012. sh_eth_write(ndev, EDMR_EL, EDMR);
  1013. else
  1014. #endif
  1015. sh_eth_write(ndev, 0, EDMR);
  1016. /* FIFO size set */
  1017. sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
  1018. sh_eth_write(ndev, 0, TFTR);
  1019. /* Frame recv control */
  1020. sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
  1021. sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
  1022. if (mdp->cd->bculr)
  1023. sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
  1024. sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
  1025. if (!mdp->cd->no_trimd)
  1026. sh_eth_write(ndev, 0, TRIMD);
  1027. /* Recv frame limit set register */
  1028. sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  1029. RFLR);
  1030. sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
  1031. if (start)
  1032. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1033. /* PAUSE Prohibition */
  1034. val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
  1035. ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
  1036. sh_eth_write(ndev, val, ECMR);
  1037. if (mdp->cd->set_rate)
  1038. mdp->cd->set_rate(ndev);
  1039. /* E-MAC Status Register clear */
  1040. sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
  1041. /* E-MAC Interrupt Enable register */
  1042. if (start)
  1043. sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
  1044. /* Set MAC address */
  1045. update_mac_address(ndev);
  1046. /* mask reset */
  1047. if (mdp->cd->apr)
  1048. sh_eth_write(ndev, APR_AP, APR);
  1049. if (mdp->cd->mpr)
  1050. sh_eth_write(ndev, MPR_MP, MPR);
  1051. if (mdp->cd->tpauser)
  1052. sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
  1053. if (start) {
  1054. /* Setting the Rx mode will start the Rx process. */
  1055. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1056. netif_start_queue(ndev);
  1057. }
  1058. out:
  1059. return ret;
  1060. }
  1061. /* free Tx skb function */
  1062. static int sh_eth_txfree(struct net_device *ndev)
  1063. {
  1064. struct sh_eth_private *mdp = netdev_priv(ndev);
  1065. struct sh_eth_txdesc *txdesc;
  1066. int freeNum = 0;
  1067. int entry = 0;
  1068. for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
  1069. entry = mdp->dirty_tx % mdp->num_tx_ring;
  1070. txdesc = &mdp->tx_ring[entry];
  1071. if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
  1072. break;
  1073. /* Free the original skb. */
  1074. if (mdp->tx_skbuff[entry]) {
  1075. dma_unmap_single(&ndev->dev, txdesc->addr,
  1076. txdesc->buffer_length, DMA_TO_DEVICE);
  1077. dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
  1078. mdp->tx_skbuff[entry] = NULL;
  1079. freeNum++;
  1080. }
  1081. txdesc->status = cpu_to_edmac(mdp, TD_TFP);
  1082. if (entry >= mdp->num_tx_ring - 1)
  1083. txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
  1084. ndev->stats.tx_packets++;
  1085. ndev->stats.tx_bytes += txdesc->buffer_length;
  1086. }
  1087. return freeNum;
  1088. }
  1089. /* Packet receive function */
  1090. static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
  1091. {
  1092. struct sh_eth_private *mdp = netdev_priv(ndev);
  1093. struct sh_eth_rxdesc *rxdesc;
  1094. int entry = mdp->cur_rx % mdp->num_rx_ring;
  1095. int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
  1096. struct sk_buff *skb;
  1097. u16 pkt_len = 0;
  1098. u32 desc_status;
  1099. rxdesc = &mdp->rx_ring[entry];
  1100. while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
  1101. desc_status = edmac_to_cpu(mdp, rxdesc->status);
  1102. pkt_len = rxdesc->frame_length;
  1103. #if defined(CONFIG_ARCH_R8A7740)
  1104. desc_status >>= 16;
  1105. #endif
  1106. if (--boguscnt < 0)
  1107. break;
  1108. if (!(desc_status & RDFEND))
  1109. ndev->stats.rx_length_errors++;
  1110. if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
  1111. RD_RFS5 | RD_RFS6 | RD_RFS10)) {
  1112. ndev->stats.rx_errors++;
  1113. if (desc_status & RD_RFS1)
  1114. ndev->stats.rx_crc_errors++;
  1115. if (desc_status & RD_RFS2)
  1116. ndev->stats.rx_frame_errors++;
  1117. if (desc_status & RD_RFS3)
  1118. ndev->stats.rx_length_errors++;
  1119. if (desc_status & RD_RFS4)
  1120. ndev->stats.rx_length_errors++;
  1121. if (desc_status & RD_RFS6)
  1122. ndev->stats.rx_missed_errors++;
  1123. if (desc_status & RD_RFS10)
  1124. ndev->stats.rx_over_errors++;
  1125. } else {
  1126. if (!mdp->cd->hw_swap)
  1127. sh_eth_soft_swap(
  1128. phys_to_virt(ALIGN(rxdesc->addr, 4)),
  1129. pkt_len + 2);
  1130. skb = mdp->rx_skbuff[entry];
  1131. mdp->rx_skbuff[entry] = NULL;
  1132. if (mdp->cd->rpadir)
  1133. skb_reserve(skb, NET_IP_ALIGN);
  1134. skb_put(skb, pkt_len);
  1135. skb->protocol = eth_type_trans(skb, ndev);
  1136. netif_rx(skb);
  1137. ndev->stats.rx_packets++;
  1138. ndev->stats.rx_bytes += pkt_len;
  1139. }
  1140. rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
  1141. entry = (++mdp->cur_rx) % mdp->num_rx_ring;
  1142. rxdesc = &mdp->rx_ring[entry];
  1143. }
  1144. /* Refill the Rx ring buffers. */
  1145. for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
  1146. entry = mdp->dirty_rx % mdp->num_rx_ring;
  1147. rxdesc = &mdp->rx_ring[entry];
  1148. /* The size of the buffer is 16 byte boundary. */
  1149. rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
  1150. if (mdp->rx_skbuff[entry] == NULL) {
  1151. skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
  1152. mdp->rx_skbuff[entry] = skb;
  1153. if (skb == NULL)
  1154. break; /* Better luck next round. */
  1155. dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
  1156. DMA_FROM_DEVICE);
  1157. sh_eth_set_receive_align(skb);
  1158. skb_checksum_none_assert(skb);
  1159. rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
  1160. }
  1161. if (entry >= mdp->num_rx_ring - 1)
  1162. rxdesc->status |=
  1163. cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
  1164. else
  1165. rxdesc->status |=
  1166. cpu_to_edmac(mdp, RD_RACT | RD_RFP);
  1167. }
  1168. /* Restart Rx engine if stopped. */
  1169. /* If we don't need to check status, don't. -KDU */
  1170. if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
  1171. /* fix the values for the next receiving if RDE is set */
  1172. if (intr_status & EESR_RDE)
  1173. mdp->cur_rx = mdp->dirty_rx =
  1174. (sh_eth_read(ndev, RDFAR) -
  1175. sh_eth_read(ndev, RDLAR)) >> 4;
  1176. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1177. }
  1178. return 0;
  1179. }
  1180. static void sh_eth_rcv_snd_disable(struct net_device *ndev)
  1181. {
  1182. /* disable tx and rx */
  1183. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
  1184. ~(ECMR_RE | ECMR_TE), ECMR);
  1185. }
  1186. static void sh_eth_rcv_snd_enable(struct net_device *ndev)
  1187. {
  1188. /* enable tx and rx */
  1189. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
  1190. (ECMR_RE | ECMR_TE), ECMR);
  1191. }
  1192. /* error control function */
  1193. static void sh_eth_error(struct net_device *ndev, int intr_status)
  1194. {
  1195. struct sh_eth_private *mdp = netdev_priv(ndev);
  1196. u32 felic_stat;
  1197. u32 link_stat;
  1198. u32 mask;
  1199. if (intr_status & EESR_ECI) {
  1200. felic_stat = sh_eth_read(ndev, ECSR);
  1201. sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
  1202. if (felic_stat & ECSR_ICD)
  1203. ndev->stats.tx_carrier_errors++;
  1204. if (felic_stat & ECSR_LCHNG) {
  1205. /* Link Changed */
  1206. if (mdp->cd->no_psr || mdp->no_ether_link) {
  1207. goto ignore_link;
  1208. } else {
  1209. link_stat = (sh_eth_read(ndev, PSR));
  1210. if (mdp->ether_link_active_low)
  1211. link_stat = ~link_stat;
  1212. }
  1213. if (!(link_stat & PHY_ST_LINK))
  1214. sh_eth_rcv_snd_disable(ndev);
  1215. else {
  1216. /* Link Up */
  1217. sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
  1218. ~DMAC_M_ECI, EESIPR);
  1219. /*clear int */
  1220. sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
  1221. ECSR);
  1222. sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
  1223. DMAC_M_ECI, EESIPR);
  1224. /* enable tx and rx */
  1225. sh_eth_rcv_snd_enable(ndev);
  1226. }
  1227. }
  1228. }
  1229. ignore_link:
  1230. if (intr_status & EESR_TWB) {
  1231. /* Write buck end. unused write back interrupt */
  1232. if (intr_status & EESR_TABT) /* Transmit Abort int */
  1233. ndev->stats.tx_aborted_errors++;
  1234. if (netif_msg_tx_err(mdp))
  1235. dev_err(&ndev->dev, "Transmit Abort\n");
  1236. }
  1237. if (intr_status & EESR_RABT) {
  1238. /* Receive Abort int */
  1239. if (intr_status & EESR_RFRMER) {
  1240. /* Receive Frame Overflow int */
  1241. ndev->stats.rx_frame_errors++;
  1242. if (netif_msg_rx_err(mdp))
  1243. dev_err(&ndev->dev, "Receive Abort\n");
  1244. }
  1245. }
  1246. if (intr_status & EESR_TDE) {
  1247. /* Transmit Descriptor Empty int */
  1248. ndev->stats.tx_fifo_errors++;
  1249. if (netif_msg_tx_err(mdp))
  1250. dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
  1251. }
  1252. if (intr_status & EESR_TFE) {
  1253. /* FIFO under flow */
  1254. ndev->stats.tx_fifo_errors++;
  1255. if (netif_msg_tx_err(mdp))
  1256. dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
  1257. }
  1258. if (intr_status & EESR_RDE) {
  1259. /* Receive Descriptor Empty int */
  1260. ndev->stats.rx_over_errors++;
  1261. if (netif_msg_rx_err(mdp))
  1262. dev_err(&ndev->dev, "Receive Descriptor Empty\n");
  1263. }
  1264. if (intr_status & EESR_RFE) {
  1265. /* Receive FIFO Overflow int */
  1266. ndev->stats.rx_fifo_errors++;
  1267. if (netif_msg_rx_err(mdp))
  1268. dev_err(&ndev->dev, "Receive FIFO Overflow\n");
  1269. }
  1270. if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
  1271. /* Address Error */
  1272. ndev->stats.tx_fifo_errors++;
  1273. if (netif_msg_tx_err(mdp))
  1274. dev_err(&ndev->dev, "Address Error\n");
  1275. }
  1276. mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
  1277. if (mdp->cd->no_ade)
  1278. mask &= ~EESR_ADE;
  1279. if (intr_status & mask) {
  1280. /* Tx error */
  1281. u32 edtrr = sh_eth_read(ndev, EDTRR);
  1282. /* dmesg */
  1283. dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
  1284. intr_status, mdp->cur_tx);
  1285. dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
  1286. mdp->dirty_tx, (u32) ndev->state, edtrr);
  1287. /* dirty buffer free */
  1288. sh_eth_txfree(ndev);
  1289. /* SH7712 BUG */
  1290. if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
  1291. /* tx dma start */
  1292. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  1293. }
  1294. /* wakeup */
  1295. netif_wake_queue(ndev);
  1296. }
  1297. }
  1298. static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
  1299. {
  1300. struct net_device *ndev = netdev;
  1301. struct sh_eth_private *mdp = netdev_priv(ndev);
  1302. struct sh_eth_cpu_data *cd = mdp->cd;
  1303. irqreturn_t ret = IRQ_NONE;
  1304. unsigned long intr_status;
  1305. spin_lock(&mdp->lock);
  1306. /* Get interrupt status */
  1307. intr_status = sh_eth_read(ndev, EESR);
  1308. /* Mask it with the interrupt mask, forcing ECI interrupt to be always
  1309. * enabled since it's the one that comes thru regardless of the mask,
  1310. * and we need to fully handle it in sh_eth_error() in order to quench
  1311. * it as it doesn't get cleared by just writing 1 to the ECI bit...
  1312. */
  1313. intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
  1314. /* Clear interrupt */
  1315. if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
  1316. EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
  1317. cd->tx_check | cd->eesr_err_check)) {
  1318. sh_eth_write(ndev, intr_status, EESR);
  1319. ret = IRQ_HANDLED;
  1320. } else
  1321. goto other_irq;
  1322. if (intr_status & (EESR_FRC | /* Frame recv*/
  1323. EESR_RMAF | /* Multi cast address recv*/
  1324. EESR_RRF | /* Bit frame recv */
  1325. EESR_RTLF | /* Long frame recv*/
  1326. EESR_RTSF | /* short frame recv */
  1327. EESR_PRE | /* PHY-LSI recv error */
  1328. EESR_CERF)){ /* recv frame CRC error */
  1329. sh_eth_rx(ndev, intr_status);
  1330. }
  1331. /* Tx Check */
  1332. if (intr_status & cd->tx_check) {
  1333. sh_eth_txfree(ndev);
  1334. netif_wake_queue(ndev);
  1335. }
  1336. if (intr_status & cd->eesr_err_check)
  1337. sh_eth_error(ndev, intr_status);
  1338. other_irq:
  1339. spin_unlock(&mdp->lock);
  1340. return ret;
  1341. }
  1342. /* PHY state control function */
  1343. static void sh_eth_adjust_link(struct net_device *ndev)
  1344. {
  1345. struct sh_eth_private *mdp = netdev_priv(ndev);
  1346. struct phy_device *phydev = mdp->phydev;
  1347. int new_state = 0;
  1348. if (phydev->link) {
  1349. if (phydev->duplex != mdp->duplex) {
  1350. new_state = 1;
  1351. mdp->duplex = phydev->duplex;
  1352. if (mdp->cd->set_duplex)
  1353. mdp->cd->set_duplex(ndev);
  1354. }
  1355. if (phydev->speed != mdp->speed) {
  1356. new_state = 1;
  1357. mdp->speed = phydev->speed;
  1358. if (mdp->cd->set_rate)
  1359. mdp->cd->set_rate(ndev);
  1360. }
  1361. if (!mdp->link) {
  1362. sh_eth_write(ndev,
  1363. (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
  1364. new_state = 1;
  1365. mdp->link = phydev->link;
  1366. if (mdp->cd->no_psr || mdp->no_ether_link)
  1367. sh_eth_rcv_snd_enable(ndev);
  1368. }
  1369. } else if (mdp->link) {
  1370. new_state = 1;
  1371. mdp->link = 0;
  1372. mdp->speed = 0;
  1373. mdp->duplex = -1;
  1374. if (mdp->cd->no_psr || mdp->no_ether_link)
  1375. sh_eth_rcv_snd_disable(ndev);
  1376. }
  1377. if (new_state && netif_msg_link(mdp))
  1378. phy_print_status(phydev);
  1379. }
  1380. /* PHY init function */
  1381. static int sh_eth_phy_init(struct net_device *ndev)
  1382. {
  1383. struct sh_eth_private *mdp = netdev_priv(ndev);
  1384. char phy_id[MII_BUS_ID_SIZE + 3];
  1385. struct phy_device *phydev = NULL;
  1386. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  1387. mdp->mii_bus->id , mdp->phy_id);
  1388. mdp->link = 0;
  1389. mdp->speed = 0;
  1390. mdp->duplex = -1;
  1391. /* Try connect to PHY */
  1392. phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
  1393. mdp->phy_interface);
  1394. if (IS_ERR(phydev)) {
  1395. dev_err(&ndev->dev, "phy_connect failed\n");
  1396. return PTR_ERR(phydev);
  1397. }
  1398. dev_info(&ndev->dev, "attached phy %i to driver %s\n",
  1399. phydev->addr, phydev->drv->name);
  1400. mdp->phydev = phydev;
  1401. return 0;
  1402. }
  1403. /* PHY control start function */
  1404. static int sh_eth_phy_start(struct net_device *ndev)
  1405. {
  1406. struct sh_eth_private *mdp = netdev_priv(ndev);
  1407. int ret;
  1408. ret = sh_eth_phy_init(ndev);
  1409. if (ret)
  1410. return ret;
  1411. /* reset phy - this also wakes it from PDOWN */
  1412. phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
  1413. phy_start(mdp->phydev);
  1414. return 0;
  1415. }
  1416. static int sh_eth_get_settings(struct net_device *ndev,
  1417. struct ethtool_cmd *ecmd)
  1418. {
  1419. struct sh_eth_private *mdp = netdev_priv(ndev);
  1420. unsigned long flags;
  1421. int ret;
  1422. spin_lock_irqsave(&mdp->lock, flags);
  1423. ret = phy_ethtool_gset(mdp->phydev, ecmd);
  1424. spin_unlock_irqrestore(&mdp->lock, flags);
  1425. return ret;
  1426. }
  1427. static int sh_eth_set_settings(struct net_device *ndev,
  1428. struct ethtool_cmd *ecmd)
  1429. {
  1430. struct sh_eth_private *mdp = netdev_priv(ndev);
  1431. unsigned long flags;
  1432. int ret;
  1433. spin_lock_irqsave(&mdp->lock, flags);
  1434. /* disable tx and rx */
  1435. sh_eth_rcv_snd_disable(ndev);
  1436. ret = phy_ethtool_sset(mdp->phydev, ecmd);
  1437. if (ret)
  1438. goto error_exit;
  1439. if (ecmd->duplex == DUPLEX_FULL)
  1440. mdp->duplex = 1;
  1441. else
  1442. mdp->duplex = 0;
  1443. if (mdp->cd->set_duplex)
  1444. mdp->cd->set_duplex(ndev);
  1445. error_exit:
  1446. mdelay(1);
  1447. /* enable tx and rx */
  1448. sh_eth_rcv_snd_enable(ndev);
  1449. spin_unlock_irqrestore(&mdp->lock, flags);
  1450. return ret;
  1451. }
  1452. static int sh_eth_nway_reset(struct net_device *ndev)
  1453. {
  1454. struct sh_eth_private *mdp = netdev_priv(ndev);
  1455. unsigned long flags;
  1456. int ret;
  1457. spin_lock_irqsave(&mdp->lock, flags);
  1458. ret = phy_start_aneg(mdp->phydev);
  1459. spin_unlock_irqrestore(&mdp->lock, flags);
  1460. return ret;
  1461. }
  1462. static u32 sh_eth_get_msglevel(struct net_device *ndev)
  1463. {
  1464. struct sh_eth_private *mdp = netdev_priv(ndev);
  1465. return mdp->msg_enable;
  1466. }
  1467. static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
  1468. {
  1469. struct sh_eth_private *mdp = netdev_priv(ndev);
  1470. mdp->msg_enable = value;
  1471. }
  1472. static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
  1473. "rx_current", "tx_current",
  1474. "rx_dirty", "tx_dirty",
  1475. };
  1476. #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
  1477. static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
  1478. {
  1479. switch (sset) {
  1480. case ETH_SS_STATS:
  1481. return SH_ETH_STATS_LEN;
  1482. default:
  1483. return -EOPNOTSUPP;
  1484. }
  1485. }
  1486. static void sh_eth_get_ethtool_stats(struct net_device *ndev,
  1487. struct ethtool_stats *stats, u64 *data)
  1488. {
  1489. struct sh_eth_private *mdp = netdev_priv(ndev);
  1490. int i = 0;
  1491. /* device-specific stats */
  1492. data[i++] = mdp->cur_rx;
  1493. data[i++] = mdp->cur_tx;
  1494. data[i++] = mdp->dirty_rx;
  1495. data[i++] = mdp->dirty_tx;
  1496. }
  1497. static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1498. {
  1499. switch (stringset) {
  1500. case ETH_SS_STATS:
  1501. memcpy(data, *sh_eth_gstrings_stats,
  1502. sizeof(sh_eth_gstrings_stats));
  1503. break;
  1504. }
  1505. }
  1506. static void sh_eth_get_ringparam(struct net_device *ndev,
  1507. struct ethtool_ringparam *ring)
  1508. {
  1509. struct sh_eth_private *mdp = netdev_priv(ndev);
  1510. ring->rx_max_pending = RX_RING_MAX;
  1511. ring->tx_max_pending = TX_RING_MAX;
  1512. ring->rx_pending = mdp->num_rx_ring;
  1513. ring->tx_pending = mdp->num_tx_ring;
  1514. }
  1515. static int sh_eth_set_ringparam(struct net_device *ndev,
  1516. struct ethtool_ringparam *ring)
  1517. {
  1518. struct sh_eth_private *mdp = netdev_priv(ndev);
  1519. int ret;
  1520. if (ring->tx_pending > TX_RING_MAX ||
  1521. ring->rx_pending > RX_RING_MAX ||
  1522. ring->tx_pending < TX_RING_MIN ||
  1523. ring->rx_pending < RX_RING_MIN)
  1524. return -EINVAL;
  1525. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  1526. return -EINVAL;
  1527. if (netif_running(ndev)) {
  1528. netif_tx_disable(ndev);
  1529. /* Disable interrupts by clearing the interrupt mask. */
  1530. sh_eth_write(ndev, 0x0000, EESIPR);
  1531. /* Stop the chip's Tx and Rx processes. */
  1532. sh_eth_write(ndev, 0, EDTRR);
  1533. sh_eth_write(ndev, 0, EDRRR);
  1534. synchronize_irq(ndev->irq);
  1535. }
  1536. /* Free all the skbuffs in the Rx queue. */
  1537. sh_eth_ring_free(ndev);
  1538. /* Free DMA buffer */
  1539. sh_eth_free_dma_buffer(mdp);
  1540. /* Set new parameters */
  1541. mdp->num_rx_ring = ring->rx_pending;
  1542. mdp->num_tx_ring = ring->tx_pending;
  1543. ret = sh_eth_ring_init(ndev);
  1544. if (ret < 0) {
  1545. dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
  1546. return ret;
  1547. }
  1548. ret = sh_eth_dev_init(ndev, false);
  1549. if (ret < 0) {
  1550. dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
  1551. return ret;
  1552. }
  1553. if (netif_running(ndev)) {
  1554. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1555. /* Setting the Rx mode will start the Rx process. */
  1556. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1557. netif_wake_queue(ndev);
  1558. }
  1559. return 0;
  1560. }
  1561. static const struct ethtool_ops sh_eth_ethtool_ops = {
  1562. .get_settings = sh_eth_get_settings,
  1563. .set_settings = sh_eth_set_settings,
  1564. .nway_reset = sh_eth_nway_reset,
  1565. .get_msglevel = sh_eth_get_msglevel,
  1566. .set_msglevel = sh_eth_set_msglevel,
  1567. .get_link = ethtool_op_get_link,
  1568. .get_strings = sh_eth_get_strings,
  1569. .get_ethtool_stats = sh_eth_get_ethtool_stats,
  1570. .get_sset_count = sh_eth_get_sset_count,
  1571. .get_ringparam = sh_eth_get_ringparam,
  1572. .set_ringparam = sh_eth_set_ringparam,
  1573. };
  1574. /* network device open function */
  1575. static int sh_eth_open(struct net_device *ndev)
  1576. {
  1577. int ret = 0;
  1578. struct sh_eth_private *mdp = netdev_priv(ndev);
  1579. pm_runtime_get_sync(&mdp->pdev->dev);
  1580. ret = request_irq(ndev->irq, sh_eth_interrupt,
  1581. mdp->cd->irq_flags, ndev->name, ndev);
  1582. if (ret) {
  1583. dev_err(&ndev->dev, "Can not assign IRQ number\n");
  1584. return ret;
  1585. }
  1586. /* Descriptor set */
  1587. ret = sh_eth_ring_init(ndev);
  1588. if (ret)
  1589. goto out_free_irq;
  1590. /* device init */
  1591. ret = sh_eth_dev_init(ndev, true);
  1592. if (ret)
  1593. goto out_free_irq;
  1594. /* PHY control start*/
  1595. ret = sh_eth_phy_start(ndev);
  1596. if (ret)
  1597. goto out_free_irq;
  1598. return ret;
  1599. out_free_irq:
  1600. free_irq(ndev->irq, ndev);
  1601. pm_runtime_put_sync(&mdp->pdev->dev);
  1602. return ret;
  1603. }
  1604. /* Timeout function */
  1605. static void sh_eth_tx_timeout(struct net_device *ndev)
  1606. {
  1607. struct sh_eth_private *mdp = netdev_priv(ndev);
  1608. struct sh_eth_rxdesc *rxdesc;
  1609. int i;
  1610. netif_stop_queue(ndev);
  1611. if (netif_msg_timer(mdp))
  1612. dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
  1613. " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
  1614. /* tx_errors count up */
  1615. ndev->stats.tx_errors++;
  1616. /* Free all the skbuffs in the Rx queue. */
  1617. for (i = 0; i < mdp->num_rx_ring; i++) {
  1618. rxdesc = &mdp->rx_ring[i];
  1619. rxdesc->status = 0;
  1620. rxdesc->addr = 0xBADF00D0;
  1621. if (mdp->rx_skbuff[i])
  1622. dev_kfree_skb(mdp->rx_skbuff[i]);
  1623. mdp->rx_skbuff[i] = NULL;
  1624. }
  1625. for (i = 0; i < mdp->num_tx_ring; i++) {
  1626. if (mdp->tx_skbuff[i])
  1627. dev_kfree_skb(mdp->tx_skbuff[i]);
  1628. mdp->tx_skbuff[i] = NULL;
  1629. }
  1630. /* device init */
  1631. sh_eth_dev_init(ndev, true);
  1632. }
  1633. /* Packet transmit function */
  1634. static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1635. {
  1636. struct sh_eth_private *mdp = netdev_priv(ndev);
  1637. struct sh_eth_txdesc *txdesc;
  1638. u32 entry;
  1639. unsigned long flags;
  1640. spin_lock_irqsave(&mdp->lock, flags);
  1641. if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
  1642. if (!sh_eth_txfree(ndev)) {
  1643. if (netif_msg_tx_queued(mdp))
  1644. dev_warn(&ndev->dev, "TxFD exhausted.\n");
  1645. netif_stop_queue(ndev);
  1646. spin_unlock_irqrestore(&mdp->lock, flags);
  1647. return NETDEV_TX_BUSY;
  1648. }
  1649. }
  1650. spin_unlock_irqrestore(&mdp->lock, flags);
  1651. entry = mdp->cur_tx % mdp->num_tx_ring;
  1652. mdp->tx_skbuff[entry] = skb;
  1653. txdesc = &mdp->tx_ring[entry];
  1654. /* soft swap. */
  1655. if (!mdp->cd->hw_swap)
  1656. sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
  1657. skb->len + 2);
  1658. txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
  1659. DMA_TO_DEVICE);
  1660. if (skb->len < ETHERSMALL)
  1661. txdesc->buffer_length = ETHERSMALL;
  1662. else
  1663. txdesc->buffer_length = skb->len;
  1664. if (entry >= mdp->num_tx_ring - 1)
  1665. txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
  1666. else
  1667. txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
  1668. mdp->cur_tx++;
  1669. if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
  1670. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  1671. return NETDEV_TX_OK;
  1672. }
  1673. /* device close function */
  1674. static int sh_eth_close(struct net_device *ndev)
  1675. {
  1676. struct sh_eth_private *mdp = netdev_priv(ndev);
  1677. netif_stop_queue(ndev);
  1678. /* Disable interrupts by clearing the interrupt mask. */
  1679. sh_eth_write(ndev, 0x0000, EESIPR);
  1680. /* Stop the chip's Tx and Rx processes. */
  1681. sh_eth_write(ndev, 0, EDTRR);
  1682. sh_eth_write(ndev, 0, EDRRR);
  1683. /* PHY Disconnect */
  1684. if (mdp->phydev) {
  1685. phy_stop(mdp->phydev);
  1686. phy_disconnect(mdp->phydev);
  1687. }
  1688. free_irq(ndev->irq, ndev);
  1689. /* Free all the skbuffs in the Rx queue. */
  1690. sh_eth_ring_free(ndev);
  1691. /* free DMA buffer */
  1692. sh_eth_free_dma_buffer(mdp);
  1693. pm_runtime_put_sync(&mdp->pdev->dev);
  1694. return 0;
  1695. }
  1696. static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
  1697. {
  1698. struct sh_eth_private *mdp = netdev_priv(ndev);
  1699. pm_runtime_get_sync(&mdp->pdev->dev);
  1700. ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
  1701. sh_eth_write(ndev, 0, TROCR); /* (write clear) */
  1702. ndev->stats.collisions += sh_eth_read(ndev, CDCR);
  1703. sh_eth_write(ndev, 0, CDCR); /* (write clear) */
  1704. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
  1705. sh_eth_write(ndev, 0, LCCR); /* (write clear) */
  1706. if (sh_eth_is_gether(mdp)) {
  1707. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
  1708. sh_eth_write(ndev, 0, CERCR); /* (write clear) */
  1709. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
  1710. sh_eth_write(ndev, 0, CEECR); /* (write clear) */
  1711. } else {
  1712. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
  1713. sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
  1714. }
  1715. pm_runtime_put_sync(&mdp->pdev->dev);
  1716. return &ndev->stats;
  1717. }
  1718. /* ioctl to device function */
  1719. static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
  1720. int cmd)
  1721. {
  1722. struct sh_eth_private *mdp = netdev_priv(ndev);
  1723. struct phy_device *phydev = mdp->phydev;
  1724. if (!netif_running(ndev))
  1725. return -EINVAL;
  1726. if (!phydev)
  1727. return -ENODEV;
  1728. return phy_mii_ioctl(phydev, rq, cmd);
  1729. }
  1730. /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
  1731. static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
  1732. int entry)
  1733. {
  1734. return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
  1735. }
  1736. static u32 sh_eth_tsu_get_post_mask(int entry)
  1737. {
  1738. return 0x0f << (28 - ((entry % 8) * 4));
  1739. }
  1740. static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
  1741. {
  1742. return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
  1743. }
  1744. static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
  1745. int entry)
  1746. {
  1747. struct sh_eth_private *mdp = netdev_priv(ndev);
  1748. u32 tmp;
  1749. void *reg_offset;
  1750. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  1751. tmp = ioread32(reg_offset);
  1752. iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
  1753. }
  1754. static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
  1755. int entry)
  1756. {
  1757. struct sh_eth_private *mdp = netdev_priv(ndev);
  1758. u32 post_mask, ref_mask, tmp;
  1759. void *reg_offset;
  1760. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  1761. post_mask = sh_eth_tsu_get_post_mask(entry);
  1762. ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
  1763. tmp = ioread32(reg_offset);
  1764. iowrite32(tmp & ~post_mask, reg_offset);
  1765. /* If other port enables, the function returns "true" */
  1766. return tmp & ref_mask;
  1767. }
  1768. static int sh_eth_tsu_busy(struct net_device *ndev)
  1769. {
  1770. int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
  1771. struct sh_eth_private *mdp = netdev_priv(ndev);
  1772. while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
  1773. udelay(10);
  1774. timeout--;
  1775. if (timeout <= 0) {
  1776. dev_err(&ndev->dev, "%s: timeout\n", __func__);
  1777. return -ETIMEDOUT;
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
  1783. const u8 *addr)
  1784. {
  1785. u32 val;
  1786. val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
  1787. iowrite32(val, reg);
  1788. if (sh_eth_tsu_busy(ndev) < 0)
  1789. return -EBUSY;
  1790. val = addr[4] << 8 | addr[5];
  1791. iowrite32(val, reg + 4);
  1792. if (sh_eth_tsu_busy(ndev) < 0)
  1793. return -EBUSY;
  1794. return 0;
  1795. }
  1796. static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
  1797. {
  1798. u32 val;
  1799. val = ioread32(reg);
  1800. addr[0] = (val >> 24) & 0xff;
  1801. addr[1] = (val >> 16) & 0xff;
  1802. addr[2] = (val >> 8) & 0xff;
  1803. addr[3] = val & 0xff;
  1804. val = ioread32(reg + 4);
  1805. addr[4] = (val >> 8) & 0xff;
  1806. addr[5] = val & 0xff;
  1807. }
  1808. static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
  1809. {
  1810. struct sh_eth_private *mdp = netdev_priv(ndev);
  1811. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1812. int i;
  1813. u8 c_addr[ETH_ALEN];
  1814. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  1815. sh_eth_tsu_read_entry(reg_offset, c_addr);
  1816. if (memcmp(addr, c_addr, ETH_ALEN) == 0)
  1817. return i;
  1818. }
  1819. return -ENOENT;
  1820. }
  1821. static int sh_eth_tsu_find_empty(struct net_device *ndev)
  1822. {
  1823. u8 blank[ETH_ALEN];
  1824. int entry;
  1825. memset(blank, 0, sizeof(blank));
  1826. entry = sh_eth_tsu_find_entry(ndev, blank);
  1827. return (entry < 0) ? -ENOMEM : entry;
  1828. }
  1829. static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
  1830. int entry)
  1831. {
  1832. struct sh_eth_private *mdp = netdev_priv(ndev);
  1833. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1834. int ret;
  1835. u8 blank[ETH_ALEN];
  1836. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
  1837. ~(1 << (31 - entry)), TSU_TEN);
  1838. memset(blank, 0, sizeof(blank));
  1839. ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
  1840. if (ret < 0)
  1841. return ret;
  1842. return 0;
  1843. }
  1844. static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
  1845. {
  1846. struct sh_eth_private *mdp = netdev_priv(ndev);
  1847. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1848. int i, ret;
  1849. if (!mdp->cd->tsu)
  1850. return 0;
  1851. i = sh_eth_tsu_find_entry(ndev, addr);
  1852. if (i < 0) {
  1853. /* No entry found, create one */
  1854. i = sh_eth_tsu_find_empty(ndev);
  1855. if (i < 0)
  1856. return -ENOMEM;
  1857. ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
  1858. if (ret < 0)
  1859. return ret;
  1860. /* Enable the entry */
  1861. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
  1862. (1 << (31 - i)), TSU_TEN);
  1863. }
  1864. /* Entry found or created, enable POST */
  1865. sh_eth_tsu_enable_cam_entry_post(ndev, i);
  1866. return 0;
  1867. }
  1868. static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
  1869. {
  1870. struct sh_eth_private *mdp = netdev_priv(ndev);
  1871. int i, ret;
  1872. if (!mdp->cd->tsu)
  1873. return 0;
  1874. i = sh_eth_tsu_find_entry(ndev, addr);
  1875. if (i) {
  1876. /* Entry found */
  1877. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  1878. goto done;
  1879. /* Disable the entry if both ports was disabled */
  1880. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  1881. if (ret < 0)
  1882. return ret;
  1883. }
  1884. done:
  1885. return 0;
  1886. }
  1887. static int sh_eth_tsu_purge_all(struct net_device *ndev)
  1888. {
  1889. struct sh_eth_private *mdp = netdev_priv(ndev);
  1890. int i, ret;
  1891. if (unlikely(!mdp->cd->tsu))
  1892. return 0;
  1893. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
  1894. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  1895. continue;
  1896. /* Disable the entry if both ports was disabled */
  1897. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  1898. if (ret < 0)
  1899. return ret;
  1900. }
  1901. return 0;
  1902. }
  1903. static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
  1904. {
  1905. struct sh_eth_private *mdp = netdev_priv(ndev);
  1906. u8 addr[ETH_ALEN];
  1907. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1908. int i;
  1909. if (unlikely(!mdp->cd->tsu))
  1910. return;
  1911. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  1912. sh_eth_tsu_read_entry(reg_offset, addr);
  1913. if (is_multicast_ether_addr(addr))
  1914. sh_eth_tsu_del_entry(ndev, addr);
  1915. }
  1916. }
  1917. /* Multicast reception directions set */
  1918. static void sh_eth_set_multicast_list(struct net_device *ndev)
  1919. {
  1920. struct sh_eth_private *mdp = netdev_priv(ndev);
  1921. u32 ecmr_bits;
  1922. int mcast_all = 0;
  1923. unsigned long flags;
  1924. spin_lock_irqsave(&mdp->lock, flags);
  1925. /*
  1926. * Initial condition is MCT = 1, PRM = 0.
  1927. * Depending on ndev->flags, set PRM or clear MCT
  1928. */
  1929. ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
  1930. if (!(ndev->flags & IFF_MULTICAST)) {
  1931. sh_eth_tsu_purge_mcast(ndev);
  1932. mcast_all = 1;
  1933. }
  1934. if (ndev->flags & IFF_ALLMULTI) {
  1935. sh_eth_tsu_purge_mcast(ndev);
  1936. ecmr_bits &= ~ECMR_MCT;
  1937. mcast_all = 1;
  1938. }
  1939. if (ndev->flags & IFF_PROMISC) {
  1940. sh_eth_tsu_purge_all(ndev);
  1941. ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
  1942. } else if (mdp->cd->tsu) {
  1943. struct netdev_hw_addr *ha;
  1944. netdev_for_each_mc_addr(ha, ndev) {
  1945. if (mcast_all && is_multicast_ether_addr(ha->addr))
  1946. continue;
  1947. if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
  1948. if (!mcast_all) {
  1949. sh_eth_tsu_purge_mcast(ndev);
  1950. ecmr_bits &= ~ECMR_MCT;
  1951. mcast_all = 1;
  1952. }
  1953. }
  1954. }
  1955. } else {
  1956. /* Normal, unicast/broadcast-only mode. */
  1957. ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
  1958. }
  1959. /* update the ethernet mode */
  1960. sh_eth_write(ndev, ecmr_bits, ECMR);
  1961. spin_unlock_irqrestore(&mdp->lock, flags);
  1962. }
  1963. static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
  1964. {
  1965. if (!mdp->port)
  1966. return TSU_VTAG0;
  1967. else
  1968. return TSU_VTAG1;
  1969. }
  1970. static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
  1971. __be16 proto, u16 vid)
  1972. {
  1973. struct sh_eth_private *mdp = netdev_priv(ndev);
  1974. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  1975. if (unlikely(!mdp->cd->tsu))
  1976. return -EPERM;
  1977. /* No filtering if vid = 0 */
  1978. if (!vid)
  1979. return 0;
  1980. mdp->vlan_num_ids++;
  1981. /*
  1982. * The controller has one VLAN tag HW filter. So, if the filter is
  1983. * already enabled, the driver disables it and the filte
  1984. */
  1985. if (mdp->vlan_num_ids > 1) {
  1986. /* disable VLAN filter */
  1987. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  1988. return 0;
  1989. }
  1990. sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
  1991. vtag_reg_index);
  1992. return 0;
  1993. }
  1994. static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
  1995. __be16 proto, u16 vid)
  1996. {
  1997. struct sh_eth_private *mdp = netdev_priv(ndev);
  1998. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  1999. if (unlikely(!mdp->cd->tsu))
  2000. return -EPERM;
  2001. /* No filtering if vid = 0 */
  2002. if (!vid)
  2003. return 0;
  2004. mdp->vlan_num_ids--;
  2005. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2006. return 0;
  2007. }
  2008. /* SuperH's TSU register init function */
  2009. static void sh_eth_tsu_init(struct sh_eth_private *mdp)
  2010. {
  2011. sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
  2012. sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
  2013. sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
  2014. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
  2015. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
  2016. sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
  2017. sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
  2018. sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
  2019. sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
  2020. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
  2021. if (sh_eth_is_gether(mdp)) {
  2022. sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
  2023. sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
  2024. } else {
  2025. sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
  2026. sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
  2027. }
  2028. sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
  2029. sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
  2030. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2031. sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
  2032. sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
  2033. sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
  2034. sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
  2035. }
  2036. /* MDIO bus release function */
  2037. static int sh_mdio_release(struct net_device *ndev)
  2038. {
  2039. struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
  2040. /* unregister mdio bus */
  2041. mdiobus_unregister(bus);
  2042. /* remove mdio bus info from net_device */
  2043. dev_set_drvdata(&ndev->dev, NULL);
  2044. /* free bitbang info */
  2045. free_mdio_bitbang(bus);
  2046. return 0;
  2047. }
  2048. /* MDIO bus init function */
  2049. static int sh_mdio_init(struct net_device *ndev, int id,
  2050. struct sh_eth_plat_data *pd)
  2051. {
  2052. int ret, i;
  2053. struct bb_info *bitbang;
  2054. struct sh_eth_private *mdp = netdev_priv(ndev);
  2055. /* create bit control struct for PHY */
  2056. bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
  2057. GFP_KERNEL);
  2058. if (!bitbang) {
  2059. ret = -ENOMEM;
  2060. goto out;
  2061. }
  2062. /* bitbang init */
  2063. bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
  2064. bitbang->set_gate = pd->set_mdio_gate;
  2065. bitbang->mdi_msk = PIR_MDI;
  2066. bitbang->mdo_msk = PIR_MDO;
  2067. bitbang->mmd_msk = PIR_MMD;
  2068. bitbang->mdc_msk = PIR_MDC;
  2069. bitbang->ctrl.ops = &bb_ops;
  2070. /* MII controller setting */
  2071. mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  2072. if (!mdp->mii_bus) {
  2073. ret = -ENOMEM;
  2074. goto out;
  2075. }
  2076. /* Hook up MII support for ethtool */
  2077. mdp->mii_bus->name = "sh_mii";
  2078. mdp->mii_bus->parent = &ndev->dev;
  2079. snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  2080. mdp->pdev->name, id);
  2081. /* PHY IRQ */
  2082. mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
  2083. sizeof(int) * PHY_MAX_ADDR,
  2084. GFP_KERNEL);
  2085. if (!mdp->mii_bus->irq) {
  2086. ret = -ENOMEM;
  2087. goto out_free_bus;
  2088. }
  2089. for (i = 0; i < PHY_MAX_ADDR; i++)
  2090. mdp->mii_bus->irq[i] = PHY_POLL;
  2091. /* register mdio bus */
  2092. ret = mdiobus_register(mdp->mii_bus);
  2093. if (ret)
  2094. goto out_free_bus;
  2095. dev_set_drvdata(&ndev->dev, mdp->mii_bus);
  2096. return 0;
  2097. out_free_bus:
  2098. free_mdio_bitbang(mdp->mii_bus);
  2099. out:
  2100. return ret;
  2101. }
  2102. static const u16 *sh_eth_get_register_offset(int register_type)
  2103. {
  2104. const u16 *reg_offset = NULL;
  2105. switch (register_type) {
  2106. case SH_ETH_REG_GIGABIT:
  2107. reg_offset = sh_eth_offset_gigabit;
  2108. break;
  2109. case SH_ETH_REG_FAST_RCAR:
  2110. reg_offset = sh_eth_offset_fast_rcar;
  2111. break;
  2112. case SH_ETH_REG_FAST_SH4:
  2113. reg_offset = sh_eth_offset_fast_sh4;
  2114. break;
  2115. case SH_ETH_REG_FAST_SH3_SH2:
  2116. reg_offset = sh_eth_offset_fast_sh3_sh2;
  2117. break;
  2118. default:
  2119. pr_err("Unknown register type (%d)\n", register_type);
  2120. break;
  2121. }
  2122. return reg_offset;
  2123. }
  2124. static struct net_device_ops sh_eth_netdev_ops = {
  2125. .ndo_open = sh_eth_open,
  2126. .ndo_stop = sh_eth_close,
  2127. .ndo_start_xmit = sh_eth_start_xmit,
  2128. .ndo_get_stats = sh_eth_get_stats,
  2129. .ndo_tx_timeout = sh_eth_tx_timeout,
  2130. .ndo_do_ioctl = sh_eth_do_ioctl,
  2131. .ndo_validate_addr = eth_validate_addr,
  2132. .ndo_set_mac_address = eth_mac_addr,
  2133. .ndo_change_mtu = eth_change_mtu,
  2134. };
  2135. static int sh_eth_drv_probe(struct platform_device *pdev)
  2136. {
  2137. int ret, devno = 0;
  2138. struct resource *res;
  2139. struct net_device *ndev = NULL;
  2140. struct sh_eth_private *mdp = NULL;
  2141. struct sh_eth_plat_data *pd = pdev->dev.platform_data;
  2142. const struct platform_device_id *id = platform_get_device_id(pdev);
  2143. /* get base addr */
  2144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2145. if (unlikely(res == NULL)) {
  2146. dev_err(&pdev->dev, "invalid resource\n");
  2147. ret = -EINVAL;
  2148. goto out;
  2149. }
  2150. ndev = alloc_etherdev(sizeof(struct sh_eth_private));
  2151. if (!ndev) {
  2152. ret = -ENOMEM;
  2153. goto out;
  2154. }
  2155. /* The sh Ether-specific entries in the device structure. */
  2156. ndev->base_addr = res->start;
  2157. devno = pdev->id;
  2158. if (devno < 0)
  2159. devno = 0;
  2160. ndev->dma = -1;
  2161. ret = platform_get_irq(pdev, 0);
  2162. if (ret < 0) {
  2163. ret = -ENODEV;
  2164. goto out_release;
  2165. }
  2166. ndev->irq = ret;
  2167. SET_NETDEV_DEV(ndev, &pdev->dev);
  2168. /* Fill in the fields of the device structure with ethernet values. */
  2169. ether_setup(ndev);
  2170. mdp = netdev_priv(ndev);
  2171. mdp->num_tx_ring = TX_RING_SIZE;
  2172. mdp->num_rx_ring = RX_RING_SIZE;
  2173. mdp->addr = devm_ioremap_resource(&pdev->dev, res);
  2174. if (IS_ERR(mdp->addr)) {
  2175. ret = PTR_ERR(mdp->addr);
  2176. goto out_release;
  2177. }
  2178. spin_lock_init(&mdp->lock);
  2179. mdp->pdev = pdev;
  2180. pm_runtime_enable(&pdev->dev);
  2181. pm_runtime_resume(&pdev->dev);
  2182. /* get PHY ID */
  2183. mdp->phy_id = pd->phy;
  2184. mdp->phy_interface = pd->phy_interface;
  2185. /* EDMAC endian */
  2186. mdp->edmac_endian = pd->edmac_endian;
  2187. mdp->no_ether_link = pd->no_ether_link;
  2188. mdp->ether_link_active_low = pd->ether_link_active_low;
  2189. mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
  2190. /* set cpu data */
  2191. #if defined(SH_ETH_HAS_BOTH_MODULES)
  2192. mdp->cd = sh_eth_get_cpu_data(mdp);
  2193. #else
  2194. mdp->cd = &sh_eth_my_cpu_data;
  2195. #endif
  2196. if (id->driver_data)
  2197. mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
  2198. sh_eth_set_default_cpu_data(mdp->cd);
  2199. /* set function */
  2200. if (mdp->cd->tsu) {
  2201. sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
  2202. sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
  2203. sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
  2204. sh_eth_vlan_rx_kill_vid;
  2205. }
  2206. ndev->netdev_ops = &sh_eth_netdev_ops;
  2207. SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
  2208. ndev->watchdog_timeo = TX_TIMEOUT;
  2209. /* debug message level */
  2210. mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
  2211. /* read and set MAC address */
  2212. read_mac_address(ndev, pd->mac_addr);
  2213. if (!is_valid_ether_addr(ndev->dev_addr)) {
  2214. dev_warn(&pdev->dev,
  2215. "no valid MAC address supplied, using a random one.\n");
  2216. eth_hw_addr_random(ndev);
  2217. }
  2218. /* ioremap the TSU registers */
  2219. if (mdp->cd->tsu) {
  2220. struct resource *rtsu;
  2221. rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2222. mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
  2223. if (IS_ERR(mdp->tsu_addr)) {
  2224. ret = PTR_ERR(mdp->tsu_addr);
  2225. goto out_release;
  2226. }
  2227. mdp->port = devno % 2;
  2228. ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
  2229. }
  2230. /* initialize first or needed device */
  2231. if (!devno || pd->needs_init) {
  2232. if (mdp->cd->chip_reset)
  2233. mdp->cd->chip_reset(ndev);
  2234. if (mdp->cd->tsu) {
  2235. /* TSU init (Init only)*/
  2236. sh_eth_tsu_init(mdp);
  2237. }
  2238. }
  2239. /* network device register */
  2240. ret = register_netdev(ndev);
  2241. if (ret)
  2242. goto out_release;
  2243. /* mdio bus init */
  2244. ret = sh_mdio_init(ndev, pdev->id, pd);
  2245. if (ret)
  2246. goto out_unregister;
  2247. /* print device information */
  2248. pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
  2249. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  2250. platform_set_drvdata(pdev, ndev);
  2251. return ret;
  2252. out_unregister:
  2253. unregister_netdev(ndev);
  2254. out_release:
  2255. /* net_dev free */
  2256. if (ndev)
  2257. free_netdev(ndev);
  2258. out:
  2259. return ret;
  2260. }
  2261. static int sh_eth_drv_remove(struct platform_device *pdev)
  2262. {
  2263. struct net_device *ndev = platform_get_drvdata(pdev);
  2264. sh_mdio_release(ndev);
  2265. unregister_netdev(ndev);
  2266. pm_runtime_disable(&pdev->dev);
  2267. free_netdev(ndev);
  2268. return 0;
  2269. }
  2270. #ifdef CONFIG_PM
  2271. static int sh_eth_runtime_nop(struct device *dev)
  2272. {
  2273. /*
  2274. * Runtime PM callback shared between ->runtime_suspend()
  2275. * and ->runtime_resume(). Simply returns success.
  2276. *
  2277. * This driver re-initializes all registers after
  2278. * pm_runtime_get_sync() anyway so there is no need
  2279. * to save and restore registers here.
  2280. */
  2281. return 0;
  2282. }
  2283. static const struct dev_pm_ops sh_eth_dev_pm_ops = {
  2284. .runtime_suspend = sh_eth_runtime_nop,
  2285. .runtime_resume = sh_eth_runtime_nop,
  2286. };
  2287. #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
  2288. #else
  2289. #define SH_ETH_PM_OPS NULL
  2290. #endif
  2291. static struct platform_device_id sh_eth_id_table[] = {
  2292. { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
  2293. { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
  2294. { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
  2295. { CARDNAME },
  2296. { }
  2297. };
  2298. MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
  2299. static struct platform_driver sh_eth_driver = {
  2300. .probe = sh_eth_drv_probe,
  2301. .remove = sh_eth_drv_remove,
  2302. .id_table = sh_eth_id_table,
  2303. .driver = {
  2304. .name = CARDNAME,
  2305. .pm = SH_ETH_PM_OPS,
  2306. },
  2307. };
  2308. module_platform_driver(sh_eth_driver);
  2309. MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
  2310. MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
  2311. MODULE_LICENSE("GPL v2");