at91_udc.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  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/list.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/prefetch.h>
  26. #include <linux/clk.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. #include <linux/of.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/platform_data/atmel.h>
  32. #include <asm/byteorder.h>
  33. #include <mach/hardware.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/gpio.h>
  37. #include <mach/cpu.h>
  38. #include <mach/at91sam9261_matrix.h>
  39. #include <mach/at91_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 supporting
  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->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_DATA(inode));
  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. usb_gadget_giveback_request(&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;
  397. u16 maxpacket;
  398. u32 tmp;
  399. unsigned long flags;
  400. if (!_ep || !ep
  401. || !desc || _ep->name == ep0name
  402. || desc->bDescriptorType != USB_DT_ENDPOINT
  403. || (maxpacket = usb_endpoint_maxp(desc)) == 0
  404. || maxpacket > ep->maxpacket) {
  405. DBG("bad ep or descriptor\n");
  406. return -EINVAL;
  407. }
  408. udc = ep->udc;
  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->ep.maxpacket = maxpacket;
  452. /*
  453. * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
  454. * since endpoint resets don't reset hw pingpong state.
  455. */
  456. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  457. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  458. spin_unlock_irqrestore(&udc->lock, flags);
  459. return 0;
  460. }
  461. static int at91_ep_disable (struct usb_ep * _ep)
  462. {
  463. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  464. struct at91_udc *udc = ep->udc;
  465. unsigned long flags;
  466. if (ep == &ep->udc->ep[0])
  467. return -EINVAL;
  468. spin_lock_irqsave(&udc->lock, flags);
  469. nuke(ep, -ESHUTDOWN);
  470. /* restore the endpoint's pristine config */
  471. ep->ep.desc = NULL;
  472. ep->ep.maxpacket = ep->maxpacket;
  473. /* reset fifos and endpoint */
  474. if (ep->udc->clocked) {
  475. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  476. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  477. __raw_writel(0, ep->creg);
  478. }
  479. spin_unlock_irqrestore(&udc->lock, flags);
  480. return 0;
  481. }
  482. /*
  483. * this is a PIO-only driver, so there's nothing
  484. * interesting for request or buffer allocation.
  485. */
  486. static struct usb_request *
  487. at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  488. {
  489. struct at91_request *req;
  490. req = kzalloc(sizeof (struct at91_request), gfp_flags);
  491. if (!req)
  492. return NULL;
  493. INIT_LIST_HEAD(&req->queue);
  494. return &req->req;
  495. }
  496. static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  497. {
  498. struct at91_request *req;
  499. req = container_of(_req, struct at91_request, req);
  500. BUG_ON(!list_empty(&req->queue));
  501. kfree(req);
  502. }
  503. static int at91_ep_queue(struct usb_ep *_ep,
  504. struct usb_request *_req, gfp_t gfp_flags)
  505. {
  506. struct at91_request *req;
  507. struct at91_ep *ep;
  508. struct at91_udc *udc;
  509. int status;
  510. unsigned long flags;
  511. req = container_of(_req, struct at91_request, req);
  512. ep = container_of(_ep, struct at91_ep, ep);
  513. if (!_req || !_req->complete
  514. || !_req->buf || !list_empty(&req->queue)) {
  515. DBG("invalid request\n");
  516. return -EINVAL;
  517. }
  518. if (!_ep || (!ep->ep.desc && ep->ep.name != ep0name)) {
  519. DBG("invalid ep\n");
  520. return -EINVAL;
  521. }
  522. udc = ep->udc;
  523. if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  524. DBG("invalid device\n");
  525. return -EINVAL;
  526. }
  527. _req->status = -EINPROGRESS;
  528. _req->actual = 0;
  529. spin_lock_irqsave(&udc->lock, flags);
  530. /* try to kickstart any empty and idle queue */
  531. if (list_empty(&ep->queue) && !ep->stopped) {
  532. int is_ep0;
  533. /*
  534. * If this control request has a non-empty DATA stage, this
  535. * will start that stage. It works just like a non-control
  536. * request (until the status stage starts, maybe early).
  537. *
  538. * If the data stage is empty, then this starts a successful
  539. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  540. */
  541. is_ep0 = (ep->ep.name == ep0name);
  542. if (is_ep0) {
  543. u32 tmp;
  544. if (!udc->req_pending) {
  545. status = -EINVAL;
  546. goto done;
  547. }
  548. /*
  549. * defer changing CONFG until after the gadget driver
  550. * reconfigures the endpoints.
  551. */
  552. if (udc->wait_for_config_ack) {
  553. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  554. tmp ^= AT91_UDP_CONFG;
  555. VDBG("toggle config\n");
  556. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  557. }
  558. if (req->req.length == 0) {
  559. ep0_in_status:
  560. PACKET("ep0 in/status\n");
  561. status = 0;
  562. tmp = __raw_readl(ep->creg);
  563. tmp &= ~SET_FX;
  564. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  565. __raw_writel(tmp, ep->creg);
  566. udc->req_pending = 0;
  567. goto done;
  568. }
  569. }
  570. if (ep->is_in)
  571. status = write_fifo(ep, req);
  572. else {
  573. status = read_fifo(ep, req);
  574. /* IN/STATUS stage is otherwise triggered by irq */
  575. if (status && is_ep0)
  576. goto ep0_in_status;
  577. }
  578. } else
  579. status = 0;
  580. if (req && !status) {
  581. list_add_tail (&req->queue, &ep->queue);
  582. at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
  583. }
  584. done:
  585. spin_unlock_irqrestore(&udc->lock, flags);
  586. return (status < 0) ? status : 0;
  587. }
  588. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  589. {
  590. struct at91_ep *ep;
  591. struct at91_request *req;
  592. unsigned long flags;
  593. struct at91_udc *udc;
  594. ep = container_of(_ep, struct at91_ep, ep);
  595. if (!_ep || ep->ep.name == ep0name)
  596. return -EINVAL;
  597. udc = ep->udc;
  598. spin_lock_irqsave(&udc->lock, flags);
  599. /* make sure it's actually queued on this endpoint */
  600. list_for_each_entry (req, &ep->queue, queue) {
  601. if (&req->req == _req)
  602. break;
  603. }
  604. if (&req->req != _req) {
  605. spin_unlock_irqrestore(&udc->lock, flags);
  606. return -EINVAL;
  607. }
  608. done(ep, req, -ECONNRESET);
  609. spin_unlock_irqrestore(&udc->lock, flags);
  610. return 0;
  611. }
  612. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  613. {
  614. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  615. struct at91_udc *udc = ep->udc;
  616. u32 __iomem *creg;
  617. u32 csr;
  618. unsigned long flags;
  619. int status = 0;
  620. if (!_ep || ep->is_iso || !ep->udc->clocked)
  621. return -EINVAL;
  622. creg = ep->creg;
  623. spin_lock_irqsave(&udc->lock, flags);
  624. csr = __raw_readl(creg);
  625. /*
  626. * fail with still-busy IN endpoints, ensuring correct sequencing
  627. * of data tx then stall. note that the fifo rx bytecount isn't
  628. * completely accurate as a tx bytecount.
  629. */
  630. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  631. status = -EAGAIN;
  632. else {
  633. csr |= CLR_FX;
  634. csr &= ~SET_FX;
  635. if (value) {
  636. csr |= AT91_UDP_FORCESTALL;
  637. VDBG("halt %s\n", ep->ep.name);
  638. } else {
  639. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  640. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  641. csr &= ~AT91_UDP_FORCESTALL;
  642. }
  643. __raw_writel(csr, creg);
  644. }
  645. spin_unlock_irqrestore(&udc->lock, flags);
  646. return status;
  647. }
  648. static const struct usb_ep_ops at91_ep_ops = {
  649. .enable = at91_ep_enable,
  650. .disable = at91_ep_disable,
  651. .alloc_request = at91_ep_alloc_request,
  652. .free_request = at91_ep_free_request,
  653. .queue = at91_ep_queue,
  654. .dequeue = at91_ep_dequeue,
  655. .set_halt = at91_ep_set_halt,
  656. /* there's only imprecise fifo status reporting */
  657. };
  658. /*-------------------------------------------------------------------------*/
  659. static int at91_get_frame(struct usb_gadget *gadget)
  660. {
  661. struct at91_udc *udc = to_udc(gadget);
  662. if (!to_udc(gadget)->clocked)
  663. return -EINVAL;
  664. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  665. }
  666. static int at91_wakeup(struct usb_gadget *gadget)
  667. {
  668. struct at91_udc *udc = to_udc(gadget);
  669. u32 glbstate;
  670. int status = -EINVAL;
  671. unsigned long flags;
  672. DBG("%s\n", __func__ );
  673. spin_lock_irqsave(&udc->lock, flags);
  674. if (!udc->clocked || !udc->suspended)
  675. goto done;
  676. /* NOTE: some "early versions" handle ESR differently ... */
  677. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  678. if (!(glbstate & AT91_UDP_ESR))
  679. goto done;
  680. glbstate |= AT91_UDP_ESR;
  681. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  682. done:
  683. spin_unlock_irqrestore(&udc->lock, flags);
  684. return status;
  685. }
  686. /* reinit == restore initial software state */
  687. static void udc_reinit(struct at91_udc *udc)
  688. {
  689. u32 i;
  690. INIT_LIST_HEAD(&udc->gadget.ep_list);
  691. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  692. for (i = 0; i < NUM_ENDPOINTS; i++) {
  693. struct at91_ep *ep = &udc->ep[i];
  694. if (i != 0)
  695. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  696. ep->ep.desc = NULL;
  697. ep->stopped = 0;
  698. ep->fifo_bank = 0;
  699. usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
  700. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  701. /* initialize one queue per endpoint */
  702. INIT_LIST_HEAD(&ep->queue);
  703. }
  704. }
  705. static void reset_gadget(struct at91_udc *udc)
  706. {
  707. struct usb_gadget_driver *driver = udc->driver;
  708. int i;
  709. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  710. driver = NULL;
  711. udc->gadget.speed = USB_SPEED_UNKNOWN;
  712. udc->suspended = 0;
  713. for (i = 0; i < NUM_ENDPOINTS; i++) {
  714. struct at91_ep *ep = &udc->ep[i];
  715. ep->stopped = 1;
  716. nuke(ep, -ESHUTDOWN);
  717. }
  718. if (driver) {
  719. spin_unlock(&udc->lock);
  720. usb_gadget_udc_reset(&udc->gadget, driver);
  721. spin_lock(&udc->lock);
  722. }
  723. udc_reinit(udc);
  724. }
  725. static void stop_activity(struct at91_udc *udc)
  726. {
  727. struct usb_gadget_driver *driver = udc->driver;
  728. int i;
  729. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  730. driver = NULL;
  731. udc->gadget.speed = USB_SPEED_UNKNOWN;
  732. udc->suspended = 0;
  733. for (i = 0; i < NUM_ENDPOINTS; i++) {
  734. struct at91_ep *ep = &udc->ep[i];
  735. ep->stopped = 1;
  736. nuke(ep, -ESHUTDOWN);
  737. }
  738. if (driver) {
  739. spin_unlock(&udc->lock);
  740. driver->disconnect(&udc->gadget);
  741. spin_lock(&udc->lock);
  742. }
  743. udc_reinit(udc);
  744. }
  745. static void clk_on(struct at91_udc *udc)
  746. {
  747. if (udc->clocked)
  748. return;
  749. udc->clocked = 1;
  750. if (IS_ENABLED(CONFIG_COMMON_CLK))
  751. clk_enable(udc->uclk);
  752. clk_enable(udc->iclk);
  753. clk_enable(udc->fclk);
  754. }
  755. static void clk_off(struct at91_udc *udc)
  756. {
  757. if (!udc->clocked)
  758. return;
  759. udc->clocked = 0;
  760. udc->gadget.speed = USB_SPEED_UNKNOWN;
  761. clk_disable(udc->fclk);
  762. clk_disable(udc->iclk);
  763. if (IS_ENABLED(CONFIG_COMMON_CLK))
  764. clk_disable(udc->uclk);
  765. }
  766. /*
  767. * activate/deactivate link with host; minimize power usage for
  768. * inactive links by cutting clocks and transceiver power.
  769. */
  770. static void pullup(struct at91_udc *udc, int is_on)
  771. {
  772. int active = !udc->board.pullup_active_low;
  773. if (!udc->enabled || !udc->vbus)
  774. is_on = 0;
  775. DBG("%sactive\n", is_on ? "" : "in");
  776. if (is_on) {
  777. clk_on(udc);
  778. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  779. at91_udp_write(udc, AT91_UDP_TXVC, 0);
  780. if (cpu_is_at91rm9200())
  781. gpio_set_value(udc->board.pullup_pin, active);
  782. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
  783. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  784. txvc |= AT91_UDP_TXVC_PUON;
  785. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  786. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  787. u32 usbpucr;
  788. usbpucr = at91_matrix_read(AT91_MATRIX_USBPUCR);
  789. usbpucr |= AT91_MATRIX_USBPUCR_PUON;
  790. at91_matrix_write(AT91_MATRIX_USBPUCR, usbpucr);
  791. }
  792. } else {
  793. stop_activity(udc);
  794. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  795. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  796. if (cpu_is_at91rm9200())
  797. gpio_set_value(udc->board.pullup_pin, !active);
  798. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
  799. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  800. txvc &= ~AT91_UDP_TXVC_PUON;
  801. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  802. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  803. u32 usbpucr;
  804. usbpucr = at91_matrix_read(AT91_MATRIX_USBPUCR);
  805. usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
  806. at91_matrix_write(AT91_MATRIX_USBPUCR, usbpucr);
  807. }
  808. clk_off(udc);
  809. }
  810. }
  811. /* vbus is here! turn everything on that's ready */
  812. static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
  813. {
  814. struct at91_udc *udc = to_udc(gadget);
  815. unsigned long flags;
  816. /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
  817. spin_lock_irqsave(&udc->lock, flags);
  818. udc->vbus = (is_active != 0);
  819. if (udc->driver)
  820. pullup(udc, is_active);
  821. else
  822. pullup(udc, 0);
  823. spin_unlock_irqrestore(&udc->lock, flags);
  824. return 0;
  825. }
  826. static int at91_pullup(struct usb_gadget *gadget, int is_on)
  827. {
  828. struct at91_udc *udc = to_udc(gadget);
  829. unsigned long flags;
  830. spin_lock_irqsave(&udc->lock, flags);
  831. udc->enabled = is_on = !!is_on;
  832. pullup(udc, is_on);
  833. spin_unlock_irqrestore(&udc->lock, flags);
  834. return 0;
  835. }
  836. static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
  837. {
  838. struct at91_udc *udc = to_udc(gadget);
  839. unsigned long flags;
  840. spin_lock_irqsave(&udc->lock, flags);
  841. udc->selfpowered = (is_on != 0);
  842. spin_unlock_irqrestore(&udc->lock, flags);
  843. return 0;
  844. }
  845. static int at91_start(struct usb_gadget *gadget,
  846. struct usb_gadget_driver *driver);
  847. static int at91_stop(struct usb_gadget *gadget);
  848. static const struct usb_gadget_ops at91_udc_ops = {
  849. .get_frame = at91_get_frame,
  850. .wakeup = at91_wakeup,
  851. .set_selfpowered = at91_set_selfpowered,
  852. .vbus_session = at91_vbus_session,
  853. .pullup = at91_pullup,
  854. .udc_start = at91_start,
  855. .udc_stop = at91_stop,
  856. /*
  857. * VBUS-powered devices may also also want to support bigger
  858. * power budgets after an appropriate SET_CONFIGURATION.
  859. */
  860. /* .vbus_power = at91_vbus_power, */
  861. };
  862. /*-------------------------------------------------------------------------*/
  863. static int handle_ep(struct at91_ep *ep)
  864. {
  865. struct at91_request *req;
  866. u32 __iomem *creg = ep->creg;
  867. u32 csr = __raw_readl(creg);
  868. if (!list_empty(&ep->queue))
  869. req = list_entry(ep->queue.next,
  870. struct at91_request, queue);
  871. else
  872. req = NULL;
  873. if (ep->is_in) {
  874. if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
  875. csr |= CLR_FX;
  876. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
  877. __raw_writel(csr, creg);
  878. }
  879. if (req)
  880. return write_fifo(ep, req);
  881. } else {
  882. if (csr & AT91_UDP_STALLSENT) {
  883. /* STALLSENT bit == ISOERR */
  884. if (ep->is_iso && req)
  885. req->req.status = -EILSEQ;
  886. csr |= CLR_FX;
  887. csr &= ~(SET_FX | AT91_UDP_STALLSENT);
  888. __raw_writel(csr, creg);
  889. csr = __raw_readl(creg);
  890. }
  891. if (req && (csr & RX_DATA_READY))
  892. return read_fifo(ep, req);
  893. }
  894. return 0;
  895. }
  896. union setup {
  897. u8 raw[8];
  898. struct usb_ctrlrequest r;
  899. };
  900. static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
  901. {
  902. u32 __iomem *creg = ep->creg;
  903. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  904. unsigned rxcount, i = 0;
  905. u32 tmp;
  906. union setup pkt;
  907. int status = 0;
  908. /* read and ack SETUP; hard-fail for bogus packets */
  909. rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
  910. if (likely(rxcount == 8)) {
  911. while (rxcount--)
  912. pkt.raw[i++] = __raw_readb(dreg);
  913. if (pkt.r.bRequestType & USB_DIR_IN) {
  914. csr |= AT91_UDP_DIR;
  915. ep->is_in = 1;
  916. } else {
  917. csr &= ~AT91_UDP_DIR;
  918. ep->is_in = 0;
  919. }
  920. } else {
  921. /* REVISIT this happens sometimes under load; why?? */
  922. ERR("SETUP len %d, csr %08x\n", rxcount, csr);
  923. status = -EINVAL;
  924. }
  925. csr |= CLR_FX;
  926. csr &= ~(SET_FX | AT91_UDP_RXSETUP);
  927. __raw_writel(csr, creg);
  928. udc->wait_for_addr_ack = 0;
  929. udc->wait_for_config_ack = 0;
  930. ep->stopped = 0;
  931. if (unlikely(status != 0))
  932. goto stall;
  933. #define w_index le16_to_cpu(pkt.r.wIndex)
  934. #define w_value le16_to_cpu(pkt.r.wValue)
  935. #define w_length le16_to_cpu(pkt.r.wLength)
  936. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  937. pkt.r.bRequestType, pkt.r.bRequest,
  938. w_value, w_index, w_length);
  939. /*
  940. * A few standard requests get handled here, ones that touch
  941. * hardware ... notably for device and endpoint features.
  942. */
  943. udc->req_pending = 1;
  944. csr = __raw_readl(creg);
  945. csr |= CLR_FX;
  946. csr &= ~SET_FX;
  947. switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
  948. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  949. | USB_REQ_SET_ADDRESS:
  950. __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
  951. udc->addr = w_value;
  952. udc->wait_for_addr_ack = 1;
  953. udc->req_pending = 0;
  954. /* FADDR is set later, when we ack host STATUS */
  955. return;
  956. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  957. | USB_REQ_SET_CONFIGURATION:
  958. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
  959. if (pkt.r.wValue)
  960. udc->wait_for_config_ack = (tmp == 0);
  961. else
  962. udc->wait_for_config_ack = (tmp != 0);
  963. if (udc->wait_for_config_ack)
  964. VDBG("wait for config\n");
  965. /* CONFG is toggled later, if gadget driver succeeds */
  966. break;
  967. /*
  968. * Hosts may set or clear remote wakeup status, and
  969. * devices may report they're VBUS powered.
  970. */
  971. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  972. | USB_REQ_GET_STATUS:
  973. tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  974. if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
  975. tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  976. PACKET("get device status\n");
  977. __raw_writeb(tmp, dreg);
  978. __raw_writeb(0, dreg);
  979. goto write_in;
  980. /* then STATUS starts later, automatically */
  981. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  982. | USB_REQ_SET_FEATURE:
  983. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  984. goto stall;
  985. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  986. tmp |= AT91_UDP_ESR;
  987. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  988. goto succeed;
  989. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  990. | USB_REQ_CLEAR_FEATURE:
  991. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  992. goto stall;
  993. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  994. tmp &= ~AT91_UDP_ESR;
  995. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  996. goto succeed;
  997. /*
  998. * Interfaces have no feature settings; this is pretty useless.
  999. * we won't even insist the interface exists...
  1000. */
  1001. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  1002. | USB_REQ_GET_STATUS:
  1003. PACKET("get interface status\n");
  1004. __raw_writeb(0, dreg);
  1005. __raw_writeb(0, dreg);
  1006. goto write_in;
  1007. /* then STATUS starts later, automatically */
  1008. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  1009. | USB_REQ_SET_FEATURE:
  1010. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  1011. | USB_REQ_CLEAR_FEATURE:
  1012. goto stall;
  1013. /*
  1014. * Hosts may clear bulk/intr endpoint halt after the gadget
  1015. * driver sets it (not widely used); or set it (for testing)
  1016. */
  1017. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1018. | USB_REQ_GET_STATUS:
  1019. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1020. ep = &udc->ep[tmp];
  1021. if (tmp >= NUM_ENDPOINTS || (tmp && !ep->ep.desc))
  1022. goto stall;
  1023. if (tmp) {
  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. }
  1030. PACKET("get %s status\n", ep->ep.name);
  1031. if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
  1032. tmp = (1 << USB_ENDPOINT_HALT);
  1033. else
  1034. tmp = 0;
  1035. __raw_writeb(tmp, dreg);
  1036. __raw_writeb(0, dreg);
  1037. goto write_in;
  1038. /* then STATUS starts later, automatically */
  1039. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1040. | USB_REQ_SET_FEATURE:
  1041. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1042. ep = &udc->ep[tmp];
  1043. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1044. goto stall;
  1045. if (!ep->ep.desc || ep->is_iso)
  1046. goto stall;
  1047. if ((w_index & USB_DIR_IN)) {
  1048. if (!ep->is_in)
  1049. goto stall;
  1050. } else if (ep->is_in)
  1051. goto stall;
  1052. tmp = __raw_readl(ep->creg);
  1053. tmp &= ~SET_FX;
  1054. tmp |= CLR_FX | AT91_UDP_FORCESTALL;
  1055. __raw_writel(tmp, ep->creg);
  1056. goto succeed;
  1057. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1058. | USB_REQ_CLEAR_FEATURE:
  1059. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1060. ep = &udc->ep[tmp];
  1061. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1062. goto stall;
  1063. if (tmp == 0)
  1064. goto succeed;
  1065. if (!ep->ep.desc || ep->is_iso)
  1066. goto stall;
  1067. if ((w_index & USB_DIR_IN)) {
  1068. if (!ep->is_in)
  1069. goto stall;
  1070. } else if (ep->is_in)
  1071. goto stall;
  1072. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  1073. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  1074. tmp = __raw_readl(ep->creg);
  1075. tmp |= CLR_FX;
  1076. tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
  1077. __raw_writel(tmp, ep->creg);
  1078. if (!list_empty(&ep->queue))
  1079. handle_ep(ep);
  1080. goto succeed;
  1081. }
  1082. #undef w_value
  1083. #undef w_index
  1084. #undef w_length
  1085. /* pass request up to the gadget driver */
  1086. if (udc->driver) {
  1087. spin_unlock(&udc->lock);
  1088. status = udc->driver->setup(&udc->gadget, &pkt.r);
  1089. spin_lock(&udc->lock);
  1090. }
  1091. else
  1092. status = -ENODEV;
  1093. if (status < 0) {
  1094. stall:
  1095. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1096. pkt.r.bRequestType, pkt.r.bRequest, status);
  1097. csr |= AT91_UDP_FORCESTALL;
  1098. __raw_writel(csr, creg);
  1099. udc->req_pending = 0;
  1100. }
  1101. return;
  1102. succeed:
  1103. /* immediate successful (IN) STATUS after zero length DATA */
  1104. PACKET("ep0 in/status\n");
  1105. write_in:
  1106. csr |= AT91_UDP_TXPKTRDY;
  1107. __raw_writel(csr, creg);
  1108. udc->req_pending = 0;
  1109. }
  1110. static void handle_ep0(struct at91_udc *udc)
  1111. {
  1112. struct at91_ep *ep0 = &udc->ep[0];
  1113. u32 __iomem *creg = ep0->creg;
  1114. u32 csr = __raw_readl(creg);
  1115. struct at91_request *req;
  1116. if (unlikely(csr & AT91_UDP_STALLSENT)) {
  1117. nuke(ep0, -EPROTO);
  1118. udc->req_pending = 0;
  1119. csr |= CLR_FX;
  1120. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
  1121. __raw_writel(csr, creg);
  1122. VDBG("ep0 stalled\n");
  1123. csr = __raw_readl(creg);
  1124. }
  1125. if (csr & AT91_UDP_RXSETUP) {
  1126. nuke(ep0, 0);
  1127. udc->req_pending = 0;
  1128. handle_setup(udc, ep0, csr);
  1129. return;
  1130. }
  1131. if (list_empty(&ep0->queue))
  1132. req = NULL;
  1133. else
  1134. req = list_entry(ep0->queue.next, struct at91_request, queue);
  1135. /* host ACKed an IN packet that we sent */
  1136. if (csr & AT91_UDP_TXCOMP) {
  1137. csr |= CLR_FX;
  1138. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  1139. /* write more IN DATA? */
  1140. if (req && ep0->is_in) {
  1141. if (handle_ep(ep0))
  1142. udc->req_pending = 0;
  1143. /*
  1144. * Ack after:
  1145. * - last IN DATA packet (including GET_STATUS)
  1146. * - IN/STATUS for OUT DATA
  1147. * - IN/STATUS for any zero-length DATA stage
  1148. * except for the IN DATA case, the host should send
  1149. * an OUT status later, which we'll ack.
  1150. */
  1151. } else {
  1152. udc->req_pending = 0;
  1153. __raw_writel(csr, creg);
  1154. /*
  1155. * SET_ADDRESS takes effect only after the STATUS
  1156. * (to the original address) gets acked.
  1157. */
  1158. if (udc->wait_for_addr_ack) {
  1159. u32 tmp;
  1160. at91_udp_write(udc, AT91_UDP_FADDR,
  1161. AT91_UDP_FEN | udc->addr);
  1162. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  1163. tmp &= ~AT91_UDP_FADDEN;
  1164. if (udc->addr)
  1165. tmp |= AT91_UDP_FADDEN;
  1166. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  1167. udc->wait_for_addr_ack = 0;
  1168. VDBG("address %d\n", udc->addr);
  1169. }
  1170. }
  1171. }
  1172. /* OUT packet arrived ... */
  1173. else if (csr & AT91_UDP_RX_DATA_BK0) {
  1174. csr |= CLR_FX;
  1175. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  1176. /* OUT DATA stage */
  1177. if (!ep0->is_in) {
  1178. if (req) {
  1179. if (handle_ep(ep0)) {
  1180. /* send IN/STATUS */
  1181. PACKET("ep0 in/status\n");
  1182. csr = __raw_readl(creg);
  1183. csr &= ~SET_FX;
  1184. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  1185. __raw_writel(csr, creg);
  1186. udc->req_pending = 0;
  1187. }
  1188. } else if (udc->req_pending) {
  1189. /*
  1190. * AT91 hardware has a hard time with this
  1191. * "deferred response" mode for control-OUT
  1192. * transfers. (For control-IN it's fine.)
  1193. *
  1194. * The normal solution leaves OUT data in the
  1195. * fifo until the gadget driver is ready.
  1196. * We couldn't do that here without disabling
  1197. * the IRQ that tells about SETUP packets,
  1198. * e.g. when the host gets impatient...
  1199. *
  1200. * Working around it by copying into a buffer
  1201. * would almost be a non-deferred response,
  1202. * except that it wouldn't permit reliable
  1203. * stalling of the request. Instead, demand
  1204. * that gadget drivers not use this mode.
  1205. */
  1206. DBG("no control-OUT deferred responses!\n");
  1207. __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
  1208. udc->req_pending = 0;
  1209. }
  1210. /* STATUS stage for control-IN; ack. */
  1211. } else {
  1212. PACKET("ep0 out/status ACK\n");
  1213. __raw_writel(csr, creg);
  1214. /* "early" status stage */
  1215. if (req)
  1216. done(ep0, req, 0);
  1217. }
  1218. }
  1219. }
  1220. static irqreturn_t at91_udc_irq (int irq, void *_udc)
  1221. {
  1222. struct at91_udc *udc = _udc;
  1223. u32 rescans = 5;
  1224. int disable_clock = 0;
  1225. unsigned long flags;
  1226. spin_lock_irqsave(&udc->lock, flags);
  1227. if (!udc->clocked) {
  1228. clk_on(udc);
  1229. disable_clock = 1;
  1230. }
  1231. while (rescans--) {
  1232. u32 status;
  1233. status = at91_udp_read(udc, AT91_UDP_ISR)
  1234. & at91_udp_read(udc, AT91_UDP_IMR);
  1235. if (!status)
  1236. break;
  1237. /* USB reset irq: not maskable */
  1238. if (status & AT91_UDP_ENDBUSRES) {
  1239. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1240. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1241. /* Atmel code clears this irq twice */
  1242. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1243. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1244. VDBG("end bus reset\n");
  1245. udc->addr = 0;
  1246. reset_gadget(udc);
  1247. /* enable ep0 */
  1248. at91_udp_write(udc, AT91_UDP_CSR(0),
  1249. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1250. udc->gadget.speed = USB_SPEED_FULL;
  1251. udc->suspended = 0;
  1252. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1253. /*
  1254. * NOTE: this driver keeps clocks off unless the
  1255. * USB host is present. That saves power, but for
  1256. * boards that don't support VBUS detection, both
  1257. * clocks need to be active most of the time.
  1258. */
  1259. /* host initiated suspend (3+ms bus idle) */
  1260. } else if (status & AT91_UDP_RXSUSP) {
  1261. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1262. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1263. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1264. /* VDBG("bus suspend\n"); */
  1265. if (udc->suspended)
  1266. continue;
  1267. udc->suspended = 1;
  1268. /*
  1269. * NOTE: when suspending a VBUS-powered device, the
  1270. * gadget driver should switch into slow clock mode
  1271. * and then into standby to avoid drawing more than
  1272. * 500uA power (2500uA for some high-power configs).
  1273. */
  1274. if (udc->driver && udc->driver->suspend) {
  1275. spin_unlock(&udc->lock);
  1276. udc->driver->suspend(&udc->gadget);
  1277. spin_lock(&udc->lock);
  1278. }
  1279. /* host initiated resume */
  1280. } else if (status & AT91_UDP_RXRSM) {
  1281. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1282. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1283. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1284. /* VDBG("bus resume\n"); */
  1285. if (!udc->suspended)
  1286. continue;
  1287. udc->suspended = 0;
  1288. /*
  1289. * NOTE: for a VBUS-powered device, the gadget driver
  1290. * would normally want to switch out of slow clock
  1291. * mode into normal mode.
  1292. */
  1293. if (udc->driver && udc->driver->resume) {
  1294. spin_unlock(&udc->lock);
  1295. udc->driver->resume(&udc->gadget);
  1296. spin_lock(&udc->lock);
  1297. }
  1298. /* endpoint IRQs are cleared by handling them */
  1299. } else {
  1300. int i;
  1301. unsigned mask = 1;
  1302. struct at91_ep *ep = &udc->ep[1];
  1303. if (status & mask)
  1304. handle_ep0(udc);
  1305. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1306. mask <<= 1;
  1307. if (status & mask)
  1308. handle_ep(ep);
  1309. ep++;
  1310. }
  1311. }
  1312. }
  1313. if (disable_clock)
  1314. clk_off(udc);
  1315. spin_unlock_irqrestore(&udc->lock, flags);
  1316. return IRQ_HANDLED;
  1317. }
  1318. /*-------------------------------------------------------------------------*/
  1319. static struct at91_udc controller = {
  1320. .gadget = {
  1321. .ops = &at91_udc_ops,
  1322. .ep0 = &controller.ep[0].ep,
  1323. .name = driver_name,
  1324. },
  1325. .ep[0] = {
  1326. .ep = {
  1327. .name = ep0name,
  1328. .ops = &at91_ep_ops,
  1329. },
  1330. .udc = &controller,
  1331. .maxpacket = 8,
  1332. .int_mask = 1 << 0,
  1333. },
  1334. .ep[1] = {
  1335. .ep = {
  1336. .name = "ep1",
  1337. .ops = &at91_ep_ops,
  1338. },
  1339. .udc = &controller,
  1340. .is_pingpong = 1,
  1341. .maxpacket = 64,
  1342. .int_mask = 1 << 1,
  1343. },
  1344. .ep[2] = {
  1345. .ep = {
  1346. .name = "ep2",
  1347. .ops = &at91_ep_ops,
  1348. },
  1349. .udc = &controller,
  1350. .is_pingpong = 1,
  1351. .maxpacket = 64,
  1352. .int_mask = 1 << 2,
  1353. },
  1354. .ep[3] = {
  1355. .ep = {
  1356. /* could actually do bulk too */
  1357. .name = "ep3-int",
  1358. .ops = &at91_ep_ops,
  1359. },
  1360. .udc = &controller,
  1361. .maxpacket = 8,
  1362. .int_mask = 1 << 3,
  1363. },
  1364. .ep[4] = {
  1365. .ep = {
  1366. .name = "ep4",
  1367. .ops = &at91_ep_ops,
  1368. },
  1369. .udc = &controller,
  1370. .is_pingpong = 1,
  1371. .maxpacket = 256,
  1372. .int_mask = 1 << 4,
  1373. },
  1374. .ep[5] = {
  1375. .ep = {
  1376. .name = "ep5",
  1377. .ops = &at91_ep_ops,
  1378. },
  1379. .udc = &controller,
  1380. .is_pingpong = 1,
  1381. .maxpacket = 256,
  1382. .int_mask = 1 << 5,
  1383. },
  1384. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1385. };
  1386. static void at91_vbus_update(struct at91_udc *udc, unsigned value)
  1387. {
  1388. value ^= udc->board.vbus_active_low;
  1389. if (value != udc->vbus)
  1390. at91_vbus_session(&udc->gadget, value);
  1391. }
  1392. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1393. {
  1394. struct at91_udc *udc = _udc;
  1395. /* vbus needs at least brief debouncing */
  1396. udelay(10);
  1397. at91_vbus_update(udc, gpio_get_value(udc->board.vbus_pin));
  1398. return IRQ_HANDLED;
  1399. }
  1400. static void at91_vbus_timer_work(struct work_struct *work)
  1401. {
  1402. struct at91_udc *udc = container_of(work, struct at91_udc,
  1403. vbus_timer_work);
  1404. at91_vbus_update(udc, gpio_get_value_cansleep(udc->board.vbus_pin));
  1405. if (!timer_pending(&udc->vbus_timer))
  1406. mod_timer(&udc->vbus_timer, jiffies + VBUS_POLL_TIMEOUT);
  1407. }
  1408. static void at91_vbus_timer(unsigned long data)
  1409. {
  1410. struct at91_udc *udc = (struct at91_udc *)data;
  1411. /*
  1412. * If we are polling vbus it is likely that the gpio is on an
  1413. * bus such as i2c or spi which may sleep, so schedule some work
  1414. * to read the vbus gpio
  1415. */
  1416. schedule_work(&udc->vbus_timer_work);
  1417. }
  1418. static int at91_start(struct usb_gadget *gadget,
  1419. struct usb_gadget_driver *driver)
  1420. {
  1421. struct at91_udc *udc;
  1422. udc = container_of(gadget, struct at91_udc, gadget);
  1423. udc->driver = driver;
  1424. udc->gadget.dev.of_node = udc->pdev->dev.of_node;
  1425. udc->enabled = 1;
  1426. udc->selfpowered = 1;
  1427. return 0;
  1428. }
  1429. static int at91_stop(struct usb_gadget *gadget)
  1430. {
  1431. struct at91_udc *udc;
  1432. unsigned long flags;
  1433. udc = container_of(gadget, struct at91_udc, gadget);
  1434. spin_lock_irqsave(&udc->lock, flags);
  1435. udc->enabled = 0;
  1436. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1437. spin_unlock_irqrestore(&udc->lock, flags);
  1438. udc->driver = NULL;
  1439. return 0;
  1440. }
  1441. /*-------------------------------------------------------------------------*/
  1442. static void at91udc_shutdown(struct platform_device *dev)
  1443. {
  1444. struct at91_udc *udc = platform_get_drvdata(dev);
  1445. unsigned long flags;
  1446. /* force disconnect on reboot */
  1447. spin_lock_irqsave(&udc->lock, flags);
  1448. pullup(platform_get_drvdata(dev), 0);
  1449. spin_unlock_irqrestore(&udc->lock, flags);
  1450. }
  1451. static void at91udc_of_init(struct at91_udc *udc,
  1452. struct device_node *np)
  1453. {
  1454. struct at91_udc_data *board = &udc->board;
  1455. u32 val;
  1456. enum of_gpio_flags flags;
  1457. if (of_property_read_u32(np, "atmel,vbus-polled", &val) == 0)
  1458. board->vbus_polled = 1;
  1459. board->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0,
  1460. &flags);
  1461. board->vbus_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
  1462. board->pullup_pin = of_get_named_gpio_flags(np, "atmel,pullup-gpio", 0,
  1463. &flags);
  1464. board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
  1465. }
  1466. static int at91udc_probe(struct platform_device *pdev)
  1467. {
  1468. struct device *dev = &pdev->dev;
  1469. struct at91_udc *udc;
  1470. int retval;
  1471. struct resource *res;
  1472. if (!dev_get_platdata(dev) && !pdev->dev.of_node) {
  1473. /* small (so we copy it) but critical! */
  1474. DBG("missing platform_data\n");
  1475. return -ENODEV;
  1476. }
  1477. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1478. if (!res)
  1479. return -ENXIO;
  1480. if (!request_mem_region(res->start, resource_size(res), driver_name)) {
  1481. DBG("someone's using UDC memory\n");
  1482. return -EBUSY;
  1483. }
  1484. /* init software state */
  1485. udc = &controller;
  1486. udc->gadget.dev.parent = dev;
  1487. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
  1488. at91udc_of_init(udc, pdev->dev.of_node);
  1489. else
  1490. memcpy(&udc->board, dev_get_platdata(dev),
  1491. sizeof(struct at91_udc_data));
  1492. udc->pdev = pdev;
  1493. udc->enabled = 0;
  1494. spin_lock_init(&udc->lock);
  1495. /* rm9200 needs manual D+ pullup; off by default */
  1496. if (cpu_is_at91rm9200()) {
  1497. if (!gpio_is_valid(udc->board.pullup_pin)) {
  1498. DBG("no D+ pullup?\n");
  1499. retval = -ENODEV;
  1500. goto fail0;
  1501. }
  1502. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1503. if (retval) {
  1504. DBG("D+ pullup is busy\n");
  1505. goto fail0;
  1506. }
  1507. gpio_direction_output(udc->board.pullup_pin,
  1508. udc->board.pullup_active_low);
  1509. }
  1510. /* newer chips have more FIFO memory than rm9200 */
  1511. if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
  1512. udc->ep[0].maxpacket = 64;
  1513. udc->ep[3].maxpacket = 64;
  1514. udc->ep[4].maxpacket = 512;
  1515. udc->ep[5].maxpacket = 512;
  1516. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  1517. udc->ep[3].maxpacket = 64;
  1518. } else if (cpu_is_at91sam9263()) {
  1519. udc->ep[0].maxpacket = 64;
  1520. udc->ep[3].maxpacket = 64;
  1521. }
  1522. udc->udp_baseaddr = ioremap(res->start, resource_size(res));
  1523. if (!udc->udp_baseaddr) {
  1524. retval = -ENOMEM;
  1525. goto fail0a;
  1526. }
  1527. udc_reinit(udc);
  1528. /* get interface and function clocks */
  1529. udc->iclk = clk_get(dev, "udc_clk");
  1530. udc->fclk = clk_get(dev, "udpck");
  1531. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1532. udc->uclk = clk_get(dev, "usb_clk");
  1533. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk) ||
  1534. (IS_ENABLED(CONFIG_COMMON_CLK) && IS_ERR(udc->uclk))) {
  1535. DBG("clocks missing\n");
  1536. retval = -ENODEV;
  1537. goto fail1;
  1538. }
  1539. /* don't do anything until we have both gadget driver and VBUS */
  1540. if (IS_ENABLED(CONFIG_COMMON_CLK)) {
  1541. clk_set_rate(udc->uclk, 48000000);
  1542. retval = clk_prepare(udc->uclk);
  1543. if (retval)
  1544. goto fail1;
  1545. }
  1546. retval = clk_prepare(udc->fclk);
  1547. if (retval)
  1548. goto fail1a;
  1549. retval = clk_prepare_enable(udc->iclk);
  1550. if (retval)
  1551. goto fail1b;
  1552. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1553. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1554. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1555. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1556. clk_disable(udc->iclk);
  1557. /* request UDC and maybe VBUS irqs */
  1558. udc->udp_irq = platform_get_irq(pdev, 0);
  1559. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1560. 0, driver_name, udc);
  1561. if (retval < 0) {
  1562. DBG("request irq %d failed\n", udc->udp_irq);
  1563. goto fail1c;
  1564. }
  1565. if (gpio_is_valid(udc->board.vbus_pin)) {
  1566. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1567. if (retval < 0) {
  1568. DBG("request vbus pin failed\n");
  1569. goto fail2;
  1570. }
  1571. gpio_direction_input(udc->board.vbus_pin);
  1572. /*
  1573. * Get the initial state of VBUS - we cannot expect
  1574. * a pending interrupt.
  1575. */
  1576. udc->vbus = gpio_get_value_cansleep(udc->board.vbus_pin) ^
  1577. udc->board.vbus_active_low;
  1578. if (udc->board.vbus_polled) {
  1579. INIT_WORK(&udc->vbus_timer_work, at91_vbus_timer_work);
  1580. setup_timer(&udc->vbus_timer, at91_vbus_timer,
  1581. (unsigned long)udc);
  1582. mod_timer(&udc->vbus_timer,
  1583. jiffies + VBUS_POLL_TIMEOUT);
  1584. } else {
  1585. if (request_irq(gpio_to_irq(udc->board.vbus_pin),
  1586. at91_vbus_irq, 0, driver_name, udc)) {
  1587. DBG("request vbus irq %d failed\n",
  1588. udc->board.vbus_pin);
  1589. retval = -EBUSY;
  1590. goto fail3;
  1591. }
  1592. }
  1593. } else {
  1594. DBG("no VBUS detection, assuming always-on\n");
  1595. udc->vbus = 1;
  1596. }
  1597. retval = usb_add_gadget_udc(dev, &udc->gadget);
  1598. if (retval)
  1599. goto fail4;
  1600. dev_set_drvdata(dev, udc);
  1601. device_init_wakeup(dev, 1);
  1602. create_debug_file(udc);
  1603. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1604. return 0;
  1605. fail4:
  1606. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled)
  1607. free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
  1608. fail3:
  1609. if (gpio_is_valid(udc->board.vbus_pin))
  1610. gpio_free(udc->board.vbus_pin);
  1611. fail2:
  1612. free_irq(udc->udp_irq, udc);
  1613. fail1c:
  1614. clk_unprepare(udc->iclk);
  1615. fail1b:
  1616. clk_unprepare(udc->fclk);
  1617. fail1a:
  1618. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1619. clk_unprepare(udc->uclk);
  1620. fail1:
  1621. if (IS_ENABLED(CONFIG_COMMON_CLK) && !IS_ERR(udc->uclk))
  1622. clk_put(udc->uclk);
  1623. if (!IS_ERR(udc->fclk))
  1624. clk_put(udc->fclk);
  1625. if (!IS_ERR(udc->iclk))
  1626. clk_put(udc->iclk);
  1627. iounmap(udc->udp_baseaddr);
  1628. fail0a:
  1629. if (cpu_is_at91rm9200())
  1630. gpio_free(udc->board.pullup_pin);
  1631. fail0:
  1632. release_mem_region(res->start, resource_size(res));
  1633. DBG("%s probe failed, %d\n", driver_name, retval);
  1634. return retval;
  1635. }
  1636. static int __exit at91udc_remove(struct platform_device *pdev)
  1637. {
  1638. struct at91_udc *udc = platform_get_drvdata(pdev);
  1639. struct resource *res;
  1640. unsigned long flags;
  1641. DBG("remove\n");
  1642. usb_del_gadget_udc(&udc->gadget);
  1643. if (udc->driver)
  1644. return -EBUSY;
  1645. spin_lock_irqsave(&udc->lock, flags);
  1646. pullup(udc, 0);
  1647. spin_unlock_irqrestore(&udc->lock, flags);
  1648. device_init_wakeup(&pdev->dev, 0);
  1649. remove_debug_file(udc);
  1650. if (gpio_is_valid(udc->board.vbus_pin)) {
  1651. free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
  1652. gpio_free(udc->board.vbus_pin);
  1653. }
  1654. free_irq(udc->udp_irq, udc);
  1655. iounmap(udc->udp_baseaddr);
  1656. if (cpu_is_at91rm9200())
  1657. gpio_free(udc->board.pullup_pin);
  1658. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1659. release_mem_region(res->start, resource_size(res));
  1660. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1661. clk_unprepare(udc->uclk);
  1662. clk_unprepare(udc->fclk);
  1663. clk_unprepare(udc->iclk);
  1664. clk_put(udc->iclk);
  1665. clk_put(udc->fclk);
  1666. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1667. clk_put(udc->uclk);
  1668. return 0;
  1669. }
  1670. #ifdef CONFIG_PM
  1671. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1672. {
  1673. struct at91_udc *udc = platform_get_drvdata(pdev);
  1674. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1675. unsigned long flags;
  1676. /* Unless we can act normally to the host (letting it wake us up
  1677. * whenever it has work for us) force disconnect. Wakeup requires
  1678. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1679. * tokens) and VBUS irqs (on systems which support them).
  1680. */
  1681. if ((!udc->suspended && udc->addr)
  1682. || !wake
  1683. || at91_suspend_entering_slow_clock()) {
  1684. spin_lock_irqsave(&udc->lock, flags);
  1685. pullup(udc, 0);
  1686. wake = 0;
  1687. spin_unlock_irqrestore(&udc->lock, flags);
  1688. } else
  1689. enable_irq_wake(udc->udp_irq);
  1690. udc->active_suspend = wake;
  1691. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled && wake)
  1692. enable_irq_wake(udc->board.vbus_pin);
  1693. return 0;
  1694. }
  1695. static int at91udc_resume(struct platform_device *pdev)
  1696. {
  1697. struct at91_udc *udc = platform_get_drvdata(pdev);
  1698. unsigned long flags;
  1699. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled &&
  1700. udc->active_suspend)
  1701. disable_irq_wake(udc->board.vbus_pin);
  1702. /* maybe reconnect to host; if so, clocks on */
  1703. if (udc->active_suspend)
  1704. disable_irq_wake(udc->udp_irq);
  1705. else {
  1706. spin_lock_irqsave(&udc->lock, flags);
  1707. pullup(udc, 1);
  1708. spin_unlock_irqrestore(&udc->lock, flags);
  1709. }
  1710. return 0;
  1711. }
  1712. #else
  1713. #define at91udc_suspend NULL
  1714. #define at91udc_resume NULL
  1715. #endif
  1716. #if defined(CONFIG_OF)
  1717. static const struct of_device_id at91_udc_dt_ids[] = {
  1718. { .compatible = "atmel,at91rm9200-udc" },
  1719. { /* sentinel */ }
  1720. };
  1721. MODULE_DEVICE_TABLE(of, at91_udc_dt_ids);
  1722. #endif
  1723. static struct platform_driver at91_udc_driver = {
  1724. .remove = __exit_p(at91udc_remove),
  1725. .shutdown = at91udc_shutdown,
  1726. .suspend = at91udc_suspend,
  1727. .resume = at91udc_resume,
  1728. .driver = {
  1729. .name = (char *) driver_name,
  1730. .of_match_table = of_match_ptr(at91_udc_dt_ids),
  1731. },
  1732. };
  1733. module_platform_driver_probe(at91_udc_driver, at91udc_probe);
  1734. MODULE_DESCRIPTION("AT91 udc driver");
  1735. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1736. MODULE_LICENSE("GPL");
  1737. MODULE_ALIAS("platform:at91_udc");