sh_eth.c 64 KB

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