r8152.c 243 KB

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