sh_eth.c 66 KB

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