at91_udc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1472. if (!res)
  1473. return -ENXIO;
  1474. if (!request_mem_region(res->start, resource_size(res), driver_name)) {
  1475. DBG("someone's using UDC memory\n");
  1476. return -EBUSY;
  1477. }
  1478. /* init software state */
  1479. udc = &controller;
  1480. udc->gadget.dev.parent = dev;
  1481. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
  1482. at91udc_of_init(udc, pdev->dev.of_node);
  1483. else
  1484. memcpy(&udc->board, dev_get_platdata(dev),
  1485. sizeof(struct at91_udc_data));
  1486. udc->pdev = pdev;
  1487. udc->enabled = 0;
  1488. spin_lock_init(&udc->lock);
  1489. /* rm9200 needs manual D+ pullup; off by default */
  1490. if (cpu_is_at91rm9200()) {
  1491. if (!gpio_is_valid(udc->board.pullup_pin)) {
  1492. DBG("no D+ pullup?\n");
  1493. retval = -ENODEV;
  1494. goto fail0;
  1495. }
  1496. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1497. if (retval) {
  1498. DBG("D+ pullup is busy\n");
  1499. goto fail0;
  1500. }
  1501. gpio_direction_output(udc->board.pullup_pin,
  1502. udc->board.pullup_active_low);
  1503. }
  1504. /* newer chips have more FIFO memory than rm9200 */
  1505. if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
  1506. udc->ep[0].maxpacket = 64;
  1507. udc->ep[3].maxpacket = 64;
  1508. udc->ep[4].maxpacket = 512;
  1509. udc->ep[5].maxpacket = 512;
  1510. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  1511. udc->ep[3].maxpacket = 64;
  1512. } else if (cpu_is_at91sam9263()) {
  1513. udc->ep[0].maxpacket = 64;
  1514. udc->ep[3].maxpacket = 64;
  1515. }
  1516. udc->udp_baseaddr = ioremap(res->start, resource_size(res));
  1517. if (!udc->udp_baseaddr) {
  1518. retval = -ENOMEM;
  1519. goto fail0a;
  1520. }
  1521. udc_reinit(udc);
  1522. /* get interface and function clocks */
  1523. udc->iclk = clk_get(dev, "udc_clk");
  1524. udc->fclk = clk_get(dev, "udpck");
  1525. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1526. udc->uclk = clk_get(dev, "usb_clk");
  1527. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk) ||
  1528. (IS_ENABLED(CONFIG_COMMON_CLK) && IS_ERR(udc->uclk))) {
  1529. DBG("clocks missing\n");
  1530. retval = -ENODEV;
  1531. goto fail1;
  1532. }
  1533. /* don't do anything until we have both gadget driver and VBUS */
  1534. retval = clk_prepare_enable(udc->iclk);
  1535. if (retval)
  1536. goto fail1;
  1537. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1538. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1539. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1540. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1541. clk_disable_unprepare(udc->iclk);
  1542. /* request UDC and maybe VBUS irqs */
  1543. udc->udp_irq = platform_get_irq(pdev, 0);
  1544. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1545. 0, driver_name, udc);
  1546. if (retval < 0) {
  1547. DBG("request irq %d failed\n", udc->udp_irq);
  1548. goto fail1;
  1549. }
  1550. if (gpio_is_valid(udc->board.vbus_pin)) {
  1551. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1552. if (retval < 0) {
  1553. DBG("request vbus pin failed\n");
  1554. goto fail2;
  1555. }
  1556. gpio_direction_input(udc->board.vbus_pin);
  1557. /*
  1558. * Get the initial state of VBUS - we cannot expect
  1559. * a pending interrupt.
  1560. */
  1561. udc->vbus = gpio_get_value_cansleep(udc->board.vbus_pin) ^
  1562. udc->board.vbus_active_low;
  1563. if (udc->board.vbus_polled) {
  1564. INIT_WORK(&udc->vbus_timer_work, at91_vbus_timer_work);
  1565. setup_timer(&udc->vbus_timer, at91_vbus_timer,
  1566. (unsigned long)udc);
  1567. mod_timer(&udc->vbus_timer,
  1568. jiffies + VBUS_POLL_TIMEOUT);
  1569. } else {
  1570. if (request_irq(gpio_to_irq(udc->board.vbus_pin),
  1571. at91_vbus_irq, 0, driver_name, udc)) {
  1572. DBG("request vbus irq %d failed\n",
  1573. udc->board.vbus_pin);
  1574. retval = -EBUSY;
  1575. goto fail3;
  1576. }
  1577. }
  1578. } else {
  1579. DBG("no VBUS detection, assuming always-on\n");
  1580. udc->vbus = 1;
  1581. }
  1582. retval = usb_add_gadget_udc(dev, &udc->gadget);
  1583. if (retval)
  1584. goto fail4;
  1585. dev_set_drvdata(dev, udc);
  1586. device_init_wakeup(dev, 1);
  1587. create_debug_file(udc);
  1588. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1589. return 0;
  1590. fail4:
  1591. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled)
  1592. free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
  1593. fail3:
  1594. if (gpio_is_valid(udc->board.vbus_pin))
  1595. gpio_free(udc->board.vbus_pin);
  1596. fail2:
  1597. free_irq(udc->udp_irq, udc);
  1598. fail1:
  1599. if (IS_ENABLED(CONFIG_COMMON_CLK) && !IS_ERR(udc->uclk))
  1600. clk_put(udc->uclk);
  1601. if (!IS_ERR(udc->fclk))
  1602. clk_put(udc->fclk);
  1603. if (!IS_ERR(udc->iclk))
  1604. clk_put(udc->iclk);
  1605. iounmap(udc->udp_baseaddr);
  1606. fail0a:
  1607. if (cpu_is_at91rm9200())
  1608. gpio_free(udc->board.pullup_pin);
  1609. fail0:
  1610. release_mem_region(res->start, resource_size(res));
  1611. DBG("%s probe failed, %d\n", driver_name, retval);
  1612. return retval;
  1613. }
  1614. static int __exit at91udc_remove(struct platform_device *pdev)
  1615. {
  1616. struct at91_udc *udc = platform_get_drvdata(pdev);
  1617. struct resource *res;
  1618. unsigned long flags;
  1619. DBG("remove\n");
  1620. usb_del_gadget_udc(&udc->gadget);
  1621. if (udc->driver)
  1622. return -EBUSY;
  1623. spin_lock_irqsave(&udc->lock, flags);
  1624. pullup(udc, 0);
  1625. spin_unlock_irqrestore(&udc->lock, flags);
  1626. device_init_wakeup(&pdev->dev, 0);
  1627. remove_debug_file(udc);
  1628. if (gpio_is_valid(udc->board.vbus_pin)) {
  1629. free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
  1630. gpio_free(udc->board.vbus_pin);
  1631. }
  1632. free_irq(udc->udp_irq, udc);
  1633. iounmap(udc->udp_baseaddr);
  1634. if (cpu_is_at91rm9200())
  1635. gpio_free(udc->board.pullup_pin);
  1636. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1637. release_mem_region(res->start, resource_size(res));
  1638. clk_put(udc->iclk);
  1639. clk_put(udc->fclk);
  1640. if (IS_ENABLED(CONFIG_COMMON_CLK))
  1641. clk_put(udc->uclk);
  1642. return 0;
  1643. }
  1644. #ifdef CONFIG_PM
  1645. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1646. {
  1647. struct at91_udc *udc = platform_get_drvdata(pdev);
  1648. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1649. unsigned long flags;
  1650. /* Unless we can act normally to the host (letting it wake us up
  1651. * whenever it has work for us) force disconnect. Wakeup requires
  1652. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1653. * tokens) and VBUS irqs (on systems which support them).
  1654. */
  1655. if ((!udc->suspended && udc->addr)
  1656. || !wake
  1657. || at91_suspend_entering_slow_clock()) {
  1658. spin_lock_irqsave(&udc->lock, flags);
  1659. pullup(udc, 0);
  1660. wake = 0;
  1661. spin_unlock_irqrestore(&udc->lock, flags);
  1662. } else
  1663. enable_irq_wake(udc->udp_irq);
  1664. udc->active_suspend = wake;
  1665. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled && wake)
  1666. enable_irq_wake(udc->board.vbus_pin);
  1667. return 0;
  1668. }
  1669. static int at91udc_resume(struct platform_device *pdev)
  1670. {
  1671. struct at91_udc *udc = platform_get_drvdata(pdev);
  1672. unsigned long flags;
  1673. if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled &&
  1674. udc->active_suspend)
  1675. disable_irq_wake(udc->board.vbus_pin);
  1676. /* maybe reconnect to host; if so, clocks on */
  1677. if (udc->active_suspend)
  1678. disable_irq_wake(udc->udp_irq);
  1679. else {
  1680. spin_lock_irqsave(&udc->lock, flags);
  1681. pullup(udc, 1);
  1682. spin_unlock_irqrestore(&udc->lock, flags);
  1683. }
  1684. return 0;
  1685. }
  1686. #else
  1687. #define at91udc_suspend NULL
  1688. #define at91udc_resume NULL
  1689. #endif
  1690. #if defined(CONFIG_OF)
  1691. static const struct of_device_id at91_udc_dt_ids[] = {
  1692. { .compatible = "atmel,at91rm9200-udc" },
  1693. { /* sentinel */ }
  1694. };
  1695. MODULE_DEVICE_TABLE(of, at91_udc_dt_ids);
  1696. #endif
  1697. static struct platform_driver at91_udc_driver = {
  1698. .remove = __exit_p(at91udc_remove),
  1699. .shutdown = at91udc_shutdown,
  1700. .suspend = at91udc_suspend,
  1701. .resume = at91udc_resume,
  1702. .driver = {
  1703. .name = (char *) driver_name,
  1704. .owner = THIS_MODULE,
  1705. .of_match_table = of_match_ptr(at91_udc_dt_ids),
  1706. },
  1707. };
  1708. module_platform_driver_probe(at91_udc_driver, at91udc_probe);
  1709. MODULE_DESCRIPTION("AT91 udc driver");
  1710. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1711. MODULE_LICENSE("GPL");
  1712. MODULE_ALIAS("platform:at91_udc");