pxa27x_udc.c 66 KB

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