octeon-hcd.c 110 KB

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