octeon-hcd.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Cavium Networks
  7. *
  8. * Some parts of the code were originally released under BSD license:
  9. *
  10. * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * * Neither the name of Cavium Networks nor the names of
  26. * its contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written
  28. * permission.
  29. *
  30. * This Software, including technical data, may be subject to U.S. export
  31. * control laws, including the U.S. Export Administration Act and its associated
  32. * regulations, and may be subject to export or import regulations in other
  33. * countries.
  34. *
  35. * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
  36. * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
  37. * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
  38. * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION
  39. * OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
  40. * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
  41. * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
  42. * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
  43. * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
  44. * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
  45. */
  46. #include <linux/kernel.h>
  47. #include <linux/module.h>
  48. #include <linux/init.h>
  49. #include <linux/pci.h>
  50. #include <linux/prefetch.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/usb.h>
  54. #include <linux/time.h>
  55. #include <linux/delay.h>
  56. #include <asm/octeon/cvmx.h>
  57. #include <asm/octeon/cvmx-iob-defs.h>
  58. #include <linux/usb/hcd.h>
  59. #include <linux/err.h>
  60. #include <asm/octeon/octeon.h>
  61. #include <asm/octeon/cvmx-helper.h>
  62. #include <asm/octeon/cvmx-sysinfo.h>
  63. #include <asm/octeon/cvmx-helper-board.h>
  64. #include "octeon-hcd.h"
  65. /**
  66. * enum cvmx_usb_speed - the possible USB device speeds
  67. *
  68. * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
  69. * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
  70. * @CVMX_USB_SPEED_LOW: Device is operation at 1.5Mbps
  71. */
  72. enum cvmx_usb_speed {
  73. CVMX_USB_SPEED_HIGH = 0,
  74. CVMX_USB_SPEED_FULL = 1,
  75. CVMX_USB_SPEED_LOW = 2,
  76. };
  77. /**
  78. * enum cvmx_usb_transfer - the possible USB transfer types
  79. *
  80. * @CVMX_USB_TRANSFER_CONTROL: USB transfer type control for hub and status
  81. * transfers
  82. * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
  83. * priority periodic transfers
  84. * @CVMX_USB_TRANSFER_BULK: USB transfer type bulk for large low priority
  85. * transfers
  86. * @CVMX_USB_TRANSFER_INTERRUPT: USB transfer type interrupt for high priority
  87. * periodic transfers
  88. */
  89. enum cvmx_usb_transfer {
  90. CVMX_USB_TRANSFER_CONTROL = 0,
  91. CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
  92. CVMX_USB_TRANSFER_BULK = 2,
  93. CVMX_USB_TRANSFER_INTERRUPT = 3,
  94. };
  95. /**
  96. * enum cvmx_usb_direction - the transfer directions
  97. *
  98. * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
  99. * @CVMX_USB_DIRECTION_IN: Data is transferring from the device/host to Octeon
  100. */
  101. enum cvmx_usb_direction {
  102. CVMX_USB_DIRECTION_OUT,
  103. CVMX_USB_DIRECTION_IN,
  104. };
  105. /**
  106. * enum cvmx_usb_complete - possible callback function status codes
  107. *
  108. * @CVMX_USB_COMPLETE_SUCCESS: The transaction / operation finished without
  109. * any errors
  110. * @CVMX_USB_COMPLETE_SHORT: FIXME: This is currently not implemented
  111. * @CVMX_USB_COMPLETE_CANCEL: The transaction was canceled while in flight
  112. * by a user call to cvmx_usb_cancel
  113. * @CVMX_USB_COMPLETE_ERROR: The transaction aborted with an unexpected
  114. * error status
  115. * @CVMX_USB_COMPLETE_STALL: The transaction received a USB STALL response
  116. * from the device
  117. * @CVMX_USB_COMPLETE_XACTERR: The transaction failed with an error from the
  118. * device even after a number of retries
  119. * @CVMX_USB_COMPLETE_DATATGLERR: The transaction failed with a data toggle
  120. * error even after a number of retries
  121. * @CVMX_USB_COMPLETE_BABBLEERR: The transaction failed with a babble error
  122. * @CVMX_USB_COMPLETE_FRAMEERR: The transaction failed with a frame error
  123. * even after a number of retries
  124. */
  125. enum cvmx_usb_complete {
  126. CVMX_USB_COMPLETE_SUCCESS,
  127. CVMX_USB_COMPLETE_SHORT,
  128. CVMX_USB_COMPLETE_CANCEL,
  129. CVMX_USB_COMPLETE_ERROR,
  130. CVMX_USB_COMPLETE_STALL,
  131. CVMX_USB_COMPLETE_XACTERR,
  132. CVMX_USB_COMPLETE_DATATGLERR,
  133. CVMX_USB_COMPLETE_BABBLEERR,
  134. CVMX_USB_COMPLETE_FRAMEERR,
  135. };
  136. /**
  137. * struct cvmx_usb_port_status - the USB port status information
  138. *
  139. * @port_enabled: 1 = Usb port is enabled, 0 = disabled
  140. * @port_over_current: 1 = Over current detected, 0 = Over current not
  141. * detected. Octeon doesn't support over current detection.
  142. * @port_powered: 1 = Port power is being supplied to the device, 0 =
  143. * power is off. Octeon doesn't support turning port power
  144. * off.
  145. * @port_speed: Current port speed.
  146. * @connected: 1 = A device is connected to the port, 0 = No device is
  147. * connected.
  148. * @connect_change: 1 = Device connected state changed since the last set
  149. * status call.
  150. */
  151. struct cvmx_usb_port_status {
  152. uint32_t reserved : 25;
  153. uint32_t port_enabled : 1;
  154. uint32_t port_over_current : 1;
  155. uint32_t port_powered : 1;
  156. enum cvmx_usb_speed port_speed : 2;
  157. uint32_t connected : 1;
  158. uint32_t connect_change : 1;
  159. };
  160. /**
  161. * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
  162. *
  163. * @offset: This is the offset in bytes into the main buffer where this data
  164. * is stored.
  165. * @length: This is the length in bytes of the data.
  166. * @status: This is the status of this individual packet transfer.
  167. */
  168. struct cvmx_usb_iso_packet {
  169. int offset;
  170. int length;
  171. enum cvmx_usb_complete status;
  172. };
  173. /**
  174. * enum cvmx_usb_initialize_flags - flags used by the initialization function
  175. *
  176. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI: The USB port uses a 12MHz crystal
  177. * as clock source at USB_XO and
  178. * USB_XI.
  179. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND: The USB port uses 12/24/48MHz 2.5V
  180. * board clock source at USB_XO.
  181. * USB_XI should be tied to GND.
  182. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
  183. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ: Speed of reference clock or
  184. * crystal
  185. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ: Speed of reference clock
  186. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ: Speed of reference clock
  187. * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA: Disable DMA and used polled IO for
  188. * data transfer use for the USB
  189. */
  190. enum cvmx_usb_initialize_flags {
  191. CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
  192. CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
  193. CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
  194. CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
  195. CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
  196. CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
  197. /* Bits 3-4 used to encode the clock frequency */
  198. CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
  199. };
  200. /**
  201. * enum cvmx_usb_pipe_flags - internal flags for a pipe.
  202. *
  203. * @__CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
  204. * actively using hardware. Do not use.
  205. * @__CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high
  206. * speed pipe is in the ping state. Do not
  207. * use.
  208. */
  209. enum cvmx_usb_pipe_flags {
  210. __CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
  211. __CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
  212. };
  213. /* Maximum number of times to retry failed transactions */
  214. #define MAX_RETRIES 3
  215. /* Maximum number of hardware channels supported by the USB block */
  216. #define MAX_CHANNELS 8
  217. /* The highest valid USB device address */
  218. #define MAX_USB_ADDRESS 127
  219. /* The highest valid USB endpoint number */
  220. #define MAX_USB_ENDPOINT 15
  221. /* The highest valid port number on a hub */
  222. #define MAX_USB_HUB_PORT 15
  223. /*
  224. * The low level hardware can transfer a maximum of this number of bytes in each
  225. * transfer. The field is 19 bits wide
  226. */
  227. #define MAX_TRANSFER_BYTES ((1<<19)-1)
  228. /*
  229. * The low level hardware can transfer a maximum of this number of packets in
  230. * each transfer. The field is 10 bits wide
  231. */
  232. #define MAX_TRANSFER_PACKETS ((1<<10)-1)
  233. /**
  234. * Logical transactions may take numerous low level
  235. * transactions, especially when splits are concerned. This
  236. * enum represents all of the possible stages a transaction can
  237. * be in. Note that split completes are always even. This is so
  238. * the NAK handler can backup to the previous low level
  239. * transaction with a simple clearing of bit 0.
  240. */
  241. enum cvmx_usb_stage {
  242. CVMX_USB_STAGE_NON_CONTROL,
  243. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
  244. CVMX_USB_STAGE_SETUP,
  245. CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
  246. CVMX_USB_STAGE_DATA,
  247. CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
  248. CVMX_USB_STAGE_STATUS,
  249. CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
  250. };
  251. /**
  252. * struct cvmx_usb_transaction - describes each pending USB transaction
  253. * regardless of type. These are linked together
  254. * to form a list of pending requests for a pipe.
  255. *
  256. * @node: List node for transactions in the pipe.
  257. * @type: Type of transaction, duplicated of the pipe.
  258. * @flags: State flags for this transaction.
  259. * @buffer: User's physical buffer address to read/write.
  260. * @buffer_length: Size of the user's buffer in bytes.
  261. * @control_header: For control transactions, physical address of the 8
  262. * byte standard header.
  263. * @iso_start_frame: For ISO transactions, the starting frame number.
  264. * @iso_number_packets: For ISO transactions, the number of packets in the
  265. * request.
  266. * @iso_packets: For ISO transactions, the sub packets in the request.
  267. * @actual_bytes: Actual bytes transfer for this transaction.
  268. * @stage: For control transactions, the current stage.
  269. * @urb: URB.
  270. */
  271. struct cvmx_usb_transaction {
  272. struct list_head node;
  273. enum cvmx_usb_transfer type;
  274. uint64_t buffer;
  275. int buffer_length;
  276. uint64_t control_header;
  277. int iso_start_frame;
  278. int iso_number_packets;
  279. struct cvmx_usb_iso_packet *iso_packets;
  280. int xfersize;
  281. int pktcnt;
  282. int retries;
  283. int actual_bytes;
  284. enum cvmx_usb_stage stage;
  285. struct urb *urb;
  286. };
  287. /**
  288. * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
  289. * and some USB device. It contains a list of pending
  290. * request to the device.
  291. *
  292. * @node: List node for pipe list
  293. * @next: Pipe after this one in the list
  294. * @transactions: List of pending transactions
  295. * @interval: For periodic pipes, the interval between packets in
  296. * frames
  297. * @next_tx_frame: The next frame this pipe is allowed to transmit on
  298. * @flags: State flags for this pipe
  299. * @device_speed: Speed of device connected to this pipe
  300. * @transfer_type: Type of transaction supported by this pipe
  301. * @transfer_dir: IN or OUT. Ignored for Control
  302. * @multi_count: Max packet in a row for the device
  303. * @max_packet: The device's maximum packet size in bytes
  304. * @device_addr: USB device address at other end of pipe
  305. * @endpoint_num: USB endpoint number at other end of pipe
  306. * @hub_device_addr: Hub address this device is connected to
  307. * @hub_port: Hub port this device is connected to
  308. * @pid_toggle: This toggles between 0/1 on every packet send to track
  309. * the data pid needed
  310. * @channel: Hardware DMA channel for this pipe
  311. * @split_sc_frame: The low order bits of the frame number the split
  312. * complete should be sent on
  313. */
  314. struct cvmx_usb_pipe {
  315. struct list_head node;
  316. struct list_head transactions;
  317. uint64_t interval;
  318. uint64_t next_tx_frame;
  319. enum cvmx_usb_pipe_flags flags;
  320. enum cvmx_usb_speed device_speed;
  321. enum cvmx_usb_transfer transfer_type;
  322. enum cvmx_usb_direction transfer_dir;
  323. int multi_count;
  324. uint16_t max_packet;
  325. uint8_t device_addr;
  326. uint8_t endpoint_num;
  327. uint8_t hub_device_addr;
  328. uint8_t hub_port;
  329. uint8_t pid_toggle;
  330. uint8_t channel;
  331. int8_t split_sc_frame;
  332. };
  333. struct cvmx_usb_tx_fifo {
  334. struct {
  335. int channel;
  336. int size;
  337. uint64_t address;
  338. } entry[MAX_CHANNELS+1];
  339. int head;
  340. int tail;
  341. };
  342. /**
  343. * struct cvmx_usb_state - the state of the USB block
  344. *
  345. * init_flags: Flags passed to initialize.
  346. * index: Which USB block this is for.
  347. * idle_hardware_channels: Bit set for every idle hardware channel.
  348. * usbcx_hprt: Stored port status so we don't need to read a CSR to
  349. * determine splits.
  350. * pipe_for_channel: Map channels to pipes.
  351. * pipe: Storage for pipes.
  352. * indent: Used by debug output to indent functions.
  353. * port_status: Last port status used for change notification.
  354. * idle_pipes: List of open pipes that have no transactions.
  355. * active_pipes: Active pipes indexed by transfer type.
  356. * frame_number: Increments every SOF interrupt for time keeping.
  357. * active_split: Points to the current active split, or NULL.
  358. */
  359. struct cvmx_usb_state {
  360. int init_flags;
  361. int index;
  362. int idle_hardware_channels;
  363. union cvmx_usbcx_hprt usbcx_hprt;
  364. struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
  365. int indent;
  366. struct cvmx_usb_port_status port_status;
  367. struct list_head idle_pipes;
  368. struct list_head active_pipes[4];
  369. uint64_t frame_number;
  370. struct cvmx_usb_transaction *active_split;
  371. struct cvmx_usb_tx_fifo periodic;
  372. struct cvmx_usb_tx_fifo nonperiodic;
  373. };
  374. struct octeon_hcd {
  375. spinlock_t lock;
  376. struct cvmx_usb_state usb;
  377. };
  378. /* This macro spins on a field waiting for it to reach a value */
  379. #define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
  380. ({int result; \
  381. do { \
  382. uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
  383. octeon_get_clock_rate() / 1000000; \
  384. type c; \
  385. while (1) { \
  386. c.u32 = __cvmx_usb_read_csr32(usb, address); \
  387. if (c.s.field op (value)) { \
  388. result = 0; \
  389. break; \
  390. } else if (cvmx_get_cycle() > done) { \
  391. result = -1; \
  392. break; \
  393. } else \
  394. cvmx_wait(100); \
  395. } \
  396. } while (0); \
  397. result; })
  398. /*
  399. * This macro logically sets a single field in a CSR. It does the sequence
  400. * read, modify, and write
  401. */
  402. #define USB_SET_FIELD32(address, type, field, value) \
  403. do { \
  404. type c; \
  405. c.u32 = __cvmx_usb_read_csr32(usb, address); \
  406. c.s.field = value; \
  407. __cvmx_usb_write_csr32(usb, address, c.u32); \
  408. } while (0)
  409. /* Returns the IO address to push/pop stuff data from the FIFOs */
  410. #define USB_FIFO_ADDRESS(channel, usb_index) \
  411. (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
  412. /**
  413. * struct octeon_temp_buffer - a bounce buffer for USB transfers
  414. * @temp_buffer: the newly allocated temporary buffer (including meta-data)
  415. * @orig_buffer: the original buffer passed by the USB stack
  416. * @data: the newly allocated temporary buffer (excluding meta-data)
  417. *
  418. * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
  419. * the buffer is too short, we need to allocate a temporary one, and this struct
  420. * represents it.
  421. */
  422. struct octeon_temp_buffer {
  423. void *temp_buffer;
  424. void *orig_buffer;
  425. u8 data[0];
  426. };
  427. /**
  428. * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
  429. * (if needed)
  430. * @urb: URB.
  431. * @mem_flags: Memory allocation flags.
  432. *
  433. * This function allocates a temporary bounce buffer whenever it's needed
  434. * due to HW limitations.
  435. */
  436. static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  437. {
  438. struct octeon_temp_buffer *temp;
  439. if (urb->num_sgs || urb->sg ||
  440. (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
  441. !(urb->transfer_buffer_length % sizeof(u32)))
  442. return 0;
  443. temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
  444. sizeof(*temp), mem_flags);
  445. if (!temp)
  446. return -ENOMEM;
  447. temp->temp_buffer = temp;
  448. temp->orig_buffer = urb->transfer_buffer;
  449. if (usb_urb_dir_out(urb))
  450. memcpy(temp->data, urb->transfer_buffer,
  451. urb->transfer_buffer_length);
  452. urb->transfer_buffer = temp->data;
  453. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  454. return 0;
  455. }
  456. /**
  457. * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
  458. * @urb: URB.
  459. *
  460. * Frees a buffer allocated by octeon_alloc_temp_buffer().
  461. */
  462. static void octeon_free_temp_buffer(struct urb *urb)
  463. {
  464. struct octeon_temp_buffer *temp;
  465. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  466. return;
  467. temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
  468. data);
  469. if (usb_urb_dir_in(urb))
  470. memcpy(temp->orig_buffer, urb->transfer_buffer,
  471. urb->actual_length);
  472. urb->transfer_buffer = temp->orig_buffer;
  473. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  474. kfree(temp->temp_buffer);
  475. }
  476. /**
  477. * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
  478. * @hcd: USB HCD structure.
  479. * @urb: URB.
  480. * @mem_flags: Memory allocation flags.
  481. */
  482. static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  483. gfp_t mem_flags)
  484. {
  485. int ret;
  486. ret = octeon_alloc_temp_buffer(urb, mem_flags);
  487. if (ret)
  488. return ret;
  489. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  490. if (ret)
  491. octeon_free_temp_buffer(urb);
  492. return ret;
  493. }
  494. /**
  495. * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
  496. * @hcd: USB HCD structure.
  497. * @urb: URB.
  498. */
  499. static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  500. {
  501. usb_hcd_unmap_urb_for_dma(hcd, urb);
  502. octeon_free_temp_buffer(urb);
  503. }
  504. /**
  505. * Read a USB 32bit CSR. It performs the necessary address swizzle
  506. * for 32bit CSRs and logs the value in a readable format if
  507. * debugging is on.
  508. *
  509. * @usb: USB block this access is for
  510. * @address: 64bit address to read
  511. *
  512. * Returns: Result of the read
  513. */
  514. static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_state *usb,
  515. uint64_t address)
  516. {
  517. uint32_t result = cvmx_read64_uint32(address ^ 4);
  518. return result;
  519. }
  520. /**
  521. * Write a USB 32bit CSR. It performs the necessary address
  522. * swizzle for 32bit CSRs and logs the value in a readable format
  523. * if debugging is on.
  524. *
  525. * @usb: USB block this access is for
  526. * @address: 64bit address to write
  527. * @value: Value to write
  528. */
  529. static inline void __cvmx_usb_write_csr32(struct cvmx_usb_state *usb,
  530. uint64_t address, uint32_t value)
  531. {
  532. cvmx_write64_uint32(address ^ 4, value);
  533. cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  534. }
  535. /**
  536. * Read a USB 64bit CSR. It logs the value in a readable format if
  537. * debugging is on.
  538. *
  539. * @usb: USB block this access is for
  540. * @address: 64bit address to read
  541. *
  542. * Returns: Result of the read
  543. */
  544. static inline uint64_t __cvmx_usb_read_csr64(struct cvmx_usb_state *usb,
  545. uint64_t address)
  546. {
  547. uint64_t result = cvmx_read64_uint64(address);
  548. return result;
  549. }
  550. /**
  551. * Write a USB 64bit CSR. It logs the value in a readable format
  552. * if debugging is on.
  553. *
  554. * @usb: USB block this access is for
  555. * @address: 64bit address to write
  556. * @value: Value to write
  557. */
  558. static inline void __cvmx_usb_write_csr64(struct cvmx_usb_state *usb,
  559. uint64_t address, uint64_t value)
  560. {
  561. cvmx_write64_uint64(address, value);
  562. }
  563. /**
  564. * Return non zero if this pipe connects to a non HIGH speed
  565. * device through a high speed hub.
  566. *
  567. * @usb: USB block this access is for
  568. * @pipe: Pipe to check
  569. *
  570. * Returns: Non zero if we need to do split transactions
  571. */
  572. static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_state *usb,
  573. struct cvmx_usb_pipe *pipe)
  574. {
  575. return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
  576. usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
  577. }
  578. /**
  579. * Trivial utility function to return the correct PID for a pipe
  580. *
  581. * @pipe: pipe to check
  582. *
  583. * Returns: PID for pipe
  584. */
  585. static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
  586. {
  587. if (pipe->pid_toggle)
  588. return 2; /* Data1 */
  589. else
  590. return 0; /* Data0 */
  591. }
  592. /**
  593. * Initialize a USB port for use. This must be called before any
  594. * other access to the Octeon USB port is made. The port starts
  595. * off in the disabled state.
  596. *
  597. * @usb: Pointer to an empty struct cvmx_usb_state
  598. * that will be populated by the initialize call.
  599. * This structure is then passed to all other USB
  600. * functions.
  601. * @usb_port_number:
  602. * Which Octeon USB port to initialize.
  603. *
  604. * Returns: 0 or a negative error code.
  605. */
  606. static int cvmx_usb_initialize(struct cvmx_usb_state *usb,
  607. int usb_port_number,
  608. enum cvmx_usb_initialize_flags flags)
  609. {
  610. union cvmx_usbnx_clk_ctl usbn_clk_ctl;
  611. union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
  612. int i;
  613. /* At first allow 0-1 for the usb port number */
  614. if ((usb_port_number < 0) || (usb_port_number > 1))
  615. return -EINVAL;
  616. memset(usb, 0, sizeof(*usb));
  617. usb->init_flags = flags;
  618. /* Initialize the USB state structure */
  619. usb->index = usb_port_number;
  620. INIT_LIST_HEAD(&usb->idle_pipes);
  621. for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
  622. INIT_LIST_HEAD(&usb->active_pipes[i]);
  623. /*
  624. * Power On Reset and PHY Initialization
  625. *
  626. * 1. Wait for DCOK to assert (nothing to do)
  627. *
  628. * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
  629. * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
  630. */
  631. usbn_clk_ctl.u64 =
  632. __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
  633. usbn_clk_ctl.s.por = 1;
  634. usbn_clk_ctl.s.hrst = 0;
  635. usbn_clk_ctl.s.prst = 0;
  636. usbn_clk_ctl.s.hclk_rst = 0;
  637. usbn_clk_ctl.s.enable = 0;
  638. /*
  639. * 2b. Select the USB reference clock/crystal parameters by writing
  640. * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
  641. */
  642. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
  643. /*
  644. * The USB port uses 12/24/48MHz 2.5V board clock
  645. * source at USB_XO. USB_XI should be tied to GND.
  646. * Most Octeon evaluation boards require this setting
  647. */
  648. if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
  649. OCTEON_IS_MODEL(OCTEON_CN56XX) ||
  650. OCTEON_IS_MODEL(OCTEON_CN50XX))
  651. /* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
  652. usbn_clk_ctl.s.p_rtype = 2; /* p_rclk=1 & p_xenbn=0 */
  653. else
  654. /* From CN52XX manual */
  655. usbn_clk_ctl.s.p_rtype = 1;
  656. switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
  657. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
  658. usbn_clk_ctl.s.p_c_sel = 0;
  659. break;
  660. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
  661. usbn_clk_ctl.s.p_c_sel = 1;
  662. break;
  663. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
  664. usbn_clk_ctl.s.p_c_sel = 2;
  665. break;
  666. }
  667. } else {
  668. /*
  669. * The USB port uses a 12MHz crystal as clock source
  670. * at USB_XO and USB_XI
  671. */
  672. if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
  673. /* From CN31XX,CN30XX manual */
  674. usbn_clk_ctl.s.p_rtype = 3; /* p_rclk=1 & p_xenbn=1 */
  675. else
  676. /* From CN56XX,CN52XX,CN50XX manuals. */
  677. usbn_clk_ctl.s.p_rtype = 0;
  678. usbn_clk_ctl.s.p_c_sel = 0;
  679. }
  680. /*
  681. * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
  682. * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
  683. * such that USB is as close as possible to 125Mhz
  684. */
  685. {
  686. int divisor = (octeon_get_clock_rate()+125000000-1)/125000000;
  687. /* Lower than 4 doesn't seem to work properly */
  688. if (divisor < 4)
  689. divisor = 4;
  690. usbn_clk_ctl.s.divide = divisor;
  691. usbn_clk_ctl.s.divide2 = 0;
  692. }
  693. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  694. usbn_clk_ctl.u64);
  695. /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
  696. usbn_clk_ctl.s.hclk_rst = 1;
  697. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  698. usbn_clk_ctl.u64);
  699. /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
  700. cvmx_wait(64);
  701. /*
  702. * 3. Program the power-on reset field in the USBN clock-control
  703. * register:
  704. * USBN_CLK_CTL[POR] = 0
  705. */
  706. usbn_clk_ctl.s.por = 0;
  707. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  708. usbn_clk_ctl.u64);
  709. /* 4. Wait 1 ms for PHY clock to start */
  710. mdelay(1);
  711. /*
  712. * 5. Program the Reset input from automatic test equipment field in the
  713. * USBP control and status register:
  714. * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
  715. */
  716. usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb,
  717. CVMX_USBNX_USBP_CTL_STATUS(usb->index));
  718. usbn_usbp_ctl_status.s.ate_reset = 1;
  719. __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  720. usbn_usbp_ctl_status.u64);
  721. /* 6. Wait 10 cycles */
  722. cvmx_wait(10);
  723. /*
  724. * 7. Clear ATE_RESET field in the USBN clock-control register:
  725. * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
  726. */
  727. usbn_usbp_ctl_status.s.ate_reset = 0;
  728. __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  729. usbn_usbp_ctl_status.u64);
  730. /*
  731. * 8. Program the PHY reset field in the USBN clock-control register:
  732. * USBN_CLK_CTL[PRST] = 1
  733. */
  734. usbn_clk_ctl.s.prst = 1;
  735. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  736. usbn_clk_ctl.u64);
  737. /*
  738. * 9. Program the USBP control and status register to select host or
  739. * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
  740. * device
  741. */
  742. usbn_usbp_ctl_status.s.hst_mode = 0;
  743. __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  744. usbn_usbp_ctl_status.u64);
  745. /* 10. Wait 1 us */
  746. udelay(1);
  747. /*
  748. * 11. Program the hreset_n field in the USBN clock-control register:
  749. * USBN_CLK_CTL[HRST] = 1
  750. */
  751. usbn_clk_ctl.s.hrst = 1;
  752. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  753. usbn_clk_ctl.u64);
  754. /* 12. Proceed to USB core initialization */
  755. usbn_clk_ctl.s.enable = 1;
  756. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  757. usbn_clk_ctl.u64);
  758. udelay(1);
  759. /*
  760. * USB Core Initialization
  761. *
  762. * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
  763. * determine USB core configuration parameters.
  764. *
  765. * Nothing needed
  766. *
  767. * 2. Program the following fields in the global AHB configuration
  768. * register (USBC_GAHBCFG)
  769. * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
  770. * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
  771. * Nonperiodic TxFIFO empty level (slave mode only),
  772. * USBC_GAHBCFG[NPTXFEMPLVL]
  773. * Periodic TxFIFO empty level (slave mode only),
  774. * USBC_GAHBCFG[PTXFEMPLVL]
  775. * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
  776. */
  777. {
  778. union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
  779. /* Due to an errata, CN31XX doesn't support DMA */
  780. if (OCTEON_IS_MODEL(OCTEON_CN31XX))
  781. usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
  782. usbcx_gahbcfg.u32 = 0;
  783. usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
  784. CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
  785. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  786. /* Only use one channel with non DMA */
  787. usb->idle_hardware_channels = 0x1;
  788. else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
  789. /* CN5XXX have an errata with channel 3 */
  790. usb->idle_hardware_channels = 0xf7;
  791. else
  792. usb->idle_hardware_channels = 0xff;
  793. usbcx_gahbcfg.s.hbstlen = 0;
  794. usbcx_gahbcfg.s.nptxfemplvl = 1;
  795. usbcx_gahbcfg.s.ptxfemplvl = 1;
  796. usbcx_gahbcfg.s.glblintrmsk = 1;
  797. __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
  798. usbcx_gahbcfg.u32);
  799. }
  800. /*
  801. * 3. Program the following fields in USBC_GUSBCFG register.
  802. * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
  803. * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
  804. * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
  805. * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
  806. */
  807. {
  808. union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
  809. usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb,
  810. CVMX_USBCX_GUSBCFG(usb->index));
  811. usbcx_gusbcfg.s.toutcal = 0;
  812. usbcx_gusbcfg.s.ddrsel = 0;
  813. usbcx_gusbcfg.s.usbtrdtim = 0x5;
  814. usbcx_gusbcfg.s.phylpwrclksel = 0;
  815. __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
  816. usbcx_gusbcfg.u32);
  817. }
  818. /*
  819. * 4. The software must unmask the following bits in the USBC_GINTMSK
  820. * register.
  821. * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
  822. * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
  823. */
  824. {
  825. union cvmx_usbcx_gintmsk usbcx_gintmsk;
  826. int channel;
  827. usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb,
  828. CVMX_USBCX_GINTMSK(usb->index));
  829. usbcx_gintmsk.s.otgintmsk = 1;
  830. usbcx_gintmsk.s.modemismsk = 1;
  831. usbcx_gintmsk.s.hchintmsk = 1;
  832. usbcx_gintmsk.s.sofmsk = 0;
  833. /* We need RX FIFO interrupts if we don't have DMA */
  834. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  835. usbcx_gintmsk.s.rxflvlmsk = 1;
  836. __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
  837. usbcx_gintmsk.u32);
  838. /*
  839. * Disable all channel interrupts. We'll enable them per channel
  840. * later.
  841. */
  842. for (channel = 0; channel < 8; channel++)
  843. __cvmx_usb_write_csr32(usb,
  844. CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
  845. }
  846. {
  847. /*
  848. * Host Port Initialization
  849. *
  850. * 1. Program the host-port interrupt-mask field to unmask,
  851. * USBC_GINTMSK[PRTINT] = 1
  852. */
  853. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  854. union cvmx_usbcx_gintmsk, prtintmsk, 1);
  855. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  856. union cvmx_usbcx_gintmsk, disconnintmsk, 1);
  857. /*
  858. * 2. Program the USBC_HCFG register to select full-speed host
  859. * or high-speed host.
  860. */
  861. {
  862. union cvmx_usbcx_hcfg usbcx_hcfg;
  863. usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb,
  864. CVMX_USBCX_HCFG(usb->index));
  865. usbcx_hcfg.s.fslssupp = 0;
  866. usbcx_hcfg.s.fslspclksel = 0;
  867. __cvmx_usb_write_csr32(usb,
  868. CVMX_USBCX_HCFG(usb->index),
  869. usbcx_hcfg.u32);
  870. }
  871. /*
  872. * 3. Program the port power bit to drive VBUS on the USB,
  873. * USBC_HPRT[PRTPWR] = 1
  874. */
  875. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
  876. union cvmx_usbcx_hprt, prtpwr, 1);
  877. /*
  878. * Steps 4-15 from the manual are done later in the port enable
  879. */
  880. }
  881. return 0;
  882. }
  883. /**
  884. * Shutdown a USB port after a call to cvmx_usb_initialize().
  885. * The port should be disabled with all pipes closed when this
  886. * function is called.
  887. *
  888. * @usb: USB device state populated by cvmx_usb_initialize().
  889. *
  890. * Returns: 0 or a negative error code.
  891. */
  892. static int cvmx_usb_shutdown(struct cvmx_usb_state *usb)
  893. {
  894. union cvmx_usbnx_clk_ctl usbn_clk_ctl;
  895. /* Make sure all pipes are closed */
  896. if (!list_empty(&usb->idle_pipes) ||
  897. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
  898. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
  899. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
  900. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
  901. return -EBUSY;
  902. /* Disable the clocks and put them in power on reset */
  903. usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb,
  904. CVMX_USBNX_CLK_CTL(usb->index));
  905. usbn_clk_ctl.s.enable = 1;
  906. usbn_clk_ctl.s.por = 1;
  907. usbn_clk_ctl.s.hclk_rst = 1;
  908. usbn_clk_ctl.s.prst = 0;
  909. usbn_clk_ctl.s.hrst = 0;
  910. __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
  911. usbn_clk_ctl.u64);
  912. return 0;
  913. }
  914. /**
  915. * Enable a USB port. After this call succeeds, the USB port is
  916. * online and servicing requests.
  917. *
  918. * @usb: USB device state populated by cvmx_usb_initialize().
  919. *
  920. * Returns: 0 or a negative error code.
  921. */
  922. static int cvmx_usb_enable(struct cvmx_usb_state *usb)
  923. {
  924. union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
  925. usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
  926. CVMX_USBCX_HPRT(usb->index));
  927. /*
  928. * If the port is already enabled the just return. We don't need to do
  929. * anything
  930. */
  931. if (usb->usbcx_hprt.s.prtena)
  932. return 0;
  933. /* If there is nothing plugged into the port then fail immediately */
  934. if (!usb->usbcx_hprt.s.prtconnsts)
  935. return -ETIMEDOUT;
  936. /* Program the port reset bit to start the reset process */
  937. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
  938. prtrst, 1);
  939. /*
  940. * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
  941. * process to complete.
  942. */
  943. mdelay(50);
  944. /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
  945. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
  946. prtrst, 0);
  947. /* Wait for the USBC_HPRT[PRTENA]. */
  948. if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index),
  949. union cvmx_usbcx_hprt, prtena, ==, 1, 100000))
  950. return -ETIMEDOUT;
  951. /*
  952. * Read the port speed field to get the enumerated speed,
  953. * USBC_HPRT[PRTSPD].
  954. */
  955. usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
  956. CVMX_USBCX_HPRT(usb->index));
  957. usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb,
  958. CVMX_USBCX_GHWCFG3(usb->index));
  959. /*
  960. * 13. Program the USBC_GRXFSIZ register to select the size of the
  961. * receive FIFO (25%).
  962. */
  963. USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index),
  964. union cvmx_usbcx_grxfsiz, rxfdep,
  965. usbcx_ghwcfg3.s.dfifodepth / 4);
  966. /*
  967. * 14. Program the USBC_GNPTXFSIZ register to select the size and the
  968. * start address of the non- periodic transmit FIFO for nonperiodic
  969. * transactions (50%).
  970. */
  971. {
  972. union cvmx_usbcx_gnptxfsiz siz;
  973. siz.u32 = __cvmx_usb_read_csr32(usb,
  974. CVMX_USBCX_GNPTXFSIZ(usb->index));
  975. siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
  976. siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
  977. __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index),
  978. siz.u32);
  979. }
  980. /*
  981. * 15. Program the USBC_HPTXFSIZ register to select the size and start
  982. * address of the periodic transmit FIFO for periodic transactions
  983. * (25%).
  984. */
  985. {
  986. union cvmx_usbcx_hptxfsiz siz;
  987. siz.u32 = __cvmx_usb_read_csr32(usb,
  988. CVMX_USBCX_HPTXFSIZ(usb->index));
  989. siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
  990. siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
  991. __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index),
  992. siz.u32);
  993. }
  994. /* Flush all FIFOs */
  995. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  996. union cvmx_usbcx_grstctl, txfnum, 0x10);
  997. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  998. union cvmx_usbcx_grstctl, txfflsh, 1);
  999. CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  1000. union cvmx_usbcx_grstctl,
  1001. txfflsh, ==, 0, 100);
  1002. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  1003. union cvmx_usbcx_grstctl, rxfflsh, 1);
  1004. CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  1005. union cvmx_usbcx_grstctl,
  1006. rxfflsh, ==, 0, 100);
  1007. return 0;
  1008. }
  1009. /**
  1010. * Disable a USB port. After this call the USB port will not
  1011. * generate data transfers and will not generate events.
  1012. * Transactions in process will fail and call their
  1013. * associated callbacks.
  1014. *
  1015. * @usb: USB device state populated by cvmx_usb_initialize().
  1016. *
  1017. * Returns: 0 or a negative error code.
  1018. */
  1019. static int cvmx_usb_disable(struct cvmx_usb_state *usb)
  1020. {
  1021. /* Disable the port */
  1022. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
  1023. prtena, 1);
  1024. return 0;
  1025. }
  1026. /**
  1027. * Get the current state of the USB port. Use this call to
  1028. * determine if the usb port has anything connected, is enabled,
  1029. * or has some sort of error condition. The return value of this
  1030. * call has "changed" bits to signal of the value of some fields
  1031. * have changed between calls.
  1032. *
  1033. * @usb: USB device state populated by cvmx_usb_initialize().
  1034. *
  1035. * Returns: Port status information
  1036. */
  1037. static struct cvmx_usb_port_status cvmx_usb_get_status(
  1038. struct cvmx_usb_state *usb)
  1039. {
  1040. union cvmx_usbcx_hprt usbc_hprt;
  1041. struct cvmx_usb_port_status result;
  1042. memset(&result, 0, sizeof(result));
  1043. usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
  1044. CVMX_USBCX_HPRT(usb->index));
  1045. result.port_enabled = usbc_hprt.s.prtena;
  1046. result.port_over_current = usbc_hprt.s.prtovrcurract;
  1047. result.port_powered = usbc_hprt.s.prtpwr;
  1048. result.port_speed = usbc_hprt.s.prtspd;
  1049. result.connected = usbc_hprt.s.prtconnsts;
  1050. result.connect_change =
  1051. (result.connected != usb->port_status.connected);
  1052. return result;
  1053. }
  1054. /**
  1055. * Open a virtual pipe between the host and a USB device. A pipe
  1056. * must be opened before data can be transferred between a device
  1057. * and Octeon.
  1058. *
  1059. * @usb: USB device state populated by cvmx_usb_initialize().
  1060. * @device_addr:
  1061. * USB device address to open the pipe to
  1062. * (0-127).
  1063. * @endpoint_num:
  1064. * USB endpoint number to open the pipe to
  1065. * (0-15).
  1066. * @device_speed:
  1067. * The speed of the device the pipe is going
  1068. * to. This must match the device's speed,
  1069. * which may be different than the port speed.
  1070. * @max_packet: The maximum packet length the device can
  1071. * transmit/receive (low speed=0-8, full
  1072. * speed=0-1023, high speed=0-1024). This value
  1073. * comes from the standard endpoint descriptor
  1074. * field wMaxPacketSize bits <10:0>.
  1075. * @transfer_type:
  1076. * The type of transfer this pipe is for.
  1077. * @transfer_dir:
  1078. * The direction the pipe is in. This is not
  1079. * used for control pipes.
  1080. * @interval: For ISOCHRONOUS and INTERRUPT transfers,
  1081. * this is how often the transfer is scheduled
  1082. * for. All other transfers should specify
  1083. * zero. The units are in frames (8000/sec at
  1084. * high speed, 1000/sec for full speed).
  1085. * @multi_count:
  1086. * For high speed devices, this is the maximum
  1087. * allowed number of packet per microframe.
  1088. * Specify zero for non high speed devices. This
  1089. * value comes from the standard endpoint descriptor
  1090. * field wMaxPacketSize bits <12:11>.
  1091. * @hub_device_addr:
  1092. * Hub device address this device is connected
  1093. * to. Devices connected directly to Octeon
  1094. * use zero. This is only used when the device
  1095. * is full/low speed behind a high speed hub.
  1096. * The address will be of the high speed hub,
  1097. * not and full speed hubs after it.
  1098. * @hub_port: Which port on the hub the device is
  1099. * connected. Use zero for devices connected
  1100. * directly to Octeon. Like hub_device_addr,
  1101. * this is only used for full/low speed
  1102. * devices behind a high speed hub.
  1103. *
  1104. * Returns: A non-NULL value is a pipe. NULL means an error.
  1105. */
  1106. static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct cvmx_usb_state *usb,
  1107. int device_addr,
  1108. int endpoint_num,
  1109. enum cvmx_usb_speed
  1110. device_speed,
  1111. int max_packet,
  1112. enum cvmx_usb_transfer
  1113. transfer_type,
  1114. enum cvmx_usb_direction
  1115. transfer_dir,
  1116. int interval, int multi_count,
  1117. int hub_device_addr,
  1118. int hub_port)
  1119. {
  1120. struct cvmx_usb_pipe *pipe;
  1121. if (unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
  1122. return NULL;
  1123. if (unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
  1124. return NULL;
  1125. if (unlikely(device_speed > CVMX_USB_SPEED_LOW))
  1126. return NULL;
  1127. if (unlikely((max_packet <= 0) || (max_packet > 1024)))
  1128. return NULL;
  1129. if (unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
  1130. return NULL;
  1131. if (unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
  1132. (transfer_dir != CVMX_USB_DIRECTION_IN)))
  1133. return NULL;
  1134. if (unlikely(interval < 0))
  1135. return NULL;
  1136. if (unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
  1137. return NULL;
  1138. if (unlikely(multi_count < 0))
  1139. return NULL;
  1140. if (unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
  1141. (multi_count != 0)))
  1142. return NULL;
  1143. if (unlikely((hub_device_addr < 0) ||
  1144. (hub_device_addr > MAX_USB_ADDRESS)))
  1145. return NULL;
  1146. if (unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
  1147. return NULL;
  1148. pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
  1149. if (!pipe)
  1150. return NULL;
  1151. if ((device_speed == CVMX_USB_SPEED_HIGH) &&
  1152. (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
  1153. (transfer_type == CVMX_USB_TRANSFER_BULK))
  1154. pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
  1155. pipe->device_addr = device_addr;
  1156. pipe->endpoint_num = endpoint_num;
  1157. pipe->device_speed = device_speed;
  1158. pipe->max_packet = max_packet;
  1159. pipe->transfer_type = transfer_type;
  1160. pipe->transfer_dir = transfer_dir;
  1161. INIT_LIST_HEAD(&pipe->transactions);
  1162. /*
  1163. * All pipes use interval to rate limit NAK processing. Force an
  1164. * interval if one wasn't supplied
  1165. */
  1166. if (!interval)
  1167. interval = 1;
  1168. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  1169. pipe->interval = interval*8;
  1170. /* Force start splits to be schedule on uFrame 0 */
  1171. pipe->next_tx_frame = ((usb->frame_number+7)&~7) +
  1172. pipe->interval;
  1173. } else {
  1174. pipe->interval = interval;
  1175. pipe->next_tx_frame = usb->frame_number + pipe->interval;
  1176. }
  1177. pipe->multi_count = multi_count;
  1178. pipe->hub_device_addr = hub_device_addr;
  1179. pipe->hub_port = hub_port;
  1180. pipe->pid_toggle = 0;
  1181. pipe->split_sc_frame = -1;
  1182. list_add_tail(&pipe->node, &usb->idle_pipes);
  1183. /*
  1184. * We don't need to tell the hardware about this pipe yet since
  1185. * it doesn't have any submitted requests
  1186. */
  1187. return pipe;
  1188. }
  1189. /**
  1190. * Poll the RX FIFOs and remove data as needed. This function is only used
  1191. * in non DMA mode. It is very important that this function be called quickly
  1192. * enough to prevent FIFO overflow.
  1193. *
  1194. * @usb: USB device state populated by cvmx_usb_initialize().
  1195. */
  1196. static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_state *usb)
  1197. {
  1198. union cvmx_usbcx_grxstsph rx_status;
  1199. int channel;
  1200. int bytes;
  1201. uint64_t address;
  1202. uint32_t *ptr;
  1203. rx_status.u32 = __cvmx_usb_read_csr32(usb,
  1204. CVMX_USBCX_GRXSTSPH(usb->index));
  1205. /* Only read data if IN data is there */
  1206. if (rx_status.s.pktsts != 2)
  1207. return;
  1208. /* Check if no data is available */
  1209. if (!rx_status.s.bcnt)
  1210. return;
  1211. channel = rx_status.s.chnum;
  1212. bytes = rx_status.s.bcnt;
  1213. if (!bytes)
  1214. return;
  1215. /* Get where the DMA engine would have written this data */
  1216. address = __cvmx_usb_read_csr64(usb,
  1217. CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
  1218. ptr = cvmx_phys_to_ptr(address);
  1219. __cvmx_usb_write_csr64(usb,
  1220. CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8,
  1221. address + bytes);
  1222. /* Loop writing the FIFO data for this packet into memory */
  1223. while (bytes > 0) {
  1224. *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
  1225. bytes -= 4;
  1226. }
  1227. CVMX_SYNCW;
  1228. }
  1229. /**
  1230. * Fill the TX hardware fifo with data out of the software
  1231. * fifos
  1232. *
  1233. * @usb: USB device state populated by cvmx_usb_initialize().
  1234. * @fifo: Software fifo to use
  1235. * @available: Amount of space in the hardware fifo
  1236. *
  1237. * Returns: Non zero if the hardware fifo was too small and needs
  1238. * to be serviced again.
  1239. */
  1240. static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_state *usb,
  1241. struct cvmx_usb_tx_fifo *fifo, int available)
  1242. {
  1243. /*
  1244. * We're done either when there isn't anymore space or the software FIFO
  1245. * is empty
  1246. */
  1247. while (available && (fifo->head != fifo->tail)) {
  1248. int i = fifo->tail;
  1249. const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
  1250. uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
  1251. usb->index) ^ 4;
  1252. int words = available;
  1253. /* Limit the amount of data to waht the SW fifo has */
  1254. if (fifo->entry[i].size <= available) {
  1255. words = fifo->entry[i].size;
  1256. fifo->tail++;
  1257. if (fifo->tail > MAX_CHANNELS)
  1258. fifo->tail = 0;
  1259. }
  1260. /* Update the next locations and counts */
  1261. available -= words;
  1262. fifo->entry[i].address += words * 4;
  1263. fifo->entry[i].size -= words;
  1264. /*
  1265. * Write the HW fifo data. The read every three writes is due
  1266. * to an errata on CN3XXX chips
  1267. */
  1268. while (words > 3) {
  1269. cvmx_write64_uint32(csr_address, *ptr++);
  1270. cvmx_write64_uint32(csr_address, *ptr++);
  1271. cvmx_write64_uint32(csr_address, *ptr++);
  1272. cvmx_read64_uint64(
  1273. CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  1274. words -= 3;
  1275. }
  1276. cvmx_write64_uint32(csr_address, *ptr++);
  1277. if (--words) {
  1278. cvmx_write64_uint32(csr_address, *ptr++);
  1279. if (--words)
  1280. cvmx_write64_uint32(csr_address, *ptr++);
  1281. }
  1282. cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  1283. }
  1284. return fifo->head != fifo->tail;
  1285. }
  1286. /**
  1287. * Check the hardware FIFOs and fill them as needed
  1288. *
  1289. * @usb: USB device state populated by cvmx_usb_initialize().
  1290. */
  1291. static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_state *usb)
  1292. {
  1293. if (usb->periodic.head != usb->periodic.tail) {
  1294. union cvmx_usbcx_hptxsts tx_status;
  1295. tx_status.u32 = __cvmx_usb_read_csr32(usb,
  1296. CVMX_USBCX_HPTXSTS(usb->index));
  1297. if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic,
  1298. tx_status.s.ptxfspcavail))
  1299. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1300. union cvmx_usbcx_gintmsk,
  1301. ptxfempmsk, 1);
  1302. else
  1303. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1304. union cvmx_usbcx_gintmsk,
  1305. ptxfempmsk, 0);
  1306. }
  1307. if (usb->nonperiodic.head != usb->nonperiodic.tail) {
  1308. union cvmx_usbcx_gnptxsts tx_status;
  1309. tx_status.u32 = __cvmx_usb_read_csr32(usb,
  1310. CVMX_USBCX_GNPTXSTS(usb->index));
  1311. if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
  1312. tx_status.s.nptxfspcavail))
  1313. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1314. union cvmx_usbcx_gintmsk,
  1315. nptxfempmsk, 1);
  1316. else
  1317. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1318. union cvmx_usbcx_gintmsk,
  1319. nptxfempmsk, 0);
  1320. }
  1321. }
  1322. /**
  1323. * Fill the TX FIFO with an outgoing packet
  1324. *
  1325. * @usb: USB device state populated by cvmx_usb_initialize().
  1326. * @channel: Channel number to get packet from
  1327. */
  1328. static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_state *usb, int channel)
  1329. {
  1330. union cvmx_usbcx_hccharx hcchar;
  1331. union cvmx_usbcx_hcspltx usbc_hcsplt;
  1332. union cvmx_usbcx_hctsizx usbc_hctsiz;
  1333. struct cvmx_usb_tx_fifo *fifo;
  1334. /* We only need to fill data on outbound channels */
  1335. hcchar.u32 = __cvmx_usb_read_csr32(usb,
  1336. CVMX_USBCX_HCCHARX(channel, usb->index));
  1337. if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
  1338. return;
  1339. /* OUT Splits only have data on the start and not the complete */
  1340. usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb,
  1341. CVMX_USBCX_HCSPLTX(channel, usb->index));
  1342. if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
  1343. return;
  1344. /*
  1345. * Find out how many bytes we need to fill and convert it into 32bit
  1346. * words.
  1347. */
  1348. usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
  1349. CVMX_USBCX_HCTSIZX(channel, usb->index));
  1350. if (!usbc_hctsiz.s.xfersize)
  1351. return;
  1352. if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
  1353. (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
  1354. fifo = &usb->periodic;
  1355. else
  1356. fifo = &usb->nonperiodic;
  1357. fifo->entry[fifo->head].channel = channel;
  1358. fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
  1359. fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
  1360. fifo->head++;
  1361. if (fifo->head > MAX_CHANNELS)
  1362. fifo->head = 0;
  1363. __cvmx_usb_poll_tx_fifo(usb);
  1364. }
  1365. /**
  1366. * Perform channel specific setup for Control transactions. All
  1367. * the generic stuff will already have been done in
  1368. * __cvmx_usb_start_channel()
  1369. *
  1370. * @usb: USB device state populated by cvmx_usb_initialize().
  1371. * @channel: Channel to setup
  1372. * @pipe: Pipe for control transaction
  1373. */
  1374. static void __cvmx_usb_start_channel_control(struct cvmx_usb_state *usb,
  1375. int channel,
  1376. struct cvmx_usb_pipe *pipe)
  1377. {
  1378. struct cvmx_usb_transaction *transaction =
  1379. list_first_entry(&pipe->transactions, typeof(*transaction),
  1380. node);
  1381. struct usb_ctrlrequest *header =
  1382. cvmx_phys_to_ptr(transaction->control_header);
  1383. int bytes_to_transfer = transaction->buffer_length -
  1384. transaction->actual_bytes;
  1385. int packets_to_transfer;
  1386. union cvmx_usbcx_hctsizx usbc_hctsiz;
  1387. usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
  1388. CVMX_USBCX_HCTSIZX(channel, usb->index));
  1389. switch (transaction->stage) {
  1390. case CVMX_USB_STAGE_NON_CONTROL:
  1391. case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
  1392. cvmx_dprintf("%s: ERROR - Non control stage\n", __func__);
  1393. break;
  1394. case CVMX_USB_STAGE_SETUP:
  1395. usbc_hctsiz.s.pid = 3; /* Setup */
  1396. bytes_to_transfer = sizeof(*header);
  1397. /* All Control operations start with a setup going OUT */
  1398. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1399. union cvmx_usbcx_hccharx, epdir,
  1400. CVMX_USB_DIRECTION_OUT);
  1401. /*
  1402. * Setup send the control header instead of the buffer data. The
  1403. * buffer data will be used in the next stage
  1404. */
  1405. __cvmx_usb_write_csr64(usb,
  1406. CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8,
  1407. transaction->control_header);
  1408. break;
  1409. case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
  1410. usbc_hctsiz.s.pid = 3; /* Setup */
  1411. bytes_to_transfer = 0;
  1412. /* All Control operations start with a setup going OUT */
  1413. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1414. union cvmx_usbcx_hccharx, epdir,
  1415. CVMX_USB_DIRECTION_OUT);
  1416. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1417. union cvmx_usbcx_hcspltx, compsplt, 1);
  1418. break;
  1419. case CVMX_USB_STAGE_DATA:
  1420. usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
  1421. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  1422. if (header->bRequestType & USB_DIR_IN)
  1423. bytes_to_transfer = 0;
  1424. else if (bytes_to_transfer > pipe->max_packet)
  1425. bytes_to_transfer = pipe->max_packet;
  1426. }
  1427. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1428. union cvmx_usbcx_hccharx, epdir,
  1429. ((header->bRequestType & USB_DIR_IN) ?
  1430. CVMX_USB_DIRECTION_IN :
  1431. CVMX_USB_DIRECTION_OUT));
  1432. break;
  1433. case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
  1434. usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
  1435. if (!(header->bRequestType & USB_DIR_IN))
  1436. bytes_to_transfer = 0;
  1437. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1438. union cvmx_usbcx_hccharx, epdir,
  1439. ((header->bRequestType & USB_DIR_IN) ?
  1440. CVMX_USB_DIRECTION_IN :
  1441. CVMX_USB_DIRECTION_OUT));
  1442. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1443. union cvmx_usbcx_hcspltx, compsplt, 1);
  1444. break;
  1445. case CVMX_USB_STAGE_STATUS:
  1446. usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
  1447. bytes_to_transfer = 0;
  1448. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1449. union cvmx_usbcx_hccharx, epdir,
  1450. ((header->bRequestType & USB_DIR_IN) ?
  1451. CVMX_USB_DIRECTION_OUT :
  1452. CVMX_USB_DIRECTION_IN));
  1453. break;
  1454. case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
  1455. usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
  1456. bytes_to_transfer = 0;
  1457. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1458. union cvmx_usbcx_hccharx, epdir,
  1459. ((header->bRequestType & USB_DIR_IN) ?
  1460. CVMX_USB_DIRECTION_OUT :
  1461. CVMX_USB_DIRECTION_IN));
  1462. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1463. union cvmx_usbcx_hcspltx, compsplt, 1);
  1464. break;
  1465. }
  1466. /*
  1467. * Make sure the transfer never exceeds the byte limit of the hardware.
  1468. * Further bytes will be sent as continued transactions
  1469. */
  1470. if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
  1471. /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
  1472. bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
  1473. bytes_to_transfer *= pipe->max_packet;
  1474. }
  1475. /*
  1476. * Calculate the number of packets to transfer. If the length is zero
  1477. * we still need to transfer one packet
  1478. */
  1479. packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) /
  1480. pipe->max_packet;
  1481. if (packets_to_transfer == 0)
  1482. packets_to_transfer = 1;
  1483. else if ((packets_to_transfer > 1) &&
  1484. (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
  1485. /*
  1486. * Limit to one packet when not using DMA. Channels must be
  1487. * restarted between every packet for IN transactions, so there
  1488. * is no reason to do multiple packets in a row
  1489. */
  1490. packets_to_transfer = 1;
  1491. bytes_to_transfer = packets_to_transfer * pipe->max_packet;
  1492. } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
  1493. /*
  1494. * Limit the number of packet and data transferred to what the
  1495. * hardware can handle
  1496. */
  1497. packets_to_transfer = MAX_TRANSFER_PACKETS;
  1498. bytes_to_transfer = packets_to_transfer * pipe->max_packet;
  1499. }
  1500. usbc_hctsiz.s.xfersize = bytes_to_transfer;
  1501. usbc_hctsiz.s.pktcnt = packets_to_transfer;
  1502. __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
  1503. usbc_hctsiz.u32);
  1504. }
  1505. /**
  1506. * Start a channel to perform the pipe's head transaction
  1507. *
  1508. * @usb: USB device state populated by cvmx_usb_initialize().
  1509. * @channel: Channel to setup
  1510. * @pipe: Pipe to start
  1511. */
  1512. static void __cvmx_usb_start_channel(struct cvmx_usb_state *usb,
  1513. int channel,
  1514. struct cvmx_usb_pipe *pipe)
  1515. {
  1516. struct cvmx_usb_transaction *transaction =
  1517. list_first_entry(&pipe->transactions, typeof(*transaction),
  1518. node);
  1519. /* Make sure all writes to the DMA region get flushed */
  1520. CVMX_SYNCW;
  1521. /* Attach the channel to the pipe */
  1522. usb->pipe_for_channel[channel] = pipe;
  1523. pipe->channel = channel;
  1524. pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
  1525. /* Mark this channel as in use */
  1526. usb->idle_hardware_channels &= ~(1<<channel);
  1527. /* Enable the channel interrupt bits */
  1528. {
  1529. union cvmx_usbcx_hcintx usbc_hcint;
  1530. union cvmx_usbcx_hcintmskx usbc_hcintmsk;
  1531. union cvmx_usbcx_haintmsk usbc_haintmsk;
  1532. /* Clear all channel status bits */
  1533. usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
  1534. CVMX_USBCX_HCINTX(channel, usb->index));
  1535. __cvmx_usb_write_csr32(usb,
  1536. CVMX_USBCX_HCINTX(channel, usb->index),
  1537. usbc_hcint.u32);
  1538. usbc_hcintmsk.u32 = 0;
  1539. usbc_hcintmsk.s.chhltdmsk = 1;
  1540. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
  1541. /*
  1542. * Channels need these extra interrupts when we aren't
  1543. * in DMA mode.
  1544. */
  1545. usbc_hcintmsk.s.datatglerrmsk = 1;
  1546. usbc_hcintmsk.s.frmovrunmsk = 1;
  1547. usbc_hcintmsk.s.bblerrmsk = 1;
  1548. usbc_hcintmsk.s.xacterrmsk = 1;
  1549. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  1550. /*
  1551. * Splits don't generate xfercompl, so we need
  1552. * ACK and NYET.
  1553. */
  1554. usbc_hcintmsk.s.nyetmsk = 1;
  1555. usbc_hcintmsk.s.ackmsk = 1;
  1556. }
  1557. usbc_hcintmsk.s.nakmsk = 1;
  1558. usbc_hcintmsk.s.stallmsk = 1;
  1559. usbc_hcintmsk.s.xfercomplmsk = 1;
  1560. }
  1561. __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
  1562. /* Enable the channel interrupt to propagate */
  1563. usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb,
  1564. CVMX_USBCX_HAINTMSK(usb->index));
  1565. usbc_haintmsk.s.haintmsk |= 1<<channel;
  1566. __cvmx_usb_write_csr32(usb,
  1567. CVMX_USBCX_HAINTMSK(usb->index),
  1568. usbc_haintmsk.u32);
  1569. }
  1570. /* Setup the locations the DMA engines use */
  1571. {
  1572. uint64_t dma_address = transaction->buffer +
  1573. transaction->actual_bytes;
  1574. if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
  1575. dma_address = transaction->buffer +
  1576. transaction->iso_packets[0].offset +
  1577. transaction->actual_bytes;
  1578. __cvmx_usb_write_csr64(usb,
  1579. CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8,
  1580. dma_address);
  1581. __cvmx_usb_write_csr64(usb,
  1582. CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8,
  1583. dma_address);
  1584. }
  1585. /* Setup both the size of the transfer and the SPLIT characteristics */
  1586. {
  1587. union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
  1588. union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
  1589. int packets_to_transfer;
  1590. int bytes_to_transfer = transaction->buffer_length -
  1591. transaction->actual_bytes;
  1592. /*
  1593. * ISOCHRONOUS transactions store each individual transfer size
  1594. * in the packet structure, not the global buffer_length
  1595. */
  1596. if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
  1597. bytes_to_transfer =
  1598. transaction->iso_packets[0].length -
  1599. transaction->actual_bytes;
  1600. /*
  1601. * We need to do split transactions when we are talking to non
  1602. * high speed devices that are behind a high speed hub
  1603. */
  1604. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  1605. /*
  1606. * On the start split phase (stage is even) record the
  1607. * frame number we will need to send the split complete.
  1608. * We only store the lower two bits since the time ahead
  1609. * can only be two frames
  1610. */
  1611. if ((transaction->stage&1) == 0) {
  1612. if (transaction->type == CVMX_USB_TRANSFER_BULK)
  1613. pipe->split_sc_frame =
  1614. (usb->frame_number + 1) & 0x7f;
  1615. else
  1616. pipe->split_sc_frame =
  1617. (usb->frame_number + 2) & 0x7f;
  1618. } else
  1619. pipe->split_sc_frame = -1;
  1620. usbc_hcsplt.s.spltena = 1;
  1621. usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
  1622. usbc_hcsplt.s.prtaddr = pipe->hub_port;
  1623. usbc_hcsplt.s.compsplt = (transaction->stage ==
  1624. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
  1625. /*
  1626. * SPLIT transactions can only ever transmit one data
  1627. * packet so limit the transfer size to the max packet
  1628. * size
  1629. */
  1630. if (bytes_to_transfer > pipe->max_packet)
  1631. bytes_to_transfer = pipe->max_packet;
  1632. /*
  1633. * ISOCHRONOUS OUT splits are unique in that they limit
  1634. * data transfers to 188 byte chunks representing the
  1635. * begin/middle/end of the data or all
  1636. */
  1637. if (!usbc_hcsplt.s.compsplt &&
  1638. (pipe->transfer_dir ==
  1639. CVMX_USB_DIRECTION_OUT) &&
  1640. (pipe->transfer_type ==
  1641. CVMX_USB_TRANSFER_ISOCHRONOUS)) {
  1642. /*
  1643. * Clear the split complete frame number as
  1644. * there isn't going to be a split complete
  1645. */
  1646. pipe->split_sc_frame = -1;
  1647. /*
  1648. * See if we've started this transfer and sent
  1649. * data
  1650. */
  1651. if (transaction->actual_bytes == 0) {
  1652. /*
  1653. * Nothing sent yet, this is either a
  1654. * begin or the entire payload
  1655. */
  1656. if (bytes_to_transfer <= 188)
  1657. /* Entire payload in one go */
  1658. usbc_hcsplt.s.xactpos = 3;
  1659. else
  1660. /* First part of payload */
  1661. usbc_hcsplt.s.xactpos = 2;
  1662. } else {
  1663. /*
  1664. * Continuing the previous data, we must
  1665. * either be in the middle or at the end
  1666. */
  1667. if (bytes_to_transfer <= 188)
  1668. /* End of payload */
  1669. usbc_hcsplt.s.xactpos = 1;
  1670. else
  1671. /* Middle of payload */
  1672. usbc_hcsplt.s.xactpos = 0;
  1673. }
  1674. /*
  1675. * Again, the transfer size is limited to 188
  1676. * bytes
  1677. */
  1678. if (bytes_to_transfer > 188)
  1679. bytes_to_transfer = 188;
  1680. }
  1681. }
  1682. /*
  1683. * Make sure the transfer never exceeds the byte limit of the
  1684. * hardware. Further bytes will be sent as continued
  1685. * transactions
  1686. */
  1687. if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
  1688. /*
  1689. * Round MAX_TRANSFER_BYTES to a multiple of out packet
  1690. * size
  1691. */
  1692. bytes_to_transfer = MAX_TRANSFER_BYTES /
  1693. pipe->max_packet;
  1694. bytes_to_transfer *= pipe->max_packet;
  1695. }
  1696. /*
  1697. * Calculate the number of packets to transfer. If the length is
  1698. * zero we still need to transfer one packet
  1699. */
  1700. packets_to_transfer =
  1701. (bytes_to_transfer + pipe->max_packet - 1) /
  1702. pipe->max_packet;
  1703. if (packets_to_transfer == 0)
  1704. packets_to_transfer = 1;
  1705. else if ((packets_to_transfer > 1) &&
  1706. (usb->init_flags &
  1707. CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
  1708. /*
  1709. * Limit to one packet when not using DMA. Channels must
  1710. * be restarted between every packet for IN
  1711. * transactions, so there is no reason to do multiple
  1712. * packets in a row
  1713. */
  1714. packets_to_transfer = 1;
  1715. bytes_to_transfer = packets_to_transfer *
  1716. pipe->max_packet;
  1717. } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
  1718. /*
  1719. * Limit the number of packet and data transferred to
  1720. * what the hardware can handle
  1721. */
  1722. packets_to_transfer = MAX_TRANSFER_PACKETS;
  1723. bytes_to_transfer = packets_to_transfer *
  1724. pipe->max_packet;
  1725. }
  1726. usbc_hctsiz.s.xfersize = bytes_to_transfer;
  1727. usbc_hctsiz.s.pktcnt = packets_to_transfer;
  1728. /* Update the DATA0/DATA1 toggle */
  1729. usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
  1730. /*
  1731. * High speed pipes may need a hardware ping before they start
  1732. */
  1733. if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
  1734. usbc_hctsiz.s.dopng = 1;
  1735. __cvmx_usb_write_csr32(usb,
  1736. CVMX_USBCX_HCSPLTX(channel, usb->index),
  1737. usbc_hcsplt.u32);
  1738. __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel,
  1739. usb->index), usbc_hctsiz.u32);
  1740. }
  1741. /* Setup the Host Channel Characteristics Register */
  1742. {
  1743. union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
  1744. /*
  1745. * Set the startframe odd/even properly. This is only used for
  1746. * periodic
  1747. */
  1748. usbc_hcchar.s.oddfrm = usb->frame_number&1;
  1749. /*
  1750. * Set the number of back to back packets allowed by this
  1751. * endpoint. Split transactions interpret "ec" as the number of
  1752. * immediate retries of failure. These retries happen too
  1753. * quickly, so we disable these entirely for splits
  1754. */
  1755. if (__cvmx_usb_pipe_needs_split(usb, pipe))
  1756. usbc_hcchar.s.ec = 1;
  1757. else if (pipe->multi_count < 1)
  1758. usbc_hcchar.s.ec = 1;
  1759. else if (pipe->multi_count > 3)
  1760. usbc_hcchar.s.ec = 3;
  1761. else
  1762. usbc_hcchar.s.ec = pipe->multi_count;
  1763. /* Set the rest of the endpoint specific settings */
  1764. usbc_hcchar.s.devaddr = pipe->device_addr;
  1765. usbc_hcchar.s.eptype = transaction->type;
  1766. usbc_hcchar.s.lspddev =
  1767. (pipe->device_speed == CVMX_USB_SPEED_LOW);
  1768. usbc_hcchar.s.epdir = pipe->transfer_dir;
  1769. usbc_hcchar.s.epnum = pipe->endpoint_num;
  1770. usbc_hcchar.s.mps = pipe->max_packet;
  1771. __cvmx_usb_write_csr32(usb,
  1772. CVMX_USBCX_HCCHARX(channel, usb->index),
  1773. usbc_hcchar.u32);
  1774. }
  1775. /* Do transaction type specific fixups as needed */
  1776. switch (transaction->type) {
  1777. case CVMX_USB_TRANSFER_CONTROL:
  1778. __cvmx_usb_start_channel_control(usb, channel, pipe);
  1779. break;
  1780. case CVMX_USB_TRANSFER_BULK:
  1781. case CVMX_USB_TRANSFER_INTERRUPT:
  1782. break;
  1783. case CVMX_USB_TRANSFER_ISOCHRONOUS:
  1784. if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
  1785. /*
  1786. * ISO transactions require different PIDs depending on
  1787. * direction and how many packets are needed
  1788. */
  1789. if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
  1790. if (pipe->multi_count < 2) /* Need DATA0 */
  1791. USB_SET_FIELD32(
  1792. CVMX_USBCX_HCTSIZX(channel,
  1793. usb->index),
  1794. union cvmx_usbcx_hctsizx,
  1795. pid, 0);
  1796. else /* Need MDATA */
  1797. USB_SET_FIELD32(
  1798. CVMX_USBCX_HCTSIZX(channel,
  1799. usb->index),
  1800. union cvmx_usbcx_hctsizx,
  1801. pid, 3);
  1802. }
  1803. }
  1804. break;
  1805. }
  1806. {
  1807. union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 =
  1808. __cvmx_usb_read_csr32(usb,
  1809. CVMX_USBCX_HCTSIZX(channel, usb->index))};
  1810. transaction->xfersize = usbc_hctsiz.s.xfersize;
  1811. transaction->pktcnt = usbc_hctsiz.s.pktcnt;
  1812. }
  1813. /* Remeber when we start a split transaction */
  1814. if (__cvmx_usb_pipe_needs_split(usb, pipe))
  1815. usb->active_split = transaction;
  1816. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1817. union cvmx_usbcx_hccharx, chena, 1);
  1818. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  1819. __cvmx_usb_fill_tx_fifo(usb, channel);
  1820. }
  1821. /**
  1822. * Find a pipe that is ready to be scheduled to hardware.
  1823. * @usb: USB device state populated by cvmx_usb_initialize().
  1824. * @list: Pipe list to search
  1825. * @current_frame:
  1826. * Frame counter to use as a time reference.
  1827. *
  1828. * Returns: Pipe or NULL if none are ready
  1829. */
  1830. static struct cvmx_usb_pipe *__cvmx_usb_find_ready_pipe(
  1831. struct cvmx_usb_state *usb,
  1832. struct list_head *list,
  1833. uint64_t current_frame)
  1834. {
  1835. struct cvmx_usb_pipe *pipe;
  1836. list_for_each_entry(pipe, list, node) {
  1837. struct cvmx_usb_transaction *t =
  1838. list_first_entry(&pipe->transactions, typeof(*t),
  1839. node);
  1840. if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
  1841. (pipe->next_tx_frame <= current_frame) &&
  1842. ((pipe->split_sc_frame == -1) ||
  1843. ((((int)current_frame - (int)pipe->split_sc_frame)
  1844. & 0x7f) < 0x40)) &&
  1845. (!usb->active_split || (usb->active_split == t))) {
  1846. prefetch(t);
  1847. return pipe;
  1848. }
  1849. }
  1850. return NULL;
  1851. }
  1852. /**
  1853. * Called whenever a pipe might need to be scheduled to the
  1854. * hardware.
  1855. *
  1856. * @usb: USB device state populated by cvmx_usb_initialize().
  1857. * @is_sof: True if this schedule was called on a SOF interrupt.
  1858. */
  1859. static void __cvmx_usb_schedule(struct cvmx_usb_state *usb, int is_sof)
  1860. {
  1861. int channel;
  1862. struct cvmx_usb_pipe *pipe;
  1863. int need_sof;
  1864. enum cvmx_usb_transfer ttype;
  1865. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
  1866. /*
  1867. * Without DMA we need to be careful to not schedule something
  1868. * at the end of a frame and cause an overrun.
  1869. */
  1870. union cvmx_usbcx_hfnum hfnum = {
  1871. .u32 = __cvmx_usb_read_csr32(usb,
  1872. CVMX_USBCX_HFNUM(usb->index))
  1873. };
  1874. union cvmx_usbcx_hfir hfir = {
  1875. .u32 = __cvmx_usb_read_csr32(usb,
  1876. CVMX_USBCX_HFIR(usb->index))
  1877. };
  1878. if (hfnum.s.frrem < hfir.s.frint/4)
  1879. goto done;
  1880. }
  1881. while (usb->idle_hardware_channels) {
  1882. /* Find an idle channel */
  1883. channel = __fls(usb->idle_hardware_channels);
  1884. if (unlikely(channel > 7))
  1885. break;
  1886. /* Find a pipe needing service */
  1887. pipe = NULL;
  1888. if (is_sof) {
  1889. /*
  1890. * Only process periodic pipes on SOF interrupts. This
  1891. * way we are sure that the periodic data is sent in the
  1892. * beginning of the frame
  1893. */
  1894. pipe = __cvmx_usb_find_ready_pipe(usb,
  1895. usb->active_pipes +
  1896. CVMX_USB_TRANSFER_ISOCHRONOUS,
  1897. usb->frame_number);
  1898. if (likely(!pipe))
  1899. pipe = __cvmx_usb_find_ready_pipe(usb,
  1900. usb->active_pipes +
  1901. CVMX_USB_TRANSFER_INTERRUPT,
  1902. usb->frame_number);
  1903. }
  1904. if (likely(!pipe)) {
  1905. pipe = __cvmx_usb_find_ready_pipe(usb,
  1906. usb->active_pipes +
  1907. CVMX_USB_TRANSFER_CONTROL,
  1908. usb->frame_number);
  1909. if (likely(!pipe))
  1910. pipe = __cvmx_usb_find_ready_pipe(usb,
  1911. usb->active_pipes +
  1912. CVMX_USB_TRANSFER_BULK,
  1913. usb->frame_number);
  1914. }
  1915. if (!pipe)
  1916. break;
  1917. __cvmx_usb_start_channel(usb, channel, pipe);
  1918. }
  1919. done:
  1920. /*
  1921. * Only enable SOF interrupts when we have transactions pending in the
  1922. * future that might need to be scheduled
  1923. */
  1924. need_sof = 0;
  1925. for (ttype = CVMX_USB_TRANSFER_CONTROL;
  1926. ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
  1927. list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
  1928. if (pipe->next_tx_frame > usb->frame_number) {
  1929. need_sof = 1;
  1930. break;
  1931. }
  1932. }
  1933. }
  1934. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1935. union cvmx_usbcx_gintmsk, sofmsk, need_sof);
  1936. }
  1937. static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
  1938. {
  1939. return container_of(p, struct octeon_hcd, usb);
  1940. }
  1941. static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
  1942. {
  1943. return container_of((void *)p, struct usb_hcd, hcd_priv);
  1944. }
  1945. static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
  1946. enum cvmx_usb_complete status,
  1947. struct cvmx_usb_pipe *pipe,
  1948. struct cvmx_usb_transaction
  1949. *transaction,
  1950. int bytes_transferred,
  1951. struct urb *urb)
  1952. {
  1953. struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
  1954. struct usb_hcd *hcd = octeon_to_hcd(priv);
  1955. struct device *dev = hcd->self.controller;
  1956. if (likely(status == CVMX_USB_COMPLETE_SUCCESS))
  1957. urb->actual_length = bytes_transferred;
  1958. else
  1959. urb->actual_length = 0;
  1960. urb->hcpriv = NULL;
  1961. /* For Isochronous transactions we need to update the URB packet status
  1962. list from data in our private copy */
  1963. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  1964. int i;
  1965. /*
  1966. * The pointer to the private list is stored in the setup_packet
  1967. * field.
  1968. */
  1969. struct cvmx_usb_iso_packet *iso_packet =
  1970. (struct cvmx_usb_iso_packet *) urb->setup_packet;
  1971. /* Recalculate the transfer size by adding up each packet */
  1972. urb->actual_length = 0;
  1973. for (i = 0; i < urb->number_of_packets; i++) {
  1974. if (iso_packet[i].status ==
  1975. CVMX_USB_COMPLETE_SUCCESS) {
  1976. urb->iso_frame_desc[i].status = 0;
  1977. urb->iso_frame_desc[i].actual_length =
  1978. iso_packet[i].length;
  1979. urb->actual_length +=
  1980. urb->iso_frame_desc[i].actual_length;
  1981. } else {
  1982. dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
  1983. i, urb->number_of_packets,
  1984. iso_packet[i].status, pipe,
  1985. transaction, iso_packet[i].length);
  1986. urb->iso_frame_desc[i].status = -EREMOTEIO;
  1987. }
  1988. }
  1989. /* Free the private list now that we don't need it anymore */
  1990. kfree(iso_packet);
  1991. urb->setup_packet = NULL;
  1992. }
  1993. switch (status) {
  1994. case CVMX_USB_COMPLETE_SUCCESS:
  1995. urb->status = 0;
  1996. break;
  1997. case CVMX_USB_COMPLETE_CANCEL:
  1998. if (urb->status == 0)
  1999. urb->status = -ENOENT;
  2000. break;
  2001. case CVMX_USB_COMPLETE_STALL:
  2002. dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
  2003. pipe, transaction, bytes_transferred);
  2004. urb->status = -EPIPE;
  2005. break;
  2006. case CVMX_USB_COMPLETE_BABBLEERR:
  2007. dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
  2008. pipe, transaction, bytes_transferred);
  2009. urb->status = -EPIPE;
  2010. break;
  2011. case CVMX_USB_COMPLETE_SHORT:
  2012. dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
  2013. pipe, transaction, bytes_transferred);
  2014. urb->status = -EREMOTEIO;
  2015. break;
  2016. case CVMX_USB_COMPLETE_ERROR:
  2017. case CVMX_USB_COMPLETE_XACTERR:
  2018. case CVMX_USB_COMPLETE_DATATGLERR:
  2019. case CVMX_USB_COMPLETE_FRAMEERR:
  2020. dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
  2021. status, pipe, transaction, bytes_transferred);
  2022. urb->status = -EPROTO;
  2023. break;
  2024. }
  2025. usb_hcd_unlink_urb_from_ep(octeon_to_hcd(priv), urb);
  2026. spin_unlock(&priv->lock);
  2027. usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
  2028. spin_lock(&priv->lock);
  2029. }
  2030. /**
  2031. * Signal the completion of a transaction and free it. The
  2032. * transaction will be removed from the pipe transaction list.
  2033. *
  2034. * @usb: USB device state populated by cvmx_usb_initialize().
  2035. * @pipe: Pipe the transaction is on
  2036. * @transaction:
  2037. * Transaction that completed
  2038. * @complete_code:
  2039. * Completion code
  2040. */
  2041. static void __cvmx_usb_perform_complete(
  2042. struct cvmx_usb_state *usb,
  2043. struct cvmx_usb_pipe *pipe,
  2044. struct cvmx_usb_transaction *transaction,
  2045. enum cvmx_usb_complete complete_code)
  2046. {
  2047. /* If this was a split then clear our split in progress marker */
  2048. if (usb->active_split == transaction)
  2049. usb->active_split = NULL;
  2050. /*
  2051. * Isochronous transactions need extra processing as they might not be
  2052. * done after a single data transfer
  2053. */
  2054. if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
  2055. /* Update the number of bytes transferred in this ISO packet */
  2056. transaction->iso_packets[0].length = transaction->actual_bytes;
  2057. transaction->iso_packets[0].status = complete_code;
  2058. /*
  2059. * If there are more ISOs pending and we succeeded, schedule the
  2060. * next one
  2061. */
  2062. if ((transaction->iso_number_packets > 1) &&
  2063. (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
  2064. /* No bytes transferred for this packet as of yet */
  2065. transaction->actual_bytes = 0;
  2066. /* One less ISO waiting to transfer */
  2067. transaction->iso_number_packets--;
  2068. /* Increment to the next location in our packet array */
  2069. transaction->iso_packets++;
  2070. transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
  2071. return;
  2072. }
  2073. }
  2074. /* Remove the transaction from the pipe list */
  2075. list_del(&transaction->node);
  2076. if (list_empty(&pipe->transactions))
  2077. list_move_tail(&pipe->node, &usb->idle_pipes);
  2078. octeon_usb_urb_complete_callback(usb, complete_code, pipe,
  2079. transaction,
  2080. transaction->actual_bytes,
  2081. transaction->urb);
  2082. kfree(transaction);
  2083. }
  2084. /**
  2085. * Submit a usb transaction to a pipe. Called for all types
  2086. * of transactions.
  2087. *
  2088. * @usb:
  2089. * @pipe: Which pipe to submit to.
  2090. * @type: Transaction type
  2091. * @buffer: User buffer for the transaction
  2092. * @buffer_length:
  2093. * User buffer's length in bytes
  2094. * @control_header:
  2095. * For control transactions, the 8 byte standard header
  2096. * @iso_start_frame:
  2097. * For ISO transactions, the start frame
  2098. * @iso_number_packets:
  2099. * For ISO, the number of packet in the transaction.
  2100. * @iso_packets:
  2101. * A description of each ISO packet
  2102. * @urb: URB for the callback
  2103. *
  2104. * Returns: Transaction or NULL on failure.
  2105. */
  2106. static struct cvmx_usb_transaction *__cvmx_usb_submit_transaction(
  2107. struct cvmx_usb_state *usb,
  2108. struct cvmx_usb_pipe *pipe,
  2109. enum cvmx_usb_transfer type,
  2110. uint64_t buffer,
  2111. int buffer_length,
  2112. uint64_t control_header,
  2113. int iso_start_frame,
  2114. int iso_number_packets,
  2115. struct cvmx_usb_iso_packet *iso_packets,
  2116. struct urb *urb)
  2117. {
  2118. struct cvmx_usb_transaction *transaction;
  2119. if (unlikely(pipe->transfer_type != type))
  2120. return NULL;
  2121. transaction = kzalloc(sizeof(*transaction), GFP_ATOMIC);
  2122. if (unlikely(!transaction))
  2123. return NULL;
  2124. transaction->type = type;
  2125. transaction->buffer = buffer;
  2126. transaction->buffer_length = buffer_length;
  2127. transaction->control_header = control_header;
  2128. /* FIXME: This is not used, implement it. */
  2129. transaction->iso_start_frame = iso_start_frame;
  2130. transaction->iso_number_packets = iso_number_packets;
  2131. transaction->iso_packets = iso_packets;
  2132. transaction->urb = urb;
  2133. if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
  2134. transaction->stage = CVMX_USB_STAGE_SETUP;
  2135. else
  2136. transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
  2137. if (!list_empty(&pipe->transactions)) {
  2138. list_add_tail(&transaction->node, &pipe->transactions);
  2139. } else {
  2140. list_add_tail(&transaction->node, &pipe->transactions);
  2141. list_move_tail(&pipe->node,
  2142. &usb->active_pipes[pipe->transfer_type]);
  2143. /*
  2144. * We may need to schedule the pipe if this was the head of the
  2145. * pipe.
  2146. */
  2147. __cvmx_usb_schedule(usb, 0);
  2148. }
  2149. return transaction;
  2150. }
  2151. /**
  2152. * Call to submit a USB Bulk transfer to a pipe.
  2153. *
  2154. * @usb: USB device state populated by cvmx_usb_initialize().
  2155. * @pipe: Handle to the pipe for the transfer.
  2156. * @urb: URB.
  2157. *
  2158. * Returns: A submitted transaction or NULL on failure.
  2159. */
  2160. static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
  2161. struct cvmx_usb_state *usb,
  2162. struct cvmx_usb_pipe *pipe,
  2163. struct urb *urb)
  2164. {
  2165. return __cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
  2166. urb->transfer_dma,
  2167. urb->transfer_buffer_length,
  2168. 0, /* control_header */
  2169. 0, /* iso_start_frame */
  2170. 0, /* iso_number_packets */
  2171. NULL, /* iso_packets */
  2172. urb);
  2173. }
  2174. /**
  2175. * Call to submit a USB Interrupt transfer to a pipe.
  2176. *
  2177. * @usb: USB device state populated by cvmx_usb_initialize().
  2178. * @pipe: Handle to the pipe for the transfer.
  2179. * @urb: URB returned when the callback is called.
  2180. *
  2181. * Returns: A submitted transaction or NULL on failure.
  2182. */
  2183. static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
  2184. struct cvmx_usb_state *usb,
  2185. struct cvmx_usb_pipe *pipe,
  2186. struct urb *urb)
  2187. {
  2188. return __cvmx_usb_submit_transaction(usb, pipe,
  2189. CVMX_USB_TRANSFER_INTERRUPT,
  2190. urb->transfer_dma,
  2191. urb->transfer_buffer_length,
  2192. 0, /* control_header */
  2193. 0, /* iso_start_frame */
  2194. 0, /* iso_number_packets */
  2195. NULL, /* iso_packets */
  2196. urb);
  2197. }
  2198. /**
  2199. * Call to submit a USB Control transfer to a pipe.
  2200. *
  2201. * @usb: USB device state populated by cvmx_usb_initialize().
  2202. * @pipe: Handle to the pipe for the transfer.
  2203. * @urb: URB.
  2204. *
  2205. * Returns: A submitted transaction or NULL on failure.
  2206. */
  2207. static struct cvmx_usb_transaction *cvmx_usb_submit_control(
  2208. struct cvmx_usb_state *usb,
  2209. struct cvmx_usb_pipe *pipe,
  2210. struct urb *urb)
  2211. {
  2212. int buffer_length = urb->transfer_buffer_length;
  2213. uint64_t control_header = urb->setup_dma;
  2214. struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
  2215. if ((header->bRequestType & USB_DIR_IN) == 0)
  2216. buffer_length = le16_to_cpu(header->wLength);
  2217. return __cvmx_usb_submit_transaction(usb, pipe,
  2218. CVMX_USB_TRANSFER_CONTROL,
  2219. urb->transfer_dma, buffer_length,
  2220. control_header,
  2221. 0, /* iso_start_frame */
  2222. 0, /* iso_number_packets */
  2223. NULL, /* iso_packets */
  2224. urb);
  2225. }
  2226. /**
  2227. * Call to submit a USB Isochronous transfer to a pipe.
  2228. *
  2229. * @usb: USB device state populated by cvmx_usb_initialize().
  2230. * @pipe: Handle to the pipe for the transfer.
  2231. * @urb: URB returned when the callback is called.
  2232. *
  2233. * Returns: A submitted transaction or NULL on failure.
  2234. */
  2235. static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
  2236. struct cvmx_usb_state *usb,
  2237. struct cvmx_usb_pipe *pipe,
  2238. struct urb *urb)
  2239. {
  2240. struct cvmx_usb_iso_packet *packets;
  2241. packets = (struct cvmx_usb_iso_packet *) urb->setup_packet;
  2242. return __cvmx_usb_submit_transaction(usb, pipe,
  2243. CVMX_USB_TRANSFER_ISOCHRONOUS,
  2244. urb->transfer_dma,
  2245. urb->transfer_buffer_length,
  2246. 0, /* control_header */
  2247. urb->start_frame,
  2248. urb->number_of_packets,
  2249. packets, urb);
  2250. }
  2251. /**
  2252. * Cancel one outstanding request in a pipe. Canceling a request
  2253. * can fail if the transaction has already completed before cancel
  2254. * is called. Even after a successful cancel call, it may take
  2255. * a frame or two for the cvmx_usb_poll() function to call the
  2256. * associated callback.
  2257. *
  2258. * @usb: USB device state populated by cvmx_usb_initialize().
  2259. * @pipe: Pipe to cancel requests in.
  2260. * @transaction: Transaction to cancel, returned by the submit function.
  2261. *
  2262. * Returns: 0 or a negative error code.
  2263. */
  2264. static int cvmx_usb_cancel(struct cvmx_usb_state *usb,
  2265. struct cvmx_usb_pipe *pipe,
  2266. struct cvmx_usb_transaction *transaction)
  2267. {
  2268. /*
  2269. * If the transaction is the HEAD of the queue and scheduled. We need to
  2270. * treat it special
  2271. */
  2272. if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
  2273. transaction && (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
  2274. union cvmx_usbcx_hccharx usbc_hcchar;
  2275. usb->pipe_for_channel[pipe->channel] = NULL;
  2276. pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
  2277. CVMX_SYNCW;
  2278. usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
  2279. CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
  2280. /*
  2281. * If the channel isn't enabled then the transaction already
  2282. * completed.
  2283. */
  2284. if (usbc_hcchar.s.chena) {
  2285. usbc_hcchar.s.chdis = 1;
  2286. __cvmx_usb_write_csr32(usb,
  2287. CVMX_USBCX_HCCHARX(pipe->channel,
  2288. usb->index),
  2289. usbc_hcchar.u32);
  2290. }
  2291. }
  2292. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2293. CVMX_USB_COMPLETE_CANCEL);
  2294. return 0;
  2295. }
  2296. /**
  2297. * Cancel all outstanding requests in a pipe. Logically all this
  2298. * does is call cvmx_usb_cancel() in a loop.
  2299. *
  2300. * @usb: USB device state populated by cvmx_usb_initialize().
  2301. * @pipe: Pipe to cancel requests in.
  2302. *
  2303. * Returns: 0 or a negative error code.
  2304. */
  2305. static int cvmx_usb_cancel_all(struct cvmx_usb_state *usb,
  2306. struct cvmx_usb_pipe *pipe)
  2307. {
  2308. struct cvmx_usb_transaction *transaction, *next;
  2309. /* Simply loop through and attempt to cancel each transaction */
  2310. list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
  2311. int result = cvmx_usb_cancel(usb, pipe, transaction);
  2312. if (unlikely(result != 0))
  2313. return result;
  2314. }
  2315. return 0;
  2316. }
  2317. /**
  2318. * Close a pipe created with cvmx_usb_open_pipe().
  2319. *
  2320. * @usb: USB device state populated by cvmx_usb_initialize().
  2321. * @pipe: Pipe to close.
  2322. *
  2323. * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
  2324. * outstanding transfers.
  2325. */
  2326. static int cvmx_usb_close_pipe(struct cvmx_usb_state *usb,
  2327. struct cvmx_usb_pipe *pipe)
  2328. {
  2329. /* Fail if the pipe has pending transactions */
  2330. if (!list_empty(&pipe->transactions))
  2331. return -EBUSY;
  2332. list_del(&pipe->node);
  2333. kfree(pipe);
  2334. return 0;
  2335. }
  2336. /**
  2337. * Get the current USB protocol level frame number. The frame
  2338. * number is always in the range of 0-0x7ff.
  2339. *
  2340. * @usb: USB device state populated by cvmx_usb_initialize().
  2341. *
  2342. * Returns: USB frame number
  2343. */
  2344. static int cvmx_usb_get_frame_number(struct cvmx_usb_state *usb)
  2345. {
  2346. int frame_number;
  2347. union cvmx_usbcx_hfnum usbc_hfnum;
  2348. usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
  2349. CVMX_USBCX_HFNUM(usb->index));
  2350. frame_number = usbc_hfnum.s.frnum;
  2351. return frame_number;
  2352. }
  2353. /**
  2354. * Poll a channel for status
  2355. *
  2356. * @usb: USB device
  2357. * @channel: Channel to poll
  2358. *
  2359. * Returns: Zero on success
  2360. */
  2361. static int __cvmx_usb_poll_channel(struct cvmx_usb_state *usb, int channel)
  2362. {
  2363. union cvmx_usbcx_hcintx usbc_hcint;
  2364. union cvmx_usbcx_hctsizx usbc_hctsiz;
  2365. union cvmx_usbcx_hccharx usbc_hcchar;
  2366. struct cvmx_usb_pipe *pipe;
  2367. struct cvmx_usb_transaction *transaction;
  2368. int bytes_this_transfer;
  2369. int bytes_in_last_packet;
  2370. int packets_processed;
  2371. int buffer_space_left;
  2372. /* Read the interrupt status bits for the channel */
  2373. usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
  2374. CVMX_USBCX_HCINTX(channel, usb->index));
  2375. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
  2376. usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
  2377. CVMX_USBCX_HCCHARX(channel, usb->index));
  2378. if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
  2379. /*
  2380. * There seems to be a bug in CN31XX which can cause
  2381. * interrupt IN transfers to get stuck until we do a
  2382. * write of HCCHARX without changing things
  2383. */
  2384. __cvmx_usb_write_csr32(usb,
  2385. CVMX_USBCX_HCCHARX(channel,
  2386. usb->index),
  2387. usbc_hcchar.u32);
  2388. return 0;
  2389. }
  2390. /*
  2391. * In non DMA mode the channels don't halt themselves. We need
  2392. * to manually disable channels that are left running
  2393. */
  2394. if (!usbc_hcint.s.chhltd) {
  2395. if (usbc_hcchar.s.chena) {
  2396. union cvmx_usbcx_hcintmskx hcintmsk;
  2397. /* Disable all interrupts except CHHLTD */
  2398. hcintmsk.u32 = 0;
  2399. hcintmsk.s.chhltdmsk = 1;
  2400. __cvmx_usb_write_csr32(usb,
  2401. CVMX_USBCX_HCINTMSKX(channel,
  2402. usb->index),
  2403. hcintmsk.u32);
  2404. usbc_hcchar.s.chdis = 1;
  2405. __cvmx_usb_write_csr32(usb,
  2406. CVMX_USBCX_HCCHARX(channel,
  2407. usb->index),
  2408. usbc_hcchar.u32);
  2409. return 0;
  2410. } else if (usbc_hcint.s.xfercompl) {
  2411. /*
  2412. * Successful IN/OUT with transfer complete.
  2413. * Channel halt isn't needed.
  2414. */
  2415. } else {
  2416. cvmx_dprintf("USB%d: Channel %d interrupt without halt\n",
  2417. usb->index, channel);
  2418. return 0;
  2419. }
  2420. }
  2421. } else {
  2422. /*
  2423. * There is are no interrupts that we need to process when the
  2424. * channel is still running
  2425. */
  2426. if (!usbc_hcint.s.chhltd)
  2427. return 0;
  2428. }
  2429. /* Disable the channel interrupts now that it is done */
  2430. __cvmx_usb_write_csr32(usb,
  2431. CVMX_USBCX_HCINTMSKX(channel, usb->index),
  2432. 0);
  2433. usb->idle_hardware_channels |= (1<<channel);
  2434. /* Make sure this channel is tied to a valid pipe */
  2435. pipe = usb->pipe_for_channel[channel];
  2436. prefetch(pipe);
  2437. if (!pipe)
  2438. return 0;
  2439. transaction = list_first_entry(&pipe->transactions,
  2440. typeof(*transaction),
  2441. node);
  2442. prefetch(transaction);
  2443. /*
  2444. * Disconnect this pipe from the HW channel. Later the schedule
  2445. * function will figure out which pipe needs to go
  2446. */
  2447. usb->pipe_for_channel[channel] = NULL;
  2448. pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
  2449. /*
  2450. * Read the channel config info so we can figure out how much data
  2451. * transfered
  2452. */
  2453. usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
  2454. CVMX_USBCX_HCCHARX(channel, usb->index));
  2455. usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
  2456. CVMX_USBCX_HCTSIZX(channel, usb->index));
  2457. /*
  2458. * Calculating the number of bytes successfully transferred is dependent
  2459. * on the transfer direction
  2460. */
  2461. packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
  2462. if (usbc_hcchar.s.epdir) {
  2463. /*
  2464. * IN transactions are easy. For every byte received the
  2465. * hardware decrements xfersize. All we need to do is subtract
  2466. * the current value of xfersize from its starting value and we
  2467. * know how many bytes were written to the buffer
  2468. */
  2469. bytes_this_transfer = transaction->xfersize -
  2470. usbc_hctsiz.s.xfersize;
  2471. } else {
  2472. /*
  2473. * OUT transaction don't decrement xfersize. Instead pktcnt is
  2474. * decremented on every successful packet send. The hardware
  2475. * does this when it receives an ACK, or NYET. If it doesn't
  2476. * receive one of these responses pktcnt doesn't change
  2477. */
  2478. bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
  2479. /*
  2480. * The last packet may not be a full transfer if we didn't have
  2481. * enough data
  2482. */
  2483. if (bytes_this_transfer > transaction->xfersize)
  2484. bytes_this_transfer = transaction->xfersize;
  2485. }
  2486. /* Figure out how many bytes were in the last packet of the transfer */
  2487. if (packets_processed)
  2488. bytes_in_last_packet = bytes_this_transfer -
  2489. (packets_processed - 1) * usbc_hcchar.s.mps;
  2490. else
  2491. bytes_in_last_packet = bytes_this_transfer;
  2492. /*
  2493. * As a special case, setup transactions output the setup header, not
  2494. * the user's data. For this reason we don't count setup data as bytes
  2495. * transferred
  2496. */
  2497. if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
  2498. (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
  2499. bytes_this_transfer = 0;
  2500. /*
  2501. * Add the bytes transferred to the running total. It is important that
  2502. * bytes_this_transfer doesn't count any data that needs to be
  2503. * retransmitted
  2504. */
  2505. transaction->actual_bytes += bytes_this_transfer;
  2506. if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
  2507. buffer_space_left = transaction->iso_packets[0].length -
  2508. transaction->actual_bytes;
  2509. else
  2510. buffer_space_left = transaction->buffer_length -
  2511. transaction->actual_bytes;
  2512. /*
  2513. * We need to remember the PID toggle state for the next transaction.
  2514. * The hardware already updated it for the next transaction
  2515. */
  2516. pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
  2517. /*
  2518. * For high speed bulk out, assume the next transaction will need to do
  2519. * a ping before proceeding. If this isn't true the ACK processing below
  2520. * will clear this flag
  2521. */
  2522. if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
  2523. (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
  2524. (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
  2525. pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
  2526. if (usbc_hcint.s.stall) {
  2527. /*
  2528. * STALL as a response means this transaction cannot be
  2529. * completed because the device can't process transactions. Tell
  2530. * the user. Any data that was transferred will be counted on
  2531. * the actual bytes transferred
  2532. */
  2533. pipe->pid_toggle = 0;
  2534. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2535. CVMX_USB_COMPLETE_STALL);
  2536. } else if (usbc_hcint.s.xacterr) {
  2537. /*
  2538. * We know at least one packet worked if we get a ACK or NAK.
  2539. * Reset the retry counter
  2540. */
  2541. if (usbc_hcint.s.nak || usbc_hcint.s.ack)
  2542. transaction->retries = 0;
  2543. transaction->retries++;
  2544. if (transaction->retries > MAX_RETRIES) {
  2545. /*
  2546. * XactErr as a response means the device signaled
  2547. * something wrong with the transfer. For example, PID
  2548. * toggle errors cause these
  2549. */
  2550. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2551. CVMX_USB_COMPLETE_XACTERR);
  2552. } else {
  2553. /*
  2554. * If this was a split then clear our split in progress
  2555. * marker
  2556. */
  2557. if (usb->active_split == transaction)
  2558. usb->active_split = NULL;
  2559. /*
  2560. * Rewind to the beginning of the transaction by anding
  2561. * off the split complete bit
  2562. */
  2563. transaction->stage &= ~1;
  2564. pipe->split_sc_frame = -1;
  2565. pipe->next_tx_frame += pipe->interval;
  2566. if (pipe->next_tx_frame < usb->frame_number)
  2567. pipe->next_tx_frame =
  2568. usb->frame_number + pipe->interval -
  2569. (usb->frame_number -
  2570. pipe->next_tx_frame) % pipe->interval;
  2571. }
  2572. } else if (usbc_hcint.s.bblerr) {
  2573. /* Babble Error (BblErr) */
  2574. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2575. CVMX_USB_COMPLETE_BABBLEERR);
  2576. } else if (usbc_hcint.s.datatglerr) {
  2577. /* Data toggle error */
  2578. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2579. CVMX_USB_COMPLETE_DATATGLERR);
  2580. } else if (usbc_hcint.s.nyet) {
  2581. /*
  2582. * NYET as a response is only allowed in three cases: as a
  2583. * response to a ping, as a response to a split transaction, and
  2584. * as a response to a bulk out. The ping case is handled by
  2585. * hardware, so we only have splits and bulk out
  2586. */
  2587. if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
  2588. transaction->retries = 0;
  2589. /*
  2590. * If there is more data to go then we need to try
  2591. * again. Otherwise this transaction is complete
  2592. */
  2593. if ((buffer_space_left == 0) ||
  2594. (bytes_in_last_packet < pipe->max_packet))
  2595. __cvmx_usb_perform_complete(usb, pipe,
  2596. transaction,
  2597. CVMX_USB_COMPLETE_SUCCESS);
  2598. } else {
  2599. /*
  2600. * Split transactions retry the split complete 4 times
  2601. * then rewind to the start split and do the entire
  2602. * transactions again
  2603. */
  2604. transaction->retries++;
  2605. if ((transaction->retries & 0x3) == 0) {
  2606. /*
  2607. * Rewind to the beginning of the transaction by
  2608. * anding off the split complete bit
  2609. */
  2610. transaction->stage &= ~1;
  2611. pipe->split_sc_frame = -1;
  2612. }
  2613. }
  2614. } else if (usbc_hcint.s.ack) {
  2615. transaction->retries = 0;
  2616. /*
  2617. * The ACK bit can only be checked after the other error bits.
  2618. * This is because a multi packet transfer may succeed in a
  2619. * number of packets and then get a different response on the
  2620. * last packet. In this case both ACK and the last response bit
  2621. * will be set. If none of the other response bits is set, then
  2622. * the last packet must have been an ACK
  2623. *
  2624. * Since we got an ACK, we know we don't need to do a ping on
  2625. * this pipe
  2626. */
  2627. pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
  2628. switch (transaction->type) {
  2629. case CVMX_USB_TRANSFER_CONTROL:
  2630. switch (transaction->stage) {
  2631. case CVMX_USB_STAGE_NON_CONTROL:
  2632. case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
  2633. /* This should be impossible */
  2634. __cvmx_usb_perform_complete(usb, pipe,
  2635. transaction, CVMX_USB_COMPLETE_ERROR);
  2636. break;
  2637. case CVMX_USB_STAGE_SETUP:
  2638. pipe->pid_toggle = 1;
  2639. if (__cvmx_usb_pipe_needs_split(usb, pipe))
  2640. transaction->stage =
  2641. CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
  2642. else {
  2643. struct usb_ctrlrequest *header =
  2644. cvmx_phys_to_ptr(transaction->control_header);
  2645. if (header->wLength)
  2646. transaction->stage = CVMX_USB_STAGE_DATA;
  2647. else
  2648. transaction->stage = CVMX_USB_STAGE_STATUS;
  2649. }
  2650. break;
  2651. case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
  2652. {
  2653. struct usb_ctrlrequest *header =
  2654. cvmx_phys_to_ptr(transaction->control_header);
  2655. if (header->wLength)
  2656. transaction->stage = CVMX_USB_STAGE_DATA;
  2657. else
  2658. transaction->stage = CVMX_USB_STAGE_STATUS;
  2659. }
  2660. break;
  2661. case CVMX_USB_STAGE_DATA:
  2662. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  2663. transaction->stage =
  2664. CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
  2665. /*
  2666. * For setup OUT data that are splits,
  2667. * the hardware doesn't appear to count
  2668. * transferred data. Here we manually
  2669. * update the data transferred
  2670. */
  2671. if (!usbc_hcchar.s.epdir) {
  2672. if (buffer_space_left < pipe->max_packet)
  2673. transaction->actual_bytes +=
  2674. buffer_space_left;
  2675. else
  2676. transaction->actual_bytes +=
  2677. pipe->max_packet;
  2678. }
  2679. } else if ((buffer_space_left == 0) ||
  2680. (bytes_in_last_packet <
  2681. pipe->max_packet)) {
  2682. pipe->pid_toggle = 1;
  2683. transaction->stage =
  2684. CVMX_USB_STAGE_STATUS;
  2685. }
  2686. break;
  2687. case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
  2688. if ((buffer_space_left == 0) ||
  2689. (bytes_in_last_packet <
  2690. pipe->max_packet)) {
  2691. pipe->pid_toggle = 1;
  2692. transaction->stage =
  2693. CVMX_USB_STAGE_STATUS;
  2694. } else {
  2695. transaction->stage =
  2696. CVMX_USB_STAGE_DATA;
  2697. }
  2698. break;
  2699. case CVMX_USB_STAGE_STATUS:
  2700. if (__cvmx_usb_pipe_needs_split(usb, pipe))
  2701. transaction->stage =
  2702. CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
  2703. else
  2704. __cvmx_usb_perform_complete(usb, pipe,
  2705. transaction,
  2706. CVMX_USB_COMPLETE_SUCCESS);
  2707. break;
  2708. case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
  2709. __cvmx_usb_perform_complete(usb, pipe,
  2710. transaction,
  2711. CVMX_USB_COMPLETE_SUCCESS);
  2712. break;
  2713. }
  2714. break;
  2715. case CVMX_USB_TRANSFER_BULK:
  2716. case CVMX_USB_TRANSFER_INTERRUPT:
  2717. /*
  2718. * The only time a bulk transfer isn't complete when it
  2719. * finishes with an ACK is during a split transaction.
  2720. * For splits we need to continue the transfer if more
  2721. * data is needed
  2722. */
  2723. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  2724. if (transaction->stage ==
  2725. CVMX_USB_STAGE_NON_CONTROL)
  2726. transaction->stage =
  2727. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
  2728. else {
  2729. if (buffer_space_left &&
  2730. (bytes_in_last_packet ==
  2731. pipe->max_packet))
  2732. transaction->stage =
  2733. CVMX_USB_STAGE_NON_CONTROL;
  2734. else {
  2735. if (transaction->type ==
  2736. CVMX_USB_TRANSFER_INTERRUPT)
  2737. pipe->next_tx_frame +=
  2738. pipe->interval;
  2739. __cvmx_usb_perform_complete(
  2740. usb,
  2741. pipe,
  2742. transaction,
  2743. CVMX_USB_COMPLETE_SUCCESS);
  2744. }
  2745. }
  2746. } else {
  2747. if ((pipe->device_speed ==
  2748. CVMX_USB_SPEED_HIGH) &&
  2749. (pipe->transfer_type ==
  2750. CVMX_USB_TRANSFER_BULK) &&
  2751. (pipe->transfer_dir ==
  2752. CVMX_USB_DIRECTION_OUT) &&
  2753. (usbc_hcint.s.nak))
  2754. pipe->flags |=
  2755. __CVMX_USB_PIPE_FLAGS_NEED_PING;
  2756. if (!buffer_space_left ||
  2757. (bytes_in_last_packet <
  2758. pipe->max_packet)) {
  2759. if (transaction->type ==
  2760. CVMX_USB_TRANSFER_INTERRUPT)
  2761. pipe->next_tx_frame +=
  2762. pipe->interval;
  2763. __cvmx_usb_perform_complete(usb,
  2764. pipe,
  2765. transaction,
  2766. CVMX_USB_COMPLETE_SUCCESS);
  2767. }
  2768. }
  2769. break;
  2770. case CVMX_USB_TRANSFER_ISOCHRONOUS:
  2771. if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
  2772. /*
  2773. * ISOCHRONOUS OUT splits don't require a
  2774. * complete split stage. Instead they use a
  2775. * sequence of begin OUT splits to transfer the
  2776. * data 188 bytes at a time. Once the transfer
  2777. * is complete, the pipe sleeps until the next
  2778. * schedule interval
  2779. */
  2780. if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
  2781. /*
  2782. * If no space left or this wasn't a max
  2783. * size packet then this transfer is
  2784. * complete. Otherwise start it again to
  2785. * send the next 188 bytes
  2786. */
  2787. if (!buffer_space_left ||
  2788. (bytes_this_transfer < 188)) {
  2789. pipe->next_tx_frame +=
  2790. pipe->interval;
  2791. __cvmx_usb_perform_complete(
  2792. usb,
  2793. pipe,
  2794. transaction,
  2795. CVMX_USB_COMPLETE_SUCCESS);
  2796. }
  2797. } else {
  2798. if (transaction->stage ==
  2799. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
  2800. /*
  2801. * We are in the incoming data
  2802. * phase. Keep getting data
  2803. * until we run out of space or
  2804. * get a small packet
  2805. */
  2806. if ((buffer_space_left == 0) ||
  2807. (bytes_in_last_packet <
  2808. pipe->max_packet)) {
  2809. pipe->next_tx_frame +=
  2810. pipe->interval;
  2811. __cvmx_usb_perform_complete(
  2812. usb,
  2813. pipe,
  2814. transaction,
  2815. CVMX_USB_COMPLETE_SUCCESS);
  2816. }
  2817. } else
  2818. transaction->stage =
  2819. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
  2820. }
  2821. } else {
  2822. pipe->next_tx_frame += pipe->interval;
  2823. __cvmx_usb_perform_complete(usb,
  2824. pipe,
  2825. transaction,
  2826. CVMX_USB_COMPLETE_SUCCESS);
  2827. }
  2828. break;
  2829. }
  2830. } else if (usbc_hcint.s.nak) {
  2831. /*
  2832. * If this was a split then clear our split in progress marker.
  2833. */
  2834. if (usb->active_split == transaction)
  2835. usb->active_split = NULL;
  2836. /*
  2837. * NAK as a response means the device couldn't accept the
  2838. * transaction, but it should be retried in the future. Rewind
  2839. * to the beginning of the transaction by anding off the split
  2840. * complete bit. Retry in the next interval
  2841. */
  2842. transaction->retries = 0;
  2843. transaction->stage &= ~1;
  2844. pipe->next_tx_frame += pipe->interval;
  2845. if (pipe->next_tx_frame < usb->frame_number)
  2846. pipe->next_tx_frame = usb->frame_number +
  2847. pipe->interval -
  2848. (usb->frame_number - pipe->next_tx_frame) %
  2849. pipe->interval;
  2850. } else {
  2851. struct cvmx_usb_port_status port;
  2852. port = cvmx_usb_get_status(usb);
  2853. if (port.port_enabled) {
  2854. /* We'll retry the exact same transaction again */
  2855. transaction->retries++;
  2856. } else {
  2857. /*
  2858. * We get channel halted interrupts with no result bits
  2859. * sets when the cable is unplugged
  2860. */
  2861. __cvmx_usb_perform_complete(usb, pipe, transaction,
  2862. CVMX_USB_COMPLETE_ERROR);
  2863. }
  2864. }
  2865. return 0;
  2866. }
  2867. static void octeon_usb_port_callback(struct cvmx_usb_state *usb)
  2868. {
  2869. struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
  2870. spin_unlock(&priv->lock);
  2871. usb_hcd_poll_rh_status(octeon_to_hcd(priv));
  2872. spin_lock(&priv->lock);
  2873. }
  2874. /**
  2875. * Poll the USB block for status and call all needed callback
  2876. * handlers. This function is meant to be called in the interrupt
  2877. * handler for the USB controller. It can also be called
  2878. * periodically in a loop for non-interrupt based operation.
  2879. *
  2880. * @usb: USB device state populated by cvmx_usb_initialize().
  2881. *
  2882. * Returns: 0 or a negative error code.
  2883. */
  2884. static int cvmx_usb_poll(struct cvmx_usb_state *usb)
  2885. {
  2886. union cvmx_usbcx_hfnum usbc_hfnum;
  2887. union cvmx_usbcx_gintsts usbc_gintsts;
  2888. prefetch_range(usb, sizeof(*usb));
  2889. /* Update the frame counter */
  2890. usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
  2891. CVMX_USBCX_HFNUM(usb->index));
  2892. if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
  2893. usb->frame_number += 0x4000;
  2894. usb->frame_number &= ~0x3fffull;
  2895. usb->frame_number |= usbc_hfnum.s.frnum;
  2896. /* Read the pending interrupts */
  2897. usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb,
  2898. CVMX_USBCX_GINTSTS(usb->index));
  2899. /* Clear the interrupts now that we know about them */
  2900. __cvmx_usb_write_csr32(usb,
  2901. CVMX_USBCX_GINTSTS(usb->index),
  2902. usbc_gintsts.u32);
  2903. if (usbc_gintsts.s.rxflvl) {
  2904. /*
  2905. * RxFIFO Non-Empty (RxFLvl)
  2906. * Indicates that there is at least one packet pending to be
  2907. * read from the RxFIFO.
  2908. *
  2909. * In DMA mode this is handled by hardware
  2910. */
  2911. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  2912. __cvmx_usb_poll_rx_fifo(usb);
  2913. }
  2914. if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
  2915. /* Fill the Tx FIFOs when not in DMA mode */
  2916. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  2917. __cvmx_usb_poll_tx_fifo(usb);
  2918. }
  2919. if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
  2920. union cvmx_usbcx_hprt usbc_hprt;
  2921. /*
  2922. * Disconnect Detected Interrupt (DisconnInt)
  2923. * Asserted when a device disconnect is detected.
  2924. *
  2925. * Host Port Interrupt (PrtInt)
  2926. * The core sets this bit to indicate a change in port status of
  2927. * one of the O2P USB core ports in Host mode. The application
  2928. * must read the Host Port Control and Status (HPRT) register to
  2929. * determine the exact event that caused this interrupt. The
  2930. * application must clear the appropriate status bit in the Host
  2931. * Port Control and Status register to clear this bit.
  2932. *
  2933. * Call the user's port callback
  2934. */
  2935. octeon_usb_port_callback(usb);
  2936. /* Clear the port change bits */
  2937. usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
  2938. CVMX_USBCX_HPRT(usb->index));
  2939. usbc_hprt.s.prtena = 0;
  2940. __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
  2941. usbc_hprt.u32);
  2942. }
  2943. if (usbc_gintsts.s.hchint) {
  2944. /*
  2945. * Host Channels Interrupt (HChInt)
  2946. * The core sets this bit to indicate that an interrupt is
  2947. * pending on one of the channels of the core (in Host mode).
  2948. * The application must read the Host All Channels Interrupt
  2949. * (HAINT) register to determine the exact number of the channel
  2950. * on which the interrupt occurred, and then read the
  2951. * corresponding Host Channel-n Interrupt (HCINTn) register to
  2952. * determine the exact cause of the interrupt. The application
  2953. * must clear the appropriate status bit in the HCINTn register
  2954. * to clear this bit.
  2955. */
  2956. union cvmx_usbcx_haint usbc_haint;
  2957. usbc_haint.u32 = __cvmx_usb_read_csr32(usb,
  2958. CVMX_USBCX_HAINT(usb->index));
  2959. while (usbc_haint.u32) {
  2960. int channel;
  2961. channel = __fls(usbc_haint.u32);
  2962. __cvmx_usb_poll_channel(usb, channel);
  2963. usbc_haint.u32 ^= 1<<channel;
  2964. }
  2965. }
  2966. __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
  2967. return 0;
  2968. }
  2969. /* convert between an HCD pointer and the corresponding struct octeon_hcd */
  2970. static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
  2971. {
  2972. return (struct octeon_hcd *)(hcd->hcd_priv);
  2973. }
  2974. static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
  2975. {
  2976. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  2977. unsigned long flags;
  2978. spin_lock_irqsave(&priv->lock, flags);
  2979. cvmx_usb_poll(&priv->usb);
  2980. spin_unlock_irqrestore(&priv->lock, flags);
  2981. return IRQ_HANDLED;
  2982. }
  2983. static int octeon_usb_start(struct usb_hcd *hcd)
  2984. {
  2985. hcd->state = HC_STATE_RUNNING;
  2986. return 0;
  2987. }
  2988. static void octeon_usb_stop(struct usb_hcd *hcd)
  2989. {
  2990. hcd->state = HC_STATE_HALT;
  2991. }
  2992. static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
  2993. {
  2994. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  2995. return cvmx_usb_get_frame_number(&priv->usb);
  2996. }
  2997. static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
  2998. struct urb *urb,
  2999. gfp_t mem_flags)
  3000. {
  3001. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3002. struct device *dev = hcd->self.controller;
  3003. struct cvmx_usb_transaction *transaction = NULL;
  3004. struct cvmx_usb_pipe *pipe;
  3005. unsigned long flags;
  3006. struct cvmx_usb_iso_packet *iso_packet;
  3007. struct usb_host_endpoint *ep = urb->ep;
  3008. int rc;
  3009. urb->status = 0;
  3010. spin_lock_irqsave(&priv->lock, flags);
  3011. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  3012. if (rc) {
  3013. spin_unlock_irqrestore(&priv->lock, flags);
  3014. return rc;
  3015. }
  3016. if (!ep->hcpriv) {
  3017. enum cvmx_usb_transfer transfer_type;
  3018. enum cvmx_usb_speed speed;
  3019. int split_device = 0;
  3020. int split_port = 0;
  3021. switch (usb_pipetype(urb->pipe)) {
  3022. case PIPE_ISOCHRONOUS:
  3023. transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
  3024. break;
  3025. case PIPE_INTERRUPT:
  3026. transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
  3027. break;
  3028. case PIPE_CONTROL:
  3029. transfer_type = CVMX_USB_TRANSFER_CONTROL;
  3030. break;
  3031. default:
  3032. transfer_type = CVMX_USB_TRANSFER_BULK;
  3033. break;
  3034. }
  3035. switch (urb->dev->speed) {
  3036. case USB_SPEED_LOW:
  3037. speed = CVMX_USB_SPEED_LOW;
  3038. break;
  3039. case USB_SPEED_FULL:
  3040. speed = CVMX_USB_SPEED_FULL;
  3041. break;
  3042. default:
  3043. speed = CVMX_USB_SPEED_HIGH;
  3044. break;
  3045. }
  3046. /*
  3047. * For slow devices on high speed ports we need to find the hub
  3048. * that does the speed translation so we know where to send the
  3049. * split transactions.
  3050. */
  3051. if (speed != CVMX_USB_SPEED_HIGH) {
  3052. /*
  3053. * Start at this device and work our way up the usb
  3054. * tree.
  3055. */
  3056. struct usb_device *dev = urb->dev;
  3057. while (dev->parent) {
  3058. /*
  3059. * If our parent is high speed then he'll
  3060. * receive the splits.
  3061. */
  3062. if (dev->parent->speed == USB_SPEED_HIGH) {
  3063. split_device = dev->parent->devnum;
  3064. split_port = dev->portnum;
  3065. break;
  3066. }
  3067. /*
  3068. * Move up the tree one level. If we make it all
  3069. * the way up the tree, then the port must not
  3070. * be in high speed mode and we don't need a
  3071. * split.
  3072. */
  3073. dev = dev->parent;
  3074. }
  3075. }
  3076. pipe = cvmx_usb_open_pipe(&priv->usb, usb_pipedevice(urb->pipe),
  3077. usb_pipeendpoint(urb->pipe), speed,
  3078. le16_to_cpu(ep->desc.wMaxPacketSize)
  3079. & 0x7ff,
  3080. transfer_type,
  3081. usb_pipein(urb->pipe) ?
  3082. CVMX_USB_DIRECTION_IN :
  3083. CVMX_USB_DIRECTION_OUT,
  3084. urb->interval,
  3085. (le16_to_cpu(ep->desc.wMaxPacketSize)
  3086. >> 11) & 0x3,
  3087. split_device, split_port);
  3088. if (!pipe) {
  3089. usb_hcd_unlink_urb_from_ep(hcd, urb);
  3090. spin_unlock_irqrestore(&priv->lock, flags);
  3091. dev_dbg(dev, "Failed to create pipe\n");
  3092. return -ENOMEM;
  3093. }
  3094. ep->hcpriv = pipe;
  3095. } else {
  3096. pipe = ep->hcpriv;
  3097. }
  3098. switch (usb_pipetype(urb->pipe)) {
  3099. case PIPE_ISOCHRONOUS:
  3100. dev_dbg(dev, "Submit isochronous to %d.%d\n",
  3101. usb_pipedevice(urb->pipe),
  3102. usb_pipeendpoint(urb->pipe));
  3103. /*
  3104. * Allocate a structure to use for our private list of
  3105. * isochronous packets.
  3106. */
  3107. iso_packet = kmalloc(urb->number_of_packets *
  3108. sizeof(struct cvmx_usb_iso_packet),
  3109. GFP_ATOMIC);
  3110. if (iso_packet) {
  3111. int i;
  3112. /* Fill the list with the data from the URB */
  3113. for (i = 0; i < urb->number_of_packets; i++) {
  3114. iso_packet[i].offset =
  3115. urb->iso_frame_desc[i].offset;
  3116. iso_packet[i].length =
  3117. urb->iso_frame_desc[i].length;
  3118. iso_packet[i].status =
  3119. CVMX_USB_COMPLETE_ERROR;
  3120. }
  3121. /*
  3122. * Store a pointer to the list in the URB setup_packet
  3123. * field. We know this currently isn't being used and
  3124. * this saves us a bunch of logic.
  3125. */
  3126. urb->setup_packet = (char *)iso_packet;
  3127. transaction = cvmx_usb_submit_isochronous(&priv->usb,
  3128. pipe, urb);
  3129. /*
  3130. * If submit failed we need to free our private packet
  3131. * list.
  3132. */
  3133. if (!transaction) {
  3134. urb->setup_packet = NULL;
  3135. kfree(iso_packet);
  3136. }
  3137. }
  3138. break;
  3139. case PIPE_INTERRUPT:
  3140. dev_dbg(dev, "Submit interrupt to %d.%d\n",
  3141. usb_pipedevice(urb->pipe),
  3142. usb_pipeendpoint(urb->pipe));
  3143. transaction = cvmx_usb_submit_interrupt(&priv->usb, pipe, urb);
  3144. break;
  3145. case PIPE_CONTROL:
  3146. dev_dbg(dev, "Submit control to %d.%d\n",
  3147. usb_pipedevice(urb->pipe),
  3148. usb_pipeendpoint(urb->pipe));
  3149. transaction = cvmx_usb_submit_control(&priv->usb, pipe, urb);
  3150. break;
  3151. case PIPE_BULK:
  3152. dev_dbg(dev, "Submit bulk to %d.%d\n",
  3153. usb_pipedevice(urb->pipe),
  3154. usb_pipeendpoint(urb->pipe));
  3155. transaction = cvmx_usb_submit_bulk(&priv->usb, pipe, urb);
  3156. break;
  3157. }
  3158. if (!transaction) {
  3159. usb_hcd_unlink_urb_from_ep(hcd, urb);
  3160. spin_unlock_irqrestore(&priv->lock, flags);
  3161. dev_dbg(dev, "Failed to submit\n");
  3162. return -ENOMEM;
  3163. }
  3164. urb->hcpriv = transaction;
  3165. spin_unlock_irqrestore(&priv->lock, flags);
  3166. return 0;
  3167. }
  3168. static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
  3169. struct urb *urb,
  3170. int status)
  3171. {
  3172. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3173. unsigned long flags;
  3174. int rc;
  3175. if (!urb->dev)
  3176. return -EINVAL;
  3177. spin_lock_irqsave(&priv->lock, flags);
  3178. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  3179. if (rc)
  3180. goto out;
  3181. urb->status = status;
  3182. cvmx_usb_cancel(&priv->usb, urb->ep->hcpriv, urb->hcpriv);
  3183. out:
  3184. spin_unlock_irqrestore(&priv->lock, flags);
  3185. return rc;
  3186. }
  3187. static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
  3188. struct usb_host_endpoint *ep)
  3189. {
  3190. struct device *dev = hcd->self.controller;
  3191. if (ep->hcpriv) {
  3192. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3193. struct cvmx_usb_pipe *pipe = ep->hcpriv;
  3194. unsigned long flags;
  3195. spin_lock_irqsave(&priv->lock, flags);
  3196. cvmx_usb_cancel_all(&priv->usb, pipe);
  3197. if (cvmx_usb_close_pipe(&priv->usb, pipe))
  3198. dev_dbg(dev, "Closing pipe %p failed\n", pipe);
  3199. spin_unlock_irqrestore(&priv->lock, flags);
  3200. ep->hcpriv = NULL;
  3201. }
  3202. }
  3203. static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
  3204. {
  3205. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3206. struct cvmx_usb_port_status port_status;
  3207. unsigned long flags;
  3208. spin_lock_irqsave(&priv->lock, flags);
  3209. port_status = cvmx_usb_get_status(&priv->usb);
  3210. spin_unlock_irqrestore(&priv->lock, flags);
  3211. buf[0] = 0;
  3212. buf[0] = port_status.connect_change << 1;
  3213. return buf[0] != 0;
  3214. }
  3215. static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  3216. u16 wIndex, char *buf, u16 wLength)
  3217. {
  3218. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3219. struct device *dev = hcd->self.controller;
  3220. struct cvmx_usb_port_status usb_port_status;
  3221. int port_status;
  3222. struct usb_hub_descriptor *desc;
  3223. unsigned long flags;
  3224. switch (typeReq) {
  3225. case ClearHubFeature:
  3226. dev_dbg(dev, "ClearHubFeature\n");
  3227. switch (wValue) {
  3228. case C_HUB_LOCAL_POWER:
  3229. case C_HUB_OVER_CURRENT:
  3230. /* Nothing required here */
  3231. break;
  3232. default:
  3233. return -EINVAL;
  3234. }
  3235. break;
  3236. case ClearPortFeature:
  3237. dev_dbg(dev, "ClearPortFeature\n");
  3238. if (wIndex != 1) {
  3239. dev_dbg(dev, " INVALID\n");
  3240. return -EINVAL;
  3241. }
  3242. switch (wValue) {
  3243. case USB_PORT_FEAT_ENABLE:
  3244. dev_dbg(dev, " ENABLE\n");
  3245. spin_lock_irqsave(&priv->lock, flags);
  3246. cvmx_usb_disable(&priv->usb);
  3247. spin_unlock_irqrestore(&priv->lock, flags);
  3248. break;
  3249. case USB_PORT_FEAT_SUSPEND:
  3250. dev_dbg(dev, " SUSPEND\n");
  3251. /* Not supported on Octeon */
  3252. break;
  3253. case USB_PORT_FEAT_POWER:
  3254. dev_dbg(dev, " POWER\n");
  3255. /* Not supported on Octeon */
  3256. break;
  3257. case USB_PORT_FEAT_INDICATOR:
  3258. dev_dbg(dev, " INDICATOR\n");
  3259. /* Port inidicator not supported */
  3260. break;
  3261. case USB_PORT_FEAT_C_CONNECTION:
  3262. dev_dbg(dev, " C_CONNECTION\n");
  3263. /* Clears drivers internal connect status change flag */
  3264. spin_lock_irqsave(&priv->lock, flags);
  3265. priv->usb.port_status =
  3266. cvmx_usb_get_status(&priv->usb);
  3267. spin_unlock_irqrestore(&priv->lock, flags);
  3268. break;
  3269. case USB_PORT_FEAT_C_RESET:
  3270. dev_dbg(dev, " C_RESET\n");
  3271. /*
  3272. * Clears the driver's internal Port Reset Change flag.
  3273. */
  3274. spin_lock_irqsave(&priv->lock, flags);
  3275. priv->usb.port_status =
  3276. cvmx_usb_get_status(&priv->usb);
  3277. spin_unlock_irqrestore(&priv->lock, flags);
  3278. break;
  3279. case USB_PORT_FEAT_C_ENABLE:
  3280. dev_dbg(dev, " C_ENABLE\n");
  3281. /*
  3282. * Clears the driver's internal Port Enable/Disable
  3283. * Change flag.
  3284. */
  3285. spin_lock_irqsave(&priv->lock, flags);
  3286. priv->usb.port_status =
  3287. cvmx_usb_get_status(&priv->usb);
  3288. spin_unlock_irqrestore(&priv->lock, flags);
  3289. break;
  3290. case USB_PORT_FEAT_C_SUSPEND:
  3291. dev_dbg(dev, " C_SUSPEND\n");
  3292. /*
  3293. * Clears the driver's internal Port Suspend Change
  3294. * flag, which is set when resume signaling on the host
  3295. * port is complete.
  3296. */
  3297. break;
  3298. case USB_PORT_FEAT_C_OVER_CURRENT:
  3299. dev_dbg(dev, " C_OVER_CURRENT\n");
  3300. /* Clears the driver's overcurrent Change flag */
  3301. spin_lock_irqsave(&priv->lock, flags);
  3302. priv->usb.port_status =
  3303. cvmx_usb_get_status(&priv->usb);
  3304. spin_unlock_irqrestore(&priv->lock, flags);
  3305. break;
  3306. default:
  3307. dev_dbg(dev, " UNKNOWN\n");
  3308. return -EINVAL;
  3309. }
  3310. break;
  3311. case GetHubDescriptor:
  3312. dev_dbg(dev, "GetHubDescriptor\n");
  3313. desc = (struct usb_hub_descriptor *)buf;
  3314. desc->bDescLength = 9;
  3315. desc->bDescriptorType = 0x29;
  3316. desc->bNbrPorts = 1;
  3317. desc->wHubCharacteristics = cpu_to_le16(0x08);
  3318. desc->bPwrOn2PwrGood = 1;
  3319. desc->bHubContrCurrent = 0;
  3320. desc->u.hs.DeviceRemovable[0] = 0;
  3321. desc->u.hs.DeviceRemovable[1] = 0xff;
  3322. break;
  3323. case GetHubStatus:
  3324. dev_dbg(dev, "GetHubStatus\n");
  3325. *(__le32 *) buf = 0;
  3326. break;
  3327. case GetPortStatus:
  3328. dev_dbg(dev, "GetPortStatus\n");
  3329. if (wIndex != 1) {
  3330. dev_dbg(dev, " INVALID\n");
  3331. return -EINVAL;
  3332. }
  3333. spin_lock_irqsave(&priv->lock, flags);
  3334. usb_port_status = cvmx_usb_get_status(&priv->usb);
  3335. spin_unlock_irqrestore(&priv->lock, flags);
  3336. port_status = 0;
  3337. if (usb_port_status.connect_change) {
  3338. port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
  3339. dev_dbg(dev, " C_CONNECTION\n");
  3340. }
  3341. if (usb_port_status.port_enabled) {
  3342. port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
  3343. dev_dbg(dev, " C_ENABLE\n");
  3344. }
  3345. if (usb_port_status.connected) {
  3346. port_status |= (1 << USB_PORT_FEAT_CONNECTION);
  3347. dev_dbg(dev, " CONNECTION\n");
  3348. }
  3349. if (usb_port_status.port_enabled) {
  3350. port_status |= (1 << USB_PORT_FEAT_ENABLE);
  3351. dev_dbg(dev, " ENABLE\n");
  3352. }
  3353. if (usb_port_status.port_over_current) {
  3354. port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
  3355. dev_dbg(dev, " OVER_CURRENT\n");
  3356. }
  3357. if (usb_port_status.port_powered) {
  3358. port_status |= (1 << USB_PORT_FEAT_POWER);
  3359. dev_dbg(dev, " POWER\n");
  3360. }
  3361. if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
  3362. port_status |= USB_PORT_STAT_HIGH_SPEED;
  3363. dev_dbg(dev, " HIGHSPEED\n");
  3364. } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
  3365. port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
  3366. dev_dbg(dev, " LOWSPEED\n");
  3367. }
  3368. *((__le32 *) buf) = cpu_to_le32(port_status);
  3369. break;
  3370. case SetHubFeature:
  3371. dev_dbg(dev, "SetHubFeature\n");
  3372. /* No HUB features supported */
  3373. break;
  3374. case SetPortFeature:
  3375. dev_dbg(dev, "SetPortFeature\n");
  3376. if (wIndex != 1) {
  3377. dev_dbg(dev, " INVALID\n");
  3378. return -EINVAL;
  3379. }
  3380. switch (wValue) {
  3381. case USB_PORT_FEAT_SUSPEND:
  3382. dev_dbg(dev, " SUSPEND\n");
  3383. return -EINVAL;
  3384. case USB_PORT_FEAT_POWER:
  3385. dev_dbg(dev, " POWER\n");
  3386. return -EINVAL;
  3387. case USB_PORT_FEAT_RESET:
  3388. dev_dbg(dev, " RESET\n");
  3389. spin_lock_irqsave(&priv->lock, flags);
  3390. cvmx_usb_disable(&priv->usb);
  3391. if (cvmx_usb_enable(&priv->usb))
  3392. dev_dbg(dev, "Failed to enable the port\n");
  3393. spin_unlock_irqrestore(&priv->lock, flags);
  3394. return 0;
  3395. case USB_PORT_FEAT_INDICATOR:
  3396. dev_dbg(dev, " INDICATOR\n");
  3397. /* Not supported */
  3398. break;
  3399. default:
  3400. dev_dbg(dev, " UNKNOWN\n");
  3401. return -EINVAL;
  3402. }
  3403. break;
  3404. default:
  3405. dev_dbg(dev, "Unknown root hub request\n");
  3406. return -EINVAL;
  3407. }
  3408. return 0;
  3409. }
  3410. static const struct hc_driver octeon_hc_driver = {
  3411. .description = "Octeon USB",
  3412. .product_desc = "Octeon Host Controller",
  3413. .hcd_priv_size = sizeof(struct octeon_hcd),
  3414. .irq = octeon_usb_irq,
  3415. .flags = HCD_MEMORY | HCD_USB2,
  3416. .start = octeon_usb_start,
  3417. .stop = octeon_usb_stop,
  3418. .urb_enqueue = octeon_usb_urb_enqueue,
  3419. .urb_dequeue = octeon_usb_urb_dequeue,
  3420. .endpoint_disable = octeon_usb_endpoint_disable,
  3421. .get_frame_number = octeon_usb_get_frame_number,
  3422. .hub_status_data = octeon_usb_hub_status_data,
  3423. .hub_control = octeon_usb_hub_control,
  3424. .map_urb_for_dma = octeon_map_urb_for_dma,
  3425. .unmap_urb_for_dma = octeon_unmap_urb_for_dma,
  3426. };
  3427. static int octeon_usb_probe(struct platform_device *pdev)
  3428. {
  3429. int status;
  3430. int initialize_flags;
  3431. int usb_num;
  3432. struct resource *res_mem;
  3433. struct device_node *usbn_node;
  3434. int irq = platform_get_irq(pdev, 0);
  3435. struct device *dev = &pdev->dev;
  3436. struct octeon_hcd *priv;
  3437. struct usb_hcd *hcd;
  3438. unsigned long flags;
  3439. u32 clock_rate = 48000000;
  3440. bool is_crystal_clock = false;
  3441. const char *clock_type;
  3442. int i;
  3443. if (dev->of_node == NULL) {
  3444. dev_err(dev, "Error: empty of_node\n");
  3445. return -ENXIO;
  3446. }
  3447. usbn_node = dev->of_node->parent;
  3448. i = of_property_read_u32(usbn_node,
  3449. "refclk-frequency", &clock_rate);
  3450. if (i) {
  3451. dev_err(dev, "No USBN \"refclk-frequency\"\n");
  3452. return -ENXIO;
  3453. }
  3454. switch (clock_rate) {
  3455. case 12000000:
  3456. initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
  3457. break;
  3458. case 24000000:
  3459. initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
  3460. break;
  3461. case 48000000:
  3462. initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
  3463. break;
  3464. default:
  3465. dev_err(dev, "Illebal USBN \"refclk-frequency\" %u\n",
  3466. clock_rate);
  3467. return -ENXIO;
  3468. }
  3469. i = of_property_read_string(usbn_node,
  3470. "refclk-type", &clock_type);
  3471. if (!i && strcmp("crystal", clock_type) == 0)
  3472. is_crystal_clock = true;
  3473. if (is_crystal_clock)
  3474. initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
  3475. else
  3476. initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
  3477. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3478. if (res_mem == NULL) {
  3479. dev_err(dev, "found no memory resource\n");
  3480. return -ENXIO;
  3481. }
  3482. usb_num = (res_mem->start >> 44) & 1;
  3483. if (irq < 0) {
  3484. /* Defective device tree, but we know how to fix it. */
  3485. irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
  3486. irq = irq_create_mapping(NULL, hwirq);
  3487. }
  3488. /*
  3489. * Set the DMA mask to 64bits so we get buffers already translated for
  3490. * DMA.
  3491. */
  3492. dev->coherent_dma_mask = ~0;
  3493. dev->dma_mask = &dev->coherent_dma_mask;
  3494. /*
  3495. * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
  3496. * IOB priority registers. Under heavy network load USB
  3497. * hardware can be starved by the IOB causing a crash. Give
  3498. * it a priority boost if it has been waiting more than 400
  3499. * cycles to avoid this situation.
  3500. *
  3501. * Testing indicates that a cnt_val of 8192 is not sufficient,
  3502. * but no failures are seen with 4096. We choose a value of
  3503. * 400 to give a safety factor of 10.
  3504. */
  3505. if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
  3506. union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
  3507. pri_cnt.u64 = 0;
  3508. pri_cnt.s.cnt_enb = 1;
  3509. pri_cnt.s.cnt_val = 400;
  3510. cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
  3511. }
  3512. hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
  3513. if (!hcd) {
  3514. dev_dbg(dev, "Failed to allocate memory for HCD\n");
  3515. return -1;
  3516. }
  3517. hcd->uses_new_polling = 1;
  3518. priv = (struct octeon_hcd *)hcd->hcd_priv;
  3519. spin_lock_init(&priv->lock);
  3520. status = cvmx_usb_initialize(&priv->usb, usb_num, initialize_flags);
  3521. if (status) {
  3522. dev_dbg(dev, "USB initialization failed with %d\n", status);
  3523. kfree(hcd);
  3524. return -1;
  3525. }
  3526. /* This delay is needed for CN3010, but I don't know why... */
  3527. mdelay(10);
  3528. spin_lock_irqsave(&priv->lock, flags);
  3529. cvmx_usb_poll(&priv->usb);
  3530. spin_unlock_irqrestore(&priv->lock, flags);
  3531. status = usb_add_hcd(hcd, irq, 0);
  3532. if (status) {
  3533. dev_dbg(dev, "USB add HCD failed with %d\n", status);
  3534. kfree(hcd);
  3535. return -1;
  3536. }
  3537. device_wakeup_enable(hcd->self.controller);
  3538. dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
  3539. return 0;
  3540. }
  3541. static int octeon_usb_remove(struct platform_device *pdev)
  3542. {
  3543. int status;
  3544. struct device *dev = &pdev->dev;
  3545. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3546. struct octeon_hcd *priv = hcd_to_octeon(hcd);
  3547. unsigned long flags;
  3548. usb_remove_hcd(hcd);
  3549. spin_lock_irqsave(&priv->lock, flags);
  3550. status = cvmx_usb_shutdown(&priv->usb);
  3551. spin_unlock_irqrestore(&priv->lock, flags);
  3552. if (status)
  3553. dev_dbg(dev, "USB shutdown failed with %d\n", status);
  3554. kfree(hcd);
  3555. return 0;
  3556. }
  3557. static struct of_device_id octeon_usb_match[] = {
  3558. {
  3559. .compatible = "cavium,octeon-5750-usbc",
  3560. },
  3561. {},
  3562. };
  3563. static struct platform_driver octeon_usb_driver = {
  3564. .driver = {
  3565. .name = "OcteonUSB",
  3566. .owner = THIS_MODULE,
  3567. .of_match_table = octeon_usb_match,
  3568. },
  3569. .probe = octeon_usb_probe,
  3570. .remove = octeon_usb_remove,
  3571. };
  3572. static int __init octeon_usb_driver_init(void)
  3573. {
  3574. if (usb_disabled())
  3575. return 0;
  3576. return platform_driver_register(&octeon_usb_driver);
  3577. }
  3578. module_init(octeon_usb_driver_init);
  3579. static void __exit octeon_usb_driver_exit(void)
  3580. {
  3581. if (usb_disabled())
  3582. return;
  3583. platform_driver_unregister(&octeon_usb_driver);
  3584. }
  3585. module_exit(octeon_usb_driver_exit);
  3586. MODULE_LICENSE("GPL");
  3587. MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
  3588. MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");