0111-NET-MIPS-add-ralink-SoC-ethernet-driver.patch 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772
  1. From ad11aedcc16574c0b3d3f5e40c67227d1846b94e Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Mon, 22 Apr 2013 23:20:03 +0200
  4. Subject: [PATCH 16/33] NET: MIPS: add ralink SoC ethernet driver
  5. Add support for Ralink FE and ESW.
  6. Signed-off-by: John Crispin <[email protected]>
  7. ---
  8. .../include/asm/mach-ralink/rt305x_esw_platform.h | 27 +
  9. arch/mips/ralink/rt305x.c | 1 +
  10. drivers/net/ethernet/Kconfig | 1 +
  11. drivers/net/ethernet/Makefile | 1 +
  12. drivers/net/ethernet/ralink/Kconfig | 31 +
  13. drivers/net/ethernet/ralink/Makefile | 18 +
  14. drivers/net/ethernet/ralink/esw_rt3052.c | 1463 ++++++++++++++++++++
  15. drivers/net/ethernet/ralink/esw_rt3052.h | 32 +
  16. drivers/net/ethernet/ralink/gsw_mt7620a.c | 1027 ++++++++++++++
  17. drivers/net/ethernet/ralink/gsw_mt7620a.h | 29 +
  18. drivers/net/ethernet/ralink/mdio.c | 245 ++++
  19. drivers/net/ethernet/ralink/mdio.h | 29 +
  20. drivers/net/ethernet/ralink/mdio_rt2880.c | 232 ++++
  21. drivers/net/ethernet/ralink/mdio_rt2880.h | 26 +
  22. drivers/net/ethernet/ralink/ralink_soc_eth.c | 735 ++++++++++
  23. drivers/net/ethernet/ralink/ralink_soc_eth.h | 374 +++++
  24. drivers/net/ethernet/ralink/soc_mt7620.c | 111 ++
  25. drivers/net/ethernet/ralink/soc_rt2880.c | 51 +
  26. drivers/net/ethernet/ralink/soc_rt305x.c | 113 ++
  27. drivers/net/ethernet/ralink/soc_rt3883.c | 60 +
  28. 20 files changed, 4606 insertions(+)
  29. create mode 100644 arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
  30. create mode 100644 drivers/net/ethernet/ralink/Kconfig
  31. create mode 100644 drivers/net/ethernet/ralink/Makefile
  32. create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.c
  33. create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.h
  34. create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.c
  35. create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.h
  36. create mode 100644 drivers/net/ethernet/ralink/mdio.c
  37. create mode 100644 drivers/net/ethernet/ralink/mdio.h
  38. create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.c
  39. create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.h
  40. create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.c
  41. create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.h
  42. create mode 100644 drivers/net/ethernet/ralink/soc_mt7620.c
  43. create mode 100644 drivers/net/ethernet/ralink/soc_rt2880.c
  44. create mode 100644 drivers/net/ethernet/ralink/soc_rt305x.c
  45. create mode 100644 drivers/net/ethernet/ralink/soc_rt3883.c
  46. --- /dev/null
  47. +++ b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
  48. @@ -0,0 +1,27 @@
  49. +/*
  50. + * Ralink RT305x SoC platform device registration
  51. + *
  52. + * Copyright (C) 2010 Gabor Juhos <[email protected]>
  53. + *
  54. + * This program is free software; you can redistribute it and/or modify it
  55. + * under the terms of the GNU General Public License version 2 as published
  56. + * by the Free Software Foundation.
  57. + */
  58. +
  59. +#ifndef _RT305X_ESW_PLATFORM_H
  60. +#define _RT305X_ESW_PLATFORM_H
  61. +
  62. +enum {
  63. + RT305X_ESW_VLAN_CONFIG_NONE = 0,
  64. + RT305X_ESW_VLAN_CONFIG_LLLLW,
  65. + RT305X_ESW_VLAN_CONFIG_WLLLL,
  66. +};
  67. +
  68. +struct rt305x_esw_platform_data
  69. +{
  70. + u8 vlan_config;
  71. + u32 reg_initval_fct2;
  72. + u32 reg_initval_fpa2;
  73. +};
  74. +
  75. +#endif /* _RT305X_ESW_PLATFORM_H */
  76. --- a/arch/mips/ralink/rt305x.c
  77. +++ b/arch/mips/ralink/rt305x.c
  78. @@ -221,6 +221,7 @@ void __init ralink_clk_init(void)
  79. }
  80. ralink_clk_add("cpu", cpu_rate);
  81. + ralink_clk_add("sys", sys_rate);
  82. ralink_clk_add("10000b00.spi", sys_rate);
  83. ralink_clk_add("10000100.timer", wdt_rate);
  84. ralink_clk_add("10000120.watchdog", wdt_rate);
  85. --- a/drivers/net/ethernet/Kconfig
  86. +++ b/drivers/net/ethernet/Kconfig
  87. @@ -135,6 +135,7 @@ config ETHOC
  88. source "drivers/net/ethernet/packetengines/Kconfig"
  89. source "drivers/net/ethernet/pasemi/Kconfig"
  90. source "drivers/net/ethernet/qlogic/Kconfig"
  91. +source "drivers/net/ethernet/ralink/Kconfig"
  92. source "drivers/net/ethernet/realtek/Kconfig"
  93. source "drivers/net/ethernet/renesas/Kconfig"
  94. source "drivers/net/ethernet/rdc/Kconfig"
  95. --- a/drivers/net/ethernet/Makefile
  96. +++ b/drivers/net/ethernet/Makefile
  97. @@ -53,6 +53,7 @@ obj-$(CONFIG_ETHOC) += ethoc.o
  98. obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
  99. obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
  100. obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
  101. +obj-$(CONFIG_NET_RALINK) += ralink/
  102. obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
  103. obj-$(CONFIG_SH_ETH) += renesas/
  104. obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
  105. --- /dev/null
  106. +++ b/drivers/net/ethernet/ralink/Kconfig
  107. @@ -0,0 +1,31 @@
  108. +config NET_RALINK
  109. + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
  110. + depends on RALINK
  111. + help
  112. + This driver supports the ethernet mac inside the ralink wisocs
  113. +
  114. +if NET_RALINK
  115. +
  116. +config NET_RALINK_MDIO
  117. + def_bool NET_RALINK
  118. + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
  119. + select PHYLIB
  120. +
  121. +config NET_RALINK_MDIO_RT2880
  122. + def_bool NET_RALINK
  123. + depends on (SOC_RT288X || SOC_RT3883)
  124. + select NET_RALINK_MDIO
  125. +
  126. +config NET_RALINK_ESW_RT3052
  127. + def_bool NET_RALINK
  128. + depends on SOC_RT305X
  129. + select PHYLIB
  130. + select SWCONFIG
  131. +
  132. +config NET_RALINK_GSW_MT7620
  133. + def_bool NET_RALINK
  134. + depends on SOC_MT7620
  135. + select NET_RALINK_MDIO
  136. + select PHYLIB
  137. + select SWCONFIG
  138. +endif
  139. --- /dev/null
  140. +++ b/drivers/net/ethernet/ralink/Makefile
  141. @@ -0,0 +1,18 @@
  142. +#
  143. +# Makefile for the Ralink SoCs built-in ethernet macs
  144. +#
  145. +
  146. +ralink-eth-y += ralink_soc_eth.o
  147. +
  148. +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
  149. +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
  150. +
  151. +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
  152. +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o mt7530.o
  153. +
  154. +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
  155. +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
  156. +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
  157. +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
  158. +
  159. +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
  160. --- /dev/null
  161. +++ b/drivers/net/ethernet/ralink/esw_rt3052.c
  162. @@ -0,0 +1,1463 @@
  163. +/*
  164. + * This program is free software; you can redistribute it and/or modify
  165. + * it under the terms of the GNU General Public License as published by
  166. + * the Free Software Foundation; version 2 of the License
  167. + *
  168. + * This program is distributed in the hope that it will be useful,
  169. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  170. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  171. + * GNU General Public License for more details.
  172. + *
  173. + * You should have received a copy of the GNU General Public License
  174. + * along with this program; if not, write to the Free Software
  175. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  176. + *
  177. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  178. + */
  179. +
  180. +#include <linux/module.h>
  181. +#include <linux/kernel.h>
  182. +#include <linux/types.h>
  183. +#include <linux/dma-mapping.h>
  184. +#include <linux/init.h>
  185. +#include <linux/skbuff.h>
  186. +#include <linux/etherdevice.h>
  187. +#include <linux/ethtool.h>
  188. +#include <linux/platform_device.h>
  189. +#include <linux/of_device.h>
  190. +#include <linux/clk.h>
  191. +#include <linux/of_net.h>
  192. +#include <linux/of_mdio.h>
  193. +
  194. +#include <asm/mach-ralink/ralink_regs.h>
  195. +
  196. +#include "ralink_soc_eth.h"
  197. +
  198. +#include <linux/ioport.h>
  199. +#include <linux/switch.h>
  200. +#include <linux/mii.h>
  201. +
  202. +#include <ralink_regs.h>
  203. +#include <asm/mach-ralink/rt305x.h>
  204. +#include <asm/mach-ralink/rt305x_esw_platform.h>
  205. +
  206. +/*
  207. + * HW limitations for this switch:
  208. + * - No large frame support (PKT_MAX_LEN at most 1536)
  209. + * - Can't have untagged vlan and tagged vlan on one port at the same time,
  210. + * though this might be possible using the undocumented PPE.
  211. + */
  212. +
  213. +#define RT305X_ESW_REG_ISR 0x00
  214. +#define RT305X_ESW_REG_IMR 0x04
  215. +#define RT305X_ESW_REG_FCT0 0x08
  216. +#define RT305X_ESW_REG_PFC1 0x14
  217. +#define RT305X_ESW_REG_ATS 0x24
  218. +#define RT305X_ESW_REG_ATS0 0x28
  219. +#define RT305X_ESW_REG_ATS1 0x2c
  220. +#define RT305X_ESW_REG_ATS2 0x30
  221. +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
  222. +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
  223. +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
  224. +#define RT305X_ESW_REG_POA 0x80
  225. +#define RT305X_ESW_REG_FPA 0x84
  226. +#define RT305X_ESW_REG_SOCPC 0x8c
  227. +#define RT305X_ESW_REG_POC0 0x90
  228. +#define RT305X_ESW_REG_POC1 0x94
  229. +#define RT305X_ESW_REG_POC2 0x98
  230. +#define RT305X_ESW_REG_SGC 0x9c
  231. +#define RT305X_ESW_REG_STRT 0xa0
  232. +#define RT305X_ESW_REG_PCR0 0xc0
  233. +#define RT305X_ESW_REG_PCR1 0xc4
  234. +#define RT305X_ESW_REG_FPA2 0xc8
  235. +#define RT305X_ESW_REG_FCT2 0xcc
  236. +#define RT305X_ESW_REG_SGC2 0xe4
  237. +#define RT305X_ESW_REG_P0LED 0xa4
  238. +#define RT305X_ESW_REG_P1LED 0xa8
  239. +#define RT305X_ESW_REG_P2LED 0xac
  240. +#define RT305X_ESW_REG_P3LED 0xb0
  241. +#define RT305X_ESW_REG_P4LED 0xb4
  242. +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
  243. +#define RT305X_ESW_REG_P1PC 0xec
  244. +#define RT305X_ESW_REG_P2PC 0xf0
  245. +#define RT305X_ESW_REG_P3PC 0xf4
  246. +#define RT305X_ESW_REG_P4PC 0xf8
  247. +#define RT305X_ESW_REG_P5PC 0xfc
  248. +
  249. +#define RT305X_ESW_LED_LINK 0
  250. +#define RT305X_ESW_LED_100M 1
  251. +#define RT305X_ESW_LED_DUPLEX 2
  252. +#define RT305X_ESW_LED_ACTIVITY 3
  253. +#define RT305X_ESW_LED_COLLISION 4
  254. +#define RT305X_ESW_LED_LINKACT 5
  255. +#define RT305X_ESW_LED_DUPLCOLL 6
  256. +#define RT305X_ESW_LED_10MACT 7
  257. +#define RT305X_ESW_LED_100MACT 8
  258. +/* Additional led states not in datasheet: */
  259. +#define RT305X_ESW_LED_BLINK 10
  260. +#define RT305X_ESW_LED_ON 12
  261. +
  262. +#define RT305X_ESW_LINK_S 25
  263. +#define RT305X_ESW_DUPLEX_S 9
  264. +#define RT305X_ESW_SPD_S 0
  265. +
  266. +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
  267. +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
  268. +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
  269. +
  270. +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
  271. +
  272. +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
  273. +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
  274. +
  275. +#define RT305X_ESW_PVIDC_PVID_M 0xfff
  276. +#define RT305X_ESW_PVIDC_PVID_S 12
  277. +
  278. +#define RT305X_ESW_VLANI_VID_M 0xfff
  279. +#define RT305X_ESW_VLANI_VID_S 12
  280. +
  281. +#define RT305X_ESW_VMSC_MSC_M 0xff
  282. +#define RT305X_ESW_VMSC_MSC_S 8
  283. +
  284. +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
  285. +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
  286. +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
  287. +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
  288. +
  289. +#define RT305X_ESW_POC0_EN_BP_S 0
  290. +#define RT305X_ESW_POC0_EN_FC_S 8
  291. +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
  292. +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
  293. +#define RT305X_ESW_POC0_DIS_PORT_S 23
  294. +
  295. +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
  296. +#define RT305X_ESW_POC2_UNTAG_EN_S 0
  297. +#define RT305X_ESW_POC2_ENAGING_S 8
  298. +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
  299. +
  300. +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
  301. +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
  302. +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
  303. +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
  304. +
  305. +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
  306. +#define RT305X_ESW_PFC1_EN_VLAN_S 16
  307. +#define RT305X_ESW_PFC1_EN_TOS_S 24
  308. +
  309. +#define RT305X_ESW_VLAN_NONE 0xfff
  310. +
  311. +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
  312. +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
  313. +
  314. +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
  315. +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
  316. +
  317. +#define RT305X_ESW_POA_LINK_MASK 0x1f
  318. +#define RT305X_ESW_POA_LINK_SHIFT 25
  319. +
  320. +#define RT305X_ESW_PORT_ST_CHG BIT(26)
  321. +#define RT305X_ESW_PORT0 0
  322. +#define RT305X_ESW_PORT1 1
  323. +#define RT305X_ESW_PORT2 2
  324. +#define RT305X_ESW_PORT3 3
  325. +#define RT305X_ESW_PORT4 4
  326. +#define RT305X_ESW_PORT5 5
  327. +#define RT305X_ESW_PORT6 6
  328. +
  329. +#define RT305X_ESW_PORTS_NONE 0
  330. +
  331. +#define RT305X_ESW_PMAP_LLLLLL 0x3f
  332. +#define RT305X_ESW_PMAP_LLLLWL 0x2f
  333. +#define RT305X_ESW_PMAP_WLLLLL 0x3e
  334. +
  335. +#define RT305X_ESW_PORTS_INTERNAL \
  336. + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
  337. + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
  338. + BIT(RT305X_ESW_PORT4))
  339. +
  340. +#define RT305X_ESW_PORTS_NOCPU \
  341. + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
  342. +
  343. +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
  344. +
  345. +#define RT305X_ESW_PORTS_ALL \
  346. + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
  347. +
  348. +#define RT305X_ESW_NUM_VLANS 16
  349. +#define RT305X_ESW_NUM_VIDS 4096
  350. +#define RT305X_ESW_NUM_PORTS 7
  351. +#define RT305X_ESW_NUM_LANWAN 6
  352. +#define RT305X_ESW_NUM_LEDS 5
  353. +
  354. +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
  355. +#define RT5350_EWS_REG_LED_POLARITY 0x168
  356. +#define RT5350_RESET_EPHY BIT(24)
  357. +#define SYSC_REG_RESET_CTRL 0x34
  358. +
  359. +enum {
  360. + /* Global attributes. */
  361. + RT305X_ESW_ATTR_ENABLE_VLAN,
  362. + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
  363. + RT305X_ESW_ATTR_BC_STATUS,
  364. + RT305X_ESW_ATTR_LED_FREQ,
  365. + /* Port attributes. */
  366. + RT305X_ESW_ATTR_PORT_DISABLE,
  367. + RT305X_ESW_ATTR_PORT_DOUBLETAG,
  368. + RT305X_ESW_ATTR_PORT_UNTAG,
  369. + RT305X_ESW_ATTR_PORT_LED,
  370. + RT305X_ESW_ATTR_PORT_LAN,
  371. + RT305X_ESW_ATTR_PORT_RECV_BAD,
  372. + RT305X_ESW_ATTR_PORT_RECV_GOOD,
  373. + RT5350_ESW_ATTR_PORT_TR_BAD,
  374. + RT5350_ESW_ATTR_PORT_TR_GOOD,
  375. +};
  376. +
  377. +struct esw_port {
  378. + bool disable;
  379. + bool doubletag;
  380. + bool untag;
  381. + u8 led;
  382. + u16 pvid;
  383. +};
  384. +
  385. +struct esw_vlan {
  386. + u8 ports;
  387. + u16 vid;
  388. +};
  389. +
  390. +struct rt305x_esw {
  391. + struct device *dev;
  392. + void __iomem *base;
  393. + int irq;
  394. + const struct rt305x_esw_platform_data *pdata;
  395. + /* Protects against concurrent register rmw operations. */
  396. + spinlock_t reg_rw_lock;
  397. +
  398. + unsigned char port_map;
  399. + unsigned int reg_initval_fct2;
  400. + unsigned int reg_initval_fpa2;
  401. + unsigned int reg_led_polarity;
  402. +
  403. +
  404. + struct switch_dev swdev;
  405. + bool global_vlan_enable;
  406. + bool alt_vlan_disable;
  407. + int bc_storm_protect;
  408. + int led_frequency;
  409. + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
  410. + struct esw_port ports[RT305X_ESW_NUM_PORTS];
  411. +
  412. +};
  413. +
  414. +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
  415. +{
  416. + __raw_writel(val, esw->base + reg);
  417. +}
  418. +
  419. +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
  420. +{
  421. + return __raw_readl(esw->base + reg);
  422. +}
  423. +
  424. +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
  425. + unsigned long val)
  426. +{
  427. + unsigned long t;
  428. +
  429. + t = __raw_readl(esw->base + reg) & ~mask;
  430. + __raw_writel(t | val, esw->base + reg);
  431. +}
  432. +
  433. +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
  434. + unsigned long val)
  435. +{
  436. + unsigned long flags;
  437. +
  438. + spin_lock_irqsave(&esw->reg_rw_lock, flags);
  439. + esw_rmw_raw(esw, reg, mask, val);
  440. + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
  441. +}
  442. +
  443. +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
  444. + u32 write_data)
  445. +{
  446. + unsigned long t_start = jiffies;
  447. + int ret = 0;
  448. +
  449. + while (1) {
  450. + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
  451. + RT305X_ESW_PCR1_WT_DONE))
  452. + break;
  453. + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
  454. + ret = 1;
  455. + goto out;
  456. + }
  457. + }
  458. +
  459. + write_data &= 0xffff;
  460. + esw_w32(esw,
  461. + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
  462. + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
  463. + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
  464. + RT305X_ESW_REG_PCR0);
  465. +
  466. + t_start = jiffies;
  467. + while (1) {
  468. + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
  469. + RT305X_ESW_PCR1_WT_DONE)
  470. + break;
  471. +
  472. + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
  473. + ret = 1;
  474. + break;
  475. + }
  476. + }
  477. +out:
  478. + if (ret)
  479. + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
  480. + return ret;
  481. +}
  482. +
  483. +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
  484. +{
  485. + unsigned s;
  486. + unsigned val;
  487. +
  488. + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
  489. + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
  490. + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
  491. +
  492. + return val;
  493. +}
  494. +
  495. +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
  496. +{
  497. + unsigned s;
  498. +
  499. + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
  500. + esw_rmw(esw,
  501. + RT305X_ESW_REG_VLANI(vlan / 2),
  502. + RT305X_ESW_VLANI_VID_M << s,
  503. + (vid & RT305X_ESW_VLANI_VID_M) << s);
  504. +}
  505. +
  506. +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
  507. +{
  508. + unsigned s, val;
  509. +
  510. + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
  511. + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
  512. + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
  513. +}
  514. +
  515. +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
  516. +{
  517. + unsigned s;
  518. +
  519. + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
  520. + esw_rmw(esw,
  521. + RT305X_ESW_REG_PVIDC(port / 2),
  522. + RT305X_ESW_PVIDC_PVID_M << s,
  523. + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
  524. +}
  525. +
  526. +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
  527. +{
  528. + unsigned s, val;
  529. +
  530. + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
  531. + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
  532. + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
  533. +
  534. + return val;
  535. +}
  536. +
  537. +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
  538. +{
  539. + unsigned s;
  540. +
  541. + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
  542. + esw_rmw(esw,
  543. + RT305X_ESW_REG_VMSC(vlan / 4),
  544. + RT305X_ESW_VMSC_MSC_M << s,
  545. + (msc & RT305X_ESW_VMSC_MSC_M) << s);
  546. +}
  547. +
  548. +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
  549. +{
  550. + unsigned reg;
  551. + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
  552. + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
  553. + RT305X_ESW_POC0_DIS_PORT_M;
  554. +}
  555. +
  556. +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
  557. +{
  558. + unsigned old_mask;
  559. + unsigned enable_mask;
  560. + unsigned changed;
  561. + int i;
  562. +
  563. + old_mask = esw_get_port_disable(esw);
  564. + changed = old_mask ^ disable_mask;
  565. + enable_mask = old_mask & disable_mask;
  566. +
  567. + /* enable before writing to MII */
  568. + esw_rmw(esw, RT305X_ESW_REG_POC0,
  569. + (RT305X_ESW_POC0_DIS_PORT_M <<
  570. + RT305X_ESW_POC0_DIS_PORT_S),
  571. + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
  572. +
  573. + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
  574. + if (!(changed & (1 << i)))
  575. + continue;
  576. + if (disable_mask & (1 << i)) {
  577. + /* disable */
  578. + rt305x_mii_write(esw, i, MII_BMCR,
  579. + BMCR_PDOWN);
  580. + } else {
  581. + /* enable */
  582. + rt305x_mii_write(esw, i, MII_BMCR,
  583. + BMCR_FULLDPLX |
  584. + BMCR_ANENABLE |
  585. + BMCR_ANRESTART |
  586. + BMCR_SPEED100);
  587. + }
  588. + }
  589. +
  590. + /* disable after writing to MII */
  591. + esw_rmw(esw, RT305X_ESW_REG_POC0,
  592. + (RT305X_ESW_POC0_DIS_PORT_M <<
  593. + RT305X_ESW_POC0_DIS_PORT_S),
  594. + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
  595. +}
  596. +
  597. +static void esw_set_gsc(struct rt305x_esw *esw)
  598. +{
  599. + esw_rmw(esw, RT305X_ESW_REG_SGC,
  600. + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
  601. + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
  602. + esw_rmw(esw, RT305X_ESW_REG_SGC,
  603. + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
  604. + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
  605. +}
  606. +
  607. +static int esw_apply_config(struct switch_dev *dev);
  608. +
  609. +static void esw_hw_init(struct rt305x_esw *esw)
  610. +{
  611. + int i;
  612. + u8 port_disable = 0;
  613. + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
  614. +
  615. + /* vodoo from original driver */
  616. + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
  617. + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
  618. + /* Port priority 1 for all ports, vlan enabled. */
  619. + esw_w32(esw, 0x00005555 |
  620. + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
  621. + RT305X_ESW_REG_PFC1);
  622. +
  623. + /* Enable Back Pressure, and Flow Control */
  624. + esw_w32(esw,
  625. + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
  626. + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
  627. + RT305X_ESW_REG_POC0);
  628. +
  629. + /* Enable Aging, and VLAN TAG removal */
  630. + esw_w32(esw,
  631. + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
  632. + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
  633. + RT305X_ESW_REG_POC2);
  634. +
  635. + if (esw->reg_initval_fct2)
  636. + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
  637. + else
  638. + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
  639. +
  640. + /*
  641. + * 300s aging timer, max packet len 1536, broadcast storm prevention
  642. + * disabled, disable collision abort, mac xor48 hash, 10 packet back
  643. + * pressure jam, GMII disable was_transmit, back pressure disabled,
  644. + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
  645. + * ports.
  646. + */
  647. + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
  648. +
  649. + /* Setup SoC Port control register */
  650. + esw_w32(esw,
  651. + (RT305X_ESW_SOCPC_CRC_PADDING |
  652. + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
  653. + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
  654. + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
  655. + RT305X_ESW_REG_SOCPC);
  656. +
  657. + if (esw->reg_initval_fpa2)
  658. + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
  659. + else
  660. + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
  661. + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
  662. +
  663. + /* Force Link/Activity on ports */
  664. + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
  665. + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
  666. + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
  667. + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
  668. + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
  669. +
  670. + /* Copy disabled port configuration from bootloader setup */
  671. + port_disable = esw_get_port_disable(esw);
  672. + for (i = 0; i < 6; i++)
  673. + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
  674. +
  675. + if (soc_is_rt3352()) {
  676. + /* reset EPHY */
  677. + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
  678. + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
  679. + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
  680. +
  681. + rt305x_mii_write(esw, 0, 31, 0x8000);
  682. + for (i = 0; i < 5; i++) {
  683. + if (esw->ports[i].disable) {
  684. + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
  685. + } else {
  686. + rt305x_mii_write(esw, i, MII_BMCR,
  687. + BMCR_FULLDPLX |
  688. + BMCR_ANENABLE |
  689. + BMCR_SPEED100);
  690. + }
  691. + /* TX10 waveform coefficient LSB=0 disable PHY */
  692. + rt305x_mii_write(esw, i, 26, 0x1601);
  693. + /* TX100/TX10 AD/DA current bias */
  694. + rt305x_mii_write(esw, i, 29, 0x7016);
  695. + /* TX100 slew rate control */
  696. + rt305x_mii_write(esw, i, 30, 0x0038);
  697. + }
  698. +
  699. + /* select global register */
  700. + rt305x_mii_write(esw, 0, 31, 0x0);
  701. + /* enlarge agcsel threshold 3 and threshold 2 */
  702. + rt305x_mii_write(esw, 0, 1, 0x4a40);
  703. + /* enlarge agcsel threshold 5 and threshold 4 */
  704. + rt305x_mii_write(esw, 0, 2, 0x6254);
  705. + /* enlarge agcsel threshold */
  706. + rt305x_mii_write(esw, 0, 3, 0xa17f);
  707. + rt305x_mii_write(esw, 0,12, 0x7eaa);
  708. + /* longer TP_IDL tail length */
  709. + rt305x_mii_write(esw, 0, 14, 0x65);
  710. + /* increased squelch pulse count threshold. */
  711. + rt305x_mii_write(esw, 0, 16, 0x0684);
  712. + /* set TX10 signal amplitude threshold to minimum */
  713. + rt305x_mii_write(esw, 0, 17, 0x0fe0);
  714. + /* set squelch amplitude to higher threshold */
  715. + rt305x_mii_write(esw, 0, 18, 0x40ba);
  716. + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
  717. + rt305x_mii_write(esw, 0, 22, 0x253f);
  718. + /* set PLL/Receive bias current are calibrated */
  719. + rt305x_mii_write(esw, 0, 27, 0x2fda);
  720. + /* change PLL/Receive bias current to internal(RT3350) */
  721. + rt305x_mii_write(esw, 0, 28, 0xc410);
  722. + /* change PLL bias current to internal(RT3052_MP3) */
  723. + rt305x_mii_write(esw, 0, 29, 0x598b);
  724. + /* select local register */
  725. + rt305x_mii_write(esw, 0, 31, 0x8000);
  726. + } else if (soc_is_rt5350()) {
  727. + /* reset EPHY */
  728. + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
  729. + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
  730. + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
  731. +
  732. + /* set the led polarity */
  733. + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
  734. +
  735. + /* local registers */
  736. + rt305x_mii_write(esw, 0, 31, 0x8000);
  737. + for (i = 0; i < 5; i++) {
  738. + if (esw->ports[i].disable) {
  739. + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
  740. + } else {
  741. + rt305x_mii_write(esw, i, MII_BMCR,
  742. + BMCR_FULLDPLX |
  743. + BMCR_ANENABLE |
  744. + BMCR_SPEED100);
  745. + }
  746. + /* TX10 waveform coefficient LSB=0 disable PHY */
  747. + rt305x_mii_write(esw, i, 26, 0x1601);
  748. + /* TX100/TX10 AD/DA current bias */
  749. + rt305x_mii_write(esw, i, 29, 0x7015);
  750. + /* TX100 slew rate control */
  751. + rt305x_mii_write(esw, i, 30, 0x0038);
  752. + }
  753. +
  754. + /* global registers */
  755. + rt305x_mii_write(esw, 0, 31, 0x0);
  756. + /* enlarge agcsel threshold 3 and threshold 2 */
  757. + rt305x_mii_write(esw, 0, 1, 0x4a40);
  758. + /* enlarge agcsel threshold 5 and threshold 4 */
  759. + rt305x_mii_write(esw, 0, 2, 0x6254);
  760. + /* enlarge agcsel threshold 6 */
  761. + rt305x_mii_write(esw, 0, 3, 0xa17f);
  762. + rt305x_mii_write(esw, 0, 12, 0x7eaa);
  763. + /* longer TP_IDL tail length */
  764. + rt305x_mii_write(esw, 0, 14, 0x65);
  765. + /* increased squelch pulse count threshold. */
  766. + rt305x_mii_write(esw, 0, 16, 0x0684);
  767. + /* set TX10 signal amplitude threshold to minimum */
  768. + rt305x_mii_write(esw, 0, 17, 0x0fe0);
  769. + /* set squelch amplitude to higher threshold */
  770. + rt305x_mii_write(esw, 0, 18, 0x40ba);
  771. + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
  772. + rt305x_mii_write(esw, 0, 22, 0x253f);
  773. + /* set PLL/Receive bias current are calibrated */
  774. + rt305x_mii_write(esw, 0, 27, 0x2fda);
  775. + /* change PLL/Receive bias current to internal(RT3350) */
  776. + rt305x_mii_write(esw, 0, 28, 0xc410);
  777. + /* change PLL bias current to internal(RT3052_MP3) */
  778. + rt305x_mii_write(esw, 0, 29, 0x598b);
  779. + /* select local register */
  780. + rt305x_mii_write(esw, 0, 31, 0x8000);
  781. + } else {
  782. + rt305x_mii_write(esw, 0, 31, 0x8000);
  783. + for (i = 0; i < 5; i++) {
  784. + if (esw->ports[i].disable) {
  785. + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
  786. + } else {
  787. + rt305x_mii_write(esw, i, MII_BMCR,
  788. + BMCR_FULLDPLX |
  789. + BMCR_ANENABLE |
  790. + BMCR_SPEED100);
  791. + }
  792. + /* TX10 waveform coefficient */
  793. + rt305x_mii_write(esw, i, 26, 0x1601);
  794. + /* TX100/TX10 AD/DA current bias */
  795. + rt305x_mii_write(esw, i, 29, 0x7058);
  796. + /* TX100 slew rate control */
  797. + rt305x_mii_write(esw, i, 30, 0x0018);
  798. + }
  799. +
  800. + /* PHY IOT */
  801. + /* select global register */
  802. + rt305x_mii_write(esw, 0, 31, 0x0);
  803. + /* tune TP_IDL tail and head waveform */
  804. + rt305x_mii_write(esw, 0, 22, 0x052f);
  805. + /* set TX10 signal amplitude threshold to minimum */
  806. + rt305x_mii_write(esw, 0, 17, 0x0fe0);
  807. + /* set squelch amplitude to higher threshold */
  808. + rt305x_mii_write(esw, 0, 18, 0x40ba);
  809. + /* longer TP_IDL tail length */
  810. + rt305x_mii_write(esw, 0, 14, 0x65);
  811. + /* select local register */
  812. + rt305x_mii_write(esw, 0, 31, 0x8000);
  813. + }
  814. +
  815. + if (esw->port_map)
  816. + port_map = esw->port_map;
  817. + else
  818. + port_map = RT305X_ESW_PMAP_LLLLLL;
  819. +
  820. + /*
  821. + * Unused HW feature, but still nice to be consistent here...
  822. + * This is also exported to userspace ('lan' attribute) so it's
  823. + * conveniently usable to decide which ports go into the wan vlan by
  824. + * default.
  825. + */
  826. + esw_rmw(esw, RT305X_ESW_REG_SGC2,
  827. + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
  828. + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
  829. +
  830. + /* make the switch leds blink */
  831. + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
  832. + esw->ports[i].led = 0x05;
  833. +
  834. + /* Apply the empty config. */
  835. + esw_apply_config(&esw->swdev);
  836. +
  837. + /* Only unmask the port change interrupt */
  838. + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
  839. +}
  840. +
  841. +static irqreturn_t esw_interrupt(int irq, void *_esw)
  842. +{
  843. + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
  844. + u32 status;
  845. +
  846. + status = esw_r32(esw, RT305X_ESW_REG_ISR);
  847. + if (status & RT305X_ESW_PORT_ST_CHG) {
  848. + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
  849. + link >>= RT305X_ESW_POA_LINK_SHIFT;
  850. + link &= RT305X_ESW_POA_LINK_MASK;
  851. + dev_info(esw->dev, "link changed 0x%02X\n", link);
  852. + }
  853. + esw_w32(esw, status, RT305X_ESW_REG_ISR);
  854. +
  855. + return IRQ_HANDLED;
  856. +}
  857. +
  858. +static int esw_apply_config(struct switch_dev *dev)
  859. +{
  860. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  861. + int i;
  862. + u8 disable = 0;
  863. + u8 doubletag = 0;
  864. + u8 en_vlan = 0;
  865. + u8 untag = 0;
  866. +
  867. + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
  868. + u32 vid, vmsc;
  869. + if (esw->global_vlan_enable) {
  870. + vid = esw->vlans[i].vid;
  871. + vmsc = esw->vlans[i].ports;
  872. + } else {
  873. + vid = RT305X_ESW_VLAN_NONE;
  874. + vmsc = RT305X_ESW_PORTS_NONE;
  875. + }
  876. + esw_set_vlan_id(esw, i, vid);
  877. + esw_set_vmsc(esw, i, vmsc);
  878. + }
  879. +
  880. + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
  881. + u32 pvid;
  882. + disable |= esw->ports[i].disable << i;
  883. + if (esw->global_vlan_enable) {
  884. + doubletag |= esw->ports[i].doubletag << i;
  885. + en_vlan |= 1 << i;
  886. + untag |= esw->ports[i].untag << i;
  887. + pvid = esw->ports[i].pvid;
  888. + } else {
  889. + int x = esw->alt_vlan_disable ? 0 : 1;
  890. + doubletag |= x << i;
  891. + en_vlan |= x << i;
  892. + untag |= x << i;
  893. + pvid = 0;
  894. + }
  895. + esw_set_pvid(esw, i, pvid);
  896. + if (i < RT305X_ESW_NUM_LEDS)
  897. + esw_w32(esw, esw->ports[i].led,
  898. + RT305X_ESW_REG_P0LED + 4*i);
  899. + }
  900. +
  901. + esw_set_gsc(esw);
  902. + esw_set_port_disable(esw, disable);
  903. + esw_rmw(esw, RT305X_ESW_REG_SGC2,
  904. + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
  905. + RT305X_ESW_SGC2_DOUBLE_TAG_S),
  906. + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
  907. + esw_rmw(esw, RT305X_ESW_REG_PFC1,
  908. + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
  909. + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
  910. + esw_rmw(esw, RT305X_ESW_REG_POC2,
  911. + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
  912. + untag << RT305X_ESW_POC2_UNTAG_EN_S);
  913. +
  914. + if (!esw->global_vlan_enable) {
  915. + /*
  916. + * Still need to put all ports into vlan 0 or they'll be
  917. + * isolated.
  918. + * NOTE: vlan 0 is special, no vlan tag is prepended
  919. + */
  920. + esw_set_vlan_id(esw, 0, 0);
  921. + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
  922. + }
  923. +
  924. + return 0;
  925. +}
  926. +
  927. +static int esw_reset_switch(struct switch_dev *dev)
  928. +{
  929. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  930. +
  931. + esw->global_vlan_enable = 0;
  932. + memset(esw->ports, 0, sizeof(esw->ports));
  933. + memset(esw->vlans, 0, sizeof(esw->vlans));
  934. + esw_hw_init(esw);
  935. +
  936. + return 0;
  937. +}
  938. +
  939. +static int esw_get_vlan_enable(struct switch_dev *dev,
  940. + const struct switch_attr *attr,
  941. + struct switch_val *val)
  942. +{
  943. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  944. +
  945. + val->value.i = esw->global_vlan_enable;
  946. +
  947. + return 0;
  948. +}
  949. +
  950. +static int esw_set_vlan_enable(struct switch_dev *dev,
  951. + const struct switch_attr *attr,
  952. + struct switch_val *val)
  953. +{
  954. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  955. +
  956. + esw->global_vlan_enable = val->value.i != 0;
  957. +
  958. + return 0;
  959. +}
  960. +
  961. +static int esw_get_alt_vlan_disable(struct switch_dev *dev,
  962. + const struct switch_attr *attr,
  963. + struct switch_val *val)
  964. +{
  965. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  966. +
  967. + val->value.i = esw->alt_vlan_disable;
  968. +
  969. + return 0;
  970. +}
  971. +
  972. +static int esw_set_alt_vlan_disable(struct switch_dev *dev,
  973. + const struct switch_attr *attr,
  974. + struct switch_val *val)
  975. +{
  976. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  977. +
  978. + esw->alt_vlan_disable = val->value.i != 0;
  979. +
  980. + return 0;
  981. +}
  982. +
  983. +static int
  984. +rt305x_esw_set_bc_status(struct switch_dev *dev,
  985. + const struct switch_attr *attr,
  986. + struct switch_val *val)
  987. +{
  988. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  989. +
  990. + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
  991. +
  992. + return 0;
  993. +}
  994. +
  995. +static int
  996. +rt305x_esw_get_bc_status(struct switch_dev *dev,
  997. + const struct switch_attr *attr,
  998. + struct switch_val *val)
  999. +{
  1000. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1001. +
  1002. + val->value.i = esw->bc_storm_protect;
  1003. +
  1004. + return 0;
  1005. +}
  1006. +
  1007. +static int
  1008. +rt305x_esw_set_led_freq(struct switch_dev *dev,
  1009. + const struct switch_attr *attr,
  1010. + struct switch_val *val)
  1011. +{
  1012. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1013. +
  1014. + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
  1015. +
  1016. + return 0;
  1017. +}
  1018. +
  1019. +static int
  1020. +rt305x_esw_get_led_freq(struct switch_dev *dev,
  1021. + const struct switch_attr *attr,
  1022. + struct switch_val *val)
  1023. +{
  1024. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1025. +
  1026. + val->value.i = esw->led_frequency;
  1027. +
  1028. + return 0;
  1029. +}
  1030. +
  1031. +static int esw_get_port_link(struct switch_dev *dev,
  1032. + int port,
  1033. + struct switch_port_link *link)
  1034. +{
  1035. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1036. + u32 speed, poa;
  1037. +
  1038. + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
  1039. + return -EINVAL;
  1040. +
  1041. + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
  1042. +
  1043. + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
  1044. + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
  1045. + if (port < RT305X_ESW_NUM_LEDS) {
  1046. + speed = (poa >> RT305X_ESW_SPD_S) & 1;
  1047. + } else {
  1048. + if (port == RT305X_ESW_NUM_PORTS - 1)
  1049. + poa >>= 1;
  1050. + speed = (poa >> RT305X_ESW_SPD_S) & 3;
  1051. + }
  1052. + switch (speed) {
  1053. + case 0:
  1054. + link->speed = SWITCH_PORT_SPEED_10;
  1055. + break;
  1056. + case 1:
  1057. + link->speed = SWITCH_PORT_SPEED_100;
  1058. + break;
  1059. + case 2:
  1060. + case 3: /* forced gige speed can be 2 or 3 */
  1061. + link->speed = SWITCH_PORT_SPEED_1000;
  1062. + break;
  1063. + default:
  1064. + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  1065. + break;
  1066. + }
  1067. +
  1068. + return 0;
  1069. +}
  1070. +
  1071. +static int esw_get_port_bool(struct switch_dev *dev,
  1072. + const struct switch_attr *attr,
  1073. + struct switch_val *val)
  1074. +{
  1075. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1076. + int idx = val->port_vlan;
  1077. + u32 x, reg, shift;
  1078. +
  1079. + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
  1080. + return -EINVAL;
  1081. +
  1082. + switch (attr->id) {
  1083. + case RT305X_ESW_ATTR_PORT_DISABLE:
  1084. + reg = RT305X_ESW_REG_POC0;
  1085. + shift = RT305X_ESW_POC0_DIS_PORT_S;
  1086. + break;
  1087. + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
  1088. + reg = RT305X_ESW_REG_SGC2;
  1089. + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
  1090. + break;
  1091. + case RT305X_ESW_ATTR_PORT_UNTAG:
  1092. + reg = RT305X_ESW_REG_POC2;
  1093. + shift = RT305X_ESW_POC2_UNTAG_EN_S;
  1094. + break;
  1095. + case RT305X_ESW_ATTR_PORT_LAN:
  1096. + reg = RT305X_ESW_REG_SGC2;
  1097. + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
  1098. + if (idx >= RT305X_ESW_NUM_LANWAN)
  1099. + return -EINVAL;
  1100. + break;
  1101. + default:
  1102. + return -EINVAL;
  1103. + }
  1104. +
  1105. + x = esw_r32(esw, reg);
  1106. + val->value.i = (x >> (idx + shift)) & 1;
  1107. +
  1108. + return 0;
  1109. +}
  1110. +
  1111. +static int esw_set_port_bool(struct switch_dev *dev,
  1112. + const struct switch_attr *attr,
  1113. + struct switch_val *val)
  1114. +{
  1115. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1116. + int idx = val->port_vlan;
  1117. +
  1118. + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
  1119. + val->value.i < 0 || val->value.i > 1)
  1120. + return -EINVAL;
  1121. +
  1122. + switch (attr->id) {
  1123. + case RT305X_ESW_ATTR_PORT_DISABLE:
  1124. + esw->ports[idx].disable = val->value.i;
  1125. + break;
  1126. + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
  1127. + esw->ports[idx].doubletag = val->value.i;
  1128. + break;
  1129. + case RT305X_ESW_ATTR_PORT_UNTAG:
  1130. + esw->ports[idx].untag = val->value.i;
  1131. + break;
  1132. + default:
  1133. + return -EINVAL;
  1134. + }
  1135. +
  1136. + return 0;
  1137. +}
  1138. +
  1139. +static int esw_get_port_recv_badgood(struct switch_dev *dev,
  1140. + const struct switch_attr *attr,
  1141. + struct switch_val *val)
  1142. +{
  1143. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1144. + int idx = val->port_vlan;
  1145. + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
  1146. + u32 reg;
  1147. +
  1148. + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
  1149. + return -EINVAL;
  1150. + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
  1151. + val->value.i = (reg >> shift) & 0xffff;
  1152. +
  1153. + return 0;
  1154. +}
  1155. +
  1156. +static int
  1157. +esw_get_port_tr_badgood(struct switch_dev *dev,
  1158. + const struct switch_attr *attr,
  1159. + struct switch_val *val)
  1160. +{
  1161. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1162. +
  1163. + int idx = val->port_vlan;
  1164. + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
  1165. + u32 reg;
  1166. +
  1167. + if (!soc_is_rt5350())
  1168. + return -EINVAL;
  1169. +
  1170. + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
  1171. + return -EINVAL;
  1172. +
  1173. + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
  1174. + val->value.i = (reg >> shift) & 0xffff;
  1175. +
  1176. + return 0;
  1177. +}
  1178. +
  1179. +static int esw_get_port_led(struct switch_dev *dev,
  1180. + const struct switch_attr *attr,
  1181. + struct switch_val *val)
  1182. +{
  1183. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1184. + int idx = val->port_vlan;
  1185. +
  1186. + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
  1187. + idx >= RT305X_ESW_NUM_LEDS)
  1188. + return -EINVAL;
  1189. +
  1190. + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
  1191. +
  1192. + return 0;
  1193. +}
  1194. +
  1195. +static int esw_set_port_led(struct switch_dev *dev,
  1196. + const struct switch_attr *attr,
  1197. + struct switch_val *val)
  1198. +{
  1199. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1200. + int idx = val->port_vlan;
  1201. +
  1202. + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
  1203. + return -EINVAL;
  1204. +
  1205. + esw->ports[idx].led = val->value.i;
  1206. +
  1207. + return 0;
  1208. +}
  1209. +
  1210. +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
  1211. +{
  1212. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1213. +
  1214. + if (port >= RT305X_ESW_NUM_PORTS)
  1215. + return -EINVAL;
  1216. +
  1217. + *val = esw_get_pvid(esw, port);
  1218. +
  1219. + return 0;
  1220. +}
  1221. +
  1222. +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
  1223. +{
  1224. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1225. +
  1226. + if (port >= RT305X_ESW_NUM_PORTS)
  1227. + return -EINVAL;
  1228. +
  1229. + esw->ports[port].pvid = val;
  1230. +
  1231. + return 0;
  1232. +}
  1233. +
  1234. +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  1235. +{
  1236. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1237. + u32 vmsc, poc2;
  1238. + int vlan_idx = -1;
  1239. + int i;
  1240. +
  1241. + val->len = 0;
  1242. +
  1243. + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
  1244. + return -EINVAL;
  1245. +
  1246. + /* valid vlan? */
  1247. + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
  1248. + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
  1249. + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
  1250. + vlan_idx = i;
  1251. + break;
  1252. + }
  1253. + }
  1254. +
  1255. + if (vlan_idx == -1)
  1256. + return -EINVAL;
  1257. +
  1258. + vmsc = esw_get_vmsc(esw, vlan_idx);
  1259. + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
  1260. +
  1261. + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
  1262. + struct switch_port *p;
  1263. + int port_mask = 1 << i;
  1264. +
  1265. + if (!(vmsc & port_mask))
  1266. + continue;
  1267. +
  1268. + p = &val->value.ports[val->len++];
  1269. + p->id = i;
  1270. + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
  1271. + p->flags = 0;
  1272. + else
  1273. + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
  1274. + }
  1275. +
  1276. + return 0;
  1277. +}
  1278. +
  1279. +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  1280. +{
  1281. + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
  1282. + int ports;
  1283. + int vlan_idx = -1;
  1284. + int i;
  1285. +
  1286. + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
  1287. + val->len > RT305X_ESW_NUM_PORTS)
  1288. + return -EINVAL;
  1289. +
  1290. + /* one of the already defined vlans? */
  1291. + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
  1292. + if (esw->vlans[i].vid == val->port_vlan &&
  1293. + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
  1294. + vlan_idx = i;
  1295. + break;
  1296. + }
  1297. + }
  1298. +
  1299. + /* select a free slot */
  1300. + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
  1301. + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
  1302. + vlan_idx = i;
  1303. + }
  1304. +
  1305. + /* bail if all slots are in use */
  1306. + if (vlan_idx == -1)
  1307. + return -EINVAL;
  1308. +
  1309. + ports = RT305X_ESW_PORTS_NONE;
  1310. + for (i = 0; i < val->len; i++) {
  1311. + struct switch_port *p = &val->value.ports[i];
  1312. + int port_mask = 1 << p->id;
  1313. + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
  1314. +
  1315. + if (p->id >= RT305X_ESW_NUM_PORTS)
  1316. + return -EINVAL;
  1317. +
  1318. + ports |= port_mask;
  1319. + esw->ports[p->id].untag = untagged;
  1320. + }
  1321. + esw->vlans[vlan_idx].ports = ports;
  1322. + if (ports == RT305X_ESW_PORTS_NONE)
  1323. + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
  1324. + else
  1325. + esw->vlans[vlan_idx].vid = val->port_vlan;
  1326. +
  1327. + return 0;
  1328. +}
  1329. +
  1330. +static const struct switch_attr esw_global[] = {
  1331. + {
  1332. + .type = SWITCH_TYPE_INT,
  1333. + .name = "enable_vlan",
  1334. + .description = "VLAN mode (1:enabled)",
  1335. + .max = 1,
  1336. + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
  1337. + .get = esw_get_vlan_enable,
  1338. + .set = esw_set_vlan_enable,
  1339. + },
  1340. + {
  1341. + .type = SWITCH_TYPE_INT,
  1342. + .name = "alternate_vlan_disable",
  1343. + .description = "Use en_vlan instead of doubletag to disable"
  1344. + " VLAN mode",
  1345. + .max = 1,
  1346. + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
  1347. + .get = esw_get_alt_vlan_disable,
  1348. + .set = esw_set_alt_vlan_disable,
  1349. + },
  1350. + {
  1351. + .type = SWITCH_TYPE_INT,
  1352. + .name = "bc_storm_protect",
  1353. + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
  1354. + .max = 3,
  1355. + .id = RT305X_ESW_ATTR_BC_STATUS,
  1356. + .get = rt305x_esw_get_bc_status,
  1357. + .set = rt305x_esw_set_bc_status,
  1358. + },
  1359. + {
  1360. + .type = SWITCH_TYPE_INT,
  1361. + .name = "led_frequency",
  1362. + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
  1363. + .max = 3,
  1364. + .id = RT305X_ESW_ATTR_LED_FREQ,
  1365. + .get = rt305x_esw_get_led_freq,
  1366. + .set = rt305x_esw_set_led_freq,
  1367. + }
  1368. +};
  1369. +
  1370. +static const struct switch_attr esw_port[] = {
  1371. + {
  1372. + .type = SWITCH_TYPE_INT,
  1373. + .name = "disable",
  1374. + .description = "Port state (1:disabled)",
  1375. + .max = 1,
  1376. + .id = RT305X_ESW_ATTR_PORT_DISABLE,
  1377. + .get = esw_get_port_bool,
  1378. + .set = esw_set_port_bool,
  1379. + },
  1380. + {
  1381. + .type = SWITCH_TYPE_INT,
  1382. + .name = "doubletag",
  1383. + .description = "Double tagging for incoming vlan packets "
  1384. + "(1:enabled)",
  1385. + .max = 1,
  1386. + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
  1387. + .get = esw_get_port_bool,
  1388. + .set = esw_set_port_bool,
  1389. + },
  1390. + {
  1391. + .type = SWITCH_TYPE_INT,
  1392. + .name = "untag",
  1393. + .description = "Untag (1:strip outgoing vlan tag)",
  1394. + .max = 1,
  1395. + .id = RT305X_ESW_ATTR_PORT_UNTAG,
  1396. + .get = esw_get_port_bool,
  1397. + .set = esw_set_port_bool,
  1398. + },
  1399. + {
  1400. + .type = SWITCH_TYPE_INT,
  1401. + .name = "led",
  1402. + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
  1403. + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
  1404. + " 8:100mact, 10:blink, 11:off, 12:on)",
  1405. + .max = 15,
  1406. + .id = RT305X_ESW_ATTR_PORT_LED,
  1407. + .get = esw_get_port_led,
  1408. + .set = esw_set_port_led,
  1409. + },
  1410. + {
  1411. + .type = SWITCH_TYPE_INT,
  1412. + .name = "lan",
  1413. + .description = "HW port group (0:wan, 1:lan)",
  1414. + .max = 1,
  1415. + .id = RT305X_ESW_ATTR_PORT_LAN,
  1416. + .get = esw_get_port_bool,
  1417. + },
  1418. + {
  1419. + .type = SWITCH_TYPE_INT,
  1420. + .name = "recv_bad",
  1421. + .description = "Receive bad packet counter",
  1422. + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
  1423. + .get = esw_get_port_recv_badgood,
  1424. + },
  1425. + {
  1426. + .type = SWITCH_TYPE_INT,
  1427. + .name = "recv_good",
  1428. + .description = "Receive good packet counter",
  1429. + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
  1430. + .get = esw_get_port_recv_badgood,
  1431. + },
  1432. + {
  1433. + .type = SWITCH_TYPE_INT,
  1434. + .name = "tr_bad",
  1435. +
  1436. + .description = "Transmit bad packet counter. rt5350 only",
  1437. + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
  1438. + .get = esw_get_port_tr_badgood,
  1439. + },
  1440. + {
  1441. + .type = SWITCH_TYPE_INT,
  1442. + .name = "tr_good",
  1443. +
  1444. + .description = "Transmit good packet counter. rt5350 only",
  1445. + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
  1446. + .get = esw_get_port_tr_badgood,
  1447. + },
  1448. +};
  1449. +
  1450. +static const struct switch_attr esw_vlan[] = {
  1451. +};
  1452. +
  1453. +static const struct switch_dev_ops esw_ops = {
  1454. + .attr_global = {
  1455. + .attr = esw_global,
  1456. + .n_attr = ARRAY_SIZE(esw_global),
  1457. + },
  1458. + .attr_port = {
  1459. + .attr = esw_port,
  1460. + .n_attr = ARRAY_SIZE(esw_port),
  1461. + },
  1462. + .attr_vlan = {
  1463. + .attr = esw_vlan,
  1464. + .n_attr = ARRAY_SIZE(esw_vlan),
  1465. + },
  1466. + .get_vlan_ports = esw_get_vlan_ports,
  1467. + .set_vlan_ports = esw_set_vlan_ports,
  1468. + .get_port_pvid = esw_get_port_pvid,
  1469. + .set_port_pvid = esw_set_port_pvid,
  1470. + .get_port_link = esw_get_port_link,
  1471. + .apply_config = esw_apply_config,
  1472. + .reset_switch = esw_reset_switch,
  1473. +};
  1474. +
  1475. +static struct rt305x_esw_platform_data rt3050_esw_data = {
  1476. + /* All ports are LAN ports. */
  1477. + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
  1478. + .reg_initval_fct2 = 0x00d6500c,
  1479. + /*
  1480. + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
  1481. + * turbo mii off, rgmi 3.3v off
  1482. + * port5: disabled
  1483. + * port6: enabled, gige, full-duplex, rx/tx-flow-control
  1484. + */
  1485. + .reg_initval_fpa2 = 0x3f502b28,
  1486. +};
  1487. +
  1488. +static const struct of_device_id ralink_esw_match[] = {
  1489. + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
  1490. + {},
  1491. +};
  1492. +MODULE_DEVICE_TABLE(of, ralink_esw_match);
  1493. +
  1494. +static int esw_probe(struct platform_device *pdev)
  1495. +{
  1496. + struct device_node *np = pdev->dev.of_node;
  1497. + const struct rt305x_esw_platform_data *pdata;
  1498. + const __be32 *port_map, *reg_init;
  1499. + struct rt305x_esw *esw;
  1500. + struct switch_dev *swdev;
  1501. + struct resource *res, *irq;
  1502. + int err;
  1503. +
  1504. + pdata = pdev->dev.platform_data;
  1505. + if (!pdata) {
  1506. + const struct of_device_id *match;
  1507. + match = of_match_device(ralink_esw_match, &pdev->dev);
  1508. + if (match)
  1509. + pdata = (struct rt305x_esw_platform_data *) match->data;
  1510. + }
  1511. + if (!pdata)
  1512. + return -EINVAL;
  1513. +
  1514. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1515. + if (!res) {
  1516. + dev_err(&pdev->dev, "no memory resource found\n");
  1517. + return -ENOMEM;
  1518. + }
  1519. +
  1520. + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1521. + if (!irq) {
  1522. + dev_err(&pdev->dev, "no irq resource found\n");
  1523. + return -ENOMEM;
  1524. + }
  1525. +
  1526. + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
  1527. + if (!esw) {
  1528. + dev_err(&pdev->dev, "no memory for private data\n");
  1529. + return -ENOMEM;
  1530. + }
  1531. +
  1532. + esw->dev = &pdev->dev;
  1533. + esw->irq = irq->start;
  1534. + esw->base = ioremap(res->start, resource_size(res));
  1535. + if (!esw->base) {
  1536. + dev_err(&pdev->dev, "ioremap failed\n");
  1537. + err = -ENOMEM;
  1538. + goto free_esw;
  1539. + }
  1540. +
  1541. + port_map = of_get_property(np, "ralink,portmap", NULL);
  1542. + if (port_map)
  1543. + esw->port_map = be32_to_cpu(*port_map);
  1544. +
  1545. + reg_init = of_get_property(np, "ralink,fct2", NULL);
  1546. + if (reg_init)
  1547. + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
  1548. +
  1549. + reg_init = of_get_property(np, "ralink,fpa2", NULL);
  1550. + if (reg_init)
  1551. + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
  1552. +
  1553. + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
  1554. + if (reg_init)
  1555. + esw->reg_led_polarity = be32_to_cpu(*reg_init);
  1556. +
  1557. + swdev = &esw->swdev;
  1558. + swdev->of_node = pdev->dev.of_node;
  1559. + swdev->name = "rt305x-esw";
  1560. + swdev->alias = "rt305x";
  1561. + swdev->cpu_port = RT305X_ESW_PORT6;
  1562. + swdev->ports = RT305X_ESW_NUM_PORTS;
  1563. + swdev->vlans = RT305X_ESW_NUM_VIDS;
  1564. + swdev->ops = &esw_ops;
  1565. +
  1566. + err = register_switch(swdev, NULL);
  1567. + if (err < 0) {
  1568. + dev_err(&pdev->dev, "register_switch failed\n");
  1569. + goto unmap_base;
  1570. + }
  1571. +
  1572. + platform_set_drvdata(pdev, esw);
  1573. +
  1574. + esw->pdata = pdata;
  1575. + spin_lock_init(&esw->reg_rw_lock);
  1576. +
  1577. + esw_hw_init(esw);
  1578. +
  1579. + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
  1580. + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
  1581. + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
  1582. +
  1583. + return 0;
  1584. +
  1585. +unmap_base:
  1586. + iounmap(esw->base);
  1587. +free_esw:
  1588. + kfree(esw);
  1589. + return err;
  1590. +}
  1591. +
  1592. +static int esw_remove(struct platform_device *pdev)
  1593. +{
  1594. + struct rt305x_esw *esw;
  1595. +
  1596. + esw = platform_get_drvdata(pdev);
  1597. + if (esw) {
  1598. + unregister_switch(&esw->swdev);
  1599. + platform_set_drvdata(pdev, NULL);
  1600. + iounmap(esw->base);
  1601. + kfree(esw);
  1602. + }
  1603. +
  1604. + return 0;
  1605. +}
  1606. +
  1607. +static struct platform_driver esw_driver = {
  1608. + .probe = esw_probe,
  1609. + .remove = esw_remove,
  1610. + .driver = {
  1611. + .name = "rt305x-esw",
  1612. + .owner = THIS_MODULE,
  1613. + .of_match_table = ralink_esw_match,
  1614. + },
  1615. +};
  1616. +
  1617. +int __init rtesw_init(void)
  1618. +{
  1619. + return platform_driver_register(&esw_driver);
  1620. +}
  1621. +
  1622. +void rtesw_exit(void)
  1623. +{
  1624. + platform_driver_unregister(&esw_driver);
  1625. +}
  1626. --- /dev/null
  1627. +++ b/drivers/net/ethernet/ralink/esw_rt3052.h
  1628. @@ -0,0 +1,32 @@
  1629. +/*
  1630. + * This program is free software; you can redistribute it and/or modify
  1631. + * it under the terms of the GNU General Public License as published by
  1632. + * the Free Software Foundation; version 2 of the License
  1633. + *
  1634. + * This program is distributed in the hope that it will be useful,
  1635. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1636. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1637. + * GNU General Public License for more details.
  1638. + *
  1639. + * You should have received a copy of the GNU General Public License
  1640. + * along with this program; if not, write to the Free Software
  1641. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  1642. + *
  1643. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  1644. + */
  1645. +
  1646. +#ifndef _RALINK_ESW_RT3052_H__
  1647. +#define _RALINK_ESW_RT3052_H__
  1648. +
  1649. +#ifdef CONFIG_NET_RALINK_ESW_RT3052
  1650. +
  1651. +int __init rtesw_init(void);
  1652. +void rtesw_exit(void);
  1653. +
  1654. +#else
  1655. +
  1656. +static inline int __init rtesw_init(void) { return 0; }
  1657. +static inline void rtesw_exit(void) { }
  1658. +
  1659. +#endif
  1660. +#endif
  1661. --- /dev/null
  1662. +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.c
  1663. @@ -0,0 +1,566 @@
  1664. +/*
  1665. + * This program is free software; you can redistribute it and/or modify
  1666. + * it under the terms of the GNU General Public License as published by
  1667. + * the Free Software Foundation; version 2 of the License
  1668. + *
  1669. + * This program is distributed in the hope that it will be useful,
  1670. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1671. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1672. + * GNU General Public License for more details.
  1673. + *
  1674. + * You should have received a copy of the GNU General Public License
  1675. + * along with this program; if not, write to the Free Software
  1676. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  1677. + *
  1678. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  1679. + */
  1680. +
  1681. +#include <linux/module.h>
  1682. +#include <linux/kernel.h>
  1683. +#include <linux/types.h>
  1684. +#include <linux/dma-mapping.h>
  1685. +#include <linux/init.h>
  1686. +#include <linux/skbuff.h>
  1687. +#include <linux/etherdevice.h>
  1688. +#include <linux/ethtool.h>
  1689. +#include <linux/platform_device.h>
  1690. +#include <linux/of_device.h>
  1691. +#include <linux/clk.h>
  1692. +#include <linux/of_net.h>
  1693. +#include <linux/of_mdio.h>
  1694. +#include <linux/of_irq.h>
  1695. +#include <linux/of_address.h>
  1696. +#include <linux/switch.h>
  1697. +
  1698. +#include <asm/mach-ralink/ralink_regs.h>
  1699. +
  1700. +#include "ralink_soc_eth.h"
  1701. +
  1702. +#include <linux/ioport.h>
  1703. +#include <linux/switch.h>
  1704. +#include <linux/mii.h>
  1705. +
  1706. +#include <ralink_regs.h>
  1707. +#include <asm/mach-ralink/mt7620.h>
  1708. +
  1709. +#include "ralink_soc_eth.h"
  1710. +#include "gsw_mt7620a.h"
  1711. +#include "mt7530.h"
  1712. +#include "mdio.h"
  1713. +
  1714. +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
  1715. +
  1716. +#define MT7620A_GSW_REG_PIAC 0x7004
  1717. +
  1718. +#define GSW_NUM_VLANS 16
  1719. +#define GSW_NUM_VIDS 4096
  1720. +#define GSW_NUM_PORTS 7
  1721. +#define GSW_PORT6 6
  1722. +
  1723. +#define GSW_MDIO_ACCESS BIT(31)
  1724. +#define GSW_MDIO_READ BIT(19)
  1725. +#define GSW_MDIO_WRITE BIT(18)
  1726. +#define GSW_MDIO_START BIT(16)
  1727. +#define GSW_MDIO_ADDR_SHIFT 20
  1728. +#define GSW_MDIO_REG_SHIFT 25
  1729. +
  1730. +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
  1731. +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
  1732. +#define GSW_REG_SMACCR0 0x3fE4
  1733. +#define GSW_REG_SMACCR1 0x3fE8
  1734. +#define GSW_REG_CKGCR 0x3ff0
  1735. +
  1736. +#define GSW_REG_IMR 0x7008
  1737. +#define GSW_REG_ISR 0x700c
  1738. +
  1739. +#define SYSC_REG_CFG1 0x14
  1740. +
  1741. +#define PORT_IRQ_ST_CHG 0x7f
  1742. +
  1743. +#define SYSCFG1 0x14
  1744. +
  1745. +#define ESW_PHY_POLLING 0x7000
  1746. +
  1747. +#define PMCR_IPG BIT(18)
  1748. +#define PMCR_MAC_MODE BIT(16)
  1749. +#define PMCR_FORCE BIT(15)
  1750. +#define PMCR_TX_EN BIT(14)
  1751. +#define PMCR_RX_EN BIT(13)
  1752. +#define PMCR_BACKOFF BIT(9)
  1753. +#define PMCR_BACKPRES BIT(8)
  1754. +#define PMCR_RX_FC BIT(5)
  1755. +#define PMCR_TX_FC BIT(4)
  1756. +#define PMCR_SPEED(_x) (_x << 2)
  1757. +#define PMCR_DUPLEX BIT(1)
  1758. +#define PMCR_LINK BIT(0)
  1759. +
  1760. +#define PHY_AN_EN BIT(31)
  1761. +#define PHY_PRE_EN BIT(30)
  1762. +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
  1763. +
  1764. +enum {
  1765. + /* Global attributes. */
  1766. + GSW_ATTR_ENABLE_VLAN,
  1767. + /* Port attributes. */
  1768. + GSW_ATTR_PORT_UNTAG,
  1769. +};
  1770. +
  1771. +enum {
  1772. + PORT4_EPHY = 0,
  1773. + PORT4_EXT,
  1774. +};
  1775. +
  1776. +struct mt7620_gsw {
  1777. + struct device *dev;
  1778. + void __iomem *base;
  1779. + int irq;
  1780. + int port4;
  1781. + long unsigned int autopoll;
  1782. +};
  1783. +
  1784. +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
  1785. +{
  1786. + iowrite32(val, gsw->base + reg);
  1787. +}
  1788. +
  1789. +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
  1790. +{
  1791. + return ioread32(gsw->base + reg);
  1792. +}
  1793. +
  1794. +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
  1795. +{
  1796. + unsigned long t_start = jiffies;
  1797. +
  1798. + while (1) {
  1799. + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
  1800. + return 0;
  1801. + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
  1802. + break;
  1803. + }
  1804. + }
  1805. +
  1806. + printk(KERN_ERR "mdio: MDIO timeout\n");
  1807. + return -1;
  1808. +}
  1809. +
  1810. +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
  1811. + u32 write_data)
  1812. +{
  1813. + if (mt7620_mii_busy_wait(gsw))
  1814. + return -1;
  1815. +
  1816. + write_data &= 0xffff;
  1817. +
  1818. + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
  1819. + (phy_register << GSW_MDIO_REG_SHIFT) |
  1820. + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
  1821. + MT7620A_GSW_REG_PIAC);
  1822. +
  1823. + if (mt7620_mii_busy_wait(gsw))
  1824. + return -1;
  1825. +
  1826. + return 0;
  1827. +}
  1828. +
  1829. +static u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg)
  1830. +{
  1831. + u32 d;
  1832. +
  1833. + if (mt7620_mii_busy_wait(gsw))
  1834. + return 0xffff;
  1835. +
  1836. + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
  1837. + (phy_reg << GSW_MDIO_REG_SHIFT) |
  1838. + (phy_addr << GSW_MDIO_ADDR_SHIFT),
  1839. + MT7620A_GSW_REG_PIAC);
  1840. +
  1841. + if (mt7620_mii_busy_wait(gsw))
  1842. + return 0xffff;
  1843. +
  1844. + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
  1845. +
  1846. + return d;
  1847. +}
  1848. +
  1849. +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
  1850. +{
  1851. + struct fe_priv *priv = bus->priv;
  1852. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  1853. +
  1854. + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
  1855. +}
  1856. +
  1857. +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
  1858. +{
  1859. + struct fe_priv *priv = bus->priv;
  1860. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  1861. +
  1862. + return _mt7620_mii_read(gsw, phy_addr, phy_reg);
  1863. +}
  1864. +
  1865. +static unsigned char *fe_speed_str(int speed)
  1866. +{
  1867. + switch (speed) {
  1868. + case 2:
  1869. + case SPEED_1000:
  1870. + return "1000";
  1871. + case 1:
  1872. + case SPEED_100:
  1873. + return "100";
  1874. + case 0:
  1875. + case SPEED_10:
  1876. + return "10";
  1877. + }
  1878. +
  1879. + return "? ";
  1880. +}
  1881. +
  1882. +int mt7620a_has_carrier(struct fe_priv *priv)
  1883. +{
  1884. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  1885. + int i;
  1886. +
  1887. + for (i = 0; i < GSW_PORT6; i++)
  1888. + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
  1889. + return 1;
  1890. + return 0;
  1891. +}
  1892. +
  1893. +static void mt7620a_handle_carrier(struct fe_priv *priv)
  1894. +{
  1895. + if (!priv->phy)
  1896. + return;
  1897. +
  1898. + if (mt7620a_has_carrier(priv))
  1899. + netif_carrier_on(priv->netdev);
  1900. + else
  1901. + netif_carrier_off(priv->netdev);
  1902. +}
  1903. +
  1904. +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
  1905. +{
  1906. + if (priv->link[port])
  1907. + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
  1908. + port, fe_speed_str(priv->phy->speed[port]),
  1909. + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
  1910. + else
  1911. + netdev_info(priv->netdev, "port %d link down\n", port);
  1912. + mt7620a_handle_carrier(priv);
  1913. +}
  1914. +
  1915. +static irqreturn_t gsw_interrupt(int irq, void *_priv)
  1916. +{
  1917. + struct fe_priv *priv = (struct fe_priv *) _priv;
  1918. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  1919. + u32 status;
  1920. + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
  1921. +
  1922. + status = gsw_r32(gsw, GSW_REG_ISR);
  1923. + if (status & PORT_IRQ_ST_CHG)
  1924. + for (i = 0; i <= max; i++) {
  1925. + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
  1926. + int link = status & 0x1;
  1927. +
  1928. + if (link != priv->link[i]) {
  1929. + if (link)
  1930. + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
  1931. + i, fe_speed_str((status >> 2) & 3),
  1932. + (status & 0x2) ? "Full" : "Half");
  1933. + else
  1934. + netdev_info(priv->netdev, "port %d link down\n", i);
  1935. + }
  1936. +
  1937. + priv->link[i] = link;
  1938. + }
  1939. + mt7620a_handle_carrier(priv);
  1940. +
  1941. + gsw_w32(gsw, status, GSW_REG_ISR);
  1942. +
  1943. + return IRQ_HANDLED;
  1944. +}
  1945. +
  1946. +static int mt7620_is_bga(void)
  1947. +{
  1948. + u32 bga = rt_sysc_r32(0x0c);
  1949. +
  1950. + return (bga >> 16) & 1;
  1951. +}
  1952. +
  1953. +static void gsw_auto_poll(struct mt7620_gsw *gsw)
  1954. +{
  1955. + int phy;
  1956. + int lsb = -1, msb = 0;
  1957. +
  1958. + for_each_set_bit(phy, &gsw->autopoll, 32) {
  1959. + if (lsb < 0)
  1960. + lsb = phy;
  1961. + msb = phy;
  1962. + }
  1963. +
  1964. + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
  1965. +}
  1966. +
  1967. +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
  1968. +{
  1969. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  1970. + const __be32 *_id = of_get_property(np, "reg", NULL);
  1971. + int phy_mode, size, id;
  1972. + int shift = 12;
  1973. + u32 val, mask = 0;
  1974. + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
  1975. +
  1976. + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
  1977. + if (_id)
  1978. + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
  1979. + else
  1980. + pr_err("%s: invalid port id\n", np->name);
  1981. + return;
  1982. + }
  1983. +
  1984. + id = be32_to_cpu(*_id);
  1985. +
  1986. + if (id == 4)
  1987. + shift = 14;
  1988. +
  1989. + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
  1990. + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
  1991. + pr_err("%s: invalid fixed link property\n", np->name);
  1992. + priv->phy->phy_fixed[id] = NULL;
  1993. + return;
  1994. + }
  1995. +
  1996. + phy_mode = of_get_phy_mode(np);
  1997. + switch (phy_mode) {
  1998. + case PHY_INTERFACE_MODE_RGMII:
  1999. + mask = 0;
  2000. + break;
  2001. + case PHY_INTERFACE_MODE_MII:
  2002. + mask = 1;
  2003. + break;
  2004. + case PHY_INTERFACE_MODE_RMII:
  2005. + mask = 2;
  2006. + break;
  2007. + default:
  2008. + dev_err(priv->device, "port %d - invalid phy mode\n", id);
  2009. + return;
  2010. + }
  2011. +
  2012. + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
  2013. + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
  2014. + return;
  2015. +
  2016. + val = rt_sysc_r32(SYSCFG1);
  2017. + val &= ~(3 << shift);
  2018. + val |= mask << shift;
  2019. + rt_sysc_w32(val, SYSCFG1);
  2020. +
  2021. + if (priv->phy->phy_fixed[id]) {
  2022. + const __be32 *link = priv->phy->phy_fixed[id];
  2023. + int tx_fc, rx_fc;
  2024. + u32 val = 0;
  2025. +
  2026. + priv->phy->speed[id] = be32_to_cpup(link++);
  2027. + tx_fc = be32_to_cpup(link++);
  2028. + rx_fc = be32_to_cpup(link++);
  2029. + priv->phy->duplex[id] = be32_to_cpup(link++);
  2030. + priv->link[id] = 1;
  2031. +
  2032. + switch (priv->phy->speed[id]) {
  2033. + case SPEED_10:
  2034. + val = 0;
  2035. + break;
  2036. + case SPEED_100:
  2037. + val = 1;
  2038. + break;
  2039. + case SPEED_1000:
  2040. + val = 2;
  2041. + break;
  2042. + default:
  2043. + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
  2044. + priv->phy->phy_fixed[id] = 0;
  2045. + return;
  2046. + }
  2047. + val = PMCR_SPEED(val);
  2048. + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
  2049. + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
  2050. + if (tx_fc)
  2051. + val |= PMCR_TX_FC;
  2052. + if (rx_fc)
  2053. + val |= PMCR_RX_FC;
  2054. + if (priv->phy->duplex[id])
  2055. + val |= PMCR_DUPLEX;
  2056. + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
  2057. + dev_info(priv->device, "using fixed link parameters\n");
  2058. + return;
  2059. + }
  2060. +
  2061. + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
  2062. + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
  2063. + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
  2064. +
  2065. + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
  2066. + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
  2067. + gsw->autopoll |= BIT(id);
  2068. + gsw_auto_poll(gsw);
  2069. + return;
  2070. + }
  2071. +}
  2072. +
  2073. +static void gsw_hw_init(struct mt7620_gsw *gsw)
  2074. +{
  2075. + u32 is_BGA = mt7620_is_bga();
  2076. +
  2077. + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
  2078. + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
  2079. +
  2080. + /*correct PHY setting L3.0 BGA*/
  2081. + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
  2082. +
  2083. + _mt7620_mii_write(gsw, 1, 17, 0x7444);
  2084. + if (is_BGA)
  2085. + _mt7620_mii_write(gsw, 1, 19, 0x0114);
  2086. + else
  2087. + _mt7620_mii_write(gsw, 1, 19, 0x0117);
  2088. +
  2089. + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
  2090. + _mt7620_mii_write(gsw, 1, 25, 0x6212);
  2091. + _mt7620_mii_write(gsw, 1, 26, 0x0777);
  2092. + _mt7620_mii_write(gsw, 1, 29, 0x4000);
  2093. + _mt7620_mii_write(gsw, 1, 28, 0xc077);
  2094. + _mt7620_mii_write(gsw, 1, 24, 0x0000);
  2095. +
  2096. + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
  2097. + _mt7620_mii_write(gsw, 1, 17, 0x4838);
  2098. +
  2099. + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
  2100. + if (is_BGA) {
  2101. + _mt7620_mii_write(gsw, 1, 21, 0x0515);
  2102. + _mt7620_mii_write(gsw, 1, 22, 0x0053);
  2103. + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
  2104. + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
  2105. + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
  2106. + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
  2107. + } else {
  2108. + _mt7620_mii_write(gsw, 1, 21, 0x0517);
  2109. + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
  2110. + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
  2111. + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
  2112. + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
  2113. + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
  2114. + }
  2115. + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
  2116. + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
  2117. +
  2118. + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
  2119. + _mt7620_mii_write(gsw, 0, 30, 0xa000);
  2120. + _mt7620_mii_write(gsw, 1, 30, 0xa000);
  2121. + _mt7620_mii_write(gsw, 2, 30, 0xa000);
  2122. + _mt7620_mii_write(gsw, 3, 30, 0xa000);
  2123. +
  2124. + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
  2125. + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
  2126. + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
  2127. + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
  2128. + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
  2129. + _mt7620_mii_write(gsw, 0, 16, 0x1111);
  2130. + _mt7620_mii_write(gsw, 1, 16, 0x1010);
  2131. + _mt7620_mii_write(gsw, 2, 16, 0x1515);
  2132. + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
  2133. +
  2134. + /* CPU Port6 Force Link 1G, FC ON */
  2135. + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
  2136. + /* Set Port6 CPU Port */
  2137. + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
  2138. +
  2139. + /* setup port 4 */
  2140. + if (gsw->port4 == PORT4_EPHY) {
  2141. + u32 val = rt_sysc_r32(SYSCFG1);
  2142. + val |= 3 << 14;
  2143. + rt_sysc_w32(val, SYSCFG1);
  2144. + _mt7620_mii_write(gsw, 4, 30, 0xa000);
  2145. + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
  2146. + _mt7620_mii_write(gsw, 4, 16, 0x1313);
  2147. + pr_info("gsw: setting port4 to ephy mode\n");
  2148. + }
  2149. +}
  2150. +
  2151. +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
  2152. +{
  2153. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  2154. + unsigned long flags;
  2155. +
  2156. + spin_lock_irqsave(&priv->page_lock, flags);
  2157. + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
  2158. + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
  2159. + GSW_REG_SMACCR0);
  2160. + spin_unlock_irqrestore(&priv->page_lock, flags);
  2161. +}
  2162. +
  2163. +static struct of_device_id gsw_match[] = {
  2164. + { .compatible = "ralink,mt7620a-gsw" },
  2165. + {}
  2166. +};
  2167. +
  2168. +int mt7620_gsw_config(struct fe_priv *priv)
  2169. +{
  2170. + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  2171. +
  2172. + /* is the mt7530 internal or external */
  2173. + if ((_mt7620_mii_read(gsw, 0x1f, 2) == 1) && (_mt7620_mii_read(gsw, 0x1f, 3) == 0xbeef))
  2174. + mt7530_probe(priv->device, NULL, priv->mii_bus);
  2175. + else
  2176. + mt7530_probe(priv->device, gsw->base, NULL);
  2177. +
  2178. + return 0;
  2179. +}
  2180. +
  2181. +int mt7620_gsw_probe(struct fe_priv *priv)
  2182. +{
  2183. + struct mt7620_gsw *gsw;
  2184. + struct device_node *np;
  2185. + const char *port4 = NULL;
  2186. +
  2187. + np = of_find_matching_node(NULL, gsw_match);
  2188. + if (!np) {
  2189. + dev_err(priv->device, "no gsw node found\n");
  2190. + return -EINVAL;
  2191. + }
  2192. + np = of_node_get(np);
  2193. +
  2194. + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
  2195. + if (!gsw) {
  2196. + dev_err(priv->device, "no gsw memory for private data\n");
  2197. + return -ENOMEM;
  2198. + }
  2199. +
  2200. + gsw->irq = irq_of_parse_and_map(np, 0);
  2201. + if (!gsw->irq) {
  2202. + dev_err(priv->device, "no gsw irq resource found\n");
  2203. + return -ENOMEM;
  2204. + }
  2205. +
  2206. + gsw->base = of_iomap(np, 0);
  2207. + if (!gsw->base) {
  2208. + dev_err(priv->device, "gsw ioremap failed\n");
  2209. + return -ENOMEM;
  2210. + }
  2211. +
  2212. + gsw->dev = priv->device;
  2213. + priv->soc->swpriv = gsw;
  2214. +
  2215. + of_property_read_string(np, "ralink,port4", &port4);
  2216. + if (port4 && !strcmp(port4, "ephy"))
  2217. + gsw->port4 = PORT4_EPHY;
  2218. + else if (port4 && !strcmp(port4, "gmac"))
  2219. + gsw->port4 = PORT4_EXT;
  2220. + else
  2221. + WARN_ON(port4);
  2222. +
  2223. + gsw_hw_init(gsw);
  2224. +
  2225. + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
  2226. + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
  2227. +
  2228. + return 0;
  2229. +}
  2230. --- /dev/null
  2231. +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.h
  2232. @@ -0,0 +1,30 @@
  2233. +/*
  2234. + * This program is free software; you can redistribute it and/or modify
  2235. + * it under the terms of the GNU General Public License as published by
  2236. + * the Free Software Foundation; version 2 of the License
  2237. + *
  2238. + * This program is distributed in the hope that it will be useful,
  2239. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2240. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2241. + * GNU General Public License for more details.
  2242. + *
  2243. + * You should have received a copy of the GNU General Public License
  2244. + * along with this program; if not, write to the Free Software
  2245. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2246. + *
  2247. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2248. + */
  2249. +
  2250. +#ifndef _RALINK_GSW_MT7620_H__
  2251. +#define _RALINK_GSW_MT7620_H__
  2252. +
  2253. +extern int mt7620_gsw_config(struct fe_priv *priv);
  2254. +extern int mt7620_gsw_probe(struct fe_priv *priv);
  2255. +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
  2256. +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
  2257. +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
  2258. +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
  2259. +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
  2260. +extern int mt7620a_has_carrier(struct fe_priv *priv);
  2261. +
  2262. +#endif
  2263. --- /dev/null
  2264. +++ b/drivers/net/ethernet/ralink/mdio.c
  2265. @@ -0,0 +1,244 @@
  2266. +/*
  2267. + * This program is free software; you can redistribute it and/or modify
  2268. + * it under the terms of the GNU General Public License as published by
  2269. + * the Free Software Foundation; version 2 of the License
  2270. + *
  2271. + * This program is distributed in the hope that it will be useful,
  2272. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2273. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2274. + * GNU General Public License for more details.
  2275. + *
  2276. + * You should have received a copy of the GNU General Public License
  2277. + * along with this program; if not, write to the Free Software
  2278. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2279. + *
  2280. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2281. + */
  2282. +
  2283. +#include <linux/module.h>
  2284. +#include <linux/kernel.h>
  2285. +#include <linux/types.h>
  2286. +#include <linux/dma-mapping.h>
  2287. +#include <linux/init.h>
  2288. +#include <linux/skbuff.h>
  2289. +#include <linux/etherdevice.h>
  2290. +#include <linux/ethtool.h>
  2291. +#include <linux/platform_device.h>
  2292. +#include <linux/phy.h>
  2293. +#include <linux/of_device.h>
  2294. +#include <linux/clk.h>
  2295. +#include <linux/of_net.h>
  2296. +#include <linux/of_mdio.h>
  2297. +
  2298. +#include "ralink_soc_eth.h"
  2299. +#include "mdio.h"
  2300. +
  2301. +static int fe_mdio_reset(struct mii_bus *bus)
  2302. +{
  2303. + /* TODO */
  2304. + return 0;
  2305. +}
  2306. +
  2307. +static void fe_phy_link_adjust(struct net_device *dev)
  2308. +{
  2309. + struct fe_priv *priv = netdev_priv(dev);
  2310. + unsigned long flags;
  2311. + int i;
  2312. +
  2313. + spin_lock_irqsave(&priv->phy->lock, flags);
  2314. + for (i = 0; i < 8; i++) {
  2315. + if (priv->phy->phy_node[i]) {
  2316. + struct phy_device *phydev = priv->phy->phy[i];
  2317. + int status_change = 0;
  2318. +
  2319. + if (phydev->link)
  2320. + if (priv->phy->duplex[i] != phydev->duplex ||
  2321. + priv->phy->speed[i] != phydev->speed)
  2322. + status_change = 1;
  2323. +
  2324. + if (phydev->link != priv->link[i])
  2325. + status_change = 1;
  2326. +
  2327. + switch (phydev->speed) {
  2328. + case SPEED_1000:
  2329. + case SPEED_100:
  2330. + case SPEED_10:
  2331. + priv->link[i] = phydev->link;
  2332. + priv->phy->duplex[i] = phydev->duplex;
  2333. + priv->phy->speed[i] = phydev->speed;
  2334. +
  2335. + if (status_change && priv->soc->mdio_adjust_link)
  2336. + priv->soc->mdio_adjust_link(priv, i);
  2337. + break;
  2338. + }
  2339. + }
  2340. + }
  2341. +}
  2342. +
  2343. +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
  2344. +{
  2345. + const __be32 *_port = NULL;
  2346. + struct phy_device *phydev;
  2347. + int phy_mode, port;
  2348. +
  2349. + _port = of_get_property(phy_node, "reg", NULL);
  2350. +
  2351. + if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
  2352. + pr_err("%s: invalid port id\n", phy_node->name);
  2353. + return -EINVAL;
  2354. + }
  2355. + port = be32_to_cpu(*_port);
  2356. + phy_mode = of_get_phy_mode(phy_node);
  2357. + if (phy_mode < 0) {
  2358. + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
  2359. + priv->phy->phy_node[port] = NULL;
  2360. + return -EINVAL;
  2361. + }
  2362. +
  2363. + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
  2364. + 0, phy_mode);
  2365. + if (IS_ERR(phydev)) {
  2366. + dev_err(priv->device, "could not connect to PHY\n");
  2367. + priv->phy->phy_node[port] = NULL;
  2368. + return PTR_ERR(phydev);
  2369. + }
  2370. +
  2371. + phydev->supported &= PHY_GBIT_FEATURES;
  2372. + phydev->advertising = phydev->supported;
  2373. + phydev->no_auto_carrier_off = 1;
  2374. +
  2375. + dev_info(priv->device,
  2376. + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
  2377. + port, dev_name(&phydev->dev), phydev->phy_id,
  2378. + phydev->drv->name);
  2379. +
  2380. + priv->phy->phy[port] = phydev;
  2381. + priv->link[port] = 0;
  2382. +
  2383. + return 0;
  2384. +}
  2385. +
  2386. +static int fe_phy_connect(struct fe_priv *priv)
  2387. +{
  2388. + return 0;
  2389. +}
  2390. +
  2391. +static void fe_phy_disconnect(struct fe_priv *priv)
  2392. +{
  2393. + unsigned long flags;
  2394. + int i;
  2395. +
  2396. + for (i = 0; i < 8; i++)
  2397. + if (priv->phy->phy_fixed[i]) {
  2398. + spin_lock_irqsave(&priv->phy->lock, flags);
  2399. + priv->link[i] = 0;
  2400. + if (priv->soc->mdio_adjust_link)
  2401. + priv->soc->mdio_adjust_link(priv, i);
  2402. + spin_unlock_irqrestore(&priv->phy->lock, flags);
  2403. + } else if (priv->phy->phy[i]) {
  2404. + phy_disconnect(priv->phy->phy[i]);
  2405. + }
  2406. +}
  2407. +
  2408. +static void fe_phy_start(struct fe_priv *priv)
  2409. +{
  2410. + unsigned long flags;
  2411. + int i;
  2412. +
  2413. + for (i = 0; i < 8; i++) {
  2414. + if (priv->phy->phy_fixed[i]) {
  2415. + spin_lock_irqsave(&priv->phy->lock, flags);
  2416. + priv->link[i] = 1;
  2417. + if (priv->soc->mdio_adjust_link)
  2418. + priv->soc->mdio_adjust_link(priv, i);
  2419. + spin_unlock_irqrestore(&priv->phy->lock, flags);
  2420. + } else if (priv->phy->phy[i]) {
  2421. + phy_start(priv->phy->phy[i]);
  2422. + }
  2423. + }
  2424. +}
  2425. +
  2426. +static void fe_phy_stop(struct fe_priv *priv)
  2427. +{
  2428. + unsigned long flags;
  2429. + int i;
  2430. +
  2431. + for (i = 0; i < 8; i++)
  2432. + if (priv->phy->phy_fixed[i]) {
  2433. + spin_lock_irqsave(&priv->phy->lock, flags);
  2434. + priv->link[i] = 0;
  2435. + if (priv->soc->mdio_adjust_link)
  2436. + priv->soc->mdio_adjust_link(priv, i);
  2437. + spin_unlock_irqrestore(&priv->phy->lock, flags);
  2438. + } else if (priv->phy->phy[i]) {
  2439. + phy_stop(priv->phy->phy[i]);
  2440. + }
  2441. +}
  2442. +
  2443. +static struct fe_phy phy_ralink = {
  2444. + .connect = fe_phy_connect,
  2445. + .disconnect = fe_phy_disconnect,
  2446. + .start = fe_phy_start,
  2447. + .stop = fe_phy_stop,
  2448. +};
  2449. +
  2450. +int fe_mdio_init(struct fe_priv *priv)
  2451. +{
  2452. + struct device_node *mii_np;
  2453. + int err;
  2454. +
  2455. + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
  2456. + return 0;
  2457. +
  2458. + spin_lock_init(&phy_ralink.lock);
  2459. + priv->phy = &phy_ralink;
  2460. +
  2461. + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
  2462. + if (!mii_np) {
  2463. + dev_err(priv->device, "no %s child node found", "mdio-bus");
  2464. + return -ENODEV;
  2465. + }
  2466. +
  2467. + if (!of_device_is_available(mii_np)) {
  2468. + err = 0;
  2469. + goto err_put_node;
  2470. + }
  2471. +
  2472. + priv->mii_bus = mdiobus_alloc();
  2473. + if (priv->mii_bus == NULL) {
  2474. + err = -ENOMEM;
  2475. + goto err_put_node;
  2476. + }
  2477. +
  2478. + priv->mii_bus->name = "mdio";
  2479. + priv->mii_bus->read = priv->soc->mdio_read;
  2480. + priv->mii_bus->write = priv->soc->mdio_write;
  2481. + priv->mii_bus->reset = fe_mdio_reset;
  2482. + priv->mii_bus->irq = priv->mii_irq;
  2483. + priv->mii_bus->priv = priv;
  2484. + priv->mii_bus->parent = priv->device;
  2485. +
  2486. + snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
  2487. + err = of_mdiobus_register(priv->mii_bus, mii_np);
  2488. + if (err)
  2489. + goto err_free_bus;
  2490. +
  2491. + return 0;
  2492. +
  2493. +err_free_bus:
  2494. + kfree(priv->mii_bus);
  2495. +err_put_node:
  2496. + of_node_put(mii_np);
  2497. + priv->mii_bus = NULL;
  2498. + return err;
  2499. +}
  2500. +
  2501. +void fe_mdio_cleanup(struct fe_priv *priv)
  2502. +{
  2503. + if (!priv->mii_bus)
  2504. + return;
  2505. +
  2506. + mdiobus_unregister(priv->mii_bus);
  2507. + of_node_put(priv->mii_bus->dev.of_node);
  2508. + kfree(priv->mii_bus);
  2509. +}
  2510. --- /dev/null
  2511. +++ b/drivers/net/ethernet/ralink/mdio.h
  2512. @@ -0,0 +1,29 @@
  2513. +/*
  2514. + * This program is free software; you can redistribute it and/or modify
  2515. + * it under the terms of the GNU General Public License as published by
  2516. + * the Free Software Foundation; version 2 of the License
  2517. + *
  2518. + * This program is distributed in the hope that it will be useful,
  2519. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2520. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2521. + * GNU General Public License for more details.
  2522. + *
  2523. + * You should have received a copy of the GNU General Public License
  2524. + * along with this program; if not, write to the Free Software
  2525. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2526. + *
  2527. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2528. + */
  2529. +
  2530. +#ifndef _RALINK_MDIO_H__
  2531. +#define _RALINK_MDIO_H__
  2532. +
  2533. +#ifdef CONFIG_NET_RALINK_MDIO
  2534. +extern int fe_mdio_init(struct fe_priv *priv);
  2535. +extern void fe_mdio_cleanup(struct fe_priv *priv);
  2536. +extern int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node);
  2537. +#else
  2538. +static inline int fe_mdio_init(struct fe_priv *priv) { return 0; }
  2539. +static inline void fe_mdio_cleanup(struct fe_priv *priv) {}
  2540. +#endif
  2541. +#endif
  2542. --- /dev/null
  2543. +++ b/drivers/net/ethernet/ralink/mdio_rt2880.c
  2544. @@ -0,0 +1,232 @@
  2545. +/*
  2546. + * This program is free software; you can redistribute it and/or modify
  2547. + * it under the terms of the GNU General Public License as published by
  2548. + * the Free Software Foundation; version 2 of the License
  2549. + *
  2550. + * This program is distributed in the hope that it will be useful,
  2551. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2552. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2553. + * GNU General Public License for more details.
  2554. + *
  2555. + * You should have received a copy of the GNU General Public License
  2556. + * along with this program; if not, write to the Free Software
  2557. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2558. + *
  2559. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2560. + */
  2561. +
  2562. +#include <linux/module.h>
  2563. +#include <linux/kernel.h>
  2564. +#include <linux/types.h>
  2565. +#include <linux/dma-mapping.h>
  2566. +#include <linux/init.h>
  2567. +#include <linux/skbuff.h>
  2568. +#include <linux/etherdevice.h>
  2569. +#include <linux/ethtool.h>
  2570. +#include <linux/platform_device.h>
  2571. +#include <linux/phy.h>
  2572. +#include <linux/of_device.h>
  2573. +#include <linux/clk.h>
  2574. +#include <linux/of_net.h>
  2575. +#include <linux/of_mdio.h>
  2576. +
  2577. +#include "ralink_soc_eth.h"
  2578. +#include "mdio_rt2880.h"
  2579. +#include "mdio.h"
  2580. +
  2581. +#define FE_MDIO_RETRY 1000
  2582. +
  2583. +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
  2584. +{
  2585. + switch (priv->phy->speed[0]) {
  2586. + case SPEED_1000:
  2587. + return "1000";
  2588. + case SPEED_100:
  2589. + return "100";
  2590. + case SPEED_10:
  2591. + return "10";
  2592. + }
  2593. +
  2594. + return "?";
  2595. +}
  2596. +
  2597. +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
  2598. +{
  2599. + u32 mdio_cfg;
  2600. +
  2601. + if (!priv->link[0]) {
  2602. + netif_carrier_off(priv->netdev);
  2603. + netdev_info(priv->netdev, "link down\n");
  2604. + return;
  2605. + }
  2606. +
  2607. + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
  2608. + FE_MDIO_CFG_RX_CLK_SKEW_200 |
  2609. + FE_MDIO_CFG_GP1_FRC_EN;
  2610. +
  2611. + if (priv->phy->duplex[0] == DUPLEX_FULL)
  2612. + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
  2613. +
  2614. + if (priv->phy->tx_fc[0])
  2615. + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
  2616. +
  2617. + if (priv->phy->rx_fc[0])
  2618. + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
  2619. +
  2620. + switch (priv->phy->speed[0]) {
  2621. + case SPEED_10:
  2622. + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
  2623. + break;
  2624. + case SPEED_100:
  2625. + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
  2626. + break;
  2627. + case SPEED_1000:
  2628. + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
  2629. + break;
  2630. + default:
  2631. + BUG();
  2632. + }
  2633. +
  2634. + fe_w32(mdio_cfg, FE_MDIO_CFG);
  2635. +
  2636. + netif_carrier_on(priv->netdev);
  2637. + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
  2638. + rt2880_speed_str(priv),
  2639. + (DUPLEX_FULL == priv->phy->duplex[0]) ? "Full" : "Half");
  2640. +}
  2641. +
  2642. +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
  2643. +{
  2644. + int retries;
  2645. +
  2646. + retries = FE_MDIO_RETRY;
  2647. + while (1) {
  2648. + u32 t;
  2649. +
  2650. + t = fe_r32(FE_MDIO_ACCESS);
  2651. + if ((t & (0x1 << 31)) == 0)
  2652. + return 0;
  2653. +
  2654. + if (retries-- == 0)
  2655. + break;
  2656. +
  2657. + udelay(1);
  2658. + }
  2659. +
  2660. + dev_err(priv->device, "MDIO operation timed out\n");
  2661. + return -ETIMEDOUT;
  2662. +}
  2663. +
  2664. +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
  2665. +{
  2666. + struct fe_priv *priv = bus->priv;
  2667. + int err;
  2668. + u32 t;
  2669. +
  2670. + err = rt2880_mdio_wait_ready(priv);
  2671. + if (err)
  2672. + return 0xffff;
  2673. +
  2674. + t = (phy_addr << 24) | (phy_reg << 16);
  2675. + fe_w32(t, FE_MDIO_ACCESS);
  2676. + t |= (1 << 31);
  2677. + fe_w32(t, FE_MDIO_ACCESS);
  2678. +
  2679. + err = rt2880_mdio_wait_ready(priv);
  2680. + if (err)
  2681. + return 0xffff;
  2682. +
  2683. + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
  2684. + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
  2685. +
  2686. + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
  2687. +}
  2688. +
  2689. +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
  2690. +{
  2691. + struct fe_priv *priv = bus->priv;
  2692. + int err;
  2693. + u32 t;
  2694. +
  2695. + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
  2696. + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
  2697. +
  2698. + err = rt2880_mdio_wait_ready(priv);
  2699. + if (err)
  2700. + return err;
  2701. +
  2702. + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
  2703. + fe_w32(t, FE_MDIO_ACCESS);
  2704. + t |= (1 << 31);
  2705. + fe_w32(t, FE_MDIO_ACCESS);
  2706. +
  2707. + return rt2880_mdio_wait_ready(priv);
  2708. +}
  2709. +
  2710. +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
  2711. +{
  2712. + const __be32 *id = of_get_property(np, "reg", NULL);
  2713. + const __be32 *link;
  2714. + int size;
  2715. + int phy_mode;
  2716. +
  2717. + if (!id || (be32_to_cpu(*id) != 0)) {
  2718. + pr_err("%s: invalid port id\n", np->name);
  2719. + return;
  2720. + }
  2721. +
  2722. + priv->phy->phy_fixed[0] = of_get_property(np, "ralink,fixed-link", &size);
  2723. + if (priv->phy->phy_fixed[0] && (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
  2724. + pr_err("%s: invalid fixed link property\n", np->name);
  2725. + priv->phy->phy_fixed[0] = NULL;
  2726. + return;
  2727. + }
  2728. +
  2729. + phy_mode = of_get_phy_mode(np);
  2730. + switch (phy_mode) {
  2731. + case PHY_INTERFACE_MODE_RGMII:
  2732. + break;
  2733. + case PHY_INTERFACE_MODE_MII:
  2734. + break;
  2735. + case PHY_INTERFACE_MODE_RMII:
  2736. + break;
  2737. + default:
  2738. + if (!priv->phy->phy_fixed[0])
  2739. + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[0]);
  2740. + break;
  2741. + }
  2742. +
  2743. + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
  2744. + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
  2745. + return;
  2746. +
  2747. + if (priv->phy->phy_fixed[0]) {
  2748. + link = priv->phy->phy_fixed[0];
  2749. + priv->phy->speed[0] = be32_to_cpup(link++);
  2750. + priv->phy->duplex[0] = be32_to_cpup(link++);
  2751. + priv->phy->tx_fc[0] = be32_to_cpup(link++);
  2752. + priv->phy->rx_fc[0] = be32_to_cpup(link++);
  2753. +
  2754. + priv->link[0] = 1;
  2755. + switch (priv->phy->speed[0]) {
  2756. + case SPEED_10:
  2757. + break;
  2758. + case SPEED_100:
  2759. + break;
  2760. + case SPEED_1000:
  2761. + break;
  2762. + default:
  2763. + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[0]);
  2764. + priv->phy->phy_fixed[0] = 0;
  2765. + return;
  2766. + }
  2767. + dev_info(priv->device, "using fixed link parameters\n");
  2768. + rt2880_mdio_link_adjust(priv, 0);
  2769. + return;
  2770. + }
  2771. + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0]) {
  2772. + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
  2773. + }
  2774. +
  2775. + return;
  2776. +}
  2777. --- /dev/null
  2778. +++ b/drivers/net/ethernet/ralink/mdio_rt2880.h
  2779. @@ -0,0 +1,26 @@
  2780. +/*
  2781. + * This program is free software; you can redistribute it and/or modify
  2782. + * it under the terms of the GNU General Public License as published by
  2783. + * the Free Software Foundation; version 2 of the License
  2784. + *
  2785. + * This program is distributed in the hope that it will be useful,
  2786. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2787. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2788. + * GNU General Public License for more details.
  2789. + *
  2790. + * You should have received a copy of the GNU General Public License
  2791. + * along with this program; if not, write to the Free Software
  2792. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2793. + *
  2794. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2795. + */
  2796. +
  2797. +#ifndef _RALINK_MDIO_RT2880_H__
  2798. +#define _RALINK_MDIO_RT2880_H__
  2799. +
  2800. +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
  2801. +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
  2802. +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
  2803. +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
  2804. +
  2805. +#endif
  2806. --- /dev/null
  2807. +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.c
  2808. @@ -0,0 +1,741 @@
  2809. +/*
  2810. + * This program is free software; you can redistribute it and/or modify
  2811. + * it under the terms of the GNU General Public License as published by
  2812. + * the Free Software Foundation; version 2 of the License
  2813. + *
  2814. + * This program is distributed in the hope that it will be useful,
  2815. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2816. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2817. + * GNU General Public License for more details.
  2818. + *
  2819. + * You should have received a copy of the GNU General Public License
  2820. + * along with this program; if not, write to the Free Software
  2821. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  2822. + *
  2823. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  2824. + */
  2825. +
  2826. +#include <linux/module.h>
  2827. +#include <linux/kernel.h>
  2828. +#include <linux/types.h>
  2829. +#include <linux/dma-mapping.h>
  2830. +#include <linux/init.h>
  2831. +#include <linux/skbuff.h>
  2832. +#include <linux/etherdevice.h>
  2833. +#include <linux/ethtool.h>
  2834. +#include <linux/platform_device.h>
  2835. +#include <linux/of_device.h>
  2836. +#include <linux/clk.h>
  2837. +#include <linux/of_net.h>
  2838. +#include <linux/of_mdio.h>
  2839. +#include <linux/if_vlan.h>
  2840. +#include <linux/reset.h>
  2841. +
  2842. +#include <asm/mach-ralink/ralink_regs.h>
  2843. +
  2844. +#include "ralink_soc_eth.h"
  2845. +#include "esw_rt3052.h"
  2846. +#include "mdio.h"
  2847. +
  2848. +#define TX_TIMEOUT (20 * HZ / 100)
  2849. +#define MAX_RX_LENGTH 1536
  2850. +
  2851. +static const u32 fe_reg_table_default[FE_REG_COUNT] = {
  2852. + [FE_REG_PDMA_GLO_CFG] = FE_PDMA_GLO_CFG,
  2853. + [FE_REG_PDMA_RST_CFG] = FE_PDMA_RST_CFG,
  2854. + [FE_REG_DLY_INT_CFG] = FE_DLY_INT_CFG,
  2855. + [FE_REG_TX_BASE_PTR0] = FE_TX_BASE_PTR0,
  2856. + [FE_REG_TX_MAX_CNT0] = FE_TX_MAX_CNT0,
  2857. + [FE_REG_TX_CTX_IDX0] = FE_TX_CTX_IDX0,
  2858. + [FE_REG_RX_BASE_PTR0] = FE_RX_BASE_PTR0,
  2859. + [FE_REG_RX_MAX_CNT0] = FE_RX_MAX_CNT0,
  2860. + [FE_REG_RX_CALC_IDX0] = FE_RX_CALC_IDX0,
  2861. + [FE_REG_FE_INT_ENABLE] = FE_FE_INT_ENABLE,
  2862. + [FE_REG_FE_INT_STATUS] = FE_FE_INT_STATUS,
  2863. +};
  2864. +
  2865. +static const u32 *fe_reg_table = fe_reg_table_default;
  2866. +
  2867. +static void __iomem *fe_base = 0;
  2868. +
  2869. +void fe_w32(u32 val, unsigned reg)
  2870. +{
  2871. + __raw_writel(val, fe_base + reg);
  2872. +}
  2873. +
  2874. +u32 fe_r32(unsigned reg)
  2875. +{
  2876. + return __raw_readl(fe_base + reg);
  2877. +}
  2878. +
  2879. +static inline void fe_reg_w32(u32 val, enum fe_reg reg)
  2880. +{
  2881. + fe_w32(val, fe_reg_table[reg]);
  2882. +}
  2883. +
  2884. +static inline u32 fe_reg_r32(enum fe_reg reg)
  2885. +{
  2886. + return fe_r32(fe_reg_table[reg]);
  2887. +}
  2888. +
  2889. +static inline void fe_int_disable(u32 mask)
  2890. +{
  2891. + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) & ~mask,
  2892. + FE_REG_FE_INT_ENABLE);
  2893. + /* flush write */
  2894. + fe_reg_r32(FE_REG_FE_INT_ENABLE);
  2895. +}
  2896. +
  2897. +static inline void fe_int_enable(u32 mask)
  2898. +{
  2899. + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) | mask,
  2900. + FE_REG_FE_INT_ENABLE);
  2901. + /* flush write */
  2902. + fe_reg_r32(FE_REG_FE_INT_ENABLE);
  2903. +}
  2904. +
  2905. +static inline void fe_hw_set_macaddr(struct fe_priv *priv, unsigned char *mac)
  2906. +{
  2907. + unsigned long flags;
  2908. +
  2909. + spin_lock_irqsave(&priv->page_lock, flags);
  2910. + fe_w32((mac[0] << 8) | mac[1], FE_GDMA1_MAC_ADRH);
  2911. + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
  2912. + FE_GDMA1_MAC_ADRL);
  2913. + spin_unlock_irqrestore(&priv->page_lock, flags);
  2914. +}
  2915. +
  2916. +static int fe_set_mac_address(struct net_device *dev, void *p)
  2917. +{
  2918. + int ret = eth_mac_addr(dev, p);
  2919. +
  2920. + if (!ret) {
  2921. + struct fe_priv *priv = netdev_priv(dev);
  2922. +
  2923. + if (priv->soc->set_mac)
  2924. + priv->soc->set_mac(priv, dev->dev_addr);
  2925. + else
  2926. + fe_hw_set_macaddr(priv, p);
  2927. + }
  2928. +
  2929. + return ret;
  2930. +}
  2931. +
  2932. +static struct sk_buff* fe_alloc_skb(struct fe_priv *priv)
  2933. +{
  2934. + struct sk_buff *skb;
  2935. +
  2936. + skb = netdev_alloc_skb(priv->netdev, MAX_RX_LENGTH + NET_IP_ALIGN);
  2937. + if (!skb)
  2938. + return NULL;
  2939. +
  2940. + skb_reserve(skb, NET_IP_ALIGN);
  2941. +
  2942. + return skb;
  2943. +}
  2944. +
  2945. +static int fe_alloc_rx(struct fe_priv *priv)
  2946. +{
  2947. + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
  2948. + int i;
  2949. +
  2950. + priv->rx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
  2951. + &priv->rx_phys, GFP_ATOMIC);
  2952. + if (!priv->rx_dma)
  2953. + return -ENOMEM;
  2954. +
  2955. + memset(priv->rx_dma, 0, size);
  2956. +
  2957. + for (i = 0; i < NUM_DMA_DESC; i++) {
  2958. + priv->rx_skb[i] = fe_alloc_skb(priv);
  2959. + if (!priv->rx_skb[i])
  2960. + return -ENOMEM;
  2961. + }
  2962. +
  2963. + for (i = 0; i < NUM_DMA_DESC; i++) {
  2964. + dma_addr_t dma_addr = dma_map_single(&priv->netdev->dev,
  2965. + priv->rx_skb[i]->data,
  2966. + MAX_RX_LENGTH,
  2967. + DMA_FROM_DEVICE);
  2968. + priv->rx_dma[i].rxd1 = (unsigned int) dma_addr;
  2969. +
  2970. + if (priv->soc->rx_dma)
  2971. + priv->soc->rx_dma(priv, i, MAX_RX_LENGTH);
  2972. + else
  2973. + priv->rx_dma[i].rxd2 = RX_DMA_LSO;
  2974. + }
  2975. + wmb();
  2976. +
  2977. + fe_reg_w32(priv->rx_phys, FE_REG_RX_BASE_PTR0);
  2978. + fe_reg_w32(NUM_DMA_DESC, FE_REG_RX_MAX_CNT0);
  2979. + fe_reg_w32((NUM_DMA_DESC - 1), FE_REG_RX_CALC_IDX0);
  2980. + fe_reg_w32(FE_PST_DRX_IDX0, FE_REG_PDMA_RST_CFG);
  2981. +
  2982. + return 0;
  2983. +}
  2984. +
  2985. +static int fe_alloc_tx(struct fe_priv *priv)
  2986. +{
  2987. + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
  2988. + int i;
  2989. +
  2990. + priv->tx_free_idx = 0;
  2991. +
  2992. + priv->tx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
  2993. + &priv->tx_phys, GFP_ATOMIC);
  2994. + if (!priv->tx_dma)
  2995. + return -ENOMEM;
  2996. +
  2997. + memset(priv->tx_dma, 0, size);
  2998. +
  2999. + for (i = 0; i < NUM_DMA_DESC; i++) {
  3000. + if (priv->soc->tx_dma) {
  3001. + priv->soc->tx_dma(priv, i, 0);
  3002. + continue;
  3003. + }
  3004. +
  3005. + priv->tx_dma[i].txd2 = TX_DMA_LSO | TX_DMA_DONE;
  3006. + priv->tx_dma[i].txd4 = TX_DMA_QN(3) | TX_DMA_PN(1);
  3007. + }
  3008. +
  3009. + fe_reg_w32(priv->tx_phys, FE_REG_TX_BASE_PTR0);
  3010. + fe_reg_w32(NUM_DMA_DESC, FE_REG_TX_MAX_CNT0);
  3011. + fe_reg_w32(0, FE_REG_TX_CTX_IDX0);
  3012. + fe_reg_w32(FE_PST_DTX_IDX0, FE_REG_PDMA_RST_CFG);
  3013. +
  3014. + return 0;
  3015. +}
  3016. +
  3017. +static void fe_free_dma(struct fe_priv *priv)
  3018. +{
  3019. + int i;
  3020. +
  3021. + for (i = 0; i < NUM_DMA_DESC; i++) {
  3022. + if (priv->rx_skb[i]) {
  3023. + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[i].rxd1,
  3024. + MAX_RX_LENGTH, DMA_FROM_DEVICE);
  3025. + dev_kfree_skb_any(priv->rx_skb[i]);
  3026. + priv->rx_skb[i] = NULL;
  3027. + }
  3028. +
  3029. + if (priv->tx_skb[i]) {
  3030. + dev_kfree_skb_any(priv->tx_skb[i]);
  3031. + priv->tx_skb[i] = NULL;
  3032. + }
  3033. + }
  3034. +
  3035. + if (priv->rx_dma) {
  3036. + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
  3037. + dma_free_coherent(&priv->netdev->dev, size, priv->rx_dma,
  3038. + priv->rx_phys);
  3039. + }
  3040. +
  3041. + if (priv->tx_dma) {
  3042. + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
  3043. + dma_free_coherent(&priv->netdev->dev, size, priv->tx_dma,
  3044. + priv->tx_phys);
  3045. + }
  3046. +
  3047. + netdev_reset_queue(priv->netdev);
  3048. +}
  3049. +
  3050. +static int fe_start_xmit(struct sk_buff *skb, struct net_device *dev)
  3051. +{
  3052. + struct fe_priv *priv = netdev_priv(dev);
  3053. + dma_addr_t mapped_addr;
  3054. + u32 tx_next;
  3055. + u32 tx;
  3056. +
  3057. + if (priv->soc->min_pkt_len) {
  3058. + if (skb->len < priv->soc->min_pkt_len) {
  3059. + if (skb_padto(skb, priv->soc->min_pkt_len)) {
  3060. + printk(KERN_ERR
  3061. + "fe_eth: skb_padto failed\n");
  3062. + kfree_skb(skb);
  3063. + return 0;
  3064. + }
  3065. + skb_put(skb, priv->soc->min_pkt_len - skb->len);
  3066. + }
  3067. + }
  3068. +
  3069. + dev->trans_start = jiffies;
  3070. + mapped_addr = dma_map_single(&priv->netdev->dev, skb->data,
  3071. + skb->len, DMA_TO_DEVICE);
  3072. +
  3073. + spin_lock(&priv->page_lock);
  3074. +
  3075. + tx = fe_reg_r32(FE_REG_TX_CTX_IDX0);
  3076. + tx_next = (tx + 1) % NUM_DMA_DESC;
  3077. +
  3078. + if ((priv->tx_skb[tx]) || (priv->tx_skb[tx_next]) ||
  3079. + !(priv->tx_dma[tx].txd2 & TX_DMA_DONE) ||
  3080. + !(priv->tx_dma[tx_next].txd2 & TX_DMA_DONE))
  3081. + {
  3082. + spin_unlock(&priv->page_lock);
  3083. + dev->stats.tx_dropped++;
  3084. + kfree_skb(skb);
  3085. +
  3086. + return NETDEV_TX_OK;
  3087. + }
  3088. +
  3089. + priv->tx_skb[tx] = skb;
  3090. + priv->tx_dma[tx].txd1 = (unsigned int) mapped_addr;
  3091. + wmb();
  3092. + if (priv->soc->tx_dma)
  3093. + priv->soc->tx_dma(priv, tx, skb->len);
  3094. + else
  3095. + priv->tx_dma[tx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(skb->len);
  3096. +
  3097. + if (skb->ip_summed == CHECKSUM_PARTIAL)
  3098. + priv->tx_dma[tx].txd4 |= TX_DMA_CHKSUM;
  3099. + else
  3100. + priv->tx_dma[tx].txd4 &= ~TX_DMA_CHKSUM;
  3101. +
  3102. + priv->tx_dma[tx].txd4 &= ~0x80;
  3103. +
  3104. + dev->stats.tx_packets++;
  3105. + dev->stats.tx_bytes += skb->len;
  3106. +
  3107. + fe_reg_w32(tx_next, FE_REG_TX_CTX_IDX0);
  3108. + netdev_sent_queue(dev, skb->len);
  3109. +
  3110. + spin_unlock(&priv->page_lock);
  3111. +
  3112. + return NETDEV_TX_OK;
  3113. +}
  3114. +
  3115. +static int fe_poll_rx(struct napi_struct *napi, int budget)
  3116. +{
  3117. + struct fe_priv *priv = container_of(napi, struct fe_priv, rx_napi);
  3118. + int idx = fe_reg_r32(FE_REG_RX_CALC_IDX0);
  3119. + int complete = 0;
  3120. + int rx = 0;
  3121. +
  3122. + while ((rx < budget) && !complete) {
  3123. + idx = (idx + 1) % NUM_DMA_DESC;
  3124. +
  3125. + if (priv->rx_dma[idx].rxd2 & RX_DMA_DONE) {
  3126. + struct sk_buff *new_skb = fe_alloc_skb(priv);
  3127. +
  3128. + if (new_skb) {
  3129. + int pktlen = RX_DMA_PLEN0(priv->rx_dma[idx].rxd2);
  3130. + dma_addr_t dma_addr;
  3131. +
  3132. + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[idx].rxd1,
  3133. + MAX_RX_LENGTH, DMA_FROM_DEVICE);
  3134. +
  3135. + skb_put(priv->rx_skb[idx], pktlen);
  3136. + priv->rx_skb[idx]->dev = priv->netdev;
  3137. + priv->rx_skb[idx]->protocol = eth_type_trans(priv->rx_skb[idx], priv->netdev);
  3138. + if (priv->rx_dma[idx].rxd4 & priv->soc->checksum_bit)
  3139. + priv->rx_skb[idx]->ip_summed = CHECKSUM_UNNECESSARY;
  3140. + else
  3141. + priv->rx_skb[idx]->ip_summed = CHECKSUM_NONE;
  3142. + priv->netdev->stats.rx_packets++;
  3143. + priv->netdev->stats.rx_bytes += pktlen;
  3144. + netif_receive_skb(priv->rx_skb[idx]);
  3145. +
  3146. + priv->rx_skb[idx] = new_skb;
  3147. +
  3148. + dma_addr = dma_map_single(&priv->netdev->dev,
  3149. + new_skb->data,
  3150. + MAX_RX_LENGTH,
  3151. + DMA_FROM_DEVICE);
  3152. + priv->rx_dma[idx].rxd1 = (unsigned int) dma_addr;
  3153. + wmb();
  3154. + } else {
  3155. + priv->netdev->stats.rx_dropped++;
  3156. + }
  3157. +
  3158. + if (priv->soc->rx_dma)
  3159. + priv->soc->rx_dma(priv, idx, MAX_RX_LENGTH);
  3160. + else
  3161. + priv->rx_dma[idx].rxd2 = RX_DMA_LSO;
  3162. + fe_reg_w32(idx, FE_REG_RX_CALC_IDX0);
  3163. +
  3164. + rx++;
  3165. + } else {
  3166. + complete = 1;
  3167. + }
  3168. + }
  3169. +
  3170. + if (complete) {
  3171. + napi_complete(&priv->rx_napi);
  3172. + fe_int_enable(priv->soc->rx_dly_int);
  3173. + }
  3174. +
  3175. + return rx;
  3176. +}
  3177. +
  3178. +static void fe_tx_housekeeping(unsigned long ptr)
  3179. +{
  3180. + struct net_device *dev = (struct net_device*)ptr;
  3181. + struct fe_priv *priv = netdev_priv(dev);
  3182. + unsigned int bytes_compl = 0;
  3183. + unsigned int pkts_compl = 0;
  3184. +
  3185. + spin_lock(&priv->page_lock);
  3186. + while (1) {
  3187. + struct fe_tx_dma *txd;
  3188. +
  3189. + txd = &priv->tx_dma[priv->tx_free_idx];
  3190. +
  3191. + if (!(txd->txd2 & TX_DMA_DONE) || !(priv->tx_skb[priv->tx_free_idx]))
  3192. + break;
  3193. +
  3194. + bytes_compl += priv->tx_skb[priv->tx_free_idx]->len;
  3195. + pkts_compl++;
  3196. +
  3197. + dev_kfree_skb_irq(priv->tx_skb[priv->tx_free_idx]);
  3198. + priv->tx_skb[priv->tx_free_idx] = NULL;
  3199. + priv->tx_free_idx++;
  3200. + if (priv->tx_free_idx >= NUM_DMA_DESC)
  3201. + priv->tx_free_idx = 0;
  3202. + }
  3203. +
  3204. + netdev_completed_queue(priv->netdev, pkts_compl, bytes_compl);
  3205. + spin_unlock(&priv->page_lock);
  3206. +
  3207. + fe_int_enable(priv->soc->tx_dly_int);
  3208. +}
  3209. +
  3210. +static void fe_tx_timeout(struct net_device *dev)
  3211. +{
  3212. + struct fe_priv *priv = netdev_priv(dev);
  3213. +
  3214. + tasklet_schedule(&priv->tx_tasklet);
  3215. + priv->netdev->stats.tx_errors++;
  3216. + netdev_err(dev, "transmit timed out, waking up the queue\n");
  3217. + netif_wake_queue(dev);
  3218. +}
  3219. +
  3220. +static irqreturn_t fe_handle_irq(int irq, void *dev)
  3221. +{
  3222. + struct fe_priv *priv = netdev_priv(dev);
  3223. + unsigned int status;
  3224. + unsigned int mask;
  3225. +
  3226. + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
  3227. + mask = fe_reg_r32(FE_REG_FE_INT_ENABLE);
  3228. +
  3229. + if (!(status & mask))
  3230. + return IRQ_NONE;
  3231. +
  3232. + if (status & priv->soc->rx_dly_int) {
  3233. + fe_int_disable(priv->soc->rx_dly_int);
  3234. + napi_schedule(&priv->rx_napi);
  3235. + }
  3236. +
  3237. + if (status & priv->soc->tx_dly_int) {
  3238. + fe_int_disable(priv->soc->tx_dly_int);
  3239. + tasklet_schedule(&priv->tx_tasklet);
  3240. + }
  3241. +
  3242. + fe_reg_w32(status, FE_REG_FE_INT_STATUS);
  3243. +
  3244. + return IRQ_HANDLED;
  3245. +}
  3246. +
  3247. +static int fe_hw_init(struct net_device *dev)
  3248. +{
  3249. + struct fe_priv *priv = netdev_priv(dev);
  3250. + int err;
  3251. +
  3252. + err = devm_request_irq(priv->device, dev->irq, fe_handle_irq, 0,
  3253. + dev_name(priv->device), dev);
  3254. + if (err)
  3255. + return err;
  3256. +
  3257. + err = fe_alloc_rx(priv);
  3258. + if (!err)
  3259. + err = fe_alloc_tx(priv);
  3260. + if (err)
  3261. + return err;
  3262. +
  3263. + if (priv->soc->set_mac)
  3264. + priv->soc->set_mac(priv, dev->dev_addr);
  3265. + else
  3266. + fe_hw_set_macaddr(priv, dev->dev_addr);
  3267. +
  3268. + fe_reg_w32(FE_DELAY_INIT, FE_REG_DLY_INT_CFG);
  3269. +
  3270. + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
  3271. +
  3272. + tasklet_init(&priv->tx_tasklet, fe_tx_housekeeping, (unsigned long)dev);
  3273. +
  3274. + if (priv->soc->fwd_config) {
  3275. + priv->soc->fwd_config(priv);
  3276. + } else {
  3277. + unsigned long sysclk = priv->sysclk;
  3278. +
  3279. + if (!sysclk) {
  3280. + netdev_err(dev, "unable to get clock\n");
  3281. + return -EINVAL;
  3282. + }
  3283. +
  3284. + sysclk /= FE_US_CYC_CNT_DIVISOR;
  3285. + sysclk <<= FE_US_CYC_CNT_SHIFT;
  3286. +
  3287. + fe_w32((fe_r32(FE_FE_GLO_CFG) &
  3288. + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
  3289. + FE_FE_GLO_CFG);
  3290. +
  3291. + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) & ~0xffff, FE_GDMA1_FWD_CFG);
  3292. + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) | (FE_GDM1_ICS_EN | FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
  3293. + FE_GDMA1_FWD_CFG);
  3294. + fe_w32(fe_r32(FE_CDMA_CSG_CFG) | (FE_ICS_GEN_EN | FE_TCS_GEN_EN | FE_UCS_GEN_EN),
  3295. + FE_CDMA_CSG_CFG);
  3296. + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
  3297. + }
  3298. +
  3299. + fe_w32(1, FE_FE_RST_GL);
  3300. + fe_w32(0, FE_FE_RST_GL);
  3301. +
  3302. + return 0;
  3303. +}
  3304. +
  3305. +static int fe_open(struct net_device *dev)
  3306. +{
  3307. + struct fe_priv *priv = netdev_priv(dev);
  3308. + unsigned long flags;
  3309. + u32 val;
  3310. +
  3311. + spin_lock_irqsave(&priv->page_lock, flags);
  3312. + napi_enable(&priv->rx_napi);
  3313. +
  3314. + val = FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
  3315. + val |= priv->soc->pdma_glo_cfg;
  3316. + fe_reg_w32(val, FE_REG_PDMA_GLO_CFG);
  3317. +
  3318. + spin_unlock_irqrestore(&priv->page_lock, flags);
  3319. +
  3320. + if (priv->phy)
  3321. + priv->phy->start(priv);
  3322. +
  3323. + if (priv->soc->has_carrier && priv->soc->has_carrier(priv))
  3324. + netif_carrier_on(dev);
  3325. +
  3326. + netif_start_queue(dev);
  3327. + fe_int_enable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
  3328. +
  3329. + return 0;
  3330. +}
  3331. +
  3332. +static int fe_stop(struct net_device *dev)
  3333. +{
  3334. + struct fe_priv *priv = netdev_priv(dev);
  3335. + unsigned long flags;
  3336. +
  3337. + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
  3338. +
  3339. + netif_stop_queue(dev);
  3340. +
  3341. + if (priv->phy)
  3342. + priv->phy->stop(priv);
  3343. +
  3344. + spin_lock_irqsave(&priv->page_lock, flags);
  3345. + napi_disable(&priv->rx_napi);
  3346. +
  3347. + fe_reg_w32(fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
  3348. + ~(FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN),
  3349. + FE_REG_PDMA_GLO_CFG);
  3350. + spin_unlock_irqrestore(&priv->page_lock, flags);
  3351. +
  3352. + return 0;
  3353. +}
  3354. +
  3355. +static int __init fe_init(struct net_device *dev)
  3356. +{
  3357. + struct fe_priv *priv = netdev_priv(dev);
  3358. + struct device_node *port;
  3359. + int err;
  3360. +
  3361. + BUG_ON(!priv->soc->reset_fe);
  3362. + priv->soc->reset_fe();
  3363. +
  3364. + if (priv->soc->switch_init)
  3365. + priv->soc->switch_init(priv);
  3366. +
  3367. + net_srandom(jiffies);
  3368. + memcpy(dev->dev_addr, priv->soc->mac, ETH_ALEN);
  3369. + of_get_mac_address_mtd(priv->device->of_node, dev->dev_addr);
  3370. +
  3371. + err = fe_mdio_init(priv);
  3372. + if (err)
  3373. + return err;
  3374. +
  3375. + if (priv->phy) {
  3376. + err = priv->phy->connect(priv);
  3377. + if (err)
  3378. + goto err_mdio_cleanup;
  3379. + }
  3380. +
  3381. + if (priv->soc->port_init)
  3382. + for_each_child_of_node(priv->device->of_node, port)
  3383. + if (of_device_is_compatible(port, "ralink,eth-port") && of_device_is_available(port))
  3384. + priv->soc->port_init(priv, port);
  3385. +
  3386. + err = fe_hw_init(dev);
  3387. + if (err)
  3388. + goto err_phy_disconnect;
  3389. +
  3390. + if (priv->soc->switch_config)
  3391. + priv->soc->switch_config(priv);
  3392. +
  3393. + return 0;
  3394. +
  3395. +err_phy_disconnect:
  3396. + if (priv->phy)
  3397. + priv->phy->disconnect(priv);
  3398. +err_mdio_cleanup:
  3399. + fe_mdio_cleanup(priv);
  3400. +
  3401. + return err;
  3402. +}
  3403. +
  3404. +static void fe_uninit(struct net_device *dev)
  3405. +{
  3406. + struct fe_priv *priv = netdev_priv(dev);
  3407. +
  3408. + tasklet_kill(&priv->tx_tasklet);
  3409. +
  3410. + if (priv->phy)
  3411. + priv->phy->disconnect(priv);
  3412. + fe_mdio_cleanup(priv);
  3413. +
  3414. + fe_reg_w32(0, FE_REG_FE_INT_ENABLE);
  3415. + free_irq(dev->irq, dev);
  3416. +
  3417. + fe_free_dma(priv);
  3418. +}
  3419. +
  3420. +static const struct net_device_ops fe_netdev_ops = {
  3421. + .ndo_init = fe_init,
  3422. + .ndo_uninit = fe_uninit,
  3423. + .ndo_open = fe_open,
  3424. + .ndo_stop = fe_stop,
  3425. + .ndo_start_xmit = fe_start_xmit,
  3426. + .ndo_tx_timeout = fe_tx_timeout,
  3427. + .ndo_set_mac_address = fe_set_mac_address,
  3428. + .ndo_change_mtu = eth_change_mtu,
  3429. + .ndo_validate_addr = eth_validate_addr,
  3430. +};
  3431. +
  3432. +static int fe_probe(struct platform_device *pdev)
  3433. +{
  3434. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3435. + const struct of_device_id *match;
  3436. + struct fe_soc_data *soc = NULL;
  3437. + struct net_device *netdev;
  3438. + struct fe_priv *priv;
  3439. + struct clk *sysclk;
  3440. + int err;
  3441. +
  3442. + device_reset(&pdev->dev);
  3443. +
  3444. + match = of_match_device(of_fe_match, &pdev->dev);
  3445. + soc = (struct fe_soc_data *) match->data;
  3446. + if (soc->reg_table)
  3447. + fe_reg_table = soc->reg_table;
  3448. +
  3449. + fe_base = devm_request_and_ioremap(&pdev->dev, res);
  3450. + if (!fe_base)
  3451. + return -ENOMEM;
  3452. +
  3453. + netdev = alloc_etherdev(sizeof(struct fe_priv));
  3454. + if (!netdev) {
  3455. + dev_err(&pdev->dev, "alloc_etherdev failed\n");
  3456. + return -ENOMEM;
  3457. + }
  3458. +
  3459. + strcpy(netdev->name, "eth%d");
  3460. + netdev->netdev_ops = &fe_netdev_ops;
  3461. + netdev->base_addr = (unsigned long) fe_base;
  3462. + netdev->watchdog_timeo = TX_TIMEOUT;
  3463. + netdev->features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  3464. +
  3465. + netdev->irq = platform_get_irq(pdev, 0);
  3466. + if (netdev->irq < 0) {
  3467. + dev_err(&pdev->dev, "no IRQ resource found\n");
  3468. + kfree(netdev);
  3469. + return -ENXIO;
  3470. + }
  3471. +
  3472. + priv = netdev_priv(netdev);
  3473. + memset(priv, 0, sizeof(struct fe_priv));
  3474. + spin_lock_init(&priv->page_lock);
  3475. +
  3476. + sysclk = devm_clk_get(&pdev->dev, NULL);
  3477. + if (!IS_ERR(sysclk))
  3478. + priv->sysclk = clk_get_rate(sysclk);
  3479. +
  3480. + priv->netdev = netdev;
  3481. + priv->device = &pdev->dev;
  3482. + priv->soc = soc;
  3483. +
  3484. + err = register_netdev(netdev);
  3485. + if (err) {
  3486. + dev_err(&pdev->dev, "error bringing up device\n");
  3487. + kfree(netdev);
  3488. + return err;
  3489. + }
  3490. + netif_napi_add(netdev, &priv->rx_napi, fe_poll_rx, 32);
  3491. +
  3492. + platform_set_drvdata(pdev, netdev);
  3493. +
  3494. + netdev_info(netdev, "done loading\n");
  3495. +
  3496. + return 0;
  3497. +}
  3498. +
  3499. +static int fe_remove(struct platform_device *pdev)
  3500. +{
  3501. + struct net_device *dev = platform_get_drvdata(pdev);
  3502. + struct fe_priv *priv = netdev_priv(dev);
  3503. +
  3504. + netif_stop_queue(dev);
  3505. + netif_napi_del(&priv->rx_napi);
  3506. +
  3507. + unregister_netdev(dev);
  3508. + free_netdev(dev);
  3509. +
  3510. + return 0;
  3511. +}
  3512. +
  3513. +static struct platform_driver fe_driver = {
  3514. + .probe = fe_probe,
  3515. + .remove = fe_remove,
  3516. + .driver = {
  3517. + .name = "ralink_soc_eth",
  3518. + .owner = THIS_MODULE,
  3519. + .of_match_table = of_fe_match,
  3520. + },
  3521. +};
  3522. +
  3523. +static int __init init_rtfe(void)
  3524. +{
  3525. + int ret;
  3526. +
  3527. + ret = rtesw_init();
  3528. + if (ret)
  3529. + return ret;
  3530. +
  3531. + ret = platform_driver_register(&fe_driver);
  3532. + if (ret)
  3533. + rtesw_exit();
  3534. +
  3535. + return ret;
  3536. +}
  3537. +
  3538. +static void __exit exit_rtfe(void)
  3539. +{
  3540. + platform_driver_unregister(&fe_driver);
  3541. + rtesw_exit();
  3542. +}
  3543. +
  3544. +module_init(init_rtfe);
  3545. +module_exit(exit_rtfe);
  3546. +
  3547. +MODULE_LICENSE("GPL");
  3548. +MODULE_AUTHOR("John Crispin <[email protected]>");
  3549. +MODULE_DESCRIPTION("Ethernet driver for Ralink SoC");
  3550. --- /dev/null
  3551. +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.h
  3552. @@ -0,0 +1,375 @@
  3553. +/*
  3554. + * This program is free software; you can redistribute it and/or modify
  3555. + * it under the terms of the GNU General Public License as published by
  3556. + * the Free Software Foundation; version 2 of the License
  3557. + *
  3558. + * This program is distributed in the hope that it will be useful,
  3559. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3560. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3561. + * GNU General Public License for more details.
  3562. + *
  3563. + * You should have received a copy of the GNU General Public License
  3564. + * along with this program; if not, write to the Free Software
  3565. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  3566. + *
  3567. + * based on Ralink SDK3.3
  3568. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  3569. + */
  3570. +
  3571. +#ifndef FE_ETH_H
  3572. +#define FE_ETH_H
  3573. +
  3574. +#include <linux/mii.h>
  3575. +#include <linux/interrupt.h>
  3576. +#include <linux/netdevice.h>
  3577. +#include <linux/dma-mapping.h>
  3578. +#include <linux/phy.h>
  3579. +
  3580. +
  3581. +enum fe_reg {
  3582. + FE_REG_PDMA_GLO_CFG = 0,
  3583. + FE_REG_PDMA_RST_CFG,
  3584. + FE_REG_DLY_INT_CFG,
  3585. + FE_REG_TX_BASE_PTR0,
  3586. + FE_REG_TX_MAX_CNT0,
  3587. + FE_REG_TX_CTX_IDX0,
  3588. + FE_REG_RX_BASE_PTR0,
  3589. + FE_REG_RX_MAX_CNT0,
  3590. + FE_REG_RX_CALC_IDX0,
  3591. + FE_REG_FE_INT_ENABLE,
  3592. + FE_REG_FE_INT_STATUS,
  3593. + FE_REG_FE_DMA_VID_BASE,
  3594. + FE_REG_COUNT
  3595. +};
  3596. +
  3597. +#define NUM_DMA_DESC 0x100
  3598. +
  3599. +#define FE_DELAY_EN_INT 0x80
  3600. +#define FE_DELAY_MAX_INT 0x04
  3601. +#define FE_DELAY_MAX_TOUT 0x04
  3602. +#define FE_DELAY_CHAN (((FE_DELAY_EN_INT | FE_DELAY_MAX_INT) << 8) | FE_DELAY_MAX_TOUT)
  3603. +#define FE_DELAY_INIT ((FE_DELAY_CHAN << 16) | FE_DELAY_CHAN)
  3604. +#define FE_PSE_FQFC_CFG_INIT 0x80504000
  3605. +
  3606. +/* interrupt bits */
  3607. +#define FE_CNT_PPE_AF BIT(31)
  3608. +#define FE_CNT_GDM_AF BIT(29)
  3609. +#define FE_PSE_P2_FC BIT(26)
  3610. +#define FE_PSE_BUF_DROP BIT(24)
  3611. +#define FE_GDM_OTHER_DROP BIT(23)
  3612. +#define FE_PSE_P1_FC BIT(22)
  3613. +#define FE_PSE_P0_FC BIT(21)
  3614. +#define FE_PSE_FQ_EMPTY BIT(20)
  3615. +#define FE_GE1_STA_CHG BIT(18)
  3616. +#define FE_TX_COHERENT BIT(17)
  3617. +#define FE_RX_COHERENT BIT(16)
  3618. +#define FE_TX_DONE_INT3 BIT(11)
  3619. +#define FE_TX_DONE_INT2 BIT(10)
  3620. +#define FE_TX_DONE_INT1 BIT(9)
  3621. +#define FE_TX_DONE_INT0 BIT(8)
  3622. +#define FE_RX_DONE_INT0 BIT(2)
  3623. +#define FE_TX_DLY_INT BIT(1)
  3624. +#define FE_RX_DLY_INT BIT(0)
  3625. +
  3626. +#define RT5350_RX_DLY_INT BIT(30)
  3627. +#define RT5350_TX_DLY_INT BIT(28)
  3628. +
  3629. +/* registers */
  3630. +#define FE_FE_OFFSET 0x0000
  3631. +#define FE_GDMA_OFFSET 0x0020
  3632. +#define FE_PSE_OFFSET 0x0040
  3633. +#define FE_GDMA2_OFFSET 0x0060
  3634. +#define FE_CDMA_OFFSET 0x0080
  3635. +#define FE_DMA_VID0 0x00a8
  3636. +#define FE_PDMA_OFFSET 0x0100
  3637. +#define FE_PPE_OFFSET 0x0200
  3638. +#define FE_CMTABLE_OFFSET 0x0400
  3639. +#define FE_POLICYTABLE_OFFSET 0x1000
  3640. +
  3641. +#define RT5350_PDMA_OFFSET 0x0800
  3642. +#define RT5350_SDM_OFFSET 0x0c00
  3643. +
  3644. +#define FE_MDIO_ACCESS (FE_FE_OFFSET + 0x00)
  3645. +#define FE_MDIO_CFG (FE_FE_OFFSET + 0x04)
  3646. +#define FE_FE_GLO_CFG (FE_FE_OFFSET + 0x08)
  3647. +#define FE_FE_RST_GL (FE_FE_OFFSET + 0x0C)
  3648. +#define FE_FE_INT_STATUS (FE_FE_OFFSET + 0x10)
  3649. +#define FE_FE_INT_ENABLE (FE_FE_OFFSET + 0x14)
  3650. +#define FE_MDIO_CFG2 (FE_FE_OFFSET + 0x18)
  3651. +#define FE_FOC_TS_T (FE_FE_OFFSET + 0x1C)
  3652. +
  3653. +#define FE_GDMA1_FWD_CFG (FE_GDMA_OFFSET + 0x00)
  3654. +#define FE_GDMA1_SCH_CFG (FE_GDMA_OFFSET + 0x04)
  3655. +#define FE_GDMA1_SHPR_CFG (FE_GDMA_OFFSET + 0x08)
  3656. +#define FE_GDMA1_MAC_ADRL (FE_GDMA_OFFSET + 0x0C)
  3657. +#define FE_GDMA1_MAC_ADRH (FE_GDMA_OFFSET + 0x10)
  3658. +
  3659. +#define FE_GDMA2_FWD_CFG (FE_GDMA2_OFFSET + 0x00)
  3660. +#define FE_GDMA2_SCH_CFG (FE_GDMA2_OFFSET + 0x04)
  3661. +#define FE_GDMA2_SHPR_CFG (FE_GDMA2_OFFSET + 0x08)
  3662. +#define FE_GDMA2_MAC_ADRL (FE_GDMA2_OFFSET + 0x0C)
  3663. +#define FE_GDMA2_MAC_ADRH (FE_GDMA2_OFFSET + 0x10)
  3664. +
  3665. +#define FE_PSE_FQ_CFG (FE_PSE_OFFSET + 0x00)
  3666. +#define FE_CDMA_FC_CFG (FE_PSE_OFFSET + 0x04)
  3667. +#define FE_GDMA1_FC_CFG (FE_PSE_OFFSET + 0x08)
  3668. +#define FE_GDMA2_FC_CFG (FE_PSE_OFFSET + 0x0C)
  3669. +
  3670. +#define FE_CDMA_CSG_CFG (FE_CDMA_OFFSET + 0x00)
  3671. +#define FE_CDMA_SCH_CFG (FE_CDMA_OFFSET + 0x04)
  3672. +
  3673. +#define MT7620A_GDMA_OFFSET 0x0600
  3674. +#define MT7620A_GDMA1_FWD_CFG (MT7620A_GDMA_OFFSET + 0x00)
  3675. +#define MT7620A_FE_GDMA1_SCH_CFG (MT7620A_GDMA_OFFSET + 0x04)
  3676. +#define MT7620A_FE_GDMA1_SHPR_CFG (MT7620A_GDMA_OFFSET + 0x08)
  3677. +#define MT7620A_FE_GDMA1_MAC_ADRL (MT7620A_GDMA_OFFSET + 0x0C)
  3678. +#define MT7620A_FE_GDMA1_MAC_ADRH (MT7620A_GDMA_OFFSET + 0x10)
  3679. +
  3680. +#define RT5350_TX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x00)
  3681. +#define RT5350_TX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x04)
  3682. +#define RT5350_TX_CTX_IDX0 (RT5350_PDMA_OFFSET + 0x08)
  3683. +#define RT5350_TX_DTX_IDX0 (RT5350_PDMA_OFFSET + 0x0C)
  3684. +#define RT5350_TX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x10)
  3685. +#define RT5350_TX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x14)
  3686. +#define RT5350_TX_CTX_IDX1 (RT5350_PDMA_OFFSET + 0x18)
  3687. +#define RT5350_TX_DTX_IDX1 (RT5350_PDMA_OFFSET + 0x1C)
  3688. +#define RT5350_TX_BASE_PTR2 (RT5350_PDMA_OFFSET + 0x20)
  3689. +#define RT5350_TX_MAX_CNT2 (RT5350_PDMA_OFFSET + 0x24)
  3690. +#define RT5350_TX_CTX_IDX2 (RT5350_PDMA_OFFSET + 0x28)
  3691. +#define RT5350_TX_DTX_IDX2 (RT5350_PDMA_OFFSET + 0x2C)
  3692. +#define RT5350_TX_BASE_PTR3 (RT5350_PDMA_OFFSET + 0x30)
  3693. +#define RT5350_TX_MAX_CNT3 (RT5350_PDMA_OFFSET + 0x34)
  3694. +#define RT5350_TX_CTX_IDX3 (RT5350_PDMA_OFFSET + 0x38)
  3695. +#define RT5350_TX_DTX_IDX3 (RT5350_PDMA_OFFSET + 0x3C)
  3696. +#define RT5350_RX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x100)
  3697. +#define RT5350_RX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x104)
  3698. +#define RT5350_RX_CALC_IDX0 (RT5350_PDMA_OFFSET + 0x108)
  3699. +#define RT5350_RX_DRX_IDX0 (RT5350_PDMA_OFFSET + 0x10C)
  3700. +#define RT5350_RX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x110)
  3701. +#define RT5350_RX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x114)
  3702. +#define RT5350_RX_CALC_IDX1 (RT5350_PDMA_OFFSET + 0x118)
  3703. +#define RT5350_RX_DRX_IDX1 (RT5350_PDMA_OFFSET + 0x11C)
  3704. +#define RT5350_PDMA_GLO_CFG (RT5350_PDMA_OFFSET + 0x204)
  3705. +#define RT5350_PDMA_RST_CFG (RT5350_PDMA_OFFSET + 0x208)
  3706. +#define RT5350_DLY_INT_CFG (RT5350_PDMA_OFFSET + 0x20c)
  3707. +#define RT5350_FE_INT_STATUS (RT5350_PDMA_OFFSET + 0x220)
  3708. +#define RT5350_FE_INT_ENABLE (RT5350_PDMA_OFFSET + 0x228)
  3709. +#define RT5350_PDMA_SCH_CFG (RT5350_PDMA_OFFSET + 0x280)
  3710. +
  3711. +#define FE_PDMA_GLO_CFG (FE_PDMA_OFFSET + 0x00)
  3712. +#define FE_PDMA_RST_CFG (FE_PDMA_OFFSET + 0x04)
  3713. +#define FE_PDMA_SCH_CFG (FE_PDMA_OFFSET + 0x08)
  3714. +#define FE_DLY_INT_CFG (FE_PDMA_OFFSET + 0x0C)
  3715. +#define FE_TX_BASE_PTR0 (FE_PDMA_OFFSET + 0x10)
  3716. +#define FE_TX_MAX_CNT0 (FE_PDMA_OFFSET + 0x14)
  3717. +#define FE_TX_CTX_IDX0 (FE_PDMA_OFFSET + 0x18)
  3718. +#define FE_TX_DTX_IDX0 (FE_PDMA_OFFSET + 0x1C)
  3719. +#define FE_TX_BASE_PTR1 (FE_PDMA_OFFSET + 0x20)
  3720. +#define FE_TX_MAX_CNT1 (FE_PDMA_OFFSET + 0x24)
  3721. +#define FE_TX_CTX_IDX1 (FE_PDMA_OFFSET + 0x28)
  3722. +#define FE_TX_DTX_IDX1 (FE_PDMA_OFFSET + 0x2C)
  3723. +#define FE_RX_BASE_PTR0 (FE_PDMA_OFFSET + 0x30)
  3724. +#define FE_RX_MAX_CNT0 (FE_PDMA_OFFSET + 0x34)
  3725. +#define FE_RX_CALC_IDX0 (FE_PDMA_OFFSET + 0x38)
  3726. +#define FE_RX_DRX_IDX0 (FE_PDMA_OFFSET + 0x3C)
  3727. +#define FE_TX_BASE_PTR2 (FE_PDMA_OFFSET + 0x40)
  3728. +#define FE_TX_MAX_CNT2 (FE_PDMA_OFFSET + 0x44)
  3729. +#define FE_TX_CTX_IDX2 (FE_PDMA_OFFSET + 0x48)
  3730. +#define FE_TX_DTX_IDX2 (FE_PDMA_OFFSET + 0x4C)
  3731. +#define FE_TX_BASE_PTR3 (FE_PDMA_OFFSET + 0x50)
  3732. +#define FE_TX_MAX_CNT3 (FE_PDMA_OFFSET + 0x54)
  3733. +#define FE_TX_CTX_IDX3 (FE_PDMA_OFFSET + 0x58)
  3734. +#define FE_TX_DTX_IDX3 (FE_PDMA_OFFSET + 0x5C)
  3735. +#define FE_RX_BASE_PTR1 (FE_PDMA_OFFSET + 0x60)
  3736. +#define FE_RX_MAX_CNT1 (FE_PDMA_OFFSET + 0x64)
  3737. +#define FE_RX_CALC_IDX1 (FE_PDMA_OFFSET + 0x68)
  3738. +#define FE_RX_DRX_IDX1 (FE_PDMA_OFFSET + 0x6C)
  3739. +
  3740. +#define RT5350_SDM_CFG (RT5350_SDM_OFFSET + 0x00) //Switch DMA configuration
  3741. +#define RT5350_SDM_RRING (RT5350_SDM_OFFSET + 0x04) //Switch DMA Rx Ring
  3742. +#define RT5350_SDM_TRING (RT5350_SDM_OFFSET + 0x08) //Switch DMA Tx Ring
  3743. +#define RT5350_SDM_MAC_ADRL (RT5350_SDM_OFFSET + 0x0C) //Switch MAC address LSB
  3744. +#define RT5350_SDM_MAC_ADRH (RT5350_SDM_OFFSET + 0x10) //Switch MAC Address MSB
  3745. +#define RT5350_SDM_TPCNT (RT5350_SDM_OFFSET + 0x100) //Switch DMA Tx packet count
  3746. +#define RT5350_SDM_TBCNT (RT5350_SDM_OFFSET + 0x104) //Switch DMA Tx byte count
  3747. +#define RT5350_SDM_RPCNT (RT5350_SDM_OFFSET + 0x108) //Switch DMA rx packet count
  3748. +#define RT5350_SDM_RBCNT (RT5350_SDM_OFFSET + 0x10C) //Switch DMA rx byte count
  3749. +#define RT5350_SDM_CS_ERR (RT5350_SDM_OFFSET + 0x110) //Switch DMA rx checksum error count
  3750. +
  3751. +#define RT5350_SDM_ICS_EN BIT(16)
  3752. +#define RT5350_SDM_TCS_EN BIT(17)
  3753. +#define RT5350_SDM_UCS_EN BIT(18)
  3754. +
  3755. +
  3756. +/* MDIO_CFG register bits */
  3757. +#define FE_MDIO_CFG_AUTO_POLL_EN BIT(29)
  3758. +#define FE_MDIO_CFG_GP1_BP_EN BIT(16)
  3759. +#define FE_MDIO_CFG_GP1_FRC_EN BIT(15)
  3760. +#define FE_MDIO_CFG_GP1_SPEED_10 (0 << 13)
  3761. +#define FE_MDIO_CFG_GP1_SPEED_100 (1 << 13)
  3762. +#define FE_MDIO_CFG_GP1_SPEED_1000 (2 << 13)
  3763. +#define FE_MDIO_CFG_GP1_DUPLEX BIT(12)
  3764. +#define FE_MDIO_CFG_GP1_FC_TX BIT(11)
  3765. +#define FE_MDIO_CFG_GP1_FC_RX BIT(10)
  3766. +#define FE_MDIO_CFG_GP1_LNK_DWN BIT(9)
  3767. +#define FE_MDIO_CFG_GP1_AN_FAIL BIT(8)
  3768. +#define FE_MDIO_CFG_MDC_CLK_DIV_1 (0 << 6)
  3769. +#define FE_MDIO_CFG_MDC_CLK_DIV_2 (1 << 6)
  3770. +#define FE_MDIO_CFG_MDC_CLK_DIV_4 (2 << 6)
  3771. +#define FE_MDIO_CFG_MDC_CLK_DIV_8 (3 << 6)
  3772. +#define FE_MDIO_CFG_TURBO_MII_FREQ BIT(5)
  3773. +#define FE_MDIO_CFG_TURBO_MII_MODE BIT(4)
  3774. +#define FE_MDIO_CFG_RX_CLK_SKEW_0 (0 << 2)
  3775. +#define FE_MDIO_CFG_RX_CLK_SKEW_200 (1 << 2)
  3776. +#define FE_MDIO_CFG_RX_CLK_SKEW_400 (2 << 2)
  3777. +#define FE_MDIO_CFG_RX_CLK_SKEW_INV (3 << 2)
  3778. +#define FE_MDIO_CFG_TX_CLK_SKEW_0 0
  3779. +#define FE_MDIO_CFG_TX_CLK_SKEW_200 1
  3780. +#define FE_MDIO_CFG_TX_CLK_SKEW_400 2
  3781. +#define FE_MDIO_CFG_TX_CLK_SKEW_INV 3
  3782. +
  3783. +/* uni-cast port */
  3784. +#define FE_GDM1_ICS_EN BIT(22)
  3785. +#define FE_GDM1_TCS_EN BIT(21)
  3786. +#define FE_GDM1_UCS_EN BIT(20)
  3787. +#define FE_GDM1_JMB_EN BIT(19)
  3788. +#define FE_GDM1_STRPCRC BIT(16)
  3789. +#define FE_GDM1_UFRC_P_CPU (0 << 12)
  3790. +#define FE_GDM1_UFRC_P_GDMA1 (1 << 12)
  3791. +#define FE_GDM1_UFRC_P_PPE (6 << 12)
  3792. +
  3793. +/* checksums */
  3794. +#define FE_ICS_GEN_EN BIT(2)
  3795. +#define FE_UCS_GEN_EN BIT(1)
  3796. +#define FE_TCS_GEN_EN BIT(0)
  3797. +
  3798. +/* dma ring */
  3799. +#define FE_PST_DRX_IDX0 BIT(16)
  3800. +#define FE_PST_DTX_IDX3 BIT(3)
  3801. +#define FE_PST_DTX_IDX2 BIT(2)
  3802. +#define FE_PST_DTX_IDX1 BIT(1)
  3803. +#define FE_PST_DTX_IDX0 BIT(0)
  3804. +
  3805. +#define FE_TX_WB_DDONE BIT(6)
  3806. +#define FE_RX_DMA_BUSY BIT(3)
  3807. +#define FE_TX_DMA_BUSY BIT(1)
  3808. +#define FE_RX_DMA_EN BIT(2)
  3809. +#define FE_TX_DMA_EN BIT(0)
  3810. +
  3811. +#define FE_PDMA_SIZE_4DWORDS (0 << 4)
  3812. +#define FE_PDMA_SIZE_8DWORDS (1 << 4)
  3813. +#define FE_PDMA_SIZE_16DWORDS (2 << 4)
  3814. +
  3815. +#define FE_US_CYC_CNT_MASK 0xff
  3816. +#define FE_US_CYC_CNT_SHIFT 0x8
  3817. +#define FE_US_CYC_CNT_DIVISOR 1000000
  3818. +
  3819. +#define RX_DMA_PLEN0(_x) (((_x) >> 16) & 0x3fff)
  3820. +#define RX_DMA_LSO BIT(30)
  3821. +#define RX_DMA_DONE BIT(31)
  3822. +#define RX_DMA_L4VALID BIT(30)
  3823. +
  3824. +struct fe_rx_dma {
  3825. + unsigned int rxd1;
  3826. + unsigned int rxd2;
  3827. + unsigned int rxd3;
  3828. + unsigned int rxd4;
  3829. +} __packed __aligned(4);
  3830. +
  3831. +#define TX_DMA_PLEN0_MASK ((0x3fff) << 16)
  3832. +#define TX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
  3833. +#define TX_DMA_LSO BIT(30)
  3834. +#define TX_DMA_DONE BIT(31)
  3835. +#define TX_DMA_QN(_x) ((_x) << 16)
  3836. +#define TX_DMA_PN(_x) ((_x) << 24)
  3837. +#define TX_DMA_QN_MASK TX_DMA_QN(0x7)
  3838. +#define TX_DMA_PN_MASK TX_DMA_PN(0x7)
  3839. +#define TX_DMA_CHKSUM (0x7 << 29)
  3840. +
  3841. +struct fe_tx_dma {
  3842. + unsigned int txd1;
  3843. + unsigned int txd2;
  3844. + unsigned int txd3;
  3845. + unsigned int txd4;
  3846. +} __packed __aligned(4);
  3847. +
  3848. +struct fe_priv;
  3849. +
  3850. +struct fe_phy {
  3851. + struct phy_device *phy[8];
  3852. + struct device_node *phy_node[8];
  3853. + const __be32 *phy_fixed[8];
  3854. + int duplex[8];
  3855. + int speed[8];
  3856. + int tx_fc[8];
  3857. + int rx_fc[8];
  3858. + spinlock_t lock;
  3859. +
  3860. + int (*connect)(struct fe_priv *priv);
  3861. + void (*disconnect)(struct fe_priv *priv);
  3862. + void (*start)(struct fe_priv *priv);
  3863. + void (*stop)(struct fe_priv *priv);
  3864. +};
  3865. +
  3866. +struct fe_soc_data
  3867. +{
  3868. + unsigned char mac[6];
  3869. + const u32 *reg_table;
  3870. +
  3871. + void (*reset_fe)(void);
  3872. + void (*set_mac)(struct fe_priv *priv, unsigned char *mac);
  3873. + void (*fwd_config)(struct fe_priv *priv);
  3874. + void (*tx_dma)(struct fe_priv *priv, int idx, int len);
  3875. + void (*rx_dma)(struct fe_priv *priv, int idx, int len);
  3876. + int (*switch_init)(struct fe_priv *priv);
  3877. + int (*switch_config)(struct fe_priv *priv);
  3878. + void (*port_init)(struct fe_priv *priv, struct device_node *port);
  3879. + int (*has_carrier)(struct fe_priv *priv);
  3880. + int (*mdio_init)(struct fe_priv *priv);
  3881. + void (*mdio_cleanup)(struct fe_priv *priv);
  3882. + int (*mdio_write)(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
  3883. + int (*mdio_read)(struct mii_bus *bus, int phy_addr, int phy_reg);
  3884. + void (*mdio_adjust_link)(struct fe_priv *priv, int port);
  3885. +
  3886. + void *swpriv;
  3887. + u32 pdma_glo_cfg;
  3888. + u32 rx_dly_int;
  3889. + u32 tx_dly_int;
  3890. + u32 checksum_bit;
  3891. +
  3892. + int min_pkt_len;
  3893. +};
  3894. +
  3895. +struct fe_priv
  3896. +{
  3897. + spinlock_t page_lock;
  3898. +
  3899. + struct fe_soc_data *soc;
  3900. + struct net_device *netdev;
  3901. + struct device *device;
  3902. + unsigned long sysclk;
  3903. +
  3904. + struct fe_rx_dma *rx_dma;
  3905. + struct napi_struct rx_napi;
  3906. + struct sk_buff *rx_skb[NUM_DMA_DESC];
  3907. + dma_addr_t rx_phys;
  3908. +
  3909. + struct fe_tx_dma *tx_dma;
  3910. + struct tasklet_struct tx_tasklet;
  3911. + struct sk_buff *tx_skb[NUM_DMA_DESC];
  3912. + dma_addr_t tx_phys;
  3913. + unsigned int tx_free_idx;
  3914. +
  3915. + struct fe_phy *phy;
  3916. + struct mii_bus *mii_bus;
  3917. + int mii_irq[PHY_MAX_ADDR];
  3918. +
  3919. + int link[8];
  3920. +};
  3921. +
  3922. +extern const struct of_device_id of_fe_match[];
  3923. +
  3924. +void fe_w32(u32 val, unsigned reg);
  3925. +u32 fe_r32(unsigned reg);
  3926. +
  3927. +#endif /* FE_ETH_H */
  3928. --- /dev/null
  3929. +++ b/drivers/net/ethernet/ralink/soc_mt7620.c
  3930. @@ -0,0 +1,112 @@
  3931. +/*
  3932. + * This program is free software; you can redistribute it and/or modify
  3933. + * it under the terms of the GNU General Public License as published by
  3934. + * the Free Software Foundation; version 2 of the License
  3935. + *
  3936. + * This program is distributed in the hope that it will be useful,
  3937. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3938. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3939. + * GNU General Public License for more details.
  3940. + *
  3941. + * You should have received a copy of the GNU General Public License
  3942. + * along with this program; if not, write to the Free Software
  3943. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  3944. + *
  3945. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  3946. + */
  3947. +
  3948. +#include <linux/module.h>
  3949. +#include <linux/platform_device.h>
  3950. +
  3951. +#include <asm/mach-ralink/ralink_regs.h>
  3952. +
  3953. +#include "ralink_soc_eth.h"
  3954. +#include "gsw_mt7620a.h"
  3955. +
  3956. +#define MT7620A_CDMA_CSG_CFG 0x400
  3957. +#define MT7620_DMA_VID (MT7620A_CDMA_CSG_CFG | 0x30)
  3958. +#define MT7620A_DMA_2B_OFFSET BIT(31)
  3959. +#define MT7620A_RESET_FE BIT(21)
  3960. +#define MT7620A_RESET_ESW BIT(23)
  3961. +#define MT7620_L4_VALID BIT(23)
  3962. +
  3963. +#define SYSC_REG_RESET_CTRL 0x34
  3964. +#define MAX_RX_LENGTH 1536
  3965. +
  3966. +#define CDMA_ICS_EN BIT(2)
  3967. +#define CDMA_UCS_EN BIT(1)
  3968. +#define CDMA_TCS_EN BIT(0)
  3969. +
  3970. +#define GDMA_ICS_EN BIT(22)
  3971. +#define GDMA_TCS_EN BIT(21)
  3972. +#define GDMA_UCS_EN BIT(20)
  3973. +
  3974. +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
  3975. + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
  3976. + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
  3977. + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
  3978. + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
  3979. + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
  3980. + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
  3981. + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
  3982. + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
  3983. + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
  3984. + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
  3985. + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
  3986. + [FE_REG_FE_DMA_VID_BASE] = MT7620_DMA_VID,
  3987. +};
  3988. +
  3989. +static void mt7620_fe_reset(void)
  3990. +{
  3991. + rt_sysc_w32(MT7620A_RESET_FE | MT7620A_RESET_ESW, SYSC_REG_RESET_CTRL);
  3992. + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
  3993. +}
  3994. +
  3995. +static void mt7620_fwd_config(struct fe_priv *priv)
  3996. +{
  3997. + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~7, MT7620A_GDMA1_FWD_CFG);
  3998. + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN | GDMA_TCS_EN | GDMA_UCS_EN), MT7620A_GDMA1_FWD_CFG);
  3999. + fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) | (CDMA_ICS_EN | CDMA_UCS_EN | CDMA_TCS_EN), MT7620A_CDMA_CSG_CFG);
  4000. +}
  4001. +
  4002. +static void mt7620_tx_dma(struct fe_priv *priv, int idx, int len)
  4003. +{
  4004. + if (len)
  4005. + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(len);
  4006. + else
  4007. + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_DONE;
  4008. +}
  4009. +
  4010. +static void mt7620_rx_dma(struct fe_priv *priv, int idx, int len)
  4011. +{
  4012. + priv->rx_dma[idx].rxd2 = RX_DMA_PLEN0(len);
  4013. +}
  4014. +
  4015. +static struct fe_soc_data mt7620_data = {
  4016. + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
  4017. + .reset_fe = mt7620_fe_reset,
  4018. + .set_mac = mt7620_set_mac,
  4019. + .fwd_config = mt7620_fwd_config,
  4020. + .tx_dma = mt7620_tx_dma,
  4021. + .rx_dma = mt7620_rx_dma,
  4022. + .switch_init = mt7620_gsw_probe,
  4023. + .switch_config = mt7620_gsw_config,
  4024. + .port_init = mt7620_port_init,
  4025. + .min_pkt_len = 0,
  4026. + .reg_table = rt5350_reg_table,
  4027. + .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS | MT7620A_DMA_2B_OFFSET,
  4028. + .rx_dly_int = RT5350_RX_DLY_INT,
  4029. + .tx_dly_int = RT5350_TX_DLY_INT,
  4030. + .checksum_bit = MT7620_L4_VALID,
  4031. + .has_carrier = mt7620a_has_carrier,
  4032. + .mdio_read = mt7620_mdio_read,
  4033. + .mdio_write = mt7620_mdio_write,
  4034. + .mdio_adjust_link = mt7620_mdio_link_adjust,
  4035. +};
  4036. +
  4037. +const struct of_device_id of_fe_match[] = {
  4038. + { .compatible = "ralink,mt7620a-eth", .data = &mt7620_data },
  4039. + {},
  4040. +};
  4041. +
  4042. +MODULE_DEVICE_TABLE(of, of_fe_match);
  4043. --- /dev/null
  4044. +++ b/drivers/net/ethernet/ralink/soc_rt2880.c
  4045. @@ -0,0 +1,51 @@
  4046. +/*
  4047. + * This program is free software; you can redistribute it and/or modify
  4048. + * it under the terms of the GNU General Public License as published by
  4049. + * the Free Software Foundation; version 2 of the License
  4050. + *
  4051. + * This program is distributed in the hope that it will be useful,
  4052. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4053. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4054. + * GNU General Public License for more details.
  4055. + *
  4056. + * You should have received a copy of the GNU General Public License
  4057. + * along with this program; if not, write to the Free Software
  4058. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  4059. + *
  4060. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  4061. + */
  4062. +
  4063. +#include <linux/module.h>
  4064. +
  4065. +#include <asm/mach-ralink/ralink_regs.h>
  4066. +
  4067. +#include "ralink_soc_eth.h"
  4068. +#include "mdio_rt2880.h"
  4069. +
  4070. +#define SYSC_REG_RESET_CTRL 0x034
  4071. +#define RT2880_RESET_FE BIT(18)
  4072. +
  4073. +void rt2880_fe_reset(void)
  4074. +{
  4075. + rt_sysc_w32(RT2880_RESET_FE, SYSC_REG_RESET_CTRL);
  4076. +}
  4077. +
  4078. +struct fe_soc_data rt2880_data = {
  4079. + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
  4080. + .reset_fe = rt2880_fe_reset,
  4081. + .min_pkt_len = 64,
  4082. + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
  4083. + .checksum_bit = RX_DMA_L4VALID,
  4084. + .rx_dly_int = FE_RX_DLY_INT,
  4085. + .tx_dly_int = FE_TX_DLY_INT,
  4086. + .mdio_read = rt2880_mdio_read,
  4087. + .mdio_write = rt2880_mdio_write,
  4088. + .mdio_adjust_link = rt2880_mdio_link_adjust,
  4089. +};
  4090. +
  4091. +const struct of_device_id of_fe_match[] = {
  4092. + { .compatible = "ralink,rt2880-eth", .data = &rt2880_data },
  4093. + {},
  4094. +};
  4095. +
  4096. +MODULE_DEVICE_TABLE(of, of_fe_match);
  4097. --- /dev/null
  4098. +++ b/drivers/net/ethernet/ralink/soc_rt305x.c
  4099. @@ -0,0 +1,113 @@
  4100. +/*
  4101. + * This program is free software; you can redistribute it and/or modify
  4102. + * it under the terms of the GNU General Public License as published by
  4103. + * the Free Software Foundation; version 2 of the License
  4104. + *
  4105. + * This program is distributed in the hope that it will be useful,
  4106. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4107. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4108. + * GNU General Public License for more details.
  4109. + *
  4110. + * You should have received a copy of the GNU General Public License
  4111. + * along with this program; if not, write to the Free Software
  4112. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  4113. + *
  4114. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  4115. + */
  4116. +
  4117. +#include <linux/module.h>
  4118. +
  4119. +#include <asm/mach-ralink/ralink_regs.h>
  4120. +
  4121. +#include "ralink_soc_eth.h"
  4122. +
  4123. +#define RT305X_RESET_FE BIT(21)
  4124. +#define RT305X_RESET_ESW BIT(23)
  4125. +#define SYSC_REG_RESET_CTRL 0x034
  4126. +
  4127. +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
  4128. + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
  4129. + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
  4130. + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
  4131. + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
  4132. + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
  4133. + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
  4134. + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
  4135. + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
  4136. + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
  4137. + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
  4138. + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
  4139. + [FE_REG_FE_DMA_VID_BASE] = 0,
  4140. +};
  4141. +
  4142. +static void rt305x_fe_reset(void)
  4143. +{
  4144. + rt_sysc_w32(RT305X_RESET_FE, SYSC_REG_RESET_CTRL);
  4145. + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
  4146. +}
  4147. +
  4148. +static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
  4149. +{
  4150. + unsigned long flags;
  4151. +
  4152. + spin_lock_irqsave(&priv->page_lock, flags);
  4153. + fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
  4154. + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
  4155. + RT5350_SDM_MAC_ADRL);
  4156. + spin_unlock_irqrestore(&priv->page_lock, flags);
  4157. +}
  4158. +
  4159. +static void rt5350_fwd_config(struct fe_priv *priv)
  4160. +{
  4161. + unsigned long sysclk = priv->sysclk;
  4162. +
  4163. + if (sysclk) {
  4164. + sysclk /= FE_US_CYC_CNT_DIVISOR;
  4165. + sysclk <<= FE_US_CYC_CNT_SHIFT;
  4166. +
  4167. + fe_w32((fe_r32(FE_FE_GLO_CFG) &
  4168. + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
  4169. + FE_FE_GLO_CFG);
  4170. + }
  4171. +
  4172. + fe_w32(fe_r32(RT5350_SDM_CFG) & ~0xffff, RT5350_SDM_CFG);
  4173. + fe_w32(fe_r32(RT5350_SDM_CFG) | RT5350_SDM_ICS_EN | RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN,
  4174. + RT5350_SDM_CFG);
  4175. +}
  4176. +
  4177. +static void rt5350_fe_reset(void)
  4178. +{
  4179. + rt_sysc_w32(RT305X_RESET_FE | RT305X_RESET_ESW, SYSC_REG_RESET_CTRL);
  4180. + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
  4181. +}
  4182. +
  4183. +static struct fe_soc_data rt3050_data = {
  4184. + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
  4185. + .reset_fe = rt305x_fe_reset,
  4186. + .min_pkt_len = 64,
  4187. + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
  4188. + .checksum_bit = RX_DMA_L4VALID,
  4189. + .rx_dly_int = FE_RX_DLY_INT,
  4190. + .tx_dly_int = FE_TX_DLY_INT,
  4191. +};
  4192. +
  4193. +static struct fe_soc_data rt5350_data = {
  4194. + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
  4195. + .reg_table = rt5350_reg_table,
  4196. + .reset_fe = rt5350_fe_reset,
  4197. + .set_mac = rt5350_set_mac,
  4198. + .fwd_config = rt5350_fwd_config,
  4199. + .min_pkt_len = 64,
  4200. + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
  4201. + .checksum_bit = RX_DMA_L4VALID,
  4202. + .rx_dly_int = RT5350_RX_DLY_INT,
  4203. + .tx_dly_int = RT5350_TX_DLY_INT,
  4204. +};
  4205. +
  4206. +const struct of_device_id of_fe_match[] = {
  4207. + { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
  4208. + { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
  4209. + {},
  4210. +};
  4211. +
  4212. +MODULE_DEVICE_TABLE(of, of_fe_match);
  4213. --- /dev/null
  4214. +++ b/drivers/net/ethernet/ralink/soc_rt3883.c
  4215. @@ -0,0 +1,60 @@
  4216. +/*
  4217. + * This program is free software; you can redistribute it and/or modify
  4218. + * it under the terms of the GNU General Public License as published by
  4219. + * the Free Software Foundation; version 2 of the License
  4220. + *
  4221. + * This program is distributed in the hope that it will be useful,
  4222. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4223. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4224. + * GNU General Public License for more details.
  4225. + *
  4226. + * You should have received a copy of the GNU General Public License
  4227. + * along with this program; if not, write to the Free Software
  4228. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  4229. + *
  4230. + * Copyright (C) 2009-2013 John Crispin <[email protected]>
  4231. + */
  4232. +
  4233. +#include <linux/module.h>
  4234. +
  4235. +#include <asm/mach-ralink/ralink_regs.h>
  4236. +
  4237. +#include "ralink_soc_eth.h"
  4238. +#include "mdio_rt2880.h"
  4239. +
  4240. +#define RT3883_SYSC_REG_RSTCTRL 0x34
  4241. +#define RT3883_RSTCTRL_FE BIT(21)
  4242. +
  4243. +static void rt3883_fe_reset(void)
  4244. +{
  4245. + u32 t;
  4246. +
  4247. + t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
  4248. + t |= RT3883_RSTCTRL_FE;
  4249. + rt_sysc_w32(t , RT3883_SYSC_REG_RSTCTRL);
  4250. +
  4251. + t &= ~RT3883_RSTCTRL_FE;
  4252. + rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
  4253. +}
  4254. +
  4255. +static struct fe_soc_data rt3883_data = {
  4256. + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
  4257. + .reset_fe = rt3883_fe_reset,
  4258. + .min_pkt_len = 64,
  4259. + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
  4260. + .rx_dly_int = FE_RX_DLY_INT,
  4261. + .tx_dly_int = FE_TX_DLY_INT,
  4262. + .checksum_bit = RX_DMA_L4VALID,
  4263. + .mdio_read = rt2880_mdio_read,
  4264. + .mdio_write = rt2880_mdio_write,
  4265. + .mdio_adjust_link = rt2880_mdio_link_adjust,
  4266. + .port_init = rt2880_port_init,
  4267. +};
  4268. +
  4269. +const struct of_device_id of_fe_match[] = {
  4270. + { .compatible = "ralink,rt3883-eth", .data = &rt3883_data },
  4271. + {},
  4272. +};
  4273. +
  4274. +MODULE_DEVICE_TABLE(of, of_fe_match);
  4275. +
  4276. --- /dev/null
  4277. +++ b/drivers/net/ethernet/ralink/mt7530.c
  4278. @@ -0,0 +1,467 @@
  4279. +/*
  4280. + * This program is free software; you can redistribute it and/or
  4281. + * modify it under the terms of the GNU General Public License
  4282. + * as published by the Free Software Foundation; either version 2
  4283. + * of the License, or (at your option) any later version.
  4284. + *
  4285. + * This program is distributed in the hope that it will be useful,
  4286. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4287. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4288. + * GNU General Public License for more details.
  4289. + *
  4290. + * Copyright (C) 2013 John Crispin <[email protected]>
  4291. + */
  4292. +
  4293. +#include <linux/if.h>
  4294. +#include <linux/module.h>
  4295. +#include <linux/init.h>
  4296. +#include <linux/list.h>
  4297. +#include <linux/if_ether.h>
  4298. +#include <linux/skbuff.h>
  4299. +#include <linux/netdevice.h>
  4300. +#include <linux/netlink.h>
  4301. +#include <linux/bitops.h>
  4302. +#include <net/genetlink.h>
  4303. +#include <linux/switch.h>
  4304. +#include <linux/delay.h>
  4305. +#include <linux/phy.h>
  4306. +#include <linux/netdevice.h>
  4307. +#include <linux/etherdevice.h>
  4308. +#include <linux/lockdep.h>
  4309. +#include <linux/workqueue.h>
  4310. +#include <linux/of_device.h>
  4311. +
  4312. +#include "mt7530.h"
  4313. +
  4314. +#define MT7530_CPU_PORT 6
  4315. +#define MT7530_NUM_PORTS 7
  4316. +#define MT7530_NUM_VLANS 16
  4317. +#define MT7530_NUM_VIDS 16
  4318. +
  4319. +#define REG_ESW_VLAN_VTCR 0x90
  4320. +#define REG_ESW_VLAN_VAWD1 0x94
  4321. +#define REG_ESW_VLAN_VAWD2 0x98
  4322. +
  4323. +enum {
  4324. + /* Global attributes. */
  4325. + MT7530_ATTR_ENABLE_VLAN,
  4326. +};
  4327. +
  4328. +struct mt7530_port {
  4329. + u16 pvid;
  4330. +};
  4331. +
  4332. +struct mt7530_vlan {
  4333. + u8 ports;
  4334. +};
  4335. +
  4336. +struct mt7530_priv {
  4337. + void __iomem *base;
  4338. + struct mii_bus *bus;
  4339. + struct switch_dev swdev;
  4340. +
  4341. + bool global_vlan_enable;
  4342. + struct mt7530_vlan vlans[MT7530_NUM_VLANS];
  4343. + struct mt7530_port ports[MT7530_NUM_PORTS];
  4344. +};
  4345. +
  4346. +struct mt7530_mapping {
  4347. + char *name;
  4348. + u8 pvids[6];
  4349. + u8 vlans[8];
  4350. +} mt7530_defaults[] = {
  4351. + {
  4352. + .name = "llllw",
  4353. + .pvids = { 1, 1, 1, 1, 2, 1 },
  4354. + .vlans = { 0, 0x4f, 0x50 },
  4355. + }, {
  4356. + .name = "wllll",
  4357. + .pvids = { 2, 1, 1, 1, 1, 1 },
  4358. + .vlans = { 0, 0x5e, 0x41 },
  4359. + },
  4360. +};
  4361. +
  4362. +struct mt7530_mapping*
  4363. +mt7530_find_mapping(struct device_node *np)
  4364. +{
  4365. + const char *map;
  4366. + int i;
  4367. +
  4368. + if (of_property_read_string(np, "ralink,port-map", &map))
  4369. + return NULL;
  4370. +
  4371. + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
  4372. + if (!strcmp(map, mt7530_defaults[i].name))
  4373. + return &mt7530_defaults[i];
  4374. +
  4375. + return NULL;
  4376. +}
  4377. +
  4378. +static void
  4379. +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
  4380. +{
  4381. + int i = 0;
  4382. +
  4383. + mt7530->global_vlan_enable = 1;
  4384. +
  4385. + for (i = 0; i < 6; i++)
  4386. + mt7530->ports[i].pvid = map->pvids[i];
  4387. + for (i = 0; i < 8; i++)
  4388. + mt7530->vlans[i].ports = map->vlans[i];
  4389. +}
  4390. +
  4391. +static int
  4392. +mt7530_reset_switch(struct switch_dev *dev)
  4393. +{
  4394. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4395. +
  4396. + memset(priv->ports, 0, sizeof(priv->ports));
  4397. + memset(priv->vlans, 0, sizeof(priv->vlans));
  4398. +
  4399. + return 0;
  4400. +}
  4401. +
  4402. +static int
  4403. +mt7530_get_vlan_enable(struct switch_dev *dev,
  4404. + const struct switch_attr *attr,
  4405. + struct switch_val *val)
  4406. +{
  4407. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4408. +
  4409. + val->value.i = priv->global_vlan_enable;
  4410. +
  4411. + return 0;
  4412. +}
  4413. +
  4414. +static int
  4415. +mt7530_set_vlan_enable(struct switch_dev *dev,
  4416. + const struct switch_attr *attr,
  4417. + struct switch_val *val)
  4418. +{
  4419. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4420. +
  4421. + priv->global_vlan_enable = val->value.i != 0;
  4422. +
  4423. + return 0;
  4424. +}
  4425. +
  4426. +static u32
  4427. +mt7530_r32(struct mt7530_priv *priv, u32 reg)
  4428. +{
  4429. + if (priv->bus) {
  4430. + u16 high, low;
  4431. +
  4432. + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  4433. + low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
  4434. + high = mdiobus_read(priv->bus, 0x1f, 0x10);
  4435. +
  4436. + return (high << 16) | (low & 0xffff);
  4437. + }
  4438. +
  4439. + return ioread32(priv->base + reg);
  4440. +}
  4441. +
  4442. +static void
  4443. +mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
  4444. +{
  4445. + if (priv->bus) {
  4446. + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  4447. + mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
  4448. + mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
  4449. + return;
  4450. + }
  4451. +
  4452. + iowrite32(val, priv->base + reg);
  4453. +}
  4454. +
  4455. +static void
  4456. +mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
  4457. +{
  4458. + int i;
  4459. +
  4460. + mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
  4461. +
  4462. + for (i = 0; i < 20; i++) {
  4463. + u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
  4464. +
  4465. + if ((val & BIT(31)) == 0)
  4466. + break;
  4467. +
  4468. + udelay(1000);
  4469. + }
  4470. + if (i == 20)
  4471. + printk("mt7530: vtcr timeout\n");
  4472. +}
  4473. +
  4474. +static int
  4475. +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
  4476. +{
  4477. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4478. +
  4479. + if (port >= MT7530_NUM_PORTS)
  4480. + return -EINVAL;
  4481. +
  4482. + *val = mt7530_r32(priv, 0x2014 + (0x100 * port));
  4483. + *val &= 0xff;
  4484. +
  4485. + return 0;
  4486. +}
  4487. +
  4488. +static int
  4489. +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
  4490. +{
  4491. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4492. +
  4493. + if (port >= MT7530_NUM_PORTS)
  4494. + return -1;
  4495. +
  4496. + priv->ports[port].pvid = pvid;
  4497. +
  4498. + return 0;
  4499. +}
  4500. +
  4501. +static int
  4502. +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  4503. +{
  4504. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4505. + u32 member;
  4506. + int i;
  4507. +
  4508. + val->len = 0;
  4509. +
  4510. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VIDS)
  4511. + return -EINVAL;
  4512. +
  4513. + mt7530_vtcr(priv, 0, val->port_vlan);
  4514. + member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
  4515. + member >>= 16;
  4516. + member &= 0xff;
  4517. +
  4518. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  4519. + struct switch_port *p;
  4520. + if (!(member & BIT(i)))
  4521. + continue;
  4522. +
  4523. + p = &val->value.ports[val->len++];
  4524. + p->id = i;
  4525. + p->flags = 0;
  4526. + }
  4527. +
  4528. + return 0;
  4529. +}
  4530. +
  4531. +static int
  4532. +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  4533. +{
  4534. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4535. + int ports = 0;
  4536. + int i;
  4537. +
  4538. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VIDS ||
  4539. + val->len > MT7530_NUM_PORTS)
  4540. + return -EINVAL;
  4541. +
  4542. + for (i = 0; i < val->len; i++) {
  4543. + struct switch_port *p = &val->value.ports[i];
  4544. +
  4545. + if (p->id >= MT7530_NUM_PORTS)
  4546. + return -EINVAL;
  4547. +
  4548. + ports |= BIT(p->id);
  4549. + }
  4550. + priv->vlans[val->port_vlan].ports = ports;
  4551. +
  4552. + return 0;
  4553. +}
  4554. +
  4555. +static int
  4556. +mt7530_apply_config(struct switch_dev *dev)
  4557. +{
  4558. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4559. + int i;
  4560. +
  4561. + if (!priv->global_vlan_enable) {
  4562. + mt7530_w32(priv, 0x2004, 0xff000);
  4563. + mt7530_w32(priv, 0x2104, 0xff000);
  4564. + mt7530_w32(priv, 0x2204, 0xff000);
  4565. + mt7530_w32(priv, 0x2304, 0xff000);
  4566. + mt7530_w32(priv, 0x2404, 0xff000);
  4567. + mt7530_w32(priv, 0x2504, 0xff000);
  4568. + mt7530_w32(priv, 0x2604, 0xff000);
  4569. + mt7530_w32(priv, 0x2010, 0x810000c);
  4570. + mt7530_w32(priv, 0x2110, 0x810000c);
  4571. + mt7530_w32(priv, 0x2210, 0x810000c);
  4572. + mt7530_w32(priv, 0x2310, 0x810000c);
  4573. + mt7530_w32(priv, 0x2410, 0x810000c);
  4574. + mt7530_w32(priv, 0x2510, 0x810000c);
  4575. + mt7530_w32(priv, 0x2610, 0x810000c);
  4576. + return 0;
  4577. + }
  4578. +
  4579. + // LAN/WAN ports as security mode
  4580. + mt7530_w32(priv, 0x2004, 0xff0003);
  4581. + mt7530_w32(priv, 0x2104, 0xff0003);
  4582. + mt7530_w32(priv, 0x2204, 0xff0003);
  4583. + mt7530_w32(priv, 0x2304, 0xff0003);
  4584. + mt7530_w32(priv, 0x2404, 0xff0003);
  4585. + mt7530_w32(priv, 0x2504, 0xff0003);
  4586. + // LAN/WAN ports as transparent port
  4587. + mt7530_w32(priv, 0x2010, 0x810000c0);
  4588. + mt7530_w32(priv, 0x2110, 0x810000c0);
  4589. + mt7530_w32(priv, 0x2210, 0x810000c0);
  4590. + mt7530_w32(priv, 0x2310, 0x810000c0);
  4591. + mt7530_w32(priv, 0x2410, 0x810000c0);
  4592. + mt7530_w32(priv, 0x2510, 0x810000c0);
  4593. +
  4594. + // set CPU/P7 port as user port
  4595. + mt7530_w32(priv, 0x2610, 0x81000000);
  4596. + mt7530_w32(priv, 0x2710, 0x81000000);
  4597. +
  4598. + mt7530_w32(priv, 0x2604, 0x20ff0003);
  4599. + mt7530_w32(priv, 0x2704, 0x20ff0003);
  4600. + mt7530_w32(priv, 0x2610, 0x81000000);
  4601. +
  4602. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  4603. + u8 ports = priv->vlans[i].ports;
  4604. + u32 val = mt7530_r32(priv, 0x100 + 4 * (i / 2));
  4605. +
  4606. + if (i % 2 == 0) {
  4607. + val &= 0xfff000;
  4608. + val |= i;
  4609. + } else {
  4610. + val &= 0xfff;
  4611. + val |= (i << 12);
  4612. + }
  4613. + mt7530_w32(priv, 0x100 + 4 * (i / 2), val);
  4614. +
  4615. + if (ports)
  4616. + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, BIT(30) | (ports << 16) | BIT(0));
  4617. + else
  4618. + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
  4619. +
  4620. + mt7530_vtcr(priv, 1, i);
  4621. + }
  4622. +
  4623. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  4624. + mt7530_w32(priv, 0x2014 + (0x100 * i), 0x10000 | priv->ports[i].pvid);
  4625. +
  4626. + return 0;
  4627. +}
  4628. +
  4629. +static int
  4630. +mt7530_get_port_link(struct switch_dev *dev, int port,
  4631. + struct switch_port_link *link)
  4632. +{
  4633. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  4634. + u32 speed, pmsr;
  4635. +
  4636. + if (port < 0 || port >= MT7530_NUM_PORTS)
  4637. + return -EINVAL;
  4638. +
  4639. + pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
  4640. +
  4641. + link->link = pmsr & 1;
  4642. + link->duplex = (pmsr >> 1) & 1;
  4643. + speed = (pmsr >> 2) & 3;
  4644. +
  4645. + switch (speed) {
  4646. + case 0:
  4647. + link->speed = SWITCH_PORT_SPEED_10;
  4648. + break;
  4649. + case 1:
  4650. + link->speed = SWITCH_PORT_SPEED_100;
  4651. + break;
  4652. + case 2:
  4653. + case 3: /* forced gige speed can be 2 or 3 */
  4654. + link->speed = SWITCH_PORT_SPEED_1000;
  4655. + break;
  4656. + default:
  4657. + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  4658. + break;
  4659. + }
  4660. +
  4661. + return 0;
  4662. +}
  4663. +
  4664. +static const struct switch_attr mt7530_global[] = {
  4665. + {
  4666. + .type = SWITCH_TYPE_INT,
  4667. + .name = "enable_vlan",
  4668. + .description = "VLAN mode (1:enabled)",
  4669. + .max = 1,
  4670. + .id = MT7530_ATTR_ENABLE_VLAN,
  4671. + .get = mt7530_get_vlan_enable,
  4672. + .set = mt7530_set_vlan_enable,
  4673. + },
  4674. +};
  4675. +
  4676. +static const struct switch_attr mt7530_port[] = {
  4677. +};
  4678. +
  4679. +static const struct switch_attr mt7530_vlan[] = {
  4680. +};
  4681. +
  4682. +static const struct switch_dev_ops mt7530_ops = {
  4683. + .attr_global = {
  4684. + .attr = mt7530_global,
  4685. + .n_attr = ARRAY_SIZE(mt7530_global),
  4686. + },
  4687. + .attr_port = {
  4688. + .attr = mt7530_port,
  4689. + .n_attr = ARRAY_SIZE(mt7530_port),
  4690. + },
  4691. + .attr_vlan = {
  4692. + .attr = mt7530_vlan,
  4693. + .n_attr = ARRAY_SIZE(mt7530_vlan),
  4694. + },
  4695. + .get_vlan_ports = mt7530_get_vlan_ports,
  4696. + .set_vlan_ports = mt7530_set_vlan_ports,
  4697. + .get_port_pvid = mt7530_get_port_pvid,
  4698. + .set_port_pvid = mt7530_set_port_pvid,
  4699. + .get_port_link = mt7530_get_port_link,
  4700. + .apply_config = mt7530_apply_config,
  4701. + .reset_switch = mt7530_reset_switch,
  4702. +};
  4703. +
  4704. +int
  4705. +mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus)
  4706. +{
  4707. + struct switch_dev *swdev;
  4708. + struct mt7530_priv *mt7530;
  4709. + struct mt7530_mapping *map;
  4710. + int ret;
  4711. +
  4712. + if (bus && bus->phy_map[0x1f]->phy_id != 0x1beef)
  4713. + return 0;
  4714. +
  4715. + mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
  4716. + if (!mt7530)
  4717. + return -ENOMEM;
  4718. +
  4719. + mt7530->base = base;
  4720. + mt7530->bus = bus;
  4721. + mt7530->global_vlan_enable = 1;
  4722. +
  4723. + swdev = &mt7530->swdev;
  4724. + swdev->name = "mt7530";
  4725. + swdev->alias = "mt7530";
  4726. + swdev->cpu_port = MT7530_CPU_PORT;
  4727. + swdev->ports = MT7530_NUM_PORTS;
  4728. + swdev->vlans = MT7530_NUM_VLANS;
  4729. + swdev->ops = &mt7530_ops;
  4730. +
  4731. + ret = register_switch(swdev, NULL);
  4732. + if (ret) {
  4733. + dev_err(dev, "failed to register mt7530\n");
  4734. + return ret;
  4735. + }
  4736. +
  4737. + dev_info(dev, "loaded mt7530 driver\n");
  4738. +
  4739. + map = mt7530_find_mapping(dev->of_node);
  4740. + if (map)
  4741. + mt7530_apply_mapping(mt7530, map);
  4742. + mt7530_apply_config(swdev);
  4743. +
  4744. + return 0;
  4745. +}
  4746. --- /dev/null
  4747. +++ b/drivers/net/ethernet/ralink/mt7530.h
  4748. @@ -0,0 +1,20 @@
  4749. +/*
  4750. + * This program is free software; you can redistribute it and/or
  4751. + * modify it under the terms of the GNU General Public License
  4752. + * as published by the Free Software Foundation; either version 2
  4753. + * of the License, or (at your option) any later version.
  4754. + *
  4755. + * This program is distributed in the hope that it will be useful,
  4756. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4757. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4758. + * GNU General Public License for more details.
  4759. + *
  4760. + * Copyright (C) 2013 John Crispin <[email protected]>
  4761. + */
  4762. +
  4763. +#ifndef _MT7530_H__
  4764. +#define _MT7530_H__
  4765. +
  4766. +int mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus);
  4767. +
  4768. +#endif