sh_eth.c 71 KB

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