at91_udc.c 51 KB

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