usb_otg_aotg.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056
  1. /*
  2. * Copyright (c) 2020 Actions Corporation.
  3. * Author: Jinang Lv <lvjinang@actions-semi.com>
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. /**
  8. * @file
  9. * @brief Actions USB OTG controller driver
  10. *
  11. * Actions USB OTG controller driver. The driver implements
  12. * the low level control routines to deal directly with the hardware.
  13. */
  14. #include <kernel.h>
  15. #include <sys/sys_io.h>
  16. #include <board.h>
  17. #include <init.h>
  18. #include <drivers/dma.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <sys/byteorder.h>
  23. #include <devicetree.h>
  24. #include <drivers/usb/usb_dc.h>
  25. #include <drivers/usb/usb_hc.h>
  26. #include <drivers/usb/usb_phy.h>
  27. #include <soc_clock.h>
  28. #include <soc_reset.h>
  29. #include <soc_pmu.h>
  30. #include "usb_otg_aotg.h"
  31. #include <logging/log.h>
  32. #define LOG_LEVEL CONFIG_SYS_LOG_USB_OTG_DRIVER_LEVEL
  33. LOG_MODULE_REGISTER(usb_otg_aotg);
  34. #define DMA_ID_USB0 4
  35. #define DMA_IRQ_TC (0) /* DMA completion flag */
  36. #define DMA_IRQ_HF (1) /* DMA half-full flag */
  37. #define USB_AOTG_DMA_BURST8_LEN 32
  38. #define USB_AOTG_DMA_MAX_SIZE (128 * 1024)
  39. #define USB_AOTG_DMA_MAX_LEN (64 * 1024)
  40. #ifdef CONFIG_USB_AOTG_UDC_DMA
  41. #define USB_EP_OUT_DMA_CAP(ep_idx) (ep_idx == USB_AOTG_OUT_EP_2)
  42. #define USB_EP_IN_DMA_CAP(ep_idx) (ep_idx == USB_AOTG_IN_EP_1)
  43. #else
  44. #define USB_EP_OUT_DMA_CAP(ep) (false)
  45. #define USB_EP_IN_DMA_CAP(ep) (false)
  46. #endif
  47. /*
  48. * USB device controller endpoint private structure.
  49. */
  50. struct aotg_dc_ep_ctrl_prv {
  51. uint8_t ep_ena; /* Endpoint Enabled */
  52. uint8_t ep_type;
  53. uint16_t mps; /* Endpoint Max packet size */
  54. usb_dc_ep_callback cb; /* Endpoint callback function */
  55. /*
  56. * "data_len" has different meanings in different cases.
  57. *
  58. * for ep0-in: set in setup phase.
  59. * for ep0-out: set in setup or data phase.
  60. * for epX-in legacy mode: not used.
  61. * for epX-in new mode: set by upper layer, update in epin interrupt.
  62. * for epX-in DMA: set by upper layer to help if need to set busy.
  63. * for epX-out: set in epout interrupt.
  64. * for epX-out async: set by upper layer, update in epout interrupt
  65. * for epX-out DMA: set by upper layer.
  66. */
  67. uint32_t data_len;
  68. uint32_t actual; /* actual length: only for epX-out async */
  69. /*
  70. * "data" has different meanings in different cases.
  71. *
  72. * for ep0-in: not used.
  73. * for ep0-out: not used.
  74. * for epX-in legacy mode: not used.
  75. * for epX-in new mode: set by upper layer, update in epin interrupt.
  76. * for epX-out: not used.
  77. * for epX-out async: set by upper layer, update in epout interrupt.
  78. */
  79. uint8_t *data;
  80. uint8_t multi; /* multi-fifo */
  81. };
  82. /*
  83. * USB device controller private structure.
  84. */
  85. struct usb_aotg_dc_prv {
  86. usb_dc_status_callback status_cb;
  87. struct aotg_dc_ep_ctrl_prv in_ep_ctrl[USB_AOTG_IN_EP_NUM+1];
  88. struct aotg_dc_ep_ctrl_prv out_ep_ctrl[USB_AOTG_OUT_EP_NUM+1];
  89. uint8_t attached;
  90. uint8_t phase; /* Control transfer stage */
  91. uint8_t speed;
  92. union {
  93. uint8_t raw_setup[sizeof(struct usb_setup_packet)];
  94. struct usb_setup_packet setup;
  95. };
  96. };
  97. /*
  98. * USB host controller endpoint private structure.
  99. */
  100. struct aotg_hc_ep_ctrl_prv {
  101. uint8_t ep_ena; /* Endpoint Enabled */
  102. uint8_t ep_type;
  103. uint16_t mps; /* Endpoint Max packet size */
  104. struct usb_request *urb;
  105. uint8_t ep_addr; /* Endpoint Address */
  106. uint8_t err_count;
  107. };
  108. /*
  109. * USB host controller private structure.
  110. */
  111. struct usb_aotg_hc_prv {
  112. uint32_t port;
  113. struct aotg_hc_ep_ctrl_prv in_ep_ctrl[USB_AOTG_IN_EP_NUM];
  114. struct aotg_hc_ep_ctrl_prv out_ep_ctrl[USB_AOTG_OUT_EP_NUM];
  115. uint8_t attached;
  116. uint8_t phase; /* Control transfer stage */
  117. uint8_t speed;
  118. };
  119. struct usb_aotg_otg_prv {
  120. union {
  121. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  122. struct usb_aotg_dc_prv dc;
  123. #endif
  124. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  125. struct usb_aotg_hc_prv hc;
  126. #endif
  127. };
  128. };
  129. static struct usb_aotg_otg_prv usb_aotg;
  130. static uint8_t usb_aotg_mode;
  131. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  132. #define usb_aotg_dc (usb_aotg.dc)
  133. #endif
  134. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  135. #define usb_aotg_hc (usb_aotg.hc)
  136. #endif
  137. #ifdef CONFIG_USB_AOTG_UDC_DMA
  138. struct usb_aotg_dma_prv {
  139. const struct device *dma_dev;
  140. int epin_dma;
  141. int epout_dma_single;
  142. int epout_dma_burst8;
  143. struct dma_config epin_dma_config;
  144. struct dma_config epout_dma_config_single;
  145. struct dma_config epout_dma_config_burst8;
  146. struct dma_block_config epin_dma_block;
  147. struct dma_block_config epout_dma_block_single;
  148. struct dma_block_config epout_dma_block_burst8;
  149. };
  150. static struct usb_aotg_dma_prv usb_aotg_dma;
  151. #endif
  152. /*
  153. * Dump aotg registers
  154. */
  155. static inline void usb_aotg_reg_dump(void)
  156. {
  157. LOG_DBG("USB AOTG registers:");
  158. LOG_DBG("IDVBUSCTRL: 0x%x", usb_read8(IDVBUSCTRL));
  159. LOG_DBG("DPDMCTRL: 0x%x", usb_read8(DPDMCTRL));
  160. LOG_DBG("LINESTATUS: 0x%x", usb_read8(LINESTATUS));
  161. LOG_DBG("USBIEN: 0x%x", usb_read8(USBIEN));
  162. LOG_DBG("OTGIEN: 0x%x", usb_read8(OTGIEN));
  163. LOG_DBG("USBEIEN: 0x%x", usb_read8(USBEIEN));
  164. LOG_DBG("USBIRQ: 0x%x", usb_read8(USBIRQ));
  165. LOG_DBG("OTGIRQ: 0x%x", usb_read8(OTGIRQ));
  166. LOG_DBG("USBEIRQ: 0x%x", usb_read8(USBEIRQ));
  167. LOG_DBG("OTGSTATE: 0x%x", usb_read8(OTGSTATE));
  168. }
  169. /*
  170. * Dump aotg endpoint registers
  171. */
  172. static inline void usb_aotg_ep_reg_dump(uint8_t ep)
  173. {
  174. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  175. ARG_UNUSED(ep_idx);
  176. LOG_DBG("USB AOTG EP 0x%x registers:", ep);
  177. if (USB_EP_DIR_IS_OUT(ep)) {
  178. LOG_DBG("HCINCTRL: 0x%x", usb_read8(HCINxCTRL(ep_idx)));
  179. LOG_DBG("STADDR: 0x%x", usb_read32(EPxOUT_STADDR(ep_idx)));
  180. LOG_DBG("OUT_CTRL: 0x%x", usb_read8(OUTxCTRL(ep_idx)));
  181. LOG_DBG("OUT_MAXPKT: 0x%x", usb_read16(OUTxMAXPKT(ep_idx)));
  182. LOG_DBG("OUT_IEN: 0x%x", usb_read8(OUTIEN));
  183. LOG_DBG("OUT_CS: 0x%x", usb_read8(OUTxCS(ep_idx)));
  184. LOG_DBG("OUT_SHTPKT: 0x%x", usb_read8(OUT_SHTPKT));
  185. LOG_DBG("OUT_BC: 0x%x", usb_read16(OUTxBC(ep_idx)));
  186. LOG_DBG("OUT_IRQ: 0x%x", usb_read8(OUTIRQ));
  187. } else {
  188. LOG_DBG("HCOUTCTRL: 0x%x", usb_read8(HCOUTxCTRL(ep_idx)));
  189. LOG_DBG("STADDR: 0x%x", usb_read32(EPxIN_STADDR(ep_idx)));
  190. LOG_DBG("IN_CTRL: 0x%x", usb_read8(INxCTRL(ep_idx)));
  191. LOG_DBG("IN_MAXPKT: 0x%x", usb_read16(INxMAXPKT(ep_idx)));
  192. LOG_DBG("IN_IEN: 0x%x", usb_read8(INIEN));
  193. LOG_DBG("IN_CS: 0x%x", usb_read8(INxCS(ep_idx)));
  194. LOG_DBG("IN_IRQ: 0x%x", usb_read8(INIRQ));
  195. }
  196. }
  197. /*
  198. * Check if the address of endpoint is valid
  199. *
  200. * NOTE: we don't check if the address of control endpoint is non-zero!
  201. */
  202. static inline bool usb_aotg_ep_addr_valid(uint8_t ep)
  203. {
  204. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  205. if (USB_EP_DIR_IS_OUT(ep) && (ep_idx < USB_AOTG_OUT_EP_NUM)) {
  206. return true;
  207. } else if (USB_EP_DIR_IS_IN(ep) && (ep_idx < USB_AOTG_IN_EP_NUM)) {
  208. return true;
  209. }
  210. return false;
  211. }
  212. /*
  213. * Enable USB controller clock
  214. */
  215. static inline void usb_aotg_clock_enable(void)
  216. {
  217. acts_clock_peripheral_enable(CLOCK_ID_USB);
  218. }
  219. /*
  220. * Disable USB controller clock
  221. */
  222. static inline void usb_aotg_clock_disable(void)
  223. {
  224. acts_clock_peripheral_disable(CLOCK_ID_USB);
  225. }
  226. /*
  227. * Reset USB controller
  228. */
  229. static inline int usb_aotg_reset(void)
  230. {
  231. acts_reset_peripheral(RESET_ID_USB2);
  232. acts_reset_peripheral(RESET_ID_USB);
  233. usb_aotg_reset_specific();
  234. /* Wait for USB controller until reset done */
  235. while (usb_read8(LINESTATUS) & BIT(OTGRESET)) {
  236. ;
  237. }
  238. return 0;
  239. }
  240. /*
  241. * Enable controller
  242. */
  243. static inline void usb_aotg_enable(void)
  244. {
  245. usb_aotg_power_on();
  246. usb_aotg_clock_enable();
  247. usb_aotg_dpdm_init();
  248. usb_aotg_reset();
  249. }
  250. /*
  251. * Disable controller
  252. */
  253. static inline void usb_aotg_disable(void)
  254. {
  255. usb_aotg_disable_specific();
  256. usb_aotg_clock_disable();
  257. usb_aotg_power_off();
  258. }
  259. /*
  260. * Reset specific endpoint
  261. */
  262. static inline int usb_aotg_ep_reset(uint8_t aotg_ep, uint8_t type)
  263. {
  264. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  265. if (!ep_idx) {
  266. LOG_DBG("Ep reset all endpoints");
  267. }
  268. /* Select specific endpoint */
  269. if (USB_EP_DIR_IS_OUT(aotg_ep)) {
  270. usb_write8(EPRST, ep_idx);
  271. } else {
  272. usb_write8(EPRST, ep_idx | BIT(EPRST_IO_IN));
  273. }
  274. if (type == USB_AOTG_EP_FIFO_RESET) {
  275. usb_set_bit8(EPRST, EPRST_FIFORST);
  276. } else if (type == USB_AOTG_EP_TOGGLE_RESET) {
  277. usb_set_bit8(EPRST, EPRST_TOGRST);
  278. } else if (type == USB_AOTG_EP_RESET) {
  279. usb_set_bit8(EPRST, EPRST_FIFORST);
  280. usb_set_bit8(EPRST, EPRST_TOGRST);
  281. } else {
  282. LOG_ERR("Ep reset type: %d", type);
  283. return -EINVAL;
  284. }
  285. return 0;
  286. }
  287. static inline int usb_aotg_exit(void)
  288. {
  289. usb_aotg_enable();
  290. usb_aotg_disable();
  291. return 0;
  292. }
  293. /*
  294. * Write data to EPx FIFO
  295. *
  296. * @return Actual length
  297. */
  298. static inline int ep_write_fifo(uint8_t ep, const uint8_t *const data,
  299. uint32_t data_len)
  300. {
  301. uint32_t i;
  302. uint32_t len;
  303. uint8_t left;
  304. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  305. const uint8_t *buf = (const uint8_t *)data;
  306. uint8_t count = 0;
  307. if (USB_EP_IN_DMA_CAP(ep_idx)) {
  308. /* FIXME: wait 100us at most */
  309. while (usb_read8(INxCS(ep_idx)) & BIT(EPCS_BUSY)) {
  310. if (++count > 100) {
  311. break;
  312. }
  313. k_busy_wait(1);
  314. }
  315. }
  316. /* Check Busy */
  317. if (usb_read8(INxCS(ep_idx)) & BIT(EPCS_BUSY)) {
  318. LOG_DBG("EP 0x%x still busy", ep);
  319. return -EAGAIN;
  320. }
  321. if (USB_EP_IN_DMA_CAP(ep_idx)) {
  322. usb_write8(INIRQ, BIT(ep_idx));
  323. usb_set_bit8(INIEN, ep_idx);
  324. }
  325. /* 32-bit alignment */
  326. if (((long)buf & 0x3) == 0) {
  327. len = data_len >> 2;
  328. left = data_len & 0x3;
  329. for (i = 0; i < len; i++, buf += 4) {
  330. usb_write32(FIFOxDAT(ep_idx), *(uint32_t *)buf);
  331. }
  332. /* 16-bit alignment */
  333. } else if (((long)buf & 0x1) == 0) {
  334. len = data_len >> 1;
  335. left = data_len & 0x1;
  336. for (i = 0; i < len; i++, buf += 2) {
  337. usb_write16(FIFOxDAT(ep_idx), *(uint16_t *)buf);
  338. }
  339. } else {
  340. len = data_len;
  341. left = 0;
  342. for (i = 0; i < len; i++, buf++) {
  343. usb_write8(FIFOxDAT(ep_idx), *buf);
  344. }
  345. }
  346. /* Handle left byte(s) */
  347. for (i = 0; i < left; i++, buf++) {
  348. usb_write8(FIFOxDAT(ep_idx), *buf);
  349. }
  350. /* Set Byte Counter */
  351. usb_write16(INxBC(ep_idx), data_len);
  352. /* Set busy */
  353. usb_set_bit8(INxCS(ep_idx), EPCS_BUSY);
  354. return data_len;
  355. }
  356. /*
  357. * Read data from EP FIFO
  358. *
  359. * @return Actual length
  360. */
  361. static inline int ep_read_fifo(uint8_t ep, uint8_t *data, uint32_t data_len)
  362. {
  363. uint32_t i;
  364. uint32_t len;
  365. uint8_t left;
  366. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  367. uint32_t addr = FIFOxDAT(ep_idx);
  368. uint8_t *buf = (uint8_t *)data;
  369. /* Check Busy */
  370. if (usb_read8(OUTxCS(ep_idx)) & BIT(EPCS_BUSY)) {
  371. LOG_DBG("EP 0x%x still busy", ep);
  372. return -EBUSY;
  373. }
  374. /* 32-bit alignment */
  375. if (((long)buf & 0x3) == 0) {
  376. len = data_len >> 2;
  377. left = data_len & 0x3;
  378. for (i = 0; i < len; i++, buf += 4) {
  379. *(uint32_t *)buf = usb_read32(addr);
  380. }
  381. /* 16-bit alignment */
  382. } else if (((long)buf & 0x1) == 0) {
  383. len = data_len >> 1;
  384. left = data_len & 0x1;
  385. for (i = 0; i < len; i++, buf += 2) {
  386. *(uint16_t *)buf = usb_read16(addr);
  387. }
  388. } else {
  389. len = data_len;
  390. left = 0;
  391. for (i = 0; i < len; i++, buf++) {
  392. *buf = usb_read8(addr);
  393. }
  394. }
  395. /* Handle left byte(s) */
  396. for (i = 0; i < left; i++, buf++) {
  397. *buf = usb_read8(addr);
  398. }
  399. return data_len;
  400. }
  401. /*
  402. * Data stage: write data to EP0 FIFO
  403. *
  404. * @return Actual length
  405. */
  406. static inline int ep0_write_fifo(const uint8_t *const data, uint32_t data_len)
  407. {
  408. uint32_t i;
  409. uint8_t j;
  410. uint32_t addr = EP0IN_FIFO;
  411. /* Check Busy */
  412. if (usb_read8(EP0CS) & BIT(EP0CS_INBUSY)) {
  413. LOG_DBG("still busy");
  414. return -EAGAIN;
  415. }
  416. /* 32-bit alignment */
  417. if (((long)data & 0x3) == 0) {
  418. for (i = 0; i < (data_len & ~0x3); i += 4) {
  419. usb_write32(addr + i, *(uint32_t *)(data + i));
  420. }
  421. for (j = 0; j < (data_len & 0x3); j++) {
  422. usb_write8(addr + i + j, *(data + i + j));
  423. }
  424. /* 16-bit alignment */
  425. } else if (((long)data & 0x1) == 0) {
  426. for (i = 0; i < (data_len & ~0x1); i += 2) {
  427. usb_write16(addr + i, *(uint16_t *)(data + i));
  428. }
  429. /* The last byte */
  430. if (data_len & 0x1) {
  431. usb_write8(addr + i, *(data + i));
  432. }
  433. } else {
  434. for (i = 0; i < data_len; i++) {
  435. usb_write8(addr + i, *(data + i));
  436. }
  437. }
  438. /* Set Byte Counter: set busy */
  439. usb_write8(IN0BC, data_len);
  440. return data_len;
  441. }
  442. /*
  443. * Data stage: read data from EP0 FIFO
  444. *
  445. * @return Actual length
  446. */
  447. static inline int ep0_read_fifo(uint8_t *data, uint32_t data_len)
  448. {
  449. uint32_t i;
  450. uint8_t j;
  451. uint8_t len;
  452. uint32_t addr = EP0OUT_FIFO;
  453. /* Check Busy */
  454. if (usb_read8(EP0CS) & BIT(EP0CS_OUTBUSY)) {
  455. LOG_DBG("still busy");
  456. return -EBUSY;
  457. }
  458. /* Get Actual length */
  459. len = usb_read8(OUT0BC);
  460. if (data_len < len) {
  461. len = data_len;
  462. }
  463. /* 32-bit alignment */
  464. if (((long)data & 0x3) == 0) {
  465. for (i = 0; i < (len & ~0x3); i += 4) {
  466. *(uint32_t *)(data + i) = usb_read32(addr + i);
  467. }
  468. for (j = 0; j < (len & 0x3); j++) {
  469. *(data + i + j) = usb_read8(addr + i + j);
  470. }
  471. /* 16-bit alignment */
  472. } else if (((long)data & 0x1) == 0) {
  473. for (i = 0; i < (len & ~0x1); i += 2) {
  474. *(uint16_t *)(data + i) = usb_read16(addr + i);
  475. }
  476. /* The last byte */
  477. if (len & 0x1) {
  478. *(data + i) = usb_read8(addr + i);
  479. }
  480. } else {
  481. for (i = 0; i < len; i++) {
  482. *(data + i) = usb_read8(addr + i);
  483. }
  484. }
  485. return len;
  486. }
  487. /*
  488. * Device related
  489. */
  490. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  491. /*
  492. * Check if the MaxPacketSize of endpoint is valid
  493. */
  494. static inline bool aotg_dc_ep_mps_valid(uint16_t ep_mps,
  495. enum usb_dc_ep_type ep_type)
  496. {
  497. enum usb_device_speed speed = usb_aotg_dc.speed;
  498. switch (ep_type) {
  499. case USB_DC_EP_CONTROL:
  500. return ep_mps <= USB_MAX_CTRL_MPS;
  501. case USB_DC_EP_BULK:
  502. if (speed == USB_SPEED_HIGH) {
  503. return ep_mps == USB_MAX_HS_BULK_MPS;
  504. } else {
  505. return ep_mps <= USB_MAX_FS_BULK_MPS;
  506. }
  507. case USB_DC_EP_INTERRUPT:
  508. if (speed == USB_SPEED_HIGH) {
  509. return ep_mps <= USB_MAX_HS_INTR_MPS;
  510. } else {
  511. return ep_mps <= USB_MAX_FS_INTR_MPS;
  512. }
  513. case USB_DC_EP_ISOCHRONOUS:
  514. if (speed == USB_SPEED_HIGH) {
  515. return ep_mps <= USB_MAX_HS_ISOC_MPS;
  516. } else {
  517. return ep_mps <= USB_MAX_FS_ISOC_MPS;
  518. }
  519. }
  520. return false;
  521. }
  522. /*
  523. * Check if the endpoint is enabled
  524. */
  525. static inline bool aotg_dc_ep_is_enabled(uint8_t ep)
  526. {
  527. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  528. if (USB_EP_DIR_IS_OUT(ep) && usb_aotg_dc.out_ep_ctrl[ep_idx].ep_ena) {
  529. return true;
  530. }
  531. if (USB_EP_DIR_IS_IN(ep) && usb_aotg_dc.in_ep_ctrl[ep_idx].ep_ena) {
  532. return true;
  533. }
  534. return false;
  535. }
  536. /*
  537. * Using single FIFO for every endpoint (except for ep0) by default
  538. */
  539. static inline int aotg_dc_ep_alloc_fifo(uint8_t ep)
  540. {
  541. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  542. if (!ep_idx) {
  543. return 0;
  544. }
  545. if (USB_EP_DIR_IS_OUT(ep)) {
  546. if (ep_idx >= USB_AOTG_OUT_EP_NUM) {
  547. return -EINVAL;
  548. }
  549. return aotg_dc_epout_alloc_fifo_specific(ep_idx);
  550. }
  551. if (ep_idx >= USB_AOTG_IN_EP_NUM) {
  552. return -EINVAL;
  553. }
  554. return aotg_dc_epin_alloc_fifo_specific(ep_idx);
  555. }
  556. /*
  557. * Max Packet Size Limit
  558. */
  559. static inline int aotg_dc_set_max_packet(uint8_t ep, uint16_t ep_mps,
  560. enum usb_dc_ep_type ep_type)
  561. {
  562. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  563. /*
  564. * NOTE: If the MaxPacketSize is beyond the limit, we won't try to
  565. * assign a valid value, because it will fail anyway when the upper
  566. * layer try to check capacity of the endpoint!
  567. */
  568. if (!aotg_dc_ep_mps_valid(ep_mps, ep_type)) {
  569. return -EINVAL;
  570. }
  571. if (USB_EP_DIR_IS_OUT(ep)) {
  572. usb_write16(OUTxMAXPKT(ep_idx), ep_mps);
  573. usb_aotg_dc.out_ep_ctrl[ep_idx].mps = ep_mps;
  574. } else {
  575. usb_write16(INxMAXPKT(ep_idx), ep_mps);
  576. usb_aotg_dc.in_ep_ctrl[ep_idx].mps = ep_mps;
  577. }
  578. return 0;
  579. }
  580. /*
  581. * Endpoint type
  582. */
  583. static inline void aotg_dc_set_ep_type(uint8_t ep, enum usb_dc_ep_type ep_type)
  584. {
  585. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  586. if (USB_EP_DIR_IS_OUT(ep)) {
  587. usb_aotg_dc.out_ep_ctrl[ep_idx].ep_type = ep_type;
  588. } else {
  589. usb_aotg_dc.in_ep_ctrl[ep_idx].ep_type = ep_type;
  590. }
  591. }
  592. /*
  593. * Initialize AOTG hardware endpoint.
  594. *
  595. * Set FIFO address, endpoint type, FIFO type, max packet size
  596. */
  597. static inline int aotg_dc_ep_set(uint8_t ep, uint16_t ep_mps,
  598. enum usb_dc_ep_type ep_type)
  599. {
  600. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  601. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  602. int ret;
  603. LOG_DBG("Ep setup 0x%x, mps %d, type %d", ep, ep_mps, ep_type);
  604. /* Set FIFO address */
  605. ret = aotg_dc_ep_alloc_fifo(ep);
  606. if (ret) {
  607. return ret;
  608. }
  609. /* Set Max Packet */
  610. ret = aotg_dc_set_max_packet(ep, ep_mps, ep_type);
  611. if (ret) {
  612. return ret;
  613. }
  614. /* Set EP type */
  615. aotg_dc_set_ep_type(ep, ep_type);
  616. /* No need to set for endpoint 0 */
  617. if (!ep_idx) {
  618. return 0;
  619. }
  620. if (USB_EP_DIR_IS_OUT(ep)) {
  621. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  622. if (ep_ctrl->multi) {
  623. /* Set endpoint type and FIFO type */
  624. usb_write8(OUTxCTRL(ep_idx),
  625. (ep_type << 2) | USB_AOTG_FIFO_DOUBLE);
  626. /* Set FIFOCTRL */
  627. usb_write8(FIFOCTRL, ep_idx | BIT(FIFOCTRL_AUTO));
  628. } else {
  629. usb_write8(OUTxCTRL(ep_idx),
  630. (ep_type << 2) | USB_AOTG_FIFO_SINGLE);
  631. /* Set FIFOCTRL */
  632. if (USB_EP_OUT_DMA_CAP(ep_idx)) {
  633. usb_write8(FIFOCTRL, ep_idx | BIT(FIFOCTRL_AUTO));
  634. } else {
  635. usb_write8(FIFOCTRL, ep_idx);
  636. }
  637. }
  638. } else {
  639. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[ep_idx];
  640. if (ep_ctrl->multi) {
  641. usb_write8(INxCTRL(ep_idx),
  642. (ep_type << 2) | USB_AOTG_FIFO_DOUBLE);
  643. } else {
  644. usb_write8(INxCTRL(ep_idx),
  645. (ep_type << 2) | USB_AOTG_FIFO_SINGLE);
  646. if (USB_EP_IN_DMA_CAP(ep_idx)) {
  647. usb_write8(FIFOCTRL, ep_idx | BIT(FIFOCTRL_IO_IN) | BIT(FIFOCTRL_AUTO));
  648. } else {
  649. usb_write8(FIFOCTRL, ep_idx | BIT(FIFOCTRL_IO_IN));
  650. }
  651. }
  652. }
  653. return 0;
  654. }
  655. /*
  656. * Prepare for the next OUT transaction
  657. *
  658. * There are two ways: write OUTxBC or set busy for EPxCS.
  659. * I don't know which one is better really, just keep it both.
  660. */
  661. static inline void aotg_dc_prep_rx(uint8_t ep_idx)
  662. {
  663. /* Set busy */
  664. if (!ep_idx) {
  665. #if 0
  666. usb_set_bit8(EP0CS, EP0CS_OUTBUSY);
  667. #endif
  668. usb_write8(OUT0BC, 0x0);
  669. } else {
  670. #if 0
  671. usb_write8(OUTxCS(ep_idx), 0);
  672. #endif
  673. if (!(usb_read8(OUTxCS(ep_idx)) & BIT(EPCS_BUSY))) {
  674. usb_set_bit8(OUTxCS(ep_idx), EPCS_BUSY);
  675. }
  676. }
  677. }
  678. /*
  679. * Setup stage: read data from Setup FIFO
  680. */
  681. static inline int ep0_read_setup(uint8_t *data, uint32_t data_len)
  682. {
  683. uint8_t len = data_len;
  684. if (data_len > sizeof(struct usb_setup_packet)) {
  685. len = sizeof(struct usb_setup_packet);
  686. }
  687. memcpy(data, usb_aotg_dc.raw_setup, len);
  688. return len;
  689. }
  690. /*
  691. * Status stage
  692. */
  693. static inline void handle_status(void)
  694. {
  695. usb_set_bit8(EP0CS, EP0CS_NAK);
  696. }
  697. /*
  698. * Receive data from host: could be Setup, Data or Status
  699. *
  700. * @return transfer length
  701. */
  702. static inline int aotg_dc_rx(uint8_t ep, uint8_t *data, uint32_t data_len)
  703. {
  704. int act_len = 0;
  705. /* Endpoint 0 */
  706. if (!USB_EP_ADDR2IDX(ep)) {
  707. if (usb_aotg_dc.phase == USB_AOTG_SETUP) {
  708. act_len = ep0_read_setup(data, data_len);
  709. } else if (usb_aotg_dc.phase == USB_AOTG_OUT_DATA) {
  710. act_len = ep0_read_fifo(data, data_len);
  711. #if 0
  712. } else if (usb_aotg_dc.phase == USB_AOTG_OUT_STATUS) {
  713. /*
  714. * Case 1: Setup, In Data, Out Status
  715. */
  716. handle_status();
  717. #endif
  718. }
  719. } else {
  720. act_len = ep_read_fifo(ep, data, data_len);
  721. }
  722. return act_len;
  723. }
  724. /*
  725. * Send data to host
  726. *
  727. * @return transfer length
  728. */
  729. static inline int aotg_dc_tx(uint8_t ep, const uint8_t *const data,
  730. uint32_t data_len)
  731. {
  732. uint32_t act_len = 0;
  733. /* Endpoint 0 */
  734. if (!USB_EP_ADDR2IDX(ep)) {
  735. if ((usb_aotg_dc.phase == USB_AOTG_IN_DATA) && !data_len) {
  736. /*
  737. * Handling zero-length packet for In-data phase
  738. */
  739. act_len = ep0_write_fifo(data, data_len);
  740. } else if (!data_len) {
  741. /*
  742. * Handling in-status stage
  743. * Case 2: Setup, In Status
  744. * Case 3: Setup, Out Data, In Status
  745. */
  746. if (usb_aotg_dc.phase != USB_AOTG_SET_ADDRESS) {
  747. handle_status();
  748. }
  749. usb_aotg_dc.phase = USB_AOTG_IN_STATUS;
  750. } else {
  751. act_len = ep0_write_fifo(data, data_len);
  752. usb_aotg_dc.phase = USB_AOTG_IN_DATA;
  753. }
  754. } else {
  755. act_len = ep_write_fifo(ep, data, data_len);
  756. }
  757. return act_len;
  758. }
  759. static inline void aotg_dc_handle_otg(void)
  760. {
  761. uint8_t tmp;
  762. uint8_t state;
  763. /* Clear OTG IRQ(s) */
  764. tmp = usb_read8(OTGIEN) & usb_read8(OTGIRQ);
  765. usb_write8(OTGIRQ, tmp);
  766. state = usb_read8(OTGSTATE);
  767. /* If AOTG enters b_peripheral state, enable Core interrupts */
  768. if (state == OTG_B_PERIPHERAL) {
  769. #ifdef CONFIG_USB_AOTG_OTG_SUPPORT_HS
  770. usb_set_bit8(USBIEN, USBIEN_HS);
  771. #endif
  772. usb_set_bit8(USBIEN, USBIEN_RESET);
  773. usb_set_bit8(USBIEN, USBIEN_SETUP);
  774. }
  775. LOG_DBG("State: 0x%x, irq: 0x%x", state, tmp);
  776. }
  777. static inline void aotg_dc_handle_reset(void)
  778. {
  779. /* Clear USB Reset IRQ */
  780. usb_write8(USBIRQ, BIT(USBIRQ_RESET));
  781. usb_aotg_dc.speed = USB_SPEED_UNKNOWN;
  782. /* Clear USB SOF IRQ */
  783. usb_write8(USBIRQ, BIT(USBIRQ_SOF));
  784. /* Enable USB SOF IRQ */
  785. usb_set_bit8(USBIEN, USBIEN_SOF);
  786. LOG_DBG("");
  787. #ifdef CONFIG_USB_AOTG_UDC_DMA
  788. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_single);
  789. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_burst8);
  790. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epin_dma);
  791. #endif
  792. /* Inform upper layers */
  793. if (usb_aotg_dc.status_cb) {
  794. usb_aotg_dc.status_cb(USB_DC_RESET, NULL);
  795. }
  796. }
  797. #ifdef CONFIG_USB_AOTG_OTG_SUPPORT_HS
  798. static inline void aotg_dc_handle_hs(void)
  799. {
  800. /* Clear USB high-speed IRQ */
  801. usb_write8(USBIRQ, BIT(USBIRQ_HS));
  802. usb_aotg_dc.speed = USB_SPEED_HIGH;
  803. LOG_DBG("");
  804. /* Inform upper layers */
  805. if (usb_aotg_dc.status_cb) {
  806. usb_aotg_dc.status_cb(USB_DC_HIGHSPEED, NULL);
  807. }
  808. }
  809. #endif
  810. static inline void aotg_dc_handle_sof(void)
  811. {
  812. /* Clear USB SOF IRQ */
  813. usb_write8(USBIRQ, BIT(USBIRQ_SOF));
  814. /* Disable USB SOF IRQ */
  815. usb_clear_bit8(USBIEN, USBIEN_SOF);
  816. if (usb_aotg_dc.speed == USB_SPEED_UNKNOWN) {
  817. usb_aotg_dc.speed = USB_SPEED_FULL;
  818. }
  819. LOG_DBG("");
  820. /* Inform upper layers */
  821. if (usb_aotg_dc.status_cb) {
  822. usb_aotg_dc.status_cb(USB_DC_SOF, NULL);
  823. }
  824. }
  825. static inline void aotg_dc_handle_resume(uint8_t usbeien)
  826. {
  827. /* clear resume */
  828. usb_write8(USBEIRQ, BIT(USBEIRQ_RESUME) | usbeien);
  829. /* disable resume */
  830. usb_write8(USBEIEN, (~BIT(USBEIEN_RESUME)) & usbeien);
  831. if (usb_aotg_dc.status_cb) {
  832. usb_aotg_dc.status_cb(USB_DC_RESUME, NULL);
  833. }
  834. }
  835. static inline void aotg_dc_handle_suspend(uint8_t usbeien)
  836. {
  837. usb_write8(USBIRQ, BIT(USBIRQ_SUSPEND));
  838. /* clear resume */
  839. usb_write8(USBEIRQ, BIT(USBEIRQ_RESUME) | usbeien);
  840. /* enable resume */
  841. usb_write8(USBEIEN, BIT(USBEIEN_RESUME) | usbeien);
  842. if (usb_aotg_dc.status_cb) {
  843. usb_aotg_dc.status_cb(USB_DC_SUSPEND, NULL);
  844. }
  845. }
  846. /*
  847. * Handle SETUP packet
  848. */
  849. static inline void aotg_dc_handle_setup(void)
  850. {
  851. usb_dc_ep_callback ep_cb;
  852. uint32_t addr = SETUP_FIFO;
  853. uint8_t i;
  854. /* Clear SETUP IRQ */
  855. usb_write8(USBIRQ, BIT(USBIRQ_SETUP));
  856. /* Fetch setup packet */
  857. for (i = 0; i < sizeof(struct usb_setup_packet); i++, addr++) {
  858. *(usb_aotg_dc.raw_setup + i) = usb_read8(addr);
  859. }
  860. /* Set setup data length */
  861. usb_aotg_dc.out_ep_ctrl[USB_AOTG_OUT_EP_0].data_len =
  862. sizeof(struct usb_setup_packet);
  863. /* Set in data length */
  864. if (USB_REQ_DIR_IN(usb_aotg_dc.setup.bmRequestType)) {
  865. usb_aotg_dc.in_ep_ctrl[USB_AOTG_IN_EP_0].data_len =
  866. usb_aotg_dc.setup.wLength;
  867. }
  868. usb_aotg_dc.phase = USB_AOTG_SETUP;
  869. ep_cb = usb_aotg_dc.out_ep_ctrl[USB_AOTG_OUT_EP_0].cb;
  870. /* Call the registered callback if any */
  871. if (ep_cb) {
  872. ep_cb(USB_AOTG_OUT_EP_0, USB_DC_EP_SETUP);
  873. }
  874. }
  875. /*
  876. * Handle EPxOUT data
  877. */
  878. static inline void aotg_dc_handle_epout(uint8_t ep_idx)
  879. {
  880. uint8_t ep = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_OUT);
  881. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  882. uint16_t len;
  883. /* Clear EPxOUT IRQ */
  884. usb_write8(OUTIRQ, BIT(ep_idx));
  885. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  886. /* Get Actual Length */
  887. if (!ep_idx) {
  888. /* Check phase before checking byte counter */
  889. if (usb_aotg_dc.phase == USB_AOTG_OUT_STATUS) {
  890. return;
  891. }
  892. /*
  893. * EP0OUT data irq may come before EPxOUT token irq in
  894. * out-status phase, filter it otherwise it will be disturbed.
  895. * It may come even in in-data phase, can't explain!
  896. *
  897. * Keep it (only for record), we will check setup packet!
  898. */
  899. if (usb_aotg_dc.phase == USB_AOTG_IN_DATA) {
  900. return;
  901. }
  902. /* Filter by direction and length of setup packet */
  903. if (USB_REQ_DIR_IN(usb_aotg_dc.setup.bmRequestType)) {
  904. return;
  905. } else if (usb_aotg_dc.setup.wLength == 0) {
  906. return;
  907. }
  908. /* NOTICE: OUT0BC will be set in setup phase and data phase */
  909. len = usb_read8(OUT0BC);
  910. if (len == 0) {
  911. return;
  912. }
  913. usb_aotg_dc.phase = USB_AOTG_OUT_DATA;
  914. } else {
  915. len = usb_read16(OUTxBC(ep_idx));
  916. if (len == 0) {
  917. LOG_DBG("ep 0x%x recv zero-length packet", ep);
  918. }
  919. if (ep_ctrl->data) {
  920. /* Disable interrupt if received all data to support auto-mode */
  921. if ((ep_ctrl->data_len == len) ||
  922. (len < ep_ctrl->mps)) {
  923. usb_clear_bit8(OUTIEN, ep_idx);
  924. LOG_DBG("Disable irq");
  925. }
  926. /* FIXME: leave it to upper layer, should we clear fifo? */
  927. if (len > ep_ctrl->data_len) {
  928. LOG_DBG("babble: %d, %d", len, ep_ctrl->data_len);
  929. len = ep_ctrl->data_len;
  930. }
  931. ep_read_fifo(ep, ep_ctrl->data, len);
  932. ep_ctrl->actual += len;
  933. ep_ctrl->data_len -= len;
  934. ep_ctrl->data += len;
  935. if (ep_ctrl->data_len == 0) {
  936. LOG_DBG("0x%x done 0x%x", ep, usb_read8(OUTxCS(ep_idx)));
  937. ep_ctrl->data = NULL;
  938. goto done;
  939. } else if (len < ep_ctrl->mps) {
  940. LOG_DBG("0x%x short %d", ep, len);
  941. ep_ctrl->data = NULL;
  942. goto done;
  943. } else {
  944. /* read continue */
  945. if (!(usb_read8(OUTxCS(ep_idx)) & BIT(EPCS_BUSY))) {
  946. usb_set_bit8(OUTxCS(ep_idx), EPCS_BUSY);
  947. }
  948. return;
  949. }
  950. }
  951. }
  952. ep_ctrl->data_len = len;
  953. done:
  954. /* Call the registered callback if any */
  955. if (ep_ctrl->cb) {
  956. ep_ctrl->cb(ep, USB_DC_EP_DATA_OUT);
  957. }
  958. }
  959. /*
  960. * Handle EPxIN data
  961. */
  962. static inline void aotg_dc_handle_epin(uint8_t ep_idx)
  963. {
  964. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  965. uint32_t len;
  966. uint8_t ep;
  967. /* Clear EPxIN IRQ */
  968. usb_write8(INIRQ, BIT(ep_idx));
  969. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[ep_idx];
  970. ep = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_IN);
  971. if (ep_idx == 0) {
  972. goto done;
  973. }
  974. /* NOTICE: should never happen! */
  975. if (ep_ctrl->data == NULL) {
  976. LOG_DBG("0x%x", ep_idx);
  977. goto done;
  978. }
  979. if (ep_ctrl->data_len == 0) {
  980. LOG_DBG("0x%x done", ep);
  981. ep_ctrl->data = NULL;
  982. goto done;
  983. }
  984. if (ep_ctrl->data_len > ep_ctrl->mps) {
  985. len = ep_ctrl->mps;
  986. } else {
  987. len = ep_ctrl->data_len;
  988. }
  989. ep_write_fifo(ep, ep_ctrl->data, len);
  990. ep_ctrl->data_len -= len;
  991. ep_ctrl->data += len;
  992. return;
  993. done:
  994. /* Call the registered callback if any */
  995. if (ep_ctrl->cb) {
  996. ep_ctrl->cb(ep, USB_DC_EP_DATA_IN);
  997. }
  998. }
  999. /*
  1000. * Handle EPxIN empty
  1001. */
  1002. static inline void aotg_dc_handle_epin_empty(void)
  1003. {
  1004. uint8_t ien = (usb_read8(INEMPTY_IEN) & INEMPTY_IEN_MASK) >> INEMPTY_IEN_SHIFT;
  1005. uint8_t irq = (usb_read8(INEMPTY_IRQ) & INEMPTY_IRQ_MASK) >> INEMPTY_IRQ_SHIFT;
  1006. uint8_t ep_msk = ien & irq;
  1007. usb_dc_ep_callback ep_cb;
  1008. uint8_t ep_idx, ep;
  1009. uint8_t i;
  1010. /* clear all pendings */
  1011. usb_write8(INEMPTY_IRQ, INEMPTY_IRQ_MASK);
  1012. /* disable all interrupts */
  1013. usb_write8(INEMPTY_IEN, 0);
  1014. for (i = 0; i < USB_AOTG_IN_EP_NUM; i++) {
  1015. if (ep_msk & BIT(i)) {
  1016. ep_idx = INEMPTY_IRQ2ADDR(i) & USB_EP_NUM_MASK;
  1017. ep = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_IN);
  1018. LOG_DBG("ep: 0x%x", ep);
  1019. ep_cb = usb_aotg_dc.in_ep_ctrl[ep_idx].cb;
  1020. /* Call the registered callback if any */
  1021. if (ep_cb) {
  1022. ep_cb(ep, USB_DC_EP_DATA_IN);
  1023. }
  1024. }
  1025. }
  1026. }
  1027. static inline void aotg_dc_handle_out_token_internal(uint8_t ep_idx)
  1028. {
  1029. if (ep_idx == USB_AOTG_OUT_EP_0) {
  1030. /*
  1031. * We may get one or more OUT token(s) even though
  1032. * we have set ACK bit already, filter the case!
  1033. */
  1034. if (usb_aotg_dc.phase == USB_AOTG_OUT_STATUS) {
  1035. return;
  1036. }
  1037. /*
  1038. * Case 1: Setup, In Data, Out Status
  1039. *
  1040. * Status stage: no need to check busy
  1041. */
  1042. if (usb_aotg_dc.phase == USB_AOTG_IN_DATA) {
  1043. handle_status();
  1044. usb_aotg_dc.phase = USB_AOTG_OUT_STATUS;
  1045. return;
  1046. }
  1047. }
  1048. /* Do nothing */
  1049. return;
  1050. /*
  1051. * NOTE: out token only take cares of out status phase, don't
  1052. * use it for out data phase, because ep0 may be busy all the
  1053. * time and can never complete!
  1054. */
  1055. #if 0
  1056. usb_dc_ep_callback ep_cb;
  1057. while (usb_read8(EP0CS) & BIT(EP0CS_OUTBUSY)) {
  1058. ;
  1059. }
  1060. usb_aotg_dc.out_ep_ctrl[ep_idx].data_len = usb_read8(OUT0BC);
  1061. usb_aotg_dc.phase = USB_AOTG_OUT_DATA;
  1062. ep_cb = usb_aotg_dc.out_ep_ctrl[ep_idx].cb;
  1063. ep_idx = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_OUT);
  1064. /* Call the registered callback if any */
  1065. if (ep_cb) {
  1066. ep_cb(ep_idx, USB_DC_EP_DATA_OUT);
  1067. }
  1068. #endif
  1069. }
  1070. /*
  1071. * Handle EPxOUT Token
  1072. */
  1073. static inline void aotg_dc_handle_out_token(void)
  1074. {
  1075. uint8_t ep_msk, i;
  1076. /* Get endpoint mask */
  1077. ep_msk = (usb_read8(OUT_TOKIRQ) & usb_read8(OUT_TOKIEN));
  1078. /* NOTE: There may be more than one out token irqs simutaneously */
  1079. for (i = 0; i < EPOUT_TOKEN_NUM; i++) {
  1080. if (ep_msk & BIT(i)) {
  1081. /* Clear IRQ */
  1082. usb_write8(OUT_TOKIRQ, BIT(i));
  1083. aotg_dc_handle_out_token_internal(i);
  1084. }
  1085. }
  1086. }
  1087. /*
  1088. * Handle EPxIN Token
  1089. */
  1090. static inline void aotg_dc_handle_in_token(void)
  1091. {
  1092. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1093. uint8_t ep_msk, i;
  1094. /* Get endpoint mask */
  1095. ep_msk = usb_read8(IN_TOKIRQ) & usb_read8(IN_TOKIEN);
  1096. /* NOTE: There may be more than one in token irqs simutaneously */
  1097. for (i = 0; i < EPIN_TOKEN_NUM; i++) {
  1098. if (ep_msk & BIT(i)) {
  1099. /* Clear IRQ */
  1100. usb_write8(IN_TOKIRQ, BIT(i));
  1101. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[i];
  1102. /* Call the registered callback if any */
  1103. if (ep_ctrl->cb) {
  1104. ep_ctrl->cb(i, USB_DC_EP_DATA_IN);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. /*
  1110. * Handle EPxOUT Short Packet
  1111. */
  1112. static inline void aotg_dc_handle_short_packet(void)
  1113. {
  1114. usb_write8(OUT_SHTPKT, usb_read8(OUT_SHTPKT));
  1115. LOG_DBG("aotg short packet");
  1116. }
  1117. static inline void aotg_dc_isr_dispatch(uint8_t vector)
  1118. {
  1119. uint8_t usbeien = usb_read8(USBEIEN) & USBEIEN_MASK;
  1120. /* USB Resume */
  1121. if (vector == UIV_RESUME) {
  1122. aotg_dc_handle_resume(usbeien);
  1123. return;
  1124. }
  1125. /*
  1126. * Make sure external IRQ has been cleared right!
  1127. *
  1128. * TODO: If two or more IRQs are pending simultaneously,
  1129. * EIRQ will be pending immediately after cleared or not?
  1130. */
  1131. while (usb_read8(USBEIRQ) & BIT(USBEIRQ_EXTERN)) {
  1132. usb_write8(USBEIRQ, BIT(USBEIRQ_EXTERN) | usbeien);
  1133. }
  1134. switch (vector) {
  1135. /* OTG */
  1136. case UIV_OTGIRQ:
  1137. aotg_dc_handle_otg();
  1138. break;
  1139. /* USB Reset */
  1140. case UIV_USBRST:
  1141. aotg_dc_handle_reset();
  1142. break;
  1143. #ifdef CONFIG_USB_AOTG_OTG_SUPPORT_HS
  1144. /* High-speed */
  1145. case UIV_HSPEED:
  1146. aotg_dc_handle_hs();
  1147. break;
  1148. #endif
  1149. /* SOF */
  1150. case UIV_SOF:
  1151. aotg_dc_handle_sof();
  1152. break;
  1153. /* USB Suspend */
  1154. case UIV_SUSPEND:
  1155. aotg_dc_handle_suspend(usbeien);
  1156. break;
  1157. /* SETUP */
  1158. case UIV_SUDAV:
  1159. aotg_dc_handle_setup();
  1160. break;
  1161. /* EPxOUT */
  1162. case UIV_EP0OUT:
  1163. case UIV_EP1OUT:
  1164. case UIV_EP2OUT:
  1165. case UIV_EP3OUT:
  1166. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  1167. case UIV_EP4OUT:
  1168. case UIV_EP5OUT:
  1169. case UIV_EP6OUT:
  1170. #endif
  1171. aotg_dc_handle_epout(UIV_EPOUT_VEC2ADDR(vector));
  1172. break;
  1173. /* EPxIN */
  1174. case UIV_EP0IN:
  1175. case UIV_EP1IN:
  1176. case UIV_EP2IN:
  1177. case UIV_EP3IN:
  1178. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  1179. case UIV_EP4IN:
  1180. case UIV_EP5IN:
  1181. case UIV_EP6IN:
  1182. #endif
  1183. aotg_dc_handle_epin(UIV_EPIN_VEC2ADDR(vector));
  1184. break;
  1185. /* EPxIN empty */
  1186. case UIV_HCOUTEMPTY:
  1187. aotg_dc_handle_epin_empty();
  1188. break;
  1189. /*
  1190. * Using OUT Token for EP0OUT data transfer for assistance.
  1191. *
  1192. * AOTG handles in/out status by software (handle_status()),
  1193. * which means it depends on USB core totally. But USB core
  1194. * thinks that controller should take care of out status by
  1195. * itself (excluding int status phase). That means EP0 Data
  1196. * IRQ will never be pending until handle_status() called!
  1197. */
  1198. case UIV_OUTTOKEN:
  1199. aotg_dc_handle_out_token();
  1200. break;
  1201. case UIV_INTOKEN:
  1202. aotg_dc_handle_in_token();
  1203. break;
  1204. /* Short Packet */
  1205. case UIV_EPOUTSHTPKT:
  1206. aotg_dc_handle_short_packet();
  1207. break;
  1208. default:
  1209. break;
  1210. }
  1211. }
  1212. /* Disable soft disconnect */
  1213. static inline void aotg_dc_connect(void)
  1214. {
  1215. usb_clear_bit8(USBCS, USBCS_DISCONN);
  1216. }
  1217. /* Enable soft disconnect */
  1218. static inline void aotg_dc_disconnect(void)
  1219. {
  1220. usb_set_bit8(USBCS, USBCS_DISCONN);
  1221. }
  1222. static void usb_dc_hardware_init(void)
  1223. {
  1224. usb_write32(AVDDLDO_CTL, 0x05);
  1225. k_msleep(1);
  1226. usb_write32(SPLL_CTL, sys_read32(SPLL_CTL)|0x01);
  1227. k_msleep(1);
  1228. LOG_DBG("SPLL_CTL: 0x%x", sys_read32(SPLL_CTL));
  1229. acts_clock_peripheral_enable(CLOCK_ID_USB);
  1230. acts_clock_peripheral_enable(CLOCK_ID_USB2);
  1231. LOG_DBG("CMU_DEVCLKEN0: 0x%x", sys_read32(CMU_DEVCLKEN0));
  1232. }
  1233. int usb_dc_attach(void)
  1234. {
  1235. if (usb_aotg_dc.attached) {
  1236. LOG_DBG("already");
  1237. return 0;
  1238. }
  1239. usb_dc_hardware_init();
  1240. aotg_dc_fifo_enable();
  1241. usb_aotg_enable();
  1242. usb_write8(IDVBUSCTRL, IDVBUS_DEVICE);
  1243. usb_write8(DPDMCTRL, DPDM_HOST);
  1244. aotg_dc_phy_init();
  1245. #ifdef CONFIG_USB_AOTG_DC_FS
  1246. aotg_dc_force_fs();
  1247. #endif
  1248. /* Enable OTG(b_peripheral) interrupt */
  1249. usb_set_bit8(OTGIEN, OTGIEN_PERIPHERAL);
  1250. /* Enable external interrupt */
  1251. usb_set_bit8(USBEIEN, USBEIEN_EXTERN);
  1252. irq_enable(USB_AOTG_IRQ);
  1253. aotg_dc_connect();
  1254. usb_aotg_reg_dump();
  1255. /* status_cb is registered */
  1256. usb_aotg_dc.speed = USB_SPEED_UNKNOWN;
  1257. usb_aotg_dc.attached = 1;
  1258. LOG_DBG("");
  1259. return 0;
  1260. }
  1261. int usb_dc_detach(void)
  1262. {
  1263. unsigned int key = irq_lock();
  1264. uint8_t speed;
  1265. if (!usb_aotg_dc.attached) {
  1266. irq_unlock(key);
  1267. LOG_DBG("already");
  1268. return 0;
  1269. }
  1270. irq_disable(USB_AOTG_IRQ);
  1271. aotg_dc_disconnect();
  1272. #ifdef CONFIG_USB_AOTG_UDC_DMA
  1273. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_single);
  1274. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_burst8);
  1275. dma_stop(usb_aotg_dma.dma_dev, usb_aotg_dma.epin_dma);
  1276. #endif
  1277. /* recover speed */
  1278. speed = usb_aotg_dc.speed;
  1279. memset(&usb_aotg_dc, 0, sizeof(usb_aotg_dc));
  1280. usb_aotg_dc.speed = speed;
  1281. usb_aotg_reset();
  1282. usb_aotg_disable();
  1283. aotg_dc_fifo_disable();
  1284. irq_unlock(key);
  1285. LOG_DBG("");
  1286. return 0;
  1287. }
  1288. int usb_dc_reset(void)
  1289. {
  1290. LOG_DBG("");
  1291. /* Clear private data */
  1292. memset(&usb_aotg_dc, 0, sizeof(usb_aotg_dc));
  1293. return 0;
  1294. }
  1295. int usb_dc_set_address(const uint8_t addr)
  1296. {
  1297. if (addr > USB_AOTG_MAX_ADDR) {
  1298. return -EINVAL;
  1299. }
  1300. /* Respond "Set Address" automatically in device mode */
  1301. LOG_DBG("Set Address: %d", usb_read8(FNADDR));
  1302. /* Set Address phase */
  1303. usb_aotg_dc.phase = USB_AOTG_SET_ADDRESS;
  1304. /* Enable suspend */
  1305. usb_write8(USBIRQ, BIT(USBIRQ_SUSPEND));
  1306. usb_set_bit8(USBIEN, USBIEN_SUSPEND);
  1307. return 0;
  1308. }
  1309. int usb_dc_set_status_callback(const usb_dc_status_callback cb)
  1310. {
  1311. usb_aotg_dc.status_cb = cb;
  1312. return 0;
  1313. }
  1314. int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
  1315. {
  1316. LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
  1317. cfg->ep_type);
  1318. /*
  1319. * Check if the address of control endpoint is non-zero!
  1320. */
  1321. if ((cfg->ep_type == USB_DC_EP_CONTROL)
  1322. && USB_EP_ADDR2IDX(cfg->ep_addr)) {
  1323. LOG_ERR("invalid endpoint configuration");
  1324. return -EINVAL;
  1325. }
  1326. if (!aotg_dc_ep_mps_valid(cfg->ep_mps, cfg->ep_type)) {
  1327. LOG_WRN("unsupported packet size");
  1328. return -EINVAL;
  1329. }
  1330. if (!usb_aotg_ep_addr_valid(cfg->ep_addr)) {
  1331. LOG_WRN("endpoint 0x%x address out of range", cfg->ep_addr);
  1332. return -EINVAL;
  1333. }
  1334. return 0;
  1335. }
  1336. int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const ep_cfg)
  1337. {
  1338. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep_cfg->ep_addr))
  1339. return -EINVAL;
  1340. return aotg_dc_ep_set(ep_cfg->ep_addr, ep_cfg->ep_mps,
  1341. ep_cfg->ep_type);
  1342. }
  1343. int usb_dc_ep_set_stall(const uint8_t ep)
  1344. {
  1345. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1346. unsigned int key;
  1347. if (!usb_aotg_ep_addr_valid(ep)) {
  1348. return -EINVAL;
  1349. }
  1350. key = irq_lock();
  1351. if (!usb_aotg_dc.attached) {
  1352. irq_unlock(key);
  1353. return -ENODEV;
  1354. }
  1355. /* Endpoint 0 */
  1356. if (!ep_idx) {
  1357. usb_set_bit8(EP0CS, EP0CS_STALL);
  1358. irq_unlock(key);
  1359. return 0;
  1360. }
  1361. if (USB_EP_DIR_IS_OUT(ep)) {
  1362. usb_set_bit8(OUTxCTRL(ep_idx), EPCTRL_STALL);
  1363. /* clear pending in case data received before stalled */
  1364. usb_write8(OUTIRQ, BIT(ep_idx));
  1365. } else {
  1366. usb_set_bit8(INxCTRL(ep_idx), EPCTRL_STALL);
  1367. }
  1368. /* Reset the data toggle */
  1369. usb_aotg_ep_reset(ep, USB_AOTG_EP_TOGGLE_RESET);
  1370. irq_unlock(key);
  1371. return 0;
  1372. }
  1373. int usb_dc_ep_clear_stall(const uint8_t ep)
  1374. {
  1375. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1376. unsigned int key;
  1377. if (!usb_aotg_ep_addr_valid(ep)) {
  1378. return -EINVAL;
  1379. }
  1380. key = irq_lock();
  1381. if (!usb_aotg_dc.attached) {
  1382. irq_unlock(key);
  1383. return -ENODEV;
  1384. }
  1385. if (!ep_idx) {
  1386. /*
  1387. * case 1: EP0 stall will be cleared when SETUP token comes
  1388. * case 2: we can clear EP0CS_STALL bit to clear stall
  1389. */
  1390. usb_clear_bit8(EP0CS, EP0CS_STALL);
  1391. irq_unlock(key);
  1392. return 0;
  1393. }
  1394. if (USB_EP_DIR_IS_OUT(ep)) {
  1395. usb_clear_bit8(OUTxCTRL(ep_idx), EPCTRL_STALL);
  1396. } else {
  1397. usb_clear_bit8(INxCTRL(ep_idx), EPCTRL_STALL);
  1398. }
  1399. irq_unlock(key);
  1400. return 0;
  1401. }
  1402. int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
  1403. {
  1404. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1405. unsigned int key;
  1406. if (!usb_aotg_ep_addr_valid(ep)) {
  1407. return -EINVAL;
  1408. }
  1409. if (!stalled) {
  1410. return -EINVAL;
  1411. }
  1412. key = irq_lock();
  1413. if (!usb_aotg_dc.attached) {
  1414. irq_unlock(key);
  1415. return -ENODEV;
  1416. }
  1417. *stalled = 0;
  1418. /* Endpoint 0 */
  1419. if (!ep_idx && (usb_read8(EP0CS) & BIT(EP0CS_STALL))) {
  1420. *stalled = 1;
  1421. irq_unlock(key);
  1422. return 0;
  1423. }
  1424. if (USB_EP_DIR_IS_OUT(ep)) {
  1425. if (usb_read8(OUTxCTRL(ep_idx)) & BIT(EPCTRL_STALL)) {
  1426. *stalled = 1;
  1427. }
  1428. } else {
  1429. if (usb_read8(INxCTRL(ep_idx)) & BIT(EPCTRL_STALL)) {
  1430. *stalled = 1;
  1431. }
  1432. }
  1433. irq_unlock(key);
  1434. return 0;
  1435. }
  1436. int usb_dc_ep_halt(const uint8_t ep)
  1437. {
  1438. /*
  1439. * Just set stall, by the way no clear halt operation?
  1440. */
  1441. return usb_dc_ep_set_stall(ep);
  1442. }
  1443. int usb_dc_ep_enable(const uint8_t ep)
  1444. {
  1445. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1446. bool dir_out = USB_EP_DIR_IS_OUT(ep);
  1447. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep)) {
  1448. return -EINVAL;
  1449. }
  1450. if (usb_aotg_ep_reset(ep, USB_AOTG_EP_RESET)) {
  1451. return -EINVAL;
  1452. }
  1453. /* Enable EP interrupts */
  1454. if (dir_out) {
  1455. /* EP0 OUT uses OUT Token IRQ for assistance */
  1456. if (!ep_idx) {
  1457. usb_set_bit8(OUT_TOKIEN, EP0OUT_TOKEN);
  1458. }
  1459. if (!USB_EP_OUT_DMA_CAP(ep_idx)) {
  1460. usb_set_bit8(OUTIEN, ep_idx);
  1461. }
  1462. usb_aotg_dc.out_ep_ctrl[ep_idx].ep_ena = 1;
  1463. } else {
  1464. #if 0
  1465. /* Using In Token IRQ for isochronous transfer */
  1466. if (ep_type_isoc(usb_aotg_dc.in_ep_ctrl[ep_idx].ep_type)) {
  1467. usb_set_bit8(IN_TOKIEN, ep_idx);
  1468. } else {
  1469. usb_set_bit8(INIEN, ep_idx);
  1470. }
  1471. #else
  1472. if (!USB_EP_IN_DMA_CAP(ep_idx)) {
  1473. usb_set_bit8(INIEN, ep_idx);
  1474. }
  1475. #endif
  1476. usb_aotg_dc.in_ep_ctrl[ep_idx].ep_ena = 1;
  1477. }
  1478. /* Endpoint 0 */
  1479. if (!ep_idx) {
  1480. return 0;
  1481. }
  1482. if (dir_out) {
  1483. /* Activate Ep */
  1484. usb_set_bit8(OUTxCTRL(ep_idx), EPCTRL_VALID);
  1485. /* Short Packet */
  1486. /* usb_set_bit8(OUT_SHTPKT, ep_idx); */
  1487. } else {
  1488. usb_set_bit8(INxCTRL(ep_idx), EPCTRL_VALID);
  1489. }
  1490. /* Prepare EP for rx */
  1491. if (dir_out) {
  1492. aotg_dc_prep_rx(ep_idx);
  1493. }
  1494. usb_aotg_ep_reg_dump(ep);
  1495. return 0;
  1496. }
  1497. int usb_dc_ep_disable(const uint8_t ep)
  1498. {
  1499. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1500. bool dir_out = USB_EP_DIR_IS_OUT(ep);
  1501. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep)) {
  1502. return -EINVAL;
  1503. }
  1504. /* Disable EP interrupts */
  1505. if (dir_out) {
  1506. if (!ep_idx) {
  1507. usb_clear_bit8(OUT_TOKIEN, EP0OUT_TOKEN);
  1508. }
  1509. usb_clear_bit8(OUTIEN, ep_idx);
  1510. usb_aotg_dc.out_ep_ctrl[ep_idx].ep_ena = 0;
  1511. } else {
  1512. usb_clear_bit8(INIEN, ep_idx);
  1513. usb_aotg_dc.in_ep_ctrl[ep_idx].ep_ena = 0;
  1514. }
  1515. /* Endpoint 0 */
  1516. if (!ep_idx) {
  1517. return 0;
  1518. }
  1519. /* De-activate Ep */
  1520. if (dir_out) {
  1521. usb_clear_bit8(OUTxCTRL(ep_idx), EPCTRL_VALID);
  1522. } else {
  1523. usb_clear_bit8(INxCTRL(ep_idx), EPCTRL_VALID);
  1524. }
  1525. return 0;
  1526. }
  1527. int usb_dc_ep_flush(const uint8_t ep)
  1528. {
  1529. unsigned int key;
  1530. int ret;
  1531. if (!usb_aotg_ep_addr_valid(ep)) {
  1532. return -EINVAL;
  1533. }
  1534. key = irq_lock();
  1535. if (!usb_aotg_dc.attached) {
  1536. irq_unlock(key);
  1537. return -ENODEV;
  1538. }
  1539. ret = usb_aotg_ep_reset(ep, USB_AOTG_EP_FIFO_RESET);
  1540. irq_unlock(key);
  1541. return ret;
  1542. }
  1543. /*
  1544. * legacy mode: write mps at most and depends on upper layer to write again
  1545. * if data_len >= mps for all out endpoints.
  1546. * new mode: write once (controller will handle if data_len >= mps) for all
  1547. * non-control endpoints, control endpoints still works in legacy
  1548. * mode.
  1549. */
  1550. int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
  1551. const uint32_t data_len, uint32_t * const ret_bytes)
  1552. {
  1553. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1554. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1555. unsigned int key;
  1556. int ret;
  1557. uint32_t len;
  1558. if (!usb_aotg_ep_addr_valid(ep)) {
  1559. LOG_ERR("No valid endpoint");
  1560. return -EINVAL;
  1561. }
  1562. /* Check if IN ep */
  1563. if (!USB_EP_DIR_IS_IN(ep)) {
  1564. LOG_ERR("Wrong endpoint direction");
  1565. return -EINVAL;
  1566. }
  1567. /* Check if ep enabled */
  1568. if (!aotg_dc_ep_is_enabled(ep)) {
  1569. LOG_ERR("Not enabled endpoint");
  1570. return -EINVAL;
  1571. }
  1572. key = irq_lock();
  1573. if (!usb_aotg_dc.attached) {
  1574. irq_unlock(key);
  1575. return -ENODEV;
  1576. }
  1577. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[ep_idx];
  1578. #ifdef CONFIG_USB_AOTG_UDC_DMA
  1579. /* need 32-bit alignment */
  1580. if (((long)data & 0x3) != 0) {
  1581. goto cpu_write;
  1582. }
  1583. if (USB_EP_IN_DMA_CAP(ep_idx)) {
  1584. if ((data_len < USB_AOTG_DMA_BURST8_LEN) ||
  1585. (data_len % USB_AOTG_DMA_BURST8_LEN)) {
  1586. goto cpu_write;
  1587. }
  1588. if (data_len >= USB_AOTG_DMA_MAX_SIZE) {
  1589. len = USB_AOTG_DMA_MAX_LEN;
  1590. } else {
  1591. len = data_len;
  1592. }
  1593. ep_ctrl->data_len = len;
  1594. /* Disable EP interrupt */
  1595. usb_clear_bit8(INIEN, ep_idx);
  1596. usb_write8(IN1_DMACTL, BIT(DMACTL_FIFORST));
  1597. usb_write8(IN1_DMACTL, 0);
  1598. usb_write8(IN1_DMALEN1L, (uint8_t)len);
  1599. usb_write8(IN1_DMALEN1M, (uint8_t)(len >> 8));
  1600. usb_write8(IN1_DMALEN1H, (uint8_t)(len >> 16) | (0x1 << 1));
  1601. usb_write8(IN1_DMALEN2H, 0);
  1602. usb_write8(IN1_DMACTL, BIT(DMACTL_START));
  1603. dma_reload(usb_aotg_dma.dma_dev, usb_aotg_dma.epin_dma,
  1604. (uint32_t)data, FIFO1DAT, len);
  1605. dma_start(usb_aotg_dma.dma_dev, usb_aotg_dma.epin_dma);
  1606. if (ret_bytes) {
  1607. *ret_bytes = len;
  1608. }
  1609. irq_unlock(key);
  1610. return 0;
  1611. }
  1612. cpu_write:
  1613. #endif
  1614. ep_ctrl->data = NULL;
  1615. len = data_len > ep_ctrl->mps ? ep_ctrl->mps : data_len;
  1616. if (ep_idx != 0) {
  1617. goto new_mode;
  1618. }
  1619. ret = aotg_dc_tx(ep, data, len);
  1620. if (ret < 0) {
  1621. irq_unlock(key);
  1622. return ret;
  1623. }
  1624. if (ret_bytes) {
  1625. *ret_bytes = ret;
  1626. }
  1627. if (usb_aotg_dc.phase != USB_AOTG_IN_DATA) {
  1628. irq_unlock(key);
  1629. return 0;
  1630. }
  1631. usb_aotg_dc.in_ep_ctrl[ep_idx].data_len -= ret;
  1632. if ((usb_aotg_dc.in_ep_ctrl[ep_idx].data_len == 0) ||
  1633. (ret < MAX_PACKET_SIZE0)) {
  1634. /* Case 1: Setup, In Data, Out Status */
  1635. handle_status();
  1636. usb_aotg_dc.phase = USB_AOTG_OUT_STATUS;
  1637. }
  1638. irq_unlock(key);
  1639. return 0;
  1640. new_mode:
  1641. LOG_DBG("%d", data_len);
  1642. ep_ctrl->data_len = data_len - len;
  1643. ep_ctrl->data = (uint8_t *)data + len;
  1644. ret = ep_write_fifo(ep, data, len);
  1645. if (ret < 0) {
  1646. irq_unlock(key);
  1647. return ret;
  1648. }
  1649. if (ret_bytes) {
  1650. *ret_bytes = data_len;
  1651. }
  1652. irq_unlock(key);
  1653. return 0;
  1654. }
  1655. int usb_dc_ep_read(const uint8_t ep, uint8_t *const data,
  1656. const uint32_t max_data_len, uint32_t * const read_bytes)
  1657. {
  1658. if (usb_dc_ep_read_wait(ep, data, max_data_len, read_bytes) != 0) {
  1659. return -EINVAL;
  1660. }
  1661. if (!data && !max_data_len) {
  1662. /* When both buffer and max data to read are zero the above
  1663. * call would fetch the data len and we simply return.
  1664. */
  1665. return 0;
  1666. }
  1667. if (usb_dc_ep_read_continue(ep) != 0) {
  1668. return -EINVAL;
  1669. }
  1670. return 0;
  1671. }
  1672. int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
  1673. {
  1674. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1675. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep)) {
  1676. return -EINVAL;
  1677. }
  1678. if (USB_EP_DIR_IS_IN(ep)) {
  1679. usb_aotg_dc.in_ep_ctrl[ep_idx].cb = cb;
  1680. } else {
  1681. usb_aotg_dc.out_ep_ctrl[ep_idx].cb = cb;
  1682. }
  1683. return 0;
  1684. }
  1685. /*
  1686. * read() and read_async() co-exist, which means fisrt read_async() should be
  1687. * called ASAP to make sure outirq is after read_async(). It is recommended
  1688. * called in status_cb().
  1689. */
  1690. int usb_dc_ep_read_async(uint8_t ep, uint8_t *data, uint32_t max_data_len,
  1691. uint32_t *read_bytes)
  1692. {
  1693. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1694. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1695. unsigned int key;
  1696. if (!usb_aotg_ep_addr_valid(ep)) {
  1697. LOG_ERR("No valid endpoint");
  1698. return -EINVAL;
  1699. }
  1700. /* Check if OUT ep */
  1701. if (!USB_EP_DIR_IS_OUT(ep)) {
  1702. LOG_ERR("Wrong endpoint direction");
  1703. return -EINVAL;
  1704. }
  1705. /* Allow to read 0 bytes */
  1706. if (!data && max_data_len) {
  1707. LOG_ERR("Wrong arguments");
  1708. return -EINVAL;
  1709. }
  1710. /* Check if ep enabled */
  1711. if (!aotg_dc_ep_is_enabled(ep)) {
  1712. LOG_ERR("Not enabled endpoint");
  1713. return -EINVAL;
  1714. }
  1715. key = irq_lock();
  1716. if (!usb_aotg_dc.attached) {
  1717. irq_unlock(key);
  1718. return -ENODEV;
  1719. }
  1720. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  1721. #ifdef CONFIG_USB_AOTG_UDC_DMA
  1722. /* need 32-bit alignment */
  1723. if (((long)data & 0x3) != 0) {
  1724. goto cpu_mode;
  1725. }
  1726. if (!USB_EP_OUT_DMA_CAP(ep_idx)) {
  1727. LOG_DBG("for DMA only");
  1728. goto cpu_mode;
  1729. }
  1730. if (max_data_len >= USB_AOTG_DMA_MAX_SIZE) {
  1731. max_data_len = USB_AOTG_DMA_MAX_LEN;
  1732. }
  1733. ep_ctrl->data_len = max_data_len;
  1734. ep_ctrl->actual = 0;
  1735. usb_write8(OUT2_DMACTL, BIT(DMACTL_FIFORST));
  1736. usb_write8(OUT2_DMACTL, 0);
  1737. usb_write8(OUT2_DMALENL, (uint8_t)max_data_len);
  1738. usb_write8(OUT2_DMALENM, (uint8_t)(max_data_len >> 8));
  1739. usb_write8(OUT2_DMALENH, (uint8_t)(max_data_len >> 16));
  1740. if ((max_data_len >= USB_AOTG_DMA_BURST8_LEN) &&
  1741. !(max_data_len % USB_AOTG_DMA_BURST8_LEN)) {
  1742. usb_write8(OUT2_DMACTL, BIT(DMACTL_START));
  1743. dma_reload(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_burst8,
  1744. FIFO2DAT, (uint32_t)data, max_data_len);
  1745. dma_start(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_burst8);
  1746. } else {
  1747. usb_write8(OUT2_DMACTL, BIT(DMACTL_MODE) | BIT(DMACTL_START));
  1748. dma_reload(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_single,
  1749. FIFO2DAT, (uint32_t)data, max_data_len);
  1750. dma_start(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_single);
  1751. }
  1752. if (read_bytes) {
  1753. *read_bytes = max_data_len;
  1754. }
  1755. irq_unlock(key);
  1756. return 0;
  1757. cpu_mode:
  1758. #endif
  1759. /* FIXME: always return max_data_len */
  1760. if (read_bytes) {
  1761. *read_bytes = max_data_len;
  1762. }
  1763. LOG_DBG("%d", max_data_len);
  1764. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  1765. ep_ctrl->data_len = max_data_len;
  1766. ep_ctrl->data = data;
  1767. ep_ctrl->actual = 0;
  1768. /* Enable interrupt */
  1769. usb_set_bit8(OUTIEN, ep_idx);
  1770. aotg_dc_prep_rx(ep_idx);
  1771. irq_unlock(key);
  1772. return 0;
  1773. }
  1774. int usb_dc_ep_read_actual(uint8_t ep, uint32_t *read_bytes)
  1775. {
  1776. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1777. uint8_t ep_idx;
  1778. if (!USB_EP_DIR_IS_OUT(ep)) {
  1779. return -EINVAL;
  1780. }
  1781. ep_idx = USB_EP_ADDR2IDX(ep) & USB_EP_NUM_MASK;
  1782. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  1783. if (read_bytes) {
  1784. *read_bytes = ep_ctrl->actual;
  1785. }
  1786. return 0;
  1787. }
  1788. #ifdef CONFIG_USB_AOTG_DC_MULTI_FIFO
  1789. static uint8_t usb_dc_ep_io_cancelled;
  1790. int usb_dc_ep_set_multi_fifo(uint8_t ep)
  1791. {
  1792. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1793. if (USB_EP_DIR_IS_OUT(ep)) {
  1794. usb_aotg_dc.out_ep_ctrl[ep_idx].multi = 1;
  1795. } else {
  1796. usb_aotg_dc.in_ep_ctrl[ep_idx].multi = 1;
  1797. }
  1798. return 0;
  1799. }
  1800. /*
  1801. * Specific for epin multi-fito non-auto mode, depends on class driver heavily!
  1802. * Until now, only tested for mass storage.
  1803. *
  1804. * for short packet: just like write() with new mode.
  1805. * for others: write data_len bytes synchronously.
  1806. */
  1807. int usb_dc_ep_write_pending(const uint8_t ep, uint8_t *data,
  1808. uint32_t data_len, uint32_t * const ret_bytes)
  1809. {
  1810. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1811. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1812. int ret;
  1813. uint32_t len;
  1814. uint16_t count;
  1815. uint8_t in_empty;
  1816. uint8_t npak;
  1817. uint32_t left;
  1818. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep)) {
  1819. LOG_ERR("No valid endpoint");
  1820. return -EINVAL;
  1821. }
  1822. /* for non-control endpoints */
  1823. if (ep_idx == 0) {
  1824. LOG_ERR("control endpoint");
  1825. return -EINVAL;
  1826. }
  1827. /* Check if IN ep */
  1828. if (!USB_EP_DIR_IS_IN(ep)) {
  1829. LOG_ERR("Wrong endpoint direction");
  1830. return -EINVAL;
  1831. }
  1832. /* Check if ep enabled */
  1833. if (!aotg_dc_ep_is_enabled(ep)) {
  1834. LOG_ERR("Not enabled endpoint");
  1835. return -EINVAL;
  1836. }
  1837. LOG_DBG("%d", data_len);
  1838. /* Disable interrupt always */
  1839. usb_clear_bit8(INIEN, ep_idx);
  1840. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[ep_idx];
  1841. if (data_len < ep_ctrl->mps) {
  1842. goto write;
  1843. }
  1844. usb_dc_ep_io_cancelled = 0;
  1845. npak = usb_read8(INxCS(ep_idx)) & EPCS_NPAK_MASK;
  1846. left = data_len;
  1847. while (left) {
  1848. if (left >= ep_ctrl->mps) {
  1849. len = ep_ctrl->mps;
  1850. } else {
  1851. len = left;
  1852. }
  1853. ep_write_fifo(ep, data, len);
  1854. left -= len;
  1855. data += len;
  1856. LOG_DBG("CS: 0x%x", usb_read8(INxCS(ep_idx)));
  1857. count = 0;
  1858. while (usb_read8(INxCS(ep_idx)) & BIT(EPCS_BUSY)) {
  1859. if (usb_dc_ep_io_cancelled) {
  1860. goto exit;
  1861. }
  1862. if (++count < 1000) {
  1863. k_busy_wait(1);
  1864. continue;
  1865. }
  1866. k_sleep(K_MSEC(20));
  1867. /* timeout: 1000us + 200ms */
  1868. if (count > 1010) {
  1869. LOG_ERR("timeout: 0x%x", usb_read8(INxCS(ep_idx)));
  1870. goto exit;
  1871. }
  1872. }
  1873. }
  1874. /* wait for fifo empty */
  1875. count = 0;
  1876. while (1) {
  1877. LOG_DBG("last CS: 0x%x", usb_read8(INxCS(ep_idx)));
  1878. if ((usb_read8(INxCS(ep_idx)) & EPCS_NPAK_MASK) == npak) {
  1879. break;
  1880. }
  1881. if (usb_dc_ep_io_cancelled) {
  1882. goto exit;
  1883. }
  1884. if (++count < 1000) {
  1885. k_busy_wait(1);
  1886. continue;
  1887. }
  1888. k_sleep(K_MSEC(20));
  1889. /* timeout: 1000us + 200ms */
  1890. if (count > 1010) {
  1891. LOG_ERR("last timeout 0x%x", usb_read8(INxCS(ep_idx)));
  1892. goto exit;
  1893. }
  1894. }
  1895. /* Call the registered callback if any */
  1896. if (ep_ctrl->cb) {
  1897. ep_ctrl->cb(ep, USB_DC_EP_DATA_IN);
  1898. }
  1899. exit:
  1900. if (ret_bytes) {
  1901. *ret_bytes = data_len - left;
  1902. }
  1903. LOG_DBG("done CS: 0x%x", usb_read8(INxCS(ep_idx)));
  1904. return 0;
  1905. write:
  1906. in_empty = usb_read8(INEMPTY_IEN) & INEMPTY_IEN_MASK;
  1907. /* clear pending */
  1908. usb_write8(INEMPTY_IRQ, BIT(IRQ_INxEMPTY(ep_idx)) | in_empty);
  1909. ret = ep_write_fifo(ep, data, data_len);
  1910. if (ret < 0) {
  1911. return ret;
  1912. }
  1913. /* enable empty */
  1914. usb_write8(INEMPTY_IEN, BIT(IEN_INxEMPTY(ep_idx)) | in_empty);
  1915. if (ret_bytes) {
  1916. *ret_bytes = data_len;
  1917. }
  1918. return 0;
  1919. }
  1920. /*
  1921. * Specific for epout multi-fito auto mode, depends on class driver heavily!
  1922. * Until now, only tested for mass storage.
  1923. *
  1924. * for short packet: just like read_async(), but no need to set busy.
  1925. * for others: read max_data_len bytes synchronously.
  1926. */
  1927. int usb_dc_ep_read_pending(uint8_t ep, uint8_t *data, uint32_t data_len,
  1928. uint32_t *read_bytes)
  1929. {
  1930. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  1931. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  1932. uint32_t len;
  1933. uint16_t count;
  1934. uint32_t left;
  1935. if (!usb_aotg_dc.attached || !usb_aotg_ep_addr_valid(ep)) {
  1936. LOG_ERR("No valid endpoint");
  1937. return -EINVAL;
  1938. }
  1939. /* for non-control endpoints */
  1940. if (ep_idx == 0) {
  1941. LOG_ERR("control endpoint");
  1942. return -EINVAL;
  1943. }
  1944. /* Check if OUT ep */
  1945. if (!USB_EP_DIR_IS_OUT(ep)) {
  1946. LOG_ERR("Wrong endpoint direction");
  1947. return -EINVAL;
  1948. }
  1949. /* Allow to read 0 bytes */
  1950. if (!data && data_len) {
  1951. LOG_ERR("Wrong arguments");
  1952. return -EINVAL;
  1953. }
  1954. /* Check if ep enabled */
  1955. if (!aotg_dc_ep_is_enabled(ep)) {
  1956. LOG_ERR("Not enabled endpoint");
  1957. return -EINVAL;
  1958. }
  1959. LOG_DBG("%d", data_len);
  1960. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[ep_idx];
  1961. if (data_len < ep_ctrl->mps) {
  1962. goto read_async;
  1963. }
  1964. /* Disable interrupt */
  1965. usb_clear_bit8(OUTIEN, ep_idx);
  1966. usb_dc_ep_io_cancelled = 0;
  1967. left = data_len;
  1968. while (left) {
  1969. count = 0;
  1970. while (usb_read8(OUTxCS(ep_idx)) & BIT(EPCS_BUSY)) {
  1971. if (usb_dc_ep_io_cancelled) {
  1972. goto exit;
  1973. }
  1974. if (++count < 1000) {
  1975. k_busy_wait(1);
  1976. continue;
  1977. }
  1978. k_sleep(K_MSEC(20));
  1979. /* timeout: 1000us + 200ms */
  1980. if (count > 1010) {
  1981. LOG_ERR("timeout: 0x%x left: %d",
  1982. usb_read8(OUTxCS(ep_idx)), left);
  1983. goto exit;
  1984. }
  1985. }
  1986. len = usb_read16(OUTxBC(ep_idx));
  1987. LOG_DBG("BC: %d", len);
  1988. ep_read_fifo(ep, data, len);
  1989. left -= len;
  1990. data += len;
  1991. LOG_DBG("CS: 0x%x", usb_read8(OUTxCS(ep_idx)));
  1992. }
  1993. exit:
  1994. if (read_bytes) {
  1995. *read_bytes = data_len - left;
  1996. }
  1997. /* Clear EPxIN IRQ */
  1998. usb_write8(OUTIRQ, BIT(ep_idx));
  1999. /* Call the registered callback if any */
  2000. if (ep_ctrl->cb) {
  2001. ep_ctrl->cb(ep, USB_DC_EP_DATA_OUT);
  2002. }
  2003. return 0;
  2004. read_async:
  2005. /* FIXME: always return max_data_len */
  2006. if (read_bytes) {
  2007. *read_bytes = data_len;
  2008. }
  2009. ep_ctrl->data_len = data_len;
  2010. ep_ctrl->data = data;
  2011. /* Enable interrupt */
  2012. usb_set_bit8(OUTIEN, ep_idx);
  2013. return 0;
  2014. }
  2015. int usb_dc_ep_io_cancel(void)
  2016. {
  2017. usb_dc_ep_io_cancelled = 1;
  2018. return 0;
  2019. }
  2020. #endif /* CONFIG_USB_AOTG_DC_MULTI_FIFO */
  2021. int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
  2022. uint32_t *read_bytes)
  2023. {
  2024. uint32_t data_len, bytes_to_copy;
  2025. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  2026. unsigned int key;
  2027. if (!usb_aotg_ep_addr_valid(ep)) {
  2028. LOG_ERR("No valid endpoint");
  2029. return -EINVAL;
  2030. }
  2031. /* Check if OUT ep */
  2032. if (!USB_EP_DIR_IS_OUT(ep)) {
  2033. LOG_ERR("Wrong endpoint direction");
  2034. return -EINVAL;
  2035. }
  2036. /* Allow to read 0 bytes */
  2037. if (!data && max_data_len) {
  2038. LOG_ERR("Wrong arguments");
  2039. return -EINVAL;
  2040. }
  2041. /* Check if ep enabled */
  2042. if (!aotg_dc_ep_is_enabled(ep)) {
  2043. LOG_ERR("Not enabled endpoint");
  2044. return -EINVAL;
  2045. }
  2046. data_len = usb_aotg_dc.out_ep_ctrl[ep_idx].data_len;
  2047. if (!data && !max_data_len) {
  2048. /* When both buffer and max data to read are zero return
  2049. * the available data in buffer
  2050. */
  2051. if (read_bytes) {
  2052. *read_bytes = data_len;
  2053. }
  2054. return 0;
  2055. }
  2056. if (data_len > max_data_len) {
  2057. LOG_ERR("Not enough room (%d) to copy all the rcvd data(%d)!",
  2058. max_data_len, data_len);
  2059. bytes_to_copy = max_data_len;
  2060. } else {
  2061. bytes_to_copy = data_len;
  2062. }
  2063. LOG_DBG("Read EP%d, req %d, read %d bytes",
  2064. ep, max_data_len, bytes_to_copy);
  2065. key = irq_lock();
  2066. if (!usb_aotg_dc.attached) {
  2067. irq_unlock(key);
  2068. return -ENODEV;
  2069. }
  2070. aotg_dc_rx(ep, data, bytes_to_copy);
  2071. irq_unlock(key);
  2072. usb_aotg_dc.out_ep_ctrl[ep_idx].data_len -= bytes_to_copy;
  2073. if (read_bytes) {
  2074. *read_bytes = bytes_to_copy;
  2075. }
  2076. return 0;
  2077. }
  2078. int usb_dc_ep_read_continue(uint8_t ep)
  2079. {
  2080. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  2081. unsigned int key;
  2082. if (!usb_aotg_ep_addr_valid(ep)) {
  2083. LOG_ERR("No valid endpoint");
  2084. return -EINVAL;
  2085. }
  2086. /* Check if OUT ep */
  2087. if (!USB_EP_DIR_IS_OUT(ep)) {
  2088. LOG_ERR("Wrong endpoint direction");
  2089. return -EINVAL;
  2090. }
  2091. if (!usb_aotg_dc.out_ep_ctrl[ep_idx].data_len) {
  2092. key = irq_lock();
  2093. if (!usb_aotg_dc.attached) {
  2094. irq_unlock(key);
  2095. return -ENODEV;
  2096. }
  2097. aotg_dc_prep_rx(ep_idx);
  2098. irq_unlock(key);
  2099. }
  2100. return 0;
  2101. }
  2102. int usb_dc_ep_mps(const uint8_t ep)
  2103. {
  2104. uint8_t ep_idx = USB_EP_ADDR2IDX(ep);
  2105. if (USB_EP_DIR_IS_OUT(ep)) {
  2106. return usb_aotg_dc.out_ep_ctrl[ep_idx].mps;
  2107. } else {
  2108. return usb_aotg_dc.in_ep_ctrl[ep_idx].mps;
  2109. }
  2110. }
  2111. #ifdef CONFIG_USB_AOTG_UDC_DMA
  2112. static void usb_aotg_epin_dma_callback(const struct device *dev, void *data,
  2113. uint32_t channel, int reason)
  2114. {
  2115. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  2116. uint8_t ep;
  2117. if (reason != DMA_IRQ_TC) {
  2118. return;
  2119. }
  2120. ep_ctrl = &usb_aotg_dc.in_ep_ctrl[USB_AOTG_IN_EP_1];
  2121. /* Set busy */
  2122. if (ep_ctrl->data_len < ep_ctrl->mps) {
  2123. usb_set_bit8(INxCS(USB_AOTG_IN_EP_1), EPCS_BUSY);
  2124. }
  2125. ep = USB_EP_IDX2ADDR(USB_AOTG_IN_EP_1, USB_EP_DIR_IN);
  2126. /* Call the registered callback if any */
  2127. if (ep_ctrl->cb) {
  2128. ep_ctrl->cb(ep, USB_DC_EP_DATA_IN);
  2129. }
  2130. }
  2131. static void usb_aotg_epout_dma_callback(const struct device *dev, void *data,
  2132. uint32_t channel, int reason)
  2133. {
  2134. struct aotg_dc_ep_ctrl_prv *ep_ctrl;
  2135. if (reason != DMA_IRQ_TC) {
  2136. return;
  2137. }
  2138. ep_ctrl = &usb_aotg_dc.out_ep_ctrl[USB_AOTG_OUT_EP_2];
  2139. ep_ctrl->actual = ep_ctrl->data_len;
  2140. /* Call the registered callback if any */
  2141. if (ep_ctrl->cb) {
  2142. ep_ctrl->cb(USB_AOTG_OUT_EP_2, USB_DC_EP_DATA_OUT);
  2143. }
  2144. }
  2145. static int usb_aotg_dma_init(void)
  2146. {
  2147. int ret;
  2148. usb_aotg_dma.dma_dev = device_get_binding(CONFIG_DMA_0_NAME);
  2149. if (!usb_aotg_dma.dma_dev) {
  2150. printk("%s no dma_dev\n", __func__);
  2151. return -ENODEV;
  2152. }
  2153. /*
  2154. * USB epout single DMA
  2155. */
  2156. usb_aotg_dma.epout_dma_single = dma_request(usb_aotg_dma.dma_dev, 0xff);
  2157. if (usb_aotg_dma.epout_dma_single < 0) {
  2158. printk("%s epout single dma_request\n", __func__);
  2159. return -EINVAL;
  2160. }
  2161. usb_aotg_dma.epout_dma_config_single.channel_direction = PERIPHERAL_TO_MEMORY;
  2162. usb_aotg_dma.epout_dma_config_single.dma_callback = usb_aotg_epout_dma_callback;
  2163. usb_aotg_dma.epout_dma_config_single.source_data_size = 1;
  2164. usb_aotg_dma.epout_dma_config_single.source_burst_length = 1;
  2165. usb_aotg_dma.epout_dma_config_single.dma_slot = DMA_ID_USB0;
  2166. usb_aotg_dma.epout_dma_config_single.complete_callback_en = 1;
  2167. usb_aotg_dma.epout_dma_config_single.head_block = &usb_aotg_dma.epout_dma_block_single;
  2168. ret = dma_config(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_single,
  2169. &usb_aotg_dma.epout_dma_config_single);
  2170. if (ret) {
  2171. printk("%s epout single dma_config %d\n", __func__, ret);
  2172. return -EINVAL;
  2173. }
  2174. /*
  2175. * USB epout burst8 DMA
  2176. */
  2177. usb_aotg_dma.epout_dma_burst8 = dma_request(usb_aotg_dma.dma_dev, 0xff);
  2178. if (usb_aotg_dma.epout_dma_burst8 < 0) {
  2179. printk("%s epout burst8 dma_request\n", __func__);
  2180. return -EINVAL;
  2181. }
  2182. usb_aotg_dma.epout_dma_config_burst8.channel_direction = PERIPHERAL_TO_MEMORY;
  2183. usb_aotg_dma.epout_dma_config_burst8.dma_callback = usb_aotg_epout_dma_callback;
  2184. usb_aotg_dma.epout_dma_config_burst8.source_data_size = 4;
  2185. usb_aotg_dma.epout_dma_config_burst8.source_burst_length = 0;
  2186. usb_aotg_dma.epout_dma_config_burst8.dma_slot = DMA_ID_USB0;
  2187. usb_aotg_dma.epout_dma_config_burst8.complete_callback_en = 1;
  2188. usb_aotg_dma.epout_dma_config_burst8.head_block = &usb_aotg_dma.epout_dma_block_burst8;
  2189. ret = dma_config(usb_aotg_dma.dma_dev, usb_aotg_dma.epout_dma_burst8,
  2190. &usb_aotg_dma.epout_dma_config_burst8);
  2191. if (ret) {
  2192. printk("%s epout burst8 dma_config %d\n", __func__, ret);
  2193. return -EINVAL;
  2194. }
  2195. /*
  2196. * USB epin burst8 DMA
  2197. */
  2198. usb_aotg_dma.epin_dma = dma_request(usb_aotg_dma.dma_dev, 0xff);
  2199. if (usb_aotg_dma.epin_dma < 0) {
  2200. printk("%s epin dma_request\n", __func__);
  2201. return -EINVAL;
  2202. }
  2203. usb_aotg_dma.epin_dma_config.channel_direction = MEMORY_TO_PERIPHERAL;
  2204. usb_aotg_dma.epin_dma_config.dma_callback = usb_aotg_epin_dma_callback;
  2205. usb_aotg_dma.epin_dma_config.source_data_size = 4;
  2206. usb_aotg_dma.epout_dma_config_burst8.source_burst_length = 0;
  2207. usb_aotg_dma.epin_dma_config.dma_slot = DMA_ID_USB0;
  2208. usb_aotg_dma.epin_dma_config.complete_callback_en = 1;
  2209. usb_aotg_dma.epin_dma_config.head_block = &usb_aotg_dma.epin_dma_block;
  2210. ret = dma_config(usb_aotg_dma.dma_dev, usb_aotg_dma.epin_dma,
  2211. &usb_aotg_dma.epin_dma_config);
  2212. if (ret) {
  2213. printk("%s epin dma_config %d\n", __func__, ret);
  2214. return -EINVAL;
  2215. }
  2216. return 0;
  2217. }
  2218. #endif
  2219. enum usb_device_speed usb_dc_maxspeed(void)
  2220. {
  2221. #ifdef CONFIG_USB_AOTG_OTG_SUPPORT_HS
  2222. #ifdef CONFIG_USB_AOTG_DC_FS
  2223. return USB_SPEED_FULL;
  2224. #else
  2225. return USB_SPEED_HIGH;
  2226. #endif
  2227. #else
  2228. return USB_SPEED_FULL;
  2229. #endif
  2230. }
  2231. enum usb_device_speed usb_dc_speed(void)
  2232. {
  2233. return usb_aotg_dc.speed;
  2234. }
  2235. int usb_dc_do_remote_wakeup(void)
  2236. {
  2237. usb_set_bit8(USBCS, USBCS_WAKEUP);
  2238. return 0;
  2239. }
  2240. #endif /* CONFIG_USB_AOTG_DC_ENABLED */
  2241. /*
  2242. * Host related
  2243. */
  2244. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  2245. static inline void aotg_hc_handle_reset(void);
  2246. /*
  2247. * Map endpoint address of peripheral to controller's
  2248. *
  2249. * mapping rules:
  2250. * 1. out_ep_ctrl[idx] used for peripheral's ep-in
  2251. * 2. in_ep_ctrl[idx] used for peripheral's ep-out
  2252. *
  2253. * for examples:
  2254. * 1. 0x81(peri_ep) -- 0x01(aotg_ep) -- dir_out -- out_ep_ctrl
  2255. * 2. 0x01(peri_ep) -- 0x81(aotg_ep) -- dir_in --in_ep_ctrl
  2256. * 3. 0x80(peri_ep) -- 0x00(aotg_ep) -- dir_out -- out_ep_ctrl
  2257. * 4. 0x00(peri_ep) -- 0x80(aotg_ep) -- dir_in --in_ep_ctrl
  2258. */
  2259. static inline int ep_map(uint8_t ep)
  2260. {
  2261. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2262. uint8_t i;
  2263. /* suppose ep0-in is 0x80 and ep0-out is 0x0 always */
  2264. if (ep == USB_CONTROL_OUT_EP0) {
  2265. usb_aotg_hc.in_ep_ctrl[0].ep_addr = ep;
  2266. return 0;
  2267. } else if (ep == USB_CONTROL_IN_EP0) {
  2268. usb_aotg_hc.out_ep_ctrl[0].ep_addr = ep;
  2269. return 0;
  2270. }
  2271. if (USB_EP_DIR_IS_OUT(ep)) {
  2272. for (i = USB_AOTG_IN_EP_NUM - 1; i > USB_AOTG_EP0_IDX; i--) {
  2273. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[i];
  2274. if (ep_ctrl->ep_ena) {
  2275. continue;
  2276. }
  2277. ep_ctrl->ep_addr = ep;
  2278. return 0;
  2279. }
  2280. } else {
  2281. for (i = USB_AOTG_OUT_EP_1; i < USB_AOTG_OUT_EP_NUM; i++) {
  2282. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[i];
  2283. if (ep_ctrl->ep_ena) {
  2284. continue;
  2285. }
  2286. ep_ctrl->ep_addr = ep;
  2287. return 0;
  2288. }
  2289. }
  2290. return -EINVAL;
  2291. }
  2292. /*
  2293. * Convert endpoint address of peripheral to controller's
  2294. */
  2295. static inline uint8_t find_ep(uint8_t ep)
  2296. {
  2297. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2298. uint8_t i;
  2299. if (ep == USB_CONTROL_OUT_EP0) {
  2300. return USB_CONTROL_IN_EP0;
  2301. } else if (ep == USB_CONTROL_IN_EP0) {
  2302. return USB_CONTROL_OUT_EP0;
  2303. }
  2304. if (USB_EP_DIR_IS_OUT(ep)) {
  2305. for (i = USB_AOTG_IN_EP_NUM - 1; i > USB_AOTG_EP0_IDX; i--) {
  2306. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[i];
  2307. if (ep_ctrl->ep_addr != ep) {
  2308. continue;
  2309. }
  2310. return USB_EP_IDX2ADDR(i, USB_EP_DIR_IN);
  2311. }
  2312. } else {
  2313. for (i = USB_AOTG_OUT_EP_NUM - 1; i > USB_AOTG_EP0_IDX; i--) {
  2314. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[i];
  2315. if (ep_ctrl->ep_addr != ep) {
  2316. continue;
  2317. }
  2318. return i;
  2319. }
  2320. }
  2321. /* never */
  2322. return 0;
  2323. }
  2324. /*
  2325. * Check if the MaxPacketSize of endpoint is valid
  2326. */
  2327. static inline bool aotg_hc_ep_mps_valid(uint16_t ep_mps, enum usb_ep_type ep_type)
  2328. {
  2329. enum usb_device_speed speed = usb_aotg_hc.speed;
  2330. switch (ep_type) {
  2331. case USB_EP_CONTROL:
  2332. /* Arbitrary: speed may be unknown */
  2333. return ep_mps <= USB_MAX_CTRL_MPS;
  2334. case USB_EP_BULK:
  2335. if (speed == USB_SPEED_HIGH) {
  2336. return ep_mps == USB_MAX_HS_BULK_MPS;
  2337. } else if (speed == USB_SPEED_FULL) {
  2338. return ep_mps <= USB_MAX_FS_BULK_MPS;
  2339. }
  2340. break;
  2341. case USB_EP_INTERRUPT:
  2342. if (speed == USB_SPEED_HIGH) {
  2343. return ep_mps <= USB_MAX_HS_INTR_MPS;
  2344. } else if (speed == USB_SPEED_FULL) {
  2345. return ep_mps <= USB_MAX_FS_INTR_MPS;
  2346. }
  2347. break;
  2348. case USB_EP_ISOCHRONOUS:
  2349. if (speed == USB_SPEED_HIGH) {
  2350. return ep_mps <= USB_MAX_HS_ISOC_MPS;
  2351. } else if (speed == USB_SPEED_FULL) {
  2352. return ep_mps <= USB_MAX_FS_ISOC_MPS;
  2353. }
  2354. break;
  2355. }
  2356. return false;
  2357. }
  2358. static inline bool aotg_hc_ep_is_enabled(uint8_t aotg_ep)
  2359. {
  2360. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2361. if (USB_EP_DIR_IS_OUT(aotg_ep) &&
  2362. usb_aotg_hc.out_ep_ctrl[ep_idx].ep_ena) {
  2363. return true;
  2364. }
  2365. if (USB_EP_DIR_IS_IN(aotg_ep) &&
  2366. usb_aotg_hc.in_ep_ctrl[ep_idx].ep_ena) {
  2367. return true;
  2368. }
  2369. return false;
  2370. }
  2371. /*
  2372. * Using single FIFO for every endpoint (except for ep0) by default
  2373. */
  2374. static inline int aotg_hc_ep_alloc_fifo(uint8_t aotg_ep)
  2375. {
  2376. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2377. if (!ep_idx) {
  2378. return 0;
  2379. }
  2380. if (USB_EP_DIR_IS_OUT(aotg_ep)) {
  2381. if (ep_idx >= USB_AOTG_OUT_EP_NUM) {
  2382. return -EINVAL;
  2383. }
  2384. return aotg_hc_epout_alloc_fifo_specific(ep_idx);
  2385. }
  2386. if (ep_idx >= USB_AOTG_IN_EP_NUM) {
  2387. return -EINVAL;
  2388. }
  2389. return aotg_hc_epin_alloc_fifo_specific(ep_idx);
  2390. }
  2391. /*
  2392. * Max Packet Size Limit
  2393. */
  2394. static inline int aotg_hc_set_max_packet(uint8_t aotg_ep, uint16_t ep_mps,
  2395. enum usb_ep_type ep_type)
  2396. {
  2397. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2398. if (!aotg_hc_ep_mps_valid(ep_mps, ep_type)) {
  2399. return -EINVAL;
  2400. }
  2401. if (USB_EP_DIR_IS_OUT(aotg_ep)) {
  2402. usb_write16(OUTxMAXPKT(ep_idx), ep_mps);
  2403. usb_aotg_hc.out_ep_ctrl[ep_idx].mps = ep_mps;
  2404. } else {
  2405. usb_write16(INxMAXPKT(ep_idx), ep_mps);
  2406. usb_aotg_hc.in_ep_ctrl[ep_idx].mps = ep_mps;
  2407. }
  2408. return 0;
  2409. }
  2410. /*
  2411. * Endpoint type
  2412. */
  2413. static inline void aotg_hc_set_ep_type(uint8_t aotg_ep, enum usb_ep_type ep_type)
  2414. {
  2415. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2416. if (USB_EP_DIR_IS_OUT(aotg_ep)) {
  2417. usb_aotg_hc.out_ep_ctrl[ep_idx].ep_type = ep_type;
  2418. } else {
  2419. usb_aotg_hc.in_ep_ctrl[ep_idx].ep_type = ep_type;
  2420. }
  2421. }
  2422. static inline int aotg_hc_ep_set(uint8_t ep, uint16_t ep_mps,
  2423. enum usb_ep_type ep_type)
  2424. {
  2425. uint8_t aotg_ep = find_ep(ep);
  2426. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2427. int ret;
  2428. LOG_DBG("0x%x(0x%x), mps %d, type %d", aotg_ep, ep, ep_mps,
  2429. ep_type);
  2430. /* Set FIFO address */
  2431. ret = aotg_hc_ep_alloc_fifo(aotg_ep);
  2432. if (ret) {
  2433. return ret;
  2434. }
  2435. /* Set Max Packet */
  2436. ret = aotg_hc_set_max_packet(aotg_ep, ep_mps, ep_type);
  2437. if (ret) {
  2438. return ret;
  2439. }
  2440. /* Set EP type */
  2441. aotg_hc_set_ep_type(aotg_ep, ep_type);
  2442. /* No need to set for endpoint 0 */
  2443. if (!ep_idx) {
  2444. usb_write8(EP0MAXPKT, ep_mps);
  2445. return 0;
  2446. }
  2447. if (USB_EP_DIR_IS_OUT(aotg_ep)) {
  2448. usb_write8(HCINxCTRL(ep_idx), ep);
  2449. /* Set endpoint type and FIFO type */
  2450. usb_write8(OUTxCTRL(ep_idx),
  2451. (ep_type << 2) | USB_AOTG_FIFO_SINGLE);
  2452. } else {
  2453. usb_write8(HCOUTxCTRL(ep_idx), USB_EP_ADDR2IDX(ep));
  2454. usb_write8(INxCTRL(ep_idx),
  2455. (ep_type << 2) | USB_AOTG_FIFO_SINGLE);
  2456. }
  2457. return 0;
  2458. }
  2459. int usb_hc_ep_configure(const struct usb_hc_ep_cfg_data * const ep_cfg)
  2460. {
  2461. if (!usb_aotg_hc.attached || ep_map(ep_cfg->ep_addr)) {
  2462. return -EINVAL;
  2463. }
  2464. return aotg_hc_ep_set(ep_cfg->ep_addr, ep_cfg->ep_mps,
  2465. ep_cfg->ep_type);
  2466. }
  2467. int usb_hc_ep_enable(const uint8_t ep)
  2468. {
  2469. uint8_t aotg_ep = find_ep(ep);
  2470. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2471. bool dir_out = USB_EP_DIR_IS_OUT(aotg_ep);
  2472. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2473. return -EINVAL;
  2474. }
  2475. /* NOTE: re-enable is allowed */
  2476. if (dir_out) {
  2477. usb_write8(OUTIRQ, BIT(IRQ_EPxOUT(ep_idx)));
  2478. usb_set_bit8(OUTIEN, ep_idx);
  2479. usb_set_bit8(HCINEPERRIEN, ep_idx);
  2480. usb_aotg_hc.out_ep_ctrl[ep_idx].ep_ena = 1;
  2481. } else {
  2482. usb_write8(INIRQ, BIT(IRQ_EPxIN(ep_idx)));
  2483. usb_set_bit8(INIEN, ep_idx);
  2484. usb_set_bit8(HCOUTEPERRIEN, ep_idx);
  2485. usb_aotg_hc.in_ep_ctrl[ep_idx].ep_ena = 1;
  2486. }
  2487. /* Endpoint 0 */
  2488. if (!ep_idx) {
  2489. return 0;
  2490. }
  2491. if (usb_aotg_ep_reset(aotg_ep, USB_AOTG_EP_RESET)) {
  2492. return -EINVAL;
  2493. }
  2494. /* Activate endpoint and set FIFOCTRL */
  2495. if (dir_out) {
  2496. usb_set_bit8(OUTxCTRL(ep_idx), EPCTRL_VALID);
  2497. usb_write8(FIFOCTRL, ep_idx);
  2498. } else {
  2499. usb_set_bit8(INxCTRL(ep_idx), EPCTRL_VALID);
  2500. usb_write8(FIFOCTRL, ep_idx | BIT(FIFOCTRL_IO_IN));
  2501. }
  2502. usb_aotg_ep_reg_dump(aotg_ep);
  2503. return 0;
  2504. }
  2505. static int aotg_hc_ep_disable(const uint8_t aotg_ep)
  2506. {
  2507. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2508. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2509. bool dir_out = USB_EP_DIR_IS_OUT(aotg_ep);
  2510. struct usb_request *urb;
  2511. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2512. return -EINVAL;
  2513. }
  2514. if (dir_out) {
  2515. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[ep_idx];
  2516. } else {
  2517. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[ep_idx];
  2518. }
  2519. /* Already disabled */
  2520. if (ep_ctrl->ep_ena == 0) {
  2521. return 0;
  2522. }
  2523. ep_ctrl->ep_ena = 0;
  2524. /* Disable EP interrupts */
  2525. if (dir_out) {
  2526. usb_clear_bit8(OUTIEN, ep_idx);
  2527. usb_clear_bit8(HCINEPERRIEN, ep_idx);
  2528. } else {
  2529. usb_clear_bit8(INIEN, ep_idx);
  2530. usb_clear_bit8(HCOUTEPERRIEN, ep_idx);
  2531. }
  2532. /* Endpoint 0 */
  2533. if (!ep_idx) {
  2534. goto done;
  2535. }
  2536. /* De-activate Ep */
  2537. if (dir_out) {
  2538. usb_clear_bit8(OUTxCTRL(ep_idx), EPCTRL_VALID);
  2539. } else {
  2540. usb_clear_bit8(INxCTRL(ep_idx), EPCTRL_VALID);
  2541. }
  2542. done:
  2543. urb = ep_ctrl->urb;
  2544. if (urb) {
  2545. ep_ctrl->urb = NULL;
  2546. urb->status = -ENODEV;
  2547. urb->complete(urb);
  2548. }
  2549. return 0;
  2550. }
  2551. int usb_hc_ep_disable(const uint8_t ep)
  2552. {
  2553. const uint8_t aotg_ep = find_ep(ep);
  2554. return aotg_hc_ep_disable(aotg_ep);
  2555. }
  2556. int usb_hc_ep_flush(const uint8_t ep)
  2557. {
  2558. uint8_t aotg_ep = find_ep(ep);
  2559. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2560. return -EINVAL;
  2561. }
  2562. return usb_aotg_ep_reset(aotg_ep, USB_AOTG_EP_FIFO_RESET);
  2563. }
  2564. int usb_hc_ep_reset(const uint8_t ep)
  2565. {
  2566. uint8_t aotg_ep = find_ep(ep);
  2567. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2568. return -EINVAL;
  2569. }
  2570. return usb_aotg_ep_reset(aotg_ep, USB_AOTG_EP_RESET);
  2571. }
  2572. int usb_hc_set_address(const uint8_t addr)
  2573. {
  2574. if (addr > USB_AOTG_MAX_ADDR) {
  2575. return -EINVAL;
  2576. }
  2577. usb_write8(FNADDR, addr);
  2578. return 0;
  2579. }
  2580. static inline bool bus_reset_done(void)
  2581. {
  2582. /* reset done after sending a SOF packet */
  2583. if ((usb_read8(USBIRQ) & (BIT(USBIRQ_SOF) | BIT(USBIRQ_RESET))) ==
  2584. (BIT(USBIRQ_SOF) | BIT(USBIRQ_RESET))) {
  2585. usb_write8(USBIRQ, usb_read8(USBIRQ));
  2586. return true;
  2587. } else {
  2588. return false;
  2589. }
  2590. }
  2591. /* FIXME: support port status only */
  2592. int usb_hc_root_control(void *buf, int len, struct usb_setup_packet *setup,
  2593. int timeout)
  2594. {
  2595. uint16_t req, value;
  2596. int ret = -EPIPE;
  2597. if (!usb_aotg_hc.attached) {
  2598. return -ENODEV;
  2599. }
  2600. req = (setup->bmRequestType << 8) | setup->bRequest;
  2601. value = sys_le16_to_cpu(setup->wValue);
  2602. switch (req) {
  2603. case GetPortStatus:
  2604. if (bus_reset_done()) {
  2605. aotg_hc_handle_reset();
  2606. }
  2607. *(uint32_t *)buf = sys_cpu_to_le32(usb_aotg_hc.port);
  2608. return 4;
  2609. case SetPortFeature:
  2610. if (value == USB_PORT_FEAT_RESET) {
  2611. usb_aotg_hc.port &= ~(USB_PORT_STAT_ENABLE
  2612. | USB_PORT_STAT_LOW_SPEED
  2613. | USB_PORT_STAT_HIGH_SPEED);
  2614. usb_aotg_hc.port |= USB_PORT_STAT_RESET;
  2615. /* port reset */
  2616. usb_set_bit8(HCPORTCTRL, PORTRST);
  2617. return 0;
  2618. }
  2619. break;
  2620. default:
  2621. break;
  2622. }
  2623. return ret;
  2624. }
  2625. static inline int aotg_hc_control_urb(struct usb_request *urb)
  2626. {
  2627. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2628. if (urb->len == 0) {
  2629. /* setup, in status */
  2630. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX];
  2631. } else if (urb->ep == USB_CONTROL_OUT_EP0) {
  2632. /* setup, out data, in status */
  2633. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX];
  2634. } else {
  2635. /* setup, in data, out status */
  2636. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[USB_AOTG_EP0_IDX];
  2637. }
  2638. if (ep_ctrl->urb != NULL) {
  2639. return -EBUSY;
  2640. }
  2641. ep_ctrl->urb = urb;
  2642. /* SETUP */
  2643. usb_aotg_hc.phase = USB_AOTG_SETUP;
  2644. usb_set_bit8(EP0CS, EP0CS_HCSET);
  2645. ep0_write_fifo((const uint8_t *const)urb->setup_packet,
  2646. sizeof(struct usb_setup_packet));
  2647. return 0;
  2648. }
  2649. static inline void aotg_hc_tx_in_pkt(uint8_t ep_idx, uint16_t num_of_pkt)
  2650. {
  2651. LOG_DBG("ep_idx: %d, num_of_pkt: %d", ep_idx, num_of_pkt);
  2652. /* set the number of IN Packet(s) */
  2653. usb_write8(HCINxCNTL(ep_idx), (uint8_t)num_of_pkt);
  2654. usb_write8(HCINxCNTH(ep_idx), (uint8_t)(num_of_pkt >> 8));
  2655. /* set busy */
  2656. if (!(usb_read8(OUTxCS(ep_idx)) & BIT(EPCS_BUSY))) {
  2657. usb_set_bit8(OUTxCS(ep_idx), EPCS_BUSY);
  2658. }
  2659. /* short packet control */
  2660. /* usb_set_bit8(HCINCTRL, HCINx_SHORT(ep_idx)); */
  2661. /* send IN Token */
  2662. usb_set_bit8(HCINCTRL, HCINx_START(ep_idx));
  2663. }
  2664. int usb_hc_submit_urb(struct usb_request *urb)
  2665. {
  2666. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2667. uint8_t aotg_ep = find_ep(urb->ep);
  2668. unsigned int key;
  2669. uint8_t ep_idx;
  2670. int ret;
  2671. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2672. return -EINVAL;
  2673. }
  2674. /* disconnect may happen */
  2675. key = irq_lock();
  2676. /* Check if ep enabled */
  2677. if (!aotg_hc_ep_is_enabled(aotg_ep)) {
  2678. irq_unlock(key);
  2679. return -ENODEV;
  2680. }
  2681. ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2682. if (!ep_idx) {
  2683. ret = aotg_hc_control_urb(urb);
  2684. irq_unlock(key);
  2685. return ret;
  2686. }
  2687. if (USB_EP_DIR_IS_IN(aotg_ep)) {
  2688. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[ep_idx];
  2689. if (ep_ctrl->urb != NULL) {
  2690. irq_unlock(key);
  2691. return -EBUSY;
  2692. }
  2693. ep_ctrl->urb = urb;
  2694. urb->actual = urb->len > ep_ctrl->mps ? ep_ctrl->mps : urb->len;
  2695. /* set actual before write, actual may not correct if error */
  2696. if (ep_write_fifo(aotg_ep, urb->buf, urb->actual) < 0) {
  2697. ep_ctrl->urb = NULL; /* reset if failed */
  2698. urb->actual = 0;
  2699. irq_unlock(key);
  2700. return -EAGAIN;
  2701. }
  2702. urb->buf += urb->actual;
  2703. } else {
  2704. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[ep_idx];
  2705. if (ep_ctrl->urb != NULL) {
  2706. irq_unlock(key);
  2707. return -EBUSY;
  2708. }
  2709. ep_ctrl->urb = urb;
  2710. aotg_hc_tx_in_pkt(ep_idx, DIV_ROUND_UP(urb->len, ep_ctrl->mps));
  2711. }
  2712. irq_unlock(key);
  2713. return 0;
  2714. }
  2715. int usb_hc_cancel_urb(struct usb_request *urb)
  2716. {
  2717. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2718. uint8_t aotg_ep = find_ep(urb->ep);
  2719. uint8_t ep_idx = USB_EP_ADDR2IDX(aotg_ep);
  2720. bool dir_out = USB_EP_DIR_IS_OUT(aotg_ep);
  2721. if (!usb_aotg_hc.attached || !usb_aotg_ep_addr_valid(aotg_ep)) {
  2722. return -EINVAL;
  2723. }
  2724. if (dir_out) {
  2725. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[ep_idx];
  2726. } else {
  2727. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[ep_idx];
  2728. }
  2729. /* Already disabled */
  2730. if (ep_ctrl->ep_ena == 0) {
  2731. return 0;
  2732. }
  2733. if (!ep_idx) {
  2734. goto done;
  2735. }
  2736. if (dir_out) {
  2737. usb_clear_bit8(HCINCTRL, HCINx_START(ep_idx));
  2738. }
  2739. usb_aotg_ep_reset(aotg_ep, USB_AOTG_EP_FIFO_RESET);
  2740. done:
  2741. ep_ctrl->urb = NULL;
  2742. urb->complete(urb);
  2743. return 0;
  2744. }
  2745. static inline void aotg_hc_handle_otg(void)
  2746. {
  2747. u8_t tmp;
  2748. u8_t state;
  2749. /* Clear OTG IRQ(s) */
  2750. tmp = usb_read8(OTGIEN) & usb_read8(OTGIRQ);
  2751. usb_write8(OTGIRQ, tmp);
  2752. state = usb_read8(OTGSTATE);
  2753. /* If AOTG enters a_host state, enable state change irq(a_host->a_wait_bcon) */
  2754. if (state == OTG_A_HOST) {
  2755. usb_set_bit8(OTGSTATE_IEN, OTGSTATE_A_WAIT_BCON);
  2756. }
  2757. LOG_DBG("State: 0x%x, irq: 0x%x", state, tmp);
  2758. }
  2759. static inline void aotg_hc_handle_otg_state(void)
  2760. {
  2761. u8_t i;
  2762. /* clear interrupt request */
  2763. usb_set_bit8(OTGSTATE_IRQ, OTGSTATE_A_WAIT_BCON);
  2764. if (usb_read8(OTGSTATE) == OTG_A_WAIT_BCON) {
  2765. for (i = 0; i < USB_AOTG_IN_EP_NUM; i++) {
  2766. aotg_hc_ep_disable(USB_EP_IDX2ADDR(i, USB_EP_DIR_IN));
  2767. }
  2768. for (i = 0; i < USB_AOTG_OUT_EP_NUM; i++) {
  2769. aotg_hc_ep_disable(USB_EP_IDX2ADDR(i, USB_EP_DIR_OUT));
  2770. }
  2771. }
  2772. }
  2773. static inline void aotg_hc_handle_reset(void)
  2774. {
  2775. uint8_t usbcs;
  2776. /* Clear USB Reset IRQ */
  2777. usb_write8(USBIRQ, BIT(USBIRQ_RESET));
  2778. usb_aotg_hc.port &= ~USB_PORT_STAT_RESET;
  2779. usb_aotg_hc.port |= USB_PORT_STAT_ENABLE;
  2780. /* reset all ep-in */
  2781. usb_aotg_ep_reset(0x80, USB_AOTG_EP_RESET);
  2782. /* reset all ep-out */
  2783. usb_aotg_ep_reset(0x0, USB_AOTG_EP_RESET);
  2784. usbcs = usb_read8(USBCS);
  2785. if (usbcs & BIT(USBCS_SPEED)) {
  2786. usb_aotg_hc.speed = USB_SPEED_HIGH;
  2787. usb_aotg_hc.port |= USB_PORT_STAT_HIGH_SPEED;
  2788. } else if (usb_read8(USBCS) & BIT(USBCS_LS)) {
  2789. usb_aotg_hc.speed = USB_SPEED_LOW;
  2790. usb_aotg_hc.port |= USB_PORT_STAT_LOW_SPEED;
  2791. } else {
  2792. usb_aotg_hc.speed = USB_SPEED_FULL;
  2793. }
  2794. LOG_DBG("USBCS: 0x%x", usbcs);
  2795. }
  2796. static inline void aotg_hc_handle_sof(void)
  2797. {
  2798. usb_set_bit8(USBIRQ, USBIRQ_SOF);
  2799. }
  2800. static inline void aotg_hc_handle_ep0out(void)
  2801. {
  2802. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2803. struct usb_request *urb;
  2804. uint8_t rx_len, mps;
  2805. /* find urb */
  2806. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[USB_AOTG_EP0_IDX];
  2807. urb = ep_ctrl->urb;
  2808. if (!urb) {
  2809. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX];
  2810. urb = ep_ctrl->urb;
  2811. if (!urb) {
  2812. return;
  2813. }
  2814. }
  2815. ep_ctrl->err_count = 0;
  2816. switch (usb_aotg_hc.phase) {
  2817. case USB_AOTG_IN_DATA:
  2818. mps = usb_aotg_hc.out_ep_ctrl[USB_AOTG_EP0_IDX].mps;
  2819. rx_len = urb->len > mps ? mps : urb->len;
  2820. rx_len = ep0_read_fifo(urb->buf, rx_len);
  2821. urb->buf += rx_len;
  2822. urb->actual += rx_len;
  2823. /* FIXME: handle babble */
  2824. /* short packet or complete */
  2825. if ((rx_len < mps) || (urb->actual == urb->len)) {
  2826. usb_aotg_hc.phase = USB_AOTG_OUT_STATUS;
  2827. usb_clear_bit8(EP0CS, EP0CS_HCSET);
  2828. usb_set_bit8(EP0CS, EP0CS_SETTOGGLE);
  2829. usb_write8(IN0BC, 0);
  2830. } else {
  2831. usb_write8(OUT0BC, 0x0);
  2832. }
  2833. break;
  2834. case USB_AOTG_IN_STATUS:
  2835. usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX].urb = NULL;
  2836. urb->status = 0;
  2837. urb->complete(urb);
  2838. break;
  2839. default:
  2840. break;
  2841. }
  2842. }
  2843. /*
  2844. * Handle EPxOUT data
  2845. */
  2846. static inline void aotg_hc_handle_epout(uint8_t ep_idx)
  2847. {
  2848. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2849. struct usb_request *urb;
  2850. bool done = false;
  2851. uint16_t rx_len;
  2852. uint8_t aotg_ep;
  2853. usb_write8(OUTIRQ, BIT(IRQ_EPxOUT(ep_idx)));
  2854. if (!ep_idx) {
  2855. return aotg_hc_handle_ep0out();
  2856. }
  2857. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[ep_idx];
  2858. urb = ep_ctrl->urb;
  2859. if (!urb) {
  2860. LOG_DBG("ep_idx: %d no urb", ep_idx);
  2861. return;
  2862. }
  2863. ep_ctrl->err_count = 0;
  2864. LOG_DBG("hcepin ep_idx: %d, urb->actual: %d, urb->len: %d",
  2865. ep_idx, urb->actual, urb->len);
  2866. aotg_ep = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_OUT);
  2867. rx_len = usb_read16(OUTxBC(ep_idx));
  2868. ep_read_fifo(aotg_ep, urb->buf + urb->actual, rx_len);
  2869. urb->actual += rx_len;
  2870. if (urb->actual > urb->len) {
  2871. LOG_DBG("0x%x(0x%x) babble", aotg_ep, ep_ctrl->ep_addr);
  2872. urb->status = -EIO;
  2873. done = true;
  2874. } else if (urb->actual == urb->len) {
  2875. urb->status = 0;
  2876. done = true;
  2877. } else if (rx_len < ep_ctrl->mps) {
  2878. LOG_DBG("0x%x(0x%x) short %d", aotg_ep, ep_ctrl->ep_addr,
  2879. rx_len);
  2880. urb->status = 0;
  2881. done = true;
  2882. } else {
  2883. usb_set_bit8(OUTxCS(ep_idx), EPCS_BUSY);
  2884. }
  2885. if (done) {
  2886. ep_ctrl->urb = NULL;
  2887. urb->complete(urb);
  2888. }
  2889. }
  2890. static inline void aotg_hc_handle_ep0in(void)
  2891. {
  2892. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2893. struct usb_request *urb;
  2894. uint8_t tx_len, mps;
  2895. /* find urb */
  2896. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[USB_AOTG_EP0_IDX];
  2897. urb = ep_ctrl->urb;
  2898. if (!urb) {
  2899. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX];
  2900. urb = ep_ctrl->urb;
  2901. if (!urb) {
  2902. return;
  2903. }
  2904. }
  2905. ep_ctrl->err_count = 0;
  2906. switch (usb_aotg_hc.phase) {
  2907. case USB_AOTG_SETUP:
  2908. if (urb->len == 0) {
  2909. usb_aotg_hc.phase = USB_AOTG_IN_STATUS;
  2910. usb_set_bit8(EP0CS, EP0CS_SETTOGGLE);
  2911. usb_write8(OUT0BC, 0x0);
  2912. break;
  2913. }
  2914. if (urb->ep == USB_CONTROL_IN_EP0) {
  2915. usb_aotg_hc.phase = USB_AOTG_IN_DATA;
  2916. /* send IN token */
  2917. usb_write8(OUT0BC, 0x0);
  2918. } else {
  2919. usb_aotg_hc.phase = USB_AOTG_OUT_DATA;
  2920. mps = usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX].mps;
  2921. urb->actual = urb->len > mps ? mps : urb->len;
  2922. usb_clear_bit8(EP0CS, EP0CS_HCSET);
  2923. ep0_write_fifo(urb->buf, urb->actual);
  2924. urb->buf += urb->actual;
  2925. }
  2926. break;
  2927. case USB_AOTG_OUT_DATA:
  2928. if (urb->actual == urb->len) {
  2929. usb_aotg_hc.phase = USB_AOTG_IN_STATUS;
  2930. usb_set_bit8(EP0CS, EP0CS_SETTOGGLE);
  2931. usb_write8(OUT0BC, 0x0);
  2932. } else {
  2933. mps = usb_aotg_hc.in_ep_ctrl[USB_AOTG_EP0_IDX].mps;
  2934. tx_len = urb->len > mps ? mps : urb->len;
  2935. if ((urb->len - urb->actual) > mps) {
  2936. tx_len = mps;
  2937. } else {
  2938. tx_len = urb->len - urb->actual;
  2939. }
  2940. usb_clear_bit8(EP0CS, EP0CS_HCSET);
  2941. ep0_write_fifo(urb->buf, tx_len);
  2942. urb->buf += tx_len;
  2943. urb->actual += tx_len;
  2944. }
  2945. break;
  2946. case USB_AOTG_OUT_STATUS:
  2947. usb_aotg_hc.out_ep_ctrl[USB_AOTG_EP0_IDX].urb = NULL;
  2948. urb->status = 0;
  2949. urb->complete(urb);
  2950. break;
  2951. default:
  2952. break;
  2953. }
  2954. }
  2955. /*
  2956. * Handle EPxIN data
  2957. */
  2958. static inline void aotg_hc_handle_epin(uint8_t ep_idx)
  2959. {
  2960. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2961. struct usb_request *urb;
  2962. uint16_t tx_len;
  2963. uint8_t aotg_ep;
  2964. usb_write8(INIRQ, BIT(IRQ_EPxIN(ep_idx)));
  2965. if (!ep_idx) {
  2966. return aotg_hc_handle_ep0in();
  2967. }
  2968. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[ep_idx];
  2969. urb = ep_ctrl->urb;
  2970. if (!urb) {
  2971. LOG_DBG("ep_idx: %d no urb", ep_idx);
  2972. return;
  2973. }
  2974. ep_ctrl->err_count = 0;
  2975. LOG_DBG("ep_idx: %d, urb->actual: %d, urb->len: %d",
  2976. ep_idx, urb->actual, urb->len);
  2977. if (urb->actual == urb->len) {
  2978. ep_ctrl->urb = NULL;
  2979. urb->status = 0;
  2980. urb->complete(urb);
  2981. return;
  2982. }
  2983. aotg_ep = USB_EP_IDX2ADDR(ep_idx, USB_EP_DIR_IN);
  2984. tx_len = urb->len - urb->actual;
  2985. tx_len = tx_len > ep_ctrl->mps ? ep_ctrl->mps : tx_len;
  2986. ep_write_fifo(aotg_ep, urb->buf, tx_len);
  2987. urb->buf += tx_len;
  2988. urb->actual += tx_len;
  2989. }
  2990. static inline void aotg_hc_handle_epout_err(uint8_t ep_idx)
  2991. {
  2992. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  2993. struct usb_request *urb;
  2994. uint8_t type;
  2995. usb_write8(HCINEPERRIRQ, BIT(HCEPxERRIRQ(ep_idx)));
  2996. ep_ctrl = &usb_aotg_hc.out_ep_ctrl[ep_idx];
  2997. urb = ep_ctrl->urb;
  2998. if (!urb) {
  2999. return;
  3000. }
  3001. type = usb_read8(HCINxERR(ep_idx)) & HCEPERR_TYPE_MASK;
  3002. LOG_DBG("ep_idx: %d, type: 0x%x", ep_idx, type);
  3003. switch (type) {
  3004. default:
  3005. if (++ep_ctrl->err_count < ERR_COUNT_MAX) {
  3006. /* resend or try again */
  3007. usb_set_bit8(HCINxERR(ep_idx), HCEPERR_RESEND);
  3008. } else {
  3009. usb_clear_bit8(HCINCTRL, HCINx_START(ep_idx));
  3010. ep_ctrl->err_count = 0;
  3011. ep_ctrl->urb = NULL;
  3012. urb->status = -EIO;
  3013. urb->complete(urb);
  3014. }
  3015. break;
  3016. /* could it happen? */
  3017. case NO_ERR:
  3018. break;
  3019. /* stall */
  3020. case ERR_STALL:
  3021. usb_clear_bit8(HCINCTRL, HCINx_START(ep_idx));
  3022. ep_ctrl->urb = NULL;
  3023. urb->status = -EPIPE;
  3024. urb->complete(urb);
  3025. break;
  3026. }
  3027. }
  3028. static inline void aotg_hc_handle_epin_err(uint8_t ep_idx)
  3029. {
  3030. struct aotg_hc_ep_ctrl_prv *ep_ctrl;
  3031. struct usb_request *urb;
  3032. uint8_t type;
  3033. usb_write8(HCOUTEPERRIRQ, BIT(HCEPxERRIRQ(ep_idx)));
  3034. ep_ctrl = &usb_aotg_hc.in_ep_ctrl[ep_idx];
  3035. urb = ep_ctrl->urb;
  3036. if (!urb) {
  3037. return;
  3038. }
  3039. type = usb_read8(HCOUTxERR(ep_idx)) & HCEPERR_TYPE_MASK;
  3040. LOG_DBG("ep_idx: %d, type: 0x%x", ep_idx, type);
  3041. switch (usb_read8(HCOUTxERR(ep_idx)) & HCEPERR_TYPE_MASK) {
  3042. default:
  3043. if (++ep_ctrl->err_count < ERR_COUNT_MAX) {
  3044. /* resend or try again */
  3045. usb_set_bit8(HCOUTxERR(ep_idx), HCEPERR_RESEND);
  3046. } else {
  3047. ep_ctrl->err_count = 0;
  3048. ep_ctrl->urb = NULL;
  3049. urb->status = -EIO;
  3050. urb->complete(urb);
  3051. }
  3052. break;
  3053. /* could it happen? */
  3054. case NO_ERR:
  3055. break;
  3056. /* stall */
  3057. case ERR_STALL:
  3058. ep_ctrl->urb = NULL;
  3059. urb->status = -EPIPE;
  3060. urb->complete(urb);
  3061. break;
  3062. }
  3063. }
  3064. static inline void aotg_hc_isr_dispatch(uint8_t vector)
  3065. {
  3066. uint8_t usbeien = usb_read8(USBEIEN) & USBEIEN_MASK;
  3067. /*
  3068. * Make sure external IRQ has been cleared right!
  3069. *
  3070. * TODO: If two or more IRQs are pending simultaneously,
  3071. * EIRQ will be pending immediately after cleared or not?
  3072. */
  3073. while (usb_read8(USBEIRQ) & BIT(USBEIRQ_EXTERN)) {
  3074. usb_write8(USBEIRQ, BIT(USBEIRQ_EXTERN) | usbeien);
  3075. }
  3076. switch (vector) {
  3077. /* OTG */
  3078. case UIV_OTGIRQ:
  3079. aotg_hc_handle_otg();
  3080. break;
  3081. case UIV_OTGSTATE:
  3082. aotg_hc_handle_otg_state();
  3083. break;
  3084. /* USB Reset */
  3085. case UIV_USBRST:
  3086. aotg_hc_handle_reset();
  3087. break;
  3088. case UIV_SOF:
  3089. aotg_hc_handle_sof();
  3090. break;
  3091. /* HCINx */
  3092. case UIV_EP0OUT:
  3093. case UIV_EP1OUT:
  3094. case UIV_EP2OUT:
  3095. case UIV_EP3OUT:
  3096. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  3097. case UIV_EP4OUT:
  3098. case UIV_EP5OUT:
  3099. case UIV_EP6OUT:
  3100. #endif
  3101. aotg_hc_handle_epout(UIV_EPOUT_VEC2ADDR(vector));
  3102. break;
  3103. /* HCOUTx */
  3104. case UIV_EP0IN:
  3105. case UIV_EP1IN:
  3106. case UIV_EP2IN:
  3107. case UIV_EP3IN:
  3108. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  3109. case UIV_EP4IN:
  3110. case UIV_EP5IN:
  3111. case UIV_EP6IN:
  3112. #endif
  3113. aotg_hc_handle_epin(UIV_EPIN_VEC2ADDR(vector));
  3114. break;
  3115. /* HCINxERR */
  3116. case UIV_HCIN0ERR:
  3117. case UIV_HCIN1ERR:
  3118. case UIV_HCIN2ERR:
  3119. case UIV_HCIN3ERR:
  3120. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  3121. case UIV_HCIN4ERR:
  3122. case UIV_HCIN5ERR:
  3123. case UIV_HCIN6ERR:
  3124. #endif
  3125. aotg_hc_handle_epout_err(UIV_HCINERR_VEC2ADDR(vector));
  3126. break;
  3127. /* HCOUTxERR */
  3128. case UIV_HCOUT0ERR:
  3129. case UIV_HCOUT1ERR:
  3130. case UIV_HCOUT2ERR:
  3131. case UIV_HCOUT3ERR:
  3132. #if (CONFIG_USB_AOTG_OTG_VERSION == USB_AOTG_VERSION_LEOPARD)
  3133. case UIV_HCOUT4ERR:
  3134. case UIV_HCOUT5ERR:
  3135. case UIV_HCOUT6ERR:
  3136. #endif
  3137. aotg_hc_handle_epin_err(UIV_HCOUTERR_VEC2ADDR(vector));
  3138. break;
  3139. default:
  3140. break;
  3141. }
  3142. }
  3143. int usb_hc_reset(void)
  3144. {
  3145. int ret;
  3146. ret = usb_aotg_reset();
  3147. /* Clear private data */
  3148. memset(&usb_aotg_hc, 0, sizeof(usb_aotg_hc));
  3149. return ret;
  3150. }
  3151. int usb_hc_enable(void)
  3152. {
  3153. if (usb_aotg_hc.attached) {
  3154. LOG_DBG("already");
  3155. return 0;
  3156. }
  3157. aotg_hc_fifo_enable();
  3158. /* Enable OTG(a_host) interrupt */
  3159. usb_set_bit8(OTGIEN, OTGIEN_LOCSOF);
  3160. /* Enable external interrupt */
  3161. usb_set_bit8(USBEIEN, USBEIEN_EXTERN);
  3162. irq_enable(USB_AOTG_IRQ);
  3163. memset(&usb_aotg_hc, 0, sizeof(usb_aotg_hc));
  3164. usb_aotg_hc.port = USB_PORT_STAT_CONNECTION;
  3165. usb_aotg_hc.attached = 1;
  3166. usb_aotg_reg_dump();
  3167. LOG_DBG("");
  3168. return 0;
  3169. }
  3170. int usb_hc_disable(void)
  3171. {
  3172. if (!usb_aotg_hc.attached) {
  3173. LOG_DBG("already");
  3174. return 0;
  3175. }
  3176. irq_disable(USB_AOTG_IRQ);
  3177. memset(&usb_aotg_hc, 0, sizeof(usb_aotg_hc));
  3178. usb_aotg_disable();
  3179. aotg_hc_fifo_disable();
  3180. LOG_DBG("");
  3181. return 0;
  3182. }
  3183. /* usb ram alloc/free */
  3184. int usb_hc_fifo_control(bool enable)
  3185. {
  3186. if (enable) {
  3187. return aotg_hc_fifo_enable();
  3188. } else {
  3189. return aotg_hc_fifo_disable();
  3190. }
  3191. return 0;
  3192. }
  3193. #endif /* CONFIG_USB_AOTG_HC_ENABLED */
  3194. uint8_t usb_phy_get_id(void)
  3195. {
  3196. return USB_ID_INVALID;
  3197. }
  3198. uint8_t usb_phy_get_vbus(void)
  3199. {
  3200. if(soc_pmu_get_dc5v_status()){
  3201. return USB_VBUS_HIGH;
  3202. }
  3203. return USB_VBUS_LOW;
  3204. }
  3205. bool usb_phy_hc_attached(void)
  3206. {
  3207. /* full-speed or high-speed */
  3208. if ((usb_read8(LINESTATUS) & LINESTATE_MASK) == LINESTATE_DP) {
  3209. return true;
  3210. }
  3211. return false;
  3212. }
  3213. bool usb_phy_hc_connected(void)
  3214. {
  3215. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  3216. if ((usb_read8(USBSTATE) == OTG_A_HOST) && bus_reset_done()) {
  3217. return true;
  3218. }
  3219. #endif
  3220. return false;
  3221. }
  3222. bool usb_phy_hc_disconnected(void)
  3223. {
  3224. if (usb_read8(USBSTATE) != OTG_A_HOST) {
  3225. return true;
  3226. }
  3227. return false;
  3228. }
  3229. bool usb_phy_dc_attached(void)
  3230. {
  3231. return ((usb_read8(DPDMCTRL) & BIT(PLUGIN)) &&
  3232. !(usb_read8(LINESTATUS) & LINESTATE_MASK));
  3233. }
  3234. bool usb_phy_dc_detached(void)
  3235. {
  3236. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  3237. return usb_aotg_dc.attached == 0;
  3238. #else
  3239. return true;
  3240. #endif
  3241. }
  3242. bool usb_phy_dc_connected(void)
  3243. {
  3244. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  3245. return usb_aotg_dc.speed != USB_SPEED_UNKNOWN;
  3246. #else
  3247. return false;
  3248. #endif
  3249. }
  3250. bool usb_phy_dc_disconnected(void)
  3251. {
  3252. return !(usb_read8(DPDMCTRL) & BIT(PLUGIN));
  3253. }
  3254. void usb_phy_dc_disconnect(void)
  3255. {
  3256. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  3257. aotg_dc_disconnect();
  3258. #endif
  3259. }
  3260. int usb_phy_enter_a_idle(void)
  3261. {
  3262. usb_aotg_mode = USB_OTG_HOST;
  3263. usb_aotg_clock_enable();
  3264. usb_aotg_dpdm_init();
  3265. acts_reset_peripheral(RESET_ID_USB2);
  3266. usb_write8(IDVBUSCTRL, IDVBUS_HOST);
  3267. usb_write8(DPDMCTRL, DPDM_HOST);
  3268. return 0;
  3269. }
  3270. int usb_phy_enter_b_idle(void)
  3271. {
  3272. usb_aotg_mode = USB_OTG_DEVICE;
  3273. usb_dc_hardware_init();
  3274. usb_aotg_clock_enable();
  3275. usb_aotg_dpdm_init();
  3276. acts_reset_peripheral(RESET_ID_USB2);
  3277. usb_write8(IDVBUSCTRL, IDVBUS_DEVICE);
  3278. usb_write8(DPDMCTRL, DPDM_DEVICE);
  3279. return 0;
  3280. }
  3281. int usb_phy_enter_a_wait_bcon(void)
  3282. {
  3283. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  3284. usb_aotg_enable();
  3285. usb_write8(IDVBUSCTRL, IDVBUS_HOST);
  3286. usb_write8(DPDMCTRL, DPDM_HOST);
  3287. aotg_hc_phy_init();
  3288. usb_set_bit8(OTGCTRL, OTGCTRL_BUSREQ);
  3289. #endif
  3290. return 0;
  3291. }
  3292. static void usb_aotg_isr_handler(void)
  3293. {
  3294. /* IRQ Vector */
  3295. uint8_t vector = usb_read8(IVECT);
  3296. LOG_DBG("vector: 0x%x", vector);
  3297. if (usb_aotg_mode == USB_OTG_HOST) {
  3298. #ifdef CONFIG_USB_AOTG_HC_ENABLED
  3299. aotg_hc_isr_dispatch(vector);
  3300. #endif
  3301. } else if (usb_aotg_mode == USB_OTG_DEVICE) {
  3302. #ifdef CONFIG_USB_AOTG_DC_ENABLED
  3303. aotg_dc_isr_dispatch(vector);
  3304. #endif
  3305. }
  3306. }
  3307. int usb_phy_reset(void)
  3308. {
  3309. usb_aotg_exit();
  3310. return 0;
  3311. }
  3312. int usb_phy_init(void)
  3313. {
  3314. usb_aotg_exit();
  3315. /* Connect and enable USB interrupt */
  3316. IRQ_CONNECT(USB_AOTG_IRQ, CONFIG_USB_AOTG_OTG_IRQ_PRIO,
  3317. usb_aotg_isr_handler, 0, 0);
  3318. irq_disable(USB_AOTG_IRQ);
  3319. return 0;
  3320. }
  3321. int usb_phy_exit(void)
  3322. {
  3323. usb_aotg_exit();
  3324. usb_aotg_dpdm_exit();
  3325. return 0;
  3326. }
  3327. static int usb_aotg_pre_init(const struct device *dev)
  3328. {
  3329. ARG_UNUSED(dev);
  3330. #ifdef CONFIG_USB_AOTG_UDC_DMA
  3331. usb_aotg_dma_init();
  3332. #endif
  3333. return 0;
  3334. }
  3335. SYS_INIT(usb_aotg_pre_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);