pxa27x_udc.c 66 KB

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