r8152.c 243 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
  4. */
  5. #include <linux/signal.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/mii.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/hex.h>
  13. #include <linux/phy.h>
  14. #include <linux/usb.h>
  15. #include <linux/crc32.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/list.h>
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h>
  21. #include <net/ip6_checksum.h>
  22. #include <uapi/linux/mdio.h>
  23. #include <linux/mdio.h>
  24. #include <linux/usb/cdc.h>
  25. #include <linux/suspend.h>
  26. #include <linux/atomic.h>
  27. #include <linux/acpi.h>
  28. #include <linux/firmware.h>
  29. #include <crypto/sha2.h>
  30. #include <linux/usb/r8152.h>
  31. #include <net/gso.h>
  32. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  33. #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
  34. #define MODULENAME "r8152"
  35. #define R8152_PHY_ID 32
  36. #define PLA_IDR 0xc000
  37. #define PLA_RCR 0xc010
  38. #define PLA_RCR1 0xc012
  39. #define PLA_RMS 0xc016
  40. #define PLA_RXFIFO_CTRL0 0xc0a0
  41. #define PLA_RXFIFO_FULL 0xc0a2
  42. #define PLA_RXFIFO_CTRL1 0xc0a4
  43. #define PLA_RX_FIFO_FULL 0xc0a6
  44. #define PLA_RXFIFO_CTRL2 0xc0a8
  45. #define PLA_RX_FIFO_EMPTY 0xc0aa
  46. #define PLA_DMY_REG0 0xc0b0
  47. #define PLA_FMC 0xc0b4
  48. #define PLA_CFG_WOL 0xc0b6
  49. #define PLA_TEREDO_CFG 0xc0bc
  50. #define PLA_TEREDO_WAKE_BASE 0xc0c4
  51. #define PLA_MAR 0xcd00
  52. #define PLA_BACKUP 0xd000
  53. #define PLA_BDC_CR 0xd1a0
  54. #define PLA_TEREDO_TIMER 0xd2cc
  55. #define PLA_REALWOW_TIMER 0xd2e8
  56. #define PLA_UPHY_TIMER 0xd388
  57. #define PLA_SUSPEND_FLAG 0xd38a
  58. #define PLA_INDICATE_FALG 0xd38c
  59. #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
  60. #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
  61. #define PLA_EXTRA_STATUS 0xd398
  62. #define PLA_GPHY_CTRL 0xd3ae
  63. #define PLA_POL_GPIO_CTRL 0xdc6a
  64. #define PLA_EFUSE_DATA 0xdd00
  65. #define PLA_EFUSE_CMD 0xdd02
  66. #define PLA_LEDSEL 0xdd90
  67. #define PLA_LED_FEATURE 0xdd92
  68. #define PLA_PHYAR 0xde00
  69. #define PLA_BOOT_CTRL 0xe004
  70. #define PLA_LWAKE_CTRL_REG 0xe007
  71. #define PLA_GPHY_INTR_IMR 0xe022
  72. #define PLA_EEE_CR 0xe040
  73. #define PLA_EEE_TXTWSYS 0xe04c
  74. #define PLA_EEE_TXTWSYS_2P5G 0xe058
  75. #define PLA_EEEP_CR 0xe080
  76. #define PLA_MAC_PWR_CTRL 0xe0c0
  77. #define PLA_MAC_PWR_CTRL2 0xe0ca
  78. #define PLA_MAC_PWR_CTRL3 0xe0cc
  79. #define PLA_MAC_PWR_CTRL4 0xe0ce
  80. #define PLA_WDT6_CTRL 0xe428
  81. #define PLA_TCR0 0xe610
  82. #define PLA_TCR1 0xe612
  83. #define PLA_MTPS 0xe615
  84. #define PLA_TXFIFO_CTRL 0xe618
  85. #define PLA_TXFIFO_FULL 0xe61a
  86. #define PLA_RSTTALLY 0xe800
  87. #define PLA_CR 0xe813
  88. #define PLA_CRWECR 0xe81c
  89. #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
  90. #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
  91. #define PLA_CONFIG5 0xe822
  92. #define PLA_PHY_PWR 0xe84c
  93. #define PLA_OOB_CTRL 0xe84f
  94. #define PLA_CPCR 0xe854
  95. #define PLA_MISC_0 0xe858
  96. #define PLA_MISC_1 0xe85a
  97. #define PLA_OCP_GPHY_BASE 0xe86c
  98. #define PLA_TALLYCNT 0xe890
  99. #define PLA_SFF_STS_7 0xe8de
  100. #define PLA_PHYSTATUS 0xe908
  101. #define PLA_CONFIG6 0xe90a /* CONFIG6 */
  102. #define PLA_USB_CFG 0xe952
  103. #define PLA_BP_BA 0xfc26
  104. #define PLA_BP_0 0xfc28
  105. #define PLA_BP_1 0xfc2a
  106. #define PLA_BP_2 0xfc2c
  107. #define PLA_BP_3 0xfc2e
  108. #define PLA_BP_4 0xfc30
  109. #define PLA_BP_5 0xfc32
  110. #define PLA_BP_6 0xfc34
  111. #define PLA_BP_7 0xfc36
  112. #define PLA_BP_EN 0xfc38
  113. #define USB_USB2PHY 0xb41e
  114. #define USB_SSPHYLINK1 0xb426
  115. #define USB_SSPHYLINK2 0xb428
  116. #define USB_L1_CTRL 0xb45e
  117. #define USB_U2P3_CTRL 0xb460
  118. #define USB_CSR_DUMMY1 0xb464
  119. #define USB_CSR_DUMMY2 0xb466
  120. #define USB_DEV_STAT 0xb808
  121. #define USB_CONNECT_TIMER 0xcbf8
  122. #define USB_MSC_TIMER 0xcbfc
  123. #define USB_BURST_SIZE 0xcfc0
  124. #define USB_FW_FIX_EN0 0xcfca
  125. #define USB_FW_FIX_EN1 0xcfcc
  126. #define USB_LPM_CONFIG 0xcfd8
  127. #define USB_ECM_OPTION 0xcfee
  128. #define USB_CSTMR 0xcfef /* RTL8153A */
  129. #define USB_MISC_2 0xcfff
  130. #define USB_ECM_OP 0xd26b
  131. #define USB_GPHY_CTRL 0xd284
  132. #define USB_SPEED_OPTION 0xd32a
  133. #define USB_FW_CTRL 0xd334 /* RTL8153B */
  134. #define USB_FC_TIMER 0xd340
  135. #define USB_USB_CTRL 0xd406
  136. #define USB_PHY_CTRL 0xd408
  137. #define USB_TX_AGG 0xd40a
  138. #define USB_RX_BUF_TH 0xd40c
  139. #define USB_USB_TIMER 0xd428
  140. #define USB_RX_EARLY_TIMEOUT 0xd42c
  141. #define USB_RX_EARLY_SIZE 0xd42e
  142. #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
  143. #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
  144. #define USB_TX_DMA 0xd434
  145. #define USB_UPT_RXDMA_OWN 0xd437
  146. #define USB_UPHY3_MDCMDIO 0xd480
  147. #define USB_TOLERANCE 0xd490
  148. #define USB_LPM_CTRL 0xd41a
  149. #define USB_BMU_RESET 0xd4b0
  150. #define USB_BMU_CONFIG 0xd4b4
  151. #define USB_U1U2_TIMER 0xd4da
  152. #define USB_FW_TASK 0xd4e8 /* RTL8153B */
  153. #define USB_RX_AGGR_NUM 0xd4ee
  154. #define USB_UPS_CTRL 0xd800
  155. #define USB_POWER_CUT 0xd80a
  156. #define USB_MISC_0 0xd81a
  157. #define USB_MISC_1 0xd81f
  158. #define USB_AFE_CTRL2 0xd824
  159. #define USB_UPHY_XTAL 0xd826
  160. #define USB_UPS_CFG 0xd842
  161. #define USB_UPS_FLAGS 0xd848
  162. #define USB_WDT1_CTRL 0xe404
  163. #define USB_WDT11_CTRL 0xe43c
  164. #define USB_BP_BA PLA_BP_BA
  165. #define USB_BP_0 PLA_BP_0
  166. #define USB_BP_1 PLA_BP_1
  167. #define USB_BP_2 PLA_BP_2
  168. #define USB_BP_3 PLA_BP_3
  169. #define USB_BP_4 PLA_BP_4
  170. #define USB_BP_5 PLA_BP_5
  171. #define USB_BP_6 PLA_BP_6
  172. #define USB_BP_7 PLA_BP_7
  173. #define USB_BP_EN PLA_BP_EN /* RTL8153A */
  174. #define USB_BP_8 0xfc38 /* RTL8153B */
  175. #define USB_BP_9 0xfc3a
  176. #define USB_BP_10 0xfc3c
  177. #define USB_BP_11 0xfc3e
  178. #define USB_BP_12 0xfc40
  179. #define USB_BP_13 0xfc42
  180. #define USB_BP_14 0xfc44
  181. #define USB_BP_15 0xfc46
  182. #define USB_BP2_EN 0xfc48
  183. /* OCP Registers */
  184. #define OCP_ALDPS_CONFIG 0x2010
  185. #define OCP_EEE_CONFIG1 0x2080
  186. #define OCP_EEE_CONFIG2 0x2092
  187. #define OCP_EEE_CONFIG3 0x2094
  188. #define OCP_BASE_MII 0xa400
  189. #define OCP_EEE_AR 0xa41a
  190. #define OCP_EEE_DATA 0xa41c
  191. #define OCP_PHY_STATUS 0xa420
  192. #define OCP_INTR_EN 0xa424
  193. #define OCP_NCTL_CFG 0xa42c
  194. #define OCP_POWER_CFG 0xa430
  195. #define OCP_EEE_CFG 0xa432
  196. #define OCP_SRAM_ADDR 0xa436
  197. #define OCP_SRAM_DATA 0xa438
  198. #define OCP_DOWN_SPEED 0xa442
  199. #define OCP_EEE_ABLE 0xa5c4
  200. #define OCP_EEE_ADV 0xa5d0
  201. #define OCP_EEE_LPABLE 0xa5d2
  202. #define OCP_10GBT_CTRL 0xa5d4
  203. #define OCP_10GBT_STAT 0xa5d6
  204. #define OCP_EEE_ADV2 0xa6d4
  205. #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
  206. #define OCP_PHY_PATCH_STAT 0xb800
  207. #define OCP_PHY_PATCH_CMD 0xb820
  208. #define OCP_PHY_LOCK 0xb82e
  209. #define OCP_ADC_IOFFSET 0xbcfc
  210. #define OCP_ADC_CFG 0xbc06
  211. #define OCP_SYSCLK_CFG 0xc416
  212. /* SRAM Register */
  213. #define SRAM_GREEN_CFG 0x8011
  214. #define SRAM_LPF_CFG 0x8012
  215. #define SRAM_GPHY_FW_VER 0x801e
  216. #define SRAM_10M_AMP1 0x8080
  217. #define SRAM_10M_AMP2 0x8082
  218. #define SRAM_IMPEDANCE 0x8084
  219. #define SRAM_PHY_LOCK 0xb82e
  220. /* PLA_RCR */
  221. #define RCR_AAP 0x00000001
  222. #define RCR_APM 0x00000002
  223. #define RCR_AM 0x00000004
  224. #define RCR_AB 0x00000008
  225. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  226. #define SLOT_EN BIT(11)
  227. /* PLA_RCR1 */
  228. #define OUTER_VLAN BIT(7)
  229. #define INNER_VLAN BIT(6)
  230. /* PLA_RXFIFO_CTRL0 */
  231. #define RXFIFO_THR1_NORMAL 0x00080002
  232. #define RXFIFO_THR1_OOB 0x01800003
  233. /* PLA_RXFIFO_FULL */
  234. #define RXFIFO_FULL_MASK 0xfff
  235. /* PLA_RXFIFO_CTRL1 */
  236. #define RXFIFO_THR2_FULL 0x00000060
  237. #define RXFIFO_THR2_HIGH 0x00000038
  238. #define RXFIFO_THR2_OOB 0x0000004a
  239. #define RXFIFO_THR2_NORMAL 0x00a0
  240. /* PLA_RXFIFO_CTRL2 */
  241. #define RXFIFO_THR3_FULL 0x00000078
  242. #define RXFIFO_THR3_HIGH 0x00000048
  243. #define RXFIFO_THR3_OOB 0x0000005a
  244. #define RXFIFO_THR3_NORMAL 0x0110
  245. /* PLA_TXFIFO_CTRL */
  246. #define TXFIFO_THR_NORMAL 0x00400008
  247. #define TXFIFO_THR_NORMAL2 0x01000008
  248. /* PLA_DMY_REG0 */
  249. #define ECM_ALDPS 0x0002
  250. /* PLA_FMC */
  251. #define FMC_FCR_MCU_EN 0x0001
  252. /* PLA_EEEP_CR */
  253. #define EEEP_CR_EEEP_TX 0x0002
  254. /* PLA_WDT6_CTRL */
  255. #define WDT6_SET_MODE 0x0010
  256. /* PLA_TCR0 */
  257. #define TCR0_TX_EMPTY 0x0800
  258. #define TCR0_AUTO_FIFO 0x0080
  259. /* PLA_TCR1 */
  260. #define VERSION_MASK 0x7cf0
  261. #define IFG_MASK (BIT(3) | BIT(9) | BIT(8))
  262. #define IFG_144NS BIT(9)
  263. #define IFG_96NS (BIT(9) | BIT(8))
  264. /* PLA_MTPS */
  265. #define MTPS_JUMBO (12 * 1024 / 64)
  266. #define MTPS_DEFAULT (6 * 1024 / 64)
  267. /* PLA_RSTTALLY */
  268. #define TALLY_RESET 0x0001
  269. /* PLA_CR */
  270. #define CR_RST 0x10
  271. #define CR_RE 0x08
  272. #define CR_TE 0x04
  273. /* PLA_CRWECR */
  274. #define CRWECR_NORAML 0x00
  275. #define CRWECR_CONFIG 0xc0
  276. /* PLA_OOB_CTRL */
  277. #define NOW_IS_OOB 0x80
  278. #define TXFIFO_EMPTY 0x20
  279. #define RXFIFO_EMPTY 0x10
  280. #define LINK_LIST_READY 0x02
  281. #define DIS_MCU_CLROOB 0x01
  282. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  283. /* PLA_MISC_1 */
  284. #define RXDY_GATED_EN 0x0008
  285. /* PLA_SFF_STS_7 */
  286. #define RE_INIT_LL 0x8000
  287. #define MCU_BORW_EN 0x4000
  288. /* PLA_CPCR */
  289. #define FLOW_CTRL_EN BIT(0)
  290. #define CPCR_RX_VLAN 0x0040
  291. /* PLA_CFG_WOL */
  292. #define MAGIC_EN 0x0001
  293. /* PLA_TEREDO_CFG */
  294. #define TEREDO_SEL 0x8000
  295. #define TEREDO_WAKE_MASK 0x7f00
  296. #define TEREDO_RS_EVENT_MASK 0x00fe
  297. #define OOB_TEREDO_EN 0x0001
  298. /* PLA_BDC_CR */
  299. #define ALDPS_PROXY_MODE 0x0001
  300. /* PLA_EFUSE_CMD */
  301. #define EFUSE_READ_CMD BIT(15)
  302. #define EFUSE_DATA_BIT16 BIT(7)
  303. /* PLA_CONFIG34 */
  304. #define LINK_ON_WAKE_EN 0x0010
  305. #define LINK_OFF_WAKE_EN 0x0008
  306. /* PLA_CONFIG6 */
  307. #define LANWAKE_CLR_EN BIT(0)
  308. /* PLA_USB_CFG */
  309. #define EN_XG_LIP BIT(1)
  310. #define EN_G_LIP BIT(2)
  311. /* PLA_CONFIG5 */
  312. #define BWF_EN 0x0040
  313. #define MWF_EN 0x0020
  314. #define UWF_EN 0x0010
  315. #define LAN_WAKE_EN 0x0002
  316. /* PLA_LED_FEATURE */
  317. #define LED_MODE_MASK 0x0700
  318. /* PLA_PHY_PWR */
  319. #define TX_10M_IDLE_EN 0x0080
  320. #define PFM_PWM_SWITCH 0x0040
  321. #define TEST_IO_OFF BIT(4)
  322. /* PLA_MAC_PWR_CTRL */
  323. #define D3_CLK_GATED_EN 0x00004000
  324. #define MCU_CLK_RATIO 0x07010f07
  325. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  326. #define ALDPS_SPDWN_RATIO 0x0f87
  327. /* PLA_MAC_PWR_CTRL2 */
  328. #define EEE_SPDWN_RATIO 0x8007
  329. #define MAC_CLK_SPDWN_EN BIT(15)
  330. #define EEE_SPDWN_RATIO_MASK 0xff
  331. /* PLA_MAC_PWR_CTRL3 */
  332. #define PLA_MCU_SPDWN_EN BIT(14)
  333. #define PKT_AVAIL_SPDWN_EN 0x0100
  334. #define SUSPEND_SPDWN_EN 0x0004
  335. #define U1U2_SPDWN_EN 0x0002
  336. #define L1_SPDWN_EN 0x0001
  337. /* PLA_MAC_PWR_CTRL4 */
  338. #define PWRSAVE_SPDWN_EN 0x1000
  339. #define RXDV_SPDWN_EN 0x0800
  340. #define TX10MIDLE_EN 0x0100
  341. #define IDLE_SPDWN_EN BIT(6)
  342. #define TP100_SPDWN_EN 0x0020
  343. #define TP500_SPDWN_EN 0x0010
  344. #define TP1000_SPDWN_EN 0x0008
  345. #define EEE_SPDWN_EN 0x0001
  346. /* PLA_GPHY_INTR_IMR */
  347. #define GPHY_STS_MSK 0x0001
  348. #define SPEED_DOWN_MSK 0x0002
  349. #define SPDWN_RXDV_MSK 0x0004
  350. #define SPDWN_LINKCHG_MSK 0x0008
  351. /* PLA_PHYAR */
  352. #define PHYAR_FLAG 0x80000000
  353. /* PLA_EEE_CR */
  354. #define EEE_RX_EN 0x0001
  355. #define EEE_TX_EN 0x0002
  356. /* PLA_BOOT_CTRL */
  357. #define AUTOLOAD_DONE 0x0002
  358. /* PLA_LWAKE_CTRL_REG */
  359. #define LANWAKE_PIN BIT(7)
  360. /* PLA_SUSPEND_FLAG */
  361. #define LINK_CHG_EVENT BIT(0)
  362. /* PLA_INDICATE_FALG */
  363. #define UPCOMING_RUNTIME_D3 BIT(0)
  364. /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
  365. #define DEBUG_OE BIT(0)
  366. #define DEBUG_LTSSM 0x0082
  367. /* PLA_EXTRA_STATUS */
  368. #define CUR_LINK_OK BIT(15)
  369. #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
  370. #define LINK_CHANGE_FLAG BIT(8)
  371. #define POLL_LINK_CHG BIT(0)
  372. /* PLA_GPHY_CTRL */
  373. #define GPHY_FLASH BIT(1)
  374. /* PLA_POL_GPIO_CTRL */
  375. #define DACK_DET_EN BIT(15)
  376. #define POL_GPHY_PATCH BIT(4)
  377. /* USB_USB2PHY */
  378. #define USB2PHY_SUSPEND 0x0001
  379. #define USB2PHY_L1 0x0002
  380. /* USB_SSPHYLINK1 */
  381. #define DELAY_PHY_PWR_CHG BIT(1)
  382. /* USB_SSPHYLINK2 */
  383. #define pwd_dn_scale_mask 0x3ffe
  384. #define pwd_dn_scale(x) ((x) << 1)
  385. /* USB_CSR_DUMMY1 */
  386. #define DYNAMIC_BURST 0x0001
  387. /* USB_CSR_DUMMY2 */
  388. #define EP4_FULL_FC 0x0001
  389. /* USB_DEV_STAT */
  390. #define STAT_SPEED_MASK 0x0006
  391. #define STAT_SPEED_HIGH 0x0000
  392. #define STAT_SPEED_FULL 0x0002
  393. /* USB_FW_FIX_EN0 */
  394. #define FW_FIX_SUSPEND BIT(14)
  395. /* USB_FW_FIX_EN1 */
  396. #define FW_IP_RESET_EN BIT(9)
  397. /* USB_LPM_CONFIG */
  398. #define LPM_U1U2_EN BIT(0)
  399. /* USB_TX_AGG */
  400. #define TX_AGG_MAX_THRESHOLD 0x03
  401. /* USB_RX_BUF_TH */
  402. #define RX_THR_SUPPER 0x0c350180
  403. #define RX_THR_HIGH 0x7a120180
  404. #define RX_THR_SLOW 0xffff0180
  405. #define RX_THR_B 0x00010001
  406. /* USB_TX_DMA */
  407. #define TEST_MODE_DISABLE 0x00000001
  408. #define TX_SIZE_ADJUST1 0x00000100
  409. /* USB_BMU_RESET */
  410. #define BMU_RESET_EP_IN 0x01
  411. #define BMU_RESET_EP_OUT 0x02
  412. /* USB_BMU_CONFIG */
  413. #define ACT_ODMA BIT(1)
  414. /* USB_UPT_RXDMA_OWN */
  415. #define OWN_UPDATE BIT(0)
  416. #define OWN_CLEAR BIT(1)
  417. /* USB_FW_TASK */
  418. #define FC_PATCH_TASK BIT(1)
  419. /* USB_RX_AGGR_NUM */
  420. #define RX_AGGR_NUM_MASK 0x1ff
  421. /* USB_UPS_CTRL */
  422. #define POWER_CUT 0x0100
  423. /* USB_PM_CTRL_STATUS */
  424. #define RESUME_INDICATE 0x0001
  425. /* USB_ECM_OPTION */
  426. #define BYPASS_MAC_RESET BIT(5)
  427. /* USB_CSTMR */
  428. #define FORCE_SUPER BIT(0)
  429. /* USB_MISC_2 */
  430. #define UPS_FORCE_PWR_DOWN BIT(0)
  431. /* USB_ECM_OP */
  432. #define EN_ALL_SPEED BIT(0)
  433. /* USB_GPHY_CTRL */
  434. #define GPHY_PATCH_DONE BIT(2)
  435. #define BYPASS_FLASH BIT(5)
  436. #define BACKUP_RESTRORE BIT(6)
  437. /* USB_SPEED_OPTION */
  438. #define RG_PWRDN_EN BIT(8)
  439. #define ALL_SPEED_OFF BIT(9)
  440. /* USB_FW_CTRL */
  441. #define FLOW_CTRL_PATCH_OPT BIT(1)
  442. #define AUTO_SPEEDUP BIT(3)
  443. #define FLOW_CTRL_PATCH_2 BIT(8)
  444. /* USB_FC_TIMER */
  445. #define CTRL_TIMER_EN BIT(15)
  446. /* USB_USB_CTRL */
  447. #define CDC_ECM_EN BIT(3)
  448. #define RX_AGG_DISABLE 0x0010
  449. #define RX_ZERO_EN 0x0080
  450. /* USB_U2P3_CTRL */
  451. #define U2P3_ENABLE 0x0001
  452. #define RX_DETECT8 BIT(3)
  453. /* USB_POWER_CUT */
  454. #define PWR_EN 0x0001
  455. #define PHASE2_EN 0x0008
  456. #define UPS_EN BIT(4)
  457. #define USP_PREWAKE BIT(5)
  458. /* USB_MISC_0 */
  459. #define PCUT_STATUS 0x0001
  460. /* USB_RX_EARLY_TIMEOUT */
  461. #define COALESCE_SUPER 85000U
  462. #define COALESCE_HIGH 250000U
  463. #define COALESCE_SLOW 524280U
  464. /* USB_WDT1_CTRL */
  465. #define WTD1_EN BIT(0)
  466. /* USB_WDT11_CTRL */
  467. #define TIMER11_EN 0x0001
  468. /* USB_LPM_CTRL */
  469. /* bit 4 ~ 5: fifo empty boundary */
  470. #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
  471. /* bit 2 ~ 3: LMP timer */
  472. #define LPM_TIMER_MASK 0x0c
  473. #define LPM_TIMER_500MS 0x04 /* 500 ms */
  474. #define LPM_TIMER_500US 0x0c /* 500 us */
  475. #define ROK_EXIT_LPM 0x02
  476. /* USB_AFE_CTRL2 */
  477. #define SEN_VAL_MASK 0xf800
  478. #define SEN_VAL_NORMAL 0xa000
  479. #define SEL_RXIDLE 0x0100
  480. /* USB_UPHY_XTAL */
  481. #define OOBS_POLLING BIT(8)
  482. /* USB_UPS_CFG */
  483. #define SAW_CNT_1MS_MASK 0x0fff
  484. #define MID_REVERSE BIT(5) /* RTL8156A */
  485. /* USB_UPS_FLAGS */
  486. #define UPS_FLAGS_R_TUNE BIT(0)
  487. #define UPS_FLAGS_EN_10M_CKDIV BIT(1)
  488. #define UPS_FLAGS_250M_CKDIV BIT(2)
  489. #define UPS_FLAGS_EN_ALDPS BIT(3)
  490. #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
  491. #define UPS_FLAGS_SPEED_MASK (0xf << 16)
  492. #define ups_flags_speed(x) ((x) << 16)
  493. #define UPS_FLAGS_EN_EEE BIT(20)
  494. #define UPS_FLAGS_EN_500M_EEE BIT(21)
  495. #define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
  496. #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
  497. #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
  498. #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
  499. #define UPS_FLAGS_EN_GREEN BIT(26)
  500. #define UPS_FLAGS_EN_FLOW_CTR BIT(27)
  501. enum spd_duplex {
  502. NWAY_10M_HALF,
  503. NWAY_10M_FULL,
  504. NWAY_100M_HALF,
  505. NWAY_100M_FULL,
  506. NWAY_1000M_FULL,
  507. FORCE_10M_HALF,
  508. FORCE_10M_FULL,
  509. FORCE_100M_HALF,
  510. FORCE_100M_FULL,
  511. FORCE_1000M_FULL,
  512. NWAY_2500M_FULL,
  513. };
  514. /* OCP_ALDPS_CONFIG */
  515. #define ENPWRSAVE 0x8000
  516. #define ENPDNPS 0x0200
  517. #define LINKENA 0x0100
  518. #define DIS_SDSAVE 0x0010
  519. /* OCP_PHY_STATUS */
  520. #define PHY_STAT_MASK 0x0007
  521. #define PHY_STAT_EXT_INIT 2
  522. #define PHY_STAT_LAN_ON 3
  523. #define PHY_STAT_PWRDN 5
  524. /* OCP_INTR_EN */
  525. #define INTR_SPEED_FORCE BIT(3)
  526. /* OCP_NCTL_CFG */
  527. #define PGA_RETURN_EN BIT(1)
  528. /* OCP_POWER_CFG */
  529. #define EEE_CLKDIV_EN 0x8000
  530. #define EN_ALDPS 0x0004
  531. #define EN_10M_PLLOFF 0x0001
  532. /* OCP_EEE_CONFIG1 */
  533. #define RG_TXLPI_MSK_HFDUP 0x8000
  534. #define RG_MATCLR_EN 0x4000
  535. #define EEE_10_CAP 0x2000
  536. #define EEE_NWAY_EN 0x1000
  537. #define TX_QUIET_EN 0x0200
  538. #define RX_QUIET_EN 0x0100
  539. #define sd_rise_time_mask 0x0070
  540. #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
  541. #define RG_RXLPI_MSK_HFDUP 0x0008
  542. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  543. /* OCP_EEE_CONFIG2 */
  544. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  545. #define RG_DACQUIET_EN 0x0400
  546. #define RG_LDVQUIET_EN 0x0200
  547. #define RG_CKRSEL 0x0020
  548. #define RG_EEEPRG_EN 0x0010
  549. /* OCP_EEE_CONFIG3 */
  550. #define fast_snr_mask 0xff80
  551. #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
  552. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  553. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  554. /* OCP_EEE_AR */
  555. /* bit[15:14] function */
  556. #define FUN_ADDR 0x0000
  557. #define FUN_DATA 0x4000
  558. /* bit[4:0] device addr */
  559. /* OCP_EEE_CFG */
  560. #define CTAP_SHORT_EN 0x0040
  561. #define EEE10_EN 0x0010
  562. /* OCP_DOWN_SPEED */
  563. #define EN_EEE_CMODE BIT(14)
  564. #define EN_EEE_1000 BIT(13)
  565. #define EN_EEE_100 BIT(12)
  566. #define EN_10M_CLKDIV BIT(11)
  567. #define EN_10M_BGOFF 0x0080
  568. /* OCP_10GBT_CTRL */
  569. #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */
  570. /* OCP_PHY_STATE */
  571. #define TXDIS_STATE 0x01
  572. #define ABD_STATE 0x02
  573. /* OCP_PHY_PATCH_STAT */
  574. #define PATCH_READY BIT(6)
  575. /* OCP_PHY_PATCH_CMD */
  576. #define PATCH_REQUEST BIT(4)
  577. /* OCP_PHY_LOCK */
  578. #define PATCH_LOCK BIT(0)
  579. /* OCP_ADC_CFG */
  580. #define CKADSEL_L 0x0100
  581. #define ADC_EN 0x0080
  582. #define EN_EMI_L 0x0040
  583. /* OCP_SYSCLK_CFG */
  584. #define sysclk_div_expo(x) (min(x, 5) << 8)
  585. #define clk_div_expo(x) (min(x, 5) << 4)
  586. /* SRAM_GREEN_CFG */
  587. #define GREEN_ETH_EN BIT(15)
  588. #define R_TUNE_EN BIT(11)
  589. /* SRAM_LPF_CFG */
  590. #define LPF_AUTO_TUNE 0x8000
  591. /* SRAM_10M_AMP1 */
  592. #define GDAC_IB_UPALL 0x0008
  593. /* SRAM_10M_AMP2 */
  594. #define AMP_DN 0x0200
  595. /* SRAM_IMPEDANCE */
  596. #define RX_DRIVING_MASK 0x6000
  597. /* SRAM_PHY_LOCK */
  598. #define PHY_PATCH_LOCK 0x0001
  599. /* MAC PASSTHRU */
  600. #define AD_MASK 0xfee0
  601. #define BND_MASK 0x0004
  602. #define BD_MASK 0x0001
  603. #define EFUSE 0xcfdb
  604. #define PASS_THRU_MASK 0x1
  605. #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
  606. enum rtl_register_content {
  607. _2500bps = BIT(10),
  608. _1250bps = BIT(9),
  609. _500bps = BIT(8),
  610. _tx_flow = BIT(6),
  611. _rx_flow = BIT(5),
  612. _1000bps = 0x10,
  613. _100bps = 0x08,
  614. _10bps = 0x04,
  615. LINK_STATUS = 0x02,
  616. FULL_DUP = 0x01,
  617. };
  618. #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
  619. #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
  620. #define RTL8152_MAX_TX 4
  621. #define RTL8152_MAX_RX 10
  622. #define INTBUFSIZE 2
  623. #define TX_ALIGN 4
  624. #define RX_ALIGN 8
  625. #define RTL8152_RX_MAX_PENDING 4096
  626. #define RTL8152_RXFG_HEADSZ 256
  627. #define INTR_LINK 0x0004
  628. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
  629. #define RTL8153_RMS RTL8153_MAX_PACKET
  630. #define RTL8152_TX_TIMEOUT (5 * HZ)
  631. #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
  632. #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
  633. #define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
  634. /* rtl8152 flags */
  635. enum rtl8152_flags {
  636. RTL8152_INACCESSIBLE = 0,
  637. RTL8152_SET_RX_MODE,
  638. WORK_ENABLE,
  639. RTL8152_LINK_CHG,
  640. SELECTIVE_SUSPEND,
  641. PHY_RESET,
  642. SCHEDULE_TASKLET,
  643. GREEN_ETHERNET,
  644. RX_EPROTO,
  645. IN_PRE_RESET,
  646. PROBED_WITH_NO_ERRORS,
  647. PROBE_SHOULD_RETRY,
  648. };
  649. #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB 0x721e
  650. #define DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK 0x3054
  651. #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
  652. #define DEVICE_ID_THINKPAD_USB_C_DONGLE 0x720c
  653. #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
  654. #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3 0x3062
  655. #define DEVICE_ID_THINKPAD_HYBRID_USB_C_DOCK 0xa359
  656. struct tally_counter {
  657. __le64 tx_packets;
  658. __le64 rx_packets;
  659. __le64 tx_errors;
  660. __le32 rx_errors;
  661. __le16 rx_missed;
  662. __le16 align_errors;
  663. __le32 tx_one_collision;
  664. __le32 tx_multi_collision;
  665. __le64 rx_unicast;
  666. __le64 rx_broadcast;
  667. __le32 rx_multicast;
  668. __le16 tx_aborted;
  669. __le16 tx_underrun;
  670. };
  671. struct rx_desc {
  672. __le32 opts1;
  673. #define RX_LEN_MASK 0x7fff
  674. __le32 opts2;
  675. #define RD_UDP_CS BIT(23)
  676. #define RD_TCP_CS BIT(22)
  677. #define RD_IPV6_CS BIT(20)
  678. #define RD_IPV4_CS BIT(19)
  679. __le32 opts3;
  680. #define IPF BIT(23) /* IP checksum fail */
  681. #define UDPF BIT(22) /* UDP checksum fail */
  682. #define TCPF BIT(21) /* TCP checksum fail */
  683. #define RX_VLAN_TAG BIT(16)
  684. __le32 opts4;
  685. __le32 opts5;
  686. __le32 opts6;
  687. };
  688. struct tx_desc {
  689. __le32 opts1;
  690. #define TX_FS BIT(31) /* First segment of a packet */
  691. #define TX_LS BIT(30) /* Final segment of a packet */
  692. #define GTSENDV4 BIT(28)
  693. #define GTSENDV6 BIT(27)
  694. #define GTTCPHO_SHIFT 18
  695. #define GTTCPHO_MAX 0x7fU
  696. #define TX_LEN_MAX 0x3ffffU
  697. __le32 opts2;
  698. #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
  699. #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
  700. #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
  701. #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
  702. #define MSS_SHIFT 17
  703. #define MSS_MAX 0x7ffU
  704. #define TCPHO_SHIFT 17
  705. #define TCPHO_MAX 0x7ffU
  706. #define TX_VLAN_TAG BIT(16)
  707. };
  708. struct r8152;
  709. struct rx_agg {
  710. struct list_head list, info_list;
  711. struct urb *urb;
  712. struct r8152 *context;
  713. struct page *page;
  714. void *buffer;
  715. };
  716. struct tx_agg {
  717. struct list_head list;
  718. struct urb *urb;
  719. struct r8152 *context;
  720. void *buffer;
  721. void *head;
  722. u32 skb_num;
  723. u32 skb_len;
  724. };
  725. struct r8152 {
  726. unsigned long flags;
  727. struct usb_device *udev;
  728. struct napi_struct napi;
  729. struct usb_interface *intf;
  730. struct net_device *netdev;
  731. struct urb *intr_urb;
  732. struct tx_agg tx_info[RTL8152_MAX_TX];
  733. struct list_head rx_info, rx_used;
  734. struct list_head rx_done, tx_free;
  735. struct sk_buff_head tx_queue, rx_queue;
  736. spinlock_t rx_lock, tx_lock;
  737. struct delayed_work schedule, hw_phy_work;
  738. struct mii_if_info mii;
  739. struct mutex control; /* use for hw setting */
  740. #ifdef CONFIG_PM_SLEEP
  741. struct notifier_block pm_notifier;
  742. #endif
  743. struct tasklet_struct tx_tl;
  744. struct rtl_ops {
  745. void (*init)(struct r8152 *tp);
  746. int (*enable)(struct r8152 *tp);
  747. void (*disable)(struct r8152 *tp);
  748. void (*up)(struct r8152 *tp);
  749. void (*down)(struct r8152 *tp);
  750. void (*unload)(struct r8152 *tp);
  751. int (*eee_get)(struct r8152 *tp, struct ethtool_keee *eee);
  752. int (*eee_set)(struct r8152 *tp, struct ethtool_keee *eee);
  753. bool (*in_nway)(struct r8152 *tp);
  754. void (*hw_phy_cfg)(struct r8152 *tp);
  755. void (*autosuspend_en)(struct r8152 *tp, bool enable);
  756. void (*change_mtu)(struct r8152 *tp);
  757. } rtl_ops;
  758. struct ups_info {
  759. u32 r_tune:1;
  760. u32 _10m_ckdiv:1;
  761. u32 _250m_ckdiv:1;
  762. u32 aldps:1;
  763. u32 lite_mode:2;
  764. u32 speed_duplex:4;
  765. u32 eee:1;
  766. u32 eee_lite:1;
  767. u32 eee_ckdiv:1;
  768. u32 eee_plloff_100:1;
  769. u32 eee_plloff_giga:1;
  770. u32 eee_cmod_lv:1;
  771. u32 green:1;
  772. u32 flow_control:1;
  773. u32 ctap_short_off:1;
  774. } ups_info;
  775. #define RTL_VER_SIZE 32
  776. struct rtl_fw {
  777. const char *fw_name;
  778. const struct firmware *fw;
  779. char version[RTL_VER_SIZE];
  780. int (*pre_fw)(struct r8152 *tp);
  781. int (*post_fw)(struct r8152 *tp);
  782. bool retry;
  783. } rtl_fw;
  784. atomic_t rx_count;
  785. bool eee_en;
  786. int intr_interval;
  787. u32 saved_wolopts;
  788. u32 msg_enable;
  789. u32 tx_qlen;
  790. u32 coalesce;
  791. u32 advertising;
  792. u32 rx_buf_sz;
  793. u32 rx_copybreak;
  794. u32 rx_pending;
  795. u32 fc_pause_on, fc_pause_off;
  796. unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
  797. u32 support_2500full:1;
  798. u32 lenovo_macpassthru:1;
  799. u32 dell_tb_rx_agg_bug:1;
  800. u16 ocp_base;
  801. u16 speed;
  802. u16 eee_adv;
  803. u8 *intr_buff;
  804. u8 version;
  805. u8 duplex;
  806. u8 autoneg;
  807. unsigned int reg_access_reset_count;
  808. };
  809. /**
  810. * struct fw_block - block type and total length
  811. * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
  812. * RTL_FW_USB and so on.
  813. * @length: total length of the current block.
  814. */
  815. struct fw_block {
  816. __le32 type;
  817. __le32 length;
  818. } __packed;
  819. /**
  820. * struct fw_header - header of the firmware file
  821. * @checksum: checksum of sha256 which is calculated from the whole file
  822. * except the checksum field of the file. That is, calculate sha256
  823. * from the version field to the end of the file.
  824. * @version: version of this firmware.
  825. * @blocks: the first firmware block of the file
  826. */
  827. struct fw_header {
  828. u8 checksum[32];
  829. char version[RTL_VER_SIZE];
  830. struct fw_block blocks[];
  831. } __packed;
  832. enum rtl8152_fw_flags {
  833. FW_FLAGS_USB = 0,
  834. FW_FLAGS_PLA,
  835. FW_FLAGS_START,
  836. FW_FLAGS_STOP,
  837. FW_FLAGS_NC,
  838. FW_FLAGS_NC1,
  839. FW_FLAGS_NC2,
  840. FW_FLAGS_UC2,
  841. FW_FLAGS_UC,
  842. FW_FLAGS_SPEED_UP,
  843. FW_FLAGS_VER,
  844. };
  845. enum rtl8152_fw_fixup_cmd {
  846. FW_FIXUP_AND = 0,
  847. FW_FIXUP_OR,
  848. FW_FIXUP_NOT,
  849. FW_FIXUP_XOR,
  850. };
  851. struct fw_phy_set {
  852. __le16 addr;
  853. __le16 data;
  854. } __packed;
  855. struct fw_phy_speed_up {
  856. struct fw_block blk_hdr;
  857. __le16 fw_offset;
  858. __le16 version;
  859. __le16 fw_reg;
  860. __le16 reserved;
  861. char info[];
  862. } __packed;
  863. struct fw_phy_ver {
  864. struct fw_block blk_hdr;
  865. struct fw_phy_set ver;
  866. __le32 reserved;
  867. } __packed;
  868. struct fw_phy_fixup {
  869. struct fw_block blk_hdr;
  870. struct fw_phy_set setting;
  871. __le16 bit_cmd;
  872. __le16 reserved;
  873. } __packed;
  874. struct fw_phy_union {
  875. struct fw_block blk_hdr;
  876. __le16 fw_offset;
  877. __le16 fw_reg;
  878. struct fw_phy_set pre_set[2];
  879. struct fw_phy_set bp[8];
  880. struct fw_phy_set bp_en;
  881. u8 pre_num;
  882. u8 bp_num;
  883. char info[];
  884. } __packed;
  885. /**
  886. * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
  887. * The layout of the firmware block is:
  888. * <struct fw_mac> + <info> + <firmware data>.
  889. * @blk_hdr: firmware descriptor (type, length)
  890. * @fw_offset: offset of the firmware binary data. The start address of
  891. * the data would be the address of struct fw_mac + @fw_offset.
  892. * @fw_reg: the register to load the firmware. Depends on chip.
  893. * @bp_ba_addr: the register to write break point base address. Depends on
  894. * chip.
  895. * @bp_ba_value: break point base address. Depends on chip.
  896. * @bp_en_addr: the register to write break point enabled mask. Depends
  897. * on chip.
  898. * @bp_en_value: break point enabled mask. Depends on the firmware.
  899. * @bp_start: the start register of break points. Depends on chip.
  900. * @bp_num: the break point number which needs to be set for this firmware.
  901. * Depends on the firmware.
  902. * @bp: break points. Depends on firmware.
  903. * @reserved: reserved space (unused)
  904. * @fw_ver_reg: the register to store the fw version.
  905. * @fw_ver_data: the firmware version of the current type.
  906. * @info: additional information for debugging, and is followed by the
  907. * binary data of firmware.
  908. */
  909. struct fw_mac {
  910. struct fw_block blk_hdr;
  911. __le16 fw_offset;
  912. __le16 fw_reg;
  913. __le16 bp_ba_addr;
  914. __le16 bp_ba_value;
  915. __le16 bp_en_addr;
  916. __le16 bp_en_value;
  917. __le16 bp_start;
  918. __le16 bp_num;
  919. __le16 bp[16]; /* any value determined by firmware */
  920. __le32 reserved;
  921. __le16 fw_ver_reg;
  922. u8 fw_ver_data;
  923. char info[];
  924. } __packed;
  925. /**
  926. * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
  927. * This is used to set patch key when loading the firmware of PHY.
  928. * @blk_hdr: firmware descriptor (type, length)
  929. * @key_reg: the register to write the patch key.
  930. * @key_data: patch key.
  931. * @reserved: reserved space (unused)
  932. */
  933. struct fw_phy_patch_key {
  934. struct fw_block blk_hdr;
  935. __le16 key_reg;
  936. __le16 key_data;
  937. __le32 reserved;
  938. } __packed;
  939. /**
  940. * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
  941. * The layout of the firmware block is:
  942. * <struct fw_phy_nc> + <info> + <firmware data>.
  943. * @blk_hdr: firmware descriptor (type, length)
  944. * @fw_offset: offset of the firmware binary data. The start address of
  945. * the data would be the address of struct fw_phy_nc + @fw_offset.
  946. * @fw_reg: the register to load the firmware. Depends on chip.
  947. * @ba_reg: the register to write the base address. Depends on chip.
  948. * @ba_data: base address. Depends on chip.
  949. * @patch_en_addr: the register of enabling patch mode. Depends on chip.
  950. * @patch_en_value: patch mode enabled mask. Depends on the firmware.
  951. * @mode_reg: the regitster of switching the mode.
  952. * @mode_pre: the mode needing to be set before loading the firmware.
  953. * @mode_post: the mode to be set when finishing to load the firmware.
  954. * @reserved: reserved space (unused)
  955. * @bp_start: the start register of break points. Depends on chip.
  956. * @bp_num: the break point number which needs to be set for this firmware.
  957. * Depends on the firmware.
  958. * @bp: break points. Depends on firmware.
  959. * @info: additional information for debugging, and is followed by the
  960. * binary data of firmware.
  961. */
  962. struct fw_phy_nc {
  963. struct fw_block blk_hdr;
  964. __le16 fw_offset;
  965. __le16 fw_reg;
  966. __le16 ba_reg;
  967. __le16 ba_data;
  968. __le16 patch_en_addr;
  969. __le16 patch_en_value;
  970. __le16 mode_reg;
  971. __le16 mode_pre;
  972. __le16 mode_post;
  973. __le16 reserved;
  974. __le16 bp_start;
  975. __le16 bp_num;
  976. __le16 bp[4];
  977. char info[];
  978. } __packed;
  979. enum rtl_fw_type {
  980. RTL_FW_END = 0,
  981. RTL_FW_PLA,
  982. RTL_FW_USB,
  983. RTL_FW_PHY_START,
  984. RTL_FW_PHY_STOP,
  985. RTL_FW_PHY_NC,
  986. RTL_FW_PHY_FIXUP,
  987. RTL_FW_PHY_UNION_NC,
  988. RTL_FW_PHY_UNION_NC1,
  989. RTL_FW_PHY_UNION_NC2,
  990. RTL_FW_PHY_UNION_UC2,
  991. RTL_FW_PHY_UNION_UC,
  992. RTL_FW_PHY_UNION_MISC,
  993. RTL_FW_PHY_SPEED_UP,
  994. RTL_FW_PHY_VER,
  995. };
  996. enum rtl_version {
  997. RTL_VER_UNKNOWN = 0,
  998. RTL_VER_01,
  999. RTL_VER_02,
  1000. RTL_VER_03,
  1001. RTL_VER_04,
  1002. RTL_VER_05,
  1003. RTL_VER_06,
  1004. RTL_VER_07,
  1005. RTL_VER_08,
  1006. RTL_VER_09,
  1007. RTL_TEST_01,
  1008. RTL_VER_10,
  1009. RTL_VER_11,
  1010. RTL_VER_12,
  1011. RTL_VER_13,
  1012. RTL_VER_14,
  1013. RTL_VER_15,
  1014. RTL_VER_MAX
  1015. };
  1016. enum tx_csum_stat {
  1017. TX_CSUM_SUCCESS = 0,
  1018. TX_CSUM_TSO,
  1019. TX_CSUM_NONE
  1020. };
  1021. #define RTL_ADVERTISED_10_HALF BIT(0)
  1022. #define RTL_ADVERTISED_10_FULL BIT(1)
  1023. #define RTL_ADVERTISED_100_HALF BIT(2)
  1024. #define RTL_ADVERTISED_100_FULL BIT(3)
  1025. #define RTL_ADVERTISED_1000_HALF BIT(4)
  1026. #define RTL_ADVERTISED_1000_FULL BIT(5)
  1027. #define RTL_ADVERTISED_2500_FULL BIT(6)
  1028. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  1029. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  1030. */
  1031. static const int multicast_filter_limit = 32;
  1032. static unsigned int agg_buf_sz = 16384;
  1033. #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
  1034. /* If register access fails then we block access and issue a reset. If this
  1035. * happens too many times in a row without a successful access then we stop
  1036. * trying to reset and just leave access blocked.
  1037. */
  1038. #define REGISTER_ACCESS_MAX_RESETS 3
  1039. static void rtl_set_inaccessible(struct r8152 *tp)
  1040. {
  1041. set_bit(RTL8152_INACCESSIBLE, &tp->flags);
  1042. smp_mb__after_atomic();
  1043. }
  1044. static void rtl_set_accessible(struct r8152 *tp)
  1045. {
  1046. clear_bit(RTL8152_INACCESSIBLE, &tp->flags);
  1047. smp_mb__after_atomic();
  1048. }
  1049. static
  1050. int r8152_control_msg(struct r8152 *tp, unsigned int pipe, __u8 request,
  1051. __u8 requesttype, __u16 value, __u16 index, void *data,
  1052. __u16 size, const char *msg_tag)
  1053. {
  1054. struct usb_device *udev = tp->udev;
  1055. int ret;
  1056. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1057. return -ENODEV;
  1058. ret = usb_control_msg(udev, pipe, request, requesttype,
  1059. value, index, data, size,
  1060. USB_CTRL_GET_TIMEOUT);
  1061. /* No need to issue a reset to report an error if the USB device got
  1062. * unplugged; just return immediately.
  1063. */
  1064. if (ret == -ENODEV)
  1065. return ret;
  1066. /* If the write was successful then we're done */
  1067. if (ret >= 0) {
  1068. tp->reg_access_reset_count = 0;
  1069. return ret;
  1070. }
  1071. dev_err(&udev->dev,
  1072. "Failed to %s %d bytes at %#06x/%#06x (%d)\n",
  1073. msg_tag, size, value, index, ret);
  1074. /* Block all future register access until we reset. Much of the code
  1075. * in the driver doesn't check for errors. Notably, many parts of the
  1076. * driver do a read/modify/write of a register value without
  1077. * confirming that the read succeeded. Writing back modified garbage
  1078. * like this can fully wedge the adapter, requiring a power cycle.
  1079. */
  1080. rtl_set_inaccessible(tp);
  1081. /* If probe hasn't yet finished, then we'll request a retry of the
  1082. * whole probe routine if we get any control transfer errors. We
  1083. * never have to clear this bit since we free/reallocate the whole "tp"
  1084. * structure if we retry probe.
  1085. */
  1086. if (!test_bit(PROBED_WITH_NO_ERRORS, &tp->flags)) {
  1087. set_bit(PROBE_SHOULD_RETRY, &tp->flags);
  1088. return ret;
  1089. }
  1090. /* Failing to access registers in pre-reset is not surprising since we
  1091. * wouldn't be resetting if things were behaving normally. The register
  1092. * access we do in pre-reset isn't truly mandatory--we're just reusing
  1093. * the disable() function and trying to be nice by powering the
  1094. * adapter down before resetting it. Thus, if we're in pre-reset,
  1095. * we'll return right away and not try to queue up yet another reset.
  1096. * We know the post-reset is already coming.
  1097. */
  1098. if (test_bit(IN_PRE_RESET, &tp->flags))
  1099. return ret;
  1100. if (tp->reg_access_reset_count < REGISTER_ACCESS_MAX_RESETS) {
  1101. usb_queue_reset_device(tp->intf);
  1102. tp->reg_access_reset_count++;
  1103. } else if (tp->reg_access_reset_count == REGISTER_ACCESS_MAX_RESETS) {
  1104. dev_err(&udev->dev,
  1105. "Tried to reset %d times; giving up.\n",
  1106. REGISTER_ACCESS_MAX_RESETS);
  1107. }
  1108. return ret;
  1109. }
  1110. static
  1111. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  1112. {
  1113. int ret;
  1114. void *tmp;
  1115. tmp = kmalloc(size, GFP_KERNEL);
  1116. if (!tmp)
  1117. return -ENOMEM;
  1118. ret = r8152_control_msg(tp, tp->pipe_ctrl_in,
  1119. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  1120. value, index, tmp, size, "read");
  1121. if (ret < 0)
  1122. memset(data, 0xff, size);
  1123. else
  1124. memcpy(data, tmp, size);
  1125. kfree(tmp);
  1126. return ret;
  1127. }
  1128. static
  1129. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  1130. {
  1131. int ret;
  1132. void *tmp;
  1133. tmp = kmemdup(data, size, GFP_KERNEL);
  1134. if (!tmp)
  1135. return -ENOMEM;
  1136. ret = r8152_control_msg(tp, tp->pipe_ctrl_out,
  1137. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  1138. value, index, tmp, size, "write");
  1139. kfree(tmp);
  1140. return ret;
  1141. }
  1142. static void rtl_set_unplug(struct r8152 *tp)
  1143. {
  1144. if (tp->udev->state == USB_STATE_NOTATTACHED)
  1145. rtl_set_inaccessible(tp);
  1146. }
  1147. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  1148. void *data, u16 type)
  1149. {
  1150. u16 limit = 64;
  1151. int ret = 0;
  1152. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1153. return -ENODEV;
  1154. /* both size and indix must be 4 bytes align */
  1155. if ((size & 3) || !size || (index & 3) || !data)
  1156. return -EPERM;
  1157. if ((u32)index + (u32)size > 0xffff)
  1158. return -EPERM;
  1159. while (size) {
  1160. if (size > limit) {
  1161. ret = get_registers(tp, index, type, limit, data);
  1162. if (ret < 0)
  1163. break;
  1164. index += limit;
  1165. data += limit;
  1166. size -= limit;
  1167. } else {
  1168. ret = get_registers(tp, index, type, size, data);
  1169. if (ret < 0)
  1170. break;
  1171. index += size;
  1172. data += size;
  1173. size = 0;
  1174. break;
  1175. }
  1176. }
  1177. if (ret == -ENODEV)
  1178. rtl_set_unplug(tp);
  1179. return ret;
  1180. }
  1181. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  1182. u16 size, void *data, u16 type)
  1183. {
  1184. int ret;
  1185. u16 byteen_start, byteen_end, byen;
  1186. u16 limit = 512;
  1187. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1188. return -ENODEV;
  1189. /* both size and indix must be 4 bytes align */
  1190. if ((size & 3) || !size || (index & 3) || !data)
  1191. return -EPERM;
  1192. if ((u32)index + (u32)size > 0xffff)
  1193. return -EPERM;
  1194. byteen_start = byteen & BYTE_EN_START_MASK;
  1195. byteen_end = byteen & BYTE_EN_END_MASK;
  1196. byen = byteen_start | (byteen_start << 4);
  1197. /* Split the first DWORD if the byte_en is not 0xff */
  1198. if (byen != BYTE_EN_DWORD) {
  1199. ret = set_registers(tp, index, type | byen, 4, data);
  1200. if (ret < 0)
  1201. goto error1;
  1202. index += 4;
  1203. data += 4;
  1204. size -= 4;
  1205. }
  1206. if (size) {
  1207. byen = byteen_end | (byteen_end >> 4);
  1208. /* Split the last DWORD if the byte_en is not 0xff */
  1209. if (byen != BYTE_EN_DWORD)
  1210. size -= 4;
  1211. while (size) {
  1212. if (size > limit) {
  1213. ret = set_registers(tp, index,
  1214. type | BYTE_EN_DWORD,
  1215. limit, data);
  1216. if (ret < 0)
  1217. goto error1;
  1218. index += limit;
  1219. data += limit;
  1220. size -= limit;
  1221. } else {
  1222. ret = set_registers(tp, index,
  1223. type | BYTE_EN_DWORD,
  1224. size, data);
  1225. if (ret < 0)
  1226. goto error1;
  1227. index += size;
  1228. data += size;
  1229. size = 0;
  1230. break;
  1231. }
  1232. }
  1233. /* Set the last DWORD */
  1234. if (byen != BYTE_EN_DWORD)
  1235. ret = set_registers(tp, index, type | byen, 4, data);
  1236. }
  1237. error1:
  1238. if (ret == -ENODEV)
  1239. rtl_set_unplug(tp);
  1240. return ret;
  1241. }
  1242. static inline
  1243. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  1244. {
  1245. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  1246. }
  1247. static inline
  1248. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  1249. {
  1250. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  1251. }
  1252. static inline
  1253. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  1254. {
  1255. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  1256. }
  1257. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  1258. {
  1259. __le32 data;
  1260. generic_ocp_read(tp, index, sizeof(data), &data, type);
  1261. return __le32_to_cpu(data);
  1262. }
  1263. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  1264. {
  1265. __le32 tmp = __cpu_to_le32(data);
  1266. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  1267. }
  1268. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  1269. {
  1270. u32 data;
  1271. __le32 tmp;
  1272. u16 byen = BYTE_EN_WORD;
  1273. u8 shift = index & 2;
  1274. index &= ~3;
  1275. byen <<= shift;
  1276. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
  1277. data = __le32_to_cpu(tmp);
  1278. data >>= (shift * 8);
  1279. data &= 0xffff;
  1280. return (u16)data;
  1281. }
  1282. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  1283. {
  1284. u32 mask = 0xffff;
  1285. __le32 tmp;
  1286. u16 byen = BYTE_EN_WORD;
  1287. u8 shift = index & 2;
  1288. data &= mask;
  1289. if (index & 2) {
  1290. byen <<= shift;
  1291. mask <<= (shift * 8);
  1292. data <<= (shift * 8);
  1293. index &= ~3;
  1294. }
  1295. tmp = __cpu_to_le32(data);
  1296. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  1297. }
  1298. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  1299. {
  1300. u32 data;
  1301. __le32 tmp;
  1302. u8 shift = index & 3;
  1303. index &= ~3;
  1304. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  1305. data = __le32_to_cpu(tmp);
  1306. data >>= (shift * 8);
  1307. data &= 0xff;
  1308. return (u8)data;
  1309. }
  1310. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  1311. {
  1312. u32 mask = 0xff;
  1313. __le32 tmp;
  1314. u16 byen = BYTE_EN_BYTE;
  1315. u8 shift = index & 3;
  1316. data &= mask;
  1317. if (index & 3) {
  1318. byen <<= shift;
  1319. mask <<= (shift * 8);
  1320. data <<= (shift * 8);
  1321. index &= ~3;
  1322. }
  1323. tmp = __cpu_to_le32(data);
  1324. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  1325. }
  1326. static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
  1327. {
  1328. u16 ocp_base, ocp_index;
  1329. ocp_base = addr & 0xf000;
  1330. if (ocp_base != tp->ocp_base) {
  1331. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  1332. tp->ocp_base = ocp_base;
  1333. }
  1334. ocp_index = (addr & 0x0fff) | 0xb000;
  1335. return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
  1336. }
  1337. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  1338. {
  1339. u16 ocp_base, ocp_index;
  1340. ocp_base = addr & 0xf000;
  1341. if (ocp_base != tp->ocp_base) {
  1342. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  1343. tp->ocp_base = ocp_base;
  1344. }
  1345. ocp_index = (addr & 0x0fff) | 0xb000;
  1346. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  1347. }
  1348. static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  1349. {
  1350. ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
  1351. }
  1352. static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  1353. {
  1354. return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
  1355. }
  1356. static void sram_write(struct r8152 *tp, u16 addr, u16 data)
  1357. {
  1358. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  1359. ocp_reg_write(tp, OCP_SRAM_DATA, data);
  1360. }
  1361. static u16 sram_read(struct r8152 *tp, u16 addr)
  1362. {
  1363. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  1364. return ocp_reg_read(tp, OCP_SRAM_DATA);
  1365. }
  1366. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  1367. {
  1368. struct r8152 *tp = netdev_priv(netdev);
  1369. int ret;
  1370. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1371. return -ENODEV;
  1372. if (phy_id != R8152_PHY_ID)
  1373. return -EINVAL;
  1374. ret = r8152_mdio_read(tp, reg);
  1375. return ret;
  1376. }
  1377. static
  1378. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  1379. {
  1380. struct r8152 *tp = netdev_priv(netdev);
  1381. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1382. return;
  1383. if (phy_id != R8152_PHY_ID)
  1384. return;
  1385. r8152_mdio_write(tp, reg, val);
  1386. }
  1387. static int
  1388. r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
  1389. static int
  1390. rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
  1391. u32 advertising);
  1392. static int __rtl8152_set_mac_address(struct net_device *netdev,
  1393. struct sockaddr_storage *addr,
  1394. bool in_resume)
  1395. {
  1396. struct r8152 *tp = netdev_priv(netdev);
  1397. int ret = -EADDRNOTAVAIL;
  1398. if (!is_valid_ether_addr(addr->__data))
  1399. goto out1;
  1400. if (!in_resume) {
  1401. ret = usb_autopm_get_interface(tp->intf);
  1402. if (ret < 0)
  1403. goto out1;
  1404. }
  1405. mutex_lock(&tp->control);
  1406. eth_hw_addr_set(netdev, addr->__data);
  1407. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1408. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->__data);
  1409. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1410. mutex_unlock(&tp->control);
  1411. if (!in_resume)
  1412. usb_autopm_put_interface(tp->intf);
  1413. out1:
  1414. return ret;
  1415. }
  1416. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  1417. {
  1418. return __rtl8152_set_mac_address(netdev, p, false);
  1419. }
  1420. /* Devices containing proper chips can support a persistent
  1421. * host system provided MAC address.
  1422. * Examples of this are Dell TB15 and Dell WD15 docks
  1423. */
  1424. static int vendor_mac_passthru_addr_read(struct r8152 *tp,
  1425. struct sockaddr_storage *ss)
  1426. {
  1427. acpi_status status;
  1428. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1429. union acpi_object *obj;
  1430. int ret = -EINVAL;
  1431. u32 ocp_data;
  1432. unsigned char buf[6];
  1433. char *mac_obj_name;
  1434. acpi_object_type mac_obj_type;
  1435. int mac_strlen;
  1436. if (tp->lenovo_macpassthru) {
  1437. mac_obj_name = "\\MACA";
  1438. mac_obj_type = ACPI_TYPE_STRING;
  1439. mac_strlen = 0x16;
  1440. } else {
  1441. /* test for -AD variant of RTL8153 */
  1442. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  1443. if ((ocp_data & AD_MASK) == 0x1000) {
  1444. /* test for MAC address pass-through bit */
  1445. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
  1446. if ((ocp_data & PASS_THRU_MASK) != 1) {
  1447. netif_dbg(tp, probe, tp->netdev,
  1448. "No efuse for RTL8153-AD MAC pass through\n");
  1449. return -ENODEV;
  1450. }
  1451. } else {
  1452. /* test for RTL8153-BND and RTL8153-BD */
  1453. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
  1454. if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
  1455. netif_dbg(tp, probe, tp->netdev,
  1456. "Invalid variant for MAC pass through\n");
  1457. return -ENODEV;
  1458. }
  1459. }
  1460. mac_obj_name = "\\_SB.AMAC";
  1461. mac_obj_type = ACPI_TYPE_BUFFER;
  1462. mac_strlen = 0x17;
  1463. }
  1464. /* returns _AUXMAC_#AABBCCDDEEFF# */
  1465. status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
  1466. obj = (union acpi_object *)buffer.pointer;
  1467. if (!ACPI_SUCCESS(status))
  1468. return -ENODEV;
  1469. if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
  1470. netif_warn(tp, probe, tp->netdev,
  1471. "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
  1472. obj->type, obj->string.length);
  1473. goto amacout;
  1474. }
  1475. if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
  1476. strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
  1477. netif_warn(tp, probe, tp->netdev,
  1478. "Invalid header when reading pass-thru MAC addr\n");
  1479. goto amacout;
  1480. }
  1481. ret = hex2bin(buf, obj->string.pointer + 9, 6);
  1482. if (!(ret == 0 && is_valid_ether_addr(buf))) {
  1483. netif_warn(tp, probe, tp->netdev,
  1484. "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
  1485. ret, buf);
  1486. ret = -EINVAL;
  1487. goto amacout;
  1488. }
  1489. memcpy(ss->__data, buf, 6);
  1490. tp->netdev->addr_assign_type = NET_ADDR_STOLEN;
  1491. netif_info(tp, probe, tp->netdev,
  1492. "Using pass-thru MAC addr %pM\n", ss->__data);
  1493. amacout:
  1494. kfree(obj);
  1495. return ret;
  1496. }
  1497. static int determine_ethernet_addr(struct r8152 *tp,
  1498. struct sockaddr_storage *ss)
  1499. {
  1500. struct net_device *dev = tp->netdev;
  1501. int ret;
  1502. ss->ss_family = dev->type;
  1503. ret = eth_platform_get_mac_address(&tp->udev->dev, ss->__data);
  1504. if (ret < 0) {
  1505. if (tp->version == RTL_VER_01) {
  1506. ret = pla_ocp_read(tp, PLA_IDR, 8, ss->__data);
  1507. } else {
  1508. /* if device doesn't support MAC pass through this will
  1509. * be expected to be non-zero
  1510. */
  1511. ret = vendor_mac_passthru_addr_read(tp, ss);
  1512. if (ret < 0)
  1513. ret = pla_ocp_read(tp, PLA_BACKUP, 8,
  1514. ss->__data);
  1515. }
  1516. }
  1517. if (ret < 0) {
  1518. netif_err(tp, probe, dev, "Get ether addr fail\n");
  1519. } else if (!is_valid_ether_addr(ss->__data)) {
  1520. netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
  1521. ss->__data);
  1522. eth_hw_addr_random(dev);
  1523. ether_addr_copy(ss->__data, dev->dev_addr);
  1524. netif_info(tp, probe, dev, "Random ether addr %pM\n",
  1525. ss->__data);
  1526. return 0;
  1527. }
  1528. return ret;
  1529. }
  1530. static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
  1531. {
  1532. struct net_device *dev = tp->netdev;
  1533. struct sockaddr_storage ss;
  1534. int ret;
  1535. ret = determine_ethernet_addr(tp, &ss);
  1536. if (ret < 0)
  1537. return ret;
  1538. if (tp->version == RTL_VER_01)
  1539. eth_hw_addr_set(dev, ss.__data);
  1540. else
  1541. ret = __rtl8152_set_mac_address(dev, &ss, in_resume);
  1542. return ret;
  1543. }
  1544. static void read_bulk_callback(struct urb *urb)
  1545. {
  1546. struct net_device *netdev;
  1547. int status = urb->status;
  1548. struct rx_agg *agg;
  1549. struct r8152 *tp;
  1550. unsigned long flags;
  1551. agg = urb->context;
  1552. if (!agg)
  1553. return;
  1554. tp = agg->context;
  1555. if (!tp)
  1556. return;
  1557. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1558. return;
  1559. if (!test_bit(WORK_ENABLE, &tp->flags))
  1560. return;
  1561. netdev = tp->netdev;
  1562. /* When link down, the driver would cancel all bulks. */
  1563. /* This avoid the re-submitting bulk */
  1564. if (!netif_carrier_ok(netdev))
  1565. return;
  1566. usb_mark_last_busy(tp->udev);
  1567. switch (status) {
  1568. case 0:
  1569. if (urb->actual_length < ETH_ZLEN)
  1570. break;
  1571. spin_lock_irqsave(&tp->rx_lock, flags);
  1572. list_add_tail(&agg->list, &tp->rx_done);
  1573. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1574. napi_schedule(&tp->napi);
  1575. return;
  1576. case -ESHUTDOWN:
  1577. rtl_set_unplug(tp);
  1578. netif_device_detach(tp->netdev);
  1579. return;
  1580. case -EPROTO:
  1581. urb->actual_length = 0;
  1582. spin_lock_irqsave(&tp->rx_lock, flags);
  1583. list_add_tail(&agg->list, &tp->rx_done);
  1584. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1585. set_bit(RX_EPROTO, &tp->flags);
  1586. schedule_delayed_work(&tp->schedule, 1);
  1587. return;
  1588. case -ENOENT:
  1589. return; /* the urb is in unlink state */
  1590. case -ETIME:
  1591. if (net_ratelimit())
  1592. netdev_warn(netdev, "maybe reset is needed?\n");
  1593. break;
  1594. default:
  1595. if (net_ratelimit())
  1596. netdev_warn(netdev, "Rx status %d\n", status);
  1597. break;
  1598. }
  1599. r8152_submit_rx(tp, agg, GFP_ATOMIC);
  1600. }
  1601. static void write_bulk_callback(struct urb *urb)
  1602. {
  1603. struct net_device_stats *stats;
  1604. struct net_device *netdev;
  1605. struct tx_agg *agg;
  1606. struct r8152 *tp;
  1607. unsigned long flags;
  1608. int status = urb->status;
  1609. agg = urb->context;
  1610. if (!agg)
  1611. return;
  1612. tp = agg->context;
  1613. if (!tp)
  1614. return;
  1615. netdev = tp->netdev;
  1616. stats = &netdev->stats;
  1617. if (status) {
  1618. if (net_ratelimit())
  1619. netdev_warn(netdev, "Tx status %d\n", status);
  1620. stats->tx_errors += agg->skb_num;
  1621. } else {
  1622. stats->tx_packets += agg->skb_num;
  1623. stats->tx_bytes += agg->skb_len;
  1624. }
  1625. spin_lock_irqsave(&tp->tx_lock, flags);
  1626. list_add_tail(&agg->list, &tp->tx_free);
  1627. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1628. usb_autopm_put_interface_async(tp->intf);
  1629. if (!netif_carrier_ok(netdev))
  1630. return;
  1631. if (!test_bit(WORK_ENABLE, &tp->flags))
  1632. return;
  1633. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1634. return;
  1635. if (!skb_queue_empty(&tp->tx_queue))
  1636. tasklet_schedule(&tp->tx_tl);
  1637. }
  1638. static void intr_callback(struct urb *urb)
  1639. {
  1640. struct r8152 *tp;
  1641. __le16 *d;
  1642. int status = urb->status;
  1643. int res;
  1644. tp = urb->context;
  1645. if (!tp)
  1646. return;
  1647. if (!test_bit(WORK_ENABLE, &tp->flags))
  1648. return;
  1649. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  1650. return;
  1651. switch (status) {
  1652. case 0: /* success */
  1653. break;
  1654. case -ECONNRESET: /* unlink */
  1655. case -ESHUTDOWN:
  1656. netif_device_detach(tp->netdev);
  1657. fallthrough;
  1658. case -ENOENT:
  1659. case -EPROTO:
  1660. netif_info(tp, intr, tp->netdev,
  1661. "Stop submitting intr, status %d\n", status);
  1662. return;
  1663. case -EOVERFLOW:
  1664. if (net_ratelimit())
  1665. netif_info(tp, intr, tp->netdev,
  1666. "intr status -EOVERFLOW\n");
  1667. goto resubmit;
  1668. /* -EPIPE: should clear the halt */
  1669. default:
  1670. netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
  1671. goto resubmit;
  1672. }
  1673. d = urb->transfer_buffer;
  1674. if (INTR_LINK & __le16_to_cpu(d[0])) {
  1675. if (!netif_carrier_ok(tp->netdev)) {
  1676. set_bit(RTL8152_LINK_CHG, &tp->flags);
  1677. schedule_delayed_work(&tp->schedule, 0);
  1678. }
  1679. } else {
  1680. if (netif_carrier_ok(tp->netdev)) {
  1681. netif_stop_queue(tp->netdev);
  1682. set_bit(RTL8152_LINK_CHG, &tp->flags);
  1683. schedule_delayed_work(&tp->schedule, 0);
  1684. }
  1685. }
  1686. resubmit:
  1687. res = usb_submit_urb(urb, GFP_ATOMIC);
  1688. if (res == -ENODEV) {
  1689. rtl_set_unplug(tp);
  1690. netif_device_detach(tp->netdev);
  1691. } else if (res) {
  1692. netif_err(tp, intr, tp->netdev,
  1693. "can't resubmit intr, status %d\n", res);
  1694. }
  1695. }
  1696. static inline void *rx_agg_align(void *data)
  1697. {
  1698. return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
  1699. }
  1700. static inline void *tx_agg_align(void *data)
  1701. {
  1702. return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
  1703. }
  1704. static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
  1705. {
  1706. list_del(&agg->info_list);
  1707. usb_free_urb(agg->urb);
  1708. put_page(agg->page);
  1709. kfree(agg);
  1710. atomic_dec(&tp->rx_count);
  1711. }
  1712. static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
  1713. {
  1714. struct net_device *netdev = tp->netdev;
  1715. int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  1716. unsigned int order = get_order(tp->rx_buf_sz);
  1717. struct rx_agg *rx_agg;
  1718. unsigned long flags;
  1719. rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
  1720. if (!rx_agg)
  1721. return NULL;
  1722. rx_agg->page = alloc_pages(mflags | __GFP_COMP | __GFP_NOWARN, order);
  1723. if (!rx_agg->page)
  1724. goto free_rx;
  1725. rx_agg->buffer = page_address(rx_agg->page);
  1726. rx_agg->urb = usb_alloc_urb(0, mflags);
  1727. if (!rx_agg->urb)
  1728. goto free_buf;
  1729. rx_agg->context = tp;
  1730. INIT_LIST_HEAD(&rx_agg->list);
  1731. INIT_LIST_HEAD(&rx_agg->info_list);
  1732. spin_lock_irqsave(&tp->rx_lock, flags);
  1733. list_add_tail(&rx_agg->info_list, &tp->rx_info);
  1734. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1735. atomic_inc(&tp->rx_count);
  1736. return rx_agg;
  1737. free_buf:
  1738. __free_pages(rx_agg->page, order);
  1739. free_rx:
  1740. kfree(rx_agg);
  1741. return NULL;
  1742. }
  1743. static void free_all_mem(struct r8152 *tp)
  1744. {
  1745. struct rx_agg *agg, *agg_next;
  1746. unsigned long flags;
  1747. int i;
  1748. spin_lock_irqsave(&tp->rx_lock, flags);
  1749. list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
  1750. free_rx_agg(tp, agg);
  1751. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1752. WARN_ON(atomic_read(&tp->rx_count));
  1753. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1754. usb_free_urb(tp->tx_info[i].urb);
  1755. tp->tx_info[i].urb = NULL;
  1756. kfree(tp->tx_info[i].buffer);
  1757. tp->tx_info[i].buffer = NULL;
  1758. tp->tx_info[i].head = NULL;
  1759. }
  1760. usb_free_urb(tp->intr_urb);
  1761. tp->intr_urb = NULL;
  1762. kfree(tp->intr_buff);
  1763. tp->intr_buff = NULL;
  1764. }
  1765. static int alloc_all_mem(struct r8152 *tp)
  1766. {
  1767. struct net_device *netdev = tp->netdev;
  1768. struct usb_interface *intf = tp->intf;
  1769. struct usb_host_interface *alt = intf->cur_altsetting;
  1770. struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
  1771. int node, i;
  1772. node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  1773. spin_lock_init(&tp->rx_lock);
  1774. spin_lock_init(&tp->tx_lock);
  1775. INIT_LIST_HEAD(&tp->rx_info);
  1776. INIT_LIST_HEAD(&tp->tx_free);
  1777. INIT_LIST_HEAD(&tp->rx_done);
  1778. skb_queue_head_init(&tp->tx_queue);
  1779. skb_queue_head_init(&tp->rx_queue);
  1780. atomic_set(&tp->rx_count, 0);
  1781. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1782. if (!alloc_rx_agg(tp, GFP_KERNEL))
  1783. goto err1;
  1784. }
  1785. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1786. struct urb *urb;
  1787. u8 *buf;
  1788. buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
  1789. if (!buf)
  1790. goto err1;
  1791. if (buf != tx_agg_align(buf)) {
  1792. kfree(buf);
  1793. buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
  1794. node);
  1795. if (!buf)
  1796. goto err1;
  1797. }
  1798. urb = usb_alloc_urb(0, GFP_KERNEL);
  1799. if (!urb) {
  1800. kfree(buf);
  1801. goto err1;
  1802. }
  1803. INIT_LIST_HEAD(&tp->tx_info[i].list);
  1804. tp->tx_info[i].context = tp;
  1805. tp->tx_info[i].urb = urb;
  1806. tp->tx_info[i].buffer = buf;
  1807. tp->tx_info[i].head = tx_agg_align(buf);
  1808. list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
  1809. }
  1810. tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  1811. if (!tp->intr_urb)
  1812. goto err1;
  1813. tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  1814. if (!tp->intr_buff)
  1815. goto err1;
  1816. tp->intr_interval = (int)ep_intr->desc.bInterval;
  1817. usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
  1818. tp->intr_buff, INTBUFSIZE, intr_callback,
  1819. tp, tp->intr_interval);
  1820. return 0;
  1821. err1:
  1822. free_all_mem(tp);
  1823. return -ENOMEM;
  1824. }
  1825. static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
  1826. {
  1827. struct tx_agg *agg = NULL;
  1828. unsigned long flags;
  1829. if (list_empty(&tp->tx_free))
  1830. return NULL;
  1831. spin_lock_irqsave(&tp->tx_lock, flags);
  1832. if (!list_empty(&tp->tx_free)) {
  1833. struct list_head *cursor;
  1834. cursor = tp->tx_free.next;
  1835. list_del_init(cursor);
  1836. agg = list_entry(cursor, struct tx_agg, list);
  1837. }
  1838. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1839. return agg;
  1840. }
  1841. /* r8152_csum_workaround()
  1842. * The hw limits the value of the transport offset. When the offset is out of
  1843. * range, calculate the checksum by sw.
  1844. */
  1845. static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
  1846. struct sk_buff_head *list)
  1847. {
  1848. if (skb_shinfo(skb)->gso_size) {
  1849. netdev_features_t features = tp->netdev->features;
  1850. struct sk_buff *segs, *seg, *next;
  1851. struct sk_buff_head seg_list;
  1852. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  1853. segs = skb_gso_segment(skb, features);
  1854. if (IS_ERR(segs) || !segs)
  1855. goto drop;
  1856. __skb_queue_head_init(&seg_list);
  1857. skb_list_walk_safe(segs, seg, next) {
  1858. skb_mark_not_on_list(seg);
  1859. __skb_queue_tail(&seg_list, seg);
  1860. }
  1861. skb_queue_splice(&seg_list, list);
  1862. dev_kfree_skb(skb);
  1863. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1864. if (skb_checksum_help(skb) < 0)
  1865. goto drop;
  1866. __skb_queue_head(list, skb);
  1867. } else {
  1868. struct net_device_stats *stats;
  1869. drop:
  1870. stats = &tp->netdev->stats;
  1871. stats->tx_dropped++;
  1872. dev_kfree_skb(skb);
  1873. }
  1874. }
  1875. static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
  1876. {
  1877. if (skb_vlan_tag_present(skb)) {
  1878. u32 opts2;
  1879. opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
  1880. desc->opts2 |= cpu_to_le32(opts2);
  1881. }
  1882. }
  1883. static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
  1884. {
  1885. u32 opts2 = le32_to_cpu(desc->opts2);
  1886. if (opts2 & RX_VLAN_TAG)
  1887. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1888. swab16(opts2 & 0xffff));
  1889. }
  1890. static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
  1891. struct sk_buff *skb, u32 len)
  1892. {
  1893. u32 mss = skb_shinfo(skb)->gso_size;
  1894. u32 opts1, opts2 = 0;
  1895. int ret = TX_CSUM_SUCCESS;
  1896. WARN_ON_ONCE(len > TX_LEN_MAX);
  1897. opts1 = len | TX_FS | TX_LS;
  1898. if (mss) {
  1899. u32 transport_offset = (u32)skb_transport_offset(skb);
  1900. if (transport_offset > GTTCPHO_MAX) {
  1901. netif_warn(tp, tx_err, tp->netdev,
  1902. "Invalid transport offset 0x%x for TSO\n",
  1903. transport_offset);
  1904. ret = TX_CSUM_TSO;
  1905. goto unavailable;
  1906. }
  1907. switch (vlan_get_protocol(skb)) {
  1908. case htons(ETH_P_IP):
  1909. opts1 |= GTSENDV4;
  1910. break;
  1911. case htons(ETH_P_IPV6):
  1912. if (skb_cow_head(skb, 0)) {
  1913. ret = TX_CSUM_TSO;
  1914. goto unavailable;
  1915. }
  1916. tcp_v6_gso_csum_prep(skb);
  1917. opts1 |= GTSENDV6;
  1918. break;
  1919. default:
  1920. WARN_ON_ONCE(1);
  1921. break;
  1922. }
  1923. opts1 |= transport_offset << GTTCPHO_SHIFT;
  1924. opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
  1925. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1926. u32 transport_offset = (u32)skb_transport_offset(skb);
  1927. u8 ip_protocol;
  1928. if (transport_offset > TCPHO_MAX) {
  1929. netif_warn(tp, tx_err, tp->netdev,
  1930. "Invalid transport offset 0x%x\n",
  1931. transport_offset);
  1932. ret = TX_CSUM_NONE;
  1933. goto unavailable;
  1934. }
  1935. switch (vlan_get_protocol(skb)) {
  1936. case htons(ETH_P_IP):
  1937. opts2 |= IPV4_CS;
  1938. ip_protocol = ip_hdr(skb)->protocol;
  1939. break;
  1940. case htons(ETH_P_IPV6):
  1941. opts2 |= IPV6_CS;
  1942. ip_protocol = ipv6_hdr(skb)->nexthdr;
  1943. break;
  1944. default:
  1945. ip_protocol = IPPROTO_RAW;
  1946. break;
  1947. }
  1948. if (ip_protocol == IPPROTO_TCP)
  1949. opts2 |= TCP_CS;
  1950. else if (ip_protocol == IPPROTO_UDP)
  1951. opts2 |= UDP_CS;
  1952. else
  1953. WARN_ON_ONCE(1);
  1954. opts2 |= transport_offset << TCPHO_SHIFT;
  1955. }
  1956. desc->opts2 = cpu_to_le32(opts2);
  1957. desc->opts1 = cpu_to_le32(opts1);
  1958. unavailable:
  1959. return ret;
  1960. }
  1961. static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
  1962. {
  1963. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1964. int remain, ret;
  1965. u8 *tx_data;
  1966. __skb_queue_head_init(&skb_head);
  1967. spin_lock(&tx_queue->lock);
  1968. skb_queue_splice_init(tx_queue, &skb_head);
  1969. spin_unlock(&tx_queue->lock);
  1970. tx_data = agg->head;
  1971. agg->skb_num = 0;
  1972. agg->skb_len = 0;
  1973. remain = agg_buf_sz;
  1974. while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
  1975. struct tx_desc *tx_desc;
  1976. struct sk_buff *skb;
  1977. unsigned int len;
  1978. skb = __skb_dequeue(&skb_head);
  1979. if (!skb)
  1980. break;
  1981. len = skb->len + sizeof(*tx_desc);
  1982. if (len > remain) {
  1983. __skb_queue_head(&skb_head, skb);
  1984. break;
  1985. }
  1986. tx_data = tx_agg_align(tx_data);
  1987. tx_desc = (struct tx_desc *)tx_data;
  1988. if (r8152_tx_csum(tp, tx_desc, skb, skb->len)) {
  1989. r8152_csum_workaround(tp, skb, &skb_head);
  1990. continue;
  1991. }
  1992. rtl_tx_vlan_tag(tx_desc, skb);
  1993. tx_data += sizeof(*tx_desc);
  1994. len = skb->len;
  1995. if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
  1996. struct net_device_stats *stats = &tp->netdev->stats;
  1997. stats->tx_dropped++;
  1998. dev_kfree_skb_any(skb);
  1999. tx_data -= sizeof(*tx_desc);
  2000. continue;
  2001. }
  2002. tx_data += len;
  2003. agg->skb_len += len;
  2004. agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
  2005. dev_kfree_skb_any(skb);
  2006. remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
  2007. if (tp->dell_tb_rx_agg_bug)
  2008. break;
  2009. }
  2010. if (!skb_queue_empty(&skb_head)) {
  2011. spin_lock(&tx_queue->lock);
  2012. skb_queue_splice(&skb_head, tx_queue);
  2013. spin_unlock(&tx_queue->lock);
  2014. }
  2015. netif_tx_lock(tp->netdev);
  2016. if (netif_queue_stopped(tp->netdev) &&
  2017. skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
  2018. netif_wake_queue(tp->netdev);
  2019. netif_tx_unlock(tp->netdev);
  2020. ret = usb_autopm_get_interface_async(tp->intf);
  2021. if (ret < 0)
  2022. goto out_tx_fill;
  2023. usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
  2024. agg->head, (int)(tx_data - (u8 *)agg->head),
  2025. (usb_complete_t)write_bulk_callback, agg);
  2026. ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
  2027. if (ret < 0)
  2028. usb_autopm_put_interface_async(tp->intf);
  2029. else
  2030. netif_trans_update(tp->netdev);
  2031. out_tx_fill:
  2032. return ret;
  2033. }
  2034. static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
  2035. {
  2036. u8 checksum = CHECKSUM_NONE;
  2037. u32 opts2, opts3;
  2038. if (!(tp->netdev->features & NETIF_F_RXCSUM))
  2039. goto return_result;
  2040. opts2 = le32_to_cpu(rx_desc->opts2);
  2041. opts3 = le32_to_cpu(rx_desc->opts3);
  2042. if (opts2 & RD_IPV4_CS) {
  2043. if (opts3 & IPF)
  2044. checksum = CHECKSUM_NONE;
  2045. else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
  2046. checksum = CHECKSUM_UNNECESSARY;
  2047. else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
  2048. checksum = CHECKSUM_UNNECESSARY;
  2049. } else if (opts2 & RD_IPV6_CS) {
  2050. if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
  2051. checksum = CHECKSUM_UNNECESSARY;
  2052. else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
  2053. checksum = CHECKSUM_UNNECESSARY;
  2054. }
  2055. return_result:
  2056. return checksum;
  2057. }
  2058. static inline bool rx_count_exceed(struct r8152 *tp)
  2059. {
  2060. return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
  2061. }
  2062. static inline int agg_offset(struct rx_agg *agg, void *addr)
  2063. {
  2064. return (int)(addr - agg->buffer);
  2065. }
  2066. static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
  2067. {
  2068. struct rx_agg *agg, *agg_next, *agg_free = NULL;
  2069. unsigned long flags;
  2070. spin_lock_irqsave(&tp->rx_lock, flags);
  2071. list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
  2072. if (page_count(agg->page) == 1) {
  2073. if (!agg_free) {
  2074. list_del_init(&agg->list);
  2075. agg_free = agg;
  2076. continue;
  2077. }
  2078. if (rx_count_exceed(tp)) {
  2079. list_del_init(&agg->list);
  2080. free_rx_agg(tp, agg);
  2081. }
  2082. break;
  2083. }
  2084. }
  2085. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2086. if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
  2087. agg_free = alloc_rx_agg(tp, mflags);
  2088. return agg_free;
  2089. }
  2090. static int rx_bottom(struct r8152 *tp, int budget)
  2091. {
  2092. unsigned long flags;
  2093. struct list_head *cursor, *next, rx_queue;
  2094. int ret = 0, work_done = 0;
  2095. struct napi_struct *napi = &tp->napi;
  2096. if (!skb_queue_empty(&tp->rx_queue)) {
  2097. while (work_done < budget) {
  2098. struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
  2099. struct net_device *netdev = tp->netdev;
  2100. struct net_device_stats *stats = &netdev->stats;
  2101. unsigned int pkt_len;
  2102. if (!skb)
  2103. break;
  2104. pkt_len = skb->len;
  2105. napi_gro_receive(napi, skb);
  2106. work_done++;
  2107. stats->rx_packets++;
  2108. stats->rx_bytes += pkt_len;
  2109. }
  2110. }
  2111. if (list_empty(&tp->rx_done) || work_done >= budget)
  2112. goto out1;
  2113. clear_bit(RX_EPROTO, &tp->flags);
  2114. INIT_LIST_HEAD(&rx_queue);
  2115. spin_lock_irqsave(&tp->rx_lock, flags);
  2116. list_splice_init(&tp->rx_done, &rx_queue);
  2117. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2118. list_for_each_safe(cursor, next, &rx_queue) {
  2119. struct rx_desc *rx_desc;
  2120. struct rx_agg *agg, *agg_free;
  2121. int len_used = 0;
  2122. struct urb *urb;
  2123. u8 *rx_data;
  2124. /* A bulk transfer of USB may contain may packets, so the
  2125. * total packets may more than the budget. Deal with all
  2126. * packets in current bulk transfer, and stop to handle the
  2127. * next bulk transfer until next schedule, if budget is
  2128. * exhausted.
  2129. */
  2130. if (work_done >= budget)
  2131. break;
  2132. list_del_init(cursor);
  2133. agg = list_entry(cursor, struct rx_agg, list);
  2134. urb = agg->urb;
  2135. if (urb->status != 0 || urb->actual_length < ETH_ZLEN)
  2136. goto submit;
  2137. agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
  2138. rx_desc = agg->buffer;
  2139. rx_data = agg->buffer;
  2140. len_used += sizeof(struct rx_desc);
  2141. while (urb->actual_length > len_used) {
  2142. struct net_device *netdev = tp->netdev;
  2143. struct net_device_stats *stats = &netdev->stats;
  2144. unsigned int pkt_len, rx_frag_head_sz, len;
  2145. struct sk_buff *skb;
  2146. bool use_frags;
  2147. WARN_ON_ONCE(skb_queue_len(&tp->rx_queue) >= 1000);
  2148. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  2149. if (pkt_len < ETH_ZLEN)
  2150. break;
  2151. len_used += pkt_len;
  2152. if (urb->actual_length < len_used)
  2153. break;
  2154. pkt_len -= ETH_FCS_LEN;
  2155. len = pkt_len;
  2156. rx_data += sizeof(struct rx_desc);
  2157. if (!agg_free || tp->rx_copybreak > len)
  2158. use_frags = false;
  2159. else
  2160. use_frags = true;
  2161. if (use_frags) {
  2162. /* If the budget is exhausted, the packet
  2163. * would be queued in the driver. That is,
  2164. * napi_gro_frags() wouldn't be called, so
  2165. * we couldn't use napi_get_frags().
  2166. */
  2167. if (work_done >= budget) {
  2168. rx_frag_head_sz = tp->rx_copybreak;
  2169. skb = napi_alloc_skb(napi,
  2170. rx_frag_head_sz);
  2171. } else {
  2172. rx_frag_head_sz = 0;
  2173. skb = napi_get_frags(napi);
  2174. }
  2175. } else {
  2176. rx_frag_head_sz = 0;
  2177. skb = napi_alloc_skb(napi, len);
  2178. }
  2179. if (!skb) {
  2180. stats->rx_dropped++;
  2181. goto find_next_rx;
  2182. }
  2183. skb->ip_summed = r8152_rx_csum(tp, rx_desc);
  2184. rtl_rx_vlan_tag(rx_desc, skb);
  2185. if (use_frags) {
  2186. if (rx_frag_head_sz) {
  2187. memcpy(skb->data, rx_data,
  2188. rx_frag_head_sz);
  2189. skb_put(skb, rx_frag_head_sz);
  2190. len -= rx_frag_head_sz;
  2191. rx_data += rx_frag_head_sz;
  2192. skb->protocol = eth_type_trans(skb,
  2193. netdev);
  2194. }
  2195. skb_add_rx_frag(skb, 0, agg->page,
  2196. agg_offset(agg, rx_data),
  2197. len, SKB_DATA_ALIGN(len));
  2198. get_page(agg->page);
  2199. } else {
  2200. memcpy(skb->data, rx_data, len);
  2201. skb_put(skb, len);
  2202. skb->protocol = eth_type_trans(skb, netdev);
  2203. }
  2204. if (work_done < budget) {
  2205. if (use_frags)
  2206. napi_gro_frags(napi);
  2207. else
  2208. napi_gro_receive(napi, skb);
  2209. work_done++;
  2210. stats->rx_packets++;
  2211. stats->rx_bytes += pkt_len;
  2212. } else {
  2213. __skb_queue_tail(&tp->rx_queue, skb);
  2214. }
  2215. find_next_rx:
  2216. rx_data = rx_agg_align(rx_data + len + ETH_FCS_LEN);
  2217. rx_desc = (struct rx_desc *)rx_data;
  2218. len_used = agg_offset(agg, rx_data);
  2219. len_used += sizeof(struct rx_desc);
  2220. }
  2221. WARN_ON(!agg_free && page_count(agg->page) > 1);
  2222. if (agg_free) {
  2223. spin_lock_irqsave(&tp->rx_lock, flags);
  2224. if (page_count(agg->page) == 1) {
  2225. list_add(&agg_free->list, &tp->rx_used);
  2226. } else {
  2227. list_add_tail(&agg->list, &tp->rx_used);
  2228. agg = agg_free;
  2229. urb = agg->urb;
  2230. }
  2231. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2232. }
  2233. submit:
  2234. if (!ret) {
  2235. ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  2236. } else {
  2237. urb->actual_length = 0;
  2238. list_add_tail(&agg->list, next);
  2239. }
  2240. }
  2241. /* Splice the remained list back to rx_done for next schedule */
  2242. if (!list_empty(&rx_queue)) {
  2243. spin_lock_irqsave(&tp->rx_lock, flags);
  2244. list_splice(&rx_queue, &tp->rx_done);
  2245. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2246. }
  2247. out1:
  2248. return work_done;
  2249. }
  2250. static void tx_bottom(struct r8152 *tp)
  2251. {
  2252. int res;
  2253. do {
  2254. struct net_device *netdev = tp->netdev;
  2255. struct tx_agg *agg;
  2256. if (skb_queue_empty(&tp->tx_queue))
  2257. break;
  2258. agg = r8152_get_tx_agg(tp);
  2259. if (!agg)
  2260. break;
  2261. res = r8152_tx_agg_fill(tp, agg);
  2262. if (!res)
  2263. continue;
  2264. if (res == -ENODEV) {
  2265. rtl_set_unplug(tp);
  2266. netif_device_detach(netdev);
  2267. } else {
  2268. struct net_device_stats *stats = &netdev->stats;
  2269. unsigned long flags;
  2270. netif_warn(tp, tx_err, netdev,
  2271. "failed tx_urb %d\n", res);
  2272. stats->tx_dropped += agg->skb_num;
  2273. spin_lock_irqsave(&tp->tx_lock, flags);
  2274. list_add_tail(&agg->list, &tp->tx_free);
  2275. spin_unlock_irqrestore(&tp->tx_lock, flags);
  2276. }
  2277. } while (res == 0);
  2278. }
  2279. static void bottom_half(struct tasklet_struct *t)
  2280. {
  2281. struct r8152 *tp = from_tasklet(tp, t, tx_tl);
  2282. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2283. return;
  2284. if (!test_bit(WORK_ENABLE, &tp->flags))
  2285. return;
  2286. /* When link down, the driver would cancel all bulks. */
  2287. /* This avoid the re-submitting bulk */
  2288. if (!netif_carrier_ok(tp->netdev))
  2289. return;
  2290. clear_bit(SCHEDULE_TASKLET, &tp->flags);
  2291. tx_bottom(tp);
  2292. }
  2293. static int r8152_poll(struct napi_struct *napi, int budget)
  2294. {
  2295. struct r8152 *tp = container_of(napi, struct r8152, napi);
  2296. int work_done;
  2297. if (!budget)
  2298. return 0;
  2299. work_done = rx_bottom(tp, budget);
  2300. if (work_done < budget) {
  2301. if (!napi_complete_done(napi, work_done))
  2302. goto out;
  2303. if (!list_empty(&tp->rx_done))
  2304. napi_schedule(napi);
  2305. }
  2306. out:
  2307. return work_done;
  2308. }
  2309. static
  2310. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
  2311. {
  2312. int ret;
  2313. /* The rx would be stopped, so skip submitting */
  2314. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) ||
  2315. !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
  2316. return 0;
  2317. usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
  2318. agg->buffer, tp->rx_buf_sz,
  2319. (usb_complete_t)read_bulk_callback, agg);
  2320. ret = usb_submit_urb(agg->urb, mem_flags);
  2321. if (ret == -ENODEV) {
  2322. rtl_set_unplug(tp);
  2323. netif_device_detach(tp->netdev);
  2324. } else if (ret) {
  2325. struct urb *urb = agg->urb;
  2326. unsigned long flags;
  2327. urb->actual_length = 0;
  2328. spin_lock_irqsave(&tp->rx_lock, flags);
  2329. list_add_tail(&agg->list, &tp->rx_done);
  2330. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2331. netif_err(tp, rx_err, tp->netdev,
  2332. "Couldn't submit rx[%p], ret = %d\n", agg, ret);
  2333. napi_schedule(&tp->napi);
  2334. }
  2335. return ret;
  2336. }
  2337. static void rtl_drop_queued_tx(struct r8152 *tp)
  2338. {
  2339. struct net_device_stats *stats = &tp->netdev->stats;
  2340. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  2341. struct sk_buff *skb;
  2342. if (skb_queue_empty(tx_queue))
  2343. return;
  2344. __skb_queue_head_init(&skb_head);
  2345. spin_lock_bh(&tx_queue->lock);
  2346. skb_queue_splice_init(tx_queue, &skb_head);
  2347. spin_unlock_bh(&tx_queue->lock);
  2348. while ((skb = __skb_dequeue(&skb_head))) {
  2349. dev_kfree_skb(skb);
  2350. stats->tx_dropped++;
  2351. }
  2352. }
  2353. static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  2354. {
  2355. struct r8152 *tp = netdev_priv(netdev);
  2356. netif_warn(tp, tx_err, netdev, "Tx timeout\n");
  2357. usb_queue_reset_device(tp->intf);
  2358. }
  2359. static void rtl8152_set_rx_mode(struct net_device *netdev)
  2360. {
  2361. struct r8152 *tp = netdev_priv(netdev);
  2362. if (netif_carrier_ok(netdev)) {
  2363. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  2364. schedule_delayed_work(&tp->schedule, 0);
  2365. }
  2366. }
  2367. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  2368. {
  2369. struct r8152 *tp = netdev_priv(netdev);
  2370. u32 mc_filter[2]; /* Multicast hash filter */
  2371. __le32 tmp[2];
  2372. u32 ocp_data;
  2373. netif_stop_queue(netdev);
  2374. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2375. ocp_data &= ~RCR_ACPT_ALL;
  2376. ocp_data |= RCR_AB | RCR_APM;
  2377. if (netdev->flags & IFF_PROMISC) {
  2378. /* Unconditionally log net taps. */
  2379. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  2380. ocp_data |= RCR_AM | RCR_AAP;
  2381. mc_filter[1] = 0xffffffff;
  2382. mc_filter[0] = 0xffffffff;
  2383. } else if ((netdev->flags & IFF_MULTICAST &&
  2384. netdev_mc_count(netdev) > multicast_filter_limit) ||
  2385. (netdev->flags & IFF_ALLMULTI)) {
  2386. /* Too many to filter perfectly -- accept all multicasts. */
  2387. ocp_data |= RCR_AM;
  2388. mc_filter[1] = 0xffffffff;
  2389. mc_filter[0] = 0xffffffff;
  2390. } else {
  2391. mc_filter[1] = 0;
  2392. mc_filter[0] = 0;
  2393. if (netdev->flags & IFF_MULTICAST) {
  2394. struct netdev_hw_addr *ha;
  2395. netdev_for_each_mc_addr(ha, netdev) {
  2396. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  2397. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  2398. ocp_data |= RCR_AM;
  2399. }
  2400. }
  2401. }
  2402. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  2403. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  2404. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  2405. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2406. netif_wake_queue(netdev);
  2407. }
  2408. static netdev_features_t
  2409. rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
  2410. netdev_features_t features)
  2411. {
  2412. u32 mss = skb_shinfo(skb)->gso_size;
  2413. int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
  2414. if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) &&
  2415. skb_transport_offset(skb) > max_offset)
  2416. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2417. else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
  2418. features &= ~NETIF_F_GSO_MASK;
  2419. return features;
  2420. }
  2421. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  2422. struct net_device *netdev)
  2423. {
  2424. struct r8152 *tp = netdev_priv(netdev);
  2425. skb_tx_timestamp(skb);
  2426. skb_queue_tail(&tp->tx_queue, skb);
  2427. if (!list_empty(&tp->tx_free)) {
  2428. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2429. set_bit(SCHEDULE_TASKLET, &tp->flags);
  2430. schedule_delayed_work(&tp->schedule, 0);
  2431. } else {
  2432. usb_mark_last_busy(tp->udev);
  2433. tasklet_schedule(&tp->tx_tl);
  2434. }
  2435. } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
  2436. netif_stop_queue(netdev);
  2437. }
  2438. return NETDEV_TX_OK;
  2439. }
  2440. static void r8152b_reset_packet_filter(struct r8152 *tp)
  2441. {
  2442. u32 ocp_data;
  2443. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  2444. ocp_data &= ~FMC_FCR_MCU_EN;
  2445. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  2446. ocp_data |= FMC_FCR_MCU_EN;
  2447. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  2448. }
  2449. static void rtl8152_nic_reset(struct r8152 *tp)
  2450. {
  2451. u32 ocp_data;
  2452. int i;
  2453. switch (tp->version) {
  2454. case RTL_TEST_01:
  2455. case RTL_VER_10:
  2456. case RTL_VER_11:
  2457. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  2458. ocp_data &= ~CR_TE;
  2459. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  2460. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
  2461. ocp_data &= ~BMU_RESET_EP_IN;
  2462. ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  2463. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2464. ocp_data |= CDC_ECM_EN;
  2465. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2466. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  2467. ocp_data &= ~CR_RE;
  2468. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  2469. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
  2470. ocp_data |= BMU_RESET_EP_IN;
  2471. ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  2472. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2473. ocp_data &= ~CDC_ECM_EN;
  2474. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2475. break;
  2476. default:
  2477. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  2478. for (i = 0; i < 1000; i++) {
  2479. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2480. break;
  2481. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  2482. break;
  2483. usleep_range(100, 400);
  2484. }
  2485. break;
  2486. }
  2487. }
  2488. static void set_tx_qlen(struct r8152 *tp)
  2489. {
  2490. tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
  2491. }
  2492. static inline u16 rtl8152_get_speed(struct r8152 *tp)
  2493. {
  2494. return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  2495. }
  2496. static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
  2497. {
  2498. u32 ocp_data;
  2499. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  2500. if (enable)
  2501. ocp_data |= EEEP_CR_EEEP_TX;
  2502. else
  2503. ocp_data &= ~EEEP_CR_EEEP_TX;
  2504. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  2505. }
  2506. static void rtl_set_eee_plus(struct r8152 *tp)
  2507. {
  2508. if (rtl8152_get_speed(tp) & _10bps)
  2509. rtl_eee_plus_en(tp, true);
  2510. else
  2511. rtl_eee_plus_en(tp, false);
  2512. }
  2513. static void rxdy_gated_en(struct r8152 *tp, bool enable)
  2514. {
  2515. u32 ocp_data;
  2516. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  2517. if (enable)
  2518. ocp_data |= RXDY_GATED_EN;
  2519. else
  2520. ocp_data &= ~RXDY_GATED_EN;
  2521. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  2522. }
  2523. static int rtl_start_rx(struct r8152 *tp)
  2524. {
  2525. struct rx_agg *agg, *agg_next;
  2526. struct list_head tmp_list;
  2527. unsigned long flags;
  2528. int ret = 0, i = 0;
  2529. INIT_LIST_HEAD(&tmp_list);
  2530. spin_lock_irqsave(&tp->rx_lock, flags);
  2531. INIT_LIST_HEAD(&tp->rx_done);
  2532. INIT_LIST_HEAD(&tp->rx_used);
  2533. list_splice_init(&tp->rx_info, &tmp_list);
  2534. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2535. list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
  2536. INIT_LIST_HEAD(&agg->list);
  2537. /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
  2538. if (++i > RTL8152_MAX_RX) {
  2539. spin_lock_irqsave(&tp->rx_lock, flags);
  2540. list_add_tail(&agg->list, &tp->rx_used);
  2541. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2542. } else if (unlikely(ret < 0)) {
  2543. spin_lock_irqsave(&tp->rx_lock, flags);
  2544. list_add_tail(&agg->list, &tp->rx_done);
  2545. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2546. } else {
  2547. ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
  2548. }
  2549. }
  2550. spin_lock_irqsave(&tp->rx_lock, flags);
  2551. WARN_ON(!list_empty(&tp->rx_info));
  2552. list_splice(&tmp_list, &tp->rx_info);
  2553. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2554. return ret;
  2555. }
  2556. static int rtl_stop_rx(struct r8152 *tp)
  2557. {
  2558. struct rx_agg *agg, *agg_next;
  2559. struct list_head tmp_list;
  2560. unsigned long flags;
  2561. INIT_LIST_HEAD(&tmp_list);
  2562. /* The usb_kill_urb() couldn't be used in atomic.
  2563. * Therefore, move the list of rx_info to a tmp one.
  2564. * Then, list_for_each_entry_safe could be used without
  2565. * spin lock.
  2566. */
  2567. spin_lock_irqsave(&tp->rx_lock, flags);
  2568. list_splice_init(&tp->rx_info, &tmp_list);
  2569. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2570. list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
  2571. /* At least RTL8152_MAX_RX rx_agg have the page_count being
  2572. * equal to 1, so the other ones could be freed safely.
  2573. */
  2574. if (page_count(agg->page) > 1)
  2575. free_rx_agg(tp, agg);
  2576. else
  2577. usb_kill_urb(agg->urb);
  2578. }
  2579. /* Move back the list of temp to the rx_info */
  2580. spin_lock_irqsave(&tp->rx_lock, flags);
  2581. WARN_ON(!list_empty(&tp->rx_info));
  2582. list_splice(&tmp_list, &tp->rx_info);
  2583. spin_unlock_irqrestore(&tp->rx_lock, flags);
  2584. while (!skb_queue_empty(&tp->rx_queue))
  2585. dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
  2586. return 0;
  2587. }
  2588. static void rtl_set_ifg(struct r8152 *tp, u16 speed)
  2589. {
  2590. u32 ocp_data;
  2591. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  2592. ocp_data &= ~IFG_MASK;
  2593. if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
  2594. ocp_data |= IFG_144NS;
  2595. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
  2596. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  2597. ocp_data &= ~TX10MIDLE_EN;
  2598. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  2599. } else {
  2600. ocp_data |= IFG_96NS;
  2601. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
  2602. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  2603. ocp_data |= TX10MIDLE_EN;
  2604. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  2605. }
  2606. }
  2607. static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
  2608. {
  2609. ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
  2610. OWN_UPDATE | OWN_CLEAR);
  2611. }
  2612. static int rtl_enable(struct r8152 *tp)
  2613. {
  2614. u32 ocp_data;
  2615. r8152b_reset_packet_filter(tp);
  2616. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  2617. ocp_data |= CR_RE | CR_TE;
  2618. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  2619. switch (tp->version) {
  2620. case RTL_VER_01:
  2621. case RTL_VER_02:
  2622. case RTL_VER_03:
  2623. case RTL_VER_04:
  2624. case RTL_VER_05:
  2625. case RTL_VER_06:
  2626. case RTL_VER_07:
  2627. break;
  2628. default:
  2629. r8153b_rx_agg_chg_indicate(tp);
  2630. break;
  2631. }
  2632. rxdy_gated_en(tp, false);
  2633. return 0;
  2634. }
  2635. static int rtl8152_enable(struct r8152 *tp)
  2636. {
  2637. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2638. return -ENODEV;
  2639. set_tx_qlen(tp);
  2640. rtl_set_eee_plus(tp);
  2641. return rtl_enable(tp);
  2642. }
  2643. static void r8153_set_rx_early_timeout(struct r8152 *tp)
  2644. {
  2645. u32 ocp_data = tp->coalesce / 8;
  2646. switch (tp->version) {
  2647. case RTL_VER_03:
  2648. case RTL_VER_04:
  2649. case RTL_VER_05:
  2650. case RTL_VER_06:
  2651. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
  2652. ocp_data);
  2653. break;
  2654. case RTL_VER_08:
  2655. case RTL_VER_09:
  2656. case RTL_VER_14:
  2657. /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
  2658. * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
  2659. */
  2660. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
  2661. 128 / 8);
  2662. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
  2663. ocp_data);
  2664. break;
  2665. case RTL_VER_10:
  2666. case RTL_VER_11:
  2667. case RTL_VER_12:
  2668. case RTL_VER_13:
  2669. case RTL_VER_15:
  2670. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
  2671. 640 / 8);
  2672. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
  2673. ocp_data);
  2674. break;
  2675. default:
  2676. break;
  2677. }
  2678. }
  2679. static void r8153_set_rx_early_size(struct r8152 *tp)
  2680. {
  2681. u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
  2682. switch (tp->version) {
  2683. case RTL_VER_03:
  2684. case RTL_VER_04:
  2685. case RTL_VER_05:
  2686. case RTL_VER_06:
  2687. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
  2688. ocp_data / 4);
  2689. break;
  2690. case RTL_VER_08:
  2691. case RTL_VER_09:
  2692. case RTL_VER_14:
  2693. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
  2694. ocp_data / 8);
  2695. break;
  2696. case RTL_TEST_01:
  2697. case RTL_VER_10:
  2698. case RTL_VER_11:
  2699. case RTL_VER_12:
  2700. case RTL_VER_13:
  2701. case RTL_VER_15:
  2702. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
  2703. ocp_data / 8);
  2704. break;
  2705. default:
  2706. WARN_ON_ONCE(1);
  2707. break;
  2708. }
  2709. }
  2710. static int rtl8153_enable(struct r8152 *tp)
  2711. {
  2712. u32 ocp_data;
  2713. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2714. return -ENODEV;
  2715. set_tx_qlen(tp);
  2716. rtl_set_eee_plus(tp);
  2717. r8153_set_rx_early_timeout(tp);
  2718. r8153_set_rx_early_size(tp);
  2719. rtl_set_ifg(tp, rtl8152_get_speed(tp));
  2720. switch (tp->version) {
  2721. case RTL_VER_09:
  2722. case RTL_VER_14:
  2723. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  2724. ocp_data &= ~FC_PATCH_TASK;
  2725. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  2726. usleep_range(1000, 2000);
  2727. ocp_data |= FC_PATCH_TASK;
  2728. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  2729. break;
  2730. default:
  2731. break;
  2732. }
  2733. return rtl_enable(tp);
  2734. }
  2735. static void rtl_disable(struct r8152 *tp)
  2736. {
  2737. u32 ocp_data;
  2738. int i;
  2739. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  2740. rtl_drop_queued_tx(tp);
  2741. return;
  2742. }
  2743. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2744. ocp_data &= ~RCR_ACPT_ALL;
  2745. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2746. rtl_drop_queued_tx(tp);
  2747. for (i = 0; i < RTL8152_MAX_TX; i++)
  2748. usb_kill_urb(tp->tx_info[i].urb);
  2749. rxdy_gated_en(tp, true);
  2750. for (i = 0; i < 1000; i++) {
  2751. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2752. break;
  2753. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2754. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  2755. break;
  2756. usleep_range(1000, 2000);
  2757. }
  2758. for (i = 0; i < 1000; i++) {
  2759. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  2760. break;
  2761. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  2762. break;
  2763. usleep_range(1000, 2000);
  2764. }
  2765. rtl_stop_rx(tp);
  2766. rtl8152_nic_reset(tp);
  2767. }
  2768. static void r8152_power_cut_en(struct r8152 *tp, bool enable)
  2769. {
  2770. u32 ocp_data;
  2771. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  2772. if (enable)
  2773. ocp_data |= POWER_CUT;
  2774. else
  2775. ocp_data &= ~POWER_CUT;
  2776. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  2777. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  2778. ocp_data &= ~RESUME_INDICATE;
  2779. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  2780. }
  2781. static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
  2782. {
  2783. u32 ocp_data;
  2784. switch (tp->version) {
  2785. case RTL_VER_01:
  2786. case RTL_VER_02:
  2787. case RTL_VER_03:
  2788. case RTL_VER_04:
  2789. case RTL_VER_05:
  2790. case RTL_VER_06:
  2791. case RTL_VER_07:
  2792. case RTL_VER_08:
  2793. case RTL_VER_09:
  2794. case RTL_VER_14:
  2795. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  2796. if (enable)
  2797. ocp_data |= CPCR_RX_VLAN;
  2798. else
  2799. ocp_data &= ~CPCR_RX_VLAN;
  2800. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  2801. break;
  2802. case RTL_TEST_01:
  2803. case RTL_VER_10:
  2804. case RTL_VER_11:
  2805. case RTL_VER_12:
  2806. case RTL_VER_13:
  2807. case RTL_VER_15:
  2808. default:
  2809. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
  2810. if (enable)
  2811. ocp_data |= OUTER_VLAN | INNER_VLAN;
  2812. else
  2813. ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
  2814. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
  2815. break;
  2816. }
  2817. }
  2818. static int rtl8152_set_features(struct net_device *dev,
  2819. netdev_features_t features)
  2820. {
  2821. netdev_features_t changed = features ^ dev->features;
  2822. struct r8152 *tp = netdev_priv(dev);
  2823. int ret;
  2824. ret = usb_autopm_get_interface(tp->intf);
  2825. if (ret < 0)
  2826. goto out;
  2827. mutex_lock(&tp->control);
  2828. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  2829. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2830. rtl_rx_vlan_en(tp, true);
  2831. else
  2832. rtl_rx_vlan_en(tp, false);
  2833. }
  2834. mutex_unlock(&tp->control);
  2835. usb_autopm_put_interface(tp->intf);
  2836. out:
  2837. return ret;
  2838. }
  2839. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  2840. static u32 __rtl_get_wol(struct r8152 *tp)
  2841. {
  2842. u32 ocp_data;
  2843. u32 wolopts = 0;
  2844. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  2845. if (ocp_data & LINK_ON_WAKE_EN)
  2846. wolopts |= WAKE_PHY;
  2847. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  2848. if (ocp_data & UWF_EN)
  2849. wolopts |= WAKE_UCAST;
  2850. if (ocp_data & BWF_EN)
  2851. wolopts |= WAKE_BCAST;
  2852. if (ocp_data & MWF_EN)
  2853. wolopts |= WAKE_MCAST;
  2854. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  2855. if (ocp_data & MAGIC_EN)
  2856. wolopts |= WAKE_MAGIC;
  2857. return wolopts;
  2858. }
  2859. static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
  2860. {
  2861. u32 ocp_data;
  2862. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  2863. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  2864. ocp_data &= ~LINK_ON_WAKE_EN;
  2865. if (wolopts & WAKE_PHY)
  2866. ocp_data |= LINK_ON_WAKE_EN;
  2867. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  2868. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  2869. ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
  2870. if (wolopts & WAKE_UCAST)
  2871. ocp_data |= UWF_EN;
  2872. if (wolopts & WAKE_BCAST)
  2873. ocp_data |= BWF_EN;
  2874. if (wolopts & WAKE_MCAST)
  2875. ocp_data |= MWF_EN;
  2876. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
  2877. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  2878. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  2879. ocp_data &= ~MAGIC_EN;
  2880. if (wolopts & WAKE_MAGIC)
  2881. ocp_data |= MAGIC_EN;
  2882. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  2883. if (wolopts & WAKE_ANY)
  2884. device_set_wakeup_enable(&tp->udev->dev, true);
  2885. else
  2886. device_set_wakeup_enable(&tp->udev->dev, false);
  2887. }
  2888. static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
  2889. {
  2890. u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
  2891. /* MAC clock speed down */
  2892. if (enable)
  2893. ocp_data |= MAC_CLK_SPDWN_EN;
  2894. else
  2895. ocp_data &= ~MAC_CLK_SPDWN_EN;
  2896. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
  2897. }
  2898. static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
  2899. {
  2900. u32 ocp_data;
  2901. /* MAC clock speed down */
  2902. if (enable) {
  2903. /* aldps_spdwn_ratio, tp10_spdwn_ratio */
  2904. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
  2905. 0x0403);
  2906. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
  2907. ocp_data &= ~EEE_SPDWN_RATIO_MASK;
  2908. ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
  2909. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
  2910. } else {
  2911. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
  2912. ocp_data &= ~MAC_CLK_SPDWN_EN;
  2913. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
  2914. }
  2915. }
  2916. static void r8153_u1u2en(struct r8152 *tp, bool enable)
  2917. {
  2918. u8 u1u2[8];
  2919. if (enable)
  2920. memset(u1u2, 0xff, sizeof(u1u2));
  2921. else
  2922. memset(u1u2, 0x00, sizeof(u1u2));
  2923. usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
  2924. }
  2925. static void r8153b_u1u2en(struct r8152 *tp, bool enable)
  2926. {
  2927. u32 ocp_data;
  2928. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
  2929. if (enable)
  2930. ocp_data |= LPM_U1U2_EN;
  2931. else
  2932. ocp_data &= ~LPM_U1U2_EN;
  2933. ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
  2934. }
  2935. static void r8153_u2p3en(struct r8152 *tp, bool enable)
  2936. {
  2937. u32 ocp_data;
  2938. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  2939. if (enable)
  2940. ocp_data |= U2P3_ENABLE;
  2941. else
  2942. ocp_data &= ~U2P3_ENABLE;
  2943. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  2944. }
  2945. static void r8153b_ups_flags(struct r8152 *tp)
  2946. {
  2947. u32 ups_flags = 0;
  2948. if (tp->ups_info.green)
  2949. ups_flags |= UPS_FLAGS_EN_GREEN;
  2950. if (tp->ups_info.aldps)
  2951. ups_flags |= UPS_FLAGS_EN_ALDPS;
  2952. if (tp->ups_info.eee)
  2953. ups_flags |= UPS_FLAGS_EN_EEE;
  2954. if (tp->ups_info.flow_control)
  2955. ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
  2956. if (tp->ups_info.eee_ckdiv)
  2957. ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
  2958. if (tp->ups_info.eee_cmod_lv)
  2959. ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
  2960. if (tp->ups_info.r_tune)
  2961. ups_flags |= UPS_FLAGS_R_TUNE;
  2962. if (tp->ups_info._10m_ckdiv)
  2963. ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
  2964. if (tp->ups_info.eee_plloff_100)
  2965. ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
  2966. if (tp->ups_info.eee_plloff_giga)
  2967. ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
  2968. if (tp->ups_info._250m_ckdiv)
  2969. ups_flags |= UPS_FLAGS_250M_CKDIV;
  2970. if (tp->ups_info.ctap_short_off)
  2971. ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
  2972. switch (tp->ups_info.speed_duplex) {
  2973. case NWAY_10M_HALF:
  2974. ups_flags |= ups_flags_speed(1);
  2975. break;
  2976. case NWAY_10M_FULL:
  2977. ups_flags |= ups_flags_speed(2);
  2978. break;
  2979. case NWAY_100M_HALF:
  2980. ups_flags |= ups_flags_speed(3);
  2981. break;
  2982. case NWAY_100M_FULL:
  2983. ups_flags |= ups_flags_speed(4);
  2984. break;
  2985. case NWAY_1000M_FULL:
  2986. ups_flags |= ups_flags_speed(5);
  2987. break;
  2988. case FORCE_10M_HALF:
  2989. ups_flags |= ups_flags_speed(6);
  2990. break;
  2991. case FORCE_10M_FULL:
  2992. ups_flags |= ups_flags_speed(7);
  2993. break;
  2994. case FORCE_100M_HALF:
  2995. ups_flags |= ups_flags_speed(8);
  2996. break;
  2997. case FORCE_100M_FULL:
  2998. ups_flags |= ups_flags_speed(9);
  2999. break;
  3000. default:
  3001. break;
  3002. }
  3003. ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
  3004. }
  3005. static void r8156_ups_flags(struct r8152 *tp)
  3006. {
  3007. u32 ups_flags = 0;
  3008. if (tp->ups_info.green)
  3009. ups_flags |= UPS_FLAGS_EN_GREEN;
  3010. if (tp->ups_info.aldps)
  3011. ups_flags |= UPS_FLAGS_EN_ALDPS;
  3012. if (tp->ups_info.eee)
  3013. ups_flags |= UPS_FLAGS_EN_EEE;
  3014. if (tp->ups_info.flow_control)
  3015. ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
  3016. if (tp->ups_info.eee_ckdiv)
  3017. ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
  3018. if (tp->ups_info._10m_ckdiv)
  3019. ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
  3020. if (tp->ups_info.eee_plloff_100)
  3021. ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
  3022. if (tp->ups_info.eee_plloff_giga)
  3023. ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
  3024. if (tp->ups_info._250m_ckdiv)
  3025. ups_flags |= UPS_FLAGS_250M_CKDIV;
  3026. switch (tp->ups_info.speed_duplex) {
  3027. case FORCE_10M_HALF:
  3028. ups_flags |= ups_flags_speed(0);
  3029. break;
  3030. case FORCE_10M_FULL:
  3031. ups_flags |= ups_flags_speed(1);
  3032. break;
  3033. case FORCE_100M_HALF:
  3034. ups_flags |= ups_flags_speed(2);
  3035. break;
  3036. case FORCE_100M_FULL:
  3037. ups_flags |= ups_flags_speed(3);
  3038. break;
  3039. case NWAY_10M_HALF:
  3040. ups_flags |= ups_flags_speed(4);
  3041. break;
  3042. case NWAY_10M_FULL:
  3043. ups_flags |= ups_flags_speed(5);
  3044. break;
  3045. case NWAY_100M_HALF:
  3046. ups_flags |= ups_flags_speed(6);
  3047. break;
  3048. case NWAY_100M_FULL:
  3049. ups_flags |= ups_flags_speed(7);
  3050. break;
  3051. case NWAY_1000M_FULL:
  3052. ups_flags |= ups_flags_speed(8);
  3053. break;
  3054. case NWAY_2500M_FULL:
  3055. ups_flags |= ups_flags_speed(9);
  3056. break;
  3057. default:
  3058. break;
  3059. }
  3060. switch (tp->ups_info.lite_mode) {
  3061. case 1:
  3062. ups_flags |= 0 << 5;
  3063. break;
  3064. case 2:
  3065. ups_flags |= 2 << 5;
  3066. break;
  3067. case 0:
  3068. default:
  3069. ups_flags |= 1 << 5;
  3070. break;
  3071. }
  3072. ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
  3073. }
  3074. static void rtl_green_en(struct r8152 *tp, bool enable)
  3075. {
  3076. u16 data;
  3077. data = sram_read(tp, SRAM_GREEN_CFG);
  3078. if (enable)
  3079. data |= GREEN_ETH_EN;
  3080. else
  3081. data &= ~GREEN_ETH_EN;
  3082. sram_write(tp, SRAM_GREEN_CFG, data);
  3083. tp->ups_info.green = enable;
  3084. }
  3085. static void r8153b_green_en(struct r8152 *tp, bool enable)
  3086. {
  3087. if (enable) {
  3088. sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
  3089. sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
  3090. sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
  3091. } else {
  3092. sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
  3093. sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
  3094. sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
  3095. }
  3096. rtl_green_en(tp, true);
  3097. }
  3098. static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
  3099. {
  3100. u16 data;
  3101. int i;
  3102. for (i = 0; i < 500; i++) {
  3103. data = ocp_reg_read(tp, OCP_PHY_STATUS);
  3104. data &= PHY_STAT_MASK;
  3105. if (desired) {
  3106. if (data == desired)
  3107. break;
  3108. } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
  3109. data == PHY_STAT_EXT_INIT) {
  3110. break;
  3111. }
  3112. msleep(20);
  3113. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  3114. break;
  3115. }
  3116. return data;
  3117. }
  3118. static void r8153b_ups_en(struct r8152 *tp, bool enable)
  3119. {
  3120. u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
  3121. if (enable) {
  3122. r8153b_ups_flags(tp);
  3123. ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
  3124. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3125. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3126. ocp_data |= UPS_FORCE_PWR_DOWN;
  3127. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3128. } else {
  3129. ocp_data &= ~(UPS_EN | USP_PREWAKE);
  3130. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3131. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3132. ocp_data &= ~UPS_FORCE_PWR_DOWN;
  3133. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3134. if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
  3135. int i;
  3136. for (i = 0; i < 500; i++) {
  3137. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  3138. return;
  3139. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  3140. AUTOLOAD_DONE)
  3141. break;
  3142. msleep(20);
  3143. }
  3144. tp->rtl_ops.hw_phy_cfg(tp);
  3145. rtl8152_set_speed(tp, tp->autoneg, tp->speed,
  3146. tp->duplex, tp->advertising);
  3147. }
  3148. }
  3149. }
  3150. static void r8153c_ups_en(struct r8152 *tp, bool enable)
  3151. {
  3152. u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
  3153. if (enable) {
  3154. r8153b_ups_flags(tp);
  3155. ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
  3156. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3157. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3158. ocp_data |= UPS_FORCE_PWR_DOWN;
  3159. ocp_data &= ~BIT(7);
  3160. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3161. } else {
  3162. ocp_data &= ~(UPS_EN | USP_PREWAKE);
  3163. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3164. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3165. ocp_data &= ~UPS_FORCE_PWR_DOWN;
  3166. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3167. if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
  3168. int i;
  3169. for (i = 0; i < 500; i++) {
  3170. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  3171. return;
  3172. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  3173. AUTOLOAD_DONE)
  3174. break;
  3175. msleep(20);
  3176. }
  3177. tp->rtl_ops.hw_phy_cfg(tp);
  3178. rtl8152_set_speed(tp, tp->autoneg, tp->speed,
  3179. tp->duplex, tp->advertising);
  3180. }
  3181. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  3182. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  3183. ocp_data |= BIT(8);
  3184. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  3185. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  3186. }
  3187. }
  3188. static void r8156_ups_en(struct r8152 *tp, bool enable)
  3189. {
  3190. u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
  3191. if (enable) {
  3192. r8156_ups_flags(tp);
  3193. ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
  3194. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3195. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3196. ocp_data |= UPS_FORCE_PWR_DOWN;
  3197. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3198. switch (tp->version) {
  3199. case RTL_VER_13:
  3200. case RTL_VER_15:
  3201. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
  3202. ocp_data &= ~OOBS_POLLING;
  3203. ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
  3204. break;
  3205. default:
  3206. break;
  3207. }
  3208. } else {
  3209. ocp_data &= ~(UPS_EN | USP_PREWAKE);
  3210. ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3211. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  3212. ocp_data &= ~UPS_FORCE_PWR_DOWN;
  3213. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  3214. if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
  3215. tp->rtl_ops.hw_phy_cfg(tp);
  3216. rtl8152_set_speed(tp, tp->autoneg, tp->speed,
  3217. tp->duplex, tp->advertising);
  3218. }
  3219. }
  3220. }
  3221. static void r8153_power_cut_en(struct r8152 *tp, bool enable)
  3222. {
  3223. u32 ocp_data;
  3224. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  3225. if (enable)
  3226. ocp_data |= PWR_EN | PHASE2_EN;
  3227. else
  3228. ocp_data &= ~(PWR_EN | PHASE2_EN);
  3229. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3230. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  3231. ocp_data &= ~PCUT_STATUS;
  3232. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  3233. }
  3234. static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
  3235. {
  3236. u32 ocp_data;
  3237. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  3238. if (enable)
  3239. ocp_data |= PWR_EN | PHASE2_EN;
  3240. else
  3241. ocp_data &= ~PWR_EN;
  3242. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  3243. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  3244. ocp_data &= ~PCUT_STATUS;
  3245. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  3246. }
  3247. static void r8153_queue_wake(struct r8152 *tp, bool enable)
  3248. {
  3249. u32 ocp_data;
  3250. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
  3251. if (enable)
  3252. ocp_data |= UPCOMING_RUNTIME_D3;
  3253. else
  3254. ocp_data &= ~UPCOMING_RUNTIME_D3;
  3255. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
  3256. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
  3257. ocp_data &= ~LINK_CHG_EVENT;
  3258. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
  3259. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  3260. ocp_data &= ~LINK_CHANGE_FLAG;
  3261. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  3262. }
  3263. static bool rtl_can_wakeup(struct r8152 *tp)
  3264. {
  3265. struct usb_device *udev = tp->udev;
  3266. return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
  3267. }
  3268. static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
  3269. {
  3270. if (enable) {
  3271. u32 ocp_data;
  3272. __rtl_set_wol(tp, WAKE_ANY);
  3273. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  3274. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  3275. ocp_data |= LINK_OFF_WAKE_EN;
  3276. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  3277. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  3278. } else {
  3279. u32 ocp_data;
  3280. __rtl_set_wol(tp, tp->saved_wolopts);
  3281. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  3282. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  3283. ocp_data &= ~LINK_OFF_WAKE_EN;
  3284. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  3285. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  3286. }
  3287. }
  3288. static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
  3289. {
  3290. if (enable) {
  3291. r8153_u1u2en(tp, false);
  3292. r8153_u2p3en(tp, false);
  3293. rtl_runtime_suspend_enable(tp, true);
  3294. } else {
  3295. rtl_runtime_suspend_enable(tp, false);
  3296. switch (tp->version) {
  3297. case RTL_VER_03:
  3298. case RTL_VER_04:
  3299. break;
  3300. case RTL_VER_05:
  3301. case RTL_VER_06:
  3302. default:
  3303. r8153_u2p3en(tp, true);
  3304. break;
  3305. }
  3306. r8153_u1u2en(tp, true);
  3307. }
  3308. }
  3309. static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
  3310. {
  3311. if (enable) {
  3312. r8153_queue_wake(tp, true);
  3313. r8153b_u1u2en(tp, false);
  3314. r8153_u2p3en(tp, false);
  3315. rtl_runtime_suspend_enable(tp, true);
  3316. r8153b_ups_en(tp, true);
  3317. } else {
  3318. r8153b_ups_en(tp, false);
  3319. r8153_queue_wake(tp, false);
  3320. rtl_runtime_suspend_enable(tp, false);
  3321. if (tp->udev->speed >= USB_SPEED_SUPER)
  3322. r8153b_u1u2en(tp, true);
  3323. }
  3324. }
  3325. static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
  3326. {
  3327. if (enable) {
  3328. r8153_queue_wake(tp, true);
  3329. r8153b_u1u2en(tp, false);
  3330. r8153_u2p3en(tp, false);
  3331. rtl_runtime_suspend_enable(tp, true);
  3332. r8153c_ups_en(tp, true);
  3333. } else {
  3334. r8153c_ups_en(tp, false);
  3335. r8153_queue_wake(tp, false);
  3336. rtl_runtime_suspend_enable(tp, false);
  3337. r8153b_u1u2en(tp, true);
  3338. }
  3339. }
  3340. static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
  3341. {
  3342. if (enable) {
  3343. r8153_queue_wake(tp, true);
  3344. r8153b_u1u2en(tp, false);
  3345. r8153_u2p3en(tp, false);
  3346. rtl_runtime_suspend_enable(tp, true);
  3347. } else {
  3348. r8153_queue_wake(tp, false);
  3349. rtl_runtime_suspend_enable(tp, false);
  3350. r8153_u2p3en(tp, true);
  3351. if (tp->udev->speed >= USB_SPEED_SUPER)
  3352. r8153b_u1u2en(tp, true);
  3353. }
  3354. }
  3355. static void r8153_teredo_off(struct r8152 *tp)
  3356. {
  3357. u32 ocp_data;
  3358. switch (tp->version) {
  3359. case RTL_VER_01:
  3360. case RTL_VER_02:
  3361. case RTL_VER_03:
  3362. case RTL_VER_04:
  3363. case RTL_VER_05:
  3364. case RTL_VER_06:
  3365. case RTL_VER_07:
  3366. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  3367. ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
  3368. OOB_TEREDO_EN);
  3369. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  3370. break;
  3371. case RTL_VER_08:
  3372. case RTL_VER_09:
  3373. case RTL_TEST_01:
  3374. case RTL_VER_10:
  3375. case RTL_VER_11:
  3376. case RTL_VER_12:
  3377. case RTL_VER_13:
  3378. case RTL_VER_14:
  3379. case RTL_VER_15:
  3380. default:
  3381. /* The bit 0 ~ 7 are relative with teredo settings. They are
  3382. * W1C (write 1 to clear), so set all 1 to disable it.
  3383. */
  3384. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
  3385. break;
  3386. }
  3387. ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
  3388. ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
  3389. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
  3390. }
  3391. static void rtl_reset_bmu(struct r8152 *tp)
  3392. {
  3393. u32 ocp_data;
  3394. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
  3395. ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
  3396. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  3397. ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
  3398. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  3399. }
  3400. /* Clear the bp to stop the firmware before loading a new one */
  3401. static void rtl_clear_bp(struct r8152 *tp, u16 type)
  3402. {
  3403. u16 bp[16] = {0};
  3404. u16 bp_num;
  3405. switch (tp->version) {
  3406. case RTL_VER_08:
  3407. case RTL_VER_09:
  3408. case RTL_VER_10:
  3409. case RTL_VER_11:
  3410. case RTL_VER_12:
  3411. case RTL_VER_13:
  3412. case RTL_VER_15:
  3413. if (type == MCU_TYPE_USB) {
  3414. ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
  3415. bp_num = 16;
  3416. break;
  3417. }
  3418. fallthrough;
  3419. case RTL_VER_03:
  3420. case RTL_VER_04:
  3421. case RTL_VER_05:
  3422. case RTL_VER_06:
  3423. ocp_write_byte(tp, type, PLA_BP_EN, 0);
  3424. fallthrough;
  3425. case RTL_VER_01:
  3426. case RTL_VER_02:
  3427. case RTL_VER_07:
  3428. bp_num = 8;
  3429. break;
  3430. case RTL_VER_14:
  3431. default:
  3432. ocp_write_word(tp, type, USB_BP2_EN, 0);
  3433. bp_num = 16;
  3434. break;
  3435. }
  3436. generic_ocp_write(tp, PLA_BP_0, BYTE_EN_DWORD, bp_num << 1, bp, type);
  3437. /* wait 3 ms to make sure the firmware is stopped */
  3438. usleep_range(3000, 6000);
  3439. ocp_write_word(tp, type, PLA_BP_BA, 0);
  3440. }
  3441. static inline void rtl_reset_ocp_base(struct r8152 *tp)
  3442. {
  3443. tp->ocp_base = -1;
  3444. }
  3445. static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
  3446. {
  3447. u16 data, check;
  3448. int i;
  3449. data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
  3450. if (request) {
  3451. data |= PATCH_REQUEST;
  3452. check = 0;
  3453. } else {
  3454. data &= ~PATCH_REQUEST;
  3455. check = PATCH_READY;
  3456. }
  3457. ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
  3458. for (i = 0; wait && i < 5000; i++) {
  3459. u32 ocp_data;
  3460. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  3461. return -ENODEV;
  3462. usleep_range(1000, 2000);
  3463. ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
  3464. if ((ocp_data & PATCH_READY) ^ check)
  3465. break;
  3466. }
  3467. if (request && wait &&
  3468. !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
  3469. dev_err(&tp->intf->dev, "PHY patch request fail\n");
  3470. rtl_phy_patch_request(tp, false, false);
  3471. return -ETIME;
  3472. } else {
  3473. return 0;
  3474. }
  3475. }
  3476. static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
  3477. {
  3478. if (patch_key && key_addr) {
  3479. sram_write(tp, key_addr, patch_key);
  3480. sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
  3481. } else if (key_addr) {
  3482. u16 data;
  3483. sram_write(tp, 0x0000, 0x0000);
  3484. data = ocp_reg_read(tp, OCP_PHY_LOCK);
  3485. data &= ~PATCH_LOCK;
  3486. ocp_reg_write(tp, OCP_PHY_LOCK, data);
  3487. sram_write(tp, key_addr, 0x0000);
  3488. } else {
  3489. WARN_ON_ONCE(1);
  3490. }
  3491. }
  3492. static int
  3493. rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
  3494. {
  3495. if (rtl_phy_patch_request(tp, true, wait))
  3496. return -ETIME;
  3497. rtl_patch_key_set(tp, key_addr, patch_key);
  3498. return 0;
  3499. }
  3500. static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
  3501. {
  3502. rtl_patch_key_set(tp, key_addr, 0);
  3503. rtl_phy_patch_request(tp, false, wait);
  3504. return 0;
  3505. }
  3506. static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
  3507. {
  3508. u16 fw_offset;
  3509. u32 length;
  3510. bool rc = false;
  3511. switch (tp->version) {
  3512. case RTL_VER_01:
  3513. case RTL_VER_02:
  3514. case RTL_VER_03:
  3515. case RTL_VER_04:
  3516. case RTL_VER_05:
  3517. case RTL_VER_06:
  3518. case RTL_VER_07:
  3519. case RTL_VER_08:
  3520. case RTL_VER_09:
  3521. case RTL_VER_10:
  3522. case RTL_VER_11:
  3523. case RTL_VER_12:
  3524. case RTL_VER_14:
  3525. goto out;
  3526. case RTL_VER_13:
  3527. case RTL_VER_15:
  3528. default:
  3529. break;
  3530. }
  3531. fw_offset = __le16_to_cpu(phy->fw_offset);
  3532. length = __le32_to_cpu(phy->blk_hdr.length);
  3533. if (fw_offset < sizeof(*phy) || length <= fw_offset) {
  3534. dev_err(&tp->intf->dev, "invalid fw_offset\n");
  3535. goto out;
  3536. }
  3537. length -= fw_offset;
  3538. if (length & 3) {
  3539. dev_err(&tp->intf->dev, "invalid block length\n");
  3540. goto out;
  3541. }
  3542. if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
  3543. dev_err(&tp->intf->dev, "invalid register to load firmware\n");
  3544. goto out;
  3545. }
  3546. rc = true;
  3547. out:
  3548. return rc;
  3549. }
  3550. static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
  3551. {
  3552. bool rc = false;
  3553. switch (tp->version) {
  3554. case RTL_VER_10:
  3555. case RTL_VER_11:
  3556. case RTL_VER_12:
  3557. case RTL_VER_13:
  3558. case RTL_VER_15:
  3559. break;
  3560. default:
  3561. goto out;
  3562. }
  3563. if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
  3564. dev_err(&tp->intf->dev, "invalid block length\n");
  3565. goto out;
  3566. }
  3567. if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
  3568. dev_err(&tp->intf->dev, "invalid phy ver addr\n");
  3569. goto out;
  3570. }
  3571. rc = true;
  3572. out:
  3573. return rc;
  3574. }
  3575. static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
  3576. {
  3577. bool rc = false;
  3578. switch (tp->version) {
  3579. case RTL_VER_10:
  3580. case RTL_VER_11:
  3581. case RTL_VER_12:
  3582. case RTL_VER_13:
  3583. case RTL_VER_15:
  3584. break;
  3585. default:
  3586. goto out;
  3587. }
  3588. if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
  3589. dev_err(&tp->intf->dev, "invalid block length\n");
  3590. goto out;
  3591. }
  3592. if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
  3593. __le16_to_cpu(fix->setting.data) != BIT(7)) {
  3594. dev_err(&tp->intf->dev, "invalid phy fixup\n");
  3595. goto out;
  3596. }
  3597. rc = true;
  3598. out:
  3599. return rc;
  3600. }
  3601. static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
  3602. {
  3603. u16 fw_offset;
  3604. u32 length;
  3605. bool rc = false;
  3606. switch (tp->version) {
  3607. case RTL_VER_10:
  3608. case RTL_VER_11:
  3609. case RTL_VER_12:
  3610. case RTL_VER_13:
  3611. case RTL_VER_15:
  3612. break;
  3613. default:
  3614. goto out;
  3615. }
  3616. fw_offset = __le16_to_cpu(phy->fw_offset);
  3617. length = __le32_to_cpu(phy->blk_hdr.length);
  3618. if (fw_offset < sizeof(*phy) || length <= fw_offset) {
  3619. dev_err(&tp->intf->dev, "invalid fw_offset\n");
  3620. goto out;
  3621. }
  3622. length -= fw_offset;
  3623. if (length & 1) {
  3624. dev_err(&tp->intf->dev, "invalid block length\n");
  3625. goto out;
  3626. }
  3627. if (phy->pre_num > 2) {
  3628. dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
  3629. goto out;
  3630. }
  3631. if (phy->bp_num > 8) {
  3632. dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
  3633. goto out;
  3634. }
  3635. rc = true;
  3636. out:
  3637. return rc;
  3638. }
  3639. static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
  3640. {
  3641. u32 length;
  3642. u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
  3643. bool rc = false;
  3644. switch (tp->version) {
  3645. case RTL_VER_04:
  3646. case RTL_VER_05:
  3647. case RTL_VER_06:
  3648. fw_reg = 0xa014;
  3649. ba_reg = 0xa012;
  3650. patch_en_addr = 0xa01a;
  3651. mode_reg = 0xb820;
  3652. bp_start = 0xa000;
  3653. break;
  3654. default:
  3655. goto out;
  3656. }
  3657. fw_offset = __le16_to_cpu(phy->fw_offset);
  3658. if (fw_offset < sizeof(*phy)) {
  3659. dev_err(&tp->intf->dev, "fw_offset too small\n");
  3660. goto out;
  3661. }
  3662. length = __le32_to_cpu(phy->blk_hdr.length);
  3663. if (length < fw_offset) {
  3664. dev_err(&tp->intf->dev, "invalid fw_offset\n");
  3665. goto out;
  3666. }
  3667. length -= __le16_to_cpu(phy->fw_offset);
  3668. if (!length || (length & 1)) {
  3669. dev_err(&tp->intf->dev, "invalid block length\n");
  3670. goto out;
  3671. }
  3672. if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
  3673. dev_err(&tp->intf->dev, "invalid register to load firmware\n");
  3674. goto out;
  3675. }
  3676. if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
  3677. dev_err(&tp->intf->dev, "invalid base address register\n");
  3678. goto out;
  3679. }
  3680. if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
  3681. dev_err(&tp->intf->dev,
  3682. "invalid patch mode enabled register\n");
  3683. goto out;
  3684. }
  3685. if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
  3686. dev_err(&tp->intf->dev,
  3687. "invalid register to switch the mode\n");
  3688. goto out;
  3689. }
  3690. if (__le16_to_cpu(phy->bp_start) != bp_start) {
  3691. dev_err(&tp->intf->dev,
  3692. "invalid start register of break point\n");
  3693. goto out;
  3694. }
  3695. if (__le16_to_cpu(phy->bp_num) > 4) {
  3696. dev_err(&tp->intf->dev, "invalid break point number\n");
  3697. goto out;
  3698. }
  3699. rc = true;
  3700. out:
  3701. return rc;
  3702. }
  3703. static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
  3704. {
  3705. u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
  3706. bool rc = false;
  3707. u32 length, type;
  3708. int i, max_bp;
  3709. type = __le32_to_cpu(mac->blk_hdr.type);
  3710. if (type == RTL_FW_PLA) {
  3711. switch (tp->version) {
  3712. case RTL_VER_01:
  3713. case RTL_VER_02:
  3714. case RTL_VER_07:
  3715. fw_reg = 0xf800;
  3716. bp_ba_addr = PLA_BP_BA;
  3717. bp_en_addr = 0;
  3718. bp_start = PLA_BP_0;
  3719. max_bp = 8;
  3720. break;
  3721. case RTL_VER_03:
  3722. case RTL_VER_04:
  3723. case RTL_VER_05:
  3724. case RTL_VER_06:
  3725. case RTL_VER_08:
  3726. case RTL_VER_09:
  3727. case RTL_VER_11:
  3728. case RTL_VER_12:
  3729. case RTL_VER_13:
  3730. case RTL_VER_15:
  3731. fw_reg = 0xf800;
  3732. bp_ba_addr = PLA_BP_BA;
  3733. bp_en_addr = PLA_BP_EN;
  3734. bp_start = PLA_BP_0;
  3735. max_bp = 8;
  3736. break;
  3737. case RTL_VER_14:
  3738. fw_reg = 0xf800;
  3739. bp_ba_addr = PLA_BP_BA;
  3740. bp_en_addr = USB_BP2_EN;
  3741. bp_start = PLA_BP_0;
  3742. max_bp = 16;
  3743. break;
  3744. default:
  3745. goto out;
  3746. }
  3747. } else if (type == RTL_FW_USB) {
  3748. switch (tp->version) {
  3749. case RTL_VER_03:
  3750. case RTL_VER_04:
  3751. case RTL_VER_05:
  3752. case RTL_VER_06:
  3753. fw_reg = 0xf800;
  3754. bp_ba_addr = USB_BP_BA;
  3755. bp_en_addr = USB_BP_EN;
  3756. bp_start = USB_BP_0;
  3757. max_bp = 8;
  3758. break;
  3759. case RTL_VER_08:
  3760. case RTL_VER_09:
  3761. case RTL_VER_11:
  3762. case RTL_VER_12:
  3763. case RTL_VER_13:
  3764. case RTL_VER_14:
  3765. case RTL_VER_15:
  3766. fw_reg = 0xe600;
  3767. bp_ba_addr = USB_BP_BA;
  3768. bp_en_addr = USB_BP2_EN;
  3769. bp_start = USB_BP_0;
  3770. max_bp = 16;
  3771. break;
  3772. case RTL_VER_01:
  3773. case RTL_VER_02:
  3774. case RTL_VER_07:
  3775. default:
  3776. goto out;
  3777. }
  3778. } else {
  3779. goto out;
  3780. }
  3781. fw_offset = __le16_to_cpu(mac->fw_offset);
  3782. if (fw_offset < sizeof(*mac)) {
  3783. dev_err(&tp->intf->dev, "fw_offset too small\n");
  3784. goto out;
  3785. }
  3786. length = __le32_to_cpu(mac->blk_hdr.length);
  3787. if (length < fw_offset) {
  3788. dev_err(&tp->intf->dev, "invalid fw_offset\n");
  3789. goto out;
  3790. }
  3791. length -= fw_offset;
  3792. if (length < 4 || (length & 3)) {
  3793. dev_err(&tp->intf->dev, "invalid block length\n");
  3794. goto out;
  3795. }
  3796. if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
  3797. dev_err(&tp->intf->dev, "invalid register to load firmware\n");
  3798. goto out;
  3799. }
  3800. if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
  3801. dev_err(&tp->intf->dev, "invalid base address register\n");
  3802. goto out;
  3803. }
  3804. if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
  3805. dev_err(&tp->intf->dev, "invalid enabled mask register\n");
  3806. goto out;
  3807. }
  3808. if (__le16_to_cpu(mac->bp_start) != bp_start) {
  3809. dev_err(&tp->intf->dev,
  3810. "invalid start register of break point\n");
  3811. goto out;
  3812. }
  3813. if (__le16_to_cpu(mac->bp_num) > max_bp) {
  3814. dev_err(&tp->intf->dev, "invalid break point number\n");
  3815. goto out;
  3816. }
  3817. for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
  3818. if (mac->bp[i]) {
  3819. dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
  3820. goto out;
  3821. }
  3822. }
  3823. rc = true;
  3824. out:
  3825. return rc;
  3826. }
  3827. /* Verify the checksum for the firmware file. It is calculated from the version
  3828. * field to the end of the file. Compare the result with the checksum field to
  3829. * make sure the file is correct.
  3830. */
  3831. static long rtl8152_fw_verify_checksum(struct r8152 *tp,
  3832. struct fw_header *fw_hdr, size_t size)
  3833. {
  3834. u8 checksum[sizeof(fw_hdr->checksum)];
  3835. BUILD_BUG_ON(sizeof(checksum) != SHA256_DIGEST_SIZE);
  3836. sha256(fw_hdr->version, size - sizeof(checksum), checksum);
  3837. if (memcmp(fw_hdr->checksum, checksum, sizeof(checksum))) {
  3838. dev_err(&tp->intf->dev, "checksum fail\n");
  3839. return -EFAULT;
  3840. }
  3841. return 0;
  3842. }
  3843. static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
  3844. {
  3845. const struct firmware *fw = rtl_fw->fw;
  3846. struct fw_header *fw_hdr = (struct fw_header *)fw->data;
  3847. unsigned long fw_flags = 0;
  3848. long ret = -EFAULT;
  3849. int i;
  3850. if (fw->size < sizeof(*fw_hdr)) {
  3851. dev_err(&tp->intf->dev, "file too small\n");
  3852. goto fail;
  3853. }
  3854. ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
  3855. if (ret)
  3856. goto fail;
  3857. ret = -EFAULT;
  3858. for (i = sizeof(*fw_hdr); i < fw->size;) {
  3859. struct fw_block *block = (struct fw_block *)&fw->data[i];
  3860. u32 type;
  3861. if ((i + sizeof(*block)) > fw->size)
  3862. goto fail;
  3863. type = __le32_to_cpu(block->type);
  3864. switch (type) {
  3865. case RTL_FW_END:
  3866. if (__le32_to_cpu(block->length) != sizeof(*block))
  3867. goto fail;
  3868. goto fw_end;
  3869. case RTL_FW_PLA:
  3870. if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
  3871. dev_err(&tp->intf->dev,
  3872. "multiple PLA firmware encountered");
  3873. goto fail;
  3874. }
  3875. if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
  3876. dev_err(&tp->intf->dev,
  3877. "check PLA firmware failed\n");
  3878. goto fail;
  3879. }
  3880. __set_bit(FW_FLAGS_PLA, &fw_flags);
  3881. break;
  3882. case RTL_FW_USB:
  3883. if (test_bit(FW_FLAGS_USB, &fw_flags)) {
  3884. dev_err(&tp->intf->dev,
  3885. "multiple USB firmware encountered");
  3886. goto fail;
  3887. }
  3888. if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
  3889. dev_err(&tp->intf->dev,
  3890. "check USB firmware failed\n");
  3891. goto fail;
  3892. }
  3893. __set_bit(FW_FLAGS_USB, &fw_flags);
  3894. break;
  3895. case RTL_FW_PHY_START:
  3896. if (test_bit(FW_FLAGS_START, &fw_flags) ||
  3897. test_bit(FW_FLAGS_NC, &fw_flags) ||
  3898. test_bit(FW_FLAGS_NC1, &fw_flags) ||
  3899. test_bit(FW_FLAGS_NC2, &fw_flags) ||
  3900. test_bit(FW_FLAGS_UC2, &fw_flags) ||
  3901. test_bit(FW_FLAGS_UC, &fw_flags) ||
  3902. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  3903. dev_err(&tp->intf->dev,
  3904. "check PHY_START fail\n");
  3905. goto fail;
  3906. }
  3907. if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
  3908. dev_err(&tp->intf->dev,
  3909. "Invalid length for PHY_START\n");
  3910. goto fail;
  3911. }
  3912. __set_bit(FW_FLAGS_START, &fw_flags);
  3913. break;
  3914. case RTL_FW_PHY_STOP:
  3915. if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
  3916. !test_bit(FW_FLAGS_START, &fw_flags)) {
  3917. dev_err(&tp->intf->dev,
  3918. "Check PHY_STOP fail\n");
  3919. goto fail;
  3920. }
  3921. if (__le32_to_cpu(block->length) != sizeof(*block)) {
  3922. dev_err(&tp->intf->dev,
  3923. "Invalid length for PHY_STOP\n");
  3924. goto fail;
  3925. }
  3926. __set_bit(FW_FLAGS_STOP, &fw_flags);
  3927. break;
  3928. case RTL_FW_PHY_NC:
  3929. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  3930. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  3931. dev_err(&tp->intf->dev,
  3932. "check PHY_NC fail\n");
  3933. goto fail;
  3934. }
  3935. if (test_bit(FW_FLAGS_NC, &fw_flags)) {
  3936. dev_err(&tp->intf->dev,
  3937. "multiple PHY NC encountered\n");
  3938. goto fail;
  3939. }
  3940. if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
  3941. dev_err(&tp->intf->dev,
  3942. "check PHY NC firmware failed\n");
  3943. goto fail;
  3944. }
  3945. __set_bit(FW_FLAGS_NC, &fw_flags);
  3946. break;
  3947. case RTL_FW_PHY_UNION_NC:
  3948. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  3949. test_bit(FW_FLAGS_NC1, &fw_flags) ||
  3950. test_bit(FW_FLAGS_NC2, &fw_flags) ||
  3951. test_bit(FW_FLAGS_UC2, &fw_flags) ||
  3952. test_bit(FW_FLAGS_UC, &fw_flags) ||
  3953. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  3954. dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
  3955. goto fail;
  3956. }
  3957. if (test_bit(FW_FLAGS_NC, &fw_flags)) {
  3958. dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
  3959. goto fail;
  3960. }
  3961. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  3962. dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
  3963. goto fail;
  3964. }
  3965. __set_bit(FW_FLAGS_NC, &fw_flags);
  3966. break;
  3967. case RTL_FW_PHY_UNION_NC1:
  3968. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  3969. test_bit(FW_FLAGS_NC2, &fw_flags) ||
  3970. test_bit(FW_FLAGS_UC2, &fw_flags) ||
  3971. test_bit(FW_FLAGS_UC, &fw_flags) ||
  3972. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  3973. dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
  3974. goto fail;
  3975. }
  3976. if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
  3977. dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
  3978. goto fail;
  3979. }
  3980. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  3981. dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
  3982. goto fail;
  3983. }
  3984. __set_bit(FW_FLAGS_NC1, &fw_flags);
  3985. break;
  3986. case RTL_FW_PHY_UNION_NC2:
  3987. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  3988. test_bit(FW_FLAGS_UC2, &fw_flags) ||
  3989. test_bit(FW_FLAGS_UC, &fw_flags) ||
  3990. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  3991. dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
  3992. goto fail;
  3993. }
  3994. if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
  3995. dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
  3996. goto fail;
  3997. }
  3998. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  3999. dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
  4000. goto fail;
  4001. }
  4002. __set_bit(FW_FLAGS_NC2, &fw_flags);
  4003. break;
  4004. case RTL_FW_PHY_UNION_UC2:
  4005. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  4006. test_bit(FW_FLAGS_UC, &fw_flags) ||
  4007. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  4008. dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
  4009. goto fail;
  4010. }
  4011. if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
  4012. dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
  4013. goto fail;
  4014. }
  4015. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  4016. dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
  4017. goto fail;
  4018. }
  4019. __set_bit(FW_FLAGS_UC2, &fw_flags);
  4020. break;
  4021. case RTL_FW_PHY_UNION_UC:
  4022. if (!test_bit(FW_FLAGS_START, &fw_flags) ||
  4023. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  4024. dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
  4025. goto fail;
  4026. }
  4027. if (test_bit(FW_FLAGS_UC, &fw_flags)) {
  4028. dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
  4029. goto fail;
  4030. }
  4031. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  4032. dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
  4033. goto fail;
  4034. }
  4035. __set_bit(FW_FLAGS_UC, &fw_flags);
  4036. break;
  4037. case RTL_FW_PHY_UNION_MISC:
  4038. if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
  4039. dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
  4040. goto fail;
  4041. }
  4042. break;
  4043. case RTL_FW_PHY_FIXUP:
  4044. if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
  4045. dev_err(&tp->intf->dev, "check PHY fixup failed\n");
  4046. goto fail;
  4047. }
  4048. break;
  4049. case RTL_FW_PHY_SPEED_UP:
  4050. if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
  4051. dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
  4052. goto fail;
  4053. }
  4054. if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
  4055. dev_err(&tp->intf->dev, "check PHY speed up failed\n");
  4056. goto fail;
  4057. }
  4058. __set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
  4059. break;
  4060. case RTL_FW_PHY_VER:
  4061. if (test_bit(FW_FLAGS_START, &fw_flags) ||
  4062. test_bit(FW_FLAGS_NC, &fw_flags) ||
  4063. test_bit(FW_FLAGS_NC1, &fw_flags) ||
  4064. test_bit(FW_FLAGS_NC2, &fw_flags) ||
  4065. test_bit(FW_FLAGS_UC2, &fw_flags) ||
  4066. test_bit(FW_FLAGS_UC, &fw_flags) ||
  4067. test_bit(FW_FLAGS_STOP, &fw_flags)) {
  4068. dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
  4069. goto fail;
  4070. }
  4071. if (test_bit(FW_FLAGS_VER, &fw_flags)) {
  4072. dev_err(&tp->intf->dev, "multiple PHY version encountered");
  4073. goto fail;
  4074. }
  4075. if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
  4076. dev_err(&tp->intf->dev, "check PHY version failed\n");
  4077. goto fail;
  4078. }
  4079. __set_bit(FW_FLAGS_VER, &fw_flags);
  4080. break;
  4081. default:
  4082. dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
  4083. type);
  4084. break;
  4085. }
  4086. /* next block */
  4087. i += ALIGN(__le32_to_cpu(block->length), 8);
  4088. }
  4089. fw_end:
  4090. if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
  4091. dev_err(&tp->intf->dev, "without PHY_STOP\n");
  4092. goto fail;
  4093. }
  4094. return 0;
  4095. fail:
  4096. return ret;
  4097. }
  4098. static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
  4099. {
  4100. u32 len;
  4101. u8 *data;
  4102. rtl_reset_ocp_base(tp);
  4103. if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
  4104. dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
  4105. return;
  4106. }
  4107. len = __le32_to_cpu(phy->blk_hdr.length);
  4108. len -= __le16_to_cpu(phy->fw_offset);
  4109. data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
  4110. if (rtl_phy_patch_request(tp, true, wait))
  4111. return;
  4112. while (len) {
  4113. u32 ocp_data, size;
  4114. int i;
  4115. if (len < 2048)
  4116. size = len;
  4117. else
  4118. size = 2048;
  4119. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
  4120. ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
  4121. ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
  4122. generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
  4123. data += size;
  4124. len -= size;
  4125. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
  4126. ocp_data |= POL_GPHY_PATCH;
  4127. ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
  4128. for (i = 0; i < 1000; i++) {
  4129. if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
  4130. break;
  4131. }
  4132. if (i == 1000) {
  4133. dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
  4134. break;
  4135. }
  4136. }
  4137. rtl_reset_ocp_base(tp);
  4138. rtl_phy_patch_request(tp, false, wait);
  4139. if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
  4140. dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
  4141. else
  4142. dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
  4143. }
  4144. static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
  4145. {
  4146. u16 ver_addr, ver;
  4147. ver_addr = __le16_to_cpu(phy_ver->ver.addr);
  4148. ver = __le16_to_cpu(phy_ver->ver.data);
  4149. rtl_reset_ocp_base(tp);
  4150. if (sram_read(tp, ver_addr) >= ver) {
  4151. dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
  4152. return 0;
  4153. }
  4154. sram_write(tp, ver_addr, ver);
  4155. dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
  4156. return ver;
  4157. }
  4158. static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
  4159. {
  4160. u16 addr, data;
  4161. rtl_reset_ocp_base(tp);
  4162. addr = __le16_to_cpu(fix->setting.addr);
  4163. data = ocp_reg_read(tp, addr);
  4164. switch (__le16_to_cpu(fix->bit_cmd)) {
  4165. case FW_FIXUP_AND:
  4166. data &= __le16_to_cpu(fix->setting.data);
  4167. break;
  4168. case FW_FIXUP_OR:
  4169. data |= __le16_to_cpu(fix->setting.data);
  4170. break;
  4171. case FW_FIXUP_NOT:
  4172. data &= ~__le16_to_cpu(fix->setting.data);
  4173. break;
  4174. case FW_FIXUP_XOR:
  4175. data ^= __le16_to_cpu(fix->setting.data);
  4176. break;
  4177. default:
  4178. return;
  4179. }
  4180. ocp_reg_write(tp, addr, data);
  4181. dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
  4182. }
  4183. static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
  4184. {
  4185. __le16 *data;
  4186. u32 length;
  4187. int i, num;
  4188. rtl_reset_ocp_base(tp);
  4189. num = phy->pre_num;
  4190. for (i = 0; i < num; i++)
  4191. sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
  4192. __le16_to_cpu(phy->pre_set[i].data));
  4193. length = __le32_to_cpu(phy->blk_hdr.length);
  4194. length -= __le16_to_cpu(phy->fw_offset);
  4195. num = length / 2;
  4196. data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
  4197. ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
  4198. for (i = 0; i < num; i++)
  4199. ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
  4200. num = phy->bp_num;
  4201. for (i = 0; i < num; i++)
  4202. sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
  4203. if (phy->bp_num && phy->bp_en.addr)
  4204. sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
  4205. dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
  4206. }
  4207. static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
  4208. {
  4209. u16 mode_reg, bp_index;
  4210. u32 length, i, num;
  4211. __le16 *data;
  4212. rtl_reset_ocp_base(tp);
  4213. mode_reg = __le16_to_cpu(phy->mode_reg);
  4214. sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
  4215. sram_write(tp, __le16_to_cpu(phy->ba_reg),
  4216. __le16_to_cpu(phy->ba_data));
  4217. length = __le32_to_cpu(phy->blk_hdr.length);
  4218. length -= __le16_to_cpu(phy->fw_offset);
  4219. num = length / 2;
  4220. data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
  4221. ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
  4222. for (i = 0; i < num; i++)
  4223. ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
  4224. sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
  4225. __le16_to_cpu(phy->patch_en_value));
  4226. bp_index = __le16_to_cpu(phy->bp_start);
  4227. num = __le16_to_cpu(phy->bp_num);
  4228. for (i = 0; i < num; i++) {
  4229. sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
  4230. bp_index += 2;
  4231. }
  4232. sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
  4233. dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
  4234. }
  4235. static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
  4236. {
  4237. u16 bp_en_addr, type, fw_ver_reg;
  4238. u32 length;
  4239. u8 *data;
  4240. switch (__le32_to_cpu(mac->blk_hdr.type)) {
  4241. case RTL_FW_PLA:
  4242. type = MCU_TYPE_PLA;
  4243. break;
  4244. case RTL_FW_USB:
  4245. type = MCU_TYPE_USB;
  4246. break;
  4247. default:
  4248. return;
  4249. }
  4250. fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
  4251. if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
  4252. dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
  4253. return;
  4254. }
  4255. rtl_clear_bp(tp, type);
  4256. /* Enable backup/restore of MACDBG. This is required after clearing PLA
  4257. * break points and before applying the PLA firmware.
  4258. */
  4259. if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
  4260. !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
  4261. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
  4262. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
  4263. }
  4264. length = __le32_to_cpu(mac->blk_hdr.length);
  4265. length -= __le16_to_cpu(mac->fw_offset);
  4266. data = (u8 *)mac;
  4267. data += __le16_to_cpu(mac->fw_offset);
  4268. if (generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length,
  4269. data, type) < 0) {
  4270. dev_err(&tp->intf->dev, "Write %s fw fail\n",
  4271. type ? "PLA" : "USB");
  4272. return;
  4273. }
  4274. ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
  4275. __le16_to_cpu(mac->bp_ba_value));
  4276. if (generic_ocp_write(tp, __le16_to_cpu(mac->bp_start), BYTE_EN_DWORD,
  4277. ALIGN(__le16_to_cpu(mac->bp_num) << 1, 4),
  4278. mac->bp, type) < 0) {
  4279. dev_err(&tp->intf->dev, "Write %s bp fail\n",
  4280. type ? "PLA" : "USB");
  4281. return;
  4282. }
  4283. bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
  4284. if (bp_en_addr)
  4285. ocp_write_word(tp, type, bp_en_addr,
  4286. __le16_to_cpu(mac->bp_en_value));
  4287. if (fw_ver_reg)
  4288. ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
  4289. mac->fw_ver_data);
  4290. dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
  4291. }
  4292. static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
  4293. {
  4294. struct rtl_fw *rtl_fw = &tp->rtl_fw;
  4295. const struct firmware *fw;
  4296. struct fw_header *fw_hdr;
  4297. struct fw_phy_patch_key *key;
  4298. u16 key_addr = 0;
  4299. int i, patch_phy = 1;
  4300. if (IS_ERR_OR_NULL(rtl_fw->fw))
  4301. return;
  4302. fw = rtl_fw->fw;
  4303. fw_hdr = (struct fw_header *)fw->data;
  4304. if (rtl_fw->pre_fw)
  4305. rtl_fw->pre_fw(tp);
  4306. for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
  4307. struct fw_block *block = (struct fw_block *)&fw->data[i];
  4308. switch (__le32_to_cpu(block->type)) {
  4309. case RTL_FW_END:
  4310. goto post_fw;
  4311. case RTL_FW_PLA:
  4312. case RTL_FW_USB:
  4313. rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
  4314. break;
  4315. case RTL_FW_PHY_START:
  4316. if (!patch_phy)
  4317. break;
  4318. key = (struct fw_phy_patch_key *)block;
  4319. key_addr = __le16_to_cpu(key->key_reg);
  4320. rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
  4321. break;
  4322. case RTL_FW_PHY_STOP:
  4323. if (!patch_phy)
  4324. break;
  4325. WARN_ON(!key_addr);
  4326. rtl_post_ram_code(tp, key_addr, !power_cut);
  4327. break;
  4328. case RTL_FW_PHY_NC:
  4329. rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
  4330. break;
  4331. case RTL_FW_PHY_VER:
  4332. patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
  4333. break;
  4334. case RTL_FW_PHY_UNION_NC:
  4335. case RTL_FW_PHY_UNION_NC1:
  4336. case RTL_FW_PHY_UNION_NC2:
  4337. case RTL_FW_PHY_UNION_UC2:
  4338. case RTL_FW_PHY_UNION_UC:
  4339. case RTL_FW_PHY_UNION_MISC:
  4340. if (patch_phy)
  4341. rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
  4342. break;
  4343. case RTL_FW_PHY_FIXUP:
  4344. if (patch_phy)
  4345. rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
  4346. break;
  4347. case RTL_FW_PHY_SPEED_UP:
  4348. rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
  4349. break;
  4350. default:
  4351. break;
  4352. }
  4353. i += ALIGN(__le32_to_cpu(block->length), 8);
  4354. }
  4355. post_fw:
  4356. if (rtl_fw->post_fw)
  4357. rtl_fw->post_fw(tp);
  4358. rtl_reset_ocp_base(tp);
  4359. strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
  4360. dev_dbg(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
  4361. }
  4362. static void rtl8152_release_firmware(struct r8152 *tp)
  4363. {
  4364. struct rtl_fw *rtl_fw = &tp->rtl_fw;
  4365. if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
  4366. release_firmware(rtl_fw->fw);
  4367. rtl_fw->fw = NULL;
  4368. }
  4369. }
  4370. static int rtl8152_request_firmware(struct r8152 *tp)
  4371. {
  4372. struct rtl_fw *rtl_fw = &tp->rtl_fw;
  4373. long rc;
  4374. if (rtl_fw->fw || !rtl_fw->fw_name) {
  4375. dev_info(&tp->intf->dev, "skip request firmware\n");
  4376. rc = 0;
  4377. goto result;
  4378. }
  4379. rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
  4380. if (rc < 0)
  4381. goto result;
  4382. rc = rtl8152_check_firmware(tp, rtl_fw);
  4383. if (rc < 0)
  4384. release_firmware(rtl_fw->fw);
  4385. result:
  4386. if (rc) {
  4387. rtl_fw->fw = ERR_PTR(rc);
  4388. dev_warn(&tp->intf->dev,
  4389. "unable to load firmware patch %s (%ld)\n",
  4390. rtl_fw->fw_name, rc);
  4391. }
  4392. return rc;
  4393. }
  4394. static void r8152_aldps_en(struct r8152 *tp, bool enable)
  4395. {
  4396. if (enable) {
  4397. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  4398. LINKENA | DIS_SDSAVE);
  4399. } else {
  4400. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
  4401. DIS_SDSAVE);
  4402. msleep(20);
  4403. }
  4404. }
  4405. static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
  4406. {
  4407. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
  4408. ocp_reg_write(tp, OCP_EEE_DATA, reg);
  4409. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
  4410. }
  4411. static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
  4412. {
  4413. u16 data;
  4414. r8152_mmd_indirect(tp, dev, reg);
  4415. data = ocp_reg_read(tp, OCP_EEE_DATA);
  4416. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  4417. return data;
  4418. }
  4419. static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
  4420. {
  4421. r8152_mmd_indirect(tp, dev, reg);
  4422. ocp_reg_write(tp, OCP_EEE_DATA, data);
  4423. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  4424. }
  4425. static void r8152_eee_en(struct r8152 *tp, bool enable)
  4426. {
  4427. u16 config1, config2, config3;
  4428. u32 ocp_data;
  4429. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  4430. config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
  4431. config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
  4432. config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
  4433. if (enable) {
  4434. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  4435. config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
  4436. config1 |= sd_rise_time(1);
  4437. config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
  4438. config3 |= fast_snr(42);
  4439. } else {
  4440. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  4441. config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
  4442. RX_QUIET_EN);
  4443. config1 |= sd_rise_time(7);
  4444. config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
  4445. config3 |= fast_snr(511);
  4446. }
  4447. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  4448. ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
  4449. ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
  4450. ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
  4451. }
  4452. static void r8153_eee_en(struct r8152 *tp, bool enable)
  4453. {
  4454. u32 ocp_data;
  4455. u16 config;
  4456. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  4457. config = ocp_reg_read(tp, OCP_EEE_CFG);
  4458. if (enable) {
  4459. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  4460. config |= EEE10_EN;
  4461. } else {
  4462. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  4463. config &= ~EEE10_EN;
  4464. }
  4465. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  4466. ocp_reg_write(tp, OCP_EEE_CFG, config);
  4467. tp->ups_info.eee = enable;
  4468. }
  4469. static void r8156_eee_en(struct r8152 *tp, bool enable)
  4470. {
  4471. u16 config;
  4472. r8153_eee_en(tp, enable);
  4473. config = ocp_reg_read(tp, OCP_EEE_ADV2);
  4474. if (enable)
  4475. config |= MDIO_EEE_2_5GT;
  4476. else
  4477. config &= ~MDIO_EEE_2_5GT;
  4478. ocp_reg_write(tp, OCP_EEE_ADV2, config);
  4479. }
  4480. static void rtl_eee_enable(struct r8152 *tp, bool enable)
  4481. {
  4482. switch (tp->version) {
  4483. case RTL_VER_01:
  4484. case RTL_VER_02:
  4485. case RTL_VER_07:
  4486. if (enable) {
  4487. r8152_eee_en(tp, true);
  4488. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
  4489. tp->eee_adv);
  4490. } else {
  4491. r8152_eee_en(tp, false);
  4492. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
  4493. }
  4494. break;
  4495. case RTL_VER_03:
  4496. case RTL_VER_04:
  4497. case RTL_VER_05:
  4498. case RTL_VER_06:
  4499. case RTL_VER_08:
  4500. case RTL_VER_09:
  4501. case RTL_VER_14:
  4502. if (enable) {
  4503. r8153_eee_en(tp, true);
  4504. ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
  4505. } else {
  4506. r8153_eee_en(tp, false);
  4507. ocp_reg_write(tp, OCP_EEE_ADV, 0);
  4508. }
  4509. break;
  4510. case RTL_VER_10:
  4511. case RTL_VER_11:
  4512. case RTL_VER_12:
  4513. case RTL_VER_13:
  4514. case RTL_VER_15:
  4515. if (enable) {
  4516. r8156_eee_en(tp, true);
  4517. ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
  4518. } else {
  4519. r8156_eee_en(tp, false);
  4520. ocp_reg_write(tp, OCP_EEE_ADV, 0);
  4521. }
  4522. break;
  4523. default:
  4524. break;
  4525. }
  4526. }
  4527. static void r8152b_enable_fc(struct r8152 *tp)
  4528. {
  4529. u16 anar;
  4530. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  4531. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  4532. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  4533. tp->ups_info.flow_control = true;
  4534. }
  4535. static void rtl8152_disable(struct r8152 *tp)
  4536. {
  4537. r8152_aldps_en(tp, false);
  4538. rtl_disable(tp);
  4539. r8152_aldps_en(tp, true);
  4540. }
  4541. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  4542. {
  4543. rtl8152_apply_firmware(tp, false);
  4544. rtl_eee_enable(tp, tp->eee_en);
  4545. r8152_aldps_en(tp, true);
  4546. r8152b_enable_fc(tp);
  4547. set_bit(PHY_RESET, &tp->flags);
  4548. }
  4549. static void wait_oob_link_list_ready(struct r8152 *tp)
  4550. {
  4551. u32 ocp_data;
  4552. int i;
  4553. for (i = 0; i < 1000; i++) {
  4554. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  4555. break;
  4556. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4557. if (ocp_data & LINK_LIST_READY)
  4558. break;
  4559. usleep_range(1000, 2000);
  4560. }
  4561. }
  4562. static void r8156b_wait_loading_flash(struct r8152 *tp)
  4563. {
  4564. if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
  4565. !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
  4566. int i;
  4567. for (i = 0; i < 100; i++) {
  4568. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  4569. break;
  4570. if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
  4571. break;
  4572. usleep_range(1000, 2000);
  4573. }
  4574. }
  4575. }
  4576. static void r8152b_exit_oob(struct r8152 *tp)
  4577. {
  4578. u32 ocp_data;
  4579. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  4580. ocp_data &= ~RCR_ACPT_ALL;
  4581. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  4582. rxdy_gated_en(tp, true);
  4583. r8153_teredo_off(tp);
  4584. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  4585. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  4586. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4587. ocp_data &= ~NOW_IS_OOB;
  4588. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  4589. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4590. ocp_data &= ~MCU_BORW_EN;
  4591. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4592. wait_oob_link_list_ready(tp);
  4593. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4594. ocp_data |= RE_INIT_LL;
  4595. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4596. wait_oob_link_list_ready(tp);
  4597. rtl8152_nic_reset(tp);
  4598. /* rx share fifo credit full threshold */
  4599. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  4600. if (tp->udev->speed == USB_SPEED_FULL ||
  4601. tp->udev->speed == USB_SPEED_LOW) {
  4602. /* rx share fifo credit near full threshold */
  4603. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  4604. RXFIFO_THR2_FULL);
  4605. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  4606. RXFIFO_THR3_FULL);
  4607. } else {
  4608. /* rx share fifo credit near full threshold */
  4609. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  4610. RXFIFO_THR2_HIGH);
  4611. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  4612. RXFIFO_THR3_HIGH);
  4613. }
  4614. /* TX share fifo free credit full threshold */
  4615. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  4616. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  4617. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
  4618. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  4619. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  4620. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  4621. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  4622. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  4623. ocp_data |= TCR0_AUTO_FIFO;
  4624. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  4625. }
  4626. static void r8152b_enter_oob(struct r8152 *tp)
  4627. {
  4628. u32 ocp_data;
  4629. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4630. ocp_data &= ~NOW_IS_OOB;
  4631. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  4632. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  4633. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  4634. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  4635. rtl_disable(tp);
  4636. wait_oob_link_list_ready(tp);
  4637. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4638. ocp_data |= RE_INIT_LL;
  4639. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4640. wait_oob_link_list_ready(tp);
  4641. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  4642. rtl_rx_vlan_en(tp, true);
  4643. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
  4644. ocp_data |= ALDPS_PROXY_MODE;
  4645. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
  4646. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4647. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  4648. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  4649. rxdy_gated_en(tp, false);
  4650. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  4651. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  4652. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  4653. }
  4654. static int r8153_pre_firmware_1(struct r8152 *tp)
  4655. {
  4656. int i;
  4657. /* Wait till the WTD timer is ready. It would take at most 104 ms. */
  4658. for (i = 0; i < 104; i++) {
  4659. u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
  4660. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  4661. return -ENODEV;
  4662. if (!(ocp_data & WTD1_EN))
  4663. break;
  4664. usleep_range(1000, 2000);
  4665. }
  4666. return 0;
  4667. }
  4668. static int r8153_post_firmware_1(struct r8152 *tp)
  4669. {
  4670. /* set USB_BP_4 to support USB_SPEED_SUPER only */
  4671. if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
  4672. ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
  4673. /* reset UPHY timer to 36 ms */
  4674. ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
  4675. return 0;
  4676. }
  4677. static int r8153_pre_firmware_2(struct r8152 *tp)
  4678. {
  4679. u32 ocp_data;
  4680. r8153_pre_firmware_1(tp);
  4681. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
  4682. ocp_data &= ~FW_FIX_SUSPEND;
  4683. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
  4684. return 0;
  4685. }
  4686. static int r8153_post_firmware_2(struct r8152 *tp)
  4687. {
  4688. u32 ocp_data;
  4689. /* enable bp0 if support USB_SPEED_SUPER only */
  4690. if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
  4691. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
  4692. ocp_data |= BIT(0);
  4693. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
  4694. }
  4695. /* reset UPHY timer to 36 ms */
  4696. ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
  4697. /* enable U3P3 check, set the counter to 4 */
  4698. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
  4699. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
  4700. ocp_data |= FW_FIX_SUSPEND;
  4701. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
  4702. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
  4703. ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
  4704. ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
  4705. return 0;
  4706. }
  4707. static int r8153_post_firmware_3(struct r8152 *tp)
  4708. {
  4709. u32 ocp_data;
  4710. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
  4711. ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
  4712. ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
  4713. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
  4714. ocp_data |= FW_IP_RESET_EN;
  4715. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
  4716. return 0;
  4717. }
  4718. static int r8153b_pre_firmware_1(struct r8152 *tp)
  4719. {
  4720. /* enable fc timer and set timer to 1 second. */
  4721. ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
  4722. CTRL_TIMER_EN | (1000 / 8));
  4723. return 0;
  4724. }
  4725. static int r8153b_post_firmware_1(struct r8152 *tp)
  4726. {
  4727. u32 ocp_data;
  4728. /* enable bp0 for RTL8153-BND */
  4729. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
  4730. if (ocp_data & BND_MASK) {
  4731. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
  4732. ocp_data |= BIT(0);
  4733. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
  4734. }
  4735. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
  4736. ocp_data |= FLOW_CTRL_PATCH_OPT;
  4737. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
  4738. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  4739. ocp_data |= FC_PATCH_TASK;
  4740. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  4741. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
  4742. ocp_data |= FW_IP_RESET_EN;
  4743. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
  4744. return 0;
  4745. }
  4746. static int r8153c_post_firmware_1(struct r8152 *tp)
  4747. {
  4748. u32 ocp_data;
  4749. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
  4750. ocp_data |= FLOW_CTRL_PATCH_2;
  4751. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
  4752. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  4753. ocp_data |= FC_PATCH_TASK;
  4754. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  4755. return 0;
  4756. }
  4757. static int r8156a_post_firmware_1(struct r8152 *tp)
  4758. {
  4759. u32 ocp_data;
  4760. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
  4761. ocp_data |= FW_IP_RESET_EN;
  4762. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
  4763. /* Modify U3PHY parameter for compatibility issue */
  4764. ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
  4765. ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
  4766. return 0;
  4767. }
  4768. static void r8153_aldps_en(struct r8152 *tp, bool enable)
  4769. {
  4770. u16 data;
  4771. data = ocp_reg_read(tp, OCP_POWER_CFG);
  4772. if (enable) {
  4773. data |= EN_ALDPS;
  4774. ocp_reg_write(tp, OCP_POWER_CFG, data);
  4775. } else {
  4776. int i;
  4777. data &= ~EN_ALDPS;
  4778. ocp_reg_write(tp, OCP_POWER_CFG, data);
  4779. for (i = 0; i < 20; i++) {
  4780. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  4781. return;
  4782. usleep_range(1000, 2000);
  4783. if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
  4784. break;
  4785. }
  4786. }
  4787. tp->ups_info.aldps = enable;
  4788. }
  4789. static void r8153_hw_phy_cfg(struct r8152 *tp)
  4790. {
  4791. u32 ocp_data;
  4792. u16 data;
  4793. /* disable ALDPS before updating the PHY parameters */
  4794. r8153_aldps_en(tp, false);
  4795. /* disable EEE before updating the PHY parameters */
  4796. rtl_eee_enable(tp, false);
  4797. rtl8152_apply_firmware(tp, false);
  4798. if (tp->version == RTL_VER_03) {
  4799. data = ocp_reg_read(tp, OCP_EEE_CFG);
  4800. data &= ~CTAP_SHORT_EN;
  4801. ocp_reg_write(tp, OCP_EEE_CFG, data);
  4802. }
  4803. data = ocp_reg_read(tp, OCP_POWER_CFG);
  4804. data |= EEE_CLKDIV_EN;
  4805. ocp_reg_write(tp, OCP_POWER_CFG, data);
  4806. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  4807. data |= EN_10M_BGOFF;
  4808. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  4809. data = ocp_reg_read(tp, OCP_POWER_CFG);
  4810. data |= EN_10M_PLLOFF;
  4811. ocp_reg_write(tp, OCP_POWER_CFG, data);
  4812. sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
  4813. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  4814. ocp_data |= PFM_PWM_SWITCH;
  4815. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  4816. /* Enable LPF corner auto tune */
  4817. sram_write(tp, SRAM_LPF_CFG, 0xf70f);
  4818. /* Adjust 10M Amplitude */
  4819. sram_write(tp, SRAM_10M_AMP1, 0x00af);
  4820. sram_write(tp, SRAM_10M_AMP2, 0x0208);
  4821. if (tp->eee_en)
  4822. rtl_eee_enable(tp, true);
  4823. r8153_aldps_en(tp, true);
  4824. r8152b_enable_fc(tp);
  4825. switch (tp->version) {
  4826. case RTL_VER_03:
  4827. case RTL_VER_04:
  4828. break;
  4829. case RTL_VER_05:
  4830. case RTL_VER_06:
  4831. default:
  4832. r8153_u2p3en(tp, true);
  4833. break;
  4834. }
  4835. set_bit(PHY_RESET, &tp->flags);
  4836. }
  4837. static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
  4838. {
  4839. u32 ocp_data;
  4840. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
  4841. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
  4842. ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
  4843. ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
  4844. return ocp_data;
  4845. }
  4846. static void r8153b_hw_phy_cfg(struct r8152 *tp)
  4847. {
  4848. u32 ocp_data;
  4849. u16 data;
  4850. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  4851. if (ocp_data & PCUT_STATUS) {
  4852. ocp_data &= ~PCUT_STATUS;
  4853. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  4854. }
  4855. /* disable ALDPS before updating the PHY parameters */
  4856. r8153_aldps_en(tp, false);
  4857. /* disable EEE before updating the PHY parameters */
  4858. rtl_eee_enable(tp, false);
  4859. /* U1/U2/L1 idle timer. 500 us */
  4860. ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
  4861. data = r8153_phy_status(tp, 0);
  4862. switch (data) {
  4863. case PHY_STAT_PWRDN:
  4864. case PHY_STAT_EXT_INIT:
  4865. rtl8152_apply_firmware(tp, true);
  4866. data = r8152_mdio_read(tp, MII_BMCR);
  4867. data &= ~BMCR_PDOWN;
  4868. r8152_mdio_write(tp, MII_BMCR, data);
  4869. break;
  4870. case PHY_STAT_LAN_ON:
  4871. default:
  4872. rtl8152_apply_firmware(tp, false);
  4873. break;
  4874. }
  4875. r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
  4876. data = sram_read(tp, SRAM_GREEN_CFG);
  4877. data |= R_TUNE_EN;
  4878. sram_write(tp, SRAM_GREEN_CFG, data);
  4879. data = ocp_reg_read(tp, OCP_NCTL_CFG);
  4880. data |= PGA_RETURN_EN;
  4881. ocp_reg_write(tp, OCP_NCTL_CFG, data);
  4882. /* ADC Bias Calibration:
  4883. * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
  4884. * bit (bit3) to rebuild the real 16-bit data. Write the data to the
  4885. * ADC ioffset.
  4886. */
  4887. ocp_data = r8152_efuse_read(tp, 0x7d);
  4888. data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
  4889. if (data != 0xffff)
  4890. ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
  4891. /* ups mode tx-link-pulse timing adjustment:
  4892. * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
  4893. * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
  4894. */
  4895. ocp_data = ocp_reg_read(tp, 0xc426);
  4896. ocp_data &= 0x3fff;
  4897. if (ocp_data) {
  4898. u32 swr_cnt_1ms_ini;
  4899. swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
  4900. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
  4901. ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
  4902. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
  4903. }
  4904. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  4905. ocp_data |= PFM_PWM_SWITCH;
  4906. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  4907. /* Advnace EEE */
  4908. if (!rtl_phy_patch_request(tp, true, true)) {
  4909. data = ocp_reg_read(tp, OCP_POWER_CFG);
  4910. data |= EEE_CLKDIV_EN;
  4911. ocp_reg_write(tp, OCP_POWER_CFG, data);
  4912. tp->ups_info.eee_ckdiv = true;
  4913. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  4914. data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
  4915. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  4916. tp->ups_info.eee_cmod_lv = true;
  4917. tp->ups_info._10m_ckdiv = true;
  4918. tp->ups_info.eee_plloff_giga = true;
  4919. ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
  4920. ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
  4921. tp->ups_info._250m_ckdiv = true;
  4922. rtl_phy_patch_request(tp, false, true);
  4923. }
  4924. if (tp->eee_en)
  4925. rtl_eee_enable(tp, true);
  4926. r8153_aldps_en(tp, true);
  4927. r8152b_enable_fc(tp);
  4928. set_bit(PHY_RESET, &tp->flags);
  4929. }
  4930. static void r8153c_hw_phy_cfg(struct r8152 *tp)
  4931. {
  4932. r8153b_hw_phy_cfg(tp);
  4933. tp->ups_info.r_tune = true;
  4934. }
  4935. static void rtl8153_change_mtu(struct r8152 *tp)
  4936. {
  4937. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
  4938. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
  4939. }
  4940. static void r8153_first_init(struct r8152 *tp)
  4941. {
  4942. u32 ocp_data;
  4943. rxdy_gated_en(tp, true);
  4944. r8153_teredo_off(tp);
  4945. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  4946. ocp_data &= ~RCR_ACPT_ALL;
  4947. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  4948. rtl8152_nic_reset(tp);
  4949. rtl_reset_bmu(tp);
  4950. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4951. ocp_data &= ~NOW_IS_OOB;
  4952. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  4953. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4954. ocp_data &= ~MCU_BORW_EN;
  4955. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4956. wait_oob_link_list_ready(tp);
  4957. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4958. ocp_data |= RE_INIT_LL;
  4959. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4960. wait_oob_link_list_ready(tp);
  4961. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  4962. rtl8153_change_mtu(tp);
  4963. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  4964. ocp_data |= TCR0_AUTO_FIFO;
  4965. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  4966. rtl8152_nic_reset(tp);
  4967. /* rx share fifo credit full threshold */
  4968. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  4969. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  4970. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  4971. /* TX share fifo free credit full threshold */
  4972. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  4973. }
  4974. static void r8153_enter_oob(struct r8152 *tp)
  4975. {
  4976. u32 ocp_data;
  4977. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  4978. ocp_data &= ~NOW_IS_OOB;
  4979. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  4980. /* RX FIFO settings for OOB */
  4981. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  4982. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  4983. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  4984. rtl_disable(tp);
  4985. rtl_reset_bmu(tp);
  4986. wait_oob_link_list_ready(tp);
  4987. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  4988. ocp_data |= RE_INIT_LL;
  4989. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  4990. wait_oob_link_list_ready(tp);
  4991. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
  4992. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
  4993. switch (tp->version) {
  4994. case RTL_VER_03:
  4995. case RTL_VER_04:
  4996. case RTL_VER_05:
  4997. case RTL_VER_06:
  4998. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  4999. ocp_data &= ~TEREDO_WAKE_MASK;
  5000. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  5001. break;
  5002. case RTL_VER_08:
  5003. case RTL_VER_09:
  5004. case RTL_VER_14:
  5005. /* Clear teredo wake event. bit[15:8] is the teredo wakeup
  5006. * type. Set it to zero. bits[7:0] are the W1C bits about
  5007. * the events. Set them to all 1 to clear them.
  5008. */
  5009. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
  5010. break;
  5011. default:
  5012. break;
  5013. }
  5014. rtl_rx_vlan_en(tp, true);
  5015. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
  5016. ocp_data |= ALDPS_PROXY_MODE;
  5017. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
  5018. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  5019. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  5020. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  5021. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  5022. ocp_data |= MCU_BORW_EN;
  5023. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  5024. rxdy_gated_en(tp, false);
  5025. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  5026. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  5027. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  5028. }
  5029. static void rtl8153_disable(struct r8152 *tp)
  5030. {
  5031. r8153_aldps_en(tp, false);
  5032. rtl_disable(tp);
  5033. rtl_reset_bmu(tp);
  5034. r8153_aldps_en(tp, true);
  5035. }
  5036. static u32 fc_pause_on_auto(struct r8152 *tp)
  5037. {
  5038. return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
  5039. }
  5040. static u32 fc_pause_off_auto(struct r8152 *tp)
  5041. {
  5042. return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
  5043. }
  5044. static void r8156_fc_parameter(struct r8152 *tp)
  5045. {
  5046. u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
  5047. u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
  5048. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
  5049. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
  5050. }
  5051. static int rtl8156_enable(struct r8152 *tp)
  5052. {
  5053. u32 ocp_data;
  5054. u16 speed;
  5055. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5056. return -ENODEV;
  5057. r8156_fc_parameter(tp);
  5058. set_tx_qlen(tp);
  5059. rtl_set_eee_plus(tp);
  5060. r8153_set_rx_early_timeout(tp);
  5061. r8153_set_rx_early_size(tp);
  5062. speed = rtl8152_get_speed(tp);
  5063. rtl_set_ifg(tp, speed);
  5064. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  5065. if (speed & _2500bps)
  5066. ocp_data &= ~IDLE_SPDWN_EN;
  5067. else
  5068. ocp_data |= IDLE_SPDWN_EN;
  5069. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  5070. if (speed & _1000bps)
  5071. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
  5072. else if (speed & _500bps)
  5073. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
  5074. if (tp->udev->speed == USB_SPEED_HIGH) {
  5075. /* USB 0xb45e[3:0] l1_nyet_hird */
  5076. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
  5077. ocp_data &= ~0xf;
  5078. if (is_flow_control(speed))
  5079. ocp_data |= 0xf;
  5080. else
  5081. ocp_data |= 0x1;
  5082. ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
  5083. }
  5084. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  5085. ocp_data &= ~FC_PATCH_TASK;
  5086. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  5087. usleep_range(1000, 2000);
  5088. ocp_data |= FC_PATCH_TASK;
  5089. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  5090. return rtl_enable(tp);
  5091. }
  5092. static void rtl8156_disable(struct r8152 *tp)
  5093. {
  5094. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 0);
  5095. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 0);
  5096. rtl8153_disable(tp);
  5097. }
  5098. static int rtl8156b_enable(struct r8152 *tp)
  5099. {
  5100. u32 ocp_data;
  5101. u16 speed;
  5102. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5103. return -ENODEV;
  5104. set_tx_qlen(tp);
  5105. rtl_set_eee_plus(tp);
  5106. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
  5107. ocp_data &= ~RX_AGGR_NUM_MASK;
  5108. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
  5109. r8153_set_rx_early_timeout(tp);
  5110. r8153_set_rx_early_size(tp);
  5111. speed = rtl8152_get_speed(tp);
  5112. rtl_set_ifg(tp, speed);
  5113. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  5114. if (speed & _2500bps)
  5115. ocp_data &= ~IDLE_SPDWN_EN;
  5116. else
  5117. ocp_data |= IDLE_SPDWN_EN;
  5118. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  5119. if (tp->udev->speed == USB_SPEED_HIGH) {
  5120. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
  5121. ocp_data &= ~0xf;
  5122. if (is_flow_control(speed))
  5123. ocp_data |= 0xf;
  5124. else
  5125. ocp_data |= 0x1;
  5126. ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
  5127. }
  5128. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  5129. ocp_data &= ~FC_PATCH_TASK;
  5130. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  5131. usleep_range(1000, 2000);
  5132. ocp_data |= FC_PATCH_TASK;
  5133. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  5134. return rtl_enable(tp);
  5135. }
  5136. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
  5137. u32 advertising)
  5138. {
  5139. u16 bmcr;
  5140. int ret = 0;
  5141. if (autoneg == AUTONEG_DISABLE) {
  5142. if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
  5143. return -EINVAL;
  5144. switch (speed) {
  5145. case SPEED_10:
  5146. bmcr = BMCR_SPEED10;
  5147. if (duplex == DUPLEX_FULL) {
  5148. bmcr |= BMCR_FULLDPLX;
  5149. tp->ups_info.speed_duplex = FORCE_10M_FULL;
  5150. } else {
  5151. tp->ups_info.speed_duplex = FORCE_10M_HALF;
  5152. }
  5153. break;
  5154. case SPEED_100:
  5155. bmcr = BMCR_SPEED100;
  5156. if (duplex == DUPLEX_FULL) {
  5157. bmcr |= BMCR_FULLDPLX;
  5158. tp->ups_info.speed_duplex = FORCE_100M_FULL;
  5159. } else {
  5160. tp->ups_info.speed_duplex = FORCE_100M_HALF;
  5161. }
  5162. break;
  5163. case SPEED_1000:
  5164. if (tp->mii.supports_gmii) {
  5165. bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
  5166. tp->ups_info.speed_duplex = NWAY_1000M_FULL;
  5167. break;
  5168. }
  5169. fallthrough;
  5170. default:
  5171. ret = -EINVAL;
  5172. goto out;
  5173. }
  5174. if (duplex == DUPLEX_FULL)
  5175. tp->mii.full_duplex = 1;
  5176. else
  5177. tp->mii.full_duplex = 0;
  5178. tp->mii.force_media = 1;
  5179. } else {
  5180. u16 orig, new1;
  5181. u32 support;
  5182. support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
  5183. RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
  5184. if (tp->mii.supports_gmii) {
  5185. support |= RTL_ADVERTISED_1000_FULL;
  5186. if (tp->support_2500full)
  5187. support |= RTL_ADVERTISED_2500_FULL;
  5188. }
  5189. if (!(advertising & support))
  5190. return -EINVAL;
  5191. orig = r8152_mdio_read(tp, MII_ADVERTISE);
  5192. new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  5193. ADVERTISE_100HALF | ADVERTISE_100FULL);
  5194. if (advertising & RTL_ADVERTISED_10_HALF) {
  5195. new1 |= ADVERTISE_10HALF;
  5196. tp->ups_info.speed_duplex = NWAY_10M_HALF;
  5197. }
  5198. if (advertising & RTL_ADVERTISED_10_FULL) {
  5199. new1 |= ADVERTISE_10FULL;
  5200. tp->ups_info.speed_duplex = NWAY_10M_FULL;
  5201. }
  5202. if (advertising & RTL_ADVERTISED_100_HALF) {
  5203. new1 |= ADVERTISE_100HALF;
  5204. tp->ups_info.speed_duplex = NWAY_100M_HALF;
  5205. }
  5206. if (advertising & RTL_ADVERTISED_100_FULL) {
  5207. new1 |= ADVERTISE_100FULL;
  5208. tp->ups_info.speed_duplex = NWAY_100M_FULL;
  5209. }
  5210. if (orig != new1) {
  5211. r8152_mdio_write(tp, MII_ADVERTISE, new1);
  5212. tp->mii.advertising = new1;
  5213. }
  5214. if (tp->mii.supports_gmii) {
  5215. orig = r8152_mdio_read(tp, MII_CTRL1000);
  5216. new1 = orig & ~(ADVERTISE_1000FULL |
  5217. ADVERTISE_1000HALF);
  5218. if (advertising & RTL_ADVERTISED_1000_FULL) {
  5219. new1 |= ADVERTISE_1000FULL;
  5220. tp->ups_info.speed_duplex = NWAY_1000M_FULL;
  5221. }
  5222. if (orig != new1)
  5223. r8152_mdio_write(tp, MII_CTRL1000, new1);
  5224. }
  5225. if (tp->support_2500full) {
  5226. orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
  5227. new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
  5228. if (advertising & RTL_ADVERTISED_2500_FULL) {
  5229. new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
  5230. tp->ups_info.speed_duplex = NWAY_2500M_FULL;
  5231. }
  5232. if (orig != new1)
  5233. ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
  5234. }
  5235. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  5236. tp->mii.force_media = 0;
  5237. }
  5238. if (test_and_clear_bit(PHY_RESET, &tp->flags))
  5239. bmcr |= BMCR_RESET;
  5240. r8152_mdio_write(tp, MII_BMCR, bmcr);
  5241. if (bmcr & BMCR_RESET) {
  5242. int i;
  5243. for (i = 0; i < 50; i++) {
  5244. msleep(20);
  5245. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  5246. break;
  5247. }
  5248. }
  5249. out:
  5250. return ret;
  5251. }
  5252. static void rtl8152_up(struct r8152 *tp)
  5253. {
  5254. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5255. return;
  5256. r8152_aldps_en(tp, false);
  5257. r8152b_exit_oob(tp);
  5258. r8152_aldps_en(tp, true);
  5259. }
  5260. static void rtl8152_down(struct r8152 *tp)
  5261. {
  5262. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  5263. rtl_drop_queued_tx(tp);
  5264. return;
  5265. }
  5266. r8152_power_cut_en(tp, false);
  5267. r8152_aldps_en(tp, false);
  5268. r8152b_enter_oob(tp);
  5269. r8152_aldps_en(tp, true);
  5270. }
  5271. static void rtl8153_up(struct r8152 *tp)
  5272. {
  5273. u32 ocp_data;
  5274. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5275. return;
  5276. r8153_u1u2en(tp, false);
  5277. r8153_u2p3en(tp, false);
  5278. r8153_aldps_en(tp, false);
  5279. r8153_first_init(tp);
  5280. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
  5281. ocp_data |= LANWAKE_CLR_EN;
  5282. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
  5283. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
  5284. ocp_data &= ~LANWAKE_PIN;
  5285. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
  5286. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
  5287. ocp_data &= ~DELAY_PHY_PWR_CHG;
  5288. ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
  5289. r8153_aldps_en(tp, true);
  5290. switch (tp->version) {
  5291. case RTL_VER_03:
  5292. case RTL_VER_04:
  5293. break;
  5294. case RTL_VER_05:
  5295. case RTL_VER_06:
  5296. default:
  5297. r8153_u2p3en(tp, true);
  5298. break;
  5299. }
  5300. r8153_u1u2en(tp, true);
  5301. }
  5302. static void rtl8153_down(struct r8152 *tp)
  5303. {
  5304. u32 ocp_data;
  5305. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  5306. rtl_drop_queued_tx(tp);
  5307. return;
  5308. }
  5309. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
  5310. ocp_data &= ~LANWAKE_CLR_EN;
  5311. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
  5312. r8153_u1u2en(tp, false);
  5313. r8153_u2p3en(tp, false);
  5314. r8153_power_cut_en(tp, false);
  5315. r8153_aldps_en(tp, false);
  5316. r8153_enter_oob(tp);
  5317. r8153_aldps_en(tp, true);
  5318. }
  5319. static void rtl8153b_up(struct r8152 *tp)
  5320. {
  5321. u32 ocp_data;
  5322. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5323. return;
  5324. r8153b_u1u2en(tp, false);
  5325. r8153_u2p3en(tp, false);
  5326. r8153_aldps_en(tp, false);
  5327. r8153_first_init(tp);
  5328. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
  5329. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5330. ocp_data &= ~PLA_MCU_SPDWN_EN;
  5331. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5332. r8153_aldps_en(tp, true);
  5333. if (tp->udev->speed >= USB_SPEED_SUPER)
  5334. r8153b_u1u2en(tp, true);
  5335. }
  5336. static void rtl8153b_down(struct r8152 *tp)
  5337. {
  5338. u32 ocp_data;
  5339. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  5340. rtl_drop_queued_tx(tp);
  5341. return;
  5342. }
  5343. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5344. ocp_data |= PLA_MCU_SPDWN_EN;
  5345. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5346. r8153b_u1u2en(tp, false);
  5347. r8153_u2p3en(tp, false);
  5348. r8153b_power_cut_en(tp, false);
  5349. r8153_aldps_en(tp, false);
  5350. r8153_enter_oob(tp);
  5351. r8153_aldps_en(tp, true);
  5352. }
  5353. static void rtl8153c_change_mtu(struct r8152 *tp)
  5354. {
  5355. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
  5356. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
  5357. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
  5358. /* Adjust the tx fifo free credit full threshold, otherwise
  5359. * the fifo would be too small to send a jumbo frame packet.
  5360. */
  5361. if (tp->netdev->mtu < 8000)
  5362. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
  5363. else
  5364. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
  5365. }
  5366. static void rtl8153c_up(struct r8152 *tp)
  5367. {
  5368. u32 ocp_data;
  5369. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5370. return;
  5371. r8153b_u1u2en(tp, false);
  5372. r8153_u2p3en(tp, false);
  5373. r8153_aldps_en(tp, false);
  5374. rxdy_gated_en(tp, true);
  5375. r8153_teredo_off(tp);
  5376. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  5377. ocp_data &= ~RCR_ACPT_ALL;
  5378. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  5379. rtl8152_nic_reset(tp);
  5380. rtl_reset_bmu(tp);
  5381. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  5382. ocp_data &= ~NOW_IS_OOB;
  5383. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  5384. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  5385. ocp_data &= ~MCU_BORW_EN;
  5386. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  5387. wait_oob_link_list_ready(tp);
  5388. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  5389. ocp_data |= RE_INIT_LL;
  5390. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  5391. wait_oob_link_list_ready(tp);
  5392. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  5393. rtl8153c_change_mtu(tp);
  5394. rtl8152_nic_reset(tp);
  5395. /* rx share fifo credit full threshold */
  5396. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
  5397. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
  5398. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  5399. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  5400. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
  5401. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  5402. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  5403. ocp_data |= BIT(8);
  5404. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  5405. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  5406. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5407. ocp_data &= ~PLA_MCU_SPDWN_EN;
  5408. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5409. r8153_aldps_en(tp, true);
  5410. r8153b_u1u2en(tp, true);
  5411. }
  5412. static void rtl8156_change_mtu(struct r8152 *tp)
  5413. {
  5414. u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
  5415. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
  5416. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
  5417. r8156_fc_parameter(tp);
  5418. /* TX share fifo free credit full threshold */
  5419. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
  5420. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
  5421. ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
  5422. }
  5423. static void rtl8156_up(struct r8152 *tp)
  5424. {
  5425. u32 ocp_data;
  5426. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5427. return;
  5428. r8153b_u1u2en(tp, false);
  5429. r8153_u2p3en(tp, false);
  5430. r8153_aldps_en(tp, false);
  5431. rxdy_gated_en(tp, true);
  5432. r8153_teredo_off(tp);
  5433. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  5434. ocp_data &= ~RCR_ACPT_ALL;
  5435. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  5436. rtl8152_nic_reset(tp);
  5437. rtl_reset_bmu(tp);
  5438. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  5439. ocp_data &= ~NOW_IS_OOB;
  5440. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  5441. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  5442. ocp_data &= ~MCU_BORW_EN;
  5443. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  5444. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  5445. rtl8156_change_mtu(tp);
  5446. switch (tp->version) {
  5447. case RTL_TEST_01:
  5448. case RTL_VER_10:
  5449. case RTL_VER_11:
  5450. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
  5451. ocp_data |= ACT_ODMA;
  5452. ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
  5453. break;
  5454. default:
  5455. break;
  5456. }
  5457. /* share FIFO settings */
  5458. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
  5459. ocp_data &= ~RXFIFO_FULL_MASK;
  5460. ocp_data |= 0x08;
  5461. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
  5462. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5463. ocp_data &= ~PLA_MCU_SPDWN_EN;
  5464. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5465. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
  5466. ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
  5467. ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
  5468. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
  5469. if (tp->saved_wolopts != __rtl_get_wol(tp)) {
  5470. netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
  5471. __rtl_set_wol(tp, tp->saved_wolopts);
  5472. }
  5473. r8153_aldps_en(tp, true);
  5474. r8153_u2p3en(tp, true);
  5475. if (tp->udev->speed >= USB_SPEED_SUPER)
  5476. r8153b_u1u2en(tp, true);
  5477. }
  5478. static void rtl8156_down(struct r8152 *tp)
  5479. {
  5480. u32 ocp_data;
  5481. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  5482. rtl_drop_queued_tx(tp);
  5483. return;
  5484. }
  5485. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5486. ocp_data |= PLA_MCU_SPDWN_EN;
  5487. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5488. r8153b_u1u2en(tp, false);
  5489. r8153_u2p3en(tp, false);
  5490. r8153b_power_cut_en(tp, false);
  5491. r8153_aldps_en(tp, false);
  5492. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  5493. ocp_data &= ~NOW_IS_OOB;
  5494. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  5495. /* RX FIFO settings for OOB */
  5496. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16);
  5497. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16);
  5498. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16);
  5499. rtl_disable(tp);
  5500. rtl_reset_bmu(tp);
  5501. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
  5502. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
  5503. /* Clear teredo wake event. bit[15:8] is the teredo wakeup
  5504. * type. Set it to zero. bits[7:0] are the W1C bits about
  5505. * the events. Set them to all 1 to clear them.
  5506. */
  5507. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
  5508. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  5509. ocp_data |= NOW_IS_OOB;
  5510. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  5511. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  5512. ocp_data |= MCU_BORW_EN;
  5513. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  5514. rtl_rx_vlan_en(tp, true);
  5515. rxdy_gated_en(tp, false);
  5516. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  5517. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  5518. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  5519. r8153_aldps_en(tp, true);
  5520. }
  5521. static bool rtl8152_in_nway(struct r8152 *tp)
  5522. {
  5523. u16 nway_state;
  5524. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
  5525. tp->ocp_base = 0x2000;
  5526. ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
  5527. nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
  5528. /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
  5529. if (nway_state & 0xc000)
  5530. return false;
  5531. else
  5532. return true;
  5533. }
  5534. static bool rtl8153_in_nway(struct r8152 *tp)
  5535. {
  5536. u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
  5537. if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
  5538. return false;
  5539. else
  5540. return true;
  5541. }
  5542. static void r8156_mdio_force_mode(struct r8152 *tp)
  5543. {
  5544. u16 data;
  5545. /* Select force mode through 0xa5b4 bit 15
  5546. * 0: MDIO force mode
  5547. * 1: MMD force mode
  5548. */
  5549. data = ocp_reg_read(tp, 0xa5b4);
  5550. if (data & BIT(15)) {
  5551. data &= ~BIT(15);
  5552. ocp_reg_write(tp, 0xa5b4, data);
  5553. }
  5554. }
  5555. static void set_carrier(struct r8152 *tp)
  5556. {
  5557. struct net_device *netdev = tp->netdev;
  5558. struct napi_struct *napi = &tp->napi;
  5559. u16 speed;
  5560. speed = rtl8152_get_speed(tp);
  5561. if (speed & LINK_STATUS) {
  5562. if (!netif_carrier_ok(netdev)) {
  5563. tp->rtl_ops.enable(tp);
  5564. netif_stop_queue(netdev);
  5565. napi_disable(napi);
  5566. netif_carrier_on(netdev);
  5567. rtl_start_rx(tp);
  5568. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  5569. _rtl8152_set_rx_mode(netdev);
  5570. napi_enable(napi);
  5571. netif_wake_queue(netdev);
  5572. netif_info(tp, link, netdev, "carrier on\n");
  5573. } else if (netif_queue_stopped(netdev) &&
  5574. skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
  5575. netif_wake_queue(netdev);
  5576. }
  5577. } else {
  5578. if (netif_carrier_ok(netdev)) {
  5579. netif_carrier_off(netdev);
  5580. tasklet_disable(&tp->tx_tl);
  5581. napi_disable(napi);
  5582. tp->rtl_ops.disable(tp);
  5583. napi_enable(napi);
  5584. tasklet_enable(&tp->tx_tl);
  5585. netif_info(tp, link, netdev, "carrier off\n");
  5586. }
  5587. }
  5588. }
  5589. static void rtl_work_func_t(struct work_struct *work)
  5590. {
  5591. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  5592. /* If the device is unplugged or !netif_running(), the workqueue
  5593. * doesn't need to wake the device, and could return directly.
  5594. */
  5595. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || !netif_running(tp->netdev))
  5596. return;
  5597. if (usb_autopm_get_interface(tp->intf) < 0)
  5598. return;
  5599. if (!test_bit(WORK_ENABLE, &tp->flags))
  5600. goto out1;
  5601. if (!mutex_trylock(&tp->control)) {
  5602. schedule_delayed_work(&tp->schedule, 0);
  5603. goto out1;
  5604. }
  5605. if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
  5606. set_carrier(tp);
  5607. if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
  5608. _rtl8152_set_rx_mode(tp->netdev);
  5609. /* don't schedule tasket before linking */
  5610. if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
  5611. netif_carrier_ok(tp->netdev))
  5612. tasklet_schedule(&tp->tx_tl);
  5613. if (test_and_clear_bit(RX_EPROTO, &tp->flags) &&
  5614. !list_empty(&tp->rx_done))
  5615. napi_schedule(&tp->napi);
  5616. mutex_unlock(&tp->control);
  5617. out1:
  5618. usb_autopm_put_interface(tp->intf);
  5619. }
  5620. static void rtl_hw_phy_work_func_t(struct work_struct *work)
  5621. {
  5622. struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
  5623. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5624. return;
  5625. if (usb_autopm_get_interface(tp->intf) < 0)
  5626. return;
  5627. mutex_lock(&tp->control);
  5628. if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
  5629. tp->rtl_fw.retry = false;
  5630. tp->rtl_fw.fw = NULL;
  5631. /* Delay execution in case request_firmware() is not ready yet.
  5632. */
  5633. queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
  5634. goto ignore_once;
  5635. }
  5636. tp->rtl_ops.hw_phy_cfg(tp);
  5637. rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
  5638. tp->advertising);
  5639. ignore_once:
  5640. mutex_unlock(&tp->control);
  5641. usb_autopm_put_interface(tp->intf);
  5642. }
  5643. #ifdef CONFIG_PM_SLEEP
  5644. static int rtl_notifier(struct notifier_block *nb, unsigned long action,
  5645. void *data)
  5646. {
  5647. struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
  5648. switch (action) {
  5649. case PM_HIBERNATION_PREPARE:
  5650. case PM_SUSPEND_PREPARE:
  5651. usb_autopm_get_interface(tp->intf);
  5652. break;
  5653. case PM_POST_HIBERNATION:
  5654. case PM_POST_SUSPEND:
  5655. usb_autopm_put_interface(tp->intf);
  5656. break;
  5657. case PM_POST_RESTORE:
  5658. case PM_RESTORE_PREPARE:
  5659. default:
  5660. break;
  5661. }
  5662. return NOTIFY_DONE;
  5663. }
  5664. #endif
  5665. static int rtl8152_open(struct net_device *netdev)
  5666. {
  5667. struct r8152 *tp = netdev_priv(netdev);
  5668. int res = 0;
  5669. if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
  5670. cancel_delayed_work_sync(&tp->hw_phy_work);
  5671. rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
  5672. }
  5673. res = alloc_all_mem(tp);
  5674. if (res)
  5675. goto out;
  5676. res = usb_autopm_get_interface(tp->intf);
  5677. if (res < 0)
  5678. goto out_free;
  5679. mutex_lock(&tp->control);
  5680. tp->rtl_ops.up(tp);
  5681. netif_carrier_off(netdev);
  5682. netif_start_queue(netdev);
  5683. set_bit(WORK_ENABLE, &tp->flags);
  5684. res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  5685. if (res) {
  5686. if (res == -ENODEV)
  5687. netif_device_detach(tp->netdev);
  5688. netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
  5689. res);
  5690. goto out_unlock;
  5691. }
  5692. napi_enable(&tp->napi);
  5693. tasklet_enable(&tp->tx_tl);
  5694. mutex_unlock(&tp->control);
  5695. usb_autopm_put_interface(tp->intf);
  5696. #ifdef CONFIG_PM_SLEEP
  5697. tp->pm_notifier.notifier_call = rtl_notifier;
  5698. register_pm_notifier(&tp->pm_notifier);
  5699. #endif
  5700. return 0;
  5701. out_unlock:
  5702. mutex_unlock(&tp->control);
  5703. usb_autopm_put_interface(tp->intf);
  5704. out_free:
  5705. free_all_mem(tp);
  5706. out:
  5707. return res;
  5708. }
  5709. static int rtl8152_close(struct net_device *netdev)
  5710. {
  5711. struct r8152 *tp = netdev_priv(netdev);
  5712. int res = 0;
  5713. #ifdef CONFIG_PM_SLEEP
  5714. unregister_pm_notifier(&tp->pm_notifier);
  5715. #endif
  5716. tasklet_disable(&tp->tx_tl);
  5717. clear_bit(WORK_ENABLE, &tp->flags);
  5718. usb_kill_urb(tp->intr_urb);
  5719. cancel_delayed_work_sync(&tp->schedule);
  5720. napi_disable(&tp->napi);
  5721. netif_stop_queue(netdev);
  5722. res = usb_autopm_get_interface(tp->intf);
  5723. if (res < 0 || test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
  5724. rtl_drop_queued_tx(tp);
  5725. rtl_stop_rx(tp);
  5726. } else {
  5727. mutex_lock(&tp->control);
  5728. tp->rtl_ops.down(tp);
  5729. mutex_unlock(&tp->control);
  5730. }
  5731. if (!res)
  5732. usb_autopm_put_interface(tp->intf);
  5733. free_all_mem(tp);
  5734. return res;
  5735. }
  5736. static void rtl_tally_reset(struct r8152 *tp)
  5737. {
  5738. u32 ocp_data;
  5739. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
  5740. ocp_data |= TALLY_RESET;
  5741. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
  5742. }
  5743. static void r8152b_init(struct r8152 *tp)
  5744. {
  5745. u32 ocp_data;
  5746. u16 data;
  5747. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5748. return;
  5749. data = r8152_mdio_read(tp, MII_BMCR);
  5750. if (data & BMCR_PDOWN) {
  5751. data &= ~BMCR_PDOWN;
  5752. r8152_mdio_write(tp, MII_BMCR, data);
  5753. }
  5754. r8152_aldps_en(tp, false);
  5755. if (tp->version == RTL_VER_01) {
  5756. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  5757. ocp_data &= ~LED_MODE_MASK;
  5758. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  5759. }
  5760. r8152_power_cut_en(tp, false);
  5761. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  5762. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  5763. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  5764. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  5765. ocp_data &= ~MCU_CLK_RATIO_MASK;
  5766. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  5767. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  5768. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  5769. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  5770. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  5771. rtl_tally_reset(tp);
  5772. /* enable rx aggregation */
  5773. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  5774. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  5775. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  5776. }
  5777. static void r8153_init(struct r8152 *tp)
  5778. {
  5779. u32 ocp_data;
  5780. u16 data;
  5781. int i;
  5782. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5783. return;
  5784. r8153_u1u2en(tp, false);
  5785. for (i = 0; i < 500; i++) {
  5786. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  5787. AUTOLOAD_DONE)
  5788. break;
  5789. msleep(20);
  5790. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5791. break;
  5792. }
  5793. data = r8153_phy_status(tp, 0);
  5794. if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
  5795. tp->version == RTL_VER_05)
  5796. ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
  5797. data = r8152_mdio_read(tp, MII_BMCR);
  5798. if (data & BMCR_PDOWN) {
  5799. data &= ~BMCR_PDOWN;
  5800. r8152_mdio_write(tp, MII_BMCR, data);
  5801. }
  5802. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  5803. r8153_u2p3en(tp, false);
  5804. if (tp->version == RTL_VER_04) {
  5805. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
  5806. ocp_data &= ~pwd_dn_scale_mask;
  5807. ocp_data |= pwd_dn_scale(96);
  5808. ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
  5809. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
  5810. ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
  5811. ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
  5812. } else if (tp->version == RTL_VER_05) {
  5813. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
  5814. ocp_data &= ~ECM_ALDPS;
  5815. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
  5816. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  5817. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  5818. ocp_data &= ~DYNAMIC_BURST;
  5819. else
  5820. ocp_data |= DYNAMIC_BURST;
  5821. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  5822. } else if (tp->version == RTL_VER_06) {
  5823. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  5824. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  5825. ocp_data &= ~DYNAMIC_BURST;
  5826. else
  5827. ocp_data |= DYNAMIC_BURST;
  5828. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  5829. r8153_queue_wake(tp, false);
  5830. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  5831. if (rtl8152_get_speed(tp) & LINK_STATUS)
  5832. ocp_data |= CUR_LINK_OK;
  5833. else
  5834. ocp_data &= ~CUR_LINK_OK;
  5835. ocp_data |= POLL_LINK_CHG;
  5836. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  5837. }
  5838. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
  5839. ocp_data |= EP4_FULL_FC;
  5840. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
  5841. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
  5842. ocp_data &= ~TIMER11_EN;
  5843. ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
  5844. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  5845. ocp_data &= ~LED_MODE_MASK;
  5846. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  5847. ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
  5848. if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
  5849. ocp_data |= LPM_TIMER_500MS;
  5850. else
  5851. ocp_data |= LPM_TIMER_500US;
  5852. ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
  5853. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
  5854. ocp_data &= ~SEN_VAL_MASK;
  5855. ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
  5856. ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
  5857. ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
  5858. r8153_power_cut_en(tp, false);
  5859. rtl_runtime_suspend_enable(tp, false);
  5860. r8153_mac_clk_speed_down(tp, false);
  5861. r8153_u1u2en(tp, true);
  5862. usb_enable_lpm(tp->udev);
  5863. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
  5864. ocp_data |= LANWAKE_CLR_EN;
  5865. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
  5866. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
  5867. ocp_data &= ~LANWAKE_PIN;
  5868. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
  5869. /* rx aggregation */
  5870. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  5871. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  5872. if (tp->dell_tb_rx_agg_bug)
  5873. ocp_data |= RX_AGG_DISABLE;
  5874. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  5875. rtl_tally_reset(tp);
  5876. switch (tp->udev->speed) {
  5877. case USB_SPEED_SUPER:
  5878. case USB_SPEED_SUPER_PLUS:
  5879. tp->coalesce = COALESCE_SUPER;
  5880. break;
  5881. case USB_SPEED_HIGH:
  5882. tp->coalesce = COALESCE_HIGH;
  5883. break;
  5884. default:
  5885. tp->coalesce = COALESCE_SLOW;
  5886. break;
  5887. }
  5888. }
  5889. static void r8153b_init(struct r8152 *tp)
  5890. {
  5891. u32 ocp_data;
  5892. u16 data;
  5893. int i;
  5894. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5895. return;
  5896. r8153b_u1u2en(tp, false);
  5897. for (i = 0; i < 500; i++) {
  5898. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  5899. AUTOLOAD_DONE)
  5900. break;
  5901. msleep(20);
  5902. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5903. break;
  5904. }
  5905. data = r8153_phy_status(tp, 0);
  5906. data = r8152_mdio_read(tp, MII_BMCR);
  5907. if (data & BMCR_PDOWN) {
  5908. data &= ~BMCR_PDOWN;
  5909. r8152_mdio_write(tp, MII_BMCR, data);
  5910. }
  5911. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  5912. r8153_u2p3en(tp, false);
  5913. /* MSC timer = 0xfff * 8ms = 32760 ms */
  5914. ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
  5915. r8153b_power_cut_en(tp, false);
  5916. r8153b_ups_en(tp, false);
  5917. r8153_queue_wake(tp, false);
  5918. rtl_runtime_suspend_enable(tp, false);
  5919. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  5920. if (rtl8152_get_speed(tp) & LINK_STATUS)
  5921. ocp_data |= CUR_LINK_OK;
  5922. else
  5923. ocp_data &= ~CUR_LINK_OK;
  5924. ocp_data |= POLL_LINK_CHG;
  5925. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  5926. if (tp->udev->speed >= USB_SPEED_SUPER)
  5927. r8153b_u1u2en(tp, true);
  5928. usb_enable_lpm(tp->udev);
  5929. /* MAC clock speed down */
  5930. r8153_mac_clk_speed_down(tp, true);
  5931. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  5932. ocp_data &= ~PLA_MCU_SPDWN_EN;
  5933. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  5934. if (tp->version == RTL_VER_09) {
  5935. /* Disable Test IO for 32QFN */
  5936. if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
  5937. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  5938. ocp_data |= TEST_IO_OFF;
  5939. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  5940. }
  5941. }
  5942. set_bit(GREEN_ETHERNET, &tp->flags);
  5943. /* rx aggregation */
  5944. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  5945. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  5946. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  5947. rtl_tally_reset(tp);
  5948. tp->coalesce = 15000; /* 15 us */
  5949. }
  5950. static void r8153c_init(struct r8152 *tp)
  5951. {
  5952. u32 ocp_data;
  5953. u16 data;
  5954. int i;
  5955. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5956. return;
  5957. r8153b_u1u2en(tp, false);
  5958. /* Disable spi_en */
  5959. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  5960. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  5961. ocp_data &= ~BIT(3);
  5962. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
  5963. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
  5964. ocp_data |= BIT(1);
  5965. ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
  5966. for (i = 0; i < 500; i++) {
  5967. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  5968. AUTOLOAD_DONE)
  5969. break;
  5970. msleep(20);
  5971. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  5972. return;
  5973. }
  5974. data = r8153_phy_status(tp, 0);
  5975. data = r8152_mdio_read(tp, MII_BMCR);
  5976. if (data & BMCR_PDOWN) {
  5977. data &= ~BMCR_PDOWN;
  5978. r8152_mdio_write(tp, MII_BMCR, data);
  5979. }
  5980. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  5981. r8153_u2p3en(tp, false);
  5982. /* MSC timer = 0xfff * 8ms = 32760 ms */
  5983. ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
  5984. r8153b_power_cut_en(tp, false);
  5985. r8153c_ups_en(tp, false);
  5986. r8153_queue_wake(tp, false);
  5987. rtl_runtime_suspend_enable(tp, false);
  5988. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  5989. if (rtl8152_get_speed(tp) & LINK_STATUS)
  5990. ocp_data |= CUR_LINK_OK;
  5991. else
  5992. ocp_data &= ~CUR_LINK_OK;
  5993. ocp_data |= POLL_LINK_CHG;
  5994. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  5995. r8153b_u1u2en(tp, true);
  5996. usb_enable_lpm(tp->udev);
  5997. /* MAC clock speed down */
  5998. r8153_mac_clk_speed_down(tp, true);
  5999. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
  6000. ocp_data &= ~BIT(7);
  6001. ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
  6002. set_bit(GREEN_ETHERNET, &tp->flags);
  6003. /* rx aggregation */
  6004. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  6005. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  6006. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  6007. rtl_tally_reset(tp);
  6008. tp->coalesce = 15000; /* 15 us */
  6009. }
  6010. static void r8156_hw_phy_cfg(struct r8152 *tp)
  6011. {
  6012. u32 ocp_data;
  6013. u16 data;
  6014. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  6015. if (ocp_data & PCUT_STATUS) {
  6016. ocp_data &= ~PCUT_STATUS;
  6017. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  6018. }
  6019. data = r8153_phy_status(tp, 0);
  6020. switch (data) {
  6021. case PHY_STAT_EXT_INIT:
  6022. rtl8152_apply_firmware(tp, true);
  6023. data = ocp_reg_read(tp, 0xa468);
  6024. data &= ~(BIT(3) | BIT(1));
  6025. ocp_reg_write(tp, 0xa468, data);
  6026. break;
  6027. case PHY_STAT_LAN_ON:
  6028. case PHY_STAT_PWRDN:
  6029. default:
  6030. rtl8152_apply_firmware(tp, false);
  6031. break;
  6032. }
  6033. /* disable ALDPS before updating the PHY parameters */
  6034. r8153_aldps_en(tp, false);
  6035. /* disable EEE before updating the PHY parameters */
  6036. rtl_eee_enable(tp, false);
  6037. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  6038. WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
  6039. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  6040. ocp_data |= PFM_PWM_SWITCH;
  6041. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  6042. switch (tp->version) {
  6043. case RTL_VER_10:
  6044. data = ocp_reg_read(tp, 0xad40);
  6045. data &= ~0x3ff;
  6046. data |= BIT(7) | BIT(2);
  6047. ocp_reg_write(tp, 0xad40, data);
  6048. data = ocp_reg_read(tp, 0xad4e);
  6049. data |= BIT(4);
  6050. ocp_reg_write(tp, 0xad4e, data);
  6051. data = ocp_reg_read(tp, 0xad16);
  6052. data &= ~0x3ff;
  6053. data |= 0x6;
  6054. ocp_reg_write(tp, 0xad16, data);
  6055. data = ocp_reg_read(tp, 0xad32);
  6056. data &= ~0x3f;
  6057. data |= 6;
  6058. ocp_reg_write(tp, 0xad32, data);
  6059. data = ocp_reg_read(tp, 0xac08);
  6060. data &= ~(BIT(12) | BIT(8));
  6061. ocp_reg_write(tp, 0xac08, data);
  6062. data = ocp_reg_read(tp, 0xac8a);
  6063. data |= BIT(12) | BIT(13) | BIT(14);
  6064. data &= ~BIT(15);
  6065. ocp_reg_write(tp, 0xac8a, data);
  6066. data = ocp_reg_read(tp, 0xad18);
  6067. data |= BIT(10);
  6068. ocp_reg_write(tp, 0xad18, data);
  6069. data = ocp_reg_read(tp, 0xad1a);
  6070. data |= 0x3ff;
  6071. ocp_reg_write(tp, 0xad1a, data);
  6072. data = ocp_reg_read(tp, 0xad1c);
  6073. data |= 0x3ff;
  6074. ocp_reg_write(tp, 0xad1c, data);
  6075. data = sram_read(tp, 0x80ea);
  6076. data &= ~0xff00;
  6077. data |= 0xc400;
  6078. sram_write(tp, 0x80ea, data);
  6079. data = sram_read(tp, 0x80eb);
  6080. data &= ~0x0700;
  6081. data |= 0x0300;
  6082. sram_write(tp, 0x80eb, data);
  6083. data = sram_read(tp, 0x80f8);
  6084. data &= ~0xff00;
  6085. data |= 0x1c00;
  6086. sram_write(tp, 0x80f8, data);
  6087. data = sram_read(tp, 0x80f1);
  6088. data &= ~0xff00;
  6089. data |= 0x3000;
  6090. sram_write(tp, 0x80f1, data);
  6091. data = sram_read(tp, 0x80fe);
  6092. data &= ~0xff00;
  6093. data |= 0xa500;
  6094. sram_write(tp, 0x80fe, data);
  6095. data = sram_read(tp, 0x8102);
  6096. data &= ~0xff00;
  6097. data |= 0x5000;
  6098. sram_write(tp, 0x8102, data);
  6099. data = sram_read(tp, 0x8015);
  6100. data &= ~0xff00;
  6101. data |= 0x3300;
  6102. sram_write(tp, 0x8015, data);
  6103. data = sram_read(tp, 0x8100);
  6104. data &= ~0xff00;
  6105. data |= 0x7000;
  6106. sram_write(tp, 0x8100, data);
  6107. data = sram_read(tp, 0x8014);
  6108. data &= ~0xff00;
  6109. data |= 0xf000;
  6110. sram_write(tp, 0x8014, data);
  6111. data = sram_read(tp, 0x8016);
  6112. data &= ~0xff00;
  6113. data |= 0x6500;
  6114. sram_write(tp, 0x8016, data);
  6115. data = sram_read(tp, 0x80dc);
  6116. data &= ~0xff00;
  6117. data |= 0xed00;
  6118. sram_write(tp, 0x80dc, data);
  6119. data = sram_read(tp, 0x80df);
  6120. data |= BIT(8);
  6121. sram_write(tp, 0x80df, data);
  6122. data = sram_read(tp, 0x80e1);
  6123. data &= ~BIT(8);
  6124. sram_write(tp, 0x80e1, data);
  6125. data = ocp_reg_read(tp, 0xbf06);
  6126. data &= ~0x003f;
  6127. data |= 0x0038;
  6128. ocp_reg_write(tp, 0xbf06, data);
  6129. sram_write(tp, 0x819f, 0xddb6);
  6130. ocp_reg_write(tp, 0xbc34, 0x5555);
  6131. data = ocp_reg_read(tp, 0xbf0a);
  6132. data &= ~0x0e00;
  6133. data |= 0x0a00;
  6134. ocp_reg_write(tp, 0xbf0a, data);
  6135. data = ocp_reg_read(tp, 0xbd2c);
  6136. data &= ~BIT(13);
  6137. ocp_reg_write(tp, 0xbd2c, data);
  6138. break;
  6139. case RTL_VER_11:
  6140. data = ocp_reg_read(tp, 0xad16);
  6141. data |= 0x3ff;
  6142. ocp_reg_write(tp, 0xad16, data);
  6143. data = ocp_reg_read(tp, 0xad32);
  6144. data &= ~0x3f;
  6145. data |= 6;
  6146. ocp_reg_write(tp, 0xad32, data);
  6147. data = ocp_reg_read(tp, 0xac08);
  6148. data &= ~(BIT(12) | BIT(8));
  6149. ocp_reg_write(tp, 0xac08, data);
  6150. data = ocp_reg_read(tp, 0xacc0);
  6151. data &= ~0x3;
  6152. data |= BIT(1);
  6153. ocp_reg_write(tp, 0xacc0, data);
  6154. data = ocp_reg_read(tp, 0xad40);
  6155. data &= ~0xe7;
  6156. data |= BIT(6) | BIT(2);
  6157. ocp_reg_write(tp, 0xad40, data);
  6158. data = ocp_reg_read(tp, 0xac14);
  6159. data &= ~BIT(7);
  6160. ocp_reg_write(tp, 0xac14, data);
  6161. data = ocp_reg_read(tp, 0xac80);
  6162. data &= ~(BIT(8) | BIT(9));
  6163. ocp_reg_write(tp, 0xac80, data);
  6164. data = ocp_reg_read(tp, 0xac5e);
  6165. data &= ~0x7;
  6166. data |= BIT(1);
  6167. ocp_reg_write(tp, 0xac5e, data);
  6168. ocp_reg_write(tp, 0xad4c, 0x00a8);
  6169. ocp_reg_write(tp, 0xac5c, 0x01ff);
  6170. data = ocp_reg_read(tp, 0xac8a);
  6171. data &= ~0xf0;
  6172. data |= BIT(4) | BIT(5);
  6173. ocp_reg_write(tp, 0xac8a, data);
  6174. ocp_reg_write(tp, 0xb87c, 0x8157);
  6175. data = ocp_reg_read(tp, 0xb87e);
  6176. data &= ~0xff00;
  6177. data |= 0x0500;
  6178. ocp_reg_write(tp, 0xb87e, data);
  6179. ocp_reg_write(tp, 0xb87c, 0x8159);
  6180. data = ocp_reg_read(tp, 0xb87e);
  6181. data &= ~0xff00;
  6182. data |= 0x0700;
  6183. ocp_reg_write(tp, 0xb87e, data);
  6184. /* AAGC */
  6185. ocp_reg_write(tp, 0xb87c, 0x80a2);
  6186. ocp_reg_write(tp, 0xb87e, 0x0153);
  6187. ocp_reg_write(tp, 0xb87c, 0x809c);
  6188. ocp_reg_write(tp, 0xb87e, 0x0153);
  6189. /* EEE parameter */
  6190. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
  6191. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
  6192. ocp_data |= EN_XG_LIP | EN_G_LIP;
  6193. ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
  6194. sram_write(tp, 0x8257, 0x020f); /* XG PLL */
  6195. sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
  6196. if (rtl_phy_patch_request(tp, true, true))
  6197. return;
  6198. /* Advance EEE */
  6199. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  6200. ocp_data |= EEE_SPDWN_EN;
  6201. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  6202. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  6203. data &= ~(EN_EEE_100 | EN_EEE_1000);
  6204. data |= EN_10M_CLKDIV;
  6205. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  6206. tp->ups_info._10m_ckdiv = true;
  6207. tp->ups_info.eee_plloff_100 = false;
  6208. tp->ups_info.eee_plloff_giga = false;
  6209. data = ocp_reg_read(tp, OCP_POWER_CFG);
  6210. data &= ~EEE_CLKDIV_EN;
  6211. ocp_reg_write(tp, OCP_POWER_CFG, data);
  6212. tp->ups_info.eee_ckdiv = false;
  6213. ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
  6214. ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
  6215. tp->ups_info._250m_ckdiv = false;
  6216. rtl_phy_patch_request(tp, false, true);
  6217. /* enable ADC Ibias Cal */
  6218. data = ocp_reg_read(tp, 0xd068);
  6219. data |= BIT(13);
  6220. ocp_reg_write(tp, 0xd068, data);
  6221. /* enable Thermal Sensor */
  6222. data = sram_read(tp, 0x81a2);
  6223. data &= ~BIT(8);
  6224. sram_write(tp, 0x81a2, data);
  6225. data = ocp_reg_read(tp, 0xb54c);
  6226. data &= ~0xff00;
  6227. data |= 0xdb00;
  6228. ocp_reg_write(tp, 0xb54c, data);
  6229. /* Nway 2.5G Lite */
  6230. data = ocp_reg_read(tp, 0xa454);
  6231. data &= ~BIT(0);
  6232. ocp_reg_write(tp, 0xa454, data);
  6233. /* CS DSP solution */
  6234. data = ocp_reg_read(tp, OCP_10GBT_CTRL);
  6235. data |= RTL_ADV2_5G_F_R;
  6236. ocp_reg_write(tp, OCP_10GBT_CTRL, data);
  6237. data = ocp_reg_read(tp, 0xad4e);
  6238. data &= ~BIT(4);
  6239. ocp_reg_write(tp, 0xad4e, data);
  6240. data = ocp_reg_read(tp, 0xa86a);
  6241. data &= ~BIT(0);
  6242. ocp_reg_write(tp, 0xa86a, data);
  6243. /* MDI SWAP */
  6244. if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
  6245. (ocp_reg_read(tp, 0xd068) & BIT(1))) {
  6246. u16 swap_a, swap_b;
  6247. data = ocp_reg_read(tp, 0xd068);
  6248. data &= ~0x1f;
  6249. data |= 0x1; /* p0 */
  6250. ocp_reg_write(tp, 0xd068, data);
  6251. swap_a = ocp_reg_read(tp, 0xd06a);
  6252. data &= ~0x18;
  6253. data |= 0x18; /* p3 */
  6254. ocp_reg_write(tp, 0xd068, data);
  6255. swap_b = ocp_reg_read(tp, 0xd06a);
  6256. data &= ~0x18; /* p0 */
  6257. ocp_reg_write(tp, 0xd068, data);
  6258. ocp_reg_write(tp, 0xd06a,
  6259. (swap_a & ~0x7ff) | (swap_b & 0x7ff));
  6260. data |= 0x18; /* p3 */
  6261. ocp_reg_write(tp, 0xd068, data);
  6262. ocp_reg_write(tp, 0xd06a,
  6263. (swap_b & ~0x7ff) | (swap_a & 0x7ff));
  6264. data &= ~0x18;
  6265. data |= 0x08; /* p1 */
  6266. ocp_reg_write(tp, 0xd068, data);
  6267. swap_a = ocp_reg_read(tp, 0xd06a);
  6268. data &= ~0x18;
  6269. data |= 0x10; /* p2 */
  6270. ocp_reg_write(tp, 0xd068, data);
  6271. swap_b = ocp_reg_read(tp, 0xd06a);
  6272. data &= ~0x18;
  6273. data |= 0x08; /* p1 */
  6274. ocp_reg_write(tp, 0xd068, data);
  6275. ocp_reg_write(tp, 0xd06a,
  6276. (swap_a & ~0x7ff) | (swap_b & 0x7ff));
  6277. data &= ~0x18;
  6278. data |= 0x10; /* p2 */
  6279. ocp_reg_write(tp, 0xd068, data);
  6280. ocp_reg_write(tp, 0xd06a,
  6281. (swap_b & ~0x7ff) | (swap_a & 0x7ff));
  6282. swap_a = ocp_reg_read(tp, 0xbd5a);
  6283. swap_b = ocp_reg_read(tp, 0xbd5c);
  6284. ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
  6285. ((swap_b & 0x1f) << 8) |
  6286. ((swap_b >> 8) & 0x1f));
  6287. ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
  6288. ((swap_a & 0x1f) << 8) |
  6289. ((swap_a >> 8) & 0x1f));
  6290. swap_a = ocp_reg_read(tp, 0xbc18);
  6291. swap_b = ocp_reg_read(tp, 0xbc1a);
  6292. ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
  6293. ((swap_b & 0x1f) << 8) |
  6294. ((swap_b >> 8) & 0x1f));
  6295. ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
  6296. ((swap_a & 0x1f) << 8) |
  6297. ((swap_a >> 8) & 0x1f));
  6298. }
  6299. /* Notify the MAC when the speed is changed to force mode. */
  6300. data = ocp_reg_read(tp, OCP_INTR_EN);
  6301. data |= INTR_SPEED_FORCE;
  6302. ocp_reg_write(tp, OCP_INTR_EN, data);
  6303. break;
  6304. default:
  6305. break;
  6306. }
  6307. rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
  6308. data = ocp_reg_read(tp, 0xa428);
  6309. data &= ~BIT(9);
  6310. ocp_reg_write(tp, 0xa428, data);
  6311. data = ocp_reg_read(tp, 0xa5ea);
  6312. data &= ~BIT(0);
  6313. ocp_reg_write(tp, 0xa5ea, data);
  6314. tp->ups_info.lite_mode = 0;
  6315. if (tp->eee_en)
  6316. rtl_eee_enable(tp, true);
  6317. r8153_aldps_en(tp, true);
  6318. r8152b_enable_fc(tp);
  6319. r8153_u2p3en(tp, true);
  6320. set_bit(PHY_RESET, &tp->flags);
  6321. }
  6322. static void r8156b_hw_phy_cfg(struct r8152 *tp)
  6323. {
  6324. u32 ocp_data;
  6325. u16 data;
  6326. switch (tp->version) {
  6327. case RTL_VER_12:
  6328. ocp_reg_write(tp, 0xbf86, 0x9000);
  6329. data = ocp_reg_read(tp, 0xc402);
  6330. data |= BIT(10);
  6331. ocp_reg_write(tp, 0xc402, data);
  6332. data &= ~BIT(10);
  6333. ocp_reg_write(tp, 0xc402, data);
  6334. ocp_reg_write(tp, 0xbd86, 0x1010);
  6335. ocp_reg_write(tp, 0xbd88, 0x1010);
  6336. data = ocp_reg_read(tp, 0xbd4e);
  6337. data &= ~(BIT(10) | BIT(11));
  6338. data |= BIT(11);
  6339. ocp_reg_write(tp, 0xbd4e, data);
  6340. data = ocp_reg_read(tp, 0xbf46);
  6341. data &= ~0xf00;
  6342. data |= 0x700;
  6343. ocp_reg_write(tp, 0xbf46, data);
  6344. break;
  6345. case RTL_VER_13:
  6346. case RTL_VER_15:
  6347. r8156b_wait_loading_flash(tp);
  6348. break;
  6349. default:
  6350. break;
  6351. }
  6352. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  6353. if (ocp_data & PCUT_STATUS) {
  6354. ocp_data &= ~PCUT_STATUS;
  6355. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  6356. }
  6357. data = r8153_phy_status(tp, 0);
  6358. switch (data) {
  6359. case PHY_STAT_EXT_INIT:
  6360. rtl8152_apply_firmware(tp, true);
  6361. data = ocp_reg_read(tp, 0xa466);
  6362. data &= ~BIT(0);
  6363. ocp_reg_write(tp, 0xa466, data);
  6364. data = ocp_reg_read(tp, 0xa468);
  6365. data &= ~(BIT(3) | BIT(1));
  6366. ocp_reg_write(tp, 0xa468, data);
  6367. break;
  6368. case PHY_STAT_LAN_ON:
  6369. case PHY_STAT_PWRDN:
  6370. default:
  6371. rtl8152_apply_firmware(tp, false);
  6372. break;
  6373. }
  6374. data = r8152_mdio_read(tp, MII_BMCR);
  6375. if (data & BMCR_PDOWN) {
  6376. data &= ~BMCR_PDOWN;
  6377. r8152_mdio_write(tp, MII_BMCR, data);
  6378. }
  6379. /* disable ALDPS before updating the PHY parameters */
  6380. r8153_aldps_en(tp, false);
  6381. /* disable EEE before updating the PHY parameters */
  6382. rtl_eee_enable(tp, false);
  6383. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  6384. WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
  6385. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  6386. ocp_data |= PFM_PWM_SWITCH;
  6387. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  6388. switch (tp->version) {
  6389. case RTL_VER_12:
  6390. data = ocp_reg_read(tp, 0xbc08);
  6391. data |= BIT(3) | BIT(2);
  6392. ocp_reg_write(tp, 0xbc08, data);
  6393. data = sram_read(tp, 0x8fff);
  6394. data &= ~0xff00;
  6395. data |= 0x0400;
  6396. sram_write(tp, 0x8fff, data);
  6397. data = ocp_reg_read(tp, 0xacda);
  6398. data |= 0xff00;
  6399. ocp_reg_write(tp, 0xacda, data);
  6400. data = ocp_reg_read(tp, 0xacde);
  6401. data |= 0xf000;
  6402. ocp_reg_write(tp, 0xacde, data);
  6403. ocp_reg_write(tp, 0xac8c, 0x0ffc);
  6404. ocp_reg_write(tp, 0xac46, 0xb7b4);
  6405. ocp_reg_write(tp, 0xac50, 0x0fbc);
  6406. ocp_reg_write(tp, 0xac3c, 0x9240);
  6407. ocp_reg_write(tp, 0xac4e, 0x0db4);
  6408. ocp_reg_write(tp, 0xacc6, 0x0707);
  6409. ocp_reg_write(tp, 0xacc8, 0xa0d3);
  6410. ocp_reg_write(tp, 0xad08, 0x0007);
  6411. ocp_reg_write(tp, 0xb87c, 0x8560);
  6412. ocp_reg_write(tp, 0xb87e, 0x19cc);
  6413. ocp_reg_write(tp, 0xb87c, 0x8562);
  6414. ocp_reg_write(tp, 0xb87e, 0x19cc);
  6415. ocp_reg_write(tp, 0xb87c, 0x8564);
  6416. ocp_reg_write(tp, 0xb87e, 0x19cc);
  6417. ocp_reg_write(tp, 0xb87c, 0x8566);
  6418. ocp_reg_write(tp, 0xb87e, 0x147d);
  6419. ocp_reg_write(tp, 0xb87c, 0x8568);
  6420. ocp_reg_write(tp, 0xb87e, 0x147d);
  6421. ocp_reg_write(tp, 0xb87c, 0x856a);
  6422. ocp_reg_write(tp, 0xb87e, 0x147d);
  6423. ocp_reg_write(tp, 0xb87c, 0x8ffe);
  6424. ocp_reg_write(tp, 0xb87e, 0x0907);
  6425. ocp_reg_write(tp, 0xb87c, 0x80d6);
  6426. ocp_reg_write(tp, 0xb87e, 0x2801);
  6427. ocp_reg_write(tp, 0xb87c, 0x80f2);
  6428. ocp_reg_write(tp, 0xb87e, 0x2801);
  6429. ocp_reg_write(tp, 0xb87c, 0x80f4);
  6430. ocp_reg_write(tp, 0xb87e, 0x6077);
  6431. ocp_reg_write(tp, 0xb506, 0x01e7);
  6432. ocp_reg_write(tp, 0xb87c, 0x8013);
  6433. ocp_reg_write(tp, 0xb87e, 0x0700);
  6434. ocp_reg_write(tp, 0xb87c, 0x8fb9);
  6435. ocp_reg_write(tp, 0xb87e, 0x2801);
  6436. ocp_reg_write(tp, 0xb87c, 0x8fba);
  6437. ocp_reg_write(tp, 0xb87e, 0x0100);
  6438. ocp_reg_write(tp, 0xb87c, 0x8fbc);
  6439. ocp_reg_write(tp, 0xb87e, 0x1900);
  6440. ocp_reg_write(tp, 0xb87c, 0x8fbe);
  6441. ocp_reg_write(tp, 0xb87e, 0xe100);
  6442. ocp_reg_write(tp, 0xb87c, 0x8fc0);
  6443. ocp_reg_write(tp, 0xb87e, 0x0800);
  6444. ocp_reg_write(tp, 0xb87c, 0x8fc2);
  6445. ocp_reg_write(tp, 0xb87e, 0xe500);
  6446. ocp_reg_write(tp, 0xb87c, 0x8fc4);
  6447. ocp_reg_write(tp, 0xb87e, 0x0f00);
  6448. ocp_reg_write(tp, 0xb87c, 0x8fc6);
  6449. ocp_reg_write(tp, 0xb87e, 0xf100);
  6450. ocp_reg_write(tp, 0xb87c, 0x8fc8);
  6451. ocp_reg_write(tp, 0xb87e, 0x0400);
  6452. ocp_reg_write(tp, 0xb87c, 0x8fca);
  6453. ocp_reg_write(tp, 0xb87e, 0xf300);
  6454. ocp_reg_write(tp, 0xb87c, 0x8fcc);
  6455. ocp_reg_write(tp, 0xb87e, 0xfd00);
  6456. ocp_reg_write(tp, 0xb87c, 0x8fce);
  6457. ocp_reg_write(tp, 0xb87e, 0xff00);
  6458. ocp_reg_write(tp, 0xb87c, 0x8fd0);
  6459. ocp_reg_write(tp, 0xb87e, 0xfb00);
  6460. ocp_reg_write(tp, 0xb87c, 0x8fd2);
  6461. ocp_reg_write(tp, 0xb87e, 0x0100);
  6462. ocp_reg_write(tp, 0xb87c, 0x8fd4);
  6463. ocp_reg_write(tp, 0xb87e, 0xf400);
  6464. ocp_reg_write(tp, 0xb87c, 0x8fd6);
  6465. ocp_reg_write(tp, 0xb87e, 0xff00);
  6466. ocp_reg_write(tp, 0xb87c, 0x8fd8);
  6467. ocp_reg_write(tp, 0xb87e, 0xf600);
  6468. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
  6469. ocp_data |= EN_XG_LIP | EN_G_LIP;
  6470. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
  6471. ocp_reg_write(tp, 0xb87c, 0x813d);
  6472. ocp_reg_write(tp, 0xb87e, 0x390e);
  6473. ocp_reg_write(tp, 0xb87c, 0x814f);
  6474. ocp_reg_write(tp, 0xb87e, 0x790e);
  6475. ocp_reg_write(tp, 0xb87c, 0x80b0);
  6476. ocp_reg_write(tp, 0xb87e, 0x0f31);
  6477. data = ocp_reg_read(tp, 0xbf4c);
  6478. data |= BIT(1);
  6479. ocp_reg_write(tp, 0xbf4c, data);
  6480. data = ocp_reg_read(tp, 0xbcca);
  6481. data |= BIT(9) | BIT(8);
  6482. ocp_reg_write(tp, 0xbcca, data);
  6483. ocp_reg_write(tp, 0xb87c, 0x8141);
  6484. ocp_reg_write(tp, 0xb87e, 0x320e);
  6485. ocp_reg_write(tp, 0xb87c, 0x8153);
  6486. ocp_reg_write(tp, 0xb87e, 0x720e);
  6487. ocp_reg_write(tp, 0xb87c, 0x8529);
  6488. ocp_reg_write(tp, 0xb87e, 0x050e);
  6489. data = ocp_reg_read(tp, OCP_EEE_CFG);
  6490. data &= ~CTAP_SHORT_EN;
  6491. ocp_reg_write(tp, OCP_EEE_CFG, data);
  6492. sram_write(tp, 0x816c, 0xc4a0);
  6493. sram_write(tp, 0x8170, 0xc4a0);
  6494. sram_write(tp, 0x8174, 0x04a0);
  6495. sram_write(tp, 0x8178, 0x04a0);
  6496. sram_write(tp, 0x817c, 0x0719);
  6497. sram_write(tp, 0x8ff4, 0x0400);
  6498. sram_write(tp, 0x8ff1, 0x0404);
  6499. ocp_reg_write(tp, 0xbf4a, 0x001b);
  6500. ocp_reg_write(tp, 0xb87c, 0x8033);
  6501. ocp_reg_write(tp, 0xb87e, 0x7c13);
  6502. ocp_reg_write(tp, 0xb87c, 0x8037);
  6503. ocp_reg_write(tp, 0xb87e, 0x7c13);
  6504. ocp_reg_write(tp, 0xb87c, 0x803b);
  6505. ocp_reg_write(tp, 0xb87e, 0xfc32);
  6506. ocp_reg_write(tp, 0xb87c, 0x803f);
  6507. ocp_reg_write(tp, 0xb87e, 0x7c13);
  6508. ocp_reg_write(tp, 0xb87c, 0x8043);
  6509. ocp_reg_write(tp, 0xb87e, 0x7c13);
  6510. ocp_reg_write(tp, 0xb87c, 0x8047);
  6511. ocp_reg_write(tp, 0xb87e, 0x7c13);
  6512. ocp_reg_write(tp, 0xb87c, 0x8145);
  6513. ocp_reg_write(tp, 0xb87e, 0x370e);
  6514. ocp_reg_write(tp, 0xb87c, 0x8157);
  6515. ocp_reg_write(tp, 0xb87e, 0x770e);
  6516. ocp_reg_write(tp, 0xb87c, 0x8169);
  6517. ocp_reg_write(tp, 0xb87e, 0x0d0a);
  6518. ocp_reg_write(tp, 0xb87c, 0x817b);
  6519. ocp_reg_write(tp, 0xb87e, 0x1d0a);
  6520. data = sram_read(tp, 0x8217);
  6521. data &= ~0xff00;
  6522. data |= 0x5000;
  6523. sram_write(tp, 0x8217, data);
  6524. data = sram_read(tp, 0x821a);
  6525. data &= ~0xff00;
  6526. data |= 0x5000;
  6527. sram_write(tp, 0x821a, data);
  6528. sram_write(tp, 0x80da, 0x0403);
  6529. data = sram_read(tp, 0x80dc);
  6530. data &= ~0xff00;
  6531. data |= 0x1000;
  6532. sram_write(tp, 0x80dc, data);
  6533. sram_write(tp, 0x80b3, 0x0384);
  6534. sram_write(tp, 0x80b7, 0x2007);
  6535. data = sram_read(tp, 0x80ba);
  6536. data &= ~0xff00;
  6537. data |= 0x6c00;
  6538. sram_write(tp, 0x80ba, data);
  6539. sram_write(tp, 0x80b5, 0xf009);
  6540. data = sram_read(tp, 0x80bd);
  6541. data &= ~0xff00;
  6542. data |= 0x9f00;
  6543. sram_write(tp, 0x80bd, data);
  6544. sram_write(tp, 0x80c7, 0xf083);
  6545. sram_write(tp, 0x80dd, 0x03f0);
  6546. data = sram_read(tp, 0x80df);
  6547. data &= ~0xff00;
  6548. data |= 0x1000;
  6549. sram_write(tp, 0x80df, data);
  6550. sram_write(tp, 0x80cb, 0x2007);
  6551. data = sram_read(tp, 0x80ce);
  6552. data &= ~0xff00;
  6553. data |= 0x6c00;
  6554. sram_write(tp, 0x80ce, data);
  6555. sram_write(tp, 0x80c9, 0x8009);
  6556. data = sram_read(tp, 0x80d1);
  6557. data &= ~0xff00;
  6558. data |= 0x8000;
  6559. sram_write(tp, 0x80d1, data);
  6560. sram_write(tp, 0x80a3, 0x200a);
  6561. sram_write(tp, 0x80a5, 0xf0ad);
  6562. sram_write(tp, 0x809f, 0x6073);
  6563. sram_write(tp, 0x80a1, 0x000b);
  6564. data = sram_read(tp, 0x80a9);
  6565. data &= ~0xff00;
  6566. data |= 0xc000;
  6567. sram_write(tp, 0x80a9, data);
  6568. if (rtl_phy_patch_request(tp, true, true))
  6569. return;
  6570. data = ocp_reg_read(tp, 0xb896);
  6571. data &= ~BIT(0);
  6572. ocp_reg_write(tp, 0xb896, data);
  6573. data = ocp_reg_read(tp, 0xb892);
  6574. data &= ~0xff00;
  6575. ocp_reg_write(tp, 0xb892, data);
  6576. ocp_reg_write(tp, 0xb88e, 0xc23e);
  6577. ocp_reg_write(tp, 0xb890, 0x0000);
  6578. ocp_reg_write(tp, 0xb88e, 0xc240);
  6579. ocp_reg_write(tp, 0xb890, 0x0103);
  6580. ocp_reg_write(tp, 0xb88e, 0xc242);
  6581. ocp_reg_write(tp, 0xb890, 0x0507);
  6582. ocp_reg_write(tp, 0xb88e, 0xc244);
  6583. ocp_reg_write(tp, 0xb890, 0x090b);
  6584. ocp_reg_write(tp, 0xb88e, 0xc246);
  6585. ocp_reg_write(tp, 0xb890, 0x0c0e);
  6586. ocp_reg_write(tp, 0xb88e, 0xc248);
  6587. ocp_reg_write(tp, 0xb890, 0x1012);
  6588. ocp_reg_write(tp, 0xb88e, 0xc24a);
  6589. ocp_reg_write(tp, 0xb890, 0x1416);
  6590. data = ocp_reg_read(tp, 0xb896);
  6591. data |= BIT(0);
  6592. ocp_reg_write(tp, 0xb896, data);
  6593. rtl_phy_patch_request(tp, false, true);
  6594. data = ocp_reg_read(tp, 0xa86a);
  6595. data |= BIT(0);
  6596. ocp_reg_write(tp, 0xa86a, data);
  6597. data = ocp_reg_read(tp, 0xa6f0);
  6598. data |= BIT(0);
  6599. ocp_reg_write(tp, 0xa6f0, data);
  6600. ocp_reg_write(tp, 0xbfa0, 0xd70d);
  6601. ocp_reg_write(tp, 0xbfa2, 0x4100);
  6602. ocp_reg_write(tp, 0xbfa4, 0xe868);
  6603. ocp_reg_write(tp, 0xbfa6, 0xdc59);
  6604. ocp_reg_write(tp, 0xb54c, 0x3c18);
  6605. data = ocp_reg_read(tp, 0xbfa4);
  6606. data &= ~BIT(5);
  6607. ocp_reg_write(tp, 0xbfa4, data);
  6608. data = sram_read(tp, 0x817d);
  6609. data |= BIT(12);
  6610. sram_write(tp, 0x817d, data);
  6611. break;
  6612. case RTL_VER_13:
  6613. /* 2.5G INRX */
  6614. data = ocp_reg_read(tp, 0xac46);
  6615. data &= ~0x00f0;
  6616. data |= 0x0090;
  6617. ocp_reg_write(tp, 0xac46, data);
  6618. data = ocp_reg_read(tp, 0xad30);
  6619. data &= ~0x0003;
  6620. data |= 0x0001;
  6621. ocp_reg_write(tp, 0xad30, data);
  6622. fallthrough;
  6623. case RTL_VER_15:
  6624. /* EEE parameter */
  6625. ocp_reg_write(tp, 0xb87c, 0x80f5);
  6626. ocp_reg_write(tp, 0xb87e, 0x760e);
  6627. ocp_reg_write(tp, 0xb87c, 0x8107);
  6628. ocp_reg_write(tp, 0xb87e, 0x360e);
  6629. ocp_reg_write(tp, 0xb87c, 0x8551);
  6630. data = ocp_reg_read(tp, 0xb87e);
  6631. data &= ~0xff00;
  6632. data |= 0x0800;
  6633. ocp_reg_write(tp, 0xb87e, data);
  6634. /* ADC_PGA parameter */
  6635. data = ocp_reg_read(tp, 0xbf00);
  6636. data &= ~0xe000;
  6637. data |= 0xa000;
  6638. ocp_reg_write(tp, 0xbf00, data);
  6639. data = ocp_reg_read(tp, 0xbf46);
  6640. data &= ~0x0f00;
  6641. data |= 0x0300;
  6642. ocp_reg_write(tp, 0xbf46, data);
  6643. /* Green Table-PGA, 1G full viterbi */
  6644. sram_write(tp, 0x8044, 0x2417);
  6645. sram_write(tp, 0x804a, 0x2417);
  6646. sram_write(tp, 0x8050, 0x2417);
  6647. sram_write(tp, 0x8056, 0x2417);
  6648. sram_write(tp, 0x805c, 0x2417);
  6649. sram_write(tp, 0x8062, 0x2417);
  6650. sram_write(tp, 0x8068, 0x2417);
  6651. sram_write(tp, 0x806e, 0x2417);
  6652. sram_write(tp, 0x8074, 0x2417);
  6653. sram_write(tp, 0x807a, 0x2417);
  6654. /* XG PLL */
  6655. data = ocp_reg_read(tp, 0xbf84);
  6656. data &= ~0xe000;
  6657. data |= 0xa000;
  6658. ocp_reg_write(tp, 0xbf84, data);
  6659. break;
  6660. default:
  6661. break;
  6662. }
  6663. /* Notify the MAC when the speed is changed to force mode. */
  6664. data = ocp_reg_read(tp, OCP_INTR_EN);
  6665. data |= INTR_SPEED_FORCE;
  6666. ocp_reg_write(tp, OCP_INTR_EN, data);
  6667. if (rtl_phy_patch_request(tp, true, true))
  6668. return;
  6669. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
  6670. ocp_data |= EEE_SPDWN_EN;
  6671. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
  6672. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  6673. data &= ~(EN_EEE_100 | EN_EEE_1000);
  6674. data |= EN_10M_CLKDIV;
  6675. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  6676. tp->ups_info._10m_ckdiv = true;
  6677. tp->ups_info.eee_plloff_100 = false;
  6678. tp->ups_info.eee_plloff_giga = false;
  6679. data = ocp_reg_read(tp, OCP_POWER_CFG);
  6680. data &= ~EEE_CLKDIV_EN;
  6681. ocp_reg_write(tp, OCP_POWER_CFG, data);
  6682. tp->ups_info.eee_ckdiv = false;
  6683. rtl_phy_patch_request(tp, false, true);
  6684. rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
  6685. data = ocp_reg_read(tp, 0xa428);
  6686. data &= ~BIT(9);
  6687. ocp_reg_write(tp, 0xa428, data);
  6688. data = ocp_reg_read(tp, 0xa5ea);
  6689. data &= ~BIT(0);
  6690. ocp_reg_write(tp, 0xa5ea, data);
  6691. tp->ups_info.lite_mode = 0;
  6692. if (tp->eee_en)
  6693. rtl_eee_enable(tp, true);
  6694. r8153_aldps_en(tp, true);
  6695. r8152b_enable_fc(tp);
  6696. r8153_u2p3en(tp, true);
  6697. set_bit(PHY_RESET, &tp->flags);
  6698. }
  6699. static void r8156_init(struct r8152 *tp)
  6700. {
  6701. u32 ocp_data;
  6702. u16 data;
  6703. int i;
  6704. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  6705. return;
  6706. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
  6707. ocp_data &= ~EN_ALL_SPEED;
  6708. ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
  6709. ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
  6710. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
  6711. ocp_data |= BYPASS_MAC_RESET;
  6712. ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
  6713. r8153b_u1u2en(tp, false);
  6714. for (i = 0; i < 500; i++) {
  6715. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  6716. AUTOLOAD_DONE)
  6717. break;
  6718. msleep(20);
  6719. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  6720. return;
  6721. }
  6722. data = r8153_phy_status(tp, 0);
  6723. if (data == PHY_STAT_EXT_INIT) {
  6724. data = ocp_reg_read(tp, 0xa468);
  6725. data &= ~(BIT(3) | BIT(1));
  6726. ocp_reg_write(tp, 0xa468, data);
  6727. }
  6728. data = r8152_mdio_read(tp, MII_BMCR);
  6729. if (data & BMCR_PDOWN) {
  6730. data &= ~BMCR_PDOWN;
  6731. r8152_mdio_write(tp, MII_BMCR, data);
  6732. }
  6733. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  6734. WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
  6735. r8153_u2p3en(tp, false);
  6736. /* MSC timer = 0xfff * 8ms = 32760 ms */
  6737. ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
  6738. /* U1/U2/L1 idle timer. 500 us */
  6739. ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
  6740. r8153b_power_cut_en(tp, false);
  6741. r8156_ups_en(tp, false);
  6742. r8153_queue_wake(tp, false);
  6743. rtl_runtime_suspend_enable(tp, false);
  6744. if (tp->udev->speed >= USB_SPEED_SUPER)
  6745. r8153b_u1u2en(tp, true);
  6746. usb_enable_lpm(tp->udev);
  6747. r8156_mac_clk_spd(tp, true);
  6748. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  6749. ocp_data &= ~PLA_MCU_SPDWN_EN;
  6750. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  6751. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  6752. if (rtl8152_get_speed(tp) & LINK_STATUS)
  6753. ocp_data |= CUR_LINK_OK;
  6754. else
  6755. ocp_data &= ~CUR_LINK_OK;
  6756. ocp_data |= POLL_LINK_CHG;
  6757. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  6758. set_bit(GREEN_ETHERNET, &tp->flags);
  6759. /* rx aggregation */
  6760. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  6761. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  6762. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  6763. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
  6764. ocp_data |= ACT_ODMA;
  6765. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
  6766. r8156_mdio_force_mode(tp);
  6767. rtl_tally_reset(tp);
  6768. tp->coalesce = 15000; /* 15 us */
  6769. }
  6770. static void r8156b_init(struct r8152 *tp)
  6771. {
  6772. u32 ocp_data;
  6773. u16 data;
  6774. int i;
  6775. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  6776. return;
  6777. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
  6778. ocp_data &= ~EN_ALL_SPEED;
  6779. ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
  6780. ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
  6781. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
  6782. ocp_data |= BYPASS_MAC_RESET;
  6783. ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
  6784. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  6785. ocp_data |= RX_DETECT8;
  6786. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  6787. r8153b_u1u2en(tp, false);
  6788. switch (tp->version) {
  6789. case RTL_VER_13:
  6790. case RTL_VER_15:
  6791. r8156b_wait_loading_flash(tp);
  6792. break;
  6793. default:
  6794. break;
  6795. }
  6796. for (i = 0; i < 500; i++) {
  6797. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  6798. AUTOLOAD_DONE)
  6799. break;
  6800. msleep(20);
  6801. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  6802. return;
  6803. }
  6804. data = r8153_phy_status(tp, 0);
  6805. if (data == PHY_STAT_EXT_INIT) {
  6806. data = ocp_reg_read(tp, 0xa468);
  6807. data &= ~(BIT(3) | BIT(1));
  6808. ocp_reg_write(tp, 0xa468, data);
  6809. data = ocp_reg_read(tp, 0xa466);
  6810. data &= ~BIT(0);
  6811. ocp_reg_write(tp, 0xa466, data);
  6812. }
  6813. data = r8152_mdio_read(tp, MII_BMCR);
  6814. if (data & BMCR_PDOWN) {
  6815. data &= ~BMCR_PDOWN;
  6816. r8152_mdio_write(tp, MII_BMCR, data);
  6817. }
  6818. data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
  6819. r8153_u2p3en(tp, false);
  6820. /* MSC timer = 0xfff * 8ms = 32760 ms */
  6821. ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
  6822. /* U1/U2/L1 idle timer. 500 us */
  6823. ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
  6824. r8153b_power_cut_en(tp, false);
  6825. r8156_ups_en(tp, false);
  6826. r8153_queue_wake(tp, false);
  6827. rtl_runtime_suspend_enable(tp, false);
  6828. if (tp->udev->speed >= USB_SPEED_SUPER)
  6829. r8153b_u1u2en(tp, true);
  6830. usb_enable_lpm(tp->udev);
  6831. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
  6832. ocp_data &= ~SLOT_EN;
  6833. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  6834. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  6835. ocp_data |= FLOW_CTRL_EN;
  6836. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  6837. /* enable fc timer and set timer to 600 ms. */
  6838. ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
  6839. CTRL_TIMER_EN | (600 / 8));
  6840. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
  6841. if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
  6842. ocp_data |= FLOW_CTRL_PATCH_2;
  6843. ocp_data &= ~AUTO_SPEEDUP;
  6844. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
  6845. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
  6846. ocp_data |= FC_PATCH_TASK;
  6847. ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
  6848. r8156_mac_clk_spd(tp, true);
  6849. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
  6850. ocp_data &= ~PLA_MCU_SPDWN_EN;
  6851. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
  6852. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
  6853. if (rtl8152_get_speed(tp) & LINK_STATUS)
  6854. ocp_data |= CUR_LINK_OK;
  6855. else
  6856. ocp_data &= ~CUR_LINK_OK;
  6857. ocp_data |= POLL_LINK_CHG;
  6858. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
  6859. set_bit(GREEN_ETHERNET, &tp->flags);
  6860. /* rx aggregation */
  6861. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  6862. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  6863. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  6864. r8156_mdio_force_mode(tp);
  6865. rtl_tally_reset(tp);
  6866. tp->coalesce = 15000; /* 15 us */
  6867. }
  6868. static bool rtl_check_vendor_ok(struct usb_interface *intf)
  6869. {
  6870. struct usb_host_interface *alt = intf->cur_altsetting;
  6871. struct usb_endpoint_descriptor *in, *out, *intr;
  6872. if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
  6873. dev_err(&intf->dev, "Expected endpoints are not found\n");
  6874. return false;
  6875. }
  6876. /* Check Rx endpoint address */
  6877. if (usb_endpoint_num(in) != 1) {
  6878. dev_err(&intf->dev, "Invalid Rx endpoint address\n");
  6879. return false;
  6880. }
  6881. /* Check Tx endpoint address */
  6882. if (usb_endpoint_num(out) != 2) {
  6883. dev_err(&intf->dev, "Invalid Tx endpoint address\n");
  6884. return false;
  6885. }
  6886. /* Check interrupt endpoint address */
  6887. if (usb_endpoint_num(intr) != 3) {
  6888. dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
  6889. return false;
  6890. }
  6891. return true;
  6892. }
  6893. static int rtl8152_pre_reset(struct usb_interface *intf)
  6894. {
  6895. struct r8152 *tp = usb_get_intfdata(intf);
  6896. struct net_device *netdev;
  6897. rtnl_lock();
  6898. if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags))
  6899. return 0;
  6900. netdev = tp->netdev;
  6901. if (!netif_running(netdev))
  6902. return 0;
  6903. netif_stop_queue(netdev);
  6904. tasklet_disable(&tp->tx_tl);
  6905. clear_bit(WORK_ENABLE, &tp->flags);
  6906. usb_kill_urb(tp->intr_urb);
  6907. cancel_delayed_work_sync(&tp->schedule);
  6908. napi_disable(&tp->napi);
  6909. if (netif_carrier_ok(netdev)) {
  6910. mutex_lock(&tp->control);
  6911. set_bit(IN_PRE_RESET, &tp->flags);
  6912. tp->rtl_ops.disable(tp);
  6913. clear_bit(IN_PRE_RESET, &tp->flags);
  6914. mutex_unlock(&tp->control);
  6915. }
  6916. return 0;
  6917. }
  6918. static int rtl8152_post_reset(struct usb_interface *intf)
  6919. {
  6920. struct r8152 *tp = usb_get_intfdata(intf);
  6921. struct net_device *netdev;
  6922. struct sockaddr_storage ss;
  6923. if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags))
  6924. goto exit;
  6925. rtl_set_accessible(tp);
  6926. /* reset the MAC address in case of policy change */
  6927. if (determine_ethernet_addr(tp, &ss) >= 0)
  6928. dev_set_mac_address(tp->netdev, &ss, NULL);
  6929. netdev = tp->netdev;
  6930. if (!netif_running(netdev))
  6931. goto exit;
  6932. set_bit(WORK_ENABLE, &tp->flags);
  6933. if (netif_carrier_ok(netdev)) {
  6934. mutex_lock(&tp->control);
  6935. tp->rtl_ops.enable(tp);
  6936. rtl_start_rx(tp);
  6937. _rtl8152_set_rx_mode(netdev);
  6938. mutex_unlock(&tp->control);
  6939. }
  6940. napi_enable(&tp->napi);
  6941. tasklet_enable(&tp->tx_tl);
  6942. netif_wake_queue(netdev);
  6943. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  6944. if (!list_empty(&tp->rx_done))
  6945. napi_schedule(&tp->napi);
  6946. exit:
  6947. rtnl_unlock();
  6948. return 0;
  6949. }
  6950. static bool delay_autosuspend(struct r8152 *tp)
  6951. {
  6952. bool sw_linking = !!netif_carrier_ok(tp->netdev);
  6953. bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
  6954. /* This means a linking change occurs and the driver doesn't detect it,
  6955. * yet. If the driver has disabled tx/rx and hw is linking on, the
  6956. * device wouldn't wake up by receiving any packet.
  6957. */
  6958. if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
  6959. return true;
  6960. /* If the linking down is occurred by nway, the device may miss the
  6961. * linking change event. And it wouldn't wake when linking on.
  6962. */
  6963. if (!sw_linking && tp->rtl_ops.in_nway(tp))
  6964. return true;
  6965. else if (!skb_queue_empty(&tp->tx_queue))
  6966. return true;
  6967. else
  6968. return false;
  6969. }
  6970. static int rtl8152_runtime_resume(struct r8152 *tp)
  6971. {
  6972. struct net_device *netdev = tp->netdev;
  6973. if (netif_running(netdev) && netdev->flags & IFF_UP) {
  6974. struct napi_struct *napi = &tp->napi;
  6975. tp->rtl_ops.autosuspend_en(tp, false);
  6976. napi_disable(napi);
  6977. set_bit(WORK_ENABLE, &tp->flags);
  6978. if (netif_carrier_ok(netdev)) {
  6979. if (rtl8152_get_speed(tp) & LINK_STATUS) {
  6980. rtl_start_rx(tp);
  6981. } else {
  6982. netif_carrier_off(netdev);
  6983. tp->rtl_ops.disable(tp);
  6984. netif_info(tp, link, netdev, "linking down\n");
  6985. }
  6986. }
  6987. napi_enable(napi);
  6988. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  6989. smp_mb__after_atomic();
  6990. if (!list_empty(&tp->rx_done))
  6991. napi_schedule(&tp->napi);
  6992. usb_submit_urb(tp->intr_urb, GFP_NOIO);
  6993. } else {
  6994. if (netdev->flags & IFF_UP)
  6995. tp->rtl_ops.autosuspend_en(tp, false);
  6996. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  6997. }
  6998. return 0;
  6999. }
  7000. static int rtl8152_system_resume(struct r8152 *tp)
  7001. {
  7002. struct net_device *netdev = tp->netdev;
  7003. netif_device_attach(netdev);
  7004. if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
  7005. tp->rtl_ops.up(tp);
  7006. netif_carrier_off(netdev);
  7007. set_bit(WORK_ENABLE, &tp->flags);
  7008. usb_submit_urb(tp->intr_urb, GFP_NOIO);
  7009. }
  7010. return 0;
  7011. }
  7012. static int rtl8152_runtime_suspend(struct r8152 *tp)
  7013. {
  7014. struct net_device *netdev = tp->netdev;
  7015. int ret = 0;
  7016. if (!tp->rtl_ops.autosuspend_en)
  7017. return -EBUSY;
  7018. set_bit(SELECTIVE_SUSPEND, &tp->flags);
  7019. smp_mb__after_atomic();
  7020. if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
  7021. u32 rcr = 0;
  7022. if (netif_carrier_ok(netdev)) {
  7023. u32 ocp_data;
  7024. rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  7025. ocp_data = rcr & ~RCR_ACPT_ALL;
  7026. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  7027. rxdy_gated_en(tp, true);
  7028. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
  7029. PLA_OOB_CTRL);
  7030. if (!(ocp_data & RXFIFO_EMPTY)) {
  7031. rxdy_gated_en(tp, false);
  7032. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
  7033. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  7034. smp_mb__after_atomic();
  7035. ret = -EBUSY;
  7036. goto out1;
  7037. }
  7038. }
  7039. clear_bit(WORK_ENABLE, &tp->flags);
  7040. usb_kill_urb(tp->intr_urb);
  7041. tp->rtl_ops.autosuspend_en(tp, true);
  7042. if (netif_carrier_ok(netdev)) {
  7043. struct napi_struct *napi = &tp->napi;
  7044. napi_disable(napi);
  7045. rtl_stop_rx(tp);
  7046. rxdy_gated_en(tp, false);
  7047. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
  7048. napi_enable(napi);
  7049. }
  7050. if (delay_autosuspend(tp)) {
  7051. rtl8152_runtime_resume(tp);
  7052. ret = -EBUSY;
  7053. }
  7054. }
  7055. out1:
  7056. return ret;
  7057. }
  7058. static int rtl8152_system_suspend(struct r8152 *tp)
  7059. {
  7060. struct net_device *netdev = tp->netdev;
  7061. netif_device_detach(netdev);
  7062. if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
  7063. struct napi_struct *napi = &tp->napi;
  7064. clear_bit(WORK_ENABLE, &tp->flags);
  7065. usb_kill_urb(tp->intr_urb);
  7066. tasklet_disable(&tp->tx_tl);
  7067. napi_disable(napi);
  7068. cancel_delayed_work_sync(&tp->schedule);
  7069. tp->rtl_ops.down(tp);
  7070. napi_enable(napi);
  7071. tasklet_enable(&tp->tx_tl);
  7072. }
  7073. /* If we're inaccessible here then some of the work that we did to
  7074. * get the adapter ready for suspend didn't work. Queue up a wakeup
  7075. * event so we can try again.
  7076. */
  7077. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7078. pm_wakeup_event(&tp->udev->dev, 0);
  7079. return 0;
  7080. }
  7081. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  7082. {
  7083. struct r8152 *tp = usb_get_intfdata(intf);
  7084. int ret;
  7085. mutex_lock(&tp->control);
  7086. if (PMSG_IS_AUTO(message))
  7087. ret = rtl8152_runtime_suspend(tp);
  7088. else
  7089. ret = rtl8152_system_suspend(tp);
  7090. mutex_unlock(&tp->control);
  7091. return ret;
  7092. }
  7093. static int rtl8152_resume(struct usb_interface *intf)
  7094. {
  7095. struct r8152 *tp = usb_get_intfdata(intf);
  7096. bool runtime_resume = test_bit(SELECTIVE_SUSPEND, &tp->flags);
  7097. int ret;
  7098. mutex_lock(&tp->control);
  7099. rtl_reset_ocp_base(tp);
  7100. if (runtime_resume)
  7101. ret = rtl8152_runtime_resume(tp);
  7102. else
  7103. ret = rtl8152_system_resume(tp);
  7104. mutex_unlock(&tp->control);
  7105. /* If the device is RTL8152_INACCESSIBLE here then we should do a
  7106. * reset. This is important because the usb_lock_device_for_reset()
  7107. * that happens as a result of usb_queue_reset_device() will silently
  7108. * fail if the device was suspended or if too much time passed.
  7109. *
  7110. * NOTE: The device is locked here so we can directly do the reset.
  7111. * We don't need usb_lock_device_for_reset() because that's just a
  7112. * wrapper over device_lock() and device_resume() (which calls us)
  7113. * does that for us.
  7114. */
  7115. if (!runtime_resume && test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7116. usb_reset_device(tp->udev);
  7117. return ret;
  7118. }
  7119. static int rtl8152_reset_resume(struct usb_interface *intf)
  7120. {
  7121. struct r8152 *tp = usb_get_intfdata(intf);
  7122. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  7123. rtl_reset_ocp_base(tp);
  7124. tp->rtl_ops.init(tp);
  7125. queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
  7126. set_ethernet_addr(tp, true);
  7127. return rtl8152_resume(intf);
  7128. }
  7129. static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  7130. {
  7131. struct r8152 *tp = netdev_priv(dev);
  7132. if (usb_autopm_get_interface(tp->intf) < 0)
  7133. return;
  7134. if (!rtl_can_wakeup(tp)) {
  7135. wol->supported = 0;
  7136. wol->wolopts = 0;
  7137. } else {
  7138. mutex_lock(&tp->control);
  7139. wol->supported = WAKE_ANY;
  7140. wol->wolopts = __rtl_get_wol(tp);
  7141. mutex_unlock(&tp->control);
  7142. }
  7143. usb_autopm_put_interface(tp->intf);
  7144. }
  7145. static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  7146. {
  7147. struct r8152 *tp = netdev_priv(dev);
  7148. int ret;
  7149. if (!rtl_can_wakeup(tp))
  7150. return -EOPNOTSUPP;
  7151. if (wol->wolopts & ~WAKE_ANY)
  7152. return -EINVAL;
  7153. ret = usb_autopm_get_interface(tp->intf);
  7154. if (ret < 0)
  7155. goto out_set_wol;
  7156. mutex_lock(&tp->control);
  7157. __rtl_set_wol(tp, wol->wolopts);
  7158. tp->saved_wolopts = wol->wolopts & WAKE_ANY;
  7159. mutex_unlock(&tp->control);
  7160. usb_autopm_put_interface(tp->intf);
  7161. out_set_wol:
  7162. return ret;
  7163. }
  7164. static u32 rtl8152_get_msglevel(struct net_device *dev)
  7165. {
  7166. struct r8152 *tp = netdev_priv(dev);
  7167. return tp->msg_enable;
  7168. }
  7169. static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
  7170. {
  7171. struct r8152 *tp = netdev_priv(dev);
  7172. tp->msg_enable = value;
  7173. }
  7174. static void rtl8152_get_drvinfo(struct net_device *netdev,
  7175. struct ethtool_drvinfo *info)
  7176. {
  7177. struct r8152 *tp = netdev_priv(netdev);
  7178. strscpy(info->driver, MODULENAME, sizeof(info->driver));
  7179. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  7180. if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
  7181. strscpy(info->fw_version, tp->rtl_fw.version,
  7182. sizeof(info->fw_version));
  7183. }
  7184. static
  7185. int rtl8152_get_link_ksettings(struct net_device *netdev,
  7186. struct ethtool_link_ksettings *cmd)
  7187. {
  7188. struct r8152 *tp = netdev_priv(netdev);
  7189. int ret;
  7190. if (!tp->mii.mdio_read)
  7191. return -EOPNOTSUPP;
  7192. ret = usb_autopm_get_interface(tp->intf);
  7193. if (ret < 0)
  7194. goto out;
  7195. mutex_lock(&tp->control);
  7196. mii_ethtool_get_link_ksettings(&tp->mii, cmd);
  7197. linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  7198. cmd->link_modes.supported, tp->support_2500full);
  7199. if (tp->support_2500full) {
  7200. linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  7201. cmd->link_modes.advertising,
  7202. ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
  7203. linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  7204. cmd->link_modes.lp_advertising,
  7205. ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
  7206. if (is_speed_2500(rtl8152_get_speed(tp)))
  7207. cmd->base.speed = SPEED_2500;
  7208. }
  7209. mutex_unlock(&tp->control);
  7210. usb_autopm_put_interface(tp->intf);
  7211. out:
  7212. return ret;
  7213. }
  7214. static int rtl8152_set_link_ksettings(struct net_device *dev,
  7215. const struct ethtool_link_ksettings *cmd)
  7216. {
  7217. struct r8152 *tp = netdev_priv(dev);
  7218. u32 advertising = 0;
  7219. int ret;
  7220. ret = usb_autopm_get_interface(tp->intf);
  7221. if (ret < 0)
  7222. goto out;
  7223. if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  7224. cmd->link_modes.advertising))
  7225. advertising |= RTL_ADVERTISED_10_HALF;
  7226. if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  7227. cmd->link_modes.advertising))
  7228. advertising |= RTL_ADVERTISED_10_FULL;
  7229. if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  7230. cmd->link_modes.advertising))
  7231. advertising |= RTL_ADVERTISED_100_HALF;
  7232. if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  7233. cmd->link_modes.advertising))
  7234. advertising |= RTL_ADVERTISED_100_FULL;
  7235. if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  7236. cmd->link_modes.advertising))
  7237. advertising |= RTL_ADVERTISED_1000_HALF;
  7238. if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  7239. cmd->link_modes.advertising))
  7240. advertising |= RTL_ADVERTISED_1000_FULL;
  7241. if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  7242. cmd->link_modes.advertising))
  7243. advertising |= RTL_ADVERTISED_2500_FULL;
  7244. mutex_lock(&tp->control);
  7245. ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
  7246. cmd->base.duplex, advertising);
  7247. if (!ret) {
  7248. tp->autoneg = cmd->base.autoneg;
  7249. tp->speed = cmd->base.speed;
  7250. tp->duplex = cmd->base.duplex;
  7251. tp->advertising = advertising;
  7252. }
  7253. mutex_unlock(&tp->control);
  7254. usb_autopm_put_interface(tp->intf);
  7255. out:
  7256. return ret;
  7257. }
  7258. static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
  7259. "tx_packets",
  7260. "rx_packets",
  7261. "tx_errors",
  7262. "rx_errors",
  7263. "rx_missed",
  7264. "align_errors",
  7265. "tx_single_collisions",
  7266. "tx_multi_collisions",
  7267. "rx_unicast",
  7268. "rx_broadcast",
  7269. "rx_multicast",
  7270. "tx_aborted",
  7271. "tx_underrun",
  7272. };
  7273. static int rtl8152_get_sset_count(struct net_device *dev, int sset)
  7274. {
  7275. switch (sset) {
  7276. case ETH_SS_STATS:
  7277. return ARRAY_SIZE(rtl8152_gstrings);
  7278. default:
  7279. return -EOPNOTSUPP;
  7280. }
  7281. }
  7282. static void rtl8152_get_ethtool_stats(struct net_device *dev,
  7283. struct ethtool_stats *stats, u64 *data)
  7284. {
  7285. struct r8152 *tp = netdev_priv(dev);
  7286. struct tally_counter tally;
  7287. if (usb_autopm_get_interface(tp->intf) < 0)
  7288. return;
  7289. generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
  7290. usb_autopm_put_interface(tp->intf);
  7291. data[0] = le64_to_cpu(tally.tx_packets);
  7292. data[1] = le64_to_cpu(tally.rx_packets);
  7293. data[2] = le64_to_cpu(tally.tx_errors);
  7294. data[3] = le32_to_cpu(tally.rx_errors);
  7295. data[4] = le16_to_cpu(tally.rx_missed);
  7296. data[5] = le16_to_cpu(tally.align_errors);
  7297. data[6] = le32_to_cpu(tally.tx_one_collision);
  7298. data[7] = le32_to_cpu(tally.tx_multi_collision);
  7299. data[8] = le64_to_cpu(tally.rx_unicast);
  7300. data[9] = le64_to_cpu(tally.rx_broadcast);
  7301. data[10] = le32_to_cpu(tally.rx_multicast);
  7302. data[11] = le16_to_cpu(tally.tx_aborted);
  7303. data[12] = le16_to_cpu(tally.tx_underrun);
  7304. }
  7305. static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  7306. {
  7307. switch (stringset) {
  7308. case ETH_SS_STATS:
  7309. memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
  7310. break;
  7311. }
  7312. }
  7313. static int r8152_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
  7314. {
  7315. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  7316. u16 val;
  7317. val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  7318. mii_eee_cap1_mod_linkmode_t(eee->supported, val);
  7319. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  7320. mii_eee_cap1_mod_linkmode_t(eee->advertised, val);
  7321. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  7322. mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val);
  7323. eee->eee_enabled = tp->eee_en;
  7324. linkmode_and(common, eee->advertised, eee->lp_advertised);
  7325. eee->eee_active = phy_check_valid(tp->speed, tp->duplex, common);
  7326. return 0;
  7327. }
  7328. static int r8152_set_eee(struct r8152 *tp, struct ethtool_keee *eee)
  7329. {
  7330. u16 val = linkmode_to_mii_eee_cap1_t(eee->advertised);
  7331. tp->eee_en = eee->eee_enabled;
  7332. tp->eee_adv = val;
  7333. rtl_eee_enable(tp, tp->eee_en);
  7334. return 0;
  7335. }
  7336. static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
  7337. {
  7338. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  7339. u16 val;
  7340. val = ocp_reg_read(tp, OCP_EEE_ABLE);
  7341. mii_eee_cap1_mod_linkmode_t(eee->supported, val);
  7342. val = ocp_reg_read(tp, OCP_EEE_ADV);
  7343. mii_eee_cap1_mod_linkmode_t(eee->advertised, val);
  7344. val = ocp_reg_read(tp, OCP_EEE_LPABLE);
  7345. mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val);
  7346. eee->eee_enabled = tp->eee_en;
  7347. linkmode_and(common, eee->advertised, eee->lp_advertised);
  7348. eee->eee_active = phy_check_valid(tp->speed, tp->duplex, common);
  7349. return 0;
  7350. }
  7351. static int
  7352. rtl_ethtool_get_eee(struct net_device *net, struct ethtool_keee *edata)
  7353. {
  7354. struct r8152 *tp = netdev_priv(net);
  7355. int ret;
  7356. if (!tp->rtl_ops.eee_get) {
  7357. ret = -EOPNOTSUPP;
  7358. goto out;
  7359. }
  7360. ret = usb_autopm_get_interface(tp->intf);
  7361. if (ret < 0)
  7362. goto out;
  7363. mutex_lock(&tp->control);
  7364. ret = tp->rtl_ops.eee_get(tp, edata);
  7365. mutex_unlock(&tp->control);
  7366. usb_autopm_put_interface(tp->intf);
  7367. out:
  7368. return ret;
  7369. }
  7370. static int
  7371. rtl_ethtool_set_eee(struct net_device *net, struct ethtool_keee *edata)
  7372. {
  7373. struct r8152 *tp = netdev_priv(net);
  7374. int ret;
  7375. if (!tp->rtl_ops.eee_set) {
  7376. ret = -EOPNOTSUPP;
  7377. goto out;
  7378. }
  7379. ret = usb_autopm_get_interface(tp->intf);
  7380. if (ret < 0)
  7381. goto out;
  7382. mutex_lock(&tp->control);
  7383. ret = tp->rtl_ops.eee_set(tp, edata);
  7384. if (!ret)
  7385. ret = mii_nway_restart(&tp->mii);
  7386. mutex_unlock(&tp->control);
  7387. usb_autopm_put_interface(tp->intf);
  7388. out:
  7389. return ret;
  7390. }
  7391. static int rtl8152_nway_reset(struct net_device *dev)
  7392. {
  7393. struct r8152 *tp = netdev_priv(dev);
  7394. int ret;
  7395. ret = usb_autopm_get_interface(tp->intf);
  7396. if (ret < 0)
  7397. goto out;
  7398. mutex_lock(&tp->control);
  7399. ret = mii_nway_restart(&tp->mii);
  7400. mutex_unlock(&tp->control);
  7401. usb_autopm_put_interface(tp->intf);
  7402. out:
  7403. return ret;
  7404. }
  7405. static int rtl8152_get_coalesce(struct net_device *netdev,
  7406. struct ethtool_coalesce *coalesce,
  7407. struct kernel_ethtool_coalesce *kernel_coal,
  7408. struct netlink_ext_ack *extack)
  7409. {
  7410. struct r8152 *tp = netdev_priv(netdev);
  7411. switch (tp->version) {
  7412. case RTL_VER_01:
  7413. case RTL_VER_02:
  7414. case RTL_VER_07:
  7415. return -EOPNOTSUPP;
  7416. default:
  7417. break;
  7418. }
  7419. coalesce->rx_coalesce_usecs = tp->coalesce;
  7420. return 0;
  7421. }
  7422. static int rtl8152_set_coalesce(struct net_device *netdev,
  7423. struct ethtool_coalesce *coalesce,
  7424. struct kernel_ethtool_coalesce *kernel_coal,
  7425. struct netlink_ext_ack *extack)
  7426. {
  7427. struct r8152 *tp = netdev_priv(netdev);
  7428. int ret;
  7429. switch (tp->version) {
  7430. case RTL_VER_01:
  7431. case RTL_VER_02:
  7432. case RTL_VER_07:
  7433. return -EOPNOTSUPP;
  7434. default:
  7435. break;
  7436. }
  7437. if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
  7438. return -EINVAL;
  7439. ret = usb_autopm_get_interface(tp->intf);
  7440. if (ret < 0)
  7441. return ret;
  7442. mutex_lock(&tp->control);
  7443. if (tp->coalesce != coalesce->rx_coalesce_usecs) {
  7444. tp->coalesce = coalesce->rx_coalesce_usecs;
  7445. if (netif_running(netdev) && netif_carrier_ok(netdev)) {
  7446. netif_stop_queue(netdev);
  7447. napi_disable(&tp->napi);
  7448. tp->rtl_ops.disable(tp);
  7449. tp->rtl_ops.enable(tp);
  7450. rtl_start_rx(tp);
  7451. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  7452. _rtl8152_set_rx_mode(netdev);
  7453. napi_enable(&tp->napi);
  7454. netif_wake_queue(netdev);
  7455. }
  7456. }
  7457. mutex_unlock(&tp->control);
  7458. usb_autopm_put_interface(tp->intf);
  7459. return ret;
  7460. }
  7461. static int rtl8152_get_tunable(struct net_device *netdev,
  7462. const struct ethtool_tunable *tunable, void *d)
  7463. {
  7464. struct r8152 *tp = netdev_priv(netdev);
  7465. switch (tunable->id) {
  7466. case ETHTOOL_RX_COPYBREAK:
  7467. *(u32 *)d = tp->rx_copybreak;
  7468. break;
  7469. default:
  7470. return -EOPNOTSUPP;
  7471. }
  7472. return 0;
  7473. }
  7474. static int rtl8152_set_tunable(struct net_device *netdev,
  7475. const struct ethtool_tunable *tunable,
  7476. const void *d)
  7477. {
  7478. struct r8152 *tp = netdev_priv(netdev);
  7479. u32 val;
  7480. switch (tunable->id) {
  7481. case ETHTOOL_RX_COPYBREAK:
  7482. val = *(u32 *)d;
  7483. if (val < ETH_ZLEN) {
  7484. netif_err(tp, rx_err, netdev,
  7485. "Invalid rx copy break value\n");
  7486. return -EINVAL;
  7487. }
  7488. if (tp->rx_copybreak != val) {
  7489. if (netdev->flags & IFF_UP) {
  7490. mutex_lock(&tp->control);
  7491. napi_disable(&tp->napi);
  7492. tp->rx_copybreak = val;
  7493. napi_enable(&tp->napi);
  7494. mutex_unlock(&tp->control);
  7495. } else {
  7496. tp->rx_copybreak = val;
  7497. }
  7498. }
  7499. break;
  7500. default:
  7501. return -EOPNOTSUPP;
  7502. }
  7503. return 0;
  7504. }
  7505. static void rtl8152_get_ringparam(struct net_device *netdev,
  7506. struct ethtool_ringparam *ring,
  7507. struct kernel_ethtool_ringparam *kernel_ring,
  7508. struct netlink_ext_ack *extack)
  7509. {
  7510. struct r8152 *tp = netdev_priv(netdev);
  7511. ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
  7512. ring->rx_pending = tp->rx_pending;
  7513. }
  7514. static int rtl8152_set_ringparam(struct net_device *netdev,
  7515. struct ethtool_ringparam *ring,
  7516. struct kernel_ethtool_ringparam *kernel_ring,
  7517. struct netlink_ext_ack *extack)
  7518. {
  7519. struct r8152 *tp = netdev_priv(netdev);
  7520. if (ring->rx_pending < (RTL8152_MAX_RX * 2))
  7521. return -EINVAL;
  7522. if (tp->rx_pending != ring->rx_pending) {
  7523. if (netdev->flags & IFF_UP) {
  7524. mutex_lock(&tp->control);
  7525. napi_disable(&tp->napi);
  7526. tp->rx_pending = ring->rx_pending;
  7527. napi_enable(&tp->napi);
  7528. mutex_unlock(&tp->control);
  7529. } else {
  7530. tp->rx_pending = ring->rx_pending;
  7531. }
  7532. }
  7533. return 0;
  7534. }
  7535. static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
  7536. {
  7537. struct r8152 *tp = netdev_priv(netdev);
  7538. u16 bmcr, lcladv, rmtadv;
  7539. u8 cap;
  7540. if (usb_autopm_get_interface(tp->intf) < 0)
  7541. return;
  7542. mutex_lock(&tp->control);
  7543. bmcr = r8152_mdio_read(tp, MII_BMCR);
  7544. lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
  7545. rmtadv = r8152_mdio_read(tp, MII_LPA);
  7546. mutex_unlock(&tp->control);
  7547. usb_autopm_put_interface(tp->intf);
  7548. if (!(bmcr & BMCR_ANENABLE)) {
  7549. pause->autoneg = 0;
  7550. pause->rx_pause = 0;
  7551. pause->tx_pause = 0;
  7552. return;
  7553. }
  7554. pause->autoneg = 1;
  7555. cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  7556. if (cap & FLOW_CTRL_RX)
  7557. pause->rx_pause = 1;
  7558. if (cap & FLOW_CTRL_TX)
  7559. pause->tx_pause = 1;
  7560. }
  7561. static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
  7562. {
  7563. struct r8152 *tp = netdev_priv(netdev);
  7564. u16 old, new1;
  7565. u8 cap = 0;
  7566. int ret;
  7567. ret = usb_autopm_get_interface(tp->intf);
  7568. if (ret < 0)
  7569. return ret;
  7570. mutex_lock(&tp->control);
  7571. if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
  7572. ret = -EINVAL;
  7573. goto out;
  7574. }
  7575. if (pause->rx_pause)
  7576. cap |= FLOW_CTRL_RX;
  7577. if (pause->tx_pause)
  7578. cap |= FLOW_CTRL_TX;
  7579. old = r8152_mdio_read(tp, MII_ADVERTISE);
  7580. new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
  7581. if (old != new1)
  7582. r8152_mdio_write(tp, MII_ADVERTISE, new1);
  7583. out:
  7584. mutex_unlock(&tp->control);
  7585. usb_autopm_put_interface(tp->intf);
  7586. return ret;
  7587. }
  7588. static const struct ethtool_ops ops = {
  7589. .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
  7590. .get_drvinfo = rtl8152_get_drvinfo,
  7591. .get_link = ethtool_op_get_link,
  7592. .nway_reset = rtl8152_nway_reset,
  7593. .get_msglevel = rtl8152_get_msglevel,
  7594. .set_msglevel = rtl8152_set_msglevel,
  7595. .get_wol = rtl8152_get_wol,
  7596. .set_wol = rtl8152_set_wol,
  7597. .get_strings = rtl8152_get_strings,
  7598. .get_sset_count = rtl8152_get_sset_count,
  7599. .get_ethtool_stats = rtl8152_get_ethtool_stats,
  7600. .get_coalesce = rtl8152_get_coalesce,
  7601. .set_coalesce = rtl8152_set_coalesce,
  7602. .get_eee = rtl_ethtool_get_eee,
  7603. .set_eee = rtl_ethtool_set_eee,
  7604. .get_link_ksettings = rtl8152_get_link_ksettings,
  7605. .set_link_ksettings = rtl8152_set_link_ksettings,
  7606. .get_tunable = rtl8152_get_tunable,
  7607. .set_tunable = rtl8152_set_tunable,
  7608. .get_ringparam = rtl8152_get_ringparam,
  7609. .set_ringparam = rtl8152_set_ringparam,
  7610. .get_pauseparam = rtl8152_get_pauseparam,
  7611. .set_pauseparam = rtl8152_set_pauseparam,
  7612. .get_ts_info = ethtool_op_get_ts_info,
  7613. };
  7614. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  7615. {
  7616. struct r8152 *tp = netdev_priv(netdev);
  7617. struct mii_ioctl_data *data = if_mii(rq);
  7618. int res;
  7619. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7620. return -ENODEV;
  7621. res = usb_autopm_get_interface(tp->intf);
  7622. if (res < 0)
  7623. goto out;
  7624. switch (cmd) {
  7625. case SIOCGMIIPHY:
  7626. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  7627. break;
  7628. case SIOCGMIIREG:
  7629. mutex_lock(&tp->control);
  7630. data->val_out = r8152_mdio_read(tp, data->reg_num);
  7631. mutex_unlock(&tp->control);
  7632. break;
  7633. case SIOCSMIIREG:
  7634. if (!capable(CAP_NET_ADMIN)) {
  7635. res = -EPERM;
  7636. break;
  7637. }
  7638. mutex_lock(&tp->control);
  7639. r8152_mdio_write(tp, data->reg_num, data->val_in);
  7640. mutex_unlock(&tp->control);
  7641. break;
  7642. default:
  7643. res = -EOPNOTSUPP;
  7644. }
  7645. usb_autopm_put_interface(tp->intf);
  7646. out:
  7647. return res;
  7648. }
  7649. static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
  7650. {
  7651. struct r8152 *tp = netdev_priv(dev);
  7652. int ret;
  7653. switch (tp->version) {
  7654. case RTL_VER_01:
  7655. case RTL_VER_02:
  7656. case RTL_VER_07:
  7657. WRITE_ONCE(dev->mtu, new_mtu);
  7658. return 0;
  7659. default:
  7660. break;
  7661. }
  7662. ret = usb_autopm_get_interface(tp->intf);
  7663. if (ret < 0)
  7664. return ret;
  7665. mutex_lock(&tp->control);
  7666. WRITE_ONCE(dev->mtu, new_mtu);
  7667. if (netif_running(dev)) {
  7668. if (tp->rtl_ops.change_mtu)
  7669. tp->rtl_ops.change_mtu(tp);
  7670. if (netif_carrier_ok(dev)) {
  7671. netif_stop_queue(dev);
  7672. napi_disable(&tp->napi);
  7673. tasklet_disable(&tp->tx_tl);
  7674. tp->rtl_ops.disable(tp);
  7675. tp->rtl_ops.enable(tp);
  7676. rtl_start_rx(tp);
  7677. tasklet_enable(&tp->tx_tl);
  7678. napi_enable(&tp->napi);
  7679. rtl8152_set_rx_mode(dev);
  7680. netif_wake_queue(dev);
  7681. }
  7682. }
  7683. mutex_unlock(&tp->control);
  7684. usb_autopm_put_interface(tp->intf);
  7685. return ret;
  7686. }
  7687. static const struct net_device_ops rtl8152_netdev_ops = {
  7688. .ndo_open = rtl8152_open,
  7689. .ndo_stop = rtl8152_close,
  7690. .ndo_eth_ioctl = rtl8152_ioctl,
  7691. .ndo_start_xmit = rtl8152_start_xmit,
  7692. .ndo_tx_timeout = rtl8152_tx_timeout,
  7693. .ndo_set_features = rtl8152_set_features,
  7694. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  7695. .ndo_set_mac_address = rtl8152_set_mac_address,
  7696. .ndo_change_mtu = rtl8152_change_mtu,
  7697. .ndo_validate_addr = eth_validate_addr,
  7698. .ndo_features_check = rtl8152_features_check,
  7699. };
  7700. static void rtl8152_unload(struct r8152 *tp)
  7701. {
  7702. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7703. return;
  7704. if (tp->version != RTL_VER_01)
  7705. r8152_power_cut_en(tp, true);
  7706. }
  7707. static void rtl8153_unload(struct r8152 *tp)
  7708. {
  7709. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7710. return;
  7711. r8153_power_cut_en(tp, false);
  7712. }
  7713. static void rtl8153b_unload(struct r8152 *tp)
  7714. {
  7715. if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
  7716. return;
  7717. r8153b_power_cut_en(tp, false);
  7718. }
  7719. static int rtl_ops_init(struct r8152 *tp)
  7720. {
  7721. struct rtl_ops *ops = &tp->rtl_ops;
  7722. int ret = 0;
  7723. switch (tp->version) {
  7724. case RTL_VER_01:
  7725. case RTL_VER_02:
  7726. case RTL_VER_07:
  7727. ops->init = r8152b_init;
  7728. ops->enable = rtl8152_enable;
  7729. ops->disable = rtl8152_disable;
  7730. ops->up = rtl8152_up;
  7731. ops->down = rtl8152_down;
  7732. ops->unload = rtl8152_unload;
  7733. ops->eee_get = r8152_get_eee;
  7734. ops->eee_set = r8152_set_eee;
  7735. ops->in_nway = rtl8152_in_nway;
  7736. ops->hw_phy_cfg = r8152b_hw_phy_cfg;
  7737. ops->autosuspend_en = rtl_runtime_suspend_enable;
  7738. tp->rx_buf_sz = 16 * 1024;
  7739. tp->eee_en = true;
  7740. tp->eee_adv = MDIO_EEE_100TX;
  7741. break;
  7742. case RTL_VER_03:
  7743. case RTL_VER_04:
  7744. case RTL_VER_05:
  7745. case RTL_VER_06:
  7746. ops->init = r8153_init;
  7747. ops->enable = rtl8153_enable;
  7748. ops->disable = rtl8153_disable;
  7749. ops->up = rtl8153_up;
  7750. ops->down = rtl8153_down;
  7751. ops->unload = rtl8153_unload;
  7752. ops->eee_get = r8153_get_eee;
  7753. ops->eee_set = r8152_set_eee;
  7754. ops->in_nway = rtl8153_in_nway;
  7755. ops->hw_phy_cfg = r8153_hw_phy_cfg;
  7756. ops->autosuspend_en = rtl8153_runtime_enable;
  7757. ops->change_mtu = rtl8153_change_mtu;
  7758. if (tp->udev->speed < USB_SPEED_SUPER)
  7759. tp->rx_buf_sz = 16 * 1024;
  7760. else
  7761. tp->rx_buf_sz = 32 * 1024;
  7762. tp->eee_en = true;
  7763. tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
  7764. break;
  7765. case RTL_VER_08:
  7766. case RTL_VER_09:
  7767. ops->init = r8153b_init;
  7768. ops->enable = rtl8153_enable;
  7769. ops->disable = rtl8153_disable;
  7770. ops->up = rtl8153b_up;
  7771. ops->down = rtl8153b_down;
  7772. ops->unload = rtl8153b_unload;
  7773. ops->eee_get = r8153_get_eee;
  7774. ops->eee_set = r8152_set_eee;
  7775. ops->in_nway = rtl8153_in_nway;
  7776. ops->hw_phy_cfg = r8153b_hw_phy_cfg;
  7777. ops->autosuspend_en = rtl8153b_runtime_enable;
  7778. ops->change_mtu = rtl8153_change_mtu;
  7779. tp->rx_buf_sz = 32 * 1024;
  7780. tp->eee_en = true;
  7781. tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
  7782. break;
  7783. case RTL_VER_11:
  7784. tp->eee_en = true;
  7785. tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
  7786. fallthrough;
  7787. case RTL_VER_10:
  7788. ops->init = r8156_init;
  7789. ops->enable = rtl8156_enable;
  7790. ops->disable = rtl8156_disable;
  7791. ops->up = rtl8156_up;
  7792. ops->down = rtl8156_down;
  7793. ops->unload = rtl8153_unload;
  7794. ops->eee_get = r8153_get_eee;
  7795. ops->eee_set = r8152_set_eee;
  7796. ops->in_nway = rtl8153_in_nway;
  7797. ops->hw_phy_cfg = r8156_hw_phy_cfg;
  7798. ops->autosuspend_en = rtl8156_runtime_enable;
  7799. ops->change_mtu = rtl8156_change_mtu;
  7800. tp->rx_buf_sz = 48 * 1024;
  7801. tp->support_2500full = 1;
  7802. break;
  7803. case RTL_VER_12:
  7804. case RTL_VER_13:
  7805. tp->support_2500full = 1;
  7806. fallthrough;
  7807. case RTL_VER_15:
  7808. tp->eee_en = true;
  7809. tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
  7810. ops->init = r8156b_init;
  7811. ops->enable = rtl8156b_enable;
  7812. ops->disable = rtl8153_disable;
  7813. ops->up = rtl8156_up;
  7814. ops->down = rtl8156_down;
  7815. ops->unload = rtl8153_unload;
  7816. ops->eee_get = r8153_get_eee;
  7817. ops->eee_set = r8152_set_eee;
  7818. ops->in_nway = rtl8153_in_nway;
  7819. ops->hw_phy_cfg = r8156b_hw_phy_cfg;
  7820. ops->autosuspend_en = rtl8156_runtime_enable;
  7821. ops->change_mtu = rtl8156_change_mtu;
  7822. tp->rx_buf_sz = 48 * 1024;
  7823. break;
  7824. case RTL_VER_14:
  7825. ops->init = r8153c_init;
  7826. ops->enable = rtl8153_enable;
  7827. ops->disable = rtl8153_disable;
  7828. ops->up = rtl8153c_up;
  7829. ops->down = rtl8153b_down;
  7830. ops->unload = rtl8153_unload;
  7831. ops->eee_get = r8153_get_eee;
  7832. ops->eee_set = r8152_set_eee;
  7833. ops->in_nway = rtl8153_in_nway;
  7834. ops->hw_phy_cfg = r8153c_hw_phy_cfg;
  7835. ops->autosuspend_en = rtl8153c_runtime_enable;
  7836. ops->change_mtu = rtl8153c_change_mtu;
  7837. tp->rx_buf_sz = 32 * 1024;
  7838. tp->eee_en = true;
  7839. tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
  7840. break;
  7841. default:
  7842. ret = -ENODEV;
  7843. dev_err(&tp->intf->dev, "Unknown Device\n");
  7844. break;
  7845. }
  7846. return ret;
  7847. }
  7848. #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
  7849. #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
  7850. #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
  7851. #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
  7852. #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw"
  7853. #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw"
  7854. #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw"
  7855. MODULE_FIRMWARE(FIRMWARE_8153A_2);
  7856. MODULE_FIRMWARE(FIRMWARE_8153A_3);
  7857. MODULE_FIRMWARE(FIRMWARE_8153A_4);
  7858. MODULE_FIRMWARE(FIRMWARE_8153B_2);
  7859. MODULE_FIRMWARE(FIRMWARE_8153C_1);
  7860. MODULE_FIRMWARE(FIRMWARE_8156A_2);
  7861. MODULE_FIRMWARE(FIRMWARE_8156B_2);
  7862. static int rtl_fw_init(struct r8152 *tp)
  7863. {
  7864. struct rtl_fw *rtl_fw = &tp->rtl_fw;
  7865. switch (tp->version) {
  7866. case RTL_VER_04:
  7867. rtl_fw->fw_name = FIRMWARE_8153A_2;
  7868. rtl_fw->pre_fw = r8153_pre_firmware_1;
  7869. rtl_fw->post_fw = r8153_post_firmware_1;
  7870. break;
  7871. case RTL_VER_05:
  7872. rtl_fw->fw_name = FIRMWARE_8153A_3;
  7873. rtl_fw->pre_fw = r8153_pre_firmware_2;
  7874. rtl_fw->post_fw = r8153_post_firmware_2;
  7875. break;
  7876. case RTL_VER_06:
  7877. rtl_fw->fw_name = FIRMWARE_8153A_4;
  7878. rtl_fw->post_fw = r8153_post_firmware_3;
  7879. break;
  7880. case RTL_VER_09:
  7881. rtl_fw->fw_name = FIRMWARE_8153B_2;
  7882. rtl_fw->pre_fw = r8153b_pre_firmware_1;
  7883. rtl_fw->post_fw = r8153b_post_firmware_1;
  7884. break;
  7885. case RTL_VER_11:
  7886. rtl_fw->fw_name = FIRMWARE_8156A_2;
  7887. rtl_fw->post_fw = r8156a_post_firmware_1;
  7888. break;
  7889. case RTL_VER_13:
  7890. case RTL_VER_15:
  7891. rtl_fw->fw_name = FIRMWARE_8156B_2;
  7892. break;
  7893. case RTL_VER_14:
  7894. rtl_fw->fw_name = FIRMWARE_8153C_1;
  7895. rtl_fw->pre_fw = r8153b_pre_firmware_1;
  7896. rtl_fw->post_fw = r8153c_post_firmware_1;
  7897. break;
  7898. default:
  7899. break;
  7900. }
  7901. return 0;
  7902. }
  7903. static u8 __rtl_get_hw_ver(struct usb_device *udev)
  7904. {
  7905. u32 ocp_data = 0;
  7906. __le32 *tmp;
  7907. u8 version;
  7908. int ret;
  7909. int i;
  7910. tmp = kmalloc_obj(*tmp);
  7911. if (!tmp)
  7912. return 0;
  7913. /* Retry up to 3 times in case there is a transitory error. We do this
  7914. * since retrying a read of the version is always safe and this
  7915. * function doesn't take advantage of r8152_control_msg().
  7916. */
  7917. for (i = 0; i < 3; i++) {
  7918. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  7919. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  7920. PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp),
  7921. USB_CTRL_GET_TIMEOUT);
  7922. if (ret > 0) {
  7923. ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
  7924. break;
  7925. }
  7926. }
  7927. if (i != 0 && ret > 0)
  7928. dev_warn(&udev->dev, "Needed %d retries to read version\n", i);
  7929. kfree(tmp);
  7930. switch (ocp_data) {
  7931. case 0x4c00:
  7932. version = RTL_VER_01;
  7933. break;
  7934. case 0x4c10:
  7935. version = RTL_VER_02;
  7936. break;
  7937. case 0x5c00:
  7938. version = RTL_VER_03;
  7939. break;
  7940. case 0x5c10:
  7941. version = RTL_VER_04;
  7942. break;
  7943. case 0x5c20:
  7944. version = RTL_VER_05;
  7945. break;
  7946. case 0x5c30:
  7947. version = RTL_VER_06;
  7948. break;
  7949. case 0x4800:
  7950. version = RTL_VER_07;
  7951. break;
  7952. case 0x6000:
  7953. version = RTL_VER_08;
  7954. break;
  7955. case 0x6010:
  7956. version = RTL_VER_09;
  7957. break;
  7958. case 0x7010:
  7959. version = RTL_TEST_01;
  7960. break;
  7961. case 0x7020:
  7962. version = RTL_VER_10;
  7963. break;
  7964. case 0x7030:
  7965. version = RTL_VER_11;
  7966. break;
  7967. case 0x7400:
  7968. version = RTL_VER_12;
  7969. break;
  7970. case 0x7410:
  7971. version = RTL_VER_13;
  7972. break;
  7973. case 0x6400:
  7974. version = RTL_VER_14;
  7975. break;
  7976. case 0x7420:
  7977. version = RTL_VER_15;
  7978. break;
  7979. default:
  7980. version = RTL_VER_UNKNOWN;
  7981. dev_info(&udev->dev, "Unknown version 0x%04x\n", ocp_data);
  7982. break;
  7983. }
  7984. return version;
  7985. }
  7986. u8 rtl8152_get_version(struct usb_interface *intf)
  7987. {
  7988. u8 version;
  7989. version = __rtl_get_hw_ver(interface_to_usbdev(intf));
  7990. dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
  7991. return version;
  7992. }
  7993. EXPORT_SYMBOL_GPL(rtl8152_get_version);
  7994. static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev)
  7995. {
  7996. int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor);
  7997. int product_id = le16_to_cpu(udev->descriptor.idProduct);
  7998. int vendor_id = le16_to_cpu(udev->descriptor.idVendor);
  7999. if (vendor_id == VENDOR_ID_LENOVO) {
  8000. switch (product_id) {
  8001. case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB:
  8002. case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK:
  8003. case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
  8004. case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
  8005. case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3:
  8006. case DEVICE_ID_THINKPAD_USB_C_DONGLE:
  8007. case DEVICE_ID_THINKPAD_HYBRID_USB_C_DOCK:
  8008. return 1;
  8009. }
  8010. } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) {
  8011. switch (product_id) {
  8012. case 0x8153:
  8013. return 1;
  8014. }
  8015. }
  8016. return 0;
  8017. }
  8018. static int rtl8152_probe_once(struct usb_interface *intf,
  8019. const struct usb_device_id *id, u8 version)
  8020. {
  8021. struct usb_device *udev = interface_to_usbdev(intf);
  8022. struct r8152 *tp;
  8023. struct net_device *netdev;
  8024. int ret;
  8025. usb_reset_device(udev);
  8026. netdev = alloc_etherdev(sizeof(struct r8152));
  8027. if (!netdev) {
  8028. dev_err(&intf->dev, "Out of memory\n");
  8029. return -ENOMEM;
  8030. }
  8031. SET_NETDEV_DEV(netdev, &intf->dev);
  8032. tp = netdev_priv(netdev);
  8033. tp->msg_enable = 0x7FFF;
  8034. tp->udev = udev;
  8035. tp->netdev = netdev;
  8036. tp->intf = intf;
  8037. tp->version = version;
  8038. tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
  8039. tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
  8040. tp->pipe_in = usb_rcvbulkpipe(udev, 1);
  8041. tp->pipe_out = usb_sndbulkpipe(udev, 2);
  8042. tp->pipe_intr = usb_rcvintpipe(udev, 3);
  8043. switch (version) {
  8044. case RTL_VER_01:
  8045. case RTL_VER_02:
  8046. case RTL_VER_07:
  8047. tp->mii.supports_gmii = 0;
  8048. break;
  8049. default:
  8050. tp->mii.supports_gmii = 1;
  8051. break;
  8052. }
  8053. ret = rtl_ops_init(tp);
  8054. if (ret)
  8055. goto out;
  8056. rtl_fw_init(tp);
  8057. mutex_init(&tp->control);
  8058. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  8059. INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
  8060. tasklet_setup(&tp->tx_tl, bottom_half);
  8061. tasklet_disable(&tp->tx_tl);
  8062. netdev->netdev_ops = &rtl8152_netdev_ops;
  8063. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  8064. netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  8065. NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
  8066. NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
  8067. NETIF_F_HW_VLAN_CTAG_TX;
  8068. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  8069. NETIF_F_TSO | NETIF_F_FRAGLIST |
  8070. NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
  8071. NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
  8072. netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  8073. NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
  8074. NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  8075. if (tp->version == RTL_VER_01) {
  8076. netdev->features &= ~NETIF_F_RXCSUM;
  8077. netdev->hw_features &= ~NETIF_F_RXCSUM;
  8078. }
  8079. tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev);
  8080. if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
  8081. (!strcmp(udev->serial, "000001000000") ||
  8082. !strcmp(udev->serial, "000002000000"))) {
  8083. dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
  8084. tp->dell_tb_rx_agg_bug = 1;
  8085. }
  8086. netdev->ethtool_ops = &ops;
  8087. netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
  8088. /* MTU range: 68 - 1500 or 9194 */
  8089. netdev->min_mtu = ETH_MIN_MTU;
  8090. switch (tp->version) {
  8091. case RTL_VER_03:
  8092. case RTL_VER_04:
  8093. case RTL_VER_05:
  8094. case RTL_VER_06:
  8095. case RTL_VER_08:
  8096. case RTL_VER_09:
  8097. case RTL_VER_14:
  8098. netdev->max_mtu = size_to_mtu(9 * 1024);
  8099. break;
  8100. case RTL_VER_10:
  8101. case RTL_VER_11:
  8102. netdev->max_mtu = size_to_mtu(15 * 1024);
  8103. break;
  8104. case RTL_VER_12:
  8105. case RTL_VER_13:
  8106. case RTL_VER_15:
  8107. netdev->max_mtu = size_to_mtu(16 * 1024);
  8108. break;
  8109. case RTL_VER_01:
  8110. case RTL_VER_02:
  8111. case RTL_VER_07:
  8112. default:
  8113. netdev->max_mtu = ETH_DATA_LEN;
  8114. break;
  8115. }
  8116. tp->mii.dev = netdev;
  8117. tp->mii.mdio_read = read_mii_word;
  8118. tp->mii.mdio_write = write_mii_word;
  8119. tp->mii.phy_id_mask = 0x3f;
  8120. tp->mii.reg_num_mask = 0x1f;
  8121. tp->mii.phy_id = R8152_PHY_ID;
  8122. tp->autoneg = AUTONEG_ENABLE;
  8123. tp->speed = SPEED_100;
  8124. tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
  8125. RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
  8126. if (tp->mii.supports_gmii) {
  8127. if (tp->support_2500full &&
  8128. tp->udev->speed >= USB_SPEED_SUPER) {
  8129. tp->speed = SPEED_2500;
  8130. tp->advertising |= RTL_ADVERTISED_2500_FULL;
  8131. } else {
  8132. tp->speed = SPEED_1000;
  8133. }
  8134. tp->advertising |= RTL_ADVERTISED_1000_FULL;
  8135. }
  8136. tp->duplex = DUPLEX_FULL;
  8137. tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
  8138. tp->rx_pending = 10 * RTL8152_MAX_RX;
  8139. intf->needs_remote_wakeup = 1;
  8140. if (!rtl_can_wakeup(tp))
  8141. __rtl_set_wol(tp, 0);
  8142. else
  8143. tp->saved_wolopts = __rtl_get_wol(tp);
  8144. tp->rtl_ops.init(tp);
  8145. #if IS_BUILTIN(CONFIG_USB_RTL8152)
  8146. /* Retry in case request_firmware() is not ready yet. */
  8147. tp->rtl_fw.retry = true;
  8148. #endif
  8149. queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
  8150. set_ethernet_addr(tp, false);
  8151. usb_set_intfdata(intf, tp);
  8152. netif_napi_add(netdev, &tp->napi, r8152_poll);
  8153. ret = register_netdev(netdev);
  8154. if (ret != 0) {
  8155. dev_err(&intf->dev, "couldn't register the device\n");
  8156. goto out1;
  8157. }
  8158. if (tp->saved_wolopts)
  8159. device_set_wakeup_enable(&udev->dev, true);
  8160. else
  8161. device_set_wakeup_enable(&udev->dev, false);
  8162. /* If we saw a control transfer error while probing then we may
  8163. * want to try probe() again. Consider this an error.
  8164. */
  8165. if (test_bit(PROBE_SHOULD_RETRY, &tp->flags))
  8166. goto out2;
  8167. set_bit(PROBED_WITH_NO_ERRORS, &tp->flags);
  8168. return 0;
  8169. out2:
  8170. unregister_netdev(netdev);
  8171. out1:
  8172. tasklet_kill(&tp->tx_tl);
  8173. cancel_delayed_work_sync(&tp->hw_phy_work);
  8174. if (tp->rtl_ops.unload)
  8175. tp->rtl_ops.unload(tp);
  8176. rtl8152_release_firmware(tp);
  8177. usb_set_intfdata(intf, NULL);
  8178. out:
  8179. if (test_bit(PROBE_SHOULD_RETRY, &tp->flags))
  8180. ret = -EAGAIN;
  8181. free_netdev(netdev);
  8182. return ret;
  8183. }
  8184. #define RTL8152_PROBE_TRIES 3
  8185. static int rtl8152_probe(struct usb_interface *intf,
  8186. const struct usb_device_id *id)
  8187. {
  8188. u8 version;
  8189. int ret;
  8190. int i;
  8191. if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
  8192. return -ENODEV;
  8193. if (!rtl_check_vendor_ok(intf))
  8194. return -ENODEV;
  8195. version = rtl8152_get_version(intf);
  8196. if (version == RTL_VER_UNKNOWN)
  8197. return -ENODEV;
  8198. for (i = 0; i < RTL8152_PROBE_TRIES; i++) {
  8199. ret = rtl8152_probe_once(intf, id, version);
  8200. if (ret != -EAGAIN)
  8201. break;
  8202. }
  8203. if (ret == -EAGAIN) {
  8204. dev_err(&intf->dev,
  8205. "r8152 failed probe after %d tries; giving up\n", i);
  8206. return -ENODEV;
  8207. }
  8208. return ret;
  8209. }
  8210. static void rtl8152_disconnect(struct usb_interface *intf)
  8211. {
  8212. struct r8152 *tp = usb_get_intfdata(intf);
  8213. usb_set_intfdata(intf, NULL);
  8214. if (tp) {
  8215. rtl_set_unplug(tp);
  8216. unregister_netdev(tp->netdev);
  8217. tasklet_kill(&tp->tx_tl);
  8218. cancel_delayed_work_sync(&tp->hw_phy_work);
  8219. if (tp->rtl_ops.unload)
  8220. tp->rtl_ops.unload(tp);
  8221. rtl8152_release_firmware(tp);
  8222. free_netdev(tp->netdev);
  8223. }
  8224. }
  8225. /* table of devices that work with this driver */
  8226. static const struct usb_device_id rtl8152_table[] = {
  8227. /* Realtek */
  8228. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) },
  8229. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) },
  8230. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) },
  8231. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) },
  8232. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) },
  8233. { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) },
  8234. /* Microsoft */
  8235. { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) },
  8236. { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) },
  8237. { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) },
  8238. { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e) },
  8239. { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) },
  8240. /* Lenovo */
  8241. { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) },
  8242. { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) },
  8243. { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) },
  8244. { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) },
  8245. { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) },
  8246. { USB_DEVICE(VENDOR_ID_LENOVO, 0x3098) },
  8247. { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) },
  8248. { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) },
  8249. { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) },
  8250. { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) },
  8251. { USB_DEVICE(VENDOR_ID_LENOVO, 0xa359) },
  8252. { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) },
  8253. { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) },
  8254. { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) },
  8255. { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) },
  8256. { USB_DEVICE(VENDOR_ID_TPLINK, 0x0602) },
  8257. { USB_DEVICE(VENDOR_ID_DLINK, 0xb301) },
  8258. { USB_DEVICE(VENDOR_ID_DELL, 0xb097) },
  8259. { USB_DEVICE(VENDOR_ID_ASUS, 0x1976) },
  8260. { USB_DEVICE(VENDOR_ID_TRENDNET, 0xe02b) },
  8261. {}
  8262. };
  8263. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  8264. static struct usb_driver rtl8152_driver = {
  8265. .name = MODULENAME,
  8266. .id_table = rtl8152_table,
  8267. .probe = rtl8152_probe,
  8268. .disconnect = rtl8152_disconnect,
  8269. .suspend = rtl8152_suspend,
  8270. .resume = rtl8152_resume,
  8271. .reset_resume = rtl8152_reset_resume,
  8272. .pre_reset = rtl8152_pre_reset,
  8273. .post_reset = rtl8152_post_reset,
  8274. .supports_autosuspend = 1,
  8275. .disable_hub_initiated_lpm = 1,
  8276. };
  8277. static int rtl8152_cfgselector_choose_configuration(struct usb_device *udev)
  8278. {
  8279. struct usb_host_config *c;
  8280. int i, num_configs;
  8281. /* Switch the device to vendor mode, if and only if the vendor mode
  8282. * driver supports it.
  8283. */
  8284. if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN)
  8285. return -ENODEV;
  8286. /* The vendor mode is not always config #1, so to find it out. */
  8287. c = udev->config;
  8288. num_configs = udev->descriptor.bNumConfigurations;
  8289. for (i = 0; i < num_configs; (i++, c++)) {
  8290. struct usb_interface_descriptor *desc = NULL;
  8291. if (!c->desc.bNumInterfaces)
  8292. continue;
  8293. desc = &c->intf_cache[0]->altsetting->desc;
  8294. if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
  8295. break;
  8296. }
  8297. if (i == num_configs)
  8298. return -ENODEV;
  8299. return c->desc.bConfigurationValue;
  8300. }
  8301. static struct usb_device_driver rtl8152_cfgselector_driver = {
  8302. .name = MODULENAME "-cfgselector",
  8303. .choose_configuration = rtl8152_cfgselector_choose_configuration,
  8304. .id_table = rtl8152_table,
  8305. .generic_subclass = 1,
  8306. .supports_autosuspend = 1,
  8307. };
  8308. static int __init rtl8152_driver_init(void)
  8309. {
  8310. int ret;
  8311. ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE);
  8312. if (ret)
  8313. return ret;
  8314. ret = usb_register(&rtl8152_driver);
  8315. if (ret)
  8316. usb_deregister_device_driver(&rtl8152_cfgselector_driver);
  8317. return ret;
  8318. }
  8319. static void __exit rtl8152_driver_exit(void)
  8320. {
  8321. usb_deregister(&rtl8152_driver);
  8322. usb_deregister_device_driver(&rtl8152_cfgselector_driver);
  8323. }
  8324. module_init(rtl8152_driver_init);
  8325. module_exit(rtl8152_driver_exit);
  8326. MODULE_AUTHOR(DRIVER_AUTHOR);
  8327. MODULE_DESCRIPTION(DRIVER_DESC);
  8328. MODULE_LICENSE("GPL");