octeon-hcd.c 110 KB

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