at91_udc.c 51 KB

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