oxu210hp-hcd.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2008 Rodolfo Giometti <[email protected]>
  4. * Copyright (c) 2008 Eurotech S.p.A. <[email protected]>
  5. *
  6. * This code is *strongly* based on EHCI-HCD code by David Brownell since
  7. * the chip is a quasi-EHCI compatible.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/ioport.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/errno.h>
  18. #include <linux/timer.h>
  19. #include <linux/list.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/io.h>
  26. #include <linux/iopoll.h>
  27. #include <asm/irq.h>
  28. #include <asm/unaligned.h>
  29. #include <linux/irq.h>
  30. #include <linux/platform_device.h>
  31. #define DRIVER_VERSION "0.0.50"
  32. #define OXU_DEVICEID 0x00
  33. #define OXU_REV_MASK 0xffff0000
  34. #define OXU_REV_SHIFT 16
  35. #define OXU_REV_2100 0x2100
  36. #define OXU_BO_SHIFT 8
  37. #define OXU_BO_MASK (0x3 << OXU_BO_SHIFT)
  38. #define OXU_MAJ_REV_SHIFT 4
  39. #define OXU_MAJ_REV_MASK (0xf << OXU_MAJ_REV_SHIFT)
  40. #define OXU_MIN_REV_SHIFT 0
  41. #define OXU_MIN_REV_MASK (0xf << OXU_MIN_REV_SHIFT)
  42. #define OXU_HOSTIFCONFIG 0x04
  43. #define OXU_SOFTRESET 0x08
  44. #define OXU_SRESET (1 << 0)
  45. #define OXU_PIOBURSTREADCTRL 0x0C
  46. #define OXU_CHIPIRQSTATUS 0x10
  47. #define OXU_CHIPIRQEN_SET 0x14
  48. #define OXU_CHIPIRQEN_CLR 0x18
  49. #define OXU_USBSPHLPWUI 0x00000080
  50. #define OXU_USBOTGLPWUI 0x00000040
  51. #define OXU_USBSPHI 0x00000002
  52. #define OXU_USBOTGI 0x00000001
  53. #define OXU_CLKCTRL_SET 0x1C
  54. #define OXU_SYSCLKEN 0x00000008
  55. #define OXU_USBSPHCLKEN 0x00000002
  56. #define OXU_USBOTGCLKEN 0x00000001
  57. #define OXU_ASO 0x68
  58. #define OXU_SPHPOEN 0x00000100
  59. #define OXU_OVRCCURPUPDEN 0x00000800
  60. #define OXU_ASO_OP (1 << 10)
  61. #define OXU_COMPARATOR 0x000004000
  62. #define OXU_USBMODE 0x1A8
  63. #define OXU_VBPS 0x00000020
  64. #define OXU_ES_LITTLE 0x00000000
  65. #define OXU_CM_HOST_ONLY 0x00000003
  66. /*
  67. * Proper EHCI structs & defines
  68. */
  69. /* Magic numbers that can affect system performance */
  70. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  71. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  72. #define EHCI_TUNE_RL_TT 0
  73. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  74. #define EHCI_TUNE_MULT_TT 1
  75. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  76. struct oxu_hcd;
  77. /* EHCI register interface, corresponds to EHCI Revision 0.95 specification */
  78. /* Section 2.2 Host Controller Capability Registers */
  79. struct ehci_caps {
  80. /* these fields are specified as 8 and 16 bit registers,
  81. * but some hosts can't perform 8 or 16 bit PCI accesses.
  82. */
  83. u32 hc_capbase;
  84. #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
  85. #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
  86. u32 hcs_params; /* HCSPARAMS - offset 0x4 */
  87. #define HCS_DEBUG_PORT(p) (((p)>>20)&0xf) /* bits 23:20, debug port? */
  88. #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
  89. #define HCS_N_CC(p) (((p)>>12)&0xf) /* bits 15:12, #companion HCs */
  90. #define HCS_N_PCC(p) (((p)>>8)&0xf) /* bits 11:8, ports per CC */
  91. #define HCS_PORTROUTED(p) ((p)&(1 << 7)) /* true: port routing */
  92. #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
  93. #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
  94. u32 hcc_params; /* HCCPARAMS - offset 0x8 */
  95. #define HCC_EXT_CAPS(p) (((p)>>8)&0xff) /* for pci extended caps */
  96. #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
  97. #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
  98. #define HCC_CANPARK(p) ((p)&(1 << 2)) /* true: can park on async qh */
  99. #define HCC_PGM_FRAMELISTLEN(p) ((p)&(1 << 1)) /* true: periodic_size changes*/
  100. #define HCC_64BIT_ADDR(p) ((p)&(1)) /* true: can use 64-bit addr */
  101. u8 portroute[8]; /* nibbles for routing - offset 0xC */
  102. } __packed;
  103. /* Section 2.3 Host Controller Operational Registers */
  104. struct ehci_regs {
  105. /* USBCMD: offset 0x00 */
  106. u32 command;
  107. /* 23:16 is r/w intr rate, in microframes; default "8" == 1/msec */
  108. #define CMD_PARK (1<<11) /* enable "park" on async qh */
  109. #define CMD_PARK_CNT(c) (((c)>>8)&3) /* how many transfers to park for */
  110. #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
  111. #define CMD_IAAD (1<<6) /* "doorbell" interrupt async advance */
  112. #define CMD_ASE (1<<5) /* async schedule enable */
  113. #define CMD_PSE (1<<4) /* periodic schedule enable */
  114. /* 3:2 is periodic frame list size */
  115. #define CMD_RESET (1<<1) /* reset HC not bus */
  116. #define CMD_RUN (1<<0) /* start/stop HC */
  117. /* USBSTS: offset 0x04 */
  118. u32 status;
  119. #define STS_ASS (1<<15) /* Async Schedule Status */
  120. #define STS_PSS (1<<14) /* Periodic Schedule Status */
  121. #define STS_RECL (1<<13) /* Reclamation */
  122. #define STS_HALT (1<<12) /* Not running (any reason) */
  123. /* some bits reserved */
  124. /* these STS_* flags are also intr_enable bits (USBINTR) */
  125. #define STS_IAA (1<<5) /* Interrupted on async advance */
  126. #define STS_FATAL (1<<4) /* such as some PCI access errors */
  127. #define STS_FLR (1<<3) /* frame list rolled over */
  128. #define STS_PCD (1<<2) /* port change detect */
  129. #define STS_ERR (1<<1) /* "error" completion (overflow, ...) */
  130. #define STS_INT (1<<0) /* "normal" completion (short, ...) */
  131. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  132. /* USBINTR: offset 0x08 */
  133. u32 intr_enable;
  134. /* FRINDEX: offset 0x0C */
  135. u32 frame_index; /* current microframe number */
  136. /* CTRLDSSEGMENT: offset 0x10 */
  137. u32 segment; /* address bits 63:32 if needed */
  138. /* PERIODICLISTBASE: offset 0x14 */
  139. u32 frame_list; /* points to periodic list */
  140. /* ASYNCLISTADDR: offset 0x18 */
  141. u32 async_next; /* address of next async queue head */
  142. u32 reserved[9];
  143. /* CONFIGFLAG: offset 0x40 */
  144. u32 configured_flag;
  145. #define FLAG_CF (1<<0) /* true: we'll support "high speed" */
  146. /* PORTSC: offset 0x44 */
  147. u32 port_status[0]; /* up to N_PORTS */
  148. /* 31:23 reserved */
  149. #define PORT_WKOC_E (1<<22) /* wake on overcurrent (enable) */
  150. #define PORT_WKDISC_E (1<<21) /* wake on disconnect (enable) */
  151. #define PORT_WKCONN_E (1<<20) /* wake on connect (enable) */
  152. /* 19:16 for port testing */
  153. #define PORT_LED_OFF (0<<14)
  154. #define PORT_LED_AMBER (1<<14)
  155. #define PORT_LED_GREEN (2<<14)
  156. #define PORT_LED_MASK (3<<14)
  157. #define PORT_OWNER (1<<13) /* true: companion hc owns this port */
  158. #define PORT_POWER (1<<12) /* true: has power (see PPC) */
  159. #define PORT_USB11(x) (((x)&(3<<10)) == (1<<10)) /* USB 1.1 device */
  160. /* 11:10 for detecting lowspeed devices (reset vs release ownership) */
  161. /* 9 reserved */
  162. #define PORT_RESET (1<<8) /* reset port */
  163. #define PORT_SUSPEND (1<<7) /* suspend port */
  164. #define PORT_RESUME (1<<6) /* resume it */
  165. #define PORT_OCC (1<<5) /* over current change */
  166. #define PORT_OC (1<<4) /* over current active */
  167. #define PORT_PEC (1<<3) /* port enable change */
  168. #define PORT_PE (1<<2) /* port enable */
  169. #define PORT_CSC (1<<1) /* connect status change */
  170. #define PORT_CONNECT (1<<0) /* device connected */
  171. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
  172. } __packed;
  173. /* Appendix C, Debug port ... intended for use with special "debug devices"
  174. * that can help if there's no serial console. (nonstandard enumeration.)
  175. */
  176. struct ehci_dbg_port {
  177. u32 control;
  178. #define DBGP_OWNER (1<<30)
  179. #define DBGP_ENABLED (1<<28)
  180. #define DBGP_DONE (1<<16)
  181. #define DBGP_INUSE (1<<10)
  182. #define DBGP_ERRCODE(x) (((x)>>7)&0x07)
  183. # define DBGP_ERR_BAD 1
  184. # define DBGP_ERR_SIGNAL 2
  185. #define DBGP_ERROR (1<<6)
  186. #define DBGP_GO (1<<5)
  187. #define DBGP_OUT (1<<4)
  188. #define DBGP_LEN(x) (((x)>>0)&0x0f)
  189. u32 pids;
  190. #define DBGP_PID_GET(x) (((x)>>16)&0xff)
  191. #define DBGP_PID_SET(data, tok) (((data)<<8)|(tok))
  192. u32 data03;
  193. u32 data47;
  194. u32 address;
  195. #define DBGP_EPADDR(dev, ep) (((dev)<<8)|(ep))
  196. } __packed;
  197. #define QTD_NEXT(dma) cpu_to_le32((u32)dma)
  198. /*
  199. * EHCI Specification 0.95 Section 3.5
  200. * QTD: describe data transfer components (buffer, direction, ...)
  201. * See Fig 3-6 "Queue Element Transfer Descriptor Block Diagram".
  202. *
  203. * These are associated only with "QH" (Queue Head) structures,
  204. * used with control, bulk, and interrupt transfers.
  205. */
  206. struct ehci_qtd {
  207. /* first part defined by EHCI spec */
  208. __le32 hw_next; /* see EHCI 3.5.1 */
  209. __le32 hw_alt_next; /* see EHCI 3.5.2 */
  210. __le32 hw_token; /* see EHCI 3.5.3 */
  211. #define QTD_TOGGLE (1 << 31) /* data toggle */
  212. #define QTD_LENGTH(tok) (((tok)>>16) & 0x7fff)
  213. #define QTD_IOC (1 << 15) /* interrupt on complete */
  214. #define QTD_CERR(tok) (((tok)>>10) & 0x3)
  215. #define QTD_PID(tok) (((tok)>>8) & 0x3)
  216. #define QTD_STS_ACTIVE (1 << 7) /* HC may execute this */
  217. #define QTD_STS_HALT (1 << 6) /* halted on error */
  218. #define QTD_STS_DBE (1 << 5) /* data buffer error (in HC) */
  219. #define QTD_STS_BABBLE (1 << 4) /* device was babbling (qtd halted) */
  220. #define QTD_STS_XACT (1 << 3) /* device gave illegal response */
  221. #define QTD_STS_MMF (1 << 2) /* incomplete split transaction */
  222. #define QTD_STS_STS (1 << 1) /* split transaction state */
  223. #define QTD_STS_PING (1 << 0) /* issue PING? */
  224. __le32 hw_buf[5]; /* see EHCI 3.5.4 */
  225. __le32 hw_buf_hi[5]; /* Appendix B */
  226. /* the rest is HCD-private */
  227. dma_addr_t qtd_dma; /* qtd address */
  228. struct list_head qtd_list; /* sw qtd list */
  229. struct urb *urb; /* qtd's urb */
  230. size_t length; /* length of buffer */
  231. u32 qtd_buffer_len;
  232. void *buffer;
  233. dma_addr_t buffer_dma;
  234. void *transfer_buffer;
  235. void *transfer_dma;
  236. } __aligned(32);
  237. /* mask NakCnt+T in qh->hw_alt_next */
  238. #define QTD_MASK cpu_to_le32 (~0x1f)
  239. #define IS_SHORT_READ(token) (QTD_LENGTH(token) != 0 && QTD_PID(token) == 1)
  240. /* Type tag from {qh, itd, sitd, fstn}->hw_next */
  241. #define Q_NEXT_TYPE(dma) ((dma) & cpu_to_le32 (3 << 1))
  242. /* values for that type tag */
  243. #define Q_TYPE_QH cpu_to_le32 (1 << 1)
  244. /* next async queue entry, or pointer to interrupt/periodic QH */
  245. #define QH_NEXT(dma) (cpu_to_le32(((u32)dma)&~0x01f)|Q_TYPE_QH)
  246. /* for periodic/async schedules and qtd lists, mark end of list */
  247. #define EHCI_LIST_END cpu_to_le32(1) /* "null pointer" to hw */
  248. /*
  249. * Entries in periodic shadow table are pointers to one of four kinds
  250. * of data structure. That's dictated by the hardware; a type tag is
  251. * encoded in the low bits of the hardware's periodic schedule. Use
  252. * Q_NEXT_TYPE to get the tag.
  253. *
  254. * For entries in the async schedule, the type tag always says "qh".
  255. */
  256. union ehci_shadow {
  257. struct ehci_qh *qh; /* Q_TYPE_QH */
  258. __le32 *hw_next; /* (all types) */
  259. void *ptr;
  260. };
  261. /*
  262. * EHCI Specification 0.95 Section 3.6
  263. * QH: describes control/bulk/interrupt endpoints
  264. * See Fig 3-7 "Queue Head Structure Layout".
  265. *
  266. * These appear in both the async and (for interrupt) periodic schedules.
  267. */
  268. struct ehci_qh {
  269. /* first part defined by EHCI spec */
  270. __le32 hw_next; /* see EHCI 3.6.1 */
  271. __le32 hw_info1; /* see EHCI 3.6.2 */
  272. #define QH_HEAD 0x00008000
  273. __le32 hw_info2; /* see EHCI 3.6.2 */
  274. #define QH_SMASK 0x000000ff
  275. #define QH_CMASK 0x0000ff00
  276. #define QH_HUBADDR 0x007f0000
  277. #define QH_HUBPORT 0x3f800000
  278. #define QH_MULT 0xc0000000
  279. __le32 hw_current; /* qtd list - see EHCI 3.6.4 */
  280. /* qtd overlay (hardware parts of a struct ehci_qtd) */
  281. __le32 hw_qtd_next;
  282. __le32 hw_alt_next;
  283. __le32 hw_token;
  284. __le32 hw_buf[5];
  285. __le32 hw_buf_hi[5];
  286. /* the rest is HCD-private */
  287. dma_addr_t qh_dma; /* address of qh */
  288. union ehci_shadow qh_next; /* ptr to qh; or periodic */
  289. struct list_head qtd_list; /* sw qtd list */
  290. struct ehci_qtd *dummy;
  291. struct ehci_qh *reclaim; /* next to reclaim */
  292. struct oxu_hcd *oxu;
  293. struct kref kref;
  294. unsigned int stamp;
  295. u8 qh_state;
  296. #define QH_STATE_LINKED 1 /* HC sees this */
  297. #define QH_STATE_UNLINK 2 /* HC may still see this */
  298. #define QH_STATE_IDLE 3 /* HC doesn't see this */
  299. #define QH_STATE_UNLINK_WAIT 4 /* LINKED and on reclaim q */
  300. #define QH_STATE_COMPLETING 5 /* don't touch token.HALT */
  301. /* periodic schedule info */
  302. u8 usecs; /* intr bandwidth */
  303. u8 gap_uf; /* uframes split/csplit gap */
  304. u8 c_usecs; /* ... split completion bw */
  305. u16 tt_usecs; /* tt downstream bandwidth */
  306. unsigned short period; /* polling interval */
  307. unsigned short start; /* where polling starts */
  308. #define NO_FRAME ((unsigned short)~0) /* pick new start */
  309. struct usb_device *dev; /* access to TT */
  310. } __aligned(32);
  311. /*
  312. * Proper OXU210HP structs
  313. */
  314. #define OXU_OTG_CORE_OFFSET 0x00400
  315. #define OXU_OTG_CAP_OFFSET (OXU_OTG_CORE_OFFSET + 0x100)
  316. #define OXU_SPH_CORE_OFFSET 0x00800
  317. #define OXU_SPH_CAP_OFFSET (OXU_SPH_CORE_OFFSET + 0x100)
  318. #define OXU_OTG_MEM 0xE000
  319. #define OXU_SPH_MEM 0x16000
  320. /* Only how many elements & element structure are specifies here. */
  321. /* 2 host controllers are enabled - total size <= 28 kbytes */
  322. #define DEFAULT_I_TDPS 1024
  323. #define QHEAD_NUM 16
  324. #define QTD_NUM 32
  325. #define SITD_NUM 8
  326. #define MURB_NUM 8
  327. #define BUFFER_NUM 8
  328. #define BUFFER_SIZE 512
  329. struct oxu_info {
  330. struct usb_hcd *hcd[2];
  331. };
  332. struct oxu_buf {
  333. u8 buffer[BUFFER_SIZE];
  334. } __aligned(BUFFER_SIZE);
  335. struct oxu_onchip_mem {
  336. struct oxu_buf db_pool[BUFFER_NUM];
  337. u32 frame_list[DEFAULT_I_TDPS];
  338. struct ehci_qh qh_pool[QHEAD_NUM];
  339. struct ehci_qtd qtd_pool[QTD_NUM];
  340. } __aligned(4 << 10);
  341. #define EHCI_MAX_ROOT_PORTS 15 /* see HCS_N_PORTS */
  342. struct oxu_murb {
  343. struct urb urb;
  344. struct urb *main;
  345. u8 last;
  346. };
  347. struct oxu_hcd { /* one per controller */
  348. unsigned int is_otg:1;
  349. u8 qh_used[QHEAD_NUM];
  350. u8 qtd_used[QTD_NUM];
  351. u8 db_used[BUFFER_NUM];
  352. u8 murb_used[MURB_NUM];
  353. struct oxu_onchip_mem __iomem *mem;
  354. spinlock_t mem_lock;
  355. struct timer_list urb_timer;
  356. struct ehci_caps __iomem *caps;
  357. struct ehci_regs __iomem *regs;
  358. u32 hcs_params; /* cached register copy */
  359. spinlock_t lock;
  360. /* async schedule support */
  361. struct ehci_qh *async;
  362. struct ehci_qh *reclaim;
  363. unsigned int reclaim_ready:1;
  364. unsigned int scanning:1;
  365. /* periodic schedule support */
  366. unsigned int periodic_size;
  367. __le32 *periodic; /* hw periodic table */
  368. dma_addr_t periodic_dma;
  369. unsigned int i_thresh; /* uframes HC might cache */
  370. union ehci_shadow *pshadow; /* mirror hw periodic table */
  371. int next_uframe; /* scan periodic, start here */
  372. unsigned int periodic_sched; /* periodic activity count */
  373. /* per root hub port */
  374. unsigned long reset_done[EHCI_MAX_ROOT_PORTS];
  375. /* bit vectors (one bit per port) */
  376. unsigned long bus_suspended; /* which ports were
  377. * already suspended at the
  378. * start of a bus suspend
  379. */
  380. unsigned long companion_ports;/* which ports are dedicated
  381. * to the companion controller
  382. */
  383. struct timer_list watchdog;
  384. unsigned long actions;
  385. unsigned int stamp;
  386. unsigned long next_statechange;
  387. u32 command;
  388. /* SILICON QUIRKS */
  389. struct list_head urb_list; /* this is the head to urb
  390. * queue that didn't get enough
  391. * resources
  392. */
  393. struct oxu_murb *murb_pool; /* murb per split big urb */
  394. unsigned int urb_len;
  395. u8 sbrn; /* packed release number */
  396. };
  397. #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
  398. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  399. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  400. #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
  401. enum ehci_timer_action {
  402. TIMER_IO_WATCHDOG,
  403. TIMER_IAA_WATCHDOG,
  404. TIMER_ASYNC_SHRINK,
  405. TIMER_ASYNC_OFF,
  406. };
  407. /*
  408. * Main defines
  409. */
  410. #define oxu_dbg(oxu, fmt, args...) \
  411. dev_dbg(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  412. #define oxu_err(oxu, fmt, args...) \
  413. dev_err(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  414. #define oxu_info(oxu, fmt, args...) \
  415. dev_info(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  416. #ifdef CONFIG_DYNAMIC_DEBUG
  417. #define DEBUG
  418. #endif
  419. static inline struct usb_hcd *oxu_to_hcd(struct oxu_hcd *oxu)
  420. {
  421. return container_of((void *) oxu, struct usb_hcd, hcd_priv);
  422. }
  423. static inline struct oxu_hcd *hcd_to_oxu(struct usb_hcd *hcd)
  424. {
  425. return (struct oxu_hcd *) (hcd->hcd_priv);
  426. }
  427. /*
  428. * Debug stuff
  429. */
  430. #undef OXU_URB_TRACE
  431. #undef OXU_VERBOSE_DEBUG
  432. #ifdef OXU_VERBOSE_DEBUG
  433. #define oxu_vdbg oxu_dbg
  434. #else
  435. #define oxu_vdbg(oxu, fmt, args...) /* Nop */
  436. #endif
  437. #ifdef DEBUG
  438. static int __attribute__((__unused__))
  439. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  440. {
  441. return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  442. label, label[0] ? " " : "", status,
  443. (status & STS_ASS) ? " Async" : "",
  444. (status & STS_PSS) ? " Periodic" : "",
  445. (status & STS_RECL) ? " Recl" : "",
  446. (status & STS_HALT) ? " Halt" : "",
  447. (status & STS_IAA) ? " IAA" : "",
  448. (status & STS_FATAL) ? " FATAL" : "",
  449. (status & STS_FLR) ? " FLR" : "",
  450. (status & STS_PCD) ? " PCD" : "",
  451. (status & STS_ERR) ? " ERR" : "",
  452. (status & STS_INT) ? " INT" : ""
  453. );
  454. }
  455. static int __attribute__((__unused__))
  456. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  457. {
  458. return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  459. label, label[0] ? " " : "", enable,
  460. (enable & STS_IAA) ? " IAA" : "",
  461. (enable & STS_FATAL) ? " FATAL" : "",
  462. (enable & STS_FLR) ? " FLR" : "",
  463. (enable & STS_PCD) ? " PCD" : "",
  464. (enable & STS_ERR) ? " ERR" : "",
  465. (enable & STS_INT) ? " INT" : ""
  466. );
  467. }
  468. static const char *const fls_strings[] =
  469. { "1024", "512", "256", "??" };
  470. static int dbg_command_buf(char *buf, unsigned len,
  471. const char *label, u32 command)
  472. {
  473. return scnprintf(buf, len,
  474. "%s%scommand %06x %s=%d ithresh=%d%s%s%s%s period=%s%s %s",
  475. label, label[0] ? " " : "", command,
  476. (command & CMD_PARK) ? "park" : "(park)",
  477. CMD_PARK_CNT(command),
  478. (command >> 16) & 0x3f,
  479. (command & CMD_LRESET) ? " LReset" : "",
  480. (command & CMD_IAAD) ? " IAAD" : "",
  481. (command & CMD_ASE) ? " Async" : "",
  482. (command & CMD_PSE) ? " Periodic" : "",
  483. fls_strings[(command >> 2) & 0x3],
  484. (command & CMD_RESET) ? " Reset" : "",
  485. (command & CMD_RUN) ? "RUN" : "HALT"
  486. );
  487. }
  488. static int dbg_port_buf(char *buf, unsigned len, const char *label,
  489. int port, u32 status)
  490. {
  491. char *sig;
  492. /* signaling state */
  493. switch (status & (3 << 10)) {
  494. case 0 << 10:
  495. sig = "se0";
  496. break;
  497. case 1 << 10:
  498. sig = "k"; /* low speed */
  499. break;
  500. case 2 << 10:
  501. sig = "j";
  502. break;
  503. default:
  504. sig = "?";
  505. break;
  506. }
  507. return scnprintf(buf, len,
  508. "%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s",
  509. label, label[0] ? " " : "", port, status,
  510. (status & PORT_POWER) ? " POWER" : "",
  511. (status & PORT_OWNER) ? " OWNER" : "",
  512. sig,
  513. (status & PORT_RESET) ? " RESET" : "",
  514. (status & PORT_SUSPEND) ? " SUSPEND" : "",
  515. (status & PORT_RESUME) ? " RESUME" : "",
  516. (status & PORT_OCC) ? " OCC" : "",
  517. (status & PORT_OC) ? " OC" : "",
  518. (status & PORT_PEC) ? " PEC" : "",
  519. (status & PORT_PE) ? " PE" : "",
  520. (status & PORT_CSC) ? " CSC" : "",
  521. (status & PORT_CONNECT) ? " CONNECT" : ""
  522. );
  523. }
  524. #else
  525. static inline int __attribute__((__unused__))
  526. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  527. { return 0; }
  528. static inline int __attribute__((__unused__))
  529. dbg_command_buf(char *buf, unsigned len, const char *label, u32 command)
  530. { return 0; }
  531. static inline int __attribute__((__unused__))
  532. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  533. { return 0; }
  534. static inline int __attribute__((__unused__))
  535. dbg_port_buf(char *buf, unsigned len, const char *label, int port, u32 status)
  536. { return 0; }
  537. #endif /* DEBUG */
  538. /* functions have the "wrong" filename when they're output... */
  539. #define dbg_status(oxu, label, status) { \
  540. char _buf[80]; \
  541. dbg_status_buf(_buf, sizeof _buf, label, status); \
  542. oxu_dbg(oxu, "%s\n", _buf); \
  543. }
  544. #define dbg_cmd(oxu, label, command) { \
  545. char _buf[80]; \
  546. dbg_command_buf(_buf, sizeof _buf, label, command); \
  547. oxu_dbg(oxu, "%s\n", _buf); \
  548. }
  549. #define dbg_port(oxu, label, port, status) { \
  550. char _buf[80]; \
  551. dbg_port_buf(_buf, sizeof _buf, label, port, status); \
  552. oxu_dbg(oxu, "%s\n", _buf); \
  553. }
  554. /*
  555. * Module parameters
  556. */
  557. /* Initial IRQ latency: faster than hw default */
  558. static int log2_irq_thresh; /* 0 to 6 */
  559. module_param(log2_irq_thresh, int, S_IRUGO);
  560. MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  561. /* Initial park setting: slower than hw default */
  562. static unsigned park;
  563. module_param(park, uint, S_IRUGO);
  564. MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  565. /* For flakey hardware, ignore overcurrent indicators */
  566. static bool ignore_oc;
  567. module_param(ignore_oc, bool, S_IRUGO);
  568. MODULE_PARM_DESC(ignore_oc, "ignore bogus hardware overcurrent indications");
  569. static void ehci_work(struct oxu_hcd *oxu);
  570. static int oxu_hub_control(struct usb_hcd *hcd,
  571. u16 typeReq, u16 wValue, u16 wIndex,
  572. char *buf, u16 wLength);
  573. /*
  574. * Local functions
  575. */
  576. /* Low level read/write registers functions */
  577. static inline u32 oxu_readl(void __iomem *base, u32 reg)
  578. {
  579. return readl(base + reg);
  580. }
  581. static inline void oxu_writel(void __iomem *base, u32 reg, u32 val)
  582. {
  583. writel(val, base + reg);
  584. }
  585. static inline void timer_action_done(struct oxu_hcd *oxu,
  586. enum ehci_timer_action action)
  587. {
  588. clear_bit(action, &oxu->actions);
  589. }
  590. static inline void timer_action(struct oxu_hcd *oxu,
  591. enum ehci_timer_action action)
  592. {
  593. if (!test_and_set_bit(action, &oxu->actions)) {
  594. unsigned long t;
  595. switch (action) {
  596. case TIMER_IAA_WATCHDOG:
  597. t = EHCI_IAA_JIFFIES;
  598. break;
  599. case TIMER_IO_WATCHDOG:
  600. t = EHCI_IO_JIFFIES;
  601. break;
  602. case TIMER_ASYNC_OFF:
  603. t = EHCI_ASYNC_JIFFIES;
  604. break;
  605. case TIMER_ASYNC_SHRINK:
  606. default:
  607. t = EHCI_SHRINK_JIFFIES;
  608. break;
  609. }
  610. t += jiffies;
  611. /* all timings except IAA watchdog can be overridden.
  612. * async queue SHRINK often precedes IAA. while it's ready
  613. * to go OFF neither can matter, and afterwards the IO
  614. * watchdog stops unless there's still periodic traffic.
  615. */
  616. if (action != TIMER_IAA_WATCHDOG
  617. && t > oxu->watchdog.expires
  618. && timer_pending(&oxu->watchdog))
  619. return;
  620. mod_timer(&oxu->watchdog, t);
  621. }
  622. }
  623. /*
  624. * handshake - spin reading hc until handshake completes or fails
  625. * @ptr: address of hc register to be read
  626. * @mask: bits to look at in result of read
  627. * @done: value of those bits when handshake succeeds
  628. * @usec: timeout in microseconds
  629. *
  630. * Returns negative errno, or zero on success
  631. *
  632. * Success happens when the "mask" bits have the specified value (hardware
  633. * handshake done). There are two failure modes: "usec" have passed (major
  634. * hardware flakeout), or the register reads as all-ones (hardware removed).
  635. *
  636. * That last failure should_only happen in cases like physical cardbus eject
  637. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  638. * bridge shutdown: shutting down the bridge before the devices using it.
  639. */
  640. static int handshake(struct oxu_hcd *oxu, void __iomem *ptr,
  641. u32 mask, u32 done, int usec)
  642. {
  643. u32 result;
  644. int ret;
  645. ret = readl_poll_timeout_atomic(ptr, result,
  646. ((result & mask) == done ||
  647. result == U32_MAX),
  648. 1, usec);
  649. if (result == U32_MAX) /* card removed */
  650. return -ENODEV;
  651. return ret;
  652. }
  653. /* Force HC to halt state from unknown (EHCI spec section 2.3) */
  654. static int ehci_halt(struct oxu_hcd *oxu)
  655. {
  656. u32 temp = readl(&oxu->regs->status);
  657. /* disable any irqs left enabled by previous code */
  658. writel(0, &oxu->regs->intr_enable);
  659. if ((temp & STS_HALT) != 0)
  660. return 0;
  661. temp = readl(&oxu->regs->command);
  662. temp &= ~CMD_RUN;
  663. writel(temp, &oxu->regs->command);
  664. return handshake(oxu, &oxu->regs->status,
  665. STS_HALT, STS_HALT, 16 * 125);
  666. }
  667. /* Put TDI/ARC silicon into EHCI mode */
  668. static void tdi_reset(struct oxu_hcd *oxu)
  669. {
  670. u32 __iomem *reg_ptr;
  671. u32 tmp;
  672. reg_ptr = (u32 __iomem *)(((u8 __iomem *)oxu->regs) + 0x68);
  673. tmp = readl(reg_ptr);
  674. tmp |= 0x3;
  675. writel(tmp, reg_ptr);
  676. }
  677. /* Reset a non-running (STS_HALT == 1) controller */
  678. static int ehci_reset(struct oxu_hcd *oxu)
  679. {
  680. int retval;
  681. u32 command = readl(&oxu->regs->command);
  682. command |= CMD_RESET;
  683. dbg_cmd(oxu, "reset", command);
  684. writel(command, &oxu->regs->command);
  685. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  686. oxu->next_statechange = jiffies;
  687. retval = handshake(oxu, &oxu->regs->command,
  688. CMD_RESET, 0, 250 * 1000);
  689. if (retval)
  690. return retval;
  691. tdi_reset(oxu);
  692. return retval;
  693. }
  694. /* Idle the controller (from running) */
  695. static void ehci_quiesce(struct oxu_hcd *oxu)
  696. {
  697. u32 temp;
  698. #ifdef DEBUG
  699. BUG_ON(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state));
  700. #endif
  701. /* wait for any schedule enables/disables to take effect */
  702. temp = readl(&oxu->regs->command) << 10;
  703. temp &= STS_ASS | STS_PSS;
  704. if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
  705. temp, 16 * 125) != 0) {
  706. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  707. return;
  708. }
  709. /* then disable anything that's still active */
  710. temp = readl(&oxu->regs->command);
  711. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  712. writel(temp, &oxu->regs->command);
  713. /* hardware can take 16 microframes to turn off ... */
  714. if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
  715. 0, 16 * 125) != 0) {
  716. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  717. return;
  718. }
  719. }
  720. static int check_reset_complete(struct oxu_hcd *oxu, int index,
  721. u32 __iomem *status_reg, int port_status)
  722. {
  723. if (!(port_status & PORT_CONNECT)) {
  724. oxu->reset_done[index] = 0;
  725. return port_status;
  726. }
  727. /* if reset finished and it's still not enabled -- handoff */
  728. if (!(port_status & PORT_PE)) {
  729. oxu_dbg(oxu, "Failed to enable port %d on root hub TT\n",
  730. index+1);
  731. return port_status;
  732. } else
  733. oxu_dbg(oxu, "port %d high speed\n", index + 1);
  734. return port_status;
  735. }
  736. static void ehci_hub_descriptor(struct oxu_hcd *oxu,
  737. struct usb_hub_descriptor *desc)
  738. {
  739. int ports = HCS_N_PORTS(oxu->hcs_params);
  740. u16 temp;
  741. desc->bDescriptorType = USB_DT_HUB;
  742. desc->bPwrOn2PwrGood = 10; /* oxu 1.0, 2.3.9 says 20ms max */
  743. desc->bHubContrCurrent = 0;
  744. desc->bNbrPorts = ports;
  745. temp = 1 + (ports / 8);
  746. desc->bDescLength = 7 + 2 * temp;
  747. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  748. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  749. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  750. temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  751. if (HCS_PPC(oxu->hcs_params))
  752. temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
  753. else
  754. temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  755. desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
  756. }
  757. /* Allocate an OXU210HP on-chip memory data buffer
  758. *
  759. * An on-chip memory data buffer is required for each OXU210HP USB transfer.
  760. * Each transfer descriptor has one or more on-chip memory data buffers.
  761. *
  762. * Data buffers are allocated from a fix sized pool of data blocks.
  763. * To minimise fragmentation and give reasonable memory utlisation,
  764. * data buffers are allocated with sizes the power of 2 multiples of
  765. * the block size, starting on an address a multiple of the allocated size.
  766. *
  767. * FIXME: callers of this function require a buffer to be allocated for
  768. * len=0. This is a waste of on-chip memory and should be fix. Then this
  769. * function should be changed to not allocate a buffer for len=0.
  770. */
  771. static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)
  772. {
  773. int n_blocks; /* minium blocks needed to hold len */
  774. int a_blocks; /* blocks allocated */
  775. int i, j;
  776. /* Don't allocte bigger than supported */
  777. if (len > BUFFER_SIZE * BUFFER_NUM) {
  778. oxu_err(oxu, "buffer too big (%d)\n", len);
  779. return -ENOMEM;
  780. }
  781. spin_lock(&oxu->mem_lock);
  782. /* Number of blocks needed to hold len */
  783. n_blocks = (len + BUFFER_SIZE - 1) / BUFFER_SIZE;
  784. /* Round the number of blocks up to the power of 2 */
  785. for (a_blocks = 1; a_blocks < n_blocks; a_blocks <<= 1)
  786. ;
  787. /* Find a suitable available data buffer */
  788. for (i = 0; i < BUFFER_NUM;
  789. i += max(a_blocks, (int)oxu->db_used[i])) {
  790. /* Check all the required blocks are available */
  791. for (j = 0; j < a_blocks; j++)
  792. if (oxu->db_used[i + j])
  793. break;
  794. if (j != a_blocks)
  795. continue;
  796. /* Allocate blocks found! */
  797. qtd->buffer = (void *) &oxu->mem->db_pool[i];
  798. qtd->buffer_dma = virt_to_phys(qtd->buffer);
  799. qtd->qtd_buffer_len = BUFFER_SIZE * a_blocks;
  800. oxu->db_used[i] = a_blocks;
  801. spin_unlock(&oxu->mem_lock);
  802. return 0;
  803. }
  804. /* Failed */
  805. spin_unlock(&oxu->mem_lock);
  806. return -ENOMEM;
  807. }
  808. static void oxu_buf_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
  809. {
  810. int index;
  811. spin_lock(&oxu->mem_lock);
  812. index = (qtd->buffer - (void *) &oxu->mem->db_pool[0])
  813. / BUFFER_SIZE;
  814. oxu->db_used[index] = 0;
  815. qtd->qtd_buffer_len = 0;
  816. qtd->buffer_dma = 0;
  817. qtd->buffer = NULL;
  818. spin_unlock(&oxu->mem_lock);
  819. }
  820. static inline void ehci_qtd_init(struct ehci_qtd *qtd, dma_addr_t dma)
  821. {
  822. memset(qtd, 0, sizeof *qtd);
  823. qtd->qtd_dma = dma;
  824. qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
  825. qtd->hw_next = EHCI_LIST_END;
  826. qtd->hw_alt_next = EHCI_LIST_END;
  827. INIT_LIST_HEAD(&qtd->qtd_list);
  828. }
  829. static inline void oxu_qtd_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
  830. {
  831. int index;
  832. if (qtd->buffer)
  833. oxu_buf_free(oxu, qtd);
  834. spin_lock(&oxu->mem_lock);
  835. index = qtd - &oxu->mem->qtd_pool[0];
  836. oxu->qtd_used[index] = 0;
  837. spin_unlock(&oxu->mem_lock);
  838. }
  839. static struct ehci_qtd *ehci_qtd_alloc(struct oxu_hcd *oxu)
  840. {
  841. int i;
  842. struct ehci_qtd *qtd = NULL;
  843. spin_lock(&oxu->mem_lock);
  844. for (i = 0; i < QTD_NUM; i++)
  845. if (!oxu->qtd_used[i])
  846. break;
  847. if (i < QTD_NUM) {
  848. qtd = (struct ehci_qtd *) &oxu->mem->qtd_pool[i];
  849. memset(qtd, 0, sizeof *qtd);
  850. qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
  851. qtd->hw_next = EHCI_LIST_END;
  852. qtd->hw_alt_next = EHCI_LIST_END;
  853. INIT_LIST_HEAD(&qtd->qtd_list);
  854. qtd->qtd_dma = virt_to_phys(qtd);
  855. oxu->qtd_used[i] = 1;
  856. }
  857. spin_unlock(&oxu->mem_lock);
  858. return qtd;
  859. }
  860. static void oxu_qh_free(struct oxu_hcd *oxu, struct ehci_qh *qh)
  861. {
  862. int index;
  863. spin_lock(&oxu->mem_lock);
  864. index = qh - &oxu->mem->qh_pool[0];
  865. oxu->qh_used[index] = 0;
  866. spin_unlock(&oxu->mem_lock);
  867. }
  868. static void qh_destroy(struct kref *kref)
  869. {
  870. struct ehci_qh *qh = container_of(kref, struct ehci_qh, kref);
  871. struct oxu_hcd *oxu = qh->oxu;
  872. /* clean qtds first, and know this is not linked */
  873. if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  874. oxu_dbg(oxu, "unused qh not empty!\n");
  875. BUG();
  876. }
  877. if (qh->dummy)
  878. oxu_qtd_free(oxu, qh->dummy);
  879. oxu_qh_free(oxu, qh);
  880. }
  881. static struct ehci_qh *oxu_qh_alloc(struct oxu_hcd *oxu)
  882. {
  883. int i;
  884. struct ehci_qh *qh = NULL;
  885. spin_lock(&oxu->mem_lock);
  886. for (i = 0; i < QHEAD_NUM; i++)
  887. if (!oxu->qh_used[i])
  888. break;
  889. if (i < QHEAD_NUM) {
  890. qh = (struct ehci_qh *) &oxu->mem->qh_pool[i];
  891. memset(qh, 0, sizeof *qh);
  892. kref_init(&qh->kref);
  893. qh->oxu = oxu;
  894. qh->qh_dma = virt_to_phys(qh);
  895. INIT_LIST_HEAD(&qh->qtd_list);
  896. /* dummy td enables safe urb queuing */
  897. qh->dummy = ehci_qtd_alloc(oxu);
  898. if (qh->dummy == NULL) {
  899. oxu_dbg(oxu, "no dummy td\n");
  900. oxu->qh_used[i] = 0;
  901. qh = NULL;
  902. goto unlock;
  903. }
  904. oxu->qh_used[i] = 1;
  905. }
  906. unlock:
  907. spin_unlock(&oxu->mem_lock);
  908. return qh;
  909. }
  910. /* to share a qh (cpu threads, or hc) */
  911. static inline struct ehci_qh *qh_get(struct ehci_qh *qh)
  912. {
  913. kref_get(&qh->kref);
  914. return qh;
  915. }
  916. static inline void qh_put(struct ehci_qh *qh)
  917. {
  918. kref_put(&qh->kref, qh_destroy);
  919. }
  920. static void oxu_murb_free(struct oxu_hcd *oxu, struct oxu_murb *murb)
  921. {
  922. int index;
  923. spin_lock(&oxu->mem_lock);
  924. index = murb - &oxu->murb_pool[0];
  925. oxu->murb_used[index] = 0;
  926. spin_unlock(&oxu->mem_lock);
  927. }
  928. static struct oxu_murb *oxu_murb_alloc(struct oxu_hcd *oxu)
  929. {
  930. int i;
  931. struct oxu_murb *murb = NULL;
  932. spin_lock(&oxu->mem_lock);
  933. for (i = 0; i < MURB_NUM; i++)
  934. if (!oxu->murb_used[i])
  935. break;
  936. if (i < MURB_NUM) {
  937. murb = &(oxu->murb_pool)[i];
  938. oxu->murb_used[i] = 1;
  939. }
  940. spin_unlock(&oxu->mem_lock);
  941. return murb;
  942. }
  943. /* The queue heads and transfer descriptors are managed from pools tied
  944. * to each of the "per device" structures.
  945. * This is the initialisation and cleanup code.
  946. */
  947. static void ehci_mem_cleanup(struct oxu_hcd *oxu)
  948. {
  949. kfree(oxu->murb_pool);
  950. oxu->murb_pool = NULL;
  951. if (oxu->async)
  952. qh_put(oxu->async);
  953. oxu->async = NULL;
  954. del_timer(&oxu->urb_timer);
  955. oxu->periodic = NULL;
  956. /* shadow periodic table */
  957. kfree(oxu->pshadow);
  958. oxu->pshadow = NULL;
  959. }
  960. /* Remember to add cleanup code (above) if you add anything here.
  961. */
  962. static int ehci_mem_init(struct oxu_hcd *oxu, gfp_t flags)
  963. {
  964. int i;
  965. for (i = 0; i < oxu->periodic_size; i++)
  966. oxu->mem->frame_list[i] = EHCI_LIST_END;
  967. for (i = 0; i < QHEAD_NUM; i++)
  968. oxu->qh_used[i] = 0;
  969. for (i = 0; i < QTD_NUM; i++)
  970. oxu->qtd_used[i] = 0;
  971. oxu->murb_pool = kcalloc(MURB_NUM, sizeof(struct oxu_murb), flags);
  972. if (!oxu->murb_pool)
  973. goto fail;
  974. for (i = 0; i < MURB_NUM; i++)
  975. oxu->murb_used[i] = 0;
  976. oxu->async = oxu_qh_alloc(oxu);
  977. if (!oxu->async)
  978. goto fail;
  979. oxu->periodic = (__le32 *) &oxu->mem->frame_list;
  980. oxu->periodic_dma = virt_to_phys(oxu->periodic);
  981. for (i = 0; i < oxu->periodic_size; i++)
  982. oxu->periodic[i] = EHCI_LIST_END;
  983. /* software shadow of hardware table */
  984. oxu->pshadow = kcalloc(oxu->periodic_size, sizeof(void *), flags);
  985. if (oxu->pshadow != NULL)
  986. return 0;
  987. fail:
  988. oxu_dbg(oxu, "couldn't init memory\n");
  989. ehci_mem_cleanup(oxu);
  990. return -ENOMEM;
  991. }
  992. /* Fill a qtd, returning how much of the buffer we were able to queue up.
  993. */
  994. static int qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf, size_t len,
  995. int token, int maxpacket)
  996. {
  997. int i, count;
  998. u64 addr = buf;
  999. /* one buffer entry per 4K ... first might be short or unaligned */
  1000. qtd->hw_buf[0] = cpu_to_le32((u32)addr);
  1001. qtd->hw_buf_hi[0] = cpu_to_le32((u32)(addr >> 32));
  1002. count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  1003. if (likely(len < count)) /* ... iff needed */
  1004. count = len;
  1005. else {
  1006. buf += 0x1000;
  1007. buf &= ~0x0fff;
  1008. /* per-qtd limit: from 16K to 20K (best alignment) */
  1009. for (i = 1; count < len && i < 5; i++) {
  1010. addr = buf;
  1011. qtd->hw_buf[i] = cpu_to_le32((u32)addr);
  1012. qtd->hw_buf_hi[i] = cpu_to_le32((u32)(addr >> 32));
  1013. buf += 0x1000;
  1014. if ((count + 0x1000) < len)
  1015. count += 0x1000;
  1016. else
  1017. count = len;
  1018. }
  1019. /* short packets may only terminate transfers */
  1020. if (count != len)
  1021. count -= (count % maxpacket);
  1022. }
  1023. qtd->hw_token = cpu_to_le32((count << 16) | token);
  1024. qtd->length = count;
  1025. return count;
  1026. }
  1027. static inline void qh_update(struct oxu_hcd *oxu,
  1028. struct ehci_qh *qh, struct ehci_qtd *qtd)
  1029. {
  1030. /* writes to an active overlay are unsafe */
  1031. BUG_ON(qh->qh_state != QH_STATE_IDLE);
  1032. qh->hw_qtd_next = QTD_NEXT(qtd->qtd_dma);
  1033. qh->hw_alt_next = EHCI_LIST_END;
  1034. /* Except for control endpoints, we make hardware maintain data
  1035. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  1036. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  1037. * ever clear it.
  1038. */
  1039. if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) {
  1040. unsigned is_out, epnum;
  1041. is_out = !(qtd->hw_token & cpu_to_le32(1 << 8));
  1042. epnum = (le32_to_cpup(&qh->hw_info1) >> 8) & 0x0f;
  1043. if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  1044. qh->hw_token &= ~cpu_to_le32(QTD_TOGGLE);
  1045. usb_settoggle(qh->dev, epnum, is_out, 1);
  1046. }
  1047. }
  1048. /* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
  1049. wmb();
  1050. qh->hw_token &= cpu_to_le32(QTD_TOGGLE | QTD_STS_PING);
  1051. }
  1052. /* If it weren't for a common silicon quirk (writing the dummy into the qh
  1053. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  1054. * recovery (including urb dequeue) would need software changes to a QH...
  1055. */
  1056. static void qh_refresh(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1057. {
  1058. struct ehci_qtd *qtd;
  1059. if (list_empty(&qh->qtd_list))
  1060. qtd = qh->dummy;
  1061. else {
  1062. qtd = list_entry(qh->qtd_list.next,
  1063. struct ehci_qtd, qtd_list);
  1064. /* first qtd may already be partially processed */
  1065. if (cpu_to_le32(qtd->qtd_dma) == qh->hw_current)
  1066. qtd = NULL;
  1067. }
  1068. if (qtd)
  1069. qh_update(oxu, qh, qtd);
  1070. }
  1071. static void qtd_copy_status(struct oxu_hcd *oxu, struct urb *urb,
  1072. size_t length, u32 token)
  1073. {
  1074. /* count IN/OUT bytes, not SETUP (even short packets) */
  1075. if (likely(QTD_PID(token) != 2))
  1076. urb->actual_length += length - QTD_LENGTH(token);
  1077. /* don't modify error codes */
  1078. if (unlikely(urb->status != -EINPROGRESS))
  1079. return;
  1080. /* force cleanup after short read; not always an error */
  1081. if (unlikely(IS_SHORT_READ(token)))
  1082. urb->status = -EREMOTEIO;
  1083. /* serious "can't proceed" faults reported by the hardware */
  1084. if (token & QTD_STS_HALT) {
  1085. if (token & QTD_STS_BABBLE) {
  1086. /* FIXME "must" disable babbling device's port too */
  1087. urb->status = -EOVERFLOW;
  1088. } else if (token & QTD_STS_MMF) {
  1089. /* fs/ls interrupt xfer missed the complete-split */
  1090. urb->status = -EPROTO;
  1091. } else if (token & QTD_STS_DBE) {
  1092. urb->status = (QTD_PID(token) == 1) /* IN ? */
  1093. ? -ENOSR /* hc couldn't read data */
  1094. : -ECOMM; /* hc couldn't write data */
  1095. } else if (token & QTD_STS_XACT) {
  1096. /* timeout, bad crc, wrong PID, etc; retried */
  1097. if (QTD_CERR(token))
  1098. urb->status = -EPIPE;
  1099. else {
  1100. oxu_dbg(oxu, "devpath %s ep%d%s 3strikes\n",
  1101. urb->dev->devpath,
  1102. usb_pipeendpoint(urb->pipe),
  1103. usb_pipein(urb->pipe) ? "in" : "out");
  1104. urb->status = -EPROTO;
  1105. }
  1106. /* CERR nonzero + no errors + halt --> stall */
  1107. } else if (QTD_CERR(token))
  1108. urb->status = -EPIPE;
  1109. else /* unknown */
  1110. urb->status = -EPROTO;
  1111. oxu_vdbg(oxu, "dev%d ep%d%s qtd token %08x --> status %d\n",
  1112. usb_pipedevice(urb->pipe),
  1113. usb_pipeendpoint(urb->pipe),
  1114. usb_pipein(urb->pipe) ? "in" : "out",
  1115. token, urb->status);
  1116. }
  1117. }
  1118. static void ehci_urb_done(struct oxu_hcd *oxu, struct urb *urb)
  1119. __releases(oxu->lock)
  1120. __acquires(oxu->lock)
  1121. {
  1122. if (likely(urb->hcpriv != NULL)) {
  1123. struct ehci_qh *qh = (struct ehci_qh *) urb->hcpriv;
  1124. /* S-mask in a QH means it's an interrupt urb */
  1125. if ((qh->hw_info2 & cpu_to_le32(QH_SMASK)) != 0) {
  1126. /* ... update hc-wide periodic stats (for usbfs) */
  1127. oxu_to_hcd(oxu)->self.bandwidth_int_reqs--;
  1128. }
  1129. qh_put(qh);
  1130. }
  1131. urb->hcpriv = NULL;
  1132. switch (urb->status) {
  1133. case -EINPROGRESS: /* success */
  1134. urb->status = 0;
  1135. break;
  1136. default: /* fault */
  1137. break;
  1138. case -EREMOTEIO: /* fault or normal */
  1139. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  1140. urb->status = 0;
  1141. break;
  1142. case -ECONNRESET: /* canceled */
  1143. case -ENOENT:
  1144. break;
  1145. }
  1146. #ifdef OXU_URB_TRACE
  1147. oxu_dbg(oxu, "%s %s urb %p ep%d%s status %d len %d/%d\n",
  1148. __func__, urb->dev->devpath, urb,
  1149. usb_pipeendpoint(urb->pipe),
  1150. usb_pipein(urb->pipe) ? "in" : "out",
  1151. urb->status,
  1152. urb->actual_length, urb->transfer_buffer_length);
  1153. #endif
  1154. /* complete() can reenter this HCD */
  1155. spin_unlock(&oxu->lock);
  1156. usb_hcd_giveback_urb(oxu_to_hcd(oxu), urb, urb->status);
  1157. spin_lock(&oxu->lock);
  1158. }
  1159. static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
  1160. static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
  1161. static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
  1162. static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
  1163. #define HALT_BIT cpu_to_le32(QTD_STS_HALT)
  1164. /* Process and free completed qtds for a qh, returning URBs to drivers.
  1165. * Chases up to qh->hw_current. Returns number of completions called,
  1166. * indicating how much "real" work we did.
  1167. */
  1168. static unsigned qh_completions(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1169. {
  1170. struct ehci_qtd *last = NULL, *end = qh->dummy;
  1171. struct ehci_qtd *qtd, *tmp;
  1172. int stopped;
  1173. unsigned count = 0;
  1174. int do_status = 0;
  1175. u8 state;
  1176. struct oxu_murb *murb = NULL;
  1177. if (unlikely(list_empty(&qh->qtd_list)))
  1178. return count;
  1179. /* completions (or tasks on other cpus) must never clobber HALT
  1180. * till we've gone through and cleaned everything up, even when
  1181. * they add urbs to this qh's queue or mark them for unlinking.
  1182. *
  1183. * NOTE: unlinking expects to be done in queue order.
  1184. */
  1185. state = qh->qh_state;
  1186. qh->qh_state = QH_STATE_COMPLETING;
  1187. stopped = (state == QH_STATE_IDLE);
  1188. /* remove de-activated QTDs from front of queue.
  1189. * after faults (including short reads), cleanup this urb
  1190. * then let the queue advance.
  1191. * if queue is stopped, handles unlinks.
  1192. */
  1193. list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
  1194. struct urb *urb;
  1195. u32 token = 0;
  1196. urb = qtd->urb;
  1197. /* Clean up any state from previous QTD ...*/
  1198. if (last) {
  1199. if (likely(last->urb != urb)) {
  1200. if (last->urb->complete == NULL) {
  1201. murb = (struct oxu_murb *) last->urb;
  1202. last->urb = murb->main;
  1203. if (murb->last) {
  1204. ehci_urb_done(oxu, last->urb);
  1205. count++;
  1206. }
  1207. oxu_murb_free(oxu, murb);
  1208. } else {
  1209. ehci_urb_done(oxu, last->urb);
  1210. count++;
  1211. }
  1212. }
  1213. oxu_qtd_free(oxu, last);
  1214. last = NULL;
  1215. }
  1216. /* ignore urbs submitted during completions we reported */
  1217. if (qtd == end)
  1218. break;
  1219. /* hardware copies qtd out of qh overlay */
  1220. rmb();
  1221. token = le32_to_cpu(qtd->hw_token);
  1222. /* always clean up qtds the hc de-activated */
  1223. if ((token & QTD_STS_ACTIVE) == 0) {
  1224. if ((token & QTD_STS_HALT) != 0) {
  1225. stopped = 1;
  1226. /* magic dummy for some short reads; qh won't advance.
  1227. * that silicon quirk can kick in with this dummy too.
  1228. */
  1229. } else if (IS_SHORT_READ(token) &&
  1230. !(qtd->hw_alt_next & EHCI_LIST_END)) {
  1231. stopped = 1;
  1232. goto halt;
  1233. }
  1234. /* stop scanning when we reach qtds the hc is using */
  1235. } else if (likely(!stopped &&
  1236. HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) {
  1237. break;
  1238. } else {
  1239. stopped = 1;
  1240. if (unlikely(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)))
  1241. urb->status = -ESHUTDOWN;
  1242. /* ignore active urbs unless some previous qtd
  1243. * for the urb faulted (including short read) or
  1244. * its urb was canceled. we may patch qh or qtds.
  1245. */
  1246. if (likely(urb->status == -EINPROGRESS))
  1247. continue;
  1248. /* issue status after short control reads */
  1249. if (unlikely(do_status != 0)
  1250. && QTD_PID(token) == 0 /* OUT */) {
  1251. do_status = 0;
  1252. continue;
  1253. }
  1254. /* token in overlay may be most current */
  1255. if (state == QH_STATE_IDLE
  1256. && cpu_to_le32(qtd->qtd_dma)
  1257. == qh->hw_current)
  1258. token = le32_to_cpu(qh->hw_token);
  1259. /* force halt for unlinked or blocked qh, so we'll
  1260. * patch the qh later and so that completions can't
  1261. * activate it while we "know" it's stopped.
  1262. */
  1263. if ((HALT_BIT & qh->hw_token) == 0) {
  1264. halt:
  1265. qh->hw_token |= HALT_BIT;
  1266. wmb();
  1267. }
  1268. }
  1269. /* Remove it from the queue */
  1270. qtd_copy_status(oxu, urb->complete ?
  1271. urb : ((struct oxu_murb *) urb)->main,
  1272. qtd->length, token);
  1273. if ((usb_pipein(qtd->urb->pipe)) &&
  1274. (NULL != qtd->transfer_buffer))
  1275. memcpy(qtd->transfer_buffer, qtd->buffer, qtd->length);
  1276. do_status = (urb->status == -EREMOTEIO)
  1277. && usb_pipecontrol(urb->pipe);
  1278. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  1279. last = list_entry(qtd->qtd_list.prev,
  1280. struct ehci_qtd, qtd_list);
  1281. last->hw_next = qtd->hw_next;
  1282. }
  1283. list_del(&qtd->qtd_list);
  1284. last = qtd;
  1285. }
  1286. /* last urb's completion might still need calling */
  1287. if (likely(last != NULL)) {
  1288. if (last->urb->complete == NULL) {
  1289. murb = (struct oxu_murb *) last->urb;
  1290. last->urb = murb->main;
  1291. if (murb->last) {
  1292. ehci_urb_done(oxu, last->urb);
  1293. count++;
  1294. }
  1295. oxu_murb_free(oxu, murb);
  1296. } else {
  1297. ehci_urb_done(oxu, last->urb);
  1298. count++;
  1299. }
  1300. oxu_qtd_free(oxu, last);
  1301. }
  1302. /* restore original state; caller must unlink or relink */
  1303. qh->qh_state = state;
  1304. /* be sure the hardware's done with the qh before refreshing
  1305. * it after fault cleanup, or recovering from silicon wrongly
  1306. * overlaying the dummy qtd (which reduces DMA chatter).
  1307. */
  1308. if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) {
  1309. switch (state) {
  1310. case QH_STATE_IDLE:
  1311. qh_refresh(oxu, qh);
  1312. break;
  1313. case QH_STATE_LINKED:
  1314. /* should be rare for periodic transfers,
  1315. * except maybe high bandwidth ...
  1316. */
  1317. if ((cpu_to_le32(QH_SMASK)
  1318. & qh->hw_info2) != 0) {
  1319. intr_deschedule(oxu, qh);
  1320. (void) qh_schedule(oxu, qh);
  1321. } else
  1322. unlink_async(oxu, qh);
  1323. break;
  1324. /* otherwise, unlink already started */
  1325. }
  1326. }
  1327. return count;
  1328. }
  1329. /* High bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  1330. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  1331. /* ... and packet size, for any kind of endpoint descriptor */
  1332. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  1333. /* Reverse of qh_urb_transaction: free a list of TDs.
  1334. * used for cleanup after errors, before HC sees an URB's TDs.
  1335. */
  1336. static void qtd_list_free(struct oxu_hcd *oxu,
  1337. struct urb *urb, struct list_head *head)
  1338. {
  1339. struct ehci_qtd *qtd, *temp;
  1340. list_for_each_entry_safe(qtd, temp, head, qtd_list) {
  1341. list_del(&qtd->qtd_list);
  1342. oxu_qtd_free(oxu, qtd);
  1343. }
  1344. }
  1345. /* Create a list of filled qtds for this URB; won't link into qh.
  1346. */
  1347. static struct list_head *qh_urb_transaction(struct oxu_hcd *oxu,
  1348. struct urb *urb,
  1349. struct list_head *head,
  1350. gfp_t flags)
  1351. {
  1352. struct ehci_qtd *qtd, *qtd_prev;
  1353. dma_addr_t buf;
  1354. int len, maxpacket;
  1355. int is_input;
  1356. u32 token;
  1357. void *transfer_buf = NULL;
  1358. int ret;
  1359. /*
  1360. * URBs map to sequences of QTDs: one logical transaction
  1361. */
  1362. qtd = ehci_qtd_alloc(oxu);
  1363. if (unlikely(!qtd))
  1364. return NULL;
  1365. list_add_tail(&qtd->qtd_list, head);
  1366. qtd->urb = urb;
  1367. token = QTD_STS_ACTIVE;
  1368. token |= (EHCI_TUNE_CERR << 10);
  1369. /* for split transactions, SplitXState initialized to zero */
  1370. len = urb->transfer_buffer_length;
  1371. is_input = usb_pipein(urb->pipe);
  1372. if (!urb->transfer_buffer && urb->transfer_buffer_length && is_input)
  1373. urb->transfer_buffer = phys_to_virt(urb->transfer_dma);
  1374. if (usb_pipecontrol(urb->pipe)) {
  1375. /* SETUP pid */
  1376. ret = oxu_buf_alloc(oxu, qtd, sizeof(struct usb_ctrlrequest));
  1377. if (ret)
  1378. goto cleanup;
  1379. qtd_fill(qtd, qtd->buffer_dma, sizeof(struct usb_ctrlrequest),
  1380. token | (2 /* "setup" */ << 8), 8);
  1381. memcpy(qtd->buffer, qtd->urb->setup_packet,
  1382. sizeof(struct usb_ctrlrequest));
  1383. /* ... and always at least one more pid */
  1384. token ^= QTD_TOGGLE;
  1385. qtd_prev = qtd;
  1386. qtd = ehci_qtd_alloc(oxu);
  1387. if (unlikely(!qtd))
  1388. goto cleanup;
  1389. qtd->urb = urb;
  1390. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1391. list_add_tail(&qtd->qtd_list, head);
  1392. /* for zero length DATA stages, STATUS is always IN */
  1393. if (len == 0)
  1394. token |= (1 /* "in" */ << 8);
  1395. }
  1396. /*
  1397. * Data transfer stage: buffer setup
  1398. */
  1399. ret = oxu_buf_alloc(oxu, qtd, len);
  1400. if (ret)
  1401. goto cleanup;
  1402. buf = qtd->buffer_dma;
  1403. transfer_buf = urb->transfer_buffer;
  1404. if (!is_input)
  1405. memcpy(qtd->buffer, qtd->urb->transfer_buffer, len);
  1406. if (is_input)
  1407. token |= (1 /* "in" */ << 8);
  1408. /* else it's already initted to "out" pid (0 << 8) */
  1409. maxpacket = usb_maxpacket(urb->dev, urb->pipe);
  1410. /*
  1411. * buffer gets wrapped in one or more qtds;
  1412. * last one may be "short" (including zero len)
  1413. * and may serve as a control status ack
  1414. */
  1415. for (;;) {
  1416. int this_qtd_len;
  1417. this_qtd_len = qtd_fill(qtd, buf, len, token, maxpacket);
  1418. qtd->transfer_buffer = transfer_buf;
  1419. len -= this_qtd_len;
  1420. buf += this_qtd_len;
  1421. transfer_buf += this_qtd_len;
  1422. if (is_input)
  1423. qtd->hw_alt_next = oxu->async->hw_alt_next;
  1424. /* qh makes control packets use qtd toggle; maybe switch it */
  1425. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  1426. token ^= QTD_TOGGLE;
  1427. if (likely(len <= 0))
  1428. break;
  1429. qtd_prev = qtd;
  1430. qtd = ehci_qtd_alloc(oxu);
  1431. if (unlikely(!qtd))
  1432. goto cleanup;
  1433. if (likely(len > 0)) {
  1434. ret = oxu_buf_alloc(oxu, qtd, len);
  1435. if (ret)
  1436. goto cleanup;
  1437. }
  1438. qtd->urb = urb;
  1439. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1440. list_add_tail(&qtd->qtd_list, head);
  1441. }
  1442. /* unless the bulk/interrupt caller wants a chance to clean
  1443. * up after short reads, hc should advance qh past this urb
  1444. */
  1445. if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
  1446. || usb_pipecontrol(urb->pipe)))
  1447. qtd->hw_alt_next = EHCI_LIST_END;
  1448. /*
  1449. * control requests may need a terminating data "status" ack;
  1450. * bulk ones may need a terminating short packet (zero length).
  1451. */
  1452. if (likely(urb->transfer_buffer_length != 0)) {
  1453. int one_more = 0;
  1454. if (usb_pipecontrol(urb->pipe)) {
  1455. one_more = 1;
  1456. token ^= 0x0100; /* "in" <--> "out" */
  1457. token |= QTD_TOGGLE; /* force DATA1 */
  1458. } else if (usb_pipebulk(urb->pipe)
  1459. && (urb->transfer_flags & URB_ZERO_PACKET)
  1460. && !(urb->transfer_buffer_length % maxpacket)) {
  1461. one_more = 1;
  1462. }
  1463. if (one_more) {
  1464. qtd_prev = qtd;
  1465. qtd = ehci_qtd_alloc(oxu);
  1466. if (unlikely(!qtd))
  1467. goto cleanup;
  1468. qtd->urb = urb;
  1469. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1470. list_add_tail(&qtd->qtd_list, head);
  1471. /* never any data in such packets */
  1472. qtd_fill(qtd, 0, 0, token, 0);
  1473. }
  1474. }
  1475. /* by default, enable interrupt on urb completion */
  1476. qtd->hw_token |= cpu_to_le32(QTD_IOC);
  1477. return head;
  1478. cleanup:
  1479. qtd_list_free(oxu, urb, head);
  1480. return NULL;
  1481. }
  1482. /* Each QH holds a qtd list; a QH is used for everything except iso.
  1483. *
  1484. * For interrupt urbs, the scheduler must set the microframe scheduling
  1485. * mask(s) each time the QH gets scheduled. For highspeed, that's
  1486. * just one microframe in the s-mask. For split interrupt transactions
  1487. * there are additional complications: c-mask, maybe FSTNs.
  1488. */
  1489. static struct ehci_qh *qh_make(struct oxu_hcd *oxu,
  1490. struct urb *urb, gfp_t flags)
  1491. {
  1492. struct ehci_qh *qh = oxu_qh_alloc(oxu);
  1493. u32 info1 = 0, info2 = 0;
  1494. int is_input, type;
  1495. int maxp = 0;
  1496. if (!qh)
  1497. return qh;
  1498. /*
  1499. * init endpoint/device data for this QH
  1500. */
  1501. info1 |= usb_pipeendpoint(urb->pipe) << 8;
  1502. info1 |= usb_pipedevice(urb->pipe) << 0;
  1503. is_input = usb_pipein(urb->pipe);
  1504. type = usb_pipetype(urb->pipe);
  1505. maxp = usb_maxpacket(urb->dev, urb->pipe);
  1506. /* Compute interrupt scheduling parameters just once, and save.
  1507. * - allowing for high bandwidth, how many nsec/uframe are used?
  1508. * - split transactions need a second CSPLIT uframe; same question
  1509. * - splits also need a schedule gap (for full/low speed I/O)
  1510. * - qh has a polling interval
  1511. *
  1512. * For control/bulk requests, the HC or TT handles these.
  1513. */
  1514. if (type == PIPE_INTERRUPT) {
  1515. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  1516. is_input, 0,
  1517. hb_mult(maxp) * max_packet(maxp)));
  1518. qh->start = NO_FRAME;
  1519. if (urb->dev->speed == USB_SPEED_HIGH) {
  1520. qh->c_usecs = 0;
  1521. qh->gap_uf = 0;
  1522. qh->period = urb->interval >> 3;
  1523. if (qh->period == 0 && urb->interval != 1) {
  1524. /* NOTE interval 2 or 4 uframes could work.
  1525. * But interval 1 scheduling is simpler, and
  1526. * includes high bandwidth.
  1527. */
  1528. oxu_dbg(oxu, "intr period %d uframes, NYET!\n",
  1529. urb->interval);
  1530. goto done;
  1531. }
  1532. } else {
  1533. struct usb_tt *tt = urb->dev->tt;
  1534. int think_time;
  1535. /* gap is f(FS/LS transfer times) */
  1536. qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  1537. is_input, 0, maxp) / (125 * 1000);
  1538. /* FIXME this just approximates SPLIT/CSPLIT times */
  1539. if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  1540. qh->c_usecs = qh->usecs + HS_USECS(0);
  1541. qh->usecs = HS_USECS(1);
  1542. } else { /* SPLIT+DATA, gap, CSPLIT */
  1543. qh->usecs += HS_USECS(1);
  1544. qh->c_usecs = HS_USECS(0);
  1545. }
  1546. think_time = tt ? tt->think_time : 0;
  1547. qh->tt_usecs = NS_TO_US(think_time +
  1548. usb_calc_bus_time(urb->dev->speed,
  1549. is_input, 0, max_packet(maxp)));
  1550. qh->period = urb->interval;
  1551. }
  1552. }
  1553. /* support for tt scheduling, and access to toggles */
  1554. qh->dev = urb->dev;
  1555. /* using TT? */
  1556. switch (urb->dev->speed) {
  1557. case USB_SPEED_LOW:
  1558. info1 |= (1 << 12); /* EPS "low" */
  1559. fallthrough;
  1560. case USB_SPEED_FULL:
  1561. /* EPS 0 means "full" */
  1562. if (type != PIPE_INTERRUPT)
  1563. info1 |= (EHCI_TUNE_RL_TT << 28);
  1564. if (type == PIPE_CONTROL) {
  1565. info1 |= (1 << 27); /* for TT */
  1566. info1 |= 1 << 14; /* toggle from qtd */
  1567. }
  1568. info1 |= maxp << 16;
  1569. info2 |= (EHCI_TUNE_MULT_TT << 30);
  1570. info2 |= urb->dev->ttport << 23;
  1571. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  1572. break;
  1573. case USB_SPEED_HIGH: /* no TT involved */
  1574. info1 |= (2 << 12); /* EPS "high" */
  1575. if (type == PIPE_CONTROL) {
  1576. info1 |= (EHCI_TUNE_RL_HS << 28);
  1577. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  1578. info1 |= 1 << 14; /* toggle from qtd */
  1579. info2 |= (EHCI_TUNE_MULT_HS << 30);
  1580. } else if (type == PIPE_BULK) {
  1581. info1 |= (EHCI_TUNE_RL_HS << 28);
  1582. info1 |= 512 << 16; /* usb2 fixed maxpacket */
  1583. info2 |= (EHCI_TUNE_MULT_HS << 30);
  1584. } else { /* PIPE_INTERRUPT */
  1585. info1 |= max_packet(maxp) << 16;
  1586. info2 |= hb_mult(maxp) << 30;
  1587. }
  1588. break;
  1589. default:
  1590. oxu_dbg(oxu, "bogus dev %p speed %d\n", urb->dev, urb->dev->speed);
  1591. done:
  1592. qh_put(qh);
  1593. return NULL;
  1594. }
  1595. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  1596. /* init as live, toggle clear, advance to dummy */
  1597. qh->qh_state = QH_STATE_IDLE;
  1598. qh->hw_info1 = cpu_to_le32(info1);
  1599. qh->hw_info2 = cpu_to_le32(info2);
  1600. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  1601. qh_refresh(oxu, qh);
  1602. return qh;
  1603. }
  1604. /* Move qh (and its qtds) onto async queue; maybe enable queue.
  1605. */
  1606. static void qh_link_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1607. {
  1608. __le32 dma = QH_NEXT(qh->qh_dma);
  1609. struct ehci_qh *head;
  1610. /* (re)start the async schedule? */
  1611. head = oxu->async;
  1612. timer_action_done(oxu, TIMER_ASYNC_OFF);
  1613. if (!head->qh_next.qh) {
  1614. u32 cmd = readl(&oxu->regs->command);
  1615. if (!(cmd & CMD_ASE)) {
  1616. /* in case a clear of CMD_ASE didn't take yet */
  1617. (void)handshake(oxu, &oxu->regs->status,
  1618. STS_ASS, 0, 150);
  1619. cmd |= CMD_ASE | CMD_RUN;
  1620. writel(cmd, &oxu->regs->command);
  1621. oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
  1622. /* posted write need not be known to HC yet ... */
  1623. }
  1624. }
  1625. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  1626. if (qh->qh_state == QH_STATE_IDLE)
  1627. qh_refresh(oxu, qh);
  1628. /* splice right after start */
  1629. qh->qh_next = head->qh_next;
  1630. qh->hw_next = head->hw_next;
  1631. wmb();
  1632. head->qh_next.qh = qh;
  1633. head->hw_next = dma;
  1634. qh->qh_state = QH_STATE_LINKED;
  1635. /* qtd completions reported later by interrupt */
  1636. }
  1637. #define QH_ADDR_MASK cpu_to_le32(0x7f)
  1638. /*
  1639. * For control/bulk/interrupt, return QH with these TDs appended.
  1640. * Allocates and initializes the QH if necessary.
  1641. * Returns null if it can't allocate a QH it needs to.
  1642. * If the QH has TDs (urbs) already, that's great.
  1643. */
  1644. static struct ehci_qh *qh_append_tds(struct oxu_hcd *oxu,
  1645. struct urb *urb, struct list_head *qtd_list,
  1646. int epnum, void **ptr)
  1647. {
  1648. struct ehci_qh *qh = NULL;
  1649. qh = (struct ehci_qh *) *ptr;
  1650. if (unlikely(qh == NULL)) {
  1651. /* can't sleep here, we have oxu->lock... */
  1652. qh = qh_make(oxu, urb, GFP_ATOMIC);
  1653. *ptr = qh;
  1654. }
  1655. if (likely(qh != NULL)) {
  1656. struct ehci_qtd *qtd;
  1657. if (unlikely(list_empty(qtd_list)))
  1658. qtd = NULL;
  1659. else
  1660. qtd = list_entry(qtd_list->next, struct ehci_qtd,
  1661. qtd_list);
  1662. /* control qh may need patching ... */
  1663. if (unlikely(epnum == 0)) {
  1664. /* usb_reset_device() briefly reverts to address 0 */
  1665. if (usb_pipedevice(urb->pipe) == 0)
  1666. qh->hw_info1 &= ~QH_ADDR_MASK;
  1667. }
  1668. /* just one way to queue requests: swap with the dummy qtd.
  1669. * only hc or qh_refresh() ever modify the overlay.
  1670. */
  1671. if (likely(qtd != NULL)) {
  1672. struct ehci_qtd *dummy;
  1673. dma_addr_t dma;
  1674. __le32 token;
  1675. /* to avoid racing the HC, use the dummy td instead of
  1676. * the first td of our list (becomes new dummy). both
  1677. * tds stay deactivated until we're done, when the
  1678. * HC is allowed to fetch the old dummy (4.10.2).
  1679. */
  1680. token = qtd->hw_token;
  1681. qtd->hw_token = HALT_BIT;
  1682. wmb();
  1683. dummy = qh->dummy;
  1684. dma = dummy->qtd_dma;
  1685. *dummy = *qtd;
  1686. dummy->qtd_dma = dma;
  1687. list_del(&qtd->qtd_list);
  1688. list_add(&dummy->qtd_list, qtd_list);
  1689. list_splice(qtd_list, qh->qtd_list.prev);
  1690. ehci_qtd_init(qtd, qtd->qtd_dma);
  1691. qh->dummy = qtd;
  1692. /* hc must see the new dummy at list end */
  1693. dma = qtd->qtd_dma;
  1694. qtd = list_entry(qh->qtd_list.prev,
  1695. struct ehci_qtd, qtd_list);
  1696. qtd->hw_next = QTD_NEXT(dma);
  1697. /* let the hc process these next qtds */
  1698. dummy->hw_token = (token & ~(0x80));
  1699. wmb();
  1700. dummy->hw_token = token;
  1701. urb->hcpriv = qh_get(qh);
  1702. }
  1703. }
  1704. return qh;
  1705. }
  1706. static int submit_async(struct oxu_hcd *oxu, struct urb *urb,
  1707. struct list_head *qtd_list, gfp_t mem_flags)
  1708. {
  1709. int epnum = urb->ep->desc.bEndpointAddress;
  1710. unsigned long flags;
  1711. struct ehci_qh *qh = NULL;
  1712. int rc = 0;
  1713. #ifdef OXU_URB_TRACE
  1714. struct ehci_qtd *qtd;
  1715. qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
  1716. oxu_dbg(oxu, "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  1717. __func__, urb->dev->devpath, urb,
  1718. epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
  1719. urb->transfer_buffer_length,
  1720. qtd, urb->ep->hcpriv);
  1721. #endif
  1722. spin_lock_irqsave(&oxu->lock, flags);
  1723. if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
  1724. rc = -ESHUTDOWN;
  1725. goto done;
  1726. }
  1727. qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
  1728. if (unlikely(qh == NULL)) {
  1729. rc = -ENOMEM;
  1730. goto done;
  1731. }
  1732. /* Control/bulk operations through TTs don't need scheduling,
  1733. * the HC and TT handle it when the TT has a buffer ready.
  1734. */
  1735. if (likely(qh->qh_state == QH_STATE_IDLE))
  1736. qh_link_async(oxu, qh_get(qh));
  1737. done:
  1738. spin_unlock_irqrestore(&oxu->lock, flags);
  1739. if (unlikely(qh == NULL))
  1740. qtd_list_free(oxu, urb, qtd_list);
  1741. return rc;
  1742. }
  1743. /* The async qh for the qtds being reclaimed are now unlinked from the HC */
  1744. static void end_unlink_async(struct oxu_hcd *oxu)
  1745. {
  1746. struct ehci_qh *qh = oxu->reclaim;
  1747. struct ehci_qh *next;
  1748. timer_action_done(oxu, TIMER_IAA_WATCHDOG);
  1749. qh->qh_state = QH_STATE_IDLE;
  1750. qh->qh_next.qh = NULL;
  1751. qh_put(qh); /* refcount from reclaim */
  1752. /* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
  1753. next = qh->reclaim;
  1754. oxu->reclaim = next;
  1755. oxu->reclaim_ready = 0;
  1756. qh->reclaim = NULL;
  1757. qh_completions(oxu, qh);
  1758. if (!list_empty(&qh->qtd_list)
  1759. && HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  1760. qh_link_async(oxu, qh);
  1761. else {
  1762. qh_put(qh); /* refcount from async list */
  1763. /* it's not free to turn the async schedule on/off; leave it
  1764. * active but idle for a while once it empties.
  1765. */
  1766. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state)
  1767. && oxu->async->qh_next.qh == NULL)
  1768. timer_action(oxu, TIMER_ASYNC_OFF);
  1769. }
  1770. if (next) {
  1771. oxu->reclaim = NULL;
  1772. start_unlink_async(oxu, next);
  1773. }
  1774. }
  1775. /* makes sure the async qh will become idle */
  1776. /* caller must own oxu->lock */
  1777. static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1778. {
  1779. int cmd = readl(&oxu->regs->command);
  1780. struct ehci_qh *prev;
  1781. #ifdef DEBUG
  1782. assert_spin_locked(&oxu->lock);
  1783. BUG_ON(oxu->reclaim || (qh->qh_state != QH_STATE_LINKED
  1784. && qh->qh_state != QH_STATE_UNLINK_WAIT));
  1785. #endif
  1786. /* stop async schedule right now? */
  1787. if (unlikely(qh == oxu->async)) {
  1788. /* can't get here without STS_ASS set */
  1789. if (oxu_to_hcd(oxu)->state != HC_STATE_HALT
  1790. && !oxu->reclaim) {
  1791. /* ... and CMD_IAAD clear */
  1792. writel(cmd & ~CMD_ASE, &oxu->regs->command);
  1793. wmb();
  1794. /* handshake later, if we need to */
  1795. timer_action_done(oxu, TIMER_ASYNC_OFF);
  1796. }
  1797. return;
  1798. }
  1799. qh->qh_state = QH_STATE_UNLINK;
  1800. oxu->reclaim = qh = qh_get(qh);
  1801. prev = oxu->async;
  1802. while (prev->qh_next.qh != qh)
  1803. prev = prev->qh_next.qh;
  1804. prev->hw_next = qh->hw_next;
  1805. prev->qh_next = qh->qh_next;
  1806. wmb();
  1807. if (unlikely(oxu_to_hcd(oxu)->state == HC_STATE_HALT)) {
  1808. /* if (unlikely(qh->reclaim != 0))
  1809. * this will recurse, probably not much
  1810. */
  1811. end_unlink_async(oxu);
  1812. return;
  1813. }
  1814. oxu->reclaim_ready = 0;
  1815. cmd |= CMD_IAAD;
  1816. writel(cmd, &oxu->regs->command);
  1817. (void) readl(&oxu->regs->command);
  1818. timer_action(oxu, TIMER_IAA_WATCHDOG);
  1819. }
  1820. static void scan_async(struct oxu_hcd *oxu)
  1821. {
  1822. struct ehci_qh *qh;
  1823. enum ehci_timer_action action = TIMER_IO_WATCHDOG;
  1824. if (!++(oxu->stamp))
  1825. oxu->stamp++;
  1826. timer_action_done(oxu, TIMER_ASYNC_SHRINK);
  1827. rescan:
  1828. qh = oxu->async->qh_next.qh;
  1829. if (likely(qh != NULL)) {
  1830. do {
  1831. /* clean any finished work for this qh */
  1832. if (!list_empty(&qh->qtd_list)
  1833. && qh->stamp != oxu->stamp) {
  1834. int temp;
  1835. /* unlinks could happen here; completion
  1836. * reporting drops the lock. rescan using
  1837. * the latest schedule, but don't rescan
  1838. * qhs we already finished (no looping).
  1839. */
  1840. qh = qh_get(qh);
  1841. qh->stamp = oxu->stamp;
  1842. temp = qh_completions(oxu, qh);
  1843. qh_put(qh);
  1844. if (temp != 0)
  1845. goto rescan;
  1846. }
  1847. /* unlink idle entries, reducing HC PCI usage as well
  1848. * as HCD schedule-scanning costs. delay for any qh
  1849. * we just scanned, there's a not-unusual case that it
  1850. * doesn't stay idle for long.
  1851. * (plus, avoids some kind of re-activation race.)
  1852. */
  1853. if (list_empty(&qh->qtd_list)) {
  1854. if (qh->stamp == oxu->stamp)
  1855. action = TIMER_ASYNC_SHRINK;
  1856. else if (!oxu->reclaim
  1857. && qh->qh_state == QH_STATE_LINKED)
  1858. start_unlink_async(oxu, qh);
  1859. }
  1860. qh = qh->qh_next.qh;
  1861. } while (qh);
  1862. }
  1863. if (action == TIMER_ASYNC_SHRINK)
  1864. timer_action(oxu, TIMER_ASYNC_SHRINK);
  1865. }
  1866. /*
  1867. * periodic_next_shadow - return "next" pointer on shadow list
  1868. * @periodic: host pointer to qh/itd/sitd
  1869. * @tag: hardware tag for type of this record
  1870. */
  1871. static union ehci_shadow *periodic_next_shadow(union ehci_shadow *periodic,
  1872. __le32 tag)
  1873. {
  1874. switch (tag) {
  1875. default:
  1876. case Q_TYPE_QH:
  1877. return &periodic->qh->qh_next;
  1878. }
  1879. }
  1880. /* caller must hold oxu->lock */
  1881. static void periodic_unlink(struct oxu_hcd *oxu, unsigned frame, void *ptr)
  1882. {
  1883. union ehci_shadow *prev_p = &oxu->pshadow[frame];
  1884. __le32 *hw_p = &oxu->periodic[frame];
  1885. union ehci_shadow here = *prev_p;
  1886. /* find predecessor of "ptr"; hw and shadow lists are in sync */
  1887. while (here.ptr && here.ptr != ptr) {
  1888. prev_p = periodic_next_shadow(prev_p, Q_NEXT_TYPE(*hw_p));
  1889. hw_p = here.hw_next;
  1890. here = *prev_p;
  1891. }
  1892. /* an interrupt entry (at list end) could have been shared */
  1893. if (!here.ptr)
  1894. return;
  1895. /* update shadow and hardware lists ... the old "next" pointers
  1896. * from ptr may still be in use, the caller updates them.
  1897. */
  1898. *prev_p = *periodic_next_shadow(&here, Q_NEXT_TYPE(*hw_p));
  1899. *hw_p = *here.hw_next;
  1900. }
  1901. /* how many of the uframe's 125 usecs are allocated? */
  1902. static unsigned short periodic_usecs(struct oxu_hcd *oxu,
  1903. unsigned frame, unsigned uframe)
  1904. {
  1905. __le32 *hw_p = &oxu->periodic[frame];
  1906. union ehci_shadow *q = &oxu->pshadow[frame];
  1907. unsigned usecs = 0;
  1908. while (q->ptr) {
  1909. switch (Q_NEXT_TYPE(*hw_p)) {
  1910. case Q_TYPE_QH:
  1911. default:
  1912. /* is it in the S-mask? */
  1913. if (q->qh->hw_info2 & cpu_to_le32(1 << uframe))
  1914. usecs += q->qh->usecs;
  1915. /* ... or C-mask? */
  1916. if (q->qh->hw_info2 & cpu_to_le32(1 << (8 + uframe)))
  1917. usecs += q->qh->c_usecs;
  1918. hw_p = &q->qh->hw_next;
  1919. q = &q->qh->qh_next;
  1920. break;
  1921. }
  1922. }
  1923. #ifdef DEBUG
  1924. if (usecs > 100)
  1925. oxu_err(oxu, "uframe %d sched overrun: %d usecs\n",
  1926. frame * 8 + uframe, usecs);
  1927. #endif
  1928. return usecs;
  1929. }
  1930. static int enable_periodic(struct oxu_hcd *oxu)
  1931. {
  1932. u32 cmd;
  1933. int status;
  1934. /* did clearing PSE did take effect yet?
  1935. * takes effect only at frame boundaries...
  1936. */
  1937. status = handshake(oxu, &oxu->regs->status, STS_PSS, 0, 9 * 125);
  1938. if (status != 0) {
  1939. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  1940. usb_hc_died(oxu_to_hcd(oxu));
  1941. return status;
  1942. }
  1943. cmd = readl(&oxu->regs->command) | CMD_PSE;
  1944. writel(cmd, &oxu->regs->command);
  1945. /* posted write ... PSS happens later */
  1946. oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
  1947. /* make sure ehci_work scans these */
  1948. oxu->next_uframe = readl(&oxu->regs->frame_index)
  1949. % (oxu->periodic_size << 3);
  1950. return 0;
  1951. }
  1952. static int disable_periodic(struct oxu_hcd *oxu)
  1953. {
  1954. u32 cmd;
  1955. int status;
  1956. /* did setting PSE not take effect yet?
  1957. * takes effect only at frame boundaries...
  1958. */
  1959. status = handshake(oxu, &oxu->regs->status, STS_PSS, STS_PSS, 9 * 125);
  1960. if (status != 0) {
  1961. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  1962. usb_hc_died(oxu_to_hcd(oxu));
  1963. return status;
  1964. }
  1965. cmd = readl(&oxu->regs->command) & ~CMD_PSE;
  1966. writel(cmd, &oxu->regs->command);
  1967. /* posted write ... */
  1968. oxu->next_uframe = -1;
  1969. return 0;
  1970. }
  1971. /* periodic schedule slots have iso tds (normal or split) first, then a
  1972. * sparse tree for active interrupt transfers.
  1973. *
  1974. * this just links in a qh; caller guarantees uframe masks are set right.
  1975. * no FSTN support (yet; oxu 0.96+)
  1976. */
  1977. static int qh_link_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1978. {
  1979. unsigned i;
  1980. unsigned period = qh->period;
  1981. dev_dbg(&qh->dev->dev,
  1982. "link qh%d-%04x/%p start %d [%d/%d us]\n",
  1983. period, le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
  1984. qh, qh->start, qh->usecs, qh->c_usecs);
  1985. /* high bandwidth, or otherwise every microframe */
  1986. if (period == 0)
  1987. period = 1;
  1988. for (i = qh->start; i < oxu->periodic_size; i += period) {
  1989. union ehci_shadow *prev = &oxu->pshadow[i];
  1990. __le32 *hw_p = &oxu->periodic[i];
  1991. union ehci_shadow here = *prev;
  1992. __le32 type = 0;
  1993. /* skip the iso nodes at list head */
  1994. while (here.ptr) {
  1995. type = Q_NEXT_TYPE(*hw_p);
  1996. if (type == Q_TYPE_QH)
  1997. break;
  1998. prev = periodic_next_shadow(prev, type);
  1999. hw_p = &here.qh->hw_next;
  2000. here = *prev;
  2001. }
  2002. /* sorting each branch by period (slow-->fast)
  2003. * enables sharing interior tree nodes
  2004. */
  2005. while (here.ptr && qh != here.qh) {
  2006. if (qh->period > here.qh->period)
  2007. break;
  2008. prev = &here.qh->qh_next;
  2009. hw_p = &here.qh->hw_next;
  2010. here = *prev;
  2011. }
  2012. /* link in this qh, unless some earlier pass did that */
  2013. if (qh != here.qh) {
  2014. qh->qh_next = here;
  2015. if (here.qh)
  2016. qh->hw_next = *hw_p;
  2017. wmb();
  2018. prev->qh = qh;
  2019. *hw_p = QH_NEXT(qh->qh_dma);
  2020. }
  2021. }
  2022. qh->qh_state = QH_STATE_LINKED;
  2023. qh_get(qh);
  2024. /* update per-qh bandwidth for usbfs */
  2025. oxu_to_hcd(oxu)->self.bandwidth_allocated += qh->period
  2026. ? ((qh->usecs + qh->c_usecs) / qh->period)
  2027. : (qh->usecs * 8);
  2028. /* maybe enable periodic schedule processing */
  2029. if (!oxu->periodic_sched++)
  2030. return enable_periodic(oxu);
  2031. return 0;
  2032. }
  2033. static void qh_unlink_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
  2034. {
  2035. unsigned i;
  2036. unsigned period;
  2037. /* FIXME:
  2038. * IF this isn't high speed
  2039. * and this qh is active in the current uframe
  2040. * (and overlay token SplitXstate is false?)
  2041. * THEN
  2042. * qh->hw_info1 |= cpu_to_le32(1 << 7 "ignore");
  2043. */
  2044. /* high bandwidth, or otherwise part of every microframe */
  2045. period = qh->period;
  2046. if (period == 0)
  2047. period = 1;
  2048. for (i = qh->start; i < oxu->periodic_size; i += period)
  2049. periodic_unlink(oxu, i, qh);
  2050. /* update per-qh bandwidth for usbfs */
  2051. oxu_to_hcd(oxu)->self.bandwidth_allocated -= qh->period
  2052. ? ((qh->usecs + qh->c_usecs) / qh->period)
  2053. : (qh->usecs * 8);
  2054. dev_dbg(&qh->dev->dev,
  2055. "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  2056. qh->period,
  2057. le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
  2058. qh, qh->start, qh->usecs, qh->c_usecs);
  2059. /* qh->qh_next still "live" to HC */
  2060. qh->qh_state = QH_STATE_UNLINK;
  2061. qh->qh_next.ptr = NULL;
  2062. qh_put(qh);
  2063. /* maybe turn off periodic schedule */
  2064. oxu->periodic_sched--;
  2065. if (!oxu->periodic_sched)
  2066. (void) disable_periodic(oxu);
  2067. }
  2068. static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
  2069. {
  2070. unsigned wait;
  2071. qh_unlink_periodic(oxu, qh);
  2072. /* simple/paranoid: always delay, expecting the HC needs to read
  2073. * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
  2074. * expect hub_wq to clean up after any CSPLITs we won't issue.
  2075. * active high speed queues may need bigger delays...
  2076. */
  2077. if (list_empty(&qh->qtd_list)
  2078. || (cpu_to_le32(QH_CMASK) & qh->hw_info2) != 0)
  2079. wait = 2;
  2080. else
  2081. wait = 55; /* worst case: 3 * 1024 */
  2082. udelay(wait);
  2083. qh->qh_state = QH_STATE_IDLE;
  2084. qh->hw_next = EHCI_LIST_END;
  2085. wmb();
  2086. }
  2087. static int check_period(struct oxu_hcd *oxu,
  2088. unsigned frame, unsigned uframe,
  2089. unsigned period, unsigned usecs)
  2090. {
  2091. int claimed;
  2092. /* complete split running into next frame?
  2093. * given FSTN support, we could sometimes check...
  2094. */
  2095. if (uframe >= 8)
  2096. return 0;
  2097. /*
  2098. * 80% periodic == 100 usec/uframe available
  2099. * convert "usecs we need" to "max already claimed"
  2100. */
  2101. usecs = 100 - usecs;
  2102. /* we "know" 2 and 4 uframe intervals were rejected; so
  2103. * for period 0, check _every_ microframe in the schedule.
  2104. */
  2105. if (unlikely(period == 0)) {
  2106. do {
  2107. for (uframe = 0; uframe < 7; uframe++) {
  2108. claimed = periodic_usecs(oxu, frame, uframe);
  2109. if (claimed > usecs)
  2110. return 0;
  2111. }
  2112. } while ((frame += 1) < oxu->periodic_size);
  2113. /* just check the specified uframe, at that period */
  2114. } else {
  2115. do {
  2116. claimed = periodic_usecs(oxu, frame, uframe);
  2117. if (claimed > usecs)
  2118. return 0;
  2119. } while ((frame += period) < oxu->periodic_size);
  2120. }
  2121. return 1;
  2122. }
  2123. static int check_intr_schedule(struct oxu_hcd *oxu,
  2124. unsigned frame, unsigned uframe,
  2125. const struct ehci_qh *qh, __le32 *c_maskp)
  2126. {
  2127. int retval = -ENOSPC;
  2128. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  2129. goto done;
  2130. if (!check_period(oxu, frame, uframe, qh->period, qh->usecs))
  2131. goto done;
  2132. if (!qh->c_usecs) {
  2133. retval = 0;
  2134. *c_maskp = 0;
  2135. goto done;
  2136. }
  2137. done:
  2138. return retval;
  2139. }
  2140. /* "first fit" scheduling policy used the first time through,
  2141. * or when the previous schedule slot can't be re-used.
  2142. */
  2143. static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
  2144. {
  2145. int status;
  2146. unsigned uframe;
  2147. __le32 c_mask;
  2148. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  2149. qh_refresh(oxu, qh);
  2150. qh->hw_next = EHCI_LIST_END;
  2151. frame = qh->start;
  2152. /* reuse the previous schedule slots, if we can */
  2153. if (frame < qh->period) {
  2154. uframe = ffs(le32_to_cpup(&qh->hw_info2) & QH_SMASK);
  2155. status = check_intr_schedule(oxu, frame, --uframe,
  2156. qh, &c_mask);
  2157. } else {
  2158. uframe = 0;
  2159. c_mask = 0;
  2160. status = -ENOSPC;
  2161. }
  2162. /* else scan the schedule to find a group of slots such that all
  2163. * uframes have enough periodic bandwidth available.
  2164. */
  2165. if (status) {
  2166. /* "normal" case, uframing flexible except with splits */
  2167. if (qh->period) {
  2168. frame = qh->period - 1;
  2169. do {
  2170. for (uframe = 0; uframe < 8; uframe++) {
  2171. status = check_intr_schedule(oxu,
  2172. frame, uframe, qh,
  2173. &c_mask);
  2174. if (status == 0)
  2175. break;
  2176. }
  2177. } while (status && frame--);
  2178. /* qh->period == 0 means every uframe */
  2179. } else {
  2180. frame = 0;
  2181. status = check_intr_schedule(oxu, 0, 0, qh, &c_mask);
  2182. }
  2183. if (status)
  2184. goto done;
  2185. qh->start = frame;
  2186. /* reset S-frame and (maybe) C-frame masks */
  2187. qh->hw_info2 &= cpu_to_le32(~(QH_CMASK | QH_SMASK));
  2188. qh->hw_info2 |= qh->period
  2189. ? cpu_to_le32(1 << uframe)
  2190. : cpu_to_le32(QH_SMASK);
  2191. qh->hw_info2 |= c_mask;
  2192. } else
  2193. oxu_dbg(oxu, "reused qh %p schedule\n", qh);
  2194. /* stuff into the periodic schedule */
  2195. status = qh_link_periodic(oxu, qh);
  2196. done:
  2197. return status;
  2198. }
  2199. static int intr_submit(struct oxu_hcd *oxu, struct urb *urb,
  2200. struct list_head *qtd_list, gfp_t mem_flags)
  2201. {
  2202. unsigned epnum;
  2203. unsigned long flags;
  2204. struct ehci_qh *qh;
  2205. int status = 0;
  2206. struct list_head empty;
  2207. /* get endpoint and transfer/schedule data */
  2208. epnum = urb->ep->desc.bEndpointAddress;
  2209. spin_lock_irqsave(&oxu->lock, flags);
  2210. if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
  2211. status = -ESHUTDOWN;
  2212. goto done;
  2213. }
  2214. /* get qh and force any scheduling errors */
  2215. INIT_LIST_HEAD(&empty);
  2216. qh = qh_append_tds(oxu, urb, &empty, epnum, &urb->ep->hcpriv);
  2217. if (qh == NULL) {
  2218. status = -ENOMEM;
  2219. goto done;
  2220. }
  2221. if (qh->qh_state == QH_STATE_IDLE) {
  2222. status = qh_schedule(oxu, qh);
  2223. if (status != 0)
  2224. goto done;
  2225. }
  2226. /* then queue the urb's tds to the qh */
  2227. qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
  2228. BUG_ON(qh == NULL);
  2229. /* ... update usbfs periodic stats */
  2230. oxu_to_hcd(oxu)->self.bandwidth_int_reqs++;
  2231. done:
  2232. spin_unlock_irqrestore(&oxu->lock, flags);
  2233. if (status)
  2234. qtd_list_free(oxu, urb, qtd_list);
  2235. return status;
  2236. }
  2237. static inline int itd_submit(struct oxu_hcd *oxu, struct urb *urb,
  2238. gfp_t mem_flags)
  2239. {
  2240. oxu_dbg(oxu, "iso support is missing!\n");
  2241. return -ENOSYS;
  2242. }
  2243. static inline int sitd_submit(struct oxu_hcd *oxu, struct urb *urb,
  2244. gfp_t mem_flags)
  2245. {
  2246. oxu_dbg(oxu, "split iso support is missing!\n");
  2247. return -ENOSYS;
  2248. }
  2249. static void scan_periodic(struct oxu_hcd *oxu)
  2250. {
  2251. unsigned frame, clock, now_uframe, mod;
  2252. unsigned modified;
  2253. mod = oxu->periodic_size << 3;
  2254. /*
  2255. * When running, scan from last scan point up to "now"
  2256. * else clean up by scanning everything that's left.
  2257. * Touches as few pages as possible: cache-friendly.
  2258. */
  2259. now_uframe = oxu->next_uframe;
  2260. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  2261. clock = readl(&oxu->regs->frame_index);
  2262. else
  2263. clock = now_uframe + mod - 1;
  2264. clock %= mod;
  2265. for (;;) {
  2266. union ehci_shadow q, *q_p;
  2267. __le32 type, *hw_p;
  2268. /* don't scan past the live uframe */
  2269. frame = now_uframe >> 3;
  2270. if (frame != (clock >> 3)) {
  2271. /* safe to scan the whole frame at once */
  2272. now_uframe |= 0x07;
  2273. }
  2274. restart:
  2275. /* scan each element in frame's queue for completions */
  2276. q_p = &oxu->pshadow[frame];
  2277. hw_p = &oxu->periodic[frame];
  2278. q.ptr = q_p->ptr;
  2279. type = Q_NEXT_TYPE(*hw_p);
  2280. modified = 0;
  2281. while (q.ptr != NULL) {
  2282. union ehci_shadow temp;
  2283. switch (type) {
  2284. case Q_TYPE_QH:
  2285. /* handle any completions */
  2286. temp.qh = qh_get(q.qh);
  2287. type = Q_NEXT_TYPE(q.qh->hw_next);
  2288. q = q.qh->qh_next;
  2289. modified = qh_completions(oxu, temp.qh);
  2290. if (unlikely(list_empty(&temp.qh->qtd_list)))
  2291. intr_deschedule(oxu, temp.qh);
  2292. qh_put(temp.qh);
  2293. break;
  2294. default:
  2295. oxu_dbg(oxu, "corrupt type %d frame %d shadow %p\n",
  2296. type, frame, q.ptr);
  2297. q.ptr = NULL;
  2298. }
  2299. /* assume completion callbacks modify the queue */
  2300. if (unlikely(modified))
  2301. goto restart;
  2302. }
  2303. /* Stop when we catch up to the HC */
  2304. /* FIXME: this assumes we won't get lapped when
  2305. * latencies climb; that should be rare, but...
  2306. * detect it, and just go all the way around.
  2307. * FLR might help detect this case, so long as latencies
  2308. * don't exceed periodic_size msec (default 1.024 sec).
  2309. */
  2310. /* FIXME: likewise assumes HC doesn't halt mid-scan */
  2311. if (now_uframe == clock) {
  2312. unsigned now;
  2313. if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  2314. break;
  2315. oxu->next_uframe = now_uframe;
  2316. now = readl(&oxu->regs->frame_index) % mod;
  2317. if (now_uframe == now)
  2318. break;
  2319. /* rescan the rest of this frame, then ... */
  2320. clock = now;
  2321. } else {
  2322. now_uframe++;
  2323. now_uframe %= mod;
  2324. }
  2325. }
  2326. }
  2327. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  2328. * The firmware seems to think that powering off is a wakeup event!
  2329. * This routine turns off remote wakeup and everything else, on all ports.
  2330. */
  2331. static void ehci_turn_off_all_ports(struct oxu_hcd *oxu)
  2332. {
  2333. int port = HCS_N_PORTS(oxu->hcs_params);
  2334. while (port--)
  2335. writel(PORT_RWC_BITS, &oxu->regs->port_status[port]);
  2336. }
  2337. static void ehci_port_power(struct oxu_hcd *oxu, int is_on)
  2338. {
  2339. unsigned port;
  2340. if (!HCS_PPC(oxu->hcs_params))
  2341. return;
  2342. oxu_dbg(oxu, "...power%s ports...\n", is_on ? "up" : "down");
  2343. for (port = HCS_N_PORTS(oxu->hcs_params); port > 0; ) {
  2344. if (is_on)
  2345. oxu_hub_control(oxu_to_hcd(oxu), SetPortFeature,
  2346. USB_PORT_FEAT_POWER, port--, NULL, 0);
  2347. else
  2348. oxu_hub_control(oxu_to_hcd(oxu), ClearPortFeature,
  2349. USB_PORT_FEAT_POWER, port--, NULL, 0);
  2350. }
  2351. msleep(20);
  2352. }
  2353. /* Called from some interrupts, timers, and so on.
  2354. * It calls driver completion functions, after dropping oxu->lock.
  2355. */
  2356. static void ehci_work(struct oxu_hcd *oxu)
  2357. {
  2358. timer_action_done(oxu, TIMER_IO_WATCHDOG);
  2359. if (oxu->reclaim_ready)
  2360. end_unlink_async(oxu);
  2361. /* another CPU may drop oxu->lock during a schedule scan while
  2362. * it reports urb completions. this flag guards against bogus
  2363. * attempts at re-entrant schedule scanning.
  2364. */
  2365. if (oxu->scanning)
  2366. return;
  2367. oxu->scanning = 1;
  2368. scan_async(oxu);
  2369. if (oxu->next_uframe != -1)
  2370. scan_periodic(oxu);
  2371. oxu->scanning = 0;
  2372. /* the IO watchdog guards against hardware or driver bugs that
  2373. * misplace IRQs, and should let us run completely without IRQs.
  2374. * such lossage has been observed on both VT6202 and VT8235.
  2375. */
  2376. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) &&
  2377. (oxu->async->qh_next.ptr != NULL ||
  2378. oxu->periodic_sched != 0))
  2379. timer_action(oxu, TIMER_IO_WATCHDOG);
  2380. }
  2381. static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  2382. {
  2383. /* if we need to use IAA and it's busy, defer */
  2384. if (qh->qh_state == QH_STATE_LINKED
  2385. && oxu->reclaim
  2386. && HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) {
  2387. struct ehci_qh *last;
  2388. for (last = oxu->reclaim;
  2389. last->reclaim;
  2390. last = last->reclaim)
  2391. continue;
  2392. qh->qh_state = QH_STATE_UNLINK_WAIT;
  2393. last->reclaim = qh;
  2394. /* bypass IAA if the hc can't care */
  2395. } else if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && oxu->reclaim)
  2396. end_unlink_async(oxu);
  2397. /* something else might have unlinked the qh by now */
  2398. if (qh->qh_state == QH_STATE_LINKED)
  2399. start_unlink_async(oxu, qh);
  2400. }
  2401. /*
  2402. * USB host controller methods
  2403. */
  2404. static irqreturn_t oxu210_hcd_irq(struct usb_hcd *hcd)
  2405. {
  2406. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2407. u32 status, pcd_status = 0;
  2408. int bh;
  2409. spin_lock(&oxu->lock);
  2410. status = readl(&oxu->regs->status);
  2411. /* e.g. cardbus physical eject */
  2412. if (status == ~(u32) 0) {
  2413. oxu_dbg(oxu, "device removed\n");
  2414. goto dead;
  2415. }
  2416. /* Shared IRQ? */
  2417. status &= INTR_MASK;
  2418. if (!status || unlikely(hcd->state == HC_STATE_HALT)) {
  2419. spin_unlock(&oxu->lock);
  2420. return IRQ_NONE;
  2421. }
  2422. /* clear (just) interrupts */
  2423. writel(status, &oxu->regs->status);
  2424. readl(&oxu->regs->command); /* unblock posted write */
  2425. bh = 0;
  2426. #ifdef OXU_VERBOSE_DEBUG
  2427. /* unrequested/ignored: Frame List Rollover */
  2428. dbg_status(oxu, "irq", status);
  2429. #endif
  2430. /* INT, ERR, and IAA interrupt rates can be throttled */
  2431. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  2432. if (likely((status & (STS_INT|STS_ERR)) != 0))
  2433. bh = 1;
  2434. /* complete the unlinking of some qh [4.15.2.3] */
  2435. if (status & STS_IAA) {
  2436. oxu->reclaim_ready = 1;
  2437. bh = 1;
  2438. }
  2439. /* remote wakeup [4.3.1] */
  2440. if (status & STS_PCD) {
  2441. unsigned i = HCS_N_PORTS(oxu->hcs_params);
  2442. pcd_status = status;
  2443. /* resume root hub? */
  2444. if (!(readl(&oxu->regs->command) & CMD_RUN))
  2445. usb_hcd_resume_root_hub(hcd);
  2446. while (i--) {
  2447. int pstatus = readl(&oxu->regs->port_status[i]);
  2448. if (pstatus & PORT_OWNER)
  2449. continue;
  2450. if (!(pstatus & PORT_RESUME)
  2451. || oxu->reset_done[i] != 0)
  2452. continue;
  2453. /* start USB_RESUME_TIMEOUT resume signaling from this
  2454. * port, and make hub_wq collect PORT_STAT_C_SUSPEND to
  2455. * stop that signaling.
  2456. */
  2457. oxu->reset_done[i] = jiffies +
  2458. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  2459. oxu_dbg(oxu, "port %d remote wakeup\n", i + 1);
  2460. mod_timer(&hcd->rh_timer, oxu->reset_done[i]);
  2461. }
  2462. }
  2463. /* PCI errors [4.15.2.4] */
  2464. if (unlikely((status & STS_FATAL) != 0)) {
  2465. /* bogus "fatal" IRQs appear on some chips... why? */
  2466. status = readl(&oxu->regs->status);
  2467. dbg_cmd(oxu, "fatal", readl(&oxu->regs->command));
  2468. dbg_status(oxu, "fatal", status);
  2469. if (status & STS_HALT) {
  2470. oxu_err(oxu, "fatal error\n");
  2471. dead:
  2472. ehci_reset(oxu);
  2473. writel(0, &oxu->regs->configured_flag);
  2474. usb_hc_died(hcd);
  2475. /* generic layer kills/unlinks all urbs, then
  2476. * uses oxu_stop to clean up the rest
  2477. */
  2478. bh = 1;
  2479. }
  2480. }
  2481. if (bh)
  2482. ehci_work(oxu);
  2483. spin_unlock(&oxu->lock);
  2484. if (pcd_status & STS_PCD)
  2485. usb_hcd_poll_rh_status(hcd);
  2486. return IRQ_HANDLED;
  2487. }
  2488. static irqreturn_t oxu_irq(struct usb_hcd *hcd)
  2489. {
  2490. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2491. int ret = IRQ_HANDLED;
  2492. u32 status = oxu_readl(hcd->regs, OXU_CHIPIRQSTATUS);
  2493. u32 enable = oxu_readl(hcd->regs, OXU_CHIPIRQEN_SET);
  2494. /* Disable all interrupt */
  2495. oxu_writel(hcd->regs, OXU_CHIPIRQEN_CLR, enable);
  2496. if ((oxu->is_otg && (status & OXU_USBOTGI)) ||
  2497. (!oxu->is_otg && (status & OXU_USBSPHI)))
  2498. oxu210_hcd_irq(hcd);
  2499. else
  2500. ret = IRQ_NONE;
  2501. /* Enable all interrupt back */
  2502. oxu_writel(hcd->regs, OXU_CHIPIRQEN_SET, enable);
  2503. return ret;
  2504. }
  2505. static void oxu_watchdog(struct timer_list *t)
  2506. {
  2507. struct oxu_hcd *oxu = from_timer(oxu, t, watchdog);
  2508. unsigned long flags;
  2509. spin_lock_irqsave(&oxu->lock, flags);
  2510. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  2511. if (oxu->reclaim) {
  2512. u32 status = readl(&oxu->regs->status);
  2513. if (status & STS_IAA) {
  2514. oxu_vdbg(oxu, "lost IAA\n");
  2515. writel(STS_IAA, &oxu->regs->status);
  2516. oxu->reclaim_ready = 1;
  2517. }
  2518. }
  2519. /* stop async processing after it's idled a bit */
  2520. if (test_bit(TIMER_ASYNC_OFF, &oxu->actions))
  2521. start_unlink_async(oxu, oxu->async);
  2522. /* oxu could run by timer, without IRQs ... */
  2523. ehci_work(oxu);
  2524. spin_unlock_irqrestore(&oxu->lock, flags);
  2525. }
  2526. /* One-time init, only for memory state.
  2527. */
  2528. static int oxu_hcd_init(struct usb_hcd *hcd)
  2529. {
  2530. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2531. u32 temp;
  2532. int retval;
  2533. u32 hcc_params;
  2534. spin_lock_init(&oxu->lock);
  2535. timer_setup(&oxu->watchdog, oxu_watchdog, 0);
  2536. /*
  2537. * hw default: 1K periodic list heads, one per frame.
  2538. * periodic_size can shrink by USBCMD update if hcc_params allows.
  2539. */
  2540. oxu->periodic_size = DEFAULT_I_TDPS;
  2541. retval = ehci_mem_init(oxu, GFP_KERNEL);
  2542. if (retval < 0)
  2543. return retval;
  2544. /* controllers may cache some of the periodic schedule ... */
  2545. hcc_params = readl(&oxu->caps->hcc_params);
  2546. if (HCC_ISOC_CACHE(hcc_params)) /* full frame cache */
  2547. oxu->i_thresh = 8;
  2548. else /* N microframes cached */
  2549. oxu->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  2550. oxu->reclaim = NULL;
  2551. oxu->reclaim_ready = 0;
  2552. oxu->next_uframe = -1;
  2553. /*
  2554. * dedicate a qh for the async ring head, since we couldn't unlink
  2555. * a 'real' qh without stopping the async schedule [4.8]. use it
  2556. * as the 'reclamation list head' too.
  2557. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  2558. * from automatically advancing to the next td after short reads.
  2559. */
  2560. oxu->async->qh_next.qh = NULL;
  2561. oxu->async->hw_next = QH_NEXT(oxu->async->qh_dma);
  2562. oxu->async->hw_info1 = cpu_to_le32(QH_HEAD);
  2563. oxu->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  2564. oxu->async->hw_qtd_next = EHCI_LIST_END;
  2565. oxu->async->qh_state = QH_STATE_LINKED;
  2566. oxu->async->hw_alt_next = QTD_NEXT(oxu->async->dummy->qtd_dma);
  2567. /* clear interrupt enables, set irq latency */
  2568. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  2569. log2_irq_thresh = 0;
  2570. temp = 1 << (16 + log2_irq_thresh);
  2571. if (HCC_CANPARK(hcc_params)) {
  2572. /* HW default park == 3, on hardware that supports it (like
  2573. * NVidia and ALI silicon), maximizes throughput on the async
  2574. * schedule by avoiding QH fetches between transfers.
  2575. *
  2576. * With fast usb storage devices and NForce2, "park" seems to
  2577. * make problems: throughput reduction (!), data errors...
  2578. */
  2579. if (park) {
  2580. park = min(park, (unsigned) 3);
  2581. temp |= CMD_PARK;
  2582. temp |= park << 8;
  2583. }
  2584. oxu_dbg(oxu, "park %d\n", park);
  2585. }
  2586. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  2587. /* periodic schedule size can be smaller than default */
  2588. temp &= ~(3 << 2);
  2589. temp |= (EHCI_TUNE_FLS << 2);
  2590. }
  2591. oxu->command = temp;
  2592. return 0;
  2593. }
  2594. /* Called during probe() after chip reset completes.
  2595. */
  2596. static int oxu_reset(struct usb_hcd *hcd)
  2597. {
  2598. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2599. spin_lock_init(&oxu->mem_lock);
  2600. INIT_LIST_HEAD(&oxu->urb_list);
  2601. oxu->urb_len = 0;
  2602. if (oxu->is_otg) {
  2603. oxu->caps = hcd->regs + OXU_OTG_CAP_OFFSET;
  2604. oxu->regs = hcd->regs + OXU_OTG_CAP_OFFSET + \
  2605. HC_LENGTH(readl(&oxu->caps->hc_capbase));
  2606. oxu->mem = hcd->regs + OXU_SPH_MEM;
  2607. } else {
  2608. oxu->caps = hcd->regs + OXU_SPH_CAP_OFFSET;
  2609. oxu->regs = hcd->regs + OXU_SPH_CAP_OFFSET + \
  2610. HC_LENGTH(readl(&oxu->caps->hc_capbase));
  2611. oxu->mem = hcd->regs + OXU_OTG_MEM;
  2612. }
  2613. oxu->hcs_params = readl(&oxu->caps->hcs_params);
  2614. oxu->sbrn = 0x20;
  2615. return oxu_hcd_init(hcd);
  2616. }
  2617. static int oxu_run(struct usb_hcd *hcd)
  2618. {
  2619. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2620. int retval;
  2621. u32 temp, hcc_params;
  2622. hcd->uses_new_polling = 1;
  2623. /* EHCI spec section 4.1 */
  2624. retval = ehci_reset(oxu);
  2625. if (retval != 0) {
  2626. ehci_mem_cleanup(oxu);
  2627. return retval;
  2628. }
  2629. writel(oxu->periodic_dma, &oxu->regs->frame_list);
  2630. writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
  2631. /* hcc_params controls whether oxu->regs->segment must (!!!)
  2632. * be used; it constrains QH/ITD/SITD and QTD locations.
  2633. * dma_pool consistent memory always uses segment zero.
  2634. * streaming mappings for I/O buffers, like dma_map_single(),
  2635. * can return segments above 4GB, if the device allows.
  2636. *
  2637. * NOTE: the dma mask is visible through dev->dma_mask, so
  2638. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  2639. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  2640. * host side drivers though.
  2641. */
  2642. hcc_params = readl(&oxu->caps->hcc_params);
  2643. if (HCC_64BIT_ADDR(hcc_params))
  2644. writel(0, &oxu->regs->segment);
  2645. oxu->command &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE |
  2646. CMD_ASE | CMD_RESET);
  2647. oxu->command |= CMD_RUN;
  2648. writel(oxu->command, &oxu->regs->command);
  2649. dbg_cmd(oxu, "init", oxu->command);
  2650. /*
  2651. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  2652. * are explicitly handed to companion controller(s), so no TT is
  2653. * involved with the root hub. (Except where one is integrated,
  2654. * and there's no companion controller unless maybe for USB OTG.)
  2655. */
  2656. hcd->state = HC_STATE_RUNNING;
  2657. writel(FLAG_CF, &oxu->regs->configured_flag);
  2658. readl(&oxu->regs->command); /* unblock posted writes */
  2659. temp = HC_VERSION(readl(&oxu->caps->hc_capbase));
  2660. oxu_info(oxu, "USB %x.%x started, quasi-EHCI %x.%02x, driver %s%s\n",
  2661. ((oxu->sbrn & 0xf0)>>4), (oxu->sbrn & 0x0f),
  2662. temp >> 8, temp & 0xff, DRIVER_VERSION,
  2663. ignore_oc ? ", overcurrent ignored" : "");
  2664. writel(INTR_MASK, &oxu->regs->intr_enable); /* Turn On Interrupts */
  2665. return 0;
  2666. }
  2667. static void oxu_stop(struct usb_hcd *hcd)
  2668. {
  2669. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2670. /* Turn off port power on all root hub ports. */
  2671. ehci_port_power(oxu, 0);
  2672. /* no more interrupts ... */
  2673. del_timer_sync(&oxu->watchdog);
  2674. spin_lock_irq(&oxu->lock);
  2675. if (HC_IS_RUNNING(hcd->state))
  2676. ehci_quiesce(oxu);
  2677. ehci_reset(oxu);
  2678. writel(0, &oxu->regs->intr_enable);
  2679. spin_unlock_irq(&oxu->lock);
  2680. /* let companion controllers work when we aren't */
  2681. writel(0, &oxu->regs->configured_flag);
  2682. /* root hub is shut down separately (first, when possible) */
  2683. spin_lock_irq(&oxu->lock);
  2684. if (oxu->async)
  2685. ehci_work(oxu);
  2686. spin_unlock_irq(&oxu->lock);
  2687. ehci_mem_cleanup(oxu);
  2688. dbg_status(oxu, "oxu_stop completed", readl(&oxu->regs->status));
  2689. }
  2690. /* Kick in for silicon on any bus (not just pci, etc).
  2691. * This forcibly disables dma and IRQs, helping kexec and other cases
  2692. * where the next system software may expect clean state.
  2693. */
  2694. static void oxu_shutdown(struct usb_hcd *hcd)
  2695. {
  2696. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2697. (void) ehci_halt(oxu);
  2698. ehci_turn_off_all_ports(oxu);
  2699. /* make BIOS/etc use companion controller during reboot */
  2700. writel(0, &oxu->regs->configured_flag);
  2701. /* unblock posted writes */
  2702. readl(&oxu->regs->configured_flag);
  2703. }
  2704. /* Non-error returns are a promise to giveback() the urb later
  2705. * we drop ownership so next owner (or urb unlink) can get it
  2706. *
  2707. * urb + dev is in hcd.self.controller.urb_list
  2708. * we're queueing TDs onto software and hardware lists
  2709. *
  2710. * hcd-specific init for hcpriv hasn't been done yet
  2711. *
  2712. * NOTE: control, bulk, and interrupt share the same code to append TDs
  2713. * to a (possibly active) QH, and the same QH scanning code.
  2714. */
  2715. static int __oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2716. gfp_t mem_flags)
  2717. {
  2718. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2719. struct list_head qtd_list;
  2720. INIT_LIST_HEAD(&qtd_list);
  2721. switch (usb_pipetype(urb->pipe)) {
  2722. case PIPE_CONTROL:
  2723. case PIPE_BULK:
  2724. default:
  2725. if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
  2726. return -ENOMEM;
  2727. return submit_async(oxu, urb, &qtd_list, mem_flags);
  2728. case PIPE_INTERRUPT:
  2729. if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
  2730. return -ENOMEM;
  2731. return intr_submit(oxu, urb, &qtd_list, mem_flags);
  2732. case PIPE_ISOCHRONOUS:
  2733. if (urb->dev->speed == USB_SPEED_HIGH)
  2734. return itd_submit(oxu, urb, mem_flags);
  2735. else
  2736. return sitd_submit(oxu, urb, mem_flags);
  2737. }
  2738. }
  2739. /* This function is responsible for breaking URBs with big data size
  2740. * into smaller size and processing small urbs in sequence.
  2741. */
  2742. static int oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2743. gfp_t mem_flags)
  2744. {
  2745. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2746. int num, rem;
  2747. void *transfer_buffer;
  2748. struct urb *murb;
  2749. int i, ret;
  2750. /* If not bulk pipe just enqueue the URB */
  2751. if (!usb_pipebulk(urb->pipe))
  2752. return __oxu_urb_enqueue(hcd, urb, mem_flags);
  2753. /* Otherwise we should verify the USB transfer buffer size! */
  2754. transfer_buffer = urb->transfer_buffer;
  2755. num = urb->transfer_buffer_length / 4096;
  2756. rem = urb->transfer_buffer_length % 4096;
  2757. if (rem != 0)
  2758. num++;
  2759. /* If URB is smaller than 4096 bytes just enqueue it! */
  2760. if (num == 1)
  2761. return __oxu_urb_enqueue(hcd, urb, mem_flags);
  2762. /* Ok, we have more job to do! :) */
  2763. for (i = 0; i < num - 1; i++) {
  2764. /* Get free micro URB poll till a free urb is received */
  2765. do {
  2766. murb = (struct urb *) oxu_murb_alloc(oxu);
  2767. if (!murb)
  2768. schedule();
  2769. } while (!murb);
  2770. /* Coping the urb */
  2771. memcpy(murb, urb, sizeof(struct urb));
  2772. murb->transfer_buffer_length = 4096;
  2773. murb->transfer_buffer = transfer_buffer + i * 4096;
  2774. /* Null pointer for the encodes that this is a micro urb */
  2775. murb->complete = NULL;
  2776. ((struct oxu_murb *) murb)->main = urb;
  2777. ((struct oxu_murb *) murb)->last = 0;
  2778. /* This loop is to guarantee urb to be processed when there's
  2779. * not enough resources at a particular time by retrying.
  2780. */
  2781. do {
  2782. ret = __oxu_urb_enqueue(hcd, murb, mem_flags);
  2783. if (ret)
  2784. schedule();
  2785. } while (ret);
  2786. }
  2787. /* Last urb requires special handling */
  2788. /* Get free micro URB poll till a free urb is received */
  2789. do {
  2790. murb = (struct urb *) oxu_murb_alloc(oxu);
  2791. if (!murb)
  2792. schedule();
  2793. } while (!murb);
  2794. /* Coping the urb */
  2795. memcpy(murb, urb, sizeof(struct urb));
  2796. murb->transfer_buffer_length = rem > 0 ? rem : 4096;
  2797. murb->transfer_buffer = transfer_buffer + (num - 1) * 4096;
  2798. /* Null pointer for the encodes that this is a micro urb */
  2799. murb->complete = NULL;
  2800. ((struct oxu_murb *) murb)->main = urb;
  2801. ((struct oxu_murb *) murb)->last = 1;
  2802. do {
  2803. ret = __oxu_urb_enqueue(hcd, murb, mem_flags);
  2804. if (ret)
  2805. schedule();
  2806. } while (ret);
  2807. return ret;
  2808. }
  2809. /* Remove from hardware lists.
  2810. * Completions normally happen asynchronously
  2811. */
  2812. static int oxu_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2813. {
  2814. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2815. struct ehci_qh *qh;
  2816. unsigned long flags;
  2817. spin_lock_irqsave(&oxu->lock, flags);
  2818. switch (usb_pipetype(urb->pipe)) {
  2819. case PIPE_CONTROL:
  2820. case PIPE_BULK:
  2821. default:
  2822. qh = (struct ehci_qh *) urb->hcpriv;
  2823. if (!qh)
  2824. break;
  2825. unlink_async(oxu, qh);
  2826. break;
  2827. case PIPE_INTERRUPT:
  2828. qh = (struct ehci_qh *) urb->hcpriv;
  2829. if (!qh)
  2830. break;
  2831. switch (qh->qh_state) {
  2832. case QH_STATE_LINKED:
  2833. intr_deschedule(oxu, qh);
  2834. fallthrough;
  2835. case QH_STATE_IDLE:
  2836. qh_completions(oxu, qh);
  2837. break;
  2838. default:
  2839. oxu_dbg(oxu, "bogus qh %p state %d\n",
  2840. qh, qh->qh_state);
  2841. goto done;
  2842. }
  2843. /* reschedule QH iff another request is queued */
  2844. if (!list_empty(&qh->qtd_list)
  2845. && HC_IS_RUNNING(hcd->state)) {
  2846. int status;
  2847. status = qh_schedule(oxu, qh);
  2848. spin_unlock_irqrestore(&oxu->lock, flags);
  2849. if (status != 0) {
  2850. /* shouldn't happen often, but ...
  2851. * FIXME kill those tds' urbs
  2852. */
  2853. dev_err(hcd->self.controller,
  2854. "can't reschedule qh %p, err %d\n", qh,
  2855. status);
  2856. }
  2857. return status;
  2858. }
  2859. break;
  2860. }
  2861. done:
  2862. spin_unlock_irqrestore(&oxu->lock, flags);
  2863. return 0;
  2864. }
  2865. /* Bulk qh holds the data toggle */
  2866. static void oxu_endpoint_disable(struct usb_hcd *hcd,
  2867. struct usb_host_endpoint *ep)
  2868. {
  2869. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2870. unsigned long flags;
  2871. struct ehci_qh *qh, *tmp;
  2872. /* ASSERT: any requests/urbs are being unlinked */
  2873. /* ASSERT: nobody can be submitting urbs for this any more */
  2874. rescan:
  2875. spin_lock_irqsave(&oxu->lock, flags);
  2876. qh = ep->hcpriv;
  2877. if (!qh)
  2878. goto done;
  2879. /* endpoints can be iso streams. for now, we don't
  2880. * accelerate iso completions ... so spin a while.
  2881. */
  2882. if (qh->hw_info1 == 0) {
  2883. oxu_vdbg(oxu, "iso delay\n");
  2884. goto idle_timeout;
  2885. }
  2886. if (!HC_IS_RUNNING(hcd->state))
  2887. qh->qh_state = QH_STATE_IDLE;
  2888. switch (qh->qh_state) {
  2889. case QH_STATE_LINKED:
  2890. for (tmp = oxu->async->qh_next.qh;
  2891. tmp && tmp != qh;
  2892. tmp = tmp->qh_next.qh)
  2893. continue;
  2894. /* periodic qh self-unlinks on empty */
  2895. if (!tmp)
  2896. goto nogood;
  2897. unlink_async(oxu, qh);
  2898. fallthrough;
  2899. case QH_STATE_UNLINK: /* wait for hw to finish? */
  2900. idle_timeout:
  2901. spin_unlock_irqrestore(&oxu->lock, flags);
  2902. schedule_timeout_uninterruptible(1);
  2903. goto rescan;
  2904. case QH_STATE_IDLE: /* fully unlinked */
  2905. if (list_empty(&qh->qtd_list)) {
  2906. qh_put(qh);
  2907. break;
  2908. }
  2909. fallthrough;
  2910. default:
  2911. nogood:
  2912. /* caller was supposed to have unlinked any requests;
  2913. * that's not our job. just leak this memory.
  2914. */
  2915. oxu_err(oxu, "qh %p (#%02x) state %d%s\n",
  2916. qh, ep->desc.bEndpointAddress, qh->qh_state,
  2917. list_empty(&qh->qtd_list) ? "" : "(has tds)");
  2918. break;
  2919. }
  2920. ep->hcpriv = NULL;
  2921. done:
  2922. spin_unlock_irqrestore(&oxu->lock, flags);
  2923. }
  2924. static int oxu_get_frame(struct usb_hcd *hcd)
  2925. {
  2926. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2927. return (readl(&oxu->regs->frame_index) >> 3) %
  2928. oxu->periodic_size;
  2929. }
  2930. /* Build "status change" packet (one or two bytes) from HC registers */
  2931. static int oxu_hub_status_data(struct usb_hcd *hcd, char *buf)
  2932. {
  2933. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2934. u32 temp, mask, status = 0;
  2935. int ports, i, retval = 1;
  2936. unsigned long flags;
  2937. /* if !PM, root hub timers won't get shut down ... */
  2938. if (!HC_IS_RUNNING(hcd->state))
  2939. return 0;
  2940. /* init status to no-changes */
  2941. buf[0] = 0;
  2942. ports = HCS_N_PORTS(oxu->hcs_params);
  2943. if (ports > 7) {
  2944. buf[1] = 0;
  2945. retval++;
  2946. }
  2947. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  2948. * causing massive log spam unless we completely ignore them. It
  2949. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  2950. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  2951. * PORT_POWER; that's surprising, but maybe within-spec.
  2952. */
  2953. if (!ignore_oc)
  2954. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  2955. else
  2956. mask = PORT_CSC | PORT_PEC;
  2957. /* no hub change reports (bit 0) for now (power, ...) */
  2958. /* port N changes (bit N)? */
  2959. spin_lock_irqsave(&oxu->lock, flags);
  2960. for (i = 0; i < ports; i++) {
  2961. temp = readl(&oxu->regs->port_status[i]);
  2962. /*
  2963. * Return status information even for ports with OWNER set.
  2964. * Otherwise hub_wq wouldn't see the disconnect event when a
  2965. * high-speed device is switched over to the companion
  2966. * controller by the user.
  2967. */
  2968. if (!(temp & PORT_CONNECT))
  2969. oxu->reset_done[i] = 0;
  2970. if ((temp & mask) != 0 || ((temp & PORT_RESUME) != 0 &&
  2971. time_after_eq(jiffies, oxu->reset_done[i]))) {
  2972. if (i < 7)
  2973. buf[0] |= 1 << (i + 1);
  2974. else
  2975. buf[1] |= 1 << (i - 7);
  2976. status = STS_PCD;
  2977. }
  2978. }
  2979. /* FIXME autosuspend idle root hubs */
  2980. spin_unlock_irqrestore(&oxu->lock, flags);
  2981. return status ? retval : 0;
  2982. }
  2983. /* Returns the speed of a device attached to a port on the root hub. */
  2984. static inline unsigned int oxu_port_speed(struct oxu_hcd *oxu,
  2985. unsigned int portsc)
  2986. {
  2987. switch ((portsc >> 26) & 3) {
  2988. case 0:
  2989. return 0;
  2990. case 1:
  2991. return USB_PORT_STAT_LOW_SPEED;
  2992. case 2:
  2993. default:
  2994. return USB_PORT_STAT_HIGH_SPEED;
  2995. }
  2996. }
  2997. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  2998. static int oxu_hub_control(struct usb_hcd *hcd, u16 typeReq,
  2999. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  3000. {
  3001. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  3002. int ports = HCS_N_PORTS(oxu->hcs_params);
  3003. u32 __iomem *status_reg = &oxu->regs->port_status[wIndex - 1];
  3004. u32 temp, status;
  3005. unsigned long flags;
  3006. int retval = 0;
  3007. unsigned selector;
  3008. /*
  3009. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  3010. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  3011. * (track current state ourselves) ... blink for diagnostics,
  3012. * power, "this is the one", etc. EHCI spec supports this.
  3013. */
  3014. spin_lock_irqsave(&oxu->lock, flags);
  3015. switch (typeReq) {
  3016. case ClearHubFeature:
  3017. switch (wValue) {
  3018. case C_HUB_LOCAL_POWER:
  3019. case C_HUB_OVER_CURRENT:
  3020. /* no hub-wide feature/status flags */
  3021. break;
  3022. default:
  3023. goto error;
  3024. }
  3025. break;
  3026. case ClearPortFeature:
  3027. if (!wIndex || wIndex > ports)
  3028. goto error;
  3029. wIndex--;
  3030. temp = readl(status_reg);
  3031. /*
  3032. * Even if OWNER is set, so the port is owned by the
  3033. * companion controller, hub_wq needs to be able to clear
  3034. * the port-change status bits (especially
  3035. * USB_PORT_STAT_C_CONNECTION).
  3036. */
  3037. switch (wValue) {
  3038. case USB_PORT_FEAT_ENABLE:
  3039. writel(temp & ~PORT_PE, status_reg);
  3040. break;
  3041. case USB_PORT_FEAT_C_ENABLE:
  3042. writel((temp & ~PORT_RWC_BITS) | PORT_PEC, status_reg);
  3043. break;
  3044. case USB_PORT_FEAT_SUSPEND:
  3045. if (temp & PORT_RESET)
  3046. goto error;
  3047. if (temp & PORT_SUSPEND) {
  3048. if ((temp & PORT_PE) == 0)
  3049. goto error;
  3050. /* resume signaling for 20 msec */
  3051. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  3052. writel(temp | PORT_RESUME, status_reg);
  3053. oxu->reset_done[wIndex] = jiffies
  3054. + msecs_to_jiffies(20);
  3055. }
  3056. break;
  3057. case USB_PORT_FEAT_C_SUSPEND:
  3058. /* we auto-clear this feature */
  3059. break;
  3060. case USB_PORT_FEAT_POWER:
  3061. if (HCS_PPC(oxu->hcs_params))
  3062. writel(temp & ~(PORT_RWC_BITS | PORT_POWER),
  3063. status_reg);
  3064. break;
  3065. case USB_PORT_FEAT_C_CONNECTION:
  3066. writel((temp & ~PORT_RWC_BITS) | PORT_CSC, status_reg);
  3067. break;
  3068. case USB_PORT_FEAT_C_OVER_CURRENT:
  3069. writel((temp & ~PORT_RWC_BITS) | PORT_OCC, status_reg);
  3070. break;
  3071. case USB_PORT_FEAT_C_RESET:
  3072. /* GetPortStatus clears reset */
  3073. break;
  3074. default:
  3075. goto error;
  3076. }
  3077. readl(&oxu->regs->command); /* unblock posted write */
  3078. break;
  3079. case GetHubDescriptor:
  3080. ehci_hub_descriptor(oxu, (struct usb_hub_descriptor *)
  3081. buf);
  3082. break;
  3083. case GetHubStatus:
  3084. /* no hub-wide feature/status flags */
  3085. memset(buf, 0, 4);
  3086. break;
  3087. case GetPortStatus:
  3088. if (!wIndex || wIndex > ports)
  3089. goto error;
  3090. wIndex--;
  3091. status = 0;
  3092. temp = readl(status_reg);
  3093. /* wPortChange bits */
  3094. if (temp & PORT_CSC)
  3095. status |= USB_PORT_STAT_C_CONNECTION << 16;
  3096. if (temp & PORT_PEC)
  3097. status |= USB_PORT_STAT_C_ENABLE << 16;
  3098. if ((temp & PORT_OCC) && !ignore_oc)
  3099. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  3100. /* whoever resumes must GetPortStatus to complete it!! */
  3101. if (temp & PORT_RESUME) {
  3102. /* Remote Wakeup received? */
  3103. if (!oxu->reset_done[wIndex]) {
  3104. /* resume signaling for 20 msec */
  3105. oxu->reset_done[wIndex] = jiffies
  3106. + msecs_to_jiffies(20);
  3107. /* check the port again */
  3108. mod_timer(&oxu_to_hcd(oxu)->rh_timer,
  3109. oxu->reset_done[wIndex]);
  3110. }
  3111. /* resume completed? */
  3112. else if (time_after_eq(jiffies,
  3113. oxu->reset_done[wIndex])) {
  3114. status |= USB_PORT_STAT_C_SUSPEND << 16;
  3115. oxu->reset_done[wIndex] = 0;
  3116. /* stop resume signaling */
  3117. temp = readl(status_reg);
  3118. writel(temp & ~(PORT_RWC_BITS | PORT_RESUME),
  3119. status_reg);
  3120. retval = handshake(oxu, status_reg,
  3121. PORT_RESUME, 0, 2000 /* 2msec */);
  3122. if (retval != 0) {
  3123. oxu_err(oxu,
  3124. "port %d resume error %d\n",
  3125. wIndex + 1, retval);
  3126. goto error;
  3127. }
  3128. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  3129. }
  3130. }
  3131. /* whoever resets must GetPortStatus to complete it!! */
  3132. if ((temp & PORT_RESET)
  3133. && time_after_eq(jiffies,
  3134. oxu->reset_done[wIndex])) {
  3135. status |= USB_PORT_STAT_C_RESET << 16;
  3136. oxu->reset_done[wIndex] = 0;
  3137. /* force reset to complete */
  3138. writel(temp & ~(PORT_RWC_BITS | PORT_RESET),
  3139. status_reg);
  3140. /* REVISIT: some hardware needs 550+ usec to clear
  3141. * this bit; seems too long to spin routinely...
  3142. */
  3143. retval = handshake(oxu, status_reg,
  3144. PORT_RESET, 0, 750);
  3145. if (retval != 0) {
  3146. oxu_err(oxu, "port %d reset error %d\n",
  3147. wIndex + 1, retval);
  3148. goto error;
  3149. }
  3150. /* see what we found out */
  3151. temp = check_reset_complete(oxu, wIndex, status_reg,
  3152. readl(status_reg));
  3153. }
  3154. /* transfer dedicated ports to the companion hc */
  3155. if ((temp & PORT_CONNECT) &&
  3156. test_bit(wIndex, &oxu->companion_ports)) {
  3157. temp &= ~PORT_RWC_BITS;
  3158. temp |= PORT_OWNER;
  3159. writel(temp, status_reg);
  3160. oxu_dbg(oxu, "port %d --> companion\n", wIndex + 1);
  3161. temp = readl(status_reg);
  3162. }
  3163. /*
  3164. * Even if OWNER is set, there's no harm letting hub_wq
  3165. * see the wPortStatus values (they should all be 0 except
  3166. * for PORT_POWER anyway).
  3167. */
  3168. if (temp & PORT_CONNECT) {
  3169. status |= USB_PORT_STAT_CONNECTION;
  3170. /* status may be from integrated TT */
  3171. status |= oxu_port_speed(oxu, temp);
  3172. }
  3173. if (temp & PORT_PE)
  3174. status |= USB_PORT_STAT_ENABLE;
  3175. if (temp & (PORT_SUSPEND|PORT_RESUME))
  3176. status |= USB_PORT_STAT_SUSPEND;
  3177. if (temp & PORT_OC)
  3178. status |= USB_PORT_STAT_OVERCURRENT;
  3179. if (temp & PORT_RESET)
  3180. status |= USB_PORT_STAT_RESET;
  3181. if (temp & PORT_POWER)
  3182. status |= USB_PORT_STAT_POWER;
  3183. #ifndef OXU_VERBOSE_DEBUG
  3184. if (status & ~0xffff) /* only if wPortChange is interesting */
  3185. #endif
  3186. dbg_port(oxu, "GetStatus", wIndex + 1, temp);
  3187. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  3188. break;
  3189. case SetHubFeature:
  3190. switch (wValue) {
  3191. case C_HUB_LOCAL_POWER:
  3192. case C_HUB_OVER_CURRENT:
  3193. /* no hub-wide feature/status flags */
  3194. break;
  3195. default:
  3196. goto error;
  3197. }
  3198. break;
  3199. case SetPortFeature:
  3200. selector = wIndex >> 8;
  3201. wIndex &= 0xff;
  3202. if (!wIndex || wIndex > ports)
  3203. goto error;
  3204. wIndex--;
  3205. temp = readl(status_reg);
  3206. if (temp & PORT_OWNER)
  3207. break;
  3208. temp &= ~PORT_RWC_BITS;
  3209. switch (wValue) {
  3210. case USB_PORT_FEAT_SUSPEND:
  3211. if ((temp & PORT_PE) == 0
  3212. || (temp & PORT_RESET) != 0)
  3213. goto error;
  3214. if (device_may_wakeup(&hcd->self.root_hub->dev))
  3215. temp |= PORT_WAKE_BITS;
  3216. writel(temp | PORT_SUSPEND, status_reg);
  3217. break;
  3218. case USB_PORT_FEAT_POWER:
  3219. if (HCS_PPC(oxu->hcs_params))
  3220. writel(temp | PORT_POWER, status_reg);
  3221. break;
  3222. case USB_PORT_FEAT_RESET:
  3223. if (temp & PORT_RESUME)
  3224. goto error;
  3225. /* line status bits may report this as low speed,
  3226. * which can be fine if this root hub has a
  3227. * transaction translator built in.
  3228. */
  3229. oxu_vdbg(oxu, "port %d reset\n", wIndex + 1);
  3230. temp |= PORT_RESET;
  3231. temp &= ~PORT_PE;
  3232. /*
  3233. * caller must wait, then call GetPortStatus
  3234. * usb 2.0 spec says 50 ms resets on root
  3235. */
  3236. oxu->reset_done[wIndex] = jiffies
  3237. + msecs_to_jiffies(50);
  3238. writel(temp, status_reg);
  3239. break;
  3240. /* For downstream facing ports (these): one hub port is put
  3241. * into test mode according to USB2 11.24.2.13, then the hub
  3242. * must be reset (which for root hub now means rmmod+modprobe,
  3243. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  3244. * about the EHCI-specific stuff.
  3245. */
  3246. case USB_PORT_FEAT_TEST:
  3247. if (!selector || selector > 5)
  3248. goto error;
  3249. ehci_quiesce(oxu);
  3250. ehci_halt(oxu);
  3251. temp |= selector << 16;
  3252. writel(temp, status_reg);
  3253. break;
  3254. default:
  3255. goto error;
  3256. }
  3257. readl(&oxu->regs->command); /* unblock posted writes */
  3258. break;
  3259. default:
  3260. error:
  3261. /* "stall" on error */
  3262. retval = -EPIPE;
  3263. }
  3264. spin_unlock_irqrestore(&oxu->lock, flags);
  3265. return retval;
  3266. }
  3267. #ifdef CONFIG_PM
  3268. static int oxu_bus_suspend(struct usb_hcd *hcd)
  3269. {
  3270. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  3271. int port;
  3272. int mask;
  3273. oxu_dbg(oxu, "suspend root hub\n");
  3274. if (time_before(jiffies, oxu->next_statechange))
  3275. msleep(5);
  3276. port = HCS_N_PORTS(oxu->hcs_params);
  3277. spin_lock_irq(&oxu->lock);
  3278. /* stop schedules, clean any completed work */
  3279. if (HC_IS_RUNNING(hcd->state)) {
  3280. ehci_quiesce(oxu);
  3281. hcd->state = HC_STATE_QUIESCING;
  3282. }
  3283. oxu->command = readl(&oxu->regs->command);
  3284. if (oxu->reclaim)
  3285. oxu->reclaim_ready = 1;
  3286. ehci_work(oxu);
  3287. /* Unlike other USB host controller types, EHCI doesn't have
  3288. * any notion of "global" or bus-wide suspend. The driver has
  3289. * to manually suspend all the active unsuspended ports, and
  3290. * then manually resume them in the bus_resume() routine.
  3291. */
  3292. oxu->bus_suspended = 0;
  3293. while (port--) {
  3294. u32 __iomem *reg = &oxu->regs->port_status[port];
  3295. u32 t1 = readl(reg) & ~PORT_RWC_BITS;
  3296. u32 t2 = t1;
  3297. /* keep track of which ports we suspend */
  3298. if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
  3299. !(t1 & PORT_SUSPEND)) {
  3300. t2 |= PORT_SUSPEND;
  3301. set_bit(port, &oxu->bus_suspended);
  3302. }
  3303. /* enable remote wakeup on all ports */
  3304. if (device_may_wakeup(&hcd->self.root_hub->dev))
  3305. t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
  3306. else
  3307. t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  3308. if (t1 != t2) {
  3309. oxu_vdbg(oxu, "port %d, %08x -> %08x\n",
  3310. port + 1, t1, t2);
  3311. writel(t2, reg);
  3312. }
  3313. }
  3314. spin_unlock_irq(&oxu->lock);
  3315. /* turn off now-idle HC */
  3316. del_timer_sync(&oxu->watchdog);
  3317. spin_lock_irq(&oxu->lock);
  3318. ehci_halt(oxu);
  3319. hcd->state = HC_STATE_SUSPENDED;
  3320. /* allow remote wakeup */
  3321. mask = INTR_MASK;
  3322. if (!device_may_wakeup(&hcd->self.root_hub->dev))
  3323. mask &= ~STS_PCD;
  3324. writel(mask, &oxu->regs->intr_enable);
  3325. readl(&oxu->regs->intr_enable);
  3326. oxu->next_statechange = jiffies + msecs_to_jiffies(10);
  3327. spin_unlock_irq(&oxu->lock);
  3328. return 0;
  3329. }
  3330. /* Caller has locked the root hub, and should reset/reinit on error */
  3331. static int oxu_bus_resume(struct usb_hcd *hcd)
  3332. {
  3333. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  3334. u32 temp;
  3335. int i;
  3336. if (time_before(jiffies, oxu->next_statechange))
  3337. msleep(5);
  3338. spin_lock_irq(&oxu->lock);
  3339. /* Ideally and we've got a real resume here, and no port's power
  3340. * was lost. (For PCI, that means Vaux was maintained.) But we
  3341. * could instead be restoring a swsusp snapshot -- so that BIOS was
  3342. * the last user of the controller, not reset/pm hardware keeping
  3343. * state we gave to it.
  3344. */
  3345. temp = readl(&oxu->regs->intr_enable);
  3346. oxu_dbg(oxu, "resume root hub%s\n", temp ? "" : " after power loss");
  3347. /* at least some APM implementations will try to deliver
  3348. * IRQs right away, so delay them until we're ready.
  3349. */
  3350. writel(0, &oxu->regs->intr_enable);
  3351. /* re-init operational registers */
  3352. writel(0, &oxu->regs->segment);
  3353. writel(oxu->periodic_dma, &oxu->regs->frame_list);
  3354. writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
  3355. /* restore CMD_RUN, framelist size, and irq threshold */
  3356. writel(oxu->command, &oxu->regs->command);
  3357. /* Some controller/firmware combinations need a delay during which
  3358. * they set up the port statuses. See Bugzilla #8190. */
  3359. mdelay(8);
  3360. /* manually resume the ports we suspended during bus_suspend() */
  3361. i = HCS_N_PORTS(oxu->hcs_params);
  3362. while (i--) {
  3363. temp = readl(&oxu->regs->port_status[i]);
  3364. temp &= ~(PORT_RWC_BITS
  3365. | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
  3366. if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
  3367. oxu->reset_done[i] = jiffies + msecs_to_jiffies(20);
  3368. temp |= PORT_RESUME;
  3369. }
  3370. writel(temp, &oxu->regs->port_status[i]);
  3371. }
  3372. i = HCS_N_PORTS(oxu->hcs_params);
  3373. mdelay(20);
  3374. while (i--) {
  3375. temp = readl(&oxu->regs->port_status[i]);
  3376. if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
  3377. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  3378. writel(temp, &oxu->regs->port_status[i]);
  3379. oxu_vdbg(oxu, "resumed port %d\n", i + 1);
  3380. }
  3381. }
  3382. (void) readl(&oxu->regs->command);
  3383. /* maybe re-activate the schedule(s) */
  3384. temp = 0;
  3385. if (oxu->async->qh_next.qh)
  3386. temp |= CMD_ASE;
  3387. if (oxu->periodic_sched)
  3388. temp |= CMD_PSE;
  3389. if (temp) {
  3390. oxu->command |= temp;
  3391. writel(oxu->command, &oxu->regs->command);
  3392. }
  3393. oxu->next_statechange = jiffies + msecs_to_jiffies(5);
  3394. hcd->state = HC_STATE_RUNNING;
  3395. /* Now we can safely re-enable irqs */
  3396. writel(INTR_MASK, &oxu->regs->intr_enable);
  3397. spin_unlock_irq(&oxu->lock);
  3398. return 0;
  3399. }
  3400. #else
  3401. static int oxu_bus_suspend(struct usb_hcd *hcd)
  3402. {
  3403. return 0;
  3404. }
  3405. static int oxu_bus_resume(struct usb_hcd *hcd)
  3406. {
  3407. return 0;
  3408. }
  3409. #endif /* CONFIG_PM */
  3410. static const struct hc_driver oxu_hc_driver = {
  3411. .description = "oxu210hp_hcd",
  3412. .product_desc = "oxu210hp HCD",
  3413. .hcd_priv_size = sizeof(struct oxu_hcd),
  3414. /*
  3415. * Generic hardware linkage
  3416. */
  3417. .irq = oxu_irq,
  3418. .flags = HCD_MEMORY | HCD_USB2,
  3419. /*
  3420. * Basic lifecycle operations
  3421. */
  3422. .reset = oxu_reset,
  3423. .start = oxu_run,
  3424. .stop = oxu_stop,
  3425. .shutdown = oxu_shutdown,
  3426. /*
  3427. * Managing i/o requests and associated device resources
  3428. */
  3429. .urb_enqueue = oxu_urb_enqueue,
  3430. .urb_dequeue = oxu_urb_dequeue,
  3431. .endpoint_disable = oxu_endpoint_disable,
  3432. /*
  3433. * Scheduling support
  3434. */
  3435. .get_frame_number = oxu_get_frame,
  3436. /*
  3437. * Root hub support
  3438. */
  3439. .hub_status_data = oxu_hub_status_data,
  3440. .hub_control = oxu_hub_control,
  3441. .bus_suspend = oxu_bus_suspend,
  3442. .bus_resume = oxu_bus_resume,
  3443. };
  3444. /*
  3445. * Module stuff
  3446. */
  3447. static void oxu_configuration(struct platform_device *pdev, void __iomem *base)
  3448. {
  3449. u32 tmp;
  3450. /* Initialize top level registers.
  3451. * First write ever
  3452. */
  3453. oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
  3454. oxu_writel(base, OXU_SOFTRESET, OXU_SRESET);
  3455. oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
  3456. tmp = oxu_readl(base, OXU_PIOBURSTREADCTRL);
  3457. oxu_writel(base, OXU_PIOBURSTREADCTRL, tmp | 0x0040);
  3458. oxu_writel(base, OXU_ASO, OXU_SPHPOEN | OXU_OVRCCURPUPDEN |
  3459. OXU_COMPARATOR | OXU_ASO_OP);
  3460. tmp = oxu_readl(base, OXU_CLKCTRL_SET);
  3461. oxu_writel(base, OXU_CLKCTRL_SET, tmp | OXU_SYSCLKEN | OXU_USBOTGCLKEN);
  3462. /* Clear all top interrupt enable */
  3463. oxu_writel(base, OXU_CHIPIRQEN_CLR, 0xff);
  3464. /* Clear all top interrupt status */
  3465. oxu_writel(base, OXU_CHIPIRQSTATUS, 0xff);
  3466. /* Enable all needed top interrupt except OTG SPH core */
  3467. oxu_writel(base, OXU_CHIPIRQEN_SET, OXU_USBSPHLPWUI | OXU_USBOTGLPWUI);
  3468. }
  3469. static int oxu_verify_id(struct platform_device *pdev, void __iomem *base)
  3470. {
  3471. u32 id;
  3472. static const char * const bo[] = {
  3473. "reserved",
  3474. "128-pin LQFP",
  3475. "84-pin TFBGA",
  3476. "reserved",
  3477. };
  3478. /* Read controller signature register to find a match */
  3479. id = oxu_readl(base, OXU_DEVICEID);
  3480. dev_info(&pdev->dev, "device ID %x\n", id);
  3481. if ((id & OXU_REV_MASK) != (OXU_REV_2100 << OXU_REV_SHIFT))
  3482. return -1;
  3483. dev_info(&pdev->dev, "found device %x %s (%04x:%04x)\n",
  3484. id >> OXU_REV_SHIFT,
  3485. bo[(id & OXU_BO_MASK) >> OXU_BO_SHIFT],
  3486. (id & OXU_MAJ_REV_MASK) >> OXU_MAJ_REV_SHIFT,
  3487. (id & OXU_MIN_REV_MASK) >> OXU_MIN_REV_SHIFT);
  3488. return 0;
  3489. }
  3490. static const struct hc_driver oxu_hc_driver;
  3491. static struct usb_hcd *oxu_create(struct platform_device *pdev,
  3492. unsigned long memstart, unsigned long memlen,
  3493. void __iomem *base, int irq, int otg)
  3494. {
  3495. struct device *dev = &pdev->dev;
  3496. struct usb_hcd *hcd;
  3497. struct oxu_hcd *oxu;
  3498. int ret;
  3499. /* Set endian mode and host mode */
  3500. oxu_writel(base + (otg ? OXU_OTG_CORE_OFFSET : OXU_SPH_CORE_OFFSET),
  3501. OXU_USBMODE,
  3502. OXU_CM_HOST_ONLY | OXU_ES_LITTLE | OXU_VBPS);
  3503. hcd = usb_create_hcd(&oxu_hc_driver, dev,
  3504. otg ? "oxu210hp_otg" : "oxu210hp_sph");
  3505. if (!hcd)
  3506. return ERR_PTR(-ENOMEM);
  3507. hcd->rsrc_start = memstart;
  3508. hcd->rsrc_len = memlen;
  3509. hcd->regs = base;
  3510. hcd->irq = irq;
  3511. hcd->state = HC_STATE_HALT;
  3512. oxu = hcd_to_oxu(hcd);
  3513. oxu->is_otg = otg;
  3514. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  3515. if (ret < 0) {
  3516. usb_put_hcd(hcd);
  3517. return ERR_PTR(ret);
  3518. }
  3519. device_wakeup_enable(hcd->self.controller);
  3520. return hcd;
  3521. }
  3522. static int oxu_init(struct platform_device *pdev,
  3523. unsigned long memstart, unsigned long memlen,
  3524. void __iomem *base, int irq)
  3525. {
  3526. struct oxu_info *info = platform_get_drvdata(pdev);
  3527. struct usb_hcd *hcd;
  3528. int ret;
  3529. /* First time configuration at start up */
  3530. oxu_configuration(pdev, base);
  3531. ret = oxu_verify_id(pdev, base);
  3532. if (ret) {
  3533. dev_err(&pdev->dev, "no devices found!\n");
  3534. return -ENODEV;
  3535. }
  3536. /* Create the OTG controller */
  3537. hcd = oxu_create(pdev, memstart, memlen, base, irq, 1);
  3538. if (IS_ERR(hcd)) {
  3539. dev_err(&pdev->dev, "cannot create OTG controller!\n");
  3540. ret = PTR_ERR(hcd);
  3541. goto error_create_otg;
  3542. }
  3543. info->hcd[0] = hcd;
  3544. /* Create the SPH host controller */
  3545. hcd = oxu_create(pdev, memstart, memlen, base, irq, 0);
  3546. if (IS_ERR(hcd)) {
  3547. dev_err(&pdev->dev, "cannot create SPH controller!\n");
  3548. ret = PTR_ERR(hcd);
  3549. goto error_create_sph;
  3550. }
  3551. info->hcd[1] = hcd;
  3552. oxu_writel(base, OXU_CHIPIRQEN_SET,
  3553. oxu_readl(base, OXU_CHIPIRQEN_SET) | 3);
  3554. return 0;
  3555. error_create_sph:
  3556. usb_remove_hcd(info->hcd[0]);
  3557. usb_put_hcd(info->hcd[0]);
  3558. error_create_otg:
  3559. return ret;
  3560. }
  3561. static int oxu_drv_probe(struct platform_device *pdev)
  3562. {
  3563. struct resource *res;
  3564. void __iomem *base;
  3565. unsigned long memstart, memlen;
  3566. int irq, ret;
  3567. struct oxu_info *info;
  3568. if (usb_disabled())
  3569. return -ENODEV;
  3570. /*
  3571. * Get the platform resources
  3572. */
  3573. irq = platform_get_irq(pdev, 0);
  3574. if (irq < 0)
  3575. return irq;
  3576. dev_dbg(&pdev->dev, "IRQ resource %d\n", irq);
  3577. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3578. base = devm_ioremap_resource(&pdev->dev, res);
  3579. if (IS_ERR(base)) {
  3580. ret = PTR_ERR(base);
  3581. goto error;
  3582. }
  3583. memstart = res->start;
  3584. memlen = resource_size(res);
  3585. ret = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING);
  3586. if (ret) {
  3587. dev_err(&pdev->dev, "error setting irq type\n");
  3588. ret = -EFAULT;
  3589. goto error;
  3590. }
  3591. /* Allocate a driver data struct to hold useful info for both
  3592. * SPH & OTG devices
  3593. */
  3594. info = devm_kzalloc(&pdev->dev, sizeof(struct oxu_info), GFP_KERNEL);
  3595. if (!info) {
  3596. ret = -EFAULT;
  3597. goto error;
  3598. }
  3599. platform_set_drvdata(pdev, info);
  3600. ret = oxu_init(pdev, memstart, memlen, base, irq);
  3601. if (ret < 0) {
  3602. dev_dbg(&pdev->dev, "cannot init USB devices\n");
  3603. goto error;
  3604. }
  3605. dev_info(&pdev->dev, "devices enabled and running\n");
  3606. platform_set_drvdata(pdev, info);
  3607. return 0;
  3608. error:
  3609. dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), ret);
  3610. return ret;
  3611. }
  3612. static void oxu_remove(struct platform_device *pdev, struct usb_hcd *hcd)
  3613. {
  3614. usb_remove_hcd(hcd);
  3615. usb_put_hcd(hcd);
  3616. }
  3617. static int oxu_drv_remove(struct platform_device *pdev)
  3618. {
  3619. struct oxu_info *info = platform_get_drvdata(pdev);
  3620. oxu_remove(pdev, info->hcd[0]);
  3621. oxu_remove(pdev, info->hcd[1]);
  3622. return 0;
  3623. }
  3624. static void oxu_drv_shutdown(struct platform_device *pdev)
  3625. {
  3626. oxu_drv_remove(pdev);
  3627. }
  3628. #if 0
  3629. /* FIXME: TODO */
  3630. static int oxu_drv_suspend(struct device *dev)
  3631. {
  3632. struct platform_device *pdev = to_platform_device(dev);
  3633. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3634. return 0;
  3635. }
  3636. static int oxu_drv_resume(struct device *dev)
  3637. {
  3638. struct platform_device *pdev = to_platform_device(dev);
  3639. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3640. return 0;
  3641. }
  3642. #else
  3643. #define oxu_drv_suspend NULL
  3644. #define oxu_drv_resume NULL
  3645. #endif
  3646. static struct platform_driver oxu_driver = {
  3647. .probe = oxu_drv_probe,
  3648. .remove = oxu_drv_remove,
  3649. .shutdown = oxu_drv_shutdown,
  3650. .suspend = oxu_drv_suspend,
  3651. .resume = oxu_drv_resume,
  3652. .driver = {
  3653. .name = "oxu210hp-hcd",
  3654. .bus = &platform_bus_type
  3655. }
  3656. };
  3657. module_platform_driver(oxu_driver);
  3658. MODULE_DESCRIPTION("Oxford OXU210HP HCD driver - ver. " DRIVER_VERSION);
  3659. MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  3660. MODULE_LICENSE("GPL");