at91_udc.c 50 KB

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