octeon-hcd.c 110 KB

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