pxa27x_udc.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Handles the Intel 27x USB Device Controller (UDC)
  4. *
  5. * Inspired by original driver by Frank Becker, David Brownell, and others.
  6. * Copyright (C) 2008 Robert Jarzmik
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/delay.h>
  15. #include <linux/list.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/clk.h>
  19. #include <linux/irq.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/prefetch.h>
  24. #include <linux/byteorder/generic.h>
  25. #include <linux/platform_data/pxa2xx_udc.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. #include <linux/usb/phy.h>
  32. #include "pxa27x_udc.h"
  33. /*
  34. * This driver handles the USB Device Controller (UDC) in Intel's PXA 27x
  35. * series processors.
  36. *
  37. * Such controller drivers work with a gadget driver. The gadget driver
  38. * returns descriptors, implements configuration and data protocols used
  39. * by the host to interact with this device, and allocates endpoints to
  40. * the different protocol interfaces. The controller driver virtualizes
  41. * usb hardware so that the gadget drivers will be more portable.
  42. *
  43. * This UDC hardware wants to implement a bit too much USB protocol. The
  44. * biggest issues are: that the endpoints have to be set up before the
  45. * controller can be enabled (minor, and not uncommon); and each endpoint
  46. * can only have one configuration, interface and alternative interface
  47. * number (major, and very unusual). Once set up, these cannot be changed
  48. * without a controller reset.
  49. *
  50. * The workaround is to setup all combinations necessary for the gadgets which
  51. * will work with this driver. This is done in pxa_udc structure, statically.
  52. * See pxa_udc, udc_usb_ep versus pxa_ep, and matching function find_pxa_ep.
  53. * (You could modify this if needed. Some drivers have a "fifo_mode" module
  54. * parameter to facilitate such changes.)
  55. *
  56. * The combinations have been tested with these gadgets :
  57. * - zero gadget
  58. * - file storage gadget
  59. * - ether gadget
  60. *
  61. * The driver doesn't use DMA, only IO access and IRQ callbacks. No use is
  62. * made of UDC's double buffering either. USB "On-The-Go" is not implemented.
  63. *
  64. * All the requests are handled the same way :
  65. * - the drivers tries to handle the request directly to the IO
  66. * - if the IO fifo is not big enough, the remaining is send/received in
  67. * interrupt handling.
  68. */
  69. #define DRIVER_VERSION "2008-04-18"
  70. #define DRIVER_DESC "PXA 27x USB Device Controller driver"
  71. static const char driver_name[] = "pxa27x_udc";
  72. static struct pxa_udc *the_controller;
  73. static void handle_ep(struct pxa_ep *ep);
  74. /*
  75. * Debug filesystem
  76. */
  77. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  78. #include <linux/debugfs.h>
  79. #include <linux/uaccess.h>
  80. #include <linux/seq_file.h>
  81. static int state_dbg_show(struct seq_file *s, void *p)
  82. {
  83. struct pxa_udc *udc = s->private;
  84. u32 tmp;
  85. if (!udc->driver)
  86. return -ENODEV;
  87. /* basic device status */
  88. seq_printf(s, DRIVER_DESC "\n"
  89. "%s version: %s\n"
  90. "Gadget driver: %s\n",
  91. driver_name, DRIVER_VERSION,
  92. udc->driver ? udc->driver->driver.name : "(none)");
  93. tmp = udc_readl(udc, UDCCR);
  94. seq_printf(s,
  95. "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), con=%d,inter=%d,altinter=%d\n",
  96. tmp,
  97. (tmp & UDCCR_OEN) ? " oen":"",
  98. (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
  99. (tmp & UDCCR_AHNP) ? " rem" : "",
  100. (tmp & UDCCR_BHNP) ? " rstir" : "",
  101. (tmp & UDCCR_DWRE) ? " dwre" : "",
  102. (tmp & UDCCR_SMAC) ? " smac" : "",
  103. (tmp & UDCCR_EMCE) ? " emce" : "",
  104. (tmp & UDCCR_UDR) ? " udr" : "",
  105. (tmp & UDCCR_UDA) ? " uda" : "",
  106. (tmp & UDCCR_UDE) ? " ude" : "",
  107. (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
  108. (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
  109. (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S);
  110. /* registers for device and ep0 */
  111. seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n",
  112. udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1));
  113. seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n",
  114. udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1));
  115. seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR));
  116. seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, reconfig=%lu\n",
  117. udc->stats.irqs_reset, udc->stats.irqs_suspend,
  118. udc->stats.irqs_resume, udc->stats.irqs_reconfig);
  119. return 0;
  120. }
  121. DEFINE_SHOW_ATTRIBUTE(state_dbg);
  122. static int queues_dbg_show(struct seq_file *s, void *p)
  123. {
  124. struct pxa_udc *udc = s->private;
  125. struct pxa_ep *ep;
  126. struct pxa27x_request *req;
  127. int i, maxpkt;
  128. if (!udc->driver)
  129. return -ENODEV;
  130. /* dump endpoint queues */
  131. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  132. ep = &udc->pxa_ep[i];
  133. maxpkt = ep->fifo_size;
  134. seq_printf(s, "%-12s max_pkt=%d %s\n",
  135. EPNAME(ep), maxpkt, "pio");
  136. if (list_empty(&ep->queue)) {
  137. seq_puts(s, "\t(nothing queued)\n");
  138. continue;
  139. }
  140. list_for_each_entry(req, &ep->queue, queue) {
  141. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  142. &req->req, req->req.actual,
  143. req->req.length, req->req.buf);
  144. }
  145. }
  146. return 0;
  147. }
  148. DEFINE_SHOW_ATTRIBUTE(queues_dbg);
  149. static int eps_dbg_show(struct seq_file *s, void *p)
  150. {
  151. struct pxa_udc *udc = s->private;
  152. struct pxa_ep *ep;
  153. int i;
  154. u32 tmp;
  155. if (!udc->driver)
  156. return -ENODEV;
  157. ep = &udc->pxa_ep[0];
  158. tmp = udc_ep_readl(ep, UDCCSR);
  159. seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n",
  160. tmp,
  161. (tmp & UDCCSR0_SA) ? " sa" : "",
  162. (tmp & UDCCSR0_RNE) ? " rne" : "",
  163. (tmp & UDCCSR0_FST) ? " fst" : "",
  164. (tmp & UDCCSR0_SST) ? " sst" : "",
  165. (tmp & UDCCSR0_DME) ? " dme" : "",
  166. (tmp & UDCCSR0_IPR) ? " ipr" : "",
  167. (tmp & UDCCSR0_OPC) ? " opc" : "");
  168. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  169. ep = &udc->pxa_ep[i];
  170. tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR);
  171. seq_printf(s, "%-12s: IN %lu(%lu reqs), OUT %lu(%lu reqs), irqs=%lu, udccr=0x%08x, udccsr=0x%03x, udcbcr=%d\n",
  172. EPNAME(ep),
  173. ep->stats.in_bytes, ep->stats.in_ops,
  174. ep->stats.out_bytes, ep->stats.out_ops,
  175. ep->stats.irqs,
  176. tmp, udc_ep_readl(ep, UDCCSR),
  177. udc_ep_readl(ep, UDCBCR));
  178. }
  179. return 0;
  180. }
  181. DEFINE_SHOW_ATTRIBUTE(eps_dbg);
  182. static void pxa_init_debugfs(struct pxa_udc *udc)
  183. {
  184. struct dentry *root, *state, *queues, *eps;
  185. root = debugfs_create_dir(udc->gadget.name, NULL);
  186. if (IS_ERR(root) || !root)
  187. goto err_root;
  188. state = debugfs_create_file("udcstate", 0400, root, udc,
  189. &state_dbg_fops);
  190. if (!state)
  191. goto err_state;
  192. queues = debugfs_create_file("queues", 0400, root, udc,
  193. &queues_dbg_fops);
  194. if (!queues)
  195. goto err_queues;
  196. eps = debugfs_create_file("epstate", 0400, root, udc,
  197. &eps_dbg_fops);
  198. if (!eps)
  199. goto err_eps;
  200. udc->debugfs_root = root;
  201. udc->debugfs_state = state;
  202. udc->debugfs_queues = queues;
  203. udc->debugfs_eps = eps;
  204. return;
  205. err_eps:
  206. debugfs_remove(eps);
  207. err_queues:
  208. debugfs_remove(queues);
  209. err_state:
  210. debugfs_remove(root);
  211. err_root:
  212. dev_err(udc->dev, "debugfs is not available\n");
  213. }
  214. static void pxa_cleanup_debugfs(struct pxa_udc *udc)
  215. {
  216. debugfs_remove(udc->debugfs_eps);
  217. debugfs_remove(udc->debugfs_queues);
  218. debugfs_remove(udc->debugfs_state);
  219. debugfs_remove(udc->debugfs_root);
  220. udc->debugfs_eps = NULL;
  221. udc->debugfs_queues = NULL;
  222. udc->debugfs_state = NULL;
  223. udc->debugfs_root = NULL;
  224. }
  225. #else
  226. static inline void pxa_init_debugfs(struct pxa_udc *udc)
  227. {
  228. }
  229. static inline void pxa_cleanup_debugfs(struct pxa_udc *udc)
  230. {
  231. }
  232. #endif
  233. /**
  234. * is_match_usb_pxa - check if usb_ep and pxa_ep match
  235. * @udc_usb_ep: usb endpoint
  236. * @ep: pxa endpoint
  237. * @config: configuration required in pxa_ep
  238. * @interface: interface required in pxa_ep
  239. * @altsetting: altsetting required in pxa_ep
  240. *
  241. * Returns 1 if all criteria match between pxa and usb endpoint, 0 otherwise
  242. */
  243. static int is_match_usb_pxa(struct udc_usb_ep *udc_usb_ep, struct pxa_ep *ep,
  244. int config, int interface, int altsetting)
  245. {
  246. if (usb_endpoint_num(&udc_usb_ep->desc) != ep->addr)
  247. return 0;
  248. if (usb_endpoint_dir_in(&udc_usb_ep->desc) != ep->dir_in)
  249. return 0;
  250. if (usb_endpoint_type(&udc_usb_ep->desc) != ep->type)
  251. return 0;
  252. if ((ep->config != config) || (ep->interface != interface)
  253. || (ep->alternate != altsetting))
  254. return 0;
  255. return 1;
  256. }
  257. /**
  258. * find_pxa_ep - find pxa_ep structure matching udc_usb_ep
  259. * @udc: pxa udc
  260. * @udc_usb_ep: udc_usb_ep structure
  261. *
  262. * Match udc_usb_ep and all pxa_ep available, to see if one matches.
  263. * This is necessary because of the strong pxa hardware restriction requiring
  264. * that once pxa endpoints are initialized, their configuration is freezed, and
  265. * no change can be made to their address, direction, or in which configuration,
  266. * interface or altsetting they are active ... which differs from more usual
  267. * models which have endpoints be roughly just addressable fifos, and leave
  268. * configuration events up to gadget drivers (like all control messages).
  269. *
  270. * Note that there is still a blurred point here :
  271. * - we rely on UDCCR register "active interface" and "active altsetting".
  272. * This is a nonsense in regard of USB spec, where multiple interfaces are
  273. * active at the same time.
  274. * - if we knew for sure that the pxa can handle multiple interface at the
  275. * same time, assuming Intel's Developer Guide is wrong, this function
  276. * should be reviewed, and a cache of couples (iface, altsetting) should
  277. * be kept in the pxa_udc structure. In this case this function would match
  278. * against the cache of couples instead of the "last altsetting" set up.
  279. *
  280. * Returns the matched pxa_ep structure or NULL if none found
  281. */
  282. static struct pxa_ep *find_pxa_ep(struct pxa_udc *udc,
  283. struct udc_usb_ep *udc_usb_ep)
  284. {
  285. int i;
  286. struct pxa_ep *ep;
  287. int cfg = udc->config;
  288. int iface = udc->last_interface;
  289. int alt = udc->last_alternate;
  290. if (udc_usb_ep == &udc->udc_usb_ep[0])
  291. return &udc->pxa_ep[0];
  292. for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
  293. ep = &udc->pxa_ep[i];
  294. if (is_match_usb_pxa(udc_usb_ep, ep, cfg, iface, alt))
  295. return ep;
  296. }
  297. return NULL;
  298. }
  299. /**
  300. * update_pxa_ep_matches - update pxa_ep cached values in all udc_usb_ep
  301. * @udc: pxa udc
  302. *
  303. * Context: in_interrupt()
  304. *
  305. * Updates all pxa_ep fields in udc_usb_ep structures, if this field was
  306. * previously set up (and is not NULL). The update is necessary is a
  307. * configuration change or altsetting change was issued by the USB host.
  308. */
  309. static void update_pxa_ep_matches(struct pxa_udc *udc)
  310. {
  311. int i;
  312. struct udc_usb_ep *udc_usb_ep;
  313. for (i = 1; i < NR_USB_ENDPOINTS; i++) {
  314. udc_usb_ep = &udc->udc_usb_ep[i];
  315. if (udc_usb_ep->pxa_ep)
  316. udc_usb_ep->pxa_ep = find_pxa_ep(udc, udc_usb_ep);
  317. }
  318. }
  319. /**
  320. * pio_irq_enable - Enables irq generation for one endpoint
  321. * @ep: udc endpoint
  322. */
  323. static void pio_irq_enable(struct pxa_ep *ep)
  324. {
  325. struct pxa_udc *udc = ep->dev;
  326. int index = EPIDX(ep);
  327. u32 udcicr0 = udc_readl(udc, UDCICR0);
  328. u32 udcicr1 = udc_readl(udc, UDCICR1);
  329. if (index < 16)
  330. udc_writel(udc, UDCICR0, udcicr0 | (3 << (index * 2)));
  331. else
  332. udc_writel(udc, UDCICR1, udcicr1 | (3 << ((index - 16) * 2)));
  333. }
  334. /**
  335. * pio_irq_disable - Disables irq generation for one endpoint
  336. * @ep: udc endpoint
  337. */
  338. static void pio_irq_disable(struct pxa_ep *ep)
  339. {
  340. struct pxa_udc *udc = ep->dev;
  341. int index = EPIDX(ep);
  342. u32 udcicr0 = udc_readl(udc, UDCICR0);
  343. u32 udcicr1 = udc_readl(udc, UDCICR1);
  344. if (index < 16)
  345. udc_writel(udc, UDCICR0, udcicr0 & ~(3 << (index * 2)));
  346. else
  347. udc_writel(udc, UDCICR1, udcicr1 & ~(3 << ((index - 16) * 2)));
  348. }
  349. /**
  350. * udc_set_mask_UDCCR - set bits in UDCCR
  351. * @udc: udc device
  352. * @mask: bits to set in UDCCR
  353. *
  354. * Sets bits in UDCCR, leaving DME and FST bits as they were.
  355. */
  356. static inline void udc_set_mask_UDCCR(struct pxa_udc *udc, int mask)
  357. {
  358. u32 udccr = udc_readl(udc, UDCCR);
  359. udc_writel(udc, UDCCR,
  360. (udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS));
  361. }
  362. /**
  363. * udc_clear_mask_UDCCR - clears bits in UDCCR
  364. * @udc: udc device
  365. * @mask: bit to clear in UDCCR
  366. *
  367. * Clears bits in UDCCR, leaving DME and FST bits as they were.
  368. */
  369. static inline void udc_clear_mask_UDCCR(struct pxa_udc *udc, int mask)
  370. {
  371. u32 udccr = udc_readl(udc, UDCCR);
  372. udc_writel(udc, UDCCR,
  373. (udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS));
  374. }
  375. /**
  376. * ep_write_UDCCSR - set bits in UDCCSR
  377. * @udc: udc device
  378. * @mask: bits to set in UDCCR
  379. *
  380. * Sets bits in UDCCSR (UDCCSR0 and UDCCSR*).
  381. *
  382. * A specific case is applied to ep0 : the ACM bit is always set to 1, for
  383. * SET_INTERFACE and SET_CONFIGURATION.
  384. */
  385. static inline void ep_write_UDCCSR(struct pxa_ep *ep, int mask)
  386. {
  387. if (is_ep0(ep))
  388. mask |= UDCCSR0_ACM;
  389. udc_ep_writel(ep, UDCCSR, mask);
  390. }
  391. /**
  392. * ep_count_bytes_remain - get how many bytes in udc endpoint
  393. * @ep: udc endpoint
  394. *
  395. * Returns number of bytes in OUT fifos. Broken for IN fifos (-EOPNOTSUPP)
  396. */
  397. static int ep_count_bytes_remain(struct pxa_ep *ep)
  398. {
  399. if (ep->dir_in)
  400. return -EOPNOTSUPP;
  401. return udc_ep_readl(ep, UDCBCR) & 0x3ff;
  402. }
  403. /**
  404. * ep_is_empty - checks if ep has byte ready for reading
  405. * @ep: udc endpoint
  406. *
  407. * If endpoint is the control endpoint, checks if there are bytes in the
  408. * control endpoint fifo. If endpoint is a data endpoint, checks if bytes
  409. * are ready for reading on OUT endpoint.
  410. *
  411. * Returns 0 if ep not empty, 1 if ep empty, -EOPNOTSUPP if IN endpoint
  412. */
  413. static int ep_is_empty(struct pxa_ep *ep)
  414. {
  415. int ret;
  416. if (!is_ep0(ep) && ep->dir_in)
  417. return -EOPNOTSUPP;
  418. if (is_ep0(ep))
  419. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR0_RNE);
  420. else
  421. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNE);
  422. return ret;
  423. }
  424. /**
  425. * ep_is_full - checks if ep has place to write bytes
  426. * @ep: udc endpoint
  427. *
  428. * If endpoint is not the control endpoint and is an IN endpoint, checks if
  429. * there is place to write bytes into the endpoint.
  430. *
  431. * Returns 0 if ep not full, 1 if ep full, -EOPNOTSUPP if OUT endpoint
  432. */
  433. static int ep_is_full(struct pxa_ep *ep)
  434. {
  435. if (is_ep0(ep))
  436. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_IPR);
  437. if (!ep->dir_in)
  438. return -EOPNOTSUPP;
  439. return (!(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNF));
  440. }
  441. /**
  442. * epout_has_pkt - checks if OUT endpoint fifo has a packet available
  443. * @ep: pxa endpoint
  444. *
  445. * Returns 1 if a complete packet is available, 0 if not, -EOPNOTSUPP for IN ep.
  446. */
  447. static int epout_has_pkt(struct pxa_ep *ep)
  448. {
  449. if (!is_ep0(ep) && ep->dir_in)
  450. return -EOPNOTSUPP;
  451. if (is_ep0(ep))
  452. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_OPC);
  453. return (udc_ep_readl(ep, UDCCSR) & UDCCSR_PC);
  454. }
  455. /**
  456. * set_ep0state - Set ep0 automata state
  457. * @dev: udc device
  458. * @state: state
  459. */
  460. static void set_ep0state(struct pxa_udc *udc, int state)
  461. {
  462. struct pxa_ep *ep = &udc->pxa_ep[0];
  463. char *old_stname = EP0_STNAME(udc);
  464. udc->ep0state = state;
  465. ep_dbg(ep, "state=%s->%s, udccsr0=0x%03x, udcbcr=%d\n", old_stname,
  466. EP0_STNAME(udc), udc_ep_readl(ep, UDCCSR),
  467. udc_ep_readl(ep, UDCBCR));
  468. }
  469. /**
  470. * ep0_idle - Put control endpoint into idle state
  471. * @dev: udc device
  472. */
  473. static void ep0_idle(struct pxa_udc *dev)
  474. {
  475. set_ep0state(dev, WAIT_FOR_SETUP);
  476. }
  477. /**
  478. * inc_ep_stats_reqs - Update ep stats counts
  479. * @ep: physical endpoint
  480. * @req: usb request
  481. * @is_in: ep direction (USB_DIR_IN or 0)
  482. *
  483. */
  484. static void inc_ep_stats_reqs(struct pxa_ep *ep, int is_in)
  485. {
  486. if (is_in)
  487. ep->stats.in_ops++;
  488. else
  489. ep->stats.out_ops++;
  490. }
  491. /**
  492. * inc_ep_stats_bytes - Update ep stats counts
  493. * @ep: physical endpoint
  494. * @count: bytes transferred on endpoint
  495. * @is_in: ep direction (USB_DIR_IN or 0)
  496. */
  497. static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
  498. {
  499. if (is_in)
  500. ep->stats.in_bytes += count;
  501. else
  502. ep->stats.out_bytes += count;
  503. }
  504. /**
  505. * pxa_ep_setup - Sets up an usb physical endpoint
  506. * @ep: pxa27x physical endpoint
  507. *
  508. * Find the physical pxa27x ep, and setup its UDCCR
  509. */
  510. static void pxa_ep_setup(struct pxa_ep *ep)
  511. {
  512. u32 new_udccr;
  513. new_udccr = ((ep->config << UDCCONR_CN_S) & UDCCONR_CN)
  514. | ((ep->interface << UDCCONR_IN_S) & UDCCONR_IN)
  515. | ((ep->alternate << UDCCONR_AISN_S) & UDCCONR_AISN)
  516. | ((EPADDR(ep) << UDCCONR_EN_S) & UDCCONR_EN)
  517. | ((EPXFERTYPE(ep) << UDCCONR_ET_S) & UDCCONR_ET)
  518. | ((ep->dir_in) ? UDCCONR_ED : 0)
  519. | ((ep->fifo_size << UDCCONR_MPS_S) & UDCCONR_MPS)
  520. | UDCCONR_EE;
  521. udc_ep_writel(ep, UDCCR, new_udccr);
  522. }
  523. /**
  524. * pxa_eps_setup - Sets up all usb physical endpoints
  525. * @dev: udc device
  526. *
  527. * Setup all pxa physical endpoints, except ep0
  528. */
  529. static void pxa_eps_setup(struct pxa_udc *dev)
  530. {
  531. unsigned int i;
  532. dev_dbg(dev->dev, "%s: dev=%p\n", __func__, dev);
  533. for (i = 1; i < NR_PXA_ENDPOINTS; i++)
  534. pxa_ep_setup(&dev->pxa_ep[i]);
  535. }
  536. /**
  537. * pxa_ep_alloc_request - Allocate usb request
  538. * @_ep: usb endpoint
  539. * @gfp_flags:
  540. *
  541. * For the pxa27x, these can just wrap kmalloc/kfree. gadget drivers
  542. * must still pass correctly initialized endpoints, since other controller
  543. * drivers may care about how it's currently set up (dma issues etc).
  544. */
  545. static struct usb_request *
  546. pxa_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  547. {
  548. struct pxa27x_request *req;
  549. req = kzalloc(sizeof *req, gfp_flags);
  550. if (!req)
  551. return NULL;
  552. INIT_LIST_HEAD(&req->queue);
  553. req->in_use = 0;
  554. req->udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  555. return &req->req;
  556. }
  557. /**
  558. * pxa_ep_free_request - Free usb request
  559. * @_ep: usb endpoint
  560. * @_req: usb request
  561. *
  562. * Wrapper around kfree to free _req
  563. */
  564. static void pxa_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  565. {
  566. struct pxa27x_request *req;
  567. req = container_of(_req, struct pxa27x_request, req);
  568. WARN_ON(!list_empty(&req->queue));
  569. kfree(req);
  570. }
  571. /**
  572. * ep_add_request - add a request to the endpoint's queue
  573. * @ep: usb endpoint
  574. * @req: usb request
  575. *
  576. * Context: ep->lock held
  577. *
  578. * Queues the request in the endpoint's queue, and enables the interrupts
  579. * on the endpoint.
  580. */
  581. static void ep_add_request(struct pxa_ep *ep, struct pxa27x_request *req)
  582. {
  583. if (unlikely(!req))
  584. return;
  585. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  586. req->req.length, udc_ep_readl(ep, UDCCSR));
  587. req->in_use = 1;
  588. list_add_tail(&req->queue, &ep->queue);
  589. pio_irq_enable(ep);
  590. }
  591. /**
  592. * ep_del_request - removes a request from the endpoint's queue
  593. * @ep: usb endpoint
  594. * @req: usb request
  595. *
  596. * Context: ep->lock held
  597. *
  598. * Unqueue the request from the endpoint's queue. If there are no more requests
  599. * on the endpoint, and if it's not the control endpoint, interrupts are
  600. * disabled on the endpoint.
  601. */
  602. static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
  603. {
  604. if (unlikely(!req))
  605. return;
  606. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  607. req->req.length, udc_ep_readl(ep, UDCCSR));
  608. list_del_init(&req->queue);
  609. req->in_use = 0;
  610. if (!is_ep0(ep) && list_empty(&ep->queue))
  611. pio_irq_disable(ep);
  612. }
  613. /**
  614. * req_done - Complete an usb request
  615. * @ep: pxa physical endpoint
  616. * @req: pxa request
  617. * @status: usb request status sent to gadget API
  618. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  619. *
  620. * Context: ep->lock held if flags not NULL, else ep->lock released
  621. *
  622. * Retire a pxa27x usb request. Endpoint must be locked.
  623. */
  624. static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
  625. unsigned long *pflags)
  626. {
  627. unsigned long flags;
  628. ep_del_request(ep, req);
  629. if (likely(req->req.status == -EINPROGRESS))
  630. req->req.status = status;
  631. else
  632. status = req->req.status;
  633. if (status && status != -ESHUTDOWN)
  634. ep_dbg(ep, "complete req %p stat %d len %u/%u\n",
  635. &req->req, status,
  636. req->req.actual, req->req.length);
  637. if (pflags)
  638. spin_unlock_irqrestore(&ep->lock, *pflags);
  639. local_irq_save(flags);
  640. usb_gadget_giveback_request(&req->udc_usb_ep->usb_ep, &req->req);
  641. local_irq_restore(flags);
  642. if (pflags)
  643. spin_lock_irqsave(&ep->lock, *pflags);
  644. }
  645. /**
  646. * ep_end_out_req - Ends endpoint OUT request
  647. * @ep: physical endpoint
  648. * @req: pxa request
  649. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  650. *
  651. * Context: ep->lock held or released (see req_done())
  652. *
  653. * Ends endpoint OUT request (completes usb request).
  654. */
  655. static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  656. unsigned long *pflags)
  657. {
  658. inc_ep_stats_reqs(ep, !USB_DIR_IN);
  659. req_done(ep, req, 0, pflags);
  660. }
  661. /**
  662. * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
  663. * @ep: physical endpoint
  664. * @req: pxa request
  665. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  666. *
  667. * Context: ep->lock held or released (see req_done())
  668. *
  669. * Ends control endpoint OUT request (completes usb request), and puts
  670. * control endpoint into idle state
  671. */
  672. static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  673. unsigned long *pflags)
  674. {
  675. set_ep0state(ep->dev, OUT_STATUS_STAGE);
  676. ep_end_out_req(ep, req, pflags);
  677. ep0_idle(ep->dev);
  678. }
  679. /**
  680. * ep_end_in_req - Ends endpoint IN request
  681. * @ep: physical endpoint
  682. * @req: pxa request
  683. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  684. *
  685. * Context: ep->lock held or released (see req_done())
  686. *
  687. * Ends endpoint IN request (completes usb request).
  688. */
  689. static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  690. unsigned long *pflags)
  691. {
  692. inc_ep_stats_reqs(ep, USB_DIR_IN);
  693. req_done(ep, req, 0, pflags);
  694. }
  695. /**
  696. * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
  697. * @ep: physical endpoint
  698. * @req: pxa request
  699. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  700. *
  701. * Context: ep->lock held or released (see req_done())
  702. *
  703. * Ends control endpoint IN request (completes usb request), and puts
  704. * control endpoint into status state
  705. */
  706. static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  707. unsigned long *pflags)
  708. {
  709. set_ep0state(ep->dev, IN_STATUS_STAGE);
  710. ep_end_in_req(ep, req, pflags);
  711. }
  712. /**
  713. * nuke - Dequeue all requests
  714. * @ep: pxa endpoint
  715. * @status: usb request status
  716. *
  717. * Context: ep->lock released
  718. *
  719. * Dequeues all requests on an endpoint. As a side effect, interrupts will be
  720. * disabled on that endpoint (because no more requests).
  721. */
  722. static void nuke(struct pxa_ep *ep, int status)
  723. {
  724. struct pxa27x_request *req;
  725. unsigned long flags;
  726. spin_lock_irqsave(&ep->lock, flags);
  727. while (!list_empty(&ep->queue)) {
  728. req = list_entry(ep->queue.next, struct pxa27x_request, queue);
  729. req_done(ep, req, status, &flags);
  730. }
  731. spin_unlock_irqrestore(&ep->lock, flags);
  732. }
  733. /**
  734. * read_packet - transfer 1 packet from an OUT endpoint into request
  735. * @ep: pxa physical endpoint
  736. * @req: usb request
  737. *
  738. * Takes bytes from OUT endpoint and transfers them info the usb request.
  739. * If there is less space in request than bytes received in OUT endpoint,
  740. * bytes are left in the OUT endpoint.
  741. *
  742. * Returns how many bytes were actually transferred
  743. */
  744. static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
  745. {
  746. u32 *buf;
  747. int bytes_ep, bufferspace, count, i;
  748. bytes_ep = ep_count_bytes_remain(ep);
  749. bufferspace = req->req.length - req->req.actual;
  750. buf = (u32 *)(req->req.buf + req->req.actual);
  751. prefetchw(buf);
  752. if (likely(!ep_is_empty(ep)))
  753. count = min(bytes_ep, bufferspace);
  754. else /* zlp */
  755. count = 0;
  756. for (i = count; i > 0; i -= 4)
  757. *buf++ = udc_ep_readl(ep, UDCDR);
  758. req->req.actual += count;
  759. ep_write_UDCCSR(ep, UDCCSR_PC);
  760. return count;
  761. }
  762. /**
  763. * write_packet - transfer 1 packet from request into an IN endpoint
  764. * @ep: pxa physical endpoint
  765. * @req: usb request
  766. * @max: max bytes that fit into endpoint
  767. *
  768. * Takes bytes from usb request, and transfers them into the physical
  769. * endpoint. If there are no bytes to transfer, doesn't write anything
  770. * to physical endpoint.
  771. *
  772. * Returns how many bytes were actually transferred.
  773. */
  774. static int write_packet(struct pxa_ep *ep, struct pxa27x_request *req,
  775. unsigned int max)
  776. {
  777. int length, count, remain, i;
  778. u32 *buf;
  779. u8 *buf_8;
  780. buf = (u32 *)(req->req.buf + req->req.actual);
  781. prefetch(buf);
  782. length = min(req->req.length - req->req.actual, max);
  783. req->req.actual += length;
  784. remain = length & 0x3;
  785. count = length & ~(0x3);
  786. for (i = count; i > 0 ; i -= 4)
  787. udc_ep_writel(ep, UDCDR, *buf++);
  788. buf_8 = (u8 *)buf;
  789. for (i = remain; i > 0; i--)
  790. udc_ep_writeb(ep, UDCDR, *buf_8++);
  791. ep_vdbg(ep, "length=%d+%d, udccsr=0x%03x\n", count, remain,
  792. udc_ep_readl(ep, UDCCSR));
  793. return length;
  794. }
  795. /**
  796. * read_fifo - Transfer packets from OUT endpoint into usb request
  797. * @ep: pxa physical endpoint
  798. * @req: usb request
  799. *
  800. * Context: callable when in_interrupt()
  801. *
  802. * Unload as many packets as possible from the fifo we use for usb OUT
  803. * transfers and put them into the request. Caller should have made sure
  804. * there's at least one packet ready.
  805. * Doesn't complete the request, that's the caller's job
  806. *
  807. * Returns 1 if the request completed, 0 otherwise
  808. */
  809. static int read_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  810. {
  811. int count, is_short, completed = 0;
  812. while (epout_has_pkt(ep)) {
  813. count = read_packet(ep, req);
  814. inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
  815. is_short = (count < ep->fifo_size);
  816. ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
  817. udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
  818. &req->req, req->req.actual, req->req.length);
  819. /* completion */
  820. if (is_short || req->req.actual == req->req.length) {
  821. completed = 1;
  822. break;
  823. }
  824. /* finished that packet. the next one may be waiting... */
  825. }
  826. return completed;
  827. }
  828. /**
  829. * write_fifo - transfer packets from usb request into an IN endpoint
  830. * @ep: pxa physical endpoint
  831. * @req: pxa usb request
  832. *
  833. * Write to an IN endpoint fifo, as many packets as possible.
  834. * irqs will use this to write the rest later.
  835. * caller guarantees at least one packet buffer is ready (or a zlp).
  836. * Doesn't complete the request, that's the caller's job
  837. *
  838. * Returns 1 if request fully transferred, 0 if partial transfer
  839. */
  840. static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  841. {
  842. unsigned max;
  843. int count, is_short, is_last = 0, completed = 0, totcount = 0;
  844. u32 udccsr;
  845. max = ep->fifo_size;
  846. do {
  847. udccsr = udc_ep_readl(ep, UDCCSR);
  848. if (udccsr & UDCCSR_PC) {
  849. ep_vdbg(ep, "Clearing Transmit Complete, udccsr=%x\n",
  850. udccsr);
  851. ep_write_UDCCSR(ep, UDCCSR_PC);
  852. }
  853. if (udccsr & UDCCSR_TRN) {
  854. ep_vdbg(ep, "Clearing Underrun on, udccsr=%x\n",
  855. udccsr);
  856. ep_write_UDCCSR(ep, UDCCSR_TRN);
  857. }
  858. count = write_packet(ep, req, max);
  859. inc_ep_stats_bytes(ep, count, USB_DIR_IN);
  860. totcount += count;
  861. /* last packet is usually short (or a zlp) */
  862. if (unlikely(count < max)) {
  863. is_last = 1;
  864. is_short = 1;
  865. } else {
  866. if (likely(req->req.length > req->req.actual)
  867. || req->req.zero)
  868. is_last = 0;
  869. else
  870. is_last = 1;
  871. /* interrupt/iso maxpacket may not fill the fifo */
  872. is_short = unlikely(max < ep->fifo_size);
  873. }
  874. if (is_short)
  875. ep_write_UDCCSR(ep, UDCCSR_SP);
  876. /* requests complete when all IN data is in the FIFO */
  877. if (is_last) {
  878. completed = 1;
  879. break;
  880. }
  881. } while (!ep_is_full(ep));
  882. ep_dbg(ep, "wrote count:%d bytes%s%s, left:%d req=%p\n",
  883. totcount, is_last ? "/L" : "", is_short ? "/S" : "",
  884. req->req.length - req->req.actual, &req->req);
  885. return completed;
  886. }
  887. /**
  888. * read_ep0_fifo - Transfer packets from control endpoint into usb request
  889. * @ep: control endpoint
  890. * @req: pxa usb request
  891. *
  892. * Special ep0 version of the above read_fifo. Reads as many bytes from control
  893. * endpoint as can be read, and stores them into usb request (limited by request
  894. * maximum length).
  895. *
  896. * Returns 0 if usb request only partially filled, 1 if fully filled
  897. */
  898. static int read_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  899. {
  900. int count, is_short, completed = 0;
  901. while (epout_has_pkt(ep)) {
  902. count = read_packet(ep, req);
  903. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  904. inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
  905. is_short = (count < ep->fifo_size);
  906. ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
  907. udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
  908. &req->req, req->req.actual, req->req.length);
  909. if (is_short || req->req.actual >= req->req.length) {
  910. completed = 1;
  911. break;
  912. }
  913. }
  914. return completed;
  915. }
  916. /**
  917. * write_ep0_fifo - Send a request to control endpoint (ep0 in)
  918. * @ep: control endpoint
  919. * @req: request
  920. *
  921. * Context: callable when in_interrupt()
  922. *
  923. * Sends a request (or a part of the request) to the control endpoint (ep0 in).
  924. * If the request doesn't fit, the remaining part will be sent from irq.
  925. * The request is considered fully written only if either :
  926. * - last write transferred all remaining bytes, but fifo was not fully filled
  927. * - last write was a 0 length write
  928. *
  929. * Returns 1 if request fully written, 0 if request only partially sent
  930. */
  931. static int write_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  932. {
  933. unsigned count;
  934. int is_last, is_short;
  935. count = write_packet(ep, req, EP0_FIFO_SIZE);
  936. inc_ep_stats_bytes(ep, count, USB_DIR_IN);
  937. is_short = (count < EP0_FIFO_SIZE);
  938. is_last = ((count == 0) || (count < EP0_FIFO_SIZE));
  939. /* Sends either a short packet or a 0 length packet */
  940. if (unlikely(is_short))
  941. ep_write_UDCCSR(ep, UDCCSR0_IPR);
  942. ep_dbg(ep, "in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n",
  943. count, is_short ? "/S" : "", is_last ? "/L" : "",
  944. req->req.length - req->req.actual,
  945. &req->req, udc_ep_readl(ep, UDCCSR));
  946. return is_last;
  947. }
  948. /**
  949. * pxa_ep_queue - Queue a request into an IN endpoint
  950. * @_ep: usb endpoint
  951. * @_req: usb request
  952. * @gfp_flags: flags
  953. *
  954. * Context: normally called when !in_interrupt, but callable when in_interrupt()
  955. * in the special case of ep0 setup :
  956. * (irq->handle_ep0_ctrl_req->gadget_setup->pxa_ep_queue)
  957. *
  958. * Returns 0 if succedeed, error otherwise
  959. */
  960. static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  961. gfp_t gfp_flags)
  962. {
  963. struct udc_usb_ep *udc_usb_ep;
  964. struct pxa_ep *ep;
  965. struct pxa27x_request *req;
  966. struct pxa_udc *dev;
  967. unsigned long flags;
  968. int rc = 0;
  969. int is_first_req;
  970. unsigned length;
  971. int recursion_detected;
  972. req = container_of(_req, struct pxa27x_request, req);
  973. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  974. if (unlikely(!_req || !_req->complete || !_req->buf))
  975. return -EINVAL;
  976. if (unlikely(!_ep))
  977. return -EINVAL;
  978. ep = udc_usb_ep->pxa_ep;
  979. if (unlikely(!ep))
  980. return -EINVAL;
  981. dev = ep->dev;
  982. if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  983. ep_dbg(ep, "bogus device state\n");
  984. return -ESHUTDOWN;
  985. }
  986. /* iso is always one packet per request, that's the only way
  987. * we can report per-packet status. that also helps with dma.
  988. */
  989. if (unlikely(EPXFERTYPE_is_ISO(ep)
  990. && req->req.length > ep->fifo_size))
  991. return -EMSGSIZE;
  992. spin_lock_irqsave(&ep->lock, flags);
  993. recursion_detected = ep->in_handle_ep;
  994. is_first_req = list_empty(&ep->queue);
  995. ep_dbg(ep, "queue req %p(first=%s), len %d buf %p\n",
  996. _req, is_first_req ? "yes" : "no",
  997. _req->length, _req->buf);
  998. if (!ep->enabled) {
  999. _req->status = -ESHUTDOWN;
  1000. rc = -ESHUTDOWN;
  1001. goto out_locked;
  1002. }
  1003. if (req->in_use) {
  1004. ep_err(ep, "refusing to queue req %p (already queued)\n", req);
  1005. goto out_locked;
  1006. }
  1007. length = _req->length;
  1008. _req->status = -EINPROGRESS;
  1009. _req->actual = 0;
  1010. ep_add_request(ep, req);
  1011. spin_unlock_irqrestore(&ep->lock, flags);
  1012. if (is_ep0(ep)) {
  1013. switch (dev->ep0state) {
  1014. case WAIT_ACK_SET_CONF_INTERF:
  1015. if (length == 0) {
  1016. ep_end_in_req(ep, req, NULL);
  1017. } else {
  1018. ep_err(ep, "got a request of %d bytes while"
  1019. "in state WAIT_ACK_SET_CONF_INTERF\n",
  1020. length);
  1021. ep_del_request(ep, req);
  1022. rc = -EL2HLT;
  1023. }
  1024. ep0_idle(ep->dev);
  1025. break;
  1026. case IN_DATA_STAGE:
  1027. if (!ep_is_full(ep))
  1028. if (write_ep0_fifo(ep, req))
  1029. ep0_end_in_req(ep, req, NULL);
  1030. break;
  1031. case OUT_DATA_STAGE:
  1032. if ((length == 0) || !epout_has_pkt(ep))
  1033. if (read_ep0_fifo(ep, req))
  1034. ep0_end_out_req(ep, req, NULL);
  1035. break;
  1036. default:
  1037. ep_err(ep, "odd state %s to send me a request\n",
  1038. EP0_STNAME(ep->dev));
  1039. ep_del_request(ep, req);
  1040. rc = -EL2HLT;
  1041. break;
  1042. }
  1043. } else {
  1044. if (!recursion_detected)
  1045. handle_ep(ep);
  1046. }
  1047. out:
  1048. return rc;
  1049. out_locked:
  1050. spin_unlock_irqrestore(&ep->lock, flags);
  1051. goto out;
  1052. }
  1053. /**
  1054. * pxa_ep_dequeue - Dequeue one request
  1055. * @_ep: usb endpoint
  1056. * @_req: usb request
  1057. *
  1058. * Return 0 if no error, -EINVAL or -ECONNRESET otherwise
  1059. */
  1060. static int pxa_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1061. {
  1062. struct pxa_ep *ep;
  1063. struct udc_usb_ep *udc_usb_ep;
  1064. struct pxa27x_request *req;
  1065. unsigned long flags;
  1066. int rc = -EINVAL;
  1067. if (!_ep)
  1068. return rc;
  1069. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1070. ep = udc_usb_ep->pxa_ep;
  1071. if (!ep || is_ep0(ep))
  1072. return rc;
  1073. spin_lock_irqsave(&ep->lock, flags);
  1074. /* make sure it's actually queued on this endpoint */
  1075. list_for_each_entry(req, &ep->queue, queue) {
  1076. if (&req->req == _req) {
  1077. rc = 0;
  1078. break;
  1079. }
  1080. }
  1081. spin_unlock_irqrestore(&ep->lock, flags);
  1082. if (!rc)
  1083. req_done(ep, req, -ECONNRESET, NULL);
  1084. return rc;
  1085. }
  1086. /**
  1087. * pxa_ep_set_halt - Halts operations on one endpoint
  1088. * @_ep: usb endpoint
  1089. * @value:
  1090. *
  1091. * Returns 0 if no error, -EINVAL, -EROFS, -EAGAIN otherwise
  1092. */
  1093. static int pxa_ep_set_halt(struct usb_ep *_ep, int value)
  1094. {
  1095. struct pxa_ep *ep;
  1096. struct udc_usb_ep *udc_usb_ep;
  1097. unsigned long flags;
  1098. int rc;
  1099. if (!_ep)
  1100. return -EINVAL;
  1101. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1102. ep = udc_usb_ep->pxa_ep;
  1103. if (!ep || is_ep0(ep))
  1104. return -EINVAL;
  1105. if (value == 0) {
  1106. /*
  1107. * This path (reset toggle+halt) is needed to implement
  1108. * SET_INTERFACE on normal hardware. but it can't be
  1109. * done from software on the PXA UDC, and the hardware
  1110. * forgets to do it as part of SET_INTERFACE automagic.
  1111. */
  1112. ep_dbg(ep, "only host can clear halt\n");
  1113. return -EROFS;
  1114. }
  1115. spin_lock_irqsave(&ep->lock, flags);
  1116. rc = -EAGAIN;
  1117. if (ep->dir_in && (ep_is_full(ep) || !list_empty(&ep->queue)))
  1118. goto out;
  1119. /* FST, FEF bits are the same for control and non control endpoints */
  1120. rc = 0;
  1121. ep_write_UDCCSR(ep, UDCCSR_FST | UDCCSR_FEF);
  1122. if (is_ep0(ep))
  1123. set_ep0state(ep->dev, STALL);
  1124. out:
  1125. spin_unlock_irqrestore(&ep->lock, flags);
  1126. return rc;
  1127. }
  1128. /**
  1129. * pxa_ep_fifo_status - Get how many bytes in physical endpoint
  1130. * @_ep: usb endpoint
  1131. *
  1132. * Returns number of bytes in OUT fifos. Broken for IN fifos.
  1133. */
  1134. static int pxa_ep_fifo_status(struct usb_ep *_ep)
  1135. {
  1136. struct pxa_ep *ep;
  1137. struct udc_usb_ep *udc_usb_ep;
  1138. if (!_ep)
  1139. return -ENODEV;
  1140. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1141. ep = udc_usb_ep->pxa_ep;
  1142. if (!ep || is_ep0(ep))
  1143. return -ENODEV;
  1144. if (ep->dir_in)
  1145. return -EOPNOTSUPP;
  1146. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN || ep_is_empty(ep))
  1147. return 0;
  1148. else
  1149. return ep_count_bytes_remain(ep) + 1;
  1150. }
  1151. /**
  1152. * pxa_ep_fifo_flush - Flushes one endpoint
  1153. * @_ep: usb endpoint
  1154. *
  1155. * Discards all data in one endpoint(IN or OUT), except control endpoint.
  1156. */
  1157. static void pxa_ep_fifo_flush(struct usb_ep *_ep)
  1158. {
  1159. struct pxa_ep *ep;
  1160. struct udc_usb_ep *udc_usb_ep;
  1161. unsigned long flags;
  1162. if (!_ep)
  1163. return;
  1164. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1165. ep = udc_usb_ep->pxa_ep;
  1166. if (!ep || is_ep0(ep))
  1167. return;
  1168. spin_lock_irqsave(&ep->lock, flags);
  1169. if (unlikely(!list_empty(&ep->queue)))
  1170. ep_dbg(ep, "called while queue list not empty\n");
  1171. ep_dbg(ep, "called\n");
  1172. /* for OUT, just read and discard the FIFO contents. */
  1173. if (!ep->dir_in) {
  1174. while (!ep_is_empty(ep))
  1175. udc_ep_readl(ep, UDCDR);
  1176. } else {
  1177. /* most IN status is the same, but ISO can't stall */
  1178. ep_write_UDCCSR(ep,
  1179. UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN
  1180. | (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST));
  1181. }
  1182. spin_unlock_irqrestore(&ep->lock, flags);
  1183. }
  1184. /**
  1185. * pxa_ep_enable - Enables usb endpoint
  1186. * @_ep: usb endpoint
  1187. * @desc: usb endpoint descriptor
  1188. *
  1189. * Nothing much to do here, as ep configuration is done once and for all
  1190. * before udc is enabled. After udc enable, no physical endpoint configuration
  1191. * can be changed.
  1192. * Function makes sanity checks and flushes the endpoint.
  1193. */
  1194. static int pxa_ep_enable(struct usb_ep *_ep,
  1195. const struct usb_endpoint_descriptor *desc)
  1196. {
  1197. struct pxa_ep *ep;
  1198. struct udc_usb_ep *udc_usb_ep;
  1199. struct pxa_udc *udc;
  1200. if (!_ep || !desc)
  1201. return -EINVAL;
  1202. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1203. if (udc_usb_ep->pxa_ep) {
  1204. ep = udc_usb_ep->pxa_ep;
  1205. ep_warn(ep, "usb_ep %s already enabled, doing nothing\n",
  1206. _ep->name);
  1207. } else {
  1208. ep = find_pxa_ep(udc_usb_ep->dev, udc_usb_ep);
  1209. }
  1210. if (!ep || is_ep0(ep)) {
  1211. dev_err(udc_usb_ep->dev->dev,
  1212. "unable to match pxa_ep for ep %s\n",
  1213. _ep->name);
  1214. return -EINVAL;
  1215. }
  1216. if ((desc->bDescriptorType != USB_DT_ENDPOINT)
  1217. || (ep->type != usb_endpoint_type(desc))) {
  1218. ep_err(ep, "type mismatch\n");
  1219. return -EINVAL;
  1220. }
  1221. if (ep->fifo_size < usb_endpoint_maxp(desc)) {
  1222. ep_err(ep, "bad maxpacket\n");
  1223. return -ERANGE;
  1224. }
  1225. udc_usb_ep->pxa_ep = ep;
  1226. udc = ep->dev;
  1227. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  1228. ep_err(ep, "bogus device state\n");
  1229. return -ESHUTDOWN;
  1230. }
  1231. ep->enabled = 1;
  1232. /* flush fifo (mostly for OUT buffers) */
  1233. pxa_ep_fifo_flush(_ep);
  1234. ep_dbg(ep, "enabled\n");
  1235. return 0;
  1236. }
  1237. /**
  1238. * pxa_ep_disable - Disable usb endpoint
  1239. * @_ep: usb endpoint
  1240. *
  1241. * Same as for pxa_ep_enable, no physical endpoint configuration can be
  1242. * changed.
  1243. * Function flushes the endpoint and related requests.
  1244. */
  1245. static int pxa_ep_disable(struct usb_ep *_ep)
  1246. {
  1247. struct pxa_ep *ep;
  1248. struct udc_usb_ep *udc_usb_ep;
  1249. if (!_ep)
  1250. return -EINVAL;
  1251. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1252. ep = udc_usb_ep->pxa_ep;
  1253. if (!ep || is_ep0(ep) || !list_empty(&ep->queue))
  1254. return -EINVAL;
  1255. ep->enabled = 0;
  1256. nuke(ep, -ESHUTDOWN);
  1257. pxa_ep_fifo_flush(_ep);
  1258. udc_usb_ep->pxa_ep = NULL;
  1259. ep_dbg(ep, "disabled\n");
  1260. return 0;
  1261. }
  1262. static const struct usb_ep_ops pxa_ep_ops = {
  1263. .enable = pxa_ep_enable,
  1264. .disable = pxa_ep_disable,
  1265. .alloc_request = pxa_ep_alloc_request,
  1266. .free_request = pxa_ep_free_request,
  1267. .queue = pxa_ep_queue,
  1268. .dequeue = pxa_ep_dequeue,
  1269. .set_halt = pxa_ep_set_halt,
  1270. .fifo_status = pxa_ep_fifo_status,
  1271. .fifo_flush = pxa_ep_fifo_flush,
  1272. };
  1273. /**
  1274. * dplus_pullup - Connect or disconnect pullup resistor to D+ pin
  1275. * @udc: udc device
  1276. * @on: 0 if disconnect pullup resistor, 1 otherwise
  1277. * Context: any
  1278. *
  1279. * Handle D+ pullup resistor, make the device visible to the usb bus, and
  1280. * declare it as a full speed usb device
  1281. */
  1282. static void dplus_pullup(struct pxa_udc *udc, int on)
  1283. {
  1284. if (udc->gpiod) {
  1285. gpiod_set_value(udc->gpiod, on);
  1286. } else if (udc->udc_command) {
  1287. if (on)
  1288. udc->udc_command(PXA2XX_UDC_CMD_CONNECT);
  1289. else
  1290. udc->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  1291. }
  1292. udc->pullup_on = on;
  1293. }
  1294. /**
  1295. * pxa_udc_get_frame - Returns usb frame number
  1296. * @_gadget: usb gadget
  1297. */
  1298. static int pxa_udc_get_frame(struct usb_gadget *_gadget)
  1299. {
  1300. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1301. return (udc_readl(udc, UDCFNR) & 0x7ff);
  1302. }
  1303. /**
  1304. * pxa_udc_wakeup - Force udc device out of suspend
  1305. * @_gadget: usb gadget
  1306. *
  1307. * Returns 0 if successful, error code otherwise
  1308. */
  1309. static int pxa_udc_wakeup(struct usb_gadget *_gadget)
  1310. {
  1311. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1312. /* host may not have enabled remote wakeup */
  1313. if ((udc_readl(udc, UDCCR) & UDCCR_DWRE) == 0)
  1314. return -EHOSTUNREACH;
  1315. udc_set_mask_UDCCR(udc, UDCCR_UDR);
  1316. return 0;
  1317. }
  1318. static void udc_enable(struct pxa_udc *udc);
  1319. static void udc_disable(struct pxa_udc *udc);
  1320. /**
  1321. * should_enable_udc - Tells if UDC should be enabled
  1322. * @udc: udc device
  1323. * Context: any
  1324. *
  1325. * The UDC should be enabled if :
  1326. * - the pullup resistor is connected
  1327. * - and a gadget driver is bound
  1328. * - and vbus is sensed (or no vbus sense is available)
  1329. *
  1330. * Returns 1 if UDC should be enabled, 0 otherwise
  1331. */
  1332. static int should_enable_udc(struct pxa_udc *udc)
  1333. {
  1334. int put_on;
  1335. put_on = ((udc->pullup_on) && (udc->driver));
  1336. put_on &= ((udc->vbus_sensed) || (IS_ERR_OR_NULL(udc->transceiver)));
  1337. return put_on;
  1338. }
  1339. /**
  1340. * should_disable_udc - Tells if UDC should be disabled
  1341. * @udc: udc device
  1342. * Context: any
  1343. *
  1344. * The UDC should be disabled if :
  1345. * - the pullup resistor is not connected
  1346. * - or no gadget driver is bound
  1347. * - or no vbus is sensed (when vbus sesing is available)
  1348. *
  1349. * Returns 1 if UDC should be disabled
  1350. */
  1351. static int should_disable_udc(struct pxa_udc *udc)
  1352. {
  1353. int put_off;
  1354. put_off = ((!udc->pullup_on) || (!udc->driver));
  1355. put_off |= ((!udc->vbus_sensed) && (!IS_ERR_OR_NULL(udc->transceiver)));
  1356. return put_off;
  1357. }
  1358. /**
  1359. * pxa_udc_pullup - Offer manual D+ pullup control
  1360. * @_gadget: usb gadget using the control
  1361. * @is_active: 0 if disconnect, else connect D+ pullup resistor
  1362. * Context: !in_interrupt()
  1363. *
  1364. * Returns 0 if OK, -EOPNOTSUPP if udc driver doesn't handle D+ pullup
  1365. */
  1366. static int pxa_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1367. {
  1368. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1369. if (!udc->gpiod && !udc->udc_command)
  1370. return -EOPNOTSUPP;
  1371. dplus_pullup(udc, is_active);
  1372. if (should_enable_udc(udc))
  1373. udc_enable(udc);
  1374. if (should_disable_udc(udc))
  1375. udc_disable(udc);
  1376. return 0;
  1377. }
  1378. /**
  1379. * pxa_udc_vbus_session - Called by external transceiver to enable/disable udc
  1380. * @_gadget: usb gadget
  1381. * @is_active: 0 if should disable the udc, 1 if should enable
  1382. *
  1383. * Enables the udc, and optionnaly activates D+ pullup resistor. Or disables the
  1384. * udc, and deactivates D+ pullup resistor.
  1385. *
  1386. * Returns 0
  1387. */
  1388. static int pxa_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  1389. {
  1390. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1391. udc->vbus_sensed = is_active;
  1392. if (should_enable_udc(udc))
  1393. udc_enable(udc);
  1394. if (should_disable_udc(udc))
  1395. udc_disable(udc);
  1396. return 0;
  1397. }
  1398. /**
  1399. * pxa_udc_vbus_draw - Called by gadget driver after SET_CONFIGURATION completed
  1400. * @_gadget: usb gadget
  1401. * @mA: current drawn
  1402. *
  1403. * Context: !in_interrupt()
  1404. *
  1405. * Called after a configuration was chosen by a USB host, to inform how much
  1406. * current can be drawn by the device from VBus line.
  1407. *
  1408. * Returns 0 or -EOPNOTSUPP if no transceiver is handling the udc
  1409. */
  1410. static int pxa_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1411. {
  1412. struct pxa_udc *udc;
  1413. udc = to_gadget_udc(_gadget);
  1414. if (!IS_ERR_OR_NULL(udc->transceiver))
  1415. return usb_phy_set_power(udc->transceiver, mA);
  1416. return -EOPNOTSUPP;
  1417. }
  1418. /**
  1419. * pxa_udc_phy_event - Called by phy upon VBus event
  1420. * @nb: notifier block
  1421. * @action: phy action, is vbus connect or disconnect
  1422. * @data: the usb_gadget structure in pxa_udc
  1423. *
  1424. * Called by the USB Phy when a cable connect or disconnect is sensed.
  1425. *
  1426. * Returns 0
  1427. */
  1428. static int pxa_udc_phy_event(struct notifier_block *nb, unsigned long action,
  1429. void *data)
  1430. {
  1431. struct usb_gadget *gadget = data;
  1432. switch (action) {
  1433. case USB_EVENT_VBUS:
  1434. usb_gadget_vbus_connect(gadget);
  1435. return NOTIFY_OK;
  1436. case USB_EVENT_NONE:
  1437. usb_gadget_vbus_disconnect(gadget);
  1438. return NOTIFY_OK;
  1439. default:
  1440. return NOTIFY_DONE;
  1441. }
  1442. }
  1443. static struct notifier_block pxa27x_udc_phy = {
  1444. .notifier_call = pxa_udc_phy_event,
  1445. };
  1446. static int pxa27x_udc_start(struct usb_gadget *g,
  1447. struct usb_gadget_driver *driver);
  1448. static int pxa27x_udc_stop(struct usb_gadget *g);
  1449. static const struct usb_gadget_ops pxa_udc_ops = {
  1450. .get_frame = pxa_udc_get_frame,
  1451. .wakeup = pxa_udc_wakeup,
  1452. .pullup = pxa_udc_pullup,
  1453. .vbus_session = pxa_udc_vbus_session,
  1454. .vbus_draw = pxa_udc_vbus_draw,
  1455. .udc_start = pxa27x_udc_start,
  1456. .udc_stop = pxa27x_udc_stop,
  1457. };
  1458. /**
  1459. * udc_disable - disable udc device controller
  1460. * @udc: udc device
  1461. * Context: any
  1462. *
  1463. * Disables the udc device : disables clocks, udc interrupts, control endpoint
  1464. * interrupts.
  1465. */
  1466. static void udc_disable(struct pxa_udc *udc)
  1467. {
  1468. if (!udc->enabled)
  1469. return;
  1470. udc_writel(udc, UDCICR0, 0);
  1471. udc_writel(udc, UDCICR1, 0);
  1472. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1473. ep0_idle(udc);
  1474. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1475. clk_disable(udc->clk);
  1476. udc->enabled = 0;
  1477. }
  1478. /**
  1479. * udc_init_data - Initialize udc device data structures
  1480. * @dev: udc device
  1481. *
  1482. * Initializes gadget endpoint list, endpoints locks. No action is taken
  1483. * on the hardware.
  1484. */
  1485. static void udc_init_data(struct pxa_udc *dev)
  1486. {
  1487. int i;
  1488. struct pxa_ep *ep;
  1489. /* device/ep0 records init */
  1490. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1491. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1492. dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0];
  1493. dev->gadget.quirk_altset_not_supp = 1;
  1494. ep0_idle(dev);
  1495. /* PXA endpoints init */
  1496. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  1497. ep = &dev->pxa_ep[i];
  1498. ep->enabled = is_ep0(ep);
  1499. INIT_LIST_HEAD(&ep->queue);
  1500. spin_lock_init(&ep->lock);
  1501. }
  1502. /* USB endpoints init */
  1503. for (i = 1; i < NR_USB_ENDPOINTS; i++) {
  1504. list_add_tail(&dev->udc_usb_ep[i].usb_ep.ep_list,
  1505. &dev->gadget.ep_list);
  1506. usb_ep_set_maxpacket_limit(&dev->udc_usb_ep[i].usb_ep,
  1507. dev->udc_usb_ep[i].usb_ep.maxpacket);
  1508. }
  1509. }
  1510. /**
  1511. * udc_enable - Enables the udc device
  1512. * @dev: udc device
  1513. *
  1514. * Enables the udc device : enables clocks, udc interrupts, control endpoint
  1515. * interrupts, sets usb as UDC client and setups endpoints.
  1516. */
  1517. static void udc_enable(struct pxa_udc *udc)
  1518. {
  1519. if (udc->enabled)
  1520. return;
  1521. clk_enable(udc->clk);
  1522. udc_writel(udc, UDCICR0, 0);
  1523. udc_writel(udc, UDCICR1, 0);
  1524. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1525. ep0_idle(udc);
  1526. udc->gadget.speed = USB_SPEED_FULL;
  1527. memset(&udc->stats, 0, sizeof(udc->stats));
  1528. pxa_eps_setup(udc);
  1529. udc_set_mask_UDCCR(udc, UDCCR_UDE);
  1530. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_ACM);
  1531. udelay(2);
  1532. if (udc_readl(udc, UDCCR) & UDCCR_EMCE)
  1533. dev_err(udc->dev, "Configuration errors, udc disabled\n");
  1534. /*
  1535. * Caller must be able to sleep in order to cope with startup transients
  1536. */
  1537. msleep(100);
  1538. /* enable suspend/resume and reset irqs */
  1539. udc_writel(udc, UDCICR1,
  1540. UDCICR1_IECC | UDCICR1_IERU
  1541. | UDCICR1_IESU | UDCICR1_IERS);
  1542. /* enable ep0 irqs */
  1543. pio_irq_enable(&udc->pxa_ep[0]);
  1544. udc->enabled = 1;
  1545. }
  1546. /**
  1547. * pxa27x_start - Register gadget driver
  1548. * @driver: gadget driver
  1549. * @bind: bind function
  1550. *
  1551. * When a driver is successfully registered, it will receive control requests
  1552. * including set_configuration(), which enables non-control requests. Then
  1553. * usb traffic follows until a disconnect is reported. Then a host may connect
  1554. * again, or the driver might get unbound.
  1555. *
  1556. * Note that the udc is not automatically enabled. Check function
  1557. * should_enable_udc().
  1558. *
  1559. * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise
  1560. */
  1561. static int pxa27x_udc_start(struct usb_gadget *g,
  1562. struct usb_gadget_driver *driver)
  1563. {
  1564. struct pxa_udc *udc = to_pxa(g);
  1565. int retval;
  1566. /* first hook up the driver ... */
  1567. udc->driver = driver;
  1568. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  1569. retval = otg_set_peripheral(udc->transceiver->otg,
  1570. &udc->gadget);
  1571. if (retval) {
  1572. dev_err(udc->dev, "can't bind to transceiver\n");
  1573. goto fail;
  1574. }
  1575. }
  1576. if (should_enable_udc(udc))
  1577. udc_enable(udc);
  1578. return 0;
  1579. fail:
  1580. udc->driver = NULL;
  1581. return retval;
  1582. }
  1583. /**
  1584. * stop_activity - Stops udc endpoints
  1585. * @udc: udc device
  1586. * @driver: gadget driver
  1587. *
  1588. * Disables all udc endpoints (even control endpoint), report disconnect to
  1589. * the gadget user.
  1590. */
  1591. static void stop_activity(struct pxa_udc *udc)
  1592. {
  1593. int i;
  1594. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1595. for (i = 0; i < NR_USB_ENDPOINTS; i++)
  1596. pxa_ep_disable(&udc->udc_usb_ep[i].usb_ep);
  1597. }
  1598. /**
  1599. * pxa27x_udc_stop - Unregister the gadget driver
  1600. * @driver: gadget driver
  1601. *
  1602. * Returns 0 if no error, -ENODEV, -EINVAL otherwise
  1603. */
  1604. static int pxa27x_udc_stop(struct usb_gadget *g)
  1605. {
  1606. struct pxa_udc *udc = to_pxa(g);
  1607. stop_activity(udc);
  1608. udc_disable(udc);
  1609. udc->driver = NULL;
  1610. if (!IS_ERR_OR_NULL(udc->transceiver))
  1611. return otg_set_peripheral(udc->transceiver->otg, NULL);
  1612. return 0;
  1613. }
  1614. /**
  1615. * handle_ep0_ctrl_req - handle control endpoint control request
  1616. * @udc: udc device
  1617. * @req: control request
  1618. */
  1619. static void handle_ep0_ctrl_req(struct pxa_udc *udc,
  1620. struct pxa27x_request *req)
  1621. {
  1622. struct pxa_ep *ep = &udc->pxa_ep[0];
  1623. union {
  1624. struct usb_ctrlrequest r;
  1625. u32 word[2];
  1626. } u;
  1627. int i;
  1628. int have_extrabytes = 0;
  1629. unsigned long flags;
  1630. nuke(ep, -EPROTO);
  1631. spin_lock_irqsave(&ep->lock, flags);
  1632. /*
  1633. * In the PXA320 manual, in the section about Back-to-Back setup
  1634. * packets, it describes this situation. The solution is to set OPC to
  1635. * get rid of the status packet, and then continue with the setup
  1636. * packet. Generalize to pxa27x CPUs.
  1637. */
  1638. if (epout_has_pkt(ep) && (ep_count_bytes_remain(ep) == 0))
  1639. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  1640. /* read SETUP packet */
  1641. for (i = 0; i < 2; i++) {
  1642. if (unlikely(ep_is_empty(ep)))
  1643. goto stall;
  1644. u.word[i] = udc_ep_readl(ep, UDCDR);
  1645. }
  1646. have_extrabytes = !ep_is_empty(ep);
  1647. while (!ep_is_empty(ep)) {
  1648. i = udc_ep_readl(ep, UDCDR);
  1649. ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i);
  1650. }
  1651. ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1652. u.r.bRequestType, u.r.bRequest,
  1653. le16_to_cpu(u.r.wValue), le16_to_cpu(u.r.wIndex),
  1654. le16_to_cpu(u.r.wLength));
  1655. if (unlikely(have_extrabytes))
  1656. goto stall;
  1657. if (u.r.bRequestType & USB_DIR_IN)
  1658. set_ep0state(udc, IN_DATA_STAGE);
  1659. else
  1660. set_ep0state(udc, OUT_DATA_STAGE);
  1661. /* Tell UDC to enter Data Stage */
  1662. ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
  1663. spin_unlock_irqrestore(&ep->lock, flags);
  1664. i = udc->driver->setup(&udc->gadget, &u.r);
  1665. spin_lock_irqsave(&ep->lock, flags);
  1666. if (i < 0)
  1667. goto stall;
  1668. out:
  1669. spin_unlock_irqrestore(&ep->lock, flags);
  1670. return;
  1671. stall:
  1672. ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
  1673. udc_ep_readl(ep, UDCCSR), i);
  1674. ep_write_UDCCSR(ep, UDCCSR0_FST | UDCCSR0_FTF);
  1675. set_ep0state(udc, STALL);
  1676. goto out;
  1677. }
  1678. /**
  1679. * handle_ep0 - Handle control endpoint data transfers
  1680. * @udc: udc device
  1681. * @fifo_irq: 1 if triggered by fifo service type irq
  1682. * @opc_irq: 1 if triggered by output packet complete type irq
  1683. *
  1684. * Context : when in_interrupt() or with ep->lock held
  1685. *
  1686. * Tries to transfer all pending request data into the endpoint and/or
  1687. * transfer all pending data in the endpoint into usb requests.
  1688. * Handles states of ep0 automata.
  1689. *
  1690. * PXA27x hardware handles several standard usb control requests without
  1691. * driver notification. The requests fully handled by hardware are :
  1692. * SET_ADDRESS, SET_FEATURE, CLEAR_FEATURE, GET_CONFIGURATION, GET_INTERFACE,
  1693. * GET_STATUS
  1694. * The requests handled by hardware, but with irq notification are :
  1695. * SYNCH_FRAME, SET_CONFIGURATION, SET_INTERFACE
  1696. * The remaining standard requests really handled by handle_ep0 are :
  1697. * GET_DESCRIPTOR, SET_DESCRIPTOR, specific requests.
  1698. * Requests standardized outside of USB 2.0 chapter 9 are handled more
  1699. * uniformly, by gadget drivers.
  1700. *
  1701. * The control endpoint state machine is _not_ USB spec compliant, it's even
  1702. * hardly compliant with Intel PXA270 developers guide.
  1703. * The key points which inferred this state machine are :
  1704. * - on every setup token, bit UDCCSR0_SA is raised and held until cleared by
  1705. * software.
  1706. * - on every OUT packet received, UDCCSR0_OPC is raised and held until
  1707. * cleared by software.
  1708. * - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it
  1709. * before reading ep0.
  1710. * This is true only for PXA27x. This is not true anymore for PXA3xx family
  1711. * (check Back-to-Back setup packet in developers guide).
  1712. * - irq can be called on a "packet complete" event (opc_irq=1), while
  1713. * UDCCSR0_OPC is not yet raised (delta can be as big as 100ms
  1714. * from experimentation).
  1715. * - as UDCCSR0_SA can be activated while in irq handling, and clearing
  1716. * UDCCSR0_OPC would flush the setup data, we almost never clear UDCCSR0_OPC
  1717. * => we never actually read the "status stage" packet of an IN data stage
  1718. * => this is not documented in Intel documentation
  1719. * - hardware as no idea of STATUS STAGE, it only handle SETUP STAGE and DATA
  1720. * STAGE. The driver add STATUS STAGE to send last zero length packet in
  1721. * OUT_STATUS_STAGE.
  1722. * - special attention was needed for IN_STATUS_STAGE. If a packet complete
  1723. * event is detected, we terminate the status stage without ackowledging the
  1724. * packet (not to risk to loose a potential SETUP packet)
  1725. */
  1726. static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
  1727. {
  1728. u32 udccsr0;
  1729. struct pxa_ep *ep = &udc->pxa_ep[0];
  1730. struct pxa27x_request *req = NULL;
  1731. int completed = 0;
  1732. if (!list_empty(&ep->queue))
  1733. req = list_entry(ep->queue.next, struct pxa27x_request, queue);
  1734. udccsr0 = udc_ep_readl(ep, UDCCSR);
  1735. ep_dbg(ep, "state=%s, req=%p, udccsr0=0x%03x, udcbcr=%d, irq_msk=%x\n",
  1736. EP0_STNAME(udc), req, udccsr0, udc_ep_readl(ep, UDCBCR),
  1737. (fifo_irq << 1 | opc_irq));
  1738. if (udccsr0 & UDCCSR0_SST) {
  1739. ep_dbg(ep, "clearing stall status\n");
  1740. nuke(ep, -EPIPE);
  1741. ep_write_UDCCSR(ep, UDCCSR0_SST);
  1742. ep0_idle(udc);
  1743. }
  1744. if (udccsr0 & UDCCSR0_SA) {
  1745. nuke(ep, 0);
  1746. set_ep0state(udc, SETUP_STAGE);
  1747. }
  1748. switch (udc->ep0state) {
  1749. case WAIT_FOR_SETUP:
  1750. /*
  1751. * Hardware bug : beware, we cannot clear OPC, since we would
  1752. * miss a potential OPC irq for a setup packet.
  1753. * So, we only do ... nothing, and hope for a next irq with
  1754. * UDCCSR0_SA set.
  1755. */
  1756. break;
  1757. case SETUP_STAGE:
  1758. udccsr0 &= UDCCSR0_CTRL_REQ_MASK;
  1759. if (likely(udccsr0 == UDCCSR0_CTRL_REQ_MASK))
  1760. handle_ep0_ctrl_req(udc, req);
  1761. break;
  1762. case IN_DATA_STAGE: /* GET_DESCRIPTOR */
  1763. if (epout_has_pkt(ep))
  1764. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  1765. if (req && !ep_is_full(ep))
  1766. completed = write_ep0_fifo(ep, req);
  1767. if (completed)
  1768. ep0_end_in_req(ep, req, NULL);
  1769. break;
  1770. case OUT_DATA_STAGE: /* SET_DESCRIPTOR */
  1771. if (epout_has_pkt(ep) && req)
  1772. completed = read_ep0_fifo(ep, req);
  1773. if (completed)
  1774. ep0_end_out_req(ep, req, NULL);
  1775. break;
  1776. case STALL:
  1777. ep_write_UDCCSR(ep, UDCCSR0_FST);
  1778. break;
  1779. case IN_STATUS_STAGE:
  1780. /*
  1781. * Hardware bug : beware, we cannot clear OPC, since we would
  1782. * miss a potential PC irq for a setup packet.
  1783. * So, we only put the ep0 into WAIT_FOR_SETUP state.
  1784. */
  1785. if (opc_irq)
  1786. ep0_idle(udc);
  1787. break;
  1788. case OUT_STATUS_STAGE:
  1789. case WAIT_ACK_SET_CONF_INTERF:
  1790. ep_warn(ep, "should never get in %s state here!!!\n",
  1791. EP0_STNAME(ep->dev));
  1792. ep0_idle(udc);
  1793. break;
  1794. }
  1795. }
  1796. /**
  1797. * handle_ep - Handle endpoint data tranfers
  1798. * @ep: pxa physical endpoint
  1799. *
  1800. * Tries to transfer all pending request data into the endpoint and/or
  1801. * transfer all pending data in the endpoint into usb requests.
  1802. *
  1803. * Is always called when in_interrupt() and with ep->lock released.
  1804. */
  1805. static void handle_ep(struct pxa_ep *ep)
  1806. {
  1807. struct pxa27x_request *req;
  1808. int completed;
  1809. u32 udccsr;
  1810. int is_in = ep->dir_in;
  1811. int loop = 0;
  1812. unsigned long flags;
  1813. spin_lock_irqsave(&ep->lock, flags);
  1814. if (ep->in_handle_ep)
  1815. goto recursion_detected;
  1816. ep->in_handle_ep = 1;
  1817. do {
  1818. completed = 0;
  1819. udccsr = udc_ep_readl(ep, UDCCSR);
  1820. if (likely(!list_empty(&ep->queue)))
  1821. req = list_entry(ep->queue.next,
  1822. struct pxa27x_request, queue);
  1823. else
  1824. req = NULL;
  1825. ep_dbg(ep, "req:%p, udccsr 0x%03x loop=%d\n",
  1826. req, udccsr, loop++);
  1827. if (unlikely(udccsr & (UDCCSR_SST | UDCCSR_TRN)))
  1828. udc_ep_writel(ep, UDCCSR,
  1829. udccsr & (UDCCSR_SST | UDCCSR_TRN));
  1830. if (!req)
  1831. break;
  1832. if (unlikely(is_in)) {
  1833. if (likely(!ep_is_full(ep)))
  1834. completed = write_fifo(ep, req);
  1835. } else {
  1836. if (likely(epout_has_pkt(ep)))
  1837. completed = read_fifo(ep, req);
  1838. }
  1839. if (completed) {
  1840. if (is_in)
  1841. ep_end_in_req(ep, req, &flags);
  1842. else
  1843. ep_end_out_req(ep, req, &flags);
  1844. }
  1845. } while (completed);
  1846. ep->in_handle_ep = 0;
  1847. recursion_detected:
  1848. spin_unlock_irqrestore(&ep->lock, flags);
  1849. }
  1850. /**
  1851. * pxa27x_change_configuration - Handle SET_CONF usb request notification
  1852. * @udc: udc device
  1853. * @config: usb configuration
  1854. *
  1855. * Post the request to upper level.
  1856. * Don't use any pxa specific harware configuration capabilities
  1857. */
  1858. static void pxa27x_change_configuration(struct pxa_udc *udc, int config)
  1859. {
  1860. struct usb_ctrlrequest req ;
  1861. dev_dbg(udc->dev, "config=%d\n", config);
  1862. udc->config = config;
  1863. udc->last_interface = 0;
  1864. udc->last_alternate = 0;
  1865. req.bRequestType = 0;
  1866. req.bRequest = USB_REQ_SET_CONFIGURATION;
  1867. req.wValue = config;
  1868. req.wIndex = 0;
  1869. req.wLength = 0;
  1870. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1871. udc->driver->setup(&udc->gadget, &req);
  1872. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
  1873. }
  1874. /**
  1875. * pxa27x_change_interface - Handle SET_INTERF usb request notification
  1876. * @udc: udc device
  1877. * @iface: interface number
  1878. * @alt: alternate setting number
  1879. *
  1880. * Post the request to upper level.
  1881. * Don't use any pxa specific harware configuration capabilities
  1882. */
  1883. static void pxa27x_change_interface(struct pxa_udc *udc, int iface, int alt)
  1884. {
  1885. struct usb_ctrlrequest req;
  1886. dev_dbg(udc->dev, "interface=%d, alternate setting=%d\n", iface, alt);
  1887. udc->last_interface = iface;
  1888. udc->last_alternate = alt;
  1889. req.bRequestType = USB_RECIP_INTERFACE;
  1890. req.bRequest = USB_REQ_SET_INTERFACE;
  1891. req.wValue = alt;
  1892. req.wIndex = iface;
  1893. req.wLength = 0;
  1894. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1895. udc->driver->setup(&udc->gadget, &req);
  1896. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
  1897. }
  1898. /*
  1899. * irq_handle_data - Handle data transfer
  1900. * @irq: irq IRQ number
  1901. * @udc: dev pxa_udc device structure
  1902. *
  1903. * Called from irq handler, transferts data to or from endpoint to queue
  1904. */
  1905. static void irq_handle_data(int irq, struct pxa_udc *udc)
  1906. {
  1907. int i;
  1908. struct pxa_ep *ep;
  1909. u32 udcisr0 = udc_readl(udc, UDCISR0) & UDCCISR0_EP_MASK;
  1910. u32 udcisr1 = udc_readl(udc, UDCISR1) & UDCCISR1_EP_MASK;
  1911. if (udcisr0 & UDCISR_INT_MASK) {
  1912. udc->pxa_ep[0].stats.irqs++;
  1913. udc_writel(udc, UDCISR0, UDCISR_INT(0, UDCISR_INT_MASK));
  1914. handle_ep0(udc, !!(udcisr0 & UDCICR_FIFOERR),
  1915. !!(udcisr0 & UDCICR_PKTCOMPL));
  1916. }
  1917. udcisr0 >>= 2;
  1918. for (i = 1; udcisr0 != 0 && i < 16; udcisr0 >>= 2, i++) {
  1919. if (!(udcisr0 & UDCISR_INT_MASK))
  1920. continue;
  1921. udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK));
  1922. WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
  1923. if (i < ARRAY_SIZE(udc->pxa_ep)) {
  1924. ep = &udc->pxa_ep[i];
  1925. ep->stats.irqs++;
  1926. handle_ep(ep);
  1927. }
  1928. }
  1929. for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
  1930. udc_writel(udc, UDCISR1, UDCISR_INT(i - 16, UDCISR_INT_MASK));
  1931. if (!(udcisr1 & UDCISR_INT_MASK))
  1932. continue;
  1933. WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
  1934. if (i < ARRAY_SIZE(udc->pxa_ep)) {
  1935. ep = &udc->pxa_ep[i];
  1936. ep->stats.irqs++;
  1937. handle_ep(ep);
  1938. }
  1939. }
  1940. }
  1941. /**
  1942. * irq_udc_suspend - Handle IRQ "UDC Suspend"
  1943. * @udc: udc device
  1944. */
  1945. static void irq_udc_suspend(struct pxa_udc *udc)
  1946. {
  1947. udc_writel(udc, UDCISR1, UDCISR1_IRSU);
  1948. udc->stats.irqs_suspend++;
  1949. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1950. && udc->driver && udc->driver->suspend)
  1951. udc->driver->suspend(&udc->gadget);
  1952. ep0_idle(udc);
  1953. }
  1954. /**
  1955. * irq_udc_resume - Handle IRQ "UDC Resume"
  1956. * @udc: udc device
  1957. */
  1958. static void irq_udc_resume(struct pxa_udc *udc)
  1959. {
  1960. udc_writel(udc, UDCISR1, UDCISR1_IRRU);
  1961. udc->stats.irqs_resume++;
  1962. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1963. && udc->driver && udc->driver->resume)
  1964. udc->driver->resume(&udc->gadget);
  1965. }
  1966. /**
  1967. * irq_udc_reconfig - Handle IRQ "UDC Change Configuration"
  1968. * @udc: udc device
  1969. */
  1970. static void irq_udc_reconfig(struct pxa_udc *udc)
  1971. {
  1972. unsigned config, interface, alternate, config_change;
  1973. u32 udccr = udc_readl(udc, UDCCR);
  1974. udc_writel(udc, UDCISR1, UDCISR1_IRCC);
  1975. udc->stats.irqs_reconfig++;
  1976. config = (udccr & UDCCR_ACN) >> UDCCR_ACN_S;
  1977. config_change = (config != udc->config);
  1978. pxa27x_change_configuration(udc, config);
  1979. interface = (udccr & UDCCR_AIN) >> UDCCR_AIN_S;
  1980. alternate = (udccr & UDCCR_AAISN) >> UDCCR_AAISN_S;
  1981. pxa27x_change_interface(udc, interface, alternate);
  1982. if (config_change)
  1983. update_pxa_ep_matches(udc);
  1984. udc_set_mask_UDCCR(udc, UDCCR_SMAC);
  1985. }
  1986. /**
  1987. * irq_udc_reset - Handle IRQ "UDC Reset"
  1988. * @udc: udc device
  1989. */
  1990. static void irq_udc_reset(struct pxa_udc *udc)
  1991. {
  1992. u32 udccr = udc_readl(udc, UDCCR);
  1993. struct pxa_ep *ep = &udc->pxa_ep[0];
  1994. dev_info(udc->dev, "USB reset\n");
  1995. udc_writel(udc, UDCISR1, UDCISR1_IRRS);
  1996. udc->stats.irqs_reset++;
  1997. if ((udccr & UDCCR_UDA) == 0) {
  1998. dev_dbg(udc->dev, "USB reset start\n");
  1999. stop_activity(udc);
  2000. }
  2001. udc->gadget.speed = USB_SPEED_FULL;
  2002. memset(&udc->stats, 0, sizeof udc->stats);
  2003. nuke(ep, -EPROTO);
  2004. ep_write_UDCCSR(ep, UDCCSR0_FTF | UDCCSR0_OPC);
  2005. ep0_idle(udc);
  2006. }
  2007. /**
  2008. * pxa_udc_irq - Main irq handler
  2009. * @irq: irq number
  2010. * @_dev: udc device
  2011. *
  2012. * Handles all udc interrupts
  2013. */
  2014. static irqreturn_t pxa_udc_irq(int irq, void *_dev)
  2015. {
  2016. struct pxa_udc *udc = _dev;
  2017. u32 udcisr0 = udc_readl(udc, UDCISR0);
  2018. u32 udcisr1 = udc_readl(udc, UDCISR1);
  2019. u32 udccr = udc_readl(udc, UDCCR);
  2020. u32 udcisr1_spec;
  2021. dev_vdbg(udc->dev, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
  2022. "UDCCR:0x%08x\n", udcisr0, udcisr1, udccr);
  2023. udcisr1_spec = udcisr1 & 0xf8000000;
  2024. if (unlikely(udcisr1_spec & UDCISR1_IRSU))
  2025. irq_udc_suspend(udc);
  2026. if (unlikely(udcisr1_spec & UDCISR1_IRRU))
  2027. irq_udc_resume(udc);
  2028. if (unlikely(udcisr1_spec & UDCISR1_IRCC))
  2029. irq_udc_reconfig(udc);
  2030. if (unlikely(udcisr1_spec & UDCISR1_IRRS))
  2031. irq_udc_reset(udc);
  2032. if ((udcisr0 & UDCCISR0_EP_MASK) | (udcisr1 & UDCCISR1_EP_MASK))
  2033. irq_handle_data(irq, udc);
  2034. return IRQ_HANDLED;
  2035. }
  2036. static struct pxa_udc memory = {
  2037. .gadget = {
  2038. .ops = &pxa_udc_ops,
  2039. .ep0 = &memory.udc_usb_ep[0].usb_ep,
  2040. .name = driver_name,
  2041. .dev = {
  2042. .init_name = "gadget",
  2043. },
  2044. },
  2045. .udc_usb_ep = {
  2046. USB_EP_CTRL,
  2047. USB_EP_OUT_BULK(1),
  2048. USB_EP_IN_BULK(2),
  2049. USB_EP_IN_ISO(3),
  2050. USB_EP_OUT_ISO(4),
  2051. USB_EP_IN_INT(5),
  2052. },
  2053. .pxa_ep = {
  2054. PXA_EP_CTRL,
  2055. /* Endpoints for gadget zero */
  2056. PXA_EP_OUT_BULK(1, 1, 3, 0, 0),
  2057. PXA_EP_IN_BULK(2, 2, 3, 0, 0),
  2058. /* Endpoints for ether gadget, file storage gadget */
  2059. PXA_EP_OUT_BULK(3, 1, 1, 0, 0),
  2060. PXA_EP_IN_BULK(4, 2, 1, 0, 0),
  2061. PXA_EP_IN_ISO(5, 3, 1, 0, 0),
  2062. PXA_EP_OUT_ISO(6, 4, 1, 0, 0),
  2063. PXA_EP_IN_INT(7, 5, 1, 0, 0),
  2064. /* Endpoints for RNDIS, serial */
  2065. PXA_EP_OUT_BULK(8, 1, 2, 0, 0),
  2066. PXA_EP_IN_BULK(9, 2, 2, 0, 0),
  2067. PXA_EP_IN_INT(10, 5, 2, 0, 0),
  2068. /*
  2069. * All the following endpoints are only for completion. They
  2070. * won't never work, as multiple interfaces are really broken on
  2071. * the pxa.
  2072. */
  2073. PXA_EP_OUT_BULK(11, 1, 2, 1, 0),
  2074. PXA_EP_IN_BULK(12, 2, 2, 1, 0),
  2075. /* Endpoint for CDC Ether */
  2076. PXA_EP_OUT_BULK(13, 1, 1, 1, 1),
  2077. PXA_EP_IN_BULK(14, 2, 1, 1, 1),
  2078. }
  2079. };
  2080. #if defined(CONFIG_OF)
  2081. static const struct of_device_id udc_pxa_dt_ids[] = {
  2082. { .compatible = "marvell,pxa270-udc" },
  2083. {}
  2084. };
  2085. MODULE_DEVICE_TABLE(of, udc_pxa_dt_ids);
  2086. #endif
  2087. /**
  2088. * pxa_udc_probe - probes the udc device
  2089. * @_dev: platform device
  2090. *
  2091. * Perform basic init : allocates udc clock, creates sysfs files, requests
  2092. * irq.
  2093. */
  2094. static int pxa_udc_probe(struct platform_device *pdev)
  2095. {
  2096. struct resource *regs;
  2097. struct pxa_udc *udc = &memory;
  2098. int retval = 0, gpio;
  2099. struct pxa2xx_udc_mach_info *mach = dev_get_platdata(&pdev->dev);
  2100. unsigned long gpio_flags;
  2101. if (mach) {
  2102. gpio_flags = mach->gpio_pullup_inverted ? GPIOF_ACTIVE_LOW : 0;
  2103. gpio = mach->gpio_pullup;
  2104. if (gpio_is_valid(gpio)) {
  2105. retval = devm_gpio_request_one(&pdev->dev, gpio,
  2106. gpio_flags,
  2107. "USB D+ pullup");
  2108. if (retval)
  2109. return retval;
  2110. udc->gpiod = gpio_to_desc(mach->gpio_pullup);
  2111. }
  2112. udc->udc_command = mach->udc_command;
  2113. } else {
  2114. udc->gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_ASIS);
  2115. }
  2116. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2117. udc->regs = devm_ioremap_resource(&pdev->dev, regs);
  2118. if (IS_ERR(udc->regs))
  2119. return PTR_ERR(udc->regs);
  2120. udc->irq = platform_get_irq(pdev, 0);
  2121. if (udc->irq < 0)
  2122. return udc->irq;
  2123. udc->dev = &pdev->dev;
  2124. if (of_have_populated_dt()) {
  2125. udc->transceiver =
  2126. devm_usb_get_phy_by_phandle(udc->dev, "phys", 0);
  2127. if (IS_ERR(udc->transceiver))
  2128. return PTR_ERR(udc->transceiver);
  2129. } else {
  2130. udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  2131. }
  2132. if (IS_ERR(udc->gpiod)) {
  2133. dev_err(&pdev->dev, "Couldn't find or request D+ gpio : %ld\n",
  2134. PTR_ERR(udc->gpiod));
  2135. return PTR_ERR(udc->gpiod);
  2136. }
  2137. if (udc->gpiod)
  2138. gpiod_direction_output(udc->gpiod, 0);
  2139. udc->clk = devm_clk_get(&pdev->dev, NULL);
  2140. if (IS_ERR(udc->clk))
  2141. return PTR_ERR(udc->clk);
  2142. retval = clk_prepare(udc->clk);
  2143. if (retval)
  2144. return retval;
  2145. udc->vbus_sensed = 0;
  2146. the_controller = udc;
  2147. platform_set_drvdata(pdev, udc);
  2148. udc_init_data(udc);
  2149. /* irq setup after old hardware state is cleaned up */
  2150. retval = devm_request_irq(&pdev->dev, udc->irq, pxa_udc_irq,
  2151. IRQF_SHARED, driver_name, udc);
  2152. if (retval != 0) {
  2153. dev_err(udc->dev, "%s: can't get irq %i, err %d\n",
  2154. driver_name, udc->irq, retval);
  2155. goto err;
  2156. }
  2157. if (!IS_ERR_OR_NULL(udc->transceiver))
  2158. usb_register_notifier(udc->transceiver, &pxa27x_udc_phy);
  2159. retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  2160. if (retval)
  2161. goto err_add_gadget;
  2162. pxa_init_debugfs(udc);
  2163. if (should_enable_udc(udc))
  2164. udc_enable(udc);
  2165. return 0;
  2166. err_add_gadget:
  2167. if (!IS_ERR_OR_NULL(udc->transceiver))
  2168. usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
  2169. err:
  2170. clk_unprepare(udc->clk);
  2171. return retval;
  2172. }
  2173. /**
  2174. * pxa_udc_remove - removes the udc device driver
  2175. * @_dev: platform device
  2176. */
  2177. static int pxa_udc_remove(struct platform_device *_dev)
  2178. {
  2179. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2180. usb_del_gadget_udc(&udc->gadget);
  2181. pxa_cleanup_debugfs(udc);
  2182. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  2183. usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
  2184. usb_put_phy(udc->transceiver);
  2185. }
  2186. udc->transceiver = NULL;
  2187. the_controller = NULL;
  2188. clk_unprepare(udc->clk);
  2189. return 0;
  2190. }
  2191. static void pxa_udc_shutdown(struct platform_device *_dev)
  2192. {
  2193. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2194. if (udc_readl(udc, UDCCR) & UDCCR_UDE)
  2195. udc_disable(udc);
  2196. }
  2197. #ifdef CONFIG_PXA27x
  2198. extern void pxa27x_clear_otgph(void);
  2199. #else
  2200. #define pxa27x_clear_otgph() do {} while (0)
  2201. #endif
  2202. #ifdef CONFIG_PM
  2203. /**
  2204. * pxa_udc_suspend - Suspend udc device
  2205. * @_dev: platform device
  2206. * @state: suspend state
  2207. *
  2208. * Suspends udc : saves configuration registers (UDCCR*), then disables the udc
  2209. * device.
  2210. */
  2211. static int pxa_udc_suspend(struct platform_device *_dev, pm_message_t state)
  2212. {
  2213. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2214. struct pxa_ep *ep;
  2215. ep = &udc->pxa_ep[0];
  2216. udc->udccsr0 = udc_ep_readl(ep, UDCCSR);
  2217. udc_disable(udc);
  2218. udc->pullup_resume = udc->pullup_on;
  2219. dplus_pullup(udc, 0);
  2220. if (udc->driver)
  2221. udc->driver->disconnect(&udc->gadget);
  2222. return 0;
  2223. }
  2224. /**
  2225. * pxa_udc_resume - Resume udc device
  2226. * @_dev: platform device
  2227. *
  2228. * Resumes udc : restores configuration registers (UDCCR*), then enables the udc
  2229. * device.
  2230. */
  2231. static int pxa_udc_resume(struct platform_device *_dev)
  2232. {
  2233. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2234. struct pxa_ep *ep;
  2235. ep = &udc->pxa_ep[0];
  2236. udc_ep_writel(ep, UDCCSR, udc->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME));
  2237. dplus_pullup(udc, udc->pullup_resume);
  2238. if (should_enable_udc(udc))
  2239. udc_enable(udc);
  2240. /*
  2241. * We do not handle OTG yet.
  2242. *
  2243. * OTGPH bit is set when sleep mode is entered.
  2244. * it indicates that OTG pad is retaining its state.
  2245. * Upon exit from sleep mode and before clearing OTGPH,
  2246. * Software must configure the USB OTG pad, UDC, and UHC
  2247. * to the state they were in before entering sleep mode.
  2248. */
  2249. pxa27x_clear_otgph();
  2250. return 0;
  2251. }
  2252. #endif
  2253. /* work with hotplug and coldplug */
  2254. MODULE_ALIAS("platform:pxa27x-udc");
  2255. static struct platform_driver udc_driver = {
  2256. .driver = {
  2257. .name = "pxa27x-udc",
  2258. .of_match_table = of_match_ptr(udc_pxa_dt_ids),
  2259. },
  2260. .probe = pxa_udc_probe,
  2261. .remove = pxa_udc_remove,
  2262. .shutdown = pxa_udc_shutdown,
  2263. #ifdef CONFIG_PM
  2264. .suspend = pxa_udc_suspend,
  2265. .resume = pxa_udc_resume
  2266. #endif
  2267. };
  2268. module_platform_driver(udc_driver);
  2269. MODULE_DESCRIPTION(DRIVER_DESC);
  2270. MODULE_AUTHOR("Robert Jarzmik");
  2271. MODULE_LICENSE("GPL");