oxu210hp-hcd.c 111 KB

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