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