octeon-hcd.c 110 KB

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