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/irqdomain.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/platform_device.h>
  55. #include <linux/of.h>
  56. #include <asm/octeon/octeon.h>
  57. #include "octeon-hcd.h"
  58. /**
  59. * enum cvmx_usb_speed - the possible USB device speeds
  60. *
  61. * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
  62. * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
  63. * @CVMX_USB_SPEED_LOW: Device is operation at 1.5Mbps
  64. */
  65. enum cvmx_usb_speed {
  66. CVMX_USB_SPEED_HIGH = 0,
  67. CVMX_USB_SPEED_FULL = 1,
  68. CVMX_USB_SPEED_LOW = 2,
  69. };
  70. /**
  71. * enum cvmx_usb_transfer - the possible USB transfer types
  72. *
  73. * @CVMX_USB_TRANSFER_CONTROL: USB transfer type control for hub and status
  74. * transfers
  75. * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
  76. * priority periodic transfers
  77. * @CVMX_USB_TRANSFER_BULK: USB transfer type bulk for large low priority
  78. * transfers
  79. * @CVMX_USB_TRANSFER_INTERRUPT: USB transfer type interrupt for high priority
  80. * periodic transfers
  81. */
  82. enum cvmx_usb_transfer {
  83. CVMX_USB_TRANSFER_CONTROL = 0,
  84. CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
  85. CVMX_USB_TRANSFER_BULK = 2,
  86. CVMX_USB_TRANSFER_INTERRUPT = 3,
  87. };
  88. /**
  89. * enum cvmx_usb_direction - the transfer directions
  90. *
  91. * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
  92. * @CVMX_USB_DIRECTION_IN: Data is transferring from the device/host to Octeon
  93. */
  94. enum cvmx_usb_direction {
  95. CVMX_USB_DIRECTION_OUT,
  96. CVMX_USB_DIRECTION_IN,
  97. };
  98. /**
  99. * enum cvmx_usb_status - possible callback function status codes
  100. *
  101. * @CVMX_USB_STATUS_OK: The transaction / operation finished without
  102. * any errors
  103. * @CVMX_USB_STATUS_SHORT: FIXME: This is currently not implemented
  104. * @CVMX_USB_STATUS_CANCEL: The transaction was canceled while in flight
  105. * by a user call to cvmx_usb_cancel
  106. * @CVMX_USB_STATUS_ERROR: The transaction aborted with an unexpected
  107. * error status
  108. * @CVMX_USB_STATUS_STALL: The transaction received a USB STALL response
  109. * from the device
  110. * @CVMX_USB_STATUS_XACTERR: The transaction failed with an error from the
  111. * device even after a number of retries
  112. * @CVMX_USB_STATUS_DATATGLERR: The transaction failed with a data toggle
  113. * error even after a number of retries
  114. * @CVMX_USB_STATUS_BABBLEERR: The transaction failed with a babble error
  115. * @CVMX_USB_STATUS_FRAMEERR: The transaction failed with a frame error
  116. * even after a number of retries
  117. */
  118. enum cvmx_usb_status {
  119. CVMX_USB_STATUS_OK,
  120. CVMX_USB_STATUS_SHORT,
  121. CVMX_USB_STATUS_CANCEL,
  122. CVMX_USB_STATUS_ERROR,
  123. CVMX_USB_STATUS_STALL,
  124. CVMX_USB_STATUS_XACTERR,
  125. CVMX_USB_STATUS_DATATGLERR,
  126. CVMX_USB_STATUS_BABBLEERR,
  127. CVMX_USB_STATUS_FRAMEERR,
  128. };
  129. /**
  130. * struct cvmx_usb_port_status - the USB port status information
  131. *
  132. * @port_enabled: 1 = Usb port is enabled, 0 = disabled
  133. * @port_over_current: 1 = Over current detected, 0 = Over current not
  134. * detected. Octeon doesn't support over current detection.
  135. * @port_powered: 1 = Port power is being supplied to the device, 0 =
  136. * power is off. Octeon doesn't support turning port power
  137. * off.
  138. * @port_speed: Current port speed.
  139. * @connected: 1 = A device is connected to the port, 0 = No device is
  140. * connected.
  141. * @connect_change: 1 = Device connected state changed since the last set
  142. * status call.
  143. */
  144. struct cvmx_usb_port_status {
  145. u32 reserved : 25;
  146. u32 port_enabled : 1;
  147. u32 port_over_current : 1;
  148. u32 port_powered : 1;
  149. enum cvmx_usb_speed port_speed : 2;
  150. u32 connected : 1;
  151. u32 connect_change : 1;
  152. };
  153. /**
  154. * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
  155. *
  156. * @offset: This is the offset in bytes into the main buffer where this data
  157. * is stored.
  158. * @length: This is the length in bytes of the data.
  159. * @status: This is the status of this individual packet transfer.
  160. */
  161. struct cvmx_usb_iso_packet {
  162. int offset;
  163. int length;
  164. enum cvmx_usb_status status;
  165. };
  166. /**
  167. * enum cvmx_usb_initialize_flags - flags used by the initialization function
  168. *
  169. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI: The USB port uses a 12MHz crystal
  170. * as clock source at USB_XO and
  171. * USB_XI.
  172. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND: The USB port uses 12/24/48MHz 2.5V
  173. * board clock source at USB_XO.
  174. * USB_XI should be tied to GND.
  175. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
  176. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ: Speed of reference clock or
  177. * crystal
  178. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ: Speed of reference clock
  179. * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ: Speed of reference clock
  180. * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA: Disable DMA and used polled IO for
  181. * data transfer use for the USB
  182. */
  183. enum cvmx_usb_initialize_flags {
  184. CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
  185. CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
  186. CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
  187. CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
  188. CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
  189. CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
  190. /* Bits 3-4 used to encode the clock frequency */
  191. CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
  192. };
  193. /**
  194. * enum cvmx_usb_pipe_flags - internal flags for a pipe.
  195. *
  196. * @CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
  197. * actively using hardware.
  198. * @CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high speed
  199. * pipe is in the ping state.
  200. */
  201. enum cvmx_usb_pipe_flags {
  202. CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
  203. CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
  204. };
  205. /* Maximum number of times to retry failed transactions */
  206. #define MAX_RETRIES 3
  207. /* Maximum number of hardware channels supported by the USB block */
  208. #define MAX_CHANNELS 8
  209. /*
  210. * The low level hardware can transfer a maximum of this number of bytes in each
  211. * transfer. The field is 19 bits wide
  212. */
  213. #define MAX_TRANSFER_BYTES ((1 << 19) - 1)
  214. /*
  215. * The low level hardware can transfer a maximum of this number of packets in
  216. * each transfer. The field is 10 bits wide
  217. */
  218. #define MAX_TRANSFER_PACKETS ((1 << 10) - 1)
  219. /**
  220. * Logical transactions may take numerous low level
  221. * transactions, especially when splits are concerned. This
  222. * enum represents all of the possible stages a transaction can
  223. * be in. Note that split completes are always even. This is so
  224. * the NAK handler can backup to the previous low level
  225. * transaction with a simple clearing of bit 0.
  226. */
  227. enum cvmx_usb_stage {
  228. CVMX_USB_STAGE_NON_CONTROL,
  229. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
  230. CVMX_USB_STAGE_SETUP,
  231. CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
  232. CVMX_USB_STAGE_DATA,
  233. CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
  234. CVMX_USB_STAGE_STATUS,
  235. CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
  236. };
  237. /**
  238. * struct cvmx_usb_transaction - describes each pending USB transaction
  239. * regardless of type. These are linked together
  240. * to form a list of pending requests for a pipe.
  241. *
  242. * @node: List node for transactions in the pipe.
  243. * @type: Type of transaction, duplicated of the pipe.
  244. * @flags: State flags for this transaction.
  245. * @buffer: User's physical buffer address to read/write.
  246. * @buffer_length: Size of the user's buffer in bytes.
  247. * @control_header: For control transactions, physical address of the 8
  248. * byte standard header.
  249. * @iso_start_frame: For ISO transactions, the starting frame number.
  250. * @iso_number_packets: For ISO transactions, the number of packets in the
  251. * request.
  252. * @iso_packets: For ISO transactions, the sub packets in the request.
  253. * @actual_bytes: Actual bytes transfer for this transaction.
  254. * @stage: For control transactions, the current stage.
  255. * @urb: URB.
  256. */
  257. struct cvmx_usb_transaction {
  258. struct list_head node;
  259. enum cvmx_usb_transfer type;
  260. u64 buffer;
  261. int buffer_length;
  262. u64 control_header;
  263. int iso_start_frame;
  264. int iso_number_packets;
  265. struct cvmx_usb_iso_packet *iso_packets;
  266. int xfersize;
  267. int pktcnt;
  268. int retries;
  269. int actual_bytes;
  270. enum cvmx_usb_stage stage;
  271. struct urb *urb;
  272. };
  273. /**
  274. * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
  275. * and some USB device. It contains a list of pending
  276. * request to the device.
  277. *
  278. * @node: List node for pipe list
  279. * @next: Pipe after this one in the list
  280. * @transactions: List of pending transactions
  281. * @interval: For periodic pipes, the interval between packets in
  282. * frames
  283. * @next_tx_frame: The next frame this pipe is allowed to transmit on
  284. * @flags: State flags for this pipe
  285. * @device_speed: Speed of device connected to this pipe
  286. * @transfer_type: Type of transaction supported by this pipe
  287. * @transfer_dir: IN or OUT. Ignored for Control
  288. * @multi_count: Max packet in a row for the device
  289. * @max_packet: The device's maximum packet size in bytes
  290. * @device_addr: USB device address at other end of pipe
  291. * @endpoint_num: USB endpoint number at other end of pipe
  292. * @hub_device_addr: Hub address this device is connected to
  293. * @hub_port: Hub port this device is connected to
  294. * @pid_toggle: This toggles between 0/1 on every packet send to track
  295. * the data pid needed
  296. * @channel: Hardware DMA channel for this pipe
  297. * @split_sc_frame: The low order bits of the frame number the split
  298. * complete should be sent on
  299. */
  300. struct cvmx_usb_pipe {
  301. struct list_head node;
  302. struct list_head transactions;
  303. u64 interval;
  304. u64 next_tx_frame;
  305. enum cvmx_usb_pipe_flags flags;
  306. enum cvmx_usb_speed device_speed;
  307. enum cvmx_usb_transfer transfer_type;
  308. enum cvmx_usb_direction transfer_dir;
  309. int multi_count;
  310. u16 max_packet;
  311. u8 device_addr;
  312. u8 endpoint_num;
  313. u8 hub_device_addr;
  314. u8 hub_port;
  315. u8 pid_toggle;
  316. u8 channel;
  317. s8 split_sc_frame;
  318. };
  319. struct cvmx_usb_tx_fifo {
  320. struct {
  321. int channel;
  322. int size;
  323. u64 address;
  324. } entry[MAX_CHANNELS + 1];
  325. int head;
  326. int tail;
  327. };
  328. /**
  329. * struct octeon_hcd - the state of the USB block
  330. *
  331. * lock: Serialization lock.
  332. * init_flags: Flags passed to initialize.
  333. * index: Which USB block this is for.
  334. * idle_hardware_channels: Bit set for every idle hardware channel.
  335. * usbcx_hprt: Stored port status so we don't need to read a CSR to
  336. * determine splits.
  337. * pipe_for_channel: Map channels to pipes.
  338. * pipe: Storage for pipes.
  339. * indent: Used by debug output to indent functions.
  340. * port_status: Last port status used for change notification.
  341. * idle_pipes: List of open pipes that have no transactions.
  342. * active_pipes: Active pipes indexed by transfer type.
  343. * frame_number: Increments every SOF interrupt for time keeping.
  344. * active_split: Points to the current active split, or NULL.
  345. */
  346. struct octeon_hcd {
  347. spinlock_t lock; /* serialization lock */
  348. int init_flags;
  349. int index;
  350. int idle_hardware_channels;
  351. union cvmx_usbcx_hprt usbcx_hprt;
  352. struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
  353. int indent;
  354. struct cvmx_usb_port_status port_status;
  355. struct list_head idle_pipes;
  356. struct list_head active_pipes[4];
  357. u64 frame_number;
  358. struct cvmx_usb_transaction *active_split;
  359. struct cvmx_usb_tx_fifo periodic;
  360. struct cvmx_usb_tx_fifo nonperiodic;
  361. };
  362. /*
  363. * This macro logically sets a single field in a CSR. It does the sequence
  364. * read, modify, and write
  365. */
  366. #define USB_SET_FIELD32(address, _union, field, value) \
  367. do { \
  368. union _union c; \
  369. \
  370. c.u32 = cvmx_usb_read_csr32(usb, address); \
  371. c.s.field = value; \
  372. cvmx_usb_write_csr32(usb, address, c.u32); \
  373. } while (0)
  374. /* Returns the IO address to push/pop stuff data from the FIFOs */
  375. #define USB_FIFO_ADDRESS(channel, usb_index) \
  376. (CVMX_USBCX_GOTGCTL(usb_index) + ((channel) + 1) * 0x1000)
  377. /**
  378. * struct octeon_temp_buffer - a bounce buffer for USB transfers
  379. * @orig_buffer: the original buffer passed by the USB stack
  380. * @data: the newly allocated temporary buffer (excluding meta-data)
  381. *
  382. * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
  383. * the buffer is too short, we need to allocate a temporary one, and this struct
  384. * represents it.
  385. */
  386. struct octeon_temp_buffer {
  387. void *orig_buffer;
  388. u8 data[];
  389. };
  390. static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
  391. {
  392. return container_of((void *)p, struct usb_hcd, hcd_priv);
  393. }
  394. /**
  395. * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
  396. * (if needed)
  397. * @urb: URB.
  398. * @mem_flags: Memory allocation flags.
  399. *
  400. * This function allocates a temporary bounce buffer whenever it's needed
  401. * due to HW limitations.
  402. */
  403. static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  404. {
  405. struct octeon_temp_buffer *temp;
  406. if (urb->num_sgs || urb->sg ||
  407. (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
  408. !(urb->transfer_buffer_length % sizeof(u32)))
  409. return 0;
  410. temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
  411. sizeof(*temp), mem_flags);
  412. if (!temp)
  413. return -ENOMEM;
  414. temp->orig_buffer = urb->transfer_buffer;
  415. if (usb_urb_dir_out(urb))
  416. memcpy(temp->data, urb->transfer_buffer,
  417. urb->transfer_buffer_length);
  418. urb->transfer_buffer = temp->data;
  419. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  420. return 0;
  421. }
  422. /**
  423. * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
  424. * @urb: URB.
  425. *
  426. * Frees a buffer allocated by octeon_alloc_temp_buffer().
  427. */
  428. static void octeon_free_temp_buffer(struct urb *urb)
  429. {
  430. struct octeon_temp_buffer *temp;
  431. size_t length;
  432. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  433. return;
  434. temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
  435. data);
  436. if (usb_urb_dir_in(urb)) {
  437. if (usb_pipeisoc(urb->pipe))
  438. length = urb->transfer_buffer_length;
  439. else
  440. length = urb->actual_length;
  441. memcpy(temp->orig_buffer, urb->transfer_buffer, length);
  442. }
  443. urb->transfer_buffer = temp->orig_buffer;
  444. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  445. kfree(temp);
  446. }
  447. /**
  448. * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
  449. * @hcd: USB HCD structure.
  450. * @urb: URB.
  451. * @mem_flags: Memory allocation flags.
  452. */
  453. static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  454. gfp_t mem_flags)
  455. {
  456. int ret;
  457. ret = octeon_alloc_temp_buffer(urb, mem_flags);
  458. if (ret)
  459. return ret;
  460. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  461. if (ret)
  462. octeon_free_temp_buffer(urb);
  463. return ret;
  464. }
  465. /**
  466. * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
  467. * @hcd: USB HCD structure.
  468. * @urb: URB.
  469. */
  470. static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  471. {
  472. usb_hcd_unmap_urb_for_dma(hcd, urb);
  473. octeon_free_temp_buffer(urb);
  474. }
  475. /**
  476. * Read a USB 32bit CSR. It performs the necessary address swizzle
  477. * for 32bit CSRs and logs the value in a readable format if
  478. * debugging is on.
  479. *
  480. * @usb: USB block this access is for
  481. * @address: 64bit address to read
  482. *
  483. * Returns: Result of the read
  484. */
  485. static inline u32 cvmx_usb_read_csr32(struct octeon_hcd *usb, u64 address)
  486. {
  487. return cvmx_read64_uint32(address ^ 4);
  488. }
  489. /**
  490. * Write a USB 32bit CSR. It performs the necessary address
  491. * swizzle for 32bit CSRs and logs the value in a readable format
  492. * if debugging is on.
  493. *
  494. * @usb: USB block this access is for
  495. * @address: 64bit address to write
  496. * @value: Value to write
  497. */
  498. static inline void cvmx_usb_write_csr32(struct octeon_hcd *usb,
  499. u64 address, u32 value)
  500. {
  501. cvmx_write64_uint32(address ^ 4, value);
  502. cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  503. }
  504. /**
  505. * Return non zero if this pipe connects to a non HIGH speed
  506. * device through a high speed hub.
  507. *
  508. * @usb: USB block this access is for
  509. * @pipe: Pipe to check
  510. *
  511. * Returns: Non zero if we need to do split transactions
  512. */
  513. static inline int cvmx_usb_pipe_needs_split(struct octeon_hcd *usb,
  514. struct cvmx_usb_pipe *pipe)
  515. {
  516. return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
  517. usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
  518. }
  519. /**
  520. * Trivial utility function to return the correct PID for a pipe
  521. *
  522. * @pipe: pipe to check
  523. *
  524. * Returns: PID for pipe
  525. */
  526. static inline int cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
  527. {
  528. if (pipe->pid_toggle)
  529. return 2; /* Data1 */
  530. return 0; /* Data0 */
  531. }
  532. /* Loops through register until txfflsh or rxfflsh become zero.*/
  533. static int cvmx_wait_tx_rx(struct octeon_hcd *usb, int fflsh_type)
  534. {
  535. int result;
  536. u64 address = CVMX_USBCX_GRSTCTL(usb->index);
  537. u64 done = cvmx_get_cycle() + 100 *
  538. (u64)octeon_get_clock_rate / 1000000;
  539. union cvmx_usbcx_grstctl c;
  540. while (1) {
  541. c.u32 = cvmx_usb_read_csr32(usb, address);
  542. if (fflsh_type == 0 && c.s.txfflsh == 0) {
  543. result = 0;
  544. break;
  545. } else if (fflsh_type == 1 && c.s.rxfflsh == 0) {
  546. result = 0;
  547. break;
  548. } else if (cvmx_get_cycle() > done) {
  549. result = -1;
  550. break;
  551. }
  552. __delay(100);
  553. }
  554. return result;
  555. }
  556. static void cvmx_fifo_setup(struct octeon_hcd *usb)
  557. {
  558. union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
  559. union cvmx_usbcx_gnptxfsiz npsiz;
  560. union cvmx_usbcx_hptxfsiz psiz;
  561. usbcx_ghwcfg3.u32 = cvmx_usb_read_csr32(usb,
  562. CVMX_USBCX_GHWCFG3(usb->index));
  563. /*
  564. * Program the USBC_GRXFSIZ register to select the size of the receive
  565. * FIFO (25%).
  566. */
  567. USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz,
  568. rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
  569. /*
  570. * Program the USBC_GNPTXFSIZ register to select the size and the start
  571. * address of the non-periodic transmit FIFO for nonperiodic
  572. * transactions (50%).
  573. */
  574. npsiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
  575. npsiz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
  576. npsiz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
  577. cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), npsiz.u32);
  578. /*
  579. * Program the USBC_HPTXFSIZ register to select the size and start
  580. * address of the periodic transmit FIFO for periodic transactions
  581. * (25%).
  582. */
  583. psiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
  584. psiz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
  585. psiz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
  586. cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), psiz.u32);
  587. /* Flush all FIFOs */
  588. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  589. cvmx_usbcx_grstctl, txfnum, 0x10);
  590. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  591. cvmx_usbcx_grstctl, txfflsh, 1);
  592. cvmx_wait_tx_rx(usb, 0);
  593. USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
  594. cvmx_usbcx_grstctl, rxfflsh, 1);
  595. cvmx_wait_tx_rx(usb, 1);
  596. }
  597. /**
  598. * Shutdown a USB port after a call to cvmx_usb_initialize().
  599. * The port should be disabled with all pipes closed when this
  600. * function is called.
  601. *
  602. * @usb: USB device state populated by cvmx_usb_initialize().
  603. *
  604. * Returns: 0 or a negative error code.
  605. */
  606. static int cvmx_usb_shutdown(struct octeon_hcd *usb)
  607. {
  608. union cvmx_usbnx_clk_ctl usbn_clk_ctl;
  609. /* Make sure all pipes are closed */
  610. if (!list_empty(&usb->idle_pipes) ||
  611. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
  612. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
  613. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
  614. !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
  615. return -EBUSY;
  616. /* Disable the clocks and put them in power on reset */
  617. usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
  618. usbn_clk_ctl.s.enable = 1;
  619. usbn_clk_ctl.s.por = 1;
  620. usbn_clk_ctl.s.hclk_rst = 1;
  621. usbn_clk_ctl.s.prst = 0;
  622. usbn_clk_ctl.s.hrst = 0;
  623. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  624. return 0;
  625. }
  626. /**
  627. * Initialize a USB port for use. This must be called before any
  628. * other access to the Octeon USB port is made. The port starts
  629. * off in the disabled state.
  630. *
  631. * @dev: Pointer to struct device for logging purposes.
  632. * @usb: Pointer to struct octeon_hcd.
  633. *
  634. * Returns: 0 or a negative error code.
  635. */
  636. static int cvmx_usb_initialize(struct device *dev,
  637. struct octeon_hcd *usb)
  638. {
  639. int channel;
  640. int divisor;
  641. int retries = 0;
  642. union cvmx_usbcx_hcfg usbcx_hcfg;
  643. union cvmx_usbnx_clk_ctl usbn_clk_ctl;
  644. union cvmx_usbcx_gintsts usbc_gintsts;
  645. union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
  646. union cvmx_usbcx_gintmsk usbcx_gintmsk;
  647. union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
  648. union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
  649. retry:
  650. /*
  651. * Power On Reset and PHY Initialization
  652. *
  653. * 1. Wait for DCOK to assert (nothing to do)
  654. *
  655. * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
  656. * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
  657. */
  658. usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
  659. usbn_clk_ctl.s.por = 1;
  660. usbn_clk_ctl.s.hrst = 0;
  661. usbn_clk_ctl.s.prst = 0;
  662. usbn_clk_ctl.s.hclk_rst = 0;
  663. usbn_clk_ctl.s.enable = 0;
  664. /*
  665. * 2b. Select the USB reference clock/crystal parameters by writing
  666. * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
  667. */
  668. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
  669. /*
  670. * The USB port uses 12/24/48MHz 2.5V board clock
  671. * source at USB_XO. USB_XI should be tied to GND.
  672. * Most Octeon evaluation boards require this setting
  673. */
  674. if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
  675. OCTEON_IS_MODEL(OCTEON_CN56XX) ||
  676. OCTEON_IS_MODEL(OCTEON_CN50XX))
  677. /* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
  678. usbn_clk_ctl.s.p_rtype = 2; /* p_rclk=1 & p_xenbn=0 */
  679. else
  680. /* From CN52XX manual */
  681. usbn_clk_ctl.s.p_rtype = 1;
  682. switch (usb->init_flags &
  683. CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
  684. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
  685. usbn_clk_ctl.s.p_c_sel = 0;
  686. break;
  687. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
  688. usbn_clk_ctl.s.p_c_sel = 1;
  689. break;
  690. case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
  691. usbn_clk_ctl.s.p_c_sel = 2;
  692. break;
  693. }
  694. } else {
  695. /*
  696. * The USB port uses a 12MHz crystal as clock source
  697. * at USB_XO and USB_XI
  698. */
  699. if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
  700. /* From CN31XX,CN30XX manual */
  701. usbn_clk_ctl.s.p_rtype = 3; /* p_rclk=1 & p_xenbn=1 */
  702. else
  703. /* From CN56XX,CN52XX,CN50XX manuals. */
  704. usbn_clk_ctl.s.p_rtype = 0;
  705. usbn_clk_ctl.s.p_c_sel = 0;
  706. }
  707. /*
  708. * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
  709. * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
  710. * such that USB is as close as possible to 125Mhz
  711. */
  712. divisor = DIV_ROUND_UP(octeon_get_clock_rate(), 125000000);
  713. /* Lower than 4 doesn't seem to work properly */
  714. if (divisor < 4)
  715. divisor = 4;
  716. usbn_clk_ctl.s.divide = divisor;
  717. usbn_clk_ctl.s.divide2 = 0;
  718. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  719. /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
  720. usbn_clk_ctl.s.hclk_rst = 1;
  721. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  722. /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
  723. __delay(64);
  724. /*
  725. * 3. Program the power-on reset field in the USBN clock-control
  726. * register:
  727. * USBN_CLK_CTL[POR] = 0
  728. */
  729. usbn_clk_ctl.s.por = 0;
  730. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  731. /* 4. Wait 1 ms for PHY clock to start */
  732. mdelay(1);
  733. /*
  734. * 5. Program the Reset input from automatic test equipment field in the
  735. * USBP control and status register:
  736. * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
  737. */
  738. usbn_usbp_ctl_status.u64 =
  739. cvmx_read64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index));
  740. usbn_usbp_ctl_status.s.ate_reset = 1;
  741. cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  742. usbn_usbp_ctl_status.u64);
  743. /* 6. Wait 10 cycles */
  744. __delay(10);
  745. /*
  746. * 7. Clear ATE_RESET field in the USBN clock-control register:
  747. * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
  748. */
  749. usbn_usbp_ctl_status.s.ate_reset = 0;
  750. cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  751. usbn_usbp_ctl_status.u64);
  752. /*
  753. * 8. Program the PHY reset field in the USBN clock-control register:
  754. * USBN_CLK_CTL[PRST] = 1
  755. */
  756. usbn_clk_ctl.s.prst = 1;
  757. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  758. /*
  759. * 9. Program the USBP control and status register to select host or
  760. * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
  761. * device
  762. */
  763. usbn_usbp_ctl_status.s.hst_mode = 0;
  764. cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
  765. usbn_usbp_ctl_status.u64);
  766. /* 10. Wait 1 us */
  767. udelay(1);
  768. /*
  769. * 11. Program the hreset_n field in the USBN clock-control register:
  770. * USBN_CLK_CTL[HRST] = 1
  771. */
  772. usbn_clk_ctl.s.hrst = 1;
  773. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  774. /* 12. Proceed to USB core initialization */
  775. usbn_clk_ctl.s.enable = 1;
  776. cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
  777. udelay(1);
  778. /*
  779. * USB Core Initialization
  780. *
  781. * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
  782. * determine USB core configuration parameters.
  783. *
  784. * Nothing needed
  785. *
  786. * 2. Program the following fields in the global AHB configuration
  787. * register (USBC_GAHBCFG)
  788. * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
  789. * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
  790. * Nonperiodic TxFIFO empty level (slave mode only),
  791. * USBC_GAHBCFG[NPTXFEMPLVL]
  792. * Periodic TxFIFO empty level (slave mode only),
  793. * USBC_GAHBCFG[PTXFEMPLVL]
  794. * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
  795. */
  796. usbcx_gahbcfg.u32 = 0;
  797. usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
  798. CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
  799. usbcx_gahbcfg.s.hbstlen = 0;
  800. usbcx_gahbcfg.s.nptxfemplvl = 1;
  801. usbcx_gahbcfg.s.ptxfemplvl = 1;
  802. usbcx_gahbcfg.s.glblintrmsk = 1;
  803. cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
  804. usbcx_gahbcfg.u32);
  805. /*
  806. * 3. Program the following fields in USBC_GUSBCFG register.
  807. * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
  808. * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
  809. * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
  810. * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
  811. */
  812. usbcx_gusbcfg.u32 = cvmx_usb_read_csr32(usb,
  813. CVMX_USBCX_GUSBCFG(usb->index));
  814. usbcx_gusbcfg.s.toutcal = 0;
  815. usbcx_gusbcfg.s.ddrsel = 0;
  816. usbcx_gusbcfg.s.usbtrdtim = 0x5;
  817. usbcx_gusbcfg.s.phylpwrclksel = 0;
  818. cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
  819. usbcx_gusbcfg.u32);
  820. /*
  821. * 4. The software must unmask the following bits in the USBC_GINTMSK
  822. * register.
  823. * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
  824. * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
  825. */
  826. usbcx_gintmsk.u32 = cvmx_usb_read_csr32(usb,
  827. CVMX_USBCX_GINTMSK(usb->index));
  828. usbcx_gintmsk.s.otgintmsk = 1;
  829. usbcx_gintmsk.s.modemismsk = 1;
  830. usbcx_gintmsk.s.hchintmsk = 1;
  831. usbcx_gintmsk.s.sofmsk = 0;
  832. /* We need RX FIFO interrupts if we don't have DMA */
  833. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  834. usbcx_gintmsk.s.rxflvlmsk = 1;
  835. cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
  836. usbcx_gintmsk.u32);
  837. /*
  838. * Disable all channel interrupts. We'll enable them per channel later.
  839. */
  840. for (channel = 0; channel < 8; channel++)
  841. cvmx_usb_write_csr32(usb,
  842. CVMX_USBCX_HCINTMSKX(channel, usb->index),
  843. 0);
  844. /*
  845. * Host Port Initialization
  846. *
  847. * 1. Program the host-port interrupt-mask field to unmask,
  848. * USBC_GINTMSK[PRTINT] = 1
  849. */
  850. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  851. cvmx_usbcx_gintmsk, prtintmsk, 1);
  852. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  853. cvmx_usbcx_gintmsk, disconnintmsk, 1);
  854. /*
  855. * 2. Program the USBC_HCFG register to select full-speed host
  856. * or high-speed host.
  857. */
  858. usbcx_hcfg.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
  859. usbcx_hcfg.s.fslssupp = 0;
  860. usbcx_hcfg.s.fslspclksel = 0;
  861. cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
  862. cvmx_fifo_setup(usb);
  863. /*
  864. * If the controller is getting port events right after the reset, it
  865. * means the initialization failed. Try resetting the controller again
  866. * in such case. This is seen to happen after cold boot on DSR-1000N.
  867. */
  868. usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
  869. CVMX_USBCX_GINTSTS(usb->index));
  870. cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
  871. usbc_gintsts.u32);
  872. dev_dbg(dev, "gintsts after reset: 0x%x\n", (int)usbc_gintsts.u32);
  873. if (!usbc_gintsts.s.disconnint && !usbc_gintsts.s.prtint)
  874. return 0;
  875. if (retries++ >= 5)
  876. return -EAGAIN;
  877. dev_info(dev, "controller reset failed (gintsts=0x%x) - retrying\n",
  878. (int)usbc_gintsts.u32);
  879. msleep(50);
  880. cvmx_usb_shutdown(usb);
  881. msleep(50);
  882. goto retry;
  883. }
  884. /**
  885. * Reset a USB port. After this call succeeds, the USB port is
  886. * online and servicing requests.
  887. *
  888. * @usb: USB device state populated by cvmx_usb_initialize().
  889. */
  890. static void cvmx_usb_reset_port(struct octeon_hcd *usb)
  891. {
  892. usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
  893. CVMX_USBCX_HPRT(usb->index));
  894. /* Program the port reset bit to start the reset process */
  895. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
  896. prtrst, 1);
  897. /*
  898. * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
  899. * process to complete.
  900. */
  901. mdelay(50);
  902. /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
  903. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
  904. prtrst, 0);
  905. /*
  906. * Read the port speed field to get the enumerated speed,
  907. * USBC_HPRT[PRTSPD].
  908. */
  909. usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
  910. CVMX_USBCX_HPRT(usb->index));
  911. }
  912. /**
  913. * Disable a USB port. After this call the USB port will not
  914. * generate data transfers and will not generate events.
  915. * Transactions in process will fail and call their
  916. * associated callbacks.
  917. *
  918. * @usb: USB device state populated by cvmx_usb_initialize().
  919. *
  920. * Returns: 0 or a negative error code.
  921. */
  922. static int cvmx_usb_disable(struct octeon_hcd *usb)
  923. {
  924. /* Disable the port */
  925. USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
  926. prtena, 1);
  927. return 0;
  928. }
  929. /**
  930. * Get the current state of the USB port. Use this call to
  931. * determine if the usb port has anything connected, is enabled,
  932. * or has some sort of error condition. The return value of this
  933. * call has "changed" bits to signal of the value of some fields
  934. * have changed between calls.
  935. *
  936. * @usb: USB device state populated by cvmx_usb_initialize().
  937. *
  938. * Returns: Port status information
  939. */
  940. static struct cvmx_usb_port_status cvmx_usb_get_status(struct octeon_hcd *usb)
  941. {
  942. union cvmx_usbcx_hprt usbc_hprt;
  943. struct cvmx_usb_port_status result;
  944. memset(&result, 0, sizeof(result));
  945. usbc_hprt.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
  946. result.port_enabled = usbc_hprt.s.prtena;
  947. result.port_over_current = usbc_hprt.s.prtovrcurract;
  948. result.port_powered = usbc_hprt.s.prtpwr;
  949. result.port_speed = usbc_hprt.s.prtspd;
  950. result.connected = usbc_hprt.s.prtconnsts;
  951. result.connect_change =
  952. result.connected != usb->port_status.connected;
  953. return result;
  954. }
  955. /**
  956. * Open a virtual pipe between the host and a USB device. A pipe
  957. * must be opened before data can be transferred between a device
  958. * and Octeon.
  959. *
  960. * @usb: USB device state populated by cvmx_usb_initialize().
  961. * @device_addr:
  962. * USB device address to open the pipe to
  963. * (0-127).
  964. * @endpoint_num:
  965. * USB endpoint number to open the pipe to
  966. * (0-15).
  967. * @device_speed:
  968. * The speed of the device the pipe is going
  969. * to. This must match the device's speed,
  970. * which may be different than the port speed.
  971. * @max_packet: The maximum packet length the device can
  972. * transmit/receive (low speed=0-8, full
  973. * speed=0-1023, high speed=0-1024). This value
  974. * comes from the standard endpoint descriptor
  975. * field wMaxPacketSize bits <10:0>.
  976. * @transfer_type:
  977. * The type of transfer this pipe is for.
  978. * @transfer_dir:
  979. * The direction the pipe is in. This is not
  980. * used for control pipes.
  981. * @interval: For ISOCHRONOUS and INTERRUPT transfers,
  982. * this is how often the transfer is scheduled
  983. * for. All other transfers should specify
  984. * zero. The units are in frames (8000/sec at
  985. * high speed, 1000/sec for full speed).
  986. * @multi_count:
  987. * For high speed devices, this is the maximum
  988. * allowed number of packet per microframe.
  989. * Specify zero for non high speed devices. This
  990. * value comes from the standard endpoint descriptor
  991. * field wMaxPacketSize bits <12:11>.
  992. * @hub_device_addr:
  993. * Hub device address this device is connected
  994. * to. Devices connected directly to Octeon
  995. * use zero. This is only used when the device
  996. * is full/low speed behind a high speed hub.
  997. * The address will be of the high speed hub,
  998. * not and full speed hubs after it.
  999. * @hub_port: Which port on the hub the device is
  1000. * connected. Use zero for devices connected
  1001. * directly to Octeon. Like hub_device_addr,
  1002. * this is only used for full/low speed
  1003. * devices behind a high speed hub.
  1004. *
  1005. * Returns: A non-NULL value is a pipe. NULL means an error.
  1006. */
  1007. static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct octeon_hcd *usb,
  1008. int device_addr,
  1009. int endpoint_num,
  1010. enum cvmx_usb_speed
  1011. device_speed,
  1012. int max_packet,
  1013. enum cvmx_usb_transfer
  1014. transfer_type,
  1015. enum cvmx_usb_direction
  1016. transfer_dir,
  1017. int interval, int multi_count,
  1018. int hub_device_addr,
  1019. int hub_port)
  1020. {
  1021. struct cvmx_usb_pipe *pipe;
  1022. pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
  1023. if (!pipe)
  1024. return NULL;
  1025. if ((device_speed == CVMX_USB_SPEED_HIGH) &&
  1026. (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
  1027. (transfer_type == CVMX_USB_TRANSFER_BULK))
  1028. pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
  1029. pipe->device_addr = device_addr;
  1030. pipe->endpoint_num = endpoint_num;
  1031. pipe->device_speed = device_speed;
  1032. pipe->max_packet = max_packet;
  1033. pipe->transfer_type = transfer_type;
  1034. pipe->transfer_dir = transfer_dir;
  1035. INIT_LIST_HEAD(&pipe->transactions);
  1036. /*
  1037. * All pipes use interval to rate limit NAK processing. Force an
  1038. * interval if one wasn't supplied
  1039. */
  1040. if (!interval)
  1041. interval = 1;
  1042. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  1043. pipe->interval = interval * 8;
  1044. /* Force start splits to be schedule on uFrame 0 */
  1045. pipe->next_tx_frame = ((usb->frame_number + 7) & ~7) +
  1046. pipe->interval;
  1047. } else {
  1048. pipe->interval = interval;
  1049. pipe->next_tx_frame = usb->frame_number + pipe->interval;
  1050. }
  1051. pipe->multi_count = multi_count;
  1052. pipe->hub_device_addr = hub_device_addr;
  1053. pipe->hub_port = hub_port;
  1054. pipe->pid_toggle = 0;
  1055. pipe->split_sc_frame = -1;
  1056. list_add_tail(&pipe->node, &usb->idle_pipes);
  1057. /*
  1058. * We don't need to tell the hardware about this pipe yet since
  1059. * it doesn't have any submitted requests
  1060. */
  1061. return pipe;
  1062. }
  1063. /**
  1064. * Poll the RX FIFOs and remove data as needed. This function is only used
  1065. * in non DMA mode. It is very important that this function be called quickly
  1066. * enough to prevent FIFO overflow.
  1067. *
  1068. * @usb: USB device state populated by cvmx_usb_initialize().
  1069. */
  1070. static void cvmx_usb_poll_rx_fifo(struct octeon_hcd *usb)
  1071. {
  1072. union cvmx_usbcx_grxstsph rx_status;
  1073. int channel;
  1074. int bytes;
  1075. u64 address;
  1076. u32 *ptr;
  1077. rx_status.u32 = cvmx_usb_read_csr32(usb,
  1078. CVMX_USBCX_GRXSTSPH(usb->index));
  1079. /* Only read data if IN data is there */
  1080. if (rx_status.s.pktsts != 2)
  1081. return;
  1082. /* Check if no data is available */
  1083. if (!rx_status.s.bcnt)
  1084. return;
  1085. channel = rx_status.s.chnum;
  1086. bytes = rx_status.s.bcnt;
  1087. if (!bytes)
  1088. return;
  1089. /* Get where the DMA engine would have written this data */
  1090. address = cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
  1091. channel * 8);
  1092. ptr = cvmx_phys_to_ptr(address);
  1093. cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel * 8,
  1094. address + bytes);
  1095. /* Loop writing the FIFO data for this packet into memory */
  1096. while (bytes > 0) {
  1097. *ptr++ = cvmx_usb_read_csr32(usb,
  1098. USB_FIFO_ADDRESS(channel, usb->index));
  1099. bytes -= 4;
  1100. }
  1101. CVMX_SYNCW;
  1102. }
  1103. /**
  1104. * Fill the TX hardware fifo with data out of the software
  1105. * fifos
  1106. *
  1107. * @usb: USB device state populated by cvmx_usb_initialize().
  1108. * @fifo: Software fifo to use
  1109. * @available: Amount of space in the hardware fifo
  1110. *
  1111. * Returns: Non zero if the hardware fifo was too small and needs
  1112. * to be serviced again.
  1113. */
  1114. static int cvmx_usb_fill_tx_hw(struct octeon_hcd *usb,
  1115. struct cvmx_usb_tx_fifo *fifo, int available)
  1116. {
  1117. /*
  1118. * We're done either when there isn't anymore space or the software FIFO
  1119. * is empty
  1120. */
  1121. while (available && (fifo->head != fifo->tail)) {
  1122. int i = fifo->tail;
  1123. const u32 *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
  1124. u64 csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
  1125. usb->index) ^ 4;
  1126. int words = available;
  1127. /* Limit the amount of data to what the SW fifo has */
  1128. if (fifo->entry[i].size <= available) {
  1129. words = fifo->entry[i].size;
  1130. fifo->tail++;
  1131. if (fifo->tail > MAX_CHANNELS)
  1132. fifo->tail = 0;
  1133. }
  1134. /* Update the next locations and counts */
  1135. available -= words;
  1136. fifo->entry[i].address += words * 4;
  1137. fifo->entry[i].size -= words;
  1138. /*
  1139. * Write the HW fifo data. The read every three writes is due
  1140. * to an errata on CN3XXX chips
  1141. */
  1142. while (words > 3) {
  1143. cvmx_write64_uint32(csr_address, *ptr++);
  1144. cvmx_write64_uint32(csr_address, *ptr++);
  1145. cvmx_write64_uint32(csr_address, *ptr++);
  1146. cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  1147. words -= 3;
  1148. }
  1149. cvmx_write64_uint32(csr_address, *ptr++);
  1150. if (--words) {
  1151. cvmx_write64_uint32(csr_address, *ptr++);
  1152. if (--words)
  1153. cvmx_write64_uint32(csr_address, *ptr++);
  1154. }
  1155. cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
  1156. }
  1157. return fifo->head != fifo->tail;
  1158. }
  1159. /**
  1160. * Check the hardware FIFOs and fill them as needed
  1161. *
  1162. * @usb: USB device state populated by cvmx_usb_initialize().
  1163. */
  1164. static void cvmx_usb_poll_tx_fifo(struct octeon_hcd *usb)
  1165. {
  1166. if (usb->periodic.head != usb->periodic.tail) {
  1167. union cvmx_usbcx_hptxsts tx_status;
  1168. tx_status.u32 = cvmx_usb_read_csr32(usb,
  1169. CVMX_USBCX_HPTXSTS(usb->index));
  1170. if (cvmx_usb_fill_tx_hw(usb, &usb->periodic,
  1171. tx_status.s.ptxfspcavail))
  1172. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1173. cvmx_usbcx_gintmsk, ptxfempmsk, 1);
  1174. else
  1175. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1176. cvmx_usbcx_gintmsk, ptxfempmsk, 0);
  1177. }
  1178. if (usb->nonperiodic.head != usb->nonperiodic.tail) {
  1179. union cvmx_usbcx_gnptxsts tx_status;
  1180. tx_status.u32 = cvmx_usb_read_csr32(usb,
  1181. CVMX_USBCX_GNPTXSTS(usb->index));
  1182. if (cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
  1183. tx_status.s.nptxfspcavail))
  1184. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1185. cvmx_usbcx_gintmsk, nptxfempmsk, 1);
  1186. else
  1187. USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
  1188. cvmx_usbcx_gintmsk, nptxfempmsk, 0);
  1189. }
  1190. }
  1191. /**
  1192. * Fill the TX FIFO with an outgoing packet
  1193. *
  1194. * @usb: USB device state populated by cvmx_usb_initialize().
  1195. * @channel: Channel number to get packet from
  1196. */
  1197. static void cvmx_usb_fill_tx_fifo(struct octeon_hcd *usb, int channel)
  1198. {
  1199. union cvmx_usbcx_hccharx hcchar;
  1200. union cvmx_usbcx_hcspltx usbc_hcsplt;
  1201. union cvmx_usbcx_hctsizx usbc_hctsiz;
  1202. struct cvmx_usb_tx_fifo *fifo;
  1203. /* We only need to fill data on outbound channels */
  1204. hcchar.u32 = cvmx_usb_read_csr32(usb,
  1205. CVMX_USBCX_HCCHARX(channel, usb->index));
  1206. if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
  1207. return;
  1208. /* OUT Splits only have data on the start and not the complete */
  1209. usbc_hcsplt.u32 = cvmx_usb_read_csr32(usb,
  1210. CVMX_USBCX_HCSPLTX(channel, usb->index));
  1211. if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
  1212. return;
  1213. /*
  1214. * Find out how many bytes we need to fill and convert it into 32bit
  1215. * words.
  1216. */
  1217. usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
  1218. CVMX_USBCX_HCTSIZX(channel, usb->index));
  1219. if (!usbc_hctsiz.s.xfersize)
  1220. return;
  1221. if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
  1222. (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
  1223. fifo = &usb->periodic;
  1224. else
  1225. fifo = &usb->nonperiodic;
  1226. fifo->entry[fifo->head].channel = channel;
  1227. fifo->entry[fifo->head].address =
  1228. cvmx_read64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
  1229. channel * 8);
  1230. fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize + 3) >> 2;
  1231. fifo->head++;
  1232. if (fifo->head > MAX_CHANNELS)
  1233. fifo->head = 0;
  1234. cvmx_usb_poll_tx_fifo(usb);
  1235. }
  1236. /**
  1237. * Perform channel specific setup for Control transactions. All
  1238. * the generic stuff will already have been done in cvmx_usb_start_channel().
  1239. *
  1240. * @usb: USB device state populated by cvmx_usb_initialize().
  1241. * @channel: Channel to setup
  1242. * @pipe: Pipe for control transaction
  1243. */
  1244. static void cvmx_usb_start_channel_control(struct octeon_hcd *usb,
  1245. int channel,
  1246. struct cvmx_usb_pipe *pipe)
  1247. {
  1248. struct usb_hcd *hcd = octeon_to_hcd(usb);
  1249. struct device *dev = hcd->self.controller;
  1250. struct cvmx_usb_transaction *transaction =
  1251. list_first_entry(&pipe->transactions, typeof(*transaction),
  1252. node);
  1253. struct usb_ctrlrequest *header =
  1254. cvmx_phys_to_ptr(transaction->control_header);
  1255. int bytes_to_transfer = transaction->buffer_length -
  1256. transaction->actual_bytes;
  1257. int packets_to_transfer;
  1258. union cvmx_usbcx_hctsizx usbc_hctsiz;
  1259. usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
  1260. CVMX_USBCX_HCTSIZX(channel, usb->index));
  1261. switch (transaction->stage) {
  1262. case CVMX_USB_STAGE_NON_CONTROL:
  1263. case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
  1264. dev_err(dev, "%s: ERROR - Non control stage\n", __func__);
  1265. break;
  1266. case CVMX_USB_STAGE_SETUP:
  1267. usbc_hctsiz.s.pid = 3; /* Setup */
  1268. bytes_to_transfer = sizeof(*header);
  1269. /* All Control operations start with a setup going OUT */
  1270. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1271. cvmx_usbcx_hccharx, epdir,
  1272. CVMX_USB_DIRECTION_OUT);
  1273. /*
  1274. * Setup send the control header instead of the buffer data. The
  1275. * buffer data will be used in the next stage
  1276. */
  1277. cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
  1278. channel * 8,
  1279. transaction->control_header);
  1280. break;
  1281. case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
  1282. usbc_hctsiz.s.pid = 3; /* Setup */
  1283. bytes_to_transfer = 0;
  1284. /* All Control operations start with a setup going OUT */
  1285. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1286. cvmx_usbcx_hccharx, epdir,
  1287. CVMX_USB_DIRECTION_OUT);
  1288. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1289. cvmx_usbcx_hcspltx, compsplt, 1);
  1290. break;
  1291. case CVMX_USB_STAGE_DATA:
  1292. usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
  1293. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  1294. if (header->bRequestType & USB_DIR_IN)
  1295. bytes_to_transfer = 0;
  1296. else if (bytes_to_transfer > pipe->max_packet)
  1297. bytes_to_transfer = pipe->max_packet;
  1298. }
  1299. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1300. cvmx_usbcx_hccharx, epdir,
  1301. ((header->bRequestType & USB_DIR_IN) ?
  1302. CVMX_USB_DIRECTION_IN :
  1303. CVMX_USB_DIRECTION_OUT));
  1304. break;
  1305. case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
  1306. usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
  1307. if (!(header->bRequestType & USB_DIR_IN))
  1308. bytes_to_transfer = 0;
  1309. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1310. cvmx_usbcx_hccharx, epdir,
  1311. ((header->bRequestType & USB_DIR_IN) ?
  1312. CVMX_USB_DIRECTION_IN :
  1313. CVMX_USB_DIRECTION_OUT));
  1314. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1315. cvmx_usbcx_hcspltx, compsplt, 1);
  1316. break;
  1317. case CVMX_USB_STAGE_STATUS:
  1318. usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
  1319. bytes_to_transfer = 0;
  1320. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1321. cvmx_usbcx_hccharx, epdir,
  1322. ((header->bRequestType & USB_DIR_IN) ?
  1323. CVMX_USB_DIRECTION_OUT :
  1324. CVMX_USB_DIRECTION_IN));
  1325. break;
  1326. case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
  1327. usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
  1328. bytes_to_transfer = 0;
  1329. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1330. cvmx_usbcx_hccharx, epdir,
  1331. ((header->bRequestType & USB_DIR_IN) ?
  1332. CVMX_USB_DIRECTION_OUT :
  1333. CVMX_USB_DIRECTION_IN));
  1334. USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
  1335. cvmx_usbcx_hcspltx, compsplt, 1);
  1336. break;
  1337. }
  1338. /*
  1339. * Make sure the transfer never exceeds the byte limit of the hardware.
  1340. * Further bytes will be sent as continued transactions
  1341. */
  1342. if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
  1343. /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
  1344. bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
  1345. bytes_to_transfer *= pipe->max_packet;
  1346. }
  1347. /*
  1348. * Calculate the number of packets to transfer. If the length is zero
  1349. * we still need to transfer one packet
  1350. */
  1351. packets_to_transfer = DIV_ROUND_UP(bytes_to_transfer,
  1352. pipe->max_packet);
  1353. if (packets_to_transfer == 0) {
  1354. packets_to_transfer = 1;
  1355. } else if ((packets_to_transfer > 1) &&
  1356. (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
  1357. /*
  1358. * Limit to one packet when not using DMA. Channels must be
  1359. * restarted between every packet for IN transactions, so there
  1360. * is no reason to do multiple packets in a row
  1361. */
  1362. packets_to_transfer = 1;
  1363. bytes_to_transfer = packets_to_transfer * pipe->max_packet;
  1364. } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
  1365. /*
  1366. * Limit the number of packet and data transferred to what the
  1367. * hardware can handle
  1368. */
  1369. packets_to_transfer = MAX_TRANSFER_PACKETS;
  1370. bytes_to_transfer = packets_to_transfer * pipe->max_packet;
  1371. }
  1372. usbc_hctsiz.s.xfersize = bytes_to_transfer;
  1373. usbc_hctsiz.s.pktcnt = packets_to_transfer;
  1374. cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
  1375. usbc_hctsiz.u32);
  1376. }
  1377. /**
  1378. * Start a channel to perform the pipe's head transaction
  1379. *
  1380. * @usb: USB device state populated by cvmx_usb_initialize().
  1381. * @channel: Channel to setup
  1382. * @pipe: Pipe to start
  1383. */
  1384. static void cvmx_usb_start_channel(struct octeon_hcd *usb, int channel,
  1385. struct cvmx_usb_pipe *pipe)
  1386. {
  1387. struct cvmx_usb_transaction *transaction =
  1388. list_first_entry(&pipe->transactions, typeof(*transaction),
  1389. node);
  1390. /* Make sure all writes to the DMA region get flushed */
  1391. CVMX_SYNCW;
  1392. /* Attach the channel to the pipe */
  1393. usb->pipe_for_channel[channel] = pipe;
  1394. pipe->channel = channel;
  1395. pipe->flags |= CVMX_USB_PIPE_FLAGS_SCHEDULED;
  1396. /* Mark this channel as in use */
  1397. usb->idle_hardware_channels &= ~(1 << channel);
  1398. /* Enable the channel interrupt bits */
  1399. {
  1400. union cvmx_usbcx_hcintx usbc_hcint;
  1401. union cvmx_usbcx_hcintmskx usbc_hcintmsk;
  1402. union cvmx_usbcx_haintmsk usbc_haintmsk;
  1403. /* Clear all channel status bits */
  1404. usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
  1405. CVMX_USBCX_HCINTX(channel, usb->index));
  1406. cvmx_usb_write_csr32(usb,
  1407. CVMX_USBCX_HCINTX(channel, usb->index),
  1408. usbc_hcint.u32);
  1409. usbc_hcintmsk.u32 = 0;
  1410. usbc_hcintmsk.s.chhltdmsk = 1;
  1411. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
  1412. /*
  1413. * Channels need these extra interrupts when we aren't
  1414. * in DMA mode.
  1415. */
  1416. usbc_hcintmsk.s.datatglerrmsk = 1;
  1417. usbc_hcintmsk.s.frmovrunmsk = 1;
  1418. usbc_hcintmsk.s.bblerrmsk = 1;
  1419. usbc_hcintmsk.s.xacterrmsk = 1;
  1420. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  1421. /*
  1422. * Splits don't generate xfercompl, so we need
  1423. * ACK and NYET.
  1424. */
  1425. usbc_hcintmsk.s.nyetmsk = 1;
  1426. usbc_hcintmsk.s.ackmsk = 1;
  1427. }
  1428. usbc_hcintmsk.s.nakmsk = 1;
  1429. usbc_hcintmsk.s.stallmsk = 1;
  1430. usbc_hcintmsk.s.xfercomplmsk = 1;
  1431. }
  1432. cvmx_usb_write_csr32(usb,
  1433. CVMX_USBCX_HCINTMSKX(channel, usb->index),
  1434. usbc_hcintmsk.u32);
  1435. /* Enable the channel interrupt to propagate */
  1436. usbc_haintmsk.u32 = cvmx_usb_read_csr32(usb,
  1437. CVMX_USBCX_HAINTMSK(usb->index));
  1438. usbc_haintmsk.s.haintmsk |= 1 << channel;
  1439. cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index),
  1440. usbc_haintmsk.u32);
  1441. }
  1442. /* Setup the location the DMA engine uses. */
  1443. {
  1444. u64 reg;
  1445. u64 dma_address = transaction->buffer +
  1446. transaction->actual_bytes;
  1447. if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
  1448. dma_address = transaction->buffer +
  1449. transaction->iso_packets[0].offset +
  1450. transaction->actual_bytes;
  1451. if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
  1452. reg = CVMX_USBNX_DMA0_OUTB_CHN0(usb->index);
  1453. else
  1454. reg = CVMX_USBNX_DMA0_INB_CHN0(usb->index);
  1455. cvmx_write64_uint64(reg + channel * 8, dma_address);
  1456. }
  1457. /* Setup both the size of the transfer and the SPLIT characteristics */
  1458. {
  1459. union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
  1460. union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
  1461. int packets_to_transfer;
  1462. int bytes_to_transfer = transaction->buffer_length -
  1463. transaction->actual_bytes;
  1464. /*
  1465. * ISOCHRONOUS transactions store each individual transfer size
  1466. * in the packet structure, not the global buffer_length
  1467. */
  1468. if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
  1469. bytes_to_transfer =
  1470. transaction->iso_packets[0].length -
  1471. transaction->actual_bytes;
  1472. /*
  1473. * We need to do split transactions when we are talking to non
  1474. * high speed devices that are behind a high speed hub
  1475. */
  1476. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  1477. /*
  1478. * On the start split phase (stage is even) record the
  1479. * frame number we will need to send the split complete.
  1480. * We only store the lower two bits since the time ahead
  1481. * can only be two frames
  1482. */
  1483. if ((transaction->stage & 1) == 0) {
  1484. if (transaction->type == CVMX_USB_TRANSFER_BULK)
  1485. pipe->split_sc_frame =
  1486. (usb->frame_number + 1) & 0x7f;
  1487. else
  1488. pipe->split_sc_frame =
  1489. (usb->frame_number + 2) & 0x7f;
  1490. } else {
  1491. pipe->split_sc_frame = -1;
  1492. }
  1493. usbc_hcsplt.s.spltena = 1;
  1494. usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
  1495. usbc_hcsplt.s.prtaddr = pipe->hub_port;
  1496. usbc_hcsplt.s.compsplt = (transaction->stage ==
  1497. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
  1498. /*
  1499. * SPLIT transactions can only ever transmit one data
  1500. * packet so limit the transfer size to the max packet
  1501. * size
  1502. */
  1503. if (bytes_to_transfer > pipe->max_packet)
  1504. bytes_to_transfer = pipe->max_packet;
  1505. /*
  1506. * ISOCHRONOUS OUT splits are unique in that they limit
  1507. * data transfers to 188 byte chunks representing the
  1508. * begin/middle/end of the data or all
  1509. */
  1510. if (!usbc_hcsplt.s.compsplt &&
  1511. (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
  1512. (pipe->transfer_type ==
  1513. CVMX_USB_TRANSFER_ISOCHRONOUS)) {
  1514. /*
  1515. * Clear the split complete frame number as
  1516. * there isn't going to be a split complete
  1517. */
  1518. pipe->split_sc_frame = -1;
  1519. /*
  1520. * See if we've started this transfer and sent
  1521. * data
  1522. */
  1523. if (transaction->actual_bytes == 0) {
  1524. /*
  1525. * Nothing sent yet, this is either a
  1526. * begin or the entire payload
  1527. */
  1528. if (bytes_to_transfer <= 188)
  1529. /* Entire payload in one go */
  1530. usbc_hcsplt.s.xactpos = 3;
  1531. else
  1532. /* First part of payload */
  1533. usbc_hcsplt.s.xactpos = 2;
  1534. } else {
  1535. /*
  1536. * Continuing the previous data, we must
  1537. * either be in the middle or at the end
  1538. */
  1539. if (bytes_to_transfer <= 188)
  1540. /* End of payload */
  1541. usbc_hcsplt.s.xactpos = 1;
  1542. else
  1543. /* Middle of payload */
  1544. usbc_hcsplt.s.xactpos = 0;
  1545. }
  1546. /*
  1547. * Again, the transfer size is limited to 188
  1548. * bytes
  1549. */
  1550. if (bytes_to_transfer > 188)
  1551. bytes_to_transfer = 188;
  1552. }
  1553. }
  1554. /*
  1555. * Make sure the transfer never exceeds the byte limit of the
  1556. * hardware. Further bytes will be sent as continued
  1557. * transactions
  1558. */
  1559. if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
  1560. /*
  1561. * Round MAX_TRANSFER_BYTES to a multiple of out packet
  1562. * size
  1563. */
  1564. bytes_to_transfer = MAX_TRANSFER_BYTES /
  1565. pipe->max_packet;
  1566. bytes_to_transfer *= pipe->max_packet;
  1567. }
  1568. /*
  1569. * Calculate the number of packets to transfer. If the length is
  1570. * zero we still need to transfer one packet
  1571. */
  1572. packets_to_transfer =
  1573. DIV_ROUND_UP(bytes_to_transfer, pipe->max_packet);
  1574. if (packets_to_transfer == 0) {
  1575. packets_to_transfer = 1;
  1576. } else if ((packets_to_transfer > 1) &&
  1577. (usb->init_flags &
  1578. CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
  1579. /*
  1580. * Limit to one packet when not using DMA. Channels must
  1581. * be restarted between every packet for IN
  1582. * transactions, so there is no reason to do multiple
  1583. * packets in a row
  1584. */
  1585. packets_to_transfer = 1;
  1586. bytes_to_transfer = packets_to_transfer *
  1587. pipe->max_packet;
  1588. } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
  1589. /*
  1590. * Limit the number of packet and data transferred to
  1591. * what the hardware can handle
  1592. */
  1593. packets_to_transfer = MAX_TRANSFER_PACKETS;
  1594. bytes_to_transfer = packets_to_transfer *
  1595. pipe->max_packet;
  1596. }
  1597. usbc_hctsiz.s.xfersize = bytes_to_transfer;
  1598. usbc_hctsiz.s.pktcnt = packets_to_transfer;
  1599. /* Update the DATA0/DATA1 toggle */
  1600. usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
  1601. /*
  1602. * High speed pipes may need a hardware ping before they start
  1603. */
  1604. if (pipe->flags & CVMX_USB_PIPE_FLAGS_NEED_PING)
  1605. usbc_hctsiz.s.dopng = 1;
  1606. cvmx_usb_write_csr32(usb,
  1607. CVMX_USBCX_HCSPLTX(channel, usb->index),
  1608. usbc_hcsplt.u32);
  1609. cvmx_usb_write_csr32(usb,
  1610. CVMX_USBCX_HCTSIZX(channel, usb->index),
  1611. usbc_hctsiz.u32);
  1612. }
  1613. /* Setup the Host Channel Characteristics Register */
  1614. {
  1615. union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
  1616. /*
  1617. * Set the startframe odd/even properly. This is only used for
  1618. * periodic
  1619. */
  1620. usbc_hcchar.s.oddfrm = usb->frame_number & 1;
  1621. /*
  1622. * Set the number of back to back packets allowed by this
  1623. * endpoint. Split transactions interpret "ec" as the number of
  1624. * immediate retries of failure. These retries happen too
  1625. * quickly, so we disable these entirely for splits
  1626. */
  1627. if (cvmx_usb_pipe_needs_split(usb, pipe))
  1628. usbc_hcchar.s.ec = 1;
  1629. else if (pipe->multi_count < 1)
  1630. usbc_hcchar.s.ec = 1;
  1631. else if (pipe->multi_count > 3)
  1632. usbc_hcchar.s.ec = 3;
  1633. else
  1634. usbc_hcchar.s.ec = pipe->multi_count;
  1635. /* Set the rest of the endpoint specific settings */
  1636. usbc_hcchar.s.devaddr = pipe->device_addr;
  1637. usbc_hcchar.s.eptype = transaction->type;
  1638. usbc_hcchar.s.lspddev =
  1639. (pipe->device_speed == CVMX_USB_SPEED_LOW);
  1640. usbc_hcchar.s.epdir = pipe->transfer_dir;
  1641. usbc_hcchar.s.epnum = pipe->endpoint_num;
  1642. usbc_hcchar.s.mps = pipe->max_packet;
  1643. cvmx_usb_write_csr32(usb,
  1644. CVMX_USBCX_HCCHARX(channel, usb->index),
  1645. usbc_hcchar.u32);
  1646. }
  1647. /* Do transaction type specific fixups as needed */
  1648. switch (transaction->type) {
  1649. case CVMX_USB_TRANSFER_CONTROL:
  1650. cvmx_usb_start_channel_control(usb, channel, pipe);
  1651. break;
  1652. case CVMX_USB_TRANSFER_BULK:
  1653. case CVMX_USB_TRANSFER_INTERRUPT:
  1654. break;
  1655. case CVMX_USB_TRANSFER_ISOCHRONOUS:
  1656. if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
  1657. /*
  1658. * ISO transactions require different PIDs depending on
  1659. * direction and how many packets are needed
  1660. */
  1661. if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
  1662. if (pipe->multi_count < 2) /* Need DATA0 */
  1663. USB_SET_FIELD32(
  1664. CVMX_USBCX_HCTSIZX(channel,
  1665. usb->index),
  1666. cvmx_usbcx_hctsizx, pid, 0);
  1667. else /* Need MDATA */
  1668. USB_SET_FIELD32(
  1669. CVMX_USBCX_HCTSIZX(channel,
  1670. usb->index),
  1671. cvmx_usbcx_hctsizx, pid, 3);
  1672. }
  1673. }
  1674. break;
  1675. }
  1676. {
  1677. union cvmx_usbcx_hctsizx usbc_hctsiz = { .u32 =
  1678. cvmx_usb_read_csr32(usb,
  1679. CVMX_USBCX_HCTSIZX(channel,
  1680. usb->index))
  1681. };
  1682. transaction->xfersize = usbc_hctsiz.s.xfersize;
  1683. transaction->pktcnt = usbc_hctsiz.s.pktcnt;
  1684. }
  1685. /* Remember when we start a split transaction */
  1686. if (cvmx_usb_pipe_needs_split(usb, pipe))
  1687. usb->active_split = transaction;
  1688. USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
  1689. cvmx_usbcx_hccharx, chena, 1);
  1690. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
  1691. cvmx_usb_fill_tx_fifo(usb, channel);
  1692. }
  1693. /**
  1694. * Find a pipe that is ready to be scheduled to hardware.
  1695. * @usb: USB device state populated by cvmx_usb_initialize().
  1696. * @xfer_type: Transfer type
  1697. *
  1698. * Returns: Pipe or NULL if none are ready
  1699. */
  1700. static struct cvmx_usb_pipe *cvmx_usb_find_ready_pipe(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,
  2137. usb->index));
  2138. /*
  2139. * If the channel isn't enabled then the transaction already
  2140. * completed.
  2141. */
  2142. if (usbc_hcchar.s.chena) {
  2143. usbc_hcchar.s.chdis = 1;
  2144. cvmx_usb_write_csr32(usb,
  2145. CVMX_USBCX_HCCHARX(pipe->channel,
  2146. usb->index),
  2147. usbc_hcchar.u32);
  2148. }
  2149. }
  2150. cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_CANCEL);
  2151. return 0;
  2152. }
  2153. /**
  2154. * Cancel all outstanding requests in a pipe. Logically all this
  2155. * does is call cvmx_usb_cancel() in a loop.
  2156. *
  2157. * @usb: USB device state populated by cvmx_usb_initialize().
  2158. * @pipe: Pipe to cancel requests in.
  2159. *
  2160. * Returns: 0 or a negative error code.
  2161. */
  2162. static int cvmx_usb_cancel_all(struct octeon_hcd *usb,
  2163. struct cvmx_usb_pipe *pipe)
  2164. {
  2165. struct cvmx_usb_transaction *transaction, *next;
  2166. /* Simply loop through and attempt to cancel each transaction */
  2167. list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
  2168. int result = cvmx_usb_cancel(usb, pipe, transaction);
  2169. if (unlikely(result != 0))
  2170. return result;
  2171. }
  2172. return 0;
  2173. }
  2174. /**
  2175. * Close a pipe created with cvmx_usb_open_pipe().
  2176. *
  2177. * @usb: USB device state populated by cvmx_usb_initialize().
  2178. * @pipe: Pipe to close.
  2179. *
  2180. * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
  2181. * outstanding transfers.
  2182. */
  2183. static int cvmx_usb_close_pipe(struct octeon_hcd *usb,
  2184. struct cvmx_usb_pipe *pipe)
  2185. {
  2186. /* Fail if the pipe has pending transactions */
  2187. if (!list_empty(&pipe->transactions))
  2188. return -EBUSY;
  2189. list_del(&pipe->node);
  2190. kfree(pipe);
  2191. return 0;
  2192. }
  2193. /**
  2194. * Get the current USB protocol level frame number. The frame
  2195. * number is always in the range of 0-0x7ff.
  2196. *
  2197. * @usb: USB device state populated by cvmx_usb_initialize().
  2198. *
  2199. * Returns: USB frame number
  2200. */
  2201. static int cvmx_usb_get_frame_number(struct octeon_hcd *usb)
  2202. {
  2203. union cvmx_usbcx_hfnum usbc_hfnum;
  2204. usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
  2205. return usbc_hfnum.s.frnum;
  2206. }
  2207. static void cvmx_usb_transfer_control(struct octeon_hcd *usb,
  2208. struct cvmx_usb_pipe *pipe,
  2209. struct cvmx_usb_transaction *transaction,
  2210. union cvmx_usbcx_hccharx usbc_hcchar,
  2211. int buffer_space_left,
  2212. int bytes_in_last_packet)
  2213. {
  2214. switch (transaction->stage) {
  2215. case CVMX_USB_STAGE_NON_CONTROL:
  2216. case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
  2217. /* This should be impossible */
  2218. cvmx_usb_complete(usb, pipe, transaction,
  2219. CVMX_USB_STATUS_ERROR);
  2220. break;
  2221. case CVMX_USB_STAGE_SETUP:
  2222. pipe->pid_toggle = 1;
  2223. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  2224. transaction->stage =
  2225. CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
  2226. } else {
  2227. struct usb_ctrlrequest *header =
  2228. cvmx_phys_to_ptr(transaction->control_header);
  2229. if (header->wLength)
  2230. transaction->stage = CVMX_USB_STAGE_DATA;
  2231. else
  2232. transaction->stage = CVMX_USB_STAGE_STATUS;
  2233. }
  2234. break;
  2235. case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
  2236. {
  2237. struct usb_ctrlrequest *header =
  2238. cvmx_phys_to_ptr(transaction->control_header);
  2239. if (header->wLength)
  2240. transaction->stage = CVMX_USB_STAGE_DATA;
  2241. else
  2242. transaction->stage = CVMX_USB_STAGE_STATUS;
  2243. }
  2244. break;
  2245. case CVMX_USB_STAGE_DATA:
  2246. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  2247. transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
  2248. /*
  2249. * For setup OUT data that are splits,
  2250. * the hardware doesn't appear to count
  2251. * transferred data. Here we manually
  2252. * update the data transferred
  2253. */
  2254. if (!usbc_hcchar.s.epdir) {
  2255. if (buffer_space_left < pipe->max_packet)
  2256. transaction->actual_bytes +=
  2257. buffer_space_left;
  2258. else
  2259. transaction->actual_bytes +=
  2260. pipe->max_packet;
  2261. }
  2262. } else if ((buffer_space_left == 0) ||
  2263. (bytes_in_last_packet < pipe->max_packet)) {
  2264. pipe->pid_toggle = 1;
  2265. transaction->stage = CVMX_USB_STAGE_STATUS;
  2266. }
  2267. break;
  2268. case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
  2269. if ((buffer_space_left == 0) ||
  2270. (bytes_in_last_packet < pipe->max_packet)) {
  2271. pipe->pid_toggle = 1;
  2272. transaction->stage = CVMX_USB_STAGE_STATUS;
  2273. } else {
  2274. transaction->stage = CVMX_USB_STAGE_DATA;
  2275. }
  2276. break;
  2277. case CVMX_USB_STAGE_STATUS:
  2278. if (cvmx_usb_pipe_needs_split(usb, pipe))
  2279. transaction->stage =
  2280. CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
  2281. else
  2282. cvmx_usb_complete(usb, pipe, transaction,
  2283. CVMX_USB_STATUS_OK);
  2284. break;
  2285. case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
  2286. cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
  2287. break;
  2288. }
  2289. }
  2290. static void cvmx_usb_transfer_bulk(struct octeon_hcd *usb,
  2291. struct cvmx_usb_pipe *pipe,
  2292. struct cvmx_usb_transaction *transaction,
  2293. union cvmx_usbcx_hcintx usbc_hcint,
  2294. int buffer_space_left,
  2295. int bytes_in_last_packet)
  2296. {
  2297. /*
  2298. * The only time a bulk transfer isn't complete when it finishes with
  2299. * an ACK is during a split transaction. For splits we need to continue
  2300. * the transfer if more data is needed.
  2301. */
  2302. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  2303. if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
  2304. transaction->stage =
  2305. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
  2306. else if (buffer_space_left &&
  2307. (bytes_in_last_packet == pipe->max_packet))
  2308. transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
  2309. else
  2310. cvmx_usb_complete(usb, pipe, transaction,
  2311. CVMX_USB_STATUS_OK);
  2312. } else {
  2313. if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
  2314. (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
  2315. (usbc_hcint.s.nak))
  2316. pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
  2317. if (!buffer_space_left ||
  2318. (bytes_in_last_packet < pipe->max_packet))
  2319. cvmx_usb_complete(usb, pipe, transaction,
  2320. CVMX_USB_STATUS_OK);
  2321. }
  2322. }
  2323. static void cvmx_usb_transfer_intr(struct octeon_hcd *usb,
  2324. struct cvmx_usb_pipe *pipe,
  2325. struct cvmx_usb_transaction *transaction,
  2326. int buffer_space_left,
  2327. int bytes_in_last_packet)
  2328. {
  2329. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  2330. if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL) {
  2331. transaction->stage =
  2332. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
  2333. } else if (buffer_space_left &&
  2334. (bytes_in_last_packet == pipe->max_packet)) {
  2335. transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
  2336. } else {
  2337. pipe->next_tx_frame += pipe->interval;
  2338. cvmx_usb_complete(usb, pipe, transaction,
  2339. CVMX_USB_STATUS_OK);
  2340. }
  2341. } else if (!buffer_space_left ||
  2342. (bytes_in_last_packet < pipe->max_packet)) {
  2343. pipe->next_tx_frame += pipe->interval;
  2344. cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
  2345. }
  2346. }
  2347. static void cvmx_usb_transfer_isoc(struct octeon_hcd *usb,
  2348. struct cvmx_usb_pipe *pipe,
  2349. struct cvmx_usb_transaction *transaction,
  2350. int buffer_space_left,
  2351. int bytes_in_last_packet,
  2352. int bytes_this_transfer)
  2353. {
  2354. if (cvmx_usb_pipe_needs_split(usb, pipe)) {
  2355. /*
  2356. * ISOCHRONOUS OUT splits don't require a complete split stage.
  2357. * Instead they use a sequence of begin OUT splits to transfer
  2358. * the data 188 bytes at a time. Once the transfer is complete,
  2359. * the pipe sleeps until the next schedule interval.
  2360. */
  2361. if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
  2362. /*
  2363. * If no space left or this wasn't a max size packet
  2364. * then this transfer is complete. Otherwise start it
  2365. * again to send the next 188 bytes
  2366. */
  2367. if (!buffer_space_left || (bytes_this_transfer < 188)) {
  2368. pipe->next_tx_frame += pipe->interval;
  2369. cvmx_usb_complete(usb, pipe, transaction,
  2370. CVMX_USB_STATUS_OK);
  2371. }
  2372. return;
  2373. }
  2374. if (transaction->stage ==
  2375. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
  2376. /*
  2377. * We are in the incoming data phase. Keep getting data
  2378. * until we run out of space or get a small packet
  2379. */
  2380. if ((buffer_space_left == 0) ||
  2381. (bytes_in_last_packet < pipe->max_packet)) {
  2382. pipe->next_tx_frame += pipe->interval;
  2383. cvmx_usb_complete(usb, pipe, transaction,
  2384. CVMX_USB_STATUS_OK);
  2385. }
  2386. } else {
  2387. transaction->stage =
  2388. CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
  2389. }
  2390. } else {
  2391. pipe->next_tx_frame += pipe->interval;
  2392. cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
  2393. }
  2394. }
  2395. /**
  2396. * Poll a channel for status
  2397. *
  2398. * @usb: USB device
  2399. * @channel: Channel to poll
  2400. *
  2401. * Returns: Zero on success
  2402. */
  2403. static int cvmx_usb_poll_channel(struct octeon_hcd *usb, int channel)
  2404. {
  2405. struct usb_hcd *hcd = octeon_to_hcd(usb);
  2406. struct device *dev = hcd->self.controller;
  2407. union cvmx_usbcx_hcintx usbc_hcint;
  2408. union cvmx_usbcx_hctsizx usbc_hctsiz;
  2409. union cvmx_usbcx_hccharx usbc_hcchar;
  2410. struct cvmx_usb_pipe *pipe;
  2411. struct cvmx_usb_transaction *transaction;
  2412. int bytes_this_transfer;
  2413. int bytes_in_last_packet;
  2414. int packets_processed;
  2415. int buffer_space_left;
  2416. /* Read the interrupt status bits for the channel */
  2417. usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
  2418. CVMX_USBCX_HCINTX(channel, usb->index));
  2419. if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
  2420. usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
  2421. CVMX_USBCX_HCCHARX(channel,
  2422. 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 (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_DMA | 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. i = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
  3339. if (i)
  3340. return i;
  3341. /*
  3342. * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
  3343. * IOB priority registers. Under heavy network load USB
  3344. * hardware can be starved by the IOB causing a crash. Give
  3345. * it a priority boost if it has been waiting more than 400
  3346. * cycles to avoid this situation.
  3347. *
  3348. * Testing indicates that a cnt_val of 8192 is not sufficient,
  3349. * but no failures are seen with 4096. We choose a value of
  3350. * 400 to give a safety factor of 10.
  3351. */
  3352. if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
  3353. union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
  3354. pri_cnt.u64 = 0;
  3355. pri_cnt.s.cnt_enb = 1;
  3356. pri_cnt.s.cnt_val = 400;
  3357. cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
  3358. }
  3359. hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
  3360. if (!hcd) {
  3361. dev_dbg(dev, "Failed to allocate memory for HCD\n");
  3362. return -1;
  3363. }
  3364. hcd->uses_new_polling = 1;
  3365. usb = (struct octeon_hcd *)hcd->hcd_priv;
  3366. spin_lock_init(&usb->lock);
  3367. usb->init_flags = initialize_flags;
  3368. /* Initialize the USB state structure */
  3369. usb->index = usb_num;
  3370. INIT_LIST_HEAD(&usb->idle_pipes);
  3371. for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
  3372. INIT_LIST_HEAD(&usb->active_pipes[i]);
  3373. /* Due to an errata, CN31XX doesn't support DMA */
  3374. if (OCTEON_IS_MODEL(OCTEON_CN31XX)) {
  3375. usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
  3376. /* Only use one channel with non DMA */
  3377. usb->idle_hardware_channels = 0x1;
  3378. } else if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
  3379. /* CN5XXX have an errata with channel 3 */
  3380. usb->idle_hardware_channels = 0xf7;
  3381. } else {
  3382. usb->idle_hardware_channels = 0xff;
  3383. }
  3384. status = cvmx_usb_initialize(dev, usb);
  3385. if (status) {
  3386. dev_dbg(dev, "USB initialization failed with %d\n", status);
  3387. usb_put_hcd(hcd);
  3388. return -1;
  3389. }
  3390. status = usb_add_hcd(hcd, irq, 0);
  3391. if (status) {
  3392. dev_dbg(dev, "USB add HCD failed with %d\n", status);
  3393. usb_put_hcd(hcd);
  3394. return -1;
  3395. }
  3396. device_wakeup_enable(hcd->self.controller);
  3397. dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
  3398. return 0;
  3399. }
  3400. static void octeon_usb_remove(struct platform_device *pdev)
  3401. {
  3402. int status;
  3403. struct device *dev = &pdev->dev;
  3404. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3405. struct octeon_hcd *usb = hcd_to_octeon(hcd);
  3406. unsigned long flags;
  3407. usb_remove_hcd(hcd);
  3408. spin_lock_irqsave(&usb->lock, flags);
  3409. status = cvmx_usb_shutdown(usb);
  3410. spin_unlock_irqrestore(&usb->lock, flags);
  3411. if (status)
  3412. dev_dbg(dev, "USB shutdown failed with %d\n", status);
  3413. usb_put_hcd(hcd);
  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_new = 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.");