oxu210hp-hcd.c 97 KB

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