at91_udc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  1. /*
  2. * at91_udc -- driver for at91-series USB peripheral controller
  3. *
  4. * Copyright (C) 2004 by Thomas Rathbone
  5. * Copyright (C) 2005 by HP Labs
  6. * Copyright (C) 2005 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #undef VERBOSE_DEBUG
  14. #undef PACKET_TRACE
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <linux/ioport.h>
  20. #include <linux/slab.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/prefetch.h>
  27. #include <linux/clk.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/prefetch.h>
  31. #include <asm/byteorder.h>
  32. #include <mach/hardware.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/system.h>
  36. #include <asm/gpio.h>
  37. #include <mach/board.h>
  38. #include <mach/cpu.h>
  39. #include <mach/at91sam9261_matrix.h>
  40. #include "at91_udc.h"
  41. /*
  42. * This controller is simple and PIO-only. It's used in many AT91-series
  43. * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
  44. * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
  45. *
  46. * This driver expects the board has been wired with two GPIOs suppporting
  47. * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
  48. * testing hasn't covered such cases.)
  49. *
  50. * The pullup is most important (so it's integrated on sam926x parts). It
  51. * provides software control over whether the host enumerates the device.
  52. *
  53. * The VBUS sensing helps during enumeration, and allows both USB clocks
  54. * (and the transceiver) to stay gated off until they're necessary, saving
  55. * power. During USB suspend, the 48 MHz clock is gated off in hardware;
  56. * it may also be gated off by software during some Linux sleep states.
  57. */
  58. #define DRIVER_VERSION "3 May 2006"
  59. static const char driver_name [] = "at91_udc";
  60. static const char ep0name[] = "ep0";
  61. #define VBUS_POLL_TIMEOUT msecs_to_jiffies(1000)
  62. #define at91_udp_read(udc, reg) \
  63. __raw_readl((udc)->udp_baseaddr + (reg))
  64. #define at91_udp_write(udc, reg, val) \
  65. __raw_writel((val), (udc)->udp_baseaddr + (reg))
  66. /*-------------------------------------------------------------------------*/
  67. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  68. #include <linux/seq_file.h>
  69. static const char debug_filename[] = "driver/udc";
  70. #define FOURBITS "%s%s%s%s"
  71. #define EIGHTBITS FOURBITS FOURBITS
  72. static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
  73. {
  74. static char *types[] = {
  75. "control", "out-iso", "out-bulk", "out-int",
  76. "BOGUS", "in-iso", "in-bulk", "in-int"};
  77. u32 csr;
  78. struct at91_request *req;
  79. unsigned long flags;
  80. struct at91_udc *udc = ep->udc;
  81. spin_lock_irqsave(&udc->lock, flags);
  82. csr = __raw_readl(ep->creg);
  83. /* NOTE: not collecting per-endpoint irq statistics... */
  84. seq_printf(s, "\n");
  85. seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
  86. ep->ep.name, ep->ep.maxpacket,
  87. ep->is_in ? "in" : "out",
  88. ep->is_iso ? " iso" : "",
  89. ep->is_pingpong
  90. ? (ep->fifo_bank ? "pong" : "ping")
  91. : "",
  92. ep->stopped ? " stopped" : "");
  93. seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
  94. csr,
  95. (csr & 0x07ff0000) >> 16,
  96. (csr & (1 << 15)) ? "enabled" : "disabled",
  97. (csr & (1 << 11)) ? "DATA1" : "DATA0",
  98. types[(csr & 0x700) >> 8],
  99. /* iff type is control then print current direction */
  100. (!(csr & 0x700))
  101. ? ((csr & (1 << 7)) ? " IN" : " OUT")
  102. : "",
  103. (csr & (1 << 6)) ? " rxdatabk1" : "",
  104. (csr & (1 << 5)) ? " forcestall" : "",
  105. (csr & (1 << 4)) ? " txpktrdy" : "",
  106. (csr & (1 << 3)) ? " stallsent" : "",
  107. (csr & (1 << 2)) ? " rxsetup" : "",
  108. (csr & (1 << 1)) ? " rxdatabk0" : "",
  109. (csr & (1 << 0)) ? " txcomp" : "");
  110. if (list_empty (&ep->queue))
  111. seq_printf(s, "\t(queue empty)\n");
  112. else list_for_each_entry (req, &ep->queue, queue) {
  113. unsigned length = req->req.actual;
  114. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  115. &req->req, length,
  116. req->req.length, req->req.buf);
  117. }
  118. spin_unlock_irqrestore(&udc->lock, flags);
  119. }
  120. static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
  121. {
  122. int i;
  123. seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
  124. (mask & (1 << 13)) ? " wakeup" : "",
  125. (mask & (1 << 12)) ? " endbusres" : "",
  126. (mask & (1 << 11)) ? " sofint" : "",
  127. (mask & (1 << 10)) ? " extrsm" : "",
  128. (mask & (1 << 9)) ? " rxrsm" : "",
  129. (mask & (1 << 8)) ? " rxsusp" : "");
  130. for (i = 0; i < 8; i++) {
  131. if (mask & (1 << i))
  132. seq_printf(s, " ep%d", i);
  133. }
  134. seq_printf(s, "\n");
  135. }
  136. static int proc_udc_show(struct seq_file *s, void *unused)
  137. {
  138. struct at91_udc *udc = s->private;
  139. struct at91_ep *ep;
  140. u32 tmp;
  141. seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
  142. seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
  143. udc->vbus ? "present" : "off",
  144. udc->enabled
  145. ? (udc->vbus ? "active" : "enabled")
  146. : "disabled",
  147. udc->selfpowered ? "self" : "VBUS",
  148. udc->suspended ? ", suspended" : "",
  149. udc->driver ? udc->driver->driver.name : "(none)");
  150. /* don't access registers when interface isn't clocked */
  151. if (!udc->clocked) {
  152. seq_printf(s, "(not clocked)\n");
  153. return 0;
  154. }
  155. tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
  156. seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
  157. (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
  158. (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
  159. (tmp & AT91_UDP_NUM));
  160. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  161. seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
  162. (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
  163. (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
  164. (tmp & AT91_UDP_ESR) ? " esr" : "",
  165. (tmp & AT91_UDP_CONFG) ? " confg" : "",
  166. (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
  167. tmp = at91_udp_read(udc, AT91_UDP_FADDR);
  168. seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
  169. (tmp & AT91_UDP_FEN) ? " fen" : "",
  170. (tmp & AT91_UDP_FADD));
  171. proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
  172. proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
  173. if (udc->enabled && udc->vbus) {
  174. proc_ep_show(s, &udc->ep[0]);
  175. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  176. if (ep->desc)
  177. proc_ep_show(s, ep);
  178. }
  179. }
  180. return 0;
  181. }
  182. static int proc_udc_open(struct inode *inode, struct file *file)
  183. {
  184. return single_open(file, proc_udc_show, PDE(inode)->data);
  185. }
  186. static const struct file_operations proc_ops = {
  187. .owner = THIS_MODULE,
  188. .open = proc_udc_open,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = single_release,
  192. };
  193. static void create_debug_file(struct at91_udc *udc)
  194. {
  195. udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
  196. }
  197. static void remove_debug_file(struct at91_udc *udc)
  198. {
  199. if (udc->pde)
  200. remove_proc_entry(debug_filename, NULL);
  201. }
  202. #else
  203. static inline void create_debug_file(struct at91_udc *udc) {}
  204. static inline void remove_debug_file(struct at91_udc *udc) {}
  205. #endif
  206. /*-------------------------------------------------------------------------*/
  207. static void done(struct at91_ep *ep, struct at91_request *req, int status)
  208. {
  209. unsigned stopped = ep->stopped;
  210. struct at91_udc *udc = ep->udc;
  211. list_del_init(&req->queue);
  212. if (req->req.status == -EINPROGRESS)
  213. req->req.status = status;
  214. else
  215. status = req->req.status;
  216. if (status && status != -ESHUTDOWN)
  217. VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
  218. ep->stopped = 1;
  219. spin_unlock(&udc->lock);
  220. req->req.complete(&ep->ep, &req->req);
  221. spin_lock(&udc->lock);
  222. ep->stopped = stopped;
  223. /* ep0 is always ready; other endpoints need a non-empty queue */
  224. if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
  225. at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
  226. }
  227. /*-------------------------------------------------------------------------*/
  228. /* bits indicating OUT fifo has data ready */
  229. #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
  230. /*
  231. * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
  232. * back most of the value you just read (because of side effects, including
  233. * bits that may change after reading and before writing).
  234. *
  235. * Except when changing a specific bit, always write values which:
  236. * - clear SET_FX bits (setting them could change something)
  237. * - set CLR_FX bits (clearing them could change something)
  238. *
  239. * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
  240. * that shouldn't normally be changed.
  241. *
  242. * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
  243. * implying a need to wait for one write to complete (test relevant bits)
  244. * before starting the next write. This shouldn't be an issue given how
  245. * infrequently we write, except maybe for write-then-read idioms.
  246. */
  247. #define SET_FX (AT91_UDP_TXPKTRDY)
  248. #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
  249. | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
  250. /* pull OUT packet data from the endpoint's fifo */
  251. static int read_fifo (struct at91_ep *ep, struct at91_request *req)
  252. {
  253. u32 __iomem *creg = ep->creg;
  254. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  255. u32 csr;
  256. u8 *buf;
  257. unsigned int count, bufferspace, is_done;
  258. buf = req->req.buf + req->req.actual;
  259. bufferspace = req->req.length - req->req.actual;
  260. /*
  261. * there might be nothing to read if ep_queue() calls us,
  262. * or if we already emptied both pingpong buffers
  263. */
  264. rescan:
  265. csr = __raw_readl(creg);
  266. if ((csr & RX_DATA_READY) == 0)
  267. return 0;
  268. count = (csr & AT91_UDP_RXBYTECNT) >> 16;
  269. if (count > ep->ep.maxpacket)
  270. count = ep->ep.maxpacket;
  271. if (count > bufferspace) {
  272. DBG("%s buffer overflow\n", ep->ep.name);
  273. req->req.status = -EOVERFLOW;
  274. count = bufferspace;
  275. }
  276. __raw_readsb(dreg, buf, count);
  277. /* release and swap pingpong mem bank */
  278. csr |= CLR_FX;
  279. if (ep->is_pingpong) {
  280. if (ep->fifo_bank == 0) {
  281. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  282. ep->fifo_bank = 1;
  283. } else {
  284. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
  285. ep->fifo_bank = 0;
  286. }
  287. } else
  288. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  289. __raw_writel(csr, creg);
  290. req->req.actual += count;
  291. is_done = (count < ep->ep.maxpacket);
  292. if (count == bufferspace)
  293. is_done = 1;
  294. PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
  295. is_done ? " (done)" : "");
  296. /*
  297. * avoid extra trips through IRQ logic for packets already in
  298. * the fifo ... maybe preventing an extra (expensive) OUT-NAK
  299. */
  300. if (is_done)
  301. done(ep, req, 0);
  302. else if (ep->is_pingpong) {
  303. /*
  304. * One dummy read to delay the code because of a HW glitch:
  305. * CSR returns bad RXCOUNT when read too soon after updating
  306. * RX_DATA_BK flags.
  307. */
  308. csr = __raw_readl(creg);
  309. bufferspace -= count;
  310. buf += count;
  311. goto rescan;
  312. }
  313. return is_done;
  314. }
  315. /* load fifo for an IN packet */
  316. static int write_fifo(struct at91_ep *ep, struct at91_request *req)
  317. {
  318. u32 __iomem *creg = ep->creg;
  319. u32 csr = __raw_readl(creg);
  320. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  321. unsigned total, count, is_last;
  322. u8 *buf;
  323. /*
  324. * TODO: allow for writing two packets to the fifo ... that'll
  325. * reduce the amount of IN-NAKing, but probably won't affect
  326. * throughput much. (Unlike preventing OUT-NAKing!)
  327. */
  328. /*
  329. * If ep_queue() calls us, the queue is empty and possibly in
  330. * odd states like TXCOMP not yet cleared (we do it, saving at
  331. * least one IRQ) or the fifo not yet being free. Those aren't
  332. * issues normally (IRQ handler fast path).
  333. */
  334. if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
  335. if (csr & AT91_UDP_TXCOMP) {
  336. csr |= CLR_FX;
  337. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  338. __raw_writel(csr, creg);
  339. csr = __raw_readl(creg);
  340. }
  341. if (csr & AT91_UDP_TXPKTRDY)
  342. return 0;
  343. }
  344. buf = req->req.buf + req->req.actual;
  345. prefetch(buf);
  346. total = req->req.length - req->req.actual;
  347. if (ep->ep.maxpacket < total) {
  348. count = ep->ep.maxpacket;
  349. is_last = 0;
  350. } else {
  351. count = total;
  352. is_last = (count < ep->ep.maxpacket) || !req->req.zero;
  353. }
  354. /*
  355. * Write the packet, maybe it's a ZLP.
  356. *
  357. * NOTE: incrementing req->actual before we receive the ACK means
  358. * gadget driver IN bytecounts can be wrong in fault cases. That's
  359. * fixable with PIO drivers like this one (save "count" here, and
  360. * do the increment later on TX irq), but not for most DMA hardware.
  361. *
  362. * So all gadget drivers must accept that potential error. Some
  363. * hardware supports precise fifo status reporting, letting them
  364. * recover when the actual bytecount matters (e.g. for USB Test
  365. * and Measurement Class devices).
  366. */
  367. __raw_writesb(dreg, buf, count);
  368. csr &= ~SET_FX;
  369. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  370. __raw_writel(csr, creg);
  371. req->req.actual += count;
  372. PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
  373. is_last ? " (done)" : "");
  374. if (is_last)
  375. done(ep, req, 0);
  376. return is_last;
  377. }
  378. static void nuke(struct at91_ep *ep, int status)
  379. {
  380. struct at91_request *req;
  381. /* terminate any request in the queue */
  382. ep->stopped = 1;
  383. if (list_empty(&ep->queue))
  384. return;
  385. VDBG("%s %s\n", __func__, ep->ep.name);
  386. while (!list_empty(&ep->queue)) {
  387. req = list_entry(ep->queue.next, struct at91_request, queue);
  388. done(ep, req, status);
  389. }
  390. }
  391. /*-------------------------------------------------------------------------*/
  392. static int at91_ep_enable(struct usb_ep *_ep,
  393. const struct usb_endpoint_descriptor *desc)
  394. {
  395. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  396. struct at91_udc *udc = ep->udc;
  397. u16 maxpacket;
  398. u32 tmp;
  399. unsigned long flags;
  400. if (!_ep || !ep
  401. || !desc || ep->desc
  402. || _ep->name == ep0name
  403. || desc->bDescriptorType != USB_DT_ENDPOINT
  404. || (maxpacket = usb_endpoint_maxp(desc)) == 0
  405. || maxpacket > ep->maxpacket) {
  406. DBG("bad ep or descriptor\n");
  407. return -EINVAL;
  408. }
  409. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  410. DBG("bogus device state\n");
  411. return -ESHUTDOWN;
  412. }
  413. tmp = usb_endpoint_type(desc);
  414. switch (tmp) {
  415. case USB_ENDPOINT_XFER_CONTROL:
  416. DBG("only one control endpoint\n");
  417. return -EINVAL;
  418. case USB_ENDPOINT_XFER_INT:
  419. if (maxpacket > 64)
  420. goto bogus_max;
  421. break;
  422. case USB_ENDPOINT_XFER_BULK:
  423. switch (maxpacket) {
  424. case 8:
  425. case 16:
  426. case 32:
  427. case 64:
  428. goto ok;
  429. }
  430. bogus_max:
  431. DBG("bogus maxpacket %d\n", maxpacket);
  432. return -EINVAL;
  433. case USB_ENDPOINT_XFER_ISOC:
  434. if (!ep->is_pingpong) {
  435. DBG("iso requires double buffering\n");
  436. return -EINVAL;
  437. }
  438. break;
  439. }
  440. ok:
  441. spin_lock_irqsave(&udc->lock, flags);
  442. /* initialize endpoint to match this descriptor */
  443. ep->is_in = usb_endpoint_dir_in(desc);
  444. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
  445. ep->stopped = 0;
  446. if (ep->is_in)
  447. tmp |= 0x04;
  448. tmp <<= 8;
  449. tmp |= AT91_UDP_EPEDS;
  450. __raw_writel(tmp, ep->creg);
  451. ep->desc = desc;
  452. ep->ep.maxpacket = maxpacket;
  453. /*
  454. * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
  455. * since endpoint resets don't reset hw pingpong state.
  456. */
  457. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  458. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  459. spin_unlock_irqrestore(&udc->lock, flags);
  460. return 0;
  461. }
  462. static int at91_ep_disable (struct usb_ep * _ep)
  463. {
  464. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  465. struct at91_udc *udc = ep->udc;
  466. unsigned long flags;
  467. if (ep == &ep->udc->ep[0])
  468. return -EINVAL;
  469. spin_lock_irqsave(&udc->lock, flags);
  470. nuke(ep, -ESHUTDOWN);
  471. /* restore the endpoint's pristine config */
  472. ep->desc = NULL;
  473. ep->ep.maxpacket = ep->maxpacket;
  474. /* reset fifos and endpoint */
  475. if (ep->udc->clocked) {
  476. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  477. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  478. __raw_writel(0, ep->creg);
  479. }
  480. spin_unlock_irqrestore(&udc->lock, flags);
  481. return 0;
  482. }
  483. /*
  484. * this is a PIO-only driver, so there's nothing
  485. * interesting for request or buffer allocation.
  486. */
  487. static struct usb_request *
  488. at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  489. {
  490. struct at91_request *req;
  491. req = kzalloc(sizeof (struct at91_request), gfp_flags);
  492. if (!req)
  493. return NULL;
  494. INIT_LIST_HEAD(&req->queue);
  495. return &req->req;
  496. }
  497. static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  498. {
  499. struct at91_request *req;
  500. req = container_of(_req, struct at91_request, req);
  501. BUG_ON(!list_empty(&req->queue));
  502. kfree(req);
  503. }
  504. static int at91_ep_queue(struct usb_ep *_ep,
  505. struct usb_request *_req, gfp_t gfp_flags)
  506. {
  507. struct at91_request *req;
  508. struct at91_ep *ep;
  509. struct at91_udc *udc;
  510. int status;
  511. unsigned long flags;
  512. req = container_of(_req, struct at91_request, req);
  513. ep = container_of(_ep, struct at91_ep, ep);
  514. if (!_req || !_req->complete
  515. || !_req->buf || !list_empty(&req->queue)) {
  516. DBG("invalid request\n");
  517. return -EINVAL;
  518. }
  519. if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
  520. DBG("invalid ep\n");
  521. return -EINVAL;
  522. }
  523. udc = ep->udc;
  524. if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  525. DBG("invalid device\n");
  526. return -EINVAL;
  527. }
  528. _req->status = -EINPROGRESS;
  529. _req->actual = 0;
  530. spin_lock_irqsave(&udc->lock, flags);
  531. /* try to kickstart any empty and idle queue */
  532. if (list_empty(&ep->queue) && !ep->stopped) {
  533. int is_ep0;
  534. /*
  535. * If this control request has a non-empty DATA stage, this
  536. * will start that stage. It works just like a non-control
  537. * request (until the status stage starts, maybe early).
  538. *
  539. * If the data stage is empty, then this starts a successful
  540. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  541. */
  542. is_ep0 = (ep->ep.name == ep0name);
  543. if (is_ep0) {
  544. u32 tmp;
  545. if (!udc->req_pending) {
  546. status = -EINVAL;
  547. goto done;
  548. }
  549. /*
  550. * defer changing CONFG until after the gadget driver
  551. * reconfigures the endpoints.
  552. */
  553. if (udc->wait_for_config_ack) {
  554. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  555. tmp ^= AT91_UDP_CONFG;
  556. VDBG("toggle config\n");
  557. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  558. }
  559. if (req->req.length == 0) {
  560. ep0_in_status:
  561. PACKET("ep0 in/status\n");
  562. status = 0;
  563. tmp = __raw_readl(ep->creg);
  564. tmp &= ~SET_FX;
  565. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  566. __raw_writel(tmp, ep->creg);
  567. udc->req_pending = 0;
  568. goto done;
  569. }
  570. }
  571. if (ep->is_in)
  572. status = write_fifo(ep, req);
  573. else {
  574. status = read_fifo(ep, req);
  575. /* IN/STATUS stage is otherwise triggered by irq */
  576. if (status && is_ep0)
  577. goto ep0_in_status;
  578. }
  579. } else
  580. status = 0;
  581. if (req && !status) {
  582. list_add_tail (&req->queue, &ep->queue);
  583. at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
  584. }
  585. done:
  586. spin_unlock_irqrestore(&udc->lock, flags);
  587. return (status < 0) ? status : 0;
  588. }
  589. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  590. {
  591. struct at91_ep *ep;
  592. struct at91_request *req;
  593. unsigned long flags;
  594. struct at91_udc *udc;
  595. ep = container_of(_ep, struct at91_ep, ep);
  596. if (!_ep || ep->ep.name == ep0name)
  597. return -EINVAL;
  598. udc = ep->udc;
  599. spin_lock_irqsave(&udc->lock, flags);
  600. /* make sure it's actually queued on this endpoint */
  601. list_for_each_entry (req, &ep->queue, queue) {
  602. if (&req->req == _req)
  603. break;
  604. }
  605. if (&req->req != _req) {
  606. spin_unlock_irqrestore(&udc->lock, flags);
  607. return -EINVAL;
  608. }
  609. done(ep, req, -ECONNRESET);
  610. spin_unlock_irqrestore(&udc->lock, flags);
  611. return 0;
  612. }
  613. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  614. {
  615. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  616. struct at91_udc *udc = ep->udc;
  617. u32 __iomem *creg;
  618. u32 csr;
  619. unsigned long flags;
  620. int status = 0;
  621. if (!_ep || ep->is_iso || !ep->udc->clocked)
  622. return -EINVAL;
  623. creg = ep->creg;
  624. spin_lock_irqsave(&udc->lock, flags);
  625. csr = __raw_readl(creg);
  626. /*
  627. * fail with still-busy IN endpoints, ensuring correct sequencing
  628. * of data tx then stall. note that the fifo rx bytecount isn't
  629. * completely accurate as a tx bytecount.
  630. */
  631. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  632. status = -EAGAIN;
  633. else {
  634. csr |= CLR_FX;
  635. csr &= ~SET_FX;
  636. if (value) {
  637. csr |= AT91_UDP_FORCESTALL;
  638. VDBG("halt %s\n", ep->ep.name);
  639. } else {
  640. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  641. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  642. csr &= ~AT91_UDP_FORCESTALL;
  643. }
  644. __raw_writel(csr, creg);
  645. }
  646. spin_unlock_irqrestore(&udc->lock, flags);
  647. return status;
  648. }
  649. static const struct usb_ep_ops at91_ep_ops = {
  650. .enable = at91_ep_enable,
  651. .disable = at91_ep_disable,
  652. .alloc_request = at91_ep_alloc_request,
  653. .free_request = at91_ep_free_request,
  654. .queue = at91_ep_queue,
  655. .dequeue = at91_ep_dequeue,
  656. .set_halt = at91_ep_set_halt,
  657. /* there's only imprecise fifo status reporting */
  658. };
  659. /*-------------------------------------------------------------------------*/
  660. static int at91_get_frame(struct usb_gadget *gadget)
  661. {
  662. struct at91_udc *udc = to_udc(gadget);
  663. if (!to_udc(gadget)->clocked)
  664. return -EINVAL;
  665. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  666. }
  667. static int at91_wakeup(struct usb_gadget *gadget)
  668. {
  669. struct at91_udc *udc = to_udc(gadget);
  670. u32 glbstate;
  671. int status = -EINVAL;
  672. unsigned long flags;
  673. DBG("%s\n", __func__ );
  674. spin_lock_irqsave(&udc->lock, flags);
  675. if (!udc->clocked || !udc->suspended)
  676. goto done;
  677. /* NOTE: some "early versions" handle ESR differently ... */
  678. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  679. if (!(glbstate & AT91_UDP_ESR))
  680. goto done;
  681. glbstate |= AT91_UDP_ESR;
  682. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  683. done:
  684. spin_unlock_irqrestore(&udc->lock, flags);
  685. return status;
  686. }
  687. /* reinit == restore initial software state */
  688. static void udc_reinit(struct at91_udc *udc)
  689. {
  690. u32 i;
  691. INIT_LIST_HEAD(&udc->gadget.ep_list);
  692. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  693. for (i = 0; i < NUM_ENDPOINTS; i++) {
  694. struct at91_ep *ep = &udc->ep[i];
  695. if (i != 0)
  696. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  697. ep->desc = NULL;
  698. ep->stopped = 0;
  699. ep->fifo_bank = 0;
  700. ep->ep.maxpacket = ep->maxpacket;
  701. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  702. /* initialize one queue per endpoint */
  703. INIT_LIST_HEAD(&ep->queue);
  704. }
  705. }
  706. static void stop_activity(struct at91_udc *udc)
  707. {
  708. struct usb_gadget_driver *driver = udc->driver;
  709. int i;
  710. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  711. driver = NULL;
  712. udc->gadget.speed = USB_SPEED_UNKNOWN;
  713. udc->suspended = 0;
  714. for (i = 0; i < NUM_ENDPOINTS; i++) {
  715. struct at91_ep *ep = &udc->ep[i];
  716. ep->stopped = 1;
  717. nuke(ep, -ESHUTDOWN);
  718. }
  719. if (driver) {
  720. spin_unlock(&udc->lock);
  721. driver->disconnect(&udc->gadget);
  722. spin_lock(&udc->lock);
  723. }
  724. udc_reinit(udc);
  725. }
  726. static void clk_on(struct at91_udc *udc)
  727. {
  728. if (udc->clocked)
  729. return;
  730. udc->clocked = 1;
  731. clk_enable(udc->iclk);
  732. clk_enable(udc->fclk);
  733. }
  734. static void clk_off(struct at91_udc *udc)
  735. {
  736. if (!udc->clocked)
  737. return;
  738. udc->clocked = 0;
  739. udc->gadget.speed = USB_SPEED_UNKNOWN;
  740. clk_disable(udc->fclk);
  741. clk_disable(udc->iclk);
  742. }
  743. /*
  744. * activate/deactivate link with host; minimize power usage for
  745. * inactive links by cutting clocks and transceiver power.
  746. */
  747. static void pullup(struct at91_udc *udc, int is_on)
  748. {
  749. int active = !udc->board.pullup_active_low;
  750. if (!udc->enabled || !udc->vbus)
  751. is_on = 0;
  752. DBG("%sactive\n", is_on ? "" : "in");
  753. if (is_on) {
  754. clk_on(udc);
  755. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  756. at91_udp_write(udc, AT91_UDP_TXVC, 0);
  757. if (cpu_is_at91rm9200())
  758. gpio_set_value(udc->board.pullup_pin, active);
  759. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
  760. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  761. txvc |= AT91_UDP_TXVC_PUON;
  762. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  763. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  764. u32 usbpucr;
  765. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  766. usbpucr |= AT91_MATRIX_USBPUCR_PUON;
  767. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  768. }
  769. } else {
  770. stop_activity(udc);
  771. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  772. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  773. if (cpu_is_at91rm9200())
  774. gpio_set_value(udc->board.pullup_pin, !active);
  775. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
  776. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  777. txvc &= ~AT91_UDP_TXVC_PUON;
  778. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  779. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  780. u32 usbpucr;
  781. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  782. usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
  783. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  784. }
  785. clk_off(udc);
  786. }
  787. }
  788. /* vbus is here! turn everything on that's ready */
  789. static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
  790. {
  791. struct at91_udc *udc = to_udc(gadget);
  792. unsigned long flags;
  793. /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
  794. spin_lock_irqsave(&udc->lock, flags);
  795. udc->vbus = (is_active != 0);
  796. if (udc->driver)
  797. pullup(udc, is_active);
  798. else
  799. pullup(udc, 0);
  800. spin_unlock_irqrestore(&udc->lock, flags);
  801. return 0;
  802. }
  803. static int at91_pullup(struct usb_gadget *gadget, int is_on)
  804. {
  805. struct at91_udc *udc = to_udc(gadget);
  806. unsigned long flags;
  807. spin_lock_irqsave(&udc->lock, flags);
  808. udc->enabled = is_on = !!is_on;
  809. pullup(udc, is_on);
  810. spin_unlock_irqrestore(&udc->lock, flags);
  811. return 0;
  812. }
  813. static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
  814. {
  815. struct at91_udc *udc = to_udc(gadget);
  816. unsigned long flags;
  817. spin_lock_irqsave(&udc->lock, flags);
  818. udc->selfpowered = (is_on != 0);
  819. spin_unlock_irqrestore(&udc->lock, flags);
  820. return 0;
  821. }
  822. static int at91_start(struct usb_gadget_driver *driver,
  823. int (*bind)(struct usb_gadget *));
  824. static int at91_stop(struct usb_gadget_driver *driver);
  825. static const struct usb_gadget_ops at91_udc_ops = {
  826. .get_frame = at91_get_frame,
  827. .wakeup = at91_wakeup,
  828. .set_selfpowered = at91_set_selfpowered,
  829. .vbus_session = at91_vbus_session,
  830. .pullup = at91_pullup,
  831. .start = at91_start,
  832. .stop = at91_stop,
  833. /*
  834. * VBUS-powered devices may also also want to support bigger
  835. * power budgets after an appropriate SET_CONFIGURATION.
  836. */
  837. /* .vbus_power = at91_vbus_power, */
  838. };
  839. /*-------------------------------------------------------------------------*/
  840. static int handle_ep(struct at91_ep *ep)
  841. {
  842. struct at91_request *req;
  843. u32 __iomem *creg = ep->creg;
  844. u32 csr = __raw_readl(creg);
  845. if (!list_empty(&ep->queue))
  846. req = list_entry(ep->queue.next,
  847. struct at91_request, queue);
  848. else
  849. req = NULL;
  850. if (ep->is_in) {
  851. if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
  852. csr |= CLR_FX;
  853. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
  854. __raw_writel(csr, creg);
  855. }
  856. if (req)
  857. return write_fifo(ep, req);
  858. } else {
  859. if (csr & AT91_UDP_STALLSENT) {
  860. /* STALLSENT bit == ISOERR */
  861. if (ep->is_iso && req)
  862. req->req.status = -EILSEQ;
  863. csr |= CLR_FX;
  864. csr &= ~(SET_FX | AT91_UDP_STALLSENT);
  865. __raw_writel(csr, creg);
  866. csr = __raw_readl(creg);
  867. }
  868. if (req && (csr & RX_DATA_READY))
  869. return read_fifo(ep, req);
  870. }
  871. return 0;
  872. }
  873. union setup {
  874. u8 raw[8];
  875. struct usb_ctrlrequest r;
  876. };
  877. static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
  878. {
  879. u32 __iomem *creg = ep->creg;
  880. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  881. unsigned rxcount, i = 0;
  882. u32 tmp;
  883. union setup pkt;
  884. int status = 0;
  885. /* read and ack SETUP; hard-fail for bogus packets */
  886. rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
  887. if (likely(rxcount == 8)) {
  888. while (rxcount--)
  889. pkt.raw[i++] = __raw_readb(dreg);
  890. if (pkt.r.bRequestType & USB_DIR_IN) {
  891. csr |= AT91_UDP_DIR;
  892. ep->is_in = 1;
  893. } else {
  894. csr &= ~AT91_UDP_DIR;
  895. ep->is_in = 0;
  896. }
  897. } else {
  898. /* REVISIT this happens sometimes under load; why?? */
  899. ERR("SETUP len %d, csr %08x\n", rxcount, csr);
  900. status = -EINVAL;
  901. }
  902. csr |= CLR_FX;
  903. csr &= ~(SET_FX | AT91_UDP_RXSETUP);
  904. __raw_writel(csr, creg);
  905. udc->wait_for_addr_ack = 0;
  906. udc->wait_for_config_ack = 0;
  907. ep->stopped = 0;
  908. if (unlikely(status != 0))
  909. goto stall;
  910. #define w_index le16_to_cpu(pkt.r.wIndex)
  911. #define w_value le16_to_cpu(pkt.r.wValue)
  912. #define w_length le16_to_cpu(pkt.r.wLength)
  913. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  914. pkt.r.bRequestType, pkt.r.bRequest,
  915. w_value, w_index, w_length);
  916. /*
  917. * A few standard requests get handled here, ones that touch
  918. * hardware ... notably for device and endpoint features.
  919. */
  920. udc->req_pending = 1;
  921. csr = __raw_readl(creg);
  922. csr |= CLR_FX;
  923. csr &= ~SET_FX;
  924. switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
  925. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  926. | USB_REQ_SET_ADDRESS:
  927. __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
  928. udc->addr = w_value;
  929. udc->wait_for_addr_ack = 1;
  930. udc->req_pending = 0;
  931. /* FADDR is set later, when we ack host STATUS */
  932. return;
  933. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  934. | USB_REQ_SET_CONFIGURATION:
  935. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
  936. if (pkt.r.wValue)
  937. udc->wait_for_config_ack = (tmp == 0);
  938. else
  939. udc->wait_for_config_ack = (tmp != 0);
  940. if (udc->wait_for_config_ack)
  941. VDBG("wait for config\n");
  942. /* CONFG is toggled later, if gadget driver succeeds */
  943. break;
  944. /*
  945. * Hosts may set or clear remote wakeup status, and
  946. * devices may report they're VBUS powered.
  947. */
  948. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  949. | USB_REQ_GET_STATUS:
  950. tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  951. if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
  952. tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  953. PACKET("get device status\n");
  954. __raw_writeb(tmp, dreg);
  955. __raw_writeb(0, dreg);
  956. goto write_in;
  957. /* then STATUS starts later, automatically */
  958. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  959. | USB_REQ_SET_FEATURE:
  960. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  961. goto stall;
  962. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  963. tmp |= AT91_UDP_ESR;
  964. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  965. goto succeed;
  966. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  967. | USB_REQ_CLEAR_FEATURE:
  968. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  969. goto stall;
  970. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  971. tmp &= ~AT91_UDP_ESR;
  972. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  973. goto succeed;
  974. /*
  975. * Interfaces have no feature settings; this is pretty useless.
  976. * we won't even insist the interface exists...
  977. */
  978. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  979. | USB_REQ_GET_STATUS:
  980. PACKET("get interface status\n");
  981. __raw_writeb(0, dreg);
  982. __raw_writeb(0, dreg);
  983. goto write_in;
  984. /* then STATUS starts later, automatically */
  985. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  986. | USB_REQ_SET_FEATURE:
  987. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  988. | USB_REQ_CLEAR_FEATURE:
  989. goto stall;
  990. /*
  991. * Hosts may clear bulk/intr endpoint halt after the gadget
  992. * driver sets it (not widely used); or set it (for testing)
  993. */
  994. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  995. | USB_REQ_GET_STATUS:
  996. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  997. ep = &udc->ep[tmp];
  998. if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
  999. goto stall;
  1000. if (tmp) {
  1001. if ((w_index & USB_DIR_IN)) {
  1002. if (!ep->is_in)
  1003. goto stall;
  1004. } else if (ep->is_in)
  1005. goto stall;
  1006. }
  1007. PACKET("get %s status\n", ep->ep.name);
  1008. if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
  1009. tmp = (1 << USB_ENDPOINT_HALT);
  1010. else
  1011. tmp = 0;
  1012. __raw_writeb(tmp, dreg);
  1013. __raw_writeb(0, dreg);
  1014. goto write_in;
  1015. /* then STATUS starts later, automatically */
  1016. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1017. | USB_REQ_SET_FEATURE:
  1018. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1019. ep = &udc->ep[tmp];
  1020. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1021. goto stall;
  1022. if (!ep->desc || ep->is_iso)
  1023. goto stall;
  1024. if ((w_index & USB_DIR_IN)) {
  1025. if (!ep->is_in)
  1026. goto stall;
  1027. } else if (ep->is_in)
  1028. goto stall;
  1029. tmp = __raw_readl(ep->creg);
  1030. tmp &= ~SET_FX;
  1031. tmp |= CLR_FX | AT91_UDP_FORCESTALL;
  1032. __raw_writel(tmp, ep->creg);
  1033. goto succeed;
  1034. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1035. | USB_REQ_CLEAR_FEATURE:
  1036. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1037. ep = &udc->ep[tmp];
  1038. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1039. goto stall;
  1040. if (tmp == 0)
  1041. goto succeed;
  1042. if (!ep->desc || ep->is_iso)
  1043. goto stall;
  1044. if ((w_index & USB_DIR_IN)) {
  1045. if (!ep->is_in)
  1046. goto stall;
  1047. } else if (ep->is_in)
  1048. goto stall;
  1049. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  1050. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  1051. tmp = __raw_readl(ep->creg);
  1052. tmp |= CLR_FX;
  1053. tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
  1054. __raw_writel(tmp, ep->creg);
  1055. if (!list_empty(&ep->queue))
  1056. handle_ep(ep);
  1057. goto succeed;
  1058. }
  1059. #undef w_value
  1060. #undef w_index
  1061. #undef w_length
  1062. /* pass request up to the gadget driver */
  1063. if (udc->driver) {
  1064. spin_unlock(&udc->lock);
  1065. status = udc->driver->setup(&udc->gadget, &pkt.r);
  1066. spin_lock(&udc->lock);
  1067. }
  1068. else
  1069. status = -ENODEV;
  1070. if (status < 0) {
  1071. stall:
  1072. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1073. pkt.r.bRequestType, pkt.r.bRequest, status);
  1074. csr |= AT91_UDP_FORCESTALL;
  1075. __raw_writel(csr, creg);
  1076. udc->req_pending = 0;
  1077. }
  1078. return;
  1079. succeed:
  1080. /* immediate successful (IN) STATUS after zero length DATA */
  1081. PACKET("ep0 in/status\n");
  1082. write_in:
  1083. csr |= AT91_UDP_TXPKTRDY;
  1084. __raw_writel(csr, creg);
  1085. udc->req_pending = 0;
  1086. }
  1087. static void handle_ep0(struct at91_udc *udc)
  1088. {
  1089. struct at91_ep *ep0 = &udc->ep[0];
  1090. u32 __iomem *creg = ep0->creg;
  1091. u32 csr = __raw_readl(creg);
  1092. struct at91_request *req;
  1093. if (unlikely(csr & AT91_UDP_STALLSENT)) {
  1094. nuke(ep0, -EPROTO);
  1095. udc->req_pending = 0;
  1096. csr |= CLR_FX;
  1097. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
  1098. __raw_writel(csr, creg);
  1099. VDBG("ep0 stalled\n");
  1100. csr = __raw_readl(creg);
  1101. }
  1102. if (csr & AT91_UDP_RXSETUP) {
  1103. nuke(ep0, 0);
  1104. udc->req_pending = 0;
  1105. handle_setup(udc, ep0, csr);
  1106. return;
  1107. }
  1108. if (list_empty(&ep0->queue))
  1109. req = NULL;
  1110. else
  1111. req = list_entry(ep0->queue.next, struct at91_request, queue);
  1112. /* host ACKed an IN packet that we sent */
  1113. if (csr & AT91_UDP_TXCOMP) {
  1114. csr |= CLR_FX;
  1115. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  1116. /* write more IN DATA? */
  1117. if (req && ep0->is_in) {
  1118. if (handle_ep(ep0))
  1119. udc->req_pending = 0;
  1120. /*
  1121. * Ack after:
  1122. * - last IN DATA packet (including GET_STATUS)
  1123. * - IN/STATUS for OUT DATA
  1124. * - IN/STATUS for any zero-length DATA stage
  1125. * except for the IN DATA case, the host should send
  1126. * an OUT status later, which we'll ack.
  1127. */
  1128. } else {
  1129. udc->req_pending = 0;
  1130. __raw_writel(csr, creg);
  1131. /*
  1132. * SET_ADDRESS takes effect only after the STATUS
  1133. * (to the original address) gets acked.
  1134. */
  1135. if (udc->wait_for_addr_ack) {
  1136. u32 tmp;
  1137. at91_udp_write(udc, AT91_UDP_FADDR,
  1138. AT91_UDP_FEN | udc->addr);
  1139. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  1140. tmp &= ~AT91_UDP_FADDEN;
  1141. if (udc->addr)
  1142. tmp |= AT91_UDP_FADDEN;
  1143. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  1144. udc->wait_for_addr_ack = 0;
  1145. VDBG("address %d\n", udc->addr);
  1146. }
  1147. }
  1148. }
  1149. /* OUT packet arrived ... */
  1150. else if (csr & AT91_UDP_RX_DATA_BK0) {
  1151. csr |= CLR_FX;
  1152. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  1153. /* OUT DATA stage */
  1154. if (!ep0->is_in) {
  1155. if (req) {
  1156. if (handle_ep(ep0)) {
  1157. /* send IN/STATUS */
  1158. PACKET("ep0 in/status\n");
  1159. csr = __raw_readl(creg);
  1160. csr &= ~SET_FX;
  1161. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  1162. __raw_writel(csr, creg);
  1163. udc->req_pending = 0;
  1164. }
  1165. } else if (udc->req_pending) {
  1166. /*
  1167. * AT91 hardware has a hard time with this
  1168. * "deferred response" mode for control-OUT
  1169. * transfers. (For control-IN it's fine.)
  1170. *
  1171. * The normal solution leaves OUT data in the
  1172. * fifo until the gadget driver is ready.
  1173. * We couldn't do that here without disabling
  1174. * the IRQ that tells about SETUP packets,
  1175. * e.g. when the host gets impatient...
  1176. *
  1177. * Working around it by copying into a buffer
  1178. * would almost be a non-deferred response,
  1179. * except that it wouldn't permit reliable
  1180. * stalling of the request. Instead, demand
  1181. * that gadget drivers not use this mode.
  1182. */
  1183. DBG("no control-OUT deferred responses!\n");
  1184. __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
  1185. udc->req_pending = 0;
  1186. }
  1187. /* STATUS stage for control-IN; ack. */
  1188. } else {
  1189. PACKET("ep0 out/status ACK\n");
  1190. __raw_writel(csr, creg);
  1191. /* "early" status stage */
  1192. if (req)
  1193. done(ep0, req, 0);
  1194. }
  1195. }
  1196. }
  1197. static irqreturn_t at91_udc_irq (int irq, void *_udc)
  1198. {
  1199. struct at91_udc *udc = _udc;
  1200. u32 rescans = 5;
  1201. int disable_clock = 0;
  1202. unsigned long flags;
  1203. spin_lock_irqsave(&udc->lock, flags);
  1204. if (!udc->clocked) {
  1205. clk_on(udc);
  1206. disable_clock = 1;
  1207. }
  1208. while (rescans--) {
  1209. u32 status;
  1210. status = at91_udp_read(udc, AT91_UDP_ISR)
  1211. & at91_udp_read(udc, AT91_UDP_IMR);
  1212. if (!status)
  1213. break;
  1214. /* USB reset irq: not maskable */
  1215. if (status & AT91_UDP_ENDBUSRES) {
  1216. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1217. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1218. /* Atmel code clears this irq twice */
  1219. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1220. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1221. VDBG("end bus reset\n");
  1222. udc->addr = 0;
  1223. stop_activity(udc);
  1224. /* enable ep0 */
  1225. at91_udp_write(udc, AT91_UDP_CSR(0),
  1226. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1227. udc->gadget.speed = USB_SPEED_FULL;
  1228. udc->suspended = 0;
  1229. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1230. /*
  1231. * NOTE: this driver keeps clocks off unless the
  1232. * USB host is present. That saves power, but for
  1233. * boards that don't support VBUS detection, both
  1234. * clocks need to be active most of the time.
  1235. */
  1236. /* host initiated suspend (3+ms bus idle) */
  1237. } else if (status & AT91_UDP_RXSUSP) {
  1238. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1239. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1240. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1241. /* VDBG("bus suspend\n"); */
  1242. if (udc->suspended)
  1243. continue;
  1244. udc->suspended = 1;
  1245. /*
  1246. * NOTE: when suspending a VBUS-powered device, the
  1247. * gadget driver should switch into slow clock mode
  1248. * and then into standby to avoid drawing more than
  1249. * 500uA power (2500uA for some high-power configs).
  1250. */
  1251. if (udc->driver && udc->driver->suspend) {
  1252. spin_unlock(&udc->lock);
  1253. udc->driver->suspend(&udc->gadget);
  1254. spin_lock(&udc->lock);
  1255. }
  1256. /* host initiated resume */
  1257. } else if (status & AT91_UDP_RXRSM) {
  1258. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1259. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1260. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1261. /* VDBG("bus resume\n"); */
  1262. if (!udc->suspended)
  1263. continue;
  1264. udc->suspended = 0;
  1265. /*
  1266. * NOTE: for a VBUS-powered device, the gadget driver
  1267. * would normally want to switch out of slow clock
  1268. * mode into normal mode.
  1269. */
  1270. if (udc->driver && udc->driver->resume) {
  1271. spin_unlock(&udc->lock);
  1272. udc->driver->resume(&udc->gadget);
  1273. spin_lock(&udc->lock);
  1274. }
  1275. /* endpoint IRQs are cleared by handling them */
  1276. } else {
  1277. int i;
  1278. unsigned mask = 1;
  1279. struct at91_ep *ep = &udc->ep[1];
  1280. if (status & mask)
  1281. handle_ep0(udc);
  1282. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1283. mask <<= 1;
  1284. if (status & mask)
  1285. handle_ep(ep);
  1286. ep++;
  1287. }
  1288. }
  1289. }
  1290. if (disable_clock)
  1291. clk_off(udc);
  1292. spin_unlock_irqrestore(&udc->lock, flags);
  1293. return IRQ_HANDLED;
  1294. }
  1295. /*-------------------------------------------------------------------------*/
  1296. static void nop_release(struct device *dev)
  1297. {
  1298. /* nothing to free */
  1299. }
  1300. static struct at91_udc controller = {
  1301. .gadget = {
  1302. .ops = &at91_udc_ops,
  1303. .ep0 = &controller.ep[0].ep,
  1304. .name = driver_name,
  1305. .dev = {
  1306. .init_name = "gadget",
  1307. .release = nop_release,
  1308. }
  1309. },
  1310. .ep[0] = {
  1311. .ep = {
  1312. .name = ep0name,
  1313. .ops = &at91_ep_ops,
  1314. },
  1315. .udc = &controller,
  1316. .maxpacket = 8,
  1317. .int_mask = 1 << 0,
  1318. },
  1319. .ep[1] = {
  1320. .ep = {
  1321. .name = "ep1",
  1322. .ops = &at91_ep_ops,
  1323. },
  1324. .udc = &controller,
  1325. .is_pingpong = 1,
  1326. .maxpacket = 64,
  1327. .int_mask = 1 << 1,
  1328. },
  1329. .ep[2] = {
  1330. .ep = {
  1331. .name = "ep2",
  1332. .ops = &at91_ep_ops,
  1333. },
  1334. .udc = &controller,
  1335. .is_pingpong = 1,
  1336. .maxpacket = 64,
  1337. .int_mask = 1 << 2,
  1338. },
  1339. .ep[3] = {
  1340. .ep = {
  1341. /* could actually do bulk too */
  1342. .name = "ep3-int",
  1343. .ops = &at91_ep_ops,
  1344. },
  1345. .udc = &controller,
  1346. .maxpacket = 8,
  1347. .int_mask = 1 << 3,
  1348. },
  1349. .ep[4] = {
  1350. .ep = {
  1351. .name = "ep4",
  1352. .ops = &at91_ep_ops,
  1353. },
  1354. .udc = &controller,
  1355. .is_pingpong = 1,
  1356. .maxpacket = 256,
  1357. .int_mask = 1 << 4,
  1358. },
  1359. .ep[5] = {
  1360. .ep = {
  1361. .name = "ep5",
  1362. .ops = &at91_ep_ops,
  1363. },
  1364. .udc = &controller,
  1365. .is_pingpong = 1,
  1366. .maxpacket = 256,
  1367. .int_mask = 1 << 5,
  1368. },
  1369. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1370. };
  1371. static void at91_vbus_update(struct at91_udc *udc, unsigned value)
  1372. {
  1373. value ^= udc->board.vbus_active_low;
  1374. if (value != udc->vbus)
  1375. at91_vbus_session(&udc->gadget, value);
  1376. }
  1377. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1378. {
  1379. struct at91_udc *udc = _udc;
  1380. /* vbus needs at least brief debouncing */
  1381. udelay(10);
  1382. at91_vbus_update(udc, gpio_get_value(udc->board.vbus_pin));
  1383. return IRQ_HANDLED;
  1384. }
  1385. static void at91_vbus_timer_work(struct work_struct *work)
  1386. {
  1387. struct at91_udc *udc = container_of(work, struct at91_udc,
  1388. vbus_timer_work);
  1389. at91_vbus_update(udc, gpio_get_value_cansleep(udc->board.vbus_pin));
  1390. if (!timer_pending(&udc->vbus_timer))
  1391. mod_timer(&udc->vbus_timer, jiffies + VBUS_POLL_TIMEOUT);
  1392. }
  1393. static void at91_vbus_timer(unsigned long data)
  1394. {
  1395. struct at91_udc *udc = (struct at91_udc *)data;
  1396. /*
  1397. * If we are polling vbus it is likely that the gpio is on an
  1398. * bus such as i2c or spi which may sleep, so schedule some work
  1399. * to read the vbus gpio
  1400. */
  1401. if (!work_pending(&udc->vbus_timer_work))
  1402. schedule_work(&udc->vbus_timer_work);
  1403. }
  1404. static int at91_start(struct usb_gadget_driver *driver,
  1405. int (*bind)(struct usb_gadget *))
  1406. {
  1407. struct at91_udc *udc = &controller;
  1408. int retval;
  1409. unsigned long flags;
  1410. if (!driver
  1411. || driver->max_speed < USB_SPEED_FULL
  1412. || !bind
  1413. || !driver->setup) {
  1414. DBG("bad parameter.\n");
  1415. return -EINVAL;
  1416. }
  1417. if (udc->driver) {
  1418. DBG("UDC already has a gadget driver\n");
  1419. return -EBUSY;
  1420. }
  1421. udc->driver = driver;
  1422. udc->gadget.dev.driver = &driver->driver;
  1423. dev_set_drvdata(&udc->gadget.dev, &driver->driver);
  1424. udc->enabled = 1;
  1425. udc->selfpowered = 1;
  1426. retval = bind(&udc->gadget);
  1427. if (retval) {
  1428. DBG("bind() returned %d\n", retval);
  1429. udc->driver = NULL;
  1430. udc->gadget.dev.driver = NULL;
  1431. dev_set_drvdata(&udc->gadget.dev, NULL);
  1432. udc->enabled = 0;
  1433. udc->selfpowered = 0;
  1434. return retval;
  1435. }
  1436. spin_lock_irqsave(&udc->lock, flags);
  1437. pullup(udc, 1);
  1438. spin_unlock_irqrestore(&udc->lock, flags);
  1439. DBG("bound to %s\n", driver->driver.name);
  1440. return 0;
  1441. }
  1442. static int at91_stop(struct usb_gadget_driver *driver)
  1443. {
  1444. struct at91_udc *udc = &controller;
  1445. unsigned long flags;
  1446. if (!driver || driver != udc->driver || !driver->unbind)
  1447. return -EINVAL;
  1448. spin_lock_irqsave(&udc->lock, flags);
  1449. udc->enabled = 0;
  1450. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1451. pullup(udc, 0);
  1452. spin_unlock_irqrestore(&udc->lock, flags);
  1453. driver->unbind(&udc->gadget);
  1454. udc->gadget.dev.driver = NULL;
  1455. dev_set_drvdata(&udc->gadget.dev, NULL);
  1456. udc->driver = NULL;
  1457. DBG("unbound from %s\n", driver->driver.name);
  1458. return 0;
  1459. }
  1460. /*-------------------------------------------------------------------------*/
  1461. static void at91udc_shutdown(struct platform_device *dev)
  1462. {
  1463. struct at91_udc *udc = platform_get_drvdata(dev);
  1464. unsigned long flags;
  1465. /* force disconnect on reboot */
  1466. spin_lock_irqsave(&udc->lock, flags);
  1467. pullup(platform_get_drvdata(dev), 0);
  1468. spin_unlock_irqrestore(&udc->lock, flags);
  1469. }
  1470. static int __init at91udc_probe(struct platform_device *pdev)
  1471. {
  1472. struct device *dev = &pdev->dev;
  1473. struct at91_udc *udc;
  1474. int retval;
  1475. struct resource *res;
  1476. if (!dev->platform_data) {
  1477. /* small (so we copy it) but critical! */
  1478. DBG("missing platform_data\n");
  1479. return -ENODEV;
  1480. }
  1481. if (pdev->num_resources != 2) {
  1482. DBG("invalid num_resources\n");
  1483. return -ENODEV;
  1484. }
  1485. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1486. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1487. DBG("invalid resource type\n");
  1488. return -ENODEV;
  1489. }
  1490. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1491. if (!res)
  1492. return -ENXIO;
  1493. if (!request_mem_region(res->start, resource_size(res), driver_name)) {
  1494. DBG("someone's using UDC memory\n");
  1495. return -EBUSY;
  1496. }
  1497. /* init software state */
  1498. udc = &controller;
  1499. udc->gadget.dev.parent = dev;
  1500. udc->board = *(struct at91_udc_data *) dev->platform_data;
  1501. udc->pdev = pdev;
  1502. udc->enabled = 0;
  1503. spin_lock_init(&udc->lock);
  1504. /* rm9200 needs manual D+ pullup; off by default */
  1505. if (cpu_is_at91rm9200()) {
  1506. if (gpio_is_valid(udc->board.pullup_pin)) {
  1507. DBG("no D+ pullup?\n");
  1508. retval = -ENODEV;
  1509. goto fail0;
  1510. }
  1511. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1512. if (retval) {
  1513. DBG("D+ pullup is busy\n");
  1514. goto fail0;
  1515. }
  1516. gpio_direction_output(udc->board.pullup_pin,
  1517. udc->board.pullup_active_low);
  1518. }
  1519. /* newer chips have more FIFO memory than rm9200 */
  1520. if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
  1521. udc->ep[0].maxpacket = 64;
  1522. udc->ep[3].maxpacket = 64;
  1523. udc->ep[4].maxpacket = 512;
  1524. udc->ep[5].maxpacket = 512;
  1525. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  1526. udc->ep[3].maxpacket = 64;
  1527. } else if (cpu_is_at91sam9263()) {
  1528. udc->ep[0].maxpacket = 64;
  1529. udc->ep[3].maxpacket = 64;
  1530. }
  1531. udc->udp_baseaddr = ioremap(res->start, resource_size(res));
  1532. if (!udc->udp_baseaddr) {
  1533. retval = -ENOMEM;
  1534. goto fail0a;
  1535. }
  1536. udc_reinit(udc);
  1537. /* get interface and function clocks */
  1538. udc->iclk = clk_get(dev, "udc_clk");
  1539. udc->fclk = clk_get(dev, "udpck");
  1540. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1541. DBG("clocks missing\n");
  1542. retval = -ENODEV;
  1543. /* NOTE: we "know" here that refcounts on these are NOPs */
  1544. goto fail0b;
  1545. }
  1546. retval = device_register(&udc->gadget.dev);
  1547. if (retval < 0) {
  1548. put_device(&udc->gadget.dev);
  1549. goto fail0b;
  1550. }
  1551. /* don't do anything until we have both gadget driver and VBUS */
  1552. clk_enable(udc->iclk);
  1553. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1554. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1555. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1556. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1557. clk_disable(udc->iclk);
  1558. /* request UDC and maybe VBUS irqs */
  1559. udc->udp_irq = platform_get_irq(pdev, 0);
  1560. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1561. 0, driver_name, udc);
  1562. if (retval < 0) {
  1563. DBG("request irq %d failed\n", udc->udp_irq);
  1564. goto fail1;
  1565. }
  1566. if (gpio_is_valid(udc->board.vbus_pin)) {
  1567. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1568. if (retval < 0) {
  1569. DBG("request vbus pin failed\n");
  1570. goto fail2;
  1571. }
  1572. gpio_direction_input(udc->board.vbus_pin);
  1573. /*
  1574. * Get the initial state of VBUS - we cannot expect
  1575. * a pending interrupt.
  1576. */
  1577. udc->vbus = gpio_get_value_cansleep(udc->board.vbus_pin) ^
  1578. udc->board.vbus_active_low;
  1579. if (udc->board.vbus_polled) {
  1580. INIT_WORK(&udc->vbus_timer_work, at91_vbus_timer_work);
  1581. setup_timer(&udc->vbus_timer, at91_vbus_timer,
  1582. (unsigned long)udc);
  1583. mod_timer(&udc->vbus_timer,
  1584. jiffies + VBUS_POLL_TIMEOUT);
  1585. } else {
  1586. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1587. 0, driver_name, udc)) {
  1588. DBG("request vbus irq %d failed\n",
  1589. udc->board.vbus_pin);
  1590. retval = -EBUSY;
  1591. goto fail3;
  1592. }
  1593. }
  1594. } else {
  1595. DBG("no VBUS detection, assuming always-on\n");
  1596. udc->vbus = 1;
  1597. }
  1598. retval = usb_add_gadget_udc(dev, &udc->gadget);
  1599. if (retval)
  1600. goto fail4;
  1601. dev_set_drvdata(dev, udc);
  1602. device_init_wakeup(dev, 1);
  1603. create_debug_file(udc);
  1604. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1605. return 0;
  1606. fail4:
  1607. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled)
  1608. free_irq(udc->board.vbus_pin, udc);
  1609. fail3:
  1610. if (gpio_is_valid(udc->board.vbus_pin))
  1611. gpio_free(udc->board.vbus_pin);
  1612. fail2:
  1613. free_irq(udc->udp_irq, udc);
  1614. fail1:
  1615. device_unregister(&udc->gadget.dev);
  1616. fail0b:
  1617. iounmap(udc->udp_baseaddr);
  1618. fail0a:
  1619. if (cpu_is_at91rm9200())
  1620. gpio_free(udc->board.pullup_pin);
  1621. fail0:
  1622. release_mem_region(res->start, resource_size(res));
  1623. DBG("%s probe failed, %d\n", driver_name, retval);
  1624. return retval;
  1625. }
  1626. static int __exit at91udc_remove(struct platform_device *pdev)
  1627. {
  1628. struct at91_udc *udc = platform_get_drvdata(pdev);
  1629. struct resource *res;
  1630. unsigned long flags;
  1631. DBG("remove\n");
  1632. usb_del_gadget_udc(&udc->gadget);
  1633. if (udc->driver)
  1634. return -EBUSY;
  1635. spin_lock_irqsave(&udc->lock, flags);
  1636. pullup(udc, 0);
  1637. spin_unlock_irqrestore(&udc->lock, flags);
  1638. device_init_wakeup(&pdev->dev, 0);
  1639. remove_debug_file(udc);
  1640. if (gpio_is_valid(udc->board.vbus_pin)) {
  1641. free_irq(udc->board.vbus_pin, udc);
  1642. gpio_free(udc->board.vbus_pin);
  1643. }
  1644. free_irq(udc->udp_irq, udc);
  1645. device_unregister(&udc->gadget.dev);
  1646. iounmap(udc->udp_baseaddr);
  1647. if (cpu_is_at91rm9200())
  1648. gpio_free(udc->board.pullup_pin);
  1649. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1650. release_mem_region(res->start, resource_size(res));
  1651. clk_put(udc->iclk);
  1652. clk_put(udc->fclk);
  1653. return 0;
  1654. }
  1655. #ifdef CONFIG_PM
  1656. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1657. {
  1658. struct at91_udc *udc = platform_get_drvdata(pdev);
  1659. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1660. unsigned long flags;
  1661. /* Unless we can act normally to the host (letting it wake us up
  1662. * whenever it has work for us) force disconnect. Wakeup requires
  1663. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1664. * tokens) and VBUS irqs (on systems which support them).
  1665. */
  1666. if ((!udc->suspended && udc->addr)
  1667. || !wake
  1668. || at91_suspend_entering_slow_clock()) {
  1669. spin_lock_irqsave(&udc->lock, flags);
  1670. pullup(udc, 0);
  1671. wake = 0;
  1672. spin_unlock_irqrestore(&udc->lock, flags);
  1673. } else
  1674. enable_irq_wake(udc->udp_irq);
  1675. udc->active_suspend = wake;
  1676. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled && wake)
  1677. enable_irq_wake(udc->board.vbus_pin);
  1678. return 0;
  1679. }
  1680. static int at91udc_resume(struct platform_device *pdev)
  1681. {
  1682. struct at91_udc *udc = platform_get_drvdata(pdev);
  1683. unsigned long flags;
  1684. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled &&
  1685. udc->active_suspend)
  1686. disable_irq_wake(udc->board.vbus_pin);
  1687. /* maybe reconnect to host; if so, clocks on */
  1688. if (udc->active_suspend)
  1689. disable_irq_wake(udc->udp_irq);
  1690. else {
  1691. spin_lock_irqsave(&udc->lock, flags);
  1692. pullup(udc, 1);
  1693. spin_unlock_irqrestore(&udc->lock, flags);
  1694. }
  1695. return 0;
  1696. }
  1697. #else
  1698. #define at91udc_suspend NULL
  1699. #define at91udc_resume NULL
  1700. #endif
  1701. static struct platform_driver at91_udc_driver = {
  1702. .remove = __exit_p(at91udc_remove),
  1703. .shutdown = at91udc_shutdown,
  1704. .suspend = at91udc_suspend,
  1705. .resume = at91udc_resume,
  1706. .driver = {
  1707. .name = (char *) driver_name,
  1708. .owner = THIS_MODULE,
  1709. },
  1710. };
  1711. static int __init udc_init_module(void)
  1712. {
  1713. return platform_driver_probe(&at91_udc_driver, at91udc_probe);
  1714. }
  1715. module_init(udc_init_module);
  1716. static void __exit udc_exit_module(void)
  1717. {
  1718. platform_driver_unregister(&at91_udc_driver);
  1719. }
  1720. module_exit(udc_exit_module);
  1721. MODULE_DESCRIPTION("AT91 udc driver");
  1722. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1723. MODULE_LICENSE("GPL");
  1724. MODULE_ALIAS("platform:at91_udc");