udc-xilinx.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  1. /*
  2. * Xilinx USB peripheral controller driver
  3. *
  4. * Copyright (C) 2004 by Thomas Rathbone
  5. * Copyright (C) 2005 by HP Labs
  6. * Copyright (C) 2005 by David Brownell
  7. * Copyright (C) 2010 - 2014 Xilinx, Inc.
  8. *
  9. * Some parts of this driver code is based on the driver for at91-series
  10. * USB peripheral controller (at91_udc.c).
  11. *
  12. * This program is free software; you can redistribute it
  13. * and/or modify it under the terms of the GNU General Public
  14. * License as published by the Free Software Foundation;
  15. * either version 2 of the License, or (at your option) any
  16. * later version.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. /* Register offsets for the USB device.*/
  32. #define XUSB_EP0_CONFIG_OFFSET 0x0000 /* EP0 Config Reg Offset */
  33. #define XUSB_SETUP_PKT_ADDR_OFFSET 0x0080 /* Setup Packet Address */
  34. #define XUSB_ADDRESS_OFFSET 0x0100 /* Address Register */
  35. #define XUSB_CONTROL_OFFSET 0x0104 /* Control Register */
  36. #define XUSB_STATUS_OFFSET 0x0108 /* Status Register */
  37. #define XUSB_FRAMENUM_OFFSET 0x010C /* Frame Number Register */
  38. #define XUSB_IER_OFFSET 0x0110 /* Interrupt Enable Register */
  39. #define XUSB_BUFFREADY_OFFSET 0x0114 /* Buffer Ready Register */
  40. #define XUSB_TESTMODE_OFFSET 0x0118 /* Test Mode Register */
  41. #define XUSB_DMA_RESET_OFFSET 0x0200 /* DMA Soft Reset Register */
  42. #define XUSB_DMA_CONTROL_OFFSET 0x0204 /* DMA Control Register */
  43. #define XUSB_DMA_DSAR_ADDR_OFFSET 0x0208 /* DMA source Address Reg */
  44. #define XUSB_DMA_DDAR_ADDR_OFFSET 0x020C /* DMA destination Addr Reg */
  45. #define XUSB_DMA_LENGTH_OFFSET 0x0210 /* DMA Length Register */
  46. #define XUSB_DMA_STATUS_OFFSET 0x0214 /* DMA Status Register */
  47. /* Endpoint Configuration Space offsets */
  48. #define XUSB_EP_CFGSTATUS_OFFSET 0x00 /* Endpoint Config Status */
  49. #define XUSB_EP_BUF0COUNT_OFFSET 0x08 /* Buffer 0 Count */
  50. #define XUSB_EP_BUF1COUNT_OFFSET 0x0C /* Buffer 1 Count */
  51. #define XUSB_CONTROL_USB_READY_MASK 0x80000000 /* USB ready Mask */
  52. #define XUSB_CONTROL_USB_RMTWAKE_MASK 0x40000000 /* Remote wake up mask */
  53. /* Interrupt register related masks.*/
  54. #define XUSB_STATUS_GLOBAL_INTR_MASK 0x80000000 /* Global Intr Enable */
  55. #define XUSB_STATUS_DMADONE_MASK 0x04000000 /* DMA done Mask */
  56. #define XUSB_STATUS_DMAERR_MASK 0x02000000 /* DMA Error Mask */
  57. #define XUSB_STATUS_DMABUSY_MASK 0x80000000 /* DMA Error Mask */
  58. #define XUSB_STATUS_RESUME_MASK 0x01000000 /* USB Resume Mask */
  59. #define XUSB_STATUS_RESET_MASK 0x00800000 /* USB Reset Mask */
  60. #define XUSB_STATUS_SUSPEND_MASK 0x00400000 /* USB Suspend Mask */
  61. #define XUSB_STATUS_DISCONNECT_MASK 0x00200000 /* USB Disconnect Mask */
  62. #define XUSB_STATUS_FIFO_BUFF_RDY_MASK 0x00100000 /* FIFO Buff Ready Mask */
  63. #define XUSB_STATUS_FIFO_BUFF_FREE_MASK 0x00080000 /* FIFO Buff Free Mask */
  64. #define XUSB_STATUS_SETUP_PACKET_MASK 0x00040000 /* Setup packet received */
  65. #define XUSB_STATUS_EP1_BUFF2_COMP_MASK 0x00000200 /* EP 1 Buff 2 Processed */
  66. #define XUSB_STATUS_EP1_BUFF1_COMP_MASK 0x00000002 /* EP 1 Buff 1 Processed */
  67. #define XUSB_STATUS_EP0_BUFF2_COMP_MASK 0x00000100 /* EP 0 Buff 2 Processed */
  68. #define XUSB_STATUS_EP0_BUFF1_COMP_MASK 0x00000001 /* EP 0 Buff 1 Processed */
  69. #define XUSB_STATUS_HIGH_SPEED_MASK 0x00010000 /* USB Speed Mask */
  70. /* Suspend,Reset,Suspend and Disconnect Mask */
  71. #define XUSB_STATUS_INTR_EVENT_MASK 0x01E00000
  72. /* Buffers completion Mask */
  73. #define XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK 0x0000FEFF
  74. /* Mask for buffer 0 and buffer 1 completion for all Endpoints */
  75. #define XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK 0x00000101
  76. #define XUSB_STATUS_EP_BUFF2_SHIFT 8 /* EP buffer offset */
  77. /* Endpoint Configuration Status Register */
  78. #define XUSB_EP_CFG_VALID_MASK 0x80000000 /* Endpoint Valid bit */
  79. #define XUSB_EP_CFG_STALL_MASK 0x40000000 /* Endpoint Stall bit */
  80. #define XUSB_EP_CFG_DATA_TOGGLE_MASK 0x08000000 /* Endpoint Data toggle */
  81. /* USB device specific global configuration constants.*/
  82. #define XUSB_MAX_ENDPOINTS 8 /* Maximum End Points */
  83. #define XUSB_EP_NUMBER_ZERO 0 /* End point Zero */
  84. /* DPRAM is the source address for DMA transfer */
  85. #define XUSB_DMA_READ_FROM_DPRAM 0x80000000
  86. #define XUSB_DMA_DMASR_BUSY 0x80000000 /* DMA busy */
  87. #define XUSB_DMA_DMASR_ERROR 0x40000000 /* DMA Error */
  88. /*
  89. * When this bit is set, the DMA buffer ready bit is set by hardware upon
  90. * DMA transfer completion.
  91. */
  92. #define XUSB_DMA_BRR_CTRL 0x40000000 /* DMA bufready ctrl bit */
  93. /* Phase States */
  94. #define SETUP_PHASE 0x0000 /* Setup Phase */
  95. #define DATA_PHASE 0x0001 /* Data Phase */
  96. #define STATUS_PHASE 0x0002 /* Status Phase */
  97. #define EP0_MAX_PACKET 64 /* Endpoint 0 maximum packet length */
  98. #define STATUSBUFF_SIZE 2 /* Buffer size for GET_STATUS command */
  99. #define EPNAME_SIZE 4 /* Buffer size for endpoint name */
  100. /* container_of helper macros */
  101. #define to_udc(g) container_of((g), struct xusb_udc, gadget)
  102. #define to_xusb_ep(ep) container_of((ep), struct xusb_ep, ep_usb)
  103. #define to_xusb_req(req) container_of((req), struct xusb_req, usb_req)
  104. /**
  105. * struct xusb_req - Xilinx USB device request structure
  106. * @usb_req: Linux usb request structure
  107. * @queue: usb device request queue
  108. * @ep: pointer to xusb_endpoint structure
  109. */
  110. struct xusb_req {
  111. struct usb_request usb_req;
  112. struct list_head queue;
  113. struct xusb_ep *ep;
  114. };
  115. /**
  116. * struct xusb_ep - USB end point structure.
  117. * @ep_usb: usb endpoint instance
  118. * @queue: endpoint message queue
  119. * @udc: xilinx usb peripheral driver instance pointer
  120. * @desc: pointer to the usb endpoint descriptor
  121. * @rambase: the endpoint buffer address
  122. * @offset: the endpoint register offset value
  123. * @name: name of the endpoint
  124. * @epnumber: endpoint number
  125. * @maxpacket: maximum packet size the endpoint can store
  126. * @buffer0count: the size of the packet recieved in the first buffer
  127. * @buffer1count: the size of the packet received in the second buffer
  128. * @curbufnum: current buffer of endpoint that will be processed next
  129. * @buffer0ready: the busy state of first buffer
  130. * @buffer1ready: the busy state of second buffer
  131. * @is_in: endpoint direction (IN or OUT)
  132. * @is_iso: endpoint type(isochronous or non isochronous)
  133. */
  134. struct xusb_ep {
  135. struct usb_ep ep_usb;
  136. struct list_head queue;
  137. struct xusb_udc *udc;
  138. const struct usb_endpoint_descriptor *desc;
  139. u32 rambase;
  140. u32 offset;
  141. char name[4];
  142. u16 epnumber;
  143. u16 maxpacket;
  144. u16 buffer0count;
  145. u16 buffer1count;
  146. u8 curbufnum;
  147. bool buffer0ready;
  148. bool buffer1ready;
  149. bool is_in;
  150. bool is_iso;
  151. };
  152. /**
  153. * struct xusb_udc - USB peripheral driver structure
  154. * @gadget: USB gadget driver instance
  155. * @ep: an array of endpoint structures
  156. * @driver: pointer to the usb gadget driver instance
  157. * @setup: usb_ctrlrequest structure for control requests
  158. * @req: pointer to dummy request for get status command
  159. * @dev: pointer to device structure in gadget
  160. * @usb_state: device in suspended state or not
  161. * @remote_wkp: remote wakeup enabled by host
  162. * @setupseqtx: tx status
  163. * @setupseqrx: rx status
  164. * @addr: the usb device base address
  165. * @lock: instance of spinlock
  166. * @dma_enabled: flag indicating whether the dma is included in the system
  167. * @read_fn: function pointer to read device registers
  168. * @write_fn: function pointer to write to device registers
  169. */
  170. struct xusb_udc {
  171. struct usb_gadget gadget;
  172. struct xusb_ep ep[8];
  173. struct usb_gadget_driver *driver;
  174. struct usb_ctrlrequest setup;
  175. struct xusb_req *req;
  176. struct device *dev;
  177. u32 usb_state;
  178. u32 remote_wkp;
  179. u32 setupseqtx;
  180. u32 setupseqrx;
  181. void __iomem *addr;
  182. spinlock_t lock;
  183. bool dma_enabled;
  184. unsigned int (*read_fn)(void __iomem *);
  185. void (*write_fn)(void __iomem *, u32, u32);
  186. };
  187. /* Endpoint buffer start addresses in the core */
  188. static u32 rambase[8] = { 0x22, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
  189. 0x1600 };
  190. static const char driver_name[] = "xilinx-udc";
  191. static const char ep0name[] = "ep0";
  192. /* Control endpoint configuration.*/
  193. static const struct usb_endpoint_descriptor config_bulk_out_desc = {
  194. .bLength = USB_DT_ENDPOINT_SIZE,
  195. .bDescriptorType = USB_DT_ENDPOINT,
  196. .bEndpointAddress = USB_DIR_OUT,
  197. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  198. .wMaxPacketSize = cpu_to_le16(EP0_MAX_PACKET),
  199. };
  200. /**
  201. * xudc_write32 - little endian write to device registers
  202. * @addr: base addr of device registers
  203. * @offset: register offset
  204. * @val: data to be written
  205. */
  206. static void xudc_write32(void __iomem *addr, u32 offset, u32 val)
  207. {
  208. iowrite32(val, addr + offset);
  209. }
  210. /**
  211. * xudc_read32 - little endian read from device registers
  212. * @addr: addr of device register
  213. * Return: value at addr
  214. */
  215. static unsigned int xudc_read32(void __iomem *addr)
  216. {
  217. return ioread32(addr);
  218. }
  219. /**
  220. * xudc_write32_be - big endian write to device registers
  221. * @addr: base addr of device registers
  222. * @offset: register offset
  223. * @val: data to be written
  224. */
  225. static void xudc_write32_be(void __iomem *addr, u32 offset, u32 val)
  226. {
  227. iowrite32be(val, addr + offset);
  228. }
  229. /**
  230. * xudc_read32_be - big endian read from device registers
  231. * @addr: addr of device register
  232. * Return: value at addr
  233. */
  234. static unsigned int xudc_read32_be(void __iomem *addr)
  235. {
  236. return ioread32be(addr);
  237. }
  238. /**
  239. * xudc_wrstatus - Sets up the usb device status stages.
  240. * @udc: pointer to the usb device controller structure.
  241. */
  242. static void xudc_wrstatus(struct xusb_udc *udc)
  243. {
  244. struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
  245. u32 epcfgreg;
  246. epcfgreg = udc->read_fn(udc->addr + ep0->offset)|
  247. XUSB_EP_CFG_DATA_TOGGLE_MASK;
  248. udc->write_fn(udc->addr, ep0->offset, epcfgreg);
  249. udc->write_fn(udc->addr, ep0->offset + XUSB_EP_BUF0COUNT_OFFSET, 0);
  250. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
  251. }
  252. /**
  253. * xudc_epconfig - Configures the given endpoint.
  254. * @ep: pointer to the usb device endpoint structure.
  255. * @udc: pointer to the usb peripheral controller structure.
  256. *
  257. * This function configures a specific endpoint with the given configuration
  258. * data.
  259. */
  260. static void xudc_epconfig(struct xusb_ep *ep, struct xusb_udc *udc)
  261. {
  262. u32 epcfgreg;
  263. /*
  264. * Configure the end point direction, type, Max Packet Size and the
  265. * EP buffer location.
  266. */
  267. epcfgreg = ((ep->is_in << 29) | (ep->is_iso << 28) |
  268. (ep->ep_usb.maxpacket << 15) | (ep->rambase));
  269. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  270. /* Set the Buffer count and the Buffer ready bits.*/
  271. udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF0COUNT_OFFSET,
  272. ep->buffer0count);
  273. udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF1COUNT_OFFSET,
  274. ep->buffer1count);
  275. if (ep->buffer0ready)
  276. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  277. 1 << ep->epnumber);
  278. if (ep->buffer1ready)
  279. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  280. 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
  281. }
  282. /**
  283. * xudc_start_dma - Starts DMA transfer.
  284. * @ep: pointer to the usb device endpoint structure.
  285. * @src: DMA source address.
  286. * @dst: DMA destination address.
  287. * @length: number of bytes to transfer.
  288. *
  289. * Return: 0 on success, error code on failure
  290. *
  291. * This function starts DMA transfer by writing to DMA source,
  292. * destination and lenth registers.
  293. */
  294. static int xudc_start_dma(struct xusb_ep *ep, dma_addr_t src,
  295. dma_addr_t dst, u32 length)
  296. {
  297. struct xusb_udc *udc = ep->udc;
  298. int rc = 0;
  299. u32 timeout = 500;
  300. u32 reg;
  301. /*
  302. * Set the addresses in the DMA source and
  303. * destination registers and then set the length
  304. * into the DMA length register.
  305. */
  306. udc->write_fn(udc->addr, XUSB_DMA_DSAR_ADDR_OFFSET, src);
  307. udc->write_fn(udc->addr, XUSB_DMA_DDAR_ADDR_OFFSET, dst);
  308. udc->write_fn(udc->addr, XUSB_DMA_LENGTH_OFFSET, length);
  309. /*
  310. * Wait till DMA transaction is complete and
  311. * check whether the DMA transaction was
  312. * successful.
  313. */
  314. do {
  315. reg = udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET);
  316. if (!(reg & XUSB_DMA_DMASR_BUSY))
  317. break;
  318. /*
  319. * We can't sleep here, because it's also called from
  320. * interrupt context.
  321. */
  322. timeout--;
  323. if (!timeout) {
  324. dev_err(udc->dev, "DMA timeout\n");
  325. return -ETIMEDOUT;
  326. }
  327. udelay(1);
  328. } while (1);
  329. if ((udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET) &
  330. XUSB_DMA_DMASR_ERROR) == XUSB_DMA_DMASR_ERROR){
  331. dev_err(udc->dev, "DMA Error\n");
  332. rc = -EINVAL;
  333. }
  334. return rc;
  335. }
  336. /**
  337. * xudc_dma_send - Sends IN data using DMA.
  338. * @ep: pointer to the usb device endpoint structure.
  339. * @req: pointer to the usb request structure.
  340. * @buffer: pointer to data to be sent.
  341. * @length: number of bytes to send.
  342. *
  343. * Return: 0 on success, -EAGAIN if no buffer is free and error
  344. * code on failure.
  345. *
  346. * This function sends data using DMA.
  347. */
  348. static int xudc_dma_send(struct xusb_ep *ep, struct xusb_req *req,
  349. u8 *buffer, u32 length)
  350. {
  351. u32 *eprambase;
  352. dma_addr_t src;
  353. dma_addr_t dst;
  354. struct xusb_udc *udc = ep->udc;
  355. src = req->usb_req.dma + req->usb_req.actual;
  356. if (req->usb_req.length)
  357. dma_sync_single_for_device(udc->dev, src,
  358. length, DMA_TO_DEVICE);
  359. if (!ep->curbufnum && !ep->buffer0ready) {
  360. /* Get the Buffer address and copy the transmit data.*/
  361. eprambase = (u32 __force *)(udc->addr + ep->rambase);
  362. dst = virt_to_phys(eprambase);
  363. udc->write_fn(udc->addr, ep->offset +
  364. XUSB_EP_BUF0COUNT_OFFSET, length);
  365. udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
  366. XUSB_DMA_BRR_CTRL | (1 << ep->epnumber));
  367. ep->buffer0ready = 1;
  368. ep->curbufnum = 1;
  369. } else if (ep->curbufnum && !ep->buffer1ready) {
  370. /* Get the Buffer address and copy the transmit data.*/
  371. eprambase = (u32 __force *)(udc->addr + ep->rambase +
  372. ep->ep_usb.maxpacket);
  373. dst = virt_to_phys(eprambase);
  374. udc->write_fn(udc->addr, ep->offset +
  375. XUSB_EP_BUF1COUNT_OFFSET, length);
  376. udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
  377. XUSB_DMA_BRR_CTRL | (1 << (ep->epnumber +
  378. XUSB_STATUS_EP_BUFF2_SHIFT)));
  379. ep->buffer1ready = 1;
  380. ep->curbufnum = 0;
  381. } else {
  382. /* None of ping pong buffers are ready currently .*/
  383. return -EAGAIN;
  384. }
  385. return xudc_start_dma(ep, src, dst, length);
  386. }
  387. /**
  388. * xudc_dma_receive - Receives OUT data using DMA.
  389. * @ep: pointer to the usb device endpoint structure.
  390. * @req: pointer to the usb request structure.
  391. * @buffer: pointer to storage buffer of received data.
  392. * @length: number of bytes to receive.
  393. *
  394. * Return: 0 on success, -EAGAIN if no buffer is free and error
  395. * code on failure.
  396. *
  397. * This function receives data using DMA.
  398. */
  399. static int xudc_dma_receive(struct xusb_ep *ep, struct xusb_req *req,
  400. u8 *buffer, u32 length)
  401. {
  402. u32 *eprambase;
  403. dma_addr_t src;
  404. dma_addr_t dst;
  405. struct xusb_udc *udc = ep->udc;
  406. dst = req->usb_req.dma + req->usb_req.actual;
  407. if (!ep->curbufnum && !ep->buffer0ready) {
  408. /* Get the Buffer address and copy the transmit data */
  409. eprambase = (u32 __force *)(udc->addr + ep->rambase);
  410. src = virt_to_phys(eprambase);
  411. udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
  412. XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
  413. (1 << ep->epnumber));
  414. ep->buffer0ready = 1;
  415. ep->curbufnum = 1;
  416. } else if (ep->curbufnum && !ep->buffer1ready) {
  417. /* Get the Buffer address and copy the transmit data */
  418. eprambase = (u32 __force *)(udc->addr +
  419. ep->rambase + ep->ep_usb.maxpacket);
  420. src = virt_to_phys(eprambase);
  421. udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
  422. XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
  423. (1 << (ep->epnumber +
  424. XUSB_STATUS_EP_BUFF2_SHIFT)));
  425. ep->buffer1ready = 1;
  426. ep->curbufnum = 0;
  427. } else {
  428. /* None of the ping-pong buffers are ready currently */
  429. return -EAGAIN;
  430. }
  431. return xudc_start_dma(ep, src, dst, length);
  432. }
  433. /**
  434. * xudc_eptxrx - Transmits or receives data to or from an endpoint.
  435. * @ep: pointer to the usb endpoint configuration structure.
  436. * @req: pointer to the usb request structure.
  437. * @bufferptr: pointer to buffer containing the data to be sent.
  438. * @bufferlen: The number of data bytes to be sent.
  439. *
  440. * Return: 0 on success, -EAGAIN if no buffer is free.
  441. *
  442. * This function copies the transmit/receive data to/from the end point buffer
  443. * and enables the buffer for transmission/reception.
  444. */
  445. static int xudc_eptxrx(struct xusb_ep *ep, struct xusb_req *req,
  446. u8 *bufferptr, u32 bufferlen)
  447. {
  448. u32 *eprambase;
  449. u32 bytestosend;
  450. int rc = 0;
  451. struct xusb_udc *udc = ep->udc;
  452. bytestosend = bufferlen;
  453. if (udc->dma_enabled) {
  454. if (ep->is_in)
  455. rc = xudc_dma_send(ep, req, bufferptr, bufferlen);
  456. else
  457. rc = xudc_dma_receive(ep, req, bufferptr, bufferlen);
  458. return rc;
  459. }
  460. /* Put the transmit buffer into the correct ping-pong buffer.*/
  461. if (!ep->curbufnum && !ep->buffer0ready) {
  462. /* Get the Buffer address and copy the transmit data.*/
  463. eprambase = (u32 __force *)(udc->addr + ep->rambase);
  464. if (ep->is_in) {
  465. memcpy(eprambase, bufferptr, bytestosend);
  466. udc->write_fn(udc->addr, ep->offset +
  467. XUSB_EP_BUF0COUNT_OFFSET, bufferlen);
  468. } else {
  469. memcpy(bufferptr, eprambase, bytestosend);
  470. }
  471. /*
  472. * Enable the buffer for transmission.
  473. */
  474. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  475. 1 << ep->epnumber);
  476. ep->buffer0ready = 1;
  477. ep->curbufnum = 1;
  478. } else if (ep->curbufnum && !ep->buffer1ready) {
  479. /* Get the Buffer address and copy the transmit data.*/
  480. eprambase = (u32 __force *)(udc->addr + ep->rambase +
  481. ep->ep_usb.maxpacket);
  482. if (ep->is_in) {
  483. memcpy(eprambase, bufferptr, bytestosend);
  484. udc->write_fn(udc->addr, ep->offset +
  485. XUSB_EP_BUF1COUNT_OFFSET, bufferlen);
  486. } else {
  487. memcpy(bufferptr, eprambase, bytestosend);
  488. }
  489. /*
  490. * Enable the buffer for transmission.
  491. */
  492. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  493. 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
  494. ep->buffer1ready = 1;
  495. ep->curbufnum = 0;
  496. } else {
  497. /* None of the ping-pong buffers are ready currently */
  498. return -EAGAIN;
  499. }
  500. return rc;
  501. }
  502. /**
  503. * xudc_done - Exeutes the endpoint data transfer completion tasks.
  504. * @ep: pointer to the usb device endpoint structure.
  505. * @req: pointer to the usb request structure.
  506. * @status: Status of the data transfer.
  507. *
  508. * Deletes the message from the queue and updates data transfer completion
  509. * status.
  510. */
  511. static void xudc_done(struct xusb_ep *ep, struct xusb_req *req, int status)
  512. {
  513. struct xusb_udc *udc = ep->udc;
  514. list_del_init(&req->queue);
  515. if (req->usb_req.status == -EINPROGRESS)
  516. req->usb_req.status = status;
  517. else
  518. status = req->usb_req.status;
  519. if (status && status != -ESHUTDOWN)
  520. dev_dbg(udc->dev, "%s done %p, status %d\n",
  521. ep->ep_usb.name, req, status);
  522. /* unmap request if DMA is present*/
  523. if (udc->dma_enabled && ep->epnumber && req->usb_req.length)
  524. usb_gadget_unmap_request(&udc->gadget, &req->usb_req,
  525. ep->is_in);
  526. if (req->usb_req.complete) {
  527. spin_unlock(&udc->lock);
  528. req->usb_req.complete(&ep->ep_usb, &req->usb_req);
  529. spin_lock(&udc->lock);
  530. }
  531. }
  532. /**
  533. * xudc_read_fifo - Reads the data from the given endpoint buffer.
  534. * @ep: pointer to the usb device endpoint structure.
  535. * @req: pointer to the usb request structure.
  536. *
  537. * Return: 0 if request is completed and -EAGAIN if not completed.
  538. *
  539. * Pulls OUT packet data from the endpoint buffer.
  540. */
  541. static int xudc_read_fifo(struct xusb_ep *ep, struct xusb_req *req)
  542. {
  543. u8 *buf;
  544. u32 is_short, count, bufferspace;
  545. u8 bufoffset;
  546. u8 two_pkts = 0;
  547. int ret;
  548. int retval = -EAGAIN;
  549. struct xusb_udc *udc = ep->udc;
  550. if (ep->buffer0ready && ep->buffer1ready) {
  551. dev_dbg(udc->dev, "Packet NOT ready!\n");
  552. return retval;
  553. }
  554. top:
  555. if (ep->curbufnum)
  556. bufoffset = XUSB_EP_BUF1COUNT_OFFSET;
  557. else
  558. bufoffset = XUSB_EP_BUF0COUNT_OFFSET;
  559. count = udc->read_fn(udc->addr + ep->offset + bufoffset);
  560. if (!ep->buffer0ready && !ep->buffer1ready)
  561. two_pkts = 1;
  562. buf = req->usb_req.buf + req->usb_req.actual;
  563. prefetchw(buf);
  564. bufferspace = req->usb_req.length - req->usb_req.actual;
  565. is_short = count < ep->ep_usb.maxpacket;
  566. if (unlikely(!bufferspace)) {
  567. /*
  568. * This happens when the driver's buffer
  569. * is smaller than what the host sent.
  570. * discard the extra data.
  571. */
  572. if (req->usb_req.status != -EOVERFLOW)
  573. dev_dbg(udc->dev, "%s overflow %d\n",
  574. ep->ep_usb.name, count);
  575. req->usb_req.status = -EOVERFLOW;
  576. xudc_done(ep, req, -EOVERFLOW);
  577. return 0;
  578. }
  579. ret = xudc_eptxrx(ep, req, buf, count);
  580. switch (ret) {
  581. case 0:
  582. req->usb_req.actual += min(count, bufferspace);
  583. dev_dbg(udc->dev, "read %s, %d bytes%s req %p %d/%d\n",
  584. ep->ep_usb.name, count, is_short ? "/S" : "", req,
  585. req->usb_req.actual, req->usb_req.length);
  586. bufferspace -= count;
  587. /* Completion */
  588. if ((req->usb_req.actual == req->usb_req.length) || is_short) {
  589. if (udc->dma_enabled && req->usb_req.length)
  590. dma_sync_single_for_cpu(udc->dev,
  591. req->usb_req.dma,
  592. req->usb_req.actual,
  593. DMA_FROM_DEVICE);
  594. xudc_done(ep, req, 0);
  595. return 0;
  596. }
  597. if (two_pkts) {
  598. two_pkts = 0;
  599. goto top;
  600. }
  601. break;
  602. case -EAGAIN:
  603. dev_dbg(udc->dev, "receive busy\n");
  604. break;
  605. case -EINVAL:
  606. case -ETIMEDOUT:
  607. /* DMA error, dequeue the request */
  608. xudc_done(ep, req, -ECONNRESET);
  609. retval = 0;
  610. break;
  611. }
  612. return retval;
  613. }
  614. /**
  615. * xudc_write_fifo - Writes data into the given endpoint buffer.
  616. * @ep: pointer to the usb device endpoint structure.
  617. * @req: pointer to the usb request structure.
  618. *
  619. * Return: 0 if request is completed and -EAGAIN if not completed.
  620. *
  621. * Loads endpoint buffer for an IN packet.
  622. */
  623. static int xudc_write_fifo(struct xusb_ep *ep, struct xusb_req *req)
  624. {
  625. u32 max;
  626. u32 length;
  627. int ret;
  628. int retval = -EAGAIN;
  629. struct xusb_udc *udc = ep->udc;
  630. int is_last, is_short = 0;
  631. u8 *buf;
  632. max = le16_to_cpu(ep->desc->wMaxPacketSize);
  633. buf = req->usb_req.buf + req->usb_req.actual;
  634. prefetch(buf);
  635. length = req->usb_req.length - req->usb_req.actual;
  636. length = min(length, max);
  637. ret = xudc_eptxrx(ep, req, buf, length);
  638. switch (ret) {
  639. case 0:
  640. req->usb_req.actual += length;
  641. if (unlikely(length != max)) {
  642. is_last = is_short = 1;
  643. } else {
  644. if (likely(req->usb_req.length !=
  645. req->usb_req.actual) || req->usb_req.zero)
  646. is_last = 0;
  647. else
  648. is_last = 1;
  649. }
  650. dev_dbg(udc->dev, "%s: wrote %s %d bytes%s%s %d left %p\n",
  651. __func__, ep->ep_usb.name, length, is_last ? "/L" : "",
  652. is_short ? "/S" : "",
  653. req->usb_req.length - req->usb_req.actual, req);
  654. /* completion */
  655. if (is_last) {
  656. xudc_done(ep, req, 0);
  657. retval = 0;
  658. }
  659. break;
  660. case -EAGAIN:
  661. dev_dbg(udc->dev, "Send busy\n");
  662. break;
  663. case -EINVAL:
  664. case -ETIMEDOUT:
  665. /* DMA error, dequeue the request */
  666. xudc_done(ep, req, -ECONNRESET);
  667. retval = 0;
  668. break;
  669. }
  670. return retval;
  671. }
  672. /**
  673. * xudc_nuke - Cleans up the data transfer message list.
  674. * @ep: pointer to the usb device endpoint structure.
  675. * @status: Status of the data transfer.
  676. */
  677. static void xudc_nuke(struct xusb_ep *ep, int status)
  678. {
  679. struct xusb_req *req;
  680. while (!list_empty(&ep->queue)) {
  681. req = list_first_entry(&ep->queue, struct xusb_req, queue);
  682. xudc_done(ep, req, status);
  683. }
  684. }
  685. /**
  686. * xudc_ep_set_halt - Stalls/unstalls the given endpoint.
  687. * @_ep: pointer to the usb device endpoint structure.
  688. * @value: value to indicate stall/unstall.
  689. *
  690. * Return: 0 for success and error value on failure
  691. */
  692. static int xudc_ep_set_halt(struct usb_ep *_ep, int value)
  693. {
  694. struct xusb_ep *ep = to_xusb_ep(_ep);
  695. struct xusb_udc *udc;
  696. unsigned long flags;
  697. u32 epcfgreg;
  698. if (!_ep || (!ep->desc && ep->epnumber)) {
  699. pr_debug("%s: bad ep or descriptor\n", __func__);
  700. return -EINVAL;
  701. }
  702. udc = ep->udc;
  703. if (ep->is_in && (!list_empty(&ep->queue)) && value) {
  704. dev_dbg(udc->dev, "requests pending can't halt\n");
  705. return -EAGAIN;
  706. }
  707. if (ep->buffer0ready || ep->buffer1ready) {
  708. dev_dbg(udc->dev, "HW buffers busy can't halt\n");
  709. return -EAGAIN;
  710. }
  711. spin_lock_irqsave(&udc->lock, flags);
  712. if (value) {
  713. /* Stall the device.*/
  714. epcfgreg = udc->read_fn(udc->addr + ep->offset);
  715. epcfgreg |= XUSB_EP_CFG_STALL_MASK;
  716. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  717. } else {
  718. /* Unstall the device.*/
  719. epcfgreg = udc->read_fn(udc->addr + ep->offset);
  720. epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
  721. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  722. if (ep->epnumber) {
  723. /* Reset the toggle bit.*/
  724. epcfgreg = udc->read_fn(ep->udc->addr + ep->offset);
  725. epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
  726. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  727. }
  728. }
  729. spin_unlock_irqrestore(&udc->lock, flags);
  730. return 0;
  731. }
  732. /**
  733. * xudc_ep_enable - Enables the given endpoint.
  734. * @ep: pointer to the xusb endpoint structure.
  735. * @desc: pointer to usb endpoint descriptor.
  736. *
  737. * Return: 0 for success and error value on failure
  738. */
  739. static int __xudc_ep_enable(struct xusb_ep *ep,
  740. const struct usb_endpoint_descriptor *desc)
  741. {
  742. struct xusb_udc *udc = ep->udc;
  743. u32 tmp;
  744. u32 epcfg;
  745. u32 ier;
  746. u16 maxpacket;
  747. ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
  748. /* Bit 3...0:endpoint number */
  749. ep->epnumber = (desc->bEndpointAddress & 0x0f);
  750. ep->desc = desc;
  751. ep->ep_usb.desc = desc;
  752. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  753. ep->ep_usb.maxpacket = maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  754. switch (tmp) {
  755. case USB_ENDPOINT_XFER_CONTROL:
  756. dev_dbg(udc->dev, "only one control endpoint\n");
  757. /* NON- ISO */
  758. ep->is_iso = 0;
  759. return -EINVAL;
  760. case USB_ENDPOINT_XFER_INT:
  761. /* NON- ISO */
  762. ep->is_iso = 0;
  763. if (maxpacket > 64) {
  764. dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
  765. return -EINVAL;
  766. }
  767. break;
  768. case USB_ENDPOINT_XFER_BULK:
  769. /* NON- ISO */
  770. ep->is_iso = 0;
  771. if (!(is_power_of_2(maxpacket) && maxpacket >= 8 &&
  772. maxpacket <= 512)) {
  773. dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
  774. return -EINVAL;
  775. }
  776. break;
  777. case USB_ENDPOINT_XFER_ISOC:
  778. /* ISO */
  779. ep->is_iso = 1;
  780. break;
  781. }
  782. ep->buffer0ready = 0;
  783. ep->buffer1ready = 0;
  784. ep->curbufnum = 0;
  785. ep->rambase = rambase[ep->epnumber];
  786. xudc_epconfig(ep, udc);
  787. dev_dbg(udc->dev, "Enable Endpoint %d max pkt is %d\n",
  788. ep->epnumber, maxpacket);
  789. /* Enable the End point.*/
  790. epcfg = udc->read_fn(udc->addr + ep->offset);
  791. epcfg |= XUSB_EP_CFG_VALID_MASK;
  792. udc->write_fn(udc->addr, ep->offset, epcfg);
  793. if (ep->epnumber)
  794. ep->rambase <<= 2;
  795. /* Enable buffer completion interrupts for endpoint */
  796. ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  797. ier |= (XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK << ep->epnumber);
  798. udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
  799. /* for OUT endpoint set buffers ready to receive */
  800. if (ep->epnumber && !ep->is_in) {
  801. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  802. 1 << ep->epnumber);
  803. ep->buffer0ready = 1;
  804. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
  805. (1 << (ep->epnumber +
  806. XUSB_STATUS_EP_BUFF2_SHIFT)));
  807. ep->buffer1ready = 1;
  808. }
  809. return 0;
  810. }
  811. /**
  812. * xudc_ep_enable - Enables the given endpoint.
  813. * @_ep: pointer to the usb endpoint structure.
  814. * @desc: pointer to usb endpoint descriptor.
  815. *
  816. * Return: 0 for success and error value on failure
  817. */
  818. static int xudc_ep_enable(struct usb_ep *_ep,
  819. const struct usb_endpoint_descriptor *desc)
  820. {
  821. struct xusb_ep *ep;
  822. struct xusb_udc *udc;
  823. unsigned long flags;
  824. int ret;
  825. if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  826. pr_debug("%s: bad ep or descriptor\n", __func__);
  827. return -EINVAL;
  828. }
  829. ep = to_xusb_ep(_ep);
  830. udc = ep->udc;
  831. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  832. dev_dbg(udc->dev, "bogus device state\n");
  833. return -ESHUTDOWN;
  834. }
  835. spin_lock_irqsave(&udc->lock, flags);
  836. ret = __xudc_ep_enable(ep, desc);
  837. spin_unlock_irqrestore(&udc->lock, flags);
  838. return ret;
  839. }
  840. /**
  841. * xudc_ep_disable - Disables the given endpoint.
  842. * @_ep: pointer to the usb endpoint structure.
  843. *
  844. * Return: 0 for success and error value on failure
  845. */
  846. static int xudc_ep_disable(struct usb_ep *_ep)
  847. {
  848. struct xusb_ep *ep;
  849. unsigned long flags;
  850. u32 epcfg;
  851. struct xusb_udc *udc;
  852. if (!_ep) {
  853. pr_debug("%s: invalid ep\n", __func__);
  854. return -EINVAL;
  855. }
  856. ep = to_xusb_ep(_ep);
  857. udc = ep->udc;
  858. spin_lock_irqsave(&udc->lock, flags);
  859. xudc_nuke(ep, -ESHUTDOWN);
  860. /* Restore the endpoint's pristine config */
  861. ep->desc = NULL;
  862. ep->ep_usb.desc = NULL;
  863. dev_dbg(udc->dev, "USB Ep %d disable\n ", ep->epnumber);
  864. /* Disable the endpoint.*/
  865. epcfg = udc->read_fn(udc->addr + ep->offset);
  866. epcfg &= ~XUSB_EP_CFG_VALID_MASK;
  867. udc->write_fn(udc->addr, ep->offset, epcfg);
  868. spin_unlock_irqrestore(&udc->lock, flags);
  869. return 0;
  870. }
  871. /**
  872. * xudc_ep_alloc_request - Initializes the request queue.
  873. * @_ep: pointer to the usb endpoint structure.
  874. * @gfp_flags: Flags related to the request call.
  875. *
  876. * Return: pointer to request structure on success and a NULL on failure.
  877. */
  878. static struct usb_request *xudc_ep_alloc_request(struct usb_ep *_ep,
  879. gfp_t gfp_flags)
  880. {
  881. struct xusb_ep *ep = to_xusb_ep(_ep);
  882. struct xusb_udc *udc;
  883. struct xusb_req *req;
  884. udc = ep->udc;
  885. req = kzalloc(sizeof(*req), gfp_flags);
  886. if (!req) {
  887. dev_err(udc->dev, "%s:not enough memory", __func__);
  888. return NULL;
  889. }
  890. req->ep = ep;
  891. INIT_LIST_HEAD(&req->queue);
  892. return &req->usb_req;
  893. }
  894. /**
  895. * xudc_free_request - Releases the request from queue.
  896. * @_ep: pointer to the usb device endpoint structure.
  897. * @_req: pointer to the usb request structure.
  898. */
  899. static void xudc_free_request(struct usb_ep *_ep, struct usb_request *_req)
  900. {
  901. struct xusb_req *req = to_xusb_req(_req);
  902. kfree(req);
  903. }
  904. /**
  905. * xudc_ep0_queue - Adds the request to endpoint 0 queue.
  906. * @ep0: pointer to the xusb endpoint 0 structure.
  907. * @req: pointer to the xusb request structure.
  908. *
  909. * Return: 0 for success and error value on failure
  910. */
  911. static int __xudc_ep0_queue(struct xusb_ep *ep0, struct xusb_req *req)
  912. {
  913. struct xusb_udc *udc = ep0->udc;
  914. u32 length;
  915. u8 *corebuf;
  916. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  917. dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
  918. return -EINVAL;
  919. }
  920. if (!list_empty(&ep0->queue)) {
  921. dev_dbg(udc->dev, "%s:ep0 busy\n", __func__);
  922. return -EBUSY;
  923. }
  924. req->usb_req.status = -EINPROGRESS;
  925. req->usb_req.actual = 0;
  926. list_add_tail(&req->queue, &ep0->queue);
  927. if (udc->setup.bRequestType & USB_DIR_IN) {
  928. prefetch(req->usb_req.buf);
  929. length = req->usb_req.length;
  930. corebuf = (void __force *) ((ep0->rambase << 2) +
  931. udc->addr);
  932. length = req->usb_req.actual = min_t(u32, length,
  933. EP0_MAX_PACKET);
  934. memcpy(corebuf, req->usb_req.buf, length);
  935. udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, length);
  936. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
  937. } else {
  938. if (udc->setup.wLength) {
  939. /* Enable EP0 buffer to receive data */
  940. udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
  941. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
  942. } else {
  943. xudc_wrstatus(udc);
  944. }
  945. }
  946. return 0;
  947. }
  948. /**
  949. * xudc_ep0_queue - Adds the request to endpoint 0 queue.
  950. * @_ep: pointer to the usb endpoint 0 structure.
  951. * @_req: pointer to the usb request structure.
  952. * @gfp_flags: Flags related to the request call.
  953. *
  954. * Return: 0 for success and error value on failure
  955. */
  956. static int xudc_ep0_queue(struct usb_ep *_ep, struct usb_request *_req,
  957. gfp_t gfp_flags)
  958. {
  959. struct xusb_req *req = to_xusb_req(_req);
  960. struct xusb_ep *ep0 = to_xusb_ep(_ep);
  961. struct xusb_udc *udc = ep0->udc;
  962. unsigned long flags;
  963. int ret;
  964. spin_lock_irqsave(&udc->lock, flags);
  965. ret = __xudc_ep0_queue(ep0, req);
  966. spin_unlock_irqrestore(&udc->lock, flags);
  967. return ret;
  968. }
  969. /**
  970. * xudc_ep_queue - Adds the request to endpoint queue.
  971. * @_ep: pointer to the usb endpoint structure.
  972. * @_req: pointer to the usb request structure.
  973. * @gfp_flags: Flags related to the request call.
  974. *
  975. * Return: 0 for success and error value on failure
  976. */
  977. static int xudc_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  978. gfp_t gfp_flags)
  979. {
  980. struct xusb_req *req = to_xusb_req(_req);
  981. struct xusb_ep *ep = to_xusb_ep(_ep);
  982. struct xusb_udc *udc = ep->udc;
  983. int ret;
  984. unsigned long flags;
  985. if (!ep->desc) {
  986. dev_dbg(udc->dev, "%s:queing request to disabled %s\n",
  987. __func__, ep->name);
  988. return -ESHUTDOWN;
  989. }
  990. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  991. dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
  992. return -EINVAL;
  993. }
  994. spin_lock_irqsave(&udc->lock, flags);
  995. _req->status = -EINPROGRESS;
  996. _req->actual = 0;
  997. if (udc->dma_enabled) {
  998. ret = usb_gadget_map_request(&udc->gadget, &req->usb_req,
  999. ep->is_in);
  1000. if (ret) {
  1001. dev_dbg(udc->dev, "gadget_map failed ep%d\n",
  1002. ep->epnumber);
  1003. spin_unlock_irqrestore(&udc->lock, flags);
  1004. return -EAGAIN;
  1005. }
  1006. }
  1007. if (list_empty(&ep->queue)) {
  1008. if (ep->is_in) {
  1009. dev_dbg(udc->dev, "xudc_write_fifo from ep_queue\n");
  1010. if (!xudc_write_fifo(ep, req))
  1011. req = NULL;
  1012. } else {
  1013. dev_dbg(udc->dev, "xudc_read_fifo from ep_queue\n");
  1014. if (!xudc_read_fifo(ep, req))
  1015. req = NULL;
  1016. }
  1017. }
  1018. if (req != NULL)
  1019. list_add_tail(&req->queue, &ep->queue);
  1020. spin_unlock_irqrestore(&udc->lock, flags);
  1021. return 0;
  1022. }
  1023. /**
  1024. * xudc_ep_dequeue - Removes the request from the queue.
  1025. * @_ep: pointer to the usb device endpoint structure.
  1026. * @_req: pointer to the usb request structure.
  1027. *
  1028. * Return: 0 for success and error value on failure
  1029. */
  1030. static int xudc_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1031. {
  1032. struct xusb_ep *ep = to_xusb_ep(_ep);
  1033. struct xusb_req *req = to_xusb_req(_req);
  1034. struct xusb_udc *udc = ep->udc;
  1035. unsigned long flags;
  1036. spin_lock_irqsave(&udc->lock, flags);
  1037. /* Make sure it's actually queued on this endpoint */
  1038. list_for_each_entry(req, &ep->queue, queue) {
  1039. if (&req->usb_req == _req)
  1040. break;
  1041. }
  1042. if (&req->usb_req != _req) {
  1043. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1044. return -EINVAL;
  1045. }
  1046. xudc_done(ep, req, -ECONNRESET);
  1047. spin_unlock_irqrestore(&udc->lock, flags);
  1048. return 0;
  1049. }
  1050. /**
  1051. * xudc_ep0_enable - Enables the given endpoint.
  1052. * @ep: pointer to the usb endpoint structure.
  1053. * @desc: pointer to usb endpoint descriptor.
  1054. *
  1055. * Return: error always.
  1056. *
  1057. * endpoint 0 enable should not be called by gadget layer.
  1058. */
  1059. static int xudc_ep0_enable(struct usb_ep *ep,
  1060. const struct usb_endpoint_descriptor *desc)
  1061. {
  1062. return -EINVAL;
  1063. }
  1064. /**
  1065. * xudc_ep0_disable - Disables the given endpoint.
  1066. * @ep: pointer to the usb endpoint structure.
  1067. *
  1068. * Return: error always.
  1069. *
  1070. * endpoint 0 disable should not be called by gadget layer.
  1071. */
  1072. static int xudc_ep0_disable(struct usb_ep *ep)
  1073. {
  1074. return -EINVAL;
  1075. }
  1076. static const struct usb_ep_ops xusb_ep0_ops = {
  1077. .enable = xudc_ep0_enable,
  1078. .disable = xudc_ep0_disable,
  1079. .alloc_request = xudc_ep_alloc_request,
  1080. .free_request = xudc_free_request,
  1081. .queue = xudc_ep0_queue,
  1082. .dequeue = xudc_ep_dequeue,
  1083. .set_halt = xudc_ep_set_halt,
  1084. };
  1085. static const struct usb_ep_ops xusb_ep_ops = {
  1086. .enable = xudc_ep_enable,
  1087. .disable = xudc_ep_disable,
  1088. .alloc_request = xudc_ep_alloc_request,
  1089. .free_request = xudc_free_request,
  1090. .queue = xudc_ep_queue,
  1091. .dequeue = xudc_ep_dequeue,
  1092. .set_halt = xudc_ep_set_halt,
  1093. };
  1094. /**
  1095. * xudc_get_frame - Reads the current usb frame number.
  1096. * @gadget: pointer to the usb gadget structure.
  1097. *
  1098. * Return: current frame number for success and error value on failure.
  1099. */
  1100. static int xudc_get_frame(struct usb_gadget *gadget)
  1101. {
  1102. struct xusb_udc *udc;
  1103. int frame;
  1104. if (!gadget)
  1105. return -ENODEV;
  1106. udc = to_udc(gadget);
  1107. frame = udc->read_fn(udc->addr + XUSB_FRAMENUM_OFFSET);
  1108. return frame;
  1109. }
  1110. /**
  1111. * xudc_wakeup - Send remote wakeup signal to host
  1112. * @gadget: pointer to the usb gadget structure.
  1113. *
  1114. * Return: 0 on success and error on failure
  1115. */
  1116. static int xudc_wakeup(struct usb_gadget *gadget)
  1117. {
  1118. struct xusb_udc *udc = to_udc(gadget);
  1119. u32 crtlreg;
  1120. int status = -EINVAL;
  1121. unsigned long flags;
  1122. spin_lock_irqsave(&udc->lock, flags);
  1123. /* Remote wake up not enabled by host */
  1124. if (!udc->remote_wkp)
  1125. goto done;
  1126. crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
  1127. crtlreg |= XUSB_CONTROL_USB_RMTWAKE_MASK;
  1128. /* set remote wake up bit */
  1129. udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
  1130. /*
  1131. * wait for a while and reset remote wake up bit since this bit
  1132. * is not cleared by HW after sending remote wakeup to host.
  1133. */
  1134. mdelay(2);
  1135. crtlreg &= ~XUSB_CONTROL_USB_RMTWAKE_MASK;
  1136. udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
  1137. status = 0;
  1138. done:
  1139. spin_unlock_irqrestore(&udc->lock, flags);
  1140. return status;
  1141. }
  1142. /**
  1143. * xudc_pullup - start/stop USB traffic
  1144. * @gadget: pointer to the usb gadget structure.
  1145. * @is_on: flag to start or stop
  1146. *
  1147. * Return: 0 always
  1148. *
  1149. * This function starts/stops SIE engine of IP based on is_on.
  1150. */
  1151. static int xudc_pullup(struct usb_gadget *gadget, int is_on)
  1152. {
  1153. struct xusb_udc *udc = to_udc(gadget);
  1154. unsigned long flags;
  1155. u32 crtlreg;
  1156. spin_lock_irqsave(&udc->lock, flags);
  1157. crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
  1158. if (is_on)
  1159. crtlreg |= XUSB_CONTROL_USB_READY_MASK;
  1160. else
  1161. crtlreg &= ~XUSB_CONTROL_USB_READY_MASK;
  1162. udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
  1163. spin_unlock_irqrestore(&udc->lock, flags);
  1164. return 0;
  1165. }
  1166. /**
  1167. * xudc_eps_init - initialize endpoints.
  1168. * @udc: pointer to the usb device controller structure.
  1169. */
  1170. static void xudc_eps_init(struct xusb_udc *udc)
  1171. {
  1172. u32 ep_number;
  1173. INIT_LIST_HEAD(&udc->gadget.ep_list);
  1174. for (ep_number = 0; ep_number < XUSB_MAX_ENDPOINTS; ep_number++) {
  1175. struct xusb_ep *ep = &udc->ep[ep_number];
  1176. if (ep_number) {
  1177. list_add_tail(&ep->ep_usb.ep_list,
  1178. &udc->gadget.ep_list);
  1179. usb_ep_set_maxpacket_limit(&ep->ep_usb,
  1180. (unsigned short) ~0);
  1181. snprintf(ep->name, EPNAME_SIZE, "ep%d", ep_number);
  1182. ep->ep_usb.name = ep->name;
  1183. ep->ep_usb.ops = &xusb_ep_ops;
  1184. ep->ep_usb.caps.type_iso = true;
  1185. ep->ep_usb.caps.type_bulk = true;
  1186. ep->ep_usb.caps.type_int = true;
  1187. } else {
  1188. ep->ep_usb.name = ep0name;
  1189. usb_ep_set_maxpacket_limit(&ep->ep_usb, EP0_MAX_PACKET);
  1190. ep->ep_usb.ops = &xusb_ep0_ops;
  1191. ep->ep_usb.caps.type_control = true;
  1192. }
  1193. ep->ep_usb.caps.dir_in = true;
  1194. ep->ep_usb.caps.dir_out = true;
  1195. ep->udc = udc;
  1196. ep->epnumber = ep_number;
  1197. ep->desc = NULL;
  1198. /*
  1199. * The configuration register address offset between
  1200. * each endpoint is 0x10.
  1201. */
  1202. ep->offset = XUSB_EP0_CONFIG_OFFSET + (ep_number * 0x10);
  1203. ep->is_in = 0;
  1204. ep->is_iso = 0;
  1205. ep->maxpacket = 0;
  1206. xudc_epconfig(ep, udc);
  1207. /* Initialize one queue per endpoint */
  1208. INIT_LIST_HEAD(&ep->queue);
  1209. }
  1210. }
  1211. /**
  1212. * xudc_stop_activity - Stops any further activity on the device.
  1213. * @udc: pointer to the usb device controller structure.
  1214. */
  1215. static void xudc_stop_activity(struct xusb_udc *udc)
  1216. {
  1217. int i;
  1218. struct xusb_ep *ep;
  1219. for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
  1220. ep = &udc->ep[i];
  1221. xudc_nuke(ep, -ESHUTDOWN);
  1222. }
  1223. }
  1224. /**
  1225. * xudc_start - Starts the device.
  1226. * @gadget: pointer to the usb gadget structure
  1227. * @driver: pointer to gadget driver structure
  1228. *
  1229. * Return: zero on success and error on failure
  1230. */
  1231. static int xudc_start(struct usb_gadget *gadget,
  1232. struct usb_gadget_driver *driver)
  1233. {
  1234. struct xusb_udc *udc = to_udc(gadget);
  1235. struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
  1236. const struct usb_endpoint_descriptor *desc = &config_bulk_out_desc;
  1237. unsigned long flags;
  1238. int ret = 0;
  1239. spin_lock_irqsave(&udc->lock, flags);
  1240. if (udc->driver) {
  1241. dev_err(udc->dev, "%s is already bound to %s\n",
  1242. udc->gadget.name, udc->driver->driver.name);
  1243. ret = -EBUSY;
  1244. goto err;
  1245. }
  1246. /* hook up the driver */
  1247. udc->driver = driver;
  1248. udc->gadget.speed = driver->max_speed;
  1249. /* Enable the control endpoint. */
  1250. ret = __xudc_ep_enable(ep0, desc);
  1251. /* Set device address and remote wakeup to 0 */
  1252. udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
  1253. udc->remote_wkp = 0;
  1254. err:
  1255. spin_unlock_irqrestore(&udc->lock, flags);
  1256. return ret;
  1257. }
  1258. /**
  1259. * xudc_stop - stops the device.
  1260. * @gadget: pointer to the usb gadget structure
  1261. * @driver: pointer to usb gadget driver structure
  1262. *
  1263. * Return: zero always
  1264. */
  1265. static int xudc_stop(struct usb_gadget *gadget)
  1266. {
  1267. struct xusb_udc *udc = to_udc(gadget);
  1268. unsigned long flags;
  1269. spin_lock_irqsave(&udc->lock, flags);
  1270. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1271. udc->driver = NULL;
  1272. /* Set device address and remote wakeup to 0 */
  1273. udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
  1274. udc->remote_wkp = 0;
  1275. xudc_stop_activity(udc);
  1276. spin_unlock_irqrestore(&udc->lock, flags);
  1277. return 0;
  1278. }
  1279. static const struct usb_gadget_ops xusb_udc_ops = {
  1280. .get_frame = xudc_get_frame,
  1281. .wakeup = xudc_wakeup,
  1282. .pullup = xudc_pullup,
  1283. .udc_start = xudc_start,
  1284. .udc_stop = xudc_stop,
  1285. };
  1286. /**
  1287. * xudc_clear_stall_all_ep - clears stall of every endpoint.
  1288. * @udc: pointer to the udc structure.
  1289. */
  1290. static void xudc_clear_stall_all_ep(struct xusb_udc *udc)
  1291. {
  1292. struct xusb_ep *ep;
  1293. u32 epcfgreg;
  1294. int i;
  1295. for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
  1296. ep = &udc->ep[i];
  1297. epcfgreg = udc->read_fn(udc->addr + ep->offset);
  1298. epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
  1299. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  1300. if (ep->epnumber) {
  1301. /* Reset the toggle bit.*/
  1302. epcfgreg = udc->read_fn(udc->addr + ep->offset);
  1303. epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
  1304. udc->write_fn(udc->addr, ep->offset, epcfgreg);
  1305. }
  1306. }
  1307. }
  1308. /**
  1309. * xudc_startup_handler - The usb device controller interrupt handler.
  1310. * @udc: pointer to the udc structure.
  1311. * @intrstatus: The mask value containing the interrupt sources.
  1312. *
  1313. * This function handles the RESET,SUSPEND,RESUME and DISCONNECT interrupts.
  1314. */
  1315. static void xudc_startup_handler(struct xusb_udc *udc, u32 intrstatus)
  1316. {
  1317. u32 intrreg;
  1318. if (intrstatus & XUSB_STATUS_RESET_MASK) {
  1319. dev_dbg(udc->dev, "Reset\n");
  1320. if (intrstatus & XUSB_STATUS_HIGH_SPEED_MASK)
  1321. udc->gadget.speed = USB_SPEED_HIGH;
  1322. else
  1323. udc->gadget.speed = USB_SPEED_FULL;
  1324. xudc_stop_activity(udc);
  1325. xudc_clear_stall_all_ep(udc);
  1326. udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
  1327. /* Set device address and remote wakeup to 0 */
  1328. udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
  1329. udc->remote_wkp = 0;
  1330. /* Enable the suspend, resume and disconnect */
  1331. intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1332. intrreg |= XUSB_STATUS_SUSPEND_MASK | XUSB_STATUS_RESUME_MASK |
  1333. XUSB_STATUS_DISCONNECT_MASK;
  1334. udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
  1335. }
  1336. if (intrstatus & XUSB_STATUS_SUSPEND_MASK) {
  1337. dev_dbg(udc->dev, "Suspend\n");
  1338. /* Enable the reset, resume and disconnect */
  1339. intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1340. intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
  1341. XUSB_STATUS_DISCONNECT_MASK;
  1342. udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
  1343. udc->usb_state = USB_STATE_SUSPENDED;
  1344. if (udc->driver->suspend) {
  1345. spin_unlock(&udc->lock);
  1346. udc->driver->suspend(&udc->gadget);
  1347. spin_lock(&udc->lock);
  1348. }
  1349. }
  1350. if (intrstatus & XUSB_STATUS_RESUME_MASK) {
  1351. bool condition = (udc->usb_state != USB_STATE_SUSPENDED);
  1352. dev_WARN_ONCE(udc->dev, condition,
  1353. "Resume IRQ while not suspended\n");
  1354. dev_dbg(udc->dev, "Resume\n");
  1355. /* Enable the reset, suspend and disconnect */
  1356. intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1357. intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_SUSPEND_MASK |
  1358. XUSB_STATUS_DISCONNECT_MASK;
  1359. udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
  1360. udc->usb_state = 0;
  1361. if (udc->driver->resume) {
  1362. spin_unlock(&udc->lock);
  1363. udc->driver->resume(&udc->gadget);
  1364. spin_lock(&udc->lock);
  1365. }
  1366. }
  1367. if (intrstatus & XUSB_STATUS_DISCONNECT_MASK) {
  1368. dev_dbg(udc->dev, "Disconnect\n");
  1369. /* Enable the reset, resume and suspend */
  1370. intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1371. intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
  1372. XUSB_STATUS_SUSPEND_MASK;
  1373. udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
  1374. if (udc->driver && udc->driver->disconnect) {
  1375. spin_unlock(&udc->lock);
  1376. udc->driver->disconnect(&udc->gadget);
  1377. spin_lock(&udc->lock);
  1378. }
  1379. }
  1380. }
  1381. /**
  1382. * xudc_ep0_stall - Stall endpoint zero.
  1383. * @udc: pointer to the udc structure.
  1384. *
  1385. * This function stalls endpoint zero.
  1386. */
  1387. static void xudc_ep0_stall(struct xusb_udc *udc)
  1388. {
  1389. u32 epcfgreg;
  1390. struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
  1391. epcfgreg = udc->read_fn(udc->addr + ep0->offset);
  1392. epcfgreg |= XUSB_EP_CFG_STALL_MASK;
  1393. udc->write_fn(udc->addr, ep0->offset, epcfgreg);
  1394. }
  1395. /**
  1396. * xudc_setaddress - executes SET_ADDRESS command
  1397. * @udc: pointer to the udc structure.
  1398. *
  1399. * This function executes USB SET_ADDRESS command
  1400. */
  1401. static void xudc_setaddress(struct xusb_udc *udc)
  1402. {
  1403. struct xusb_ep *ep0 = &udc->ep[0];
  1404. struct xusb_req *req = udc->req;
  1405. int ret;
  1406. req->usb_req.length = 0;
  1407. ret = __xudc_ep0_queue(ep0, req);
  1408. if (ret == 0)
  1409. return;
  1410. dev_err(udc->dev, "Can't respond to SET ADDRESS request\n");
  1411. xudc_ep0_stall(udc);
  1412. }
  1413. /**
  1414. * xudc_getstatus - executes GET_STATUS command
  1415. * @udc: pointer to the udc structure.
  1416. *
  1417. * This function executes USB GET_STATUS command
  1418. */
  1419. static void xudc_getstatus(struct xusb_udc *udc)
  1420. {
  1421. struct xusb_ep *ep0 = &udc->ep[0];
  1422. struct xusb_req *req = udc->req;
  1423. struct xusb_ep *target_ep;
  1424. u16 status = 0;
  1425. u32 epcfgreg;
  1426. int epnum;
  1427. u32 halt;
  1428. int ret;
  1429. switch (udc->setup.bRequestType & USB_RECIP_MASK) {
  1430. case USB_RECIP_DEVICE:
  1431. /* Get device status */
  1432. status = 1 << USB_DEVICE_SELF_POWERED;
  1433. if (udc->remote_wkp)
  1434. status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  1435. break;
  1436. case USB_RECIP_INTERFACE:
  1437. break;
  1438. case USB_RECIP_ENDPOINT:
  1439. epnum = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
  1440. target_ep = &udc->ep[epnum];
  1441. epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
  1442. halt = epcfgreg & XUSB_EP_CFG_STALL_MASK;
  1443. if (udc->setup.wIndex & USB_DIR_IN) {
  1444. if (!target_ep->is_in)
  1445. goto stall;
  1446. } else {
  1447. if (target_ep->is_in)
  1448. goto stall;
  1449. }
  1450. if (halt)
  1451. status = 1 << USB_ENDPOINT_HALT;
  1452. break;
  1453. default:
  1454. goto stall;
  1455. }
  1456. req->usb_req.length = 2;
  1457. *(u16 *)req->usb_req.buf = cpu_to_le16(status);
  1458. ret = __xudc_ep0_queue(ep0, req);
  1459. if (ret == 0)
  1460. return;
  1461. stall:
  1462. dev_err(udc->dev, "Can't respond to getstatus request\n");
  1463. xudc_ep0_stall(udc);
  1464. }
  1465. /**
  1466. * xudc_set_clear_feature - Executes the set feature and clear feature commands.
  1467. * @udc: pointer to the usb device controller structure.
  1468. *
  1469. * Processes the SET_FEATURE and CLEAR_FEATURE commands.
  1470. */
  1471. static void xudc_set_clear_feature(struct xusb_udc *udc)
  1472. {
  1473. struct xusb_ep *ep0 = &udc->ep[0];
  1474. struct xusb_req *req = udc->req;
  1475. struct xusb_ep *target_ep;
  1476. u8 endpoint;
  1477. u8 outinbit;
  1478. u32 epcfgreg;
  1479. int flag = (udc->setup.bRequest == USB_REQ_SET_FEATURE ? 1 : 0);
  1480. int ret;
  1481. switch (udc->setup.bRequestType) {
  1482. case USB_RECIP_DEVICE:
  1483. switch (udc->setup.wValue) {
  1484. case USB_DEVICE_TEST_MODE:
  1485. /*
  1486. * The Test Mode will be executed
  1487. * after the status phase.
  1488. */
  1489. break;
  1490. case USB_DEVICE_REMOTE_WAKEUP:
  1491. if (flag)
  1492. udc->remote_wkp = 1;
  1493. else
  1494. udc->remote_wkp = 0;
  1495. break;
  1496. default:
  1497. xudc_ep0_stall(udc);
  1498. break;
  1499. }
  1500. break;
  1501. case USB_RECIP_ENDPOINT:
  1502. if (!udc->setup.wValue) {
  1503. endpoint = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
  1504. target_ep = &udc->ep[endpoint];
  1505. outinbit = udc->setup.wIndex & USB_ENDPOINT_DIR_MASK;
  1506. outinbit = outinbit >> 7;
  1507. /* Make sure direction matches.*/
  1508. if (outinbit != target_ep->is_in) {
  1509. xudc_ep0_stall(udc);
  1510. return;
  1511. }
  1512. epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
  1513. if (!endpoint) {
  1514. /* Clear the stall.*/
  1515. epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
  1516. udc->write_fn(udc->addr,
  1517. target_ep->offset, epcfgreg);
  1518. } else {
  1519. if (flag) {
  1520. epcfgreg |= XUSB_EP_CFG_STALL_MASK;
  1521. udc->write_fn(udc->addr,
  1522. target_ep->offset,
  1523. epcfgreg);
  1524. } else {
  1525. /* Unstall the endpoint.*/
  1526. epcfgreg &= ~(XUSB_EP_CFG_STALL_MASK |
  1527. XUSB_EP_CFG_DATA_TOGGLE_MASK);
  1528. udc->write_fn(udc->addr,
  1529. target_ep->offset,
  1530. epcfgreg);
  1531. }
  1532. }
  1533. }
  1534. break;
  1535. default:
  1536. xudc_ep0_stall(udc);
  1537. return;
  1538. }
  1539. req->usb_req.length = 0;
  1540. ret = __xudc_ep0_queue(ep0, req);
  1541. if (ret == 0)
  1542. return;
  1543. dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n");
  1544. xudc_ep0_stall(udc);
  1545. }
  1546. /**
  1547. * xudc_handle_setup - Processes the setup packet.
  1548. * @udc: pointer to the usb device controller structure.
  1549. *
  1550. * Process setup packet and delegate to gadget layer.
  1551. */
  1552. static void xudc_handle_setup(struct xusb_udc *udc)
  1553. {
  1554. struct xusb_ep *ep0 = &udc->ep[0];
  1555. struct usb_ctrlrequest setup;
  1556. u32 *ep0rambase;
  1557. /* Load up the chapter 9 command buffer.*/
  1558. ep0rambase = (u32 __force *) (udc->addr + XUSB_SETUP_PKT_ADDR_OFFSET);
  1559. memcpy(&setup, ep0rambase, 8);
  1560. udc->setup = setup;
  1561. udc->setup.wValue = cpu_to_le16(setup.wValue);
  1562. udc->setup.wIndex = cpu_to_le16(setup.wIndex);
  1563. udc->setup.wLength = cpu_to_le16(setup.wLength);
  1564. /* Clear previous requests */
  1565. xudc_nuke(ep0, -ECONNRESET);
  1566. if (udc->setup.bRequestType & USB_DIR_IN) {
  1567. /* Execute the get command.*/
  1568. udc->setupseqrx = STATUS_PHASE;
  1569. udc->setupseqtx = DATA_PHASE;
  1570. } else {
  1571. /* Execute the put command.*/
  1572. udc->setupseqrx = DATA_PHASE;
  1573. udc->setupseqtx = STATUS_PHASE;
  1574. }
  1575. switch (udc->setup.bRequest) {
  1576. case USB_REQ_GET_STATUS:
  1577. /* Data+Status phase form udc */
  1578. if ((udc->setup.bRequestType &
  1579. (USB_DIR_IN | USB_TYPE_MASK)) !=
  1580. (USB_DIR_IN | USB_TYPE_STANDARD))
  1581. break;
  1582. xudc_getstatus(udc);
  1583. return;
  1584. case USB_REQ_SET_ADDRESS:
  1585. /* Status phase from udc */
  1586. if (udc->setup.bRequestType != (USB_DIR_OUT |
  1587. USB_TYPE_STANDARD | USB_RECIP_DEVICE))
  1588. break;
  1589. xudc_setaddress(udc);
  1590. return;
  1591. case USB_REQ_CLEAR_FEATURE:
  1592. case USB_REQ_SET_FEATURE:
  1593. /* Requests with no data phase, status phase from udc */
  1594. if ((udc->setup.bRequestType & USB_TYPE_MASK)
  1595. != USB_TYPE_STANDARD)
  1596. break;
  1597. xudc_set_clear_feature(udc);
  1598. return;
  1599. default:
  1600. break;
  1601. }
  1602. spin_unlock(&udc->lock);
  1603. if (udc->driver->setup(&udc->gadget, &setup) < 0)
  1604. xudc_ep0_stall(udc);
  1605. spin_lock(&udc->lock);
  1606. }
  1607. /**
  1608. * xudc_ep0_out - Processes the endpoint 0 OUT token.
  1609. * @udc: pointer to the usb device controller structure.
  1610. */
  1611. static void xudc_ep0_out(struct xusb_udc *udc)
  1612. {
  1613. struct xusb_ep *ep0 = &udc->ep[0];
  1614. struct xusb_req *req;
  1615. u8 *ep0rambase;
  1616. unsigned int bytes_to_rx;
  1617. void *buffer;
  1618. req = list_first_entry(&ep0->queue, struct xusb_req, queue);
  1619. switch (udc->setupseqrx) {
  1620. case STATUS_PHASE:
  1621. /*
  1622. * This resets both state machines for the next
  1623. * Setup packet.
  1624. */
  1625. udc->setupseqrx = SETUP_PHASE;
  1626. udc->setupseqtx = SETUP_PHASE;
  1627. req->usb_req.actual = req->usb_req.length;
  1628. xudc_done(ep0, req, 0);
  1629. break;
  1630. case DATA_PHASE:
  1631. bytes_to_rx = udc->read_fn(udc->addr +
  1632. XUSB_EP_BUF0COUNT_OFFSET);
  1633. /* Copy the data to be received from the DPRAM. */
  1634. ep0rambase = (u8 __force *) (udc->addr +
  1635. (ep0->rambase << 2));
  1636. buffer = req->usb_req.buf + req->usb_req.actual;
  1637. req->usb_req.actual = req->usb_req.actual + bytes_to_rx;
  1638. memcpy(buffer, ep0rambase, bytes_to_rx);
  1639. if (req->usb_req.length == req->usb_req.actual) {
  1640. /* Data transfer completed get ready for Status stage */
  1641. xudc_wrstatus(udc);
  1642. } else {
  1643. /* Enable EP0 buffer to receive data */
  1644. udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
  1645. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
  1646. }
  1647. break;
  1648. default:
  1649. break;
  1650. }
  1651. }
  1652. /**
  1653. * xudc_ep0_in - Processes the endpoint 0 IN token.
  1654. * @udc: pointer to the usb device controller structure.
  1655. */
  1656. static void xudc_ep0_in(struct xusb_udc *udc)
  1657. {
  1658. struct xusb_ep *ep0 = &udc->ep[0];
  1659. struct xusb_req *req;
  1660. unsigned int bytes_to_tx;
  1661. void *buffer;
  1662. u32 epcfgreg;
  1663. u16 count = 0;
  1664. u16 length;
  1665. u8 *ep0rambase;
  1666. u8 test_mode = udc->setup.wIndex >> 8;
  1667. req = list_first_entry(&ep0->queue, struct xusb_req, queue);
  1668. bytes_to_tx = req->usb_req.length - req->usb_req.actual;
  1669. switch (udc->setupseqtx) {
  1670. case STATUS_PHASE:
  1671. switch (udc->setup.bRequest) {
  1672. case USB_REQ_SET_ADDRESS:
  1673. /* Set the address of the device.*/
  1674. udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET,
  1675. udc->setup.wValue);
  1676. break;
  1677. case USB_REQ_SET_FEATURE:
  1678. if (udc->setup.bRequestType ==
  1679. USB_RECIP_DEVICE) {
  1680. if (udc->setup.wValue ==
  1681. USB_DEVICE_TEST_MODE)
  1682. udc->write_fn(udc->addr,
  1683. XUSB_TESTMODE_OFFSET,
  1684. test_mode);
  1685. }
  1686. break;
  1687. }
  1688. req->usb_req.actual = req->usb_req.length;
  1689. xudc_done(ep0, req, 0);
  1690. break;
  1691. case DATA_PHASE:
  1692. if (!bytes_to_tx) {
  1693. /*
  1694. * We're done with data transfer, next
  1695. * will be zero length OUT with data toggle of
  1696. * 1. Setup data_toggle.
  1697. */
  1698. epcfgreg = udc->read_fn(udc->addr + ep0->offset);
  1699. epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
  1700. udc->write_fn(udc->addr, ep0->offset, epcfgreg);
  1701. udc->setupseqtx = STATUS_PHASE;
  1702. } else {
  1703. length = count = min_t(u32, bytes_to_tx,
  1704. EP0_MAX_PACKET);
  1705. /* Copy the data to be transmitted into the DPRAM. */
  1706. ep0rambase = (u8 __force *) (udc->addr +
  1707. (ep0->rambase << 2));
  1708. buffer = req->usb_req.buf + req->usb_req.actual;
  1709. req->usb_req.actual = req->usb_req.actual + length;
  1710. memcpy(ep0rambase, buffer, length);
  1711. }
  1712. udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, count);
  1713. udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
  1714. break;
  1715. default:
  1716. break;
  1717. }
  1718. }
  1719. /**
  1720. * xudc_ctrl_ep_handler - Endpoint 0 interrupt handler.
  1721. * @udc: pointer to the udc structure.
  1722. * @intrstatus: It's the mask value for the interrupt sources on endpoint 0.
  1723. *
  1724. * Processes the commands received during enumeration phase.
  1725. */
  1726. static void xudc_ctrl_ep_handler(struct xusb_udc *udc, u32 intrstatus)
  1727. {
  1728. if (intrstatus & XUSB_STATUS_SETUP_PACKET_MASK) {
  1729. xudc_handle_setup(udc);
  1730. } else {
  1731. if (intrstatus & XUSB_STATUS_FIFO_BUFF_RDY_MASK)
  1732. xudc_ep0_out(udc);
  1733. else if (intrstatus & XUSB_STATUS_FIFO_BUFF_FREE_MASK)
  1734. xudc_ep0_in(udc);
  1735. }
  1736. }
  1737. /**
  1738. * xudc_nonctrl_ep_handler - Non control endpoint interrupt handler.
  1739. * @udc: pointer to the udc structure.
  1740. * @epnum: End point number for which the interrupt is to be processed
  1741. * @intrstatus: mask value for interrupt sources of endpoints other
  1742. * than endpoint 0.
  1743. *
  1744. * Processes the buffer completion interrupts.
  1745. */
  1746. static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
  1747. u32 intrstatus)
  1748. {
  1749. struct xusb_req *req;
  1750. struct xusb_ep *ep;
  1751. ep = &udc->ep[epnum];
  1752. /* Process the End point interrupts.*/
  1753. if (intrstatus & (XUSB_STATUS_EP0_BUFF1_COMP_MASK << epnum))
  1754. ep->buffer0ready = 0;
  1755. if (intrstatus & (XUSB_STATUS_EP0_BUFF2_COMP_MASK << epnum))
  1756. ep->buffer1ready = 0;
  1757. if (list_empty(&ep->queue))
  1758. return;
  1759. req = list_first_entry(&ep->queue, struct xusb_req, queue);
  1760. if (ep->is_in)
  1761. xudc_write_fifo(ep, req);
  1762. else
  1763. xudc_read_fifo(ep, req);
  1764. }
  1765. /**
  1766. * xudc_irq - The main interrupt handler.
  1767. * @irq: The interrupt number.
  1768. * @_udc: pointer to the usb device controller structure.
  1769. *
  1770. * Return: IRQ_HANDLED after the interrupt is handled.
  1771. */
  1772. static irqreturn_t xudc_irq(int irq, void *_udc)
  1773. {
  1774. struct xusb_udc *udc = _udc;
  1775. u32 intrstatus;
  1776. u32 ier;
  1777. u8 index;
  1778. u32 bufintr;
  1779. unsigned long flags;
  1780. spin_lock_irqsave(&udc->lock, flags);
  1781. /*
  1782. * Event interrupts are level sensitive hence first disable
  1783. * IER, read ISR and figure out active interrupts.
  1784. */
  1785. ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1786. ier &= ~XUSB_STATUS_INTR_EVENT_MASK;
  1787. udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
  1788. /* Read the Interrupt Status Register.*/
  1789. intrstatus = udc->read_fn(udc->addr + XUSB_STATUS_OFFSET);
  1790. /* Call the handler for the event interrupt.*/
  1791. if (intrstatus & XUSB_STATUS_INTR_EVENT_MASK) {
  1792. /*
  1793. * Check if there is any action to be done for :
  1794. * - USB Reset received {XUSB_STATUS_RESET_MASK}
  1795. * - USB Suspend received {XUSB_STATUS_SUSPEND_MASK}
  1796. * - USB Resume received {XUSB_STATUS_RESUME_MASK}
  1797. * - USB Disconnect received {XUSB_STATUS_DISCONNECT_MASK}
  1798. */
  1799. xudc_startup_handler(udc, intrstatus);
  1800. }
  1801. /* Check the buffer completion interrupts */
  1802. if (intrstatus & XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK) {
  1803. /* Enable Reset, Suspend, Resume and Disconnect */
  1804. ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
  1805. ier |= XUSB_STATUS_INTR_EVENT_MASK;
  1806. udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
  1807. if (intrstatus & XUSB_STATUS_EP0_BUFF1_COMP_MASK)
  1808. xudc_ctrl_ep_handler(udc, intrstatus);
  1809. for (index = 1; index < 8; index++) {
  1810. bufintr = ((intrstatus &
  1811. (XUSB_STATUS_EP1_BUFF1_COMP_MASK <<
  1812. (index - 1))) || (intrstatus &
  1813. (XUSB_STATUS_EP1_BUFF2_COMP_MASK <<
  1814. (index - 1))));
  1815. if (bufintr) {
  1816. xudc_nonctrl_ep_handler(udc, index,
  1817. intrstatus);
  1818. }
  1819. }
  1820. }
  1821. spin_unlock_irqrestore(&udc->lock, flags);
  1822. return IRQ_HANDLED;
  1823. }
  1824. /**
  1825. * xudc_probe - The device probe function for driver initialization.
  1826. * @pdev: pointer to the platform device structure.
  1827. *
  1828. * Return: 0 for success and error value on failure
  1829. */
  1830. static int xudc_probe(struct platform_device *pdev)
  1831. {
  1832. struct device_node *np = pdev->dev.of_node;
  1833. struct resource *res;
  1834. struct xusb_udc *udc;
  1835. struct xusb_ep *ep0;
  1836. int irq;
  1837. int ret;
  1838. u32 ier;
  1839. u8 *buff;
  1840. udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
  1841. if (!udc)
  1842. return -ENOMEM;
  1843. /* Create a dummy request for GET_STATUS, SET_ADDRESS */
  1844. udc->req = devm_kzalloc(&pdev->dev, sizeof(struct xusb_req),
  1845. GFP_KERNEL);
  1846. if (!udc->req)
  1847. return -ENOMEM;
  1848. buff = devm_kzalloc(&pdev->dev, STATUSBUFF_SIZE, GFP_KERNEL);
  1849. if (!buff)
  1850. return -ENOMEM;
  1851. udc->req->usb_req.buf = buff;
  1852. /* Map the registers */
  1853. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1854. udc->addr = devm_ioremap_resource(&pdev->dev, res);
  1855. if (IS_ERR(udc->addr))
  1856. return PTR_ERR(udc->addr);
  1857. irq = platform_get_irq(pdev, 0);
  1858. if (irq < 0) {
  1859. dev_err(&pdev->dev, "unable to get irq\n");
  1860. return irq;
  1861. }
  1862. ret = devm_request_irq(&pdev->dev, irq, xudc_irq, 0,
  1863. dev_name(&pdev->dev), udc);
  1864. if (ret < 0) {
  1865. dev_dbg(&pdev->dev, "unable to request irq %d", irq);
  1866. goto fail;
  1867. }
  1868. udc->dma_enabled = of_property_read_bool(np, "xlnx,has-builtin-dma");
  1869. /* Setup gadget structure */
  1870. udc->gadget.ops = &xusb_udc_ops;
  1871. udc->gadget.max_speed = USB_SPEED_HIGH;
  1872. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1873. udc->gadget.ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO].ep_usb;
  1874. udc->gadget.name = driver_name;
  1875. spin_lock_init(&udc->lock);
  1876. /* Check for IP endianness */
  1877. udc->write_fn = xudc_write32_be;
  1878. udc->read_fn = xudc_read32_be;
  1879. udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, TEST_J);
  1880. if ((udc->read_fn(udc->addr + XUSB_TESTMODE_OFFSET))
  1881. != TEST_J) {
  1882. udc->write_fn = xudc_write32;
  1883. udc->read_fn = xudc_read32;
  1884. }
  1885. udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
  1886. xudc_eps_init(udc);
  1887. ep0 = &udc->ep[0];
  1888. /* Set device address to 0.*/
  1889. udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
  1890. ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  1891. if (ret)
  1892. goto fail;
  1893. udc->dev = &udc->gadget.dev;
  1894. /* Enable the interrupts.*/
  1895. ier = XUSB_STATUS_GLOBAL_INTR_MASK | XUSB_STATUS_INTR_EVENT_MASK |
  1896. XUSB_STATUS_FIFO_BUFF_RDY_MASK | XUSB_STATUS_FIFO_BUFF_FREE_MASK |
  1897. XUSB_STATUS_SETUP_PACKET_MASK |
  1898. XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK;
  1899. udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
  1900. platform_set_drvdata(pdev, udc);
  1901. dev_vdbg(&pdev->dev, "%s at 0x%08X mapped to %p %s\n",
  1902. driver_name, (u32)res->start, udc->addr,
  1903. udc->dma_enabled ? "with DMA" : "without DMA");
  1904. return 0;
  1905. fail:
  1906. dev_err(&pdev->dev, "probe failed, %d\n", ret);
  1907. return ret;
  1908. }
  1909. /**
  1910. * xudc_remove - Releases the resources allocated during the initialization.
  1911. * @pdev: pointer to the platform device structure.
  1912. *
  1913. * Return: 0 always
  1914. */
  1915. static int xudc_remove(struct platform_device *pdev)
  1916. {
  1917. struct xusb_udc *udc = platform_get_drvdata(pdev);
  1918. usb_del_gadget_udc(&udc->gadget);
  1919. return 0;
  1920. }
  1921. /* Match table for of_platform binding */
  1922. static const struct of_device_id usb_of_match[] = {
  1923. { .compatible = "xlnx,usb2-device-4.00.a", },
  1924. { /* end of list */ },
  1925. };
  1926. MODULE_DEVICE_TABLE(of, usb_of_match);
  1927. static struct platform_driver xudc_driver = {
  1928. .driver = {
  1929. .name = driver_name,
  1930. .of_match_table = usb_of_match,
  1931. },
  1932. .probe = xudc_probe,
  1933. .remove = xudc_remove,
  1934. };
  1935. module_platform_driver(xudc_driver);
  1936. MODULE_DESCRIPTION("Xilinx udc driver");
  1937. MODULE_AUTHOR("Xilinx, Inc");
  1938. MODULE_LICENSE("GPL");