at91_udc.c 50 KB

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