sh_eth.c 64 KB

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